4 * Write file data over NFS.
6 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
9 #include <linux/types.h>
10 #include <linux/slab.h>
12 #include <linux/pagemap.h>
13 #include <linux/file.h>
14 #include <linux/writeback.h>
15 #include <linux/swap.h>
17 #include <linux/sunrpc/clnt.h>
18 #include <linux/nfs_fs.h>
19 #include <linux/nfs_mount.h>
20 #include <linux/nfs_page.h>
21 #include <linux/backing-dev.h>
23 #include <asm/uaccess.h>
25 #include "delegation.h"
29 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
31 #define MIN_POOL_WRITE (32)
32 #define MIN_POOL_COMMIT (4)
35 * Local function declarations
37 static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc,
38 struct inode *inode, int ioflags);
39 static void nfs_redirty_request(struct nfs_page *req);
40 static const struct rpc_call_ops nfs_write_partial_ops;
41 static const struct rpc_call_ops nfs_write_full_ops;
42 static const struct rpc_call_ops nfs_commit_ops;
44 static struct kmem_cache *nfs_wdata_cachep;
45 static mempool_t *nfs_wdata_mempool;
46 static mempool_t *nfs_commit_mempool;
48 struct nfs_write_data *nfs_commitdata_alloc(void)
50 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
53 memset(p, 0, sizeof(*p));
54 INIT_LIST_HEAD(&p->pages);
55 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
60 void nfs_commit_free(struct nfs_write_data *p)
62 if (p && (p->pagevec != &p->page_array[0]))
64 mempool_free(p, nfs_commit_mempool);
67 struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
69 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
72 memset(p, 0, sizeof(*p));
73 INIT_LIST_HEAD(&p->pages);
74 p->npages = pagecount;
75 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
76 if (pagecount <= ARRAY_SIZE(p->page_array))
77 p->pagevec = p->page_array;
79 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
81 mempool_free(p, nfs_wdata_mempool);
89 static void nfs_writedata_free(struct nfs_write_data *p)
91 if (p && (p->pagevec != &p->page_array[0]))
93 mempool_free(p, nfs_wdata_mempool);
96 void nfs_writedata_release(void *data)
98 struct nfs_write_data *wdata = data;
100 put_nfs_open_context(wdata->args.context);
101 nfs_writedata_free(wdata);
104 static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
108 set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
111 static struct nfs_page *nfs_page_find_request_locked(struct page *page)
113 struct nfs_page *req = NULL;
115 if (PagePrivate(page)) {
116 req = (struct nfs_page *)page_private(page);
118 kref_get(&req->wb_kref);
123 static struct nfs_page *nfs_page_find_request(struct page *page)
125 struct inode *inode = page->mapping->host;
126 struct nfs_page *req = NULL;
128 spin_lock(&inode->i_lock);
129 req = nfs_page_find_request_locked(page);
130 spin_unlock(&inode->i_lock);
134 /* Adjust the file length if we're writing beyond the end */
135 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
137 struct inode *inode = page->mapping->host;
141 spin_lock(&inode->i_lock);
142 i_size = i_size_read(inode);
143 end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
144 if (i_size > 0 && page->index < end_index)
146 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
149 i_size_write(inode, end);
150 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
152 spin_unlock(&inode->i_lock);
155 /* A writeback failed: mark the page as bad, and invalidate the page cache */
156 static void nfs_set_pageerror(struct page *page)
159 nfs_zap_mapping(page->mapping->host, page->mapping);
162 /* We can set the PG_uptodate flag if we see that a write request
163 * covers the full page.
165 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
167 if (PageUptodate(page))
171 if (count != nfs_page_length(page))
173 SetPageUptodate(page);
176 static int wb_priority(struct writeback_control *wbc)
178 if (wbc->for_reclaim)
179 return FLUSH_HIGHPRI | FLUSH_STABLE;
180 if (wbc->for_kupdate)
186 * NFS congestion control
189 int nfs_congestion_kb;
191 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
192 #define NFS_CONGESTION_OFF_THRESH \
193 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
195 static int nfs_set_page_writeback(struct page *page)
197 int ret = test_set_page_writeback(page);
200 struct inode *inode = page->mapping->host;
201 struct nfs_server *nfss = NFS_SERVER(inode);
203 if (atomic_long_inc_return(&nfss->writeback) >
204 NFS_CONGESTION_ON_THRESH)
205 set_bdi_congested(&nfss->backing_dev_info, WRITE);
210 static void nfs_end_page_writeback(struct page *page)
212 struct inode *inode = page->mapping->host;
213 struct nfs_server *nfss = NFS_SERVER(inode);
215 end_page_writeback(page);
216 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
217 clear_bdi_congested(&nfss->backing_dev_info, WRITE);
221 * Find an associated nfs write request, and prepare to flush it out
222 * May return an error if the user signalled nfs_wait_on_request().
224 static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
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);
235 spin_unlock(&inode->i_lock);
238 if (nfs_set_page_tag_locked(req))
240 /* Note: If we hold the page lock, as is the case in nfs_writepage,
241 * then the call to nfs_set_page_tag_locked() will always
242 * succeed provided that someone hasn't already marked the
243 * request as dirty (in which case we don't care).
245 spin_unlock(&inode->i_lock);
246 ret = nfs_wait_on_request(req);
247 nfs_release_request(req);
250 spin_lock(&inode->i_lock);
252 if (test_bit(PG_CLEAN, &req->wb_flags)) {
253 spin_unlock(&inode->i_lock);
256 if (nfs_set_page_writeback(page) != 0) {
257 spin_unlock(&inode->i_lock);
260 spin_unlock(&inode->i_lock);
261 if (!nfs_pageio_add_request(pgio, req)) {
262 nfs_redirty_request(req);
263 return pgio->pg_error;
268 static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
270 struct inode *inode = page->mapping->host;
272 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
273 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
275 nfs_pageio_cond_complete(pgio, page->index);
276 return nfs_page_async_flush(pgio, page);
280 * Write an mmapped page to the server.
282 static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
284 struct nfs_pageio_descriptor pgio;
287 nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
288 err = nfs_do_writepage(page, wbc, &pgio);
289 nfs_pageio_complete(&pgio);
292 if (pgio.pg_error < 0)
293 return pgio.pg_error;
297 int nfs_writepage(struct page *page, struct writeback_control *wbc)
301 ret = nfs_writepage_locked(page, wbc);
306 static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
310 ret = nfs_do_writepage(page, wbc, data);
315 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
317 struct inode *inode = mapping->host;
318 unsigned long *bitlock = &NFS_I(inode)->flags;
319 struct nfs_pageio_descriptor pgio;
322 /* Stop dirtying of new pages while we sync */
323 err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
324 nfs_wait_bit_killable, TASK_KILLABLE);
328 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
330 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
331 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
332 nfs_pageio_complete(&pgio);
334 clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
335 smp_mb__after_clear_bit();
336 wake_up_bit(bitlock, NFS_INO_FLUSHING);
349 * Insert a write request into an inode
351 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
353 struct nfs_inode *nfsi = NFS_I(inode);
356 error = radix_tree_preload(GFP_NOFS);
360 /* Lock the request! */
361 nfs_lock_request_dontget(req);
363 spin_lock(&inode->i_lock);
364 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
368 if (nfs_have_delegation(inode, FMODE_WRITE))
371 SetPagePrivate(req->wb_page);
372 set_page_private(req->wb_page, (unsigned long)req);
374 kref_get(&req->wb_kref);
375 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
376 NFS_PAGE_TAG_LOCKED);
377 spin_unlock(&inode->i_lock);
378 radix_tree_preload_end();
384 * Remove a write request from an inode
386 static void nfs_inode_remove_request(struct nfs_page *req)
388 struct inode *inode = req->wb_context->path.dentry->d_inode;
389 struct nfs_inode *nfsi = NFS_I(inode);
391 BUG_ON (!NFS_WBACK_BUSY(req));
393 spin_lock(&inode->i_lock);
394 set_page_private(req->wb_page, 0);
395 ClearPagePrivate(req->wb_page);
396 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
399 spin_unlock(&inode->i_lock);
402 spin_unlock(&inode->i_lock);
403 nfs_clear_request(req);
404 nfs_release_request(req);
408 nfs_mark_request_dirty(struct nfs_page *req)
410 __set_page_dirty_nobuffers(req->wb_page);
413 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
415 * Add a request to the inode's commit list.
418 nfs_mark_request_commit(struct nfs_page *req)
420 struct inode *inode = req->wb_context->path.dentry->d_inode;
421 struct nfs_inode *nfsi = NFS_I(inode);
423 spin_lock(&inode->i_lock);
424 set_bit(PG_CLEAN, &(req)->wb_flags);
425 radix_tree_tag_set(&nfsi->nfs_page_tree,
427 NFS_PAGE_TAG_COMMIT);
428 spin_unlock(&inode->i_lock);
429 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
430 inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
431 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
435 nfs_clear_request_commit(struct nfs_page *req)
437 struct page *page = req->wb_page;
439 if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) {
440 dec_zone_page_state(page, NR_UNSTABLE_NFS);
441 dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
448 int nfs_write_need_commit(struct nfs_write_data *data)
450 return data->verf.committed != NFS_FILE_SYNC;
454 int nfs_reschedule_unstable_write(struct nfs_page *req)
456 if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) {
457 nfs_mark_request_commit(req);
460 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
461 nfs_mark_request_dirty(req);
468 nfs_mark_request_commit(struct nfs_page *req)
473 nfs_clear_request_commit(struct nfs_page *req)
479 int nfs_write_need_commit(struct nfs_write_data *data)
485 int nfs_reschedule_unstable_write(struct nfs_page *req)
492 * Wait for a request to complete.
494 * Interruptible by fatal signals only.
496 static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
498 struct nfs_inode *nfsi = NFS_I(inode);
499 struct nfs_page *req;
500 pgoff_t idx_end, next;
501 unsigned int res = 0;
507 idx_end = idx_start + npages - 1;
510 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) {
511 if (req->wb_index > idx_end)
514 next = req->wb_index + 1;
515 BUG_ON(!NFS_WBACK_BUSY(req));
517 kref_get(&req->wb_kref);
518 spin_unlock(&inode->i_lock);
519 error = nfs_wait_on_request(req);
520 nfs_release_request(req);
521 spin_lock(&inode->i_lock);
529 static void nfs_cancel_commit_list(struct list_head *head)
531 struct nfs_page *req;
533 while(!list_empty(head)) {
534 req = nfs_list_entry(head->next);
535 nfs_list_remove_request(req);
536 nfs_clear_request_commit(req);
537 nfs_inode_remove_request(req);
538 nfs_unlock_request(req);
542 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
544 nfs_need_commit(struct nfs_inode *nfsi)
546 return radix_tree_tagged(&nfsi->nfs_page_tree, NFS_PAGE_TAG_COMMIT);
550 * nfs_scan_commit - Scan an inode for commit requests
551 * @inode: NFS inode to scan
552 * @dst: destination list
553 * @idx_start: lower bound of page->index to scan.
554 * @npages: idx_start + npages sets the upper bound to scan.
556 * Moves requests from the inode's 'commit' request list.
557 * The requests are *not* checked to ensure that they form a contiguous set.
560 nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
562 struct nfs_inode *nfsi = NFS_I(inode);
564 if (!nfs_need_commit(nfsi))
567 return nfs_scan_list(nfsi, dst, idx_start, npages, NFS_PAGE_TAG_COMMIT);
570 static inline int nfs_need_commit(struct nfs_inode *nfsi)
575 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
582 * Search for an existing write request, and attempt to update
583 * it to reflect a new dirty region on a given page.
585 * If the attempt fails, then the existing request is flushed out
588 static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
593 struct nfs_page *req;
598 if (!PagePrivate(page))
601 end = offset + bytes;
602 spin_lock(&inode->i_lock);
605 req = nfs_page_find_request_locked(page);
609 rqend = req->wb_offset + req->wb_bytes;
611 * Tell the caller to flush out the request if
612 * the offsets are non-contiguous.
613 * Note: nfs_flush_incompatible() will already
614 * have flushed out requests having wrong owners.
617 || end < req->wb_offset)
620 if (nfs_set_page_tag_locked(req))
623 /* The request is locked, so wait and then retry */
624 spin_unlock(&inode->i_lock);
625 error = nfs_wait_on_request(req);
626 nfs_release_request(req);
629 spin_lock(&inode->i_lock);
632 if (nfs_clear_request_commit(req))
633 radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree,
634 req->wb_index, NFS_PAGE_TAG_COMMIT);
636 /* Okay, the request matches. Update the region */
637 if (offset < req->wb_offset) {
638 req->wb_offset = offset;
639 req->wb_pgbase = offset;
642 req->wb_bytes = end - req->wb_offset;
644 req->wb_bytes = rqend - req->wb_offset;
646 spin_unlock(&inode->i_lock);
649 spin_unlock(&inode->i_lock);
650 nfs_release_request(req);
651 error = nfs_wb_page(inode, page);
653 return ERR_PTR(error);
657 * Try to update an existing write request, or create one if there is none.
659 * Note: Should always be called with the Page Lock held to prevent races
660 * if we have to add a new request. Also assumes that the caller has
661 * already called nfs_flush_incompatible() if necessary.
663 static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
664 struct page *page, unsigned int offset, unsigned int bytes)
666 struct inode *inode = page->mapping->host;
667 struct nfs_page *req;
670 req = nfs_try_to_update_request(inode, page, offset, bytes);
673 req = nfs_create_request(ctx, inode, page, offset, bytes);
676 error = nfs_inode_add_request(inode, req);
678 nfs_release_request(req);
679 req = ERR_PTR(error);
685 static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
686 unsigned int offset, unsigned int count)
688 struct nfs_page *req;
690 req = nfs_setup_write_request(ctx, page, offset, count);
693 /* Update file length */
694 nfs_grow_file(page, offset, count);
695 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
696 nfs_clear_page_tag_locked(req);
700 int nfs_flush_incompatible(struct file *file, struct page *page)
702 struct nfs_open_context *ctx = nfs_file_open_context(file);
703 struct nfs_page *req;
704 int do_flush, status;
706 * Look for a request corresponding to this page. If there
707 * is one, and it belongs to another file, we flush it out
708 * before we try to copy anything into the page. Do this
709 * due to the lack of an ACCESS-type call in NFSv2.
710 * Also do the same if we find a request from an existing
714 req = nfs_page_find_request(page);
717 do_flush = req->wb_page != page || req->wb_context != ctx;
718 nfs_release_request(req);
721 status = nfs_wb_page(page->mapping->host, page);
722 } while (status == 0);
727 * If the page cache is marked as unsafe or invalid, then we can't rely on
728 * the PageUptodate() flag. In this case, we will need to turn off
729 * write optimisations that depend on the page contents being correct.
731 static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
733 return PageUptodate(page) &&
734 !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
738 * Update and possibly write a cached page of an NFS file.
740 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
741 * things with a page scheduled for an RPC call (e.g. invalidate it).
743 int nfs_updatepage(struct file *file, struct page *page,
744 unsigned int offset, unsigned int count)
746 struct nfs_open_context *ctx = nfs_file_open_context(file);
747 struct inode *inode = page->mapping->host;
750 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
752 dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n",
753 file->f_path.dentry->d_parent->d_name.name,
754 file->f_path.dentry->d_name.name, count,
755 (long long)(page_offset(page) + offset));
757 /* If we're not using byte range locks, and we know the page
758 * is up to date, it may be more efficient to extend the write
759 * to cover the entire page in order to avoid fragmentation
762 if (nfs_write_pageuptodate(page, inode) &&
763 inode->i_flock == NULL &&
764 !(file->f_flags & O_SYNC)) {
765 count = max(count + offset, nfs_page_length(page));
769 status = nfs_writepage_setup(ctx, page, offset, count);
771 nfs_set_pageerror(page);
773 __set_page_dirty_nobuffers(page);
775 dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
776 status, (long long)i_size_read(inode));
780 static void nfs_writepage_release(struct nfs_page *req)
783 if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req)) {
784 nfs_end_page_writeback(req->wb_page);
785 nfs_inode_remove_request(req);
787 nfs_end_page_writeback(req->wb_page);
788 nfs_clear_page_tag_locked(req);
791 static int flush_task_priority(int how)
793 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
795 return RPC_PRIORITY_HIGH;
797 return RPC_PRIORITY_LOW;
799 return RPC_PRIORITY_NORMAL;
803 * Set up the argument/result storage required for the RPC call.
805 static int nfs_write_rpcsetup(struct nfs_page *req,
806 struct nfs_write_data *data,
807 const struct rpc_call_ops *call_ops,
808 unsigned int count, unsigned int offset,
811 struct inode *inode = req->wb_context->path.dentry->d_inode;
812 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
813 int priority = flush_task_priority(how);
814 struct rpc_task *task;
815 struct rpc_message msg = {
816 .rpc_argp = &data->args,
817 .rpc_resp = &data->res,
818 .rpc_cred = req->wb_context->cred,
820 struct rpc_task_setup task_setup_data = {
821 .rpc_client = NFS_CLIENT(inode),
824 .callback_ops = call_ops,
825 .callback_data = data,
826 .workqueue = nfsiod_workqueue,
828 .priority = priority,
831 /* Set up the RPC argument and reply structs
832 * NB: take care not to mess about with data->commit et al. */
835 data->inode = inode = req->wb_context->path.dentry->d_inode;
836 data->cred = msg.rpc_cred;
838 data->args.fh = NFS_FH(inode);
839 data->args.offset = req_offset(req) + offset;
840 data->args.pgbase = req->wb_pgbase + offset;
841 data->args.pages = data->pagevec;
842 data->args.count = count;
843 data->args.context = get_nfs_open_context(req->wb_context);
844 data->args.stable = NFS_UNSTABLE;
845 if (how & FLUSH_STABLE) {
846 data->args.stable = NFS_DATA_SYNC;
847 if (!nfs_need_commit(NFS_I(inode)))
848 data->args.stable = NFS_FILE_SYNC;
851 data->res.fattr = &data->fattr;
852 data->res.count = count;
853 data->res.verf = &data->verf;
854 nfs_fattr_init(&data->fattr);
856 /* Set up the initial task struct. */
857 NFS_PROTO(inode)->write_setup(data, &msg);
859 dprintk("NFS: %5u initiated write call "
860 "(req %s/%lld, %u bytes @ offset %llu)\n",
863 (long long)NFS_FILEID(inode),
865 (unsigned long long)data->args.offset);
867 task = rpc_run_task(&task_setup_data);
869 return PTR_ERR(task);
874 /* If a nfs_flush_* function fails, it should remove reqs from @head and
875 * call this on each, which will prepare them to be retried on next
876 * writeback using standard nfs.
878 static void nfs_redirty_request(struct nfs_page *req)
880 nfs_mark_request_dirty(req);
881 nfs_end_page_writeback(req->wb_page);
882 nfs_clear_page_tag_locked(req);
886 * Generate multiple small requests to write out a single
887 * contiguous dirty area on one page.
889 static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
891 struct nfs_page *req = nfs_list_entry(head->next);
892 struct page *page = req->wb_page;
893 struct nfs_write_data *data;
894 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
900 nfs_list_remove_request(req);
904 size_t len = min(nbytes, wsize);
906 data = nfs_writedata_alloc(1);
909 list_add(&data->pages, &list);
912 } while (nbytes != 0);
913 atomic_set(&req->wb_complete, requests);
915 ClearPageError(page);
921 data = list_entry(list.next, struct nfs_write_data, pages);
922 list_del_init(&data->pages);
924 data->pagevec[0] = page;
928 ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
934 } while (nbytes != 0);
939 while (!list_empty(&list)) {
940 data = list_entry(list.next, struct nfs_write_data, pages);
941 list_del(&data->pages);
942 nfs_writedata_release(data);
944 nfs_redirty_request(req);
949 * Create an RPC task for the given write request and kick it.
950 * The page must have been locked by the caller.
952 * It may happen that the page we're passed is not marked dirty.
953 * This is the case if nfs_updatepage detects a conflicting request
954 * that has been written but not committed.
956 static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
958 struct nfs_page *req;
960 struct nfs_write_data *data;
962 data = nfs_writedata_alloc(npages);
966 pages = data->pagevec;
967 while (!list_empty(head)) {
968 req = nfs_list_entry(head->next);
969 nfs_list_remove_request(req);
970 nfs_list_add_request(req, &data->pages);
971 ClearPageError(req->wb_page);
972 *pages++ = req->wb_page;
974 req = nfs_list_entry(data->pages.next);
976 /* Set up the argument struct */
977 return nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
979 while (!list_empty(head)) {
980 req = nfs_list_entry(head->next);
981 nfs_list_remove_request(req);
982 nfs_redirty_request(req);
987 static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
988 struct inode *inode, int ioflags)
990 size_t wsize = NFS_SERVER(inode)->wsize;
992 if (wsize < PAGE_CACHE_SIZE)
993 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
995 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
999 * Handle a write reply that flushed part of a page.
1001 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
1003 struct nfs_write_data *data = calldata;
1005 dprintk("NFS: %5u write(%s/%lld %d@%lld)",
1007 data->req->wb_context->path.dentry->d_inode->i_sb->s_id,
1009 NFS_FILEID(data->req->wb_context->path.dentry->d_inode),
1010 data->req->wb_bytes, (long long)req_offset(data->req));
1012 nfs_writeback_done(task, data);
1015 static void nfs_writeback_release_partial(void *calldata)
1017 struct nfs_write_data *data = calldata;
1018 struct nfs_page *req = data->req;
1019 struct page *page = req->wb_page;
1020 int status = data->task.tk_status;
1023 nfs_set_pageerror(page);
1024 nfs_context_set_write_error(req->wb_context, status);
1025 dprintk(", error = %d\n", status);
1029 if (nfs_write_need_commit(data)) {
1030 struct inode *inode = page->mapping->host;
1032 spin_lock(&inode->i_lock);
1033 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
1034 /* Do nothing we need to resend the writes */
1035 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1036 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1037 dprintk(" defer commit\n");
1038 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1039 set_bit(PG_NEED_RESCHED, &req->wb_flags);
1040 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
1041 dprintk(" server reboot detected\n");
1043 spin_unlock(&inode->i_lock);
1048 if (atomic_dec_and_test(&req->wb_complete))
1049 nfs_writepage_release(req);
1050 nfs_writedata_release(calldata);
1053 static const struct rpc_call_ops nfs_write_partial_ops = {
1054 .rpc_call_done = nfs_writeback_done_partial,
1055 .rpc_release = nfs_writeback_release_partial,
1059 * Handle a write reply that flushes a whole page.
1061 * FIXME: There is an inherent race with invalidate_inode_pages and
1062 * writebacks since the page->count is kept > 1 for as long
1063 * as the page has a write request pending.
1065 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1067 struct nfs_write_data *data = calldata;
1069 nfs_writeback_done(task, data);
1072 static void nfs_writeback_release_full(void *calldata)
1074 struct nfs_write_data *data = calldata;
1075 int status = data->task.tk_status;
1077 /* Update attributes as result of writeback. */
1078 while (!list_empty(&data->pages)) {
1079 struct nfs_page *req = nfs_list_entry(data->pages.next);
1080 struct page *page = req->wb_page;
1082 nfs_list_remove_request(req);
1084 dprintk("NFS: %5u write (%s/%lld %d@%lld)",
1086 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1087 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1089 (long long)req_offset(req));
1092 nfs_set_pageerror(page);
1093 nfs_context_set_write_error(req->wb_context, status);
1094 dprintk(", error = %d\n", status);
1095 goto remove_request;
1098 if (nfs_write_need_commit(data)) {
1099 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1100 nfs_mark_request_commit(req);
1101 nfs_end_page_writeback(page);
1102 dprintk(" marked for commit\n");
1107 nfs_end_page_writeback(page);
1108 nfs_inode_remove_request(req);
1110 nfs_clear_page_tag_locked(req);
1112 nfs_writedata_release(calldata);
1115 static const struct rpc_call_ops nfs_write_full_ops = {
1116 .rpc_call_done = nfs_writeback_done_full,
1117 .rpc_release = nfs_writeback_release_full,
1122 * This function is called when the WRITE call is complete.
1124 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1126 struct nfs_writeargs *argp = &data->args;
1127 struct nfs_writeres *resp = &data->res;
1130 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1131 task->tk_pid, task->tk_status);
1134 * ->write_done will attempt to use post-op attributes to detect
1135 * conflicting writes by other clients. A strict interpretation
1136 * of close-to-open would allow us to continue caching even if
1137 * another writer had changed the file, but some applications
1138 * depend on tighter cache coherency when writing.
1140 status = NFS_PROTO(data->inode)->write_done(task, data);
1143 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1145 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1146 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1147 /* We tried a write call, but the server did not
1148 * commit data to stable storage even though we
1150 * Note: There is a known bug in Tru64 < 5.0 in which
1151 * the server reports NFS_DATA_SYNC, but performs
1152 * NFS_FILE_SYNC. We therefore implement this checking
1153 * as a dprintk() in order to avoid filling syslog.
1155 static unsigned long complain;
1157 if (time_before(complain, jiffies)) {
1158 dprintk("NFS: faulty NFS server %s:"
1159 " (committed = %d) != (stable = %d)\n",
1160 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1161 resp->verf->committed, argp->stable);
1162 complain = jiffies + 300 * HZ;
1166 /* Is this a short write? */
1167 if (task->tk_status >= 0 && resp->count < argp->count) {
1168 static unsigned long complain;
1170 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1172 /* Has the server at least made some progress? */
1173 if (resp->count != 0) {
1174 /* Was this an NFSv2 write or an NFSv3 stable write? */
1175 if (resp->verf->committed != NFS_UNSTABLE) {
1176 /* Resend from where the server left off */
1177 argp->offset += resp->count;
1178 argp->pgbase += resp->count;
1179 argp->count -= resp->count;
1181 /* Resend as a stable write in order to avoid
1182 * headaches in the case of a server crash.
1184 argp->stable = NFS_FILE_SYNC;
1186 rpc_restart_call(task);
1189 if (time_before(complain, jiffies)) {
1191 "NFS: Server wrote zero bytes, expected %u.\n",
1193 complain = jiffies + 300 * HZ;
1195 /* Can't do anything about it except throw an error. */
1196 task->tk_status = -EIO;
1202 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1203 void nfs_commitdata_release(void *data)
1205 struct nfs_write_data *wdata = data;
1207 put_nfs_open_context(wdata->args.context);
1208 nfs_commit_free(wdata);
1212 * Set up the argument/result storage required for the RPC call.
1214 static int nfs_commit_rpcsetup(struct list_head *head,
1215 struct nfs_write_data *data,
1218 struct nfs_page *first = nfs_list_entry(head->next);
1219 struct inode *inode = first->wb_context->path.dentry->d_inode;
1220 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1221 int priority = flush_task_priority(how);
1222 struct rpc_task *task;
1223 struct rpc_message msg = {
1224 .rpc_argp = &data->args,
1225 .rpc_resp = &data->res,
1226 .rpc_cred = first->wb_context->cred,
1228 struct rpc_task_setup task_setup_data = {
1229 .task = &data->task,
1230 .rpc_client = NFS_CLIENT(inode),
1231 .rpc_message = &msg,
1232 .callback_ops = &nfs_commit_ops,
1233 .callback_data = data,
1234 .workqueue = nfsiod_workqueue,
1236 .priority = priority,
1239 /* Set up the RPC argument and reply structs
1240 * NB: take care not to mess about with data->commit et al. */
1242 list_splice_init(head, &data->pages);
1244 data->inode = inode;
1245 data->cred = msg.rpc_cred;
1247 data->args.fh = NFS_FH(data->inode);
1248 /* Note: we always request a commit of the entire inode */
1249 data->args.offset = 0;
1250 data->args.count = 0;
1251 data->args.context = get_nfs_open_context(first->wb_context);
1252 data->res.count = 0;
1253 data->res.fattr = &data->fattr;
1254 data->res.verf = &data->verf;
1255 nfs_fattr_init(&data->fattr);
1257 /* Set up the initial task struct. */
1258 NFS_PROTO(inode)->commit_setup(data, &msg);
1260 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1262 task = rpc_run_task(&task_setup_data);
1264 return PTR_ERR(task);
1270 * Commit dirty pages
1273 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1275 struct nfs_write_data *data;
1276 struct nfs_page *req;
1278 data = nfs_commitdata_alloc();
1283 /* Set up the argument struct */
1284 return nfs_commit_rpcsetup(head, data, how);
1286 while (!list_empty(head)) {
1287 req = nfs_list_entry(head->next);
1288 nfs_list_remove_request(req);
1289 nfs_mark_request_commit(req);
1290 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1291 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1293 nfs_clear_page_tag_locked(req);
1299 * COMMIT call returned
1301 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1303 struct nfs_write_data *data = calldata;
1305 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1306 task->tk_pid, task->tk_status);
1308 /* Call the NFS version-specific code */
1309 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1313 static void nfs_commit_release(void *calldata)
1315 struct nfs_write_data *data = calldata;
1316 struct nfs_page *req;
1317 int status = data->task.tk_status;
1319 while (!list_empty(&data->pages)) {
1320 req = nfs_list_entry(data->pages.next);
1321 nfs_list_remove_request(req);
1322 nfs_clear_request_commit(req);
1324 dprintk("NFS: commit (%s/%lld %d@%lld)",
1325 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1326 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1328 (long long)req_offset(req));
1330 nfs_context_set_write_error(req->wb_context, status);
1331 nfs_inode_remove_request(req);
1332 dprintk(", error = %d\n", status);
1336 /* Okay, COMMIT succeeded, apparently. Check the verifier
1337 * returned by the server against all stored verfs. */
1338 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1339 /* We have a match */
1340 nfs_inode_remove_request(req);
1344 /* We have a mismatch. Write the page again */
1345 dprintk(" mismatch\n");
1346 nfs_mark_request_dirty(req);
1348 nfs_clear_page_tag_locked(req);
1350 nfs_commitdata_release(calldata);
1353 static const struct rpc_call_ops nfs_commit_ops = {
1354 .rpc_call_done = nfs_commit_done,
1355 .rpc_release = nfs_commit_release,
1358 int nfs_commit_inode(struct inode *inode, int how)
1363 spin_lock(&inode->i_lock);
1364 res = nfs_scan_commit(inode, &head, 0, 0);
1365 spin_unlock(&inode->i_lock);
1367 int error = nfs_commit_list(inode, &head, how);
1374 static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1380 long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
1382 struct inode *inode = mapping->host;
1383 pgoff_t idx_start, idx_end;
1384 unsigned int npages = 0;
1386 int nocommit = how & FLUSH_NOCOMMIT;
1390 if (wbc->range_cyclic)
1393 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1394 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1395 if (idx_end > idx_start) {
1396 pgoff_t l_npages = 1 + idx_end - idx_start;
1398 if (sizeof(npages) != sizeof(l_npages) &&
1399 (pgoff_t)npages != l_npages)
1403 how &= ~FLUSH_NOCOMMIT;
1404 spin_lock(&inode->i_lock);
1406 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1411 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1414 if (how & FLUSH_INVALIDATE) {
1415 spin_unlock(&inode->i_lock);
1416 nfs_cancel_commit_list(&head);
1418 spin_lock(&inode->i_lock);
1421 pages += nfs_scan_commit(inode, &head, 0, 0);
1422 spin_unlock(&inode->i_lock);
1423 ret = nfs_commit_list(inode, &head, how);
1424 spin_lock(&inode->i_lock);
1427 spin_unlock(&inode->i_lock);
1431 static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
1435 ret = nfs_writepages(mapping, wbc);
1438 ret = nfs_sync_mapping_wait(mapping, wbc, how);
1443 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1447 /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
1448 static int nfs_write_mapping(struct address_space *mapping, int how)
1450 struct writeback_control wbc = {
1451 .bdi = mapping->backing_dev_info,
1452 .sync_mode = WB_SYNC_ALL,
1453 .nr_to_write = LONG_MAX,
1455 .range_end = LLONG_MAX,
1456 .for_writepages = 1,
1459 return __nfs_write_mapping(mapping, &wbc, how);
1463 * flush the inode to disk.
1465 int nfs_wb_all(struct inode *inode)
1467 return nfs_write_mapping(inode->i_mapping, 0);
1470 int nfs_wb_nocommit(struct inode *inode)
1472 return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
1475 int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1477 struct nfs_page *req;
1478 loff_t range_start = page_offset(page);
1479 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1480 struct writeback_control wbc = {
1481 .bdi = page->mapping->backing_dev_info,
1482 .sync_mode = WB_SYNC_ALL,
1483 .nr_to_write = LONG_MAX,
1484 .range_start = range_start,
1485 .range_end = range_end,
1489 BUG_ON(!PageLocked(page));
1491 req = nfs_page_find_request(page);
1494 if (test_bit(PG_CLEAN, &req->wb_flags)) {
1495 nfs_release_request(req);
1498 if (nfs_lock_request_dontget(req)) {
1499 nfs_inode_remove_request(req);
1501 * In case nfs_inode_remove_request has marked the
1502 * page as being dirty
1504 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1505 nfs_unlock_request(req);
1508 ret = nfs_wait_on_request(req);
1512 if (!PagePrivate(page))
1514 ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
1519 static int nfs_wb_page_priority(struct inode *inode, struct page *page,
1522 loff_t range_start = page_offset(page);
1523 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1524 struct writeback_control wbc = {
1525 .bdi = page->mapping->backing_dev_info,
1526 .sync_mode = WB_SYNC_ALL,
1527 .nr_to_write = LONG_MAX,
1528 .range_start = range_start,
1529 .range_end = range_end,
1534 if (clear_page_dirty_for_io(page)) {
1535 ret = nfs_writepage_locked(page, &wbc);
1538 } else if (!PagePrivate(page))
1540 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1543 } while (PagePrivate(page));
1546 __mark_inode_dirty(inode, I_DIRTY_PAGES);
1551 * Write back all requests on one page - we do this before reading it.
1553 int nfs_wb_page(struct inode *inode, struct page* page)
1555 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
1558 int __init nfs_init_writepagecache(void)
1560 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1561 sizeof(struct nfs_write_data),
1562 0, SLAB_HWCACHE_ALIGN,
1564 if (nfs_wdata_cachep == NULL)
1567 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1569 if (nfs_wdata_mempool == NULL)
1572 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1574 if (nfs_commit_mempool == NULL)
1578 * NFS congestion size, scale with available memory.
1590 * This allows larger machines to have larger/more transfers.
1591 * Limit the default to 256M
1593 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1594 if (nfs_congestion_kb > 256*1024)
1595 nfs_congestion_kb = 256*1024;
1600 void nfs_destroy_writepagecache(void)
1602 mempool_destroy(nfs_commit_mempool);
1603 mempool_destroy(nfs_wdata_mempool);
1604 kmem_cache_destroy(nfs_wdata_cachep);