NFS: Create a generic_pgio function
[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
00bfa30a 32static bool nfs_pgarray_set(struct nfs_page_array *p, unsigned int pagecount)
30dd374f
FI
33{
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
4db6e0b7
FI
45void 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;
584aa810 54 hdr->dreq = desc->pg_dreq;
f6166384 55 hdr->layout_private = desc->pg_layout_private;
4db6e0b7 56 hdr->release = release;
061ae2ed 57 hdr->completion_ops = desc->pg_completion_ops;
584aa810
FI
58 if (hdr->completion_ops->init_hdr)
59 hdr->completion_ops->init_hdr(hdr);
4db6e0b7 60}
89d77c8f 61EXPORT_SYMBOL_GPL(nfs_pgheader_init);
4db6e0b7
FI
62
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
1da177e4
LT
75static inline struct nfs_page *
76nfs_page_alloc(void)
77{
192e501b 78 struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_NOIO);
72895b1a 79 if (p)
1da177e4 80 INIT_LIST_HEAD(&p->wb_list);
1da177e4
LT
81 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
577b4232
TM
90static 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
1da177e4
LT
139/**
140 * nfs_create_request - Create an NFS read/write request.
c02f557d 141 * @ctx: open context to use
1da177e4
LT
142 * @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
a19b89ca 148 * create two different requests for the same page.
1da177e4
LT
149 * 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{
1da177e4 156 struct nfs_page *req;
b3c54de6 157 struct nfs_lock_context *l_ctx;
1da177e4 158
c58c8441
TM
159 if (test_bit(NFS_CONTEXT_BAD, &ctx->flags))
160 return ERR_PTR(-EBADF);
18eb8842
TM
161 /* try to allocate the request struct */
162 req = nfs_page_alloc();
163 if (req == NULL)
164 return ERR_PTR(-ENOMEM);
1da177e4 165
015f0212 166 /* get lock context early so we can deal with alloc failures */
b3c54de6
TM
167 l_ctx = nfs_get_lock_context(ctx);
168 if (IS_ERR(l_ctx)) {
015f0212 169 nfs_page_free(req);
b3c54de6 170 return ERR_CAST(l_ctx);
015f0212 171 }
b3c54de6 172 req->wb_lock_context = l_ctx;
577b4232 173 nfs_iocounter_inc(&l_ctx->io_count);
015f0212 174
1da177e4
LT
175 /* 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;
d56b4ddf 179 req->wb_index = page_file_index(page);
1da177e4
LT
180 page_cache_get(page);
181 req->wb_offset = offset;
182 req->wb_pgbase = offset;
183 req->wb_bytes = count;
1da177e4 184 req->wb_context = get_nfs_open_context(ctx);
c03b4024 185 kref_init(&req->wb_kref);
1da177e4
LT
186 return req;
187}
188
189/**
1d1afcbc 190 * nfs_unlock_request - Unlock request and wake up sleepers.
1da177e4
LT
191 * @req:
192 */
1d1afcbc 193void nfs_unlock_request(struct nfs_page *req)
1da177e4
LT
194{
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();
464a98bd 202 wake_up_bit(&req->wb_flags, PG_BUSY);
3aff4ebb
TM
203}
204
205/**
1d1afcbc
TM
206 * nfs_unlock_and_release_request - Unlock request and release the nfs_page
207 * @req:
3aff4ebb 208 */
1d1afcbc 209void nfs_unlock_and_release_request(struct nfs_page *req)
3aff4ebb 210{
1d1afcbc 211 nfs_unlock_request(req);
1da177e4
LT
212 nfs_release_request(req);
213}
214
4d65c520 215/*
1da177e4
LT
216 * nfs_clear_request - Free up all resources allocated to the request
217 * @req:
218 *
bb6fbc45
TM
219 * Release page and open context resources associated with a read/write
220 * request after it has completed.
1da177e4 221 */
4d65c520 222static void nfs_clear_request(struct nfs_page *req)
1da177e4 223{
cd52ed35 224 struct page *page = req->wb_page;
bb6fbc45 225 struct nfs_open_context *ctx = req->wb_context;
f11ac8db 226 struct nfs_lock_context *l_ctx = req->wb_lock_context;
bb6fbc45 227
cd52ed35 228 if (page != NULL) {
cd52ed35 229 page_cache_release(page);
1da177e4
LT
230 req->wb_page = NULL;
231 }
f11ac8db 232 if (l_ctx != NULL) {
577b4232 233 nfs_iocounter_dec(&l_ctx->io_count);
f11ac8db
TM
234 nfs_put_lock_context(l_ctx);
235 req->wb_lock_context = NULL;
236 }
bb6fbc45
TM
237 if (ctx != NULL) {
238 put_nfs_open_context(ctx);
239 req->wb_context = NULL;
240 }
1da177e4
LT
241}
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 */
c03b4024 250static void nfs_free_request(struct kref *kref)
1da177e4 251{
c03b4024 252 struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
1da177e4 253
bb6fbc45 254 /* Release struct file and open context */
1da177e4 255 nfs_clear_request(req);
1da177e4
LT
256 nfs_page_free(req);
257}
258
c03b4024
TM
259void nfs_release_request(struct nfs_page *req)
260{
261 kref_put(&req->wb_kref, nfs_free_request);
262}
263
9f557cd8
TM
264static int nfs_wait_bit_uninterruptible(void *word)
265{
266 io_schedule();
267 return 0;
268}
269
1da177e4
LT
270/**
271 * nfs_wait_on_request - Wait for a request to complete.
272 * @req: request to wait upon.
273 *
150030b7 274 * Interruptible by fatal signals only.
1da177e4
LT
275 * The user is responsible for holding a count on the request.
276 */
277int
278nfs_wait_on_request(struct nfs_page *req)
279{
9f557cd8
TM
280 return wait_on_bit(&req->wb_flags, PG_BUSY,
281 nfs_wait_bit_uninterruptible,
282 TASK_UNINTERRUPTIBLE);
1da177e4
LT
283}
284
19345cb2 285bool nfs_generic_pg_test(struct nfs_pageio_descriptor *desc, struct nfs_page *prev, struct nfs_page *req)
5b36c7dc
BH
286{
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}
19345cb2 299EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
5b36c7dc 300
00bfa30a
AS
301static 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
4a0de55c
AS
306/**
307 * 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
00bfa30a
AS
337/**
338 * nfs_pgio_data_alloc - Allocate pageio data
339 * @hdr: The header making a request
340 * @pagecount: Number of pages to create
341 */
ef2c488c
AS
342static struct nfs_pgio_data *nfs_pgio_data_alloc(struct nfs_pgio_header *hdr,
343 unsigned int pagecount)
00bfa30a
AS
344{
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
ce59515c
AS
392/**
393 * 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 */
ef2c488c 400static void nfs_pgio_rpcsetup(struct nfs_pgio_data *data,
ce59515c
AS
401 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
a4cdda59
AS
436/**
437 * nfs_pgio_prepare - Prepare pageio data to go over the wire
438 * @task: The current task
439 * @calldata: pageio data to prepare
440 */
6f92fa45 441static void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
a4cdda59
AS
442{
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
844c9e69
AS
450/**
451 * nfs_pgio_error - Clean up from a pageio error
452 * @desc: IO descriptor
453 * @hdr: pageio header
454 */
ef2c488c 455static int nfs_pgio_error(struct nfs_pageio_descriptor *desc,
844c9e69
AS
456 struct nfs_pgio_header *hdr)
457{
458 struct nfs_pgio_data *data;
459
460 set_bit(NFS_IOHDR_REDO, &hdr->flags);
461 while (!list_empty(&hdr->rpc_list)) {
462 data = list_first_entry(&hdr->rpc_list, struct nfs_pgio_data, list);
463 list_del(&data->list);
464 nfs_pgio_data_release(data);
465 }
466 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
467 return -ENOMEM;
468}
469
a4cdda59
AS
470/**
471 * nfs_pgio_release - Release pageio data
472 * @calldata: The pageio data to release
473 */
6f92fa45 474static void nfs_pgio_release(void *calldata)
a4cdda59
AS
475{
476 struct nfs_pgio_data *data = calldata;
477 if (data->header->rw_ops->rw_release)
478 data->header->rw_ops->rw_release(data);
479 nfs_pgio_data_release(data);
480}
481
1da177e4 482/**
d8a5ad75
TM
483 * nfs_pageio_init - initialise a page io descriptor
484 * @desc: pointer to descriptor
bcb71bba
TM
485 * @inode: pointer to inode
486 * @doio: pointer to io function
487 * @bsize: io block size
488 * @io_flags: extra parameters for the io function
d8a5ad75 489 */
bcb71bba
TM
490void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
491 struct inode *inode,
1751c363 492 const struct nfs_pageio_ops *pg_ops,
061ae2ed 493 const struct nfs_pgio_completion_ops *compl_ops,
4a0de55c 494 const struct nfs_rw_ops *rw_ops,
84dde76c 495 size_t bsize,
bcb71bba 496 int io_flags)
d8a5ad75
TM
497{
498 INIT_LIST_HEAD(&desc->pg_list);
bcb71bba 499 desc->pg_bytes_written = 0;
d8a5ad75
TM
500 desc->pg_count = 0;
501 desc->pg_bsize = bsize;
502 desc->pg_base = 0;
b31268ac 503 desc->pg_moreio = 0;
d9156f9f 504 desc->pg_recoalesce = 0;
bcb71bba 505 desc->pg_inode = inode;
1751c363 506 desc->pg_ops = pg_ops;
061ae2ed 507 desc->pg_completion_ops = compl_ops;
4a0de55c 508 desc->pg_rw_ops = rw_ops;
bcb71bba
TM
509 desc->pg_ioflags = io_flags;
510 desc->pg_error = 0;
94ad1c80 511 desc->pg_lseg = NULL;
584aa810 512 desc->pg_dreq = NULL;
f6166384 513 desc->pg_layout_private = NULL;
d8a5ad75 514}
89d77c8f 515EXPORT_SYMBOL_GPL(nfs_pageio_init);
d8a5ad75 516
0eecb214
AS
517/**
518 * nfs_pgio_result - Basic pageio error handling
519 * @task: The task that ran
520 * @calldata: Pageio data to check
521 */
6f92fa45 522static void nfs_pgio_result(struct rpc_task *task, void *calldata)
0eecb214
AS
523{
524 struct nfs_pgio_data *data = calldata;
525 struct inode *inode = data->header->inode;
526
527 dprintk("NFS: %s: %5u, (status %d)\n", __func__,
528 task->tk_pid, task->tk_status);
529
530 if (data->header->rw_ops->rw_done(task, data, inode) != 0)
531 return;
532 if (task->tk_status < 0)
533 nfs_set_pgio_error(data->header, task->tk_status, data->args.offset);
534 else
535 data->header->rw_ops->rw_result(task, data);
536}
537
ef2c488c
AS
538/*
539 * Generate multiple small requests to read or write a single
540 * contiguous dirty on one page.
541 */
542static int nfs_pgio_multi(struct nfs_pageio_descriptor *desc,
543 struct nfs_pgio_header *hdr)
544{
545 struct nfs_page *req = hdr->req;
546 struct page *page = req->wb_page;
547 struct nfs_pgio_data *data;
548 size_t wsize = desc->pg_bsize, nbytes;
549 unsigned int offset;
550 int requests = 0;
551 struct nfs_commit_info cinfo;
552
553 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
554
555 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
556 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo) ||
557 desc->pg_count > wsize))
558 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
559
560 offset = 0;
561 nbytes = desc->pg_count;
562 do {
563 size_t len = min(nbytes, wsize);
564
565 data = nfs_pgio_data_alloc(hdr, 1);
566 if (!data)
567 return nfs_pgio_error(desc, hdr);
568 data->pages.pagevec[0] = page;
569 nfs_pgio_rpcsetup(data, len, offset, desc->pg_ioflags, &cinfo);
570 list_add(&data->list, &hdr->rpc_list);
571 requests++;
572 nbytes -= len;
573 offset += len;
574 } while (nbytes != 0);
575
576 nfs_list_remove_request(req);
577 nfs_list_add_request(req, &hdr->pages);
578 desc->pg_rpc_callops = &nfs_pgio_common_ops;
579 return 0;
580}
581
582/*
583 * Create an RPC task for the given read or write request and kick it.
584 * The page must have been locked by the caller.
585 *
586 * It may happen that the page we're passed is not marked dirty.
587 * This is the case if nfs_updatepage detects a conflicting request
588 * that has been written but not committed.
589 */
590static int nfs_pgio_one(struct nfs_pageio_descriptor *desc,
591 struct nfs_pgio_header *hdr)
592{
593 struct nfs_page *req;
594 struct page **pages;
595 struct nfs_pgio_data *data;
596 struct list_head *head = &desc->pg_list;
597 struct nfs_commit_info cinfo;
598
599 data = nfs_pgio_data_alloc(hdr, nfs_page_array_len(desc->pg_base,
600 desc->pg_count));
601 if (!data)
602 return nfs_pgio_error(desc, hdr);
603
604 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
605 pages = data->pages.pagevec;
606 while (!list_empty(head)) {
607 req = nfs_list_entry(head->next);
608 nfs_list_remove_request(req);
609 nfs_list_add_request(req, &hdr->pages);
610 *pages++ = req->wb_page;
611 }
612
613 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
614 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
615 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
616
617 /* Set up the argument struct */
618 nfs_pgio_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo);
619 list_add(&data->list, &hdr->rpc_list);
620 desc->pg_rpc_callops = &nfs_pgio_common_ops;
621 return 0;
622}
623
624int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
625 struct nfs_pgio_header *hdr)
626{
627 if (desc->pg_bsize < PAGE_CACHE_SIZE)
628 return nfs_pgio_multi(desc, hdr);
629 return nfs_pgio_one(desc, hdr);
630}
631EXPORT_SYMBOL_GPL(nfs_generic_pgio);
632
4109bb74
TM
633static bool nfs_match_open_context(const struct nfs_open_context *ctx1,
634 const struct nfs_open_context *ctx2)
635{
636 return ctx1->cred == ctx2->cred && ctx1->state == ctx2->state;
637}
638
639static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
640 const struct nfs_lock_context *l2)
641{
642 return l1->lockowner.l_owner == l2->lockowner.l_owner
643 && l1->lockowner.l_pid == l2->lockowner.l_pid;
644}
645
d8a5ad75
TM
646/**
647 * nfs_can_coalesce_requests - test two requests for compatibility
648 * @prev: pointer to nfs_page
649 * @req: pointer to nfs_page
650 *
651 * The nfs_page structures 'prev' and 'req' are compared to ensure that the
652 * page data area they describe is contiguous, and that their RPC
653 * credentials, NFSv4 open state, and lockowners are the same.
654 *
655 * Return 'true' if this is the case, else return 'false'.
656 */
18ad0a9f
BH
657static bool nfs_can_coalesce_requests(struct nfs_page *prev,
658 struct nfs_page *req,
659 struct nfs_pageio_descriptor *pgio)
d8a5ad75 660{
4109bb74 661 if (!nfs_match_open_context(req->wb_context, prev->wb_context))
18ad0a9f 662 return false;
4109bb74
TM
663 if (req->wb_context->dentry->d_inode->i_flock != NULL &&
664 !nfs_match_lock_context(req->wb_lock_context, prev->wb_lock_context))
18ad0a9f 665 return false;
d8a5ad75 666 if (req->wb_pgbase != 0)
18ad0a9f 667 return false;
d8a5ad75 668 if (prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
18ad0a9f 669 return false;
1825a0d0
FI
670 if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
671 return false;
1751c363 672 return pgio->pg_ops->pg_test(pgio, prev, req);
d8a5ad75
TM
673}
674
675/**
bcb71bba 676 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
d8a5ad75
TM
677 * @desc: destination io descriptor
678 * @req: request
679 *
680 * Returns true if the request 'req' was successfully coalesced into the
681 * existing list of pages 'desc'.
682 */
bcb71bba
TM
683static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
684 struct nfs_page *req)
d8a5ad75 685{
d8a5ad75
TM
686 if (desc->pg_count != 0) {
687 struct nfs_page *prev;
688
d8a5ad75 689 prev = nfs_list_entry(desc->pg_list.prev);
94ad1c80 690 if (!nfs_can_coalesce_requests(prev, req, desc))
d8a5ad75 691 return 0;
5b36c7dc 692 } else {
d8007d4d
TM
693 if (desc->pg_ops->pg_init)
694 desc->pg_ops->pg_init(desc, req);
d8a5ad75 695 desc->pg_base = req->wb_pgbase;
5b36c7dc 696 }
d8a5ad75
TM
697 nfs_list_remove_request(req);
698 nfs_list_add_request(req, &desc->pg_list);
5b36c7dc 699 desc->pg_count += req->wb_bytes;
d8a5ad75
TM
700 return 1;
701}
702
bcb71bba
TM
703/*
704 * Helper for nfs_pageio_add_request and nfs_pageio_complete
705 */
706static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
707{
708 if (!list_empty(&desc->pg_list)) {
1751c363 709 int error = desc->pg_ops->pg_doio(desc);
bcb71bba
TM
710 if (error < 0)
711 desc->pg_error = error;
712 else
713 desc->pg_bytes_written += desc->pg_count;
714 }
715 if (list_empty(&desc->pg_list)) {
716 desc->pg_count = 0;
717 desc->pg_base = 0;
718 }
719}
720
721/**
722 * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
723 * @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 */
d9156f9f 729static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
8b09bee3 730 struct nfs_page *req)
bcb71bba
TM
731{
732 while (!nfs_pageio_do_add_request(desc, req)) {
b31268ac 733 desc->pg_moreio = 1;
bcb71bba
TM
734 nfs_pageio_doio(desc);
735 if (desc->pg_error < 0)
736 return 0;
b31268ac 737 desc->pg_moreio = 0;
d9156f9f
TM
738 if (desc->pg_recoalesce)
739 return 0;
bcb71bba
TM
740 }
741 return 1;
742}
743
d9156f9f
TM
744static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
745{
746 LIST_HEAD(head);
747
748 do {
749 list_splice_init(&desc->pg_list, &head);
750 desc->pg_bytes_written -= desc->pg_count;
751 desc->pg_count = 0;
752 desc->pg_base = 0;
753 desc->pg_recoalesce = 0;
754
755 while (!list_empty(&head)) {
756 struct nfs_page *req;
757
758 req = list_first_entry(&head, struct nfs_page, wb_list);
759 nfs_list_remove_request(req);
760 if (__nfs_pageio_add_request(desc, req))
761 continue;
762 if (desc->pg_error < 0)
763 return 0;
764 break;
765 }
766 } while (desc->pg_recoalesce);
767 return 1;
768}
769
770int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
771 struct nfs_page *req)
772{
773 int ret;
774
775 do {
776 ret = __nfs_pageio_add_request(desc, req);
777 if (ret)
778 break;
779 if (desc->pg_error < 0)
780 break;
781 ret = nfs_do_recoalesce(desc);
782 } while (ret);
783 return ret;
784}
89d77c8f 785EXPORT_SYMBOL_GPL(nfs_pageio_add_request);
d9156f9f 786
bcb71bba
TM
787/**
788 * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
789 * @desc: pointer to io descriptor
790 */
791void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
792{
d9156f9f
TM
793 for (;;) {
794 nfs_pageio_doio(desc);
795 if (!desc->pg_recoalesce)
796 break;
797 if (!nfs_do_recoalesce(desc))
798 break;
799 }
bcb71bba 800}
89d77c8f 801EXPORT_SYMBOL_GPL(nfs_pageio_complete);
bcb71bba 802
7fe7f848
TM
803/**
804 * nfs_pageio_cond_complete - Conditional I/O completion
805 * @desc: pointer to io descriptor
806 * @index: page index
807 *
808 * It is important to ensure that processes don't try to take locks
809 * on non-contiguous ranges of pages as that might deadlock. This
810 * function should be called before attempting to wait on a locked
811 * nfs_page. It will complete the I/O if the page index 'index'
812 * is not contiguous with the existing list of pages in 'desc'.
813 */
814void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
815{
816 if (!list_empty(&desc->pg_list)) {
817 struct nfs_page *prev = nfs_list_entry(desc->pg_list.prev);
818 if (index != prev->wb_index + 1)
d9156f9f 819 nfs_pageio_complete(desc);
7fe7f848
TM
820 }
821}
822
f7b422b1 823int __init nfs_init_nfspagecache(void)
1da177e4
LT
824{
825 nfs_page_cachep = kmem_cache_create("nfs_page",
826 sizeof(struct nfs_page),
827 0, SLAB_HWCACHE_ALIGN,
20c2df83 828 NULL);
1da177e4
LT
829 if (nfs_page_cachep == NULL)
830 return -ENOMEM;
831
832 return 0;
833}
834
266bee88 835void nfs_destroy_nfspagecache(void)
1da177e4 836{
1a1d92c1 837 kmem_cache_destroy(nfs_page_cachep);
1da177e4
LT
838}
839
ef2c488c 840static const struct rpc_call_ops nfs_pgio_common_ops = {
6f92fa45
AS
841 .rpc_call_prepare = nfs_pgio_prepare,
842 .rpc_call_done = nfs_pgio_result,
843 .rpc_release = nfs_pgio_release,
844};