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 nfs_flush_inode(inode, 0, 0, FLUSH_STABLE);
320 err = nfs_writepage_sync(NULL, inode, page, 0,
324 redirty_page_for_writepage(wbc, page);
331 if (inode_referenced)
337 * Note: causes nfs_update_request() to block on the assumption
338 * that the writeback is generated due to memory pressure.
340 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
342 struct backing_dev_info *bdi = mapping->backing_dev_info;
343 struct inode *inode = mapping->host;
346 err = generic_writepages(mapping, wbc);
349 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
350 if (wbc->nonblocking)
352 nfs_wait_on_write_congestion(mapping, 0);
354 err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
357 wbc->nr_to_write -= err;
358 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
359 err = nfs_wait_on_requests(inode, 0, 0);
363 err = nfs_commit_inode(inode, 0, 0, wb_priority(wbc));
365 wbc->nr_to_write -= err;
369 clear_bit(BDI_write_congested, &bdi->state);
370 wake_up_all(&nfs_write_congestion);
375 * Insert a write request into an inode
378 nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
380 struct nfs_inode *nfsi = NFS_I(inode);
383 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
384 BUG_ON(error == -EEXIST);
389 nfs_begin_data_update(inode);
397 * Insert a write request into an inode
400 nfs_inode_remove_request(struct nfs_page *req)
402 struct nfs_inode *nfsi;
405 BUG_ON (!NFS_WBACK_BUSY(req));
406 spin_lock(&nfs_wreq_lock);
407 inode = req->wb_inode;
409 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
412 spin_unlock(&nfs_wreq_lock);
413 nfs_end_data_update_defer(inode);
416 spin_unlock(&nfs_wreq_lock);
417 nfs_clear_request(req);
418 nfs_release_request(req);
424 static inline struct nfs_page *
425 _nfs_find_request(struct inode *inode, unsigned long index)
427 struct nfs_inode *nfsi = NFS_I(inode);
428 struct nfs_page *req;
430 req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
436 static struct nfs_page *
437 nfs_find_request(struct inode *inode, unsigned long index)
439 struct nfs_page *req;
441 spin_lock(&nfs_wreq_lock);
442 req = _nfs_find_request(inode, index);
443 spin_unlock(&nfs_wreq_lock);
448 * Add a request to the inode's dirty list.
451 nfs_mark_request_dirty(struct nfs_page *req)
453 struct inode *inode = req->wb_inode;
454 struct nfs_inode *nfsi = NFS_I(inode);
456 spin_lock(&nfs_wreq_lock);
457 nfs_list_add_request(req, &nfsi->dirty);
459 spin_unlock(&nfs_wreq_lock);
460 inc_page_state(nr_dirty);
461 mark_inode_dirty(inode);
465 * Check if a request is dirty
468 nfs_dirty_request(struct nfs_page *req)
470 struct nfs_inode *nfsi = NFS_I(req->wb_inode);
471 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
474 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
476 * Add a request to the inode's commit list.
479 nfs_mark_request_commit(struct nfs_page *req)
481 struct inode *inode = req->wb_inode;
482 struct nfs_inode *nfsi = NFS_I(inode);
484 spin_lock(&nfs_wreq_lock);
485 nfs_list_add_request(req, &nfsi->commit);
487 spin_unlock(&nfs_wreq_lock);
488 inc_page_state(nr_unstable);
489 mark_inode_dirty(inode);
494 * Wait for a request to complete.
496 * Interruptible by signals only if mounted with intr flag.
499 nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
501 struct nfs_inode *nfsi = NFS_I(inode);
502 struct nfs_page *req;
503 unsigned long idx_end, next;
504 unsigned int res = 0;
510 idx_end = idx_start + npages - 1;
512 spin_lock(&nfs_wreq_lock);
514 while (radix_tree_gang_lookup(&nfsi->nfs_page_tree, (void **)&req, next, 1)) {
515 if (req->wb_index > idx_end)
518 next = req->wb_index + 1;
519 if (!NFS_WBACK_BUSY(req))
523 spin_unlock(&nfs_wreq_lock);
524 error = nfs_wait_on_request(req);
525 nfs_release_request(req);
528 spin_lock(&nfs_wreq_lock);
531 spin_unlock(&nfs_wreq_lock);
536 * nfs_scan_dirty - Scan an inode for dirty requests
537 * @inode: NFS inode to scan
538 * @dst: destination list
539 * @idx_start: lower bound of page->index to scan.
540 * @npages: idx_start + npages sets the upper bound to scan.
542 * Moves requests from the inode's dirty page list.
543 * The requests are *not* checked to ensure that they form a contiguous set.
546 nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
548 struct nfs_inode *nfsi = NFS_I(inode);
550 res = nfs_scan_list(&nfsi->dirty, dst, idx_start, npages);
552 sub_page_state(nr_dirty,res);
553 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
554 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
558 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
560 * nfs_scan_commit - Scan an inode for commit requests
561 * @inode: NFS inode to scan
562 * @dst: destination list
563 * @idx_start: lower bound of page->index to scan.
564 * @npages: idx_start + npages sets the upper bound to scan.
566 * Moves requests from the inode's 'commit' request list.
567 * The requests are *not* checked to ensure that they form a contiguous set.
570 nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
572 struct nfs_inode *nfsi = NFS_I(inode);
574 res = nfs_scan_list(&nfsi->commit, dst, idx_start, npages);
575 nfsi->ncommit -= res;
576 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
577 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
582 static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
584 struct backing_dev_info *bdi = mapping->backing_dev_info;
590 if (!bdi_write_congested(bdi))
593 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
596 rpc_clnt_sigmask(clnt, &oldset);
597 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
598 if (bdi_write_congested(bdi)) {
604 rpc_clnt_sigunmask(clnt, &oldset);
606 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
607 if (bdi_write_congested(bdi))
610 finish_wait(&nfs_write_congestion, &wait);
616 * Try to update any existing write request, or create one if there is none.
617 * In order to match, the request's credentials must match those of
618 * the calling process.
620 * Note: Should always be called with the Page Lock held!
622 static struct nfs_page *
623 nfs_update_request(struct file* file, struct inode *inode, struct page *page,
624 unsigned int offset, unsigned int bytes)
626 struct nfs_server *server = NFS_SERVER(inode);
627 struct nfs_page *req, *new = NULL;
628 unsigned long rqend, end;
630 end = offset + bytes;
632 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
633 return ERR_PTR(-ERESTARTSYS);
635 /* Loop over all inode entries and see if we find
636 * A request for the page we wish to update
638 spin_lock(&nfs_wreq_lock);
639 req = _nfs_find_request(inode, page->index);
641 if (!nfs_lock_request_dontget(req)) {
643 spin_unlock(&nfs_wreq_lock);
644 error = nfs_wait_on_request(req);
645 nfs_release_request(req);
647 return ERR_PTR(error);
650 spin_unlock(&nfs_wreq_lock);
652 nfs_release_request(new);
658 nfs_lock_request_dontget(new);
659 error = nfs_inode_add_request(inode, new);
661 spin_unlock(&nfs_wreq_lock);
662 nfs_unlock_request(new);
663 return ERR_PTR(error);
665 spin_unlock(&nfs_wreq_lock);
666 nfs_mark_request_dirty(new);
669 spin_unlock(&nfs_wreq_lock);
671 new = nfs_create_request(file, inode, page, offset, bytes);
680 /* We have a request for our page.
681 * If the creds don't match, or the
682 * page addresses don't match,
683 * tell the caller to wait on the conflicting
686 rqend = req->wb_offset + req->wb_bytes;
687 if (req->wb_file != file
688 || req->wb_page != page
689 || !nfs_dirty_request(req)
690 || offset > rqend || end < req->wb_offset) {
691 nfs_unlock_request(req);
692 return ERR_PTR(-EBUSY);
695 /* Okay, the request matches. Update the region */
696 if (offset < req->wb_offset) {
697 req->wb_offset = offset;
698 req->wb_pgbase = offset;
699 req->wb_bytes = rqend - req->wb_offset;
703 req->wb_bytes = end - req->wb_offset;
709 nfs_flush_incompatible(struct file *file, struct page *page)
711 struct inode *inode = page->mapping->host;
712 struct nfs_page *req;
715 * Look for a request corresponding to this page. If there
716 * is one, and it belongs to another file, we flush it out
717 * before we try to copy anything into the page. Do this
718 * due to the lack of an ACCESS-type call in NFSv2.
719 * Also do the same if we find a request from an existing
722 req = nfs_find_request(inode, page->index);
724 if (!NFS_PROTO(inode)->request_compatible(req, file, page))
725 status = nfs_wb_page(inode, page);
726 nfs_release_request(req);
728 return (status < 0) ? status : 0;
732 * Update and possibly write a cached page of an NFS file.
734 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
735 * things with a page scheduled for an RPC call (e.g. invalidate it).
737 int nfs_updatepage(struct file *file, struct page *page,
738 unsigned int offset, unsigned int count)
740 struct dentry *dentry = file->f_dentry;
741 struct inode *inode = page->mapping->host;
742 struct nfs_page *req;
745 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
746 dentry->d_parent->d_name.name, dentry->d_name.name,
747 count, (long long)(page_offset(page) +offset));
749 if (IS_SYNC(inode)) {
750 status = nfs_writepage_sync(file, inode, page, offset, count, 0);
752 if (offset == 0 && status == PAGE_CACHE_SIZE)
753 SetPageUptodate(page);
759 /* If we're not using byte range locks, and we know the page
760 * is entirely in cache, it may be more efficient to avoid
761 * fragmenting write requests.
763 if (PageUptodate(page) && inode->i_flock == NULL) {
764 loff_t end_offs = i_size_read(inode) - 1;
765 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
769 if (unlikely(end_offs < 0)) {
771 } else if (page->index == end_index) {
773 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
776 } else if (page->index < end_index)
777 count = PAGE_CACHE_SIZE;
781 * Try to find an NFS request corresponding to this page
783 * If the existing request cannot be updated, we must flush
787 req = nfs_update_request(file, inode, page, offset, count);
788 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
789 if (status != -EBUSY)
791 /* Request could not be updated. Flush it out and try again */
792 status = nfs_wb_page(inode, page);
793 } while (status >= 0);
799 /* Update file length */
800 nfs_grow_file(page, offset, count);
801 /* Set the PG_uptodate flag? */
802 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
803 nfs_unlock_request(req);
805 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
806 status, (long long)i_size_read(inode));
808 ClearPageUptodate(page);
812 static void nfs_writepage_release(struct nfs_page *req)
814 end_page_writeback(req->wb_page);
816 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
817 if (!PageError(req->wb_page)) {
818 if (NFS_NEED_RESCHED(req)) {
819 nfs_mark_request_dirty(req);
821 } else if (NFS_NEED_COMMIT(req)) {
822 nfs_mark_request_commit(req);
826 nfs_inode_remove_request(req);
829 nfs_clear_commit(req);
830 nfs_clear_reschedule(req);
832 nfs_inode_remove_request(req);
834 nfs_unlock_request(req);
837 static inline int flush_task_priority(int how)
839 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
841 return RPC_PRIORITY_HIGH;
843 return RPC_PRIORITY_LOW;
845 return RPC_PRIORITY_NORMAL;
849 * Set up the argument/result storage required for the RPC call.
851 static void nfs_write_rpcsetup(struct nfs_page *req,
852 struct nfs_write_data *data,
853 unsigned int count, unsigned int offset,
856 struct rpc_task *task = &data->task;
859 /* Set up the RPC argument and reply structs
860 * NB: take care not to mess about with data->commit et al. */
863 data->inode = inode = req->wb_inode;
864 data->cred = req->wb_cred;
866 data->args.fh = NFS_FH(inode);
867 data->args.offset = req_offset(req) + offset;
868 data->args.pgbase = req->wb_pgbase + offset;
869 data->args.pages = data->pagevec;
870 data->args.count = count;
871 data->args.lockowner = req->wb_lockowner;
872 data->args.state = req->wb_state;
874 data->res.fattr = &data->fattr;
875 data->res.count = count;
876 data->res.verf = &data->verf;
878 NFS_PROTO(inode)->write_setup(data, how);
880 data->task.tk_priority = flush_task_priority(how);
881 data->task.tk_cookie = (unsigned long)inode;
882 data->task.tk_calldata = data;
883 /* Release requests */
884 data->task.tk_release = nfs_writedata_release;
886 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
889 (long long)NFS_FILEID(inode),
891 (unsigned long long)data->args.offset);
894 static void nfs_execute_write(struct nfs_write_data *data)
896 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
899 rpc_clnt_sigmask(clnt, &oldset);
901 rpc_execute(&data->task);
903 rpc_clnt_sigunmask(clnt, &oldset);
907 * Generate multiple small requests to write out a single
908 * contiguous dirty area on one page.
910 static int nfs_flush_multi(struct list_head *head, struct inode *inode, int how)
912 struct nfs_page *req = nfs_list_entry(head->next);
913 struct page *page = req->wb_page;
914 struct nfs_write_data *data;
915 unsigned int wsize = NFS_SERVER(inode)->wsize;
916 unsigned int nbytes, offset;
920 nfs_list_remove_request(req);
922 nbytes = req->wb_bytes;
924 data = nfs_writedata_alloc();
927 list_add(&data->pages, &list);
933 atomic_set(&req->wb_complete, requests);
935 ClearPageError(page);
936 SetPageWriteback(page);
938 nbytes = req->wb_bytes;
940 data = list_entry(list.next, struct nfs_write_data, pages);
941 list_del_init(&data->pages);
943 data->pagevec[0] = page;
944 data->complete = nfs_writeback_done_partial;
946 if (nbytes > wsize) {
947 nfs_write_rpcsetup(req, data, wsize, offset, how);
951 nfs_write_rpcsetup(req, data, nbytes, offset, how);
954 nfs_execute_write(data);
955 } while (nbytes != 0);
960 while (!list_empty(&list)) {
961 data = list_entry(list.next, struct nfs_write_data, pages);
962 list_del(&data->pages);
963 nfs_writedata_free(data);
965 nfs_mark_request_dirty(req);
966 nfs_unlock_request(req);
971 * Create an RPC task for the given write request and kick it.
972 * The page must have been locked by the caller.
974 * It may happen that the page we're passed is not marked dirty.
975 * This is the case if nfs_updatepage detects a conflicting request
976 * that has been written but not committed.
978 static int nfs_flush_one(struct list_head *head, struct inode *inode, int how)
980 struct nfs_page *req;
982 struct nfs_write_data *data;
985 if (NFS_SERVER(inode)->wsize < PAGE_CACHE_SIZE)
986 return nfs_flush_multi(head, inode, how);
988 data = nfs_writedata_alloc();
992 pages = data->pagevec;
994 while (!list_empty(head)) {
995 req = nfs_list_entry(head->next);
996 nfs_list_remove_request(req);
997 nfs_list_add_request(req, &data->pages);
998 ClearPageError(req->wb_page);
999 SetPageWriteback(req->wb_page);
1000 *pages++ = req->wb_page;
1001 count += req->wb_bytes;
1003 req = nfs_list_entry(data->pages.next);
1005 data->complete = nfs_writeback_done_full;
1006 /* Set up the argument struct */
1007 nfs_write_rpcsetup(req, data, count, 0, how);
1009 nfs_execute_write(data);
1012 while (!list_empty(head)) {
1013 struct nfs_page *req = nfs_list_entry(head->next);
1014 nfs_list_remove_request(req);
1015 nfs_mark_request_dirty(req);
1016 nfs_unlock_request(req);
1022 nfs_flush_list(struct list_head *head, int wpages, int how)
1024 LIST_HEAD(one_request);
1025 struct nfs_page *req;
1027 unsigned int pages = 0;
1029 while (!list_empty(head)) {
1030 pages += nfs_coalesce_requests(head, &one_request, wpages);
1031 req = nfs_list_entry(one_request.next);
1032 error = nfs_flush_one(&one_request, req->wb_inode, how);
1039 while (!list_empty(head)) {
1040 req = nfs_list_entry(head->next);
1041 nfs_list_remove_request(req);
1042 nfs_mark_request_dirty(req);
1043 nfs_unlock_request(req);
1049 * Handle a write reply that flushed part of a page.
1051 static void nfs_writeback_done_partial(struct nfs_write_data *data, int status)
1053 struct nfs_page *req = data->req;
1054 struct page *page = req->wb_page;
1056 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1057 req->wb_inode->i_sb->s_id,
1058 (long long)NFS_FILEID(req->wb_inode),
1060 (long long)req_offset(req));
1063 ClearPageUptodate(page);
1066 req->wb_file->f_error = status;
1067 dprintk(", error = %d\n", status);
1069 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1070 if (data->verf.committed < NFS_FILE_SYNC) {
1071 if (!NFS_NEED_COMMIT(req)) {
1072 nfs_defer_commit(req);
1073 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1074 dprintk(" defer commit\n");
1075 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1076 nfs_defer_reschedule(req);
1077 dprintk(" server reboot detected\n");
1084 if (atomic_dec_and_test(&req->wb_complete))
1085 nfs_writepage_release(req);
1089 * Handle a write reply that flushes a whole page.
1091 * FIXME: There is an inherent race with invalidate_inode_pages and
1092 * writebacks since the page->count is kept > 1 for as long
1093 * as the page has a write request pending.
1095 static void nfs_writeback_done_full(struct nfs_write_data *data, int status)
1097 struct nfs_page *req;
1100 /* Update attributes as result of writeback. */
1101 while (!list_empty(&data->pages)) {
1102 req = nfs_list_entry(data->pages.next);
1103 nfs_list_remove_request(req);
1104 page = req->wb_page;
1106 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1107 req->wb_inode->i_sb->s_id,
1108 (long long)NFS_FILEID(req->wb_inode),
1110 (long long)req_offset(req));
1113 ClearPageUptodate(page);
1116 req->wb_file->f_error = status;
1117 end_page_writeback(page);
1118 nfs_inode_remove_request(req);
1119 dprintk(", error = %d\n", status);
1122 end_page_writeback(page);
1124 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1125 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1126 nfs_inode_remove_request(req);
1130 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1131 nfs_mark_request_commit(req);
1132 dprintk(" marked for commit\n");
1134 nfs_inode_remove_request(req);
1137 nfs_unlock_request(req);
1142 * This function is called when the WRITE call is complete.
1144 void nfs_writeback_done(struct rpc_task *task)
1146 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
1147 struct nfs_writeargs *argp = &data->args;
1148 struct nfs_writeres *resp = &data->res;
1150 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1151 task->tk_pid, task->tk_status);
1153 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1154 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1155 /* We tried a write call, but the server did not
1156 * commit data to stable storage even though we
1158 * Note: There is a known bug in Tru64 < 5.0 in which
1159 * the server reports NFS_DATA_SYNC, but performs
1160 * NFS_FILE_SYNC. We therefore implement this checking
1161 * as a dprintk() in order to avoid filling syslog.
1163 static unsigned long complain;
1165 if (time_before(complain, jiffies)) {
1166 dprintk("NFS: faulty NFS server %s:"
1167 " (committed = %d) != (stable = %d)\n",
1168 NFS_SERVER(data->inode)->hostname,
1169 resp->verf->committed, argp->stable);
1170 complain = jiffies + 300 * HZ;
1174 /* Is this a short write? */
1175 if (task->tk_status >= 0 && resp->count < argp->count) {
1176 static unsigned long complain;
1178 /* Has the server at least made some progress? */
1179 if (resp->count != 0) {
1180 /* Was this an NFSv2 write or an NFSv3 stable write? */
1181 if (resp->verf->committed != NFS_UNSTABLE) {
1182 /* Resend from where the server left off */
1183 argp->offset += resp->count;
1184 argp->pgbase += resp->count;
1185 argp->count -= resp->count;
1187 /* Resend as a stable write in order to avoid
1188 * headaches in the case of a server crash.
1190 argp->stable = NFS_FILE_SYNC;
1192 rpc_restart_call(task);
1195 if (time_before(complain, jiffies)) {
1197 "NFS: Server wrote less than requested.\n");
1198 complain = jiffies + 300 * HZ;
1200 /* Can't do anything about it except throw an error. */
1201 task->tk_status = -EIO;
1205 * Process the nfs_page list
1207 data->complete(data, task->tk_status);
1211 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1212 static void nfs_commit_release(struct rpc_task *task)
1214 struct nfs_write_data *wdata = (struct nfs_write_data *)task->tk_calldata;
1215 nfs_commit_free(wdata);
1219 * Set up the argument/result storage required for the RPC call.
1221 static void nfs_commit_rpcsetup(struct list_head *head,
1222 struct nfs_write_data *data, int how)
1224 struct rpc_task *task = &data->task;
1225 struct nfs_page *first, *last;
1226 struct inode *inode;
1227 loff_t start, end, len;
1229 /* Set up the RPC argument and reply structs
1230 * NB: take care not to mess about with data->commit et al. */
1232 list_splice_init(head, &data->pages);
1233 first = nfs_list_entry(data->pages.next);
1234 last = nfs_list_entry(data->pages.prev);
1235 inode = first->wb_inode;
1238 * Determine the offset range of requests in the COMMIT call.
1239 * We rely on the fact that data->pages is an ordered list...
1241 start = req_offset(first);
1242 end = req_offset(last) + last->wb_bytes;
1244 /* If 'len' is not a 32-bit quantity, pass '0' in the COMMIT call */
1245 if (end >= i_size_read(inode) || len < 0 || len > (~((u32)0) >> 1))
1248 data->inode = inode;
1249 data->cred = first->wb_cred;
1251 data->args.fh = NFS_FH(data->inode);
1252 data->args.offset = start;
1253 data->args.count = len;
1254 data->res.count = len;
1255 data->res.fattr = &data->fattr;
1256 data->res.verf = &data->verf;
1258 NFS_PROTO(inode)->commit_setup(data, how);
1260 data->task.tk_priority = flush_task_priority(how);
1261 data->task.tk_cookie = (unsigned long)inode;
1262 data->task.tk_calldata = data;
1263 /* Release requests */
1264 data->task.tk_release = nfs_commit_release;
1266 dprintk("NFS: %4d initiated commit call\n", task->tk_pid);
1270 * Commit dirty pages
1273 nfs_commit_list(struct list_head *head, int how)
1275 struct nfs_write_data *data;
1276 struct nfs_page *req;
1278 data = nfs_commit_alloc();
1283 /* Set up the argument struct */
1284 nfs_commit_rpcsetup(head, data, how);
1286 nfs_execute_write(data);
1289 while (!list_empty(head)) {
1290 req = nfs_list_entry(head->next);
1291 nfs_list_remove_request(req);
1292 nfs_mark_request_commit(req);
1293 nfs_unlock_request(req);
1299 * COMMIT call returned
1302 nfs_commit_done(struct rpc_task *task)
1304 struct nfs_write_data *data = (struct nfs_write_data *)task->tk_calldata;
1305 struct nfs_page *req;
1308 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1309 task->tk_pid, task->tk_status);
1311 while (!list_empty(&data->pages)) {
1312 req = nfs_list_entry(data->pages.next);
1313 nfs_list_remove_request(req);
1315 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1316 req->wb_inode->i_sb->s_id,
1317 (long long)NFS_FILEID(req->wb_inode),
1319 (long long)req_offset(req));
1320 if (task->tk_status < 0) {
1322 req->wb_file->f_error = task->tk_status;
1323 nfs_inode_remove_request(req);
1324 dprintk(", error = %d\n", task->tk_status);
1328 /* Okay, COMMIT succeeded, apparently. Check the verifier
1329 * returned by the server against all stored verfs. */
1330 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1331 /* We have a match */
1332 nfs_inode_remove_request(req);
1336 /* We have a mismatch. Write the page again */
1337 dprintk(" mismatch\n");
1338 nfs_mark_request_dirty(req);
1340 nfs_unlock_request(req);
1343 sub_page_state(nr_unstable,res);
1347 int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
1348 unsigned int npages, int how)
1354 spin_lock(&nfs_wreq_lock);
1355 res = nfs_scan_dirty(inode, &head, idx_start, npages);
1356 spin_unlock(&nfs_wreq_lock);
1358 error = nfs_flush_list(&head, NFS_SERVER(inode)->wpages, how);
1364 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1365 int nfs_commit_inode(struct inode *inode, unsigned long idx_start,
1366 unsigned int npages, int how)
1372 spin_lock(&nfs_wreq_lock);
1373 res = nfs_scan_commit(inode, &head, idx_start, npages);
1375 res += nfs_scan_commit(inode, &head, 0, 0);
1376 spin_unlock(&nfs_wreq_lock);
1377 error = nfs_commit_list(&head, how);
1379 spin_unlock(&nfs_wreq_lock);
1386 int nfs_sync_inode(struct inode *inode, unsigned long idx_start,
1387 unsigned int npages, int how)
1392 wait = how & FLUSH_WAIT;
1398 error = nfs_wait_on_requests(inode, idx_start, npages);
1400 error = nfs_flush_inode(inode, idx_start, npages, how);
1401 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1403 error = nfs_commit_inode(inode, idx_start, npages, how);
1405 } while (error > 0);
1409 int nfs_init_writepagecache(void)
1411 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1412 sizeof(struct nfs_write_data),
1413 0, SLAB_HWCACHE_ALIGN,
1415 if (nfs_wdata_cachep == NULL)
1418 nfs_wdata_mempool = mempool_create(MIN_POOL_WRITE,
1422 if (nfs_wdata_mempool == NULL)
1425 nfs_commit_mempool = mempool_create(MIN_POOL_COMMIT,
1429 if (nfs_commit_mempool == NULL)
1435 void nfs_destroy_writepagecache(void)
1437 mempool_destroy(nfs_commit_mempool);
1438 mempool_destroy(nfs_wdata_mempool);
1439 if (kmem_cache_destroy(nfs_wdata_cachep))
1440 printk(KERN_INFO "nfs_write_data: not all structures were freed\n");