GNU Linux-libre 4.14.290-gnu1
[releases.git] / fs / nfs / pagelist.c
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
12 #include <linux/slab.h>
13 #include <linux/file.h>
14 #include <linux/sched.h>
15 #include <linux/sunrpc/clnt.h>
16 #include <linux/nfs.h>
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>
22 #include <linux/export.h>
23
24 #include "internal.h"
25 #include "pnfs.h"
26
27 #define NFSDBG_FACILITY         NFSDBG_PAGECACHE
28
29 static struct kmem_cache *nfs_page_cachep;
30 static const struct rpc_call_ops nfs_pgio_common_ops;
31
32 struct nfs_pgio_mirror *
33 nfs_pgio_current_mirror(struct nfs_pageio_descriptor *desc)
34 {
35         return nfs_pgio_has_mirroring(desc) ?
36                 &desc->pg_mirrors[desc->pg_mirror_idx] :
37                 &desc->pg_mirrors[0];
38 }
39 EXPORT_SYMBOL_GPL(nfs_pgio_current_mirror);
40
41 void nfs_pgheader_init(struct nfs_pageio_descriptor *desc,
42                        struct nfs_pgio_header *hdr,
43                        void (*release)(struct nfs_pgio_header *hdr))
44 {
45         struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
46
47
48         hdr->req = nfs_list_entry(mirror->pg_list.next);
49         hdr->inode = desc->pg_inode;
50         hdr->cred = hdr->req->wb_context->cred;
51         hdr->io_start = req_offset(hdr->req);
52         hdr->good_bytes = mirror->pg_count;
53         hdr->io_completion = desc->pg_io_completion;
54         hdr->dreq = desc->pg_dreq;
55         hdr->release = release;
56         hdr->completion_ops = desc->pg_completion_ops;
57         if (hdr->completion_ops->init_hdr)
58                 hdr->completion_ops->init_hdr(hdr);
59
60         hdr->pgio_mirror_idx = desc->pg_mirror_idx;
61 }
62 EXPORT_SYMBOL_GPL(nfs_pgheader_init);
63
64 void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos)
65 {
66         spin_lock(&hdr->lock);
67         if (!test_and_set_bit(NFS_IOHDR_ERROR, &hdr->flags)
68             || pos < hdr->io_start + hdr->good_bytes) {
69                 clear_bit(NFS_IOHDR_EOF, &hdr->flags);
70                 hdr->good_bytes = pos - hdr->io_start;
71                 hdr->error = error;
72         }
73         spin_unlock(&hdr->lock);
74 }
75
76 static inline struct nfs_page *
77 nfs_page_alloc(void)
78 {
79         struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_NOIO);
80         if (p)
81                 INIT_LIST_HEAD(&p->wb_list);
82         return p;
83 }
84
85 static inline void
86 nfs_page_free(struct nfs_page *p)
87 {
88         kmem_cache_free(nfs_page_cachep, p);
89 }
90
91 /**
92  * nfs_iocounter_wait - wait for i/o to complete
93  * @l_ctx: nfs_lock_context with io_counter to use
94  *
95  * returns -ERESTARTSYS if interrupted by a fatal signal.
96  * Otherwise returns 0 once the io_count hits 0.
97  */
98 int
99 nfs_iocounter_wait(struct nfs_lock_context *l_ctx)
100 {
101         return wait_on_atomic_t(&l_ctx->io_count, nfs_wait_atomic_killable,
102                         TASK_KILLABLE);
103 }
104
105 /**
106  * nfs_async_iocounter_wait - wait on a rpc_waitqueue for I/O
107  * to complete
108  * @task: the rpc_task that should wait
109  * @l_ctx: nfs_lock_context with io_counter to check
110  *
111  * Returns true if there is outstanding I/O to wait on and the
112  * task has been put to sleep.
113  */
114 bool
115 nfs_async_iocounter_wait(struct rpc_task *task, struct nfs_lock_context *l_ctx)
116 {
117         struct inode *inode = d_inode(l_ctx->open_context->dentry);
118         bool ret = false;
119
120         if (atomic_read(&l_ctx->io_count) > 0) {
121                 rpc_sleep_on(&NFS_SERVER(inode)->uoc_rpcwaitq, task, NULL);
122                 ret = true;
123         }
124
125         if (atomic_read(&l_ctx->io_count) == 0) {
126                 rpc_wake_up_queued_task(&NFS_SERVER(inode)->uoc_rpcwaitq, task);
127                 ret = false;
128         }
129
130         return ret;
131 }
132 EXPORT_SYMBOL_GPL(nfs_async_iocounter_wait);
133
134 /*
135  * nfs_page_set_headlock - set the request PG_HEADLOCK
136  * @req: request that is to be locked
137  *
138  * this lock must be held when modifying req->wb_head
139  *
140  * return 0 on success, < 0 on error
141  */
142 int
143 nfs_page_set_headlock(struct nfs_page *req)
144 {
145         if (!test_and_set_bit(PG_HEADLOCK, &req->wb_flags))
146                 return 0;
147
148         set_bit(PG_CONTENDED1, &req->wb_flags);
149         smp_mb__after_atomic();
150         return wait_on_bit_lock(&req->wb_flags, PG_HEADLOCK,
151                                 TASK_UNINTERRUPTIBLE);
152 }
153
154 /*
155  * nfs_page_clear_headlock - clear the request PG_HEADLOCK
156  * @req: request that is to be locked
157  */
158 void
159 nfs_page_clear_headlock(struct nfs_page *req)
160 {
161         smp_mb__before_atomic();
162         clear_bit(PG_HEADLOCK, &req->wb_flags);
163         smp_mb__after_atomic();
164         if (!test_bit(PG_CONTENDED1, &req->wb_flags))
165                 return;
166         wake_up_bit(&req->wb_flags, PG_HEADLOCK);
167 }
168
169 /*
170  * nfs_page_group_lock - lock the head of the page group
171  * @req: request in group that is to be locked
172  *
173  * this lock must be held when traversing or modifying the page
174  * group list
175  *
176  * return 0 on success, < 0 on error
177  */
178 int
179 nfs_page_group_lock(struct nfs_page *req)
180 {
181         int ret;
182
183         ret = nfs_page_set_headlock(req);
184         if (ret || req->wb_head == req)
185                 return ret;
186         return nfs_page_set_headlock(req->wb_head);
187 }
188
189 /*
190  * nfs_page_group_unlock - unlock the head of the page group
191  * @req: request in group that is to be unlocked
192  */
193 void
194 nfs_page_group_unlock(struct nfs_page *req)
195 {
196         if (req != req->wb_head)
197                 nfs_page_clear_headlock(req->wb_head);
198         nfs_page_clear_headlock(req);
199 }
200
201 /*
202  * nfs_page_group_sync_on_bit_locked
203  *
204  * must be called with page group lock held
205  */
206 static bool
207 nfs_page_group_sync_on_bit_locked(struct nfs_page *req, unsigned int bit)
208 {
209         struct nfs_page *head = req->wb_head;
210         struct nfs_page *tmp;
211
212         WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_flags));
213         WARN_ON_ONCE(test_and_set_bit(bit, &req->wb_flags));
214
215         tmp = req->wb_this_page;
216         while (tmp != req) {
217                 if (!test_bit(bit, &tmp->wb_flags))
218                         return false;
219                 tmp = tmp->wb_this_page;
220         }
221
222         /* true! reset all bits */
223         tmp = req;
224         do {
225                 clear_bit(bit, &tmp->wb_flags);
226                 tmp = tmp->wb_this_page;
227         } while (tmp != req);
228
229         return true;
230 }
231
232 /*
233  * nfs_page_group_sync_on_bit - set bit on current request, but only
234  *   return true if the bit is set for all requests in page group
235  * @req - request in page group
236  * @bit - PG_* bit that is used to sync page group
237  */
238 bool nfs_page_group_sync_on_bit(struct nfs_page *req, unsigned int bit)
239 {
240         bool ret;
241
242         nfs_page_group_lock(req);
243         ret = nfs_page_group_sync_on_bit_locked(req, bit);
244         nfs_page_group_unlock(req);
245
246         return ret;
247 }
248
249 /*
250  * nfs_page_group_init - Initialize the page group linkage for @req
251  * @req - a new nfs request
252  * @prev - the previous request in page group, or NULL if @req is the first
253  *         or only request in the group (the head).
254  */
255 static inline void
256 nfs_page_group_init(struct nfs_page *req, struct nfs_page *prev)
257 {
258         struct inode *inode;
259         WARN_ON_ONCE(prev == req);
260
261         if (!prev) {
262                 /* a head request */
263                 req->wb_head = req;
264                 req->wb_this_page = req;
265         } else {
266                 /* a subrequest */
267                 WARN_ON_ONCE(prev->wb_this_page != prev->wb_head);
268                 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &prev->wb_head->wb_flags));
269                 req->wb_head = prev->wb_head;
270                 req->wb_this_page = prev->wb_this_page;
271                 prev->wb_this_page = req;
272
273                 /* All subrequests take a ref on the head request until
274                  * nfs_page_group_destroy is called */
275                 kref_get(&req->wb_head->wb_kref);
276
277                 /* grab extra ref and bump the request count if head request
278                  * has extra ref from the write/commit path to handle handoff
279                  * between write and commit lists. */
280                 if (test_bit(PG_INODE_REF, &prev->wb_head->wb_flags)) {
281                         inode = page_file_mapping(req->wb_page)->host;
282                         set_bit(PG_INODE_REF, &req->wb_flags);
283                         kref_get(&req->wb_kref);
284                         atomic_long_inc(&NFS_I(inode)->nrequests);
285                 }
286         }
287 }
288
289 /*
290  * nfs_page_group_destroy - sync the destruction of page groups
291  * @req - request that no longer needs the page group
292  *
293  * releases the page group reference from each member once all
294  * members have called this function.
295  */
296 static void
297 nfs_page_group_destroy(struct kref *kref)
298 {
299         struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
300         struct nfs_page *head = req->wb_head;
301         struct nfs_page *tmp, *next;
302
303         if (!nfs_page_group_sync_on_bit(req, PG_TEARDOWN))
304                 goto out;
305
306         tmp = req;
307         do {
308                 next = tmp->wb_this_page;
309                 /* unlink and free */
310                 tmp->wb_this_page = tmp;
311                 tmp->wb_head = tmp;
312                 nfs_free_request(tmp);
313                 tmp = next;
314         } while (tmp != req);
315 out:
316         /* subrequests must release the ref on the head request */
317         if (head != req)
318                 nfs_release_request(head);
319 }
320
321 /**
322  * nfs_create_request - Create an NFS read/write request.
323  * @ctx: open context to use
324  * @page: page to write
325  * @last: last nfs request created for this page group or NULL if head
326  * @offset: starting offset within the page for the write
327  * @count: number of bytes to read/write
328  *
329  * The page must be locked by the caller. This makes sure we never
330  * create two different requests for the same page.
331  * User should ensure it is safe to sleep in this function.
332  */
333 struct nfs_page *
334 nfs_create_request(struct nfs_open_context *ctx, struct page *page,
335                    struct nfs_page *last, unsigned int offset,
336                    unsigned int count)
337 {
338         struct nfs_page         *req;
339         struct nfs_lock_context *l_ctx;
340
341         if (test_bit(NFS_CONTEXT_BAD, &ctx->flags))
342                 return ERR_PTR(-EBADF);
343         /* try to allocate the request struct */
344         req = nfs_page_alloc();
345         if (req == NULL)
346                 return ERR_PTR(-ENOMEM);
347
348         /* get lock context early so we can deal with alloc failures */
349         l_ctx = nfs_get_lock_context(ctx);
350         if (IS_ERR(l_ctx)) {
351                 nfs_page_free(req);
352                 return ERR_CAST(l_ctx);
353         }
354         req->wb_lock_context = l_ctx;
355         atomic_inc(&l_ctx->io_count);
356
357         /* Initialize the request struct. Initially, we assume a
358          * long write-back delay. This will be adjusted in
359          * update_nfs_request below if the region is not locked. */
360         req->wb_page    = page;
361         if (page) {
362                 req->wb_index = page_index(page);
363                 get_page(page);
364         }
365         req->wb_offset  = offset;
366         req->wb_pgbase  = offset;
367         req->wb_bytes   = count;
368         req->wb_context = get_nfs_open_context(ctx);
369         kref_init(&req->wb_kref);
370         nfs_page_group_init(req, last);
371         return req;
372 }
373
374 /**
375  * nfs_unlock_request - Unlock request and wake up sleepers.
376  * @req:
377  */
378 void nfs_unlock_request(struct nfs_page *req)
379 {
380         if (!NFS_WBACK_BUSY(req)) {
381                 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
382                 BUG();
383         }
384         smp_mb__before_atomic();
385         clear_bit(PG_BUSY, &req->wb_flags);
386         smp_mb__after_atomic();
387         if (!test_bit(PG_CONTENDED2, &req->wb_flags))
388                 return;
389         wake_up_bit(&req->wb_flags, PG_BUSY);
390 }
391
392 /**
393  * nfs_unlock_and_release_request - Unlock request and release the nfs_page
394  * @req:
395  */
396 void nfs_unlock_and_release_request(struct nfs_page *req)
397 {
398         nfs_unlock_request(req);
399         nfs_release_request(req);
400 }
401
402 /*
403  * nfs_clear_request - Free up all resources allocated to the request
404  * @req:
405  *
406  * Release page and open context resources associated with a read/write
407  * request after it has completed.
408  */
409 static void nfs_clear_request(struct nfs_page *req)
410 {
411         struct page *page = req->wb_page;
412         struct nfs_open_context *ctx = req->wb_context;
413         struct nfs_lock_context *l_ctx = req->wb_lock_context;
414
415         if (page != NULL) {
416                 put_page(page);
417                 req->wb_page = NULL;
418         }
419         if (l_ctx != NULL) {
420                 if (atomic_dec_and_test(&l_ctx->io_count)) {
421                         wake_up_atomic_t(&l_ctx->io_count);
422                         if (test_bit(NFS_CONTEXT_UNLOCK, &ctx->flags))
423                                 rpc_wake_up(&NFS_SERVER(d_inode(ctx->dentry))->uoc_rpcwaitq);
424                 }
425                 nfs_put_lock_context(l_ctx);
426                 req->wb_lock_context = NULL;
427         }
428         if (ctx != NULL) {
429                 put_nfs_open_context(ctx);
430                 req->wb_context = NULL;
431         }
432 }
433
434 /**
435  * nfs_release_request - Release the count on an NFS read/write request
436  * @req: request to release
437  *
438  * Note: Should never be called with the spinlock held!
439  */
440 void nfs_free_request(struct nfs_page *req)
441 {
442         WARN_ON_ONCE(req->wb_this_page != req);
443
444         /* extra debug: make sure no sync bits are still set */
445         WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
446         WARN_ON_ONCE(test_bit(PG_UNLOCKPAGE, &req->wb_flags));
447         WARN_ON_ONCE(test_bit(PG_UPTODATE, &req->wb_flags));
448         WARN_ON_ONCE(test_bit(PG_WB_END, &req->wb_flags));
449         WARN_ON_ONCE(test_bit(PG_REMOVE, &req->wb_flags));
450
451         /* Release struct file and open context */
452         nfs_clear_request(req);
453         nfs_page_free(req);
454 }
455
456 void nfs_release_request(struct nfs_page *req)
457 {
458         kref_put(&req->wb_kref, nfs_page_group_destroy);
459 }
460 EXPORT_SYMBOL_GPL(nfs_release_request);
461
462 /**
463  * nfs_wait_on_request - Wait for a request to complete.
464  * @req: request to wait upon.
465  *
466  * Interruptible by fatal signals only.
467  * The user is responsible for holding a count on the request.
468  */
469 int
470 nfs_wait_on_request(struct nfs_page *req)
471 {
472         if (!test_bit(PG_BUSY, &req->wb_flags))
473                 return 0;
474         set_bit(PG_CONTENDED2, &req->wb_flags);
475         smp_mb__after_atomic();
476         return wait_on_bit_io(&req->wb_flags, PG_BUSY,
477                               TASK_UNINTERRUPTIBLE);
478 }
479 EXPORT_SYMBOL_GPL(nfs_wait_on_request);
480
481 /*
482  * nfs_generic_pg_test - determine if requests can be coalesced
483  * @desc: pointer to descriptor
484  * @prev: previous request in desc, or NULL
485  * @req: this request
486  *
487  * Returns zero if @req can be coalesced into @desc, otherwise it returns
488  * the size of the request.
489  */
490 size_t nfs_generic_pg_test(struct nfs_pageio_descriptor *desc,
491                            struct nfs_page *prev, struct nfs_page *req)
492 {
493         struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
494
495
496         if (mirror->pg_count > mirror->pg_bsize) {
497                 /* should never happen */
498                 WARN_ON_ONCE(1);
499                 return 0;
500         }
501
502         /*
503          * Limit the request size so that we can still allocate a page array
504          * for it without upsetting the slab allocator.
505          */
506         if (((mirror->pg_count + req->wb_bytes) >> PAGE_SHIFT) *
507                         sizeof(struct page *) > PAGE_SIZE)
508                 return 0;
509
510         return min(mirror->pg_bsize - mirror->pg_count, (size_t)req->wb_bytes);
511 }
512 EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
513
514 struct nfs_pgio_header *nfs_pgio_header_alloc(const struct nfs_rw_ops *ops)
515 {
516         struct nfs_pgio_header *hdr = ops->rw_alloc_header();
517
518         if (hdr) {
519                 INIT_LIST_HEAD(&hdr->pages);
520                 spin_lock_init(&hdr->lock);
521                 hdr->rw_ops = ops;
522         }
523         return hdr;
524 }
525 EXPORT_SYMBOL_GPL(nfs_pgio_header_alloc);
526
527 /**
528  * nfs_pgio_data_destroy - make @hdr suitable for reuse
529  *
530  * Frees memory and releases refs from nfs_generic_pgio, so that it may
531  * be called again.
532  *
533  * @hdr: A header that has had nfs_generic_pgio called
534  */
535 static void nfs_pgio_data_destroy(struct nfs_pgio_header *hdr)
536 {
537         if (hdr->args.context)
538                 put_nfs_open_context(hdr->args.context);
539         if (hdr->page_array.pagevec != hdr->page_array.page_array)
540                 kfree(hdr->page_array.pagevec);
541 }
542
543 /*
544  * nfs_pgio_header_free - Free a read or write header
545  * @hdr: The header to free
546  */
547 void nfs_pgio_header_free(struct nfs_pgio_header *hdr)
548 {
549         nfs_pgio_data_destroy(hdr);
550         hdr->rw_ops->rw_free_header(hdr);
551 }
552 EXPORT_SYMBOL_GPL(nfs_pgio_header_free);
553
554 /**
555  * nfs_pgio_rpcsetup - Set up arguments for a pageio call
556  * @hdr: The pageio hdr
557  * @count: Number of bytes to read
558  * @offset: Initial offset
559  * @how: How to commit data (writes only)
560  * @cinfo: Commit information for the call (writes only)
561  */
562 static void nfs_pgio_rpcsetup(struct nfs_pgio_header *hdr,
563                               unsigned int count, unsigned int offset,
564                               int how, struct nfs_commit_info *cinfo)
565 {
566         struct nfs_page *req = hdr->req;
567
568         /* Set up the RPC argument and reply structs
569          * NB: take care not to mess about with hdr->commit et al. */
570
571         hdr->args.fh     = NFS_FH(hdr->inode);
572         hdr->args.offset = req_offset(req) + offset;
573         /* pnfs_set_layoutcommit needs this */
574         hdr->mds_offset = hdr->args.offset;
575         hdr->args.pgbase = req->wb_pgbase + offset;
576         hdr->args.pages  = hdr->page_array.pagevec;
577         hdr->args.count  = count;
578         hdr->args.context = get_nfs_open_context(req->wb_context);
579         hdr->args.lock_context = req->wb_lock_context;
580         hdr->args.stable  = NFS_UNSTABLE;
581         switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
582         case 0:
583                 break;
584         case FLUSH_COND_STABLE:
585                 if (nfs_reqs_to_commit(cinfo))
586                         break;
587         default:
588                 hdr->args.stable = NFS_FILE_SYNC;
589         }
590
591         hdr->res.fattr   = &hdr->fattr;
592         hdr->res.count   = 0;
593         hdr->res.eof     = 0;
594         hdr->res.verf    = &hdr->verf;
595         nfs_fattr_init(&hdr->fattr);
596 }
597
598 /**
599  * nfs_pgio_prepare - Prepare pageio hdr to go over the wire
600  * @task: The current task
601  * @calldata: pageio header to prepare
602  */
603 static void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
604 {
605         struct nfs_pgio_header *hdr = calldata;
606         int err;
607         err = NFS_PROTO(hdr->inode)->pgio_rpc_prepare(task, hdr);
608         if (err)
609                 rpc_exit(task, err);
610 }
611
612 int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr,
613                       struct rpc_cred *cred, const struct nfs_rpc_ops *rpc_ops,
614                       const struct rpc_call_ops *call_ops, int how, int flags)
615 {
616         struct rpc_task *task;
617         struct rpc_message msg = {
618                 .rpc_argp = &hdr->args,
619                 .rpc_resp = &hdr->res,
620                 .rpc_cred = cred,
621         };
622         struct rpc_task_setup task_setup_data = {
623                 .rpc_client = clnt,
624                 .task = &hdr->task,
625                 .rpc_message = &msg,
626                 .callback_ops = call_ops,
627                 .callback_data = hdr,
628                 .workqueue = nfsiod_workqueue,
629                 .flags = RPC_TASK_ASYNC | flags,
630         };
631         int ret = 0;
632
633         hdr->rw_ops->rw_initiate(hdr, &msg, rpc_ops, &task_setup_data, how);
634
635         dprintk("NFS: initiated pgio call "
636                 "(req %s/%llu, %u bytes @ offset %llu)\n",
637                 hdr->inode->i_sb->s_id,
638                 (unsigned long long)NFS_FILEID(hdr->inode),
639                 hdr->args.count,
640                 (unsigned long long)hdr->args.offset);
641
642         task = rpc_run_task(&task_setup_data);
643         if (IS_ERR(task)) {
644                 ret = PTR_ERR(task);
645                 goto out;
646         }
647         if (how & FLUSH_SYNC) {
648                 ret = rpc_wait_for_completion_task(task);
649                 if (ret == 0)
650                         ret = task->tk_status;
651         }
652         rpc_put_task(task);
653 out:
654         return ret;
655 }
656 EXPORT_SYMBOL_GPL(nfs_initiate_pgio);
657
658 /**
659  * nfs_pgio_error - Clean up from a pageio error
660  * @desc: IO descriptor
661  * @hdr: pageio header
662  */
663 static void nfs_pgio_error(struct nfs_pgio_header *hdr)
664 {
665         set_bit(NFS_IOHDR_REDO, &hdr->flags);
666         hdr->completion_ops->completion(hdr);
667 }
668
669 /**
670  * nfs_pgio_release - Release pageio data
671  * @calldata: The pageio header to release
672  */
673 static void nfs_pgio_release(void *calldata)
674 {
675         struct nfs_pgio_header *hdr = calldata;
676         hdr->completion_ops->completion(hdr);
677 }
678
679 static void nfs_pageio_mirror_init(struct nfs_pgio_mirror *mirror,
680                                    unsigned int bsize)
681 {
682         INIT_LIST_HEAD(&mirror->pg_list);
683         mirror->pg_bytes_written = 0;
684         mirror->pg_count = 0;
685         mirror->pg_bsize = bsize;
686         mirror->pg_base = 0;
687         mirror->pg_recoalesce = 0;
688 }
689
690 /**
691  * nfs_pageio_init - initialise a page io descriptor
692  * @desc: pointer to descriptor
693  * @inode: pointer to inode
694  * @pg_ops: pointer to pageio operations
695  * @compl_ops: pointer to pageio completion operations
696  * @rw_ops: pointer to nfs read/write operations
697  * @bsize: io block size
698  * @io_flags: extra parameters for the io function
699  */
700 void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
701                      struct inode *inode,
702                      const struct nfs_pageio_ops *pg_ops,
703                      const struct nfs_pgio_completion_ops *compl_ops,
704                      const struct nfs_rw_ops *rw_ops,
705                      size_t bsize,
706                      int io_flags)
707 {
708         desc->pg_moreio = 0;
709         desc->pg_inode = inode;
710         desc->pg_ops = pg_ops;
711         desc->pg_completion_ops = compl_ops;
712         desc->pg_rw_ops = rw_ops;
713         desc->pg_ioflags = io_flags;
714         desc->pg_error = 0;
715         desc->pg_lseg = NULL;
716         desc->pg_io_completion = NULL;
717         desc->pg_dreq = NULL;
718         desc->pg_bsize = bsize;
719
720         desc->pg_mirror_count = 1;
721         desc->pg_mirror_idx = 0;
722
723         desc->pg_mirrors_dynamic = NULL;
724         desc->pg_mirrors = desc->pg_mirrors_static;
725         nfs_pageio_mirror_init(&desc->pg_mirrors[0], bsize);
726 }
727
728 /**
729  * nfs_pgio_result - Basic pageio error handling
730  * @task: The task that ran
731  * @calldata: Pageio header to check
732  */
733 static void nfs_pgio_result(struct rpc_task *task, void *calldata)
734 {
735         struct nfs_pgio_header *hdr = calldata;
736         struct inode *inode = hdr->inode;
737
738         dprintk("NFS: %s: %5u, (status %d)\n", __func__,
739                 task->tk_pid, task->tk_status);
740
741         if (hdr->rw_ops->rw_done(task, hdr, inode) != 0)
742                 return;
743         if (task->tk_status < 0)
744                 nfs_set_pgio_error(hdr, task->tk_status, hdr->args.offset);
745         else
746                 hdr->rw_ops->rw_result(task, hdr);
747 }
748
749 /*
750  * Create an RPC task for the given read or write request and kick it.
751  * The page must have been locked by the caller.
752  *
753  * It may happen that the page we're passed is not marked dirty.
754  * This is the case if nfs_updatepage detects a conflicting request
755  * that has been written but not committed.
756  */
757 int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
758                      struct nfs_pgio_header *hdr)
759 {
760         struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
761
762         struct nfs_page         *req;
763         struct page             **pages,
764                                 *last_page;
765         struct list_head *head = &mirror->pg_list;
766         struct nfs_commit_info cinfo;
767         struct nfs_page_array *pg_array = &hdr->page_array;
768         unsigned int pagecount, pageused;
769         gfp_t gfp_flags = GFP_KERNEL;
770
771         pagecount = nfs_page_array_len(mirror->pg_base, mirror->pg_count);
772         pg_array->npages = pagecount;
773
774         if (pagecount <= ARRAY_SIZE(pg_array->page_array))
775                 pg_array->pagevec = pg_array->page_array;
776         else {
777                 if (hdr->rw_mode == FMODE_WRITE)
778                         gfp_flags = GFP_NOIO;
779                 pg_array->pagevec = kcalloc(pagecount, sizeof(struct page *), gfp_flags);
780                 if (!pg_array->pagevec) {
781                         pg_array->npages = 0;
782                         nfs_pgio_error(hdr);
783                         desc->pg_error = -ENOMEM;
784                         return desc->pg_error;
785                 }
786         }
787
788         nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
789         pages = hdr->page_array.pagevec;
790         last_page = NULL;
791         pageused = 0;
792         while (!list_empty(head)) {
793                 req = nfs_list_entry(head->next);
794                 nfs_list_move_request(req, &hdr->pages);
795
796                 if (!last_page || last_page != req->wb_page) {
797                         pageused++;
798                         if (pageused > pagecount)
799                                 break;
800                         *pages++ = last_page = req->wb_page;
801                 }
802         }
803         if (WARN_ON_ONCE(pageused != pagecount)) {
804                 nfs_pgio_error(hdr);
805                 desc->pg_error = -EINVAL;
806                 return desc->pg_error;
807         }
808
809         if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
810             (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
811                 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
812
813         /* Set up the argument struct */
814         nfs_pgio_rpcsetup(hdr, mirror->pg_count, 0, desc->pg_ioflags, &cinfo);
815         desc->pg_rpc_callops = &nfs_pgio_common_ops;
816         return 0;
817 }
818 EXPORT_SYMBOL_GPL(nfs_generic_pgio);
819
820 static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
821 {
822         struct nfs_pgio_header *hdr;
823         int ret;
824
825         hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
826         if (!hdr) {
827                 desc->pg_error = -ENOMEM;
828                 return desc->pg_error;
829         }
830         nfs_pgheader_init(desc, hdr, nfs_pgio_header_free);
831         ret = nfs_generic_pgio(desc, hdr);
832         if (ret == 0)
833                 ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode),
834                                         hdr,
835                                         hdr->cred,
836                                         NFS_PROTO(hdr->inode),
837                                         desc->pg_rpc_callops,
838                                         desc->pg_ioflags, 0);
839         return ret;
840 }
841
842 static struct nfs_pgio_mirror *
843 nfs_pageio_alloc_mirrors(struct nfs_pageio_descriptor *desc,
844                 unsigned int mirror_count)
845 {
846         struct nfs_pgio_mirror *ret;
847         unsigned int i;
848
849         kfree(desc->pg_mirrors_dynamic);
850         desc->pg_mirrors_dynamic = NULL;
851         if (mirror_count == 1)
852                 return desc->pg_mirrors_static;
853         ret = kmalloc_array(mirror_count, sizeof(*ret), GFP_NOFS);
854         if (ret != NULL) {
855                 for (i = 0; i < mirror_count; i++)
856                         nfs_pageio_mirror_init(&ret[i], desc->pg_bsize);
857                 desc->pg_mirrors_dynamic = ret;
858         }
859         return ret;
860 }
861
862 /*
863  * nfs_pageio_setup_mirroring - determine if mirroring is to be used
864  *                              by calling the pg_get_mirror_count op
865  */
866 static void nfs_pageio_setup_mirroring(struct nfs_pageio_descriptor *pgio,
867                                        struct nfs_page *req)
868 {
869         unsigned int mirror_count = 1;
870
871         if (pgio->pg_ops->pg_get_mirror_count)
872                 mirror_count = pgio->pg_ops->pg_get_mirror_count(pgio, req);
873         if (mirror_count == pgio->pg_mirror_count || pgio->pg_error < 0)
874                 return;
875
876         if (!mirror_count || mirror_count > NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX) {
877                 pgio->pg_error = -EINVAL;
878                 return;
879         }
880
881         pgio->pg_mirrors = nfs_pageio_alloc_mirrors(pgio, mirror_count);
882         if (pgio->pg_mirrors == NULL) {
883                 pgio->pg_error = -ENOMEM;
884                 pgio->pg_mirrors = pgio->pg_mirrors_static;
885                 mirror_count = 1;
886         }
887         pgio->pg_mirror_count = mirror_count;
888 }
889
890 static void nfs_pageio_cleanup_mirroring(struct nfs_pageio_descriptor *pgio)
891 {
892         pgio->pg_mirror_count = 1;
893         pgio->pg_mirror_idx = 0;
894         pgio->pg_mirrors = pgio->pg_mirrors_static;
895         kfree(pgio->pg_mirrors_dynamic);
896         pgio->pg_mirrors_dynamic = NULL;
897 }
898
899 static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
900                 const struct nfs_lock_context *l2)
901 {
902         return l1->lockowner == l2->lockowner;
903 }
904
905 /**
906  * nfs_can_coalesce_requests - test two requests for compatibility
907  * @prev: pointer to nfs_page
908  * @req: pointer to nfs_page
909  *
910  * The nfs_page structures 'prev' and 'req' are compared to ensure that the
911  * page data area they describe is contiguous, and that their RPC
912  * credentials, NFSv4 open state, and lockowners are the same.
913  *
914  * Return 'true' if this is the case, else return 'false'.
915  */
916 static bool nfs_can_coalesce_requests(struct nfs_page *prev,
917                                       struct nfs_page *req,
918                                       struct nfs_pageio_descriptor *pgio)
919 {
920         size_t size;
921         struct file_lock_context *flctx;
922
923         if (prev) {
924                 if (!nfs_match_open_context(req->wb_context, prev->wb_context))
925                         return false;
926                 flctx = d_inode(req->wb_context->dentry)->i_flctx;
927                 if (flctx != NULL &&
928                     !(list_empty_careful(&flctx->flc_posix) &&
929                       list_empty_careful(&flctx->flc_flock)) &&
930                     !nfs_match_lock_context(req->wb_lock_context,
931                                             prev->wb_lock_context))
932                         return false;
933                 if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
934                         return false;
935                 if (req->wb_page == prev->wb_page) {
936                         if (req->wb_pgbase != prev->wb_pgbase + prev->wb_bytes)
937                                 return false;
938                 } else {
939                         if (req->wb_pgbase != 0 ||
940                             prev->wb_pgbase + prev->wb_bytes != PAGE_SIZE)
941                                 return false;
942                 }
943         }
944         size = pgio->pg_ops->pg_test(pgio, prev, req);
945         WARN_ON_ONCE(size > req->wb_bytes);
946         if (size && size < req->wb_bytes)
947                 req->wb_bytes = size;
948         return size > 0;
949 }
950
951 /**
952  * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
953  * @desc: destination io descriptor
954  * @req: request
955  *
956  * Returns true if the request 'req' was successfully coalesced into the
957  * existing list of pages 'desc'.
958  */
959 static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
960                                      struct nfs_page *req)
961 {
962         struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
963
964         struct nfs_page *prev = NULL;
965
966         if (mirror->pg_count != 0) {
967                 prev = nfs_list_entry(mirror->pg_list.prev);
968         } else {
969                 if (desc->pg_ops->pg_init)
970                         desc->pg_ops->pg_init(desc, req);
971                 if (desc->pg_error < 0)
972                         return 0;
973                 mirror->pg_base = req->wb_pgbase;
974         }
975         if (!nfs_can_coalesce_requests(prev, req, desc))
976                 return 0;
977         nfs_list_move_request(req, &mirror->pg_list);
978         mirror->pg_count += req->wb_bytes;
979         return 1;
980 }
981
982 /*
983  * Helper for nfs_pageio_add_request and nfs_pageio_complete
984  */
985 static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
986 {
987         struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
988
989         if (!list_empty(&mirror->pg_list)) {
990                 int error = desc->pg_ops->pg_doio(desc);
991                 if (error < 0)
992                         desc->pg_error = error;
993                 if (list_empty(&mirror->pg_list)) {
994                         mirror->pg_bytes_written += mirror->pg_count;
995                         mirror->pg_count = 0;
996                         mirror->pg_base = 0;
997                         mirror->pg_recoalesce = 0;
998                 }
999         }
1000 }
1001
1002 static void
1003 nfs_pageio_cleanup_request(struct nfs_pageio_descriptor *desc,
1004                 struct nfs_page *req)
1005 {
1006         LIST_HEAD(head);
1007
1008         nfs_list_move_request(req, &head);
1009         desc->pg_completion_ops->error_cleanup(&head, desc->pg_error);
1010 }
1011
1012 /**
1013  * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
1014  * @desc: destination io descriptor
1015  * @req: request
1016  *
1017  * This may split a request into subrequests which are all part of the
1018  * same page group.
1019  *
1020  * Returns true if the request 'req' was successfully coalesced into the
1021  * existing list of pages 'desc'.
1022  */
1023 static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
1024                            struct nfs_page *req)
1025 {
1026         struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
1027
1028         struct nfs_page *subreq;
1029         unsigned int bytes_left = 0;
1030         unsigned int offset, pgbase;
1031
1032         nfs_page_group_lock(req);
1033
1034         subreq = req;
1035         bytes_left = subreq->wb_bytes;
1036         offset = subreq->wb_offset;
1037         pgbase = subreq->wb_pgbase;
1038
1039         do {
1040                 if (!nfs_pageio_do_add_request(desc, subreq)) {
1041                         /* make sure pg_test call(s) did nothing */
1042                         WARN_ON_ONCE(subreq->wb_bytes != bytes_left);
1043                         WARN_ON_ONCE(subreq->wb_offset != offset);
1044                         WARN_ON_ONCE(subreq->wb_pgbase != pgbase);
1045
1046                         nfs_page_group_unlock(req);
1047                         desc->pg_moreio = 1;
1048                         nfs_pageio_doio(desc);
1049                         if (desc->pg_error < 0 || mirror->pg_recoalesce)
1050                                 goto out_cleanup_subreq;
1051                         /* retry add_request for this subreq */
1052                         nfs_page_group_lock(req);
1053                         continue;
1054                 }
1055
1056                 /* check for buggy pg_test call(s) */
1057                 WARN_ON_ONCE(subreq->wb_bytes + subreq->wb_pgbase > PAGE_SIZE);
1058                 WARN_ON_ONCE(subreq->wb_bytes > bytes_left);
1059                 WARN_ON_ONCE(subreq->wb_bytes == 0);
1060
1061                 bytes_left -= subreq->wb_bytes;
1062                 offset += subreq->wb_bytes;
1063                 pgbase += subreq->wb_bytes;
1064
1065                 if (bytes_left) {
1066                         subreq = nfs_create_request(req->wb_context,
1067                                         req->wb_page,
1068                                         subreq, pgbase, bytes_left);
1069                         if (IS_ERR(subreq))
1070                                 goto err_ptr;
1071                         nfs_lock_request(subreq);
1072                         subreq->wb_offset  = offset;
1073                         subreq->wb_index = req->wb_index;
1074                 }
1075         } while (bytes_left > 0);
1076
1077         nfs_page_group_unlock(req);
1078         return 1;
1079 err_ptr:
1080         desc->pg_error = PTR_ERR(subreq);
1081         nfs_page_group_unlock(req);
1082         return 0;
1083 out_cleanup_subreq:
1084         if (req != subreq)
1085                 nfs_pageio_cleanup_request(desc, subreq);
1086         return 0;
1087 }
1088
1089 static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
1090 {
1091         struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
1092         LIST_HEAD(head);
1093
1094         do {
1095                 list_splice_init(&mirror->pg_list, &head);
1096                 mirror->pg_count = 0;
1097                 mirror->pg_base = 0;
1098                 mirror->pg_recoalesce = 0;
1099
1100                 while (!list_empty(&head)) {
1101                         struct nfs_page *req;
1102
1103                         req = list_first_entry(&head, struct nfs_page, wb_list);
1104                         if (__nfs_pageio_add_request(desc, req))
1105                                 continue;
1106                         if (desc->pg_error < 0) {
1107                                 list_splice_tail(&head, &mirror->pg_list);
1108                                 mirror->pg_recoalesce = 1;
1109                                 return 0;
1110                         }
1111                         break;
1112                 }
1113         } while (mirror->pg_recoalesce);
1114         return 1;
1115 }
1116
1117 static int nfs_pageio_add_request_mirror(struct nfs_pageio_descriptor *desc,
1118                 struct nfs_page *req)
1119 {
1120         int ret;
1121
1122         do {
1123                 ret = __nfs_pageio_add_request(desc, req);
1124                 if (ret)
1125                         break;
1126                 if (desc->pg_error < 0)
1127                         break;
1128                 ret = nfs_do_recoalesce(desc);
1129         } while (ret);
1130
1131         return ret;
1132 }
1133
1134 static void nfs_pageio_error_cleanup(struct nfs_pageio_descriptor *desc)
1135 {
1136         u32 midx;
1137         struct nfs_pgio_mirror *mirror;
1138
1139         if (!desc->pg_error)
1140                 return;
1141
1142         for (midx = 0; midx < desc->pg_mirror_count; midx++) {
1143                 mirror = &desc->pg_mirrors[midx];
1144                 desc->pg_completion_ops->error_cleanup(&mirror->pg_list,
1145                                 desc->pg_error);
1146         }
1147 }
1148
1149 int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
1150                            struct nfs_page *req)
1151 {
1152         u32 midx;
1153         unsigned int pgbase, offset, bytes;
1154         struct nfs_page *dupreq, *lastreq;
1155
1156         pgbase = req->wb_pgbase;
1157         offset = req->wb_offset;
1158         bytes = req->wb_bytes;
1159
1160         nfs_pageio_setup_mirroring(desc, req);
1161         if (desc->pg_error < 0)
1162                 goto out_failed;
1163
1164         for (midx = 0; midx < desc->pg_mirror_count; midx++) {
1165                 if (midx) {
1166                         nfs_page_group_lock(req);
1167
1168                         /* find the last request */
1169                         for (lastreq = req->wb_head;
1170                              lastreq->wb_this_page != req->wb_head;
1171                              lastreq = lastreq->wb_this_page)
1172                                 ;
1173
1174                         dupreq = nfs_create_request(req->wb_context,
1175                                         req->wb_page, lastreq, pgbase, bytes);
1176
1177                         if (IS_ERR(dupreq)) {
1178                                 nfs_page_group_unlock(req);
1179                                 desc->pg_error = PTR_ERR(dupreq);
1180                                 goto out_failed;
1181                         }
1182
1183                         nfs_lock_request(dupreq);
1184                         nfs_page_group_unlock(req);
1185                         dupreq->wb_offset = offset;
1186                         dupreq->wb_index = req->wb_index;
1187                 } else
1188                         dupreq = req;
1189
1190                 if (nfs_pgio_has_mirroring(desc))
1191                         desc->pg_mirror_idx = midx;
1192                 if (!nfs_pageio_add_request_mirror(desc, dupreq))
1193                         goto out_cleanup_subreq;
1194         }
1195
1196         return 1;
1197
1198 out_cleanup_subreq:
1199         if (req != dupreq)
1200                 nfs_pageio_cleanup_request(desc, dupreq);
1201 out_failed:
1202         /* remember fatal errors */
1203         if (nfs_error_is_fatal(desc->pg_error))
1204                 nfs_context_set_write_error(req->wb_context,
1205                                                 desc->pg_error);
1206         nfs_pageio_error_cleanup(desc);
1207         return 0;
1208 }
1209
1210 /*
1211  * nfs_pageio_complete_mirror - Complete I/O on the current mirror of an
1212  *                              nfs_pageio_descriptor
1213  * @desc: pointer to io descriptor
1214  * @mirror_idx: pointer to mirror index
1215  */
1216 static void nfs_pageio_complete_mirror(struct nfs_pageio_descriptor *desc,
1217                                        u32 mirror_idx)
1218 {
1219         struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[mirror_idx];
1220         u32 restore_idx = desc->pg_mirror_idx;
1221
1222         if (nfs_pgio_has_mirroring(desc))
1223                 desc->pg_mirror_idx = mirror_idx;
1224         for (;;) {
1225                 nfs_pageio_doio(desc);
1226                 if (desc->pg_error < 0 || !mirror->pg_recoalesce)
1227                         break;
1228                 if (!nfs_do_recoalesce(desc))
1229                         break;
1230         }
1231         desc->pg_mirror_idx = restore_idx;
1232 }
1233
1234 /*
1235  * nfs_pageio_resend - Transfer requests to new descriptor and resend
1236  * @hdr - the pgio header to move request from
1237  * @desc - the pageio descriptor to add requests to
1238  *
1239  * Try to move each request (nfs_page) from @hdr to @desc then attempt
1240  * to send them.
1241  *
1242  * Returns 0 on success and < 0 on error.
1243  */
1244 int nfs_pageio_resend(struct nfs_pageio_descriptor *desc,
1245                       struct nfs_pgio_header *hdr)
1246 {
1247         LIST_HEAD(pages);
1248
1249         desc->pg_io_completion = hdr->io_completion;
1250         desc->pg_dreq = hdr->dreq;
1251         list_splice_init(&hdr->pages, &pages);
1252         while (!list_empty(&pages)) {
1253                 struct nfs_page *req = nfs_list_entry(pages.next);
1254
1255                 if (!nfs_pageio_add_request(desc, req))
1256                         break;
1257         }
1258         nfs_pageio_complete(desc);
1259         if (!list_empty(&pages)) {
1260                 int err = desc->pg_error < 0 ? desc->pg_error : -EIO;
1261                 hdr->completion_ops->error_cleanup(&pages, err);
1262                 nfs_set_pgio_error(hdr, err, hdr->io_start);
1263                 return err;
1264         }
1265         return 0;
1266 }
1267 EXPORT_SYMBOL_GPL(nfs_pageio_resend);
1268
1269 /**
1270  * nfs_pageio_complete - Complete I/O then cleanup an nfs_pageio_descriptor
1271  * @desc: pointer to io descriptor
1272  */
1273 void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
1274 {
1275         u32 midx;
1276
1277         for (midx = 0; midx < desc->pg_mirror_count; midx++)
1278                 nfs_pageio_complete_mirror(desc, midx);
1279
1280         if (desc->pg_error < 0)
1281                 nfs_pageio_error_cleanup(desc);
1282         if (desc->pg_ops->pg_cleanup)
1283                 desc->pg_ops->pg_cleanup(desc);
1284         nfs_pageio_cleanup_mirroring(desc);
1285 }
1286
1287 /**
1288  * nfs_pageio_cond_complete - Conditional I/O completion
1289  * @desc: pointer to io descriptor
1290  * @index: page index
1291  *
1292  * It is important to ensure that processes don't try to take locks
1293  * on non-contiguous ranges of pages as that might deadlock. This
1294  * function should be called before attempting to wait on a locked
1295  * nfs_page. It will complete the I/O if the page index 'index'
1296  * is not contiguous with the existing list of pages in 'desc'.
1297  */
1298 void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
1299 {
1300         struct nfs_pgio_mirror *mirror;
1301         struct nfs_page *prev;
1302         u32 midx;
1303
1304         for (midx = 0; midx < desc->pg_mirror_count; midx++) {
1305                 mirror = &desc->pg_mirrors[midx];
1306                 if (!list_empty(&mirror->pg_list)) {
1307                         prev = nfs_list_entry(mirror->pg_list.prev);
1308                         if (index != prev->wb_index + 1) {
1309                                 nfs_pageio_complete(desc);
1310                                 break;
1311                         }
1312                 }
1313         }
1314 }
1315
1316 /*
1317  * nfs_pageio_stop_mirroring - stop using mirroring (set mirror count to 1)
1318  */
1319 void nfs_pageio_stop_mirroring(struct nfs_pageio_descriptor *pgio)
1320 {
1321         nfs_pageio_complete(pgio);
1322 }
1323
1324 int __init nfs_init_nfspagecache(void)
1325 {
1326         nfs_page_cachep = kmem_cache_create("nfs_page",
1327                                             sizeof(struct nfs_page),
1328                                             0, SLAB_HWCACHE_ALIGN,
1329                                             NULL);
1330         if (nfs_page_cachep == NULL)
1331                 return -ENOMEM;
1332
1333         return 0;
1334 }
1335
1336 void nfs_destroy_nfspagecache(void)
1337 {
1338         kmem_cache_destroy(nfs_page_cachep);
1339 }
1340
1341 static const struct rpc_call_ops nfs_pgio_common_ops = {
1342         .rpc_call_prepare = nfs_pgio_prepare,
1343         .rpc_call_done = nfs_pgio_result,
1344         .rpc_release = nfs_pgio_release,
1345 };
1346
1347 const struct nfs_pageio_ops nfs_pgio_rw_ops = {
1348         .pg_test = nfs_generic_pg_test,
1349         .pg_doio = nfs_generic_pg_pgios,
1350 };