1 // SPDX-License-Identifier: GPL-2.0-only
5 * Write file data over NFS.
7 * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
10 #include <linux/types.h>
11 #include <linux/slab.h>
13 #include <linux/pagemap.h>
14 #include <linux/file.h>
15 #include <linux/writeback.h>
16 #include <linux/swap.h>
17 #include <linux/migrate.h>
19 #include <linux/sunrpc/clnt.h>
20 #include <linux/nfs_fs.h>
21 #include <linux/nfs_mount.h>
22 #include <linux/nfs_page.h>
23 #include <linux/backing-dev.h>
24 #include <linux/export.h>
25 #include <linux/freezer.h>
26 #include <linux/wait.h>
27 #include <linux/iversion.h>
29 #include <linux/uaccess.h>
30 #include <linux/sched/mm.h>
32 #include "delegation.h"
41 #define NFSDBG_FACILITY NFSDBG_PAGECACHE
43 #define MIN_POOL_WRITE (32)
44 #define MIN_POOL_COMMIT (4)
46 struct nfs_io_completion
{
47 void (*complete
)(void *data
);
53 * Local function declarations
55 static void nfs_redirty_request(struct nfs_page
*req
);
56 static const struct rpc_call_ops nfs_commit_ops
;
57 static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops
;
58 static const struct nfs_commit_completion_ops nfs_commit_completion_ops
;
59 static const struct nfs_rw_ops nfs_rw_write_ops
;
60 static void nfs_inode_remove_request(struct nfs_page
*req
);
61 static void nfs_clear_request_commit(struct nfs_page
*req
);
62 static void nfs_init_cinfo_from_inode(struct nfs_commit_info
*cinfo
,
64 static struct nfs_page
*
65 nfs_page_search_commits_for_head_request_locked(struct nfs_inode
*nfsi
,
68 static struct kmem_cache
*nfs_wdata_cachep
;
69 static mempool_t
*nfs_wdata_mempool
;
70 static struct kmem_cache
*nfs_cdata_cachep
;
71 static mempool_t
*nfs_commit_mempool
;
73 struct nfs_commit_data
*nfs_commitdata_alloc(bool never_fail
)
75 struct nfs_commit_data
*p
;
78 p
= mempool_alloc(nfs_commit_mempool
, GFP_NOIO
);
80 /* It is OK to do some reclaim, not no safe to wait
81 * for anything to be returned to the pool.
82 * mempool_alloc() cannot handle that particular combination,
83 * so we need two separate attempts.
85 p
= mempool_alloc(nfs_commit_mempool
, GFP_NOWAIT
);
87 p
= kmem_cache_alloc(nfs_cdata_cachep
, GFP_NOIO
|
88 __GFP_NOWARN
| __GFP_NORETRY
);
93 memset(p
, 0, sizeof(*p
));
94 INIT_LIST_HEAD(&p
->pages
);
97 EXPORT_SYMBOL_GPL(nfs_commitdata_alloc
);
99 void nfs_commit_free(struct nfs_commit_data
*p
)
101 mempool_free(p
, nfs_commit_mempool
);
103 EXPORT_SYMBOL_GPL(nfs_commit_free
);
105 static struct nfs_pgio_header
*nfs_writehdr_alloc(void)
107 struct nfs_pgio_header
*p
= mempool_alloc(nfs_wdata_mempool
, GFP_KERNEL
);
109 memset(p
, 0, sizeof(*p
));
110 p
->rw_mode
= FMODE_WRITE
;
114 static void nfs_writehdr_free(struct nfs_pgio_header
*hdr
)
116 mempool_free(hdr
, nfs_wdata_mempool
);
119 static struct nfs_io_completion
*nfs_io_completion_alloc(gfp_t gfp_flags
)
121 return kmalloc(sizeof(struct nfs_io_completion
), gfp_flags
);
124 static void nfs_io_completion_init(struct nfs_io_completion
*ioc
,
125 void (*complete
)(void *), void *data
)
127 ioc
->complete
= complete
;
129 kref_init(&ioc
->refcount
);
132 static void nfs_io_completion_release(struct kref
*kref
)
134 struct nfs_io_completion
*ioc
= container_of(kref
,
135 struct nfs_io_completion
, refcount
);
136 ioc
->complete(ioc
->data
);
140 static void nfs_io_completion_get(struct nfs_io_completion
*ioc
)
143 kref_get(&ioc
->refcount
);
146 static void nfs_io_completion_put(struct nfs_io_completion
*ioc
)
149 kref_put(&ioc
->refcount
, nfs_io_completion_release
);
152 static struct nfs_page
*
153 nfs_page_private_request(struct page
*page
)
155 if (!PagePrivate(page
))
157 return (struct nfs_page
*)page_private(page
);
161 * nfs_page_find_head_request_locked - find head request associated with @page
163 * must be called while holding the inode lock.
165 * returns matching head request with reference held, or NULL if not found.
167 static struct nfs_page
*
168 nfs_page_find_private_request(struct page
*page
)
170 struct address_space
*mapping
= page_file_mapping(page
);
171 struct nfs_page
*req
;
173 if (!PagePrivate(page
))
175 spin_lock(&mapping
->private_lock
);
176 req
= nfs_page_private_request(page
);
178 WARN_ON_ONCE(req
->wb_head
!= req
);
179 kref_get(&req
->wb_kref
);
181 spin_unlock(&mapping
->private_lock
);
185 static struct nfs_page
*
186 nfs_page_find_swap_request(struct page
*page
)
188 struct inode
*inode
= page_file_mapping(page
)->host
;
189 struct nfs_inode
*nfsi
= NFS_I(inode
);
190 struct nfs_page
*req
= NULL
;
191 if (!PageSwapCache(page
))
193 mutex_lock(&nfsi
->commit_mutex
);
194 if (PageSwapCache(page
)) {
195 req
= nfs_page_search_commits_for_head_request_locked(nfsi
,
198 WARN_ON_ONCE(req
->wb_head
!= req
);
199 kref_get(&req
->wb_kref
);
202 mutex_unlock(&nfsi
->commit_mutex
);
207 * nfs_page_find_head_request - find head request associated with @page
209 * returns matching head request with reference held, or NULL if not found.
211 static struct nfs_page
*nfs_page_find_head_request(struct page
*page
)
213 struct nfs_page
*req
;
215 req
= nfs_page_find_private_request(page
);
217 req
= nfs_page_find_swap_request(page
);
221 /* Adjust the file length if we're writing beyond the end */
222 static void nfs_grow_file(struct page
*page
, unsigned int offset
, unsigned int count
)
224 struct inode
*inode
= page_file_mapping(page
)->host
;
228 spin_lock(&inode
->i_lock
);
229 i_size
= i_size_read(inode
);
230 end_index
= (i_size
- 1) >> PAGE_SHIFT
;
231 if (i_size
> 0 && page_index(page
) < end_index
)
233 end
= page_file_offset(page
) + ((loff_t
)offset
+count
);
236 i_size_write(inode
, end
);
237 NFS_I(inode
)->cache_validity
&= ~NFS_INO_INVALID_SIZE
;
238 nfs_inc_stats(inode
, NFSIOS_EXTENDWRITE
);
240 spin_unlock(&inode
->i_lock
);
243 /* A writeback failed: mark the page as bad, and invalidate the page cache */
244 static void nfs_set_pageerror(struct address_space
*mapping
)
246 struct inode
*inode
= mapping
->host
;
248 nfs_zap_mapping(mapping
->host
, mapping
);
249 /* Force file size revalidation */
250 spin_lock(&inode
->i_lock
);
251 NFS_I(inode
)->cache_validity
|= NFS_INO_REVAL_FORCED
|
252 NFS_INO_REVAL_PAGECACHE
|
253 NFS_INO_INVALID_SIZE
;
254 spin_unlock(&inode
->i_lock
);
257 static void nfs_mapping_set_error(struct page
*page
, int error
)
259 struct address_space
*mapping
= page_file_mapping(page
);
262 mapping_set_error(mapping
, error
);
263 nfs_set_pageerror(mapping
);
267 * nfs_page_group_search_locked
268 * @head - head request of page group
269 * @page_offset - offset into page
271 * Search page group with head @head to find a request that contains the
272 * page offset @page_offset.
274 * Returns a pointer to the first matching nfs request, or NULL if no
277 * Must be called with the page group lock held
279 static struct nfs_page
*
280 nfs_page_group_search_locked(struct nfs_page
*head
, unsigned int page_offset
)
282 struct nfs_page
*req
;
286 if (page_offset
>= req
->wb_pgbase
&&
287 page_offset
< (req
->wb_pgbase
+ req
->wb_bytes
))
290 req
= req
->wb_this_page
;
291 } while (req
!= head
);
297 * nfs_page_group_covers_page
298 * @head - head request of page group
300 * Return true if the page group with head @head covers the whole page,
301 * returns false otherwise
303 static bool nfs_page_group_covers_page(struct nfs_page
*req
)
305 struct nfs_page
*tmp
;
306 unsigned int pos
= 0;
307 unsigned int len
= nfs_page_length(req
->wb_page
);
309 nfs_page_group_lock(req
);
312 tmp
= nfs_page_group_search_locked(req
->wb_head
, pos
);
315 pos
= tmp
->wb_pgbase
+ tmp
->wb_bytes
;
318 nfs_page_group_unlock(req
);
322 /* We can set the PG_uptodate flag if we see that a write request
323 * covers the full page.
325 static void nfs_mark_uptodate(struct nfs_page
*req
)
327 if (PageUptodate(req
->wb_page
))
329 if (!nfs_page_group_covers_page(req
))
331 SetPageUptodate(req
->wb_page
);
334 static int wb_priority(struct writeback_control
*wbc
)
338 if (wbc
->sync_mode
== WB_SYNC_ALL
)
339 ret
= FLUSH_COND_STABLE
;
344 * NFS congestion control
347 int nfs_congestion_kb
;
349 #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
350 #define NFS_CONGESTION_OFF_THRESH \
351 (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
353 static void nfs_set_page_writeback(struct page
*page
)
355 struct inode
*inode
= page_file_mapping(page
)->host
;
356 struct nfs_server
*nfss
= NFS_SERVER(inode
);
357 int ret
= test_set_page_writeback(page
);
359 WARN_ON_ONCE(ret
!= 0);
361 if (atomic_long_inc_return(&nfss
->writeback
) >
362 NFS_CONGESTION_ON_THRESH
)
363 set_bdi_congested(inode_to_bdi(inode
), BLK_RW_ASYNC
);
366 static void nfs_end_page_writeback(struct nfs_page
*req
)
368 struct inode
*inode
= page_file_mapping(req
->wb_page
)->host
;
369 struct nfs_server
*nfss
= NFS_SERVER(inode
);
372 is_done
= nfs_page_group_sync_on_bit(req
, PG_WB_END
);
373 nfs_unlock_request(req
);
377 end_page_writeback(req
->wb_page
);
378 if (atomic_long_dec_return(&nfss
->writeback
) < NFS_CONGESTION_OFF_THRESH
)
379 clear_bdi_congested(inode_to_bdi(inode
), BLK_RW_ASYNC
);
383 * nfs_unroll_locks_and_wait - unlock all newly locked reqs and wait on @req
385 * this is a helper function for nfs_lock_and_join_requests
387 * @inode - inode associated with request page group, must be holding inode lock
388 * @head - head request of page group, must be holding head lock
389 * @req - request that couldn't lock and needs to wait on the req bit lock
391 * NOTE: this must be called holding page_group bit lock
392 * which will be released before returning.
394 * returns 0 on success, < 0 on error.
397 nfs_unroll_locks(struct inode
*inode
, struct nfs_page
*head
,
398 struct nfs_page
*req
)
400 struct nfs_page
*tmp
;
402 /* relinquish all the locks successfully grabbed this run */
403 for (tmp
= head
->wb_this_page
; tmp
!= req
; tmp
= tmp
->wb_this_page
) {
404 if (!kref_read(&tmp
->wb_kref
))
406 nfs_unlock_and_release_request(tmp
);
411 * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests
413 * @destroy_list - request list (using wb_this_page) terminated by @old_head
414 * @old_head - the old head of the list
416 * All subrequests must be locked and removed from all lists, so at this point
417 * they are only "active" in this function, and possibly in nfs_wait_on_request
418 * with a reference held by some other context.
421 nfs_destroy_unlinked_subrequests(struct nfs_page
*destroy_list
,
422 struct nfs_page
*old_head
,
425 while (destroy_list
) {
426 struct nfs_page
*subreq
= destroy_list
;
428 destroy_list
= (subreq
->wb_this_page
== old_head
) ?
429 NULL
: subreq
->wb_this_page
;
431 WARN_ON_ONCE(old_head
!= subreq
->wb_head
);
433 /* make sure old group is not used */
434 subreq
->wb_this_page
= subreq
;
436 clear_bit(PG_REMOVE
, &subreq
->wb_flags
);
438 /* Note: races with nfs_page_group_destroy() */
439 if (!kref_read(&subreq
->wb_kref
)) {
440 /* Check if we raced with nfs_page_group_destroy() */
441 if (test_and_clear_bit(PG_TEARDOWN
, &subreq
->wb_flags
))
442 nfs_free_request(subreq
);
446 subreq
->wb_head
= subreq
;
447 nfs_release_request(old_head
);
449 if (test_and_clear_bit(PG_INODE_REF
, &subreq
->wb_flags
)) {
450 nfs_release_request(subreq
);
451 atomic_long_dec(&NFS_I(inode
)->nrequests
);
454 /* subreq is now totally disconnected from page group or any
455 * write / commit lists. last chance to wake any waiters */
456 nfs_unlock_and_release_request(subreq
);
461 * nfs_lock_and_join_requests - join all subreqs to the head req and return
462 * a locked reference, cancelling any pending
463 * operations for this page.
465 * @page - the page used to lookup the "page group" of nfs_page structures
467 * This function joins all sub requests to the head request by first
468 * locking all requests in the group, cancelling any pending operations
469 * and finally updating the head request to cover the whole range covered by
470 * the (former) group. All subrequests are removed from any write or commit
471 * lists, unlinked from the group and destroyed.
473 * Returns a locked, referenced pointer to the head request - which after
474 * this call is guaranteed to be the only request associated with the page.
475 * Returns NULL if no requests are found for @page, or a ERR_PTR if an
476 * error was encountered.
478 static struct nfs_page
*
479 nfs_lock_and_join_requests(struct page
*page
)
481 struct inode
*inode
= page_file_mapping(page
)->host
;
482 struct nfs_page
*head
, *subreq
;
483 struct nfs_page
*destroy_list
= NULL
;
484 unsigned int total_bytes
;
489 * A reference is taken only on the head request which acts as a
490 * reference to the whole page group - the group will not be destroyed
491 * until the head reference is released.
493 head
= nfs_page_find_head_request(page
);
497 /* lock the page head first in order to avoid an ABBA inefficiency */
498 if (!nfs_lock_request(head
)) {
499 ret
= nfs_wait_on_request(head
);
500 nfs_release_request(head
);
506 /* Ensure that nobody removed the request before we locked it */
507 if (head
!= nfs_page_private_request(page
) && !PageSwapCache(page
)) {
508 nfs_unlock_and_release_request(head
);
512 ret
= nfs_page_group_lock(head
);
514 goto release_request
;
516 /* lock each request in the page group */
517 total_bytes
= head
->wb_bytes
;
518 for (subreq
= head
->wb_this_page
; subreq
!= head
;
519 subreq
= subreq
->wb_this_page
) {
521 if (!kref_get_unless_zero(&subreq
->wb_kref
)) {
522 if (subreq
->wb_offset
== head
->wb_offset
+ total_bytes
)
523 total_bytes
+= subreq
->wb_bytes
;
527 while (!nfs_lock_request(subreq
)) {
529 * Unlock page to allow nfs_page_group_sync_on_bit()
532 nfs_page_group_unlock(head
);
533 ret
= nfs_wait_on_request(subreq
);
535 ret
= nfs_page_group_lock(head
);
537 nfs_unroll_locks(inode
, head
, subreq
);
538 nfs_release_request(subreq
);
539 goto release_request
;
543 * Subrequests are always contiguous, non overlapping
544 * and in order - but may be repeated (mirrored writes).
546 if (subreq
->wb_offset
== (head
->wb_offset
+ total_bytes
)) {
547 /* keep track of how many bytes this group covers */
548 total_bytes
+= subreq
->wb_bytes
;
549 } else if (WARN_ON_ONCE(subreq
->wb_offset
< head
->wb_offset
||
550 ((subreq
->wb_offset
+ subreq
->wb_bytes
) >
551 (head
->wb_offset
+ total_bytes
)))) {
552 nfs_page_group_unlock(head
);
553 nfs_unroll_locks(inode
, head
, subreq
);
554 nfs_unlock_and_release_request(subreq
);
556 goto release_request
;
560 /* Now that all requests are locked, make sure they aren't on any list.
561 * Commit list removal accounting is done after locks are dropped */
564 nfs_clear_request_commit(subreq
);
565 subreq
= subreq
->wb_this_page
;
566 } while (subreq
!= head
);
568 /* unlink subrequests from head, destroy them later */
569 if (head
->wb_this_page
!= head
) {
570 /* destroy list will be terminated by head */
571 destroy_list
= head
->wb_this_page
;
572 head
->wb_this_page
= head
;
574 /* change head request to cover whole range that
575 * the former page group covered */
576 head
->wb_bytes
= total_bytes
;
579 /* Postpone destruction of this request */
580 if (test_and_clear_bit(PG_REMOVE
, &head
->wb_flags
)) {
581 set_bit(PG_INODE_REF
, &head
->wb_flags
);
582 kref_get(&head
->wb_kref
);
583 atomic_long_inc(&NFS_I(inode
)->nrequests
);
586 nfs_page_group_unlock(head
);
588 nfs_destroy_unlinked_subrequests(destroy_list
, head
, inode
);
590 /* Did we lose a race with nfs_inode_remove_request()? */
591 if (!(PagePrivate(page
) || PageSwapCache(page
))) {
592 nfs_unlock_and_release_request(head
);
596 /* still holds ref on head from nfs_page_find_head_request
597 * and still has lock on head from lock loop */
601 nfs_unlock_and_release_request(head
);
605 static void nfs_write_error(struct nfs_page
*req
, int error
)
607 trace_nfs_write_error(req
, error
);
608 nfs_mapping_set_error(req
->wb_page
, error
);
609 nfs_inode_remove_request(req
);
610 nfs_end_page_writeback(req
);
611 nfs_release_request(req
);
615 * Find an associated nfs write request, and prepare to flush it out
616 * May return an error if the user signalled nfs_wait_on_request().
618 static int nfs_page_async_flush(struct nfs_pageio_descriptor
*pgio
,
621 struct nfs_page
*req
;
624 req
= nfs_lock_and_join_requests(page
);
631 nfs_set_page_writeback(page
);
632 WARN_ON_ONCE(test_bit(PG_CLEAN
, &req
->wb_flags
));
634 /* If there is a fatal error that covers this write, just exit */
635 ret
= pgio
->pg_error
;
636 if (nfs_error_is_fatal_on_server(ret
))
640 if (!nfs_pageio_add_request(pgio
, req
)) {
641 ret
= pgio
->pg_error
;
643 * Remove the problematic req upon fatal errors on the server
645 if (nfs_error_is_fatal(ret
)) {
646 if (nfs_error_is_fatal_on_server(ret
))
650 nfs_redirty_request(req
);
653 nfs_add_stats(page_file_mapping(page
)->host
,
654 NFSIOS_WRITEPAGES
, 1);
658 nfs_write_error(req
, ret
);
662 static int nfs_do_writepage(struct page
*page
, struct writeback_control
*wbc
,
663 struct nfs_pageio_descriptor
*pgio
)
667 nfs_pageio_cond_complete(pgio
, page_index(page
));
668 ret
= nfs_page_async_flush(pgio
, page
);
669 if (ret
== -EAGAIN
) {
670 redirty_page_for_writepage(wbc
, page
);
671 ret
= AOP_WRITEPAGE_ACTIVATE
;
677 * Write an mmapped page to the server.
679 static int nfs_writepage_locked(struct page
*page
,
680 struct writeback_control
*wbc
)
682 struct nfs_pageio_descriptor pgio
;
683 struct inode
*inode
= page_file_mapping(page
)->host
;
686 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGE
);
687 nfs_pageio_init_write(&pgio
, inode
, 0,
688 false, &nfs_async_write_completion_ops
);
689 err
= nfs_do_writepage(page
, wbc
, &pgio
);
691 nfs_pageio_complete(&pgio
);
694 if (nfs_error_is_fatal(pgio
.pg_error
))
695 return pgio
.pg_error
;
699 int nfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
703 ret
= nfs_writepage_locked(page
, wbc
);
704 if (ret
!= AOP_WRITEPAGE_ACTIVATE
)
709 static int nfs_writepages_callback(struct page
*page
, struct writeback_control
*wbc
, void *data
)
713 ret
= nfs_do_writepage(page
, wbc
, data
);
714 if (ret
!= AOP_WRITEPAGE_ACTIVATE
)
719 static void nfs_io_completion_commit(void *inode
)
721 nfs_commit_inode(inode
, 0);
724 int nfs_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
726 struct inode
*inode
= mapping
->host
;
727 struct nfs_pageio_descriptor pgio
;
728 struct nfs_io_completion
*ioc
;
731 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGES
);
733 ioc
= nfs_io_completion_alloc(GFP_KERNEL
);
735 nfs_io_completion_init(ioc
, nfs_io_completion_commit
, inode
);
737 nfs_pageio_init_write(&pgio
, inode
, wb_priority(wbc
), false,
738 &nfs_async_write_completion_ops
);
739 pgio
.pg_io_completion
= ioc
;
740 err
= write_cache_pages(mapping
, wbc
, nfs_writepages_callback
, &pgio
);
742 nfs_pageio_complete(&pgio
);
743 nfs_io_completion_put(ioc
);
748 if (nfs_error_is_fatal(err
))
756 * Insert a write request into an inode
758 static void nfs_inode_add_request(struct inode
*inode
, struct nfs_page
*req
)
760 struct address_space
*mapping
= page_file_mapping(req
->wb_page
);
761 struct nfs_inode
*nfsi
= NFS_I(inode
);
763 WARN_ON_ONCE(req
->wb_this_page
!= req
);
765 /* Lock the request! */
766 nfs_lock_request(req
);
769 * Swap-space should not get truncated. Hence no need to plug the race
770 * with invalidate/truncate.
772 spin_lock(&mapping
->private_lock
);
773 if (!nfs_have_writebacks(inode
) &&
774 NFS_PROTO(inode
)->have_delegation(inode
, FMODE_WRITE
))
775 inode_inc_iversion_raw(inode
);
776 if (likely(!PageSwapCache(req
->wb_page
))) {
777 set_bit(PG_MAPPED
, &req
->wb_flags
);
778 SetPagePrivate(req
->wb_page
);
779 set_page_private(req
->wb_page
, (unsigned long)req
);
781 spin_unlock(&mapping
->private_lock
);
782 atomic_long_inc(&nfsi
->nrequests
);
783 /* this a head request for a page group - mark it as having an
784 * extra reference so sub groups can follow suit.
785 * This flag also informs pgio layer when to bump nrequests when
786 * adding subrequests. */
787 WARN_ON(test_and_set_bit(PG_INODE_REF
, &req
->wb_flags
));
788 kref_get(&req
->wb_kref
);
792 * Remove a write request from an inode
794 static void nfs_inode_remove_request(struct nfs_page
*req
)
796 struct address_space
*mapping
= page_file_mapping(req
->wb_page
);
797 struct inode
*inode
= mapping
->host
;
798 struct nfs_inode
*nfsi
= NFS_I(inode
);
799 struct nfs_page
*head
;
801 if (nfs_page_group_sync_on_bit(req
, PG_REMOVE
)) {
804 spin_lock(&mapping
->private_lock
);
805 if (likely(head
->wb_page
&& !PageSwapCache(head
->wb_page
))) {
806 set_page_private(head
->wb_page
, 0);
807 ClearPagePrivate(head
->wb_page
);
808 clear_bit(PG_MAPPED
, &head
->wb_flags
);
810 spin_unlock(&mapping
->private_lock
);
813 if (test_and_clear_bit(PG_INODE_REF
, &req
->wb_flags
)) {
814 nfs_release_request(req
);
815 atomic_long_dec(&nfsi
->nrequests
);
820 nfs_mark_request_dirty(struct nfs_page
*req
)
823 __set_page_dirty_nobuffers(req
->wb_page
);
827 * nfs_page_search_commits_for_head_request_locked
829 * Search through commit lists on @inode for the head request for @page.
830 * Must be called while holding the inode (which is cinfo) lock.
832 * Returns the head request if found, or NULL if not found.
834 static struct nfs_page
*
835 nfs_page_search_commits_for_head_request_locked(struct nfs_inode
*nfsi
,
838 struct nfs_page
*freq
, *t
;
839 struct nfs_commit_info cinfo
;
840 struct inode
*inode
= &nfsi
->vfs_inode
;
842 nfs_init_cinfo_from_inode(&cinfo
, inode
);
844 /* search through pnfs commit lists */
845 freq
= pnfs_search_commit_reqs(inode
, &cinfo
, page
);
847 return freq
->wb_head
;
849 /* Linearly search the commit list for the correct request */
850 list_for_each_entry_safe(freq
, t
, &cinfo
.mds
->list
, wb_list
) {
851 if (freq
->wb_page
== page
)
852 return freq
->wb_head
;
859 * nfs_request_add_commit_list_locked - add request to a commit list
860 * @req: pointer to a struct nfs_page
861 * @dst: commit list head
862 * @cinfo: holds list lock and accounting info
864 * This sets the PG_CLEAN bit, updates the cinfo count of
865 * number of outstanding requests requiring a commit as well as
868 * The caller must hold NFS_I(cinfo->inode)->commit_mutex, and the
872 nfs_request_add_commit_list_locked(struct nfs_page
*req
, struct list_head
*dst
,
873 struct nfs_commit_info
*cinfo
)
875 set_bit(PG_CLEAN
, &req
->wb_flags
);
876 nfs_list_add_request(req
, dst
);
877 atomic_long_inc(&cinfo
->mds
->ncommit
);
879 EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked
);
882 * nfs_request_add_commit_list - add request to a commit list
883 * @req: pointer to a struct nfs_page
884 * @cinfo: holds list lock and accounting info
886 * This sets the PG_CLEAN bit, updates the cinfo count of
887 * number of outstanding requests requiring a commit as well as
890 * The caller must _not_ hold the cinfo->lock, but must be
891 * holding the nfs_page lock.
894 nfs_request_add_commit_list(struct nfs_page
*req
, struct nfs_commit_info
*cinfo
)
896 mutex_lock(&NFS_I(cinfo
->inode
)->commit_mutex
);
897 nfs_request_add_commit_list_locked(req
, &cinfo
->mds
->list
, cinfo
);
898 mutex_unlock(&NFS_I(cinfo
->inode
)->commit_mutex
);
900 nfs_mark_page_unstable(req
->wb_page
, cinfo
);
902 EXPORT_SYMBOL_GPL(nfs_request_add_commit_list
);
905 * nfs_request_remove_commit_list - Remove request from a commit list
906 * @req: pointer to a nfs_page
907 * @cinfo: holds list lock and accounting info
909 * This clears the PG_CLEAN bit, and updates the cinfo's count of
910 * number of outstanding requests requiring a commit
911 * It does not update the MM page stats.
913 * The caller _must_ hold the cinfo->lock and the nfs_page lock.
916 nfs_request_remove_commit_list(struct nfs_page
*req
,
917 struct nfs_commit_info
*cinfo
)
919 if (!test_and_clear_bit(PG_CLEAN
, &(req
)->wb_flags
))
921 nfs_list_remove_request(req
);
922 atomic_long_dec(&cinfo
->mds
->ncommit
);
924 EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list
);
926 static void nfs_init_cinfo_from_inode(struct nfs_commit_info
*cinfo
,
929 cinfo
->inode
= inode
;
930 cinfo
->mds
= &NFS_I(inode
)->commit_info
;
931 cinfo
->ds
= pnfs_get_ds_info(inode
);
933 cinfo
->completion_ops
= &nfs_commit_completion_ops
;
936 void nfs_init_cinfo(struct nfs_commit_info
*cinfo
,
938 struct nfs_direct_req
*dreq
)
941 nfs_init_cinfo_from_dreq(cinfo
, dreq
);
943 nfs_init_cinfo_from_inode(cinfo
, inode
);
945 EXPORT_SYMBOL_GPL(nfs_init_cinfo
);
948 * Add a request to the inode's commit list.
951 nfs_mark_request_commit(struct nfs_page
*req
, struct pnfs_layout_segment
*lseg
,
952 struct nfs_commit_info
*cinfo
, u32 ds_commit_idx
)
954 if (pnfs_mark_request_commit(req
, lseg
, cinfo
, ds_commit_idx
))
956 nfs_request_add_commit_list(req
, cinfo
);
960 nfs_clear_page_commit(struct page
*page
)
962 dec_node_page_state(page
, NR_UNSTABLE_NFS
);
963 dec_wb_stat(&inode_to_bdi(page_file_mapping(page
)->host
)->wb
,
967 /* Called holding the request lock on @req */
969 nfs_clear_request_commit(struct nfs_page
*req
)
971 if (test_bit(PG_CLEAN
, &req
->wb_flags
)) {
972 struct nfs_open_context
*ctx
= nfs_req_openctx(req
);
973 struct inode
*inode
= d_inode(ctx
->dentry
);
974 struct nfs_commit_info cinfo
;
976 nfs_init_cinfo_from_inode(&cinfo
, inode
);
977 mutex_lock(&NFS_I(inode
)->commit_mutex
);
978 if (!pnfs_clear_request_commit(req
, &cinfo
)) {
979 nfs_request_remove_commit_list(req
, &cinfo
);
981 mutex_unlock(&NFS_I(inode
)->commit_mutex
);
982 nfs_clear_page_commit(req
->wb_page
);
986 int nfs_write_need_commit(struct nfs_pgio_header
*hdr
)
988 if (hdr
->verf
.committed
== NFS_DATA_SYNC
)
989 return hdr
->lseg
== NULL
;
990 return hdr
->verf
.committed
!= NFS_FILE_SYNC
;
993 static void nfs_async_write_init(struct nfs_pgio_header
*hdr
)
995 nfs_io_completion_get(hdr
->io_completion
);
998 static void nfs_write_completion(struct nfs_pgio_header
*hdr
)
1000 struct nfs_commit_info cinfo
;
1001 unsigned long bytes
= 0;
1003 if (test_bit(NFS_IOHDR_REDO
, &hdr
->flags
))
1005 nfs_init_cinfo_from_inode(&cinfo
, hdr
->inode
);
1006 while (!list_empty(&hdr
->pages
)) {
1007 struct nfs_page
*req
= nfs_list_entry(hdr
->pages
.next
);
1009 bytes
+= req
->wb_bytes
;
1010 nfs_list_remove_request(req
);
1011 if (test_bit(NFS_IOHDR_ERROR
, &hdr
->flags
) &&
1012 (hdr
->good_bytes
< bytes
)) {
1013 trace_nfs_comp_error(req
, hdr
->error
);
1014 nfs_mapping_set_error(req
->wb_page
, hdr
->error
);
1017 if (nfs_write_need_commit(hdr
)) {
1018 /* Reset wb_nio, since the write was successful. */
1020 memcpy(&req
->wb_verf
, &hdr
->verf
.verifier
, sizeof(req
->wb_verf
));
1021 nfs_mark_request_commit(req
, hdr
->lseg
, &cinfo
,
1022 hdr
->pgio_mirror_idx
);
1026 nfs_inode_remove_request(req
);
1028 nfs_end_page_writeback(req
);
1029 nfs_release_request(req
);
1032 nfs_io_completion_put(hdr
->io_completion
);
1037 nfs_reqs_to_commit(struct nfs_commit_info
*cinfo
)
1039 return atomic_long_read(&cinfo
->mds
->ncommit
);
1042 /* NFS_I(cinfo->inode)->commit_mutex held by caller */
1044 nfs_scan_commit_list(struct list_head
*src
, struct list_head
*dst
,
1045 struct nfs_commit_info
*cinfo
, int max
)
1047 struct nfs_page
*req
, *tmp
;
1051 list_for_each_entry_safe(req
, tmp
, src
, wb_list
) {
1052 kref_get(&req
->wb_kref
);
1053 if (!nfs_lock_request(req
)) {
1056 /* Prevent deadlock with nfs_lock_and_join_requests */
1057 if (!list_empty(dst
)) {
1058 nfs_release_request(req
);
1061 /* Ensure we make progress to prevent livelock */
1062 mutex_unlock(&NFS_I(cinfo
->inode
)->commit_mutex
);
1063 status
= nfs_wait_on_request(req
);
1064 nfs_release_request(req
);
1065 mutex_lock(&NFS_I(cinfo
->inode
)->commit_mutex
);
1070 nfs_request_remove_commit_list(req
, cinfo
);
1071 clear_bit(PG_COMMIT_TO_DS
, &req
->wb_flags
);
1072 nfs_list_add_request(req
, dst
);
1074 if ((ret
== max
) && !cinfo
->dreq
)
1080 EXPORT_SYMBOL_GPL(nfs_scan_commit_list
);
1083 * nfs_scan_commit - Scan an inode for commit requests
1084 * @inode: NFS inode to scan
1085 * @dst: mds destination list
1086 * @cinfo: mds and ds lists of reqs ready to commit
1088 * Moves requests from the inode's 'commit' request list.
1089 * The requests are *not* checked to ensure that they form a contiguous set.
1092 nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
,
1093 struct nfs_commit_info
*cinfo
)
1097 if (!atomic_long_read(&cinfo
->mds
->ncommit
))
1099 mutex_lock(&NFS_I(cinfo
->inode
)->commit_mutex
);
1100 if (atomic_long_read(&cinfo
->mds
->ncommit
) > 0) {
1101 const int max
= INT_MAX
;
1103 ret
= nfs_scan_commit_list(&cinfo
->mds
->list
, dst
,
1105 ret
+= pnfs_scan_commit_lists(inode
, cinfo
, max
- ret
);
1107 mutex_unlock(&NFS_I(cinfo
->inode
)->commit_mutex
);
1112 * Search for an existing write request, and attempt to update
1113 * it to reflect a new dirty region on a given page.
1115 * If the attempt fails, then the existing request is flushed out
1118 static struct nfs_page
*nfs_try_to_update_request(struct inode
*inode
,
1120 unsigned int offset
,
1123 struct nfs_page
*req
;
1128 end
= offset
+ bytes
;
1130 req
= nfs_lock_and_join_requests(page
);
1131 if (IS_ERR_OR_NULL(req
))
1134 rqend
= req
->wb_offset
+ req
->wb_bytes
;
1136 * Tell the caller to flush out the request if
1137 * the offsets are non-contiguous.
1138 * Note: nfs_flush_incompatible() will already
1139 * have flushed out requests having wrong owners.
1141 if (offset
> rqend
|| end
< req
->wb_offset
)
1144 /* Okay, the request matches. Update the region */
1145 if (offset
< req
->wb_offset
) {
1146 req
->wb_offset
= offset
;
1147 req
->wb_pgbase
= offset
;
1150 req
->wb_bytes
= end
- req
->wb_offset
;
1152 req
->wb_bytes
= rqend
- req
->wb_offset
;
1157 * Note: we mark the request dirty here because
1158 * nfs_lock_and_join_requests() cannot preserve
1159 * commit flags, so we have to replay the write.
1161 nfs_mark_request_dirty(req
);
1162 nfs_unlock_and_release_request(req
);
1163 error
= nfs_wb_page(inode
, page
);
1164 return (error
< 0) ? ERR_PTR(error
) : NULL
;
1168 * Try to update an existing write request, or create one if there is none.
1170 * Note: Should always be called with the Page Lock held to prevent races
1171 * if we have to add a new request. Also assumes that the caller has
1172 * already called nfs_flush_incompatible() if necessary.
1174 static struct nfs_page
* nfs_setup_write_request(struct nfs_open_context
* ctx
,
1175 struct page
*page
, unsigned int offset
, unsigned int bytes
)
1177 struct inode
*inode
= page_file_mapping(page
)->host
;
1178 struct nfs_page
*req
;
1180 req
= nfs_try_to_update_request(inode
, page
, offset
, bytes
);
1183 req
= nfs_create_request(ctx
, page
, offset
, bytes
);
1186 nfs_inode_add_request(inode
, req
);
1191 static int nfs_writepage_setup(struct nfs_open_context
*ctx
, struct page
*page
,
1192 unsigned int offset
, unsigned int count
)
1194 struct nfs_page
*req
;
1196 req
= nfs_setup_write_request(ctx
, page
, offset
, count
);
1198 return PTR_ERR(req
);
1199 /* Update file length */
1200 nfs_grow_file(page
, offset
, count
);
1201 nfs_mark_uptodate(req
);
1202 nfs_mark_request_dirty(req
);
1203 nfs_unlock_and_release_request(req
);
1207 int nfs_flush_incompatible(struct file
*file
, struct page
*page
)
1209 struct nfs_open_context
*ctx
= nfs_file_open_context(file
);
1210 struct nfs_lock_context
*l_ctx
;
1211 struct file_lock_context
*flctx
= file_inode(file
)->i_flctx
;
1212 struct nfs_page
*req
;
1213 int do_flush
, status
;
1215 * Look for a request corresponding to this page. If there
1216 * is one, and it belongs to another file, we flush it out
1217 * before we try to copy anything into the page. Do this
1218 * due to the lack of an ACCESS-type call in NFSv2.
1219 * Also do the same if we find a request from an existing
1223 req
= nfs_page_find_head_request(page
);
1226 l_ctx
= req
->wb_lock_context
;
1227 do_flush
= req
->wb_page
!= page
||
1228 !nfs_match_open_context(nfs_req_openctx(req
), ctx
);
1229 if (l_ctx
&& flctx
&&
1230 !(list_empty_careful(&flctx
->flc_posix
) &&
1231 list_empty_careful(&flctx
->flc_flock
))) {
1232 do_flush
|= l_ctx
->lockowner
!= current
->files
;
1234 nfs_release_request(req
);
1237 status
= nfs_wb_page(page_file_mapping(page
)->host
, page
);
1238 } while (status
== 0);
1243 * Avoid buffered writes when a open context credential's key would
1246 * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
1248 * Return 0 and set a credential flag which triggers the inode to flush
1249 * and performs NFS_FILE_SYNC writes if the key will expired within
1250 * RPC_KEY_EXPIRE_TIMEO.
1253 nfs_key_timeout_notify(struct file
*filp
, struct inode
*inode
)
1255 struct nfs_open_context
*ctx
= nfs_file_open_context(filp
);
1257 if (nfs_ctx_key_to_expire(ctx
, inode
) &&
1259 /* Already expired! */
1265 * Test if the open context credential key is marked to expire soon.
1267 bool nfs_ctx_key_to_expire(struct nfs_open_context
*ctx
, struct inode
*inode
)
1269 struct rpc_auth
*auth
= NFS_SERVER(inode
)->client
->cl_auth
;
1270 struct rpc_cred
*cred
= ctx
->ll_cred
;
1271 struct auth_cred acred
= {
1275 if (cred
&& !cred
->cr_ops
->crmatch(&acred
, cred
, 0)) {
1277 ctx
->ll_cred
= NULL
;
1281 cred
= auth
->au_ops
->lookup_cred(auth
, &acred
, 0);
1282 if (!cred
|| IS_ERR(cred
))
1284 ctx
->ll_cred
= cred
;
1285 return !!(cred
->cr_ops
->crkey_timeout
&&
1286 cred
->cr_ops
->crkey_timeout(cred
));
1290 * If the page cache is marked as unsafe or invalid, then we can't rely on
1291 * the PageUptodate() flag. In this case, we will need to turn off
1292 * write optimisations that depend on the page contents being correct.
1294 static bool nfs_write_pageuptodate(struct page
*page
, struct inode
*inode
)
1296 struct nfs_inode
*nfsi
= NFS_I(inode
);
1298 if (nfs_have_delegated_attributes(inode
))
1300 if (nfsi
->cache_validity
& NFS_INO_REVAL_PAGECACHE
)
1303 if (test_bit(NFS_INO_INVALIDATING
, &nfsi
->flags
))
1306 if (nfsi
->cache_validity
& NFS_INO_INVALID_DATA
)
1308 return PageUptodate(page
) != 0;
1312 is_whole_file_wrlock(struct file_lock
*fl
)
1314 return fl
->fl_start
== 0 && fl
->fl_end
== OFFSET_MAX
&&
1315 fl
->fl_type
== F_WRLCK
;
1318 /* If we know the page is up to date, and we're not using byte range locks (or
1319 * if we have the whole file locked for writing), it may be more efficient to
1320 * extend the write to cover the entire page in order to avoid fragmentation
1323 * If the file is opened for synchronous writes then we can just skip the rest
1326 static int nfs_can_extend_write(struct file
*file
, struct page
*page
, struct inode
*inode
)
1329 struct file_lock_context
*flctx
= inode
->i_flctx
;
1330 struct file_lock
*fl
;
1332 if (file
->f_flags
& O_DSYNC
)
1334 if (!nfs_write_pageuptodate(page
, inode
))
1336 if (NFS_PROTO(inode
)->have_delegation(inode
, FMODE_WRITE
))
1338 if (!flctx
|| (list_empty_careful(&flctx
->flc_flock
) &&
1339 list_empty_careful(&flctx
->flc_posix
)))
1342 /* Check to see if there are whole file write locks */
1344 spin_lock(&flctx
->flc_lock
);
1345 if (!list_empty(&flctx
->flc_posix
)) {
1346 fl
= list_first_entry(&flctx
->flc_posix
, struct file_lock
,
1348 if (is_whole_file_wrlock(fl
))
1350 } else if (!list_empty(&flctx
->flc_flock
)) {
1351 fl
= list_first_entry(&flctx
->flc_flock
, struct file_lock
,
1353 if (fl
->fl_type
== F_WRLCK
)
1356 spin_unlock(&flctx
->flc_lock
);
1361 * Update and possibly write a cached page of an NFS file.
1363 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
1364 * things with a page scheduled for an RPC call (e.g. invalidate it).
1366 int nfs_updatepage(struct file
*file
, struct page
*page
,
1367 unsigned int offset
, unsigned int count
)
1369 struct nfs_open_context
*ctx
= nfs_file_open_context(file
);
1370 struct address_space
*mapping
= page_file_mapping(page
);
1371 struct inode
*inode
= mapping
->host
;
1374 nfs_inc_stats(inode
, NFSIOS_VFSUPDATEPAGE
);
1376 dprintk("NFS: nfs_updatepage(%pD2 %d@%lld)\n",
1377 file
, count
, (long long)(page_file_offset(page
) + offset
));
1382 if (nfs_can_extend_write(file
, page
, inode
)) {
1383 count
= max(count
+ offset
, nfs_page_length(page
));
1387 status
= nfs_writepage_setup(ctx
, page
, offset
, count
);
1389 nfs_set_pageerror(mapping
);
1391 __set_page_dirty_nobuffers(page
);
1393 dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
1394 status
, (long long)i_size_read(inode
));
1398 static int flush_task_priority(int how
)
1400 switch (how
& (FLUSH_HIGHPRI
|FLUSH_LOWPRI
)) {
1402 return RPC_PRIORITY_HIGH
;
1404 return RPC_PRIORITY_LOW
;
1406 return RPC_PRIORITY_NORMAL
;
1409 static void nfs_initiate_write(struct nfs_pgio_header
*hdr
,
1410 struct rpc_message
*msg
,
1411 const struct nfs_rpc_ops
*rpc_ops
,
1412 struct rpc_task_setup
*task_setup_data
, int how
)
1414 int priority
= flush_task_priority(how
);
1416 task_setup_data
->priority
= priority
;
1417 rpc_ops
->write_setup(hdr
, msg
, &task_setup_data
->rpc_client
);
1418 trace_nfs_initiate_write(hdr
);
1421 /* If a nfs_flush_* function fails, it should remove reqs from @head and
1422 * call this on each, which will prepare them to be retried on next
1423 * writeback using standard nfs.
1425 static void nfs_redirty_request(struct nfs_page
*req
)
1427 /* Bump the transmission count */
1429 nfs_mark_request_dirty(req
);
1430 set_bit(NFS_CONTEXT_RESEND_WRITES
, &nfs_req_openctx(req
)->flags
);
1431 nfs_end_page_writeback(req
);
1432 nfs_release_request(req
);
1435 static void nfs_async_write_error(struct list_head
*head
, int error
)
1437 struct nfs_page
*req
;
1439 while (!list_empty(head
)) {
1440 req
= nfs_list_entry(head
->next
);
1441 nfs_list_remove_request(req
);
1442 if (nfs_error_is_fatal(error
))
1443 nfs_write_error(req
, error
);
1445 nfs_redirty_request(req
);
1449 static void nfs_async_write_reschedule_io(struct nfs_pgio_header
*hdr
)
1451 nfs_async_write_error(&hdr
->pages
, 0);
1452 filemap_fdatawrite_range(hdr
->inode
->i_mapping
, hdr
->args
.offset
,
1453 hdr
->args
.offset
+ hdr
->args
.count
- 1);
1456 static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops
= {
1457 .init_hdr
= nfs_async_write_init
,
1458 .error_cleanup
= nfs_async_write_error
,
1459 .completion
= nfs_write_completion
,
1460 .reschedule_io
= nfs_async_write_reschedule_io
,
1463 void nfs_pageio_init_write(struct nfs_pageio_descriptor
*pgio
,
1464 struct inode
*inode
, int ioflags
, bool force_mds
,
1465 const struct nfs_pgio_completion_ops
*compl_ops
)
1467 struct nfs_server
*server
= NFS_SERVER(inode
);
1468 const struct nfs_pageio_ops
*pg_ops
= &nfs_pgio_rw_ops
;
1470 #ifdef CONFIG_NFS_V4_1
1471 if (server
->pnfs_curr_ld
&& !force_mds
)
1472 pg_ops
= server
->pnfs_curr_ld
->pg_write_ops
;
1474 nfs_pageio_init(pgio
, inode
, pg_ops
, compl_ops
, &nfs_rw_write_ops
,
1475 server
->wsize
, ioflags
);
1477 EXPORT_SYMBOL_GPL(nfs_pageio_init_write
);
1479 void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor
*pgio
)
1481 struct nfs_pgio_mirror
*mirror
;
1483 if (pgio
->pg_ops
&& pgio
->pg_ops
->pg_cleanup
)
1484 pgio
->pg_ops
->pg_cleanup(pgio
);
1486 pgio
->pg_ops
= &nfs_pgio_rw_ops
;
1488 nfs_pageio_stop_mirroring(pgio
);
1490 mirror
= &pgio
->pg_mirrors
[0];
1491 mirror
->pg_bsize
= NFS_SERVER(pgio
->pg_inode
)->wsize
;
1493 EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds
);
1496 void nfs_commit_prepare(struct rpc_task
*task
, void *calldata
)
1498 struct nfs_commit_data
*data
= calldata
;
1500 NFS_PROTO(data
->inode
)->commit_rpc_prepare(task
, data
);
1504 * Special version of should_remove_suid() that ignores capabilities.
1506 static int nfs_should_remove_suid(const struct inode
*inode
)
1508 umode_t mode
= inode
->i_mode
;
1511 /* suid always must be killed */
1512 if (unlikely(mode
& S_ISUID
))
1513 kill
= ATTR_KILL_SUID
;
1516 * sgid without any exec bits is just a mandatory locking mark; leave
1517 * it alone. If some exec bits are set, it's a real sgid; kill it.
1519 if (unlikely((mode
& S_ISGID
) && (mode
& S_IXGRP
)))
1520 kill
|= ATTR_KILL_SGID
;
1522 if (unlikely(kill
&& S_ISREG(mode
)))
1528 static void nfs_writeback_check_extend(struct nfs_pgio_header
*hdr
,
1529 struct nfs_fattr
*fattr
)
1531 struct nfs_pgio_args
*argp
= &hdr
->args
;
1532 struct nfs_pgio_res
*resp
= &hdr
->res
;
1533 u64 size
= argp
->offset
+ resp
->count
;
1535 if (!(fattr
->valid
& NFS_ATTR_FATTR_SIZE
))
1537 if (nfs_size_to_loff_t(fattr
->size
) < i_size_read(hdr
->inode
)) {
1538 fattr
->valid
&= ~NFS_ATTR_FATTR_SIZE
;
1541 if (size
!= fattr
->size
)
1543 /* Set attribute barrier */
1544 nfs_fattr_set_barrier(fattr
);
1545 /* ...and update size */
1546 fattr
->valid
|= NFS_ATTR_FATTR_SIZE
;
1549 void nfs_writeback_update_inode(struct nfs_pgio_header
*hdr
)
1551 struct nfs_fattr
*fattr
= &hdr
->fattr
;
1552 struct inode
*inode
= hdr
->inode
;
1554 spin_lock(&inode
->i_lock
);
1555 nfs_writeback_check_extend(hdr
, fattr
);
1556 nfs_post_op_update_inode_force_wcc_locked(inode
, fattr
);
1557 spin_unlock(&inode
->i_lock
);
1559 EXPORT_SYMBOL_GPL(nfs_writeback_update_inode
);
1562 * This function is called when the WRITE call is complete.
1564 static int nfs_writeback_done(struct rpc_task
*task
,
1565 struct nfs_pgio_header
*hdr
,
1566 struct inode
*inode
)
1571 * ->write_done will attempt to use post-op attributes to detect
1572 * conflicting writes by other clients. A strict interpretation
1573 * of close-to-open would allow us to continue caching even if
1574 * another writer had changed the file, but some applications
1575 * depend on tighter cache coherency when writing.
1577 status
= NFS_PROTO(inode
)->write_done(task
, hdr
);
1581 nfs_add_stats(inode
, NFSIOS_SERVERWRITTENBYTES
, hdr
->res
.count
);
1582 trace_nfs_writeback_done(task
, hdr
);
1584 if (hdr
->res
.verf
->committed
< hdr
->args
.stable
&&
1585 task
->tk_status
>= 0) {
1586 /* We tried a write call, but the server did not
1587 * commit data to stable storage even though we
1589 * Note: There is a known bug in Tru64 < 5.0 in which
1590 * the server reports NFS_DATA_SYNC, but performs
1591 * NFS_FILE_SYNC. We therefore implement this checking
1592 * as a dprintk() in order to avoid filling syslog.
1594 static unsigned long complain
;
1596 /* Note this will print the MDS for a DS write */
1597 if (time_before(complain
, jiffies
)) {
1598 dprintk("NFS: faulty NFS server %s:"
1599 " (committed = %d) != (stable = %d)\n",
1600 NFS_SERVER(inode
)->nfs_client
->cl_hostname
,
1601 hdr
->res
.verf
->committed
, hdr
->args
.stable
);
1602 complain
= jiffies
+ 300 * HZ
;
1606 /* Deal with the suid/sgid bit corner case */
1607 if (nfs_should_remove_suid(inode
)) {
1608 spin_lock(&inode
->i_lock
);
1609 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_OTHER
;
1610 spin_unlock(&inode
->i_lock
);
1616 * This function is called when the WRITE call is complete.
1618 static void nfs_writeback_result(struct rpc_task
*task
,
1619 struct nfs_pgio_header
*hdr
)
1621 struct nfs_pgio_args
*argp
= &hdr
->args
;
1622 struct nfs_pgio_res
*resp
= &hdr
->res
;
1624 if (resp
->count
< argp
->count
) {
1625 static unsigned long complain
;
1627 /* This a short write! */
1628 nfs_inc_stats(hdr
->inode
, NFSIOS_SHORTWRITE
);
1630 /* Has the server at least made some progress? */
1631 if (resp
->count
== 0) {
1632 if (time_before(complain
, jiffies
)) {
1634 "NFS: Server wrote zero bytes, expected %u.\n",
1636 complain
= jiffies
+ 300 * HZ
;
1638 nfs_set_pgio_error(hdr
, -EIO
, argp
->offset
);
1639 task
->tk_status
= -EIO
;
1643 /* For non rpc-based layout drivers, retry-through-MDS */
1644 if (!task
->tk_ops
) {
1645 hdr
->pnfs_error
= -EAGAIN
;
1649 /* Was this an NFSv2 write or an NFSv3 stable write? */
1650 if (resp
->verf
->committed
!= NFS_UNSTABLE
) {
1651 /* Resend from where the server left off */
1652 hdr
->mds_offset
+= resp
->count
;
1653 argp
->offset
+= resp
->count
;
1654 argp
->pgbase
+= resp
->count
;
1655 argp
->count
-= resp
->count
;
1657 /* Resend as a stable write in order to avoid
1658 * headaches in the case of a server crash.
1660 argp
->stable
= NFS_FILE_SYNC
;
1663 resp
->verf
->committed
= 0;
1664 rpc_restart_call_prepare(task
);
1668 static int wait_on_commit(struct nfs_mds_commit_info
*cinfo
)
1670 return wait_var_event_killable(&cinfo
->rpcs_out
,
1671 !atomic_read(&cinfo
->rpcs_out
));
1674 static void nfs_commit_begin(struct nfs_mds_commit_info
*cinfo
)
1676 atomic_inc(&cinfo
->rpcs_out
);
1679 static void nfs_commit_end(struct nfs_mds_commit_info
*cinfo
)
1681 if (atomic_dec_and_test(&cinfo
->rpcs_out
))
1682 wake_up_var(&cinfo
->rpcs_out
);
1685 void nfs_commitdata_release(struct nfs_commit_data
*data
)
1687 put_nfs_open_context(data
->context
);
1688 nfs_commit_free(data
);
1690 EXPORT_SYMBOL_GPL(nfs_commitdata_release
);
1692 int nfs_initiate_commit(struct rpc_clnt
*clnt
, struct nfs_commit_data
*data
,
1693 const struct nfs_rpc_ops
*nfs_ops
,
1694 const struct rpc_call_ops
*call_ops
,
1697 struct rpc_task
*task
;
1698 int priority
= flush_task_priority(how
);
1699 struct rpc_message msg
= {
1700 .rpc_argp
= &data
->args
,
1701 .rpc_resp
= &data
->res
,
1702 .rpc_cred
= data
->cred
,
1704 struct rpc_task_setup task_setup_data
= {
1705 .task
= &data
->task
,
1707 .rpc_message
= &msg
,
1708 .callback_ops
= call_ops
,
1709 .callback_data
= data
,
1710 .workqueue
= nfsiod_workqueue
,
1711 .flags
= RPC_TASK_ASYNC
| flags
,
1712 .priority
= priority
,
1714 /* Set up the initial task struct. */
1715 nfs_ops
->commit_setup(data
, &msg
, &task_setup_data
.rpc_client
);
1716 trace_nfs_initiate_commit(data
);
1718 dprintk("NFS: initiated commit call\n");
1720 task
= rpc_run_task(&task_setup_data
);
1722 return PTR_ERR(task
);
1723 if (how
& FLUSH_SYNC
)
1724 rpc_wait_for_completion_task(task
);
1728 EXPORT_SYMBOL_GPL(nfs_initiate_commit
);
1730 static loff_t
nfs_get_lwb(struct list_head
*head
)
1733 struct nfs_page
*req
;
1735 list_for_each_entry(req
, head
, wb_list
)
1736 if (lwb
< (req_offset(req
) + req
->wb_bytes
))
1737 lwb
= req_offset(req
) + req
->wb_bytes
;
1743 * Set up the argument/result storage required for the RPC call.
1745 void nfs_init_commit(struct nfs_commit_data
*data
,
1746 struct list_head
*head
,
1747 struct pnfs_layout_segment
*lseg
,
1748 struct nfs_commit_info
*cinfo
)
1750 struct nfs_page
*first
= nfs_list_entry(head
->next
);
1751 struct nfs_open_context
*ctx
= nfs_req_openctx(first
);
1752 struct inode
*inode
= d_inode(ctx
->dentry
);
1754 /* Set up the RPC argument and reply structs
1755 * NB: take care not to mess about with data->commit et al. */
1757 list_splice_init(head
, &data
->pages
);
1759 data
->inode
= inode
;
1760 data
->cred
= ctx
->cred
;
1761 data
->lseg
= lseg
; /* reference transferred */
1762 /* only set lwb for pnfs commit */
1764 data
->lwb
= nfs_get_lwb(&data
->pages
);
1765 data
->mds_ops
= &nfs_commit_ops
;
1766 data
->completion_ops
= cinfo
->completion_ops
;
1767 data
->dreq
= cinfo
->dreq
;
1769 data
->args
.fh
= NFS_FH(data
->inode
);
1770 /* Note: we always request a commit of the entire inode */
1771 data
->args
.offset
= 0;
1772 data
->args
.count
= 0;
1773 data
->context
= get_nfs_open_context(ctx
);
1774 data
->res
.fattr
= &data
->fattr
;
1775 data
->res
.verf
= &data
->verf
;
1776 nfs_fattr_init(&data
->fattr
);
1778 EXPORT_SYMBOL_GPL(nfs_init_commit
);
1780 void nfs_retry_commit(struct list_head
*page_list
,
1781 struct pnfs_layout_segment
*lseg
,
1782 struct nfs_commit_info
*cinfo
,
1785 struct nfs_page
*req
;
1787 while (!list_empty(page_list
)) {
1788 req
= nfs_list_entry(page_list
->next
);
1789 nfs_list_remove_request(req
);
1790 nfs_mark_request_commit(req
, lseg
, cinfo
, ds_commit_idx
);
1792 nfs_clear_page_commit(req
->wb_page
);
1793 nfs_unlock_and_release_request(req
);
1796 EXPORT_SYMBOL_GPL(nfs_retry_commit
);
1799 nfs_commit_resched_write(struct nfs_commit_info
*cinfo
,
1800 struct nfs_page
*req
)
1802 __set_page_dirty_nobuffers(req
->wb_page
);
1806 * Commit dirty pages
1809 nfs_commit_list(struct inode
*inode
, struct list_head
*head
, int how
,
1810 struct nfs_commit_info
*cinfo
)
1812 struct nfs_commit_data
*data
;
1814 /* another commit raced with us */
1815 if (list_empty(head
))
1818 data
= nfs_commitdata_alloc(true);
1820 /* Set up the argument struct */
1821 nfs_init_commit(data
, head
, NULL
, cinfo
);
1822 atomic_inc(&cinfo
->mds
->rpcs_out
);
1823 return nfs_initiate_commit(NFS_CLIENT(inode
), data
, NFS_PROTO(inode
),
1824 data
->mds_ops
, how
, 0);
1828 * COMMIT call returned
1830 static void nfs_commit_done(struct rpc_task
*task
, void *calldata
)
1832 struct nfs_commit_data
*data
= calldata
;
1834 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1835 task
->tk_pid
, task
->tk_status
);
1837 /* Call the NFS version-specific code */
1838 NFS_PROTO(data
->inode
)->commit_done(task
, data
);
1839 trace_nfs_commit_done(task
, data
);
1842 static void nfs_commit_release_pages(struct nfs_commit_data
*data
)
1844 const struct nfs_writeverf
*verf
= data
->res
.verf
;
1845 struct nfs_page
*req
;
1846 int status
= data
->task
.tk_status
;
1847 struct nfs_commit_info cinfo
;
1848 struct nfs_server
*nfss
;
1850 while (!list_empty(&data
->pages
)) {
1851 req
= nfs_list_entry(data
->pages
.next
);
1852 nfs_list_remove_request(req
);
1854 nfs_clear_page_commit(req
->wb_page
);
1856 dprintk("NFS: commit (%s/%llu %d@%lld)",
1857 nfs_req_openctx(req
)->dentry
->d_sb
->s_id
,
1858 (unsigned long long)NFS_FILEID(d_inode(nfs_req_openctx(req
)->dentry
)),
1860 (long long)req_offset(req
));
1863 trace_nfs_commit_error(req
, status
);
1864 nfs_mapping_set_error(req
->wb_page
, status
);
1865 nfs_inode_remove_request(req
);
1867 dprintk_cont(", error = %d\n", status
);
1871 /* Okay, COMMIT succeeded, apparently. Check the verifier
1872 * returned by the server against all stored verfs. */
1873 if (verf
->committed
> NFS_UNSTABLE
&&
1874 !nfs_write_verifier_cmp(&req
->wb_verf
, &verf
->verifier
)) {
1875 /* We have a match */
1877 nfs_inode_remove_request(req
);
1878 dprintk_cont(" OK\n");
1881 /* We have a mismatch. Write the page again */
1882 dprintk_cont(" mismatch\n");
1883 nfs_mark_request_dirty(req
);
1884 set_bit(NFS_CONTEXT_RESEND_WRITES
, &nfs_req_openctx(req
)->flags
);
1886 nfs_unlock_and_release_request(req
);
1887 /* Latency breaker */
1890 nfss
= NFS_SERVER(data
->inode
);
1891 if (atomic_long_read(&nfss
->writeback
) < NFS_CONGESTION_OFF_THRESH
)
1892 clear_bdi_congested(inode_to_bdi(data
->inode
), BLK_RW_ASYNC
);
1894 nfs_init_cinfo(&cinfo
, data
->inode
, data
->dreq
);
1895 nfs_commit_end(cinfo
.mds
);
1898 static void nfs_commit_release(void *calldata
)
1900 struct nfs_commit_data
*data
= calldata
;
1902 data
->completion_ops
->completion(data
);
1903 nfs_commitdata_release(calldata
);
1906 static const struct rpc_call_ops nfs_commit_ops
= {
1907 .rpc_call_prepare
= nfs_commit_prepare
,
1908 .rpc_call_done
= nfs_commit_done
,
1909 .rpc_release
= nfs_commit_release
,
1912 static const struct nfs_commit_completion_ops nfs_commit_completion_ops
= {
1913 .completion
= nfs_commit_release_pages
,
1914 .resched_write
= nfs_commit_resched_write
,
1917 int nfs_generic_commit_list(struct inode
*inode
, struct list_head
*head
,
1918 int how
, struct nfs_commit_info
*cinfo
)
1922 status
= pnfs_commit_list(inode
, head
, how
, cinfo
);
1923 if (status
== PNFS_NOT_ATTEMPTED
)
1924 status
= nfs_commit_list(inode
, head
, how
, cinfo
);
1928 static int __nfs_commit_inode(struct inode
*inode
, int how
,
1929 struct writeback_control
*wbc
)
1932 struct nfs_commit_info cinfo
;
1933 int may_wait
= how
& FLUSH_SYNC
;
1936 nfs_init_cinfo_from_inode(&cinfo
, inode
);
1937 nfs_commit_begin(cinfo
.mds
);
1939 ret
= nscan
= nfs_scan_commit(inode
, &head
, &cinfo
);
1942 ret
= nfs_generic_commit_list(inode
, &head
, how
, &cinfo
);
1946 if (wbc
&& wbc
->sync_mode
== WB_SYNC_NONE
) {
1947 if (nscan
< wbc
->nr_to_write
)
1948 wbc
->nr_to_write
-= nscan
;
1950 wbc
->nr_to_write
= 0;
1952 if (nscan
< INT_MAX
)
1956 nfs_commit_end(cinfo
.mds
);
1957 if (ret
|| !may_wait
)
1959 return wait_on_commit(cinfo
.mds
);
1962 int nfs_commit_inode(struct inode
*inode
, int how
)
1964 return __nfs_commit_inode(inode
, how
, NULL
);
1966 EXPORT_SYMBOL_GPL(nfs_commit_inode
);
1968 int nfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1970 struct nfs_inode
*nfsi
= NFS_I(inode
);
1971 int flags
= FLUSH_SYNC
;
1974 if (wbc
->sync_mode
== WB_SYNC_NONE
) {
1975 /* no commits means nothing needs to be done */
1976 if (!atomic_long_read(&nfsi
->commit_info
.ncommit
))
1977 goto check_requests_outstanding
;
1979 /* Don't commit yet if this is a non-blocking flush and there
1980 * are a lot of outstanding writes for this mapping.
1982 if (mapping_tagged(inode
->i_mapping
, PAGECACHE_TAG_WRITEBACK
))
1983 goto out_mark_dirty
;
1985 /* don't wait for the COMMIT response */
1989 ret
= __nfs_commit_inode(inode
, flags
, wbc
);
1991 if (flags
& FLUSH_SYNC
)
1993 } else if (atomic_long_read(&nfsi
->commit_info
.ncommit
))
1994 goto out_mark_dirty
;
1996 check_requests_outstanding
:
1997 if (!atomic_read(&nfsi
->commit_info
.rpcs_out
))
2000 __mark_inode_dirty(inode
, I_DIRTY_DATASYNC
);
2003 EXPORT_SYMBOL_GPL(nfs_write_inode
);
2006 * Wrapper for filemap_write_and_wait_range()
2008 * Needed for pNFS in order to ensure data becomes visible to the
2011 int nfs_filemap_write_and_wait_range(struct address_space
*mapping
,
2012 loff_t lstart
, loff_t lend
)
2016 ret
= filemap_write_and_wait_range(mapping
, lstart
, lend
);
2018 ret
= pnfs_sync_inode(mapping
->host
, true);
2021 EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range
);
2024 * flush the inode to disk.
2026 int nfs_wb_all(struct inode
*inode
)
2030 trace_nfs_writeback_inode_enter(inode
);
2032 ret
= filemap_write_and_wait(inode
->i_mapping
);
2035 ret
= nfs_commit_inode(inode
, FLUSH_SYNC
);
2038 pnfs_sync_inode(inode
, true);
2042 trace_nfs_writeback_inode_exit(inode
, ret
);
2045 EXPORT_SYMBOL_GPL(nfs_wb_all
);
2047 int nfs_wb_page_cancel(struct inode
*inode
, struct page
*page
)
2049 struct nfs_page
*req
;
2052 wait_on_page_writeback(page
);
2054 /* blocking call to cancel all requests and join to a single (head)
2056 req
= nfs_lock_and_join_requests(page
);
2061 /* all requests from this page have been cancelled by
2062 * nfs_lock_and_join_requests, so just remove the head
2063 * request from the inode / page_private pointer and
2065 nfs_inode_remove_request(req
);
2066 nfs_unlock_and_release_request(req
);
2073 * Write back all requests on one page - we do this before reading it.
2075 int nfs_wb_page(struct inode
*inode
, struct page
*page
)
2077 loff_t range_start
= page_file_offset(page
);
2078 loff_t range_end
= range_start
+ (loff_t
)(PAGE_SIZE
- 1);
2079 struct writeback_control wbc
= {
2080 .sync_mode
= WB_SYNC_ALL
,
2082 .range_start
= range_start
,
2083 .range_end
= range_end
,
2087 trace_nfs_writeback_page_enter(inode
);
2090 wait_on_page_writeback(page
);
2091 if (clear_page_dirty_for_io(page
)) {
2092 ret
= nfs_writepage_locked(page
, &wbc
);
2098 if (!PagePrivate(page
))
2100 ret
= nfs_commit_inode(inode
, FLUSH_SYNC
);
2105 trace_nfs_writeback_page_exit(inode
, ret
);
2109 #ifdef CONFIG_MIGRATION
2110 int nfs_migrate_page(struct address_space
*mapping
, struct page
*newpage
,
2111 struct page
*page
, enum migrate_mode mode
)
2114 * If PagePrivate is set, then the page is currently associated with
2115 * an in-progress read or write request. Don't try to migrate it.
2117 * FIXME: we could do this in principle, but we'll need a way to ensure
2118 * that we can safely release the inode reference while holding
2121 if (PagePrivate(page
))
2124 if (!nfs_fscache_release_page(page
, GFP_KERNEL
))
2127 return migrate_page(mapping
, newpage
, page
, mode
);
2131 int __init
nfs_init_writepagecache(void)
2133 nfs_wdata_cachep
= kmem_cache_create("nfs_write_data",
2134 sizeof(struct nfs_pgio_header
),
2135 0, SLAB_HWCACHE_ALIGN
,
2137 if (nfs_wdata_cachep
== NULL
)
2140 nfs_wdata_mempool
= mempool_create_slab_pool(MIN_POOL_WRITE
,
2142 if (nfs_wdata_mempool
== NULL
)
2143 goto out_destroy_write_cache
;
2145 nfs_cdata_cachep
= kmem_cache_create("nfs_commit_data",
2146 sizeof(struct nfs_commit_data
),
2147 0, SLAB_HWCACHE_ALIGN
,
2149 if (nfs_cdata_cachep
== NULL
)
2150 goto out_destroy_write_mempool
;
2152 nfs_commit_mempool
= mempool_create_slab_pool(MIN_POOL_COMMIT
,
2154 if (nfs_commit_mempool
== NULL
)
2155 goto out_destroy_commit_cache
;
2158 * NFS congestion size, scale with available memory.
2170 * This allows larger machines to have larger/more transfers.
2171 * Limit the default to 256M
2173 nfs_congestion_kb
= (16*int_sqrt(totalram_pages())) << (PAGE_SHIFT
-10);
2174 if (nfs_congestion_kb
> 256*1024)
2175 nfs_congestion_kb
= 256*1024;
2179 out_destroy_commit_cache
:
2180 kmem_cache_destroy(nfs_cdata_cachep
);
2181 out_destroy_write_mempool
:
2182 mempool_destroy(nfs_wdata_mempool
);
2183 out_destroy_write_cache
:
2184 kmem_cache_destroy(nfs_wdata_cachep
);
2188 void nfs_destroy_writepagecache(void)
2190 mempool_destroy(nfs_commit_mempool
);
2191 kmem_cache_destroy(nfs_cdata_cachep
);
2192 mempool_destroy(nfs_wdata_mempool
);
2193 kmem_cache_destroy(nfs_wdata_cachep
);
2196 static const struct nfs_rw_ops nfs_rw_write_ops
= {
2197 .rw_alloc_header
= nfs_writehdr_alloc
,
2198 .rw_free_header
= nfs_writehdr_free
,
2199 .rw_done
= nfs_writeback_done
,
2200 .rw_result
= nfs_writeback_result
,
2201 .rw_initiate
= nfs_initiate_write
,