blob: d349bd4c48db5d38e7a9cc3bc2b700af4e219f66 [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>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040014#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/sunrpc/clnt.h>
Trond Myklebust1313e602012-01-17 22:04:24 -050016#include <linux/nfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/nfs3.h>
18#include <linux/nfs4.h>
19#include <linux/nfs_page.h>
20#include <linux/nfs_fs.h>
21#include <linux/nfs_mount.h>
Paul Gortmakerafeacc82011-05-26 16:00:52 -040022#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Trond Myklebust8d5658c2007-04-10 09:26:35 -040024#include "internal.h"
Fred Isamanbae724e2011-03-01 01:34:15 +000025#include "pnfs.h"
Trond Myklebust8d5658c2007-04-10 09:26:35 -040026
Christoph Lametere18b8902006-12-06 20:33:20 -080027static struct kmem_cache *nfs_page_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Fred Isaman30dd3742012-04-20 14:47:45 -040029bool nfs_pgarray_set(struct nfs_page_array *p, unsigned int pagecount)
30{
31 p->npages = pagecount;
32 if (pagecount <= ARRAY_SIZE(p->page_array))
33 p->pagevec = p->page_array;
34 else {
35 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL);
36 if (!p->pagevec)
37 p->npages = 0;
38 }
39 return p->pagevec != NULL;
40}
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static inline struct nfs_page *
43nfs_page_alloc(void)
44{
Jesper Juhl72895b12010-12-09 23:17:15 +010045 struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_KERNEL);
46 if (p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 INIT_LIST_HEAD(&p->wb_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 return p;
49}
50
51static inline void
52nfs_page_free(struct nfs_page *p)
53{
54 kmem_cache_free(nfs_page_cachep, p);
55}
56
57/**
58 * nfs_create_request - Create an NFS read/write request.
Chuck Leverc02f5572011-10-25 12:17:43 -040059 * @ctx: open context to use
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * @inode: inode to which the request is attached
61 * @page: page to write
62 * @offset: starting offset within the page for the write
63 * @count: number of bytes to read/write
64 *
65 * The page must be locked by the caller. This makes sure we never
Jason Uhlenkotta19b89c2007-04-26 17:25:51 -070066 * create two different requests for the same page.
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * User should ensure it is safe to sleep in this function.
68 */
69struct nfs_page *
70nfs_create_request(struct nfs_open_context *ctx, struct inode *inode,
71 struct page *page,
72 unsigned int offset, unsigned int count)
73{
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 struct nfs_page *req;
75
Trond Myklebust18eb8842010-05-13 12:51:02 -040076 /* try to allocate the request struct */
77 req = nfs_page_alloc();
78 if (req == NULL)
79 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Jeff Layton015f0212010-10-28 10:10:37 -040081 /* get lock context early so we can deal with alloc failures */
82 req->wb_lock_context = nfs_get_lock_context(ctx);
83 if (req->wb_lock_context == NULL) {
84 nfs_page_free(req);
85 return ERR_PTR(-ENOMEM);
86 }
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 /* Initialize the request struct. Initially, we assume a
89 * long write-back delay. This will be adjusted in
90 * update_nfs_request below if the region is not locked. */
91 req->wb_page = page;
92 atomic_set(&req->wb_complete, 0);
93 req->wb_index = page->index;
94 page_cache_get(page);
Trond Myklebustcd52ed32006-03-20 13:44:04 -050095 BUG_ON(PagePrivate(page));
96 BUG_ON(!PageLocked(page));
97 BUG_ON(page->mapping->host != inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 req->wb_offset = offset;
99 req->wb_pgbase = offset;
100 req->wb_bytes = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 req->wb_context = get_nfs_open_context(ctx);
Trond Myklebustc03b4022007-06-17 13:26:38 -0400102 kref_init(&req->wb_kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return req;
104}
105
106/**
107 * nfs_unlock_request - Unlock request and wake up sleepers.
108 * @req:
109 */
110void nfs_unlock_request(struct nfs_page *req)
111{
112 if (!NFS_WBACK_BUSY(req)) {
113 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
114 BUG();
115 }
116 smp_mb__before_clear_bit();
117 clear_bit(PG_BUSY, &req->wb_flags);
118 smp_mb__after_clear_bit();
Trond Myklebust464a98b2005-06-22 17:16:21 +0000119 wake_up_bit(&req->wb_flags, PG_BUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 nfs_release_request(req);
121}
122
Trond Myklebust4d65c522011-03-25 14:15:11 -0400123/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * nfs_clear_request - Free up all resources allocated to the request
125 * @req:
126 *
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500127 * Release page and open context resources associated with a read/write
128 * request after it has completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 */
Trond Myklebust4d65c522011-03-25 14:15:11 -0400130static void nfs_clear_request(struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500132 struct page *page = req->wb_page;
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500133 struct nfs_open_context *ctx = req->wb_context;
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400134 struct nfs_lock_context *l_ctx = req->wb_lock_context;
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500135
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500136 if (page != NULL) {
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500137 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 req->wb_page = NULL;
139 }
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400140 if (l_ctx != NULL) {
141 nfs_put_lock_context(l_ctx);
142 req->wb_lock_context = NULL;
143 }
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500144 if (ctx != NULL) {
145 put_nfs_open_context(ctx);
146 req->wb_context = NULL;
147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
150
151/**
152 * nfs_release_request - Release the count on an NFS read/write request
153 * @req: request to release
154 *
155 * Note: Should never be called with the spinlock held!
156 */
Trond Myklebustc03b4022007-06-17 13:26:38 -0400157static void nfs_free_request(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Trond Myklebustc03b4022007-06-17 13:26:38 -0400159 struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500161 /* Release struct file and open context */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 nfs_clear_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 nfs_page_free(req);
164}
165
Trond Myklebustc03b4022007-06-17 13:26:38 -0400166void nfs_release_request(struct nfs_page *req)
167{
168 kref_put(&req->wb_kref, nfs_free_request);
169}
170
Trond Myklebust9f557cd2010-02-03 08:27:22 -0500171static int nfs_wait_bit_uninterruptible(void *word)
172{
173 io_schedule();
174 return 0;
175}
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/**
178 * nfs_wait_on_request - Wait for a request to complete.
179 * @req: request to wait upon.
180 *
Matthew Wilcox150030b2007-12-06 16:24:39 -0500181 * Interruptible by fatal signals only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * The user is responsible for holding a count on the request.
183 */
184int
185nfs_wait_on_request(struct nfs_page *req)
186{
Trond Myklebust9f557cd2010-02-03 08:27:22 -0500187 return wait_on_bit(&req->wb_flags, PG_BUSY,
188 nfs_wait_bit_uninterruptible,
189 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Benny Halevy19345cb2011-06-19 18:33:46 -0400192bool nfs_generic_pg_test(struct nfs_pageio_descriptor *desc, struct nfs_page *prev, struct nfs_page *req)
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300193{
194 /*
195 * FIXME: ideally we should be able to coalesce all requests
196 * that are not block boundary aligned, but currently this
197 * is problematic for the case of bsize < PAGE_CACHE_SIZE,
198 * since nfs_flush_multi and nfs_pagein_multi assume you
199 * can have only one struct nfs_page.
200 */
201 if (desc->pg_bsize < PAGE_SIZE)
202 return 0;
203
204 return desc->pg_count + req->wb_bytes <= desc->pg_bsize;
205}
Benny Halevy19345cb2011-06-19 18:33:46 -0400206EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/**
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400209 * nfs_pageio_init - initialise a page io descriptor
210 * @desc: pointer to descriptor
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400211 * @inode: pointer to inode
212 * @doio: pointer to io function
213 * @bsize: io block size
214 * @io_flags: extra parameters for the io function
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400215 */
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400216void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
217 struct inode *inode,
Trond Myklebust1751c362011-06-10 13:30:23 -0400218 const struct nfs_pageio_ops *pg_ops,
Trond Myklebust84dde762007-05-04 14:44:06 -0400219 size_t bsize,
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400220 int io_flags)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400221{
222 INIT_LIST_HEAD(&desc->pg_list);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400223 desc->pg_bytes_written = 0;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400224 desc->pg_count = 0;
225 desc->pg_bsize = bsize;
226 desc->pg_base = 0;
Trond Myklebustb31268a2011-03-21 17:02:00 -0400227 desc->pg_moreio = 0;
Trond Myklebustd9156f92011-07-12 13:42:02 -0400228 desc->pg_recoalesce = 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400229 desc->pg_inode = inode;
Trond Myklebust1751c362011-06-10 13:30:23 -0400230 desc->pg_ops = pg_ops;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400231 desc->pg_ioflags = io_flags;
232 desc->pg_error = 0;
Fred Isaman94ad1c82011-03-01 01:34:14 +0000233 desc->pg_lseg = NULL;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400234}
235
236/**
237 * nfs_can_coalesce_requests - test two requests for compatibility
238 * @prev: pointer to nfs_page
239 * @req: pointer to nfs_page
240 *
241 * The nfs_page structures 'prev' and 'req' are compared to ensure that the
242 * page data area they describe is contiguous, and that their RPC
243 * credentials, NFSv4 open state, and lockowners are the same.
244 *
245 * Return 'true' if this is the case, else return 'false'.
246 */
Benny Halevy18ad0a92011-05-25 21:03:56 +0300247static bool nfs_can_coalesce_requests(struct nfs_page *prev,
248 struct nfs_page *req,
249 struct nfs_pageio_descriptor *pgio)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400250{
251 if (req->wb_context->cred != prev->wb_context->cred)
Benny Halevy18ad0a92011-05-25 21:03:56 +0300252 return false;
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400253 if (req->wb_lock_context->lockowner != prev->wb_lock_context->lockowner)
Benny Halevy18ad0a92011-05-25 21:03:56 +0300254 return false;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400255 if (req->wb_context->state != prev->wb_context->state)
Benny Halevy18ad0a92011-05-25 21:03:56 +0300256 return false;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400257 if (req->wb_index != (prev->wb_index + 1))
Benny Halevy18ad0a92011-05-25 21:03:56 +0300258 return false;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400259 if (req->wb_pgbase != 0)
Benny Halevy18ad0a92011-05-25 21:03:56 +0300260 return false;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400261 if (prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
Benny Halevy18ad0a92011-05-25 21:03:56 +0300262 return false;
Trond Myklebust1751c362011-06-10 13:30:23 -0400263 return pgio->pg_ops->pg_test(pgio, prev, req);
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400264}
265
266/**
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400267 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400268 * @desc: destination io descriptor
269 * @req: request
270 *
271 * Returns true if the request 'req' was successfully coalesced into the
272 * existing list of pages 'desc'.
273 */
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400274static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
275 struct nfs_page *req)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400276{
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400277 if (desc->pg_count != 0) {
278 struct nfs_page *prev;
279
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400280 prev = nfs_list_entry(desc->pg_list.prev);
Fred Isaman94ad1c82011-03-01 01:34:14 +0000281 if (!nfs_can_coalesce_requests(prev, req, desc))
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400282 return 0;
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300283 } else {
Trond Myklebustd8007d42011-06-10 13:30:23 -0400284 if (desc->pg_ops->pg_init)
285 desc->pg_ops->pg_init(desc, req);
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400286 desc->pg_base = req->wb_pgbase;
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300287 }
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400288 nfs_list_remove_request(req);
289 nfs_list_add_request(req, &desc->pg_list);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300290 desc->pg_count += req->wb_bytes;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400291 return 1;
292}
293
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400294/*
295 * Helper for nfs_pageio_add_request and nfs_pageio_complete
296 */
297static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
298{
299 if (!list_empty(&desc->pg_list)) {
Trond Myklebust1751c362011-06-10 13:30:23 -0400300 int error = desc->pg_ops->pg_doio(desc);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400301 if (error < 0)
302 desc->pg_error = error;
303 else
304 desc->pg_bytes_written += desc->pg_count;
305 }
306 if (list_empty(&desc->pg_list)) {
307 desc->pg_count = 0;
308 desc->pg_base = 0;
309 }
310}
311
312/**
313 * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
314 * @desc: destination io descriptor
315 * @req: request
316 *
317 * Returns true if the request 'req' was successfully coalesced into the
318 * existing list of pages 'desc'.
319 */
Trond Myklebustd9156f92011-07-12 13:42:02 -0400320static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400321 struct nfs_page *req)
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400322{
323 while (!nfs_pageio_do_add_request(desc, req)) {
Trond Myklebustb31268a2011-03-21 17:02:00 -0400324 desc->pg_moreio = 1;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400325 nfs_pageio_doio(desc);
326 if (desc->pg_error < 0)
327 return 0;
Trond Myklebustb31268a2011-03-21 17:02:00 -0400328 desc->pg_moreio = 0;
Trond Myklebustd9156f92011-07-12 13:42:02 -0400329 if (desc->pg_recoalesce)
330 return 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400331 }
332 return 1;
333}
334
Trond Myklebustd9156f92011-07-12 13:42:02 -0400335static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
336{
337 LIST_HEAD(head);
338
339 do {
340 list_splice_init(&desc->pg_list, &head);
341 desc->pg_bytes_written -= desc->pg_count;
342 desc->pg_count = 0;
343 desc->pg_base = 0;
344 desc->pg_recoalesce = 0;
345
346 while (!list_empty(&head)) {
347 struct nfs_page *req;
348
349 req = list_first_entry(&head, struct nfs_page, wb_list);
350 nfs_list_remove_request(req);
351 if (__nfs_pageio_add_request(desc, req))
352 continue;
353 if (desc->pg_error < 0)
354 return 0;
355 break;
356 }
357 } while (desc->pg_recoalesce);
358 return 1;
359}
360
361int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
362 struct nfs_page *req)
363{
364 int ret;
365
366 do {
367 ret = __nfs_pageio_add_request(desc, req);
368 if (ret)
369 break;
370 if (desc->pg_error < 0)
371 break;
372 ret = nfs_do_recoalesce(desc);
373 } while (ret);
374 return ret;
375}
376
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400377/**
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400378 * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
379 * @desc: pointer to io descriptor
380 */
381void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
382{
Trond Myklebustd9156f92011-07-12 13:42:02 -0400383 for (;;) {
384 nfs_pageio_doio(desc);
385 if (!desc->pg_recoalesce)
386 break;
387 if (!nfs_do_recoalesce(desc))
388 break;
389 }
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400390}
391
Trond Myklebust7fe7f842007-05-20 10:18:27 -0400392/**
393 * nfs_pageio_cond_complete - Conditional I/O completion
394 * @desc: pointer to io descriptor
395 * @index: page index
396 *
397 * It is important to ensure that processes don't try to take locks
398 * on non-contiguous ranges of pages as that might deadlock. This
399 * function should be called before attempting to wait on a locked
400 * nfs_page. It will complete the I/O if the page index 'index'
401 * is not contiguous with the existing list of pages in 'desc'.
402 */
403void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
404{
405 if (!list_empty(&desc->pg_list)) {
406 struct nfs_page *prev = nfs_list_entry(desc->pg_list.prev);
407 if (index != prev->wb_index + 1)
Trond Myklebustd9156f92011-07-12 13:42:02 -0400408 nfs_pageio_complete(desc);
Trond Myklebust7fe7f842007-05-20 10:18:27 -0400409 }
410}
411
David Howellsf7b422b2006-06-09 09:34:33 -0400412int __init nfs_init_nfspagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 nfs_page_cachep = kmem_cache_create("nfs_page",
415 sizeof(struct nfs_page),
416 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +0900417 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (nfs_page_cachep == NULL)
419 return -ENOMEM;
420
421 return 0;
422}
423
David Brownell266bee82006-06-27 12:59:15 -0700424void nfs_destroy_nfspagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700426 kmem_cache_destroy(nfs_page_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428