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 = kmalloc(size, GFP_NOFS);
105 memset(p->pagevec, 0, size);
107 mempool_free(p, nfs_commit_mempool);
115 static inline 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 void nfs_writedata_release(void *wdata)
124 nfs_writedata_free(wdata);
127 /* Adjust the file length if we're writing beyond the end */
128 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
130 struct inode *inode = page->mapping->host;
131 loff_t end, i_size = i_size_read(inode);
132 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
134 if (i_size > 0 && page->index < end_index)
136 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
139 i_size_write(inode, end);
142 /* We can set the PG_uptodate flag if we see that a write request
143 * covers the full page.
145 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
149 if (PageUptodate(page))
153 if (count == PAGE_CACHE_SIZE) {
154 SetPageUptodate(page);
158 end_offs = i_size_read(page->mapping->host) - 1;
161 /* Is this the last page? */
162 if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
164 /* This is the last page: set PG_uptodate if we cover the entire
165 * extent of the data, then zero the rest of the page.
167 if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
168 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
169 SetPageUptodate(page);
174 * Write a page synchronously.
175 * Offset is the data offset within the page.
177 static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
178 struct page *page, unsigned int offset, unsigned int count,
181 unsigned int wsize = NFS_SERVER(inode)->wsize;
182 int result, written = 0;
183 struct nfs_write_data *wdata;
185 wdata = nfs_writedata_alloc(1);
190 wdata->cred = ctx->cred;
191 wdata->inode = inode;
192 wdata->args.fh = NFS_FH(inode);
193 wdata->args.context = ctx;
194 wdata->args.pages = &page;
195 wdata->args.stable = NFS_FILE_SYNC;
196 wdata->args.pgbase = offset;
197 wdata->args.count = wsize;
198 wdata->res.fattr = &wdata->fattr;
199 wdata->res.verf = &wdata->verf;
201 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
203 (long long)NFS_FILEID(inode),
204 count, (long long)(page_offset(page) + offset));
206 set_page_writeback(page);
207 nfs_begin_data_update(inode);
210 wdata->args.count = count;
211 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
213 result = NFS_PROTO(inode)->write(wdata);
216 /* Must mark the page invalid after I/O error */
217 ClearPageUptodate(page);
220 if (result < wdata->args.count)
221 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
222 wdata->args.count, result);
224 wdata->args.offset += result;
225 wdata->args.pgbase += result;
229 /* Update file length */
230 nfs_grow_file(page, offset, written);
231 /* Set the PG_uptodate flag? */
232 nfs_mark_uptodate(page, offset, written);
235 ClearPageError(page);
238 nfs_end_data_update(inode);
239 end_page_writeback(page);
240 nfs_writedata_free(wdata);
241 return written ? written : result;
244 static int nfs_writepage_async(struct nfs_open_context *ctx,
245 struct inode *inode, struct page *page,
246 unsigned int offset, unsigned int count)
248 struct nfs_page *req;
250 req = nfs_update_request(ctx, inode, page, offset, count);
253 /* Update file length */
254 nfs_grow_file(page, offset, count);
255 /* Set the PG_uptodate flag? */
256 nfs_mark_uptodate(page, offset, count);
257 nfs_unlock_request(req);
261 static int wb_priority(struct writeback_control *wbc)
263 if (wbc->for_reclaim)
264 return FLUSH_HIGHPRI;
265 if (wbc->for_kupdate)
271 * Write an mmapped page to the server.
273 int nfs_writepage(struct page *page, struct writeback_control *wbc)
275 struct nfs_open_context *ctx;
276 struct inode *inode = page->mapping->host;
277 unsigned long end_index;
278 unsigned offset = PAGE_CACHE_SIZE;
279 loff_t i_size = i_size_read(inode);
280 int inode_referenced = 0;
281 int priority = wb_priority(wbc);
285 * Note: We need to ensure that we have a reference to the inode
286 * if we are to do asynchronous writes. If not, waiting
287 * in nfs_wait_on_request() may deadlock with clear_inode().
289 * If igrab() fails here, then it is in any case safe to
290 * call nfs_wb_page(), since there will be no pending writes.
292 if (igrab(inode) != 0)
293 inode_referenced = 1;
294 end_index = i_size >> PAGE_CACHE_SHIFT;
296 /* Ensure we've flushed out any previous writes */
297 nfs_wb_page_priority(inode, page, priority);
300 if (page->index < end_index)
302 /* things got complicated... */
303 offset = i_size & (PAGE_CACHE_SIZE-1);
305 /* OK, are we completely out? */
306 err = 0; /* potential race with truncate - ignore */
307 if (page->index >= end_index+1 || !offset)
310 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
316 if (!IS_SYNC(inode) && inode_referenced) {
317 err = nfs_writepage_async(ctx, inode, page, 0, offset);
318 if (!wbc->for_writepages)
319 nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
321 err = nfs_writepage_sync(ctx, inode, page, 0,
325 redirty_page_for_writepage(wbc, page);
330 put_nfs_open_context(ctx);
333 if (inode_referenced)
339 * Note: causes nfs_update_request() to block on the assumption
340 * that the writeback is generated due to memory pressure.
342 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
344 struct backing_dev_info *bdi = mapping->backing_dev_info;
345 struct inode *inode = mapping->host;
348 err = generic_writepages(mapping, wbc);
351 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
352 if (wbc->nonblocking)
354 nfs_wait_on_write_congestion(mapping, 0);
356 err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
359 wbc->nr_to_write -= err;
360 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
361 err = nfs_wait_on_requests(inode, 0, 0);
365 err = nfs_commit_inode(inode, wb_priority(wbc));
367 wbc->nr_to_write -= err;
371 clear_bit(BDI_write_congested, &bdi->state);
372 wake_up_all(&nfs_write_congestion);
377 * Insert a write request into an inode
379 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
381 struct nfs_inode *nfsi = NFS_I(inode);
384 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
385 BUG_ON(error == -EEXIST);
390 nfs_begin_data_update(inode);
391 if (nfs_have_delegation(inode, FMODE_WRITE))
395 atomic_inc(&req->wb_count);
400 * Insert a write request into an inode
402 static void nfs_inode_remove_request(struct nfs_page *req)
404 struct inode *inode = req->wb_context->dentry->d_inode;
405 struct nfs_inode *nfsi = NFS_I(inode);
407 BUG_ON (!NFS_WBACK_BUSY(req));
409 spin_lock(&nfsi->req_lock);
410 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
413 spin_unlock(&nfsi->req_lock);
414 nfs_end_data_update(inode);
417 spin_unlock(&nfsi->req_lock);
418 nfs_clear_request(req);
419 nfs_release_request(req);
425 static inline struct nfs_page *
426 _nfs_find_request(struct inode *inode, unsigned long index)
428 struct nfs_inode *nfsi = NFS_I(inode);
429 struct nfs_page *req;
431 req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
433 atomic_inc(&req->wb_count);
437 static struct nfs_page *
438 nfs_find_request(struct inode *inode, unsigned long index)
440 struct nfs_page *req;
441 struct nfs_inode *nfsi = NFS_I(inode);
443 spin_lock(&nfsi->req_lock);
444 req = _nfs_find_request(inode, index);
445 spin_unlock(&nfsi->req_lock);
450 * Add a request to the inode's dirty list.
453 nfs_mark_request_dirty(struct nfs_page *req)
455 struct inode *inode = req->wb_context->dentry->d_inode;
456 struct nfs_inode *nfsi = NFS_I(inode);
458 spin_lock(&nfsi->req_lock);
459 radix_tree_tag_set(&nfsi->nfs_page_tree,
460 req->wb_index, NFS_PAGE_TAG_DIRTY);
461 nfs_list_add_request(req, &nfsi->dirty);
463 spin_unlock(&nfsi->req_lock);
464 inc_page_state(nr_dirty);
465 mark_inode_dirty(inode);
469 * Check if a request is dirty
472 nfs_dirty_request(struct nfs_page *req)
474 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
475 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
478 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
480 * Add a request to the inode's commit list.
483 nfs_mark_request_commit(struct nfs_page *req)
485 struct inode *inode = req->wb_context->dentry->d_inode;
486 struct nfs_inode *nfsi = NFS_I(inode);
488 spin_lock(&nfsi->req_lock);
489 nfs_list_add_request(req, &nfsi->commit);
491 spin_unlock(&nfsi->req_lock);
492 inc_page_state(nr_unstable);
493 mark_inode_dirty(inode);
498 * Wait for a request to complete.
500 * Interruptible by signals only if mounted with intr flag.
503 nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
505 struct nfs_inode *nfsi = NFS_I(inode);
506 struct nfs_page *req;
507 unsigned long idx_end, next;
508 unsigned int res = 0;
514 idx_end = idx_start + npages - 1;
516 spin_lock(&nfsi->req_lock);
518 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
519 if (req->wb_index > idx_end)
522 next = req->wb_index + 1;
523 BUG_ON(!NFS_WBACK_BUSY(req));
525 atomic_inc(&req->wb_count);
526 spin_unlock(&nfsi->req_lock);
527 error = nfs_wait_on_request(req);
528 nfs_release_request(req);
531 spin_lock(&nfsi->req_lock);
534 spin_unlock(&nfsi->req_lock);
539 * nfs_scan_dirty - Scan an inode for dirty requests
540 * @inode: NFS inode to scan
541 * @dst: destination list
542 * @idx_start: lower bound of page->index to scan.
543 * @npages: idx_start + npages sets the upper bound to scan.
545 * Moves requests from the inode's dirty page list.
546 * The requests are *not* checked to ensure that they form a contiguous set.
549 nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
551 struct nfs_inode *nfsi = NFS_I(inode);
554 if (nfsi->ndirty != 0) {
555 res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages);
557 sub_page_state(nr_dirty,res);
558 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
559 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
564 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
566 * nfs_scan_commit - Scan an inode for commit requests
567 * @inode: NFS inode to scan
568 * @dst: destination list
569 * @idx_start: lower bound of page->index to scan.
570 * @npages: idx_start + npages sets the upper bound to scan.
572 * Moves requests from the inode's 'commit' request list.
573 * The requests are *not* checked to ensure that they form a contiguous set.
576 nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
578 struct nfs_inode *nfsi = NFS_I(inode);
581 if (nfsi->ncommit != 0) {
582 res = nfs_scan_list(&nfsi->commit, dst, idx_start, npages);
583 nfsi->ncommit -= res;
584 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
585 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
591 static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
593 struct backing_dev_info *bdi = mapping->backing_dev_info;
599 if (!bdi_write_congested(bdi))
602 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
605 rpc_clnt_sigmask(clnt, &oldset);
606 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
607 if (bdi_write_congested(bdi)) {
613 rpc_clnt_sigunmask(clnt, &oldset);
615 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
616 if (bdi_write_congested(bdi))
619 finish_wait(&nfs_write_congestion, &wait);
625 * Try to update any existing write request, or create one if there is none.
626 * In order to match, the request's credentials must match those of
627 * the calling process.
629 * Note: Should always be called with the Page Lock held!
631 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
632 struct inode *inode, struct page *page,
633 unsigned int offset, unsigned int bytes)
635 struct nfs_server *server = NFS_SERVER(inode);
636 struct nfs_inode *nfsi = NFS_I(inode);
637 struct nfs_page *req, *new = NULL;
638 unsigned long rqend, end;
640 end = offset + bytes;
642 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
643 return ERR_PTR(-ERESTARTSYS);
645 /* Loop over all inode entries and see if we find
646 * A request for the page we wish to update
648 spin_lock(&nfsi->req_lock);
649 req = _nfs_find_request(inode, page->index);
651 if (!nfs_lock_request_dontget(req)) {
653 spin_unlock(&nfsi->req_lock);
654 error = nfs_wait_on_request(req);
655 nfs_release_request(req);
658 nfs_release_request(new);
659 return ERR_PTR(error);
663 spin_unlock(&nfsi->req_lock);
665 nfs_release_request(new);
671 nfs_lock_request_dontget(new);
672 error = nfs_inode_add_request(inode, new);
674 spin_unlock(&nfsi->req_lock);
675 nfs_unlock_request(new);
676 return ERR_PTR(error);
678 spin_unlock(&nfsi->req_lock);
679 nfs_mark_request_dirty(new);
682 spin_unlock(&nfsi->req_lock);
684 new = nfs_create_request(ctx, inode, page, offset, bytes);
689 /* We have a request for our page.
690 * If the creds don't match, or the
691 * page addresses don't match,
692 * tell the caller to wait on the conflicting
695 rqend = req->wb_offset + req->wb_bytes;
696 if (req->wb_context != ctx
697 || req->wb_page != page
698 || !nfs_dirty_request(req)
699 || offset > rqend || end < req->wb_offset) {
700 nfs_unlock_request(req);
701 return ERR_PTR(-EBUSY);
704 /* Okay, the request matches. Update the region */
705 if (offset < req->wb_offset) {
706 req->wb_offset = offset;
707 req->wb_pgbase = offset;
708 req->wb_bytes = rqend - req->wb_offset;
712 req->wb_bytes = end - req->wb_offset;
717 int nfs_flush_incompatible(struct file *file, struct page *page)
719 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
720 struct inode *inode = page->mapping->host;
721 struct nfs_page *req;
724 * Look for a request corresponding to this page. If there
725 * is one, and it belongs to another file, we flush it out
726 * before we try to copy anything into the page. Do this
727 * due to the lack of an ACCESS-type call in NFSv2.
728 * Also do the same if we find a request from an existing
731 req = nfs_find_request(inode, page->index);
733 if (req->wb_page != page || ctx != req->wb_context)
734 status = nfs_wb_page(inode, page);
735 nfs_release_request(req);
737 return (status < 0) ? status : 0;
741 * Update and possibly write a cached page of an NFS file.
743 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
744 * things with a page scheduled for an RPC call (e.g. invalidate it).
746 int nfs_updatepage(struct file *file, struct page *page,
747 unsigned int offset, unsigned int count)
749 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
750 struct inode *inode = page->mapping->host;
751 struct nfs_page *req;
754 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
755 file->f_dentry->d_parent->d_name.name,
756 file->f_dentry->d_name.name, count,
757 (long long)(page_offset(page) +offset));
759 if (IS_SYNC(inode)) {
760 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
762 if (offset == 0 && status == PAGE_CACHE_SIZE)
763 SetPageUptodate(page);
769 /* If we're not using byte range locks, and we know the page
770 * is entirely in cache, it may be more efficient to avoid
771 * fragmenting write requests.
773 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
774 loff_t end_offs = i_size_read(inode) - 1;
775 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
779 if (unlikely(end_offs < 0)) {
781 } else if (page->index == end_index) {
783 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
786 } else if (page->index < end_index)
787 count = PAGE_CACHE_SIZE;
791 * Try to find an NFS request corresponding to this page
793 * If the existing request cannot be updated, we must flush
797 req = nfs_update_request(ctx, inode, page, offset, count);
798 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
799 if (status != -EBUSY)
801 /* Request could not be updated. Flush it out and try again */
802 status = nfs_wb_page(inode, page);
803 } while (status >= 0);
809 /* Update file length */
810 nfs_grow_file(page, offset, count);
811 /* Set the PG_uptodate flag? */
812 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
813 nfs_unlock_request(req);
815 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
816 status, (long long)i_size_read(inode));
818 ClearPageUptodate(page);
822 static void nfs_writepage_release(struct nfs_page *req)
824 end_page_writeback(req->wb_page);
826 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
827 if (!PageError(req->wb_page)) {
828 if (NFS_NEED_RESCHED(req)) {
829 nfs_mark_request_dirty(req);
831 } else if (NFS_NEED_COMMIT(req)) {
832 nfs_mark_request_commit(req);
836 nfs_inode_remove_request(req);
839 nfs_clear_commit(req);
840 nfs_clear_reschedule(req);
842 nfs_inode_remove_request(req);
844 nfs_clear_page_writeback(req);
847 static inline int flush_task_priority(int how)
849 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
851 return RPC_PRIORITY_HIGH;
853 return RPC_PRIORITY_LOW;
855 return RPC_PRIORITY_NORMAL;
859 * Set up the argument/result storage required for the RPC call.
861 static void nfs_write_rpcsetup(struct nfs_page *req,
862 struct nfs_write_data *data,
863 unsigned int count, unsigned int offset,
868 /* Set up the RPC argument and reply structs
869 * NB: take care not to mess about with data->commit et al. */
872 data->inode = inode = req->wb_context->dentry->d_inode;
873 data->cred = req->wb_context->cred;
875 data->args.fh = NFS_FH(inode);
876 data->args.offset = req_offset(req) + offset;
877 data->args.pgbase = req->wb_pgbase + offset;
878 data->args.pages = data->pagevec;
879 data->args.count = count;
880 data->args.context = req->wb_context;
882 data->res.fattr = &data->fattr;
883 data->res.count = count;
884 data->res.verf = &data->verf;
885 nfs_fattr_init(&data->fattr);
887 NFS_PROTO(inode)->write_setup(data, how);
889 data->task.tk_priority = flush_task_priority(how);
890 data->task.tk_cookie = (unsigned long)inode;
892 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
895 (long long)NFS_FILEID(inode),
897 (unsigned long long)data->args.offset);
900 static void nfs_execute_write(struct nfs_write_data *data)
902 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
905 rpc_clnt_sigmask(clnt, &oldset);
907 rpc_execute(&data->task);
909 rpc_clnt_sigunmask(clnt, &oldset);
913 * Generate multiple small requests to write out a single
914 * contiguous dirty area on one page.
916 static int nfs_flush_multi(struct list_head *head, struct inode *inode, int how)
918 struct nfs_page *req = nfs_list_entry(head->next);
919 struct page *page = req->wb_page;
920 struct nfs_write_data *data;
921 unsigned int wsize = NFS_SERVER(inode)->wsize;
922 unsigned int nbytes, offset;
926 nfs_list_remove_request(req);
928 nbytes = req->wb_bytes;
930 data = nfs_writedata_alloc(1);
933 list_add(&data->pages, &list);
939 atomic_set(&req->wb_complete, requests);
941 ClearPageError(page);
942 set_page_writeback(page);
944 nbytes = req->wb_bytes;
946 data = list_entry(list.next, struct nfs_write_data, pages);
947 list_del_init(&data->pages);
949 data->pagevec[0] = page;
950 data->complete = nfs_writeback_done_partial;
952 if (nbytes > wsize) {
953 nfs_write_rpcsetup(req, data, wsize, offset, how);
957 nfs_write_rpcsetup(req, data, nbytes, offset, how);
960 nfs_execute_write(data);
961 } while (nbytes != 0);
966 while (!list_empty(&list)) {
967 data = list_entry(list.next, struct nfs_write_data, pages);
968 list_del(&data->pages);
969 nfs_writedata_free(data);
971 nfs_mark_request_dirty(req);
972 nfs_clear_page_writeback(req);
977 * Create an RPC task for the given write request and kick it.
978 * The page must have been locked by the caller.
980 * It may happen that the page we're passed is not marked dirty.
981 * This is the case if nfs_updatepage detects a conflicting request
982 * that has been written but not committed.
984 static int nfs_flush_one(struct list_head *head, struct inode *inode, int how)
986 struct nfs_page *req;
988 struct nfs_write_data *data;
991 if (NFS_SERVER(inode)->wsize < PAGE_CACHE_SIZE)
992 return nfs_flush_multi(head, inode, how);
994 data = nfs_writedata_alloc(NFS_SERVER(inode)->wpages);
998 pages = data->pagevec;
1000 while (!list_empty(head)) {
1001 req = nfs_list_entry(head->next);
1002 nfs_list_remove_request(req);
1003 nfs_list_add_request(req, &data->pages);
1004 ClearPageError(req->wb_page);
1005 set_page_writeback(req->wb_page);
1006 *pages++ = req->wb_page;
1007 count += req->wb_bytes;
1009 req = nfs_list_entry(data->pages.next);
1011 data->complete = nfs_writeback_done_full;
1012 /* Set up the argument struct */
1013 nfs_write_rpcsetup(req, data, count, 0, how);
1015 nfs_execute_write(data);
1018 while (!list_empty(head)) {
1019 struct nfs_page *req = nfs_list_entry(head->next);
1020 nfs_list_remove_request(req);
1021 nfs_mark_request_dirty(req);
1022 nfs_clear_page_writeback(req);
1028 nfs_flush_list(struct list_head *head, int wpages, int how)
1030 LIST_HEAD(one_request);
1031 struct nfs_page *req;
1033 unsigned int pages = 0;
1035 while (!list_empty(head)) {
1036 pages += nfs_coalesce_requests(head, &one_request, wpages);
1037 req = nfs_list_entry(one_request.next);
1038 error = nfs_flush_one(&one_request, req->wb_context->dentry->d_inode, how);
1045 while (!list_empty(head)) {
1046 req = nfs_list_entry(head->next);
1047 nfs_list_remove_request(req);
1048 nfs_mark_request_dirty(req);
1049 nfs_clear_page_writeback(req);
1055 * Handle a write reply that flushed part of a page.
1057 static void nfs_writeback_done_partial(struct nfs_write_data *data, int status)
1059 struct nfs_page *req = data->req;
1060 struct page *page = req->wb_page;
1062 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1063 req->wb_context->dentry->d_inode->i_sb->s_id,
1064 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1066 (long long)req_offset(req));
1069 ClearPageUptodate(page);
1071 req->wb_context->error = status;
1072 dprintk(", error = %d\n", status);
1074 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1075 if (data->verf.committed < NFS_FILE_SYNC) {
1076 if (!NFS_NEED_COMMIT(req)) {
1077 nfs_defer_commit(req);
1078 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1079 dprintk(" defer commit\n");
1080 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1081 nfs_defer_reschedule(req);
1082 dprintk(" server reboot detected\n");
1089 if (atomic_dec_and_test(&req->wb_complete))
1090 nfs_writepage_release(req);
1094 * Handle a write reply that flushes a whole page.
1096 * FIXME: There is an inherent race with invalidate_inode_pages and
1097 * writebacks since the page->count is kept > 1 for as long
1098 * as the page has a write request pending.
1100 static void nfs_writeback_done_full(struct nfs_write_data *data, int status)
1102 struct nfs_page *req;
1105 /* Update attributes as result of writeback. */
1106 while (!list_empty(&data->pages)) {
1107 req = nfs_list_entry(data->pages.next);
1108 nfs_list_remove_request(req);
1109 page = req->wb_page;
1111 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1112 req->wb_context->dentry->d_inode->i_sb->s_id,
1113 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1115 (long long)req_offset(req));
1118 ClearPageUptodate(page);
1120 req->wb_context->error = status;
1121 end_page_writeback(page);
1122 nfs_inode_remove_request(req);
1123 dprintk(", error = %d\n", status);
1126 end_page_writeback(page);
1128 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1129 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1130 nfs_inode_remove_request(req);
1134 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1135 nfs_mark_request_commit(req);
1136 dprintk(" marked for commit\n");
1138 nfs_inode_remove_request(req);
1141 nfs_clear_page_writeback(req);
1146 * This function is called when the WRITE call is complete.
1148 void nfs_writeback_done(struct rpc_task *task, void *calldata)
1150 struct nfs_write_data *data = calldata;
1151 struct nfs_writeargs *argp = &data->args;
1152 struct nfs_writeres *resp = &data->res;
1154 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1155 task->tk_pid, task->tk_status);
1157 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1158 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1159 /* We tried a write call, but the server did not
1160 * commit data to stable storage even though we
1162 * Note: There is a known bug in Tru64 < 5.0 in which
1163 * the server reports NFS_DATA_SYNC, but performs
1164 * NFS_FILE_SYNC. We therefore implement this checking
1165 * as a dprintk() in order to avoid filling syslog.
1167 static unsigned long complain;
1169 if (time_before(complain, jiffies)) {
1170 dprintk("NFS: faulty NFS server %s:"
1171 " (committed = %d) != (stable = %d)\n",
1172 NFS_SERVER(data->inode)->hostname,
1173 resp->verf->committed, argp->stable);
1174 complain = jiffies + 300 * HZ;
1178 /* Is this a short write? */
1179 if (task->tk_status >= 0 && resp->count < argp->count) {
1180 static unsigned long complain;
1182 /* Has the server at least made some progress? */
1183 if (resp->count != 0) {
1184 /* Was this an NFSv2 write or an NFSv3 stable write? */
1185 if (resp->verf->committed != NFS_UNSTABLE) {
1186 /* Resend from where the server left off */
1187 argp->offset += resp->count;
1188 argp->pgbase += resp->count;
1189 argp->count -= resp->count;
1191 /* Resend as a stable write in order to avoid
1192 * headaches in the case of a server crash.
1194 argp->stable = NFS_FILE_SYNC;
1196 rpc_restart_call(task);
1199 if (time_before(complain, jiffies)) {
1201 "NFS: Server wrote zero bytes, expected %u.\n",
1203 complain = jiffies + 300 * HZ;
1205 /* Can't do anything about it except throw an error. */
1206 task->tk_status = -EIO;
1210 * Process the nfs_page list
1212 data->complete(data, task->tk_status);
1216 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1217 void nfs_commit_release(void *wdata)
1219 nfs_commit_free(wdata);
1223 * Set up the argument/result storage required for the RPC call.
1225 static void nfs_commit_rpcsetup(struct list_head *head,
1226 struct nfs_write_data *data, int how)
1228 struct nfs_page *first;
1229 struct inode *inode;
1231 /* Set up the RPC argument and reply structs
1232 * NB: take care not to mess about with data->commit et al. */
1234 list_splice_init(head, &data->pages);
1235 first = nfs_list_entry(data->pages.next);
1236 inode = first->wb_context->dentry->d_inode;
1238 data->inode = inode;
1239 data->cred = first->wb_context->cred;
1241 data->args.fh = NFS_FH(data->inode);
1242 /* Note: we always request a commit of the entire inode */
1243 data->args.offset = 0;
1244 data->args.count = 0;
1245 data->res.count = 0;
1246 data->res.fattr = &data->fattr;
1247 data->res.verf = &data->verf;
1248 nfs_fattr_init(&data->fattr);
1250 NFS_PROTO(inode)->commit_setup(data, how);
1252 data->task.tk_priority = flush_task_priority(how);
1253 data->task.tk_cookie = (unsigned long)inode;
1255 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
1259 * Commit dirty pages
1262 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1264 struct nfs_write_data *data;
1265 struct nfs_page *req;
1267 data = nfs_commit_alloc(NFS_SERVER(inode)->wpages);
1272 /* Set up the argument struct */
1273 nfs_commit_rpcsetup(head, data, how);
1275 nfs_execute_write(data);
1278 while (!list_empty(head)) {
1279 req = nfs_list_entry(head->next);
1280 nfs_list_remove_request(req);
1281 nfs_mark_request_commit(req);
1282 nfs_clear_page_writeback(req);
1288 * COMMIT call returned
1290 void nfs_commit_done(struct rpc_task *task, void *calldata)
1292 struct nfs_write_data *data = calldata;
1293 struct nfs_page *req;
1296 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1297 task->tk_pid, task->tk_status);
1299 while (!list_empty(&data->pages)) {
1300 req = nfs_list_entry(data->pages.next);
1301 nfs_list_remove_request(req);
1303 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1304 req->wb_context->dentry->d_inode->i_sb->s_id,
1305 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1307 (long long)req_offset(req));
1308 if (task->tk_status < 0) {
1309 req->wb_context->error = task->tk_status;
1310 nfs_inode_remove_request(req);
1311 dprintk(", error = %d\n", task->tk_status);
1315 /* Okay, COMMIT succeeded, apparently. Check the verifier
1316 * returned by the server against all stored verfs. */
1317 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1318 /* We have a match */
1319 nfs_inode_remove_request(req);
1323 /* We have a mismatch. Write the page again */
1324 dprintk(" mismatch\n");
1325 nfs_mark_request_dirty(req);
1327 nfs_clear_page_writeback(req);
1330 sub_page_state(nr_unstable,res);
1334 static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
1335 unsigned int npages, int how)
1337 struct nfs_inode *nfsi = NFS_I(inode);
1342 spin_lock(&nfsi->req_lock);
1343 res = nfs_scan_dirty(inode, &head, idx_start, npages);
1344 spin_unlock(&nfsi->req_lock);
1346 struct nfs_server *server = NFS_SERVER(inode);
1348 /* For single writes, FLUSH_STABLE is more efficient */
1349 if (res == nfsi->npages && nfsi->npages <= server->wpages) {
1350 if (res > 1 || nfs_list_entry(head.next)->wb_bytes <= server->wsize)
1351 how |= FLUSH_STABLE;
1353 error = nfs_flush_list(&head, server->wpages, how);
1360 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1361 int nfs_commit_inode(struct inode *inode, int how)
1363 struct nfs_inode *nfsi = NFS_I(inode);
1368 spin_lock(&nfsi->req_lock);
1369 res = nfs_scan_commit(inode, &head, 0, 0);
1370 spin_unlock(&nfsi->req_lock);
1372 error = nfs_commit_list(inode, &head, how);
1380 int nfs_sync_inode(struct inode *inode, unsigned long idx_start,
1381 unsigned int npages, int how)
1383 int nocommit = how & FLUSH_NOCOMMIT;
1384 int wait = how & FLUSH_WAIT;
1387 how &= ~(FLUSH_WAIT|FLUSH_NOCOMMIT);
1391 error = nfs_wait_on_requests(inode, idx_start, npages);
1395 error = nfs_flush_inode(inode, idx_start, npages, how);
1399 error = nfs_commit_inode(inode, how);
1400 } while (error > 0);
1404 int nfs_init_writepagecache(void)
1406 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1407 sizeof(struct nfs_write_data),
1408 0, SLAB_HWCACHE_ALIGN,
1410 if (nfs_wdata_cachep == NULL)
1413 nfs_wdata_mempool = mempool_create(MIN_POOL_WRITE,
1417 if (nfs_wdata_mempool == NULL)
1420 nfs_commit_mempool = mempool_create(MIN_POOL_COMMIT,
1424 if (nfs_commit_mempool == NULL)
1430 void nfs_destroy_writepagecache(void)
1432 mempool_destroy(nfs_commit_mempool);
1433 mempool_destroy(nfs_wdata_mempool);
1434 if (kmem_cache_destroy(nfs_wdata_cachep))
1435 printk(KERN_INFO "nfs_write_data: not all structures were freed\n");