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"
69 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
71 #define MIN_POOL_WRITE (32)
72 #define MIN_POOL_COMMIT (4)
75 * Local function declarations
77 static struct nfs_page * nfs_update_request(struct nfs_open_context*,
79 unsigned int, unsigned int);
80 static void nfs_mark_request_dirty(struct nfs_page *req);
81 static int nfs_wait_on_write_congestion(struct address_space *, int);
82 static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
83 static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how);
84 static int nfs_wb_page_priority(struct inode *inode, struct page *page, int how);
85 static const struct rpc_call_ops nfs_write_partial_ops;
86 static const struct rpc_call_ops nfs_write_full_ops;
87 static const struct rpc_call_ops nfs_commit_ops;
89 static kmem_cache_t *nfs_wdata_cachep;
90 static mempool_t *nfs_wdata_mempool;
91 static mempool_t *nfs_commit_mempool;
93 static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
95 struct nfs_write_data *nfs_commit_alloc(void)
97 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
100 memset(p, 0, sizeof(*p));
101 INIT_LIST_HEAD(&p->pages);
106 void nfs_commit_rcu_free(struct rcu_head *head)
108 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
109 if (p && (p->pagevec != &p->page_array[0]))
111 mempool_free(p, nfs_commit_mempool);
114 void nfs_commit_free(struct nfs_write_data *wdata)
116 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
119 struct nfs_write_data *nfs_writedata_alloc(size_t len)
121 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
122 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
125 memset(p, 0, sizeof(*p));
126 INIT_LIST_HEAD(&p->pages);
127 p->npages = pagecount;
128 if (pagecount <= ARRAY_SIZE(p->page_array))
129 p->pagevec = p->page_array;
131 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
133 mempool_free(p, nfs_wdata_mempool);
141 static void nfs_writedata_rcu_free(struct rcu_head *head)
143 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
144 if (p && (p->pagevec != &p->page_array[0]))
146 mempool_free(p, nfs_wdata_mempool);
149 static void nfs_writedata_free(struct nfs_write_data *wdata)
151 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
154 void nfs_writedata_release(void *wdata)
156 nfs_writedata_free(wdata);
159 static struct nfs_page *nfs_page_find_request_locked(struct page *page)
161 struct nfs_page *req = NULL;
163 if (PagePrivate(page)) {
164 req = (struct nfs_page *)page_private(page);
166 atomic_inc(&req->wb_count);
171 static struct nfs_page *nfs_page_find_request(struct page *page)
173 struct nfs_page *req = NULL;
174 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
177 req = nfs_page_find_request_locked(page);
178 spin_unlock(req_lock);
182 /* Adjust the file length if we're writing beyond the end */
183 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
185 struct inode *inode = page->mapping->host;
186 loff_t end, i_size = i_size_read(inode);
187 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
189 if (i_size > 0 && page->index < end_index)
191 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
194 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
195 i_size_write(inode, end);
198 /* We can set the PG_uptodate flag if we see that a write request
199 * covers the full page.
201 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
203 if (PageUptodate(page))
207 if (count != nfs_page_length(page))
209 if (count != PAGE_CACHE_SIZE)
210 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
211 SetPageUptodate(page);
214 static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
215 unsigned int offset, unsigned int count)
217 struct nfs_page *req;
221 req = nfs_update_request(ctx, page, offset, count);
227 ret = nfs_wb_page(page->mapping->host, page);
231 /* Update file length */
232 nfs_grow_file(page, offset, count);
233 /* Set the PG_uptodate flag? */
234 nfs_mark_uptodate(page, offset, count);
235 nfs_unlock_request(req);
239 static int wb_priority(struct writeback_control *wbc)
241 if (wbc->for_reclaim)
242 return FLUSH_HIGHPRI;
243 if (wbc->for_kupdate)
249 * Find an associated nfs write request, and prepare to flush it out
250 * Returns 1 if there was no write request, or if the request was
251 * already tagged by nfs_set_page_dirty.Returns 0 if the request
253 * May also return an error if the user signalled nfs_wait_on_request().
255 static int nfs_page_mark_flush(struct page *page)
257 struct nfs_page *req;
258 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
263 req = nfs_page_find_request_locked(page);
265 spin_unlock(req_lock);
268 if (nfs_lock_request_dontget(req))
270 /* Note: If we hold the page lock, as is the case in nfs_writepage,
271 * then the call to nfs_lock_request_dontget() will always
272 * succeed provided that someone hasn't already marked the
273 * request as dirty (in which case we don't care).
275 spin_unlock(req_lock);
276 ret = nfs_wait_on_request(req);
277 nfs_release_request(req);
282 spin_unlock(req_lock);
283 if (test_and_set_bit(PG_FLUSHING, &req->wb_flags) == 0)
284 nfs_mark_request_dirty(req);
285 ret = test_bit(PG_NEED_FLUSH, &req->wb_flags);
286 nfs_unlock_request(req);
291 * Write an mmapped page to the server.
293 static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
295 struct nfs_open_context *ctx;
296 struct inode *inode = page->mapping->host;
300 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
301 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
303 err = nfs_page_mark_flush(page);
307 offset = nfs_page_length(page);
311 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
316 err = nfs_writepage_setup(ctx, page, 0, offset);
317 put_nfs_open_context(ctx);
320 err = nfs_page_mark_flush(page);
324 if (!wbc->for_writepages)
325 nfs_flush_mapping(page->mapping, wbc, wb_priority(wbc));
329 int nfs_writepage(struct page *page, struct writeback_control *wbc)
333 err = nfs_writepage_locked(page, wbc);
339 * Note: causes nfs_update_request() to block on the assumption
340 * that the writeback is generated due to memory pressure.
342 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
344 struct backing_dev_info *bdi = mapping->backing_dev_info;
345 struct inode *inode = mapping->host;
348 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
350 err = generic_writepages(mapping, wbc);
353 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
354 if (wbc->nonblocking)
356 nfs_wait_on_write_congestion(mapping, 0);
358 err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
361 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
362 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
363 err = nfs_wait_on_requests(inode, 0, 0);
367 err = nfs_commit_inode(inode, wb_priority(wbc));
371 clear_bit(BDI_write_congested, &bdi->state);
372 wake_up_all(&nfs_write_congestion);
373 congestion_end(WRITE);
378 * Insert a write request into an inode
380 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
382 struct nfs_inode *nfsi = NFS_I(inode);
385 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
386 BUG_ON(error == -EEXIST);
391 nfs_begin_data_update(inode);
392 if (nfs_have_delegation(inode, FMODE_WRITE))
395 SetPagePrivate(req->wb_page);
396 set_page_private(req->wb_page, (unsigned long)req);
398 atomic_inc(&req->wb_count);
403 * Insert a write request into an inode
405 static void nfs_inode_remove_request(struct nfs_page *req)
407 struct inode *inode = req->wb_context->dentry->d_inode;
408 struct nfs_inode *nfsi = NFS_I(inode);
410 BUG_ON (!NFS_WBACK_BUSY(req));
412 spin_lock(&nfsi->req_lock);
413 set_page_private(req->wb_page, 0);
414 ClearPagePrivate(req->wb_page);
415 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
418 spin_unlock(&nfsi->req_lock);
419 nfs_end_data_update(inode);
422 spin_unlock(&nfsi->req_lock);
423 nfs_clear_request(req);
424 nfs_release_request(req);
428 * Add a request to the inode's dirty list.
431 nfs_mark_request_dirty(struct nfs_page *req)
433 struct inode *inode = req->wb_context->dentry->d_inode;
434 struct nfs_inode *nfsi = NFS_I(inode);
436 spin_lock(&nfsi->req_lock);
437 radix_tree_tag_set(&nfsi->nfs_page_tree,
438 req->wb_index, NFS_PAGE_TAG_DIRTY);
439 nfs_list_add_request(req, &nfsi->dirty);
441 spin_unlock(&nfsi->req_lock);
442 inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
443 mark_inode_dirty(inode);
447 * Check if a request is dirty
450 nfs_dirty_request(struct nfs_page *req)
452 return test_bit(PG_FLUSHING, &req->wb_flags) == 0;
455 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
457 * Add a request to the inode's commit list.
460 nfs_mark_request_commit(struct nfs_page *req)
462 struct inode *inode = req->wb_context->dentry->d_inode;
463 struct nfs_inode *nfsi = NFS_I(inode);
465 spin_lock(&nfsi->req_lock);
466 nfs_list_add_request(req, &nfsi->commit);
468 spin_unlock(&nfsi->req_lock);
469 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
470 mark_inode_dirty(inode);
475 * Wait for a request to complete.
477 * Interruptible by signals only if mounted with intr flag.
479 static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
481 struct nfs_inode *nfsi = NFS_I(inode);
482 struct nfs_page *req;
483 unsigned long idx_end, next;
484 unsigned int res = 0;
490 idx_end = idx_start + npages - 1;
493 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
494 if (req->wb_index > idx_end)
497 next = req->wb_index + 1;
498 BUG_ON(!NFS_WBACK_BUSY(req));
500 atomic_inc(&req->wb_count);
501 spin_unlock(&nfsi->req_lock);
502 error = nfs_wait_on_request(req);
503 nfs_release_request(req);
504 spin_lock(&nfsi->req_lock);
512 static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
514 struct nfs_inode *nfsi = NFS_I(inode);
517 spin_lock(&nfsi->req_lock);
518 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
519 spin_unlock(&nfsi->req_lock);
523 static void nfs_cancel_dirty_list(struct list_head *head)
525 struct nfs_page *req;
526 while(!list_empty(head)) {
527 req = nfs_list_entry(head->next);
528 nfs_list_remove_request(req);
529 nfs_inode_remove_request(req);
530 nfs_clear_page_writeback(req);
534 static void nfs_cancel_commit_list(struct list_head *head)
536 struct nfs_page *req;
538 while(!list_empty(head)) {
539 req = nfs_list_entry(head->next);
540 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
541 nfs_list_remove_request(req);
542 nfs_inode_remove_request(req);
543 nfs_unlock_request(req);
547 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
549 * nfs_scan_commit - Scan an inode for commit requests
550 * @inode: NFS inode to scan
551 * @dst: destination list
552 * @idx_start: lower bound of page->index to scan.
553 * @npages: idx_start + npages sets the upper bound to scan.
555 * Moves requests from the inode's 'commit' request list.
556 * The requests are *not* checked to ensure that they form a contiguous set.
559 nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
561 struct nfs_inode *nfsi = NFS_I(inode);
564 if (nfsi->ncommit != 0) {
565 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
566 nfsi->ncommit -= res;
567 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
568 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
573 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
579 static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
581 struct backing_dev_info *bdi = mapping->backing_dev_info;
587 if (!bdi_write_congested(bdi))
590 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
593 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
596 rpc_clnt_sigmask(clnt, &oldset);
597 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
598 if (bdi_write_congested(bdi)) {
604 rpc_clnt_sigunmask(clnt, &oldset);
606 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
607 if (bdi_write_congested(bdi))
610 finish_wait(&nfs_write_congestion, &wait);
616 * Try to update any existing write request, or create one if there is none.
617 * In order to match, the request's credentials must match those of
618 * the calling process.
620 * Note: Should always be called with the Page Lock held!
622 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
623 struct page *page, unsigned int offset, unsigned int bytes)
625 struct inode *inode = page->mapping->host;
626 struct nfs_inode *nfsi = NFS_I(inode);
627 struct nfs_page *req, *new = NULL;
628 unsigned long rqend, end;
630 end = offset + bytes;
632 if (nfs_wait_on_write_congestion(page->mapping, NFS_SERVER(inode)->flags & NFS_MOUNT_INTR))
633 return ERR_PTR(-ERESTARTSYS);
635 /* Loop over all inode entries and see if we find
636 * A request for the page we wish to update
638 spin_lock(&nfsi->req_lock);
639 req = nfs_page_find_request_locked(page);
641 if (!nfs_lock_request_dontget(req)) {
644 spin_unlock(&nfsi->req_lock);
645 error = nfs_wait_on_request(req);
646 nfs_release_request(req);
649 nfs_release_request(new);
650 return ERR_PTR(error);
654 spin_unlock(&nfsi->req_lock);
656 nfs_release_request(new);
662 nfs_lock_request_dontget(new);
663 error = nfs_inode_add_request(inode, new);
665 spin_unlock(&nfsi->req_lock);
666 nfs_unlock_request(new);
667 return ERR_PTR(error);
669 spin_unlock(&nfsi->req_lock);
672 spin_unlock(&nfsi->req_lock);
674 new = nfs_create_request(ctx, inode, page, offset, bytes);
679 /* We have a request for our page.
680 * If the creds don't match, or the
681 * page addresses don't match,
682 * tell the caller to wait on the conflicting
685 rqend = req->wb_offset + req->wb_bytes;
686 if (req->wb_context != ctx
687 || req->wb_page != page
688 || !nfs_dirty_request(req)
689 || offset > rqend || end < req->wb_offset) {
690 nfs_unlock_request(req);
691 return ERR_PTR(-EBUSY);
694 /* Okay, the request matches. Update the region */
695 if (offset < req->wb_offset) {
696 req->wb_offset = offset;
697 req->wb_pgbase = offset;
698 req->wb_bytes = rqend - req->wb_offset;
702 req->wb_bytes = end - req->wb_offset;
707 int nfs_flush_incompatible(struct file *file, struct page *page)
709 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
710 struct nfs_page *req;
711 int do_flush, status;
713 * Look for a request corresponding to this page. If there
714 * is one, and it belongs to another file, we flush it out
715 * before we try to copy anything into the page. Do this
716 * due to the lack of an ACCESS-type call in NFSv2.
717 * Also do the same if we find a request from an existing
721 req = nfs_page_find_request(page);
724 do_flush = req->wb_page != page || req->wb_context != ctx
725 || !nfs_dirty_request(req);
726 nfs_release_request(req);
729 status = nfs_wb_page(page->mapping->host, page);
730 } while (status == 0);
735 * Update and possibly write a cached page of an NFS file.
737 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
738 * things with a page scheduled for an RPC call (e.g. invalidate it).
740 int nfs_updatepage(struct file *file, struct page *page,
741 unsigned int offset, unsigned int count)
743 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
744 struct inode *inode = page->mapping->host;
747 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
749 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
750 file->f_dentry->d_parent->d_name.name,
751 file->f_dentry->d_name.name, count,
752 (long long)(page_offset(page) +offset));
754 /* If we're not using byte range locks, and we know the page
755 * is entirely in cache, it may be more efficient to avoid
756 * fragmenting write requests.
758 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
759 count = max(count + offset, nfs_page_length(page));
763 status = nfs_writepage_setup(ctx, page, offset, count);
764 __set_page_dirty_nobuffers(page);
766 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
767 status, (long long)i_size_read(inode));
769 ClearPageUptodate(page);
773 static void nfs_writepage_release(struct nfs_page *req)
775 end_page_writeback(req->wb_page);
777 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
778 if (!PageError(req->wb_page)) {
779 if (NFS_NEED_RESCHED(req)) {
780 nfs_mark_request_dirty(req);
782 } else if (NFS_NEED_COMMIT(req)) {
783 nfs_mark_request_commit(req);
787 nfs_inode_remove_request(req);
790 nfs_clear_commit(req);
791 nfs_clear_reschedule(req);
793 nfs_inode_remove_request(req);
795 nfs_clear_page_writeback(req);
798 static inline int flush_task_priority(int how)
800 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
802 return RPC_PRIORITY_HIGH;
804 return RPC_PRIORITY_LOW;
806 return RPC_PRIORITY_NORMAL;
810 * Set up the argument/result storage required for the RPC call.
812 static void nfs_write_rpcsetup(struct nfs_page *req,
813 struct nfs_write_data *data,
814 const struct rpc_call_ops *call_ops,
815 unsigned int count, unsigned int offset,
821 /* Set up the RPC argument and reply structs
822 * NB: take care not to mess about with data->commit et al. */
825 data->inode = inode = req->wb_context->dentry->d_inode;
826 data->cred = req->wb_context->cred;
828 data->args.fh = NFS_FH(inode);
829 data->args.offset = req_offset(req) + offset;
830 data->args.pgbase = req->wb_pgbase + offset;
831 data->args.pages = data->pagevec;
832 data->args.count = count;
833 data->args.context = req->wb_context;
835 data->res.fattr = &data->fattr;
836 data->res.count = count;
837 data->res.verf = &data->verf;
838 nfs_fattr_init(&data->fattr);
840 /* Set up the initial task struct. */
841 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
842 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
843 NFS_PROTO(inode)->write_setup(data, how);
845 data->task.tk_priority = flush_task_priority(how);
846 data->task.tk_cookie = (unsigned long)inode;
848 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
851 (long long)NFS_FILEID(inode),
853 (unsigned long long)data->args.offset);
856 static void nfs_execute_write(struct nfs_write_data *data)
858 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
861 rpc_clnt_sigmask(clnt, &oldset);
862 rpc_execute(&data->task);
863 rpc_clnt_sigunmask(clnt, &oldset);
867 * Generate multiple small requests to write out a single
868 * contiguous dirty area on one page.
870 static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
872 struct nfs_page *req = nfs_list_entry(head->next);
873 struct page *page = req->wb_page;
874 struct nfs_write_data *data;
875 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
880 nfs_list_remove_request(req);
882 nbytes = req->wb_bytes;
884 size_t len = min(nbytes, wsize);
886 data = nfs_writedata_alloc(len);
889 list_add(&data->pages, &list);
892 } while (nbytes != 0);
893 atomic_set(&req->wb_complete, requests);
895 ClearPageError(page);
896 set_page_writeback(page);
898 nbytes = req->wb_bytes;
900 data = list_entry(list.next, struct nfs_write_data, pages);
901 list_del_init(&data->pages);
903 data->pagevec[0] = page;
905 if (nbytes > wsize) {
906 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
911 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
912 nbytes, offset, how);
915 nfs_execute_write(data);
916 } while (nbytes != 0);
921 while (!list_empty(&list)) {
922 data = list_entry(list.next, struct nfs_write_data, pages);
923 list_del(&data->pages);
924 nfs_writedata_release(data);
926 nfs_mark_request_dirty(req);
927 nfs_clear_page_writeback(req);
932 * Create an RPC task for the given write request and kick it.
933 * The page must have been locked by the caller.
935 * It may happen that the page we're passed is not marked dirty.
936 * This is the case if nfs_updatepage detects a conflicting request
937 * that has been written but not committed.
939 static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
941 struct nfs_page *req;
943 struct nfs_write_data *data;
946 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
950 pages = data->pagevec;
952 while (!list_empty(head)) {
953 req = nfs_list_entry(head->next);
954 nfs_list_remove_request(req);
955 nfs_list_add_request(req, &data->pages);
956 ClearPageError(req->wb_page);
957 set_page_writeback(req->wb_page);
958 *pages++ = req->wb_page;
959 count += req->wb_bytes;
961 req = nfs_list_entry(data->pages.next);
963 /* Set up the argument struct */
964 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
966 nfs_execute_write(data);
969 while (!list_empty(head)) {
970 struct nfs_page *req = nfs_list_entry(head->next);
971 nfs_list_remove_request(req);
972 nfs_mark_request_dirty(req);
973 nfs_clear_page_writeback(req);
978 static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
980 LIST_HEAD(one_request);
981 int (*flush_one)(struct inode *, struct list_head *, int);
982 struct nfs_page *req;
983 int wpages = NFS_SERVER(inode)->wpages;
984 int wsize = NFS_SERVER(inode)->wsize;
987 flush_one = nfs_flush_one;
988 if (wsize < PAGE_CACHE_SIZE)
989 flush_one = nfs_flush_multi;
990 /* For single writes, FLUSH_STABLE is more efficient */
991 if (npages <= wpages && npages == NFS_I(inode)->npages
992 && nfs_list_entry(head->next)->wb_bytes <= wsize)
996 nfs_coalesce_requests(head, &one_request, wpages);
997 req = nfs_list_entry(one_request.next);
998 error = flush_one(inode, &one_request, how);
1001 } while (!list_empty(head));
1004 while (!list_empty(head)) {
1005 req = nfs_list_entry(head->next);
1006 nfs_list_remove_request(req);
1007 nfs_mark_request_dirty(req);
1008 nfs_clear_page_writeback(req);
1014 * Handle a write reply that flushed part of a page.
1016 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
1018 struct nfs_write_data *data = calldata;
1019 struct nfs_page *req = data->req;
1020 struct page *page = req->wb_page;
1022 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1023 req->wb_context->dentry->d_inode->i_sb->s_id,
1024 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1026 (long long)req_offset(req));
1028 if (nfs_writeback_done(task, data) != 0)
1031 if (task->tk_status < 0) {
1032 ClearPageUptodate(page);
1034 req->wb_context->error = task->tk_status;
1035 dprintk(", error = %d\n", task->tk_status);
1037 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1038 if (data->verf.committed < NFS_FILE_SYNC) {
1039 if (!NFS_NEED_COMMIT(req)) {
1040 nfs_defer_commit(req);
1041 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1042 dprintk(" defer commit\n");
1043 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1044 nfs_defer_reschedule(req);
1045 dprintk(" server reboot detected\n");
1052 if (atomic_dec_and_test(&req->wb_complete))
1053 nfs_writepage_release(req);
1056 static const struct rpc_call_ops nfs_write_partial_ops = {
1057 .rpc_call_done = nfs_writeback_done_partial,
1058 .rpc_release = nfs_writedata_release,
1062 * Handle a write reply that flushes a whole page.
1064 * FIXME: There is an inherent race with invalidate_inode_pages and
1065 * writebacks since the page->count is kept > 1 for as long
1066 * as the page has a write request pending.
1068 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1070 struct nfs_write_data *data = calldata;
1071 struct nfs_page *req;
1074 if (nfs_writeback_done(task, data) != 0)
1077 /* Update attributes as result of writeback. */
1078 while (!list_empty(&data->pages)) {
1079 req = nfs_list_entry(data->pages.next);
1080 nfs_list_remove_request(req);
1081 page = req->wb_page;
1083 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1084 req->wb_context->dentry->d_inode->i_sb->s_id,
1085 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1087 (long long)req_offset(req));
1089 if (task->tk_status < 0) {
1090 ClearPageUptodate(page);
1092 req->wb_context->error = task->tk_status;
1093 end_page_writeback(page);
1094 nfs_inode_remove_request(req);
1095 dprintk(", error = %d\n", task->tk_status);
1098 end_page_writeback(page);
1100 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1101 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1102 nfs_inode_remove_request(req);
1106 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1107 nfs_mark_request_commit(req);
1108 dprintk(" marked for commit\n");
1110 nfs_inode_remove_request(req);
1113 nfs_clear_page_writeback(req);
1117 static const struct rpc_call_ops nfs_write_full_ops = {
1118 .rpc_call_done = nfs_writeback_done_full,
1119 .rpc_release = nfs_writedata_release,
1124 * This function is called when the WRITE call is complete.
1126 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1128 struct nfs_writeargs *argp = &data->args;
1129 struct nfs_writeres *resp = &data->res;
1132 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1133 task->tk_pid, task->tk_status);
1136 * ->write_done will attempt to use post-op attributes to detect
1137 * conflicting writes by other clients. A strict interpretation
1138 * of close-to-open would allow us to continue caching even if
1139 * another writer had changed the file, but some applications
1140 * depend on tighter cache coherency when writing.
1142 status = NFS_PROTO(data->inode)->write_done(task, data);
1145 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1147 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1148 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1149 /* We tried a write call, but the server did not
1150 * commit data to stable storage even though we
1152 * Note: There is a known bug in Tru64 < 5.0 in which
1153 * the server reports NFS_DATA_SYNC, but performs
1154 * NFS_FILE_SYNC. We therefore implement this checking
1155 * as a dprintk() in order to avoid filling syslog.
1157 static unsigned long complain;
1159 if (time_before(complain, jiffies)) {
1160 dprintk("NFS: faulty NFS server %s:"
1161 " (committed = %d) != (stable = %d)\n",
1162 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1163 resp->verf->committed, argp->stable);
1164 complain = jiffies + 300 * HZ;
1168 /* Is this a short write? */
1169 if (task->tk_status >= 0 && resp->count < argp->count) {
1170 static unsigned long complain;
1172 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1174 /* Has the server at least made some progress? */
1175 if (resp->count != 0) {
1176 /* Was this an NFSv2 write or an NFSv3 stable write? */
1177 if (resp->verf->committed != NFS_UNSTABLE) {
1178 /* Resend from where the server left off */
1179 argp->offset += resp->count;
1180 argp->pgbase += resp->count;
1181 argp->count -= resp->count;
1183 /* Resend as a stable write in order to avoid
1184 * headaches in the case of a server crash.
1186 argp->stable = NFS_FILE_SYNC;
1188 rpc_restart_call(task);
1191 if (time_before(complain, jiffies)) {
1193 "NFS: Server wrote zero bytes, expected %u.\n",
1195 complain = jiffies + 300 * HZ;
1197 /* Can't do anything about it except throw an error. */
1198 task->tk_status = -EIO;
1204 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1205 void nfs_commit_release(void *wdata)
1207 nfs_commit_free(wdata);
1211 * Set up the argument/result storage required for the RPC call.
1213 static void nfs_commit_rpcsetup(struct list_head *head,
1214 struct nfs_write_data *data,
1217 struct nfs_page *first;
1218 struct inode *inode;
1221 /* Set up the RPC argument and reply structs
1222 * NB: take care not to mess about with data->commit et al. */
1224 list_splice_init(head, &data->pages);
1225 first = nfs_list_entry(data->pages.next);
1226 inode = first->wb_context->dentry->d_inode;
1228 data->inode = inode;
1229 data->cred = first->wb_context->cred;
1231 data->args.fh = NFS_FH(data->inode);
1232 /* Note: we always request a commit of the entire inode */
1233 data->args.offset = 0;
1234 data->args.count = 0;
1235 data->res.count = 0;
1236 data->res.fattr = &data->fattr;
1237 data->res.verf = &data->verf;
1238 nfs_fattr_init(&data->fattr);
1240 /* Set up the initial task struct. */
1241 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1242 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
1243 NFS_PROTO(inode)->commit_setup(data, how);
1245 data->task.tk_priority = flush_task_priority(how);
1246 data->task.tk_cookie = (unsigned long)inode;
1248 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
1252 * Commit dirty pages
1255 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1257 struct nfs_write_data *data;
1258 struct nfs_page *req;
1260 data = nfs_commit_alloc();
1265 /* Set up the argument struct */
1266 nfs_commit_rpcsetup(head, data, how);
1268 nfs_execute_write(data);
1271 while (!list_empty(head)) {
1272 req = nfs_list_entry(head->next);
1273 nfs_list_remove_request(req);
1274 nfs_mark_request_commit(req);
1275 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1276 nfs_clear_page_writeback(req);
1282 * COMMIT call returned
1284 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1286 struct nfs_write_data *data = calldata;
1287 struct nfs_page *req;
1289 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1290 task->tk_pid, task->tk_status);
1292 /* Call the NFS version-specific code */
1293 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1296 while (!list_empty(&data->pages)) {
1297 req = nfs_list_entry(data->pages.next);
1298 nfs_list_remove_request(req);
1299 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1301 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1302 req->wb_context->dentry->d_inode->i_sb->s_id,
1303 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1305 (long long)req_offset(req));
1306 if (task->tk_status < 0) {
1307 req->wb_context->error = task->tk_status;
1308 nfs_inode_remove_request(req);
1309 dprintk(", error = %d\n", task->tk_status);
1313 /* Okay, COMMIT succeeded, apparently. Check the verifier
1314 * returned by the server against all stored verfs. */
1315 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1316 /* We have a match */
1317 nfs_inode_remove_request(req);
1321 /* We have a mismatch. Write the page again */
1322 dprintk(" mismatch\n");
1323 nfs_mark_request_dirty(req);
1325 nfs_clear_page_writeback(req);
1329 static const struct rpc_call_ops nfs_commit_ops = {
1330 .rpc_call_done = nfs_commit_done,
1331 .rpc_release = nfs_commit_release,
1334 static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1340 static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
1342 struct nfs_inode *nfsi = NFS_I(mapping->host);
1346 spin_lock(&nfsi->req_lock);
1347 res = nfs_scan_dirty(mapping, wbc, &head);
1348 spin_unlock(&nfsi->req_lock);
1350 int error = nfs_flush_list(mapping->host, &head, res, how);
1357 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1358 int nfs_commit_inode(struct inode *inode, int how)
1360 struct nfs_inode *nfsi = NFS_I(inode);
1364 spin_lock(&nfsi->req_lock);
1365 res = nfs_scan_commit(inode, &head, 0, 0);
1366 spin_unlock(&nfsi->req_lock);
1368 int error = nfs_commit_list(inode, &head, how);
1376 long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
1378 struct inode *inode = mapping->host;
1379 struct nfs_inode *nfsi = NFS_I(inode);
1380 unsigned long idx_start, idx_end;
1381 unsigned int npages = 0;
1383 int nocommit = how & FLUSH_NOCOMMIT;
1387 if (wbc->range_cyclic)
1390 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1391 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1392 if (idx_end > idx_start) {
1393 unsigned long l_npages = 1 + idx_end - idx_start;
1395 if (sizeof(npages) != sizeof(l_npages) &&
1396 (unsigned long)npages != l_npages)
1400 how &= ~FLUSH_NOCOMMIT;
1401 spin_lock(&nfsi->req_lock);
1403 wbc->pages_skipped = 0;
1404 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1407 pages = nfs_scan_dirty(mapping, wbc, &head);
1409 spin_unlock(&nfsi->req_lock);
1410 if (how & FLUSH_INVALIDATE) {
1411 nfs_cancel_dirty_list(&head);
1414 ret = nfs_flush_list(inode, &head, pages, how);
1415 spin_lock(&nfsi->req_lock);
1418 if (wbc->pages_skipped != 0)
1422 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1424 if (wbc->pages_skipped != 0)
1428 if (how & FLUSH_INVALIDATE) {
1429 spin_unlock(&nfsi->req_lock);
1430 nfs_cancel_commit_list(&head);
1432 spin_lock(&nfsi->req_lock);
1435 pages += nfs_scan_commit(inode, &head, 0, 0);
1436 spin_unlock(&nfsi->req_lock);
1437 ret = nfs_commit_list(inode, &head, how);
1438 spin_lock(&nfsi->req_lock);
1440 spin_unlock(&nfsi->req_lock);
1445 * flush the inode to disk.
1447 int nfs_wb_all(struct inode *inode)
1449 struct address_space *mapping = inode->i_mapping;
1450 struct writeback_control wbc = {
1451 .bdi = mapping->backing_dev_info,
1452 .sync_mode = WB_SYNC_ALL,
1453 .nr_to_write = LONG_MAX,
1458 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1464 int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1466 struct writeback_control wbc = {
1467 .bdi = mapping->backing_dev_info,
1468 .sync_mode = WB_SYNC_ALL,
1469 .nr_to_write = LONG_MAX,
1470 .range_start = range_start,
1471 .range_end = range_end,
1475 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1481 static int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
1483 loff_t range_start = page_offset(page);
1484 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1485 struct writeback_control wbc = {
1486 .bdi = page->mapping->backing_dev_info,
1487 .sync_mode = WB_SYNC_ALL,
1488 .nr_to_write = LONG_MAX,
1489 .range_start = range_start,
1490 .range_end = range_end,
1494 BUG_ON(!PageLocked(page));
1495 if (!(how & FLUSH_NOWRITEPAGE) && clear_page_dirty_for_io(page)) {
1496 ret = nfs_writepage_locked(page, &wbc);
1500 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1508 * Write back all requests on one page - we do this before reading it.
1510 int nfs_wb_page(struct inode *inode, struct page* page)
1512 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
1515 int nfs_set_page_dirty(struct page *page)
1517 struct nfs_page *req;
1519 req = nfs_page_find_request(page);
1521 /* Mark any existing write requests for flushing */
1522 set_bit(PG_NEED_FLUSH, &req->wb_flags);
1523 nfs_release_request(req);
1525 return __set_page_dirty_nobuffers(page);
1529 int __init nfs_init_writepagecache(void)
1531 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1532 sizeof(struct nfs_write_data),
1533 0, SLAB_HWCACHE_ALIGN,
1535 if (nfs_wdata_cachep == NULL)
1538 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1540 if (nfs_wdata_mempool == NULL)
1543 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1545 if (nfs_commit_mempool == NULL)
1551 void nfs_destroy_writepagecache(void)
1553 mempool_destroy(nfs_commit_mempool);
1554 mempool_destroy(nfs_wdata_mempool);
1555 kmem_cache_destroy(nfs_wdata_cachep);