1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/fs/nfs/pagelist.c
5 * A set of helper functions for managing NFS read and write requests.
6 * The main purpose of these routines is to provide support for the
7 * coalescing of several requests into a single RPC call.
9 * Copyright 2000, 2001 (c) Trond Myklebust <trond.myklebust@fys.uio.no>
13 #include <linux/slab.h>
14 #include <linux/file.h>
15 #include <linux/sched.h>
16 #include <linux/sunrpc/clnt.h>
17 #include <linux/nfs.h>
18 #include <linux/nfs3.h>
19 #include <linux/nfs4.h>
20 #include <linux/nfs_fs.h>
21 #include <linux/nfs_page.h>
22 #include <linux/nfs_mount.h>
23 #include <linux/export.h>
28 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
30 static struct kmem_cache *nfs_page_cachep;
31 static const struct rpc_call_ops nfs_pgio_common_ops;
33 struct nfs_pgio_mirror *
34 nfs_pgio_current_mirror(struct nfs_pageio_descriptor *desc)
36 return nfs_pgio_has_mirroring(desc) ?
37 &desc->pg_mirrors[desc->pg_mirror_idx] :
40 EXPORT_SYMBOL_GPL(nfs_pgio_current_mirror);
42 void nfs_pgheader_init(struct nfs_pageio_descriptor *desc,
43 struct nfs_pgio_header *hdr,
44 void (*release)(struct nfs_pgio_header *hdr))
46 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
49 hdr->req = nfs_list_entry(mirror->pg_list.next);
50 hdr->inode = desc->pg_inode;
51 hdr->cred = nfs_req_openctx(hdr->req)->cred;
52 hdr->io_start = req_offset(hdr->req);
53 hdr->good_bytes = mirror->pg_count;
54 hdr->io_completion = desc->pg_io_completion;
55 hdr->dreq = desc->pg_dreq;
56 hdr->release = release;
57 hdr->completion_ops = desc->pg_completion_ops;
58 if (hdr->completion_ops->init_hdr)
59 hdr->completion_ops->init_hdr(hdr);
61 hdr->pgio_mirror_idx = desc->pg_mirror_idx;
63 EXPORT_SYMBOL_GPL(nfs_pgheader_init);
65 void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos)
67 unsigned int new = pos - hdr->io_start;
69 if (hdr->good_bytes > new) {
70 hdr->good_bytes = new;
71 clear_bit(NFS_IOHDR_EOF, &hdr->flags);
72 if (!test_and_set_bit(NFS_IOHDR_ERROR, &hdr->flags))
77 static inline struct nfs_page *
80 struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_KERNEL);
82 INIT_LIST_HEAD(&p->wb_list);
87 nfs_page_free(struct nfs_page *p)
89 kmem_cache_free(nfs_page_cachep, p);
93 * nfs_iocounter_wait - wait for i/o to complete
94 * @l_ctx: nfs_lock_context with io_counter to use
96 * returns -ERESTARTSYS if interrupted by a fatal signal.
97 * Otherwise returns 0 once the io_count hits 0.
100 nfs_iocounter_wait(struct nfs_lock_context *l_ctx)
102 return wait_var_event_killable(&l_ctx->io_count,
103 !atomic_read(&l_ctx->io_count));
107 * nfs_async_iocounter_wait - wait on a rpc_waitqueue for I/O
109 * @task: the rpc_task that should wait
110 * @l_ctx: nfs_lock_context with io_counter to check
112 * Returns true if there is outstanding I/O to wait on and the
113 * task has been put to sleep.
116 nfs_async_iocounter_wait(struct rpc_task *task, struct nfs_lock_context *l_ctx)
118 struct inode *inode = d_inode(l_ctx->open_context->dentry);
121 if (atomic_read(&l_ctx->io_count) > 0) {
122 rpc_sleep_on(&NFS_SERVER(inode)->uoc_rpcwaitq, task, NULL);
126 if (atomic_read(&l_ctx->io_count) == 0) {
127 rpc_wake_up_queued_task(&NFS_SERVER(inode)->uoc_rpcwaitq, task);
133 EXPORT_SYMBOL_GPL(nfs_async_iocounter_wait);
136 * nfs_page_set_headlock - set the request PG_HEADLOCK
137 * @req: request that is to be locked
139 * this lock must be held when modifying req->wb_head
141 * return 0 on success, < 0 on error
144 nfs_page_set_headlock(struct nfs_page *req)
146 if (!test_and_set_bit(PG_HEADLOCK, &req->wb_flags))
149 set_bit(PG_CONTENDED1, &req->wb_flags);
150 smp_mb__after_atomic();
151 return wait_on_bit_lock(&req->wb_flags, PG_HEADLOCK,
152 TASK_UNINTERRUPTIBLE);
156 * nfs_page_clear_headlock - clear the request PG_HEADLOCK
157 * @req: request that is to be locked
160 nfs_page_clear_headlock(struct nfs_page *req)
162 smp_mb__before_atomic();
163 clear_bit(PG_HEADLOCK, &req->wb_flags);
164 smp_mb__after_atomic();
165 if (!test_bit(PG_CONTENDED1, &req->wb_flags))
167 wake_up_bit(&req->wb_flags, PG_HEADLOCK);
171 * nfs_page_group_lock - lock the head of the page group
172 * @req: request in group that is to be locked
174 * this lock must be held when traversing or modifying the page
177 * return 0 on success, < 0 on error
180 nfs_page_group_lock(struct nfs_page *req)
184 ret = nfs_page_set_headlock(req);
185 if (ret || req->wb_head == req)
187 return nfs_page_set_headlock(req->wb_head);
191 * nfs_page_group_unlock - unlock the head of the page group
192 * @req: request in group that is to be unlocked
195 nfs_page_group_unlock(struct nfs_page *req)
197 if (req != req->wb_head)
198 nfs_page_clear_headlock(req->wb_head);
199 nfs_page_clear_headlock(req);
203 * nfs_page_group_sync_on_bit_locked
205 * must be called with page group lock held
208 nfs_page_group_sync_on_bit_locked(struct nfs_page *req, unsigned int bit)
210 struct nfs_page *head = req->wb_head;
211 struct nfs_page *tmp;
213 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_flags));
214 WARN_ON_ONCE(test_and_set_bit(bit, &req->wb_flags));
216 tmp = req->wb_this_page;
218 if (!test_bit(bit, &tmp->wb_flags))
220 tmp = tmp->wb_this_page;
223 /* true! reset all bits */
226 clear_bit(bit, &tmp->wb_flags);
227 tmp = tmp->wb_this_page;
228 } while (tmp != req);
234 * nfs_page_group_sync_on_bit - set bit on current request, but only
235 * return true if the bit is set for all requests in page group
236 * @req - request in page group
237 * @bit - PG_* bit that is used to sync page group
239 bool nfs_page_group_sync_on_bit(struct nfs_page *req, unsigned int bit)
243 nfs_page_group_lock(req);
244 ret = nfs_page_group_sync_on_bit_locked(req, bit);
245 nfs_page_group_unlock(req);
251 * nfs_page_group_init - Initialize the page group linkage for @req
252 * @req - a new nfs request
253 * @prev - the previous request in page group, or NULL if @req is the first
254 * or only request in the group (the head).
257 nfs_page_group_init(struct nfs_page *req, struct nfs_page *prev)
260 WARN_ON_ONCE(prev == req);
265 req->wb_this_page = req;
268 WARN_ON_ONCE(prev->wb_this_page != prev->wb_head);
269 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &prev->wb_head->wb_flags));
270 req->wb_head = prev->wb_head;
271 req->wb_this_page = prev->wb_this_page;
272 prev->wb_this_page = req;
274 /* All subrequests take a ref on the head request until
275 * nfs_page_group_destroy is called */
276 kref_get(&req->wb_head->wb_kref);
278 /* grab extra ref and bump the request count if head request
279 * has extra ref from the write/commit path to handle handoff
280 * between write and commit lists. */
281 if (test_bit(PG_INODE_REF, &prev->wb_head->wb_flags)) {
282 inode = page_file_mapping(req->wb_page)->host;
283 set_bit(PG_INODE_REF, &req->wb_flags);
284 kref_get(&req->wb_kref);
285 atomic_long_inc(&NFS_I(inode)->nrequests);
291 * nfs_page_group_destroy - sync the destruction of page groups
292 * @req - request that no longer needs the page group
294 * releases the page group reference from each member once all
295 * members have called this function.
298 nfs_page_group_destroy(struct kref *kref)
300 struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
301 struct nfs_page *head = req->wb_head;
302 struct nfs_page *tmp, *next;
304 if (!nfs_page_group_sync_on_bit(req, PG_TEARDOWN))
309 next = tmp->wb_this_page;
310 /* unlink and free */
311 tmp->wb_this_page = tmp;
313 nfs_free_request(tmp);
315 } while (tmp != req);
317 /* subrequests must release the ref on the head request */
319 nfs_release_request(head);
322 static struct nfs_page *
323 __nfs_create_request(struct nfs_lock_context *l_ctx, struct page *page,
324 unsigned int pgbase, unsigned int offset,
327 struct nfs_page *req;
328 struct nfs_open_context *ctx = l_ctx->open_context;
330 if (test_bit(NFS_CONTEXT_BAD, &ctx->flags))
331 return ERR_PTR(-EBADF);
332 /* try to allocate the request struct */
333 req = nfs_page_alloc();
335 return ERR_PTR(-ENOMEM);
337 req->wb_lock_context = l_ctx;
338 refcount_inc(&l_ctx->count);
339 atomic_inc(&l_ctx->io_count);
341 /* Initialize the request struct. Initially, we assume a
342 * long write-back delay. This will be adjusted in
343 * update_nfs_request below if the region is not locked. */
346 req->wb_index = page_index(page);
349 req->wb_offset = offset;
350 req->wb_pgbase = pgbase;
351 req->wb_bytes = count;
352 kref_init(&req->wb_kref);
358 * nfs_create_request - Create an NFS read/write request.
359 * @ctx: open context to use
360 * @page: page to write
361 * @offset: starting offset within the page for the write
362 * @count: number of bytes to read/write
364 * The page must be locked by the caller. This makes sure we never
365 * create two different requests for the same page.
366 * User should ensure it is safe to sleep in this function.
369 nfs_create_request(struct nfs_open_context *ctx, struct page *page,
370 unsigned int offset, unsigned int count)
372 struct nfs_lock_context *l_ctx = nfs_get_lock_context(ctx);
373 struct nfs_page *ret;
376 return ERR_CAST(l_ctx);
377 ret = __nfs_create_request(l_ctx, page, offset, offset, count);
379 nfs_page_group_init(ret, NULL);
380 nfs_put_lock_context(l_ctx);
384 static struct nfs_page *
385 nfs_create_subreq(struct nfs_page *req, struct nfs_page *last,
386 unsigned int pgbase, unsigned int offset,
389 struct nfs_page *ret;
391 ret = __nfs_create_request(req->wb_lock_context, req->wb_page,
392 pgbase, offset, count);
394 nfs_lock_request(ret);
395 ret->wb_index = req->wb_index;
396 nfs_page_group_init(ret, last);
397 ret->wb_nio = req->wb_nio;
403 * nfs_unlock_request - Unlock request and wake up sleepers.
404 * @req: pointer to request
406 void nfs_unlock_request(struct nfs_page *req)
408 if (!NFS_WBACK_BUSY(req)) {
409 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
412 smp_mb__before_atomic();
413 clear_bit(PG_BUSY, &req->wb_flags);
414 smp_mb__after_atomic();
415 if (!test_bit(PG_CONTENDED2, &req->wb_flags))
417 wake_up_bit(&req->wb_flags, PG_BUSY);
421 * nfs_unlock_and_release_request - Unlock request and release the nfs_page
422 * @req: pointer to request
424 void nfs_unlock_and_release_request(struct nfs_page *req)
426 nfs_unlock_request(req);
427 nfs_release_request(req);
431 * nfs_clear_request - Free up all resources allocated to the request
434 * Release page and open context resources associated with a read/write
435 * request after it has completed.
437 static void nfs_clear_request(struct nfs_page *req)
439 struct page *page = req->wb_page;
440 struct nfs_lock_context *l_ctx = req->wb_lock_context;
441 struct nfs_open_context *ctx;
448 if (atomic_dec_and_test(&l_ctx->io_count)) {
449 wake_up_var(&l_ctx->io_count);
450 ctx = l_ctx->open_context;
451 if (test_bit(NFS_CONTEXT_UNLOCK, &ctx->flags))
452 rpc_wake_up(&NFS_SERVER(d_inode(ctx->dentry))->uoc_rpcwaitq);
454 nfs_put_lock_context(l_ctx);
455 req->wb_lock_context = NULL;
460 * nfs_release_request - Release the count on an NFS read/write request
461 * @req: request to release
463 * Note: Should never be called with the spinlock held!
465 void nfs_free_request(struct nfs_page *req)
467 WARN_ON_ONCE(req->wb_this_page != req);
469 /* extra debug: make sure no sync bits are still set */
470 WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
471 WARN_ON_ONCE(test_bit(PG_UNLOCKPAGE, &req->wb_flags));
472 WARN_ON_ONCE(test_bit(PG_UPTODATE, &req->wb_flags));
473 WARN_ON_ONCE(test_bit(PG_WB_END, &req->wb_flags));
474 WARN_ON_ONCE(test_bit(PG_REMOVE, &req->wb_flags));
476 /* Release struct file and open context */
477 nfs_clear_request(req);
481 void nfs_release_request(struct nfs_page *req)
483 kref_put(&req->wb_kref, nfs_page_group_destroy);
485 EXPORT_SYMBOL_GPL(nfs_release_request);
488 * nfs_wait_on_request - Wait for a request to complete.
489 * @req: request to wait upon.
491 * Interruptible by fatal signals only.
492 * The user is responsible for holding a count on the request.
495 nfs_wait_on_request(struct nfs_page *req)
497 if (!test_bit(PG_BUSY, &req->wb_flags))
499 set_bit(PG_CONTENDED2, &req->wb_flags);
500 smp_mb__after_atomic();
501 return wait_on_bit_io(&req->wb_flags, PG_BUSY,
502 TASK_UNINTERRUPTIBLE);
504 EXPORT_SYMBOL_GPL(nfs_wait_on_request);
507 * nfs_generic_pg_test - determine if requests can be coalesced
508 * @desc: pointer to descriptor
509 * @prev: previous request in desc, or NULL
512 * Returns zero if @req cannot be coalesced into @desc, otherwise it returns
513 * the size of the request.
515 size_t nfs_generic_pg_test(struct nfs_pageio_descriptor *desc,
516 struct nfs_page *prev, struct nfs_page *req)
518 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
521 if (mirror->pg_count > mirror->pg_bsize) {
522 /* should never happen */
528 * Limit the request size so that we can still allocate a page array
529 * for it without upsetting the slab allocator.
531 if (((mirror->pg_count + req->wb_bytes) >> PAGE_SHIFT) *
532 sizeof(struct page *) > PAGE_SIZE)
535 return min(mirror->pg_bsize - mirror->pg_count, (size_t)req->wb_bytes);
537 EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
539 struct nfs_pgio_header *nfs_pgio_header_alloc(const struct nfs_rw_ops *ops)
541 struct nfs_pgio_header *hdr = ops->rw_alloc_header();
544 INIT_LIST_HEAD(&hdr->pages);
549 EXPORT_SYMBOL_GPL(nfs_pgio_header_alloc);
552 * nfs_pgio_data_destroy - make @hdr suitable for reuse
554 * Frees memory and releases refs from nfs_generic_pgio, so that it may
557 * @hdr: A header that has had nfs_generic_pgio called
559 static void nfs_pgio_data_destroy(struct nfs_pgio_header *hdr)
561 if (hdr->args.context)
562 put_nfs_open_context(hdr->args.context);
563 if (hdr->page_array.pagevec != hdr->page_array.page_array)
564 kfree(hdr->page_array.pagevec);
568 * nfs_pgio_header_free - Free a read or write header
569 * @hdr: The header to free
571 void nfs_pgio_header_free(struct nfs_pgio_header *hdr)
573 nfs_pgio_data_destroy(hdr);
574 hdr->rw_ops->rw_free_header(hdr);
576 EXPORT_SYMBOL_GPL(nfs_pgio_header_free);
579 * nfs_pgio_rpcsetup - Set up arguments for a pageio call
580 * @hdr: The pageio hdr
581 * @count: Number of bytes to read
582 * @how: How to commit data (writes only)
583 * @cinfo: Commit information for the call (writes only)
585 static void nfs_pgio_rpcsetup(struct nfs_pgio_header *hdr,
587 int how, struct nfs_commit_info *cinfo)
589 struct nfs_page *req = hdr->req;
591 /* Set up the RPC argument and reply structs
592 * NB: take care not to mess about with hdr->commit et al. */
594 hdr->args.fh = NFS_FH(hdr->inode);
595 hdr->args.offset = req_offset(req);
596 /* pnfs_set_layoutcommit needs this */
597 hdr->mds_offset = hdr->args.offset;
598 hdr->args.pgbase = req->wb_pgbase;
599 hdr->args.pages = hdr->page_array.pagevec;
600 hdr->args.count = count;
601 hdr->args.context = get_nfs_open_context(nfs_req_openctx(req));
602 hdr->args.lock_context = req->wb_lock_context;
603 hdr->args.stable = NFS_UNSTABLE;
604 switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
607 case FLUSH_COND_STABLE:
608 if (nfs_reqs_to_commit(cinfo))
612 hdr->args.stable = NFS_FILE_SYNC;
615 hdr->res.fattr = &hdr->fattr;
618 hdr->res.verf = &hdr->verf;
619 nfs_fattr_init(&hdr->fattr);
623 * nfs_pgio_prepare - Prepare pageio hdr to go over the wire
624 * @task: The current task
625 * @calldata: pageio header to prepare
627 static void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
629 struct nfs_pgio_header *hdr = calldata;
631 err = NFS_PROTO(hdr->inode)->pgio_rpc_prepare(task, hdr);
636 int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr,
637 const struct cred *cred, const struct nfs_rpc_ops *rpc_ops,
638 const struct rpc_call_ops *call_ops, int how, int flags)
640 struct rpc_task *task;
641 struct rpc_message msg = {
642 .rpc_argp = &hdr->args,
643 .rpc_resp = &hdr->res,
646 struct rpc_task_setup task_setup_data = {
650 .callback_ops = call_ops,
651 .callback_data = hdr,
652 .workqueue = nfsiod_workqueue,
653 .flags = RPC_TASK_ASYNC | flags,
657 hdr->rw_ops->rw_initiate(hdr, &msg, rpc_ops, &task_setup_data, how);
659 dprintk("NFS: initiated pgio call "
660 "(req %s/%llu, %u bytes @ offset %llu)\n",
661 hdr->inode->i_sb->s_id,
662 (unsigned long long)NFS_FILEID(hdr->inode),
664 (unsigned long long)hdr->args.offset);
666 task = rpc_run_task(&task_setup_data);
671 if (how & FLUSH_SYNC) {
672 ret = rpc_wait_for_completion_task(task);
674 ret = task->tk_status;
680 EXPORT_SYMBOL_GPL(nfs_initiate_pgio);
683 * nfs_pgio_error - Clean up from a pageio error
684 * @hdr: pageio header
686 static void nfs_pgio_error(struct nfs_pgio_header *hdr)
688 set_bit(NFS_IOHDR_REDO, &hdr->flags);
689 hdr->completion_ops->completion(hdr);
693 * nfs_pgio_release - Release pageio data
694 * @calldata: The pageio header to release
696 static void nfs_pgio_release(void *calldata)
698 struct nfs_pgio_header *hdr = calldata;
699 hdr->completion_ops->completion(hdr);
702 static void nfs_pageio_mirror_init(struct nfs_pgio_mirror *mirror,
705 INIT_LIST_HEAD(&mirror->pg_list);
706 mirror->pg_bytes_written = 0;
707 mirror->pg_count = 0;
708 mirror->pg_bsize = bsize;
710 mirror->pg_recoalesce = 0;
714 * nfs_pageio_init - initialise a page io descriptor
715 * @desc: pointer to descriptor
716 * @inode: pointer to inode
717 * @pg_ops: pointer to pageio operations
718 * @compl_ops: pointer to pageio completion operations
719 * @rw_ops: pointer to nfs read/write operations
720 * @bsize: io block size
721 * @io_flags: extra parameters for the io function
723 void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
725 const struct nfs_pageio_ops *pg_ops,
726 const struct nfs_pgio_completion_ops *compl_ops,
727 const struct nfs_rw_ops *rw_ops,
732 desc->pg_inode = inode;
733 desc->pg_ops = pg_ops;
734 desc->pg_completion_ops = compl_ops;
735 desc->pg_rw_ops = rw_ops;
736 desc->pg_ioflags = io_flags;
738 desc->pg_lseg = NULL;
739 desc->pg_io_completion = NULL;
740 desc->pg_dreq = NULL;
741 desc->pg_bsize = bsize;
743 desc->pg_mirror_count = 1;
744 desc->pg_mirror_idx = 0;
746 desc->pg_mirrors_dynamic = NULL;
747 desc->pg_mirrors = desc->pg_mirrors_static;
748 nfs_pageio_mirror_init(&desc->pg_mirrors[0], bsize);
749 desc->pg_maxretrans = 0;
753 * nfs_pgio_result - Basic pageio error handling
754 * @task: The task that ran
755 * @calldata: Pageio header to check
757 static void nfs_pgio_result(struct rpc_task *task, void *calldata)
759 struct nfs_pgio_header *hdr = calldata;
760 struct inode *inode = hdr->inode;
762 dprintk("NFS: %s: %5u, (status %d)\n", __func__,
763 task->tk_pid, task->tk_status);
765 if (hdr->rw_ops->rw_done(task, hdr, inode) != 0)
767 if (task->tk_status < 0)
768 nfs_set_pgio_error(hdr, task->tk_status, hdr->args.offset);
770 hdr->rw_ops->rw_result(task, hdr);
774 * Create an RPC task for the given read or write request and kick it.
775 * The page must have been locked by the caller.
777 * It may happen that the page we're passed is not marked dirty.
778 * This is the case if nfs_updatepage detects a conflicting request
779 * that has been written but not committed.
781 int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
782 struct nfs_pgio_header *hdr)
784 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
786 struct nfs_page *req;
789 struct list_head *head = &mirror->pg_list;
790 struct nfs_commit_info cinfo;
791 struct nfs_page_array *pg_array = &hdr->page_array;
792 unsigned int pagecount, pageused;
793 gfp_t gfp_flags = GFP_KERNEL;
795 pagecount = nfs_page_array_len(mirror->pg_base, mirror->pg_count);
796 pg_array->npages = pagecount;
798 if (pagecount <= ARRAY_SIZE(pg_array->page_array))
799 pg_array->pagevec = pg_array->page_array;
801 pg_array->pagevec = kcalloc(pagecount, sizeof(struct page *), gfp_flags);
802 if (!pg_array->pagevec) {
803 pg_array->npages = 0;
805 desc->pg_error = -ENOMEM;
806 return desc->pg_error;
810 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
811 pages = hdr->page_array.pagevec;
814 while (!list_empty(head)) {
815 req = nfs_list_entry(head->next);
816 nfs_list_move_request(req, &hdr->pages);
818 if (!last_page || last_page != req->wb_page) {
820 if (pageused > pagecount)
822 *pages++ = last_page = req->wb_page;
825 if (WARN_ON_ONCE(pageused != pagecount)) {
827 desc->pg_error = -EINVAL;
828 return desc->pg_error;
831 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
832 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
833 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
835 /* Set up the argument struct */
836 nfs_pgio_rpcsetup(hdr, mirror->pg_count, desc->pg_ioflags, &cinfo);
837 desc->pg_rpc_callops = &nfs_pgio_common_ops;
840 EXPORT_SYMBOL_GPL(nfs_generic_pgio);
842 static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
844 struct nfs_pgio_header *hdr;
847 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
849 desc->pg_error = -ENOMEM;
850 return desc->pg_error;
852 nfs_pgheader_init(desc, hdr, nfs_pgio_header_free);
853 ret = nfs_generic_pgio(desc, hdr);
855 ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode),
858 NFS_PROTO(hdr->inode),
859 desc->pg_rpc_callops,
860 desc->pg_ioflags, 0);
864 static struct nfs_pgio_mirror *
865 nfs_pageio_alloc_mirrors(struct nfs_pageio_descriptor *desc,
866 unsigned int mirror_count)
868 struct nfs_pgio_mirror *ret;
871 kfree(desc->pg_mirrors_dynamic);
872 desc->pg_mirrors_dynamic = NULL;
873 if (mirror_count == 1)
874 return desc->pg_mirrors_static;
875 ret = kmalloc_array(mirror_count, sizeof(*ret), GFP_KERNEL);
877 for (i = 0; i < mirror_count; i++)
878 nfs_pageio_mirror_init(&ret[i], desc->pg_bsize);
879 desc->pg_mirrors_dynamic = ret;
885 * nfs_pageio_setup_mirroring - determine if mirroring is to be used
886 * by calling the pg_get_mirror_count op
888 static void nfs_pageio_setup_mirroring(struct nfs_pageio_descriptor *pgio,
889 struct nfs_page *req)
891 unsigned int mirror_count = 1;
893 if (pgio->pg_ops->pg_get_mirror_count)
894 mirror_count = pgio->pg_ops->pg_get_mirror_count(pgio, req);
895 if (mirror_count == pgio->pg_mirror_count || pgio->pg_error < 0)
898 if (!mirror_count || mirror_count > NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX) {
899 pgio->pg_error = -EINVAL;
903 pgio->pg_mirrors = nfs_pageio_alloc_mirrors(pgio, mirror_count);
904 if (pgio->pg_mirrors == NULL) {
905 pgio->pg_error = -ENOMEM;
906 pgio->pg_mirrors = pgio->pg_mirrors_static;
909 pgio->pg_mirror_count = mirror_count;
912 static void nfs_pageio_cleanup_mirroring(struct nfs_pageio_descriptor *pgio)
914 pgio->pg_mirror_count = 1;
915 pgio->pg_mirror_idx = 0;
916 pgio->pg_mirrors = pgio->pg_mirrors_static;
917 kfree(pgio->pg_mirrors_dynamic);
918 pgio->pg_mirrors_dynamic = NULL;
921 static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
922 const struct nfs_lock_context *l2)
924 return l1->lockowner == l2->lockowner;
928 * nfs_can_coalesce_requests - test two requests for compatibility
929 * @prev: pointer to nfs_page
930 * @req: pointer to nfs_page
931 * @pgio: pointer to nfs_pagio_descriptor
933 * The nfs_page structures 'prev' and 'req' are compared to ensure that the
934 * page data area they describe is contiguous, and that their RPC
935 * credentials, NFSv4 open state, and lockowners are the same.
937 * Return 'true' if this is the case, else return 'false'.
939 static bool nfs_can_coalesce_requests(struct nfs_page *prev,
940 struct nfs_page *req,
941 struct nfs_pageio_descriptor *pgio)
944 struct file_lock_context *flctx;
947 if (!nfs_match_open_context(nfs_req_openctx(req), nfs_req_openctx(prev)))
949 flctx = d_inode(nfs_req_openctx(req)->dentry)->i_flctx;
951 !(list_empty_careful(&flctx->flc_posix) &&
952 list_empty_careful(&flctx->flc_flock)) &&
953 !nfs_match_lock_context(req->wb_lock_context,
954 prev->wb_lock_context))
956 if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
958 if (req->wb_page == prev->wb_page) {
959 if (req->wb_pgbase != prev->wb_pgbase + prev->wb_bytes)
962 if (req->wb_pgbase != 0 ||
963 prev->wb_pgbase + prev->wb_bytes != PAGE_SIZE)
967 size = pgio->pg_ops->pg_test(pgio, prev, req);
968 WARN_ON_ONCE(size > req->wb_bytes);
969 if (size && size < req->wb_bytes)
970 req->wb_bytes = size;
975 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
976 * @desc: destination io descriptor
979 * Returns true if the request 'req' was successfully coalesced into the
980 * existing list of pages 'desc'.
982 static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
983 struct nfs_page *req)
985 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
987 struct nfs_page *prev = NULL;
989 if (list_empty(&mirror->pg_list)) {
990 if (desc->pg_ops->pg_init)
991 desc->pg_ops->pg_init(desc, req);
992 if (desc->pg_error < 0)
994 mirror->pg_base = req->wb_pgbase;
995 mirror->pg_count = 0;
996 mirror->pg_recoalesce = 0;
998 prev = nfs_list_entry(mirror->pg_list.prev);
1000 if (desc->pg_maxretrans && req->wb_nio > desc->pg_maxretrans) {
1001 if (NFS_SERVER(desc->pg_inode)->flags & NFS_MOUNT_SOFTERR)
1002 desc->pg_error = -ETIMEDOUT;
1004 desc->pg_error = -EIO;
1008 if (!nfs_can_coalesce_requests(prev, req, desc))
1010 nfs_list_move_request(req, &mirror->pg_list);
1011 mirror->pg_count += req->wb_bytes;
1016 * Helper for nfs_pageio_add_request and nfs_pageio_complete
1018 static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
1020 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
1022 if (!list_empty(&mirror->pg_list)) {
1023 int error = desc->pg_ops->pg_doio(desc);
1025 desc->pg_error = error;
1026 if (list_empty(&mirror->pg_list)) {
1027 mirror->pg_bytes_written += mirror->pg_count;
1028 mirror->pg_count = 0;
1029 mirror->pg_base = 0;
1030 mirror->pg_recoalesce = 0;
1036 nfs_pageio_cleanup_request(struct nfs_pageio_descriptor *desc,
1037 struct nfs_page *req)
1041 nfs_list_move_request(req, &head);
1042 desc->pg_completion_ops->error_cleanup(&head, desc->pg_error);
1046 * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
1047 * @desc: destination io descriptor
1050 * This may split a request into subrequests which are all part of the
1053 * Returns true if the request 'req' was successfully coalesced into the
1054 * existing list of pages 'desc'.
1056 static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
1057 struct nfs_page *req)
1059 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
1061 struct nfs_page *subreq;
1062 unsigned int bytes_left = 0;
1063 unsigned int offset, pgbase;
1065 nfs_page_group_lock(req);
1068 bytes_left = subreq->wb_bytes;
1069 offset = subreq->wb_offset;
1070 pgbase = subreq->wb_pgbase;
1073 if (!nfs_pageio_do_add_request(desc, subreq)) {
1074 /* make sure pg_test call(s) did nothing */
1075 WARN_ON_ONCE(subreq->wb_bytes != bytes_left);
1076 WARN_ON_ONCE(subreq->wb_offset != offset);
1077 WARN_ON_ONCE(subreq->wb_pgbase != pgbase);
1079 nfs_page_group_unlock(req);
1080 desc->pg_moreio = 1;
1081 nfs_pageio_doio(desc);
1082 if (desc->pg_error < 0 || mirror->pg_recoalesce)
1083 goto out_cleanup_subreq;
1084 /* retry add_request for this subreq */
1085 nfs_page_group_lock(req);
1089 /* check for buggy pg_test call(s) */
1090 WARN_ON_ONCE(subreq->wb_bytes + subreq->wb_pgbase > PAGE_SIZE);
1091 WARN_ON_ONCE(subreq->wb_bytes > bytes_left);
1092 WARN_ON_ONCE(subreq->wb_bytes == 0);
1094 bytes_left -= subreq->wb_bytes;
1095 offset += subreq->wb_bytes;
1096 pgbase += subreq->wb_bytes;
1099 subreq = nfs_create_subreq(req, subreq, pgbase,
1100 offset, bytes_left);
1104 } while (bytes_left > 0);
1106 nfs_page_group_unlock(req);
1109 desc->pg_error = PTR_ERR(subreq);
1110 nfs_page_group_unlock(req);
1114 nfs_pageio_cleanup_request(desc, subreq);
1118 static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
1120 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
1124 list_splice_init(&mirror->pg_list, &head);
1125 mirror->pg_count = 0;
1126 mirror->pg_base = 0;
1127 mirror->pg_recoalesce = 0;
1129 while (!list_empty(&head)) {
1130 struct nfs_page *req;
1132 req = list_first_entry(&head, struct nfs_page, wb_list);
1133 if (__nfs_pageio_add_request(desc, req))
1135 if (desc->pg_error < 0) {
1136 list_splice_tail(&head, &mirror->pg_list);
1137 mirror->pg_recoalesce = 1;
1142 } while (mirror->pg_recoalesce);
1146 static int nfs_pageio_add_request_mirror(struct nfs_pageio_descriptor *desc,
1147 struct nfs_page *req)
1152 ret = __nfs_pageio_add_request(desc, req);
1155 if (desc->pg_error < 0)
1157 ret = nfs_do_recoalesce(desc);
1163 static void nfs_pageio_error_cleanup(struct nfs_pageio_descriptor *desc)
1166 struct nfs_pgio_mirror *mirror;
1168 if (!desc->pg_error)
1171 for (midx = 0; midx < desc->pg_mirror_count; midx++) {
1172 mirror = &desc->pg_mirrors[midx];
1173 desc->pg_completion_ops->error_cleanup(&mirror->pg_list,
1178 int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
1179 struct nfs_page *req)
1182 unsigned int pgbase, offset, bytes;
1183 struct nfs_page *dupreq, *lastreq;
1185 pgbase = req->wb_pgbase;
1186 offset = req->wb_offset;
1187 bytes = req->wb_bytes;
1189 nfs_pageio_setup_mirroring(desc, req);
1190 if (desc->pg_error < 0)
1193 /* Create the mirror instances first, and fire them off */
1194 for (midx = 1; midx < desc->pg_mirror_count; midx++) {
1195 nfs_page_group_lock(req);
1197 /* find the last request */
1198 for (lastreq = req->wb_head;
1199 lastreq->wb_this_page != req->wb_head;
1200 lastreq = lastreq->wb_this_page)
1203 dupreq = nfs_create_subreq(req, lastreq,
1204 pgbase, offset, bytes);
1206 nfs_page_group_unlock(req);
1207 if (IS_ERR(dupreq)) {
1208 desc->pg_error = PTR_ERR(dupreq);
1212 desc->pg_mirror_idx = midx;
1213 if (!nfs_pageio_add_request_mirror(desc, dupreq))
1214 goto out_cleanup_subreq;
1217 desc->pg_mirror_idx = 0;
1218 if (!nfs_pageio_add_request_mirror(desc, req))
1224 nfs_pageio_cleanup_request(desc, dupreq);
1226 nfs_pageio_error_cleanup(desc);
1231 * nfs_pageio_complete_mirror - Complete I/O on the current mirror of an
1232 * nfs_pageio_descriptor
1233 * @desc: pointer to io descriptor
1234 * @mirror_idx: pointer to mirror index
1236 static void nfs_pageio_complete_mirror(struct nfs_pageio_descriptor *desc,
1239 struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[mirror_idx];
1240 u32 restore_idx = desc->pg_mirror_idx;
1242 if (nfs_pgio_has_mirroring(desc))
1243 desc->pg_mirror_idx = mirror_idx;
1245 nfs_pageio_doio(desc);
1246 if (desc->pg_error < 0 || !mirror->pg_recoalesce)
1248 if (!nfs_do_recoalesce(desc))
1251 desc->pg_mirror_idx = restore_idx;
1255 * nfs_pageio_resend - Transfer requests to new descriptor and resend
1256 * @hdr - the pgio header to move request from
1257 * @desc - the pageio descriptor to add requests to
1259 * Try to move each request (nfs_page) from @hdr to @desc then attempt
1262 * Returns 0 on success and < 0 on error.
1264 int nfs_pageio_resend(struct nfs_pageio_descriptor *desc,
1265 struct nfs_pgio_header *hdr)
1269 desc->pg_io_completion = hdr->io_completion;
1270 desc->pg_dreq = hdr->dreq;
1271 list_splice_init(&hdr->pages, &pages);
1272 while (!list_empty(&pages)) {
1273 struct nfs_page *req = nfs_list_entry(pages.next);
1275 if (!nfs_pageio_add_request(desc, req))
1278 nfs_pageio_complete(desc);
1279 if (!list_empty(&pages)) {
1280 int err = desc->pg_error < 0 ? desc->pg_error : -EIO;
1281 hdr->completion_ops->error_cleanup(&pages, err);
1282 nfs_set_pgio_error(hdr, err, hdr->io_start);
1287 EXPORT_SYMBOL_GPL(nfs_pageio_resend);
1290 * nfs_pageio_complete - Complete I/O then cleanup an nfs_pageio_descriptor
1291 * @desc: pointer to io descriptor
1293 void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
1297 for (midx = 0; midx < desc->pg_mirror_count; midx++)
1298 nfs_pageio_complete_mirror(desc, midx);
1300 if (desc->pg_error < 0)
1301 nfs_pageio_error_cleanup(desc);
1302 if (desc->pg_ops->pg_cleanup)
1303 desc->pg_ops->pg_cleanup(desc);
1304 nfs_pageio_cleanup_mirroring(desc);
1308 * nfs_pageio_cond_complete - Conditional I/O completion
1309 * @desc: pointer to io descriptor
1310 * @index: page index
1312 * It is important to ensure that processes don't try to take locks
1313 * on non-contiguous ranges of pages as that might deadlock. This
1314 * function should be called before attempting to wait on a locked
1315 * nfs_page. It will complete the I/O if the page index 'index'
1316 * is not contiguous with the existing list of pages in 'desc'.
1318 void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
1320 struct nfs_pgio_mirror *mirror;
1321 struct nfs_page *prev;
1324 for (midx = 0; midx < desc->pg_mirror_count; midx++) {
1325 mirror = &desc->pg_mirrors[midx];
1326 if (!list_empty(&mirror->pg_list)) {
1327 prev = nfs_list_entry(mirror->pg_list.prev);
1328 if (index != prev->wb_index + 1) {
1329 nfs_pageio_complete(desc);
1337 * nfs_pageio_stop_mirroring - stop using mirroring (set mirror count to 1)
1339 void nfs_pageio_stop_mirroring(struct nfs_pageio_descriptor *pgio)
1341 nfs_pageio_complete(pgio);
1344 int __init nfs_init_nfspagecache(void)
1346 nfs_page_cachep = kmem_cache_create("nfs_page",
1347 sizeof(struct nfs_page),
1348 0, SLAB_HWCACHE_ALIGN,
1350 if (nfs_page_cachep == NULL)
1356 void nfs_destroy_nfspagecache(void)
1358 kmem_cache_destroy(nfs_page_cachep);
1361 static const struct rpc_call_ops nfs_pgio_common_ops = {
1362 .rpc_call_prepare = nfs_pgio_prepare,
1363 .rpc_call_done = nfs_pgio_result,
1364 .rpc_release = nfs_pgio_release,
1367 const struct nfs_pageio_ops nfs_pgio_rw_ops = {
1368 .pg_test = nfs_generic_pg_test,
1369 .pg_doio = nfs_generic_pg_pgios,