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>
20 #include <linux/writeback.h>
22 #define NFS_PARANOIA 1
24 static struct kmem_cache *nfs_page_cachep;
26 static inline struct nfs_page *
30 p = kmem_cache_alloc(nfs_page_cachep, GFP_KERNEL);
32 memset(p, 0, sizeof(*p));
33 INIT_LIST_HEAD(&p->wb_list);
39 nfs_page_free(struct nfs_page *p)
41 kmem_cache_free(nfs_page_cachep, p);
45 * nfs_create_request - Create an NFS read/write request.
46 * @file: file descriptor to use
47 * @inode: inode to which the request is attached
48 * @page: page to write
49 * @offset: starting offset within the page for the write
50 * @count: number of bytes to read/write
52 * The page must be locked by the caller. This makes sure we never
53 * create two different requests for the same page, and avoids
54 * a possible deadlock when we reach the hard limit on the number
56 * User should ensure it is safe to sleep in this function.
59 nfs_create_request(struct nfs_open_context *ctx, struct inode *inode,
61 unsigned int offset, unsigned int count)
63 struct nfs_server *server = NFS_SERVER(inode);
66 /* Deal with hard limits. */
68 /* try to allocate the request struct */
69 req = nfs_page_alloc();
73 /* Try to free up at least one request in order to stay
74 * below the hard limit
76 if (signalled() && (server->flags & NFS_MOUNT_INTR))
77 return ERR_PTR(-ERESTARTSYS);
81 /* Initialize the request struct. Initially, we assume a
82 * long write-back delay. This will be adjusted in
83 * update_nfs_request below if the region is not locked. */
85 atomic_set(&req->wb_complete, 0);
86 req->wb_index = page->index;
88 BUG_ON(PagePrivate(page));
89 BUG_ON(!PageLocked(page));
90 BUG_ON(page->mapping->host != inode);
91 req->wb_offset = offset;
92 req->wb_pgbase = offset;
93 req->wb_bytes = count;
94 atomic_set(&req->wb_count, 1);
95 req->wb_context = get_nfs_open_context(ctx);
101 * nfs_unlock_request - Unlock request and wake up sleepers.
104 void nfs_unlock_request(struct nfs_page *req)
106 if (!NFS_WBACK_BUSY(req)) {
107 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
110 smp_mb__before_clear_bit();
111 clear_bit(PG_BUSY, &req->wb_flags);
112 smp_mb__after_clear_bit();
113 wake_up_bit(&req->wb_flags, PG_BUSY);
114 nfs_release_request(req);
118 * nfs_set_page_writeback_locked - Lock a request for writeback
121 int nfs_set_page_writeback_locked(struct nfs_page *req)
123 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
125 if (!nfs_lock_request(req))
127 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK);
132 * nfs_clear_page_writeback - Unlock request and wake up sleepers
134 void nfs_clear_page_writeback(struct nfs_page *req)
136 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
138 if (req->wb_page != NULL) {
139 spin_lock(&nfsi->req_lock);
140 radix_tree_tag_clear(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK);
141 spin_unlock(&nfsi->req_lock);
143 nfs_unlock_request(req);
147 * nfs_clear_request - Free up all resources allocated to the request
150 * Release page resources associated with a write request after it
153 void nfs_clear_request(struct nfs_page *req)
155 struct page *page = req->wb_page;
157 page_cache_release(page);
164 * nfs_release_request - Release the count on an NFS read/write request
165 * @req: request to release
167 * Note: Should never be called with the spinlock held!
170 nfs_release_request(struct nfs_page *req)
172 if (!atomic_dec_and_test(&req->wb_count))
176 BUG_ON (!list_empty(&req->wb_list));
177 BUG_ON (NFS_WBACK_BUSY(req));
180 /* Release struct file or cached credential */
181 nfs_clear_request(req);
182 put_nfs_open_context(req->wb_context);
186 static int nfs_wait_bit_interruptible(void *word)
190 if (signal_pending(current))
198 * nfs_wait_on_request - Wait for a request to complete.
199 * @req: request to wait upon.
201 * Interruptible by signals only if mounted with intr flag.
202 * The user is responsible for holding a count on the request.
205 nfs_wait_on_request(struct nfs_page *req)
207 struct rpc_clnt *clnt = NFS_CLIENT(req->wb_context->dentry->d_inode);
211 if (!test_bit(PG_BUSY, &req->wb_flags))
214 * Note: the call to rpc_clnt_sigmask() suffices to ensure that we
215 * are not interrupted if intr flag is not set
217 rpc_clnt_sigmask(clnt, &oldmask);
218 ret = out_of_line_wait_on_bit(&req->wb_flags, PG_BUSY,
219 nfs_wait_bit_interruptible, TASK_INTERRUPTIBLE);
220 rpc_clnt_sigunmask(clnt, &oldmask);
226 * nfs_pageio_init - initialise a page io descriptor
227 * @desc: pointer to descriptor
228 * @inode: pointer to inode
229 * @doio: pointer to io function
230 * @bsize: io block size
231 * @io_flags: extra parameters for the io function
233 void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
235 int (*doio)(struct inode *, struct list_head *, size_t, int),
239 INIT_LIST_HEAD(&desc->pg_list);
240 desc->pg_bytes_written = 0;
242 desc->pg_bsize = bsize;
244 desc->pg_inode = inode;
245 desc->pg_doio = doio;
246 desc->pg_ioflags = io_flags;
251 * nfs_can_coalesce_requests - test two requests for compatibility
252 * @prev: pointer to nfs_page
253 * @req: pointer to nfs_page
255 * The nfs_page structures 'prev' and 'req' are compared to ensure that the
256 * page data area they describe is contiguous, and that their RPC
257 * credentials, NFSv4 open state, and lockowners are the same.
259 * Return 'true' if this is the case, else return 'false'.
261 static int nfs_can_coalesce_requests(struct nfs_page *prev,
262 struct nfs_page *req)
264 if (req->wb_context->cred != prev->wb_context->cred)
266 if (req->wb_context->lockowner != prev->wb_context->lockowner)
268 if (req->wb_context->state != prev->wb_context->state)
270 if (req->wb_index != (prev->wb_index + 1))
272 if (req->wb_pgbase != 0)
274 if (prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
280 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
281 * @desc: destination io descriptor
284 * Returns true if the request 'req' was successfully coalesced into the
285 * existing list of pages 'desc'.
287 static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
288 struct nfs_page *req)
290 size_t newlen = req->wb_bytes;
292 if (desc->pg_count != 0) {
293 struct nfs_page *prev;
296 * FIXME: ideally we should be able to coalesce all requests
297 * that are not block boundary aligned, but currently this
298 * is problematic for the case of bsize < PAGE_CACHE_SIZE,
299 * since nfs_flush_multi and nfs_pagein_multi assume you
300 * can have only one struct nfs_page.
302 newlen += desc->pg_count;
303 if (desc->pg_base + newlen > desc->pg_bsize)
305 prev = nfs_list_entry(desc->pg_list.prev);
306 if (!nfs_can_coalesce_requests(prev, req))
309 desc->pg_base = req->wb_pgbase;
310 nfs_list_remove_request(req);
311 nfs_list_add_request(req, &desc->pg_list);
312 desc->pg_count = newlen;
317 * Helper for nfs_pageio_add_request and nfs_pageio_complete
319 static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
321 if (!list_empty(&desc->pg_list)) {
322 int error = desc->pg_doio(desc->pg_inode,
327 desc->pg_error = error;
329 desc->pg_bytes_written += desc->pg_count;
331 if (list_empty(&desc->pg_list)) {
338 * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
339 * @desc: destination io descriptor
342 * Returns true if the request 'req' was successfully coalesced into the
343 * existing list of pages 'desc'.
345 int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
346 struct nfs_page *req)
348 while (!nfs_pageio_do_add_request(desc, req)) {
349 nfs_pageio_doio(desc);
350 if (desc->pg_error < 0)
357 * nfs_pageio_add_list - Split coalesced requests out from a list.
358 * @desc: destination io descriptor
361 * Moves a maximum of 'nmax' elements from one list to another.
362 * The elements are checked to ensure that they form a contiguous set
363 * of pages, and that the RPC credentials are the same.
365 void nfs_pageio_add_list(struct nfs_pageio_descriptor *desc,
366 struct list_head *head)
368 while (!list_empty(head)) {
369 struct nfs_page *req = nfs_list_entry(head->next);
370 if (!nfs_pageio_add_request(desc, req))
376 * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
377 * @desc: pointer to io descriptor
379 void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
381 nfs_pageio_doio(desc);
384 #define NFS_SCAN_MAXENTRIES 16
386 * nfs_scan_dirty - Scan the radix tree for dirty requests
387 * @mapping: pointer to address space
388 * @wbc: writeback_control structure
389 * @dst: Destination list
391 * Moves elements from one of the inode request lists.
392 * If the number of requests is set to 0, the entire address_space
393 * starting at index idx_start, is scanned.
394 * The requests are *not* checked to ensure that they form a contiguous set.
395 * You must be holding the inode's req_lock when calling this function
397 long nfs_scan_dirty(struct address_space *mapping,
398 struct writeback_control *wbc,
399 struct list_head *dst)
401 struct nfs_inode *nfsi = NFS_I(mapping->host);
402 struct nfs_page *pgvec[NFS_SCAN_MAXENTRIES];
403 struct nfs_page *req;
404 pgoff_t idx_start, idx_end;
408 if (nfsi->ndirty == 0)
410 if (wbc->range_cyclic) {
413 } else if (wbc->range_end == 0) {
414 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
417 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
418 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
422 unsigned int toscan = NFS_SCAN_MAXENTRIES;
424 found = radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree,
425 (void **)&pgvec[0], idx_start, toscan,
428 /* Did we make progress? */
432 for (i = 0; i < found; i++) {
434 if (!wbc->range_cyclic && req->wb_index > idx_end)
437 /* Try to lock request and mark it for writeback */
438 if (!nfs_set_page_writeback_locked(req))
440 radix_tree_tag_clear(&nfsi->nfs_page_tree,
441 req->wb_index, NFS_PAGE_TAG_DIRTY);
443 nfs_list_remove_request(req);
444 nfs_list_add_request(req, dst);
449 idx_start = req->wb_index + 1;
453 WARN_ON ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty));
458 * nfs_scan_list - Scan a list for matching requests
460 * @head: One of the NFS inode request lists
461 * @dst: Destination list
462 * @idx_start: lower bound of page->index to scan
463 * @npages: idx_start + npages sets the upper bound to scan.
465 * Moves elements from one of the inode request lists.
466 * If the number of requests is set to 0, the entire address_space
467 * starting at index idx_start, is scanned.
468 * The requests are *not* checked to ensure that they form a contiguous set.
469 * You must be holding the inode's req_lock when calling this function
471 int nfs_scan_list(struct nfs_inode *nfsi, struct list_head *head,
472 struct list_head *dst, unsigned long idx_start,
475 struct nfs_page *pgvec[NFS_SCAN_MAXENTRIES];
476 struct nfs_page *req;
477 unsigned long idx_end;
485 idx_end = idx_start + npages - 1;
488 found = radix_tree_gang_lookup(&nfsi->nfs_page_tree,
489 (void **)&pgvec[0], idx_start,
490 NFS_SCAN_MAXENTRIES);
493 for (i = 0; i < found; i++) {
495 if (req->wb_index > idx_end)
497 idx_start = req->wb_index + 1;
498 if (req->wb_list_head != head)
500 if (nfs_set_page_writeback_locked(req)) {
501 nfs_list_remove_request(req);
502 nfs_list_add_request(req, dst);
512 int __init nfs_init_nfspagecache(void)
514 nfs_page_cachep = kmem_cache_create("nfs_page",
515 sizeof(struct nfs_page),
516 0, SLAB_HWCACHE_ALIGN,
518 if (nfs_page_cachep == NULL)
524 void nfs_destroy_nfspagecache(void)
526 kmem_cache_destroy(nfs_page_cachep);