4 * Writing file data over NFS.
6 * We do it like this: When a (user) process wishes to write data to an
7 * NFS file, a write request is allocated that contains the RPC task data
8 * plus some info on the page to be written, and added to the inode's
9 * write chain. If the process writes past the end of the page, an async
10 * RPC call to write the page is scheduled immediately; otherwise, the call
11 * is delayed for a few seconds.
13 * Just like readahead, no async I/O is performed if wsize < PAGE_SIZE.
15 * Write requests are kept on the inode's writeback list. Each entry in
16 * that list references the page (portion) to be written. When the
17 * cache timeout has expired, the RPC task is woken up, and tries to
18 * lock the page. As soon as it manages to do so, the request is moved
19 * from the writeback list to the writelock list.
21 * Note: we must make sure never to confuse the inode passed in the
22 * write_page request with the one in page->inode. As far as I understand
23 * it, these are different when doing a swap-out.
25 * To understand everything that goes on here and in the NFS read code,
26 * one should be aware that a page is locked in exactly one of the following
29 * - A write request is in progress.
30 * - A user process is in generic_file_write/nfs_update_page
31 * - A user process is in generic_file_read
33 * Also note that because of the way pages are invalidated in
34 * nfs_revalidate_inode, the following assertions hold:
36 * - If a page is dirty, there will be no read requests (a page will
37 * not be re-read unless invalidated by nfs_revalidate_inode).
38 * - If the page is not uptodate, there will be no pending write
39 * requests, and no process will be in nfs_update_page.
41 * FIXME: Interaction with the vmscan routines is not optimal yet.
42 * Either vmscan must be made nfs-savvy, or we need a different page
43 * reclaim concept that supports something like FS-independent
44 * buffer_heads with a b_ops-> field.
46 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
49 #include <linux/config.h>
50 #include <linux/types.h>
51 #include <linux/slab.h>
53 #include <linux/pagemap.h>
54 #include <linux/file.h>
55 #include <linux/mpage.h>
56 #include <linux/writeback.h>
58 #include <linux/sunrpc/clnt.h>
59 #include <linux/nfs_fs.h>
60 #include <linux/nfs_mount.h>
61 #include <linux/nfs_page.h>
62 #include <asm/uaccess.h>
63 #include <linux/smp_lock.h>
65 #include "delegation.h"
67 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
69 #define MIN_POOL_WRITE (32)
70 #define MIN_POOL_COMMIT (4)
73 * Local function declarations
75 static struct nfs_page * nfs_update_request(struct nfs_open_context*,
78 unsigned int, unsigned int);
79 static void nfs_writeback_done_partial(struct nfs_write_data *, int);
80 static void nfs_writeback_done_full(struct nfs_write_data *, int);
81 static int nfs_wait_on_write_congestion(struct address_space *, int);
82 static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
84 static kmem_cache_t *nfs_wdata_cachep;
85 mempool_t *nfs_wdata_mempool;
86 mempool_t *nfs_commit_mempool;
88 static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
90 void nfs_writedata_release(struct rpc_task *task)
92 struct nfs_write_data *wdata = (struct nfs_write_data *)task->tk_calldata;
93 nfs_writedata_free(wdata);
96 /* Adjust the file length if we're writing beyond the end */
97 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
99 struct inode *inode = page->mapping->host;
100 loff_t end, i_size = i_size_read(inode);
101 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
103 if (i_size > 0 && page->index < end_index)
105 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
108 i_size_write(inode, end);
111 /* We can set the PG_uptodate flag if we see that a write request
112 * covers the full page.
114 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
118 if (PageUptodate(page))
122 if (count == PAGE_CACHE_SIZE) {
123 SetPageUptodate(page);
127 end_offs = i_size_read(page->mapping->host) - 1;
130 /* Is this the last page? */
131 if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
133 /* This is the last page: set PG_uptodate if we cover the entire
134 * extent of the data, then zero the rest of the page.
136 if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
137 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
138 SetPageUptodate(page);
143 * Write a page synchronously.
144 * Offset is the data offset within the page.
146 static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
147 struct page *page, unsigned int offset, unsigned int count,
150 unsigned int wsize = NFS_SERVER(inode)->wsize;
151 int result, written = 0;
152 struct nfs_write_data *wdata;
154 wdata = nfs_writedata_alloc();
159 wdata->cred = ctx->cred;
160 wdata->inode = inode;
161 wdata->args.fh = NFS_FH(inode);
162 wdata->args.context = ctx;
163 wdata->args.pages = &page;
164 wdata->args.stable = NFS_FILE_SYNC;
165 wdata->args.pgbase = offset;
166 wdata->args.count = wsize;
167 wdata->res.fattr = &wdata->fattr;
168 wdata->res.verf = &wdata->verf;
170 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
172 (long long)NFS_FILEID(inode),
173 count, (long long)(page_offset(page) + offset));
175 nfs_begin_data_update(inode);
178 wdata->args.count = count;
179 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
181 result = NFS_PROTO(inode)->write(wdata);
184 /* Must mark the page invalid after I/O error */
185 ClearPageUptodate(page);
188 if (result < wdata->args.count)
189 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
190 wdata->args.count, result);
192 wdata->args.offset += result;
193 wdata->args.pgbase += result;
197 /* Update file length */
198 nfs_grow_file(page, offset, written);
199 /* Set the PG_uptodate flag? */
200 nfs_mark_uptodate(page, offset, written);
203 ClearPageError(page);
206 nfs_end_data_update_defer(inode);
207 nfs_writedata_free(wdata);
208 return written ? written : result;
211 static int nfs_writepage_async(struct nfs_open_context *ctx,
212 struct inode *inode, struct page *page,
213 unsigned int offset, unsigned int count)
215 struct nfs_page *req;
218 req = nfs_update_request(ctx, inode, page, offset, count);
219 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
222 /* Update file length */
223 nfs_grow_file(page, offset, count);
224 /* Set the PG_uptodate flag? */
225 nfs_mark_uptodate(page, offset, count);
226 nfs_unlock_request(req);
231 static int wb_priority(struct writeback_control *wbc)
233 if (wbc->for_reclaim)
234 return FLUSH_HIGHPRI;
235 if (wbc->for_kupdate)
241 * Write an mmapped page to the server.
243 int nfs_writepage(struct page *page, struct writeback_control *wbc)
245 struct nfs_open_context *ctx;
246 struct inode *inode = page->mapping->host;
247 unsigned long end_index;
248 unsigned offset = PAGE_CACHE_SIZE;
249 loff_t i_size = i_size_read(inode);
250 int inode_referenced = 0;
251 int priority = wb_priority(wbc);
255 * Note: We need to ensure that we have a reference to the inode
256 * if we are to do asynchronous writes. If not, waiting
257 * in nfs_wait_on_request() may deadlock with clear_inode().
259 * If igrab() fails here, then it is in any case safe to
260 * call nfs_wb_page(), since there will be no pending writes.
262 if (igrab(inode) != 0)
263 inode_referenced = 1;
264 end_index = i_size >> PAGE_CACHE_SHIFT;
266 /* Ensure we've flushed out any previous writes */
267 nfs_wb_page_priority(inode, page, priority);
270 if (page->index < end_index)
272 /* things got complicated... */
273 offset = i_size & (PAGE_CACHE_SIZE-1);
275 /* OK, are we completely out? */
276 err = 0; /* potential race with truncate - ignore */
277 if (page->index >= end_index+1 || !offset)
280 ctx = nfs_find_open_context(inode, FMODE_WRITE);
286 if (!IS_SYNC(inode) && inode_referenced) {
287 err = nfs_writepage_async(ctx, inode, page, 0, offset);
290 if (wbc->for_reclaim)
291 nfs_flush_inode(inode, 0, 0, FLUSH_STABLE);
294 err = nfs_writepage_sync(ctx, inode, page, 0,
298 redirty_page_for_writepage(wbc, page);
303 put_nfs_open_context(ctx);
306 if (inode_referenced)
312 * Note: causes nfs_update_request() to block on the assumption
313 * that the writeback is generated due to memory pressure.
315 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
317 struct backing_dev_info *bdi = mapping->backing_dev_info;
318 struct inode *inode = mapping->host;
321 err = generic_writepages(mapping, wbc);
324 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
325 if (wbc->nonblocking)
327 nfs_wait_on_write_congestion(mapping, 0);
329 err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
332 wbc->nr_to_write -= err;
333 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
334 err = nfs_wait_on_requests(inode, 0, 0);
338 err = nfs_commit_inode(inode, 0, 0, wb_priority(wbc));
340 wbc->nr_to_write -= err;
344 clear_bit(BDI_write_congested, &bdi->state);
345 wake_up_all(&nfs_write_congestion);
350 * Insert a write request into an inode
352 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
354 struct nfs_inode *nfsi = NFS_I(inode);
357 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
358 BUG_ON(error == -EEXIST);
363 nfs_begin_data_update(inode);
364 if (nfs_have_delegation(inode, FMODE_WRITE))
368 atomic_inc(&req->wb_count);
373 * Insert a write request into an inode
375 static void nfs_inode_remove_request(struct nfs_page *req)
377 struct inode *inode = req->wb_context->dentry->d_inode;
378 struct nfs_inode *nfsi = NFS_I(inode);
380 BUG_ON (!NFS_WBACK_BUSY(req));
382 spin_lock(&nfsi->req_lock);
383 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
386 spin_unlock(&nfsi->req_lock);
387 nfs_end_data_update_defer(inode);
390 spin_unlock(&nfsi->req_lock);
391 nfs_clear_request(req);
392 nfs_release_request(req);
398 static inline struct nfs_page *
399 _nfs_find_request(struct inode *inode, unsigned long index)
401 struct nfs_inode *nfsi = NFS_I(inode);
402 struct nfs_page *req;
404 req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
406 atomic_inc(&req->wb_count);
410 static struct nfs_page *
411 nfs_find_request(struct inode *inode, unsigned long index)
413 struct nfs_page *req;
414 struct nfs_inode *nfsi = NFS_I(inode);
416 spin_lock(&nfsi->req_lock);
417 req = _nfs_find_request(inode, index);
418 spin_unlock(&nfsi->req_lock);
423 * Add a request to the inode's dirty list.
426 nfs_mark_request_dirty(struct nfs_page *req)
428 struct inode *inode = req->wb_context->dentry->d_inode;
429 struct nfs_inode *nfsi = NFS_I(inode);
431 spin_lock(&nfsi->req_lock);
432 nfs_list_add_request(req, &nfsi->dirty);
434 spin_unlock(&nfsi->req_lock);
435 inc_page_state(nr_dirty);
436 mark_inode_dirty(inode);
440 * Check if a request is dirty
443 nfs_dirty_request(struct nfs_page *req)
445 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
446 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
449 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
451 * Add a request to the inode's commit list.
454 nfs_mark_request_commit(struct nfs_page *req)
456 struct inode *inode = req->wb_context->dentry->d_inode;
457 struct nfs_inode *nfsi = NFS_I(inode);
459 spin_lock(&nfsi->req_lock);
460 nfs_list_add_request(req, &nfsi->commit);
462 spin_unlock(&nfsi->req_lock);
463 inc_page_state(nr_unstable);
464 mark_inode_dirty(inode);
469 * Wait for a request to complete.
471 * Interruptible by signals only if mounted with intr flag.
474 nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
476 struct nfs_inode *nfsi = NFS_I(inode);
477 struct nfs_page *req;
478 unsigned long idx_end, next;
479 unsigned int res = 0;
485 idx_end = idx_start + npages - 1;
487 spin_lock(&nfsi->req_lock);
489 while (radix_tree_gang_lookup(&nfsi->nfs_page_tree, (void **)&req, next, 1)) {
490 if (req->wb_index > idx_end)
493 next = req->wb_index + 1;
494 if (!NFS_WBACK_BUSY(req))
497 atomic_inc(&req->wb_count);
498 spin_unlock(&nfsi->req_lock);
499 error = nfs_wait_on_request(req);
500 nfs_release_request(req);
503 spin_lock(&nfsi->req_lock);
506 spin_unlock(&nfsi->req_lock);
511 * nfs_scan_dirty - Scan an inode for dirty requests
512 * @inode: NFS inode to scan
513 * @dst: destination list
514 * @idx_start: lower bound of page->index to scan.
515 * @npages: idx_start + npages sets the upper bound to scan.
517 * Moves requests from the inode's dirty page list.
518 * The requests are *not* checked to ensure that they form a contiguous set.
521 nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
523 struct nfs_inode *nfsi = NFS_I(inode);
525 res = nfs_scan_list(&nfsi->dirty, dst, idx_start, npages);
527 sub_page_state(nr_dirty,res);
528 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
529 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
533 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
535 * nfs_scan_commit - Scan an inode for commit requests
536 * @inode: NFS inode to scan
537 * @dst: destination list
538 * @idx_start: lower bound of page->index to scan.
539 * @npages: idx_start + npages sets the upper bound to scan.
541 * Moves requests from the inode's 'commit' request list.
542 * The requests are *not* checked to ensure that they form a contiguous set.
545 nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
547 struct nfs_inode *nfsi = NFS_I(inode);
549 res = nfs_scan_list(&nfsi->commit, dst, idx_start, npages);
550 nfsi->ncommit -= res;
551 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
552 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
557 static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
559 struct backing_dev_info *bdi = mapping->backing_dev_info;
565 if (!bdi_write_congested(bdi))
568 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
571 rpc_clnt_sigmask(clnt, &oldset);
572 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
573 if (bdi_write_congested(bdi)) {
579 rpc_clnt_sigunmask(clnt, &oldset);
581 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
582 if (bdi_write_congested(bdi))
585 finish_wait(&nfs_write_congestion, &wait);
591 * Try to update any existing write request, or create one if there is none.
592 * In order to match, the request's credentials must match those of
593 * the calling process.
595 * Note: Should always be called with the Page Lock held!
597 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
598 struct inode *inode, struct page *page,
599 unsigned int offset, unsigned int bytes)
601 struct nfs_server *server = NFS_SERVER(inode);
602 struct nfs_inode *nfsi = NFS_I(inode);
603 struct nfs_page *req, *new = NULL;
604 unsigned long rqend, end;
606 end = offset + bytes;
608 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
609 return ERR_PTR(-ERESTARTSYS);
611 /* Loop over all inode entries and see if we find
612 * A request for the page we wish to update
614 spin_lock(&nfsi->req_lock);
615 req = _nfs_find_request(inode, page->index);
617 if (!nfs_lock_request_dontget(req)) {
619 spin_unlock(&nfsi->req_lock);
620 error = nfs_wait_on_request(req);
621 nfs_release_request(req);
623 return ERR_PTR(error);
626 spin_unlock(&nfsi->req_lock);
628 nfs_release_request(new);
634 nfs_lock_request_dontget(new);
635 error = nfs_inode_add_request(inode, new);
637 spin_unlock(&nfsi->req_lock);
638 nfs_unlock_request(new);
639 return ERR_PTR(error);
641 spin_unlock(&nfsi->req_lock);
642 nfs_mark_request_dirty(new);
645 spin_unlock(&nfsi->req_lock);
647 new = nfs_create_request(ctx, inode, page, offset, bytes);
652 /* We have a request for our page.
653 * If the creds don't match, or the
654 * page addresses don't match,
655 * tell the caller to wait on the conflicting
658 rqend = req->wb_offset + req->wb_bytes;
659 if (req->wb_context != ctx
660 || req->wb_page != page
661 || !nfs_dirty_request(req)
662 || offset > rqend || end < req->wb_offset) {
663 nfs_unlock_request(req);
664 return ERR_PTR(-EBUSY);
667 /* Okay, the request matches. Update the region */
668 if (offset < req->wb_offset) {
669 req->wb_offset = offset;
670 req->wb_pgbase = offset;
671 req->wb_bytes = rqend - req->wb_offset;
675 req->wb_bytes = end - req->wb_offset;
680 int nfs_flush_incompatible(struct file *file, struct page *page)
682 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
683 struct inode *inode = page->mapping->host;
684 struct nfs_page *req;
687 * Look for a request corresponding to this page. If there
688 * is one, and it belongs to another file, we flush it out
689 * before we try to copy anything into the page. Do this
690 * due to the lack of an ACCESS-type call in NFSv2.
691 * Also do the same if we find a request from an existing
694 req = nfs_find_request(inode, page->index);
696 if (req->wb_page != page || ctx != req->wb_context)
697 status = nfs_wb_page(inode, page);
698 nfs_release_request(req);
700 return (status < 0) ? status : 0;
704 * Update and possibly write a cached page of an NFS file.
706 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
707 * things with a page scheduled for an RPC call (e.g. invalidate it).
709 int nfs_updatepage(struct file *file, struct page *page,
710 unsigned int offset, unsigned int count)
712 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
713 struct dentry *dentry = file->f_dentry;
714 struct inode *inode = page->mapping->host;
715 struct nfs_page *req;
718 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
719 dentry->d_parent->d_name.name, dentry->d_name.name,
720 count, (long long)(page_offset(page) +offset));
722 if (IS_SYNC(inode)) {
723 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
725 if (offset == 0 && status == PAGE_CACHE_SIZE)
726 SetPageUptodate(page);
732 /* If we're not using byte range locks, and we know the page
733 * is entirely in cache, it may be more efficient to avoid
734 * fragmenting write requests.
736 if (PageUptodate(page) && inode->i_flock == NULL) {
737 loff_t end_offs = i_size_read(inode) - 1;
738 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
742 if (unlikely(end_offs < 0)) {
744 } else if (page->index == end_index) {
746 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
749 } else if (page->index < end_index)
750 count = PAGE_CACHE_SIZE;
754 * Try to find an NFS request corresponding to this page
756 * If the existing request cannot be updated, we must flush
760 req = nfs_update_request(ctx, inode, page, offset, count);
761 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
762 if (status != -EBUSY)
764 /* Request could not be updated. Flush it out and try again */
765 status = nfs_wb_page(inode, page);
766 } while (status >= 0);
772 /* Update file length */
773 nfs_grow_file(page, offset, count);
774 /* Set the PG_uptodate flag? */
775 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
776 nfs_unlock_request(req);
778 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
779 status, (long long)i_size_read(inode));
781 ClearPageUptodate(page);
785 static void nfs_writepage_release(struct nfs_page *req)
787 end_page_writeback(req->wb_page);
789 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
790 if (!PageError(req->wb_page)) {
791 if (NFS_NEED_RESCHED(req)) {
792 nfs_mark_request_dirty(req);
794 } else if (NFS_NEED_COMMIT(req)) {
795 nfs_mark_request_commit(req);
799 nfs_inode_remove_request(req);
802 nfs_clear_commit(req);
803 nfs_clear_reschedule(req);
805 nfs_inode_remove_request(req);
807 nfs_unlock_request(req);
810 static inline int flush_task_priority(int how)
812 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
814 return RPC_PRIORITY_HIGH;
816 return RPC_PRIORITY_LOW;
818 return RPC_PRIORITY_NORMAL;
822 * Set up the argument/result storage required for the RPC call.
824 static void nfs_write_rpcsetup(struct nfs_page *req,
825 struct nfs_write_data *data,
826 unsigned int count, unsigned int offset,
829 struct rpc_task *task = &data->task;
832 /* Set up the RPC argument and reply structs
833 * NB: take care not to mess about with data->commit et al. */
836 data->inode = inode = req->wb_context->dentry->d_inode;
837 data->cred = req->wb_context->cred;
839 data->args.fh = NFS_FH(inode);
840 data->args.offset = req_offset(req) + offset;
841 data->args.pgbase = req->wb_pgbase + offset;
842 data->args.pages = data->pagevec;
843 data->args.count = count;
844 data->args.context = req->wb_context;
846 data->res.fattr = &data->fattr;
847 data->res.count = count;
848 data->res.verf = &data->verf;
850 NFS_PROTO(inode)->write_setup(data, how);
852 data->task.tk_priority = flush_task_priority(how);
853 data->task.tk_cookie = (unsigned long)inode;
854 data->task.tk_calldata = data;
855 /* Release requests */
856 data->task.tk_release = nfs_writedata_release;
858 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
861 (long long)NFS_FILEID(inode),
863 (unsigned long long)data->args.offset);
866 static void nfs_execute_write(struct nfs_write_data *data)
868 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
871 rpc_clnt_sigmask(clnt, &oldset);
873 rpc_execute(&data->task);
875 rpc_clnt_sigunmask(clnt, &oldset);
879 * Generate multiple small requests to write out a single
880 * contiguous dirty area on one page.
882 static int nfs_flush_multi(struct list_head *head, struct inode *inode, int how)
884 struct nfs_page *req = nfs_list_entry(head->next);
885 struct page *page = req->wb_page;
886 struct nfs_write_data *data;
887 unsigned int wsize = NFS_SERVER(inode)->wsize;
888 unsigned int nbytes, offset;
892 nfs_list_remove_request(req);
894 nbytes = req->wb_bytes;
896 data = nfs_writedata_alloc();
899 list_add(&data->pages, &list);
905 atomic_set(&req->wb_complete, requests);
907 ClearPageError(page);
908 SetPageWriteback(page);
910 nbytes = req->wb_bytes;
912 data = list_entry(list.next, struct nfs_write_data, pages);
913 list_del_init(&data->pages);
915 data->pagevec[0] = page;
916 data->complete = nfs_writeback_done_partial;
918 if (nbytes > wsize) {
919 nfs_write_rpcsetup(req, data, wsize, offset, how);
923 nfs_write_rpcsetup(req, data, nbytes, offset, how);
926 nfs_execute_write(data);
927 } while (nbytes != 0);
932 while (!list_empty(&list)) {
933 data = list_entry(list.next, struct nfs_write_data, pages);
934 list_del(&data->pages);
935 nfs_writedata_free(data);
937 nfs_mark_request_dirty(req);
938 nfs_unlock_request(req);
943 * Create an RPC task for the given write request and kick it.
944 * The page must have been locked by the caller.
946 * It may happen that the page we're passed is not marked dirty.
947 * This is the case if nfs_updatepage detects a conflicting request
948 * that has been written but not committed.
950 static int nfs_flush_one(struct list_head *head, struct inode *inode, int how)
952 struct nfs_page *req;
954 struct nfs_write_data *data;
957 if (NFS_SERVER(inode)->wsize < PAGE_CACHE_SIZE)
958 return nfs_flush_multi(head, inode, how);
960 data = nfs_writedata_alloc();
964 pages = data->pagevec;
966 while (!list_empty(head)) {
967 req = nfs_list_entry(head->next);
968 nfs_list_remove_request(req);
969 nfs_list_add_request(req, &data->pages);
970 ClearPageError(req->wb_page);
971 SetPageWriteback(req->wb_page);
972 *pages++ = req->wb_page;
973 count += req->wb_bytes;
975 req = nfs_list_entry(data->pages.next);
977 data->complete = nfs_writeback_done_full;
978 /* Set up the argument struct */
979 nfs_write_rpcsetup(req, data, count, 0, how);
981 nfs_execute_write(data);
984 while (!list_empty(head)) {
985 struct nfs_page *req = nfs_list_entry(head->next);
986 nfs_list_remove_request(req);
987 nfs_mark_request_dirty(req);
988 nfs_unlock_request(req);
994 nfs_flush_list(struct list_head *head, int wpages, int how)
996 LIST_HEAD(one_request);
997 struct nfs_page *req;
999 unsigned int pages = 0;
1001 while (!list_empty(head)) {
1002 pages += nfs_coalesce_requests(head, &one_request, wpages);
1003 req = nfs_list_entry(one_request.next);
1004 error = nfs_flush_one(&one_request, req->wb_context->dentry->d_inode, how);
1011 while (!list_empty(head)) {
1012 req = nfs_list_entry(head->next);
1013 nfs_list_remove_request(req);
1014 nfs_mark_request_dirty(req);
1015 nfs_unlock_request(req);
1021 * Handle a write reply that flushed part of a page.
1023 static void nfs_writeback_done_partial(struct nfs_write_data *data, int status)
1025 struct nfs_page *req = data->req;
1026 struct page *page = req->wb_page;
1028 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1029 req->wb_context->dentry->d_inode->i_sb->s_id,
1030 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1032 (long long)req_offset(req));
1035 ClearPageUptodate(page);
1037 req->wb_context->error = status;
1038 dprintk(", error = %d\n", status);
1040 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1041 if (data->verf.committed < NFS_FILE_SYNC) {
1042 if (!NFS_NEED_COMMIT(req)) {
1043 nfs_defer_commit(req);
1044 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1045 dprintk(" defer commit\n");
1046 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1047 nfs_defer_reschedule(req);
1048 dprintk(" server reboot detected\n");
1055 if (atomic_dec_and_test(&req->wb_complete))
1056 nfs_writepage_release(req);
1060 * Handle a write reply that flushes a whole page.
1062 * FIXME: There is an inherent race with invalidate_inode_pages and
1063 * writebacks since the page->count is kept > 1 for as long
1064 * as the page has a write request pending.
1066 static void nfs_writeback_done_full(struct nfs_write_data *data, int status)
1068 struct nfs_page *req;
1071 /* Update attributes as result of writeback. */
1072 while (!list_empty(&data->pages)) {
1073 req = nfs_list_entry(data->pages.next);
1074 nfs_list_remove_request(req);
1075 page = req->wb_page;
1077 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1078 req->wb_context->dentry->d_inode->i_sb->s_id,
1079 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1081 (long long)req_offset(req));
1084 ClearPageUptodate(page);
1086 req->wb_context->error = status;
1087 end_page_writeback(page);
1088 nfs_inode_remove_request(req);
1089 dprintk(", error = %d\n", status);
1092 end_page_writeback(page);
1094 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1095 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1096 nfs_inode_remove_request(req);
1100 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1101 nfs_mark_request_commit(req);
1102 dprintk(" marked for commit\n");
1104 nfs_inode_remove_request(req);
1107 nfs_unlock_request(req);
1112 * This function is called when the WRITE call is complete.
1114 void nfs_writeback_done(struct rpc_task *task)
1116 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
1117 struct nfs_writeargs *argp = &data->args;
1118 struct nfs_writeres *resp = &data->res;
1120 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1121 task->tk_pid, task->tk_status);
1123 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1124 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1125 /* We tried a write call, but the server did not
1126 * commit data to stable storage even though we
1128 * Note: There is a known bug in Tru64 < 5.0 in which
1129 * the server reports NFS_DATA_SYNC, but performs
1130 * NFS_FILE_SYNC. We therefore implement this checking
1131 * as a dprintk() in order to avoid filling syslog.
1133 static unsigned long complain;
1135 if (time_before(complain, jiffies)) {
1136 dprintk("NFS: faulty NFS server %s:"
1137 " (committed = %d) != (stable = %d)\n",
1138 NFS_SERVER(data->inode)->hostname,
1139 resp->verf->committed, argp->stable);
1140 complain = jiffies + 300 * HZ;
1144 /* Is this a short write? */
1145 if (task->tk_status >= 0 && resp->count < argp->count) {
1146 static unsigned long complain;
1148 /* Has the server at least made some progress? */
1149 if (resp->count != 0) {
1150 /* Was this an NFSv2 write or an NFSv3 stable write? */
1151 if (resp->verf->committed != NFS_UNSTABLE) {
1152 /* Resend from where the server left off */
1153 argp->offset += resp->count;
1154 argp->pgbase += resp->count;
1155 argp->count -= resp->count;
1157 /* Resend as a stable write in order to avoid
1158 * headaches in the case of a server crash.
1160 argp->stable = NFS_FILE_SYNC;
1162 rpc_restart_call(task);
1165 if (time_before(complain, jiffies)) {
1167 "NFS: Server wrote zero bytes, expected %u.\n",
1169 complain = jiffies + 300 * HZ;
1171 /* Can't do anything about it except throw an error. */
1172 task->tk_status = -EIO;
1176 * Process the nfs_page list
1178 data->complete(data, task->tk_status);
1182 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1183 static void nfs_commit_release(struct rpc_task *task)
1185 struct nfs_write_data *wdata = (struct nfs_write_data *)task->tk_calldata;
1186 nfs_commit_free(wdata);
1190 * Set up the argument/result storage required for the RPC call.
1192 static void nfs_commit_rpcsetup(struct list_head *head,
1193 struct nfs_write_data *data, int how)
1195 struct rpc_task *task = &data->task;
1196 struct nfs_page *first, *last;
1197 struct inode *inode;
1198 loff_t start, end, len;
1200 /* Set up the RPC argument and reply structs
1201 * NB: take care not to mess about with data->commit et al. */
1203 list_splice_init(head, &data->pages);
1204 first = nfs_list_entry(data->pages.next);
1205 last = nfs_list_entry(data->pages.prev);
1206 inode = first->wb_context->dentry->d_inode;
1209 * Determine the offset range of requests in the COMMIT call.
1210 * We rely on the fact that data->pages is an ordered list...
1212 start = req_offset(first);
1213 end = req_offset(last) + last->wb_bytes;
1215 /* If 'len' is not a 32-bit quantity, pass '0' in the COMMIT call */
1216 if (end >= i_size_read(inode) || len < 0 || len > (~((u32)0) >> 1))
1219 data->inode = inode;
1220 data->cred = first->wb_context->cred;
1222 data->args.fh = NFS_FH(data->inode);
1223 data->args.offset = start;
1224 data->args.count = len;
1225 data->res.count = len;
1226 data->res.fattr = &data->fattr;
1227 data->res.verf = &data->verf;
1229 NFS_PROTO(inode)->commit_setup(data, how);
1231 data->task.tk_priority = flush_task_priority(how);
1232 data->task.tk_cookie = (unsigned long)inode;
1233 data->task.tk_calldata = data;
1234 /* Release requests */
1235 data->task.tk_release = nfs_commit_release;
1237 dprintk("NFS: %4d initiated commit call\n", task->tk_pid);
1241 * Commit dirty pages
1244 nfs_commit_list(struct list_head *head, int how)
1246 struct nfs_write_data *data;
1247 struct nfs_page *req;
1249 data = nfs_commit_alloc();
1254 /* Set up the argument struct */
1255 nfs_commit_rpcsetup(head, data, how);
1257 nfs_execute_write(data);
1260 while (!list_empty(head)) {
1261 req = nfs_list_entry(head->next);
1262 nfs_list_remove_request(req);
1263 nfs_mark_request_commit(req);
1264 nfs_unlock_request(req);
1270 * COMMIT call returned
1273 nfs_commit_done(struct rpc_task *task)
1275 struct nfs_write_data *data = (struct nfs_write_data *)task->tk_calldata;
1276 struct nfs_page *req;
1279 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1280 task->tk_pid, task->tk_status);
1282 while (!list_empty(&data->pages)) {
1283 req = nfs_list_entry(data->pages.next);
1284 nfs_list_remove_request(req);
1286 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1287 req->wb_context->dentry->d_inode->i_sb->s_id,
1288 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1290 (long long)req_offset(req));
1291 if (task->tk_status < 0) {
1292 req->wb_context->error = task->tk_status;
1293 nfs_inode_remove_request(req);
1294 dprintk(", error = %d\n", task->tk_status);
1298 /* Okay, COMMIT succeeded, apparently. Check the verifier
1299 * returned by the server against all stored verfs. */
1300 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1301 /* We have a match */
1302 nfs_inode_remove_request(req);
1306 /* We have a mismatch. Write the page again */
1307 dprintk(" mismatch\n");
1308 nfs_mark_request_dirty(req);
1310 nfs_unlock_request(req);
1313 sub_page_state(nr_unstable,res);
1317 int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
1318 unsigned int npages, int how)
1320 struct nfs_inode *nfsi = NFS_I(inode);
1325 spin_lock(&nfsi->req_lock);
1326 res = nfs_scan_dirty(inode, &head, idx_start, npages);
1327 spin_unlock(&nfsi->req_lock);
1329 error = nfs_flush_list(&head, NFS_SERVER(inode)->wpages, how);
1335 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1336 int nfs_commit_inode(struct inode *inode, unsigned long idx_start,
1337 unsigned int npages, int how)
1339 struct nfs_inode *nfsi = NFS_I(inode);
1344 spin_lock(&nfsi->req_lock);
1345 res = nfs_scan_commit(inode, &head, idx_start, npages);
1347 res += nfs_scan_commit(inode, &head, 0, 0);
1348 spin_unlock(&nfsi->req_lock);
1349 error = nfs_commit_list(&head, how);
1351 spin_unlock(&nfsi->req_lock);
1358 int nfs_sync_inode(struct inode *inode, unsigned long idx_start,
1359 unsigned int npages, int how)
1364 wait = how & FLUSH_WAIT;
1370 error = nfs_wait_on_requests(inode, idx_start, npages);
1372 error = nfs_flush_inode(inode, idx_start, npages, how);
1373 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1375 error = nfs_commit_inode(inode, idx_start, npages, how);
1377 } while (error > 0);
1381 int nfs_init_writepagecache(void)
1383 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1384 sizeof(struct nfs_write_data),
1385 0, SLAB_HWCACHE_ALIGN,
1387 if (nfs_wdata_cachep == NULL)
1390 nfs_wdata_mempool = mempool_create(MIN_POOL_WRITE,
1394 if (nfs_wdata_mempool == NULL)
1397 nfs_commit_mempool = mempool_create(MIN_POOL_COMMIT,
1401 if (nfs_commit_mempool == NULL)
1407 void nfs_destroy_writepagecache(void)
1409 mempool_destroy(nfs_commit_mempool);
1410 mempool_destroy(nfs_wdata_mempool);
1411 if (kmem_cache_destroy(nfs_wdata_cachep))
1412 printk(KERN_INFO "nfs_write_data: not all structures were freed\n");