blob: 094537ddd344cf296368fb5b689d16a18c96b1f4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/pagelist.c
3 *
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.
7 *
8 * Copyright 2000, 2001 (c) Trond Myklebust <trond.myklebust@fys.uio.no>
9 *
10 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#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>
Trond Myklebust3f442542006-09-17 14:46:44 -040020#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#define NFS_PARANOIA 1
23
Christoph Lametere18b8902006-12-06 20:33:20 -080024static struct kmem_cache *nfs_page_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26static inline struct nfs_page *
27nfs_page_alloc(void)
28{
29 struct nfs_page *p;
Christoph Lametere94b1762006-12-06 20:33:17 -080030 p = kmem_cache_alloc(nfs_page_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 if (p) {
32 memset(p, 0, sizeof(*p));
33 INIT_LIST_HEAD(&p->wb_list);
34 }
35 return p;
36}
37
38static inline void
39nfs_page_free(struct nfs_page *p)
40{
41 kmem_cache_free(nfs_page_cachep, p);
42}
43
44/**
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
51 *
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
55 * of dirty pages.
56 * User should ensure it is safe to sleep in this function.
57 */
58struct nfs_page *
59nfs_create_request(struct nfs_open_context *ctx, struct inode *inode,
60 struct page *page,
61 unsigned int offset, unsigned int count)
62{
63 struct nfs_server *server = NFS_SERVER(inode);
64 struct nfs_page *req;
65
66 /* Deal with hard limits. */
67 for (;;) {
68 /* try to allocate the request struct */
69 req = nfs_page_alloc();
70 if (req != NULL)
71 break;
72
73 /* Try to free up at least one request in order to stay
74 * below the hard limit
75 */
76 if (signalled() && (server->flags & NFS_MOUNT_INTR))
77 return ERR_PTR(-ERESTARTSYS);
78 yield();
79 }
80
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. */
84 req->wb_page = page;
85 atomic_set(&req->wb_complete, 0);
86 req->wb_index = page->index;
87 page_cache_get(page);
Trond Myklebustcd52ed32006-03-20 13:44:04 -050088 BUG_ON(PagePrivate(page));
89 BUG_ON(!PageLocked(page));
90 BUG_ON(page->mapping->host != inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 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);
96
97 return req;
98}
99
100/**
101 * nfs_unlock_request - Unlock request and wake up sleepers.
102 * @req:
103 */
104void nfs_unlock_request(struct nfs_page *req)
105{
106 if (!NFS_WBACK_BUSY(req)) {
107 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
108 BUG();
109 }
110 smp_mb__before_clear_bit();
111 clear_bit(PG_BUSY, &req->wb_flags);
112 smp_mb__after_clear_bit();
Trond Myklebust464a98b2005-06-22 17:16:21 +0000113 wake_up_bit(&req->wb_flags, PG_BUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 nfs_release_request(req);
115}
116
117/**
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000118 * nfs_set_page_writeback_locked - Lock a request for writeback
119 * @req:
120 */
121int nfs_set_page_writeback_locked(struct nfs_page *req)
122{
123 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
124
125 if (!nfs_lock_request(req))
126 return 0;
127 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_WRITEBACK);
128 return 1;
129}
130
131/**
132 * nfs_clear_page_writeback - Unlock request and wake up sleepers
133 */
134void nfs_clear_page_writeback(struct nfs_page *req)
135{
136 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
137
Trond Myklebustdeb7d632006-03-20 13:44:50 -0500138 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);
142 }
Trond Myklebustc6a556b2005-06-22 17:16:30 +0000143 nfs_unlock_request(req);
144}
145
146/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 * nfs_clear_request - Free up all resources allocated to the request
148 * @req:
149 *
150 * Release page resources associated with a write request after it
151 * has completed.
152 */
153void nfs_clear_request(struct nfs_page *req)
154{
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500155 struct page *page = req->wb_page;
156 if (page != NULL) {
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500157 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 req->wb_page = NULL;
159 }
160}
161
162
163/**
164 * nfs_release_request - Release the count on an NFS read/write request
165 * @req: request to release
166 *
167 * Note: Should never be called with the spinlock held!
168 */
169void
170nfs_release_request(struct nfs_page *req)
171{
172 if (!atomic_dec_and_test(&req->wb_count))
173 return;
174
175#ifdef NFS_PARANOIA
176 BUG_ON (!list_empty(&req->wb_list));
177 BUG_ON (NFS_WBACK_BUSY(req));
178#endif
179
180 /* Release struct file or cached credential */
181 nfs_clear_request(req);
182 put_nfs_open_context(req->wb_context);
183 nfs_page_free(req);
184}
185
Trond Myklebust464a98b2005-06-22 17:16:21 +0000186static int nfs_wait_bit_interruptible(void *word)
187{
188 int ret = 0;
189
190 if (signal_pending(current))
191 ret = -ERESTARTSYS;
192 else
193 schedule();
194 return ret;
195}
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/**
198 * nfs_wait_on_request - Wait for a request to complete.
199 * @req: request to wait upon.
200 *
201 * Interruptible by signals only if mounted with intr flag.
202 * The user is responsible for holding a count on the request.
203 */
204int
205nfs_wait_on_request(struct nfs_page *req)
206{
Trond Myklebust464a98b2005-06-22 17:16:21 +0000207 struct rpc_clnt *clnt = NFS_CLIENT(req->wb_context->dentry->d_inode);
208 sigset_t oldmask;
209 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Trond Myklebust464a98b2005-06-22 17:16:21 +0000211 if (!test_bit(PG_BUSY, &req->wb_flags))
212 goto out;
213 /*
214 * Note: the call to rpc_clnt_sigmask() suffices to ensure that we
215 * are not interrupted if intr flag is not set
216 */
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);
221out:
222 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225/**
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400226 * nfs_pageio_init - initialise a page io descriptor
227 * @desc: pointer to descriptor
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400228 * @inode: pointer to inode
229 * @doio: pointer to io function
230 * @bsize: io block size
231 * @io_flags: extra parameters for the io function
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400232 */
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400233void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
234 struct inode *inode,
235 int (*doio)(struct inode *, struct list_head *, size_t, int),
236 unsigned int bsize,
237 int io_flags)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400238{
239 INIT_LIST_HEAD(&desc->pg_list);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400240 desc->pg_bytes_written = 0;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400241 desc->pg_count = 0;
242 desc->pg_bsize = bsize;
243 desc->pg_base = 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400244 desc->pg_inode = inode;
245 desc->pg_doio = doio;
246 desc->pg_ioflags = io_flags;
247 desc->pg_error = 0;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400248}
249
250/**
251 * nfs_can_coalesce_requests - test two requests for compatibility
252 * @prev: pointer to nfs_page
253 * @req: pointer to nfs_page
254 *
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.
258 *
259 * Return 'true' if this is the case, else return 'false'.
260 */
261static int nfs_can_coalesce_requests(struct nfs_page *prev,
262 struct nfs_page *req)
263{
264 if (req->wb_context->cred != prev->wb_context->cred)
265 return 0;
266 if (req->wb_context->lockowner != prev->wb_context->lockowner)
267 return 0;
268 if (req->wb_context->state != prev->wb_context->state)
269 return 0;
270 if (req->wb_index != (prev->wb_index + 1))
271 return 0;
272 if (req->wb_pgbase != 0)
273 return 0;
274 if (prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
275 return 0;
276 return 1;
277}
278
279/**
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400280 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400281 * @desc: destination io descriptor
282 * @req: request
283 *
284 * Returns true if the request 'req' was successfully coalesced into the
285 * existing list of pages 'desc'.
286 */
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400287static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
288 struct nfs_page *req)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400289{
290 size_t newlen = req->wb_bytes;
291
292 if (desc->pg_count != 0) {
293 struct nfs_page *prev;
294
295 /*
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.
301 */
302 newlen += desc->pg_count;
303 if (desc->pg_base + newlen > desc->pg_bsize)
304 return 0;
305 prev = nfs_list_entry(desc->pg_list.prev);
306 if (!nfs_can_coalesce_requests(prev, req))
307 return 0;
308 } else
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;
313 return 1;
314}
315
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400316/*
317 * Helper for nfs_pageio_add_request and nfs_pageio_complete
318 */
319static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
320{
321 if (!list_empty(&desc->pg_list)) {
322 int error = desc->pg_doio(desc->pg_inode,
323 &desc->pg_list,
324 desc->pg_count,
325 desc->pg_ioflags);
326 if (error < 0)
327 desc->pg_error = error;
328 else
329 desc->pg_bytes_written += desc->pg_count;
330 }
331 if (list_empty(&desc->pg_list)) {
332 desc->pg_count = 0;
333 desc->pg_base = 0;
334 }
335}
336
337/**
338 * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
339 * @desc: destination io descriptor
340 * @req: request
341 *
342 * Returns true if the request 'req' was successfully coalesced into the
343 * existing list of pages 'desc'.
344 */
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400345int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
346 struct nfs_page *req)
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400347{
348 while (!nfs_pageio_do_add_request(desc, req)) {
349 nfs_pageio_doio(desc);
350 if (desc->pg_error < 0)
351 return 0;
352 }
353 return 1;
354}
355
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400356/**
357 * nfs_pageio_add_list - Split coalesced requests out from a list.
358 * @desc: destination io descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 * @head: source list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 *
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.
364 */
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400365void nfs_pageio_add_list(struct nfs_pageio_descriptor *desc,
366 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 while (!list_empty(head)) {
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400369 struct nfs_page *req = nfs_list_entry(head->next);
370 if (!nfs_pageio_add_request(desc, req))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 break;
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400375/**
376 * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
377 * @desc: pointer to io descriptor
378 */
379void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
380{
381 nfs_pageio_doio(desc);
382}
383
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000384#define NFS_SCAN_MAXENTRIES 16
385/**
Trond Myklebust3f442542006-09-17 14:46:44 -0400386 * nfs_scan_dirty - Scan the radix tree for dirty requests
387 * @mapping: pointer to address space
388 * @wbc: writeback_control structure
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000389 * @dst: Destination list
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000390 *
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
396 */
Trond Myklebust3f442542006-09-17 14:46:44 -0400397long nfs_scan_dirty(struct address_space *mapping,
398 struct writeback_control *wbc,
399 struct list_head *dst)
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000400{
Trond Myklebust3f442542006-09-17 14:46:44 -0400401 struct nfs_inode *nfsi = NFS_I(mapping->host);
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000402 struct nfs_page *pgvec[NFS_SCAN_MAXENTRIES];
403 struct nfs_page *req;
Trond Myklebust3f442542006-09-17 14:46:44 -0400404 pgoff_t idx_start, idx_end;
Trond Myklebust3f442542006-09-17 14:46:44 -0400405 long res = 0;
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000406 int found, i;
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000407
Trond Myklebust9cf85e02006-12-05 00:36:56 -0500408 if (nfsi->ndirty == 0)
Trond Myklebust3f442542006-09-17 14:46:44 -0400409 return 0;
410 if (wbc->range_cyclic) {
411 idx_start = 0;
412 idx_end = ULONG_MAX;
413 } else if (wbc->range_end == 0) {
414 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
415 idx_end = ULONG_MAX;
416 } else {
417 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
418 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
419 }
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000420
421 for (;;) {
Trond Myklebust3f442542006-09-17 14:46:44 -0400422 unsigned int toscan = NFS_SCAN_MAXENTRIES;
423
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000424 found = radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree,
Trond Myklebust3f442542006-09-17 14:46:44 -0400425 (void **)&pgvec[0], idx_start, toscan,
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000426 NFS_PAGE_TAG_DIRTY);
Trond Myklebust3f442542006-09-17 14:46:44 -0400427
428 /* Did we make progress? */
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000429 if (found <= 0)
430 break;
Trond Myklebust3f442542006-09-17 14:46:44 -0400431
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000432 for (i = 0; i < found; i++) {
433 req = pgvec[i];
Trond Myklebust3f442542006-09-17 14:46:44 -0400434 if (!wbc->range_cyclic && req->wb_index > idx_end)
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000435 goto out;
436
Trond Myklebust3f442542006-09-17 14:46:44 -0400437 /* Try to lock request and mark it for writeback */
438 if (!nfs_set_page_writeback_locked(req))
439 goto next;
440 radix_tree_tag_clear(&nfsi->nfs_page_tree,
441 req->wb_index, NFS_PAGE_TAG_DIRTY);
442 nfsi->ndirty--;
443 nfs_list_remove_request(req);
444 nfs_list_add_request(req, dst);
Trond Myklebust3f442542006-09-17 14:46:44 -0400445 res++;
446 if (res == LONG_MAX)
447 goto out;
Trond Myklebust3f442542006-09-17 14:46:44 -0400448next:
449 idx_start = req->wb_index + 1;
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000450 }
451 }
452out:
Trond Myklebust3f442542006-09-17 14:46:44 -0400453 WARN_ON ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty));
Trond Myklebust3da28eb2005-06-22 17:16:31 +0000454 return res;
455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/**
458 * nfs_scan_list - Scan a list for matching requests
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400459 * @nfsi: NFS inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 * @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.
464 *
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
470 */
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400471int nfs_scan_list(struct nfs_inode *nfsi, struct list_head *head,
472 struct list_head *dst, unsigned long idx_start,
473 unsigned int npages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400475 struct nfs_page *pgvec[NFS_SCAN_MAXENTRIES];
476 struct nfs_page *req;
477 unsigned long idx_end;
478 int found, i;
479 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 res = 0;
482 if (npages == 0)
483 idx_end = ~0;
484 else
485 idx_end = idx_start + npages - 1;
486
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400487 for (;;) {
488 found = radix_tree_gang_lookup(&nfsi->nfs_page_tree,
489 (void **)&pgvec[0], idx_start,
490 NFS_SCAN_MAXENTRIES);
491 if (found <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 break;
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400493 for (i = 0; i < found; i++) {
494 req = pgvec[i];
495 if (req->wb_index > idx_end)
496 goto out;
497 idx_start = req->wb_index + 1;
498 if (req->wb_list_head != head)
499 continue;
500 if (nfs_set_page_writeback_locked(req)) {
501 nfs_list_remove_request(req);
502 nfs_list_add_request(req, dst);
503 res++;
504 }
505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
Trond Myklebustd2ccddf2006-05-31 01:13:38 -0400508out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return res;
510}
511
David Howellsf7b422b2006-06-09 09:34:33 -0400512int __init nfs_init_nfspagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 nfs_page_cachep = kmem_cache_create("nfs_page",
515 sizeof(struct nfs_page),
516 0, SLAB_HWCACHE_ALIGN,
517 NULL, NULL);
518 if (nfs_page_cachep == NULL)
519 return -ENOMEM;
520
521 return 0;
522}
523
David Brownell266bee82006-06-27 12:59:15 -0700524void nfs_destroy_nfspagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700526 kmem_cache_destroy(nfs_page_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528