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"
33 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
35 #define MIN_POOL_WRITE (32)
36 #define MIN_POOL_COMMIT (4)
39 * Local function declarations
41 static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc,
42 struct inode *inode, int ioflags);
43 static void nfs_redirty_request(struct nfs_page *req);
44 static const struct rpc_call_ops nfs_write_partial_ops;
45 static const struct rpc_call_ops nfs_write_full_ops;
46 static const struct rpc_call_ops nfs_commit_ops;
48 static struct kmem_cache *nfs_wdata_cachep;
49 static mempool_t *nfs_wdata_mempool;
50 static mempool_t *nfs_commit_mempool;
52 struct nfs_write_data *nfs_commitdata_alloc(void)
54 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
57 memset(p, 0, sizeof(*p));
58 INIT_LIST_HEAD(&p->pages);
63 void nfs_commit_free(struct nfs_write_data *p)
65 if (p && (p->pagevec != &p->page_array[0]))
67 mempool_free(p, nfs_commit_mempool);
70 struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
72 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
75 memset(p, 0, sizeof(*p));
76 INIT_LIST_HEAD(&p->pages);
77 p->npages = pagecount;
78 if (pagecount <= ARRAY_SIZE(p->page_array))
79 p->pagevec = p->page_array;
81 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
83 mempool_free(p, nfs_wdata_mempool);
91 void nfs_writedata_free(struct nfs_write_data *p)
93 if (p && (p->pagevec != &p->page_array[0]))
95 mempool_free(p, nfs_wdata_mempool);
98 static void nfs_writedata_release(struct nfs_write_data *wdata)
100 put_lseg(wdata->lseg);
101 put_nfs_open_context(wdata->args.context);
102 nfs_writedata_free(wdata);
105 static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
109 set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
112 static struct nfs_page *nfs_page_find_request_locked(struct page *page)
114 struct nfs_page *req = NULL;
116 if (PagePrivate(page)) {
117 req = (struct nfs_page *)page_private(page);
119 kref_get(&req->wb_kref);
124 static struct nfs_page *nfs_page_find_request(struct page *page)
126 struct inode *inode = page->mapping->host;
127 struct nfs_page *req = NULL;
129 spin_lock(&inode->i_lock);
130 req = nfs_page_find_request_locked(page);
131 spin_unlock(&inode->i_lock);
135 /* Adjust the file length if we're writing beyond the end */
136 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
138 struct inode *inode = page->mapping->host;
142 spin_lock(&inode->i_lock);
143 i_size = i_size_read(inode);
144 end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
145 if (i_size > 0 && page->index < end_index)
147 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
150 i_size_write(inode, end);
151 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
153 spin_unlock(&inode->i_lock);
156 /* A writeback failed: mark the page as bad, and invalidate the page cache */
157 static void nfs_set_pageerror(struct page *page)
160 nfs_zap_mapping(page->mapping->host, page->mapping);
163 /* We can set the PG_uptodate flag if we see that a write request
164 * covers the full page.
166 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
168 if (PageUptodate(page))
172 if (count != nfs_page_length(page))
174 SetPageUptodate(page);
177 static int wb_priority(struct writeback_control *wbc)
179 if (wbc->for_reclaim)
180 return FLUSH_HIGHPRI | FLUSH_STABLE;
181 if (wbc->for_kupdate || wbc->for_background)
182 return FLUSH_LOWPRI | FLUSH_COND_STABLE;
183 return FLUSH_COND_STABLE;
187 * NFS congestion control
190 int nfs_congestion_kb;
192 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
193 #define NFS_CONGESTION_OFF_THRESH \
194 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
196 static int nfs_set_page_writeback(struct page *page)
198 int ret = test_set_page_writeback(page);
201 struct inode *inode = page->mapping->host;
202 struct nfs_server *nfss = NFS_SERVER(inode);
204 page_cache_get(page);
205 if (atomic_long_inc_return(&nfss->writeback) >
206 NFS_CONGESTION_ON_THRESH) {
207 set_bdi_congested(&nfss->backing_dev_info,
214 static void nfs_end_page_writeback(struct page *page)
216 struct inode *inode = page->mapping->host;
217 struct nfs_server *nfss = NFS_SERVER(inode);
219 end_page_writeback(page);
220 page_cache_release(page);
221 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
222 clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
225 static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock)
227 struct inode *inode = page->mapping->host;
228 struct nfs_page *req;
231 spin_lock(&inode->i_lock);
233 req = nfs_page_find_request_locked(page);
236 if (nfs_set_page_tag_locked(req))
238 /* Note: If we hold the page lock, as is the case in nfs_writepage,
239 * then the call to nfs_set_page_tag_locked() will always
240 * succeed provided that someone hasn't already marked the
241 * request as dirty (in which case we don't care).
243 spin_unlock(&inode->i_lock);
245 ret = nfs_wait_on_request(req);
248 nfs_release_request(req);
251 spin_lock(&inode->i_lock);
253 spin_unlock(&inode->i_lock);
258 * Find an associated nfs write request, and prepare to flush it out
259 * May return an error if the user signalled nfs_wait_on_request().
261 static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
262 struct page *page, bool nonblock)
264 struct nfs_page *req;
267 req = nfs_find_and_lock_request(page, nonblock);
274 ret = nfs_set_page_writeback(page);
276 BUG_ON(test_bit(PG_CLEAN, &req->wb_flags));
278 if (!nfs_pageio_add_request(pgio, req)) {
279 nfs_redirty_request(req);
280 ret = pgio->pg_error;
286 static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
288 struct inode *inode = page->mapping->host;
291 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
292 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
294 nfs_pageio_cond_complete(pgio, page->index);
295 ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
296 if (ret == -EAGAIN) {
297 redirty_page_for_writepage(wbc, page);
304 * Write an mmapped page to the server.
306 static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
308 struct nfs_pageio_descriptor pgio;
311 nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
312 err = nfs_do_writepage(page, wbc, &pgio);
313 nfs_pageio_complete(&pgio);
316 if (pgio.pg_error < 0)
317 return pgio.pg_error;
321 int nfs_writepage(struct page *page, struct writeback_control *wbc)
325 ret = nfs_writepage_locked(page, wbc);
330 static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
334 ret = nfs_do_writepage(page, wbc, data);
339 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
341 struct inode *inode = mapping->host;
342 unsigned long *bitlock = &NFS_I(inode)->flags;
343 struct nfs_pageio_descriptor pgio;
346 /* Stop dirtying of new pages while we sync */
347 err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
348 nfs_wait_bit_killable, TASK_KILLABLE);
352 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
354 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
355 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
356 nfs_pageio_complete(&pgio);
358 clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
359 smp_mb__after_clear_bit();
360 wake_up_bit(bitlock, NFS_INO_FLUSHING);
373 * Insert a write request into an inode
375 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
377 struct nfs_inode *nfsi = NFS_I(inode);
380 error = radix_tree_preload(GFP_NOFS);
384 /* Lock the request! */
385 nfs_lock_request_dontget(req);
387 spin_lock(&inode->i_lock);
388 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
392 if (nfs_have_delegation(inode, FMODE_WRITE))
395 set_bit(PG_MAPPED, &req->wb_flags);
396 SetPagePrivate(req->wb_page);
397 set_page_private(req->wb_page, (unsigned long)req);
399 kref_get(&req->wb_kref);
400 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
401 NFS_PAGE_TAG_LOCKED);
402 spin_unlock(&inode->i_lock);
403 radix_tree_preload_end();
409 * Remove a write request from an inode
411 static void nfs_inode_remove_request(struct nfs_page *req)
413 struct inode *inode = req->wb_context->path.dentry->d_inode;
414 struct nfs_inode *nfsi = NFS_I(inode);
416 BUG_ON (!NFS_WBACK_BUSY(req));
418 spin_lock(&inode->i_lock);
419 set_page_private(req->wb_page, 0);
420 ClearPagePrivate(req->wb_page);
421 clear_bit(PG_MAPPED, &req->wb_flags);
422 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
425 spin_unlock(&inode->i_lock);
428 spin_unlock(&inode->i_lock);
429 nfs_release_request(req);
433 nfs_mark_request_dirty(struct nfs_page *req)
435 __set_page_dirty_nobuffers(req->wb_page);
436 __mark_inode_dirty(req->wb_page->mapping->host, I_DIRTY_DATASYNC);
439 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
441 * Add a request to the inode's commit list.
444 nfs_mark_request_commit(struct nfs_page *req)
446 struct inode *inode = req->wb_context->path.dentry->d_inode;
447 struct nfs_inode *nfsi = NFS_I(inode);
449 spin_lock(&inode->i_lock);
450 set_bit(PG_CLEAN, &(req)->wb_flags);
451 radix_tree_tag_set(&nfsi->nfs_page_tree,
453 NFS_PAGE_TAG_COMMIT);
455 spin_unlock(&inode->i_lock);
456 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
457 inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
458 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
462 nfs_clear_request_commit(struct nfs_page *req)
464 struct page *page = req->wb_page;
466 if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) {
467 dec_zone_page_state(page, NR_UNSTABLE_NFS);
468 dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
475 int nfs_write_need_commit(struct nfs_write_data *data)
477 return data->verf.committed != NFS_FILE_SYNC;
481 int nfs_reschedule_unstable_write(struct nfs_page *req)
483 if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) {
484 nfs_mark_request_commit(req);
487 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
488 nfs_mark_request_dirty(req);
495 nfs_mark_request_commit(struct nfs_page *req)
500 nfs_clear_request_commit(struct nfs_page *req)
506 int nfs_write_need_commit(struct nfs_write_data *data)
512 int nfs_reschedule_unstable_write(struct nfs_page *req)
518 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
520 nfs_need_commit(struct nfs_inode *nfsi)
522 return radix_tree_tagged(&nfsi->nfs_page_tree, NFS_PAGE_TAG_COMMIT);
526 * nfs_scan_commit - Scan an inode for commit requests
527 * @inode: NFS inode to scan
528 * @dst: destination list
529 * @idx_start: lower bound of page->index to scan.
530 * @npages: idx_start + npages sets the upper bound to scan.
532 * Moves requests from the inode's 'commit' request list.
533 * The requests are *not* checked to ensure that they form a contiguous set.
536 nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
538 struct nfs_inode *nfsi = NFS_I(inode);
541 if (!nfs_need_commit(nfsi))
544 ret = nfs_scan_list(nfsi, dst, idx_start, npages, NFS_PAGE_TAG_COMMIT);
546 nfsi->ncommit -= ret;
547 if (nfs_need_commit(NFS_I(inode)))
548 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
552 static inline int nfs_need_commit(struct nfs_inode *nfsi)
557 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
564 * Search for an existing write request, and attempt to update
565 * it to reflect a new dirty region on a given page.
567 * If the attempt fails, then the existing request is flushed out
570 static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
575 struct nfs_page *req;
580 if (!PagePrivate(page))
583 end = offset + bytes;
584 spin_lock(&inode->i_lock);
587 req = nfs_page_find_request_locked(page);
591 rqend = req->wb_offset + req->wb_bytes;
593 * Tell the caller to flush out the request if
594 * the offsets are non-contiguous.
595 * Note: nfs_flush_incompatible() will already
596 * have flushed out requests having wrong owners.
599 || end < req->wb_offset)
602 if (nfs_set_page_tag_locked(req))
605 /* The request is locked, so wait and then retry */
606 spin_unlock(&inode->i_lock);
607 error = nfs_wait_on_request(req);
608 nfs_release_request(req);
611 spin_lock(&inode->i_lock);
614 if (nfs_clear_request_commit(req) &&
615 radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree,
616 req->wb_index, NFS_PAGE_TAG_COMMIT) != NULL)
617 NFS_I(inode)->ncommit--;
619 /* Okay, the request matches. Update the region */
620 if (offset < req->wb_offset) {
621 req->wb_offset = offset;
622 req->wb_pgbase = offset;
625 req->wb_bytes = end - req->wb_offset;
627 req->wb_bytes = rqend - req->wb_offset;
629 spin_unlock(&inode->i_lock);
632 spin_unlock(&inode->i_lock);
633 nfs_release_request(req);
634 error = nfs_wb_page(inode, page);
636 return ERR_PTR(error);
640 * Try to update an existing write request, or create one if there is none.
642 * Note: Should always be called with the Page Lock held to prevent races
643 * if we have to add a new request. Also assumes that the caller has
644 * already called nfs_flush_incompatible() if necessary.
646 static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
647 struct page *page, unsigned int offset, unsigned int bytes)
649 struct inode *inode = page->mapping->host;
650 struct nfs_page *req;
653 req = nfs_try_to_update_request(inode, page, offset, bytes);
656 req = nfs_create_request(ctx, inode, page, offset, bytes);
659 error = nfs_inode_add_request(inode, req);
661 nfs_release_request(req);
662 req = ERR_PTR(error);
668 static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
669 unsigned int offset, unsigned int count)
671 struct nfs_page *req;
673 req = nfs_setup_write_request(ctx, page, offset, count);
676 nfs_mark_request_dirty(req);
677 /* Update file length */
678 nfs_grow_file(page, offset, count);
679 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
680 nfs_mark_request_dirty(req);
681 nfs_clear_page_tag_locked(req);
685 int nfs_flush_incompatible(struct file *file, struct page *page)
687 struct nfs_open_context *ctx = nfs_file_open_context(file);
688 struct nfs_page *req;
689 int do_flush, status;
691 * Look for a request corresponding to this page. If there
692 * is one, and it belongs to another file, we flush it out
693 * before we try to copy anything into the page. Do this
694 * due to the lack of an ACCESS-type call in NFSv2.
695 * Also do the same if we find a request from an existing
699 req = nfs_page_find_request(page);
702 do_flush = req->wb_page != page || req->wb_context != ctx ||
703 req->wb_lock_context->lockowner != current->files ||
704 req->wb_lock_context->pid != current->tgid;
705 nfs_release_request(req);
708 status = nfs_wb_page(page->mapping->host, page);
709 } while (status == 0);
714 * If the page cache is marked as unsafe or invalid, then we can't rely on
715 * the PageUptodate() flag. In this case, we will need to turn off
716 * write optimisations that depend on the page contents being correct.
718 static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
720 return PageUptodate(page) &&
721 !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
725 * Update and possibly write a cached page of an NFS file.
727 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
728 * things with a page scheduled for an RPC call (e.g. invalidate it).
730 int nfs_updatepage(struct file *file, struct page *page,
731 unsigned int offset, unsigned int count)
733 struct nfs_open_context *ctx = nfs_file_open_context(file);
734 struct inode *inode = page->mapping->host;
737 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
739 dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n",
740 file->f_path.dentry->d_parent->d_name.name,
741 file->f_path.dentry->d_name.name, count,
742 (long long)(page_offset(page) + offset));
744 /* If we're not using byte range locks, and we know the page
745 * is up to date, it may be more efficient to extend the write
746 * to cover the entire page in order to avoid fragmentation
749 if (nfs_write_pageuptodate(page, inode) &&
750 inode->i_flock == NULL &&
751 !(file->f_flags & O_DSYNC)) {
752 count = max(count + offset, nfs_page_length(page));
756 status = nfs_writepage_setup(ctx, page, offset, count);
758 nfs_set_pageerror(page);
760 dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
761 status, (long long)i_size_read(inode));
765 static void nfs_writepage_release(struct nfs_page *req)
767 struct page *page = req->wb_page;
769 if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req))
770 nfs_inode_remove_request(req);
771 nfs_clear_page_tag_locked(req);
772 nfs_end_page_writeback(page);
775 static int flush_task_priority(int how)
777 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
779 return RPC_PRIORITY_HIGH;
781 return RPC_PRIORITY_LOW;
783 return RPC_PRIORITY_NORMAL;
786 int nfs_initiate_write(struct nfs_write_data *data,
787 struct rpc_clnt *clnt,
788 const struct rpc_call_ops *call_ops,
791 struct inode *inode = data->inode;
792 int priority = flush_task_priority(how);
793 struct rpc_task *task;
794 struct rpc_message msg = {
795 .rpc_argp = &data->args,
796 .rpc_resp = &data->res,
797 .rpc_cred = data->cred,
799 struct rpc_task_setup task_setup_data = {
803 .callback_ops = call_ops,
804 .callback_data = data,
805 .workqueue = nfsiod_workqueue,
806 .flags = RPC_TASK_ASYNC,
807 .priority = priority,
811 /* Set up the initial task struct. */
812 NFS_PROTO(inode)->write_setup(data, &msg);
814 dprintk("NFS: %5u initiated write call "
815 "(req %s/%lld, %u bytes @ offset %llu)\n",
818 (long long)NFS_FILEID(inode),
820 (unsigned long long)data->args.offset);
822 task = rpc_run_task(&task_setup_data);
827 if (how & FLUSH_SYNC) {
828 ret = rpc_wait_for_completion_task(task);
830 ret = task->tk_status;
836 EXPORT_SYMBOL_GPL(nfs_initiate_write);
839 * Set up the argument/result storage required for the RPC call.
841 static int nfs_write_rpcsetup(struct nfs_page *req,
842 struct nfs_write_data *data,
843 const struct rpc_call_ops *call_ops,
844 unsigned int count, unsigned int offset,
845 struct pnfs_layout_segment *lseg,
848 struct inode *inode = req->wb_context->path.dentry->d_inode;
850 /* Set up the RPC argument and reply structs
851 * NB: take care not to mess about with data->commit et al. */
854 data->inode = inode = req->wb_context->path.dentry->d_inode;
855 data->cred = req->wb_context->cred;
856 data->lseg = get_lseg(lseg);
858 data->args.fh = NFS_FH(inode);
859 data->args.offset = req_offset(req) + offset;
860 data->args.pgbase = req->wb_pgbase + offset;
861 data->args.pages = data->pagevec;
862 data->args.count = count;
863 data->args.context = get_nfs_open_context(req->wb_context);
864 data->args.lock_context = req->wb_lock_context;
865 data->args.stable = NFS_UNSTABLE;
866 if (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
867 data->args.stable = NFS_DATA_SYNC;
868 if (!nfs_need_commit(NFS_I(inode)))
869 data->args.stable = NFS_FILE_SYNC;
872 data->res.fattr = &data->fattr;
873 data->res.count = count;
874 data->res.verf = &data->verf;
875 nfs_fattr_init(&data->fattr);
878 (pnfs_try_to_write_data(data, call_ops, how) == PNFS_ATTEMPTED))
881 return nfs_initiate_write(data, NFS_CLIENT(inode), call_ops, how);
884 /* If a nfs_flush_* function fails, it should remove reqs from @head and
885 * call this on each, which will prepare them to be retried on next
886 * writeback using standard nfs.
888 static void nfs_redirty_request(struct nfs_page *req)
890 struct page *page = req->wb_page;
892 nfs_mark_request_dirty(req);
893 nfs_clear_page_tag_locked(req);
894 nfs_end_page_writeback(page);
898 * Generate multiple small requests to write out a single
899 * contiguous dirty area on one page.
901 static int nfs_flush_multi(struct nfs_pageio_descriptor *desc)
903 struct nfs_page *req = nfs_list_entry(desc->pg_list.next);
904 struct page *page = req->wb_page;
905 struct nfs_write_data *data;
906 size_t wsize = NFS_SERVER(desc->pg_inode)->wsize, nbytes;
910 struct pnfs_layout_segment *lseg;
913 nfs_list_remove_request(req);
915 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
916 (desc->pg_moreio || NFS_I(desc->pg_inode)->ncommit ||
917 desc->pg_count > wsize))
918 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
921 nbytes = desc->pg_count;
923 size_t len = min(nbytes, wsize);
925 data = nfs_writedata_alloc(1);
928 list_add(&data->pages, &list);
931 } while (nbytes != 0);
932 atomic_set(&req->wb_complete, requests);
934 BUG_ON(desc->pg_lseg);
935 lseg = pnfs_update_layout(desc->pg_inode, req->wb_context, IOMODE_RW);
936 ClearPageError(page);
938 nbytes = desc->pg_count;
942 data = list_entry(list.next, struct nfs_write_data, pages);
943 list_del_init(&data->pages);
945 data->pagevec[0] = page;
949 ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
950 wsize, offset, lseg, desc->pg_ioflags);
955 } while (nbytes != 0);
958 desc->pg_lseg = NULL;
962 while (!list_empty(&list)) {
963 data = list_entry(list.next, struct nfs_write_data, pages);
964 list_del(&data->pages);
965 nfs_writedata_free(data);
967 nfs_redirty_request(req);
972 * Create an RPC task for the given write request and kick it.
973 * The page must have been locked by the caller.
975 * It may happen that the page we're passed is not marked dirty.
976 * This is the case if nfs_updatepage detects a conflicting request
977 * that has been written but not committed.
979 static int nfs_flush_one(struct nfs_pageio_descriptor *desc)
981 struct nfs_page *req;
983 struct nfs_write_data *data;
984 struct list_head *head = &desc->pg_list;
985 struct pnfs_layout_segment *lseg = desc->pg_lseg;
988 data = nfs_writedata_alloc(nfs_page_array_len(desc->pg_base,
991 while (!list_empty(head)) {
992 req = nfs_list_entry(head->next);
993 nfs_list_remove_request(req);
994 nfs_redirty_request(req);
999 pages = data->pagevec;
1000 while (!list_empty(head)) {
1001 req = nfs_list_entry(head->next);
1002 nfs_list_remove_request(req);
1003 nfs_list_add_request(req, &data->pages);
1004 ClearPageError(req->wb_page);
1005 *pages++ = req->wb_page;
1007 req = nfs_list_entry(data->pages.next);
1008 if ((!lseg) && list_is_singular(&data->pages))
1009 lseg = pnfs_update_layout(desc->pg_inode, req->wb_context, IOMODE_RW);
1011 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1012 (desc->pg_moreio || NFS_I(desc->pg_inode)->ncommit))
1013 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1015 /* Set up the argument struct */
1016 ret = nfs_write_rpcsetup(req, data, &nfs_write_full_ops, desc->pg_count, 0, lseg, desc->pg_ioflags);
1018 put_lseg(lseg); /* Cleans any gotten in ->pg_test */
1019 desc->pg_lseg = NULL;
1023 static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1024 struct inode *inode, int ioflags)
1026 size_t wsize = NFS_SERVER(inode)->wsize;
1028 pnfs_pageio_init_write(pgio, inode);
1030 if (wsize < PAGE_CACHE_SIZE)
1031 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
1033 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
1037 * Handle a write reply that flushed part of a page.
1039 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
1041 struct nfs_write_data *data = calldata;
1043 dprintk("NFS: %5u write(%s/%lld %d@%lld)",
1045 data->req->wb_context->path.dentry->d_inode->i_sb->s_id,
1047 NFS_FILEID(data->req->wb_context->path.dentry->d_inode),
1048 data->req->wb_bytes, (long long)req_offset(data->req));
1050 nfs_writeback_done(task, data);
1053 static void nfs_writeback_release_partial(void *calldata)
1055 struct nfs_write_data *data = calldata;
1056 struct nfs_page *req = data->req;
1057 struct page *page = req->wb_page;
1058 int status = data->task.tk_status;
1061 nfs_set_pageerror(page);
1062 nfs_context_set_write_error(req->wb_context, status);
1063 dprintk(", error = %d\n", status);
1067 if (nfs_write_need_commit(data)) {
1068 struct inode *inode = page->mapping->host;
1070 spin_lock(&inode->i_lock);
1071 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
1072 /* Do nothing we need to resend the writes */
1073 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1074 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1075 dprintk(" defer commit\n");
1076 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1077 set_bit(PG_NEED_RESCHED, &req->wb_flags);
1078 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
1079 dprintk(" server reboot detected\n");
1081 spin_unlock(&inode->i_lock);
1086 if (atomic_dec_and_test(&req->wb_complete))
1087 nfs_writepage_release(req);
1088 nfs_writedata_release(calldata);
1091 #if defined(CONFIG_NFS_V4_1)
1092 void nfs_write_prepare(struct rpc_task *task, void *calldata)
1094 struct nfs_write_data *data = calldata;
1096 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
1097 &data->args.seq_args,
1098 &data->res.seq_res, 1, task))
1100 rpc_call_start(task);
1102 #endif /* CONFIG_NFS_V4_1 */
1104 static const struct rpc_call_ops nfs_write_partial_ops = {
1105 #if defined(CONFIG_NFS_V4_1)
1106 .rpc_call_prepare = nfs_write_prepare,
1107 #endif /* CONFIG_NFS_V4_1 */
1108 .rpc_call_done = nfs_writeback_done_partial,
1109 .rpc_release = nfs_writeback_release_partial,
1113 * Handle a write reply that flushes a whole page.
1115 * FIXME: There is an inherent race with invalidate_inode_pages and
1116 * writebacks since the page->count is kept > 1 for as long
1117 * as the page has a write request pending.
1119 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1121 struct nfs_write_data *data = calldata;
1123 nfs_writeback_done(task, data);
1126 static void nfs_writeback_release_full(void *calldata)
1128 struct nfs_write_data *data = calldata;
1129 int status = data->task.tk_status;
1131 /* Update attributes as result of writeback. */
1132 while (!list_empty(&data->pages)) {
1133 struct nfs_page *req = nfs_list_entry(data->pages.next);
1134 struct page *page = req->wb_page;
1136 nfs_list_remove_request(req);
1138 dprintk("NFS: %5u write (%s/%lld %d@%lld)",
1140 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1141 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1143 (long long)req_offset(req));
1146 nfs_set_pageerror(page);
1147 nfs_context_set_write_error(req->wb_context, status);
1148 dprintk(", error = %d\n", status);
1149 goto remove_request;
1152 if (nfs_write_need_commit(data)) {
1153 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1154 nfs_mark_request_commit(req);
1155 dprintk(" marked for commit\n");
1160 nfs_inode_remove_request(req);
1162 nfs_clear_page_tag_locked(req);
1163 nfs_end_page_writeback(page);
1165 nfs_writedata_release(calldata);
1168 static const struct rpc_call_ops nfs_write_full_ops = {
1169 #if defined(CONFIG_NFS_V4_1)
1170 .rpc_call_prepare = nfs_write_prepare,
1171 #endif /* CONFIG_NFS_V4_1 */
1172 .rpc_call_done = nfs_writeback_done_full,
1173 .rpc_release = nfs_writeback_release_full,
1178 * This function is called when the WRITE call is complete.
1180 void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1182 struct nfs_writeargs *argp = &data->args;
1183 struct nfs_writeres *resp = &data->res;
1184 struct nfs_server *server = NFS_SERVER(data->inode);
1187 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1188 task->tk_pid, task->tk_status);
1191 * ->write_done will attempt to use post-op attributes to detect
1192 * conflicting writes by other clients. A strict interpretation
1193 * of close-to-open would allow us to continue caching even if
1194 * another writer had changed the file, but some applications
1195 * depend on tighter cache coherency when writing.
1197 status = NFS_PROTO(data->inode)->write_done(task, data);
1200 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1202 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1203 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1204 /* We tried a write call, but the server did not
1205 * commit data to stable storage even though we
1207 * Note: There is a known bug in Tru64 < 5.0 in which
1208 * the server reports NFS_DATA_SYNC, but performs
1209 * NFS_FILE_SYNC. We therefore implement this checking
1210 * as a dprintk() in order to avoid filling syslog.
1212 static unsigned long complain;
1214 /* Note this will print the MDS for a DS write */
1215 if (time_before(complain, jiffies)) {
1216 dprintk("NFS: faulty NFS server %s:"
1217 " (committed = %d) != (stable = %d)\n",
1218 server->nfs_client->cl_hostname,
1219 resp->verf->committed, argp->stable);
1220 complain = jiffies + 300 * HZ;
1224 /* Is this a short write? */
1225 if (task->tk_status >= 0 && resp->count < argp->count) {
1226 static unsigned long complain;
1228 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1230 /* Has the server at least made some progress? */
1231 if (resp->count != 0) {
1232 /* Was this an NFSv2 write or an NFSv3 stable write? */
1233 if (resp->verf->committed != NFS_UNSTABLE) {
1234 /* Resend from where the server left off */
1235 data->mds_offset += resp->count;
1236 argp->offset += resp->count;
1237 argp->pgbase += resp->count;
1238 argp->count -= resp->count;
1240 /* Resend as a stable write in order to avoid
1241 * headaches in the case of a server crash.
1243 argp->stable = NFS_FILE_SYNC;
1245 nfs_restart_rpc(task, server->nfs_client);
1248 if (time_before(complain, jiffies)) {
1250 "NFS: Server wrote zero bytes, expected %u.\n",
1252 complain = jiffies + 300 * HZ;
1254 /* Can't do anything about it except throw an error. */
1255 task->tk_status = -EIO;
1261 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1262 static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
1266 if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
1270 ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1272 nfs_wait_bit_killable,
1274 return (ret < 0) ? ret : 1;
1277 static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
1279 clear_bit(NFS_INO_COMMIT, &nfsi->flags);
1280 smp_mb__after_clear_bit();
1281 wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
1285 static void nfs_commitdata_release(void *data)
1287 struct nfs_write_data *wdata = data;
1289 put_nfs_open_context(wdata->args.context);
1290 nfs_commit_free(wdata);
1294 * Set up the argument/result storage required for the RPC call.
1296 static int nfs_commit_rpcsetup(struct list_head *head,
1297 struct nfs_write_data *data,
1300 struct nfs_page *first = nfs_list_entry(head->next);
1301 struct inode *inode = first->wb_context->path.dentry->d_inode;
1302 int priority = flush_task_priority(how);
1303 struct rpc_task *task;
1304 struct rpc_message msg = {
1305 .rpc_argp = &data->args,
1306 .rpc_resp = &data->res,
1307 .rpc_cred = first->wb_context->cred,
1309 struct rpc_task_setup task_setup_data = {
1310 .task = &data->task,
1311 .rpc_client = NFS_CLIENT(inode),
1312 .rpc_message = &msg,
1313 .callback_ops = &nfs_commit_ops,
1314 .callback_data = data,
1315 .workqueue = nfsiod_workqueue,
1316 .flags = RPC_TASK_ASYNC,
1317 .priority = priority,
1320 /* Set up the RPC argument and reply structs
1321 * NB: take care not to mess about with data->commit et al. */
1323 list_splice_init(head, &data->pages);
1325 data->inode = inode;
1326 data->cred = msg.rpc_cred;
1328 data->args.fh = NFS_FH(data->inode);
1329 /* Note: we always request a commit of the entire inode */
1330 data->args.offset = 0;
1331 data->args.count = 0;
1332 data->args.context = get_nfs_open_context(first->wb_context);
1333 data->res.count = 0;
1334 data->res.fattr = &data->fattr;
1335 data->res.verf = &data->verf;
1336 nfs_fattr_init(&data->fattr);
1338 /* Set up the initial task struct. */
1339 NFS_PROTO(inode)->commit_setup(data, &msg);
1341 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1343 task = rpc_run_task(&task_setup_data);
1345 return PTR_ERR(task);
1346 if (how & FLUSH_SYNC)
1347 rpc_wait_for_completion_task(task);
1353 * Commit dirty pages
1356 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1358 struct nfs_write_data *data;
1359 struct nfs_page *req;
1361 data = nfs_commitdata_alloc();
1366 /* Set up the argument struct */
1367 return nfs_commit_rpcsetup(head, data, how);
1369 while (!list_empty(head)) {
1370 req = nfs_list_entry(head->next);
1371 nfs_list_remove_request(req);
1372 nfs_mark_request_commit(req);
1373 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1374 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1376 nfs_clear_page_tag_locked(req);
1378 nfs_commit_clear_lock(NFS_I(inode));
1383 * COMMIT call returned
1385 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1387 struct nfs_write_data *data = calldata;
1389 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1390 task->tk_pid, task->tk_status);
1392 /* Call the NFS version-specific code */
1393 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1397 static void nfs_commit_release(void *calldata)
1399 struct nfs_write_data *data = calldata;
1400 struct nfs_page *req;
1401 int status = data->task.tk_status;
1403 while (!list_empty(&data->pages)) {
1404 req = nfs_list_entry(data->pages.next);
1405 nfs_list_remove_request(req);
1406 nfs_clear_request_commit(req);
1408 dprintk("NFS: commit (%s/%lld %d@%lld)",
1409 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1410 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1412 (long long)req_offset(req));
1414 nfs_context_set_write_error(req->wb_context, status);
1415 nfs_inode_remove_request(req);
1416 dprintk(", error = %d\n", status);
1420 /* Okay, COMMIT succeeded, apparently. Check the verifier
1421 * returned by the server against all stored verfs. */
1422 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1423 /* We have a match */
1424 nfs_inode_remove_request(req);
1428 /* We have a mismatch. Write the page again */
1429 dprintk(" mismatch\n");
1430 nfs_mark_request_dirty(req);
1432 nfs_clear_page_tag_locked(req);
1434 nfs_commit_clear_lock(NFS_I(data->inode));
1435 nfs_commitdata_release(calldata);
1438 static const struct rpc_call_ops nfs_commit_ops = {
1439 #if defined(CONFIG_NFS_V4_1)
1440 .rpc_call_prepare = nfs_write_prepare,
1441 #endif /* CONFIG_NFS_V4_1 */
1442 .rpc_call_done = nfs_commit_done,
1443 .rpc_release = nfs_commit_release,
1446 int nfs_commit_inode(struct inode *inode, int how)
1449 int may_wait = how & FLUSH_SYNC;
1452 res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1454 goto out_mark_dirty;
1455 spin_lock(&inode->i_lock);
1456 res = nfs_scan_commit(inode, &head, 0, 0);
1457 spin_unlock(&inode->i_lock);
1459 int error = nfs_commit_list(inode, &head, how);
1463 goto out_mark_dirty;
1464 error = wait_on_bit(&NFS_I(inode)->flags,
1466 nfs_wait_bit_killable,
1471 nfs_commit_clear_lock(NFS_I(inode));
1473 /* Note: If we exit without ensuring that the commit is complete,
1474 * we must mark the inode as dirty. Otherwise, future calls to
1475 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1476 * that the data is on the disk.
1479 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
1483 static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
1485 struct nfs_inode *nfsi = NFS_I(inode);
1486 int flags = FLUSH_SYNC;
1489 if (wbc->sync_mode == WB_SYNC_NONE) {
1490 /* Don't commit yet if this is a non-blocking flush and there
1491 * are a lot of outstanding writes for this mapping.
1493 if (nfsi->ncommit <= (nfsi->npages >> 1))
1494 goto out_mark_dirty;
1496 /* don't wait for the COMMIT response */
1500 ret = nfs_commit_inode(inode, flags);
1502 if (wbc->sync_mode == WB_SYNC_NONE) {
1503 if (ret < wbc->nr_to_write)
1504 wbc->nr_to_write -= ret;
1506 wbc->nr_to_write = 0;
1511 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
1515 static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
1521 int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
1523 return nfs_commit_unstable_pages(inode, wbc);
1527 * flush the inode to disk.
1529 int nfs_wb_all(struct inode *inode)
1531 struct writeback_control wbc = {
1532 .sync_mode = WB_SYNC_ALL,
1533 .nr_to_write = LONG_MAX,
1535 .range_end = LLONG_MAX,
1538 return sync_inode(inode, &wbc);
1541 int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1543 struct nfs_page *req;
1546 BUG_ON(!PageLocked(page));
1548 wait_on_page_writeback(page);
1549 req = nfs_page_find_request(page);
1552 if (nfs_lock_request_dontget(req)) {
1553 nfs_inode_remove_request(req);
1555 * In case nfs_inode_remove_request has marked the
1556 * page as being dirty
1558 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1559 nfs_unlock_request(req);
1562 ret = nfs_wait_on_request(req);
1563 nfs_release_request(req);
1571 * Write back all requests on one page - we do this before reading it.
1573 int nfs_wb_page(struct inode *inode, struct page *page)
1575 loff_t range_start = page_offset(page);
1576 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1577 struct writeback_control wbc = {
1578 .sync_mode = WB_SYNC_ALL,
1580 .range_start = range_start,
1581 .range_end = range_end,
1586 wait_on_page_writeback(page);
1587 if (clear_page_dirty_for_io(page)) {
1588 ret = nfs_writepage_locked(page, &wbc);
1593 if (!PagePrivate(page))
1595 ret = nfs_commit_inode(inode, FLUSH_SYNC);
1604 #ifdef CONFIG_MIGRATION
1605 int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1608 struct nfs_page *req;
1611 nfs_fscache_release_page(page, GFP_KERNEL);
1613 req = nfs_find_and_lock_request(page, false);
1618 ret = migrate_page(mapping, newpage, page);
1623 page_cache_get(newpage);
1624 spin_lock(&mapping->host->i_lock);
1625 req->wb_page = newpage;
1626 SetPagePrivate(newpage);
1627 set_page_private(newpage, (unsigned long)req);
1628 ClearPagePrivate(page);
1629 set_page_private(page, 0);
1630 spin_unlock(&mapping->host->i_lock);
1631 page_cache_release(page);
1633 nfs_clear_page_tag_locked(req);
1639 int __init nfs_init_writepagecache(void)
1641 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1642 sizeof(struct nfs_write_data),
1643 0, SLAB_HWCACHE_ALIGN,
1645 if (nfs_wdata_cachep == NULL)
1648 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1650 if (nfs_wdata_mempool == NULL)
1653 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1655 if (nfs_commit_mempool == NULL)
1659 * NFS congestion size, scale with available memory.
1671 * This allows larger machines to have larger/more transfers.
1672 * Limit the default to 256M
1674 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1675 if (nfs_congestion_kb > 256*1024)
1676 nfs_congestion_kb = 256*1024;
1681 void nfs_destroy_writepagecache(void)
1683 mempool_destroy(nfs_commit_mempool);
1684 mempool_destroy(nfs_wdata_mempool);
1685 kmem_cache_destroy(nfs_wdata_cachep);