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_var_event_killable(&l_ctx->io_count,
102 !atomic_read(&l_ctx->io_count));
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_var(&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,
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);
573 /* pnfs_set_layoutcommit needs this */
574 hdr->mds_offset = hdr->args.offset;
575 hdr->args.pgbase = req->wb_pgbase;
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))
589 hdr->args.stable = NFS_FILE_SYNC;
592 hdr->res.fattr = &hdr->fattr;
595 hdr->res.verf = &hdr->verf;
596 nfs_fattr_init(&hdr->fattr);
600 * nfs_pgio_prepare - Prepare pageio hdr to go over the wire
601 * @task: The current task
602 * @calldata: pageio header to prepare
604 static void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
606 struct nfs_pgio_header *hdr = calldata;
608 err = NFS_PROTO(hdr->inode)->pgio_rpc_prepare(task, hdr);
613 int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr,
614 struct rpc_cred *cred, const struct nfs_rpc_ops *rpc_ops,
615 const struct rpc_call_ops *call_ops, int how, int flags)
617 struct rpc_task *task;
618 struct rpc_message msg = {
619 .rpc_argp = &hdr->args,
620 .rpc_resp = &hdr->res,
623 struct rpc_task_setup task_setup_data = {
627 .callback_ops = call_ops,
628 .callback_data = hdr,
629 .workqueue = nfsiod_workqueue,
630 .flags = RPC_TASK_ASYNC | flags,
634 hdr->rw_ops->rw_initiate(hdr, &msg, rpc_ops, &task_setup_data, how);
636 dprintk("NFS: initiated pgio call "
637 "(req %s/%llu, %u bytes @ offset %llu)\n",
638 hdr->inode->i_sb->s_id,
639 (unsigned long long)NFS_FILEID(hdr->inode),
641 (unsigned long long)hdr->args.offset);
643 task = rpc_run_task(&task_setup_data);
648 if (how & FLUSH_SYNC) {
649 ret = rpc_wait_for_completion_task(task);
651 ret = task->tk_status;
657 EXPORT_SYMBOL_GPL(nfs_initiate_pgio);
660 * nfs_pgio_error - Clean up from a pageio error
661 * @desc: IO descriptor
662 * @hdr: pageio header
664 static void nfs_pgio_error(struct nfs_pgio_header *hdr)
666 set_bit(NFS_IOHDR_REDO, &hdr->flags);
667 hdr->completion_ops->completion(hdr);
671 * nfs_pgio_release - Release pageio data
672 * @calldata: The pageio header to release
674 static void nfs_pgio_release(void *calldata)
676 struct nfs_pgio_header *hdr = calldata;
677 hdr->completion_ops->completion(hdr);
680 static void nfs_pageio_mirror_init(struct nfs_pgio_mirror *mirror,
683 INIT_LIST_HEAD(&mirror->pg_list);
684 mirror->pg_bytes_written = 0;
685 mirror->pg_count = 0;
686 mirror->pg_bsize = bsize;
688 mirror->pg_recoalesce = 0;
692 * nfs_pageio_init - initialise a page io descriptor
693 * @desc: pointer to descriptor
694 * @inode: pointer to inode
695 * @pg_ops: pointer to pageio operations
696 * @compl_ops: pointer to pageio completion operations
697 * @rw_ops: pointer to nfs read/write operations
698 * @bsize: io block size
699 * @io_flags: extra parameters for the io function
701 void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
703 const struct nfs_pageio_ops *pg_ops,
704 const struct nfs_pgio_completion_ops *compl_ops,
705 const struct nfs_rw_ops *rw_ops,
710 desc->pg_inode = inode;
711 desc->pg_ops = pg_ops;
712 desc->pg_completion_ops = compl_ops;
713 desc->pg_rw_ops = rw_ops;
714 desc->pg_ioflags = io_flags;
716 desc->pg_lseg = NULL;
717 desc->pg_io_completion = NULL;
718 desc->pg_dreq = NULL;
719 desc->pg_bsize = bsize;
721 desc->pg_mirror_count = 1;
722 desc->pg_mirror_idx = 0;
724 desc->pg_mirrors_dynamic = NULL;
725 desc->pg_mirrors = desc->pg_mirrors_static;
726 nfs_pageio_mirror_init(&desc->pg_mirrors[0], bsize);
730 * nfs_pgio_result - Basic pageio error handling
731 * @task: The task that ran
732 * @calldata: Pageio header to check
734 static void nfs_pgio_result(struct rpc_task *task, void *calldata)
736 struct nfs_pgio_header *hdr = calldata;
737 struct inode *inode = hdr->inode;
739 dprintk("NFS: %s: %5u, (status %d)\n", __func__,
740 task->tk_pid, task->tk_status);
742 if (hdr->rw_ops->rw_done(task, hdr, inode) != 0)
744 if (task->tk_status < 0)
745 nfs_set_pgio_error(hdr, task->tk_status, hdr->args.offset);
747 hdr->rw_ops->rw_result(task, hdr);
751 * Create an RPC task for the given read or write request and kick it.
752 * The page must have been locked by the caller.
754 * It may happen that the page we're passed is not marked dirty.
755 * This is the case if nfs_updatepage detects a conflicting request
756 * that has been written but not committed.
758 int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
759 struct nfs_pgio_header *hdr)
761 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
763 struct nfs_page *req;
766 struct list_head *head = &mirror->pg_list;
767 struct nfs_commit_info cinfo;
768 struct nfs_page_array *pg_array = &hdr->page_array;
769 unsigned int pagecount, pageused;
770 gfp_t gfp_flags = GFP_KERNEL;
772 pagecount = nfs_page_array_len(mirror->pg_base, mirror->pg_count);
773 pg_array->npages = pagecount;
775 if (pagecount <= ARRAY_SIZE(pg_array->page_array))
776 pg_array->pagevec = pg_array->page_array;
778 if (hdr->rw_mode == FMODE_WRITE)
779 gfp_flags = GFP_NOIO;
780 pg_array->pagevec = kcalloc(pagecount, sizeof(struct page *), gfp_flags);
781 if (!pg_array->pagevec) {
782 pg_array->npages = 0;
784 desc->pg_error = -ENOMEM;
785 return desc->pg_error;
789 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
790 pages = hdr->page_array.pagevec;
793 while (!list_empty(head)) {
794 req = nfs_list_entry(head->next);
795 nfs_list_move_request(req, &hdr->pages);
797 if (!last_page || last_page != req->wb_page) {
799 if (pageused > pagecount)
801 *pages++ = last_page = req->wb_page;
804 if (WARN_ON_ONCE(pageused != pagecount)) {
806 desc->pg_error = -EINVAL;
807 return desc->pg_error;
810 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
811 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
812 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
814 /* Set up the argument struct */
815 nfs_pgio_rpcsetup(hdr, mirror->pg_count, desc->pg_ioflags, &cinfo);
816 desc->pg_rpc_callops = &nfs_pgio_common_ops;
819 EXPORT_SYMBOL_GPL(nfs_generic_pgio);
821 static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
823 struct nfs_pgio_header *hdr;
826 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
828 desc->pg_error = -ENOMEM;
829 return desc->pg_error;
831 nfs_pgheader_init(desc, hdr, nfs_pgio_header_free);
832 ret = nfs_generic_pgio(desc, hdr);
834 ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode),
837 NFS_PROTO(hdr->inode),
838 desc->pg_rpc_callops,
839 desc->pg_ioflags, 0);
843 static struct nfs_pgio_mirror *
844 nfs_pageio_alloc_mirrors(struct nfs_pageio_descriptor *desc,
845 unsigned int mirror_count)
847 struct nfs_pgio_mirror *ret;
850 kfree(desc->pg_mirrors_dynamic);
851 desc->pg_mirrors_dynamic = NULL;
852 if (mirror_count == 1)
853 return desc->pg_mirrors_static;
854 ret = kmalloc_array(mirror_count, sizeof(*ret), GFP_NOFS);
856 for (i = 0; i < mirror_count; i++)
857 nfs_pageio_mirror_init(&ret[i], desc->pg_bsize);
858 desc->pg_mirrors_dynamic = ret;
864 * nfs_pageio_setup_mirroring - determine if mirroring is to be used
865 * by calling the pg_get_mirror_count op
867 static void nfs_pageio_setup_mirroring(struct nfs_pageio_descriptor *pgio,
868 struct nfs_page *req)
870 unsigned int mirror_count = 1;
872 if (pgio->pg_ops->pg_get_mirror_count)
873 mirror_count = pgio->pg_ops->pg_get_mirror_count(pgio, req);
874 if (mirror_count == pgio->pg_mirror_count || pgio->pg_error < 0)
877 if (!mirror_count || mirror_count > NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX) {
878 pgio->pg_error = -EINVAL;
882 pgio->pg_mirrors = nfs_pageio_alloc_mirrors(pgio, mirror_count);
883 if (pgio->pg_mirrors == NULL) {
884 pgio->pg_error = -ENOMEM;
885 pgio->pg_mirrors = pgio->pg_mirrors_static;
888 pgio->pg_mirror_count = mirror_count;
891 static void nfs_pageio_cleanup_mirroring(struct nfs_pageio_descriptor *pgio)
893 pgio->pg_mirror_count = 1;
894 pgio->pg_mirror_idx = 0;
895 pgio->pg_mirrors = pgio->pg_mirrors_static;
896 kfree(pgio->pg_mirrors_dynamic);
897 pgio->pg_mirrors_dynamic = NULL;
900 static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
901 const struct nfs_lock_context *l2)
903 return l1->lockowner == l2->lockowner;
907 * nfs_can_coalesce_requests - test two requests for compatibility
908 * @prev: pointer to nfs_page
909 * @req: pointer to nfs_page
911 * The nfs_page structures 'prev' and 'req' are compared to ensure that the
912 * page data area they describe is contiguous, and that their RPC
913 * credentials, NFSv4 open state, and lockowners are the same.
915 * Return 'true' if this is the case, else return 'false'.
917 static bool nfs_can_coalesce_requests(struct nfs_page *prev,
918 struct nfs_page *req,
919 struct nfs_pageio_descriptor *pgio)
922 struct file_lock_context *flctx;
925 if (!nfs_match_open_context(req->wb_context, prev->wb_context))
927 flctx = d_inode(req->wb_context->dentry)->i_flctx;
929 !(list_empty_careful(&flctx->flc_posix) &&
930 list_empty_careful(&flctx->flc_flock)) &&
931 !nfs_match_lock_context(req->wb_lock_context,
932 prev->wb_lock_context))
934 if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
936 if (req->wb_page == prev->wb_page) {
937 if (req->wb_pgbase != prev->wb_pgbase + prev->wb_bytes)
940 if (req->wb_pgbase != 0 ||
941 prev->wb_pgbase + prev->wb_bytes != PAGE_SIZE)
945 size = pgio->pg_ops->pg_test(pgio, prev, req);
946 WARN_ON_ONCE(size > req->wb_bytes);
947 if (size && size < req->wb_bytes)
948 req->wb_bytes = size;
953 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
954 * @desc: destination io descriptor
957 * Returns true if the request 'req' was successfully coalesced into the
958 * existing list of pages 'desc'.
960 static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
961 struct nfs_page *req)
963 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
965 struct nfs_page *prev = NULL;
967 if (mirror->pg_count != 0) {
968 prev = nfs_list_entry(mirror->pg_list.prev);
970 if (desc->pg_ops->pg_init)
971 desc->pg_ops->pg_init(desc, req);
972 if (desc->pg_error < 0)
974 mirror->pg_base = req->wb_pgbase;
976 if (!nfs_can_coalesce_requests(prev, req, desc))
978 nfs_list_move_request(req, &mirror->pg_list);
979 mirror->pg_count += req->wb_bytes;
984 * Helper for nfs_pageio_add_request and nfs_pageio_complete
986 static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
988 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
990 if (!list_empty(&mirror->pg_list)) {
991 int error = desc->pg_ops->pg_doio(desc);
993 desc->pg_error = error;
994 if (list_empty(&mirror->pg_list)) {
995 mirror->pg_bytes_written += mirror->pg_count;
996 mirror->pg_count = 0;
998 mirror->pg_recoalesce = 0;
1004 nfs_pageio_cleanup_request(struct nfs_pageio_descriptor *desc,
1005 struct nfs_page *req)
1009 nfs_list_move_request(req, &head);
1010 desc->pg_completion_ops->error_cleanup(&head, desc->pg_error);
1014 * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
1015 * @desc: destination io descriptor
1018 * This may split a request into subrequests which are all part of the
1021 * Returns true if the request 'req' was successfully coalesced into the
1022 * existing list of pages 'desc'.
1024 static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
1025 struct nfs_page *req)
1027 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
1029 struct nfs_page *subreq;
1030 unsigned int bytes_left = 0;
1031 unsigned int offset, pgbase;
1033 nfs_page_group_lock(req);
1036 bytes_left = subreq->wb_bytes;
1037 offset = subreq->wb_offset;
1038 pgbase = subreq->wb_pgbase;
1041 if (!nfs_pageio_do_add_request(desc, subreq)) {
1042 /* make sure pg_test call(s) did nothing */
1043 WARN_ON_ONCE(subreq->wb_bytes != bytes_left);
1044 WARN_ON_ONCE(subreq->wb_offset != offset);
1045 WARN_ON_ONCE(subreq->wb_pgbase != pgbase);
1047 nfs_page_group_unlock(req);
1048 desc->pg_moreio = 1;
1049 nfs_pageio_doio(desc);
1050 if (desc->pg_error < 0 || mirror->pg_recoalesce)
1051 goto out_cleanup_subreq;
1052 /* retry add_request for this subreq */
1053 nfs_page_group_lock(req);
1057 /* check for buggy pg_test call(s) */
1058 WARN_ON_ONCE(subreq->wb_bytes + subreq->wb_pgbase > PAGE_SIZE);
1059 WARN_ON_ONCE(subreq->wb_bytes > bytes_left);
1060 WARN_ON_ONCE(subreq->wb_bytes == 0);
1062 bytes_left -= subreq->wb_bytes;
1063 offset += subreq->wb_bytes;
1064 pgbase += subreq->wb_bytes;
1067 subreq = nfs_create_request(req->wb_context,
1069 subreq, pgbase, bytes_left);
1072 nfs_lock_request(subreq);
1073 subreq->wb_offset = offset;
1074 subreq->wb_index = req->wb_index;
1076 } while (bytes_left > 0);
1078 nfs_page_group_unlock(req);
1081 desc->pg_error = PTR_ERR(subreq);
1082 nfs_page_group_unlock(req);
1086 nfs_pageio_cleanup_request(desc, subreq);
1090 static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
1092 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
1096 list_splice_init(&mirror->pg_list, &head);
1097 mirror->pg_count = 0;
1098 mirror->pg_base = 0;
1099 mirror->pg_recoalesce = 0;
1101 while (!list_empty(&head)) {
1102 struct nfs_page *req;
1104 req = list_first_entry(&head, struct nfs_page, wb_list);
1105 if (__nfs_pageio_add_request(desc, req))
1107 if (desc->pg_error < 0) {
1108 list_splice_tail(&head, &mirror->pg_list);
1109 mirror->pg_recoalesce = 1;
1114 } while (mirror->pg_recoalesce);
1118 static int nfs_pageio_add_request_mirror(struct nfs_pageio_descriptor *desc,
1119 struct nfs_page *req)
1124 ret = __nfs_pageio_add_request(desc, req);
1127 if (desc->pg_error < 0)
1129 ret = nfs_do_recoalesce(desc);
1135 static void nfs_pageio_error_cleanup(struct nfs_pageio_descriptor *desc)
1138 struct nfs_pgio_mirror *mirror;
1140 if (!desc->pg_error)
1143 for (midx = 0; midx < desc->pg_mirror_count; midx++) {
1144 mirror = &desc->pg_mirrors[midx];
1145 desc->pg_completion_ops->error_cleanup(&mirror->pg_list,
1150 int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
1151 struct nfs_page *req)
1154 unsigned int pgbase, offset, bytes;
1155 struct nfs_page *dupreq, *lastreq;
1157 pgbase = req->wb_pgbase;
1158 offset = req->wb_offset;
1159 bytes = req->wb_bytes;
1161 nfs_pageio_setup_mirroring(desc, req);
1162 if (desc->pg_error < 0)
1165 for (midx = 0; midx < desc->pg_mirror_count; midx++) {
1167 nfs_page_group_lock(req);
1169 /* find the last request */
1170 for (lastreq = req->wb_head;
1171 lastreq->wb_this_page != req->wb_head;
1172 lastreq = lastreq->wb_this_page)
1175 dupreq = nfs_create_request(req->wb_context,
1176 req->wb_page, lastreq, pgbase, bytes);
1178 if (IS_ERR(dupreq)) {
1179 nfs_page_group_unlock(req);
1180 desc->pg_error = PTR_ERR(dupreq);
1184 nfs_lock_request(dupreq);
1185 nfs_page_group_unlock(req);
1186 dupreq->wb_offset = offset;
1187 dupreq->wb_index = req->wb_index;
1191 if (nfs_pgio_has_mirroring(desc))
1192 desc->pg_mirror_idx = midx;
1193 if (!nfs_pageio_add_request_mirror(desc, dupreq))
1194 goto out_cleanup_subreq;
1201 nfs_pageio_cleanup_request(desc, dupreq);
1203 /* remember fatal errors */
1204 if (nfs_error_is_fatal(desc->pg_error))
1205 nfs_context_set_write_error(req->wb_context,
1207 nfs_pageio_error_cleanup(desc);
1212 * nfs_pageio_complete_mirror - Complete I/O on the current mirror of an
1213 * nfs_pageio_descriptor
1214 * @desc: pointer to io descriptor
1215 * @mirror_idx: pointer to mirror index
1217 static void nfs_pageio_complete_mirror(struct nfs_pageio_descriptor *desc,
1220 struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[mirror_idx];
1221 u32 restore_idx = desc->pg_mirror_idx;
1223 if (nfs_pgio_has_mirroring(desc))
1224 desc->pg_mirror_idx = mirror_idx;
1226 nfs_pageio_doio(desc);
1227 if (desc->pg_error < 0 || !mirror->pg_recoalesce)
1229 if (!nfs_do_recoalesce(desc))
1232 desc->pg_mirror_idx = restore_idx;
1236 * nfs_pageio_resend - Transfer requests to new descriptor and resend
1237 * @hdr - the pgio header to move request from
1238 * @desc - the pageio descriptor to add requests to
1240 * Try to move each request (nfs_page) from @hdr to @desc then attempt
1243 * Returns 0 on success and < 0 on error.
1245 int nfs_pageio_resend(struct nfs_pageio_descriptor *desc,
1246 struct nfs_pgio_header *hdr)
1250 desc->pg_io_completion = hdr->io_completion;
1251 desc->pg_dreq = hdr->dreq;
1252 list_splice_init(&hdr->pages, &pages);
1253 while (!list_empty(&pages)) {
1254 struct nfs_page *req = nfs_list_entry(pages.next);
1256 if (!nfs_pageio_add_request(desc, req))
1259 nfs_pageio_complete(desc);
1260 if (!list_empty(&pages)) {
1261 int err = desc->pg_error < 0 ? desc->pg_error : -EIO;
1262 hdr->completion_ops->error_cleanup(&pages, err);
1263 nfs_set_pgio_error(hdr, err, hdr->io_start);
1268 EXPORT_SYMBOL_GPL(nfs_pageio_resend);
1271 * nfs_pageio_complete - Complete I/O then cleanup an nfs_pageio_descriptor
1272 * @desc: pointer to io descriptor
1274 void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
1278 for (midx = 0; midx < desc->pg_mirror_count; midx++)
1279 nfs_pageio_complete_mirror(desc, midx);
1281 if (desc->pg_error < 0)
1282 nfs_pageio_error_cleanup(desc);
1283 if (desc->pg_ops->pg_cleanup)
1284 desc->pg_ops->pg_cleanup(desc);
1285 nfs_pageio_cleanup_mirroring(desc);
1289 * nfs_pageio_cond_complete - Conditional I/O completion
1290 * @desc: pointer to io descriptor
1291 * @index: page index
1293 * It is important to ensure that processes don't try to take locks
1294 * on non-contiguous ranges of pages as that might deadlock. This
1295 * function should be called before attempting to wait on a locked
1296 * nfs_page. It will complete the I/O if the page index 'index'
1297 * is not contiguous with the existing list of pages in 'desc'.
1299 void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
1301 struct nfs_pgio_mirror *mirror;
1302 struct nfs_page *prev;
1305 for (midx = 0; midx < desc->pg_mirror_count; midx++) {
1306 mirror = &desc->pg_mirrors[midx];
1307 if (!list_empty(&mirror->pg_list)) {
1308 prev = nfs_list_entry(mirror->pg_list.prev);
1309 if (index != prev->wb_index + 1) {
1310 nfs_pageio_complete(desc);
1318 * nfs_pageio_stop_mirroring - stop using mirroring (set mirror count to 1)
1320 void nfs_pageio_stop_mirroring(struct nfs_pageio_descriptor *pgio)
1322 nfs_pageio_complete(pgio);
1325 int __init nfs_init_nfspagecache(void)
1327 nfs_page_cachep = kmem_cache_create("nfs_page",
1328 sizeof(struct nfs_page),
1329 0, SLAB_HWCACHE_ALIGN,
1331 if (nfs_page_cachep == NULL)
1337 void nfs_destroy_nfspagecache(void)
1339 kmem_cache_destroy(nfs_page_cachep);
1342 static const struct rpc_call_ops nfs_pgio_common_ops = {
1343 .rpc_call_prepare = nfs_pgio_prepare,
1344 .rpc_call_done = nfs_pgio_result,
1345 .rpc_release = nfs_pgio_release,
1348 const struct nfs_pageio_ops nfs_pgio_rw_ops = {
1349 .pg_test = nfs_generic_pg_test,
1350 .pg_doio = nfs_generic_pg_pgios,