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 long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how);
83 static int nfs_wb_page_priority(struct inode *inode, struct page *page, 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_mapping(page->mapping, wbc, 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_mapping(mapping, wbc, wb_priority(wbc));
397 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
398 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
399 err = nfs_wait_on_requests(inode, 0, 0);
403 err = nfs_commit_inode(inode, wb_priority(wbc));
407 clear_bit(BDI_write_congested, &bdi->state);
408 wake_up_all(&nfs_write_congestion);
409 congestion_end(WRITE);
414 * Insert a write request into an inode
416 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
418 struct nfs_inode *nfsi = NFS_I(inode);
421 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
422 BUG_ON(error == -EEXIST);
427 nfs_begin_data_update(inode);
428 if (nfs_have_delegation(inode, FMODE_WRITE))
431 SetPagePrivate(req->wb_page);
433 atomic_inc(&req->wb_count);
438 * Insert a write request into an inode
440 static void nfs_inode_remove_request(struct nfs_page *req)
442 struct inode *inode = req->wb_context->dentry->d_inode;
443 struct nfs_inode *nfsi = NFS_I(inode);
445 BUG_ON (!NFS_WBACK_BUSY(req));
447 spin_lock(&nfsi->req_lock);
448 ClearPagePrivate(req->wb_page);
449 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
452 spin_unlock(&nfsi->req_lock);
453 nfs_end_data_update(inode);
456 spin_unlock(&nfsi->req_lock);
457 nfs_clear_request(req);
458 nfs_release_request(req);
464 static inline struct nfs_page *
465 _nfs_find_request(struct inode *inode, unsigned long index)
467 struct nfs_inode *nfsi = NFS_I(inode);
468 struct nfs_page *req;
470 req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
472 atomic_inc(&req->wb_count);
476 static struct nfs_page *
477 nfs_find_request(struct inode *inode, unsigned long index)
479 struct nfs_page *req;
480 struct nfs_inode *nfsi = NFS_I(inode);
482 spin_lock(&nfsi->req_lock);
483 req = _nfs_find_request(inode, index);
484 spin_unlock(&nfsi->req_lock);
489 * Add a request to the inode's dirty list.
492 nfs_mark_request_dirty(struct nfs_page *req)
494 struct inode *inode = req->wb_context->dentry->d_inode;
495 struct nfs_inode *nfsi = NFS_I(inode);
497 spin_lock(&nfsi->req_lock);
498 radix_tree_tag_set(&nfsi->nfs_page_tree,
499 req->wb_index, NFS_PAGE_TAG_DIRTY);
500 nfs_list_add_request(req, &nfsi->dirty);
502 spin_unlock(&nfsi->req_lock);
503 inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
504 mark_inode_dirty(inode);
508 * Check if a request is dirty
511 nfs_dirty_request(struct nfs_page *req)
513 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
514 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
517 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
519 * Add a request to the inode's commit list.
522 nfs_mark_request_commit(struct nfs_page *req)
524 struct inode *inode = req->wb_context->dentry->d_inode;
525 struct nfs_inode *nfsi = NFS_I(inode);
527 spin_lock(&nfsi->req_lock);
528 nfs_list_add_request(req, &nfsi->commit);
530 spin_unlock(&nfsi->req_lock);
531 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
532 mark_inode_dirty(inode);
537 * Wait for a request to complete.
539 * Interruptible by signals only if mounted with intr flag.
541 static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
543 struct nfs_inode *nfsi = NFS_I(inode);
544 struct nfs_page *req;
545 unsigned long idx_end, next;
546 unsigned int res = 0;
552 idx_end = idx_start + npages - 1;
555 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
556 if (req->wb_index > idx_end)
559 next = req->wb_index + 1;
560 BUG_ON(!NFS_WBACK_BUSY(req));
562 atomic_inc(&req->wb_count);
563 spin_unlock(&nfsi->req_lock);
564 error = nfs_wait_on_request(req);
565 nfs_release_request(req);
566 spin_lock(&nfsi->req_lock);
574 static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
576 struct nfs_inode *nfsi = NFS_I(inode);
579 spin_lock(&nfsi->req_lock);
580 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
581 spin_unlock(&nfsi->req_lock);
585 static void nfs_cancel_dirty_list(struct list_head *head)
587 struct nfs_page *req;
588 while(!list_empty(head)) {
589 req = nfs_list_entry(head->next);
590 nfs_list_remove_request(req);
591 nfs_inode_remove_request(req);
592 nfs_clear_page_writeback(req);
596 static void nfs_cancel_commit_list(struct list_head *head)
598 struct nfs_page *req;
600 while(!list_empty(head)) {
601 req = nfs_list_entry(head->next);
602 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
603 nfs_list_remove_request(req);
604 nfs_inode_remove_request(req);
605 nfs_unlock_request(req);
609 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
611 * nfs_scan_commit - Scan an inode for commit 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 'commit' request list.
618 * The requests are *not* checked to ensure that they form a contiguous set.
621 nfs_scan_commit(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->ncommit != 0) {
627 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
628 nfsi->ncommit -= res;
629 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
630 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
635 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
641 static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
643 struct backing_dev_info *bdi = mapping->backing_dev_info;
649 if (!bdi_write_congested(bdi))
652 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
655 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
658 rpc_clnt_sigmask(clnt, &oldset);
659 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
660 if (bdi_write_congested(bdi)) {
666 rpc_clnt_sigunmask(clnt, &oldset);
668 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
669 if (bdi_write_congested(bdi))
672 finish_wait(&nfs_write_congestion, &wait);
678 * Try to update any existing write request, or create one if there is none.
679 * In order to match, the request's credentials must match those of
680 * the calling process.
682 * Note: Should always be called with the Page Lock held!
684 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
685 struct inode *inode, struct page *page,
686 unsigned int offset, unsigned int bytes)
688 struct nfs_server *server = NFS_SERVER(inode);
689 struct nfs_inode *nfsi = NFS_I(inode);
690 struct nfs_page *req, *new = NULL;
691 unsigned long rqend, end;
693 end = offset + bytes;
695 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
696 return ERR_PTR(-ERESTARTSYS);
698 /* Loop over all inode entries and see if we find
699 * A request for the page we wish to update
701 spin_lock(&nfsi->req_lock);
702 req = _nfs_find_request(inode, page->index);
704 if (!nfs_lock_request_dontget(req)) {
706 spin_unlock(&nfsi->req_lock);
707 error = nfs_wait_on_request(req);
708 nfs_release_request(req);
711 nfs_release_request(new);
712 return ERR_PTR(error);
716 spin_unlock(&nfsi->req_lock);
718 nfs_release_request(new);
724 nfs_lock_request_dontget(new);
725 error = nfs_inode_add_request(inode, new);
727 spin_unlock(&nfsi->req_lock);
728 nfs_unlock_request(new);
729 return ERR_PTR(error);
731 spin_unlock(&nfsi->req_lock);
732 nfs_mark_request_dirty(new);
735 spin_unlock(&nfsi->req_lock);
737 new = nfs_create_request(ctx, inode, page, offset, bytes);
742 /* We have a request for our page.
743 * If the creds don't match, or the
744 * page addresses don't match,
745 * tell the caller to wait on the conflicting
748 rqend = req->wb_offset + req->wb_bytes;
749 if (req->wb_context != ctx
750 || req->wb_page != page
751 || !nfs_dirty_request(req)
752 || offset > rqend || end < req->wb_offset) {
753 nfs_unlock_request(req);
754 return ERR_PTR(-EBUSY);
757 /* Okay, the request matches. Update the region */
758 if (offset < req->wb_offset) {
759 req->wb_offset = offset;
760 req->wb_pgbase = offset;
761 req->wb_bytes = rqend - req->wb_offset;
765 req->wb_bytes = end - req->wb_offset;
770 int nfs_flush_incompatible(struct file *file, struct page *page)
772 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
773 struct inode *inode = page->mapping->host;
774 struct nfs_page *req;
777 * Look for a request corresponding to this page. If there
778 * is one, and it belongs to another file, we flush it out
779 * before we try to copy anything into the page. Do this
780 * due to the lack of an ACCESS-type call in NFSv2.
781 * Also do the same if we find a request from an existing
784 req = nfs_find_request(inode, page->index);
786 if (req->wb_page != page || ctx != req->wb_context)
787 status = nfs_wb_page(inode, page);
788 nfs_release_request(req);
790 return (status < 0) ? status : 0;
794 * Update and possibly write a cached page of an NFS file.
796 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
797 * things with a page scheduled for an RPC call (e.g. invalidate it).
799 int nfs_updatepage(struct file *file, struct page *page,
800 unsigned int offset, unsigned int count)
802 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
803 struct inode *inode = page->mapping->host;
804 struct nfs_page *req;
807 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
809 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
810 file->f_dentry->d_parent->d_name.name,
811 file->f_dentry->d_name.name, count,
812 (long long)(page_offset(page) +offset));
814 if (IS_SYNC(inode)) {
815 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
817 if (offset == 0 && status == PAGE_CACHE_SIZE)
818 SetPageUptodate(page);
824 /* If we're not using byte range locks, and we know the page
825 * is entirely in cache, it may be more efficient to avoid
826 * fragmenting write requests.
828 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
829 loff_t end_offs = i_size_read(inode) - 1;
830 unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
834 if (unlikely(end_offs < 0)) {
836 } else if (page->index == end_index) {
838 pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
841 } else if (page->index < end_index)
842 count = PAGE_CACHE_SIZE;
846 * Try to find an NFS request corresponding to this page
848 * If the existing request cannot be updated, we must flush
852 req = nfs_update_request(ctx, inode, page, offset, count);
853 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
854 if (status != -EBUSY)
856 /* Request could not be updated. Flush it out and try again */
857 status = nfs_wb_page(inode, page);
858 } while (status >= 0);
864 /* Update file length */
865 nfs_grow_file(page, offset, count);
866 /* Set the PG_uptodate flag? */
867 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
868 nfs_unlock_request(req);
870 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
871 status, (long long)i_size_read(inode));
873 ClearPageUptodate(page);
877 static void nfs_writepage_release(struct nfs_page *req)
879 end_page_writeback(req->wb_page);
881 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
882 if (!PageError(req->wb_page)) {
883 if (NFS_NEED_RESCHED(req)) {
884 nfs_mark_request_dirty(req);
886 } else if (NFS_NEED_COMMIT(req)) {
887 nfs_mark_request_commit(req);
891 nfs_inode_remove_request(req);
894 nfs_clear_commit(req);
895 nfs_clear_reschedule(req);
897 nfs_inode_remove_request(req);
899 nfs_clear_page_writeback(req);
902 static inline int flush_task_priority(int how)
904 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
906 return RPC_PRIORITY_HIGH;
908 return RPC_PRIORITY_LOW;
910 return RPC_PRIORITY_NORMAL;
914 * Set up the argument/result storage required for the RPC call.
916 static void nfs_write_rpcsetup(struct nfs_page *req,
917 struct nfs_write_data *data,
918 const struct rpc_call_ops *call_ops,
919 unsigned int count, unsigned int offset,
925 /* Set up the RPC argument and reply structs
926 * NB: take care not to mess about with data->commit et al. */
929 data->inode = inode = req->wb_context->dentry->d_inode;
930 data->cred = req->wb_context->cred;
932 data->args.fh = NFS_FH(inode);
933 data->args.offset = req_offset(req) + offset;
934 data->args.pgbase = req->wb_pgbase + offset;
935 data->args.pages = data->pagevec;
936 data->args.count = count;
937 data->args.context = req->wb_context;
939 data->res.fattr = &data->fattr;
940 data->res.count = count;
941 data->res.verf = &data->verf;
942 nfs_fattr_init(&data->fattr);
944 /* Set up the initial task struct. */
945 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
946 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
947 NFS_PROTO(inode)->write_setup(data, how);
949 data->task.tk_priority = flush_task_priority(how);
950 data->task.tk_cookie = (unsigned long)inode;
952 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
955 (long long)NFS_FILEID(inode),
957 (unsigned long long)data->args.offset);
960 static void nfs_execute_write(struct nfs_write_data *data)
962 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
965 rpc_clnt_sigmask(clnt, &oldset);
966 rpc_execute(&data->task);
967 rpc_clnt_sigunmask(clnt, &oldset);
971 * Generate multiple small requests to write out a single
972 * contiguous dirty area on one page.
974 static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
976 struct nfs_page *req = nfs_list_entry(head->next);
977 struct page *page = req->wb_page;
978 struct nfs_write_data *data;
979 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
984 nfs_list_remove_request(req);
986 nbytes = req->wb_bytes;
988 size_t len = min(nbytes, wsize);
990 data = nfs_writedata_alloc(len);
993 list_add(&data->pages, &list);
996 } while (nbytes != 0);
997 atomic_set(&req->wb_complete, requests);
999 ClearPageError(page);
1000 set_page_writeback(page);
1002 nbytes = req->wb_bytes;
1004 data = list_entry(list.next, struct nfs_write_data, pages);
1005 list_del_init(&data->pages);
1007 data->pagevec[0] = page;
1009 if (nbytes > wsize) {
1010 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1011 wsize, offset, how);
1015 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
1016 nbytes, offset, how);
1019 nfs_execute_write(data);
1020 } while (nbytes != 0);
1025 while (!list_empty(&list)) {
1026 data = list_entry(list.next, struct nfs_write_data, pages);
1027 list_del(&data->pages);
1028 nfs_writedata_release(data);
1030 nfs_mark_request_dirty(req);
1031 nfs_clear_page_writeback(req);
1036 * Create an RPC task for the given write request and kick it.
1037 * The page must have been locked by the caller.
1039 * It may happen that the page we're passed is not marked dirty.
1040 * This is the case if nfs_updatepage detects a conflicting request
1041 * that has been written but not committed.
1043 static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
1045 struct nfs_page *req;
1046 struct page **pages;
1047 struct nfs_write_data *data;
1050 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
1054 pages = data->pagevec;
1056 while (!list_empty(head)) {
1057 req = nfs_list_entry(head->next);
1058 nfs_list_remove_request(req);
1059 nfs_list_add_request(req, &data->pages);
1060 ClearPageError(req->wb_page);
1061 set_page_writeback(req->wb_page);
1062 *pages++ = req->wb_page;
1063 count += req->wb_bytes;
1065 req = nfs_list_entry(data->pages.next);
1067 /* Set up the argument struct */
1068 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
1070 nfs_execute_write(data);
1073 while (!list_empty(head)) {
1074 struct nfs_page *req = nfs_list_entry(head->next);
1075 nfs_list_remove_request(req);
1076 nfs_mark_request_dirty(req);
1077 nfs_clear_page_writeback(req);
1082 static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
1084 LIST_HEAD(one_request);
1085 int (*flush_one)(struct inode *, struct list_head *, int);
1086 struct nfs_page *req;
1087 int wpages = NFS_SERVER(inode)->wpages;
1088 int wsize = NFS_SERVER(inode)->wsize;
1091 flush_one = nfs_flush_one;
1092 if (wsize < PAGE_CACHE_SIZE)
1093 flush_one = nfs_flush_multi;
1094 /* For single writes, FLUSH_STABLE is more efficient */
1095 if (npages <= wpages && npages == NFS_I(inode)->npages
1096 && nfs_list_entry(head->next)->wb_bytes <= wsize)
1097 how |= FLUSH_STABLE;
1100 nfs_coalesce_requests(head, &one_request, wpages);
1101 req = nfs_list_entry(one_request.next);
1102 error = flush_one(inode, &one_request, how);
1105 } while (!list_empty(head));
1108 while (!list_empty(head)) {
1109 req = nfs_list_entry(head->next);
1110 nfs_list_remove_request(req);
1111 nfs_mark_request_dirty(req);
1112 nfs_clear_page_writeback(req);
1118 * Handle a write reply that flushed part of a page.
1120 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
1122 struct nfs_write_data *data = calldata;
1123 struct nfs_page *req = data->req;
1124 struct page *page = req->wb_page;
1126 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1127 req->wb_context->dentry->d_inode->i_sb->s_id,
1128 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1130 (long long)req_offset(req));
1132 if (nfs_writeback_done(task, data) != 0)
1135 if (task->tk_status < 0) {
1136 ClearPageUptodate(page);
1138 req->wb_context->error = task->tk_status;
1139 dprintk(", error = %d\n", task->tk_status);
1141 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1142 if (data->verf.committed < NFS_FILE_SYNC) {
1143 if (!NFS_NEED_COMMIT(req)) {
1144 nfs_defer_commit(req);
1145 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1146 dprintk(" defer commit\n");
1147 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1148 nfs_defer_reschedule(req);
1149 dprintk(" server reboot detected\n");
1156 if (atomic_dec_and_test(&req->wb_complete))
1157 nfs_writepage_release(req);
1160 static const struct rpc_call_ops nfs_write_partial_ops = {
1161 .rpc_call_done = nfs_writeback_done_partial,
1162 .rpc_release = nfs_writedata_release,
1166 * Handle a write reply that flushes a whole page.
1168 * FIXME: There is an inherent race with invalidate_inode_pages and
1169 * writebacks since the page->count is kept > 1 for as long
1170 * as the page has a write request pending.
1172 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1174 struct nfs_write_data *data = calldata;
1175 struct nfs_page *req;
1178 if (nfs_writeback_done(task, data) != 0)
1181 /* Update attributes as result of writeback. */
1182 while (!list_empty(&data->pages)) {
1183 req = nfs_list_entry(data->pages.next);
1184 nfs_list_remove_request(req);
1185 page = req->wb_page;
1187 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1188 req->wb_context->dentry->d_inode->i_sb->s_id,
1189 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1191 (long long)req_offset(req));
1193 if (task->tk_status < 0) {
1194 ClearPageUptodate(page);
1196 req->wb_context->error = task->tk_status;
1197 end_page_writeback(page);
1198 nfs_inode_remove_request(req);
1199 dprintk(", error = %d\n", task->tk_status);
1202 end_page_writeback(page);
1204 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1205 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1206 nfs_inode_remove_request(req);
1210 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1211 nfs_mark_request_commit(req);
1212 dprintk(" marked for commit\n");
1214 nfs_inode_remove_request(req);
1217 nfs_clear_page_writeback(req);
1221 static const struct rpc_call_ops nfs_write_full_ops = {
1222 .rpc_call_done = nfs_writeback_done_full,
1223 .rpc_release = nfs_writedata_release,
1228 * This function is called when the WRITE call is complete.
1230 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1232 struct nfs_writeargs *argp = &data->args;
1233 struct nfs_writeres *resp = &data->res;
1236 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1237 task->tk_pid, task->tk_status);
1240 * ->write_done will attempt to use post-op attributes to detect
1241 * conflicting writes by other clients. A strict interpretation
1242 * of close-to-open would allow us to continue caching even if
1243 * another writer had changed the file, but some applications
1244 * depend on tighter cache coherency when writing.
1246 status = NFS_PROTO(data->inode)->write_done(task, data);
1249 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1251 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1252 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1253 /* We tried a write call, but the server did not
1254 * commit data to stable storage even though we
1256 * Note: There is a known bug in Tru64 < 5.0 in which
1257 * the server reports NFS_DATA_SYNC, but performs
1258 * NFS_FILE_SYNC. We therefore implement this checking
1259 * as a dprintk() in order to avoid filling syslog.
1261 static unsigned long complain;
1263 if (time_before(complain, jiffies)) {
1264 dprintk("NFS: faulty NFS server %s:"
1265 " (committed = %d) != (stable = %d)\n",
1266 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1267 resp->verf->committed, argp->stable);
1268 complain = jiffies + 300 * HZ;
1272 /* Is this a short write? */
1273 if (task->tk_status >= 0 && resp->count < argp->count) {
1274 static unsigned long complain;
1276 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1278 /* Has the server at least made some progress? */
1279 if (resp->count != 0) {
1280 /* Was this an NFSv2 write or an NFSv3 stable write? */
1281 if (resp->verf->committed != NFS_UNSTABLE) {
1282 /* Resend from where the server left off */
1283 argp->offset += resp->count;
1284 argp->pgbase += resp->count;
1285 argp->count -= resp->count;
1287 /* Resend as a stable write in order to avoid
1288 * headaches in the case of a server crash.
1290 argp->stable = NFS_FILE_SYNC;
1292 rpc_restart_call(task);
1295 if (time_before(complain, jiffies)) {
1297 "NFS: Server wrote zero bytes, expected %u.\n",
1299 complain = jiffies + 300 * HZ;
1301 /* Can't do anything about it except throw an error. */
1302 task->tk_status = -EIO;
1308 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1309 void nfs_commit_release(void *wdata)
1311 nfs_commit_free(wdata);
1315 * Set up the argument/result storage required for the RPC call.
1317 static void nfs_commit_rpcsetup(struct list_head *head,
1318 struct nfs_write_data *data,
1321 struct nfs_page *first;
1322 struct inode *inode;
1325 /* Set up the RPC argument and reply structs
1326 * NB: take care not to mess about with data->commit et al. */
1328 list_splice_init(head, &data->pages);
1329 first = nfs_list_entry(data->pages.next);
1330 inode = first->wb_context->dentry->d_inode;
1332 data->inode = inode;
1333 data->cred = first->wb_context->cred;
1335 data->args.fh = NFS_FH(data->inode);
1336 /* Note: we always request a commit of the entire inode */
1337 data->args.offset = 0;
1338 data->args.count = 0;
1339 data->res.count = 0;
1340 data->res.fattr = &data->fattr;
1341 data->res.verf = &data->verf;
1342 nfs_fattr_init(&data->fattr);
1344 /* Set up the initial task struct. */
1345 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1346 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
1347 NFS_PROTO(inode)->commit_setup(data, how);
1349 data->task.tk_priority = flush_task_priority(how);
1350 data->task.tk_cookie = (unsigned long)inode;
1352 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
1356 * Commit dirty pages
1359 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1361 struct nfs_write_data *data;
1362 struct nfs_page *req;
1364 data = nfs_commit_alloc();
1369 /* Set up the argument struct */
1370 nfs_commit_rpcsetup(head, data, how);
1372 nfs_execute_write(data);
1375 while (!list_empty(head)) {
1376 req = nfs_list_entry(head->next);
1377 nfs_list_remove_request(req);
1378 nfs_mark_request_commit(req);
1379 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1380 nfs_clear_page_writeback(req);
1386 * COMMIT call returned
1388 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1390 struct nfs_write_data *data = calldata;
1391 struct nfs_page *req;
1393 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1394 task->tk_pid, task->tk_status);
1396 /* Call the NFS version-specific code */
1397 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1400 while (!list_empty(&data->pages)) {
1401 req = nfs_list_entry(data->pages.next);
1402 nfs_list_remove_request(req);
1403 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1405 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1406 req->wb_context->dentry->d_inode->i_sb->s_id,
1407 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1409 (long long)req_offset(req));
1410 if (task->tk_status < 0) {
1411 req->wb_context->error = task->tk_status;
1412 nfs_inode_remove_request(req);
1413 dprintk(", error = %d\n", task->tk_status);
1417 /* Okay, COMMIT succeeded, apparently. Check the verifier
1418 * returned by the server against all stored verfs. */
1419 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1420 /* We have a match */
1421 nfs_inode_remove_request(req);
1425 /* We have a mismatch. Write the page again */
1426 dprintk(" mismatch\n");
1427 nfs_mark_request_dirty(req);
1429 nfs_clear_page_writeback(req);
1433 static const struct rpc_call_ops nfs_commit_ops = {
1434 .rpc_call_done = nfs_commit_done,
1435 .rpc_release = nfs_commit_release,
1438 static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1444 static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
1446 struct nfs_inode *nfsi = NFS_I(mapping->host);
1450 spin_lock(&nfsi->req_lock);
1451 res = nfs_scan_dirty(mapping, wbc, &head);
1452 spin_unlock(&nfsi->req_lock);
1454 int error = nfs_flush_list(mapping->host, &head, res, how);
1461 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1462 int nfs_commit_inode(struct inode *inode, int how)
1464 struct nfs_inode *nfsi = NFS_I(inode);
1468 spin_lock(&nfsi->req_lock);
1469 res = nfs_scan_commit(inode, &head, 0, 0);
1470 spin_unlock(&nfsi->req_lock);
1472 int error = nfs_commit_list(inode, &head, how);
1480 long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
1482 struct inode *inode = mapping->host;
1483 struct nfs_inode *nfsi = NFS_I(inode);
1484 unsigned long idx_start, idx_end;
1485 unsigned int npages = 0;
1487 int nocommit = how & FLUSH_NOCOMMIT;
1491 if (wbc->range_cyclic)
1494 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1495 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1496 if (idx_end > idx_start) {
1497 unsigned long l_npages = 1 + idx_end - idx_start;
1499 if (sizeof(npages) != sizeof(l_npages) &&
1500 (unsigned long)npages != l_npages)
1504 how &= ~FLUSH_NOCOMMIT;
1505 spin_lock(&nfsi->req_lock);
1507 wbc->pages_skipped = 0;
1508 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1511 pages = nfs_scan_dirty(mapping, wbc, &head);
1513 spin_unlock(&nfsi->req_lock);
1514 if (how & FLUSH_INVALIDATE) {
1515 nfs_cancel_dirty_list(&head);
1518 ret = nfs_flush_list(inode, &head, pages, how);
1519 spin_lock(&nfsi->req_lock);
1522 if (wbc->pages_skipped != 0)
1526 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1528 if (wbc->pages_skipped != 0)
1532 if (how & FLUSH_INVALIDATE) {
1533 spin_unlock(&nfsi->req_lock);
1534 nfs_cancel_commit_list(&head);
1536 spin_lock(&nfsi->req_lock);
1539 pages += nfs_scan_commit(inode, &head, 0, 0);
1540 spin_unlock(&nfsi->req_lock);
1541 ret = nfs_commit_list(inode, &head, how);
1542 spin_lock(&nfsi->req_lock);
1544 spin_unlock(&nfsi->req_lock);
1549 * flush the inode to disk.
1551 int nfs_wb_all(struct inode *inode)
1553 struct address_space *mapping = inode->i_mapping;
1554 struct writeback_control wbc = {
1555 .bdi = mapping->backing_dev_info,
1556 .sync_mode = WB_SYNC_ALL,
1557 .nr_to_write = LONG_MAX,
1562 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1568 int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1570 struct writeback_control wbc = {
1571 .bdi = mapping->backing_dev_info,
1572 .sync_mode = WB_SYNC_ALL,
1573 .nr_to_write = LONG_MAX,
1574 .range_start = range_start,
1575 .range_end = range_end,
1579 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1585 static int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
1587 loff_t range_start = page_offset(page);
1588 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1590 return nfs_sync_mapping_range(inode->i_mapping, range_start, range_end, how | FLUSH_STABLE);
1594 * Write back all requests on one page - we do this before reading it.
1596 int nfs_wb_page(struct inode *inode, struct page* page)
1598 return nfs_wb_page_priority(inode, page, 0);
1602 int __init nfs_init_writepagecache(void)
1604 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1605 sizeof(struct nfs_write_data),
1606 0, SLAB_HWCACHE_ALIGN,
1608 if (nfs_wdata_cachep == NULL)
1611 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1613 if (nfs_wdata_mempool == NULL)
1616 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1618 if (nfs_commit_mempool == NULL)
1624 void nfs_destroy_writepagecache(void)
1626 mempool_destroy(nfs_commit_mempool);
1627 mempool_destroy(nfs_wdata_mempool);
1628 kmem_cache_destroy(nfs_wdata_cachep);