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.
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>
24 #include <linux/filelock.h>
30 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
32 static struct kmem_cache *nfs_page_cachep;
33 static const struct rpc_call_ops nfs_pgio_common_ops;
35 struct nfs_page_iter_page {
36 const struct nfs_page *req;
40 static void nfs_page_iter_page_init(struct nfs_page_iter_page *i,
41 const struct nfs_page *req)
47 static void nfs_page_iter_page_advance(struct nfs_page_iter_page *i, size_t sz)
49 const struct nfs_page *req = i->req;
50 size_t tmp = i->count + sz;
52 i->count = (tmp < req->wb_bytes) ? tmp : req->wb_bytes;
55 static struct page *nfs_page_iter_page_get(struct nfs_page_iter_page *i)
57 const struct nfs_page *req = i->req;
60 if (i->count != req->wb_bytes) {
61 size_t base = i->count + req->wb_pgbase;
62 size_t len = PAGE_SIZE - offset_in_page(base);
64 page = nfs_page_to_page(req, base);
65 nfs_page_iter_page_advance(i, len);
71 static struct nfs_pgio_mirror *
72 nfs_pgio_get_mirror(struct nfs_pageio_descriptor *desc, u32 idx)
74 if (desc->pg_ops->pg_get_mirror)
75 return desc->pg_ops->pg_get_mirror(desc, idx);
76 return &desc->pg_mirrors[0];
79 struct nfs_pgio_mirror *
80 nfs_pgio_current_mirror(struct nfs_pageio_descriptor *desc)
82 return nfs_pgio_get_mirror(desc, desc->pg_mirror_idx);
84 EXPORT_SYMBOL_GPL(nfs_pgio_current_mirror);
87 nfs_pgio_set_current_mirror(struct nfs_pageio_descriptor *desc, u32 idx)
89 if (desc->pg_ops->pg_set_mirror)
90 return desc->pg_ops->pg_set_mirror(desc, idx);
91 return desc->pg_mirror_idx;
94 void nfs_pgheader_init(struct nfs_pageio_descriptor *desc,
95 struct nfs_pgio_header *hdr,
96 void (*release)(struct nfs_pgio_header *hdr))
98 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
101 hdr->req = nfs_list_entry(mirror->pg_list.next);
102 hdr->inode = desc->pg_inode;
103 hdr->cred = nfs_req_openctx(hdr->req)->cred;
104 hdr->io_start = req_offset(hdr->req);
105 hdr->good_bytes = mirror->pg_count;
106 hdr->io_completion = desc->pg_io_completion;
107 hdr->dreq = desc->pg_dreq;
108 hdr->release = release;
109 hdr->completion_ops = desc->pg_completion_ops;
110 if (hdr->completion_ops->init_hdr)
111 hdr->completion_ops->init_hdr(hdr);
113 hdr->pgio_mirror_idx = desc->pg_mirror_idx;
115 EXPORT_SYMBOL_GPL(nfs_pgheader_init);
117 void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos)
119 unsigned int new = pos - hdr->io_start;
121 trace_nfs_pgio_error(hdr, error, pos);
122 if (hdr->good_bytes > new) {
123 hdr->good_bytes = new;
124 clear_bit(NFS_IOHDR_EOF, &hdr->flags);
125 if (!test_and_set_bit(NFS_IOHDR_ERROR, &hdr->flags))
130 static inline struct nfs_page *nfs_page_alloc(void)
133 kmem_cache_zalloc(nfs_page_cachep, nfs_io_gfp_mask());
135 INIT_LIST_HEAD(&p->wb_list);
140 nfs_page_free(struct nfs_page *p)
142 kmem_cache_free(nfs_page_cachep, p);
146 * nfs_iocounter_wait - wait for i/o to complete
147 * @l_ctx: nfs_lock_context with io_counter to use
149 * returns -ERESTARTSYS if interrupted by a fatal signal.
150 * Otherwise returns 0 once the io_count hits 0.
153 nfs_iocounter_wait(struct nfs_lock_context *l_ctx)
155 return wait_var_event_killable(&l_ctx->io_count,
156 !atomic_read(&l_ctx->io_count));
160 * nfs_async_iocounter_wait - wait on a rpc_waitqueue for I/O
162 * @task: the rpc_task that should wait
163 * @l_ctx: nfs_lock_context with io_counter to check
165 * Returns true if there is outstanding I/O to wait on and the
166 * task has been put to sleep.
169 nfs_async_iocounter_wait(struct rpc_task *task, struct nfs_lock_context *l_ctx)
171 struct inode *inode = d_inode(l_ctx->open_context->dentry);
174 if (atomic_read(&l_ctx->io_count) > 0) {
175 rpc_sleep_on(&NFS_SERVER(inode)->uoc_rpcwaitq, task, NULL);
179 if (atomic_read(&l_ctx->io_count) == 0) {
180 rpc_wake_up_queued_task(&NFS_SERVER(inode)->uoc_rpcwaitq, task);
186 EXPORT_SYMBOL_GPL(nfs_async_iocounter_wait);
189 * nfs_page_lock_head_request - page lock the head of the page group
190 * @req: any member of the page group
193 nfs_page_group_lock_head(struct nfs_page *req)
195 struct nfs_page *head = req->wb_head;
197 while (!nfs_lock_request(head)) {
198 int ret = nfs_wait_on_request(head);
203 kref_get(&head->wb_kref);
208 * nfs_unroll_locks - unlock all newly locked reqs and wait on @req
209 * @head: head request of page group, must be holding head lock
210 * @req: request that couldn't lock and needs to wait on the req bit lock
212 * This is a helper function for nfs_lock_and_join_requests
213 * returns 0 on success, < 0 on error.
216 nfs_unroll_locks(struct nfs_page *head, struct nfs_page *req)
218 struct nfs_page *tmp;
220 /* relinquish all the locks successfully grabbed this run */
221 for (tmp = head->wb_this_page ; tmp != req; tmp = tmp->wb_this_page) {
222 if (!kref_read(&tmp->wb_kref))
224 nfs_unlock_and_release_request(tmp);
229 * nfs_page_group_lock_subreq - try to lock a subrequest
230 * @head: head request of page group
231 * @subreq: request to lock
233 * This is a helper function for nfs_lock_and_join_requests which
234 * must be called with the head request and page group both locked.
235 * On error, it returns with the page group unlocked.
238 nfs_page_group_lock_subreq(struct nfs_page *head, struct nfs_page *subreq)
242 if (!kref_get_unless_zero(&subreq->wb_kref))
244 while (!nfs_lock_request(subreq)) {
245 nfs_page_group_unlock(head);
246 ret = nfs_wait_on_request(subreq);
248 ret = nfs_page_group_lock(head);
250 nfs_unroll_locks(head, subreq);
251 nfs_release_request(subreq);
259 * nfs_page_group_lock_subrequests - try to lock the subrequests
260 * @head: head request of page group
262 * This is a helper function for nfs_lock_and_join_requests which
263 * must be called with the head request locked.
265 int nfs_page_group_lock_subrequests(struct nfs_page *head)
267 struct nfs_page *subreq;
270 ret = nfs_page_group_lock(head);
273 /* lock each request in the page group */
274 for (subreq = head->wb_this_page; subreq != head;
275 subreq = subreq->wb_this_page) {
276 ret = nfs_page_group_lock_subreq(head, subreq);
280 nfs_page_group_unlock(head);
285 * nfs_page_set_headlock - set the request PG_HEADLOCK
286 * @req: request that is to be locked
288 * this lock must be held when modifying req->wb_head
290 * return 0 on success, < 0 on error
293 nfs_page_set_headlock(struct nfs_page *req)
295 if (!test_and_set_bit(PG_HEADLOCK, &req->wb_flags))
298 set_bit(PG_CONTENDED1, &req->wb_flags);
299 smp_mb__after_atomic();
300 return wait_on_bit_lock(&req->wb_flags, PG_HEADLOCK,
301 TASK_UNINTERRUPTIBLE);
305 * nfs_page_clear_headlock - clear the request PG_HEADLOCK
306 * @req: request that is to be locked
309 nfs_page_clear_headlock(struct nfs_page *req)
311 clear_bit_unlock(PG_HEADLOCK, &req->wb_flags);
312 smp_mb__after_atomic();
313 if (!test_bit(PG_CONTENDED1, &req->wb_flags))
315 wake_up_bit(&req->wb_flags, PG_HEADLOCK);
319 * nfs_page_group_lock - lock the head of the page group
320 * @req: request in group that is to be locked
322 * this lock must be held when traversing or modifying the page
325 * return 0 on success, < 0 on error
328 nfs_page_group_lock(struct nfs_page *req)
332 ret = nfs_page_set_headlock(req);
333 if (ret || req->wb_head == req)
335 return nfs_page_set_headlock(req->wb_head);
339 * nfs_page_group_unlock - unlock the head of the page group
340 * @req: request in group that is to be unlocked
343 nfs_page_group_unlock(struct nfs_page *req)
345 if (req != req->wb_head)
346 nfs_page_clear_headlock(req->wb_head);
347 nfs_page_clear_headlock(req);
351 * nfs_page_group_sync_on_bit_locked
353 * must be called with page group lock held
356 nfs_page_group_sync_on_bit_locked(struct nfs_page *req, unsigned int bit)
358 struct nfs_page *head = req->wb_head;
359 struct nfs_page *tmp;
361 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_flags));
362 WARN_ON_ONCE(test_and_set_bit(bit, &req->wb_flags));
364 tmp = req->wb_this_page;
366 if (!test_bit(bit, &tmp->wb_flags))
368 tmp = tmp->wb_this_page;
371 /* true! reset all bits */
374 clear_bit(bit, &tmp->wb_flags);
375 tmp = tmp->wb_this_page;
376 } while (tmp != req);
382 * nfs_page_group_sync_on_bit - set bit on current request, but only
383 * return true if the bit is set for all requests in page group
384 * @req - request in page group
385 * @bit - PG_* bit that is used to sync page group
387 bool nfs_page_group_sync_on_bit(struct nfs_page *req, unsigned int bit)
391 nfs_page_group_lock(req);
392 ret = nfs_page_group_sync_on_bit_locked(req, bit);
393 nfs_page_group_unlock(req);
399 * nfs_page_group_init - Initialize the page group linkage for @req
400 * @req - a new nfs request
401 * @prev - the previous request in page group, or NULL if @req is the first
402 * or only request in the group (the head).
405 nfs_page_group_init(struct nfs_page *req, struct nfs_page *prev)
408 WARN_ON_ONCE(prev == req);
413 req->wb_this_page = req;
416 WARN_ON_ONCE(prev->wb_this_page != prev->wb_head);
417 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &prev->wb_head->wb_flags));
418 req->wb_head = prev->wb_head;
419 req->wb_this_page = prev->wb_this_page;
420 prev->wb_this_page = req;
422 /* All subrequests take a ref on the head request until
423 * nfs_page_group_destroy is called */
424 kref_get(&req->wb_head->wb_kref);
426 /* grab extra ref and bump the request count if head request
427 * has extra ref from the write/commit path to handle handoff
428 * between write and commit lists. */
429 if (test_bit(PG_INODE_REF, &prev->wb_head->wb_flags)) {
430 inode = nfs_page_to_inode(req);
431 set_bit(PG_INODE_REF, &req->wb_flags);
432 kref_get(&req->wb_kref);
433 atomic_long_inc(&NFS_I(inode)->nrequests);
439 * nfs_page_group_destroy - sync the destruction of page groups
440 * @req - request that no longer needs the page group
442 * releases the page group reference from each member once all
443 * members have called this function.
446 nfs_page_group_destroy(struct kref *kref)
448 struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
449 struct nfs_page *head = req->wb_head;
450 struct nfs_page *tmp, *next;
452 if (!nfs_page_group_sync_on_bit(req, PG_TEARDOWN))
457 next = tmp->wb_this_page;
458 /* unlink and free */
459 tmp->wb_this_page = tmp;
461 nfs_free_request(tmp);
463 } while (tmp != req);
465 /* subrequests must release the ref on the head request */
467 nfs_release_request(head);
470 static struct nfs_page *nfs_page_create(struct nfs_lock_context *l_ctx,
471 unsigned int pgbase, pgoff_t index,
472 unsigned int offset, unsigned int count)
474 struct nfs_page *req;
475 struct nfs_open_context *ctx = l_ctx->open_context;
477 if (test_bit(NFS_CONTEXT_BAD, &ctx->flags))
478 return ERR_PTR(-EBADF);
479 /* try to allocate the request struct */
480 req = nfs_page_alloc();
482 return ERR_PTR(-ENOMEM);
484 req->wb_lock_context = l_ctx;
485 refcount_inc(&l_ctx->count);
486 atomic_inc(&l_ctx->io_count);
488 /* Initialize the request struct. Initially, we assume a
489 * long write-back delay. This will be adjusted in
490 * update_nfs_request below if the region is not locked. */
491 req->wb_pgbase = pgbase;
492 req->wb_index = index;
493 req->wb_offset = offset;
494 req->wb_bytes = count;
495 kref_init(&req->wb_kref);
500 static void nfs_page_assign_folio(struct nfs_page *req, struct folio *folio)
503 req->wb_folio = folio;
505 set_bit(PG_FOLIO, &req->wb_flags);
509 static void nfs_page_assign_page(struct nfs_page *req, struct page *page)
518 * nfs_page_create_from_page - Create an NFS read/write request.
519 * @ctx: open context to use
520 * @page: page to write
521 * @pgbase: starting offset within the page for the write
522 * @offset: file offset for the write
523 * @count: number of bytes to read/write
525 * The page must be locked by the caller. This makes sure we never
526 * create two different requests for the same page.
527 * User should ensure it is safe to sleep in this function.
529 struct nfs_page *nfs_page_create_from_page(struct nfs_open_context *ctx,
531 unsigned int pgbase, loff_t offset,
534 struct nfs_lock_context *l_ctx = nfs_get_lock_context(ctx);
535 struct nfs_page *ret;
538 return ERR_CAST(l_ctx);
539 ret = nfs_page_create(l_ctx, pgbase, offset >> PAGE_SHIFT,
540 offset_in_page(offset), count);
542 nfs_page_assign_page(ret, page);
543 nfs_page_group_init(ret, NULL);
545 nfs_put_lock_context(l_ctx);
550 * nfs_page_create_from_folio - Create an NFS read/write request.
551 * @ctx: open context to use
552 * @folio: folio to write
553 * @offset: starting offset within the folio for the write
554 * @count: number of bytes to read/write
556 * The page must be locked by the caller. This makes sure we never
557 * create two different requests for the same page.
558 * User should ensure it is safe to sleep in this function.
560 struct nfs_page *nfs_page_create_from_folio(struct nfs_open_context *ctx,
565 struct nfs_lock_context *l_ctx = nfs_get_lock_context(ctx);
566 struct nfs_page *ret;
569 return ERR_CAST(l_ctx);
570 ret = nfs_page_create(l_ctx, offset, folio_index(folio), offset, count);
572 nfs_page_assign_folio(ret, folio);
573 nfs_page_group_init(ret, NULL);
575 nfs_put_lock_context(l_ctx);
579 static struct nfs_page *
580 nfs_create_subreq(struct nfs_page *req,
585 struct nfs_page *last;
586 struct nfs_page *ret;
587 struct folio *folio = nfs_page_to_folio(req);
588 struct page *page = nfs_page_to_page(req, pgbase);
590 ret = nfs_page_create(req->wb_lock_context, pgbase, req->wb_index,
594 nfs_page_assign_folio(ret, folio);
596 nfs_page_assign_page(ret, page);
597 /* find the last request */
598 for (last = req->wb_head;
599 last->wb_this_page != req->wb_head;
600 last = last->wb_this_page)
603 nfs_lock_request(ret);
604 nfs_page_group_init(ret, last);
605 ret->wb_nio = req->wb_nio;
611 * nfs_unlock_request - Unlock request and wake up sleepers.
612 * @req: pointer to request
614 void nfs_unlock_request(struct nfs_page *req)
616 clear_bit_unlock(PG_BUSY, &req->wb_flags);
617 smp_mb__after_atomic();
618 if (!test_bit(PG_CONTENDED2, &req->wb_flags))
620 wake_up_bit(&req->wb_flags, PG_BUSY);
624 * nfs_unlock_and_release_request - Unlock request and release the nfs_page
625 * @req: pointer to request
627 void nfs_unlock_and_release_request(struct nfs_page *req)
629 nfs_unlock_request(req);
630 nfs_release_request(req);
634 * nfs_clear_request - Free up all resources allocated to the request
637 * Release page and open context resources associated with a read/write
638 * request after it has completed.
640 static void nfs_clear_request(struct nfs_page *req)
642 struct folio *folio = nfs_page_to_folio(req);
643 struct page *page = req->wb_page;
644 struct nfs_lock_context *l_ctx = req->wb_lock_context;
645 struct nfs_open_context *ctx;
649 req->wb_folio = NULL;
650 clear_bit(PG_FOLIO, &req->wb_flags);
651 } else if (page != NULL) {
656 if (atomic_dec_and_test(&l_ctx->io_count)) {
657 wake_up_var(&l_ctx->io_count);
658 ctx = l_ctx->open_context;
659 if (test_bit(NFS_CONTEXT_UNLOCK, &ctx->flags))
660 rpc_wake_up(&NFS_SERVER(d_inode(ctx->dentry))->uoc_rpcwaitq);
662 nfs_put_lock_context(l_ctx);
663 req->wb_lock_context = NULL;
668 * nfs_free_request - Release the count on an NFS read/write request
669 * @req: request to release
671 * Note: Should never be called with the spinlock held!
673 void nfs_free_request(struct nfs_page *req)
675 WARN_ON_ONCE(req->wb_this_page != req);
677 /* extra debug: make sure no sync bits are still set */
678 WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
679 WARN_ON_ONCE(test_bit(PG_UNLOCKPAGE, &req->wb_flags));
680 WARN_ON_ONCE(test_bit(PG_UPTODATE, &req->wb_flags));
681 WARN_ON_ONCE(test_bit(PG_WB_END, &req->wb_flags));
682 WARN_ON_ONCE(test_bit(PG_REMOVE, &req->wb_flags));
684 /* Release struct file and open context */
685 nfs_clear_request(req);
689 void nfs_release_request(struct nfs_page *req)
691 kref_put(&req->wb_kref, nfs_page_group_destroy);
693 EXPORT_SYMBOL_GPL(nfs_release_request);
696 * nfs_wait_on_request - Wait for a request to complete.
697 * @req: request to wait upon.
699 * Interruptible by fatal signals only.
700 * The user is responsible for holding a count on the request.
703 nfs_wait_on_request(struct nfs_page *req)
705 if (!test_bit(PG_BUSY, &req->wb_flags))
707 set_bit(PG_CONTENDED2, &req->wb_flags);
708 smp_mb__after_atomic();
709 return wait_on_bit_io(&req->wb_flags, PG_BUSY,
710 TASK_UNINTERRUPTIBLE);
712 EXPORT_SYMBOL_GPL(nfs_wait_on_request);
715 * nfs_generic_pg_test - determine if requests can be coalesced
716 * @desc: pointer to descriptor
717 * @prev: previous request in desc, or NULL
720 * Returns zero if @req cannot be coalesced into @desc, otherwise it returns
721 * the size of the request.
723 size_t nfs_generic_pg_test(struct nfs_pageio_descriptor *desc,
724 struct nfs_page *prev, struct nfs_page *req)
726 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
729 if (mirror->pg_count > mirror->pg_bsize) {
730 /* should never happen */
736 * Limit the request size so that we can still allocate a page array
737 * for it without upsetting the slab allocator.
739 if (((mirror->pg_count + req->wb_bytes) >> PAGE_SHIFT) *
740 sizeof(struct page *) > PAGE_SIZE)
743 return min(mirror->pg_bsize - mirror->pg_count, (size_t)req->wb_bytes);
745 EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
747 struct nfs_pgio_header *nfs_pgio_header_alloc(const struct nfs_rw_ops *ops)
749 struct nfs_pgio_header *hdr = ops->rw_alloc_header();
752 INIT_LIST_HEAD(&hdr->pages);
757 EXPORT_SYMBOL_GPL(nfs_pgio_header_alloc);
760 * nfs_pgio_data_destroy - make @hdr suitable for reuse
762 * Frees memory and releases refs from nfs_generic_pgio, so that it may
765 * @hdr: A header that has had nfs_generic_pgio called
767 static void nfs_pgio_data_destroy(struct nfs_pgio_header *hdr)
769 if (hdr->args.context)
770 put_nfs_open_context(hdr->args.context);
771 if (hdr->page_array.pagevec != hdr->page_array.page_array)
772 kfree(hdr->page_array.pagevec);
776 * nfs_pgio_header_free - Free a read or write header
777 * @hdr: The header to free
779 void nfs_pgio_header_free(struct nfs_pgio_header *hdr)
781 nfs_pgio_data_destroy(hdr);
782 hdr->rw_ops->rw_free_header(hdr);
784 EXPORT_SYMBOL_GPL(nfs_pgio_header_free);
787 * nfs_pgio_rpcsetup - Set up arguments for a pageio call
788 * @hdr: The pageio hdr
790 * @count: Number of bytes to read
791 * @how: How to commit data (writes only)
792 * @cinfo: Commit information for the call (writes only)
794 static void nfs_pgio_rpcsetup(struct nfs_pgio_header *hdr, unsigned int pgbase,
795 unsigned int count, int how,
796 struct nfs_commit_info *cinfo)
798 struct nfs_page *req = hdr->req;
800 /* Set up the RPC argument and reply structs
801 * NB: take care not to mess about with hdr->commit et al. */
803 hdr->args.fh = NFS_FH(hdr->inode);
804 hdr->args.offset = req_offset(req);
805 /* pnfs_set_layoutcommit needs this */
806 hdr->mds_offset = hdr->args.offset;
807 hdr->args.pgbase = pgbase;
808 hdr->args.pages = hdr->page_array.pagevec;
809 hdr->args.count = count;
810 hdr->args.context = get_nfs_open_context(nfs_req_openctx(req));
811 hdr->args.lock_context = req->wb_lock_context;
812 hdr->args.stable = NFS_UNSTABLE;
813 switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
816 case FLUSH_COND_STABLE:
817 if (nfs_reqs_to_commit(cinfo))
821 hdr->args.stable = NFS_FILE_SYNC;
824 hdr->res.fattr = &hdr->fattr;
827 hdr->res.verf = &hdr->verf;
828 nfs_fattr_init(&hdr->fattr);
832 * nfs_pgio_prepare - Prepare pageio hdr to go over the wire
833 * @task: The current task
834 * @calldata: pageio header to prepare
836 static void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
838 struct nfs_pgio_header *hdr = calldata;
840 err = NFS_PROTO(hdr->inode)->pgio_rpc_prepare(task, hdr);
845 int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr,
846 const struct cred *cred, const struct nfs_rpc_ops *rpc_ops,
847 const struct rpc_call_ops *call_ops, int how, int flags)
849 struct rpc_task *task;
850 struct rpc_message msg = {
851 .rpc_argp = &hdr->args,
852 .rpc_resp = &hdr->res,
855 struct rpc_task_setup task_setup_data = {
859 .callback_ops = call_ops,
860 .callback_data = hdr,
861 .workqueue = nfsiod_workqueue,
862 .flags = RPC_TASK_ASYNC | flags,
865 if (nfs_server_capable(hdr->inode, NFS_CAP_MOVEABLE))
866 task_setup_data.flags |= RPC_TASK_MOVEABLE;
868 hdr->rw_ops->rw_initiate(hdr, &msg, rpc_ops, &task_setup_data, how);
870 dprintk("NFS: initiated pgio call "
871 "(req %s/%llu, %u bytes @ offset %llu)\n",
872 hdr->inode->i_sb->s_id,
873 (unsigned long long)NFS_FILEID(hdr->inode),
875 (unsigned long long)hdr->args.offset);
877 task = rpc_run_task(&task_setup_data);
879 return PTR_ERR(task);
883 EXPORT_SYMBOL_GPL(nfs_initiate_pgio);
886 * nfs_pgio_error - Clean up from a pageio error
887 * @hdr: pageio header
889 static void nfs_pgio_error(struct nfs_pgio_header *hdr)
891 set_bit(NFS_IOHDR_REDO, &hdr->flags);
892 hdr->completion_ops->completion(hdr);
896 * nfs_pgio_release - Release pageio data
897 * @calldata: The pageio header to release
899 static void nfs_pgio_release(void *calldata)
901 struct nfs_pgio_header *hdr = calldata;
902 hdr->completion_ops->completion(hdr);
905 static void nfs_pageio_mirror_init(struct nfs_pgio_mirror *mirror,
908 INIT_LIST_HEAD(&mirror->pg_list);
909 mirror->pg_bytes_written = 0;
910 mirror->pg_count = 0;
911 mirror->pg_bsize = bsize;
913 mirror->pg_recoalesce = 0;
917 * nfs_pageio_init - initialise a page io descriptor
918 * @desc: pointer to descriptor
919 * @inode: pointer to inode
920 * @pg_ops: pointer to pageio operations
921 * @compl_ops: pointer to pageio completion operations
922 * @rw_ops: pointer to nfs read/write operations
923 * @bsize: io block size
924 * @io_flags: extra parameters for the io function
926 void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
928 const struct nfs_pageio_ops *pg_ops,
929 const struct nfs_pgio_completion_ops *compl_ops,
930 const struct nfs_rw_ops *rw_ops,
935 desc->pg_inode = inode;
936 desc->pg_ops = pg_ops;
937 desc->pg_completion_ops = compl_ops;
938 desc->pg_rw_ops = rw_ops;
939 desc->pg_ioflags = io_flags;
941 desc->pg_lseg = NULL;
942 desc->pg_io_completion = NULL;
943 desc->pg_dreq = NULL;
944 desc->pg_bsize = bsize;
946 desc->pg_mirror_count = 1;
947 desc->pg_mirror_idx = 0;
949 desc->pg_mirrors_dynamic = NULL;
950 desc->pg_mirrors = desc->pg_mirrors_static;
951 nfs_pageio_mirror_init(&desc->pg_mirrors[0], bsize);
952 desc->pg_maxretrans = 0;
956 * nfs_pgio_result - Basic pageio error handling
957 * @task: The task that ran
958 * @calldata: Pageio header to check
960 static void nfs_pgio_result(struct rpc_task *task, void *calldata)
962 struct nfs_pgio_header *hdr = calldata;
963 struct inode *inode = hdr->inode;
965 if (hdr->rw_ops->rw_done(task, hdr, inode) != 0)
967 if (task->tk_status < 0)
968 nfs_set_pgio_error(hdr, task->tk_status, hdr->args.offset);
970 hdr->rw_ops->rw_result(task, hdr);
974 * Create an RPC task for the given read or write request and kick it.
975 * The page must have been locked by the caller.
977 * It may happen that the page we're passed is not marked dirty.
978 * This is the case if nfs_updatepage detects a conflicting request
979 * that has been written but not committed.
981 int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
982 struct nfs_pgio_header *hdr)
984 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
986 struct nfs_page *req;
989 struct list_head *head = &mirror->pg_list;
990 struct nfs_commit_info cinfo;
991 struct nfs_page_array *pg_array = &hdr->page_array;
992 unsigned int pagecount, pageused;
993 unsigned int pg_base = offset_in_page(mirror->pg_base);
994 gfp_t gfp_flags = nfs_io_gfp_mask();
996 pagecount = nfs_page_array_len(pg_base, mirror->pg_count);
997 pg_array->npages = pagecount;
999 if (pagecount <= ARRAY_SIZE(pg_array->page_array))
1000 pg_array->pagevec = pg_array->page_array;
1002 pg_array->pagevec = kcalloc(pagecount, sizeof(struct page *), gfp_flags);
1003 if (!pg_array->pagevec) {
1004 pg_array->npages = 0;
1005 nfs_pgio_error(hdr);
1006 desc->pg_error = -ENOMEM;
1007 return desc->pg_error;
1011 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
1012 pages = hdr->page_array.pagevec;
1015 while (!list_empty(head)) {
1016 struct nfs_page_iter_page i;
1019 req = nfs_list_entry(head->next);
1020 nfs_list_move_request(req, &hdr->pages);
1022 if (req->wb_pgbase == 0)
1025 nfs_page_iter_page_init(&i, req);
1026 while ((page = nfs_page_iter_page_get(&i)) != NULL) {
1027 if (last_page != page) {
1029 if (pageused > pagecount)
1031 *pages++ = last_page = page;
1036 if (WARN_ON_ONCE(pageused != pagecount)) {
1037 nfs_pgio_error(hdr);
1038 desc->pg_error = -EINVAL;
1039 return desc->pg_error;
1042 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1043 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
1044 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1046 /* Set up the argument struct */
1047 nfs_pgio_rpcsetup(hdr, pg_base, mirror->pg_count, desc->pg_ioflags,
1049 desc->pg_rpc_callops = &nfs_pgio_common_ops;
1052 EXPORT_SYMBOL_GPL(nfs_generic_pgio);
1054 static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
1056 struct nfs_pgio_header *hdr;
1058 unsigned short task_flags = 0;
1060 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
1062 desc->pg_error = -ENOMEM;
1063 return desc->pg_error;
1065 nfs_pgheader_init(desc, hdr, nfs_pgio_header_free);
1066 ret = nfs_generic_pgio(desc, hdr);
1068 if (NFS_SERVER(hdr->inode)->nfs_client->cl_minorversion)
1069 task_flags = RPC_TASK_MOVEABLE;
1070 ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode),
1073 NFS_PROTO(hdr->inode),
1074 desc->pg_rpc_callops,
1076 RPC_TASK_CRED_NOREF | task_flags);
1081 static struct nfs_pgio_mirror *
1082 nfs_pageio_alloc_mirrors(struct nfs_pageio_descriptor *desc,
1083 unsigned int mirror_count)
1085 struct nfs_pgio_mirror *ret;
1088 kfree(desc->pg_mirrors_dynamic);
1089 desc->pg_mirrors_dynamic = NULL;
1090 if (mirror_count == 1)
1091 return desc->pg_mirrors_static;
1092 ret = kmalloc_array(mirror_count, sizeof(*ret), nfs_io_gfp_mask());
1094 for (i = 0; i < mirror_count; i++)
1095 nfs_pageio_mirror_init(&ret[i], desc->pg_bsize);
1096 desc->pg_mirrors_dynamic = ret;
1102 * nfs_pageio_setup_mirroring - determine if mirroring is to be used
1103 * by calling the pg_get_mirror_count op
1105 static void nfs_pageio_setup_mirroring(struct nfs_pageio_descriptor *pgio,
1106 struct nfs_page *req)
1108 unsigned int mirror_count = 1;
1110 if (pgio->pg_ops->pg_get_mirror_count)
1111 mirror_count = pgio->pg_ops->pg_get_mirror_count(pgio, req);
1112 if (mirror_count == pgio->pg_mirror_count || pgio->pg_error < 0)
1115 if (!mirror_count || mirror_count > NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX) {
1116 pgio->pg_error = -EINVAL;
1120 pgio->pg_mirrors = nfs_pageio_alloc_mirrors(pgio, mirror_count);
1121 if (pgio->pg_mirrors == NULL) {
1122 pgio->pg_error = -ENOMEM;
1123 pgio->pg_mirrors = pgio->pg_mirrors_static;
1126 pgio->pg_mirror_count = mirror_count;
1129 static void nfs_pageio_cleanup_mirroring(struct nfs_pageio_descriptor *pgio)
1131 pgio->pg_mirror_count = 1;
1132 pgio->pg_mirror_idx = 0;
1133 pgio->pg_mirrors = pgio->pg_mirrors_static;
1134 kfree(pgio->pg_mirrors_dynamic);
1135 pgio->pg_mirrors_dynamic = NULL;
1138 static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
1139 const struct nfs_lock_context *l2)
1141 return l1->lockowner == l2->lockowner;
1144 static bool nfs_page_is_contiguous(const struct nfs_page *prev,
1145 const struct nfs_page *req)
1147 size_t prev_end = prev->wb_pgbase + prev->wb_bytes;
1149 if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
1151 if (req->wb_pgbase == 0)
1152 return prev_end == nfs_page_max_length(prev);
1153 if (req->wb_pgbase == prev_end) {
1154 struct folio *folio = nfs_page_to_folio(req);
1156 return folio == nfs_page_to_folio(prev);
1157 return req->wb_page == prev->wb_page;
1163 * nfs_coalesce_size - test two requests for compatibility
1164 * @prev: pointer to nfs_page
1165 * @req: pointer to nfs_page
1166 * @pgio: pointer to nfs_pagio_descriptor
1168 * The nfs_page structures 'prev' and 'req' are compared to ensure that the
1169 * page data area they describe is contiguous, and that their RPC
1170 * credentials, NFSv4 open state, and lockowners are the same.
1172 * Returns size of the request that can be coalesced
1174 static unsigned int nfs_coalesce_size(struct nfs_page *prev,
1175 struct nfs_page *req,
1176 struct nfs_pageio_descriptor *pgio)
1178 struct file_lock_context *flctx;
1181 if (!nfs_match_open_context(nfs_req_openctx(req), nfs_req_openctx(prev)))
1183 flctx = locks_inode_context(d_inode(nfs_req_openctx(req)->dentry));
1184 if (flctx != NULL &&
1185 !(list_empty_careful(&flctx->flc_posix) &&
1186 list_empty_careful(&flctx->flc_flock)) &&
1187 !nfs_match_lock_context(req->wb_lock_context,
1188 prev->wb_lock_context))
1190 if (!nfs_page_is_contiguous(prev, req))
1193 return pgio->pg_ops->pg_test(pgio, prev, req);
1197 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
1198 * @desc: destination io descriptor
1201 * If the request 'req' was successfully coalesced into the existing list
1202 * of pages 'desc', it returns the size of req.
1205 nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
1206 struct nfs_page *req)
1208 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
1209 struct nfs_page *prev = NULL;
1212 if (list_empty(&mirror->pg_list)) {
1213 if (desc->pg_ops->pg_init)
1214 desc->pg_ops->pg_init(desc, req);
1215 if (desc->pg_error < 0)
1217 mirror->pg_base = req->wb_pgbase;
1218 mirror->pg_count = 0;
1219 mirror->pg_recoalesce = 0;
1221 prev = nfs_list_entry(mirror->pg_list.prev);
1223 if (desc->pg_maxretrans && req->wb_nio > desc->pg_maxretrans) {
1224 if (NFS_SERVER(desc->pg_inode)->flags & NFS_MOUNT_SOFTERR)
1225 desc->pg_error = -ETIMEDOUT;
1227 desc->pg_error = -EIO;
1231 size = nfs_coalesce_size(prev, req, desc);
1232 if (size < req->wb_bytes)
1234 nfs_list_move_request(req, &mirror->pg_list);
1235 mirror->pg_count += req->wb_bytes;
1236 return req->wb_bytes;
1240 * Helper for nfs_pageio_add_request and nfs_pageio_complete
1242 static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
1244 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
1246 if (!list_empty(&mirror->pg_list)) {
1247 int error = desc->pg_ops->pg_doio(desc);
1249 desc->pg_error = error;
1250 if (list_empty(&mirror->pg_list))
1251 mirror->pg_bytes_written += mirror->pg_count;
1256 nfs_pageio_cleanup_request(struct nfs_pageio_descriptor *desc,
1257 struct nfs_page *req)
1261 nfs_list_move_request(req, &head);
1262 desc->pg_completion_ops->error_cleanup(&head, desc->pg_error);
1266 * __nfs_pageio_add_request - Attempt to coalesce a request into a page list.
1267 * @desc: destination io descriptor
1270 * This may split a request into subrequests which are all part of the
1271 * same page group. If so, it will submit @req as the last one, to ensure
1272 * the pointer to @req is still valid in case of failure.
1274 * Returns true if the request 'req' was successfully coalesced into the
1275 * existing list of pages 'desc'.
1277 static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
1278 struct nfs_page *req)
1280 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
1281 struct nfs_page *subreq;
1282 unsigned int size, subreq_size;
1284 nfs_page_group_lock(req);
1287 subreq_size = subreq->wb_bytes;
1289 size = nfs_pageio_do_add_request(desc, subreq);
1290 if (size == subreq_size) {
1291 /* We successfully submitted a request */
1294 req->wb_pgbase += size;
1295 req->wb_bytes -= size;
1296 req->wb_offset += size;
1297 subreq_size = req->wb_bytes;
1301 if (WARN_ON_ONCE(subreq != req)) {
1302 nfs_page_group_unlock(req);
1303 nfs_pageio_cleanup_request(desc, subreq);
1305 subreq_size = req->wb_bytes;
1306 nfs_page_group_lock(req);
1309 /* Can't coalesce any more, so do I/O */
1310 nfs_page_group_unlock(req);
1311 desc->pg_moreio = 1;
1312 nfs_pageio_doio(desc);
1313 if (desc->pg_error < 0 || mirror->pg_recoalesce)
1315 /* retry add_request for this subreq */
1316 nfs_page_group_lock(req);
1319 subreq = nfs_create_subreq(req, req->wb_pgbase,
1320 req->wb_offset, size);
1326 nfs_page_group_unlock(req);
1329 desc->pg_error = PTR_ERR(subreq);
1330 nfs_page_group_unlock(req);
1334 static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
1336 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
1340 list_splice_init(&mirror->pg_list, &head);
1341 mirror->pg_recoalesce = 0;
1343 while (!list_empty(&head)) {
1344 struct nfs_page *req;
1346 req = list_first_entry(&head, struct nfs_page, wb_list);
1347 if (__nfs_pageio_add_request(desc, req))
1349 if (desc->pg_error < 0) {
1350 list_splice_tail(&head, &mirror->pg_list);
1351 mirror->pg_recoalesce = 1;
1356 } while (mirror->pg_recoalesce);
1360 static int nfs_pageio_add_request_mirror(struct nfs_pageio_descriptor *desc,
1361 struct nfs_page *req)
1366 ret = __nfs_pageio_add_request(desc, req);
1369 if (desc->pg_error < 0)
1371 ret = nfs_do_recoalesce(desc);
1377 static void nfs_pageio_error_cleanup(struct nfs_pageio_descriptor *desc)
1380 struct nfs_pgio_mirror *mirror;
1382 if (!desc->pg_error)
1385 for (midx = 0; midx < desc->pg_mirror_count; midx++) {
1386 mirror = nfs_pgio_get_mirror(desc, midx);
1387 desc->pg_completion_ops->error_cleanup(&mirror->pg_list,
1392 int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
1393 struct nfs_page *req)
1396 unsigned int pgbase, offset, bytes;
1397 struct nfs_page *dupreq;
1399 pgbase = req->wb_pgbase;
1400 offset = req->wb_offset;
1401 bytes = req->wb_bytes;
1403 nfs_pageio_setup_mirroring(desc, req);
1404 if (desc->pg_error < 0)
1407 /* Create the mirror instances first, and fire them off */
1408 for (midx = 1; midx < desc->pg_mirror_count; midx++) {
1409 nfs_page_group_lock(req);
1411 dupreq = nfs_create_subreq(req,
1412 pgbase, offset, bytes);
1414 nfs_page_group_unlock(req);
1415 if (IS_ERR(dupreq)) {
1416 desc->pg_error = PTR_ERR(dupreq);
1420 nfs_pgio_set_current_mirror(desc, midx);
1421 if (!nfs_pageio_add_request_mirror(desc, dupreq))
1422 goto out_cleanup_subreq;
1425 nfs_pgio_set_current_mirror(desc, 0);
1426 if (!nfs_pageio_add_request_mirror(desc, req))
1432 nfs_pageio_cleanup_request(desc, dupreq);
1434 nfs_pageio_error_cleanup(desc);
1439 * nfs_pageio_complete_mirror - Complete I/O on the current mirror of an
1440 * nfs_pageio_descriptor
1441 * @desc: pointer to io descriptor
1442 * @mirror_idx: pointer to mirror index
1444 static void nfs_pageio_complete_mirror(struct nfs_pageio_descriptor *desc,
1447 struct nfs_pgio_mirror *mirror;
1450 restore_idx = nfs_pgio_set_current_mirror(desc, mirror_idx);
1451 mirror = nfs_pgio_current_mirror(desc);
1454 nfs_pageio_doio(desc);
1455 if (desc->pg_error < 0 || !mirror->pg_recoalesce)
1457 if (!nfs_do_recoalesce(desc))
1460 nfs_pgio_set_current_mirror(desc, restore_idx);
1464 * nfs_pageio_resend - Transfer requests to new descriptor and resend
1465 * @hdr - the pgio header to move request from
1466 * @desc - the pageio descriptor to add requests to
1468 * Try to move each request (nfs_page) from @hdr to @desc then attempt
1471 * Returns 0 on success and < 0 on error.
1473 int nfs_pageio_resend(struct nfs_pageio_descriptor *desc,
1474 struct nfs_pgio_header *hdr)
1478 desc->pg_io_completion = hdr->io_completion;
1479 desc->pg_dreq = hdr->dreq;
1480 list_splice_init(&hdr->pages, &pages);
1481 while (!list_empty(&pages)) {
1482 struct nfs_page *req = nfs_list_entry(pages.next);
1484 if (!nfs_pageio_add_request(desc, req))
1487 nfs_pageio_complete(desc);
1488 if (!list_empty(&pages)) {
1489 int err = desc->pg_error < 0 ? desc->pg_error : -EIO;
1490 hdr->completion_ops->error_cleanup(&pages, err);
1491 nfs_set_pgio_error(hdr, err, hdr->io_start);
1496 EXPORT_SYMBOL_GPL(nfs_pageio_resend);
1499 * nfs_pageio_complete - Complete I/O then cleanup an nfs_pageio_descriptor
1500 * @desc: pointer to io descriptor
1502 void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
1506 for (midx = 0; midx < desc->pg_mirror_count; midx++)
1507 nfs_pageio_complete_mirror(desc, midx);
1509 if (desc->pg_error < 0)
1510 nfs_pageio_error_cleanup(desc);
1511 if (desc->pg_ops->pg_cleanup)
1512 desc->pg_ops->pg_cleanup(desc);
1513 nfs_pageio_cleanup_mirroring(desc);
1517 * nfs_pageio_cond_complete - Conditional I/O completion
1518 * @desc: pointer to io descriptor
1519 * @index: page index
1521 * It is important to ensure that processes don't try to take locks
1522 * on non-contiguous ranges of pages as that might deadlock. This
1523 * function should be called before attempting to wait on a locked
1524 * nfs_page. It will complete the I/O if the page index 'index'
1525 * is not contiguous with the existing list of pages in 'desc'.
1527 void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
1529 struct nfs_pgio_mirror *mirror;
1530 struct nfs_page *prev;
1531 struct folio *folio;
1534 for (midx = 0; midx < desc->pg_mirror_count; midx++) {
1535 mirror = nfs_pgio_get_mirror(desc, midx);
1536 if (!list_empty(&mirror->pg_list)) {
1537 prev = nfs_list_entry(mirror->pg_list.prev);
1538 folio = nfs_page_to_folio(prev);
1540 if (index == folio_next_index(folio))
1542 } else if (index == prev->wb_index + 1)
1544 nfs_pageio_complete(desc);
1551 * nfs_pageio_stop_mirroring - stop using mirroring (set mirror count to 1)
1553 void nfs_pageio_stop_mirroring(struct nfs_pageio_descriptor *pgio)
1555 nfs_pageio_complete(pgio);
1558 int __init nfs_init_nfspagecache(void)
1560 nfs_page_cachep = kmem_cache_create("nfs_page",
1561 sizeof(struct nfs_page),
1562 0, SLAB_HWCACHE_ALIGN,
1564 if (nfs_page_cachep == NULL)
1570 void nfs_destroy_nfspagecache(void)
1572 kmem_cache_destroy(nfs_page_cachep);
1575 static const struct rpc_call_ops nfs_pgio_common_ops = {
1576 .rpc_call_prepare = nfs_pgio_prepare,
1577 .rpc_call_done = nfs_pgio_result,
1578 .rpc_release = nfs_pgio_release,
1581 const struct nfs_pageio_ops nfs_pgio_rw_ops = {
1582 .pg_test = nfs_generic_pg_test,
1583 .pg_doio = nfs_generic_pg_pgios,