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