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_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 static 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(void)
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);
105 void nfs_commit_rcu_free(struct rcu_head *head)
107 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
108 if (p && (p->pagevec != &p->page_array[0]))
110 mempool_free(p, nfs_commit_mempool);
113 void nfs_commit_free(struct nfs_write_data *wdata)
115 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
118 struct nfs_write_data *nfs_writedata_alloc(size_t len)
120 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
121 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
124 memset(p, 0, sizeof(*p));
125 INIT_LIST_HEAD(&p->pages);
126 p->npages = pagecount;
127 if (pagecount <= ARRAY_SIZE(p->page_array))
128 p->pagevec = p->page_array;
130 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
132 mempool_free(p, nfs_wdata_mempool);
140 static void nfs_writedata_rcu_free(struct rcu_head *head)
142 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
143 if (p && (p->pagevec != &p->page_array[0]))
145 mempool_free(p, nfs_wdata_mempool);
148 static void nfs_writedata_free(struct nfs_write_data *wdata)
150 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
153 void nfs_writedata_release(void *wdata)
155 nfs_writedata_free(wdata);
158 /* Adjust the file length if we're writing beyond the end */
159 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
161 struct inode *inode = page->mapping->host;
162 loff_t end, i_size = i_size_read(inode);
163 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
165 if (i_size > 0 && page->index < end_index)
167 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
170 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
171 i_size_write(inode, end);
174 /* We can set the PG_uptodate flag if we see that a write request
175 * covers the full page.
177 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
181 if (PageUptodate(page))
185 if (count == PAGE_CACHE_SIZE) {
186 SetPageUptodate(page);
190 end_offs = i_size_read(page->mapping->host) - 1;
193 /* Is this the last page? */
194 if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
196 /* This is the last page: set PG_uptodate if we cover the entire
197 * extent of the data, then zero the rest of the page.
199 if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
200 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
201 SetPageUptodate(page);
206 * Write a page synchronously.
207 * Offset is the data offset within the page.
209 static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
210 struct page *page, unsigned int offset, unsigned int count,
213 unsigned int wsize = NFS_SERVER(inode)->wsize;
214 int result, written = 0;
215 struct nfs_write_data *wdata;
217 wdata = nfs_writedata_alloc(wsize);
222 wdata->cred = ctx->cred;
223 wdata->inode = inode;
224 wdata->args.fh = NFS_FH(inode);
225 wdata->args.context = ctx;
226 wdata->args.pages = &page;
227 wdata->args.stable = NFS_FILE_SYNC;
228 wdata->args.pgbase = offset;
229 wdata->args.count = wsize;
230 wdata->res.fattr = &wdata->fattr;
231 wdata->res.verf = &wdata->verf;
233 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
235 (long long)NFS_FILEID(inode),
236 count, (long long)(page_offset(page) + offset));
238 set_page_writeback(page);
239 nfs_begin_data_update(inode);
242 wdata->args.count = count;
243 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
245 result = NFS_PROTO(inode)->write(wdata);
248 /* Must mark the page invalid after I/O error */
249 ClearPageUptodate(page);
252 if (result < wdata->args.count)
253 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
254 wdata->args.count, result);
256 wdata->args.offset += result;
257 wdata->args.pgbase += result;
260 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
262 /* Update file length */
263 nfs_grow_file(page, offset, written);
264 /* Set the PG_uptodate flag? */
265 nfs_mark_uptodate(page, offset, written);
268 ClearPageError(page);
271 nfs_end_data_update(inode);
272 end_page_writeback(page);
273 nfs_writedata_release(wdata);
274 return written ? written : result;
277 static int nfs_writepage_async(struct nfs_open_context *ctx,
278 struct inode *inode, struct page *page,
279 unsigned int offset, unsigned int count)
281 struct nfs_page *req;
283 req = nfs_update_request(ctx, inode, page, offset, count);
286 /* Update file length */
287 nfs_grow_file(page, offset, count);
288 /* Set the PG_uptodate flag? */
289 nfs_mark_uptodate(page, offset, count);
290 nfs_unlock_request(req);
294 static int wb_priority(struct writeback_control *wbc)
296 if (wbc->for_reclaim)
297 return FLUSH_HIGHPRI;
298 if (wbc->for_kupdate)
304 * Write an mmapped page to the server.
306 int nfs_writepage(struct page *page, struct writeback_control *wbc)
308 struct nfs_open_context *ctx;
309 struct inode *inode = page->mapping->host;
310 unsigned long end_index;
311 unsigned offset = PAGE_CACHE_SIZE;
312 loff_t i_size = i_size_read(inode);
313 int inode_referenced = 0;
314 int priority = wb_priority(wbc);
317 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
318 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
321 * Note: We need to ensure that we have a reference to the inode
322 * if we are to do asynchronous writes. If not, waiting
323 * in nfs_wait_on_request() may deadlock with clear_inode().
325 * If igrab() fails here, then it is in any case safe to
326 * call nfs_wb_page(), since there will be no pending writes.
328 if (igrab(inode) != 0)
329 inode_referenced = 1;
330 end_index = i_size >> PAGE_CACHE_SHIFT;
332 /* Ensure we've flushed out any previous writes */
333 nfs_wb_page_priority(inode, page, priority);
336 if (page->index < end_index)
338 /* things got complicated... */
339 offset = i_size & (PAGE_CACHE_SIZE-1);
341 /* OK, are we completely out? */
342 err = 0; /* potential race with truncate - ignore */
343 if (page->index >= end_index+1 || !offset)
346 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
352 if (!IS_SYNC(inode) && inode_referenced) {
353 err = nfs_writepage_async(ctx, inode, page, 0, offset);
354 if (!wbc->for_writepages)
355 nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
357 err = nfs_writepage_sync(ctx, inode, page, 0,
361 redirty_page_for_writepage(wbc, page);
366 put_nfs_open_context(ctx);
369 if (inode_referenced)
375 * Note: causes nfs_update_request() to block on the assumption
376 * that the writeback is generated due to memory pressure.
378 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
380 struct backing_dev_info *bdi = mapping->backing_dev_info;
381 struct inode *inode = mapping->host;
384 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
386 err = generic_writepages(mapping, wbc);
389 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
390 if (wbc->nonblocking)
392 nfs_wait_on_write_congestion(mapping, 0);
394 err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
397 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
398 wbc->nr_to_write -= err;
399 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
400 err = nfs_wait_on_requests(inode, 0, 0);
404 err = nfs_commit_inode(inode, wb_priority(wbc));
406 wbc->nr_to_write -= err;
410 clear_bit(BDI_write_congested, &bdi->state);
411 wake_up_all(&nfs_write_congestion);
412 congestion_end(WRITE);
417 * Insert a write request into an inode
419 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
421 struct nfs_inode *nfsi = NFS_I(inode);
424 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
425 BUG_ON(error == -EEXIST);
430 nfs_begin_data_update(inode);
431 if (nfs_have_delegation(inode, FMODE_WRITE))
434 SetPagePrivate(req->wb_page);
436 atomic_inc(&req->wb_count);
441 * Insert a write request into an inode
443 static void nfs_inode_remove_request(struct nfs_page *req)
445 struct inode *inode = req->wb_context->dentry->d_inode;
446 struct nfs_inode *nfsi = NFS_I(inode);
448 BUG_ON (!NFS_WBACK_BUSY(req));
450 spin_lock(&nfsi->req_lock);
451 ClearPagePrivate(req->wb_page);
452 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
455 spin_unlock(&nfsi->req_lock);
456 nfs_end_data_update(inode);
459 spin_unlock(&nfsi->req_lock);
460 nfs_clear_request(req);
461 nfs_release_request(req);
467 static inline struct nfs_page *
468 _nfs_find_request(struct inode *inode, unsigned long index)
470 struct nfs_inode *nfsi = NFS_I(inode);
471 struct nfs_page *req;
473 req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
475 atomic_inc(&req->wb_count);
479 static struct nfs_page *
480 nfs_find_request(struct inode *inode, unsigned long index)
482 struct nfs_page *req;
483 struct nfs_inode *nfsi = NFS_I(inode);
485 spin_lock(&nfsi->req_lock);
486 req = _nfs_find_request(inode, index);
487 spin_unlock(&nfsi->req_lock);
492 * Add a request to the inode's dirty list.
495 nfs_mark_request_dirty(struct nfs_page *req)
497 struct inode *inode = req->wb_context->dentry->d_inode;
498 struct nfs_inode *nfsi = NFS_I(inode);
500 spin_lock(&nfsi->req_lock);
501 radix_tree_tag_set(&nfsi->nfs_page_tree,
502 req->wb_index, NFS_PAGE_TAG_DIRTY);
503 nfs_list_add_request(req, &nfsi->dirty);
505 spin_unlock(&nfsi->req_lock);
506 inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
507 mark_inode_dirty(inode);
511 * Check if a request is dirty
514 nfs_dirty_request(struct nfs_page *req)
516 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
517 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
520 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
522 * Add a request to the inode's commit list.
525 nfs_mark_request_commit(struct nfs_page *req)
527 struct inode *inode = req->wb_context->dentry->d_inode;
528 struct nfs_inode *nfsi = NFS_I(inode);
530 spin_lock(&nfsi->req_lock);
531 nfs_list_add_request(req, &nfsi->commit);
533 spin_unlock(&nfsi->req_lock);
534 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
535 mark_inode_dirty(inode);
540 * Wait for a request to complete.
542 * Interruptible by signals only if mounted with intr flag.
544 static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
546 struct nfs_inode *nfsi = NFS_I(inode);
547 struct nfs_page *req;
548 unsigned long idx_end, next;
549 unsigned int res = 0;
555 idx_end = idx_start + npages - 1;
558 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
559 if (req->wb_index > idx_end)
562 next = req->wb_index + 1;
563 BUG_ON(!NFS_WBACK_BUSY(req));
565 atomic_inc(&req->wb_count);
566 spin_unlock(&nfsi->req_lock);
567 error = nfs_wait_on_request(req);
568 nfs_release_request(req);
569 spin_lock(&nfsi->req_lock);
577 static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
579 struct nfs_inode *nfsi = NFS_I(inode);
582 spin_lock(&nfsi->req_lock);
583 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
584 spin_unlock(&nfsi->req_lock);
588 static void nfs_cancel_dirty_list(struct list_head *head)
590 struct nfs_page *req;
591 while(!list_empty(head)) {
592 req = nfs_list_entry(head->next);
593 nfs_list_remove_request(req);
594 nfs_inode_remove_request(req);
595 nfs_clear_page_writeback(req);
599 static void nfs_cancel_commit_list(struct list_head *head)
601 struct nfs_page *req;
603 while(!list_empty(head)) {
604 req = nfs_list_entry(head->next);
605 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
606 nfs_list_remove_request(req);
607 nfs_inode_remove_request(req);
608 nfs_unlock_request(req);
613 * nfs_scan_dirty - Scan an inode for dirty requests
614 * @inode: NFS inode to scan
615 * @dst: destination list
616 * @idx_start: lower bound of page->index to scan.
617 * @npages: idx_start + npages sets the upper bound to scan.
619 * Moves requests from the inode's dirty page list.
620 * The requests are *not* checked to ensure that they form a contiguous set.
623 nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
625 struct nfs_inode *nfsi = NFS_I(inode);
628 if (nfsi->ndirty != 0) {
629 res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages);
631 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
632 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
637 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
639 * nfs_scan_commit - Scan an inode for commit requests
640 * @inode: NFS inode to scan
641 * @dst: destination list
642 * @idx_start: lower bound of page->index to scan.
643 * @npages: idx_start + npages sets the upper bound to scan.
645 * Moves requests from the inode's 'commit' request list.
646 * The requests are *not* checked to ensure that they form a contiguous set.
649 nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
651 struct nfs_inode *nfsi = NFS_I(inode);
654 if (nfsi->ncommit != 0) {
655 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
656 nfsi->ncommit -= res;
657 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
658 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
663 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
669 static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
671 struct backing_dev_info *bdi = mapping->backing_dev_info;
677 if (!bdi_write_congested(bdi))
680 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
683 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
686 rpc_clnt_sigmask(clnt, &oldset);
687 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
688 if (bdi_write_congested(bdi)) {
694 rpc_clnt_sigunmask(clnt, &oldset);
696 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
697 if (bdi_write_congested(bdi))
700 finish_wait(&nfs_write_congestion, &wait);
706 * Try to update any existing write request, or create one if there is none.
707 * In order to match, the request's credentials must match those of
708 * the calling process.
710 * Note: Should always be called with the Page Lock held!
712 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
713 struct inode *inode, struct page *page,
714 unsigned int offset, unsigned int bytes)
716 struct nfs_server *server = NFS_SERVER(inode);
717 struct nfs_inode *nfsi = NFS_I(inode);
718 struct nfs_page *req, *new = NULL;
719 unsigned long rqend, end;
721 end = offset + bytes;
723 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
724 return ERR_PTR(-ERESTARTSYS);
726 /* Loop over all inode entries and see if we find
727 * A request for the page we wish to update
729 spin_lock(&nfsi->req_lock);
730 req = _nfs_find_request(inode, page->index);
732 if (!nfs_lock_request_dontget(req)) {
734 spin_unlock(&nfsi->req_lock);
735 error = nfs_wait_on_request(req);
736 nfs_release_request(req);
739 nfs_release_request(new);
740 return ERR_PTR(error);
744 spin_unlock(&nfsi->req_lock);
746 nfs_release_request(new);
752 nfs_lock_request_dontget(new);
753 error = nfs_inode_add_request(inode, new);
755 spin_unlock(&nfsi->req_lock);
756 nfs_unlock_request(new);
757 return ERR_PTR(error);
759 spin_unlock(&nfsi->req_lock);
760 nfs_mark_request_dirty(new);
763 spin_unlock(&nfsi->req_lock);
765 new = nfs_create_request(ctx, inode, page, offset, bytes);
770 /* We have a request for our page.
771 * If the creds don't match, or the
772 * page addresses don't match,
773 * tell the caller to wait on the conflicting
776 rqend = req->wb_offset + req->wb_bytes;
777 if (req->wb_context != ctx
778 || req->wb_page != page
779 || !nfs_dirty_request(req)
780 || offset > rqend || end < req->wb_offset) {
781 nfs_unlock_request(req);
782 return ERR_PTR(-EBUSY);
785 /* Okay, the request matches. Update the region */
786 if (offset < req->wb_offset) {
787 req->wb_offset = offset;
788 req->wb_pgbase = offset;
789 req->wb_bytes = rqend - req->wb_offset;
793 req->wb_bytes = end - req->wb_offset;
798 int nfs_flush_incompatible(struct file *file, struct page *page)
800 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
801 struct inode *inode = page->mapping->host;
802 struct nfs_page *req;
805 * Look for a request corresponding to this page. If there
806 * is one, and it belongs to another file, we flush it out
807 * before we try to copy anything into the page. Do this
808 * due to the lack of an ACCESS-type call in NFSv2.
809 * Also do the same if we find a request from an existing
812 req = nfs_find_request(inode, page->index);
814 if (req->wb_page != page || ctx != req->wb_context)
815 status = nfs_wb_page(inode, page);
816 nfs_release_request(req);
818 return (status < 0) ? status : 0;
822 * Update and possibly write a cached page of an NFS file.
824 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
825 * things with a page scheduled for an RPC call (e.g. invalidate it).
827 int nfs_updatepage(struct file *file, struct page *page,
828 unsigned int offset, unsigned int count)
830 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
831 struct inode *inode = page->mapping->host;
832 struct nfs_page *req;
835 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
837 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
838 file->f_dentry->d_parent->d_name.name,
839 file->f_dentry->d_name.name, count,
840 (long long)(page_offset(page) +offset));
842 if (IS_SYNC(inode)) {
843 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
845 if (offset == 0 && status == PAGE_CACHE_SIZE)
846 SetPageUptodate(page);
852 /* If we're not using byte range locks, and we know the page
853 * is entirely in cache, it may be more efficient to avoid
854 * fragmenting write requests.
856 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
857 loff_t end_offs = i_size_read(inode) - 1;
858 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
862 if (unlikely(end_offs < 0)) {
864 } else if (page->index == end_index) {
866 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
869 } else if (page->index < end_index)
870 count = PAGE_CACHE_SIZE;
874 * Try to find an NFS request corresponding to this page
876 * If the existing request cannot be updated, we must flush
880 req = nfs_update_request(ctx, inode, page, offset, count);
881 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
882 if (status != -EBUSY)
884 /* Request could not be updated. Flush it out and try again */
885 status = nfs_wb_page(inode, page);
886 } while (status >= 0);
892 /* Update file length */
893 nfs_grow_file(page, offset, count);
894 /* Set the PG_uptodate flag? */
895 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
896 nfs_unlock_request(req);
898 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
899 status, (long long)i_size_read(inode));
901 ClearPageUptodate(page);
905 static void nfs_writepage_release(struct nfs_page *req)
907 end_page_writeback(req->wb_page);
909 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
910 if (!PageError(req->wb_page)) {
911 if (NFS_NEED_RESCHED(req)) {
912 nfs_mark_request_dirty(req);
914 } else if (NFS_NEED_COMMIT(req)) {
915 nfs_mark_request_commit(req);
919 nfs_inode_remove_request(req);
922 nfs_clear_commit(req);
923 nfs_clear_reschedule(req);
925 nfs_inode_remove_request(req);
927 nfs_clear_page_writeback(req);
930 static inline int flush_task_priority(int how)
932 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
934 return RPC_PRIORITY_HIGH;
936 return RPC_PRIORITY_LOW;
938 return RPC_PRIORITY_NORMAL;
942 * Set up the argument/result storage required for the RPC call.
944 static void nfs_write_rpcsetup(struct nfs_page *req,
945 struct nfs_write_data *data,
946 const struct rpc_call_ops *call_ops,
947 unsigned int count, unsigned int offset,
953 /* Set up the RPC argument and reply structs
954 * NB: take care not to mess about with data->commit et al. */
957 data->inode = inode = req->wb_context->dentry->d_inode;
958 data->cred = req->wb_context->cred;
960 data->args.fh = NFS_FH(inode);
961 data->args.offset = req_offset(req) + offset;
962 data->args.pgbase = req->wb_pgbase + offset;
963 data->args.pages = data->pagevec;
964 data->args.count = count;
965 data->args.context = req->wb_context;
967 data->res.fattr = &data->fattr;
968 data->res.count = count;
969 data->res.verf = &data->verf;
970 nfs_fattr_init(&data->fattr);
972 /* Set up the initial task struct. */
973 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
974 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
975 NFS_PROTO(inode)->write_setup(data, how);
977 data->task.tk_priority = flush_task_priority(how);
978 data->task.tk_cookie = (unsigned long)inode;
980 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
983 (long long)NFS_FILEID(inode),
985 (unsigned long long)data->args.offset);
988 static void nfs_execute_write(struct nfs_write_data *data)
990 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
993 rpc_clnt_sigmask(clnt, &oldset);
995 rpc_execute(&data->task);
997 rpc_clnt_sigunmask(clnt, &oldset);
1001 * Generate multiple small requests to write out a single
1002 * contiguous dirty area on one page.
1004 static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
1006 struct nfs_page *req = nfs_list_entry(head->next);
1007 struct page *page = req->wb_page;
1008 struct nfs_write_data *data;
1009 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
1010 unsigned int offset;
1014 nfs_list_remove_request(req);
1016 nbytes = req->wb_bytes;
1018 size_t len = min(nbytes, wsize);
1020 data = nfs_writedata_alloc(len);
1023 list_add(&data->pages, &list);
1026 } while (nbytes != 0);
1027 atomic_set(&req->wb_complete, requests);
1029 ClearPageError(page);
1030 set_page_writeback(page);
1032 nbytes = req->wb_bytes;
1034 data = list_entry(list.next, struct nfs_write_data, pages);
1035 list_del_init(&data->pages);
1037 data->pagevec[0] = page;
1039 if (nbytes > wsize) {
1040 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1041 wsize, offset, how);
1045 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1046 nbytes, offset, how);
1049 nfs_execute_write(data);
1050 } while (nbytes != 0);
1055 while (!list_empty(&list)) {
1056 data = list_entry(list.next, struct nfs_write_data, pages);
1057 list_del(&data->pages);
1058 nfs_writedata_release(data);
1060 nfs_mark_request_dirty(req);
1061 nfs_clear_page_writeback(req);
1066 * Create an RPC task for the given write request and kick it.
1067 * The page must have been locked by the caller.
1069 * It may happen that the page we're passed is not marked dirty.
1070 * This is the case if nfs_updatepage detects a conflicting request
1071 * that has been written but not committed.
1073 static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
1075 struct nfs_page *req;
1076 struct page **pages;
1077 struct nfs_write_data *data;
1080 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
1084 pages = data->pagevec;
1086 while (!list_empty(head)) {
1087 req = nfs_list_entry(head->next);
1088 nfs_list_remove_request(req);
1089 nfs_list_add_request(req, &data->pages);
1090 ClearPageError(req->wb_page);
1091 set_page_writeback(req->wb_page);
1092 *pages++ = req->wb_page;
1093 count += req->wb_bytes;
1095 req = nfs_list_entry(data->pages.next);
1097 /* Set up the argument struct */
1098 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
1100 nfs_execute_write(data);
1103 while (!list_empty(head)) {
1104 struct nfs_page *req = nfs_list_entry(head->next);
1105 nfs_list_remove_request(req);
1106 nfs_mark_request_dirty(req);
1107 nfs_clear_page_writeback(req);
1112 static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
1114 LIST_HEAD(one_request);
1115 int (*flush_one)(struct inode *, struct list_head *, int);
1116 struct nfs_page *req;
1117 int wpages = NFS_SERVER(inode)->wpages;
1118 int wsize = NFS_SERVER(inode)->wsize;
1121 flush_one = nfs_flush_one;
1122 if (wsize < PAGE_CACHE_SIZE)
1123 flush_one = nfs_flush_multi;
1124 /* For single writes, FLUSH_STABLE is more efficient */
1125 if (npages <= wpages && npages == NFS_I(inode)->npages
1126 && nfs_list_entry(head->next)->wb_bytes <= wsize)
1127 how |= FLUSH_STABLE;
1130 nfs_coalesce_requests(head, &one_request, wpages);
1131 req = nfs_list_entry(one_request.next);
1132 error = flush_one(inode, &one_request, how);
1135 } while (!list_empty(head));
1138 while (!list_empty(head)) {
1139 req = nfs_list_entry(head->next);
1140 nfs_list_remove_request(req);
1141 nfs_mark_request_dirty(req);
1142 nfs_clear_page_writeback(req);
1148 * Handle a write reply that flushed part of a page.
1150 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
1152 struct nfs_write_data *data = calldata;
1153 struct nfs_page *req = data->req;
1154 struct page *page = req->wb_page;
1156 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1157 req->wb_context->dentry->d_inode->i_sb->s_id,
1158 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1160 (long long)req_offset(req));
1162 if (nfs_writeback_done(task, data) != 0)
1165 if (task->tk_status < 0) {
1166 ClearPageUptodate(page);
1168 req->wb_context->error = task->tk_status;
1169 dprintk(", error = %d\n", task->tk_status);
1171 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1172 if (data->verf.committed < NFS_FILE_SYNC) {
1173 if (!NFS_NEED_COMMIT(req)) {
1174 nfs_defer_commit(req);
1175 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1176 dprintk(" defer commit\n");
1177 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1178 nfs_defer_reschedule(req);
1179 dprintk(" server reboot detected\n");
1186 if (atomic_dec_and_test(&req->wb_complete))
1187 nfs_writepage_release(req);
1190 static const struct rpc_call_ops nfs_write_partial_ops = {
1191 .rpc_call_done = nfs_writeback_done_partial,
1192 .rpc_release = nfs_writedata_release,
1196 * Handle a write reply that flushes a whole page.
1198 * FIXME: There is an inherent race with invalidate_inode_pages and
1199 * writebacks since the page->count is kept > 1 for as long
1200 * as the page has a write request pending.
1202 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1204 struct nfs_write_data *data = calldata;
1205 struct nfs_page *req;
1208 if (nfs_writeback_done(task, data) != 0)
1211 /* Update attributes as result of writeback. */
1212 while (!list_empty(&data->pages)) {
1213 req = nfs_list_entry(data->pages.next);
1214 nfs_list_remove_request(req);
1215 page = req->wb_page;
1217 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1218 req->wb_context->dentry->d_inode->i_sb->s_id,
1219 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1221 (long long)req_offset(req));
1223 if (task->tk_status < 0) {
1224 ClearPageUptodate(page);
1226 req->wb_context->error = task->tk_status;
1227 end_page_writeback(page);
1228 nfs_inode_remove_request(req);
1229 dprintk(", error = %d\n", task->tk_status);
1232 end_page_writeback(page);
1234 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1235 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1236 nfs_inode_remove_request(req);
1240 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1241 nfs_mark_request_commit(req);
1242 dprintk(" marked for commit\n");
1244 nfs_inode_remove_request(req);
1247 nfs_clear_page_writeback(req);
1251 static const struct rpc_call_ops nfs_write_full_ops = {
1252 .rpc_call_done = nfs_writeback_done_full,
1253 .rpc_release = nfs_writedata_release,
1258 * This function is called when the WRITE call is complete.
1260 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1262 struct nfs_writeargs *argp = &data->args;
1263 struct nfs_writeres *resp = &data->res;
1266 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1267 task->tk_pid, task->tk_status);
1270 * ->write_done will attempt to use post-op attributes to detect
1271 * conflicting writes by other clients. A strict interpretation
1272 * of close-to-open would allow us to continue caching even if
1273 * another writer had changed the file, but some applications
1274 * depend on tighter cache coherency when writing.
1276 status = NFS_PROTO(data->inode)->write_done(task, data);
1279 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1281 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1282 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1283 /* We tried a write call, but the server did not
1284 * commit data to stable storage even though we
1286 * Note: There is a known bug in Tru64 < 5.0 in which
1287 * the server reports NFS_DATA_SYNC, but performs
1288 * NFS_FILE_SYNC. We therefore implement this checking
1289 * as a dprintk() in order to avoid filling syslog.
1291 static unsigned long complain;
1293 if (time_before(complain, jiffies)) {
1294 dprintk("NFS: faulty NFS server %s:"
1295 " (committed = %d) != (stable = %d)\n",
1296 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1297 resp->verf->committed, argp->stable);
1298 complain = jiffies + 300 * HZ;
1302 /* Is this a short write? */
1303 if (task->tk_status >= 0 && resp->count < argp->count) {
1304 static unsigned long complain;
1306 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1308 /* Has the server at least made some progress? */
1309 if (resp->count != 0) {
1310 /* Was this an NFSv2 write or an NFSv3 stable write? */
1311 if (resp->verf->committed != NFS_UNSTABLE) {
1312 /* Resend from where the server left off */
1313 argp->offset += resp->count;
1314 argp->pgbase += resp->count;
1315 argp->count -= resp->count;
1317 /* Resend as a stable write in order to avoid
1318 * headaches in the case of a server crash.
1320 argp->stable = NFS_FILE_SYNC;
1322 rpc_restart_call(task);
1325 if (time_before(complain, jiffies)) {
1327 "NFS: Server wrote zero bytes, expected %u.\n",
1329 complain = jiffies + 300 * HZ;
1331 /* Can't do anything about it except throw an error. */
1332 task->tk_status = -EIO;
1338 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1339 void nfs_commit_release(void *wdata)
1341 nfs_commit_free(wdata);
1345 * Set up the argument/result storage required for the RPC call.
1347 static void nfs_commit_rpcsetup(struct list_head *head,
1348 struct nfs_write_data *data,
1351 struct nfs_page *first;
1352 struct inode *inode;
1355 /* Set up the RPC argument and reply structs
1356 * NB: take care not to mess about with data->commit et al. */
1358 list_splice_init(head, &data->pages);
1359 first = nfs_list_entry(data->pages.next);
1360 inode = first->wb_context->dentry->d_inode;
1362 data->inode = inode;
1363 data->cred = first->wb_context->cred;
1365 data->args.fh = NFS_FH(data->inode);
1366 /* Note: we always request a commit of the entire inode */
1367 data->args.offset = 0;
1368 data->args.count = 0;
1369 data->res.count = 0;
1370 data->res.fattr = &data->fattr;
1371 data->res.verf = &data->verf;
1372 nfs_fattr_init(&data->fattr);
1374 /* Set up the initial task struct. */
1375 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1376 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
1377 NFS_PROTO(inode)->commit_setup(data, how);
1379 data->task.tk_priority = flush_task_priority(how);
1380 data->task.tk_cookie = (unsigned long)inode;
1382 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
1386 * Commit dirty pages
1389 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1391 struct nfs_write_data *data;
1392 struct nfs_page *req;
1394 data = nfs_commit_alloc();
1399 /* Set up the argument struct */
1400 nfs_commit_rpcsetup(head, data, how);
1402 nfs_execute_write(data);
1405 while (!list_empty(head)) {
1406 req = nfs_list_entry(head->next);
1407 nfs_list_remove_request(req);
1408 nfs_mark_request_commit(req);
1409 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1410 nfs_clear_page_writeback(req);
1416 * COMMIT call returned
1418 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1420 struct nfs_write_data *data = calldata;
1421 struct nfs_page *req;
1423 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1424 task->tk_pid, task->tk_status);
1426 /* Call the NFS version-specific code */
1427 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1430 while (!list_empty(&data->pages)) {
1431 req = nfs_list_entry(data->pages.next);
1432 nfs_list_remove_request(req);
1433 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1435 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1436 req->wb_context->dentry->d_inode->i_sb->s_id,
1437 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1439 (long long)req_offset(req));
1440 if (task->tk_status < 0) {
1441 req->wb_context->error = task->tk_status;
1442 nfs_inode_remove_request(req);
1443 dprintk(", error = %d\n", task->tk_status);
1447 /* Okay, COMMIT succeeded, apparently. Check the verifier
1448 * returned by the server against all stored verfs. */
1449 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1450 /* We have a match */
1451 nfs_inode_remove_request(req);
1455 /* We have a mismatch. Write the page again */
1456 dprintk(" mismatch\n");
1457 nfs_mark_request_dirty(req);
1459 nfs_clear_page_writeback(req);
1463 static const struct rpc_call_ops nfs_commit_ops = {
1464 .rpc_call_done = nfs_commit_done,
1465 .rpc_release = nfs_commit_release,
1468 static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1474 static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
1475 unsigned int npages, int how)
1477 struct nfs_inode *nfsi = NFS_I(inode);
1481 spin_lock(&nfsi->req_lock);
1482 res = nfs_scan_dirty(inode, &head, idx_start, npages);
1483 spin_unlock(&nfsi->req_lock);
1485 int error = nfs_flush_list(inode, &head, res, how);
1492 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1493 int nfs_commit_inode(struct inode *inode, int how)
1495 struct nfs_inode *nfsi = NFS_I(inode);
1499 spin_lock(&nfsi->req_lock);
1500 res = nfs_scan_commit(inode, &head, 0, 0);
1501 spin_unlock(&nfsi->req_lock);
1503 int error = nfs_commit_list(inode, &head, how);
1511 int nfs_sync_inode_wait(struct inode *inode, unsigned long idx_start,
1512 unsigned int npages, int how)
1514 struct nfs_inode *nfsi = NFS_I(inode);
1516 int nocommit = how & FLUSH_NOCOMMIT;
1519 how &= ~FLUSH_NOCOMMIT;
1520 spin_lock(&nfsi->req_lock);
1522 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1525 pages = nfs_scan_dirty(inode, &head, idx_start, npages);
1527 spin_unlock(&nfsi->req_lock);
1528 if (how & FLUSH_INVALIDATE)
1529 nfs_cancel_dirty_list(&head);
1531 ret = nfs_flush_list(inode, &head, pages, how);
1532 spin_lock(&nfsi->req_lock);
1537 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1540 if (how & FLUSH_INVALIDATE) {
1541 spin_unlock(&nfsi->req_lock);
1542 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);