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"
30 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
32 #define MIN_POOL_WRITE (32)
33 #define MIN_POOL_COMMIT (4)
36 * Local function declarations
38 static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc,
39 struct inode *inode, int ioflags);
40 static void nfs_redirty_request(struct nfs_page *req);
41 static const struct rpc_call_ops nfs_write_partial_ops;
42 static const struct rpc_call_ops nfs_write_full_ops;
43 static const struct rpc_call_ops nfs_commit_ops;
45 static struct kmem_cache *nfs_wdata_cachep;
46 static mempool_t *nfs_wdata_mempool;
47 static mempool_t *nfs_commit_mempool;
49 struct nfs_write_data *nfs_commitdata_alloc(void)
51 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
54 memset(p, 0, sizeof(*p));
55 INIT_LIST_HEAD(&p->pages);
56 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
61 void nfs_commit_free(struct nfs_write_data *p)
63 if (p && (p->pagevec != &p->page_array[0]))
65 mempool_free(p, nfs_commit_mempool);
68 struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
70 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
73 memset(p, 0, sizeof(*p));
74 INIT_LIST_HEAD(&p->pages);
75 p->npages = pagecount;
76 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
77 if (pagecount <= ARRAY_SIZE(p->page_array))
78 p->pagevec = p->page_array;
80 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
82 mempool_free(p, nfs_wdata_mempool);
90 void nfs_writedata_free(struct nfs_write_data *p)
92 if (p && (p->pagevec != &p->page_array[0]))
94 mempool_free(p, nfs_wdata_mempool);
97 static void nfs_writedata_release(struct nfs_write_data *wdata)
99 put_nfs_open_context(wdata->args.context);
100 nfs_writedata_free(wdata);
103 static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
107 set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
110 static struct nfs_page *nfs_page_find_request_locked(struct page *page)
112 struct nfs_page *req = NULL;
114 if (PagePrivate(page)) {
115 req = (struct nfs_page *)page_private(page);
117 kref_get(&req->wb_kref);
122 static struct nfs_page *nfs_page_find_request(struct page *page)
124 struct inode *inode = page->mapping->host;
125 struct nfs_page *req = NULL;
127 spin_lock(&inode->i_lock);
128 req = nfs_page_find_request_locked(page);
129 spin_unlock(&inode->i_lock);
133 /* Adjust the file length if we're writing beyond the end */
134 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
136 struct inode *inode = page->mapping->host;
140 spin_lock(&inode->i_lock);
141 i_size = i_size_read(inode);
142 end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
143 if (i_size > 0 && page->index < end_index)
145 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
148 i_size_write(inode, end);
149 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
151 spin_unlock(&inode->i_lock);
154 /* A writeback failed: mark the page as bad, and invalidate the page cache */
155 static void nfs_set_pageerror(struct page *page)
158 nfs_zap_mapping(page->mapping->host, page->mapping);
161 /* We can set the PG_uptodate flag if we see that a write request
162 * covers the full page.
164 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
166 if (PageUptodate(page))
170 if (count != nfs_page_length(page))
172 SetPageUptodate(page);
175 static int wb_priority(struct writeback_control *wbc)
177 if (wbc->for_reclaim)
178 return FLUSH_HIGHPRI | FLUSH_STABLE;
179 if (wbc->for_kupdate)
185 * NFS congestion control
188 int nfs_congestion_kb;
190 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
191 #define NFS_CONGESTION_OFF_THRESH \
192 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
194 static int nfs_set_page_writeback(struct page *page)
196 int ret = test_set_page_writeback(page);
199 struct inode *inode = page->mapping->host;
200 struct nfs_server *nfss = NFS_SERVER(inode);
202 if (atomic_long_inc_return(&nfss->writeback) >
203 NFS_CONGESTION_ON_THRESH) {
204 set_bdi_congested(&nfss->backing_dev_info,
211 static void nfs_end_page_writeback(struct page *page)
213 struct inode *inode = page->mapping->host;
214 struct nfs_server *nfss = NFS_SERVER(inode);
216 end_page_writeback(page);
217 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
218 clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
222 * Find an associated nfs write request, and prepare to flush it out
223 * May return an error if the user signalled nfs_wait_on_request().
225 static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
228 struct inode *inode = page->mapping->host;
229 struct nfs_page *req;
232 spin_lock(&inode->i_lock);
234 req = nfs_page_find_request_locked(page);
236 spin_unlock(&inode->i_lock);
239 if (nfs_set_page_tag_locked(req))
241 /* Note: If we hold the page lock, as is the case in nfs_writepage,
242 * then the call to nfs_set_page_tag_locked() will always
243 * succeed provided that someone hasn't already marked the
244 * request as dirty (in which case we don't care).
246 spin_unlock(&inode->i_lock);
247 ret = nfs_wait_on_request(req);
248 nfs_release_request(req);
251 spin_lock(&inode->i_lock);
253 if (test_bit(PG_CLEAN, &req->wb_flags)) {
254 spin_unlock(&inode->i_lock);
257 if (nfs_set_page_writeback(page) != 0) {
258 spin_unlock(&inode->i_lock);
261 spin_unlock(&inode->i_lock);
262 if (!nfs_pageio_add_request(pgio, req)) {
263 nfs_redirty_request(req);
264 return pgio->pg_error;
269 static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
271 struct inode *inode = page->mapping->host;
273 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
274 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
276 nfs_pageio_cond_complete(pgio, page->index);
277 return nfs_page_async_flush(pgio, page);
281 * Write an mmapped page to the server.
283 static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
285 struct nfs_pageio_descriptor pgio;
288 nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
289 err = nfs_do_writepage(page, wbc, &pgio);
290 nfs_pageio_complete(&pgio);
293 if (pgio.pg_error < 0)
294 return pgio.pg_error;
298 int nfs_writepage(struct page *page, struct writeback_control *wbc)
302 ret = nfs_writepage_locked(page, wbc);
307 static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
311 ret = nfs_do_writepage(page, wbc, data);
316 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
318 struct inode *inode = mapping->host;
319 unsigned long *bitlock = &NFS_I(inode)->flags;
320 struct nfs_pageio_descriptor pgio;
323 /* Stop dirtying of new pages while we sync */
324 err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
325 nfs_wait_bit_killable, TASK_KILLABLE);
329 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
331 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
332 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
333 nfs_pageio_complete(&pgio);
335 clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
336 smp_mb__after_clear_bit();
337 wake_up_bit(bitlock, NFS_INO_FLUSHING);
350 * Insert a write request into an inode
352 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
354 struct nfs_inode *nfsi = NFS_I(inode);
357 error = radix_tree_preload(GFP_NOFS);
361 /* Lock the request! */
362 nfs_lock_request_dontget(req);
364 spin_lock(&inode->i_lock);
365 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
369 if (nfs_have_delegation(inode, FMODE_WRITE))
372 SetPagePrivate(req->wb_page);
373 set_page_private(req->wb_page, (unsigned long)req);
375 kref_get(&req->wb_kref);
376 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
377 NFS_PAGE_TAG_LOCKED);
378 spin_unlock(&inode->i_lock);
379 radix_tree_preload_end();
385 * Remove a write request from an inode
387 static void nfs_inode_remove_request(struct nfs_page *req)
389 struct inode *inode = req->wb_context->path.dentry->d_inode;
390 struct nfs_inode *nfsi = NFS_I(inode);
392 BUG_ON (!NFS_WBACK_BUSY(req));
394 spin_lock(&inode->i_lock);
395 set_page_private(req->wb_page, 0);
396 ClearPagePrivate(req->wb_page);
397 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
400 spin_unlock(&inode->i_lock);
403 spin_unlock(&inode->i_lock);
404 nfs_clear_request(req);
405 nfs_release_request(req);
409 nfs_mark_request_dirty(struct nfs_page *req)
411 __set_page_dirty_nobuffers(req->wb_page);
414 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
416 * Add a request to the inode's commit list.
419 nfs_mark_request_commit(struct nfs_page *req)
421 struct inode *inode = req->wb_context->path.dentry->d_inode;
422 struct nfs_inode *nfsi = NFS_I(inode);
424 spin_lock(&inode->i_lock);
425 set_bit(PG_CLEAN, &(req)->wb_flags);
426 radix_tree_tag_set(&nfsi->nfs_page_tree,
428 NFS_PAGE_TAG_COMMIT);
429 spin_unlock(&inode->i_lock);
430 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
431 inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
432 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
436 nfs_clear_request_commit(struct nfs_page *req)
438 struct page *page = req->wb_page;
440 if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) {
441 dec_zone_page_state(page, NR_UNSTABLE_NFS);
442 dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
449 int nfs_write_need_commit(struct nfs_write_data *data)
451 return data->verf.committed != NFS_FILE_SYNC;
455 int nfs_reschedule_unstable_write(struct nfs_page *req)
457 if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) {
458 nfs_mark_request_commit(req);
461 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
462 nfs_mark_request_dirty(req);
469 nfs_mark_request_commit(struct nfs_page *req)
474 nfs_clear_request_commit(struct nfs_page *req)
480 int nfs_write_need_commit(struct nfs_write_data *data)
486 int nfs_reschedule_unstable_write(struct nfs_page *req)
493 * Wait for a request to complete.
495 * Interruptible by fatal signals only.
497 static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
499 struct nfs_inode *nfsi = NFS_I(inode);
500 struct nfs_page *req;
501 pgoff_t idx_end, next;
502 unsigned int res = 0;
508 idx_end = idx_start + npages - 1;
511 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) {
512 if (req->wb_index > idx_end)
515 next = req->wb_index + 1;
516 BUG_ON(!NFS_WBACK_BUSY(req));
518 kref_get(&req->wb_kref);
519 spin_unlock(&inode->i_lock);
520 error = nfs_wait_on_request(req);
521 nfs_release_request(req);
522 spin_lock(&inode->i_lock);
530 static void nfs_cancel_commit_list(struct list_head *head)
532 struct nfs_page *req;
534 while(!list_empty(head)) {
535 req = nfs_list_entry(head->next);
536 nfs_list_remove_request(req);
537 nfs_clear_request_commit(req);
538 nfs_inode_remove_request(req);
539 nfs_unlock_request(req);
543 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
545 nfs_need_commit(struct nfs_inode *nfsi)
547 return radix_tree_tagged(&nfsi->nfs_page_tree, NFS_PAGE_TAG_COMMIT);
551 * nfs_scan_commit - Scan an inode for commit requests
552 * @inode: NFS inode to scan
553 * @dst: destination list
554 * @idx_start: lower bound of page->index to scan.
555 * @npages: idx_start + npages sets the upper bound to scan.
557 * Moves requests from the inode's 'commit' request list.
558 * The requests are *not* checked to ensure that they form a contiguous set.
561 nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
563 struct nfs_inode *nfsi = NFS_I(inode);
565 if (!nfs_need_commit(nfsi))
568 return nfs_scan_list(nfsi, dst, idx_start, npages, NFS_PAGE_TAG_COMMIT);
571 static inline int nfs_need_commit(struct nfs_inode *nfsi)
576 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
583 * Search for an existing write request, and attempt to update
584 * it to reflect a new dirty region on a given page.
586 * If the attempt fails, then the existing request is flushed out
589 static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
594 struct nfs_page *req;
599 if (!PagePrivate(page))
602 end = offset + bytes;
603 spin_lock(&inode->i_lock);
606 req = nfs_page_find_request_locked(page);
610 rqend = req->wb_offset + req->wb_bytes;
612 * Tell the caller to flush out the request if
613 * the offsets are non-contiguous.
614 * Note: nfs_flush_incompatible() will already
615 * have flushed out requests having wrong owners.
618 || end < req->wb_offset)
621 if (nfs_set_page_tag_locked(req))
624 /* The request is locked, so wait and then retry */
625 spin_unlock(&inode->i_lock);
626 error = nfs_wait_on_request(req);
627 nfs_release_request(req);
630 spin_lock(&inode->i_lock);
633 if (nfs_clear_request_commit(req))
634 radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree,
635 req->wb_index, NFS_PAGE_TAG_COMMIT);
637 /* Okay, the request matches. Update the region */
638 if (offset < req->wb_offset) {
639 req->wb_offset = offset;
640 req->wb_pgbase = offset;
643 req->wb_bytes = end - req->wb_offset;
645 req->wb_bytes = rqend - req->wb_offset;
647 spin_unlock(&inode->i_lock);
650 spin_unlock(&inode->i_lock);
651 nfs_release_request(req);
652 error = nfs_wb_page(inode, page);
654 return ERR_PTR(error);
658 * Try to update an existing write request, or create one if there is none.
660 * Note: Should always be called with the Page Lock held to prevent races
661 * if we have to add a new request. Also assumes that the caller has
662 * already called nfs_flush_incompatible() if necessary.
664 static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
665 struct page *page, unsigned int offset, unsigned int bytes)
667 struct inode *inode = page->mapping->host;
668 struct nfs_page *req;
671 req = nfs_try_to_update_request(inode, page, offset, bytes);
674 req = nfs_create_request(ctx, inode, page, offset, bytes);
677 error = nfs_inode_add_request(inode, req);
679 nfs_release_request(req);
680 req = ERR_PTR(error);
686 static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
687 unsigned int offset, unsigned int count)
689 struct nfs_page *req;
691 req = nfs_setup_write_request(ctx, page, offset, count);
694 /* Update file length */
695 nfs_grow_file(page, offset, count);
696 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
697 nfs_clear_page_tag_locked(req);
701 int nfs_flush_incompatible(struct file *file, struct page *page)
703 struct nfs_open_context *ctx = nfs_file_open_context(file);
704 struct nfs_page *req;
705 int do_flush, status;
707 * Look for a request corresponding to this page. If there
708 * is one, and it belongs to another file, we flush it out
709 * before we try to copy anything into the page. Do this
710 * due to the lack of an ACCESS-type call in NFSv2.
711 * Also do the same if we find a request from an existing
715 req = nfs_page_find_request(page);
718 do_flush = req->wb_page != page || req->wb_context != ctx;
719 nfs_release_request(req);
722 status = nfs_wb_page(page->mapping->host, page);
723 } while (status == 0);
728 * If the page cache is marked as unsafe or invalid, then we can't rely on
729 * the PageUptodate() flag. In this case, we will need to turn off
730 * write optimisations that depend on the page contents being correct.
732 static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
734 return PageUptodate(page) &&
735 !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
739 * Update and possibly write a cached page of an NFS file.
741 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
742 * things with a page scheduled for an RPC call (e.g. invalidate it).
744 int nfs_updatepage(struct file *file, struct page *page,
745 unsigned int offset, unsigned int count)
747 struct nfs_open_context *ctx = nfs_file_open_context(file);
748 struct inode *inode = page->mapping->host;
751 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
753 dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n",
754 file->f_path.dentry->d_parent->d_name.name,
755 file->f_path.dentry->d_name.name, count,
756 (long long)(page_offset(page) + offset));
758 /* If we're not using byte range locks, and we know the page
759 * is up to date, it may be more efficient to extend the write
760 * to cover the entire page in order to avoid fragmentation
763 if (nfs_write_pageuptodate(page, inode) &&
764 inode->i_flock == NULL &&
765 !(file->f_flags & O_SYNC)) {
766 count = max(count + offset, nfs_page_length(page));
770 status = nfs_writepage_setup(ctx, page, offset, count);
772 nfs_set_pageerror(page);
774 __set_page_dirty_nobuffers(page);
776 dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
777 status, (long long)i_size_read(inode));
781 static void nfs_writepage_release(struct nfs_page *req)
784 if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req)) {
785 nfs_end_page_writeback(req->wb_page);
786 nfs_inode_remove_request(req);
788 nfs_end_page_writeback(req->wb_page);
789 nfs_clear_page_tag_locked(req);
792 static int flush_task_priority(int how)
794 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
796 return RPC_PRIORITY_HIGH;
798 return RPC_PRIORITY_LOW;
800 return RPC_PRIORITY_NORMAL;
804 * Set up the argument/result storage required for the RPC call.
806 static int nfs_write_rpcsetup(struct nfs_page *req,
807 struct nfs_write_data *data,
808 const struct rpc_call_ops *call_ops,
809 unsigned int count, unsigned int offset,
812 struct inode *inode = req->wb_context->path.dentry->d_inode;
813 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
814 int priority = flush_task_priority(how);
815 struct rpc_task *task;
816 struct rpc_message msg = {
817 .rpc_argp = &data->args,
818 .rpc_resp = &data->res,
819 .rpc_cred = req->wb_context->cred,
821 struct rpc_task_setup task_setup_data = {
822 .rpc_client = NFS_CLIENT(inode),
825 .callback_ops = call_ops,
826 .callback_data = data,
827 .workqueue = nfsiod_workqueue,
829 .priority = priority,
832 /* Set up the RPC argument and reply structs
833 * NB: take care not to mess about with data->commit et al. */
836 data->inode = inode = req->wb_context->path.dentry->d_inode;
837 data->cred = msg.rpc_cred;
839 data->args.fh = NFS_FH(inode);
840 data->args.offset = req_offset(req) + offset;
841 data->args.pgbase = req->wb_pgbase + offset;
842 data->args.pages = data->pagevec;
843 data->args.count = count;
844 data->args.context = get_nfs_open_context(req->wb_context);
845 data->args.stable = NFS_UNSTABLE;
846 if (how & FLUSH_STABLE) {
847 data->args.stable = NFS_DATA_SYNC;
848 if (!nfs_need_commit(NFS_I(inode)))
849 data->args.stable = NFS_FILE_SYNC;
852 data->res.fattr = &data->fattr;
853 data->res.count = count;
854 data->res.verf = &data->verf;
855 nfs_fattr_init(&data->fattr);
857 /* Set up the initial task struct. */
858 NFS_PROTO(inode)->write_setup(data, &msg);
860 dprintk("NFS: %5u initiated write call "
861 "(req %s/%lld, %u bytes @ offset %llu)\n",
864 (long long)NFS_FILEID(inode),
866 (unsigned long long)data->args.offset);
868 task = rpc_run_task(&task_setup_data);
870 return PTR_ERR(task);
875 /* If a nfs_flush_* function fails, it should remove reqs from @head and
876 * call this on each, which will prepare them to be retried on next
877 * writeback using standard nfs.
879 static void nfs_redirty_request(struct nfs_page *req)
881 nfs_mark_request_dirty(req);
882 nfs_end_page_writeback(req->wb_page);
883 nfs_clear_page_tag_locked(req);
887 * Generate multiple small requests to write out a single
888 * contiguous dirty area on one page.
890 static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
892 struct nfs_page *req = nfs_list_entry(head->next);
893 struct page *page = req->wb_page;
894 struct nfs_write_data *data;
895 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
901 nfs_list_remove_request(req);
905 size_t len = min(nbytes, wsize);
907 data = nfs_writedata_alloc(1);
910 list_add(&data->pages, &list);
913 } while (nbytes != 0);
914 atomic_set(&req->wb_complete, requests);
916 ClearPageError(page);
922 data = list_entry(list.next, struct nfs_write_data, pages);
923 list_del_init(&data->pages);
925 data->pagevec[0] = page;
929 ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
935 } while (nbytes != 0);
940 while (!list_empty(&list)) {
941 data = list_entry(list.next, struct nfs_write_data, pages);
942 list_del(&data->pages);
943 nfs_writedata_release(data);
945 nfs_redirty_request(req);
950 * Create an RPC task for the given write request and kick it.
951 * The page must have been locked by the caller.
953 * It may happen that the page we're passed is not marked dirty.
954 * This is the case if nfs_updatepage detects a conflicting request
955 * that has been written but not committed.
957 static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
959 struct nfs_page *req;
961 struct nfs_write_data *data;
963 data = nfs_writedata_alloc(npages);
967 pages = data->pagevec;
968 while (!list_empty(head)) {
969 req = nfs_list_entry(head->next);
970 nfs_list_remove_request(req);
971 nfs_list_add_request(req, &data->pages);
972 ClearPageError(req->wb_page);
973 *pages++ = req->wb_page;
975 req = nfs_list_entry(data->pages.next);
977 /* Set up the argument struct */
978 return nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
980 while (!list_empty(head)) {
981 req = nfs_list_entry(head->next);
982 nfs_list_remove_request(req);
983 nfs_redirty_request(req);
988 static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
989 struct inode *inode, int ioflags)
991 size_t wsize = NFS_SERVER(inode)->wsize;
993 if (wsize < PAGE_CACHE_SIZE)
994 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
996 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
1000 * Handle a write reply that flushed part of a page.
1002 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
1004 struct nfs_write_data *data = calldata;
1006 dprintk("NFS: %5u write(%s/%lld %d@%lld)",
1008 data->req->wb_context->path.dentry->d_inode->i_sb->s_id,
1010 NFS_FILEID(data->req->wb_context->path.dentry->d_inode),
1011 data->req->wb_bytes, (long long)req_offset(data->req));
1013 nfs_writeback_done(task, data);
1016 static void nfs_writeback_release_partial(void *calldata)
1018 struct nfs_write_data *data = calldata;
1019 struct nfs_page *req = data->req;
1020 struct page *page = req->wb_page;
1021 int status = data->task.tk_status;
1024 nfs_set_pageerror(page);
1025 nfs_context_set_write_error(req->wb_context, status);
1026 dprintk(", error = %d\n", status);
1030 if (nfs_write_need_commit(data)) {
1031 struct inode *inode = page->mapping->host;
1033 spin_lock(&inode->i_lock);
1034 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
1035 /* Do nothing we need to resend the writes */
1036 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1037 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1038 dprintk(" defer commit\n");
1039 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1040 set_bit(PG_NEED_RESCHED, &req->wb_flags);
1041 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
1042 dprintk(" server reboot detected\n");
1044 spin_unlock(&inode->i_lock);
1049 if (atomic_dec_and_test(&req->wb_complete))
1050 nfs_writepage_release(req);
1051 nfs_writedata_release(calldata);
1054 #if defined(CONFIG_NFS_V4_1)
1055 void nfs_write_prepare(struct rpc_task *task, void *calldata)
1057 struct nfs_write_data *data = calldata;
1058 struct nfs_client *clp = (NFS_SERVER(data->inode))->nfs_client;
1060 if (nfs4_setup_sequence(clp, &data->args.seq_args,
1061 &data->res.seq_res, 1, task))
1063 rpc_call_start(task);
1065 #endif /* CONFIG_NFS_V4_1 */
1067 static const struct rpc_call_ops nfs_write_partial_ops = {
1068 #if defined(CONFIG_NFS_V4_1)
1069 .rpc_call_prepare = nfs_write_prepare,
1070 #endif /* CONFIG_NFS_V4_1 */
1071 .rpc_call_done = nfs_writeback_done_partial,
1072 .rpc_release = nfs_writeback_release_partial,
1076 * Handle a write reply that flushes a whole page.
1078 * FIXME: There is an inherent race with invalidate_inode_pages and
1079 * writebacks since the page->count is kept > 1 for as long
1080 * as the page has a write request pending.
1082 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1084 struct nfs_write_data *data = calldata;
1086 nfs_writeback_done(task, data);
1089 static void nfs_writeback_release_full(void *calldata)
1091 struct nfs_write_data *data = calldata;
1092 int status = data->task.tk_status;
1094 /* Update attributes as result of writeback. */
1095 while (!list_empty(&data->pages)) {
1096 struct nfs_page *req = nfs_list_entry(data->pages.next);
1097 struct page *page = req->wb_page;
1099 nfs_list_remove_request(req);
1101 dprintk("NFS: %5u write (%s/%lld %d@%lld)",
1103 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1104 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1106 (long long)req_offset(req));
1109 nfs_set_pageerror(page);
1110 nfs_context_set_write_error(req->wb_context, status);
1111 dprintk(", error = %d\n", status);
1112 goto remove_request;
1115 if (nfs_write_need_commit(data)) {
1116 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1117 nfs_mark_request_commit(req);
1118 nfs_end_page_writeback(page);
1119 dprintk(" marked for commit\n");
1124 nfs_end_page_writeback(page);
1125 nfs_inode_remove_request(req);
1127 nfs_clear_page_tag_locked(req);
1129 nfs_writedata_release(calldata);
1132 static const struct rpc_call_ops nfs_write_full_ops = {
1133 #if defined(CONFIG_NFS_V4_1)
1134 .rpc_call_prepare = nfs_write_prepare,
1135 #endif /* CONFIG_NFS_V4_1 */
1136 .rpc_call_done = nfs_writeback_done_full,
1137 .rpc_release = nfs_writeback_release_full,
1142 * This function is called when the WRITE call is complete.
1144 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1146 struct nfs_writeargs *argp = &data->args;
1147 struct nfs_writeres *resp = &data->res;
1148 struct nfs_server *server = NFS_SERVER(data->inode);
1151 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1152 task->tk_pid, task->tk_status);
1155 * ->write_done will attempt to use post-op attributes to detect
1156 * conflicting writes by other clients. A strict interpretation
1157 * of close-to-open would allow us to continue caching even if
1158 * another writer had changed the file, but some applications
1159 * depend on tighter cache coherency when writing.
1161 status = NFS_PROTO(data->inode)->write_done(task, data);
1164 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1166 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1167 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1168 /* We tried a write call, but the server did not
1169 * commit data to stable storage even though we
1171 * Note: There is a known bug in Tru64 < 5.0 in which
1172 * the server reports NFS_DATA_SYNC, but performs
1173 * NFS_FILE_SYNC. We therefore implement this checking
1174 * as a dprintk() in order to avoid filling syslog.
1176 static unsigned long complain;
1178 if (time_before(complain, jiffies)) {
1179 dprintk("NFS: faulty NFS server %s:"
1180 " (committed = %d) != (stable = %d)\n",
1181 server->nfs_client->cl_hostname,
1182 resp->verf->committed, argp->stable);
1183 complain = jiffies + 300 * HZ;
1187 /* Is this a short write? */
1188 if (task->tk_status >= 0 && resp->count < argp->count) {
1189 static unsigned long complain;
1191 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1193 /* Has the server at least made some progress? */
1194 if (resp->count != 0) {
1195 /* Was this an NFSv2 write or an NFSv3 stable write? */
1196 if (resp->verf->committed != NFS_UNSTABLE) {
1197 /* Resend from where the server left off */
1198 argp->offset += resp->count;
1199 argp->pgbase += resp->count;
1200 argp->count -= resp->count;
1202 /* Resend as a stable write in order to avoid
1203 * headaches in the case of a server crash.
1205 argp->stable = NFS_FILE_SYNC;
1207 nfs4_restart_rpc(task, server->nfs_client);
1210 if (time_before(complain, jiffies)) {
1212 "NFS: Server wrote zero bytes, expected %u.\n",
1214 complain = jiffies + 300 * HZ;
1216 /* Can't do anything about it except throw an error. */
1217 task->tk_status = -EIO;
1219 nfs4_sequence_free_slot(server->nfs_client, &data->res.seq_res);
1224 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1225 void nfs_commitdata_release(void *data)
1227 struct nfs_write_data *wdata = data;
1229 put_nfs_open_context(wdata->args.context);
1230 nfs_commit_free(wdata);
1234 * Set up the argument/result storage required for the RPC call.
1236 static int nfs_commit_rpcsetup(struct list_head *head,
1237 struct nfs_write_data *data,
1240 struct nfs_page *first = nfs_list_entry(head->next);
1241 struct inode *inode = first->wb_context->path.dentry->d_inode;
1242 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1243 int priority = flush_task_priority(how);
1244 struct rpc_task *task;
1245 struct rpc_message msg = {
1246 .rpc_argp = &data->args,
1247 .rpc_resp = &data->res,
1248 .rpc_cred = first->wb_context->cred,
1250 struct rpc_task_setup task_setup_data = {
1251 .task = &data->task,
1252 .rpc_client = NFS_CLIENT(inode),
1253 .rpc_message = &msg,
1254 .callback_ops = &nfs_commit_ops,
1255 .callback_data = data,
1256 .workqueue = nfsiod_workqueue,
1258 .priority = priority,
1261 /* Set up the RPC argument and reply structs
1262 * NB: take care not to mess about with data->commit et al. */
1264 list_splice_init(head, &data->pages);
1266 data->inode = inode;
1267 data->cred = msg.rpc_cred;
1269 data->args.fh = NFS_FH(data->inode);
1270 /* Note: we always request a commit of the entire inode */
1271 data->args.offset = 0;
1272 data->args.count = 0;
1273 data->args.context = get_nfs_open_context(first->wb_context);
1274 data->res.count = 0;
1275 data->res.fattr = &data->fattr;
1276 data->res.verf = &data->verf;
1277 nfs_fattr_init(&data->fattr);
1279 /* Set up the initial task struct. */
1280 NFS_PROTO(inode)->commit_setup(data, &msg);
1282 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1284 task = rpc_run_task(&task_setup_data);
1286 return PTR_ERR(task);
1292 * Commit dirty pages
1295 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1297 struct nfs_write_data *data;
1298 struct nfs_page *req;
1300 data = nfs_commitdata_alloc();
1305 /* Set up the argument struct */
1306 return nfs_commit_rpcsetup(head, data, how);
1308 while (!list_empty(head)) {
1309 req = nfs_list_entry(head->next);
1310 nfs_list_remove_request(req);
1311 nfs_mark_request_commit(req);
1312 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1313 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1315 nfs_clear_page_tag_locked(req);
1321 * COMMIT call returned
1323 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1325 struct nfs_write_data *data = calldata;
1327 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1328 task->tk_pid, task->tk_status);
1330 /* Call the NFS version-specific code */
1331 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1335 static void nfs_commit_release(void *calldata)
1337 struct nfs_write_data *data = calldata;
1338 struct nfs_page *req;
1339 int status = data->task.tk_status;
1341 while (!list_empty(&data->pages)) {
1342 req = nfs_list_entry(data->pages.next);
1343 nfs_list_remove_request(req);
1344 nfs_clear_request_commit(req);
1346 dprintk("NFS: commit (%s/%lld %d@%lld)",
1347 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1348 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1350 (long long)req_offset(req));
1352 nfs_context_set_write_error(req->wb_context, status);
1353 nfs_inode_remove_request(req);
1354 dprintk(", error = %d\n", status);
1358 /* Okay, COMMIT succeeded, apparently. Check the verifier
1359 * returned by the server against all stored verfs. */
1360 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1361 /* We have a match */
1362 nfs_inode_remove_request(req);
1366 /* We have a mismatch. Write the page again */
1367 dprintk(" mismatch\n");
1368 nfs_mark_request_dirty(req);
1370 nfs_clear_page_tag_locked(req);
1372 nfs_commitdata_release(calldata);
1375 static const struct rpc_call_ops nfs_commit_ops = {
1376 #if defined(CONFIG_NFS_V4_1)
1377 .rpc_call_prepare = nfs_write_prepare,
1378 #endif /* CONFIG_NFS_V4_1 */
1379 .rpc_call_done = nfs_commit_done,
1380 .rpc_release = nfs_commit_release,
1383 int nfs_commit_inode(struct inode *inode, int how)
1388 spin_lock(&inode->i_lock);
1389 res = nfs_scan_commit(inode, &head, 0, 0);
1390 spin_unlock(&inode->i_lock);
1392 int error = nfs_commit_list(inode, &head, how);
1399 static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1405 long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
1407 struct inode *inode = mapping->host;
1408 pgoff_t idx_start, idx_end;
1409 unsigned int npages = 0;
1411 int nocommit = how & FLUSH_NOCOMMIT;
1415 if (wbc->range_cyclic)
1418 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1419 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1420 if (idx_end > idx_start) {
1421 pgoff_t l_npages = 1 + idx_end - idx_start;
1423 if (sizeof(npages) != sizeof(l_npages) &&
1424 (pgoff_t)npages != l_npages)
1428 how &= ~FLUSH_NOCOMMIT;
1429 spin_lock(&inode->i_lock);
1431 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1436 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1439 if (how & FLUSH_INVALIDATE) {
1440 spin_unlock(&inode->i_lock);
1441 nfs_cancel_commit_list(&head);
1443 spin_lock(&inode->i_lock);
1446 pages += nfs_scan_commit(inode, &head, 0, 0);
1447 spin_unlock(&inode->i_lock);
1448 ret = nfs_commit_list(inode, &head, how);
1449 spin_lock(&inode->i_lock);
1452 spin_unlock(&inode->i_lock);
1456 static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
1460 ret = nfs_writepages(mapping, wbc);
1463 ret = nfs_sync_mapping_wait(mapping, wbc, how);
1468 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1472 /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
1473 static int nfs_write_mapping(struct address_space *mapping, int how)
1475 struct writeback_control wbc = {
1476 .bdi = mapping->backing_dev_info,
1477 .sync_mode = WB_SYNC_ALL,
1478 .nr_to_write = LONG_MAX,
1480 .range_end = LLONG_MAX,
1481 .for_writepages = 1,
1484 return __nfs_write_mapping(mapping, &wbc, how);
1488 * flush the inode to disk.
1490 int nfs_wb_all(struct inode *inode)
1492 return nfs_write_mapping(inode->i_mapping, 0);
1495 int nfs_wb_nocommit(struct inode *inode)
1497 return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
1500 int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1502 struct nfs_page *req;
1503 loff_t range_start = page_offset(page);
1504 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1505 struct writeback_control wbc = {
1506 .bdi = page->mapping->backing_dev_info,
1507 .sync_mode = WB_SYNC_ALL,
1508 .nr_to_write = LONG_MAX,
1509 .range_start = range_start,
1510 .range_end = range_end,
1514 BUG_ON(!PageLocked(page));
1516 req = nfs_page_find_request(page);
1519 if (test_bit(PG_CLEAN, &req->wb_flags)) {
1520 nfs_release_request(req);
1523 if (nfs_lock_request_dontget(req)) {
1524 nfs_inode_remove_request(req);
1526 * In case nfs_inode_remove_request has marked the
1527 * page as being dirty
1529 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1530 nfs_unlock_request(req);
1533 ret = nfs_wait_on_request(req);
1537 if (!PagePrivate(page))
1539 ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
1544 static int nfs_wb_page_priority(struct inode *inode, struct page *page,
1547 loff_t range_start = page_offset(page);
1548 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1549 struct writeback_control wbc = {
1550 .bdi = page->mapping->backing_dev_info,
1551 .sync_mode = WB_SYNC_ALL,
1552 .nr_to_write = LONG_MAX,
1553 .range_start = range_start,
1554 .range_end = range_end,
1559 if (clear_page_dirty_for_io(page)) {
1560 ret = nfs_writepage_locked(page, &wbc);
1563 } else if (!PagePrivate(page))
1565 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1568 } while (PagePrivate(page));
1571 __mark_inode_dirty(inode, I_DIRTY_PAGES);
1576 * Write back all requests on one page - we do this before reading it.
1578 int nfs_wb_page(struct inode *inode, struct page* page)
1580 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
1583 int __init nfs_init_writepagecache(void)
1585 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1586 sizeof(struct nfs_write_data),
1587 0, SLAB_HWCACHE_ALIGN,
1589 if (nfs_wdata_cachep == NULL)
1592 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1594 if (nfs_wdata_mempool == NULL)
1597 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1599 if (nfs_commit_mempool == NULL)
1603 * NFS congestion size, scale with available memory.
1615 * This allows larger machines to have larger/more transfers.
1616 * Limit the default to 256M
1618 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1619 if (nfs_congestion_kb > 256*1024)
1620 nfs_congestion_kb = 256*1024;
1625 void nfs_destroy_writepagecache(void)
1627 mempool_destroy(nfs_commit_mempool);
1628 mempool_destroy(nfs_wdata_mempool);
1629 kmem_cache_destroy(nfs_wdata_cachep);