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>
64 #include <linux/mempool.h>
66 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
68 #define MIN_POOL_WRITE (32)
69 #define MIN_POOL_COMMIT (4)
72 * Local function declarations
74 static struct nfs_page * nfs_update_request(struct file*, struct inode *,
76 unsigned int, unsigned int);
77 static void nfs_writeback_done_partial(struct nfs_write_data *, int);
78 static void nfs_writeback_done_full(struct nfs_write_data *, 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);
82 static kmem_cache_t *nfs_wdata_cachep;
83 static mempool_t *nfs_wdata_mempool;
84 static mempool_t *nfs_commit_mempool;
86 static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
88 static __inline__ struct nfs_write_data *nfs_writedata_alloc(void)
90 struct nfs_write_data *p;
91 p = (struct nfs_write_data *)mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
93 memset(p, 0, sizeof(*p));
94 INIT_LIST_HEAD(&p->pages);
99 static __inline__ void nfs_writedata_free(struct nfs_write_data *p)
101 mempool_free(p, nfs_wdata_mempool);
104 static void nfs_writedata_release(struct rpc_task *task)
106 struct nfs_write_data *wdata = (struct nfs_write_data *)task->tk_calldata;
107 nfs_writedata_free(wdata);
110 static __inline__ struct nfs_write_data *nfs_commit_alloc(void)
112 struct nfs_write_data *p;
113 p = (struct nfs_write_data *)mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
115 memset(p, 0, sizeof(*p));
116 INIT_LIST_HEAD(&p->pages);
121 static __inline__ void nfs_commit_free(struct nfs_write_data *p)
123 mempool_free(p, nfs_commit_mempool);
126 /* Adjust the file length if we're writing beyond the end */
127 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
129 struct inode *inode = page->mapping->host;
130 loff_t end, i_size = i_size_read(inode);
131 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
133 if (i_size > 0 && page->index < end_index)
135 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
138 i_size_write(inode, end);
141 /* We can set the PG_uptodate flag if we see that a write request
142 * covers the full page.
144 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
148 if (PageUptodate(page))
152 if (count == PAGE_CACHE_SIZE) {
153 SetPageUptodate(page);
157 end_offs = i_size_read(page->mapping->host) - 1;
160 /* Is this the last page? */
161 if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
163 /* This is the last page: set PG_uptodate if we cover the entire
164 * extent of the data, then zero the rest of the page.
166 if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
167 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
168 SetPageUptodate(page);
173 * Write a page synchronously.
174 * Offset is the data offset within the page.
176 static int nfs_writepage_sync(struct file *file, struct inode *inode,
177 struct page *page, unsigned int offset, unsigned int count,
180 unsigned int wsize = NFS_SERVER(inode)->wsize;
181 int result, written = 0;
182 struct nfs_write_data *wdata;
184 wdata = kmalloc(sizeof(*wdata), GFP_NOFS);
188 memset(wdata, 0, sizeof(*wdata));
190 wdata->inode = inode;
191 wdata->args.fh = NFS_FH(inode);
192 wdata->args.lockowner = current->files;
193 wdata->args.pages = &page;
194 wdata->args.stable = NFS_FILE_SYNC;
195 wdata->args.pgbase = offset;
196 wdata->args.count = wsize;
197 wdata->res.fattr = &wdata->fattr;
198 wdata->res.verf = &wdata->verf;
200 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
202 (long long)NFS_FILEID(inode),
203 count, (long long)(page_offset(page) + offset));
205 nfs_begin_data_update(inode);
208 wdata->args.count = count;
209 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
211 result = NFS_PROTO(inode)->write(wdata, file);
214 /* Must mark the page invalid after I/O error */
215 ClearPageUptodate(page);
218 if (result < wdata->args.count)
219 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
220 wdata->args.count, result);
222 wdata->args.offset += result;
223 wdata->args.pgbase += result;
227 /* Update file length */
228 nfs_grow_file(page, offset, written);
229 /* Set the PG_uptodate flag? */
230 nfs_mark_uptodate(page, offset, written);
233 ClearPageError(page);
236 nfs_end_data_update_defer(inode);
238 put_rpccred(wdata->cred);
241 return written ? written : result;
244 static int nfs_writepage_async(struct file *file, struct inode *inode,
245 struct page *page, unsigned int offset, unsigned int count)
247 struct nfs_page *req;
250 req = nfs_update_request(file, inode, page, offset, count);
251 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
254 /* Update file length */
255 nfs_grow_file(page, offset, count);
256 /* Set the PG_uptodate flag? */
257 nfs_mark_uptodate(page, offset, count);
258 nfs_unlock_request(req);
263 static int wb_priority(struct writeback_control *wbc)
265 if (wbc->for_reclaim)
266 return FLUSH_HIGHPRI;
267 if (wbc->for_kupdate)
273 * Write an mmapped page to the server.
275 int nfs_writepage(struct page *page, struct writeback_control *wbc)
277 struct inode *inode = page->mapping->host;
278 unsigned long end_index;
279 unsigned offset = PAGE_CACHE_SIZE;
280 loff_t i_size = i_size_read(inode);
281 int inode_referenced = 0;
282 int priority = wb_priority(wbc);
286 * Note: We need to ensure that we have a reference to the inode
287 * if we are to do asynchronous writes. If not, waiting
288 * in nfs_wait_on_request() may deadlock with clear_inode().
290 * If igrab() fails here, then it is in any case safe to
291 * call nfs_wb_page(), since there will be no pending writes.
293 if (igrab(inode) != 0)
294 inode_referenced = 1;
295 end_index = i_size >> PAGE_CACHE_SHIFT;
297 /* Ensure we've flushed out any previous writes */
298 nfs_wb_page_priority(inode, page, priority);
301 if (page->index < end_index)
303 /* things got complicated... */
304 offset = i_size & (PAGE_CACHE_SIZE-1);
306 /* OK, are we completely out? */
307 err = 0; /* potential race with truncate - ignore */
308 if (page->index >= end_index+1 || !offset)
312 if (!IS_SYNC(inode) && inode_referenced) {
313 err = nfs_writepage_async(NULL, inode, page, 0, offset);
316 if (wbc->for_reclaim)
317 err = WRITEPAGE_ACTIVATE;
320 err = nfs_writepage_sync(NULL, inode, page, 0,
324 redirty_page_for_writepage(wbc, page);
330 if (err != WRITEPAGE_ACTIVATE)
332 if (inode_referenced)
338 * Note: causes nfs_update_request() to block on the assumption
339 * that the writeback is generated due to memory pressure.
341 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
343 struct backing_dev_info *bdi = mapping->backing_dev_info;
344 struct inode *inode = mapping->host;
347 err = generic_writepages(mapping, wbc);
350 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
351 if (wbc->nonblocking)
353 nfs_wait_on_write_congestion(mapping, 0);
355 err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
358 wbc->nr_to_write -= err;
359 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
360 err = nfs_wait_on_requests(inode, 0, 0);
364 err = nfs_commit_inode(inode, 0, 0, wb_priority(wbc));
366 wbc->nr_to_write -= err;
370 clear_bit(BDI_write_congested, &bdi->state);
371 wake_up_all(&nfs_write_congestion);
376 * Insert a write request into an inode
379 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);
398 * Insert a write request into an inode
401 nfs_inode_remove_request(struct nfs_page *req)
403 struct nfs_inode *nfsi;
406 BUG_ON (!NFS_WBACK_BUSY(req));
407 spin_lock(&nfs_wreq_lock);
408 inode = req->wb_inode;
410 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
413 spin_unlock(&nfs_wreq_lock);
414 nfs_end_data_update_defer(inode);
417 spin_unlock(&nfs_wreq_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);
437 static struct nfs_page *
438 nfs_find_request(struct inode *inode, unsigned long index)
440 struct nfs_page *req;
442 spin_lock(&nfs_wreq_lock);
443 req = _nfs_find_request(inode, index);
444 spin_unlock(&nfs_wreq_lock);
449 * Add a request to the inode's dirty list.
452 nfs_mark_request_dirty(struct nfs_page *req)
454 struct inode *inode = req->wb_inode;
455 struct nfs_inode *nfsi = NFS_I(inode);
457 spin_lock(&nfs_wreq_lock);
458 nfs_list_add_request(req, &nfsi->dirty);
460 spin_unlock(&nfs_wreq_lock);
461 inc_page_state(nr_dirty);
462 mark_inode_dirty(inode);
466 * Check if a request is dirty
469 nfs_dirty_request(struct nfs_page *req)
471 struct nfs_inode *nfsi = NFS_I(req->wb_inode);
472 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
475 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
477 * Add a request to the inode's commit list.
480 nfs_mark_request_commit(struct nfs_page *req)
482 struct inode *inode = req->wb_inode;
483 struct nfs_inode *nfsi = NFS_I(inode);
485 spin_lock(&nfs_wreq_lock);
486 nfs_list_add_request(req, &nfsi->commit);
488 spin_unlock(&nfs_wreq_lock);
489 inc_page_state(nr_unstable);
490 mark_inode_dirty(inode);
495 * Wait for a request to complete.
497 * Interruptible by signals only if mounted with intr flag.
500 nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
502 struct nfs_inode *nfsi = NFS_I(inode);
503 struct nfs_page *req;
504 unsigned long idx_end, next;
505 unsigned int res = 0;
511 idx_end = idx_start + npages - 1;
513 spin_lock(&nfs_wreq_lock);
515 while (radix_tree_gang_lookup(&nfsi->nfs_page_tree, (void **)&req, next, 1)) {
516 if (req->wb_index > idx_end)
519 next = req->wb_index + 1;
520 if (!NFS_WBACK_BUSY(req))
524 spin_unlock(&nfs_wreq_lock);
525 error = nfs_wait_on_request(req);
526 nfs_release_request(req);
529 spin_lock(&nfs_wreq_lock);
532 spin_unlock(&nfs_wreq_lock);
537 * nfs_scan_dirty - Scan an inode for dirty requests
538 * @inode: NFS inode to scan
539 * @dst: destination list
540 * @idx_start: lower bound of page->index to scan.
541 * @npages: idx_start + npages sets the upper bound to scan.
543 * Moves requests from the inode's dirty page list.
544 * The requests are *not* checked to ensure that they form a contiguous set.
547 nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
549 struct nfs_inode *nfsi = NFS_I(inode);
551 res = nfs_scan_list(&nfsi->dirty, dst, idx_start, npages);
553 sub_page_state(nr_dirty,res);
554 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
555 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
559 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
561 * nfs_scan_commit - Scan an inode for commit requests
562 * @inode: NFS inode to scan
563 * @dst: destination list
564 * @idx_start: lower bound of page->index to scan.
565 * @npages: idx_start + npages sets the upper bound to scan.
567 * Moves requests from the inode's 'commit' request list.
568 * The requests are *not* checked to ensure that they form a contiguous set.
571 nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
573 struct nfs_inode *nfsi = NFS_I(inode);
575 res = nfs_scan_list(&nfsi->commit, dst, idx_start, npages);
576 nfsi->ncommit -= res;
577 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
578 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
583 static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
585 struct backing_dev_info *bdi = mapping->backing_dev_info;
591 if (!bdi_write_congested(bdi))
594 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
597 rpc_clnt_sigmask(clnt, &oldset);
598 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
599 if (bdi_write_congested(bdi)) {
605 rpc_clnt_sigunmask(clnt, &oldset);
607 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
608 if (bdi_write_congested(bdi))
611 finish_wait(&nfs_write_congestion, &wait);
617 * Try to update any existing write request, or create one if there is none.
618 * In order to match, the request's credentials must match those of
619 * the calling process.
621 * Note: Should always be called with the Page Lock held!
623 static struct nfs_page *
624 nfs_update_request(struct file* file, struct inode *inode, struct page *page,
625 unsigned int offset, unsigned int bytes)
627 struct nfs_server *server = NFS_SERVER(inode);
628 struct nfs_page *req, *new = NULL;
629 unsigned long rqend, end;
631 end = offset + bytes;
633 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
634 return ERR_PTR(-ERESTARTSYS);
636 /* Loop over all inode entries and see if we find
637 * A request for the page we wish to update
639 spin_lock(&nfs_wreq_lock);
640 req = _nfs_find_request(inode, page->index);
642 if (!nfs_lock_request_dontget(req)) {
644 spin_unlock(&nfs_wreq_lock);
645 error = nfs_wait_on_request(req);
646 nfs_release_request(req);
648 return ERR_PTR(error);
651 spin_unlock(&nfs_wreq_lock);
653 nfs_release_request(new);
659 nfs_lock_request_dontget(new);
660 error = nfs_inode_add_request(inode, new);
662 spin_unlock(&nfs_wreq_lock);
663 nfs_unlock_request(new);
664 return ERR_PTR(error);
666 spin_unlock(&nfs_wreq_lock);
667 nfs_mark_request_dirty(new);
670 spin_unlock(&nfs_wreq_lock);
672 new = nfs_create_request(file, inode, page, offset, bytes);
681 /* We have a request for our page.
682 * If the creds don't match, or the
683 * page addresses don't match,
684 * tell the caller to wait on the conflicting
687 rqend = req->wb_offset + req->wb_bytes;
688 if (req->wb_file != file
689 || req->wb_page != page
690 || !nfs_dirty_request(req)
691 || offset > rqend || end < req->wb_offset) {
692 nfs_unlock_request(req);
693 return ERR_PTR(-EBUSY);
696 /* Okay, the request matches. Update the region */
697 if (offset < req->wb_offset) {
698 req->wb_offset = offset;
699 req->wb_pgbase = offset;
700 req->wb_bytes = rqend - req->wb_offset;
704 req->wb_bytes = end - req->wb_offset;
710 nfs_flush_incompatible(struct file *file, struct page *page)
712 struct inode *inode = page->mapping->host;
713 struct nfs_page *req;
716 * Look for a request corresponding to this page. If there
717 * is one, and it belongs to another file, we flush it out
718 * before we try to copy anything into the page. Do this
719 * due to the lack of an ACCESS-type call in NFSv2.
720 * Also do the same if we find a request from an existing
723 req = nfs_find_request(inode, page->index);
725 if (!NFS_PROTO(inode)->request_compatible(req, file, page))
726 status = nfs_wb_page(inode, page);
727 nfs_release_request(req);
729 return (status < 0) ? status : 0;
733 * Update and possibly write a cached page of an NFS file.
735 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
736 * things with a page scheduled for an RPC call (e.g. invalidate it).
738 int nfs_updatepage(struct file *file, struct page *page,
739 unsigned int offset, unsigned int count)
741 struct dentry *dentry = file->f_dentry;
742 struct inode *inode = page->mapping->host;
743 struct nfs_page *req;
746 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
747 dentry->d_parent->d_name.name, dentry->d_name.name,
748 count, (long long)(page_offset(page) +offset));
750 if (IS_SYNC(inode)) {
751 status = nfs_writepage_sync(file, inode, page, offset, count, 0);
753 if (offset == 0 && status == PAGE_CACHE_SIZE)
754 SetPageUptodate(page);
760 /* If we're not using byte range locks, and we know the page
761 * is entirely in cache, it may be more efficient to avoid
762 * fragmenting write requests.
764 if (PageUptodate(page) && inode->i_flock == NULL) {
765 loff_t end_offs = i_size_read(inode) - 1;
766 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
770 if (unlikely(end_offs < 0)) {
772 } else if (page->index == end_index) {
774 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
777 } else if (page->index < end_index)
778 count = PAGE_CACHE_SIZE;
782 * Try to find an NFS request corresponding to this page
784 * If the existing request cannot be updated, we must flush
788 req = nfs_update_request(file, inode, page, offset, count);
789 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
790 if (status != -EBUSY)
792 /* Request could not be updated. Flush it out and try again */
793 status = nfs_wb_page(inode, page);
794 } while (status >= 0);
800 /* Update file length */
801 nfs_grow_file(page, offset, count);
802 /* Set the PG_uptodate flag? */
803 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
804 nfs_unlock_request(req);
806 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
807 status, (long long)i_size_read(inode));
809 ClearPageUptodate(page);
813 static void nfs_writepage_release(struct nfs_page *req)
815 end_page_writeback(req->wb_page);
817 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
818 if (!PageError(req->wb_page)) {
819 if (NFS_NEED_RESCHED(req)) {
820 nfs_mark_request_dirty(req);
822 } else if (NFS_NEED_COMMIT(req)) {
823 nfs_mark_request_commit(req);
827 nfs_inode_remove_request(req);
830 nfs_clear_commit(req);
831 nfs_clear_reschedule(req);
833 nfs_inode_remove_request(req);
835 nfs_unlock_request(req);
838 static inline int flush_task_priority(int how)
840 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
842 return RPC_PRIORITY_HIGH;
844 return RPC_PRIORITY_LOW;
846 return RPC_PRIORITY_NORMAL;
850 * Set up the argument/result storage required for the RPC call.
852 static void nfs_write_rpcsetup(struct nfs_page *req,
853 struct nfs_write_data *data,
854 unsigned int count, unsigned int offset,
857 struct rpc_task *task = &data->task;
860 /* Set up the RPC argument and reply structs
861 * NB: take care not to mess about with data->commit et al. */
864 data->inode = inode = req->wb_inode;
865 data->cred = req->wb_cred;
867 data->args.fh = NFS_FH(inode);
868 data->args.offset = req_offset(req) + offset;
869 data->args.pgbase = req->wb_pgbase + offset;
870 data->args.pages = data->pagevec;
871 data->args.count = count;
872 data->args.lockowner = req->wb_lockowner;
873 data->args.state = req->wb_state;
875 data->res.fattr = &data->fattr;
876 data->res.count = count;
877 data->res.verf = &data->verf;
879 NFS_PROTO(inode)->write_setup(data, how);
881 data->task.tk_priority = flush_task_priority(how);
882 data->task.tk_cookie = (unsigned long)inode;
883 data->task.tk_calldata = data;
884 /* Release requests */
885 data->task.tk_release = nfs_writedata_release;
887 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
890 (long long)NFS_FILEID(inode),
892 (unsigned long long)data->args.offset);
895 static void nfs_execute_write(struct nfs_write_data *data)
897 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
900 rpc_clnt_sigmask(clnt, &oldset);
902 rpc_execute(&data->task);
904 rpc_clnt_sigunmask(clnt, &oldset);
908 * Generate multiple small requests to write out a single
909 * contiguous dirty area on one page.
911 static int nfs_flush_multi(struct list_head *head, struct inode *inode, int how)
913 struct nfs_page *req = nfs_list_entry(head->next);
914 struct page *page = req->wb_page;
915 struct nfs_write_data *data;
916 unsigned int wsize = NFS_SERVER(inode)->wsize;
917 unsigned int nbytes, offset;
921 nfs_list_remove_request(req);
923 nbytes = req->wb_bytes;
925 data = nfs_writedata_alloc();
928 list_add(&data->pages, &list);
934 atomic_set(&req->wb_complete, requests);
936 ClearPageError(page);
937 SetPageWriteback(page);
939 nbytes = req->wb_bytes;
941 data = list_entry(list.next, struct nfs_write_data, pages);
942 list_del_init(&data->pages);
944 data->pagevec[0] = page;
945 data->complete = nfs_writeback_done_partial;
947 if (nbytes > wsize) {
948 nfs_write_rpcsetup(req, data, wsize, offset, how);
952 nfs_write_rpcsetup(req, data, nbytes, offset, how);
955 nfs_execute_write(data);
956 } while (nbytes != 0);
961 while (!list_empty(&list)) {
962 data = list_entry(list.next, struct nfs_write_data, pages);
963 list_del(&data->pages);
964 nfs_writedata_free(data);
966 nfs_mark_request_dirty(req);
967 nfs_unlock_request(req);
972 * Create an RPC task for the given write request and kick it.
973 * The page must have been locked by the caller.
975 * It may happen that the page we're passed is not marked dirty.
976 * This is the case if nfs_updatepage detects a conflicting request
977 * that has been written but not committed.
979 static int nfs_flush_one(struct list_head *head, struct inode *inode, int how)
981 struct nfs_page *req;
983 struct nfs_write_data *data;
986 if (NFS_SERVER(inode)->wsize < PAGE_CACHE_SIZE)
987 return nfs_flush_multi(head, inode, how);
989 data = nfs_writedata_alloc();
993 pages = data->pagevec;
995 while (!list_empty(head)) {
996 req = nfs_list_entry(head->next);
997 nfs_list_remove_request(req);
998 nfs_list_add_request(req, &data->pages);
999 ClearPageError(req->wb_page);
1000 SetPageWriteback(req->wb_page);
1001 *pages++ = req->wb_page;
1002 count += req->wb_bytes;
1004 req = nfs_list_entry(data->pages.next);
1006 data->complete = nfs_writeback_done_full;
1007 /* Set up the argument struct */
1008 nfs_write_rpcsetup(req, data, count, 0, how);
1010 nfs_execute_write(data);
1013 while (!list_empty(head)) {
1014 struct nfs_page *req = nfs_list_entry(head->next);
1015 nfs_list_remove_request(req);
1016 nfs_mark_request_dirty(req);
1017 nfs_unlock_request(req);
1023 nfs_flush_list(struct list_head *head, int wpages, int how)
1025 LIST_HEAD(one_request);
1026 struct nfs_page *req;
1028 unsigned int pages = 0;
1030 while (!list_empty(head)) {
1031 pages += nfs_coalesce_requests(head, &one_request, wpages);
1032 req = nfs_list_entry(one_request.next);
1033 error = nfs_flush_one(&one_request, req->wb_inode, how);
1040 while (!list_empty(head)) {
1041 req = nfs_list_entry(head->next);
1042 nfs_list_remove_request(req);
1043 nfs_mark_request_dirty(req);
1044 nfs_unlock_request(req);
1050 * Handle a write reply that flushed part of a page.
1052 static void nfs_writeback_done_partial(struct nfs_write_data *data, int status)
1054 struct nfs_page *req = data->req;
1055 struct page *page = req->wb_page;
1057 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1058 req->wb_inode->i_sb->s_id,
1059 (long long)NFS_FILEID(req->wb_inode),
1061 (long long)req_offset(req));
1064 ClearPageUptodate(page);
1067 req->wb_file->f_error = status;
1068 dprintk(", error = %d\n", status);
1070 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1071 if (data->verf.committed < NFS_FILE_SYNC) {
1072 if (!NFS_NEED_COMMIT(req)) {
1073 nfs_defer_commit(req);
1074 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1075 dprintk(" defer commit\n");
1076 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1077 nfs_defer_reschedule(req);
1078 dprintk(" server reboot detected\n");
1085 if (atomic_dec_and_test(&req->wb_complete))
1086 nfs_writepage_release(req);
1090 * Handle a write reply that flushes a whole page.
1092 * FIXME: There is an inherent race with invalidate_inode_pages and
1093 * writebacks since the page->count is kept > 1 for as long
1094 * as the page has a write request pending.
1096 static void nfs_writeback_done_full(struct nfs_write_data *data, int status)
1098 struct nfs_page *req;
1101 /* Update attributes as result of writeback. */
1102 while (!list_empty(&data->pages)) {
1103 req = nfs_list_entry(data->pages.next);
1104 nfs_list_remove_request(req);
1105 page = req->wb_page;
1107 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1108 req->wb_inode->i_sb->s_id,
1109 (long long)NFS_FILEID(req->wb_inode),
1111 (long long)req_offset(req));
1114 ClearPageUptodate(page);
1117 req->wb_file->f_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_unlock_request(req);
1143 * This function is called when the WRITE call is complete.
1145 void nfs_writeback_done(struct rpc_task *task)
1147 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_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 less than requested.\n");
1199 complain = jiffies + 300 * HZ;
1201 /* Can't do anything about it except throw an error. */
1202 task->tk_status = -EIO;
1206 * Process the nfs_page list
1208 data->complete(data, task->tk_status);
1212 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1213 static void nfs_commit_release(struct rpc_task *task)
1215 struct nfs_write_data *wdata = (struct nfs_write_data *)task->tk_calldata;
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 rpc_task *task = &data->task;
1226 struct nfs_page *first, *last;
1227 struct inode *inode;
1228 loff_t start, end, len;
1230 /* Set up the RPC argument and reply structs
1231 * NB: take care not to mess about with data->commit et al. */
1233 list_splice_init(head, &data->pages);
1234 first = nfs_list_entry(data->pages.next);
1235 last = nfs_list_entry(data->pages.prev);
1236 inode = first->wb_inode;
1239 * Determine the offset range of requests in the COMMIT call.
1240 * We rely on the fact that data->pages is an ordered list...
1242 start = req_offset(first);
1243 end = req_offset(last) + last->wb_bytes;
1245 /* If 'len' is not a 32-bit quantity, pass '0' in the COMMIT call */
1246 if (end >= i_size_read(inode) || len < 0 || len > (~((u32)0) >> 1))
1249 data->inode = inode;
1250 data->cred = first->wb_cred;
1252 data->args.fh = NFS_FH(data->inode);
1253 data->args.offset = start;
1254 data->args.count = len;
1255 data->res.count = len;
1256 data->res.fattr = &data->fattr;
1257 data->res.verf = &data->verf;
1259 NFS_PROTO(inode)->commit_setup(data, how);
1261 data->task.tk_priority = flush_task_priority(how);
1262 data->task.tk_cookie = (unsigned long)inode;
1263 data->task.tk_calldata = data;
1264 /* Release requests */
1265 data->task.tk_release = nfs_commit_release;
1267 dprintk("NFS: %4d initiated commit call\n", task->tk_pid);
1271 * Commit dirty pages
1274 nfs_commit_list(struct list_head *head, int how)
1276 struct nfs_write_data *data;
1277 struct nfs_page *req;
1279 data = nfs_commit_alloc();
1284 /* Set up the argument struct */
1285 nfs_commit_rpcsetup(head, data, how);
1287 nfs_execute_write(data);
1290 while (!list_empty(head)) {
1291 req = nfs_list_entry(head->next);
1292 nfs_list_remove_request(req);
1293 nfs_mark_request_commit(req);
1294 nfs_unlock_request(req);
1300 * COMMIT call returned
1303 nfs_commit_done(struct rpc_task *task)
1305 struct nfs_write_data *data = (struct nfs_write_data *)task->tk_calldata;
1306 struct nfs_page *req;
1309 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1310 task->tk_pid, task->tk_status);
1312 while (!list_empty(&data->pages)) {
1313 req = nfs_list_entry(data->pages.next);
1314 nfs_list_remove_request(req);
1316 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1317 req->wb_inode->i_sb->s_id,
1318 (long long)NFS_FILEID(req->wb_inode),
1320 (long long)req_offset(req));
1321 if (task->tk_status < 0) {
1323 req->wb_file->f_error = task->tk_status;
1324 nfs_inode_remove_request(req);
1325 dprintk(", error = %d\n", task->tk_status);
1329 /* Okay, COMMIT succeeded, apparently. Check the verifier
1330 * returned by the server against all stored verfs. */
1331 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1332 /* We have a match */
1333 nfs_inode_remove_request(req);
1337 /* We have a mismatch. Write the page again */
1338 dprintk(" mismatch\n");
1339 nfs_mark_request_dirty(req);
1341 nfs_unlock_request(req);
1344 sub_page_state(nr_unstable,res);
1348 int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
1349 unsigned int npages, int how)
1355 spin_lock(&nfs_wreq_lock);
1356 res = nfs_scan_dirty(inode, &head, idx_start, npages);
1357 spin_unlock(&nfs_wreq_lock);
1359 error = nfs_flush_list(&head, NFS_SERVER(inode)->wpages, how);
1365 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1366 int nfs_commit_inode(struct inode *inode, unsigned long idx_start,
1367 unsigned int npages, int how)
1373 spin_lock(&nfs_wreq_lock);
1374 res = nfs_scan_commit(inode, &head, idx_start, npages);
1376 res += nfs_scan_commit(inode, &head, 0, 0);
1377 spin_unlock(&nfs_wreq_lock);
1378 error = nfs_commit_list(&head, how);
1380 spin_unlock(&nfs_wreq_lock);
1387 int nfs_sync_inode(struct inode *inode, unsigned long idx_start,
1388 unsigned int npages, int how)
1393 wait = how & FLUSH_WAIT;
1399 error = nfs_wait_on_requests(inode, idx_start, npages);
1401 error = nfs_flush_inode(inode, idx_start, npages, how);
1402 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1404 error = nfs_commit_inode(inode, idx_start, npages, how);
1406 } while (error > 0);
1410 int nfs_init_writepagecache(void)
1412 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1413 sizeof(struct nfs_write_data),
1414 0, SLAB_HWCACHE_ALIGN,
1416 if (nfs_wdata_cachep == NULL)
1419 nfs_wdata_mempool = mempool_create(MIN_POOL_WRITE,
1423 if (nfs_wdata_mempool == NULL)
1426 nfs_commit_mempool = mempool_create(MIN_POOL_COMMIT,
1430 if (nfs_commit_mempool == NULL)
1436 void nfs_destroy_writepagecache(void)
1438 mempool_destroy(nfs_commit_mempool);
1439 mempool_destroy(nfs_wdata_mempool);
1440 if (kmem_cache_destroy(nfs_wdata_cachep))
1441 printk(KERN_INFO "nfs_write_data: not all structures were freed\n");