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"
68 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
70 #define MIN_POOL_WRITE (32)
71 #define MIN_POOL_COMMIT (4)
74 * Local function declarations
76 static struct nfs_page * nfs_update_request(struct nfs_open_context*,
79 unsigned int, unsigned int);
80 static int nfs_wait_on_write_congestion(struct address_space *, int);
81 static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
82 static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
83 unsigned int npages, int how);
84 static const struct rpc_call_ops nfs_write_partial_ops;
85 static const struct rpc_call_ops nfs_write_full_ops;
86 static const struct rpc_call_ops nfs_commit_ops;
88 static kmem_cache_t *nfs_wdata_cachep;
89 mempool_t *nfs_wdata_mempool;
90 static mempool_t *nfs_commit_mempool;
92 static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
94 struct nfs_write_data *nfs_commit_alloc(unsigned int pagecount)
96 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
99 memset(p, 0, sizeof(*p));
100 INIT_LIST_HEAD(&p->pages);
101 if (pagecount < NFS_PAGEVEC_SIZE)
102 p->pagevec = &p->page_array[0];
104 size_t size = ++pagecount * sizeof(struct page *);
105 p->pagevec = kzalloc(size, GFP_NOFS);
107 mempool_free(p, nfs_commit_mempool);
115 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 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
140 i_size_write(inode, end);
143 /* We can set the PG_uptodate flag if we see that a write request
144 * covers the full page.
146 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
150 if (PageUptodate(page))
154 if (count == PAGE_CACHE_SIZE) {
155 SetPageUptodate(page);
159 end_offs = i_size_read(page->mapping->host) - 1;
162 /* Is this the last page? */
163 if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
165 /* This is the last page: set PG_uptodate if we cover the entire
166 * extent of the data, then zero the rest of the page.
168 if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
169 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
170 SetPageUptodate(page);
175 * Write a page synchronously.
176 * Offset is the data offset within the page.
178 static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
179 struct page *page, unsigned int offset, unsigned int count,
182 unsigned int wsize = NFS_SERVER(inode)->wsize;
183 int result, written = 0;
184 struct nfs_write_data *wdata;
186 wdata = nfs_writedata_alloc(1);
191 wdata->cred = ctx->cred;
192 wdata->inode = inode;
193 wdata->args.fh = NFS_FH(inode);
194 wdata->args.context = ctx;
195 wdata->args.pages = &page;
196 wdata->args.stable = NFS_FILE_SYNC;
197 wdata->args.pgbase = offset;
198 wdata->args.count = wsize;
199 wdata->res.fattr = &wdata->fattr;
200 wdata->res.verf = &wdata->verf;
202 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
204 (long long)NFS_FILEID(inode),
205 count, (long long)(page_offset(page) + offset));
207 set_page_writeback(page);
208 nfs_begin_data_update(inode);
211 wdata->args.count = count;
212 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
214 result = NFS_PROTO(inode)->write(wdata);
217 /* Must mark the page invalid after I/O error */
218 ClearPageUptodate(page);
221 if (result < wdata->args.count)
222 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
223 wdata->args.count, result);
225 wdata->args.offset += result;
226 wdata->args.pgbase += result;
229 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
231 /* Update file length */
232 nfs_grow_file(page, offset, written);
233 /* Set the PG_uptodate flag? */
234 nfs_mark_uptodate(page, offset, written);
237 ClearPageError(page);
240 nfs_end_data_update(inode);
241 end_page_writeback(page);
242 nfs_writedata_free(wdata);
243 return written ? written : result;
246 static int nfs_writepage_async(struct nfs_open_context *ctx,
247 struct inode *inode, struct page *page,
248 unsigned int offset, unsigned int count)
250 struct nfs_page *req;
252 req = nfs_update_request(ctx, inode, page, offset, count);
255 /* Update file length */
256 nfs_grow_file(page, offset, count);
257 /* Set the PG_uptodate flag? */
258 nfs_mark_uptodate(page, offset, count);
259 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 nfs_open_context *ctx;
278 struct inode *inode = page->mapping->host;
279 unsigned long end_index;
280 unsigned offset = PAGE_CACHE_SIZE;
281 loff_t i_size = i_size_read(inode);
282 int inode_referenced = 0;
283 int priority = wb_priority(wbc);
286 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
287 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
290 * Note: We need to ensure that we have a reference to the inode
291 * if we are to do asynchronous writes. If not, waiting
292 * in nfs_wait_on_request() may deadlock with clear_inode().
294 * If igrab() fails here, then it is in any case safe to
295 * call nfs_wb_page(), since there will be no pending writes.
297 if (igrab(inode) != 0)
298 inode_referenced = 1;
299 end_index = i_size >> PAGE_CACHE_SHIFT;
301 /* Ensure we've flushed out any previous writes */
302 nfs_wb_page_priority(inode, page, priority);
305 if (page->index < end_index)
307 /* things got complicated... */
308 offset = i_size & (PAGE_CACHE_SIZE-1);
310 /* OK, are we completely out? */
311 err = 0; /* potential race with truncate - ignore */
312 if (page->index >= end_index+1 || !offset)
315 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
321 if (!IS_SYNC(inode) && inode_referenced) {
322 err = nfs_writepage_async(ctx, inode, page, 0, offset);
323 if (!wbc->for_writepages)
324 nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
326 err = nfs_writepage_sync(ctx, inode, page, 0,
330 redirty_page_for_writepage(wbc, page);
335 put_nfs_open_context(ctx);
338 if (inode_referenced)
344 * Note: causes nfs_update_request() to block on the assumption
345 * that the writeback is generated due to memory pressure.
347 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
349 struct backing_dev_info *bdi = mapping->backing_dev_info;
350 struct inode *inode = mapping->host;
353 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
355 err = generic_writepages(mapping, wbc);
358 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
359 if (wbc->nonblocking)
361 nfs_wait_on_write_congestion(mapping, 0);
363 err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
366 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
367 wbc->nr_to_write -= err;
368 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
369 err = nfs_wait_on_requests(inode, 0, 0);
373 err = nfs_commit_inode(inode, wb_priority(wbc));
375 wbc->nr_to_write -= err;
379 clear_bit(BDI_write_congested, &bdi->state);
380 wake_up_all(&nfs_write_congestion);
385 * Insert a write request into an inode
387 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
389 struct nfs_inode *nfsi = NFS_I(inode);
392 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
393 BUG_ON(error == -EEXIST);
398 nfs_begin_data_update(inode);
399 if (nfs_have_delegation(inode, FMODE_WRITE))
403 atomic_inc(&req->wb_count);
408 * Insert a write request into an inode
410 static void nfs_inode_remove_request(struct nfs_page *req)
412 struct inode *inode = req->wb_context->dentry->d_inode;
413 struct nfs_inode *nfsi = NFS_I(inode);
415 BUG_ON (!NFS_WBACK_BUSY(req));
417 spin_lock(&nfsi->req_lock);
418 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
421 spin_unlock(&nfsi->req_lock);
422 nfs_end_data_update(inode);
425 spin_unlock(&nfsi->req_lock);
426 nfs_clear_request(req);
427 nfs_release_request(req);
433 static inline struct nfs_page *
434 _nfs_find_request(struct inode *inode, unsigned long index)
436 struct nfs_inode *nfsi = NFS_I(inode);
437 struct nfs_page *req;
439 req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
441 atomic_inc(&req->wb_count);
445 static struct nfs_page *
446 nfs_find_request(struct inode *inode, unsigned long index)
448 struct nfs_page *req;
449 struct nfs_inode *nfsi = NFS_I(inode);
451 spin_lock(&nfsi->req_lock);
452 req = _nfs_find_request(inode, index);
453 spin_unlock(&nfsi->req_lock);
458 * Add a request to the inode's dirty list.
461 nfs_mark_request_dirty(struct nfs_page *req)
463 struct inode *inode = req->wb_context->dentry->d_inode;
464 struct nfs_inode *nfsi = NFS_I(inode);
466 spin_lock(&nfsi->req_lock);
467 radix_tree_tag_set(&nfsi->nfs_page_tree,
468 req->wb_index, NFS_PAGE_TAG_DIRTY);
469 nfs_list_add_request(req, &nfsi->dirty);
471 spin_unlock(&nfsi->req_lock);
472 inc_page_state(nr_dirty);
473 mark_inode_dirty(inode);
477 * Check if a request is dirty
480 nfs_dirty_request(struct nfs_page *req)
482 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
483 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
486 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
488 * Add a request to the inode's commit list.
491 nfs_mark_request_commit(struct nfs_page *req)
493 struct inode *inode = req->wb_context->dentry->d_inode;
494 struct nfs_inode *nfsi = NFS_I(inode);
496 spin_lock(&nfsi->req_lock);
497 nfs_list_add_request(req, &nfsi->commit);
499 spin_unlock(&nfsi->req_lock);
500 inc_page_state(nr_unstable);
501 mark_inode_dirty(inode);
506 * Wait for a request to complete.
508 * Interruptible by signals only if mounted with intr flag.
511 nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
513 struct nfs_inode *nfsi = NFS_I(inode);
514 struct nfs_page *req;
515 unsigned long idx_end, next;
516 unsigned int res = 0;
522 idx_end = idx_start + npages - 1;
524 spin_lock(&nfsi->req_lock);
526 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
527 if (req->wb_index > idx_end)
530 next = req->wb_index + 1;
531 BUG_ON(!NFS_WBACK_BUSY(req));
533 atomic_inc(&req->wb_count);
534 spin_unlock(&nfsi->req_lock);
535 error = nfs_wait_on_request(req);
536 nfs_release_request(req);
539 spin_lock(&nfsi->req_lock);
542 spin_unlock(&nfsi->req_lock);
547 * nfs_scan_dirty - Scan an inode for dirty requests
548 * @inode: NFS inode to scan
549 * @dst: destination list
550 * @idx_start: lower bound of page->index to scan.
551 * @npages: idx_start + npages sets the upper bound to scan.
553 * Moves requests from the inode's dirty page list.
554 * The requests are *not* checked to ensure that they form a contiguous set.
557 nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
559 struct nfs_inode *nfsi = NFS_I(inode);
562 if (nfsi->ndirty != 0) {
563 res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages);
565 sub_page_state(nr_dirty,res);
566 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
567 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
572 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
574 * nfs_scan_commit - Scan an inode for commit requests
575 * @inode: NFS inode to scan
576 * @dst: destination list
577 * @idx_start: lower bound of page->index to scan.
578 * @npages: idx_start + npages sets the upper bound to scan.
580 * Moves requests from the inode's 'commit' request list.
581 * The requests are *not* checked to ensure that they form a contiguous set.
584 nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
586 struct nfs_inode *nfsi = NFS_I(inode);
589 if (nfsi->ncommit != 0) {
590 res = nfs_scan_list(&nfsi->commit, dst, idx_start, npages);
591 nfsi->ncommit -= res;
592 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
593 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
599 static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
601 struct backing_dev_info *bdi = mapping->backing_dev_info;
607 if (!bdi_write_congested(bdi))
610 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
613 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
616 rpc_clnt_sigmask(clnt, &oldset);
617 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
618 if (bdi_write_congested(bdi)) {
624 rpc_clnt_sigunmask(clnt, &oldset);
626 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
627 if (bdi_write_congested(bdi))
630 finish_wait(&nfs_write_congestion, &wait);
636 * Try to update any existing write request, or create one if there is none.
637 * In order to match, the request's credentials must match those of
638 * the calling process.
640 * Note: Should always be called with the Page Lock held!
642 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
643 struct inode *inode, struct page *page,
644 unsigned int offset, unsigned int bytes)
646 struct nfs_server *server = NFS_SERVER(inode);
647 struct nfs_inode *nfsi = NFS_I(inode);
648 struct nfs_page *req, *new = NULL;
649 unsigned long rqend, end;
651 end = offset + bytes;
653 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
654 return ERR_PTR(-ERESTARTSYS);
656 /* Loop over all inode entries and see if we find
657 * A request for the page we wish to update
659 spin_lock(&nfsi->req_lock);
660 req = _nfs_find_request(inode, page->index);
662 if (!nfs_lock_request_dontget(req)) {
664 spin_unlock(&nfsi->req_lock);
665 error = nfs_wait_on_request(req);
666 nfs_release_request(req);
669 nfs_release_request(new);
670 return ERR_PTR(error);
674 spin_unlock(&nfsi->req_lock);
676 nfs_release_request(new);
682 nfs_lock_request_dontget(new);
683 error = nfs_inode_add_request(inode, new);
685 spin_unlock(&nfsi->req_lock);
686 nfs_unlock_request(new);
687 return ERR_PTR(error);
689 spin_unlock(&nfsi->req_lock);
690 nfs_mark_request_dirty(new);
693 spin_unlock(&nfsi->req_lock);
695 new = nfs_create_request(ctx, inode, page, offset, bytes);
700 /* We have a request for our page.
701 * If the creds don't match, or the
702 * page addresses don't match,
703 * tell the caller to wait on the conflicting
706 rqend = req->wb_offset + req->wb_bytes;
707 if (req->wb_context != ctx
708 || req->wb_page != page
709 || !nfs_dirty_request(req)
710 || offset > rqend || end < req->wb_offset) {
711 nfs_unlock_request(req);
712 return ERR_PTR(-EBUSY);
715 /* Okay, the request matches. Update the region */
716 if (offset < req->wb_offset) {
717 req->wb_offset = offset;
718 req->wb_pgbase = offset;
719 req->wb_bytes = rqend - req->wb_offset;
723 req->wb_bytes = end - req->wb_offset;
728 int nfs_flush_incompatible(struct file *file, struct page *page)
730 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
731 struct inode *inode = page->mapping->host;
732 struct nfs_page *req;
735 * Look for a request corresponding to this page. If there
736 * is one, and it belongs to another file, we flush it out
737 * before we try to copy anything into the page. Do this
738 * due to the lack of an ACCESS-type call in NFSv2.
739 * Also do the same if we find a request from an existing
742 req = nfs_find_request(inode, page->index);
744 if (req->wb_page != page || ctx != req->wb_context)
745 status = nfs_wb_page(inode, page);
746 nfs_release_request(req);
748 return (status < 0) ? status : 0;
752 * Update and possibly write a cached page of an NFS file.
754 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
755 * things with a page scheduled for an RPC call (e.g. invalidate it).
757 int nfs_updatepage(struct file *file, struct page *page,
758 unsigned int offset, unsigned int count)
760 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
761 struct inode *inode = page->mapping->host;
762 struct nfs_page *req;
765 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
767 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
768 file->f_dentry->d_parent->d_name.name,
769 file->f_dentry->d_name.name, count,
770 (long long)(page_offset(page) +offset));
772 if (IS_SYNC(inode)) {
773 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
775 if (offset == 0 && status == PAGE_CACHE_SIZE)
776 SetPageUptodate(page);
782 /* If we're not using byte range locks, and we know the page
783 * is entirely in cache, it may be more efficient to avoid
784 * fragmenting write requests.
786 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
787 loff_t end_offs = i_size_read(inode) - 1;
788 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
792 if (unlikely(end_offs < 0)) {
794 } else if (page->index == end_index) {
796 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
799 } else if (page->index < end_index)
800 count = PAGE_CACHE_SIZE;
804 * Try to find an NFS request corresponding to this page
806 * If the existing request cannot be updated, we must flush
810 req = nfs_update_request(ctx, inode, page, offset, count);
811 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
812 if (status != -EBUSY)
814 /* Request could not be updated. Flush it out and try again */
815 status = nfs_wb_page(inode, page);
816 } while (status >= 0);
822 /* Update file length */
823 nfs_grow_file(page, offset, count);
824 /* Set the PG_uptodate flag? */
825 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
826 nfs_unlock_request(req);
828 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
829 status, (long long)i_size_read(inode));
831 ClearPageUptodate(page);
835 static void nfs_writepage_release(struct nfs_page *req)
837 end_page_writeback(req->wb_page);
839 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
840 if (!PageError(req->wb_page)) {
841 if (NFS_NEED_RESCHED(req)) {
842 nfs_mark_request_dirty(req);
844 } else if (NFS_NEED_COMMIT(req)) {
845 nfs_mark_request_commit(req);
849 nfs_inode_remove_request(req);
852 nfs_clear_commit(req);
853 nfs_clear_reschedule(req);
855 nfs_inode_remove_request(req);
857 nfs_clear_page_writeback(req);
860 static inline int flush_task_priority(int how)
862 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
864 return RPC_PRIORITY_HIGH;
866 return RPC_PRIORITY_LOW;
868 return RPC_PRIORITY_NORMAL;
872 * Set up the argument/result storage required for the RPC call.
874 static void nfs_write_rpcsetup(struct nfs_page *req,
875 struct nfs_write_data *data,
876 const struct rpc_call_ops *call_ops,
877 unsigned int count, unsigned int offset,
883 /* Set up the RPC argument and reply structs
884 * NB: take care not to mess about with data->commit et al. */
887 data->inode = inode = req->wb_context->dentry->d_inode;
888 data->cred = req->wb_context->cred;
890 data->args.fh = NFS_FH(inode);
891 data->args.offset = req_offset(req) + offset;
892 data->args.pgbase = req->wb_pgbase + offset;
893 data->args.pages = data->pagevec;
894 data->args.count = count;
895 data->args.context = req->wb_context;
897 data->res.fattr = &data->fattr;
898 data->res.count = count;
899 data->res.verf = &data->verf;
900 nfs_fattr_init(&data->fattr);
902 /* Set up the initial task struct. */
903 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
904 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
905 NFS_PROTO(inode)->write_setup(data, how);
907 data->task.tk_priority = flush_task_priority(how);
908 data->task.tk_cookie = (unsigned long)inode;
910 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
913 (long long)NFS_FILEID(inode),
915 (unsigned long long)data->args.offset);
918 static void nfs_execute_write(struct nfs_write_data *data)
920 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
923 rpc_clnt_sigmask(clnt, &oldset);
925 rpc_execute(&data->task);
927 rpc_clnt_sigunmask(clnt, &oldset);
931 * Generate multiple small requests to write out a single
932 * contiguous dirty area on one page.
934 static int nfs_flush_multi(struct list_head *head, struct inode *inode, int how)
936 struct nfs_page *req = nfs_list_entry(head->next);
937 struct page *page = req->wb_page;
938 struct nfs_write_data *data;
939 unsigned int wsize = NFS_SERVER(inode)->wsize;
940 unsigned int nbytes, offset;
944 nfs_list_remove_request(req);
946 nbytes = req->wb_bytes;
948 data = nfs_writedata_alloc(1);
951 list_add(&data->pages, &list);
957 atomic_set(&req->wb_complete, requests);
959 ClearPageError(page);
960 set_page_writeback(page);
962 nbytes = req->wb_bytes;
964 data = list_entry(list.next, struct nfs_write_data, pages);
965 list_del_init(&data->pages);
967 data->pagevec[0] = page;
969 if (nbytes > wsize) {
970 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
975 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
976 nbytes, offset, how);
979 nfs_execute_write(data);
980 } while (nbytes != 0);
985 while (!list_empty(&list)) {
986 data = list_entry(list.next, struct nfs_write_data, pages);
987 list_del(&data->pages);
988 nfs_writedata_free(data);
990 nfs_mark_request_dirty(req);
991 nfs_clear_page_writeback(req);
996 * Create an RPC task for the given write request and kick it.
997 * The page must have been locked by the caller.
999 * It may happen that the page we're passed is not marked dirty.
1000 * This is the case if nfs_updatepage detects a conflicting request
1001 * that has been written but not committed.
1003 static int nfs_flush_one(struct list_head *head, struct inode *inode, int how)
1005 struct nfs_page *req;
1006 struct page **pages;
1007 struct nfs_write_data *data;
1010 if (NFS_SERVER(inode)->wsize < PAGE_CACHE_SIZE)
1011 return nfs_flush_multi(head, inode, how);
1013 data = nfs_writedata_alloc(NFS_SERVER(inode)->wpages);
1017 pages = data->pagevec;
1019 while (!list_empty(head)) {
1020 req = nfs_list_entry(head->next);
1021 nfs_list_remove_request(req);
1022 nfs_list_add_request(req, &data->pages);
1023 ClearPageError(req->wb_page);
1024 set_page_writeback(req->wb_page);
1025 *pages++ = req->wb_page;
1026 count += req->wb_bytes;
1028 req = nfs_list_entry(data->pages.next);
1030 /* Set up the argument struct */
1031 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
1033 nfs_execute_write(data);
1036 while (!list_empty(head)) {
1037 struct nfs_page *req = nfs_list_entry(head->next);
1038 nfs_list_remove_request(req);
1039 nfs_mark_request_dirty(req);
1040 nfs_clear_page_writeback(req);
1046 nfs_flush_list(struct list_head *head, int wpages, int how)
1048 LIST_HEAD(one_request);
1049 struct nfs_page *req;
1051 unsigned int pages = 0;
1053 while (!list_empty(head)) {
1054 pages += nfs_coalesce_requests(head, &one_request, wpages);
1055 req = nfs_list_entry(one_request.next);
1056 error = nfs_flush_one(&one_request, req->wb_context->dentry->d_inode, how);
1063 while (!list_empty(head)) {
1064 req = nfs_list_entry(head->next);
1065 nfs_list_remove_request(req);
1066 nfs_mark_request_dirty(req);
1067 nfs_clear_page_writeback(req);
1073 * Handle a write reply that flushed part of a page.
1075 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
1077 struct nfs_write_data *data = calldata;
1078 struct nfs_page *req = data->req;
1079 struct page *page = req->wb_page;
1081 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1082 req->wb_context->dentry->d_inode->i_sb->s_id,
1083 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1085 (long long)req_offset(req));
1087 if (nfs_writeback_done(task, data) != 0)
1090 if (task->tk_status < 0) {
1091 ClearPageUptodate(page);
1093 req->wb_context->error = task->tk_status;
1094 dprintk(", error = %d\n", task->tk_status);
1096 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1097 if (data->verf.committed < NFS_FILE_SYNC) {
1098 if (!NFS_NEED_COMMIT(req)) {
1099 nfs_defer_commit(req);
1100 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1101 dprintk(" defer commit\n");
1102 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1103 nfs_defer_reschedule(req);
1104 dprintk(" server reboot detected\n");
1111 if (atomic_dec_and_test(&req->wb_complete))
1112 nfs_writepage_release(req);
1115 static const struct rpc_call_ops nfs_write_partial_ops = {
1116 .rpc_call_done = nfs_writeback_done_partial,
1117 .rpc_release = nfs_writedata_release,
1121 * Handle a write reply that flushes a whole page.
1123 * FIXME: There is an inherent race with invalidate_inode_pages and
1124 * writebacks since the page->count is kept > 1 for as long
1125 * as the page has a write request pending.
1127 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1129 struct nfs_write_data *data = calldata;
1130 struct nfs_page *req;
1133 if (nfs_writeback_done(task, data) != 0)
1136 /* Update attributes as result of writeback. */
1137 while (!list_empty(&data->pages)) {
1138 req = nfs_list_entry(data->pages.next);
1139 nfs_list_remove_request(req);
1140 page = req->wb_page;
1142 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1143 req->wb_context->dentry->d_inode->i_sb->s_id,
1144 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1146 (long long)req_offset(req));
1148 if (task->tk_status < 0) {
1149 ClearPageUptodate(page);
1151 req->wb_context->error = task->tk_status;
1152 end_page_writeback(page);
1153 nfs_inode_remove_request(req);
1154 dprintk(", error = %d\n", task->tk_status);
1157 end_page_writeback(page);
1159 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1160 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1161 nfs_inode_remove_request(req);
1165 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1166 nfs_mark_request_commit(req);
1167 dprintk(" marked for commit\n");
1169 nfs_inode_remove_request(req);
1172 nfs_clear_page_writeback(req);
1176 static const struct rpc_call_ops nfs_write_full_ops = {
1177 .rpc_call_done = nfs_writeback_done_full,
1178 .rpc_release = nfs_writedata_release,
1183 * This function is called when the WRITE call is complete.
1185 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1187 struct nfs_writeargs *argp = &data->args;
1188 struct nfs_writeres *resp = &data->res;
1191 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1192 task->tk_pid, task->tk_status);
1194 /* Call the NFS version-specific code */
1195 status = NFS_PROTO(data->inode)->write_done(task, data);
1198 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1200 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1201 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1202 /* We tried a write call, but the server did not
1203 * commit data to stable storage even though we
1205 * Note: There is a known bug in Tru64 < 5.0 in which
1206 * the server reports NFS_DATA_SYNC, but performs
1207 * NFS_FILE_SYNC. We therefore implement this checking
1208 * as a dprintk() in order to avoid filling syslog.
1210 static unsigned long complain;
1212 if (time_before(complain, jiffies)) {
1213 dprintk("NFS: faulty NFS server %s:"
1214 " (committed = %d) != (stable = %d)\n",
1215 NFS_SERVER(data->inode)->hostname,
1216 resp->verf->committed, argp->stable);
1217 complain = jiffies + 300 * HZ;
1221 /* Is this a short write? */
1222 if (task->tk_status >= 0 && resp->count < argp->count) {
1223 static unsigned long complain;
1225 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1227 /* Has the server at least made some progress? */
1228 if (resp->count != 0) {
1229 /* Was this an NFSv2 write or an NFSv3 stable write? */
1230 if (resp->verf->committed != NFS_UNSTABLE) {
1231 /* Resend from where the server left off */
1232 argp->offset += resp->count;
1233 argp->pgbase += resp->count;
1234 argp->count -= resp->count;
1236 /* Resend as a stable write in order to avoid
1237 * headaches in the case of a server crash.
1239 argp->stable = NFS_FILE_SYNC;
1241 rpc_restart_call(task);
1244 if (time_before(complain, jiffies)) {
1246 "NFS: Server wrote zero bytes, expected %u.\n",
1248 complain = jiffies + 300 * HZ;
1250 /* Can't do anything about it except throw an error. */
1251 task->tk_status = -EIO;
1257 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1258 void nfs_commit_release(void *wdata)
1260 nfs_commit_free(wdata);
1264 * Set up the argument/result storage required for the RPC call.
1266 static void nfs_commit_rpcsetup(struct list_head *head,
1267 struct nfs_write_data *data,
1270 struct nfs_page *first;
1271 struct inode *inode;
1274 /* Set up the RPC argument and reply structs
1275 * NB: take care not to mess about with data->commit et al. */
1277 list_splice_init(head, &data->pages);
1278 first = nfs_list_entry(data->pages.next);
1279 inode = first->wb_context->dentry->d_inode;
1281 data->inode = inode;
1282 data->cred = first->wb_context->cred;
1284 data->args.fh = NFS_FH(data->inode);
1285 /* Note: we always request a commit of the entire inode */
1286 data->args.offset = 0;
1287 data->args.count = 0;
1288 data->res.count = 0;
1289 data->res.fattr = &data->fattr;
1290 data->res.verf = &data->verf;
1291 nfs_fattr_init(&data->fattr);
1293 /* Set up the initial task struct. */
1294 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1295 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
1296 NFS_PROTO(inode)->commit_setup(data, how);
1298 data->task.tk_priority = flush_task_priority(how);
1299 data->task.tk_cookie = (unsigned long)inode;
1301 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
1305 * Commit dirty pages
1308 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1310 struct nfs_write_data *data;
1311 struct nfs_page *req;
1313 data = nfs_commit_alloc(NFS_SERVER(inode)->wpages);
1318 /* Set up the argument struct */
1319 nfs_commit_rpcsetup(head, data, how);
1321 nfs_execute_write(data);
1324 while (!list_empty(head)) {
1325 req = nfs_list_entry(head->next);
1326 nfs_list_remove_request(req);
1327 nfs_mark_request_commit(req);
1328 nfs_clear_page_writeback(req);
1334 * COMMIT call returned
1336 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1338 struct nfs_write_data *data = calldata;
1339 struct nfs_page *req;
1342 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1343 task->tk_pid, task->tk_status);
1345 /* Call the NFS version-specific code */
1346 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1349 while (!list_empty(&data->pages)) {
1350 req = nfs_list_entry(data->pages.next);
1351 nfs_list_remove_request(req);
1353 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1354 req->wb_context->dentry->d_inode->i_sb->s_id,
1355 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1357 (long long)req_offset(req));
1358 if (task->tk_status < 0) {
1359 req->wb_context->error = task->tk_status;
1360 nfs_inode_remove_request(req);
1361 dprintk(", error = %d\n", task->tk_status);
1365 /* Okay, COMMIT succeeded, apparently. Check the verifier
1366 * returned by the server against all stored verfs. */
1367 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1368 /* We have a match */
1369 nfs_inode_remove_request(req);
1373 /* We have a mismatch. Write the page again */
1374 dprintk(" mismatch\n");
1375 nfs_mark_request_dirty(req);
1377 nfs_clear_page_writeback(req);
1380 sub_page_state(nr_unstable,res);
1383 static const struct rpc_call_ops nfs_commit_ops = {
1384 .rpc_call_done = nfs_commit_done,
1385 .rpc_release = nfs_commit_release,
1389 static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
1390 unsigned int npages, int how)
1392 struct nfs_inode *nfsi = NFS_I(inode);
1397 spin_lock(&nfsi->req_lock);
1398 res = nfs_scan_dirty(inode, &head, idx_start, npages);
1399 spin_unlock(&nfsi->req_lock);
1401 struct nfs_server *server = NFS_SERVER(inode);
1403 /* For single writes, FLUSH_STABLE is more efficient */
1404 if (res == nfsi->npages && nfsi->npages <= server->wpages) {
1405 if (res > 1 || nfs_list_entry(head.next)->wb_bytes <= server->wsize)
1406 how |= FLUSH_STABLE;
1408 error = nfs_flush_list(&head, server->wpages, how);
1415 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1416 int nfs_commit_inode(struct inode *inode, int how)
1418 struct nfs_inode *nfsi = NFS_I(inode);
1423 spin_lock(&nfsi->req_lock);
1424 res = nfs_scan_commit(inode, &head, 0, 0);
1425 spin_unlock(&nfsi->req_lock);
1427 error = nfs_commit_list(inode, &head, how);
1435 int nfs_sync_inode(struct inode *inode, unsigned long idx_start,
1436 unsigned int npages, int how)
1438 int nocommit = how & FLUSH_NOCOMMIT;
1439 int wait = how & FLUSH_WAIT;
1442 how &= ~(FLUSH_WAIT|FLUSH_NOCOMMIT);
1446 error = nfs_wait_on_requests(inode, idx_start, npages);
1450 error = nfs_flush_inode(inode, idx_start, npages, how);
1454 error = nfs_commit_inode(inode, how);
1455 } while (error > 0);
1459 int nfs_init_writepagecache(void)
1461 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1462 sizeof(struct nfs_write_data),
1463 0, SLAB_HWCACHE_ALIGN,
1465 if (nfs_wdata_cachep == NULL)
1468 nfs_wdata_mempool = mempool_create(MIN_POOL_WRITE,
1472 if (nfs_wdata_mempool == NULL)
1475 nfs_commit_mempool = mempool_create(MIN_POOL_COMMIT,
1479 if (nfs_commit_mempool == NULL)
1485 void nfs_destroy_writepagecache(void)
1487 mempool_destroy(nfs_commit_mempool);
1488 mempool_destroy(nfs_wdata_mempool);
1489 if (kmem_cache_destroy(nfs_wdata_cachep))
1490 printk(KERN_INFO "nfs_write_data: not all structures were freed\n");