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 hdr->req = nfs_list_entry(desc->pg_list.next);
50 hdr->inode = desc->pg_inode;
51 hdr->cred = hdr->req->wb_context->cred;
52 hdr->io_start = req_offset(hdr->req);
53 hdr->good_bytes = desc->pg_count;
54 hdr->dreq = desc->pg_dreq;
55 hdr->layout_private = desc->pg_layout_private;
56 hdr->release = release;
57 hdr->completion_ops = desc->pg_completion_ops;
58 if (hdr->completion_ops->init_hdr)
59 hdr->completion_ops->init_hdr(hdr);
61 EXPORT_SYMBOL_GPL(nfs_pgheader_init);
63 void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos)
65 spin_lock(&hdr->lock);
66 if (pos < hdr->io_start + hdr->good_bytes) {
67 set_bit(NFS_IOHDR_ERROR, &hdr->flags);
68 clear_bit(NFS_IOHDR_EOF, &hdr->flags);
69 hdr->good_bytes = pos - hdr->io_start;
72 spin_unlock(&hdr->lock);
75 static inline struct nfs_page *
78 struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_NOIO);
80 INIT_LIST_HEAD(&p->wb_list);
85 nfs_page_free(struct nfs_page *p)
87 kmem_cache_free(nfs_page_cachep, p);
91 nfs_iocounter_inc(struct nfs_io_counter *c)
93 atomic_inc(&c->io_count);
97 nfs_iocounter_dec(struct nfs_io_counter *c)
99 if (atomic_dec_and_test(&c->io_count)) {
100 clear_bit(NFS_IO_INPROGRESS, &c->flags);
101 smp_mb__after_atomic();
102 wake_up_bit(&c->flags, NFS_IO_INPROGRESS);
107 __nfs_iocounter_wait(struct nfs_io_counter *c)
109 wait_queue_head_t *wq = bit_waitqueue(&c->flags, NFS_IO_INPROGRESS);
110 DEFINE_WAIT_BIT(q, &c->flags, NFS_IO_INPROGRESS);
114 prepare_to_wait(wq, &q.wait, TASK_KILLABLE);
115 set_bit(NFS_IO_INPROGRESS, &c->flags);
116 if (atomic_read(&c->io_count) == 0)
118 ret = nfs_wait_bit_killable(&q.key);
119 } while (atomic_read(&c->io_count) != 0);
120 finish_wait(wq, &q.wait);
125 * nfs_iocounter_wait - wait for i/o to complete
126 * @c: nfs_io_counter to use
128 * returns -ERESTARTSYS if interrupted by a fatal signal.
129 * Otherwise returns 0 once the io_count hits 0.
132 nfs_iocounter_wait(struct nfs_io_counter *c)
134 if (atomic_read(&c->io_count) == 0)
136 return __nfs_iocounter_wait(c);
140 * nfs_page_group_lock - lock the head of the page group
141 * @req - request in group that is to be locked
143 * this lock must be held if modifying the page group list
146 nfs_page_group_lock(struct nfs_page *req)
148 struct nfs_page *head = req->wb_head;
150 WARN_ON_ONCE(head != head->wb_head);
152 wait_on_bit_lock(&head->wb_flags, PG_HEADLOCK,
153 TASK_UNINTERRUPTIBLE);
157 * nfs_page_group_unlock - unlock the head of the page group
158 * @req - request in group that is to be unlocked
161 nfs_page_group_unlock(struct nfs_page *req)
163 struct nfs_page *head = req->wb_head;
165 WARN_ON_ONCE(head != head->wb_head);
167 smp_mb__before_atomic();
168 clear_bit(PG_HEADLOCK, &head->wb_flags);
169 smp_mb__after_atomic();
170 wake_up_bit(&head->wb_flags, PG_HEADLOCK);
174 * nfs_page_group_sync_on_bit_locked
176 * must be called with page group lock held
179 nfs_page_group_sync_on_bit_locked(struct nfs_page *req, unsigned int bit)
181 struct nfs_page *head = req->wb_head;
182 struct nfs_page *tmp;
184 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_flags));
185 WARN_ON_ONCE(test_and_set_bit(bit, &req->wb_flags));
187 tmp = req->wb_this_page;
189 if (!test_bit(bit, &tmp->wb_flags))
191 tmp = tmp->wb_this_page;
194 /* true! reset all bits */
197 clear_bit(bit, &tmp->wb_flags);
198 tmp = tmp->wb_this_page;
199 } while (tmp != req);
205 * nfs_page_group_sync_on_bit - set bit on current request, but only
206 * return true if the bit is set for all requests in page group
207 * @req - request in page group
208 * @bit - PG_* bit that is used to sync page group
210 bool nfs_page_group_sync_on_bit(struct nfs_page *req, unsigned int bit)
214 nfs_page_group_lock(req);
215 ret = nfs_page_group_sync_on_bit_locked(req, bit);
216 nfs_page_group_unlock(req);
222 * nfs_page_group_init - Initialize the page group linkage for @req
223 * @req - a new nfs request
224 * @prev - the previous request in page group, or NULL if @req is the first
225 * or only request in the group (the head).
228 nfs_page_group_init(struct nfs_page *req, struct nfs_page *prev)
230 WARN_ON_ONCE(prev == req);
235 req->wb_this_page = req;
238 WARN_ON_ONCE(prev->wb_this_page != prev->wb_head);
239 WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &prev->wb_head->wb_flags));
240 req->wb_head = prev->wb_head;
241 req->wb_this_page = prev->wb_this_page;
242 prev->wb_this_page = req;
244 /* All subrequests take a ref on the head request until
245 * nfs_page_group_destroy is called */
246 kref_get(&req->wb_head->wb_kref);
248 /* grab extra ref if head request has extra ref from
249 * the write/commit path to handle handoff between write
250 * and commit lists */
251 if (test_bit(PG_INODE_REF, &prev->wb_head->wb_flags)) {
252 set_bit(PG_INODE_REF, &req->wb_flags);
253 kref_get(&req->wb_kref);
259 * nfs_page_group_destroy - sync the destruction of page groups
260 * @req - request that no longer needs the page group
262 * releases the page group reference from each member once all
263 * members have called this function.
266 nfs_page_group_destroy(struct kref *kref)
268 struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
269 struct nfs_page *tmp, *next;
271 /* subrequests must release the ref on the head request */
272 if (req->wb_head != req)
273 nfs_release_request(req->wb_head);
275 if (!nfs_page_group_sync_on_bit(req, PG_TEARDOWN))
280 next = tmp->wb_this_page;
281 /* unlink and free */
282 tmp->wb_this_page = tmp;
284 nfs_free_request(tmp);
286 } while (tmp != req);
290 * nfs_create_request - Create an NFS read/write request.
291 * @ctx: open context to use
292 * @page: page to write
293 * @last: last nfs request created for this page group or NULL if head
294 * @offset: starting offset within the page for the write
295 * @count: number of bytes to read/write
297 * The page must be locked by the caller. This makes sure we never
298 * create two different requests for the same page.
299 * User should ensure it is safe to sleep in this function.
302 nfs_create_request(struct nfs_open_context *ctx, struct page *page,
303 struct nfs_page *last, unsigned int offset,
306 struct nfs_page *req;
307 struct nfs_lock_context *l_ctx;
309 if (test_bit(NFS_CONTEXT_BAD, &ctx->flags))
310 return ERR_PTR(-EBADF);
311 /* try to allocate the request struct */
312 req = nfs_page_alloc();
314 return ERR_PTR(-ENOMEM);
316 /* get lock context early so we can deal with alloc failures */
317 l_ctx = nfs_get_lock_context(ctx);
320 return ERR_CAST(l_ctx);
322 req->wb_lock_context = l_ctx;
323 nfs_iocounter_inc(&l_ctx->io_count);
325 /* Initialize the request struct. Initially, we assume a
326 * long write-back delay. This will be adjusted in
327 * update_nfs_request below if the region is not locked. */
329 req->wb_index = page_file_index(page);
330 page_cache_get(page);
331 req->wb_offset = offset;
332 req->wb_pgbase = offset;
333 req->wb_bytes = count;
334 req->wb_context = get_nfs_open_context(ctx);
335 kref_init(&req->wb_kref);
336 nfs_page_group_init(req, last);
341 * nfs_unlock_request - Unlock request and wake up sleepers.
344 void nfs_unlock_request(struct nfs_page *req)
346 if (!NFS_WBACK_BUSY(req)) {
347 printk(KERN_ERR "NFS: Invalid unlock attempted\n");
350 smp_mb__before_atomic();
351 clear_bit(PG_BUSY, &req->wb_flags);
352 smp_mb__after_atomic();
353 wake_up_bit(&req->wb_flags, PG_BUSY);
357 * nfs_unlock_and_release_request - Unlock request and release the nfs_page
360 void nfs_unlock_and_release_request(struct nfs_page *req)
362 nfs_unlock_request(req);
363 nfs_release_request(req);
367 * nfs_clear_request - Free up all resources allocated to the request
370 * Release page and open context resources associated with a read/write
371 * request after it has completed.
373 static void nfs_clear_request(struct nfs_page *req)
375 struct page *page = req->wb_page;
376 struct nfs_open_context *ctx = req->wb_context;
377 struct nfs_lock_context *l_ctx = req->wb_lock_context;
380 page_cache_release(page);
384 nfs_iocounter_dec(&l_ctx->io_count);
385 nfs_put_lock_context(l_ctx);
386 req->wb_lock_context = NULL;
389 put_nfs_open_context(ctx);
390 req->wb_context = NULL;
395 * nfs_release_request - Release the count on an NFS read/write request
396 * @req: request to release
398 * Note: Should never be called with the spinlock held!
400 void nfs_free_request(struct nfs_page *req)
402 WARN_ON_ONCE(req->wb_this_page != req);
404 /* extra debug: make sure no sync bits are still set */
405 WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
406 WARN_ON_ONCE(test_bit(PG_UNLOCKPAGE, &req->wb_flags));
407 WARN_ON_ONCE(test_bit(PG_UPTODATE, &req->wb_flags));
408 WARN_ON_ONCE(test_bit(PG_WB_END, &req->wb_flags));
409 WARN_ON_ONCE(test_bit(PG_REMOVE, &req->wb_flags));
411 /* Release struct file and open context */
412 nfs_clear_request(req);
416 void nfs_release_request(struct nfs_page *req)
418 kref_put(&req->wb_kref, nfs_page_group_destroy);
422 * nfs_wait_on_request - Wait for a request to complete.
423 * @req: request to wait upon.
425 * Interruptible by fatal signals only.
426 * The user is responsible for holding a count on the request.
429 nfs_wait_on_request(struct nfs_page *req)
431 return wait_on_bit_io(&req->wb_flags, PG_BUSY,
432 TASK_UNINTERRUPTIBLE);
436 * nfs_generic_pg_test - determine if requests can be coalesced
437 * @desc: pointer to descriptor
438 * @prev: previous request in desc, or NULL
441 * Returns zero if @req can be coalesced into @desc, otherwise it returns
442 * the size of the request.
444 size_t nfs_generic_pg_test(struct nfs_pageio_descriptor *desc,
445 struct nfs_page *prev, struct nfs_page *req)
447 if (desc->pg_count > desc->pg_bsize) {
448 /* should never happen */
453 return min(desc->pg_bsize - desc->pg_count, (size_t)req->wb_bytes);
455 EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
457 static inline struct nfs_rw_header *NFS_RW_HEADER(struct nfs_pgio_header *hdr)
459 return container_of(hdr, struct nfs_rw_header, header);
463 * nfs_rw_header_alloc - Allocate a header for a read or write
464 * @ops: Read or write function vector
466 struct nfs_rw_header *nfs_rw_header_alloc(const struct nfs_rw_ops *ops)
468 struct nfs_rw_header *header = ops->rw_alloc_header();
471 struct nfs_pgio_header *hdr = &header->header;
473 INIT_LIST_HEAD(&hdr->pages);
474 spin_lock_init(&hdr->lock);
475 atomic_set(&hdr->refcnt, 0);
480 EXPORT_SYMBOL_GPL(nfs_rw_header_alloc);
483 * nfs_rw_header_free - Free a read or write header
484 * @hdr: The header to free
486 void nfs_rw_header_free(struct nfs_pgio_header *hdr)
488 hdr->rw_ops->rw_free_header(NFS_RW_HEADER(hdr));
490 EXPORT_SYMBOL_GPL(nfs_rw_header_free);
493 * nfs_pgio_data_alloc - Allocate pageio data
494 * @hdr: The header making a request
495 * @pagecount: Number of pages to create
497 static struct nfs_pgio_data *nfs_pgio_data_alloc(struct nfs_pgio_header *hdr,
498 unsigned int pagecount)
500 struct nfs_pgio_data *data, *prealloc;
502 prealloc = &NFS_RW_HEADER(hdr)->rpc_data;
503 if (prealloc->header == NULL)
506 data = kzalloc(sizeof(*data), GFP_KERNEL);
510 if (nfs_pgarray_set(&data->pages, pagecount)) {
512 atomic_inc(&hdr->refcnt);
514 if (data != prealloc)
523 * nfs_pgio_data_release - Properly free pageio data
524 * @data: The data to release
526 void nfs_pgio_data_release(struct nfs_pgio_data *data)
528 struct nfs_pgio_header *hdr = data->header;
529 struct nfs_rw_header *pageio_header = NFS_RW_HEADER(hdr);
531 put_nfs_open_context(data->args.context);
532 if (data->pages.pagevec != data->pages.page_array)
533 kfree(data->pages.pagevec);
534 if (data == &pageio_header->rpc_data) {
538 if (atomic_dec_and_test(&hdr->refcnt))
539 hdr->completion_ops->completion(hdr);
540 /* Note: we only free the rpc_task after callbacks are done.
541 * See the comment in rpc_free_task() for why
545 EXPORT_SYMBOL_GPL(nfs_pgio_data_release);
548 * nfs_pgio_rpcsetup - Set up arguments for a pageio call
549 * @data: The pageio data
550 * @count: Number of bytes to read
551 * @offset: Initial offset
552 * @how: How to commit data (writes only)
553 * @cinfo: Commit information for the call (writes only)
555 static void nfs_pgio_rpcsetup(struct nfs_pgio_data *data,
556 unsigned int count, unsigned int offset,
557 int how, struct nfs_commit_info *cinfo)
559 struct nfs_page *req = data->header->req;
561 /* Set up the RPC argument and reply structs
562 * NB: take care not to mess about with data->commit et al. */
564 data->args.fh = NFS_FH(data->header->inode);
565 data->args.offset = req_offset(req) + offset;
566 /* pnfs_set_layoutcommit needs this */
567 data->mds_offset = data->args.offset;
568 data->args.pgbase = req->wb_pgbase + offset;
569 data->args.pages = data->pages.pagevec;
570 data->args.count = count;
571 data->args.context = get_nfs_open_context(req->wb_context);
572 data->args.lock_context = req->wb_lock_context;
573 data->args.stable = NFS_UNSTABLE;
574 switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
577 case FLUSH_COND_STABLE:
578 if (nfs_reqs_to_commit(cinfo))
581 data->args.stable = NFS_FILE_SYNC;
584 data->res.fattr = &data->fattr;
585 data->res.count = count;
587 data->res.verf = &data->verf;
588 nfs_fattr_init(&data->fattr);
592 * nfs_pgio_prepare - Prepare pageio data to go over the wire
593 * @task: The current task
594 * @calldata: pageio data to prepare
596 static void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
598 struct nfs_pgio_data *data = calldata;
600 err = NFS_PROTO(data->header->inode)->pgio_rpc_prepare(task, data);
605 int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_data *data,
606 const struct rpc_call_ops *call_ops, int how, int flags)
608 struct rpc_task *task;
609 struct rpc_message msg = {
610 .rpc_argp = &data->args,
611 .rpc_resp = &data->res,
612 .rpc_cred = data->header->cred,
614 struct rpc_task_setup task_setup_data = {
618 .callback_ops = call_ops,
619 .callback_data = data,
620 .workqueue = nfsiod_workqueue,
621 .flags = RPC_TASK_ASYNC | flags,
625 data->header->rw_ops->rw_initiate(data, &msg, &task_setup_data, how);
627 dprintk("NFS: %5u initiated pgio call "
628 "(req %s/%llu, %u bytes @ offset %llu)\n",
630 data->header->inode->i_sb->s_id,
631 (unsigned long long)NFS_FILEID(data->header->inode),
633 (unsigned long long)data->args.offset);
635 task = rpc_run_task(&task_setup_data);
640 if (how & FLUSH_SYNC) {
641 ret = rpc_wait_for_completion_task(task);
643 ret = task->tk_status;
649 EXPORT_SYMBOL_GPL(nfs_initiate_pgio);
652 * nfs_pgio_error - Clean up from a pageio error
653 * @desc: IO descriptor
654 * @hdr: pageio header
656 static int nfs_pgio_error(struct nfs_pageio_descriptor *desc,
657 struct nfs_pgio_header *hdr)
659 set_bit(NFS_IOHDR_REDO, &hdr->flags);
660 nfs_pgio_data_release(hdr->data);
662 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
667 * nfs_pgio_release - Release pageio data
668 * @calldata: The pageio data to release
670 static void nfs_pgio_release(void *calldata)
672 struct nfs_pgio_data *data = calldata;
673 if (data->header->rw_ops->rw_release)
674 data->header->rw_ops->rw_release(data);
675 nfs_pgio_data_release(data);
679 * nfs_pageio_init - initialise a page io descriptor
680 * @desc: pointer to descriptor
681 * @inode: pointer to inode
682 * @doio: pointer to io function
683 * @bsize: io block size
684 * @io_flags: extra parameters for the io function
686 void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
688 const struct nfs_pageio_ops *pg_ops,
689 const struct nfs_pgio_completion_ops *compl_ops,
690 const struct nfs_rw_ops *rw_ops,
694 INIT_LIST_HEAD(&desc->pg_list);
695 desc->pg_bytes_written = 0;
697 desc->pg_bsize = bsize;
700 desc->pg_recoalesce = 0;
701 desc->pg_inode = inode;
702 desc->pg_ops = pg_ops;
703 desc->pg_completion_ops = compl_ops;
704 desc->pg_rw_ops = rw_ops;
705 desc->pg_ioflags = io_flags;
707 desc->pg_lseg = NULL;
708 desc->pg_dreq = NULL;
709 desc->pg_layout_private = NULL;
711 EXPORT_SYMBOL_GPL(nfs_pageio_init);
714 * nfs_pgio_result - Basic pageio error handling
715 * @task: The task that ran
716 * @calldata: Pageio data to check
718 static void nfs_pgio_result(struct rpc_task *task, void *calldata)
720 struct nfs_pgio_data *data = calldata;
721 struct inode *inode = data->header->inode;
723 dprintk("NFS: %s: %5u, (status %d)\n", __func__,
724 task->tk_pid, task->tk_status);
726 if (data->header->rw_ops->rw_done(task, data, inode) != 0)
728 if (task->tk_status < 0)
729 nfs_set_pgio_error(data->header, task->tk_status, data->args.offset);
731 data->header->rw_ops->rw_result(task, data);
735 * Create an RPC task for the given read or write request and kick it.
736 * The page must have been locked by the caller.
738 * It may happen that the page we're passed is not marked dirty.
739 * This is the case if nfs_updatepage detects a conflicting request
740 * that has been written but not committed.
742 int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
743 struct nfs_pgio_header *hdr)
745 struct nfs_page *req;
747 struct nfs_pgio_data *data;
748 struct list_head *head = &desc->pg_list;
749 struct nfs_commit_info cinfo;
751 data = nfs_pgio_data_alloc(hdr, nfs_page_array_len(desc->pg_base,
754 return nfs_pgio_error(desc, hdr);
756 nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
757 pages = data->pages.pagevec;
758 while (!list_empty(head)) {
759 req = nfs_list_entry(head->next);
760 nfs_list_remove_request(req);
761 nfs_list_add_request(req, &hdr->pages);
762 *pages++ = req->wb_page;
765 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
766 (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
767 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
769 /* Set up the argument struct */
770 nfs_pgio_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo);
772 desc->pg_rpc_callops = &nfs_pgio_common_ops;
775 EXPORT_SYMBOL_GPL(nfs_generic_pgio);
777 static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
779 struct nfs_rw_header *rw_hdr;
780 struct nfs_pgio_header *hdr;
783 rw_hdr = nfs_rw_header_alloc(desc->pg_rw_ops);
785 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
788 hdr = &rw_hdr->header;
789 nfs_pgheader_init(desc, hdr, nfs_rw_header_free);
790 atomic_inc(&hdr->refcnt);
791 ret = nfs_generic_pgio(desc, hdr);
793 ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode),
794 hdr->data, desc->pg_rpc_callops,
795 desc->pg_ioflags, 0);
796 if (atomic_dec_and_test(&hdr->refcnt))
797 hdr->completion_ops->completion(hdr);
801 static bool nfs_match_open_context(const struct nfs_open_context *ctx1,
802 const struct nfs_open_context *ctx2)
804 return ctx1->cred == ctx2->cred && ctx1->state == ctx2->state;
807 static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
808 const struct nfs_lock_context *l2)
810 return l1->lockowner.l_owner == l2->lockowner.l_owner
811 && l1->lockowner.l_pid == l2->lockowner.l_pid;
815 * nfs_can_coalesce_requests - test two requests for compatibility
816 * @prev: pointer to nfs_page
817 * @req: pointer to nfs_page
819 * The nfs_page structures 'prev' and 'req' are compared to ensure that the
820 * page data area they describe is contiguous, and that their RPC
821 * credentials, NFSv4 open state, and lockowners are the same.
823 * Return 'true' if this is the case, else return 'false'.
825 static bool nfs_can_coalesce_requests(struct nfs_page *prev,
826 struct nfs_page *req,
827 struct nfs_pageio_descriptor *pgio)
832 if (!nfs_match_open_context(req->wb_context, prev->wb_context))
834 if (req->wb_context->dentry->d_inode->i_flock != NULL &&
835 !nfs_match_lock_context(req->wb_lock_context,
836 prev->wb_lock_context))
838 if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
841 size = pgio->pg_ops->pg_test(pgio, prev, req);
842 WARN_ON_ONCE(size > req->wb_bytes);
843 if (size && size < req->wb_bytes)
844 req->wb_bytes = size;
849 * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
850 * @desc: destination io descriptor
853 * Returns true if the request 'req' was successfully coalesced into the
854 * existing list of pages 'desc'.
856 static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
857 struct nfs_page *req)
859 struct nfs_page *prev = NULL;
860 if (desc->pg_count != 0) {
861 prev = nfs_list_entry(desc->pg_list.prev);
863 if (desc->pg_ops->pg_init)
864 desc->pg_ops->pg_init(desc, req);
865 desc->pg_base = req->wb_pgbase;
867 if (!nfs_can_coalesce_requests(prev, req, desc))
869 nfs_list_remove_request(req);
870 nfs_list_add_request(req, &desc->pg_list);
871 desc->pg_count += req->wb_bytes;
876 * Helper for nfs_pageio_add_request and nfs_pageio_complete
878 static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
880 if (!list_empty(&desc->pg_list)) {
881 int error = desc->pg_ops->pg_doio(desc);
883 desc->pg_error = error;
885 desc->pg_bytes_written += desc->pg_count;
887 if (list_empty(&desc->pg_list)) {
894 * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
895 * @desc: destination io descriptor
898 * This may split a request into subrequests which are all part of the
901 * Returns true if the request 'req' was successfully coalesced into the
902 * existing list of pages 'desc'.
904 static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
905 struct nfs_page *req)
907 struct nfs_page *subreq;
908 unsigned int bytes_left = 0;
909 unsigned int offset, pgbase;
911 nfs_page_group_lock(req);
914 bytes_left = subreq->wb_bytes;
915 offset = subreq->wb_offset;
916 pgbase = subreq->wb_pgbase;
919 if (!nfs_pageio_do_add_request(desc, subreq)) {
920 /* make sure pg_test call(s) did nothing */
921 WARN_ON_ONCE(subreq->wb_bytes != bytes_left);
922 WARN_ON_ONCE(subreq->wb_offset != offset);
923 WARN_ON_ONCE(subreq->wb_pgbase != pgbase);
925 nfs_page_group_unlock(req);
927 nfs_pageio_doio(desc);
928 if (desc->pg_error < 0)
930 if (desc->pg_recoalesce)
932 /* retry add_request for this subreq */
933 nfs_page_group_lock(req);
937 /* check for buggy pg_test call(s) */
938 WARN_ON_ONCE(subreq->wb_bytes + subreq->wb_pgbase > PAGE_SIZE);
939 WARN_ON_ONCE(subreq->wb_bytes > bytes_left);
940 WARN_ON_ONCE(subreq->wb_bytes == 0);
942 bytes_left -= subreq->wb_bytes;
943 offset += subreq->wb_bytes;
944 pgbase += subreq->wb_bytes;
947 subreq = nfs_create_request(req->wb_context,
949 subreq, pgbase, bytes_left);
952 nfs_lock_request(subreq);
953 subreq->wb_offset = offset;
954 subreq->wb_index = req->wb_index;
956 } while (bytes_left > 0);
958 nfs_page_group_unlock(req);
961 desc->pg_error = PTR_ERR(subreq);
962 nfs_page_group_unlock(req);
966 static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
971 list_splice_init(&desc->pg_list, &head);
972 desc->pg_bytes_written -= desc->pg_count;
975 desc->pg_recoalesce = 0;
978 while (!list_empty(&head)) {
979 struct nfs_page *req;
981 req = list_first_entry(&head, struct nfs_page, wb_list);
982 nfs_list_remove_request(req);
983 if (__nfs_pageio_add_request(desc, req))
985 if (desc->pg_error < 0)
989 } while (desc->pg_recoalesce);
993 int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
994 struct nfs_page *req)
999 ret = __nfs_pageio_add_request(desc, req);
1002 if (desc->pg_error < 0)
1004 ret = nfs_do_recoalesce(desc);
1008 EXPORT_SYMBOL_GPL(nfs_pageio_add_request);
1011 * nfs_pageio_complete - Complete I/O on an nfs_pageio_descriptor
1012 * @desc: pointer to io descriptor
1014 void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
1017 nfs_pageio_doio(desc);
1018 if (!desc->pg_recoalesce)
1020 if (!nfs_do_recoalesce(desc))
1024 EXPORT_SYMBOL_GPL(nfs_pageio_complete);
1027 * nfs_pageio_cond_complete - Conditional I/O completion
1028 * @desc: pointer to io descriptor
1029 * @index: page index
1031 * It is important to ensure that processes don't try to take locks
1032 * on non-contiguous ranges of pages as that might deadlock. This
1033 * function should be called before attempting to wait on a locked
1034 * nfs_page. It will complete the I/O if the page index 'index'
1035 * is not contiguous with the existing list of pages in 'desc'.
1037 void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
1039 if (!list_empty(&desc->pg_list)) {
1040 struct nfs_page *prev = nfs_list_entry(desc->pg_list.prev);
1041 if (index != prev->wb_index + 1)
1042 nfs_pageio_complete(desc);
1046 int __init nfs_init_nfspagecache(void)
1048 nfs_page_cachep = kmem_cache_create("nfs_page",
1049 sizeof(struct nfs_page),
1050 0, SLAB_HWCACHE_ALIGN,
1052 if (nfs_page_cachep == NULL)
1058 void nfs_destroy_nfspagecache(void)
1060 kmem_cache_destroy(nfs_page_cachep);
1063 static const struct rpc_call_ops nfs_pgio_common_ops = {
1064 .rpc_call_prepare = nfs_pgio_prepare,
1065 .rpc_call_done = nfs_pgio_result,
1066 .rpc_release = nfs_pgio_release,
1069 const struct nfs_pageio_ops nfs_pgio_rw_ops = {
1070 .pg_test = nfs_generic_pg_test,
1071 .pg_doio = nfs_generic_pg_pgios,