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"
36 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
38 #define MIN_POOL_WRITE (32)
39 #define MIN_POOL_COMMIT (4)
42 * Local function declarations
44 static void nfs_redirty_request(struct nfs_page
*req
);
45 static const struct rpc_call_ops nfs_write_common_ops
;
46 static const struct rpc_call_ops nfs_commit_ops
;
47 static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops
;
48 static const struct nfs_commit_completion_ops nfs_commit_completion_ops
;
50 static struct kmem_cache
*nfs_wdata_cachep
;
51 static mempool_t
*nfs_wdata_mempool
;
52 static struct kmem_cache
*nfs_cdata_cachep
;
53 static mempool_t
*nfs_commit_mempool
;
55 struct nfs_commit_data
*nfs_commitdata_alloc(void)
57 struct nfs_commit_data
*p
= mempool_alloc(nfs_commit_mempool
, GFP_NOIO
);
60 memset(p
, 0, sizeof(*p
));
61 INIT_LIST_HEAD(&p
->pages
);
65 EXPORT_SYMBOL_GPL(nfs_commitdata_alloc
);
67 void nfs_commit_free(struct nfs_commit_data
*p
)
69 mempool_free(p
, nfs_commit_mempool
);
71 EXPORT_SYMBOL_GPL(nfs_commit_free
);
73 struct nfs_rw_header
*nfs_writehdr_alloc(void)
75 struct nfs_rw_header
*p
= mempool_alloc(nfs_wdata_mempool
, GFP_NOIO
);
78 struct nfs_pgio_header
*hdr
= &p
->header
;
80 memset(p
, 0, sizeof(*p
));
81 INIT_LIST_HEAD(&hdr
->pages
);
82 INIT_LIST_HEAD(&hdr
->rpc_list
);
83 spin_lock_init(&hdr
->lock
);
84 atomic_set(&hdr
->refcnt
, 0);
88 EXPORT_SYMBOL_GPL(nfs_writehdr_alloc
);
90 void nfs_writehdr_free(struct nfs_pgio_header
*hdr
)
92 struct nfs_rw_header
*whdr
= container_of(hdr
, struct nfs_rw_header
, header
);
93 mempool_free(whdr
, nfs_wdata_mempool
);
95 EXPORT_SYMBOL_GPL(nfs_writehdr_free
);
97 static void nfs_context_set_write_error(struct nfs_open_context
*ctx
, int error
)
101 set_bit(NFS_CONTEXT_ERROR_WRITE
, &ctx
->flags
);
104 static struct nfs_page
*
105 nfs_page_find_request_locked(struct nfs_inode
*nfsi
, struct page
*page
)
107 struct nfs_page
*req
= NULL
;
109 if (PagePrivate(page
))
110 req
= (struct nfs_page
*)page_private(page
);
111 else if (unlikely(PageSwapCache(page
))) {
112 struct nfs_page
*freq
, *t
;
114 /* Linearly search the commit list for the correct req */
115 list_for_each_entry_safe(freq
, t
, &nfsi
->commit_info
.list
, wb_list
) {
116 if (freq
->wb_page
== page
) {
124 kref_get(&req
->wb_kref
);
129 static struct nfs_page
*nfs_page_find_request(struct page
*page
)
131 struct inode
*inode
= page_file_mapping(page
)->host
;
132 struct nfs_page
*req
= NULL
;
134 spin_lock(&inode
->i_lock
);
135 req
= nfs_page_find_request_locked(NFS_I(inode
), page
);
136 spin_unlock(&inode
->i_lock
);
140 /* Adjust the file length if we're writing beyond the end */
141 static void nfs_grow_file(struct page
*page
, unsigned int offset
, unsigned int count
)
143 struct inode
*inode
= page_file_mapping(page
)->host
;
147 spin_lock(&inode
->i_lock
);
148 i_size
= i_size_read(inode
);
149 end_index
= (i_size
- 1) >> PAGE_CACHE_SHIFT
;
150 if (i_size
> 0 && page_file_index(page
) < end_index
)
152 end
= page_file_offset(page
) + ((loff_t
)offset
+count
);
155 i_size_write(inode
, end
);
156 nfs_inc_stats(inode
, NFSIOS_EXTENDWRITE
);
158 spin_unlock(&inode
->i_lock
);
161 /* A writeback failed: mark the page as bad, and invalidate the page cache */
162 static void nfs_set_pageerror(struct page
*page
)
164 nfs_zap_mapping(page_file_mapping(page
)->host
, page_file_mapping(page
));
167 /* We can set the PG_uptodate flag if we see that a write request
168 * covers the full page.
170 static void nfs_mark_uptodate(struct page
*page
, unsigned int base
, unsigned int count
)
172 if (PageUptodate(page
))
176 if (count
!= nfs_page_length(page
))
178 SetPageUptodate(page
);
181 static int wb_priority(struct writeback_control
*wbc
)
183 if (wbc
->for_reclaim
)
184 return FLUSH_HIGHPRI
| FLUSH_STABLE
;
185 if (wbc
->for_kupdate
|| wbc
->for_background
)
186 return FLUSH_LOWPRI
| FLUSH_COND_STABLE
;
187 return FLUSH_COND_STABLE
;
191 * NFS congestion control
194 int nfs_congestion_kb
;
196 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
197 #define NFS_CONGESTION_OFF_THRESH \
198 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
200 static void nfs_set_page_writeback(struct page
*page
)
202 struct nfs_server
*nfss
= NFS_SERVER(page_file_mapping(page
)->host
);
203 int ret
= test_set_page_writeback(page
);
205 WARN_ON_ONCE(ret
!= 0);
207 if (atomic_long_inc_return(&nfss
->writeback
) >
208 NFS_CONGESTION_ON_THRESH
) {
209 set_bdi_congested(&nfss
->backing_dev_info
,
214 static void nfs_end_page_writeback(struct page
*page
)
216 struct inode
*inode
= page_file_mapping(page
)->host
;
217 struct nfs_server
*nfss
= NFS_SERVER(inode
);
219 end_page_writeback(page
);
220 if (atomic_long_dec_return(&nfss
->writeback
) < NFS_CONGESTION_OFF_THRESH
)
221 clear_bdi_congested(&nfss
->backing_dev_info
, BLK_RW_ASYNC
);
224 static struct nfs_page
*nfs_find_and_lock_request(struct page
*page
, bool nonblock
)
226 struct inode
*inode
= page_file_mapping(page
)->host
;
227 struct nfs_page
*req
;
230 spin_lock(&inode
->i_lock
);
232 req
= nfs_page_find_request_locked(NFS_I(inode
), page
);
235 if (nfs_lock_request(req
))
237 /* Note: If we hold the page lock, as is the case in nfs_writepage,
238 * then the call to nfs_lock_request() will always
239 * succeed provided that someone hasn't already marked the
240 * request as dirty (in which case we don't care).
242 spin_unlock(&inode
->i_lock
);
244 ret
= nfs_wait_on_request(req
);
247 nfs_release_request(req
);
250 spin_lock(&inode
->i_lock
);
252 spin_unlock(&inode
->i_lock
);
257 * Find an associated nfs write request, and prepare to flush it out
258 * May return an error if the user signalled nfs_wait_on_request().
260 static int nfs_page_async_flush(struct nfs_pageio_descriptor
*pgio
,
261 struct page
*page
, bool nonblock
)
263 struct nfs_page
*req
;
266 req
= nfs_find_and_lock_request(page
, nonblock
);
273 nfs_set_page_writeback(page
);
274 WARN_ON_ONCE(test_bit(PG_CLEAN
, &req
->wb_flags
));
277 if (!nfs_pageio_add_request(pgio
, req
)) {
278 nfs_redirty_request(req
);
279 ret
= pgio
->pg_error
;
285 static int nfs_do_writepage(struct page
*page
, struct writeback_control
*wbc
, struct nfs_pageio_descriptor
*pgio
)
287 struct inode
*inode
= page_file_mapping(page
)->host
;
290 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGE
);
291 nfs_add_stats(inode
, NFSIOS_WRITEPAGES
, 1);
293 nfs_pageio_cond_complete(pgio
, page_file_index(page
));
294 ret
= nfs_page_async_flush(pgio
, page
, wbc
->sync_mode
== WB_SYNC_NONE
);
295 if (ret
== -EAGAIN
) {
296 redirty_page_for_writepage(wbc
, page
);
303 * Write an mmapped page to the server.
305 static int nfs_writepage_locked(struct page
*page
, struct writeback_control
*wbc
)
307 struct nfs_pageio_descriptor pgio
;
310 nfs_pageio_init_write(&pgio
, page
->mapping
->host
, wb_priority(wbc
),
311 false, &nfs_async_write_completion_ops
);
312 err
= nfs_do_writepage(page
, wbc
, &pgio
);
313 nfs_pageio_complete(&pgio
);
316 if (pgio
.pg_error
< 0)
317 return pgio
.pg_error
;
321 int nfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
325 ret
= nfs_writepage_locked(page
, wbc
);
330 static int nfs_writepages_callback(struct page
*page
, struct writeback_control
*wbc
, void *data
)
334 ret
= nfs_do_writepage(page
, wbc
, data
);
339 int nfs_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
341 struct inode
*inode
= mapping
->host
;
342 unsigned long *bitlock
= &NFS_I(inode
)->flags
;
343 struct nfs_pageio_descriptor pgio
;
346 /* Stop dirtying of new pages while we sync */
347 err
= wait_on_bit_lock(bitlock
, NFS_INO_FLUSHING
,
348 nfs_wait_bit_killable
, TASK_KILLABLE
);
352 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGES
);
354 nfs_pageio_init_write(&pgio
, inode
, wb_priority(wbc
), false,
355 &nfs_async_write_completion_ops
);
356 err
= write_cache_pages(mapping
, wbc
, nfs_writepages_callback
, &pgio
);
357 nfs_pageio_complete(&pgio
);
359 clear_bit_unlock(NFS_INO_FLUSHING
, bitlock
);
360 smp_mb__after_clear_bit();
361 wake_up_bit(bitlock
, NFS_INO_FLUSHING
);
374 * Insert a write request into an inode
376 static void nfs_inode_add_request(struct inode
*inode
, struct nfs_page
*req
)
378 struct nfs_inode
*nfsi
= NFS_I(inode
);
380 /* Lock the request! */
381 nfs_lock_request(req
);
383 spin_lock(&inode
->i_lock
);
384 if (!nfsi
->npages
&& NFS_PROTO(inode
)->have_delegation(inode
, FMODE_WRITE
))
387 * Swap-space should not get truncated. Hence no need to plug the race
388 * with invalidate/truncate.
390 if (likely(!PageSwapCache(req
->wb_page
))) {
391 set_bit(PG_MAPPED
, &req
->wb_flags
);
392 SetPagePrivate(req
->wb_page
);
393 set_page_private(req
->wb_page
, (unsigned long)req
);
396 kref_get(&req
->wb_kref
);
397 spin_unlock(&inode
->i_lock
);
401 * Remove a write request from an inode
403 static void nfs_inode_remove_request(struct nfs_page
*req
)
405 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
406 struct nfs_inode
*nfsi
= NFS_I(inode
);
408 spin_lock(&inode
->i_lock
);
409 if (likely(!PageSwapCache(req
->wb_page
))) {
410 set_page_private(req
->wb_page
, 0);
411 ClearPagePrivate(req
->wb_page
);
412 clear_bit(PG_MAPPED
, &req
->wb_flags
);
415 spin_unlock(&inode
->i_lock
);
416 nfs_release_request(req
);
420 nfs_mark_request_dirty(struct nfs_page
*req
)
422 __set_page_dirty_nobuffers(req
->wb_page
);
425 #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
427 * nfs_request_add_commit_list - add request to a commit list
428 * @req: pointer to a struct nfs_page
429 * @dst: commit list head
430 * @cinfo: holds list lock and accounting info
432 * This sets the PG_CLEAN bit, updates the cinfo count of
433 * number of outstanding requests requiring a commit as well as
436 * The caller must _not_ hold the cinfo->lock, but must be
437 * holding the nfs_page lock.
440 nfs_request_add_commit_list(struct nfs_page
*req
, struct list_head
*dst
,
441 struct nfs_commit_info
*cinfo
)
443 set_bit(PG_CLEAN
, &(req
)->wb_flags
);
444 spin_lock(cinfo
->lock
);
445 nfs_list_add_request(req
, dst
);
446 cinfo
->mds
->ncommit
++;
447 spin_unlock(cinfo
->lock
);
449 inc_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
450 inc_bdi_stat(page_file_mapping(req
->wb_page
)->backing_dev_info
,
452 __mark_inode_dirty(req
->wb_context
->dentry
->d_inode
,
456 EXPORT_SYMBOL_GPL(nfs_request_add_commit_list
);
459 * nfs_request_remove_commit_list - Remove request from a commit list
460 * @req: pointer to a nfs_page
461 * @cinfo: holds list lock and accounting info
463 * This clears the PG_CLEAN bit, and updates the cinfo's count of
464 * number of outstanding requests requiring a commit
465 * It does not update the MM page stats.
467 * The caller _must_ hold the cinfo->lock and the nfs_page lock.
470 nfs_request_remove_commit_list(struct nfs_page
*req
,
471 struct nfs_commit_info
*cinfo
)
473 if (!test_and_clear_bit(PG_CLEAN
, &(req
)->wb_flags
))
475 nfs_list_remove_request(req
);
476 cinfo
->mds
->ncommit
--;
478 EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list
);
480 static void nfs_init_cinfo_from_inode(struct nfs_commit_info
*cinfo
,
483 cinfo
->lock
= &inode
->i_lock
;
484 cinfo
->mds
= &NFS_I(inode
)->commit_info
;
485 cinfo
->ds
= pnfs_get_ds_info(inode
);
487 cinfo
->completion_ops
= &nfs_commit_completion_ops
;
490 void nfs_init_cinfo(struct nfs_commit_info
*cinfo
,
492 struct nfs_direct_req
*dreq
)
495 nfs_init_cinfo_from_dreq(cinfo
, dreq
);
497 nfs_init_cinfo_from_inode(cinfo
, inode
);
499 EXPORT_SYMBOL_GPL(nfs_init_cinfo
);
502 * Add a request to the inode's commit list.
505 nfs_mark_request_commit(struct nfs_page
*req
, struct pnfs_layout_segment
*lseg
,
506 struct nfs_commit_info
*cinfo
)
508 if (pnfs_mark_request_commit(req
, lseg
, cinfo
))
510 nfs_request_add_commit_list(req
, &cinfo
->mds
->list
, cinfo
);
514 nfs_clear_page_commit(struct page
*page
)
516 dec_zone_page_state(page
, NR_UNSTABLE_NFS
);
517 dec_bdi_stat(page_file_mapping(page
)->backing_dev_info
, BDI_RECLAIMABLE
);
521 nfs_clear_request_commit(struct nfs_page
*req
)
523 if (test_bit(PG_CLEAN
, &req
->wb_flags
)) {
524 struct inode
*inode
= req
->wb_context
->dentry
->d_inode
;
525 struct nfs_commit_info cinfo
;
527 nfs_init_cinfo_from_inode(&cinfo
, inode
);
528 if (!pnfs_clear_request_commit(req
, &cinfo
)) {
529 spin_lock(cinfo
.lock
);
530 nfs_request_remove_commit_list(req
, &cinfo
);
531 spin_unlock(cinfo
.lock
);
533 nfs_clear_page_commit(req
->wb_page
);
538 int nfs_write_need_commit(struct nfs_pgio_data
*data
)
540 if (data
->verf
.committed
== NFS_DATA_SYNC
)
541 return data
->header
->lseg
== NULL
;
542 return data
->verf
.committed
!= NFS_FILE_SYNC
;
546 static void nfs_init_cinfo_from_inode(struct nfs_commit_info
*cinfo
,
551 void nfs_init_cinfo(struct nfs_commit_info
*cinfo
,
553 struct nfs_direct_req
*dreq
)
558 nfs_mark_request_commit(struct nfs_page
*req
, struct pnfs_layout_segment
*lseg
,
559 struct nfs_commit_info
*cinfo
)
564 nfs_clear_request_commit(struct nfs_page
*req
)
569 int nfs_write_need_commit(struct nfs_pgio_data
*data
)
576 static void nfs_write_completion(struct nfs_pgio_header
*hdr
)
578 struct nfs_commit_info cinfo
;
579 unsigned long bytes
= 0;
581 if (test_bit(NFS_IOHDR_REDO
, &hdr
->flags
))
583 nfs_init_cinfo_from_inode(&cinfo
, hdr
->inode
);
584 while (!list_empty(&hdr
->pages
)) {
585 struct nfs_page
*req
= nfs_list_entry(hdr
->pages
.next
);
587 bytes
+= req
->wb_bytes
;
588 nfs_list_remove_request(req
);
589 if (test_bit(NFS_IOHDR_ERROR
, &hdr
->flags
) &&
590 (hdr
->good_bytes
< bytes
)) {
591 nfs_set_pageerror(req
->wb_page
);
592 nfs_context_set_write_error(req
->wb_context
, hdr
->error
);
595 if (test_bit(NFS_IOHDR_NEED_RESCHED
, &hdr
->flags
)) {
596 nfs_mark_request_dirty(req
);
599 if (test_bit(NFS_IOHDR_NEED_COMMIT
, &hdr
->flags
)) {
600 memcpy(&req
->wb_verf
, &hdr
->verf
.verifier
, sizeof(req
->wb_verf
));
601 nfs_mark_request_commit(req
, hdr
->lseg
, &cinfo
);
605 nfs_inode_remove_request(req
);
607 nfs_unlock_request(req
);
608 nfs_end_page_writeback(req
->wb_page
);
609 nfs_release_request(req
);
615 #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
617 nfs_reqs_to_commit(struct nfs_commit_info
*cinfo
)
619 return cinfo
->mds
->ncommit
;
622 /* cinfo->lock held by caller */
624 nfs_scan_commit_list(struct list_head
*src
, struct list_head
*dst
,
625 struct nfs_commit_info
*cinfo
, int max
)
627 struct nfs_page
*req
, *tmp
;
630 list_for_each_entry_safe(req
, tmp
, src
, wb_list
) {
631 if (!nfs_lock_request(req
))
633 kref_get(&req
->wb_kref
);
634 if (cond_resched_lock(cinfo
->lock
))
635 list_safe_reset_next(req
, tmp
, wb_list
);
636 nfs_request_remove_commit_list(req
, cinfo
);
637 nfs_list_add_request(req
, dst
);
639 if ((ret
== max
) && !cinfo
->dreq
)
646 * nfs_scan_commit - Scan an inode for commit requests
647 * @inode: NFS inode to scan
648 * @dst: mds destination list
649 * @cinfo: mds and ds lists of reqs ready to commit
651 * Moves requests from the inode's 'commit' request list.
652 * The requests are *not* checked to ensure that they form a contiguous set.
655 nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
,
656 struct nfs_commit_info
*cinfo
)
660 spin_lock(cinfo
->lock
);
661 if (cinfo
->mds
->ncommit
> 0) {
662 const int max
= INT_MAX
;
664 ret
= nfs_scan_commit_list(&cinfo
->mds
->list
, dst
,
666 ret
+= pnfs_scan_commit_lists(inode
, cinfo
, max
- ret
);
668 spin_unlock(cinfo
->lock
);
673 static unsigned long nfs_reqs_to_commit(struct nfs_commit_info
*cinfo
)
678 int nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
,
679 struct nfs_commit_info
*cinfo
)
686 * Search for an existing write request, and attempt to update
687 * it to reflect a new dirty region on a given page.
689 * If the attempt fails, then the existing request is flushed out
692 static struct nfs_page
*nfs_try_to_update_request(struct inode
*inode
,
697 struct nfs_page
*req
;
702 if (!PagePrivate(page
))
705 end
= offset
+ bytes
;
706 spin_lock(&inode
->i_lock
);
709 req
= nfs_page_find_request_locked(NFS_I(inode
), page
);
713 rqend
= req
->wb_offset
+ req
->wb_bytes
;
715 * Tell the caller to flush out the request if
716 * the offsets are non-contiguous.
717 * Note: nfs_flush_incompatible() will already
718 * have flushed out requests having wrong owners.
721 || end
< req
->wb_offset
)
724 if (nfs_lock_request(req
))
727 /* The request is locked, so wait and then retry */
728 spin_unlock(&inode
->i_lock
);
729 error
= nfs_wait_on_request(req
);
730 nfs_release_request(req
);
733 spin_lock(&inode
->i_lock
);
736 /* Okay, the request matches. Update the region */
737 if (offset
< req
->wb_offset
) {
738 req
->wb_offset
= offset
;
739 req
->wb_pgbase
= offset
;
742 req
->wb_bytes
= end
- req
->wb_offset
;
744 req
->wb_bytes
= rqend
- req
->wb_offset
;
746 spin_unlock(&inode
->i_lock
);
748 nfs_clear_request_commit(req
);
751 spin_unlock(&inode
->i_lock
);
752 nfs_release_request(req
);
753 error
= nfs_wb_page(inode
, page
);
755 return ERR_PTR(error
);
759 * Try to update an existing write request, or create one if there is none.
761 * Note: Should always be called with the Page Lock held to prevent races
762 * if we have to add a new request. Also assumes that the caller has
763 * already called nfs_flush_incompatible() if necessary.
765 static struct nfs_page
* nfs_setup_write_request(struct nfs_open_context
* ctx
,
766 struct page
*page
, unsigned int offset
, unsigned int bytes
)
768 struct inode
*inode
= page_file_mapping(page
)->host
;
769 struct nfs_page
*req
;
771 req
= nfs_try_to_update_request(inode
, page
, offset
, bytes
);
774 req
= nfs_create_request(ctx
, inode
, page
, offset
, bytes
);
777 nfs_inode_add_request(inode
, req
);
782 static int nfs_writepage_setup(struct nfs_open_context
*ctx
, struct page
*page
,
783 unsigned int offset
, unsigned int count
)
785 struct nfs_page
*req
;
787 req
= nfs_setup_write_request(ctx
, page
, offset
, count
);
790 /* Update file length */
791 nfs_grow_file(page
, offset
, count
);
792 nfs_mark_uptodate(page
, req
->wb_pgbase
, req
->wb_bytes
);
793 nfs_mark_request_dirty(req
);
794 nfs_unlock_and_release_request(req
);
798 int nfs_flush_incompatible(struct file
*file
, struct page
*page
)
800 struct nfs_open_context
*ctx
= nfs_file_open_context(file
);
801 struct nfs_lock_context
*l_ctx
;
802 struct nfs_page
*req
;
803 int do_flush
, status
;
805 * Look for a request corresponding to this page. If there
806 * is one, and it belongs to another file, we flush it out
807 * before we try to copy anything into the page. Do this
808 * due to the lack of an ACCESS-type call in NFSv2.
809 * Also do the same if we find a request from an existing
813 req
= nfs_page_find_request(page
);
816 l_ctx
= req
->wb_lock_context
;
817 do_flush
= req
->wb_page
!= page
|| req
->wb_context
!= ctx
;
818 if (l_ctx
&& ctx
->dentry
->d_inode
->i_flock
!= NULL
) {
819 do_flush
|= l_ctx
->lockowner
.l_owner
!= current
->files
820 || l_ctx
->lockowner
.l_pid
!= current
->tgid
;
822 nfs_release_request(req
);
825 status
= nfs_wb_page(page_file_mapping(page
)->host
, page
);
826 } while (status
== 0);
831 * Avoid buffered writes when a open context credential's key would
834 * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
836 * Return 0 and set a credential flag which triggers the inode to flush
837 * and performs NFS_FILE_SYNC writes if the key will expired within
838 * RPC_KEY_EXPIRE_TIMEO.
841 nfs_key_timeout_notify(struct file
*filp
, struct inode
*inode
)
843 struct nfs_open_context
*ctx
= nfs_file_open_context(filp
);
844 struct rpc_auth
*auth
= NFS_SERVER(inode
)->client
->cl_auth
;
846 return rpcauth_key_timeout_notify(auth
, ctx
->cred
);
850 * Test if the open context credential key is marked to expire soon.
852 bool nfs_ctx_key_to_expire(struct nfs_open_context
*ctx
)
854 return rpcauth_cred_key_to_expire(ctx
->cred
);
858 * If the page cache is marked as unsafe or invalid, then we can't rely on
859 * the PageUptodate() flag. In this case, we will need to turn off
860 * write optimisations that depend on the page contents being correct.
862 static bool nfs_write_pageuptodate(struct page
*page
, struct inode
*inode
)
864 struct nfs_inode
*nfsi
= NFS_I(inode
);
866 if (nfs_have_delegated_attributes(inode
))
868 if (nfsi
->cache_validity
& (NFS_INO_INVALID_DATA
|NFS_INO_REVAL_PAGECACHE
))
871 if (test_bit(NFS_INO_INVALIDATING
, &nfsi
->flags
))
874 return PageUptodate(page
) != 0;
877 /* If we know the page is up to date, and we're not using byte range locks (or
878 * if we have the whole file locked for writing), it may be more efficient to
879 * extend the write to cover the entire page in order to avoid fragmentation
882 * If the file is opened for synchronous writes then we can just skip the rest
885 static int nfs_can_extend_write(struct file
*file
, struct page
*page
, struct inode
*inode
)
887 if (file
->f_flags
& O_DSYNC
)
889 if (!nfs_write_pageuptodate(page
, inode
))
891 if (NFS_PROTO(inode
)->have_delegation(inode
, FMODE_WRITE
))
893 if (inode
->i_flock
== NULL
|| (inode
->i_flock
->fl_start
== 0 &&
894 inode
->i_flock
->fl_end
== OFFSET_MAX
&&
895 inode
->i_flock
->fl_type
!= F_RDLCK
))
901 * Update and possibly write a cached page of an NFS file.
903 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
904 * things with a page scheduled for an RPC call (e.g. invalidate it).
906 int nfs_updatepage(struct file
*file
, struct page
*page
,
907 unsigned int offset
, unsigned int count
)
909 struct nfs_open_context
*ctx
= nfs_file_open_context(file
);
910 struct inode
*inode
= page_file_mapping(page
)->host
;
913 nfs_inc_stats(inode
, NFSIOS_VFSUPDATEPAGE
);
915 dprintk("NFS: nfs_updatepage(%pD2 %d@%lld)\n",
916 file
, count
, (long long)(page_file_offset(page
) + offset
));
918 if (nfs_can_extend_write(file
, page
, inode
)) {
919 count
= max(count
+ offset
, nfs_page_length(page
));
923 status
= nfs_writepage_setup(ctx
, page
, offset
, count
);
925 nfs_set_pageerror(page
);
927 __set_page_dirty_nobuffers(page
);
929 dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
930 status
, (long long)i_size_read(inode
));
934 static int flush_task_priority(int how
)
936 switch (how
& (FLUSH_HIGHPRI
|FLUSH_LOWPRI
)) {
938 return RPC_PRIORITY_HIGH
;
940 return RPC_PRIORITY_LOW
;
942 return RPC_PRIORITY_NORMAL
;
945 int nfs_initiate_write(struct rpc_clnt
*clnt
,
946 struct nfs_pgio_data
*data
,
947 const struct rpc_call_ops
*call_ops
,
950 struct inode
*inode
= data
->header
->inode
;
951 int priority
= flush_task_priority(how
);
952 struct rpc_task
*task
;
953 struct rpc_message msg
= {
954 .rpc_argp
= &data
->args
,
955 .rpc_resp
= &data
->res
,
956 .rpc_cred
= data
->header
->cred
,
958 struct rpc_task_setup task_setup_data
= {
962 .callback_ops
= call_ops
,
963 .callback_data
= data
,
964 .workqueue
= nfsiod_workqueue
,
965 .flags
= RPC_TASK_ASYNC
| flags
,
966 .priority
= priority
,
970 /* Set up the initial task struct. */
971 NFS_PROTO(inode
)->write_setup(data
, &msg
);
973 dprintk("NFS: %5u initiated write call "
974 "(req %s/%llu, %u bytes @ offset %llu)\n",
977 (unsigned long long)NFS_FILEID(inode
),
979 (unsigned long long)data
->args
.offset
);
981 nfs4_state_protect_write(NFS_SERVER(inode
)->nfs_client
,
982 &task_setup_data
.rpc_client
, &msg
, data
);
984 task
= rpc_run_task(&task_setup_data
);
989 if (how
& FLUSH_SYNC
) {
990 ret
= rpc_wait_for_completion_task(task
);
992 ret
= task
->tk_status
;
998 EXPORT_SYMBOL_GPL(nfs_initiate_write
);
1001 * Set up the argument/result storage required for the RPC call.
1003 static void nfs_write_rpcsetup(struct nfs_pgio_data
*data
,
1004 unsigned int count
, unsigned int offset
,
1005 int how
, struct nfs_commit_info
*cinfo
)
1007 struct nfs_page
*req
= data
->header
->req
;
1009 /* Set up the RPC argument and reply structs
1010 * NB: take care not to mess about with data->commit et al. */
1012 data
->args
.fh
= NFS_FH(data
->header
->inode
);
1013 data
->args
.offset
= req_offset(req
) + offset
;
1014 /* pnfs_set_layoutcommit needs this */
1015 data
->mds_offset
= data
->args
.offset
;
1016 data
->args
.pgbase
= req
->wb_pgbase
+ offset
;
1017 data
->args
.pages
= data
->pages
.pagevec
;
1018 data
->args
.count
= count
;
1019 data
->args
.context
= get_nfs_open_context(req
->wb_context
);
1020 data
->args
.lock_context
= req
->wb_lock_context
;
1021 data
->args
.stable
= NFS_UNSTABLE
;
1022 switch (how
& (FLUSH_STABLE
| FLUSH_COND_STABLE
)) {
1025 case FLUSH_COND_STABLE
:
1026 if (nfs_reqs_to_commit(cinfo
))
1029 data
->args
.stable
= NFS_FILE_SYNC
;
1032 data
->res
.fattr
= &data
->fattr
;
1033 data
->res
.count
= count
;
1034 data
->res
.verf
= &data
->verf
;
1035 nfs_fattr_init(&data
->fattr
);
1038 static int nfs_do_write(struct nfs_pgio_data
*data
,
1039 const struct rpc_call_ops
*call_ops
,
1042 struct inode
*inode
= data
->header
->inode
;
1044 return nfs_initiate_write(NFS_CLIENT(inode
), data
, call_ops
, how
, 0);
1047 static int nfs_do_multiple_writes(struct list_head
*head
,
1048 const struct rpc_call_ops
*call_ops
,
1051 struct nfs_pgio_data
*data
;
1054 while (!list_empty(head
)) {
1057 data
= list_first_entry(head
, struct nfs_pgio_data
, list
);
1058 list_del_init(&data
->list
);
1060 ret2
= nfs_do_write(data
, call_ops
, how
);
1067 /* If a nfs_flush_* function fails, it should remove reqs from @head and
1068 * call this on each, which will prepare them to be retried on next
1069 * writeback using standard nfs.
1071 static void nfs_redirty_request(struct nfs_page
*req
)
1073 nfs_mark_request_dirty(req
);
1074 nfs_unlock_request(req
);
1075 nfs_end_page_writeback(req
->wb_page
);
1076 nfs_release_request(req
);
1079 static void nfs_async_write_error(struct list_head
*head
)
1081 struct nfs_page
*req
;
1083 while (!list_empty(head
)) {
1084 req
= nfs_list_entry(head
->next
);
1085 nfs_list_remove_request(req
);
1086 nfs_redirty_request(req
);
1090 static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops
= {
1091 .error_cleanup
= nfs_async_write_error
,
1092 .completion
= nfs_write_completion
,
1095 static void nfs_flush_error(struct nfs_pageio_descriptor
*desc
,
1096 struct nfs_pgio_header
*hdr
)
1098 set_bit(NFS_IOHDR_REDO
, &hdr
->flags
);
1099 while (!list_empty(&hdr
->rpc_list
)) {
1100 struct nfs_pgio_data
*data
= list_first_entry(&hdr
->rpc_list
,
1101 struct nfs_pgio_data
, list
);
1102 list_del(&data
->list
);
1103 nfs_pgio_data_release(data
);
1105 desc
->pg_completion_ops
->error_cleanup(&desc
->pg_list
);
1109 * Generate multiple small requests to write out a single
1110 * contiguous dirty area on one page.
1112 static int nfs_flush_multi(struct nfs_pageio_descriptor
*desc
,
1113 struct nfs_pgio_header
*hdr
)
1115 struct nfs_page
*req
= hdr
->req
;
1116 struct page
*page
= req
->wb_page
;
1117 struct nfs_pgio_data
*data
;
1118 size_t wsize
= desc
->pg_bsize
, nbytes
;
1119 unsigned int offset
;
1121 struct nfs_commit_info cinfo
;
1123 nfs_init_cinfo(&cinfo
, desc
->pg_inode
, desc
->pg_dreq
);
1125 if ((desc
->pg_ioflags
& FLUSH_COND_STABLE
) &&
1126 (desc
->pg_moreio
|| nfs_reqs_to_commit(&cinfo
) ||
1127 desc
->pg_count
> wsize
))
1128 desc
->pg_ioflags
&= ~FLUSH_COND_STABLE
;
1132 nbytes
= desc
->pg_count
;
1134 size_t len
= min(nbytes
, wsize
);
1136 data
= nfs_pgio_data_alloc(hdr
, 1);
1138 nfs_flush_error(desc
, hdr
);
1141 data
->pages
.pagevec
[0] = page
;
1142 nfs_write_rpcsetup(data
, len
, offset
, desc
->pg_ioflags
, &cinfo
);
1143 list_add(&data
->list
, &hdr
->rpc_list
);
1147 } while (nbytes
!= 0);
1148 nfs_list_remove_request(req
);
1149 nfs_list_add_request(req
, &hdr
->pages
);
1150 desc
->pg_rpc_callops
= &nfs_write_common_ops
;
1155 * Create an RPC task for the given write request and kick it.
1156 * The page must have been locked by the caller.
1158 * It may happen that the page we're passed is not marked dirty.
1159 * This is the case if nfs_updatepage detects a conflicting request
1160 * that has been written but not committed.
1162 static int nfs_flush_one(struct nfs_pageio_descriptor
*desc
,
1163 struct nfs_pgio_header
*hdr
)
1165 struct nfs_page
*req
;
1166 struct page
**pages
;
1167 struct nfs_pgio_data
*data
;
1168 struct list_head
*head
= &desc
->pg_list
;
1169 struct nfs_commit_info cinfo
;
1171 data
= nfs_pgio_data_alloc(hdr
, nfs_page_array_len(desc
->pg_base
,
1174 nfs_flush_error(desc
, hdr
);
1178 nfs_init_cinfo(&cinfo
, desc
->pg_inode
, desc
->pg_dreq
);
1179 pages
= data
->pages
.pagevec
;
1180 while (!list_empty(head
)) {
1181 req
= nfs_list_entry(head
->next
);
1182 nfs_list_remove_request(req
);
1183 nfs_list_add_request(req
, &hdr
->pages
);
1184 *pages
++ = req
->wb_page
;
1187 if ((desc
->pg_ioflags
& FLUSH_COND_STABLE
) &&
1188 (desc
->pg_moreio
|| nfs_reqs_to_commit(&cinfo
)))
1189 desc
->pg_ioflags
&= ~FLUSH_COND_STABLE
;
1191 /* Set up the argument struct */
1192 nfs_write_rpcsetup(data
, desc
->pg_count
, 0, desc
->pg_ioflags
, &cinfo
);
1193 list_add(&data
->list
, &hdr
->rpc_list
);
1194 desc
->pg_rpc_callops
= &nfs_write_common_ops
;
1198 int nfs_generic_flush(struct nfs_pageio_descriptor
*desc
,
1199 struct nfs_pgio_header
*hdr
)
1201 if (desc
->pg_bsize
< PAGE_CACHE_SIZE
)
1202 return nfs_flush_multi(desc
, hdr
);
1203 return nfs_flush_one(desc
, hdr
);
1205 EXPORT_SYMBOL_GPL(nfs_generic_flush
);
1207 static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor
*desc
)
1209 struct nfs_rw_header
*whdr
;
1210 struct nfs_pgio_header
*hdr
;
1213 whdr
= nfs_writehdr_alloc();
1215 desc
->pg_completion_ops
->error_cleanup(&desc
->pg_list
);
1218 hdr
= &whdr
->header
;
1219 nfs_pgheader_init(desc
, hdr
, nfs_writehdr_free
);
1220 atomic_inc(&hdr
->refcnt
);
1221 ret
= nfs_generic_flush(desc
, hdr
);
1223 ret
= nfs_do_multiple_writes(&hdr
->rpc_list
,
1224 desc
->pg_rpc_callops
,
1226 if (atomic_dec_and_test(&hdr
->refcnt
))
1227 hdr
->completion_ops
->completion(hdr
);
1231 static const struct nfs_pageio_ops nfs_pageio_write_ops
= {
1232 .pg_test
= nfs_generic_pg_test
,
1233 .pg_doio
= nfs_generic_pg_writepages
,
1236 void nfs_pageio_init_write(struct nfs_pageio_descriptor
*pgio
,
1237 struct inode
*inode
, int ioflags
, bool force_mds
,
1238 const struct nfs_pgio_completion_ops
*compl_ops
)
1240 struct nfs_server
*server
= NFS_SERVER(inode
);
1241 const struct nfs_pageio_ops
*pg_ops
= &nfs_pageio_write_ops
;
1243 #ifdef CONFIG_NFS_V4_1
1244 if (server
->pnfs_curr_ld
&& !force_mds
)
1245 pg_ops
= server
->pnfs_curr_ld
->pg_write_ops
;
1247 nfs_pageio_init(pgio
, inode
, pg_ops
, compl_ops
, server
->wsize
, ioflags
);
1249 EXPORT_SYMBOL_GPL(nfs_pageio_init_write
);
1251 void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor
*pgio
)
1253 pgio
->pg_ops
= &nfs_pageio_write_ops
;
1254 pgio
->pg_bsize
= NFS_SERVER(pgio
->pg_inode
)->wsize
;
1256 EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds
);
1259 void nfs_write_prepare(struct rpc_task
*task
, void *calldata
)
1261 struct nfs_pgio_data
*data
= calldata
;
1263 err
= NFS_PROTO(data
->header
->inode
)->write_rpc_prepare(task
, data
);
1265 rpc_exit(task
, err
);
1268 void nfs_commit_prepare(struct rpc_task
*task
, void *calldata
)
1270 struct nfs_commit_data
*data
= calldata
;
1272 NFS_PROTO(data
->inode
)->commit_rpc_prepare(task
, data
);
1276 * Handle a write reply that flushes a whole page.
1278 * FIXME: There is an inherent race with invalidate_inode_pages and
1279 * writebacks since the page->count is kept > 1 for as long
1280 * as the page has a write request pending.
1282 static void nfs_writeback_done_common(struct rpc_task
*task
, void *calldata
)
1284 struct nfs_pgio_data
*data
= calldata
;
1286 nfs_writeback_done(task
, data
);
1289 static void nfs_writeback_release_common(void *calldata
)
1291 struct nfs_pgio_data
*data
= calldata
;
1292 struct nfs_pgio_header
*hdr
= data
->header
;
1293 int status
= data
->task
.tk_status
;
1295 if ((status
>= 0) && nfs_write_need_commit(data
)) {
1296 spin_lock(&hdr
->lock
);
1297 if (test_bit(NFS_IOHDR_NEED_RESCHED
, &hdr
->flags
))
1299 else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT
, &hdr
->flags
))
1300 memcpy(&hdr
->verf
, &data
->verf
, sizeof(hdr
->verf
));
1301 else if (memcmp(&hdr
->verf
, &data
->verf
, sizeof(hdr
->verf
)))
1302 set_bit(NFS_IOHDR_NEED_RESCHED
, &hdr
->flags
);
1303 spin_unlock(&hdr
->lock
);
1305 nfs_pgio_data_release(data
);
1308 static const struct rpc_call_ops nfs_write_common_ops
= {
1309 .rpc_call_prepare
= nfs_write_prepare
,
1310 .rpc_call_done
= nfs_writeback_done_common
,
1311 .rpc_release
= nfs_writeback_release_common
,
1315 * Special version of should_remove_suid() that ignores capabilities.
1317 static int nfs_should_remove_suid(const struct inode
*inode
)
1319 umode_t mode
= inode
->i_mode
;
1322 /* suid always must be killed */
1323 if (unlikely(mode
& S_ISUID
))
1324 kill
= ATTR_KILL_SUID
;
1327 * sgid without any exec bits is just a mandatory locking mark; leave
1328 * it alone. If some exec bits are set, it's a real sgid; kill it.
1330 if (unlikely((mode
& S_ISGID
) && (mode
& S_IXGRP
)))
1331 kill
|= ATTR_KILL_SGID
;
1333 if (unlikely(kill
&& S_ISREG(mode
)))
1340 * This function is called when the WRITE call is complete.
1342 void nfs_writeback_done(struct rpc_task
*task
, struct nfs_pgio_data
*data
)
1344 struct nfs_pgio_args
*argp
= &data
->args
;
1345 struct nfs_pgio_res
*resp
= &data
->res
;
1346 struct inode
*inode
= data
->header
->inode
;
1349 dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
1350 task
->tk_pid
, task
->tk_status
);
1353 * ->write_done will attempt to use post-op attributes to detect
1354 * conflicting writes by other clients. A strict interpretation
1355 * of close-to-open would allow us to continue caching even if
1356 * another writer had changed the file, but some applications
1357 * depend on tighter cache coherency when writing.
1359 status
= NFS_PROTO(inode
)->write_done(task
, data
);
1362 nfs_add_stats(inode
, NFSIOS_SERVERWRITTENBYTES
, resp
->count
);
1364 #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
1365 if (resp
->verf
->committed
< argp
->stable
&& task
->tk_status
>= 0) {
1366 /* We tried a write call, but the server did not
1367 * commit data to stable storage even though we
1369 * Note: There is a known bug in Tru64 < 5.0 in which
1370 * the server reports NFS_DATA_SYNC, but performs
1371 * NFS_FILE_SYNC. We therefore implement this checking
1372 * as a dprintk() in order to avoid filling syslog.
1374 static unsigned long complain
;
1376 /* Note this will print the MDS for a DS write */
1377 if (time_before(complain
, jiffies
)) {
1378 dprintk("NFS: faulty NFS server %s:"
1379 " (committed = %d) != (stable = %d)\n",
1380 NFS_SERVER(inode
)->nfs_client
->cl_hostname
,
1381 resp
->verf
->committed
, argp
->stable
);
1382 complain
= jiffies
+ 300 * HZ
;
1386 if (task
->tk_status
< 0) {
1387 nfs_set_pgio_error(data
->header
, task
->tk_status
, argp
->offset
);
1391 /* Deal with the suid/sgid bit corner case */
1392 if (nfs_should_remove_suid(inode
))
1393 nfs_mark_for_revalidate(inode
);
1395 if (resp
->count
< argp
->count
) {
1396 static unsigned long complain
;
1398 /* This a short write! */
1399 nfs_inc_stats(inode
, NFSIOS_SHORTWRITE
);
1401 /* Has the server at least made some progress? */
1402 if (resp
->count
== 0) {
1403 if (time_before(complain
, jiffies
)) {
1405 "NFS: Server wrote zero bytes, expected %u.\n",
1407 complain
= jiffies
+ 300 * HZ
;
1409 nfs_set_pgio_error(data
->header
, -EIO
, argp
->offset
);
1410 task
->tk_status
= -EIO
;
1413 /* Was this an NFSv2 write or an NFSv3 stable write? */
1414 if (resp
->verf
->committed
!= NFS_UNSTABLE
) {
1415 /* Resend from where the server left off */
1416 data
->mds_offset
+= resp
->count
;
1417 argp
->offset
+= resp
->count
;
1418 argp
->pgbase
+= resp
->count
;
1419 argp
->count
-= resp
->count
;
1421 /* Resend as a stable write in order to avoid
1422 * headaches in the case of a server crash.
1424 argp
->stable
= NFS_FILE_SYNC
;
1426 rpc_restart_call_prepare(task
);
1431 #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
1432 static int nfs_commit_set_lock(struct nfs_inode
*nfsi
, int may_wait
)
1436 if (!test_and_set_bit(NFS_INO_COMMIT
, &nfsi
->flags
))
1440 ret
= out_of_line_wait_on_bit_lock(&nfsi
->flags
,
1442 nfs_wait_bit_killable
,
1444 return (ret
< 0) ? ret
: 1;
1447 static void nfs_commit_clear_lock(struct nfs_inode
*nfsi
)
1449 clear_bit(NFS_INO_COMMIT
, &nfsi
->flags
);
1450 smp_mb__after_clear_bit();
1451 wake_up_bit(&nfsi
->flags
, NFS_INO_COMMIT
);
1454 void nfs_commitdata_release(struct nfs_commit_data
*data
)
1456 put_nfs_open_context(data
->context
);
1457 nfs_commit_free(data
);
1459 EXPORT_SYMBOL_GPL(nfs_commitdata_release
);
1461 int nfs_initiate_commit(struct rpc_clnt
*clnt
, struct nfs_commit_data
*data
,
1462 const struct rpc_call_ops
*call_ops
,
1465 struct rpc_task
*task
;
1466 int priority
= flush_task_priority(how
);
1467 struct rpc_message msg
= {
1468 .rpc_argp
= &data
->args
,
1469 .rpc_resp
= &data
->res
,
1470 .rpc_cred
= data
->cred
,
1472 struct rpc_task_setup task_setup_data
= {
1473 .task
= &data
->task
,
1475 .rpc_message
= &msg
,
1476 .callback_ops
= call_ops
,
1477 .callback_data
= data
,
1478 .workqueue
= nfsiod_workqueue
,
1479 .flags
= RPC_TASK_ASYNC
| flags
,
1480 .priority
= priority
,
1482 /* Set up the initial task struct. */
1483 NFS_PROTO(data
->inode
)->commit_setup(data
, &msg
);
1485 dprintk("NFS: %5u initiated commit call\n", data
->task
.tk_pid
);
1487 nfs4_state_protect(NFS_SERVER(data
->inode
)->nfs_client
,
1488 NFS_SP4_MACH_CRED_COMMIT
, &task_setup_data
.rpc_client
, &msg
);
1490 task
= rpc_run_task(&task_setup_data
);
1492 return PTR_ERR(task
);
1493 if (how
& FLUSH_SYNC
)
1494 rpc_wait_for_completion_task(task
);
1498 EXPORT_SYMBOL_GPL(nfs_initiate_commit
);
1501 * Set up the argument/result storage required for the RPC call.
1503 void nfs_init_commit(struct nfs_commit_data
*data
,
1504 struct list_head
*head
,
1505 struct pnfs_layout_segment
*lseg
,
1506 struct nfs_commit_info
*cinfo
)
1508 struct nfs_page
*first
= nfs_list_entry(head
->next
);
1509 struct inode
*inode
= first
->wb_context
->dentry
->d_inode
;
1511 /* Set up the RPC argument and reply structs
1512 * NB: take care not to mess about with data->commit et al. */
1514 list_splice_init(head
, &data
->pages
);
1516 data
->inode
= inode
;
1517 data
->cred
= first
->wb_context
->cred
;
1518 data
->lseg
= lseg
; /* reference transferred */
1519 data
->mds_ops
= &nfs_commit_ops
;
1520 data
->completion_ops
= cinfo
->completion_ops
;
1521 data
->dreq
= cinfo
->dreq
;
1523 data
->args
.fh
= NFS_FH(data
->inode
);
1524 /* Note: we always request a commit of the entire inode */
1525 data
->args
.offset
= 0;
1526 data
->args
.count
= 0;
1527 data
->context
= get_nfs_open_context(first
->wb_context
);
1528 data
->res
.fattr
= &data
->fattr
;
1529 data
->res
.verf
= &data
->verf
;
1530 nfs_fattr_init(&data
->fattr
);
1532 EXPORT_SYMBOL_GPL(nfs_init_commit
);
1534 void nfs_retry_commit(struct list_head
*page_list
,
1535 struct pnfs_layout_segment
*lseg
,
1536 struct nfs_commit_info
*cinfo
)
1538 struct nfs_page
*req
;
1540 while (!list_empty(page_list
)) {
1541 req
= nfs_list_entry(page_list
->next
);
1542 nfs_list_remove_request(req
);
1543 nfs_mark_request_commit(req
, lseg
, cinfo
);
1545 dec_zone_page_state(req
->wb_page
, NR_UNSTABLE_NFS
);
1546 dec_bdi_stat(page_file_mapping(req
->wb_page
)->backing_dev_info
,
1549 nfs_unlock_and_release_request(req
);
1552 EXPORT_SYMBOL_GPL(nfs_retry_commit
);
1555 * Commit dirty pages
1558 nfs_commit_list(struct inode
*inode
, struct list_head
*head
, int how
,
1559 struct nfs_commit_info
*cinfo
)
1561 struct nfs_commit_data
*data
;
1563 data
= nfs_commitdata_alloc();
1568 /* Set up the argument struct */
1569 nfs_init_commit(data
, head
, NULL
, cinfo
);
1570 atomic_inc(&cinfo
->mds
->rpcs_out
);
1571 return nfs_initiate_commit(NFS_CLIENT(inode
), data
, data
->mds_ops
,
1574 nfs_retry_commit(head
, NULL
, cinfo
);
1575 cinfo
->completion_ops
->error_cleanup(NFS_I(inode
));
1580 * COMMIT call returned
1582 static void nfs_commit_done(struct rpc_task
*task
, void *calldata
)
1584 struct nfs_commit_data
*data
= calldata
;
1586 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1587 task
->tk_pid
, task
->tk_status
);
1589 /* Call the NFS version-specific code */
1590 NFS_PROTO(data
->inode
)->commit_done(task
, data
);
1593 static void nfs_commit_release_pages(struct nfs_commit_data
*data
)
1595 struct nfs_page
*req
;
1596 int status
= data
->task
.tk_status
;
1597 struct nfs_commit_info cinfo
;
1599 while (!list_empty(&data
->pages
)) {
1600 req
= nfs_list_entry(data
->pages
.next
);
1601 nfs_list_remove_request(req
);
1602 nfs_clear_page_commit(req
->wb_page
);
1604 dprintk("NFS: commit (%s/%llu %d@%lld)",
1605 req
->wb_context
->dentry
->d_sb
->s_id
,
1606 (unsigned long long)NFS_FILEID(req
->wb_context
->dentry
->d_inode
),
1608 (long long)req_offset(req
));
1610 nfs_context_set_write_error(req
->wb_context
, status
);
1611 nfs_inode_remove_request(req
);
1612 dprintk(", error = %d\n", status
);
1616 /* Okay, COMMIT succeeded, apparently. Check the verifier
1617 * returned by the server against all stored verfs. */
1618 if (!memcmp(&req
->wb_verf
, &data
->verf
.verifier
, sizeof(req
->wb_verf
))) {
1619 /* We have a match */
1620 nfs_inode_remove_request(req
);
1624 /* We have a mismatch. Write the page again */
1625 dprintk(" mismatch\n");
1626 nfs_mark_request_dirty(req
);
1627 set_bit(NFS_CONTEXT_RESEND_WRITES
, &req
->wb_context
->flags
);
1629 nfs_unlock_and_release_request(req
);
1631 nfs_init_cinfo(&cinfo
, data
->inode
, data
->dreq
);
1632 if (atomic_dec_and_test(&cinfo
.mds
->rpcs_out
))
1633 nfs_commit_clear_lock(NFS_I(data
->inode
));
1636 static void nfs_commit_release(void *calldata
)
1638 struct nfs_commit_data
*data
= calldata
;
1640 data
->completion_ops
->completion(data
);
1641 nfs_commitdata_release(calldata
);
1644 static const struct rpc_call_ops nfs_commit_ops
= {
1645 .rpc_call_prepare
= nfs_commit_prepare
,
1646 .rpc_call_done
= nfs_commit_done
,
1647 .rpc_release
= nfs_commit_release
,
1650 static const struct nfs_commit_completion_ops nfs_commit_completion_ops
= {
1651 .completion
= nfs_commit_release_pages
,
1652 .error_cleanup
= nfs_commit_clear_lock
,
1655 int nfs_generic_commit_list(struct inode
*inode
, struct list_head
*head
,
1656 int how
, struct nfs_commit_info
*cinfo
)
1660 status
= pnfs_commit_list(inode
, head
, how
, cinfo
);
1661 if (status
== PNFS_NOT_ATTEMPTED
)
1662 status
= nfs_commit_list(inode
, head
, how
, cinfo
);
1666 int nfs_commit_inode(struct inode
*inode
, int how
)
1669 struct nfs_commit_info cinfo
;
1670 int may_wait
= how
& FLUSH_SYNC
;
1673 res
= nfs_commit_set_lock(NFS_I(inode
), may_wait
);
1675 goto out_mark_dirty
;
1676 nfs_init_cinfo_from_inode(&cinfo
, inode
);
1677 res
= nfs_scan_commit(inode
, &head
, &cinfo
);
1681 error
= nfs_generic_commit_list(inode
, &head
, how
, &cinfo
);
1685 goto out_mark_dirty
;
1686 error
= wait_on_bit(&NFS_I(inode
)->flags
,
1688 nfs_wait_bit_killable
,
1693 nfs_commit_clear_lock(NFS_I(inode
));
1695 /* Note: If we exit without ensuring that the commit is complete,
1696 * we must mark the inode as dirty. Otherwise, future calls to
1697 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1698 * that the data is on the disk.
1701 __mark_inode_dirty(inode
, I_DIRTY_DATASYNC
);
1705 static int nfs_commit_unstable_pages(struct inode
*inode
, struct writeback_control
*wbc
)
1707 struct nfs_inode
*nfsi
= NFS_I(inode
);
1708 int flags
= FLUSH_SYNC
;
1711 /* no commits means nothing needs to be done */
1712 if (!nfsi
->commit_info
.ncommit
)
1715 if (wbc
->sync_mode
== WB_SYNC_NONE
) {
1716 /* Don't commit yet if this is a non-blocking flush and there
1717 * are a lot of outstanding writes for this mapping.
1719 if (nfsi
->commit_info
.ncommit
<= (nfsi
->npages
>> 1))
1720 goto out_mark_dirty
;
1722 /* don't wait for the COMMIT response */
1726 ret
= nfs_commit_inode(inode
, flags
);
1728 if (wbc
->sync_mode
== WB_SYNC_NONE
) {
1729 if (ret
< wbc
->nr_to_write
)
1730 wbc
->nr_to_write
-= ret
;
1732 wbc
->nr_to_write
= 0;
1737 __mark_inode_dirty(inode
, I_DIRTY_DATASYNC
);
1741 static int nfs_commit_unstable_pages(struct inode
*inode
, struct writeback_control
*wbc
)
1747 int nfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1749 return nfs_commit_unstable_pages(inode
, wbc
);
1751 EXPORT_SYMBOL_GPL(nfs_write_inode
);
1754 * flush the inode to disk.
1756 int nfs_wb_all(struct inode
*inode
)
1758 struct writeback_control wbc
= {
1759 .sync_mode
= WB_SYNC_ALL
,
1760 .nr_to_write
= LONG_MAX
,
1762 .range_end
= LLONG_MAX
,
1766 trace_nfs_writeback_inode_enter(inode
);
1768 ret
= sync_inode(inode
, &wbc
);
1770 trace_nfs_writeback_inode_exit(inode
, ret
);
1773 EXPORT_SYMBOL_GPL(nfs_wb_all
);
1775 int nfs_wb_page_cancel(struct inode
*inode
, struct page
*page
)
1777 struct nfs_page
*req
;
1781 wait_on_page_writeback(page
);
1782 req
= nfs_page_find_request(page
);
1785 if (nfs_lock_request(req
)) {
1786 nfs_clear_request_commit(req
);
1787 nfs_inode_remove_request(req
);
1789 * In case nfs_inode_remove_request has marked the
1790 * page as being dirty
1792 cancel_dirty_page(page
, PAGE_CACHE_SIZE
);
1793 nfs_unlock_and_release_request(req
);
1796 ret
= nfs_wait_on_request(req
);
1797 nfs_release_request(req
);
1805 * Write back all requests on one page - we do this before reading it.
1807 int nfs_wb_page(struct inode
*inode
, struct page
*page
)
1809 loff_t range_start
= page_file_offset(page
);
1810 loff_t range_end
= range_start
+ (loff_t
)(PAGE_CACHE_SIZE
- 1);
1811 struct writeback_control wbc
= {
1812 .sync_mode
= WB_SYNC_ALL
,
1814 .range_start
= range_start
,
1815 .range_end
= range_end
,
1819 trace_nfs_writeback_page_enter(inode
);
1822 wait_on_page_writeback(page
);
1823 if (clear_page_dirty_for_io(page
)) {
1824 ret
= nfs_writepage_locked(page
, &wbc
);
1830 if (!PagePrivate(page
))
1832 ret
= nfs_commit_inode(inode
, FLUSH_SYNC
);
1837 trace_nfs_writeback_page_exit(inode
, ret
);
1841 #ifdef CONFIG_MIGRATION
1842 int nfs_migrate_page(struct address_space
*mapping
, struct page
*newpage
,
1843 struct page
*page
, enum migrate_mode mode
)
1846 * If PagePrivate is set, then the page is currently associated with
1847 * an in-progress read or write request. Don't try to migrate it.
1849 * FIXME: we could do this in principle, but we'll need a way to ensure
1850 * that we can safely release the inode reference while holding
1853 if (PagePrivate(page
))
1856 if (!nfs_fscache_release_page(page
, GFP_KERNEL
))
1859 return migrate_page(mapping
, newpage
, page
, mode
);
1863 int __init
nfs_init_writepagecache(void)
1865 nfs_wdata_cachep
= kmem_cache_create("nfs_write_data",
1866 sizeof(struct nfs_rw_header
),
1867 0, SLAB_HWCACHE_ALIGN
,
1869 if (nfs_wdata_cachep
== NULL
)
1872 nfs_wdata_mempool
= mempool_create_slab_pool(MIN_POOL_WRITE
,
1874 if (nfs_wdata_mempool
== NULL
)
1875 goto out_destroy_write_cache
;
1877 nfs_cdata_cachep
= kmem_cache_create("nfs_commit_data",
1878 sizeof(struct nfs_commit_data
),
1879 0, SLAB_HWCACHE_ALIGN
,
1881 if (nfs_cdata_cachep
== NULL
)
1882 goto out_destroy_write_mempool
;
1884 nfs_commit_mempool
= mempool_create_slab_pool(MIN_POOL_COMMIT
,
1886 if (nfs_commit_mempool
== NULL
)
1887 goto out_destroy_commit_cache
;
1890 * NFS congestion size, scale with available memory.
1902 * This allows larger machines to have larger/more transfers.
1903 * Limit the default to 256M
1905 nfs_congestion_kb
= (16*int_sqrt(totalram_pages
)) << (PAGE_SHIFT
-10);
1906 if (nfs_congestion_kb
> 256*1024)
1907 nfs_congestion_kb
= 256*1024;
1911 out_destroy_commit_cache
:
1912 kmem_cache_destroy(nfs_cdata_cachep
);
1913 out_destroy_write_mempool
:
1914 mempool_destroy(nfs_wdata_mempool
);
1915 out_destroy_write_cache
:
1916 kmem_cache_destroy(nfs_wdata_cachep
);
1920 void nfs_destroy_writepagecache(void)
1922 mempool_destroy(nfs_commit_mempool
);
1923 kmem_cache_destroy(nfs_cdata_cachep
);
1924 mempool_destroy(nfs_wdata_mempool
);
1925 kmem_cache_destroy(nfs_wdata_cachep
);