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 int 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);
360 BUG_ON(error == -EEXIST);
365 if (nfs_have_delegation(inode, FMODE_WRITE))
368 SetPagePrivate(req->wb_page);
369 set_page_private(req->wb_page, (unsigned long)req);
371 kref_get(&req->wb_kref);
372 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_LOCKED);
377 * Remove a write request from an inode
379 static void nfs_inode_remove_request(struct nfs_page *req)
381 struct inode *inode = req->wb_context->path.dentry->d_inode;
382 struct nfs_inode *nfsi = NFS_I(inode);
384 BUG_ON (!NFS_WBACK_BUSY(req));
386 spin_lock(&inode->i_lock);
387 set_page_private(req->wb_page, 0);
388 ClearPagePrivate(req->wb_page);
389 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
392 spin_unlock(&inode->i_lock);
395 spin_unlock(&inode->i_lock);
396 nfs_clear_request(req);
397 nfs_release_request(req);
401 nfs_redirty_request(struct nfs_page *req)
403 __set_page_dirty_nobuffers(req->wb_page);
407 * Check if a request is dirty
410 nfs_dirty_request(struct nfs_page *req)
412 struct page *page = req->wb_page;
414 if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
416 return !PageWriteback(req->wb_page);
419 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
421 * Add a request to the inode's commit list.
424 nfs_mark_request_commit(struct nfs_page *req)
426 struct inode *inode = req->wb_context->path.dentry->d_inode;
427 struct nfs_inode *nfsi = NFS_I(inode);
429 spin_lock(&inode->i_lock);
431 set_bit(PG_NEED_COMMIT, &(req)->wb_flags);
432 radix_tree_tag_set(&nfsi->nfs_page_tree,
434 NFS_PAGE_TAG_COMMIT);
435 spin_unlock(&inode->i_lock);
436 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
437 inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
438 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
442 int nfs_write_need_commit(struct nfs_write_data *data)
444 return data->verf.committed != NFS_FILE_SYNC;
448 int nfs_reschedule_unstable_write(struct nfs_page *req)
450 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
451 nfs_mark_request_commit(req);
454 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
455 nfs_redirty_request(req);
462 nfs_mark_request_commit(struct nfs_page *req)
467 int nfs_write_need_commit(struct nfs_write_data *data)
473 int nfs_reschedule_unstable_write(struct nfs_page *req)
480 * Wait for a request to complete.
482 * Interruptible by fatal signals only.
484 static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
486 struct nfs_inode *nfsi = NFS_I(inode);
487 struct nfs_page *req;
488 pgoff_t idx_end, next;
489 unsigned int res = 0;
495 idx_end = idx_start + npages - 1;
498 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) {
499 if (req->wb_index > idx_end)
502 next = req->wb_index + 1;
503 BUG_ON(!NFS_WBACK_BUSY(req));
505 kref_get(&req->wb_kref);
506 spin_unlock(&inode->i_lock);
507 error = nfs_wait_on_request(req);
508 nfs_release_request(req);
509 spin_lock(&inode->i_lock);
517 static void nfs_cancel_commit_list(struct list_head *head)
519 struct nfs_page *req;
521 while(!list_empty(head)) {
522 req = nfs_list_entry(head->next);
523 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
524 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
526 nfs_list_remove_request(req);
527 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
528 nfs_inode_remove_request(req);
529 nfs_unlock_request(req);
533 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
535 * nfs_scan_commit - Scan an inode for commit requests
536 * @inode: NFS inode to scan
537 * @dst: destination list
538 * @idx_start: lower bound of page->index to scan.
539 * @npages: idx_start + npages sets the upper bound to scan.
541 * Moves requests from the inode's 'commit' request list.
542 * The requests are *not* checked to ensure that they form a contiguous set.
545 nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
547 struct nfs_inode *nfsi = NFS_I(inode);
550 if (nfsi->ncommit != 0) {
551 res = nfs_scan_list(nfsi, dst, idx_start, npages,
552 NFS_PAGE_TAG_COMMIT);
553 nfsi->ncommit -= res;
558 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
565 * Try to update any existing write request, or create one if there is none.
566 * In order to match, the request's credentials must match those of
567 * the calling process.
569 * Note: Should always be called with the Page Lock held!
571 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
572 struct page *page, unsigned int offset, unsigned int bytes)
574 struct address_space *mapping = page->mapping;
575 struct inode *inode = mapping->host;
576 struct nfs_page *req, *new = NULL;
579 end = offset + bytes;
582 /* Loop over all inode entries and see if we find
583 * A request for the page we wish to update
585 spin_lock(&inode->i_lock);
586 req = nfs_page_find_request_locked(page);
588 if (!nfs_set_page_tag_locked(req)) {
591 spin_unlock(&inode->i_lock);
592 error = nfs_wait_on_request(req);
593 nfs_release_request(req);
596 nfs_release_request(new);
597 return ERR_PTR(error);
601 spin_unlock(&inode->i_lock);
603 nfs_release_request(new);
609 nfs_lock_request_dontget(new);
610 error = nfs_inode_add_request(inode, new);
612 spin_unlock(&inode->i_lock);
613 nfs_unlock_request(new);
614 return ERR_PTR(error);
616 spin_unlock(&inode->i_lock);
620 spin_unlock(&inode->i_lock);
622 new = nfs_create_request(ctx, inode, page, offset, bytes);
627 /* We have a request for our page.
628 * If the creds don't match, or the
629 * page addresses don't match,
630 * tell the caller to wait on the conflicting
633 rqend = req->wb_offset + req->wb_bytes;
634 if (req->wb_context != ctx
635 || req->wb_page != page
636 || !nfs_dirty_request(req)
637 || offset > rqend || end < req->wb_offset) {
638 nfs_clear_page_tag_locked(req);
639 return ERR_PTR(-EBUSY);
642 /* Okay, the request matches. Update the region */
643 if (offset < req->wb_offset) {
644 req->wb_offset = offset;
645 req->wb_pgbase = offset;
646 req->wb_bytes = max(end, rqend) - req->wb_offset;
651 req->wb_bytes = end - req->wb_offset;
655 /* If this page might potentially be marked as up to date,
656 * then we need to zero any uninitalised data. */
657 if (req->wb_pgbase == 0 && req->wb_bytes != PAGE_CACHE_SIZE
658 && !PageUptodate(req->wb_page))
659 zero_user_segment(req->wb_page, req->wb_bytes, PAGE_CACHE_SIZE);
663 int nfs_flush_incompatible(struct file *file, struct page *page)
665 struct nfs_open_context *ctx = nfs_file_open_context(file);
666 struct nfs_page *req;
667 int do_flush, status;
669 * Look for a request corresponding to this page. If there
670 * is one, and it belongs to another file, we flush it out
671 * before we try to copy anything into the page. Do this
672 * due to the lack of an ACCESS-type call in NFSv2.
673 * Also do the same if we find a request from an existing
677 req = nfs_page_find_request(page);
680 do_flush = req->wb_page != page || req->wb_context != ctx
681 || !nfs_dirty_request(req);
682 nfs_release_request(req);
685 status = nfs_wb_page(page->mapping->host, page);
686 } while (status == 0);
691 * If the page cache is marked as unsafe or invalid, then we can't rely on
692 * the PageUptodate() flag. In this case, we will need to turn off
693 * write optimisations that depend on the page contents being correct.
695 static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
697 return PageUptodate(page) &&
698 !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
702 * Update and possibly write a cached page of an NFS file.
704 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
705 * things with a page scheduled for an RPC call (e.g. invalidate it).
707 int nfs_updatepage(struct file *file, struct page *page,
708 unsigned int offset, unsigned int count)
710 struct nfs_open_context *ctx = nfs_file_open_context(file);
711 struct inode *inode = page->mapping->host;
714 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
716 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
717 file->f_path.dentry->d_parent->d_name.name,
718 file->f_path.dentry->d_name.name, count,
719 (long long)(page_offset(page) +offset));
721 /* If we're not using byte range locks, and we know the page
722 * is up to date, it may be more efficient to extend the write
723 * to cover the entire page in order to avoid fragmentation
726 if (nfs_write_pageuptodate(page, inode) &&
727 inode->i_flock == NULL &&
728 !(file->f_flags & O_SYNC)) {
729 count = max(count + offset, nfs_page_length(page));
733 status = nfs_writepage_setup(ctx, page, offset, count);
734 __set_page_dirty_nobuffers(page);
736 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
737 status, (long long)i_size_read(inode));
739 nfs_set_pageerror(page);
743 static void nfs_writepage_release(struct nfs_page *req)
746 if (PageError(req->wb_page)) {
747 nfs_end_page_writeback(req->wb_page);
748 nfs_inode_remove_request(req);
749 } else if (!nfs_reschedule_unstable_write(req)) {
750 /* Set the PG_uptodate flag */
751 nfs_mark_uptodate(req->wb_page, req->wb_pgbase, req->wb_bytes);
752 nfs_end_page_writeback(req->wb_page);
753 nfs_inode_remove_request(req);
755 nfs_end_page_writeback(req->wb_page);
756 nfs_clear_page_tag_locked(req);
759 static int flush_task_priority(int how)
761 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
763 return RPC_PRIORITY_HIGH;
765 return RPC_PRIORITY_LOW;
767 return RPC_PRIORITY_NORMAL;
771 * Set up the argument/result storage required for the RPC call.
773 static void nfs_write_rpcsetup(struct nfs_page *req,
774 struct nfs_write_data *data,
775 const struct rpc_call_ops *call_ops,
776 unsigned int count, unsigned int offset,
779 struct inode *inode = req->wb_context->path.dentry->d_inode;
780 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
781 int priority = flush_task_priority(how);
782 struct rpc_task *task;
783 struct rpc_message msg = {
784 .rpc_argp = &data->args,
785 .rpc_resp = &data->res,
786 .rpc_cred = req->wb_context->cred,
788 struct rpc_task_setup task_setup_data = {
789 .rpc_client = NFS_CLIENT(inode),
792 .callback_ops = call_ops,
793 .callback_data = data,
794 .workqueue = nfsiod_workqueue,
796 .priority = priority,
799 /* Set up the RPC argument and reply structs
800 * NB: take care not to mess about with data->commit et al. */
803 data->inode = inode = req->wb_context->path.dentry->d_inode;
804 data->cred = msg.rpc_cred;
806 data->args.fh = NFS_FH(inode);
807 data->args.offset = req_offset(req) + offset;
808 data->args.pgbase = req->wb_pgbase + offset;
809 data->args.pages = data->pagevec;
810 data->args.count = count;
811 data->args.context = get_nfs_open_context(req->wb_context);
812 data->args.stable = NFS_UNSTABLE;
813 if (how & FLUSH_STABLE) {
814 data->args.stable = NFS_DATA_SYNC;
815 if (!NFS_I(inode)->ncommit)
816 data->args.stable = NFS_FILE_SYNC;
819 data->res.fattr = &data->fattr;
820 data->res.count = count;
821 data->res.verf = &data->verf;
822 nfs_fattr_init(&data->fattr);
824 /* Set up the initial task struct. */
825 NFS_PROTO(inode)->write_setup(data, &msg);
827 dprintk("NFS: %5u initiated write call "
828 "(req %s/%Ld, %u bytes @ offset %Lu)\n",
831 (long long)NFS_FILEID(inode),
833 (unsigned long long)data->args.offset);
835 task = rpc_run_task(&task_setup_data);
841 * Generate multiple small requests to write out a single
842 * contiguous dirty area on one page.
844 static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
846 struct nfs_page *req = nfs_list_entry(head->next);
847 struct page *page = req->wb_page;
848 struct nfs_write_data *data;
849 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
854 nfs_list_remove_request(req);
858 size_t len = min(nbytes, wsize);
860 data = nfs_writedata_alloc(1);
863 list_add(&data->pages, &list);
866 } while (nbytes != 0);
867 atomic_set(&req->wb_complete, requests);
869 ClearPageError(page);
873 data = list_entry(list.next, struct nfs_write_data, pages);
874 list_del_init(&data->pages);
876 data->pagevec[0] = page;
880 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
884 } while (nbytes != 0);
889 while (!list_empty(&list)) {
890 data = list_entry(list.next, struct nfs_write_data, pages);
891 list_del(&data->pages);
892 nfs_writedata_release(data);
894 nfs_redirty_request(req);
895 nfs_end_page_writeback(req->wb_page);
896 nfs_clear_page_tag_locked(req);
901 * Create an RPC task for the given write request and kick it.
902 * The page must have been locked by the caller.
904 * It may happen that the page we're passed is not marked dirty.
905 * This is the case if nfs_updatepage detects a conflicting request
906 * that has been written but not committed.
908 static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
910 struct nfs_page *req;
912 struct nfs_write_data *data;
914 data = nfs_writedata_alloc(npages);
918 pages = data->pagevec;
919 while (!list_empty(head)) {
920 req = nfs_list_entry(head->next);
921 nfs_list_remove_request(req);
922 nfs_list_add_request(req, &data->pages);
923 ClearPageError(req->wb_page);
924 *pages++ = req->wb_page;
926 req = nfs_list_entry(data->pages.next);
928 /* Set up the argument struct */
929 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
933 while (!list_empty(head)) {
934 req = nfs_list_entry(head->next);
935 nfs_list_remove_request(req);
936 nfs_redirty_request(req);
937 nfs_end_page_writeback(req->wb_page);
938 nfs_clear_page_tag_locked(req);
943 static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
944 struct inode *inode, int ioflags)
946 size_t wsize = NFS_SERVER(inode)->wsize;
948 if (wsize < PAGE_CACHE_SIZE)
949 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
951 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
955 * Handle a write reply that flushed part of a page.
957 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
959 struct nfs_write_data *data = calldata;
960 struct nfs_page *req = data->req;
961 struct page *page = req->wb_page;
963 dprintk("NFS: write (%s/%Ld %d@%Ld)",
964 req->wb_context->path.dentry->d_inode->i_sb->s_id,
965 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
967 (long long)req_offset(req));
969 if (nfs_writeback_done(task, data) != 0)
972 if (task->tk_status < 0) {
973 nfs_set_pageerror(page);
974 nfs_context_set_write_error(req->wb_context, task->tk_status);
975 dprintk(", error = %d\n", task->tk_status);
979 if (nfs_write_need_commit(data)) {
980 struct inode *inode = page->mapping->host;
982 spin_lock(&inode->i_lock);
983 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
984 /* Do nothing we need to resend the writes */
985 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
986 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
987 dprintk(" defer commit\n");
988 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
989 set_bit(PG_NEED_RESCHED, &req->wb_flags);
990 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
991 dprintk(" server reboot detected\n");
993 spin_unlock(&inode->i_lock);
998 if (atomic_dec_and_test(&req->wb_complete))
999 nfs_writepage_release(req);
1002 static const struct rpc_call_ops nfs_write_partial_ops = {
1003 .rpc_call_done = nfs_writeback_done_partial,
1004 .rpc_release = nfs_writedata_release,
1008 * Handle a write reply that flushes a whole page.
1010 * FIXME: There is an inherent race with invalidate_inode_pages and
1011 * writebacks since the page->count is kept > 1 for as long
1012 * as the page has a write request pending.
1014 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1016 struct nfs_write_data *data = calldata;
1017 struct nfs_page *req;
1020 if (nfs_writeback_done(task, data) != 0)
1023 /* Update attributes as result of writeback. */
1024 while (!list_empty(&data->pages)) {
1025 req = nfs_list_entry(data->pages.next);
1026 nfs_list_remove_request(req);
1027 page = req->wb_page;
1029 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1030 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1031 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1033 (long long)req_offset(req));
1035 if (task->tk_status < 0) {
1036 nfs_set_pageerror(page);
1037 nfs_context_set_write_error(req->wb_context, task->tk_status);
1038 dprintk(", error = %d\n", task->tk_status);
1039 goto remove_request;
1042 if (nfs_write_need_commit(data)) {
1043 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1044 nfs_mark_request_commit(req);
1045 nfs_end_page_writeback(page);
1046 dprintk(" marked for commit\n");
1049 /* Set the PG_uptodate flag? */
1050 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
1053 nfs_end_page_writeback(page);
1054 nfs_inode_remove_request(req);
1056 nfs_clear_page_tag_locked(req);
1060 static const struct rpc_call_ops nfs_write_full_ops = {
1061 .rpc_call_done = nfs_writeback_done_full,
1062 .rpc_release = nfs_writedata_release,
1067 * This function is called when the WRITE call is complete.
1069 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1071 struct nfs_writeargs *argp = &data->args;
1072 struct nfs_writeres *resp = &data->res;
1075 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1076 task->tk_pid, task->tk_status);
1079 * ->write_done will attempt to use post-op attributes to detect
1080 * conflicting writes by other clients. A strict interpretation
1081 * of close-to-open would allow us to continue caching even if
1082 * another writer had changed the file, but some applications
1083 * depend on tighter cache coherency when writing.
1085 status = NFS_PROTO(data->inode)->write_done(task, data);
1088 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1090 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1091 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1092 /* We tried a write call, but the server did not
1093 * commit data to stable storage even though we
1095 * Note: There is a known bug in Tru64 < 5.0 in which
1096 * the server reports NFS_DATA_SYNC, but performs
1097 * NFS_FILE_SYNC. We therefore implement this checking
1098 * as a dprintk() in order to avoid filling syslog.
1100 static unsigned long complain;
1102 if (time_before(complain, jiffies)) {
1103 dprintk("NFS: faulty NFS server %s:"
1104 " (committed = %d) != (stable = %d)\n",
1105 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1106 resp->verf->committed, argp->stable);
1107 complain = jiffies + 300 * HZ;
1111 /* Is this a short write? */
1112 if (task->tk_status >= 0 && resp->count < argp->count) {
1113 static unsigned long complain;
1115 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1117 /* Has the server at least made some progress? */
1118 if (resp->count != 0) {
1119 /* Was this an NFSv2 write or an NFSv3 stable write? */
1120 if (resp->verf->committed != NFS_UNSTABLE) {
1121 /* Resend from where the server left off */
1122 argp->offset += resp->count;
1123 argp->pgbase += resp->count;
1124 argp->count -= resp->count;
1126 /* Resend as a stable write in order to avoid
1127 * headaches in the case of a server crash.
1129 argp->stable = NFS_FILE_SYNC;
1131 rpc_restart_call(task);
1134 if (time_before(complain, jiffies)) {
1136 "NFS: Server wrote zero bytes, expected %u.\n",
1138 complain = jiffies + 300 * HZ;
1140 /* Can't do anything about it except throw an error. */
1141 task->tk_status = -EIO;
1147 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1148 void nfs_commit_release(void *data)
1150 struct nfs_write_data *wdata = data;
1152 put_nfs_open_context(wdata->args.context);
1153 nfs_commit_free(wdata);
1157 * Set up the argument/result storage required for the RPC call.
1159 static void nfs_commit_rpcsetup(struct list_head *head,
1160 struct nfs_write_data *data,
1163 struct nfs_page *first = nfs_list_entry(head->next);
1164 struct inode *inode = first->wb_context->path.dentry->d_inode;
1165 int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1166 int priority = flush_task_priority(how);
1167 struct rpc_task *task;
1168 struct rpc_message msg = {
1169 .rpc_argp = &data->args,
1170 .rpc_resp = &data->res,
1171 .rpc_cred = first->wb_context->cred,
1173 struct rpc_task_setup task_setup_data = {
1174 .task = &data->task,
1175 .rpc_client = NFS_CLIENT(inode),
1176 .rpc_message = &msg,
1177 .callback_ops = &nfs_commit_ops,
1178 .callback_data = data,
1179 .workqueue = nfsiod_workqueue,
1181 .priority = priority,
1184 /* Set up the RPC argument and reply structs
1185 * NB: take care not to mess about with data->commit et al. */
1187 list_splice_init(head, &data->pages);
1189 data->inode = inode;
1190 data->cred = msg.rpc_cred;
1192 data->args.fh = NFS_FH(data->inode);
1193 /* Note: we always request a commit of the entire inode */
1194 data->args.offset = 0;
1195 data->args.count = 0;
1196 data->args.context = get_nfs_open_context(first->wb_context);
1197 data->res.count = 0;
1198 data->res.fattr = &data->fattr;
1199 data->res.verf = &data->verf;
1200 nfs_fattr_init(&data->fattr);
1202 /* Set up the initial task struct. */
1203 NFS_PROTO(inode)->commit_setup(data, &msg);
1205 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1207 task = rpc_run_task(&task_setup_data);
1213 * Commit dirty pages
1216 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1218 struct nfs_write_data *data;
1219 struct nfs_page *req;
1221 data = nfs_commit_alloc();
1226 /* Set up the argument struct */
1227 nfs_commit_rpcsetup(head, data, how);
1231 while (!list_empty(head)) {
1232 req = nfs_list_entry(head->next);
1233 nfs_list_remove_request(req);
1234 nfs_mark_request_commit(req);
1235 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1236 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1238 nfs_clear_page_tag_locked(req);
1244 * COMMIT call returned
1246 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1248 struct nfs_write_data *data = calldata;
1249 struct nfs_page *req;
1251 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1252 task->tk_pid, task->tk_status);
1254 /* Call the NFS version-specific code */
1255 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1258 while (!list_empty(&data->pages)) {
1259 req = nfs_list_entry(data->pages.next);
1260 nfs_list_remove_request(req);
1261 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
1262 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1263 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1266 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1267 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1268 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1270 (long long)req_offset(req));
1271 if (task->tk_status < 0) {
1272 nfs_context_set_write_error(req->wb_context, task->tk_status);
1273 nfs_inode_remove_request(req);
1274 dprintk(", error = %d\n", task->tk_status);
1278 /* Okay, COMMIT succeeded, apparently. Check the verifier
1279 * returned by the server against all stored verfs. */
1280 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1281 /* We have a match */
1282 /* Set the PG_uptodate flag */
1283 nfs_mark_uptodate(req->wb_page, req->wb_pgbase,
1285 nfs_inode_remove_request(req);
1289 /* We have a mismatch. Write the page again */
1290 dprintk(" mismatch\n");
1291 nfs_redirty_request(req);
1293 nfs_clear_page_tag_locked(req);
1297 static const struct rpc_call_ops nfs_commit_ops = {
1298 .rpc_call_done = nfs_commit_done,
1299 .rpc_release = nfs_commit_release,
1302 int nfs_commit_inode(struct inode *inode, int how)
1307 spin_lock(&inode->i_lock);
1308 res = nfs_scan_commit(inode, &head, 0, 0);
1309 spin_unlock(&inode->i_lock);
1311 int error = nfs_commit_list(inode, &head, how);
1318 static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1324 long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
1326 struct inode *inode = mapping->host;
1327 pgoff_t idx_start, idx_end;
1328 unsigned int npages = 0;
1330 int nocommit = how & FLUSH_NOCOMMIT;
1334 if (wbc->range_cyclic)
1337 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1338 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1339 if (idx_end > idx_start) {
1340 pgoff_t l_npages = 1 + idx_end - idx_start;
1342 if (sizeof(npages) != sizeof(l_npages) &&
1343 (pgoff_t)npages != l_npages)
1347 how &= ~FLUSH_NOCOMMIT;
1348 spin_lock(&inode->i_lock);
1350 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1355 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1358 if (how & FLUSH_INVALIDATE) {
1359 spin_unlock(&inode->i_lock);
1360 nfs_cancel_commit_list(&head);
1362 spin_lock(&inode->i_lock);
1365 pages += nfs_scan_commit(inode, &head, 0, 0);
1366 spin_unlock(&inode->i_lock);
1367 ret = nfs_commit_list(inode, &head, how);
1368 spin_lock(&inode->i_lock);
1371 spin_unlock(&inode->i_lock);
1375 static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
1379 ret = nfs_writepages(mapping, wbc);
1382 ret = nfs_sync_mapping_wait(mapping, wbc, how);
1387 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1391 /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
1392 static int nfs_write_mapping(struct address_space *mapping, int how)
1394 struct writeback_control wbc = {
1395 .bdi = mapping->backing_dev_info,
1396 .sync_mode = WB_SYNC_NONE,
1397 .nr_to_write = LONG_MAX,
1398 .for_writepages = 1,
1403 ret = __nfs_write_mapping(mapping, &wbc, how);
1406 wbc.sync_mode = WB_SYNC_ALL;
1407 return __nfs_write_mapping(mapping, &wbc, how);
1411 * flush the inode to disk.
1413 int nfs_wb_all(struct inode *inode)
1415 return nfs_write_mapping(inode->i_mapping, 0);
1418 int nfs_wb_nocommit(struct inode *inode)
1420 return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
1423 int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1425 struct nfs_page *req;
1426 loff_t range_start = page_offset(page);
1427 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1428 struct writeback_control wbc = {
1429 .bdi = page->mapping->backing_dev_info,
1430 .sync_mode = WB_SYNC_ALL,
1431 .nr_to_write = LONG_MAX,
1432 .range_start = range_start,
1433 .range_end = range_end,
1437 BUG_ON(!PageLocked(page));
1439 req = nfs_page_find_request(page);
1442 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1443 nfs_release_request(req);
1446 if (nfs_lock_request_dontget(req)) {
1447 nfs_inode_remove_request(req);
1449 * In case nfs_inode_remove_request has marked the
1450 * page as being dirty
1452 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1453 nfs_unlock_request(req);
1456 ret = nfs_wait_on_request(req);
1460 if (!PagePrivate(page))
1462 ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
1467 static int nfs_wb_page_priority(struct inode *inode, struct page *page,
1470 loff_t range_start = page_offset(page);
1471 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1472 struct writeback_control wbc = {
1473 .bdi = page->mapping->backing_dev_info,
1474 .sync_mode = WB_SYNC_ALL,
1475 .nr_to_write = LONG_MAX,
1476 .range_start = range_start,
1477 .range_end = range_end,
1481 BUG_ON(!PageLocked(page));
1482 if (clear_page_dirty_for_io(page)) {
1483 ret = nfs_writepage_locked(page, &wbc);
1487 if (!PagePrivate(page))
1489 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1493 __mark_inode_dirty(inode, I_DIRTY_PAGES);
1498 * Write back all requests on one page - we do this before reading it.
1500 int nfs_wb_page(struct inode *inode, struct page* page)
1502 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
1505 int __init nfs_init_writepagecache(void)
1507 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1508 sizeof(struct nfs_write_data),
1509 0, SLAB_HWCACHE_ALIGN,
1511 if (nfs_wdata_cachep == NULL)
1514 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1516 if (nfs_wdata_mempool == NULL)
1519 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1521 if (nfs_commit_mempool == NULL)
1525 * NFS congestion size, scale with available memory.
1537 * This allows larger machines to have larger/more transfers.
1538 * Limit the default to 256M
1540 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1541 if (nfs_congestion_kb > 256*1024)
1542 nfs_congestion_kb = 256*1024;
1547 void nfs_destroy_writepagecache(void)
1549 mempool_destroy(nfs_commit_mempool);
1550 mempool_destroy(nfs_wdata_mempool);
1551 kmem_cache_destroy(nfs_wdata_cachep);