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_free(struct nfs_write_data *p)
107 if (p && (p->pagevec != &p->page_array[0]))
109 mempool_free(p, nfs_commit_mempool);
112 struct nfs_write_data *nfs_writedata_alloc(size_t len)
114 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
115 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
118 memset(p, 0, sizeof(*p));
119 INIT_LIST_HEAD(&p->pages);
120 p->npages = pagecount;
121 if (pagecount <= ARRAY_SIZE(p->page_array))
122 p->pagevec = p->page_array;
124 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
126 mempool_free(p, nfs_wdata_mempool);
134 static void nfs_writedata_free(struct nfs_write_data *p)
136 if (p && (p->pagevec != &p->page_array[0]))
138 mempool_free(p, nfs_wdata_mempool);
141 void nfs_writedata_release(void *wdata)
143 nfs_writedata_free(wdata);
146 /* Adjust the file length if we're writing beyond the end */
147 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
149 struct inode *inode = page->mapping->host;
150 loff_t end, i_size = i_size_read(inode);
151 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
153 if (i_size > 0 && page->index < end_index)
155 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
158 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
159 i_size_write(inode, end);
162 /* We can set the PG_uptodate flag if we see that a write request
163 * covers the full page.
165 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
169 if (PageUptodate(page))
173 if (count == PAGE_CACHE_SIZE) {
174 SetPageUptodate(page);
178 end_offs = i_size_read(page->mapping->host) - 1;
181 /* Is this the last page? */
182 if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
184 /* This is the last page: set PG_uptodate if we cover the entire
185 * extent of the data, then zero the rest of the page.
187 if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
188 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
189 SetPageUptodate(page);
194 * Write a page synchronously.
195 * Offset is the data offset within the page.
197 static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
198 struct page *page, unsigned int offset, unsigned int count,
201 unsigned int wsize = NFS_SERVER(inode)->wsize;
202 int result, written = 0;
203 struct nfs_write_data *wdata;
205 wdata = nfs_writedata_alloc(wsize);
210 wdata->cred = ctx->cred;
211 wdata->inode = inode;
212 wdata->args.fh = NFS_FH(inode);
213 wdata->args.context = ctx;
214 wdata->args.pages = &page;
215 wdata->args.stable = NFS_FILE_SYNC;
216 wdata->args.pgbase = offset;
217 wdata->args.count = wsize;
218 wdata->res.fattr = &wdata->fattr;
219 wdata->res.verf = &wdata->verf;
221 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
223 (long long)NFS_FILEID(inode),
224 count, (long long)(page_offset(page) + offset));
226 set_page_writeback(page);
227 nfs_begin_data_update(inode);
230 wdata->args.count = count;
231 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
233 result = NFS_PROTO(inode)->write(wdata);
236 /* Must mark the page invalid after I/O error */
237 ClearPageUptodate(page);
240 if (result < wdata->args.count)
241 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
242 wdata->args.count, result);
244 wdata->args.offset += result;
245 wdata->args.pgbase += result;
248 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
250 /* Update file length */
251 nfs_grow_file(page, offset, written);
252 /* Set the PG_uptodate flag? */
253 nfs_mark_uptodate(page, offset, written);
256 ClearPageError(page);
259 nfs_end_data_update(inode);
260 end_page_writeback(page);
261 nfs_writedata_free(wdata);
262 return written ? written : result;
265 static int nfs_writepage_async(struct nfs_open_context *ctx,
266 struct inode *inode, struct page *page,
267 unsigned int offset, unsigned int count)
269 struct nfs_page *req;
271 req = nfs_update_request(ctx, inode, page, offset, count);
274 /* Update file length */
275 nfs_grow_file(page, offset, count);
276 /* Set the PG_uptodate flag? */
277 nfs_mark_uptodate(page, offset, count);
278 nfs_unlock_request(req);
282 static int wb_priority(struct writeback_control *wbc)
284 if (wbc->for_reclaim)
285 return FLUSH_HIGHPRI;
286 if (wbc->for_kupdate)
292 * Write an mmapped page to the server.
294 int nfs_writepage(struct page *page, struct writeback_control *wbc)
296 struct nfs_open_context *ctx;
297 struct inode *inode = page->mapping->host;
298 unsigned long end_index;
299 unsigned offset = PAGE_CACHE_SIZE;
300 loff_t i_size = i_size_read(inode);
301 int inode_referenced = 0;
302 int priority = wb_priority(wbc);
305 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
306 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
309 * Note: We need to ensure that we have a reference to the inode
310 * if we are to do asynchronous writes. If not, waiting
311 * in nfs_wait_on_request() may deadlock with clear_inode().
313 * If igrab() fails here, then it is in any case safe to
314 * call nfs_wb_page(), since there will be no pending writes.
316 if (igrab(inode) != 0)
317 inode_referenced = 1;
318 end_index = i_size >> PAGE_CACHE_SHIFT;
320 /* Ensure we've flushed out any previous writes */
321 nfs_wb_page_priority(inode, page, priority);
324 if (page->index < end_index)
326 /* things got complicated... */
327 offset = i_size & (PAGE_CACHE_SIZE-1);
329 /* OK, are we completely out? */
330 err = 0; /* potential race with truncate - ignore */
331 if (page->index >= end_index+1 || !offset)
334 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
340 if (!IS_SYNC(inode) && inode_referenced) {
341 err = nfs_writepage_async(ctx, inode, page, 0, offset);
342 if (!wbc->for_writepages)
343 nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
345 err = nfs_writepage_sync(ctx, inode, page, 0,
349 redirty_page_for_writepage(wbc, page);
354 put_nfs_open_context(ctx);
357 if (inode_referenced)
363 * Note: causes nfs_update_request() to block on the assumption
364 * that the writeback is generated due to memory pressure.
366 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
368 struct backing_dev_info *bdi = mapping->backing_dev_info;
369 struct inode *inode = mapping->host;
372 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
374 err = generic_writepages(mapping, wbc);
377 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
378 if (wbc->nonblocking)
380 nfs_wait_on_write_congestion(mapping, 0);
382 err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
385 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
386 wbc->nr_to_write -= err;
387 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
388 err = nfs_wait_on_requests(inode, 0, 0);
392 err = nfs_commit_inode(inode, wb_priority(wbc));
394 wbc->nr_to_write -= err;
398 clear_bit(BDI_write_congested, &bdi->state);
399 wake_up_all(&nfs_write_congestion);
400 congestion_end(WRITE);
405 * Insert a write request into an inode
407 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
409 struct nfs_inode *nfsi = NFS_I(inode);
412 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
413 BUG_ON(error == -EEXIST);
418 nfs_begin_data_update(inode);
419 if (nfs_have_delegation(inode, FMODE_WRITE))
422 SetPagePrivate(req->wb_page);
424 atomic_inc(&req->wb_count);
429 * Insert a write request into an inode
431 static void nfs_inode_remove_request(struct nfs_page *req)
433 struct inode *inode = req->wb_context->dentry->d_inode;
434 struct nfs_inode *nfsi = NFS_I(inode);
436 BUG_ON (!NFS_WBACK_BUSY(req));
438 spin_lock(&nfsi->req_lock);
439 ClearPagePrivate(req->wb_page);
440 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
443 spin_unlock(&nfsi->req_lock);
444 nfs_end_data_update(inode);
447 spin_unlock(&nfsi->req_lock);
448 nfs_clear_request(req);
449 nfs_release_request(req);
455 static inline struct nfs_page *
456 _nfs_find_request(struct inode *inode, unsigned long index)
458 struct nfs_inode *nfsi = NFS_I(inode);
459 struct nfs_page *req;
461 req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
463 atomic_inc(&req->wb_count);
467 static struct nfs_page *
468 nfs_find_request(struct inode *inode, unsigned long index)
470 struct nfs_page *req;
471 struct nfs_inode *nfsi = NFS_I(inode);
473 spin_lock(&nfsi->req_lock);
474 req = _nfs_find_request(inode, index);
475 spin_unlock(&nfsi->req_lock);
480 * Add a request to the inode's dirty list.
483 nfs_mark_request_dirty(struct nfs_page *req)
485 struct inode *inode = req->wb_context->dentry->d_inode;
486 struct nfs_inode *nfsi = NFS_I(inode);
488 spin_lock(&nfsi->req_lock);
489 radix_tree_tag_set(&nfsi->nfs_page_tree,
490 req->wb_index, NFS_PAGE_TAG_DIRTY);
491 nfs_list_add_request(req, &nfsi->dirty);
493 spin_unlock(&nfsi->req_lock);
494 inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
495 mark_inode_dirty(inode);
499 * Check if a request is dirty
502 nfs_dirty_request(struct nfs_page *req)
504 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
505 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
508 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
510 * Add a request to the inode's commit list.
513 nfs_mark_request_commit(struct nfs_page *req)
515 struct inode *inode = req->wb_context->dentry->d_inode;
516 struct nfs_inode *nfsi = NFS_I(inode);
518 spin_lock(&nfsi->req_lock);
519 nfs_list_add_request(req, &nfsi->commit);
521 spin_unlock(&nfsi->req_lock);
522 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
523 mark_inode_dirty(inode);
528 * Wait for a request to complete.
530 * Interruptible by signals only if mounted with intr flag.
532 static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
534 struct nfs_inode *nfsi = NFS_I(inode);
535 struct nfs_page *req;
536 unsigned long idx_end, next;
537 unsigned int res = 0;
543 idx_end = idx_start + npages - 1;
546 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
547 if (req->wb_index > idx_end)
550 next = req->wb_index + 1;
551 BUG_ON(!NFS_WBACK_BUSY(req));
553 atomic_inc(&req->wb_count);
554 spin_unlock(&nfsi->req_lock);
555 error = nfs_wait_on_request(req);
556 nfs_release_request(req);
557 spin_lock(&nfsi->req_lock);
565 static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
567 struct nfs_inode *nfsi = NFS_I(inode);
570 spin_lock(&nfsi->req_lock);
571 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
572 spin_unlock(&nfsi->req_lock);
576 static void nfs_cancel_dirty_list(struct list_head *head)
578 struct nfs_page *req;
579 while(!list_empty(head)) {
580 req = nfs_list_entry(head->next);
581 nfs_list_remove_request(req);
582 nfs_inode_remove_request(req);
583 nfs_clear_page_writeback(req);
587 static void nfs_cancel_commit_list(struct list_head *head)
589 struct nfs_page *req;
591 while(!list_empty(head)) {
592 req = nfs_list_entry(head->next);
593 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
594 nfs_list_remove_request(req);
595 nfs_inode_remove_request(req);
596 nfs_unlock_request(req);
601 * nfs_scan_dirty - Scan an inode for dirty requests
602 * @inode: NFS inode to scan
603 * @dst: destination list
604 * @idx_start: lower bound of page->index to scan.
605 * @npages: idx_start + npages sets the upper bound to scan.
607 * Moves requests from the inode's dirty page list.
608 * The requests are *not* checked to ensure that they form a contiguous set.
611 nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
613 struct nfs_inode *nfsi = NFS_I(inode);
616 if (nfsi->ndirty != 0) {
617 res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages);
619 if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
620 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
625 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
627 * nfs_scan_commit - Scan an inode for commit requests
628 * @inode: NFS inode to scan
629 * @dst: destination list
630 * @idx_start: lower bound of page->index to scan.
631 * @npages: idx_start + npages sets the upper bound to scan.
633 * Moves requests from the inode's 'commit' request list.
634 * The requests are *not* checked to ensure that they form a contiguous set.
637 nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
639 struct nfs_inode *nfsi = NFS_I(inode);
642 if (nfsi->ncommit != 0) {
643 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
644 nfsi->ncommit -= res;
645 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
646 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
651 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
657 static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
659 struct backing_dev_info *bdi = mapping->backing_dev_info;
665 if (!bdi_write_congested(bdi))
668 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
671 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
674 rpc_clnt_sigmask(clnt, &oldset);
675 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
676 if (bdi_write_congested(bdi)) {
682 rpc_clnt_sigunmask(clnt, &oldset);
684 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
685 if (bdi_write_congested(bdi))
688 finish_wait(&nfs_write_congestion, &wait);
694 * Try to update any existing write request, or create one if there is none.
695 * In order to match, the request's credentials must match those of
696 * the calling process.
698 * Note: Should always be called with the Page Lock held!
700 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
701 struct inode *inode, struct page *page,
702 unsigned int offset, unsigned int bytes)
704 struct nfs_server *server = NFS_SERVER(inode);
705 struct nfs_inode *nfsi = NFS_I(inode);
706 struct nfs_page *req, *new = NULL;
707 unsigned long rqend, end;
709 end = offset + bytes;
711 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
712 return ERR_PTR(-ERESTARTSYS);
714 /* Loop over all inode entries and see if we find
715 * A request for the page we wish to update
717 spin_lock(&nfsi->req_lock);
718 req = _nfs_find_request(inode, page->index);
720 if (!nfs_lock_request_dontget(req)) {
722 spin_unlock(&nfsi->req_lock);
723 error = nfs_wait_on_request(req);
724 nfs_release_request(req);
727 nfs_release_request(new);
728 return ERR_PTR(error);
732 spin_unlock(&nfsi->req_lock);
734 nfs_release_request(new);
740 nfs_lock_request_dontget(new);
741 error = nfs_inode_add_request(inode, new);
743 spin_unlock(&nfsi->req_lock);
744 nfs_unlock_request(new);
745 return ERR_PTR(error);
747 spin_unlock(&nfsi->req_lock);
748 nfs_mark_request_dirty(new);
751 spin_unlock(&nfsi->req_lock);
753 new = nfs_create_request(ctx, inode, page, offset, bytes);
758 /* We have a request for our page.
759 * If the creds don't match, or the
760 * page addresses don't match,
761 * tell the caller to wait on the conflicting
764 rqend = req->wb_offset + req->wb_bytes;
765 if (req->wb_context != ctx
766 || req->wb_page != page
767 || !nfs_dirty_request(req)
768 || offset > rqend || end < req->wb_offset) {
769 nfs_unlock_request(req);
770 return ERR_PTR(-EBUSY);
773 /* Okay, the request matches. Update the region */
774 if (offset < req->wb_offset) {
775 req->wb_offset = offset;
776 req->wb_pgbase = offset;
777 req->wb_bytes = rqend - req->wb_offset;
781 req->wb_bytes = end - req->wb_offset;
786 int nfs_flush_incompatible(struct file *file, struct page *page)
788 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
789 struct inode *inode = page->mapping->host;
790 struct nfs_page *req;
793 * Look for a request corresponding to this page. If there
794 * is one, and it belongs to another file, we flush it out
795 * before we try to copy anything into the page. Do this
796 * due to the lack of an ACCESS-type call in NFSv2.
797 * Also do the same if we find a request from an existing
800 req = nfs_find_request(inode, page->index);
802 if (req->wb_page != page || ctx != req->wb_context)
803 status = nfs_wb_page(inode, page);
804 nfs_release_request(req);
806 return (status < 0) ? status : 0;
810 * Update and possibly write a cached page of an NFS file.
812 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
813 * things with a page scheduled for an RPC call (e.g. invalidate it).
815 int nfs_updatepage(struct file *file, struct page *page,
816 unsigned int offset, unsigned int count)
818 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
819 struct inode *inode = page->mapping->host;
820 struct nfs_page *req;
823 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
825 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
826 file->f_dentry->d_parent->d_name.name,
827 file->f_dentry->d_name.name, count,
828 (long long)(page_offset(page) +offset));
830 if (IS_SYNC(inode)) {
831 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
833 if (offset == 0 && status == PAGE_CACHE_SIZE)
834 SetPageUptodate(page);
840 /* If we're not using byte range locks, and we know the page
841 * is entirely in cache, it may be more efficient to avoid
842 * fragmenting write requests.
844 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
845 loff_t end_offs = i_size_read(inode) - 1;
846 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
850 if (unlikely(end_offs < 0)) {
852 } else if (page->index == end_index) {
854 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
857 } else if (page->index < end_index)
858 count = PAGE_CACHE_SIZE;
862 * Try to find an NFS request corresponding to this page
864 * If the existing request cannot be updated, we must flush
868 req = nfs_update_request(ctx, inode, page, offset, count);
869 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
870 if (status != -EBUSY)
872 /* Request could not be updated. Flush it out and try again */
873 status = nfs_wb_page(inode, page);
874 } while (status >= 0);
880 /* Update file length */
881 nfs_grow_file(page, offset, count);
882 /* Set the PG_uptodate flag? */
883 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
884 nfs_unlock_request(req);
886 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
887 status, (long long)i_size_read(inode));
889 ClearPageUptodate(page);
893 static void nfs_writepage_release(struct nfs_page *req)
895 end_page_writeback(req->wb_page);
897 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
898 if (!PageError(req->wb_page)) {
899 if (NFS_NEED_RESCHED(req)) {
900 nfs_mark_request_dirty(req);
902 } else if (NFS_NEED_COMMIT(req)) {
903 nfs_mark_request_commit(req);
907 nfs_inode_remove_request(req);
910 nfs_clear_commit(req);
911 nfs_clear_reschedule(req);
913 nfs_inode_remove_request(req);
915 nfs_clear_page_writeback(req);
918 static inline int flush_task_priority(int how)
920 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
922 return RPC_PRIORITY_HIGH;
924 return RPC_PRIORITY_LOW;
926 return RPC_PRIORITY_NORMAL;
930 * Set up the argument/result storage required for the RPC call.
932 static void nfs_write_rpcsetup(struct nfs_page *req,
933 struct nfs_write_data *data,
934 const struct rpc_call_ops *call_ops,
935 unsigned int count, unsigned int offset,
941 /* Set up the RPC argument and reply structs
942 * NB: take care not to mess about with data->commit et al. */
945 data->inode = inode = req->wb_context->dentry->d_inode;
946 data->cred = req->wb_context->cred;
948 data->args.fh = NFS_FH(inode);
949 data->args.offset = req_offset(req) + offset;
950 data->args.pgbase = req->wb_pgbase + offset;
951 data->args.pages = data->pagevec;
952 data->args.count = count;
953 data->args.context = req->wb_context;
955 data->res.fattr = &data->fattr;
956 data->res.count = count;
957 data->res.verf = &data->verf;
958 nfs_fattr_init(&data->fattr);
960 /* Set up the initial task struct. */
961 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
962 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
963 NFS_PROTO(inode)->write_setup(data, how);
965 data->task.tk_priority = flush_task_priority(how);
966 data->task.tk_cookie = (unsigned long)inode;
968 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
971 (long long)NFS_FILEID(inode),
973 (unsigned long long)data->args.offset);
976 static void nfs_execute_write(struct nfs_write_data *data)
978 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
981 rpc_clnt_sigmask(clnt, &oldset);
983 rpc_execute(&data->task);
985 rpc_clnt_sigunmask(clnt, &oldset);
989 * Generate multiple small requests to write out a single
990 * contiguous dirty area on one page.
992 static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
994 struct nfs_page *req = nfs_list_entry(head->next);
995 struct page *page = req->wb_page;
996 struct nfs_write_data *data;
997 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
1002 nfs_list_remove_request(req);
1004 nbytes = req->wb_bytes;
1006 size_t len = min(nbytes, wsize);
1008 data = nfs_writedata_alloc(len);
1011 list_add(&data->pages, &list);
1014 } while (nbytes != 0);
1015 atomic_set(&req->wb_complete, requests);
1017 ClearPageError(page);
1018 set_page_writeback(page);
1020 nbytes = req->wb_bytes;
1022 data = list_entry(list.next, struct nfs_write_data, pages);
1023 list_del_init(&data->pages);
1025 data->pagevec[0] = page;
1027 if (nbytes > wsize) {
1028 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1029 wsize, offset, how);
1033 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1034 nbytes, offset, how);
1037 nfs_execute_write(data);
1038 } while (nbytes != 0);
1043 while (!list_empty(&list)) {
1044 data = list_entry(list.next, struct nfs_write_data, pages);
1045 list_del(&data->pages);
1046 nfs_writedata_free(data);
1048 nfs_mark_request_dirty(req);
1049 nfs_clear_page_writeback(req);
1054 * Create an RPC task for the given write request and kick it.
1055 * The page must have been locked by the caller.
1057 * It may happen that the page we're passed is not marked dirty.
1058 * This is the case if nfs_updatepage detects a conflicting request
1059 * that has been written but not committed.
1061 static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
1063 struct nfs_page *req;
1064 struct page **pages;
1065 struct nfs_write_data *data;
1068 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
1072 pages = data->pagevec;
1074 while (!list_empty(head)) {
1075 req = nfs_list_entry(head->next);
1076 nfs_list_remove_request(req);
1077 nfs_list_add_request(req, &data->pages);
1078 ClearPageError(req->wb_page);
1079 set_page_writeback(req->wb_page);
1080 *pages++ = req->wb_page;
1081 count += req->wb_bytes;
1083 req = nfs_list_entry(data->pages.next);
1085 /* Set up the argument struct */
1086 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
1088 nfs_execute_write(data);
1091 while (!list_empty(head)) {
1092 struct nfs_page *req = nfs_list_entry(head->next);
1093 nfs_list_remove_request(req);
1094 nfs_mark_request_dirty(req);
1095 nfs_clear_page_writeback(req);
1100 static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
1102 LIST_HEAD(one_request);
1103 int (*flush_one)(struct inode *, struct list_head *, int);
1104 struct nfs_page *req;
1105 int wpages = NFS_SERVER(inode)->wpages;
1106 int wsize = NFS_SERVER(inode)->wsize;
1109 flush_one = nfs_flush_one;
1110 if (wsize < PAGE_CACHE_SIZE)
1111 flush_one = nfs_flush_multi;
1112 /* For single writes, FLUSH_STABLE is more efficient */
1113 if (npages <= wpages && npages == NFS_I(inode)->npages
1114 && nfs_list_entry(head->next)->wb_bytes <= wsize)
1115 how |= FLUSH_STABLE;
1118 nfs_coalesce_requests(head, &one_request, wpages);
1119 req = nfs_list_entry(one_request.next);
1120 error = flush_one(inode, &one_request, how);
1123 } while (!list_empty(head));
1126 while (!list_empty(head)) {
1127 req = nfs_list_entry(head->next);
1128 nfs_list_remove_request(req);
1129 nfs_mark_request_dirty(req);
1130 nfs_clear_page_writeback(req);
1136 * Handle a write reply that flushed part of a page.
1138 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
1140 struct nfs_write_data *data = calldata;
1141 struct nfs_page *req = data->req;
1142 struct page *page = req->wb_page;
1144 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1145 req->wb_context->dentry->d_inode->i_sb->s_id,
1146 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1148 (long long)req_offset(req));
1150 if (nfs_writeback_done(task, data) != 0)
1153 if (task->tk_status < 0) {
1154 ClearPageUptodate(page);
1156 req->wb_context->error = task->tk_status;
1157 dprintk(", error = %d\n", task->tk_status);
1159 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1160 if (data->verf.committed < NFS_FILE_SYNC) {
1161 if (!NFS_NEED_COMMIT(req)) {
1162 nfs_defer_commit(req);
1163 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1164 dprintk(" defer commit\n");
1165 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1166 nfs_defer_reschedule(req);
1167 dprintk(" server reboot detected\n");
1174 if (atomic_dec_and_test(&req->wb_complete))
1175 nfs_writepage_release(req);
1178 static const struct rpc_call_ops nfs_write_partial_ops = {
1179 .rpc_call_done = nfs_writeback_done_partial,
1180 .rpc_release = nfs_writedata_release,
1184 * Handle a write reply that flushes a whole page.
1186 * FIXME: There is an inherent race with invalidate_inode_pages and
1187 * writebacks since the page->count is kept > 1 for as long
1188 * as the page has a write request pending.
1190 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1192 struct nfs_write_data *data = calldata;
1193 struct nfs_page *req;
1196 if (nfs_writeback_done(task, data) != 0)
1199 /* Update attributes as result of writeback. */
1200 while (!list_empty(&data->pages)) {
1201 req = nfs_list_entry(data->pages.next);
1202 nfs_list_remove_request(req);
1203 page = req->wb_page;
1205 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1206 req->wb_context->dentry->d_inode->i_sb->s_id,
1207 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1209 (long long)req_offset(req));
1211 if (task->tk_status < 0) {
1212 ClearPageUptodate(page);
1214 req->wb_context->error = task->tk_status;
1215 end_page_writeback(page);
1216 nfs_inode_remove_request(req);
1217 dprintk(", error = %d\n", task->tk_status);
1220 end_page_writeback(page);
1222 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1223 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1224 nfs_inode_remove_request(req);
1228 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1229 nfs_mark_request_commit(req);
1230 dprintk(" marked for commit\n");
1232 nfs_inode_remove_request(req);
1235 nfs_clear_page_writeback(req);
1239 static const struct rpc_call_ops nfs_write_full_ops = {
1240 .rpc_call_done = nfs_writeback_done_full,
1241 .rpc_release = nfs_writedata_release,
1246 * This function is called when the WRITE call is complete.
1248 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1250 struct nfs_writeargs *argp = &data->args;
1251 struct nfs_writeres *resp = &data->res;
1254 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1255 task->tk_pid, task->tk_status);
1258 * ->write_done will attempt to use post-op attributes to detect
1259 * conflicting writes by other clients. A strict interpretation
1260 * of close-to-open would allow us to continue caching even if
1261 * another writer had changed the file, but some applications
1262 * depend on tighter cache coherency when writing.
1264 status = NFS_PROTO(data->inode)->write_done(task, data);
1267 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1269 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1270 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1271 /* We tried a write call, but the server did not
1272 * commit data to stable storage even though we
1274 * Note: There is a known bug in Tru64 < 5.0 in which
1275 * the server reports NFS_DATA_SYNC, but performs
1276 * NFS_FILE_SYNC. We therefore implement this checking
1277 * as a dprintk() in order to avoid filling syslog.
1279 static unsigned long complain;
1281 if (time_before(complain, jiffies)) {
1282 dprintk("NFS: faulty NFS server %s:"
1283 " (committed = %d) != (stable = %d)\n",
1284 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1285 resp->verf->committed, argp->stable);
1286 complain = jiffies + 300 * HZ;
1290 /* Is this a short write? */
1291 if (task->tk_status >= 0 && resp->count < argp->count) {
1292 static unsigned long complain;
1294 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1296 /* Has the server at least made some progress? */
1297 if (resp->count != 0) {
1298 /* Was this an NFSv2 write or an NFSv3 stable write? */
1299 if (resp->verf->committed != NFS_UNSTABLE) {
1300 /* Resend from where the server left off */
1301 argp->offset += resp->count;
1302 argp->pgbase += resp->count;
1303 argp->count -= resp->count;
1305 /* Resend as a stable write in order to avoid
1306 * headaches in the case of a server crash.
1308 argp->stable = NFS_FILE_SYNC;
1310 rpc_restart_call(task);
1313 if (time_before(complain, jiffies)) {
1315 "NFS: Server wrote zero bytes, expected %u.\n",
1317 complain = jiffies + 300 * HZ;
1319 /* Can't do anything about it except throw an error. */
1320 task->tk_status = -EIO;
1326 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1327 void nfs_commit_release(void *wdata)
1329 nfs_commit_free(wdata);
1333 * Set up the argument/result storage required for the RPC call.
1335 static void nfs_commit_rpcsetup(struct list_head *head,
1336 struct nfs_write_data *data,
1339 struct nfs_page *first;
1340 struct inode *inode;
1343 /* Set up the RPC argument and reply structs
1344 * NB: take care not to mess about with data->commit et al. */
1346 list_splice_init(head, &data->pages);
1347 first = nfs_list_entry(data->pages.next);
1348 inode = first->wb_context->dentry->d_inode;
1350 data->inode = inode;
1351 data->cred = first->wb_context->cred;
1353 data->args.fh = NFS_FH(data->inode);
1354 /* Note: we always request a commit of the entire inode */
1355 data->args.offset = 0;
1356 data->args.count = 0;
1357 data->res.count = 0;
1358 data->res.fattr = &data->fattr;
1359 data->res.verf = &data->verf;
1360 nfs_fattr_init(&data->fattr);
1362 /* Set up the initial task struct. */
1363 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1364 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
1365 NFS_PROTO(inode)->commit_setup(data, how);
1367 data->task.tk_priority = flush_task_priority(how);
1368 data->task.tk_cookie = (unsigned long)inode;
1370 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
1374 * Commit dirty pages
1377 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1379 struct nfs_write_data *data;
1380 struct nfs_page *req;
1382 data = nfs_commit_alloc();
1387 /* Set up the argument struct */
1388 nfs_commit_rpcsetup(head, data, how);
1390 nfs_execute_write(data);
1393 while (!list_empty(head)) {
1394 req = nfs_list_entry(head->next);
1395 nfs_list_remove_request(req);
1396 nfs_mark_request_commit(req);
1397 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1398 nfs_clear_page_writeback(req);
1404 * COMMIT call returned
1406 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1408 struct nfs_write_data *data = calldata;
1409 struct nfs_page *req;
1411 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1412 task->tk_pid, task->tk_status);
1414 /* Call the NFS version-specific code */
1415 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1418 while (!list_empty(&data->pages)) {
1419 req = nfs_list_entry(data->pages.next);
1420 nfs_list_remove_request(req);
1421 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1423 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1424 req->wb_context->dentry->d_inode->i_sb->s_id,
1425 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1427 (long long)req_offset(req));
1428 if (task->tk_status < 0) {
1429 req->wb_context->error = task->tk_status;
1430 nfs_inode_remove_request(req);
1431 dprintk(", error = %d\n", task->tk_status);
1435 /* Okay, COMMIT succeeded, apparently. Check the verifier
1436 * returned by the server against all stored verfs. */
1437 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1438 /* We have a match */
1439 nfs_inode_remove_request(req);
1443 /* We have a mismatch. Write the page again */
1444 dprintk(" mismatch\n");
1445 nfs_mark_request_dirty(req);
1447 nfs_clear_page_writeback(req);
1451 static const struct rpc_call_ops nfs_commit_ops = {
1452 .rpc_call_done = nfs_commit_done,
1453 .rpc_release = nfs_commit_release,
1456 static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1462 static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
1463 unsigned int npages, int how)
1465 struct nfs_inode *nfsi = NFS_I(inode);
1469 spin_lock(&nfsi->req_lock);
1470 res = nfs_scan_dirty(inode, &head, idx_start, npages);
1471 spin_unlock(&nfsi->req_lock);
1473 int error = nfs_flush_list(inode, &head, res, how);
1480 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1481 int nfs_commit_inode(struct inode *inode, int how)
1483 struct nfs_inode *nfsi = NFS_I(inode);
1487 spin_lock(&nfsi->req_lock);
1488 res = nfs_scan_commit(inode, &head, 0, 0);
1489 spin_unlock(&nfsi->req_lock);
1491 int error = nfs_commit_list(inode, &head, how);
1499 int nfs_sync_inode_wait(struct inode *inode, unsigned long idx_start,
1500 unsigned int npages, int how)
1502 struct nfs_inode *nfsi = NFS_I(inode);
1504 int nocommit = how & FLUSH_NOCOMMIT;
1507 how &= ~FLUSH_NOCOMMIT;
1508 spin_lock(&nfsi->req_lock);
1510 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1513 pages = nfs_scan_dirty(inode, &head, idx_start, npages);
1515 spin_unlock(&nfsi->req_lock);
1516 if (how & FLUSH_INVALIDATE)
1517 nfs_cancel_dirty_list(&head);
1519 ret = nfs_flush_list(inode, &head, pages, how);
1520 spin_lock(&nfsi->req_lock);
1525 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1528 if (how & FLUSH_INVALIDATE) {
1529 spin_unlock(&nfsi->req_lock);
1530 nfs_cancel_commit_list(&head);
1531 spin_lock(&nfsi->req_lock);
1534 pages += nfs_scan_commit(inode, &head, 0, 0);
1535 spin_unlock(&nfsi->req_lock);
1536 ret = nfs_commit_list(inode, &head, how);
1537 spin_lock(&nfsi->req_lock);
1539 spin_unlock(&nfsi->req_lock);
1543 int __init nfs_init_writepagecache(void)
1545 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1546 sizeof(struct nfs_write_data),
1547 0, SLAB_HWCACHE_ALIGN,
1549 if (nfs_wdata_cachep == NULL)
1552 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1554 if (nfs_wdata_mempool == NULL)
1557 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1559 if (nfs_commit_mempool == NULL)
1565 void nfs_destroy_writepagecache(void)
1567 mempool_destroy(nfs_commit_mempool);
1568 mempool_destroy(nfs_wdata_mempool);
1569 kmem_cache_destroy(nfs_wdata_cachep);