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>
16 #include <linux/migrate.h>
18 #include <linux/sunrpc/clnt.h>
19 #include <linux/nfs_fs.h>
20 #include <linux/nfs_mount.h>
21 #include <linux/nfs_page.h>
22 #include <linux/backing-dev.h>
24 #include <asm/uaccess.h>
26 #include "delegation.h"
32 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
34 #define MIN_POOL_WRITE (32)
35 #define MIN_POOL_COMMIT (4)
38 * Local function declarations
40 static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc,
41 struct inode *inode, int ioflags);
42 static void nfs_redirty_request(struct nfs_page *req);
43 static const struct rpc_call_ops nfs_write_partial_ops;
44 static const struct rpc_call_ops nfs_write_full_ops;
45 static const struct rpc_call_ops nfs_commit_ops;
47 static struct kmem_cache *nfs_wdata_cachep;
48 static mempool_t *nfs_wdata_mempool;
49 static mempool_t *nfs_commit_mempool;
51 struct nfs_write_data *nfs_commitdata_alloc(void)
53 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
56 memset(p, 0, sizeof(*p));
57 INIT_LIST_HEAD(&p->pages);
62 void nfs_commit_free(struct nfs_write_data *p)
64 if (p && (p->pagevec != &p->page_array[0]))
66 mempool_free(p, nfs_commit_mempool);
69 struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
71 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
74 memset(p, 0, sizeof(*p));
75 INIT_LIST_HEAD(&p->pages);
76 p->npages = pagecount;
77 if (pagecount <= ARRAY_SIZE(p->page_array))
78 p->pagevec = p->page_array;
80 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
82 mempool_free(p, nfs_wdata_mempool);
90 void nfs_writedata_free(struct nfs_write_data *p)
92 if (p && (p->pagevec != &p->page_array[0]))
94 mempool_free(p, nfs_wdata_mempool);
97 static void nfs_writedata_release(struct nfs_write_data *wdata)
99 put_nfs_open_context(wdata->args.context);
100 nfs_writedata_free(wdata);
103 static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
107 set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
110 static struct nfs_page *nfs_page_find_request_locked(struct page *page)
112 struct nfs_page *req = NULL;
114 if (PagePrivate(page)) {
115 req = (struct nfs_page *)page_private(page);
117 kref_get(&req->wb_kref);
122 static struct nfs_page *nfs_page_find_request(struct page *page)
124 struct inode *inode = page->mapping->host;
125 struct nfs_page *req = NULL;
127 spin_lock(&inode->i_lock);
128 req = nfs_page_find_request_locked(page);
129 spin_unlock(&inode->i_lock);
133 /* Adjust the file length if we're writing beyond the end */
134 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
136 struct inode *inode = page->mapping->host;
140 spin_lock(&inode->i_lock);
141 i_size = i_size_read(inode);
142 end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
143 if (i_size > 0 && page->index < end_index)
145 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
148 i_size_write(inode, end);
149 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
151 spin_unlock(&inode->i_lock);
154 /* A writeback failed: mark the page as bad, and invalidate the page cache */
155 static void nfs_set_pageerror(struct page *page)
158 nfs_zap_mapping(page->mapping->host, page->mapping);
161 /* We can set the PG_uptodate flag if we see that a write request
162 * covers the full page.
164 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
166 if (PageUptodate(page))
170 if (count != nfs_page_length(page))
172 SetPageUptodate(page);
175 static int wb_priority(struct writeback_control *wbc)
177 if (wbc->for_reclaim)
178 return FLUSH_HIGHPRI | FLUSH_STABLE;
179 if (wbc->for_kupdate || wbc->for_background)
185 * NFS congestion control
188 int nfs_congestion_kb;
190 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
191 #define NFS_CONGESTION_OFF_THRESH \
192 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
194 static int nfs_set_page_writeback(struct page *page)
196 int ret = test_set_page_writeback(page);
199 struct inode *inode = page->mapping->host;
200 struct nfs_server *nfss = NFS_SERVER(inode);
202 page_cache_get(page);
203 if (atomic_long_inc_return(&nfss->writeback) >
204 NFS_CONGESTION_ON_THRESH) {
205 set_bdi_congested(&nfss->backing_dev_info,
212 static void nfs_end_page_writeback(struct page *page)
214 struct inode *inode = page->mapping->host;
215 struct nfs_server *nfss = NFS_SERVER(inode);
217 end_page_writeback(page);
218 page_cache_release(page);
219 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
220 clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
223 static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock)
225 struct inode *inode = page->mapping->host;
226 struct nfs_page *req;
229 spin_lock(&inode->i_lock);
231 req = nfs_page_find_request_locked(page);
234 if (nfs_set_page_tag_locked(req))
236 /* Note: If we hold the page lock, as is the case in nfs_writepage,
237 * then the call to nfs_set_page_tag_locked() will always
238 * succeed provided that someone hasn't already marked the
239 * request as dirty (in which case we don't care).
241 spin_unlock(&inode->i_lock);
243 ret = nfs_wait_on_request(req);
246 nfs_release_request(req);
249 spin_lock(&inode->i_lock);
251 spin_unlock(&inode->i_lock);
256 * Find an associated nfs write request, and prepare to flush it out
257 * May return an error if the user signalled nfs_wait_on_request().
259 static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
260 struct page *page, bool nonblock)
262 struct nfs_page *req;
265 req = nfs_find_and_lock_request(page, nonblock);
272 ret = nfs_set_page_writeback(page);
274 BUG_ON(test_bit(PG_CLEAN, &req->wb_flags));
276 if (!nfs_pageio_add_request(pgio, req)) {
277 nfs_redirty_request(req);
278 ret = pgio->pg_error;
284 static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
286 struct inode *inode = page->mapping->host;
289 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
290 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
292 nfs_pageio_cond_complete(pgio, page->index);
293 ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
294 if (ret == -EAGAIN) {
295 redirty_page_for_writepage(wbc, page);
302 * Write an mmapped page to the server.
304 static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
306 struct nfs_pageio_descriptor pgio;
309 nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
310 err = nfs_do_writepage(page, wbc, &pgio);
311 nfs_pageio_complete(&pgio);
314 if (pgio.pg_error < 0)
315 return pgio.pg_error;
319 int nfs_writepage(struct page *page, struct writeback_control *wbc)
323 ret = nfs_writepage_locked(page, wbc);
328 static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
332 ret = nfs_do_writepage(page, wbc, data);
337 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
339 struct inode *inode = mapping->host;
340 unsigned long *bitlock = &NFS_I(inode)->flags;
341 struct nfs_pageio_descriptor pgio;
344 /* Stop dirtying of new pages while we sync */
345 err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
346 nfs_wait_bit_killable, TASK_KILLABLE);
350 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
352 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
353 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
354 nfs_pageio_complete(&pgio);
356 clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
357 smp_mb__after_clear_bit();
358 wake_up_bit(bitlock, NFS_INO_FLUSHING);
371 * Insert a write request into an inode
373 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
375 struct nfs_inode *nfsi = NFS_I(inode);
378 error = radix_tree_preload(GFP_NOFS);
382 /* Lock the request! */
383 nfs_lock_request_dontget(req);
385 spin_lock(&inode->i_lock);
386 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
390 if (nfs_have_delegation(inode, FMODE_WRITE))
393 SetPagePrivate(req->wb_page);
394 set_page_private(req->wb_page, (unsigned long)req);
396 kref_get(&req->wb_kref);
397 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
398 NFS_PAGE_TAG_LOCKED);
399 spin_unlock(&inode->i_lock);
400 radix_tree_preload_end();
406 * Remove a write request from an inode
408 static void nfs_inode_remove_request(struct nfs_page *req)
410 struct inode *inode = req->wb_context->path.dentry->d_inode;
411 struct nfs_inode *nfsi = NFS_I(inode);
413 BUG_ON (!NFS_WBACK_BUSY(req));
415 spin_lock(&inode->i_lock);
416 set_page_private(req->wb_page, 0);
417 ClearPagePrivate(req->wb_page);
418 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
421 spin_unlock(&inode->i_lock);
424 spin_unlock(&inode->i_lock);
425 nfs_clear_request(req);
426 nfs_release_request(req);
430 nfs_mark_request_dirty(struct nfs_page *req)
432 __set_page_dirty_nobuffers(req->wb_page);
433 __mark_inode_dirty(req->wb_page->mapping->host, I_DIRTY_DATASYNC);
436 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
438 * Add a request to the inode's commit list.
441 nfs_mark_request_commit(struct nfs_page *req)
443 struct inode *inode = req->wb_context->path.dentry->d_inode;
444 struct nfs_inode *nfsi = NFS_I(inode);
446 spin_lock(&inode->i_lock);
447 set_bit(PG_CLEAN, &(req)->wb_flags);
448 radix_tree_tag_set(&nfsi->nfs_page_tree,
450 NFS_PAGE_TAG_COMMIT);
452 spin_unlock(&inode->i_lock);
453 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
454 inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
455 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
459 nfs_clear_request_commit(struct nfs_page *req)
461 struct page *page = req->wb_page;
463 if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) {
464 dec_zone_page_state(page, NR_UNSTABLE_NFS);
465 dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
472 int nfs_write_need_commit(struct nfs_write_data *data)
474 return data->verf.committed != NFS_FILE_SYNC;
478 int nfs_reschedule_unstable_write(struct nfs_page *req)
480 if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) {
481 nfs_mark_request_commit(req);
484 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
485 nfs_mark_request_dirty(req);
492 nfs_mark_request_commit(struct nfs_page *req)
497 nfs_clear_request_commit(struct nfs_page *req)
503 int nfs_write_need_commit(struct nfs_write_data *data)
509 int nfs_reschedule_unstable_write(struct nfs_page *req)
515 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
517 nfs_need_commit(struct nfs_inode *nfsi)
519 return radix_tree_tagged(&nfsi->nfs_page_tree, NFS_PAGE_TAG_COMMIT);
523 * nfs_scan_commit - Scan an inode for commit requests
524 * @inode: NFS inode to scan
525 * @dst: destination list
526 * @idx_start: lower bound of page->index to scan.
527 * @npages: idx_start + npages sets the upper bound to scan.
529 * Moves requests from the inode's 'commit' request list.
530 * The requests are *not* checked to ensure that they form a contiguous set.
533 nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
535 struct nfs_inode *nfsi = NFS_I(inode);
538 if (!nfs_need_commit(nfsi))
541 ret = nfs_scan_list(nfsi, dst, idx_start, npages, NFS_PAGE_TAG_COMMIT);
543 nfsi->ncommit -= ret;
544 if (nfs_need_commit(NFS_I(inode)))
545 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
549 static inline int nfs_need_commit(struct nfs_inode *nfsi)
554 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
561 * Search for an existing write request, and attempt to update
562 * it to reflect a new dirty region on a given page.
564 * If the attempt fails, then the existing request is flushed out
567 static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
572 struct nfs_page *req;
577 if (!PagePrivate(page))
580 end = offset + bytes;
581 spin_lock(&inode->i_lock);
584 req = nfs_page_find_request_locked(page);
588 rqend = req->wb_offset + req->wb_bytes;
590 * Tell the caller to flush out the request if
591 * the offsets are non-contiguous.
592 * Note: nfs_flush_incompatible() will already
593 * have flushed out requests having wrong owners.
596 || end < req->wb_offset)
599 if (nfs_set_page_tag_locked(req))
602 /* The request is locked, so wait and then retry */
603 spin_unlock(&inode->i_lock);
604 error = nfs_wait_on_request(req);
605 nfs_release_request(req);
608 spin_lock(&inode->i_lock);
611 if (nfs_clear_request_commit(req) &&
612 radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree,
613 req->wb_index, NFS_PAGE_TAG_COMMIT) != NULL)
614 NFS_I(inode)->ncommit--;
616 /* Okay, the request matches. Update the region */
617 if (offset < req->wb_offset) {
618 req->wb_offset = offset;
619 req->wb_pgbase = offset;
622 req->wb_bytes = end - req->wb_offset;
624 req->wb_bytes = rqend - req->wb_offset;
626 spin_unlock(&inode->i_lock);
629 spin_unlock(&inode->i_lock);
630 nfs_release_request(req);
631 error = nfs_wb_page(inode, page);
633 return ERR_PTR(error);
637 * Try to update an existing write request, or create one if there is none.
639 * Note: Should always be called with the Page Lock held to prevent races
640 * if we have to add a new request. Also assumes that the caller has
641 * already called nfs_flush_incompatible() if necessary.
643 static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
644 struct page *page, unsigned int offset, unsigned int bytes)
646 struct inode *inode = page->mapping->host;
647 struct nfs_page *req;
650 req = nfs_try_to_update_request(inode, page, offset, bytes);
653 req = nfs_create_request(ctx, inode, page, offset, bytes);
656 error = nfs_inode_add_request(inode, req);
658 nfs_release_request(req);
659 req = ERR_PTR(error);
665 static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
666 unsigned int offset, unsigned int count)
668 struct nfs_page *req;
670 req = nfs_setup_write_request(ctx, page, offset, count);
673 nfs_mark_request_dirty(req);
674 /* Update file length */
675 nfs_grow_file(page, offset, count);
676 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
677 nfs_mark_request_dirty(req);
678 nfs_clear_page_tag_locked(req);
682 int nfs_flush_incompatible(struct file *file, struct page *page)
684 struct nfs_open_context *ctx = nfs_file_open_context(file);
685 struct nfs_page *req;
686 int do_flush, status;
688 * Look for a request corresponding to this page. If there
689 * is one, and it belongs to another file, we flush it out
690 * before we try to copy anything into the page. Do this
691 * due to the lack of an ACCESS-type call in NFSv2.
692 * Also do the same if we find a request from an existing
696 req = nfs_page_find_request(page);
699 do_flush = req->wb_page != page || req->wb_context != ctx ||
700 req->wb_lock_context->lockowner != current->files ||
701 req->wb_lock_context->pid != current->tgid;
702 nfs_release_request(req);
705 status = nfs_wb_page(page->mapping->host, page);
706 } while (status == 0);
711 * If the page cache is marked as unsafe or invalid, then we can't rely on
712 * the PageUptodate() flag. In this case, we will need to turn off
713 * write optimisations that depend on the page contents being correct.
715 static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
717 return PageUptodate(page) &&
718 !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
722 * Update and possibly write a cached page of an NFS file.
724 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
725 * things with a page scheduled for an RPC call (e.g. invalidate it).
727 int nfs_updatepage(struct file *file, struct page *page,
728 unsigned int offset, unsigned int count)
730 struct nfs_open_context *ctx = nfs_file_open_context(file);
731 struct inode *inode = page->mapping->host;
734 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
736 dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n",
737 file->f_path.dentry->d_parent->d_name.name,
738 file->f_path.dentry->d_name.name, count,
739 (long long)(page_offset(page) + offset));
741 /* If we're not using byte range locks, and we know the page
742 * is up to date, it may be more efficient to extend the write
743 * to cover the entire page in order to avoid fragmentation
746 if (nfs_write_pageuptodate(page, inode) &&
747 inode->i_flock == NULL &&
748 !(file->f_flags & O_DSYNC)) {
749 count = max(count + offset, nfs_page_length(page));
753 status = nfs_writepage_setup(ctx, page, offset, count);
755 nfs_set_pageerror(page);
757 dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
758 status, (long long)i_size_read(inode));
762 static void nfs_writepage_release(struct nfs_page *req)
764 struct page *page = req->wb_page;
766 if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req))
767 nfs_inode_remove_request(req);
768 nfs_clear_page_tag_locked(req);
769 nfs_end_page_writeback(page);
772 static int flush_task_priority(int how)
774 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
776 return RPC_PRIORITY_HIGH;
778 return RPC_PRIORITY_LOW;
780 return RPC_PRIORITY_NORMAL;
784 * Set up the argument/result storage required for the RPC call.
786 static int nfs_write_rpcsetup(struct nfs_page *req,
787 struct nfs_write_data *data,
788 const struct rpc_call_ops *call_ops,
789 unsigned int count, unsigned int offset,
792 struct inode *inode = req->wb_context->path.dentry->d_inode;
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,
807 .flags = RPC_TASK_ASYNC,
808 .priority = priority,
812 /* Set up the RPC argument and reply structs
813 * NB: take care not to mess about with data->commit et al. */
816 data->inode = inode = req->wb_context->path.dentry->d_inode;
817 data->cred = msg.rpc_cred;
819 data->args.fh = NFS_FH(inode);
820 data->args.offset = req_offset(req) + offset;
821 data->args.pgbase = req->wb_pgbase + offset;
822 data->args.pages = data->pagevec;
823 data->args.count = count;
824 data->args.context = get_nfs_open_context(req->wb_context);
825 data->args.lock_context = req->wb_lock_context;
826 data->args.stable = NFS_UNSTABLE;
827 if (how & FLUSH_STABLE) {
828 data->args.stable = NFS_DATA_SYNC;
829 if (!nfs_need_commit(NFS_I(inode)))
830 data->args.stable = NFS_FILE_SYNC;
833 data->res.fattr = &data->fattr;
834 data->res.count = count;
835 data->res.verf = &data->verf;
836 nfs_fattr_init(&data->fattr);
838 /* Set up the initial task struct. */
839 NFS_PROTO(inode)->write_setup(data, &msg);
841 dprintk("NFS: %5u initiated write call "
842 "(req %s/%lld, %u bytes @ offset %llu)\n",
845 (long long)NFS_FILEID(inode),
847 (unsigned long long)data->args.offset);
849 task = rpc_run_task(&task_setup_data);
854 if (how & FLUSH_SYNC) {
855 ret = rpc_wait_for_completion_task(task);
857 ret = task->tk_status;
864 /* If a nfs_flush_* function fails, it should remove reqs from @head and
865 * call this on each, which will prepare them to be retried on next
866 * writeback using standard nfs.
868 static void nfs_redirty_request(struct nfs_page *req)
870 struct page *page = req->wb_page;
872 nfs_mark_request_dirty(req);
873 nfs_clear_page_tag_locked(req);
874 nfs_end_page_writeback(page);
878 * Generate multiple small requests to write out a single
879 * contiguous dirty area on one page.
881 static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
883 struct nfs_page *req = nfs_list_entry(head->next);
884 struct page *page = req->wb_page;
885 struct nfs_write_data *data;
886 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
892 nfs_list_remove_request(req);
896 size_t len = min(nbytes, wsize);
898 data = nfs_writedata_alloc(1);
901 list_add(&data->pages, &list);
904 } while (nbytes != 0);
905 atomic_set(&req->wb_complete, requests);
907 ClearPageError(page);
913 data = list_entry(list.next, struct nfs_write_data, pages);
914 list_del_init(&data->pages);
916 data->pagevec[0] = page;
920 ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
926 } while (nbytes != 0);
931 while (!list_empty(&list)) {
932 data = list_entry(list.next, struct nfs_write_data, pages);
933 list_del(&data->pages);
934 nfs_writedata_release(data);
936 nfs_redirty_request(req);
941 * Create an RPC task for the given write request and kick it.
942 * The page must have been locked by the caller.
944 * It may happen that the page we're passed is not marked dirty.
945 * This is the case if nfs_updatepage detects a conflicting request
946 * that has been written but not committed.
948 static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
950 struct nfs_page *req;
952 struct nfs_write_data *data;
954 data = nfs_writedata_alloc(npages);
958 pages = data->pagevec;
959 while (!list_empty(head)) {
960 req = nfs_list_entry(head->next);
961 nfs_list_remove_request(req);
962 nfs_list_add_request(req, &data->pages);
963 ClearPageError(req->wb_page);
964 *pages++ = req->wb_page;
966 req = nfs_list_entry(data->pages.next);
968 /* Set up the argument struct */
969 return nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
971 while (!list_empty(head)) {
972 req = nfs_list_entry(head->next);
973 nfs_list_remove_request(req);
974 nfs_redirty_request(req);
979 static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
980 struct inode *inode, int ioflags)
982 size_t wsize = NFS_SERVER(inode)->wsize;
984 if (wsize < PAGE_CACHE_SIZE)
985 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
987 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
991 * Handle a write reply that flushed part of a page.
993 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
995 struct nfs_write_data *data = calldata;
997 dprintk("NFS: %5u write(%s/%lld %d@%lld)",
999 data->req->wb_context->path.dentry->d_inode->i_sb->s_id,
1001 NFS_FILEID(data->req->wb_context->path.dentry->d_inode),
1002 data->req->wb_bytes, (long long)req_offset(data->req));
1004 nfs_writeback_done(task, data);
1007 static void nfs_writeback_release_partial(void *calldata)
1009 struct nfs_write_data *data = calldata;
1010 struct nfs_page *req = data->req;
1011 struct page *page = req->wb_page;
1012 int status = data->task.tk_status;
1015 nfs_set_pageerror(page);
1016 nfs_context_set_write_error(req->wb_context, status);
1017 dprintk(", error = %d\n", status);
1021 if (nfs_write_need_commit(data)) {
1022 struct inode *inode = page->mapping->host;
1024 spin_lock(&inode->i_lock);
1025 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
1026 /* Do nothing we need to resend the writes */
1027 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1028 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1029 dprintk(" defer commit\n");
1030 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1031 set_bit(PG_NEED_RESCHED, &req->wb_flags);
1032 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
1033 dprintk(" server reboot detected\n");
1035 spin_unlock(&inode->i_lock);
1040 if (atomic_dec_and_test(&req->wb_complete))
1041 nfs_writepage_release(req);
1042 nfs_writedata_release(calldata);
1045 #if defined(CONFIG_NFS_V4_1)
1046 void nfs_write_prepare(struct rpc_task *task, void *calldata)
1048 struct nfs_write_data *data = calldata;
1050 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
1051 &data->args.seq_args,
1052 &data->res.seq_res, 1, task))
1054 rpc_call_start(task);
1056 #endif /* CONFIG_NFS_V4_1 */
1058 static const struct rpc_call_ops nfs_write_partial_ops = {
1059 #if defined(CONFIG_NFS_V4_1)
1060 .rpc_call_prepare = nfs_write_prepare,
1061 #endif /* CONFIG_NFS_V4_1 */
1062 .rpc_call_done = nfs_writeback_done_partial,
1063 .rpc_release = nfs_writeback_release_partial,
1067 * Handle a write reply that flushes a whole page.
1069 * FIXME: There is an inherent race with invalidate_inode_pages and
1070 * writebacks since the page->count is kept > 1 for as long
1071 * as the page has a write request pending.
1073 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1075 struct nfs_write_data *data = calldata;
1077 nfs_writeback_done(task, data);
1080 static void nfs_writeback_release_full(void *calldata)
1082 struct nfs_write_data *data = calldata;
1083 int status = data->task.tk_status;
1085 /* Update attributes as result of writeback. */
1086 while (!list_empty(&data->pages)) {
1087 struct nfs_page *req = nfs_list_entry(data->pages.next);
1088 struct page *page = req->wb_page;
1090 nfs_list_remove_request(req);
1092 dprintk("NFS: %5u write (%s/%lld %d@%lld)",
1094 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1095 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1097 (long long)req_offset(req));
1100 nfs_set_pageerror(page);
1101 nfs_context_set_write_error(req->wb_context, status);
1102 dprintk(", error = %d\n", status);
1103 goto remove_request;
1106 if (nfs_write_need_commit(data)) {
1107 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1108 nfs_mark_request_commit(req);
1109 dprintk(" marked for commit\n");
1114 nfs_inode_remove_request(req);
1116 nfs_clear_page_tag_locked(req);
1117 nfs_end_page_writeback(page);
1119 nfs_writedata_release(calldata);
1122 static const struct rpc_call_ops nfs_write_full_ops = {
1123 #if defined(CONFIG_NFS_V4_1)
1124 .rpc_call_prepare = nfs_write_prepare,
1125 #endif /* CONFIG_NFS_V4_1 */
1126 .rpc_call_done = nfs_writeback_done_full,
1127 .rpc_release = nfs_writeback_release_full,
1132 * This function is called when the WRITE call is complete.
1134 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1136 struct nfs_writeargs *argp = &data->args;
1137 struct nfs_writeres *resp = &data->res;
1138 struct nfs_server *server = NFS_SERVER(data->inode);
1141 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1142 task->tk_pid, task->tk_status);
1145 * ->write_done will attempt to use post-op attributes to detect
1146 * conflicting writes by other clients. A strict interpretation
1147 * of close-to-open would allow us to continue caching even if
1148 * another writer had changed the file, but some applications
1149 * depend on tighter cache coherency when writing.
1151 status = NFS_PROTO(data->inode)->write_done(task, data);
1154 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1156 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1157 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1158 /* We tried a write call, but the server did not
1159 * commit data to stable storage even though we
1161 * Note: There is a known bug in Tru64 < 5.0 in which
1162 * the server reports NFS_DATA_SYNC, but performs
1163 * NFS_FILE_SYNC. We therefore implement this checking
1164 * as a dprintk() in order to avoid filling syslog.
1166 static unsigned long complain;
1168 if (time_before(complain, jiffies)) {
1169 dprintk("NFS: faulty NFS server %s:"
1170 " (committed = %d) != (stable = %d)\n",
1171 server->nfs_client->cl_hostname,
1172 resp->verf->committed, argp->stable);
1173 complain = jiffies + 300 * HZ;
1177 /* Is this a short write? */
1178 if (task->tk_status >= 0 && resp->count < argp->count) {
1179 static unsigned long complain;
1181 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1183 /* Has the server at least made some progress? */
1184 if (resp->count != 0) {
1185 /* Was this an NFSv2 write or an NFSv3 stable write? */
1186 if (resp->verf->committed != NFS_UNSTABLE) {
1187 /* Resend from where the server left off */
1188 argp->offset += resp->count;
1189 argp->pgbase += resp->count;
1190 argp->count -= resp->count;
1192 /* Resend as a stable write in order to avoid
1193 * headaches in the case of a server crash.
1195 argp->stable = NFS_FILE_SYNC;
1197 nfs_restart_rpc(task, server->nfs_client);
1200 if (time_before(complain, jiffies)) {
1202 "NFS: Server wrote zero bytes, expected %u.\n",
1204 complain = jiffies + 300 * HZ;
1206 /* Can't do anything about it except throw an error. */
1207 task->tk_status = -EIO;
1213 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1214 static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
1216 if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
1218 if (may_wait && !out_of_line_wait_on_bit_lock(&nfsi->flags,
1219 NFS_INO_COMMIT, nfs_wait_bit_killable,
1225 static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
1227 clear_bit(NFS_INO_COMMIT, &nfsi->flags);
1228 smp_mb__after_clear_bit();
1229 wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
1233 static void nfs_commitdata_release(void *data)
1235 struct nfs_write_data *wdata = data;
1237 put_nfs_open_context(wdata->args.context);
1238 nfs_commit_free(wdata);
1242 * Set up the argument/result storage required for the RPC call.
1244 static int nfs_commit_rpcsetup(struct list_head *head,
1245 struct nfs_write_data *data,
1248 struct nfs_page *first = nfs_list_entry(head->next);
1249 struct inode *inode = first->wb_context->path.dentry->d_inode;
1250 int priority = flush_task_priority(how);
1251 struct rpc_task *task;
1252 struct rpc_message msg = {
1253 .rpc_argp = &data->args,
1254 .rpc_resp = &data->res,
1255 .rpc_cred = first->wb_context->cred,
1257 struct rpc_task_setup task_setup_data = {
1258 .task = &data->task,
1259 .rpc_client = NFS_CLIENT(inode),
1260 .rpc_message = &msg,
1261 .callback_ops = &nfs_commit_ops,
1262 .callback_data = data,
1263 .workqueue = nfsiod_workqueue,
1264 .flags = RPC_TASK_ASYNC,
1265 .priority = priority,
1268 /* Set up the RPC argument and reply structs
1269 * NB: take care not to mess about with data->commit et al. */
1271 list_splice_init(head, &data->pages);
1273 data->inode = inode;
1274 data->cred = msg.rpc_cred;
1276 data->args.fh = NFS_FH(data->inode);
1277 /* Note: we always request a commit of the entire inode */
1278 data->args.offset = 0;
1279 data->args.count = 0;
1280 data->args.context = get_nfs_open_context(first->wb_context);
1281 data->res.count = 0;
1282 data->res.fattr = &data->fattr;
1283 data->res.verf = &data->verf;
1284 nfs_fattr_init(&data->fattr);
1286 /* Set up the initial task struct. */
1287 NFS_PROTO(inode)->commit_setup(data, &msg);
1289 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1291 task = rpc_run_task(&task_setup_data);
1293 return PTR_ERR(task);
1299 * Commit dirty pages
1302 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1304 struct nfs_write_data *data;
1305 struct nfs_page *req;
1307 data = nfs_commitdata_alloc();
1312 /* Set up the argument struct */
1313 return nfs_commit_rpcsetup(head, data, how);
1315 while (!list_empty(head)) {
1316 req = nfs_list_entry(head->next);
1317 nfs_list_remove_request(req);
1318 nfs_mark_request_commit(req);
1319 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1320 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1322 nfs_clear_page_tag_locked(req);
1324 nfs_commit_clear_lock(NFS_I(inode));
1329 * COMMIT call returned
1331 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1333 struct nfs_write_data *data = calldata;
1335 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1336 task->tk_pid, task->tk_status);
1338 /* Call the NFS version-specific code */
1339 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1343 static void nfs_commit_release(void *calldata)
1345 struct nfs_write_data *data = calldata;
1346 struct nfs_page *req;
1347 int status = data->task.tk_status;
1349 while (!list_empty(&data->pages)) {
1350 req = nfs_list_entry(data->pages.next);
1351 nfs_list_remove_request(req);
1352 nfs_clear_request_commit(req);
1354 dprintk("NFS: commit (%s/%lld %d@%lld)",
1355 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1356 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1358 (long long)req_offset(req));
1360 nfs_context_set_write_error(req->wb_context, status);
1361 nfs_inode_remove_request(req);
1362 dprintk(", error = %d\n", status);
1366 /* Okay, COMMIT succeeded, apparently. Check the verifier
1367 * returned by the server against all stored verfs. */
1368 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1369 /* We have a match */
1370 nfs_inode_remove_request(req);
1374 /* We have a mismatch. Write the page again */
1375 dprintk(" mismatch\n");
1376 nfs_mark_request_dirty(req);
1378 nfs_clear_page_tag_locked(req);
1380 nfs_commit_clear_lock(NFS_I(data->inode));
1381 nfs_commitdata_release(calldata);
1384 static const struct rpc_call_ops nfs_commit_ops = {
1385 #if defined(CONFIG_NFS_V4_1)
1386 .rpc_call_prepare = nfs_write_prepare,
1387 #endif /* CONFIG_NFS_V4_1 */
1388 .rpc_call_done = nfs_commit_done,
1389 .rpc_release = nfs_commit_release,
1392 int nfs_commit_inode(struct inode *inode, int how)
1395 int may_wait = how & FLUSH_SYNC;
1398 if (!nfs_commit_set_lock(NFS_I(inode), may_wait))
1399 goto out_mark_dirty;
1400 spin_lock(&inode->i_lock);
1401 res = nfs_scan_commit(inode, &head, 0, 0);
1402 spin_unlock(&inode->i_lock);
1404 int error = nfs_commit_list(inode, &head, how);
1408 wait_on_bit(&NFS_I(inode)->flags, NFS_INO_COMMIT,
1409 nfs_wait_bit_killable,
1412 goto out_mark_dirty;
1414 nfs_commit_clear_lock(NFS_I(inode));
1416 /* Note: If we exit without ensuring that the commit is complete,
1417 * we must mark the inode as dirty. Otherwise, future calls to
1418 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1419 * that the data is on the disk.
1422 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
1426 static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
1428 struct nfs_inode *nfsi = NFS_I(inode);
1429 int flags = FLUSH_SYNC;
1432 if (wbc->sync_mode == WB_SYNC_NONE) {
1433 /* Don't commit yet if this is a non-blocking flush and there
1434 * are a lot of outstanding writes for this mapping.
1436 if (nfsi->ncommit <= (nfsi->npages >> 1))
1437 goto out_mark_dirty;
1439 /* don't wait for the COMMIT response */
1443 ret = nfs_commit_inode(inode, flags);
1445 if (wbc->sync_mode == WB_SYNC_NONE) {
1446 if (ret < wbc->nr_to_write)
1447 wbc->nr_to_write -= ret;
1449 wbc->nr_to_write = 0;
1454 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
1458 static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
1464 int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
1466 return nfs_commit_unstable_pages(inode, wbc);
1470 * flush the inode to disk.
1472 int nfs_wb_all(struct inode *inode)
1474 struct writeback_control wbc = {
1475 .sync_mode = WB_SYNC_ALL,
1476 .nr_to_write = LONG_MAX,
1478 .range_end = LLONG_MAX,
1481 return sync_inode(inode, &wbc);
1484 int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1486 struct nfs_page *req;
1489 BUG_ON(!PageLocked(page));
1491 wait_on_page_writeback(page);
1492 req = nfs_page_find_request(page);
1495 if (nfs_lock_request_dontget(req)) {
1496 nfs_inode_remove_request(req);
1498 * In case nfs_inode_remove_request has marked the
1499 * page as being dirty
1501 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1502 nfs_unlock_request(req);
1505 ret = nfs_wait_on_request(req);
1506 nfs_release_request(req);
1514 * Write back all requests on one page - we do this before reading it.
1516 int nfs_wb_page(struct inode *inode, struct page *page)
1518 loff_t range_start = page_offset(page);
1519 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1520 struct writeback_control wbc = {
1521 .sync_mode = WB_SYNC_ALL,
1523 .range_start = range_start,
1524 .range_end = range_end,
1529 wait_on_page_writeback(page);
1530 if (clear_page_dirty_for_io(page)) {
1531 ret = nfs_writepage_locked(page, &wbc);
1536 if (!PagePrivate(page))
1538 ret = nfs_commit_inode(inode, FLUSH_SYNC);
1547 #ifdef CONFIG_MIGRATION
1548 int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1551 struct nfs_page *req;
1554 nfs_fscache_release_page(page, GFP_KERNEL);
1556 req = nfs_find_and_lock_request(page, false);
1561 ret = migrate_page(mapping, newpage, page);
1566 page_cache_get(newpage);
1567 spin_lock(&mapping->host->i_lock);
1568 req->wb_page = newpage;
1569 SetPagePrivate(newpage);
1570 set_page_private(newpage, (unsigned long)req);
1571 ClearPagePrivate(page);
1572 set_page_private(page, 0);
1573 spin_unlock(&mapping->host->i_lock);
1574 page_cache_release(page);
1576 nfs_clear_page_tag_locked(req);
1582 int __init nfs_init_writepagecache(void)
1584 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1585 sizeof(struct nfs_write_data),
1586 0, SLAB_HWCACHE_ALIGN,
1588 if (nfs_wdata_cachep == NULL)
1591 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1593 if (nfs_wdata_mempool == NULL)
1596 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1598 if (nfs_commit_mempool == NULL)
1602 * NFS congestion size, scale with available memory.
1614 * This allows larger machines to have larger/more transfers.
1615 * Limit the default to 256M
1617 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1618 if (nfs_congestion_kb > 256*1024)
1619 nfs_congestion_kb = 256*1024;
1624 void nfs_destroy_writepagecache(void)
1626 mempool_destroy(nfs_commit_mempool);
1627 mempool_destroy(nfs_wdata_mempool);
1628 kmem_cache_destroy(nfs_wdata_cachep);