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 void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
117 set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
120 static struct nfs_page *nfs_page_find_request_locked(struct page *page)
122 struct nfs_page *req = NULL;
124 if (PagePrivate(page)) {
125 req = (struct nfs_page *)page_private(page);
127 kref_get(&req->wb_kref);
132 static struct nfs_page *nfs_page_find_request(struct page *page)
134 struct inode *inode = page->mapping->host;
135 struct nfs_page *req = NULL;
137 spin_lock(&inode->i_lock);
138 req = nfs_page_find_request_locked(page);
139 spin_unlock(&inode->i_lock);
143 /* Adjust the file length if we're writing beyond the end */
144 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
146 struct inode *inode = page->mapping->host;
147 loff_t end, i_size = i_size_read(inode);
148 pgoff_t end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
150 if (i_size > 0 && page->index < end_index)
152 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
155 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
156 i_size_write(inode, end);
159 /* A writeback failed: mark the page as bad, and invalidate the page cache */
160 static void nfs_set_pageerror(struct page *page)
163 nfs_zap_mapping(page->mapping->host, page->mapping);
166 /* We can set the PG_uptodate flag if we see that a write request
167 * covers the full page.
169 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
171 if (PageUptodate(page))
175 if (count != nfs_page_length(page))
177 if (count != PAGE_CACHE_SIZE)
178 zero_user_page(page, count, PAGE_CACHE_SIZE - count, KM_USER0);
179 SetPageUptodate(page);
182 static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
183 unsigned int offset, unsigned int count)
185 struct nfs_page *req;
189 req = nfs_update_request(ctx, page, offset, count);
195 ret = nfs_wb_page(page->mapping->host, page);
199 /* Update file length */
200 nfs_grow_file(page, offset, count);
201 nfs_unlock_request(req);
205 static int wb_priority(struct writeback_control *wbc)
207 if (wbc->for_reclaim)
208 return FLUSH_HIGHPRI | FLUSH_STABLE;
209 if (wbc->for_kupdate)
215 * NFS congestion control
218 int nfs_congestion_kb;
220 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
221 #define NFS_CONGESTION_OFF_THRESH \
222 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
224 static int nfs_set_page_writeback(struct page *page)
226 int ret = test_set_page_writeback(page);
229 struct inode *inode = page->mapping->host;
230 struct nfs_server *nfss = NFS_SERVER(inode);
232 if (atomic_long_inc_return(&nfss->writeback) >
233 NFS_CONGESTION_ON_THRESH)
234 set_bdi_congested(&nfss->backing_dev_info, WRITE);
239 static void nfs_end_page_writeback(struct page *page)
241 struct inode *inode = page->mapping->host;
242 struct nfs_server *nfss = NFS_SERVER(inode);
244 end_page_writeback(page);
245 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) {
246 clear_bdi_congested(&nfss->backing_dev_info, WRITE);
247 congestion_end(WRITE);
252 * Find an associated nfs write request, and prepare to flush it out
253 * May return an error if the user signalled nfs_wait_on_request().
255 static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
258 struct inode *inode = page->mapping->host;
259 struct nfs_inode *nfsi = NFS_I(inode);
260 struct nfs_page *req;
263 spin_lock(&inode->i_lock);
265 req = nfs_page_find_request_locked(page);
267 spin_unlock(&inode->i_lock);
270 if (nfs_lock_request_dontget(req))
272 /* Note: If we hold the page lock, as is the case in nfs_writepage,
273 * then the call to nfs_lock_request_dontget() will always
274 * succeed provided that someone hasn't already marked the
275 * request as dirty (in which case we don't care).
277 spin_unlock(&inode->i_lock);
278 ret = nfs_wait_on_request(req);
279 nfs_release_request(req);
282 spin_lock(&inode->i_lock);
284 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
285 /* This request is marked for commit */
286 spin_unlock(&inode->i_lock);
287 nfs_unlock_request(req);
288 nfs_pageio_complete(pgio);
291 if (nfs_set_page_writeback(page) != 0) {
292 spin_unlock(&inode->i_lock);
295 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
296 NFS_PAGE_TAG_LOCKED);
297 spin_unlock(&inode->i_lock);
298 nfs_pageio_add_request(pgio, req);
302 static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
304 struct inode *inode = page->mapping->host;
306 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
307 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
309 nfs_pageio_cond_complete(pgio, page->index);
310 return nfs_page_async_flush(pgio, page);
314 * Write an mmapped page to the server.
316 static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
318 struct nfs_pageio_descriptor pgio;
321 nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
322 err = nfs_do_writepage(page, wbc, &pgio);
323 nfs_pageio_complete(&pgio);
326 if (pgio.pg_error < 0)
327 return pgio.pg_error;
331 int nfs_writepage(struct page *page, struct writeback_control *wbc)
335 ret = nfs_writepage_locked(page, wbc);
340 static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
344 ret = nfs_do_writepage(page, wbc, data);
349 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
351 struct inode *inode = mapping->host;
352 struct nfs_pageio_descriptor pgio;
355 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
357 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
358 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
359 nfs_pageio_complete(&pgio);
362 if (pgio.pg_error < 0)
363 return pgio.pg_error;
368 * Insert a write request into an inode
370 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
372 struct nfs_inode *nfsi = NFS_I(inode);
375 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
376 BUG_ON(error == -EEXIST);
381 if (nfs_have_delegation(inode, FMODE_WRITE))
384 SetPagePrivate(req->wb_page);
385 set_page_private(req->wb_page, (unsigned long)req);
387 kref_get(&req->wb_kref);
392 * Remove a write request from an inode
394 static void nfs_inode_remove_request(struct nfs_page *req)
396 struct inode *inode = req->wb_context->path.dentry->d_inode;
397 struct nfs_inode *nfsi = NFS_I(inode);
399 BUG_ON (!NFS_WBACK_BUSY(req));
401 spin_lock(&inode->i_lock);
402 set_page_private(req->wb_page, 0);
403 ClearPagePrivate(req->wb_page);
404 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
407 spin_unlock(&inode->i_lock);
410 spin_unlock(&inode->i_lock);
411 nfs_clear_request(req);
412 nfs_release_request(req);
416 nfs_redirty_request(struct nfs_page *req)
418 __set_page_dirty_nobuffers(req->wb_page);
422 * Check if a request is dirty
425 nfs_dirty_request(struct nfs_page *req)
427 struct page *page = req->wb_page;
429 if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
431 return !PageWriteback(req->wb_page);
434 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
436 * Add a request to the inode's commit list.
439 nfs_mark_request_commit(struct nfs_page *req)
441 struct inode *inode = req->wb_context->path.dentry->d_inode;
442 struct nfs_inode *nfsi = NFS_I(inode);
444 spin_lock(&inode->i_lock);
446 set_bit(PG_NEED_COMMIT, &(req)->wb_flags);
447 radix_tree_tag_set(&nfsi->nfs_page_tree,
449 NFS_PAGE_TAG_COMMIT);
450 spin_unlock(&inode->i_lock);
451 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
452 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
456 int nfs_write_need_commit(struct nfs_write_data *data)
458 return data->verf.committed != NFS_FILE_SYNC;
462 int nfs_reschedule_unstable_write(struct nfs_page *req)
464 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
465 nfs_mark_request_commit(req);
468 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
469 nfs_redirty_request(req);
476 nfs_mark_request_commit(struct nfs_page *req)
481 int nfs_write_need_commit(struct nfs_write_data *data)
487 int nfs_reschedule_unstable_write(struct nfs_page *req)
494 * Wait for a request to complete.
496 * Interruptible by signals only if mounted with intr flag.
498 static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
500 struct nfs_inode *nfsi = NFS_I(inode);
501 struct nfs_page *req;
502 pgoff_t idx_end, next;
503 unsigned int res = 0;
509 idx_end = idx_start + npages - 1;
512 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) {
513 if (req->wb_index > idx_end)
516 next = req->wb_index + 1;
517 BUG_ON(!NFS_WBACK_BUSY(req));
519 kref_get(&req->wb_kref);
520 spin_unlock(&inode->i_lock);
521 error = nfs_wait_on_request(req);
522 nfs_release_request(req);
523 spin_lock(&inode->i_lock);
531 static void nfs_cancel_commit_list(struct list_head *head)
533 struct nfs_page *req;
535 while(!list_empty(head)) {
536 req = nfs_list_entry(head->next);
537 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
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_lock_request_dontget(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);
631 spin_unlock(&inode->i_lock);
633 new = nfs_create_request(ctx, inode, page, offset, bytes);
638 /* We have a request for our page.
639 * If the creds don't match, or the
640 * page addresses don't match,
641 * tell the caller to wait on the conflicting
644 rqend = req->wb_offset + req->wb_bytes;
645 if (req->wb_context != ctx
646 || req->wb_page != page
647 || !nfs_dirty_request(req)
648 || offset > rqend || end < req->wb_offset) {
649 nfs_unlock_request(req);
650 return ERR_PTR(-EBUSY);
653 /* Okay, the request matches. Update the region */
654 if (offset < req->wb_offset) {
655 req->wb_offset = offset;
656 req->wb_pgbase = offset;
657 req->wb_bytes = rqend - req->wb_offset;
661 req->wb_bytes = end - req->wb_offset;
666 int nfs_flush_incompatible(struct file *file, struct page *page)
668 struct nfs_open_context *ctx = nfs_file_open_context(file);
669 struct nfs_page *req;
670 int do_flush, status;
672 * Look for a request corresponding to this page. If there
673 * is one, and it belongs to another file, we flush it out
674 * before we try to copy anything into the page. Do this
675 * due to the lack of an ACCESS-type call in NFSv2.
676 * Also do the same if we find a request from an existing
680 req = nfs_page_find_request(page);
683 do_flush = req->wb_page != page || req->wb_context != ctx
684 || !nfs_dirty_request(req);
685 nfs_release_request(req);
688 status = nfs_wb_page(page->mapping->host, page);
689 } while (status == 0);
694 * Update and possibly write a cached page of an NFS file.
696 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
697 * things with a page scheduled for an RPC call (e.g. invalidate it).
699 int nfs_updatepage(struct file *file, struct page *page,
700 unsigned int offset, unsigned int count)
702 struct nfs_open_context *ctx = nfs_file_open_context(file);
703 struct inode *inode = page->mapping->host;
706 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
708 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
709 file->f_path.dentry->d_parent->d_name.name,
710 file->f_path.dentry->d_name.name, count,
711 (long long)(page_offset(page) +offset));
713 /* If we're not using byte range locks, and we know the page
714 * is entirely in cache, it may be more efficient to avoid
715 * fragmenting write requests.
717 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
718 count = max(count + offset, nfs_page_length(page));
722 status = nfs_writepage_setup(ctx, page, offset, count);
723 __set_page_dirty_nobuffers(page);
725 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
726 status, (long long)i_size_read(inode));
728 nfs_set_pageerror(page);
732 static void nfs_writepage_release(struct nfs_page *req)
735 if (PageError(req->wb_page)) {
736 nfs_end_page_writeback(req->wb_page);
737 nfs_inode_remove_request(req);
738 } else if (!nfs_reschedule_unstable_write(req)) {
739 /* Set the PG_uptodate flag */
740 nfs_mark_uptodate(req->wb_page, req->wb_pgbase, req->wb_bytes);
741 nfs_end_page_writeback(req->wb_page);
742 nfs_inode_remove_request(req);
744 nfs_end_page_writeback(req->wb_page);
745 nfs_clear_page_tag_locked(req);
748 static inline int flush_task_priority(int how)
750 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
752 return RPC_PRIORITY_HIGH;
754 return RPC_PRIORITY_LOW;
756 return RPC_PRIORITY_NORMAL;
760 * Set up the argument/result storage required for the RPC call.
762 static void nfs_write_rpcsetup(struct nfs_page *req,
763 struct nfs_write_data *data,
764 const struct rpc_call_ops *call_ops,
765 unsigned int count, unsigned int offset,
771 /* Set up the RPC argument and reply structs
772 * NB: take care not to mess about with data->commit et al. */
775 data->inode = inode = req->wb_context->path.dentry->d_inode;
776 data->cred = req->wb_context->cred;
778 data->args.fh = NFS_FH(inode);
779 data->args.offset = req_offset(req) + offset;
780 data->args.pgbase = req->wb_pgbase + offset;
781 data->args.pages = data->pagevec;
782 data->args.count = count;
783 data->args.context = req->wb_context;
785 data->res.fattr = &data->fattr;
786 data->res.count = count;
787 data->res.verf = &data->verf;
788 nfs_fattr_init(&data->fattr);
790 /* Set up the initial task struct. */
791 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
792 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
793 NFS_PROTO(inode)->write_setup(data, how);
795 data->task.tk_priority = flush_task_priority(how);
796 data->task.tk_cookie = (unsigned long)inode;
798 dprintk("NFS: %5u initiated write call "
799 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
802 (long long)NFS_FILEID(inode),
804 (unsigned long long)data->args.offset);
807 static void nfs_execute_write(struct nfs_write_data *data)
809 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
812 rpc_clnt_sigmask(clnt, &oldset);
813 rpc_execute(&data->task);
814 rpc_clnt_sigunmask(clnt, &oldset);
818 * Generate multiple small requests to write out a single
819 * contiguous dirty area on one page.
821 static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
823 struct nfs_page *req = nfs_list_entry(head->next);
824 struct page *page = req->wb_page;
825 struct nfs_write_data *data;
826 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
831 nfs_list_remove_request(req);
835 size_t len = min(nbytes, wsize);
837 data = nfs_writedata_alloc(1);
840 list_add(&data->pages, &list);
843 } while (nbytes != 0);
844 atomic_set(&req->wb_complete, requests);
846 ClearPageError(page);
850 data = list_entry(list.next, struct nfs_write_data, pages);
851 list_del_init(&data->pages);
853 data->pagevec[0] = page;
857 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
861 nfs_execute_write(data);
862 } while (nbytes != 0);
867 while (!list_empty(&list)) {
868 data = list_entry(list.next, struct nfs_write_data, pages);
869 list_del(&data->pages);
870 nfs_writedata_release(data);
872 nfs_redirty_request(req);
873 nfs_end_page_writeback(req->wb_page);
874 nfs_clear_page_tag_locked(req);
879 * Create an RPC task for the given write request and kick it.
880 * The page must have been locked by the caller.
882 * It may happen that the page we're passed is not marked dirty.
883 * This is the case if nfs_updatepage detects a conflicting request
884 * that has been written but not committed.
886 static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
888 struct nfs_page *req;
890 struct nfs_write_data *data;
892 data = nfs_writedata_alloc(npages);
896 pages = data->pagevec;
897 while (!list_empty(head)) {
898 req = nfs_list_entry(head->next);
899 nfs_list_remove_request(req);
900 nfs_list_add_request(req, &data->pages);
901 ClearPageError(req->wb_page);
902 *pages++ = req->wb_page;
904 req = nfs_list_entry(data->pages.next);
906 /* Set up the argument struct */
907 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
909 nfs_execute_write(data);
912 while (!list_empty(head)) {
913 req = nfs_list_entry(head->next);
914 nfs_list_remove_request(req);
915 nfs_redirty_request(req);
916 nfs_end_page_writeback(req->wb_page);
917 nfs_clear_page_tag_locked(req);
922 static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
923 struct inode *inode, int ioflags)
925 int wsize = NFS_SERVER(inode)->wsize;
927 if (wsize < PAGE_CACHE_SIZE)
928 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
930 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
934 * Handle a write reply that flushed part of a page.
936 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
938 struct nfs_write_data *data = calldata;
939 struct nfs_page *req = data->req;
940 struct page *page = req->wb_page;
942 dprintk("NFS: write (%s/%Ld %d@%Ld)",
943 req->wb_context->path.dentry->d_inode->i_sb->s_id,
944 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
946 (long long)req_offset(req));
948 if (nfs_writeback_done(task, data) != 0)
951 if (task->tk_status < 0) {
952 nfs_set_pageerror(page);
953 nfs_context_set_write_error(req->wb_context, task->tk_status);
954 dprintk(", error = %d\n", task->tk_status);
958 if (nfs_write_need_commit(data)) {
959 struct inode *inode = page->mapping->host;
961 spin_lock(&inode->i_lock);
962 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
963 /* Do nothing we need to resend the writes */
964 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
965 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
966 dprintk(" defer commit\n");
967 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
968 set_bit(PG_NEED_RESCHED, &req->wb_flags);
969 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
970 dprintk(" server reboot detected\n");
972 spin_unlock(&inode->i_lock);
977 if (atomic_dec_and_test(&req->wb_complete))
978 nfs_writepage_release(req);
981 static const struct rpc_call_ops nfs_write_partial_ops = {
982 .rpc_call_done = nfs_writeback_done_partial,
983 .rpc_release = nfs_writedata_release,
987 * Handle a write reply that flushes a whole page.
989 * FIXME: There is an inherent race with invalidate_inode_pages and
990 * writebacks since the page->count is kept > 1 for as long
991 * as the page has a write request pending.
993 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
995 struct nfs_write_data *data = calldata;
996 struct nfs_page *req;
999 if (nfs_writeback_done(task, data) != 0)
1002 /* Update attributes as result of writeback. */
1003 while (!list_empty(&data->pages)) {
1004 req = nfs_list_entry(data->pages.next);
1005 nfs_list_remove_request(req);
1006 page = req->wb_page;
1008 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1009 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1010 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1012 (long long)req_offset(req));
1014 if (task->tk_status < 0) {
1015 nfs_set_pageerror(page);
1016 nfs_context_set_write_error(req->wb_context, task->tk_status);
1017 dprintk(", error = %d\n", task->tk_status);
1018 goto remove_request;
1021 if (nfs_write_need_commit(data)) {
1022 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1023 nfs_mark_request_commit(req);
1024 nfs_end_page_writeback(page);
1025 dprintk(" marked for commit\n");
1028 /* Set the PG_uptodate flag? */
1029 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
1032 nfs_end_page_writeback(page);
1033 nfs_inode_remove_request(req);
1035 nfs_clear_page_tag_locked(req);
1039 static const struct rpc_call_ops nfs_write_full_ops = {
1040 .rpc_call_done = nfs_writeback_done_full,
1041 .rpc_release = nfs_writedata_release,
1046 * This function is called when the WRITE call is complete.
1048 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1050 struct nfs_writeargs *argp = &data->args;
1051 struct nfs_writeres *resp = &data->res;
1054 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1055 task->tk_pid, task->tk_status);
1058 * ->write_done will attempt to use post-op attributes to detect
1059 * conflicting writes by other clients. A strict interpretation
1060 * of close-to-open would allow us to continue caching even if
1061 * another writer had changed the file, but some applications
1062 * depend on tighter cache coherency when writing.
1064 status = NFS_PROTO(data->inode)->write_done(task, data);
1067 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1069 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1070 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1071 /* We tried a write call, but the server did not
1072 * commit data to stable storage even though we
1074 * Note: There is a known bug in Tru64 < 5.0 in which
1075 * the server reports NFS_DATA_SYNC, but performs
1076 * NFS_FILE_SYNC. We therefore implement this checking
1077 * as a dprintk() in order to avoid filling syslog.
1079 static unsigned long complain;
1081 if (time_before(complain, jiffies)) {
1082 dprintk("NFS: faulty NFS server %s:"
1083 " (committed = %d) != (stable = %d)\n",
1084 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1085 resp->verf->committed, argp->stable);
1086 complain = jiffies + 300 * HZ;
1090 /* Is this a short write? */
1091 if (task->tk_status >= 0 && resp->count < argp->count) {
1092 static unsigned long complain;
1094 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1096 /* Has the server at least made some progress? */
1097 if (resp->count != 0) {
1098 /* Was this an NFSv2 write or an NFSv3 stable write? */
1099 if (resp->verf->committed != NFS_UNSTABLE) {
1100 /* Resend from where the server left off */
1101 argp->offset += resp->count;
1102 argp->pgbase += resp->count;
1103 argp->count -= resp->count;
1105 /* Resend as a stable write in order to avoid
1106 * headaches in the case of a server crash.
1108 argp->stable = NFS_FILE_SYNC;
1110 rpc_restart_call(task);
1113 if (time_before(complain, jiffies)) {
1115 "NFS: Server wrote zero bytes, expected %u.\n",
1117 complain = jiffies + 300 * HZ;
1119 /* Can't do anything about it except throw an error. */
1120 task->tk_status = -EIO;
1126 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1127 void nfs_commit_release(void *wdata)
1129 nfs_commit_free(wdata);
1133 * Set up the argument/result storage required for the RPC call.
1135 static void nfs_commit_rpcsetup(struct list_head *head,
1136 struct nfs_write_data *data,
1139 struct nfs_page *first;
1140 struct inode *inode;
1143 /* Set up the RPC argument and reply structs
1144 * NB: take care not to mess about with data->commit et al. */
1146 list_splice_init(head, &data->pages);
1147 first = nfs_list_entry(data->pages.next);
1148 inode = first->wb_context->path.dentry->d_inode;
1150 data->inode = inode;
1151 data->cred = first->wb_context->cred;
1153 data->args.fh = NFS_FH(data->inode);
1154 /* Note: we always request a commit of the entire inode */
1155 data->args.offset = 0;
1156 data->args.count = 0;
1157 data->res.count = 0;
1158 data->res.fattr = &data->fattr;
1159 data->res.verf = &data->verf;
1160 nfs_fattr_init(&data->fattr);
1162 /* Set up the initial task struct. */
1163 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1164 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
1165 NFS_PROTO(inode)->commit_setup(data, how);
1167 data->task.tk_priority = flush_task_priority(how);
1168 data->task.tk_cookie = (unsigned long)inode;
1170 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1174 * Commit dirty pages
1177 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1179 struct nfs_write_data *data;
1180 struct nfs_page *req;
1182 data = nfs_commit_alloc();
1187 /* Set up the argument struct */
1188 nfs_commit_rpcsetup(head, data, how);
1190 nfs_execute_write(data);
1193 while (!list_empty(head)) {
1194 req = nfs_list_entry(head->next);
1195 nfs_list_remove_request(req);
1196 nfs_mark_request_commit(req);
1197 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1198 nfs_clear_page_tag_locked(req);
1204 * COMMIT call returned
1206 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1208 struct nfs_write_data *data = calldata;
1209 struct nfs_page *req;
1211 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1212 task->tk_pid, task->tk_status);
1214 /* Call the NFS version-specific code */
1215 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1218 while (!list_empty(&data->pages)) {
1219 req = nfs_list_entry(data->pages.next);
1220 nfs_list_remove_request(req);
1221 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
1222 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1224 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1225 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1226 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1228 (long long)req_offset(req));
1229 if (task->tk_status < 0) {
1230 nfs_context_set_write_error(req->wb_context, task->tk_status);
1231 nfs_inode_remove_request(req);
1232 dprintk(", error = %d\n", task->tk_status);
1236 /* Okay, COMMIT succeeded, apparently. Check the verifier
1237 * returned by the server against all stored verfs. */
1238 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1239 /* We have a match */
1240 /* Set the PG_uptodate flag */
1241 nfs_mark_uptodate(req->wb_page, req->wb_pgbase,
1243 nfs_inode_remove_request(req);
1247 /* We have a mismatch. Write the page again */
1248 dprintk(" mismatch\n");
1249 nfs_redirty_request(req);
1251 nfs_clear_page_tag_locked(req);
1255 static const struct rpc_call_ops nfs_commit_ops = {
1256 .rpc_call_done = nfs_commit_done,
1257 .rpc_release = nfs_commit_release,
1260 int nfs_commit_inode(struct inode *inode, int how)
1265 spin_lock(&inode->i_lock);
1266 res = nfs_scan_commit(inode, &head, 0, 0);
1267 spin_unlock(&inode->i_lock);
1269 int error = nfs_commit_list(inode, &head, how);
1276 static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1282 long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
1284 struct inode *inode = mapping->host;
1285 pgoff_t idx_start, idx_end;
1286 unsigned int npages = 0;
1288 int nocommit = how & FLUSH_NOCOMMIT;
1292 if (wbc->range_cyclic)
1295 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1296 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1297 if (idx_end > idx_start) {
1298 pgoff_t l_npages = 1 + idx_end - idx_start;
1300 if (sizeof(npages) != sizeof(l_npages) &&
1301 (pgoff_t)npages != l_npages)
1305 how &= ~FLUSH_NOCOMMIT;
1306 spin_lock(&inode->i_lock);
1308 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1313 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1316 if (how & FLUSH_INVALIDATE) {
1317 spin_unlock(&inode->i_lock);
1318 nfs_cancel_commit_list(&head);
1320 spin_lock(&inode->i_lock);
1323 pages += nfs_scan_commit(inode, &head, 0, 0);
1324 spin_unlock(&inode->i_lock);
1325 ret = nfs_commit_list(inode, &head, how);
1326 spin_lock(&inode->i_lock);
1329 spin_unlock(&inode->i_lock);
1333 static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
1337 ret = nfs_writepages(mapping, wbc);
1340 ret = nfs_sync_mapping_wait(mapping, wbc, how);
1345 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1349 /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
1350 static int nfs_write_mapping(struct address_space *mapping, int how)
1352 struct writeback_control wbc = {
1353 .bdi = mapping->backing_dev_info,
1354 .sync_mode = WB_SYNC_NONE,
1355 .nr_to_write = LONG_MAX,
1356 .for_writepages = 1,
1361 ret = __nfs_write_mapping(mapping, &wbc, how);
1364 wbc.sync_mode = WB_SYNC_ALL;
1365 return __nfs_write_mapping(mapping, &wbc, how);
1369 * flush the inode to disk.
1371 int nfs_wb_all(struct inode *inode)
1373 return nfs_write_mapping(inode->i_mapping, 0);
1376 int nfs_wb_nocommit(struct inode *inode)
1378 return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
1381 int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1383 struct nfs_page *req;
1384 loff_t range_start = page_offset(page);
1385 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1386 struct writeback_control wbc = {
1387 .bdi = page->mapping->backing_dev_info,
1388 .sync_mode = WB_SYNC_ALL,
1389 .nr_to_write = LONG_MAX,
1390 .range_start = range_start,
1391 .range_end = range_end,
1395 BUG_ON(!PageLocked(page));
1397 req = nfs_page_find_request(page);
1400 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1401 nfs_release_request(req);
1404 if (nfs_lock_request_dontget(req)) {
1405 nfs_inode_remove_request(req);
1407 * In case nfs_inode_remove_request has marked the
1408 * page as being dirty
1410 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1411 nfs_unlock_request(req);
1414 ret = nfs_wait_on_request(req);
1418 if (!PagePrivate(page))
1420 ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
1425 int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
1427 loff_t range_start = page_offset(page);
1428 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1429 struct writeback_control wbc = {
1430 .bdi = page->mapping->backing_dev_info,
1431 .sync_mode = WB_SYNC_ALL,
1432 .nr_to_write = LONG_MAX,
1433 .range_start = range_start,
1434 .range_end = range_end,
1438 BUG_ON(!PageLocked(page));
1439 if (clear_page_dirty_for_io(page)) {
1440 ret = nfs_writepage_locked(page, &wbc);
1444 if (!PagePrivate(page))
1446 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1450 __mark_inode_dirty(inode, I_DIRTY_PAGES);
1455 * Write back all requests on one page - we do this before reading it.
1457 int nfs_wb_page(struct inode *inode, struct page* page)
1459 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
1462 int __init nfs_init_writepagecache(void)
1464 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1465 sizeof(struct nfs_write_data),
1466 0, SLAB_HWCACHE_ALIGN,
1468 if (nfs_wdata_cachep == NULL)
1471 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1473 if (nfs_wdata_mempool == NULL)
1476 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1478 if (nfs_commit_mempool == NULL)
1482 * NFS congestion size, scale with available memory.
1494 * This allows larger machines to have larger/more transfers.
1495 * Limit the default to 256M
1497 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1498 if (nfs_congestion_kb > 256*1024)
1499 nfs_congestion_kb = 256*1024;
1504 void nfs_destroy_writepagecache(void)
1506 mempool_destroy(nfs_commit_mempool);
1507 mempool_destroy(nfs_wdata_mempool);
1508 kmem_cache_destroy(nfs_wdata_cachep);