nfs: page group syncing in read path
[linux-2.6-block.git] / fs / nfs / pagelist.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4
LT
12#include <linux/slab.h>
13#include <linux/file.h>
e8edc6e0 14#include <linux/sched.h>
1da177e4 15#include <linux/sunrpc/clnt.h>
1313e603 16#include <linux/nfs.h>
1da177e4
LT
17#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>
afeacc8c 22#include <linux/export.h>
1da177e4 23
8d5658c9 24#include "internal.h"
bae724ef 25#include "pnfs.h"
8d5658c9 26
0eecb214
AS
27#define NFSDBG_FACILITY NFSDBG_PAGECACHE
28
e18b890b 29static struct kmem_cache *nfs_page_cachep;
ef2c488c 30static const struct rpc_call_ops nfs_pgio_common_ops;
1da177e4 31
2bfc6e56
WAA
32static void nfs_free_request(struct nfs_page *);
33
00bfa30a 34static bool nfs_pgarray_set(struct nfs_page_array *p, unsigned int pagecount)
30dd374f
FI
35{
36 p->npages = pagecount;
37 if (pagecount <= ARRAY_SIZE(p->page_array))
38 p->pagevec = p->page_array;
39 else {
40 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL);
41 if (!p->pagevec)
42 p->npages = 0;
43 }
44 return p->pagevec != NULL;
45}
46
4db6e0b7
FI
47void nfs_pgheader_init(struct nfs_pageio_descriptor *desc,
48 struct nfs_pgio_header *hdr,
49 void (*release)(struct nfs_pgio_header *hdr))
50{
51 hdr->req = nfs_list_entry(desc->pg_list.next);
52 hdr->inode = desc->pg_inode;
53 hdr->cred = hdr->req->wb_context->cred;
54 hdr->io_start = req_offset(hdr->req);
55 hdr->good_bytes = desc->pg_count;
584aa810 56 hdr->dreq = desc->pg_dreq;
f6166384 57 hdr->layout_private = desc->pg_layout_private;
4db6e0b7 58 hdr->release = release;
061ae2ed 59 hdr->completion_ops = desc->pg_completion_ops;
584aa810
FI
60 if (hdr->completion_ops->init_hdr)
61 hdr->completion_ops->init_hdr(hdr);
4db6e0b7 62}
89d77c8f 63EXPORT_SYMBOL_GPL(nfs_pgheader_init);
4db6e0b7
FI
64
65void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos)
66{
67 spin_lock(&hdr->lock);
68 if (pos < hdr->io_start + hdr->good_bytes) {
69 set_bit(NFS_IOHDR_ERROR, &hdr->flags);
70 clear_bit(NFS_IOHDR_EOF, &hdr->flags);
71 hdr->good_bytes = pos - hdr->io_start;
72 hdr->error = error;
73 }
74 spin_unlock(&hdr->lock);
75}
76
1da177e4
LT
77static inline struct nfs_page *
78nfs_page_alloc(void)
79{
192e501b 80 struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_NOIO);
72895b1a 81 if (p)
1da177e4 82 INIT_LIST_HEAD(&p->wb_list);
1da177e4
LT
83 return p;
84}
85
86static inline void
87nfs_page_free(struct nfs_page *p)
88{
89 kmem_cache_free(nfs_page_cachep, p);
90}
91
577b4232
TM
92static void
93nfs_iocounter_inc(struct nfs_io_counter *c)
94{
95 atomic_inc(&c->io_count);
96}
97
98static void
99nfs_iocounter_dec(struct nfs_io_counter *c)
100{
101 if (atomic_dec_and_test(&c->io_count)) {
102 clear_bit(NFS_IO_INPROGRESS, &c->flags);
103 smp_mb__after_clear_bit();
104 wake_up_bit(&c->flags, NFS_IO_INPROGRESS);
105 }
106}
107
108static int
109__nfs_iocounter_wait(struct nfs_io_counter *c)
110{
111 wait_queue_head_t *wq = bit_waitqueue(&c->flags, NFS_IO_INPROGRESS);
112 DEFINE_WAIT_BIT(q, &c->flags, NFS_IO_INPROGRESS);
113 int ret = 0;
114
115 do {
116 prepare_to_wait(wq, &q.wait, TASK_KILLABLE);
117 set_bit(NFS_IO_INPROGRESS, &c->flags);
118 if (atomic_read(&c->io_count) == 0)
119 break;
120 ret = nfs_wait_bit_killable(&c->flags);
121 } while (atomic_read(&c->io_count) != 0);
122 finish_wait(wq, &q.wait);
123 return ret;
124}
125
126/**
127 * nfs_iocounter_wait - wait for i/o to complete
128 * @c: nfs_io_counter to use
129 *
130 * returns -ERESTARTSYS if interrupted by a fatal signal.
131 * Otherwise returns 0 once the io_count hits 0.
132 */
133int
134nfs_iocounter_wait(struct nfs_io_counter *c)
135{
136 if (atomic_read(&c->io_count) == 0)
137 return 0;
138 return __nfs_iocounter_wait(c);
139}
140
2bfc6e56
WAA
141/*
142 * nfs_page_group_lock - lock the head of the page group
143 * @req - request in group that is to be locked
144 *
145 * this lock must be held if modifying the page group list
146 */
147void
148nfs_page_group_lock(struct nfs_page *req)
149{
150 struct nfs_page *head = req->wb_head;
151 int err = -EAGAIN;
152
153 WARN_ON_ONCE(head != head->wb_head);
154
155 while (err)
156 err = wait_on_bit_lock(&head->wb_flags, PG_HEADLOCK,
157 nfs_wait_bit_killable, TASK_KILLABLE);
158}
159
160/*
161 * nfs_page_group_unlock - unlock the head of the page group
162 * @req - request in group that is to be unlocked
163 */
164void
165nfs_page_group_unlock(struct nfs_page *req)
166{
167 struct nfs_page *head = req->wb_head;
168
169 WARN_ON_ONCE(head != head->wb_head);
170
171 smp_mb__before_clear_bit();
172 clear_bit(PG_HEADLOCK, &head->wb_flags);
173 smp_mb__after_clear_bit();
174 wake_up_bit(&head->wb_flags, PG_HEADLOCK);
175}
176
177/*
178 * nfs_page_group_sync_on_bit_locked
179 *
180 * must be called with page group lock held
181 */
182static bool
183nfs_page_group_sync_on_bit_locked(struct nfs_page *req, unsigned int bit)
184{
185 struct nfs_page *head = req->wb_head;
186 struct nfs_page *tmp;
187
188 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_flags));
189 WARN_ON_ONCE(test_and_set_bit(bit, &req->wb_flags));
190
191 tmp = req->wb_this_page;
192 while (tmp != req) {
193 if (!test_bit(bit, &tmp->wb_flags))
194 return false;
195 tmp = tmp->wb_this_page;
196 }
197
198 /* true! reset all bits */
199 tmp = req;
200 do {
201 clear_bit(bit, &tmp->wb_flags);
202 tmp = tmp->wb_this_page;
203 } while (tmp != req);
204
205 return true;
206}
207
208/*
209 * nfs_page_group_sync_on_bit - set bit on current request, but only
210 * return true if the bit is set for all requests in page group
211 * @req - request in page group
212 * @bit - PG_* bit that is used to sync page group
213 */
214bool nfs_page_group_sync_on_bit(struct nfs_page *req, unsigned int bit)
215{
216 bool ret;
217
218 nfs_page_group_lock(req);
219 ret = nfs_page_group_sync_on_bit_locked(req, bit);
220 nfs_page_group_unlock(req);
221
222 return ret;
223}
224
225/*
226 * nfs_page_group_init - Initialize the page group linkage for @req
227 * @req - a new nfs request
228 * @prev - the previous request in page group, or NULL if @req is the first
229 * or only request in the group (the head).
230 */
231static inline void
232nfs_page_group_init(struct nfs_page *req, struct nfs_page *prev)
233{
234 WARN_ON_ONCE(prev == req);
235
236 if (!prev) {
237 req->wb_head = req;
238 req->wb_this_page = req;
239 } else {
240 WARN_ON_ONCE(prev->wb_this_page != prev->wb_head);
241 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &prev->wb_head->wb_flags));
242 req->wb_head = prev->wb_head;
243 req->wb_this_page = prev->wb_this_page;
244 prev->wb_this_page = req;
245
246 /* grab extra ref if head request has extra ref from
247 * the write/commit path to handle handoff between write
248 * and commit lists */
249 if (test_bit(PG_INODE_REF, &prev->wb_head->wb_flags))
250 kref_get(&req->wb_kref);
251 }
252}
253
254/*
255 * nfs_page_group_destroy - sync the destruction of page groups
256 * @req - request that no longer needs the page group
257 *
258 * releases the page group reference from each member once all
259 * members have called this function.
260 */
261static void
262nfs_page_group_destroy(struct kref *kref)
263{
264 struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
265 struct nfs_page *tmp, *next;
266
267 if (!nfs_page_group_sync_on_bit(req, PG_TEARDOWN))
268 return;
269
270 tmp = req;
271 do {
272 next = tmp->wb_this_page;
273 /* unlink and free */
274 tmp->wb_this_page = tmp;
275 tmp->wb_head = tmp;
276 nfs_free_request(tmp);
277 tmp = next;
278 } while (tmp != req);
279}
280
1da177e4
LT
281/**
282 * nfs_create_request - Create an NFS read/write request.
c02f557d 283 * @ctx: open context to use
1da177e4 284 * @page: page to write
2bfc6e56 285 * @last: last nfs request created for this page group or NULL if head
1da177e4
LT
286 * @offset: starting offset within the page for the write
287 * @count: number of bytes to read/write
288 *
289 * The page must be locked by the caller. This makes sure we never
a19b89ca 290 * create two different requests for the same page.
1da177e4
LT
291 * User should ensure it is safe to sleep in this function.
292 */
293struct nfs_page *
8c8f1ac1 294nfs_create_request(struct nfs_open_context *ctx, struct page *page,
2bfc6e56
WAA
295 struct nfs_page *last, unsigned int offset,
296 unsigned int count)
1da177e4 297{
1da177e4 298 struct nfs_page *req;
b3c54de6 299 struct nfs_lock_context *l_ctx;
1da177e4 300
c58c8441
TM
301 if (test_bit(NFS_CONTEXT_BAD, &ctx->flags))
302 return ERR_PTR(-EBADF);
18eb8842
TM
303 /* try to allocate the request struct */
304 req = nfs_page_alloc();
305 if (req == NULL)
306 return ERR_PTR(-ENOMEM);
1da177e4 307
015f0212 308 /* get lock context early so we can deal with alloc failures */
b3c54de6
TM
309 l_ctx = nfs_get_lock_context(ctx);
310 if (IS_ERR(l_ctx)) {
015f0212 311 nfs_page_free(req);
b3c54de6 312 return ERR_CAST(l_ctx);
015f0212 313 }
b3c54de6 314 req->wb_lock_context = l_ctx;
577b4232 315 nfs_iocounter_inc(&l_ctx->io_count);
015f0212 316
1da177e4
LT
317 /* Initialize the request struct. Initially, we assume a
318 * long write-back delay. This will be adjusted in
319 * update_nfs_request below if the region is not locked. */
320 req->wb_page = page;
d56b4ddf 321 req->wb_index = page_file_index(page);
1da177e4
LT
322 page_cache_get(page);
323 req->wb_offset = offset;
324 req->wb_pgbase = offset;
325 req->wb_bytes = count;
1da177e4 326 req->wb_context = get_nfs_open_context(ctx);
c03b4024 327 kref_init(&req->wb_kref);
2bfc6e56 328 nfs_page_group_init(req, last);
1da177e4
LT
329 return req;
330}
331
332/**
1d1afcbc 333 * nfs_unlock_request - Unlock request and wake up sleepers.
1da177e4
LT
334 * @req:
335 */
1d1afcbc 336void nfs_unlock_request(struct nfs_page *req)
1da177e4
LT
337{
338 if (!NFS_WBACK_BUSY(req)) {
339 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
340 BUG();
341 }
342 smp_mb__before_clear_bit();
343 clear_bit(PG_BUSY, &req->wb_flags);
344 smp_mb__after_clear_bit();
464a98bd 345 wake_up_bit(&req->wb_flags, PG_BUSY);
3aff4ebb
TM
346}
347
348/**
1d1afcbc
TM
349 * nfs_unlock_and_release_request - Unlock request and release the nfs_page
350 * @req:
3aff4ebb 351 */
1d1afcbc 352void nfs_unlock_and_release_request(struct nfs_page *req)
3aff4ebb 353{
1d1afcbc 354 nfs_unlock_request(req);
1da177e4
LT
355 nfs_release_request(req);
356}
357
4d65c520 358/*
1da177e4
LT
359 * nfs_clear_request - Free up all resources allocated to the request
360 * @req:
361 *
bb6fbc45
TM
362 * Release page and open context resources associated with a read/write
363 * request after it has completed.
1da177e4 364 */
4d65c520 365static void nfs_clear_request(struct nfs_page *req)
1da177e4 366{
cd52ed35 367 struct page *page = req->wb_page;
bb6fbc45 368 struct nfs_open_context *ctx = req->wb_context;
f11ac8db 369 struct nfs_lock_context *l_ctx = req->wb_lock_context;
bb6fbc45 370
cd52ed35 371 if (page != NULL) {
cd52ed35 372 page_cache_release(page);
1da177e4
LT
373 req->wb_page = NULL;
374 }
f11ac8db 375 if (l_ctx != NULL) {
577b4232 376 nfs_iocounter_dec(&l_ctx->io_count);
f11ac8db
TM
377 nfs_put_lock_context(l_ctx);
378 req->wb_lock_context = NULL;
379 }
bb6fbc45
TM
380 if (ctx != NULL) {
381 put_nfs_open_context(ctx);
382 req->wb_context = NULL;
383 }
1da177e4
LT
384}
385
1da177e4
LT
386/**
387 * nfs_release_request - Release the count on an NFS read/write request
388 * @req: request to release
389 *
390 * Note: Should never be called with the spinlock held!
391 */
2bfc6e56 392static void nfs_free_request(struct nfs_page *req)
1da177e4 393{
2bfc6e56
WAA
394 WARN_ON_ONCE(req->wb_this_page != req);
395
396 /* extra debug: make sure no sync bits are still set */
397 WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
67d0338e
WAA
398 WARN_ON_ONCE(test_bit(PG_UNLOCKPAGE, &req->wb_flags));
399 WARN_ON_ONCE(test_bit(PG_UPTODATE, &req->wb_flags));
1da177e4 400
bb6fbc45 401 /* Release struct file and open context */
1da177e4 402 nfs_clear_request(req);
1da177e4
LT
403 nfs_page_free(req);
404}
405
c03b4024
TM
406void nfs_release_request(struct nfs_page *req)
407{
2bfc6e56 408 kref_put(&req->wb_kref, nfs_page_group_destroy);
c03b4024
TM
409}
410
9f557cd8
TM
411static int nfs_wait_bit_uninterruptible(void *word)
412{
413 io_schedule();
414 return 0;
415}
416
1da177e4
LT
417/**
418 * nfs_wait_on_request - Wait for a request to complete.
419 * @req: request to wait upon.
420 *
150030b7 421 * Interruptible by fatal signals only.
1da177e4
LT
422 * The user is responsible for holding a count on the request.
423 */
424int
425nfs_wait_on_request(struct nfs_page *req)
426{
9f557cd8
TM
427 return wait_on_bit(&req->wb_flags, PG_BUSY,
428 nfs_wait_bit_uninterruptible,
429 TASK_UNINTERRUPTIBLE);
1da177e4
LT
430}
431
b4fdac1a
WAA
432/*
433 * nfs_generic_pg_test - determine if requests can be coalesced
434 * @desc: pointer to descriptor
435 * @prev: previous request in desc, or NULL
436 * @req: this request
437 *
438 * Returns zero if @req can be coalesced into @desc, otherwise it returns
439 * the size of the request.
440 */
441size_t nfs_generic_pg_test(struct nfs_pageio_descriptor *desc,
442 struct nfs_page *prev, struct nfs_page *req)
5b36c7dc 443{
ab75e417
WAA
444 if (!prev)
445 return req->wb_bytes;
5b36c7dc
BH
446 /*
447 * FIXME: ideally we should be able to coalesce all requests
448 * that are not block boundary aligned, but currently this
449 * is problematic for the case of bsize < PAGE_CACHE_SIZE,
450 * since nfs_flush_multi and nfs_pagein_multi assume you
451 * can have only one struct nfs_page.
452 */
453 if (desc->pg_bsize < PAGE_SIZE)
454 return 0;
455
b4fdac1a
WAA
456 if (desc->pg_count + req->wb_bytes <= desc->pg_bsize)
457 return req->wb_bytes;
458 return 0;
5b36c7dc 459}
19345cb2 460EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
5b36c7dc 461
00bfa30a
AS
462static inline struct nfs_rw_header *NFS_RW_HEADER(struct nfs_pgio_header *hdr)
463{
464 return container_of(hdr, struct nfs_rw_header, header);
465}
466
4a0de55c
AS
467/**
468 * nfs_rw_header_alloc - Allocate a header for a read or write
469 * @ops: Read or write function vector
470 */
471struct nfs_rw_header *nfs_rw_header_alloc(const struct nfs_rw_ops *ops)
472{
473 struct nfs_rw_header *header = ops->rw_alloc_header();
474
475 if (header) {
476 struct nfs_pgio_header *hdr = &header->header;
477
478 INIT_LIST_HEAD(&hdr->pages);
479 INIT_LIST_HEAD(&hdr->rpc_list);
480 spin_lock_init(&hdr->lock);
481 atomic_set(&hdr->refcnt, 0);
482 hdr->rw_ops = ops;
483 }
484 return header;
485}
486EXPORT_SYMBOL_GPL(nfs_rw_header_alloc);
487
488/*
489 * nfs_rw_header_free - Free a read or write header
490 * @hdr: The header to free
491 */
492void nfs_rw_header_free(struct nfs_pgio_header *hdr)
493{
494 hdr->rw_ops->rw_free_header(NFS_RW_HEADER(hdr));
495}
496EXPORT_SYMBOL_GPL(nfs_rw_header_free);
497
00bfa30a
AS
498/**
499 * nfs_pgio_data_alloc - Allocate pageio data
500 * @hdr: The header making a request
501 * @pagecount: Number of pages to create
502 */
ef2c488c
AS
503static struct nfs_pgio_data *nfs_pgio_data_alloc(struct nfs_pgio_header *hdr,
504 unsigned int pagecount)
00bfa30a
AS
505{
506 struct nfs_pgio_data *data, *prealloc;
507
508 prealloc = &NFS_RW_HEADER(hdr)->rpc_data;
509 if (prealloc->header == NULL)
510 data = prealloc;
511 else
512 data = kzalloc(sizeof(*data), GFP_KERNEL);
513 if (!data)
514 goto out;
515
516 if (nfs_pgarray_set(&data->pages, pagecount)) {
517 data->header = hdr;
518 atomic_inc(&hdr->refcnt);
519 } else {
520 if (data != prealloc)
521 kfree(data);
522 data = NULL;
523 }
524out:
525 return data;
526}
527
528/**
529 * nfs_pgio_data_release - Properly free pageio data
530 * @data: The data to release
531 */
532void nfs_pgio_data_release(struct nfs_pgio_data *data)
533{
534 struct nfs_pgio_header *hdr = data->header;
535 struct nfs_rw_header *pageio_header = NFS_RW_HEADER(hdr);
536
537 put_nfs_open_context(data->args.context);
538 if (data->pages.pagevec != data->pages.page_array)
539 kfree(data->pages.pagevec);
540 if (data == &pageio_header->rpc_data) {
541 data->header = NULL;
542 data = NULL;
543 }
544 if (atomic_dec_and_test(&hdr->refcnt))
545 hdr->completion_ops->completion(hdr);
546 /* Note: we only free the rpc_task after callbacks are done.
547 * See the comment in rpc_free_task() for why
548 */
549 kfree(data);
550}
551EXPORT_SYMBOL_GPL(nfs_pgio_data_release);
552
ce59515c
AS
553/**
554 * nfs_pgio_rpcsetup - Set up arguments for a pageio call
555 * @data: The pageio data
556 * @count: Number of bytes to read
557 * @offset: Initial offset
558 * @how: How to commit data (writes only)
559 * @cinfo: Commit information for the call (writes only)
560 */
ef2c488c 561static void nfs_pgio_rpcsetup(struct nfs_pgio_data *data,
ce59515c
AS
562 unsigned int count, unsigned int offset,
563 int how, struct nfs_commit_info *cinfo)
564{
565 struct nfs_page *req = data->header->req;
566
567 /* Set up the RPC argument and reply structs
568 * NB: take care not to mess about with data->commit et al. */
569
570 data->args.fh = NFS_FH(data->header->inode);
571 data->args.offset = req_offset(req) + offset;
572 /* pnfs_set_layoutcommit needs this */
573 data->mds_offset = data->args.offset;
574 data->args.pgbase = req->wb_pgbase + offset;
575 data->args.pages = data->pages.pagevec;
576 data->args.count = count;
577 data->args.context = get_nfs_open_context(req->wb_context);
578 data->args.lock_context = req->wb_lock_context;
579 data->args.stable = NFS_UNSTABLE;
580 switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
581 case 0:
582 break;
583 case FLUSH_COND_STABLE:
584 if (nfs_reqs_to_commit(cinfo))
585 break;
586 default:
587 data->args.stable = NFS_FILE_SYNC;
588 }
589
590 data->res.fattr = &data->fattr;
591 data->res.count = count;
592 data->res.eof = 0;
593 data->res.verf = &data->verf;
594 nfs_fattr_init(&data->fattr);
595}
596
a4cdda59
AS
597/**
598 * nfs_pgio_prepare - Prepare pageio data to go over the wire
599 * @task: The current task
600 * @calldata: pageio data to prepare
601 */
6f92fa45 602static void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
a4cdda59
AS
603{
604 struct nfs_pgio_data *data = calldata;
605 int err;
606 err = NFS_PROTO(data->header->inode)->pgio_rpc_prepare(task, data);
607 if (err)
608 rpc_exit(task, err);
609}
610
1ed26f33
AS
611int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_data *data,
612 const struct rpc_call_ops *call_ops, int how, int flags)
613{
614 struct rpc_task *task;
615 struct rpc_message msg = {
616 .rpc_argp = &data->args,
617 .rpc_resp = &data->res,
618 .rpc_cred = data->header->cred,
619 };
620 struct rpc_task_setup task_setup_data = {
621 .rpc_client = clnt,
622 .task = &data->task,
623 .rpc_message = &msg,
624 .callback_ops = call_ops,
625 .callback_data = data,
626 .workqueue = nfsiod_workqueue,
627 .flags = RPC_TASK_ASYNC | flags,
628 };
629 int ret = 0;
630
631 data->header->rw_ops->rw_initiate(data, &msg, &task_setup_data, how);
632
633 dprintk("NFS: %5u initiated pgio call "
634 "(req %s/%llu, %u bytes @ offset %llu)\n",
635 data->task.tk_pid,
636 data->header->inode->i_sb->s_id,
637 (unsigned long long)NFS_FILEID(data->header->inode),
638 data->args.count,
639 (unsigned long long)data->args.offset);
640
641 task = rpc_run_task(&task_setup_data);
642 if (IS_ERR(task)) {
643 ret = PTR_ERR(task);
644 goto out;
645 }
646 if (how & FLUSH_SYNC) {
647 ret = rpc_wait_for_completion_task(task);
648 if (ret == 0)
649 ret = task->tk_status;
650 }
651 rpc_put_task(task);
652out:
653 return ret;
654}
655EXPORT_SYMBOL_GPL(nfs_initiate_pgio);
656
cf485fcd 657static int nfs_do_multiple_pgios(struct list_head *head,
c3766276
AS
658 const struct rpc_call_ops *call_ops,
659 int how)
660{
661 struct nfs_pgio_data *data;
662 int ret = 0;
663
664 while (!list_empty(head)) {
665 int ret2;
666
667 data = list_first_entry(head, struct nfs_pgio_data, list);
668 list_del_init(&data->list);
669
670 ret2 = nfs_initiate_pgio(NFS_CLIENT(data->header->inode),
671 data, call_ops, how, 0);
672 if (ret == 0)
673 ret = ret2;
674 }
675 return ret;
676}
677
844c9e69
AS
678/**
679 * nfs_pgio_error - Clean up from a pageio error
680 * @desc: IO descriptor
681 * @hdr: pageio header
682 */
ef2c488c 683static int nfs_pgio_error(struct nfs_pageio_descriptor *desc,
844c9e69
AS
684 struct nfs_pgio_header *hdr)
685{
686 struct nfs_pgio_data *data;
687
688 set_bit(NFS_IOHDR_REDO, &hdr->flags);
689 while (!list_empty(&hdr->rpc_list)) {
690 data = list_first_entry(&hdr->rpc_list, struct nfs_pgio_data, list);
691 list_del(&data->list);
692 nfs_pgio_data_release(data);
693 }
694 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
695 return -ENOMEM;
696}
697
a4cdda59
AS
698/**
699 * nfs_pgio_release - Release pageio data
700 * @calldata: The pageio data to release
701 */
6f92fa45 702static void nfs_pgio_release(void *calldata)
a4cdda59
AS
703{
704 struct nfs_pgio_data *data = calldata;
705 if (data->header->rw_ops->rw_release)
706 data->header->rw_ops->rw_release(data);
707 nfs_pgio_data_release(data);
708}
709
1da177e4 710/**
d8a5ad75
TM
711 * nfs_pageio_init - initialise a page io descriptor
712 * @desc: pointer to descriptor
bcb71bba
TM
713 * @inode: pointer to inode
714 * @doio: pointer to io function
715 * @bsize: io block size
716 * @io_flags: extra parameters for the io function
d8a5ad75 717 */
bcb71bba
TM
718void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
719 struct inode *inode,
1751c363 720 const struct nfs_pageio_ops *pg_ops,
061ae2ed 721 const struct nfs_pgio_completion_ops *compl_ops,
4a0de55c 722 const struct nfs_rw_ops *rw_ops,
84dde76c 723 size_t bsize,
bcb71bba 724 int io_flags)
d8a5ad75
TM
725{
726 INIT_LIST_HEAD(&desc->pg_list);
bcb71bba 727 desc->pg_bytes_written = 0;
d8a5ad75
TM
728 desc->pg_count = 0;
729 desc->pg_bsize = bsize;
730 desc->pg_base = 0;
b31268ac 731 desc->pg_moreio = 0;
d9156f9f 732 desc->pg_recoalesce = 0;
bcb71bba 733 desc->pg_inode = inode;
1751c363 734 desc->pg_ops = pg_ops;
061ae2ed 735 desc->pg_completion_ops = compl_ops;
4a0de55c 736 desc->pg_rw_ops = rw_ops;
bcb71bba
TM
737 desc->pg_ioflags = io_flags;
738 desc->pg_error = 0;
94ad1c80 739 desc->pg_lseg = NULL;
584aa810 740 desc->pg_dreq = NULL;
f6166384 741 desc->pg_layout_private = NULL;
d8a5ad75 742}
89d77c8f 743EXPORT_SYMBOL_GPL(nfs_pageio_init);
d8a5ad75 744
0eecb214
AS
745/**
746 * nfs_pgio_result - Basic pageio error handling
747 * @task: The task that ran
748 * @calldata: Pageio data to check
749 */
6f92fa45 750static void nfs_pgio_result(struct rpc_task *task, void *calldata)
0eecb214
AS
751{
752 struct nfs_pgio_data *data = calldata;
753 struct inode *inode = data->header->inode;
754
755 dprintk("NFS: %s: %5u, (status %d)\n", __func__,
756 task->tk_pid, task->tk_status);
757
758 if (data->header->rw_ops->rw_done(task, data, inode) != 0)
759 return;
760 if (task->tk_status < 0)
761 nfs_set_pgio_error(data->header, task->tk_status, data->args.offset);
762 else
763 data->header->rw_ops->rw_result(task, data);
764}
765
ef2c488c
AS
766/*
767 * Generate multiple small requests to read or write a single
768 * contiguous dirty on one page.
769 */
770static int nfs_pgio_multi(struct nfs_pageio_descriptor *desc,
771 struct nfs_pgio_header *hdr)
772{
773 struct nfs_page *req = hdr->req;
774 struct page *page = req->wb_page;
775 struct nfs_pgio_data *data;
776 size_t wsize = desc->pg_bsize, nbytes;
777 unsigned int offset;
778 int requests = 0;
779 struct nfs_commit_info cinfo;
780
781 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
782
783 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
784 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo) ||
785 desc->pg_count > wsize))
786 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
787
788 offset = 0;
789 nbytes = desc->pg_count;
790 do {
791 size_t len = min(nbytes, wsize);
792
793 data = nfs_pgio_data_alloc(hdr, 1);
794 if (!data)
795 return nfs_pgio_error(desc, hdr);
796 data->pages.pagevec[0] = page;
797 nfs_pgio_rpcsetup(data, len, offset, desc->pg_ioflags, &cinfo);
798 list_add(&data->list, &hdr->rpc_list);
799 requests++;
800 nbytes -= len;
801 offset += len;
802 } while (nbytes != 0);
803
804 nfs_list_remove_request(req);
805 nfs_list_add_request(req, &hdr->pages);
806 desc->pg_rpc_callops = &nfs_pgio_common_ops;
807 return 0;
808}
809
810/*
811 * Create an RPC task for the given read or write request and kick it.
812 * The page must have been locked by the caller.
813 *
814 * It may happen that the page we're passed is not marked dirty.
815 * This is the case if nfs_updatepage detects a conflicting request
816 * that has been written but not committed.
817 */
818static int nfs_pgio_one(struct nfs_pageio_descriptor *desc,
819 struct nfs_pgio_header *hdr)
820{
821 struct nfs_page *req;
822 struct page **pages;
823 struct nfs_pgio_data *data;
824 struct list_head *head = &desc->pg_list;
825 struct nfs_commit_info cinfo;
826
827 data = nfs_pgio_data_alloc(hdr, nfs_page_array_len(desc->pg_base,
828 desc->pg_count));
829 if (!data)
830 return nfs_pgio_error(desc, hdr);
831
832 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
833 pages = data->pages.pagevec;
834 while (!list_empty(head)) {
835 req = nfs_list_entry(head->next);
836 nfs_list_remove_request(req);
837 nfs_list_add_request(req, &hdr->pages);
838 *pages++ = req->wb_page;
839 }
840
841 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
842 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
843 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
844
845 /* Set up the argument struct */
846 nfs_pgio_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo);
847 list_add(&data->list, &hdr->rpc_list);
848 desc->pg_rpc_callops = &nfs_pgio_common_ops;
849 return 0;
850}
851
41d8d5b7 852static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
cf485fcd
AS
853{
854 struct nfs_rw_header *rw_hdr;
855 struct nfs_pgio_header *hdr;
856 int ret;
857
858 rw_hdr = nfs_rw_header_alloc(desc->pg_rw_ops);
859 if (!rw_hdr) {
860 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
861 return -ENOMEM;
862 }
863 hdr = &rw_hdr->header;
864 nfs_pgheader_init(desc, hdr, nfs_rw_header_free);
865 atomic_inc(&hdr->refcnt);
866 ret = nfs_generic_pgio(desc, hdr);
867 if (ret == 0)
868 ret = nfs_do_multiple_pgios(&hdr->rpc_list,
869 desc->pg_rpc_callops,
870 desc->pg_ioflags);
871 if (atomic_dec_and_test(&hdr->refcnt))
872 hdr->completion_ops->completion(hdr);
873 return ret;
874}
875
ef2c488c
AS
876int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
877 struct nfs_pgio_header *hdr)
878{
879 if (desc->pg_bsize < PAGE_CACHE_SIZE)
880 return nfs_pgio_multi(desc, hdr);
881 return nfs_pgio_one(desc, hdr);
882}
883EXPORT_SYMBOL_GPL(nfs_generic_pgio);
884
4109bb74
TM
885static bool nfs_match_open_context(const struct nfs_open_context *ctx1,
886 const struct nfs_open_context *ctx2)
887{
888 return ctx1->cred == ctx2->cred && ctx1->state == ctx2->state;
889}
890
891static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
892 const struct nfs_lock_context *l2)
893{
894 return l1->lockowner.l_owner == l2->lockowner.l_owner
895 && l1->lockowner.l_pid == l2->lockowner.l_pid;
896}
897
d8a5ad75
TM
898/**
899 * nfs_can_coalesce_requests - test two requests for compatibility
900 * @prev: pointer to nfs_page
901 * @req: pointer to nfs_page
902 *
903 * The nfs_page structures 'prev' and 'req' are compared to ensure that the
904 * page data area they describe is contiguous, and that their RPC
905 * credentials, NFSv4 open state, and lockowners are the same.
906 *
907 * Return 'true' if this is the case, else return 'false'.
908 */
18ad0a9f
BH
909static bool nfs_can_coalesce_requests(struct nfs_page *prev,
910 struct nfs_page *req,
911 struct nfs_pageio_descriptor *pgio)
d8a5ad75 912{
b4fdac1a
WAA
913 size_t size;
914
ab75e417
WAA
915 if (prev) {
916 if (!nfs_match_open_context(req->wb_context, prev->wb_context))
917 return false;
918 if (req->wb_context->dentry->d_inode->i_flock != NULL &&
919 !nfs_match_lock_context(req->wb_lock_context,
920 prev->wb_lock_context))
921 return false;
922 if (req->wb_pgbase != 0)
923 return false;
924 if (prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
925 return false;
926 if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
927 return false;
928 }
b4fdac1a
WAA
929 size = pgio->pg_ops->pg_test(pgio, prev, req);
930 WARN_ON_ONCE(size && size != req->wb_bytes);
931 return size > 0;
d8a5ad75
TM
932}
933
934/**
bcb71bba 935 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
d8a5ad75
TM
936 * @desc: destination io descriptor
937 * @req: request
938 *
939 * Returns true if the request 'req' was successfully coalesced into the
940 * existing list of pages 'desc'.
941 */
bcb71bba
TM
942static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
943 struct nfs_page *req)
d8a5ad75 944{
ab75e417 945 struct nfs_page *prev = NULL;
d8a5ad75 946 if (desc->pg_count != 0) {
d8a5ad75 947 prev = nfs_list_entry(desc->pg_list.prev);
5b36c7dc 948 } else {
d8007d4d
TM
949 if (desc->pg_ops->pg_init)
950 desc->pg_ops->pg_init(desc, req);
d8a5ad75 951 desc->pg_base = req->wb_pgbase;
5b36c7dc 952 }
ab75e417
WAA
953 if (!nfs_can_coalesce_requests(prev, req, desc))
954 return 0;
d8a5ad75
TM
955 nfs_list_remove_request(req);
956 nfs_list_add_request(req, &desc->pg_list);
5b36c7dc 957 desc->pg_count += req->wb_bytes;
d8a5ad75
TM
958 return 1;
959}
960
bcb71bba
TM
961/*
962 * Helper for nfs_pageio_add_request and nfs_pageio_complete
963 */
964static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
965{
966 if (!list_empty(&desc->pg_list)) {
1751c363 967 int error = desc->pg_ops->pg_doio(desc);
bcb71bba
TM
968 if (error < 0)
969 desc->pg_error = error;
970 else
971 desc->pg_bytes_written += desc->pg_count;
972 }
973 if (list_empty(&desc->pg_list)) {
974 desc->pg_count = 0;
975 desc->pg_base = 0;
976 }
977}
978
979/**
980 * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
981 * @desc: destination io descriptor
982 * @req: request
983 *
2bfc6e56
WAA
984 * This may split a request into subrequests which are all part of the
985 * same page group.
986 *
bcb71bba
TM
987 * Returns true if the request 'req' was successfully coalesced into the
988 * existing list of pages 'desc'.
989 */
d9156f9f 990static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
8b09bee3 991 struct nfs_page *req)
bcb71bba 992{
2bfc6e56
WAA
993 struct nfs_page *subreq;
994 unsigned int bytes_left = 0;
995 unsigned int offset, pgbase;
996
997 nfs_page_group_lock(req);
998
999 subreq = req;
1000 bytes_left = subreq->wb_bytes;
1001 offset = subreq->wb_offset;
1002 pgbase = subreq->wb_pgbase;
1003
1004 do {
1005 if (!nfs_pageio_do_add_request(desc, subreq)) {
1006 /* make sure pg_test call(s) did nothing */
1007 WARN_ON_ONCE(subreq->wb_bytes != bytes_left);
1008 WARN_ON_ONCE(subreq->wb_offset != offset);
1009 WARN_ON_ONCE(subreq->wb_pgbase != pgbase);
1010
1011 nfs_page_group_unlock(req);
1012 desc->pg_moreio = 1;
1013 nfs_pageio_doio(desc);
1014 if (desc->pg_error < 0)
1015 return 0;
1016 desc->pg_moreio = 0;
1017 if (desc->pg_recoalesce)
1018 return 0;
1019 /* retry add_request for this subreq */
1020 nfs_page_group_lock(req);
1021 continue;
1022 }
1023
1024 /* check for buggy pg_test call(s) */
1025 WARN_ON_ONCE(subreq->wb_bytes + subreq->wb_pgbase > PAGE_SIZE);
1026 WARN_ON_ONCE(subreq->wb_bytes > bytes_left);
1027 WARN_ON_ONCE(subreq->wb_bytes == 0);
1028
1029 bytes_left -= subreq->wb_bytes;
1030 offset += subreq->wb_bytes;
1031 pgbase += subreq->wb_bytes;
1032
1033 if (bytes_left) {
1034 subreq = nfs_create_request(req->wb_context,
1035 req->wb_page,
1036 subreq, pgbase, bytes_left);
1037 nfs_lock_request(subreq);
1038 subreq->wb_offset = offset;
1039 subreq->wb_index = req->wb_index;
1040 }
1041 } while (bytes_left > 0);
1042
1043 nfs_page_group_unlock(req);
bcb71bba
TM
1044 return 1;
1045}
1046
d9156f9f
TM
1047static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
1048{
1049 LIST_HEAD(head);
1050
1051 do {
1052 list_splice_init(&desc->pg_list, &head);
1053 desc->pg_bytes_written -= desc->pg_count;
1054 desc->pg_count = 0;
1055 desc->pg_base = 0;
1056 desc->pg_recoalesce = 0;
1057
1058 while (!list_empty(&head)) {
1059 struct nfs_page *req;
1060
1061 req = list_first_entry(&head, struct nfs_page, wb_list);
1062 nfs_list_remove_request(req);
1063 if (__nfs_pageio_add_request(desc, req))
1064 continue;
1065 if (desc->pg_error < 0)
1066 return 0;
1067 break;
1068 }
1069 } while (desc->pg_recoalesce);
1070 return 1;
1071}
1072
1073int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
1074 struct nfs_page *req)
1075{
1076 int ret;
1077
1078 do {
1079 ret = __nfs_pageio_add_request(desc, req);
1080 if (ret)
1081 break;
1082 if (desc->pg_error < 0)
1083 break;
1084 ret = nfs_do_recoalesce(desc);
1085 } while (ret);
1086 return ret;
1087}
89d77c8f 1088EXPORT_SYMBOL_GPL(nfs_pageio_add_request);
d9156f9f 1089
bcb71bba
TM
1090/**
1091 * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
1092 * @desc: pointer to io descriptor
1093 */
1094void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
1095{
d9156f9f
TM
1096 for (;;) {
1097 nfs_pageio_doio(desc);
1098 if (!desc->pg_recoalesce)
1099 break;
1100 if (!nfs_do_recoalesce(desc))
1101 break;
1102 }
bcb71bba 1103}
89d77c8f 1104EXPORT_SYMBOL_GPL(nfs_pageio_complete);
bcb71bba 1105
7fe7f848
TM
1106/**
1107 * nfs_pageio_cond_complete - Conditional I/O completion
1108 * @desc: pointer to io descriptor
1109 * @index: page index
1110 *
1111 * It is important to ensure that processes don't try to take locks
1112 * on non-contiguous ranges of pages as that might deadlock. This
1113 * function should be called before attempting to wait on a locked
1114 * nfs_page. It will complete the I/O if the page index 'index'
1115 * is not contiguous with the existing list of pages in 'desc'.
1116 */
1117void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
1118{
1119 if (!list_empty(&desc->pg_list)) {
1120 struct nfs_page *prev = nfs_list_entry(desc->pg_list.prev);
1121 if (index != prev->wb_index + 1)
d9156f9f 1122 nfs_pageio_complete(desc);
7fe7f848
TM
1123 }
1124}
1125
f7b422b1 1126int __init nfs_init_nfspagecache(void)
1da177e4
LT
1127{
1128 nfs_page_cachep = kmem_cache_create("nfs_page",
1129 sizeof(struct nfs_page),
1130 0, SLAB_HWCACHE_ALIGN,
20c2df83 1131 NULL);
1da177e4
LT
1132 if (nfs_page_cachep == NULL)
1133 return -ENOMEM;
1134
1135 return 0;
1136}
1137
266bee88 1138void nfs_destroy_nfspagecache(void)
1da177e4 1139{
1a1d92c1 1140 kmem_cache_destroy(nfs_page_cachep);
1da177e4
LT
1141}
1142
ef2c488c 1143static const struct rpc_call_ops nfs_pgio_common_ops = {
6f92fa45
AS
1144 .rpc_call_prepare = nfs_pgio_prepare,
1145 .rpc_call_done = nfs_pgio_result,
1146 .rpc_release = nfs_pgio_release,
1147};
41d8d5b7
AS
1148
1149const struct nfs_pageio_ops nfs_pgio_rw_ops = {
1150 .pg_test = nfs_generic_pg_test,
1151 .pg_doio = nfs_generic_pg_pgios,
1152};