net, sunrpc: suppress allocation warning in rpc_malloc()
[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));
20633f04
WAA
400 WARN_ON_ONCE(test_bit(PG_WB_END, &req->wb_flags));
401 WARN_ON_ONCE(test_bit(PG_REMOVE, &req->wb_flags));
1da177e4 402
bb6fbc45 403 /* Release struct file and open context */
1da177e4 404 nfs_clear_request(req);
1da177e4
LT
405 nfs_page_free(req);
406}
407
c03b4024
TM
408void nfs_release_request(struct nfs_page *req)
409{
2bfc6e56 410 kref_put(&req->wb_kref, nfs_page_group_destroy);
c03b4024
TM
411}
412
9f557cd8
TM
413static int nfs_wait_bit_uninterruptible(void *word)
414{
415 io_schedule();
416 return 0;
417}
418
1da177e4
LT
419/**
420 * nfs_wait_on_request - Wait for a request to complete.
421 * @req: request to wait upon.
422 *
150030b7 423 * Interruptible by fatal signals only.
1da177e4
LT
424 * The user is responsible for holding a count on the request.
425 */
426int
427nfs_wait_on_request(struct nfs_page *req)
428{
9f557cd8
TM
429 return wait_on_bit(&req->wb_flags, PG_BUSY,
430 nfs_wait_bit_uninterruptible,
431 TASK_UNINTERRUPTIBLE);
1da177e4
LT
432}
433
b4fdac1a
WAA
434/*
435 * nfs_generic_pg_test - determine if requests can be coalesced
436 * @desc: pointer to descriptor
437 * @prev: previous request in desc, or NULL
438 * @req: this request
439 *
440 * Returns zero if @req can be coalesced into @desc, otherwise it returns
441 * the size of the request.
442 */
443size_t nfs_generic_pg_test(struct nfs_pageio_descriptor *desc,
444 struct nfs_page *prev, struct nfs_page *req)
5b36c7dc 445{
f0cb9ab8
WAA
446 if (desc->pg_count > desc->pg_bsize) {
447 /* should never happen */
448 WARN_ON_ONCE(1);
5b36c7dc 449 return 0;
f0cb9ab8 450 }
5b36c7dc 451
f0cb9ab8 452 return min(desc->pg_bsize - desc->pg_count, (size_t)req->wb_bytes);
5b36c7dc 453}
19345cb2 454EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
5b36c7dc 455
00bfa30a
AS
456static inline struct nfs_rw_header *NFS_RW_HEADER(struct nfs_pgio_header *hdr)
457{
458 return container_of(hdr, struct nfs_rw_header, header);
459}
460
4a0de55c
AS
461/**
462 * nfs_rw_header_alloc - Allocate a header for a read or write
463 * @ops: Read or write function vector
464 */
465struct nfs_rw_header *nfs_rw_header_alloc(const struct nfs_rw_ops *ops)
466{
467 struct nfs_rw_header *header = ops->rw_alloc_header();
468
469 if (header) {
470 struct nfs_pgio_header *hdr = &header->header;
471
472 INIT_LIST_HEAD(&hdr->pages);
4a0de55c
AS
473 spin_lock_init(&hdr->lock);
474 atomic_set(&hdr->refcnt, 0);
475 hdr->rw_ops = ops;
476 }
477 return header;
478}
479EXPORT_SYMBOL_GPL(nfs_rw_header_alloc);
480
481/*
482 * nfs_rw_header_free - Free a read or write header
483 * @hdr: The header to free
484 */
485void nfs_rw_header_free(struct nfs_pgio_header *hdr)
486{
487 hdr->rw_ops->rw_free_header(NFS_RW_HEADER(hdr));
488}
489EXPORT_SYMBOL_GPL(nfs_rw_header_free);
490
00bfa30a
AS
491/**
492 * nfs_pgio_data_alloc - Allocate pageio data
493 * @hdr: The header making a request
494 * @pagecount: Number of pages to create
495 */
ef2c488c
AS
496static struct nfs_pgio_data *nfs_pgio_data_alloc(struct nfs_pgio_header *hdr,
497 unsigned int pagecount)
00bfa30a
AS
498{
499 struct nfs_pgio_data *data, *prealloc;
500
501 prealloc = &NFS_RW_HEADER(hdr)->rpc_data;
502 if (prealloc->header == NULL)
503 data = prealloc;
504 else
505 data = kzalloc(sizeof(*data), GFP_KERNEL);
506 if (!data)
507 goto out;
508
509 if (nfs_pgarray_set(&data->pages, pagecount)) {
510 data->header = hdr;
511 atomic_inc(&hdr->refcnt);
512 } else {
513 if (data != prealloc)
514 kfree(data);
515 data = NULL;
516 }
517out:
518 return data;
519}
520
521/**
522 * nfs_pgio_data_release - Properly free pageio data
523 * @data: The data to release
524 */
525void nfs_pgio_data_release(struct nfs_pgio_data *data)
526{
527 struct nfs_pgio_header *hdr = data->header;
528 struct nfs_rw_header *pageio_header = NFS_RW_HEADER(hdr);
529
530 put_nfs_open_context(data->args.context);
531 if (data->pages.pagevec != data->pages.page_array)
532 kfree(data->pages.pagevec);
533 if (data == &pageio_header->rpc_data) {
534 data->header = NULL;
535 data = NULL;
536 }
537 if (atomic_dec_and_test(&hdr->refcnt))
538 hdr->completion_ops->completion(hdr);
539 /* Note: we only free the rpc_task after callbacks are done.
540 * See the comment in rpc_free_task() for why
541 */
542 kfree(data);
543}
544EXPORT_SYMBOL_GPL(nfs_pgio_data_release);
545
ce59515c
AS
546/**
547 * nfs_pgio_rpcsetup - Set up arguments for a pageio call
548 * @data: The pageio data
549 * @count: Number of bytes to read
550 * @offset: Initial offset
551 * @how: How to commit data (writes only)
552 * @cinfo: Commit information for the call (writes only)
553 */
ef2c488c 554static void nfs_pgio_rpcsetup(struct nfs_pgio_data *data,
ce59515c
AS
555 unsigned int count, unsigned int offset,
556 int how, struct nfs_commit_info *cinfo)
557{
558 struct nfs_page *req = data->header->req;
559
560 /* Set up the RPC argument and reply structs
561 * NB: take care not to mess about with data->commit et al. */
562
563 data->args.fh = NFS_FH(data->header->inode);
564 data->args.offset = req_offset(req) + offset;
565 /* pnfs_set_layoutcommit needs this */
566 data->mds_offset = data->args.offset;
567 data->args.pgbase = req->wb_pgbase + offset;
568 data->args.pages = data->pages.pagevec;
569 data->args.count = count;
570 data->args.context = get_nfs_open_context(req->wb_context);
571 data->args.lock_context = req->wb_lock_context;
572 data->args.stable = NFS_UNSTABLE;
573 switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
574 case 0:
575 break;
576 case FLUSH_COND_STABLE:
577 if (nfs_reqs_to_commit(cinfo))
578 break;
579 default:
580 data->args.stable = NFS_FILE_SYNC;
581 }
582
583 data->res.fattr = &data->fattr;
584 data->res.count = count;
585 data->res.eof = 0;
586 data->res.verf = &data->verf;
587 nfs_fattr_init(&data->fattr);
588}
589
a4cdda59
AS
590/**
591 * nfs_pgio_prepare - Prepare pageio data to go over the wire
592 * @task: The current task
593 * @calldata: pageio data to prepare
594 */
6f92fa45 595static void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
a4cdda59
AS
596{
597 struct nfs_pgio_data *data = calldata;
598 int err;
599 err = NFS_PROTO(data->header->inode)->pgio_rpc_prepare(task, data);
600 if (err)
601 rpc_exit(task, err);
602}
603
1ed26f33
AS
604int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_data *data,
605 const struct rpc_call_ops *call_ops, int how, int flags)
606{
607 struct rpc_task *task;
608 struct rpc_message msg = {
609 .rpc_argp = &data->args,
610 .rpc_resp = &data->res,
611 .rpc_cred = data->header->cred,
612 };
613 struct rpc_task_setup task_setup_data = {
614 .rpc_client = clnt,
615 .task = &data->task,
616 .rpc_message = &msg,
617 .callback_ops = call_ops,
618 .callback_data = data,
619 .workqueue = nfsiod_workqueue,
620 .flags = RPC_TASK_ASYNC | flags,
621 };
622 int ret = 0;
623
624 data->header->rw_ops->rw_initiate(data, &msg, &task_setup_data, how);
625
626 dprintk("NFS: %5u initiated pgio call "
627 "(req %s/%llu, %u bytes @ offset %llu)\n",
628 data->task.tk_pid,
629 data->header->inode->i_sb->s_id,
630 (unsigned long long)NFS_FILEID(data->header->inode),
631 data->args.count,
632 (unsigned long long)data->args.offset);
633
634 task = rpc_run_task(&task_setup_data);
635 if (IS_ERR(task)) {
636 ret = PTR_ERR(task);
637 goto out;
638 }
639 if (how & FLUSH_SYNC) {
640 ret = rpc_wait_for_completion_task(task);
641 if (ret == 0)
642 ret = task->tk_status;
643 }
644 rpc_put_task(task);
645out:
646 return ret;
647}
648EXPORT_SYMBOL_GPL(nfs_initiate_pgio);
649
844c9e69
AS
650/**
651 * nfs_pgio_error - Clean up from a pageio error
652 * @desc: IO descriptor
653 * @hdr: pageio header
654 */
ef2c488c 655static int nfs_pgio_error(struct nfs_pageio_descriptor *desc,
844c9e69
AS
656 struct nfs_pgio_header *hdr)
657{
844c9e69 658 set_bit(NFS_IOHDR_REDO, &hdr->flags);
7f714720
WAA
659 nfs_pgio_data_release(hdr->data);
660 hdr->data = NULL;
844c9e69
AS
661 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
662 return -ENOMEM;
663}
664
a4cdda59
AS
665/**
666 * nfs_pgio_release - Release pageio data
667 * @calldata: The pageio data to release
668 */
6f92fa45 669static void nfs_pgio_release(void *calldata)
a4cdda59
AS
670{
671 struct nfs_pgio_data *data = calldata;
672 if (data->header->rw_ops->rw_release)
673 data->header->rw_ops->rw_release(data);
674 nfs_pgio_data_release(data);
675}
676
1da177e4 677/**
d8a5ad75
TM
678 * nfs_pageio_init - initialise a page io descriptor
679 * @desc: pointer to descriptor
bcb71bba
TM
680 * @inode: pointer to inode
681 * @doio: pointer to io function
682 * @bsize: io block size
683 * @io_flags: extra parameters for the io function
d8a5ad75 684 */
bcb71bba
TM
685void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
686 struct inode *inode,
1751c363 687 const struct nfs_pageio_ops *pg_ops,
061ae2ed 688 const struct nfs_pgio_completion_ops *compl_ops,
4a0de55c 689 const struct nfs_rw_ops *rw_ops,
84dde76c 690 size_t bsize,
bcb71bba 691 int io_flags)
d8a5ad75
TM
692{
693 INIT_LIST_HEAD(&desc->pg_list);
bcb71bba 694 desc->pg_bytes_written = 0;
d8a5ad75
TM
695 desc->pg_count = 0;
696 desc->pg_bsize = bsize;
697 desc->pg_base = 0;
b31268ac 698 desc->pg_moreio = 0;
d9156f9f 699 desc->pg_recoalesce = 0;
bcb71bba 700 desc->pg_inode = inode;
1751c363 701 desc->pg_ops = pg_ops;
061ae2ed 702 desc->pg_completion_ops = compl_ops;
4a0de55c 703 desc->pg_rw_ops = rw_ops;
bcb71bba
TM
704 desc->pg_ioflags = io_flags;
705 desc->pg_error = 0;
94ad1c80 706 desc->pg_lseg = NULL;
584aa810 707 desc->pg_dreq = NULL;
f6166384 708 desc->pg_layout_private = NULL;
d8a5ad75 709}
89d77c8f 710EXPORT_SYMBOL_GPL(nfs_pageio_init);
d8a5ad75 711
0eecb214
AS
712/**
713 * nfs_pgio_result - Basic pageio error handling
714 * @task: The task that ran
715 * @calldata: Pageio data to check
716 */
6f92fa45 717static void nfs_pgio_result(struct rpc_task *task, void *calldata)
0eecb214
AS
718{
719 struct nfs_pgio_data *data = calldata;
720 struct inode *inode = data->header->inode;
721
722 dprintk("NFS: %s: %5u, (status %d)\n", __func__,
723 task->tk_pid, task->tk_status);
724
725 if (data->header->rw_ops->rw_done(task, data, inode) != 0)
726 return;
727 if (task->tk_status < 0)
728 nfs_set_pgio_error(data->header, task->tk_status, data->args.offset);
729 else
730 data->header->rw_ops->rw_result(task, data);
731}
732
ef2c488c
AS
733/*
734 * Create an RPC task for the given read or write request and kick it.
735 * The page must have been locked by the caller.
736 *
737 * It may happen that the page we're passed is not marked dirty.
738 * This is the case if nfs_updatepage detects a conflicting request
739 * that has been written but not committed.
740 */
f0cb9ab8
WAA
741int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
742 struct nfs_pgio_header *hdr)
ef2c488c
AS
743{
744 struct nfs_page *req;
745 struct page **pages;
746 struct nfs_pgio_data *data;
747 struct list_head *head = &desc->pg_list;
748 struct nfs_commit_info cinfo;
749
750 data = nfs_pgio_data_alloc(hdr, nfs_page_array_len(desc->pg_base,
751 desc->pg_count));
752 if (!data)
753 return nfs_pgio_error(desc, hdr);
754
755 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
756 pages = data->pages.pagevec;
757 while (!list_empty(head)) {
758 req = nfs_list_entry(head->next);
759 nfs_list_remove_request(req);
760 nfs_list_add_request(req, &hdr->pages);
761 *pages++ = req->wb_page;
762 }
763
764 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
765 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
766 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
767
768 /* Set up the argument struct */
769 nfs_pgio_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo);
7f714720 770 hdr->data = data;
ef2c488c
AS
771 desc->pg_rpc_callops = &nfs_pgio_common_ops;
772 return 0;
773}
f0cb9ab8 774EXPORT_SYMBOL_GPL(nfs_generic_pgio);
ef2c488c 775
41d8d5b7 776static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
cf485fcd
AS
777{
778 struct nfs_rw_header *rw_hdr;
779 struct nfs_pgio_header *hdr;
780 int ret;
781
782 rw_hdr = nfs_rw_header_alloc(desc->pg_rw_ops);
783 if (!rw_hdr) {
784 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
785 return -ENOMEM;
786 }
787 hdr = &rw_hdr->header;
788 nfs_pgheader_init(desc, hdr, nfs_rw_header_free);
789 atomic_inc(&hdr->refcnt);
790 ret = nfs_generic_pgio(desc, hdr);
791 if (ret == 0)
7f714720
WAA
792 ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode),
793 hdr->data, desc->pg_rpc_callops,
794 desc->pg_ioflags, 0);
cf485fcd
AS
795 if (atomic_dec_and_test(&hdr->refcnt))
796 hdr->completion_ops->completion(hdr);
797 return ret;
798}
799
4109bb74
TM
800static bool nfs_match_open_context(const struct nfs_open_context *ctx1,
801 const struct nfs_open_context *ctx2)
802{
803 return ctx1->cred == ctx2->cred && ctx1->state == ctx2->state;
804}
805
806static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
807 const struct nfs_lock_context *l2)
808{
809 return l1->lockowner.l_owner == l2->lockowner.l_owner
810 && l1->lockowner.l_pid == l2->lockowner.l_pid;
811}
812
d8a5ad75
TM
813/**
814 * nfs_can_coalesce_requests - test two requests for compatibility
815 * @prev: pointer to nfs_page
816 * @req: pointer to nfs_page
817 *
818 * The nfs_page structures 'prev' and 'req' are compared to ensure that the
819 * page data area they describe is contiguous, and that their RPC
820 * credentials, NFSv4 open state, and lockowners are the same.
821 *
822 * Return 'true' if this is the case, else return 'false'.
823 */
18ad0a9f
BH
824static bool nfs_can_coalesce_requests(struct nfs_page *prev,
825 struct nfs_page *req,
826 struct nfs_pageio_descriptor *pgio)
d8a5ad75 827{
b4fdac1a
WAA
828 size_t size;
829
ab75e417
WAA
830 if (prev) {
831 if (!nfs_match_open_context(req->wb_context, prev->wb_context))
832 return false;
833 if (req->wb_context->dentry->d_inode->i_flock != NULL &&
834 !nfs_match_lock_context(req->wb_lock_context,
835 prev->wb_lock_context))
836 return false;
ab75e417
WAA
837 if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
838 return false;
839 }
b4fdac1a 840 size = pgio->pg_ops->pg_test(pgio, prev, req);
f0cb9ab8
WAA
841 WARN_ON_ONCE(size > req->wb_bytes);
842 if (size && size < req->wb_bytes)
843 req->wb_bytes = size;
b4fdac1a 844 return size > 0;
d8a5ad75
TM
845}
846
847/**
bcb71bba 848 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
d8a5ad75
TM
849 * @desc: destination io descriptor
850 * @req: request
851 *
852 * Returns true if the request 'req' was successfully coalesced into the
853 * existing list of pages 'desc'.
854 */
bcb71bba
TM
855static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
856 struct nfs_page *req)
d8a5ad75 857{
ab75e417 858 struct nfs_page *prev = NULL;
d8a5ad75 859 if (desc->pg_count != 0) {
d8a5ad75 860 prev = nfs_list_entry(desc->pg_list.prev);
5b36c7dc 861 } else {
d8007d4d
TM
862 if (desc->pg_ops->pg_init)
863 desc->pg_ops->pg_init(desc, req);
d8a5ad75 864 desc->pg_base = req->wb_pgbase;
5b36c7dc 865 }
ab75e417
WAA
866 if (!nfs_can_coalesce_requests(prev, req, desc))
867 return 0;
d8a5ad75
TM
868 nfs_list_remove_request(req);
869 nfs_list_add_request(req, &desc->pg_list);
5b36c7dc 870 desc->pg_count += req->wb_bytes;
d8a5ad75
TM
871 return 1;
872}
873
bcb71bba
TM
874/*
875 * Helper for nfs_pageio_add_request and nfs_pageio_complete
876 */
877static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
878{
879 if (!list_empty(&desc->pg_list)) {
1751c363 880 int error = desc->pg_ops->pg_doio(desc);
bcb71bba
TM
881 if (error < 0)
882 desc->pg_error = error;
883 else
884 desc->pg_bytes_written += desc->pg_count;
885 }
886 if (list_empty(&desc->pg_list)) {
887 desc->pg_count = 0;
888 desc->pg_base = 0;
889 }
890}
891
892/**
893 * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
894 * @desc: destination io descriptor
895 * @req: request
896 *
2bfc6e56
WAA
897 * This may split a request into subrequests which are all part of the
898 * same page group.
899 *
bcb71bba
TM
900 * Returns true if the request 'req' was successfully coalesced into the
901 * existing list of pages 'desc'.
902 */
d9156f9f 903static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
8b09bee3 904 struct nfs_page *req)
bcb71bba 905{
2bfc6e56
WAA
906 struct nfs_page *subreq;
907 unsigned int bytes_left = 0;
908 unsigned int offset, pgbase;
909
910 nfs_page_group_lock(req);
911
912 subreq = req;
913 bytes_left = subreq->wb_bytes;
914 offset = subreq->wb_offset;
915 pgbase = subreq->wb_pgbase;
916
917 do {
918 if (!nfs_pageio_do_add_request(desc, subreq)) {
919 /* make sure pg_test call(s) did nothing */
920 WARN_ON_ONCE(subreq->wb_bytes != bytes_left);
921 WARN_ON_ONCE(subreq->wb_offset != offset);
922 WARN_ON_ONCE(subreq->wb_pgbase != pgbase);
923
924 nfs_page_group_unlock(req);
925 desc->pg_moreio = 1;
926 nfs_pageio_doio(desc);
927 if (desc->pg_error < 0)
928 return 0;
929 desc->pg_moreio = 0;
930 if (desc->pg_recoalesce)
931 return 0;
932 /* retry add_request for this subreq */
933 nfs_page_group_lock(req);
934 continue;
935 }
936
937 /* check for buggy pg_test call(s) */
938 WARN_ON_ONCE(subreq->wb_bytes + subreq->wb_pgbase > PAGE_SIZE);
939 WARN_ON_ONCE(subreq->wb_bytes > bytes_left);
940 WARN_ON_ONCE(subreq->wb_bytes == 0);
941
942 bytes_left -= subreq->wb_bytes;
943 offset += subreq->wb_bytes;
944 pgbase += subreq->wb_bytes;
945
946 if (bytes_left) {
947 subreq = nfs_create_request(req->wb_context,
948 req->wb_page,
949 subreq, pgbase, bytes_left);
950 nfs_lock_request(subreq);
951 subreq->wb_offset = offset;
952 subreq->wb_index = req->wb_index;
953 }
954 } while (bytes_left > 0);
955
956 nfs_page_group_unlock(req);
bcb71bba
TM
957 return 1;
958}
959
d9156f9f
TM
960static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
961{
962 LIST_HEAD(head);
963
964 do {
965 list_splice_init(&desc->pg_list, &head);
966 desc->pg_bytes_written -= desc->pg_count;
967 desc->pg_count = 0;
968 desc->pg_base = 0;
969 desc->pg_recoalesce = 0;
970
971 while (!list_empty(&head)) {
972 struct nfs_page *req;
973
974 req = list_first_entry(&head, struct nfs_page, wb_list);
975 nfs_list_remove_request(req);
976 if (__nfs_pageio_add_request(desc, req))
977 continue;
978 if (desc->pg_error < 0)
979 return 0;
980 break;
981 }
982 } while (desc->pg_recoalesce);
983 return 1;
984}
985
986int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
987 struct nfs_page *req)
988{
989 int ret;
990
991 do {
992 ret = __nfs_pageio_add_request(desc, req);
993 if (ret)
994 break;
995 if (desc->pg_error < 0)
996 break;
997 ret = nfs_do_recoalesce(desc);
998 } while (ret);
999 return ret;
1000}
89d77c8f 1001EXPORT_SYMBOL_GPL(nfs_pageio_add_request);
d9156f9f 1002
bcb71bba
TM
1003/**
1004 * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
1005 * @desc: pointer to io descriptor
1006 */
1007void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
1008{
d9156f9f
TM
1009 for (;;) {
1010 nfs_pageio_doio(desc);
1011 if (!desc->pg_recoalesce)
1012 break;
1013 if (!nfs_do_recoalesce(desc))
1014 break;
1015 }
bcb71bba 1016}
89d77c8f 1017EXPORT_SYMBOL_GPL(nfs_pageio_complete);
bcb71bba 1018
7fe7f848
TM
1019/**
1020 * nfs_pageio_cond_complete - Conditional I/O completion
1021 * @desc: pointer to io descriptor
1022 * @index: page index
1023 *
1024 * It is important to ensure that processes don't try to take locks
1025 * on non-contiguous ranges of pages as that might deadlock. This
1026 * function should be called before attempting to wait on a locked
1027 * nfs_page. It will complete the I/O if the page index 'index'
1028 * is not contiguous with the existing list of pages in 'desc'.
1029 */
1030void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
1031{
1032 if (!list_empty(&desc->pg_list)) {
1033 struct nfs_page *prev = nfs_list_entry(desc->pg_list.prev);
1034 if (index != prev->wb_index + 1)
d9156f9f 1035 nfs_pageio_complete(desc);
7fe7f848
TM
1036 }
1037}
1038
f7b422b1 1039int __init nfs_init_nfspagecache(void)
1da177e4
LT
1040{
1041 nfs_page_cachep = kmem_cache_create("nfs_page",
1042 sizeof(struct nfs_page),
1043 0, SLAB_HWCACHE_ALIGN,
20c2df83 1044 NULL);
1da177e4
LT
1045 if (nfs_page_cachep == NULL)
1046 return -ENOMEM;
1047
1048 return 0;
1049}
1050
266bee88 1051void nfs_destroy_nfspagecache(void)
1da177e4 1052{
1a1d92c1 1053 kmem_cache_destroy(nfs_page_cachep);
1da177e4
LT
1054}
1055
ef2c488c 1056static const struct rpc_call_ops nfs_pgio_common_ops = {
6f92fa45
AS
1057 .rpc_call_prepare = nfs_pgio_prepare,
1058 .rpc_call_done = nfs_pgio_result,
1059 .rpc_release = nfs_pgio_release,
1060};
41d8d5b7
AS
1061
1062const struct nfs_pageio_ops nfs_pgio_rw_ops = {
1063 .pg_test = nfs_generic_pg_test,
1064 .pg_doio = nfs_generic_pg_pgios,
1065};