2 * linux/fs/nfs/pagelist.c
4 * A set of helper functions for managing NFS read and write requests.
5 * The main purpose of these routines is to provide support for the
6 * coalescing of several requests into a single RPC call.
8 * Copyright 2000, 2001 (c) Trond Myklebust <trond.myklebust@fys.uio.no>
12 #include <linux/config.h>
13 #include <linux/slab.h>
14 #include <linux/file.h>
15 #include <linux/sunrpc/clnt.h>
16 #include <linux/nfs3.h>
17 #include <linux/nfs_page.h>
18 #include <linux/nfs_fs.h>
19 #include <linux/nfs_flushd.h>
20 #include <linux/nfs_mount.h>
22 #define NFS_PARANOIA 1
27 spinlock_t nfs_wreq_lock = SPIN_LOCK_UNLOCKED;
29 static kmem_cache_t *nfs_page_cachep;
31 static inline struct nfs_page *
35 p = kmem_cache_alloc(nfs_page_cachep, SLAB_NOFS);
37 memset(p, 0, sizeof(*p));
38 INIT_LIST_HEAD(&p->wb_hash);
39 INIT_LIST_HEAD(&p->wb_list);
40 INIT_LIST_HEAD(&p->wb_lru);
41 init_waitqueue_head(&p->wb_wait);
47 nfs_page_free(struct nfs_page *p)
49 kmem_cache_free(nfs_page_cachep, p);
52 static int nfs_try_to_free_pages(struct nfs_server *);
55 * nfs_create_request - Create an NFS read/write request.
56 * @cred: RPC credential to use
57 * @inode: inode to which the request is attached
58 * @page: page to write
59 * @offset: starting offset within the page for the write
60 * @count: number of bytes to read/write
62 * The page must be locked by the caller. This makes sure we never
63 * create two different requests for the same page, and avoids
64 * a possible deadlock when we reach the hard limit on the number
66 * User should ensure it is safe to sleep in this function.
69 nfs_create_request(struct rpc_cred *cred, struct inode *inode,
71 unsigned int offset, unsigned int count)
73 struct nfs_server *server = NFS_SERVER(inode);
74 struct nfs_reqlist *cache = NFS_REQUESTLIST(inode);
77 /* Deal with hard limits. */
79 /* Prevent races by incrementing *before* we test */
80 atomic_inc(&cache->nr_requests);
82 /* If we haven't reached the local hard limit yet,
83 * try to allocate the request struct */
84 if (atomic_read(&cache->nr_requests) <= MAX_REQUEST_HARD) {
85 req = nfs_page_alloc();
90 atomic_dec(&cache->nr_requests);
92 /* Try to free up at least one request in order to stay
93 * below the hard limit
95 if (nfs_try_to_free_pages(server))
97 if (signalled() && (server->flags & NFS_MOUNT_INTR))
98 return ERR_PTR(-ERESTARTSYS);
102 /* Initialize the request struct. Initially, we assume a
103 * long write-back delay. This will be adjusted in
104 * update_nfs_request below if the region is not locked. */
106 page_cache_get(page);
107 req->wb_offset = offset;
108 req->wb_bytes = count;
111 req->wb_cred = get_rpccred(cred);
112 req->wb_inode = inode;
119 * nfs_clear_request - Free up all resources allocated to the request
122 * Release all resources associated with a write request after it
125 void nfs_clear_request(struct nfs_page *req)
127 /* Release struct file or cached credential */
133 put_rpccred(req->wb_cred);
137 atomic_dec(&NFS_REQUESTLIST(req->wb_inode)->nr_requests);
139 BUG_ON(atomic_read(&NFS_REQUESTLIST(req->wb_inode)->nr_requests) < 0);
141 page_cache_release(req->wb_page);
148 * nfs_release_request - Release the count on an NFS read/write request
149 * @req: request to release
151 * Note: Should never be called with the spinlock held!
154 nfs_release_request(struct nfs_page *req)
156 spin_lock(&nfs_wreq_lock);
157 if (--req->wb_count) {
158 spin_unlock(&nfs_wreq_lock);
162 spin_unlock(&nfs_wreq_lock);
165 BUG_ON(!list_empty(&req->wb_list));
166 BUG_ON(!list_empty(&req->wb_hash));
167 BUG_ON(NFS_WBACK_BUSY(req));
170 /* Release struct file or cached credential */
171 nfs_clear_request(req);
176 * nfs_list_add_request - Insert a request into a sorted list
178 * @head: head of list into which to insert the request.
180 * Note that the wb_list is sorted by page index in order to facilitate
181 * coalescing of requests.
182 * We use an insertion sort that is optimized for the case of appended
186 nfs_list_add_request(struct nfs_page *req, struct list_head *head)
188 struct list_head *pos;
189 unsigned long pg_idx = page_index(req->wb_page);
192 if (!list_empty(&req->wb_list)) {
193 printk(KERN_ERR "NFS: Add to list failed!\n");
197 list_for_each_prev(pos, head) {
198 struct nfs_page *p = nfs_list_entry(pos);
199 if (page_index(p->wb_page) < pg_idx)
202 list_add(&req->wb_list, pos);
203 req->wb_list_head = head;
207 * nfs_wait_on_request - Wait for a request to complete.
208 * @req: request to wait upon.
210 * Interruptible by signals only if mounted with intr flag.
211 * The user is responsible for holding a count on the request.
214 nfs_wait_on_request(struct nfs_page *req)
216 struct inode *inode = req->wb_inode;
217 struct rpc_clnt *clnt = NFS_CLIENT(inode);
219 if (!NFS_WBACK_BUSY(req))
221 return nfs_wait_event(clnt, req->wb_wait, !NFS_WBACK_BUSY(req));
225 * nfs_coalesce_requests - Split coalesced requests out from a list.
227 * @dst: destination list
228 * @nmax: maximum number of requests to coalesce
230 * Moves a maximum of 'nmax' elements from one list to another.
231 * The elements are checked to ensure that they form a contiguous set
232 * of pages, and that they originated from the same file.
235 nfs_coalesce_requests(struct list_head *head, struct list_head *dst,
238 struct nfs_page *req = NULL;
239 unsigned int npages = 0;
241 while (!list_empty(head)) {
242 struct nfs_page *prev = req;
244 req = nfs_list_entry(head->next);
246 if (req->wb_cred != prev->wb_cred)
248 if (page_index(req->wb_page) != page_index(prev->wb_page)+1)
251 if (req->wb_offset != 0)
254 nfs_list_remove_request(req);
255 nfs_list_add_request(req, dst);
257 if (req->wb_offset + req->wb_bytes != PAGE_CACHE_SIZE)
266 * nfs_scan_forward - Coalesce more requests
267 * @req: First request to add
268 * @dst: destination list
269 * @nmax: maximum number of requests to coalesce
271 * Tries to coalesce more requests by traversing the request's wb_list.
272 * Moves the resulting list into dst. Requests are guaranteed to be
273 * contiguous, and to originate from the same file.
276 nfs_scan_forward(struct nfs_page *req, struct list_head *dst, int nmax)
278 struct nfs_server *server = NFS_SERVER(req->wb_inode);
279 struct list_head *pos, *head = req->wb_list_head;
280 struct rpc_cred *cred = req->wb_cred;
281 unsigned long idx = page_index(req->wb_page) + 1;
284 for (pos = req->wb_list.next; nfs_lock_request(req); pos = pos->next) {
285 nfs_list_remove_request(req);
286 nfs_list_add_request(req, dst);
288 __nfs_add_lru(&server->lru_busy, req);
294 if (req->wb_offset + req->wb_bytes != PAGE_CACHE_SIZE)
296 req = nfs_list_entry(pos);
297 if (page_index(req->wb_page) != idx++)
299 if (req->wb_offset != 0)
301 if (req->wb_cred != cred)
308 * nfs_scan_lru - Scan one of the least recently used list
309 * @head: One of the NFS superblock lru lists
310 * @dst: Destination list
311 * @nmax: maximum number of requests to coalesce
313 * Scans one of the NFS superblock lru lists for upto nmax requests
314 * and returns them on a list. The requests are all guaranteed to be
315 * contiguous, originating from the same inode and the same file.
318 nfs_scan_lru(struct list_head *head, struct list_head *dst, int nmax)
320 struct list_head *pos;
321 struct nfs_page *req;
324 list_for_each(pos, head) {
325 req = nfs_lru_entry(pos);
326 npages = nfs_scan_forward(req, dst, nmax);
334 * nfs_scan_lru_timeout - Scan one of the superblock lru lists for timed out requests
335 * @head: One of the NFS superblock lru lists
336 * @dst: Destination list
337 * @nmax: maximum number of requests to coalesce
339 * Scans one of the NFS superblock lru lists for upto nmax requests
340 * and returns them on a list. The requests are all guaranteed to be
341 * contiguous, originating from the same inode and the same file.
342 * The first request on the destination list will be timed out, the
343 * others are not guaranteed to be so.
346 nfs_scan_lru_timeout(struct list_head *head, struct list_head *dst, int nmax)
348 struct list_head *pos;
349 struct nfs_page *req;
352 list_for_each(pos, head) {
353 req = nfs_lru_entry(pos);
354 if (time_after(req->wb_timeout, jiffies))
356 npages = nfs_scan_forward(req, dst, nmax);
364 * nfs_scan_list - Scan a list for matching requests
365 * @head: One of the NFS inode request lists
366 * @dst: Destination list
367 * @idx_start: lower bound of page->index to scan
368 * @npages: idx_start + npages sets the upper bound to scan.
370 * Moves elements from one of the inode request lists.
371 * If the number of requests is set to 0, the entire address_space
372 * starting at index idx_start, is scanned.
373 * The requests are *not* checked to ensure that they form a contiguous set.
374 * You must be holding the nfs_wreq_lock when calling this function
377 nfs_scan_list(struct list_head *head, struct list_head *dst,
378 unsigned long idx_start, unsigned int npages)
380 struct list_head *pos, *tmp;
381 struct nfs_page *req;
382 unsigned long idx_end;
389 idx_end = idx_start + npages - 1;
391 list_for_each_safe(pos, tmp, head) {
392 unsigned long pg_idx;
394 req = nfs_list_entry(pos);
396 pg_idx = page_index(req->wb_page);
397 if (pg_idx < idx_start)
399 if (pg_idx > idx_end)
402 if (!nfs_lock_request(req))
404 nfs_list_remove_request(req);
405 nfs_list_add_request(req, dst);
407 __nfs_add_lru(&NFS_SERVER(req->wb_inode)->lru_busy, req);
414 * nfs_try_to_free_pages - Free up NFS read/write requests
415 * @server: The NFS superblock
417 * This function attempts to flush out NFS reads and writes in order
418 * to keep the hard limit on the total number of pending requests
419 * on a given NFS partition.
420 * Note: we first try to commit unstable writes, then flush out pending
421 * reads, then finally the dirty pages.
422 * The assumption is that this reflects the ordering from the fastest
423 * to the slowest method for reclaiming requests.
426 nfs_try_to_free_pages(struct nfs_server *server)
429 struct nfs_page *req = NULL;
434 int status = nfs_wait_on_request(req);
435 nfs_release_request(req);
440 nreq = atomic_read(&server->rw_requests->nr_requests);
441 if (nreq < MAX_REQUEST_HARD)
443 spin_lock(&nfs_wreq_lock);
444 /* Are there any busy RPC calls that might free up requests? */
445 if (!list_empty(&server->lru_busy)) {
446 req = nfs_lru_entry(server->lru_busy.next);
449 spin_unlock(&nfs_wreq_lock);
454 /* Let's try to free up some completed NFSv3 unstable writes */
455 nfs_scan_lru_commit(server, &head);
456 if (!list_empty(&head)) {
457 spin_unlock(&nfs_wreq_lock);
458 nfs_commit_list(&head, 0);
462 /* OK, so we try to free up some pending readaheads */
463 nfs_scan_lru_read(server, &head);
464 if (!list_empty(&head)) {
465 spin_unlock(&nfs_wreq_lock);
466 nfs_pagein_list(&head, server->rpages);
469 /* Last resort: we try to flush out single requests */
470 nfs_scan_lru_dirty(server, &head);
471 if (!list_empty(&head)) {
472 spin_unlock(&nfs_wreq_lock);
473 nfs_flush_list(&head, server->wpages, FLUSH_STABLE);
476 spin_unlock(&nfs_wreq_lock);
479 /* We failed to free up requests */
483 int nfs_init_nfspagecache(void)
485 nfs_page_cachep = kmem_cache_create("nfs_page",
486 sizeof(struct nfs_page),
487 0, SLAB_HWCACHE_ALIGN,
489 if (nfs_page_cachep == NULL)
495 void nfs_destroy_nfspagecache(void)
497 if (kmem_cache_destroy(nfs_page_cachep))
498 printk(KERN_INFO "nfs_page: not all structures were freed\n");