blob: 82233431880dc25a5794c7b8cdb561f2cb715b1b [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;
Anna Schumakeref2c4882014-05-06 09:12:36 -040030static const struct rpc_call_ops nfs_pgio_common_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Anna Schumaker00bfa302014-05-06 09:12:29 -040032static bool nfs_pgarray_set(struct nfs_page_array *p, unsigned int pagecount)
Fred Isaman30dd3742012-04-20 14:47:45 -040033{
34 p->npages = pagecount;
35 if (pagecount <= ARRAY_SIZE(p->page_array))
36 p->pagevec = p->page_array;
37 else {
38 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL);
39 if (!p->pagevec)
40 p->npages = 0;
41 }
42 return p->pagevec != NULL;
43}
44
Fred Isaman4db6e0b2012-04-20 14:47:46 -040045void nfs_pgheader_init(struct nfs_pageio_descriptor *desc,
46 struct nfs_pgio_header *hdr,
47 void (*release)(struct nfs_pgio_header *hdr))
48{
49 hdr->req = nfs_list_entry(desc->pg_list.next);
50 hdr->inode = desc->pg_inode;
51 hdr->cred = hdr->req->wb_context->cred;
52 hdr->io_start = req_offset(hdr->req);
53 hdr->good_bytes = desc->pg_count;
Fred Isaman584aa812012-04-20 14:47:51 -040054 hdr->dreq = desc->pg_dreq;
Peng Taof6166382012-08-02 15:36:09 +030055 hdr->layout_private = desc->pg_layout_private;
Fred Isaman4db6e0b2012-04-20 14:47:46 -040056 hdr->release = release;
Fred Isaman061ae2e2012-04-20 14:47:48 -040057 hdr->completion_ops = desc->pg_completion_ops;
Fred Isaman584aa812012-04-20 14:47:51 -040058 if (hdr->completion_ops->init_hdr)
59 hdr->completion_ops->init_hdr(hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -040060}
Bryan Schumaker89d77c82012-07-30 16:05:25 -040061EXPORT_SYMBOL_GPL(nfs_pgheader_init);
Fred Isaman4db6e0b2012-04-20 14:47:46 -040062
63void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos)
64{
65 spin_lock(&hdr->lock);
66 if (pos < hdr->io_start + hdr->good_bytes) {
67 set_bit(NFS_IOHDR_ERROR, &hdr->flags);
68 clear_bit(NFS_IOHDR_EOF, &hdr->flags);
69 hdr->good_bytes = pos - hdr->io_start;
70 hdr->error = error;
71 }
72 spin_unlock(&hdr->lock);
73}
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static inline struct nfs_page *
76nfs_page_alloc(void)
77{
Mel Gorman192e5012012-07-31 16:45:16 -070078 struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_NOIO);
Jesper Juhl72895b12010-12-09 23:17:15 +010079 if (p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 INIT_LIST_HEAD(&p->wb_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return p;
82}
83
84static inline void
85nfs_page_free(struct nfs_page *p)
86{
87 kmem_cache_free(nfs_page_cachep, p);
88}
89
Trond Myklebust577b4232013-04-08 21:38:12 -040090static void
91nfs_iocounter_inc(struct nfs_io_counter *c)
92{
93 atomic_inc(&c->io_count);
94}
95
96static void
97nfs_iocounter_dec(struct nfs_io_counter *c)
98{
99 if (atomic_dec_and_test(&c->io_count)) {
100 clear_bit(NFS_IO_INPROGRESS, &c->flags);
101 smp_mb__after_clear_bit();
102 wake_up_bit(&c->flags, NFS_IO_INPROGRESS);
103 }
104}
105
106static int
107__nfs_iocounter_wait(struct nfs_io_counter *c)
108{
109 wait_queue_head_t *wq = bit_waitqueue(&c->flags, NFS_IO_INPROGRESS);
110 DEFINE_WAIT_BIT(q, &c->flags, NFS_IO_INPROGRESS);
111 int ret = 0;
112
113 do {
114 prepare_to_wait(wq, &q.wait, TASK_KILLABLE);
115 set_bit(NFS_IO_INPROGRESS, &c->flags);
116 if (atomic_read(&c->io_count) == 0)
117 break;
118 ret = nfs_wait_bit_killable(&c->flags);
119 } while (atomic_read(&c->io_count) != 0);
120 finish_wait(wq, &q.wait);
121 return ret;
122}
123
124/**
125 * nfs_iocounter_wait - wait for i/o to complete
126 * @c: nfs_io_counter to use
127 *
128 * returns -ERESTARTSYS if interrupted by a fatal signal.
129 * Otherwise returns 0 once the io_count hits 0.
130 */
131int
132nfs_iocounter_wait(struct nfs_io_counter *c)
133{
134 if (atomic_read(&c->io_count) == 0)
135 return 0;
136 return __nfs_iocounter_wait(c);
137}
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139/**
140 * nfs_create_request - Create an NFS read/write request.
Chuck Leverc02f5572011-10-25 12:17:43 -0400141 * @ctx: open context to use
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 * @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 *
Weston Andros Adamson8c8f1ac12014-05-15 11:56:42 -0400151nfs_create_request(struct nfs_open_context *ctx, struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 unsigned int offset, unsigned int count)
153{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct nfs_page *req;
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400155 struct nfs_lock_context *l_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Trond Myklebustc58c8442013-03-18 19:45:14 -0400157 if (test_bit(NFS_CONTEXT_BAD, &ctx->flags))
158 return ERR_PTR(-EBADF);
Trond Myklebust18eb8842010-05-13 12:51:02 -0400159 /* try to allocate the request struct */
160 req = nfs_page_alloc();
161 if (req == NULL)
162 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Jeff Layton015f0212010-10-28 10:10:37 -0400164 /* get lock context early so we can deal with alloc failures */
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400165 l_ctx = nfs_get_lock_context(ctx);
166 if (IS_ERR(l_ctx)) {
Jeff Layton015f0212010-10-28 10:10:37 -0400167 nfs_page_free(req);
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400168 return ERR_CAST(l_ctx);
Jeff Layton015f0212010-10-28 10:10:37 -0400169 }
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400170 req->wb_lock_context = l_ctx;
Trond Myklebust577b4232013-04-08 21:38:12 -0400171 nfs_iocounter_inc(&l_ctx->io_count);
Jeff Layton015f0212010-10-28 10:10:37 -0400172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 /* Initialize the request struct. Initially, we assume a
174 * long write-back delay. This will be adjusted in
175 * update_nfs_request below if the region is not locked. */
176 req->wb_page = page;
Mel Gormand56b4dd2012-07-31 16:45:06 -0700177 req->wb_index = page_file_index(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 page_cache_get(page);
179 req->wb_offset = offset;
180 req->wb_pgbase = offset;
181 req->wb_bytes = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 req->wb_context = get_nfs_open_context(ctx);
Trond Myklebustc03b4022007-06-17 13:26:38 -0400183 kref_init(&req->wb_kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return req;
185}
186
187/**
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400188 * nfs_unlock_request - Unlock request and wake up sleepers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * @req:
190 */
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400191void nfs_unlock_request(struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 if (!NFS_WBACK_BUSY(req)) {
194 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
195 BUG();
196 }
197 smp_mb__before_clear_bit();
198 clear_bit(PG_BUSY, &req->wb_flags);
199 smp_mb__after_clear_bit();
Trond Myklebust464a98b2005-06-22 17:16:21 +0000200 wake_up_bit(&req->wb_flags, PG_BUSY);
Trond Myklebust3aff4eb2012-05-09 14:30:35 -0400201}
202
203/**
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400204 * nfs_unlock_and_release_request - Unlock request and release the nfs_page
205 * @req:
Trond Myklebust3aff4eb2012-05-09 14:30:35 -0400206 */
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400207void nfs_unlock_and_release_request(struct nfs_page *req)
Trond Myklebust3aff4eb2012-05-09 14:30:35 -0400208{
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400209 nfs_unlock_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 nfs_release_request(req);
211}
212
Trond Myklebust4d65c522011-03-25 14:15:11 -0400213/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 * nfs_clear_request - Free up all resources allocated to the request
215 * @req:
216 *
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500217 * Release page and open context resources associated with a read/write
218 * request after it has completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 */
Trond Myklebust4d65c522011-03-25 14:15:11 -0400220static void nfs_clear_request(struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500222 struct page *page = req->wb_page;
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500223 struct nfs_open_context *ctx = req->wb_context;
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400224 struct nfs_lock_context *l_ctx = req->wb_lock_context;
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500225
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500226 if (page != NULL) {
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500227 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 req->wb_page = NULL;
229 }
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400230 if (l_ctx != NULL) {
Trond Myklebust577b4232013-04-08 21:38:12 -0400231 nfs_iocounter_dec(&l_ctx->io_count);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400232 nfs_put_lock_context(l_ctx);
233 req->wb_lock_context = NULL;
234 }
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500235 if (ctx != NULL) {
236 put_nfs_open_context(ctx);
237 req->wb_context = NULL;
238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
241
242/**
243 * nfs_release_request - Release the count on an NFS read/write request
244 * @req: request to release
245 *
246 * Note: Should never be called with the spinlock held!
247 */
Trond Myklebustc03b4022007-06-17 13:26:38 -0400248static void nfs_free_request(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Trond Myklebustc03b4022007-06-17 13:26:38 -0400250 struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500252 /* Release struct file and open context */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 nfs_clear_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 nfs_page_free(req);
255}
256
Trond Myklebustc03b4022007-06-17 13:26:38 -0400257void nfs_release_request(struct nfs_page *req)
258{
259 kref_put(&req->wb_kref, nfs_free_request);
260}
261
Trond Myklebust9f557cd2010-02-03 08:27:22 -0500262static int nfs_wait_bit_uninterruptible(void *word)
263{
264 io_schedule();
265 return 0;
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268/**
269 * nfs_wait_on_request - Wait for a request to complete.
270 * @req: request to wait upon.
271 *
Matthew Wilcox150030b2007-12-06 16:24:39 -0500272 * Interruptible by fatal signals only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * The user is responsible for holding a count on the request.
274 */
275int
276nfs_wait_on_request(struct nfs_page *req)
277{
Trond Myklebust9f557cd2010-02-03 08:27:22 -0500278 return wait_on_bit(&req->wb_flags, PG_BUSY,
279 nfs_wait_bit_uninterruptible,
280 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400283/*
284 * nfs_generic_pg_test - determine if requests can be coalesced
285 * @desc: pointer to descriptor
286 * @prev: previous request in desc, or NULL
287 * @req: this request
288 *
289 * Returns zero if @req can be coalesced into @desc, otherwise it returns
290 * the size of the request.
291 */
292size_t nfs_generic_pg_test(struct nfs_pageio_descriptor *desc,
293 struct nfs_page *prev, struct nfs_page *req)
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300294{
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 if (desc->pg_bsize < PAGE_SIZE)
303 return 0;
304
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400305 if (desc->pg_count + req->wb_bytes <= desc->pg_bsize)
306 return req->wb_bytes;
307 return 0;
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300308}
Benny Halevy19345cb2011-06-19 18:33:46 -0400309EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300310
Anna Schumaker00bfa302014-05-06 09:12:29 -0400311static inline struct nfs_rw_header *NFS_RW_HEADER(struct nfs_pgio_header *hdr)
312{
313 return container_of(hdr, struct nfs_rw_header, header);
314}
315
316/**
Anna Schumaker4a0de552014-05-06 09:12:30 -0400317 * nfs_rw_header_alloc - Allocate a header for a read or write
318 * @ops: Read or write function vector
319 */
320struct nfs_rw_header *nfs_rw_header_alloc(const struct nfs_rw_ops *ops)
321{
322 struct nfs_rw_header *header = ops->rw_alloc_header();
323
324 if (header) {
325 struct nfs_pgio_header *hdr = &header->header;
326
327 INIT_LIST_HEAD(&hdr->pages);
328 INIT_LIST_HEAD(&hdr->rpc_list);
329 spin_lock_init(&hdr->lock);
330 atomic_set(&hdr->refcnt, 0);
331 hdr->rw_ops = ops;
332 }
333 return header;
334}
335EXPORT_SYMBOL_GPL(nfs_rw_header_alloc);
336
337/*
338 * nfs_rw_header_free - Free a read or write header
339 * @hdr: The header to free
340 */
341void nfs_rw_header_free(struct nfs_pgio_header *hdr)
342{
343 hdr->rw_ops->rw_free_header(NFS_RW_HEADER(hdr));
344}
345EXPORT_SYMBOL_GPL(nfs_rw_header_free);
346
347/**
Anna Schumaker00bfa302014-05-06 09:12:29 -0400348 * nfs_pgio_data_alloc - Allocate pageio data
349 * @hdr: The header making a request
350 * @pagecount: Number of pages to create
351 */
Anna Schumakeref2c4882014-05-06 09:12:36 -0400352static struct nfs_pgio_data *nfs_pgio_data_alloc(struct nfs_pgio_header *hdr,
353 unsigned int pagecount)
Anna Schumaker00bfa302014-05-06 09:12:29 -0400354{
355 struct nfs_pgio_data *data, *prealloc;
356
357 prealloc = &NFS_RW_HEADER(hdr)->rpc_data;
358 if (prealloc->header == NULL)
359 data = prealloc;
360 else
361 data = kzalloc(sizeof(*data), GFP_KERNEL);
362 if (!data)
363 goto out;
364
365 if (nfs_pgarray_set(&data->pages, pagecount)) {
366 data->header = hdr;
367 atomic_inc(&hdr->refcnt);
368 } else {
369 if (data != prealloc)
370 kfree(data);
371 data = NULL;
372 }
373out:
374 return data;
375}
376
377/**
378 * nfs_pgio_data_release - Properly free pageio data
379 * @data: The data to release
380 */
381void nfs_pgio_data_release(struct nfs_pgio_data *data)
382{
383 struct nfs_pgio_header *hdr = data->header;
384 struct nfs_rw_header *pageio_header = NFS_RW_HEADER(hdr);
385
386 put_nfs_open_context(data->args.context);
387 if (data->pages.pagevec != data->pages.page_array)
388 kfree(data->pages.pagevec);
389 if (data == &pageio_header->rpc_data) {
390 data->header = NULL;
391 data = NULL;
392 }
393 if (atomic_dec_and_test(&hdr->refcnt))
394 hdr->completion_ops->completion(hdr);
395 /* Note: we only free the rpc_task after callbacks are done.
396 * See the comment in rpc_free_task() for why
397 */
398 kfree(data);
399}
400EXPORT_SYMBOL_GPL(nfs_pgio_data_release);
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402/**
Anna Schumakerce595152014-05-06 09:12:34 -0400403 * nfs_pgio_rpcsetup - Set up arguments for a pageio call
404 * @data: The pageio data
405 * @count: Number of bytes to read
406 * @offset: Initial offset
407 * @how: How to commit data (writes only)
408 * @cinfo: Commit information for the call (writes only)
409 */
Anna Schumakeref2c4882014-05-06 09:12:36 -0400410static void nfs_pgio_rpcsetup(struct nfs_pgio_data *data,
Anna Schumakerce595152014-05-06 09:12:34 -0400411 unsigned int count, unsigned int offset,
412 int how, struct nfs_commit_info *cinfo)
413{
414 struct nfs_page *req = data->header->req;
415
416 /* Set up the RPC argument and reply structs
417 * NB: take care not to mess about with data->commit et al. */
418
419 data->args.fh = NFS_FH(data->header->inode);
420 data->args.offset = req_offset(req) + offset;
421 /* pnfs_set_layoutcommit needs this */
422 data->mds_offset = data->args.offset;
423 data->args.pgbase = req->wb_pgbase + offset;
424 data->args.pages = data->pages.pagevec;
425 data->args.count = count;
426 data->args.context = get_nfs_open_context(req->wb_context);
427 data->args.lock_context = req->wb_lock_context;
428 data->args.stable = NFS_UNSTABLE;
429 switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
430 case 0:
431 break;
432 case FLUSH_COND_STABLE:
433 if (nfs_reqs_to_commit(cinfo))
434 break;
435 default:
436 data->args.stable = NFS_FILE_SYNC;
437 }
438
439 data->res.fattr = &data->fattr;
440 data->res.count = count;
441 data->res.eof = 0;
442 data->res.verf = &data->verf;
443 nfs_fattr_init(&data->fattr);
444}
445
446/**
Anna Schumakera4cdda52014-05-06 09:12:31 -0400447 * nfs_pgio_prepare - Prepare pageio data to go over the wire
448 * @task: The current task
449 * @calldata: pageio data to prepare
450 */
Anna Schumaker6f92fa42014-05-06 09:12:33 -0400451static void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
Anna Schumakera4cdda52014-05-06 09:12:31 -0400452{
453 struct nfs_pgio_data *data = calldata;
454 int err;
455 err = NFS_PROTO(data->header->inode)->pgio_rpc_prepare(task, data);
456 if (err)
457 rpc_exit(task, err);
458}
459
Anna Schumaker1ed26f32014-05-06 09:12:37 -0400460int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_data *data,
461 const struct rpc_call_ops *call_ops, int how, int flags)
462{
463 struct rpc_task *task;
464 struct rpc_message msg = {
465 .rpc_argp = &data->args,
466 .rpc_resp = &data->res,
467 .rpc_cred = data->header->cred,
468 };
469 struct rpc_task_setup task_setup_data = {
470 .rpc_client = clnt,
471 .task = &data->task,
472 .rpc_message = &msg,
473 .callback_ops = call_ops,
474 .callback_data = data,
475 .workqueue = nfsiod_workqueue,
476 .flags = RPC_TASK_ASYNC | flags,
477 };
478 int ret = 0;
479
480 data->header->rw_ops->rw_initiate(data, &msg, &task_setup_data, how);
481
482 dprintk("NFS: %5u initiated pgio call "
483 "(req %s/%llu, %u bytes @ offset %llu)\n",
484 data->task.tk_pid,
485 data->header->inode->i_sb->s_id,
486 (unsigned long long)NFS_FILEID(data->header->inode),
487 data->args.count,
488 (unsigned long long)data->args.offset);
489
490 task = rpc_run_task(&task_setup_data);
491 if (IS_ERR(task)) {
492 ret = PTR_ERR(task);
493 goto out;
494 }
495 if (how & FLUSH_SYNC) {
496 ret = rpc_wait_for_completion_task(task);
497 if (ret == 0)
498 ret = task->tk_status;
499 }
500 rpc_put_task(task);
501out:
502 return ret;
503}
504EXPORT_SYMBOL_GPL(nfs_initiate_pgio);
505
Anna Schumakercf485fc2014-05-06 09:12:39 -0400506static int nfs_do_multiple_pgios(struct list_head *head,
Anna Schumakerc3766272014-05-06 09:12:38 -0400507 const struct rpc_call_ops *call_ops,
508 int how)
509{
510 struct nfs_pgio_data *data;
511 int ret = 0;
512
513 while (!list_empty(head)) {
514 int ret2;
515
516 data = list_first_entry(head, struct nfs_pgio_data, list);
517 list_del_init(&data->list);
518
519 ret2 = nfs_initiate_pgio(NFS_CLIENT(data->header->inode),
520 data, call_ops, how, 0);
521 if (ret == 0)
522 ret = ret2;
523 }
524 return ret;
525}
526
Anna Schumakera4cdda52014-05-06 09:12:31 -0400527/**
Anna Schumaker844c9e62014-05-06 09:12:35 -0400528 * nfs_pgio_error - Clean up from a pageio error
529 * @desc: IO descriptor
530 * @hdr: pageio header
531 */
Anna Schumakeref2c4882014-05-06 09:12:36 -0400532static int nfs_pgio_error(struct nfs_pageio_descriptor *desc,
Anna Schumaker844c9e62014-05-06 09:12:35 -0400533 struct nfs_pgio_header *hdr)
534{
535 struct nfs_pgio_data *data;
536
537 set_bit(NFS_IOHDR_REDO, &hdr->flags);
538 while (!list_empty(&hdr->rpc_list)) {
539 data = list_first_entry(&hdr->rpc_list, struct nfs_pgio_data, list);
540 list_del(&data->list);
541 nfs_pgio_data_release(data);
542 }
543 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
544 return -ENOMEM;
545}
546
547/**
Anna Schumakera4cdda52014-05-06 09:12:31 -0400548 * nfs_pgio_release - Release pageio data
549 * @calldata: The pageio data to release
550 */
Anna Schumaker6f92fa42014-05-06 09:12:33 -0400551static void nfs_pgio_release(void *calldata)
Anna Schumakera4cdda52014-05-06 09:12:31 -0400552{
553 struct nfs_pgio_data *data = calldata;
554 if (data->header->rw_ops->rw_release)
555 data->header->rw_ops->rw_release(data);
556 nfs_pgio_data_release(data);
557}
558
559/**
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400560 * nfs_pageio_init - initialise a page io descriptor
561 * @desc: pointer to descriptor
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400562 * @inode: pointer to inode
563 * @doio: pointer to io function
564 * @bsize: io block size
565 * @io_flags: extra parameters for the io function
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400566 */
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400567void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
568 struct inode *inode,
Trond Myklebust1751c362011-06-10 13:30:23 -0400569 const struct nfs_pageio_ops *pg_ops,
Fred Isaman061ae2e2012-04-20 14:47:48 -0400570 const struct nfs_pgio_completion_ops *compl_ops,
Anna Schumaker4a0de552014-05-06 09:12:30 -0400571 const struct nfs_rw_ops *rw_ops,
Trond Myklebust84dde762007-05-04 14:44:06 -0400572 size_t bsize,
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400573 int io_flags)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400574{
575 INIT_LIST_HEAD(&desc->pg_list);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400576 desc->pg_bytes_written = 0;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400577 desc->pg_count = 0;
578 desc->pg_bsize = bsize;
579 desc->pg_base = 0;
Trond Myklebustb31268a2011-03-21 17:02:00 -0400580 desc->pg_moreio = 0;
Trond Myklebustd9156f92011-07-12 13:42:02 -0400581 desc->pg_recoalesce = 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400582 desc->pg_inode = inode;
Trond Myklebust1751c362011-06-10 13:30:23 -0400583 desc->pg_ops = pg_ops;
Fred Isaman061ae2e2012-04-20 14:47:48 -0400584 desc->pg_completion_ops = compl_ops;
Anna Schumaker4a0de552014-05-06 09:12:30 -0400585 desc->pg_rw_ops = rw_ops;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400586 desc->pg_ioflags = io_flags;
587 desc->pg_error = 0;
Fred Isaman94ad1c82011-03-01 01:34:14 +0000588 desc->pg_lseg = NULL;
Fred Isaman584aa812012-04-20 14:47:51 -0400589 desc->pg_dreq = NULL;
Peng Taof6166382012-08-02 15:36:09 +0300590 desc->pg_layout_private = NULL;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400591}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400592EXPORT_SYMBOL_GPL(nfs_pageio_init);
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400593
Anna Schumaker0eecb212014-05-06 09:12:32 -0400594/**
595 * nfs_pgio_result - Basic pageio error handling
596 * @task: The task that ran
597 * @calldata: Pageio data to check
598 */
Anna Schumaker6f92fa42014-05-06 09:12:33 -0400599static void nfs_pgio_result(struct rpc_task *task, void *calldata)
Anna Schumaker0eecb212014-05-06 09:12:32 -0400600{
601 struct nfs_pgio_data *data = calldata;
602 struct inode *inode = data->header->inode;
603
604 dprintk("NFS: %s: %5u, (status %d)\n", __func__,
605 task->tk_pid, task->tk_status);
606
607 if (data->header->rw_ops->rw_done(task, data, inode) != 0)
608 return;
609 if (task->tk_status < 0)
610 nfs_set_pgio_error(data->header, task->tk_status, data->args.offset);
611 else
612 data->header->rw_ops->rw_result(task, data);
613}
614
Anna Schumakeref2c4882014-05-06 09:12:36 -0400615/*
616 * Generate multiple small requests to read or write a single
617 * contiguous dirty on one page.
618 */
619static int nfs_pgio_multi(struct nfs_pageio_descriptor *desc,
620 struct nfs_pgio_header *hdr)
621{
622 struct nfs_page *req = hdr->req;
623 struct page *page = req->wb_page;
624 struct nfs_pgio_data *data;
625 size_t wsize = desc->pg_bsize, nbytes;
626 unsigned int offset;
627 int requests = 0;
628 struct nfs_commit_info cinfo;
629
630 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
631
632 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
633 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo) ||
634 desc->pg_count > wsize))
635 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
636
637 offset = 0;
638 nbytes = desc->pg_count;
639 do {
640 size_t len = min(nbytes, wsize);
641
642 data = nfs_pgio_data_alloc(hdr, 1);
643 if (!data)
644 return nfs_pgio_error(desc, hdr);
645 data->pages.pagevec[0] = page;
646 nfs_pgio_rpcsetup(data, len, offset, desc->pg_ioflags, &cinfo);
647 list_add(&data->list, &hdr->rpc_list);
648 requests++;
649 nbytes -= len;
650 offset += len;
651 } while (nbytes != 0);
652
653 nfs_list_remove_request(req);
654 nfs_list_add_request(req, &hdr->pages);
655 desc->pg_rpc_callops = &nfs_pgio_common_ops;
656 return 0;
657}
658
659/*
660 * Create an RPC task for the given read or write request and kick it.
661 * The page must have been locked by the caller.
662 *
663 * It may happen that the page we're passed is not marked dirty.
664 * This is the case if nfs_updatepage detects a conflicting request
665 * that has been written but not committed.
666 */
667static int nfs_pgio_one(struct nfs_pageio_descriptor *desc,
668 struct nfs_pgio_header *hdr)
669{
670 struct nfs_page *req;
671 struct page **pages;
672 struct nfs_pgio_data *data;
673 struct list_head *head = &desc->pg_list;
674 struct nfs_commit_info cinfo;
675
676 data = nfs_pgio_data_alloc(hdr, nfs_page_array_len(desc->pg_base,
677 desc->pg_count));
678 if (!data)
679 return nfs_pgio_error(desc, hdr);
680
681 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
682 pages = data->pages.pagevec;
683 while (!list_empty(head)) {
684 req = nfs_list_entry(head->next);
685 nfs_list_remove_request(req);
686 nfs_list_add_request(req, &hdr->pages);
687 *pages++ = req->wb_page;
688 }
689
690 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
691 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
692 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
693
694 /* Set up the argument struct */
695 nfs_pgio_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo);
696 list_add(&data->list, &hdr->rpc_list);
697 desc->pg_rpc_callops = &nfs_pgio_common_ops;
698 return 0;
699}
700
Anna Schumaker41d8d5b2014-05-06 09:12:40 -0400701static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
Anna Schumakercf485fc2014-05-06 09:12:39 -0400702{
703 struct nfs_rw_header *rw_hdr;
704 struct nfs_pgio_header *hdr;
705 int ret;
706
707 rw_hdr = nfs_rw_header_alloc(desc->pg_rw_ops);
708 if (!rw_hdr) {
709 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
710 return -ENOMEM;
711 }
712 hdr = &rw_hdr->header;
713 nfs_pgheader_init(desc, hdr, nfs_rw_header_free);
714 atomic_inc(&hdr->refcnt);
715 ret = nfs_generic_pgio(desc, hdr);
716 if (ret == 0)
717 ret = nfs_do_multiple_pgios(&hdr->rpc_list,
718 desc->pg_rpc_callops,
719 desc->pg_ioflags);
720 if (atomic_dec_and_test(&hdr->refcnt))
721 hdr->completion_ops->completion(hdr);
722 return ret;
723}
724
Anna Schumakeref2c4882014-05-06 09:12:36 -0400725int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
726 struct nfs_pgio_header *hdr)
727{
728 if (desc->pg_bsize < PAGE_CACHE_SIZE)
729 return nfs_pgio_multi(desc, hdr);
730 return nfs_pgio_one(desc, hdr);
731}
732EXPORT_SYMBOL_GPL(nfs_generic_pgio);
733
Trond Myklebust4109bb72013-09-06 11:09:38 -0400734static bool nfs_match_open_context(const struct nfs_open_context *ctx1,
735 const struct nfs_open_context *ctx2)
736{
737 return ctx1->cred == ctx2->cred && ctx1->state == ctx2->state;
738}
739
740static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
741 const struct nfs_lock_context *l2)
742{
743 return l1->lockowner.l_owner == l2->lockowner.l_owner
744 && l1->lockowner.l_pid == l2->lockowner.l_pid;
745}
746
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400747/**
748 * nfs_can_coalesce_requests - test two requests for compatibility
749 * @prev: pointer to nfs_page
750 * @req: pointer to nfs_page
751 *
752 * The nfs_page structures 'prev' and 'req' are compared to ensure that the
753 * page data area they describe is contiguous, and that their RPC
754 * credentials, NFSv4 open state, and lockowners are the same.
755 *
756 * Return 'true' if this is the case, else return 'false'.
757 */
Benny Halevy18ad0a92011-05-25 21:03:56 +0300758static bool nfs_can_coalesce_requests(struct nfs_page *prev,
759 struct nfs_page *req,
760 struct nfs_pageio_descriptor *pgio)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400761{
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400762 size_t size;
763
Trond Myklebust4109bb72013-09-06 11:09:38 -0400764 if (!nfs_match_open_context(req->wb_context, prev->wb_context))
Benny Halevy18ad0a92011-05-25 21:03:56 +0300765 return false;
Trond Myklebust4109bb72013-09-06 11:09:38 -0400766 if (req->wb_context->dentry->d_inode->i_flock != NULL &&
767 !nfs_match_lock_context(req->wb_lock_context, prev->wb_lock_context))
Benny Halevy18ad0a92011-05-25 21:03:56 +0300768 return false;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400769 if (req->wb_pgbase != 0)
Benny Halevy18ad0a92011-05-25 21:03:56 +0300770 return false;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400771 if (prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
Benny Halevy18ad0a92011-05-25 21:03:56 +0300772 return false;
Fred Isaman1825a0d2012-04-20 19:55:31 -0400773 if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
774 return false;
Weston Andros Adamsonb4fdac12014-05-15 11:56:43 -0400775 size = pgio->pg_ops->pg_test(pgio, prev, req);
776 WARN_ON_ONCE(size && size != req->wb_bytes);
777 return size > 0;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400778}
779
780/**
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400781 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400782 * @desc: destination io descriptor
783 * @req: request
784 *
785 * Returns true if the request 'req' was successfully coalesced into the
786 * existing list of pages 'desc'.
787 */
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400788static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
789 struct nfs_page *req)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400790{
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400791 if (desc->pg_count != 0) {
792 struct nfs_page *prev;
793
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400794 prev = nfs_list_entry(desc->pg_list.prev);
Fred Isaman94ad1c82011-03-01 01:34:14 +0000795 if (!nfs_can_coalesce_requests(prev, req, desc))
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400796 return 0;
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300797 } else {
Trond Myklebustd8007d42011-06-10 13:30:23 -0400798 if (desc->pg_ops->pg_init)
799 desc->pg_ops->pg_init(desc, req);
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400800 desc->pg_base = req->wb_pgbase;
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300801 }
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400802 nfs_list_remove_request(req);
803 nfs_list_add_request(req, &desc->pg_list);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300804 desc->pg_count += req->wb_bytes;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400805 return 1;
806}
807
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400808/*
809 * Helper for nfs_pageio_add_request and nfs_pageio_complete
810 */
811static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
812{
813 if (!list_empty(&desc->pg_list)) {
Trond Myklebust1751c362011-06-10 13:30:23 -0400814 int error = desc->pg_ops->pg_doio(desc);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400815 if (error < 0)
816 desc->pg_error = error;
817 else
818 desc->pg_bytes_written += desc->pg_count;
819 }
820 if (list_empty(&desc->pg_list)) {
821 desc->pg_count = 0;
822 desc->pg_base = 0;
823 }
824}
825
826/**
827 * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
828 * @desc: destination io descriptor
829 * @req: request
830 *
831 * Returns true if the request 'req' was successfully coalesced into the
832 * existing list of pages 'desc'.
833 */
Trond Myklebustd9156f92011-07-12 13:42:02 -0400834static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400835 struct nfs_page *req)
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400836{
837 while (!nfs_pageio_do_add_request(desc, req)) {
Trond Myklebustb31268a2011-03-21 17:02:00 -0400838 desc->pg_moreio = 1;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400839 nfs_pageio_doio(desc);
840 if (desc->pg_error < 0)
841 return 0;
Trond Myklebustb31268a2011-03-21 17:02:00 -0400842 desc->pg_moreio = 0;
Trond Myklebustd9156f92011-07-12 13:42:02 -0400843 if (desc->pg_recoalesce)
844 return 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400845 }
846 return 1;
847}
848
Trond Myklebustd9156f92011-07-12 13:42:02 -0400849static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
850{
851 LIST_HEAD(head);
852
853 do {
854 list_splice_init(&desc->pg_list, &head);
855 desc->pg_bytes_written -= desc->pg_count;
856 desc->pg_count = 0;
857 desc->pg_base = 0;
858 desc->pg_recoalesce = 0;
859
860 while (!list_empty(&head)) {
861 struct nfs_page *req;
862
863 req = list_first_entry(&head, struct nfs_page, wb_list);
864 nfs_list_remove_request(req);
865 if (__nfs_pageio_add_request(desc, req))
866 continue;
867 if (desc->pg_error < 0)
868 return 0;
869 break;
870 }
871 } while (desc->pg_recoalesce);
872 return 1;
873}
874
875int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
876 struct nfs_page *req)
877{
878 int ret;
879
880 do {
881 ret = __nfs_pageio_add_request(desc, req);
882 if (ret)
883 break;
884 if (desc->pg_error < 0)
885 break;
886 ret = nfs_do_recoalesce(desc);
887 } while (ret);
888 return ret;
889}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400890EXPORT_SYMBOL_GPL(nfs_pageio_add_request);
Trond Myklebustd9156f92011-07-12 13:42:02 -0400891
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400892/**
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400893 * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
894 * @desc: pointer to io descriptor
895 */
896void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
897{
Trond Myklebustd9156f92011-07-12 13:42:02 -0400898 for (;;) {
899 nfs_pageio_doio(desc);
900 if (!desc->pg_recoalesce)
901 break;
902 if (!nfs_do_recoalesce(desc))
903 break;
904 }
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400905}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400906EXPORT_SYMBOL_GPL(nfs_pageio_complete);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400907
Trond Myklebust7fe7f842007-05-20 10:18:27 -0400908/**
909 * nfs_pageio_cond_complete - Conditional I/O completion
910 * @desc: pointer to io descriptor
911 * @index: page index
912 *
913 * It is important to ensure that processes don't try to take locks
914 * on non-contiguous ranges of pages as that might deadlock. This
915 * function should be called before attempting to wait on a locked
916 * nfs_page. It will complete the I/O if the page index 'index'
917 * is not contiguous with the existing list of pages in 'desc'.
918 */
919void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
920{
921 if (!list_empty(&desc->pg_list)) {
922 struct nfs_page *prev = nfs_list_entry(desc->pg_list.prev);
923 if (index != prev->wb_index + 1)
Trond Myklebustd9156f92011-07-12 13:42:02 -0400924 nfs_pageio_complete(desc);
Trond Myklebust7fe7f842007-05-20 10:18:27 -0400925 }
926}
927
David Howellsf7b422b2006-06-09 09:34:33 -0400928int __init nfs_init_nfspagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
930 nfs_page_cachep = kmem_cache_create("nfs_page",
931 sizeof(struct nfs_page),
932 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +0900933 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (nfs_page_cachep == NULL)
935 return -ENOMEM;
936
937 return 0;
938}
939
David Brownell266bee82006-06-27 12:59:15 -0700940void nfs_destroy_nfspagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700942 kmem_cache_destroy(nfs_page_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
Anna Schumakeref2c4882014-05-06 09:12:36 -0400945static const struct rpc_call_ops nfs_pgio_common_ops = {
Anna Schumaker6f92fa42014-05-06 09:12:33 -0400946 .rpc_call_prepare = nfs_pgio_prepare,
947 .rpc_call_done = nfs_pgio_result,
948 .rpc_release = nfs_pgio_release,
949};
Anna Schumaker41d8d5b2014-05-06 09:12:40 -0400950
951const struct nfs_pageio_ops nfs_pgio_rw_ops = {
952 .pg_test = nfs_generic_pg_test,
953 .pg_doio = nfs_generic_pg_pgios,
954};