xref: /linux/fs/nfs/write.c (revision b571cfcb9dcac187c6d967987792d37cb0688610)
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;
1850c493b5cSTrond Myklebust 	end = folio_file_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 
2170c493b5cSTrond Myklebust 	folio_set_error(folio);
2186c984083STrond Myklebust 	filemap_set_wb_err(mapping, error);
2196c984083STrond Myklebust 	if (mapping->host)
2206c984083STrond Myklebust 		errseq_set(&mapping->host->i_sb->s_wb_err,
2216c984083STrond Myklebust 			   error == -ENOSPC ? -ENOSPC : -EIO);
222b8946d7bSTrond Myklebust 	nfs_set_pageerror(mapping);
2236fbda89bSTrond Myklebust }
2246fbda89bSTrond Myklebust 
225d72ddcbaSWeston Andros Adamson /*
226d72ddcbaSWeston Andros Adamson  * nfs_page_group_search_locked
227d72ddcbaSWeston Andros Adamson  * @head - head request of page group
228d72ddcbaSWeston Andros Adamson  * @page_offset - offset into page
229d72ddcbaSWeston Andros Adamson  *
230d72ddcbaSWeston Andros Adamson  * Search page group with head @head to find a request that contains the
231d72ddcbaSWeston Andros Adamson  * page offset @page_offset.
232d72ddcbaSWeston Andros Adamson  *
233d72ddcbaSWeston Andros Adamson  * Returns a pointer to the first matching nfs request, or NULL if no
234d72ddcbaSWeston Andros Adamson  * match is found.
235d72ddcbaSWeston Andros Adamson  *
236d72ddcbaSWeston Andros Adamson  * Must be called with the page group lock held
237d72ddcbaSWeston Andros Adamson  */
238d72ddcbaSWeston Andros Adamson static struct nfs_page *
239d72ddcbaSWeston Andros Adamson nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset)
240d72ddcbaSWeston Andros Adamson {
241d72ddcbaSWeston Andros Adamson 	struct nfs_page *req;
242d72ddcbaSWeston Andros Adamson 
243d72ddcbaSWeston Andros Adamson 	req = head;
244d72ddcbaSWeston Andros Adamson 	do {
245d72ddcbaSWeston Andros Adamson 		if (page_offset >= req->wb_pgbase &&
246d72ddcbaSWeston Andros Adamson 		    page_offset < (req->wb_pgbase + req->wb_bytes))
247d72ddcbaSWeston Andros Adamson 			return req;
248d72ddcbaSWeston Andros Adamson 
249d72ddcbaSWeston Andros Adamson 		req = req->wb_this_page;
250d72ddcbaSWeston Andros Adamson 	} while (req != head);
251d72ddcbaSWeston Andros Adamson 
252d72ddcbaSWeston Andros Adamson 	return NULL;
253d72ddcbaSWeston Andros Adamson }
254d72ddcbaSWeston Andros Adamson 
255d72ddcbaSWeston Andros Adamson /*
256d72ddcbaSWeston Andros Adamson  * nfs_page_group_covers_page
257d72ddcbaSWeston Andros Adamson  * @head - head request of page group
258d72ddcbaSWeston Andros Adamson  *
259d72ddcbaSWeston Andros Adamson  * Return true if the page group with head @head covers the whole page,
260d72ddcbaSWeston Andros Adamson  * returns false otherwise
261d72ddcbaSWeston Andros Adamson  */
262d72ddcbaSWeston Andros Adamson static bool nfs_page_group_covers_page(struct nfs_page *req)
263d72ddcbaSWeston Andros Adamson {
2640c493b5cSTrond Myklebust 	unsigned int len = nfs_folio_length(nfs_page_to_folio(req));
265d72ddcbaSWeston Andros Adamson 	struct nfs_page *tmp;
266d72ddcbaSWeston Andros Adamson 	unsigned int pos = 0;
267d72ddcbaSWeston Andros Adamson 
2681344b7eaSTrond Myklebust 	nfs_page_group_lock(req);
269d72ddcbaSWeston Andros Adamson 
2707e8a30f8STrond Myklebust 	for (;;) {
271d72ddcbaSWeston Andros Adamson 		tmp = nfs_page_group_search_locked(req->wb_head, pos);
2727e8a30f8STrond Myklebust 		if (!tmp)
2737e8a30f8STrond Myklebust 			break;
2747e8a30f8STrond Myklebust 		pos = tmp->wb_pgbase + tmp->wb_bytes;
275d72ddcbaSWeston Andros Adamson 	}
276d72ddcbaSWeston Andros Adamson 
277d72ddcbaSWeston Andros Adamson 	nfs_page_group_unlock(req);
2787e8a30f8STrond Myklebust 	return pos >= len;
279d72ddcbaSWeston Andros Adamson }
280d72ddcbaSWeston Andros Adamson 
2811da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
2821da177e4SLinus Torvalds  * covers the full page.
2831da177e4SLinus Torvalds  */
284d72ddcbaSWeston Andros Adamson static void nfs_mark_uptodate(struct nfs_page *req)
2851da177e4SLinus Torvalds {
2860c493b5cSTrond Myklebust 	struct folio *folio = nfs_page_to_folio(req);
2870c493b5cSTrond Myklebust 
2880c493b5cSTrond Myklebust 	if (folio_test_uptodate(folio))
2891da177e4SLinus Torvalds 		return;
290d72ddcbaSWeston Andros Adamson 	if (!nfs_page_group_covers_page(req))
2911da177e4SLinus Torvalds 		return;
2920c493b5cSTrond Myklebust 	folio_mark_uptodate(folio);
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
2961da177e4SLinus Torvalds {
297e87b4c7aSNeilBrown 	int ret = 0;
298cca588d6STrond Myklebust 
299e87b4c7aSNeilBrown 	if (wbc->sync_mode == WB_SYNC_ALL)
300e87b4c7aSNeilBrown 		ret = FLUSH_COND_STABLE;
301e87b4c7aSNeilBrown 	return ret;
3021da177e4SLinus Torvalds }
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds /*
30589a09141SPeter Zijlstra  * NFS congestion control
30689a09141SPeter Zijlstra  */
30789a09141SPeter Zijlstra 
30889a09141SPeter Zijlstra int nfs_congestion_kb;
30989a09141SPeter Zijlstra 
31089a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
31189a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
31289a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
31389a09141SPeter Zijlstra 
3140c493b5cSTrond Myklebust static void nfs_folio_set_writeback(struct folio *folio)
31589a09141SPeter Zijlstra {
3167e8e78a0SChristoph Hellwig 	struct nfs_server *nfss = NFS_SERVER(folio->mapping->host);
3175a6d41b3STrond Myklebust 
3180c493b5cSTrond Myklebust 	folio_start_writeback(folio);
3190c493b5cSTrond Myklebust 	if (atomic_long_inc_return(&nfss->writeback) > NFS_CONGESTION_ON_THRESH)
3206df25e58SNeilBrown 		nfss->write_congested = 1;
32189a09141SPeter Zijlstra }
32289a09141SPeter Zijlstra 
3230c493b5cSTrond Myklebust static void nfs_folio_end_writeback(struct folio *folio)
32489a09141SPeter Zijlstra {
3257e8e78a0SChristoph Hellwig 	struct nfs_server *nfss = NFS_SERVER(folio->mapping->host);
32689a09141SPeter Zijlstra 
3270c493b5cSTrond Myklebust 	folio_end_writeback(folio);
3280c493b5cSTrond Myklebust 	if (atomic_long_dec_return(&nfss->writeback) <
3292f1f3104SJan Kara 	    NFS_CONGESTION_OFF_THRESH) {
3306df25e58SNeilBrown 		nfss->write_congested = 0;
3312f1f3104SJan Kara 		wake_up_all(&nfss->write_congestion_wait);
3322f1f3104SJan Kara 	}
33389a09141SPeter Zijlstra }
33489a09141SPeter Zijlstra 
3350c493b5cSTrond Myklebust static void nfs_page_end_writeback(struct nfs_page *req)
3360c493b5cSTrond Myklebust {
3370c493b5cSTrond Myklebust 	if (nfs_page_group_sync_on_bit(req, PG_WB_END)) {
3380c493b5cSTrond Myklebust 		nfs_unlock_request(req);
3390c493b5cSTrond Myklebust 		nfs_folio_end_writeback(nfs_page_to_folio(req));
3400c493b5cSTrond Myklebust 	} else
3410c493b5cSTrond Myklebust 		nfs_unlock_request(req);
3420c493b5cSTrond Myklebust }
3430c493b5cSTrond Myklebust 
344d4581383SWeston Andros Adamson /*
345d4581383SWeston Andros Adamson  * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests
346d4581383SWeston Andros Adamson  *
347d4581383SWeston Andros Adamson  * @destroy_list - request list (using wb_this_page) terminated by @old_head
348d4581383SWeston Andros Adamson  * @old_head - the old head of the list
349d4581383SWeston Andros Adamson  *
350d4581383SWeston Andros Adamson  * All subrequests must be locked and removed from all lists, so at this point
351d4581383SWeston Andros Adamson  * they are only "active" in this function, and possibly in nfs_wait_on_request
352d4581383SWeston Andros Adamson  * with a reference held by some other context.
353d4581383SWeston Andros Adamson  */
354d4581383SWeston Andros Adamson static void
355d4581383SWeston Andros Adamson nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list,
356b66aaa8dSTrond Myklebust 				 struct nfs_page *old_head,
357b66aaa8dSTrond Myklebust 				 struct inode *inode)
358d4581383SWeston Andros Adamson {
359d4581383SWeston Andros Adamson 	while (destroy_list) {
360d4581383SWeston Andros Adamson 		struct nfs_page *subreq = destroy_list;
361d4581383SWeston Andros Adamson 
362d4581383SWeston Andros Adamson 		destroy_list = (subreq->wb_this_page == old_head) ?
363d4581383SWeston Andros Adamson 				   NULL : subreq->wb_this_page;
364d4581383SWeston Andros Adamson 
36508ca8b21STrond Myklebust 		/* Note: lock subreq in order to change subreq->wb_head */
36608ca8b21STrond Myklebust 		nfs_page_set_headlock(subreq);
367d4581383SWeston Andros Adamson 		WARN_ON_ONCE(old_head != subreq->wb_head);
368d4581383SWeston Andros Adamson 
369d4581383SWeston Andros Adamson 		/* make sure old group is not used */
370d4581383SWeston Andros Adamson 		subreq->wb_this_page = subreq;
37108ca8b21STrond Myklebust 		subreq->wb_head = subreq;
372d4581383SWeston Andros Adamson 
373902a4c00STrond Myklebust 		clear_bit(PG_REMOVE, &subreq->wb_flags);
374902a4c00STrond Myklebust 
3755b2b5187STrond Myklebust 		/* Note: races with nfs_page_group_destroy() */
3765b2b5187STrond Myklebust 		if (!kref_read(&subreq->wb_kref)) {
3775b2b5187STrond Myklebust 			/* Check if we raced with nfs_page_group_destroy() */
37808ca8b21STrond Myklebust 			if (test_and_clear_bit(PG_TEARDOWN, &subreq->wb_flags)) {
37908ca8b21STrond Myklebust 				nfs_page_clear_headlock(subreq);
3805b2b5187STrond Myklebust 				nfs_free_request(subreq);
38108ca8b21STrond Myklebust 			} else
38208ca8b21STrond Myklebust 				nfs_page_clear_headlock(subreq);
3835b2b5187STrond Myklebust 			continue;
3845b2b5187STrond Myklebust 		}
38508ca8b21STrond Myklebust 		nfs_page_clear_headlock(subreq);
386d4581383SWeston Andros Adamson 
387add42de3STrond Myklebust 		nfs_release_request(old_head);
3885b2b5187STrond Myklebust 
389b66aaa8dSTrond Myklebust 		if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags)) {
390d4581383SWeston Andros Adamson 			nfs_release_request(subreq);
391a6b6d5b8STrond Myklebust 			atomic_long_dec(&NFS_I(inode)->nrequests);
392d4581383SWeston Andros Adamson 		}
3935b2b5187STrond Myklebust 
3945b2b5187STrond Myklebust 		/* subreq is now totally disconnected from page group or any
3955b2b5187STrond Myklebust 		 * write / commit lists. last chance to wake any waiters */
3965b2b5187STrond Myklebust 		nfs_unlock_and_release_request(subreq);
397d4581383SWeston Andros Adamson 	}
398d4581383SWeston Andros Adamson }
399d4581383SWeston Andros Adamson 
400d4581383SWeston Andros Adamson /*
401e00ed89dSTrond Myklebust  * nfs_join_page_group - destroy subrequests of the head req
402e00ed89dSTrond Myklebust  * @head: the page used to lookup the "page group" of nfs_page structures
403e00ed89dSTrond Myklebust  * @inode: Inode to which the request belongs.
404d4581383SWeston Andros Adamson  *
405d4581383SWeston Andros Adamson  * This function joins all sub requests to the head request by first
406d4581383SWeston Andros Adamson  * locking all requests in the group, cancelling any pending operations
407d4581383SWeston Andros Adamson  * and finally updating the head request to cover the whole range covered by
408d4581383SWeston Andros Adamson  * the (former) group.  All subrequests are removed from any write or commit
409d4581383SWeston Andros Adamson  * lists, unlinked from the group and destroyed.
410d4581383SWeston Andros Adamson  */
411b193a78dSTrond Myklebust void nfs_join_page_group(struct nfs_page *head, struct nfs_commit_info *cinfo,
412b193a78dSTrond Myklebust 			 struct inode *inode)
413d4581383SWeston Andros Adamson {
414e00ed89dSTrond Myklebust 	struct nfs_page *subreq;
415d4581383SWeston Andros Adamson 	struct nfs_page *destroy_list = NULL;
416a62f8e3bSTrond Myklebust 	unsigned int pgbase, off, bytes;
417a62f8e3bSTrond Myklebust 
418a62f8e3bSTrond Myklebust 	pgbase = head->wb_pgbase;
419a62f8e3bSTrond Myklebust 	bytes = head->wb_bytes;
420a62f8e3bSTrond Myklebust 	off = head->wb_offset;
421a0e265bcSTrond Myklebust 	for (subreq = head->wb_this_page; subreq != head;
422a0e265bcSTrond Myklebust 			subreq = subreq->wb_this_page) {
423a62f8e3bSTrond Myklebust 		/* Subrequests should always form a contiguous range */
424a62f8e3bSTrond Myklebust 		if (pgbase > subreq->wb_pgbase) {
425a62f8e3bSTrond Myklebust 			off -= pgbase - subreq->wb_pgbase;
426a62f8e3bSTrond Myklebust 			bytes += pgbase - subreq->wb_pgbase;
427a62f8e3bSTrond Myklebust 			pgbase = subreq->wb_pgbase;
428a62f8e3bSTrond Myklebust 		}
429a62f8e3bSTrond Myklebust 		bytes = max(subreq->wb_pgbase + subreq->wb_bytes
430a62f8e3bSTrond Myklebust 				- pgbase, bytes);
4311bd5d6d0STrond Myklebust 	}
4321bd5d6d0STrond Myklebust 
433a62f8e3bSTrond Myklebust 	/* Set the head request's range to cover the former page group */
434a62f8e3bSTrond Myklebust 	head->wb_pgbase = pgbase;
435a62f8e3bSTrond Myklebust 	head->wb_bytes = bytes;
436a62f8e3bSTrond Myklebust 	head->wb_offset = off;
437d4581383SWeston Andros Adamson 
438d4581383SWeston Andros Adamson 	/* Now that all requests are locked, make sure they aren't on any list.
439d4581383SWeston Andros Adamson 	 * Commit list removal accounting is done after locks are dropped */
440d4581383SWeston Andros Adamson 	subreq = head;
441d4581383SWeston Andros Adamson 	do {
442b193a78dSTrond Myklebust 		nfs_clear_request_commit(cinfo, subreq);
443d4581383SWeston Andros Adamson 		subreq = subreq->wb_this_page;
444d4581383SWeston Andros Adamson 	} while (subreq != head);
445d4581383SWeston Andros Adamson 
446d4581383SWeston Andros Adamson 	/* unlink subrequests from head, destroy them later */
447d4581383SWeston Andros Adamson 	if (head->wb_this_page != head) {
448d4581383SWeston Andros Adamson 		/* destroy list will be terminated by head */
449d4581383SWeston Andros Adamson 		destroy_list = head->wb_this_page;
450d4581383SWeston Andros Adamson 		head->wb_this_page = head;
451d4581383SWeston Andros Adamson 	}
452d4581383SWeston Andros Adamson 
453b66aaa8dSTrond Myklebust 	nfs_destroy_unlinked_subrequests(destroy_list, head, inode);
454b5bab9bfSTrond Myklebust }
455b5bab9bfSTrond Myklebust 
456f1b7c755SChristoph Hellwig /**
457f1b7c755SChristoph Hellwig  * nfs_wait_on_request - Wait for a request to complete.
458f1b7c755SChristoph Hellwig  * @req: request to wait upon.
459f1b7c755SChristoph Hellwig  *
460f1b7c755SChristoph Hellwig  * Interruptible by fatal signals only.
461f1b7c755SChristoph Hellwig  * The user is responsible for holding a count on the request.
462f1b7c755SChristoph Hellwig  */
463f1b7c755SChristoph Hellwig static int nfs_wait_on_request(struct nfs_page *req)
464f1b7c755SChristoph Hellwig {
465f1b7c755SChristoph Hellwig 	if (!test_bit(PG_BUSY, &req->wb_flags))
466f1b7c755SChristoph Hellwig 		return 0;
467f1b7c755SChristoph Hellwig 	set_bit(PG_CONTENDED2, &req->wb_flags);
468f1b7c755SChristoph Hellwig 	smp_mb__after_atomic();
469f1b7c755SChristoph Hellwig 	return wait_on_bit_io(&req->wb_flags, PG_BUSY,
470f1b7c755SChristoph Hellwig 			      TASK_UNINTERRUPTIBLE);
471f1b7c755SChristoph Hellwig }
472f1b7c755SChristoph Hellwig 
473e00ed89dSTrond Myklebust /*
47425edbcacSChristoph Hellwig  * nfs_unroll_locks -  unlock all newly locked reqs and wait on @req
47525edbcacSChristoph Hellwig  * @head: head request of page group, must be holding head lock
47625edbcacSChristoph Hellwig  * @req: request that couldn't lock and needs to wait on the req bit lock
47725edbcacSChristoph Hellwig  *
47825edbcacSChristoph Hellwig  * This is a helper function for nfs_lock_and_join_requests
47925edbcacSChristoph Hellwig  * returns 0 on success, < 0 on error.
48025edbcacSChristoph Hellwig  */
48125edbcacSChristoph Hellwig static void
48225edbcacSChristoph Hellwig nfs_unroll_locks(struct nfs_page *head, struct nfs_page *req)
48325edbcacSChristoph Hellwig {
48425edbcacSChristoph Hellwig 	struct nfs_page *tmp;
48525edbcacSChristoph Hellwig 
48625edbcacSChristoph Hellwig 	/* relinquish all the locks successfully grabbed this run */
48725edbcacSChristoph Hellwig 	for (tmp = head->wb_this_page ; tmp != req; tmp = tmp->wb_this_page) {
48825edbcacSChristoph Hellwig 		if (!kref_read(&tmp->wb_kref))
48925edbcacSChristoph Hellwig 			continue;
49025edbcacSChristoph Hellwig 		nfs_unlock_and_release_request(tmp);
49125edbcacSChristoph Hellwig 	}
49225edbcacSChristoph Hellwig }
49325edbcacSChristoph Hellwig 
49425edbcacSChristoph Hellwig /*
49525edbcacSChristoph Hellwig  * nfs_page_group_lock_subreq -  try to lock a subrequest
49625edbcacSChristoph Hellwig  * @head: head request of page group
49725edbcacSChristoph Hellwig  * @subreq: request to lock
49825edbcacSChristoph Hellwig  *
49925edbcacSChristoph Hellwig  * This is a helper function for nfs_lock_and_join_requests which
50025edbcacSChristoph Hellwig  * must be called with the head request and page group both locked.
50125edbcacSChristoph Hellwig  * On error, it returns with the page group unlocked.
50225edbcacSChristoph Hellwig  */
50325edbcacSChristoph Hellwig static int
50425edbcacSChristoph Hellwig nfs_page_group_lock_subreq(struct nfs_page *head, struct nfs_page *subreq)
50525edbcacSChristoph Hellwig {
50625edbcacSChristoph Hellwig 	int ret;
50725edbcacSChristoph Hellwig 
50825edbcacSChristoph Hellwig 	if (!kref_get_unless_zero(&subreq->wb_kref))
50925edbcacSChristoph Hellwig 		return 0;
51025edbcacSChristoph Hellwig 	while (!nfs_lock_request(subreq)) {
51125edbcacSChristoph Hellwig 		nfs_page_group_unlock(head);
51225edbcacSChristoph Hellwig 		ret = nfs_wait_on_request(subreq);
51325edbcacSChristoph Hellwig 		if (!ret)
51425edbcacSChristoph Hellwig 			ret = nfs_page_group_lock(head);
51525edbcacSChristoph Hellwig 		if (ret < 0) {
51625edbcacSChristoph Hellwig 			nfs_unroll_locks(head, subreq);
51725edbcacSChristoph Hellwig 			nfs_release_request(subreq);
51825edbcacSChristoph Hellwig 			return ret;
51925edbcacSChristoph Hellwig 		}
52025edbcacSChristoph Hellwig 	}
52125edbcacSChristoph Hellwig 	return 0;
52225edbcacSChristoph Hellwig }
52325edbcacSChristoph Hellwig 
52425edbcacSChristoph Hellwig /*
525e00ed89dSTrond Myklebust  * nfs_lock_and_join_requests - join all subreqs to the head req
5260c493b5cSTrond Myklebust  * @folio: the folio used to lookup the "page group" of nfs_page structures
527e00ed89dSTrond Myklebust  *
528e00ed89dSTrond Myklebust  * This function joins all sub requests to the head request by first
529e00ed89dSTrond Myklebust  * locking all requests in the group, cancelling any pending operations
530e00ed89dSTrond Myklebust  * and finally updating the head request to cover the whole range covered by
531e00ed89dSTrond Myklebust  * the (former) group.  All subrequests are removed from any write or commit
532e00ed89dSTrond Myklebust  * lists, unlinked from the group and destroyed.
533e00ed89dSTrond Myklebust  *
534e00ed89dSTrond Myklebust  * Returns a locked, referenced pointer to the head request - which after
535e00ed89dSTrond Myklebust  * this call is guaranteed to be the only request associated with the page.
5360c493b5cSTrond Myklebust  * Returns NULL if no requests are found for @folio, or a ERR_PTR if an
537e00ed89dSTrond Myklebust  * error was encountered.
538e00ed89dSTrond Myklebust  */
5390c493b5cSTrond Myklebust static struct nfs_page *nfs_lock_and_join_requests(struct folio *folio)
540e00ed89dSTrond Myklebust {
5417e8e78a0SChristoph Hellwig 	struct inode *inode = folio->mapping->host;
54225edbcacSChristoph Hellwig 	struct nfs_page *head, *subreq;
543b193a78dSTrond Myklebust 	struct nfs_commit_info cinfo;
544*b571cfcbSChristoph Hellwig 	bool removed;
545e00ed89dSTrond Myklebust 	int ret;
546e00ed89dSTrond Myklebust 
547e00ed89dSTrond Myklebust 	/*
548e00ed89dSTrond Myklebust 	 * A reference is taken only on the head request which acts as a
549e00ed89dSTrond Myklebust 	 * reference to the whole page group - the group will not be destroyed
550e00ed89dSTrond Myklebust 	 * until the head reference is released.
551e00ed89dSTrond Myklebust 	 */
552c3f22357SChristoph Hellwig retry:
553c3f22357SChristoph Hellwig 	head = nfs_folio_find_head_request(folio);
554c3f22357SChristoph Hellwig 	if (!head)
555c3f22357SChristoph Hellwig 		return NULL;
5560671d8f1SMarkus Elfring 
557c3f22357SChristoph Hellwig 	while (!nfs_lock_request(head)) {
558c3f22357SChristoph Hellwig 		ret = nfs_wait_on_request(head);
559c3f22357SChristoph Hellwig 		if (ret < 0)
5600671d8f1SMarkus Elfring 			return ERR_PTR(ret);
561612c9384STrond Myklebust 	}
562074cc1deSTrond Myklebust 
563c3f22357SChristoph Hellwig 	/* Ensure that nobody removed the request before we locked it */
564c3f22357SChristoph Hellwig 	if (head != folio->private) {
565c3f22357SChristoph Hellwig 		nfs_unlock_and_release_request(head);
566c3f22357SChristoph Hellwig 		goto retry;
567c3f22357SChristoph Hellwig 	}
568e00ed89dSTrond Myklebust 
56925edbcacSChristoph Hellwig 	ret = nfs_page_group_lock(head);
570c3f22357SChristoph Hellwig 	if (ret < 0)
571c3f22357SChristoph Hellwig 		goto out_unlock;
572c3f22357SChristoph Hellwig 
573*b571cfcbSChristoph Hellwig 	removed = test_bit(PG_REMOVE, &head->wb_flags);
574*b571cfcbSChristoph Hellwig 
57525edbcacSChristoph Hellwig 	/* lock each request in the page group */
57625edbcacSChristoph Hellwig 	for (subreq = head->wb_this_page;
57725edbcacSChristoph Hellwig 	     subreq != head;
57825edbcacSChristoph Hellwig 	     subreq = subreq->wb_this_page) {
579*b571cfcbSChristoph Hellwig 		if (test_bit(PG_REMOVE, &subreq->wb_flags))
580*b571cfcbSChristoph Hellwig 			removed = true;
58125edbcacSChristoph Hellwig 		ret = nfs_page_group_lock_subreq(head, subreq);
58225edbcacSChristoph Hellwig 		if (ret < 0)
58325edbcacSChristoph Hellwig 			goto out_unlock;
58425edbcacSChristoph Hellwig 	}
58525edbcacSChristoph Hellwig 
58625edbcacSChristoph Hellwig 	nfs_page_group_unlock(head);
58725edbcacSChristoph Hellwig 
588*b571cfcbSChristoph Hellwig 	/*
589*b571cfcbSChristoph Hellwig 	 * If PG_REMOVE is set on any request, I/O on that request has
590*b571cfcbSChristoph Hellwig 	 * completed, but some requests were still under I/O at the time
591*b571cfcbSChristoph Hellwig 	 * we locked the head request.
592*b571cfcbSChristoph Hellwig 	 *
593*b571cfcbSChristoph Hellwig 	 * In that case the above wait for all requests means that all I/O
594*b571cfcbSChristoph Hellwig 	 * has now finished, and we can restart from a clean slate.  Let the
595*b571cfcbSChristoph Hellwig 	 * old requests go away and start from scratch instead.
596*b571cfcbSChristoph Hellwig 	 */
597*b571cfcbSChristoph Hellwig 	if (removed) {
598*b571cfcbSChristoph Hellwig 		nfs_unroll_locks(head, head);
599*b571cfcbSChristoph Hellwig 		nfs_unlock_and_release_request(head);
600*b571cfcbSChristoph Hellwig 		goto retry;
601*b571cfcbSChristoph Hellwig 	}
602*b571cfcbSChristoph Hellwig 
603c3f22357SChristoph Hellwig 	nfs_init_cinfo_from_inode(&cinfo, inode);
604c3f22357SChristoph Hellwig 	nfs_join_page_group(head, &cinfo, inode);
605e00ed89dSTrond Myklebust 	return head;
606c3f22357SChristoph Hellwig 
607c3f22357SChristoph Hellwig out_unlock:
608c3f22357SChristoph Hellwig 	nfs_unlock_and_release_request(head);
609c3f22357SChristoph Hellwig 	return ERR_PTR(ret);
610e00ed89dSTrond Myklebust }
611e00ed89dSTrond Myklebust 
6126fbda89bSTrond Myklebust static void nfs_write_error(struct nfs_page *req, int error)
6130bcbf039SPeng Tao {
6146dd85e83STrond Myklebust 	trace_nfs_write_error(nfs_page_to_inode(req), req, error);
6150c493b5cSTrond Myklebust 	nfs_mapping_set_error(nfs_page_to_folio(req), error);
61606c9fdf3STrond Myklebust 	nfs_inode_remove_request(req);
6170c493b5cSTrond Myklebust 	nfs_page_end_writeback(req);
6181f84ccdfSFred Isaman 	nfs_release_request(req);
6190bcbf039SPeng Tao }
6200bcbf039SPeng Tao 
621074cc1deSTrond Myklebust /*
622074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
623074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
624074cc1deSTrond Myklebust  */
6250c493b5cSTrond Myklebust static int nfs_page_async_flush(struct folio *folio,
626c6fd3511STrond Myklebust 				struct writeback_control *wbc,
627c6fd3511STrond Myklebust 				struct nfs_pageio_descriptor *pgio)
628074cc1deSTrond Myklebust {
629074cc1deSTrond Myklebust 	struct nfs_page *req;
630074cc1deSTrond Myklebust 	int ret = 0;
631074cc1deSTrond Myklebust 
6320c493b5cSTrond Myklebust 	req = nfs_lock_and_join_requests(folio);
633074cc1deSTrond Myklebust 	if (!req)
634074cc1deSTrond Myklebust 		goto out;
635074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
636074cc1deSTrond Myklebust 	if (IS_ERR(req))
637074cc1deSTrond Myklebust 		goto out;
638074cc1deSTrond Myklebust 
6390c493b5cSTrond Myklebust 	nfs_folio_set_writeback(folio);
640deed85e7STrond Myklebust 	WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
641074cc1deSTrond Myklebust 
642a6598813STrond Myklebust 	/* If there is a fatal error that covers this write, just exit */
64396c41455STrond Myklebust 	ret = pgio->pg_error;
64496c41455STrond Myklebust 	if (nfs_error_is_fatal_on_server(ret))
645a6598813STrond Myklebust 		goto out_launder;
646a6598813STrond Myklebust 
64796c41455STrond Myklebust 	ret = 0;
648f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
649074cc1deSTrond Myklebust 		ret = pgio->pg_error;
6500bcbf039SPeng Tao 		/*
651c373fff7STrond Myklebust 		 * Remove the problematic req upon fatal errors on the server
6520bcbf039SPeng Tao 		 */
653c373fff7STrond Myklebust 		if (nfs_error_is_fatal_on_server(ret))
654a6598813STrond Myklebust 			goto out_launder;
655c6fd3511STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE)
656c6fd3511STrond Myklebust 			ret = AOP_WRITEPAGE_ACTIVATE;
6570c493b5cSTrond Myklebust 		folio_redirty_for_writepage(wbc, folio);
6588fc75bedSTrond Myklebust 		nfs_redirty_request(req);
65996c41455STrond Myklebust 		pgio->pg_error = 0;
66040f90271STrond Myklebust 	} else
6617e8e78a0SChristoph Hellwig 		nfs_add_stats(folio->mapping->host,
66240f90271STrond Myklebust 			      NFSIOS_WRITEPAGES, 1);
663074cc1deSTrond Myklebust out:
664074cc1deSTrond Myklebust 	return ret;
665a6598813STrond Myklebust out_launder:
6666fbda89bSTrond Myklebust 	nfs_write_error(req, ret);
66714bebe3cSTrond Myklebust 	return 0;
668e261f51fSTrond Myklebust }
669e261f51fSTrond Myklebust 
6700c493b5cSTrond Myklebust static int nfs_do_writepage(struct folio *folio, struct writeback_control *wbc,
671c373fff7STrond Myklebust 			    struct nfs_pageio_descriptor *pgio)
672f758c885STrond Myklebust {
6737e8e78a0SChristoph Hellwig 	nfs_pageio_cond_complete(pgio, folio->index);
6740c493b5cSTrond Myklebust 	return nfs_page_async_flush(folio, wbc, pgio);
675f758c885STrond Myklebust }
676f758c885STrond Myklebust 
677e261f51fSTrond Myklebust /*
6781da177e4SLinus Torvalds  * Write an mmapped page to the server.
6791da177e4SLinus Torvalds  */
6800c493b5cSTrond Myklebust static int nfs_writepage_locked(struct folio *folio,
681c373fff7STrond Myklebust 				struct writeback_control *wbc)
6821da177e4SLinus Torvalds {
683f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
6847e8e78a0SChristoph Hellwig 	struct inode *inode = folio->mapping->host;
685e261f51fSTrond Myklebust 	int err;
6861da177e4SLinus Torvalds 
68740f90271STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
6880c493b5cSTrond Myklebust 	nfs_pageio_init_write(&pgio, inode, 0, false,
6890c493b5cSTrond Myklebust 			      &nfs_async_write_completion_ops);
6900c493b5cSTrond Myklebust 	err = nfs_do_writepage(folio, wbc, &pgio);
69196c41455STrond Myklebust 	pgio.pg_error = 0;
692f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
6934d770ccfSTrond Myklebust 	return err;
6944d770ccfSTrond Myklebust }
6954d770ccfSTrond Myklebust 
696d585bdbeSMatthew Wilcox (Oracle) static int nfs_writepages_callback(struct folio *folio,
6970c493b5cSTrond Myklebust 				   struct writeback_control *wbc, void *data)
698f758c885STrond Myklebust {
699f758c885STrond Myklebust 	int ret;
700f758c885STrond Myklebust 
7010c493b5cSTrond Myklebust 	ret = nfs_do_writepage(folio, wbc, data);
70296c41455STrond Myklebust 	if (ret != AOP_WRITEPAGE_ACTIVATE)
703d585bdbeSMatthew Wilcox (Oracle) 		folio_unlock(folio);
704f758c885STrond Myklebust 	return ret;
7051da177e4SLinus Torvalds }
7061da177e4SLinus Torvalds 
707919e3bd9STrond Myklebust static void nfs_io_completion_commit(void *inode)
708919e3bd9STrond Myklebust {
709919e3bd9STrond Myklebust 	nfs_commit_inode(inode, 0);
710919e3bd9STrond Myklebust }
711919e3bd9STrond Myklebust 
7121da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
7131da177e4SLinus Torvalds {
7141da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
715c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
716ed7bcdb3STrond Myklebust 	struct nfs_io_completion *ioc = NULL;
717ed7bcdb3STrond Myklebust 	unsigned int mntflags = NFS_SERVER(inode)->flags;
7182f1f3104SJan Kara 	struct nfs_server *nfss = NFS_SERVER(inode);
719ed7bcdb3STrond Myklebust 	int priority = 0;
7201da177e4SLinus Torvalds 	int err;
7211da177e4SLinus Torvalds 
7222f1f3104SJan Kara 	/* Wait with writeback until write congestion eases */
7232f1f3104SJan Kara 	if (wbc->sync_mode == WB_SYNC_NONE && nfss->write_congested) {
7242f1f3104SJan Kara 		err = wait_event_killable(nfss->write_congestion_wait,
7252f1f3104SJan Kara 					  nfss->write_congested == 0);
7262f1f3104SJan Kara 		if (err)
7272f1f3104SJan Kara 			return err;
7282f1f3104SJan Kara 	}
7296df25e58SNeilBrown 
73091d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
73191d5b470SChuck Lever 
732ed7bcdb3STrond Myklebust 	if (!(mntflags & NFS_MOUNT_WRITE_EAGER) || wbc->for_kupdate ||
733ed7bcdb3STrond Myklebust 	    wbc->for_background || wbc->for_sync || wbc->for_reclaim) {
7342b17d725STrond Myklebust 		ioc = nfs_io_completion_alloc(GFP_KERNEL);
735919e3bd9STrond Myklebust 		if (ioc)
736ed7bcdb3STrond Myklebust 			nfs_io_completion_init(ioc, nfs_io_completion_commit,
737ed7bcdb3STrond Myklebust 					       inode);
738ed7bcdb3STrond Myklebust 		priority = wb_priority(wbc);
739ed7bcdb3STrond Myklebust 	}
740919e3bd9STrond Myklebust 
741c6fd3511STrond Myklebust 	do {
742ed7bcdb3STrond Myklebust 		nfs_pageio_init_write(&pgio, inode, priority, false,
743a20c93e3SChristoph Hellwig 				      &nfs_async_write_completion_ops);
744919e3bd9STrond Myklebust 		pgio.pg_io_completion = ioc;
745c6fd3511STrond Myklebust 		err = write_cache_pages(mapping, wbc, nfs_writepages_callback,
746c6fd3511STrond Myklebust 					&pgio);
74796c41455STrond Myklebust 		pgio.pg_error = 0;
748c63c7b05STrond Myklebust 		nfs_pageio_complete(&pgio);
7496e7434abSTrond Myklebust 		if (err == -EAGAIN && mntflags & NFS_MOUNT_SOFTERR)
7506e7434abSTrond Myklebust 			break;
751c6fd3511STrond Myklebust 	} while (err < 0 && !nfs_error_is_fatal(err));
752919e3bd9STrond Myklebust 	nfs_io_completion_put(ioc);
75372cb77f4STrond Myklebust 
754f758c885STrond Myklebust 	if (err < 0)
75572cb77f4STrond Myklebust 		goto out_err;
756c63c7b05STrond Myklebust 	return 0;
75772cb77f4STrond Myklebust out_err:
75872cb77f4STrond Myklebust 	return err;
7591da177e4SLinus Torvalds }
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds /*
7621da177e4SLinus Torvalds  * Insert a write request into an inode
7631da177e4SLinus Torvalds  */
7640c493b5cSTrond Myklebust static void nfs_inode_add_request(struct nfs_page *req)
7651da177e4SLinus Torvalds {
7660c493b5cSTrond Myklebust 	struct folio *folio = nfs_page_to_folio(req);
7677e8e78a0SChristoph Hellwig 	struct address_space *mapping = folio->mapping;
7680c493b5cSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(mapping->host);
769e7d39069STrond Myklebust 
7702bfc6e56SWeston Andros Adamson 	WARN_ON_ONCE(req->wb_this_page != req);
7712bfc6e56SWeston Andros Adamson 
772e7d39069STrond Myklebust 	/* Lock the request! */
7737ad84aa9STrond Myklebust 	nfs_lock_request(req);
774600f111eSMatthew Wilcox (Oracle) 	spin_lock(&mapping->i_private_lock);
7752df485a7STrond Myklebust 	set_bit(PG_MAPPED, &req->wb_flags);
7760c493b5cSTrond Myklebust 	folio_set_private(folio);
7770c493b5cSTrond Myklebust 	folio->private = req;
778600f111eSMatthew Wilcox (Oracle) 	spin_unlock(&mapping->i_private_lock);
779a6b6d5b8STrond Myklebust 	atomic_long_inc(&nfsi->nrequests);
78017089a29SWeston Andros Adamson 	/* this a head request for a page group - mark it as having an
781cb1410c7SWeston Andros Adamson 	 * extra reference so sub groups can follow suit.
782cb1410c7SWeston Andros Adamson 	 * This flag also informs pgio layer when to bump nrequests when
783cb1410c7SWeston Andros Adamson 	 * adding subrequests. */
78417089a29SWeston Andros Adamson 	WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags));
785c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
7861da177e4SLinus Torvalds }
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds /*
78989a09141SPeter Zijlstra  * Remove a write request from an inode
7901da177e4SLinus Torvalds  */
7911da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
7921da177e4SLinus Torvalds {
7936a6d4644SScott Mayhew 	struct nfs_inode *nfsi = NFS_I(nfs_page_to_inode(req));
7946a6d4644SScott Mayhew 
79520633f04SWeston Andros Adamson 	if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) {
7960c493b5cSTrond Myklebust 		struct folio *folio = nfs_page_to_folio(req->wb_head);
7977e8e78a0SChristoph Hellwig 		struct address_space *mapping = folio->mapping;
7981da177e4SLinus Torvalds 
799600f111eSMatthew Wilcox (Oracle) 		spin_lock(&mapping->i_private_lock);
8007e8e78a0SChristoph Hellwig 		if (likely(folio)) {
8010c493b5cSTrond Myklebust 			folio->private = NULL;
8020c493b5cSTrond Myklebust 			folio_clear_private(folio);
8030c493b5cSTrond Myklebust 			clear_bit(PG_MAPPED, &req->wb_head->wb_flags);
80429418aa4SMel Gorman 		}
805600f111eSMatthew Wilcox (Oracle) 		spin_unlock(&mapping->i_private_lock);
80620633f04SWeston Andros Adamson 	}
80717089a29SWeston Andros Adamson 
80833ea5aaaSZhangXiaoxu 	if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags)) {
8096a6d4644SScott Mayhew 		atomic_long_dec(&nfsi->nrequests);
810dd1b2026SJeff Layton 		nfs_release_request(req);
81133ea5aaaSZhangXiaoxu 	}
8121da177e4SLinus Torvalds }
8131da177e4SLinus Torvalds 
8140c493b5cSTrond Myklebust static void nfs_mark_request_dirty(struct nfs_page *req)
81561822ab5STrond Myklebust {
8160c493b5cSTrond Myklebust 	struct folio *folio = nfs_page_to_folio(req);
8170c493b5cSTrond Myklebust 	if (folio)
8180c493b5cSTrond Myklebust 		filemap_dirty_folio(folio_mapping(folio), folio);
81961822ab5STrond Myklebust }
82061822ab5STrond Myklebust 
8218dd37758STrond Myklebust /**
82286d80f97STrond Myklebust  * nfs_request_add_commit_list_locked - add request to a commit list
82386d80f97STrond Myklebust  * @req: pointer to a struct nfs_page
82486d80f97STrond Myklebust  * @dst: commit list head
82586d80f97STrond Myklebust  * @cinfo: holds list lock and accounting info
82686d80f97STrond Myklebust  *
82786d80f97STrond Myklebust  * This sets the PG_CLEAN bit, updates the cinfo count of
82886d80f97STrond Myklebust  * number of outstanding requests requiring a commit as well as
82986d80f97STrond Myklebust  * the MM page stats.
83086d80f97STrond Myklebust  *
831e824f99aSTrond Myklebust  * The caller must hold NFS_I(cinfo->inode)->commit_mutex, and the
832e824f99aSTrond Myklebust  * nfs_page lock.
83386d80f97STrond Myklebust  */
83486d80f97STrond Myklebust void
83586d80f97STrond Myklebust nfs_request_add_commit_list_locked(struct nfs_page *req, struct list_head *dst,
83686d80f97STrond Myklebust 			    struct nfs_commit_info *cinfo)
83786d80f97STrond Myklebust {
83886d80f97STrond Myklebust 	set_bit(PG_CLEAN, &req->wb_flags);
83986d80f97STrond Myklebust 	nfs_list_add_request(req, dst);
8405cb953d4STrond Myklebust 	atomic_long_inc(&cinfo->mds->ncommit);
84186d80f97STrond Myklebust }
84286d80f97STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked);
84386d80f97STrond Myklebust 
84486d80f97STrond Myklebust /**
8458dd37758STrond Myklebust  * nfs_request_add_commit_list - add request to a commit list
8468dd37758STrond Myklebust  * @req: pointer to a struct nfs_page
847ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
8488dd37758STrond Myklebust  *
849ea2cf228SFred Isaman  * This sets the PG_CLEAN bit, updates the cinfo count of
8508dd37758STrond Myklebust  * number of outstanding requests requiring a commit as well as
8518dd37758STrond Myklebust  * the MM page stats.
8528dd37758STrond Myklebust  *
853ea2cf228SFred Isaman  * The caller must _not_ hold the cinfo->lock, but must be
8548dd37758STrond Myklebust  * holding the nfs_page lock.
8558dd37758STrond Myklebust  */
8568dd37758STrond Myklebust void
8576272dcc6SAnna Schumaker nfs_request_add_commit_list(struct nfs_page *req, struct nfs_commit_info *cinfo)
8588dd37758STrond Myklebust {
859e824f99aSTrond Myklebust 	mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
8606272dcc6SAnna Schumaker 	nfs_request_add_commit_list_locked(req, &cinfo->mds->list, cinfo);
861e824f99aSTrond Myklebust 	mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
8620c493b5cSTrond Myklebust 	nfs_folio_mark_unstable(nfs_page_to_folio(req), cinfo);
8638dd37758STrond Myklebust }
8648dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
8658dd37758STrond Myklebust 
8668dd37758STrond Myklebust /**
8678dd37758STrond Myklebust  * nfs_request_remove_commit_list - Remove request from a commit list
8688dd37758STrond Myklebust  * @req: pointer to a nfs_page
869ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
8708dd37758STrond Myklebust  *
871ea2cf228SFred Isaman  * This clears the PG_CLEAN bit, and updates the cinfo's count of
8728dd37758STrond Myklebust  * number of outstanding requests requiring a commit
8738dd37758STrond Myklebust  * It does not update the MM page stats.
8748dd37758STrond Myklebust  *
875ea2cf228SFred Isaman  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
8768dd37758STrond Myklebust  */
8778dd37758STrond Myklebust void
878ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req,
879ea2cf228SFred Isaman 			       struct nfs_commit_info *cinfo)
8808dd37758STrond Myklebust {
8818dd37758STrond Myklebust 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
8828dd37758STrond Myklebust 		return;
8838dd37758STrond Myklebust 	nfs_list_remove_request(req);
8845cb953d4STrond Myklebust 	atomic_long_dec(&cinfo->mds->ncommit);
8858dd37758STrond Myklebust }
8868dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
8878dd37758STrond Myklebust 
888ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
889ea2cf228SFred Isaman 				      struct inode *inode)
890ea2cf228SFred Isaman {
891fe238e60SDave Wysochanski 	cinfo->inode = inode;
892ea2cf228SFred Isaman 	cinfo->mds = &NFS_I(inode)->commit_info;
893ea2cf228SFred Isaman 	cinfo->ds = pnfs_get_ds_info(inode);
894b359f9d0SFred Isaman 	cinfo->dreq = NULL;
895f453a54aSFred Isaman 	cinfo->completion_ops = &nfs_commit_completion_ops;
896ea2cf228SFred Isaman }
897ea2cf228SFred Isaman 
898ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
899ea2cf228SFred Isaman 		    struct inode *inode,
900ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq)
901ea2cf228SFred Isaman {
9021763da12SFred Isaman 	if (dreq)
9031763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
9041763da12SFred Isaman 	else
905ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(cinfo, inode);
906ea2cf228SFred Isaman }
907ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo);
9088dd37758STrond Myklebust 
9091da177e4SLinus Torvalds /*
9101da177e4SLinus Torvalds  * Add a request to the inode's commit list.
9111da177e4SLinus Torvalds  */
9121763da12SFred Isaman void
913ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
914b57ff130SWeston Andros Adamson 			struct nfs_commit_info *cinfo, u32 ds_commit_idx)
9151da177e4SLinus Torvalds {
916b57ff130SWeston Andros Adamson 	if (pnfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx))
9178dd37758STrond Myklebust 		return;
9186272dcc6SAnna Schumaker 	nfs_request_add_commit_list(req, cinfo);
9191da177e4SLinus Torvalds }
9208e821cadSTrond Myklebust 
9210c493b5cSTrond Myklebust static void nfs_folio_clear_commit(struct folio *folio)
922e468bae9STrond Myklebust {
9230c493b5cSTrond Myklebust 	if (folio) {
9240c493b5cSTrond Myklebust 		long nr = folio_nr_pages(folio);
9250c493b5cSTrond Myklebust 
9260c493b5cSTrond Myklebust 		node_stat_mod_folio(folio, NR_WRITEBACK, -nr);
9277e8e78a0SChristoph Hellwig 		wb_stat_mod(&inode_to_bdi(folio->mapping->host)->wb,
9280c493b5cSTrond Myklebust 			    WB_WRITEBACK, -nr);
9290c493b5cSTrond Myklebust 	}
930e468bae9STrond Myklebust }
931d6d6dc7cSFred Isaman 
932b5bab9bfSTrond Myklebust /* Called holding the request lock on @req */
933b193a78dSTrond Myklebust static void nfs_clear_request_commit(struct nfs_commit_info *cinfo,
934b193a78dSTrond Myklebust 				     struct nfs_page *req)
935d6d6dc7cSFred Isaman {
9368dd37758STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
9379fcd5960STrond Myklebust 		struct nfs_open_context *ctx = nfs_req_openctx(req);
9389fcd5960STrond Myklebust 		struct inode *inode = d_inode(ctx->dentry);
939d6d6dc7cSFred Isaman 
940e824f99aSTrond Myklebust 		mutex_lock(&NFS_I(inode)->commit_mutex);
941b193a78dSTrond Myklebust 		if (!pnfs_clear_request_commit(req, cinfo)) {
942b193a78dSTrond Myklebust 			nfs_request_remove_commit_list(req, cinfo);
943d6d6dc7cSFred Isaman 		}
944e824f99aSTrond Myklebust 		mutex_unlock(&NFS_I(inode)->commit_mutex);
9450c493b5cSTrond Myklebust 		nfs_folio_clear_commit(nfs_page_to_folio(req));
9468dd37758STrond Myklebust 	}
947e468bae9STrond Myklebust }
948e468bae9STrond Myklebust 
949d45f60c6SWeston Andros Adamson int nfs_write_need_commit(struct nfs_pgio_header *hdr)
9508e821cadSTrond Myklebust {
951c65e6254SWeston Andros Adamson 	if (hdr->verf.committed == NFS_DATA_SYNC)
952d45f60c6SWeston Andros Adamson 		return hdr->lseg == NULL;
953c65e6254SWeston Andros Adamson 	return hdr->verf.committed != NFS_FILE_SYNC;
9548e821cadSTrond Myklebust }
9558e821cadSTrond Myklebust 
956919e3bd9STrond Myklebust static void nfs_async_write_init(struct nfs_pgio_header *hdr)
957919e3bd9STrond Myklebust {
958919e3bd9STrond Myklebust 	nfs_io_completion_get(hdr->io_completion);
959919e3bd9STrond Myklebust }
960919e3bd9STrond Myklebust 
961061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
9626c75dc0dSFred Isaman {
963ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
9646c75dc0dSFred Isaman 	unsigned long bytes = 0;
9656c75dc0dSFred Isaman 
9666c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
9676c75dc0dSFred Isaman 		goto out;
968ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
9696c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
9706c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
9716c75dc0dSFred Isaman 
9726c75dc0dSFred Isaman 		bytes += req->wb_bytes;
9736c75dc0dSFred Isaman 		nfs_list_remove_request(req);
9746c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
9756c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
976af887e43STrond Myklebust 			trace_nfs_comp_error(hdr->inode, req, hdr->error);
9770c493b5cSTrond Myklebust 			nfs_mapping_set_error(nfs_page_to_folio(req),
9780c493b5cSTrond Myklebust 					      hdr->error);
9796c75dc0dSFred Isaman 			goto remove_req;
9806c75dc0dSFred Isaman 		}
981c65e6254SWeston Andros Adamson 		if (nfs_write_need_commit(hdr)) {
98233344e0fSTrond Myklebust 			/* Reset wb_nio, since the write was successful. */
98333344e0fSTrond Myklebust 			req->wb_nio = 0;
984f79d06f5SAnna Schumaker 			memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
985b57ff130SWeston Andros Adamson 			nfs_mark_request_commit(req, hdr->lseg, &cinfo,
986a7d42ddbSWeston Andros Adamson 				hdr->pgio_mirror_idx);
9876c75dc0dSFred Isaman 			goto next;
9886c75dc0dSFred Isaman 		}
9896c75dc0dSFred Isaman remove_req:
9906c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
9916c75dc0dSFred Isaman next:
9920c493b5cSTrond Myklebust 		nfs_page_end_writeback(req);
9933aff4ebbSTrond Myklebust 		nfs_release_request(req);
9946c75dc0dSFred Isaman 	}
9956c75dc0dSFred Isaman out:
996919e3bd9STrond Myklebust 	nfs_io_completion_put(hdr->io_completion);
9976c75dc0dSFred Isaman 	hdr->release(hdr);
9986c75dc0dSFred Isaman }
9996c75dc0dSFred Isaman 
1000ce59515cSAnna Schumaker unsigned long
1001ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
1002fb8a1f11STrond Myklebust {
10035cb953d4STrond Myklebust 	return atomic_long_read(&cinfo->mds->ncommit);
1004fb8a1f11STrond Myklebust }
1005fb8a1f11STrond Myklebust 
1006e824f99aSTrond Myklebust /* NFS_I(cinfo->inode)->commit_mutex held by caller */
10071763da12SFred Isaman int
1008ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
1009ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
1010d6d6dc7cSFred Isaman {
1011137da553STrond Myklebust 	struct nfs_page *req, *tmp;
1012d6d6dc7cSFred Isaman 	int ret = 0;
1013d6d6dc7cSFred Isaman 
1014137da553STrond Myklebust 	list_for_each_entry_safe(req, tmp, src, wb_list) {
10157ad84aa9STrond Myklebust 		kref_get(&req->wb_kref);
10162ce209c4STrond Myklebust 		if (!nfs_lock_request(req)) {
1017137da553STrond Myklebust 			nfs_release_request(req);
1018137da553STrond Myklebust 			continue;
1019137da553STrond Myklebust 		}
1020ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
10215d2a9d9dSTrond Myklebust 		clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
10228dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
1023d6d6dc7cSFred Isaman 		ret++;
10241763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
1025d6d6dc7cSFred Isaman 			break;
1026e824f99aSTrond Myklebust 		cond_resched();
1027d6d6dc7cSFred Isaman 	}
1028d6d6dc7cSFred Isaman 	return ret;
1029d6d6dc7cSFred Isaman }
10305d2a9d9dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_scan_commit_list);
1031d6d6dc7cSFred Isaman 
10321da177e4SLinus Torvalds /*
10331da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
10341da177e4SLinus Torvalds  * @inode: NFS inode to scan
1035ea2cf228SFred Isaman  * @dst: mds destination list
1036ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
10371da177e4SLinus Torvalds  *
10381da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
10391da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
10401da177e4SLinus Torvalds  */
10411763da12SFred Isaman int
1042ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
1043ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
10441da177e4SLinus Torvalds {
1045d6d6dc7cSFred Isaman 	int ret = 0;
1046fb8a1f11STrond Myklebust 
10475cb953d4STrond Myklebust 	if (!atomic_long_read(&cinfo->mds->ncommit))
10485cb953d4STrond Myklebust 		return 0;
1049e824f99aSTrond Myklebust 	mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
10505cb953d4STrond Myklebust 	if (atomic_long_read(&cinfo->mds->ncommit) > 0) {
10518dd37758STrond Myklebust 		const int max = INT_MAX;
1052d6d6dc7cSFred Isaman 
1053ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
1054ea2cf228SFred Isaman 					   cinfo, max);
1055ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
1056d6d6dc7cSFred Isaman 	}
1057e824f99aSTrond Myklebust 	mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
1058ff778d02STrond Myklebust 	return ret;
10591da177e4SLinus Torvalds }
1060d6d6dc7cSFred Isaman 
10611da177e4SLinus Torvalds /*
1062e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
1063e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
10641da177e4SLinus Torvalds  *
1065e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
1066e7d39069STrond Myklebust  * to disk.
10671da177e4SLinus Torvalds  */
10680c493b5cSTrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct folio *folio,
1069e7d39069STrond Myklebust 						  unsigned int offset,
1070e7d39069STrond Myklebust 						  unsigned int bytes)
10711da177e4SLinus Torvalds {
1072e7d39069STrond Myklebust 	struct nfs_page *req;
1073e7d39069STrond Myklebust 	unsigned int rqend;
1074e7d39069STrond Myklebust 	unsigned int end;
10751da177e4SLinus Torvalds 	int error;
1076277459d2STrond Myklebust 
1077e7d39069STrond Myklebust 	end = offset + bytes;
1078e7d39069STrond Myklebust 
10790c493b5cSTrond Myklebust 	req = nfs_lock_and_join_requests(folio);
1080f6032f21STrond Myklebust 	if (IS_ERR_OR_NULL(req))
1081f6032f21STrond Myklebust 		return req;
10822bfc6e56SWeston Andros Adamson 
1083e7d39069STrond Myklebust 	rqend = req->wb_offset + req->wb_bytes;
1084e7d39069STrond Myklebust 	/*
1085e7d39069STrond Myklebust 	 * Tell the caller to flush out the request if
1086e7d39069STrond Myklebust 	 * the offsets are non-contiguous.
1087e7d39069STrond Myklebust 	 * Note: nfs_flush_incompatible() will already
1088e7d39069STrond Myklebust 	 * have flushed out requests having wrong owners.
1089e7d39069STrond Myklebust 	 */
1090f6032f21STrond Myklebust 	if (offset > rqend || end < req->wb_offset)
1091e7d39069STrond Myklebust 		goto out_flushme;
1092e7d39069STrond Myklebust 
10931da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
10941da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
10951da177e4SLinus Torvalds 		req->wb_offset = offset;
10961da177e4SLinus Torvalds 		req->wb_pgbase = offset;
10971da177e4SLinus Torvalds 	}
10981da177e4SLinus Torvalds 	if (end > rqend)
10991da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
1100e7d39069STrond Myklebust 	else
1101e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
110233344e0fSTrond Myklebust 	req->wb_nio = 0;
1103e7d39069STrond Myklebust 	return req;
1104e7d39069STrond Myklebust out_flushme:
1105f6032f21STrond Myklebust 	/*
1106f6032f21STrond Myklebust 	 * Note: we mark the request dirty here because
1107f6032f21STrond Myklebust 	 * nfs_lock_and_join_requests() cannot preserve
1108f6032f21STrond Myklebust 	 * commit flags, so we have to replay the write.
1109f6032f21STrond Myklebust 	 */
1110f6032f21STrond Myklebust 	nfs_mark_request_dirty(req);
1111f6032f21STrond Myklebust 	nfs_unlock_and_release_request(req);
11127e8e78a0SChristoph Hellwig 	error = nfs_wb_folio(folio->mapping->host, folio);
1113f6032f21STrond Myklebust 	return (error < 0) ? ERR_PTR(error) : NULL;
1114e7d39069STrond Myklebust }
11151da177e4SLinus Torvalds 
1116e7d39069STrond Myklebust /*
1117e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
1118e7d39069STrond Myklebust  *
1119e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
1120e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
1121e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
1122e7d39069STrond Myklebust  */
1123e7d39069STrond Myklebust static struct nfs_page *nfs_setup_write_request(struct nfs_open_context *ctx,
11240c493b5cSTrond Myklebust 						struct folio *folio,
11250c493b5cSTrond Myklebust 						unsigned int offset,
11260c493b5cSTrond Myklebust 						unsigned int bytes)
1127e7d39069STrond Myklebust {
1128e7d39069STrond Myklebust 	struct nfs_page *req;
1129e7d39069STrond Myklebust 
11300c493b5cSTrond Myklebust 	req = nfs_try_to_update_request(folio, offset, bytes);
1131e7d39069STrond Myklebust 	if (req != NULL)
1132e7d39069STrond Myklebust 		goto out;
11330c493b5cSTrond Myklebust 	req = nfs_page_create_from_folio(ctx, folio, offset, bytes);
1134e7d39069STrond Myklebust 	if (IS_ERR(req))
1135e7d39069STrond Myklebust 		goto out;
11360c493b5cSTrond Myklebust 	nfs_inode_add_request(req);
1137efc91ed0STrond Myklebust out:
113861e930a9STrond Myklebust 	return req;
11391da177e4SLinus Torvalds }
11401da177e4SLinus Torvalds 
11410c493b5cSTrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx,
11420c493b5cSTrond Myklebust 			       struct folio *folio, unsigned int offset,
11430c493b5cSTrond Myklebust 			       unsigned int count)
1144e7d39069STrond Myklebust {
1145e7d39069STrond Myklebust 	struct nfs_page *req;
1146e7d39069STrond Myklebust 
11470c493b5cSTrond Myklebust 	req = nfs_setup_write_request(ctx, folio, offset, count);
1148e7d39069STrond Myklebust 	if (IS_ERR(req))
1149e7d39069STrond Myklebust 		return PTR_ERR(req);
1150e7d39069STrond Myklebust 	/* Update file length */
11510c493b5cSTrond Myklebust 	nfs_grow_file(folio, offset, count);
1152d72ddcbaSWeston Andros Adamson 	nfs_mark_uptodate(req);
1153a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
11541d1afcbcSTrond Myklebust 	nfs_unlock_and_release_request(req);
1155e7d39069STrond Myklebust 	return 0;
1156e7d39069STrond Myklebust }
1157e7d39069STrond Myklebust 
11580c493b5cSTrond Myklebust int nfs_flush_incompatible(struct file *file, struct folio *folio)
11591da177e4SLinus Torvalds {
1160cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
11612a369153STrond Myklebust 	struct nfs_lock_context *l_ctx;
116217b985deSJeff Layton 	struct file_lock_context *flctx = locks_inode_context(file_inode(file));
11631da177e4SLinus Torvalds 	struct nfs_page	*req;
11641a54533eSTrond Myklebust 	int do_flush, status;
11651da177e4SLinus Torvalds 	/*
11661da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
11671da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
11681da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
11691da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
11701da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
11711da177e4SLinus Torvalds 	 * dropped page.
11721da177e4SLinus Torvalds 	 */
11731a54533eSTrond Myklebust 	do {
11740c493b5cSTrond Myklebust 		req = nfs_folio_find_head_request(folio);
11751a54533eSTrond Myklebust 		if (req == NULL)
11761a54533eSTrond Myklebust 			return 0;
11772a369153STrond Myklebust 		l_ctx = req->wb_lock_context;
11780c493b5cSTrond Myklebust 		do_flush = nfs_page_to_folio(req) != folio ||
11799fcd5960STrond Myklebust 			   !nfs_match_open_context(nfs_req_openctx(req), ctx);
1180bd61e0a9SJeff Layton 		if (l_ctx && flctx &&
1181bd61e0a9SJeff Layton 		    !(list_empty_careful(&flctx->flc_posix) &&
1182bd61e0a9SJeff Layton 		      list_empty_careful(&flctx->flc_flock))) {
1183d51fdb87SNeilBrown 			do_flush |= l_ctx->lockowner != current->files;
11845263e31eSJeff Layton 		}
11851da177e4SLinus Torvalds 		nfs_release_request(req);
11861a54533eSTrond Myklebust 		if (!do_flush)
11871a54533eSTrond Myklebust 			return 0;
11887e8e78a0SChristoph Hellwig 		status = nfs_wb_folio(folio->mapping->host, folio);
11891a54533eSTrond Myklebust 	} while (status == 0);
11901a54533eSTrond Myklebust 	return status;
11911da177e4SLinus Torvalds }
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds /*
1194dc24826bSAndy Adamson  * Avoid buffered writes when a open context credential's key would
1195dc24826bSAndy Adamson  * expire soon.
1196dc24826bSAndy Adamson  *
1197dc24826bSAndy Adamson  * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
1198dc24826bSAndy Adamson  *
1199dc24826bSAndy Adamson  * Return 0 and set a credential flag which triggers the inode to flush
1200dc24826bSAndy Adamson  * and performs  NFS_FILE_SYNC writes if the key will expired within
1201dc24826bSAndy Adamson  * RPC_KEY_EXPIRE_TIMEO.
1202dc24826bSAndy Adamson  */
1203dc24826bSAndy Adamson int
1204dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode)
1205dc24826bSAndy Adamson {
1206dc24826bSAndy Adamson 	struct nfs_open_context *ctx = nfs_file_open_context(filp);
1207dc24826bSAndy Adamson 
1208ddf529eeSNeilBrown 	if (nfs_ctx_key_to_expire(ctx, inode) &&
1209ca05cbaeSTrond Myklebust 	    !rcu_access_pointer(ctx->ll_cred))
1210ddf529eeSNeilBrown 		/* Already expired! */
1211ddf529eeSNeilBrown 		return -EACCES;
1212ddf529eeSNeilBrown 	return 0;
1213dc24826bSAndy Adamson }
1214dc24826bSAndy Adamson 
1215dc24826bSAndy Adamson /*
1216dc24826bSAndy Adamson  * Test if the open context credential key is marked to expire soon.
1217dc24826bSAndy Adamson  */
1218ce52914eSScott Mayhew bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode)
1219dc24826bSAndy Adamson {
1220ce52914eSScott Mayhew 	struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
1221ca05cbaeSTrond Myklebust 	struct rpc_cred *cred, *new, *old = NULL;
1222ddf529eeSNeilBrown 	struct auth_cred acred = {
1223a52458b4SNeilBrown 		.cred = ctx->cred,
1224ddf529eeSNeilBrown 	};
1225ca05cbaeSTrond Myklebust 	bool ret = false;
1226ce52914eSScott Mayhew 
1227ca05cbaeSTrond Myklebust 	rcu_read_lock();
1228ca05cbaeSTrond Myklebust 	cred = rcu_dereference(ctx->ll_cred);
1229ca05cbaeSTrond Myklebust 	if (cred && !(cred->cr_ops->crkey_timeout &&
1230ca05cbaeSTrond Myklebust 		      cred->cr_ops->crkey_timeout(cred)))
1231ca05cbaeSTrond Myklebust 		goto out;
1232ca05cbaeSTrond Myklebust 	rcu_read_unlock();
1233ca05cbaeSTrond Myklebust 
1234ca05cbaeSTrond Myklebust 	new = auth->au_ops->lookup_cred(auth, &acred, 0);
1235ca05cbaeSTrond Myklebust 	if (new == cred) {
1236ca05cbaeSTrond Myklebust 		put_rpccred(new);
1237ddf529eeSNeilBrown 		return true;
1238ca05cbaeSTrond Myklebust 	}
1239ca05cbaeSTrond Myklebust 	if (IS_ERR_OR_NULL(new)) {
1240ca05cbaeSTrond Myklebust 		new = NULL;
1241ca05cbaeSTrond Myklebust 		ret = true;
1242ca05cbaeSTrond Myklebust 	} else if (new->cr_ops->crkey_timeout &&
1243ca05cbaeSTrond Myklebust 		   new->cr_ops->crkey_timeout(new))
1244ca05cbaeSTrond Myklebust 		ret = true;
1245ca05cbaeSTrond Myklebust 
1246ca05cbaeSTrond Myklebust 	rcu_read_lock();
1247ca05cbaeSTrond Myklebust 	old = rcu_dereference_protected(xchg(&ctx->ll_cred,
1248ca05cbaeSTrond Myklebust 					     RCU_INITIALIZER(new)), 1);
1249ca05cbaeSTrond Myklebust out:
1250ca05cbaeSTrond Myklebust 	rcu_read_unlock();
1251ca05cbaeSTrond Myklebust 	put_rpccred(old);
1252ca05cbaeSTrond Myklebust 	return ret;
1253dc24826bSAndy Adamson }
1254dc24826bSAndy Adamson 
1255dc24826bSAndy Adamson /*
12565d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
12575d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
12585d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
12595d47a356STrond Myklebust  */
12600c493b5cSTrond Myklebust static bool nfs_folio_write_uptodate(struct folio *folio, unsigned int pagelen)
12615d47a356STrond Myklebust {
12627e8e78a0SChristoph Hellwig 	struct inode *inode = folio->mapping->host;
1263d529ef83SJeff Layton 	struct nfs_inode *nfsi = NFS_I(inode);
1264d529ef83SJeff Layton 
12658d197a56STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
12668d197a56STrond Myklebust 		goto out;
1267fc9dc401STrond Myklebust 	if (nfsi->cache_validity &
126813c0b082STrond Myklebust 	    (NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE))
1269d529ef83SJeff Layton 		return false;
12704db72b40SJeff Layton 	smp_rmb();
1271fc9dc401STrond Myklebust 	if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags) && pagelen != 0)
12728d197a56STrond Myklebust 		return false;
12738d197a56STrond Myklebust out:
1274fc9dc401STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_INVALID_DATA && pagelen != 0)
127518dd78c4SScott Mayhew 		return false;
12760c493b5cSTrond Myklebust 	return folio_test_uptodate(folio) != 0;
12775d47a356STrond Myklebust }
12785d47a356STrond Myklebust 
12795263e31eSJeff Layton static bool
12805263e31eSJeff Layton is_whole_file_wrlock(struct file_lock *fl)
12815263e31eSJeff Layton {
12825263e31eSJeff Layton 	return fl->fl_start == 0 && fl->fl_end == OFFSET_MAX &&
1283d7c9616bSJeff Layton 			lock_is_write(fl);
12845263e31eSJeff Layton }
12855263e31eSJeff Layton 
1286c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or
1287c7559663SScott Mayhew  * if we have the whole file locked for writing), it may be more efficient to
1288c7559663SScott Mayhew  * extend the write to cover the entire page in order to avoid fragmentation
1289c7559663SScott Mayhew  * inefficiencies.
1290c7559663SScott Mayhew  *
1291263b4509SScott Mayhew  * If the file is opened for synchronous writes then we can just skip the rest
1292263b4509SScott Mayhew  * of the checks.
1293c7559663SScott Mayhew  */
12940c493b5cSTrond Myklebust static int nfs_can_extend_write(struct file *file, struct folio *folio,
12950c493b5cSTrond Myklebust 				unsigned int pagelen)
1296c7559663SScott Mayhew {
12970c493b5cSTrond Myklebust 	struct inode *inode = file_inode(file);
129817b985deSJeff Layton 	struct file_lock_context *flctx = locks_inode_context(inode);
12995263e31eSJeff Layton 	struct file_lock *fl;
13000c493b5cSTrond Myklebust 	int ret;
13015263e31eSJeff Layton 
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,
13480c493b5cSTrond Myklebust 		(long long)(folio_file_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)) {
1354fc9dc401STrond Myklebust 		count = max(count + offset, pagelen);
13551da177e4SLinus Torvalds 		offset = 0;
13561da177e4SLinus Torvalds 	}
13571da177e4SLinus Torvalds 
13580c493b5cSTrond Myklebust 	status = nfs_writepage_setup(ctx, folio, offset, count);
135903fa9e84STrond Myklebust 	if (status < 0)
1360d2ceb7e5SBenjamin Coddington 		nfs_set_pageerror(mapping);
1361149a4fddSBenjamin Coddington out:
13620c493b5cSTrond Myklebust 	dprintk("NFS:       nfs_update_folio returns %d (isize %lld)\n",
13631da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
13641da177e4SLinus Torvalds 	return status;
13651da177e4SLinus Torvalds }
13661da177e4SLinus Torvalds 
13673ff7576dSTrond Myklebust static int flush_task_priority(int how)
13681da177e4SLinus Torvalds {
13691da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
13701da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
13711da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
13721da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
13731da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
13741da177e4SLinus Torvalds 	}
13751da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
13761da177e4SLinus Torvalds }
13771da177e4SLinus Torvalds 
1378d45f60c6SWeston Andros Adamson static void nfs_initiate_write(struct nfs_pgio_header *hdr,
1379d45f60c6SWeston Andros Adamson 			       struct rpc_message *msg,
1380abde71f4STom Haynes 			       const struct nfs_rpc_ops *rpc_ops,
13811ed26f33SAnna Schumaker 			       struct rpc_task_setup *task_setup_data, int how)
13821da177e4SLinus Torvalds {
13833ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
13841da177e4SLinus Torvalds 
13858db55a03SNeilBrown 	if (IS_SWAPFILE(hdr->inode))
13868db55a03SNeilBrown 		task_setup_data->flags |= RPC_TASK_SWAPPER;
13871ed26f33SAnna Schumaker 	task_setup_data->priority = priority;
1388fb91fb0eSAnna Schumaker 	rpc_ops->write_setup(hdr, msg, &task_setup_data->rpc_client);
13895bb2a7cbSTrond Myklebust 	trace_nfs_initiate_write(hdr);
1390275acaafSTrond Myklebust }
1391275acaafSTrond Myklebust 
13926d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
13936d884e8fSFred  * call this on each, which will prepare them to be retried on next
13946d884e8fSFred  * writeback using standard nfs.
13956d884e8fSFred  */
13966d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
13976d884e8fSFred {
13986dd85e83STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(nfs_page_to_inode(req));
139967f4b5dcSTrond Myklebust 
140033344e0fSTrond Myklebust 	/* Bump the transmission count */
140133344e0fSTrond Myklebust 	req->wb_nio++;
14026d884e8fSFred 	nfs_mark_request_dirty(req);
140367f4b5dcSTrond Myklebust 	atomic_long_inc(&nfsi->redirtied_pages);
14040c493b5cSTrond Myklebust 	nfs_page_end_writeback(req);
14053aff4ebbSTrond Myklebust 	nfs_release_request(req);
14066d884e8fSFred }
14076d884e8fSFred 
1408df3accb8STrond Myklebust static void nfs_async_write_error(struct list_head *head, int error)
14096c75dc0dSFred Isaman {
14106c75dc0dSFred Isaman 	struct nfs_page	*req;
14116c75dc0dSFred Isaman 
14126c75dc0dSFred Isaman 	while (!list_empty(head)) {
14136c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
14146c75dc0dSFred Isaman 		nfs_list_remove_request(req);
1415cea9ba72STrond Myklebust 		if (nfs_error_is_fatal_on_server(error))
14166fbda89bSTrond Myklebust 			nfs_write_error(req, error);
14176fbda89bSTrond Myklebust 		else
14186c75dc0dSFred Isaman 			nfs_redirty_request(req);
14196c75dc0dSFred Isaman 	}
14206c75dc0dSFred Isaman }
14216c75dc0dSFred Isaman 
1422dc602dd7STrond Myklebust static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr)
1423dc602dd7STrond Myklebust {
1424df3accb8STrond Myklebust 	nfs_async_write_error(&hdr->pages, 0);
1425dc602dd7STrond Myklebust }
1426dc602dd7STrond Myklebust 
1427061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1428919e3bd9STrond Myklebust 	.init_hdr = nfs_async_write_init,
1429061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1430061ae2edSFred Isaman 	.completion = nfs_write_completion,
1431dc602dd7STrond Myklebust 	.reschedule_io = nfs_async_write_reschedule_io,
1432061ae2edSFred Isaman };
1433061ae2edSFred Isaman 
143457208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1435a20c93e3SChristoph Hellwig 			       struct inode *inode, int ioflags, bool force_mds,
1436061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
14371751c363STrond Myklebust {
1438a20c93e3SChristoph Hellwig 	struct nfs_server *server = NFS_SERVER(inode);
143941d8d5b7SAnna Schumaker 	const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
1440a20c93e3SChristoph Hellwig 
1441a20c93e3SChristoph Hellwig #ifdef CONFIG_NFS_V4_1
1442a20c93e3SChristoph Hellwig 	if (server->pnfs_curr_ld && !force_mds)
1443a20c93e3SChristoph Hellwig 		pg_ops = server->pnfs_curr_ld->pg_write_ops;
1444a20c93e3SChristoph Hellwig #endif
14454a0de55cSAnna Schumaker 	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
14463bde7afdSTrond Myklebust 			server->wsize, ioflags);
14471751c363STrond Myklebust }
1448ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
14491751c363STrond Myklebust 
1450dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1451dce81290STrond Myklebust {
1452a7d42ddbSWeston Andros Adamson 	struct nfs_pgio_mirror *mirror;
1453a7d42ddbSWeston Andros Adamson 
14546f29b9bbSKinglong Mee 	if (pgio->pg_ops && pgio->pg_ops->pg_cleanup)
14556f29b9bbSKinglong Mee 		pgio->pg_ops->pg_cleanup(pgio);
14566f29b9bbSKinglong Mee 
145741d8d5b7SAnna Schumaker 	pgio->pg_ops = &nfs_pgio_rw_ops;
1458a7d42ddbSWeston Andros Adamson 
1459a7d42ddbSWeston Andros Adamson 	nfs_pageio_stop_mirroring(pgio);
1460a7d42ddbSWeston Andros Adamson 
1461a7d42ddbSWeston Andros Adamson 	mirror = &pgio->pg_mirrors[0];
1462a7d42ddbSWeston Andros Adamson 	mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1463dce81290STrond Myklebust }
14641f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1465dce81290STrond Myklebust 
14661da177e4SLinus Torvalds 
14670b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
14680b7c0153SFred Isaman {
14690b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
14700b7c0153SFred Isaman 
14710b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
14720b7c0153SFred Isaman }
14730b7c0153SFred Isaman 
1474a08a8cd3STrond Myklebust static void nfs_writeback_check_extend(struct nfs_pgio_header *hdr,
1475a08a8cd3STrond Myklebust 		struct nfs_fattr *fattr)
1476a08a8cd3STrond Myklebust {
1477a08a8cd3STrond Myklebust 	struct nfs_pgio_args *argp = &hdr->args;
1478a08a8cd3STrond Myklebust 	struct nfs_pgio_res *resp = &hdr->res;
14792b83d3deSTrond Myklebust 	u64 size = argp->offset + resp->count;
1480a08a8cd3STrond Myklebust 
1481a08a8cd3STrond Myklebust 	if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
14822b83d3deSTrond Myklebust 		fattr->size = size;
14832b83d3deSTrond Myklebust 	if (nfs_size_to_loff_t(fattr->size) < i_size_read(hdr->inode)) {
14842b83d3deSTrond Myklebust 		fattr->valid &= ~NFS_ATTR_FATTR_SIZE;
1485a08a8cd3STrond Myklebust 		return;
14862b83d3deSTrond Myklebust 	}
14872b83d3deSTrond Myklebust 	if (size != fattr->size)
1488a08a8cd3STrond Myklebust 		return;
1489a08a8cd3STrond Myklebust 	/* Set attribute barrier */
1490a08a8cd3STrond Myklebust 	nfs_fattr_set_barrier(fattr);
14912b83d3deSTrond Myklebust 	/* ...and update size */
14922b83d3deSTrond Myklebust 	fattr->valid |= NFS_ATTR_FATTR_SIZE;
1493a08a8cd3STrond Myklebust }
1494a08a8cd3STrond Myklebust 
1495a08a8cd3STrond Myklebust void nfs_writeback_update_inode(struct nfs_pgio_header *hdr)
1496a08a8cd3STrond Myklebust {
14972b83d3deSTrond Myklebust 	struct nfs_fattr *fattr = &hdr->fattr;
1498a08a8cd3STrond Myklebust 	struct inode *inode = hdr->inode;
1499a08a8cd3STrond Myklebust 
1500e12912d9STrond Myklebust 	if (nfs_have_delegated_mtime(inode)) {
1501e12912d9STrond Myklebust 		spin_lock(&inode->i_lock);
1502e12912d9STrond Myklebust 		nfs_set_cache_invalid(inode, NFS_INO_INVALID_BLOCKS);
1503e12912d9STrond Myklebust 		spin_unlock(&inode->i_lock);
1504e12912d9STrond Myklebust 		return;
1505e12912d9STrond Myklebust 	}
1506e12912d9STrond Myklebust 
1507a08a8cd3STrond Myklebust 	spin_lock(&inode->i_lock);
1508a08a8cd3STrond Myklebust 	nfs_writeback_check_extend(hdr, fattr);
1509a08a8cd3STrond Myklebust 	nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
1510a08a8cd3STrond Myklebust 	spin_unlock(&inode->i_lock);
1511a08a8cd3STrond Myklebust }
1512a08a8cd3STrond Myklebust EXPORT_SYMBOL_GPL(nfs_writeback_update_inode);
1513a08a8cd3STrond Myklebust 
15141da177e4SLinus Torvalds /*
15151da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
15161da177e4SLinus Torvalds  */
1517d45f60c6SWeston Andros Adamson static int nfs_writeback_done(struct rpc_task *task,
1518d45f60c6SWeston Andros Adamson 			      struct nfs_pgio_header *hdr,
15190eecb214SAnna Schumaker 			      struct inode *inode)
15201da177e4SLinus Torvalds {
1521788e7a89STrond Myklebust 	int status;
15221da177e4SLinus Torvalds 
1523f551e44fSChuck Lever 	/*
1524f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1525f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1526f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1527f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1528f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1529f551e44fSChuck Lever 	 */
1530d45f60c6SWeston Andros Adamson 	status = NFS_PROTO(inode)->write_done(task, hdr);
1531788e7a89STrond Myklebust 	if (status != 0)
15320eecb214SAnna Schumaker 		return status;
15338224b273SChuck Lever 
1534d45f60c6SWeston Andros Adamson 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count);
15355bb2a7cbSTrond Myklebust 	trace_nfs_writeback_done(task, hdr);
153691d5b470SChuck Lever 
153769d96651SJeff Layton 	if (task->tk_status >= 0) {
153869d96651SJeff Layton 		enum nfs3_stable_how committed = hdr->res.verf->committed;
153969d96651SJeff Layton 
154069d96651SJeff Layton 		if (committed == NFS_UNSTABLE) {
154169d96651SJeff Layton 			/*
154269d96651SJeff Layton 			 * We have some uncommitted data on the server at
154369d96651SJeff Layton 			 * this point, so ensure that we keep track of that
154469d96651SJeff Layton 			 * fact irrespective of what later writes do.
154569d96651SJeff Layton 			 */
154669d96651SJeff Layton 			set_bit(NFS_IOHDR_UNSTABLE_WRITES, &hdr->flags);
154769d96651SJeff Layton 		}
154869d96651SJeff Layton 
154969d96651SJeff Layton 		if (committed < hdr->args.stable) {
15501da177e4SLinus Torvalds 			/* We tried a write call, but the server did not
15511da177e4SLinus Torvalds 			 * commit data to stable storage even though we
15521da177e4SLinus Torvalds 			 * requested it.
15531da177e4SLinus Torvalds 			 * Note: There is a known bug in Tru64 < 5.0 in which
15541da177e4SLinus Torvalds 			 *	 the server reports NFS_DATA_SYNC, but performs
15551da177e4SLinus Torvalds 			 *	 NFS_FILE_SYNC. We therefore implement this checking
15561da177e4SLinus Torvalds 			 *	 as a dprintk() in order to avoid filling syslog.
15571da177e4SLinus Torvalds 			 */
15581da177e4SLinus Torvalds 			static unsigned long    complain;
15591da177e4SLinus Torvalds 
1560a69aef14SFred Isaman 			/* Note this will print the MDS for a DS write */
15611da177e4SLinus Torvalds 			if (time_before(complain, jiffies)) {
15621da177e4SLinus Torvalds 				dprintk("NFS:       faulty NFS server %s:"
15631da177e4SLinus Torvalds 					" (committed = %d) != (stable = %d)\n",
1564cd841605SFred Isaman 					NFS_SERVER(inode)->nfs_client->cl_hostname,
156569d96651SJeff Layton 					committed, hdr->args.stable);
15661da177e4SLinus Torvalds 				complain = jiffies + 300 * HZ;
15671da177e4SLinus Torvalds 			}
15681da177e4SLinus Torvalds 		}
156969d96651SJeff Layton 	}
15701f2edbe3STrond Myklebust 
15711f2edbe3STrond Myklebust 	/* Deal with the suid/sgid bit corner case */
157216e14375STrond Myklebust 	if (nfs_should_remove_suid(inode)) {
157316e14375STrond Myklebust 		spin_lock(&inode->i_lock);
1574720869ebSTrond Myklebust 		nfs_set_cache_invalid(inode, NFS_INO_INVALID_MODE);
157516e14375STrond Myklebust 		spin_unlock(&inode->i_lock);
157616e14375STrond Myklebust 	}
15770eecb214SAnna Schumaker 	return 0;
15780eecb214SAnna Schumaker }
15790eecb214SAnna Schumaker 
15800eecb214SAnna Schumaker /*
15810eecb214SAnna Schumaker  * This function is called when the WRITE call is complete.
15820eecb214SAnna Schumaker  */
1583d45f60c6SWeston Andros Adamson static void nfs_writeback_result(struct rpc_task *task,
1584d45f60c6SWeston Andros Adamson 				 struct nfs_pgio_header *hdr)
15850eecb214SAnna Schumaker {
1586d45f60c6SWeston Andros Adamson 	struct nfs_pgio_args	*argp = &hdr->args;
1587d45f60c6SWeston Andros Adamson 	struct nfs_pgio_res	*resp = &hdr->res;
15881f2edbe3STrond Myklebust 
15891f2edbe3STrond Myklebust 	if (resp->count < argp->count) {
15901da177e4SLinus Torvalds 		static unsigned long    complain;
15911da177e4SLinus Torvalds 
15926c75dc0dSFred Isaman 		/* This a short write! */
1593d45f60c6SWeston Andros Adamson 		nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE);
159491d5b470SChuck Lever 
15951da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
15966c75dc0dSFred Isaman 		if (resp->count == 0) {
15976c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
15986c75dc0dSFred Isaman 				printk(KERN_WARNING
15996c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
16006c75dc0dSFred Isaman 				       argp->count);
16016c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
16026c75dc0dSFred Isaman 			}
1603d45f60c6SWeston Andros Adamson 			nfs_set_pgio_error(hdr, -EIO, argp->offset);
16046c75dc0dSFred Isaman 			task->tk_status = -EIO;
16056c75dc0dSFred Isaman 			return;
16066c75dc0dSFred Isaman 		}
1607f8417b48SKinglong Mee 
1608f8417b48SKinglong Mee 		/* For non rpc-based layout drivers, retry-through-MDS */
1609f8417b48SKinglong Mee 		if (!task->tk_ops) {
1610f8417b48SKinglong Mee 			hdr->pnfs_error = -EAGAIN;
1611f8417b48SKinglong Mee 			return;
1612f8417b48SKinglong Mee 		}
1613f8417b48SKinglong Mee 
16141da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
16151da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
16161da177e4SLinus Torvalds 			/* Resend from where the server left off */
1617d45f60c6SWeston Andros Adamson 			hdr->mds_offset += resp->count;
16181da177e4SLinus Torvalds 			argp->offset += resp->count;
16191da177e4SLinus Torvalds 			argp->pgbase += resp->count;
16201da177e4SLinus Torvalds 			argp->count -= resp->count;
16211da177e4SLinus Torvalds 		} else {
16221da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
16231da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
16241da177e4SLinus Torvalds 			 */
16251da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
16261da177e4SLinus Torvalds 		}
16278c9cb714STrond Myklebust 		resp->count = 0;
16288c9cb714STrond Myklebust 		resp->verf->committed = 0;
1629d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
16301da177e4SLinus Torvalds 	}
16311da177e4SLinus Torvalds }
16321da177e4SLinus Torvalds 
1633af7cf057STrond Myklebust static int wait_on_commit(struct nfs_mds_commit_info *cinfo)
163471d0a611STrond Myklebust {
1635723c921eSPeter Zijlstra 	return wait_var_event_killable(&cinfo->rpcs_out,
1636723c921eSPeter Zijlstra 				       !atomic_read(&cinfo->rpcs_out));
1637af7cf057STrond Myklebust }
1638af7cf057STrond Myklebust 
163917f46b80SJosef Bacik void nfs_commit_begin(struct nfs_mds_commit_info *cinfo)
1640af7cf057STrond Myklebust {
1641af7cf057STrond Myklebust 	atomic_inc(&cinfo->rpcs_out);
1642af7cf057STrond Myklebust }
1643af7cf057STrond Myklebust 
1644133a48abSTrond Myklebust bool nfs_commit_end(struct nfs_mds_commit_info *cinfo)
1645af7cf057STrond Myklebust {
1646133a48abSTrond Myklebust 	if (atomic_dec_and_test(&cinfo->rpcs_out)) {
1647723c921eSPeter Zijlstra 		wake_up_var(&cinfo->rpcs_out);
1648133a48abSTrond Myklebust 		return true;
1649133a48abSTrond Myklebust 	}
1650133a48abSTrond Myklebust 	return false;
165171d0a611STrond Myklebust }
165271d0a611STrond Myklebust 
16530b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
16541da177e4SLinus Torvalds {
16550b7c0153SFred Isaman 	put_nfs_open_context(data->context);
16560b7c0153SFred Isaman 	nfs_commit_free(data);
16571da177e4SLinus Torvalds }
1658e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
16591da177e4SLinus Torvalds 
16600b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
1661c36aae9aSPeng Tao 			const struct nfs_rpc_ops *nfs_ops,
16629ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
16639f0ec176SAndy Adamson 			int how, int flags)
16641da177e4SLinus Torvalds {
166507737691STrond Myklebust 	struct rpc_task *task;
16669ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1667bdc7f021STrond Myklebust 	struct rpc_message msg = {
1668bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1669bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
16709ace33cdSFred Isaman 		.rpc_cred = data->cred,
1671bdc7f021STrond Myklebust 	};
167284115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
167307737691STrond Myklebust 		.task = &data->task,
16749ace33cdSFred Isaman 		.rpc_client = clnt,
1675bdc7f021STrond Myklebust 		.rpc_message = &msg,
16769ace33cdSFred Isaman 		.callback_ops = call_ops,
167784115e1cSTrond Myklebust 		.callback_data = data,
1678101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
16794fa7ef69STrond Myklebust 		.flags = RPC_TASK_ASYNC | flags,
16803ff7576dSTrond Myklebust 		.priority = priority,
168184115e1cSTrond Myklebust 	};
1682118f09edSOlga Kornievskaia 
1683118f09edSOlga Kornievskaia 	if (nfs_server_capable(data->inode, NFS_CAP_MOVEABLE))
1684118f09edSOlga Kornievskaia 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
1685118f09edSOlga Kornievskaia 
1686788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
1687e9ae1ee2SAnna Schumaker 	nfs_ops->commit_setup(data, &msg, &task_setup_data.rpc_client);
16888224b273SChuck Lever 	trace_nfs_initiate_commit(data);
16891da177e4SLinus Torvalds 
1690b4839ebeSKinglong Mee 	dprintk("NFS: initiated commit call\n");
1691bdc7f021STrond Myklebust 
169207737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1693dbae4c73STrond Myklebust 	if (IS_ERR(task))
1694dbae4c73STrond Myklebust 		return PTR_ERR(task);
1695d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1696d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
169707737691STrond Myklebust 	rpc_put_task(task);
1698dbae4c73STrond Myklebust 	return 0;
16991da177e4SLinus Torvalds }
1700e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
17011da177e4SLinus Torvalds 
1702378520b8SPeng Tao static loff_t nfs_get_lwb(struct list_head *head)
1703378520b8SPeng Tao {
1704378520b8SPeng Tao 	loff_t lwb = 0;
1705378520b8SPeng Tao 	struct nfs_page *req;
1706378520b8SPeng Tao 
1707378520b8SPeng Tao 	list_for_each_entry(req, head, wb_list)
1708378520b8SPeng Tao 		if (lwb < (req_offset(req) + req->wb_bytes))
1709378520b8SPeng Tao 			lwb = req_offset(req) + req->wb_bytes;
1710378520b8SPeng Tao 
1711378520b8SPeng Tao 	return lwb;
1712378520b8SPeng Tao }
1713378520b8SPeng Tao 
17141da177e4SLinus Torvalds /*
17159ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
17169ace33cdSFred Isaman  */
17170b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1718988b6dceSFred Isaman 		     struct list_head *head,
1719f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1720f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
17219ace33cdSFred Isaman {
172219573c93STrond Myklebust 	struct nfs_page *first;
172319573c93STrond Myklebust 	struct nfs_open_context *ctx;
172419573c93STrond Myklebust 	struct inode *inode;
17259ace33cdSFred Isaman 
17269ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
17279ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
17289ace33cdSFred Isaman 
172919573c93STrond Myklebust 	if (head)
17309ace33cdSFred Isaman 		list_splice_init(head, &data->pages);
17319ace33cdSFred Isaman 
173219573c93STrond Myklebust 	first = nfs_list_entry(data->pages.next);
173319573c93STrond Myklebust 	ctx = nfs_req_openctx(first);
173419573c93STrond Myklebust 	inode = d_inode(ctx->dentry);
173519573c93STrond Myklebust 
17369ace33cdSFred Isaman 	data->inode	  = inode;
17379fcd5960STrond Myklebust 	data->cred	  = ctx->cred;
1738988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
1739378520b8SPeng Tao 	/* only set lwb for pnfs commit */
1740378520b8SPeng Tao 	if (lseg)
1741378520b8SPeng Tao 		data->lwb = nfs_get_lwb(&data->pages);
17429ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1743f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1744b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
17459ace33cdSFred Isaman 
17469ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
17479ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
17489ace33cdSFred Isaman 	data->args.offset = 0;
17499ace33cdSFred Isaman 	data->args.count  = 0;
17509fcd5960STrond Myklebust 	data->context     = get_nfs_open_context(ctx);
17519ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
17529ace33cdSFred Isaman 	data->res.verf    = &data->verf;
17539ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
1754133a48abSTrond Myklebust 	nfs_commit_begin(cinfo->mds);
17559ace33cdSFred Isaman }
1756e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
17579ace33cdSFred Isaman 
1758e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1759ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1760b57ff130SWeston Andros Adamson 		      struct nfs_commit_info *cinfo,
1761b57ff130SWeston Andros Adamson 		      u32 ds_commit_idx)
176264bfeb49SFred Isaman {
176364bfeb49SFred Isaman 	struct nfs_page *req;
176464bfeb49SFred Isaman 
176564bfeb49SFred Isaman 	while (!list_empty(page_list)) {
176664bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
176764bfeb49SFred Isaman 		nfs_list_remove_request(req);
1768b57ff130SWeston Andros Adamson 		nfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx);
17690c493b5cSTrond Myklebust 		nfs_folio_clear_commit(nfs_page_to_folio(req));
17701d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
177164bfeb49SFred Isaman 	}
177264bfeb49SFred Isaman }
1773e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
177464bfeb49SFred Isaman 
17750c493b5cSTrond Myklebust static void nfs_commit_resched_write(struct nfs_commit_info *cinfo,
1776b20135d0STrond Myklebust 				     struct nfs_page *req)
1777b20135d0STrond Myklebust {
17780c493b5cSTrond Myklebust 	struct folio *folio = nfs_page_to_folio(req);
17790c493b5cSTrond Myklebust 
17800c493b5cSTrond Myklebust 	filemap_dirty_folio(folio_mapping(folio), folio);
1781b20135d0STrond Myklebust }
1782b20135d0STrond Myklebust 
17839ace33cdSFred Isaman /*
17841da177e4SLinus Torvalds  * Commit dirty pages
17851da177e4SLinus Torvalds  */
17861da177e4SLinus Torvalds static int
1787ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1788ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
17891da177e4SLinus Torvalds {
17900b7c0153SFred Isaman 	struct nfs_commit_data	*data;
179185e39feeSOlga Kornievskaia 	unsigned short task_flags = 0;
17921da177e4SLinus Torvalds 
1793ade8febdSWeston Andros Adamson 	/* another commit raced with us */
1794ade8febdSWeston Andros Adamson 	if (list_empty(head))
1795ade8febdSWeston Andros Adamson 		return 0;
1796ade8febdSWeston Andros Adamson 
1797515dcdcdSTrond Myklebust 	data = nfs_commitdata_alloc();
1798515dcdcdSTrond Myklebust 	if (!data) {
1799515dcdcdSTrond Myklebust 		nfs_retry_commit(head, NULL, cinfo, -1);
1800515dcdcdSTrond Myklebust 		return -ENOMEM;
1801515dcdcdSTrond Myklebust 	}
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds 	/* Set up the argument struct */
1804f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
180585e39feeSOlga Kornievskaia 	if (NFS_SERVER(inode)->nfs_client->cl_minorversion)
180685e39feeSOlga Kornievskaia 		task_flags = RPC_TASK_MOVEABLE;
1807c36aae9aSPeng Tao 	return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode),
180885e39feeSOlga Kornievskaia 				   data->mds_ops, how,
180985e39feeSOlga Kornievskaia 				   RPC_TASK_CRED_NOREF | task_flags);
18101da177e4SLinus Torvalds }
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds /*
18131da177e4SLinus Torvalds  * COMMIT call returned
18141da177e4SLinus Torvalds  */
1815788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
18161da177e4SLinus Torvalds {
18170b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
18181da177e4SLinus Torvalds 
1819788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1820c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
18217bdd297eSTrond Myklebust 	trace_nfs_commit_done(task, data);
1822c9d8f89dSTrond Myklebust }
1823c9d8f89dSTrond Myklebust 
1824f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1825c9d8f89dSTrond Myklebust {
1826221203ceSTrond Myklebust 	const struct nfs_writeverf *verf = data->res.verf;
1827c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1828c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1829f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
18300c493b5cSTrond Myklebust 	struct folio *folio;
1831788e7a89STrond Myklebust 
18321da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
18331da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
18341da177e4SLinus Torvalds 		nfs_list_remove_request(req);
18350c493b5cSTrond Myklebust 		folio = nfs_page_to_folio(req);
18360c493b5cSTrond Myklebust 		nfs_folio_clear_commit(folio);
18371da177e4SLinus Torvalds 
18381e8968c5SNiels de Vos 		dprintk("NFS:       commit (%s/%llu %d@%lld)",
18399fcd5960STrond Myklebust 			nfs_req_openctx(req)->dentry->d_sb->s_id,
18409fcd5960STrond Myklebust 			(unsigned long long)NFS_FILEID(d_inode(nfs_req_openctx(req)->dentry)),
18411da177e4SLinus Torvalds 			req->wb_bytes,
18421da177e4SLinus Torvalds 			(long long)req_offset(req));
1843c9d8f89dSTrond Myklebust 		if (status < 0) {
18440c493b5cSTrond Myklebust 			if (folio) {
1845af887e43STrond Myklebust 				trace_nfs_commit_error(data->inode, req,
1846af887e43STrond Myklebust 						       status);
18470c493b5cSTrond Myklebust 				nfs_mapping_set_error(folio, status);
18481da177e4SLinus Torvalds 				nfs_inode_remove_request(req);
18496fbda89bSTrond Myklebust 			}
1850ddeaa637SJoe Perches 			dprintk_cont(", error = %d\n", status);
18511da177e4SLinus Torvalds 			goto next;
18521da177e4SLinus Torvalds 		}
18531da177e4SLinus Torvalds 
18541da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
18551da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
18561f28476dSTrond Myklebust 		if (nfs_write_match_verf(verf, req)) {
18571da177e4SLinus Torvalds 			/* We have a match */
18580c493b5cSTrond Myklebust 			if (folio)
18591da177e4SLinus Torvalds 				nfs_inode_remove_request(req);
1860ddeaa637SJoe Perches 			dprintk_cont(" OK\n");
18611da177e4SLinus Torvalds 			goto next;
18621da177e4SLinus Torvalds 		}
18631da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
1864ddeaa637SJoe Perches 		dprintk_cont(" mismatch\n");
18656d884e8fSFred 		nfs_mark_request_dirty(req);
186667f4b5dcSTrond Myklebust 		atomic_long_inc(&NFS_I(data->inode)->redirtied_pages);
18671da177e4SLinus Torvalds 	next:
18681d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
18697f1bda44STrond Myklebust 		/* Latency breaker */
18707f1bda44STrond Myklebust 		cond_resched();
18711da177e4SLinus Torvalds 	}
1872353db796SNeilBrown 
1873f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1874af7cf057STrond Myklebust 	nfs_commit_end(cinfo.mds);
18755917ce84SFred Isaman }
18765917ce84SFred Isaman 
18775917ce84SFred Isaman static void nfs_commit_release(void *calldata)
18785917ce84SFred Isaman {
18790b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
18805917ce84SFred Isaman 
1881f453a54aSFred Isaman 	data->completion_ops->completion(data);
1882c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
18831da177e4SLinus Torvalds }
1884788e7a89STrond Myklebust 
1885788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
18860b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1887788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1888788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1889788e7a89STrond Myklebust };
18901da177e4SLinus Torvalds 
1891f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1892f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1893b20135d0STrond Myklebust 	.resched_write = nfs_commit_resched_write,
1894f453a54aSFred Isaman };
1895f453a54aSFred Isaman 
18961763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1897ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
189884c53ab5SFred Isaman {
189984c53ab5SFred Isaman 	int status;
190084c53ab5SFred Isaman 
1901ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
190284c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1903ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
190484c53ab5SFred Isaman 	return status;
190584c53ab5SFred Isaman }
190684c53ab5SFred Isaman 
1907c4f24df9STrond Myklebust static int __nfs_commit_inode(struct inode *inode, int how,
1908c4f24df9STrond Myklebust 		struct writeback_control *wbc)
19091da177e4SLinus Torvalds {
19101da177e4SLinus Torvalds 	LIST_HEAD(head);
1911ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
191271d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1913c4f24df9STrond Myklebust 	int ret, nscan;
19141da177e4SLinus Torvalds 
191564a93dbfSTrond Myklebust 	how &= ~FLUSH_SYNC;
1916ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1917af7cf057STrond Myklebust 	nfs_commit_begin(cinfo.mds);
1918c4f24df9STrond Myklebust 	for (;;) {
1919c4f24df9STrond Myklebust 		ret = nscan = nfs_scan_commit(inode, &head, &cinfo);
1920c4f24df9STrond Myklebust 		if (ret <= 0)
1921c4f24df9STrond Myklebust 			break;
1922c4f24df9STrond Myklebust 		ret = nfs_generic_commit_list(inode, &head, how, &cinfo);
1923c4f24df9STrond Myklebust 		if (ret < 0)
1924c4f24df9STrond Myklebust 			break;
1925c4f24df9STrond Myklebust 		ret = 0;
1926c4f24df9STrond Myklebust 		if (wbc && wbc->sync_mode == WB_SYNC_NONE) {
1927c4f24df9STrond Myklebust 			if (nscan < wbc->nr_to_write)
1928c4f24df9STrond Myklebust 				wbc->nr_to_write -= nscan;
1929c4f24df9STrond Myklebust 			else
1930c4f24df9STrond Myklebust 				wbc->nr_to_write = 0;
1931c4f24df9STrond Myklebust 		}
1932c4f24df9STrond Myklebust 		if (nscan < INT_MAX)
1933c4f24df9STrond Myklebust 			break;
1934c4f24df9STrond Myklebust 		cond_resched();
1935c4f24df9STrond Myklebust 	}
1936af7cf057STrond Myklebust 	nfs_commit_end(cinfo.mds);
1937c4f24df9STrond Myklebust 	if (ret || !may_wait)
1938c4f24df9STrond Myklebust 		return ret;
1939c4f24df9STrond Myklebust 	return wait_on_commit(cinfo.mds);
1940c4f24df9STrond Myklebust }
1941c4f24df9STrond Myklebust 
1942c4f24df9STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
1943c4f24df9STrond Myklebust {
1944c4f24df9STrond Myklebust 	return __nfs_commit_inode(inode, how, NULL);
19451da177e4SLinus Torvalds }
1946b20135d0STrond Myklebust EXPORT_SYMBOL_GPL(nfs_commit_inode);
19478fc795f7STrond Myklebust 
1948ae09c31fSAnna Schumaker int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
19498fc795f7STrond Myklebust {
1950420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1951420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1952420e3646STrond Myklebust 	int ret = 0;
19538fc795f7STrond Myklebust 
1954c4f24df9STrond Myklebust 	if (wbc->sync_mode == WB_SYNC_NONE) {
19553236c3e1SJeff Layton 		/* no commits means nothing needs to be done */
19565cb953d4STrond Myklebust 		if (!atomic_long_read(&nfsi->commit_info.ncommit))
1957c4f24df9STrond Myklebust 			goto check_requests_outstanding;
19583236c3e1SJeff Layton 
1959a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1960a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1961420e3646STrond Myklebust 		 */
19621a4edf0fSTrond Myklebust 		if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1963420e3646STrond Myklebust 			goto out_mark_dirty;
1964420e3646STrond Myklebust 
1965a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1966420e3646STrond Myklebust 		flags = 0;
1967a00dd6c0SJeff Layton 	}
1968a00dd6c0SJeff Layton 
1969c4f24df9STrond Myklebust 	ret = __nfs_commit_inode(inode, flags, wbc);
1970c4f24df9STrond Myklebust 	if (!ret) {
1971c4f24df9STrond Myklebust 		if (flags & FLUSH_SYNC)
19728fc795f7STrond Myklebust 			return 0;
1973c4f24df9STrond Myklebust 	} else if (atomic_long_read(&nfsi->commit_info.ncommit))
1974c4f24df9STrond Myklebust 		goto out_mark_dirty;
1975c4f24df9STrond Myklebust 
1976c4f24df9STrond Myklebust check_requests_outstanding:
1977c4f24df9STrond Myklebust 	if (!atomic_read(&nfsi->commit_info.rpcs_out))
1978c4f24df9STrond Myklebust 		return ret;
1979420e3646STrond Myklebust out_mark_dirty:
19808fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
19818fc795f7STrond Myklebust 	return ret;
19828fc795f7STrond Myklebust }
198389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode);
1984863a3c6cSAndy Adamson 
1985acdc53b2STrond Myklebust /*
1986837bb1d7STrond Myklebust  * Wrapper for filemap_write_and_wait_range()
1987837bb1d7STrond Myklebust  *
1988837bb1d7STrond Myklebust  * Needed for pNFS in order to ensure data becomes visible to the
1989837bb1d7STrond Myklebust  * client.
1990837bb1d7STrond Myklebust  */
1991837bb1d7STrond Myklebust int nfs_filemap_write_and_wait_range(struct address_space *mapping,
1992837bb1d7STrond Myklebust 		loff_t lstart, loff_t lend)
1993837bb1d7STrond Myklebust {
1994837bb1d7STrond Myklebust 	int ret;
1995837bb1d7STrond Myklebust 
1996837bb1d7STrond Myklebust 	ret = filemap_write_and_wait_range(mapping, lstart, lend);
1997837bb1d7STrond Myklebust 	if (ret == 0)
1998837bb1d7STrond Myklebust 		ret = pnfs_sync_inode(mapping->host, true);
1999837bb1d7STrond Myklebust 	return ret;
2000837bb1d7STrond Myklebust }
2001837bb1d7STrond Myklebust EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range);
2002837bb1d7STrond Myklebust 
2003837bb1d7STrond Myklebust /*
2004acdc53b2STrond Myklebust  * flush the inode to disk.
2005acdc53b2STrond Myklebust  */
2006acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
200734901f70STrond Myklebust {
2008f4ce1299STrond Myklebust 	int ret;
200934901f70STrond Myklebust 
2010f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_enter(inode);
2011f4ce1299STrond Myklebust 
20125bb89b47STrond Myklebust 	ret = filemap_write_and_wait(inode->i_mapping);
20136b196875SChuck Lever 	if (ret)
20146b196875SChuck Lever 		goto out;
20155bb89b47STrond Myklebust 	ret = nfs_commit_inode(inode, FLUSH_SYNC);
20166b196875SChuck Lever 	if (ret < 0)
20176b196875SChuck Lever 		goto out;
20185bb89b47STrond Myklebust 	pnfs_sync_inode(inode, true);
20196b196875SChuck Lever 	ret = 0;
2020f4ce1299STrond Myklebust 
20216b196875SChuck Lever out:
2022f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_exit(inode, ret);
2023f4ce1299STrond Myklebust 	return ret;
20241c75950bSTrond Myklebust }
2025ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all);
20261c75950bSTrond Myklebust 
20276d740c76SMatthew Wilcox (Oracle) int nfs_wb_folio_cancel(struct inode *inode, struct folio *folio)
20281b3b4a1aSTrond Myklebust {
20291b3b4a1aSTrond Myklebust 	struct nfs_page *req;
20301b3b4a1aSTrond Myklebust 	int ret = 0;
20311b3b4a1aSTrond Myklebust 
20326d740c76SMatthew Wilcox (Oracle) 	folio_wait_writeback(folio);
20333e217045SWeston Andros Adamson 
20343e217045SWeston Andros Adamson 	/* blocking call to cancel all requests and join to a single (head)
20353e217045SWeston Andros Adamson 	 * request */
20360c493b5cSTrond Myklebust 	req = nfs_lock_and_join_requests(folio);
20373e217045SWeston Andros Adamson 
20383e217045SWeston Andros Adamson 	if (IS_ERR(req)) {
20393e217045SWeston Andros Adamson 		ret = PTR_ERR(req);
20403e217045SWeston Andros Adamson 	} else if (req) {
20416d740c76SMatthew Wilcox (Oracle) 		/* all requests from this folio have been cancelled by
20423e217045SWeston Andros Adamson 		 * nfs_lock_and_join_requests, so just remove the head
20433e217045SWeston Andros Adamson 		 * request from the inode / page_private pointer and
20443e217045SWeston Andros Adamson 		 * release it */
20451b3b4a1aSTrond Myklebust 		nfs_inode_remove_request(req);
20461d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
20471b3b4a1aSTrond Myklebust 	}
20483e217045SWeston Andros Adamson 
20491b3b4a1aSTrond Myklebust 	return ret;
20501b3b4a1aSTrond Myklebust }
20511b3b4a1aSTrond Myklebust 
20525241060eSTrond Myklebust /**
20535241060eSTrond Myklebust  * nfs_wb_folio - Write back all requests on one page
20545241060eSTrond Myklebust  * @inode: pointer to page
20555241060eSTrond Myklebust  * @folio: pointer to folio
20565241060eSTrond Myklebust  *
20575241060eSTrond Myklebust  * Assumes that the folio has been locked by the caller, and will
20585241060eSTrond Myklebust  * not unlock it.
20591c75950bSTrond Myklebust  */
20605241060eSTrond Myklebust int nfs_wb_folio(struct inode *inode, struct folio *folio)
20611c75950bSTrond Myklebust {
20625241060eSTrond Myklebust 	loff_t range_start = folio_file_pos(folio);
20635241060eSTrond Myklebust 	loff_t range_end = range_start + (loff_t)folio_size(folio) - 1;
20647f2f12d9STrond Myklebust 	struct writeback_control wbc = {
20657f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
20667f2f12d9STrond Myklebust 		.nr_to_write = 0,
20677f2f12d9STrond Myklebust 		.range_start = range_start,
20687f2f12d9STrond Myklebust 		.range_end = range_end,
20697f2f12d9STrond Myklebust 	};
20707f2f12d9STrond Myklebust 	int ret;
20717f2f12d9STrond Myklebust 
2072256093feSTrond Myklebust 	trace_nfs_writeback_folio(inode, folio);
2073f4ce1299STrond Myklebust 
20740522f6adSTrond Myklebust 	for (;;) {
20755241060eSTrond Myklebust 		folio_wait_writeback(folio);
20765241060eSTrond Myklebust 		if (folio_clear_dirty_for_io(folio)) {
20770c493b5cSTrond Myklebust 			ret = nfs_writepage_locked(folio, &wbc);
20787f2f12d9STrond Myklebust 			if (ret < 0)
20797f2f12d9STrond Myklebust 				goto out_error;
20800522f6adSTrond Myklebust 			continue;
20817f2f12d9STrond Myklebust 		}
2082f4ce1299STrond Myklebust 		ret = 0;
20835241060eSTrond Myklebust 		if (!folio_test_private(folio))
20840522f6adSTrond Myklebust 			break;
20850522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
20867f2f12d9STrond Myklebust 		if (ret < 0)
20877f2f12d9STrond Myklebust 			goto out_error;
20887f2f12d9STrond Myklebust 	}
20897f2f12d9STrond Myklebust out_error:
2090256093feSTrond Myklebust 	trace_nfs_writeback_folio_done(inode, folio, ret);
20917f2f12d9STrond Myklebust 	return ret;
20921c75950bSTrond Myklebust }
20931c75950bSTrond Myklebust 
2094074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
20954ae84a80SMatthew Wilcox (Oracle) int nfs_migrate_folio(struct address_space *mapping, struct folio *dst,
20964ae84a80SMatthew Wilcox (Oracle) 		struct folio *src, enum migrate_mode mode)
2097074cc1deSTrond Myklebust {
20982da95652SJeff Layton 	/*
20994ae84a80SMatthew Wilcox (Oracle) 	 * If the private flag is set, the folio is currently associated with
21002da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
21012da95652SJeff Layton 	 *
21022da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
21032da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
21044ae84a80SMatthew Wilcox (Oracle) 	 *        the folio lock.
21052da95652SJeff Layton 	 */
21064ae84a80SMatthew Wilcox (Oracle) 	if (folio_test_private(src))
21072da95652SJeff Layton 		return -EBUSY;
2108074cc1deSTrond Myklebust 
21092e9d7e4bSDavid Howells 	if (folio_test_private_2(src)) { /* [DEPRECATED] */
211016f2f4e6SDavid Howells 		if (mode == MIGRATE_ASYNC)
21118c209ce7SDavid Howells 			return -EBUSY;
21122e9d7e4bSDavid Howells 		folio_wait_private_2(src);
211316f2f4e6SDavid Howells 	}
2114074cc1deSTrond Myklebust 
211554184650SMatthew Wilcox (Oracle) 	return migrate_folio(mapping, dst, src, mode);
2116074cc1deSTrond Myklebust }
2117074cc1deSTrond Myklebust #endif
2118074cc1deSTrond Myklebust 
2119f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
21201da177e4SLinus Torvalds {
21211da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
21221e7f3a48SWeston Andros Adamson 					     sizeof(struct nfs_pgio_header),
21231da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
212420c2df83SPaul Mundt 					     NULL);
21251da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
21261da177e4SLinus Torvalds 		return -ENOMEM;
21271da177e4SLinus Torvalds 
212893d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
21291da177e4SLinus Torvalds 						     nfs_wdata_cachep);
21301da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
21313dd4765fSJeff Layton 		goto out_destroy_write_cache;
21321da177e4SLinus Torvalds 
21330b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
21340b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
21350b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
21360b7c0153SFred Isaman 					     NULL);
21370b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
21383dd4765fSJeff Layton 		goto out_destroy_write_mempool;
21390b7c0153SFred Isaman 
214093d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
21414c100210SYanchuan Nian 						      nfs_cdata_cachep);
21421da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
21433dd4765fSJeff Layton 		goto out_destroy_commit_cache;
21441da177e4SLinus Torvalds 
214589a09141SPeter Zijlstra 	/*
214689a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
214789a09141SPeter Zijlstra 	 *
214889a09141SPeter Zijlstra 	 *  64MB:    8192k
214989a09141SPeter Zijlstra 	 * 128MB:   11585k
215089a09141SPeter Zijlstra 	 * 256MB:   16384k
215189a09141SPeter Zijlstra 	 * 512MB:   23170k
215289a09141SPeter Zijlstra 	 *   1GB:   32768k
215389a09141SPeter Zijlstra 	 *   2GB:   46340k
215489a09141SPeter Zijlstra 	 *   4GB:   65536k
215589a09141SPeter Zijlstra 	 *   8GB:   92681k
215689a09141SPeter Zijlstra 	 *  16GB:  131072k
215789a09141SPeter Zijlstra 	 *
215889a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
215989a09141SPeter Zijlstra 	 * Limit the default to 256M
216089a09141SPeter Zijlstra 	 */
2161ca79b0c2SArun KS 	nfs_congestion_kb = (16*int_sqrt(totalram_pages())) << (PAGE_SHIFT-10);
216289a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
216389a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
216489a09141SPeter Zijlstra 
21651da177e4SLinus Torvalds 	return 0;
21663dd4765fSJeff Layton 
21673dd4765fSJeff Layton out_destroy_commit_cache:
21683dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
21693dd4765fSJeff Layton out_destroy_write_mempool:
21703dd4765fSJeff Layton 	mempool_destroy(nfs_wdata_mempool);
21713dd4765fSJeff Layton out_destroy_write_cache:
21723dd4765fSJeff Layton 	kmem_cache_destroy(nfs_wdata_cachep);
21733dd4765fSJeff Layton 	return -ENOMEM;
21741da177e4SLinus Torvalds }
21751da177e4SLinus Torvalds 
2176266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
21771da177e4SLinus Torvalds {
21781da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
21793dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
21801da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
21811a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
21821da177e4SLinus Torvalds }
21831da177e4SLinus Torvalds 
21844a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops = {
21854a0de55cSAnna Schumaker 	.rw_alloc_header	= nfs_writehdr_alloc,
21864a0de55cSAnna Schumaker 	.rw_free_header		= nfs_writehdr_free,
21870eecb214SAnna Schumaker 	.rw_done		= nfs_writeback_done,
21880eecb214SAnna Schumaker 	.rw_result		= nfs_writeback_result,
21891ed26f33SAnna Schumaker 	.rw_initiate		= nfs_initiate_write,
21904a0de55cSAnna Schumaker };
2191