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);
250 * Find an associated nfs write request, and prepare to flush it out
251 * May return an error if the user signalled nfs_wait_on_request().
253 static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
256 struct inode *inode = page->mapping->host;
257 struct nfs_inode *nfsi = NFS_I(inode);
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_lock_request_dontget(req))
270 /* Note: If we hold the page lock, as is the case in nfs_writepage,
271 * then the call to nfs_lock_request_dontget() 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_unlock_request(req);
286 nfs_pageio_complete(pgio);
289 if (nfs_set_page_writeback(page) != 0) {
290 spin_unlock(&inode->i_lock);
293 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
294 NFS_PAGE_TAG_LOCKED);
295 spin_unlock(&inode->i_lock);
296 nfs_pageio_add_request(pgio, req);
300 static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
302 struct inode *inode = page->mapping->host;
304 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
305 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
307 nfs_pageio_cond_complete(pgio, page->index);
308 return nfs_page_async_flush(pgio, page);
312 * Write an mmapped page to the server.
314 static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
316 struct nfs_pageio_descriptor pgio;
319 nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
320 err = nfs_do_writepage(page, wbc, &pgio);
321 nfs_pageio_complete(&pgio);
324 if (pgio.pg_error < 0)
325 return pgio.pg_error;
329 int nfs_writepage(struct page *page, struct writeback_control *wbc)
333 ret = nfs_writepage_locked(page, wbc);
338 static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
342 ret = nfs_do_writepage(page, wbc, data);
347 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
349 struct inode *inode = mapping->host;
350 struct nfs_pageio_descriptor pgio;
353 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
355 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
356 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
357 nfs_pageio_complete(&pgio);
360 if (pgio.pg_error < 0)
361 return pgio.pg_error;
366 * Insert a write request into an inode
368 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
370 struct nfs_inode *nfsi = NFS_I(inode);
373 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
374 BUG_ON(error == -EEXIST);
379 if (nfs_have_delegation(inode, FMODE_WRITE))
382 SetPagePrivate(req->wb_page);
383 set_page_private(req->wb_page, (unsigned long)req);
385 kref_get(&req->wb_kref);
390 * Remove a write request from an inode
392 static void nfs_inode_remove_request(struct nfs_page *req)
394 struct inode *inode = req->wb_context->path.dentry->d_inode;
395 struct nfs_inode *nfsi = NFS_I(inode);
397 BUG_ON (!NFS_WBACK_BUSY(req));
399 spin_lock(&inode->i_lock);
400 set_page_private(req->wb_page, 0);
401 ClearPagePrivate(req->wb_page);
402 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
405 spin_unlock(&inode->i_lock);
408 spin_unlock(&inode->i_lock);
409 nfs_clear_request(req);
410 nfs_release_request(req);
414 nfs_redirty_request(struct nfs_page *req)
416 __set_page_dirty_nobuffers(req->wb_page);
420 * Check if a request is dirty
423 nfs_dirty_request(struct nfs_page *req)
425 struct page *page = req->wb_page;
427 if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
429 return !PageWriteback(req->wb_page);
432 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
434 * Add a request to the inode's commit list.
437 nfs_mark_request_commit(struct nfs_page *req)
439 struct inode *inode = req->wb_context->path.dentry->d_inode;
440 struct nfs_inode *nfsi = NFS_I(inode);
442 spin_lock(&inode->i_lock);
444 set_bit(PG_NEED_COMMIT, &(req)->wb_flags);
445 radix_tree_tag_set(&nfsi->nfs_page_tree,
447 NFS_PAGE_TAG_COMMIT);
448 spin_unlock(&inode->i_lock);
449 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
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 signals only if mounted with intr flag.
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 nfs_list_remove_request(req);
537 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
538 nfs_inode_remove_request(req);
539 nfs_unlock_request(req);
543 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
545 * nfs_scan_commit - Scan an inode for commit requests
546 * @inode: NFS inode to scan
547 * @dst: destination list
548 * @idx_start: lower bound of page->index to scan.
549 * @npages: idx_start + npages sets the upper bound to scan.
551 * Moves requests from the inode's 'commit' request list.
552 * The requests are *not* checked to ensure that they form a contiguous set.
555 nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
557 struct nfs_inode *nfsi = NFS_I(inode);
560 if (nfsi->ncommit != 0) {
561 res = nfs_scan_list(nfsi, dst, idx_start, npages,
562 NFS_PAGE_TAG_COMMIT);
563 nfsi->ncommit -= res;
568 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
575 * Try to update any existing write request, or create one if there is none.
576 * In order to match, the request's credentials must match those of
577 * the calling process.
579 * Note: Should always be called with the Page Lock held!
581 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
582 struct page *page, unsigned int offset, unsigned int bytes)
584 struct address_space *mapping = page->mapping;
585 struct inode *inode = mapping->host;
586 struct nfs_page *req, *new = NULL;
589 end = offset + bytes;
592 /* Loop over all inode entries and see if we find
593 * A request for the page we wish to update
595 spin_lock(&inode->i_lock);
596 req = nfs_page_find_request_locked(page);
598 if (!nfs_lock_request_dontget(req)) {
601 spin_unlock(&inode->i_lock);
602 error = nfs_wait_on_request(req);
603 nfs_release_request(req);
606 nfs_release_request(new);
607 return ERR_PTR(error);
611 spin_unlock(&inode->i_lock);
613 nfs_release_request(new);
619 nfs_lock_request_dontget(new);
620 error = nfs_inode_add_request(inode, new);
622 spin_unlock(&inode->i_lock);
623 nfs_unlock_request(new);
624 return ERR_PTR(error);
626 spin_unlock(&inode->i_lock);
629 spin_unlock(&inode->i_lock);
631 new = nfs_create_request(ctx, inode, page, offset, bytes);
636 /* We have a request for our page.
637 * If the creds don't match, or the
638 * page addresses don't match,
639 * tell the caller to wait on the conflicting
642 rqend = req->wb_offset + req->wb_bytes;
643 if (req->wb_context != ctx
644 || req->wb_page != page
645 || !nfs_dirty_request(req)
646 || offset > rqend || end < req->wb_offset) {
647 nfs_unlock_request(req);
648 return ERR_PTR(-EBUSY);
651 /* Okay, the request matches. Update the region */
652 if (offset < req->wb_offset) {
653 req->wb_offset = offset;
654 req->wb_pgbase = offset;
655 req->wb_bytes = rqend - req->wb_offset;
659 req->wb_bytes = end - req->wb_offset;
664 int nfs_flush_incompatible(struct file *file, struct page *page)
666 struct nfs_open_context *ctx = nfs_file_open_context(file);
667 struct nfs_page *req;
668 int do_flush, status;
670 * Look for a request corresponding to this page. If there
671 * is one, and it belongs to another file, we flush it out
672 * before we try to copy anything into the page. Do this
673 * due to the lack of an ACCESS-type call in NFSv2.
674 * Also do the same if we find a request from an existing
678 req = nfs_page_find_request(page);
681 do_flush = req->wb_page != page || req->wb_context != ctx
682 || !nfs_dirty_request(req);
683 nfs_release_request(req);
686 status = nfs_wb_page(page->mapping->host, page);
687 } while (status == 0);
692 * Update and possibly write a cached page of an NFS file.
694 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
695 * things with a page scheduled for an RPC call (e.g. invalidate it).
697 int nfs_updatepage(struct file *file, struct page *page,
698 unsigned int offset, unsigned int count)
700 struct nfs_open_context *ctx = nfs_file_open_context(file);
701 struct inode *inode = page->mapping->host;
704 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
706 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
707 file->f_path.dentry->d_parent->d_name.name,
708 file->f_path.dentry->d_name.name, count,
709 (long long)(page_offset(page) +offset));
711 /* If we're not using byte range locks, and we know the page
712 * is entirely in cache, it may be more efficient to avoid
713 * fragmenting write requests.
715 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
716 count = max(count + offset, nfs_page_length(page));
720 status = nfs_writepage_setup(ctx, page, offset, count);
721 __set_page_dirty_nobuffers(page);
723 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
724 status, (long long)i_size_read(inode));
726 nfs_set_pageerror(page);
730 static void nfs_writepage_release(struct nfs_page *req)
733 if (PageError(req->wb_page)) {
734 nfs_end_page_writeback(req->wb_page);
735 nfs_inode_remove_request(req);
736 } else if (!nfs_reschedule_unstable_write(req)) {
737 /* Set the PG_uptodate flag */
738 nfs_mark_uptodate(req->wb_page, req->wb_pgbase, req->wb_bytes);
739 nfs_end_page_writeback(req->wb_page);
740 nfs_inode_remove_request(req);
742 nfs_end_page_writeback(req->wb_page);
743 nfs_clear_page_tag_locked(req);
746 static inline int flush_task_priority(int how)
748 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
750 return RPC_PRIORITY_HIGH;
752 return RPC_PRIORITY_LOW;
754 return RPC_PRIORITY_NORMAL;
758 * Set up the argument/result storage required for the RPC call.
760 static void nfs_write_rpcsetup(struct nfs_page *req,
761 struct nfs_write_data *data,
762 const struct rpc_call_ops *call_ops,
763 unsigned int count, unsigned int offset,
769 /* Set up the RPC argument and reply structs
770 * NB: take care not to mess about with data->commit et al. */
773 data->inode = inode = req->wb_context->path.dentry->d_inode;
774 data->cred = req->wb_context->cred;
776 data->args.fh = NFS_FH(inode);
777 data->args.offset = req_offset(req) + offset;
778 data->args.pgbase = req->wb_pgbase + offset;
779 data->args.pages = data->pagevec;
780 data->args.count = count;
781 data->args.context = req->wb_context;
783 data->res.fattr = &data->fattr;
784 data->res.count = count;
785 data->res.verf = &data->verf;
786 nfs_fattr_init(&data->fattr);
788 /* Set up the initial task struct. */
789 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
790 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
791 NFS_PROTO(inode)->write_setup(data, how);
793 data->task.tk_priority = flush_task_priority(how);
794 data->task.tk_cookie = (unsigned long)inode;
796 dprintk("NFS: %5u initiated write call "
797 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
800 (long long)NFS_FILEID(inode),
802 (unsigned long long)data->args.offset);
805 static void nfs_execute_write(struct nfs_write_data *data)
807 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
810 rpc_clnt_sigmask(clnt, &oldset);
811 rpc_execute(&data->task);
812 rpc_clnt_sigunmask(clnt, &oldset);
816 * Generate multiple small requests to write out a single
817 * contiguous dirty area on one page.
819 static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
821 struct nfs_page *req = nfs_list_entry(head->next);
822 struct page *page = req->wb_page;
823 struct nfs_write_data *data;
824 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
829 nfs_list_remove_request(req);
833 size_t len = min(nbytes, wsize);
835 data = nfs_writedata_alloc(1);
838 list_add(&data->pages, &list);
841 } while (nbytes != 0);
842 atomic_set(&req->wb_complete, requests);
844 ClearPageError(page);
848 data = list_entry(list.next, struct nfs_write_data, pages);
849 list_del_init(&data->pages);
851 data->pagevec[0] = page;
855 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
859 nfs_execute_write(data);
860 } while (nbytes != 0);
865 while (!list_empty(&list)) {
866 data = list_entry(list.next, struct nfs_write_data, pages);
867 list_del(&data->pages);
868 nfs_writedata_release(data);
870 nfs_redirty_request(req);
871 nfs_end_page_writeback(req->wb_page);
872 nfs_clear_page_tag_locked(req);
877 * Create an RPC task for the given write request and kick it.
878 * The page must have been locked by the caller.
880 * It may happen that the page we're passed is not marked dirty.
881 * This is the case if nfs_updatepage detects a conflicting request
882 * that has been written but not committed.
884 static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
886 struct nfs_page *req;
888 struct nfs_write_data *data;
890 data = nfs_writedata_alloc(npages);
894 pages = data->pagevec;
895 while (!list_empty(head)) {
896 req = nfs_list_entry(head->next);
897 nfs_list_remove_request(req);
898 nfs_list_add_request(req, &data->pages);
899 ClearPageError(req->wb_page);
900 *pages++ = req->wb_page;
902 req = nfs_list_entry(data->pages.next);
904 /* Set up the argument struct */
905 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
907 nfs_execute_write(data);
910 while (!list_empty(head)) {
911 req = nfs_list_entry(head->next);
912 nfs_list_remove_request(req);
913 nfs_redirty_request(req);
914 nfs_end_page_writeback(req->wb_page);
915 nfs_clear_page_tag_locked(req);
920 static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
921 struct inode *inode, int ioflags)
923 int wsize = NFS_SERVER(inode)->wsize;
925 if (wsize < PAGE_CACHE_SIZE)
926 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
928 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
932 * Handle a write reply that flushed part of a page.
934 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
936 struct nfs_write_data *data = calldata;
937 struct nfs_page *req = data->req;
938 struct page *page = req->wb_page;
940 dprintk("NFS: write (%s/%Ld %d@%Ld)",
941 req->wb_context->path.dentry->d_inode->i_sb->s_id,
942 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
944 (long long)req_offset(req));
946 if (nfs_writeback_done(task, data) != 0)
949 if (task->tk_status < 0) {
950 nfs_set_pageerror(page);
951 nfs_context_set_write_error(req->wb_context, task->tk_status);
952 dprintk(", error = %d\n", task->tk_status);
956 if (nfs_write_need_commit(data)) {
957 struct inode *inode = page->mapping->host;
959 spin_lock(&inode->i_lock);
960 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
961 /* Do nothing we need to resend the writes */
962 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
963 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
964 dprintk(" defer commit\n");
965 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
966 set_bit(PG_NEED_RESCHED, &req->wb_flags);
967 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
968 dprintk(" server reboot detected\n");
970 spin_unlock(&inode->i_lock);
975 if (atomic_dec_and_test(&req->wb_complete))
976 nfs_writepage_release(req);
979 static const struct rpc_call_ops nfs_write_partial_ops = {
980 .rpc_call_done = nfs_writeback_done_partial,
981 .rpc_release = nfs_writedata_release,
985 * Handle a write reply that flushes a whole page.
987 * FIXME: There is an inherent race with invalidate_inode_pages and
988 * writebacks since the page->count is kept > 1 for as long
989 * as the page has a write request pending.
991 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
993 struct nfs_write_data *data = calldata;
994 struct nfs_page *req;
997 if (nfs_writeback_done(task, data) != 0)
1000 /* Update attributes as result of writeback. */
1001 while (!list_empty(&data->pages)) {
1002 req = nfs_list_entry(data->pages.next);
1003 nfs_list_remove_request(req);
1004 page = req->wb_page;
1006 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1007 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1008 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1010 (long long)req_offset(req));
1012 if (task->tk_status < 0) {
1013 nfs_set_pageerror(page);
1014 nfs_context_set_write_error(req->wb_context, task->tk_status);
1015 dprintk(", error = %d\n", task->tk_status);
1016 goto remove_request;
1019 if (nfs_write_need_commit(data)) {
1020 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1021 nfs_mark_request_commit(req);
1022 nfs_end_page_writeback(page);
1023 dprintk(" marked for commit\n");
1026 /* Set the PG_uptodate flag? */
1027 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
1030 nfs_end_page_writeback(page);
1031 nfs_inode_remove_request(req);
1033 nfs_clear_page_tag_locked(req);
1037 static const struct rpc_call_ops nfs_write_full_ops = {
1038 .rpc_call_done = nfs_writeback_done_full,
1039 .rpc_release = nfs_writedata_release,
1044 * This function is called when the WRITE call is complete.
1046 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1048 struct nfs_writeargs *argp = &data->args;
1049 struct nfs_writeres *resp = &data->res;
1052 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1053 task->tk_pid, task->tk_status);
1056 * ->write_done will attempt to use post-op attributes to detect
1057 * conflicting writes by other clients. A strict interpretation
1058 * of close-to-open would allow us to continue caching even if
1059 * another writer had changed the file, but some applications
1060 * depend on tighter cache coherency when writing.
1062 status = NFS_PROTO(data->inode)->write_done(task, data);
1065 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1067 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1068 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1069 /* We tried a write call, but the server did not
1070 * commit data to stable storage even though we
1072 * Note: There is a known bug in Tru64 < 5.0 in which
1073 * the server reports NFS_DATA_SYNC, but performs
1074 * NFS_FILE_SYNC. We therefore implement this checking
1075 * as a dprintk() in order to avoid filling syslog.
1077 static unsigned long complain;
1079 if (time_before(complain, jiffies)) {
1080 dprintk("NFS: faulty NFS server %s:"
1081 " (committed = %d) != (stable = %d)\n",
1082 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1083 resp->verf->committed, argp->stable);
1084 complain = jiffies + 300 * HZ;
1088 /* Is this a short write? */
1089 if (task->tk_status >= 0 && resp->count < argp->count) {
1090 static unsigned long complain;
1092 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1094 /* Has the server at least made some progress? */
1095 if (resp->count != 0) {
1096 /* Was this an NFSv2 write or an NFSv3 stable write? */
1097 if (resp->verf->committed != NFS_UNSTABLE) {
1098 /* Resend from where the server left off */
1099 argp->offset += resp->count;
1100 argp->pgbase += resp->count;
1101 argp->count -= resp->count;
1103 /* Resend as a stable write in order to avoid
1104 * headaches in the case of a server crash.
1106 argp->stable = NFS_FILE_SYNC;
1108 rpc_restart_call(task);
1111 if (time_before(complain, jiffies)) {
1113 "NFS: Server wrote zero bytes, expected %u.\n",
1115 complain = jiffies + 300 * HZ;
1117 /* Can't do anything about it except throw an error. */
1118 task->tk_status = -EIO;
1124 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1125 void nfs_commit_release(void *wdata)
1127 nfs_commit_free(wdata);
1131 * Set up the argument/result storage required for the RPC call.
1133 static void nfs_commit_rpcsetup(struct list_head *head,
1134 struct nfs_write_data *data,
1137 struct nfs_page *first;
1138 struct inode *inode;
1141 /* Set up the RPC argument and reply structs
1142 * NB: take care not to mess about with data->commit et al. */
1144 list_splice_init(head, &data->pages);
1145 first = nfs_list_entry(data->pages.next);
1146 inode = first->wb_context->path.dentry->d_inode;
1148 data->inode = inode;
1149 data->cred = first->wb_context->cred;
1151 data->args.fh = NFS_FH(data->inode);
1152 /* Note: we always request a commit of the entire inode */
1153 data->args.offset = 0;
1154 data->args.count = 0;
1155 data->res.count = 0;
1156 data->res.fattr = &data->fattr;
1157 data->res.verf = &data->verf;
1158 nfs_fattr_init(&data->fattr);
1160 /* Set up the initial task struct. */
1161 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1162 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
1163 NFS_PROTO(inode)->commit_setup(data, how);
1165 data->task.tk_priority = flush_task_priority(how);
1166 data->task.tk_cookie = (unsigned long)inode;
1168 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1172 * Commit dirty pages
1175 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1177 struct nfs_write_data *data;
1178 struct nfs_page *req;
1180 data = nfs_commit_alloc();
1185 /* Set up the argument struct */
1186 nfs_commit_rpcsetup(head, data, how);
1188 nfs_execute_write(data);
1191 while (!list_empty(head)) {
1192 req = nfs_list_entry(head->next);
1193 nfs_list_remove_request(req);
1194 nfs_mark_request_commit(req);
1195 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1196 nfs_clear_page_tag_locked(req);
1202 * COMMIT call returned
1204 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1206 struct nfs_write_data *data = calldata;
1207 struct nfs_page *req;
1209 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1210 task->tk_pid, task->tk_status);
1212 /* Call the NFS version-specific code */
1213 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1216 while (!list_empty(&data->pages)) {
1217 req = nfs_list_entry(data->pages.next);
1218 nfs_list_remove_request(req);
1219 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
1220 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1222 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1223 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1224 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1226 (long long)req_offset(req));
1227 if (task->tk_status < 0) {
1228 nfs_context_set_write_error(req->wb_context, task->tk_status);
1229 nfs_inode_remove_request(req);
1230 dprintk(", error = %d\n", task->tk_status);
1234 /* Okay, COMMIT succeeded, apparently. Check the verifier
1235 * returned by the server against all stored verfs. */
1236 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1237 /* We have a match */
1238 /* Set the PG_uptodate flag */
1239 nfs_mark_uptodate(req->wb_page, req->wb_pgbase,
1241 nfs_inode_remove_request(req);
1245 /* We have a mismatch. Write the page again */
1246 dprintk(" mismatch\n");
1247 nfs_redirty_request(req);
1249 nfs_clear_page_tag_locked(req);
1253 static const struct rpc_call_ops nfs_commit_ops = {
1254 .rpc_call_done = nfs_commit_done,
1255 .rpc_release = nfs_commit_release,
1258 int nfs_commit_inode(struct inode *inode, int how)
1263 spin_lock(&inode->i_lock);
1264 res = nfs_scan_commit(inode, &head, 0, 0);
1265 spin_unlock(&inode->i_lock);
1267 int error = nfs_commit_list(inode, &head, how);
1274 static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1280 long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
1282 struct inode *inode = mapping->host;
1283 pgoff_t idx_start, idx_end;
1284 unsigned int npages = 0;
1286 int nocommit = how & FLUSH_NOCOMMIT;
1290 if (wbc->range_cyclic)
1293 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1294 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1295 if (idx_end > idx_start) {
1296 pgoff_t l_npages = 1 + idx_end - idx_start;
1298 if (sizeof(npages) != sizeof(l_npages) &&
1299 (pgoff_t)npages != l_npages)
1303 how &= ~FLUSH_NOCOMMIT;
1304 spin_lock(&inode->i_lock);
1306 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1311 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1314 if (how & FLUSH_INVALIDATE) {
1315 spin_unlock(&inode->i_lock);
1316 nfs_cancel_commit_list(&head);
1318 spin_lock(&inode->i_lock);
1321 pages += nfs_scan_commit(inode, &head, 0, 0);
1322 spin_unlock(&inode->i_lock);
1323 ret = nfs_commit_list(inode, &head, how);
1324 spin_lock(&inode->i_lock);
1327 spin_unlock(&inode->i_lock);
1331 static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
1335 ret = nfs_writepages(mapping, wbc);
1338 ret = nfs_sync_mapping_wait(mapping, wbc, how);
1343 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1347 /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
1348 static int nfs_write_mapping(struct address_space *mapping, int how)
1350 struct writeback_control wbc = {
1351 .bdi = mapping->backing_dev_info,
1352 .sync_mode = WB_SYNC_NONE,
1353 .nr_to_write = LONG_MAX,
1354 .for_writepages = 1,
1359 ret = __nfs_write_mapping(mapping, &wbc, how);
1362 wbc.sync_mode = WB_SYNC_ALL;
1363 return __nfs_write_mapping(mapping, &wbc, how);
1367 * flush the inode to disk.
1369 int nfs_wb_all(struct inode *inode)
1371 return nfs_write_mapping(inode->i_mapping, 0);
1374 int nfs_wb_nocommit(struct inode *inode)
1376 return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
1379 int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1381 struct nfs_page *req;
1382 loff_t range_start = page_offset(page);
1383 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1384 struct writeback_control wbc = {
1385 .bdi = page->mapping->backing_dev_info,
1386 .sync_mode = WB_SYNC_ALL,
1387 .nr_to_write = LONG_MAX,
1388 .range_start = range_start,
1389 .range_end = range_end,
1393 BUG_ON(!PageLocked(page));
1395 req = nfs_page_find_request(page);
1398 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1399 nfs_release_request(req);
1402 if (nfs_lock_request_dontget(req)) {
1403 nfs_inode_remove_request(req);
1405 * In case nfs_inode_remove_request has marked the
1406 * page as being dirty
1408 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1409 nfs_unlock_request(req);
1412 ret = nfs_wait_on_request(req);
1416 if (!PagePrivate(page))
1418 ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
1423 int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
1425 loff_t range_start = page_offset(page);
1426 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1427 struct writeback_control wbc = {
1428 .bdi = page->mapping->backing_dev_info,
1429 .sync_mode = WB_SYNC_ALL,
1430 .nr_to_write = LONG_MAX,
1431 .range_start = range_start,
1432 .range_end = range_end,
1436 BUG_ON(!PageLocked(page));
1437 if (clear_page_dirty_for_io(page)) {
1438 ret = nfs_writepage_locked(page, &wbc);
1442 if (!PagePrivate(page))
1444 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1448 __mark_inode_dirty(inode, I_DIRTY_PAGES);
1453 * Write back all requests on one page - we do this before reading it.
1455 int nfs_wb_page(struct inode *inode, struct page* page)
1457 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
1460 int __init nfs_init_writepagecache(void)
1462 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1463 sizeof(struct nfs_write_data),
1464 0, SLAB_HWCACHE_ALIGN,
1466 if (nfs_wdata_cachep == NULL)
1469 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1471 if (nfs_wdata_mempool == NULL)
1474 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1476 if (nfs_commit_mempool == NULL)
1480 * NFS congestion size, scale with available memory.
1492 * This allows larger machines to have larger/more transfers.
1493 * Limit the default to 256M
1495 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1496 if (nfs_congestion_kb > 256*1024)
1497 nfs_congestion_kb = 256*1024;
1502 void nfs_destroy_writepagecache(void)
1504 mempool_destroy(nfs_commit_mempool);
1505 mempool_destroy(nfs_wdata_mempool);
1506 kmem_cache_destroy(nfs_wdata_cachep);