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);
657 return ERR_PTR(error);
660 spin_unlock(&nfsi->req_lock);
662 nfs_release_request(new);
668 nfs_lock_request_dontget(new);
669 error = nfs_inode_add_request(inode, new);
671 spin_unlock(&nfsi->req_lock);
672 nfs_unlock_request(new);
673 return ERR_PTR(error);
675 spin_unlock(&nfsi->req_lock);
676 nfs_mark_request_dirty(new);
679 spin_unlock(&nfsi->req_lock);
681 new = nfs_create_request(ctx, inode, page, offset, bytes);
686 /* We have a request for our page.
687 * If the creds don't match, or the
688 * page addresses don't match,
689 * tell the caller to wait on the conflicting
692 rqend = req->wb_offset + req->wb_bytes;
693 if (req->wb_context != ctx
694 || req->wb_page != page
695 || !nfs_dirty_request(req)
696 || offset > rqend || end < req->wb_offset) {
697 nfs_unlock_request(req);
698 return ERR_PTR(-EBUSY);
701 /* Okay, the request matches. Update the region */
702 if (offset < req->wb_offset) {
703 req->wb_offset = offset;
704 req->wb_pgbase = offset;
705 req->wb_bytes = rqend - req->wb_offset;
709 req->wb_bytes = end - req->wb_offset;
714 int nfs_flush_incompatible(struct file *file, struct page *page)
716 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
717 struct inode *inode = page->mapping->host;
718 struct nfs_page *req;
721 * Look for a request corresponding to this page. If there
722 * is one, and it belongs to another file, we flush it out
723 * before we try to copy anything into the page. Do this
724 * due to the lack of an ACCESS-type call in NFSv2.
725 * Also do the same if we find a request from an existing
728 req = nfs_find_request(inode, page->index);
730 if (req->wb_page != page || ctx != req->wb_context)
731 status = nfs_wb_page(inode, page);
732 nfs_release_request(req);
734 return (status < 0) ? status : 0;
738 * Update and possibly write a cached page of an NFS file.
740 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
741 * things with a page scheduled for an RPC call (e.g. invalidate it).
743 int nfs_updatepage(struct file *file, struct page *page,
744 unsigned int offset, unsigned int count)
746 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
747 struct inode *inode = page->mapping->host;
748 struct nfs_page *req;
751 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
752 file->f_dentry->d_parent->d_name.name,
753 file->f_dentry->d_name.name, count,
754 (long long)(page_offset(page) +offset));
756 if (IS_SYNC(inode)) {
757 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
759 if (offset == 0 && status == PAGE_CACHE_SIZE)
760 SetPageUptodate(page);
766 /* If we're not using byte range locks, and we know the page
767 * is entirely in cache, it may be more efficient to avoid
768 * fragmenting write requests.
770 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
771 loff_t end_offs = i_size_read(inode) - 1;
772 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
776 if (unlikely(end_offs < 0)) {
778 } else if (page->index == end_index) {
780 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
783 } else if (page->index < end_index)
784 count = PAGE_CACHE_SIZE;
788 * Try to find an NFS request corresponding to this page
790 * If the existing request cannot be updated, we must flush
794 req = nfs_update_request(ctx, inode, page, offset, count);
795 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
796 if (status != -EBUSY)
798 /* Request could not be updated. Flush it out and try again */
799 status = nfs_wb_page(inode, page);
800 } while (status >= 0);
806 /* Update file length */
807 nfs_grow_file(page, offset, count);
808 /* Set the PG_uptodate flag? */
809 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
810 nfs_unlock_request(req);
812 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
813 status, (long long)i_size_read(inode));
815 ClearPageUptodate(page);
819 static void nfs_writepage_release(struct nfs_page *req)
821 end_page_writeback(req->wb_page);
823 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
824 if (!PageError(req->wb_page)) {
825 if (NFS_NEED_RESCHED(req)) {
826 nfs_mark_request_dirty(req);
828 } else if (NFS_NEED_COMMIT(req)) {
829 nfs_mark_request_commit(req);
833 nfs_inode_remove_request(req);
836 nfs_clear_commit(req);
837 nfs_clear_reschedule(req);
839 nfs_inode_remove_request(req);
841 nfs_clear_page_writeback(req);
844 static inline int flush_task_priority(int how)
846 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
848 return RPC_PRIORITY_HIGH;
850 return RPC_PRIORITY_LOW;
852 return RPC_PRIORITY_NORMAL;
856 * Set up the argument/result storage required for the RPC call.
858 static void nfs_write_rpcsetup(struct nfs_page *req,
859 struct nfs_write_data *data,
860 unsigned int count, unsigned int offset,
865 /* Set up the RPC argument and reply structs
866 * NB: take care not to mess about with data->commit et al. */
869 data->inode = inode = req->wb_context->dentry->d_inode;
870 data->cred = req->wb_context->cred;
872 data->args.fh = NFS_FH(inode);
873 data->args.offset = req_offset(req) + offset;
874 data->args.pgbase = req->wb_pgbase + offset;
875 data->args.pages = data->pagevec;
876 data->args.count = count;
877 data->args.context = req->wb_context;
879 data->res.fattr = &data->fattr;
880 data->res.count = count;
881 data->res.verf = &data->verf;
882 nfs_fattr_init(&data->fattr);
884 NFS_PROTO(inode)->write_setup(data, how);
886 data->task.tk_priority = flush_task_priority(how);
887 data->task.tk_cookie = (unsigned long)inode;
889 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
892 (long long)NFS_FILEID(inode),
894 (unsigned long long)data->args.offset);
897 static void nfs_execute_write(struct nfs_write_data *data)
899 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
902 rpc_clnt_sigmask(clnt, &oldset);
904 rpc_execute(&data->task);
906 rpc_clnt_sigunmask(clnt, &oldset);
910 * Generate multiple small requests to write out a single
911 * contiguous dirty area on one page.
913 static int nfs_flush_multi(struct list_head *head, struct inode *inode, int how)
915 struct nfs_page *req = nfs_list_entry(head->next);
916 struct page *page = req->wb_page;
917 struct nfs_write_data *data;
918 unsigned int wsize = NFS_SERVER(inode)->wsize;
919 unsigned int nbytes, offset;
923 nfs_list_remove_request(req);
925 nbytes = req->wb_bytes;
927 data = nfs_writedata_alloc(1);
930 list_add(&data->pages, &list);
936 atomic_set(&req->wb_complete, requests);
938 ClearPageError(page);
939 set_page_writeback(page);
941 nbytes = req->wb_bytes;
943 data = list_entry(list.next, struct nfs_write_data, pages);
944 list_del_init(&data->pages);
946 data->pagevec[0] = page;
947 data->complete = nfs_writeback_done_partial;
949 if (nbytes > wsize) {
950 nfs_write_rpcsetup(req, data, wsize, offset, how);
954 nfs_write_rpcsetup(req, data, nbytes, offset, how);
957 nfs_execute_write(data);
958 } while (nbytes != 0);
963 while (!list_empty(&list)) {
964 data = list_entry(list.next, struct nfs_write_data, pages);
965 list_del(&data->pages);
966 nfs_writedata_free(data);
968 nfs_mark_request_dirty(req);
969 nfs_clear_page_writeback(req);
974 * Create an RPC task for the given write request and kick it.
975 * The page must have been locked by the caller.
977 * It may happen that the page we're passed is not marked dirty.
978 * This is the case if nfs_updatepage detects a conflicting request
979 * that has been written but not committed.
981 static int nfs_flush_one(struct list_head *head, struct inode *inode, int how)
983 struct nfs_page *req;
985 struct nfs_write_data *data;
988 if (NFS_SERVER(inode)->wsize < PAGE_CACHE_SIZE)
989 return nfs_flush_multi(head, inode, how);
991 data = nfs_writedata_alloc(NFS_SERVER(inode)->wpages);
995 pages = data->pagevec;
997 while (!list_empty(head)) {
998 req = nfs_list_entry(head->next);
999 nfs_list_remove_request(req);
1000 nfs_list_add_request(req, &data->pages);
1001 ClearPageError(req->wb_page);
1002 set_page_writeback(req->wb_page);
1003 *pages++ = req->wb_page;
1004 count += req->wb_bytes;
1006 req = nfs_list_entry(data->pages.next);
1008 data->complete = nfs_writeback_done_full;
1009 /* Set up the argument struct */
1010 nfs_write_rpcsetup(req, data, count, 0, how);
1012 nfs_execute_write(data);
1015 while (!list_empty(head)) {
1016 struct nfs_page *req = nfs_list_entry(head->next);
1017 nfs_list_remove_request(req);
1018 nfs_mark_request_dirty(req);
1019 nfs_clear_page_writeback(req);
1025 nfs_flush_list(struct list_head *head, int wpages, int how)
1027 LIST_HEAD(one_request);
1028 struct nfs_page *req;
1030 unsigned int pages = 0;
1032 while (!list_empty(head)) {
1033 pages += nfs_coalesce_requests(head, &one_request, wpages);
1034 req = nfs_list_entry(one_request.next);
1035 error = nfs_flush_one(&one_request, req->wb_context->dentry->d_inode, how);
1042 while (!list_empty(head)) {
1043 req = nfs_list_entry(head->next);
1044 nfs_list_remove_request(req);
1045 nfs_mark_request_dirty(req);
1046 nfs_clear_page_writeback(req);
1052 * Handle a write reply that flushed part of a page.
1054 static void nfs_writeback_done_partial(struct nfs_write_data *data, int status)
1056 struct nfs_page *req = data->req;
1057 struct page *page = req->wb_page;
1059 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1060 req->wb_context->dentry->d_inode->i_sb->s_id,
1061 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1063 (long long)req_offset(req));
1066 ClearPageUptodate(page);
1068 req->wb_context->error = status;
1069 dprintk(", error = %d\n", status);
1071 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1072 if (data->verf.committed < NFS_FILE_SYNC) {
1073 if (!NFS_NEED_COMMIT(req)) {
1074 nfs_defer_commit(req);
1075 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1076 dprintk(" defer commit\n");
1077 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1078 nfs_defer_reschedule(req);
1079 dprintk(" server reboot detected\n");
1086 if (atomic_dec_and_test(&req->wb_complete))
1087 nfs_writepage_release(req);
1091 * Handle a write reply that flushes a whole page.
1093 * FIXME: There is an inherent race with invalidate_inode_pages and
1094 * writebacks since the page->count is kept > 1 for as long
1095 * as the page has a write request pending.
1097 static void nfs_writeback_done_full(struct nfs_write_data *data, int status)
1099 struct nfs_page *req;
1102 /* Update attributes as result of writeback. */
1103 while (!list_empty(&data->pages)) {
1104 req = nfs_list_entry(data->pages.next);
1105 nfs_list_remove_request(req);
1106 page = req->wb_page;
1108 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1109 req->wb_context->dentry->d_inode->i_sb->s_id,
1110 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1112 (long long)req_offset(req));
1115 ClearPageUptodate(page);
1117 req->wb_context->error = status;
1118 end_page_writeback(page);
1119 nfs_inode_remove_request(req);
1120 dprintk(", error = %d\n", status);
1123 end_page_writeback(page);
1125 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1126 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1127 nfs_inode_remove_request(req);
1131 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1132 nfs_mark_request_commit(req);
1133 dprintk(" marked for commit\n");
1135 nfs_inode_remove_request(req);
1138 nfs_clear_page_writeback(req);
1143 * This function is called when the WRITE call is complete.
1145 void nfs_writeback_done(struct rpc_task *task, void *calldata)
1147 struct nfs_write_data *data = calldata;
1148 struct nfs_writeargs *argp = &data->args;
1149 struct nfs_writeres *resp = &data->res;
1151 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1152 task->tk_pid, task->tk_status);
1154 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1155 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1156 /* We tried a write call, but the server did not
1157 * commit data to stable storage even though we
1159 * Note: There is a known bug in Tru64 < 5.0 in which
1160 * the server reports NFS_DATA_SYNC, but performs
1161 * NFS_FILE_SYNC. We therefore implement this checking
1162 * as a dprintk() in order to avoid filling syslog.
1164 static unsigned long complain;
1166 if (time_before(complain, jiffies)) {
1167 dprintk("NFS: faulty NFS server %s:"
1168 " (committed = %d) != (stable = %d)\n",
1169 NFS_SERVER(data->inode)->hostname,
1170 resp->verf->committed, argp->stable);
1171 complain = jiffies + 300 * HZ;
1175 /* Is this a short write? */
1176 if (task->tk_status >= 0 && resp->count < argp->count) {
1177 static unsigned long complain;
1179 /* Has the server at least made some progress? */
1180 if (resp->count != 0) {
1181 /* Was this an NFSv2 write or an NFSv3 stable write? */
1182 if (resp->verf->committed != NFS_UNSTABLE) {
1183 /* Resend from where the server left off */
1184 argp->offset += resp->count;
1185 argp->pgbase += resp->count;
1186 argp->count -= resp->count;
1188 /* Resend as a stable write in order to avoid
1189 * headaches in the case of a server crash.
1191 argp->stable = NFS_FILE_SYNC;
1193 rpc_restart_call(task);
1196 if (time_before(complain, jiffies)) {
1198 "NFS: Server wrote zero bytes, expected %u.\n",
1200 complain = jiffies + 300 * HZ;
1202 /* Can't do anything about it except throw an error. */
1203 task->tk_status = -EIO;
1207 * Process the nfs_page list
1209 data->complete(data, task->tk_status);
1213 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1214 void nfs_commit_release(void *wdata)
1216 nfs_commit_free(wdata);
1220 * Set up the argument/result storage required for the RPC call.
1222 static void nfs_commit_rpcsetup(struct list_head *head,
1223 struct nfs_write_data *data, int how)
1225 struct nfs_page *first;
1226 struct inode *inode;
1228 /* Set up the RPC argument and reply structs
1229 * NB: take care not to mess about with data->commit et al. */
1231 list_splice_init(head, &data->pages);
1232 first = nfs_list_entry(data->pages.next);
1233 inode = first->wb_context->dentry->d_inode;
1235 data->inode = inode;
1236 data->cred = first->wb_context->cred;
1238 data->args.fh = NFS_FH(data->inode);
1239 /* Note: we always request a commit of the entire inode */
1240 data->args.offset = 0;
1241 data->args.count = 0;
1242 data->res.count = 0;
1243 data->res.fattr = &data->fattr;
1244 data->res.verf = &data->verf;
1245 nfs_fattr_init(&data->fattr);
1247 NFS_PROTO(inode)->commit_setup(data, how);
1249 data->task.tk_priority = flush_task_priority(how);
1250 data->task.tk_cookie = (unsigned long)inode;
1252 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
1256 * Commit dirty pages
1259 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1261 struct nfs_write_data *data;
1262 struct nfs_page *req;
1264 data = nfs_commit_alloc(NFS_SERVER(inode)->wpages);
1269 /* Set up the argument struct */
1270 nfs_commit_rpcsetup(head, data, how);
1272 nfs_execute_write(data);
1275 while (!list_empty(head)) {
1276 req = nfs_list_entry(head->next);
1277 nfs_list_remove_request(req);
1278 nfs_mark_request_commit(req);
1279 nfs_clear_page_writeback(req);
1285 * COMMIT call returned
1287 void nfs_commit_done(struct rpc_task *task, void *calldata)
1289 struct nfs_write_data *data = calldata;
1290 struct nfs_page *req;
1293 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1294 task->tk_pid, task->tk_status);
1296 while (!list_empty(&data->pages)) {
1297 req = nfs_list_entry(data->pages.next);
1298 nfs_list_remove_request(req);
1300 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1301 req->wb_context->dentry->d_inode->i_sb->s_id,
1302 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1304 (long long)req_offset(req));
1305 if (task->tk_status < 0) {
1306 req->wb_context->error = task->tk_status;
1307 nfs_inode_remove_request(req);
1308 dprintk(", error = %d\n", task->tk_status);
1312 /* Okay, COMMIT succeeded, apparently. Check the verifier
1313 * returned by the server against all stored verfs. */
1314 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1315 /* We have a match */
1316 nfs_inode_remove_request(req);
1320 /* We have a mismatch. Write the page again */
1321 dprintk(" mismatch\n");
1322 nfs_mark_request_dirty(req);
1324 nfs_clear_page_writeback(req);
1327 sub_page_state(nr_unstable,res);
1331 static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
1332 unsigned int npages, int how)
1334 struct nfs_inode *nfsi = NFS_I(inode);
1339 spin_lock(&nfsi->req_lock);
1340 res = nfs_scan_dirty(inode, &head, idx_start, npages);
1341 spin_unlock(&nfsi->req_lock);
1343 struct nfs_server *server = NFS_SERVER(inode);
1345 /* For single writes, FLUSH_STABLE is more efficient */
1346 if (res == nfsi->npages && nfsi->npages <= server->wpages) {
1347 if (res > 1 || nfs_list_entry(head.next)->wb_bytes <= server->wsize)
1348 how |= FLUSH_STABLE;
1350 error = nfs_flush_list(&head, server->wpages, how);
1357 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1358 int nfs_commit_inode(struct inode *inode, int how)
1360 struct nfs_inode *nfsi = NFS_I(inode);
1365 spin_lock(&nfsi->req_lock);
1366 res = nfs_scan_commit(inode, &head, 0, 0);
1367 spin_unlock(&nfsi->req_lock);
1369 error = nfs_commit_list(inode, &head, how);
1377 int nfs_sync_inode(struct inode *inode, unsigned long idx_start,
1378 unsigned int npages, int how)
1380 int nocommit = how & FLUSH_NOCOMMIT;
1381 int wait = how & FLUSH_WAIT;
1384 how &= ~(FLUSH_WAIT|FLUSH_NOCOMMIT);
1388 error = nfs_wait_on_requests(inode, idx_start, npages);
1392 error = nfs_flush_inode(inode, idx_start, npages, how);
1396 error = nfs_commit_inode(inode, how);
1397 } while (error > 0);
1401 int nfs_init_writepagecache(void)
1403 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1404 sizeof(struct nfs_write_data),
1405 0, SLAB_HWCACHE_ALIGN,
1407 if (nfs_wdata_cachep == NULL)
1410 nfs_wdata_mempool = mempool_create(MIN_POOL_WRITE,
1414 if (nfs_wdata_mempool == NULL)
1417 nfs_commit_mempool = mempool_create(MIN_POOL_COMMIT,
1421 if (nfs_commit_mempool == NULL)
1427 void nfs_destroy_writepagecache(void)
1429 mempool_destroy(nfs_commit_mempool);
1430 mempool_destroy(nfs_wdata_mempool);
1431 if (kmem_cache_destroy(nfs_wdata_cachep))
1432 printk(KERN_INFO "nfs_write_data: not all structures were freed\n");