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/types.h>
50 #include <linux/slab.h>
52 #include <linux/pagemap.h>
53 #include <linux/file.h>
54 #include <linux/mpage.h>
55 #include <linux/writeback.h>
57 #include <linux/sunrpc/clnt.h>
58 #include <linux/nfs_fs.h>
59 #include <linux/nfs_mount.h>
60 #include <linux/nfs_page.h>
61 #include <asm/uaccess.h>
62 #include <linux/smp_lock.h>
64 #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 int nfs_wait_on_write_congestion(struct address_space *, int);
80 static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
81 static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
82 unsigned int npages, int how);
83 static const struct rpc_call_ops nfs_write_partial_ops;
84 static const struct rpc_call_ops nfs_write_full_ops;
85 static const struct rpc_call_ops nfs_commit_ops;
87 static kmem_cache_t *nfs_wdata_cachep;
88 static mempool_t *nfs_wdata_mempool;
89 static mempool_t *nfs_commit_mempool;
91 static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
93 struct nfs_write_data *nfs_commit_alloc(unsigned int pagecount)
95 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
98 memset(p, 0, sizeof(*p));
99 INIT_LIST_HEAD(&p->pages);
100 if (pagecount <= ARRAY_SIZE(p->page_array))
101 p->pagevec = p->page_array;
103 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
105 mempool_free(p, nfs_commit_mempool);
113 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 struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
122 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
125 memset(p, 0, sizeof(*p));
126 INIT_LIST_HEAD(&p->pages);
127 if (pagecount <= ARRAY_SIZE(p->page_array))
128 p->pagevec = p->page_array;
130 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
132 mempool_free(p, nfs_wdata_mempool);
140 void nfs_writedata_free(struct nfs_write_data *p)
142 if (p && (p->pagevec != &p->page_array[0]))
144 mempool_free(p, nfs_wdata_mempool);
147 void nfs_writedata_release(void *wdata)
149 nfs_writedata_free(wdata);
152 /* Adjust the file length if we're writing beyond the end */
153 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
155 struct inode *inode = page->mapping->host;
156 loff_t end, i_size = i_size_read(inode);
157 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
159 if (i_size > 0 && page->index < end_index)
161 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
164 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
165 i_size_write(inode, end);
168 /* We can set the PG_uptodate flag if we see that a write request
169 * covers the full page.
171 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
175 if (PageUptodate(page))
179 if (count == PAGE_CACHE_SIZE) {
180 SetPageUptodate(page);
184 end_offs = i_size_read(page->mapping->host) - 1;
187 /* Is this the last page? */
188 if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
190 /* This is the last page: set PG_uptodate if we cover the entire
191 * extent of the data, then zero the rest of the page.
193 if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
194 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
195 SetPageUptodate(page);
200 * Write a page synchronously.
201 * Offset is the data offset within the page.
203 static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
204 struct page *page, unsigned int offset, unsigned int count,
207 unsigned int wsize = NFS_SERVER(inode)->wsize;
208 int result, written = 0;
209 struct nfs_write_data *wdata;
211 wdata = nfs_writedata_alloc(1);
216 wdata->cred = ctx->cred;
217 wdata->inode = inode;
218 wdata->args.fh = NFS_FH(inode);
219 wdata->args.context = ctx;
220 wdata->args.pages = &page;
221 wdata->args.stable = NFS_FILE_SYNC;
222 wdata->args.pgbase = offset;
223 wdata->args.count = wsize;
224 wdata->res.fattr = &wdata->fattr;
225 wdata->res.verf = &wdata->verf;
227 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
229 (long long)NFS_FILEID(inode),
230 count, (long long)(page_offset(page) + offset));
232 set_page_writeback(page);
233 nfs_begin_data_update(inode);
236 wdata->args.count = count;
237 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
239 result = NFS_PROTO(inode)->write(wdata);
242 /* Must mark the page invalid after I/O error */
243 ClearPageUptodate(page);
246 if (result < wdata->args.count)
247 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
248 wdata->args.count, result);
250 wdata->args.offset += result;
251 wdata->args.pgbase += result;
254 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
256 /* Update file length */
257 nfs_grow_file(page, offset, written);
258 /* Set the PG_uptodate flag? */
259 nfs_mark_uptodate(page, offset, written);
262 ClearPageError(page);
265 nfs_end_data_update(inode);
266 end_page_writeback(page);
267 nfs_writedata_free(wdata);
268 return written ? written : result;
271 static int nfs_writepage_async(struct nfs_open_context *ctx,
272 struct inode *inode, struct page *page,
273 unsigned int offset, unsigned int count)
275 struct nfs_page *req;
277 req = nfs_update_request(ctx, inode, page, offset, count);
280 /* Update file length */
281 nfs_grow_file(page, offset, count);
282 /* Set the PG_uptodate flag? */
283 nfs_mark_uptodate(page, offset, count);
284 nfs_unlock_request(req);
288 static int wb_priority(struct writeback_control *wbc)
290 if (wbc->for_reclaim)
291 return FLUSH_HIGHPRI;
292 if (wbc->for_kupdate)
298 * Write an mmapped page to the server.
300 int nfs_writepage(struct page *page, struct writeback_control *wbc)
302 struct nfs_open_context *ctx;
303 struct inode *inode = page->mapping->host;
304 unsigned long end_index;
305 unsigned offset = PAGE_CACHE_SIZE;
306 loff_t i_size = i_size_read(inode);
307 int inode_referenced = 0;
308 int priority = wb_priority(wbc);
311 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
312 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
315 * Note: We need to ensure that we have a reference to the inode
316 * if we are to do asynchronous writes. If not, waiting
317 * in nfs_wait_on_request() may deadlock with clear_inode().
319 * If igrab() fails here, then it is in any case safe to
320 * call nfs_wb_page(), since there will be no pending writes.
322 if (igrab(inode) != 0)
323 inode_referenced = 1;
324 end_index = i_size >> PAGE_CACHE_SHIFT;
326 /* Ensure we've flushed out any previous writes */
327 nfs_wb_page_priority(inode, page, priority);
330 if (page->index < end_index)
332 /* things got complicated... */
333 offset = i_size & (PAGE_CACHE_SIZE-1);
335 /* OK, are we completely out? */
336 err = 0; /* potential race with truncate - ignore */
337 if (page->index >= end_index+1 || !offset)
340 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
346 if (!IS_SYNC(inode) && inode_referenced) {
347 err = nfs_writepage_async(ctx, inode, page, 0, offset);
348 if (!wbc->for_writepages)
349 nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
351 err = nfs_writepage_sync(ctx, inode, page, 0,
355 redirty_page_for_writepage(wbc, page);
360 put_nfs_open_context(ctx);
363 if (inode_referenced)
369 * Note: causes nfs_update_request() to block on the assumption
370 * that the writeback is generated due to memory pressure.
372 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
374 struct backing_dev_info *bdi = mapping->backing_dev_info;
375 struct inode *inode = mapping->host;
378 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
380 err = generic_writepages(mapping, wbc);
383 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
384 if (wbc->nonblocking)
386 nfs_wait_on_write_congestion(mapping, 0);
388 err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
391 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
392 wbc->nr_to_write -= err;
393 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
394 err = nfs_wait_on_requests(inode, 0, 0);
398 err = nfs_commit_inode(inode, wb_priority(wbc));
400 wbc->nr_to_write -= err;
404 clear_bit(BDI_write_congested, &bdi->state);
405 wake_up_all(&nfs_write_congestion);
410 * Insert a write request into an inode
412 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
414 struct nfs_inode *nfsi = NFS_I(inode);
417 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
418 BUG_ON(error == -EEXIST);
423 nfs_begin_data_update(inode);
424 if (nfs_have_delegation(inode, FMODE_WRITE))
427 SetPagePrivate(req->wb_page);
429 atomic_inc(&req->wb_count);
434 * Insert a write request into an inode
436 static void nfs_inode_remove_request(struct nfs_page *req)
438 struct inode *inode = req->wb_context->dentry->d_inode;
439 struct nfs_inode *nfsi = NFS_I(inode);
441 BUG_ON (!NFS_WBACK_BUSY(req));
443 spin_lock(&nfsi->req_lock);
444 ClearPagePrivate(req->wb_page);
445 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
448 spin_unlock(&nfsi->req_lock);
449 nfs_end_data_update(inode);
452 spin_unlock(&nfsi->req_lock);
453 nfs_clear_request(req);
454 nfs_release_request(req);
460 static inline struct nfs_page *
461 _nfs_find_request(struct inode *inode, unsigned long index)
463 struct nfs_inode *nfsi = NFS_I(inode);
464 struct nfs_page *req;
466 req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
468 atomic_inc(&req->wb_count);
472 static struct nfs_page *
473 nfs_find_request(struct inode *inode, unsigned long index)
475 struct nfs_page *req;
476 struct nfs_inode *nfsi = NFS_I(inode);
478 spin_lock(&nfsi->req_lock);
479 req = _nfs_find_request(inode, index);
480 spin_unlock(&nfsi->req_lock);
485 * Add a request to the inode's dirty list.
488 nfs_mark_request_dirty(struct nfs_page *req)
490 struct inode *inode = req->wb_context->dentry->d_inode;
491 struct nfs_inode *nfsi = NFS_I(inode);
493 spin_lock(&nfsi->req_lock);
494 radix_tree_tag_set(&nfsi->nfs_page_tree,
495 req->wb_index, NFS_PAGE_TAG_DIRTY);
496 nfs_list_add_request(req, &nfsi->dirty);
498 spin_unlock(&nfsi->req_lock);
499 inc_page_state(nr_dirty);
500 mark_inode_dirty(inode);
504 * Check if a request is dirty
507 nfs_dirty_request(struct nfs_page *req)
509 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
510 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
513 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
515 * Add a request to the inode's commit list.
518 nfs_mark_request_commit(struct nfs_page *req)
520 struct inode *inode = req->wb_context->dentry->d_inode;
521 struct nfs_inode *nfsi = NFS_I(inode);
523 spin_lock(&nfsi->req_lock);
524 nfs_list_add_request(req, &nfsi->commit);
526 spin_unlock(&nfsi->req_lock);
527 inc_page_state(nr_unstable);
528 mark_inode_dirty(inode);
533 * Wait for a request to complete.
535 * Interruptible by signals only if mounted with intr flag.
537 static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
539 struct nfs_inode *nfsi = NFS_I(inode);
540 struct nfs_page *req;
541 unsigned long idx_end, next;
542 unsigned int res = 0;
548 idx_end = idx_start + npages - 1;
551 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
552 if (req->wb_index > idx_end)
555 next = req->wb_index + 1;
556 BUG_ON(!NFS_WBACK_BUSY(req));
558 atomic_inc(&req->wb_count);
559 spin_unlock(&nfsi->req_lock);
560 error = nfs_wait_on_request(req);
561 nfs_release_request(req);
562 spin_lock(&nfsi->req_lock);
570 static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
572 struct nfs_inode *nfsi = NFS_I(inode);
575 spin_lock(&nfsi->req_lock);
576 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
577 spin_unlock(&nfsi->req_lock);
581 static void nfs_cancel_requests(struct list_head *head)
583 struct nfs_page *req;
584 while(!list_empty(head)) {
585 req = nfs_list_entry(head->next);
586 nfs_list_remove_request(req);
587 nfs_inode_remove_request(req);
588 nfs_clear_page_writeback(req);
593 * nfs_scan_dirty - Scan an inode for dirty requests
594 * @inode: NFS inode to scan
595 * @dst: destination list
596 * @idx_start: lower bound of page->index to scan.
597 * @npages: idx_start + npages sets the upper bound to scan.
599 * Moves requests from the inode's dirty page list.
600 * The requests are *not* checked to ensure that they form a contiguous set.
603 nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
605 struct nfs_inode *nfsi = NFS_I(inode);
608 if (nfsi->ndirty != 0) {
609 res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages);
611 sub_page_state(nr_dirty,res);
612 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
613 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
618 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
620 * nfs_scan_commit - Scan an inode for commit requests
621 * @inode: NFS inode to scan
622 * @dst: destination list
623 * @idx_start: lower bound of page->index to scan.
624 * @npages: idx_start + npages sets the upper bound to scan.
626 * Moves requests from the inode's 'commit' request list.
627 * The requests are *not* checked to ensure that they form a contiguous set.
630 nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
632 struct nfs_inode *nfsi = NFS_I(inode);
635 if (nfsi->ncommit != 0) {
636 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
637 nfsi->ncommit -= res;
638 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
639 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
644 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
650 static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
652 struct backing_dev_info *bdi = mapping->backing_dev_info;
658 if (!bdi_write_congested(bdi))
661 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
664 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
667 rpc_clnt_sigmask(clnt, &oldset);
668 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
669 if (bdi_write_congested(bdi)) {
675 rpc_clnt_sigunmask(clnt, &oldset);
677 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
678 if (bdi_write_congested(bdi))
681 finish_wait(&nfs_write_congestion, &wait);
687 * Try to update any existing write request, or create one if there is none.
688 * In order to match, the request's credentials must match those of
689 * the calling process.
691 * Note: Should always be called with the Page Lock held!
693 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
694 struct inode *inode, struct page *page,
695 unsigned int offset, unsigned int bytes)
697 struct nfs_server *server = NFS_SERVER(inode);
698 struct nfs_inode *nfsi = NFS_I(inode);
699 struct nfs_page *req, *new = NULL;
700 unsigned long rqend, end;
702 end = offset + bytes;
704 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
705 return ERR_PTR(-ERESTARTSYS);
707 /* Loop over all inode entries and see if we find
708 * A request for the page we wish to update
710 spin_lock(&nfsi->req_lock);
711 req = _nfs_find_request(inode, page->index);
713 if (!nfs_lock_request_dontget(req)) {
715 spin_unlock(&nfsi->req_lock);
716 error = nfs_wait_on_request(req);
717 nfs_release_request(req);
720 nfs_release_request(new);
721 return ERR_PTR(error);
725 spin_unlock(&nfsi->req_lock);
727 nfs_release_request(new);
733 nfs_lock_request_dontget(new);
734 error = nfs_inode_add_request(inode, new);
736 spin_unlock(&nfsi->req_lock);
737 nfs_unlock_request(new);
738 return ERR_PTR(error);
740 spin_unlock(&nfsi->req_lock);
741 nfs_mark_request_dirty(new);
744 spin_unlock(&nfsi->req_lock);
746 new = nfs_create_request(ctx, inode, page, offset, bytes);
751 /* We have a request for our page.
752 * If the creds don't match, or the
753 * page addresses don't match,
754 * tell the caller to wait on the conflicting
757 rqend = req->wb_offset + req->wb_bytes;
758 if (req->wb_context != ctx
759 || req->wb_page != page
760 || !nfs_dirty_request(req)
761 || offset > rqend || end < req->wb_offset) {
762 nfs_unlock_request(req);
763 return ERR_PTR(-EBUSY);
766 /* Okay, the request matches. Update the region */
767 if (offset < req->wb_offset) {
768 req->wb_offset = offset;
769 req->wb_pgbase = offset;
770 req->wb_bytes = rqend - req->wb_offset;
774 req->wb_bytes = end - req->wb_offset;
779 int nfs_flush_incompatible(struct file *file, struct page *page)
781 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
782 struct inode *inode = page->mapping->host;
783 struct nfs_page *req;
786 * Look for a request corresponding to this page. If there
787 * is one, and it belongs to another file, we flush it out
788 * before we try to copy anything into the page. Do this
789 * due to the lack of an ACCESS-type call in NFSv2.
790 * Also do the same if we find a request from an existing
793 req = nfs_find_request(inode, page->index);
795 if (req->wb_page != page || ctx != req->wb_context)
796 status = nfs_wb_page(inode, page);
797 nfs_release_request(req);
799 return (status < 0) ? status : 0;
803 * Update and possibly write a cached page of an NFS file.
805 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
806 * things with a page scheduled for an RPC call (e.g. invalidate it).
808 int nfs_updatepage(struct file *file, struct page *page,
809 unsigned int offset, unsigned int count)
811 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
812 struct inode *inode = page->mapping->host;
813 struct nfs_page *req;
816 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
818 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
819 file->f_dentry->d_parent->d_name.name,
820 file->f_dentry->d_name.name, count,
821 (long long)(page_offset(page) +offset));
823 if (IS_SYNC(inode)) {
824 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
826 if (offset == 0 && status == PAGE_CACHE_SIZE)
827 SetPageUptodate(page);
833 /* If we're not using byte range locks, and we know the page
834 * is entirely in cache, it may be more efficient to avoid
835 * fragmenting write requests.
837 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
838 loff_t end_offs = i_size_read(inode) - 1;
839 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
843 if (unlikely(end_offs < 0)) {
845 } else if (page->index == end_index) {
847 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
850 } else if (page->index < end_index)
851 count = PAGE_CACHE_SIZE;
855 * Try to find an NFS request corresponding to this page
857 * If the existing request cannot be updated, we must flush
861 req = nfs_update_request(ctx, inode, page, offset, count);
862 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
863 if (status != -EBUSY)
865 /* Request could not be updated. Flush it out and try again */
866 status = nfs_wb_page(inode, page);
867 } while (status >= 0);
873 /* Update file length */
874 nfs_grow_file(page, offset, count);
875 /* Set the PG_uptodate flag? */
876 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
877 nfs_unlock_request(req);
879 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
880 status, (long long)i_size_read(inode));
882 ClearPageUptodate(page);
886 static void nfs_writepage_release(struct nfs_page *req)
888 end_page_writeback(req->wb_page);
890 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
891 if (!PageError(req->wb_page)) {
892 if (NFS_NEED_RESCHED(req)) {
893 nfs_mark_request_dirty(req);
895 } else if (NFS_NEED_COMMIT(req)) {
896 nfs_mark_request_commit(req);
900 nfs_inode_remove_request(req);
903 nfs_clear_commit(req);
904 nfs_clear_reschedule(req);
906 nfs_inode_remove_request(req);
908 nfs_clear_page_writeback(req);
911 static inline int flush_task_priority(int how)
913 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
915 return RPC_PRIORITY_HIGH;
917 return RPC_PRIORITY_LOW;
919 return RPC_PRIORITY_NORMAL;
923 * Set up the argument/result storage required for the RPC call.
925 static void nfs_write_rpcsetup(struct nfs_page *req,
926 struct nfs_write_data *data,
927 const struct rpc_call_ops *call_ops,
928 unsigned int count, unsigned int offset,
934 /* Set up the RPC argument and reply structs
935 * NB: take care not to mess about with data->commit et al. */
938 data->inode = inode = req->wb_context->dentry->d_inode;
939 data->cred = req->wb_context->cred;
941 data->args.fh = NFS_FH(inode);
942 data->args.offset = req_offset(req) + offset;
943 data->args.pgbase = req->wb_pgbase + offset;
944 data->args.pages = data->pagevec;
945 data->args.count = count;
946 data->args.context = req->wb_context;
948 data->res.fattr = &data->fattr;
949 data->res.count = count;
950 data->res.verf = &data->verf;
951 nfs_fattr_init(&data->fattr);
953 /* Set up the initial task struct. */
954 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
955 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
956 NFS_PROTO(inode)->write_setup(data, how);
958 data->task.tk_priority = flush_task_priority(how);
959 data->task.tk_cookie = (unsigned long)inode;
961 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
964 (long long)NFS_FILEID(inode),
966 (unsigned long long)data->args.offset);
969 static void nfs_execute_write(struct nfs_write_data *data)
971 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
974 rpc_clnt_sigmask(clnt, &oldset);
976 rpc_execute(&data->task);
978 rpc_clnt_sigunmask(clnt, &oldset);
982 * Generate multiple small requests to write out a single
983 * contiguous dirty area on one page.
985 static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
987 struct nfs_page *req = nfs_list_entry(head->next);
988 struct page *page = req->wb_page;
989 struct nfs_write_data *data;
990 unsigned int wsize = NFS_SERVER(inode)->wsize;
991 unsigned int nbytes, offset;
995 nfs_list_remove_request(req);
997 nbytes = req->wb_bytes;
999 data = nfs_writedata_alloc(1);
1002 list_add(&data->pages, &list);
1004 if (nbytes <= wsize)
1008 atomic_set(&req->wb_complete, requests);
1010 ClearPageError(page);
1011 set_page_writeback(page);
1013 nbytes = req->wb_bytes;
1015 data = list_entry(list.next, struct nfs_write_data, pages);
1016 list_del_init(&data->pages);
1018 data->pagevec[0] = page;
1020 if (nbytes > wsize) {
1021 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1022 wsize, offset, how);
1026 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1027 nbytes, offset, how);
1030 nfs_execute_write(data);
1031 } while (nbytes != 0);
1036 while (!list_empty(&list)) {
1037 data = list_entry(list.next, struct nfs_write_data, pages);
1038 list_del(&data->pages);
1039 nfs_writedata_free(data);
1041 nfs_mark_request_dirty(req);
1042 nfs_clear_page_writeback(req);
1047 * Create an RPC task for the given write request and kick it.
1048 * The page must have been locked by the caller.
1050 * It may happen that the page we're passed is not marked dirty.
1051 * This is the case if nfs_updatepage detects a conflicting request
1052 * that has been written but not committed.
1054 static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
1056 struct nfs_page *req;
1057 struct page **pages;
1058 struct nfs_write_data *data;
1061 data = nfs_writedata_alloc(NFS_SERVER(inode)->wpages);
1065 pages = data->pagevec;
1067 while (!list_empty(head)) {
1068 req = nfs_list_entry(head->next);
1069 nfs_list_remove_request(req);
1070 nfs_list_add_request(req, &data->pages);
1071 ClearPageError(req->wb_page);
1072 set_page_writeback(req->wb_page);
1073 *pages++ = req->wb_page;
1074 count += req->wb_bytes;
1076 req = nfs_list_entry(data->pages.next);
1078 /* Set up the argument struct */
1079 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
1081 nfs_execute_write(data);
1084 while (!list_empty(head)) {
1085 struct nfs_page *req = nfs_list_entry(head->next);
1086 nfs_list_remove_request(req);
1087 nfs_mark_request_dirty(req);
1088 nfs_clear_page_writeback(req);
1093 static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
1095 LIST_HEAD(one_request);
1096 int (*flush_one)(struct inode *, struct list_head *, int);
1097 struct nfs_page *req;
1098 int wpages = NFS_SERVER(inode)->wpages;
1099 int wsize = NFS_SERVER(inode)->wsize;
1102 flush_one = nfs_flush_one;
1103 if (wsize < PAGE_CACHE_SIZE)
1104 flush_one = nfs_flush_multi;
1105 /* For single writes, FLUSH_STABLE is more efficient */
1106 if (npages <= wpages && npages == NFS_I(inode)->npages
1107 && nfs_list_entry(head->next)->wb_bytes <= wsize)
1108 how |= FLUSH_STABLE;
1111 nfs_coalesce_requests(head, &one_request, wpages);
1112 req = nfs_list_entry(one_request.next);
1113 error = flush_one(inode, &one_request, how);
1116 } while (!list_empty(head));
1119 while (!list_empty(head)) {
1120 req = nfs_list_entry(head->next);
1121 nfs_list_remove_request(req);
1122 nfs_mark_request_dirty(req);
1123 nfs_clear_page_writeback(req);
1129 * Handle a write reply that flushed part of a page.
1131 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
1133 struct nfs_write_data *data = calldata;
1134 struct nfs_page *req = data->req;
1135 struct page *page = req->wb_page;
1137 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1138 req->wb_context->dentry->d_inode->i_sb->s_id,
1139 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1141 (long long)req_offset(req));
1143 if (nfs_writeback_done(task, data) != 0)
1146 if (task->tk_status < 0) {
1147 ClearPageUptodate(page);
1149 req->wb_context->error = task->tk_status;
1150 dprintk(", error = %d\n", task->tk_status);
1152 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1153 if (data->verf.committed < NFS_FILE_SYNC) {
1154 if (!NFS_NEED_COMMIT(req)) {
1155 nfs_defer_commit(req);
1156 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1157 dprintk(" defer commit\n");
1158 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1159 nfs_defer_reschedule(req);
1160 dprintk(" server reboot detected\n");
1167 if (atomic_dec_and_test(&req->wb_complete))
1168 nfs_writepage_release(req);
1171 static const struct rpc_call_ops nfs_write_partial_ops = {
1172 .rpc_call_done = nfs_writeback_done_partial,
1173 .rpc_release = nfs_writedata_release,
1177 * Handle a write reply that flushes a whole page.
1179 * FIXME: There is an inherent race with invalidate_inode_pages and
1180 * writebacks since the page->count is kept > 1 for as long
1181 * as the page has a write request pending.
1183 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1185 struct nfs_write_data *data = calldata;
1186 struct nfs_page *req;
1189 if (nfs_writeback_done(task, data) != 0)
1192 /* Update attributes as result of writeback. */
1193 while (!list_empty(&data->pages)) {
1194 req = nfs_list_entry(data->pages.next);
1195 nfs_list_remove_request(req);
1196 page = req->wb_page;
1198 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1199 req->wb_context->dentry->d_inode->i_sb->s_id,
1200 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1202 (long long)req_offset(req));
1204 if (task->tk_status < 0) {
1205 ClearPageUptodate(page);
1207 req->wb_context->error = task->tk_status;
1208 end_page_writeback(page);
1209 nfs_inode_remove_request(req);
1210 dprintk(", error = %d\n", task->tk_status);
1213 end_page_writeback(page);
1215 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1216 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1217 nfs_inode_remove_request(req);
1221 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1222 nfs_mark_request_commit(req);
1223 dprintk(" marked for commit\n");
1225 nfs_inode_remove_request(req);
1228 nfs_clear_page_writeback(req);
1232 static const struct rpc_call_ops nfs_write_full_ops = {
1233 .rpc_call_done = nfs_writeback_done_full,
1234 .rpc_release = nfs_writedata_release,
1239 * This function is called when the WRITE call is complete.
1241 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1243 struct nfs_writeargs *argp = &data->args;
1244 struct nfs_writeres *resp = &data->res;
1247 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1248 task->tk_pid, task->tk_status);
1250 /* Call the NFS version-specific code */
1251 status = NFS_PROTO(data->inode)->write_done(task, data);
1254 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1256 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1257 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1258 /* We tried a write call, but the server did not
1259 * commit data to stable storage even though we
1261 * Note: There is a known bug in Tru64 < 5.0 in which
1262 * the server reports NFS_DATA_SYNC, but performs
1263 * NFS_FILE_SYNC. We therefore implement this checking
1264 * as a dprintk() in order to avoid filling syslog.
1266 static unsigned long complain;
1268 if (time_before(complain, jiffies)) {
1269 dprintk("NFS: faulty NFS server %s:"
1270 " (committed = %d) != (stable = %d)\n",
1271 NFS_SERVER(data->inode)->hostname,
1272 resp->verf->committed, argp->stable);
1273 complain = jiffies + 300 * HZ;
1277 /* Is this a short write? */
1278 if (task->tk_status >= 0 && resp->count < argp->count) {
1279 static unsigned long complain;
1281 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1283 /* Has the server at least made some progress? */
1284 if (resp->count != 0) {
1285 /* Was this an NFSv2 write or an NFSv3 stable write? */
1286 if (resp->verf->committed != NFS_UNSTABLE) {
1287 /* Resend from where the server left off */
1288 argp->offset += resp->count;
1289 argp->pgbase += resp->count;
1290 argp->count -= resp->count;
1292 /* Resend as a stable write in order to avoid
1293 * headaches in the case of a server crash.
1295 argp->stable = NFS_FILE_SYNC;
1297 rpc_restart_call(task);
1300 if (time_before(complain, jiffies)) {
1302 "NFS: Server wrote zero bytes, expected %u.\n",
1304 complain = jiffies + 300 * HZ;
1306 /* Can't do anything about it except throw an error. */
1307 task->tk_status = -EIO;
1313 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1314 void nfs_commit_release(void *wdata)
1316 nfs_commit_free(wdata);
1320 * Set up the argument/result storage required for the RPC call.
1322 static void nfs_commit_rpcsetup(struct list_head *head,
1323 struct nfs_write_data *data,
1326 struct nfs_page *first;
1327 struct inode *inode;
1330 /* Set up the RPC argument and reply structs
1331 * NB: take care not to mess about with data->commit et al. */
1333 list_splice_init(head, &data->pages);
1334 first = nfs_list_entry(data->pages.next);
1335 inode = first->wb_context->dentry->d_inode;
1337 data->inode = inode;
1338 data->cred = first->wb_context->cred;
1340 data->args.fh = NFS_FH(data->inode);
1341 /* Note: we always request a commit of the entire inode */
1342 data->args.offset = 0;
1343 data->args.count = 0;
1344 data->res.count = 0;
1345 data->res.fattr = &data->fattr;
1346 data->res.verf = &data->verf;
1347 nfs_fattr_init(&data->fattr);
1349 /* Set up the initial task struct. */
1350 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1351 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
1352 NFS_PROTO(inode)->commit_setup(data, how);
1354 data->task.tk_priority = flush_task_priority(how);
1355 data->task.tk_cookie = (unsigned long)inode;
1357 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
1361 * Commit dirty pages
1364 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1366 struct nfs_write_data *data;
1367 struct nfs_page *req;
1369 data = nfs_commit_alloc(NFS_SERVER(inode)->wpages);
1374 /* Set up the argument struct */
1375 nfs_commit_rpcsetup(head, data, how);
1377 nfs_execute_write(data);
1380 while (!list_empty(head)) {
1381 req = nfs_list_entry(head->next);
1382 nfs_list_remove_request(req);
1383 nfs_mark_request_commit(req);
1384 nfs_clear_page_writeback(req);
1390 * COMMIT call returned
1392 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1394 struct nfs_write_data *data = calldata;
1395 struct nfs_page *req;
1398 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1399 task->tk_pid, task->tk_status);
1401 /* Call the NFS version-specific code */
1402 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1405 while (!list_empty(&data->pages)) {
1406 req = nfs_list_entry(data->pages.next);
1407 nfs_list_remove_request(req);
1409 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1410 req->wb_context->dentry->d_inode->i_sb->s_id,
1411 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1413 (long long)req_offset(req));
1414 if (task->tk_status < 0) {
1415 req->wb_context->error = task->tk_status;
1416 nfs_inode_remove_request(req);
1417 dprintk(", error = %d\n", task->tk_status);
1421 /* Okay, COMMIT succeeded, apparently. Check the verifier
1422 * returned by the server against all stored verfs. */
1423 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1424 /* We have a match */
1425 nfs_inode_remove_request(req);
1429 /* We have a mismatch. Write the page again */
1430 dprintk(" mismatch\n");
1431 nfs_mark_request_dirty(req);
1433 nfs_clear_page_writeback(req);
1436 sub_page_state(nr_unstable,res);
1439 static const struct rpc_call_ops nfs_commit_ops = {
1440 .rpc_call_done = nfs_commit_done,
1441 .rpc_release = nfs_commit_release,
1444 static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1450 static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
1451 unsigned int npages, int how)
1453 struct nfs_inode *nfsi = NFS_I(inode);
1457 spin_lock(&nfsi->req_lock);
1458 res = nfs_scan_dirty(inode, &head, idx_start, npages);
1459 spin_unlock(&nfsi->req_lock);
1461 int error = nfs_flush_list(inode, &head, res, how);
1468 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1469 int nfs_commit_inode(struct inode *inode, int how)
1471 struct nfs_inode *nfsi = NFS_I(inode);
1475 spin_lock(&nfsi->req_lock);
1476 res = nfs_scan_commit(inode, &head, 0, 0);
1477 spin_unlock(&nfsi->req_lock);
1479 int error = nfs_commit_list(inode, &head, how);
1487 int nfs_sync_inode_wait(struct inode *inode, unsigned long idx_start,
1488 unsigned int npages, int how)
1490 struct nfs_inode *nfsi = NFS_I(inode);
1492 int nocommit = how & FLUSH_NOCOMMIT;
1495 how &= ~FLUSH_NOCOMMIT;
1496 spin_lock(&nfsi->req_lock);
1498 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1501 pages = nfs_scan_dirty(inode, &head, idx_start, npages);
1503 spin_unlock(&nfsi->req_lock);
1504 if (how & FLUSH_INVALIDATE)
1505 nfs_cancel_requests(&head);
1507 ret = nfs_flush_list(inode, &head, pages, how);
1508 spin_lock(&nfsi->req_lock);
1513 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1516 if (how & FLUSH_INVALIDATE) {
1517 spin_unlock(&nfsi->req_lock);
1518 nfs_cancel_requests(&head);
1519 spin_lock(&nfsi->req_lock);
1522 pages += nfs_scan_commit(inode, &head, 0, 0);
1523 spin_unlock(&nfsi->req_lock);
1524 ret = nfs_commit_list(inode, &head, how);
1525 spin_lock(&nfsi->req_lock);
1527 spin_unlock(&nfsi->req_lock);
1531 int __init nfs_init_writepagecache(void)
1533 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1534 sizeof(struct nfs_write_data),
1535 0, SLAB_HWCACHE_ALIGN,
1537 if (nfs_wdata_cachep == NULL)
1540 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1542 if (nfs_wdata_mempool == NULL)
1545 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1547 if (nfs_commit_mempool == NULL)
1553 void nfs_destroy_writepagecache(void)
1555 mempool_destroy(nfs_commit_mempool);
1556 mempool_destroy(nfs_wdata_mempool);
1557 if (kmem_cache_destroy(nfs_wdata_cachep))
1558 printk(KERN_INFO "nfs_write_data: not all structures were freed\n");