blob: ab5b1850ca4fcbbd916ac79d294d636a0c49e2c0 [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 * @inode: inode to which the request is attached
143 * @page: page to write
144 * @offset: starting offset within the page for the write
145 * @count: number of bytes to read/write
146 *
147 * The page must be locked by the caller. This makes sure we never
Jason Uhlenkotta19b89c2007-04-26 17:25:51 -0700148 * create two different requests for the same page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 * User should ensure it is safe to sleep in this function.
150 */
151struct nfs_page *
152nfs_create_request(struct nfs_open_context *ctx, struct inode *inode,
153 struct page *page,
154 unsigned int offset, unsigned int count)
155{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 struct nfs_page *req;
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400157 struct nfs_lock_context *l_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Trond Myklebustc58c8442013-03-18 19:45:14 -0400159 if (test_bit(NFS_CONTEXT_BAD, &ctx->flags))
160 return ERR_PTR(-EBADF);
Trond Myklebust18eb8842010-05-13 12:51:02 -0400161 /* try to allocate the request struct */
162 req = nfs_page_alloc();
163 if (req == NULL)
164 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Jeff Layton015f0212010-10-28 10:10:37 -0400166 /* get lock context early so we can deal with alloc failures */
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400167 l_ctx = nfs_get_lock_context(ctx);
168 if (IS_ERR(l_ctx)) {
Jeff Layton015f0212010-10-28 10:10:37 -0400169 nfs_page_free(req);
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400170 return ERR_CAST(l_ctx);
Jeff Layton015f0212010-10-28 10:10:37 -0400171 }
Trond Myklebustb3c54de2012-08-13 17:15:50 -0400172 req->wb_lock_context = l_ctx;
Trond Myklebust577b4232013-04-08 21:38:12 -0400173 nfs_iocounter_inc(&l_ctx->io_count);
Jeff Layton015f0212010-10-28 10:10:37 -0400174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 /* Initialize the request struct. Initially, we assume a
176 * long write-back delay. This will be adjusted in
177 * update_nfs_request below if the region is not locked. */
178 req->wb_page = page;
Mel Gormand56b4dd2012-07-31 16:45:06 -0700179 req->wb_index = page_file_index(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 page_cache_get(page);
181 req->wb_offset = offset;
182 req->wb_pgbase = offset;
183 req->wb_bytes = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 req->wb_context = get_nfs_open_context(ctx);
Trond Myklebustc03b4022007-06-17 13:26:38 -0400185 kref_init(&req->wb_kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return req;
187}
188
189/**
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400190 * nfs_unlock_request - Unlock request and wake up sleepers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * @req:
192 */
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400193void nfs_unlock_request(struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 if (!NFS_WBACK_BUSY(req)) {
196 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
197 BUG();
198 }
199 smp_mb__before_clear_bit();
200 clear_bit(PG_BUSY, &req->wb_flags);
201 smp_mb__after_clear_bit();
Trond Myklebust464a98b2005-06-22 17:16:21 +0000202 wake_up_bit(&req->wb_flags, PG_BUSY);
Trond Myklebust3aff4eb2012-05-09 14:30:35 -0400203}
204
205/**
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400206 * nfs_unlock_and_release_request - Unlock request and release the nfs_page
207 * @req:
Trond Myklebust3aff4eb2012-05-09 14:30:35 -0400208 */
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400209void nfs_unlock_and_release_request(struct nfs_page *req)
Trond Myklebust3aff4eb2012-05-09 14:30:35 -0400210{
Trond Myklebust1d1afcb2012-05-09 14:04:55 -0400211 nfs_unlock_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 nfs_release_request(req);
213}
214
Trond Myklebust4d65c522011-03-25 14:15:11 -0400215/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * nfs_clear_request - Free up all resources allocated to the request
217 * @req:
218 *
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500219 * Release page and open context resources associated with a read/write
220 * request after it has completed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 */
Trond Myklebust4d65c522011-03-25 14:15:11 -0400222static void nfs_clear_request(struct nfs_page *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500224 struct page *page = req->wb_page;
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500225 struct nfs_open_context *ctx = req->wb_context;
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400226 struct nfs_lock_context *l_ctx = req->wb_lock_context;
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500227
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500228 if (page != NULL) {
Trond Myklebustcd52ed32006-03-20 13:44:04 -0500229 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 req->wb_page = NULL;
231 }
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400232 if (l_ctx != NULL) {
Trond Myklebust577b4232013-04-08 21:38:12 -0400233 nfs_iocounter_dec(&l_ctx->io_count);
Trond Myklebustf11ac8d2010-06-25 16:35:53 -0400234 nfs_put_lock_context(l_ctx);
235 req->wb_lock_context = NULL;
236 }
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500237 if (ctx != NULL) {
238 put_nfs_open_context(ctx);
239 req->wb_context = NULL;
240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243
244/**
245 * nfs_release_request - Release the count on an NFS read/write request
246 * @req: request to release
247 *
248 * Note: Should never be called with the spinlock held!
249 */
Trond Myklebustc03b4022007-06-17 13:26:38 -0400250static void nfs_free_request(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Trond Myklebustc03b4022007-06-17 13:26:38 -0400252 struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Trond Myklebustbb6fbc42010-03-11 09:19:35 -0500254 /* Release struct file and open context */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 nfs_clear_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 nfs_page_free(req);
257}
258
Trond Myklebustc03b4022007-06-17 13:26:38 -0400259void nfs_release_request(struct nfs_page *req)
260{
261 kref_put(&req->wb_kref, nfs_free_request);
262}
263
Trond Myklebust9f557cd2010-02-03 08:27:22 -0500264static int nfs_wait_bit_uninterruptible(void *word)
265{
266 io_schedule();
267 return 0;
268}
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270/**
271 * nfs_wait_on_request - Wait for a request to complete.
272 * @req: request to wait upon.
273 *
Matthew Wilcox150030b2007-12-06 16:24:39 -0500274 * Interruptible by fatal signals only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 * The user is responsible for holding a count on the request.
276 */
277int
278nfs_wait_on_request(struct nfs_page *req)
279{
Trond Myklebust9f557cd2010-02-03 08:27:22 -0500280 return wait_on_bit(&req->wb_flags, PG_BUSY,
281 nfs_wait_bit_uninterruptible,
282 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Benny Halevy19345cb2011-06-19 18:33:46 -0400285bool nfs_generic_pg_test(struct nfs_pageio_descriptor *desc, struct nfs_page *prev, struct nfs_page *req)
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300286{
287 /*
288 * FIXME: ideally we should be able to coalesce all requests
289 * that are not block boundary aligned, but currently this
290 * is problematic for the case of bsize < PAGE_CACHE_SIZE,
291 * since nfs_flush_multi and nfs_pagein_multi assume you
292 * can have only one struct nfs_page.
293 */
294 if (desc->pg_bsize < PAGE_SIZE)
295 return 0;
296
297 return desc->pg_count + req->wb_bytes <= desc->pg_bsize;
298}
Benny Halevy19345cb2011-06-19 18:33:46 -0400299EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300300
Anna Schumaker00bfa302014-05-06 09:12:29 -0400301static inline struct nfs_rw_header *NFS_RW_HEADER(struct nfs_pgio_header *hdr)
302{
303 return container_of(hdr, struct nfs_rw_header, header);
304}
305
306/**
Anna Schumaker4a0de552014-05-06 09:12:30 -0400307 * nfs_rw_header_alloc - Allocate a header for a read or write
308 * @ops: Read or write function vector
309 */
310struct nfs_rw_header *nfs_rw_header_alloc(const struct nfs_rw_ops *ops)
311{
312 struct nfs_rw_header *header = ops->rw_alloc_header();
313
314 if (header) {
315 struct nfs_pgio_header *hdr = &header->header;
316
317 INIT_LIST_HEAD(&hdr->pages);
318 INIT_LIST_HEAD(&hdr->rpc_list);
319 spin_lock_init(&hdr->lock);
320 atomic_set(&hdr->refcnt, 0);
321 hdr->rw_ops = ops;
322 }
323 return header;
324}
325EXPORT_SYMBOL_GPL(nfs_rw_header_alloc);
326
327/*
328 * nfs_rw_header_free - Free a read or write header
329 * @hdr: The header to free
330 */
331void nfs_rw_header_free(struct nfs_pgio_header *hdr)
332{
333 hdr->rw_ops->rw_free_header(NFS_RW_HEADER(hdr));
334}
335EXPORT_SYMBOL_GPL(nfs_rw_header_free);
336
337/**
Anna Schumaker00bfa302014-05-06 09:12:29 -0400338 * nfs_pgio_data_alloc - Allocate pageio data
339 * @hdr: The header making a request
340 * @pagecount: Number of pages to create
341 */
Anna Schumakeref2c4882014-05-06 09:12:36 -0400342static struct nfs_pgio_data *nfs_pgio_data_alloc(struct nfs_pgio_header *hdr,
343 unsigned int pagecount)
Anna Schumaker00bfa302014-05-06 09:12:29 -0400344{
345 struct nfs_pgio_data *data, *prealloc;
346
347 prealloc = &NFS_RW_HEADER(hdr)->rpc_data;
348 if (prealloc->header == NULL)
349 data = prealloc;
350 else
351 data = kzalloc(sizeof(*data), GFP_KERNEL);
352 if (!data)
353 goto out;
354
355 if (nfs_pgarray_set(&data->pages, pagecount)) {
356 data->header = hdr;
357 atomic_inc(&hdr->refcnt);
358 } else {
359 if (data != prealloc)
360 kfree(data);
361 data = NULL;
362 }
363out:
364 return data;
365}
366
367/**
368 * nfs_pgio_data_release - Properly free pageio data
369 * @data: The data to release
370 */
371void nfs_pgio_data_release(struct nfs_pgio_data *data)
372{
373 struct nfs_pgio_header *hdr = data->header;
374 struct nfs_rw_header *pageio_header = NFS_RW_HEADER(hdr);
375
376 put_nfs_open_context(data->args.context);
377 if (data->pages.pagevec != data->pages.page_array)
378 kfree(data->pages.pagevec);
379 if (data == &pageio_header->rpc_data) {
380 data->header = NULL;
381 data = NULL;
382 }
383 if (atomic_dec_and_test(&hdr->refcnt))
384 hdr->completion_ops->completion(hdr);
385 /* Note: we only free the rpc_task after callbacks are done.
386 * See the comment in rpc_free_task() for why
387 */
388 kfree(data);
389}
390EXPORT_SYMBOL_GPL(nfs_pgio_data_release);
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392/**
Anna Schumakerce595152014-05-06 09:12:34 -0400393 * nfs_pgio_rpcsetup - Set up arguments for a pageio call
394 * @data: The pageio data
395 * @count: Number of bytes to read
396 * @offset: Initial offset
397 * @how: How to commit data (writes only)
398 * @cinfo: Commit information for the call (writes only)
399 */
Anna Schumakeref2c4882014-05-06 09:12:36 -0400400static void nfs_pgio_rpcsetup(struct nfs_pgio_data *data,
Anna Schumakerce595152014-05-06 09:12:34 -0400401 unsigned int count, unsigned int offset,
402 int how, struct nfs_commit_info *cinfo)
403{
404 struct nfs_page *req = data->header->req;
405
406 /* Set up the RPC argument and reply structs
407 * NB: take care not to mess about with data->commit et al. */
408
409 data->args.fh = NFS_FH(data->header->inode);
410 data->args.offset = req_offset(req) + offset;
411 /* pnfs_set_layoutcommit needs this */
412 data->mds_offset = data->args.offset;
413 data->args.pgbase = req->wb_pgbase + offset;
414 data->args.pages = data->pages.pagevec;
415 data->args.count = count;
416 data->args.context = get_nfs_open_context(req->wb_context);
417 data->args.lock_context = req->wb_lock_context;
418 data->args.stable = NFS_UNSTABLE;
419 switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
420 case 0:
421 break;
422 case FLUSH_COND_STABLE:
423 if (nfs_reqs_to_commit(cinfo))
424 break;
425 default:
426 data->args.stable = NFS_FILE_SYNC;
427 }
428
429 data->res.fattr = &data->fattr;
430 data->res.count = count;
431 data->res.eof = 0;
432 data->res.verf = &data->verf;
433 nfs_fattr_init(&data->fattr);
434}
435
436/**
Anna Schumakera4cdda52014-05-06 09:12:31 -0400437 * nfs_pgio_prepare - Prepare pageio data to go over the wire
438 * @task: The current task
439 * @calldata: pageio data to prepare
440 */
Anna Schumaker6f92fa42014-05-06 09:12:33 -0400441static void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
Anna Schumakera4cdda52014-05-06 09:12:31 -0400442{
443 struct nfs_pgio_data *data = calldata;
444 int err;
445 err = NFS_PROTO(data->header->inode)->pgio_rpc_prepare(task, data);
446 if (err)
447 rpc_exit(task, err);
448}
449
Anna Schumaker1ed26f32014-05-06 09:12:37 -0400450int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_data *data,
451 const struct rpc_call_ops *call_ops, int how, int flags)
452{
453 struct rpc_task *task;
454 struct rpc_message msg = {
455 .rpc_argp = &data->args,
456 .rpc_resp = &data->res,
457 .rpc_cred = data->header->cred,
458 };
459 struct rpc_task_setup task_setup_data = {
460 .rpc_client = clnt,
461 .task = &data->task,
462 .rpc_message = &msg,
463 .callback_ops = call_ops,
464 .callback_data = data,
465 .workqueue = nfsiod_workqueue,
466 .flags = RPC_TASK_ASYNC | flags,
467 };
468 int ret = 0;
469
470 data->header->rw_ops->rw_initiate(data, &msg, &task_setup_data, how);
471
472 dprintk("NFS: %5u initiated pgio call "
473 "(req %s/%llu, %u bytes @ offset %llu)\n",
474 data->task.tk_pid,
475 data->header->inode->i_sb->s_id,
476 (unsigned long long)NFS_FILEID(data->header->inode),
477 data->args.count,
478 (unsigned long long)data->args.offset);
479
480 task = rpc_run_task(&task_setup_data);
481 if (IS_ERR(task)) {
482 ret = PTR_ERR(task);
483 goto out;
484 }
485 if (how & FLUSH_SYNC) {
486 ret = rpc_wait_for_completion_task(task);
487 if (ret == 0)
488 ret = task->tk_status;
489 }
490 rpc_put_task(task);
491out:
492 return ret;
493}
494EXPORT_SYMBOL_GPL(nfs_initiate_pgio);
495
Anna Schumakera4cdda52014-05-06 09:12:31 -0400496/**
Anna Schumaker844c9e62014-05-06 09:12:35 -0400497 * nfs_pgio_error - Clean up from a pageio error
498 * @desc: IO descriptor
499 * @hdr: pageio header
500 */
Anna Schumakeref2c4882014-05-06 09:12:36 -0400501static int nfs_pgio_error(struct nfs_pageio_descriptor *desc,
Anna Schumaker844c9e62014-05-06 09:12:35 -0400502 struct nfs_pgio_header *hdr)
503{
504 struct nfs_pgio_data *data;
505
506 set_bit(NFS_IOHDR_REDO, &hdr->flags);
507 while (!list_empty(&hdr->rpc_list)) {
508 data = list_first_entry(&hdr->rpc_list, struct nfs_pgio_data, list);
509 list_del(&data->list);
510 nfs_pgio_data_release(data);
511 }
512 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
513 return -ENOMEM;
514}
515
516/**
Anna Schumakera4cdda52014-05-06 09:12:31 -0400517 * nfs_pgio_release - Release pageio data
518 * @calldata: The pageio data to release
519 */
Anna Schumaker6f92fa42014-05-06 09:12:33 -0400520static void nfs_pgio_release(void *calldata)
Anna Schumakera4cdda52014-05-06 09:12:31 -0400521{
522 struct nfs_pgio_data *data = calldata;
523 if (data->header->rw_ops->rw_release)
524 data->header->rw_ops->rw_release(data);
525 nfs_pgio_data_release(data);
526}
527
528/**
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400529 * nfs_pageio_init - initialise a page io descriptor
530 * @desc: pointer to descriptor
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400531 * @inode: pointer to inode
532 * @doio: pointer to io function
533 * @bsize: io block size
534 * @io_flags: extra parameters for the io function
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400535 */
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400536void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
537 struct inode *inode,
Trond Myklebust1751c362011-06-10 13:30:23 -0400538 const struct nfs_pageio_ops *pg_ops,
Fred Isaman061ae2e2012-04-20 14:47:48 -0400539 const struct nfs_pgio_completion_ops *compl_ops,
Anna Schumaker4a0de552014-05-06 09:12:30 -0400540 const struct nfs_rw_ops *rw_ops,
Trond Myklebust84dde762007-05-04 14:44:06 -0400541 size_t bsize,
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400542 int io_flags)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400543{
544 INIT_LIST_HEAD(&desc->pg_list);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400545 desc->pg_bytes_written = 0;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400546 desc->pg_count = 0;
547 desc->pg_bsize = bsize;
548 desc->pg_base = 0;
Trond Myklebustb31268a2011-03-21 17:02:00 -0400549 desc->pg_moreio = 0;
Trond Myklebustd9156f92011-07-12 13:42:02 -0400550 desc->pg_recoalesce = 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400551 desc->pg_inode = inode;
Trond Myklebust1751c362011-06-10 13:30:23 -0400552 desc->pg_ops = pg_ops;
Fred Isaman061ae2e2012-04-20 14:47:48 -0400553 desc->pg_completion_ops = compl_ops;
Anna Schumaker4a0de552014-05-06 09:12:30 -0400554 desc->pg_rw_ops = rw_ops;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400555 desc->pg_ioflags = io_flags;
556 desc->pg_error = 0;
Fred Isaman94ad1c82011-03-01 01:34:14 +0000557 desc->pg_lseg = NULL;
Fred Isaman584aa812012-04-20 14:47:51 -0400558 desc->pg_dreq = NULL;
Peng Taof6166382012-08-02 15:36:09 +0300559 desc->pg_layout_private = NULL;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400560}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400561EXPORT_SYMBOL_GPL(nfs_pageio_init);
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400562
Anna Schumaker0eecb212014-05-06 09:12:32 -0400563/**
564 * nfs_pgio_result - Basic pageio error handling
565 * @task: The task that ran
566 * @calldata: Pageio data to check
567 */
Anna Schumaker6f92fa42014-05-06 09:12:33 -0400568static void nfs_pgio_result(struct rpc_task *task, void *calldata)
Anna Schumaker0eecb212014-05-06 09:12:32 -0400569{
570 struct nfs_pgio_data *data = calldata;
571 struct inode *inode = data->header->inode;
572
573 dprintk("NFS: %s: %5u, (status %d)\n", __func__,
574 task->tk_pid, task->tk_status);
575
576 if (data->header->rw_ops->rw_done(task, data, inode) != 0)
577 return;
578 if (task->tk_status < 0)
579 nfs_set_pgio_error(data->header, task->tk_status, data->args.offset);
580 else
581 data->header->rw_ops->rw_result(task, data);
582}
583
Anna Schumakeref2c4882014-05-06 09:12:36 -0400584/*
585 * Generate multiple small requests to read or write a single
586 * contiguous dirty on one page.
587 */
588static int nfs_pgio_multi(struct nfs_pageio_descriptor *desc,
589 struct nfs_pgio_header *hdr)
590{
591 struct nfs_page *req = hdr->req;
592 struct page *page = req->wb_page;
593 struct nfs_pgio_data *data;
594 size_t wsize = desc->pg_bsize, nbytes;
595 unsigned int offset;
596 int requests = 0;
597 struct nfs_commit_info cinfo;
598
599 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
600
601 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
602 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo) ||
603 desc->pg_count > wsize))
604 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
605
606 offset = 0;
607 nbytes = desc->pg_count;
608 do {
609 size_t len = min(nbytes, wsize);
610
611 data = nfs_pgio_data_alloc(hdr, 1);
612 if (!data)
613 return nfs_pgio_error(desc, hdr);
614 data->pages.pagevec[0] = page;
615 nfs_pgio_rpcsetup(data, len, offset, desc->pg_ioflags, &cinfo);
616 list_add(&data->list, &hdr->rpc_list);
617 requests++;
618 nbytes -= len;
619 offset += len;
620 } while (nbytes != 0);
621
622 nfs_list_remove_request(req);
623 nfs_list_add_request(req, &hdr->pages);
624 desc->pg_rpc_callops = &nfs_pgio_common_ops;
625 return 0;
626}
627
628/*
629 * Create an RPC task for the given read or write request and kick it.
630 * The page must have been locked by the caller.
631 *
632 * It may happen that the page we're passed is not marked dirty.
633 * This is the case if nfs_updatepage detects a conflicting request
634 * that has been written but not committed.
635 */
636static int nfs_pgio_one(struct nfs_pageio_descriptor *desc,
637 struct nfs_pgio_header *hdr)
638{
639 struct nfs_page *req;
640 struct page **pages;
641 struct nfs_pgio_data *data;
642 struct list_head *head = &desc->pg_list;
643 struct nfs_commit_info cinfo;
644
645 data = nfs_pgio_data_alloc(hdr, nfs_page_array_len(desc->pg_base,
646 desc->pg_count));
647 if (!data)
648 return nfs_pgio_error(desc, hdr);
649
650 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
651 pages = data->pages.pagevec;
652 while (!list_empty(head)) {
653 req = nfs_list_entry(head->next);
654 nfs_list_remove_request(req);
655 nfs_list_add_request(req, &hdr->pages);
656 *pages++ = req->wb_page;
657 }
658
659 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
660 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
661 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
662
663 /* Set up the argument struct */
664 nfs_pgio_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo);
665 list_add(&data->list, &hdr->rpc_list);
666 desc->pg_rpc_callops = &nfs_pgio_common_ops;
667 return 0;
668}
669
670int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
671 struct nfs_pgio_header *hdr)
672{
673 if (desc->pg_bsize < PAGE_CACHE_SIZE)
674 return nfs_pgio_multi(desc, hdr);
675 return nfs_pgio_one(desc, hdr);
676}
677EXPORT_SYMBOL_GPL(nfs_generic_pgio);
678
Trond Myklebust4109bb72013-09-06 11:09:38 -0400679static bool nfs_match_open_context(const struct nfs_open_context *ctx1,
680 const struct nfs_open_context *ctx2)
681{
682 return ctx1->cred == ctx2->cred && ctx1->state == ctx2->state;
683}
684
685static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
686 const struct nfs_lock_context *l2)
687{
688 return l1->lockowner.l_owner == l2->lockowner.l_owner
689 && l1->lockowner.l_pid == l2->lockowner.l_pid;
690}
691
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400692/**
693 * nfs_can_coalesce_requests - test two requests for compatibility
694 * @prev: pointer to nfs_page
695 * @req: pointer to nfs_page
696 *
697 * The nfs_page structures 'prev' and 'req' are compared to ensure that the
698 * page data area they describe is contiguous, and that their RPC
699 * credentials, NFSv4 open state, and lockowners are the same.
700 *
701 * Return 'true' if this is the case, else return 'false'.
702 */
Benny Halevy18ad0a92011-05-25 21:03:56 +0300703static bool nfs_can_coalesce_requests(struct nfs_page *prev,
704 struct nfs_page *req,
705 struct nfs_pageio_descriptor *pgio)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400706{
Trond Myklebust4109bb72013-09-06 11:09:38 -0400707 if (!nfs_match_open_context(req->wb_context, prev->wb_context))
Benny Halevy18ad0a92011-05-25 21:03:56 +0300708 return false;
Trond Myklebust4109bb72013-09-06 11:09:38 -0400709 if (req->wb_context->dentry->d_inode->i_flock != NULL &&
710 !nfs_match_lock_context(req->wb_lock_context, prev->wb_lock_context))
Benny Halevy18ad0a92011-05-25 21:03:56 +0300711 return false;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400712 if (req->wb_pgbase != 0)
Benny Halevy18ad0a92011-05-25 21:03:56 +0300713 return false;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400714 if (prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
Benny Halevy18ad0a92011-05-25 21:03:56 +0300715 return false;
Fred Isaman1825a0d2012-04-20 19:55:31 -0400716 if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
717 return false;
Trond Myklebust1751c362011-06-10 13:30:23 -0400718 return pgio->pg_ops->pg_test(pgio, prev, req);
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400719}
720
721/**
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400722 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400723 * @desc: destination io descriptor
724 * @req: request
725 *
726 * Returns true if the request 'req' was successfully coalesced into the
727 * existing list of pages 'desc'.
728 */
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400729static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
730 struct nfs_page *req)
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400731{
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400732 if (desc->pg_count != 0) {
733 struct nfs_page *prev;
734
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400735 prev = nfs_list_entry(desc->pg_list.prev);
Fred Isaman94ad1c82011-03-01 01:34:14 +0000736 if (!nfs_can_coalesce_requests(prev, req, desc))
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400737 return 0;
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300738 } else {
Trond Myklebustd8007d42011-06-10 13:30:23 -0400739 if (desc->pg_ops->pg_init)
740 desc->pg_ops->pg_init(desc, req);
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400741 desc->pg_base = req->wb_pgbase;
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300742 }
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400743 nfs_list_remove_request(req);
744 nfs_list_add_request(req, &desc->pg_list);
Boaz Harrosh5b36c7d2011-05-29 11:45:39 +0300745 desc->pg_count += req->wb_bytes;
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400746 return 1;
747}
748
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400749/*
750 * Helper for nfs_pageio_add_request and nfs_pageio_complete
751 */
752static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
753{
754 if (!list_empty(&desc->pg_list)) {
Trond Myklebust1751c362011-06-10 13:30:23 -0400755 int error = desc->pg_ops->pg_doio(desc);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400756 if (error < 0)
757 desc->pg_error = error;
758 else
759 desc->pg_bytes_written += desc->pg_count;
760 }
761 if (list_empty(&desc->pg_list)) {
762 desc->pg_count = 0;
763 desc->pg_base = 0;
764 }
765}
766
767/**
768 * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
769 * @desc: destination io descriptor
770 * @req: request
771 *
772 * Returns true if the request 'req' was successfully coalesced into the
773 * existing list of pages 'desc'.
774 */
Trond Myklebustd9156f92011-07-12 13:42:02 -0400775static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
Trond Myklebust8b09bee2007-04-02 18:48:28 -0400776 struct nfs_page *req)
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400777{
778 while (!nfs_pageio_do_add_request(desc, req)) {
Trond Myklebustb31268a2011-03-21 17:02:00 -0400779 desc->pg_moreio = 1;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400780 nfs_pageio_doio(desc);
781 if (desc->pg_error < 0)
782 return 0;
Trond Myklebustb31268a2011-03-21 17:02:00 -0400783 desc->pg_moreio = 0;
Trond Myklebustd9156f92011-07-12 13:42:02 -0400784 if (desc->pg_recoalesce)
785 return 0;
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400786 }
787 return 1;
788}
789
Trond Myklebustd9156f92011-07-12 13:42:02 -0400790static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
791{
792 LIST_HEAD(head);
793
794 do {
795 list_splice_init(&desc->pg_list, &head);
796 desc->pg_bytes_written -= desc->pg_count;
797 desc->pg_count = 0;
798 desc->pg_base = 0;
799 desc->pg_recoalesce = 0;
800
801 while (!list_empty(&head)) {
802 struct nfs_page *req;
803
804 req = list_first_entry(&head, struct nfs_page, wb_list);
805 nfs_list_remove_request(req);
806 if (__nfs_pageio_add_request(desc, req))
807 continue;
808 if (desc->pg_error < 0)
809 return 0;
810 break;
811 }
812 } while (desc->pg_recoalesce);
813 return 1;
814}
815
816int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
817 struct nfs_page *req)
818{
819 int ret;
820
821 do {
822 ret = __nfs_pageio_add_request(desc, req);
823 if (ret)
824 break;
825 if (desc->pg_error < 0)
826 break;
827 ret = nfs_do_recoalesce(desc);
828 } while (ret);
829 return ret;
830}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400831EXPORT_SYMBOL_GPL(nfs_pageio_add_request);
Trond Myklebustd9156f92011-07-12 13:42:02 -0400832
Trond Myklebustd8a5ad72007-04-02 18:48:28 -0400833/**
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400834 * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
835 * @desc: pointer to io descriptor
836 */
837void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
838{
Trond Myklebustd9156f92011-07-12 13:42:02 -0400839 for (;;) {
840 nfs_pageio_doio(desc);
841 if (!desc->pg_recoalesce)
842 break;
843 if (!nfs_do_recoalesce(desc))
844 break;
845 }
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400846}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400847EXPORT_SYMBOL_GPL(nfs_pageio_complete);
Trond Myklebustbcb71bb2007-04-02 18:48:28 -0400848
Trond Myklebust7fe7f842007-05-20 10:18:27 -0400849/**
850 * nfs_pageio_cond_complete - Conditional I/O completion
851 * @desc: pointer to io descriptor
852 * @index: page index
853 *
854 * It is important to ensure that processes don't try to take locks
855 * on non-contiguous ranges of pages as that might deadlock. This
856 * function should be called before attempting to wait on a locked
857 * nfs_page. It will complete the I/O if the page index 'index'
858 * is not contiguous with the existing list of pages in 'desc'.
859 */
860void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
861{
862 if (!list_empty(&desc->pg_list)) {
863 struct nfs_page *prev = nfs_list_entry(desc->pg_list.prev);
864 if (index != prev->wb_index + 1)
Trond Myklebustd9156f92011-07-12 13:42:02 -0400865 nfs_pageio_complete(desc);
Trond Myklebust7fe7f842007-05-20 10:18:27 -0400866 }
867}
868
David Howellsf7b422b2006-06-09 09:34:33 -0400869int __init nfs_init_nfspagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 nfs_page_cachep = kmem_cache_create("nfs_page",
872 sizeof(struct nfs_page),
873 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +0900874 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (nfs_page_cachep == NULL)
876 return -ENOMEM;
877
878 return 0;
879}
880
David Brownell266bee82006-06-27 12:59:15 -0700881void nfs_destroy_nfspagecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700883 kmem_cache_destroy(nfs_page_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
Anna Schumakeref2c4882014-05-06 09:12:36 -0400886static const struct rpc_call_ops nfs_pgio_common_ops = {
Anna Schumaker6f92fa42014-05-06 09:12:33 -0400887 .rpc_call_prepare = nfs_pgio_prepare,
888 .rpc_call_done = nfs_pgio_result,
889 .rpc_release = nfs_pgio_release,
890};