4 * Write file data over NFS.
6 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
9 #include <linux/types.h>
10 #include <linux/slab.h>
12 #include <linux/pagemap.h>
13 #include <linux/file.h>
14 #include <linux/writeback.h>
15 #include <linux/swap.h>
17 #include <linux/sunrpc/clnt.h>
18 #include <linux/nfs_fs.h>
19 #include <linux/nfs_mount.h>
20 #include <linux/nfs_page.h>
21 #include <linux/backing-dev.h>
23 #include <asm/uaccess.h>
24 #include <linux/smp_lock.h>
26 #include "delegation.h"
30 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
32 #define MIN_POOL_WRITE (32)
33 #define MIN_POOL_COMMIT (4)
36 * Local function declarations
38 static struct nfs_page * nfs_update_request(struct nfs_open_context*,
40 unsigned int, unsigned int);
41 static void nfs_mark_request_dirty(struct nfs_page *req);
42 static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how);
43 static const struct rpc_call_ops nfs_write_partial_ops;
44 static const struct rpc_call_ops nfs_write_full_ops;
45 static const struct rpc_call_ops nfs_commit_ops;
47 static struct kmem_cache *nfs_wdata_cachep;
48 static mempool_t *nfs_wdata_mempool;
49 static mempool_t *nfs_commit_mempool;
51 struct nfs_write_data *nfs_commit_alloc(void)
53 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
56 memset(p, 0, sizeof(*p));
57 INIT_LIST_HEAD(&p->pages);
62 void nfs_commit_rcu_free(struct rcu_head *head)
64 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
65 if (p && (p->pagevec != &p->page_array[0]))
67 mempool_free(p, nfs_commit_mempool);
70 void nfs_commit_free(struct nfs_write_data *wdata)
72 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
75 struct nfs_write_data *nfs_writedata_alloc(size_t len)
77 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
78 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
81 memset(p, 0, sizeof(*p));
82 INIT_LIST_HEAD(&p->pages);
83 p->npages = pagecount;
84 if (pagecount <= ARRAY_SIZE(p->page_array))
85 p->pagevec = p->page_array;
87 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
89 mempool_free(p, nfs_wdata_mempool);
97 static void nfs_writedata_rcu_free(struct rcu_head *head)
99 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
100 if (p && (p->pagevec != &p->page_array[0]))
102 mempool_free(p, nfs_wdata_mempool);
105 static void nfs_writedata_free(struct nfs_write_data *wdata)
107 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
110 void nfs_writedata_release(void *wdata)
112 nfs_writedata_free(wdata);
115 static struct nfs_page *nfs_page_find_request_locked(struct page *page)
117 struct nfs_page *req = NULL;
119 if (PagePrivate(page)) {
120 req = (struct nfs_page *)page_private(page);
122 atomic_inc(&req->wb_count);
127 static struct nfs_page *nfs_page_find_request(struct page *page)
129 struct nfs_page *req = NULL;
130 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
133 req = nfs_page_find_request_locked(page);
134 spin_unlock(req_lock);
138 /* Adjust the file length if we're writing beyond the end */
139 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
141 struct inode *inode = page->mapping->host;
142 loff_t end, i_size = i_size_read(inode);
143 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
145 if (i_size > 0 && page->index < end_index)
147 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
150 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
151 i_size_write(inode, end);
154 /* A writeback failed: mark the page as bad, and invalidate the page cache */
155 static void nfs_set_pageerror(struct page *page)
158 nfs_zap_mapping(page->mapping->host, page->mapping);
161 /* We can set the PG_uptodate flag if we see that a write request
162 * covers the full page.
164 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
166 if (PageUptodate(page))
170 if (count != nfs_page_length(page))
172 if (count != PAGE_CACHE_SIZE)
173 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
174 SetPageUptodate(page);
177 static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
178 unsigned int offset, unsigned int count)
180 struct nfs_page *req;
184 req = nfs_update_request(ctx, page, offset, count);
190 ret = nfs_wb_page(page->mapping->host, page);
194 /* Update file length */
195 nfs_grow_file(page, offset, count);
196 /* Set the PG_uptodate flag? */
197 nfs_mark_uptodate(page, offset, count);
198 nfs_unlock_request(req);
202 static int wb_priority(struct writeback_control *wbc)
204 if (wbc->for_reclaim)
205 return FLUSH_HIGHPRI;
206 if (wbc->for_kupdate)
212 * NFS congestion control
215 int nfs_congestion_kb;
217 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
218 #define NFS_CONGESTION_OFF_THRESH \
219 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
221 static int nfs_set_page_writeback(struct page *page)
223 int ret = test_set_page_writeback(page);
226 struct inode *inode = page->mapping->host;
227 struct nfs_server *nfss = NFS_SERVER(inode);
229 if (atomic_inc_return(&nfss->writeback) >
230 NFS_CONGESTION_ON_THRESH)
231 set_bdi_congested(&nfss->backing_dev_info, WRITE);
236 static void nfs_end_page_writeback(struct page *page)
238 struct inode *inode = page->mapping->host;
239 struct nfs_server *nfss = NFS_SERVER(inode);
241 end_page_writeback(page);
242 if (atomic_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) {
243 clear_bdi_congested(&nfss->backing_dev_info, WRITE);
244 congestion_end(WRITE);
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 (nfs_set_page_writeback(page) == 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, FLUSH_STABLE|wb_priority(wbc));
329 int nfs_writepage(struct page *page, struct writeback_control *wbc)
333 err = nfs_writepage_locked(page, wbc);
338 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
340 struct inode *inode = mapping->host;
343 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
345 err = generic_writepages(mapping, wbc);
348 err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
351 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
358 * Insert a write request into an inode
360 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
362 struct nfs_inode *nfsi = NFS_I(inode);
365 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
366 BUG_ON(error == -EEXIST);
371 nfs_begin_data_update(inode);
372 if (nfs_have_delegation(inode, FMODE_WRITE))
375 SetPagePrivate(req->wb_page);
376 set_page_private(req->wb_page, (unsigned long)req);
378 atomic_inc(&req->wb_count);
383 * Remove a write request from an inode
385 static void nfs_inode_remove_request(struct nfs_page *req)
387 struct inode *inode = req->wb_context->dentry->d_inode;
388 struct nfs_inode *nfsi = NFS_I(inode);
390 BUG_ON (!NFS_WBACK_BUSY(req));
392 spin_lock(&nfsi->req_lock);
393 set_page_private(req->wb_page, 0);
394 ClearPagePrivate(req->wb_page);
395 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
398 spin_unlock(&nfsi->req_lock);
399 nfs_end_data_update(inode);
402 spin_unlock(&nfsi->req_lock);
403 nfs_clear_request(req);
404 nfs_release_request(req);
408 * Add a request to the inode's dirty list.
411 nfs_mark_request_dirty(struct nfs_page *req)
413 struct inode *inode = req->wb_context->dentry->d_inode;
414 struct nfs_inode *nfsi = NFS_I(inode);
416 spin_lock(&nfsi->req_lock);
417 radix_tree_tag_set(&nfsi->nfs_page_tree,
418 req->wb_index, NFS_PAGE_TAG_DIRTY);
419 nfs_list_add_request(req, &nfsi->dirty);
421 spin_unlock(&nfsi->req_lock);
422 __mark_inode_dirty(inode, I_DIRTY_PAGES);
426 nfs_redirty_request(struct nfs_page *req)
428 __set_page_dirty_nobuffers(req->wb_page);
432 * Check if a request is dirty
435 nfs_dirty_request(struct nfs_page *req)
437 struct page *page = req->wb_page;
441 return !PageWriteback(req->wb_page);
444 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
446 * Add a request to the inode's commit list.
449 nfs_mark_request_commit(struct nfs_page *req)
451 struct inode *inode = req->wb_context->dentry->d_inode;
452 struct nfs_inode *nfsi = NFS_I(inode);
454 spin_lock(&nfsi->req_lock);
455 nfs_list_add_request(req, &nfsi->commit);
457 spin_unlock(&nfsi->req_lock);
458 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
459 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
464 * Wait for a request to complete.
466 * Interruptible by signals only if mounted with intr flag.
468 static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
470 struct nfs_inode *nfsi = NFS_I(inode);
471 struct nfs_page *req;
472 unsigned long idx_end, next;
473 unsigned int res = 0;
479 idx_end = idx_start + npages - 1;
482 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
483 if (req->wb_index > idx_end)
486 next = req->wb_index + 1;
487 BUG_ON(!NFS_WBACK_BUSY(req));
489 atomic_inc(&req->wb_count);
490 spin_unlock(&nfsi->req_lock);
491 error = nfs_wait_on_request(req);
492 nfs_release_request(req);
493 spin_lock(&nfsi->req_lock);
501 static void nfs_cancel_dirty_list(struct list_head *head)
503 struct nfs_page *req;
504 while(!list_empty(head)) {
505 req = nfs_list_entry(head->next);
506 nfs_list_remove_request(req);
507 nfs_end_page_writeback(req->wb_page);
508 nfs_inode_remove_request(req);
509 nfs_clear_page_writeback(req);
513 static void nfs_cancel_commit_list(struct list_head *head)
515 struct nfs_page *req;
517 while(!list_empty(head)) {
518 req = nfs_list_entry(head->next);
519 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
520 nfs_list_remove_request(req);
521 nfs_inode_remove_request(req);
522 nfs_unlock_request(req);
526 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
528 * nfs_scan_commit - Scan an inode for commit requests
529 * @inode: NFS inode to scan
530 * @dst: destination list
531 * @idx_start: lower bound of page->index to scan.
532 * @npages: idx_start + npages sets the upper bound to scan.
534 * Moves requests from the inode's 'commit' request list.
535 * The requests are *not* checked to ensure that they form a contiguous set.
538 nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
540 struct nfs_inode *nfsi = NFS_I(inode);
543 if (nfsi->ncommit != 0) {
544 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
545 nfsi->ncommit -= res;
546 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
547 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
552 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
558 static int nfs_wait_on_write_congestion(struct address_space *mapping)
560 struct inode *inode = mapping->host;
561 struct backing_dev_info *bdi = mapping->backing_dev_info;
566 if (!bdi_write_congested(bdi))
569 nfs_inc_stats(inode, NFSIOS_CONGESTIONWAIT);
572 struct rpc_clnt *clnt = NFS_CLIENT(inode);
575 rpc_clnt_sigmask(clnt, &oldset);
576 ret = congestion_wait_interruptible(WRITE, HZ/10);
577 rpc_clnt_sigunmask(clnt, &oldset);
578 if (ret == -ERESTARTSYS)
581 } while (bdi_write_congested(bdi));
587 * Try to update any existing write request, or create one if there is none.
588 * In order to match, the request's credentials must match those of
589 * the calling process.
591 * Note: Should always be called with the Page Lock held!
593 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
594 struct page *page, unsigned int offset, unsigned int bytes)
596 struct address_space *mapping = page->mapping;
597 struct inode *inode = mapping->host;
598 struct nfs_inode *nfsi = NFS_I(inode);
599 struct nfs_page *req, *new = NULL;
600 unsigned long rqend, end;
602 end = offset + bytes;
604 if (nfs_wait_on_write_congestion(mapping))
605 return ERR_PTR(-ERESTARTSYS);
607 /* Loop over all inode entries and see if we find
608 * A request for the page we wish to update
610 spin_lock(&nfsi->req_lock);
611 req = nfs_page_find_request_locked(page);
613 if (!nfs_lock_request_dontget(req)) {
616 spin_unlock(&nfsi->req_lock);
617 error = nfs_wait_on_request(req);
618 nfs_release_request(req);
621 nfs_release_request(new);
622 return ERR_PTR(error);
626 spin_unlock(&nfsi->req_lock);
628 nfs_release_request(new);
634 nfs_lock_request_dontget(new);
635 error = nfs_inode_add_request(inode, new);
637 spin_unlock(&nfsi->req_lock);
638 nfs_unlock_request(new);
639 return ERR_PTR(error);
641 spin_unlock(&nfsi->req_lock);
644 spin_unlock(&nfsi->req_lock);
646 new = nfs_create_request(ctx, inode, page, offset, bytes);
651 /* We have a request for our page.
652 * If the creds don't match, or the
653 * page addresses don't match,
654 * tell the caller to wait on the conflicting
657 rqend = req->wb_offset + req->wb_bytes;
658 if (req->wb_context != ctx
659 || req->wb_page != page
660 || !nfs_dirty_request(req)
661 || offset > rqend || end < req->wb_offset) {
662 nfs_unlock_request(req);
663 return ERR_PTR(-EBUSY);
666 /* Okay, the request matches. Update the region */
667 if (offset < req->wb_offset) {
668 req->wb_offset = offset;
669 req->wb_pgbase = offset;
670 req->wb_bytes = rqend - req->wb_offset;
674 req->wb_bytes = end - req->wb_offset;
679 int nfs_flush_incompatible(struct file *file, struct page *page)
681 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
682 struct nfs_page *req;
683 int do_flush, status;
685 * Look for a request corresponding to this page. If there
686 * is one, and it belongs to another file, we flush it out
687 * before we try to copy anything into the page. Do this
688 * due to the lack of an ACCESS-type call in NFSv2.
689 * Also do the same if we find a request from an existing
693 req = nfs_page_find_request(page);
696 do_flush = req->wb_page != page || req->wb_context != ctx
697 || !nfs_dirty_request(req);
698 nfs_release_request(req);
701 status = nfs_wb_page(page->mapping->host, page);
702 } while (status == 0);
707 * Update and possibly write a cached page of an NFS file.
709 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
710 * things with a page scheduled for an RPC call (e.g. invalidate it).
712 int nfs_updatepage(struct file *file, struct page *page,
713 unsigned int offset, unsigned int count)
715 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
716 struct inode *inode = page->mapping->host;
719 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
721 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
722 file->f_path.dentry->d_parent->d_name.name,
723 file->f_path.dentry->d_name.name, count,
724 (long long)(page_offset(page) +offset));
726 /* If we're not using byte range locks, and we know the page
727 * is entirely in cache, it may be more efficient to avoid
728 * fragmenting write requests.
730 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
731 count = max(count + offset, nfs_page_length(page));
735 status = nfs_writepage_setup(ctx, page, offset, count);
736 __set_page_dirty_nobuffers(page);
738 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
739 status, (long long)i_size_read(inode));
741 nfs_set_pageerror(page);
745 static void nfs_writepage_release(struct nfs_page *req)
747 nfs_end_page_writeback(req->wb_page);
749 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
750 if (!PageError(req->wb_page)) {
751 if (NFS_NEED_RESCHED(req)) {
752 nfs_redirty_request(req);
754 } else if (NFS_NEED_COMMIT(req)) {
755 nfs_mark_request_commit(req);
759 nfs_inode_remove_request(req);
762 nfs_clear_commit(req);
763 nfs_clear_reschedule(req);
765 nfs_inode_remove_request(req);
767 nfs_clear_page_writeback(req);
770 static inline int flush_task_priority(int how)
772 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
774 return RPC_PRIORITY_HIGH;
776 return RPC_PRIORITY_LOW;
778 return RPC_PRIORITY_NORMAL;
782 * Set up the argument/result storage required for the RPC call.
784 static void nfs_write_rpcsetup(struct nfs_page *req,
785 struct nfs_write_data *data,
786 const struct rpc_call_ops *call_ops,
787 unsigned int count, unsigned int offset,
793 /* Set up the RPC argument and reply structs
794 * NB: take care not to mess about with data->commit et al. */
797 data->inode = inode = req->wb_context->dentry->d_inode;
798 data->cred = req->wb_context->cred;
800 data->args.fh = NFS_FH(inode);
801 data->args.offset = req_offset(req) + offset;
802 data->args.pgbase = req->wb_pgbase + offset;
803 data->args.pages = data->pagevec;
804 data->args.count = count;
805 data->args.context = req->wb_context;
807 data->res.fattr = &data->fattr;
808 data->res.count = count;
809 data->res.verf = &data->verf;
810 nfs_fattr_init(&data->fattr);
812 /* Set up the initial task struct. */
813 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
814 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
815 NFS_PROTO(inode)->write_setup(data, how);
817 data->task.tk_priority = flush_task_priority(how);
818 data->task.tk_cookie = (unsigned long)inode;
820 dprintk("NFS: %5u initiated write call "
821 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
824 (long long)NFS_FILEID(inode),
826 (unsigned long long)data->args.offset);
829 static void nfs_execute_write(struct nfs_write_data *data)
831 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
834 rpc_clnt_sigmask(clnt, &oldset);
835 rpc_execute(&data->task);
836 rpc_clnt_sigunmask(clnt, &oldset);
840 * Generate multiple small requests to write out a single
841 * contiguous dirty area on one page.
843 static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
845 struct nfs_page *req = nfs_list_entry(head->next);
846 struct page *page = req->wb_page;
847 struct nfs_write_data *data;
848 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
853 nfs_list_remove_request(req);
855 nbytes = req->wb_bytes;
857 size_t len = min(nbytes, wsize);
859 data = nfs_writedata_alloc(len);
862 list_add(&data->pages, &list);
865 } while (nbytes != 0);
866 atomic_set(&req->wb_complete, requests);
868 ClearPageError(page);
870 nbytes = req->wb_bytes;
872 data = list_entry(list.next, struct nfs_write_data, pages);
873 list_del_init(&data->pages);
875 data->pagevec[0] = page;
877 if (nbytes > wsize) {
878 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
883 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
884 nbytes, offset, how);
887 nfs_execute_write(data);
888 } while (nbytes != 0);
893 while (!list_empty(&list)) {
894 data = list_entry(list.next, struct nfs_write_data, pages);
895 list_del(&data->pages);
896 nfs_writedata_release(data);
898 nfs_end_page_writeback(req->wb_page);
899 nfs_redirty_request(req);
900 nfs_clear_page_writeback(req);
905 * Create an RPC task for the given write request and kick it.
906 * The page must have been locked by the caller.
908 * It may happen that the page we're passed is not marked dirty.
909 * This is the case if nfs_updatepage detects a conflicting request
910 * that has been written but not committed.
912 static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
914 struct nfs_page *req;
916 struct nfs_write_data *data;
919 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
923 pages = data->pagevec;
925 while (!list_empty(head)) {
926 req = nfs_list_entry(head->next);
927 nfs_list_remove_request(req);
928 nfs_list_add_request(req, &data->pages);
929 ClearPageError(req->wb_page);
930 *pages++ = req->wb_page;
931 count += req->wb_bytes;
933 req = nfs_list_entry(data->pages.next);
935 /* Set up the argument struct */
936 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
938 nfs_execute_write(data);
941 while (!list_empty(head)) {
942 struct nfs_page *req = nfs_list_entry(head->next);
943 nfs_list_remove_request(req);
944 nfs_end_page_writeback(req->wb_page);
945 nfs_redirty_request(req);
946 nfs_clear_page_writeback(req);
951 static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
953 LIST_HEAD(one_request);
954 int (*flush_one)(struct inode *, struct list_head *, int);
955 struct nfs_page *req;
956 int wpages = NFS_SERVER(inode)->wpages;
957 int wsize = NFS_SERVER(inode)->wsize;
960 flush_one = nfs_flush_one;
961 if (wsize < PAGE_CACHE_SIZE)
962 flush_one = nfs_flush_multi;
963 /* For single writes, FLUSH_STABLE is more efficient */
964 if (npages <= wpages && npages == NFS_I(inode)->npages
965 && nfs_list_entry(head->next)->wb_bytes <= wsize)
969 nfs_coalesce_requests(head, &one_request, wpages);
970 req = nfs_list_entry(one_request.next);
971 error = flush_one(inode, &one_request, how);
974 } while (!list_empty(head));
977 while (!list_empty(head)) {
978 req = nfs_list_entry(head->next);
979 nfs_list_remove_request(req);
980 nfs_end_page_writeback(req->wb_page);
981 nfs_redirty_request(req);
982 nfs_clear_page_writeback(req);
988 * Handle a write reply that flushed part of a page.
990 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
992 struct nfs_write_data *data = calldata;
993 struct nfs_page *req = data->req;
994 struct page *page = req->wb_page;
996 dprintk("NFS: write (%s/%Ld %d@%Ld)",
997 req->wb_context->dentry->d_inode->i_sb->s_id,
998 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1000 (long long)req_offset(req));
1002 if (nfs_writeback_done(task, data) != 0)
1005 if (task->tk_status < 0) {
1006 nfs_set_pageerror(page);
1007 req->wb_context->error = task->tk_status;
1008 dprintk(", error = %d\n", task->tk_status);
1010 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1011 if (data->verf.committed < NFS_FILE_SYNC) {
1012 if (!NFS_NEED_COMMIT(req)) {
1013 nfs_defer_commit(req);
1014 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1015 dprintk(" defer commit\n");
1016 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1017 nfs_defer_reschedule(req);
1018 dprintk(" server reboot detected\n");
1025 if (atomic_dec_and_test(&req->wb_complete))
1026 nfs_writepage_release(req);
1029 static const struct rpc_call_ops nfs_write_partial_ops = {
1030 .rpc_call_done = nfs_writeback_done_partial,
1031 .rpc_release = nfs_writedata_release,
1035 * Handle a write reply that flushes a whole page.
1037 * FIXME: There is an inherent race with invalidate_inode_pages and
1038 * writebacks since the page->count is kept > 1 for as long
1039 * as the page has a write request pending.
1041 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1043 struct nfs_write_data *data = calldata;
1044 struct nfs_page *req;
1047 if (nfs_writeback_done(task, data) != 0)
1050 /* Update attributes as result of writeback. */
1051 while (!list_empty(&data->pages)) {
1052 req = nfs_list_entry(data->pages.next);
1053 nfs_list_remove_request(req);
1054 page = req->wb_page;
1056 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1057 req->wb_context->dentry->d_inode->i_sb->s_id,
1058 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1060 (long long)req_offset(req));
1062 if (task->tk_status < 0) {
1063 nfs_set_pageerror(page);
1064 req->wb_context->error = task->tk_status;
1065 nfs_end_page_writeback(page);
1066 nfs_inode_remove_request(req);
1067 dprintk(", error = %d\n", task->tk_status);
1070 nfs_end_page_writeback(page);
1072 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1073 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1074 nfs_inode_remove_request(req);
1078 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1079 nfs_mark_request_commit(req);
1080 dprintk(" marked for commit\n");
1082 nfs_inode_remove_request(req);
1085 nfs_clear_page_writeback(req);
1089 static const struct rpc_call_ops nfs_write_full_ops = {
1090 .rpc_call_done = nfs_writeback_done_full,
1091 .rpc_release = nfs_writedata_release,
1096 * This function is called when the WRITE call is complete.
1098 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1100 struct nfs_writeargs *argp = &data->args;
1101 struct nfs_writeres *resp = &data->res;
1104 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1105 task->tk_pid, task->tk_status);
1108 * ->write_done will attempt to use post-op attributes to detect
1109 * conflicting writes by other clients. A strict interpretation
1110 * of close-to-open would allow us to continue caching even if
1111 * another writer had changed the file, but some applications
1112 * depend on tighter cache coherency when writing.
1114 status = NFS_PROTO(data->inode)->write_done(task, data);
1117 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1119 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1120 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1121 /* We tried a write call, but the server did not
1122 * commit data to stable storage even though we
1124 * Note: There is a known bug in Tru64 < 5.0 in which
1125 * the server reports NFS_DATA_SYNC, but performs
1126 * NFS_FILE_SYNC. We therefore implement this checking
1127 * as a dprintk() in order to avoid filling syslog.
1129 static unsigned long complain;
1131 if (time_before(complain, jiffies)) {
1132 dprintk("NFS: faulty NFS server %s:"
1133 " (committed = %d) != (stable = %d)\n",
1134 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1135 resp->verf->committed, argp->stable);
1136 complain = jiffies + 300 * HZ;
1140 /* Is this a short write? */
1141 if (task->tk_status >= 0 && resp->count < argp->count) {
1142 static unsigned long complain;
1144 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1146 /* Has the server at least made some progress? */
1147 if (resp->count != 0) {
1148 /* Was this an NFSv2 write or an NFSv3 stable write? */
1149 if (resp->verf->committed != NFS_UNSTABLE) {
1150 /* Resend from where the server left off */
1151 argp->offset += resp->count;
1152 argp->pgbase += resp->count;
1153 argp->count -= resp->count;
1155 /* Resend as a stable write in order to avoid
1156 * headaches in the case of a server crash.
1158 argp->stable = NFS_FILE_SYNC;
1160 rpc_restart_call(task);
1163 if (time_before(complain, jiffies)) {
1165 "NFS: Server wrote zero bytes, expected %u.\n",
1167 complain = jiffies + 300 * HZ;
1169 /* Can't do anything about it except throw an error. */
1170 task->tk_status = -EIO;
1176 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1177 void nfs_commit_release(void *wdata)
1179 nfs_commit_free(wdata);
1183 * Set up the argument/result storage required for the RPC call.
1185 static void nfs_commit_rpcsetup(struct list_head *head,
1186 struct nfs_write_data *data,
1189 struct nfs_page *first;
1190 struct inode *inode;
1193 /* Set up the RPC argument and reply structs
1194 * NB: take care not to mess about with data->commit et al. */
1196 list_splice_init(head, &data->pages);
1197 first = nfs_list_entry(data->pages.next);
1198 inode = first->wb_context->dentry->d_inode;
1200 data->inode = inode;
1201 data->cred = first->wb_context->cred;
1203 data->args.fh = NFS_FH(data->inode);
1204 /* Note: we always request a commit of the entire inode */
1205 data->args.offset = 0;
1206 data->args.count = 0;
1207 data->res.count = 0;
1208 data->res.fattr = &data->fattr;
1209 data->res.verf = &data->verf;
1210 nfs_fattr_init(&data->fattr);
1212 /* Set up the initial task struct. */
1213 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1214 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
1215 NFS_PROTO(inode)->commit_setup(data, how);
1217 data->task.tk_priority = flush_task_priority(how);
1218 data->task.tk_cookie = (unsigned long)inode;
1220 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1224 * Commit dirty pages
1227 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1229 struct nfs_write_data *data;
1230 struct nfs_page *req;
1232 data = nfs_commit_alloc();
1237 /* Set up the argument struct */
1238 nfs_commit_rpcsetup(head, data, how);
1240 nfs_execute_write(data);
1243 while (!list_empty(head)) {
1244 req = nfs_list_entry(head->next);
1245 nfs_list_remove_request(req);
1246 nfs_mark_request_commit(req);
1247 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1248 nfs_clear_page_writeback(req);
1254 * COMMIT call returned
1256 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1258 struct nfs_write_data *data = calldata;
1259 struct nfs_page *req;
1261 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1262 task->tk_pid, task->tk_status);
1264 /* Call the NFS version-specific code */
1265 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1268 while (!list_empty(&data->pages)) {
1269 req = nfs_list_entry(data->pages.next);
1270 nfs_list_remove_request(req);
1271 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1273 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1274 req->wb_context->dentry->d_inode->i_sb->s_id,
1275 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1277 (long long)req_offset(req));
1278 if (task->tk_status < 0) {
1279 req->wb_context->error = task->tk_status;
1280 nfs_inode_remove_request(req);
1281 dprintk(", error = %d\n", task->tk_status);
1285 /* Okay, COMMIT succeeded, apparently. Check the verifier
1286 * returned by the server against all stored verfs. */
1287 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1288 /* We have a match */
1289 nfs_inode_remove_request(req);
1293 /* We have a mismatch. Write the page again */
1294 dprintk(" mismatch\n");
1295 nfs_redirty_request(req);
1297 nfs_clear_page_writeback(req);
1301 static const struct rpc_call_ops nfs_commit_ops = {
1302 .rpc_call_done = nfs_commit_done,
1303 .rpc_release = nfs_commit_release,
1306 static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1312 static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
1314 struct nfs_inode *nfsi = NFS_I(mapping->host);
1318 spin_lock(&nfsi->req_lock);
1319 res = nfs_scan_dirty(mapping, wbc, &head);
1320 spin_unlock(&nfsi->req_lock);
1322 int error = nfs_flush_list(mapping->host, &head, res, how);
1329 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1330 int nfs_commit_inode(struct inode *inode, int how)
1332 struct nfs_inode *nfsi = NFS_I(inode);
1336 spin_lock(&nfsi->req_lock);
1337 res = nfs_scan_commit(inode, &head, 0, 0);
1338 spin_unlock(&nfsi->req_lock);
1340 int error = nfs_commit_list(inode, &head, how);
1348 long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
1350 struct inode *inode = mapping->host;
1351 struct nfs_inode *nfsi = NFS_I(inode);
1352 unsigned long idx_start, idx_end;
1353 unsigned int npages = 0;
1355 int nocommit = how & FLUSH_NOCOMMIT;
1359 if (wbc->range_cyclic)
1362 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1363 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1364 if (idx_end > idx_start) {
1365 unsigned long l_npages = 1 + idx_end - idx_start;
1367 if (sizeof(npages) != sizeof(l_npages) &&
1368 (unsigned long)npages != l_npages)
1372 how &= ~FLUSH_NOCOMMIT;
1373 spin_lock(&nfsi->req_lock);
1375 wbc->pages_skipped = 0;
1376 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1379 pages = nfs_scan_dirty(mapping, wbc, &head);
1381 spin_unlock(&nfsi->req_lock);
1382 if (how & FLUSH_INVALIDATE) {
1383 nfs_cancel_dirty_list(&head);
1386 ret = nfs_flush_list(inode, &head, pages, how);
1387 spin_lock(&nfsi->req_lock);
1390 if (wbc->pages_skipped != 0)
1394 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1396 if (wbc->pages_skipped != 0)
1400 if (how & FLUSH_INVALIDATE) {
1401 spin_unlock(&nfsi->req_lock);
1402 nfs_cancel_commit_list(&head);
1404 spin_lock(&nfsi->req_lock);
1407 pages += nfs_scan_commit(inode, &head, 0, 0);
1408 spin_unlock(&nfsi->req_lock);
1409 ret = nfs_commit_list(inode, &head, how);
1410 spin_lock(&nfsi->req_lock);
1412 spin_unlock(&nfsi->req_lock);
1417 * flush the inode to disk.
1419 int nfs_wb_all(struct inode *inode)
1421 struct address_space *mapping = inode->i_mapping;
1422 struct writeback_control wbc = {
1423 .bdi = mapping->backing_dev_info,
1424 .sync_mode = WB_SYNC_ALL,
1425 .nr_to_write = LONG_MAX,
1426 .for_writepages = 1,
1431 ret = generic_writepages(mapping, &wbc);
1434 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1438 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1442 int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1444 struct writeback_control wbc = {
1445 .bdi = mapping->backing_dev_info,
1446 .sync_mode = WB_SYNC_ALL,
1447 .nr_to_write = LONG_MAX,
1448 .range_start = range_start,
1449 .range_end = range_end,
1450 .for_writepages = 1,
1454 if (!(how & FLUSH_NOWRITEPAGE)) {
1455 ret = generic_writepages(mapping, &wbc);
1459 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1463 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1467 int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
1469 loff_t range_start = page_offset(page);
1470 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1471 struct writeback_control wbc = {
1472 .bdi = page->mapping->backing_dev_info,
1473 .sync_mode = WB_SYNC_ALL,
1474 .nr_to_write = LONG_MAX,
1475 .range_start = range_start,
1476 .range_end = range_end,
1480 BUG_ON(!PageLocked(page));
1481 if (!(how & FLUSH_NOWRITEPAGE) && clear_page_dirty_for_io(page)) {
1482 ret = nfs_writepage_locked(page, &wbc);
1486 if (!PagePrivate(page))
1488 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1492 __mark_inode_dirty(inode, I_DIRTY_PAGES);
1497 * Write back all requests on one page - we do this before reading it.
1499 int nfs_wb_page(struct inode *inode, struct page* page)
1501 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
1504 int nfs_set_page_dirty(struct page *page)
1506 struct nfs_page *req;
1508 req = nfs_page_find_request(page);
1510 /* Mark any existing write requests for flushing */
1511 set_bit(PG_NEED_FLUSH, &req->wb_flags);
1512 nfs_release_request(req);
1514 return __set_page_dirty_nobuffers(page);
1518 int __init nfs_init_writepagecache(void)
1520 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1521 sizeof(struct nfs_write_data),
1522 0, SLAB_HWCACHE_ALIGN,
1524 if (nfs_wdata_cachep == NULL)
1527 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1529 if (nfs_wdata_mempool == NULL)
1532 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1534 if (nfs_commit_mempool == NULL)
1538 * NFS congestion size, scale with available memory.
1550 * This allows larger machines to have larger/more transfers.
1551 * Limit the default to 256M
1553 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1554 if (nfs_congestion_kb > 256*1024)
1555 nfs_congestion_kb = 256*1024;
1560 void nfs_destroy_writepagecache(void)
1562 mempool_destroy(nfs_commit_mempool);
1563 mempool_destroy(nfs_wdata_mempool);
1564 kmem_cache_destroy(nfs_wdata_cachep);