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);
391 if (PageDirty(req->wb_page))
392 set_bit(PG_NEED_FLUSH, &req->wb_flags);
394 atomic_inc(&req->wb_count);
399 * Remove a write request from an inode
401 static void nfs_inode_remove_request(struct nfs_page *req)
403 struct inode *inode = req->wb_context->dentry->d_inode;
404 struct nfs_inode *nfsi = NFS_I(inode);
406 BUG_ON (!NFS_WBACK_BUSY(req));
408 spin_lock(&nfsi->req_lock);
409 set_page_private(req->wb_page, 0);
410 ClearPagePrivate(req->wb_page);
411 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
412 if (test_and_clear_bit(PG_NEED_FLUSH, &req->wb_flags))
413 __set_page_dirty_nobuffers(req->wb_page);
416 spin_unlock(&nfsi->req_lock);
417 nfs_end_data_update(inode);
420 spin_unlock(&nfsi->req_lock);
421 nfs_clear_request(req);
422 nfs_release_request(req);
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;
439 if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
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 set_bit(PG_NEED_COMMIT, &(req)->wb_flags);
458 spin_unlock(&nfsi->req_lock);
459 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
460 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
464 int nfs_write_need_commit(struct nfs_write_data *data)
466 return data->verf.committed != NFS_FILE_SYNC;
470 int nfs_reschedule_unstable_write(struct nfs_page *req)
472 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
473 nfs_mark_request_commit(req);
476 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
477 nfs_redirty_request(req);
484 nfs_mark_request_commit(struct nfs_page *req)
489 int nfs_write_need_commit(struct nfs_write_data *data)
495 int nfs_reschedule_unstable_write(struct nfs_page *req)
502 * Wait for a request to complete.
504 * Interruptible by signals only if mounted with intr flag.
506 static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
508 struct nfs_inode *nfsi = NFS_I(inode);
509 struct nfs_page *req;
510 unsigned long idx_end, next;
511 unsigned int res = 0;
517 idx_end = idx_start + npages - 1;
520 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
521 if (req->wb_index > idx_end)
524 next = req->wb_index + 1;
525 BUG_ON(!NFS_WBACK_BUSY(req));
527 atomic_inc(&req->wb_count);
528 spin_unlock(&nfsi->req_lock);
529 error = nfs_wait_on_request(req);
530 nfs_release_request(req);
531 spin_lock(&nfsi->req_lock);
539 static void nfs_cancel_dirty_list(struct list_head *head)
541 struct nfs_page *req;
542 while(!list_empty(head)) {
543 req = nfs_list_entry(head->next);
544 nfs_list_remove_request(req);
545 nfs_end_page_writeback(req->wb_page);
546 nfs_inode_remove_request(req);
547 nfs_clear_page_writeback(req);
551 static void nfs_cancel_commit_list(struct list_head *head)
553 struct nfs_page *req;
555 while(!list_empty(head)) {
556 req = nfs_list_entry(head->next);
557 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
558 nfs_list_remove_request(req);
559 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
560 nfs_inode_remove_request(req);
561 nfs_unlock_request(req);
565 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
567 * nfs_scan_commit - Scan an inode for commit requests
568 * @inode: NFS inode to scan
569 * @dst: destination list
570 * @idx_start: lower bound of page->index to scan.
571 * @npages: idx_start + npages sets the upper bound to scan.
573 * Moves requests from the inode's 'commit' request list.
574 * The requests are *not* checked to ensure that they form a contiguous set.
577 nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
579 struct nfs_inode *nfsi = NFS_I(inode);
582 if (nfsi->ncommit != 0) {
583 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
584 nfsi->ncommit -= res;
585 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
586 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
591 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
598 * Try to update any existing write request, or create one if there is none.
599 * In order to match, the request's credentials must match those of
600 * the calling process.
602 * Note: Should always be called with the Page Lock held!
604 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
605 struct page *page, unsigned int offset, unsigned int bytes)
607 struct address_space *mapping = page->mapping;
608 struct inode *inode = mapping->host;
609 struct nfs_inode *nfsi = NFS_I(inode);
610 struct nfs_page *req, *new = NULL;
611 unsigned long rqend, end;
613 end = offset + bytes;
616 /* Loop over all inode entries and see if we find
617 * A request for the page we wish to update
619 spin_lock(&nfsi->req_lock);
620 req = nfs_page_find_request_locked(page);
622 if (!nfs_lock_request_dontget(req)) {
625 spin_unlock(&nfsi->req_lock);
626 error = nfs_wait_on_request(req);
627 nfs_release_request(req);
630 nfs_release_request(new);
631 return ERR_PTR(error);
635 spin_unlock(&nfsi->req_lock);
637 nfs_release_request(new);
643 nfs_lock_request_dontget(new);
644 error = nfs_inode_add_request(inode, new);
646 spin_unlock(&nfsi->req_lock);
647 nfs_unlock_request(new);
648 return ERR_PTR(error);
650 spin_unlock(&nfsi->req_lock);
653 spin_unlock(&nfsi->req_lock);
655 new = nfs_create_request(ctx, inode, page, offset, bytes);
660 /* We have a request for our page.
661 * If the creds don't match, or the
662 * page addresses don't match,
663 * tell the caller to wait on the conflicting
666 rqend = req->wb_offset + req->wb_bytes;
667 if (req->wb_context != ctx
668 || req->wb_page != page
669 || !nfs_dirty_request(req)
670 || offset > rqend || end < req->wb_offset) {
671 nfs_unlock_request(req);
672 return ERR_PTR(-EBUSY);
675 /* Okay, the request matches. Update the region */
676 if (offset < req->wb_offset) {
677 req->wb_offset = offset;
678 req->wb_pgbase = offset;
679 req->wb_bytes = rqend - req->wb_offset;
683 req->wb_bytes = end - req->wb_offset;
688 int nfs_flush_incompatible(struct file *file, struct page *page)
690 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
691 struct nfs_page *req;
692 int do_flush, status;
694 * Look for a request corresponding to this page. If there
695 * is one, and it belongs to another file, we flush it out
696 * before we try to copy anything into the page. Do this
697 * due to the lack of an ACCESS-type call in NFSv2.
698 * Also do the same if we find a request from an existing
702 req = nfs_page_find_request(page);
705 do_flush = req->wb_page != page || req->wb_context != ctx
706 || !nfs_dirty_request(req);
707 nfs_release_request(req);
710 status = nfs_wb_page(page->mapping->host, page);
711 } while (status == 0);
716 * Update and possibly write a cached page of an NFS file.
718 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
719 * things with a page scheduled for an RPC call (e.g. invalidate it).
721 int nfs_updatepage(struct file *file, struct page *page,
722 unsigned int offset, unsigned int count)
724 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
725 struct inode *inode = page->mapping->host;
728 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
730 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
731 file->f_path.dentry->d_parent->d_name.name,
732 file->f_path.dentry->d_name.name, count,
733 (long long)(page_offset(page) +offset));
735 /* If we're not using byte range locks, and we know the page
736 * is entirely in cache, it may be more efficient to avoid
737 * fragmenting write requests.
739 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
740 count = max(count + offset, nfs_page_length(page));
744 status = nfs_writepage_setup(ctx, page, offset, count);
745 __set_page_dirty_nobuffers(page);
747 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
748 status, (long long)i_size_read(inode));
750 nfs_set_pageerror(page);
754 static void nfs_writepage_release(struct nfs_page *req)
757 if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req)) {
758 nfs_end_page_writeback(req->wb_page);
759 nfs_inode_remove_request(req);
761 nfs_end_page_writeback(req->wb_page);
762 nfs_clear_page_writeback(req);
765 static inline int flush_task_priority(int how)
767 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
769 return RPC_PRIORITY_HIGH;
771 return RPC_PRIORITY_LOW;
773 return RPC_PRIORITY_NORMAL;
777 * Set up the argument/result storage required for the RPC call.
779 static void nfs_write_rpcsetup(struct nfs_page *req,
780 struct nfs_write_data *data,
781 const struct rpc_call_ops *call_ops,
782 unsigned int count, unsigned int offset,
788 /* Set up the RPC argument and reply structs
789 * NB: take care not to mess about with data->commit et al. */
792 data->inode = inode = req->wb_context->dentry->d_inode;
793 data->cred = req->wb_context->cred;
795 data->args.fh = NFS_FH(inode);
796 data->args.offset = req_offset(req) + offset;
797 data->args.pgbase = req->wb_pgbase + offset;
798 data->args.pages = data->pagevec;
799 data->args.count = count;
800 data->args.context = req->wb_context;
802 data->res.fattr = &data->fattr;
803 data->res.count = count;
804 data->res.verf = &data->verf;
805 nfs_fattr_init(&data->fattr);
807 /* Set up the initial task struct. */
808 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
809 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
810 NFS_PROTO(inode)->write_setup(data, how);
812 data->task.tk_priority = flush_task_priority(how);
813 data->task.tk_cookie = (unsigned long)inode;
815 dprintk("NFS: %5u initiated write call "
816 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
819 (long long)NFS_FILEID(inode),
821 (unsigned long long)data->args.offset);
824 static void nfs_execute_write(struct nfs_write_data *data)
826 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
829 rpc_clnt_sigmask(clnt, &oldset);
830 rpc_execute(&data->task);
831 rpc_clnt_sigunmask(clnt, &oldset);
835 * Generate multiple small requests to write out a single
836 * contiguous dirty area on one page.
838 static int nfs_flush_multi(struct inode *inode, struct list_head *head, size_t count, int how)
840 struct nfs_page *req = nfs_list_entry(head->next);
841 struct page *page = req->wb_page;
842 struct nfs_write_data *data;
843 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
848 nfs_list_remove_request(req);
852 size_t len = min(nbytes, wsize);
854 data = nfs_writedata_alloc(len);
857 list_add(&data->pages, &list);
860 } while (nbytes != 0);
861 atomic_set(&req->wb_complete, requests);
863 ClearPageError(page);
867 data = list_entry(list.next, struct nfs_write_data, pages);
868 list_del_init(&data->pages);
870 data->pagevec[0] = page;
874 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
878 nfs_execute_write(data);
879 } while (nbytes != 0);
884 while (!list_empty(&list)) {
885 data = list_entry(list.next, struct nfs_write_data, pages);
886 list_del(&data->pages);
887 nfs_writedata_release(data);
889 nfs_redirty_request(req);
890 nfs_end_page_writeback(req->wb_page);
891 nfs_clear_page_writeback(req);
896 * Create an RPC task for the given write request and kick it.
897 * The page must have been locked by the caller.
899 * It may happen that the page we're passed is not marked dirty.
900 * This is the case if nfs_updatepage detects a conflicting request
901 * that has been written but not committed.
903 static int nfs_flush_one(struct inode *inode, struct list_head *head, size_t count, int how)
905 struct nfs_page *req;
907 struct nfs_write_data *data;
909 data = nfs_writedata_alloc(count);
913 pages = data->pagevec;
914 while (!list_empty(head)) {
915 req = nfs_list_entry(head->next);
916 nfs_list_remove_request(req);
917 nfs_list_add_request(req, &data->pages);
918 ClearPageError(req->wb_page);
919 *pages++ = req->wb_page;
921 req = nfs_list_entry(data->pages.next);
923 /* Set up the argument struct */
924 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
926 nfs_execute_write(data);
929 while (!list_empty(head)) {
930 struct nfs_page *req = nfs_list_entry(head->next);
931 nfs_list_remove_request(req);
932 nfs_redirty_request(req);
933 nfs_end_page_writeback(req->wb_page);
934 nfs_clear_page_writeback(req);
939 static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
941 struct nfs_pageio_descriptor desc;
942 int wpages = NFS_SERVER(inode)->wpages;
943 int wsize = NFS_SERVER(inode)->wsize;
945 /* For single writes, FLUSH_STABLE is more efficient */
946 if (npages <= wpages && npages == NFS_I(inode)->npages
947 && nfs_list_entry(head->next)->wb_bytes <= wsize)
950 if (wsize < PAGE_CACHE_SIZE)
951 nfs_pageio_init(&desc, inode, nfs_flush_multi, wsize, how);
953 nfs_pageio_init(&desc, inode, nfs_flush_one, wsize, how);
954 nfs_pageio_add_list(&desc, head);
955 nfs_pageio_complete(&desc);
956 if (desc.pg_error == 0)
958 while (!list_empty(head)) {
959 struct nfs_page *req = nfs_list_entry(head->next);
960 nfs_list_remove_request(req);
961 nfs_redirty_request(req);
962 nfs_end_page_writeback(req->wb_page);
963 nfs_clear_page_writeback(req);
965 return desc.pg_error;
969 * Handle a write reply that flushed part of a page.
971 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
973 struct nfs_write_data *data = calldata;
974 struct nfs_page *req = data->req;
975 struct page *page = req->wb_page;
977 dprintk("NFS: write (%s/%Ld %d@%Ld)",
978 req->wb_context->dentry->d_inode->i_sb->s_id,
979 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
981 (long long)req_offset(req));
983 if (nfs_writeback_done(task, data) != 0)
986 if (task->tk_status < 0) {
987 nfs_set_pageerror(page);
988 req->wb_context->error = task->tk_status;
989 dprintk(", error = %d\n", task->tk_status);
993 if (nfs_write_need_commit(data)) {
994 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
997 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
998 /* Do nothing we need to resend the writes */
999 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1000 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1001 dprintk(" defer commit\n");
1002 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1003 set_bit(PG_NEED_RESCHED, &req->wb_flags);
1004 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
1005 dprintk(" server reboot detected\n");
1007 spin_unlock(req_lock);
1012 if (atomic_dec_and_test(&req->wb_complete))
1013 nfs_writepage_release(req);
1016 static const struct rpc_call_ops nfs_write_partial_ops = {
1017 .rpc_call_done = nfs_writeback_done_partial,
1018 .rpc_release = nfs_writedata_release,
1022 * Handle a write reply that flushes a whole page.
1024 * FIXME: There is an inherent race with invalidate_inode_pages and
1025 * writebacks since the page->count is kept > 1 for as long
1026 * as the page has a write request pending.
1028 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1030 struct nfs_write_data *data = calldata;
1031 struct nfs_page *req;
1034 if (nfs_writeback_done(task, data) != 0)
1037 /* Update attributes as result of writeback. */
1038 while (!list_empty(&data->pages)) {
1039 req = nfs_list_entry(data->pages.next);
1040 nfs_list_remove_request(req);
1041 page = req->wb_page;
1043 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1044 req->wb_context->dentry->d_inode->i_sb->s_id,
1045 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1047 (long long)req_offset(req));
1049 if (task->tk_status < 0) {
1050 nfs_set_pageerror(page);
1051 req->wb_context->error = task->tk_status;
1052 dprintk(", error = %d\n", task->tk_status);
1053 goto remove_request;
1056 if (nfs_write_need_commit(data)) {
1057 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1058 nfs_mark_request_commit(req);
1059 nfs_end_page_writeback(page);
1060 dprintk(" marked for commit\n");
1065 nfs_end_page_writeback(page);
1066 nfs_inode_remove_request(req);
1068 nfs_clear_page_writeback(req);
1072 static const struct rpc_call_ops nfs_write_full_ops = {
1073 .rpc_call_done = nfs_writeback_done_full,
1074 .rpc_release = nfs_writedata_release,
1079 * This function is called when the WRITE call is complete.
1081 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1083 struct nfs_writeargs *argp = &data->args;
1084 struct nfs_writeres *resp = &data->res;
1087 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1088 task->tk_pid, task->tk_status);
1091 * ->write_done will attempt to use post-op attributes to detect
1092 * conflicting writes by other clients. A strict interpretation
1093 * of close-to-open would allow us to continue caching even if
1094 * another writer had changed the file, but some applications
1095 * depend on tighter cache coherency when writing.
1097 status = NFS_PROTO(data->inode)->write_done(task, data);
1100 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1102 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1103 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1104 /* We tried a write call, but the server did not
1105 * commit data to stable storage even though we
1107 * Note: There is a known bug in Tru64 < 5.0 in which
1108 * the server reports NFS_DATA_SYNC, but performs
1109 * NFS_FILE_SYNC. We therefore implement this checking
1110 * as a dprintk() in order to avoid filling syslog.
1112 static unsigned long complain;
1114 if (time_before(complain, jiffies)) {
1115 dprintk("NFS: faulty NFS server %s:"
1116 " (committed = %d) != (stable = %d)\n",
1117 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1118 resp->verf->committed, argp->stable);
1119 complain = jiffies + 300 * HZ;
1123 /* Is this a short write? */
1124 if (task->tk_status >= 0 && resp->count < argp->count) {
1125 static unsigned long complain;
1127 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1129 /* Has the server at least made some progress? */
1130 if (resp->count != 0) {
1131 /* Was this an NFSv2 write or an NFSv3 stable write? */
1132 if (resp->verf->committed != NFS_UNSTABLE) {
1133 /* Resend from where the server left off */
1134 argp->offset += resp->count;
1135 argp->pgbase += resp->count;
1136 argp->count -= resp->count;
1138 /* Resend as a stable write in order to avoid
1139 * headaches in the case of a server crash.
1141 argp->stable = NFS_FILE_SYNC;
1143 rpc_restart_call(task);
1146 if (time_before(complain, jiffies)) {
1148 "NFS: Server wrote zero bytes, expected %u.\n",
1150 complain = jiffies + 300 * HZ;
1152 /* Can't do anything about it except throw an error. */
1153 task->tk_status = -EIO;
1159 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1160 void nfs_commit_release(void *wdata)
1162 nfs_commit_free(wdata);
1166 * Set up the argument/result storage required for the RPC call.
1168 static void nfs_commit_rpcsetup(struct list_head *head,
1169 struct nfs_write_data *data,
1172 struct nfs_page *first;
1173 struct inode *inode;
1176 /* Set up the RPC argument and reply structs
1177 * NB: take care not to mess about with data->commit et al. */
1179 list_splice_init(head, &data->pages);
1180 first = nfs_list_entry(data->pages.next);
1181 inode = first->wb_context->dentry->d_inode;
1183 data->inode = inode;
1184 data->cred = first->wb_context->cred;
1186 data->args.fh = NFS_FH(data->inode);
1187 /* Note: we always request a commit of the entire inode */
1188 data->args.offset = 0;
1189 data->args.count = 0;
1190 data->res.count = 0;
1191 data->res.fattr = &data->fattr;
1192 data->res.verf = &data->verf;
1193 nfs_fattr_init(&data->fattr);
1195 /* Set up the initial task struct. */
1196 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1197 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
1198 NFS_PROTO(inode)->commit_setup(data, how);
1200 data->task.tk_priority = flush_task_priority(how);
1201 data->task.tk_cookie = (unsigned long)inode;
1203 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1207 * Commit dirty pages
1210 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1212 struct nfs_write_data *data;
1213 struct nfs_page *req;
1215 data = nfs_commit_alloc();
1220 /* Set up the argument struct */
1221 nfs_commit_rpcsetup(head, data, how);
1223 nfs_execute_write(data);
1226 while (!list_empty(head)) {
1227 req = nfs_list_entry(head->next);
1228 nfs_list_remove_request(req);
1229 nfs_mark_request_commit(req);
1230 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1231 nfs_clear_page_writeback(req);
1237 * COMMIT call returned
1239 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1241 struct nfs_write_data *data = calldata;
1242 struct nfs_page *req;
1244 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1245 task->tk_pid, task->tk_status);
1247 /* Call the NFS version-specific code */
1248 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1251 while (!list_empty(&data->pages)) {
1252 req = nfs_list_entry(data->pages.next);
1253 nfs_list_remove_request(req);
1254 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
1255 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1257 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1258 req->wb_context->dentry->d_inode->i_sb->s_id,
1259 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1261 (long long)req_offset(req));
1262 if (task->tk_status < 0) {
1263 req->wb_context->error = task->tk_status;
1264 nfs_inode_remove_request(req);
1265 dprintk(", error = %d\n", task->tk_status);
1269 /* Okay, COMMIT succeeded, apparently. Check the verifier
1270 * returned by the server against all stored verfs. */
1271 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1272 /* We have a match */
1273 nfs_inode_remove_request(req);
1277 /* We have a mismatch. Write the page again */
1278 dprintk(" mismatch\n");
1279 nfs_redirty_request(req);
1281 nfs_clear_page_writeback(req);
1285 static const struct rpc_call_ops nfs_commit_ops = {
1286 .rpc_call_done = nfs_commit_done,
1287 .rpc_release = nfs_commit_release,
1290 static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1296 static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
1298 struct nfs_inode *nfsi = NFS_I(mapping->host);
1302 spin_lock(&nfsi->req_lock);
1303 res = nfs_scan_dirty(mapping, wbc, &head);
1304 spin_unlock(&nfsi->req_lock);
1306 int error = nfs_flush_list(mapping->host, &head, res, how);
1313 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1314 int nfs_commit_inode(struct inode *inode, int how)
1316 struct nfs_inode *nfsi = NFS_I(inode);
1320 spin_lock(&nfsi->req_lock);
1321 res = nfs_scan_commit(inode, &head, 0, 0);
1322 spin_unlock(&nfsi->req_lock);
1324 int error = nfs_commit_list(inode, &head, how);
1332 long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
1334 struct inode *inode = mapping->host;
1335 struct nfs_inode *nfsi = NFS_I(inode);
1336 unsigned long idx_start, idx_end;
1337 unsigned int npages = 0;
1339 int nocommit = how & FLUSH_NOCOMMIT;
1343 if (wbc->range_cyclic)
1346 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1347 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1348 if (idx_end > idx_start) {
1349 unsigned long l_npages = 1 + idx_end - idx_start;
1351 if (sizeof(npages) != sizeof(l_npages) &&
1352 (unsigned long)npages != l_npages)
1356 how &= ~FLUSH_NOCOMMIT;
1357 spin_lock(&nfsi->req_lock);
1359 wbc->pages_skipped = 0;
1360 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1363 pages = nfs_scan_dirty(mapping, wbc, &head);
1365 spin_unlock(&nfsi->req_lock);
1366 if (how & FLUSH_INVALIDATE) {
1367 nfs_cancel_dirty_list(&head);
1370 ret = nfs_flush_list(inode, &head, pages, how);
1371 spin_lock(&nfsi->req_lock);
1374 if (wbc->pages_skipped != 0)
1378 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1380 if (wbc->pages_skipped != 0)
1384 if (how & FLUSH_INVALIDATE) {
1385 spin_unlock(&nfsi->req_lock);
1386 nfs_cancel_commit_list(&head);
1388 spin_lock(&nfsi->req_lock);
1391 pages += nfs_scan_commit(inode, &head, 0, 0);
1392 spin_unlock(&nfsi->req_lock);
1393 ret = nfs_commit_list(inode, &head, how);
1394 spin_lock(&nfsi->req_lock);
1396 spin_unlock(&nfsi->req_lock);
1401 * flush the inode to disk.
1403 int nfs_wb_all(struct inode *inode)
1405 struct address_space *mapping = inode->i_mapping;
1406 struct writeback_control wbc = {
1407 .bdi = mapping->backing_dev_info,
1408 .sync_mode = WB_SYNC_ALL,
1409 .nr_to_write = LONG_MAX,
1410 .for_writepages = 1,
1415 ret = generic_writepages(mapping, &wbc);
1418 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1422 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1426 int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1428 struct writeback_control wbc = {
1429 .bdi = mapping->backing_dev_info,
1430 .sync_mode = WB_SYNC_ALL,
1431 .nr_to_write = LONG_MAX,
1432 .range_start = range_start,
1433 .range_end = range_end,
1434 .for_writepages = 1,
1438 if (!(how & FLUSH_NOWRITEPAGE)) {
1439 ret = generic_writepages(mapping, &wbc);
1443 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1447 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1451 int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
1453 loff_t range_start = page_offset(page);
1454 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1455 struct writeback_control wbc = {
1456 .bdi = page->mapping->backing_dev_info,
1457 .sync_mode = WB_SYNC_ALL,
1458 .nr_to_write = LONG_MAX,
1459 .range_start = range_start,
1460 .range_end = range_end,
1464 BUG_ON(!PageLocked(page));
1465 if (!(how & FLUSH_NOWRITEPAGE) && clear_page_dirty_for_io(page)) {
1466 ret = nfs_writepage_locked(page, &wbc);
1470 if (!PagePrivate(page))
1472 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1476 __mark_inode_dirty(inode, I_DIRTY_PAGES);
1481 * Write back all requests on one page - we do this before reading it.
1483 int nfs_wb_page(struct inode *inode, struct page* page)
1485 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
1488 int nfs_set_page_dirty(struct page *page)
1490 struct address_space *mapping = page->mapping;
1491 struct inode *inode;
1492 spinlock_t *req_lock;
1493 struct nfs_page *req;
1498 inode = mapping->host;
1501 req_lock = &NFS_I(inode)->req_lock;
1502 spin_lock(req_lock);
1503 req = nfs_page_find_request_locked(page);
1505 /* Mark any existing write requests for flushing */
1506 ret = !test_and_set_bit(PG_NEED_FLUSH, &req->wb_flags);
1507 spin_unlock(req_lock);
1508 nfs_release_request(req);
1511 ret = __set_page_dirty_nobuffers(page);
1512 spin_unlock(req_lock);
1515 return !TestSetPageDirty(page);
1519 int __init nfs_init_writepagecache(void)
1521 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1522 sizeof(struct nfs_write_data),
1523 0, SLAB_HWCACHE_ALIGN,
1525 if (nfs_wdata_cachep == NULL)
1528 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1530 if (nfs_wdata_mempool == NULL)
1533 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1535 if (nfs_commit_mempool == NULL)
1539 * NFS congestion size, scale with available memory.
1551 * This allows larger machines to have larger/more transfers.
1552 * Limit the default to 256M
1554 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1555 if (nfs_congestion_kb > 256*1024)
1556 nfs_congestion_kb = 256*1024;
1561 void nfs_destroy_writepagecache(void)
1563 mempool_destroy(nfs_commit_mempool);
1564 mempool_destroy(nfs_wdata_mempool);
1565 kmem_cache_destroy(nfs_wdata_cachep);