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"
68 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
70 #define MIN_POOL_WRITE (32)
71 #define MIN_POOL_COMMIT (4)
74 * Local function declarations
76 static struct nfs_page * nfs_update_request(struct nfs_open_context*,
79 unsigned int, unsigned int);
80 static int nfs_wait_on_write_congestion(struct address_space *, int);
81 static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
82 static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
83 unsigned int npages, int how);
84 static const struct rpc_call_ops nfs_write_partial_ops;
85 static const struct rpc_call_ops nfs_write_full_ops;
86 static const struct rpc_call_ops nfs_commit_ops;
88 static kmem_cache_t *nfs_wdata_cachep;
89 static mempool_t *nfs_wdata_mempool;
90 static mempool_t *nfs_commit_mempool;
92 static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
94 struct nfs_write_data *nfs_commit_alloc(unsigned int pagecount)
96 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
99 memset(p, 0, sizeof(*p));
100 INIT_LIST_HEAD(&p->pages);
101 if (pagecount < NFS_PAGEVEC_SIZE)
102 p->pagevec = &p->page_array[0];
104 size_t size = ++pagecount * sizeof(struct page *);
105 p->pagevec = kzalloc(size, GFP_NOFS);
107 mempool_free(p, nfs_commit_mempool);
115 void nfs_commit_free(struct nfs_write_data *p)
117 if (p && (p->pagevec != &p->page_array[0]))
119 mempool_free(p, nfs_commit_mempool);
122 struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
124 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
127 memset(p, 0, sizeof(*p));
128 INIT_LIST_HEAD(&p->pages);
129 if (pagecount < NFS_PAGEVEC_SIZE)
130 p->pagevec = &p->page_array[0];
132 size_t size = ++pagecount * sizeof(struct page *);
133 p->pagevec = kmalloc(size, GFP_NOFS);
135 memset(p->pagevec, 0, size);
137 mempool_free(p, nfs_wdata_mempool);
145 void nfs_writedata_free(struct nfs_write_data *p)
147 if (p && (p->pagevec != &p->page_array[0]))
149 mempool_free(p, nfs_wdata_mempool);
152 void nfs_writedata_release(void *wdata)
154 nfs_writedata_free(wdata);
157 /* Adjust the file length if we're writing beyond the end */
158 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
160 struct inode *inode = page->mapping->host;
161 loff_t end, i_size = i_size_read(inode);
162 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
164 if (i_size > 0 && page->index < end_index)
166 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
169 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
170 i_size_write(inode, end);
173 /* We can set the PG_uptodate flag if we see that a write request
174 * covers the full page.
176 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
180 if (PageUptodate(page))
184 if (count == PAGE_CACHE_SIZE) {
185 SetPageUptodate(page);
189 end_offs = i_size_read(page->mapping->host) - 1;
192 /* Is this the last page? */
193 if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
195 /* This is the last page: set PG_uptodate if we cover the entire
196 * extent of the data, then zero the rest of the page.
198 if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
199 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
200 SetPageUptodate(page);
205 * Write a page synchronously.
206 * Offset is the data offset within the page.
208 static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
209 struct page *page, unsigned int offset, unsigned int count,
212 unsigned int wsize = NFS_SERVER(inode)->wsize;
213 int result, written = 0;
214 struct nfs_write_data *wdata;
216 wdata = nfs_writedata_alloc(1);
221 wdata->cred = ctx->cred;
222 wdata->inode = inode;
223 wdata->args.fh = NFS_FH(inode);
224 wdata->args.context = ctx;
225 wdata->args.pages = &page;
226 wdata->args.stable = NFS_FILE_SYNC;
227 wdata->args.pgbase = offset;
228 wdata->args.count = wsize;
229 wdata->res.fattr = &wdata->fattr;
230 wdata->res.verf = &wdata->verf;
232 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
234 (long long)NFS_FILEID(inode),
235 count, (long long)(page_offset(page) + offset));
237 set_page_writeback(page);
238 nfs_begin_data_update(inode);
241 wdata->args.count = count;
242 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
244 result = NFS_PROTO(inode)->write(wdata);
247 /* Must mark the page invalid after I/O error */
248 ClearPageUptodate(page);
251 if (result < wdata->args.count)
252 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
253 wdata->args.count, result);
255 wdata->args.offset += result;
256 wdata->args.pgbase += result;
259 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
261 /* Update file length */
262 nfs_grow_file(page, offset, written);
263 /* Set the PG_uptodate flag? */
264 nfs_mark_uptodate(page, offset, written);
267 ClearPageError(page);
270 nfs_end_data_update(inode);
271 end_page_writeback(page);
272 nfs_writedata_free(wdata);
273 return written ? written : result;
276 static int nfs_writepage_async(struct nfs_open_context *ctx,
277 struct inode *inode, struct page *page,
278 unsigned int offset, unsigned int count)
280 struct nfs_page *req;
282 req = nfs_update_request(ctx, inode, page, offset, count);
285 /* Update file length */
286 nfs_grow_file(page, offset, count);
287 /* Set the PG_uptodate flag? */
288 nfs_mark_uptodate(page, offset, count);
289 nfs_unlock_request(req);
293 static int wb_priority(struct writeback_control *wbc)
295 if (wbc->for_reclaim)
296 return FLUSH_HIGHPRI;
297 if (wbc->for_kupdate)
303 * Write an mmapped page to the server.
305 int nfs_writepage(struct page *page, struct writeback_control *wbc)
307 struct nfs_open_context *ctx;
308 struct inode *inode = page->mapping->host;
309 unsigned long end_index;
310 unsigned offset = PAGE_CACHE_SIZE;
311 loff_t i_size = i_size_read(inode);
312 int inode_referenced = 0;
313 int priority = wb_priority(wbc);
316 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
317 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
320 * Note: We need to ensure that we have a reference to the inode
321 * if we are to do asynchronous writes. If not, waiting
322 * in nfs_wait_on_request() may deadlock with clear_inode().
324 * If igrab() fails here, then it is in any case safe to
325 * call nfs_wb_page(), since there will be no pending writes.
327 if (igrab(inode) != 0)
328 inode_referenced = 1;
329 end_index = i_size >> PAGE_CACHE_SHIFT;
331 /* Ensure we've flushed out any previous writes */
332 nfs_wb_page_priority(inode, page, priority);
335 if (page->index < end_index)
337 /* things got complicated... */
338 offset = i_size & (PAGE_CACHE_SIZE-1);
340 /* OK, are we completely out? */
341 err = 0; /* potential race with truncate - ignore */
342 if (page->index >= end_index+1 || !offset)
345 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
351 if (!IS_SYNC(inode) && inode_referenced) {
352 err = nfs_writepage_async(ctx, inode, page, 0, offset);
353 if (!wbc->for_writepages)
354 nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
356 err = nfs_writepage_sync(ctx, inode, page, 0,
360 redirty_page_for_writepage(wbc, page);
365 put_nfs_open_context(ctx);
368 if (inode_referenced)
374 * Note: causes nfs_update_request() to block on the assumption
375 * that the writeback is generated due to memory pressure.
377 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
379 struct backing_dev_info *bdi = mapping->backing_dev_info;
380 struct inode *inode = mapping->host;
383 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
385 err = generic_writepages(mapping, wbc);
388 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
389 if (wbc->nonblocking)
391 nfs_wait_on_write_congestion(mapping, 0);
393 err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
396 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
397 wbc->nr_to_write -= err;
398 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
399 err = nfs_wait_on_requests(inode, 0, 0);
403 err = nfs_commit_inode(inode, wb_priority(wbc));
405 wbc->nr_to_write -= err;
409 clear_bit(BDI_write_congested, &bdi->state);
410 wake_up_all(&nfs_write_congestion);
415 * Insert a write request into an inode
417 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
419 struct nfs_inode *nfsi = NFS_I(inode);
422 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
423 BUG_ON(error == -EEXIST);
428 nfs_begin_data_update(inode);
429 if (nfs_have_delegation(inode, FMODE_WRITE))
432 SetPagePrivate(req->wb_page);
434 atomic_inc(&req->wb_count);
439 * Insert a write request into an inode
441 static void nfs_inode_remove_request(struct nfs_page *req)
443 struct inode *inode = req->wb_context->dentry->d_inode;
444 struct nfs_inode *nfsi = NFS_I(inode);
446 BUG_ON (!NFS_WBACK_BUSY(req));
448 spin_lock(&nfsi->req_lock);
449 ClearPagePrivate(req->wb_page);
450 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
453 spin_unlock(&nfsi->req_lock);
454 nfs_end_data_update(inode);
457 spin_unlock(&nfsi->req_lock);
458 nfs_clear_request(req);
459 nfs_release_request(req);
465 static inline struct nfs_page *
466 _nfs_find_request(struct inode *inode, unsigned long index)
468 struct nfs_inode *nfsi = NFS_I(inode);
469 struct nfs_page *req;
471 req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
473 atomic_inc(&req->wb_count);
477 static struct nfs_page *
478 nfs_find_request(struct inode *inode, unsigned long index)
480 struct nfs_page *req;
481 struct nfs_inode *nfsi = NFS_I(inode);
483 spin_lock(&nfsi->req_lock);
484 req = _nfs_find_request(inode, index);
485 spin_unlock(&nfsi->req_lock);
490 * Add a request to the inode's dirty list.
493 nfs_mark_request_dirty(struct nfs_page *req)
495 struct inode *inode = req->wb_context->dentry->d_inode;
496 struct nfs_inode *nfsi = NFS_I(inode);
498 spin_lock(&nfsi->req_lock);
499 radix_tree_tag_set(&nfsi->nfs_page_tree,
500 req->wb_index, NFS_PAGE_TAG_DIRTY);
501 nfs_list_add_request(req, &nfsi->dirty);
503 spin_unlock(&nfsi->req_lock);
504 inc_page_state(nr_dirty);
505 mark_inode_dirty(inode);
509 * Check if a request is dirty
512 nfs_dirty_request(struct nfs_page *req)
514 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
515 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
518 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
520 * Add a request to the inode's commit list.
523 nfs_mark_request_commit(struct nfs_page *req)
525 struct inode *inode = req->wb_context->dentry->d_inode;
526 struct nfs_inode *nfsi = NFS_I(inode);
528 spin_lock(&nfsi->req_lock);
529 nfs_list_add_request(req, &nfsi->commit);
531 spin_unlock(&nfsi->req_lock);
532 inc_page_state(nr_unstable);
533 mark_inode_dirty(inode);
538 * Wait for a request to complete.
540 * Interruptible by signals only if mounted with intr flag.
543 nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
545 struct nfs_inode *nfsi = NFS_I(inode);
546 struct nfs_page *req;
547 unsigned long idx_end, next;
548 unsigned int res = 0;
554 idx_end = idx_start + npages - 1;
556 spin_lock(&nfsi->req_lock);
558 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
559 if (req->wb_index > idx_end)
562 next = req->wb_index + 1;
563 BUG_ON(!NFS_WBACK_BUSY(req));
565 atomic_inc(&req->wb_count);
566 spin_unlock(&nfsi->req_lock);
567 error = nfs_wait_on_request(req);
568 nfs_release_request(req);
571 spin_lock(&nfsi->req_lock);
574 spin_unlock(&nfsi->req_lock);
579 * nfs_scan_dirty - Scan an inode for dirty requests
580 * @inode: NFS inode to scan
581 * @dst: destination list
582 * @idx_start: lower bound of page->index to scan.
583 * @npages: idx_start + npages sets the upper bound to scan.
585 * Moves requests from the inode's dirty page list.
586 * The requests are *not* checked to ensure that they form a contiguous set.
589 nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
591 struct nfs_inode *nfsi = NFS_I(inode);
594 if (nfsi->ndirty != 0) {
595 res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages);
597 sub_page_state(nr_dirty,res);
598 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
599 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
604 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
606 * nfs_scan_commit - Scan an inode for commit requests
607 * @inode: NFS inode to scan
608 * @dst: destination list
609 * @idx_start: lower bound of page->index to scan.
610 * @npages: idx_start + npages sets the upper bound to scan.
612 * Moves requests from the inode's 'commit' request list.
613 * The requests are *not* checked to ensure that they form a contiguous set.
616 nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
618 struct nfs_inode *nfsi = NFS_I(inode);
621 if (nfsi->ncommit != 0) {
622 res = nfs_scan_list(&nfsi->commit, dst, idx_start, npages);
623 nfsi->ncommit -= res;
624 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
625 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
631 static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
633 struct backing_dev_info *bdi = mapping->backing_dev_info;
639 if (!bdi_write_congested(bdi))
642 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
645 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
648 rpc_clnt_sigmask(clnt, &oldset);
649 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
650 if (bdi_write_congested(bdi)) {
656 rpc_clnt_sigunmask(clnt, &oldset);
658 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
659 if (bdi_write_congested(bdi))
662 finish_wait(&nfs_write_congestion, &wait);
668 * Try to update any existing write request, or create one if there is none.
669 * In order to match, the request's credentials must match those of
670 * the calling process.
672 * Note: Should always be called with the Page Lock held!
674 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
675 struct inode *inode, struct page *page,
676 unsigned int offset, unsigned int bytes)
678 struct nfs_server *server = NFS_SERVER(inode);
679 struct nfs_inode *nfsi = NFS_I(inode);
680 struct nfs_page *req, *new = NULL;
681 unsigned long rqend, end;
683 end = offset + bytes;
685 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
686 return ERR_PTR(-ERESTARTSYS);
688 /* Loop over all inode entries and see if we find
689 * A request for the page we wish to update
691 spin_lock(&nfsi->req_lock);
692 req = _nfs_find_request(inode, page->index);
694 if (!nfs_lock_request_dontget(req)) {
696 spin_unlock(&nfsi->req_lock);
697 error = nfs_wait_on_request(req);
698 nfs_release_request(req);
701 nfs_release_request(new);
702 return ERR_PTR(error);
706 spin_unlock(&nfsi->req_lock);
708 nfs_release_request(new);
714 nfs_lock_request_dontget(new);
715 error = nfs_inode_add_request(inode, new);
717 spin_unlock(&nfsi->req_lock);
718 nfs_unlock_request(new);
719 return ERR_PTR(error);
721 spin_unlock(&nfsi->req_lock);
722 nfs_mark_request_dirty(new);
725 spin_unlock(&nfsi->req_lock);
727 new = nfs_create_request(ctx, inode, page, offset, bytes);
732 /* We have a request for our page.
733 * If the creds don't match, or the
734 * page addresses don't match,
735 * tell the caller to wait on the conflicting
738 rqend = req->wb_offset + req->wb_bytes;
739 if (req->wb_context != ctx
740 || req->wb_page != page
741 || !nfs_dirty_request(req)
742 || offset > rqend || end < req->wb_offset) {
743 nfs_unlock_request(req);
744 return ERR_PTR(-EBUSY);
747 /* Okay, the request matches. Update the region */
748 if (offset < req->wb_offset) {
749 req->wb_offset = offset;
750 req->wb_pgbase = offset;
751 req->wb_bytes = rqend - req->wb_offset;
755 req->wb_bytes = end - req->wb_offset;
760 int nfs_flush_incompatible(struct file *file, struct page *page)
762 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
763 struct inode *inode = page->mapping->host;
764 struct nfs_page *req;
767 * Look for a request corresponding to this page. If there
768 * is one, and it belongs to another file, we flush it out
769 * before we try to copy anything into the page. Do this
770 * due to the lack of an ACCESS-type call in NFSv2.
771 * Also do the same if we find a request from an existing
774 req = nfs_find_request(inode, page->index);
776 if (req->wb_page != page || ctx != req->wb_context)
777 status = nfs_wb_page(inode, page);
778 nfs_release_request(req);
780 return (status < 0) ? status : 0;
784 * Update and possibly write a cached page of an NFS file.
786 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
787 * things with a page scheduled for an RPC call (e.g. invalidate it).
789 int nfs_updatepage(struct file *file, struct page *page,
790 unsigned int offset, unsigned int count)
792 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
793 struct inode *inode = page->mapping->host;
794 struct nfs_page *req;
797 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
799 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
800 file->f_dentry->d_parent->d_name.name,
801 file->f_dentry->d_name.name, count,
802 (long long)(page_offset(page) +offset));
804 if (IS_SYNC(inode)) {
805 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
807 if (offset == 0 && status == PAGE_CACHE_SIZE)
808 SetPageUptodate(page);
814 /* If we're not using byte range locks, and we know the page
815 * is entirely in cache, it may be more efficient to avoid
816 * fragmenting write requests.
818 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
819 loff_t end_offs = i_size_read(inode) - 1;
820 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
824 if (unlikely(end_offs < 0)) {
826 } else if (page->index == end_index) {
828 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
831 } else if (page->index < end_index)
832 count = PAGE_CACHE_SIZE;
836 * Try to find an NFS request corresponding to this page
838 * If the existing request cannot be updated, we must flush
842 req = nfs_update_request(ctx, inode, page, offset, count);
843 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
844 if (status != -EBUSY)
846 /* Request could not be updated. Flush it out and try again */
847 status = nfs_wb_page(inode, page);
848 } while (status >= 0);
854 /* Update file length */
855 nfs_grow_file(page, offset, count);
856 /* Set the PG_uptodate flag? */
857 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
858 nfs_unlock_request(req);
860 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
861 status, (long long)i_size_read(inode));
863 ClearPageUptodate(page);
867 static void nfs_writepage_release(struct nfs_page *req)
869 end_page_writeback(req->wb_page);
871 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
872 if (!PageError(req->wb_page)) {
873 if (NFS_NEED_RESCHED(req)) {
874 nfs_mark_request_dirty(req);
876 } else if (NFS_NEED_COMMIT(req)) {
877 nfs_mark_request_commit(req);
881 nfs_inode_remove_request(req);
884 nfs_clear_commit(req);
885 nfs_clear_reschedule(req);
887 nfs_inode_remove_request(req);
889 nfs_clear_page_writeback(req);
892 static inline int flush_task_priority(int how)
894 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
896 return RPC_PRIORITY_HIGH;
898 return RPC_PRIORITY_LOW;
900 return RPC_PRIORITY_NORMAL;
904 * Set up the argument/result storage required for the RPC call.
906 static void nfs_write_rpcsetup(struct nfs_page *req,
907 struct nfs_write_data *data,
908 const struct rpc_call_ops *call_ops,
909 unsigned int count, unsigned int offset,
915 /* Set up the RPC argument and reply structs
916 * NB: take care not to mess about with data->commit et al. */
919 data->inode = inode = req->wb_context->dentry->d_inode;
920 data->cred = req->wb_context->cred;
922 data->args.fh = NFS_FH(inode);
923 data->args.offset = req_offset(req) + offset;
924 data->args.pgbase = req->wb_pgbase + offset;
925 data->args.pages = data->pagevec;
926 data->args.count = count;
927 data->args.context = req->wb_context;
929 data->res.fattr = &data->fattr;
930 data->res.count = count;
931 data->res.verf = &data->verf;
932 nfs_fattr_init(&data->fattr);
934 /* Set up the initial task struct. */
935 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
936 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
937 NFS_PROTO(inode)->write_setup(data, how);
939 data->task.tk_priority = flush_task_priority(how);
940 data->task.tk_cookie = (unsigned long)inode;
942 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
945 (long long)NFS_FILEID(inode),
947 (unsigned long long)data->args.offset);
950 static void nfs_execute_write(struct nfs_write_data *data)
952 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
955 rpc_clnt_sigmask(clnt, &oldset);
957 rpc_execute(&data->task);
959 rpc_clnt_sigunmask(clnt, &oldset);
963 * Generate multiple small requests to write out a single
964 * contiguous dirty area on one page.
966 static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
968 struct nfs_page *req = nfs_list_entry(head->next);
969 struct page *page = req->wb_page;
970 struct nfs_write_data *data;
971 unsigned int wsize = NFS_SERVER(inode)->wsize;
972 unsigned int nbytes, offset;
976 nfs_list_remove_request(req);
978 nbytes = req->wb_bytes;
980 data = nfs_writedata_alloc(1);
983 list_add(&data->pages, &list);
989 atomic_set(&req->wb_complete, requests);
991 ClearPageError(page);
992 set_page_writeback(page);
994 nbytes = req->wb_bytes;
996 data = list_entry(list.next, struct nfs_write_data, pages);
997 list_del_init(&data->pages);
999 data->pagevec[0] = page;
1001 if (nbytes > wsize) {
1002 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1003 wsize, offset, how);
1007 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1008 nbytes, offset, how);
1011 nfs_execute_write(data);
1012 } while (nbytes != 0);
1017 while (!list_empty(&list)) {
1018 data = list_entry(list.next, struct nfs_write_data, pages);
1019 list_del(&data->pages);
1020 nfs_writedata_free(data);
1022 nfs_mark_request_dirty(req);
1023 nfs_clear_page_writeback(req);
1028 * Create an RPC task for the given write request and kick it.
1029 * The page must have been locked by the caller.
1031 * It may happen that the page we're passed is not marked dirty.
1032 * This is the case if nfs_updatepage detects a conflicting request
1033 * that has been written but not committed.
1035 static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
1037 struct nfs_page *req;
1038 struct page **pages;
1039 struct nfs_write_data *data;
1042 data = nfs_writedata_alloc(NFS_SERVER(inode)->wpages);
1046 pages = data->pagevec;
1048 while (!list_empty(head)) {
1049 req = nfs_list_entry(head->next);
1050 nfs_list_remove_request(req);
1051 nfs_list_add_request(req, &data->pages);
1052 ClearPageError(req->wb_page);
1053 set_page_writeback(req->wb_page);
1054 *pages++ = req->wb_page;
1055 count += req->wb_bytes;
1057 req = nfs_list_entry(data->pages.next);
1059 /* Set up the argument struct */
1060 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
1062 nfs_execute_write(data);
1065 while (!list_empty(head)) {
1066 struct nfs_page *req = nfs_list_entry(head->next);
1067 nfs_list_remove_request(req);
1068 nfs_mark_request_dirty(req);
1069 nfs_clear_page_writeback(req);
1074 static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
1076 LIST_HEAD(one_request);
1077 int (*flush_one)(struct inode *, struct list_head *, int);
1078 struct nfs_page *req;
1079 int wpages = NFS_SERVER(inode)->wpages;
1080 int wsize = NFS_SERVER(inode)->wsize;
1083 flush_one = nfs_flush_one;
1084 if (wsize < PAGE_CACHE_SIZE)
1085 flush_one = nfs_flush_multi;
1086 /* For single writes, FLUSH_STABLE is more efficient */
1087 if (npages <= wpages && npages == NFS_I(inode)->npages
1088 && nfs_list_entry(head->next)->wb_bytes <= wsize)
1089 how |= FLUSH_STABLE;
1092 nfs_coalesce_requests(head, &one_request, wpages);
1093 req = nfs_list_entry(one_request.next);
1094 error = flush_one(inode, &one_request, how);
1097 } while (!list_empty(head));
1100 while (!list_empty(head)) {
1101 req = nfs_list_entry(head->next);
1102 nfs_list_remove_request(req);
1103 nfs_mark_request_dirty(req);
1104 nfs_clear_page_writeback(req);
1110 * Handle a write reply that flushed part of a page.
1112 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
1114 struct nfs_write_data *data = calldata;
1115 struct nfs_page *req = data->req;
1116 struct page *page = req->wb_page;
1118 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1119 req->wb_context->dentry->d_inode->i_sb->s_id,
1120 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1122 (long long)req_offset(req));
1124 if (nfs_writeback_done(task, data) != 0)
1127 if (task->tk_status < 0) {
1128 ClearPageUptodate(page);
1130 req->wb_context->error = task->tk_status;
1131 dprintk(", error = %d\n", task->tk_status);
1133 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1134 if (data->verf.committed < NFS_FILE_SYNC) {
1135 if (!NFS_NEED_COMMIT(req)) {
1136 nfs_defer_commit(req);
1137 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1138 dprintk(" defer commit\n");
1139 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1140 nfs_defer_reschedule(req);
1141 dprintk(" server reboot detected\n");
1148 if (atomic_dec_and_test(&req->wb_complete))
1149 nfs_writepage_release(req);
1152 static const struct rpc_call_ops nfs_write_partial_ops = {
1153 .rpc_call_done = nfs_writeback_done_partial,
1154 .rpc_release = nfs_writedata_release,
1158 * Handle a write reply that flushes a whole page.
1160 * FIXME: There is an inherent race with invalidate_inode_pages and
1161 * writebacks since the page->count is kept > 1 for as long
1162 * as the page has a write request pending.
1164 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1166 struct nfs_write_data *data = calldata;
1167 struct nfs_page *req;
1170 if (nfs_writeback_done(task, data) != 0)
1173 /* Update attributes as result of writeback. */
1174 while (!list_empty(&data->pages)) {
1175 req = nfs_list_entry(data->pages.next);
1176 nfs_list_remove_request(req);
1177 page = req->wb_page;
1179 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1180 req->wb_context->dentry->d_inode->i_sb->s_id,
1181 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1183 (long long)req_offset(req));
1185 if (task->tk_status < 0) {
1186 ClearPageUptodate(page);
1188 req->wb_context->error = task->tk_status;
1189 end_page_writeback(page);
1190 nfs_inode_remove_request(req);
1191 dprintk(", error = %d\n", task->tk_status);
1194 end_page_writeback(page);
1196 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1197 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1198 nfs_inode_remove_request(req);
1202 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1203 nfs_mark_request_commit(req);
1204 dprintk(" marked for commit\n");
1206 nfs_inode_remove_request(req);
1209 nfs_clear_page_writeback(req);
1213 static const struct rpc_call_ops nfs_write_full_ops = {
1214 .rpc_call_done = nfs_writeback_done_full,
1215 .rpc_release = nfs_writedata_release,
1220 * This function is called when the WRITE call is complete.
1222 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1224 struct nfs_writeargs *argp = &data->args;
1225 struct nfs_writeres *resp = &data->res;
1228 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1229 task->tk_pid, task->tk_status);
1231 /* Call the NFS version-specific code */
1232 status = NFS_PROTO(data->inode)->write_done(task, data);
1235 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1237 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1238 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1239 /* We tried a write call, but the server did not
1240 * commit data to stable storage even though we
1242 * Note: There is a known bug in Tru64 < 5.0 in which
1243 * the server reports NFS_DATA_SYNC, but performs
1244 * NFS_FILE_SYNC. We therefore implement this checking
1245 * as a dprintk() in order to avoid filling syslog.
1247 static unsigned long complain;
1249 if (time_before(complain, jiffies)) {
1250 dprintk("NFS: faulty NFS server %s:"
1251 " (committed = %d) != (stable = %d)\n",
1252 NFS_SERVER(data->inode)->hostname,
1253 resp->verf->committed, argp->stable);
1254 complain = jiffies + 300 * HZ;
1258 /* Is this a short write? */
1259 if (task->tk_status >= 0 && resp->count < argp->count) {
1260 static unsigned long complain;
1262 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1264 /* Has the server at least made some progress? */
1265 if (resp->count != 0) {
1266 /* Was this an NFSv2 write or an NFSv3 stable write? */
1267 if (resp->verf->committed != NFS_UNSTABLE) {
1268 /* Resend from where the server left off */
1269 argp->offset += resp->count;
1270 argp->pgbase += resp->count;
1271 argp->count -= resp->count;
1273 /* Resend as a stable write in order to avoid
1274 * headaches in the case of a server crash.
1276 argp->stable = NFS_FILE_SYNC;
1278 rpc_restart_call(task);
1281 if (time_before(complain, jiffies)) {
1283 "NFS: Server wrote zero bytes, expected %u.\n",
1285 complain = jiffies + 300 * HZ;
1287 /* Can't do anything about it except throw an error. */
1288 task->tk_status = -EIO;
1294 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1295 void nfs_commit_release(void *wdata)
1297 nfs_commit_free(wdata);
1301 * Set up the argument/result storage required for the RPC call.
1303 static void nfs_commit_rpcsetup(struct list_head *head,
1304 struct nfs_write_data *data,
1307 struct nfs_page *first;
1308 struct inode *inode;
1311 /* Set up the RPC argument and reply structs
1312 * NB: take care not to mess about with data->commit et al. */
1314 list_splice_init(head, &data->pages);
1315 first = nfs_list_entry(data->pages.next);
1316 inode = first->wb_context->dentry->d_inode;
1318 data->inode = inode;
1319 data->cred = first->wb_context->cred;
1321 data->args.fh = NFS_FH(data->inode);
1322 /* Note: we always request a commit of the entire inode */
1323 data->args.offset = 0;
1324 data->args.count = 0;
1325 data->res.count = 0;
1326 data->res.fattr = &data->fattr;
1327 data->res.verf = &data->verf;
1328 nfs_fattr_init(&data->fattr);
1330 /* Set up the initial task struct. */
1331 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1332 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
1333 NFS_PROTO(inode)->commit_setup(data, how);
1335 data->task.tk_priority = flush_task_priority(how);
1336 data->task.tk_cookie = (unsigned long)inode;
1338 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
1342 * Commit dirty pages
1345 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1347 struct nfs_write_data *data;
1348 struct nfs_page *req;
1350 data = nfs_commit_alloc(NFS_SERVER(inode)->wpages);
1355 /* Set up the argument struct */
1356 nfs_commit_rpcsetup(head, data, how);
1358 nfs_execute_write(data);
1361 while (!list_empty(head)) {
1362 req = nfs_list_entry(head->next);
1363 nfs_list_remove_request(req);
1364 nfs_mark_request_commit(req);
1365 nfs_clear_page_writeback(req);
1371 * COMMIT call returned
1373 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1375 struct nfs_write_data *data = calldata;
1376 struct nfs_page *req;
1379 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1380 task->tk_pid, task->tk_status);
1382 /* Call the NFS version-specific code */
1383 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1386 while (!list_empty(&data->pages)) {
1387 req = nfs_list_entry(data->pages.next);
1388 nfs_list_remove_request(req);
1390 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1391 req->wb_context->dentry->d_inode->i_sb->s_id,
1392 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1394 (long long)req_offset(req));
1395 if (task->tk_status < 0) {
1396 req->wb_context->error = task->tk_status;
1397 nfs_inode_remove_request(req);
1398 dprintk(", error = %d\n", task->tk_status);
1402 /* Okay, COMMIT succeeded, apparently. Check the verifier
1403 * returned by the server against all stored verfs. */
1404 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1405 /* We have a match */
1406 nfs_inode_remove_request(req);
1410 /* We have a mismatch. Write the page again */
1411 dprintk(" mismatch\n");
1412 nfs_mark_request_dirty(req);
1414 nfs_clear_page_writeback(req);
1417 sub_page_state(nr_unstable,res);
1420 static const struct rpc_call_ops nfs_commit_ops = {
1421 .rpc_call_done = nfs_commit_done,
1422 .rpc_release = nfs_commit_release,
1426 static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
1427 unsigned int npages, int how)
1429 struct nfs_inode *nfsi = NFS_I(inode);
1433 spin_lock(&nfsi->req_lock);
1434 res = nfs_scan_dirty(inode, &head, idx_start, npages);
1435 spin_unlock(&nfsi->req_lock);
1437 int error = nfs_flush_list(inode, &head, res, how);
1444 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1445 int nfs_commit_inode(struct inode *inode, int how)
1447 struct nfs_inode *nfsi = NFS_I(inode);
1451 spin_lock(&nfsi->req_lock);
1452 res = nfs_scan_commit(inode, &head, 0, 0);
1453 spin_unlock(&nfsi->req_lock);
1455 int error = nfs_commit_list(inode, &head, how);
1463 int nfs_sync_inode(struct inode *inode, unsigned long idx_start,
1464 unsigned int npages, int how)
1466 int nocommit = how & FLUSH_NOCOMMIT;
1467 int wait = how & FLUSH_WAIT;
1470 how &= ~(FLUSH_WAIT|FLUSH_NOCOMMIT);
1474 error = nfs_wait_on_requests(inode, idx_start, npages);
1478 error = nfs_flush_inode(inode, idx_start, npages, how);
1482 error = nfs_commit_inode(inode, how);
1483 } while (error > 0);
1487 int nfs_init_writepagecache(void)
1489 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1490 sizeof(struct nfs_write_data),
1491 0, SLAB_HWCACHE_ALIGN,
1493 if (nfs_wdata_cachep == NULL)
1496 nfs_wdata_mempool = mempool_create(MIN_POOL_WRITE,
1500 if (nfs_wdata_mempool == NULL)
1503 nfs_commit_mempool = mempool_create(MIN_POOL_COMMIT,
1507 if (nfs_commit_mempool == NULL)
1513 void nfs_destroy_writepagecache(void)
1515 mempool_destroy(nfs_commit_mempool);
1516 mempool_destroy(nfs_wdata_mempool);
1517 if (kmem_cache_destroy(nfs_wdata_cachep))
1518 printk(KERN_INFO "nfs_write_data: not all structures were freed\n");