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 void nfs_commit_free(struct nfs_write_data *p)
63 if (p && (p->pagevec != &p->page_array[0]))
65 mempool_free(p, nfs_commit_mempool);
68 struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
70 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
73 memset(p, 0, sizeof(*p));
74 INIT_LIST_HEAD(&p->pages);
75 p->npages = pagecount;
76 if (pagecount <= ARRAY_SIZE(p->page_array))
77 p->pagevec = p->page_array;
79 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
81 mempool_free(p, nfs_wdata_mempool);
89 static void nfs_writedata_free(struct nfs_write_data *p)
91 if (p && (p->pagevec != &p->page_array[0]))
93 mempool_free(p, nfs_wdata_mempool);
96 void nfs_writedata_release(void *data)
98 struct nfs_write_data *wdata = data;
100 put_nfs_open_context(wdata->args.context);
101 nfs_writedata_free(wdata);
104 static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
108 set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
111 static struct nfs_page *nfs_page_find_request_locked(struct page *page)
113 struct nfs_page *req = NULL;
115 if (PagePrivate(page)) {
116 req = (struct nfs_page *)page_private(page);
118 kref_get(&req->wb_kref);
123 static struct nfs_page *nfs_page_find_request(struct page *page)
125 struct inode *inode = page->mapping->host;
126 struct nfs_page *req = NULL;
128 spin_lock(&inode->i_lock);
129 req = nfs_page_find_request_locked(page);
130 spin_unlock(&inode->i_lock);
134 /* Adjust the file length if we're writing beyond the end */
135 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
137 struct inode *inode = page->mapping->host;
138 loff_t end, i_size = i_size_read(inode);
139 pgoff_t end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
141 if (i_size > 0 && page->index < end_index)
143 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
146 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
147 i_size_write(inode, end);
150 /* A writeback failed: mark the page as bad, and invalidate the page cache */
151 static void nfs_set_pageerror(struct page *page)
154 nfs_zap_mapping(page->mapping->host, page->mapping);
157 /* We can set the PG_uptodate flag if we see that a write request
158 * covers the full page.
160 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
162 if (PageUptodate(page))
166 if (count != nfs_page_length(page))
168 SetPageUptodate(page);
171 static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
172 unsigned int offset, unsigned int count)
174 struct nfs_page *req;
178 req = nfs_update_request(ctx, page, offset, count);
184 ret = nfs_wb_page(page->mapping->host, page);
188 /* Update file length */
189 nfs_grow_file(page, offset, count);
190 nfs_clear_page_tag_locked(req);
194 static int wb_priority(struct writeback_control *wbc)
196 if (wbc->for_reclaim)
197 return FLUSH_HIGHPRI | FLUSH_STABLE;
198 if (wbc->for_kupdate)
204 * NFS congestion control
207 int nfs_congestion_kb;
209 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
210 #define NFS_CONGESTION_OFF_THRESH \
211 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
213 static int nfs_set_page_writeback(struct page *page)
215 int ret = test_set_page_writeback(page);
218 struct inode *inode = page->mapping->host;
219 struct nfs_server *nfss = NFS_SERVER(inode);
221 if (atomic_long_inc_return(&nfss->writeback) >
222 NFS_CONGESTION_ON_THRESH)
223 set_bdi_congested(&nfss->backing_dev_info, WRITE);
228 static void nfs_end_page_writeback(struct page *page)
230 struct inode *inode = page->mapping->host;
231 struct nfs_server *nfss = NFS_SERVER(inode);
233 end_page_writeback(page);
234 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
235 clear_bdi_congested(&nfss->backing_dev_info, WRITE);
239 * Find an associated nfs write request, and prepare to flush it out
240 * May return an error if the user signalled nfs_wait_on_request().
242 static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
245 struct inode *inode = page->mapping->host;
246 struct nfs_page *req;
249 spin_lock(&inode->i_lock);
251 req = nfs_page_find_request_locked(page);
253 spin_unlock(&inode->i_lock);
256 if (nfs_set_page_tag_locked(req))
258 /* Note: If we hold the page lock, as is the case in nfs_writepage,
259 * then the call to nfs_set_page_tag_locked() will always
260 * succeed provided that someone hasn't already marked the
261 * request as dirty (in which case we don't care).
263 spin_unlock(&inode->i_lock);
264 ret = nfs_wait_on_request(req);
265 nfs_release_request(req);
268 spin_lock(&inode->i_lock);
270 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
271 /* This request is marked for commit */
272 spin_unlock(&inode->i_lock);
273 nfs_clear_page_tag_locked(req);
274 nfs_pageio_complete(pgio);
277 if (nfs_set_page_writeback(page) != 0) {
278 spin_unlock(&inode->i_lock);
281 spin_unlock(&inode->i_lock);
282 nfs_pageio_add_request(pgio, req);
286 static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
288 struct inode *inode = page->mapping->host;
290 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
291 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
293 nfs_pageio_cond_complete(pgio, page->index);
294 return nfs_page_async_flush(pgio, page);
298 * Write an mmapped page to the server.
300 static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
302 struct nfs_pageio_descriptor pgio;
305 nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
306 err = nfs_do_writepage(page, wbc, &pgio);
307 nfs_pageio_complete(&pgio);
310 if (pgio.pg_error < 0)
311 return pgio.pg_error;
315 int nfs_writepage(struct page *page, struct writeback_control *wbc)
319 ret = nfs_writepage_locked(page, wbc);
324 static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
328 ret = nfs_do_writepage(page, wbc, data);
333 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
335 struct inode *inode = mapping->host;
336 struct nfs_pageio_descriptor pgio;
339 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
341 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
342 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
343 nfs_pageio_complete(&pgio);
346 if (pgio.pg_error < 0)
347 return pgio.pg_error;
352 * Insert a write request into an inode
354 static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
356 struct nfs_inode *nfsi = NFS_I(inode);
359 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
363 if (nfs_have_delegation(inode, FMODE_WRITE))
366 SetPagePrivate(req->wb_page);
367 set_page_private(req->wb_page, (unsigned long)req);
369 kref_get(&req->wb_kref);
370 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
371 NFS_PAGE_TAG_LOCKED);
375 * Remove a write request from an inode
377 static void nfs_inode_remove_request(struct nfs_page *req)
379 struct inode *inode = req->wb_context->path.dentry->d_inode;
380 struct nfs_inode *nfsi = NFS_I(inode);
382 BUG_ON (!NFS_WBACK_BUSY(req));
384 spin_lock(&inode->i_lock);
385 set_page_private(req->wb_page, 0);
386 ClearPagePrivate(req->wb_page);
387 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
390 spin_unlock(&inode->i_lock);
393 spin_unlock(&inode->i_lock);
394 nfs_clear_request(req);
395 nfs_release_request(req);
399 nfs_redirty_request(struct nfs_page *req)
401 __set_page_dirty_nobuffers(req->wb_page);
405 * Check if a request is dirty
408 nfs_dirty_request(struct nfs_page *req)
410 struct page *page = req->wb_page;
412 if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
414 return !PageWriteback(req->wb_page);
417 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
419 * Add a request to the inode's commit list.
422 nfs_mark_request_commit(struct nfs_page *req)
424 struct inode *inode = req->wb_context->path.dentry->d_inode;
425 struct nfs_inode *nfsi = NFS_I(inode);
427 spin_lock(&inode->i_lock);
429 set_bit(PG_NEED_COMMIT, &(req)->wb_flags);
430 radix_tree_tag_set(&nfsi->nfs_page_tree,
432 NFS_PAGE_TAG_COMMIT);
433 spin_unlock(&inode->i_lock);
434 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
435 inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
436 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
440 int nfs_write_need_commit(struct nfs_write_data *data)
442 return data->verf.committed != NFS_FILE_SYNC;
446 int nfs_reschedule_unstable_write(struct nfs_page *req)
448 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
449 nfs_mark_request_commit(req);
452 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
453 nfs_redirty_request(req);
460 nfs_mark_request_commit(struct nfs_page *req)
465 int nfs_write_need_commit(struct nfs_write_data *data)
471 int nfs_reschedule_unstable_write(struct nfs_page *req)
478 * Wait for a request to complete.
480 * Interruptible by fatal signals only.
482 static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
484 struct nfs_inode *nfsi = NFS_I(inode);
485 struct nfs_page *req;
486 pgoff_t idx_end, next;
487 unsigned int res = 0;
493 idx_end = idx_start + npages - 1;
496 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) {
497 if (req->wb_index > idx_end)
500 next = req->wb_index + 1;
501 BUG_ON(!NFS_WBACK_BUSY(req));
503 kref_get(&req->wb_kref);
504 spin_unlock(&inode->i_lock);
505 error = nfs_wait_on_request(req);
506 nfs_release_request(req);
507 spin_lock(&inode->i_lock);
515 static void nfs_cancel_commit_list(struct list_head *head)
517 struct nfs_page *req;
519 while(!list_empty(head)) {
520 req = nfs_list_entry(head->next);
521 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
522 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
524 nfs_list_remove_request(req);
525 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
526 nfs_inode_remove_request(req);
527 nfs_unlock_request(req);
531 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
533 * nfs_scan_commit - Scan an inode for commit requests
534 * @inode: NFS inode to scan
535 * @dst: destination list
536 * @idx_start: lower bound of page->index to scan.
537 * @npages: idx_start + npages sets the upper bound to scan.
539 * Moves requests from the inode's 'commit' request list.
540 * The requests are *not* checked to ensure that they form a contiguous set.
543 nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
545 struct nfs_inode *nfsi = NFS_I(inode);
548 if (nfsi->ncommit != 0) {
549 res = nfs_scan_list(nfsi, dst, idx_start, npages,
550 NFS_PAGE_TAG_COMMIT);
551 nfsi->ncommit -= res;
556 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
563 * Try to update any existing write request, or create one if there is none.
564 * In order to match, the request's credentials must match those of
565 * the calling process.
567 * Note: Should always be called with the Page Lock held!
569 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
570 struct page *page, unsigned int offset, unsigned int bytes)
572 struct address_space *mapping = page->mapping;
573 struct inode *inode = mapping->host;
574 struct nfs_page *req, *new = NULL;
577 end = offset + bytes;
580 /* Loop over all inode entries and see if we find
581 * A request for the page we wish to update
584 if (radix_tree_preload(GFP_NOFS)) {
585 nfs_release_request(new);
586 return ERR_PTR(-ENOMEM);
590 spin_lock(&inode->i_lock);
591 req = nfs_page_find_request_locked(page);
593 if (!nfs_set_page_tag_locked(req)) {
596 spin_unlock(&inode->i_lock);
597 error = nfs_wait_on_request(req);
598 nfs_release_request(req);
601 radix_tree_preload_end();
602 nfs_release_request(new);
604 return ERR_PTR(error);
608 spin_unlock(&inode->i_lock);
610 radix_tree_preload_end();
611 nfs_release_request(new);
617 nfs_lock_request_dontget(new);
618 nfs_inode_add_request(inode, new);
619 spin_unlock(&inode->i_lock);
620 radix_tree_preload_end();
624 spin_unlock(&inode->i_lock);
626 new = nfs_create_request(ctx, inode, page, offset, bytes);
631 /* We have a request for our page.
632 * If the creds don't match, or the
633 * page addresses don't match,
634 * tell the caller to wait on the conflicting
637 rqend = req->wb_offset + req->wb_bytes;
638 if (req->wb_context != ctx
639 || req->wb_page != page
640 || !nfs_dirty_request(req)
641 || offset > rqend || end < req->wb_offset) {
642 nfs_clear_page_tag_locked(req);
643 return ERR_PTR(-EBUSY);
646 /* Okay, the request matches. Update the region */
647 if (offset < req->wb_offset) {
648 req->wb_offset = offset;
649 req->wb_pgbase = offset;
650 req->wb_bytes = max(end, rqend) - req->wb_offset;
655 req->wb_bytes = end - req->wb_offset;
659 /* If this page might potentially be marked as up to date,
660 * then we need to zero any uninitalised data. */
661 if (req->wb_pgbase == 0 && req->wb_bytes != PAGE_CACHE_SIZE
662 && !PageUptodate(req->wb_page))
663 zero_user_segment(req->wb_page, req->wb_bytes, PAGE_CACHE_SIZE);
667 int nfs_flush_incompatible(struct file *file, struct page *page)
669 struct nfs_open_context *ctx = nfs_file_open_context(file);
670 struct nfs_page *req;
671 int do_flush, status;
673 * Look for a request corresponding to this page. If there
674 * is one, and it belongs to another file, we flush it out
675 * before we try to copy anything into the page. Do this
676 * due to the lack of an ACCESS-type call in NFSv2.
677 * Also do the same if we find a request from an existing
681 req = nfs_page_find_request(page);
684 do_flush = req->wb_page != page || req->wb_context != ctx
685 || !nfs_dirty_request(req);
686 nfs_release_request(req);
689 status = nfs_wb_page(page->mapping->host, page);
690 } while (status == 0);
695 * If the page cache is marked as unsafe or invalid, then we can't rely on
696 * the PageUptodate() flag. In this case, we will need to turn off
697 * write optimisations that depend on the page contents being correct.
699 static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
701 return PageUptodate(page) &&
702 !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
706 * Update and possibly write a cached page of an NFS file.
708 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
709 * things with a page scheduled for an RPC call (e.g. invalidate it).
711 int nfs_updatepage(struct file *file, struct page *page,
712 unsigned int offset, unsigned int count)
714 struct nfs_open_context *ctx = nfs_file_open_context(file);
715 struct inode *inode = page->mapping->host;
718 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
720 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
721 file->f_path.dentry->d_parent->d_name.name,
722 file->f_path.dentry->d_name.name, count,
723 (long long)(page_offset(page) +offset));
725 /* If we're not using byte range locks, and we know the page
726 * is up to date, it may be more efficient to extend the write
727 * to cover the entire page in order to avoid fragmentation
730 if (nfs_write_pageuptodate(page, inode) &&
731 inode->i_flock == NULL &&
732 !(file->f_flags & O_SYNC)) {
733 count = max(count + offset, nfs_page_length(page));
737 status = nfs_writepage_setup(ctx, page, offset, count);
738 __set_page_dirty_nobuffers(page);
740 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
741 status, (long long)i_size_read(inode));
743 nfs_set_pageerror(page);
747 static void nfs_writepage_release(struct nfs_page *req)
750 if (PageError(req->wb_page)) {
751 nfs_end_page_writeback(req->wb_page);
752 nfs_inode_remove_request(req);
753 } else if (!nfs_reschedule_unstable_write(req)) {
754 /* Set the PG_uptodate flag */
755 nfs_mark_uptodate(req->wb_page, req->wb_pgbase, req->wb_bytes);
756 nfs_end_page_writeback(req->wb_page);
757 nfs_inode_remove_request(req);
759 nfs_end_page_writeback(req->wb_page);
760 nfs_clear_page_tag_locked(req);
763 static int flush_task_priority(int how)
765 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
767 return RPC_PRIORITY_HIGH;
769 return RPC_PRIORITY_LOW;
771 return RPC_PRIORITY_NORMAL;
775 * Set up the argument/result storage required for the RPC call.
777 static void nfs_write_rpcsetup(struct nfs_page *req,
778 struct nfs_write_data *data,
779 const struct rpc_call_ops *call_ops,
780 unsigned int count, unsigned int offset,
783 struct inode *inode = req->wb_context->path.dentry->d_inode;
784 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
785 int priority = flush_task_priority(how);
786 struct rpc_task *task;
787 struct rpc_message msg = {
788 .rpc_argp = &data->args,
789 .rpc_resp = &data->res,
790 .rpc_cred = req->wb_context->cred,
792 struct rpc_task_setup task_setup_data = {
793 .rpc_client = NFS_CLIENT(inode),
796 .callback_ops = call_ops,
797 .callback_data = data,
798 .workqueue = nfsiod_workqueue,
800 .priority = priority,
803 /* Set up the RPC argument and reply structs
804 * NB: take care not to mess about with data->commit et al. */
807 data->inode = inode = req->wb_context->path.dentry->d_inode;
808 data->cred = msg.rpc_cred;
810 data->args.fh = NFS_FH(inode);
811 data->args.offset = req_offset(req) + offset;
812 data->args.pgbase = req->wb_pgbase + offset;
813 data->args.pages = data->pagevec;
814 data->args.count = count;
815 data->args.context = get_nfs_open_context(req->wb_context);
816 data->args.stable = NFS_UNSTABLE;
817 if (how & FLUSH_STABLE) {
818 data->args.stable = NFS_DATA_SYNC;
819 if (!NFS_I(inode)->ncommit)
820 data->args.stable = NFS_FILE_SYNC;
823 data->res.fattr = &data->fattr;
824 data->res.count = count;
825 data->res.verf = &data->verf;
826 nfs_fattr_init(&data->fattr);
828 /* Set up the initial task struct. */
829 NFS_PROTO(inode)->write_setup(data, &msg);
831 dprintk("NFS: %5u initiated write call "
832 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
835 (long long)NFS_FILEID(inode),
837 (unsigned long long)data->args.offset);
839 task = rpc_run_task(&task_setup_data);
845 * Generate multiple small requests to write out a single
846 * contiguous dirty area on one page.
848 static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
850 struct nfs_page *req = nfs_list_entry(head->next);
851 struct page *page = req->wb_page;
852 struct nfs_write_data *data;
853 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
858 nfs_list_remove_request(req);
862 size_t len = min(nbytes, wsize);
864 data = nfs_writedata_alloc(1);
867 list_add(&data->pages, &list);
870 } while (nbytes != 0);
871 atomic_set(&req->wb_complete, requests);
873 ClearPageError(page);
877 data = list_entry(list.next, struct nfs_write_data, pages);
878 list_del_init(&data->pages);
880 data->pagevec[0] = page;
884 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
888 } while (nbytes != 0);
893 while (!list_empty(&list)) {
894 data = list_entry(list.next, struct nfs_write_data, pages);
895 list_del(&data->pages);
896 nfs_writedata_release(data);
898 nfs_redirty_request(req);
899 nfs_end_page_writeback(req->wb_page);
900 nfs_clear_page_tag_locked(req);
905 * Create an RPC task for the given write request and kick it.
906 * The page must have been locked by the caller.
908 * It may happen that the page we're passed is not marked dirty.
909 * This is the case if nfs_updatepage detects a conflicting request
910 * that has been written but not committed.
912 static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
914 struct nfs_page *req;
916 struct nfs_write_data *data;
918 data = nfs_writedata_alloc(npages);
922 pages = data->pagevec;
923 while (!list_empty(head)) {
924 req = nfs_list_entry(head->next);
925 nfs_list_remove_request(req);
926 nfs_list_add_request(req, &data->pages);
927 ClearPageError(req->wb_page);
928 *pages++ = req->wb_page;
930 req = nfs_list_entry(data->pages.next);
932 /* Set up the argument struct */
933 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
937 while (!list_empty(head)) {
938 req = nfs_list_entry(head->next);
939 nfs_list_remove_request(req);
940 nfs_redirty_request(req);
941 nfs_end_page_writeback(req->wb_page);
942 nfs_clear_page_tag_locked(req);
947 static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
948 struct inode *inode, int ioflags)
950 size_t wsize = NFS_SERVER(inode)->wsize;
952 if (wsize < PAGE_CACHE_SIZE)
953 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
955 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
959 * Handle a write reply that flushed part of a page.
961 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
963 struct nfs_write_data *data = calldata;
964 struct nfs_page *req = data->req;
965 struct page *page = req->wb_page;
967 dprintk("NFS: write (%s/%Ld %d@%Ld)",
968 req->wb_context->path.dentry->d_inode->i_sb->s_id,
969 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
971 (long long)req_offset(req));
973 if (nfs_writeback_done(task, data) != 0)
976 if (task->tk_status < 0) {
977 nfs_set_pageerror(page);
978 nfs_context_set_write_error(req->wb_context, task->tk_status);
979 dprintk(", error = %d\n", task->tk_status);
983 if (nfs_write_need_commit(data)) {
984 struct inode *inode = page->mapping->host;
986 spin_lock(&inode->i_lock);
987 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
988 /* Do nothing we need to resend the writes */
989 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
990 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
991 dprintk(" defer commit\n");
992 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
993 set_bit(PG_NEED_RESCHED, &req->wb_flags);
994 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
995 dprintk(" server reboot detected\n");
997 spin_unlock(&inode->i_lock);
1002 if (atomic_dec_and_test(&req->wb_complete))
1003 nfs_writepage_release(req);
1006 static const struct rpc_call_ops nfs_write_partial_ops = {
1007 .rpc_call_done = nfs_writeback_done_partial,
1008 .rpc_release = nfs_writedata_release,
1012 * Handle a write reply that flushes a whole page.
1014 * FIXME: There is an inherent race with invalidate_inode_pages and
1015 * writebacks since the page->count is kept > 1 for as long
1016 * as the page has a write request pending.
1018 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1020 struct nfs_write_data *data = calldata;
1021 struct nfs_page *req;
1024 if (nfs_writeback_done(task, data) != 0)
1027 /* Update attributes as result of writeback. */
1028 while (!list_empty(&data->pages)) {
1029 req = nfs_list_entry(data->pages.next);
1030 nfs_list_remove_request(req);
1031 page = req->wb_page;
1033 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1034 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1035 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1037 (long long)req_offset(req));
1039 if (task->tk_status < 0) {
1040 nfs_set_pageerror(page);
1041 nfs_context_set_write_error(req->wb_context, task->tk_status);
1042 dprintk(", error = %d\n", task->tk_status);
1043 goto remove_request;
1046 if (nfs_write_need_commit(data)) {
1047 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1048 nfs_mark_request_commit(req);
1049 nfs_end_page_writeback(page);
1050 dprintk(" marked for commit\n");
1053 /* Set the PG_uptodate flag? */
1054 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
1057 nfs_end_page_writeback(page);
1058 nfs_inode_remove_request(req);
1060 nfs_clear_page_tag_locked(req);
1064 static const struct rpc_call_ops nfs_write_full_ops = {
1065 .rpc_call_done = nfs_writeback_done_full,
1066 .rpc_release = nfs_writedata_release,
1071 * This function is called when the WRITE call is complete.
1073 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1075 struct nfs_writeargs *argp = &data->args;
1076 struct nfs_writeres *resp = &data->res;
1079 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1080 task->tk_pid, task->tk_status);
1083 * ->write_done will attempt to use post-op attributes to detect
1084 * conflicting writes by other clients. A strict interpretation
1085 * of close-to-open would allow us to continue caching even if
1086 * another writer had changed the file, but some applications
1087 * depend on tighter cache coherency when writing.
1089 status = NFS_PROTO(data->inode)->write_done(task, data);
1092 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1094 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1095 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1096 /* We tried a write call, but the server did not
1097 * commit data to stable storage even though we
1099 * Note: There is a known bug in Tru64 < 5.0 in which
1100 * the server reports NFS_DATA_SYNC, but performs
1101 * NFS_FILE_SYNC. We therefore implement this checking
1102 * as a dprintk() in order to avoid filling syslog.
1104 static unsigned long complain;
1106 if (time_before(complain, jiffies)) {
1107 dprintk("NFS: faulty NFS server %s:"
1108 " (committed = %d) != (stable = %d)\n",
1109 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1110 resp->verf->committed, argp->stable);
1111 complain = jiffies + 300 * HZ;
1115 /* Is this a short write? */
1116 if (task->tk_status >= 0 && resp->count < argp->count) {
1117 static unsigned long complain;
1119 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1121 /* Has the server at least made some progress? */
1122 if (resp->count != 0) {
1123 /* Was this an NFSv2 write or an NFSv3 stable write? */
1124 if (resp->verf->committed != NFS_UNSTABLE) {
1125 /* Resend from where the server left off */
1126 argp->offset += resp->count;
1127 argp->pgbase += resp->count;
1128 argp->count -= resp->count;
1130 /* Resend as a stable write in order to avoid
1131 * headaches in the case of a server crash.
1133 argp->stable = NFS_FILE_SYNC;
1135 rpc_restart_call(task);
1138 if (time_before(complain, jiffies)) {
1140 "NFS: Server wrote zero bytes, expected %u.\n",
1142 complain = jiffies + 300 * HZ;
1144 /* Can't do anything about it except throw an error. */
1145 task->tk_status = -EIO;
1151 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1152 void nfs_commit_release(void *data)
1154 struct nfs_write_data *wdata = data;
1156 put_nfs_open_context(wdata->args.context);
1157 nfs_commit_free(wdata);
1161 * Set up the argument/result storage required for the RPC call.
1163 static void nfs_commit_rpcsetup(struct list_head *head,
1164 struct nfs_write_data *data,
1167 struct nfs_page *first = nfs_list_entry(head->next);
1168 struct inode *inode = first->wb_context->path.dentry->d_inode;
1169 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1170 int priority = flush_task_priority(how);
1171 struct rpc_task *task;
1172 struct rpc_message msg = {
1173 .rpc_argp = &data->args,
1174 .rpc_resp = &data->res,
1175 .rpc_cred = first->wb_context->cred,
1177 struct rpc_task_setup task_setup_data = {
1178 .task = &data->task,
1179 .rpc_client = NFS_CLIENT(inode),
1180 .rpc_message = &msg,
1181 .callback_ops = &nfs_commit_ops,
1182 .callback_data = data,
1183 .workqueue = nfsiod_workqueue,
1185 .priority = priority,
1188 /* Set up the RPC argument and reply structs
1189 * NB: take care not to mess about with data->commit et al. */
1191 list_splice_init(head, &data->pages);
1193 data->inode = inode;
1194 data->cred = msg.rpc_cred;
1196 data->args.fh = NFS_FH(data->inode);
1197 /* Note: we always request a commit of the entire inode */
1198 data->args.offset = 0;
1199 data->args.count = 0;
1200 data->args.context = get_nfs_open_context(first->wb_context);
1201 data->res.count = 0;
1202 data->res.fattr = &data->fattr;
1203 data->res.verf = &data->verf;
1204 nfs_fattr_init(&data->fattr);
1206 /* Set up the initial task struct. */
1207 NFS_PROTO(inode)->commit_setup(data, &msg);
1209 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1211 task = rpc_run_task(&task_setup_data);
1217 * Commit dirty pages
1220 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1222 struct nfs_write_data *data;
1223 struct nfs_page *req;
1225 data = nfs_commit_alloc();
1230 /* Set up the argument struct */
1231 nfs_commit_rpcsetup(head, data, how);
1235 while (!list_empty(head)) {
1236 req = nfs_list_entry(head->next);
1237 nfs_list_remove_request(req);
1238 nfs_mark_request_commit(req);
1239 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1240 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1242 nfs_clear_page_tag_locked(req);
1248 * COMMIT call returned
1250 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1252 struct nfs_write_data *data = calldata;
1253 struct nfs_page *req;
1255 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1256 task->tk_pid, task->tk_status);
1258 /* Call the NFS version-specific code */
1259 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1262 while (!list_empty(&data->pages)) {
1263 req = nfs_list_entry(data->pages.next);
1264 nfs_list_remove_request(req);
1265 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
1266 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1267 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1270 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1271 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1272 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1274 (long long)req_offset(req));
1275 if (task->tk_status < 0) {
1276 nfs_context_set_write_error(req->wb_context, task->tk_status);
1277 nfs_inode_remove_request(req);
1278 dprintk(", error = %d\n", task->tk_status);
1282 /* Okay, COMMIT succeeded, apparently. Check the verifier
1283 * returned by the server against all stored verfs. */
1284 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1285 /* We have a match */
1286 /* Set the PG_uptodate flag */
1287 nfs_mark_uptodate(req->wb_page, req->wb_pgbase,
1289 nfs_inode_remove_request(req);
1293 /* We have a mismatch. Write the page again */
1294 dprintk(" mismatch\n");
1295 nfs_redirty_request(req);
1297 nfs_clear_page_tag_locked(req);
1301 static const struct rpc_call_ops nfs_commit_ops = {
1302 .rpc_call_done = nfs_commit_done,
1303 .rpc_release = nfs_commit_release,
1306 int nfs_commit_inode(struct inode *inode, int how)
1311 spin_lock(&inode->i_lock);
1312 res = nfs_scan_commit(inode, &head, 0, 0);
1313 spin_unlock(&inode->i_lock);
1315 int error = nfs_commit_list(inode, &head, how);
1322 static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1328 long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
1330 struct inode *inode = mapping->host;
1331 pgoff_t idx_start, idx_end;
1332 unsigned int npages = 0;
1334 int nocommit = how & FLUSH_NOCOMMIT;
1338 if (wbc->range_cyclic)
1341 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1342 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1343 if (idx_end > idx_start) {
1344 pgoff_t l_npages = 1 + idx_end - idx_start;
1346 if (sizeof(npages) != sizeof(l_npages) &&
1347 (pgoff_t)npages != l_npages)
1351 how &= ~FLUSH_NOCOMMIT;
1352 spin_lock(&inode->i_lock);
1354 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1359 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1362 if (how & FLUSH_INVALIDATE) {
1363 spin_unlock(&inode->i_lock);
1364 nfs_cancel_commit_list(&head);
1366 spin_lock(&inode->i_lock);
1369 pages += nfs_scan_commit(inode, &head, 0, 0);
1370 spin_unlock(&inode->i_lock);
1371 ret = nfs_commit_list(inode, &head, how);
1372 spin_lock(&inode->i_lock);
1375 spin_unlock(&inode->i_lock);
1379 static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
1383 ret = nfs_writepages(mapping, wbc);
1386 ret = nfs_sync_mapping_wait(mapping, wbc, how);
1391 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1395 /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
1396 static int nfs_write_mapping(struct address_space *mapping, int how)
1398 struct writeback_control wbc = {
1399 .bdi = mapping->backing_dev_info,
1400 .sync_mode = WB_SYNC_NONE,
1401 .nr_to_write = LONG_MAX,
1402 .for_writepages = 1,
1407 ret = __nfs_write_mapping(mapping, &wbc, how);
1410 wbc.sync_mode = WB_SYNC_ALL;
1411 return __nfs_write_mapping(mapping, &wbc, how);
1415 * flush the inode to disk.
1417 int nfs_wb_all(struct inode *inode)
1419 return nfs_write_mapping(inode->i_mapping, 0);
1422 int nfs_wb_nocommit(struct inode *inode)
1424 return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
1427 int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1429 struct nfs_page *req;
1430 loff_t range_start = page_offset(page);
1431 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1432 struct writeback_control wbc = {
1433 .bdi = page->mapping->backing_dev_info,
1434 .sync_mode = WB_SYNC_ALL,
1435 .nr_to_write = LONG_MAX,
1436 .range_start = range_start,
1437 .range_end = range_end,
1441 BUG_ON(!PageLocked(page));
1443 req = nfs_page_find_request(page);
1446 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1447 nfs_release_request(req);
1450 if (nfs_lock_request_dontget(req)) {
1451 nfs_inode_remove_request(req);
1453 * In case nfs_inode_remove_request has marked the
1454 * page as being dirty
1456 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1457 nfs_unlock_request(req);
1460 ret = nfs_wait_on_request(req);
1464 if (!PagePrivate(page))
1466 ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
1471 static int nfs_wb_page_priority(struct inode *inode, struct page *page,
1474 loff_t range_start = page_offset(page);
1475 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1476 struct writeback_control wbc = {
1477 .bdi = page->mapping->backing_dev_info,
1478 .sync_mode = WB_SYNC_ALL,
1479 .nr_to_write = LONG_MAX,
1480 .range_start = range_start,
1481 .range_end = range_end,
1485 BUG_ON(!PageLocked(page));
1486 if (clear_page_dirty_for_io(page)) {
1487 ret = nfs_writepage_locked(page, &wbc);
1491 if (!PagePrivate(page))
1493 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1497 __mark_inode_dirty(inode, I_DIRTY_PAGES);
1502 * Write back all requests on one page - we do this before reading it.
1504 int nfs_wb_page(struct inode *inode, struct page* page)
1506 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
1509 int __init nfs_init_writepagecache(void)
1511 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1512 sizeof(struct nfs_write_data),
1513 0, SLAB_HWCACHE_ALIGN,
1515 if (nfs_wdata_cachep == NULL)
1518 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1520 if (nfs_wdata_mempool == NULL)
1523 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1525 if (nfs_commit_mempool == NULL)
1529 * NFS congestion size, scale with available memory.
1541 * This allows larger machines to have larger/more transfers.
1542 * Limit the default to 256M
1544 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1545 if (nfs_congestion_kb > 256*1024)
1546 nfs_congestion_kb = 256*1024;
1551 void nfs_destroy_writepagecache(void)
1553 mempool_destroy(nfs_commit_mempool);
1554 mempool_destroy(nfs_wdata_mempool);
1555 kmem_cache_destroy(nfs_wdata_cachep);