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);
83 static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
84 unsigned int npages, int how);
86 static kmem_cache_t *nfs_wdata_cachep;
87 mempool_t *nfs_wdata_mempool;
88 static mempool_t *nfs_commit_mempool;
90 static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
92 static inline struct nfs_write_data *nfs_commit_alloc(unsigned int pagecount)
94 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
97 memset(p, 0, sizeof(*p));
98 INIT_LIST_HEAD(&p->pages);
99 if (pagecount < NFS_PAGEVEC_SIZE)
100 p->pagevec = &p->page_array[0];
102 size_t size = ++pagecount * sizeof(struct page *);
103 p->pagevec = kzalloc(size, GFP_NOFS);
105 mempool_free(p, nfs_commit_mempool);
113 static inline void nfs_commit_free(struct nfs_write_data *p)
115 if (p && (p->pagevec != &p->page_array[0]))
117 mempool_free(p, nfs_commit_mempool);
120 void nfs_writedata_release(void *wdata)
122 nfs_writedata_free(wdata);
125 /* Adjust the file length if we're writing beyond the end */
126 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
128 struct inode *inode = page->mapping->host;
129 loff_t end, i_size = i_size_read(inode);
130 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
132 if (i_size > 0 && page->index < end_index)
134 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
137 i_size_write(inode, end);
140 /* We can set the PG_uptodate flag if we see that a write request
141 * covers the full page.
143 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
147 if (PageUptodate(page))
151 if (count == PAGE_CACHE_SIZE) {
152 SetPageUptodate(page);
156 end_offs = i_size_read(page->mapping->host) - 1;
159 /* Is this the last page? */
160 if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
162 /* This is the last page: set PG_uptodate if we cover the entire
163 * extent of the data, then zero the rest of the page.
165 if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
166 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
167 SetPageUptodate(page);
172 * Write a page synchronously.
173 * Offset is the data offset within the page.
175 static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
176 struct page *page, unsigned int offset, unsigned int count,
179 unsigned int wsize = NFS_SERVER(inode)->wsize;
180 int result, written = 0;
181 struct nfs_write_data *wdata;
183 wdata = nfs_writedata_alloc(1);
188 wdata->cred = ctx->cred;
189 wdata->inode = inode;
190 wdata->args.fh = NFS_FH(inode);
191 wdata->args.context = ctx;
192 wdata->args.pages = &page;
193 wdata->args.stable = NFS_FILE_SYNC;
194 wdata->args.pgbase = offset;
195 wdata->args.count = wsize;
196 wdata->res.fattr = &wdata->fattr;
197 wdata->res.verf = &wdata->verf;
199 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
201 (long long)NFS_FILEID(inode),
202 count, (long long)(page_offset(page) + offset));
204 set_page_writeback(page);
205 nfs_begin_data_update(inode);
208 wdata->args.count = count;
209 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
211 result = NFS_PROTO(inode)->write(wdata);
214 /* Must mark the page invalid after I/O error */
215 ClearPageUptodate(page);
218 if (result < wdata->args.count)
219 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
220 wdata->args.count, result);
222 wdata->args.offset += result;
223 wdata->args.pgbase += result;
227 /* Update file length */
228 nfs_grow_file(page, offset, written);
229 /* Set the PG_uptodate flag? */
230 nfs_mark_uptodate(page, offset, written);
233 ClearPageError(page);
236 nfs_end_data_update(inode);
237 end_page_writeback(page);
238 nfs_writedata_free(wdata);
239 return written ? written : result;
242 static int nfs_writepage_async(struct nfs_open_context *ctx,
243 struct inode *inode, struct page *page,
244 unsigned int offset, unsigned int count)
246 struct nfs_page *req;
248 req = nfs_update_request(ctx, inode, page, offset, count);
251 /* Update file length */
252 nfs_grow_file(page, offset, count);
253 /* Set the PG_uptodate flag? */
254 nfs_mark_uptodate(page, offset, count);
255 nfs_unlock_request(req);
259 static int wb_priority(struct writeback_control *wbc)
261 if (wbc->for_reclaim)
262 return FLUSH_HIGHPRI;
263 if (wbc->for_kupdate)
269 * Write an mmapped page to the server.
271 int nfs_writepage(struct page *page, struct writeback_control *wbc)
273 struct nfs_open_context *ctx;
274 struct inode *inode = page->mapping->host;
275 unsigned long end_index;
276 unsigned offset = PAGE_CACHE_SIZE;
277 loff_t i_size = i_size_read(inode);
278 int inode_referenced = 0;
279 int priority = wb_priority(wbc);
283 * Note: We need to ensure that we have a reference to the inode
284 * if we are to do asynchronous writes. If not, waiting
285 * in nfs_wait_on_request() may deadlock with clear_inode().
287 * If igrab() fails here, then it is in any case safe to
288 * call nfs_wb_page(), since there will be no pending writes.
290 if (igrab(inode) != 0)
291 inode_referenced = 1;
292 end_index = i_size >> PAGE_CACHE_SHIFT;
294 /* Ensure we've flushed out any previous writes */
295 nfs_wb_page_priority(inode, page, priority);
298 if (page->index < end_index)
300 /* things got complicated... */
301 offset = i_size & (PAGE_CACHE_SIZE-1);
303 /* OK, are we completely out? */
304 err = 0; /* potential race with truncate - ignore */
305 if (page->index >= end_index+1 || !offset)
308 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
314 if (!IS_SYNC(inode) && inode_referenced) {
315 err = nfs_writepage_async(ctx, inode, page, 0, offset);
316 if (!wbc->for_writepages)
317 nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
319 err = nfs_writepage_sync(ctx, inode, page, 0,
323 redirty_page_for_writepage(wbc, page);
328 put_nfs_open_context(ctx);
331 if (inode_referenced)
337 * Note: causes nfs_update_request() to block on the assumption
338 * that the writeback is generated due to memory pressure.
340 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
342 struct backing_dev_info *bdi = mapping->backing_dev_info;
343 struct inode *inode = mapping->host;
346 err = generic_writepages(mapping, wbc);
349 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
350 if (wbc->nonblocking)
352 nfs_wait_on_write_congestion(mapping, 0);
354 err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
357 wbc->nr_to_write -= err;
358 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
359 err = nfs_wait_on_requests(inode, 0, 0);
363 err = nfs_commit_inode(inode, wb_priority(wbc));
365 wbc->nr_to_write -= err;
369 clear_bit(BDI_write_congested, &bdi->state);
370 wake_up_all(&nfs_write_congestion);
375 * Insert a write request into an inode
377 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
379 struct nfs_inode *nfsi = NFS_I(inode);
382 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
383 BUG_ON(error == -EEXIST);
388 nfs_begin_data_update(inode);
389 if (nfs_have_delegation(inode, FMODE_WRITE))
393 atomic_inc(&req->wb_count);
398 * Insert a write request into an inode
400 static void nfs_inode_remove_request(struct nfs_page *req)
402 struct inode *inode = req->wb_context->dentry->d_inode;
403 struct nfs_inode *nfsi = NFS_I(inode);
405 BUG_ON (!NFS_WBACK_BUSY(req));
407 spin_lock(&nfsi->req_lock);
408 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
411 spin_unlock(&nfsi->req_lock);
412 nfs_end_data_update(inode);
415 spin_unlock(&nfsi->req_lock);
416 nfs_clear_request(req);
417 nfs_release_request(req);
423 static inline struct nfs_page *
424 _nfs_find_request(struct inode *inode, unsigned long index)
426 struct nfs_inode *nfsi = NFS_I(inode);
427 struct nfs_page *req;
429 req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
431 atomic_inc(&req->wb_count);
435 static struct nfs_page *
436 nfs_find_request(struct inode *inode, unsigned long index)
438 struct nfs_page *req;
439 struct nfs_inode *nfsi = NFS_I(inode);
441 spin_lock(&nfsi->req_lock);
442 req = _nfs_find_request(inode, index);
443 spin_unlock(&nfsi->req_lock);
448 * Add a request to the inode's dirty list.
451 nfs_mark_request_dirty(struct nfs_page *req)
453 struct inode *inode = req->wb_context->dentry->d_inode;
454 struct nfs_inode *nfsi = NFS_I(inode);
456 spin_lock(&nfsi->req_lock);
457 radix_tree_tag_set(&nfsi->nfs_page_tree,
458 req->wb_index, NFS_PAGE_TAG_DIRTY);
459 nfs_list_add_request(req, &nfsi->dirty);
461 spin_unlock(&nfsi->req_lock);
462 inc_page_state(nr_dirty);
463 mark_inode_dirty(inode);
467 * Check if a request is dirty
470 nfs_dirty_request(struct nfs_page *req)
472 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
473 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
476 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
478 * Add a request to the inode's commit list.
481 nfs_mark_request_commit(struct nfs_page *req)
483 struct inode *inode = req->wb_context->dentry->d_inode;
484 struct nfs_inode *nfsi = NFS_I(inode);
486 spin_lock(&nfsi->req_lock);
487 nfs_list_add_request(req, &nfsi->commit);
489 spin_unlock(&nfsi->req_lock);
490 inc_page_state(nr_unstable);
491 mark_inode_dirty(inode);
496 * Wait for a request to complete.
498 * Interruptible by signals only if mounted with intr flag.
501 nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
503 struct nfs_inode *nfsi = NFS_I(inode);
504 struct nfs_page *req;
505 unsigned long idx_end, next;
506 unsigned int res = 0;
512 idx_end = idx_start + npages - 1;
514 spin_lock(&nfsi->req_lock);
516 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
517 if (req->wb_index > idx_end)
520 next = req->wb_index + 1;
521 BUG_ON(!NFS_WBACK_BUSY(req));
523 atomic_inc(&req->wb_count);
524 spin_unlock(&nfsi->req_lock);
525 error = nfs_wait_on_request(req);
526 nfs_release_request(req);
529 spin_lock(&nfsi->req_lock);
532 spin_unlock(&nfsi->req_lock);
537 * nfs_scan_dirty - Scan an inode for dirty requests
538 * @inode: NFS inode to scan
539 * @dst: destination list
540 * @idx_start: lower bound of page->index to scan.
541 * @npages: idx_start + npages sets the upper bound to scan.
543 * Moves requests from the inode's dirty page list.
544 * The requests are *not* checked to ensure that they form a contiguous set.
547 nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
549 struct nfs_inode *nfsi = NFS_I(inode);
552 if (nfsi->ndirty != 0) {
553 res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages);
555 sub_page_state(nr_dirty,res);
556 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
557 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
562 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
564 * nfs_scan_commit - Scan an inode for commit requests
565 * @inode: NFS inode to scan
566 * @dst: destination list
567 * @idx_start: lower bound of page->index to scan.
568 * @npages: idx_start + npages sets the upper bound to scan.
570 * Moves requests from the inode's 'commit' request list.
571 * The requests are *not* checked to ensure that they form a contiguous set.
574 nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
576 struct nfs_inode *nfsi = NFS_I(inode);
579 if (nfsi->ncommit != 0) {
580 res = nfs_scan_list(&nfsi->commit, dst, idx_start, npages);
581 nfsi->ncommit -= res;
582 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
583 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
589 static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
591 struct backing_dev_info *bdi = mapping->backing_dev_info;
597 if (!bdi_write_congested(bdi))
600 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
603 rpc_clnt_sigmask(clnt, &oldset);
604 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
605 if (bdi_write_congested(bdi)) {
611 rpc_clnt_sigunmask(clnt, &oldset);
613 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
614 if (bdi_write_congested(bdi))
617 finish_wait(&nfs_write_congestion, &wait);
623 * Try to update any existing write request, or create one if there is none.
624 * In order to match, the request's credentials must match those of
625 * the calling process.
627 * Note: Should always be called with the Page Lock held!
629 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
630 struct inode *inode, struct page *page,
631 unsigned int offset, unsigned int bytes)
633 struct nfs_server *server = NFS_SERVER(inode);
634 struct nfs_inode *nfsi = NFS_I(inode);
635 struct nfs_page *req, *new = NULL;
636 unsigned long rqend, end;
638 end = offset + bytes;
640 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
641 return ERR_PTR(-ERESTARTSYS);
643 /* Loop over all inode entries and see if we find
644 * A request for the page we wish to update
646 spin_lock(&nfsi->req_lock);
647 req = _nfs_find_request(inode, page->index);
649 if (!nfs_lock_request_dontget(req)) {
651 spin_unlock(&nfsi->req_lock);
652 error = nfs_wait_on_request(req);
653 nfs_release_request(req);
656 nfs_release_request(new);
657 return ERR_PTR(error);
661 spin_unlock(&nfsi->req_lock);
663 nfs_release_request(new);
669 nfs_lock_request_dontget(new);
670 error = nfs_inode_add_request(inode, new);
672 spin_unlock(&nfsi->req_lock);
673 nfs_unlock_request(new);
674 return ERR_PTR(error);
676 spin_unlock(&nfsi->req_lock);
677 nfs_mark_request_dirty(new);
680 spin_unlock(&nfsi->req_lock);
682 new = nfs_create_request(ctx, inode, page, offset, bytes);
687 /* We have a request for our page.
688 * If the creds don't match, or the
689 * page addresses don't match,
690 * tell the caller to wait on the conflicting
693 rqend = req->wb_offset + req->wb_bytes;
694 if (req->wb_context != ctx
695 || req->wb_page != page
696 || !nfs_dirty_request(req)
697 || offset > rqend || end < req->wb_offset) {
698 nfs_unlock_request(req);
699 return ERR_PTR(-EBUSY);
702 /* Okay, the request matches. Update the region */
703 if (offset < req->wb_offset) {
704 req->wb_offset = offset;
705 req->wb_pgbase = offset;
706 req->wb_bytes = rqend - req->wb_offset;
710 req->wb_bytes = end - req->wb_offset;
715 int nfs_flush_incompatible(struct file *file, struct page *page)
717 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
718 struct inode *inode = page->mapping->host;
719 struct nfs_page *req;
722 * Look for a request corresponding to this page. If there
723 * is one, and it belongs to another file, we flush it out
724 * before we try to copy anything into the page. Do this
725 * due to the lack of an ACCESS-type call in NFSv2.
726 * Also do the same if we find a request from an existing
729 req = nfs_find_request(inode, page->index);
731 if (req->wb_page != page || ctx != req->wb_context)
732 status = nfs_wb_page(inode, page);
733 nfs_release_request(req);
735 return (status < 0) ? status : 0;
739 * Update and possibly write a cached page of an NFS file.
741 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
742 * things with a page scheduled for an RPC call (e.g. invalidate it).
744 int nfs_updatepage(struct file *file, struct page *page,
745 unsigned int offset, unsigned int count)
747 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
748 struct inode *inode = page->mapping->host;
749 struct nfs_page *req;
752 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
753 file->f_dentry->d_parent->d_name.name,
754 file->f_dentry->d_name.name, count,
755 (long long)(page_offset(page) +offset));
757 if (IS_SYNC(inode)) {
758 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
760 if (offset == 0 && status == PAGE_CACHE_SIZE)
761 SetPageUptodate(page);
767 /* If we're not using byte range locks, and we know the page
768 * is entirely in cache, it may be more efficient to avoid
769 * fragmenting write requests.
771 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
772 loff_t end_offs = i_size_read(inode) - 1;
773 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
777 if (unlikely(end_offs < 0)) {
779 } else if (page->index == end_index) {
781 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
784 } else if (page->index < end_index)
785 count = PAGE_CACHE_SIZE;
789 * Try to find an NFS request corresponding to this page
791 * If the existing request cannot be updated, we must flush
795 req = nfs_update_request(ctx, inode, page, offset, count);
796 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
797 if (status != -EBUSY)
799 /* Request could not be updated. Flush it out and try again */
800 status = nfs_wb_page(inode, page);
801 } while (status >= 0);
807 /* Update file length */
808 nfs_grow_file(page, offset, count);
809 /* Set the PG_uptodate flag? */
810 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
811 nfs_unlock_request(req);
813 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
814 status, (long long)i_size_read(inode));
816 ClearPageUptodate(page);
820 static void nfs_writepage_release(struct nfs_page *req)
822 end_page_writeback(req->wb_page);
824 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
825 if (!PageError(req->wb_page)) {
826 if (NFS_NEED_RESCHED(req)) {
827 nfs_mark_request_dirty(req);
829 } else if (NFS_NEED_COMMIT(req)) {
830 nfs_mark_request_commit(req);
834 nfs_inode_remove_request(req);
837 nfs_clear_commit(req);
838 nfs_clear_reschedule(req);
840 nfs_inode_remove_request(req);
842 nfs_clear_page_writeback(req);
845 static inline int flush_task_priority(int how)
847 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
849 return RPC_PRIORITY_HIGH;
851 return RPC_PRIORITY_LOW;
853 return RPC_PRIORITY_NORMAL;
857 * Set up the argument/result storage required for the RPC call.
859 static void nfs_write_rpcsetup(struct nfs_page *req,
860 struct nfs_write_data *data,
861 unsigned int count, unsigned int offset,
866 /* Set up the RPC argument and reply structs
867 * NB: take care not to mess about with data->commit et al. */
870 data->inode = inode = req->wb_context->dentry->d_inode;
871 data->cred = req->wb_context->cred;
873 data->args.fh = NFS_FH(inode);
874 data->args.offset = req_offset(req) + offset;
875 data->args.pgbase = req->wb_pgbase + offset;
876 data->args.pages = data->pagevec;
877 data->args.count = count;
878 data->args.context = req->wb_context;
880 data->res.fattr = &data->fattr;
881 data->res.count = count;
882 data->res.verf = &data->verf;
883 nfs_fattr_init(&data->fattr);
885 NFS_PROTO(inode)->write_setup(data, how);
887 data->task.tk_priority = flush_task_priority(how);
888 data->task.tk_cookie = (unsigned long)inode;
890 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
893 (long long)NFS_FILEID(inode),
895 (unsigned long long)data->args.offset);
898 static void nfs_execute_write(struct nfs_write_data *data)
900 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
903 rpc_clnt_sigmask(clnt, &oldset);
905 rpc_execute(&data->task);
907 rpc_clnt_sigunmask(clnt, &oldset);
911 * Generate multiple small requests to write out a single
912 * contiguous dirty area on one page.
914 static int nfs_flush_multi(struct list_head *head, struct inode *inode, int how)
916 struct nfs_page *req = nfs_list_entry(head->next);
917 struct page *page = req->wb_page;
918 struct nfs_write_data *data;
919 unsigned int wsize = NFS_SERVER(inode)->wsize;
920 unsigned int nbytes, offset;
924 nfs_list_remove_request(req);
926 nbytes = req->wb_bytes;
928 data = nfs_writedata_alloc(1);
931 list_add(&data->pages, &list);
937 atomic_set(&req->wb_complete, requests);
939 ClearPageError(page);
940 set_page_writeback(page);
942 nbytes = req->wb_bytes;
944 data = list_entry(list.next, struct nfs_write_data, pages);
945 list_del_init(&data->pages);
947 data->pagevec[0] = page;
948 data->complete = nfs_writeback_done_partial;
950 if (nbytes > wsize) {
951 nfs_write_rpcsetup(req, data, wsize, offset, how);
955 nfs_write_rpcsetup(req, data, nbytes, offset, how);
958 nfs_execute_write(data);
959 } while (nbytes != 0);
964 while (!list_empty(&list)) {
965 data = list_entry(list.next, struct nfs_write_data, pages);
966 list_del(&data->pages);
967 nfs_writedata_free(data);
969 nfs_mark_request_dirty(req);
970 nfs_clear_page_writeback(req);
975 * Create an RPC task for the given write request and kick it.
976 * The page must have been locked by the caller.
978 * It may happen that the page we're passed is not marked dirty.
979 * This is the case if nfs_updatepage detects a conflicting request
980 * that has been written but not committed.
982 static int nfs_flush_one(struct list_head *head, struct inode *inode, int how)
984 struct nfs_page *req;
986 struct nfs_write_data *data;
989 if (NFS_SERVER(inode)->wsize < PAGE_CACHE_SIZE)
990 return nfs_flush_multi(head, inode, how);
992 data = nfs_writedata_alloc(NFS_SERVER(inode)->wpages);
996 pages = data->pagevec;
998 while (!list_empty(head)) {
999 req = nfs_list_entry(head->next);
1000 nfs_list_remove_request(req);
1001 nfs_list_add_request(req, &data->pages);
1002 ClearPageError(req->wb_page);
1003 set_page_writeback(req->wb_page);
1004 *pages++ = req->wb_page;
1005 count += req->wb_bytes;
1007 req = nfs_list_entry(data->pages.next);
1009 data->complete = nfs_writeback_done_full;
1010 /* Set up the argument struct */
1011 nfs_write_rpcsetup(req, data, count, 0, how);
1013 nfs_execute_write(data);
1016 while (!list_empty(head)) {
1017 struct nfs_page *req = nfs_list_entry(head->next);
1018 nfs_list_remove_request(req);
1019 nfs_mark_request_dirty(req);
1020 nfs_clear_page_writeback(req);
1026 nfs_flush_list(struct list_head *head, int wpages, int how)
1028 LIST_HEAD(one_request);
1029 struct nfs_page *req;
1031 unsigned int pages = 0;
1033 while (!list_empty(head)) {
1034 pages += nfs_coalesce_requests(head, &one_request, wpages);
1035 req = nfs_list_entry(one_request.next);
1036 error = nfs_flush_one(&one_request, req->wb_context->dentry->d_inode, how);
1043 while (!list_empty(head)) {
1044 req = nfs_list_entry(head->next);
1045 nfs_list_remove_request(req);
1046 nfs_mark_request_dirty(req);
1047 nfs_clear_page_writeback(req);
1053 * Handle a write reply that flushed part of a page.
1055 static void nfs_writeback_done_partial(struct nfs_write_data *data, int status)
1057 struct nfs_page *req = data->req;
1058 struct page *page = req->wb_page;
1060 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1061 req->wb_context->dentry->d_inode->i_sb->s_id,
1062 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1064 (long long)req_offset(req));
1067 ClearPageUptodate(page);
1069 req->wb_context->error = status;
1070 dprintk(", error = %d\n", status);
1072 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1073 if (data->verf.committed < NFS_FILE_SYNC) {
1074 if (!NFS_NEED_COMMIT(req)) {
1075 nfs_defer_commit(req);
1076 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1077 dprintk(" defer commit\n");
1078 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1079 nfs_defer_reschedule(req);
1080 dprintk(" server reboot detected\n");
1087 if (atomic_dec_and_test(&req->wb_complete))
1088 nfs_writepage_release(req);
1092 * Handle a write reply that flushes a whole page.
1094 * FIXME: There is an inherent race with invalidate_inode_pages and
1095 * writebacks since the page->count is kept > 1 for as long
1096 * as the page has a write request pending.
1098 static void nfs_writeback_done_full(struct nfs_write_data *data, int status)
1100 struct nfs_page *req;
1103 /* Update attributes as result of writeback. */
1104 while (!list_empty(&data->pages)) {
1105 req = nfs_list_entry(data->pages.next);
1106 nfs_list_remove_request(req);
1107 page = req->wb_page;
1109 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1110 req->wb_context->dentry->d_inode->i_sb->s_id,
1111 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1113 (long long)req_offset(req));
1116 ClearPageUptodate(page);
1118 req->wb_context->error = status;
1119 end_page_writeback(page);
1120 nfs_inode_remove_request(req);
1121 dprintk(", error = %d\n", status);
1124 end_page_writeback(page);
1126 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1127 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1128 nfs_inode_remove_request(req);
1132 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1133 nfs_mark_request_commit(req);
1134 dprintk(" marked for commit\n");
1136 nfs_inode_remove_request(req);
1139 nfs_clear_page_writeback(req);
1144 * This function is called when the WRITE call is complete.
1146 void nfs_writeback_done(struct rpc_task *task, void *calldata)
1148 struct nfs_write_data *data = calldata;
1149 struct nfs_writeargs *argp = &data->args;
1150 struct nfs_writeres *resp = &data->res;
1152 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1153 task->tk_pid, task->tk_status);
1155 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1156 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1157 /* We tried a write call, but the server did not
1158 * commit data to stable storage even though we
1160 * Note: There is a known bug in Tru64 < 5.0 in which
1161 * the server reports NFS_DATA_SYNC, but performs
1162 * NFS_FILE_SYNC. We therefore implement this checking
1163 * as a dprintk() in order to avoid filling syslog.
1165 static unsigned long complain;
1167 if (time_before(complain, jiffies)) {
1168 dprintk("NFS: faulty NFS server %s:"
1169 " (committed = %d) != (stable = %d)\n",
1170 NFS_SERVER(data->inode)->hostname,
1171 resp->verf->committed, argp->stable);
1172 complain = jiffies + 300 * HZ;
1176 /* Is this a short write? */
1177 if (task->tk_status >= 0 && resp->count < argp->count) {
1178 static unsigned long complain;
1180 /* Has the server at least made some progress? */
1181 if (resp->count != 0) {
1182 /* Was this an NFSv2 write or an NFSv3 stable write? */
1183 if (resp->verf->committed != NFS_UNSTABLE) {
1184 /* Resend from where the server left off */
1185 argp->offset += resp->count;
1186 argp->pgbase += resp->count;
1187 argp->count -= resp->count;
1189 /* Resend as a stable write in order to avoid
1190 * headaches in the case of a server crash.
1192 argp->stable = NFS_FILE_SYNC;
1194 rpc_restart_call(task);
1197 if (time_before(complain, jiffies)) {
1199 "NFS: Server wrote zero bytes, expected %u.\n",
1201 complain = jiffies + 300 * HZ;
1203 /* Can't do anything about it except throw an error. */
1204 task->tk_status = -EIO;
1208 * Process the nfs_page list
1210 data->complete(data, task->tk_status);
1214 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1215 void nfs_commit_release(void *wdata)
1217 nfs_commit_free(wdata);
1221 * Set up the argument/result storage required for the RPC call.
1223 static void nfs_commit_rpcsetup(struct list_head *head,
1224 struct nfs_write_data *data, int how)
1226 struct nfs_page *first;
1227 struct inode *inode;
1229 /* Set up the RPC argument and reply structs
1230 * NB: take care not to mess about with data->commit et al. */
1232 list_splice_init(head, &data->pages);
1233 first = nfs_list_entry(data->pages.next);
1234 inode = first->wb_context->dentry->d_inode;
1236 data->inode = inode;
1237 data->cred = first->wb_context->cred;
1239 data->args.fh = NFS_FH(data->inode);
1240 /* Note: we always request a commit of the entire inode */
1241 data->args.offset = 0;
1242 data->args.count = 0;
1243 data->res.count = 0;
1244 data->res.fattr = &data->fattr;
1245 data->res.verf = &data->verf;
1246 nfs_fattr_init(&data->fattr);
1248 NFS_PROTO(inode)->commit_setup(data, how);
1250 data->task.tk_priority = flush_task_priority(how);
1251 data->task.tk_cookie = (unsigned long)inode;
1253 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
1257 * Commit dirty pages
1260 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1262 struct nfs_write_data *data;
1263 struct nfs_page *req;
1265 data = nfs_commit_alloc(NFS_SERVER(inode)->wpages);
1270 /* Set up the argument struct */
1271 nfs_commit_rpcsetup(head, data, how);
1273 nfs_execute_write(data);
1276 while (!list_empty(head)) {
1277 req = nfs_list_entry(head->next);
1278 nfs_list_remove_request(req);
1279 nfs_mark_request_commit(req);
1280 nfs_clear_page_writeback(req);
1286 * COMMIT call returned
1288 void nfs_commit_done(struct rpc_task *task, void *calldata)
1290 struct nfs_write_data *data = calldata;
1291 struct nfs_page *req;
1294 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1295 task->tk_pid, task->tk_status);
1297 while (!list_empty(&data->pages)) {
1298 req = nfs_list_entry(data->pages.next);
1299 nfs_list_remove_request(req);
1301 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1302 req->wb_context->dentry->d_inode->i_sb->s_id,
1303 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1305 (long long)req_offset(req));
1306 if (task->tk_status < 0) {
1307 req->wb_context->error = task->tk_status;
1308 nfs_inode_remove_request(req);
1309 dprintk(", error = %d\n", task->tk_status);
1313 /* Okay, COMMIT succeeded, apparently. Check the verifier
1314 * returned by the server against all stored verfs. */
1315 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1316 /* We have a match */
1317 nfs_inode_remove_request(req);
1321 /* We have a mismatch. Write the page again */
1322 dprintk(" mismatch\n");
1323 nfs_mark_request_dirty(req);
1325 nfs_clear_page_writeback(req);
1328 sub_page_state(nr_unstable,res);
1332 static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
1333 unsigned int npages, int how)
1335 struct nfs_inode *nfsi = NFS_I(inode);
1340 spin_lock(&nfsi->req_lock);
1341 res = nfs_scan_dirty(inode, &head, idx_start, npages);
1342 spin_unlock(&nfsi->req_lock);
1344 struct nfs_server *server = NFS_SERVER(inode);
1346 /* For single writes, FLUSH_STABLE is more efficient */
1347 if (res == nfsi->npages && nfsi->npages <= server->wpages) {
1348 if (res > 1 || nfs_list_entry(head.next)->wb_bytes <= server->wsize)
1349 how |= FLUSH_STABLE;
1351 error = nfs_flush_list(&head, server->wpages, how);
1358 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1359 int nfs_commit_inode(struct inode *inode, int how)
1361 struct nfs_inode *nfsi = NFS_I(inode);
1366 spin_lock(&nfsi->req_lock);
1367 res = nfs_scan_commit(inode, &head, 0, 0);
1368 spin_unlock(&nfsi->req_lock);
1370 error = nfs_commit_list(inode, &head, how);
1378 int nfs_sync_inode(struct inode *inode, unsigned long idx_start,
1379 unsigned int npages, int how)
1381 int nocommit = how & FLUSH_NOCOMMIT;
1382 int wait = how & FLUSH_WAIT;
1385 how &= ~(FLUSH_WAIT|FLUSH_NOCOMMIT);
1389 error = nfs_wait_on_requests(inode, idx_start, npages);
1393 error = nfs_flush_inode(inode, idx_start, npages, how);
1397 error = nfs_commit_inode(inode, how);
1398 } while (error > 0);
1402 int nfs_init_writepagecache(void)
1404 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1405 sizeof(struct nfs_write_data),
1406 0, SLAB_HWCACHE_ALIGN,
1408 if (nfs_wdata_cachep == NULL)
1411 nfs_wdata_mempool = mempool_create(MIN_POOL_WRITE,
1415 if (nfs_wdata_mempool == NULL)
1418 nfs_commit_mempool = mempool_create(MIN_POOL_COMMIT,
1422 if (nfs_commit_mempool == NULL)
1428 void nfs_destroy_writepagecache(void)
1430 mempool_destroy(nfs_commit_mempool);
1431 mempool_destroy(nfs_wdata_mempool);
1432 if (kmem_cache_destroy(nfs_wdata_cachep))
1433 printk(KERN_INFO "nfs_write_data: not all structures were freed\n");