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_redirty_request(struct nfs_page
*req
);
43 static const struct rpc_call_ops nfs_write_common_ops
;
44 static const struct rpc_call_ops nfs_commit_ops
;
45 static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops
;
46 static const struct nfs_commit_completion_ops nfs_commit_completion_ops
;
48 static struct kmem_cache
*nfs_wdata_cachep
;
49 static mempool_t
*nfs_wdata_mempool
;
50 static struct kmem_cache
*nfs_cdata_cachep
;
51 static mempool_t
*nfs_commit_mempool
;
53 struct nfs_commit_data
*nfs_commitdata_alloc(void)
55 struct nfs_commit_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_commit_data
*p
)
67 mempool_free(p
, nfs_commit_mempool
);
69 EXPORT_SYMBOL_GPL(nfs_commit_free
);
71 struct nfs_write_header
*nfs_writehdr_alloc(void)
73 struct nfs_write_header
*p
= mempool_alloc(nfs_wdata_mempool
, GFP_NOFS
);
76 struct nfs_pgio_header
*hdr
= &p
->header
;
78 memset(p
, 0, sizeof(*p
));
79 INIT_LIST_HEAD(&hdr
->pages
);
80 INIT_LIST_HEAD(&hdr
->rpc_list
);
81 spin_lock_init(&hdr
->lock
);
82 atomic_set(&hdr
->refcnt
, 0);
88 static struct nfs_write_data
*nfs_writedata_alloc(struct nfs_pgio_header
*hdr
,
89 unsigned int pagecount
)
91 struct nfs_write_data
*data
, *prealloc
;
93 prealloc
= &container_of(hdr
, struct nfs_write_header
, header
)->rpc_data
;
94 if (prealloc
->header
== NULL
)
97 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
101 if (nfs_pgarray_set(&data
->pages
, pagecount
)) {
103 atomic_inc(&hdr
->refcnt
);
105 if (data
!= prealloc
)
113 void nfs_writehdr_free(struct nfs_pgio_header
*hdr
)
115 struct nfs_write_header
*whdr
= container_of(hdr
, struct nfs_write_header
, header
);
116 mempool_free(whdr
, nfs_wdata_mempool
);
119 void nfs_writedata_release(struct nfs_write_data
*wdata
)
121 struct nfs_pgio_header
*hdr
= wdata
->header
;
122 struct nfs_write_header
*write_header
= container_of(hdr
, struct nfs_write_header
, header
);
124 put_nfs_open_context(wdata
->args
.context
);
125 if (wdata
->pages
.pagevec
!= wdata
->pages
.page_array
)
126 kfree(wdata
->pages
.pagevec
);
127 if (wdata
!= &write_header
->rpc_data
)
130 wdata
->header
= NULL
;
131 if (atomic_dec_and_test(&hdr
->refcnt
))
132 hdr
->completion_ops
->completion(hdr
);
135 static void nfs_context_set_write_error(struct nfs_open_context
*ctx
, int error
)
139 set_bit(NFS_CONTEXT_ERROR_WRITE
, &ctx
->flags
);
142 static struct nfs_page
*nfs_page_find_request_locked(struct page
*page
)
144 struct nfs_page
*req
= NULL
;
146 if (PagePrivate(page
)) {
147 req
= (struct nfs_page
*)page_private(page
);
149 kref_get(&req
->wb_kref
);
154 static struct nfs_page
*nfs_page_find_request(struct page
*page
)
156 struct inode
*inode
= page
->mapping
->host
;
157 struct nfs_page
*req
= NULL
;
159 spin_lock(&inode
->i_lock
);
160 req
= nfs_page_find_request_locked(page
);
161 spin_unlock(&inode
->i_lock
);
165 /* Adjust the file length if we're writing beyond the end */
166 static void nfs_grow_file(struct page
*page
, unsigned int offset
, unsigned int count
)
168 struct inode
*inode
= page
->mapping
->host
;
172 spin_lock(&inode
->i_lock
);
173 i_size
= i_size_read(inode
);
174 end_index
= (i_size
- 1) >> PAGE_CACHE_SHIFT
;
175 if (i_size
> 0 && page
->index
< end_index
)
177 end
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + ((loff_t
)offset
+count
);
180 i_size_write(inode
, end
);
181 nfs_inc_stats(inode
, NFSIOS_EXTENDWRITE
);
183 spin_unlock(&inode
->i_lock
);
186 /* A writeback failed: mark the page as bad, and invalidate the page cache */
187 static void nfs_set_pageerror(struct page
*page
)
190 nfs_zap_mapping(page
->mapping
->host
, page
->mapping
);
193 /* We can set the PG_uptodate flag if we see that a write request
194 * covers the full page.
196 static void nfs_mark_uptodate(struct page
*page
, unsigned int base
, unsigned int count
)
198 if (PageUptodate(page
))
202 if (count
!= nfs_page_length(page
))
204 SetPageUptodate(page
);
207 static int wb_priority(struct writeback_control
*wbc
)
209 if (wbc
->for_reclaim
)
210 return FLUSH_HIGHPRI
| FLUSH_STABLE
;
211 if (wbc
->for_kupdate
|| wbc
->for_background
)
212 return FLUSH_LOWPRI
| FLUSH_COND_STABLE
;
213 return FLUSH_COND_STABLE
;
217 * NFS congestion control
220 int nfs_congestion_kb
;
222 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
223 #define NFS_CONGESTION_OFF_THRESH \
224 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
226 static int nfs_set_page_writeback(struct page
*page
)
228 int ret
= test_set_page_writeback(page
);
231 struct inode
*inode
= page
->mapping
->host
;
232 struct nfs_server
*nfss
= NFS_SERVER(inode
);
234 if (atomic_long_inc_return(&nfss
->writeback
) >
235 NFS_CONGESTION_ON_THRESH
) {
236 set_bdi_congested(&nfss
->backing_dev_info
,
243 static void nfs_end_page_writeback(struct page
*page
)
245 struct inode
*inode
= page
->mapping
->host
;
246 struct nfs_server
*nfss
= NFS_SERVER(inode
);
248 end_page_writeback(page
);
249 if (atomic_long_dec_return(&nfss
->writeback
) < NFS_CONGESTION_OFF_THRESH
)
250 clear_bdi_congested(&nfss
->backing_dev_info
, BLK_RW_ASYNC
);
253 static struct nfs_page
*nfs_find_and_lock_request(struct page
*page
, bool nonblock
)
255 struct inode
*inode
= page
->mapping
->host
;
256 struct nfs_page
*req
;
259 spin_lock(&inode
->i_lock
);
261 req
= nfs_page_find_request_locked(page
);
264 if (nfs_lock_request(req
))
266 /* Note: If we hold the page lock, as is the case in nfs_writepage,
267 * then the call to nfs_lock_request() will always
268 * succeed provided that someone hasn't already marked the
269 * request as dirty (in which case we don't care).
271 spin_unlock(&inode
->i_lock
);
273 ret
= nfs_wait_on_request(req
);
276 nfs_release_request(req
);
279 spin_lock(&inode
->i_lock
);
281 spin_unlock(&inode
->i_lock
);
286 * Find an associated nfs write request, and prepare to flush it out
287 * May return an error if the user signalled nfs_wait_on_request().
289 static int nfs_page_async_flush(struct nfs_pageio_descriptor
*pgio
,
290 struct page
*page
, bool nonblock
)
292 struct nfs_page
*req
;
295 req
= nfs_find_and_lock_request(page
, nonblock
);
302 ret
= nfs_set_page_writeback(page
);
304 BUG_ON(test_bit(PG_CLEAN
, &req
->wb_flags
));
306 if (!nfs_pageio_add_request(pgio
, req
)) {
307 nfs_redirty_request(req
);
308 ret
= pgio
->pg_error
;
314 static int nfs_do_writepage(struct page
*page
, struct writeback_control
*wbc
, struct nfs_pageio_descriptor
*pgio
)
316 struct inode
*inode
= page
->mapping
->host
;
319 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGE
);
320 nfs_add_stats(inode
, NFSIOS_WRITEPAGES
, 1);
322 nfs_pageio_cond_complete(pgio
, page
->index
);
323 ret
= nfs_page_async_flush(pgio
, page
, wbc
->sync_mode
== WB_SYNC_NONE
);
324 if (ret
== -EAGAIN
) {
325 redirty_page_for_writepage(wbc
, page
);
332 * Write an mmapped page to the server.
334 static int nfs_writepage_locked(struct page
*page
, struct writeback_control
*wbc
)
336 struct nfs_pageio_descriptor pgio
;
339 NFS_PROTO(page
->mapping
->host
)->write_pageio_init(&pgio
,
342 &nfs_async_write_completion_ops
);
343 err
= nfs_do_writepage(page
, wbc
, &pgio
);
344 nfs_pageio_complete(&pgio
);
347 if (pgio
.pg_error
< 0)
348 return pgio
.pg_error
;
352 int nfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
356 ret
= nfs_writepage_locked(page
, wbc
);
361 static int nfs_writepages_callback(struct page
*page
, struct writeback_control
*wbc
, void *data
)
365 ret
= nfs_do_writepage(page
, wbc
, data
);
370 int nfs_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
372 struct inode
*inode
= mapping
->host
;
373 unsigned long *bitlock
= &NFS_I(inode
)->flags
;
374 struct nfs_pageio_descriptor pgio
;
377 /* Stop dirtying of new pages while we sync */
378 err
= wait_on_bit_lock(bitlock
, NFS_INO_FLUSHING
,
379 nfs_wait_bit_killable
, TASK_KILLABLE
);
383 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGES
);
385 NFS_PROTO(inode
)->write_pageio_init(&pgio
, inode
, wb_priority(wbc
), &nfs_async_write_completion_ops
);
386 err
= write_cache_pages(mapping
, wbc
, nfs_writepages_callback
, &pgio
);
387 nfs_pageio_complete(&pgio
);
389 clear_bit_unlock(NFS_INO_FLUSHING
, bitlock
);
390 smp_mb__after_clear_bit();
391 wake_up_bit(bitlock
, NFS_INO_FLUSHING
);
404 * Insert a write request into an inode
406 static void nfs_inode_add_request(struct inode
*inode
, struct nfs_page
*req
)
408 struct nfs_inode
*nfsi
= NFS_I(inode
);
410 /* Lock the request! */
411 nfs_lock_request(req
);
413 spin_lock(&inode
->i_lock
);
414 if (!nfsi
->npages
&& NFS_PROTO(inode
)->have_delegation(inode
, FMODE_WRITE
))
416 set_bit(PG_MAPPED
, &req
->wb_flags
);
417 SetPagePrivate(req
->wb_page
);
418 set_page_private(req
->wb_page
, (unsigned long)req
);
420 kref_get(&req
->wb_kref
);
421 spin_unlock(&inode
->i_lock
);
425 * Remove a write request from an inode
427 static void nfs_inode_remove_request(struct nfs_page
*req
)
429 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
430 struct nfs_inode
*nfsi
= NFS_I(inode
);
432 BUG_ON (!NFS_WBACK_BUSY(req
));
434 spin_lock(&inode
->i_lock
);
435 set_page_private(req
->wb_page
, 0);
436 ClearPagePrivate(req
->wb_page
);
437 clear_bit(PG_MAPPED
, &req
->wb_flags
);
439 spin_unlock(&inode
->i_lock
);
440 nfs_release_request(req
);
444 nfs_mark_request_dirty(struct nfs_page
*req
)
446 __set_page_dirty_nobuffers(req
->wb_page
);
449 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
451 * nfs_request_add_commit_list - add request to a commit list
452 * @req: pointer to a struct nfs_page
453 * @dst: commit list head
454 * @cinfo: holds list lock and accounting info
456 * This sets the PG_CLEAN bit, updates the cinfo count of
457 * number of outstanding requests requiring a commit as well as
460 * The caller must _not_ hold the cinfo->lock, but must be
461 * holding the nfs_page lock.
464 nfs_request_add_commit_list(struct nfs_page
*req
, struct list_head
*dst
,
465 struct nfs_commit_info
*cinfo
)
467 set_bit(PG_CLEAN
, &(req
)->wb_flags
);
468 spin_lock(cinfo
->lock
);
469 nfs_list_add_request(req
, dst
);
470 cinfo
->mds
->ncommit
++;
471 spin_unlock(cinfo
->lock
);
473 inc_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
474 inc_bdi_stat(req
->wb_page
->mapping
->backing_dev_info
,
476 __mark_inode_dirty(req
->wb_context
->dentry
->d_inode
,
480 EXPORT_SYMBOL_GPL(nfs_request_add_commit_list
);
483 * nfs_request_remove_commit_list - Remove request from a commit list
484 * @req: pointer to a nfs_page
485 * @cinfo: holds list lock and accounting info
487 * This clears the PG_CLEAN bit, and updates the cinfo's count of
488 * number of outstanding requests requiring a commit
489 * It does not update the MM page stats.
491 * The caller _must_ hold the cinfo->lock and the nfs_page lock.
494 nfs_request_remove_commit_list(struct nfs_page
*req
,
495 struct nfs_commit_info
*cinfo
)
497 if (!test_and_clear_bit(PG_CLEAN
, &(req
)->wb_flags
))
499 nfs_list_remove_request(req
);
500 cinfo
->mds
->ncommit
--;
502 EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list
);
504 static void nfs_init_cinfo_from_inode(struct nfs_commit_info
*cinfo
,
507 cinfo
->lock
= &inode
->i_lock
;
508 cinfo
->mds
= &NFS_I(inode
)->commit_info
;
509 cinfo
->ds
= pnfs_get_ds_info(inode
);
511 cinfo
->completion_ops
= &nfs_commit_completion_ops
;
514 void nfs_init_cinfo(struct nfs_commit_info
*cinfo
,
516 struct nfs_direct_req
*dreq
)
519 nfs_init_cinfo_from_dreq(cinfo
, dreq
);
521 nfs_init_cinfo_from_inode(cinfo
, inode
);
523 EXPORT_SYMBOL_GPL(nfs_init_cinfo
);
526 * Add a request to the inode's commit list.
529 nfs_mark_request_commit(struct nfs_page
*req
, struct pnfs_layout_segment
*lseg
,
530 struct nfs_commit_info
*cinfo
)
532 if (pnfs_mark_request_commit(req
, lseg
, cinfo
))
534 nfs_request_add_commit_list(req
, &cinfo
->mds
->list
, cinfo
);
538 nfs_clear_page_commit(struct page
*page
)
540 dec_zone_page_state(page
, NR_UNSTABLE_NFS
);
541 dec_bdi_stat(page
->mapping
->backing_dev_info
, BDI_RECLAIMABLE
);
545 nfs_clear_request_commit(struct nfs_page
*req
)
547 if (test_bit(PG_CLEAN
, &req
->wb_flags
)) {
548 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
549 struct nfs_commit_info cinfo
;
551 nfs_init_cinfo_from_inode(&cinfo
, inode
);
552 if (!pnfs_clear_request_commit(req
, &cinfo
)) {
553 spin_lock(cinfo
.lock
);
554 nfs_request_remove_commit_list(req
, &cinfo
);
555 spin_unlock(cinfo
.lock
);
557 nfs_clear_page_commit(req
->wb_page
);
562 int nfs_write_need_commit(struct nfs_write_data
*data
)
564 if (data
->verf
.committed
== NFS_DATA_SYNC
)
565 return data
->header
->lseg
== NULL
;
566 return data
->verf
.committed
!= NFS_FILE_SYNC
;
570 static void nfs_init_cinfo_from_inode(struct nfs_commit_info
*cinfo
,
575 void nfs_init_cinfo(struct nfs_commit_info
*cinfo
,
577 struct nfs_direct_req
*dreq
)
582 nfs_mark_request_commit(struct nfs_page
*req
, struct pnfs_layout_segment
*lseg
,
583 struct nfs_commit_info
*cinfo
)
588 nfs_clear_request_commit(struct nfs_page
*req
)
593 int nfs_write_need_commit(struct nfs_write_data
*data
)
600 static void nfs_write_completion(struct nfs_pgio_header
*hdr
)
602 struct nfs_commit_info cinfo
;
603 unsigned long bytes
= 0;
605 if (test_bit(NFS_IOHDR_REDO
, &hdr
->flags
))
607 nfs_init_cinfo_from_inode(&cinfo
, hdr
->inode
);
608 while (!list_empty(&hdr
->pages
)) {
609 struct nfs_page
*req
= nfs_list_entry(hdr
->pages
.next
);
611 bytes
+= req
->wb_bytes
;
612 nfs_list_remove_request(req
);
613 if (test_bit(NFS_IOHDR_ERROR
, &hdr
->flags
) &&
614 (hdr
->good_bytes
< bytes
)) {
615 nfs_set_pageerror(req
->wb_page
);
616 nfs_context_set_write_error(req
->wb_context
, hdr
->error
);
619 if (test_bit(NFS_IOHDR_NEED_RESCHED
, &hdr
->flags
)) {
620 nfs_mark_request_dirty(req
);
623 if (test_bit(NFS_IOHDR_NEED_COMMIT
, &hdr
->flags
)) {
624 memcpy(&req
->wb_verf
, &hdr
->verf
->verifier
, sizeof(req
->wb_verf
));
625 nfs_mark_request_commit(req
, hdr
->lseg
, &cinfo
);
629 nfs_inode_remove_request(req
);
631 nfs_unlock_request(req
);
632 nfs_end_page_writeback(req
->wb_page
);
633 nfs_release_request(req
);
639 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
641 nfs_reqs_to_commit(struct nfs_commit_info
*cinfo
)
643 return cinfo
->mds
->ncommit
;
646 /* cinfo->lock held by caller */
648 nfs_scan_commit_list(struct list_head
*src
, struct list_head
*dst
,
649 struct nfs_commit_info
*cinfo
, int max
)
651 struct nfs_page
*req
, *tmp
;
654 list_for_each_entry_safe(req
, tmp
, src
, wb_list
) {
655 if (!nfs_lock_request(req
))
657 kref_get(&req
->wb_kref
);
658 if (cond_resched_lock(cinfo
->lock
))
659 list_safe_reset_next(req
, tmp
, wb_list
);
660 nfs_request_remove_commit_list(req
, cinfo
);
661 nfs_list_add_request(req
, dst
);
663 if ((ret
== max
) && !cinfo
->dreq
)
670 * nfs_scan_commit - Scan an inode for commit requests
671 * @inode: NFS inode to scan
672 * @dst: mds destination list
673 * @cinfo: mds and ds lists of reqs ready to commit
675 * Moves requests from the inode's 'commit' request list.
676 * The requests are *not* checked to ensure that they form a contiguous set.
679 nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
,
680 struct nfs_commit_info
*cinfo
)
684 spin_lock(cinfo
->lock
);
685 if (cinfo
->mds
->ncommit
> 0) {
686 const int max
= INT_MAX
;
688 ret
= nfs_scan_commit_list(&cinfo
->mds
->list
, dst
,
690 ret
+= pnfs_scan_commit_lists(inode
, cinfo
, max
- ret
);
692 spin_unlock(cinfo
->lock
);
697 static unsigned long nfs_reqs_to_commit(struct nfs_commit_info
*cinfo
)
702 int nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
,
703 struct nfs_commit_info
*cinfo
)
710 * Search for an existing write request, and attempt to update
711 * it to reflect a new dirty region on a given page.
713 * If the attempt fails, then the existing request is flushed out
716 static struct nfs_page
*nfs_try_to_update_request(struct inode
*inode
,
721 struct nfs_page
*req
;
726 if (!PagePrivate(page
))
729 end
= offset
+ bytes
;
730 spin_lock(&inode
->i_lock
);
733 req
= nfs_page_find_request_locked(page
);
737 rqend
= req
->wb_offset
+ req
->wb_bytes
;
739 * Tell the caller to flush out the request if
740 * the offsets are non-contiguous.
741 * Note: nfs_flush_incompatible() will already
742 * have flushed out requests having wrong owners.
745 || end
< req
->wb_offset
)
748 if (nfs_lock_request(req
))
751 /* The request is locked, so wait and then retry */
752 spin_unlock(&inode
->i_lock
);
753 error
= nfs_wait_on_request(req
);
754 nfs_release_request(req
);
757 spin_lock(&inode
->i_lock
);
760 /* Okay, the request matches. Update the region */
761 if (offset
< req
->wb_offset
) {
762 req
->wb_offset
= offset
;
763 req
->wb_pgbase
= offset
;
766 req
->wb_bytes
= end
- req
->wb_offset
;
768 req
->wb_bytes
= rqend
- req
->wb_offset
;
770 spin_unlock(&inode
->i_lock
);
772 nfs_clear_request_commit(req
);
775 spin_unlock(&inode
->i_lock
);
776 nfs_release_request(req
);
777 error
= nfs_wb_page(inode
, page
);
779 return ERR_PTR(error
);
783 * Try to update an existing write request, or create one if there is none.
785 * Note: Should always be called with the Page Lock held to prevent races
786 * if we have to add a new request. Also assumes that the caller has
787 * already called nfs_flush_incompatible() if necessary.
789 static struct nfs_page
* nfs_setup_write_request(struct nfs_open_context
* ctx
,
790 struct page
*page
, unsigned int offset
, unsigned int bytes
)
792 struct inode
*inode
= page
->mapping
->host
;
793 struct nfs_page
*req
;
795 req
= nfs_try_to_update_request(inode
, page
, offset
, bytes
);
798 req
= nfs_create_request(ctx
, inode
, page
, offset
, bytes
);
801 nfs_inode_add_request(inode
, req
);
806 static int nfs_writepage_setup(struct nfs_open_context
*ctx
, struct page
*page
,
807 unsigned int offset
, unsigned int count
)
809 struct nfs_page
*req
;
811 req
= nfs_setup_write_request(ctx
, page
, offset
, count
);
814 /* Update file length */
815 nfs_grow_file(page
, offset
, count
);
816 nfs_mark_uptodate(page
, req
->wb_pgbase
, req
->wb_bytes
);
817 nfs_mark_request_dirty(req
);
818 nfs_unlock_and_release_request(req
);
822 int nfs_flush_incompatible(struct file
*file
, struct page
*page
)
824 struct nfs_open_context
*ctx
= nfs_file_open_context(file
);
825 struct nfs_page
*req
;
826 int do_flush
, status
;
828 * Look for a request corresponding to this page. If there
829 * is one, and it belongs to another file, we flush it out
830 * before we try to copy anything into the page. Do this
831 * due to the lack of an ACCESS-type call in NFSv2.
832 * Also do the same if we find a request from an existing
836 req
= nfs_page_find_request(page
);
839 do_flush
= req
->wb_page
!= page
|| req
->wb_context
!= ctx
||
840 req
->wb_lock_context
->lockowner
!= current
->files
||
841 req
->wb_lock_context
->pid
!= current
->tgid
;
842 nfs_release_request(req
);
845 status
= nfs_wb_page(page
->mapping
->host
, page
);
846 } while (status
== 0);
851 * If the page cache is marked as unsafe or invalid, then we can't rely on
852 * the PageUptodate() flag. In this case, we will need to turn off
853 * write optimisations that depend on the page contents being correct.
855 static bool nfs_write_pageuptodate(struct page
*page
, struct inode
*inode
)
857 if (nfs_have_delegated_attributes(inode
))
859 if (NFS_I(inode
)->cache_validity
& NFS_INO_REVAL_PAGECACHE
)
862 return PageUptodate(page
) != 0;
866 * Update and possibly write a cached page of an NFS file.
868 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
869 * things with a page scheduled for an RPC call (e.g. invalidate it).
871 int nfs_updatepage(struct file
*file
, struct page
*page
,
872 unsigned int offset
, unsigned int count
)
874 struct nfs_open_context
*ctx
= nfs_file_open_context(file
);
875 struct inode
*inode
= page
->mapping
->host
;
878 nfs_inc_stats(inode
, NFSIOS_VFSUPDATEPAGE
);
880 dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n",
881 file
->f_path
.dentry
->d_parent
->d_name
.name
,
882 file
->f_path
.dentry
->d_name
.name
, count
,
883 (long long)(page_offset(page
) + offset
));
885 /* If we're not using byte range locks, and we know the page
886 * is up to date, it may be more efficient to extend the write
887 * to cover the entire page in order to avoid fragmentation
890 if (nfs_write_pageuptodate(page
, inode
) &&
891 inode
->i_flock
== NULL
&&
892 !(file
->f_flags
& O_DSYNC
)) {
893 count
= max(count
+ offset
, nfs_page_length(page
));
897 status
= nfs_writepage_setup(ctx
, page
, offset
, count
);
899 nfs_set_pageerror(page
);
901 __set_page_dirty_nobuffers(page
);
903 dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
904 status
, (long long)i_size_read(inode
));
908 static int flush_task_priority(int how
)
910 switch (how
& (FLUSH_HIGHPRI
|FLUSH_LOWPRI
)) {
912 return RPC_PRIORITY_HIGH
;
914 return RPC_PRIORITY_LOW
;
916 return RPC_PRIORITY_NORMAL
;
919 int nfs_initiate_write(struct rpc_clnt
*clnt
,
920 struct nfs_write_data
*data
,
921 const struct rpc_call_ops
*call_ops
,
924 struct inode
*inode
= data
->header
->inode
;
925 int priority
= flush_task_priority(how
);
926 struct rpc_task
*task
;
927 struct rpc_message msg
= {
928 .rpc_argp
= &data
->args
,
929 .rpc_resp
= &data
->res
,
930 .rpc_cred
= data
->header
->cred
,
932 struct rpc_task_setup task_setup_data
= {
936 .callback_ops
= call_ops
,
937 .callback_data
= data
,
938 .workqueue
= nfsiod_workqueue
,
939 .flags
= RPC_TASK_ASYNC
| flags
,
940 .priority
= priority
,
944 /* Set up the initial task struct. */
945 NFS_PROTO(inode
)->write_setup(data
, &msg
);
947 dprintk("NFS: %5u initiated write call "
948 "(req %s/%lld, %u bytes @ offset %llu)\n",
951 (long long)NFS_FILEID(inode
),
953 (unsigned long long)data
->args
.offset
);
955 task
= rpc_run_task(&task_setup_data
);
960 if (how
& FLUSH_SYNC
) {
961 ret
= rpc_wait_for_completion_task(task
);
963 ret
= task
->tk_status
;
969 EXPORT_SYMBOL_GPL(nfs_initiate_write
);
972 * Set up the argument/result storage required for the RPC call.
974 static void nfs_write_rpcsetup(struct nfs_write_data
*data
,
975 unsigned int count
, unsigned int offset
,
976 int how
, struct nfs_commit_info
*cinfo
)
978 struct nfs_page
*req
= data
->header
->req
;
980 /* Set up the RPC argument and reply structs
981 * NB: take care not to mess about with data->commit et al. */
983 data
->args
.fh
= NFS_FH(data
->header
->inode
);
984 data
->args
.offset
= req_offset(req
) + offset
;
985 /* pnfs_set_layoutcommit needs this */
986 data
->mds_offset
= data
->args
.offset
;
987 data
->args
.pgbase
= req
->wb_pgbase
+ offset
;
988 data
->args
.pages
= data
->pages
.pagevec
;
989 data
->args
.count
= count
;
990 data
->args
.context
= get_nfs_open_context(req
->wb_context
);
991 data
->args
.lock_context
= req
->wb_lock_context
;
992 data
->args
.stable
= NFS_UNSTABLE
;
993 switch (how
& (FLUSH_STABLE
| FLUSH_COND_STABLE
)) {
996 case FLUSH_COND_STABLE
:
997 if (nfs_reqs_to_commit(cinfo
))
1000 data
->args
.stable
= NFS_FILE_SYNC
;
1003 data
->res
.fattr
= &data
->fattr
;
1004 data
->res
.count
= count
;
1005 data
->res
.verf
= &data
->verf
;
1006 nfs_fattr_init(&data
->fattr
);
1009 static int nfs_do_write(struct nfs_write_data
*data
,
1010 const struct rpc_call_ops
*call_ops
,
1013 struct inode
*inode
= data
->header
->inode
;
1015 return nfs_initiate_write(NFS_CLIENT(inode
), data
, call_ops
, how
, 0);
1018 static int nfs_do_multiple_writes(struct list_head
*head
,
1019 const struct rpc_call_ops
*call_ops
,
1022 struct nfs_write_data
*data
;
1025 while (!list_empty(head
)) {
1028 data
= list_first_entry(head
, struct nfs_write_data
, list
);
1029 list_del_init(&data
->list
);
1031 ret2
= nfs_do_write(data
, call_ops
, how
);
1038 /* If a nfs_flush_* function fails, it should remove reqs from @head and
1039 * call this on each, which will prepare them to be retried on next
1040 * writeback using standard nfs.
1042 static void nfs_redirty_request(struct nfs_page
*req
)
1044 nfs_mark_request_dirty(req
);
1045 nfs_unlock_request(req
);
1046 nfs_end_page_writeback(req
->wb_page
);
1047 nfs_release_request(req
);
1050 static void nfs_async_write_error(struct list_head
*head
)
1052 struct nfs_page
*req
;
1054 while (!list_empty(head
)) {
1055 req
= nfs_list_entry(head
->next
);
1056 nfs_list_remove_request(req
);
1057 nfs_redirty_request(req
);
1061 static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops
= {
1062 .error_cleanup
= nfs_async_write_error
,
1063 .completion
= nfs_write_completion
,
1066 static void nfs_flush_error(struct nfs_pageio_descriptor
*desc
,
1067 struct nfs_pgio_header
*hdr
)
1069 set_bit(NFS_IOHDR_REDO
, &hdr
->flags
);
1070 while (!list_empty(&hdr
->rpc_list
)) {
1071 struct nfs_write_data
*data
= list_first_entry(&hdr
->rpc_list
,
1072 struct nfs_write_data
, list
);
1073 list_del(&data
->list
);
1074 nfs_writedata_release(data
);
1076 desc
->pg_completion_ops
->error_cleanup(&desc
->pg_list
);
1080 * Generate multiple small requests to write out a single
1081 * contiguous dirty area on one page.
1083 static int nfs_flush_multi(struct nfs_pageio_descriptor
*desc
,
1084 struct nfs_pgio_header
*hdr
)
1086 struct nfs_page
*req
= hdr
->req
;
1087 struct page
*page
= req
->wb_page
;
1088 struct nfs_write_data
*data
;
1089 size_t wsize
= desc
->pg_bsize
, nbytes
;
1090 unsigned int offset
;
1092 struct nfs_commit_info cinfo
;
1094 nfs_init_cinfo(&cinfo
, desc
->pg_inode
, desc
->pg_dreq
);
1096 if ((desc
->pg_ioflags
& FLUSH_COND_STABLE
) &&
1097 (desc
->pg_moreio
|| nfs_reqs_to_commit(&cinfo
) ||
1098 desc
->pg_count
> wsize
))
1099 desc
->pg_ioflags
&= ~FLUSH_COND_STABLE
;
1103 nbytes
= desc
->pg_count
;
1105 size_t len
= min(nbytes
, wsize
);
1107 data
= nfs_writedata_alloc(hdr
, 1);
1109 nfs_flush_error(desc
, hdr
);
1112 data
->pages
.pagevec
[0] = page
;
1113 nfs_write_rpcsetup(data
, len
, offset
, desc
->pg_ioflags
, &cinfo
);
1114 list_add(&data
->list
, &hdr
->rpc_list
);
1118 } while (nbytes
!= 0);
1119 nfs_list_remove_request(req
);
1120 nfs_list_add_request(req
, &hdr
->pages
);
1121 desc
->pg_rpc_callops
= &nfs_write_common_ops
;
1126 * Create an RPC task for the given write request and kick it.
1127 * The page must have been locked by the caller.
1129 * It may happen that the page we're passed is not marked dirty.
1130 * This is the case if nfs_updatepage detects a conflicting request
1131 * that has been written but not committed.
1133 static int nfs_flush_one(struct nfs_pageio_descriptor
*desc
,
1134 struct nfs_pgio_header
*hdr
)
1136 struct nfs_page
*req
;
1137 struct page
**pages
;
1138 struct nfs_write_data
*data
;
1139 struct list_head
*head
= &desc
->pg_list
;
1140 struct nfs_commit_info cinfo
;
1142 data
= nfs_writedata_alloc(hdr
, nfs_page_array_len(desc
->pg_base
,
1145 nfs_flush_error(desc
, hdr
);
1149 nfs_init_cinfo(&cinfo
, desc
->pg_inode
, desc
->pg_dreq
);
1150 pages
= data
->pages
.pagevec
;
1151 while (!list_empty(head
)) {
1152 req
= nfs_list_entry(head
->next
);
1153 nfs_list_remove_request(req
);
1154 nfs_list_add_request(req
, &hdr
->pages
);
1155 *pages
++ = req
->wb_page
;
1158 if ((desc
->pg_ioflags
& FLUSH_COND_STABLE
) &&
1159 (desc
->pg_moreio
|| nfs_reqs_to_commit(&cinfo
)))
1160 desc
->pg_ioflags
&= ~FLUSH_COND_STABLE
;
1162 /* Set up the argument struct */
1163 nfs_write_rpcsetup(data
, desc
->pg_count
, 0, desc
->pg_ioflags
, &cinfo
);
1164 list_add(&data
->list
, &hdr
->rpc_list
);
1165 desc
->pg_rpc_callops
= &nfs_write_common_ops
;
1169 int nfs_generic_flush(struct nfs_pageio_descriptor
*desc
,
1170 struct nfs_pgio_header
*hdr
)
1172 if (desc
->pg_bsize
< PAGE_CACHE_SIZE
)
1173 return nfs_flush_multi(desc
, hdr
);
1174 return nfs_flush_one(desc
, hdr
);
1177 static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor
*desc
)
1179 struct nfs_write_header
*whdr
;
1180 struct nfs_pgio_header
*hdr
;
1183 whdr
= nfs_writehdr_alloc();
1185 desc
->pg_completion_ops
->error_cleanup(&desc
->pg_list
);
1188 hdr
= &whdr
->header
;
1189 nfs_pgheader_init(desc
, hdr
, nfs_writehdr_free
);
1190 atomic_inc(&hdr
->refcnt
);
1191 ret
= nfs_generic_flush(desc
, hdr
);
1193 ret
= nfs_do_multiple_writes(&hdr
->rpc_list
,
1194 desc
->pg_rpc_callops
,
1196 if (atomic_dec_and_test(&hdr
->refcnt
))
1197 hdr
->completion_ops
->completion(hdr
);
1201 static const struct nfs_pageio_ops nfs_pageio_write_ops
= {
1202 .pg_test
= nfs_generic_pg_test
,
1203 .pg_doio
= nfs_generic_pg_writepages
,
1206 void nfs_pageio_init_write(struct nfs_pageio_descriptor
*pgio
,
1207 struct inode
*inode
, int ioflags
,
1208 const struct nfs_pgio_completion_ops
*compl_ops
)
1210 nfs_pageio_init(pgio
, inode
, &nfs_pageio_write_ops
, compl_ops
,
1211 NFS_SERVER(inode
)->wsize
, ioflags
);
1214 void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor
*pgio
)
1216 pgio
->pg_ops
= &nfs_pageio_write_ops
;
1217 pgio
->pg_bsize
= NFS_SERVER(pgio
->pg_inode
)->wsize
;
1219 EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds
);
1222 void nfs_write_prepare(struct rpc_task
*task
, void *calldata
)
1224 struct nfs_write_data
*data
= calldata
;
1225 NFS_PROTO(data
->header
->inode
)->write_rpc_prepare(task
, data
);
1228 void nfs_commit_prepare(struct rpc_task
*task
, void *calldata
)
1230 struct nfs_commit_data
*data
= calldata
;
1232 NFS_PROTO(data
->inode
)->commit_rpc_prepare(task
, data
);
1236 * Handle a write reply that flushes a whole page.
1238 * FIXME: There is an inherent race with invalidate_inode_pages and
1239 * writebacks since the page->count is kept > 1 for as long
1240 * as the page has a write request pending.
1242 static void nfs_writeback_done_common(struct rpc_task
*task
, void *calldata
)
1244 struct nfs_write_data
*data
= calldata
;
1246 nfs_writeback_done(task
, data
);
1249 static void nfs_writeback_release_common(void *calldata
)
1251 struct nfs_write_data
*data
= calldata
;
1252 struct nfs_pgio_header
*hdr
= data
->header
;
1253 int status
= data
->task
.tk_status
;
1255 if ((status
>= 0) && nfs_write_need_commit(data
)) {
1256 spin_lock(&hdr
->lock
);
1257 if (test_bit(NFS_IOHDR_NEED_RESCHED
, &hdr
->flags
))
1259 else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT
, &hdr
->flags
))
1260 memcpy(hdr
->verf
, &data
->verf
, sizeof(*hdr
->verf
));
1261 else if (memcmp(hdr
->verf
, &data
->verf
, sizeof(*hdr
->verf
)))
1262 set_bit(NFS_IOHDR_NEED_RESCHED
, &hdr
->flags
);
1263 spin_unlock(&hdr
->lock
);
1265 nfs_writedata_release(data
);
1268 static const struct rpc_call_ops nfs_write_common_ops
= {
1269 .rpc_call_prepare
= nfs_write_prepare
,
1270 .rpc_call_done
= nfs_writeback_done_common
,
1271 .rpc_release
= nfs_writeback_release_common
,
1276 * This function is called when the WRITE call is complete.
1278 void nfs_writeback_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
1280 struct nfs_writeargs
*argp
= &data
->args
;
1281 struct nfs_writeres
*resp
= &data
->res
;
1282 struct inode
*inode
= data
->header
->inode
;
1285 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1286 task
->tk_pid
, task
->tk_status
);
1289 * ->write_done will attempt to use post-op attributes to detect
1290 * conflicting writes by other clients. A strict interpretation
1291 * of close-to-open would allow us to continue caching even if
1292 * another writer had changed the file, but some applications
1293 * depend on tighter cache coherency when writing.
1295 status
= NFS_PROTO(inode
)->write_done(task
, data
);
1298 nfs_add_stats(inode
, NFSIOS_SERVERWRITTENBYTES
, resp
->count
);
1300 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1301 if (resp
->verf
->committed
< argp
->stable
&& task
->tk_status
>= 0) {
1302 /* We tried a write call, but the server did not
1303 * commit data to stable storage even though we
1305 * Note: There is a known bug in Tru64 < 5.0 in which
1306 * the server reports NFS_DATA_SYNC, but performs
1307 * NFS_FILE_SYNC. We therefore implement this checking
1308 * as a dprintk() in order to avoid filling syslog.
1310 static unsigned long complain
;
1312 /* Note this will print the MDS for a DS write */
1313 if (time_before(complain
, jiffies
)) {
1314 dprintk("NFS: faulty NFS server %s:"
1315 " (committed = %d) != (stable = %d)\n",
1316 NFS_SERVER(inode
)->nfs_client
->cl_hostname
,
1317 resp
->verf
->committed
, argp
->stable
);
1318 complain
= jiffies
+ 300 * HZ
;
1322 if (task
->tk_status
< 0)
1323 nfs_set_pgio_error(data
->header
, task
->tk_status
, argp
->offset
);
1324 else if (resp
->count
< argp
->count
) {
1325 static unsigned long complain
;
1327 /* This a short write! */
1328 nfs_inc_stats(inode
, NFSIOS_SHORTWRITE
);
1330 /* Has the server at least made some progress? */
1331 if (resp
->count
== 0) {
1332 if (time_before(complain
, jiffies
)) {
1334 "NFS: Server wrote zero bytes, expected %u.\n",
1336 complain
= jiffies
+ 300 * HZ
;
1338 nfs_set_pgio_error(data
->header
, -EIO
, argp
->offset
);
1339 task
->tk_status
= -EIO
;
1342 /* Was this an NFSv2 write or an NFSv3 stable write? */
1343 if (resp
->verf
->committed
!= NFS_UNSTABLE
) {
1344 /* Resend from where the server left off */
1345 data
->mds_offset
+= resp
->count
;
1346 argp
->offset
+= resp
->count
;
1347 argp
->pgbase
+= resp
->count
;
1348 argp
->count
-= resp
->count
;
1350 /* Resend as a stable write in order to avoid
1351 * headaches in the case of a server crash.
1353 argp
->stable
= NFS_FILE_SYNC
;
1355 rpc_restart_call_prepare(task
);
1360 #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1361 static int nfs_commit_set_lock(struct nfs_inode
*nfsi
, int may_wait
)
1365 if (!test_and_set_bit(NFS_INO_COMMIT
, &nfsi
->flags
))
1369 ret
= out_of_line_wait_on_bit_lock(&nfsi
->flags
,
1371 nfs_wait_bit_killable
,
1373 return (ret
< 0) ? ret
: 1;
1376 static void nfs_commit_clear_lock(struct nfs_inode
*nfsi
)
1378 clear_bit(NFS_INO_COMMIT
, &nfsi
->flags
);
1379 smp_mb__after_clear_bit();
1380 wake_up_bit(&nfsi
->flags
, NFS_INO_COMMIT
);
1383 void nfs_commitdata_release(struct nfs_commit_data
*data
)
1385 put_nfs_open_context(data
->context
);
1386 nfs_commit_free(data
);
1388 EXPORT_SYMBOL_GPL(nfs_commitdata_release
);
1390 int nfs_initiate_commit(struct rpc_clnt
*clnt
, struct nfs_commit_data
*data
,
1391 const struct rpc_call_ops
*call_ops
,
1394 struct rpc_task
*task
;
1395 int priority
= flush_task_priority(how
);
1396 struct rpc_message msg
= {
1397 .rpc_argp
= &data
->args
,
1398 .rpc_resp
= &data
->res
,
1399 .rpc_cred
= data
->cred
,
1401 struct rpc_task_setup task_setup_data
= {
1402 .task
= &data
->task
,
1404 .rpc_message
= &msg
,
1405 .callback_ops
= call_ops
,
1406 .callback_data
= data
,
1407 .workqueue
= nfsiod_workqueue
,
1408 .flags
= RPC_TASK_ASYNC
| flags
,
1409 .priority
= priority
,
1411 /* Set up the initial task struct. */
1412 NFS_PROTO(data
->inode
)->commit_setup(data
, &msg
);
1414 dprintk("NFS: %5u initiated commit call\n", data
->task
.tk_pid
);
1416 task
= rpc_run_task(&task_setup_data
);
1418 return PTR_ERR(task
);
1419 if (how
& FLUSH_SYNC
)
1420 rpc_wait_for_completion_task(task
);
1424 EXPORT_SYMBOL_GPL(nfs_initiate_commit
);
1427 * Set up the argument/result storage required for the RPC call.
1429 void nfs_init_commit(struct nfs_commit_data
*data
,
1430 struct list_head
*head
,
1431 struct pnfs_layout_segment
*lseg
,
1432 struct nfs_commit_info
*cinfo
)
1434 struct nfs_page
*first
= nfs_list_entry(head
->next
);
1435 struct inode
*inode
= first
->wb_context
->dentry
->d_inode
;
1437 /* Set up the RPC argument and reply structs
1438 * NB: take care not to mess about with data->commit et al. */
1440 list_splice_init(head
, &data
->pages
);
1442 data
->inode
= inode
;
1443 data
->cred
= first
->wb_context
->cred
;
1444 data
->lseg
= lseg
; /* reference transferred */
1445 data
->mds_ops
= &nfs_commit_ops
;
1446 data
->completion_ops
= cinfo
->completion_ops
;
1447 data
->dreq
= cinfo
->dreq
;
1449 data
->args
.fh
= NFS_FH(data
->inode
);
1450 /* Note: we always request a commit of the entire inode */
1451 data
->args
.offset
= 0;
1452 data
->args
.count
= 0;
1453 data
->context
= get_nfs_open_context(first
->wb_context
);
1454 data
->res
.fattr
= &data
->fattr
;
1455 data
->res
.verf
= &data
->verf
;
1456 nfs_fattr_init(&data
->fattr
);
1458 EXPORT_SYMBOL_GPL(nfs_init_commit
);
1460 void nfs_retry_commit(struct list_head
*page_list
,
1461 struct pnfs_layout_segment
*lseg
,
1462 struct nfs_commit_info
*cinfo
)
1464 struct nfs_page
*req
;
1466 while (!list_empty(page_list
)) {
1467 req
= nfs_list_entry(page_list
->next
);
1468 nfs_list_remove_request(req
);
1469 nfs_mark_request_commit(req
, lseg
, cinfo
);
1471 dec_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
1472 dec_bdi_stat(req
->wb_page
->mapping
->backing_dev_info
,
1475 nfs_unlock_and_release_request(req
);
1478 EXPORT_SYMBOL_GPL(nfs_retry_commit
);
1481 * Commit dirty pages
1484 nfs_commit_list(struct inode
*inode
, struct list_head
*head
, int how
,
1485 struct nfs_commit_info
*cinfo
)
1487 struct nfs_commit_data
*data
;
1489 data
= nfs_commitdata_alloc();
1494 /* Set up the argument struct */
1495 nfs_init_commit(data
, head
, NULL
, cinfo
);
1496 atomic_inc(&cinfo
->mds
->rpcs_out
);
1497 return nfs_initiate_commit(NFS_CLIENT(inode
), data
, data
->mds_ops
,
1500 nfs_retry_commit(head
, NULL
, cinfo
);
1501 cinfo
->completion_ops
->error_cleanup(NFS_I(inode
));
1506 * COMMIT call returned
1508 static void nfs_commit_done(struct rpc_task
*task
, void *calldata
)
1510 struct nfs_commit_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 static void nfs_commit_release_pages(struct nfs_commit_data
*data
)
1521 struct nfs_page
*req
;
1522 int status
= data
->task
.tk_status
;
1523 struct nfs_commit_info cinfo
;
1525 while (!list_empty(&data
->pages
)) {
1526 req
= nfs_list_entry(data
->pages
.next
);
1527 nfs_list_remove_request(req
);
1528 nfs_clear_page_commit(req
->wb_page
);
1530 dprintk("NFS: commit (%s/%lld %d@%lld)",
1531 req
->wb_context
->dentry
->d_sb
->s_id
,
1532 (long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1534 (long long)req_offset(req
));
1536 nfs_context_set_write_error(req
->wb_context
, status
);
1537 nfs_inode_remove_request(req
);
1538 dprintk(", error = %d\n", status
);
1542 /* Okay, COMMIT succeeded, apparently. Check the verifier
1543 * returned by the server against all stored verfs. */
1544 if (!memcmp(&req
->wb_verf
, &data
->verf
.verifier
, sizeof(req
->wb_verf
))) {
1545 /* We have a match */
1546 nfs_inode_remove_request(req
);
1550 /* We have a mismatch. Write the page again */
1551 dprintk(" mismatch\n");
1552 nfs_mark_request_dirty(req
);
1554 nfs_unlock_and_release_request(req
);
1556 nfs_init_cinfo(&cinfo
, data
->inode
, data
->dreq
);
1557 if (atomic_dec_and_test(&cinfo
.mds
->rpcs_out
))
1558 nfs_commit_clear_lock(NFS_I(data
->inode
));
1561 static void nfs_commit_release(void *calldata
)
1563 struct nfs_commit_data
*data
= calldata
;
1565 data
->completion_ops
->completion(data
);
1566 nfs_commitdata_release(calldata
);
1569 static const struct rpc_call_ops nfs_commit_ops
= {
1570 .rpc_call_prepare
= nfs_commit_prepare
,
1571 .rpc_call_done
= nfs_commit_done
,
1572 .rpc_release
= nfs_commit_release
,
1575 static const struct nfs_commit_completion_ops nfs_commit_completion_ops
= {
1576 .completion
= nfs_commit_release_pages
,
1577 .error_cleanup
= nfs_commit_clear_lock
,
1580 int nfs_generic_commit_list(struct inode
*inode
, struct list_head
*head
,
1581 int how
, struct nfs_commit_info
*cinfo
)
1585 status
= pnfs_commit_list(inode
, head
, how
, cinfo
);
1586 if (status
== PNFS_NOT_ATTEMPTED
)
1587 status
= nfs_commit_list(inode
, head
, how
, cinfo
);
1591 int nfs_commit_inode(struct inode
*inode
, int how
)
1594 struct nfs_commit_info cinfo
;
1595 int may_wait
= how
& FLUSH_SYNC
;
1598 res
= nfs_commit_set_lock(NFS_I(inode
), may_wait
);
1600 goto out_mark_dirty
;
1601 nfs_init_cinfo_from_inode(&cinfo
, inode
);
1602 res
= nfs_scan_commit(inode
, &head
, &cinfo
);
1606 error
= nfs_generic_commit_list(inode
, &head
, how
, &cinfo
);
1610 goto out_mark_dirty
;
1611 error
= wait_on_bit(&NFS_I(inode
)->flags
,
1613 nfs_wait_bit_killable
,
1618 nfs_commit_clear_lock(NFS_I(inode
));
1620 /* Note: If we exit without ensuring that the commit is complete,
1621 * we must mark the inode as dirty. Otherwise, future calls to
1622 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1623 * that the data is on the disk.
1626 __mark_inode_dirty(inode
, I_DIRTY_DATASYNC
);
1630 static int nfs_commit_unstable_pages(struct inode
*inode
, struct writeback_control
*wbc
)
1632 struct nfs_inode
*nfsi
= NFS_I(inode
);
1633 int flags
= FLUSH_SYNC
;
1636 /* no commits means nothing needs to be done */
1637 if (!nfsi
->commit_info
.ncommit
)
1640 if (wbc
->sync_mode
== WB_SYNC_NONE
) {
1641 /* Don't commit yet if this is a non-blocking flush and there
1642 * are a lot of outstanding writes for this mapping.
1644 if (nfsi
->commit_info
.ncommit
<= (nfsi
->npages
>> 1))
1645 goto out_mark_dirty
;
1647 /* don't wait for the COMMIT response */
1651 ret
= nfs_commit_inode(inode
, flags
);
1653 if (wbc
->sync_mode
== WB_SYNC_NONE
) {
1654 if (ret
< wbc
->nr_to_write
)
1655 wbc
->nr_to_write
-= ret
;
1657 wbc
->nr_to_write
= 0;
1662 __mark_inode_dirty(inode
, I_DIRTY_DATASYNC
);
1666 static int nfs_commit_unstable_pages(struct inode
*inode
, struct writeback_control
*wbc
)
1672 int nfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1674 return nfs_commit_unstable_pages(inode
, wbc
);
1677 #ifdef CONFIG_NFS_V4
1678 int nfs4_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1680 int ret
= nfs_write_inode(inode
, wbc
);
1682 if (ret
>= 0 && test_bit(NFS_INO_LAYOUTCOMMIT
, &NFS_I(inode
)->flags
)) {
1686 if (wbc
->sync_mode
== WB_SYNC_NONE
)
1689 status
= pnfs_layoutcommit_inode(inode
, sync
);
1698 * flush the inode to disk.
1700 int nfs_wb_all(struct inode
*inode
)
1702 struct writeback_control wbc
= {
1703 .sync_mode
= WB_SYNC_ALL
,
1704 .nr_to_write
= LONG_MAX
,
1706 .range_end
= LLONG_MAX
,
1709 return sync_inode(inode
, &wbc
);
1712 int nfs_wb_page_cancel(struct inode
*inode
, struct page
*page
)
1714 struct nfs_page
*req
;
1717 BUG_ON(!PageLocked(page
));
1719 wait_on_page_writeback(page
);
1720 req
= nfs_page_find_request(page
);
1723 if (nfs_lock_request(req
)) {
1724 nfs_clear_request_commit(req
);
1725 nfs_inode_remove_request(req
);
1727 * In case nfs_inode_remove_request has marked the
1728 * page as being dirty
1730 cancel_dirty_page(page
, PAGE_CACHE_SIZE
);
1731 nfs_unlock_and_release_request(req
);
1734 ret
= nfs_wait_on_request(req
);
1735 nfs_release_request(req
);
1743 * Write back all requests on one page - we do this before reading it.
1745 int nfs_wb_page(struct inode
*inode
, struct page
*page
)
1747 loff_t range_start
= page_offset(page
);
1748 loff_t range_end
= range_start
+ (loff_t
)(PAGE_CACHE_SIZE
- 1);
1749 struct writeback_control wbc
= {
1750 .sync_mode
= WB_SYNC_ALL
,
1752 .range_start
= range_start
,
1753 .range_end
= range_end
,
1758 wait_on_page_writeback(page
);
1759 if (clear_page_dirty_for_io(page
)) {
1760 ret
= nfs_writepage_locked(page
, &wbc
);
1765 if (!PagePrivate(page
))
1767 ret
= nfs_commit_inode(inode
, FLUSH_SYNC
);
1776 #ifdef CONFIG_MIGRATION
1777 int nfs_migrate_page(struct address_space
*mapping
, struct page
*newpage
,
1778 struct page
*page
, enum migrate_mode mode
)
1781 * If PagePrivate is set, then the page is currently associated with
1782 * an in-progress read or write request. Don't try to migrate it.
1784 * FIXME: we could do this in principle, but we'll need a way to ensure
1785 * that we can safely release the inode reference while holding
1788 if (PagePrivate(page
))
1791 nfs_fscache_release_page(page
, GFP_KERNEL
);
1793 return migrate_page(mapping
, newpage
, page
, mode
);
1797 int __init
nfs_init_writepagecache(void)
1799 nfs_wdata_cachep
= kmem_cache_create("nfs_write_data",
1800 sizeof(struct nfs_write_header
),
1801 0, SLAB_HWCACHE_ALIGN
,
1803 if (nfs_wdata_cachep
== NULL
)
1806 nfs_wdata_mempool
= mempool_create_slab_pool(MIN_POOL_WRITE
,
1808 if (nfs_wdata_mempool
== NULL
)
1811 nfs_cdata_cachep
= kmem_cache_create("nfs_commit_data",
1812 sizeof(struct nfs_commit_data
),
1813 0, SLAB_HWCACHE_ALIGN
,
1815 if (nfs_cdata_cachep
== NULL
)
1818 nfs_commit_mempool
= mempool_create_slab_pool(MIN_POOL_COMMIT
,
1820 if (nfs_commit_mempool
== NULL
)
1824 * NFS congestion size, scale with available memory.
1836 * This allows larger machines to have larger/more transfers.
1837 * Limit the default to 256M
1839 nfs_congestion_kb
= (16*int_sqrt(totalram_pages
)) << (PAGE_SHIFT
-10);
1840 if (nfs_congestion_kb
> 256*1024)
1841 nfs_congestion_kb
= 256*1024;
1846 void nfs_destroy_writepagecache(void)
1848 mempool_destroy(nfs_commit_mempool
);
1849 mempool_destroy(nfs_wdata_mempool
);
1850 kmem_cache_destroy(nfs_wdata_cachep
);