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>
25 #include "delegation.h"
29 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
31 #define MIN_POOL_WRITE (32)
32 #define MIN_POOL_COMMIT (4)
35 * Local function declarations
37 static struct nfs_page * nfs_update_request(struct nfs_open_context*,
39 unsigned int, unsigned int);
40 static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc,
41 struct inode *inode, int ioflags);
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 static 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(unsigned int pagecount)
76 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
79 memset(p, 0, sizeof(*p));
80 INIT_LIST_HEAD(&p->pages);
81 p->npages = pagecount;
82 if (pagecount <= ARRAY_SIZE(p->page_array))
83 p->pagevec = p->page_array;
85 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
87 mempool_free(p, nfs_wdata_mempool);
95 static void nfs_writedata_rcu_free(struct rcu_head *head)
97 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
98 if (p && (p->pagevec != &p->page_array[0]))
100 mempool_free(p, nfs_wdata_mempool);
103 static void nfs_writedata_free(struct nfs_write_data *wdata)
105 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
108 void nfs_writedata_release(void *data)
110 struct nfs_write_data *wdata = data;
112 put_nfs_open_context(wdata->args.context);
113 nfs_writedata_free(wdata);
116 static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
120 set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
123 static struct nfs_page *nfs_page_find_request_locked(struct page *page)
125 struct nfs_page *req = NULL;
127 if (PagePrivate(page)) {
128 req = (struct nfs_page *)page_private(page);
130 kref_get(&req->wb_kref);
135 static struct nfs_page *nfs_page_find_request(struct page *page)
137 struct inode *inode = page->mapping->host;
138 struct nfs_page *req = NULL;
140 spin_lock(&inode->i_lock);
141 req = nfs_page_find_request_locked(page);
142 spin_unlock(&inode->i_lock);
146 /* Adjust the file length if we're writing beyond the end */
147 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
149 struct inode *inode = page->mapping->host;
150 loff_t end, i_size = i_size_read(inode);
151 pgoff_t end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
153 if (i_size > 0 && page->index < end_index)
155 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
158 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
159 i_size_write(inode, end);
162 /* A writeback failed: mark the page as bad, and invalidate the page cache */
163 static void nfs_set_pageerror(struct page *page)
166 nfs_zap_mapping(page->mapping->host, page->mapping);
169 /* We can set the PG_uptodate flag if we see that a write request
170 * covers the full page.
172 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
174 if (PageUptodate(page))
178 if (count != nfs_page_length(page))
180 SetPageUptodate(page);
183 static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
184 unsigned int offset, unsigned int count)
186 struct nfs_page *req;
190 req = nfs_update_request(ctx, page, offset, count);
196 ret = nfs_wb_page(page->mapping->host, page);
200 /* Update file length */
201 nfs_grow_file(page, offset, count);
202 nfs_clear_page_tag_locked(req);
206 static int wb_priority(struct writeback_control *wbc)
208 if (wbc->for_reclaim)
209 return FLUSH_HIGHPRI | FLUSH_STABLE;
210 if (wbc->for_kupdate)
216 * NFS congestion control
219 int nfs_congestion_kb;
221 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
222 #define NFS_CONGESTION_OFF_THRESH \
223 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
225 static int nfs_set_page_writeback(struct page *page)
227 int ret = test_set_page_writeback(page);
230 struct inode *inode = page->mapping->host;
231 struct nfs_server *nfss = NFS_SERVER(inode);
233 if (atomic_long_inc_return(&nfss->writeback) >
234 NFS_CONGESTION_ON_THRESH)
235 set_bdi_congested(&nfss->backing_dev_info, WRITE);
240 static void nfs_end_page_writeback(struct page *page)
242 struct inode *inode = page->mapping->host;
243 struct nfs_server *nfss = NFS_SERVER(inode);
245 end_page_writeback(page);
246 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
247 clear_bdi_congested(&nfss->backing_dev_info, WRITE);
251 * Find an associated nfs write request, and prepare to flush it out
252 * May return an error if the user signalled nfs_wait_on_request().
254 static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
257 struct inode *inode = page->mapping->host;
258 struct nfs_page *req;
261 spin_lock(&inode->i_lock);
263 req = nfs_page_find_request_locked(page);
265 spin_unlock(&inode->i_lock);
268 if (nfs_set_page_tag_locked(req))
270 /* Note: If we hold the page lock, as is the case in nfs_writepage,
271 * then the call to nfs_set_page_tag_locked() 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(&inode->i_lock);
276 ret = nfs_wait_on_request(req);
277 nfs_release_request(req);
280 spin_lock(&inode->i_lock);
282 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
283 /* This request is marked for commit */
284 spin_unlock(&inode->i_lock);
285 nfs_clear_page_tag_locked(req);
286 nfs_pageio_complete(pgio);
289 if (nfs_set_page_writeback(page) != 0) {
290 spin_unlock(&inode->i_lock);
293 spin_unlock(&inode->i_lock);
294 nfs_pageio_add_request(pgio, req);
298 static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
300 struct inode *inode = page->mapping->host;
302 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
303 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
305 nfs_pageio_cond_complete(pgio, page->index);
306 return nfs_page_async_flush(pgio, page);
310 * Write an mmapped page to the server.
312 static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
314 struct nfs_pageio_descriptor pgio;
317 nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
318 err = nfs_do_writepage(page, wbc, &pgio);
319 nfs_pageio_complete(&pgio);
322 if (pgio.pg_error < 0)
323 return pgio.pg_error;
327 int nfs_writepage(struct page *page, struct writeback_control *wbc)
331 ret = nfs_writepage_locked(page, wbc);
336 static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
340 ret = nfs_do_writepage(page, wbc, data);
345 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
347 struct inode *inode = mapping->host;
348 struct nfs_pageio_descriptor pgio;
351 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
353 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
354 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
355 nfs_pageio_complete(&pgio);
358 if (pgio.pg_error < 0)
359 return pgio.pg_error;
364 * Insert a write request into an inode
366 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
368 struct nfs_inode *nfsi = NFS_I(inode);
371 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
372 BUG_ON(error == -EEXIST);
377 if (nfs_have_delegation(inode, FMODE_WRITE))
380 SetPagePrivate(req->wb_page);
381 set_page_private(req->wb_page, (unsigned long)req);
383 kref_get(&req->wb_kref);
384 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_LOCKED);
389 * Remove a write request from an inode
391 static void nfs_inode_remove_request(struct nfs_page *req)
393 struct inode *inode = req->wb_context->path.dentry->d_inode;
394 struct nfs_inode *nfsi = NFS_I(inode);
396 BUG_ON (!NFS_WBACK_BUSY(req));
398 spin_lock(&inode->i_lock);
399 set_page_private(req->wb_page, 0);
400 ClearPagePrivate(req->wb_page);
401 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
404 spin_unlock(&inode->i_lock);
407 spin_unlock(&inode->i_lock);
408 nfs_clear_request(req);
409 nfs_release_request(req);
413 nfs_redirty_request(struct nfs_page *req)
415 __set_page_dirty_nobuffers(req->wb_page);
419 * Check if a request is dirty
422 nfs_dirty_request(struct nfs_page *req)
424 struct page *page = req->wb_page;
426 if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
428 return !PageWriteback(req->wb_page);
431 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
433 * Add a request to the inode's commit list.
436 nfs_mark_request_commit(struct nfs_page *req)
438 struct inode *inode = req->wb_context->path.dentry->d_inode;
439 struct nfs_inode *nfsi = NFS_I(inode);
441 spin_lock(&inode->i_lock);
443 set_bit(PG_NEED_COMMIT, &(req)->wb_flags);
444 radix_tree_tag_set(&nfsi->nfs_page_tree,
446 NFS_PAGE_TAG_COMMIT);
447 spin_unlock(&inode->i_lock);
448 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
449 inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
450 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
454 int nfs_write_need_commit(struct nfs_write_data *data)
456 return data->verf.committed != NFS_FILE_SYNC;
460 int nfs_reschedule_unstable_write(struct nfs_page *req)
462 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
463 nfs_mark_request_commit(req);
466 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
467 nfs_redirty_request(req);
474 nfs_mark_request_commit(struct nfs_page *req)
479 int nfs_write_need_commit(struct nfs_write_data *data)
485 int nfs_reschedule_unstable_write(struct nfs_page *req)
492 * Wait for a request to complete.
494 * Interruptible by fatal signals only.
496 static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
498 struct nfs_inode *nfsi = NFS_I(inode);
499 struct nfs_page *req;
500 pgoff_t idx_end, next;
501 unsigned int res = 0;
507 idx_end = idx_start + npages - 1;
510 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) {
511 if (req->wb_index > idx_end)
514 next = req->wb_index + 1;
515 BUG_ON(!NFS_WBACK_BUSY(req));
517 kref_get(&req->wb_kref);
518 spin_unlock(&inode->i_lock);
519 error = nfs_wait_on_request(req);
520 nfs_release_request(req);
521 spin_lock(&inode->i_lock);
529 static void nfs_cancel_commit_list(struct list_head *head)
531 struct nfs_page *req;
533 while(!list_empty(head)) {
534 req = nfs_list_entry(head->next);
535 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
536 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
538 nfs_list_remove_request(req);
539 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
540 nfs_inode_remove_request(req);
541 nfs_unlock_request(req);
545 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
547 * nfs_scan_commit - Scan an inode for commit requests
548 * @inode: NFS inode to scan
549 * @dst: destination list
550 * @idx_start: lower bound of page->index to scan.
551 * @npages: idx_start + npages sets the upper bound to scan.
553 * Moves requests from the inode's 'commit' request list.
554 * The requests are *not* checked to ensure that they form a contiguous set.
557 nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
559 struct nfs_inode *nfsi = NFS_I(inode);
562 if (nfsi->ncommit != 0) {
563 res = nfs_scan_list(nfsi, dst, idx_start, npages,
564 NFS_PAGE_TAG_COMMIT);
565 nfsi->ncommit -= res;
570 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
577 * Try to update any existing write request, or create one if there is none.
578 * In order to match, the request's credentials must match those of
579 * the calling process.
581 * Note: Should always be called with the Page Lock held!
583 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
584 struct page *page, unsigned int offset, unsigned int bytes)
586 struct address_space *mapping = page->mapping;
587 struct inode *inode = mapping->host;
588 struct nfs_page *req, *new = NULL;
591 end = offset + bytes;
594 /* Loop over all inode entries and see if we find
595 * A request for the page we wish to update
597 spin_lock(&inode->i_lock);
598 req = nfs_page_find_request_locked(page);
600 if (!nfs_set_page_tag_locked(req)) {
603 spin_unlock(&inode->i_lock);
604 error = nfs_wait_on_request(req);
605 nfs_release_request(req);
608 nfs_release_request(new);
609 return ERR_PTR(error);
613 spin_unlock(&inode->i_lock);
615 nfs_release_request(new);
621 nfs_lock_request_dontget(new);
622 error = nfs_inode_add_request(inode, new);
624 spin_unlock(&inode->i_lock);
625 nfs_unlock_request(new);
626 return ERR_PTR(error);
628 spin_unlock(&inode->i_lock);
632 spin_unlock(&inode->i_lock);
634 new = nfs_create_request(ctx, inode, page, offset, bytes);
639 /* We have a request for our page.
640 * If the creds don't match, or the
641 * page addresses don't match,
642 * tell the caller to wait on the conflicting
645 rqend = req->wb_offset + req->wb_bytes;
646 if (req->wb_context != ctx
647 || req->wb_page != page
648 || !nfs_dirty_request(req)
649 || offset > rqend || end < req->wb_offset) {
650 nfs_clear_page_tag_locked(req);
651 return ERR_PTR(-EBUSY);
654 /* Okay, the request matches. Update the region */
655 if (offset < req->wb_offset) {
656 req->wb_offset = offset;
657 req->wb_pgbase = offset;
658 req->wb_bytes = max(end, rqend) - req->wb_offset;
663 req->wb_bytes = end - req->wb_offset;
667 /* If this page might potentially be marked as up to date,
668 * then we need to zero any uninitalised data. */
669 if (req->wb_pgbase == 0 && req->wb_bytes != PAGE_CACHE_SIZE
670 && !PageUptodate(req->wb_page))
671 zero_user_segment(req->wb_page, req->wb_bytes, PAGE_CACHE_SIZE);
675 int nfs_flush_incompatible(struct file *file, struct page *page)
677 struct nfs_open_context *ctx = nfs_file_open_context(file);
678 struct nfs_page *req;
679 int do_flush, status;
681 * Look for a request corresponding to this page. If there
682 * is one, and it belongs to another file, we flush it out
683 * before we try to copy anything into the page. Do this
684 * due to the lack of an ACCESS-type call in NFSv2.
685 * Also do the same if we find a request from an existing
689 req = nfs_page_find_request(page);
692 do_flush = req->wb_page != page || req->wb_context != ctx
693 || !nfs_dirty_request(req);
694 nfs_release_request(req);
697 status = nfs_wb_page(page->mapping->host, page);
698 } while (status == 0);
703 * If the page cache is marked as unsafe or invalid, then we can't rely on
704 * the PageUptodate() flag. In this case, we will need to turn off
705 * write optimisations that depend on the page contents being correct.
707 static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
709 return PageUptodate(page) &&
710 !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
714 * Update and possibly write a cached page of an NFS file.
716 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
717 * things with a page scheduled for an RPC call (e.g. invalidate it).
719 int nfs_updatepage(struct file *file, struct page *page,
720 unsigned int offset, unsigned int count)
722 struct nfs_open_context *ctx = nfs_file_open_context(file);
723 struct inode *inode = page->mapping->host;
726 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
728 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
729 file->f_path.dentry->d_parent->d_name.name,
730 file->f_path.dentry->d_name.name, count,
731 (long long)(page_offset(page) +offset));
733 /* If we're not using byte range locks, and we know the page
734 * is up to date, it may be more efficient to extend the write
735 * to cover the entire page in order to avoid fragmentation
738 if (nfs_write_pageuptodate(page, inode) &&
739 inode->i_flock == NULL &&
740 !(file->f_flags & O_SYNC)) {
741 count = max(count + offset, nfs_page_length(page));
745 status = nfs_writepage_setup(ctx, page, offset, count);
746 __set_page_dirty_nobuffers(page);
748 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
749 status, (long long)i_size_read(inode));
751 nfs_set_pageerror(page);
755 static void nfs_writepage_release(struct nfs_page *req)
758 if (PageError(req->wb_page)) {
759 nfs_end_page_writeback(req->wb_page);
760 nfs_inode_remove_request(req);
761 } else if (!nfs_reschedule_unstable_write(req)) {
762 /* Set the PG_uptodate flag */
763 nfs_mark_uptodate(req->wb_page, req->wb_pgbase, req->wb_bytes);
764 nfs_end_page_writeback(req->wb_page);
765 nfs_inode_remove_request(req);
767 nfs_end_page_writeback(req->wb_page);
768 nfs_clear_page_tag_locked(req);
771 static int flush_task_priority(int how)
773 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
775 return RPC_PRIORITY_HIGH;
777 return RPC_PRIORITY_LOW;
779 return RPC_PRIORITY_NORMAL;
783 * Set up the argument/result storage required for the RPC call.
785 static void nfs_write_rpcsetup(struct nfs_page *req,
786 struct nfs_write_data *data,
787 const struct rpc_call_ops *call_ops,
788 unsigned int count, unsigned int offset,
791 struct inode *inode = req->wb_context->path.dentry->d_inode;
792 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
793 int priority = flush_task_priority(how);
794 struct rpc_task *task;
795 struct rpc_message msg = {
796 .rpc_argp = &data->args,
797 .rpc_resp = &data->res,
798 .rpc_cred = req->wb_context->cred,
800 struct rpc_task_setup task_setup_data = {
801 .rpc_client = NFS_CLIENT(inode),
804 .callback_ops = call_ops,
805 .callback_data = data,
806 .workqueue = nfsiod_workqueue,
808 .priority = priority,
811 /* Set up the RPC argument and reply structs
812 * NB: take care not to mess about with data->commit et al. */
815 data->inode = inode = req->wb_context->path.dentry->d_inode;
816 data->cred = msg.rpc_cred;
818 data->args.fh = NFS_FH(inode);
819 data->args.offset = req_offset(req) + offset;
820 data->args.pgbase = req->wb_pgbase + offset;
821 data->args.pages = data->pagevec;
822 data->args.count = count;
823 data->args.context = get_nfs_open_context(req->wb_context);
824 data->args.stable = NFS_UNSTABLE;
825 if (how & FLUSH_STABLE) {
826 data->args.stable = NFS_DATA_SYNC;
827 if (!NFS_I(inode)->ncommit)
828 data->args.stable = NFS_FILE_SYNC;
831 data->res.fattr = &data->fattr;
832 data->res.count = count;
833 data->res.verf = &data->verf;
834 nfs_fattr_init(&data->fattr);
836 /* Set up the initial task struct. */
837 NFS_PROTO(inode)->write_setup(data, &msg);
839 dprintk("NFS: %5u initiated write call "
840 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
843 (long long)NFS_FILEID(inode),
845 (unsigned long long)data->args.offset);
847 task = rpc_run_task(&task_setup_data);
853 * Generate multiple small requests to write out a single
854 * contiguous dirty area on one page.
856 static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
858 struct nfs_page *req = nfs_list_entry(head->next);
859 struct page *page = req->wb_page;
860 struct nfs_write_data *data;
861 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
866 nfs_list_remove_request(req);
870 size_t len = min(nbytes, wsize);
872 data = nfs_writedata_alloc(1);
875 list_add(&data->pages, &list);
878 } while (nbytes != 0);
879 atomic_set(&req->wb_complete, requests);
881 ClearPageError(page);
885 data = list_entry(list.next, struct nfs_write_data, pages);
886 list_del_init(&data->pages);
888 data->pagevec[0] = page;
892 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
896 } while (nbytes != 0);
901 while (!list_empty(&list)) {
902 data = list_entry(list.next, struct nfs_write_data, pages);
903 list_del(&data->pages);
904 nfs_writedata_release(data);
906 nfs_redirty_request(req);
907 nfs_end_page_writeback(req->wb_page);
908 nfs_clear_page_tag_locked(req);
913 * Create an RPC task for the given write request and kick it.
914 * The page must have been locked by the caller.
916 * It may happen that the page we're passed is not marked dirty.
917 * This is the case if nfs_updatepage detects a conflicting request
918 * that has been written but not committed.
920 static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
922 struct nfs_page *req;
924 struct nfs_write_data *data;
926 data = nfs_writedata_alloc(npages);
930 pages = data->pagevec;
931 while (!list_empty(head)) {
932 req = nfs_list_entry(head->next);
933 nfs_list_remove_request(req);
934 nfs_list_add_request(req, &data->pages);
935 ClearPageError(req->wb_page);
936 *pages++ = req->wb_page;
938 req = nfs_list_entry(data->pages.next);
940 /* Set up the argument struct */
941 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
945 while (!list_empty(head)) {
946 req = nfs_list_entry(head->next);
947 nfs_list_remove_request(req);
948 nfs_redirty_request(req);
949 nfs_end_page_writeback(req->wb_page);
950 nfs_clear_page_tag_locked(req);
955 static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
956 struct inode *inode, int ioflags)
958 size_t wsize = NFS_SERVER(inode)->wsize;
960 if (wsize < PAGE_CACHE_SIZE)
961 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
963 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
967 * Handle a write reply that flushed part of a page.
969 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
971 struct nfs_write_data *data = calldata;
972 struct nfs_page *req = data->req;
973 struct page *page = req->wb_page;
975 dprintk("NFS: write (%s/%Ld %d@%Ld)",
976 req->wb_context->path.dentry->d_inode->i_sb->s_id,
977 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
979 (long long)req_offset(req));
981 if (nfs_writeback_done(task, data) != 0)
984 if (task->tk_status < 0) {
985 nfs_set_pageerror(page);
986 nfs_context_set_write_error(req->wb_context, task->tk_status);
987 dprintk(", error = %d\n", task->tk_status);
991 if (nfs_write_need_commit(data)) {
992 struct inode *inode = page->mapping->host;
994 spin_lock(&inode->i_lock);
995 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
996 /* Do nothing we need to resend the writes */
997 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
998 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
999 dprintk(" defer commit\n");
1000 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1001 set_bit(PG_NEED_RESCHED, &req->wb_flags);
1002 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
1003 dprintk(" server reboot detected\n");
1005 spin_unlock(&inode->i_lock);
1010 if (atomic_dec_and_test(&req->wb_complete))
1011 nfs_writepage_release(req);
1014 static const struct rpc_call_ops nfs_write_partial_ops = {
1015 .rpc_call_done = nfs_writeback_done_partial,
1016 .rpc_release = nfs_writedata_release,
1020 * Handle a write reply that flushes a whole page.
1022 * FIXME: There is an inherent race with invalidate_inode_pages and
1023 * writebacks since the page->count is kept > 1 for as long
1024 * as the page has a write request pending.
1026 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1028 struct nfs_write_data *data = calldata;
1029 struct nfs_page *req;
1032 if (nfs_writeback_done(task, data) != 0)
1035 /* Update attributes as result of writeback. */
1036 while (!list_empty(&data->pages)) {
1037 req = nfs_list_entry(data->pages.next);
1038 nfs_list_remove_request(req);
1039 page = req->wb_page;
1041 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1042 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1043 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1045 (long long)req_offset(req));
1047 if (task->tk_status < 0) {
1048 nfs_set_pageerror(page);
1049 nfs_context_set_write_error(req->wb_context, task->tk_status);
1050 dprintk(", error = %d\n", task->tk_status);
1051 goto remove_request;
1054 if (nfs_write_need_commit(data)) {
1055 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1056 nfs_mark_request_commit(req);
1057 nfs_end_page_writeback(page);
1058 dprintk(" marked for commit\n");
1061 /* Set the PG_uptodate flag? */
1062 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
1065 nfs_end_page_writeback(page);
1066 nfs_inode_remove_request(req);
1068 nfs_clear_page_tag_locked(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 *data)
1162 struct nfs_write_data *wdata = data;
1164 put_nfs_open_context(wdata->args.context);
1165 nfs_commit_free(wdata);
1169 * Set up the argument/result storage required for the RPC call.
1171 static void nfs_commit_rpcsetup(struct list_head *head,
1172 struct nfs_write_data *data,
1175 struct nfs_page *first = nfs_list_entry(head->next);
1176 struct inode *inode = first->wb_context->path.dentry->d_inode;
1177 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1178 int priority = flush_task_priority(how);
1179 struct rpc_task *task;
1180 struct rpc_message msg = {
1181 .rpc_argp = &data->args,
1182 .rpc_resp = &data->res,
1183 .rpc_cred = first->wb_context->cred,
1185 struct rpc_task_setup task_setup_data = {
1186 .task = &data->task,
1187 .rpc_client = NFS_CLIENT(inode),
1188 .rpc_message = &msg,
1189 .callback_ops = &nfs_commit_ops,
1190 .callback_data = data,
1191 .workqueue = nfsiod_workqueue,
1193 .priority = priority,
1196 /* Set up the RPC argument and reply structs
1197 * NB: take care not to mess about with data->commit et al. */
1199 list_splice_init(head, &data->pages);
1201 data->inode = inode;
1202 data->cred = msg.rpc_cred;
1204 data->args.fh = NFS_FH(data->inode);
1205 /* Note: we always request a commit of the entire inode */
1206 data->args.offset = 0;
1207 data->args.count = 0;
1208 data->args.context = get_nfs_open_context(first->wb_context);
1209 data->res.count = 0;
1210 data->res.fattr = &data->fattr;
1211 data->res.verf = &data->verf;
1212 nfs_fattr_init(&data->fattr);
1214 /* Set up the initial task struct. */
1215 NFS_PROTO(inode)->commit_setup(data, &msg);
1217 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1219 task = rpc_run_task(&task_setup_data);
1225 * Commit dirty pages
1228 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1230 struct nfs_write_data *data;
1231 struct nfs_page *req;
1233 data = nfs_commit_alloc();
1238 /* Set up the argument struct */
1239 nfs_commit_rpcsetup(head, data, how);
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 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1250 nfs_clear_page_tag_locked(req);
1256 * COMMIT call returned
1258 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1260 struct nfs_write_data *data = calldata;
1261 struct nfs_page *req;
1263 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1264 task->tk_pid, task->tk_status);
1266 /* Call the NFS version-specific code */
1267 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1270 while (!list_empty(&data->pages)) {
1271 req = nfs_list_entry(data->pages.next);
1272 nfs_list_remove_request(req);
1273 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
1274 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1275 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1278 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1279 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1280 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1282 (long long)req_offset(req));
1283 if (task->tk_status < 0) {
1284 nfs_context_set_write_error(req->wb_context, task->tk_status);
1285 nfs_inode_remove_request(req);
1286 dprintk(", error = %d\n", task->tk_status);
1290 /* Okay, COMMIT succeeded, apparently. Check the verifier
1291 * returned by the server against all stored verfs. */
1292 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1293 /* We have a match */
1294 /* Set the PG_uptodate flag */
1295 nfs_mark_uptodate(req->wb_page, req->wb_pgbase,
1297 nfs_inode_remove_request(req);
1301 /* We have a mismatch. Write the page again */
1302 dprintk(" mismatch\n");
1303 nfs_redirty_request(req);
1305 nfs_clear_page_tag_locked(req);
1309 static const struct rpc_call_ops nfs_commit_ops = {
1310 .rpc_call_done = nfs_commit_done,
1311 .rpc_release = nfs_commit_release,
1314 int nfs_commit_inode(struct inode *inode, int how)
1319 spin_lock(&inode->i_lock);
1320 res = nfs_scan_commit(inode, &head, 0, 0);
1321 spin_unlock(&inode->i_lock);
1323 int error = nfs_commit_list(inode, &head, how);
1330 static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1336 long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
1338 struct inode *inode = mapping->host;
1339 pgoff_t idx_start, idx_end;
1340 unsigned int npages = 0;
1342 int nocommit = how & FLUSH_NOCOMMIT;
1346 if (wbc->range_cyclic)
1349 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1350 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1351 if (idx_end > idx_start) {
1352 pgoff_t l_npages = 1 + idx_end - idx_start;
1354 if (sizeof(npages) != sizeof(l_npages) &&
1355 (pgoff_t)npages != l_npages)
1359 how &= ~FLUSH_NOCOMMIT;
1360 spin_lock(&inode->i_lock);
1362 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1367 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1370 if (how & FLUSH_INVALIDATE) {
1371 spin_unlock(&inode->i_lock);
1372 nfs_cancel_commit_list(&head);
1374 spin_lock(&inode->i_lock);
1377 pages += nfs_scan_commit(inode, &head, 0, 0);
1378 spin_unlock(&inode->i_lock);
1379 ret = nfs_commit_list(inode, &head, how);
1380 spin_lock(&inode->i_lock);
1383 spin_unlock(&inode->i_lock);
1387 static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
1391 ret = nfs_writepages(mapping, wbc);
1394 ret = nfs_sync_mapping_wait(mapping, wbc, how);
1399 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1403 /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
1404 static int nfs_write_mapping(struct address_space *mapping, int how)
1406 struct writeback_control wbc = {
1407 .bdi = mapping->backing_dev_info,
1408 .sync_mode = WB_SYNC_NONE,
1409 .nr_to_write = LONG_MAX,
1410 .for_writepages = 1,
1415 ret = __nfs_write_mapping(mapping, &wbc, how);
1418 wbc.sync_mode = WB_SYNC_ALL;
1419 return __nfs_write_mapping(mapping, &wbc, how);
1423 * flush the inode to disk.
1425 int nfs_wb_all(struct inode *inode)
1427 return nfs_write_mapping(inode->i_mapping, 0);
1430 int nfs_wb_nocommit(struct inode *inode)
1432 return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
1435 int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1437 struct nfs_page *req;
1438 loff_t range_start = page_offset(page);
1439 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1440 struct writeback_control wbc = {
1441 .bdi = page->mapping->backing_dev_info,
1442 .sync_mode = WB_SYNC_ALL,
1443 .nr_to_write = LONG_MAX,
1444 .range_start = range_start,
1445 .range_end = range_end,
1449 BUG_ON(!PageLocked(page));
1451 req = nfs_page_find_request(page);
1454 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1455 nfs_release_request(req);
1458 if (nfs_lock_request_dontget(req)) {
1459 nfs_inode_remove_request(req);
1461 * In case nfs_inode_remove_request has marked the
1462 * page as being dirty
1464 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1465 nfs_unlock_request(req);
1468 ret = nfs_wait_on_request(req);
1472 if (!PagePrivate(page))
1474 ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
1479 static int nfs_wb_page_priority(struct inode *inode, struct page *page,
1482 loff_t range_start = page_offset(page);
1483 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1484 struct writeback_control wbc = {
1485 .bdi = page->mapping->backing_dev_info,
1486 .sync_mode = WB_SYNC_ALL,
1487 .nr_to_write = LONG_MAX,
1488 .range_start = range_start,
1489 .range_end = range_end,
1493 BUG_ON(!PageLocked(page));
1494 if (clear_page_dirty_for_io(page)) {
1495 ret = nfs_writepage_locked(page, &wbc);
1499 if (!PagePrivate(page))
1501 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1505 __mark_inode_dirty(inode, I_DIRTY_PAGES);
1510 * Write back all requests on one page - we do this before reading it.
1512 int nfs_wb_page(struct inode *inode, struct page* page)
1514 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
1517 int __init nfs_init_writepagecache(void)
1519 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1520 sizeof(struct nfs_write_data),
1521 0, SLAB_HWCACHE_ALIGN,
1523 if (nfs_wdata_cachep == NULL)
1526 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1528 if (nfs_wdata_mempool == NULL)
1531 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1533 if (nfs_commit_mempool == NULL)
1537 * NFS congestion size, scale with available memory.
1549 * This allows larger machines to have larger/more transfers.
1550 * Limit the default to 256M
1552 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1553 if (nfs_congestion_kb > 256*1024)
1554 nfs_congestion_kb = 256*1024;
1559 void nfs_destroy_writepagecache(void)
1561 mempool_destroy(nfs_commit_mempool);
1562 mempool_destroy(nfs_wdata_mempool);
1563 kmem_cache_destroy(nfs_wdata_cachep);