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/nfs4.h>
18 #include <linux/nfs_page.h>
19 #include <linux/nfs_fs.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_KERNEL);
37 memset(p, 0, sizeof(*p));
38 INIT_LIST_HEAD(&p->wb_list);
39 init_waitqueue_head(&p->wb_wait);
45 nfs_page_free(struct nfs_page *p)
47 kmem_cache_free(nfs_page_cachep, p);
51 * nfs_create_request - Create an NFS read/write request.
52 * @file: file descriptor to use
53 * @inode: inode to which the request is attached
54 * @page: page to write
55 * @offset: starting offset within the page for the write
56 * @count: number of bytes to read/write
58 * The page must be locked by the caller. This makes sure we never
59 * create two different requests for the same page, and avoids
60 * a possible deadlock when we reach the hard limit on the number
62 * User should ensure it is safe to sleep in this function.
65 nfs_create_request(struct file *file, struct inode *inode,
67 unsigned int offset, unsigned int count)
69 struct nfs_server *server = NFS_SERVER(inode);
72 /* Deal with hard limits. */
74 /* try to allocate the request struct */
75 req = nfs_page_alloc();
79 /* Try to free up at least one request in order to stay
80 * below the hard limit
82 if (signalled() && (server->flags & NFS_MOUNT_INTR))
83 return ERR_PTR(-ERESTARTSYS);
87 /* Initialize the request struct. Initially, we assume a
88 * long write-back delay. This will be adjusted in
89 * update_nfs_request below if the region is not locked. */
91 atomic_set(&req->wb_complete, 0);
92 req->wb_index = page->index;
94 req->wb_offset = offset;
95 req->wb_pgbase = offset;
96 req->wb_bytes = count;
97 req->wb_inode = inode;
99 server->rpc_ops->request_init(req, file);
105 * nfs_clear_request - Free up all resources allocated to the request
108 * Release all resources associated with a write request after it
111 void nfs_clear_request(struct nfs_page *req)
114 req->wb_state = NULL;
115 /* Release struct file or cached credential */
121 put_rpccred(req->wb_cred);
125 page_cache_release(req->wb_page);
132 * nfs_release_request - Release the count on an NFS read/write request
133 * @req: request to release
135 * Note: Should never be called with the spinlock held!
138 nfs_release_request(struct nfs_page *req)
140 spin_lock(&nfs_wreq_lock);
141 if (--req->wb_count) {
142 spin_unlock(&nfs_wreq_lock);
145 spin_unlock(&nfs_wreq_lock);
148 BUG_ON (!list_empty(&req->wb_list));
149 BUG_ON (NFS_WBACK_BUSY(req));
152 /* Release struct file or cached credential */
153 nfs_clear_request(req);
158 * nfs_list_add_request - Insert a request into a sorted list
160 * @head: head of list into which to insert the request.
162 * Note that the wb_list is sorted by page index in order to facilitate
163 * coalescing of requests.
164 * We use an insertion sort that is optimized for the case of appended
168 nfs_list_add_request(struct nfs_page *req, struct list_head *head)
170 struct list_head *pos;
173 if (!list_empty(&req->wb_list)) {
174 printk(KERN_ERR "NFS: Add to list failed!\n");
178 list_for_each_prev(pos, head) {
179 struct nfs_page *p = nfs_list_entry(pos);
180 if (p->wb_index < req->wb_index)
183 list_add(&req->wb_list, pos);
184 req->wb_list_head = head;
188 * nfs_wait_on_request - Wait for a request to complete.
189 * @req: request to wait upon.
191 * Interruptible by signals only if mounted with intr flag.
192 * The user is responsible for holding a count on the request.
195 nfs_wait_on_request(struct nfs_page *req)
197 struct inode *inode = req->wb_inode;
198 struct rpc_clnt *clnt = NFS_CLIENT(inode);
200 if (!NFS_WBACK_BUSY(req))
202 return nfs_wait_event(clnt, req->wb_wait, !NFS_WBACK_BUSY(req));
206 * nfs_coalesce_requests - Split coalesced requests out from a list.
208 * @dst: destination list
209 * @nmax: maximum number of requests to coalesce
211 * Moves a maximum of 'nmax' elements from one list to another.
212 * The elements are checked to ensure that they form a contiguous set
213 * of pages, and that the RPC credentials are the same.
216 nfs_coalesce_requests(struct list_head *head, struct list_head *dst,
219 struct nfs_page *req = NULL;
220 unsigned int npages = 0;
222 while (!list_empty(head)) {
223 struct nfs_page *prev = req;
225 req = nfs_list_entry(head->next);
227 if (req->wb_cred != prev->wb_cred)
229 if (req->wb_index != (prev->wb_index + 1))
232 if (req->wb_pgbase != 0)
235 nfs_list_remove_request(req);
236 nfs_list_add_request(req, dst);
238 if (req->wb_pgbase + req->wb_bytes != PAGE_CACHE_SIZE)
247 * nfs_scan_list - Scan a list for matching requests
248 * @head: One of the NFS inode request lists
249 * @dst: Destination list
250 * @idx_start: lower bound of page->index to scan
251 * @npages: idx_start + npages sets the upper bound to scan.
253 * Moves elements from one of the inode request lists.
254 * If the number of requests is set to 0, the entire address_space
255 * starting at index idx_start, is scanned.
256 * The requests are *not* checked to ensure that they form a contiguous set.
257 * You must be holding the nfs_wreq_lock when calling this function
260 nfs_scan_list(struct list_head *head, struct list_head *dst,
261 unsigned long idx_start, unsigned int npages)
263 struct list_head *pos, *tmp;
264 struct nfs_page *req;
265 unsigned long idx_end;
272 idx_end = idx_start + npages - 1;
274 list_for_each_safe(pos, tmp, head) {
276 req = nfs_list_entry(pos);
278 if (req->wb_index < idx_start)
280 if (req->wb_index > idx_end)
283 if (!nfs_lock_request(req))
285 nfs_list_remove_request(req);
286 nfs_list_add_request(req, dst);
292 int nfs_init_nfspagecache(void)
294 nfs_page_cachep = kmem_cache_create("nfs_page",
295 sizeof(struct nfs_page),
296 0, SLAB_HWCACHE_ALIGN,
298 if (nfs_page_cachep == NULL)
304 void nfs_destroy_nfspagecache(void)
306 if (kmem_cache_destroy(nfs_page_cachep))
307 printk(KERN_INFO "nfs_page: not all structures were freed\n");