4 * Writing file data over NFS.
6 * We do it like this: When a (user) process wishes to write data to an
7 * NFS file, a write request is allocated that contains the RPC task data
8 * plus some info on the page to be written, and added to the inode's
9 * write chain. If the process writes past the end of the page, an async
10 * RPC call to write the page is scheduled immediately; otherwise, the call
11 * is delayed for a few seconds.
13 * Just like readahead, no async I/O is performed if wsize < PAGE_SIZE.
15 * Write requests are kept on the inode's writeback list. Each entry in
16 * that list references the page (portion) to be written. When the
17 * cache timeout has expired, the RPC task is woken up, and tries to
18 * lock the page. As soon as it manages to do so, the request is moved
19 * from the writeback list to the writelock list.
21 * Note: we must make sure never to confuse the inode passed in the
22 * write_page request with the one in page->inode. As far as I understand
23 * it, these are different when doing a swap-out.
25 * To understand everything that goes on here and in the NFS read code,
26 * one should be aware that a page is locked in exactly one of the following
29 * - A write request is in progress.
30 * - A user process is in generic_file_write/nfs_update_page
31 * - A user process is in generic_file_read
33 * Also note that because of the way pages are invalidated in
34 * nfs_revalidate_inode, the following assertions hold:
36 * - If a page is dirty, there will be no read requests (a page will
37 * not be re-read unless invalidated by nfs_revalidate_inode).
38 * - If the page is not uptodate, there will be no pending write
39 * requests, and no process will be in nfs_update_page.
41 * FIXME: Interaction with the vmscan routines is not optimal yet.
42 * Either vmscan must be made nfs-savvy, or we need a different page
43 * reclaim concept that supports something like FS-independent
44 * buffer_heads with a b_ops-> field.
46 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
49 #include <linux/types.h>
50 #include <linux/slab.h>
52 #include <linux/pagemap.h>
53 #include <linux/file.h>
54 #include <linux/writeback.h>
56 #include <linux/sunrpc/clnt.h>
57 #include <linux/nfs_fs.h>
58 #include <linux/nfs_mount.h>
59 #include <linux/nfs_page.h>
60 #include <linux/backing-dev.h>
62 #include <asm/uaccess.h>
63 #include <linux/smp_lock.h>
65 #include "delegation.h"
69 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
71 #define MIN_POOL_WRITE (32)
72 #define MIN_POOL_COMMIT (4)
75 * Local function declarations
77 static struct nfs_page * nfs_update_request(struct nfs_open_context*,
80 unsigned int, unsigned int);
81 static int nfs_wait_on_write_congestion(struct address_space *, int);
82 static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
83 static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how);
84 static int nfs_wb_page_priority(struct inode *inode, struct page *page, int how);
85 static const struct rpc_call_ops nfs_write_partial_ops;
86 static const struct rpc_call_ops nfs_write_full_ops;
87 static const struct rpc_call_ops nfs_commit_ops;
89 static kmem_cache_t *nfs_wdata_cachep;
90 static mempool_t *nfs_wdata_mempool;
91 static mempool_t *nfs_commit_mempool;
93 static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
95 struct nfs_write_data *nfs_commit_alloc(void)
97 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
100 memset(p, 0, sizeof(*p));
101 INIT_LIST_HEAD(&p->pages);
106 void nfs_commit_rcu_free(struct rcu_head *head)
108 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
109 if (p && (p->pagevec != &p->page_array[0]))
111 mempool_free(p, nfs_commit_mempool);
114 void nfs_commit_free(struct nfs_write_data *wdata)
116 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
119 struct nfs_write_data *nfs_writedata_alloc(size_t len)
121 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
122 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
125 memset(p, 0, sizeof(*p));
126 INIT_LIST_HEAD(&p->pages);
127 p->npages = pagecount;
128 if (pagecount <= ARRAY_SIZE(p->page_array))
129 p->pagevec = p->page_array;
131 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
133 mempool_free(p, nfs_wdata_mempool);
141 static void nfs_writedata_rcu_free(struct rcu_head *head)
143 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
144 if (p && (p->pagevec != &p->page_array[0]))
146 mempool_free(p, nfs_wdata_mempool);
149 static void nfs_writedata_free(struct nfs_write_data *wdata)
151 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
154 void nfs_writedata_release(void *wdata)
156 nfs_writedata_free(wdata);
159 static struct nfs_page *nfs_page_find_request_locked(struct page *page)
161 struct nfs_page *req = NULL;
163 if (PagePrivate(page)) {
164 req = (struct nfs_page *)page_private(page);
166 atomic_inc(&req->wb_count);
171 static struct nfs_page *nfs_page_find_request(struct page *page)
173 struct nfs_page *req = NULL;
174 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
177 req = nfs_page_find_request_locked(page);
178 spin_unlock(req_lock);
182 /* Adjust the file length if we're writing beyond the end */
183 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
185 struct inode *inode = page->mapping->host;
186 loff_t end, i_size = i_size_read(inode);
187 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
189 if (i_size > 0 && page->index < end_index)
191 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
194 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
195 i_size_write(inode, end);
198 /* We can set the PG_uptodate flag if we see that a write request
199 * covers the full page.
201 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
203 if (PageUptodate(page))
207 if (count != nfs_page_length(page))
209 if (count != PAGE_CACHE_SIZE)
210 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
211 SetPageUptodate(page);
215 * Write a page synchronously.
216 * Offset is the data offset within the page.
218 static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
219 struct page *page, unsigned int offset, unsigned int count,
222 unsigned int wsize = NFS_SERVER(inode)->wsize;
223 int result, written = 0;
224 struct nfs_write_data *wdata;
226 wdata = nfs_writedata_alloc(wsize);
231 wdata->cred = ctx->cred;
232 wdata->inode = inode;
233 wdata->args.fh = NFS_FH(inode);
234 wdata->args.context = ctx;
235 wdata->args.pages = &page;
236 wdata->args.stable = NFS_FILE_SYNC;
237 wdata->args.pgbase = offset;
238 wdata->args.count = wsize;
239 wdata->res.fattr = &wdata->fattr;
240 wdata->res.verf = &wdata->verf;
242 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
244 (long long)NFS_FILEID(inode),
245 count, (long long)(page_offset(page) + offset));
247 set_page_writeback(page);
248 nfs_begin_data_update(inode);
251 wdata->args.count = count;
252 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
254 result = NFS_PROTO(inode)->write(wdata);
257 /* Must mark the page invalid after I/O error */
258 ClearPageUptodate(page);
261 if (result < wdata->args.count)
262 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
263 wdata->args.count, result);
265 wdata->args.offset += result;
266 wdata->args.pgbase += result;
269 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
271 /* Update file length */
272 nfs_grow_file(page, offset, written);
273 /* Set the PG_uptodate flag? */
274 nfs_mark_uptodate(page, offset, written);
277 ClearPageError(page);
280 nfs_end_data_update(inode);
281 end_page_writeback(page);
282 nfs_writedata_release(wdata);
283 return written ? written : result;
286 static int nfs_writepage_async(struct nfs_open_context *ctx,
287 struct inode *inode, struct page *page,
288 unsigned int offset, unsigned int count)
290 struct nfs_page *req;
292 req = nfs_update_request(ctx, inode, page, offset, count);
295 /* Update file length */
296 nfs_grow_file(page, offset, count);
297 /* Set the PG_uptodate flag? */
298 nfs_mark_uptodate(page, offset, count);
299 nfs_unlock_request(req);
303 static int wb_priority(struct writeback_control *wbc)
305 if (wbc->for_reclaim)
306 return FLUSH_HIGHPRI;
307 if (wbc->for_kupdate)
313 * Write an mmapped page to the server.
315 int nfs_writepage(struct page *page, struct writeback_control *wbc)
317 struct nfs_open_context *ctx;
318 struct inode *inode = page->mapping->host;
320 int inode_referenced = 0;
321 int priority = wb_priority(wbc);
324 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
325 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
328 * Note: We need to ensure that we have a reference to the inode
329 * if we are to do asynchronous writes. If not, waiting
330 * in nfs_wait_on_request() may deadlock with clear_inode().
332 * If igrab() fails here, then it is in any case safe to
333 * call nfs_wb_page(), since there will be no pending writes.
335 if (igrab(inode) != 0)
336 inode_referenced = 1;
338 /* Ensure we've flushed out any previous writes */
339 nfs_wb_page_priority(inode, page, priority);
342 offset = nfs_page_length(page);
346 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
352 if (!IS_SYNC(inode) && inode_referenced) {
353 err = nfs_writepage_async(ctx, inode, page, 0, offset);
354 if (!wbc->for_writepages)
355 nfs_flush_mapping(page->mapping, wbc, wb_priority(wbc));
357 err = nfs_writepage_sync(ctx, inode, page, 0,
361 redirty_page_for_writepage(wbc, page);
366 put_nfs_open_context(ctx);
369 if (inode_referenced)
375 * Note: causes nfs_update_request() to block on the assumption
376 * that the writeback is generated due to memory pressure.
378 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
380 struct backing_dev_info *bdi = mapping->backing_dev_info;
381 struct inode *inode = mapping->host;
384 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
386 err = generic_writepages(mapping, wbc);
389 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
390 if (wbc->nonblocking)
392 nfs_wait_on_write_congestion(mapping, 0);
394 err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
397 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
398 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
399 err = nfs_wait_on_requests(inode, 0, 0);
403 err = nfs_commit_inode(inode, wb_priority(wbc));
407 clear_bit(BDI_write_congested, &bdi->state);
408 wake_up_all(&nfs_write_congestion);
409 congestion_end(WRITE);
414 * Insert a write request into an inode
416 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
418 struct nfs_inode *nfsi = NFS_I(inode);
421 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
422 BUG_ON(error == -EEXIST);
427 nfs_begin_data_update(inode);
428 if (nfs_have_delegation(inode, FMODE_WRITE))
431 SetPagePrivate(req->wb_page);
432 set_page_private(req->wb_page, (unsigned long)req);
434 atomic_inc(&req->wb_count);
439 * Insert a write request into an inode
441 static void nfs_inode_remove_request(struct nfs_page *req)
443 struct inode *inode = req->wb_context->dentry->d_inode;
444 struct nfs_inode *nfsi = NFS_I(inode);
446 BUG_ON (!NFS_WBACK_BUSY(req));
448 spin_lock(&nfsi->req_lock);
449 set_page_private(req->wb_page, 0);
450 ClearPagePrivate(req->wb_page);
451 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
454 spin_unlock(&nfsi->req_lock);
455 nfs_end_data_update(inode);
458 spin_unlock(&nfsi->req_lock);
459 nfs_clear_request(req);
460 nfs_release_request(req);
464 * Add a request to the inode's dirty list.
467 nfs_mark_request_dirty(struct nfs_page *req)
469 struct inode *inode = req->wb_context->dentry->d_inode;
470 struct nfs_inode *nfsi = NFS_I(inode);
472 spin_lock(&nfsi->req_lock);
473 radix_tree_tag_set(&nfsi->nfs_page_tree,
474 req->wb_index, NFS_PAGE_TAG_DIRTY);
475 nfs_list_add_request(req, &nfsi->dirty);
477 spin_unlock(&nfsi->req_lock);
478 inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
479 mark_inode_dirty(inode);
483 * Check if a request is dirty
486 nfs_dirty_request(struct nfs_page *req)
488 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
489 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
492 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
494 * Add a request to the inode's commit list.
497 nfs_mark_request_commit(struct nfs_page *req)
499 struct inode *inode = req->wb_context->dentry->d_inode;
500 struct nfs_inode *nfsi = NFS_I(inode);
502 spin_lock(&nfsi->req_lock);
503 nfs_list_add_request(req, &nfsi->commit);
505 spin_unlock(&nfsi->req_lock);
506 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
507 mark_inode_dirty(inode);
512 * Wait for a request to complete.
514 * Interruptible by signals only if mounted with intr flag.
516 static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
518 struct nfs_inode *nfsi = NFS_I(inode);
519 struct nfs_page *req;
520 unsigned long idx_end, next;
521 unsigned int res = 0;
527 idx_end = idx_start + npages - 1;
530 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
531 if (req->wb_index > idx_end)
534 next = req->wb_index + 1;
535 BUG_ON(!NFS_WBACK_BUSY(req));
537 atomic_inc(&req->wb_count);
538 spin_unlock(&nfsi->req_lock);
539 error = nfs_wait_on_request(req);
540 nfs_release_request(req);
541 spin_lock(&nfsi->req_lock);
549 static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
551 struct nfs_inode *nfsi = NFS_I(inode);
554 spin_lock(&nfsi->req_lock);
555 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
556 spin_unlock(&nfsi->req_lock);
560 static void nfs_cancel_dirty_list(struct list_head *head)
562 struct nfs_page *req;
563 while(!list_empty(head)) {
564 req = nfs_list_entry(head->next);
565 nfs_list_remove_request(req);
566 nfs_inode_remove_request(req);
567 nfs_clear_page_writeback(req);
571 static void nfs_cancel_commit_list(struct list_head *head)
573 struct nfs_page *req;
575 while(!list_empty(head)) {
576 req = nfs_list_entry(head->next);
577 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
578 nfs_list_remove_request(req);
579 nfs_inode_remove_request(req);
580 nfs_unlock_request(req);
584 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
586 * nfs_scan_commit - Scan an inode for commit requests
587 * @inode: NFS inode to scan
588 * @dst: destination list
589 * @idx_start: lower bound of page->index to scan.
590 * @npages: idx_start + npages sets the upper bound to scan.
592 * Moves requests from the inode's 'commit' request list.
593 * The requests are *not* checked to ensure that they form a contiguous set.
596 nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
598 struct nfs_inode *nfsi = NFS_I(inode);
601 if (nfsi->ncommit != 0) {
602 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
603 nfsi->ncommit -= res;
604 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
605 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
610 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
616 static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
618 struct backing_dev_info *bdi = mapping->backing_dev_info;
624 if (!bdi_write_congested(bdi))
627 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
630 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
633 rpc_clnt_sigmask(clnt, &oldset);
634 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
635 if (bdi_write_congested(bdi)) {
641 rpc_clnt_sigunmask(clnt, &oldset);
643 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
644 if (bdi_write_congested(bdi))
647 finish_wait(&nfs_write_congestion, &wait);
653 * Try to update any existing write request, or create one if there is none.
654 * In order to match, the request's credentials must match those of
655 * the calling process.
657 * Note: Should always be called with the Page Lock held!
659 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
660 struct inode *inode, struct page *page,
661 unsigned int offset, unsigned int bytes)
663 struct nfs_server *server = NFS_SERVER(inode);
664 struct nfs_inode *nfsi = NFS_I(inode);
665 struct nfs_page *req, *new = NULL;
666 unsigned long rqend, end;
668 end = offset + bytes;
670 if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
671 return ERR_PTR(-ERESTARTSYS);
673 /* Loop over all inode entries and see if we find
674 * A request for the page we wish to update
676 spin_lock(&nfsi->req_lock);
677 req = nfs_page_find_request_locked(page);
679 if (!nfs_lock_request_dontget(req)) {
682 spin_unlock(&nfsi->req_lock);
683 error = nfs_wait_on_request(req);
684 nfs_release_request(req);
687 nfs_release_request(new);
688 return ERR_PTR(error);
692 spin_unlock(&nfsi->req_lock);
694 nfs_release_request(new);
700 nfs_lock_request_dontget(new);
701 error = nfs_inode_add_request(inode, new);
703 spin_unlock(&nfsi->req_lock);
704 nfs_unlock_request(new);
705 return ERR_PTR(error);
707 spin_unlock(&nfsi->req_lock);
708 nfs_mark_request_dirty(new);
711 spin_unlock(&nfsi->req_lock);
713 new = nfs_create_request(ctx, inode, page, offset, bytes);
718 /* We have a request for our page.
719 * If the creds don't match, or the
720 * page addresses don't match,
721 * tell the caller to wait on the conflicting
724 rqend = req->wb_offset + req->wb_bytes;
725 if (req->wb_context != ctx
726 || req->wb_page != page
727 || !nfs_dirty_request(req)
728 || offset > rqend || end < req->wb_offset) {
729 nfs_unlock_request(req);
730 return ERR_PTR(-EBUSY);
733 /* Okay, the request matches. Update the region */
734 if (offset < req->wb_offset) {
735 req->wb_offset = offset;
736 req->wb_pgbase = offset;
737 req->wb_bytes = rqend - req->wb_offset;
741 req->wb_bytes = end - req->wb_offset;
746 int nfs_flush_incompatible(struct file *file, struct page *page)
748 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
749 struct nfs_page *req;
752 * Look for a request corresponding to this page. If there
753 * is one, and it belongs to another file, we flush it out
754 * before we try to copy anything into the page. Do this
755 * due to the lack of an ACCESS-type call in NFSv2.
756 * Also do the same if we find a request from an existing
759 req = nfs_page_find_request(page);
761 int do_flush = req->wb_page != page || req->wb_context != ctx;
763 nfs_release_request(req);
765 status = nfs_wb_page(page->mapping->host, page);
767 return (status < 0) ? status : 0;
771 * Update and possibly write a cached page of an NFS file.
773 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
774 * things with a page scheduled for an RPC call (e.g. invalidate it).
776 int nfs_updatepage(struct file *file, struct page *page,
777 unsigned int offset, unsigned int count)
779 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
780 struct inode *inode = page->mapping->host;
781 struct nfs_page *req;
784 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
786 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
787 file->f_dentry->d_parent->d_name.name,
788 file->f_dentry->d_name.name, count,
789 (long long)(page_offset(page) +offset));
791 if (IS_SYNC(inode)) {
792 status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
794 if (offset == 0 && status == PAGE_CACHE_SIZE)
795 SetPageUptodate(page);
801 /* If we're not using byte range locks, and we know the page
802 * is entirely in cache, it may be more efficient to avoid
803 * fragmenting write requests.
805 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
806 count = max(count + offset, nfs_page_length(page));
811 * Try to find an NFS request corresponding to this page
813 * If the existing request cannot be updated, we must flush
817 req = nfs_update_request(ctx, inode, page, offset, count);
818 status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
819 if (status != -EBUSY)
821 /* Request could not be updated. Flush it out and try again */
822 status = nfs_wb_page(inode, page);
823 } while (status >= 0);
829 /* Update file length */
830 nfs_grow_file(page, offset, count);
831 /* Set the PG_uptodate flag? */
832 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
833 nfs_unlock_request(req);
835 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
836 status, (long long)i_size_read(inode));
838 ClearPageUptodate(page);
842 static void nfs_writepage_release(struct nfs_page *req)
844 end_page_writeback(req->wb_page);
846 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
847 if (!PageError(req->wb_page)) {
848 if (NFS_NEED_RESCHED(req)) {
849 nfs_mark_request_dirty(req);
851 } else if (NFS_NEED_COMMIT(req)) {
852 nfs_mark_request_commit(req);
856 nfs_inode_remove_request(req);
859 nfs_clear_commit(req);
860 nfs_clear_reschedule(req);
862 nfs_inode_remove_request(req);
864 nfs_clear_page_writeback(req);
867 static inline int flush_task_priority(int how)
869 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
871 return RPC_PRIORITY_HIGH;
873 return RPC_PRIORITY_LOW;
875 return RPC_PRIORITY_NORMAL;
879 * Set up the argument/result storage required for the RPC call.
881 static void nfs_write_rpcsetup(struct nfs_page *req,
882 struct nfs_write_data *data,
883 const struct rpc_call_ops *call_ops,
884 unsigned int count, unsigned int offset,
890 /* Set up the RPC argument and reply structs
891 * NB: take care not to mess about with data->commit et al. */
894 data->inode = inode = req->wb_context->dentry->d_inode;
895 data->cred = req->wb_context->cred;
897 data->args.fh = NFS_FH(inode);
898 data->args.offset = req_offset(req) + offset;
899 data->args.pgbase = req->wb_pgbase + offset;
900 data->args.pages = data->pagevec;
901 data->args.count = count;
902 data->args.context = req->wb_context;
904 data->res.fattr = &data->fattr;
905 data->res.count = count;
906 data->res.verf = &data->verf;
907 nfs_fattr_init(&data->fattr);
909 /* Set up the initial task struct. */
910 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
911 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
912 NFS_PROTO(inode)->write_setup(data, how);
914 data->task.tk_priority = flush_task_priority(how);
915 data->task.tk_cookie = (unsigned long)inode;
917 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
920 (long long)NFS_FILEID(inode),
922 (unsigned long long)data->args.offset);
925 static void nfs_execute_write(struct nfs_write_data *data)
927 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
930 rpc_clnt_sigmask(clnt, &oldset);
931 rpc_execute(&data->task);
932 rpc_clnt_sigunmask(clnt, &oldset);
936 * Generate multiple small requests to write out a single
937 * contiguous dirty area on one page.
939 static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
941 struct nfs_page *req = nfs_list_entry(head->next);
942 struct page *page = req->wb_page;
943 struct nfs_write_data *data;
944 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
949 nfs_list_remove_request(req);
951 nbytes = req->wb_bytes;
953 size_t len = min(nbytes, wsize);
955 data = nfs_writedata_alloc(len);
958 list_add(&data->pages, &list);
961 } while (nbytes != 0);
962 atomic_set(&req->wb_complete, requests);
964 ClearPageError(page);
965 set_page_writeback(page);
967 nbytes = req->wb_bytes;
969 data = list_entry(list.next, struct nfs_write_data, pages);
970 list_del_init(&data->pages);
972 data->pagevec[0] = page;
974 if (nbytes > wsize) {
975 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
980 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
981 nbytes, offset, how);
984 nfs_execute_write(data);
985 } while (nbytes != 0);
990 while (!list_empty(&list)) {
991 data = list_entry(list.next, struct nfs_write_data, pages);
992 list_del(&data->pages);
993 nfs_writedata_release(data);
995 nfs_mark_request_dirty(req);
996 nfs_clear_page_writeback(req);
1001 * Create an RPC task for the given write request and kick it.
1002 * The page must have been locked by the caller.
1004 * It may happen that the page we're passed is not marked dirty.
1005 * This is the case if nfs_updatepage detects a conflicting request
1006 * that has been written but not committed.
1008 static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
1010 struct nfs_page *req;
1011 struct page **pages;
1012 struct nfs_write_data *data;
1015 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
1019 pages = data->pagevec;
1021 while (!list_empty(head)) {
1022 req = nfs_list_entry(head->next);
1023 nfs_list_remove_request(req);
1024 nfs_list_add_request(req, &data->pages);
1025 ClearPageError(req->wb_page);
1026 set_page_writeback(req->wb_page);
1027 *pages++ = req->wb_page;
1028 count += req->wb_bytes;
1030 req = nfs_list_entry(data->pages.next);
1032 /* Set up the argument struct */
1033 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
1035 nfs_execute_write(data);
1038 while (!list_empty(head)) {
1039 struct nfs_page *req = nfs_list_entry(head->next);
1040 nfs_list_remove_request(req);
1041 nfs_mark_request_dirty(req);
1042 nfs_clear_page_writeback(req);
1047 static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
1049 LIST_HEAD(one_request);
1050 int (*flush_one)(struct inode *, struct list_head *, int);
1051 struct nfs_page *req;
1052 int wpages = NFS_SERVER(inode)->wpages;
1053 int wsize = NFS_SERVER(inode)->wsize;
1056 flush_one = nfs_flush_one;
1057 if (wsize < PAGE_CACHE_SIZE)
1058 flush_one = nfs_flush_multi;
1059 /* For single writes, FLUSH_STABLE is more efficient */
1060 if (npages <= wpages && npages == NFS_I(inode)->npages
1061 && nfs_list_entry(head->next)->wb_bytes <= wsize)
1062 how |= FLUSH_STABLE;
1065 nfs_coalesce_requests(head, &one_request, wpages);
1066 req = nfs_list_entry(one_request.next);
1067 error = flush_one(inode, &one_request, how);
1070 } while (!list_empty(head));
1073 while (!list_empty(head)) {
1074 req = nfs_list_entry(head->next);
1075 nfs_list_remove_request(req);
1076 nfs_mark_request_dirty(req);
1077 nfs_clear_page_writeback(req);
1083 * Handle a write reply that flushed part of a page.
1085 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
1087 struct nfs_write_data *data = calldata;
1088 struct nfs_page *req = data->req;
1089 struct page *page = req->wb_page;
1091 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1092 req->wb_context->dentry->d_inode->i_sb->s_id,
1093 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1095 (long long)req_offset(req));
1097 if (nfs_writeback_done(task, data) != 0)
1100 if (task->tk_status < 0) {
1101 ClearPageUptodate(page);
1103 req->wb_context->error = task->tk_status;
1104 dprintk(", error = %d\n", task->tk_status);
1106 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1107 if (data->verf.committed < NFS_FILE_SYNC) {
1108 if (!NFS_NEED_COMMIT(req)) {
1109 nfs_defer_commit(req);
1110 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1111 dprintk(" defer commit\n");
1112 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1113 nfs_defer_reschedule(req);
1114 dprintk(" server reboot detected\n");
1121 if (atomic_dec_and_test(&req->wb_complete))
1122 nfs_writepage_release(req);
1125 static const struct rpc_call_ops nfs_write_partial_ops = {
1126 .rpc_call_done = nfs_writeback_done_partial,
1127 .rpc_release = nfs_writedata_release,
1131 * Handle a write reply that flushes a whole page.
1133 * FIXME: There is an inherent race with invalidate_inode_pages and
1134 * writebacks since the page->count is kept > 1 for as long
1135 * as the page has a write request pending.
1137 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1139 struct nfs_write_data *data = calldata;
1140 struct nfs_page *req;
1143 if (nfs_writeback_done(task, data) != 0)
1146 /* Update attributes as result of writeback. */
1147 while (!list_empty(&data->pages)) {
1148 req = nfs_list_entry(data->pages.next);
1149 nfs_list_remove_request(req);
1150 page = req->wb_page;
1152 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1153 req->wb_context->dentry->d_inode->i_sb->s_id,
1154 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1156 (long long)req_offset(req));
1158 if (task->tk_status < 0) {
1159 ClearPageUptodate(page);
1161 req->wb_context->error = task->tk_status;
1162 end_page_writeback(page);
1163 nfs_inode_remove_request(req);
1164 dprintk(", error = %d\n", task->tk_status);
1167 end_page_writeback(page);
1169 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1170 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1171 nfs_inode_remove_request(req);
1175 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1176 nfs_mark_request_commit(req);
1177 dprintk(" marked for commit\n");
1179 nfs_inode_remove_request(req);
1182 nfs_clear_page_writeback(req);
1186 static const struct rpc_call_ops nfs_write_full_ops = {
1187 .rpc_call_done = nfs_writeback_done_full,
1188 .rpc_release = nfs_writedata_release,
1193 * This function is called when the WRITE call is complete.
1195 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1197 struct nfs_writeargs *argp = &data->args;
1198 struct nfs_writeres *resp = &data->res;
1201 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1202 task->tk_pid, task->tk_status);
1205 * ->write_done will attempt to use post-op attributes to detect
1206 * conflicting writes by other clients. A strict interpretation
1207 * of close-to-open would allow us to continue caching even if
1208 * another writer had changed the file, but some applications
1209 * depend on tighter cache coherency when writing.
1211 status = NFS_PROTO(data->inode)->write_done(task, data);
1214 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1216 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1217 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1218 /* We tried a write call, but the server did not
1219 * commit data to stable storage even though we
1221 * Note: There is a known bug in Tru64 < 5.0 in which
1222 * the server reports NFS_DATA_SYNC, but performs
1223 * NFS_FILE_SYNC. We therefore implement this checking
1224 * as a dprintk() in order to avoid filling syslog.
1226 static unsigned long complain;
1228 if (time_before(complain, jiffies)) {
1229 dprintk("NFS: faulty NFS server %s:"
1230 " (committed = %d) != (stable = %d)\n",
1231 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1232 resp->verf->committed, argp->stable);
1233 complain = jiffies + 300 * HZ;
1237 /* Is this a short write? */
1238 if (task->tk_status >= 0 && resp->count < argp->count) {
1239 static unsigned long complain;
1241 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1243 /* Has the server at least made some progress? */
1244 if (resp->count != 0) {
1245 /* Was this an NFSv2 write or an NFSv3 stable write? */
1246 if (resp->verf->committed != NFS_UNSTABLE) {
1247 /* Resend from where the server left off */
1248 argp->offset += resp->count;
1249 argp->pgbase += resp->count;
1250 argp->count -= resp->count;
1252 /* Resend as a stable write in order to avoid
1253 * headaches in the case of a server crash.
1255 argp->stable = NFS_FILE_SYNC;
1257 rpc_restart_call(task);
1260 if (time_before(complain, jiffies)) {
1262 "NFS: Server wrote zero bytes, expected %u.\n",
1264 complain = jiffies + 300 * HZ;
1266 /* Can't do anything about it except throw an error. */
1267 task->tk_status = -EIO;
1273 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1274 void nfs_commit_release(void *wdata)
1276 nfs_commit_free(wdata);
1280 * Set up the argument/result storage required for the RPC call.
1282 static void nfs_commit_rpcsetup(struct list_head *head,
1283 struct nfs_write_data *data,
1286 struct nfs_page *first;
1287 struct inode *inode;
1290 /* Set up the RPC argument and reply structs
1291 * NB: take care not to mess about with data->commit et al. */
1293 list_splice_init(head, &data->pages);
1294 first = nfs_list_entry(data->pages.next);
1295 inode = first->wb_context->dentry->d_inode;
1297 data->inode = inode;
1298 data->cred = first->wb_context->cred;
1300 data->args.fh = NFS_FH(data->inode);
1301 /* Note: we always request a commit of the entire inode */
1302 data->args.offset = 0;
1303 data->args.count = 0;
1304 data->res.count = 0;
1305 data->res.fattr = &data->fattr;
1306 data->res.verf = &data->verf;
1307 nfs_fattr_init(&data->fattr);
1309 /* Set up the initial task struct. */
1310 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1311 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
1312 NFS_PROTO(inode)->commit_setup(data, how);
1314 data->task.tk_priority = flush_task_priority(how);
1315 data->task.tk_cookie = (unsigned long)inode;
1317 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
1321 * Commit dirty pages
1324 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1326 struct nfs_write_data *data;
1327 struct nfs_page *req;
1329 data = nfs_commit_alloc();
1334 /* Set up the argument struct */
1335 nfs_commit_rpcsetup(head, data, how);
1337 nfs_execute_write(data);
1340 while (!list_empty(head)) {
1341 req = nfs_list_entry(head->next);
1342 nfs_list_remove_request(req);
1343 nfs_mark_request_commit(req);
1344 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1345 nfs_clear_page_writeback(req);
1351 * COMMIT call returned
1353 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1355 struct nfs_write_data *data = calldata;
1356 struct nfs_page *req;
1358 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1359 task->tk_pid, task->tk_status);
1361 /* Call the NFS version-specific code */
1362 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1365 while (!list_empty(&data->pages)) {
1366 req = nfs_list_entry(data->pages.next);
1367 nfs_list_remove_request(req);
1368 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1370 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1371 req->wb_context->dentry->d_inode->i_sb->s_id,
1372 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1374 (long long)req_offset(req));
1375 if (task->tk_status < 0) {
1376 req->wb_context->error = task->tk_status;
1377 nfs_inode_remove_request(req);
1378 dprintk(", error = %d\n", task->tk_status);
1382 /* Okay, COMMIT succeeded, apparently. Check the verifier
1383 * returned by the server against all stored verfs. */
1384 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1385 /* We have a match */
1386 nfs_inode_remove_request(req);
1390 /* We have a mismatch. Write the page again */
1391 dprintk(" mismatch\n");
1392 nfs_mark_request_dirty(req);
1394 nfs_clear_page_writeback(req);
1398 static const struct rpc_call_ops nfs_commit_ops = {
1399 .rpc_call_done = nfs_commit_done,
1400 .rpc_release = nfs_commit_release,
1403 static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1409 static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
1411 struct nfs_inode *nfsi = NFS_I(mapping->host);
1415 spin_lock(&nfsi->req_lock);
1416 res = nfs_scan_dirty(mapping, wbc, &head);
1417 spin_unlock(&nfsi->req_lock);
1419 int error = nfs_flush_list(mapping->host, &head, res, how);
1426 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1427 int nfs_commit_inode(struct inode *inode, int how)
1429 struct nfs_inode *nfsi = NFS_I(inode);
1433 spin_lock(&nfsi->req_lock);
1434 res = nfs_scan_commit(inode, &head, 0, 0);
1435 spin_unlock(&nfsi->req_lock);
1437 int error = nfs_commit_list(inode, &head, how);
1445 long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
1447 struct inode *inode = mapping->host;
1448 struct nfs_inode *nfsi = NFS_I(inode);
1449 unsigned long idx_start, idx_end;
1450 unsigned int npages = 0;
1452 int nocommit = how & FLUSH_NOCOMMIT;
1456 if (wbc->range_cyclic)
1459 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1460 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1461 if (idx_end > idx_start) {
1462 unsigned long l_npages = 1 + idx_end - idx_start;
1464 if (sizeof(npages) != sizeof(l_npages) &&
1465 (unsigned long)npages != l_npages)
1469 how &= ~FLUSH_NOCOMMIT;
1470 spin_lock(&nfsi->req_lock);
1472 wbc->pages_skipped = 0;
1473 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1476 pages = nfs_scan_dirty(mapping, wbc, &head);
1478 spin_unlock(&nfsi->req_lock);
1479 if (how & FLUSH_INVALIDATE) {
1480 nfs_cancel_dirty_list(&head);
1483 ret = nfs_flush_list(inode, &head, pages, how);
1484 spin_lock(&nfsi->req_lock);
1487 if (wbc->pages_skipped != 0)
1491 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1493 if (wbc->pages_skipped != 0)
1497 if (how & FLUSH_INVALIDATE) {
1498 spin_unlock(&nfsi->req_lock);
1499 nfs_cancel_commit_list(&head);
1501 spin_lock(&nfsi->req_lock);
1504 pages += nfs_scan_commit(inode, &head, 0, 0);
1505 spin_unlock(&nfsi->req_lock);
1506 ret = nfs_commit_list(inode, &head, how);
1507 spin_lock(&nfsi->req_lock);
1509 spin_unlock(&nfsi->req_lock);
1514 * flush the inode to disk.
1516 int nfs_wb_all(struct inode *inode)
1518 struct address_space *mapping = inode->i_mapping;
1519 struct writeback_control wbc = {
1520 .bdi = mapping->backing_dev_info,
1521 .sync_mode = WB_SYNC_ALL,
1522 .nr_to_write = LONG_MAX,
1527 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1533 int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1535 struct writeback_control wbc = {
1536 .bdi = mapping->backing_dev_info,
1537 .sync_mode = WB_SYNC_ALL,
1538 .nr_to_write = LONG_MAX,
1539 .range_start = range_start,
1540 .range_end = range_end,
1544 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1550 static int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
1552 loff_t range_start = page_offset(page);
1553 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1555 return nfs_sync_mapping_range(inode->i_mapping, range_start, range_end, how | FLUSH_STABLE);
1559 * Write back all requests on one page - we do this before reading it.
1561 int nfs_wb_page(struct inode *inode, struct page* page)
1563 return nfs_wb_page_priority(inode, page, 0);
1567 int __init nfs_init_writepagecache(void)
1569 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1570 sizeof(struct nfs_write_data),
1571 0, SLAB_HWCACHE_ALIGN,
1573 if (nfs_wdata_cachep == NULL)
1576 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1578 if (nfs_wdata_mempool == NULL)
1581 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1583 if (nfs_commit_mempool == NULL)
1589 void nfs_destroy_writepagecache(void)
1591 mempool_destroy(nfs_commit_mempool);
1592 mempool_destroy(nfs_wdata_mempool);
1593 kmem_cache_destroy(nfs_wdata_cachep);