4 * Write file data over NFS.
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 struct nfs_page * nfs_update_request(struct nfs_open_context*,
39 unsigned int, unsigned int);
40 static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc,
41 struct inode *inode, int ioflags);
42 static const struct rpc_call_ops nfs_write_partial_ops;
43 static const struct rpc_call_ops nfs_write_full_ops;
44 static const struct rpc_call_ops nfs_commit_ops;
46 static struct kmem_cache *nfs_wdata_cachep;
47 static mempool_t *nfs_wdata_mempool;
48 static mempool_t *nfs_commit_mempool;
50 struct nfs_write_data *nfs_commit_alloc(void)
52 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
55 memset(p, 0, sizeof(*p));
56 INIT_LIST_HEAD(&p->pages);
61 static void nfs_commit_rcu_free(struct rcu_head *head)
63 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
64 if (p && (p->pagevec != &p->page_array[0]))
66 mempool_free(p, nfs_commit_mempool);
69 void nfs_commit_free(struct nfs_write_data *wdata)
71 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
74 struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
76 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
79 memset(p, 0, sizeof(*p));
80 INIT_LIST_HEAD(&p->pages);
81 p->npages = pagecount;
82 if (pagecount <= ARRAY_SIZE(p->page_array))
83 p->pagevec = p->page_array;
85 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
87 mempool_free(p, nfs_wdata_mempool);
95 static void nfs_writedata_rcu_free(struct rcu_head *head)
97 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
98 if (p && (p->pagevec != &p->page_array[0]))
100 mempool_free(p, nfs_wdata_mempool);
103 static void nfs_writedata_free(struct nfs_write_data *wdata)
105 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
108 void nfs_writedata_release(void *data)
110 struct nfs_write_data *wdata = data;
112 put_nfs_open_context(wdata->args.context);
113 nfs_writedata_free(wdata);
116 static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
120 set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
123 static struct nfs_page *nfs_page_find_request_locked(struct page *page)
125 struct nfs_page *req = NULL;
127 if (PagePrivate(page)) {
128 req = (struct nfs_page *)page_private(page);
130 kref_get(&req->wb_kref);
135 static struct nfs_page *nfs_page_find_request(struct page *page)
137 struct inode *inode = page->mapping->host;
138 struct nfs_page *req = NULL;
140 spin_lock(&inode->i_lock);
141 req = nfs_page_find_request_locked(page);
142 spin_unlock(&inode->i_lock);
146 /* Adjust the file length if we're writing beyond the end */
147 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
149 struct inode *inode = page->mapping->host;
150 loff_t end, i_size = i_size_read(inode);
151 pgoff_t end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
153 if (i_size > 0 && page->index < end_index)
155 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
158 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
159 i_size_write(inode, end);
162 /* A writeback failed: mark the page as bad, and invalidate the page cache */
163 static void nfs_set_pageerror(struct page *page)
166 nfs_zap_mapping(page->mapping->host, page->mapping);
169 /* We can set the PG_uptodate flag if we see that a write request
170 * covers the full page.
172 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
174 if (PageUptodate(page))
178 if (count != nfs_page_length(page))
180 SetPageUptodate(page);
183 static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
184 unsigned int offset, unsigned int count)
186 struct nfs_page *req;
190 req = nfs_update_request(ctx, page, offset, count);
196 ret = nfs_wb_page(page->mapping->host, page);
200 /* Update file length */
201 nfs_grow_file(page, offset, count);
202 nfs_clear_page_tag_locked(req);
206 static int wb_priority(struct writeback_control *wbc)
208 if (wbc->for_reclaim)
209 return FLUSH_HIGHPRI | FLUSH_STABLE;
210 if (wbc->for_kupdate)
216 * NFS congestion control
219 int nfs_congestion_kb;
221 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
222 #define NFS_CONGESTION_OFF_THRESH \
223 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
225 static int nfs_set_page_writeback(struct page *page)
227 int ret = test_set_page_writeback(page);
230 struct inode *inode = page->mapping->host;
231 struct nfs_server *nfss = NFS_SERVER(inode);
233 if (atomic_long_inc_return(&nfss->writeback) >
234 NFS_CONGESTION_ON_THRESH)
235 set_bdi_congested(&nfss->backing_dev_info, WRITE);
240 static void nfs_end_page_writeback(struct page *page)
242 struct inode *inode = page->mapping->host;
243 struct nfs_server *nfss = NFS_SERVER(inode);
245 end_page_writeback(page);
246 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
247 clear_bdi_congested(&nfss->backing_dev_info, WRITE);
251 * Find an associated nfs write request, and prepare to flush it out
252 * May return an error if the user signalled nfs_wait_on_request().
254 static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
257 struct inode *inode = page->mapping->host;
258 struct nfs_page *req;
261 spin_lock(&inode->i_lock);
263 req = nfs_page_find_request_locked(page);
265 spin_unlock(&inode->i_lock);
268 if (nfs_set_page_tag_locked(req))
270 /* Note: If we hold the page lock, as is the case in nfs_writepage,
271 * then the call to nfs_set_page_tag_locked() will always
272 * succeed provided that someone hasn't already marked the
273 * request as dirty (in which case we don't care).
275 spin_unlock(&inode->i_lock);
276 ret = nfs_wait_on_request(req);
277 nfs_release_request(req);
280 spin_lock(&inode->i_lock);
282 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
283 /* This request is marked for commit */
284 spin_unlock(&inode->i_lock);
285 nfs_clear_page_tag_locked(req);
286 nfs_pageio_complete(pgio);
289 if (nfs_set_page_writeback(page) != 0) {
290 spin_unlock(&inode->i_lock);
293 spin_unlock(&inode->i_lock);
294 nfs_pageio_add_request(pgio, req);
298 static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
300 struct inode *inode = page->mapping->host;
302 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
303 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
305 nfs_pageio_cond_complete(pgio, page->index);
306 return nfs_page_async_flush(pgio, page);
310 * Write an mmapped page to the server.
312 static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
314 struct nfs_pageio_descriptor pgio;
317 nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
318 err = nfs_do_writepage(page, wbc, &pgio);
319 nfs_pageio_complete(&pgio);
322 if (pgio.pg_error < 0)
323 return pgio.pg_error;
327 int nfs_writepage(struct page *page, struct writeback_control *wbc)
331 ret = nfs_writepage_locked(page, wbc);
336 static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
340 ret = nfs_do_writepage(page, wbc, data);
345 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
347 struct inode *inode = mapping->host;
348 struct nfs_pageio_descriptor pgio;
351 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
353 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
354 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
355 nfs_pageio_complete(&pgio);
358 if (pgio.pg_error < 0)
359 return pgio.pg_error;
364 * Insert a write request into an inode
366 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
368 struct nfs_inode *nfsi = NFS_I(inode);
371 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
372 BUG_ON(error == -EEXIST);
377 if (nfs_have_delegation(inode, FMODE_WRITE))
380 SetPagePrivate(req->wb_page);
381 set_page_private(req->wb_page, (unsigned long)req);
383 kref_get(&req->wb_kref);
384 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_LOCKED);
389 * Remove a write request from an inode
391 static void nfs_inode_remove_request(struct nfs_page *req)
393 struct inode *inode = req->wb_context->path.dentry->d_inode;
394 struct nfs_inode *nfsi = NFS_I(inode);
396 BUG_ON (!NFS_WBACK_BUSY(req));
398 spin_lock(&inode->i_lock);
399 set_page_private(req->wb_page, 0);
400 ClearPagePrivate(req->wb_page);
401 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
404 spin_unlock(&inode->i_lock);
407 spin_unlock(&inode->i_lock);
408 nfs_clear_request(req);
409 nfs_release_request(req);
413 nfs_redirty_request(struct nfs_page *req)
415 __set_page_dirty_nobuffers(req->wb_page);
419 * Check if a request is dirty
422 nfs_dirty_request(struct nfs_page *req)
424 struct page *page = req->wb_page;
426 if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
428 return !PageWriteback(req->wb_page);
431 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
433 * Add a request to the inode's commit list.
436 nfs_mark_request_commit(struct nfs_page *req)
438 struct inode *inode = req->wb_context->path.dentry->d_inode;
439 struct nfs_inode *nfsi = NFS_I(inode);
441 spin_lock(&inode->i_lock);
443 set_bit(PG_NEED_COMMIT, &(req)->wb_flags);
444 radix_tree_tag_set(&nfsi->nfs_page_tree,
446 NFS_PAGE_TAG_COMMIT);
447 spin_unlock(&inode->i_lock);
448 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
449 inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
450 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
454 int nfs_write_need_commit(struct nfs_write_data *data)
456 return data->verf.committed != NFS_FILE_SYNC;
460 int nfs_reschedule_unstable_write(struct nfs_page *req)
462 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
463 nfs_mark_request_commit(req);
466 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
467 nfs_redirty_request(req);
474 nfs_mark_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 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
536 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
538 nfs_list_remove_request(req);
539 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
540 nfs_inode_remove_request(req);
541 nfs_unlock_request(req);
545 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
547 * nfs_scan_commit - Scan an inode for commit requests
548 * @inode: NFS inode to scan
549 * @dst: destination list
550 * @idx_start: lower bound of page->index to scan.
551 * @npages: idx_start + npages sets the upper bound to scan.
553 * Moves requests from the inode's 'commit' request list.
554 * The requests are *not* checked to ensure that they form a contiguous set.
557 nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
559 struct nfs_inode *nfsi = NFS_I(inode);
562 if (nfsi->ncommit != 0) {
563 res = nfs_scan_list(nfsi, dst, idx_start, npages,
564 NFS_PAGE_TAG_COMMIT);
565 nfsi->ncommit -= res;
570 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
577 * Try to update any existing write request, or create one if there is none.
578 * In order to match, the request's credentials must match those of
579 * the calling process.
581 * Note: Should always be called with the Page Lock held!
583 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
584 struct page *page, unsigned int offset, unsigned int bytes)
586 struct address_space *mapping = page->mapping;
587 struct inode *inode = mapping->host;
588 struct nfs_page *req, *new = NULL;
591 end = offset + bytes;
594 /* Loop over all inode entries and see if we find
595 * A request for the page we wish to update
597 spin_lock(&inode->i_lock);
598 req = nfs_page_find_request_locked(page);
600 if (!nfs_set_page_tag_locked(req)) {
603 spin_unlock(&inode->i_lock);
604 error = nfs_wait_on_request(req);
605 nfs_release_request(req);
608 nfs_release_request(new);
609 return ERR_PTR(error);
613 spin_unlock(&inode->i_lock);
615 nfs_release_request(new);
621 nfs_lock_request_dontget(new);
622 error = nfs_inode_add_request(inode, new);
624 spin_unlock(&inode->i_lock);
625 nfs_unlock_request(new);
626 return ERR_PTR(error);
628 spin_unlock(&inode->i_lock);
632 spin_unlock(&inode->i_lock);
634 new = nfs_create_request(ctx, inode, page, offset, bytes);
639 /* We have a request for our page.
640 * If the creds don't match, or the
641 * page addresses don't match,
642 * tell the caller to wait on the conflicting
645 rqend = req->wb_offset + req->wb_bytes;
646 if (req->wb_context != ctx
647 || req->wb_page != page
648 || !nfs_dirty_request(req)
649 || offset > rqend || end < req->wb_offset) {
650 nfs_clear_page_tag_locked(req);
651 return ERR_PTR(-EBUSY);
654 /* Okay, the request matches. Update the region */
655 if (offset < req->wb_offset) {
656 req->wb_offset = offset;
657 req->wb_pgbase = offset;
658 req->wb_bytes = max(end, rqend) - req->wb_offset;
663 req->wb_bytes = end - req->wb_offset;
667 /* If this page might potentially be marked as up to date,
668 * then we need to zero any uninitalised data. */
669 if (req->wb_pgbase == 0 && req->wb_bytes != PAGE_CACHE_SIZE
670 && !PageUptodate(req->wb_page))
671 zero_user_segment(req->wb_page, req->wb_bytes, PAGE_CACHE_SIZE);
675 int nfs_flush_incompatible(struct file *file, struct page *page)
677 struct nfs_open_context *ctx = nfs_file_open_context(file);
678 struct nfs_page *req;
679 int do_flush, status;
681 * Look for a request corresponding to this page. If there
682 * is one, and it belongs to another file, we flush it out
683 * before we try to copy anything into the page. Do this
684 * due to the lack of an ACCESS-type call in NFSv2.
685 * Also do the same if we find a request from an existing
689 req = nfs_page_find_request(page);
692 do_flush = req->wb_page != page || req->wb_context != ctx
693 || !nfs_dirty_request(req);
694 nfs_release_request(req);
697 status = nfs_wb_page(page->mapping->host, page);
698 } while (status == 0);
703 * If the page cache is marked as unsafe or invalid, then we can't rely on
704 * the PageUptodate() flag. In this case, we will need to turn off
705 * write optimisations that depend on the page contents being correct.
707 static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
709 return PageUptodate(page) &&
710 !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
714 * Update and possibly write a cached page of an NFS file.
716 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
717 * things with a page scheduled for an RPC call (e.g. invalidate it).
719 int nfs_updatepage(struct file *file, struct page *page,
720 unsigned int offset, unsigned int count)
722 struct nfs_open_context *ctx = nfs_file_open_context(file);
723 struct inode *inode = page->mapping->host;
726 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
728 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
729 file->f_path.dentry->d_parent->d_name.name,
730 file->f_path.dentry->d_name.name, count,
731 (long long)(page_offset(page) +offset));
733 /* If we're not using byte range locks, and we know the page
734 * is up to date, it may be more efficient to extend the write
735 * to cover the entire page in order to avoid fragmentation
738 if (nfs_write_pageuptodate(page, inode) &&
739 inode->i_flock == NULL &&
740 !(file->f_flags & O_SYNC)) {
741 count = max(count + offset, nfs_page_length(page));
745 status = nfs_writepage_setup(ctx, page, offset, count);
746 __set_page_dirty_nobuffers(page);
748 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
749 status, (long long)i_size_read(inode));
751 nfs_set_pageerror(page);
755 static void nfs_writepage_release(struct nfs_page *req)
758 if (PageError(req->wb_page)) {
759 nfs_end_page_writeback(req->wb_page);
760 nfs_inode_remove_request(req);
761 } else if (!nfs_reschedule_unstable_write(req)) {
762 /* Set the PG_uptodate flag */
763 nfs_mark_uptodate(req->wb_page, req->wb_pgbase, req->wb_bytes);
764 nfs_end_page_writeback(req->wb_page);
765 nfs_inode_remove_request(req);
767 nfs_end_page_writeback(req->wb_page);
768 nfs_clear_page_tag_locked(req);
771 static int flush_task_priority(int how)
773 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
775 return RPC_PRIORITY_HIGH;
777 return RPC_PRIORITY_LOW;
779 return RPC_PRIORITY_NORMAL;
783 * Set up the argument/result storage required for the RPC call.
785 static void nfs_write_rpcsetup(struct nfs_page *req,
786 struct nfs_write_data *data,
787 const struct rpc_call_ops *call_ops,
788 unsigned int count, unsigned int offset,
791 struct inode *inode = req->wb_context->path.dentry->d_inode;
792 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
793 int priority = flush_task_priority(how);
794 struct rpc_task *task;
795 struct rpc_message msg = {
796 .rpc_argp = &data->args,
797 .rpc_resp = &data->res,
798 .rpc_cred = req->wb_context->cred,
800 struct rpc_task_setup task_setup_data = {
801 .rpc_client = NFS_CLIENT(inode),
804 .callback_ops = call_ops,
805 .callback_data = data,
807 .priority = priority,
810 /* Set up the RPC argument and reply structs
811 * NB: take care not to mess about with data->commit et al. */
814 data->inode = inode = req->wb_context->path.dentry->d_inode;
815 data->cred = msg.rpc_cred;
817 data->args.fh = NFS_FH(inode);
818 data->args.offset = req_offset(req) + offset;
819 data->args.pgbase = req->wb_pgbase + offset;
820 data->args.pages = data->pagevec;
821 data->args.count = count;
822 data->args.context = get_nfs_open_context(req->wb_context);
823 data->args.stable = NFS_UNSTABLE;
824 if (how & FLUSH_STABLE) {
825 data->args.stable = NFS_DATA_SYNC;
826 if (!NFS_I(inode)->ncommit)
827 data->args.stable = NFS_FILE_SYNC;
830 data->res.fattr = &data->fattr;
831 data->res.count = count;
832 data->res.verf = &data->verf;
833 nfs_fattr_init(&data->fattr);
835 /* Set up the initial task struct. */
836 NFS_PROTO(inode)->write_setup(data, &msg);
838 dprintk("NFS: %5u initiated write call "
839 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
842 (long long)NFS_FILEID(inode),
844 (unsigned long long)data->args.offset);
846 task = rpc_run_task(&task_setup_data);
852 * Generate multiple small requests to write out a single
853 * contiguous dirty area on one page.
855 static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
857 struct nfs_page *req = nfs_list_entry(head->next);
858 struct page *page = req->wb_page;
859 struct nfs_write_data *data;
860 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
865 nfs_list_remove_request(req);
869 size_t len = min(nbytes, wsize);
871 data = nfs_writedata_alloc(1);
874 list_add(&data->pages, &list);
877 } while (nbytes != 0);
878 atomic_set(&req->wb_complete, requests);
880 ClearPageError(page);
884 data = list_entry(list.next, struct nfs_write_data, pages);
885 list_del_init(&data->pages);
887 data->pagevec[0] = page;
891 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
895 } while (nbytes != 0);
900 while (!list_empty(&list)) {
901 data = list_entry(list.next, struct nfs_write_data, pages);
902 list_del(&data->pages);
903 nfs_writedata_release(data);
905 nfs_redirty_request(req);
906 nfs_end_page_writeback(req->wb_page);
907 nfs_clear_page_tag_locked(req);
912 * Create an RPC task for the given write request and kick it.
913 * The page must have been locked by the caller.
915 * It may happen that the page we're passed is not marked dirty.
916 * This is the case if nfs_updatepage detects a conflicting request
917 * that has been written but not committed.
919 static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
921 struct nfs_page *req;
923 struct nfs_write_data *data;
925 data = nfs_writedata_alloc(npages);
929 pages = data->pagevec;
930 while (!list_empty(head)) {
931 req = nfs_list_entry(head->next);
932 nfs_list_remove_request(req);
933 nfs_list_add_request(req, &data->pages);
934 ClearPageError(req->wb_page);
935 *pages++ = req->wb_page;
937 req = nfs_list_entry(data->pages.next);
939 /* Set up the argument struct */
940 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
944 while (!list_empty(head)) {
945 req = nfs_list_entry(head->next);
946 nfs_list_remove_request(req);
947 nfs_redirty_request(req);
948 nfs_end_page_writeback(req->wb_page);
949 nfs_clear_page_tag_locked(req);
954 static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
955 struct inode *inode, int ioflags)
957 size_t wsize = NFS_SERVER(inode)->wsize;
959 if (wsize < PAGE_CACHE_SIZE)
960 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
962 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
966 * Handle a write reply that flushed part of a page.
968 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
970 struct nfs_write_data *data = calldata;
971 struct nfs_page *req = data->req;
972 struct page *page = req->wb_page;
974 dprintk("NFS: write (%s/%Ld %d@%Ld)",
975 req->wb_context->path.dentry->d_inode->i_sb->s_id,
976 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
978 (long long)req_offset(req));
980 if (nfs_writeback_done(task, data) != 0)
983 if (task->tk_status < 0) {
984 nfs_set_pageerror(page);
985 nfs_context_set_write_error(req->wb_context, task->tk_status);
986 dprintk(", error = %d\n", task->tk_status);
990 if (nfs_write_need_commit(data)) {
991 struct inode *inode = page->mapping->host;
993 spin_lock(&inode->i_lock);
994 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
995 /* Do nothing we need to resend the writes */
996 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
997 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
998 dprintk(" defer commit\n");
999 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1000 set_bit(PG_NEED_RESCHED, &req->wb_flags);
1001 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
1002 dprintk(" server reboot detected\n");
1004 spin_unlock(&inode->i_lock);
1009 if (atomic_dec_and_test(&req->wb_complete))
1010 nfs_writepage_release(req);
1013 static const struct rpc_call_ops nfs_write_partial_ops = {
1014 .rpc_call_done = nfs_writeback_done_partial,
1015 .rpc_release = nfs_writedata_release,
1019 * Handle a write reply that flushes a whole page.
1021 * FIXME: There is an inherent race with invalidate_inode_pages and
1022 * writebacks since the page->count is kept > 1 for as long
1023 * as the page has a write request pending.
1025 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1027 struct nfs_write_data *data = calldata;
1028 struct nfs_page *req;
1031 if (nfs_writeback_done(task, data) != 0)
1034 /* Update attributes as result of writeback. */
1035 while (!list_empty(&data->pages)) {
1036 req = nfs_list_entry(data->pages.next);
1037 nfs_list_remove_request(req);
1038 page = req->wb_page;
1040 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1041 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1042 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1044 (long long)req_offset(req));
1046 if (task->tk_status < 0) {
1047 nfs_set_pageerror(page);
1048 nfs_context_set_write_error(req->wb_context, task->tk_status);
1049 dprintk(", error = %d\n", task->tk_status);
1050 goto remove_request;
1053 if (nfs_write_need_commit(data)) {
1054 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1055 nfs_mark_request_commit(req);
1056 nfs_end_page_writeback(page);
1057 dprintk(" marked for commit\n");
1060 /* Set the PG_uptodate flag? */
1061 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
1064 nfs_end_page_writeback(page);
1065 nfs_inode_remove_request(req);
1067 nfs_clear_page_tag_locked(req);
1071 static const struct rpc_call_ops nfs_write_full_ops = {
1072 .rpc_call_done = nfs_writeback_done_full,
1073 .rpc_release = nfs_writedata_release,
1078 * This function is called when the WRITE call is complete.
1080 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1082 struct nfs_writeargs *argp = &data->args;
1083 struct nfs_writeres *resp = &data->res;
1086 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1087 task->tk_pid, task->tk_status);
1090 * ->write_done will attempt to use post-op attributes to detect
1091 * conflicting writes by other clients. A strict interpretation
1092 * of close-to-open would allow us to continue caching even if
1093 * another writer had changed the file, but some applications
1094 * depend on tighter cache coherency when writing.
1096 status = NFS_PROTO(data->inode)->write_done(task, data);
1099 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1101 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1102 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1103 /* We tried a write call, but the server did not
1104 * commit data to stable storage even though we
1106 * Note: There is a known bug in Tru64 < 5.0 in which
1107 * the server reports NFS_DATA_SYNC, but performs
1108 * NFS_FILE_SYNC. We therefore implement this checking
1109 * as a dprintk() in order to avoid filling syslog.
1111 static unsigned long complain;
1113 if (time_before(complain, jiffies)) {
1114 dprintk("NFS: faulty NFS server %s:"
1115 " (committed = %d) != (stable = %d)\n",
1116 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1117 resp->verf->committed, argp->stable);
1118 complain = jiffies + 300 * HZ;
1122 /* Is this a short write? */
1123 if (task->tk_status >= 0 && resp->count < argp->count) {
1124 static unsigned long complain;
1126 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1128 /* Has the server at least made some progress? */
1129 if (resp->count != 0) {
1130 /* Was this an NFSv2 write or an NFSv3 stable write? */
1131 if (resp->verf->committed != NFS_UNSTABLE) {
1132 /* Resend from where the server left off */
1133 argp->offset += resp->count;
1134 argp->pgbase += resp->count;
1135 argp->count -= resp->count;
1137 /* Resend as a stable write in order to avoid
1138 * headaches in the case of a server crash.
1140 argp->stable = NFS_FILE_SYNC;
1142 rpc_restart_call(task);
1145 if (time_before(complain, jiffies)) {
1147 "NFS: Server wrote zero bytes, expected %u.\n",
1149 complain = jiffies + 300 * HZ;
1151 /* Can't do anything about it except throw an error. */
1152 task->tk_status = -EIO;
1158 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1159 void nfs_commit_release(void *data)
1161 struct nfs_write_data *wdata = data;
1163 put_nfs_open_context(wdata->args.context);
1164 nfs_commit_free(wdata);
1168 * Set up the argument/result storage required for the RPC call.
1170 static void nfs_commit_rpcsetup(struct list_head *head,
1171 struct nfs_write_data *data,
1174 struct nfs_page *first = nfs_list_entry(head->next);
1175 struct inode *inode = first->wb_context->path.dentry->d_inode;
1176 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1177 int priority = flush_task_priority(how);
1178 struct rpc_task *task;
1179 struct rpc_message msg = {
1180 .rpc_argp = &data->args,
1181 .rpc_resp = &data->res,
1182 .rpc_cred = first->wb_context->cred,
1184 struct rpc_task_setup task_setup_data = {
1185 .task = &data->task,
1186 .rpc_client = NFS_CLIENT(inode),
1187 .rpc_message = &msg,
1188 .callback_ops = &nfs_commit_ops,
1189 .callback_data = data,
1191 .priority = priority,
1194 /* Set up the RPC argument and reply structs
1195 * NB: take care not to mess about with data->commit et al. */
1197 list_splice_init(head, &data->pages);
1199 data->inode = inode;
1200 data->cred = msg.rpc_cred;
1202 data->args.fh = NFS_FH(data->inode);
1203 /* Note: we always request a commit of the entire inode */
1204 data->args.offset = 0;
1205 data->args.count = 0;
1206 data->args.context = get_nfs_open_context(first->wb_context);
1207 data->res.count = 0;
1208 data->res.fattr = &data->fattr;
1209 data->res.verf = &data->verf;
1210 nfs_fattr_init(&data->fattr);
1212 /* Set up the initial task struct. */
1213 NFS_PROTO(inode)->commit_setup(data, &msg);
1215 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1217 task = rpc_run_task(&task_setup_data);
1223 * Commit dirty pages
1226 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1228 struct nfs_write_data *data;
1229 struct nfs_page *req;
1231 data = nfs_commit_alloc();
1236 /* Set up the argument struct */
1237 nfs_commit_rpcsetup(head, data, how);
1241 while (!list_empty(head)) {
1242 req = nfs_list_entry(head->next);
1243 nfs_list_remove_request(req);
1244 nfs_mark_request_commit(req);
1245 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1246 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1248 nfs_clear_page_tag_locked(req);
1254 * COMMIT call returned
1256 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1258 struct nfs_write_data *data = calldata;
1259 struct nfs_page *req;
1261 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1262 task->tk_pid, task->tk_status);
1264 /* Call the NFS version-specific code */
1265 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1268 while (!list_empty(&data->pages)) {
1269 req = nfs_list_entry(data->pages.next);
1270 nfs_list_remove_request(req);
1271 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
1272 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1273 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1276 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1277 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1278 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1280 (long long)req_offset(req));
1281 if (task->tk_status < 0) {
1282 nfs_context_set_write_error(req->wb_context, task->tk_status);
1283 nfs_inode_remove_request(req);
1284 dprintk(", error = %d\n", task->tk_status);
1288 /* Okay, COMMIT succeeded, apparently. Check the verifier
1289 * returned by the server against all stored verfs. */
1290 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1291 /* We have a match */
1292 /* Set the PG_uptodate flag */
1293 nfs_mark_uptodate(req->wb_page, req->wb_pgbase,
1295 nfs_inode_remove_request(req);
1299 /* We have a mismatch. Write the page again */
1300 dprintk(" mismatch\n");
1301 nfs_redirty_request(req);
1303 nfs_clear_page_tag_locked(req);
1307 static const struct rpc_call_ops nfs_commit_ops = {
1308 .rpc_call_done = nfs_commit_done,
1309 .rpc_release = nfs_commit_release,
1312 int nfs_commit_inode(struct inode *inode, int how)
1317 spin_lock(&inode->i_lock);
1318 res = nfs_scan_commit(inode, &head, 0, 0);
1319 spin_unlock(&inode->i_lock);
1321 int error = nfs_commit_list(inode, &head, how);
1328 static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1334 long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
1336 struct inode *inode = mapping->host;
1337 pgoff_t idx_start, idx_end;
1338 unsigned int npages = 0;
1340 int nocommit = how & FLUSH_NOCOMMIT;
1344 if (wbc->range_cyclic)
1347 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1348 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1349 if (idx_end > idx_start) {
1350 pgoff_t l_npages = 1 + idx_end - idx_start;
1352 if (sizeof(npages) != sizeof(l_npages) &&
1353 (pgoff_t)npages != l_npages)
1357 how &= ~FLUSH_NOCOMMIT;
1358 spin_lock(&inode->i_lock);
1360 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1365 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1368 if (how & FLUSH_INVALIDATE) {
1369 spin_unlock(&inode->i_lock);
1370 nfs_cancel_commit_list(&head);
1372 spin_lock(&inode->i_lock);
1375 pages += nfs_scan_commit(inode, &head, 0, 0);
1376 spin_unlock(&inode->i_lock);
1377 ret = nfs_commit_list(inode, &head, how);
1378 spin_lock(&inode->i_lock);
1381 spin_unlock(&inode->i_lock);
1385 static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
1389 ret = nfs_writepages(mapping, wbc);
1392 ret = nfs_sync_mapping_wait(mapping, wbc, how);
1397 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1401 /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
1402 static int nfs_write_mapping(struct address_space *mapping, int how)
1404 struct writeback_control wbc = {
1405 .bdi = mapping->backing_dev_info,
1406 .sync_mode = WB_SYNC_NONE,
1407 .nr_to_write = LONG_MAX,
1408 .for_writepages = 1,
1413 ret = __nfs_write_mapping(mapping, &wbc, how);
1416 wbc.sync_mode = WB_SYNC_ALL;
1417 return __nfs_write_mapping(mapping, &wbc, how);
1421 * flush the inode to disk.
1423 int nfs_wb_all(struct inode *inode)
1425 return nfs_write_mapping(inode->i_mapping, 0);
1428 int nfs_wb_nocommit(struct inode *inode)
1430 return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
1433 int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1435 struct nfs_page *req;
1436 loff_t range_start = page_offset(page);
1437 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1438 struct writeback_control wbc = {
1439 .bdi = page->mapping->backing_dev_info,
1440 .sync_mode = WB_SYNC_ALL,
1441 .nr_to_write = LONG_MAX,
1442 .range_start = range_start,
1443 .range_end = range_end,
1447 BUG_ON(!PageLocked(page));
1449 req = nfs_page_find_request(page);
1452 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1453 nfs_release_request(req);
1456 if (nfs_lock_request_dontget(req)) {
1457 nfs_inode_remove_request(req);
1459 * In case nfs_inode_remove_request has marked the
1460 * page as being dirty
1462 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1463 nfs_unlock_request(req);
1466 ret = nfs_wait_on_request(req);
1470 if (!PagePrivate(page))
1472 ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
1477 static int nfs_wb_page_priority(struct inode *inode, struct page *page,
1480 loff_t range_start = page_offset(page);
1481 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1482 struct writeback_control wbc = {
1483 .bdi = page->mapping->backing_dev_info,
1484 .sync_mode = WB_SYNC_ALL,
1485 .nr_to_write = LONG_MAX,
1486 .range_start = range_start,
1487 .range_end = range_end,
1491 BUG_ON(!PageLocked(page));
1492 if (clear_page_dirty_for_io(page)) {
1493 ret = nfs_writepage_locked(page, &wbc);
1497 if (!PagePrivate(page))
1499 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1503 __mark_inode_dirty(inode, I_DIRTY_PAGES);
1508 * Write back all requests on one page - we do this before reading it.
1510 int nfs_wb_page(struct inode *inode, struct page* page)
1512 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
1515 int __init nfs_init_writepagecache(void)
1517 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1518 sizeof(struct nfs_write_data),
1519 0, SLAB_HWCACHE_ALIGN,
1521 if (nfs_wdata_cachep == NULL)
1524 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1526 if (nfs_wdata_mempool == NULL)
1529 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1531 if (nfs_commit_mempool == NULL)
1535 * NFS congestion size, scale with available memory.
1547 * This allows larger machines to have larger/more transfers.
1548 * Limit the default to 256M
1550 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1551 if (nfs_congestion_kb > 256*1024)
1552 nfs_congestion_kb = 256*1024;
1557 void nfs_destroy_writepagecache(void)
1559 mempool_destroy(nfs_commit_mempool);
1560 mempool_destroy(nfs_wdata_mempool);
1561 kmem_cache_destroy(nfs_wdata_cachep);