GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / fs / nfs / write.c
blobfedf4ab46064b9702a2bdd3969bcf7ac43af7a0a
1 /*
2 * linux/fs/nfs/write.c
4 * Write file data over NFS.
6 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
7 */
9 #include <linux/types.h>
10 #include <linux/slab.h>
11 #include <linux/mm.h>
12 #include <linux/pagemap.h>
13 #include <linux/file.h>
14 #include <linux/writeback.h>
15 #include <linux/swap.h>
16 #include <linux/migrate.h>
18 #include <linux/sunrpc/clnt.h>
19 #include <linux/nfs_fs.h>
20 #include <linux/nfs_mount.h>
21 #include <linux/nfs_page.h>
22 #include <linux/backing-dev.h>
24 #include <asm/uaccess.h>
26 #include "delegation.h"
27 #include "internal.h"
28 #include "iostat.h"
29 #include "nfs4_fs.h"
30 #include "fscache.h"
32 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
34 #define MIN_POOL_WRITE (32)
35 #define MIN_POOL_COMMIT (4)
38 * Local function declarations
40 static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc,
41 struct inode *inode, int ioflags);
42 static void nfs_redirty_request(struct nfs_page *req);
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 struct nfs_write_data *nfs_commitdata_alloc(void)
53 struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
55 if (p) {
56 memset(p, 0, sizeof(*p));
57 INIT_LIST_HEAD(&p->pages);
58 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
60 return p;
63 void nfs_commit_free(struct nfs_write_data *p)
65 if (p && (p->pagevec != &p->page_array[0]))
66 kfree(p->pagevec);
67 mempool_free(p, nfs_commit_mempool);
70 struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
72 struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
74 if (p) {
75 memset(p, 0, sizeof(*p));
76 INIT_LIST_HEAD(&p->pages);
77 p->npages = pagecount;
78 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
79 if (pagecount <= ARRAY_SIZE(p->page_array))
80 p->pagevec = p->page_array;
81 else {
82 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
83 if (!p->pagevec) {
84 mempool_free(p, nfs_wdata_mempool);
85 p = NULL;
89 return p;
92 void nfs_writedata_free(struct nfs_write_data *p)
94 if (p && (p->pagevec != &p->page_array[0]))
95 kfree(p->pagevec);
96 mempool_free(p, nfs_wdata_mempool);
99 static void nfs_writedata_release(struct nfs_write_data *wdata)
101 put_nfs_open_context(wdata->args.context);
102 nfs_writedata_free(wdata);
105 static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
107 ctx->error = error;
108 smp_wmb();
109 set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
112 static struct nfs_page *nfs_page_find_request_locked(struct page *page)
114 struct nfs_page *req = NULL;
116 if (PagePrivate(page)) {
117 req = (struct nfs_page *)page_private(page);
118 if (req != NULL)
119 kref_get(&req->wb_kref);
121 return req;
124 static struct nfs_page *nfs_page_find_request(struct page *page)
126 struct inode *inode = page->mapping->host;
127 struct nfs_page *req = NULL;
129 spin_lock(&inode->i_lock);
130 req = nfs_page_find_request_locked(page);
131 spin_unlock(&inode->i_lock);
132 return req;
135 /* Adjust the file length if we're writing beyond the end */
136 static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
138 struct inode *inode = page->mapping->host;
139 loff_t end, i_size;
140 pgoff_t end_index;
142 spin_lock(&inode->i_lock);
143 i_size = i_size_read(inode);
144 end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
145 if (i_size > 0 && page->index < end_index)
146 goto out;
147 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
148 if (i_size >= end)
149 goto out;
150 i_size_write(inode, end);
151 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
152 out:
153 spin_unlock(&inode->i_lock);
156 /* A writeback failed: mark the page as bad, and invalidate the page cache */
157 static void nfs_set_pageerror(struct page *page)
159 SetPageError(page);
160 nfs_zap_mapping(page->mapping->host, page->mapping);
163 /* We can set the PG_uptodate flag if we see that a write request
164 * covers the full page.
166 static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
168 if (PageUptodate(page))
169 return;
170 if (base != 0)
171 return;
172 if (count != nfs_page_length(page))
173 return;
174 SetPageUptodate(page);
177 static int wb_priority(struct writeback_control *wbc)
179 if (wbc->for_reclaim)
180 return FLUSH_HIGHPRI | FLUSH_STABLE;
181 if (wbc->for_kupdate || wbc->for_background)
182 return FLUSH_LOWPRI;
183 return 0;
187 * NFS congestion control
190 int nfs_congestion_kb;
192 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
193 #define NFS_CONGESTION_OFF_THRESH \
194 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
196 static int nfs_set_page_writeback(struct page *page)
198 int ret = test_set_page_writeback(page);
200 if (!ret) {
201 struct inode *inode = page->mapping->host;
202 struct nfs_server *nfss = NFS_SERVER(inode);
204 page_cache_get(page);
205 if (atomic_long_inc_return(&nfss->writeback) >
206 NFS_CONGESTION_ON_THRESH) {
207 set_bdi_congested(&nfss->backing_dev_info,
208 BLK_RW_ASYNC);
211 return ret;
214 static void nfs_end_page_writeback(struct page *page)
216 struct inode *inode = page->mapping->host;
217 struct nfs_server *nfss = NFS_SERVER(inode);
219 end_page_writeback(page);
220 page_cache_release(page);
221 if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
222 clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
225 static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock)
227 struct inode *inode = page->mapping->host;
228 struct nfs_page *req;
229 int ret;
231 spin_lock(&inode->i_lock);
232 for (;;) {
233 req = nfs_page_find_request_locked(page);
234 if (req == NULL)
235 break;
236 if (nfs_set_page_tag_locked(req))
237 break;
238 /* Note: If we hold the page lock, as is the case in nfs_writepage,
239 * then the call to nfs_set_page_tag_locked() will always
240 * succeed provided that someone hasn't already marked the
241 * request as dirty (in which case we don't care).
243 spin_unlock(&inode->i_lock);
244 if (!nonblock)
245 ret = nfs_wait_on_request(req);
246 else
247 ret = -EAGAIN;
248 nfs_release_request(req);
249 if (ret != 0)
250 return ERR_PTR(ret);
251 spin_lock(&inode->i_lock);
253 spin_unlock(&inode->i_lock);
254 return req;
258 * Find an associated nfs write request, and prepare to flush it out
259 * May return an error if the user signalled nfs_wait_on_request().
261 static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
262 struct page *page, bool nonblock)
264 struct nfs_page *req;
265 int ret = 0;
267 req = nfs_find_and_lock_request(page, nonblock);
268 if (!req)
269 goto out;
270 ret = PTR_ERR(req);
271 if (IS_ERR(req))
272 goto out;
274 ret = nfs_set_page_writeback(page);
275 BUG_ON(ret != 0);
276 BUG_ON(test_bit(PG_CLEAN, &req->wb_flags));
278 if (!nfs_pageio_add_request(pgio, req)) {
279 nfs_redirty_request(req);
280 ret = pgio->pg_error;
282 out:
283 return ret;
286 static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
288 struct inode *inode = page->mapping->host;
289 int ret;
291 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
292 nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
294 nfs_pageio_cond_complete(pgio, page->index);
295 ret = nfs_page_async_flush(pgio, page,
296 wbc->sync_mode == WB_SYNC_NONE ||
297 wbc->nonblocking != 0);
298 if (ret == -EAGAIN) {
299 redirty_page_for_writepage(wbc, page);
300 ret = 0;
302 return ret;
306 * Write an mmapped page to the server.
308 static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
310 struct nfs_pageio_descriptor pgio;
311 int err;
313 nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
314 err = nfs_do_writepage(page, wbc, &pgio);
315 nfs_pageio_complete(&pgio);
316 if (err < 0)
317 return err;
318 if (pgio.pg_error < 0)
319 return pgio.pg_error;
320 return 0;
323 int nfs_writepage(struct page *page, struct writeback_control *wbc)
325 int ret;
327 ret = nfs_writepage_locked(page, wbc);
328 unlock_page(page);
329 return ret;
332 static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
334 int ret;
336 ret = nfs_do_writepage(page, wbc, data);
337 unlock_page(page);
338 return ret;
341 int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
343 struct inode *inode = mapping->host;
344 unsigned long *bitlock = &NFS_I(inode)->flags;
345 struct nfs_pageio_descriptor pgio;
346 int err;
348 /* Stop dirtying of new pages while we sync */
349 err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
350 nfs_wait_bit_killable, TASK_KILLABLE);
351 if (err)
352 goto out_err;
354 nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
356 nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
357 err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
358 nfs_pageio_complete(&pgio);
360 clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
361 smp_mb__after_clear_bit();
362 wake_up_bit(bitlock, NFS_INO_FLUSHING);
364 if (err < 0)
365 goto out_err;
366 err = pgio.pg_error;
367 if (err < 0)
368 goto out_err;
369 return 0;
370 out_err:
371 return err;
375 * Insert a write request into an inode
377 static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
379 struct nfs_inode *nfsi = NFS_I(inode);
380 int error;
382 error = radix_tree_preload(GFP_NOFS);
383 if (error != 0)
384 goto out;
386 /* Lock the request! */
387 nfs_lock_request_dontget(req);
389 spin_lock(&inode->i_lock);
390 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
391 BUG_ON(error);
392 if (!nfsi->npages) {
393 igrab(inode);
394 if (nfs_have_delegation(inode, FMODE_WRITE))
395 nfsi->change_attr++;
397 SetPagePrivate(req->wb_page);
398 set_page_private(req->wb_page, (unsigned long)req);
399 nfsi->npages++;
400 kref_get(&req->wb_kref);
401 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
402 NFS_PAGE_TAG_LOCKED);
403 spin_unlock(&inode->i_lock);
404 radix_tree_preload_end();
405 out:
406 return error;
410 * Remove a write request from an inode
412 static void nfs_inode_remove_request(struct nfs_page *req)
414 struct inode *inode = req->wb_context->path.dentry->d_inode;
415 struct nfs_inode *nfsi = NFS_I(inode);
417 BUG_ON (!NFS_WBACK_BUSY(req));
419 spin_lock(&inode->i_lock);
420 set_page_private(req->wb_page, 0);
421 ClearPagePrivate(req->wb_page);
422 radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
423 nfsi->npages--;
424 if (!nfsi->npages) {
425 spin_unlock(&inode->i_lock);
426 iput(inode);
427 } else
428 spin_unlock(&inode->i_lock);
429 nfs_clear_request(req);
430 nfs_release_request(req);
433 static void
434 nfs_mark_request_dirty(struct nfs_page *req)
436 __set_page_dirty_nobuffers(req->wb_page);
437 __mark_inode_dirty(req->wb_page->mapping->host, I_DIRTY_DATASYNC);
440 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
442 * Add a request to the inode's commit list.
444 static void
445 nfs_mark_request_commit(struct nfs_page *req)
447 struct inode *inode = req->wb_context->path.dentry->d_inode;
448 struct nfs_inode *nfsi = NFS_I(inode);
450 spin_lock(&inode->i_lock);
451 set_bit(PG_CLEAN, &(req)->wb_flags);
452 radix_tree_tag_set(&nfsi->nfs_page_tree,
453 req->wb_index,
454 NFS_PAGE_TAG_COMMIT);
455 nfsi->ncommit++;
456 spin_unlock(&inode->i_lock);
457 inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
458 inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
459 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
462 static int
463 nfs_clear_request_commit(struct nfs_page *req)
465 struct page *page = req->wb_page;
467 if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) {
468 dec_zone_page_state(page, NR_UNSTABLE_NFS);
469 dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
470 return 1;
472 return 0;
475 static inline
476 int nfs_write_need_commit(struct nfs_write_data *data)
478 return data->verf.committed != NFS_FILE_SYNC;
481 static inline
482 int nfs_reschedule_unstable_write(struct nfs_page *req)
484 if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) {
485 nfs_mark_request_commit(req);
486 return 1;
488 if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
489 nfs_mark_request_dirty(req);
490 return 1;
492 return 0;
494 #else
495 static inline void
496 nfs_mark_request_commit(struct nfs_page *req)
500 static inline int
501 nfs_clear_request_commit(struct nfs_page *req)
503 return 0;
506 static inline
507 int nfs_write_need_commit(struct nfs_write_data *data)
509 return 0;
512 static inline
513 int nfs_reschedule_unstable_write(struct nfs_page *req)
515 return 0;
517 #endif
519 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
520 static int
521 nfs_need_commit(struct nfs_inode *nfsi)
523 return radix_tree_tagged(&nfsi->nfs_page_tree, NFS_PAGE_TAG_COMMIT);
527 * nfs_scan_commit - Scan an inode for commit requests
528 * @inode: NFS inode to scan
529 * @dst: destination list
530 * @idx_start: lower bound of page->index to scan.
531 * @npages: idx_start + npages sets the upper bound to scan.
533 * Moves requests from the inode's 'commit' request list.
534 * The requests are *not* checked to ensure that they form a contiguous set.
536 static int
537 nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
539 struct nfs_inode *nfsi = NFS_I(inode);
540 int ret;
542 if (!nfs_need_commit(nfsi))
543 return 0;
545 ret = nfs_scan_list(nfsi, dst, idx_start, npages, NFS_PAGE_TAG_COMMIT);
546 if (ret > 0)
547 nfsi->ncommit -= ret;
548 if (nfs_need_commit(NFS_I(inode)))
549 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
550 return ret;
552 #else
553 static inline int nfs_need_commit(struct nfs_inode *nfsi)
555 return 0;
558 static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
560 return 0;
562 #endif
565 * Search for an existing write request, and attempt to update
566 * it to reflect a new dirty region on a given page.
568 * If the attempt fails, then the existing request is flushed out
569 * to disk.
571 static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
572 struct page *page,
573 unsigned int offset,
574 unsigned int bytes)
576 struct nfs_page *req;
577 unsigned int rqend;
578 unsigned int end;
579 int error;
581 if (!PagePrivate(page))
582 return NULL;
584 end = offset + bytes;
585 spin_lock(&inode->i_lock);
587 for (;;) {
588 req = nfs_page_find_request_locked(page);
589 if (req == NULL)
590 goto out_unlock;
592 rqend = req->wb_offset + req->wb_bytes;
594 * Tell the caller to flush out the request if
595 * the offsets are non-contiguous.
596 * Note: nfs_flush_incompatible() will already
597 * have flushed out requests having wrong owners.
599 if (offset > rqend
600 || end < req->wb_offset)
601 goto out_flushme;
603 if (nfs_set_page_tag_locked(req))
604 break;
606 /* The request is locked, so wait and then retry */
607 spin_unlock(&inode->i_lock);
608 error = nfs_wait_on_request(req);
609 nfs_release_request(req);
610 if (error != 0)
611 goto out_err;
612 spin_lock(&inode->i_lock);
615 if (nfs_clear_request_commit(req) &&
616 radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree,
617 req->wb_index, NFS_PAGE_TAG_COMMIT) != NULL)
618 NFS_I(inode)->ncommit--;
620 /* Okay, the request matches. Update the region */
621 if (offset < req->wb_offset) {
622 req->wb_offset = offset;
623 req->wb_pgbase = offset;
625 if (end > rqend)
626 req->wb_bytes = end - req->wb_offset;
627 else
628 req->wb_bytes = rqend - req->wb_offset;
629 out_unlock:
630 spin_unlock(&inode->i_lock);
631 return req;
632 out_flushme:
633 spin_unlock(&inode->i_lock);
634 nfs_release_request(req);
635 error = nfs_wb_page(inode, page);
636 out_err:
637 return ERR_PTR(error);
641 * Try to update an existing write request, or create one if there is none.
643 * Note: Should always be called with the Page Lock held to prevent races
644 * if we have to add a new request. Also assumes that the caller has
645 * already called nfs_flush_incompatible() if necessary.
647 static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
648 struct page *page, unsigned int offset, unsigned int bytes)
650 struct inode *inode = page->mapping->host;
651 struct nfs_page *req;
652 int error;
654 req = nfs_try_to_update_request(inode, page, offset, bytes);
655 if (req != NULL)
656 goto out;
657 req = nfs_create_request(ctx, inode, page, offset, bytes);
658 if (IS_ERR(req))
659 goto out;
660 error = nfs_inode_add_request(inode, req);
661 if (error != 0) {
662 nfs_release_request(req);
663 req = ERR_PTR(error);
665 out:
666 return req;
669 static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
670 unsigned int offset, unsigned int count)
672 struct nfs_page *req;
674 req = nfs_setup_write_request(ctx, page, offset, count);
675 if (IS_ERR(req))
676 return PTR_ERR(req);
677 nfs_mark_request_dirty(req);
678 /* Update file length */
679 nfs_grow_file(page, offset, count);
680 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
681 nfs_mark_request_dirty(req);
682 nfs_clear_page_tag_locked(req);
683 return 0;
686 int nfs_flush_incompatible(struct file *file, struct page *page)
688 struct nfs_open_context *ctx = nfs_file_open_context(file);
689 struct nfs_page *req;
690 int do_flush, status;
692 * Look for a request corresponding to this page. If there
693 * is one, and it belongs to another file, we flush it out
694 * before we try to copy anything into the page. Do this
695 * due to the lack of an ACCESS-type call in NFSv2.
696 * Also do the same if we find a request from an existing
697 * dropped page.
699 do {
700 req = nfs_page_find_request(page);
701 if (req == NULL)
702 return 0;
703 do_flush = req->wb_page != page || req->wb_context != ctx ||
704 req->wb_lock_context->lockowner != current->files ||
705 req->wb_lock_context->pid != current->tgid;
706 nfs_release_request(req);
707 if (!do_flush)
708 return 0;
709 status = nfs_wb_page(page->mapping->host, page);
710 } while (status == 0);
711 return status;
715 * If the page cache is marked as unsafe or invalid, then we can't rely on
716 * the PageUptodate() flag. In this case, we will need to turn off
717 * write optimisations that depend on the page contents being correct.
719 static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
721 return PageUptodate(page) &&
722 !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
725 int nfs_updatepage(struct file *file, struct page *page,
726 unsigned int offset, unsigned int count)
728 struct nfs_open_context *ctx = nfs_file_open_context(file);
729 struct inode *inode = page->mapping->host;
730 int status = 0;
732 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
734 dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n",
735 file->f_path.dentry->d_parent->d_name.name,
736 file->f_path.dentry->d_name.name, count,
737 (long long)(page_offset(page) + offset));
739 /* If we're not using byte range locks, and we know the page
740 * is up to date, it may be more efficient to extend the write
741 * to cover the entire page in order to avoid fragmentation
742 * inefficiencies.
744 if (nfs_write_pageuptodate(page, inode) &&
745 inode->i_flock == NULL &&
746 !(file->f_flags & O_DSYNC)) {
747 count = max(count + offset, nfs_page_length(page));
748 offset = 0;
751 status = nfs_writepage_setup(ctx, page, offset, count);
752 if (status < 0)
753 nfs_set_pageerror(page);
755 dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
756 status, (long long)i_size_read(inode));
757 return status;
760 static void nfs_writepage_release(struct nfs_page *req)
762 struct page *page = req->wb_page;
764 if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req))
765 nfs_inode_remove_request(req);
766 nfs_clear_page_tag_locked(req);
767 nfs_end_page_writeback(page);
770 static int flush_task_priority(int how)
772 switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
773 case FLUSH_HIGHPRI:
774 return RPC_PRIORITY_HIGH;
775 case FLUSH_LOWPRI:
776 return RPC_PRIORITY_LOW;
778 return RPC_PRIORITY_NORMAL;
782 * Set up the argument/result storage required for the RPC call.
784 static int nfs_write_rpcsetup(struct nfs_page *req,
785 struct nfs_write_data *data,
786 const struct rpc_call_ops *call_ops,
787 unsigned int count, unsigned int offset,
788 int how)
790 struct inode *inode = req->wb_context->path.dentry->d_inode;
791 int priority = flush_task_priority(how);
792 struct rpc_task *task;
793 struct rpc_message msg = {
794 .rpc_argp = &data->args,
795 .rpc_resp = &data->res,
796 .rpc_cred = req->wb_context->cred,
798 struct rpc_task_setup task_setup_data = {
799 .rpc_client = NFS_CLIENT(inode),
800 .task = &data->task,
801 .rpc_message = &msg,
802 .callback_ops = call_ops,
803 .callback_data = data,
804 .workqueue = nfsiod_workqueue,
805 .flags = RPC_TASK_ASYNC,
806 .priority = priority,
808 int ret = 0;
810 /* Set up the RPC argument and reply structs
811 * NB: take care not to mess about with data->commit et al. */
813 data->req = req;
814 data->inode = inode = req->wb_context->path.dentry->d_inode;
815 data->cred = msg.rpc_cred;
817 data->args.fh = NFS_FH(inode);
818 data->args.offset = req_offset(req) + offset;
819 data->args.pgbase = req->wb_pgbase + offset;
820 data->args.pages = data->pagevec;
821 data->args.count = count;
822 data->args.context = get_nfs_open_context(req->wb_context);
823 data->args.lock_context = req->wb_lock_context;
824 data->args.stable = NFS_UNSTABLE;
825 if (how & FLUSH_STABLE) {
826 data->args.stable = NFS_DATA_SYNC;
827 if (!nfs_need_commit(NFS_I(inode)))
828 data->args.stable = NFS_FILE_SYNC;
831 data->res.fattr = &data->fattr;
832 data->res.count = count;
833 data->res.verf = &data->verf;
834 nfs_fattr_init(&data->fattr);
836 /* Set up the initial task struct. */
837 NFS_PROTO(inode)->write_setup(data, &msg);
839 dprintk("NFS: %5u initiated write call "
840 "(req %s/%lld, %u bytes @ offset %llu)\n",
841 data->task.tk_pid,
842 inode->i_sb->s_id,
843 (long long)NFS_FILEID(inode),
844 count,
845 (unsigned long long)data->args.offset);
847 task = rpc_run_task(&task_setup_data);
848 if (IS_ERR(task)) {
849 ret = PTR_ERR(task);
850 goto out;
852 if (how & FLUSH_SYNC) {
853 ret = rpc_wait_for_completion_task(task);
854 if (ret == 0)
855 ret = task->tk_status;
857 rpc_put_task(task);
858 out:
859 return ret;
862 /* If a nfs_flush_* function fails, it should remove reqs from @head and
863 * call this on each, which will prepare them to be retried on next
864 * writeback using standard nfs.
866 static void nfs_redirty_request(struct nfs_page *req)
868 struct page *page = req->wb_page;
870 nfs_mark_request_dirty(req);
871 nfs_clear_page_tag_locked(req);
872 nfs_end_page_writeback(page);
876 * Generate multiple small requests to write out a single
877 * contiguous dirty area on one page.
879 static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
881 struct nfs_page *req = nfs_list_entry(head->next);
882 struct page *page = req->wb_page;
883 struct nfs_write_data *data;
884 size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
885 unsigned int offset;
886 int requests = 0;
887 int ret = 0;
888 LIST_HEAD(list);
890 nfs_list_remove_request(req);
892 nbytes = count;
893 do {
894 size_t len = min(nbytes, wsize);
896 data = nfs_writedata_alloc(1);
897 if (!data)
898 goto out_bad;
899 list_add(&data->pages, &list);
900 requests++;
901 nbytes -= len;
902 } while (nbytes != 0);
903 atomic_set(&req->wb_complete, requests);
905 ClearPageError(page);
906 offset = 0;
907 nbytes = count;
908 do {
909 int ret2;
911 data = list_entry(list.next, struct nfs_write_data, pages);
912 list_del_init(&data->pages);
914 data->pagevec[0] = page;
916 if (nbytes < wsize)
917 wsize = nbytes;
918 ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
919 wsize, offset, how);
920 if (ret == 0)
921 ret = ret2;
922 offset += wsize;
923 nbytes -= wsize;
924 } while (nbytes != 0);
926 return ret;
928 out_bad:
929 while (!list_empty(&list)) {
930 data = list_entry(list.next, struct nfs_write_data, pages);
931 list_del(&data->pages);
932 nfs_writedata_release(data);
934 nfs_redirty_request(req);
935 return -ENOMEM;
939 * Create an RPC task for the given write request and kick it.
940 * The page must have been locked by the caller.
942 * It may happen that the page we're passed is not marked dirty.
943 * This is the case if nfs_updatepage detects a conflicting request
944 * that has been written but not committed.
946 static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
948 struct nfs_page *req;
949 struct page **pages;
950 struct nfs_write_data *data;
952 data = nfs_writedata_alloc(npages);
953 if (!data)
954 goto out_bad;
956 pages = data->pagevec;
957 while (!list_empty(head)) {
958 req = nfs_list_entry(head->next);
959 nfs_list_remove_request(req);
960 nfs_list_add_request(req, &data->pages);
961 ClearPageError(req->wb_page);
962 *pages++ = req->wb_page;
964 req = nfs_list_entry(data->pages.next);
966 /* Set up the argument struct */
967 return nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
968 out_bad:
969 while (!list_empty(head)) {
970 req = nfs_list_entry(head->next);
971 nfs_list_remove_request(req);
972 nfs_redirty_request(req);
974 return -ENOMEM;
977 static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
978 struct inode *inode, int ioflags)
980 size_t wsize = NFS_SERVER(inode)->wsize;
982 if (wsize < PAGE_CACHE_SIZE)
983 nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
984 else
985 nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
989 * Handle a write reply that flushed part of a page.
991 static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
993 struct nfs_write_data *data = calldata;
995 dprintk("NFS: %5u write(%s/%lld %d@%lld)",
996 task->tk_pid,
997 data->req->wb_context->path.dentry->d_inode->i_sb->s_id,
998 (long long)
999 NFS_FILEID(data->req->wb_context->path.dentry->d_inode),
1000 data->req->wb_bytes, (long long)req_offset(data->req));
1002 nfs_writeback_done(task, data);
1005 static void nfs_writeback_release_partial(void *calldata)
1007 struct nfs_write_data *data = calldata;
1008 struct nfs_page *req = data->req;
1009 struct page *page = req->wb_page;
1010 int status = data->task.tk_status;
1012 if (status < 0) {
1013 nfs_set_pageerror(page);
1014 nfs_context_set_write_error(req->wb_context, status);
1015 dprintk(", error = %d\n", status);
1016 goto out;
1019 if (nfs_write_need_commit(data)) {
1020 struct inode *inode = page->mapping->host;
1022 spin_lock(&inode->i_lock);
1023 if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
1024 /* Do nothing we need to resend the writes */
1025 } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
1026 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1027 dprintk(" defer commit\n");
1028 } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
1029 set_bit(PG_NEED_RESCHED, &req->wb_flags);
1030 clear_bit(PG_NEED_COMMIT, &req->wb_flags);
1031 dprintk(" server reboot detected\n");
1033 spin_unlock(&inode->i_lock);
1034 } else
1035 dprintk(" OK\n");
1037 out:
1038 if (atomic_dec_and_test(&req->wb_complete))
1039 nfs_writepage_release(req);
1040 nfs_writedata_release(calldata);
1043 #if defined(CONFIG_NFS_V4_1)
1044 void nfs_write_prepare(struct rpc_task *task, void *calldata)
1046 struct nfs_write_data *data = calldata;
1048 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
1049 &data->args.seq_args,
1050 &data->res.seq_res, 1, task))
1051 return;
1052 rpc_call_start(task);
1054 #endif /* CONFIG_NFS_V4_1 */
1056 static const struct rpc_call_ops nfs_write_partial_ops = {
1057 #if defined(CONFIG_NFS_V4_1)
1058 .rpc_call_prepare = nfs_write_prepare,
1059 #endif /* CONFIG_NFS_V4_1 */
1060 .rpc_call_done = nfs_writeback_done_partial,
1061 .rpc_release = nfs_writeback_release_partial,
1064 static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
1066 struct nfs_write_data *data = calldata;
1068 nfs_writeback_done(task, data);
1071 static void nfs_writeback_release_full(void *calldata)
1073 struct nfs_write_data *data = calldata;
1074 int status = data->task.tk_status;
1076 /* Update attributes as result of writeback. */
1077 while (!list_empty(&data->pages)) {
1078 struct nfs_page *req = nfs_list_entry(data->pages.next);
1079 struct page *page = req->wb_page;
1081 nfs_list_remove_request(req);
1083 dprintk("NFS: %5u write (%s/%lld %d@%lld)",
1084 data->task.tk_pid,
1085 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1086 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1087 req->wb_bytes,
1088 (long long)req_offset(req));
1090 if (status < 0) {
1091 nfs_set_pageerror(page);
1092 nfs_context_set_write_error(req->wb_context, status);
1093 dprintk(", error = %d\n", status);
1094 goto remove_request;
1097 if (nfs_write_need_commit(data)) {
1098 memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1099 nfs_mark_request_commit(req);
1100 dprintk(" marked for commit\n");
1101 goto next;
1103 dprintk(" OK\n");
1104 remove_request:
1105 nfs_inode_remove_request(req);
1106 next:
1107 nfs_clear_page_tag_locked(req);
1108 nfs_end_page_writeback(page);
1110 nfs_writedata_release(calldata);
1113 static const struct rpc_call_ops nfs_write_full_ops = {
1114 #if defined(CONFIG_NFS_V4_1)
1115 .rpc_call_prepare = nfs_write_prepare,
1116 #endif /* CONFIG_NFS_V4_1 */
1117 .rpc_call_done = nfs_writeback_done_full,
1118 .rpc_release = nfs_writeback_release_full,
1123 * This function is called when the WRITE call is complete.
1125 int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1127 struct nfs_writeargs *argp = &data->args;
1128 struct nfs_writeres *resp = &data->res;
1129 struct nfs_server *server = NFS_SERVER(data->inode);
1130 int status;
1132 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1133 task->tk_pid, task->tk_status);
1136 * ->write_done will attempt to use post-op attributes to detect
1137 * conflicting writes by other clients. A strict interpretation
1138 * of close-to-open would allow us to continue caching even if
1139 * another writer had changed the file, but some applications
1140 * depend on tighter cache coherency when writing.
1142 status = NFS_PROTO(data->inode)->write_done(task, data);
1143 if (status != 0)
1144 return status;
1145 nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
1147 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1148 if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
1149 /* We tried a write call, but the server did not
1150 * commit data to stable storage even though we
1151 * requested it.
1152 * Note: There is a known bug in Tru64 < 5.0 in which
1153 * the server reports NFS_DATA_SYNC, but performs
1154 * NFS_FILE_SYNC. We therefore implement this checking
1155 * as a dprintk() in order to avoid filling syslog.
1157 static unsigned long complain;
1159 if (time_before(complain, jiffies)) {
1160 dprintk("NFS: faulty NFS server %s:"
1161 " (committed = %d) != (stable = %d)\n",
1162 server->nfs_client->cl_hostname,
1163 resp->verf->committed, argp->stable);
1164 complain = jiffies + 300 * HZ;
1167 #endif
1168 /* Is this a short write? */
1169 if (task->tk_status >= 0 && resp->count < argp->count) {
1170 static unsigned long complain;
1172 nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
1174 /* Has the server at least made some progress? */
1175 if (resp->count != 0) {
1176 /* Was this an NFSv2 write or an NFSv3 stable write? */
1177 if (resp->verf->committed != NFS_UNSTABLE) {
1178 /* Resend from where the server left off */
1179 argp->offset += resp->count;
1180 argp->pgbase += resp->count;
1181 argp->count -= resp->count;
1182 } else {
1183 /* Resend as a stable write in order to avoid
1184 * headaches in the case of a server crash.
1186 argp->stable = NFS_FILE_SYNC;
1188 nfs_restart_rpc(task, server->nfs_client);
1189 return -EAGAIN;
1191 if (time_before(complain, jiffies)) {
1192 printk(KERN_WARNING
1193 "NFS: Server wrote zero bytes, expected %u.\n",
1194 argp->count);
1195 complain = jiffies + 300 * HZ;
1197 /* Can't do anything about it except throw an error. */
1198 task->tk_status = -EIO;
1200 return 0;
1204 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1205 static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
1207 if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
1208 return 1;
1209 if (may_wait && !out_of_line_wait_on_bit_lock(&nfsi->flags,
1210 NFS_INO_COMMIT, nfs_wait_bit_killable,
1211 TASK_KILLABLE))
1212 return 1;
1213 return 0;
1216 static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
1218 clear_bit(NFS_INO_COMMIT, &nfsi->flags);
1219 smp_mb__after_clear_bit();
1220 wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
1224 static void nfs_commitdata_release(void *data)
1226 struct nfs_write_data *wdata = data;
1228 put_nfs_open_context(wdata->args.context);
1229 nfs_commit_free(wdata);
1233 * Set up the argument/result storage required for the RPC call.
1235 static int nfs_commit_rpcsetup(struct list_head *head,
1236 struct nfs_write_data *data,
1237 int how)
1239 struct nfs_page *first = nfs_list_entry(head->next);
1240 struct inode *inode = first->wb_context->path.dentry->d_inode;
1241 int priority = flush_task_priority(how);
1242 struct rpc_task *task;
1243 struct rpc_message msg = {
1244 .rpc_argp = &data->args,
1245 .rpc_resp = &data->res,
1246 .rpc_cred = first->wb_context->cred,
1248 struct rpc_task_setup task_setup_data = {
1249 .task = &data->task,
1250 .rpc_client = NFS_CLIENT(inode),
1251 .rpc_message = &msg,
1252 .callback_ops = &nfs_commit_ops,
1253 .callback_data = data,
1254 .workqueue = nfsiod_workqueue,
1255 .flags = RPC_TASK_ASYNC,
1256 .priority = priority,
1259 /* Set up the RPC argument and reply structs
1260 * NB: take care not to mess about with data->commit et al. */
1262 list_splice_init(head, &data->pages);
1264 data->inode = inode;
1265 data->cred = msg.rpc_cred;
1267 data->args.fh = NFS_FH(data->inode);
1268 /* Note: we always request a commit of the entire inode */
1269 data->args.offset = 0;
1270 data->args.count = 0;
1271 data->args.context = get_nfs_open_context(first->wb_context);
1272 data->res.count = 0;
1273 data->res.fattr = &data->fattr;
1274 data->res.verf = &data->verf;
1275 nfs_fattr_init(&data->fattr);
1277 /* Set up the initial task struct. */
1278 NFS_PROTO(inode)->commit_setup(data, &msg);
1280 dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1282 task = rpc_run_task(&task_setup_data);
1283 if (IS_ERR(task))
1284 return PTR_ERR(task);
1285 rpc_put_task(task);
1286 return 0;
1290 * Commit dirty pages
1292 static int
1293 nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1295 struct nfs_write_data *data;
1296 struct nfs_page *req;
1298 data = nfs_commitdata_alloc();
1300 if (!data)
1301 goto out_bad;
1303 /* Set up the argument struct */
1304 return nfs_commit_rpcsetup(head, data, how);
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 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1312 BDI_RECLAIMABLE);
1313 nfs_clear_page_tag_locked(req);
1315 nfs_commit_clear_lock(NFS_I(inode));
1316 return -ENOMEM;
1320 * COMMIT call returned
1322 static void nfs_commit_done(struct rpc_task *task, void *calldata)
1324 struct nfs_write_data *data = calldata;
1326 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1327 task->tk_pid, task->tk_status);
1329 /* Call the NFS version-specific code */
1330 if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1331 return;
1334 static void nfs_commit_release(void *calldata)
1336 struct nfs_write_data *data = calldata;
1337 struct nfs_page *req;
1338 int status = data->task.tk_status;
1340 while (!list_empty(&data->pages)) {
1341 req = nfs_list_entry(data->pages.next);
1342 nfs_list_remove_request(req);
1343 nfs_clear_request_commit(req);
1345 dprintk("NFS: commit (%s/%lld %d@%lld)",
1346 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1347 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1348 req->wb_bytes,
1349 (long long)req_offset(req));
1350 if (status < 0) {
1351 nfs_context_set_write_error(req->wb_context, status);
1352 nfs_inode_remove_request(req);
1353 dprintk(", error = %d\n", status);
1354 goto next;
1357 /* Okay, COMMIT succeeded, apparently. Check the verifier
1358 * returned by the server against all stored verfs. */
1359 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1360 /* We have a match */
1361 nfs_inode_remove_request(req);
1362 dprintk(" OK\n");
1363 goto next;
1365 /* We have a mismatch. Write the page again */
1366 dprintk(" mismatch\n");
1367 nfs_mark_request_dirty(req);
1368 next:
1369 nfs_clear_page_tag_locked(req);
1371 nfs_commit_clear_lock(NFS_I(data->inode));
1372 nfs_commitdata_release(calldata);
1375 static const struct rpc_call_ops nfs_commit_ops = {
1376 #if defined(CONFIG_NFS_V4_1)
1377 .rpc_call_prepare = nfs_write_prepare,
1378 #endif /* CONFIG_NFS_V4_1 */
1379 .rpc_call_done = nfs_commit_done,
1380 .rpc_release = nfs_commit_release,
1383 int nfs_commit_inode(struct inode *inode, int how)
1385 LIST_HEAD(head);
1386 int may_wait = how & FLUSH_SYNC;
1387 int res = 0;
1389 if (!nfs_commit_set_lock(NFS_I(inode), may_wait))
1390 goto out_mark_dirty;
1391 spin_lock(&inode->i_lock);
1392 res = nfs_scan_commit(inode, &head, 0, 0);
1393 spin_unlock(&inode->i_lock);
1394 if (res) {
1395 int error = nfs_commit_list(inode, &head, how);
1396 if (error < 0)
1397 return error;
1398 if (may_wait)
1399 wait_on_bit(&NFS_I(inode)->flags, NFS_INO_COMMIT,
1400 nfs_wait_bit_killable,
1401 TASK_KILLABLE);
1402 else
1403 goto out_mark_dirty;
1404 } else
1405 nfs_commit_clear_lock(NFS_I(inode));
1406 return res;
1407 /* Note: If we exit without ensuring that the commit is complete,
1408 * we must mark the inode as dirty. Otherwise, future calls to
1409 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1410 * that the data is on the disk.
1412 out_mark_dirty:
1413 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
1414 return res;
1417 static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
1419 struct nfs_inode *nfsi = NFS_I(inode);
1420 int flags = FLUSH_SYNC;
1421 int ret = 0;
1423 /* Don't commit yet if this is a non-blocking flush and there are
1424 * lots of outstanding writes for this mapping.
1426 if (wbc->sync_mode == WB_SYNC_NONE &&
1427 nfsi->ncommit <= (nfsi->npages >> 1))
1428 goto out_mark_dirty;
1430 if (wbc->nonblocking || wbc->for_background)
1431 flags = 0;
1432 ret = nfs_commit_inode(inode, flags);
1433 if (ret >= 0) {
1434 if (wbc->sync_mode == WB_SYNC_NONE) {
1435 if (ret < wbc->nr_to_write)
1436 wbc->nr_to_write -= ret;
1437 else
1438 wbc->nr_to_write = 0;
1440 return 0;
1442 out_mark_dirty:
1443 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
1444 return ret;
1446 #else
1447 static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
1449 return 0;
1451 #endif
1453 int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
1455 return nfs_commit_unstable_pages(inode, wbc);
1459 * flush the inode to disk.
1461 int nfs_wb_all(struct inode *inode)
1463 struct writeback_control wbc = {
1464 .sync_mode = WB_SYNC_ALL,
1465 .nr_to_write = LONG_MAX,
1466 .range_start = 0,
1467 .range_end = LLONG_MAX,
1470 return sync_inode(inode, &wbc);
1473 int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1475 struct nfs_page *req;
1476 int ret = 0;
1478 BUG_ON(!PageLocked(page));
1479 for (;;) {
1480 wait_on_page_writeback(page);
1481 req = nfs_page_find_request(page);
1482 if (req == NULL)
1483 break;
1484 if (nfs_lock_request_dontget(req)) {
1485 nfs_inode_remove_request(req);
1487 * In case nfs_inode_remove_request has marked the
1488 * page as being dirty
1490 cancel_dirty_page(page, PAGE_CACHE_SIZE);
1491 nfs_unlock_request(req);
1492 break;
1494 ret = nfs_wait_on_request(req);
1495 nfs_release_request(req);
1496 if (ret < 0)
1497 break;
1499 return ret;
1503 * Write back all requests on one page - we do this before reading it.
1505 int nfs_wb_page(struct inode *inode, struct page *page)
1507 loff_t range_start = page_offset(page);
1508 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
1509 struct writeback_control wbc = {
1510 .sync_mode = WB_SYNC_ALL,
1511 .nr_to_write = 0,
1512 .range_start = range_start,
1513 .range_end = range_end,
1515 int ret;
1517 for (;;) {
1518 wait_on_page_writeback(page);
1519 if (clear_page_dirty_for_io(page)) {
1520 ret = nfs_writepage_locked(page, &wbc);
1521 if (ret < 0)
1522 goto out_error;
1523 continue;
1525 if (!PagePrivate(page))
1526 break;
1527 ret = nfs_commit_inode(inode, FLUSH_SYNC);
1528 if (ret < 0)
1529 goto out_error;
1531 return 0;
1532 out_error:
1533 return ret;
1536 #ifdef CONFIG_MIGRATION
1537 int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1538 struct page *page)
1540 struct nfs_page *req;
1541 int ret;
1543 nfs_fscache_release_page(page, GFP_KERNEL);
1545 req = nfs_find_and_lock_request(page, false);
1546 ret = PTR_ERR(req);
1547 if (IS_ERR(req))
1548 goto out;
1550 ret = migrate_page(mapping, newpage, page);
1551 if (!req)
1552 goto out;
1553 if (ret)
1554 goto out_unlock;
1555 page_cache_get(newpage);
1556 spin_lock(&mapping->host->i_lock);
1557 req->wb_page = newpage;
1558 SetPagePrivate(newpage);
1559 set_page_private(newpage, (unsigned long)req);
1560 ClearPagePrivate(page);
1561 set_page_private(page, 0);
1562 spin_unlock(&mapping->host->i_lock);
1563 page_cache_release(page);
1564 out_unlock:
1565 nfs_clear_page_tag_locked(req);
1566 out:
1567 return ret;
1569 #endif
1571 int __init nfs_init_writepagecache(void)
1573 nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1574 sizeof(struct nfs_write_data),
1575 0, SLAB_HWCACHE_ALIGN,
1576 NULL);
1577 if (nfs_wdata_cachep == NULL)
1578 return -ENOMEM;
1580 nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
1581 nfs_wdata_cachep);
1582 if (nfs_wdata_mempool == NULL)
1583 return -ENOMEM;
1585 nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
1586 nfs_wdata_cachep);
1587 if (nfs_commit_mempool == NULL)
1588 return -ENOMEM;
1591 * NFS congestion size, scale with available memory.
1593 * 64MB: 8192k
1594 * 128MB: 11585k
1595 * 256MB: 16384k
1596 * 512MB: 23170k
1597 * 1GB: 32768k
1598 * 2GB: 46340k
1599 * 4GB: 65536k
1600 * 8GB: 92681k
1601 * 16GB: 131072k
1603 * This allows larger machines to have larger/more transfers.
1604 * Limit the default to 256M
1606 nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
1607 if (nfs_congestion_kb > 256*1024)
1608 nfs_congestion_kb = 256*1024;
1610 return 0;
1613 void nfs_destroy_writepagecache(void)
1615 mempool_destroy(nfs_commit_mempool);
1616 mempool_destroy(nfs_wdata_mempool);
1617 kmem_cache_destroy(nfs_wdata_cachep);