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, struct pnfs_layout_segment *lseg)
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 pnfs_mark_request_commit(req, lseg);
457 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
458 inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
459 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
463 nfs_clear_request_commit(struct nfs_page *req)
465 struct page *page = req->wb_page;
467 if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) {
468 dec_zone_page_state(page, NR_UNSTABLE_NFS);
469 dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
476 int nfs_write_need_commit(struct nfs_write_data *data)
478 if (data->verf.committed == NFS_DATA_SYNC)
479 return data->lseg == NULL;
481 return data->verf.committed != NFS_FILE_SYNC;
485 int nfs_reschedule_unstable_write(struct nfs_page *req,
486 struct nfs_write_data *data)
488 if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) {
489 nfs_mark_request_commit(req, data->lseg);
492 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
493 nfs_mark_request_dirty(req);
500 nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg)
505 nfs_clear_request_commit(struct nfs_page *req)
511 int nfs_write_need_commit(struct nfs_write_data *data)
517 int nfs_reschedule_unstable_write(struct nfs_page *req,
518 struct nfs_write_data *data)
524 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
526 nfs_need_commit(struct nfs_inode *nfsi)
528 return radix_tree_tagged(&nfsi->nfs_page_tree, NFS_PAGE_TAG_COMMIT);
532 * nfs_scan_commit - Scan an inode for commit requests
533 * @inode: NFS inode to scan
534 * @dst: destination list
535 * @idx_start: lower bound of page->index to scan.
536 * @npages: idx_start + npages sets the upper bound to scan.
538 * Moves requests from the inode's 'commit' request list.
539 * The requests are *not* checked to ensure that they form a contiguous set.
542 nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
544 struct nfs_inode *nfsi = NFS_I(inode);
547 if (!nfs_need_commit(nfsi))
550 ret = nfs_scan_list(nfsi, dst, idx_start, npages, NFS_PAGE_TAG_COMMIT);
552 nfsi->ncommit -= ret;
553 if (nfs_need_commit(NFS_I(inode)))
554 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
558 static inline int nfs_need_commit(struct nfs_inode *nfsi)
563 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
570 * Search for an existing write request, and attempt to update
571 * it to reflect a new dirty region on a given page.
573 * If the attempt fails, then the existing request is flushed out
576 static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
581 struct nfs_page *req;
586 if (!PagePrivate(page))
589 end = offset + bytes;
590 spin_lock(&inode->i_lock);
593 req = nfs_page_find_request_locked(page);
597 rqend = req->wb_offset + req->wb_bytes;
599 * Tell the caller to flush out the request if
600 * the offsets are non-contiguous.
601 * Note: nfs_flush_incompatible() will already
602 * have flushed out requests having wrong owners.
605 || end < req->wb_offset)
608 if (nfs_set_page_tag_locked(req))
611 /* The request is locked, so wait and then retry */
612 spin_unlock(&inode->i_lock);
613 error = nfs_wait_on_request(req);
614 nfs_release_request(req);
617 spin_lock(&inode->i_lock);
620 if (nfs_clear_request_commit(req) &&
621 radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree,
622 req->wb_index, NFS_PAGE_TAG_COMMIT) != NULL) {
623 NFS_I(inode)->ncommit--;
624 pnfs_clear_request_commit(req);
627 /* Okay, the request matches. Update the region */
628 if (offset < req->wb_offset) {
629 req->wb_offset = offset;
630 req->wb_pgbase = offset;
633 req->wb_bytes = end - req->wb_offset;
635 req->wb_bytes = rqend - req->wb_offset;
637 spin_unlock(&inode->i_lock);
640 spin_unlock(&inode->i_lock);
641 nfs_release_request(req);
642 error = nfs_wb_page(inode, page);
644 return ERR_PTR(error);
648 * Try to update an existing write request, or create one if there is none.
650 * Note: Should always be called with the Page Lock held to prevent races
651 * if we have to add a new request. Also assumes that the caller has
652 * already called nfs_flush_incompatible() if necessary.
654 static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
655 struct page *page, unsigned int offset, unsigned int bytes)
657 struct inode *inode = page->mapping->host;
658 struct nfs_page *req;
661 req = nfs_try_to_update_request(inode, page, offset, bytes);
664 req = nfs_create_request(ctx, inode, page, offset, bytes);
667 error = nfs_inode_add_request(inode, req);
669 nfs_release_request(req);
670 req = ERR_PTR(error);
676 static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
677 unsigned int offset, unsigned int count)
679 struct nfs_page *req;
681 req = nfs_setup_write_request(ctx, page, offset, count);
684 nfs_mark_request_dirty(req);
685 /* Update file length */
686 nfs_grow_file(page, offset, count);
687 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
688 nfs_mark_request_dirty(req);
689 nfs_clear_page_tag_locked(req);
693 int nfs_flush_incompatible(struct file *file, struct page *page)
695 struct nfs_open_context *ctx = nfs_file_open_context(file);
696 struct nfs_page *req;
697 int do_flush, status;
699 * Look for a request corresponding to this page. If there
700 * is one, and it belongs to another file, we flush it out
701 * before we try to copy anything into the page. Do this
702 * due to the lack of an ACCESS-type call in NFSv2.
703 * Also do the same if we find a request from an existing
707 req = nfs_page_find_request(page);
710 do_flush = req->wb_page != page || req->wb_context != ctx ||
711 req->wb_lock_context->lockowner != current->files ||
712 req->wb_lock_context->pid != current->tgid;
713 nfs_release_request(req);
716 status = nfs_wb_page(page->mapping->host, page);
717 } while (status == 0);
722 * If the page cache is marked as unsafe or invalid, then we can't rely on
723 * the PageUptodate() flag. In this case, we will need to turn off
724 * write optimisations that depend on the page contents being correct.
726 static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
728 return PageUptodate(page) &&
729 !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
733 * Update and possibly write a cached page of an NFS file.
735 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
736 * things with a page scheduled for an RPC call (e.g. invalidate it).
738 int nfs_updatepage(struct file *file, struct page *page,
739 unsigned int offset, unsigned int count)
741 struct nfs_open_context *ctx = nfs_file_open_context(file);
742 struct inode *inode = page->mapping->host;
745 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
747 dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n",
748 file->f_path.dentry->d_parent->d_name.name,
749 file->f_path.dentry->d_name.name, count,
750 (long long)(page_offset(page) + offset));
752 /* If we're not using byte range locks, and we know the page
753 * is up to date, it may be more efficient to extend the write
754 * to cover the entire page in order to avoid fragmentation
757 if (nfs_write_pageuptodate(page, inode) &&
758 inode->i_flock == NULL &&
759 !(file->f_flags & O_DSYNC)) {
760 count = max(count + offset, nfs_page_length(page));
764 status = nfs_writepage_setup(ctx, page, offset, count);
766 nfs_set_pageerror(page);
768 dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
769 status, (long long)i_size_read(inode));
773 static void nfs_writepage_release(struct nfs_page *req,
774 struct nfs_write_data *data)
776 struct page *page = req->wb_page;
778 if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req, data))
779 nfs_inode_remove_request(req);
780 nfs_clear_page_tag_locked(req);
781 nfs_end_page_writeback(page);
784 static int flush_task_priority(int how)
786 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
788 return RPC_PRIORITY_HIGH;
790 return RPC_PRIORITY_LOW;
792 return RPC_PRIORITY_NORMAL;
795 int nfs_initiate_write(struct nfs_write_data *data,
796 struct rpc_clnt *clnt,
797 const struct rpc_call_ops *call_ops,
800 struct inode *inode = data->inode;
801 int priority = flush_task_priority(how);
802 struct rpc_task *task;
803 struct rpc_message msg = {
804 .rpc_argp = &data->args,
805 .rpc_resp = &data->res,
806 .rpc_cred = data->cred,
808 struct rpc_task_setup task_setup_data = {
812 .callback_ops = call_ops,
813 .callback_data = data,
814 .workqueue = nfsiod_workqueue,
815 .flags = RPC_TASK_ASYNC,
816 .priority = priority,
820 /* Set up the initial task struct. */
821 NFS_PROTO(inode)->write_setup(data, &msg);
823 dprintk("NFS: %5u initiated write call "
824 "(req %s/%lld, %u bytes @ offset %llu)\n",
827 (long long)NFS_FILEID(inode),
829 (unsigned long long)data->args.offset);
831 task = rpc_run_task(&task_setup_data);
836 if (how & FLUSH_SYNC) {
837 ret = rpc_wait_for_completion_task(task);
839 ret = task->tk_status;
845 EXPORT_SYMBOL_GPL(nfs_initiate_write);
848 * Set up the argument/result storage required for the RPC call.
850 static int nfs_write_rpcsetup(struct nfs_page *req,
851 struct nfs_write_data *data,
852 const struct rpc_call_ops *call_ops,
853 unsigned int count, unsigned int offset,
854 struct pnfs_layout_segment *lseg,
857 struct inode *inode = req->wb_context->path.dentry->d_inode;
859 /* Set up the RPC argument and reply structs
860 * NB: take care not to mess about with data->commit et al. */
863 data->inode = inode = req->wb_context->path.dentry->d_inode;
864 data->cred = req->wb_context->cred;
865 data->lseg = get_lseg(lseg);
867 data->args.fh = NFS_FH(inode);
868 data->args.offset = req_offset(req) + offset;
869 data->args.pgbase = req->wb_pgbase + offset;
870 data->args.pages = data->pagevec;
871 data->args.count = count;
872 data->args.context = get_nfs_open_context(req->wb_context);
873 data->args.lock_context = req->wb_lock_context;
874 data->args.stable = NFS_UNSTABLE;
875 if (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
876 data->args.stable = NFS_DATA_SYNC;
877 if (!nfs_need_commit(NFS_I(inode)))
878 data->args.stable = NFS_FILE_SYNC;
881 data->res.fattr = &data->fattr;
882 data->res.count = count;
883 data->res.verf = &data->verf;
884 nfs_fattr_init(&data->fattr);
887 (pnfs_try_to_write_data(data, call_ops, how) == PNFS_ATTEMPTED))
890 return nfs_initiate_write(data, NFS_CLIENT(inode), call_ops, how);
893 /* If a nfs_flush_* function fails, it should remove reqs from @head and
894 * call this on each, which will prepare them to be retried on next
895 * writeback using standard nfs.
897 static void nfs_redirty_request(struct nfs_page *req)
899 struct page *page = req->wb_page;
901 nfs_mark_request_dirty(req);
902 nfs_clear_page_tag_locked(req);
903 nfs_end_page_writeback(page);
907 * Generate multiple small requests to write out a single
908 * contiguous dirty area on one page.
910 static int nfs_flush_multi(struct nfs_pageio_descriptor *desc)
912 struct nfs_page *req = nfs_list_entry(desc->pg_list.next);
913 struct page *page = req->wb_page;
914 struct nfs_write_data *data;
915 size_t wsize = NFS_SERVER(desc->pg_inode)->wsize, nbytes;
919 struct pnfs_layout_segment *lseg;
922 nfs_list_remove_request(req);
924 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
925 (desc->pg_moreio || NFS_I(desc->pg_inode)->ncommit ||
926 desc->pg_count > wsize))
927 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
930 nbytes = desc->pg_count;
932 size_t len = min(nbytes, wsize);
934 data = nfs_writedata_alloc(1);
937 list_add(&data->pages, &list);
940 } while (nbytes != 0);
941 atomic_set(&req->wb_complete, requests);
943 BUG_ON(desc->pg_lseg);
944 lseg = pnfs_update_layout(desc->pg_inode, req->wb_context, IOMODE_RW);
945 ClearPageError(page);
947 nbytes = desc->pg_count;
951 data = list_entry(list.next, struct nfs_write_data, pages);
952 list_del_init(&data->pages);
954 data->pagevec[0] = page;
958 ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
959 wsize, offset, lseg, desc->pg_ioflags);
964 } while (nbytes != 0);
967 desc->pg_lseg = NULL;
971 while (!list_empty(&list)) {
972 data = list_entry(list.next, struct nfs_write_data, pages);
973 list_del(&data->pages);
974 nfs_writedata_free(data);
976 nfs_redirty_request(req);
981 * Create an RPC task for the given write request and kick it.
982 * The page must have been locked by the caller.
984 * It may happen that the page we're passed is not marked dirty.
985 * This is the case if nfs_updatepage detects a conflicting request
986 * that has been written but not committed.
988 static int nfs_flush_one(struct nfs_pageio_descriptor *desc)
990 struct nfs_page *req;
992 struct nfs_write_data *data;
993 struct list_head *head = &desc->pg_list;
994 struct pnfs_layout_segment *lseg = desc->pg_lseg;
997 data = nfs_writedata_alloc(nfs_page_array_len(desc->pg_base,
1000 while (!list_empty(head)) {
1001 req = nfs_list_entry(head->next);
1002 nfs_list_remove_request(req);
1003 nfs_redirty_request(req);
1008 pages = data->pagevec;
1009 while (!list_empty(head)) {
1010 req = nfs_list_entry(head->next);
1011 nfs_list_remove_request(req);
1012 nfs_list_add_request(req, &data->pages);
1013 ClearPageError(req->wb_page);
1014 *pages++ = req->wb_page;
1016 req = nfs_list_entry(data->pages.next);
1017 if ((!lseg) && list_is_singular(&data->pages))
1018 lseg = pnfs_update_layout(desc->pg_inode, req->wb_context, IOMODE_RW);
1020 if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1021 (desc->pg_moreio || NFS_I(desc->pg_inode)->ncommit))
1022 desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1024 /* Set up the argument struct */
1025 ret = nfs_write_rpcsetup(req, data, &nfs_write_full_ops, desc->pg_count, 0, lseg, desc->pg_ioflags);
1027 put_lseg(lseg); /* Cleans any gotten in ->pg_test */
1028 desc->pg_lseg = NULL;
1032 static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1033 struct inode *inode, int ioflags)
1035 size_t wsize = NFS_SERVER(inode)->wsize;
1037 pnfs_pageio_init_write(pgio, inode);
1039 if (wsize < PAGE_CACHE_SIZE)
1040 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
1042 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
1046 * Handle a write reply that flushed part of a page.
1048 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
1050 struct nfs_write_data *data = calldata;
1052 dprintk("NFS: %5u write(%s/%lld %d@%lld)",
1054 data->req->wb_context->path.dentry->d_inode->i_sb->s_id,
1056 NFS_FILEID(data->req->wb_context->path.dentry->d_inode),
1057 data->req->wb_bytes, (long long)req_offset(data->req));
1059 nfs_writeback_done(task, data);
1062 static void nfs_writeback_release_partial(void *calldata)
1064 struct nfs_write_data *data = calldata;
1065 struct nfs_page *req = data->req;
1066 struct page *page = req->wb_page;
1067 int status = data->task.tk_status;
1070 nfs_set_pageerror(page);
1071 nfs_context_set_write_error(req->wb_context, status);
1072 dprintk(", error = %d\n", status);
1076 if (nfs_write_need_commit(data)) {
1077 struct inode *inode = page->mapping->host;
1079 spin_lock(&inode->i_lock);
1080 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
1081 /* Do nothing we need to resend the writes */
1082 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1083 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1084 dprintk(" defer commit\n");
1085 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1086 set_bit(PG_NEED_RESCHED, &req->wb_flags);
1087 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
1088 dprintk(" server reboot detected\n");
1090 spin_unlock(&inode->i_lock);
1095 if (atomic_dec_and_test(&req->wb_complete))
1096 nfs_writepage_release(req, data);
1097 nfs_writedata_release(calldata);
1100 #if defined(CONFIG_NFS_V4_1)
1101 void nfs_write_prepare(struct rpc_task *task, void *calldata)
1103 struct nfs_write_data *data = calldata;
1105 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
1106 &data->args.seq_args,
1107 &data->res.seq_res, 1, task))
1109 rpc_call_start(task);
1111 #endif /* CONFIG_NFS_V4_1 */
1113 static const struct rpc_call_ops nfs_write_partial_ops = {
1114 #if defined(CONFIG_NFS_V4_1)
1115 .rpc_call_prepare = nfs_write_prepare,
1116 #endif /* CONFIG_NFS_V4_1 */
1117 .rpc_call_done = nfs_writeback_done_partial,
1118 .rpc_release = nfs_writeback_release_partial,
1122 * Handle a write reply that flushes a whole page.
1124 * FIXME: There is an inherent race with invalidate_inode_pages and
1125 * writebacks since the page->count is kept > 1 for as long
1126 * as the page has a write request pending.
1128 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1130 struct nfs_write_data *data = calldata;
1132 nfs_writeback_done(task, data);
1135 static void nfs_writeback_release_full(void *calldata)
1137 struct nfs_write_data *data = calldata;
1138 int status = data->task.tk_status;
1140 /* Update attributes as result of writeback. */
1141 while (!list_empty(&data->pages)) {
1142 struct nfs_page *req = nfs_list_entry(data->pages.next);
1143 struct page *page = req->wb_page;
1145 nfs_list_remove_request(req);
1147 dprintk("NFS: %5u write (%s/%lld %d@%lld)",
1149 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1150 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1152 (long long)req_offset(req));
1155 nfs_set_pageerror(page);
1156 nfs_context_set_write_error(req->wb_context, status);
1157 dprintk(", error = %d\n", status);
1158 goto remove_request;
1161 if (nfs_write_need_commit(data)) {
1162 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1163 nfs_mark_request_commit(req, data->lseg);
1164 dprintk(" marked for commit\n");
1169 nfs_inode_remove_request(req);
1171 nfs_clear_page_tag_locked(req);
1172 nfs_end_page_writeback(page);
1174 nfs_writedata_release(calldata);
1177 static const struct rpc_call_ops nfs_write_full_ops = {
1178 #if defined(CONFIG_NFS_V4_1)
1179 .rpc_call_prepare = nfs_write_prepare,
1180 #endif /* CONFIG_NFS_V4_1 */
1181 .rpc_call_done = nfs_writeback_done_full,
1182 .rpc_release = nfs_writeback_release_full,
1187 * This function is called when the WRITE call is complete.
1189 void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1191 struct nfs_writeargs *argp = &data->args;
1192 struct nfs_writeres *resp = &data->res;
1193 struct nfs_server *server = NFS_SERVER(data->inode);
1196 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1197 task->tk_pid, task->tk_status);
1200 * ->write_done will attempt to use post-op attributes to detect
1201 * conflicting writes by other clients. A strict interpretation
1202 * of close-to-open would allow us to continue caching even if
1203 * another writer had changed the file, but some applications
1204 * depend on tighter cache coherency when writing.
1206 status = NFS_PROTO(data->inode)->write_done(task, data);
1209 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1211 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1212 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1213 /* We tried a write call, but the server did not
1214 * commit data to stable storage even though we
1216 * Note: There is a known bug in Tru64 < 5.0 in which
1217 * the server reports NFS_DATA_SYNC, but performs
1218 * NFS_FILE_SYNC. We therefore implement this checking
1219 * as a dprintk() in order to avoid filling syslog.
1221 static unsigned long complain;
1223 /* Note this will print the MDS for a DS write */
1224 if (time_before(complain, jiffies)) {
1225 dprintk("NFS: faulty NFS server %s:"
1226 " (committed = %d) != (stable = %d)\n",
1227 server->nfs_client->cl_hostname,
1228 resp->verf->committed, argp->stable);
1229 complain = jiffies + 300 * HZ;
1233 /* Is this a short write? */
1234 if (task->tk_status >= 0 && resp->count < argp->count) {
1235 static unsigned long complain;
1237 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1239 /* Has the server at least made some progress? */
1240 if (resp->count != 0) {
1241 /* Was this an NFSv2 write or an NFSv3 stable write? */
1242 if (resp->verf->committed != NFS_UNSTABLE) {
1243 /* Resend from where the server left off */
1244 data->mds_offset += resp->count;
1245 argp->offset += resp->count;
1246 argp->pgbase += resp->count;
1247 argp->count -= resp->count;
1249 /* Resend as a stable write in order to avoid
1250 * headaches in the case of a server crash.
1252 argp->stable = NFS_FILE_SYNC;
1254 nfs_restart_rpc(task, server->nfs_client);
1257 if (time_before(complain, jiffies)) {
1259 "NFS: Server wrote zero bytes, expected %u.\n",
1261 complain = jiffies + 300 * HZ;
1263 /* Can't do anything about it except throw an error. */
1264 task->tk_status = -EIO;
1270 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1271 static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
1275 if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
1279 ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1281 nfs_wait_bit_killable,
1283 return (ret < 0) ? ret : 1;
1286 static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
1288 clear_bit(NFS_INO_COMMIT, &nfsi->flags);
1289 smp_mb__after_clear_bit();
1290 wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
1294 static void nfs_commitdata_release(void *data)
1296 struct nfs_write_data *wdata = data;
1298 put_lseg(wdata->lseg);
1299 put_nfs_open_context(wdata->args.context);
1300 nfs_commit_free(wdata);
1303 static int nfs_initiate_commit(struct nfs_write_data *data, struct rpc_clnt *clnt,
1304 const struct rpc_call_ops *call_ops,
1307 struct rpc_task *task;
1308 int priority = flush_task_priority(how);
1309 struct rpc_message msg = {
1310 .rpc_argp = &data->args,
1311 .rpc_resp = &data->res,
1312 .rpc_cred = data->cred,
1314 struct rpc_task_setup task_setup_data = {
1315 .task = &data->task,
1317 .rpc_message = &msg,
1318 .callback_ops = call_ops,
1319 .callback_data = data,
1320 .workqueue = nfsiod_workqueue,
1321 .flags = RPC_TASK_ASYNC,
1322 .priority = priority,
1324 /* Set up the initial task struct. */
1325 NFS_PROTO(data->inode)->commit_setup(data, &msg);
1327 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1329 task = rpc_run_task(&task_setup_data);
1331 return PTR_ERR(task);
1332 if (how & FLUSH_SYNC)
1333 rpc_wait_for_completion_task(task);
1339 * Set up the argument/result storage required for the RPC call.
1341 static void nfs_init_commit(struct nfs_write_data *data,
1342 struct list_head *head,
1343 struct pnfs_layout_segment *lseg)
1345 struct nfs_page *first = nfs_list_entry(head->next);
1346 struct inode *inode = first->wb_context->path.dentry->d_inode;
1348 /* Set up the RPC argument and reply structs
1349 * NB: take care not to mess about with data->commit et al. */
1351 list_splice_init(head, &data->pages);
1353 data->inode = inode;
1354 data->cred = first->wb_context->cred;
1355 data->lseg = lseg; /* reference transferred */
1356 data->mds_ops = &nfs_commit_ops;
1358 data->args.fh = NFS_FH(data->inode);
1359 /* Note: we always request a commit of the entire inode */
1360 data->args.offset = 0;
1361 data->args.count = 0;
1362 data->args.context = get_nfs_open_context(first->wb_context);
1363 data->res.count = 0;
1364 data->res.fattr = &data->fattr;
1365 data->res.verf = &data->verf;
1366 nfs_fattr_init(&data->fattr);
1369 static void nfs_retry_commit(struct list_head *page_list,
1370 struct pnfs_layout_segment *lseg)
1372 struct nfs_page *req;
1374 while (!list_empty(page_list)) {
1375 req = nfs_list_entry(page_list->next);
1376 nfs_list_remove_request(req);
1377 nfs_mark_request_commit(req, lseg);
1378 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1379 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1381 nfs_clear_page_tag_locked(req);
1386 * Commit dirty pages
1389 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1391 struct nfs_write_data *data;
1393 data = nfs_commitdata_alloc();
1398 /* Set up the argument struct */
1399 nfs_init_commit(data, head, NULL);
1400 return nfs_initiate_commit(data, NFS_CLIENT(inode), data->mds_ops, how);
1402 nfs_retry_commit(head, NULL);
1403 nfs_commit_clear_lock(NFS_I(inode));
1408 * COMMIT call returned
1410 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1412 struct nfs_write_data *data = calldata;
1414 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1415 task->tk_pid, task->tk_status);
1417 /* Call the NFS version-specific code */
1418 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1422 static void nfs_commit_release_pages(struct nfs_write_data *data)
1424 struct nfs_page *req;
1425 int status = data->task.tk_status;
1427 while (!list_empty(&data->pages)) {
1428 req = nfs_list_entry(data->pages.next);
1429 nfs_list_remove_request(req);
1430 nfs_clear_request_commit(req);
1432 dprintk("NFS: commit (%s/%lld %d@%lld)",
1433 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1434 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1436 (long long)req_offset(req));
1438 nfs_context_set_write_error(req->wb_context, status);
1439 nfs_inode_remove_request(req);
1440 dprintk(", error = %d\n", status);
1444 /* Okay, COMMIT succeeded, apparently. Check the verifier
1445 * returned by the server against all stored verfs. */
1446 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1447 /* We have a match */
1448 nfs_inode_remove_request(req);
1452 /* We have a mismatch. Write the page again */
1453 dprintk(" mismatch\n");
1454 nfs_mark_request_dirty(req);
1456 nfs_clear_page_tag_locked(req);
1460 static void nfs_commit_release(void *calldata)
1462 struct nfs_write_data *data = calldata;
1464 nfs_commit_release_pages(data);
1465 nfs_commit_clear_lock(NFS_I(data->inode));
1466 nfs_commitdata_release(calldata);
1469 static const struct rpc_call_ops nfs_commit_ops = {
1470 #if defined(CONFIG_NFS_V4_1)
1471 .rpc_call_prepare = nfs_write_prepare,
1472 #endif /* CONFIG_NFS_V4_1 */
1473 .rpc_call_done = nfs_commit_done,
1474 .rpc_release = nfs_commit_release,
1477 int nfs_commit_inode(struct inode *inode, int how)
1480 int may_wait = how & FLUSH_SYNC;
1483 res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1485 goto out_mark_dirty;
1486 spin_lock(&inode->i_lock);
1487 res = nfs_scan_commit(inode, &head, 0, 0);
1488 spin_unlock(&inode->i_lock);
1492 error = pnfs_commit_list(inode, &head, how);
1493 if (error == PNFS_NOT_ATTEMPTED)
1494 error = nfs_commit_list(inode, &head, how);
1498 goto out_mark_dirty;
1499 error = wait_on_bit(&NFS_I(inode)->flags,
1501 nfs_wait_bit_killable,
1506 nfs_commit_clear_lock(NFS_I(inode));
1508 /* Note: If we exit without ensuring that the commit is complete,
1509 * we must mark the inode as dirty. Otherwise, future calls to
1510 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1511 * that the data is on the disk.
1514 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
1518 static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
1520 struct nfs_inode *nfsi = NFS_I(inode);
1521 int flags = FLUSH_SYNC;
1524 if (wbc->sync_mode == WB_SYNC_NONE) {
1525 /* Don't commit yet if this is a non-blocking flush and there
1526 * are a lot of outstanding writes for this mapping.
1528 if (nfsi->ncommit <= (nfsi->npages >> 1))
1529 goto out_mark_dirty;
1531 /* don't wait for the COMMIT response */
1535 ret = nfs_commit_inode(inode, flags);
1537 if (wbc->sync_mode == WB_SYNC_NONE) {
1538 if (ret < wbc->nr_to_write)
1539 wbc->nr_to_write -= ret;
1541 wbc->nr_to_write = 0;
1546 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
1550 static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
1556 int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
1558 return nfs_commit_unstable_pages(inode, wbc);
1562 * flush the inode to disk.
1564 int nfs_wb_all(struct inode *inode)
1566 struct writeback_control wbc = {
1567 .sync_mode = WB_SYNC_ALL,
1568 .nr_to_write = LONG_MAX,
1570 .range_end = LLONG_MAX,
1573 return sync_inode(inode, &wbc);
1576 int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1578 struct nfs_page *req;
1581 BUG_ON(!PageLocked(page));
1583 wait_on_page_writeback(page);
1584 req = nfs_page_find_request(page);
1587 if (nfs_lock_request_dontget(req)) {
1588 nfs_inode_remove_request(req);
1590 * In case nfs_inode_remove_request has marked the
1591 * page as being dirty
1593 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1594 nfs_unlock_request(req);
1597 ret = nfs_wait_on_request(req);
1598 nfs_release_request(req);
1606 * Write back all requests on one page - we do this before reading it.
1608 int nfs_wb_page(struct inode *inode, struct page *page)
1610 loff_t range_start = page_offset(page);
1611 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1612 struct writeback_control wbc = {
1613 .sync_mode = WB_SYNC_ALL,
1615 .range_start = range_start,
1616 .range_end = range_end,
1621 wait_on_page_writeback(page);
1622 if (clear_page_dirty_for_io(page)) {
1623 ret = nfs_writepage_locked(page, &wbc);
1628 if (!PagePrivate(page))
1630 ret = nfs_commit_inode(inode, FLUSH_SYNC);
1639 #ifdef CONFIG_MIGRATION
1640 int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1643 struct nfs_page *req;
1646 nfs_fscache_release_page(page, GFP_KERNEL);
1648 req = nfs_find_and_lock_request(page, false);
1653 ret = migrate_page(mapping, newpage, page);
1658 page_cache_get(newpage);
1659 spin_lock(&mapping->host->i_lock);
1660 req->wb_page = newpage;
1661 SetPagePrivate(newpage);
1662 set_page_private(newpage, (unsigned long)req);
1663 ClearPagePrivate(page);
1664 set_page_private(page, 0);
1665 spin_unlock(&mapping->host->i_lock);
1666 page_cache_release(page);
1668 nfs_clear_page_tag_locked(req);
1674 int __init nfs_init_writepagecache(void)
1676 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1677 sizeof(struct nfs_write_data),
1678 0, SLAB_HWCACHE_ALIGN,
1680 if (nfs_wdata_cachep == NULL)
1683 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1685 if (nfs_wdata_mempool == NULL)
1688 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1690 if (nfs_commit_mempool == NULL)
1694 * NFS congestion size, scale with available memory.
1706 * This allows larger machines to have larger/more transfers.
1707 * Limit the default to 256M
1709 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1710 if (nfs_congestion_kb > 256*1024)
1711 nfs_congestion_kb = 256*1024;
1716 void nfs_destroy_writepagecache(void)
1718 mempool_destroy(nfs_commit_mempool);
1719 mempool_destroy(nfs_wdata_mempool);
1720 kmem_cache_destroy(nfs_wdata_cachep);