NFS: More cleanups of fs/nfs/write.c
[firewire-audio.git] / fs / nfs / write.c
blobde9a16a8f7e4bdcf0ea6d4ec4ece01c0e7ff57f2
1 /*
2 * linux/fs/nfs/write.c
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
27 * cases:
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>
51 #include <linux/mm.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"
66 #include "internal.h"
67 #include "iostat.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*,
78 struct page *,
79 unsigned int, unsigned int);
80 static int nfs_wait_on_write_congestion(struct address_space *, int);
81 static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
82 static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how);
83 static int nfs_wb_page_priority(struct inode *inode, struct page *page, int how);
84 static const struct rpc_call_ops nfs_write_partial_ops;
85 static const struct rpc_call_ops nfs_write_full_ops;
86 static const struct rpc_call_ops nfs_commit_ops;
88 static kmem_cache_t *nfs_wdata_cachep;
89 static mempool_t *nfs_wdata_mempool;
90 static mempool_t *nfs_commit_mempool;
92 static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
94 struct nfs_write_data *nfs_commit_alloc(void)
96 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
98 if (p) {
99 memset(p, 0, sizeof(*p));
100 INIT_LIST_HEAD(&p->pages);
102 return p;
105 void nfs_commit_rcu_free(struct rcu_head *head)
107 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
108 if (p && (p->pagevec != &p->page_array[0]))
109 kfree(p->pagevec);
110 mempool_free(p, nfs_commit_mempool);
113 void nfs_commit_free(struct nfs_write_data *wdata)
115 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
118 struct nfs_write_data *nfs_writedata_alloc(size_t len)
120 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
121 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
123 if (p) {
124 memset(p, 0, sizeof(*p));
125 INIT_LIST_HEAD(&p->pages);
126 p->npages = pagecount;
127 if (pagecount <= ARRAY_SIZE(p->page_array))
128 p->pagevec = p->page_array;
129 else {
130 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
131 if (!p->pagevec) {
132 mempool_free(p, nfs_wdata_mempool);
133 p = NULL;
137 return p;
140 static void nfs_writedata_rcu_free(struct rcu_head *head)
142 struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
143 if (p && (p->pagevec != &p->page_array[0]))
144 kfree(p->pagevec);
145 mempool_free(p, nfs_wdata_mempool);
148 static void nfs_writedata_free(struct nfs_write_data *wdata)
150 call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
153 void nfs_writedata_release(void *wdata)
155 nfs_writedata_free(wdata);
158 static struct nfs_page *nfs_page_find_request_locked(struct page *page)
160 struct nfs_page *req = NULL;
162 if (PagePrivate(page)) {
163 req = (struct nfs_page *)page_private(page);
164 if (req != NULL)
165 atomic_inc(&req->wb_count);
167 return req;
170 static struct nfs_page *nfs_page_find_request(struct page *page)
172 struct nfs_page *req = NULL;
173 spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
175 spin_lock(req_lock);
176 req = nfs_page_find_request_locked(page);
177 spin_unlock(req_lock);
178 return req;
181 /* Adjust the file length if we're writing beyond the end */
182 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
184 struct inode *inode = page->mapping->host;
185 loff_t end, i_size = i_size_read(inode);
186 unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
188 if (i_size > 0 && page->index < end_index)
189 return;
190 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
191 if (i_size >= end)
192 return;
193 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
194 i_size_write(inode, end);
197 /* We can set the PG_uptodate flag if we see that a write request
198 * covers the full page.
200 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
202 if (PageUptodate(page))
203 return;
204 if (base != 0)
205 return;
206 if (count != nfs_page_length(page))
207 return;
208 if (count != PAGE_CACHE_SIZE)
209 memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
210 SetPageUptodate(page);
214 * Write a page synchronously.
215 * Offset is the data offset within the page.
217 static int nfs_writepage_sync(struct nfs_open_context *ctx, struct page *page,
218 unsigned int offset, unsigned int count, int how)
220 struct inode *inode = page->mapping->host;
221 unsigned int wsize = NFS_SERVER(inode)->wsize;
222 int result, written = 0;
223 struct nfs_write_data *wdata;
225 wdata = nfs_writedata_alloc(wsize);
226 if (!wdata)
227 return -ENOMEM;
229 wdata->flags = how;
230 wdata->cred = ctx->cred;
231 wdata->inode = inode;
232 wdata->args.fh = NFS_FH(inode);
233 wdata->args.context = ctx;
234 wdata->args.pages = &page;
235 wdata->args.stable = NFS_FILE_SYNC;
236 wdata->args.pgbase = offset;
237 wdata->args.count = wsize;
238 wdata->res.fattr = &wdata->fattr;
239 wdata->res.verf = &wdata->verf;
241 dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
242 inode->i_sb->s_id,
243 (long long)NFS_FILEID(inode),
244 count, (long long)(page_offset(page) + offset));
246 set_page_writeback(page);
247 nfs_begin_data_update(inode);
248 do {
249 if (count < wsize)
250 wdata->args.count = count;
251 wdata->args.offset = page_offset(page) + wdata->args.pgbase;
253 result = NFS_PROTO(inode)->write(wdata);
255 if (result < 0) {
256 /* Must mark the page invalid after I/O error */
257 ClearPageUptodate(page);
258 goto io_error;
260 if (result < wdata->args.count)
261 printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
262 wdata->args.count, result);
264 wdata->args.offset += result;
265 wdata->args.pgbase += result;
266 written += result;
267 count -= result;
268 nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
269 } while (count);
270 /* Update file length */
271 nfs_grow_file(page, offset, written);
272 /* Set the PG_uptodate flag? */
273 nfs_mark_uptodate(page, offset, written);
275 if (PageError(page))
276 ClearPageError(page);
278 io_error:
279 nfs_end_data_update(inode);
280 end_page_writeback(page);
281 nfs_writedata_release(wdata);
282 return written ? written : result;
285 static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
286 unsigned int offset, unsigned int count)
288 struct nfs_page *req;
289 int ret;
291 for (;;) {
292 req = nfs_update_request(ctx, page, offset, count);
293 if (!IS_ERR(req))
294 break;
295 ret = PTR_ERR(req);
296 if (ret != -EBUSY)
297 return ret;
298 ret = nfs_wb_page(page->mapping->host, page);
299 if (ret != 0)
300 return ret;
302 /* Update file length */
303 nfs_grow_file(page, offset, count);
304 /* Set the PG_uptodate flag? */
305 nfs_mark_uptodate(page, offset, count);
306 nfs_unlock_request(req);
307 return 0;
310 static int wb_priority(struct writeback_control *wbc)
312 if (wbc->for_reclaim)
313 return FLUSH_HIGHPRI;
314 if (wbc->for_kupdate)
315 return FLUSH_LOWPRI;
316 return 0;
320 * Write an mmapped page to the server.
322 int nfs_writepage(struct page *page, struct writeback_control *wbc)
324 struct nfs_open_context *ctx;
325 struct inode *inode = page->mapping->host;
326 unsigned offset;
327 int err;
329 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
330 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
332 /* Ensure we've flushed out any previous writes */
333 nfs_wb_page_priority(inode, page, wb_priority(wbc));
335 err = 0;
336 offset = nfs_page_length(page);
337 if (!offset)
338 goto out;
340 ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
341 if (ctx == NULL) {
342 err = -EBADF;
343 goto out;
345 lock_kernel();
346 if (!IS_SYNC(inode)) {
347 err = nfs_writepage_setup(ctx, page, 0, offset);
348 if (!wbc->for_writepages)
349 nfs_flush_mapping(page->mapping, wbc, wb_priority(wbc));
350 } else {
351 err = nfs_writepage_sync(ctx, page, 0, offset, wb_priority(wbc));
352 if (err >= 0) {
353 if (err != offset)
354 redirty_page_for_writepage(wbc, page);
355 err = 0;
358 unlock_kernel();
359 put_nfs_open_context(ctx);
360 out:
361 unlock_page(page);
362 return err;
366 * Note: causes nfs_update_request() to block on the assumption
367 * that the writeback is generated due to memory pressure.
369 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
371 struct backing_dev_info *bdi = mapping->backing_dev_info;
372 struct inode *inode = mapping->host;
373 int err;
375 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
377 err = generic_writepages(mapping, wbc);
378 if (err)
379 return err;
380 while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
381 if (wbc->nonblocking)
382 return 0;
383 nfs_wait_on_write_congestion(mapping, 0);
385 err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
386 if (err < 0)
387 goto out;
388 nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
389 if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
390 err = nfs_wait_on_requests(inode, 0, 0);
391 if (err < 0)
392 goto out;
394 err = nfs_commit_inode(inode, wb_priority(wbc));
395 if (err > 0)
396 err = 0;
397 out:
398 clear_bit(BDI_write_congested, &bdi->state);
399 wake_up_all(&nfs_write_congestion);
400 congestion_end(WRITE);
401 return err;
405 * Insert a write request into an inode
407 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
409 struct nfs_inode *nfsi = NFS_I(inode);
410 int error;
412 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
413 BUG_ON(error == -EEXIST);
414 if (error)
415 return error;
416 if (!nfsi->npages) {
417 igrab(inode);
418 nfs_begin_data_update(inode);
419 if (nfs_have_delegation(inode, FMODE_WRITE))
420 nfsi->change_attr++;
422 SetPagePrivate(req->wb_page);
423 set_page_private(req->wb_page, (unsigned long)req);
424 nfsi->npages++;
425 atomic_inc(&req->wb_count);
426 return 0;
430 * Insert a write request into an inode
432 static void nfs_inode_remove_request(struct nfs_page *req)
434 struct inode *inode = req->wb_context->dentry->d_inode;
435 struct nfs_inode *nfsi = NFS_I(inode);
437 BUG_ON (!NFS_WBACK_BUSY(req));
439 spin_lock(&nfsi->req_lock);
440 set_page_private(req->wb_page, 0);
441 ClearPagePrivate(req->wb_page);
442 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
443 nfsi->npages--;
444 if (!nfsi->npages) {
445 spin_unlock(&nfsi->req_lock);
446 nfs_end_data_update(inode);
447 iput(inode);
448 } else
449 spin_unlock(&nfsi->req_lock);
450 nfs_clear_request(req);
451 nfs_release_request(req);
455 * Add a request to the inode's dirty list.
457 static void
458 nfs_mark_request_dirty(struct nfs_page *req)
460 struct inode *inode = req->wb_context->dentry->d_inode;
461 struct nfs_inode *nfsi = NFS_I(inode);
463 spin_lock(&nfsi->req_lock);
464 radix_tree_tag_set(&nfsi->nfs_page_tree,
465 req->wb_index, NFS_PAGE_TAG_DIRTY);
466 nfs_list_add_request(req, &nfsi->dirty);
467 nfsi->ndirty++;
468 spin_unlock(&nfsi->req_lock);
469 inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
470 mark_inode_dirty(inode);
474 * Check if a request is dirty
476 static inline int
477 nfs_dirty_request(struct nfs_page *req)
479 struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
480 return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
483 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
485 * Add a request to the inode's commit list.
487 static void
488 nfs_mark_request_commit(struct nfs_page *req)
490 struct inode *inode = req->wb_context->dentry->d_inode;
491 struct nfs_inode *nfsi = NFS_I(inode);
493 spin_lock(&nfsi->req_lock);
494 nfs_list_add_request(req, &nfsi->commit);
495 nfsi->ncommit++;
496 spin_unlock(&nfsi->req_lock);
497 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
498 mark_inode_dirty(inode);
500 #endif
503 * Wait for a request to complete.
505 * Interruptible by signals only if mounted with intr flag.
507 static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
509 struct nfs_inode *nfsi = NFS_I(inode);
510 struct nfs_page *req;
511 unsigned long idx_end, next;
512 unsigned int res = 0;
513 int error;
515 if (npages == 0)
516 idx_end = ~0;
517 else
518 idx_end = idx_start + npages - 1;
520 next = idx_start;
521 while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
522 if (req->wb_index > idx_end)
523 break;
525 next = req->wb_index + 1;
526 BUG_ON(!NFS_WBACK_BUSY(req));
528 atomic_inc(&req->wb_count);
529 spin_unlock(&nfsi->req_lock);
530 error = nfs_wait_on_request(req);
531 nfs_release_request(req);
532 spin_lock(&nfsi->req_lock);
533 if (error < 0)
534 return error;
535 res++;
537 return res;
540 static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
542 struct nfs_inode *nfsi = NFS_I(inode);
543 int ret;
545 spin_lock(&nfsi->req_lock);
546 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
547 spin_unlock(&nfsi->req_lock);
548 return ret;
551 static void nfs_cancel_dirty_list(struct list_head *head)
553 struct nfs_page *req;
554 while(!list_empty(head)) {
555 req = nfs_list_entry(head->next);
556 nfs_list_remove_request(req);
557 nfs_inode_remove_request(req);
558 nfs_clear_page_writeback(req);
562 static void nfs_cancel_commit_list(struct list_head *head)
564 struct nfs_page *req;
566 while(!list_empty(head)) {
567 req = nfs_list_entry(head->next);
568 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
569 nfs_list_remove_request(req);
570 nfs_inode_remove_request(req);
571 nfs_unlock_request(req);
575 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
577 * nfs_scan_commit - Scan an inode for commit requests
578 * @inode: NFS inode to scan
579 * @dst: destination list
580 * @idx_start: lower bound of page->index to scan.
581 * @npages: idx_start + npages sets the upper bound to scan.
583 * Moves requests from the inode's 'commit' request list.
584 * The requests are *not* checked to ensure that they form a contiguous set.
586 static int
587 nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
589 struct nfs_inode *nfsi = NFS_I(inode);
590 int res = 0;
592 if (nfsi->ncommit != 0) {
593 res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
594 nfsi->ncommit -= res;
595 if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
596 printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
598 return res;
600 #else
601 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
603 return 0;
605 #endif
607 static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
609 struct backing_dev_info *bdi = mapping->backing_dev_info;
610 DEFINE_WAIT(wait);
611 int ret = 0;
613 might_sleep();
615 if (!bdi_write_congested(bdi))
616 return 0;
618 nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
620 if (intr) {
621 struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
622 sigset_t oldset;
624 rpc_clnt_sigmask(clnt, &oldset);
625 prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
626 if (bdi_write_congested(bdi)) {
627 if (signalled())
628 ret = -ERESTARTSYS;
629 else
630 schedule();
632 rpc_clnt_sigunmask(clnt, &oldset);
633 } else {
634 prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
635 if (bdi_write_congested(bdi))
636 schedule();
638 finish_wait(&nfs_write_congestion, &wait);
639 return ret;
644 * Try to update any existing write request, or create one if there is none.
645 * In order to match, the request's credentials must match those of
646 * the calling process.
648 * Note: Should always be called with the Page Lock held!
650 static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
651 struct page *page, unsigned int offset, unsigned int bytes)
653 struct inode *inode = page->mapping->host;
654 struct nfs_inode *nfsi = NFS_I(inode);
655 struct nfs_page *req, *new = NULL;
656 unsigned long rqend, end;
658 end = offset + bytes;
660 if (nfs_wait_on_write_congestion(page->mapping, NFS_SERVER(inode)->flags & NFS_MOUNT_INTR))
661 return ERR_PTR(-ERESTARTSYS);
662 for (;;) {
663 /* Loop over all inode entries and see if we find
664 * A request for the page we wish to update
666 spin_lock(&nfsi->req_lock);
667 req = nfs_page_find_request_locked(page);
668 if (req) {
669 if (!nfs_lock_request_dontget(req)) {
670 int error;
672 spin_unlock(&nfsi->req_lock);
673 error = nfs_wait_on_request(req);
674 nfs_release_request(req);
675 if (error < 0) {
676 if (new)
677 nfs_release_request(new);
678 return ERR_PTR(error);
680 continue;
682 spin_unlock(&nfsi->req_lock);
683 if (new)
684 nfs_release_request(new);
685 break;
688 if (new) {
689 int error;
690 nfs_lock_request_dontget(new);
691 error = nfs_inode_add_request(inode, new);
692 if (error) {
693 spin_unlock(&nfsi->req_lock);
694 nfs_unlock_request(new);
695 return ERR_PTR(error);
697 spin_unlock(&nfsi->req_lock);
698 nfs_mark_request_dirty(new);
699 return new;
701 spin_unlock(&nfsi->req_lock);
703 new = nfs_create_request(ctx, inode, page, offset, bytes);
704 if (IS_ERR(new))
705 return new;
708 /* We have a request for our page.
709 * If the creds don't match, or the
710 * page addresses don't match,
711 * tell the caller to wait on the conflicting
712 * request.
714 rqend = req->wb_offset + req->wb_bytes;
715 if (req->wb_context != ctx
716 || req->wb_page != page
717 || !nfs_dirty_request(req)
718 || offset > rqend || end < req->wb_offset) {
719 nfs_unlock_request(req);
720 return ERR_PTR(-EBUSY);
723 /* Okay, the request matches. Update the region */
724 if (offset < req->wb_offset) {
725 req->wb_offset = offset;
726 req->wb_pgbase = offset;
727 req->wb_bytes = rqend - req->wb_offset;
730 if (end > rqend)
731 req->wb_bytes = end - req->wb_offset;
733 return req;
736 int nfs_flush_incompatible(struct file *file, struct page *page)
738 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
739 struct nfs_page *req;
740 int status = 0;
742 * Look for a request corresponding to this page. If there
743 * is one, and it belongs to another file, we flush it out
744 * before we try to copy anything into the page. Do this
745 * due to the lack of an ACCESS-type call in NFSv2.
746 * Also do the same if we find a request from an existing
747 * dropped page.
749 req = nfs_page_find_request(page);
750 if (req != NULL) {
751 int do_flush = req->wb_page != page || req->wb_context != ctx;
753 nfs_release_request(req);
754 if (do_flush)
755 status = nfs_wb_page(page->mapping->host, page);
757 return (status < 0) ? status : 0;
761 * Update and possibly write a cached page of an NFS file.
763 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
764 * things with a page scheduled for an RPC call (e.g. invalidate it).
766 int nfs_updatepage(struct file *file, struct page *page,
767 unsigned int offset, unsigned int count)
769 struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
770 struct inode *inode = page->mapping->host;
771 int status = 0;
773 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
775 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
776 file->f_dentry->d_parent->d_name.name,
777 file->f_dentry->d_name.name, count,
778 (long long)(page_offset(page) +offset));
780 if (IS_SYNC(inode)) {
781 status = nfs_writepage_sync(ctx, page, offset, count, 0);
782 if (status > 0) {
783 if (offset == 0 && status == PAGE_CACHE_SIZE)
784 SetPageUptodate(page);
785 return 0;
787 return status;
790 /* If we're not using byte range locks, and we know the page
791 * is entirely in cache, it may be more efficient to avoid
792 * fragmenting write requests.
794 if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
795 count = max(count + offset, nfs_page_length(page));
796 offset = 0;
799 status = nfs_writepage_setup(ctx, page, offset, count);
801 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
802 status, (long long)i_size_read(inode));
803 if (status < 0)
804 ClearPageUptodate(page);
805 return status;
808 static void nfs_writepage_release(struct nfs_page *req)
810 end_page_writeback(req->wb_page);
812 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
813 if (!PageError(req->wb_page)) {
814 if (NFS_NEED_RESCHED(req)) {
815 nfs_mark_request_dirty(req);
816 goto out;
817 } else if (NFS_NEED_COMMIT(req)) {
818 nfs_mark_request_commit(req);
819 goto out;
822 nfs_inode_remove_request(req);
824 out:
825 nfs_clear_commit(req);
826 nfs_clear_reschedule(req);
827 #else
828 nfs_inode_remove_request(req);
829 #endif
830 nfs_clear_page_writeback(req);
833 static inline int flush_task_priority(int how)
835 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
836 case FLUSH_HIGHPRI:
837 return RPC_PRIORITY_HIGH;
838 case FLUSH_LOWPRI:
839 return RPC_PRIORITY_LOW;
841 return RPC_PRIORITY_NORMAL;
845 * Set up the argument/result storage required for the RPC call.
847 static void nfs_write_rpcsetup(struct nfs_page *req,
848 struct nfs_write_data *data,
849 const struct rpc_call_ops *call_ops,
850 unsigned int count, unsigned int offset,
851 int how)
853 struct inode *inode;
854 int flags;
856 /* Set up the RPC argument and reply structs
857 * NB: take care not to mess about with data->commit et al. */
859 data->req = req;
860 data->inode = inode = req->wb_context->dentry->d_inode;
861 data->cred = req->wb_context->cred;
863 data->args.fh = NFS_FH(inode);
864 data->args.offset = req_offset(req) + offset;
865 data->args.pgbase = req->wb_pgbase + offset;
866 data->args.pages = data->pagevec;
867 data->args.count = count;
868 data->args.context = req->wb_context;
870 data->res.fattr = &data->fattr;
871 data->res.count = count;
872 data->res.verf = &data->verf;
873 nfs_fattr_init(&data->fattr);
875 /* Set up the initial task struct. */
876 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
877 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
878 NFS_PROTO(inode)->write_setup(data, how);
880 data->task.tk_priority = flush_task_priority(how);
881 data->task.tk_cookie = (unsigned long)inode;
883 dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
884 data->task.tk_pid,
885 inode->i_sb->s_id,
886 (long long)NFS_FILEID(inode),
887 count,
888 (unsigned long long)data->args.offset);
891 static void nfs_execute_write(struct nfs_write_data *data)
893 struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
894 sigset_t oldset;
896 rpc_clnt_sigmask(clnt, &oldset);
897 rpc_execute(&data->task);
898 rpc_clnt_sigunmask(clnt, &oldset);
902 * Generate multiple small requests to write out a single
903 * contiguous dirty area on one page.
905 static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
907 struct nfs_page *req = nfs_list_entry(head->next);
908 struct page *page = req->wb_page;
909 struct nfs_write_data *data;
910 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
911 unsigned int offset;
912 int requests = 0;
913 LIST_HEAD(list);
915 nfs_list_remove_request(req);
917 nbytes = req->wb_bytes;
918 do {
919 size_t len = min(nbytes, wsize);
921 data = nfs_writedata_alloc(len);
922 if (!data)
923 goto out_bad;
924 list_add(&data->pages, &list);
925 requests++;
926 nbytes -= len;
927 } while (nbytes != 0);
928 atomic_set(&req->wb_complete, requests);
930 ClearPageError(page);
931 set_page_writeback(page);
932 offset = 0;
933 nbytes = req->wb_bytes;
934 do {
935 data = list_entry(list.next, struct nfs_write_data, pages);
936 list_del_init(&data->pages);
938 data->pagevec[0] = page;
940 if (nbytes > wsize) {
941 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
942 wsize, offset, how);
943 offset += wsize;
944 nbytes -= wsize;
945 } else {
946 nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
947 nbytes, offset, how);
948 nbytes = 0;
950 nfs_execute_write(data);
951 } while (nbytes != 0);
953 return 0;
955 out_bad:
956 while (!list_empty(&list)) {
957 data = list_entry(list.next, struct nfs_write_data, pages);
958 list_del(&data->pages);
959 nfs_writedata_release(data);
961 nfs_mark_request_dirty(req);
962 nfs_clear_page_writeback(req);
963 return -ENOMEM;
967 * Create an RPC task for the given write request and kick it.
968 * The page must have been locked by the caller.
970 * It may happen that the page we're passed is not marked dirty.
971 * This is the case if nfs_updatepage detects a conflicting request
972 * that has been written but not committed.
974 static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
976 struct nfs_page *req;
977 struct page **pages;
978 struct nfs_write_data *data;
979 unsigned int count;
981 data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
982 if (!data)
983 goto out_bad;
985 pages = data->pagevec;
986 count = 0;
987 while (!list_empty(head)) {
988 req = nfs_list_entry(head->next);
989 nfs_list_remove_request(req);
990 nfs_list_add_request(req, &data->pages);
991 ClearPageError(req->wb_page);
992 set_page_writeback(req->wb_page);
993 *pages++ = req->wb_page;
994 count += req->wb_bytes;
996 req = nfs_list_entry(data->pages.next);
998 /* Set up the argument struct */
999 nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
1001 nfs_execute_write(data);
1002 return 0;
1003 out_bad:
1004 while (!list_empty(head)) {
1005 struct nfs_page *req = nfs_list_entry(head->next);
1006 nfs_list_remove_request(req);
1007 nfs_mark_request_dirty(req);
1008 nfs_clear_page_writeback(req);
1010 return -ENOMEM;
1013 static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
1015 LIST_HEAD(one_request);
1016 int (*flush_one)(struct inode *, struct list_head *, int);
1017 struct nfs_page *req;
1018 int wpages = NFS_SERVER(inode)->wpages;
1019 int wsize = NFS_SERVER(inode)->wsize;
1020 int error;
1022 flush_one = nfs_flush_one;
1023 if (wsize < PAGE_CACHE_SIZE)
1024 flush_one = nfs_flush_multi;
1025 /* For single writes, FLUSH_STABLE is more efficient */
1026 if (npages <= wpages && npages == NFS_I(inode)->npages
1027 && nfs_list_entry(head->next)->wb_bytes <= wsize)
1028 how |= FLUSH_STABLE;
1030 do {
1031 nfs_coalesce_requests(head, &one_request, wpages);
1032 req = nfs_list_entry(one_request.next);
1033 error = flush_one(inode, &one_request, how);
1034 if (error < 0)
1035 goto out_err;
1036 } while (!list_empty(head));
1037 return 0;
1038 out_err:
1039 while (!list_empty(head)) {
1040 req = nfs_list_entry(head->next);
1041 nfs_list_remove_request(req);
1042 nfs_mark_request_dirty(req);
1043 nfs_clear_page_writeback(req);
1045 return error;
1049 * Handle a write reply that flushed part of a page.
1051 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
1053 struct nfs_write_data *data = calldata;
1054 struct nfs_page *req = data->req;
1055 struct page *page = req->wb_page;
1057 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1058 req->wb_context->dentry->d_inode->i_sb->s_id,
1059 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1060 req->wb_bytes,
1061 (long long)req_offset(req));
1063 if (nfs_writeback_done(task, data) != 0)
1064 return;
1066 if (task->tk_status < 0) {
1067 ClearPageUptodate(page);
1068 SetPageError(page);
1069 req->wb_context->error = task->tk_status;
1070 dprintk(", error = %d\n", task->tk_status);
1071 } else {
1072 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1073 if (data->verf.committed < NFS_FILE_SYNC) {
1074 if (!NFS_NEED_COMMIT(req)) {
1075 nfs_defer_commit(req);
1076 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1077 dprintk(" defer commit\n");
1078 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1079 nfs_defer_reschedule(req);
1080 dprintk(" server reboot detected\n");
1082 } else
1083 #endif
1084 dprintk(" OK\n");
1087 if (atomic_dec_and_test(&req->wb_complete))
1088 nfs_writepage_release(req);
1091 static const struct rpc_call_ops nfs_write_partial_ops = {
1092 .rpc_call_done = nfs_writeback_done_partial,
1093 .rpc_release = nfs_writedata_release,
1097 * Handle a write reply that flushes a whole page.
1099 * FIXME: There is an inherent race with invalidate_inode_pages and
1100 * writebacks since the page->count is kept > 1 for as long
1101 * as the page has a write request pending.
1103 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1105 struct nfs_write_data *data = calldata;
1106 struct nfs_page *req;
1107 struct page *page;
1109 if (nfs_writeback_done(task, data) != 0)
1110 return;
1112 /* Update attributes as result of writeback. */
1113 while (!list_empty(&data->pages)) {
1114 req = nfs_list_entry(data->pages.next);
1115 nfs_list_remove_request(req);
1116 page = req->wb_page;
1118 dprintk("NFS: write (%s/%Ld %d@%Ld)",
1119 req->wb_context->dentry->d_inode->i_sb->s_id,
1120 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1121 req->wb_bytes,
1122 (long long)req_offset(req));
1124 if (task->tk_status < 0) {
1125 ClearPageUptodate(page);
1126 SetPageError(page);
1127 req->wb_context->error = task->tk_status;
1128 end_page_writeback(page);
1129 nfs_inode_remove_request(req);
1130 dprintk(", error = %d\n", task->tk_status);
1131 goto next;
1133 end_page_writeback(page);
1135 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1136 if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
1137 nfs_inode_remove_request(req);
1138 dprintk(" OK\n");
1139 goto next;
1141 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1142 nfs_mark_request_commit(req);
1143 dprintk(" marked for commit\n");
1144 #else
1145 nfs_inode_remove_request(req);
1146 #endif
1147 next:
1148 nfs_clear_page_writeback(req);
1152 static const struct rpc_call_ops nfs_write_full_ops = {
1153 .rpc_call_done = nfs_writeback_done_full,
1154 .rpc_release = nfs_writedata_release,
1159 * This function is called when the WRITE call is complete.
1161 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1163 struct nfs_writeargs *argp = &data->args;
1164 struct nfs_writeres *resp = &data->res;
1165 int status;
1167 dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
1168 task->tk_pid, task->tk_status);
1171 * ->write_done will attempt to use post-op attributes to detect
1172 * conflicting writes by other clients. A strict interpretation
1173 * of close-to-open would allow us to continue caching even if
1174 * another writer had changed the file, but some applications
1175 * depend on tighter cache coherency when writing.
1177 status = NFS_PROTO(data->inode)->write_done(task, data);
1178 if (status != 0)
1179 return status;
1180 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1182 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1183 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1184 /* We tried a write call, but the server did not
1185 * commit data to stable storage even though we
1186 * requested it.
1187 * Note: There is a known bug in Tru64 < 5.0 in which
1188 * the server reports NFS_DATA_SYNC, but performs
1189 * NFS_FILE_SYNC. We therefore implement this checking
1190 * as a dprintk() in order to avoid filling syslog.
1192 static unsigned long complain;
1194 if (time_before(complain, jiffies)) {
1195 dprintk("NFS: faulty NFS server %s:"
1196 " (committed = %d) != (stable = %d)\n",
1197 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1198 resp->verf->committed, argp->stable);
1199 complain = jiffies + 300 * HZ;
1202 #endif
1203 /* Is this a short write? */
1204 if (task->tk_status >= 0 && resp->count < argp->count) {
1205 static unsigned long complain;
1207 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1209 /* Has the server at least made some progress? */
1210 if (resp->count != 0) {
1211 /* Was this an NFSv2 write or an NFSv3 stable write? */
1212 if (resp->verf->committed != NFS_UNSTABLE) {
1213 /* Resend from where the server left off */
1214 argp->offset += resp->count;
1215 argp->pgbase += resp->count;
1216 argp->count -= resp->count;
1217 } else {
1218 /* Resend as a stable write in order to avoid
1219 * headaches in the case of a server crash.
1221 argp->stable = NFS_FILE_SYNC;
1223 rpc_restart_call(task);
1224 return -EAGAIN;
1226 if (time_before(complain, jiffies)) {
1227 printk(KERN_WARNING
1228 "NFS: Server wrote zero bytes, expected %u.\n",
1229 argp->count);
1230 complain = jiffies + 300 * HZ;
1232 /* Can't do anything about it except throw an error. */
1233 task->tk_status = -EIO;
1235 return 0;
1239 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1240 void nfs_commit_release(void *wdata)
1242 nfs_commit_free(wdata);
1246 * Set up the argument/result storage required for the RPC call.
1248 static void nfs_commit_rpcsetup(struct list_head *head,
1249 struct nfs_write_data *data,
1250 int how)
1252 struct nfs_page *first;
1253 struct inode *inode;
1254 int flags;
1256 /* Set up the RPC argument and reply structs
1257 * NB: take care not to mess about with data->commit et al. */
1259 list_splice_init(head, &data->pages);
1260 first = nfs_list_entry(data->pages.next);
1261 inode = first->wb_context->dentry->d_inode;
1263 data->inode = inode;
1264 data->cred = first->wb_context->cred;
1266 data->args.fh = NFS_FH(data->inode);
1267 /* Note: we always request a commit of the entire inode */
1268 data->args.offset = 0;
1269 data->args.count = 0;
1270 data->res.count = 0;
1271 data->res.fattr = &data->fattr;
1272 data->res.verf = &data->verf;
1273 nfs_fattr_init(&data->fattr);
1275 /* Set up the initial task struct. */
1276 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1277 rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
1278 NFS_PROTO(inode)->commit_setup(data, how);
1280 data->task.tk_priority = flush_task_priority(how);
1281 data->task.tk_cookie = (unsigned long)inode;
1283 dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
1287 * Commit dirty pages
1289 static int
1290 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1292 struct nfs_write_data *data;
1293 struct nfs_page *req;
1295 data = nfs_commit_alloc();
1297 if (!data)
1298 goto out_bad;
1300 /* Set up the argument struct */
1301 nfs_commit_rpcsetup(head, data, how);
1303 nfs_execute_write(data);
1304 return 0;
1305 out_bad:
1306 while (!list_empty(head)) {
1307 req = nfs_list_entry(head->next);
1308 nfs_list_remove_request(req);
1309 nfs_mark_request_commit(req);
1310 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1311 nfs_clear_page_writeback(req);
1313 return -ENOMEM;
1317 * COMMIT call returned
1319 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1321 struct nfs_write_data *data = calldata;
1322 struct nfs_page *req;
1324 dprintk("NFS: %4d nfs_commit_done (status %d)\n",
1325 task->tk_pid, task->tk_status);
1327 /* Call the NFS version-specific code */
1328 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1329 return;
1331 while (!list_empty(&data->pages)) {
1332 req = nfs_list_entry(data->pages.next);
1333 nfs_list_remove_request(req);
1334 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1336 dprintk("NFS: commit (%s/%Ld %d@%Ld)",
1337 req->wb_context->dentry->d_inode->i_sb->s_id,
1338 (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
1339 req->wb_bytes,
1340 (long long)req_offset(req));
1341 if (task->tk_status < 0) {
1342 req->wb_context->error = task->tk_status;
1343 nfs_inode_remove_request(req);
1344 dprintk(", error = %d\n", task->tk_status);
1345 goto next;
1348 /* Okay, COMMIT succeeded, apparently. Check the verifier
1349 * returned by the server against all stored verfs. */
1350 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1351 /* We have a match */
1352 nfs_inode_remove_request(req);
1353 dprintk(" OK\n");
1354 goto next;
1356 /* We have a mismatch. Write the page again */
1357 dprintk(" mismatch\n");
1358 nfs_mark_request_dirty(req);
1359 next:
1360 nfs_clear_page_writeback(req);
1364 static const struct rpc_call_ops nfs_commit_ops = {
1365 .rpc_call_done = nfs_commit_done,
1366 .rpc_release = nfs_commit_release,
1368 #else
1369 static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1371 return 0;
1373 #endif
1375 static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
1377 struct nfs_inode *nfsi = NFS_I(mapping->host);
1378 LIST_HEAD(head);
1379 long res;
1381 spin_lock(&nfsi->req_lock);
1382 res = nfs_scan_dirty(mapping, wbc, &head);
1383 spin_unlock(&nfsi->req_lock);
1384 if (res) {
1385 int error = nfs_flush_list(mapping->host, &head, res, how);
1386 if (error < 0)
1387 return error;
1389 return res;
1392 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1393 int nfs_commit_inode(struct inode *inode, int how)
1395 struct nfs_inode *nfsi = NFS_I(inode);
1396 LIST_HEAD(head);
1397 int res;
1399 spin_lock(&nfsi->req_lock);
1400 res = nfs_scan_commit(inode, &head, 0, 0);
1401 spin_unlock(&nfsi->req_lock);
1402 if (res) {
1403 int error = nfs_commit_list(inode, &head, how);
1404 if (error < 0)
1405 return error;
1407 return res;
1409 #endif
1411 long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
1413 struct inode *inode = mapping->host;
1414 struct nfs_inode *nfsi = NFS_I(inode);
1415 unsigned long idx_start, idx_end;
1416 unsigned int npages = 0;
1417 LIST_HEAD(head);
1418 int nocommit = how & FLUSH_NOCOMMIT;
1419 long pages, ret;
1421 /* FIXME */
1422 if (wbc->range_cyclic)
1423 idx_start = 0;
1424 else {
1425 idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
1426 idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
1427 if (idx_end > idx_start) {
1428 unsigned long l_npages = 1 + idx_end - idx_start;
1429 npages = l_npages;
1430 if (sizeof(npages) != sizeof(l_npages) &&
1431 (unsigned long)npages != l_npages)
1432 npages = 0;
1435 how &= ~FLUSH_NOCOMMIT;
1436 spin_lock(&nfsi->req_lock);
1437 do {
1438 wbc->pages_skipped = 0;
1439 ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1440 if (ret != 0)
1441 continue;
1442 pages = nfs_scan_dirty(mapping, wbc, &head);
1443 if (pages != 0) {
1444 spin_unlock(&nfsi->req_lock);
1445 if (how & FLUSH_INVALIDATE) {
1446 nfs_cancel_dirty_list(&head);
1447 ret = pages;
1448 } else
1449 ret = nfs_flush_list(inode, &head, pages, how);
1450 spin_lock(&nfsi->req_lock);
1451 continue;
1453 if (wbc->pages_skipped != 0)
1454 continue;
1455 if (nocommit)
1456 break;
1457 pages = nfs_scan_commit(inode, &head, idx_start, npages);
1458 if (pages == 0) {
1459 if (wbc->pages_skipped != 0)
1460 continue;
1461 break;
1463 if (how & FLUSH_INVALIDATE) {
1464 spin_unlock(&nfsi->req_lock);
1465 nfs_cancel_commit_list(&head);
1466 ret = pages;
1467 spin_lock(&nfsi->req_lock);
1468 continue;
1470 pages += nfs_scan_commit(inode, &head, 0, 0);
1471 spin_unlock(&nfsi->req_lock);
1472 ret = nfs_commit_list(inode, &head, how);
1473 spin_lock(&nfsi->req_lock);
1474 } while (ret >= 0);
1475 spin_unlock(&nfsi->req_lock);
1476 return ret;
1480 * flush the inode to disk.
1482 int nfs_wb_all(struct inode *inode)
1484 struct address_space *mapping = inode->i_mapping;
1485 struct writeback_control wbc = {
1486 .bdi = mapping->backing_dev_info,
1487 .sync_mode = WB_SYNC_ALL,
1488 .nr_to_write = LONG_MAX,
1489 .range_cyclic = 1,
1491 int ret;
1493 ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
1494 if (ret >= 0)
1495 return 0;
1496 return ret;
1499 int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
1501 struct writeback_control wbc = {
1502 .bdi = mapping->backing_dev_info,
1503 .sync_mode = WB_SYNC_ALL,
1504 .nr_to_write = LONG_MAX,
1505 .range_start = range_start,
1506 .range_end = range_end,
1508 int ret;
1510 ret = nfs_sync_mapping_wait(mapping, &wbc, how);
1511 if (ret >= 0)
1512 return 0;
1513 return ret;
1516 static int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
1518 loff_t range_start = page_offset(page);
1519 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1521 return nfs_sync_mapping_range(inode->i_mapping, range_start, range_end, how | FLUSH_STABLE);
1525 * Write back all requests on one page - we do this before reading it.
1527 int nfs_wb_page(struct inode *inode, struct page* page)
1529 return nfs_wb_page_priority(inode, page, 0);
1533 int __init nfs_init_writepagecache(void)
1535 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1536 sizeof(struct nfs_write_data),
1537 0, SLAB_HWCACHE_ALIGN,
1538 NULL, NULL);
1539 if (nfs_wdata_cachep == NULL)
1540 return -ENOMEM;
1542 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1543 nfs_wdata_cachep);
1544 if (nfs_wdata_mempool == NULL)
1545 return -ENOMEM;
1547 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1548 nfs_wdata_cachep);
1549 if (nfs_commit_mempool == NULL)
1550 return -ENOMEM;
1552 return 0;
1555 void nfs_destroy_writepagecache(void)
1557 mempool_destroy(nfs_commit_mempool);
1558 mempool_destroy(nfs_wdata_mempool);
1559 kmem_cache_destroy(nfs_wdata_cachep);