blob: f74df87058b61bd9742e1ab6bdffdc50c868a454 [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
Anna Schumaker0eecb212014-05-06 09:12:32 -040027#define NFSDBG_FACILITY NFSDBG_PAGECACHE
28
Christoph Lametere18b8902006-12-06 20:33:20 -080029static struct kmem_cache *nfs_page_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Anna Schumaker00bfa302014-05-06 09:12:29 -040031static bool nfs_pgarray_set(struct nfs_page_array *p, unsigned int pagecount)
Fred Isaman30dd3742012-04-20 14:47:45 -040032{
33 p->npages = pagecount;
34 if (pagecount <= ARRAY_SIZE(p->page_array))
35 p->pagevec = p->page_array;
36 else {
37 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL);
38 if (!p->pagevec)
39 p->npages = 0;
40 }
41 return p->pagevec != NULL;
42}
43
Fred Isaman4db6e0b2012-04-20 14:47:46 -040044void nfs_pgheader_init(struct nfs_pageio_descriptor *desc,
45 struct nfs_pgio_header *hdr,
46 void (*release)(struct nfs_pgio_header *hdr))
47{
48 hdr->req = nfs_list_entry(desc->pg_list.next);
49 hdr->inode = desc->pg_inode;
50 hdr->cred = hdr->req->wb_context->cred;
51 hdr->io_start = req_offset(hdr->req);
52 hdr->good_bytes = desc->pg_count;
Fred Isaman584aa812012-04-20 14:47:51 -040053 hdr->dreq = desc->pg_dreq;
Peng Taof6166382012-08-02 15:36:09 +030054 hdr->layout_private = desc->pg_layout_private;
Fred Isaman4db6e0b2012-04-20 14:47:46 -040055 hdr->release = release;
Fred Isaman061ae2e2012-04-20 14:47:48 -040056 hdr->completion_ops = desc->pg_completion_ops;
Fred Isaman584aa812012-04-20 14:47:51 -040057 if (hdr->completion_ops->init_hdr)
58 hdr->completion_ops->init_hdr(hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -040059}
Bryan Schumaker89d77c82012-07-30 16:05:25 -040060EXPORT_SYMBOL_GPL(nfs_pgheader_init);
Fred Isaman4db6e0b2012-04-20 14:47:46 -040061
62void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos)
63{
64 spin_lock(&hdr->lock);
65 if (pos < hdr->io_start + hdr->good_bytes) {
66 set_bit(NFS_IOHDR_ERROR, &hdr->flags);
67 clear_bit(NFS_IOHDR_EOF, &hdr->flags);
68 hdr->good_bytes = pos - hdr->io_start;
69 hdr->error = error;
70 }
71 spin_unlock(&hdr->lock);
72}
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static inline struct nfs_page *
75nfs_page_alloc(void)
76{
Mel Gorman192e5012012-07-31 16:45:16 -070077 struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_NOIO);
Jesper Juhl72895b12010-12-09 23:17:15 +010078 if (p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 INIT_LIST_HEAD(&p->wb_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return p;
81}
82
83static inline void
84nfs_page_free(struct nfs_page *p)
85{
86 kmem_cache_free(nfs_page_cachep, p);
87}
88
Trond Myklebust577b4232013-04-08 21:38:12 -040089static void
90nfs_iocounter_inc(struct nfs_io_counter *c)
91{
92 atomic_inc(&c->io_count);
93}
94
95static void
96nfs_iocounter_dec(struct nfs_io_counter *c)
97{
98 if (atomic_dec_and_test(&c->io_count)) {
99 clear_bit(NFS_IO_INPROGRESS, &c->flags);
100 smp_mb__after_clear_bit();
101 wake_up_bit(&c->flags, NFS_IO_INPROGRESS);
102 }
103}
104
105static int
106__nfs_iocounter_wait(struct nfs_io_counter *c)
107{
108 wait_queue_head_t *wq = bit_waitqueue(&c->flags, NFS_IO_INPROGRESS);
109 DEFINE_WAIT_BIT(q, &c->flags, NFS_IO_INPROGRESS);
110 int ret = 0;
111
112 do {
113 prepare_to_wait(wq, &q.wait, TASK_KILLABLE);
114 set_bit(NFS_IO_INPROGRESS, &c->flags);
115 if (atomic_read(&c->io_count) == 0)
116 break;
117 ret = nfs_wait_bit_killable(&c->flags);
118 } while (atomic_read(&c->io_count) != 0);
119 finish_wait(wq, &q.wait);
120 return ret;
121}
122
123/**
124 * nfs_iocounter_wait - wait for i/o to complete
125 * @c: nfs_io_counter to use
126 *
127 * returns -ERESTARTSYS if interrupted by a fatal signal.
128 * Otherwise returns 0 once the io_count hits 0.
129 */
130int
131nfs_iocounter_wait(struct nfs_io_counter *c)
132{
133 if (atomic_read(&c->io_count) == 0)
134 return 0;
135 return __nfs_iocounter_wait(c);
136}
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/**
139 * nfs_create_request - Create an NFS read/write request.
Chuck Leverc02f5572011-10-25 12:17:43 -0400140 * @ctx: open context to use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 * @inode: inode to which the request is attached
142 * @page: page to write
143 * @offset: starting offset within the page for the write
144 * @count: number of bytes to read/write
145 *
146 * The page must be locked by the caller. This makes sure we never
Jason Uhlenkotta19b89c2007-04-26 17:25:51 -0700147 * create two different requests for the same page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 * User should ensure it is safe to sleep in this function.
149 */
150struct nfs_page *
151nfs_create_request(struct nfs_open_context *ctx, struct inode *inode,
152 struct page *page,
153 unsigned int offset, unsigned int count)
154{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct nfs_page *req;
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400156 struct nfs_lock_context *l_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Trond Myklebustc58c8442013-03-18 19:45:14 -0400158 if (test_bit(NFS_CONTEXT_BAD, &ctx->flags))
159 return ERR_PTR(-EBADF);
Trond Myklebust18eb8842010-05-13 12:51:02 -0400160 /* try to allocate the request struct */
161 req = nfs_page_alloc();
162 if (req == NULL)
163 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Jeff Layton015f0212010-10-28 10:10:37 -0400165 /* get lock context early so we can deal with alloc failures */
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400166 l_ctx = nfs_get_lock_context(ctx);
167 if (IS_ERR(l_ctx)) {
Jeff Layton015f0212010-10-28 10:10:37 -0400168 nfs_page_free(req);
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400169 return ERR_CAST(l_ctx);
Jeff Layton015f0212010-10-28 10:10:37 -0400170 }
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400171 req->wb_lock_context = l_ctx;
Trond Myklebust577b4232013-04-08 21:38:12 -0400172 nfs_iocounter_inc(&l_ctx->io_count);
Jeff Layton015f0212010-10-28 10:10:37 -0400173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 /* Initialize the request struct. Initially, we assume a
175 * long write-back delay. This will be adjusted in
176 * update_nfs_request below if the region is not locked. */
177 req->wb_page = page;
Mel Gormand56b4dd2012-07-31 16:45:06 -0700178 req->wb_index = page_file_index(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 page_cache_get(page);
180 req->wb_offset = offset;
181 req->wb_pgbase = offset;
182 req->wb_bytes = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 req->wb_context = get_nfs_open_context(ctx);
Trond Myklebustc03b4022007-06-17 13:26:38 -0400184 kref_init(&req->wb_kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return req;
186}
187
188/**
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400189 * nfs_unlock_request - Unlock request and wake up sleepers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * @req:
191 */
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400192void nfs_unlock_request(struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 if (!NFS_WBACK_BUSY(req)) {
195 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
196 BUG();
197 }
198 smp_mb__before_clear_bit();
199 clear_bit(PG_BUSY, &req->wb_flags);
200 smp_mb__after_clear_bit();
Trond Myklebust464a98b2005-06-22 17:16:21 +0000201 wake_up_bit(&req->wb_flags, PG_BUSY);
Trond Myklebust3aff4eb2012-05-09 14:30:35 -0400202}
203
204/**
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400205 * nfs_unlock_and_release_request - Unlock request and release the nfs_page
206 * @req:
Trond Myklebust3aff4eb2012-05-09 14:30:35 -0400207 */
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400208void nfs_unlock_and_release_request(struct nfs_page *req)
Trond Myklebust3aff4eb2012-05-09 14:30:35 -0400209{
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400210 nfs_unlock_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 nfs_release_request(req);
212}
213
Trond Myklebust4d65c522011-03-25 14:15:11 -0400214/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * nfs_clear_request - Free up all resources allocated to the request
216 * @req:
217 *
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500218 * Release page and open context resources associated with a read/write
219 * request after it has completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 */
Trond Myklebust4d65c522011-03-25 14:15:11 -0400221static void nfs_clear_request(struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500223 struct page *page = req->wb_page;
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500224 struct nfs_open_context *ctx = req->wb_context;
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400225 struct nfs_lock_context *l_ctx = req->wb_lock_context;
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500226
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500227 if (page != NULL) {
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500228 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 req->wb_page = NULL;
230 }
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400231 if (l_ctx != NULL) {
Trond Myklebust577b4232013-04-08 21:38:12 -0400232 nfs_iocounter_dec(&l_ctx->io_count);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400233 nfs_put_lock_context(l_ctx);
234 req->wb_lock_context = NULL;
235 }
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500236 if (ctx != NULL) {
237 put_nfs_open_context(ctx);
238 req->wb_context = NULL;
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242
243/**
244 * nfs_release_request - Release the count on an NFS read/write request
245 * @req: request to release
246 *
247 * Note: Should never be called with the spinlock held!
248 */
Trond Myklebustc03b4022007-06-17 13:26:38 -0400249static void nfs_free_request(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Trond Myklebustc03b4022007-06-17 13:26:38 -0400251 struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500253 /* Release struct file and open context */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 nfs_clear_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 nfs_page_free(req);
256}
257
Trond Myklebustc03b4022007-06-17 13:26:38 -0400258void nfs_release_request(struct nfs_page *req)
259{
260 kref_put(&req->wb_kref, nfs_free_request);
261}
262
Trond Myklebust9f557cd2010-02-03 08:27:22 -0500263static int nfs_wait_bit_uninterruptible(void *word)
264{
265 io_schedule();
266 return 0;
267}
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269/**
270 * nfs_wait_on_request - Wait for a request to complete.
271 * @req: request to wait upon.
272 *
Matthew Wilcox150030b2007-12-06 16:24:39 -0500273 * Interruptible by fatal signals only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 * The user is responsible for holding a count on the request.
275 */
276int
277nfs_wait_on_request(struct nfs_page *req)
278{
Trond Myklebust9f557cd2010-02-03 08:27:22 -0500279 return wait_on_bit(&req->wb_flags, PG_BUSY,
280 nfs_wait_bit_uninterruptible,
281 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
Benny Halevy19345cb2011-06-19 18:33:46 -0400284bool nfs_generic_pg_test(struct nfs_pageio_descriptor *desc, struct nfs_page *prev, struct nfs_page *req)
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300285{
286 /*
287 * FIXME: ideally we should be able to coalesce all requests
288 * that are not block boundary aligned, but currently this
289 * is problematic for the case of bsize < PAGE_CACHE_SIZE,
290 * since nfs_flush_multi and nfs_pagein_multi assume you
291 * can have only one struct nfs_page.
292 */
293 if (desc->pg_bsize < PAGE_SIZE)
294 return 0;
295
296 return desc->pg_count + req->wb_bytes <= desc->pg_bsize;
297}
Benny Halevy19345cb2011-06-19 18:33:46 -0400298EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300299
Anna Schumaker00bfa302014-05-06 09:12:29 -0400300static inline struct nfs_rw_header *NFS_RW_HEADER(struct nfs_pgio_header *hdr)
301{
302 return container_of(hdr, struct nfs_rw_header, header);
303}
304
305/**
Anna Schumaker4a0de552014-05-06 09:12:30 -0400306 * nfs_rw_header_alloc - Allocate a header for a read or write
307 * @ops: Read or write function vector
308 */
309struct nfs_rw_header *nfs_rw_header_alloc(const struct nfs_rw_ops *ops)
310{
311 struct nfs_rw_header *header = ops->rw_alloc_header();
312
313 if (header) {
314 struct nfs_pgio_header *hdr = &header->header;
315
316 INIT_LIST_HEAD(&hdr->pages);
317 INIT_LIST_HEAD(&hdr->rpc_list);
318 spin_lock_init(&hdr->lock);
319 atomic_set(&hdr->refcnt, 0);
320 hdr->rw_ops = ops;
321 }
322 return header;
323}
324EXPORT_SYMBOL_GPL(nfs_rw_header_alloc);
325
326/*
327 * nfs_rw_header_free - Free a read or write header
328 * @hdr: The header to free
329 */
330void nfs_rw_header_free(struct nfs_pgio_header *hdr)
331{
332 hdr->rw_ops->rw_free_header(NFS_RW_HEADER(hdr));
333}
334EXPORT_SYMBOL_GPL(nfs_rw_header_free);
335
336/**
Anna Schumaker00bfa302014-05-06 09:12:29 -0400337 * nfs_pgio_data_alloc - Allocate pageio data
338 * @hdr: The header making a request
339 * @pagecount: Number of pages to create
340 */
341struct nfs_pgio_data *nfs_pgio_data_alloc(struct nfs_pgio_header *hdr,
342 unsigned int pagecount)
343{
344 struct nfs_pgio_data *data, *prealloc;
345
346 prealloc = &NFS_RW_HEADER(hdr)->rpc_data;
347 if (prealloc->header == NULL)
348 data = prealloc;
349 else
350 data = kzalloc(sizeof(*data), GFP_KERNEL);
351 if (!data)
352 goto out;
353
354 if (nfs_pgarray_set(&data->pages, pagecount)) {
355 data->header = hdr;
356 atomic_inc(&hdr->refcnt);
357 } else {
358 if (data != prealloc)
359 kfree(data);
360 data = NULL;
361 }
362out:
363 return data;
364}
365
366/**
367 * nfs_pgio_data_release - Properly free pageio data
368 * @data: The data to release
369 */
370void nfs_pgio_data_release(struct nfs_pgio_data *data)
371{
372 struct nfs_pgio_header *hdr = data->header;
373 struct nfs_rw_header *pageio_header = NFS_RW_HEADER(hdr);
374
375 put_nfs_open_context(data->args.context);
376 if (data->pages.pagevec != data->pages.page_array)
377 kfree(data->pages.pagevec);
378 if (data == &pageio_header->rpc_data) {
379 data->header = NULL;
380 data = NULL;
381 }
382 if (atomic_dec_and_test(&hdr->refcnt))
383 hdr->completion_ops->completion(hdr);
384 /* Note: we only free the rpc_task after callbacks are done.
385 * See the comment in rpc_free_task() for why
386 */
387 kfree(data);
388}
389EXPORT_SYMBOL_GPL(nfs_pgio_data_release);
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391/**
Anna Schumakera4cdda52014-05-06 09:12:31 -0400392 * nfs_pgio_prepare - Prepare pageio data to go over the wire
393 * @task: The current task
394 * @calldata: pageio data to prepare
395 */
396void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
397{
398 struct nfs_pgio_data *data = calldata;
399 int err;
400 err = NFS_PROTO(data->header->inode)->pgio_rpc_prepare(task, data);
401 if (err)
402 rpc_exit(task, err);
403}
404
405/**
406 * nfs_pgio_release - Release pageio data
407 * @calldata: The pageio data to release
408 */
409void nfs_pgio_release(void *calldata)
410{
411 struct nfs_pgio_data *data = calldata;
412 if (data->header->rw_ops->rw_release)
413 data->header->rw_ops->rw_release(data);
414 nfs_pgio_data_release(data);
415}
416
417/**
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400418 * nfs_pageio_init - initialise a page io descriptor
419 * @desc: pointer to descriptor
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400420 * @inode: pointer to inode
421 * @doio: pointer to io function
422 * @bsize: io block size
423 * @io_flags: extra parameters for the io function
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400424 */
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400425void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
426 struct inode *inode,
Trond Myklebust1751c362011-06-10 13:30:23 -0400427 const struct nfs_pageio_ops *pg_ops,
Fred Isaman061ae2e2012-04-20 14:47:48 -0400428 const struct nfs_pgio_completion_ops *compl_ops,
Anna Schumaker4a0de552014-05-06 09:12:30 -0400429 const struct nfs_rw_ops *rw_ops,
Trond Myklebust84dde762007-05-04 14:44:06 -0400430 size_t bsize,
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400431 int io_flags)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400432{
433 INIT_LIST_HEAD(&desc->pg_list);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400434 desc->pg_bytes_written = 0;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400435 desc->pg_count = 0;
436 desc->pg_bsize = bsize;
437 desc->pg_base = 0;
Trond Myklebustb31268a2011-03-21 17:02:00 -0400438 desc->pg_moreio = 0;
Trond Myklebustd9156f92011-07-12 13:42:02 -0400439 desc->pg_recoalesce = 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400440 desc->pg_inode = inode;
Trond Myklebust1751c362011-06-10 13:30:23 -0400441 desc->pg_ops = pg_ops;
Fred Isaman061ae2e2012-04-20 14:47:48 -0400442 desc->pg_completion_ops = compl_ops;
Anna Schumaker4a0de552014-05-06 09:12:30 -0400443 desc->pg_rw_ops = rw_ops;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400444 desc->pg_ioflags = io_flags;
445 desc->pg_error = 0;
Fred Isaman94ad1c82011-03-01 01:34:14 +0000446 desc->pg_lseg = NULL;
Fred Isaman584aa812012-04-20 14:47:51 -0400447 desc->pg_dreq = NULL;
Peng Taof6166382012-08-02 15:36:09 +0300448 desc->pg_layout_private = NULL;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400449}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400450EXPORT_SYMBOL_GPL(nfs_pageio_init);
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400451
Anna Schumaker0eecb212014-05-06 09:12:32 -0400452/**
453 * nfs_pgio_result - Basic pageio error handling
454 * @task: The task that ran
455 * @calldata: Pageio data to check
456 */
457void nfs_pgio_result(struct rpc_task *task, void *calldata)
458{
459 struct nfs_pgio_data *data = calldata;
460 struct inode *inode = data->header->inode;
461
462 dprintk("NFS: %s: %5u, (status %d)\n", __func__,
463 task->tk_pid, task->tk_status);
464
465 if (data->header->rw_ops->rw_done(task, data, inode) != 0)
466 return;
467 if (task->tk_status < 0)
468 nfs_set_pgio_error(data->header, task->tk_status, data->args.offset);
469 else
470 data->header->rw_ops->rw_result(task, data);
471}
472
Trond Myklebust4109bb72013-09-06 11:09:38 -0400473static bool nfs_match_open_context(const struct nfs_open_context *ctx1,
474 const struct nfs_open_context *ctx2)
475{
476 return ctx1->cred == ctx2->cred && ctx1->state == ctx2->state;
477}
478
479static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
480 const struct nfs_lock_context *l2)
481{
482 return l1->lockowner.l_owner == l2->lockowner.l_owner
483 && l1->lockowner.l_pid == l2->lockowner.l_pid;
484}
485
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400486/**
487 * nfs_can_coalesce_requests - test two requests for compatibility
488 * @prev: pointer to nfs_page
489 * @req: pointer to nfs_page
490 *
491 * The nfs_page structures 'prev' and 'req' are compared to ensure that the
492 * page data area they describe is contiguous, and that their RPC
493 * credentials, NFSv4 open state, and lockowners are the same.
494 *
495 * Return 'true' if this is the case, else return 'false'.
496 */
Benny Halevy18ad0a92011-05-25 21:03:56 +0300497static bool nfs_can_coalesce_requests(struct nfs_page *prev,
498 struct nfs_page *req,
499 struct nfs_pageio_descriptor *pgio)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400500{
Trond Myklebust4109bb72013-09-06 11:09:38 -0400501 if (!nfs_match_open_context(req->wb_context, prev->wb_context))
Benny Halevy18ad0a92011-05-25 21:03:56 +0300502 return false;
Trond Myklebust4109bb72013-09-06 11:09:38 -0400503 if (req->wb_context->dentry->d_inode->i_flock != NULL &&
504 !nfs_match_lock_context(req->wb_lock_context, prev->wb_lock_context))
Benny Halevy18ad0a92011-05-25 21:03:56 +0300505 return false;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400506 if (req->wb_pgbase != 0)
Benny Halevy18ad0a92011-05-25 21:03:56 +0300507 return false;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400508 if (prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
Benny Halevy18ad0a92011-05-25 21:03:56 +0300509 return false;
Fred Isaman1825a0d2012-04-20 19:55:31 -0400510 if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
511 return false;
Trond Myklebust1751c362011-06-10 13:30:23 -0400512 return pgio->pg_ops->pg_test(pgio, prev, req);
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400513}
514
515/**
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400516 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400517 * @desc: destination io descriptor
518 * @req: request
519 *
520 * Returns true if the request 'req' was successfully coalesced into the
521 * existing list of pages 'desc'.
522 */
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400523static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
524 struct nfs_page *req)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400525{
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400526 if (desc->pg_count != 0) {
527 struct nfs_page *prev;
528
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400529 prev = nfs_list_entry(desc->pg_list.prev);
Fred Isaman94ad1c82011-03-01 01:34:14 +0000530 if (!nfs_can_coalesce_requests(prev, req, desc))
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400531 return 0;
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300532 } else {
Trond Myklebustd8007d42011-06-10 13:30:23 -0400533 if (desc->pg_ops->pg_init)
534 desc->pg_ops->pg_init(desc, req);
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400535 desc->pg_base = req->wb_pgbase;
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300536 }
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400537 nfs_list_remove_request(req);
538 nfs_list_add_request(req, &desc->pg_list);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300539 desc->pg_count += req->wb_bytes;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400540 return 1;
541}
542
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400543/*
544 * Helper for nfs_pageio_add_request and nfs_pageio_complete
545 */
546static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
547{
548 if (!list_empty(&desc->pg_list)) {
Trond Myklebust1751c362011-06-10 13:30:23 -0400549 int error = desc->pg_ops->pg_doio(desc);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400550 if (error < 0)
551 desc->pg_error = error;
552 else
553 desc->pg_bytes_written += desc->pg_count;
554 }
555 if (list_empty(&desc->pg_list)) {
556 desc->pg_count = 0;
557 desc->pg_base = 0;
558 }
559}
560
561/**
562 * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
563 * @desc: destination io descriptor
564 * @req: request
565 *
566 * Returns true if the request 'req' was successfully coalesced into the
567 * existing list of pages 'desc'.
568 */
Trond Myklebustd9156f92011-07-12 13:42:02 -0400569static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400570 struct nfs_page *req)
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400571{
572 while (!nfs_pageio_do_add_request(desc, req)) {
Trond Myklebustb31268a2011-03-21 17:02:00 -0400573 desc->pg_moreio = 1;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400574 nfs_pageio_doio(desc);
575 if (desc->pg_error < 0)
576 return 0;
Trond Myklebustb31268a2011-03-21 17:02:00 -0400577 desc->pg_moreio = 0;
Trond Myklebustd9156f92011-07-12 13:42:02 -0400578 if (desc->pg_recoalesce)
579 return 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400580 }
581 return 1;
582}
583
Trond Myklebustd9156f92011-07-12 13:42:02 -0400584static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
585{
586 LIST_HEAD(head);
587
588 do {
589 list_splice_init(&desc->pg_list, &head);
590 desc->pg_bytes_written -= desc->pg_count;
591 desc->pg_count = 0;
592 desc->pg_base = 0;
593 desc->pg_recoalesce = 0;
594
595 while (!list_empty(&head)) {
596 struct nfs_page *req;
597
598 req = list_first_entry(&head, struct nfs_page, wb_list);
599 nfs_list_remove_request(req);
600 if (__nfs_pageio_add_request(desc, req))
601 continue;
602 if (desc->pg_error < 0)
603 return 0;
604 break;
605 }
606 } while (desc->pg_recoalesce);
607 return 1;
608}
609
610int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
611 struct nfs_page *req)
612{
613 int ret;
614
615 do {
616 ret = __nfs_pageio_add_request(desc, req);
617 if (ret)
618 break;
619 if (desc->pg_error < 0)
620 break;
621 ret = nfs_do_recoalesce(desc);
622 } while (ret);
623 return ret;
624}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400625EXPORT_SYMBOL_GPL(nfs_pageio_add_request);
Trond Myklebustd9156f92011-07-12 13:42:02 -0400626
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400627/**
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400628 * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
629 * @desc: pointer to io descriptor
630 */
631void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
632{
Trond Myklebustd9156f92011-07-12 13:42:02 -0400633 for (;;) {
634 nfs_pageio_doio(desc);
635 if (!desc->pg_recoalesce)
636 break;
637 if (!nfs_do_recoalesce(desc))
638 break;
639 }
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400640}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400641EXPORT_SYMBOL_GPL(nfs_pageio_complete);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400642
Trond Myklebust7fe7f842007-05-20 10:18:27 -0400643/**
644 * nfs_pageio_cond_complete - Conditional I/O completion
645 * @desc: pointer to io descriptor
646 * @index: page index
647 *
648 * It is important to ensure that processes don't try to take locks
649 * on non-contiguous ranges of pages as that might deadlock. This
650 * function should be called before attempting to wait on a locked
651 * nfs_page. It will complete the I/O if the page index 'index'
652 * is not contiguous with the existing list of pages in 'desc'.
653 */
654void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
655{
656 if (!list_empty(&desc->pg_list)) {
657 struct nfs_page *prev = nfs_list_entry(desc->pg_list.prev);
658 if (index != prev->wb_index + 1)
Trond Myklebustd9156f92011-07-12 13:42:02 -0400659 nfs_pageio_complete(desc);
Trond Myklebust7fe7f842007-05-20 10:18:27 -0400660 }
661}
662
David Howellsf7b422b2006-06-09 09:34:33 -0400663int __init nfs_init_nfspagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 nfs_page_cachep = kmem_cache_create("nfs_page",
666 sizeof(struct nfs_page),
667 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +0900668 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (nfs_page_cachep == NULL)
670 return -ENOMEM;
671
672 return 0;
673}
674
David Brownell266bee82006-06-27 12:59:15 -0700675void nfs_destroy_nfspagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700677 kmem_cache_destroy(nfs_page_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679