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 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 *wdata)
110 nfs_writedata_free(wdata);
113 static struct nfs_page *nfs_page_find_request_locked(struct page *page)
115 struct nfs_page *req = NULL;
117 if (PagePrivate(page)) {
118 req = (struct nfs_page *)page_private(page);
120 kref_get(&req->wb_kref);
125 static struct nfs_page *nfs_page_find_request(struct page *page)
127 struct inode *inode = page->mapping->host;
128 struct nfs_page *req = NULL;
130 spin_lock(&inode->i_lock);
131 req = nfs_page_find_request_locked(page);
132 spin_unlock(&inode->i_lock);
136 /* Adjust the file length if we're writing beyond the end */
137 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
139 struct inode *inode = page->mapping->host;
140 loff_t end, i_size = i_size_read(inode);
141 pgoff_t 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 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
149 i_size_write(inode, end);
152 /* A writeback failed: mark the page as bad, and invalidate the page cache */
153 static void nfs_set_pageerror(struct page *page)
156 nfs_zap_mapping(page->mapping->host, page->mapping);
159 /* We can set the PG_uptodate flag if we see that a write request
160 * covers the full page.
162 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
164 if (PageUptodate(page))
168 if (count != nfs_page_length(page))
170 if (count != PAGE_CACHE_SIZE)
171 zero_user_page(page, count, PAGE_CACHE_SIZE - count, KM_USER0);
172 SetPageUptodate(page);
175 static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
176 unsigned int offset, unsigned int count)
178 struct nfs_page *req;
182 req = nfs_update_request(ctx, page, offset, count);
188 ret = nfs_wb_page(page->mapping->host, page);
192 /* Update file length */
193 nfs_grow_file(page, offset, count);
194 nfs_unlock_request(req);
198 static int wb_priority(struct writeback_control *wbc)
200 if (wbc->for_reclaim)
201 return FLUSH_HIGHPRI | FLUSH_STABLE;
202 if (wbc->for_kupdate)
208 * NFS congestion control
211 int nfs_congestion_kb;
213 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
214 #define NFS_CONGESTION_OFF_THRESH \
215 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
217 static int nfs_set_page_writeback(struct page *page)
219 int ret = test_set_page_writeback(page);
222 struct inode *inode = page->mapping->host;
223 struct nfs_server *nfss = NFS_SERVER(inode);
225 if (atomic_long_inc_return(&nfss->writeback) >
226 NFS_CONGESTION_ON_THRESH)
227 set_bdi_congested(&nfss->backing_dev_info, WRITE);
232 static void nfs_end_page_writeback(struct page *page)
234 struct inode *inode = page->mapping->host;
235 struct nfs_server *nfss = NFS_SERVER(inode);
237 end_page_writeback(page);
238 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) {
239 clear_bdi_congested(&nfss->backing_dev_info, WRITE);
240 congestion_end(WRITE);
245 * Find an associated nfs write request, and prepare to flush it out
246 * May return an error if the user signalled nfs_wait_on_request().
248 static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
251 struct inode *inode = page->mapping->host;
252 struct nfs_inode *nfsi = NFS_I(inode);
253 struct nfs_page *req;
256 spin_lock(&inode->i_lock);
258 req = nfs_page_find_request_locked(page);
260 spin_unlock(&inode->i_lock);
263 if (nfs_lock_request_dontget(req))
265 /* Note: If we hold the page lock, as is the case in nfs_writepage,
266 * then the call to nfs_lock_request_dontget() will always
267 * succeed provided that someone hasn't already marked the
268 * request as dirty (in which case we don't care).
270 spin_unlock(&inode->i_lock);
271 ret = nfs_wait_on_request(req);
272 nfs_release_request(req);
275 spin_lock(&inode->i_lock);
277 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
278 /* This request is marked for commit */
279 spin_unlock(&inode->i_lock);
280 nfs_unlock_request(req);
281 nfs_pageio_complete(pgio);
284 if (nfs_set_page_writeback(page) != 0) {
285 spin_unlock(&inode->i_lock);
288 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
289 NFS_PAGE_TAG_LOCKED);
290 spin_unlock(&inode->i_lock);
291 nfs_pageio_add_request(pgio, req);
295 static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
297 struct inode *inode = page->mapping->host;
299 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
300 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
302 nfs_pageio_cond_complete(pgio, page->index);
303 return nfs_page_async_flush(pgio, page);
307 * Write an mmapped page to the server.
309 static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
311 struct nfs_pageio_descriptor pgio;
314 nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
315 err = nfs_do_writepage(page, wbc, &pgio);
316 nfs_pageio_complete(&pgio);
319 if (pgio.pg_error < 0)
320 return pgio.pg_error;
324 int nfs_writepage(struct page *page, struct writeback_control *wbc)
328 ret = nfs_writepage_locked(page, wbc);
333 static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
337 ret = nfs_do_writepage(page, wbc, data);
342 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
344 struct inode *inode = mapping->host;
345 struct nfs_pageio_descriptor pgio;
348 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
350 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
351 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
352 nfs_pageio_complete(&pgio);
355 if (pgio.pg_error < 0)
356 return pgio.pg_error;
361 * Insert a write request into an inode
363 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
365 struct nfs_inode *nfsi = NFS_I(inode);
368 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
369 BUG_ON(error == -EEXIST);
374 nfs_begin_data_update(inode);
375 if (nfs_have_delegation(inode, FMODE_WRITE))
378 SetPagePrivate(req->wb_page);
379 set_page_private(req->wb_page, (unsigned long)req);
381 kref_get(&req->wb_kref);
386 * Remove a write request from an inode
388 static void nfs_inode_remove_request(struct nfs_page *req)
390 struct inode *inode = req->wb_context->path.dentry->d_inode;
391 struct nfs_inode *nfsi = NFS_I(inode);
393 BUG_ON (!NFS_WBACK_BUSY(req));
395 spin_lock(&inode->i_lock);
396 set_page_private(req->wb_page, 0);
397 ClearPagePrivate(req->wb_page);
398 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
401 spin_unlock(&inode->i_lock);
402 nfs_end_data_update(inode);
405 spin_unlock(&inode->i_lock);
406 nfs_clear_request(req);
407 nfs_release_request(req);
411 nfs_redirty_request(struct nfs_page *req)
413 __set_page_dirty_nobuffers(req->wb_page);
417 * Check if a request is dirty
420 nfs_dirty_request(struct nfs_page *req)
422 struct page *page = req->wb_page;
424 if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
426 return !PageWriteback(req->wb_page);
429 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
431 * Add a request to the inode's commit list.
434 nfs_mark_request_commit(struct nfs_page *req)
436 struct inode *inode = req->wb_context->path.dentry->d_inode;
437 struct nfs_inode *nfsi = NFS_I(inode);
439 spin_lock(&inode->i_lock);
441 set_bit(PG_NEED_COMMIT, &(req)->wb_flags);
442 radix_tree_tag_set(&nfsi->nfs_page_tree,
444 NFS_PAGE_TAG_COMMIT);
445 spin_unlock(&inode->i_lock);
446 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
447 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
451 int nfs_write_need_commit(struct nfs_write_data *data)
453 return data->verf.committed != NFS_FILE_SYNC;
457 int nfs_reschedule_unstable_write(struct nfs_page *req)
459 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
460 nfs_mark_request_commit(req);
463 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
464 nfs_redirty_request(req);
471 nfs_mark_request_commit(struct nfs_page *req)
476 int nfs_write_need_commit(struct nfs_write_data *data)
482 int nfs_reschedule_unstable_write(struct nfs_page *req)
489 * Wait for a request to complete.
491 * Interruptible by signals only if mounted with intr flag.
493 static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
495 struct nfs_inode *nfsi = NFS_I(inode);
496 struct nfs_page *req;
497 pgoff_t idx_end, next;
498 unsigned int res = 0;
504 idx_end = idx_start + npages - 1;
507 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) {
508 if (req->wb_index > idx_end)
511 next = req->wb_index + 1;
512 BUG_ON(!NFS_WBACK_BUSY(req));
514 kref_get(&req->wb_kref);
515 spin_unlock(&inode->i_lock);
516 error = nfs_wait_on_request(req);
517 nfs_release_request(req);
518 spin_lock(&inode->i_lock);
526 static void nfs_cancel_commit_list(struct list_head *head)
528 struct nfs_page *req;
530 while(!list_empty(head)) {
531 req = nfs_list_entry(head->next);
532 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
533 nfs_list_remove_request(req);
534 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
535 nfs_inode_remove_request(req);
536 nfs_unlock_request(req);
540 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
542 * nfs_scan_commit - Scan an inode for commit requests
543 * @inode: NFS inode to scan
544 * @dst: destination list
545 * @idx_start: lower bound of page->index to scan.
546 * @npages: idx_start + npages sets the upper bound to scan.
548 * Moves requests from the inode's 'commit' request list.
549 * The requests are *not* checked to ensure that they form a contiguous set.
552 nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
554 struct nfs_inode *nfsi = NFS_I(inode);
557 if (nfsi->ncommit != 0) {
558 res = nfs_scan_list(nfsi, dst, idx_start, npages,
559 NFS_PAGE_TAG_COMMIT);
560 nfsi->ncommit -= res;
565 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
572 * Try to update any existing write request, or create one if there is none.
573 * In order to match, the request's credentials must match those of
574 * the calling process.
576 * Note: Should always be called with the Page Lock held!
578 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
579 struct page *page, unsigned int offset, unsigned int bytes)
581 struct address_space *mapping = page->mapping;
582 struct inode *inode = mapping->host;
583 struct nfs_page *req, *new = NULL;
586 end = offset + bytes;
589 /* Loop over all inode entries and see if we find
590 * A request for the page we wish to update
592 spin_lock(&inode->i_lock);
593 req = nfs_page_find_request_locked(page);
595 if (!nfs_lock_request_dontget(req)) {
598 spin_unlock(&inode->i_lock);
599 error = nfs_wait_on_request(req);
600 nfs_release_request(req);
603 nfs_release_request(new);
604 return ERR_PTR(error);
608 spin_unlock(&inode->i_lock);
610 nfs_release_request(new);
616 nfs_lock_request_dontget(new);
617 error = nfs_inode_add_request(inode, new);
619 spin_unlock(&inode->i_lock);
620 nfs_unlock_request(new);
621 return ERR_PTR(error);
623 spin_unlock(&inode->i_lock);
626 spin_unlock(&inode->i_lock);
628 new = nfs_create_request(ctx, inode, page, offset, bytes);
633 /* We have a request for our page.
634 * If the creds don't match, or the
635 * page addresses don't match,
636 * tell the caller to wait on the conflicting
639 rqend = req->wb_offset + req->wb_bytes;
640 if (req->wb_context != ctx
641 || req->wb_page != page
642 || !nfs_dirty_request(req)
643 || offset > rqend || end < req->wb_offset) {
644 nfs_unlock_request(req);
645 return ERR_PTR(-EBUSY);
648 /* Okay, the request matches. Update the region */
649 if (offset < req->wb_offset) {
650 req->wb_offset = offset;
651 req->wb_pgbase = offset;
652 req->wb_bytes = rqend - req->wb_offset;
656 req->wb_bytes = end - req->wb_offset;
661 int nfs_flush_incompatible(struct file *file, struct page *page)
663 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
664 struct nfs_page *req;
665 int do_flush, status;
667 * Look for a request corresponding to this page. If there
668 * is one, and it belongs to another file, we flush it out
669 * before we try to copy anything into the page. Do this
670 * due to the lack of an ACCESS-type call in NFSv2.
671 * Also do the same if we find a request from an existing
675 req = nfs_page_find_request(page);
678 do_flush = req->wb_page != page || req->wb_context != ctx
679 || !nfs_dirty_request(req);
680 nfs_release_request(req);
683 status = nfs_wb_page(page->mapping->host, page);
684 } while (status == 0);
689 * Update and possibly write a cached page of an NFS file.
691 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
692 * things with a page scheduled for an RPC call (e.g. invalidate it).
694 int nfs_updatepage(struct file *file, struct page *page,
695 unsigned int offset, unsigned int count)
697 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
698 struct inode *inode = page->mapping->host;
701 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
703 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
704 file->f_path.dentry->d_parent->d_name.name,
705 file->f_path.dentry->d_name.name, count,
706 (long long)(page_offset(page) +offset));
708 /* If we're not using byte range locks, and we know the page
709 * is entirely in cache, it may be more efficient to avoid
710 * fragmenting write requests.
712 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
713 count = max(count + offset, nfs_page_length(page));
717 status = nfs_writepage_setup(ctx, page, offset, count);
718 __set_page_dirty_nobuffers(page);
720 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
721 status, (long long)i_size_read(inode));
723 nfs_set_pageerror(page);
727 static void nfs_writepage_release(struct nfs_page *req)
730 if (PageError(req->wb_page)) {
731 nfs_end_page_writeback(req->wb_page);
732 nfs_inode_remove_request(req);
733 } else if (!nfs_reschedule_unstable_write(req)) {
734 /* Set the PG_uptodate flag */
735 nfs_mark_uptodate(req->wb_page, req->wb_pgbase, req->wb_bytes);
736 nfs_end_page_writeback(req->wb_page);
737 nfs_inode_remove_request(req);
739 nfs_end_page_writeback(req->wb_page);
740 nfs_clear_page_tag_locked(req);
743 static inline int flush_task_priority(int how)
745 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
747 return RPC_PRIORITY_HIGH;
749 return RPC_PRIORITY_LOW;
751 return RPC_PRIORITY_NORMAL;
755 * Set up the argument/result storage required for the RPC call.
757 static void nfs_write_rpcsetup(struct nfs_page *req,
758 struct nfs_write_data *data,
759 const struct rpc_call_ops *call_ops,
760 unsigned int count, unsigned int offset,
766 /* Set up the RPC argument and reply structs
767 * NB: take care not to mess about with data->commit et al. */
770 data->inode = inode = req->wb_context->path.dentry->d_inode;
771 data->cred = req->wb_context->cred;
773 data->args.fh = NFS_FH(inode);
774 data->args.offset = req_offset(req) + offset;
775 data->args.pgbase = req->wb_pgbase + offset;
776 data->args.pages = data->pagevec;
777 data->args.count = count;
778 data->args.context = req->wb_context;
780 data->res.fattr = &data->fattr;
781 data->res.count = count;
782 data->res.verf = &data->verf;
783 nfs_fattr_init(&data->fattr);
785 /* Set up the initial task struct. */
786 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
787 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
788 NFS_PROTO(inode)->write_setup(data, how);
790 data->task.tk_priority = flush_task_priority(how);
791 data->task.tk_cookie = (unsigned long)inode;
793 dprintk("NFS: %5u initiated write call "
794 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
797 (long long)NFS_FILEID(inode),
799 (unsigned long long)data->args.offset);
802 static void nfs_execute_write(struct nfs_write_data *data)
804 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
807 rpc_clnt_sigmask(clnt, &oldset);
808 rpc_execute(&data->task);
809 rpc_clnt_sigunmask(clnt, &oldset);
813 * Generate multiple small requests to write out a single
814 * contiguous dirty area on one page.
816 static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
818 struct nfs_page *req = nfs_list_entry(head->next);
819 struct page *page = req->wb_page;
820 struct nfs_write_data *data;
821 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
826 nfs_list_remove_request(req);
830 size_t len = min(nbytes, wsize);
832 data = nfs_writedata_alloc(1);
835 list_add(&data->pages, &list);
838 } while (nbytes != 0);
839 atomic_set(&req->wb_complete, requests);
841 ClearPageError(page);
845 data = list_entry(list.next, struct nfs_write_data, pages);
846 list_del_init(&data->pages);
848 data->pagevec[0] = page;
852 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
856 nfs_execute_write(data);
857 } while (nbytes != 0);
862 while (!list_empty(&list)) {
863 data = list_entry(list.next, struct nfs_write_data, pages);
864 list_del(&data->pages);
865 nfs_writedata_release(data);
867 nfs_redirty_request(req);
868 nfs_end_page_writeback(req->wb_page);
869 nfs_clear_page_tag_locked(req);
874 * Create an RPC task for the given write request and kick it.
875 * The page must have been locked by the caller.
877 * It may happen that the page we're passed is not marked dirty.
878 * This is the case if nfs_updatepage detects a conflicting request
879 * that has been written but not committed.
881 static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
883 struct nfs_page *req;
885 struct nfs_write_data *data;
887 data = nfs_writedata_alloc(npages);
891 pages = data->pagevec;
892 while (!list_empty(head)) {
893 req = nfs_list_entry(head->next);
894 nfs_list_remove_request(req);
895 nfs_list_add_request(req, &data->pages);
896 ClearPageError(req->wb_page);
897 *pages++ = req->wb_page;
899 req = nfs_list_entry(data->pages.next);
901 /* Set up the argument struct */
902 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
904 nfs_execute_write(data);
907 while (!list_empty(head)) {
908 req = nfs_list_entry(head->next);
909 nfs_list_remove_request(req);
910 nfs_redirty_request(req);
911 nfs_end_page_writeback(req->wb_page);
912 nfs_clear_page_tag_locked(req);
917 static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
918 struct inode *inode, int ioflags)
920 int wsize = NFS_SERVER(inode)->wsize;
922 if (wsize < PAGE_CACHE_SIZE)
923 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
925 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
929 * Handle a write reply that flushed part of a page.
931 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
933 struct nfs_write_data *data = calldata;
934 struct nfs_page *req = data->req;
935 struct page *page = req->wb_page;
937 dprintk("NFS: write (%s/%Ld %d@%Ld)",
938 req->wb_context->path.dentry->d_inode->i_sb->s_id,
939 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
941 (long long)req_offset(req));
943 if (nfs_writeback_done(task, data) != 0)
946 if (task->tk_status < 0) {
947 nfs_set_pageerror(page);
948 req->wb_context->error = task->tk_status;
949 dprintk(", error = %d\n", task->tk_status);
953 if (nfs_write_need_commit(data)) {
954 struct inode *inode = page->mapping->host;
956 spin_lock(&inode->i_lock);
957 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
958 /* Do nothing we need to resend the writes */
959 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
960 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
961 dprintk(" defer commit\n");
962 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
963 set_bit(PG_NEED_RESCHED, &req->wb_flags);
964 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
965 dprintk(" server reboot detected\n");
967 spin_unlock(&inode->i_lock);
972 if (atomic_dec_and_test(&req->wb_complete))
973 nfs_writepage_release(req);
976 static const struct rpc_call_ops nfs_write_partial_ops = {
977 .rpc_call_done = nfs_writeback_done_partial,
978 .rpc_release = nfs_writedata_release,
982 * Handle a write reply that flushes a whole page.
984 * FIXME: There is an inherent race with invalidate_inode_pages and
985 * writebacks since the page->count is kept > 1 for as long
986 * as the page has a write request pending.
988 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
990 struct nfs_write_data *data = calldata;
991 struct nfs_page *req;
994 if (nfs_writeback_done(task, data) != 0)
997 /* Update attributes as result of writeback. */
998 while (!list_empty(&data->pages)) {
999 req = nfs_list_entry(data->pages.next);
1000 nfs_list_remove_request(req);
1001 page = req->wb_page;
1003 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1004 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1005 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1007 (long long)req_offset(req));
1009 if (task->tk_status < 0) {
1010 nfs_set_pageerror(page);
1011 req->wb_context->error = task->tk_status;
1012 dprintk(", error = %d\n", task->tk_status);
1013 goto remove_request;
1016 if (nfs_write_need_commit(data)) {
1017 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1018 nfs_mark_request_commit(req);
1019 nfs_end_page_writeback(page);
1020 dprintk(" marked for commit\n");
1023 /* Set the PG_uptodate flag? */
1024 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
1027 nfs_end_page_writeback(page);
1028 nfs_inode_remove_request(req);
1030 nfs_clear_page_tag_locked(req);
1034 static const struct rpc_call_ops nfs_write_full_ops = {
1035 .rpc_call_done = nfs_writeback_done_full,
1036 .rpc_release = nfs_writedata_release,
1041 * This function is called when the WRITE call is complete.
1043 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1045 struct nfs_writeargs *argp = &data->args;
1046 struct nfs_writeres *resp = &data->res;
1049 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1050 task->tk_pid, task->tk_status);
1053 * ->write_done will attempt to use post-op attributes to detect
1054 * conflicting writes by other clients. A strict interpretation
1055 * of close-to-open would allow us to continue caching even if
1056 * another writer had changed the file, but some applications
1057 * depend on tighter cache coherency when writing.
1059 status = NFS_PROTO(data->inode)->write_done(task, data);
1062 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1064 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1065 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1066 /* We tried a write call, but the server did not
1067 * commit data to stable storage even though we
1069 * Note: There is a known bug in Tru64 < 5.0 in which
1070 * the server reports NFS_DATA_SYNC, but performs
1071 * NFS_FILE_SYNC. We therefore implement this checking
1072 * as a dprintk() in order to avoid filling syslog.
1074 static unsigned long complain;
1076 if (time_before(complain, jiffies)) {
1077 dprintk("NFS: faulty NFS server %s:"
1078 " (committed = %d) != (stable = %d)\n",
1079 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1080 resp->verf->committed, argp->stable);
1081 complain = jiffies + 300 * HZ;
1085 /* Is this a short write? */
1086 if (task->tk_status >= 0 && resp->count < argp->count) {
1087 static unsigned long complain;
1089 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1091 /* Has the server at least made some progress? */
1092 if (resp->count != 0) {
1093 /* Was this an NFSv2 write or an NFSv3 stable write? */
1094 if (resp->verf->committed != NFS_UNSTABLE) {
1095 /* Resend from where the server left off */
1096 argp->offset += resp->count;
1097 argp->pgbase += resp->count;
1098 argp->count -= resp->count;
1100 /* Resend as a stable write in order to avoid
1101 * headaches in the case of a server crash.
1103 argp->stable = NFS_FILE_SYNC;
1105 rpc_restart_call(task);
1108 if (time_before(complain, jiffies)) {
1110 "NFS: Server wrote zero bytes, expected %u.\n",
1112 complain = jiffies + 300 * HZ;
1114 /* Can't do anything about it except throw an error. */
1115 task->tk_status = -EIO;
1121 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1122 void nfs_commit_release(void *wdata)
1124 nfs_commit_free(wdata);
1128 * Set up the argument/result storage required for the RPC call.
1130 static void nfs_commit_rpcsetup(struct list_head *head,
1131 struct nfs_write_data *data,
1134 struct nfs_page *first;
1135 struct inode *inode;
1138 /* Set up the RPC argument and reply structs
1139 * NB: take care not to mess about with data->commit et al. */
1141 list_splice_init(head, &data->pages);
1142 first = nfs_list_entry(data->pages.next);
1143 inode = first->wb_context->path.dentry->d_inode;
1145 data->inode = inode;
1146 data->cred = first->wb_context->cred;
1148 data->args.fh = NFS_FH(data->inode);
1149 /* Note: we always request a commit of the entire inode */
1150 data->args.offset = 0;
1151 data->args.count = 0;
1152 data->res.count = 0;
1153 data->res.fattr = &data->fattr;
1154 data->res.verf = &data->verf;
1155 nfs_fattr_init(&data->fattr);
1157 /* Set up the initial task struct. */
1158 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1159 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
1160 NFS_PROTO(inode)->commit_setup(data, how);
1162 data->task.tk_priority = flush_task_priority(how);
1163 data->task.tk_cookie = (unsigned long)inode;
1165 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1169 * Commit dirty pages
1172 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1174 struct nfs_write_data *data;
1175 struct nfs_page *req;
1177 data = nfs_commit_alloc();
1182 /* Set up the argument struct */
1183 nfs_commit_rpcsetup(head, data, how);
1185 nfs_execute_write(data);
1188 while (!list_empty(head)) {
1189 req = nfs_list_entry(head->next);
1190 nfs_list_remove_request(req);
1191 nfs_mark_request_commit(req);
1192 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1193 nfs_clear_page_tag_locked(req);
1199 * COMMIT call returned
1201 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1203 struct nfs_write_data *data = calldata;
1204 struct nfs_page *req;
1206 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1207 task->tk_pid, task->tk_status);
1209 /* Call the NFS version-specific code */
1210 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1213 while (!list_empty(&data->pages)) {
1214 req = nfs_list_entry(data->pages.next);
1215 nfs_list_remove_request(req);
1216 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
1217 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1219 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1220 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1221 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1223 (long long)req_offset(req));
1224 if (task->tk_status < 0) {
1225 req->wb_context->error = task->tk_status;
1226 nfs_inode_remove_request(req);
1227 dprintk(", error = %d\n", task->tk_status);
1231 /* Okay, COMMIT succeeded, apparently. Check the verifier
1232 * returned by the server against all stored verfs. */
1233 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1234 /* We have a match */
1235 /* Set the PG_uptodate flag */
1236 nfs_mark_uptodate(req->wb_page, req->wb_pgbase,
1238 nfs_inode_remove_request(req);
1242 /* We have a mismatch. Write the page again */
1243 dprintk(" mismatch\n");
1244 nfs_redirty_request(req);
1246 nfs_clear_page_tag_locked(req);
1250 static const struct rpc_call_ops nfs_commit_ops = {
1251 .rpc_call_done = nfs_commit_done,
1252 .rpc_release = nfs_commit_release,
1255 int nfs_commit_inode(struct inode *inode, int how)
1260 spin_lock(&inode->i_lock);
1261 res = nfs_scan_commit(inode, &head, 0, 0);
1262 spin_unlock(&inode->i_lock);
1264 int error = nfs_commit_list(inode, &head, how);
1271 static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1277 long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
1279 struct inode *inode = mapping->host;
1280 pgoff_t idx_start, idx_end;
1281 unsigned int npages = 0;
1283 int nocommit = how & FLUSH_NOCOMMIT;
1287 if (wbc->range_cyclic)
1290 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1291 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1292 if (idx_end > idx_start) {
1293 pgoff_t l_npages = 1 + idx_end - idx_start;
1295 if (sizeof(npages) != sizeof(l_npages) &&
1296 (pgoff_t)npages != l_npages)
1300 how &= ~FLUSH_NOCOMMIT;
1301 spin_lock(&inode->i_lock);
1303 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1308 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1311 if (how & FLUSH_INVALIDATE) {
1312 spin_unlock(&inode->i_lock);
1313 nfs_cancel_commit_list(&head);
1315 spin_lock(&inode->i_lock);
1318 pages += nfs_scan_commit(inode, &head, 0, 0);
1319 spin_unlock(&inode->i_lock);
1320 ret = nfs_commit_list(inode, &head, how);
1321 spin_lock(&inode->i_lock);
1324 spin_unlock(&inode->i_lock);
1329 * flush the inode to disk.
1331 int nfs_wb_all(struct inode *inode)
1333 struct address_space *mapping = inode->i_mapping;
1334 struct writeback_control wbc = {
1335 .bdi = mapping->backing_dev_info,
1336 .sync_mode = WB_SYNC_ALL,
1337 .nr_to_write = LONG_MAX,
1338 .for_writepages = 1,
1343 ret = nfs_writepages(mapping, &wbc);
1346 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1350 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1354 int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1356 struct writeback_control wbc = {
1357 .bdi = mapping->backing_dev_info,
1358 .sync_mode = WB_SYNC_ALL,
1359 .nr_to_write = LONG_MAX,
1360 .range_start = range_start,
1361 .range_end = range_end,
1362 .for_writepages = 1,
1366 ret = nfs_writepages(mapping, &wbc);
1369 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1373 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1377 int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1379 struct nfs_page *req;
1380 loff_t range_start = page_offset(page);
1381 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1382 struct writeback_control wbc = {
1383 .bdi = page->mapping->backing_dev_info,
1384 .sync_mode = WB_SYNC_ALL,
1385 .nr_to_write = LONG_MAX,
1386 .range_start = range_start,
1387 .range_end = range_end,
1391 BUG_ON(!PageLocked(page));
1393 req = nfs_page_find_request(page);
1396 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1397 nfs_release_request(req);
1400 if (nfs_lock_request_dontget(req)) {
1401 nfs_inode_remove_request(req);
1403 * In case nfs_inode_remove_request has marked the
1404 * page as being dirty
1406 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1407 nfs_unlock_request(req);
1410 ret = nfs_wait_on_request(req);
1414 if (!PagePrivate(page))
1416 ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
1421 int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
1423 loff_t range_start = page_offset(page);
1424 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1425 struct writeback_control wbc = {
1426 .bdi = page->mapping->backing_dev_info,
1427 .sync_mode = WB_SYNC_ALL,
1428 .nr_to_write = LONG_MAX,
1429 .range_start = range_start,
1430 .range_end = range_end,
1434 BUG_ON(!PageLocked(page));
1435 if (clear_page_dirty_for_io(page)) {
1436 ret = nfs_writepage_locked(page, &wbc);
1440 if (!PagePrivate(page))
1442 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1446 __mark_inode_dirty(inode, I_DIRTY_PAGES);
1451 * Write back all requests on one page - we do this before reading it.
1453 int nfs_wb_page(struct inode *inode, struct page* page)
1455 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
1458 int __init nfs_init_writepagecache(void)
1460 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1461 sizeof(struct nfs_write_data),
1462 0, SLAB_HWCACHE_ALIGN,
1464 if (nfs_wdata_cachep == NULL)
1467 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1469 if (nfs_wdata_mempool == NULL)
1472 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1474 if (nfs_commit_mempool == NULL)
1478 * NFS congestion size, scale with available memory.
1490 * This allows larger machines to have larger/more transfers.
1491 * Limit the default to 256M
1493 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1494 if (nfs_congestion_kb > 256*1024)
1495 nfs_congestion_kb = 256*1024;
1500 void nfs_destroy_writepagecache(void)
1502 mempool_destroy(nfs_commit_mempool);
1503 mempool_destroy(nfs_wdata_mempool);
1504 kmem_cache_destroy(nfs_wdata_cachep);