xref: /linux/fs/nfs/write.c (revision 684a64bf32b6e488004e0ad7f0d7e922798f65b6)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * linux/fs/nfs/write.c
41da177e4SLinus Torvalds  *
57c85d900STrond Myklebust  * Write file data over NFS.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <linux/types.h>
111da177e4SLinus Torvalds #include <linux/slab.h>
121da177e4SLinus Torvalds #include <linux/mm.h>
131da177e4SLinus Torvalds #include <linux/pagemap.h>
141da177e4SLinus Torvalds #include <linux/file.h>
151da177e4SLinus Torvalds #include <linux/writeback.h>
1689a09141SPeter Zijlstra #include <linux/swap.h>
17074cc1deSTrond Myklebust #include <linux/migrate.h>
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
201da177e4SLinus Torvalds #include <linux/nfs_fs.h>
211da177e4SLinus Torvalds #include <linux/nfs_mount.h>
221da177e4SLinus Torvalds #include <linux/nfs_page.h>
233fcfab16SAndrew Morton #include <linux/backing-dev.h>
24afeacc8cSPaul Gortmaker #include <linux/export.h>
25af7cf057STrond Myklebust #include <linux/freezer.h>
26af7cf057STrond Myklebust #include <linux/wait.h>
271eb5d98fSJeff Layton #include <linux/iversion.h>
285970e15dSJeff Layton #include <linux/filelock.h>
293fcfab16SAndrew Morton 
307c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
31875bc3fbSTrond Myklebust #include <linux/sched/mm.h>
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #include "delegation.h"
3449a70f27STrond Myklebust #include "internal.h"
3591d5b470SChuck Lever #include "iostat.h"
36def6ed7eSAndy Adamson #include "nfs4_fs.h"
37074cc1deSTrond Myklebust #include "fscache.h"
3894ad1c80SFred Isaman #include "pnfs.h"
391da177e4SLinus Torvalds 
40f4ce1299STrond Myklebust #include "nfstrace.h"
41f4ce1299STrond Myklebust 
421da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #define MIN_POOL_WRITE		(32)
451da177e4SLinus Torvalds #define MIN_POOL_COMMIT		(4)
461da177e4SLinus Torvalds 
47919e3bd9STrond Myklebust struct nfs_io_completion {
48919e3bd9STrond Myklebust 	void (*complete)(void *data);
49919e3bd9STrond Myklebust 	void *data;
50919e3bd9STrond Myklebust 	struct kref refcount;
51919e3bd9STrond Myklebust };
52919e3bd9STrond Myklebust 
531da177e4SLinus Torvalds /*
541da177e4SLinus Torvalds  * Local function declarations
551da177e4SLinus Torvalds  */
56f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req);
57788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops;
58061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops;
59f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops;
604a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops;
6106c9fdf3STrond Myklebust static void nfs_inode_remove_request(struct nfs_page *req);
62b193a78dSTrond Myklebust static void nfs_clear_request_commit(struct nfs_commit_info *cinfo,
63b193a78dSTrond Myklebust 				     struct nfs_page *req);
6402d1426cSWeston Andros Adamson static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
6502d1426cSWeston Andros Adamson 				      struct inode *inode);
661da177e4SLinus Torvalds 
67e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep;
683feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
690b7c0153SFred Isaman static struct kmem_cache *nfs_cdata_cachep;
701da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
711da177e4SLinus Torvalds 
72515dcdcdSTrond Myklebust struct nfs_commit_data *nfs_commitdata_alloc(void)
731da177e4SLinus Torvalds {
74518662e0SNeilBrown 	struct nfs_commit_data *p;
7540859d7eSChuck Lever 
76515dcdcdSTrond Myklebust 	p = kmem_cache_zalloc(nfs_cdata_cachep, nfs_io_gfp_mask());
77515dcdcdSTrond Myklebust 	if (!p) {
78518662e0SNeilBrown 		p = mempool_alloc(nfs_commit_mempool, GFP_NOWAIT);
79518662e0SNeilBrown 		if (!p)
80518662e0SNeilBrown 			return NULL;
811da177e4SLinus Torvalds 		memset(p, 0, sizeof(*p));
82515dcdcdSTrond Myklebust 	}
831da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->pages);
841da177e4SLinus Torvalds 	return p;
851da177e4SLinus Torvalds }
86e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
871da177e4SLinus Torvalds 
880b7c0153SFred Isaman void nfs_commit_free(struct nfs_commit_data *p)
891da177e4SLinus Torvalds {
901da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
911da177e4SLinus Torvalds }
92e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free);
931da177e4SLinus Torvalds 
941e7f3a48SWeston Andros Adamson static struct nfs_pgio_header *nfs_writehdr_alloc(void)
953feb2d49STrond Myklebust {
960bae835bSTrond Myklebust 	struct nfs_pgio_header *p;
973feb2d49STrond Myklebust 
980bae835bSTrond Myklebust 	p = kmem_cache_zalloc(nfs_wdata_cachep, nfs_io_gfp_mask());
990bae835bSTrond Myklebust 	if (!p) {
1000bae835bSTrond Myklebust 		p = mempool_alloc(nfs_wdata_mempool, GFP_NOWAIT);
1010bae835bSTrond Myklebust 		if (!p)
1020bae835bSTrond Myklebust 			return NULL;
1033feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
1040bae835bSTrond Myklebust 	}
105fbe77c30SBenjamin Coddington 	p->rw_mode = FMODE_WRITE;
1063feb2d49STrond Myklebust 	return p;
1073feb2d49STrond Myklebust }
1083feb2d49STrond Myklebust 
1091e7f3a48SWeston Andros Adamson static void nfs_writehdr_free(struct nfs_pgio_header *hdr)
1106c75dc0dSFred Isaman {
1111e7f3a48SWeston Andros Adamson 	mempool_free(hdr, nfs_wdata_mempool);
1123feb2d49STrond Myklebust }
1131da177e4SLinus Torvalds 
114919e3bd9STrond Myklebust static struct nfs_io_completion *nfs_io_completion_alloc(gfp_t gfp_flags)
115919e3bd9STrond Myklebust {
116919e3bd9STrond Myklebust 	return kmalloc(sizeof(struct nfs_io_completion), gfp_flags);
117919e3bd9STrond Myklebust }
118919e3bd9STrond Myklebust 
119919e3bd9STrond Myklebust static void nfs_io_completion_init(struct nfs_io_completion *ioc,
120919e3bd9STrond Myklebust 		void (*complete)(void *), void *data)
121919e3bd9STrond Myklebust {
122919e3bd9STrond Myklebust 	ioc->complete = complete;
123919e3bd9STrond Myklebust 	ioc->data = data;
124919e3bd9STrond Myklebust 	kref_init(&ioc->refcount);
125919e3bd9STrond Myklebust }
126919e3bd9STrond Myklebust 
127919e3bd9STrond Myklebust static void nfs_io_completion_release(struct kref *kref)
128919e3bd9STrond Myklebust {
129919e3bd9STrond Myklebust 	struct nfs_io_completion *ioc = container_of(kref,
130919e3bd9STrond Myklebust 			struct nfs_io_completion, refcount);
131919e3bd9STrond Myklebust 	ioc->complete(ioc->data);
132919e3bd9STrond Myklebust 	kfree(ioc);
133919e3bd9STrond Myklebust }
134919e3bd9STrond Myklebust 
135919e3bd9STrond Myklebust static void nfs_io_completion_get(struct nfs_io_completion *ioc)
136919e3bd9STrond Myklebust {
137919e3bd9STrond Myklebust 	if (ioc != NULL)
138919e3bd9STrond Myklebust 		kref_get(&ioc->refcount);
139919e3bd9STrond Myklebust }
140919e3bd9STrond Myklebust 
141919e3bd9STrond Myklebust static void nfs_io_completion_put(struct nfs_io_completion *ioc)
142919e3bd9STrond Myklebust {
143919e3bd9STrond Myklebust 	if (ioc != NULL)
144919e3bd9STrond Myklebust 		kref_put(&ioc->refcount, nfs_io_completion_release);
145919e3bd9STrond Myklebust }
146919e3bd9STrond Myklebust 
1470c493b5cSTrond Myklebust /**
1487e8e78a0SChristoph Hellwig  * nfs_folio_find_head_request - find head request associated with a folio
1490c493b5cSTrond Myklebust  * @folio: pointer to folio
15084d3a9a9SWeston Andros Adamson  *
15184d3a9a9SWeston Andros Adamson  * must be called while holding the inode lock.
15284d3a9a9SWeston Andros Adamson  *
15384d3a9a9SWeston Andros Adamson  * returns matching head request with reference held, or NULL if not found.
15484d3a9a9SWeston Andros Adamson  */
1557e8e78a0SChristoph Hellwig static struct nfs_page *nfs_folio_find_head_request(struct folio *folio)
156277459d2STrond Myklebust {
1577e8e78a0SChristoph Hellwig 	struct address_space *mapping = folio->mapping;
158bd37d6fcSTrond Myklebust 	struct nfs_page *req;
159277459d2STrond Myklebust 
1600c493b5cSTrond Myklebust 	if (!folio_test_private(folio))
161b30d2f04STrond Myklebust 		return NULL;
162600f111eSMatthew Wilcox (Oracle) 	spin_lock(&mapping->i_private_lock);
16302e61ec1SChristoph Hellwig 	req = folio->private;
16484d3a9a9SWeston Andros Adamson 	if (req) {
16584d3a9a9SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_head != req);
16629418aa4SMel Gorman 		kref_get(&req->wb_kref);
16784d3a9a9SWeston Andros Adamson 	}
168600f111eSMatthew Wilcox (Oracle) 	spin_unlock(&mapping->i_private_lock);
169b30d2f04STrond Myklebust 	return req;
170b30d2f04STrond Myklebust }
17129418aa4SMel Gorman 
1721da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1730c493b5cSTrond Myklebust static void nfs_grow_file(struct folio *folio, unsigned int offset,
1740c493b5cSTrond Myklebust 			  unsigned int count)
1751da177e4SLinus Torvalds {
1767e8e78a0SChristoph Hellwig 	struct inode *inode = folio->mapping->host;
177a3d01454STrond Myklebust 	loff_t end, i_size;
178a3d01454STrond Myklebust 	pgoff_t end_index;
1791da177e4SLinus Torvalds 
180a3d01454STrond Myklebust 	spin_lock(&inode->i_lock);
181a3d01454STrond Myklebust 	i_size = i_size_read(inode);
1820c493b5cSTrond Myklebust 	end_index = ((i_size - 1) >> folio_shift(folio)) << folio_order(folio);
1837e8e78a0SChristoph Hellwig 	if (i_size > 0 && folio->index < end_index)
184a3d01454STrond Myklebust 		goto out;
185237d2907SKairui Song 	end = folio_pos(folio) + (loff_t)offset + (loff_t)count;
1861da177e4SLinus Torvalds 	if (i_size >= end)
187a3d01454STrond Myklebust 		goto out;
188110cb2d2SChuck Lever 	trace_nfs_size_grow(inode, end);
1891da177e4SLinus Torvalds 	i_size_write(inode, end);
190f6cdfa6dSTrond Myklebust 	NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE;
191a3d01454STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
192a3d01454STrond Myklebust out:
193e12912d9STrond Myklebust 	/* Atomically update timestamps if they are delegated to us. */
194e12912d9STrond Myklebust 	nfs_update_delegated_mtime_locked(inode);
195a3d01454STrond Myklebust 	spin_unlock(&inode->i_lock);
196a6b5a28eSDave Wysochanski 	nfs_fscache_invalidate(inode, 0);
1971da177e4SLinus Torvalds }
1981da177e4SLinus Torvalds 
199a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
200d2ceb7e5SBenjamin Coddington static void nfs_set_pageerror(struct address_space *mapping)
201a301b777STrond Myklebust {
2020df68cedSTrond Myklebust 	struct inode *inode = mapping->host;
2030df68cedSTrond Myklebust 
204d2ceb7e5SBenjamin Coddington 	nfs_zap_mapping(mapping->host, mapping);
2050df68cedSTrond Myklebust 	/* Force file size revalidation */
2060df68cedSTrond Myklebust 	spin_lock(&inode->i_lock);
207ac46b3d7STrond Myklebust 	nfs_set_cache_invalid(inode, NFS_INO_REVAL_FORCED |
20888a6099fSTrond Myklebust 					     NFS_INO_INVALID_CHANGE |
209ac46b3d7STrond Myklebust 					     NFS_INO_INVALID_SIZE);
2100df68cedSTrond Myklebust 	spin_unlock(&inode->i_lock);
211a301b777STrond Myklebust }
212a301b777STrond Myklebust 
2130c493b5cSTrond Myklebust static void nfs_mapping_set_error(struct folio *folio, int error)
2146fbda89bSTrond Myklebust {
2157e8e78a0SChristoph Hellwig 	struct address_space *mapping = folio->mapping;
216b8946d7bSTrond Myklebust 
2176c984083STrond Myklebust 	filemap_set_wb_err(mapping, error);
2186c984083STrond Myklebust 	if (mapping->host)
2196c984083STrond Myklebust 		errseq_set(&mapping->host->i_sb->s_wb_err,
2206c984083STrond Myklebust 			   error == -ENOSPC ? -ENOSPC : -EIO);
221b8946d7bSTrond Myklebust 	nfs_set_pageerror(mapping);
2226fbda89bSTrond Myklebust }
2236fbda89bSTrond Myklebust 
224d72ddcbaSWeston Andros Adamson /*
225d72ddcbaSWeston Andros Adamson  * nfs_page_group_search_locked
226d72ddcbaSWeston Andros Adamson  * @head - head request of page group
227d72ddcbaSWeston Andros Adamson  * @page_offset - offset into page
228d72ddcbaSWeston Andros Adamson  *
229d72ddcbaSWeston Andros Adamson  * Search page group with head @head to find a request that contains the
230d72ddcbaSWeston Andros Adamson  * page offset @page_offset.
231d72ddcbaSWeston Andros Adamson  *
232d72ddcbaSWeston Andros Adamson  * Returns a pointer to the first matching nfs request, or NULL if no
233d72ddcbaSWeston Andros Adamson  * match is found.
234d72ddcbaSWeston Andros Adamson  *
235d72ddcbaSWeston Andros Adamson  * Must be called with the page group lock held
236d72ddcbaSWeston Andros Adamson  */
237d72ddcbaSWeston Andros Adamson static struct nfs_page *
238d72ddcbaSWeston Andros Adamson nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset)
239d72ddcbaSWeston Andros Adamson {
240d72ddcbaSWeston Andros Adamson 	struct nfs_page *req;
241d72ddcbaSWeston Andros Adamson 
242d72ddcbaSWeston Andros Adamson 	req = head;
243d72ddcbaSWeston Andros Adamson 	do {
244d72ddcbaSWeston Andros Adamson 		if (page_offset >= req->wb_pgbase &&
245d72ddcbaSWeston Andros Adamson 		    page_offset < (req->wb_pgbase + req->wb_bytes))
246d72ddcbaSWeston Andros Adamson 			return req;
247d72ddcbaSWeston Andros Adamson 
248d72ddcbaSWeston Andros Adamson 		req = req->wb_this_page;
249d72ddcbaSWeston Andros Adamson 	} while (req != head);
250d72ddcbaSWeston Andros Adamson 
251d72ddcbaSWeston Andros Adamson 	return NULL;
252d72ddcbaSWeston Andros Adamson }
253d72ddcbaSWeston Andros Adamson 
254d72ddcbaSWeston Andros Adamson /*
255d72ddcbaSWeston Andros Adamson  * nfs_page_group_covers_page
256d72ddcbaSWeston Andros Adamson  * @head - head request of page group
257d72ddcbaSWeston Andros Adamson  *
258d72ddcbaSWeston Andros Adamson  * Return true if the page group with head @head covers the whole page,
259d72ddcbaSWeston Andros Adamson  * returns false otherwise
260d72ddcbaSWeston Andros Adamson  */
261d72ddcbaSWeston Andros Adamson static bool nfs_page_group_covers_page(struct nfs_page *req)
262d72ddcbaSWeston Andros Adamson {
2630c493b5cSTrond Myklebust 	unsigned int len = nfs_folio_length(nfs_page_to_folio(req));
264d72ddcbaSWeston Andros Adamson 	struct nfs_page *tmp;
265d72ddcbaSWeston Andros Adamson 	unsigned int pos = 0;
266d72ddcbaSWeston Andros Adamson 
2671344b7eaSTrond Myklebust 	nfs_page_group_lock(req);
268d72ddcbaSWeston Andros Adamson 
2697e8a30f8STrond Myklebust 	for (;;) {
270d72ddcbaSWeston Andros Adamson 		tmp = nfs_page_group_search_locked(req->wb_head, pos);
2717e8a30f8STrond Myklebust 		if (!tmp)
2727e8a30f8STrond Myklebust 			break;
2737e8a30f8STrond Myklebust 		pos = tmp->wb_pgbase + tmp->wb_bytes;
274d72ddcbaSWeston Andros Adamson 	}
275d72ddcbaSWeston Andros Adamson 
276d72ddcbaSWeston Andros Adamson 	nfs_page_group_unlock(req);
2777e8a30f8STrond Myklebust 	return pos >= len;
278d72ddcbaSWeston Andros Adamson }
279d72ddcbaSWeston Andros Adamson 
2801da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
2811da177e4SLinus Torvalds  * covers the full page.
2821da177e4SLinus Torvalds  */
283d72ddcbaSWeston Andros Adamson static void nfs_mark_uptodate(struct nfs_page *req)
2841da177e4SLinus Torvalds {
2850c493b5cSTrond Myklebust 	struct folio *folio = nfs_page_to_folio(req);
2860c493b5cSTrond Myklebust 
2870c493b5cSTrond Myklebust 	if (folio_test_uptodate(folio))
2881da177e4SLinus Torvalds 		return;
289d72ddcbaSWeston Andros Adamson 	if (!nfs_page_group_covers_page(req))
2901da177e4SLinus Torvalds 		return;
2910c493b5cSTrond Myklebust 	folio_mark_uptodate(folio);
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
2951da177e4SLinus Torvalds {
296e87b4c7aSNeilBrown 	int ret = 0;
297cca588d6STrond Myklebust 
298e87b4c7aSNeilBrown 	if (wbc->sync_mode == WB_SYNC_ALL)
299e87b4c7aSNeilBrown 		ret = FLUSH_COND_STABLE;
300e87b4c7aSNeilBrown 	return ret;
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds /*
30489a09141SPeter Zijlstra  * NFS congestion control
30589a09141SPeter Zijlstra  */
30689a09141SPeter Zijlstra 
30789a09141SPeter Zijlstra int nfs_congestion_kb;
30889a09141SPeter Zijlstra 
30989a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
31089a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
31189a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
31289a09141SPeter Zijlstra 
3130c493b5cSTrond Myklebust static void nfs_folio_set_writeback(struct folio *folio)
31489a09141SPeter Zijlstra {
3157e8e78a0SChristoph Hellwig 	struct nfs_server *nfss = NFS_SERVER(folio->mapping->host);
3165a6d41b3STrond Myklebust 
3170c493b5cSTrond Myklebust 	folio_start_writeback(folio);
3180c493b5cSTrond Myklebust 	if (atomic_long_inc_return(&nfss->writeback) > NFS_CONGESTION_ON_THRESH)
3196df25e58SNeilBrown 		nfss->write_congested = 1;
32089a09141SPeter Zijlstra }
32189a09141SPeter Zijlstra 
3220c493b5cSTrond Myklebust static void nfs_folio_end_writeback(struct folio *folio)
32389a09141SPeter Zijlstra {
3247e8e78a0SChristoph Hellwig 	struct nfs_server *nfss = NFS_SERVER(folio->mapping->host);
32589a09141SPeter Zijlstra 
3260c493b5cSTrond Myklebust 	folio_end_writeback(folio);
3270c493b5cSTrond Myklebust 	if (atomic_long_dec_return(&nfss->writeback) <
3282f1f3104SJan Kara 	    NFS_CONGESTION_OFF_THRESH) {
3296df25e58SNeilBrown 		nfss->write_congested = 0;
3302f1f3104SJan Kara 		wake_up_all(&nfss->write_congestion_wait);
3312f1f3104SJan Kara 	}
33289a09141SPeter Zijlstra }
33389a09141SPeter Zijlstra 
3340c493b5cSTrond Myklebust static void nfs_page_end_writeback(struct nfs_page *req)
3350c493b5cSTrond Myklebust {
3360c493b5cSTrond Myklebust 	if (nfs_page_group_sync_on_bit(req, PG_WB_END)) {
3370c493b5cSTrond Myklebust 		nfs_unlock_request(req);
3380c493b5cSTrond Myklebust 		nfs_folio_end_writeback(nfs_page_to_folio(req));
3390c493b5cSTrond Myklebust 	} else
3400c493b5cSTrond Myklebust 		nfs_unlock_request(req);
3410c493b5cSTrond Myklebust }
3420c493b5cSTrond Myklebust 
343d4581383SWeston Andros Adamson /*
344d4581383SWeston Andros Adamson  * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests
345d4581383SWeston Andros Adamson  *
346d4581383SWeston Andros Adamson  * @destroy_list - request list (using wb_this_page) terminated by @old_head
347d4581383SWeston Andros Adamson  * @old_head - the old head of the list
348d4581383SWeston Andros Adamson  *
349d4581383SWeston Andros Adamson  * All subrequests must be locked and removed from all lists, so at this point
350d4581383SWeston Andros Adamson  * they are only "active" in this function, and possibly in nfs_wait_on_request
351d4581383SWeston Andros Adamson  * with a reference held by some other context.
352d4581383SWeston Andros Adamson  */
353d4581383SWeston Andros Adamson static void
354d4581383SWeston Andros Adamson nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list,
355b66aaa8dSTrond Myklebust 				 struct nfs_page *old_head,
356b66aaa8dSTrond Myklebust 				 struct inode *inode)
357d4581383SWeston Andros Adamson {
358d4581383SWeston Andros Adamson 	while (destroy_list) {
359d4581383SWeston Andros Adamson 		struct nfs_page *subreq = destroy_list;
360d4581383SWeston Andros Adamson 
361d4581383SWeston Andros Adamson 		destroy_list = (subreq->wb_this_page == old_head) ?
362d4581383SWeston Andros Adamson 				   NULL : subreq->wb_this_page;
363d4581383SWeston Andros Adamson 
36408ca8b21STrond Myklebust 		/* Note: lock subreq in order to change subreq->wb_head */
36508ca8b21STrond Myklebust 		nfs_page_set_headlock(subreq);
366d4581383SWeston Andros Adamson 		WARN_ON_ONCE(old_head != subreq->wb_head);
367d4581383SWeston Andros Adamson 
368d4581383SWeston Andros Adamson 		/* make sure old group is not used */
369d4581383SWeston Andros Adamson 		subreq->wb_this_page = subreq;
37008ca8b21STrond Myklebust 		subreq->wb_head = subreq;
371d4581383SWeston Andros Adamson 
372902a4c00STrond Myklebust 		clear_bit(PG_REMOVE, &subreq->wb_flags);
373902a4c00STrond Myklebust 
3745b2b5187STrond Myklebust 		/* Note: races with nfs_page_group_destroy() */
3755b2b5187STrond Myklebust 		if (!kref_read(&subreq->wb_kref)) {
3765b2b5187STrond Myklebust 			/* Check if we raced with nfs_page_group_destroy() */
37708ca8b21STrond Myklebust 			if (test_and_clear_bit(PG_TEARDOWN, &subreq->wb_flags)) {
37808ca8b21STrond Myklebust 				nfs_page_clear_headlock(subreq);
3795b2b5187STrond Myklebust 				nfs_free_request(subreq);
38008ca8b21STrond Myklebust 			} else
38108ca8b21STrond Myklebust 				nfs_page_clear_headlock(subreq);
3825b2b5187STrond Myklebust 			continue;
3835b2b5187STrond Myklebust 		}
38408ca8b21STrond Myklebust 		nfs_page_clear_headlock(subreq);
385d4581383SWeston Andros Adamson 
386add42de3STrond Myklebust 		nfs_release_request(old_head);
3875b2b5187STrond Myklebust 
388b66aaa8dSTrond Myklebust 		if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags)) {
389d4581383SWeston Andros Adamson 			nfs_release_request(subreq);
390a6b6d5b8STrond Myklebust 			atomic_long_dec(&NFS_I(inode)->nrequests);
391d4581383SWeston Andros Adamson 		}
3925b2b5187STrond Myklebust 
3935b2b5187STrond Myklebust 		/* subreq is now totally disconnected from page group or any
3945b2b5187STrond Myklebust 		 * write / commit lists. last chance to wake any waiters */
3955b2b5187STrond Myklebust 		nfs_unlock_and_release_request(subreq);
396d4581383SWeston Andros Adamson 	}
397d4581383SWeston Andros Adamson }
398d4581383SWeston Andros Adamson 
399d4581383SWeston Andros Adamson /*
400e00ed89dSTrond Myklebust  * nfs_join_page_group - destroy subrequests of the head req
401e00ed89dSTrond Myklebust  * @head: the page used to lookup the "page group" of nfs_page structures
402e00ed89dSTrond Myklebust  * @inode: Inode to which the request belongs.
403d4581383SWeston Andros Adamson  *
404d4581383SWeston Andros Adamson  * This function joins all sub requests to the head request by first
405d4581383SWeston Andros Adamson  * locking all requests in the group, cancelling any pending operations
406d4581383SWeston Andros Adamson  * and finally updating the head request to cover the whole range covered by
407d4581383SWeston Andros Adamson  * the (former) group.  All subrequests are removed from any write or commit
408d4581383SWeston Andros Adamson  * lists, unlinked from the group and destroyed.
409d4581383SWeston Andros Adamson  */
410b193a78dSTrond Myklebust void nfs_join_page_group(struct nfs_page *head, struct nfs_commit_info *cinfo,
411b193a78dSTrond Myklebust 			 struct inode *inode)
412d4581383SWeston Andros Adamson {
413e00ed89dSTrond Myklebust 	struct nfs_page *subreq;
414d4581383SWeston Andros Adamson 	struct nfs_page *destroy_list = NULL;
415a62f8e3bSTrond Myklebust 	unsigned int pgbase, off, bytes;
416a62f8e3bSTrond Myklebust 
417a62f8e3bSTrond Myklebust 	pgbase = head->wb_pgbase;
418a62f8e3bSTrond Myklebust 	bytes = head->wb_bytes;
419a62f8e3bSTrond Myklebust 	off = head->wb_offset;
420a0e265bcSTrond Myklebust 	for (subreq = head->wb_this_page; subreq != head;
421a0e265bcSTrond Myklebust 			subreq = subreq->wb_this_page) {
422a62f8e3bSTrond Myklebust 		/* Subrequests should always form a contiguous range */
423a62f8e3bSTrond Myklebust 		if (pgbase > subreq->wb_pgbase) {
424a62f8e3bSTrond Myklebust 			off -= pgbase - subreq->wb_pgbase;
425a62f8e3bSTrond Myklebust 			bytes += pgbase - subreq->wb_pgbase;
426a62f8e3bSTrond Myklebust 			pgbase = subreq->wb_pgbase;
427a62f8e3bSTrond Myklebust 		}
428a62f8e3bSTrond Myklebust 		bytes = max(subreq->wb_pgbase + subreq->wb_bytes
429a62f8e3bSTrond Myklebust 				- pgbase, bytes);
4301bd5d6d0STrond Myklebust 	}
4311bd5d6d0STrond Myklebust 
432a62f8e3bSTrond Myklebust 	/* Set the head request's range to cover the former page group */
433a62f8e3bSTrond Myklebust 	head->wb_pgbase = pgbase;
434a62f8e3bSTrond Myklebust 	head->wb_bytes = bytes;
435a62f8e3bSTrond Myklebust 	head->wb_offset = off;
436d4581383SWeston Andros Adamson 
437d4581383SWeston Andros Adamson 	/* Now that all requests are locked, make sure they aren't on any list.
438d4581383SWeston Andros Adamson 	 * Commit list removal accounting is done after locks are dropped */
439d4581383SWeston Andros Adamson 	subreq = head;
440d4581383SWeston Andros Adamson 	do {
441b193a78dSTrond Myklebust 		nfs_clear_request_commit(cinfo, subreq);
442d4581383SWeston Andros Adamson 		subreq = subreq->wb_this_page;
443d4581383SWeston Andros Adamson 	} while (subreq != head);
444d4581383SWeston Andros Adamson 
445d4581383SWeston Andros Adamson 	/* unlink subrequests from head, destroy them later */
446d4581383SWeston Andros Adamson 	if (head->wb_this_page != head) {
447d4581383SWeston Andros Adamson 		/* destroy list will be terminated by head */
448d4581383SWeston Andros Adamson 		destroy_list = head->wb_this_page;
449d4581383SWeston Andros Adamson 		head->wb_this_page = head;
450d4581383SWeston Andros Adamson 	}
451d4581383SWeston Andros Adamson 
452b66aaa8dSTrond Myklebust 	nfs_destroy_unlinked_subrequests(destroy_list, head, inode);
453b5bab9bfSTrond Myklebust }
454b5bab9bfSTrond Myklebust 
455f1b7c755SChristoph Hellwig /**
456f1b7c755SChristoph Hellwig  * nfs_wait_on_request - Wait for a request to complete.
457f1b7c755SChristoph Hellwig  * @req: request to wait upon.
458f1b7c755SChristoph Hellwig  *
459f1b7c755SChristoph Hellwig  * Interruptible by fatal signals only.
460f1b7c755SChristoph Hellwig  * The user is responsible for holding a count on the request.
461f1b7c755SChristoph Hellwig  */
462f1b7c755SChristoph Hellwig static int nfs_wait_on_request(struct nfs_page *req)
463f1b7c755SChristoph Hellwig {
464f1b7c755SChristoph Hellwig 	if (!test_bit(PG_BUSY, &req->wb_flags))
465f1b7c755SChristoph Hellwig 		return 0;
466f1b7c755SChristoph Hellwig 	set_bit(PG_CONTENDED2, &req->wb_flags);
467f1b7c755SChristoph Hellwig 	smp_mb__after_atomic();
468f1b7c755SChristoph Hellwig 	return wait_on_bit_io(&req->wb_flags, PG_BUSY,
469f1b7c755SChristoph Hellwig 			      TASK_UNINTERRUPTIBLE);
470f1b7c755SChristoph Hellwig }
471f1b7c755SChristoph Hellwig 
472e00ed89dSTrond Myklebust /*
47325edbcacSChristoph Hellwig  * nfs_unroll_locks -  unlock all newly locked reqs and wait on @req
47425edbcacSChristoph Hellwig  * @head: head request of page group, must be holding head lock
47525edbcacSChristoph Hellwig  * @req: request that couldn't lock and needs to wait on the req bit lock
47625edbcacSChristoph Hellwig  *
47725edbcacSChristoph Hellwig  * This is a helper function for nfs_lock_and_join_requests
47825edbcacSChristoph Hellwig  * returns 0 on success, < 0 on error.
47925edbcacSChristoph Hellwig  */
48025edbcacSChristoph Hellwig static void
48125edbcacSChristoph Hellwig nfs_unroll_locks(struct nfs_page *head, struct nfs_page *req)
48225edbcacSChristoph Hellwig {
48325edbcacSChristoph Hellwig 	struct nfs_page *tmp;
48425edbcacSChristoph Hellwig 
48525edbcacSChristoph Hellwig 	/* relinquish all the locks successfully grabbed this run */
48625edbcacSChristoph Hellwig 	for (tmp = head->wb_this_page ; tmp != req; tmp = tmp->wb_this_page) {
48725edbcacSChristoph Hellwig 		if (!kref_read(&tmp->wb_kref))
48825edbcacSChristoph Hellwig 			continue;
48925edbcacSChristoph Hellwig 		nfs_unlock_and_release_request(tmp);
49025edbcacSChristoph Hellwig 	}
49125edbcacSChristoph Hellwig }
49225edbcacSChristoph Hellwig 
49325edbcacSChristoph Hellwig /*
49425edbcacSChristoph Hellwig  * nfs_page_group_lock_subreq -  try to lock a subrequest
49525edbcacSChristoph Hellwig  * @head: head request of page group
49625edbcacSChristoph Hellwig  * @subreq: request to lock
49725edbcacSChristoph Hellwig  *
49825edbcacSChristoph Hellwig  * This is a helper function for nfs_lock_and_join_requests which
49925edbcacSChristoph Hellwig  * must be called with the head request and page group both locked.
50025edbcacSChristoph Hellwig  * On error, it returns with the page group unlocked.
50125edbcacSChristoph Hellwig  */
50225edbcacSChristoph Hellwig static int
50325edbcacSChristoph Hellwig nfs_page_group_lock_subreq(struct nfs_page *head, struct nfs_page *subreq)
50425edbcacSChristoph Hellwig {
50525edbcacSChristoph Hellwig 	int ret;
50625edbcacSChristoph Hellwig 
50725edbcacSChristoph Hellwig 	if (!kref_get_unless_zero(&subreq->wb_kref))
50825edbcacSChristoph Hellwig 		return 0;
50925edbcacSChristoph Hellwig 	while (!nfs_lock_request(subreq)) {
51025edbcacSChristoph Hellwig 		nfs_page_group_unlock(head);
51125edbcacSChristoph Hellwig 		ret = nfs_wait_on_request(subreq);
51225edbcacSChristoph Hellwig 		if (!ret)
51325edbcacSChristoph Hellwig 			ret = nfs_page_group_lock(head);
51425edbcacSChristoph Hellwig 		if (ret < 0) {
51525edbcacSChristoph Hellwig 			nfs_unroll_locks(head, subreq);
51625edbcacSChristoph Hellwig 			nfs_release_request(subreq);
51725edbcacSChristoph Hellwig 			return ret;
51825edbcacSChristoph Hellwig 		}
51925edbcacSChristoph Hellwig 	}
52025edbcacSChristoph Hellwig 	return 0;
52125edbcacSChristoph Hellwig }
52225edbcacSChristoph Hellwig 
523e00ed89dSTrond Myklebust /*
524e00ed89dSTrond Myklebust  * nfs_lock_and_join_requests - join all subreqs to the head req
5250c493b5cSTrond Myklebust  * @folio: the folio used to lookup the "page group" of nfs_page structures
526e00ed89dSTrond Myklebust  *
527e00ed89dSTrond Myklebust  * This function joins all sub requests to the head request by first
528e00ed89dSTrond Myklebust  * locking all requests in the group, cancelling any pending operations
529e00ed89dSTrond Myklebust  * and finally updating the head request to cover the whole range covered by
530e00ed89dSTrond Myklebust  * the (former) group.  All subrequests are removed from any write or commit
531e00ed89dSTrond Myklebust  * lists, unlinked from the group and destroyed.
532e00ed89dSTrond Myklebust  *
533e00ed89dSTrond Myklebust  * Returns a locked, referenced pointer to the head request - which after
534e00ed89dSTrond Myklebust  * this call is guaranteed to be the only request associated with the page.
5350c493b5cSTrond Myklebust  * Returns NULL if no requests are found for @folio, or a ERR_PTR if an
536e00ed89dSTrond Myklebust  * error was encountered.
537e00ed89dSTrond Myklebust  */
5380c493b5cSTrond Myklebust static struct nfs_page *nfs_lock_and_join_requests(struct folio *folio)
539e00ed89dSTrond Myklebust {
5407e8e78a0SChristoph Hellwig 	struct inode *inode = folio->mapping->host;
54125edbcacSChristoph Hellwig 	struct nfs_page *head, *subreq;
542b193a78dSTrond Myklebust 	struct nfs_commit_info cinfo;
543b571cfcbSChristoph Hellwig 	bool removed;
544e00ed89dSTrond Myklebust 	int ret;
545e00ed89dSTrond Myklebust 
546e00ed89dSTrond Myklebust 	/*
547e00ed89dSTrond Myklebust 	 * A reference is taken only on the head request which acts as a
548e00ed89dSTrond Myklebust 	 * reference to the whole page group - the group will not be destroyed
549e00ed89dSTrond Myklebust 	 * until the head reference is released.
550e00ed89dSTrond Myklebust 	 */
551c3f22357SChristoph Hellwig retry:
552c3f22357SChristoph Hellwig 	head = nfs_folio_find_head_request(folio);
553c3f22357SChristoph Hellwig 	if (!head)
554c3f22357SChristoph Hellwig 		return NULL;
5550671d8f1SMarkus Elfring 
556c3f22357SChristoph Hellwig 	while (!nfs_lock_request(head)) {
557c3f22357SChristoph Hellwig 		ret = nfs_wait_on_request(head);
558c3f22357SChristoph Hellwig 		if (ret < 0)
5590671d8f1SMarkus Elfring 			return ERR_PTR(ret);
560612c9384STrond Myklebust 	}
561074cc1deSTrond Myklebust 
562c3f22357SChristoph Hellwig 	/* Ensure that nobody removed the request before we locked it */
563c3f22357SChristoph Hellwig 	if (head != folio->private) {
564c3f22357SChristoph Hellwig 		nfs_unlock_and_release_request(head);
565c3f22357SChristoph Hellwig 		goto retry;
566c3f22357SChristoph Hellwig 	}
567e00ed89dSTrond Myklebust 
56825edbcacSChristoph Hellwig 	ret = nfs_page_group_lock(head);
569c3f22357SChristoph Hellwig 	if (ret < 0)
570c3f22357SChristoph Hellwig 		goto out_unlock;
571c3f22357SChristoph Hellwig 
572b571cfcbSChristoph Hellwig 	removed = test_bit(PG_REMOVE, &head->wb_flags);
573b571cfcbSChristoph Hellwig 
57425edbcacSChristoph Hellwig 	/* lock each request in the page group */
57525edbcacSChristoph Hellwig 	for (subreq = head->wb_this_page;
57625edbcacSChristoph Hellwig 	     subreq != head;
57725edbcacSChristoph Hellwig 	     subreq = subreq->wb_this_page) {
578b571cfcbSChristoph Hellwig 		if (test_bit(PG_REMOVE, &subreq->wb_flags))
579b571cfcbSChristoph Hellwig 			removed = true;
58025edbcacSChristoph Hellwig 		ret = nfs_page_group_lock_subreq(head, subreq);
58125edbcacSChristoph Hellwig 		if (ret < 0)
58225edbcacSChristoph Hellwig 			goto out_unlock;
58325edbcacSChristoph Hellwig 	}
58425edbcacSChristoph Hellwig 
58525edbcacSChristoph Hellwig 	nfs_page_group_unlock(head);
58625edbcacSChristoph Hellwig 
587b571cfcbSChristoph Hellwig 	/*
588b571cfcbSChristoph Hellwig 	 * If PG_REMOVE is set on any request, I/O on that request has
589b571cfcbSChristoph Hellwig 	 * completed, but some requests were still under I/O at the time
590b571cfcbSChristoph Hellwig 	 * we locked the head request.
591b571cfcbSChristoph Hellwig 	 *
592b571cfcbSChristoph Hellwig 	 * In that case the above wait for all requests means that all I/O
593b571cfcbSChristoph Hellwig 	 * has now finished, and we can restart from a clean slate.  Let the
594b571cfcbSChristoph Hellwig 	 * old requests go away and start from scratch instead.
595b571cfcbSChristoph Hellwig 	 */
596b571cfcbSChristoph Hellwig 	if (removed) {
597b571cfcbSChristoph Hellwig 		nfs_unroll_locks(head, head);
598b571cfcbSChristoph Hellwig 		nfs_unlock_and_release_request(head);
599b571cfcbSChristoph Hellwig 		goto retry;
600b571cfcbSChristoph Hellwig 	}
601b571cfcbSChristoph Hellwig 
602c3f22357SChristoph Hellwig 	nfs_init_cinfo_from_inode(&cinfo, inode);
603c3f22357SChristoph Hellwig 	nfs_join_page_group(head, &cinfo, inode);
604e00ed89dSTrond Myklebust 	return head;
605c3f22357SChristoph Hellwig 
606c3f22357SChristoph Hellwig out_unlock:
607c3f22357SChristoph Hellwig 	nfs_unlock_and_release_request(head);
608c3f22357SChristoph Hellwig 	return ERR_PTR(ret);
609e00ed89dSTrond Myklebust }
610e00ed89dSTrond Myklebust 
6116fbda89bSTrond Myklebust static void nfs_write_error(struct nfs_page *req, int error)
6120bcbf039SPeng Tao {
6136dd85e83STrond Myklebust 	trace_nfs_write_error(nfs_page_to_inode(req), req, error);
6140c493b5cSTrond Myklebust 	nfs_mapping_set_error(nfs_page_to_folio(req), error);
61506c9fdf3STrond Myklebust 	nfs_inode_remove_request(req);
6160c493b5cSTrond Myklebust 	nfs_page_end_writeback(req);
6171f84ccdfSFred Isaman 	nfs_release_request(req);
6180bcbf039SPeng Tao }
6190bcbf039SPeng Tao 
620074cc1deSTrond Myklebust /*
621074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
622074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
623074cc1deSTrond Myklebust  */
6240c493b5cSTrond Myklebust static int nfs_page_async_flush(struct folio *folio,
625c6fd3511STrond Myklebust 				struct writeback_control *wbc,
626c6fd3511STrond Myklebust 				struct nfs_pageio_descriptor *pgio)
627074cc1deSTrond Myklebust {
628074cc1deSTrond Myklebust 	struct nfs_page *req;
629074cc1deSTrond Myklebust 	int ret = 0;
630074cc1deSTrond Myklebust 
6310c493b5cSTrond Myklebust 	req = nfs_lock_and_join_requests(folio);
632074cc1deSTrond Myklebust 	if (!req)
633074cc1deSTrond Myklebust 		goto out;
634074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
635074cc1deSTrond Myklebust 	if (IS_ERR(req))
636074cc1deSTrond Myklebust 		goto out;
637074cc1deSTrond Myklebust 
6380c493b5cSTrond Myklebust 	nfs_folio_set_writeback(folio);
639deed85e7STrond Myklebust 	WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
640074cc1deSTrond Myklebust 
641a6598813STrond Myklebust 	/* If there is a fatal error that covers this write, just exit */
64296c41455STrond Myklebust 	ret = pgio->pg_error;
64396c41455STrond Myklebust 	if (nfs_error_is_fatal_on_server(ret))
644a6598813STrond Myklebust 		goto out_launder;
645a6598813STrond Myklebust 
64696c41455STrond Myklebust 	ret = 0;
647f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
648074cc1deSTrond Myklebust 		ret = pgio->pg_error;
6490bcbf039SPeng Tao 		/*
650c373fff7STrond Myklebust 		 * Remove the problematic req upon fatal errors on the server
6510bcbf039SPeng Tao 		 */
652c373fff7STrond Myklebust 		if (nfs_error_is_fatal_on_server(ret))
653a6598813STrond Myklebust 			goto out_launder;
654c6fd3511STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE)
655c6fd3511STrond Myklebust 			ret = AOP_WRITEPAGE_ACTIVATE;
6560c493b5cSTrond Myklebust 		folio_redirty_for_writepage(wbc, folio);
6578fc75bedSTrond Myklebust 		nfs_redirty_request(req);
65896c41455STrond Myklebust 		pgio->pg_error = 0;
65940f90271STrond Myklebust 	} else
6607e8e78a0SChristoph Hellwig 		nfs_add_stats(folio->mapping->host,
66140f90271STrond Myklebust 			      NFSIOS_WRITEPAGES, 1);
662074cc1deSTrond Myklebust out:
663074cc1deSTrond Myklebust 	return ret;
664a6598813STrond Myklebust out_launder:
6656fbda89bSTrond Myklebust 	nfs_write_error(req, ret);
66614bebe3cSTrond Myklebust 	return 0;
667e261f51fSTrond Myklebust }
668e261f51fSTrond Myklebust 
6690c493b5cSTrond Myklebust static int nfs_do_writepage(struct folio *folio, struct writeback_control *wbc,
670c373fff7STrond Myklebust 			    struct nfs_pageio_descriptor *pgio)
671f758c885STrond Myklebust {
6727e8e78a0SChristoph Hellwig 	nfs_pageio_cond_complete(pgio, folio->index);
6730c493b5cSTrond Myklebust 	return nfs_page_async_flush(folio, wbc, pgio);
674f758c885STrond Myklebust }
675f758c885STrond Myklebust 
676e261f51fSTrond Myklebust /*
6771da177e4SLinus Torvalds  * Write an mmapped page to the server.
6781da177e4SLinus Torvalds  */
6790c493b5cSTrond Myklebust static int nfs_writepage_locked(struct folio *folio,
680c373fff7STrond Myklebust 				struct writeback_control *wbc)
6811da177e4SLinus Torvalds {
682f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
6837e8e78a0SChristoph Hellwig 	struct inode *inode = folio->mapping->host;
684e261f51fSTrond Myklebust 	int err;
6851da177e4SLinus Torvalds 
68640f90271STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
6870c493b5cSTrond Myklebust 	nfs_pageio_init_write(&pgio, inode, 0, false,
6880c493b5cSTrond Myklebust 			      &nfs_async_write_completion_ops);
6890c493b5cSTrond Myklebust 	err = nfs_do_writepage(folio, wbc, &pgio);
69096c41455STrond Myklebust 	pgio.pg_error = 0;
691f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
6924d770ccfSTrond Myklebust 	return err;
6934d770ccfSTrond Myklebust }
6944d770ccfSTrond Myklebust 
695d585bdbeSMatthew Wilcox (Oracle) static int nfs_writepages_callback(struct folio *folio,
6960c493b5cSTrond Myklebust 				   struct writeback_control *wbc, void *data)
697f758c885STrond Myklebust {
698f758c885STrond Myklebust 	int ret;
699f758c885STrond Myklebust 
7000c493b5cSTrond Myklebust 	ret = nfs_do_writepage(folio, wbc, data);
70196c41455STrond Myklebust 	if (ret != AOP_WRITEPAGE_ACTIVATE)
702d585bdbeSMatthew Wilcox (Oracle) 		folio_unlock(folio);
703f758c885STrond Myklebust 	return ret;
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
706919e3bd9STrond Myklebust static void nfs_io_completion_commit(void *inode)
707919e3bd9STrond Myklebust {
708919e3bd9STrond Myklebust 	nfs_commit_inode(inode, 0);
709919e3bd9STrond Myklebust }
710919e3bd9STrond Myklebust 
7111da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
7121da177e4SLinus Torvalds {
7131da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
714c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
715ed7bcdb3STrond Myklebust 	struct nfs_io_completion *ioc = NULL;
716ed7bcdb3STrond Myklebust 	unsigned int mntflags = NFS_SERVER(inode)->flags;
7172f1f3104SJan Kara 	struct nfs_server *nfss = NFS_SERVER(inode);
718ed7bcdb3STrond Myklebust 	int priority = 0;
7191da177e4SLinus Torvalds 	int err;
7201da177e4SLinus Torvalds 
7212f1f3104SJan Kara 	/* Wait with writeback until write congestion eases */
7222f1f3104SJan Kara 	if (wbc->sync_mode == WB_SYNC_NONE && nfss->write_congested) {
7232f1f3104SJan Kara 		err = wait_event_killable(nfss->write_congestion_wait,
7242f1f3104SJan Kara 					  nfss->write_congested == 0);
7252f1f3104SJan Kara 		if (err)
7262f1f3104SJan Kara 			return err;
7272f1f3104SJan Kara 	}
7286df25e58SNeilBrown 
72991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
73091d5b470SChuck Lever 
731ed7bcdb3STrond Myklebust 	if (!(mntflags & NFS_MOUNT_WRITE_EAGER) || wbc->for_kupdate ||
732ed7bcdb3STrond Myklebust 	    wbc->for_background || wbc->for_sync || wbc->for_reclaim) {
7332b17d725STrond Myklebust 		ioc = nfs_io_completion_alloc(GFP_KERNEL);
734919e3bd9STrond Myklebust 		if (ioc)
735ed7bcdb3STrond Myklebust 			nfs_io_completion_init(ioc, nfs_io_completion_commit,
736ed7bcdb3STrond Myklebust 					       inode);
737ed7bcdb3STrond Myklebust 		priority = wb_priority(wbc);
738ed7bcdb3STrond Myklebust 	}
739919e3bd9STrond Myklebust 
740c6fd3511STrond Myklebust 	do {
741ed7bcdb3STrond Myklebust 		nfs_pageio_init_write(&pgio, inode, priority, false,
742a20c93e3SChristoph Hellwig 				      &nfs_async_write_completion_ops);
743919e3bd9STrond Myklebust 		pgio.pg_io_completion = ioc;
744c6fd3511STrond Myklebust 		err = write_cache_pages(mapping, wbc, nfs_writepages_callback,
745c6fd3511STrond Myklebust 					&pgio);
74696c41455STrond Myklebust 		pgio.pg_error = 0;
747c63c7b05STrond Myklebust 		nfs_pageio_complete(&pgio);
7486e7434abSTrond Myklebust 		if (err == -EAGAIN && mntflags & NFS_MOUNT_SOFTERR)
7496e7434abSTrond Myklebust 			break;
750c6fd3511STrond Myklebust 	} while (err < 0 && !nfs_error_is_fatal(err));
751919e3bd9STrond Myklebust 	nfs_io_completion_put(ioc);
75272cb77f4STrond Myklebust 
753f758c885STrond Myklebust 	if (err < 0)
75472cb77f4STrond Myklebust 		goto out_err;
755c63c7b05STrond Myklebust 	return 0;
75672cb77f4STrond Myklebust out_err:
75772cb77f4STrond Myklebust 	return err;
7581da177e4SLinus Torvalds }
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds /*
7611da177e4SLinus Torvalds  * Insert a write request into an inode
7621da177e4SLinus Torvalds  */
7630c493b5cSTrond Myklebust static void nfs_inode_add_request(struct nfs_page *req)
7641da177e4SLinus Torvalds {
7650c493b5cSTrond Myklebust 	struct folio *folio = nfs_page_to_folio(req);
7667e8e78a0SChristoph Hellwig 	struct address_space *mapping = folio->mapping;
7670c493b5cSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(mapping->host);
768e7d39069STrond Myklebust 
7692bfc6e56SWeston Andros Adamson 	WARN_ON_ONCE(req->wb_this_page != req);
7702bfc6e56SWeston Andros Adamson 
771e7d39069STrond Myklebust 	/* Lock the request! */
7727ad84aa9STrond Myklebust 	nfs_lock_request(req);
773600f111eSMatthew Wilcox (Oracle) 	spin_lock(&mapping->i_private_lock);
7742df485a7STrond Myklebust 	set_bit(PG_MAPPED, &req->wb_flags);
77503e02b94SZhaoyang Huang 	folio_attach_private(folio, req);
776600f111eSMatthew Wilcox (Oracle) 	spin_unlock(&mapping->i_private_lock);
777a6b6d5b8STrond Myklebust 	atomic_long_inc(&nfsi->nrequests);
77817089a29SWeston Andros Adamson 	/* this a head request for a page group - mark it as having an
779cb1410c7SWeston Andros Adamson 	 * extra reference so sub groups can follow suit.
780cb1410c7SWeston Andros Adamson 	 * This flag also informs pgio layer when to bump nrequests when
781cb1410c7SWeston Andros Adamson 	 * adding subrequests. */
78217089a29SWeston Andros Adamson 	WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags));
783c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds /*
78789a09141SPeter Zijlstra  * Remove a write request from an inode
7881da177e4SLinus Torvalds  */
7891da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
7901da177e4SLinus Torvalds {
7916a6d4644SScott Mayhew 	struct nfs_inode *nfsi = NFS_I(nfs_page_to_inode(req));
7926a6d4644SScott Mayhew 
79320633f04SWeston Andros Adamson 	if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) {
7940c493b5cSTrond Myklebust 		struct folio *folio = nfs_page_to_folio(req->wb_head);
7957e8e78a0SChristoph Hellwig 		struct address_space *mapping = folio->mapping;
7961da177e4SLinus Torvalds 
797600f111eSMatthew Wilcox (Oracle) 		spin_lock(&mapping->i_private_lock);
7987e8e78a0SChristoph Hellwig 		if (likely(folio)) {
79903e02b94SZhaoyang Huang 			folio_detach_private(folio);
8000c493b5cSTrond Myklebust 			clear_bit(PG_MAPPED, &req->wb_head->wb_flags);
80129418aa4SMel Gorman 		}
802600f111eSMatthew Wilcox (Oracle) 		spin_unlock(&mapping->i_private_lock);
80320633f04SWeston Andros Adamson 	}
80417089a29SWeston Andros Adamson 
80533ea5aaaSZhangXiaoxu 	if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags)) {
8066a6d4644SScott Mayhew 		atomic_long_dec(&nfsi->nrequests);
807dd1b2026SJeff Layton 		nfs_release_request(req);
80833ea5aaaSZhangXiaoxu 	}
8091da177e4SLinus Torvalds }
8101da177e4SLinus Torvalds 
8110c493b5cSTrond Myklebust static void nfs_mark_request_dirty(struct nfs_page *req)
81261822ab5STrond Myklebust {
8130c493b5cSTrond Myklebust 	struct folio *folio = nfs_page_to_folio(req);
8140c493b5cSTrond Myklebust 	if (folio)
8150c493b5cSTrond Myklebust 		filemap_dirty_folio(folio_mapping(folio), folio);
81661822ab5STrond Myklebust }
81761822ab5STrond Myklebust 
8188dd37758STrond Myklebust /**
81986d80f97STrond Myklebust  * nfs_request_add_commit_list_locked - add request to a commit list
82086d80f97STrond Myklebust  * @req: pointer to a struct nfs_page
82186d80f97STrond Myklebust  * @dst: commit list head
82286d80f97STrond Myklebust  * @cinfo: holds list lock and accounting info
82386d80f97STrond Myklebust  *
82486d80f97STrond Myklebust  * This sets the PG_CLEAN bit, updates the cinfo count of
82586d80f97STrond Myklebust  * number of outstanding requests requiring a commit as well as
82686d80f97STrond Myklebust  * the MM page stats.
82786d80f97STrond Myklebust  *
828e824f99aSTrond Myklebust  * The caller must hold NFS_I(cinfo->inode)->commit_mutex, and the
829e824f99aSTrond Myklebust  * nfs_page lock.
83086d80f97STrond Myklebust  */
83186d80f97STrond Myklebust void
83286d80f97STrond Myklebust nfs_request_add_commit_list_locked(struct nfs_page *req, struct list_head *dst,
83386d80f97STrond Myklebust 			    struct nfs_commit_info *cinfo)
83486d80f97STrond Myklebust {
83586d80f97STrond Myklebust 	set_bit(PG_CLEAN, &req->wb_flags);
83686d80f97STrond Myklebust 	nfs_list_add_request(req, dst);
8375cb953d4STrond Myklebust 	atomic_long_inc(&cinfo->mds->ncommit);
83886d80f97STrond Myklebust }
83986d80f97STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked);
84086d80f97STrond Myklebust 
84186d80f97STrond Myklebust /**
8428dd37758STrond Myklebust  * nfs_request_add_commit_list - add request to a commit list
8438dd37758STrond Myklebust  * @req: pointer to a struct nfs_page
844ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
8458dd37758STrond Myklebust  *
846ea2cf228SFred Isaman  * This sets the PG_CLEAN bit, updates the cinfo count of
8478dd37758STrond Myklebust  * number of outstanding requests requiring a commit as well as
8488dd37758STrond Myklebust  * the MM page stats.
8498dd37758STrond Myklebust  *
850ea2cf228SFred Isaman  * The caller must _not_ hold the cinfo->lock, but must be
8518dd37758STrond Myklebust  * holding the nfs_page lock.
8528dd37758STrond Myklebust  */
8538dd37758STrond Myklebust void
8546272dcc6SAnna Schumaker nfs_request_add_commit_list(struct nfs_page *req, struct nfs_commit_info *cinfo)
8558dd37758STrond Myklebust {
856e824f99aSTrond Myklebust 	mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
8576272dcc6SAnna Schumaker 	nfs_request_add_commit_list_locked(req, &cinfo->mds->list, cinfo);
858e824f99aSTrond Myklebust 	mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
8590c493b5cSTrond Myklebust 	nfs_folio_mark_unstable(nfs_page_to_folio(req), cinfo);
8608dd37758STrond Myklebust }
8618dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
8628dd37758STrond Myklebust 
8638dd37758STrond Myklebust /**
8648dd37758STrond Myklebust  * nfs_request_remove_commit_list - Remove request from a commit list
8658dd37758STrond Myklebust  * @req: pointer to a nfs_page
866ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
8678dd37758STrond Myklebust  *
868ea2cf228SFred Isaman  * This clears the PG_CLEAN bit, and updates the cinfo's count of
8698dd37758STrond Myklebust  * number of outstanding requests requiring a commit
8708dd37758STrond Myklebust  * It does not update the MM page stats.
8718dd37758STrond Myklebust  *
872ea2cf228SFred Isaman  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
8738dd37758STrond Myklebust  */
8748dd37758STrond Myklebust void
875ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req,
876ea2cf228SFred Isaman 			       struct nfs_commit_info *cinfo)
8778dd37758STrond Myklebust {
8788dd37758STrond Myklebust 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
8798dd37758STrond Myklebust 		return;
8808dd37758STrond Myklebust 	nfs_list_remove_request(req);
8815cb953d4STrond Myklebust 	atomic_long_dec(&cinfo->mds->ncommit);
8828dd37758STrond Myklebust }
8838dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
8848dd37758STrond Myklebust 
885ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
886ea2cf228SFred Isaman 				      struct inode *inode)
887ea2cf228SFred Isaman {
888fe238e60SDave Wysochanski 	cinfo->inode = inode;
889ea2cf228SFred Isaman 	cinfo->mds = &NFS_I(inode)->commit_info;
890ea2cf228SFred Isaman 	cinfo->ds = pnfs_get_ds_info(inode);
891b359f9d0SFred Isaman 	cinfo->dreq = NULL;
892f453a54aSFred Isaman 	cinfo->completion_ops = &nfs_commit_completion_ops;
893ea2cf228SFred Isaman }
894ea2cf228SFred Isaman 
895ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
896ea2cf228SFred Isaman 		    struct inode *inode,
897ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq)
898ea2cf228SFred Isaman {
8991763da12SFred Isaman 	if (dreq)
9001763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
9011763da12SFred Isaman 	else
902ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(cinfo, inode);
903ea2cf228SFred Isaman }
904ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo);
9058dd37758STrond Myklebust 
9061da177e4SLinus Torvalds /*
9071da177e4SLinus Torvalds  * Add a request to the inode's commit list.
9081da177e4SLinus Torvalds  */
9091763da12SFred Isaman void
910ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
911b57ff130SWeston Andros Adamson 			struct nfs_commit_info *cinfo, u32 ds_commit_idx)
9121da177e4SLinus Torvalds {
913b57ff130SWeston Andros Adamson 	if (pnfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx))
9148dd37758STrond Myklebust 		return;
9156272dcc6SAnna Schumaker 	nfs_request_add_commit_list(req, cinfo);
9161da177e4SLinus Torvalds }
9178e821cadSTrond Myklebust 
9180c493b5cSTrond Myklebust static void nfs_folio_clear_commit(struct folio *folio)
919e468bae9STrond Myklebust {
9200c493b5cSTrond Myklebust 	if (folio) {
9210c493b5cSTrond Myklebust 		long nr = folio_nr_pages(folio);
9220c493b5cSTrond Myklebust 
9230c493b5cSTrond Myklebust 		node_stat_mod_folio(folio, NR_WRITEBACK, -nr);
9247e8e78a0SChristoph Hellwig 		wb_stat_mod(&inode_to_bdi(folio->mapping->host)->wb,
9250c493b5cSTrond Myklebust 			    WB_WRITEBACK, -nr);
9260c493b5cSTrond Myklebust 	}
927e468bae9STrond Myklebust }
928d6d6dc7cSFred Isaman 
929b5bab9bfSTrond Myklebust /* Called holding the request lock on @req */
930b193a78dSTrond Myklebust static void nfs_clear_request_commit(struct nfs_commit_info *cinfo,
931b193a78dSTrond Myklebust 				     struct nfs_page *req)
932d6d6dc7cSFred Isaman {
9338dd37758STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
9349fcd5960STrond Myklebust 		struct nfs_open_context *ctx = nfs_req_openctx(req);
9359fcd5960STrond Myklebust 		struct inode *inode = d_inode(ctx->dentry);
936d6d6dc7cSFred Isaman 
937e824f99aSTrond Myklebust 		mutex_lock(&NFS_I(inode)->commit_mutex);
938b193a78dSTrond Myklebust 		if (!pnfs_clear_request_commit(req, cinfo)) {
939b193a78dSTrond Myklebust 			nfs_request_remove_commit_list(req, cinfo);
940d6d6dc7cSFred Isaman 		}
941e824f99aSTrond Myklebust 		mutex_unlock(&NFS_I(inode)->commit_mutex);
9420c493b5cSTrond Myklebust 		nfs_folio_clear_commit(nfs_page_to_folio(req));
9438dd37758STrond Myklebust 	}
944e468bae9STrond Myklebust }
945e468bae9STrond Myklebust 
946d45f60c6SWeston Andros Adamson int nfs_write_need_commit(struct nfs_pgio_header *hdr)
9478e821cadSTrond Myklebust {
948c65e6254SWeston Andros Adamson 	if (hdr->verf.committed == NFS_DATA_SYNC)
949d45f60c6SWeston Andros Adamson 		return hdr->lseg == NULL;
950c65e6254SWeston Andros Adamson 	return hdr->verf.committed != NFS_FILE_SYNC;
9518e821cadSTrond Myklebust }
9528e821cadSTrond Myklebust 
953919e3bd9STrond Myklebust static void nfs_async_write_init(struct nfs_pgio_header *hdr)
954919e3bd9STrond Myklebust {
955919e3bd9STrond Myklebust 	nfs_io_completion_get(hdr->io_completion);
956919e3bd9STrond Myklebust }
957919e3bd9STrond Myklebust 
958061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
9596c75dc0dSFred Isaman {
960ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
9616c75dc0dSFred Isaman 	unsigned long bytes = 0;
9626c75dc0dSFred Isaman 
9636c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
9646c75dc0dSFred Isaman 		goto out;
965ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
9666c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
9676c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
9686c75dc0dSFred Isaman 
9696c75dc0dSFred Isaman 		bytes += req->wb_bytes;
9706c75dc0dSFred Isaman 		nfs_list_remove_request(req);
9716c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
9726c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
973af887e43STrond Myklebust 			trace_nfs_comp_error(hdr->inode, req, hdr->error);
9740c493b5cSTrond Myklebust 			nfs_mapping_set_error(nfs_page_to_folio(req),
9750c493b5cSTrond Myklebust 					      hdr->error);
9766c75dc0dSFred Isaman 			goto remove_req;
9776c75dc0dSFred Isaman 		}
978c65e6254SWeston Andros Adamson 		if (nfs_write_need_commit(hdr)) {
97933344e0fSTrond Myklebust 			/* Reset wb_nio, since the write was successful. */
98033344e0fSTrond Myklebust 			req->wb_nio = 0;
981f79d06f5SAnna Schumaker 			memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
982b57ff130SWeston Andros Adamson 			nfs_mark_request_commit(req, hdr->lseg, &cinfo,
983a7d42ddbSWeston Andros Adamson 				hdr->pgio_mirror_idx);
9846c75dc0dSFred Isaman 			goto next;
9856c75dc0dSFred Isaman 		}
9866c75dc0dSFred Isaman remove_req:
9876c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
9886c75dc0dSFred Isaman next:
9890c493b5cSTrond Myklebust 		nfs_page_end_writeback(req);
9903aff4ebbSTrond Myklebust 		nfs_release_request(req);
9916c75dc0dSFred Isaman 	}
9926c75dc0dSFred Isaman out:
993919e3bd9STrond Myklebust 	nfs_io_completion_put(hdr->io_completion);
9946c75dc0dSFred Isaman 	hdr->release(hdr);
9956c75dc0dSFred Isaman }
9966c75dc0dSFred Isaman 
997ce59515cSAnna Schumaker unsigned long
998ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
999fb8a1f11STrond Myklebust {
10005cb953d4STrond Myklebust 	return atomic_long_read(&cinfo->mds->ncommit);
1001fb8a1f11STrond Myklebust }
1002fb8a1f11STrond Myklebust 
1003e824f99aSTrond Myklebust /* NFS_I(cinfo->inode)->commit_mutex held by caller */
10041763da12SFred Isaman int
1005ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
1006ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
1007d6d6dc7cSFred Isaman {
1008137da553STrond Myklebust 	struct nfs_page *req, *tmp;
1009d6d6dc7cSFred Isaman 	int ret = 0;
1010d6d6dc7cSFred Isaman 
1011137da553STrond Myklebust 	list_for_each_entry_safe(req, tmp, src, wb_list) {
10127ad84aa9STrond Myklebust 		kref_get(&req->wb_kref);
10132ce209c4STrond Myklebust 		if (!nfs_lock_request(req)) {
1014137da553STrond Myklebust 			nfs_release_request(req);
1015137da553STrond Myklebust 			continue;
1016137da553STrond Myklebust 		}
1017ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
10185d2a9d9dSTrond Myklebust 		clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
10198dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
1020d6d6dc7cSFred Isaman 		ret++;
10211763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
1022d6d6dc7cSFred Isaman 			break;
1023e824f99aSTrond Myklebust 		cond_resched();
1024d6d6dc7cSFred Isaman 	}
1025d6d6dc7cSFred Isaman 	return ret;
1026d6d6dc7cSFred Isaman }
10275d2a9d9dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_scan_commit_list);
1028d6d6dc7cSFred Isaman 
10291da177e4SLinus Torvalds /*
10301da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
10311da177e4SLinus Torvalds  * @inode: NFS inode to scan
1032ea2cf228SFred Isaman  * @dst: mds destination list
1033ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
10341da177e4SLinus Torvalds  *
10351da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
10361da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
10371da177e4SLinus Torvalds  */
10381763da12SFred Isaman int
1039ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
1040ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
10411da177e4SLinus Torvalds {
1042d6d6dc7cSFred Isaman 	int ret = 0;
1043fb8a1f11STrond Myklebust 
10445cb953d4STrond Myklebust 	if (!atomic_long_read(&cinfo->mds->ncommit))
10455cb953d4STrond Myklebust 		return 0;
1046e824f99aSTrond Myklebust 	mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
10475cb953d4STrond Myklebust 	if (atomic_long_read(&cinfo->mds->ncommit) > 0) {
10488dd37758STrond Myklebust 		const int max = INT_MAX;
1049d6d6dc7cSFred Isaman 
1050ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
1051ea2cf228SFred Isaman 					   cinfo, max);
1052ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
1053d6d6dc7cSFred Isaman 	}
1054e824f99aSTrond Myklebust 	mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
1055ff778d02STrond Myklebust 	return ret;
10561da177e4SLinus Torvalds }
1057d6d6dc7cSFred Isaman 
10581da177e4SLinus Torvalds /*
1059e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
1060e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
10611da177e4SLinus Torvalds  *
1062e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
1063e7d39069STrond Myklebust  * to disk.
10641da177e4SLinus Torvalds  */
10650c493b5cSTrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct folio *folio,
1066e7d39069STrond Myklebust 						  unsigned int offset,
1067e7d39069STrond Myklebust 						  unsigned int bytes)
10681da177e4SLinus Torvalds {
1069e7d39069STrond Myklebust 	struct nfs_page *req;
1070e7d39069STrond Myklebust 	unsigned int rqend;
1071e7d39069STrond Myklebust 	unsigned int end;
10721da177e4SLinus Torvalds 	int error;
1073277459d2STrond Myklebust 
1074e7d39069STrond Myklebust 	end = offset + bytes;
1075e7d39069STrond Myklebust 
10760c493b5cSTrond Myklebust 	req = nfs_lock_and_join_requests(folio);
1077f6032f21STrond Myklebust 	if (IS_ERR_OR_NULL(req))
1078f6032f21STrond Myklebust 		return req;
10792bfc6e56SWeston Andros Adamson 
1080e7d39069STrond Myklebust 	rqend = req->wb_offset + req->wb_bytes;
1081e7d39069STrond Myklebust 	/*
1082e7d39069STrond Myklebust 	 * Tell the caller to flush out the request if
1083e7d39069STrond Myklebust 	 * the offsets are non-contiguous.
1084e7d39069STrond Myklebust 	 * Note: nfs_flush_incompatible() will already
1085e7d39069STrond Myklebust 	 * have flushed out requests having wrong owners.
1086e7d39069STrond Myklebust 	 */
1087f6032f21STrond Myklebust 	if (offset > rqend || end < req->wb_offset)
1088e7d39069STrond Myklebust 		goto out_flushme;
1089e7d39069STrond Myklebust 
10901da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
10911da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
10921da177e4SLinus Torvalds 		req->wb_offset = offset;
10931da177e4SLinus Torvalds 		req->wb_pgbase = offset;
10941da177e4SLinus Torvalds 	}
10951da177e4SLinus Torvalds 	if (end > rqend)
10961da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
1097e7d39069STrond Myklebust 	else
1098e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
109933344e0fSTrond Myklebust 	req->wb_nio = 0;
1100e7d39069STrond Myklebust 	return req;
1101e7d39069STrond Myklebust out_flushme:
1102f6032f21STrond Myklebust 	/*
1103f6032f21STrond Myklebust 	 * Note: we mark the request dirty here because
1104f6032f21STrond Myklebust 	 * nfs_lock_and_join_requests() cannot preserve
1105f6032f21STrond Myklebust 	 * commit flags, so we have to replay the write.
1106f6032f21STrond Myklebust 	 */
1107f6032f21STrond Myklebust 	nfs_mark_request_dirty(req);
1108f6032f21STrond Myklebust 	nfs_unlock_and_release_request(req);
11097e8e78a0SChristoph Hellwig 	error = nfs_wb_folio(folio->mapping->host, folio);
1110f6032f21STrond Myklebust 	return (error < 0) ? ERR_PTR(error) : NULL;
1111e7d39069STrond Myklebust }
11121da177e4SLinus Torvalds 
1113e7d39069STrond Myklebust /*
1114e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
1115e7d39069STrond Myklebust  *
1116e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
1117e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
1118e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
1119e7d39069STrond Myklebust  */
1120e7d39069STrond Myklebust static struct nfs_page *nfs_setup_write_request(struct nfs_open_context *ctx,
11210c493b5cSTrond Myklebust 						struct folio *folio,
11220c493b5cSTrond Myklebust 						unsigned int offset,
11230c493b5cSTrond Myklebust 						unsigned int bytes)
1124e7d39069STrond Myklebust {
1125e7d39069STrond Myklebust 	struct nfs_page *req;
1126e7d39069STrond Myklebust 
11270c493b5cSTrond Myklebust 	req = nfs_try_to_update_request(folio, offset, bytes);
1128e7d39069STrond Myklebust 	if (req != NULL)
1129e7d39069STrond Myklebust 		goto out;
11300c493b5cSTrond Myklebust 	req = nfs_page_create_from_folio(ctx, folio, offset, bytes);
1131e7d39069STrond Myklebust 	if (IS_ERR(req))
1132e7d39069STrond Myklebust 		goto out;
11330c493b5cSTrond Myklebust 	nfs_inode_add_request(req);
1134efc91ed0STrond Myklebust out:
113561e930a9STrond Myklebust 	return req;
11361da177e4SLinus Torvalds }
11371da177e4SLinus Torvalds 
11380c493b5cSTrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx,
11390c493b5cSTrond Myklebust 			       struct folio *folio, unsigned int offset,
11400c493b5cSTrond Myklebust 			       unsigned int count)
1141e7d39069STrond Myklebust {
1142e7d39069STrond Myklebust 	struct nfs_page *req;
1143e7d39069STrond Myklebust 
11440c493b5cSTrond Myklebust 	req = nfs_setup_write_request(ctx, folio, offset, count);
1145e7d39069STrond Myklebust 	if (IS_ERR(req))
1146e7d39069STrond Myklebust 		return PTR_ERR(req);
1147e7d39069STrond Myklebust 	/* Update file length */
11480c493b5cSTrond Myklebust 	nfs_grow_file(folio, offset, count);
1149d72ddcbaSWeston Andros Adamson 	nfs_mark_uptodate(req);
1150a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
11511d1afcbcSTrond Myklebust 	nfs_unlock_and_release_request(req);
1152e7d39069STrond Myklebust 	return 0;
1153e7d39069STrond Myklebust }
1154e7d39069STrond Myklebust 
11550c493b5cSTrond Myklebust int nfs_flush_incompatible(struct file *file, struct folio *folio)
11561da177e4SLinus Torvalds {
1157cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
11582a369153STrond Myklebust 	struct nfs_lock_context *l_ctx;
115917b985deSJeff Layton 	struct file_lock_context *flctx = locks_inode_context(file_inode(file));
11601da177e4SLinus Torvalds 	struct nfs_page	*req;
11611a54533eSTrond Myklebust 	int do_flush, status;
11621da177e4SLinus Torvalds 	/*
11631da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
11641da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
11651da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
11661da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
11671da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
11681da177e4SLinus Torvalds 	 * dropped page.
11691da177e4SLinus Torvalds 	 */
11701a54533eSTrond Myklebust 	do {
11710c493b5cSTrond Myklebust 		req = nfs_folio_find_head_request(folio);
11721a54533eSTrond Myklebust 		if (req == NULL)
11731a54533eSTrond Myklebust 			return 0;
11742a369153STrond Myklebust 		l_ctx = req->wb_lock_context;
11750c493b5cSTrond Myklebust 		do_flush = nfs_page_to_folio(req) != folio ||
11769fcd5960STrond Myklebust 			   !nfs_match_open_context(nfs_req_openctx(req), ctx);
1177bd61e0a9SJeff Layton 		if (l_ctx && flctx &&
1178bd61e0a9SJeff Layton 		    !(list_empty_careful(&flctx->flc_posix) &&
1179bd61e0a9SJeff Layton 		      list_empty_careful(&flctx->flc_flock))) {
1180d51fdb87SNeilBrown 			do_flush |= l_ctx->lockowner != current->files;
11815263e31eSJeff Layton 		}
11821da177e4SLinus Torvalds 		nfs_release_request(req);
11831a54533eSTrond Myklebust 		if (!do_flush)
11841a54533eSTrond Myklebust 			return 0;
11857e8e78a0SChristoph Hellwig 		status = nfs_wb_folio(folio->mapping->host, folio);
11861a54533eSTrond Myklebust 	} while (status == 0);
11871a54533eSTrond Myklebust 	return status;
11881da177e4SLinus Torvalds }
11891da177e4SLinus Torvalds 
11901da177e4SLinus Torvalds /*
1191dc24826bSAndy Adamson  * Avoid buffered writes when a open context credential's key would
1192dc24826bSAndy Adamson  * expire soon.
1193dc24826bSAndy Adamson  *
1194dc24826bSAndy Adamson  * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
1195dc24826bSAndy Adamson  *
1196dc24826bSAndy Adamson  * Return 0 and set a credential flag which triggers the inode to flush
1197dc24826bSAndy Adamson  * and performs  NFS_FILE_SYNC writes if the key will expired within
1198dc24826bSAndy Adamson  * RPC_KEY_EXPIRE_TIMEO.
1199dc24826bSAndy Adamson  */
1200dc24826bSAndy Adamson int
1201dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode)
1202dc24826bSAndy Adamson {
1203dc24826bSAndy Adamson 	struct nfs_open_context *ctx = nfs_file_open_context(filp);
1204dc24826bSAndy Adamson 
1205ddf529eeSNeilBrown 	if (nfs_ctx_key_to_expire(ctx, inode) &&
1206ca05cbaeSTrond Myklebust 	    !rcu_access_pointer(ctx->ll_cred))
1207ddf529eeSNeilBrown 		/* Already expired! */
1208ddf529eeSNeilBrown 		return -EACCES;
1209ddf529eeSNeilBrown 	return 0;
1210dc24826bSAndy Adamson }
1211dc24826bSAndy Adamson 
1212dc24826bSAndy Adamson /*
1213dc24826bSAndy Adamson  * Test if the open context credential key is marked to expire soon.
1214dc24826bSAndy Adamson  */
1215ce52914eSScott Mayhew bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode)
1216dc24826bSAndy Adamson {
1217ce52914eSScott Mayhew 	struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
1218ca05cbaeSTrond Myklebust 	struct rpc_cred *cred, *new, *old = NULL;
1219ddf529eeSNeilBrown 	struct auth_cred acred = {
1220a52458b4SNeilBrown 		.cred = ctx->cred,
1221ddf529eeSNeilBrown 	};
1222ca05cbaeSTrond Myklebust 	bool ret = false;
1223ce52914eSScott Mayhew 
1224ca05cbaeSTrond Myklebust 	rcu_read_lock();
1225ca05cbaeSTrond Myklebust 	cred = rcu_dereference(ctx->ll_cred);
1226ca05cbaeSTrond Myklebust 	if (cred && !(cred->cr_ops->crkey_timeout &&
1227ca05cbaeSTrond Myklebust 		      cred->cr_ops->crkey_timeout(cred)))
1228ca05cbaeSTrond Myklebust 		goto out;
1229ca05cbaeSTrond Myklebust 	rcu_read_unlock();
1230ca05cbaeSTrond Myklebust 
1231ca05cbaeSTrond Myklebust 	new = auth->au_ops->lookup_cred(auth, &acred, 0);
1232ca05cbaeSTrond Myklebust 	if (new == cred) {
1233ca05cbaeSTrond Myklebust 		put_rpccred(new);
1234ddf529eeSNeilBrown 		return true;
1235ca05cbaeSTrond Myklebust 	}
1236ca05cbaeSTrond Myklebust 	if (IS_ERR_OR_NULL(new)) {
1237ca05cbaeSTrond Myklebust 		new = NULL;
1238ca05cbaeSTrond Myklebust 		ret = true;
1239ca05cbaeSTrond Myklebust 	} else if (new->cr_ops->crkey_timeout &&
1240ca05cbaeSTrond Myklebust 		   new->cr_ops->crkey_timeout(new))
1241ca05cbaeSTrond Myklebust 		ret = true;
1242ca05cbaeSTrond Myklebust 
1243ca05cbaeSTrond Myklebust 	rcu_read_lock();
1244ca05cbaeSTrond Myklebust 	old = rcu_dereference_protected(xchg(&ctx->ll_cred,
1245ca05cbaeSTrond Myklebust 					     RCU_INITIALIZER(new)), 1);
1246ca05cbaeSTrond Myklebust out:
1247ca05cbaeSTrond Myklebust 	rcu_read_unlock();
1248ca05cbaeSTrond Myklebust 	put_rpccred(old);
1249ca05cbaeSTrond Myklebust 	return ret;
1250dc24826bSAndy Adamson }
1251dc24826bSAndy Adamson 
1252dc24826bSAndy Adamson /*
12535d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
12545d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
12555d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
12565d47a356STrond Myklebust  */
12570c493b5cSTrond Myklebust static bool nfs_folio_write_uptodate(struct folio *folio, unsigned int pagelen)
12585d47a356STrond Myklebust {
12597e8e78a0SChristoph Hellwig 	struct inode *inode = folio->mapping->host;
1260d529ef83SJeff Layton 	struct nfs_inode *nfsi = NFS_I(inode);
1261d529ef83SJeff Layton 
12628d197a56STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
12638d197a56STrond Myklebust 		goto out;
1264fc9dc401STrond Myklebust 	if (nfsi->cache_validity &
126513c0b082STrond Myklebust 	    (NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE))
1266d529ef83SJeff Layton 		return false;
12674db72b40SJeff Layton 	smp_rmb();
1268fc9dc401STrond Myklebust 	if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags) && pagelen != 0)
12698d197a56STrond Myklebust 		return false;
12708d197a56STrond Myklebust out:
1271fc9dc401STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_INVALID_DATA && pagelen != 0)
127218dd78c4SScott Mayhew 		return false;
12730c493b5cSTrond Myklebust 	return folio_test_uptodate(folio) != 0;
12745d47a356STrond Myklebust }
12755d47a356STrond Myklebust 
12765263e31eSJeff Layton static bool
12775263e31eSJeff Layton is_whole_file_wrlock(struct file_lock *fl)
12785263e31eSJeff Layton {
12795263e31eSJeff Layton 	return fl->fl_start == 0 && fl->fl_end == OFFSET_MAX &&
1280d7c9616bSJeff Layton 			lock_is_write(fl);
12815263e31eSJeff Layton }
12825263e31eSJeff Layton 
1283c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or
1284c7559663SScott Mayhew  * if we have the whole file locked for writing), it may be more efficient to
1285c7559663SScott Mayhew  * extend the write to cover the entire page in order to avoid fragmentation
1286c7559663SScott Mayhew  * inefficiencies.
1287c7559663SScott Mayhew  *
1288263b4509SScott Mayhew  * If the file is opened for synchronous writes then we can just skip the rest
1289263b4509SScott Mayhew  * of the checks.
1290c7559663SScott Mayhew  */
12910c493b5cSTrond Myklebust static int nfs_can_extend_write(struct file *file, struct folio *folio,
12920c493b5cSTrond Myklebust 				unsigned int pagelen)
1293c7559663SScott Mayhew {
12940c493b5cSTrond Myklebust 	struct inode *inode = file_inode(file);
129517b985deSJeff Layton 	struct file_lock_context *flctx = locks_inode_context(inode);
12965263e31eSJeff Layton 	struct file_lock *fl;
12970c493b5cSTrond Myklebust 	int ret;
1298dfb07e99SDan Aloni 	unsigned int mntflags = NFS_SERVER(inode)->flags;
12995263e31eSJeff Layton 
1300dfb07e99SDan Aloni 	if (mntflags & NFS_MOUNT_NO_ALIGNWRITE)
1301dfb07e99SDan Aloni 		return 0;
1302c7559663SScott Mayhew 	if (file->f_flags & O_DSYNC)
1303c7559663SScott Mayhew 		return 0;
13040c493b5cSTrond Myklebust 	if (!nfs_folio_write_uptodate(folio, pagelen))
1305263b4509SScott Mayhew 		return 0;
13064201916fSTrond Myklebust 	if (nfs_have_write_delegation(inode))
1307c7559663SScott Mayhew 		return 1;
1308bd61e0a9SJeff Layton 	if (!flctx || (list_empty_careful(&flctx->flc_flock) &&
1309bd61e0a9SJeff Layton 		       list_empty_careful(&flctx->flc_posix)))
13108fa4592aSTrond Myklebust 		return 1;
13115263e31eSJeff Layton 
13125263e31eSJeff Layton 	/* Check to see if there are whole file write locks */
13135263e31eSJeff Layton 	ret = 0;
13146109c850SJeff Layton 	spin_lock(&flctx->flc_lock);
1315bd61e0a9SJeff Layton 	if (!list_empty(&flctx->flc_posix)) {
1316bd61e0a9SJeff Layton 		fl = list_first_entry(&flctx->flc_posix, struct file_lock,
1317dd1fac6aSJeff Layton 					c.flc_list);
1318bd61e0a9SJeff Layton 		if (is_whole_file_wrlock(fl))
13195263e31eSJeff Layton 			ret = 1;
1320bd61e0a9SJeff Layton 	} else if (!list_empty(&flctx->flc_flock)) {
13215263e31eSJeff Layton 		fl = list_first_entry(&flctx->flc_flock, struct file_lock,
1322dd1fac6aSJeff Layton 					c.flc_list);
1323d7c9616bSJeff Layton 		if (lock_is_write(fl))
13245263e31eSJeff Layton 			ret = 1;
13255263e31eSJeff Layton 	}
13266109c850SJeff Layton 	spin_unlock(&flctx->flc_lock);
13275263e31eSJeff Layton 	return ret;
1328c7559663SScott Mayhew }
1329c7559663SScott Mayhew 
13305d47a356STrond Myklebust /*
13311da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
13321da177e4SLinus Torvalds  *
13331da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
13341da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
13351da177e4SLinus Torvalds  */
13360c493b5cSTrond Myklebust int nfs_update_folio(struct file *file, struct folio *folio,
13371da177e4SLinus Torvalds 		     unsigned int offset, unsigned int count)
13381da177e4SLinus Torvalds {
1339cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
13407e8e78a0SChristoph Hellwig 	struct address_space *mapping = folio->mapping;
1341d2ceb7e5SBenjamin Coddington 	struct inode *inode = mapping->host;
13420c493b5cSTrond Myklebust 	unsigned int pagelen = nfs_folio_length(folio);
13431da177e4SLinus Torvalds 	int		status = 0;
13441da177e4SLinus Torvalds 
134591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
134691d5b470SChuck Lever 
13470c493b5cSTrond Myklebust 	dprintk("NFS:       nfs_update_folio(%pD2 %d@%lld)\n", file, count,
1348237d2907SKairui Song 		(long long)(folio_pos(folio) + offset));
13491da177e4SLinus Torvalds 
1350149a4fddSBenjamin Coddington 	if (!count)
1351149a4fddSBenjamin Coddington 		goto out;
1352149a4fddSBenjamin Coddington 
13530c493b5cSTrond Myklebust 	if (nfs_can_extend_write(file, folio, pagelen)) {
135439c910a4SChristoph Hellwig 		unsigned int end = count + offset;
135539c910a4SChristoph Hellwig 
135639c910a4SChristoph Hellwig 		offset = round_down(offset, PAGE_SIZE);
135739c910a4SChristoph Hellwig 		if (end < pagelen)
135839c910a4SChristoph Hellwig 			end = min(round_up(end, PAGE_SIZE), pagelen);
135939c910a4SChristoph Hellwig 		count = end - offset;
13601da177e4SLinus Torvalds 	}
13611da177e4SLinus Torvalds 
13620c493b5cSTrond Myklebust 	status = nfs_writepage_setup(ctx, folio, offset, count);
136303fa9e84STrond Myklebust 	if (status < 0)
1364d2ceb7e5SBenjamin Coddington 		nfs_set_pageerror(mapping);
1365149a4fddSBenjamin Coddington out:
13660c493b5cSTrond Myklebust 	dprintk("NFS:       nfs_update_folio returns %d (isize %lld)\n",
13671da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
13681da177e4SLinus Torvalds 	return status;
13691da177e4SLinus Torvalds }
13701da177e4SLinus Torvalds 
13713ff7576dSTrond Myklebust static int flush_task_priority(int how)
13721da177e4SLinus Torvalds {
13731da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
13741da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
13751da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
13761da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
13771da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
13781da177e4SLinus Torvalds 	}
13791da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
13801da177e4SLinus Torvalds }
13811da177e4SLinus Torvalds 
1382d45f60c6SWeston Andros Adamson static void nfs_initiate_write(struct nfs_pgio_header *hdr,
1383d45f60c6SWeston Andros Adamson 			       struct rpc_message *msg,
1384abde71f4STom Haynes 			       const struct nfs_rpc_ops *rpc_ops,
13851ed26f33SAnna Schumaker 			       struct rpc_task_setup *task_setup_data, int how)
13861da177e4SLinus Torvalds {
13873ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
13881da177e4SLinus Torvalds 
13898db55a03SNeilBrown 	if (IS_SWAPFILE(hdr->inode))
13908db55a03SNeilBrown 		task_setup_data->flags |= RPC_TASK_SWAPPER;
13911ed26f33SAnna Schumaker 	task_setup_data->priority = priority;
1392fb91fb0eSAnna Schumaker 	rpc_ops->write_setup(hdr, msg, &task_setup_data->rpc_client);
13935bb2a7cbSTrond Myklebust 	trace_nfs_initiate_write(hdr);
1394275acaafSTrond Myklebust }
1395275acaafSTrond Myklebust 
13966d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
13976d884e8fSFred  * call this on each, which will prepare them to be retried on next
13986d884e8fSFred  * writeback using standard nfs.
13996d884e8fSFred  */
14006d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
14016d884e8fSFred {
14026dd85e83STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(nfs_page_to_inode(req));
140367f4b5dcSTrond Myklebust 
140433344e0fSTrond Myklebust 	/* Bump the transmission count */
140533344e0fSTrond Myklebust 	req->wb_nio++;
14066d884e8fSFred 	nfs_mark_request_dirty(req);
140767f4b5dcSTrond Myklebust 	atomic_long_inc(&nfsi->redirtied_pages);
14080c493b5cSTrond Myklebust 	nfs_page_end_writeback(req);
14093aff4ebbSTrond Myklebust 	nfs_release_request(req);
14106d884e8fSFred }
14116d884e8fSFred 
1412df3accb8STrond Myklebust static void nfs_async_write_error(struct list_head *head, int error)
14136c75dc0dSFred Isaman {
14146c75dc0dSFred Isaman 	struct nfs_page	*req;
14156c75dc0dSFred Isaman 
14166c75dc0dSFred Isaman 	while (!list_empty(head)) {
14176c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
14186c75dc0dSFred Isaman 		nfs_list_remove_request(req);
1419cea9ba72STrond Myklebust 		if (nfs_error_is_fatal_on_server(error))
14206fbda89bSTrond Myklebust 			nfs_write_error(req, error);
14216fbda89bSTrond Myklebust 		else
14226c75dc0dSFred Isaman 			nfs_redirty_request(req);
14236c75dc0dSFred Isaman 	}
14246c75dc0dSFred Isaman }
14256c75dc0dSFred Isaman 
1426dc602dd7STrond Myklebust static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr)
1427dc602dd7STrond Myklebust {
1428df3accb8STrond Myklebust 	nfs_async_write_error(&hdr->pages, 0);
1429dc602dd7STrond Myklebust }
1430dc602dd7STrond Myklebust 
1431061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1432919e3bd9STrond Myklebust 	.init_hdr = nfs_async_write_init,
1433061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1434061ae2edSFred Isaman 	.completion = nfs_write_completion,
1435dc602dd7STrond Myklebust 	.reschedule_io = nfs_async_write_reschedule_io,
1436061ae2edSFred Isaman };
1437061ae2edSFred Isaman 
143857208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1439a20c93e3SChristoph Hellwig 			       struct inode *inode, int ioflags, bool force_mds,
1440061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
14411751c363STrond Myklebust {
1442a20c93e3SChristoph Hellwig 	struct nfs_server *server = NFS_SERVER(inode);
144341d8d5b7SAnna Schumaker 	const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
1444a20c93e3SChristoph Hellwig 
1445a20c93e3SChristoph Hellwig #ifdef CONFIG_NFS_V4_1
1446a20c93e3SChristoph Hellwig 	if (server->pnfs_curr_ld && !force_mds)
1447a20c93e3SChristoph Hellwig 		pg_ops = server->pnfs_curr_ld->pg_write_ops;
1448a20c93e3SChristoph Hellwig #endif
14494a0de55cSAnna Schumaker 	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
14503bde7afdSTrond Myklebust 			server->wsize, ioflags);
14511751c363STrond Myklebust }
1452ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
14531751c363STrond Myklebust 
1454dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1455dce81290STrond Myklebust {
1456a7d42ddbSWeston Andros Adamson 	struct nfs_pgio_mirror *mirror;
1457a7d42ddbSWeston Andros Adamson 
14586f29b9bbSKinglong Mee 	if (pgio->pg_ops && pgio->pg_ops->pg_cleanup)
14596f29b9bbSKinglong Mee 		pgio->pg_ops->pg_cleanup(pgio);
14606f29b9bbSKinglong Mee 
146141d8d5b7SAnna Schumaker 	pgio->pg_ops = &nfs_pgio_rw_ops;
1462a7d42ddbSWeston Andros Adamson 
1463a7d42ddbSWeston Andros Adamson 	nfs_pageio_stop_mirroring(pgio);
1464a7d42ddbSWeston Andros Adamson 
1465a7d42ddbSWeston Andros Adamson 	mirror = &pgio->pg_mirrors[0];
1466a7d42ddbSWeston Andros Adamson 	mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1467dce81290STrond Myklebust }
14681f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1469dce81290STrond Myklebust 
14701da177e4SLinus Torvalds 
14710b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
14720b7c0153SFred Isaman {
14730b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
14740b7c0153SFred Isaman 
14750b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
14760b7c0153SFred Isaman }
14770b7c0153SFred Isaman 
1478a08a8cd3STrond Myklebust static void nfs_writeback_check_extend(struct nfs_pgio_header *hdr,
1479a08a8cd3STrond Myklebust 		struct nfs_fattr *fattr)
1480a08a8cd3STrond Myklebust {
1481a08a8cd3STrond Myklebust 	struct nfs_pgio_args *argp = &hdr->args;
1482a08a8cd3STrond Myklebust 	struct nfs_pgio_res *resp = &hdr->res;
14832b83d3deSTrond Myklebust 	u64 size = argp->offset + resp->count;
1484a08a8cd3STrond Myklebust 
1485a08a8cd3STrond Myklebust 	if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
14862b83d3deSTrond Myklebust 		fattr->size = size;
14872b83d3deSTrond Myklebust 	if (nfs_size_to_loff_t(fattr->size) < i_size_read(hdr->inode)) {
14882b83d3deSTrond Myklebust 		fattr->valid &= ~NFS_ATTR_FATTR_SIZE;
1489a08a8cd3STrond Myklebust 		return;
14902b83d3deSTrond Myklebust 	}
14912b83d3deSTrond Myklebust 	if (size != fattr->size)
1492a08a8cd3STrond Myklebust 		return;
1493a08a8cd3STrond Myklebust 	/* Set attribute barrier */
1494a08a8cd3STrond Myklebust 	nfs_fattr_set_barrier(fattr);
14952b83d3deSTrond Myklebust 	/* ...and update size */
14962b83d3deSTrond Myklebust 	fattr->valid |= NFS_ATTR_FATTR_SIZE;
1497a08a8cd3STrond Myklebust }
1498a08a8cd3STrond Myklebust 
1499a08a8cd3STrond Myklebust void nfs_writeback_update_inode(struct nfs_pgio_header *hdr)
1500a08a8cd3STrond Myklebust {
15012b83d3deSTrond Myklebust 	struct nfs_fattr *fattr = &hdr->fattr;
1502a08a8cd3STrond Myklebust 	struct inode *inode = hdr->inode;
1503a08a8cd3STrond Myklebust 
1504e12912d9STrond Myklebust 	if (nfs_have_delegated_mtime(inode)) {
1505e12912d9STrond Myklebust 		spin_lock(&inode->i_lock);
1506e12912d9STrond Myklebust 		nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS);
1507e12912d9STrond Myklebust 		spin_unlock(&inode->i_lock);
1508e12912d9STrond Myklebust 		return;
1509e12912d9STrond Myklebust 	}
1510e12912d9STrond Myklebust 
1511a08a8cd3STrond Myklebust 	spin_lock(&inode->i_lock);
1512a08a8cd3STrond Myklebust 	nfs_writeback_check_extend(hdr, fattr);
1513a08a8cd3STrond Myklebust 	nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
1514a08a8cd3STrond Myklebust 	spin_unlock(&inode->i_lock);
1515a08a8cd3STrond Myklebust }
1516a08a8cd3STrond Myklebust EXPORT_SYMBOL_GPL(nfs_writeback_update_inode);
1517a08a8cd3STrond Myklebust 
15181da177e4SLinus Torvalds /*
15191da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
15201da177e4SLinus Torvalds  */
1521d45f60c6SWeston Andros Adamson static int nfs_writeback_done(struct rpc_task *task,
1522d45f60c6SWeston Andros Adamson 			      struct nfs_pgio_header *hdr,
15230eecb214SAnna Schumaker 			      struct inode *inode)
15241da177e4SLinus Torvalds {
1525788e7a89STrond Myklebust 	int status;
15261da177e4SLinus Torvalds 
1527f551e44fSChuck Lever 	/*
1528f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1529f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1530f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1531f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1532f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1533f551e44fSChuck Lever 	 */
1534d45f60c6SWeston Andros Adamson 	status = NFS_PROTO(inode)->write_done(task, hdr);
1535788e7a89STrond Myklebust 	if (status != 0)
15360eecb214SAnna Schumaker 		return status;
15378224b273SChuck Lever 
1538d45f60c6SWeston Andros Adamson 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count);
15395bb2a7cbSTrond Myklebust 	trace_nfs_writeback_done(task, hdr);
154091d5b470SChuck Lever 
154169d96651SJeff Layton 	if (task->tk_status >= 0) {
154269d96651SJeff Layton 		enum nfs3_stable_how committed = hdr->res.verf->committed;
154369d96651SJeff Layton 
154469d96651SJeff Layton 		if (committed == NFS_UNSTABLE) {
154569d96651SJeff Layton 			/*
154669d96651SJeff Layton 			 * We have some uncommitted data on the server at
154769d96651SJeff Layton 			 * this point, so ensure that we keep track of that
154869d96651SJeff Layton 			 * fact irrespective of what later writes do.
154969d96651SJeff Layton 			 */
155069d96651SJeff Layton 			set_bit(NFS_IOHDR_UNSTABLE_WRITES, &hdr->flags);
155169d96651SJeff Layton 		}
155269d96651SJeff Layton 
155369d96651SJeff Layton 		if (committed < hdr->args.stable) {
15541da177e4SLinus Torvalds 			/* We tried a write call, but the server did not
15551da177e4SLinus Torvalds 			 * commit data to stable storage even though we
15561da177e4SLinus Torvalds 			 * requested it.
15571da177e4SLinus Torvalds 			 * Note: There is a known bug in Tru64 < 5.0 in which
15581da177e4SLinus Torvalds 			 *	 the server reports NFS_DATA_SYNC, but performs
15591da177e4SLinus Torvalds 			 *	 NFS_FILE_SYNC. We therefore implement this checking
15601da177e4SLinus Torvalds 			 *	 as a dprintk() in order to avoid filling syslog.
15611da177e4SLinus Torvalds 			 */
15621da177e4SLinus Torvalds 			static unsigned long    complain;
15631da177e4SLinus Torvalds 
1564a69aef14SFred Isaman 			/* Note this will print the MDS for a DS write */
15651da177e4SLinus Torvalds 			if (time_before(complain, jiffies)) {
15661da177e4SLinus Torvalds 				dprintk("NFS:       faulty NFS server %s:"
15671da177e4SLinus Torvalds 					" (committed = %d) != (stable = %d)\n",
1568cd841605SFred Isaman 					NFS_SERVER(inode)->nfs_client->cl_hostname,
156969d96651SJeff Layton 					committed, hdr->args.stable);
15701da177e4SLinus Torvalds 				complain = jiffies + 300 * HZ;
15711da177e4SLinus Torvalds 			}
15721da177e4SLinus Torvalds 		}
157369d96651SJeff Layton 	}
15741f2edbe3STrond Myklebust 
15751f2edbe3STrond Myklebust 	/* Deal with the suid/sgid bit corner case */
157616e14375STrond Myklebust 	if (nfs_should_remove_suid(inode)) {
157716e14375STrond Myklebust 		spin_lock(&inode->i_lock);
1578720869ebSTrond Myklebust 		nfs_set_cache_invalid(inode, NFS_INO_INVALID_MODE);
157916e14375STrond Myklebust 		spin_unlock(&inode->i_lock);
158016e14375STrond Myklebust 	}
15810eecb214SAnna Schumaker 	return 0;
15820eecb214SAnna Schumaker }
15830eecb214SAnna Schumaker 
15840eecb214SAnna Schumaker /*
15850eecb214SAnna Schumaker  * This function is called when the WRITE call is complete.
15860eecb214SAnna Schumaker  */
1587d45f60c6SWeston Andros Adamson static void nfs_writeback_result(struct rpc_task *task,
1588d45f60c6SWeston Andros Adamson 				 struct nfs_pgio_header *hdr)
15890eecb214SAnna Schumaker {
1590d45f60c6SWeston Andros Adamson 	struct nfs_pgio_args	*argp = &hdr->args;
1591d45f60c6SWeston Andros Adamson 	struct nfs_pgio_res	*resp = &hdr->res;
15921f2edbe3STrond Myklebust 
15931f2edbe3STrond Myklebust 	if (resp->count < argp->count) {
15941da177e4SLinus Torvalds 		static unsigned long    complain;
15951da177e4SLinus Torvalds 
15966c75dc0dSFred Isaman 		/* This a short write! */
1597d45f60c6SWeston Andros Adamson 		nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE);
159891d5b470SChuck Lever 
15991da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
16006c75dc0dSFred Isaman 		if (resp->count == 0) {
16016c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
16026c75dc0dSFred Isaman 				printk(KERN_WARNING
16036c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
16046c75dc0dSFred Isaman 				       argp->count);
16056c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
16066c75dc0dSFred Isaman 			}
1607d45f60c6SWeston Andros Adamson 			nfs_set_pgio_error(hdr, -EIO, argp->offset);
16086c75dc0dSFred Isaman 			task->tk_status = -EIO;
16096c75dc0dSFred Isaman 			return;
16106c75dc0dSFred Isaman 		}
1611f8417b48SKinglong Mee 
1612f8417b48SKinglong Mee 		/* For non rpc-based layout drivers, retry-through-MDS */
1613f8417b48SKinglong Mee 		if (!task->tk_ops) {
1614f8417b48SKinglong Mee 			hdr->pnfs_error = -EAGAIN;
1615f8417b48SKinglong Mee 			return;
1616f8417b48SKinglong Mee 		}
1617f8417b48SKinglong Mee 
16181da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
16191da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
16201da177e4SLinus Torvalds 			/* Resend from where the server left off */
1621d45f60c6SWeston Andros Adamson 			hdr->mds_offset += resp->count;
16221da177e4SLinus Torvalds 			argp->offset += resp->count;
16231da177e4SLinus Torvalds 			argp->pgbase += resp->count;
16241da177e4SLinus Torvalds 			argp->count -= resp->count;
16251da177e4SLinus Torvalds 		} else {
16261da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
16271da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
16281da177e4SLinus Torvalds 			 */
16291da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
16301da177e4SLinus Torvalds 		}
16318c9cb714STrond Myklebust 		resp->count = 0;
16328c9cb714STrond Myklebust 		resp->verf->committed = 0;
1633d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
16341da177e4SLinus Torvalds 	}
16351da177e4SLinus Torvalds }
16361da177e4SLinus Torvalds 
1637af7cf057STrond Myklebust static int wait_on_commit(struct nfs_mds_commit_info *cinfo)
163871d0a611STrond Myklebust {
1639723c921eSPeter Zijlstra 	return wait_var_event_killable(&cinfo->rpcs_out,
1640723c921eSPeter Zijlstra 				       !atomic_read(&cinfo->rpcs_out));
1641af7cf057STrond Myklebust }
1642af7cf057STrond Myklebust 
164317f46b80SJosef Bacik void nfs_commit_begin(struct nfs_mds_commit_info *cinfo)
1644af7cf057STrond Myklebust {
1645af7cf057STrond Myklebust 	atomic_inc(&cinfo->rpcs_out);
1646af7cf057STrond Myklebust }
1647af7cf057STrond Myklebust 
1648133a48abSTrond Myklebust bool nfs_commit_end(struct nfs_mds_commit_info *cinfo)
1649af7cf057STrond Myklebust {
1650133a48abSTrond Myklebust 	if (atomic_dec_and_test(&cinfo->rpcs_out)) {
1651723c921eSPeter Zijlstra 		wake_up_var(&cinfo->rpcs_out);
1652133a48abSTrond Myklebust 		return true;
1653133a48abSTrond Myklebust 	}
1654133a48abSTrond Myklebust 	return false;
165571d0a611STrond Myklebust }
165671d0a611STrond Myklebust 
16570b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
16581da177e4SLinus Torvalds {
16590b7c0153SFred Isaman 	put_nfs_open_context(data->context);
16600b7c0153SFred Isaman 	nfs_commit_free(data);
16611da177e4SLinus Torvalds }
1662e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
16631da177e4SLinus Torvalds 
16640b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
1665c36aae9aSPeng Tao 			const struct nfs_rpc_ops *nfs_ops,
16669ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
1667df24c483SMike Snitzer 			int how, int flags,
1668df24c483SMike Snitzer 			struct nfsd_file *localio)
16691da177e4SLinus Torvalds {
167007737691STrond Myklebust 	struct rpc_task *task;
16719ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1672bdc7f021STrond Myklebust 	struct rpc_message msg = {
1673bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1674bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
16759ace33cdSFred Isaman 		.rpc_cred = data->cred,
1676bdc7f021STrond Myklebust 	};
167784115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
167807737691STrond Myklebust 		.task = &data->task,
16799ace33cdSFred Isaman 		.rpc_client = clnt,
1680bdc7f021STrond Myklebust 		.rpc_message = &msg,
16819ace33cdSFred Isaman 		.callback_ops = call_ops,
168284115e1cSTrond Myklebust 		.callback_data = data,
1683101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
16844fa7ef69STrond Myklebust 		.flags = RPC_TASK_ASYNC | flags,
16853ff7576dSTrond Myklebust 		.priority = priority,
168684115e1cSTrond Myklebust 	};
1687118f09edSOlga Kornievskaia 
1688118f09edSOlga Kornievskaia 	if (nfs_server_capable(data->inode, NFS_CAP_MOVEABLE))
1689118f09edSOlga Kornievskaia 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
1690118f09edSOlga Kornievskaia 
1691788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
1692e9ae1ee2SAnna Schumaker 	nfs_ops->commit_setup(data, &msg, &task_setup_data.rpc_client);
16938224b273SChuck Lever 	trace_nfs_initiate_commit(data);
16941da177e4SLinus Torvalds 
1695b4839ebeSKinglong Mee 	dprintk("NFS: initiated commit call\n");
1696bdc7f021STrond Myklebust 
169770ba381eSWeston Andros Adamson 	if (localio)
169870ba381eSWeston Andros Adamson 		return nfs_local_commit(localio, data, call_ops, how);
169970ba381eSWeston Andros Adamson 
170007737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1701dbae4c73STrond Myklebust 	if (IS_ERR(task))
1702dbae4c73STrond Myklebust 		return PTR_ERR(task);
1703d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1704d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
170507737691STrond Myklebust 	rpc_put_task(task);
1706dbae4c73STrond Myklebust 	return 0;
17071da177e4SLinus Torvalds }
1708e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
17091da177e4SLinus Torvalds 
1710378520b8SPeng Tao static loff_t nfs_get_lwb(struct list_head *head)
1711378520b8SPeng Tao {
1712378520b8SPeng Tao 	loff_t lwb = 0;
1713378520b8SPeng Tao 	struct nfs_page *req;
1714378520b8SPeng Tao 
1715378520b8SPeng Tao 	list_for_each_entry(req, head, wb_list)
1716378520b8SPeng Tao 		if (lwb < (req_offset(req) + req->wb_bytes))
1717378520b8SPeng Tao 			lwb = req_offset(req) + req->wb_bytes;
1718378520b8SPeng Tao 
1719378520b8SPeng Tao 	return lwb;
1720378520b8SPeng Tao }
1721378520b8SPeng Tao 
17221da177e4SLinus Torvalds /*
17239ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
17249ace33cdSFred Isaman  */
17250b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1726988b6dceSFred Isaman 		     struct list_head *head,
1727f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1728f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
17299ace33cdSFred Isaman {
173019573c93STrond Myklebust 	struct nfs_page *first;
173119573c93STrond Myklebust 	struct nfs_open_context *ctx;
173219573c93STrond Myklebust 	struct inode *inode;
17339ace33cdSFred Isaman 
17349ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
17359ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
17369ace33cdSFred Isaman 
173719573c93STrond Myklebust 	if (head)
17389ace33cdSFred Isaman 		list_splice_init(head, &data->pages);
17399ace33cdSFred Isaman 
174019573c93STrond Myklebust 	first = nfs_list_entry(data->pages.next);
174119573c93STrond Myklebust 	ctx = nfs_req_openctx(first);
174219573c93STrond Myklebust 	inode = d_inode(ctx->dentry);
174319573c93STrond Myklebust 
17449ace33cdSFred Isaman 	data->inode	  = inode;
17459fcd5960STrond Myklebust 	data->cred	  = ctx->cred;
1746988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
1747378520b8SPeng Tao 	/* only set lwb for pnfs commit */
1748378520b8SPeng Tao 	if (lseg)
1749378520b8SPeng Tao 		data->lwb = nfs_get_lwb(&data->pages);
17509ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1751f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1752b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
17539ace33cdSFred Isaman 
17549ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
17559ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
17569ace33cdSFred Isaman 	data->args.offset = 0;
17579ace33cdSFred Isaman 	data->args.count  = 0;
17589fcd5960STrond Myklebust 	data->context     = get_nfs_open_context(ctx);
17599ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
17609ace33cdSFred Isaman 	data->res.verf    = &data->verf;
17619ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
1762133a48abSTrond Myklebust 	nfs_commit_begin(cinfo->mds);
17639ace33cdSFred Isaman }
1764e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
17659ace33cdSFred Isaman 
1766e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1767ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1768b57ff130SWeston Andros Adamson 		      struct nfs_commit_info *cinfo,
1769b57ff130SWeston Andros Adamson 		      u32 ds_commit_idx)
177064bfeb49SFred Isaman {
177164bfeb49SFred Isaman 	struct nfs_page *req;
177264bfeb49SFred Isaman 
177364bfeb49SFred Isaman 	while (!list_empty(page_list)) {
177464bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
177564bfeb49SFred Isaman 		nfs_list_remove_request(req);
1776b57ff130SWeston Andros Adamson 		nfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx);
17770c493b5cSTrond Myklebust 		nfs_folio_clear_commit(nfs_page_to_folio(req));
17781d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
177964bfeb49SFred Isaman 	}
178064bfeb49SFred Isaman }
1781e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
178264bfeb49SFred Isaman 
17830c493b5cSTrond Myklebust static void nfs_commit_resched_write(struct nfs_commit_info *cinfo,
1784b20135d0STrond Myklebust 				     struct nfs_page *req)
1785b20135d0STrond Myklebust {
17860c493b5cSTrond Myklebust 	struct folio *folio = nfs_page_to_folio(req);
17870c493b5cSTrond Myklebust 
17880c493b5cSTrond Myklebust 	filemap_dirty_folio(folio_mapping(folio), folio);
1789b20135d0STrond Myklebust }
1790b20135d0STrond Myklebust 
17919ace33cdSFred Isaman /*
17921da177e4SLinus Torvalds  * Commit dirty pages
17931da177e4SLinus Torvalds  */
17941da177e4SLinus Torvalds static int
1795ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1796ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
17971da177e4SLinus Torvalds {
17980b7c0153SFred Isaman 	struct nfs_commit_data	*data;
1799*fa88a7d6STrond Myklebust 	struct nfsd_file *localio;
180085e39feeSOlga Kornievskaia 	unsigned short task_flags = 0;
18011da177e4SLinus Torvalds 
1802ade8febdSWeston Andros Adamson 	/* another commit raced with us */
1803ade8febdSWeston Andros Adamson 	if (list_empty(head))
1804ade8febdSWeston Andros Adamson 		return 0;
1805ade8febdSWeston Andros Adamson 
1806515dcdcdSTrond Myklebust 	data = nfs_commitdata_alloc();
1807515dcdcdSTrond Myklebust 	if (!data) {
1808515dcdcdSTrond Myklebust 		nfs_retry_commit(head, NULL, cinfo, -1);
1809515dcdcdSTrond Myklebust 		return -ENOMEM;
1810515dcdcdSTrond Myklebust 	}
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds 	/* Set up the argument struct */
1813f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
181485e39feeSOlga Kornievskaia 	if (NFS_SERVER(inode)->nfs_client->cl_minorversion)
181585e39feeSOlga Kornievskaia 		task_flags = RPC_TASK_MOVEABLE;
1816*fa88a7d6STrond Myklebust 
1817*fa88a7d6STrond Myklebust 	localio = nfs_local_open_fh(NFS_SERVER(inode)->nfs_client, data->cred,
1818*fa88a7d6STrond Myklebust 				    data->args.fh, data->context->mode);
1819c36aae9aSPeng Tao 	return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode),
182085e39feeSOlga Kornievskaia 				   data->mds_ops, how,
1821*fa88a7d6STrond Myklebust 				   RPC_TASK_CRED_NOREF | task_flags, localio);
18221da177e4SLinus Torvalds }
18231da177e4SLinus Torvalds 
18241da177e4SLinus Torvalds /*
18251da177e4SLinus Torvalds  * COMMIT call returned
18261da177e4SLinus Torvalds  */
1827788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
18281da177e4SLinus Torvalds {
18290b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
18301da177e4SLinus Torvalds 
1831788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1832c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
18337bdd297eSTrond Myklebust 	trace_nfs_commit_done(task, data);
1834c9d8f89dSTrond Myklebust }
1835c9d8f89dSTrond Myklebust 
1836f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1837c9d8f89dSTrond Myklebust {
1838221203ceSTrond Myklebust 	const struct nfs_writeverf *verf = data->res.verf;
1839c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1840c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1841f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
18420c493b5cSTrond Myklebust 	struct folio *folio;
1843788e7a89STrond Myklebust 
18441da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
18451da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
18461da177e4SLinus Torvalds 		nfs_list_remove_request(req);
18470c493b5cSTrond Myklebust 		folio = nfs_page_to_folio(req);
18480c493b5cSTrond Myklebust 		nfs_folio_clear_commit(folio);
18491da177e4SLinus Torvalds 
18501e8968c5SNiels de Vos 		dprintk("NFS:       commit (%s/%llu %d@%lld)",
18519fcd5960STrond Myklebust 			nfs_req_openctx(req)->dentry->d_sb->s_id,
18529fcd5960STrond Myklebust 			(unsigned long long)NFS_FILEID(d_inode(nfs_req_openctx(req)->dentry)),
18531da177e4SLinus Torvalds 			req->wb_bytes,
18541da177e4SLinus Torvalds 			(long long)req_offset(req));
1855c9d8f89dSTrond Myklebust 		if (status < 0) {
18560c493b5cSTrond Myklebust 			if (folio) {
1857af887e43STrond Myklebust 				trace_nfs_commit_error(data->inode, req,
1858af887e43STrond Myklebust 						       status);
18590c493b5cSTrond Myklebust 				nfs_mapping_set_error(folio, status);
18601da177e4SLinus Torvalds 				nfs_inode_remove_request(req);
18616fbda89bSTrond Myklebust 			}
1862ddeaa637SJoe Perches 			dprintk_cont(", error = %d\n", status);
18631da177e4SLinus Torvalds 			goto next;
18641da177e4SLinus Torvalds 		}
18651da177e4SLinus Torvalds 
18661da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
18671da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
18681f28476dSTrond Myklebust 		if (nfs_write_match_verf(verf, req)) {
18691da177e4SLinus Torvalds 			/* We have a match */
18700c493b5cSTrond Myklebust 			if (folio)
18711da177e4SLinus Torvalds 				nfs_inode_remove_request(req);
1872ddeaa637SJoe Perches 			dprintk_cont(" OK\n");
18731da177e4SLinus Torvalds 			goto next;
18741da177e4SLinus Torvalds 		}
18751da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
1876ddeaa637SJoe Perches 		dprintk_cont(" mismatch\n");
18776d884e8fSFred 		nfs_mark_request_dirty(req);
187867f4b5dcSTrond Myklebust 		atomic_long_inc(&NFS_I(data->inode)->redirtied_pages);
18791da177e4SLinus Torvalds 	next:
18801d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
18817f1bda44STrond Myklebust 		/* Latency breaker */
18827f1bda44STrond Myklebust 		cond_resched();
18831da177e4SLinus Torvalds 	}
1884353db796SNeilBrown 
1885f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1886af7cf057STrond Myklebust 	nfs_commit_end(cinfo.mds);
18875917ce84SFred Isaman }
18885917ce84SFred Isaman 
18895917ce84SFred Isaman static void nfs_commit_release(void *calldata)
18905917ce84SFred Isaman {
18910b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
18925917ce84SFred Isaman 
1893f453a54aSFred Isaman 	data->completion_ops->completion(data);
1894c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
18951da177e4SLinus Torvalds }
1896788e7a89STrond Myklebust 
1897788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
18980b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1899788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1900788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1901788e7a89STrond Myklebust };
19021da177e4SLinus Torvalds 
1903f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1904f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1905b20135d0STrond Myklebust 	.resched_write = nfs_commit_resched_write,
1906f453a54aSFred Isaman };
1907f453a54aSFred Isaman 
19081763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1909ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
191084c53ab5SFred Isaman {
191184c53ab5SFred Isaman 	int status;
191284c53ab5SFred Isaman 
1913ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
191484c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1915ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
191684c53ab5SFred Isaman 	return status;
191784c53ab5SFred Isaman }
191884c53ab5SFred Isaman 
1919c4f24df9STrond Myklebust static int __nfs_commit_inode(struct inode *inode, int how,
1920c4f24df9STrond Myklebust 		struct writeback_control *wbc)
19211da177e4SLinus Torvalds {
19221da177e4SLinus Torvalds 	LIST_HEAD(head);
1923ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
192471d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1925c4f24df9STrond Myklebust 	int ret, nscan;
19261da177e4SLinus Torvalds 
192764a93dbfSTrond Myklebust 	how &= ~FLUSH_SYNC;
1928ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1929af7cf057STrond Myklebust 	nfs_commit_begin(cinfo.mds);
1930c4f24df9STrond Myklebust 	for (;;) {
1931c4f24df9STrond Myklebust 		ret = nscan = nfs_scan_commit(inode, &head, &cinfo);
1932c4f24df9STrond Myklebust 		if (ret <= 0)
1933c4f24df9STrond Myklebust 			break;
1934c4f24df9STrond Myklebust 		ret = nfs_generic_commit_list(inode, &head, how, &cinfo);
1935c4f24df9STrond Myklebust 		if (ret < 0)
1936c4f24df9STrond Myklebust 			break;
1937c4f24df9STrond Myklebust 		ret = 0;
1938c4f24df9STrond Myklebust 		if (wbc && wbc->sync_mode == WB_SYNC_NONE) {
1939c4f24df9STrond Myklebust 			if (nscan < wbc->nr_to_write)
1940c4f24df9STrond Myklebust 				wbc->nr_to_write -= nscan;
1941c4f24df9STrond Myklebust 			else
1942c4f24df9STrond Myklebust 				wbc->nr_to_write = 0;
1943c4f24df9STrond Myklebust 		}
1944c4f24df9STrond Myklebust 		if (nscan < INT_MAX)
1945c4f24df9STrond Myklebust 			break;
1946c4f24df9STrond Myklebust 		cond_resched();
1947c4f24df9STrond Myklebust 	}
1948af7cf057STrond Myklebust 	nfs_commit_end(cinfo.mds);
1949c4f24df9STrond Myklebust 	if (ret || !may_wait)
1950c4f24df9STrond Myklebust 		return ret;
1951c4f24df9STrond Myklebust 	return wait_on_commit(cinfo.mds);
1952c4f24df9STrond Myklebust }
1953c4f24df9STrond Myklebust 
1954c4f24df9STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
1955c4f24df9STrond Myklebust {
1956c4f24df9STrond Myklebust 	return __nfs_commit_inode(inode, how, NULL);
19571da177e4SLinus Torvalds }
1958b20135d0STrond Myklebust EXPORT_SYMBOL_GPL(nfs_commit_inode);
19598fc795f7STrond Myklebust 
1960ae09c31fSAnna Schumaker int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
19618fc795f7STrond Myklebust {
1962420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1963420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1964420e3646STrond Myklebust 	int ret = 0;
19658fc795f7STrond Myklebust 
1966c4f24df9STrond Myklebust 	if (wbc->sync_mode == WB_SYNC_NONE) {
19673236c3e1SJeff Layton 		/* no commits means nothing needs to be done */
19685cb953d4STrond Myklebust 		if (!atomic_long_read(&nfsi->commit_info.ncommit))
1969c4f24df9STrond Myklebust 			goto check_requests_outstanding;
19703236c3e1SJeff Layton 
1971a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1972a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1973420e3646STrond Myklebust 		 */
19741a4edf0fSTrond Myklebust 		if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1975420e3646STrond Myklebust 			goto out_mark_dirty;
1976420e3646STrond Myklebust 
1977a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1978420e3646STrond Myklebust 		flags = 0;
1979a00dd6c0SJeff Layton 	}
1980a00dd6c0SJeff Layton 
1981c4f24df9STrond Myklebust 	ret = __nfs_commit_inode(inode, flags, wbc);
1982c4f24df9STrond Myklebust 	if (!ret) {
1983c4f24df9STrond Myklebust 		if (flags & FLUSH_SYNC)
19848fc795f7STrond Myklebust 			return 0;
1985c4f24df9STrond Myklebust 	} else if (atomic_long_read(&nfsi->commit_info.ncommit))
1986c4f24df9STrond Myklebust 		goto out_mark_dirty;
1987c4f24df9STrond Myklebust 
1988c4f24df9STrond Myklebust check_requests_outstanding:
1989c4f24df9STrond Myklebust 	if (!atomic_read(&nfsi->commit_info.rpcs_out))
1990c4f24df9STrond Myklebust 		return ret;
1991420e3646STrond Myklebust out_mark_dirty:
19928fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
19938fc795f7STrond Myklebust 	return ret;
19948fc795f7STrond Myklebust }
199589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode);
1996863a3c6cSAndy Adamson 
1997acdc53b2STrond Myklebust /*
1998837bb1d7STrond Myklebust  * Wrapper for filemap_write_and_wait_range()
1999837bb1d7STrond Myklebust  *
2000837bb1d7STrond Myklebust  * Needed for pNFS in order to ensure data becomes visible to the
2001837bb1d7STrond Myklebust  * client.
2002837bb1d7STrond Myklebust  */
2003837bb1d7STrond Myklebust int nfs_filemap_write_and_wait_range(struct address_space *mapping,
2004837bb1d7STrond Myklebust 		loff_t lstart, loff_t lend)
2005837bb1d7STrond Myklebust {
2006837bb1d7STrond Myklebust 	int ret;
2007837bb1d7STrond Myklebust 
2008837bb1d7STrond Myklebust 	ret = filemap_write_and_wait_range(mapping, lstart, lend);
2009837bb1d7STrond Myklebust 	if (ret == 0)
2010837bb1d7STrond Myklebust 		ret = pnfs_sync_inode(mapping->host, true);
2011837bb1d7STrond Myklebust 	return ret;
2012837bb1d7STrond Myklebust }
2013837bb1d7STrond Myklebust EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range);
2014837bb1d7STrond Myklebust 
2015837bb1d7STrond Myklebust /*
2016acdc53b2STrond Myklebust  * flush the inode to disk.
2017acdc53b2STrond Myklebust  */
2018acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
201934901f70STrond Myklebust {
2020f4ce1299STrond Myklebust 	int ret;
202134901f70STrond Myklebust 
2022f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_enter(inode);
2023f4ce1299STrond Myklebust 
20245bb89b47STrond Myklebust 	ret = filemap_write_and_wait(inode->i_mapping);
20256b196875SChuck Lever 	if (ret)
20266b196875SChuck Lever 		goto out;
20275bb89b47STrond Myklebust 	ret = nfs_commit_inode(inode, FLUSH_SYNC);
20286b196875SChuck Lever 	if (ret < 0)
20296b196875SChuck Lever 		goto out;
20305bb89b47STrond Myklebust 	pnfs_sync_inode(inode, true);
20316b196875SChuck Lever 	ret = 0;
2032f4ce1299STrond Myklebust 
20336b196875SChuck Lever out:
2034f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_exit(inode, ret);
2035f4ce1299STrond Myklebust 	return ret;
20361c75950bSTrond Myklebust }
2037ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all);
20381c75950bSTrond Myklebust 
20396d740c76SMatthew Wilcox (Oracle) int nfs_wb_folio_cancel(struct inode *inode, struct folio *folio)
20401b3b4a1aSTrond Myklebust {
20411b3b4a1aSTrond Myklebust 	struct nfs_page *req;
20421b3b4a1aSTrond Myklebust 	int ret = 0;
20431b3b4a1aSTrond Myklebust 
20446d740c76SMatthew Wilcox (Oracle) 	folio_wait_writeback(folio);
20453e217045SWeston Andros Adamson 
20463e217045SWeston Andros Adamson 	/* blocking call to cancel all requests and join to a single (head)
20473e217045SWeston Andros Adamson 	 * request */
20480c493b5cSTrond Myklebust 	req = nfs_lock_and_join_requests(folio);
20493e217045SWeston Andros Adamson 
20503e217045SWeston Andros Adamson 	if (IS_ERR(req)) {
20513e217045SWeston Andros Adamson 		ret = PTR_ERR(req);
20523e217045SWeston Andros Adamson 	} else if (req) {
20536d740c76SMatthew Wilcox (Oracle) 		/* all requests from this folio have been cancelled by
20543e217045SWeston Andros Adamson 		 * nfs_lock_and_join_requests, so just remove the head
20553e217045SWeston Andros Adamson 		 * request from the inode / page_private pointer and
20563e217045SWeston Andros Adamson 		 * release it */
20571b3b4a1aSTrond Myklebust 		nfs_inode_remove_request(req);
20581d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
20591b3b4a1aSTrond Myklebust 	}
20603e217045SWeston Andros Adamson 
20611b3b4a1aSTrond Myklebust 	return ret;
20621b3b4a1aSTrond Myklebust }
20631b3b4a1aSTrond Myklebust 
20645241060eSTrond Myklebust /**
20655241060eSTrond Myklebust  * nfs_wb_folio - Write back all requests on one page
20665241060eSTrond Myklebust  * @inode: pointer to page
20675241060eSTrond Myklebust  * @folio: pointer to folio
20685241060eSTrond Myklebust  *
20695241060eSTrond Myklebust  * Assumes that the folio has been locked by the caller, and will
20705241060eSTrond Myklebust  * not unlock it.
20711c75950bSTrond Myklebust  */
20725241060eSTrond Myklebust int nfs_wb_folio(struct inode *inode, struct folio *folio)
20731c75950bSTrond Myklebust {
2074fada32edSChristoph Hellwig 	loff_t range_start = folio_pos(folio);
2075fada32edSChristoph Hellwig 	size_t len = folio_size(folio);
20767f2f12d9STrond Myklebust 	struct writeback_control wbc = {
20777f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
20787f2f12d9STrond Myklebust 		.nr_to_write = 0,
20797f2f12d9STrond Myklebust 		.range_start = range_start,
2080fada32edSChristoph Hellwig 		.range_end = range_start + len - 1,
20817f2f12d9STrond Myklebust 	};
20827f2f12d9STrond Myklebust 	int ret;
20837f2f12d9STrond Myklebust 
2084fada32edSChristoph Hellwig 	trace_nfs_writeback_folio(inode, range_start, len);
2085f4ce1299STrond Myklebust 
20860522f6adSTrond Myklebust 	for (;;) {
20875241060eSTrond Myklebust 		folio_wait_writeback(folio);
20885241060eSTrond Myklebust 		if (folio_clear_dirty_for_io(folio)) {
20890c493b5cSTrond Myklebust 			ret = nfs_writepage_locked(folio, &wbc);
20907f2f12d9STrond Myklebust 			if (ret < 0)
20917f2f12d9STrond Myklebust 				goto out_error;
20920522f6adSTrond Myklebust 			continue;
20937f2f12d9STrond Myklebust 		}
2094f4ce1299STrond Myklebust 		ret = 0;
20955241060eSTrond Myklebust 		if (!folio_test_private(folio))
20960522f6adSTrond Myklebust 			break;
20970522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
20987f2f12d9STrond Myklebust 		if (ret < 0)
20997f2f12d9STrond Myklebust 			goto out_error;
21007f2f12d9STrond Myklebust 	}
21017f2f12d9STrond Myklebust out_error:
2102fada32edSChristoph Hellwig 	trace_nfs_writeback_folio_done(inode, range_start, len, ret);
21037f2f12d9STrond Myklebust 	return ret;
21041c75950bSTrond Myklebust }
21051c75950bSTrond Myklebust 
2106074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
21074ae84a80SMatthew Wilcox (Oracle) int nfs_migrate_folio(struct address_space *mapping, struct folio *dst,
21084ae84a80SMatthew Wilcox (Oracle) 		struct folio *src, enum migrate_mode mode)
2109074cc1deSTrond Myklebust {
21102da95652SJeff Layton 	/*
21114ae84a80SMatthew Wilcox (Oracle) 	 * If the private flag is set, the folio is currently associated with
21122da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
21132da95652SJeff Layton 	 *
21142da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
21152da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
21164ae84a80SMatthew Wilcox (Oracle) 	 *        the folio lock.
21172da95652SJeff Layton 	 */
21184ae84a80SMatthew Wilcox (Oracle) 	if (folio_test_private(src))
21192da95652SJeff Layton 		return -EBUSY;
2120074cc1deSTrond Myklebust 
21212e9d7e4bSDavid Howells 	if (folio_test_private_2(src)) { /* [DEPRECATED] */
212216f2f4e6SDavid Howells 		if (mode == MIGRATE_ASYNC)
21238c209ce7SDavid Howells 			return -EBUSY;
21242e9d7e4bSDavid Howells 		folio_wait_private_2(src);
212516f2f4e6SDavid Howells 	}
2126074cc1deSTrond Myklebust 
212754184650SMatthew Wilcox (Oracle) 	return migrate_folio(mapping, dst, src, mode);
2128074cc1deSTrond Myklebust }
2129074cc1deSTrond Myklebust #endif
2130074cc1deSTrond Myklebust 
2131f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
21321da177e4SLinus Torvalds {
21331da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
21341e7f3a48SWeston Andros Adamson 					     sizeof(struct nfs_pgio_header),
21351da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
213620c2df83SPaul Mundt 					     NULL);
21371da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
21381da177e4SLinus Torvalds 		return -ENOMEM;
21391da177e4SLinus Torvalds 
214093d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
21411da177e4SLinus Torvalds 						     nfs_wdata_cachep);
21421da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
21433dd4765fSJeff Layton 		goto out_destroy_write_cache;
21441da177e4SLinus Torvalds 
21450b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
21460b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
21470b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
21480b7c0153SFred Isaman 					     NULL);
21490b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
21503dd4765fSJeff Layton 		goto out_destroy_write_mempool;
21510b7c0153SFred Isaman 
215293d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
21534c100210SYanchuan Nian 						      nfs_cdata_cachep);
21541da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
21553dd4765fSJeff Layton 		goto out_destroy_commit_cache;
21561da177e4SLinus Torvalds 
215789a09141SPeter Zijlstra 	/*
215889a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
215989a09141SPeter Zijlstra 	 *
216089a09141SPeter Zijlstra 	 *  64MB:    8192k
216189a09141SPeter Zijlstra 	 * 128MB:   11585k
216289a09141SPeter Zijlstra 	 * 256MB:   16384k
216389a09141SPeter Zijlstra 	 * 512MB:   23170k
216489a09141SPeter Zijlstra 	 *   1GB:   32768k
216589a09141SPeter Zijlstra 	 *   2GB:   46340k
216689a09141SPeter Zijlstra 	 *   4GB:   65536k
216789a09141SPeter Zijlstra 	 *   8GB:   92681k
216889a09141SPeter Zijlstra 	 *  16GB:  131072k
216989a09141SPeter Zijlstra 	 *
217089a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
217189a09141SPeter Zijlstra 	 * Limit the default to 256M
217289a09141SPeter Zijlstra 	 */
2173ca79b0c2SArun KS 	nfs_congestion_kb = (16*int_sqrt(totalram_pages())) << (PAGE_SHIFT-10);
217489a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
217589a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
217689a09141SPeter Zijlstra 
21771da177e4SLinus Torvalds 	return 0;
21783dd4765fSJeff Layton 
21793dd4765fSJeff Layton out_destroy_commit_cache:
21803dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
21813dd4765fSJeff Layton out_destroy_write_mempool:
21823dd4765fSJeff Layton 	mempool_destroy(nfs_wdata_mempool);
21833dd4765fSJeff Layton out_destroy_write_cache:
21843dd4765fSJeff Layton 	kmem_cache_destroy(nfs_wdata_cachep);
21853dd4765fSJeff Layton 	return -ENOMEM;
21861da177e4SLinus Torvalds }
21871da177e4SLinus Torvalds 
2188266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
21891da177e4SLinus Torvalds {
21901da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
21913dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
21921da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
21931a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
21941da177e4SLinus Torvalds }
21951da177e4SLinus Torvalds 
21964a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops = {
21974a0de55cSAnna Schumaker 	.rw_alloc_header	= nfs_writehdr_alloc,
21984a0de55cSAnna Schumaker 	.rw_free_header		= nfs_writehdr_free,
21990eecb214SAnna Schumaker 	.rw_done		= nfs_writeback_done,
22000eecb214SAnna Schumaker 	.rw_result		= nfs_writeback_result,
22011ed26f33SAnna Schumaker 	.rw_initiate		= nfs_initiate_write,
22024a0de55cSAnna Schumaker };
2203