Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * linux/fs/nfs/pagelist.c
0004  *
0005  * A set of helper functions for managing NFS read and write requests.
0006  * The main purpose of these routines is to provide support for the
0007  * coalescing of several requests into a single RPC call.
0008  *
0009  * Copyright 2000, 2001 (c) Trond Myklebust <trond.myklebust@fys.uio.no>
0010  *
0011  */
0012 
0013 #include <linux/slab.h>
0014 #include <linux/file.h>
0015 #include <linux/sched.h>
0016 #include <linux/sunrpc/clnt.h>
0017 #include <linux/nfs.h>
0018 #include <linux/nfs3.h>
0019 #include <linux/nfs4.h>
0020 #include <linux/nfs_fs.h>
0021 #include <linux/nfs_page.h>
0022 #include <linux/nfs_mount.h>
0023 #include <linux/export.h>
0024 
0025 #include "internal.h"
0026 #include "pnfs.h"
0027 #include "nfstrace.h"
0028 
0029 #define NFSDBG_FACILITY     NFSDBG_PAGECACHE
0030 
0031 static struct kmem_cache *nfs_page_cachep;
0032 static const struct rpc_call_ops nfs_pgio_common_ops;
0033 
0034 static struct nfs_pgio_mirror *
0035 nfs_pgio_get_mirror(struct nfs_pageio_descriptor *desc, u32 idx)
0036 {
0037     if (desc->pg_ops->pg_get_mirror)
0038         return desc->pg_ops->pg_get_mirror(desc, idx);
0039     return &desc->pg_mirrors[0];
0040 }
0041 
0042 struct nfs_pgio_mirror *
0043 nfs_pgio_current_mirror(struct nfs_pageio_descriptor *desc)
0044 {
0045     return nfs_pgio_get_mirror(desc, desc->pg_mirror_idx);
0046 }
0047 EXPORT_SYMBOL_GPL(nfs_pgio_current_mirror);
0048 
0049 static u32
0050 nfs_pgio_set_current_mirror(struct nfs_pageio_descriptor *desc, u32 idx)
0051 {
0052     if (desc->pg_ops->pg_set_mirror)
0053         return desc->pg_ops->pg_set_mirror(desc, idx);
0054     return desc->pg_mirror_idx;
0055 }
0056 
0057 void nfs_pgheader_init(struct nfs_pageio_descriptor *desc,
0058                struct nfs_pgio_header *hdr,
0059                void (*release)(struct nfs_pgio_header *hdr))
0060 {
0061     struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
0062 
0063 
0064     hdr->req = nfs_list_entry(mirror->pg_list.next);
0065     hdr->inode = desc->pg_inode;
0066     hdr->cred = nfs_req_openctx(hdr->req)->cred;
0067     hdr->io_start = req_offset(hdr->req);
0068     hdr->good_bytes = mirror->pg_count;
0069     hdr->io_completion = desc->pg_io_completion;
0070     hdr->dreq = desc->pg_dreq;
0071     hdr->release = release;
0072     hdr->completion_ops = desc->pg_completion_ops;
0073     if (hdr->completion_ops->init_hdr)
0074         hdr->completion_ops->init_hdr(hdr);
0075 
0076     hdr->pgio_mirror_idx = desc->pg_mirror_idx;
0077 }
0078 EXPORT_SYMBOL_GPL(nfs_pgheader_init);
0079 
0080 void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos)
0081 {
0082     unsigned int new = pos - hdr->io_start;
0083 
0084     trace_nfs_pgio_error(hdr, error, pos);
0085     if (hdr->good_bytes > new) {
0086         hdr->good_bytes = new;
0087         clear_bit(NFS_IOHDR_EOF, &hdr->flags);
0088         if (!test_and_set_bit(NFS_IOHDR_ERROR, &hdr->flags))
0089             hdr->error = error;
0090     }
0091 }
0092 
0093 static inline struct nfs_page *nfs_page_alloc(void)
0094 {
0095     struct nfs_page *p =
0096         kmem_cache_zalloc(nfs_page_cachep, nfs_io_gfp_mask());
0097     if (p)
0098         INIT_LIST_HEAD(&p->wb_list);
0099     return p;
0100 }
0101 
0102 static inline void
0103 nfs_page_free(struct nfs_page *p)
0104 {
0105     kmem_cache_free(nfs_page_cachep, p);
0106 }
0107 
0108 /**
0109  * nfs_iocounter_wait - wait for i/o to complete
0110  * @l_ctx: nfs_lock_context with io_counter to use
0111  *
0112  * returns -ERESTARTSYS if interrupted by a fatal signal.
0113  * Otherwise returns 0 once the io_count hits 0.
0114  */
0115 int
0116 nfs_iocounter_wait(struct nfs_lock_context *l_ctx)
0117 {
0118     return wait_var_event_killable(&l_ctx->io_count,
0119                        !atomic_read(&l_ctx->io_count));
0120 }
0121 
0122 /**
0123  * nfs_async_iocounter_wait - wait on a rpc_waitqueue for I/O
0124  * to complete
0125  * @task: the rpc_task that should wait
0126  * @l_ctx: nfs_lock_context with io_counter to check
0127  *
0128  * Returns true if there is outstanding I/O to wait on and the
0129  * task has been put to sleep.
0130  */
0131 bool
0132 nfs_async_iocounter_wait(struct rpc_task *task, struct nfs_lock_context *l_ctx)
0133 {
0134     struct inode *inode = d_inode(l_ctx->open_context->dentry);
0135     bool ret = false;
0136 
0137     if (atomic_read(&l_ctx->io_count) > 0) {
0138         rpc_sleep_on(&NFS_SERVER(inode)->uoc_rpcwaitq, task, NULL);
0139         ret = true;
0140     }
0141 
0142     if (atomic_read(&l_ctx->io_count) == 0) {
0143         rpc_wake_up_queued_task(&NFS_SERVER(inode)->uoc_rpcwaitq, task);
0144         ret = false;
0145     }
0146 
0147     return ret;
0148 }
0149 EXPORT_SYMBOL_GPL(nfs_async_iocounter_wait);
0150 
0151 /*
0152  * nfs_page_lock_head_request - page lock the head of the page group
0153  * @req: any member of the page group
0154  */
0155 struct nfs_page *
0156 nfs_page_group_lock_head(struct nfs_page *req)
0157 {
0158     struct nfs_page *head = req->wb_head;
0159 
0160     while (!nfs_lock_request(head)) {
0161         int ret = nfs_wait_on_request(head);
0162         if (ret < 0)
0163             return ERR_PTR(ret);
0164     }
0165     if (head != req)
0166         kref_get(&head->wb_kref);
0167     return head;
0168 }
0169 
0170 /*
0171  * nfs_unroll_locks -  unlock all newly locked reqs and wait on @req
0172  * @head: head request of page group, must be holding head lock
0173  * @req: request that couldn't lock and needs to wait on the req bit lock
0174  *
0175  * This is a helper function for nfs_lock_and_join_requests
0176  * returns 0 on success, < 0 on error.
0177  */
0178 static void
0179 nfs_unroll_locks(struct nfs_page *head, struct nfs_page *req)
0180 {
0181     struct nfs_page *tmp;
0182 
0183     /* relinquish all the locks successfully grabbed this run */
0184     for (tmp = head->wb_this_page ; tmp != req; tmp = tmp->wb_this_page) {
0185         if (!kref_read(&tmp->wb_kref))
0186             continue;
0187         nfs_unlock_and_release_request(tmp);
0188     }
0189 }
0190 
0191 /*
0192  * nfs_page_group_lock_subreq -  try to lock a subrequest
0193  * @head: head request of page group
0194  * @subreq: request to lock
0195  *
0196  * This is a helper function for nfs_lock_and_join_requests which
0197  * must be called with the head request and page group both locked.
0198  * On error, it returns with the page group unlocked.
0199  */
0200 static int
0201 nfs_page_group_lock_subreq(struct nfs_page *head, struct nfs_page *subreq)
0202 {
0203     int ret;
0204 
0205     if (!kref_get_unless_zero(&subreq->wb_kref))
0206         return 0;
0207     while (!nfs_lock_request(subreq)) {
0208         nfs_page_group_unlock(head);
0209         ret = nfs_wait_on_request(subreq);
0210         if (!ret)
0211             ret = nfs_page_group_lock(head);
0212         if (ret < 0) {
0213             nfs_unroll_locks(head, subreq);
0214             nfs_release_request(subreq);
0215             return ret;
0216         }
0217     }
0218     return 0;
0219 }
0220 
0221 /*
0222  * nfs_page_group_lock_subrequests -  try to lock the subrequests
0223  * @head: head request of page group
0224  *
0225  * This is a helper function for nfs_lock_and_join_requests which
0226  * must be called with the head request locked.
0227  */
0228 int nfs_page_group_lock_subrequests(struct nfs_page *head)
0229 {
0230     struct nfs_page *subreq;
0231     int ret;
0232 
0233     ret = nfs_page_group_lock(head);
0234     if (ret < 0)
0235         return ret;
0236     /* lock each request in the page group */
0237     for (subreq = head->wb_this_page; subreq != head;
0238             subreq = subreq->wb_this_page) {
0239         ret = nfs_page_group_lock_subreq(head, subreq);
0240         if (ret < 0)
0241             return ret;
0242     }
0243     nfs_page_group_unlock(head);
0244     return 0;
0245 }
0246 
0247 /*
0248  * nfs_page_set_headlock - set the request PG_HEADLOCK
0249  * @req: request that is to be locked
0250  *
0251  * this lock must be held when modifying req->wb_head
0252  *
0253  * return 0 on success, < 0 on error
0254  */
0255 int
0256 nfs_page_set_headlock(struct nfs_page *req)
0257 {
0258     if (!test_and_set_bit(PG_HEADLOCK, &req->wb_flags))
0259         return 0;
0260 
0261     set_bit(PG_CONTENDED1, &req->wb_flags);
0262     smp_mb__after_atomic();
0263     return wait_on_bit_lock(&req->wb_flags, PG_HEADLOCK,
0264                 TASK_UNINTERRUPTIBLE);
0265 }
0266 
0267 /*
0268  * nfs_page_clear_headlock - clear the request PG_HEADLOCK
0269  * @req: request that is to be locked
0270  */
0271 void
0272 nfs_page_clear_headlock(struct nfs_page *req)
0273 {
0274     clear_bit_unlock(PG_HEADLOCK, &req->wb_flags);
0275     smp_mb__after_atomic();
0276     if (!test_bit(PG_CONTENDED1, &req->wb_flags))
0277         return;
0278     wake_up_bit(&req->wb_flags, PG_HEADLOCK);
0279 }
0280 
0281 /*
0282  * nfs_page_group_lock - lock the head of the page group
0283  * @req: request in group that is to be locked
0284  *
0285  * this lock must be held when traversing or modifying the page
0286  * group list
0287  *
0288  * return 0 on success, < 0 on error
0289  */
0290 int
0291 nfs_page_group_lock(struct nfs_page *req)
0292 {
0293     int ret;
0294 
0295     ret = nfs_page_set_headlock(req);
0296     if (ret || req->wb_head == req)
0297         return ret;
0298     return nfs_page_set_headlock(req->wb_head);
0299 }
0300 
0301 /*
0302  * nfs_page_group_unlock - unlock the head of the page group
0303  * @req: request in group that is to be unlocked
0304  */
0305 void
0306 nfs_page_group_unlock(struct nfs_page *req)
0307 {
0308     if (req != req->wb_head)
0309         nfs_page_clear_headlock(req->wb_head);
0310     nfs_page_clear_headlock(req);
0311 }
0312 
0313 /*
0314  * nfs_page_group_sync_on_bit_locked
0315  *
0316  * must be called with page group lock held
0317  */
0318 static bool
0319 nfs_page_group_sync_on_bit_locked(struct nfs_page *req, unsigned int bit)
0320 {
0321     struct nfs_page *head = req->wb_head;
0322     struct nfs_page *tmp;
0323 
0324     WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_flags));
0325     WARN_ON_ONCE(test_and_set_bit(bit, &req->wb_flags));
0326 
0327     tmp = req->wb_this_page;
0328     while (tmp != req) {
0329         if (!test_bit(bit, &tmp->wb_flags))
0330             return false;
0331         tmp = tmp->wb_this_page;
0332     }
0333 
0334     /* true! reset all bits */
0335     tmp = req;
0336     do {
0337         clear_bit(bit, &tmp->wb_flags);
0338         tmp = tmp->wb_this_page;
0339     } while (tmp != req);
0340 
0341     return true;
0342 }
0343 
0344 /*
0345  * nfs_page_group_sync_on_bit - set bit on current request, but only
0346  *   return true if the bit is set for all requests in page group
0347  * @req - request in page group
0348  * @bit - PG_* bit that is used to sync page group
0349  */
0350 bool nfs_page_group_sync_on_bit(struct nfs_page *req, unsigned int bit)
0351 {
0352     bool ret;
0353 
0354     nfs_page_group_lock(req);
0355     ret = nfs_page_group_sync_on_bit_locked(req, bit);
0356     nfs_page_group_unlock(req);
0357 
0358     return ret;
0359 }
0360 
0361 /*
0362  * nfs_page_group_init - Initialize the page group linkage for @req
0363  * @req - a new nfs request
0364  * @prev - the previous request in page group, or NULL if @req is the first
0365  *         or only request in the group (the head).
0366  */
0367 static inline void
0368 nfs_page_group_init(struct nfs_page *req, struct nfs_page *prev)
0369 {
0370     struct inode *inode;
0371     WARN_ON_ONCE(prev == req);
0372 
0373     if (!prev) {
0374         /* a head request */
0375         req->wb_head = req;
0376         req->wb_this_page = req;
0377     } else {
0378         /* a subrequest */
0379         WARN_ON_ONCE(prev->wb_this_page != prev->wb_head);
0380         WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &prev->wb_head->wb_flags));
0381         req->wb_head = prev->wb_head;
0382         req->wb_this_page = prev->wb_this_page;
0383         prev->wb_this_page = req;
0384 
0385         /* All subrequests take a ref on the head request until
0386          * nfs_page_group_destroy is called */
0387         kref_get(&req->wb_head->wb_kref);
0388 
0389         /* grab extra ref and bump the request count if head request
0390          * has extra ref from the write/commit path to handle handoff
0391          * between write and commit lists. */
0392         if (test_bit(PG_INODE_REF, &prev->wb_head->wb_flags)) {
0393             inode = page_file_mapping(req->wb_page)->host;
0394             set_bit(PG_INODE_REF, &req->wb_flags);
0395             kref_get(&req->wb_kref);
0396             atomic_long_inc(&NFS_I(inode)->nrequests);
0397         }
0398     }
0399 }
0400 
0401 /*
0402  * nfs_page_group_destroy - sync the destruction of page groups
0403  * @req - request that no longer needs the page group
0404  *
0405  * releases the page group reference from each member once all
0406  * members have called this function.
0407  */
0408 static void
0409 nfs_page_group_destroy(struct kref *kref)
0410 {
0411     struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
0412     struct nfs_page *head = req->wb_head;
0413     struct nfs_page *tmp, *next;
0414 
0415     if (!nfs_page_group_sync_on_bit(req, PG_TEARDOWN))
0416         goto out;
0417 
0418     tmp = req;
0419     do {
0420         next = tmp->wb_this_page;
0421         /* unlink and free */
0422         tmp->wb_this_page = tmp;
0423         tmp->wb_head = tmp;
0424         nfs_free_request(tmp);
0425         tmp = next;
0426     } while (tmp != req);
0427 out:
0428     /* subrequests must release the ref on the head request */
0429     if (head != req)
0430         nfs_release_request(head);
0431 }
0432 
0433 static struct nfs_page *
0434 __nfs_create_request(struct nfs_lock_context *l_ctx, struct page *page,
0435            unsigned int pgbase, unsigned int offset,
0436            unsigned int count)
0437 {
0438     struct nfs_page     *req;
0439     struct nfs_open_context *ctx = l_ctx->open_context;
0440 
0441     if (test_bit(NFS_CONTEXT_BAD, &ctx->flags))
0442         return ERR_PTR(-EBADF);
0443     /* try to allocate the request struct */
0444     req = nfs_page_alloc();
0445     if (req == NULL)
0446         return ERR_PTR(-ENOMEM);
0447 
0448     req->wb_lock_context = l_ctx;
0449     refcount_inc(&l_ctx->count);
0450     atomic_inc(&l_ctx->io_count);
0451 
0452     /* Initialize the request struct. Initially, we assume a
0453      * long write-back delay. This will be adjusted in
0454      * update_nfs_request below if the region is not locked. */
0455     req->wb_page    = page;
0456     if (page) {
0457         req->wb_index = page_index(page);
0458         get_page(page);
0459     }
0460     req->wb_offset  = offset;
0461     req->wb_pgbase  = pgbase;
0462     req->wb_bytes   = count;
0463     kref_init(&req->wb_kref);
0464     req->wb_nio = 0;
0465     return req;
0466 }
0467 
0468 /**
0469  * nfs_create_request - Create an NFS read/write request.
0470  * @ctx: open context to use
0471  * @page: page to write
0472  * @offset: starting offset within the page for the write
0473  * @count: number of bytes to read/write
0474  *
0475  * The page must be locked by the caller. This makes sure we never
0476  * create two different requests for the same page.
0477  * User should ensure it is safe to sleep in this function.
0478  */
0479 struct nfs_page *
0480 nfs_create_request(struct nfs_open_context *ctx, struct page *page,
0481            unsigned int offset, unsigned int count)
0482 {
0483     struct nfs_lock_context *l_ctx = nfs_get_lock_context(ctx);
0484     struct nfs_page *ret;
0485 
0486     if (IS_ERR(l_ctx))
0487         return ERR_CAST(l_ctx);
0488     ret = __nfs_create_request(l_ctx, page, offset, offset, count);
0489     if (!IS_ERR(ret))
0490         nfs_page_group_init(ret, NULL);
0491     nfs_put_lock_context(l_ctx);
0492     return ret;
0493 }
0494 
0495 static struct nfs_page *
0496 nfs_create_subreq(struct nfs_page *req,
0497           unsigned int pgbase,
0498           unsigned int offset,
0499           unsigned int count)
0500 {
0501     struct nfs_page *last;
0502     struct nfs_page *ret;
0503 
0504     ret = __nfs_create_request(req->wb_lock_context, req->wb_page,
0505             pgbase, offset, count);
0506     if (!IS_ERR(ret)) {
0507         /* find the last request */
0508         for (last = req->wb_head;
0509              last->wb_this_page != req->wb_head;
0510              last = last->wb_this_page)
0511             ;
0512 
0513         nfs_lock_request(ret);
0514         ret->wb_index = req->wb_index;
0515         nfs_page_group_init(ret, last);
0516         ret->wb_nio = req->wb_nio;
0517     }
0518     return ret;
0519 }
0520 
0521 /**
0522  * nfs_unlock_request - Unlock request and wake up sleepers.
0523  * @req: pointer to request
0524  */
0525 void nfs_unlock_request(struct nfs_page *req)
0526 {
0527     clear_bit_unlock(PG_BUSY, &req->wb_flags);
0528     smp_mb__after_atomic();
0529     if (!test_bit(PG_CONTENDED2, &req->wb_flags))
0530         return;
0531     wake_up_bit(&req->wb_flags, PG_BUSY);
0532 }
0533 
0534 /**
0535  * nfs_unlock_and_release_request - Unlock request and release the nfs_page
0536  * @req: pointer to request
0537  */
0538 void nfs_unlock_and_release_request(struct nfs_page *req)
0539 {
0540     nfs_unlock_request(req);
0541     nfs_release_request(req);
0542 }
0543 
0544 /*
0545  * nfs_clear_request - Free up all resources allocated to the request
0546  * @req:
0547  *
0548  * Release page and open context resources associated with a read/write
0549  * request after it has completed.
0550  */
0551 static void nfs_clear_request(struct nfs_page *req)
0552 {
0553     struct page *page = req->wb_page;
0554     struct nfs_lock_context *l_ctx = req->wb_lock_context;
0555     struct nfs_open_context *ctx;
0556 
0557     if (page != NULL) {
0558         put_page(page);
0559         req->wb_page = NULL;
0560     }
0561     if (l_ctx != NULL) {
0562         if (atomic_dec_and_test(&l_ctx->io_count)) {
0563             wake_up_var(&l_ctx->io_count);
0564             ctx = l_ctx->open_context;
0565             if (test_bit(NFS_CONTEXT_UNLOCK, &ctx->flags))
0566                 rpc_wake_up(&NFS_SERVER(d_inode(ctx->dentry))->uoc_rpcwaitq);
0567         }
0568         nfs_put_lock_context(l_ctx);
0569         req->wb_lock_context = NULL;
0570     }
0571 }
0572 
0573 /**
0574  * nfs_free_request - Release the count on an NFS read/write request
0575  * @req: request to release
0576  *
0577  * Note: Should never be called with the spinlock held!
0578  */
0579 void nfs_free_request(struct nfs_page *req)
0580 {
0581     WARN_ON_ONCE(req->wb_this_page != req);
0582 
0583     /* extra debug: make sure no sync bits are still set */
0584     WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
0585     WARN_ON_ONCE(test_bit(PG_UNLOCKPAGE, &req->wb_flags));
0586     WARN_ON_ONCE(test_bit(PG_UPTODATE, &req->wb_flags));
0587     WARN_ON_ONCE(test_bit(PG_WB_END, &req->wb_flags));
0588     WARN_ON_ONCE(test_bit(PG_REMOVE, &req->wb_flags));
0589 
0590     /* Release struct file and open context */
0591     nfs_clear_request(req);
0592     nfs_page_free(req);
0593 }
0594 
0595 void nfs_release_request(struct nfs_page *req)
0596 {
0597     kref_put(&req->wb_kref, nfs_page_group_destroy);
0598 }
0599 EXPORT_SYMBOL_GPL(nfs_release_request);
0600 
0601 /**
0602  * nfs_wait_on_request - Wait for a request to complete.
0603  * @req: request to wait upon.
0604  *
0605  * Interruptible by fatal signals only.
0606  * The user is responsible for holding a count on the request.
0607  */
0608 int
0609 nfs_wait_on_request(struct nfs_page *req)
0610 {
0611     if (!test_bit(PG_BUSY, &req->wb_flags))
0612         return 0;
0613     set_bit(PG_CONTENDED2, &req->wb_flags);
0614     smp_mb__after_atomic();
0615     return wait_on_bit_io(&req->wb_flags, PG_BUSY,
0616                   TASK_UNINTERRUPTIBLE);
0617 }
0618 EXPORT_SYMBOL_GPL(nfs_wait_on_request);
0619 
0620 /*
0621  * nfs_generic_pg_test - determine if requests can be coalesced
0622  * @desc: pointer to descriptor
0623  * @prev: previous request in desc, or NULL
0624  * @req: this request
0625  *
0626  * Returns zero if @req cannot be coalesced into @desc, otherwise it returns
0627  * the size of the request.
0628  */
0629 size_t nfs_generic_pg_test(struct nfs_pageio_descriptor *desc,
0630                struct nfs_page *prev, struct nfs_page *req)
0631 {
0632     struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
0633 
0634 
0635     if (mirror->pg_count > mirror->pg_bsize) {
0636         /* should never happen */
0637         WARN_ON_ONCE(1);
0638         return 0;
0639     }
0640 
0641     /*
0642      * Limit the request size so that we can still allocate a page array
0643      * for it without upsetting the slab allocator.
0644      */
0645     if (((mirror->pg_count + req->wb_bytes) >> PAGE_SHIFT) *
0646             sizeof(struct page *) > PAGE_SIZE)
0647         return 0;
0648 
0649     return min(mirror->pg_bsize - mirror->pg_count, (size_t)req->wb_bytes);
0650 }
0651 EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
0652 
0653 struct nfs_pgio_header *nfs_pgio_header_alloc(const struct nfs_rw_ops *ops)
0654 {
0655     struct nfs_pgio_header *hdr = ops->rw_alloc_header();
0656 
0657     if (hdr) {
0658         INIT_LIST_HEAD(&hdr->pages);
0659         hdr->rw_ops = ops;
0660     }
0661     return hdr;
0662 }
0663 EXPORT_SYMBOL_GPL(nfs_pgio_header_alloc);
0664 
0665 /**
0666  * nfs_pgio_data_destroy - make @hdr suitable for reuse
0667  *
0668  * Frees memory and releases refs from nfs_generic_pgio, so that it may
0669  * be called again.
0670  *
0671  * @hdr: A header that has had nfs_generic_pgio called
0672  */
0673 static void nfs_pgio_data_destroy(struct nfs_pgio_header *hdr)
0674 {
0675     if (hdr->args.context)
0676         put_nfs_open_context(hdr->args.context);
0677     if (hdr->page_array.pagevec != hdr->page_array.page_array)
0678         kfree(hdr->page_array.pagevec);
0679 }
0680 
0681 /*
0682  * nfs_pgio_header_free - Free a read or write header
0683  * @hdr: The header to free
0684  */
0685 void nfs_pgio_header_free(struct nfs_pgio_header *hdr)
0686 {
0687     nfs_pgio_data_destroy(hdr);
0688     hdr->rw_ops->rw_free_header(hdr);
0689 }
0690 EXPORT_SYMBOL_GPL(nfs_pgio_header_free);
0691 
0692 /**
0693  * nfs_pgio_rpcsetup - Set up arguments for a pageio call
0694  * @hdr: The pageio hdr
0695  * @count: Number of bytes to read
0696  * @how: How to commit data (writes only)
0697  * @cinfo: Commit information for the call (writes only)
0698  */
0699 static void nfs_pgio_rpcsetup(struct nfs_pgio_header *hdr,
0700                   unsigned int count,
0701                   int how, struct nfs_commit_info *cinfo)
0702 {
0703     struct nfs_page *req = hdr->req;
0704 
0705     /* Set up the RPC argument and reply structs
0706      * NB: take care not to mess about with hdr->commit et al. */
0707 
0708     hdr->args.fh     = NFS_FH(hdr->inode);
0709     hdr->args.offset = req_offset(req);
0710     /* pnfs_set_layoutcommit needs this */
0711     hdr->mds_offset = hdr->args.offset;
0712     hdr->args.pgbase = req->wb_pgbase;
0713     hdr->args.pages  = hdr->page_array.pagevec;
0714     hdr->args.count  = count;
0715     hdr->args.context = get_nfs_open_context(nfs_req_openctx(req));
0716     hdr->args.lock_context = req->wb_lock_context;
0717     hdr->args.stable  = NFS_UNSTABLE;
0718     switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
0719     case 0:
0720         break;
0721     case FLUSH_COND_STABLE:
0722         if (nfs_reqs_to_commit(cinfo))
0723             break;
0724         fallthrough;
0725     default:
0726         hdr->args.stable = NFS_FILE_SYNC;
0727     }
0728 
0729     hdr->res.fattr   = &hdr->fattr;
0730     hdr->res.count   = 0;
0731     hdr->res.eof     = 0;
0732     hdr->res.verf    = &hdr->verf;
0733     nfs_fattr_init(&hdr->fattr);
0734 }
0735 
0736 /**
0737  * nfs_pgio_prepare - Prepare pageio hdr to go over the wire
0738  * @task: The current task
0739  * @calldata: pageio header to prepare
0740  */
0741 static void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
0742 {
0743     struct nfs_pgio_header *hdr = calldata;
0744     int err;
0745     err = NFS_PROTO(hdr->inode)->pgio_rpc_prepare(task, hdr);
0746     if (err)
0747         rpc_exit(task, err);
0748 }
0749 
0750 int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr,
0751               const struct cred *cred, const struct nfs_rpc_ops *rpc_ops,
0752               const struct rpc_call_ops *call_ops, int how, int flags)
0753 {
0754     struct rpc_task *task;
0755     struct rpc_message msg = {
0756         .rpc_argp = &hdr->args,
0757         .rpc_resp = &hdr->res,
0758         .rpc_cred = cred,
0759     };
0760     struct rpc_task_setup task_setup_data = {
0761         .rpc_client = clnt,
0762         .task = &hdr->task,
0763         .rpc_message = &msg,
0764         .callback_ops = call_ops,
0765         .callback_data = hdr,
0766         .workqueue = nfsiod_workqueue,
0767         .flags = RPC_TASK_ASYNC | flags,
0768     };
0769 
0770     if (nfs_server_capable(hdr->inode, NFS_CAP_MOVEABLE))
0771         task_setup_data.flags |= RPC_TASK_MOVEABLE;
0772 
0773     hdr->rw_ops->rw_initiate(hdr, &msg, rpc_ops, &task_setup_data, how);
0774 
0775     dprintk("NFS: initiated pgio call "
0776         "(req %s/%llu, %u bytes @ offset %llu)\n",
0777         hdr->inode->i_sb->s_id,
0778         (unsigned long long)NFS_FILEID(hdr->inode),
0779         hdr->args.count,
0780         (unsigned long long)hdr->args.offset);
0781 
0782     task = rpc_run_task(&task_setup_data);
0783     if (IS_ERR(task))
0784         return PTR_ERR(task);
0785     rpc_put_task(task);
0786     return 0;
0787 }
0788 EXPORT_SYMBOL_GPL(nfs_initiate_pgio);
0789 
0790 /**
0791  * nfs_pgio_error - Clean up from a pageio error
0792  * @hdr: pageio header
0793  */
0794 static void nfs_pgio_error(struct nfs_pgio_header *hdr)
0795 {
0796     set_bit(NFS_IOHDR_REDO, &hdr->flags);
0797     hdr->completion_ops->completion(hdr);
0798 }
0799 
0800 /**
0801  * nfs_pgio_release - Release pageio data
0802  * @calldata: The pageio header to release
0803  */
0804 static void nfs_pgio_release(void *calldata)
0805 {
0806     struct nfs_pgio_header *hdr = calldata;
0807     hdr->completion_ops->completion(hdr);
0808 }
0809 
0810 static void nfs_pageio_mirror_init(struct nfs_pgio_mirror *mirror,
0811                    unsigned int bsize)
0812 {
0813     INIT_LIST_HEAD(&mirror->pg_list);
0814     mirror->pg_bytes_written = 0;
0815     mirror->pg_count = 0;
0816     mirror->pg_bsize = bsize;
0817     mirror->pg_base = 0;
0818     mirror->pg_recoalesce = 0;
0819 }
0820 
0821 /**
0822  * nfs_pageio_init - initialise a page io descriptor
0823  * @desc: pointer to descriptor
0824  * @inode: pointer to inode
0825  * @pg_ops: pointer to pageio operations
0826  * @compl_ops: pointer to pageio completion operations
0827  * @rw_ops: pointer to nfs read/write operations
0828  * @bsize: io block size
0829  * @io_flags: extra parameters for the io function
0830  */
0831 void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
0832              struct inode *inode,
0833              const struct nfs_pageio_ops *pg_ops,
0834              const struct nfs_pgio_completion_ops *compl_ops,
0835              const struct nfs_rw_ops *rw_ops,
0836              size_t bsize,
0837              int io_flags)
0838 {
0839     desc->pg_moreio = 0;
0840     desc->pg_inode = inode;
0841     desc->pg_ops = pg_ops;
0842     desc->pg_completion_ops = compl_ops;
0843     desc->pg_rw_ops = rw_ops;
0844     desc->pg_ioflags = io_flags;
0845     desc->pg_error = 0;
0846     desc->pg_lseg = NULL;
0847     desc->pg_io_completion = NULL;
0848     desc->pg_dreq = NULL;
0849     desc->pg_bsize = bsize;
0850 
0851     desc->pg_mirror_count = 1;
0852     desc->pg_mirror_idx = 0;
0853 
0854     desc->pg_mirrors_dynamic = NULL;
0855     desc->pg_mirrors = desc->pg_mirrors_static;
0856     nfs_pageio_mirror_init(&desc->pg_mirrors[0], bsize);
0857     desc->pg_maxretrans = 0;
0858 }
0859 
0860 /**
0861  * nfs_pgio_result - Basic pageio error handling
0862  * @task: The task that ran
0863  * @calldata: Pageio header to check
0864  */
0865 static void nfs_pgio_result(struct rpc_task *task, void *calldata)
0866 {
0867     struct nfs_pgio_header *hdr = calldata;
0868     struct inode *inode = hdr->inode;
0869 
0870     if (hdr->rw_ops->rw_done(task, hdr, inode) != 0)
0871         return;
0872     if (task->tk_status < 0)
0873         nfs_set_pgio_error(hdr, task->tk_status, hdr->args.offset);
0874     else
0875         hdr->rw_ops->rw_result(task, hdr);
0876 }
0877 
0878 /*
0879  * Create an RPC task for the given read or write request and kick it.
0880  * The page must have been locked by the caller.
0881  *
0882  * It may happen that the page we're passed is not marked dirty.
0883  * This is the case if nfs_updatepage detects a conflicting request
0884  * that has been written but not committed.
0885  */
0886 int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
0887              struct nfs_pgio_header *hdr)
0888 {
0889     struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
0890 
0891     struct nfs_page     *req;
0892     struct page     **pages,
0893                 *last_page;
0894     struct list_head *head = &mirror->pg_list;
0895     struct nfs_commit_info cinfo;
0896     struct nfs_page_array *pg_array = &hdr->page_array;
0897     unsigned int pagecount, pageused;
0898     gfp_t gfp_flags = nfs_io_gfp_mask();
0899 
0900     pagecount = nfs_page_array_len(mirror->pg_base, mirror->pg_count);
0901     pg_array->npages = pagecount;
0902 
0903     if (pagecount <= ARRAY_SIZE(pg_array->page_array))
0904         pg_array->pagevec = pg_array->page_array;
0905     else {
0906         pg_array->pagevec = kcalloc(pagecount, sizeof(struct page *), gfp_flags);
0907         if (!pg_array->pagevec) {
0908             pg_array->npages = 0;
0909             nfs_pgio_error(hdr);
0910             desc->pg_error = -ENOMEM;
0911             return desc->pg_error;
0912         }
0913     }
0914 
0915     nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
0916     pages = hdr->page_array.pagevec;
0917     last_page = NULL;
0918     pageused = 0;
0919     while (!list_empty(head)) {
0920         req = nfs_list_entry(head->next);
0921         nfs_list_move_request(req, &hdr->pages);
0922 
0923         if (!last_page || last_page != req->wb_page) {
0924             pageused++;
0925             if (pageused > pagecount)
0926                 break;
0927             *pages++ = last_page = req->wb_page;
0928         }
0929     }
0930     if (WARN_ON_ONCE(pageused != pagecount)) {
0931         nfs_pgio_error(hdr);
0932         desc->pg_error = -EINVAL;
0933         return desc->pg_error;
0934     }
0935 
0936     if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
0937         (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
0938         desc->pg_ioflags &= ~FLUSH_COND_STABLE;
0939 
0940     /* Set up the argument struct */
0941     nfs_pgio_rpcsetup(hdr, mirror->pg_count, desc->pg_ioflags, &cinfo);
0942     desc->pg_rpc_callops = &nfs_pgio_common_ops;
0943     return 0;
0944 }
0945 EXPORT_SYMBOL_GPL(nfs_generic_pgio);
0946 
0947 static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
0948 {
0949     struct nfs_pgio_header *hdr;
0950     int ret;
0951     unsigned short task_flags = 0;
0952 
0953     hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
0954     if (!hdr) {
0955         desc->pg_error = -ENOMEM;
0956         return desc->pg_error;
0957     }
0958     nfs_pgheader_init(desc, hdr, nfs_pgio_header_free);
0959     ret = nfs_generic_pgio(desc, hdr);
0960     if (ret == 0) {
0961         if (NFS_SERVER(hdr->inode)->nfs_client->cl_minorversion)
0962             task_flags = RPC_TASK_MOVEABLE;
0963         ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode),
0964                     hdr,
0965                     hdr->cred,
0966                     NFS_PROTO(hdr->inode),
0967                     desc->pg_rpc_callops,
0968                     desc->pg_ioflags,
0969                     RPC_TASK_CRED_NOREF | task_flags);
0970     }
0971     return ret;
0972 }
0973 
0974 static struct nfs_pgio_mirror *
0975 nfs_pageio_alloc_mirrors(struct nfs_pageio_descriptor *desc,
0976         unsigned int mirror_count)
0977 {
0978     struct nfs_pgio_mirror *ret;
0979     unsigned int i;
0980 
0981     kfree(desc->pg_mirrors_dynamic);
0982     desc->pg_mirrors_dynamic = NULL;
0983     if (mirror_count == 1)
0984         return desc->pg_mirrors_static;
0985     ret = kmalloc_array(mirror_count, sizeof(*ret), nfs_io_gfp_mask());
0986     if (ret != NULL) {
0987         for (i = 0; i < mirror_count; i++)
0988             nfs_pageio_mirror_init(&ret[i], desc->pg_bsize);
0989         desc->pg_mirrors_dynamic = ret;
0990     }
0991     return ret;
0992 }
0993 
0994 /*
0995  * nfs_pageio_setup_mirroring - determine if mirroring is to be used
0996  *              by calling the pg_get_mirror_count op
0997  */
0998 static void nfs_pageio_setup_mirroring(struct nfs_pageio_descriptor *pgio,
0999                        struct nfs_page *req)
1000 {
1001     unsigned int mirror_count = 1;
1002 
1003     if (pgio->pg_ops->pg_get_mirror_count)
1004         mirror_count = pgio->pg_ops->pg_get_mirror_count(pgio, req);
1005     if (mirror_count == pgio->pg_mirror_count || pgio->pg_error < 0)
1006         return;
1007 
1008     if (!mirror_count || mirror_count > NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX) {
1009         pgio->pg_error = -EINVAL;
1010         return;
1011     }
1012 
1013     pgio->pg_mirrors = nfs_pageio_alloc_mirrors(pgio, mirror_count);
1014     if (pgio->pg_mirrors == NULL) {
1015         pgio->pg_error = -ENOMEM;
1016         pgio->pg_mirrors = pgio->pg_mirrors_static;
1017         mirror_count = 1;
1018     }
1019     pgio->pg_mirror_count = mirror_count;
1020 }
1021 
1022 static void nfs_pageio_cleanup_mirroring(struct nfs_pageio_descriptor *pgio)
1023 {
1024     pgio->pg_mirror_count = 1;
1025     pgio->pg_mirror_idx = 0;
1026     pgio->pg_mirrors = pgio->pg_mirrors_static;
1027     kfree(pgio->pg_mirrors_dynamic);
1028     pgio->pg_mirrors_dynamic = NULL;
1029 }
1030 
1031 static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
1032         const struct nfs_lock_context *l2)
1033 {
1034     return l1->lockowner == l2->lockowner;
1035 }
1036 
1037 /**
1038  * nfs_coalesce_size - test two requests for compatibility
1039  * @prev: pointer to nfs_page
1040  * @req: pointer to nfs_page
1041  * @pgio: pointer to nfs_pagio_descriptor
1042  *
1043  * The nfs_page structures 'prev' and 'req' are compared to ensure that the
1044  * page data area they describe is contiguous, and that their RPC
1045  * credentials, NFSv4 open state, and lockowners are the same.
1046  *
1047  * Returns size of the request that can be coalesced
1048  */
1049 static unsigned int nfs_coalesce_size(struct nfs_page *prev,
1050                       struct nfs_page *req,
1051                       struct nfs_pageio_descriptor *pgio)
1052 {
1053     struct file_lock_context *flctx;
1054 
1055     if (prev) {
1056         if (!nfs_match_open_context(nfs_req_openctx(req), nfs_req_openctx(prev)))
1057             return 0;
1058         flctx = d_inode(nfs_req_openctx(req)->dentry)->i_flctx;
1059         if (flctx != NULL &&
1060             !(list_empty_careful(&flctx->flc_posix) &&
1061               list_empty_careful(&flctx->flc_flock)) &&
1062             !nfs_match_lock_context(req->wb_lock_context,
1063                         prev->wb_lock_context))
1064             return 0;
1065         if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
1066             return 0;
1067         if (req->wb_page == prev->wb_page) {
1068             if (req->wb_pgbase != prev->wb_pgbase + prev->wb_bytes)
1069                 return 0;
1070         } else {
1071             if (req->wb_pgbase != 0 ||
1072                 prev->wb_pgbase + prev->wb_bytes != PAGE_SIZE)
1073                 return 0;
1074         }
1075     }
1076     return pgio->pg_ops->pg_test(pgio, prev, req);
1077 }
1078 
1079 /**
1080  * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
1081  * @desc: destination io descriptor
1082  * @req: request
1083  *
1084  * If the request 'req' was successfully coalesced into the existing list
1085  * of pages 'desc', it returns the size of req.
1086  */
1087 static unsigned int
1088 nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
1089         struct nfs_page *req)
1090 {
1091     struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
1092     struct nfs_page *prev = NULL;
1093     unsigned int size;
1094 
1095     if (list_empty(&mirror->pg_list)) {
1096         if (desc->pg_ops->pg_init)
1097             desc->pg_ops->pg_init(desc, req);
1098         if (desc->pg_error < 0)
1099             return 0;
1100         mirror->pg_base = req->wb_pgbase;
1101         mirror->pg_count = 0;
1102         mirror->pg_recoalesce = 0;
1103     } else
1104         prev = nfs_list_entry(mirror->pg_list.prev);
1105 
1106     if (desc->pg_maxretrans && req->wb_nio > desc->pg_maxretrans) {
1107         if (NFS_SERVER(desc->pg_inode)->flags & NFS_MOUNT_SOFTERR)
1108             desc->pg_error = -ETIMEDOUT;
1109         else
1110             desc->pg_error = -EIO;
1111         return 0;
1112     }
1113 
1114     size = nfs_coalesce_size(prev, req, desc);
1115     if (size < req->wb_bytes)
1116         return size;
1117     nfs_list_move_request(req, &mirror->pg_list);
1118     mirror->pg_count += req->wb_bytes;
1119     return req->wb_bytes;
1120 }
1121 
1122 /*
1123  * Helper for nfs_pageio_add_request and nfs_pageio_complete
1124  */
1125 static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
1126 {
1127     struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
1128 
1129     if (!list_empty(&mirror->pg_list)) {
1130         int error = desc->pg_ops->pg_doio(desc);
1131         if (error < 0)
1132             desc->pg_error = error;
1133         if (list_empty(&mirror->pg_list))
1134             mirror->pg_bytes_written += mirror->pg_count;
1135     }
1136 }
1137 
1138 static void
1139 nfs_pageio_cleanup_request(struct nfs_pageio_descriptor *desc,
1140         struct nfs_page *req)
1141 {
1142     LIST_HEAD(head);
1143 
1144     nfs_list_move_request(req, &head);
1145     desc->pg_completion_ops->error_cleanup(&head, desc->pg_error);
1146 }
1147 
1148 /**
1149  * __nfs_pageio_add_request - Attempt to coalesce a request into a page list.
1150  * @desc: destination io descriptor
1151  * @req: request
1152  *
1153  * This may split a request into subrequests which are all part of the
1154  * same page group. If so, it will submit @req as the last one, to ensure
1155  * the pointer to @req is still valid in case of failure.
1156  *
1157  * Returns true if the request 'req' was successfully coalesced into the
1158  * existing list of pages 'desc'.
1159  */
1160 static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
1161                struct nfs_page *req)
1162 {
1163     struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
1164     struct nfs_page *subreq;
1165     unsigned int size, subreq_size;
1166 
1167     nfs_page_group_lock(req);
1168 
1169     subreq = req;
1170     subreq_size = subreq->wb_bytes;
1171     for(;;) {
1172         size = nfs_pageio_do_add_request(desc, subreq);
1173         if (size == subreq_size) {
1174             /* We successfully submitted a request */
1175             if (subreq == req)
1176                 break;
1177             req->wb_pgbase += size;
1178             req->wb_bytes -= size;
1179             req->wb_offset += size;
1180             subreq_size = req->wb_bytes;
1181             subreq = req;
1182             continue;
1183         }
1184         if (WARN_ON_ONCE(subreq != req)) {
1185             nfs_page_group_unlock(req);
1186             nfs_pageio_cleanup_request(desc, subreq);
1187             subreq = req;
1188             subreq_size = req->wb_bytes;
1189             nfs_page_group_lock(req);
1190         }
1191         if (!size) {
1192             /* Can't coalesce any more, so do I/O */
1193             nfs_page_group_unlock(req);
1194             desc->pg_moreio = 1;
1195             nfs_pageio_doio(desc);
1196             if (desc->pg_error < 0 || mirror->pg_recoalesce)
1197                 return 0;
1198             /* retry add_request for this subreq */
1199             nfs_page_group_lock(req);
1200             continue;
1201         }
1202         subreq = nfs_create_subreq(req, req->wb_pgbase,
1203                 req->wb_offset, size);
1204         if (IS_ERR(subreq))
1205             goto err_ptr;
1206         subreq_size = size;
1207     }
1208 
1209     nfs_page_group_unlock(req);
1210     return 1;
1211 err_ptr:
1212     desc->pg_error = PTR_ERR(subreq);
1213     nfs_page_group_unlock(req);
1214     return 0;
1215 }
1216 
1217 static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
1218 {
1219     struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
1220     LIST_HEAD(head);
1221 
1222     do {
1223         list_splice_init(&mirror->pg_list, &head);
1224         mirror->pg_recoalesce = 0;
1225 
1226         while (!list_empty(&head)) {
1227             struct nfs_page *req;
1228 
1229             req = list_first_entry(&head, struct nfs_page, wb_list);
1230             if (__nfs_pageio_add_request(desc, req))
1231                 continue;
1232             if (desc->pg_error < 0) {
1233                 list_splice_tail(&head, &mirror->pg_list);
1234                 mirror->pg_recoalesce = 1;
1235                 return 0;
1236             }
1237             break;
1238         }
1239     } while (mirror->pg_recoalesce);
1240     return 1;
1241 }
1242 
1243 static int nfs_pageio_add_request_mirror(struct nfs_pageio_descriptor *desc,
1244         struct nfs_page *req)
1245 {
1246     int ret;
1247 
1248     do {
1249         ret = __nfs_pageio_add_request(desc, req);
1250         if (ret)
1251             break;
1252         if (desc->pg_error < 0)
1253             break;
1254         ret = nfs_do_recoalesce(desc);
1255     } while (ret);
1256 
1257     return ret;
1258 }
1259 
1260 static void nfs_pageio_error_cleanup(struct nfs_pageio_descriptor *desc)
1261 {
1262     u32 midx;
1263     struct nfs_pgio_mirror *mirror;
1264 
1265     if (!desc->pg_error)
1266         return;
1267 
1268     for (midx = 0; midx < desc->pg_mirror_count; midx++) {
1269         mirror = nfs_pgio_get_mirror(desc, midx);
1270         desc->pg_completion_ops->error_cleanup(&mirror->pg_list,
1271                 desc->pg_error);
1272     }
1273 }
1274 
1275 int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
1276                struct nfs_page *req)
1277 {
1278     u32 midx;
1279     unsigned int pgbase, offset, bytes;
1280     struct nfs_page *dupreq;
1281 
1282     pgbase = req->wb_pgbase;
1283     offset = req->wb_offset;
1284     bytes = req->wb_bytes;
1285 
1286     nfs_pageio_setup_mirroring(desc, req);
1287     if (desc->pg_error < 0)
1288         goto out_failed;
1289 
1290     /* Create the mirror instances first, and fire them off */
1291     for (midx = 1; midx < desc->pg_mirror_count; midx++) {
1292         nfs_page_group_lock(req);
1293 
1294         dupreq = nfs_create_subreq(req,
1295                 pgbase, offset, bytes);
1296 
1297         nfs_page_group_unlock(req);
1298         if (IS_ERR(dupreq)) {
1299             desc->pg_error = PTR_ERR(dupreq);
1300             goto out_failed;
1301         }
1302 
1303         nfs_pgio_set_current_mirror(desc, midx);
1304         if (!nfs_pageio_add_request_mirror(desc, dupreq))
1305             goto out_cleanup_subreq;
1306     }
1307 
1308     nfs_pgio_set_current_mirror(desc, 0);
1309     if (!nfs_pageio_add_request_mirror(desc, req))
1310         goto out_failed;
1311 
1312     return 1;
1313 
1314 out_cleanup_subreq:
1315     nfs_pageio_cleanup_request(desc, dupreq);
1316 out_failed:
1317     nfs_pageio_error_cleanup(desc);
1318     return 0;
1319 }
1320 
1321 /*
1322  * nfs_pageio_complete_mirror - Complete I/O on the current mirror of an
1323  *              nfs_pageio_descriptor
1324  * @desc: pointer to io descriptor
1325  * @mirror_idx: pointer to mirror index
1326  */
1327 static void nfs_pageio_complete_mirror(struct nfs_pageio_descriptor *desc,
1328                        u32 mirror_idx)
1329 {
1330     struct nfs_pgio_mirror *mirror;
1331     u32 restore_idx;
1332 
1333     restore_idx = nfs_pgio_set_current_mirror(desc, mirror_idx);
1334     mirror = nfs_pgio_current_mirror(desc);
1335 
1336     for (;;) {
1337         nfs_pageio_doio(desc);
1338         if (desc->pg_error < 0 || !mirror->pg_recoalesce)
1339             break;
1340         if (!nfs_do_recoalesce(desc))
1341             break;
1342     }
1343     nfs_pgio_set_current_mirror(desc, restore_idx);
1344 }
1345 
1346 /*
1347  * nfs_pageio_resend - Transfer requests to new descriptor and resend
1348  * @hdr - the pgio header to move request from
1349  * @desc - the pageio descriptor to add requests to
1350  *
1351  * Try to move each request (nfs_page) from @hdr to @desc then attempt
1352  * to send them.
1353  *
1354  * Returns 0 on success and < 0 on error.
1355  */
1356 int nfs_pageio_resend(struct nfs_pageio_descriptor *desc,
1357               struct nfs_pgio_header *hdr)
1358 {
1359     LIST_HEAD(pages);
1360 
1361     desc->pg_io_completion = hdr->io_completion;
1362     desc->pg_dreq = hdr->dreq;
1363     list_splice_init(&hdr->pages, &pages);
1364     while (!list_empty(&pages)) {
1365         struct nfs_page *req = nfs_list_entry(pages.next);
1366 
1367         if (!nfs_pageio_add_request(desc, req))
1368             break;
1369     }
1370     nfs_pageio_complete(desc);
1371     if (!list_empty(&pages)) {
1372         int err = desc->pg_error < 0 ? desc->pg_error : -EIO;
1373         hdr->completion_ops->error_cleanup(&pages, err);
1374         nfs_set_pgio_error(hdr, err, hdr->io_start);
1375         return err;
1376     }
1377     return 0;
1378 }
1379 EXPORT_SYMBOL_GPL(nfs_pageio_resend);
1380 
1381 /**
1382  * nfs_pageio_complete - Complete I/O then cleanup an nfs_pageio_descriptor
1383  * @desc: pointer to io descriptor
1384  */
1385 void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
1386 {
1387     u32 midx;
1388 
1389     for (midx = 0; midx < desc->pg_mirror_count; midx++)
1390         nfs_pageio_complete_mirror(desc, midx);
1391 
1392     if (desc->pg_error < 0)
1393         nfs_pageio_error_cleanup(desc);
1394     if (desc->pg_ops->pg_cleanup)
1395         desc->pg_ops->pg_cleanup(desc);
1396     nfs_pageio_cleanup_mirroring(desc);
1397 }
1398 
1399 /**
1400  * nfs_pageio_cond_complete - Conditional I/O completion
1401  * @desc: pointer to io descriptor
1402  * @index: page index
1403  *
1404  * It is important to ensure that processes don't try to take locks
1405  * on non-contiguous ranges of pages as that might deadlock. This
1406  * function should be called before attempting to wait on a locked
1407  * nfs_page. It will complete the I/O if the page index 'index'
1408  * is not contiguous with the existing list of pages in 'desc'.
1409  */
1410 void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
1411 {
1412     struct nfs_pgio_mirror *mirror;
1413     struct nfs_page *prev;
1414     u32 midx;
1415 
1416     for (midx = 0; midx < desc->pg_mirror_count; midx++) {
1417         mirror = nfs_pgio_get_mirror(desc, midx);
1418         if (!list_empty(&mirror->pg_list)) {
1419             prev = nfs_list_entry(mirror->pg_list.prev);
1420             if (index != prev->wb_index + 1) {
1421                 nfs_pageio_complete(desc);
1422                 break;
1423             }
1424         }
1425     }
1426 }
1427 
1428 /*
1429  * nfs_pageio_stop_mirroring - stop using mirroring (set mirror count to 1)
1430  */
1431 void nfs_pageio_stop_mirroring(struct nfs_pageio_descriptor *pgio)
1432 {
1433     nfs_pageio_complete(pgio);
1434 }
1435 
1436 int __init nfs_init_nfspagecache(void)
1437 {
1438     nfs_page_cachep = kmem_cache_create("nfs_page",
1439                         sizeof(struct nfs_page),
1440                         0, SLAB_HWCACHE_ALIGN,
1441                         NULL);
1442     if (nfs_page_cachep == NULL)
1443         return -ENOMEM;
1444 
1445     return 0;
1446 }
1447 
1448 void nfs_destroy_nfspagecache(void)
1449 {
1450     kmem_cache_destroy(nfs_page_cachep);
1451 }
1452 
1453 static const struct rpc_call_ops nfs_pgio_common_ops = {
1454     .rpc_call_prepare = nfs_pgio_prepare,
1455     .rpc_call_done = nfs_pgio_result,
1456     .rpc_release = nfs_pgio_release,
1457 };
1458 
1459 const struct nfs_pageio_ops nfs_pgio_rw_ops = {
1460     .pg_test = nfs_generic_pg_test,
1461     .pg_doio = nfs_generic_pg_pgios,
1462 };