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>
16 #include <linux/sunrpc/clnt.h>
17 #include <linux/nfs_fs.h>
18 #include <linux/nfs_mount.h>
19 #include <linux/nfs_page.h>
20 #include <linux/backing-dev.h>
22 #include <asm/uaccess.h>
23 #include <linux/smp_lock.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_mark_request_dirty(struct nfs_page *req);
41 static int nfs_wait_on_write_congestion(struct address_space *, int);
42 static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how);
43 static const struct rpc_call_ops nfs_write_partial_ops;
44 static const struct rpc_call_ops nfs_write_full_ops;
45 static const struct rpc_call_ops nfs_commit_ops;
47 static struct kmem_cache *nfs_wdata_cachep;
48 static mempool_t *nfs_wdata_mempool;
49 static mempool_t *nfs_commit_mempool;
51 static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
53 struct nfs_write_data *nfs_commit_alloc(void)
55 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
58 memset(p, 0, sizeof(*p));
59 INIT_LIST_HEAD(&p->pages);
64 void nfs_commit_rcu_free(struct rcu_head *head)
66 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
67 if (p && (p->pagevec != &p->page_array[0]))
69 mempool_free(p, nfs_commit_mempool);
72 void nfs_commit_free(struct nfs_write_data *wdata)
74 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
77 struct nfs_write_data *nfs_writedata_alloc(size_t len)
79 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
80 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
83 memset(p, 0, sizeof(*p));
84 INIT_LIST_HEAD(&p->pages);
85 p->npages = pagecount;
86 if (pagecount <= ARRAY_SIZE(p->page_array))
87 p->pagevec = p->page_array;
89 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
91 mempool_free(p, nfs_wdata_mempool);
99 static void nfs_writedata_rcu_free(struct rcu_head *head)
101 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
102 if (p && (p->pagevec != &p->page_array[0]))
104 mempool_free(p, nfs_wdata_mempool);
107 static void nfs_writedata_free(struct nfs_write_data *wdata)
109 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
112 void nfs_writedata_release(void *wdata)
114 nfs_writedata_free(wdata);
117 static struct nfs_page *nfs_page_find_request_locked(struct page *page)
119 struct nfs_page *req = NULL;
121 if (PagePrivate(page)) {
122 req = (struct nfs_page *)page_private(page);
124 atomic_inc(&req->wb_count);
129 static struct nfs_page *nfs_page_find_request(struct page *page)
131 struct nfs_page *req = NULL;
132 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
135 req = nfs_page_find_request_locked(page);
136 spin_unlock(req_lock);
140 /* Adjust the file length if we're writing beyond the end */
141 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
143 struct inode *inode = page->mapping->host;
144 loff_t end, i_size = i_size_read(inode);
145 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
147 if (i_size > 0 && page->index < end_index)
149 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
152 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
153 i_size_write(inode, end);
156 /* We can set the PG_uptodate flag if we see that a write request
157 * covers the full page.
159 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
161 if (PageUptodate(page))
165 if (count != nfs_page_length(page))
167 if (count != PAGE_CACHE_SIZE)
168 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
169 SetPageUptodate(page);
172 static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
173 unsigned int offset, unsigned int count)
175 struct nfs_page *req;
179 req = nfs_update_request(ctx, page, offset, count);
185 ret = nfs_wb_page(page->mapping->host, page);
189 /* Update file length */
190 nfs_grow_file(page, offset, count);
191 /* Set the PG_uptodate flag? */
192 nfs_mark_uptodate(page, offset, count);
193 nfs_unlock_request(req);
197 static int wb_priority(struct writeback_control *wbc)
199 if (wbc->for_reclaim)
200 return FLUSH_HIGHPRI;
201 if (wbc->for_kupdate)
207 * Find an associated nfs write request, and prepare to flush it out
208 * Returns 1 if there was no write request, or if the request was
209 * already tagged by nfs_set_page_dirty.Returns 0 if the request
211 * May also return an error if the user signalled nfs_wait_on_request().
213 static int nfs_page_mark_flush(struct page *page)
215 struct nfs_page *req;
216 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
221 req = nfs_page_find_request_locked(page);
223 spin_unlock(req_lock);
226 if (nfs_lock_request_dontget(req))
228 /* Note: If we hold the page lock, as is the case in nfs_writepage,
229 * then the call to nfs_lock_request_dontget() will always
230 * succeed provided that someone hasn't already marked the
231 * request as dirty (in which case we don't care).
233 spin_unlock(req_lock);
234 ret = nfs_wait_on_request(req);
235 nfs_release_request(req);
240 spin_unlock(req_lock);
241 if (test_and_set_bit(PG_FLUSHING, &req->wb_flags) == 0) {
242 nfs_mark_request_dirty(req);
243 set_page_writeback(page);
245 ret = test_bit(PG_NEED_FLUSH, &req->wb_flags);
246 nfs_unlock_request(req);
251 * Write an mmapped page to the server.
253 static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
255 struct nfs_open_context *ctx;
256 struct inode *inode = page->mapping->host;
260 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
261 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
263 err = nfs_page_mark_flush(page);
267 offset = nfs_page_length(page);
271 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
276 err = nfs_writepage_setup(ctx, page, 0, offset);
277 put_nfs_open_context(ctx);
280 err = nfs_page_mark_flush(page);
284 if (!wbc->for_writepages)
285 nfs_flush_mapping(page->mapping, wbc, FLUSH_STABLE|wb_priority(wbc));
289 int nfs_writepage(struct page *page, struct writeback_control *wbc)
293 err = nfs_writepage_locked(page, wbc);
299 * Note: causes nfs_update_request() to block on the assumption
300 * that the writeback is generated due to memory pressure.
302 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
304 struct backing_dev_info *bdi = mapping->backing_dev_info;
305 struct inode *inode = mapping->host;
308 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
310 err = generic_writepages(mapping, wbc);
313 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
314 if (wbc->nonblocking)
316 nfs_wait_on_write_congestion(mapping, 0);
318 err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
321 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
324 clear_bit(BDI_write_congested, &bdi->state);
325 wake_up_all(&nfs_write_congestion);
326 congestion_end(WRITE);
331 * Insert a write request into an inode
333 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
335 struct nfs_inode *nfsi = NFS_I(inode);
338 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
339 BUG_ON(error == -EEXIST);
344 nfs_begin_data_update(inode);
345 if (nfs_have_delegation(inode, FMODE_WRITE))
348 SetPagePrivate(req->wb_page);
349 set_page_private(req->wb_page, (unsigned long)req);
351 atomic_inc(&req->wb_count);
356 * Insert a write request into an inode
358 static void nfs_inode_remove_request(struct nfs_page *req)
360 struct inode *inode = req->wb_context->dentry->d_inode;
361 struct nfs_inode *nfsi = NFS_I(inode);
363 BUG_ON (!NFS_WBACK_BUSY(req));
365 spin_lock(&nfsi->req_lock);
366 set_page_private(req->wb_page, 0);
367 ClearPagePrivate(req->wb_page);
368 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
371 spin_unlock(&nfsi->req_lock);
372 nfs_end_data_update(inode);
375 spin_unlock(&nfsi->req_lock);
376 nfs_clear_request(req);
377 nfs_release_request(req);
381 * Add a request to the inode's dirty list.
384 nfs_mark_request_dirty(struct nfs_page *req)
386 struct inode *inode = req->wb_context->dentry->d_inode;
387 struct nfs_inode *nfsi = NFS_I(inode);
389 spin_lock(&nfsi->req_lock);
390 radix_tree_tag_set(&nfsi->nfs_page_tree,
391 req->wb_index, NFS_PAGE_TAG_DIRTY);
392 nfs_list_add_request(req, &nfsi->dirty);
394 spin_unlock(&nfsi->req_lock);
395 __mark_inode_dirty(inode, I_DIRTY_PAGES);
399 nfs_redirty_request(struct nfs_page *req)
401 clear_bit(PG_FLUSHING, &req->wb_flags);
402 __set_page_dirty_nobuffers(req->wb_page);
406 * Check if a request is dirty
409 nfs_dirty_request(struct nfs_page *req)
411 return test_bit(PG_FLUSHING, &req->wb_flags) == 0;
414 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
416 * Add a request to the inode's commit list.
419 nfs_mark_request_commit(struct nfs_page *req)
421 struct inode *inode = req->wb_context->dentry->d_inode;
422 struct nfs_inode *nfsi = NFS_I(inode);
424 spin_lock(&nfsi->req_lock);
425 nfs_list_add_request(req, &nfsi->commit);
427 spin_unlock(&nfsi->req_lock);
428 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
429 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
434 * Wait for a request to complete.
436 * Interruptible by signals only if mounted with intr flag.
438 static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
440 struct nfs_inode *nfsi = NFS_I(inode);
441 struct nfs_page *req;
442 unsigned long idx_end, next;
443 unsigned int res = 0;
449 idx_end = idx_start + npages - 1;
452 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
453 if (req->wb_index > idx_end)
456 next = req->wb_index + 1;
457 BUG_ON(!NFS_WBACK_BUSY(req));
459 atomic_inc(&req->wb_count);
460 spin_unlock(&nfsi->req_lock);
461 error = nfs_wait_on_request(req);
462 nfs_release_request(req);
463 spin_lock(&nfsi->req_lock);
471 static void nfs_cancel_dirty_list(struct list_head *head)
473 struct nfs_page *req;
474 while(!list_empty(head)) {
475 req = nfs_list_entry(head->next);
476 nfs_list_remove_request(req);
477 nfs_inode_remove_request(req);
478 nfs_clear_page_writeback(req);
482 static void nfs_cancel_commit_list(struct list_head *head)
484 struct nfs_page *req;
486 while(!list_empty(head)) {
487 req = nfs_list_entry(head->next);
488 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
489 nfs_list_remove_request(req);
490 nfs_inode_remove_request(req);
491 nfs_unlock_request(req);
495 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
497 * nfs_scan_commit - Scan an inode for commit requests
498 * @inode: NFS inode to scan
499 * @dst: destination list
500 * @idx_start: lower bound of page->index to scan.
501 * @npages: idx_start + npages sets the upper bound to scan.
503 * Moves requests from the inode's 'commit' request list.
504 * The requests are *not* checked to ensure that they form a contiguous set.
507 nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
509 struct nfs_inode *nfsi = NFS_I(inode);
512 if (nfsi->ncommit != 0) {
513 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
514 nfsi->ncommit -= res;
515 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
516 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
521 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
527 static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
529 struct backing_dev_info *bdi = mapping->backing_dev_info;
535 if (!bdi_write_congested(bdi))
538 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
541 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
544 rpc_clnt_sigmask(clnt, &oldset);
545 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
546 if (bdi_write_congested(bdi)) {
552 rpc_clnt_sigunmask(clnt, &oldset);
554 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
555 if (bdi_write_congested(bdi))
558 finish_wait(&nfs_write_congestion, &wait);
564 * Try to update any existing write request, or create one if there is none.
565 * In order to match, the request's credentials must match those of
566 * the calling process.
568 * Note: Should always be called with the Page Lock held!
570 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
571 struct page *page, unsigned int offset, unsigned int bytes)
573 struct inode *inode = page->mapping->host;
574 struct nfs_inode *nfsi = NFS_I(inode);
575 struct nfs_page *req, *new = NULL;
576 unsigned long rqend, end;
578 end = offset + bytes;
580 if (nfs_wait_on_write_congestion(page->mapping, NFS_SERVER(inode)->flags & NFS_MOUNT_INTR))
581 return ERR_PTR(-ERESTARTSYS);
583 /* Loop over all inode entries and see if we find
584 * A request for the page we wish to update
586 spin_lock(&nfsi->req_lock);
587 req = nfs_page_find_request_locked(page);
589 if (!nfs_lock_request_dontget(req)) {
592 spin_unlock(&nfsi->req_lock);
593 error = nfs_wait_on_request(req);
594 nfs_release_request(req);
597 nfs_release_request(new);
598 return ERR_PTR(error);
602 spin_unlock(&nfsi->req_lock);
604 nfs_release_request(new);
610 nfs_lock_request_dontget(new);
611 error = nfs_inode_add_request(inode, new);
613 spin_unlock(&nfsi->req_lock);
614 nfs_unlock_request(new);
615 return ERR_PTR(error);
617 spin_unlock(&nfsi->req_lock);
620 spin_unlock(&nfsi->req_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_unlock_request(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 = rqend - req->wb_offset;
650 req->wb_bytes = end - req->wb_offset;
655 int nfs_flush_incompatible(struct file *file, struct page *page)
657 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
658 struct nfs_page *req;
659 int do_flush, status;
661 * Look for a request corresponding to this page. If there
662 * is one, and it belongs to another file, we flush it out
663 * before we try to copy anything into the page. Do this
664 * due to the lack of an ACCESS-type call in NFSv2.
665 * Also do the same if we find a request from an existing
669 req = nfs_page_find_request(page);
672 do_flush = req->wb_page != page || req->wb_context != ctx
673 || !nfs_dirty_request(req);
674 nfs_release_request(req);
677 status = nfs_wb_page(page->mapping->host, page);
678 } while (status == 0);
683 * Update and possibly write a cached page of an NFS file.
685 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
686 * things with a page scheduled for an RPC call (e.g. invalidate it).
688 int nfs_updatepage(struct file *file, struct page *page,
689 unsigned int offset, unsigned int count)
691 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
692 struct inode *inode = page->mapping->host;
695 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
697 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
698 file->f_path.dentry->d_parent->d_name.name,
699 file->f_path.dentry->d_name.name, count,
700 (long long)(page_offset(page) +offset));
702 /* If we're not using byte range locks, and we know the page
703 * is entirely in cache, it may be more efficient to avoid
704 * fragmenting write requests.
706 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
707 count = max(count + offset, nfs_page_length(page));
711 status = nfs_writepage_setup(ctx, page, offset, count);
712 __set_page_dirty_nobuffers(page);
714 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
715 status, (long long)i_size_read(inode));
717 ClearPageUptodate(page);
721 static void nfs_writepage_release(struct nfs_page *req)
723 end_page_writeback(req->wb_page);
725 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
726 if (!PageError(req->wb_page)) {
727 if (NFS_NEED_RESCHED(req)) {
728 nfs_redirty_request(req);
730 } else if (NFS_NEED_COMMIT(req)) {
731 nfs_mark_request_commit(req);
735 nfs_inode_remove_request(req);
738 nfs_clear_commit(req);
739 nfs_clear_reschedule(req);
741 nfs_inode_remove_request(req);
743 nfs_clear_page_writeback(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->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: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
799 (long long)NFS_FILEID(inode),
801 (unsigned long long)data->args.offset);
804 static void nfs_execute_write(struct nfs_write_data *data)
806 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
809 rpc_clnt_sigmask(clnt, &oldset);
810 rpc_execute(&data->task);
811 rpc_clnt_sigunmask(clnt, &oldset);
815 * Generate multiple small requests to write out a single
816 * contiguous dirty area on one page.
818 static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
820 struct nfs_page *req = nfs_list_entry(head->next);
821 struct page *page = req->wb_page;
822 struct nfs_write_data *data;
823 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
828 nfs_list_remove_request(req);
830 nbytes = req->wb_bytes;
832 size_t len = min(nbytes, wsize);
834 data = nfs_writedata_alloc(len);
837 list_add(&data->pages, &list);
840 } while (nbytes != 0);
841 atomic_set(&req->wb_complete, requests);
843 ClearPageError(page);
845 nbytes = req->wb_bytes;
847 data = list_entry(list.next, struct nfs_write_data, pages);
848 list_del_init(&data->pages);
850 data->pagevec[0] = page;
852 if (nbytes > wsize) {
853 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
858 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
859 nbytes, offset, how);
862 nfs_execute_write(data);
863 } while (nbytes != 0);
868 while (!list_empty(&list)) {
869 data = list_entry(list.next, struct nfs_write_data, pages);
870 list_del(&data->pages);
871 nfs_writedata_release(data);
873 nfs_redirty_request(req);
874 nfs_clear_page_writeback(req);
879 * Create an RPC task for the given write request and kick it.
880 * The page must have been locked by the caller.
882 * It may happen that the page we're passed is not marked dirty.
883 * This is the case if nfs_updatepage detects a conflicting request
884 * that has been written but not committed.
886 static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
888 struct nfs_page *req;
890 struct nfs_write_data *data;
893 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
897 pages = data->pagevec;
899 while (!list_empty(head)) {
900 req = nfs_list_entry(head->next);
901 nfs_list_remove_request(req);
902 nfs_list_add_request(req, &data->pages);
903 ClearPageError(req->wb_page);
904 *pages++ = req->wb_page;
905 count += req->wb_bytes;
907 req = nfs_list_entry(data->pages.next);
909 /* Set up the argument struct */
910 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
912 nfs_execute_write(data);
915 while (!list_empty(head)) {
916 struct nfs_page *req = nfs_list_entry(head->next);
917 nfs_list_remove_request(req);
918 nfs_redirty_request(req);
919 nfs_clear_page_writeback(req);
924 static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
926 LIST_HEAD(one_request);
927 int (*flush_one)(struct inode *, struct list_head *, int);
928 struct nfs_page *req;
929 int wpages = NFS_SERVER(inode)->wpages;
930 int wsize = NFS_SERVER(inode)->wsize;
933 flush_one = nfs_flush_one;
934 if (wsize < PAGE_CACHE_SIZE)
935 flush_one = nfs_flush_multi;
936 /* For single writes, FLUSH_STABLE is more efficient */
937 if (npages <= wpages && npages == NFS_I(inode)->npages
938 && nfs_list_entry(head->next)->wb_bytes <= wsize)
942 nfs_coalesce_requests(head, &one_request, wpages);
943 req = nfs_list_entry(one_request.next);
944 error = flush_one(inode, &one_request, how);
947 } while (!list_empty(head));
950 while (!list_empty(head)) {
951 req = nfs_list_entry(head->next);
952 nfs_list_remove_request(req);
953 nfs_redirty_request(req);
954 nfs_clear_page_writeback(req);
960 * Handle a write reply that flushed part of a page.
962 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
964 struct nfs_write_data *data = calldata;
965 struct nfs_page *req = data->req;
966 struct page *page = req->wb_page;
968 dprintk("NFS: write (%s/%Ld %d@%Ld)",
969 req->wb_context->dentry->d_inode->i_sb->s_id,
970 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
972 (long long)req_offset(req));
974 if (nfs_writeback_done(task, data) != 0)
977 if (task->tk_status < 0) {
978 ClearPageUptodate(page);
980 req->wb_context->error = task->tk_status;
981 dprintk(", error = %d\n", task->tk_status);
983 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
984 if (data->verf.committed < NFS_FILE_SYNC) {
985 if (!NFS_NEED_COMMIT(req)) {
986 nfs_defer_commit(req);
987 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
988 dprintk(" defer commit\n");
989 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
990 nfs_defer_reschedule(req);
991 dprintk(" server reboot detected\n");
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->dentry->d_inode->i_sb->s_id,
1031 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1033 (long long)req_offset(req));
1035 if (task->tk_status < 0) {
1036 ClearPageUptodate(page);
1038 req->wb_context->error = task->tk_status;
1039 end_page_writeback(page);
1040 nfs_inode_remove_request(req);
1041 dprintk(", error = %d\n", task->tk_status);
1044 end_page_writeback(page);
1046 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1047 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1048 nfs_inode_remove_request(req);
1052 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1053 nfs_mark_request_commit(req);
1054 dprintk(" marked for commit\n");
1056 nfs_inode_remove_request(req);
1059 nfs_clear_page_writeback(req);
1063 static const struct rpc_call_ops nfs_write_full_ops = {
1064 .rpc_call_done = nfs_writeback_done_full,
1065 .rpc_release = nfs_writedata_release,
1070 * This function is called when the WRITE call is complete.
1072 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1074 struct nfs_writeargs *argp = &data->args;
1075 struct nfs_writeres *resp = &data->res;
1078 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1079 task->tk_pid, task->tk_status);
1082 * ->write_done will attempt to use post-op attributes to detect
1083 * conflicting writes by other clients. A strict interpretation
1084 * of close-to-open would allow us to continue caching even if
1085 * another writer had changed the file, but some applications
1086 * depend on tighter cache coherency when writing.
1088 status = NFS_PROTO(data->inode)->write_done(task, data);
1091 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1093 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1094 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1095 /* We tried a write call, but the server did not
1096 * commit data to stable storage even though we
1098 * Note: There is a known bug in Tru64 < 5.0 in which
1099 * the server reports NFS_DATA_SYNC, but performs
1100 * NFS_FILE_SYNC. We therefore implement this checking
1101 * as a dprintk() in order to avoid filling syslog.
1103 static unsigned long complain;
1105 if (time_before(complain, jiffies)) {
1106 dprintk("NFS: faulty NFS server %s:"
1107 " (committed = %d) != (stable = %d)\n",
1108 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1109 resp->verf->committed, argp->stable);
1110 complain = jiffies + 300 * HZ;
1114 /* Is this a short write? */
1115 if (task->tk_status >= 0 && resp->count < argp->count) {
1116 static unsigned long complain;
1118 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1120 /* Has the server at least made some progress? */
1121 if (resp->count != 0) {
1122 /* Was this an NFSv2 write or an NFSv3 stable write? */
1123 if (resp->verf->committed != NFS_UNSTABLE) {
1124 /* Resend from where the server left off */
1125 argp->offset += resp->count;
1126 argp->pgbase += resp->count;
1127 argp->count -= resp->count;
1129 /* Resend as a stable write in order to avoid
1130 * headaches in the case of a server crash.
1132 argp->stable = NFS_FILE_SYNC;
1134 rpc_restart_call(task);
1137 if (time_before(complain, jiffies)) {
1139 "NFS: Server wrote zero bytes, expected %u.\n",
1141 complain = jiffies + 300 * HZ;
1143 /* Can't do anything about it except throw an error. */
1144 task->tk_status = -EIO;
1150 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1151 void nfs_commit_release(void *wdata)
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;
1164 struct inode *inode;
1167 /* Set up the RPC argument and reply structs
1168 * NB: take care not to mess about with data->commit et al. */
1170 list_splice_init(head, &data->pages);
1171 first = nfs_list_entry(data->pages.next);
1172 inode = first->wb_context->dentry->d_inode;
1174 data->inode = inode;
1175 data->cred = first->wb_context->cred;
1177 data->args.fh = NFS_FH(data->inode);
1178 /* Note: we always request a commit of the entire inode */
1179 data->args.offset = 0;
1180 data->args.count = 0;
1181 data->res.count = 0;
1182 data->res.fattr = &data->fattr;
1183 data->res.verf = &data->verf;
1184 nfs_fattr_init(&data->fattr);
1186 /* Set up the initial task struct. */
1187 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1188 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
1189 NFS_PROTO(inode)->commit_setup(data, how);
1191 data->task.tk_priority = flush_task_priority(how);
1192 data->task.tk_cookie = (unsigned long)inode;
1194 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
1198 * Commit dirty pages
1201 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1203 struct nfs_write_data *data;
1204 struct nfs_page *req;
1206 data = nfs_commit_alloc();
1211 /* Set up the argument struct */
1212 nfs_commit_rpcsetup(head, data, how);
1214 nfs_execute_write(data);
1217 while (!list_empty(head)) {
1218 req = nfs_list_entry(head->next);
1219 nfs_list_remove_request(req);
1220 nfs_mark_request_commit(req);
1221 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1222 nfs_clear_page_writeback(req);
1228 * COMMIT call returned
1230 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1232 struct nfs_write_data *data = calldata;
1233 struct nfs_page *req;
1235 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1236 task->tk_pid, task->tk_status);
1238 /* Call the NFS version-specific code */
1239 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1242 while (!list_empty(&data->pages)) {
1243 req = nfs_list_entry(data->pages.next);
1244 nfs_list_remove_request(req);
1245 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1247 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1248 req->wb_context->dentry->d_inode->i_sb->s_id,
1249 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1251 (long long)req_offset(req));
1252 if (task->tk_status < 0) {
1253 req->wb_context->error = task->tk_status;
1254 nfs_inode_remove_request(req);
1255 dprintk(", error = %d\n", task->tk_status);
1259 /* Okay, COMMIT succeeded, apparently. Check the verifier
1260 * returned by the server against all stored verfs. */
1261 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1262 /* We have a match */
1263 nfs_inode_remove_request(req);
1267 /* We have a mismatch. Write the page again */
1268 dprintk(" mismatch\n");
1269 nfs_redirty_request(req);
1271 nfs_clear_page_writeback(req);
1275 static const struct rpc_call_ops nfs_commit_ops = {
1276 .rpc_call_done = nfs_commit_done,
1277 .rpc_release = nfs_commit_release,
1280 static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1286 static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
1288 struct nfs_inode *nfsi = NFS_I(mapping->host);
1292 spin_lock(&nfsi->req_lock);
1293 res = nfs_scan_dirty(mapping, wbc, &head);
1294 spin_unlock(&nfsi->req_lock);
1296 int error = nfs_flush_list(mapping->host, &head, res, how);
1303 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1304 int nfs_commit_inode(struct inode *inode, int how)
1306 struct nfs_inode *nfsi = NFS_I(inode);
1310 spin_lock(&nfsi->req_lock);
1311 res = nfs_scan_commit(inode, &head, 0, 0);
1312 spin_unlock(&nfsi->req_lock);
1314 int error = nfs_commit_list(inode, &head, how);
1322 long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
1324 struct inode *inode = mapping->host;
1325 struct nfs_inode *nfsi = NFS_I(inode);
1326 unsigned long idx_start, idx_end;
1327 unsigned int npages = 0;
1329 int nocommit = how & FLUSH_NOCOMMIT;
1333 if (wbc->range_cyclic)
1336 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1337 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1338 if (idx_end > idx_start) {
1339 unsigned long l_npages = 1 + idx_end - idx_start;
1341 if (sizeof(npages) != sizeof(l_npages) &&
1342 (unsigned long)npages != l_npages)
1346 how &= ~FLUSH_NOCOMMIT;
1347 spin_lock(&nfsi->req_lock);
1349 wbc->pages_skipped = 0;
1350 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1353 pages = nfs_scan_dirty(mapping, wbc, &head);
1355 spin_unlock(&nfsi->req_lock);
1356 if (how & FLUSH_INVALIDATE) {
1357 nfs_cancel_dirty_list(&head);
1360 ret = nfs_flush_list(inode, &head, pages, how);
1361 spin_lock(&nfsi->req_lock);
1364 if (wbc->pages_skipped != 0)
1368 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1370 if (wbc->pages_skipped != 0)
1374 if (how & FLUSH_INVALIDATE) {
1375 spin_unlock(&nfsi->req_lock);
1376 nfs_cancel_commit_list(&head);
1378 spin_lock(&nfsi->req_lock);
1381 pages += nfs_scan_commit(inode, &head, 0, 0);
1382 spin_unlock(&nfsi->req_lock);
1383 ret = nfs_commit_list(inode, &head, how);
1384 spin_lock(&nfsi->req_lock);
1386 spin_unlock(&nfsi->req_lock);
1391 * flush the inode to disk.
1393 int nfs_wb_all(struct inode *inode)
1395 struct address_space *mapping = inode->i_mapping;
1396 struct writeback_control wbc = {
1397 .bdi = mapping->backing_dev_info,
1398 .sync_mode = WB_SYNC_ALL,
1399 .nr_to_write = LONG_MAX,
1400 .for_writepages = 1,
1405 ret = generic_writepages(mapping, &wbc);
1408 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1412 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1416 int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1418 struct writeback_control wbc = {
1419 .bdi = mapping->backing_dev_info,
1420 .sync_mode = WB_SYNC_ALL,
1421 .nr_to_write = LONG_MAX,
1422 .range_start = range_start,
1423 .range_end = range_end,
1424 .for_writepages = 1,
1428 if (!(how & FLUSH_NOWRITEPAGE)) {
1429 ret = generic_writepages(mapping, &wbc);
1433 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1437 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1441 int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
1443 loff_t range_start = page_offset(page);
1444 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1445 struct writeback_control wbc = {
1446 .bdi = page->mapping->backing_dev_info,
1447 .sync_mode = WB_SYNC_ALL,
1448 .nr_to_write = LONG_MAX,
1449 .range_start = range_start,
1450 .range_end = range_end,
1454 BUG_ON(!PageLocked(page));
1455 if (!(how & FLUSH_NOWRITEPAGE) && clear_page_dirty_for_io(page)) {
1456 ret = nfs_writepage_locked(page, &wbc);
1460 if (!PagePrivate(page))
1462 ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
1466 __mark_inode_dirty(inode, I_DIRTY_PAGES);
1471 * Write back all requests on one page - we do this before reading it.
1473 int nfs_wb_page(struct inode *inode, struct page* page)
1475 return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
1478 int nfs_set_page_dirty(struct page *page)
1480 struct nfs_page *req;
1482 req = nfs_page_find_request(page);
1484 /* Mark any existing write requests for flushing */
1485 set_bit(PG_NEED_FLUSH, &req->wb_flags);
1486 nfs_release_request(req);
1488 return __set_page_dirty_nobuffers(page);
1492 int __init nfs_init_writepagecache(void)
1494 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1495 sizeof(struct nfs_write_data),
1496 0, SLAB_HWCACHE_ALIGN,
1498 if (nfs_wdata_cachep == NULL)
1501 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1503 if (nfs_wdata_mempool == NULL)
1506 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1508 if (nfs_commit_mempool == NULL)
1514 void nfs_destroy_writepagecache(void)
1516 mempool_destroy(nfs_commit_mempool);
1517 mempool_destroy(nfs_wdata_mempool);
1518 kmem_cache_destroy(nfs_wdata_cachep);