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/slab.h>
13 #include <linux/file.h>
14 #include <linux/sunrpc/clnt.h>
15 #include <linux/nfs3.h>
16 #include <linux/nfs4.h>
17 #include <linux/nfs_page.h>
18 #include <linux/nfs_fs.h>
19 #include <linux/nfs_mount.h>
23 #define NFS_PARANOIA 1
25 static struct kmem_cache *nfs_page_cachep;
27 static inline struct nfs_page *
31 p = kmem_cache_alloc(nfs_page_cachep, GFP_KERNEL);
33 memset(p, 0, sizeof(*p));
34 INIT_LIST_HEAD(&p->wb_list);
40 nfs_page_free(struct nfs_page *p)
42 kmem_cache_free(nfs_page_cachep, p);
46 * nfs_create_request - Create an NFS read/write request.
47 * @file: file descriptor to use
48 * @inode: inode to which the request is attached
49 * @page: page to write
50 * @offset: starting offset within the page for the write
51 * @count: number of bytes to read/write
53 * The page must be locked by the caller. This makes sure we never
54 * create two different requests for the same page, and avoids
55 * a possible deadlock when we reach the hard limit on the number
57 * User should ensure it is safe to sleep in this function.
60 nfs_create_request(struct nfs_open_context *ctx, struct inode *inode,
62 unsigned int offset, unsigned int count)
64 struct nfs_server *server = NFS_SERVER(inode);
67 /* Deal with hard limits. */
69 /* try to allocate the request struct */
70 req = nfs_page_alloc();
74 /* Try to free up at least one request in order to stay
75 * below the hard limit
77 if (signalled() && (server->flags & NFS_MOUNT_INTR))
78 return ERR_PTR(-ERESTARTSYS);
82 /* Initialize the request struct. Initially, we assume a
83 * long write-back delay. This will be adjusted in
84 * update_nfs_request below if the region is not locked. */
86 atomic_set(&req->wb_complete, 0);
87 req->wb_index = page->index;
89 BUG_ON(PagePrivate(page));
90 BUG_ON(!PageLocked(page));
91 BUG_ON(page->mapping->host != inode);
92 req->wb_offset = offset;
93 req->wb_pgbase = offset;
94 req->wb_bytes = count;
95 atomic_set(&req->wb_count, 1);
96 req->wb_context = get_nfs_open_context(ctx);
102 * nfs_unlock_request - Unlock request and wake up sleepers.
105 void nfs_unlock_request(struct nfs_page *req)
107 if (!NFS_WBACK_BUSY(req)) {
108 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
111 smp_mb__before_clear_bit();
112 clear_bit(PG_BUSY, &req->wb_flags);
113 smp_mb__after_clear_bit();
114 wake_up_bit(&req->wb_flags, PG_BUSY);
115 nfs_release_request(req);
119 * nfs_set_page_writeback_locked - Lock a request for writeback
122 int nfs_set_page_writeback_locked(struct nfs_page *req)
124 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
126 if (!nfs_lock_request(req))
128 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK);
133 * nfs_clear_page_writeback - Unlock request and wake up sleepers
135 void nfs_clear_page_writeback(struct nfs_page *req)
137 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
139 if (req->wb_page != NULL) {
140 spin_lock(&nfsi->req_lock);
141 radix_tree_tag_clear(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK);
142 spin_unlock(&nfsi->req_lock);
144 nfs_unlock_request(req);
148 * nfs_clear_request - Free up all resources allocated to the request
151 * Release page resources associated with a write request after it
154 void nfs_clear_request(struct nfs_page *req)
156 struct page *page = req->wb_page;
158 page_cache_release(page);
165 * nfs_release_request - Release the count on an NFS read/write request
166 * @req: request to release
168 * Note: Should never be called with the spinlock held!
171 nfs_release_request(struct nfs_page *req)
173 if (!atomic_dec_and_test(&req->wb_count))
177 BUG_ON (!list_empty(&req->wb_list));
178 BUG_ON (NFS_WBACK_BUSY(req));
181 /* Release struct file or cached credential */
182 nfs_clear_request(req);
183 put_nfs_open_context(req->wb_context);
187 static int nfs_wait_bit_interruptible(void *word)
191 if (signal_pending(current))
199 * nfs_wait_on_request - Wait for a request to complete.
200 * @req: request to wait upon.
202 * Interruptible by signals only if mounted with intr flag.
203 * The user is responsible for holding a count on the request.
206 nfs_wait_on_request(struct nfs_page *req)
208 struct rpc_clnt *clnt = NFS_CLIENT(req->wb_context->dentry->d_inode);
212 if (!test_bit(PG_BUSY, &req->wb_flags))
215 * Note: the call to rpc_clnt_sigmask() suffices to ensure that we
216 * are not interrupted if intr flag is not set
218 rpc_clnt_sigmask(clnt, &oldmask);
219 ret = out_of_line_wait_on_bit(&req->wb_flags, PG_BUSY,
220 nfs_wait_bit_interruptible, TASK_INTERRUPTIBLE);
221 rpc_clnt_sigunmask(clnt, &oldmask);
227 * nfs_pageio_init - initialise a page io descriptor
228 * @desc: pointer to descriptor
229 * @inode: pointer to inode
230 * @doio: pointer to io function
231 * @bsize: io block size
232 * @io_flags: extra parameters for the io function
234 void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
236 int (*doio)(struct inode *, struct list_head *, unsigned int, size_t, int),
240 INIT_LIST_HEAD(&desc->pg_list);
241 desc->pg_bytes_written = 0;
243 desc->pg_bsize = bsize;
245 desc->pg_inode = inode;
246 desc->pg_doio = doio;
247 desc->pg_ioflags = io_flags;
252 * nfs_can_coalesce_requests - test two requests for compatibility
253 * @prev: pointer to nfs_page
254 * @req: pointer to nfs_page
256 * The nfs_page structures 'prev' and 'req' are compared to ensure that the
257 * page data area they describe is contiguous, and that their RPC
258 * credentials, NFSv4 open state, and lockowners are the same.
260 * Return 'true' if this is the case, else return 'false'.
262 static int nfs_can_coalesce_requests(struct nfs_page *prev,
263 struct nfs_page *req)
265 if (req->wb_context->cred != prev->wb_context->cred)
267 if (req->wb_context->lockowner != prev->wb_context->lockowner)
269 if (req->wb_context->state != prev->wb_context->state)
271 if (req->wb_index != (prev->wb_index + 1))
273 if (req->wb_pgbase != 0)
275 if (prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
281 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
282 * @desc: destination io descriptor
285 * Returns true if the request 'req' was successfully coalesced into the
286 * existing list of pages 'desc'.
288 static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
289 struct nfs_page *req)
291 size_t newlen = req->wb_bytes;
293 if (desc->pg_count != 0) {
294 struct nfs_page *prev;
297 * FIXME: ideally we should be able to coalesce all requests
298 * that are not block boundary aligned, but currently this
299 * is problematic for the case of bsize < PAGE_CACHE_SIZE,
300 * since nfs_flush_multi and nfs_pagein_multi assume you
301 * can have only one struct nfs_page.
303 if (desc->pg_bsize < PAGE_SIZE)
305 newlen += desc->pg_count;
306 if (newlen > desc->pg_bsize)
308 prev = nfs_list_entry(desc->pg_list.prev);
309 if (!nfs_can_coalesce_requests(prev, req))
312 desc->pg_base = req->wb_pgbase;
313 nfs_list_remove_request(req);
314 nfs_list_add_request(req, &desc->pg_list);
315 desc->pg_count = newlen;
320 * Helper for nfs_pageio_add_request and nfs_pageio_complete
322 static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
324 if (!list_empty(&desc->pg_list)) {
325 int error = desc->pg_doio(desc->pg_inode,
327 nfs_page_array_len(desc->pg_base,
332 desc->pg_error = error;
334 desc->pg_bytes_written += desc->pg_count;
336 if (list_empty(&desc->pg_list)) {
343 * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
344 * @desc: destination io descriptor
347 * Returns true if the request 'req' was successfully coalesced into the
348 * existing list of pages 'desc'.
350 int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
351 struct nfs_page *req)
353 while (!nfs_pageio_do_add_request(desc, req)) {
354 nfs_pageio_doio(desc);
355 if (desc->pg_error < 0)
362 * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
363 * @desc: pointer to io descriptor
365 void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
367 nfs_pageio_doio(desc);
370 #define NFS_SCAN_MAXENTRIES 16
372 * nfs_scan_list - Scan a list for matching requests
374 * @head: One of the NFS inode request lists
375 * @dst: Destination list
376 * @idx_start: lower bound of page->index to scan
377 * @npages: idx_start + npages sets the upper bound to scan.
379 * Moves elements from one of the inode request lists.
380 * If the number of requests is set to 0, the entire address_space
381 * starting at index idx_start, is scanned.
382 * The requests are *not* checked to ensure that they form a contiguous set.
383 * You must be holding the inode's req_lock when calling this function
385 int nfs_scan_list(struct nfs_inode *nfsi, struct list_head *head,
386 struct list_head *dst, unsigned long idx_start,
389 struct nfs_page *pgvec[NFS_SCAN_MAXENTRIES];
390 struct nfs_page *req;
391 unsigned long idx_end;
399 idx_end = idx_start + npages - 1;
402 found = radix_tree_gang_lookup(&nfsi->nfs_page_tree,
403 (void **)&pgvec[0], idx_start,
404 NFS_SCAN_MAXENTRIES);
407 for (i = 0; i < found; i++) {
409 if (req->wb_index > idx_end)
411 idx_start = req->wb_index + 1;
412 if (req->wb_list_head != head)
414 if (nfs_set_page_writeback_locked(req)) {
415 nfs_list_remove_request(req);
416 nfs_list_add_request(req, dst);
426 int __init nfs_init_nfspagecache(void)
428 nfs_page_cachep = kmem_cache_create("nfs_page",
429 sizeof(struct nfs_page),
430 0, SLAB_HWCACHE_ALIGN,
432 if (nfs_page_cachep == NULL)
438 void nfs_destroy_nfspagecache(void)
440 kmem_cache_destroy(nfs_page_cachep);