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 long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how);
42 static const struct rpc_call_ops nfs_write_partial_ops;
43 static const struct rpc_call_ops nfs_write_full_ops;
44 static const struct rpc_call_ops nfs_commit_ops;
46 static struct kmem_cache *nfs_wdata_cachep;
47 static mempool_t *nfs_wdata_mempool;
48 static mempool_t *nfs_commit_mempool;
50 struct nfs_write_data *nfs_commit_alloc(void)
52 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
55 memset(p, 0, sizeof(*p));
56 INIT_LIST_HEAD(&p->pages);
61 void nfs_commit_rcu_free(struct rcu_head *head)
63 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
64 if (p && (p->pagevec != &p->page_array[0]))
66 mempool_free(p, nfs_commit_mempool);
69 void nfs_commit_free(struct nfs_write_data *wdata)
71 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
74 struct nfs_write_data *nfs_writedata_alloc(size_t len)
76 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
77 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
80 memset(p, 0, sizeof(*p));
81 INIT_LIST_HEAD(&p->pages);
82 p->npages = pagecount;
83 if (pagecount <= ARRAY_SIZE(p->page_array))
84 p->pagevec = p->page_array;
86 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
88 mempool_free(p, nfs_wdata_mempool);
96 static void nfs_writedata_rcu_free(struct rcu_head *head)
98 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
99 if (p && (p->pagevec != &p->page_array[0]))
101 mempool_free(p, nfs_wdata_mempool);
104 static void nfs_writedata_free(struct nfs_write_data *wdata)
106 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
109 void nfs_writedata_release(void *wdata)
111 nfs_writedata_free(wdata);
114 static struct nfs_page *nfs_page_find_request_locked(struct page *page)
116 struct nfs_page *req = NULL;
118 if (PagePrivate(page)) {
119 req = (struct nfs_page *)page_private(page);
121 atomic_inc(&req->wb_count);
126 static struct nfs_page *nfs_page_find_request(struct page *page)
128 struct nfs_page *req = NULL;
129 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
132 req = nfs_page_find_request_locked(page);
133 spin_unlock(req_lock);
137 /* Adjust the file length if we're writing beyond the end */
138 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
140 struct inode *inode = page->mapping->host;
141 loff_t end, i_size = i_size_read(inode);
142 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
144 if (i_size > 0 && page->index < end_index)
146 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
149 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
150 i_size_write(inode, end);
153 /* A writeback failed: mark the page as bad, and invalidate the page cache */
154 static void nfs_set_pageerror(struct page *page)
157 nfs_zap_mapping(page->mapping->host, page->mapping);
160 /* We can set the PG_uptodate flag if we see that a write request
161 * covers the full page.
163 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
165 if (PageUptodate(page))
169 if (count != nfs_page_length(page))
171 if (count != PAGE_CACHE_SIZE)
172 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
173 SetPageUptodate(page);
176 static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
177 unsigned int offset, unsigned int count)
179 struct nfs_page *req;
183 req = nfs_update_request(ctx, page, offset, count);
189 ret = nfs_wb_page(page->mapping->host, page);
193 /* Update file length */
194 nfs_grow_file(page, offset, count);
195 /* Set the PG_uptodate flag? */
196 nfs_mark_uptodate(page, offset, count);
197 nfs_unlock_request(req);
201 static int wb_priority(struct writeback_control *wbc)
203 if (wbc->for_reclaim)
204 return FLUSH_HIGHPRI;
205 if (wbc->for_kupdate)
211 * NFS congestion control
214 int nfs_congestion_kb;
216 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
217 #define NFS_CONGESTION_OFF_THRESH \
218 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
220 static int nfs_set_page_writeback(struct page *page)
222 int ret = test_set_page_writeback(page);
225 struct inode *inode = page->mapping->host;
226 struct nfs_server *nfss = NFS_SERVER(inode);
228 if (atomic_inc_return(&nfss->writeback) >
229 NFS_CONGESTION_ON_THRESH)
230 set_bdi_congested(&nfss->backing_dev_info, WRITE);
235 static void nfs_end_page_writeback(struct page *page)
237 struct inode *inode = page->mapping->host;
238 struct nfs_server *nfss = NFS_SERVER(inode);
240 end_page_writeback(page);
241 if (atomic_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) {
242 clear_bdi_congested(&nfss->backing_dev_info, WRITE);
243 congestion_end(WRITE);
248 * Find an associated nfs write request, and prepare to flush it out
249 * Returns 1 if there was no write request, or if the request was
250 * already tagged by nfs_set_page_dirty.Returns 0 if the request
252 * May also return an error if the user signalled nfs_wait_on_request().
254 static int nfs_page_mark_flush(struct page *page)
256 struct nfs_page *req;
257 struct nfs_inode *nfsi = NFS_I(page->mapping->host);
258 spinlock_t *req_lock = &nfsi->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 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
283 /* This request is marked for commit */
284 spin_unlock(req_lock);
285 nfs_unlock_request(req);
288 if (nfs_set_page_writeback(page) == 0) {
289 nfs_list_remove_request(req);
290 /* add the request to the inode's dirty list. */
291 radix_tree_tag_set(&nfsi->nfs_page_tree,
292 req->wb_index, NFS_PAGE_TAG_DIRTY);
293 nfs_list_add_request(req, &nfsi->dirty);
295 spin_unlock(req_lock);
296 __mark_inode_dirty(page->mapping->host, I_DIRTY_PAGES);
298 spin_unlock(req_lock);
299 ret = test_bit(PG_NEED_FLUSH, &req->wb_flags);
300 nfs_unlock_request(req);
305 * Write an mmapped page to the server.
307 static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
309 struct nfs_open_context *ctx;
310 struct inode *inode = page->mapping->host;
314 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
315 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
317 err = nfs_page_mark_flush(page);
321 offset = nfs_page_length(page);
325 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
330 err = nfs_writepage_setup(ctx, page, 0, offset);
331 put_nfs_open_context(ctx);
334 err = nfs_page_mark_flush(page);
338 if (!wbc->for_writepages)
339 nfs_flush_mapping(page->mapping, wbc, FLUSH_STABLE|wb_priority(wbc));
343 int nfs_writepage(struct page *page, struct writeback_control *wbc)
347 err = nfs_writepage_locked(page, wbc);
352 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
354 struct inode *inode = mapping->host;
357 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
359 err = generic_writepages(mapping, wbc);
362 err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
365 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
372 * Insert a write request into an inode
374 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
376 struct nfs_inode *nfsi = NFS_I(inode);
379 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
380 BUG_ON(error == -EEXIST);
385 nfs_begin_data_update(inode);
386 if (nfs_have_delegation(inode, FMODE_WRITE))
389 SetPagePrivate(req->wb_page);
390 set_page_private(req->wb_page, (unsigned long)req);
392 atomic_inc(&req->wb_count);
397 * Remove a write request from an inode
399 static void nfs_inode_remove_request(struct nfs_page *req)
401 struct inode *inode = req->wb_context->dentry->d_inode;
402 struct nfs_inode *nfsi = NFS_I(inode);
404 BUG_ON (!NFS_WBACK_BUSY(req));
406 spin_lock(&nfsi->req_lock);
407 set_page_private(req->wb_page, 0);
408 ClearPagePrivate(req->wb_page);
409 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
412 spin_unlock(&nfsi->req_lock);
413 nfs_end_data_update(inode);
416 spin_unlock(&nfsi->req_lock);
417 nfs_clear_request(req);
418 nfs_release_request(req);
422 nfs_redirty_request(struct nfs_page *req)
424 __set_page_dirty_nobuffers(req->wb_page);
428 * Check if a request is dirty
431 nfs_dirty_request(struct nfs_page *req)
433 struct page *page = req->wb_page;
435 if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
437 return !PageWriteback(req->wb_page);
440 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
442 * Add a request to the inode's commit list.
445 nfs_mark_request_commit(struct nfs_page *req)
447 struct inode *inode = req->wb_context->dentry->d_inode;
448 struct nfs_inode *nfsi = NFS_I(inode);
450 spin_lock(&nfsi->req_lock);
451 nfs_list_add_request(req, &nfsi->commit);
453 set_bit(PG_NEED_COMMIT, &(req)->wb_flags);
454 spin_unlock(&nfsi->req_lock);
455 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
456 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
460 int nfs_write_need_commit(struct nfs_write_data *data)
462 return data->verf.committed != NFS_FILE_SYNC;
466 int nfs_reschedule_unstable_write(struct nfs_page *req)
468 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
469 nfs_mark_request_commit(req);
472 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
473 nfs_redirty_request(req);
480 nfs_mark_request_commit(struct nfs_page *req)
485 int nfs_write_need_commit(struct nfs_write_data *data)
491 int nfs_reschedule_unstable_write(struct nfs_page *req)
498 * Wait for a request to complete.
500 * Interruptible by signals only if mounted with intr flag.
502 static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
504 struct nfs_inode *nfsi = NFS_I(inode);
505 struct nfs_page *req;
506 unsigned long idx_end, next;
507 unsigned int res = 0;
513 idx_end = idx_start + npages - 1;
516 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
517 if (req->wb_index > idx_end)
520 next = req->wb_index + 1;
521 BUG_ON(!NFS_WBACK_BUSY(req));
523 atomic_inc(&req->wb_count);
524 spin_unlock(&nfsi->req_lock);
525 error = nfs_wait_on_request(req);
526 nfs_release_request(req);
527 spin_lock(&nfsi->req_lock);
535 static void nfs_cancel_dirty_list(struct list_head *head)
537 struct nfs_page *req;
538 while(!list_empty(head)) {
539 req = nfs_list_entry(head->next);
540 nfs_list_remove_request(req);
541 nfs_end_page_writeback(req->wb_page);
542 nfs_inode_remove_request(req);
543 nfs_clear_page_writeback(req);
547 static void nfs_cancel_commit_list(struct list_head *head)
549 struct nfs_page *req;
551 while(!list_empty(head)) {
552 req = nfs_list_entry(head->next);
553 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
554 nfs_list_remove_request(req);
555 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
556 nfs_inode_remove_request(req);
557 nfs_unlock_request(req);
561 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
563 * nfs_scan_commit - Scan an inode for commit requests
564 * @inode: NFS inode to scan
565 * @dst: destination list
566 * @idx_start: lower bound of page->index to scan.
567 * @npages: idx_start + npages sets the upper bound to scan.
569 * Moves requests from the inode's 'commit' request list.
570 * The requests are *not* checked to ensure that they form a contiguous set.
573 nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
575 struct nfs_inode *nfsi = NFS_I(inode);
578 if (nfsi->ncommit != 0) {
579 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
580 nfsi->ncommit -= res;
581 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
582 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
587 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
593 static int nfs_wait_on_write_congestion(struct address_space *mapping)
595 struct inode *inode = mapping->host;
596 struct backing_dev_info *bdi = mapping->backing_dev_info;
601 if (!bdi_write_congested(bdi))
604 nfs_inc_stats(inode, NFSIOS_CONGESTIONWAIT);
607 struct rpc_clnt *clnt = NFS_CLIENT(inode);
610 rpc_clnt_sigmask(clnt, &oldset);
611 ret = congestion_wait_interruptible(WRITE, HZ/10);
612 rpc_clnt_sigunmask(clnt, &oldset);
613 if (ret == -ERESTARTSYS)
616 } while (bdi_write_congested(bdi));
622 * Try to update any existing write request, or create one if there is none.
623 * In order to match, the request's credentials must match those of
624 * the calling process.
626 * Note: Should always be called with the Page Lock held!
628 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
629 struct page *page, unsigned int offset, unsigned int bytes)
631 struct address_space *mapping = page->mapping;
632 struct inode *inode = mapping->host;
633 struct nfs_inode *nfsi = NFS_I(inode);
634 struct nfs_page *req, *new = NULL;
635 unsigned long rqend, end;
637 end = offset + bytes;
639 if (nfs_wait_on_write_congestion(mapping))
640 return ERR_PTR(-ERESTARTSYS);
642 /* Loop over all inode entries and see if we find
643 * A request for the page we wish to update
645 spin_lock(&nfsi->req_lock);
646 req = nfs_page_find_request_locked(page);
648 if (!nfs_lock_request_dontget(req)) {
651 spin_unlock(&nfsi->req_lock);
652 error = nfs_wait_on_request(req);
653 nfs_release_request(req);
656 nfs_release_request(new);
657 return ERR_PTR(error);
661 spin_unlock(&nfsi->req_lock);
663 nfs_release_request(new);
669 nfs_lock_request_dontget(new);
670 error = nfs_inode_add_request(inode, new);
672 spin_unlock(&nfsi->req_lock);
673 nfs_unlock_request(new);
674 return ERR_PTR(error);
676 spin_unlock(&nfsi->req_lock);
679 spin_unlock(&nfsi->req_lock);
681 new = nfs_create_request(ctx, inode, page, offset, bytes);
686 /* We have a request for our page.
687 * If the creds don't match, or the
688 * page addresses don't match,
689 * tell the caller to wait on the conflicting
692 rqend = req->wb_offset + req->wb_bytes;
693 if (req->wb_context != ctx
694 || req->wb_page != page
695 || !nfs_dirty_request(req)
696 || offset > rqend || end < req->wb_offset) {
697 nfs_unlock_request(req);
698 return ERR_PTR(-EBUSY);
701 /* Okay, the request matches. Update the region */
702 if (offset < req->wb_offset) {
703 req->wb_offset = offset;
704 req->wb_pgbase = offset;
705 req->wb_bytes = rqend - req->wb_offset;
709 req->wb_bytes = end - req->wb_offset;
714 int nfs_flush_incompatible(struct file *file, struct page *page)
716 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
717 struct nfs_page *req;
718 int do_flush, status;
720 * Look for a request corresponding to this page. If there
721 * is one, and it belongs to another file, we flush it out
722 * before we try to copy anything into the page. Do this
723 * due to the lack of an ACCESS-type call in NFSv2.
724 * Also do the same if we find a request from an existing
728 req = nfs_page_find_request(page);
731 do_flush = req->wb_page != page || req->wb_context != ctx
732 || !nfs_dirty_request(req);
733 nfs_release_request(req);
736 status = nfs_wb_page(page->mapping->host, page);
737 } while (status == 0);
742 * Update and possibly write a cached page of an NFS file.
744 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
745 * things with a page scheduled for an RPC call (e.g. invalidate it).
747 int nfs_updatepage(struct file *file, struct page *page,
748 unsigned int offset, unsigned int count)
750 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
751 struct inode *inode = page->mapping->host;
754 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
756 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
757 file->f_path.dentry->d_parent->d_name.name,
758 file->f_path.dentry->d_name.name, count,
759 (long long)(page_offset(page) +offset));
761 /* If we're not using byte range locks, and we know the page
762 * is entirely in cache, it may be more efficient to avoid
763 * fragmenting write requests.
765 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
766 count = max(count + offset, nfs_page_length(page));
770 status = nfs_writepage_setup(ctx, page, offset, count);
771 __set_page_dirty_nobuffers(page);
773 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
774 status, (long long)i_size_read(inode));
776 nfs_set_pageerror(page);
780 static void nfs_writepage_release(struct nfs_page *req)
783 if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req)) {
784 nfs_end_page_writeback(req->wb_page);
785 nfs_inode_remove_request(req);
787 nfs_end_page_writeback(req->wb_page);
788 nfs_clear_page_writeback(req);
791 static inline int flush_task_priority(int how)
793 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
795 return RPC_PRIORITY_HIGH;
797 return RPC_PRIORITY_LOW;
799 return RPC_PRIORITY_NORMAL;
803 * Set up the argument/result storage required for the RPC call.
805 static void nfs_write_rpcsetup(struct nfs_page *req,
806 struct nfs_write_data *data,
807 const struct rpc_call_ops *call_ops,
808 unsigned int count, unsigned int offset,
814 /* Set up the RPC argument and reply structs
815 * NB: take care not to mess about with data->commit et al. */
818 data->inode = inode = req->wb_context->dentry->d_inode;
819 data->cred = req->wb_context->cred;
821 data->args.fh = NFS_FH(inode);
822 data->args.offset = req_offset(req) + offset;
823 data->args.pgbase = req->wb_pgbase + offset;
824 data->args.pages = data->pagevec;
825 data->args.count = count;
826 data->args.context = req->wb_context;
828 data->res.fattr = &data->fattr;
829 data->res.count = count;
830 data->res.verf = &data->verf;
831 nfs_fattr_init(&data->fattr);
833 /* Set up the initial task struct. */
834 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
835 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
836 NFS_PROTO(inode)->write_setup(data, how);
838 data->task.tk_priority = flush_task_priority(how);
839 data->task.tk_cookie = (unsigned long)inode;
841 dprintk("NFS: %5u initiated write call "
842 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
845 (long long)NFS_FILEID(inode),
847 (unsigned long long)data->args.offset);
850 static void nfs_execute_write(struct nfs_write_data *data)
852 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
855 rpc_clnt_sigmask(clnt, &oldset);
856 rpc_execute(&data->task);
857 rpc_clnt_sigunmask(clnt, &oldset);
861 * Generate multiple small requests to write out a single
862 * contiguous dirty area on one page.
864 static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
866 struct nfs_page *req = nfs_list_entry(head->next);
867 struct page *page = req->wb_page;
868 struct nfs_write_data *data;
869 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
874 nfs_list_remove_request(req);
876 nbytes = req->wb_bytes;
878 size_t len = min(nbytes, wsize);
880 data = nfs_writedata_alloc(len);
883 list_add(&data->pages, &list);
886 } while (nbytes != 0);
887 atomic_set(&req->wb_complete, requests);
889 ClearPageError(page);
891 nbytes = req->wb_bytes;
893 data = list_entry(list.next, struct nfs_write_data, pages);
894 list_del_init(&data->pages);
896 data->pagevec[0] = page;
898 if (nbytes > wsize) {
899 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
904 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
905 nbytes, offset, how);
908 nfs_execute_write(data);
909 } while (nbytes != 0);
914 while (!list_empty(&list)) {
915 data = list_entry(list.next, struct nfs_write_data, pages);
916 list_del(&data->pages);
917 nfs_writedata_release(data);
919 nfs_redirty_request(req);
920 nfs_end_page_writeback(req->wb_page);
921 nfs_clear_page_writeback(req);
926 * Create an RPC task for the given write request and kick it.
927 * The page must have been locked by the caller.
929 * It may happen that the page we're passed is not marked dirty.
930 * This is the case if nfs_updatepage detects a conflicting request
931 * that has been written but not committed.
933 static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
935 struct nfs_page *req;
937 struct nfs_write_data *data;
940 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
944 pages = data->pagevec;
946 while (!list_empty(head)) {
947 req = nfs_list_entry(head->next);
948 nfs_list_remove_request(req);
949 nfs_list_add_request(req, &data->pages);
950 ClearPageError(req->wb_page);
951 *pages++ = req->wb_page;
952 count += req->wb_bytes;
954 req = nfs_list_entry(data->pages.next);
956 /* Set up the argument struct */
957 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
959 nfs_execute_write(data);
962 while (!list_empty(head)) {
963 struct nfs_page *req = nfs_list_entry(head->next);
964 nfs_list_remove_request(req);
965 nfs_redirty_request(req);
966 nfs_end_page_writeback(req->wb_page);
967 nfs_clear_page_writeback(req);
972 static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
974 LIST_HEAD(one_request);
975 int (*flush_one)(struct inode *, struct list_head *, int);
976 struct nfs_page *req;
977 int wpages = NFS_SERVER(inode)->wpages;
978 int wsize = NFS_SERVER(inode)->wsize;
981 flush_one = nfs_flush_one;
982 if (wsize < PAGE_CACHE_SIZE)
983 flush_one = nfs_flush_multi;
984 /* For single writes, FLUSH_STABLE is more efficient */
985 if (npages <= wpages && npages == NFS_I(inode)->npages
986 && nfs_list_entry(head->next)->wb_bytes <= wsize)
990 nfs_coalesce_requests(head, &one_request, wpages);
991 req = nfs_list_entry(one_request.next);
992 error = flush_one(inode, &one_request, how);
995 } while (!list_empty(head));
998 while (!list_empty(head)) {
999 req = nfs_list_entry(head->next);
1000 nfs_list_remove_request(req);
1001 nfs_redirty_request(req);
1002 nfs_end_page_writeback(req->wb_page);
1003 nfs_clear_page_writeback(req);
1009 * Handle a write reply that flushed part of a page.
1011 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
1013 struct nfs_write_data *data = calldata;
1014 struct nfs_page *req = data->req;
1015 struct page *page = req->wb_page;
1017 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1018 req->wb_context->dentry->d_inode->i_sb->s_id,
1019 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1021 (long long)req_offset(req));
1023 if (nfs_writeback_done(task, data) != 0)
1026 if (task->tk_status < 0) {
1027 nfs_set_pageerror(page);
1028 req->wb_context->error = task->tk_status;
1029 dprintk(", error = %d\n", task->tk_status);
1033 if (nfs_write_need_commit(data)) {
1034 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
1036 spin_lock(req_lock);
1037 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
1038 /* Do nothing we need to resend the writes */
1039 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1040 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1041 dprintk(" defer commit\n");
1042 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1043 set_bit(PG_NEED_RESCHED, &req->wb_flags);
1044 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
1045 dprintk(" server reboot detected\n");
1047 spin_unlock(req_lock);
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 nfs_set_pageerror(page);
1091 req->wb_context->error = task->tk_status;
1092 dprintk(", error = %d\n", task->tk_status);
1093 goto remove_request;
1096 if (nfs_write_need_commit(data)) {
1097 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1098 nfs_mark_request_commit(req);
1099 nfs_end_page_writeback(page);
1100 dprintk(" marked for commit\n");
1105 nfs_end_page_writeback(page);
1106 nfs_inode_remove_request(req);
1108 nfs_clear_page_writeback(req);
1112 static const struct rpc_call_ops nfs_write_full_ops = {
1113 .rpc_call_done = nfs_writeback_done_full,
1114 .rpc_release = nfs_writedata_release,
1119 * This function is called when the WRITE call is complete.
1121 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1123 struct nfs_writeargs *argp = &data->args;
1124 struct nfs_writeres *resp = &data->res;
1127 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1128 task->tk_pid, task->tk_status);
1131 * ->write_done will attempt to use post-op attributes to detect
1132 * conflicting writes by other clients. A strict interpretation
1133 * of close-to-open would allow us to continue caching even if
1134 * another writer had changed the file, but some applications
1135 * depend on tighter cache coherency when writing.
1137 status = NFS_PROTO(data->inode)->write_done(task, data);
1140 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1142 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1143 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1144 /* We tried a write call, but the server did not
1145 * commit data to stable storage even though we
1147 * Note: There is a known bug in Tru64 < 5.0 in which
1148 * the server reports NFS_DATA_SYNC, but performs
1149 * NFS_FILE_SYNC. We therefore implement this checking
1150 * as a dprintk() in order to avoid filling syslog.
1152 static unsigned long complain;
1154 if (time_before(complain, jiffies)) {
1155 dprintk("NFS: faulty NFS server %s:"
1156 " (committed = %d) != (stable = %d)\n",
1157 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1158 resp->verf->committed, argp->stable);
1159 complain = jiffies + 300 * HZ;
1163 /* Is this a short write? */
1164 if (task->tk_status >= 0 && resp->count < argp->count) {
1165 static unsigned long complain;
1167 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1169 /* Has the server at least made some progress? */
1170 if (resp->count != 0) {
1171 /* Was this an NFSv2 write or an NFSv3 stable write? */
1172 if (resp->verf->committed != NFS_UNSTABLE) {
1173 /* Resend from where the server left off */
1174 argp->offset += resp->count;
1175 argp->pgbase += resp->count;
1176 argp->count -= resp->count;
1178 /* Resend as a stable write in order to avoid
1179 * headaches in the case of a server crash.
1181 argp->stable = NFS_FILE_SYNC;
1183 rpc_restart_call(task);
1186 if (time_before(complain, jiffies)) {
1188 "NFS: Server wrote zero bytes, expected %u.\n",
1190 complain = jiffies + 300 * HZ;
1192 /* Can't do anything about it except throw an error. */
1193 task->tk_status = -EIO;
1199 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1200 void nfs_commit_release(void *wdata)
1202 nfs_commit_free(wdata);
1206 * Set up the argument/result storage required for the RPC call.
1208 static void nfs_commit_rpcsetup(struct list_head *head,
1209 struct nfs_write_data *data,
1212 struct nfs_page *first;
1213 struct inode *inode;
1216 /* Set up the RPC argument and reply structs
1217 * NB: take care not to mess about with data->commit et al. */
1219 list_splice_init(head, &data->pages);
1220 first = nfs_list_entry(data->pages.next);
1221 inode = first->wb_context->dentry->d_inode;
1223 data->inode = inode;
1224 data->cred = first->wb_context->cred;
1226 data->args.fh = NFS_FH(data->inode);
1227 /* Note: we always request a commit of the entire inode */
1228 data->args.offset = 0;
1229 data->args.count = 0;
1230 data->res.count = 0;
1231 data->res.fattr = &data->fattr;
1232 data->res.verf = &data->verf;
1233 nfs_fattr_init(&data->fattr);
1235 /* Set up the initial task struct. */
1236 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1237 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
1238 NFS_PROTO(inode)->commit_setup(data, how);
1240 data->task.tk_priority = flush_task_priority(how);
1241 data->task.tk_cookie = (unsigned long)inode;
1243 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1247 * Commit dirty pages
1250 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1252 struct nfs_write_data *data;
1253 struct nfs_page *req;
1255 data = nfs_commit_alloc();
1260 /* Set up the argument struct */
1261 nfs_commit_rpcsetup(head, data, how);
1263 nfs_execute_write(data);
1266 while (!list_empty(head)) {
1267 req = nfs_list_entry(head->next);
1268 nfs_list_remove_request(req);
1269 nfs_mark_request_commit(req);
1270 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1271 nfs_clear_page_writeback(req);
1277 * COMMIT call returned
1279 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1281 struct nfs_write_data *data = calldata;
1282 struct nfs_page *req;
1284 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1285 task->tk_pid, task->tk_status);
1287 /* Call the NFS version-specific code */
1288 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1291 while (!list_empty(&data->pages)) {
1292 req = nfs_list_entry(data->pages.next);
1293 nfs_list_remove_request(req);
1294 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
1295 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1297 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1298 req->wb_context->dentry->d_inode->i_sb->s_id,
1299 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1301 (long long)req_offset(req));
1302 if (task->tk_status < 0) {
1303 req->wb_context->error = task->tk_status;
1304 nfs_inode_remove_request(req);
1305 dprintk(", error = %d\n", task->tk_status);
1309 /* Okay, COMMIT succeeded, apparently. Check the verifier
1310 * returned by the server against all stored verfs. */
1311 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1312 /* We have a match */
1313 nfs_inode_remove_request(req);
1317 /* We have a mismatch. Write the page again */
1318 dprintk(" mismatch\n");
1319 nfs_redirty_request(req);
1321 nfs_clear_page_writeback(req);
1325 static const struct rpc_call_ops nfs_commit_ops = {
1326 .rpc_call_done = nfs_commit_done,
1327 .rpc_release = nfs_commit_release,
1330 static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1336 static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
1338 struct nfs_inode *nfsi = NFS_I(mapping->host);
1342 spin_lock(&nfsi->req_lock);
1343 res = nfs_scan_dirty(mapping, wbc, &head);
1344 spin_unlock(&nfsi->req_lock);
1346 int error = nfs_flush_list(mapping->host, &head, res, how);
1353 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1354 int nfs_commit_inode(struct inode *inode, int how)
1356 struct nfs_inode *nfsi = NFS_I(inode);
1360 spin_lock(&nfsi->req_lock);
1361 res = nfs_scan_commit(inode, &head, 0, 0);
1362 spin_unlock(&nfsi->req_lock);
1364 int error = nfs_commit_list(inode, &head, how);
1372 long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
1374 struct inode *inode = mapping->host;
1375 struct nfs_inode *nfsi = NFS_I(inode);
1376 unsigned long idx_start, idx_end;
1377 unsigned int npages = 0;
1379 int nocommit = how & FLUSH_NOCOMMIT;
1383 if (wbc->range_cyclic)
1386 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1387 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1388 if (idx_end > idx_start) {
1389 unsigned long l_npages = 1 + idx_end - idx_start;
1391 if (sizeof(npages) != sizeof(l_npages) &&
1392 (unsigned long)npages != l_npages)
1396 how &= ~FLUSH_NOCOMMIT;
1397 spin_lock(&nfsi->req_lock);
1399 wbc->pages_skipped = 0;
1400 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1403 pages = nfs_scan_dirty(mapping, wbc, &head);
1405 spin_unlock(&nfsi->req_lock);
1406 if (how & FLUSH_INVALIDATE) {
1407 nfs_cancel_dirty_list(&head);
1410 ret = nfs_flush_list(inode, &head, pages, how);
1411 spin_lock(&nfsi->req_lock);
1414 if (wbc->pages_skipped != 0)
1418 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1420 if (wbc->pages_skipped != 0)
1424 if (how & FLUSH_INVALIDATE) {
1425 spin_unlock(&nfsi->req_lock);
1426 nfs_cancel_commit_list(&head);
1428 spin_lock(&nfsi->req_lock);
1431 pages += nfs_scan_commit(inode, &head, 0, 0);
1432 spin_unlock(&nfsi->req_lock);
1433 ret = nfs_commit_list(inode, &head, how);
1434 spin_lock(&nfsi->req_lock);
1436 spin_unlock(&nfsi->req_lock);
1441 * flush the inode to disk.
1443 int nfs_wb_all(struct inode *inode)
1445 struct address_space *mapping = inode->i_mapping;
1446 struct writeback_control wbc = {
1447 .bdi = mapping->backing_dev_info,
1448 .sync_mode = WB_SYNC_ALL,
1449 .nr_to_write = LONG_MAX,
1450 .for_writepages = 1,
1455 ret = generic_writepages(mapping, &wbc);
1458 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1462 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1466 int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1468 struct writeback_control wbc = {
1469 .bdi = mapping->backing_dev_info,
1470 .sync_mode = WB_SYNC_ALL,
1471 .nr_to_write = LONG_MAX,
1472 .range_start = range_start,
1473 .range_end = range_end,
1474 .for_writepages = 1,
1478 if (!(how & FLUSH_NOWRITEPAGE)) {
1479 ret = generic_writepages(mapping, &wbc);
1483 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1487 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1491 int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
1493 loff_t range_start = page_offset(page);
1494 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1495 struct writeback_control wbc = {
1496 .bdi = page->mapping->backing_dev_info,
1497 .sync_mode = WB_SYNC_ALL,
1498 .nr_to_write = LONG_MAX,
1499 .range_start = range_start,
1500 .range_end = range_end,
1504 BUG_ON(!PageLocked(page));
1505 if (!(how & FLUSH_NOWRITEPAGE) && clear_page_dirty_for_io(page)) {
1506 ret = nfs_writepage_locked(page, &wbc);
1510 if (!PagePrivate(page))
1512 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1516 __mark_inode_dirty(inode, I_DIRTY_PAGES);
1521 * Write back all requests on one page - we do this before reading it.
1523 int nfs_wb_page(struct inode *inode, struct page* page)
1525 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
1528 int nfs_set_page_dirty(struct page *page)
1530 struct nfs_page *req;
1532 req = nfs_page_find_request(page);
1534 /* Mark any existing write requests for flushing */
1535 set_bit(PG_NEED_FLUSH, &req->wb_flags);
1536 nfs_release_request(req);
1538 return __set_page_dirty_nobuffers(page);
1542 int __init nfs_init_writepagecache(void)
1544 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1545 sizeof(struct nfs_write_data),
1546 0, SLAB_HWCACHE_ALIGN,
1548 if (nfs_wdata_cachep == NULL)
1551 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1553 if (nfs_wdata_mempool == NULL)
1556 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1558 if (nfs_commit_mempool == NULL)
1562 * NFS congestion size, scale with available memory.
1574 * This allows larger machines to have larger/more transfers.
1575 * Limit the default to 256M
1577 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1578 if (nfs_congestion_kb > 256*1024)
1579 nfs_congestion_kb = 256*1024;
1584 void nfs_destroy_writepagecache(void)
1586 mempool_destroy(nfs_commit_mempool);
1587 mempool_destroy(nfs_wdata_mempool);
1588 kmem_cache_destroy(nfs_wdata_cachep);