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 static bool nfs_pgarray_set(struct nfs_page_array *p, unsigned int pagecount)
34 p->npages = pagecount;
35 if (pagecount <= ARRAY_SIZE(p->page_array))
36 p->pagevec = p->page_array;
38 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL);
42 return p->pagevec != NULL;
45 void nfs_pgheader_init(struct nfs_pageio_descriptor *desc,
46 struct nfs_pgio_header *hdr,
47 void (*release)(struct nfs_pgio_header *hdr))
49 struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[desc->pg_mirror_idx];
52 hdr->req = nfs_list_entry(mirror->pg_list.next);
53 hdr->inode = desc->pg_inode;
54 hdr->cred = hdr->req->wb_context->cred;
55 hdr->io_start = req_offset(hdr->req);
56 hdr->good_bytes = mirror->pg_count;
57 hdr->dreq = desc->pg_dreq;
58 hdr->layout_private = desc->pg_layout_private;
59 hdr->release = release;
60 hdr->completion_ops = desc->pg_completion_ops;
61 if (hdr->completion_ops->init_hdr)
62 hdr->completion_ops->init_hdr(hdr);
64 hdr->pgio_mirror_idx = desc->pg_mirror_idx;
66 EXPORT_SYMBOL_GPL(nfs_pgheader_init);
68 void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos)
70 spin_lock(&hdr->lock);
71 if (pos < hdr->io_start + hdr->good_bytes) {
72 set_bit(NFS_IOHDR_ERROR, &hdr->flags);
73 clear_bit(NFS_IOHDR_EOF, &hdr->flags);
74 hdr->good_bytes = pos - hdr->io_start;
77 spin_unlock(&hdr->lock);
80 static inline struct nfs_page *
83 struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_NOIO);
85 INIT_LIST_HEAD(&p->wb_list);
90 nfs_page_free(struct nfs_page *p)
92 kmem_cache_free(nfs_page_cachep, p);
96 nfs_iocounter_inc(struct nfs_io_counter *c)
98 atomic_inc(&c->io_count);
102 nfs_iocounter_dec(struct nfs_io_counter *c)
104 if (atomic_dec_and_test(&c->io_count)) {
105 clear_bit(NFS_IO_INPROGRESS, &c->flags);
106 smp_mb__after_atomic();
107 wake_up_bit(&c->flags, NFS_IO_INPROGRESS);
112 __nfs_iocounter_wait(struct nfs_io_counter *c)
114 wait_queue_head_t *wq = bit_waitqueue(&c->flags, NFS_IO_INPROGRESS);
115 DEFINE_WAIT_BIT(q, &c->flags, NFS_IO_INPROGRESS);
119 prepare_to_wait(wq, &q.wait, TASK_KILLABLE);
120 set_bit(NFS_IO_INPROGRESS, &c->flags);
121 if (atomic_read(&c->io_count) == 0)
123 ret = nfs_wait_bit_killable(&q.key);
124 } while (atomic_read(&c->io_count) != 0 && !ret);
125 finish_wait(wq, &q.wait);
130 * nfs_iocounter_wait - wait for i/o to complete
131 * @c: nfs_io_counter to use
133 * returns -ERESTARTSYS if interrupted by a fatal signal.
134 * Otherwise returns 0 once the io_count hits 0.
137 nfs_iocounter_wait(struct nfs_io_counter *c)
139 if (atomic_read(&c->io_count) == 0)
141 return __nfs_iocounter_wait(c);
145 * nfs_page_group_lock - lock the head of the page group
146 * @req - request in group that is to be locked
147 * @nonblock - if true don't block waiting for lock
149 * this lock must be held if modifying the page group list
151 * return 0 on success, < 0 on error: -EDELAY if nonblocking or the
152 * result from wait_on_bit_lock
154 * NOTE: calling with nonblock=false should always have set the
155 * lock bit (see fs/buffer.c and other uses of wait_on_bit_lock
156 * with TASK_UNINTERRUPTIBLE), so there is no need to check the result.
159 nfs_page_group_lock(struct nfs_page *req, bool nonblock)
161 struct nfs_page *head = req->wb_head;
163 WARN_ON_ONCE(head != head->wb_head);
165 if (!test_and_set_bit(PG_HEADLOCK, &head->wb_flags))
169 return wait_on_bit_lock(&head->wb_flags, PG_HEADLOCK,
170 TASK_UNINTERRUPTIBLE);
176 * nfs_page_group_lock_wait - wait for the lock to clear, but don't grab it
177 * @req - a request in the group
179 * This is a blocking call to wait for the group lock to be cleared.
182 nfs_page_group_lock_wait(struct nfs_page *req)
184 struct nfs_page *head = req->wb_head;
186 WARN_ON_ONCE(head != head->wb_head);
188 wait_on_bit(&head->wb_flags, PG_HEADLOCK,
189 TASK_UNINTERRUPTIBLE);
193 * nfs_page_group_unlock - unlock the head of the page group
194 * @req - request in group that is to be unlocked
197 nfs_page_group_unlock(struct nfs_page *req)
199 struct nfs_page *head = req->wb_head;
201 WARN_ON_ONCE(head != head->wb_head);
203 smp_mb__before_atomic();
204 clear_bit(PG_HEADLOCK, &head->wb_flags);
205 smp_mb__after_atomic();
206 wake_up_bit(&head->wb_flags, PG_HEADLOCK);
210 * nfs_page_group_sync_on_bit_locked
212 * must be called with page group lock held
215 nfs_page_group_sync_on_bit_locked(struct nfs_page *req, unsigned int bit)
217 struct nfs_page *head = req->wb_head;
218 struct nfs_page *tmp;
220 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_flags));
221 WARN_ON_ONCE(test_and_set_bit(bit, &req->wb_flags));
223 tmp = req->wb_this_page;
225 if (!test_bit(bit, &tmp->wb_flags))
227 tmp = tmp->wb_this_page;
230 /* true! reset all bits */
233 clear_bit(bit, &tmp->wb_flags);
234 tmp = tmp->wb_this_page;
235 } while (tmp != req);
241 * nfs_page_group_sync_on_bit - set bit on current request, but only
242 * return true if the bit is set for all requests in page group
243 * @req - request in page group
244 * @bit - PG_* bit that is used to sync page group
246 bool nfs_page_group_sync_on_bit(struct nfs_page *req, unsigned int bit)
250 nfs_page_group_lock(req, false);
251 ret = nfs_page_group_sync_on_bit_locked(req, bit);
252 nfs_page_group_unlock(req);
258 * nfs_page_group_init - Initialize the page group linkage for @req
259 * @req - a new nfs request
260 * @prev - the previous request in page group, or NULL if @req is the first
261 * or only request in the group (the head).
264 nfs_page_group_init(struct nfs_page *req, struct nfs_page *prev)
267 WARN_ON_ONCE(prev == req);
272 req->wb_this_page = req;
275 WARN_ON_ONCE(prev->wb_this_page != prev->wb_head);
276 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &prev->wb_head->wb_flags));
277 req->wb_head = prev->wb_head;
278 req->wb_this_page = prev->wb_this_page;
279 prev->wb_this_page = req;
281 /* All subrequests take a ref on the head request until
282 * nfs_page_group_destroy is called */
283 kref_get(&req->wb_head->wb_kref);
285 /* grab extra ref and bump the request count if head request
286 * has extra ref from the write/commit path to handle handoff
287 * between write and commit lists. */
288 if (test_bit(PG_INODE_REF, &prev->wb_head->wb_flags)) {
289 inode = page_file_mapping(req->wb_page)->host;
290 set_bit(PG_INODE_REF, &req->wb_flags);
291 kref_get(&req->wb_kref);
292 spin_lock(&inode->i_lock);
293 NFS_I(inode)->nrequests++;
294 spin_unlock(&inode->i_lock);
300 * nfs_page_group_destroy - sync the destruction of page groups
301 * @req - request that no longer needs the page group
303 * releases the page group reference from each member once all
304 * members have called this function.
307 nfs_page_group_destroy(struct kref *kref)
309 struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
310 struct nfs_page *tmp, *next;
312 /* subrequests must release the ref on the head request */
313 if (req->wb_head != req)
314 nfs_release_request(req->wb_head);
316 if (!nfs_page_group_sync_on_bit(req, PG_TEARDOWN))
321 next = tmp->wb_this_page;
322 /* unlink and free */
323 tmp->wb_this_page = tmp;
325 nfs_free_request(tmp);
327 } while (tmp != req);
331 * nfs_create_request - Create an NFS read/write request.
332 * @ctx: open context to use
333 * @page: page to write
334 * @last: last nfs request created for this page group or NULL if head
335 * @offset: starting offset within the page for the write
336 * @count: number of bytes to read/write
338 * The page must be locked by the caller. This makes sure we never
339 * create two different requests for the same page.
340 * User should ensure it is safe to sleep in this function.
343 nfs_create_request(struct nfs_open_context *ctx, struct page *page,
344 struct nfs_page *last, unsigned int offset,
347 struct nfs_page *req;
348 struct nfs_lock_context *l_ctx;
350 if (test_bit(NFS_CONTEXT_BAD, &ctx->flags))
351 return ERR_PTR(-EBADF);
352 /* try to allocate the request struct */
353 req = nfs_page_alloc();
355 return ERR_PTR(-ENOMEM);
357 /* get lock context early so we can deal with alloc failures */
358 l_ctx = nfs_get_lock_context(ctx);
361 return ERR_CAST(l_ctx);
363 req->wb_lock_context = l_ctx;
364 nfs_iocounter_inc(&l_ctx->io_count);
366 /* Initialize the request struct. Initially, we assume a
367 * long write-back delay. This will be adjusted in
368 * update_nfs_request below if the region is not locked. */
370 req->wb_index = page_file_index(page);
371 page_cache_get(page);
372 req->wb_offset = offset;
373 req->wb_pgbase = offset;
374 req->wb_bytes = count;
375 req->wb_context = get_nfs_open_context(ctx);
376 kref_init(&req->wb_kref);
377 nfs_page_group_init(req, last);
382 * nfs_unlock_request - Unlock request and wake up sleepers.
385 void nfs_unlock_request(struct nfs_page *req)
387 if (!NFS_WBACK_BUSY(req)) {
388 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
391 smp_mb__before_atomic();
392 clear_bit(PG_BUSY, &req->wb_flags);
393 smp_mb__after_atomic();
394 wake_up_bit(&req->wb_flags, PG_BUSY);
398 * nfs_unlock_and_release_request - Unlock request and release the nfs_page
401 void nfs_unlock_and_release_request(struct nfs_page *req)
403 nfs_unlock_request(req);
404 nfs_release_request(req);
408 * nfs_clear_request - Free up all resources allocated to the request
411 * Release page and open context resources associated with a read/write
412 * request after it has completed.
414 static void nfs_clear_request(struct nfs_page *req)
416 struct page *page = req->wb_page;
417 struct nfs_open_context *ctx = req->wb_context;
418 struct nfs_lock_context *l_ctx = req->wb_lock_context;
421 page_cache_release(page);
425 nfs_iocounter_dec(&l_ctx->io_count);
426 nfs_put_lock_context(l_ctx);
427 req->wb_lock_context = NULL;
430 put_nfs_open_context(ctx);
431 req->wb_context = NULL;
436 * nfs_release_request - Release the count on an NFS read/write request
437 * @req: request to release
439 * Note: Should never be called with the spinlock held!
441 void nfs_free_request(struct nfs_page *req)
443 WARN_ON_ONCE(req->wb_this_page != req);
445 /* extra debug: make sure no sync bits are still set */
446 WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
447 WARN_ON_ONCE(test_bit(PG_UNLOCKPAGE, &req->wb_flags));
448 WARN_ON_ONCE(test_bit(PG_UPTODATE, &req->wb_flags));
449 WARN_ON_ONCE(test_bit(PG_WB_END, &req->wb_flags));
450 WARN_ON_ONCE(test_bit(PG_REMOVE, &req->wb_flags));
452 /* Release struct file and open context */
453 nfs_clear_request(req);
457 void nfs_release_request(struct nfs_page *req)
459 kref_put(&req->wb_kref, nfs_page_group_destroy);
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 return wait_on_bit_io(&req->wb_flags, PG_BUSY,
473 TASK_UNINTERRUPTIBLE);
477 * nfs_generic_pg_test - determine if requests can be coalesced
478 * @desc: pointer to descriptor
479 * @prev: previous request in desc, or NULL
482 * Returns zero if @req can be coalesced into @desc, otherwise it returns
483 * the size of the request.
485 size_t nfs_generic_pg_test(struct nfs_pageio_descriptor *desc,
486 struct nfs_page *prev, struct nfs_page *req)
488 struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[desc->pg_mirror_idx];
491 if (mirror->pg_count > mirror->pg_bsize) {
492 /* should never happen */
498 * Limit the request size so that we can still allocate a page array
499 * for it without upsetting the slab allocator.
501 if (((mirror->pg_count + req->wb_bytes) >> PAGE_SHIFT) *
502 sizeof(struct page) > PAGE_SIZE)
505 return min(mirror->pg_bsize - mirror->pg_count, (size_t)req->wb_bytes);
507 EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
509 struct nfs_pgio_header *nfs_pgio_header_alloc(const struct nfs_rw_ops *ops)
511 struct nfs_pgio_header *hdr = ops->rw_alloc_header();
514 INIT_LIST_HEAD(&hdr->pages);
515 spin_lock_init(&hdr->lock);
520 EXPORT_SYMBOL_GPL(nfs_pgio_header_alloc);
523 * nfs_pgio_header_free - Free a read or write header
524 * @hdr: The header to free
526 void nfs_pgio_header_free(struct nfs_pgio_header *hdr)
528 hdr->rw_ops->rw_free_header(hdr);
530 EXPORT_SYMBOL_GPL(nfs_pgio_header_free);
533 * nfs_pgio_data_destroy - make @hdr suitable for reuse
535 * Frees memory and releases refs from nfs_generic_pgio, so that it may
538 * @hdr: A header that has had nfs_generic_pgio called
540 void nfs_pgio_data_destroy(struct nfs_pgio_header *hdr)
542 if (hdr->args.context)
543 put_nfs_open_context(hdr->args.context);
544 if (hdr->page_array.pagevec != hdr->page_array.page_array)
545 kfree(hdr->page_array.pagevec);
547 EXPORT_SYMBOL_GPL(nfs_pgio_data_destroy);
550 * nfs_pgio_rpcsetup - Set up arguments for a pageio call
551 * @hdr: The pageio hdr
552 * @count: Number of bytes to read
553 * @offset: Initial offset
554 * @how: How to commit data (writes only)
555 * @cinfo: Commit information for the call (writes only)
557 static void nfs_pgio_rpcsetup(struct nfs_pgio_header *hdr,
558 unsigned int count, unsigned int offset,
559 int how, struct nfs_commit_info *cinfo)
561 struct nfs_page *req = hdr->req;
563 /* Set up the RPC argument and reply structs
564 * NB: take care not to mess about with hdr->commit et al. */
566 hdr->args.fh = NFS_FH(hdr->inode);
567 hdr->args.offset = req_offset(req) + offset;
568 /* pnfs_set_layoutcommit needs this */
569 hdr->mds_offset = hdr->args.offset;
570 hdr->args.pgbase = req->wb_pgbase + offset;
571 hdr->args.pages = hdr->page_array.pagevec;
572 hdr->args.count = count;
573 hdr->args.context = get_nfs_open_context(req->wb_context);
574 hdr->args.lock_context = req->wb_lock_context;
575 hdr->args.stable = NFS_UNSTABLE;
576 switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
579 case FLUSH_COND_STABLE:
580 if (nfs_reqs_to_commit(cinfo))
583 hdr->args.stable = NFS_FILE_SYNC;
586 hdr->res.fattr = &hdr->fattr;
587 hdr->res.count = count;
589 hdr->res.verf = &hdr->verf;
590 nfs_fattr_init(&hdr->fattr);
594 * nfs_pgio_prepare - Prepare pageio hdr to go over the wire
595 * @task: The current task
596 * @calldata: pageio header to prepare
598 static void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
600 struct nfs_pgio_header *hdr = calldata;
602 err = NFS_PROTO(hdr->inode)->pgio_rpc_prepare(task, hdr);
607 int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr,
608 struct rpc_cred *cred, const struct nfs_rpc_ops *rpc_ops,
609 const struct rpc_call_ops *call_ops, int how, int flags)
611 struct rpc_task *task;
612 struct rpc_message msg = {
613 .rpc_argp = &hdr->args,
614 .rpc_resp = &hdr->res,
617 struct rpc_task_setup task_setup_data = {
621 .callback_ops = call_ops,
622 .callback_data = hdr,
623 .workqueue = nfsiod_workqueue,
624 .flags = RPC_TASK_ASYNC | flags,
628 hdr->rw_ops->rw_initiate(hdr, &msg, rpc_ops, &task_setup_data, how);
630 dprintk("NFS: %5u initiated pgio call "
631 "(req %s/%llu, %u bytes @ offset %llu)\n",
633 hdr->inode->i_sb->s_id,
634 (unsigned long long)NFS_FILEID(hdr->inode),
636 (unsigned long long)hdr->args.offset);
638 task = rpc_run_task(&task_setup_data);
643 if (how & FLUSH_SYNC) {
644 ret = rpc_wait_for_completion_task(task);
646 ret = task->tk_status;
652 EXPORT_SYMBOL_GPL(nfs_initiate_pgio);
655 * nfs_pgio_error - Clean up from a pageio error
656 * @desc: IO descriptor
657 * @hdr: pageio header
659 static int nfs_pgio_error(struct nfs_pageio_descriptor *desc,
660 struct nfs_pgio_header *hdr)
662 struct nfs_pgio_mirror *mirror;
665 set_bit(NFS_IOHDR_REDO, &hdr->flags);
666 nfs_pgio_data_destroy(hdr);
667 hdr->completion_ops->completion(hdr);
668 /* TODO: Make sure it's right to clean up all mirrors here
669 * and not just hdr->pgio_mirror_idx */
670 for (midx = 0; midx < desc->pg_mirror_count; midx++) {
671 mirror = &desc->pg_mirrors[midx];
672 desc->pg_completion_ops->error_cleanup(&mirror->pg_list);
678 * nfs_pgio_release - Release pageio data
679 * @calldata: The pageio header to release
681 static void nfs_pgio_release(void *calldata)
683 struct nfs_pgio_header *hdr = calldata;
684 if (hdr->rw_ops->rw_release)
685 hdr->rw_ops->rw_release(hdr);
686 nfs_pgio_data_destroy(hdr);
687 hdr->completion_ops->completion(hdr);
690 static void nfs_pageio_mirror_init(struct nfs_pgio_mirror *mirror,
693 INIT_LIST_HEAD(&mirror->pg_list);
694 mirror->pg_bytes_written = 0;
695 mirror->pg_count = 0;
696 mirror->pg_bsize = bsize;
698 mirror->pg_recoalesce = 0;
702 * nfs_pageio_init - initialise a page io descriptor
703 * @desc: pointer to descriptor
704 * @inode: pointer to inode
705 * @doio: pointer to io function
706 * @bsize: io block size
707 * @io_flags: extra parameters for the io function
709 void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
711 const struct nfs_pageio_ops *pg_ops,
712 const struct nfs_pgio_completion_ops *compl_ops,
713 const struct nfs_rw_ops *rw_ops,
717 struct nfs_pgio_mirror *new;
721 desc->pg_inode = inode;
722 desc->pg_ops = pg_ops;
723 desc->pg_completion_ops = compl_ops;
724 desc->pg_rw_ops = rw_ops;
725 desc->pg_ioflags = io_flags;
727 desc->pg_lseg = NULL;
728 desc->pg_dreq = NULL;
729 desc->pg_layout_private = NULL;
730 desc->pg_bsize = bsize;
732 desc->pg_mirror_count = 1;
733 desc->pg_mirror_idx = 0;
735 if (pg_ops->pg_get_mirror_count) {
736 /* until we have a request, we don't have an lseg and no
737 * idea how many mirrors there will be */
738 new = kcalloc(NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX,
739 sizeof(struct nfs_pgio_mirror), GFP_KERNEL);
740 desc->pg_mirrors_dynamic = new;
741 desc->pg_mirrors = new;
743 for (i = 0; i < NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX; i++)
744 nfs_pageio_mirror_init(&desc->pg_mirrors[i], bsize);
746 desc->pg_mirrors_dynamic = NULL;
747 desc->pg_mirrors = desc->pg_mirrors_static;
748 nfs_pageio_mirror_init(&desc->pg_mirrors[0], bsize);
751 EXPORT_SYMBOL_GPL(nfs_pageio_init);
754 * nfs_pgio_result - Basic pageio error handling
755 * @task: The task that ran
756 * @calldata: Pageio header to check
758 static void nfs_pgio_result(struct rpc_task *task, void *calldata)
760 struct nfs_pgio_header *hdr = calldata;
761 struct inode *inode = hdr->inode;
763 dprintk("NFS: %s: %5u, (status %d)\n", __func__,
764 task->tk_pid, task->tk_status);
766 if (hdr->rw_ops->rw_done(task, hdr, inode) != 0)
768 if (task->tk_status < 0)
769 nfs_set_pgio_error(hdr, task->tk_status, hdr->args.offset);
771 hdr->rw_ops->rw_result(task, hdr);
775 * Create an RPC task for the given read or write request and kick it.
776 * The page must have been locked by the caller.
778 * It may happen that the page we're passed is not marked dirty.
779 * This is the case if nfs_updatepage detects a conflicting request
780 * that has been written but not committed.
782 int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
783 struct nfs_pgio_header *hdr)
785 struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[desc->pg_mirror_idx];
787 struct nfs_page *req;
790 struct list_head *head = &mirror->pg_list;
791 struct nfs_commit_info cinfo;
792 unsigned int pagecount, pageused;
794 pagecount = nfs_page_array_len(mirror->pg_base, mirror->pg_count);
795 if (!nfs_pgarray_set(&hdr->page_array, pagecount))
796 return nfs_pgio_error(desc, hdr);
798 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
799 pages = hdr->page_array.pagevec;
802 while (!list_empty(head)) {
803 req = nfs_list_entry(head->next);
804 nfs_list_remove_request(req);
805 nfs_list_add_request(req, &hdr->pages);
807 if (!last_page || last_page != req->wb_page) {
809 if (pageused > pagecount)
811 *pages++ = last_page = req->wb_page;
814 if (WARN_ON_ONCE(pageused != pagecount))
815 return nfs_pgio_error(desc, hdr);
817 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
818 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
819 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
821 /* Set up the argument struct */
822 nfs_pgio_rpcsetup(hdr, mirror->pg_count, 0, desc->pg_ioflags, &cinfo);
823 desc->pg_rpc_callops = &nfs_pgio_common_ops;
826 EXPORT_SYMBOL_GPL(nfs_generic_pgio);
828 static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
830 struct nfs_pgio_mirror *mirror;
831 struct nfs_pgio_header *hdr;
834 mirror = &desc->pg_mirrors[desc->pg_mirror_idx];
836 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
838 /* TODO: make sure this is right with mirroring - or
839 * should it back out all mirrors? */
840 desc->pg_completion_ops->error_cleanup(&mirror->pg_list);
843 nfs_pgheader_init(desc, hdr, nfs_pgio_header_free);
844 ret = nfs_generic_pgio(desc, hdr);
846 ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode),
849 NFS_PROTO(hdr->inode),
850 desc->pg_rpc_callops,
851 desc->pg_ioflags, 0);
856 * nfs_pageio_setup_mirroring - determine if mirroring is to be used
857 * by calling the pg_get_mirror_count op
859 static int nfs_pageio_setup_mirroring(struct nfs_pageio_descriptor *pgio,
860 struct nfs_page *req)
862 int mirror_count = 1;
864 if (!pgio->pg_ops->pg_get_mirror_count)
867 mirror_count = pgio->pg_ops->pg_get_mirror_count(pgio, req);
869 if (!mirror_count || mirror_count > NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX)
872 if (WARN_ON_ONCE(!pgio->pg_mirrors_dynamic))
875 pgio->pg_mirror_count = mirror_count;
881 * nfs_pageio_stop_mirroring - stop using mirroring (set mirror count to 1)
883 void nfs_pageio_stop_mirroring(struct nfs_pageio_descriptor *pgio)
885 pgio->pg_mirror_count = 1;
886 pgio->pg_mirror_idx = 0;
889 static void nfs_pageio_cleanup_mirroring(struct nfs_pageio_descriptor *pgio)
891 pgio->pg_mirror_count = 1;
892 pgio->pg_mirror_idx = 0;
893 pgio->pg_mirrors = pgio->pg_mirrors_static;
894 kfree(pgio->pg_mirrors_dynamic);
895 pgio->pg_mirrors_dynamic = NULL;
898 static bool nfs_match_open_context(const struct nfs_open_context *ctx1,
899 const struct nfs_open_context *ctx2)
901 return ctx1->cred == ctx2->cred && ctx1->state == ctx2->state;
904 static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
905 const struct nfs_lock_context *l2)
907 return l1->lockowner.l_owner == l2->lockowner.l_owner
908 && l1->lockowner.l_pid == l2->lockowner.l_pid;
912 * nfs_can_coalesce_requests - test two requests for compatibility
913 * @prev: pointer to nfs_page
914 * @req: pointer to nfs_page
916 * The nfs_page structures 'prev' and 'req' are compared to ensure that the
917 * page data area they describe is contiguous, and that their RPC
918 * credentials, NFSv4 open state, and lockowners are the same.
920 * Return 'true' if this is the case, else return 'false'.
922 static bool nfs_can_coalesce_requests(struct nfs_page *prev,
923 struct nfs_page *req,
924 struct nfs_pageio_descriptor *pgio)
929 if (!nfs_match_open_context(req->wb_context, prev->wb_context))
931 if (req->wb_context->dentry->d_inode->i_flock != NULL &&
932 !nfs_match_lock_context(req->wb_lock_context,
933 prev->wb_lock_context))
935 if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
937 if (req->wb_page == prev->wb_page) {
938 if (req->wb_pgbase != prev->wb_pgbase + prev->wb_bytes)
941 if (req->wb_pgbase != 0 ||
942 prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
946 size = pgio->pg_ops->pg_test(pgio, prev, req);
947 WARN_ON_ONCE(size > req->wb_bytes);
948 if (size && size < req->wb_bytes)
949 req->wb_bytes = size;
954 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
955 * @desc: destination io descriptor
958 * Returns true if the request 'req' was successfully coalesced into the
959 * existing list of pages 'desc'.
961 static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
962 struct nfs_page *req)
964 struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[desc->pg_mirror_idx];
966 struct nfs_page *prev = NULL;
968 if (mirror->pg_count != 0) {
969 prev = nfs_list_entry(mirror->pg_list.prev);
971 if (desc->pg_ops->pg_init)
972 desc->pg_ops->pg_init(desc, req);
973 mirror->pg_base = req->wb_pgbase;
975 if (!nfs_can_coalesce_requests(prev, req, desc))
977 nfs_list_remove_request(req);
978 nfs_list_add_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 = &desc->pg_mirrors[desc->pg_mirror_idx];
991 if (!list_empty(&mirror->pg_list)) {
992 int error = desc->pg_ops->pg_doio(desc);
994 desc->pg_error = error;
996 mirror->pg_bytes_written += mirror->pg_count;
998 if (list_empty(&mirror->pg_list)) {
999 mirror->pg_count = 0;
1000 mirror->pg_base = 0;
1005 * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
1006 * @desc: destination io descriptor
1009 * This may split a request into subrequests which are all part of the
1012 * Returns true if the request 'req' was successfully coalesced into the
1013 * existing list of pages 'desc'.
1015 static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
1016 struct nfs_page *req)
1018 struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[desc->pg_mirror_idx];
1020 struct nfs_page *subreq;
1021 unsigned int bytes_left = 0;
1022 unsigned int offset, pgbase;
1024 WARN_ON_ONCE(desc->pg_mirror_idx >= desc->pg_mirror_count);
1026 nfs_page_group_lock(req, false);
1029 bytes_left = subreq->wb_bytes;
1030 offset = subreq->wb_offset;
1031 pgbase = subreq->wb_pgbase;
1034 if (!nfs_pageio_do_add_request(desc, subreq)) {
1035 /* make sure pg_test call(s) did nothing */
1036 WARN_ON_ONCE(subreq->wb_bytes != bytes_left);
1037 WARN_ON_ONCE(subreq->wb_offset != offset);
1038 WARN_ON_ONCE(subreq->wb_pgbase != pgbase);
1040 nfs_page_group_unlock(req);
1041 desc->pg_moreio = 1;
1042 nfs_pageio_doio(desc);
1043 if (desc->pg_error < 0)
1045 if (mirror->pg_recoalesce)
1047 /* retry add_request for this subreq */
1048 nfs_page_group_lock(req, false);
1052 /* check for buggy pg_test call(s) */
1053 WARN_ON_ONCE(subreq->wb_bytes + subreq->wb_pgbase > PAGE_SIZE);
1054 WARN_ON_ONCE(subreq->wb_bytes > bytes_left);
1055 WARN_ON_ONCE(subreq->wb_bytes == 0);
1057 bytes_left -= subreq->wb_bytes;
1058 offset += subreq->wb_bytes;
1059 pgbase += subreq->wb_bytes;
1062 subreq = nfs_create_request(req->wb_context,
1064 subreq, pgbase, bytes_left);
1067 nfs_lock_request(subreq);
1068 subreq->wb_offset = offset;
1069 subreq->wb_index = req->wb_index;
1071 } while (bytes_left > 0);
1073 nfs_page_group_unlock(req);
1076 desc->pg_error = PTR_ERR(subreq);
1077 nfs_page_group_unlock(req);
1081 static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
1083 struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[desc->pg_mirror_idx];
1087 list_splice_init(&mirror->pg_list, &head);
1088 mirror->pg_bytes_written -= mirror->pg_count;
1089 mirror->pg_count = 0;
1090 mirror->pg_base = 0;
1091 mirror->pg_recoalesce = 0;
1093 desc->pg_moreio = 0;
1095 while (!list_empty(&head)) {
1096 struct nfs_page *req;
1098 req = list_first_entry(&head, struct nfs_page, wb_list);
1099 nfs_list_remove_request(req);
1100 if (__nfs_pageio_add_request(desc, req))
1102 if (desc->pg_error < 0)
1106 } while (mirror->pg_recoalesce);
1110 static int nfs_pageio_add_request_mirror(struct nfs_pageio_descriptor *desc,
1111 struct nfs_page *req)
1116 ret = __nfs_pageio_add_request(desc, req);
1119 if (desc->pg_error < 0)
1121 ret = nfs_do_recoalesce(desc);
1127 int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
1128 struct nfs_page *req)
1131 unsigned int pgbase, offset, bytes;
1132 struct nfs_page *dupreq, *lastreq;
1134 pgbase = req->wb_pgbase;
1135 offset = req->wb_offset;
1136 bytes = req->wb_bytes;
1138 nfs_pageio_setup_mirroring(desc, req);
1140 for (midx = 0; midx < desc->pg_mirror_count; midx++) {
1142 nfs_page_group_lock(req, false);
1144 /* find the last request */
1145 for (lastreq = req->wb_head;
1146 lastreq->wb_this_page != req->wb_head;
1147 lastreq = lastreq->wb_this_page)
1150 dupreq = nfs_create_request(req->wb_context,
1151 req->wb_page, lastreq, pgbase, bytes);
1153 if (IS_ERR(dupreq)) {
1154 nfs_page_group_unlock(req);
1158 nfs_lock_request(dupreq);
1159 nfs_page_group_unlock(req);
1160 dupreq->wb_offset = offset;
1161 dupreq->wb_index = req->wb_index;
1165 desc->pg_mirror_idx = midx;
1166 if (!nfs_pageio_add_request_mirror(desc, dupreq))
1174 * nfs_pageio_complete_mirror - Complete I/O on the current mirror of an
1175 * nfs_pageio_descriptor
1176 * @desc: pointer to io descriptor
1178 static void nfs_pageio_complete_mirror(struct nfs_pageio_descriptor *desc,
1181 struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[mirror_idx];
1182 u32 restore_idx = desc->pg_mirror_idx;
1184 desc->pg_mirror_idx = mirror_idx;
1186 nfs_pageio_doio(desc);
1187 if (!mirror->pg_recoalesce)
1189 if (!nfs_do_recoalesce(desc))
1192 desc->pg_mirror_idx = restore_idx;
1196 * nfs_pageio_resend - Transfer requests to new descriptor and resend
1197 * @hdr - the pgio header to move request from
1198 * @desc - the pageio descriptor to add requests to
1200 * Try to move each request (nfs_page) from @hdr to @desc then attempt
1203 * Returns 0 on success and < 0 on error.
1205 int nfs_pageio_resend(struct nfs_pageio_descriptor *desc,
1206 struct nfs_pgio_header *hdr)
1210 desc->pg_dreq = hdr->dreq;
1211 while (!list_empty(&hdr->pages)) {
1212 struct nfs_page *req = nfs_list_entry(hdr->pages.next);
1214 nfs_list_remove_request(req);
1215 if (!nfs_pageio_add_request(desc, req))
1216 nfs_list_add_request(req, &failed);
1218 nfs_pageio_complete(desc);
1219 if (!list_empty(&failed)) {
1220 list_move(&failed, &hdr->pages);
1225 EXPORT_SYMBOL_GPL(nfs_pageio_resend);
1228 * nfs_pageio_complete - Complete I/O then cleanup an nfs_pageio_descriptor
1229 * @desc: pointer to io descriptor
1231 void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
1235 for (midx = 0; midx < desc->pg_mirror_count; midx++)
1236 nfs_pageio_complete_mirror(desc, midx);
1238 if (desc->pg_ops->pg_cleanup)
1239 desc->pg_ops->pg_cleanup(desc);
1240 nfs_pageio_cleanup_mirroring(desc);
1244 * nfs_pageio_cond_complete - Conditional I/O completion
1245 * @desc: pointer to io descriptor
1246 * @index: page index
1248 * It is important to ensure that processes don't try to take locks
1249 * on non-contiguous ranges of pages as that might deadlock. This
1250 * function should be called before attempting to wait on a locked
1251 * nfs_page. It will complete the I/O if the page index 'index'
1252 * is not contiguous with the existing list of pages in 'desc'.
1254 void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
1256 struct nfs_pgio_mirror *mirror;
1257 struct nfs_page *prev;
1260 for (midx = 0; midx < desc->pg_mirror_count; midx++) {
1261 mirror = &desc->pg_mirrors[midx];
1262 if (!list_empty(&mirror->pg_list)) {
1263 prev = nfs_list_entry(mirror->pg_list.prev);
1264 if (index != prev->wb_index + 1)
1265 nfs_pageio_complete_mirror(desc, midx);
1270 int __init nfs_init_nfspagecache(void)
1272 nfs_page_cachep = kmem_cache_create("nfs_page",
1273 sizeof(struct nfs_page),
1274 0, SLAB_HWCACHE_ALIGN,
1276 if (nfs_page_cachep == NULL)
1282 void nfs_destroy_nfspagecache(void)
1284 kmem_cache_destroy(nfs_page_cachep);
1287 static const struct rpc_call_ops nfs_pgio_common_ops = {
1288 .rpc_call_prepare = nfs_pgio_prepare,
1289 .rpc_call_done = nfs_pgio_result,
1290 .rpc_release = nfs_pgio_release,
1293 const struct nfs_pageio_ops nfs_pgio_rw_ops = {
1294 .pg_test = nfs_generic_pg_test,
1295 .pg_doio = nfs_generic_pg_pgios,