2 * linux/fs/nfs/pagelist.c
4 * A set of helper functions for managing NFS read and write requests.
5 * The main purpose of these routines is to provide support for the
6 * coalescing of several requests into a single RPC call.
8 * Copyright 2000, 2001 (c) Trond Myklebust <trond.myklebust@fys.uio.no>
12 #include <linux/slab.h>
13 #include <linux/file.h>
14 #include <linux/sched.h>
15 #include <linux/sunrpc/clnt.h>
16 #include <linux/nfs.h>
17 #include <linux/nfs3.h>
18 #include <linux/nfs4.h>
19 #include <linux/nfs_page.h>
20 #include <linux/nfs_fs.h>
21 #include <linux/nfs_mount.h>
22 #include <linux/export.h>
27 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
29 static struct kmem_cache *nfs_page_cachep;
30 static const struct rpc_call_ops nfs_pgio_common_ops;
32 struct nfs_pgio_mirror *
33 nfs_pgio_current_mirror(struct nfs_pageio_descriptor *desc)
35 return nfs_pgio_has_mirroring(desc) ?
36 &desc->pg_mirrors[desc->pg_mirror_idx] :
39 EXPORT_SYMBOL_GPL(nfs_pgio_current_mirror);
41 void nfs_pgheader_init(struct nfs_pageio_descriptor *desc,
42 struct nfs_pgio_header *hdr,
43 void (*release)(struct nfs_pgio_header *hdr))
45 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
48 hdr->req = nfs_list_entry(mirror->pg_list.next);
49 hdr->inode = desc->pg_inode;
50 hdr->cred = hdr->req->wb_context->cred;
51 hdr->io_start = req_offset(hdr->req);
52 hdr->good_bytes = mirror->pg_count;
53 hdr->io_completion = desc->pg_io_completion;
54 hdr->dreq = desc->pg_dreq;
55 hdr->release = release;
56 hdr->completion_ops = desc->pg_completion_ops;
57 if (hdr->completion_ops->init_hdr)
58 hdr->completion_ops->init_hdr(hdr);
60 hdr->pgio_mirror_idx = desc->pg_mirror_idx;
62 EXPORT_SYMBOL_GPL(nfs_pgheader_init);
64 void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos)
66 spin_lock(&hdr->lock);
67 if (!test_and_set_bit(NFS_IOHDR_ERROR, &hdr->flags)
68 || pos < hdr->io_start + hdr->good_bytes) {
69 clear_bit(NFS_IOHDR_EOF, &hdr->flags);
70 hdr->good_bytes = pos - hdr->io_start;
73 spin_unlock(&hdr->lock);
76 static inline struct nfs_page *
79 struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_NOIO);
81 INIT_LIST_HEAD(&p->wb_list);
86 nfs_page_free(struct nfs_page *p)
88 kmem_cache_free(nfs_page_cachep, p);
92 * nfs_iocounter_wait - wait for i/o to complete
93 * @l_ctx: nfs_lock_context with io_counter to use
95 * returns -ERESTARTSYS if interrupted by a fatal signal.
96 * Otherwise returns 0 once the io_count hits 0.
99 nfs_iocounter_wait(struct nfs_lock_context *l_ctx)
101 return wait_on_atomic_t(&l_ctx->io_count, nfs_wait_atomic_killable,
106 * nfs_async_iocounter_wait - wait on a rpc_waitqueue for I/O
108 * @task: the rpc_task that should wait
109 * @l_ctx: nfs_lock_context with io_counter to check
111 * Returns true if there is outstanding I/O to wait on and the
112 * task has been put to sleep.
115 nfs_async_iocounter_wait(struct rpc_task *task, struct nfs_lock_context *l_ctx)
117 struct inode *inode = d_inode(l_ctx->open_context->dentry);
120 if (atomic_read(&l_ctx->io_count) > 0) {
121 rpc_sleep_on(&NFS_SERVER(inode)->uoc_rpcwaitq, task, NULL);
125 if (atomic_read(&l_ctx->io_count) == 0) {
126 rpc_wake_up_queued_task(&NFS_SERVER(inode)->uoc_rpcwaitq, task);
132 EXPORT_SYMBOL_GPL(nfs_async_iocounter_wait);
135 * nfs_page_set_headlock - set the request PG_HEADLOCK
136 * @req: request that is to be locked
138 * this lock must be held when modifying req->wb_head
140 * return 0 on success, < 0 on error
143 nfs_page_set_headlock(struct nfs_page *req)
145 if (!test_and_set_bit(PG_HEADLOCK, &req->wb_flags))
148 set_bit(PG_CONTENDED1, &req->wb_flags);
149 smp_mb__after_atomic();
150 return wait_on_bit_lock(&req->wb_flags, PG_HEADLOCK,
151 TASK_UNINTERRUPTIBLE);
155 * nfs_page_clear_headlock - clear the request PG_HEADLOCK
156 * @req: request that is to be locked
159 nfs_page_clear_headlock(struct nfs_page *req)
161 smp_mb__before_atomic();
162 clear_bit(PG_HEADLOCK, &req->wb_flags);
163 smp_mb__after_atomic();
164 if (!test_bit(PG_CONTENDED1, &req->wb_flags))
166 wake_up_bit(&req->wb_flags, PG_HEADLOCK);
170 * nfs_page_group_lock - lock the head of the page group
171 * @req: request in group that is to be locked
173 * this lock must be held when traversing or modifying the page
176 * return 0 on success, < 0 on error
179 nfs_page_group_lock(struct nfs_page *req)
183 ret = nfs_page_set_headlock(req);
184 if (ret || req->wb_head == req)
186 return nfs_page_set_headlock(req->wb_head);
190 * nfs_page_group_unlock - unlock the head of the page group
191 * @req: request in group that is to be unlocked
194 nfs_page_group_unlock(struct nfs_page *req)
196 if (req != req->wb_head)
197 nfs_page_clear_headlock(req->wb_head);
198 nfs_page_clear_headlock(req);
202 * nfs_page_group_sync_on_bit_locked
204 * must be called with page group lock held
207 nfs_page_group_sync_on_bit_locked(struct nfs_page *req, unsigned int bit)
209 struct nfs_page *head = req->wb_head;
210 struct nfs_page *tmp;
212 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_flags));
213 WARN_ON_ONCE(test_and_set_bit(bit, &req->wb_flags));
215 tmp = req->wb_this_page;
217 if (!test_bit(bit, &tmp->wb_flags))
219 tmp = tmp->wb_this_page;
222 /* true! reset all bits */
225 clear_bit(bit, &tmp->wb_flags);
226 tmp = tmp->wb_this_page;
227 } while (tmp != req);
233 * nfs_page_group_sync_on_bit - set bit on current request, but only
234 * return true if the bit is set for all requests in page group
235 * @req - request in page group
236 * @bit - PG_* bit that is used to sync page group
238 bool nfs_page_group_sync_on_bit(struct nfs_page *req, unsigned int bit)
242 nfs_page_group_lock(req);
243 ret = nfs_page_group_sync_on_bit_locked(req, bit);
244 nfs_page_group_unlock(req);
250 * nfs_page_group_init - Initialize the page group linkage for @req
251 * @req - a new nfs request
252 * @prev - the previous request in page group, or NULL if @req is the first
253 * or only request in the group (the head).
256 nfs_page_group_init(struct nfs_page *req, struct nfs_page *prev)
259 WARN_ON_ONCE(prev == req);
264 req->wb_this_page = req;
267 WARN_ON_ONCE(prev->wb_this_page != prev->wb_head);
268 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &prev->wb_head->wb_flags));
269 req->wb_head = prev->wb_head;
270 req->wb_this_page = prev->wb_this_page;
271 prev->wb_this_page = req;
273 /* All subrequests take a ref on the head request until
274 * nfs_page_group_destroy is called */
275 kref_get(&req->wb_head->wb_kref);
277 /* grab extra ref and bump the request count if head request
278 * has extra ref from the write/commit path to handle handoff
279 * between write and commit lists. */
280 if (test_bit(PG_INODE_REF, &prev->wb_head->wb_flags)) {
281 inode = page_file_mapping(req->wb_page)->host;
282 set_bit(PG_INODE_REF, &req->wb_flags);
283 kref_get(&req->wb_kref);
284 atomic_long_inc(&NFS_I(inode)->nrequests);
290 * nfs_page_group_destroy - sync the destruction of page groups
291 * @req - request that no longer needs the page group
293 * releases the page group reference from each member once all
294 * members have called this function.
297 nfs_page_group_destroy(struct kref *kref)
299 struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
300 struct nfs_page *head = req->wb_head;
301 struct nfs_page *tmp, *next;
303 if (!nfs_page_group_sync_on_bit(req, PG_TEARDOWN))
308 next = tmp->wb_this_page;
309 /* unlink and free */
310 tmp->wb_this_page = tmp;
312 nfs_free_request(tmp);
314 } while (tmp != req);
316 /* subrequests must release the ref on the head request */
318 nfs_release_request(head);
322 * nfs_create_request - Create an NFS read/write request.
323 * @ctx: open context to use
324 * @page: page to write
325 * @last: last nfs request created for this page group or NULL if head
326 * @offset: starting offset within the page for the write
327 * @count: number of bytes to read/write
329 * The page must be locked by the caller. This makes sure we never
330 * create two different requests for the same page.
331 * User should ensure it is safe to sleep in this function.
334 nfs_create_request(struct nfs_open_context *ctx, struct page *page,
335 struct nfs_page *last, unsigned int offset,
338 struct nfs_page *req;
339 struct nfs_lock_context *l_ctx;
341 if (test_bit(NFS_CONTEXT_BAD, &ctx->flags))
342 return ERR_PTR(-EBADF);
343 /* try to allocate the request struct */
344 req = nfs_page_alloc();
346 return ERR_PTR(-ENOMEM);
348 /* get lock context early so we can deal with alloc failures */
349 l_ctx = nfs_get_lock_context(ctx);
352 return ERR_CAST(l_ctx);
354 req->wb_lock_context = l_ctx;
355 atomic_inc(&l_ctx->io_count);
357 /* Initialize the request struct. Initially, we assume a
358 * long write-back delay. This will be adjusted in
359 * update_nfs_request below if the region is not locked. */
362 req->wb_index = page_index(page);
365 req->wb_offset = offset;
366 req->wb_pgbase = offset;
367 req->wb_bytes = count;
368 req->wb_context = get_nfs_open_context(ctx);
369 kref_init(&req->wb_kref);
370 nfs_page_group_init(req, last);
375 * nfs_unlock_request - Unlock request and wake up sleepers.
378 void nfs_unlock_request(struct nfs_page *req)
380 if (!NFS_WBACK_BUSY(req)) {
381 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
384 smp_mb__before_atomic();
385 clear_bit(PG_BUSY, &req->wb_flags);
386 smp_mb__after_atomic();
387 if (!test_bit(PG_CONTENDED2, &req->wb_flags))
389 wake_up_bit(&req->wb_flags, PG_BUSY);
393 * nfs_unlock_and_release_request - Unlock request and release the nfs_page
396 void nfs_unlock_and_release_request(struct nfs_page *req)
398 nfs_unlock_request(req);
399 nfs_release_request(req);
403 * nfs_clear_request - Free up all resources allocated to the request
406 * Release page and open context resources associated with a read/write
407 * request after it has completed.
409 static void nfs_clear_request(struct nfs_page *req)
411 struct page *page = req->wb_page;
412 struct nfs_open_context *ctx = req->wb_context;
413 struct nfs_lock_context *l_ctx = req->wb_lock_context;
420 if (atomic_dec_and_test(&l_ctx->io_count)) {
421 wake_up_atomic_t(&l_ctx->io_count);
422 if (test_bit(NFS_CONTEXT_UNLOCK, &ctx->flags))
423 rpc_wake_up(&NFS_SERVER(d_inode(ctx->dentry))->uoc_rpcwaitq);
425 nfs_put_lock_context(l_ctx);
426 req->wb_lock_context = NULL;
429 put_nfs_open_context(ctx);
430 req->wb_context = NULL;
435 * nfs_release_request - Release the count on an NFS read/write request
436 * @req: request to release
438 * Note: Should never be called with the spinlock held!
440 void nfs_free_request(struct nfs_page *req)
442 WARN_ON_ONCE(req->wb_this_page != req);
444 /* extra debug: make sure no sync bits are still set */
445 WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
446 WARN_ON_ONCE(test_bit(PG_UNLOCKPAGE, &req->wb_flags));
447 WARN_ON_ONCE(test_bit(PG_UPTODATE, &req->wb_flags));
448 WARN_ON_ONCE(test_bit(PG_WB_END, &req->wb_flags));
449 WARN_ON_ONCE(test_bit(PG_REMOVE, &req->wb_flags));
451 /* Release struct file and open context */
452 nfs_clear_request(req);
456 void nfs_release_request(struct nfs_page *req)
458 kref_put(&req->wb_kref, nfs_page_group_destroy);
460 EXPORT_SYMBOL_GPL(nfs_release_request);
463 * nfs_wait_on_request - Wait for a request to complete.
464 * @req: request to wait upon.
466 * Interruptible by fatal signals only.
467 * The user is responsible for holding a count on the request.
470 nfs_wait_on_request(struct nfs_page *req)
472 if (!test_bit(PG_BUSY, &req->wb_flags))
474 set_bit(PG_CONTENDED2, &req->wb_flags);
475 smp_mb__after_atomic();
476 return wait_on_bit_io(&req->wb_flags, PG_BUSY,
477 TASK_UNINTERRUPTIBLE);
479 EXPORT_SYMBOL_GPL(nfs_wait_on_request);
482 * nfs_generic_pg_test - determine if requests can be coalesced
483 * @desc: pointer to descriptor
484 * @prev: previous request in desc, or NULL
487 * Returns zero if @req can be coalesced into @desc, otherwise it returns
488 * the size of the request.
490 size_t nfs_generic_pg_test(struct nfs_pageio_descriptor *desc,
491 struct nfs_page *prev, struct nfs_page *req)
493 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
496 if (mirror->pg_count > mirror->pg_bsize) {
497 /* should never happen */
503 * Limit the request size so that we can still allocate a page array
504 * for it without upsetting the slab allocator.
506 if (((mirror->pg_count + req->wb_bytes) >> PAGE_SHIFT) *
507 sizeof(struct page *) > PAGE_SIZE)
510 return min(mirror->pg_bsize - mirror->pg_count, (size_t)req->wb_bytes);
512 EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
514 struct nfs_pgio_header *nfs_pgio_header_alloc(const struct nfs_rw_ops *ops)
516 struct nfs_pgio_header *hdr = ops->rw_alloc_header();
519 INIT_LIST_HEAD(&hdr->pages);
520 spin_lock_init(&hdr->lock);
525 EXPORT_SYMBOL_GPL(nfs_pgio_header_alloc);
528 * nfs_pgio_data_destroy - make @hdr suitable for reuse
530 * Frees memory and releases refs from nfs_generic_pgio, so that it may
533 * @hdr: A header that has had nfs_generic_pgio called
535 static void nfs_pgio_data_destroy(struct nfs_pgio_header *hdr)
537 if (hdr->args.context)
538 put_nfs_open_context(hdr->args.context);
539 if (hdr->page_array.pagevec != hdr->page_array.page_array)
540 kfree(hdr->page_array.pagevec);
544 * nfs_pgio_header_free - Free a read or write header
545 * @hdr: The header to free
547 void nfs_pgio_header_free(struct nfs_pgio_header *hdr)
549 nfs_pgio_data_destroy(hdr);
550 hdr->rw_ops->rw_free_header(hdr);
552 EXPORT_SYMBOL_GPL(nfs_pgio_header_free);
555 * nfs_pgio_rpcsetup - Set up arguments for a pageio call
556 * @hdr: The pageio hdr
557 * @count: Number of bytes to read
558 * @offset: Initial offset
559 * @how: How to commit data (writes only)
560 * @cinfo: Commit information for the call (writes only)
562 static void nfs_pgio_rpcsetup(struct nfs_pgio_header *hdr,
563 unsigned int count, unsigned int offset,
564 int how, struct nfs_commit_info *cinfo)
566 struct nfs_page *req = hdr->req;
568 /* Set up the RPC argument and reply structs
569 * NB: take care not to mess about with hdr->commit et al. */
571 hdr->args.fh = NFS_FH(hdr->inode);
572 hdr->args.offset = req_offset(req) + offset;
573 /* pnfs_set_layoutcommit needs this */
574 hdr->mds_offset = hdr->args.offset;
575 hdr->args.pgbase = req->wb_pgbase + offset;
576 hdr->args.pages = hdr->page_array.pagevec;
577 hdr->args.count = count;
578 hdr->args.context = get_nfs_open_context(req->wb_context);
579 hdr->args.lock_context = req->wb_lock_context;
580 hdr->args.stable = NFS_UNSTABLE;
581 switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
584 case FLUSH_COND_STABLE:
585 if (nfs_reqs_to_commit(cinfo))
588 hdr->args.stable = NFS_FILE_SYNC;
591 hdr->res.fattr = &hdr->fattr;
594 hdr->res.verf = &hdr->verf;
595 nfs_fattr_init(&hdr->fattr);
599 * nfs_pgio_prepare - Prepare pageio hdr to go over the wire
600 * @task: The current task
601 * @calldata: pageio header to prepare
603 static void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
605 struct nfs_pgio_header *hdr = calldata;
607 err = NFS_PROTO(hdr->inode)->pgio_rpc_prepare(task, hdr);
612 int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr,
613 struct rpc_cred *cred, const struct nfs_rpc_ops *rpc_ops,
614 const struct rpc_call_ops *call_ops, int how, int flags)
616 struct rpc_task *task;
617 struct rpc_message msg = {
618 .rpc_argp = &hdr->args,
619 .rpc_resp = &hdr->res,
622 struct rpc_task_setup task_setup_data = {
626 .callback_ops = call_ops,
627 .callback_data = hdr,
628 .workqueue = nfsiod_workqueue,
629 .flags = RPC_TASK_ASYNC | flags,
633 hdr->rw_ops->rw_initiate(hdr, &msg, rpc_ops, &task_setup_data, how);
635 dprintk("NFS: initiated pgio call "
636 "(req %s/%llu, %u bytes @ offset %llu)\n",
637 hdr->inode->i_sb->s_id,
638 (unsigned long long)NFS_FILEID(hdr->inode),
640 (unsigned long long)hdr->args.offset);
642 task = rpc_run_task(&task_setup_data);
647 if (how & FLUSH_SYNC) {
648 ret = rpc_wait_for_completion_task(task);
650 ret = task->tk_status;
656 EXPORT_SYMBOL_GPL(nfs_initiate_pgio);
659 * nfs_pgio_error - Clean up from a pageio error
660 * @desc: IO descriptor
661 * @hdr: pageio header
663 static void nfs_pgio_error(struct nfs_pgio_header *hdr)
665 set_bit(NFS_IOHDR_REDO, &hdr->flags);
666 hdr->completion_ops->completion(hdr);
670 * nfs_pgio_release - Release pageio data
671 * @calldata: The pageio header to release
673 static void nfs_pgio_release(void *calldata)
675 struct nfs_pgio_header *hdr = calldata;
676 hdr->completion_ops->completion(hdr);
679 static void nfs_pageio_mirror_init(struct nfs_pgio_mirror *mirror,
682 INIT_LIST_HEAD(&mirror->pg_list);
683 mirror->pg_bytes_written = 0;
684 mirror->pg_count = 0;
685 mirror->pg_bsize = bsize;
687 mirror->pg_recoalesce = 0;
691 * nfs_pageio_init - initialise a page io descriptor
692 * @desc: pointer to descriptor
693 * @inode: pointer to inode
694 * @pg_ops: pointer to pageio operations
695 * @compl_ops: pointer to pageio completion operations
696 * @rw_ops: pointer to nfs read/write operations
697 * @bsize: io block size
698 * @io_flags: extra parameters for the io function
700 void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
702 const struct nfs_pageio_ops *pg_ops,
703 const struct nfs_pgio_completion_ops *compl_ops,
704 const struct nfs_rw_ops *rw_ops,
709 desc->pg_inode = inode;
710 desc->pg_ops = pg_ops;
711 desc->pg_completion_ops = compl_ops;
712 desc->pg_rw_ops = rw_ops;
713 desc->pg_ioflags = io_flags;
715 desc->pg_lseg = NULL;
716 desc->pg_io_completion = NULL;
717 desc->pg_dreq = NULL;
718 desc->pg_bsize = bsize;
720 desc->pg_mirror_count = 1;
721 desc->pg_mirror_idx = 0;
723 desc->pg_mirrors_dynamic = NULL;
724 desc->pg_mirrors = desc->pg_mirrors_static;
725 nfs_pageio_mirror_init(&desc->pg_mirrors[0], bsize);
729 * nfs_pgio_result - Basic pageio error handling
730 * @task: The task that ran
731 * @calldata: Pageio header to check
733 static void nfs_pgio_result(struct rpc_task *task, void *calldata)
735 struct nfs_pgio_header *hdr = calldata;
736 struct inode *inode = hdr->inode;
738 dprintk("NFS: %s: %5u, (status %d)\n", __func__,
739 task->tk_pid, task->tk_status);
741 if (hdr->rw_ops->rw_done(task, hdr, inode) != 0)
743 if (task->tk_status < 0)
744 nfs_set_pgio_error(hdr, task->tk_status, hdr->args.offset);
746 hdr->rw_ops->rw_result(task, hdr);
750 * Create an RPC task for the given read or write request and kick it.
751 * The page must have been locked by the caller.
753 * It may happen that the page we're passed is not marked dirty.
754 * This is the case if nfs_updatepage detects a conflicting request
755 * that has been written but not committed.
757 int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
758 struct nfs_pgio_header *hdr)
760 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
762 struct nfs_page *req;
765 struct list_head *head = &mirror->pg_list;
766 struct nfs_commit_info cinfo;
767 struct nfs_page_array *pg_array = &hdr->page_array;
768 unsigned int pagecount, pageused;
769 gfp_t gfp_flags = GFP_KERNEL;
771 pagecount = nfs_page_array_len(mirror->pg_base, mirror->pg_count);
772 pg_array->npages = pagecount;
774 if (pagecount <= ARRAY_SIZE(pg_array->page_array))
775 pg_array->pagevec = pg_array->page_array;
777 if (hdr->rw_mode == FMODE_WRITE)
778 gfp_flags = GFP_NOIO;
779 pg_array->pagevec = kcalloc(pagecount, sizeof(struct page *), gfp_flags);
780 if (!pg_array->pagevec) {
781 pg_array->npages = 0;
783 desc->pg_error = -ENOMEM;
784 return desc->pg_error;
788 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
789 pages = hdr->page_array.pagevec;
792 while (!list_empty(head)) {
793 req = nfs_list_entry(head->next);
794 nfs_list_move_request(req, &hdr->pages);
796 if (!last_page || last_page != req->wb_page) {
798 if (pageused > pagecount)
800 *pages++ = last_page = req->wb_page;
803 if (WARN_ON_ONCE(pageused != pagecount)) {
805 desc->pg_error = -EINVAL;
806 return desc->pg_error;
809 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
810 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
811 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
813 /* Set up the argument struct */
814 nfs_pgio_rpcsetup(hdr, mirror->pg_count, 0, desc->pg_ioflags, &cinfo);
815 desc->pg_rpc_callops = &nfs_pgio_common_ops;
818 EXPORT_SYMBOL_GPL(nfs_generic_pgio);
820 static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
822 struct nfs_pgio_header *hdr;
825 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
827 desc->pg_error = -ENOMEM;
828 return desc->pg_error;
830 nfs_pgheader_init(desc, hdr, nfs_pgio_header_free);
831 ret = nfs_generic_pgio(desc, hdr);
833 ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode),
836 NFS_PROTO(hdr->inode),
837 desc->pg_rpc_callops,
838 desc->pg_ioflags, 0);
842 static struct nfs_pgio_mirror *
843 nfs_pageio_alloc_mirrors(struct nfs_pageio_descriptor *desc,
844 unsigned int mirror_count)
846 struct nfs_pgio_mirror *ret;
849 kfree(desc->pg_mirrors_dynamic);
850 desc->pg_mirrors_dynamic = NULL;
851 if (mirror_count == 1)
852 return desc->pg_mirrors_static;
853 ret = kmalloc_array(mirror_count, sizeof(*ret), GFP_NOFS);
855 for (i = 0; i < mirror_count; i++)
856 nfs_pageio_mirror_init(&ret[i], desc->pg_bsize);
857 desc->pg_mirrors_dynamic = ret;
863 * nfs_pageio_setup_mirroring - determine if mirroring is to be used
864 * by calling the pg_get_mirror_count op
866 static void nfs_pageio_setup_mirroring(struct nfs_pageio_descriptor *pgio,
867 struct nfs_page *req)
869 unsigned int mirror_count = 1;
871 if (pgio->pg_ops->pg_get_mirror_count)
872 mirror_count = pgio->pg_ops->pg_get_mirror_count(pgio, req);
873 if (mirror_count == pgio->pg_mirror_count || pgio->pg_error < 0)
876 if (!mirror_count || mirror_count > NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX) {
877 pgio->pg_error = -EINVAL;
881 pgio->pg_mirrors = nfs_pageio_alloc_mirrors(pgio, mirror_count);
882 if (pgio->pg_mirrors == NULL) {
883 pgio->pg_error = -ENOMEM;
884 pgio->pg_mirrors = pgio->pg_mirrors_static;
887 pgio->pg_mirror_count = mirror_count;
890 static void nfs_pageio_cleanup_mirroring(struct nfs_pageio_descriptor *pgio)
892 pgio->pg_mirror_count = 1;
893 pgio->pg_mirror_idx = 0;
894 pgio->pg_mirrors = pgio->pg_mirrors_static;
895 kfree(pgio->pg_mirrors_dynamic);
896 pgio->pg_mirrors_dynamic = NULL;
899 static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
900 const struct nfs_lock_context *l2)
902 return l1->lockowner == l2->lockowner;
906 * nfs_can_coalesce_requests - test two requests for compatibility
907 * @prev: pointer to nfs_page
908 * @req: pointer to nfs_page
910 * The nfs_page structures 'prev' and 'req' are compared to ensure that the
911 * page data area they describe is contiguous, and that their RPC
912 * credentials, NFSv4 open state, and lockowners are the same.
914 * Return 'true' if this is the case, else return 'false'.
916 static bool nfs_can_coalesce_requests(struct nfs_page *prev,
917 struct nfs_page *req,
918 struct nfs_pageio_descriptor *pgio)
921 struct file_lock_context *flctx;
924 if (!nfs_match_open_context(req->wb_context, prev->wb_context))
926 flctx = d_inode(req->wb_context->dentry)->i_flctx;
928 !(list_empty_careful(&flctx->flc_posix) &&
929 list_empty_careful(&flctx->flc_flock)) &&
930 !nfs_match_lock_context(req->wb_lock_context,
931 prev->wb_lock_context))
933 if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
935 if (req->wb_page == prev->wb_page) {
936 if (req->wb_pgbase != prev->wb_pgbase + prev->wb_bytes)
939 if (req->wb_pgbase != 0 ||
940 prev->wb_pgbase + prev->wb_bytes != PAGE_SIZE)
944 size = pgio->pg_ops->pg_test(pgio, prev, req);
945 WARN_ON_ONCE(size > req->wb_bytes);
946 if (size && size < req->wb_bytes)
947 req->wb_bytes = size;
952 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
953 * @desc: destination io descriptor
956 * Returns true if the request 'req' was successfully coalesced into the
957 * existing list of pages 'desc'.
959 static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
960 struct nfs_page *req)
962 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
964 struct nfs_page *prev = NULL;
966 if (mirror->pg_count != 0) {
967 prev = nfs_list_entry(mirror->pg_list.prev);
969 if (desc->pg_ops->pg_init)
970 desc->pg_ops->pg_init(desc, req);
971 if (desc->pg_error < 0)
973 mirror->pg_base = req->wb_pgbase;
975 if (!nfs_can_coalesce_requests(prev, req, desc))
977 nfs_list_move_request(req, &mirror->pg_list);
978 mirror->pg_count += req->wb_bytes;
983 * Helper for nfs_pageio_add_request and nfs_pageio_complete
985 static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
987 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
989 if (!list_empty(&mirror->pg_list)) {
990 int error = desc->pg_ops->pg_doio(desc);
992 desc->pg_error = error;
993 if (list_empty(&mirror->pg_list)) {
994 mirror->pg_bytes_written += mirror->pg_count;
995 mirror->pg_count = 0;
997 mirror->pg_recoalesce = 0;
1003 nfs_pageio_cleanup_request(struct nfs_pageio_descriptor *desc,
1004 struct nfs_page *req)
1008 nfs_list_move_request(req, &head);
1009 desc->pg_completion_ops->error_cleanup(&head, desc->pg_error);
1013 * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
1014 * @desc: destination io descriptor
1017 * This may split a request into subrequests which are all part of the
1020 * Returns true if the request 'req' was successfully coalesced into the
1021 * existing list of pages 'desc'.
1023 static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
1024 struct nfs_page *req)
1026 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
1028 struct nfs_page *subreq;
1029 unsigned int bytes_left = 0;
1030 unsigned int offset, pgbase;
1032 nfs_page_group_lock(req);
1035 bytes_left = subreq->wb_bytes;
1036 offset = subreq->wb_offset;
1037 pgbase = subreq->wb_pgbase;
1040 if (!nfs_pageio_do_add_request(desc, subreq)) {
1041 /* make sure pg_test call(s) did nothing */
1042 WARN_ON_ONCE(subreq->wb_bytes != bytes_left);
1043 WARN_ON_ONCE(subreq->wb_offset != offset);
1044 WARN_ON_ONCE(subreq->wb_pgbase != pgbase);
1046 nfs_page_group_unlock(req);
1047 desc->pg_moreio = 1;
1048 nfs_pageio_doio(desc);
1049 if (desc->pg_error < 0 || mirror->pg_recoalesce)
1050 goto out_cleanup_subreq;
1051 /* retry add_request for this subreq */
1052 nfs_page_group_lock(req);
1056 /* check for buggy pg_test call(s) */
1057 WARN_ON_ONCE(subreq->wb_bytes + subreq->wb_pgbase > PAGE_SIZE);
1058 WARN_ON_ONCE(subreq->wb_bytes > bytes_left);
1059 WARN_ON_ONCE(subreq->wb_bytes == 0);
1061 bytes_left -= subreq->wb_bytes;
1062 offset += subreq->wb_bytes;
1063 pgbase += subreq->wb_bytes;
1066 subreq = nfs_create_request(req->wb_context,
1068 subreq, pgbase, bytes_left);
1071 nfs_lock_request(subreq);
1072 subreq->wb_offset = offset;
1073 subreq->wb_index = req->wb_index;
1075 } while (bytes_left > 0);
1077 nfs_page_group_unlock(req);
1080 desc->pg_error = PTR_ERR(subreq);
1081 nfs_page_group_unlock(req);
1085 nfs_pageio_cleanup_request(desc, subreq);
1089 static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
1091 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
1095 list_splice_init(&mirror->pg_list, &head);
1096 mirror->pg_count = 0;
1097 mirror->pg_base = 0;
1098 mirror->pg_recoalesce = 0;
1100 while (!list_empty(&head)) {
1101 struct nfs_page *req;
1103 req = list_first_entry(&head, struct nfs_page, wb_list);
1104 if (__nfs_pageio_add_request(desc, req))
1106 if (desc->pg_error < 0) {
1107 list_splice_tail(&head, &mirror->pg_list);
1108 mirror->pg_recoalesce = 1;
1113 } while (mirror->pg_recoalesce);
1117 static int nfs_pageio_add_request_mirror(struct nfs_pageio_descriptor *desc,
1118 struct nfs_page *req)
1123 ret = __nfs_pageio_add_request(desc, req);
1126 if (desc->pg_error < 0)
1128 ret = nfs_do_recoalesce(desc);
1134 static void nfs_pageio_error_cleanup(struct nfs_pageio_descriptor *desc)
1137 struct nfs_pgio_mirror *mirror;
1139 if (!desc->pg_error)
1142 for (midx = 0; midx < desc->pg_mirror_count; midx++) {
1143 mirror = &desc->pg_mirrors[midx];
1144 desc->pg_completion_ops->error_cleanup(&mirror->pg_list,
1149 int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
1150 struct nfs_page *req)
1153 unsigned int pgbase, offset, bytes;
1154 struct nfs_page *dupreq, *lastreq;
1156 pgbase = req->wb_pgbase;
1157 offset = req->wb_offset;
1158 bytes = req->wb_bytes;
1160 nfs_pageio_setup_mirroring(desc, req);
1161 if (desc->pg_error < 0)
1164 for (midx = 0; midx < desc->pg_mirror_count; midx++) {
1166 nfs_page_group_lock(req);
1168 /* find the last request */
1169 for (lastreq = req->wb_head;
1170 lastreq->wb_this_page != req->wb_head;
1171 lastreq = lastreq->wb_this_page)
1174 dupreq = nfs_create_request(req->wb_context,
1175 req->wb_page, lastreq, pgbase, bytes);
1177 if (IS_ERR(dupreq)) {
1178 nfs_page_group_unlock(req);
1179 desc->pg_error = PTR_ERR(dupreq);
1183 nfs_lock_request(dupreq);
1184 nfs_page_group_unlock(req);
1185 dupreq->wb_offset = offset;
1186 dupreq->wb_index = req->wb_index;
1190 if (nfs_pgio_has_mirroring(desc))
1191 desc->pg_mirror_idx = midx;
1192 if (!nfs_pageio_add_request_mirror(desc, dupreq))
1193 goto out_cleanup_subreq;
1200 nfs_pageio_cleanup_request(desc, dupreq);
1202 /* remember fatal errors */
1203 if (nfs_error_is_fatal(desc->pg_error))
1204 nfs_context_set_write_error(req->wb_context,
1206 nfs_pageio_error_cleanup(desc);
1211 * nfs_pageio_complete_mirror - Complete I/O on the current mirror of an
1212 * nfs_pageio_descriptor
1213 * @desc: pointer to io descriptor
1214 * @mirror_idx: pointer to mirror index
1216 static void nfs_pageio_complete_mirror(struct nfs_pageio_descriptor *desc,
1219 struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[mirror_idx];
1220 u32 restore_idx = desc->pg_mirror_idx;
1222 if (nfs_pgio_has_mirroring(desc))
1223 desc->pg_mirror_idx = mirror_idx;
1225 nfs_pageio_doio(desc);
1226 if (desc->pg_error < 0 || !mirror->pg_recoalesce)
1228 if (!nfs_do_recoalesce(desc))
1231 desc->pg_mirror_idx = restore_idx;
1235 * nfs_pageio_resend - Transfer requests to new descriptor and resend
1236 * @hdr - the pgio header to move request from
1237 * @desc - the pageio descriptor to add requests to
1239 * Try to move each request (nfs_page) from @hdr to @desc then attempt
1242 * Returns 0 on success and < 0 on error.
1244 int nfs_pageio_resend(struct nfs_pageio_descriptor *desc,
1245 struct nfs_pgio_header *hdr)
1249 desc->pg_io_completion = hdr->io_completion;
1250 desc->pg_dreq = hdr->dreq;
1251 list_splice_init(&hdr->pages, &pages);
1252 while (!list_empty(&pages)) {
1253 struct nfs_page *req = nfs_list_entry(pages.next);
1255 if (!nfs_pageio_add_request(desc, req))
1258 nfs_pageio_complete(desc);
1259 if (!list_empty(&pages)) {
1260 int err = desc->pg_error < 0 ? desc->pg_error : -EIO;
1261 hdr->completion_ops->error_cleanup(&pages, err);
1262 nfs_set_pgio_error(hdr, err, hdr->io_start);
1267 EXPORT_SYMBOL_GPL(nfs_pageio_resend);
1270 * nfs_pageio_complete - Complete I/O then cleanup an nfs_pageio_descriptor
1271 * @desc: pointer to io descriptor
1273 void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
1277 for (midx = 0; midx < desc->pg_mirror_count; midx++)
1278 nfs_pageio_complete_mirror(desc, midx);
1280 if (desc->pg_error < 0)
1281 nfs_pageio_error_cleanup(desc);
1282 if (desc->pg_ops->pg_cleanup)
1283 desc->pg_ops->pg_cleanup(desc);
1284 nfs_pageio_cleanup_mirroring(desc);
1288 * nfs_pageio_cond_complete - Conditional I/O completion
1289 * @desc: pointer to io descriptor
1290 * @index: page index
1292 * It is important to ensure that processes don't try to take locks
1293 * on non-contiguous ranges of pages as that might deadlock. This
1294 * function should be called before attempting to wait on a locked
1295 * nfs_page. It will complete the I/O if the page index 'index'
1296 * is not contiguous with the existing list of pages in 'desc'.
1298 void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
1300 struct nfs_pgio_mirror *mirror;
1301 struct nfs_page *prev;
1304 for (midx = 0; midx < desc->pg_mirror_count; midx++) {
1305 mirror = &desc->pg_mirrors[midx];
1306 if (!list_empty(&mirror->pg_list)) {
1307 prev = nfs_list_entry(mirror->pg_list.prev);
1308 if (index != prev->wb_index + 1) {
1309 nfs_pageio_complete(desc);
1317 * nfs_pageio_stop_mirroring - stop using mirroring (set mirror count to 1)
1319 void nfs_pageio_stop_mirroring(struct nfs_pageio_descriptor *pgio)
1321 nfs_pageio_complete(pgio);
1324 int __init nfs_init_nfspagecache(void)
1326 nfs_page_cachep = kmem_cache_create("nfs_page",
1327 sizeof(struct nfs_page),
1328 0, SLAB_HWCACHE_ALIGN,
1330 if (nfs_page_cachep == NULL)
1336 void nfs_destroy_nfspagecache(void)
1338 kmem_cache_destroy(nfs_page_cachep);
1341 static const struct rpc_call_ops nfs_pgio_common_ops = {
1342 .rpc_call_prepare = nfs_pgio_prepare,
1343 .rpc_call_done = nfs_pgio_result,
1344 .rpc_release = nfs_pgio_release,
1347 const struct nfs_pageio_ops nfs_pgio_rw_ops = {
1348 .pg_test = nfs_generic_pg_test,
1349 .pg_doio = nfs_generic_pg_pgios,