4 * Write file data over NFS.
6 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
9 #include <linux/types.h>
10 #include <linux/slab.h>
12 #include <linux/pagemap.h>
13 #include <linux/file.h>
14 #include <linux/writeback.h>
15 #include <linux/swap.h>
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>
23 #include <linux/export.h>
25 #include <asm/uaccess.h>
27 #include "delegation.h"
34 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
36 #define MIN_POOL_WRITE (32)
37 #define MIN_POOL_COMMIT (4)
40 * Local function declarations
42 static void nfs_pageio_init_write(struct nfs_pageio_descriptor
*desc
,
43 struct inode
*inode
, int ioflags
);
44 static void nfs_redirty_request(struct nfs_page
*req
);
45 static const struct rpc_call_ops nfs_write_partial_ops
;
46 static const struct rpc_call_ops nfs_write_full_ops
;
47 static const struct rpc_call_ops nfs_commit_ops
;
49 static struct kmem_cache
*nfs_wdata_cachep
;
50 static mempool_t
*nfs_wdata_mempool
;
51 static mempool_t
*nfs_commit_mempool
;
53 struct nfs_write_data
*nfs_commitdata_alloc(void)
55 struct nfs_write_data
*p
= mempool_alloc(nfs_commit_mempool
, GFP_NOFS
);
58 memset(p
, 0, sizeof(*p
));
59 INIT_LIST_HEAD(&p
->pages
);
63 EXPORT_SYMBOL_GPL(nfs_commitdata_alloc
);
65 void nfs_commit_free(struct nfs_write_data
*p
)
67 if (p
&& (p
->pagevec
!= &p
->page_array
[0]))
69 mempool_free(p
, nfs_commit_mempool
);
71 EXPORT_SYMBOL_GPL(nfs_commit_free
);
73 struct nfs_write_data
*nfs_writedata_alloc(unsigned int pagecount
)
75 struct nfs_write_data
*p
= mempool_alloc(nfs_wdata_mempool
, GFP_NOFS
);
78 memset(p
, 0, sizeof(*p
));
79 INIT_LIST_HEAD(&p
->pages
);
80 p
->npages
= pagecount
;
81 if (pagecount
<= ARRAY_SIZE(p
->page_array
))
82 p
->pagevec
= p
->page_array
;
84 p
->pagevec
= kcalloc(pagecount
, sizeof(struct page
*), GFP_NOFS
);
86 mempool_free(p
, nfs_wdata_mempool
);
94 void nfs_writedata_free(struct nfs_write_data
*p
)
96 if (p
&& (p
->pagevec
!= &p
->page_array
[0]))
98 mempool_free(p
, nfs_wdata_mempool
);
101 void nfs_writedata_release(struct nfs_write_data
*wdata
)
103 put_lseg(wdata
->lseg
);
104 put_nfs_open_context(wdata
->args
.context
);
105 nfs_writedata_free(wdata
);
108 static void nfs_context_set_write_error(struct nfs_open_context
*ctx
, int error
)
112 set_bit(NFS_CONTEXT_ERROR_WRITE
, &ctx
->flags
);
115 static struct nfs_page
*nfs_page_find_request_locked(struct page
*page
)
117 struct nfs_page
*req
= NULL
;
119 if (PagePrivate(page
)) {
120 req
= (struct nfs_page
*)page_private(page
);
122 kref_get(&req
->wb_kref
);
127 static struct nfs_page
*nfs_page_find_request(struct page
*page
)
129 struct inode
*inode
= page
->mapping
->host
;
130 struct nfs_page
*req
= NULL
;
132 spin_lock(&inode
->i_lock
);
133 req
= nfs_page_find_request_locked(page
);
134 spin_unlock(&inode
->i_lock
);
138 /* Adjust the file length if we're writing beyond the end */
139 static void nfs_grow_file(struct page
*page
, unsigned int offset
, unsigned int count
)
141 struct inode
*inode
= page
->mapping
->host
;
145 spin_lock(&inode
->i_lock
);
146 i_size
= i_size_read(inode
);
147 end_index
= (i_size
- 1) >> PAGE_CACHE_SHIFT
;
148 if (i_size
> 0 && page
->index
< end_index
)
150 end
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + ((loff_t
)offset
+count
);
153 i_size_write(inode
, end
);
154 nfs_inc_stats(inode
, NFSIOS_EXTENDWRITE
);
156 spin_unlock(&inode
->i_lock
);
159 /* A writeback failed: mark the page as bad, and invalidate the page cache */
160 static void nfs_set_pageerror(struct page
*page
)
163 nfs_zap_mapping(page
->mapping
->host
, page
->mapping
);
166 /* We can set the PG_uptodate flag if we see that a write request
167 * covers the full page.
169 static void nfs_mark_uptodate(struct page
*page
, unsigned int base
, unsigned int count
)
171 if (PageUptodate(page
))
175 if (count
!= nfs_page_length(page
))
177 SetPageUptodate(page
);
180 static int wb_priority(struct writeback_control
*wbc
)
182 if (wbc
->for_reclaim
)
183 return FLUSH_HIGHPRI
| FLUSH_STABLE
;
184 if (wbc
->for_kupdate
|| wbc
->for_background
)
185 return FLUSH_LOWPRI
| FLUSH_COND_STABLE
;
186 return FLUSH_COND_STABLE
;
190 * NFS congestion control
193 #define NFS_WAIT_PAGES (1024L >> (PAGE_SHIFT - 10))
194 int nfs_congestion_kb
;
197 * SYNC requests will block on (2*limit) and wakeup on (2*limit-NFS_WAIT_PAGES)
198 * ASYNC requests will block on (limit) and wakeup on (limit - NFS_WAIT_PAGES)
199 * In this way SYNC writes will never be blocked by ASYNC ones.
202 static void nfs_set_congested(long nr
, struct backing_dev_info
*bdi
)
204 long limit
= nfs_congestion_kb
>> (PAGE_SHIFT
- 10);
206 if (nr
> limit
&& !test_bit(BDI_async_congested
, &bdi
->state
))
207 set_bdi_congested(bdi
, BLK_RW_ASYNC
);
208 else if (nr
> 2 * limit
&& !test_bit(BDI_sync_congested
, &bdi
->state
))
209 set_bdi_congested(bdi
, BLK_RW_SYNC
);
212 static void nfs_wait_congested(int is_sync
,
213 struct backing_dev_info
*bdi
,
214 wait_queue_head_t
*wqh
)
216 int waitbit
= is_sync
? BDI_sync_congested
: BDI_async_congested
;
219 if (!test_bit(waitbit
, &bdi
->state
))
223 prepare_to_wait(&wqh
[is_sync
], &wait
, TASK_UNINTERRUPTIBLE
);
224 if (!test_bit(waitbit
, &bdi
->state
))
229 finish_wait(&wqh
[is_sync
], &wait
);
232 static void nfs_wakeup_congested(long nr
,
233 struct backing_dev_info
*bdi
,
234 wait_queue_head_t
*wqh
)
236 long limit
= nfs_congestion_kb
>> (PAGE_SHIFT
- 10);
238 if (nr
< 2 * limit
- min(limit
/ 8, NFS_WAIT_PAGES
)) {
239 if (test_bit(BDI_sync_congested
, &bdi
->state
))
240 clear_bdi_congested(bdi
, BLK_RW_SYNC
);
241 if (waitqueue_active(&wqh
[BLK_RW_SYNC
]))
242 wake_up(&wqh
[BLK_RW_SYNC
]);
244 if (nr
< limit
- min(limit
/ 8, NFS_WAIT_PAGES
)) {
245 if (test_bit(BDI_async_congested
, &bdi
->state
))
246 clear_bdi_congested(bdi
, BLK_RW_ASYNC
);
247 if (waitqueue_active(&wqh
[BLK_RW_ASYNC
]))
248 wake_up(&wqh
[BLK_RW_ASYNC
]);
252 static int nfs_set_page_writeback(struct page
*page
)
254 int ret
= test_set_page_writeback(page
);
257 struct inode
*inode
= page
->mapping
->host
;
258 struct nfs_server
*nfss
= NFS_SERVER(inode
);
260 page_cache_get(page
);
261 nfs_set_congested(atomic_long_inc_return(&nfss
->writeback
),
262 &nfss
->backing_dev_info
);
267 static void nfs_end_page_writeback(struct page
*page
)
269 struct inode
*inode
= page
->mapping
->host
;
270 struct nfs_server
*nfss
= NFS_SERVER(inode
);
272 end_page_writeback(page
);
273 page_cache_release(page
);
275 nfs_wakeup_congested(atomic_long_dec_return(&nfss
->writeback
),
276 &nfss
->backing_dev_info
,
277 nfss
->writeback_wait
);
280 static struct nfs_page
*nfs_find_and_lock_request(struct page
*page
, bool nonblock
)
282 struct inode
*inode
= page
->mapping
->host
;
283 struct nfs_page
*req
;
286 spin_lock(&inode
->i_lock
);
288 req
= nfs_page_find_request_locked(page
);
291 if (nfs_set_page_tag_locked(req
))
293 /* Note: If we hold the page lock, as is the case in nfs_writepage,
294 * then the call to nfs_set_page_tag_locked() will always
295 * succeed provided that someone hasn't already marked the
296 * request as dirty (in which case we don't care).
298 spin_unlock(&inode
->i_lock
);
300 ret
= nfs_wait_on_request(req
);
303 nfs_release_request(req
);
306 spin_lock(&inode
->i_lock
);
308 spin_unlock(&inode
->i_lock
);
313 * Find an associated nfs write request, and prepare to flush it out
314 * May return an error if the user signalled nfs_wait_on_request().
316 static int nfs_page_async_flush(struct nfs_pageio_descriptor
*pgio
,
317 struct page
*page
, bool nonblock
)
319 struct nfs_page
*req
;
322 req
= nfs_find_and_lock_request(page
, nonblock
);
329 ret
= nfs_set_page_writeback(page
);
331 BUG_ON(test_bit(PG_CLEAN
, &req
->wb_flags
));
333 if (!nfs_pageio_add_request(pgio
, req
)) {
334 nfs_redirty_request(req
);
335 ret
= pgio
->pg_error
;
341 static int nfs_do_writepage(struct page
*page
, struct writeback_control
*wbc
, struct nfs_pageio_descriptor
*pgio
)
343 struct inode
*inode
= page
->mapping
->host
;
346 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGE
);
347 nfs_add_stats(inode
, NFSIOS_WRITEPAGES
, 1);
349 nfs_pageio_cond_complete(pgio
, page
->index
);
350 ret
= nfs_page_async_flush(pgio
, page
, wbc
->sync_mode
== WB_SYNC_NONE
);
351 if (ret
== -EAGAIN
) {
352 redirty_page_for_writepage(wbc
, page
);
359 * Write an mmapped page to the server.
361 static int nfs_writepage_locked(struct page
*page
, struct writeback_control
*wbc
)
363 struct nfs_pageio_descriptor pgio
;
366 nfs_pageio_init_write(&pgio
, page
->mapping
->host
, wb_priority(wbc
));
367 err
= nfs_do_writepage(page
, wbc
, &pgio
);
368 nfs_pageio_complete(&pgio
);
371 if (pgio
.pg_error
< 0)
372 return pgio
.pg_error
;
376 int nfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
378 struct inode
*inode
= page
->mapping
->host
;
379 struct nfs_server
*nfss
= NFS_SERVER(inode
);
382 ret
= nfs_writepage_locked(page
, wbc
);
385 nfs_wait_congested(wbc
->sync_mode
== WB_SYNC_ALL
,
386 &nfss
->backing_dev_info
,
387 nfss
->writeback_wait
);
392 static int nfs_writepages_callback(struct page
*page
, struct writeback_control
*wbc
, void *data
)
396 ret
= nfs_do_writepage(page
, wbc
, data
);
401 int nfs_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
403 struct inode
*inode
= mapping
->host
;
404 struct nfs_server
*nfss
= NFS_SERVER(inode
);
405 unsigned long *bitlock
= &NFS_I(inode
)->flags
;
406 struct nfs_pageio_descriptor pgio
;
409 /* Stop dirtying of new pages while we sync */
410 err
= wait_on_bit_lock(bitlock
, NFS_INO_FLUSHING
,
411 nfs_wait_bit_killable
, TASK_KILLABLE
);
415 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGES
);
417 nfs_pageio_init_write(&pgio
, inode
, wb_priority(wbc
));
418 err
= write_cache_pages(mapping
, wbc
, nfs_writepages_callback
, &pgio
);
419 nfs_pageio_complete(&pgio
);
421 nfs_wait_congested(wbc
->sync_mode
== WB_SYNC_ALL
,
422 &nfss
->backing_dev_info
,
423 nfss
->writeback_wait
);
425 clear_bit_unlock(NFS_INO_FLUSHING
, bitlock
);
426 smp_mb__after_clear_bit();
427 wake_up_bit(bitlock
, NFS_INO_FLUSHING
);
440 * Insert a write request into an inode
442 static int nfs_inode_add_request(struct inode
*inode
, struct nfs_page
*req
)
444 struct nfs_inode
*nfsi
= NFS_I(inode
);
447 error
= radix_tree_preload(GFP_NOFS
);
451 /* Lock the request! */
452 nfs_lock_request_dontget(req
);
454 spin_lock(&inode
->i_lock
);
455 error
= radix_tree_insert(&nfsi
->nfs_page_tree
, req
->wb_index
, req
);
457 if (!nfsi
->npages
&& nfs_have_delegation(inode
, FMODE_WRITE
))
459 set_bit(PG_MAPPED
, &req
->wb_flags
);
460 SetPagePrivate(req
->wb_page
);
461 set_page_private(req
->wb_page
, (unsigned long)req
);
463 kref_get(&req
->wb_kref
);
464 radix_tree_tag_set(&nfsi
->nfs_page_tree
, req
->wb_index
,
465 NFS_PAGE_TAG_LOCKED
);
466 spin_unlock(&inode
->i_lock
);
467 radix_tree_preload_end();
473 * Remove a write request from an inode
475 static void nfs_inode_remove_request(struct nfs_page
*req
)
477 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
478 struct nfs_inode
*nfsi
= NFS_I(inode
);
480 BUG_ON (!NFS_WBACK_BUSY(req
));
482 spin_lock(&inode
->i_lock
);
483 set_page_private(req
->wb_page
, 0);
484 ClearPagePrivate(req
->wb_page
);
485 clear_bit(PG_MAPPED
, &req
->wb_flags
);
486 radix_tree_delete(&nfsi
->nfs_page_tree
, req
->wb_index
);
488 spin_unlock(&inode
->i_lock
);
489 nfs_release_request(req
);
493 nfs_mark_request_dirty(struct nfs_page
*req
)
495 __set_page_dirty_nobuffers(req
->wb_page
);
498 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
500 * Add a request to the inode's commit list.
503 nfs_mark_request_commit(struct nfs_page
*req
, struct pnfs_layout_segment
*lseg
)
505 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
506 struct nfs_inode
*nfsi
= NFS_I(inode
);
508 spin_lock(&inode
->i_lock
);
509 set_bit(PG_CLEAN
, &(req
)->wb_flags
);
510 radix_tree_tag_set(&nfsi
->nfs_page_tree
,
512 NFS_PAGE_TAG_COMMIT
);
514 spin_unlock(&inode
->i_lock
);
515 pnfs_mark_request_commit(req
, lseg
);
516 inc_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
517 inc_bdi_stat(req
->wb_page
->mapping
->backing_dev_info
, BDI_RECLAIMABLE
);
518 __mark_inode_dirty(inode
, I_DIRTY_DATASYNC
);
522 nfs_clear_request_commit(struct nfs_page
*req
)
524 struct page
*page
= req
->wb_page
;
526 if (test_and_clear_bit(PG_CLEAN
, &(req
)->wb_flags
)) {
527 dec_zone_page_state(page
, NR_UNSTABLE_NFS
);
528 dec_bdi_stat(page
->mapping
->backing_dev_info
, BDI_RECLAIMABLE
);
535 int nfs_write_need_commit(struct nfs_write_data
*data
)
537 if (data
->verf
.committed
== NFS_DATA_SYNC
)
538 return data
->lseg
== NULL
;
540 return data
->verf
.committed
!= NFS_FILE_SYNC
;
544 int nfs_reschedule_unstable_write(struct nfs_page
*req
,
545 struct nfs_write_data
*data
)
547 if (test_and_clear_bit(PG_NEED_COMMIT
, &req
->wb_flags
)) {
548 nfs_mark_request_commit(req
, data
->lseg
);
551 if (test_and_clear_bit(PG_NEED_RESCHED
, &req
->wb_flags
)) {
552 nfs_mark_request_dirty(req
);
559 nfs_mark_request_commit(struct nfs_page
*req
, struct pnfs_layout_segment
*lseg
)
564 nfs_clear_request_commit(struct nfs_page
*req
)
570 int nfs_write_need_commit(struct nfs_write_data
*data
)
576 int nfs_reschedule_unstable_write(struct nfs_page
*req
,
577 struct nfs_write_data
*data
)
583 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
585 nfs_need_commit(struct nfs_inode
*nfsi
)
587 return radix_tree_tagged(&nfsi
->nfs_page_tree
, NFS_PAGE_TAG_COMMIT
);
591 * nfs_scan_commit - Scan an inode for commit requests
592 * @inode: NFS inode to scan
593 * @dst: destination list
594 * @idx_start: lower bound of page->index to scan.
595 * @npages: idx_start + npages sets the upper bound to scan.
597 * Moves requests from the inode's 'commit' request list.
598 * The requests are *not* checked to ensure that they form a contiguous set.
601 nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
, pgoff_t idx_start
, unsigned int npages
)
603 struct nfs_inode
*nfsi
= NFS_I(inode
);
606 if (!nfs_need_commit(nfsi
))
609 spin_lock(&inode
->i_lock
);
610 ret
= nfs_scan_list(nfsi
, dst
, idx_start
, npages
, NFS_PAGE_TAG_COMMIT
);
612 nfsi
->ncommit
-= ret
;
613 spin_unlock(&inode
->i_lock
);
615 if (nfs_need_commit(NFS_I(inode
)))
616 __mark_inode_dirty(inode
, I_DIRTY_DATASYNC
);
621 static inline int nfs_need_commit(struct nfs_inode
*nfsi
)
626 static inline int nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
, pgoff_t idx_start
, unsigned int npages
)
633 * Search for an existing write request, and attempt to update
634 * it to reflect a new dirty region on a given page.
636 * If the attempt fails, then the existing request is flushed out
639 static struct nfs_page
*nfs_try_to_update_request(struct inode
*inode
,
644 struct nfs_page
*req
;
649 if (!PagePrivate(page
))
652 end
= offset
+ bytes
;
653 spin_lock(&inode
->i_lock
);
656 req
= nfs_page_find_request_locked(page
);
660 rqend
= req
->wb_offset
+ req
->wb_bytes
;
662 * Tell the caller to flush out the request if
663 * the offsets are non-contiguous.
664 * Note: nfs_flush_incompatible() will already
665 * have flushed out requests having wrong owners.
668 || end
< req
->wb_offset
)
671 if (nfs_set_page_tag_locked(req
))
674 /* The request is locked, so wait and then retry */
675 spin_unlock(&inode
->i_lock
);
676 error
= nfs_wait_on_request(req
);
677 nfs_release_request(req
);
680 spin_lock(&inode
->i_lock
);
683 if (nfs_clear_request_commit(req
) &&
684 radix_tree_tag_clear(&NFS_I(inode
)->nfs_page_tree
,
685 req
->wb_index
, NFS_PAGE_TAG_COMMIT
) != NULL
) {
686 NFS_I(inode
)->ncommit
--;
687 pnfs_clear_request_commit(req
);
690 /* Okay, the request matches. Update the region */
691 if (offset
< req
->wb_offset
) {
692 req
->wb_offset
= offset
;
693 req
->wb_pgbase
= offset
;
696 req
->wb_bytes
= end
- req
->wb_offset
;
698 req
->wb_bytes
= rqend
- req
->wb_offset
;
700 spin_unlock(&inode
->i_lock
);
703 spin_unlock(&inode
->i_lock
);
704 nfs_release_request(req
);
705 error
= nfs_wb_page(inode
, page
);
707 return ERR_PTR(error
);
711 * Try to update an existing write request, or create one if there is none.
713 * Note: Should always be called with the Page Lock held to prevent races
714 * if we have to add a new request. Also assumes that the caller has
715 * already called nfs_flush_incompatible() if necessary.
717 static struct nfs_page
* nfs_setup_write_request(struct nfs_open_context
* ctx
,
718 struct page
*page
, unsigned int offset
, unsigned int bytes
)
720 struct inode
*inode
= page
->mapping
->host
;
721 struct nfs_page
*req
;
724 req
= nfs_try_to_update_request(inode
, page
, offset
, bytes
);
727 req
= nfs_create_request(ctx
, inode
, page
, offset
, bytes
);
730 error
= nfs_inode_add_request(inode
, req
);
732 nfs_release_request(req
);
733 req
= ERR_PTR(error
);
739 static int nfs_writepage_setup(struct nfs_open_context
*ctx
, struct page
*page
,
740 unsigned int offset
, unsigned int count
)
742 struct nfs_page
*req
;
744 req
= nfs_setup_write_request(ctx
, page
, offset
, count
);
747 /* Update file length */
748 nfs_grow_file(page
, offset
, count
);
749 nfs_mark_uptodate(page
, req
->wb_pgbase
, req
->wb_bytes
);
750 nfs_mark_request_dirty(req
);
751 nfs_clear_page_tag_locked(req
);
755 int nfs_flush_incompatible(struct file
*file
, struct page
*page
)
757 struct nfs_open_context
*ctx
= nfs_file_open_context(file
);
758 struct nfs_page
*req
;
759 int do_flush
, status
;
761 * Look for a request corresponding to this page. If there
762 * is one, and it belongs to another file, we flush it out
763 * before we try to copy anything into the page. Do this
764 * due to the lack of an ACCESS-type call in NFSv2.
765 * Also do the same if we find a request from an existing
769 req
= nfs_page_find_request(page
);
772 do_flush
= req
->wb_page
!= page
|| req
->wb_context
!= ctx
||
773 req
->wb_lock_context
->lockowner
!= current
->files
||
774 req
->wb_lock_context
->pid
!= current
->tgid
;
775 nfs_release_request(req
);
778 status
= nfs_wb_page(page
->mapping
->host
, page
);
779 } while (status
== 0);
784 * If the page cache is marked as unsafe or invalid, then we can't rely on
785 * the PageUptodate() flag. In this case, we will need to turn off
786 * write optimisations that depend on the page contents being correct.
788 static int nfs_write_pageuptodate(struct page
*page
, struct inode
*inode
)
790 return PageUptodate(page
) &&
791 !(NFS_I(inode
)->cache_validity
& (NFS_INO_REVAL_PAGECACHE
|NFS_INO_INVALID_DATA
));
795 * Update and possibly write a cached page of an NFS file.
797 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
798 * things with a page scheduled for an RPC call (e.g. invalidate it).
800 int nfs_updatepage(struct file
*file
, struct page
*page
,
801 unsigned int offset
, unsigned int count
)
803 struct nfs_open_context
*ctx
= nfs_file_open_context(file
);
804 struct inode
*inode
= page
->mapping
->host
;
807 nfs_inc_stats(inode
, NFSIOS_VFSUPDATEPAGE
);
809 dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n",
810 file
->f_path
.dentry
->d_parent
->d_name
.name
,
811 file
->f_path
.dentry
->d_name
.name
, count
,
812 (long long)(page_offset(page
) + offset
));
814 /* If we're not using byte range locks, and we know the page
815 * is up to date, it may be more efficient to extend the write
816 * to cover the entire page in order to avoid fragmentation
819 if (nfs_write_pageuptodate(page
, inode
) &&
820 inode
->i_flock
== NULL
&&
821 !(file
->f_flags
& O_DSYNC
)) {
822 count
= max(count
+ offset
, nfs_page_length(page
));
826 status
= nfs_writepage_setup(ctx
, page
, offset
, count
);
828 nfs_set_pageerror(page
);
830 __set_page_dirty_nobuffers(page
);
832 dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
833 status
, (long long)i_size_read(inode
));
837 static void nfs_writepage_release(struct nfs_page
*req
,
838 struct nfs_write_data
*data
)
840 struct page
*page
= req
->wb_page
;
842 if (PageError(req
->wb_page
) || !nfs_reschedule_unstable_write(req
, data
))
843 nfs_inode_remove_request(req
);
844 nfs_clear_page_tag_locked(req
);
845 nfs_end_page_writeback(page
);
848 static int flush_task_priority(int how
)
850 switch (how
& (FLUSH_HIGHPRI
|FLUSH_LOWPRI
)) {
852 return RPC_PRIORITY_HIGH
;
854 return RPC_PRIORITY_LOW
;
856 return RPC_PRIORITY_NORMAL
;
859 int nfs_initiate_write(struct nfs_write_data
*data
,
860 struct rpc_clnt
*clnt
,
861 const struct rpc_call_ops
*call_ops
,
864 struct inode
*inode
= data
->inode
;
865 int priority
= flush_task_priority(how
);
866 struct rpc_task
*task
;
867 struct rpc_message msg
= {
868 .rpc_argp
= &data
->args
,
869 .rpc_resp
= &data
->res
,
870 .rpc_cred
= data
->cred
,
872 struct rpc_task_setup task_setup_data
= {
876 .callback_ops
= call_ops
,
877 .callback_data
= data
,
878 .workqueue
= nfsiod_workqueue
,
879 .flags
= RPC_TASK_ASYNC
,
880 .priority
= priority
,
884 /* Set up the initial task struct. */
885 NFS_PROTO(inode
)->write_setup(data
, &msg
);
887 dprintk("NFS: %5u initiated write call "
888 "(req %s/%lld, %u bytes @ offset %llu)\n",
891 (long long)NFS_FILEID(inode
),
893 (unsigned long long)data
->args
.offset
);
895 task
= rpc_run_task(&task_setup_data
);
900 if (how
& FLUSH_SYNC
) {
901 ret
= rpc_wait_for_completion_task(task
);
903 ret
= task
->tk_status
;
909 EXPORT_SYMBOL_GPL(nfs_initiate_write
);
912 * Set up the argument/result storage required for the RPC call.
914 static void nfs_write_rpcsetup(struct nfs_page
*req
,
915 struct nfs_write_data
*data
,
916 unsigned int count
, unsigned int offset
,
919 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
921 /* Set up the RPC argument and reply structs
922 * NB: take care not to mess about with data->commit et al. */
925 data
->inode
= inode
= req
->wb_context
->dentry
->d_inode
;
926 data
->cred
= req
->wb_context
->cred
;
928 data
->args
.fh
= NFS_FH(inode
);
929 data
->args
.offset
= req_offset(req
) + offset
;
930 /* pnfs_set_layoutcommit needs this */
931 data
->mds_offset
= data
->args
.offset
;
932 data
->args
.pgbase
= req
->wb_pgbase
+ offset
;
933 data
->args
.pages
= data
->pagevec
;
934 data
->args
.count
= count
;
935 data
->args
.context
= get_nfs_open_context(req
->wb_context
);
936 data
->args
.lock_context
= req
->wb_lock_context
;
937 data
->args
.stable
= NFS_UNSTABLE
;
938 switch (how
& (FLUSH_STABLE
| FLUSH_COND_STABLE
)) {
941 case FLUSH_COND_STABLE
:
942 if (nfs_need_commit(NFS_I(inode
)))
945 data
->args
.stable
= NFS_FILE_SYNC
;
948 data
->res
.fattr
= &data
->fattr
;
949 data
->res
.count
= count
;
950 data
->res
.verf
= &data
->verf
;
951 nfs_fattr_init(&data
->fattr
);
954 static int nfs_do_write(struct nfs_write_data
*data
,
955 const struct rpc_call_ops
*call_ops
,
958 struct inode
*inode
= data
->args
.context
->dentry
->d_inode
;
960 return nfs_initiate_write(data
, NFS_CLIENT(inode
), call_ops
, how
);
963 static int nfs_do_multiple_writes(struct list_head
*head
,
964 const struct rpc_call_ops
*call_ops
,
967 struct nfs_write_data
*data
;
970 while (!list_empty(head
)) {
973 data
= list_entry(head
->next
, struct nfs_write_data
, list
);
974 list_del_init(&data
->list
);
976 ret2
= nfs_do_write(data
, call_ops
, how
);
983 /* If a nfs_flush_* function fails, it should remove reqs from @head and
984 * call this on each, which will prepare them to be retried on next
985 * writeback using standard nfs.
987 static void nfs_redirty_request(struct nfs_page
*req
)
989 struct page
*page
= req
->wb_page
;
991 nfs_mark_request_dirty(req
);
992 nfs_clear_page_tag_locked(req
);
993 nfs_end_page_writeback(page
);
997 * Generate multiple small requests to write out a single
998 * contiguous dirty area on one page.
1000 static int nfs_flush_multi(struct nfs_pageio_descriptor
*desc
, struct list_head
*res
)
1002 struct nfs_page
*req
= nfs_list_entry(desc
->pg_list
.next
);
1003 struct page
*page
= req
->wb_page
;
1004 struct nfs_write_data
*data
;
1005 size_t wsize
= desc
->pg_bsize
, nbytes
;
1006 unsigned int offset
;
1010 nfs_list_remove_request(req
);
1012 if ((desc
->pg_ioflags
& FLUSH_COND_STABLE
) &&
1013 (desc
->pg_moreio
|| NFS_I(desc
->pg_inode
)->ncommit
||
1014 desc
->pg_count
> wsize
))
1015 desc
->pg_ioflags
&= ~FLUSH_COND_STABLE
;
1019 nbytes
= desc
->pg_count
;
1021 size_t len
= min(nbytes
, wsize
);
1023 data
= nfs_writedata_alloc(1);
1026 data
->pagevec
[0] = page
;
1027 nfs_write_rpcsetup(req
, data
, len
, offset
, desc
->pg_ioflags
);
1028 list_add(&data
->list
, res
);
1032 } while (nbytes
!= 0);
1033 atomic_set(&req
->wb_complete
, requests
);
1034 desc
->pg_rpc_callops
= &nfs_write_partial_ops
;
1038 while (!list_empty(res
)) {
1039 data
= list_entry(res
->next
, struct nfs_write_data
, list
);
1040 list_del(&data
->list
);
1041 nfs_writedata_release(data
);
1043 nfs_redirty_request(req
);
1048 * Create an RPC task for the given write request and kick it.
1049 * The page must have been locked by the caller.
1051 * It may happen that the page we're passed is not marked dirty.
1052 * This is the case if nfs_updatepage detects a conflicting request
1053 * that has been written but not committed.
1055 static int nfs_flush_one(struct nfs_pageio_descriptor
*desc
, struct list_head
*res
)
1057 struct nfs_page
*req
;
1058 struct page
**pages
;
1059 struct nfs_write_data
*data
;
1060 struct list_head
*head
= &desc
->pg_list
;
1063 data
= nfs_writedata_alloc(nfs_page_array_len(desc
->pg_base
,
1066 while (!list_empty(head
)) {
1067 req
= nfs_list_entry(head
->next
);
1068 nfs_list_remove_request(req
);
1069 nfs_redirty_request(req
);
1074 pages
= data
->pagevec
;
1075 while (!list_empty(head
)) {
1076 req
= nfs_list_entry(head
->next
);
1077 nfs_list_remove_request(req
);
1078 nfs_list_add_request(req
, &data
->pages
);
1079 *pages
++ = req
->wb_page
;
1081 req
= nfs_list_entry(data
->pages
.next
);
1083 if ((desc
->pg_ioflags
& FLUSH_COND_STABLE
) &&
1084 (desc
->pg_moreio
|| NFS_I(desc
->pg_inode
)->ncommit
))
1085 desc
->pg_ioflags
&= ~FLUSH_COND_STABLE
;
1087 /* Set up the argument struct */
1088 nfs_write_rpcsetup(req
, data
, desc
->pg_count
, 0, desc
->pg_ioflags
);
1089 list_add(&data
->list
, res
);
1090 desc
->pg_rpc_callops
= &nfs_write_full_ops
;
1095 int nfs_generic_flush(struct nfs_pageio_descriptor
*desc
, struct list_head
*head
)
1097 if (desc
->pg_bsize
< PAGE_CACHE_SIZE
)
1098 return nfs_flush_multi(desc
, head
);
1099 return nfs_flush_one(desc
, head
);
1102 static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor
*desc
)
1107 ret
= nfs_generic_flush(desc
, &head
);
1109 ret
= nfs_do_multiple_writes(&head
, desc
->pg_rpc_callops
,
1114 static const struct nfs_pageio_ops nfs_pageio_write_ops
= {
1115 .pg_test
= nfs_generic_pg_test
,
1116 .pg_doio
= nfs_generic_pg_writepages
,
1119 void nfs_pageio_init_write_mds(struct nfs_pageio_descriptor
*pgio
,
1120 struct inode
*inode
, int ioflags
)
1122 nfs_pageio_init(pgio
, inode
, &nfs_pageio_write_ops
,
1123 NFS_SERVER(inode
)->wsize
, ioflags
);
1126 void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor
*pgio
)
1128 pgio
->pg_ops
= &nfs_pageio_write_ops
;
1129 pgio
->pg_bsize
= NFS_SERVER(pgio
->pg_inode
)->wsize
;
1131 EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds
);
1133 static void nfs_pageio_init_write(struct nfs_pageio_descriptor
*pgio
,
1134 struct inode
*inode
, int ioflags
)
1136 if (!pnfs_pageio_init_write(pgio
, inode
, ioflags
))
1137 nfs_pageio_init_write_mds(pgio
, inode
, ioflags
);
1141 * Handle a write reply that flushed part of a page.
1143 static void nfs_writeback_done_partial(struct rpc_task
*task
, void *calldata
)
1145 struct nfs_write_data
*data
= calldata
;
1147 dprintk("NFS: %5u write(%s/%lld %d@%lld)",
1149 data
->req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
1151 NFS_FILEID(data
->req
->wb_context
->dentry
->d_inode
),
1152 data
->req
->wb_bytes
, (long long)req_offset(data
->req
));
1154 nfs_writeback_done(task
, data
);
1157 static void nfs_writeback_release_partial(void *calldata
)
1159 struct nfs_write_data
*data
= calldata
;
1160 struct nfs_page
*req
= data
->req
;
1161 struct page
*page
= req
->wb_page
;
1162 int status
= data
->task
.tk_status
;
1165 nfs_set_pageerror(page
);
1166 nfs_context_set_write_error(req
->wb_context
, status
);
1167 dprintk(", error = %d\n", status
);
1171 if (nfs_write_need_commit(data
)) {
1172 struct inode
*inode
= page
->mapping
->host
;
1174 spin_lock(&inode
->i_lock
);
1175 if (test_bit(PG_NEED_RESCHED
, &req
->wb_flags
)) {
1176 /* Do nothing we need to resend the writes */
1177 } else if (!test_and_set_bit(PG_NEED_COMMIT
, &req
->wb_flags
)) {
1178 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
1179 dprintk(" defer commit\n");
1180 } else if (memcmp(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
))) {
1181 set_bit(PG_NEED_RESCHED
, &req
->wb_flags
);
1182 clear_bit(PG_NEED_COMMIT
, &req
->wb_flags
);
1183 dprintk(" server reboot detected\n");
1185 spin_unlock(&inode
->i_lock
);
1190 if (atomic_dec_and_test(&req
->wb_complete
))
1191 nfs_writepage_release(req
, data
);
1192 nfs_writedata_release(calldata
);
1195 #if defined(CONFIG_NFS_V4_1)
1196 void nfs_write_prepare(struct rpc_task
*task
, void *calldata
)
1198 struct nfs_write_data
*data
= calldata
;
1200 if (nfs4_setup_sequence(NFS_SERVER(data
->inode
),
1201 &data
->args
.seq_args
,
1202 &data
->res
.seq_res
, 1, task
))
1204 rpc_call_start(task
);
1206 #endif /* CONFIG_NFS_V4_1 */
1208 static const struct rpc_call_ops nfs_write_partial_ops
= {
1209 #if defined(CONFIG_NFS_V4_1)
1210 .rpc_call_prepare
= nfs_write_prepare
,
1211 #endif /* CONFIG_NFS_V4_1 */
1212 .rpc_call_done
= nfs_writeback_done_partial
,
1213 .rpc_release
= nfs_writeback_release_partial
,
1217 * Handle a write reply that flushes a whole page.
1219 * FIXME: There is an inherent race with invalidate_inode_pages and
1220 * writebacks since the page->count is kept > 1 for as long
1221 * as the page has a write request pending.
1223 static void nfs_writeback_done_full(struct rpc_task
*task
, void *calldata
)
1225 struct nfs_write_data
*data
= calldata
;
1227 nfs_writeback_done(task
, data
);
1230 static void nfs_writeback_release_full(void *calldata
)
1232 struct nfs_write_data
*data
= calldata
;
1233 int status
= data
->task
.tk_status
;
1235 /* Update attributes as result of writeback. */
1236 while (!list_empty(&data
->pages
)) {
1237 struct nfs_page
*req
= nfs_list_entry(data
->pages
.next
);
1238 struct page
*page
= req
->wb_page
;
1240 nfs_list_remove_request(req
);
1242 dprintk("NFS: %5u write (%s/%lld %d@%lld)",
1244 req
->wb_context
->dentry
->d_inode
->i_sb
->s_id
,
1245 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1247 (long long)req_offset(req
));
1250 nfs_set_pageerror(page
);
1251 nfs_context_set_write_error(req
->wb_context
, status
);
1252 dprintk(", error = %d\n", status
);
1253 goto remove_request
;
1256 if (nfs_write_need_commit(data
)) {
1257 memcpy(&req
->wb_verf
, &data
->verf
, sizeof(req
->wb_verf
));
1258 nfs_mark_request_commit(req
, data
->lseg
);
1259 dprintk(" marked for commit\n");
1264 nfs_inode_remove_request(req
);
1266 nfs_clear_page_tag_locked(req
);
1267 nfs_end_page_writeback(page
);
1269 nfs_writedata_release(calldata
);
1272 static const struct rpc_call_ops nfs_write_full_ops
= {
1273 #if defined(CONFIG_NFS_V4_1)
1274 .rpc_call_prepare
= nfs_write_prepare
,
1275 #endif /* CONFIG_NFS_V4_1 */
1276 .rpc_call_done
= nfs_writeback_done_full
,
1277 .rpc_release
= nfs_writeback_release_full
,
1282 * This function is called when the WRITE call is complete.
1284 void nfs_writeback_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
1286 struct nfs_writeargs
*argp
= &data
->args
;
1287 struct nfs_writeres
*resp
= &data
->res
;
1290 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1291 task
->tk_pid
, task
->tk_status
);
1294 * ->write_done will attempt to use post-op attributes to detect
1295 * conflicting writes by other clients. A strict interpretation
1296 * of close-to-open would allow us to continue caching even if
1297 * another writer had changed the file, but some applications
1298 * depend on tighter cache coherency when writing.
1300 status
= NFS_PROTO(data
->inode
)->write_done(task
, data
);
1303 nfs_add_stats(data
->inode
, NFSIOS_SERVERWRITTENBYTES
, resp
->count
);
1305 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1306 if (resp
->verf
->committed
< argp
->stable
&& task
->tk_status
>= 0) {
1307 /* We tried a write call, but the server did not
1308 * commit data to stable storage even though we
1310 * Note: There is a known bug in Tru64 < 5.0 in which
1311 * the server reports NFS_DATA_SYNC, but performs
1312 * NFS_FILE_SYNC. We therefore implement this checking
1313 * as a dprintk() in order to avoid filling syslog.
1315 static unsigned long complain
;
1317 /* Note this will print the MDS for a DS write */
1318 if (time_before(complain
, jiffies
)) {
1319 dprintk("NFS: faulty NFS server %s:"
1320 " (committed = %d) != (stable = %d)\n",
1321 NFS_SERVER(data
->inode
)->nfs_client
->cl_hostname
,
1322 resp
->verf
->committed
, argp
->stable
);
1323 complain
= jiffies
+ 300 * HZ
;
1327 /* Is this a short write? */
1328 if (task
->tk_status
>= 0 && resp
->count
< argp
->count
) {
1329 static unsigned long complain
;
1331 nfs_inc_stats(data
->inode
, NFSIOS_SHORTWRITE
);
1333 /* Has the server at least made some progress? */
1334 if (resp
->count
!= 0) {
1335 /* Was this an NFSv2 write or an NFSv3 stable write? */
1336 if (resp
->verf
->committed
!= NFS_UNSTABLE
) {
1337 /* Resend from where the server left off */
1338 data
->mds_offset
+= resp
->count
;
1339 argp
->offset
+= resp
->count
;
1340 argp
->pgbase
+= resp
->count
;
1341 argp
->count
-= resp
->count
;
1343 /* Resend as a stable write in order to avoid
1344 * headaches in the case of a server crash.
1346 argp
->stable
= NFS_FILE_SYNC
;
1348 rpc_restart_call_prepare(task
);
1351 if (time_before(complain
, jiffies
)) {
1353 "NFS: Server wrote zero bytes, expected %u.\n",
1355 complain
= jiffies
+ 300 * HZ
;
1357 /* Can't do anything about it except throw an error. */
1358 task
->tk_status
= -EIO
;
1364 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1365 static int nfs_commit_set_lock(struct nfs_inode
*nfsi
, int may_wait
)
1369 if (!test_and_set_bit(NFS_INO_COMMIT
, &nfsi
->flags
))
1373 ret
= out_of_line_wait_on_bit_lock(&nfsi
->flags
,
1375 nfs_wait_bit_killable
,
1377 return (ret
< 0) ? ret
: 1;
1380 void nfs_commit_clear_lock(struct nfs_inode
*nfsi
)
1382 clear_bit(NFS_INO_COMMIT
, &nfsi
->flags
);
1383 smp_mb__after_clear_bit();
1384 wake_up_bit(&nfsi
->flags
, NFS_INO_COMMIT
);
1386 EXPORT_SYMBOL_GPL(nfs_commit_clear_lock
);
1388 void nfs_commitdata_release(void *data
)
1390 struct nfs_write_data
*wdata
= data
;
1392 put_lseg(wdata
->lseg
);
1393 put_nfs_open_context(wdata
->args
.context
);
1394 nfs_commit_free(wdata
);
1396 EXPORT_SYMBOL_GPL(nfs_commitdata_release
);
1398 int nfs_initiate_commit(struct nfs_write_data
*data
, struct rpc_clnt
*clnt
,
1399 const struct rpc_call_ops
*call_ops
,
1402 struct rpc_task
*task
;
1403 int priority
= flush_task_priority(how
);
1404 struct rpc_message msg
= {
1405 .rpc_argp
= &data
->args
,
1406 .rpc_resp
= &data
->res
,
1407 .rpc_cred
= data
->cred
,
1409 struct rpc_task_setup task_setup_data
= {
1410 .task
= &data
->task
,
1412 .rpc_message
= &msg
,
1413 .callback_ops
= call_ops
,
1414 .callback_data
= data
,
1415 .workqueue
= nfsiod_workqueue
,
1416 .flags
= RPC_TASK_ASYNC
,
1417 .priority
= priority
,
1419 /* Set up the initial task struct. */
1420 NFS_PROTO(data
->inode
)->commit_setup(data
, &msg
);
1422 dprintk("NFS: %5u initiated commit call\n", data
->task
.tk_pid
);
1424 task
= rpc_run_task(&task_setup_data
);
1426 return PTR_ERR(task
);
1427 if (how
& FLUSH_SYNC
)
1428 rpc_wait_for_completion_task(task
);
1432 EXPORT_SYMBOL_GPL(nfs_initiate_commit
);
1435 * Set up the argument/result storage required for the RPC call.
1437 void nfs_init_commit(struct nfs_write_data
*data
,
1438 struct list_head
*head
,
1439 struct pnfs_layout_segment
*lseg
)
1441 struct nfs_page
*first
= nfs_list_entry(head
->next
);
1442 struct inode
*inode
= first
->wb_context
->dentry
->d_inode
;
1444 /* Set up the RPC argument and reply structs
1445 * NB: take care not to mess about with data->commit et al. */
1447 list_splice_init(head
, &data
->pages
);
1449 data
->inode
= inode
;
1450 data
->cred
= first
->wb_context
->cred
;
1451 data
->lseg
= lseg
; /* reference transferred */
1452 data
->mds_ops
= &nfs_commit_ops
;
1454 data
->args
.fh
= NFS_FH(data
->inode
);
1455 /* Note: we always request a commit of the entire inode */
1456 data
->args
.offset
= 0;
1457 data
->args
.count
= 0;
1458 data
->args
.context
= get_nfs_open_context(first
->wb_context
);
1459 data
->res
.count
= 0;
1460 data
->res
.fattr
= &data
->fattr
;
1461 data
->res
.verf
= &data
->verf
;
1462 nfs_fattr_init(&data
->fattr
);
1464 EXPORT_SYMBOL_GPL(nfs_init_commit
);
1466 void nfs_retry_commit(struct list_head
*page_list
,
1467 struct pnfs_layout_segment
*lseg
)
1469 struct nfs_page
*req
;
1471 while (!list_empty(page_list
)) {
1472 req
= nfs_list_entry(page_list
->next
);
1473 nfs_list_remove_request(req
);
1474 nfs_mark_request_commit(req
, lseg
);
1475 dec_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
1476 dec_bdi_stat(req
->wb_page
->mapping
->backing_dev_info
,
1478 nfs_clear_page_tag_locked(req
);
1481 EXPORT_SYMBOL_GPL(nfs_retry_commit
);
1484 * Commit dirty pages
1487 nfs_commit_list(struct inode
*inode
, struct list_head
*head
, int how
)
1489 struct nfs_write_data
*data
;
1491 data
= nfs_commitdata_alloc();
1496 /* Set up the argument struct */
1497 nfs_init_commit(data
, head
, NULL
);
1498 return nfs_initiate_commit(data
, NFS_CLIENT(inode
), data
->mds_ops
, how
);
1500 nfs_retry_commit(head
, NULL
);
1501 nfs_commit_clear_lock(NFS_I(inode
));
1506 * COMMIT call returned
1508 static void nfs_commit_done(struct rpc_task
*task
, void *calldata
)
1510 struct nfs_write_data
*data
= calldata
;
1512 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1513 task
->tk_pid
, task
->tk_status
);
1515 /* Call the NFS version-specific code */
1516 NFS_PROTO(data
->inode
)->commit_done(task
, data
);
1519 void nfs_commit_release_pages(struct nfs_write_data
*data
)
1521 struct nfs_page
*req
;
1522 int status
= data
->task
.tk_status
;
1524 while (!list_empty(&data
->pages
)) {
1525 req
= nfs_list_entry(data
->pages
.next
);
1526 nfs_list_remove_request(req
);
1527 nfs_clear_request_commit(req
);
1529 dprintk("NFS: commit (%s/%lld %d@%lld)",
1530 req
->wb_context
->dentry
->d_sb
->s_id
,
1531 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1533 (long long)req_offset(req
));
1535 nfs_context_set_write_error(req
->wb_context
, status
);
1536 nfs_inode_remove_request(req
);
1537 dprintk(", error = %d\n", status
);
1541 /* Okay, COMMIT succeeded, apparently. Check the verifier
1542 * returned by the server against all stored verfs. */
1543 if (!memcmp(req
->wb_verf
.verifier
, data
->verf
.verifier
, sizeof(data
->verf
.verifier
))) {
1544 /* We have a match */
1545 nfs_inode_remove_request(req
);
1549 /* We have a mismatch. Write the page again */
1550 dprintk(" mismatch\n");
1551 nfs_mark_request_dirty(req
);
1553 nfs_clear_page_tag_locked(req
);
1556 EXPORT_SYMBOL_GPL(nfs_commit_release_pages
);
1558 static void nfs_commit_release(void *calldata
)
1560 struct nfs_write_data
*data
= calldata
;
1562 nfs_commit_release_pages(data
);
1563 nfs_commit_clear_lock(NFS_I(data
->inode
));
1564 nfs_commitdata_release(calldata
);
1567 static const struct rpc_call_ops nfs_commit_ops
= {
1568 #if defined(CONFIG_NFS_V4_1)
1569 .rpc_call_prepare
= nfs_write_prepare
,
1570 #endif /* CONFIG_NFS_V4_1 */
1571 .rpc_call_done
= nfs_commit_done
,
1572 .rpc_release
= nfs_commit_release
,
1575 int nfs_commit_inode(struct inode
*inode
, int how
)
1578 int may_wait
= how
& FLUSH_SYNC
;
1581 res
= nfs_commit_set_lock(NFS_I(inode
), may_wait
);
1583 goto out_mark_dirty
;
1584 res
= nfs_scan_commit(inode
, &head
, 0, 0);
1588 error
= pnfs_commit_list(inode
, &head
, how
);
1589 if (error
== PNFS_NOT_ATTEMPTED
)
1590 error
= nfs_commit_list(inode
, &head
, how
);
1594 goto out_mark_dirty
;
1595 error
= wait_on_bit(&NFS_I(inode
)->flags
,
1597 nfs_wait_bit_killable
,
1602 nfs_commit_clear_lock(NFS_I(inode
));
1604 /* Note: If we exit without ensuring that the commit is complete,
1605 * we must mark the inode as dirty. Otherwise, future calls to
1606 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1607 * that the data is on the disk.
1610 __mark_inode_dirty(inode
, I_DIRTY_DATASYNC
);
1614 static int nfs_commit_unstable_pages(struct inode
*inode
, struct writeback_control
*wbc
)
1616 struct nfs_inode
*nfsi
= NFS_I(inode
);
1617 int flags
= FLUSH_SYNC
;
1620 /* no commits means nothing needs to be done */
1624 if (wbc
->sync_mode
== WB_SYNC_NONE
) {
1625 /* Don't commit yet if this is a non-blocking flush and there
1626 * are a lot of outstanding writes for this mapping.
1628 if (nfsi
->ncommit
<= (nfsi
->npages
>> 1))
1629 goto out_mark_dirty
;
1631 /* don't wait for the COMMIT response */
1635 ret
= nfs_commit_inode(inode
, flags
);
1637 if (wbc
->sync_mode
== WB_SYNC_NONE
) {
1638 if (ret
< wbc
->nr_to_write
)
1639 wbc
->nr_to_write
-= ret
;
1641 wbc
->nr_to_write
= 0;
1646 __mark_inode_dirty(inode
, I_DIRTY_DATASYNC
);
1650 static int nfs_commit_unstable_pages(struct inode
*inode
, struct writeback_control
*wbc
)
1656 int nfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1660 ret
= nfs_commit_unstable_pages(inode
, wbc
);
1661 if (ret
>= 0 && test_bit(NFS_INO_LAYOUTCOMMIT
, &NFS_I(inode
)->flags
)) {
1665 if (wbc
->sync_mode
== WB_SYNC_NONE
)
1668 status
= pnfs_layoutcommit_inode(inode
, sync
);
1676 * flush the inode to disk.
1678 int nfs_wb_all(struct inode
*inode
)
1680 struct writeback_control wbc
= {
1681 .sync_mode
= WB_SYNC_ALL
,
1682 .nr_to_write
= LONG_MAX
,
1684 .range_end
= LLONG_MAX
,
1687 return sync_inode(inode
, &wbc
);
1690 int nfs_wb_page_cancel(struct inode
*inode
, struct page
*page
)
1692 struct nfs_page
*req
;
1695 BUG_ON(!PageLocked(page
));
1697 wait_on_page_writeback(page
);
1698 req
= nfs_page_find_request(page
);
1701 if (nfs_lock_request_dontget(req
)) {
1702 nfs_inode_remove_request(req
);
1704 * In case nfs_inode_remove_request has marked the
1705 * page as being dirty
1707 cancel_dirty_page(page
, PAGE_CACHE_SIZE
);
1708 nfs_unlock_request(req
);
1711 ret
= nfs_wait_on_request(req
);
1712 nfs_release_request(req
);
1720 * Write back all requests on one page - we do this before reading it.
1722 int nfs_wb_page(struct inode
*inode
, struct page
*page
)
1724 loff_t range_start
= page_offset(page
);
1725 loff_t range_end
= range_start
+ (loff_t
)(PAGE_CACHE_SIZE
- 1);
1726 struct writeback_control wbc
= {
1727 .sync_mode
= WB_SYNC_ALL
,
1729 .range_start
= range_start
,
1730 .range_end
= range_end
,
1735 wait_on_page_writeback(page
);
1736 if (clear_page_dirty_for_io(page
)) {
1737 ret
= nfs_writepage_locked(page
, &wbc
);
1742 if (!PagePrivate(page
))
1744 ret
= nfs_commit_inode(inode
, FLUSH_SYNC
);
1753 #ifdef CONFIG_MIGRATION
1754 int nfs_migrate_page(struct address_space
*mapping
, struct page
*newpage
,
1755 struct page
*page
, enum migrate_mode mode
)
1758 * If PagePrivate is set, then the page is currently associated with
1759 * an in-progress read or write request. Don't try to migrate it.
1761 * FIXME: we could do this in principle, but we'll need a way to ensure
1762 * that we can safely release the inode reference while holding
1765 if (PagePrivate(page
))
1768 nfs_fscache_release_page(page
, GFP_KERNEL
);
1770 return migrate_page(mapping
, newpage
, page
, mode
);
1774 int __init
nfs_init_writepagecache(void)
1776 nfs_wdata_cachep
= kmem_cache_create("nfs_write_data",
1777 sizeof(struct nfs_write_data
),
1778 0, SLAB_HWCACHE_ALIGN
,
1780 if (nfs_wdata_cachep
== NULL
)
1783 nfs_wdata_mempool
= mempool_create_slab_pool(MIN_POOL_WRITE
,
1785 if (nfs_wdata_mempool
== NULL
)
1788 nfs_commit_mempool
= mempool_create_slab_pool(MIN_POOL_COMMIT
,
1790 if (nfs_commit_mempool
== NULL
)
1794 * NFS congestion size, scale with available memory.
1806 * This allows larger machines to have larger/more transfers.
1807 * Limit the default to 256M
1809 nfs_congestion_kb
= (16*int_sqrt(totalram_pages
)) << (PAGE_SHIFT
-10);
1810 if (nfs_congestion_kb
> 256*1024)
1811 nfs_congestion_kb
= 256*1024;
1816 void nfs_destroy_writepagecache(void)
1818 mempool_destroy(nfs_commit_mempool
);
1819 mempool_destroy(nfs_wdata_mempool
);
1820 kmem_cache_destroy(nfs_wdata_cachep
);