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
;
448 if (test_and_clear_bit(PG_INODE_REF
, &subreq
->wb_flags
)) {
449 nfs_release_request(subreq
);
450 atomic_long_dec(&NFS_I(inode
)->nrequests
);
453 /* subreq is now totally disconnected from page group or any
454 * write / commit lists. last chance to wake any waiters */
455 nfs_unlock_and_release_request(subreq
);
460 * nfs_lock_and_join_requests - join all subreqs to the head req and return
461 * a locked reference, cancelling any pending
462 * operations for this page.
464 * @page - the page used to lookup the "page group" of nfs_page structures
466 * This function joins all sub requests to the head request by first
467 * locking all requests in the group, cancelling any pending operations
468 * and finally updating the head request to cover the whole range covered by
469 * the (former) group. All subrequests are removed from any write or commit
470 * lists, unlinked from the group and destroyed.
472 * Returns a locked, referenced pointer to the head request - which after
473 * this call is guaranteed to be the only request associated with the page.
474 * Returns NULL if no requests are found for @page, or a ERR_PTR if an
475 * error was encountered.
477 static struct nfs_page
*
478 nfs_lock_and_join_requests(struct page
*page
)
480 struct inode
*inode
= page_file_mapping(page
)->host
;
481 struct nfs_page
*head
, *subreq
;
482 struct nfs_page
*destroy_list
= NULL
;
483 unsigned int total_bytes
;
488 * A reference is taken only on the head request which acts as a
489 * reference to the whole page group - the group will not be destroyed
490 * until the head reference is released.
492 head
= nfs_page_find_head_request(page
);
496 /* lock the page head first in order to avoid an ABBA inefficiency */
497 if (!nfs_lock_request(head
)) {
498 ret
= nfs_wait_on_request(head
);
499 nfs_release_request(head
);
505 /* Ensure that nobody removed the request before we locked it */
506 if (head
!= nfs_page_private_request(page
) && !PageSwapCache(page
)) {
507 nfs_unlock_and_release_request(head
);
511 ret
= nfs_page_group_lock(head
);
513 goto release_request
;
515 /* lock each request in the page group */
516 total_bytes
= head
->wb_bytes
;
517 for (subreq
= head
->wb_this_page
; subreq
!= head
;
518 subreq
= subreq
->wb_this_page
) {
520 if (!kref_get_unless_zero(&subreq
->wb_kref
)) {
521 if (subreq
->wb_offset
== head
->wb_offset
+ total_bytes
)
522 total_bytes
+= subreq
->wb_bytes
;
526 while (!nfs_lock_request(subreq
)) {
528 * Unlock page to allow nfs_page_group_sync_on_bit()
531 nfs_page_group_unlock(head
);
532 ret
= nfs_wait_on_request(subreq
);
534 ret
= nfs_page_group_lock(head
);
536 nfs_unroll_locks(inode
, head
, subreq
);
537 nfs_release_request(subreq
);
538 goto release_request
;
542 * Subrequests are always contiguous, non overlapping
543 * and in order - but may be repeated (mirrored writes).
545 if (subreq
->wb_offset
== (head
->wb_offset
+ total_bytes
)) {
546 /* keep track of how many bytes this group covers */
547 total_bytes
+= subreq
->wb_bytes
;
548 } else if (WARN_ON_ONCE(subreq
->wb_offset
< head
->wb_offset
||
549 ((subreq
->wb_offset
+ subreq
->wb_bytes
) >
550 (head
->wb_offset
+ total_bytes
)))) {
551 nfs_page_group_unlock(head
);
552 nfs_unroll_locks(inode
, head
, subreq
);
553 nfs_unlock_and_release_request(subreq
);
555 goto release_request
;
559 /* Now that all requests are locked, make sure they aren't on any list.
560 * Commit list removal accounting is done after locks are dropped */
563 nfs_clear_request_commit(subreq
);
564 subreq
= subreq
->wb_this_page
;
565 } while (subreq
!= head
);
567 /* unlink subrequests from head, destroy them later */
568 if (head
->wb_this_page
!= head
) {
569 /* destroy list will be terminated by head */
570 destroy_list
= head
->wb_this_page
;
571 head
->wb_this_page
= head
;
573 /* change head request to cover whole range that
574 * the former page group covered */
575 head
->wb_bytes
= total_bytes
;
578 /* Postpone destruction of this request */
579 if (test_and_clear_bit(PG_REMOVE
, &head
->wb_flags
)) {
580 set_bit(PG_INODE_REF
, &head
->wb_flags
);
581 kref_get(&head
->wb_kref
);
582 atomic_long_inc(&NFS_I(inode
)->nrequests
);
585 nfs_page_group_unlock(head
);
587 nfs_destroy_unlinked_subrequests(destroy_list
, head
, inode
);
589 /* Did we lose a race with nfs_inode_remove_request()? */
590 if (!(PagePrivate(page
) || PageSwapCache(page
))) {
591 nfs_unlock_and_release_request(head
);
595 /* still holds ref on head from nfs_page_find_head_request
596 * and still has lock on head from lock loop */
600 nfs_unlock_and_release_request(head
);
604 static void nfs_write_error(struct nfs_page
*req
, int error
)
606 trace_nfs_write_error(req
, error
);
607 nfs_mapping_set_error(req
->wb_page
, error
);
608 nfs_inode_remove_request(req
);
609 nfs_end_page_writeback(req
);
610 nfs_release_request(req
);
614 * Find an associated nfs write request, and prepare to flush it out
615 * May return an error if the user signalled nfs_wait_on_request().
617 static int nfs_page_async_flush(struct nfs_pageio_descriptor
*pgio
,
620 struct nfs_page
*req
;
623 req
= nfs_lock_and_join_requests(page
);
630 nfs_set_page_writeback(page
);
631 WARN_ON_ONCE(test_bit(PG_CLEAN
, &req
->wb_flags
));
633 /* If there is a fatal error that covers this write, just exit */
634 ret
= pgio
->pg_error
;
635 if (nfs_error_is_fatal_on_server(ret
))
639 if (!nfs_pageio_add_request(pgio
, req
)) {
640 ret
= pgio
->pg_error
;
642 * Remove the problematic req upon fatal errors on the server
644 if (nfs_error_is_fatal(ret
)) {
645 if (nfs_error_is_fatal_on_server(ret
))
649 nfs_redirty_request(req
);
652 nfs_add_stats(page_file_mapping(page
)->host
,
653 NFSIOS_WRITEPAGES
, 1);
657 nfs_write_error(req
, ret
);
661 static int nfs_do_writepage(struct page
*page
, struct writeback_control
*wbc
,
662 struct nfs_pageio_descriptor
*pgio
)
666 nfs_pageio_cond_complete(pgio
, page_index(page
));
667 ret
= nfs_page_async_flush(pgio
, page
);
668 if (ret
== -EAGAIN
) {
669 redirty_page_for_writepage(wbc
, page
);
670 ret
= AOP_WRITEPAGE_ACTIVATE
;
676 * Write an mmapped page to the server.
678 static int nfs_writepage_locked(struct page
*page
,
679 struct writeback_control
*wbc
)
681 struct nfs_pageio_descriptor pgio
;
682 struct inode
*inode
= page_file_mapping(page
)->host
;
685 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGE
);
686 nfs_pageio_init_write(&pgio
, inode
, 0,
687 false, &nfs_async_write_completion_ops
);
688 err
= nfs_do_writepage(page
, wbc
, &pgio
);
690 nfs_pageio_complete(&pgio
);
693 if (nfs_error_is_fatal(pgio
.pg_error
))
694 return pgio
.pg_error
;
698 int nfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
702 ret
= nfs_writepage_locked(page
, wbc
);
703 if (ret
!= AOP_WRITEPAGE_ACTIVATE
)
708 static int nfs_writepages_callback(struct page
*page
, struct writeback_control
*wbc
, void *data
)
712 ret
= nfs_do_writepage(page
, wbc
, data
);
713 if (ret
!= AOP_WRITEPAGE_ACTIVATE
)
718 static void nfs_io_completion_commit(void *inode
)
720 nfs_commit_inode(inode
, 0);
723 int nfs_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
725 struct inode
*inode
= mapping
->host
;
726 struct nfs_pageio_descriptor pgio
;
727 struct nfs_io_completion
*ioc
;
730 nfs_inc_stats(inode
, NFSIOS_VFSWRITEPAGES
);
732 ioc
= nfs_io_completion_alloc(GFP_KERNEL
);
734 nfs_io_completion_init(ioc
, nfs_io_completion_commit
, inode
);
736 nfs_pageio_init_write(&pgio
, inode
, wb_priority(wbc
), false,
737 &nfs_async_write_completion_ops
);
738 pgio
.pg_io_completion
= ioc
;
739 err
= write_cache_pages(mapping
, wbc
, nfs_writepages_callback
, &pgio
);
741 nfs_pageio_complete(&pgio
);
742 nfs_io_completion_put(ioc
);
747 if (nfs_error_is_fatal(err
))
755 * Insert a write request into an inode
757 static void nfs_inode_add_request(struct inode
*inode
, struct nfs_page
*req
)
759 struct address_space
*mapping
= page_file_mapping(req
->wb_page
);
760 struct nfs_inode
*nfsi
= NFS_I(inode
);
762 WARN_ON_ONCE(req
->wb_this_page
!= req
);
764 /* Lock the request! */
765 nfs_lock_request(req
);
768 * Swap-space should not get truncated. Hence no need to plug the race
769 * with invalidate/truncate.
771 spin_lock(&mapping
->private_lock
);
772 if (!nfs_have_writebacks(inode
) &&
773 NFS_PROTO(inode
)->have_delegation(inode
, FMODE_WRITE
))
774 inode_inc_iversion_raw(inode
);
775 if (likely(!PageSwapCache(req
->wb_page
))) {
776 set_bit(PG_MAPPED
, &req
->wb_flags
);
777 SetPagePrivate(req
->wb_page
);
778 set_page_private(req
->wb_page
, (unsigned long)req
);
780 spin_unlock(&mapping
->private_lock
);
781 atomic_long_inc(&nfsi
->nrequests
);
782 /* this a head request for a page group - mark it as having an
783 * extra reference so sub groups can follow suit.
784 * This flag also informs pgio layer when to bump nrequests when
785 * adding subrequests. */
786 WARN_ON(test_and_set_bit(PG_INODE_REF
, &req
->wb_flags
));
787 kref_get(&req
->wb_kref
);
791 * Remove a write request from an inode
793 static void nfs_inode_remove_request(struct nfs_page
*req
)
795 struct address_space
*mapping
= page_file_mapping(req
->wb_page
);
796 struct inode
*inode
= mapping
->host
;
797 struct nfs_inode
*nfsi
= NFS_I(inode
);
798 struct nfs_page
*head
;
800 if (nfs_page_group_sync_on_bit(req
, PG_REMOVE
)) {
803 spin_lock(&mapping
->private_lock
);
804 if (likely(head
->wb_page
&& !PageSwapCache(head
->wb_page
))) {
805 set_page_private(head
->wb_page
, 0);
806 ClearPagePrivate(head
->wb_page
);
807 clear_bit(PG_MAPPED
, &head
->wb_flags
);
809 spin_unlock(&mapping
->private_lock
);
812 if (test_and_clear_bit(PG_INODE_REF
, &req
->wb_flags
)) {
813 nfs_release_request(req
);
814 atomic_long_dec(&nfsi
->nrequests
);
819 nfs_mark_request_dirty(struct nfs_page
*req
)
822 __set_page_dirty_nobuffers(req
->wb_page
);
826 * nfs_page_search_commits_for_head_request_locked
828 * Search through commit lists on @inode for the head request for @page.
829 * Must be called while holding the inode (which is cinfo) lock.
831 * Returns the head request if found, or NULL if not found.
833 static struct nfs_page
*
834 nfs_page_search_commits_for_head_request_locked(struct nfs_inode
*nfsi
,
837 struct nfs_page
*freq
, *t
;
838 struct nfs_commit_info cinfo
;
839 struct inode
*inode
= &nfsi
->vfs_inode
;
841 nfs_init_cinfo_from_inode(&cinfo
, inode
);
843 /* search through pnfs commit lists */
844 freq
= pnfs_search_commit_reqs(inode
, &cinfo
, page
);
846 return freq
->wb_head
;
848 /* Linearly search the commit list for the correct request */
849 list_for_each_entry_safe(freq
, t
, &cinfo
.mds
->list
, wb_list
) {
850 if (freq
->wb_page
== page
)
851 return freq
->wb_head
;
858 * nfs_request_add_commit_list_locked - add request to a commit list
859 * @req: pointer to a struct nfs_page
860 * @dst: commit list head
861 * @cinfo: holds list lock and accounting info
863 * This sets the PG_CLEAN bit, updates the cinfo count of
864 * number of outstanding requests requiring a commit as well as
867 * The caller must hold NFS_I(cinfo->inode)->commit_mutex, and the
871 nfs_request_add_commit_list_locked(struct nfs_page
*req
, struct list_head
*dst
,
872 struct nfs_commit_info
*cinfo
)
874 set_bit(PG_CLEAN
, &req
->wb_flags
);
875 nfs_list_add_request(req
, dst
);
876 atomic_long_inc(&cinfo
->mds
->ncommit
);
878 EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked
);
881 * nfs_request_add_commit_list - add request to a commit list
882 * @req: pointer to a struct nfs_page
883 * @cinfo: holds list lock and accounting info
885 * This sets the PG_CLEAN bit, updates the cinfo count of
886 * number of outstanding requests requiring a commit as well as
889 * The caller must _not_ hold the cinfo->lock, but must be
890 * holding the nfs_page lock.
893 nfs_request_add_commit_list(struct nfs_page
*req
, struct nfs_commit_info
*cinfo
)
895 mutex_lock(&NFS_I(cinfo
->inode
)->commit_mutex
);
896 nfs_request_add_commit_list_locked(req
, &cinfo
->mds
->list
, cinfo
);
897 mutex_unlock(&NFS_I(cinfo
->inode
)->commit_mutex
);
899 nfs_mark_page_unstable(req
->wb_page
, cinfo
);
901 EXPORT_SYMBOL_GPL(nfs_request_add_commit_list
);
904 * nfs_request_remove_commit_list - Remove request from a commit list
905 * @req: pointer to a nfs_page
906 * @cinfo: holds list lock and accounting info
908 * This clears the PG_CLEAN bit, and updates the cinfo's count of
909 * number of outstanding requests requiring a commit
910 * It does not update the MM page stats.
912 * The caller _must_ hold the cinfo->lock and the nfs_page lock.
915 nfs_request_remove_commit_list(struct nfs_page
*req
,
916 struct nfs_commit_info
*cinfo
)
918 if (!test_and_clear_bit(PG_CLEAN
, &(req
)->wb_flags
))
920 nfs_list_remove_request(req
);
921 atomic_long_dec(&cinfo
->mds
->ncommit
);
923 EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list
);
925 static void nfs_init_cinfo_from_inode(struct nfs_commit_info
*cinfo
,
928 cinfo
->inode
= inode
;
929 cinfo
->mds
= &NFS_I(inode
)->commit_info
;
930 cinfo
->ds
= pnfs_get_ds_info(inode
);
932 cinfo
->completion_ops
= &nfs_commit_completion_ops
;
935 void nfs_init_cinfo(struct nfs_commit_info
*cinfo
,
937 struct nfs_direct_req
*dreq
)
940 nfs_init_cinfo_from_dreq(cinfo
, dreq
);
942 nfs_init_cinfo_from_inode(cinfo
, inode
);
944 EXPORT_SYMBOL_GPL(nfs_init_cinfo
);
947 * Add a request to the inode's commit list.
950 nfs_mark_request_commit(struct nfs_page
*req
, struct pnfs_layout_segment
*lseg
,
951 struct nfs_commit_info
*cinfo
, u32 ds_commit_idx
)
953 if (pnfs_mark_request_commit(req
, lseg
, cinfo
, ds_commit_idx
))
955 nfs_request_add_commit_list(req
, cinfo
);
959 nfs_clear_page_commit(struct page
*page
)
961 dec_node_page_state(page
, NR_UNSTABLE_NFS
);
962 dec_wb_stat(&inode_to_bdi(page_file_mapping(page
)->host
)->wb
,
966 /* Called holding the request lock on @req */
968 nfs_clear_request_commit(struct nfs_page
*req
)
970 if (test_bit(PG_CLEAN
, &req
->wb_flags
)) {
971 struct nfs_open_context
*ctx
= nfs_req_openctx(req
);
972 struct inode
*inode
= d_inode(ctx
->dentry
);
973 struct nfs_commit_info cinfo
;
975 nfs_init_cinfo_from_inode(&cinfo
, inode
);
976 mutex_lock(&NFS_I(inode
)->commit_mutex
);
977 if (!pnfs_clear_request_commit(req
, &cinfo
)) {
978 nfs_request_remove_commit_list(req
, &cinfo
);
980 mutex_unlock(&NFS_I(inode
)->commit_mutex
);
981 nfs_clear_page_commit(req
->wb_page
);
985 int nfs_write_need_commit(struct nfs_pgio_header
*hdr
)
987 if (hdr
->verf
.committed
== NFS_DATA_SYNC
)
988 return hdr
->lseg
== NULL
;
989 return hdr
->verf
.committed
!= NFS_FILE_SYNC
;
992 static void nfs_async_write_init(struct nfs_pgio_header
*hdr
)
994 nfs_io_completion_get(hdr
->io_completion
);
997 static void nfs_write_completion(struct nfs_pgio_header
*hdr
)
999 struct nfs_commit_info cinfo
;
1000 unsigned long bytes
= 0;
1002 if (test_bit(NFS_IOHDR_REDO
, &hdr
->flags
))
1004 nfs_init_cinfo_from_inode(&cinfo
, hdr
->inode
);
1005 while (!list_empty(&hdr
->pages
)) {
1006 struct nfs_page
*req
= nfs_list_entry(hdr
->pages
.next
);
1008 bytes
+= req
->wb_bytes
;
1009 nfs_list_remove_request(req
);
1010 if (test_bit(NFS_IOHDR_ERROR
, &hdr
->flags
) &&
1011 (hdr
->good_bytes
< bytes
)) {
1012 trace_nfs_comp_error(req
, hdr
->error
);
1013 nfs_mapping_set_error(req
->wb_page
, hdr
->error
);
1016 if (nfs_write_need_commit(hdr
)) {
1017 /* Reset wb_nio, since the write was successful. */
1019 memcpy(&req
->wb_verf
, &hdr
->verf
.verifier
, sizeof(req
->wb_verf
));
1020 nfs_mark_request_commit(req
, hdr
->lseg
, &cinfo
,
1021 hdr
->pgio_mirror_idx
);
1025 nfs_inode_remove_request(req
);
1027 nfs_end_page_writeback(req
);
1028 nfs_release_request(req
);
1031 nfs_io_completion_put(hdr
->io_completion
);
1036 nfs_reqs_to_commit(struct nfs_commit_info
*cinfo
)
1038 return atomic_long_read(&cinfo
->mds
->ncommit
);
1041 /* NFS_I(cinfo->inode)->commit_mutex held by caller */
1043 nfs_scan_commit_list(struct list_head
*src
, struct list_head
*dst
,
1044 struct nfs_commit_info
*cinfo
, int max
)
1046 struct nfs_page
*req
, *tmp
;
1050 list_for_each_entry_safe(req
, tmp
, src
, wb_list
) {
1051 kref_get(&req
->wb_kref
);
1052 if (!nfs_lock_request(req
)) {
1055 /* Prevent deadlock with nfs_lock_and_join_requests */
1056 if (!list_empty(dst
)) {
1057 nfs_release_request(req
);
1060 /* Ensure we make progress to prevent livelock */
1061 mutex_unlock(&NFS_I(cinfo
->inode
)->commit_mutex
);
1062 status
= nfs_wait_on_request(req
);
1063 nfs_release_request(req
);
1064 mutex_lock(&NFS_I(cinfo
->inode
)->commit_mutex
);
1069 nfs_request_remove_commit_list(req
, cinfo
);
1070 clear_bit(PG_COMMIT_TO_DS
, &req
->wb_flags
);
1071 nfs_list_add_request(req
, dst
);
1073 if ((ret
== max
) && !cinfo
->dreq
)
1079 EXPORT_SYMBOL_GPL(nfs_scan_commit_list
);
1082 * nfs_scan_commit - Scan an inode for commit requests
1083 * @inode: NFS inode to scan
1084 * @dst: mds destination list
1085 * @cinfo: mds and ds lists of reqs ready to commit
1087 * Moves requests from the inode's 'commit' request list.
1088 * The requests are *not* checked to ensure that they form a contiguous set.
1091 nfs_scan_commit(struct inode
*inode
, struct list_head
*dst
,
1092 struct nfs_commit_info
*cinfo
)
1096 if (!atomic_long_read(&cinfo
->mds
->ncommit
))
1098 mutex_lock(&NFS_I(cinfo
->inode
)->commit_mutex
);
1099 if (atomic_long_read(&cinfo
->mds
->ncommit
) > 0) {
1100 const int max
= INT_MAX
;
1102 ret
= nfs_scan_commit_list(&cinfo
->mds
->list
, dst
,
1104 ret
+= pnfs_scan_commit_lists(inode
, cinfo
, max
- ret
);
1106 mutex_unlock(&NFS_I(cinfo
->inode
)->commit_mutex
);
1111 * Search for an existing write request, and attempt to update
1112 * it to reflect a new dirty region on a given page.
1114 * If the attempt fails, then the existing request is flushed out
1117 static struct nfs_page
*nfs_try_to_update_request(struct inode
*inode
,
1119 unsigned int offset
,
1122 struct nfs_page
*req
;
1127 end
= offset
+ bytes
;
1129 req
= nfs_lock_and_join_requests(page
);
1130 if (IS_ERR_OR_NULL(req
))
1133 rqend
= req
->wb_offset
+ req
->wb_bytes
;
1135 * Tell the caller to flush out the request if
1136 * the offsets are non-contiguous.
1137 * Note: nfs_flush_incompatible() will already
1138 * have flushed out requests having wrong owners.
1140 if (offset
> rqend
|| end
< req
->wb_offset
)
1143 /* Okay, the request matches. Update the region */
1144 if (offset
< req
->wb_offset
) {
1145 req
->wb_offset
= offset
;
1146 req
->wb_pgbase
= offset
;
1149 req
->wb_bytes
= end
- req
->wb_offset
;
1151 req
->wb_bytes
= rqend
- req
->wb_offset
;
1156 * Note: we mark the request dirty here because
1157 * nfs_lock_and_join_requests() cannot preserve
1158 * commit flags, so we have to replay the write.
1160 nfs_mark_request_dirty(req
);
1161 nfs_unlock_and_release_request(req
);
1162 error
= nfs_wb_page(inode
, page
);
1163 return (error
< 0) ? ERR_PTR(error
) : NULL
;
1167 * Try to update an existing write request, or create one if there is none.
1169 * Note: Should always be called with the Page Lock held to prevent races
1170 * if we have to add a new request. Also assumes that the caller has
1171 * already called nfs_flush_incompatible() if necessary.
1173 static struct nfs_page
* nfs_setup_write_request(struct nfs_open_context
* ctx
,
1174 struct page
*page
, unsigned int offset
, unsigned int bytes
)
1176 struct inode
*inode
= page_file_mapping(page
)->host
;
1177 struct nfs_page
*req
;
1179 req
= nfs_try_to_update_request(inode
, page
, offset
, bytes
);
1182 req
= nfs_create_request(ctx
, page
, offset
, bytes
);
1185 nfs_inode_add_request(inode
, req
);
1190 static int nfs_writepage_setup(struct nfs_open_context
*ctx
, struct page
*page
,
1191 unsigned int offset
, unsigned int count
)
1193 struct nfs_page
*req
;
1195 req
= nfs_setup_write_request(ctx
, page
, offset
, count
);
1197 return PTR_ERR(req
);
1198 /* Update file length */
1199 nfs_grow_file(page
, offset
, count
);
1200 nfs_mark_uptodate(req
);
1201 nfs_mark_request_dirty(req
);
1202 nfs_unlock_and_release_request(req
);
1206 int nfs_flush_incompatible(struct file
*file
, struct page
*page
)
1208 struct nfs_open_context
*ctx
= nfs_file_open_context(file
);
1209 struct nfs_lock_context
*l_ctx
;
1210 struct file_lock_context
*flctx
= file_inode(file
)->i_flctx
;
1211 struct nfs_page
*req
;
1212 int do_flush
, status
;
1214 * Look for a request corresponding to this page. If there
1215 * is one, and it belongs to another file, we flush it out
1216 * before we try to copy anything into the page. Do this
1217 * due to the lack of an ACCESS-type call in NFSv2.
1218 * Also do the same if we find a request from an existing
1222 req
= nfs_page_find_head_request(page
);
1225 l_ctx
= req
->wb_lock_context
;
1226 do_flush
= req
->wb_page
!= page
||
1227 !nfs_match_open_context(nfs_req_openctx(req
), ctx
);
1228 if (l_ctx
&& flctx
&&
1229 !(list_empty_careful(&flctx
->flc_posix
) &&
1230 list_empty_careful(&flctx
->flc_flock
))) {
1231 do_flush
|= l_ctx
->lockowner
!= current
->files
;
1233 nfs_release_request(req
);
1236 status
= nfs_wb_page(page_file_mapping(page
)->host
, page
);
1237 } while (status
== 0);
1242 * Avoid buffered writes when a open context credential's key would
1245 * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
1247 * Return 0 and set a credential flag which triggers the inode to flush
1248 * and performs NFS_FILE_SYNC writes if the key will expired within
1249 * RPC_KEY_EXPIRE_TIMEO.
1252 nfs_key_timeout_notify(struct file
*filp
, struct inode
*inode
)
1254 struct nfs_open_context
*ctx
= nfs_file_open_context(filp
);
1256 if (nfs_ctx_key_to_expire(ctx
, inode
) &&
1258 /* Already expired! */
1264 * Test if the open context credential key is marked to expire soon.
1266 bool nfs_ctx_key_to_expire(struct nfs_open_context
*ctx
, struct inode
*inode
)
1268 struct rpc_auth
*auth
= NFS_SERVER(inode
)->client
->cl_auth
;
1269 struct rpc_cred
*cred
= ctx
->ll_cred
;
1270 struct auth_cred acred
= {
1274 if (cred
&& !cred
->cr_ops
->crmatch(&acred
, cred
, 0)) {
1276 ctx
->ll_cred
= NULL
;
1280 cred
= auth
->au_ops
->lookup_cred(auth
, &acred
, 0);
1281 if (!cred
|| IS_ERR(cred
))
1283 ctx
->ll_cred
= cred
;
1284 return !!(cred
->cr_ops
->crkey_timeout
&&
1285 cred
->cr_ops
->crkey_timeout(cred
));
1289 * If the page cache is marked as unsafe or invalid, then we can't rely on
1290 * the PageUptodate() flag. In this case, we will need to turn off
1291 * write optimisations that depend on the page contents being correct.
1293 static bool nfs_write_pageuptodate(struct page
*page
, struct inode
*inode
)
1295 struct nfs_inode
*nfsi
= NFS_I(inode
);
1297 if (nfs_have_delegated_attributes(inode
))
1299 if (nfsi
->cache_validity
& NFS_INO_REVAL_PAGECACHE
)
1302 if (test_bit(NFS_INO_INVALIDATING
, &nfsi
->flags
))
1305 if (nfsi
->cache_validity
& NFS_INO_INVALID_DATA
)
1307 return PageUptodate(page
) != 0;
1311 is_whole_file_wrlock(struct file_lock
*fl
)
1313 return fl
->fl_start
== 0 && fl
->fl_end
== OFFSET_MAX
&&
1314 fl
->fl_type
== F_WRLCK
;
1317 /* If we know the page is up to date, and we're not using byte range locks (or
1318 * if we have the whole file locked for writing), it may be more efficient to
1319 * extend the write to cover the entire page in order to avoid fragmentation
1322 * If the file is opened for synchronous writes then we can just skip the rest
1325 static int nfs_can_extend_write(struct file
*file
, struct page
*page
, struct inode
*inode
)
1328 struct file_lock_context
*flctx
= inode
->i_flctx
;
1329 struct file_lock
*fl
;
1331 if (file
->f_flags
& O_DSYNC
)
1333 if (!nfs_write_pageuptodate(page
, inode
))
1335 if (NFS_PROTO(inode
)->have_delegation(inode
, FMODE_WRITE
))
1337 if (!flctx
|| (list_empty_careful(&flctx
->flc_flock
) &&
1338 list_empty_careful(&flctx
->flc_posix
)))
1341 /* Check to see if there are whole file write locks */
1343 spin_lock(&flctx
->flc_lock
);
1344 if (!list_empty(&flctx
->flc_posix
)) {
1345 fl
= list_first_entry(&flctx
->flc_posix
, struct file_lock
,
1347 if (is_whole_file_wrlock(fl
))
1349 } else if (!list_empty(&flctx
->flc_flock
)) {
1350 fl
= list_first_entry(&flctx
->flc_flock
, struct file_lock
,
1352 if (fl
->fl_type
== F_WRLCK
)
1355 spin_unlock(&flctx
->flc_lock
);
1360 * Update and possibly write a cached page of an NFS file.
1362 * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
1363 * things with a page scheduled for an RPC call (e.g. invalidate it).
1365 int nfs_updatepage(struct file
*file
, struct page
*page
,
1366 unsigned int offset
, unsigned int count
)
1368 struct nfs_open_context
*ctx
= nfs_file_open_context(file
);
1369 struct address_space
*mapping
= page_file_mapping(page
);
1370 struct inode
*inode
= mapping
->host
;
1373 nfs_inc_stats(inode
, NFSIOS_VFSUPDATEPAGE
);
1375 dprintk("NFS: nfs_updatepage(%pD2 %d@%lld)\n",
1376 file
, count
, (long long)(page_file_offset(page
) + offset
));
1381 if (nfs_can_extend_write(file
, page
, inode
)) {
1382 count
= max(count
+ offset
, nfs_page_length(page
));
1386 status
= nfs_writepage_setup(ctx
, page
, offset
, count
);
1388 nfs_set_pageerror(mapping
);
1390 __set_page_dirty_nobuffers(page
);
1392 dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
1393 status
, (long long)i_size_read(inode
));
1397 static int flush_task_priority(int how
)
1399 switch (how
& (FLUSH_HIGHPRI
|FLUSH_LOWPRI
)) {
1401 return RPC_PRIORITY_HIGH
;
1403 return RPC_PRIORITY_LOW
;
1405 return RPC_PRIORITY_NORMAL
;
1408 static void nfs_initiate_write(struct nfs_pgio_header
*hdr
,
1409 struct rpc_message
*msg
,
1410 const struct nfs_rpc_ops
*rpc_ops
,
1411 struct rpc_task_setup
*task_setup_data
, int how
)
1413 int priority
= flush_task_priority(how
);
1415 task_setup_data
->priority
= priority
;
1416 rpc_ops
->write_setup(hdr
, msg
, &task_setup_data
->rpc_client
);
1417 trace_nfs_initiate_write(hdr
);
1420 /* If a nfs_flush_* function fails, it should remove reqs from @head and
1421 * call this on each, which will prepare them to be retried on next
1422 * writeback using standard nfs.
1424 static void nfs_redirty_request(struct nfs_page
*req
)
1426 /* Bump the transmission count */
1428 nfs_mark_request_dirty(req
);
1429 set_bit(NFS_CONTEXT_RESEND_WRITES
, &nfs_req_openctx(req
)->flags
);
1430 nfs_end_page_writeback(req
);
1431 nfs_release_request(req
);
1434 static void nfs_async_write_error(struct list_head
*head
, int error
)
1436 struct nfs_page
*req
;
1438 while (!list_empty(head
)) {
1439 req
= nfs_list_entry(head
->next
);
1440 nfs_list_remove_request(req
);
1441 if (nfs_error_is_fatal(error
))
1442 nfs_write_error(req
, error
);
1444 nfs_redirty_request(req
);
1448 static void nfs_async_write_reschedule_io(struct nfs_pgio_header
*hdr
)
1450 nfs_async_write_error(&hdr
->pages
, 0);
1451 filemap_fdatawrite_range(hdr
->inode
->i_mapping
, hdr
->args
.offset
,
1452 hdr
->args
.offset
+ hdr
->args
.count
- 1);
1455 static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops
= {
1456 .init_hdr
= nfs_async_write_init
,
1457 .error_cleanup
= nfs_async_write_error
,
1458 .completion
= nfs_write_completion
,
1459 .reschedule_io
= nfs_async_write_reschedule_io
,
1462 void nfs_pageio_init_write(struct nfs_pageio_descriptor
*pgio
,
1463 struct inode
*inode
, int ioflags
, bool force_mds
,
1464 const struct nfs_pgio_completion_ops
*compl_ops
)
1466 struct nfs_server
*server
= NFS_SERVER(inode
);
1467 const struct nfs_pageio_ops
*pg_ops
= &nfs_pgio_rw_ops
;
1469 #ifdef CONFIG_NFS_V4_1
1470 if (server
->pnfs_curr_ld
&& !force_mds
)
1471 pg_ops
= server
->pnfs_curr_ld
->pg_write_ops
;
1473 nfs_pageio_init(pgio
, inode
, pg_ops
, compl_ops
, &nfs_rw_write_ops
,
1474 server
->wsize
, ioflags
);
1476 EXPORT_SYMBOL_GPL(nfs_pageio_init_write
);
1478 void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor
*pgio
)
1480 struct nfs_pgio_mirror
*mirror
;
1482 if (pgio
->pg_ops
&& pgio
->pg_ops
->pg_cleanup
)
1483 pgio
->pg_ops
->pg_cleanup(pgio
);
1485 pgio
->pg_ops
= &nfs_pgio_rw_ops
;
1487 nfs_pageio_stop_mirroring(pgio
);
1489 mirror
= &pgio
->pg_mirrors
[0];
1490 mirror
->pg_bsize
= NFS_SERVER(pgio
->pg_inode
)->wsize
;
1492 EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds
);
1495 void nfs_commit_prepare(struct rpc_task
*task
, void *calldata
)
1497 struct nfs_commit_data
*data
= calldata
;
1499 NFS_PROTO(data
->inode
)->commit_rpc_prepare(task
, data
);
1503 * Special version of should_remove_suid() that ignores capabilities.
1505 static int nfs_should_remove_suid(const struct inode
*inode
)
1507 umode_t mode
= inode
->i_mode
;
1510 /* suid always must be killed */
1511 if (unlikely(mode
& S_ISUID
))
1512 kill
= ATTR_KILL_SUID
;
1515 * sgid without any exec bits is just a mandatory locking mark; leave
1516 * it alone. If some exec bits are set, it's a real sgid; kill it.
1518 if (unlikely((mode
& S_ISGID
) && (mode
& S_IXGRP
)))
1519 kill
|= ATTR_KILL_SGID
;
1521 if (unlikely(kill
&& S_ISREG(mode
)))
1527 static void nfs_writeback_check_extend(struct nfs_pgio_header
*hdr
,
1528 struct nfs_fattr
*fattr
)
1530 struct nfs_pgio_args
*argp
= &hdr
->args
;
1531 struct nfs_pgio_res
*resp
= &hdr
->res
;
1532 u64 size
= argp
->offset
+ resp
->count
;
1534 if (!(fattr
->valid
& NFS_ATTR_FATTR_SIZE
))
1536 if (nfs_size_to_loff_t(fattr
->size
) < i_size_read(hdr
->inode
)) {
1537 fattr
->valid
&= ~NFS_ATTR_FATTR_SIZE
;
1540 if (size
!= fattr
->size
)
1542 /* Set attribute barrier */
1543 nfs_fattr_set_barrier(fattr
);
1544 /* ...and update size */
1545 fattr
->valid
|= NFS_ATTR_FATTR_SIZE
;
1548 void nfs_writeback_update_inode(struct nfs_pgio_header
*hdr
)
1550 struct nfs_fattr
*fattr
= &hdr
->fattr
;
1551 struct inode
*inode
= hdr
->inode
;
1553 spin_lock(&inode
->i_lock
);
1554 nfs_writeback_check_extend(hdr
, fattr
);
1555 nfs_post_op_update_inode_force_wcc_locked(inode
, fattr
);
1556 spin_unlock(&inode
->i_lock
);
1558 EXPORT_SYMBOL_GPL(nfs_writeback_update_inode
);
1561 * This function is called when the WRITE call is complete.
1563 static int nfs_writeback_done(struct rpc_task
*task
,
1564 struct nfs_pgio_header
*hdr
,
1565 struct inode
*inode
)
1570 * ->write_done will attempt to use post-op attributes to detect
1571 * conflicting writes by other clients. A strict interpretation
1572 * of close-to-open would allow us to continue caching even if
1573 * another writer had changed the file, but some applications
1574 * depend on tighter cache coherency when writing.
1576 status
= NFS_PROTO(inode
)->write_done(task
, hdr
);
1580 nfs_add_stats(inode
, NFSIOS_SERVERWRITTENBYTES
, hdr
->res
.count
);
1581 trace_nfs_writeback_done(task
, hdr
);
1583 if (hdr
->res
.verf
->committed
< hdr
->args
.stable
&&
1584 task
->tk_status
>= 0) {
1585 /* We tried a write call, but the server did not
1586 * commit data to stable storage even though we
1588 * Note: There is a known bug in Tru64 < 5.0 in which
1589 * the server reports NFS_DATA_SYNC, but performs
1590 * NFS_FILE_SYNC. We therefore implement this checking
1591 * as a dprintk() in order to avoid filling syslog.
1593 static unsigned long complain
;
1595 /* Note this will print the MDS for a DS write */
1596 if (time_before(complain
, jiffies
)) {
1597 dprintk("NFS: faulty NFS server %s:"
1598 " (committed = %d) != (stable = %d)\n",
1599 NFS_SERVER(inode
)->nfs_client
->cl_hostname
,
1600 hdr
->res
.verf
->committed
, hdr
->args
.stable
);
1601 complain
= jiffies
+ 300 * HZ
;
1605 /* Deal with the suid/sgid bit corner case */
1606 if (nfs_should_remove_suid(inode
)) {
1607 spin_lock(&inode
->i_lock
);
1608 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_OTHER
;
1609 spin_unlock(&inode
->i_lock
);
1615 * This function is called when the WRITE call is complete.
1617 static void nfs_writeback_result(struct rpc_task
*task
,
1618 struct nfs_pgio_header
*hdr
)
1620 struct nfs_pgio_args
*argp
= &hdr
->args
;
1621 struct nfs_pgio_res
*resp
= &hdr
->res
;
1623 if (resp
->count
< argp
->count
) {
1624 static unsigned long complain
;
1626 /* This a short write! */
1627 nfs_inc_stats(hdr
->inode
, NFSIOS_SHORTWRITE
);
1629 /* Has the server at least made some progress? */
1630 if (resp
->count
== 0) {
1631 if (time_before(complain
, jiffies
)) {
1633 "NFS: Server wrote zero bytes, expected %u.\n",
1635 complain
= jiffies
+ 300 * HZ
;
1637 nfs_set_pgio_error(hdr
, -EIO
, argp
->offset
);
1638 task
->tk_status
= -EIO
;
1642 /* For non rpc-based layout drivers, retry-through-MDS */
1643 if (!task
->tk_ops
) {
1644 hdr
->pnfs_error
= -EAGAIN
;
1648 /* Was this an NFSv2 write or an NFSv3 stable write? */
1649 if (resp
->verf
->committed
!= NFS_UNSTABLE
) {
1650 /* Resend from where the server left off */
1651 hdr
->mds_offset
+= resp
->count
;
1652 argp
->offset
+= resp
->count
;
1653 argp
->pgbase
+= resp
->count
;
1654 argp
->count
-= resp
->count
;
1656 /* Resend as a stable write in order to avoid
1657 * headaches in the case of a server crash.
1659 argp
->stable
= NFS_FILE_SYNC
;
1662 resp
->verf
->committed
= 0;
1663 rpc_restart_call_prepare(task
);
1667 static int wait_on_commit(struct nfs_mds_commit_info
*cinfo
)
1669 return wait_var_event_killable(&cinfo
->rpcs_out
,
1670 !atomic_read(&cinfo
->rpcs_out
));
1673 static void nfs_commit_begin(struct nfs_mds_commit_info
*cinfo
)
1675 atomic_inc(&cinfo
->rpcs_out
);
1678 static void nfs_commit_end(struct nfs_mds_commit_info
*cinfo
)
1680 if (atomic_dec_and_test(&cinfo
->rpcs_out
))
1681 wake_up_var(&cinfo
->rpcs_out
);
1684 void nfs_commitdata_release(struct nfs_commit_data
*data
)
1686 put_nfs_open_context(data
->context
);
1687 nfs_commit_free(data
);
1689 EXPORT_SYMBOL_GPL(nfs_commitdata_release
);
1691 int nfs_initiate_commit(struct rpc_clnt
*clnt
, struct nfs_commit_data
*data
,
1692 const struct nfs_rpc_ops
*nfs_ops
,
1693 const struct rpc_call_ops
*call_ops
,
1696 struct rpc_task
*task
;
1697 int priority
= flush_task_priority(how
);
1698 struct rpc_message msg
= {
1699 .rpc_argp
= &data
->args
,
1700 .rpc_resp
= &data
->res
,
1701 .rpc_cred
= data
->cred
,
1703 struct rpc_task_setup task_setup_data
= {
1704 .task
= &data
->task
,
1706 .rpc_message
= &msg
,
1707 .callback_ops
= call_ops
,
1708 .callback_data
= data
,
1709 .workqueue
= nfsiod_workqueue
,
1710 .flags
= RPC_TASK_ASYNC
| flags
,
1711 .priority
= priority
,
1713 /* Set up the initial task struct. */
1714 nfs_ops
->commit_setup(data
, &msg
, &task_setup_data
.rpc_client
);
1715 trace_nfs_initiate_commit(data
);
1717 dprintk("NFS: initiated commit call\n");
1719 task
= rpc_run_task(&task_setup_data
);
1721 return PTR_ERR(task
);
1722 if (how
& FLUSH_SYNC
)
1723 rpc_wait_for_completion_task(task
);
1727 EXPORT_SYMBOL_GPL(nfs_initiate_commit
);
1729 static loff_t
nfs_get_lwb(struct list_head
*head
)
1732 struct nfs_page
*req
;
1734 list_for_each_entry(req
, head
, wb_list
)
1735 if (lwb
< (req_offset(req
) + req
->wb_bytes
))
1736 lwb
= req_offset(req
) + req
->wb_bytes
;
1742 * Set up the argument/result storage required for the RPC call.
1744 void nfs_init_commit(struct nfs_commit_data
*data
,
1745 struct list_head
*head
,
1746 struct pnfs_layout_segment
*lseg
,
1747 struct nfs_commit_info
*cinfo
)
1749 struct nfs_page
*first
= nfs_list_entry(head
->next
);
1750 struct nfs_open_context
*ctx
= nfs_req_openctx(first
);
1751 struct inode
*inode
= d_inode(ctx
->dentry
);
1753 /* Set up the RPC argument and reply structs
1754 * NB: take care not to mess about with data->commit et al. */
1756 list_splice_init(head
, &data
->pages
);
1758 data
->inode
= inode
;
1759 data
->cred
= ctx
->cred
;
1760 data
->lseg
= lseg
; /* reference transferred */
1761 /* only set lwb for pnfs commit */
1763 data
->lwb
= nfs_get_lwb(&data
->pages
);
1764 data
->mds_ops
= &nfs_commit_ops
;
1765 data
->completion_ops
= cinfo
->completion_ops
;
1766 data
->dreq
= cinfo
->dreq
;
1768 data
->args
.fh
= NFS_FH(data
->inode
);
1769 /* Note: we always request a commit of the entire inode */
1770 data
->args
.offset
= 0;
1771 data
->args
.count
= 0;
1772 data
->context
= get_nfs_open_context(ctx
);
1773 data
->res
.fattr
= &data
->fattr
;
1774 data
->res
.verf
= &data
->verf
;
1775 nfs_fattr_init(&data
->fattr
);
1777 EXPORT_SYMBOL_GPL(nfs_init_commit
);
1779 void nfs_retry_commit(struct list_head
*page_list
,
1780 struct pnfs_layout_segment
*lseg
,
1781 struct nfs_commit_info
*cinfo
,
1784 struct nfs_page
*req
;
1786 while (!list_empty(page_list
)) {
1787 req
= nfs_list_entry(page_list
->next
);
1788 nfs_list_remove_request(req
);
1789 nfs_mark_request_commit(req
, lseg
, cinfo
, ds_commit_idx
);
1791 nfs_clear_page_commit(req
->wb_page
);
1792 nfs_unlock_and_release_request(req
);
1795 EXPORT_SYMBOL_GPL(nfs_retry_commit
);
1798 nfs_commit_resched_write(struct nfs_commit_info
*cinfo
,
1799 struct nfs_page
*req
)
1801 __set_page_dirty_nobuffers(req
->wb_page
);
1805 * Commit dirty pages
1808 nfs_commit_list(struct inode
*inode
, struct list_head
*head
, int how
,
1809 struct nfs_commit_info
*cinfo
)
1811 struct nfs_commit_data
*data
;
1813 /* another commit raced with us */
1814 if (list_empty(head
))
1817 data
= nfs_commitdata_alloc(true);
1819 /* Set up the argument struct */
1820 nfs_init_commit(data
, head
, NULL
, cinfo
);
1821 atomic_inc(&cinfo
->mds
->rpcs_out
);
1822 return nfs_initiate_commit(NFS_CLIENT(inode
), data
, NFS_PROTO(inode
),
1823 data
->mds_ops
, how
, 0);
1827 * COMMIT call returned
1829 static void nfs_commit_done(struct rpc_task
*task
, void *calldata
)
1831 struct nfs_commit_data
*data
= calldata
;
1833 dprintk("NFS: %5u nfs_commit_done (status %d)\n",
1834 task
->tk_pid
, task
->tk_status
);
1836 /* Call the NFS version-specific code */
1837 NFS_PROTO(data
->inode
)->commit_done(task
, data
);
1838 trace_nfs_commit_done(task
, data
);
1841 static void nfs_commit_release_pages(struct nfs_commit_data
*data
)
1843 const struct nfs_writeverf
*verf
= data
->res
.verf
;
1844 struct nfs_page
*req
;
1845 int status
= data
->task
.tk_status
;
1846 struct nfs_commit_info cinfo
;
1847 struct nfs_server
*nfss
;
1849 while (!list_empty(&data
->pages
)) {
1850 req
= nfs_list_entry(data
->pages
.next
);
1851 nfs_list_remove_request(req
);
1853 nfs_clear_page_commit(req
->wb_page
);
1855 dprintk("NFS: commit (%s/%llu %d@%lld)",
1856 nfs_req_openctx(req
)->dentry
->d_sb
->s_id
,
1857 (unsigned long long)NFS_FILEID(d_inode(nfs_req_openctx(req
)->dentry
)),
1859 (long long)req_offset(req
));
1862 trace_nfs_commit_error(req
, status
);
1863 nfs_mapping_set_error(req
->wb_page
, status
);
1864 nfs_inode_remove_request(req
);
1866 dprintk_cont(", error = %d\n", status
);
1870 /* Okay, COMMIT succeeded, apparently. Check the verifier
1871 * returned by the server against all stored verfs. */
1872 if (verf
->committed
> NFS_UNSTABLE
&&
1873 !nfs_write_verifier_cmp(&req
->wb_verf
, &verf
->verifier
)) {
1874 /* We have a match */
1876 nfs_inode_remove_request(req
);
1877 dprintk_cont(" OK\n");
1880 /* We have a mismatch. Write the page again */
1881 dprintk_cont(" mismatch\n");
1882 nfs_mark_request_dirty(req
);
1883 set_bit(NFS_CONTEXT_RESEND_WRITES
, &nfs_req_openctx(req
)->flags
);
1885 nfs_unlock_and_release_request(req
);
1886 /* Latency breaker */
1889 nfss
= NFS_SERVER(data
->inode
);
1890 if (atomic_long_read(&nfss
->writeback
) < NFS_CONGESTION_OFF_THRESH
)
1891 clear_bdi_congested(inode_to_bdi(data
->inode
), BLK_RW_ASYNC
);
1893 nfs_init_cinfo(&cinfo
, data
->inode
, data
->dreq
);
1894 nfs_commit_end(cinfo
.mds
);
1897 static void nfs_commit_release(void *calldata
)
1899 struct nfs_commit_data
*data
= calldata
;
1901 data
->completion_ops
->completion(data
);
1902 nfs_commitdata_release(calldata
);
1905 static const struct rpc_call_ops nfs_commit_ops
= {
1906 .rpc_call_prepare
= nfs_commit_prepare
,
1907 .rpc_call_done
= nfs_commit_done
,
1908 .rpc_release
= nfs_commit_release
,
1911 static const struct nfs_commit_completion_ops nfs_commit_completion_ops
= {
1912 .completion
= nfs_commit_release_pages
,
1913 .resched_write
= nfs_commit_resched_write
,
1916 int nfs_generic_commit_list(struct inode
*inode
, struct list_head
*head
,
1917 int how
, struct nfs_commit_info
*cinfo
)
1921 status
= pnfs_commit_list(inode
, head
, how
, cinfo
);
1922 if (status
== PNFS_NOT_ATTEMPTED
)
1923 status
= nfs_commit_list(inode
, head
, how
, cinfo
);
1927 static int __nfs_commit_inode(struct inode
*inode
, int how
,
1928 struct writeback_control
*wbc
)
1931 struct nfs_commit_info cinfo
;
1932 int may_wait
= how
& FLUSH_SYNC
;
1935 nfs_init_cinfo_from_inode(&cinfo
, inode
);
1936 nfs_commit_begin(cinfo
.mds
);
1938 ret
= nscan
= nfs_scan_commit(inode
, &head
, &cinfo
);
1941 ret
= nfs_generic_commit_list(inode
, &head
, how
, &cinfo
);
1945 if (wbc
&& wbc
->sync_mode
== WB_SYNC_NONE
) {
1946 if (nscan
< wbc
->nr_to_write
)
1947 wbc
->nr_to_write
-= nscan
;
1949 wbc
->nr_to_write
= 0;
1951 if (nscan
< INT_MAX
)
1955 nfs_commit_end(cinfo
.mds
);
1956 if (ret
|| !may_wait
)
1958 return wait_on_commit(cinfo
.mds
);
1961 int nfs_commit_inode(struct inode
*inode
, int how
)
1963 return __nfs_commit_inode(inode
, how
, NULL
);
1965 EXPORT_SYMBOL_GPL(nfs_commit_inode
);
1967 int nfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1969 struct nfs_inode
*nfsi
= NFS_I(inode
);
1970 int flags
= FLUSH_SYNC
;
1973 if (wbc
->sync_mode
== WB_SYNC_NONE
) {
1974 /* no commits means nothing needs to be done */
1975 if (!atomic_long_read(&nfsi
->commit_info
.ncommit
))
1976 goto check_requests_outstanding
;
1978 /* Don't commit yet if this is a non-blocking flush and there
1979 * are a lot of outstanding writes for this mapping.
1981 if (mapping_tagged(inode
->i_mapping
, PAGECACHE_TAG_WRITEBACK
))
1982 goto out_mark_dirty
;
1984 /* don't wait for the COMMIT response */
1988 ret
= __nfs_commit_inode(inode
, flags
, wbc
);
1990 if (flags
& FLUSH_SYNC
)
1992 } else if (atomic_long_read(&nfsi
->commit_info
.ncommit
))
1993 goto out_mark_dirty
;
1995 check_requests_outstanding
:
1996 if (!atomic_read(&nfsi
->commit_info
.rpcs_out
))
1999 __mark_inode_dirty(inode
, I_DIRTY_DATASYNC
);
2002 EXPORT_SYMBOL_GPL(nfs_write_inode
);
2005 * Wrapper for filemap_write_and_wait_range()
2007 * Needed for pNFS in order to ensure data becomes visible to the
2010 int nfs_filemap_write_and_wait_range(struct address_space
*mapping
,
2011 loff_t lstart
, loff_t lend
)
2015 ret
= filemap_write_and_wait_range(mapping
, lstart
, lend
);
2017 ret
= pnfs_sync_inode(mapping
->host
, true);
2020 EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range
);
2023 * flush the inode to disk.
2025 int nfs_wb_all(struct inode
*inode
)
2029 trace_nfs_writeback_inode_enter(inode
);
2031 ret
= filemap_write_and_wait(inode
->i_mapping
);
2034 ret
= nfs_commit_inode(inode
, FLUSH_SYNC
);
2037 pnfs_sync_inode(inode
, true);
2041 trace_nfs_writeback_inode_exit(inode
, ret
);
2044 EXPORT_SYMBOL_GPL(nfs_wb_all
);
2046 int nfs_wb_page_cancel(struct inode
*inode
, struct page
*page
)
2048 struct nfs_page
*req
;
2051 wait_on_page_writeback(page
);
2053 /* blocking call to cancel all requests and join to a single (head)
2055 req
= nfs_lock_and_join_requests(page
);
2060 /* all requests from this page have been cancelled by
2061 * nfs_lock_and_join_requests, so just remove the head
2062 * request from the inode / page_private pointer and
2064 nfs_inode_remove_request(req
);
2065 nfs_unlock_and_release_request(req
);
2072 * Write back all requests on one page - we do this before reading it.
2074 int nfs_wb_page(struct inode
*inode
, struct page
*page
)
2076 loff_t range_start
= page_file_offset(page
);
2077 loff_t range_end
= range_start
+ (loff_t
)(PAGE_SIZE
- 1);
2078 struct writeback_control wbc
= {
2079 .sync_mode
= WB_SYNC_ALL
,
2081 .range_start
= range_start
,
2082 .range_end
= range_end
,
2086 trace_nfs_writeback_page_enter(inode
);
2089 wait_on_page_writeback(page
);
2090 if (clear_page_dirty_for_io(page
)) {
2091 ret
= nfs_writepage_locked(page
, &wbc
);
2097 if (!PagePrivate(page
))
2099 ret
= nfs_commit_inode(inode
, FLUSH_SYNC
);
2104 trace_nfs_writeback_page_exit(inode
, ret
);
2108 #ifdef CONFIG_MIGRATION
2109 int nfs_migrate_page(struct address_space
*mapping
, struct page
*newpage
,
2110 struct page
*page
, enum migrate_mode mode
)
2113 * If PagePrivate is set, then the page is currently associated with
2114 * an in-progress read or write request. Don't try to migrate it.
2116 * FIXME: we could do this in principle, but we'll need a way to ensure
2117 * that we can safely release the inode reference while holding
2120 if (PagePrivate(page
))
2123 if (!nfs_fscache_release_page(page
, GFP_KERNEL
))
2126 return migrate_page(mapping
, newpage
, page
, mode
);
2130 int __init
nfs_init_writepagecache(void)
2132 nfs_wdata_cachep
= kmem_cache_create("nfs_write_data",
2133 sizeof(struct nfs_pgio_header
),
2134 0, SLAB_HWCACHE_ALIGN
,
2136 if (nfs_wdata_cachep
== NULL
)
2139 nfs_wdata_mempool
= mempool_create_slab_pool(MIN_POOL_WRITE
,
2141 if (nfs_wdata_mempool
== NULL
)
2142 goto out_destroy_write_cache
;
2144 nfs_cdata_cachep
= kmem_cache_create("nfs_commit_data",
2145 sizeof(struct nfs_commit_data
),
2146 0, SLAB_HWCACHE_ALIGN
,
2148 if (nfs_cdata_cachep
== NULL
)
2149 goto out_destroy_write_mempool
;
2151 nfs_commit_mempool
= mempool_create_slab_pool(MIN_POOL_COMMIT
,
2153 if (nfs_commit_mempool
== NULL
)
2154 goto out_destroy_commit_cache
;
2157 * NFS congestion size, scale with available memory.
2169 * This allows larger machines to have larger/more transfers.
2170 * Limit the default to 256M
2172 nfs_congestion_kb
= (16*int_sqrt(totalram_pages())) << (PAGE_SHIFT
-10);
2173 if (nfs_congestion_kb
> 256*1024)
2174 nfs_congestion_kb
= 256*1024;
2178 out_destroy_commit_cache
:
2179 kmem_cache_destroy(nfs_cdata_cachep
);
2180 out_destroy_write_mempool
:
2181 mempool_destroy(nfs_wdata_mempool
);
2182 out_destroy_write_cache
:
2183 kmem_cache_destroy(nfs_wdata_cachep
);
2187 void nfs_destroy_writepagecache(void)
2189 mempool_destroy(nfs_commit_mempool
);
2190 kmem_cache_destroy(nfs_cdata_cachep
);
2191 mempool_destroy(nfs_wdata_mempool
);
2192 kmem_cache_destroy(nfs_wdata_cachep
);
2195 static const struct nfs_rw_ops nfs_rw_write_ops
= {
2196 .rw_alloc_header
= nfs_writehdr_alloc
,
2197 .rw_free_header
= nfs_writehdr_free
,
2198 .rw_done
= nfs_writeback_done
,
2199 .rw_result
= nfs_writeback_result
,
2200 .rw_initiate
= nfs_initiate_write
,