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/types.h>
50 #include <linux/slab.h>
52 #include <linux/pagemap.h>
53 #include <linux/file.h>
54 #include <linux/writeback.h>
56 #include <linux/sunrpc/clnt.h>
57 #include <linux/nfs_fs.h>
58 #include <linux/nfs_mount.h>
59 #include <linux/nfs_page.h>
60 #include <linux/backing-dev.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_mapping(struct address_space *mapping, struct writeback_control *wbc, int how);
83 static const struct rpc_call_ops nfs_write_partial_ops;
84 static const struct rpc_call_ops nfs_write_full_ops;
85 static const struct rpc_call_ops nfs_commit_ops;
87 static kmem_cache_t *nfs_wdata_cachep;
88 static mempool_t *nfs_wdata_mempool;
89 static mempool_t *nfs_commit_mempool;
91 static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
93 struct nfs_write_data *nfs_commit_alloc(void)
95 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
98 memset(p, 0, sizeof(*p));
99 INIT_LIST_HEAD(&p->pages);
104 void nfs_commit_rcu_free(struct rcu_head *head)
106 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
107 if (p && (p->pagevec != &p->page_array[0]))
109 mempool_free(p, nfs_commit_mempool);
112 void nfs_commit_free(struct nfs_write_data *wdata)
114 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
117 struct nfs_write_data *nfs_writedata_alloc(size_t len)
119 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
120 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
123 memset(p, 0, sizeof(*p));
124 INIT_LIST_HEAD(&p->pages);
125 p->npages = pagecount;
126 if (pagecount <= ARRAY_SIZE(p->page_array))
127 p->pagevec = p->page_array;
129 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
131 mempool_free(p, nfs_wdata_mempool);
139 static void nfs_writedata_rcu_free(struct rcu_head *head)
141 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
142 if (p && (p->pagevec != &p->page_array[0]))
144 mempool_free(p, nfs_wdata_mempool);
147 static void nfs_writedata_free(struct nfs_write_data *wdata)
149 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
152 void nfs_writedata_release(void *wdata)
154 nfs_writedata_free(wdata);
157 /* Adjust the file length if we're writing beyond the end */
158 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
160 struct inode *inode = page->mapping->host;
161 loff_t end, i_size = i_size_read(inode);
162 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
164 if (i_size > 0 && page->index < end_index)
166 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
169 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
170 i_size_write(inode, end);
173 /* We can set the PG_uptodate flag if we see that a write request
174 * covers the full page.
176 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
180 if (PageUptodate(page))
184 if (count == PAGE_CACHE_SIZE) {
185 SetPageUptodate(page);
189 end_offs = i_size_read(page->mapping->host) - 1;
192 /* Is this the last page? */
193 if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
195 /* This is the last page: set PG_uptodate if we cover the entire
196 * extent of the data, then zero the rest of the page.
198 if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
199 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
200 SetPageUptodate(page);
205 * Write a page synchronously.
206 * Offset is the data offset within the page.
208 static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
209 struct page *page, unsigned int offset, unsigned int count,
212 unsigned int wsize = NFS_SERVER(inode)->wsize;
213 int result, written = 0;
214 struct nfs_write_data *wdata;
216 wdata = nfs_writedata_alloc(wsize);
221 wdata->cred = ctx->cred;
222 wdata->inode = inode;
223 wdata->args.fh = NFS_FH(inode);
224 wdata->args.context = ctx;
225 wdata->args.pages = &page;
226 wdata->args.stable = NFS_FILE_SYNC;
227 wdata->args.pgbase = offset;
228 wdata->args.count = wsize;
229 wdata->res.fattr = &wdata->fattr;
230 wdata->res.verf = &wdata->verf;
232 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
234 (long long)NFS_FILEID(inode),
235 count, (long long)(page_offset(page) + offset));
237 set_page_writeback(page);
238 nfs_begin_data_update(inode);
241 wdata->args.count = count;
242 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
244 result = NFS_PROTO(inode)->write(wdata);
247 /* Must mark the page invalid after I/O error */
248 ClearPageUptodate(page);
251 if (result < wdata->args.count)
252 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
253 wdata->args.count, result);
255 wdata->args.offset += result;
256 wdata->args.pgbase += result;
259 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
261 /* Update file length */
262 nfs_grow_file(page, offset, written);
263 /* Set the PG_uptodate flag? */
264 nfs_mark_uptodate(page, offset, written);
267 ClearPageError(page);
270 nfs_end_data_update(inode);
271 end_page_writeback(page);
272 nfs_writedata_release(wdata);
273 return written ? written : result;
276 static int nfs_writepage_async(struct nfs_open_context *ctx,
277 struct inode *inode, struct page *page,
278 unsigned int offset, unsigned int count)
280 struct nfs_page *req;
282 req = nfs_update_request(ctx, inode, page, offset, count);
285 /* Update file length */
286 nfs_grow_file(page, offset, count);
287 /* Set the PG_uptodate flag? */
288 nfs_mark_uptodate(page, offset, count);
289 nfs_unlock_request(req);
293 static int wb_priority(struct writeback_control *wbc)
295 if (wbc->for_reclaim)
296 return FLUSH_HIGHPRI;
297 if (wbc->for_kupdate)
303 * Write an mmapped page to the server.
305 int nfs_writepage(struct page *page, struct writeback_control *wbc)
307 struct nfs_open_context *ctx;
308 struct inode *inode = page->mapping->host;
309 unsigned long end_index;
310 unsigned offset = PAGE_CACHE_SIZE;
311 loff_t i_size = i_size_read(inode);
312 int inode_referenced = 0;
313 int priority = wb_priority(wbc);
316 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
317 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
320 * Note: We need to ensure that we have a reference to the inode
321 * if we are to do asynchronous writes. If not, waiting
322 * in nfs_wait_on_request() may deadlock with clear_inode().
324 * If igrab() fails here, then it is in any case safe to
325 * call nfs_wb_page(), since there will be no pending writes.
327 if (igrab(inode) != 0)
328 inode_referenced = 1;
329 end_index = i_size >> PAGE_CACHE_SHIFT;
331 /* Ensure we've flushed out any previous writes */
332 nfs_wb_page_priority(inode, page, priority);
335 if (page->index < end_index)
337 /* things got complicated... */
338 offset = i_size & (PAGE_CACHE_SIZE-1);
340 /* OK, are we completely out? */
341 err = 0; /* potential race with truncate - ignore */
342 if (page->index >= end_index+1 || !offset)
345 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
351 if (!IS_SYNC(inode) && inode_referenced) {
352 err = nfs_writepage_async(ctx, inode, page, 0, offset);
353 if (!wbc->for_writepages)
354 nfs_flush_mapping(page->mapping, wbc, wb_priority(wbc));
356 err = nfs_writepage_sync(ctx, inode, page, 0,
360 redirty_page_for_writepage(wbc, page);
365 put_nfs_open_context(ctx);
368 if (inode_referenced)
374 * Note: causes nfs_update_request() to block on the assumption
375 * that the writeback is generated due to memory pressure.
377 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
379 struct backing_dev_info *bdi = mapping->backing_dev_info;
380 struct inode *inode = mapping->host;
383 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
385 err = generic_writepages(mapping, wbc);
388 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
389 if (wbc->nonblocking)
391 nfs_wait_on_write_congestion(mapping, 0);
393 err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
396 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
397 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
398 err = nfs_wait_on_requests(inode, 0, 0);
402 err = nfs_commit_inode(inode, wb_priority(wbc));
404 wbc->nr_to_write -= err;
408 clear_bit(BDI_write_congested, &bdi->state);
409 wake_up_all(&nfs_write_congestion);
410 congestion_end(WRITE);
415 * Insert a write request into an inode
417 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
419 struct nfs_inode *nfsi = NFS_I(inode);
422 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
423 BUG_ON(error == -EEXIST);
428 nfs_begin_data_update(inode);
429 if (nfs_have_delegation(inode, FMODE_WRITE))
432 SetPagePrivate(req->wb_page);
434 atomic_inc(&req->wb_count);
439 * Insert a write request into an inode
441 static void nfs_inode_remove_request(struct nfs_page *req)
443 struct inode *inode = req->wb_context->dentry->d_inode;
444 struct nfs_inode *nfsi = NFS_I(inode);
446 BUG_ON (!NFS_WBACK_BUSY(req));
448 spin_lock(&nfsi->req_lock);
449 ClearPagePrivate(req->wb_page);
450 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
453 spin_unlock(&nfsi->req_lock);
454 nfs_end_data_update(inode);
457 spin_unlock(&nfsi->req_lock);
458 nfs_clear_request(req);
459 nfs_release_request(req);
465 static inline struct nfs_page *
466 _nfs_find_request(struct inode *inode, unsigned long index)
468 struct nfs_inode *nfsi = NFS_I(inode);
469 struct nfs_page *req;
471 req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
473 atomic_inc(&req->wb_count);
477 static struct nfs_page *
478 nfs_find_request(struct inode *inode, unsigned long index)
480 struct nfs_page *req;
481 struct nfs_inode *nfsi = NFS_I(inode);
483 spin_lock(&nfsi->req_lock);
484 req = _nfs_find_request(inode, index);
485 spin_unlock(&nfsi->req_lock);
490 * Add a request to the inode's dirty list.
493 nfs_mark_request_dirty(struct nfs_page *req)
495 struct inode *inode = req->wb_context->dentry->d_inode;
496 struct nfs_inode *nfsi = NFS_I(inode);
498 spin_lock(&nfsi->req_lock);
499 radix_tree_tag_set(&nfsi->nfs_page_tree,
500 req->wb_index, NFS_PAGE_TAG_DIRTY);
501 nfs_list_add_request(req, &nfsi->dirty);
503 spin_unlock(&nfsi->req_lock);
504 inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
505 mark_inode_dirty(inode);
509 * Check if a request is dirty
512 nfs_dirty_request(struct nfs_page *req)
514 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
515 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
518 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
520 * Add a request to the inode's commit list.
523 nfs_mark_request_commit(struct nfs_page *req)
525 struct inode *inode = req->wb_context->dentry->d_inode;
526 struct nfs_inode *nfsi = NFS_I(inode);
528 spin_lock(&nfsi->req_lock);
529 nfs_list_add_request(req, &nfsi->commit);
531 spin_unlock(&nfsi->req_lock);
532 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
533 mark_inode_dirty(inode);
538 * Wait for a request to complete.
540 * Interruptible by signals only if mounted with intr flag.
542 static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
544 struct nfs_inode *nfsi = NFS_I(inode);
545 struct nfs_page *req;
546 unsigned long idx_end, next;
547 unsigned int res = 0;
553 idx_end = idx_start + npages - 1;
556 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
557 if (req->wb_index > idx_end)
560 next = req->wb_index + 1;
561 BUG_ON(!NFS_WBACK_BUSY(req));
563 atomic_inc(&req->wb_count);
564 spin_unlock(&nfsi->req_lock);
565 error = nfs_wait_on_request(req);
566 nfs_release_request(req);
567 spin_lock(&nfsi->req_lock);
575 static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
577 struct nfs_inode *nfsi = NFS_I(inode);
580 spin_lock(&nfsi->req_lock);
581 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
582 spin_unlock(&nfsi->req_lock);
586 static void nfs_cancel_dirty_list(struct list_head *head)
588 struct nfs_page *req;
589 while(!list_empty(head)) {
590 req = nfs_list_entry(head->next);
591 nfs_list_remove_request(req);
592 nfs_inode_remove_request(req);
593 nfs_clear_page_writeback(req);
597 static void nfs_cancel_commit_list(struct list_head *head)
599 struct nfs_page *req;
601 while(!list_empty(head)) {
602 req = nfs_list_entry(head->next);
603 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
604 nfs_list_remove_request(req);
605 nfs_inode_remove_request(req);
606 nfs_unlock_request(req);
611 * nfs_scan_dirty - Scan an inode for dirty requests
612 * @inode: NFS inode to scan
613 * @dst: destination list
614 * @idx_start: lower bound of page->index to scan.
615 * @npages: idx_start + npages sets the upper bound to scan.
617 * Moves requests from the inode's dirty page list.
618 * The requests are *not* checked to ensure that they form a contiguous set.
621 nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
623 struct nfs_inode *nfsi = NFS_I(inode);
626 if (nfsi->ndirty != 0) {
627 res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages);
629 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
630 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
635 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
637 * nfs_scan_commit - Scan an inode for commit requests
638 * @inode: NFS inode to scan
639 * @dst: destination list
640 * @idx_start: lower bound of page->index to scan.
641 * @npages: idx_start + npages sets the upper bound to scan.
643 * Moves requests from the inode's 'commit' request list.
644 * The requests are *not* checked to ensure that they form a contiguous set.
647 nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
649 struct nfs_inode *nfsi = NFS_I(inode);
652 if (nfsi->ncommit != 0) {
653 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
654 nfsi->ncommit -= res;
655 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
656 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
661 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
667 static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
669 struct backing_dev_info *bdi = mapping->backing_dev_info;
675 if (!bdi_write_congested(bdi))
678 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
681 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
684 rpc_clnt_sigmask(clnt, &oldset);
685 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
686 if (bdi_write_congested(bdi)) {
692 rpc_clnt_sigunmask(clnt, &oldset);
694 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
695 if (bdi_write_congested(bdi))
698 finish_wait(&nfs_write_congestion, &wait);
704 * Try to update any existing write request, or create one if there is none.
705 * In order to match, the request's credentials must match those of
706 * the calling process.
708 * Note: Should always be called with the Page Lock held!
710 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
711 struct inode *inode, struct page *page,
712 unsigned int offset, unsigned int bytes)
714 struct nfs_server *server = NFS_SERVER(inode);
715 struct nfs_inode *nfsi = NFS_I(inode);
716 struct nfs_page *req, *new = NULL;
717 unsigned long rqend, end;
719 end = offset + bytes;
721 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
722 return ERR_PTR(-ERESTARTSYS);
724 /* Loop over all inode entries and see if we find
725 * A request for the page we wish to update
727 spin_lock(&nfsi->req_lock);
728 req = _nfs_find_request(inode, page->index);
730 if (!nfs_lock_request_dontget(req)) {
732 spin_unlock(&nfsi->req_lock);
733 error = nfs_wait_on_request(req);
734 nfs_release_request(req);
737 nfs_release_request(new);
738 return ERR_PTR(error);
742 spin_unlock(&nfsi->req_lock);
744 nfs_release_request(new);
750 nfs_lock_request_dontget(new);
751 error = nfs_inode_add_request(inode, new);
753 spin_unlock(&nfsi->req_lock);
754 nfs_unlock_request(new);
755 return ERR_PTR(error);
757 spin_unlock(&nfsi->req_lock);
758 nfs_mark_request_dirty(new);
761 spin_unlock(&nfsi->req_lock);
763 new = nfs_create_request(ctx, inode, page, offset, bytes);
768 /* We have a request for our page.
769 * If the creds don't match, or the
770 * page addresses don't match,
771 * tell the caller to wait on the conflicting
774 rqend = req->wb_offset + req->wb_bytes;
775 if (req->wb_context != ctx
776 || req->wb_page != page
777 || !nfs_dirty_request(req)
778 || offset > rqend || end < req->wb_offset) {
779 nfs_unlock_request(req);
780 return ERR_PTR(-EBUSY);
783 /* Okay, the request matches. Update the region */
784 if (offset < req->wb_offset) {
785 req->wb_offset = offset;
786 req->wb_pgbase = offset;
787 req->wb_bytes = rqend - req->wb_offset;
791 req->wb_bytes = end - req->wb_offset;
796 int nfs_flush_incompatible(struct file *file, struct page *page)
798 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
799 struct inode *inode = page->mapping->host;
800 struct nfs_page *req;
803 * Look for a request corresponding to this page. If there
804 * is one, and it belongs to another file, we flush it out
805 * before we try to copy anything into the page. Do this
806 * due to the lack of an ACCESS-type call in NFSv2.
807 * Also do the same if we find a request from an existing
810 req = nfs_find_request(inode, page->index);
812 if (req->wb_page != page || ctx != req->wb_context)
813 status = nfs_wb_page(inode, page);
814 nfs_release_request(req);
816 return (status < 0) ? status : 0;
820 * Update and possibly write a cached page of an NFS file.
822 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
823 * things with a page scheduled for an RPC call (e.g. invalidate it).
825 int nfs_updatepage(struct file *file, struct page *page,
826 unsigned int offset, unsigned int count)
828 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
829 struct inode *inode = page->mapping->host;
830 struct nfs_page *req;
833 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
835 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
836 file->f_dentry->d_parent->d_name.name,
837 file->f_dentry->d_name.name, count,
838 (long long)(page_offset(page) +offset));
840 if (IS_SYNC(inode)) {
841 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
843 if (offset == 0 && status == PAGE_CACHE_SIZE)
844 SetPageUptodate(page);
850 /* If we're not using byte range locks, and we know the page
851 * is entirely in cache, it may be more efficient to avoid
852 * fragmenting write requests.
854 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
855 loff_t end_offs = i_size_read(inode) - 1;
856 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
860 if (unlikely(end_offs < 0)) {
862 } else if (page->index == end_index) {
864 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
867 } else if (page->index < end_index)
868 count = PAGE_CACHE_SIZE;
872 * Try to find an NFS request corresponding to this page
874 * If the existing request cannot be updated, we must flush
878 req = nfs_update_request(ctx, inode, page, offset, count);
879 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
880 if (status != -EBUSY)
882 /* Request could not be updated. Flush it out and try again */
883 status = nfs_wb_page(inode, page);
884 } while (status >= 0);
890 /* Update file length */
891 nfs_grow_file(page, offset, count);
892 /* Set the PG_uptodate flag? */
893 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
894 nfs_unlock_request(req);
896 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
897 status, (long long)i_size_read(inode));
899 ClearPageUptodate(page);
903 static void nfs_writepage_release(struct nfs_page *req)
905 end_page_writeback(req->wb_page);
907 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
908 if (!PageError(req->wb_page)) {
909 if (NFS_NEED_RESCHED(req)) {
910 nfs_mark_request_dirty(req);
912 } else if (NFS_NEED_COMMIT(req)) {
913 nfs_mark_request_commit(req);
917 nfs_inode_remove_request(req);
920 nfs_clear_commit(req);
921 nfs_clear_reschedule(req);
923 nfs_inode_remove_request(req);
925 nfs_clear_page_writeback(req);
928 static inline int flush_task_priority(int how)
930 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
932 return RPC_PRIORITY_HIGH;
934 return RPC_PRIORITY_LOW;
936 return RPC_PRIORITY_NORMAL;
940 * Set up the argument/result storage required for the RPC call.
942 static void nfs_write_rpcsetup(struct nfs_page *req,
943 struct nfs_write_data *data,
944 const struct rpc_call_ops *call_ops,
945 unsigned int count, unsigned int offset,
951 /* Set up the RPC argument and reply structs
952 * NB: take care not to mess about with data->commit et al. */
955 data->inode = inode = req->wb_context->dentry->d_inode;
956 data->cred = req->wb_context->cred;
958 data->args.fh = NFS_FH(inode);
959 data->args.offset = req_offset(req) + offset;
960 data->args.pgbase = req->wb_pgbase + offset;
961 data->args.pages = data->pagevec;
962 data->args.count = count;
963 data->args.context = req->wb_context;
965 data->res.fattr = &data->fattr;
966 data->res.count = count;
967 data->res.verf = &data->verf;
968 nfs_fattr_init(&data->fattr);
970 /* Set up the initial task struct. */
971 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
972 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
973 NFS_PROTO(inode)->write_setup(data, how);
975 data->task.tk_priority = flush_task_priority(how);
976 data->task.tk_cookie = (unsigned long)inode;
978 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
981 (long long)NFS_FILEID(inode),
983 (unsigned long long)data->args.offset);
986 static void nfs_execute_write(struct nfs_write_data *data)
988 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
991 rpc_clnt_sigmask(clnt, &oldset);
992 rpc_execute(&data->task);
993 rpc_clnt_sigunmask(clnt, &oldset);
997 * Generate multiple small requests to write out a single
998 * contiguous dirty area on one page.
1000 static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
1002 struct nfs_page *req = nfs_list_entry(head->next);
1003 struct page *page = req->wb_page;
1004 struct nfs_write_data *data;
1005 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
1006 unsigned int offset;
1010 nfs_list_remove_request(req);
1012 nbytes = req->wb_bytes;
1014 size_t len = min(nbytes, wsize);
1016 data = nfs_writedata_alloc(len);
1019 list_add(&data->pages, &list);
1022 } while (nbytes != 0);
1023 atomic_set(&req->wb_complete, requests);
1025 ClearPageError(page);
1026 set_page_writeback(page);
1028 nbytes = req->wb_bytes;
1030 data = list_entry(list.next, struct nfs_write_data, pages);
1031 list_del_init(&data->pages);
1033 data->pagevec[0] = page;
1035 if (nbytes > wsize) {
1036 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1037 wsize, offset, how);
1041 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1042 nbytes, offset, how);
1045 nfs_execute_write(data);
1046 } while (nbytes != 0);
1051 while (!list_empty(&list)) {
1052 data = list_entry(list.next, struct nfs_write_data, pages);
1053 list_del(&data->pages);
1054 nfs_writedata_release(data);
1056 nfs_mark_request_dirty(req);
1057 nfs_clear_page_writeback(req);
1062 * Create an RPC task for the given write request and kick it.
1063 * The page must have been locked by the caller.
1065 * It may happen that the page we're passed is not marked dirty.
1066 * This is the case if nfs_updatepage detects a conflicting request
1067 * that has been written but not committed.
1069 static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
1071 struct nfs_page *req;
1072 struct page **pages;
1073 struct nfs_write_data *data;
1076 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
1080 pages = data->pagevec;
1082 while (!list_empty(head)) {
1083 req = nfs_list_entry(head->next);
1084 nfs_list_remove_request(req);
1085 nfs_list_add_request(req, &data->pages);
1086 ClearPageError(req->wb_page);
1087 set_page_writeback(req->wb_page);
1088 *pages++ = req->wb_page;
1089 count += req->wb_bytes;
1091 req = nfs_list_entry(data->pages.next);
1093 /* Set up the argument struct */
1094 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
1096 nfs_execute_write(data);
1099 while (!list_empty(head)) {
1100 struct nfs_page *req = nfs_list_entry(head->next);
1101 nfs_list_remove_request(req);
1102 nfs_mark_request_dirty(req);
1103 nfs_clear_page_writeback(req);
1108 static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
1110 LIST_HEAD(one_request);
1111 int (*flush_one)(struct inode *, struct list_head *, int);
1112 struct nfs_page *req;
1113 int wpages = NFS_SERVER(inode)->wpages;
1114 int wsize = NFS_SERVER(inode)->wsize;
1117 flush_one = nfs_flush_one;
1118 if (wsize < PAGE_CACHE_SIZE)
1119 flush_one = nfs_flush_multi;
1120 /* For single writes, FLUSH_STABLE is more efficient */
1121 if (npages <= wpages && npages == NFS_I(inode)->npages
1122 && nfs_list_entry(head->next)->wb_bytes <= wsize)
1123 how |= FLUSH_STABLE;
1126 nfs_coalesce_requests(head, &one_request, wpages);
1127 req = nfs_list_entry(one_request.next);
1128 error = flush_one(inode, &one_request, how);
1131 } while (!list_empty(head));
1134 while (!list_empty(head)) {
1135 req = nfs_list_entry(head->next);
1136 nfs_list_remove_request(req);
1137 nfs_mark_request_dirty(req);
1138 nfs_clear_page_writeback(req);
1144 * Handle a write reply that flushed part of a page.
1146 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
1148 struct nfs_write_data *data = calldata;
1149 struct nfs_page *req = data->req;
1150 struct page *page = req->wb_page;
1152 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1153 req->wb_context->dentry->d_inode->i_sb->s_id,
1154 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1156 (long long)req_offset(req));
1158 if (nfs_writeback_done(task, data) != 0)
1161 if (task->tk_status < 0) {
1162 ClearPageUptodate(page);
1164 req->wb_context->error = task->tk_status;
1165 dprintk(", error = %d\n", task->tk_status);
1167 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1168 if (data->verf.committed < NFS_FILE_SYNC) {
1169 if (!NFS_NEED_COMMIT(req)) {
1170 nfs_defer_commit(req);
1171 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1172 dprintk(" defer commit\n");
1173 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1174 nfs_defer_reschedule(req);
1175 dprintk(" server reboot detected\n");
1182 if (atomic_dec_and_test(&req->wb_complete))
1183 nfs_writepage_release(req);
1186 static const struct rpc_call_ops nfs_write_partial_ops = {
1187 .rpc_call_done = nfs_writeback_done_partial,
1188 .rpc_release = nfs_writedata_release,
1192 * Handle a write reply that flushes a whole page.
1194 * FIXME: There is an inherent race with invalidate_inode_pages and
1195 * writebacks since the page->count is kept > 1 for as long
1196 * as the page has a write request pending.
1198 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1200 struct nfs_write_data *data = calldata;
1201 struct nfs_page *req;
1204 if (nfs_writeback_done(task, data) != 0)
1207 /* Update attributes as result of writeback. */
1208 while (!list_empty(&data->pages)) {
1209 req = nfs_list_entry(data->pages.next);
1210 nfs_list_remove_request(req);
1211 page = req->wb_page;
1213 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1214 req->wb_context->dentry->d_inode->i_sb->s_id,
1215 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1217 (long long)req_offset(req));
1219 if (task->tk_status < 0) {
1220 ClearPageUptodate(page);
1222 req->wb_context->error = task->tk_status;
1223 end_page_writeback(page);
1224 nfs_inode_remove_request(req);
1225 dprintk(", error = %d\n", task->tk_status);
1228 end_page_writeback(page);
1230 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1231 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1232 nfs_inode_remove_request(req);
1236 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1237 nfs_mark_request_commit(req);
1238 dprintk(" marked for commit\n");
1240 nfs_inode_remove_request(req);
1243 nfs_clear_page_writeback(req);
1247 static const struct rpc_call_ops nfs_write_full_ops = {
1248 .rpc_call_done = nfs_writeback_done_full,
1249 .rpc_release = nfs_writedata_release,
1254 * This function is called when the WRITE call is complete.
1256 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1258 struct nfs_writeargs *argp = &data->args;
1259 struct nfs_writeres *resp = &data->res;
1262 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1263 task->tk_pid, task->tk_status);
1266 * ->write_done will attempt to use post-op attributes to detect
1267 * conflicting writes by other clients. A strict interpretation
1268 * of close-to-open would allow us to continue caching even if
1269 * another writer had changed the file, but some applications
1270 * depend on tighter cache coherency when writing.
1272 status = NFS_PROTO(data->inode)->write_done(task, data);
1275 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1277 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1278 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1279 /* We tried a write call, but the server did not
1280 * commit data to stable storage even though we
1282 * Note: There is a known bug in Tru64 < 5.0 in which
1283 * the server reports NFS_DATA_SYNC, but performs
1284 * NFS_FILE_SYNC. We therefore implement this checking
1285 * as a dprintk() in order to avoid filling syslog.
1287 static unsigned long complain;
1289 if (time_before(complain, jiffies)) {
1290 dprintk("NFS: faulty NFS server %s:"
1291 " (committed = %d) != (stable = %d)\n",
1292 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1293 resp->verf->committed, argp->stable);
1294 complain = jiffies + 300 * HZ;
1298 /* Is this a short write? */
1299 if (task->tk_status >= 0 && resp->count < argp->count) {
1300 static unsigned long complain;
1302 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1304 /* Has the server at least made some progress? */
1305 if (resp->count != 0) {
1306 /* Was this an NFSv2 write or an NFSv3 stable write? */
1307 if (resp->verf->committed != NFS_UNSTABLE) {
1308 /* Resend from where the server left off */
1309 argp->offset += resp->count;
1310 argp->pgbase += resp->count;
1311 argp->count -= resp->count;
1313 /* Resend as a stable write in order to avoid
1314 * headaches in the case of a server crash.
1316 argp->stable = NFS_FILE_SYNC;
1318 rpc_restart_call(task);
1321 if (time_before(complain, jiffies)) {
1323 "NFS: Server wrote zero bytes, expected %u.\n",
1325 complain = jiffies + 300 * HZ;
1327 /* Can't do anything about it except throw an error. */
1328 task->tk_status = -EIO;
1334 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1335 void nfs_commit_release(void *wdata)
1337 nfs_commit_free(wdata);
1341 * Set up the argument/result storage required for the RPC call.
1343 static void nfs_commit_rpcsetup(struct list_head *head,
1344 struct nfs_write_data *data,
1347 struct nfs_page *first;
1348 struct inode *inode;
1351 /* Set up the RPC argument and reply structs
1352 * NB: take care not to mess about with data->commit et al. */
1354 list_splice_init(head, &data->pages);
1355 first = nfs_list_entry(data->pages.next);
1356 inode = first->wb_context->dentry->d_inode;
1358 data->inode = inode;
1359 data->cred = first->wb_context->cred;
1361 data->args.fh = NFS_FH(data->inode);
1362 /* Note: we always request a commit of the entire inode */
1363 data->args.offset = 0;
1364 data->args.count = 0;
1365 data->res.count = 0;
1366 data->res.fattr = &data->fattr;
1367 data->res.verf = &data->verf;
1368 nfs_fattr_init(&data->fattr);
1370 /* Set up the initial task struct. */
1371 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1372 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
1373 NFS_PROTO(inode)->commit_setup(data, how);
1375 data->task.tk_priority = flush_task_priority(how);
1376 data->task.tk_cookie = (unsigned long)inode;
1378 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
1382 * Commit dirty pages
1385 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1387 struct nfs_write_data *data;
1388 struct nfs_page *req;
1390 data = nfs_commit_alloc();
1395 /* Set up the argument struct */
1396 nfs_commit_rpcsetup(head, data, how);
1398 nfs_execute_write(data);
1401 while (!list_empty(head)) {
1402 req = nfs_list_entry(head->next);
1403 nfs_list_remove_request(req);
1404 nfs_mark_request_commit(req);
1405 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1406 nfs_clear_page_writeback(req);
1412 * COMMIT call returned
1414 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1416 struct nfs_write_data *data = calldata;
1417 struct nfs_page *req;
1419 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1420 task->tk_pid, task->tk_status);
1422 /* Call the NFS version-specific code */
1423 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1426 while (!list_empty(&data->pages)) {
1427 req = nfs_list_entry(data->pages.next);
1428 nfs_list_remove_request(req);
1429 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1431 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1432 req->wb_context->dentry->d_inode->i_sb->s_id,
1433 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1435 (long long)req_offset(req));
1436 if (task->tk_status < 0) {
1437 req->wb_context->error = task->tk_status;
1438 nfs_inode_remove_request(req);
1439 dprintk(", error = %d\n", task->tk_status);
1443 /* Okay, COMMIT succeeded, apparently. Check the verifier
1444 * returned by the server against all stored verfs. */
1445 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1446 /* We have a match */
1447 nfs_inode_remove_request(req);
1451 /* We have a mismatch. Write the page again */
1452 dprintk(" mismatch\n");
1453 nfs_mark_request_dirty(req);
1455 nfs_clear_page_writeback(req);
1459 static const struct rpc_call_ops nfs_commit_ops = {
1460 .rpc_call_done = nfs_commit_done,
1461 .rpc_release = nfs_commit_release,
1464 static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1470 static int nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
1472 struct nfs_inode *nfsi = NFS_I(mapping->host);
1474 pgoff_t index = wbc->range_start >> PAGE_CACHE_SHIFT;
1475 unsigned long npages = 1 + (wbc->range_end >> PAGE_CACHE_SHIFT) - index;
1478 spin_lock(&nfsi->req_lock);
1479 res = nfs_scan_dirty(mapping->host, &head, index, npages);
1480 spin_unlock(&nfsi->req_lock);
1482 int error = nfs_flush_list(mapping->host, &head, res, how);
1485 wbc->nr_to_write -= res;
1490 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1491 int nfs_commit_inode(struct inode *inode, int how)
1493 struct nfs_inode *nfsi = NFS_I(inode);
1497 spin_lock(&nfsi->req_lock);
1498 res = nfs_scan_commit(inode, &head, 0, 0);
1499 spin_unlock(&nfsi->req_lock);
1501 int error = nfs_commit_list(inode, &head, how);
1509 int nfs_sync_inode_wait(struct inode *inode, unsigned long idx_start,
1510 unsigned int npages, int how)
1512 struct nfs_inode *nfsi = NFS_I(inode);
1514 int nocommit = how & FLUSH_NOCOMMIT;
1517 how &= ~FLUSH_NOCOMMIT;
1518 spin_lock(&nfsi->req_lock);
1520 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1523 pages = nfs_scan_dirty(inode, &head, idx_start, npages);
1525 spin_unlock(&nfsi->req_lock);
1526 if (how & FLUSH_INVALIDATE) {
1527 nfs_cancel_dirty_list(&head);
1530 ret = nfs_flush_list(inode, &head, pages, how);
1531 spin_lock(&nfsi->req_lock);
1536 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1539 if (how & FLUSH_INVALIDATE) {
1540 spin_unlock(&nfsi->req_lock);
1541 nfs_cancel_commit_list(&head);
1543 spin_lock(&nfsi->req_lock);
1546 pages += nfs_scan_commit(inode, &head, 0, 0);
1547 spin_unlock(&nfsi->req_lock);
1548 ret = nfs_commit_list(inode, &head, how);
1549 spin_lock(&nfsi->req_lock);
1551 spin_unlock(&nfsi->req_lock);
1555 int __init nfs_init_writepagecache(void)
1557 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1558 sizeof(struct nfs_write_data),
1559 0, SLAB_HWCACHE_ALIGN,
1561 if (nfs_wdata_cachep == NULL)
1564 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1566 if (nfs_wdata_mempool == NULL)
1569 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1571 if (nfs_commit_mempool == NULL)
1577 void nfs_destroy_writepagecache(void)
1579 mempool_destroy(nfs_commit_mempool);
1580 mempool_destroy(nfs_wdata_mempool);
1581 kmem_cache_destroy(nfs_wdata_cachep);