xref: /linux/fs/nfs/write.c (revision 4201916f2ab13577d45876f4bc784be55e4a83da)
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);
663a3908c8STrond Myklebust static struct nfs_page *
673a3908c8STrond Myklebust nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
680c493b5cSTrond Myklebust 						struct folio *folio);
691da177e4SLinus Torvalds 
70e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep;
713feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
720b7c0153SFred Isaman static struct kmem_cache *nfs_cdata_cachep;
731da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
741da177e4SLinus Torvalds 
75515dcdcdSTrond Myklebust struct nfs_commit_data *nfs_commitdata_alloc(void)
761da177e4SLinus Torvalds {
77518662e0SNeilBrown 	struct nfs_commit_data *p;
7840859d7eSChuck Lever 
79515dcdcdSTrond Myklebust 	p = kmem_cache_zalloc(nfs_cdata_cachep, nfs_io_gfp_mask());
80515dcdcdSTrond Myklebust 	if (!p) {
81518662e0SNeilBrown 		p = mempool_alloc(nfs_commit_mempool, GFP_NOWAIT);
82518662e0SNeilBrown 		if (!p)
83518662e0SNeilBrown 			return NULL;
841da177e4SLinus Torvalds 		memset(p, 0, sizeof(*p));
85515dcdcdSTrond Myklebust 	}
861da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->pages);
871da177e4SLinus Torvalds 	return p;
881da177e4SLinus Torvalds }
89e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
901da177e4SLinus Torvalds 
910b7c0153SFred Isaman void nfs_commit_free(struct nfs_commit_data *p)
921da177e4SLinus Torvalds {
931da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
941da177e4SLinus Torvalds }
95e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free);
961da177e4SLinus Torvalds 
971e7f3a48SWeston Andros Adamson static struct nfs_pgio_header *nfs_writehdr_alloc(void)
983feb2d49STrond Myklebust {
990bae835bSTrond Myklebust 	struct nfs_pgio_header *p;
1003feb2d49STrond Myklebust 
1010bae835bSTrond Myklebust 	p = kmem_cache_zalloc(nfs_wdata_cachep, nfs_io_gfp_mask());
1020bae835bSTrond Myklebust 	if (!p) {
1030bae835bSTrond Myklebust 		p = mempool_alloc(nfs_wdata_mempool, GFP_NOWAIT);
1040bae835bSTrond Myklebust 		if (!p)
1050bae835bSTrond Myklebust 			return NULL;
1063feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
1070bae835bSTrond Myklebust 	}
108fbe77c30SBenjamin Coddington 	p->rw_mode = FMODE_WRITE;
1093feb2d49STrond Myklebust 	return p;
1103feb2d49STrond Myklebust }
1113feb2d49STrond Myklebust 
1121e7f3a48SWeston Andros Adamson static void nfs_writehdr_free(struct nfs_pgio_header *hdr)
1136c75dc0dSFred Isaman {
1141e7f3a48SWeston Andros Adamson 	mempool_free(hdr, nfs_wdata_mempool);
1153feb2d49STrond Myklebust }
1161da177e4SLinus Torvalds 
117919e3bd9STrond Myklebust static struct nfs_io_completion *nfs_io_completion_alloc(gfp_t gfp_flags)
118919e3bd9STrond Myklebust {
119919e3bd9STrond Myklebust 	return kmalloc(sizeof(struct nfs_io_completion), gfp_flags);
120919e3bd9STrond Myklebust }
121919e3bd9STrond Myklebust 
122919e3bd9STrond Myklebust static void nfs_io_completion_init(struct nfs_io_completion *ioc,
123919e3bd9STrond Myklebust 		void (*complete)(void *), void *data)
124919e3bd9STrond Myklebust {
125919e3bd9STrond Myklebust 	ioc->complete = complete;
126919e3bd9STrond Myklebust 	ioc->data = data;
127919e3bd9STrond Myklebust 	kref_init(&ioc->refcount);
128919e3bd9STrond Myklebust }
129919e3bd9STrond Myklebust 
130919e3bd9STrond Myklebust static void nfs_io_completion_release(struct kref *kref)
131919e3bd9STrond Myklebust {
132919e3bd9STrond Myklebust 	struct nfs_io_completion *ioc = container_of(kref,
133919e3bd9STrond Myklebust 			struct nfs_io_completion, refcount);
134919e3bd9STrond Myklebust 	ioc->complete(ioc->data);
135919e3bd9STrond Myklebust 	kfree(ioc);
136919e3bd9STrond Myklebust }
137919e3bd9STrond Myklebust 
138919e3bd9STrond Myklebust static void nfs_io_completion_get(struct nfs_io_completion *ioc)
139919e3bd9STrond Myklebust {
140919e3bd9STrond Myklebust 	if (ioc != NULL)
141919e3bd9STrond Myklebust 		kref_get(&ioc->refcount);
142919e3bd9STrond Myklebust }
143919e3bd9STrond Myklebust 
144919e3bd9STrond Myklebust static void nfs_io_completion_put(struct nfs_io_completion *ioc)
145919e3bd9STrond Myklebust {
146919e3bd9STrond Myklebust 	if (ioc != NULL)
147919e3bd9STrond Myklebust 		kref_put(&ioc->refcount, nfs_io_completion_release);
148919e3bd9STrond Myklebust }
149919e3bd9STrond Myklebust 
150e00ed89dSTrond Myklebust static void
151e00ed89dSTrond Myklebust nfs_page_set_inode_ref(struct nfs_page *req, struct inode *inode)
152e00ed89dSTrond Myklebust {
153e00ed89dSTrond Myklebust 	if (!test_and_set_bit(PG_INODE_REF, &req->wb_flags)) {
154e00ed89dSTrond Myklebust 		kref_get(&req->wb_kref);
155e00ed89dSTrond Myklebust 		atomic_long_inc(&NFS_I(inode)->nrequests);
156e00ed89dSTrond Myklebust 	}
157e00ed89dSTrond Myklebust }
158e00ed89dSTrond Myklebust 
159e00ed89dSTrond Myklebust static int
160e00ed89dSTrond Myklebust nfs_cancel_remove_inode(struct nfs_page *req, struct inode *inode)
161e00ed89dSTrond Myklebust {
162e00ed89dSTrond Myklebust 	int ret;
163e00ed89dSTrond Myklebust 
164e00ed89dSTrond Myklebust 	if (!test_bit(PG_REMOVE, &req->wb_flags))
165e00ed89dSTrond Myklebust 		return 0;
166e00ed89dSTrond Myklebust 	ret = nfs_page_group_lock(req);
167e00ed89dSTrond Myklebust 	if (ret)
168e00ed89dSTrond Myklebust 		return ret;
169e00ed89dSTrond Myklebust 	if (test_and_clear_bit(PG_REMOVE, &req->wb_flags))
170e00ed89dSTrond Myklebust 		nfs_page_set_inode_ref(req, inode);
171e00ed89dSTrond Myklebust 	nfs_page_group_unlock(req);
172e00ed89dSTrond Myklebust 	return 0;
173e00ed89dSTrond Myklebust }
174e00ed89dSTrond Myklebust 
1750c493b5cSTrond Myklebust static struct nfs_page *nfs_folio_private_request(struct folio *folio)
176bd37d6fcSTrond Myklebust {
1770c493b5cSTrond Myklebust 	return folio_get_private(folio);
178bd37d6fcSTrond Myklebust }
179bd37d6fcSTrond Myklebust 
1800c493b5cSTrond Myklebust /**
1810c493b5cSTrond Myklebust  * nfs_folio_find_private_request - find head request associated with a folio
1820c493b5cSTrond Myklebust  * @folio: pointer to folio
18384d3a9a9SWeston Andros Adamson  *
18484d3a9a9SWeston Andros Adamson  * must be called while holding the inode lock.
18584d3a9a9SWeston Andros Adamson  *
18684d3a9a9SWeston Andros Adamson  * returns matching head request with reference held, or NULL if not found.
18784d3a9a9SWeston Andros Adamson  */
1880c493b5cSTrond Myklebust static struct nfs_page *nfs_folio_find_private_request(struct folio *folio)
189277459d2STrond Myklebust {
1900c493b5cSTrond Myklebust 	struct address_space *mapping = folio_file_mapping(folio);
191bd37d6fcSTrond Myklebust 	struct nfs_page *req;
192277459d2STrond Myklebust 
1930c493b5cSTrond Myklebust 	if (!folio_test_private(folio))
194b30d2f04STrond Myklebust 		return NULL;
195600f111eSMatthew Wilcox (Oracle) 	spin_lock(&mapping->i_private_lock);
1960c493b5cSTrond Myklebust 	req = nfs_folio_private_request(folio);
19784d3a9a9SWeston Andros Adamson 	if (req) {
19884d3a9a9SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_head != req);
19929418aa4SMel Gorman 		kref_get(&req->wb_kref);
20084d3a9a9SWeston Andros Adamson 	}
201600f111eSMatthew Wilcox (Oracle) 	spin_unlock(&mapping->i_private_lock);
202b30d2f04STrond Myklebust 	return req;
203b30d2f04STrond Myklebust }
20429418aa4SMel Gorman 
2050c493b5cSTrond Myklebust static struct nfs_page *nfs_folio_find_swap_request(struct folio *folio)
206b30d2f04STrond Myklebust {
2070c493b5cSTrond Myklebust 	struct inode *inode = folio_file_mapping(folio)->host;
208b30d2f04STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
209b30d2f04STrond Myklebust 	struct nfs_page *req = NULL;
2100c493b5cSTrond Myklebust 	if (!folio_test_swapcache(folio))
211b30d2f04STrond Myklebust 		return NULL;
212e824f99aSTrond Myklebust 	mutex_lock(&nfsi->commit_mutex);
2130c493b5cSTrond Myklebust 	if (folio_test_swapcache(folio)) {
214b30d2f04STrond Myklebust 		req = nfs_page_search_commits_for_head_request_locked(nfsi,
2150c493b5cSTrond Myklebust 								      folio);
216b30d2f04STrond Myklebust 		if (req) {
217b30d2f04STrond Myklebust 			WARN_ON_ONCE(req->wb_head != req);
218b30d2f04STrond Myklebust 			kref_get(&req->wb_kref);
219b30d2f04STrond Myklebust 		}
220b30d2f04STrond Myklebust 	}
221e824f99aSTrond Myklebust 	mutex_unlock(&nfsi->commit_mutex);
222277459d2STrond Myklebust 	return req;
223277459d2STrond Myklebust }
224277459d2STrond Myklebust 
2250c493b5cSTrond Myklebust /**
2260c493b5cSTrond Myklebust  * nfs_folio_find_head_request - find head request associated with a folio
2270c493b5cSTrond Myklebust  * @folio: pointer to folio
22884d3a9a9SWeston Andros Adamson  *
22984d3a9a9SWeston Andros Adamson  * returns matching head request with reference held, or NULL if not found.
23084d3a9a9SWeston Andros Adamson  */
2310c493b5cSTrond Myklebust static struct nfs_page *nfs_folio_find_head_request(struct folio *folio)
232277459d2STrond Myklebust {
233b30d2f04STrond Myklebust 	struct nfs_page *req;
234277459d2STrond Myklebust 
2350c493b5cSTrond Myklebust 	req = nfs_folio_find_private_request(folio);
236b30d2f04STrond Myklebust 	if (!req)
2370c493b5cSTrond Myklebust 		req = nfs_folio_find_swap_request(folio);
238277459d2STrond Myklebust 	return req;
239277459d2STrond Myklebust }
240277459d2STrond Myklebust 
2410c493b5cSTrond Myklebust static struct nfs_page *nfs_folio_find_and_lock_request(struct folio *folio)
242e00ed89dSTrond Myklebust {
2430c493b5cSTrond Myklebust 	struct inode *inode = folio_file_mapping(folio)->host;
244e00ed89dSTrond Myklebust 	struct nfs_page *req, *head;
245e00ed89dSTrond Myklebust 	int ret;
246e00ed89dSTrond Myklebust 
247e00ed89dSTrond Myklebust 	for (;;) {
2480c493b5cSTrond Myklebust 		req = nfs_folio_find_head_request(folio);
249e00ed89dSTrond Myklebust 		if (!req)
250e00ed89dSTrond Myklebust 			return req;
251e00ed89dSTrond Myklebust 		head = nfs_page_group_lock_head(req);
252e00ed89dSTrond Myklebust 		if (head != req)
253e00ed89dSTrond Myklebust 			nfs_release_request(req);
254e00ed89dSTrond Myklebust 		if (IS_ERR(head))
255e00ed89dSTrond Myklebust 			return head;
256e00ed89dSTrond Myklebust 		ret = nfs_cancel_remove_inode(head, inode);
257e00ed89dSTrond Myklebust 		if (ret < 0) {
258e00ed89dSTrond Myklebust 			nfs_unlock_and_release_request(head);
259e00ed89dSTrond Myklebust 			return ERR_PTR(ret);
260e00ed89dSTrond Myklebust 		}
261e00ed89dSTrond Myklebust 		/* Ensure that nobody removed the request before we locked it */
2620c493b5cSTrond Myklebust 		if (head == nfs_folio_private_request(folio))
263e00ed89dSTrond Myklebust 			break;
2640c493b5cSTrond Myklebust 		if (folio_test_swapcache(folio))
265e00ed89dSTrond Myklebust 			break;
266e00ed89dSTrond Myklebust 		nfs_unlock_and_release_request(head);
267e00ed89dSTrond Myklebust 	}
268e00ed89dSTrond Myklebust 	return head;
269e00ed89dSTrond Myklebust }
270e00ed89dSTrond Myklebust 
2711da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
2720c493b5cSTrond Myklebust static void nfs_grow_file(struct folio *folio, unsigned int offset,
2730c493b5cSTrond Myklebust 			  unsigned int count)
2741da177e4SLinus Torvalds {
2750c493b5cSTrond Myklebust 	struct inode *inode = folio_file_mapping(folio)->host;
276a3d01454STrond Myklebust 	loff_t end, i_size;
277a3d01454STrond Myklebust 	pgoff_t end_index;
2781da177e4SLinus Torvalds 
279a3d01454STrond Myklebust 	spin_lock(&inode->i_lock);
280a3d01454STrond Myklebust 	i_size = i_size_read(inode);
2810c493b5cSTrond Myklebust 	end_index = ((i_size - 1) >> folio_shift(folio)) << folio_order(folio);
2820c493b5cSTrond Myklebust 	if (i_size > 0 && folio_index(folio) < end_index)
283a3d01454STrond Myklebust 		goto out;
2840c493b5cSTrond Myklebust 	end = folio_file_pos(folio) + (loff_t)offset + (loff_t)count;
2851da177e4SLinus Torvalds 	if (i_size >= end)
286a3d01454STrond Myklebust 		goto out;
287110cb2d2SChuck Lever 	trace_nfs_size_grow(inode, end);
2881da177e4SLinus Torvalds 	i_size_write(inode, end);
289f6cdfa6dSTrond Myklebust 	NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE;
290a3d01454STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
291a3d01454STrond Myklebust out:
292a3d01454STrond Myklebust 	spin_unlock(&inode->i_lock);
293a6b5a28eSDave Wysochanski 	nfs_fscache_invalidate(inode, 0);
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
296a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
297d2ceb7e5SBenjamin Coddington static void nfs_set_pageerror(struct address_space *mapping)
298a301b777STrond Myklebust {
2990df68cedSTrond Myklebust 	struct inode *inode = mapping->host;
3000df68cedSTrond Myklebust 
301d2ceb7e5SBenjamin Coddington 	nfs_zap_mapping(mapping->host, mapping);
3020df68cedSTrond Myklebust 	/* Force file size revalidation */
3030df68cedSTrond Myklebust 	spin_lock(&inode->i_lock);
304ac46b3d7STrond Myklebust 	nfs_set_cache_invalid(inode, NFS_INO_REVAL_FORCED |
30588a6099fSTrond Myklebust 					     NFS_INO_INVALID_CHANGE |
306ac46b3d7STrond Myklebust 					     NFS_INO_INVALID_SIZE);
3070df68cedSTrond Myklebust 	spin_unlock(&inode->i_lock);
308a301b777STrond Myklebust }
309a301b777STrond Myklebust 
3100c493b5cSTrond Myklebust static void nfs_mapping_set_error(struct folio *folio, int error)
3116fbda89bSTrond Myklebust {
3120c493b5cSTrond Myklebust 	struct address_space *mapping = folio_file_mapping(folio);
313b8946d7bSTrond Myklebust 
3140c493b5cSTrond Myklebust 	folio_set_error(folio);
3156c984083STrond Myklebust 	filemap_set_wb_err(mapping, error);
3166c984083STrond Myklebust 	if (mapping->host)
3176c984083STrond Myklebust 		errseq_set(&mapping->host->i_sb->s_wb_err,
3186c984083STrond Myklebust 			   error == -ENOSPC ? -ENOSPC : -EIO);
319b8946d7bSTrond Myklebust 	nfs_set_pageerror(mapping);
3206fbda89bSTrond Myklebust }
3216fbda89bSTrond Myklebust 
322d72ddcbaSWeston Andros Adamson /*
323d72ddcbaSWeston Andros Adamson  * nfs_page_group_search_locked
324d72ddcbaSWeston Andros Adamson  * @head - head request of page group
325d72ddcbaSWeston Andros Adamson  * @page_offset - offset into page
326d72ddcbaSWeston Andros Adamson  *
327d72ddcbaSWeston Andros Adamson  * Search page group with head @head to find a request that contains the
328d72ddcbaSWeston Andros Adamson  * page offset @page_offset.
329d72ddcbaSWeston Andros Adamson  *
330d72ddcbaSWeston Andros Adamson  * Returns a pointer to the first matching nfs request, or NULL if no
331d72ddcbaSWeston Andros Adamson  * match is found.
332d72ddcbaSWeston Andros Adamson  *
333d72ddcbaSWeston Andros Adamson  * Must be called with the page group lock held
334d72ddcbaSWeston Andros Adamson  */
335d72ddcbaSWeston Andros Adamson static struct nfs_page *
336d72ddcbaSWeston Andros Adamson nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset)
337d72ddcbaSWeston Andros Adamson {
338d72ddcbaSWeston Andros Adamson 	struct nfs_page *req;
339d72ddcbaSWeston Andros Adamson 
340d72ddcbaSWeston Andros Adamson 	req = head;
341d72ddcbaSWeston Andros Adamson 	do {
342d72ddcbaSWeston Andros Adamson 		if (page_offset >= req->wb_pgbase &&
343d72ddcbaSWeston Andros Adamson 		    page_offset < (req->wb_pgbase + req->wb_bytes))
344d72ddcbaSWeston Andros Adamson 			return req;
345d72ddcbaSWeston Andros Adamson 
346d72ddcbaSWeston Andros Adamson 		req = req->wb_this_page;
347d72ddcbaSWeston Andros Adamson 	} while (req != head);
348d72ddcbaSWeston Andros Adamson 
349d72ddcbaSWeston Andros Adamson 	return NULL;
350d72ddcbaSWeston Andros Adamson }
351d72ddcbaSWeston Andros Adamson 
352d72ddcbaSWeston Andros Adamson /*
353d72ddcbaSWeston Andros Adamson  * nfs_page_group_covers_page
354d72ddcbaSWeston Andros Adamson  * @head - head request of page group
355d72ddcbaSWeston Andros Adamson  *
356d72ddcbaSWeston Andros Adamson  * Return true if the page group with head @head covers the whole page,
357d72ddcbaSWeston Andros Adamson  * returns false otherwise
358d72ddcbaSWeston Andros Adamson  */
359d72ddcbaSWeston Andros Adamson static bool nfs_page_group_covers_page(struct nfs_page *req)
360d72ddcbaSWeston Andros Adamson {
3610c493b5cSTrond Myklebust 	unsigned int len = nfs_folio_length(nfs_page_to_folio(req));
362d72ddcbaSWeston Andros Adamson 	struct nfs_page *tmp;
363d72ddcbaSWeston Andros Adamson 	unsigned int pos = 0;
364d72ddcbaSWeston Andros Adamson 
3651344b7eaSTrond Myklebust 	nfs_page_group_lock(req);
366d72ddcbaSWeston Andros Adamson 
3677e8a30f8STrond Myklebust 	for (;;) {
368d72ddcbaSWeston Andros Adamson 		tmp = nfs_page_group_search_locked(req->wb_head, pos);
3697e8a30f8STrond Myklebust 		if (!tmp)
3707e8a30f8STrond Myklebust 			break;
3717e8a30f8STrond Myklebust 		pos = tmp->wb_pgbase + tmp->wb_bytes;
372d72ddcbaSWeston Andros Adamson 	}
373d72ddcbaSWeston Andros Adamson 
374d72ddcbaSWeston Andros Adamson 	nfs_page_group_unlock(req);
3757e8a30f8STrond Myklebust 	return pos >= len;
376d72ddcbaSWeston Andros Adamson }
377d72ddcbaSWeston Andros Adamson 
3781da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
3791da177e4SLinus Torvalds  * covers the full page.
3801da177e4SLinus Torvalds  */
381d72ddcbaSWeston Andros Adamson static void nfs_mark_uptodate(struct nfs_page *req)
3821da177e4SLinus Torvalds {
3830c493b5cSTrond Myklebust 	struct folio *folio = nfs_page_to_folio(req);
3840c493b5cSTrond Myklebust 
3850c493b5cSTrond Myklebust 	if (folio_test_uptodate(folio))
3861da177e4SLinus Torvalds 		return;
387d72ddcbaSWeston Andros Adamson 	if (!nfs_page_group_covers_page(req))
3881da177e4SLinus Torvalds 		return;
3890c493b5cSTrond Myklebust 	folio_mark_uptodate(folio);
3901da177e4SLinus Torvalds }
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
3931da177e4SLinus Torvalds {
394e87b4c7aSNeilBrown 	int ret = 0;
395cca588d6STrond Myklebust 
396e87b4c7aSNeilBrown 	if (wbc->sync_mode == WB_SYNC_ALL)
397e87b4c7aSNeilBrown 		ret = FLUSH_COND_STABLE;
398e87b4c7aSNeilBrown 	return ret;
3991da177e4SLinus Torvalds }
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds /*
40289a09141SPeter Zijlstra  * NFS congestion control
40389a09141SPeter Zijlstra  */
40489a09141SPeter Zijlstra 
40589a09141SPeter Zijlstra int nfs_congestion_kb;
40689a09141SPeter Zijlstra 
40789a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
40889a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
40989a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
41089a09141SPeter Zijlstra 
4110c493b5cSTrond Myklebust static void nfs_folio_set_writeback(struct folio *folio)
41289a09141SPeter Zijlstra {
4130c493b5cSTrond Myklebust 	struct nfs_server *nfss = NFS_SERVER(folio_file_mapping(folio)->host);
4145a6d41b3STrond Myklebust 
4150c493b5cSTrond Myklebust 	folio_start_writeback(folio);
4160c493b5cSTrond Myklebust 	if (atomic_long_inc_return(&nfss->writeback) > NFS_CONGESTION_ON_THRESH)
4176df25e58SNeilBrown 		nfss->write_congested = 1;
41889a09141SPeter Zijlstra }
41989a09141SPeter Zijlstra 
4200c493b5cSTrond Myklebust static void nfs_folio_end_writeback(struct folio *folio)
42189a09141SPeter Zijlstra {
4220c493b5cSTrond Myklebust 	struct nfs_server *nfss = NFS_SERVER(folio_file_mapping(folio)->host);
42389a09141SPeter Zijlstra 
4240c493b5cSTrond Myklebust 	folio_end_writeback(folio);
4250c493b5cSTrond Myklebust 	if (atomic_long_dec_return(&nfss->writeback) <
4260c493b5cSTrond Myklebust 	    NFS_CONGESTION_OFF_THRESH)
4276df25e58SNeilBrown 		nfss->write_congested = 0;
42889a09141SPeter Zijlstra }
42989a09141SPeter Zijlstra 
4300c493b5cSTrond Myklebust static void nfs_page_end_writeback(struct nfs_page *req)
4310c493b5cSTrond Myklebust {
4320c493b5cSTrond Myklebust 	if (nfs_page_group_sync_on_bit(req, PG_WB_END)) {
4330c493b5cSTrond Myklebust 		nfs_unlock_request(req);
4340c493b5cSTrond Myklebust 		nfs_folio_end_writeback(nfs_page_to_folio(req));
4350c493b5cSTrond Myklebust 	} else
4360c493b5cSTrond Myklebust 		nfs_unlock_request(req);
4370c493b5cSTrond Myklebust }
4380c493b5cSTrond Myklebust 
439d4581383SWeston Andros Adamson /*
440d4581383SWeston Andros Adamson  * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests
441d4581383SWeston Andros Adamson  *
442d4581383SWeston Andros Adamson  * @destroy_list - request list (using wb_this_page) terminated by @old_head
443d4581383SWeston Andros Adamson  * @old_head - the old head of the list
444d4581383SWeston Andros Adamson  *
445d4581383SWeston Andros Adamson  * All subrequests must be locked and removed from all lists, so at this point
446d4581383SWeston Andros Adamson  * they are only "active" in this function, and possibly in nfs_wait_on_request
447d4581383SWeston Andros Adamson  * with a reference held by some other context.
448d4581383SWeston Andros Adamson  */
449d4581383SWeston Andros Adamson static void
450d4581383SWeston Andros Adamson nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list,
451b66aaa8dSTrond Myklebust 				 struct nfs_page *old_head,
452b66aaa8dSTrond Myklebust 				 struct inode *inode)
453d4581383SWeston Andros Adamson {
454d4581383SWeston Andros Adamson 	while (destroy_list) {
455d4581383SWeston Andros Adamson 		struct nfs_page *subreq = destroy_list;
456d4581383SWeston Andros Adamson 
457d4581383SWeston Andros Adamson 		destroy_list = (subreq->wb_this_page == old_head) ?
458d4581383SWeston Andros Adamson 				   NULL : subreq->wb_this_page;
459d4581383SWeston Andros Adamson 
46008ca8b21STrond Myklebust 		/* Note: lock subreq in order to change subreq->wb_head */
46108ca8b21STrond Myklebust 		nfs_page_set_headlock(subreq);
462d4581383SWeston Andros Adamson 		WARN_ON_ONCE(old_head != subreq->wb_head);
463d4581383SWeston Andros Adamson 
464d4581383SWeston Andros Adamson 		/* make sure old group is not used */
465d4581383SWeston Andros Adamson 		subreq->wb_this_page = subreq;
46608ca8b21STrond Myklebust 		subreq->wb_head = subreq;
467d4581383SWeston Andros Adamson 
468902a4c00STrond Myklebust 		clear_bit(PG_REMOVE, &subreq->wb_flags);
469902a4c00STrond Myklebust 
4705b2b5187STrond Myklebust 		/* Note: races with nfs_page_group_destroy() */
4715b2b5187STrond Myklebust 		if (!kref_read(&subreq->wb_kref)) {
4725b2b5187STrond Myklebust 			/* Check if we raced with nfs_page_group_destroy() */
47308ca8b21STrond Myklebust 			if (test_and_clear_bit(PG_TEARDOWN, &subreq->wb_flags)) {
47408ca8b21STrond Myklebust 				nfs_page_clear_headlock(subreq);
4755b2b5187STrond Myklebust 				nfs_free_request(subreq);
47608ca8b21STrond Myklebust 			} else
47708ca8b21STrond Myklebust 				nfs_page_clear_headlock(subreq);
4785b2b5187STrond Myklebust 			continue;
4795b2b5187STrond Myklebust 		}
48008ca8b21STrond Myklebust 		nfs_page_clear_headlock(subreq);
481d4581383SWeston Andros Adamson 
482add42de3STrond Myklebust 		nfs_release_request(old_head);
4835b2b5187STrond Myklebust 
484b66aaa8dSTrond Myklebust 		if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags)) {
485d4581383SWeston Andros Adamson 			nfs_release_request(subreq);
486a6b6d5b8STrond Myklebust 			atomic_long_dec(&NFS_I(inode)->nrequests);
487d4581383SWeston Andros Adamson 		}
4885b2b5187STrond Myklebust 
4895b2b5187STrond Myklebust 		/* subreq is now totally disconnected from page group or any
4905b2b5187STrond Myklebust 		 * write / commit lists. last chance to wake any waiters */
4915b2b5187STrond Myklebust 		nfs_unlock_and_release_request(subreq);
492d4581383SWeston Andros Adamson 	}
493d4581383SWeston Andros Adamson }
494d4581383SWeston Andros Adamson 
495d4581383SWeston Andros Adamson /*
496e00ed89dSTrond Myklebust  * nfs_join_page_group - destroy subrequests of the head req
497e00ed89dSTrond Myklebust  * @head: the page used to lookup the "page group" of nfs_page structures
498e00ed89dSTrond Myklebust  * @inode: Inode to which the request belongs.
499d4581383SWeston Andros Adamson  *
500d4581383SWeston Andros Adamson  * This function joins all sub requests to the head request by first
501d4581383SWeston Andros Adamson  * locking all requests in the group, cancelling any pending operations
502d4581383SWeston Andros Adamson  * and finally updating the head request to cover the whole range covered by
503d4581383SWeston Andros Adamson  * the (former) group.  All subrequests are removed from any write or commit
504d4581383SWeston Andros Adamson  * lists, unlinked from the group and destroyed.
505d4581383SWeston Andros Adamson  */
506b193a78dSTrond Myklebust void nfs_join_page_group(struct nfs_page *head, struct nfs_commit_info *cinfo,
507b193a78dSTrond Myklebust 			 struct inode *inode)
508d4581383SWeston Andros Adamson {
509e00ed89dSTrond Myklebust 	struct nfs_page *subreq;
510d4581383SWeston Andros Adamson 	struct nfs_page *destroy_list = NULL;
511a62f8e3bSTrond Myklebust 	unsigned int pgbase, off, bytes;
512a62f8e3bSTrond Myklebust 
513a62f8e3bSTrond Myklebust 	pgbase = head->wb_pgbase;
514a62f8e3bSTrond Myklebust 	bytes = head->wb_bytes;
515a62f8e3bSTrond Myklebust 	off = head->wb_offset;
516a0e265bcSTrond Myklebust 	for (subreq = head->wb_this_page; subreq != head;
517a0e265bcSTrond Myklebust 			subreq = subreq->wb_this_page) {
518a62f8e3bSTrond Myklebust 		/* Subrequests should always form a contiguous range */
519a62f8e3bSTrond Myklebust 		if (pgbase > subreq->wb_pgbase) {
520a62f8e3bSTrond Myklebust 			off -= pgbase - subreq->wb_pgbase;
521a62f8e3bSTrond Myklebust 			bytes += pgbase - subreq->wb_pgbase;
522a62f8e3bSTrond Myklebust 			pgbase = subreq->wb_pgbase;
523a62f8e3bSTrond Myklebust 		}
524a62f8e3bSTrond Myklebust 		bytes = max(subreq->wb_pgbase + subreq->wb_bytes
525a62f8e3bSTrond Myklebust 				- pgbase, bytes);
5261bd5d6d0STrond Myklebust 	}
5271bd5d6d0STrond Myklebust 
528a62f8e3bSTrond Myklebust 	/* Set the head request's range to cover the former page group */
529a62f8e3bSTrond Myklebust 	head->wb_pgbase = pgbase;
530a62f8e3bSTrond Myklebust 	head->wb_bytes = bytes;
531a62f8e3bSTrond Myklebust 	head->wb_offset = off;
532d4581383SWeston Andros Adamson 
533d4581383SWeston Andros Adamson 	/* Now that all requests are locked, make sure they aren't on any list.
534d4581383SWeston Andros Adamson 	 * Commit list removal accounting is done after locks are dropped */
535d4581383SWeston Andros Adamson 	subreq = head;
536d4581383SWeston Andros Adamson 	do {
537b193a78dSTrond Myklebust 		nfs_clear_request_commit(cinfo, subreq);
538d4581383SWeston Andros Adamson 		subreq = subreq->wb_this_page;
539d4581383SWeston Andros Adamson 	} while (subreq != head);
540d4581383SWeston Andros Adamson 
541d4581383SWeston Andros Adamson 	/* unlink subrequests from head, destroy them later */
542d4581383SWeston Andros Adamson 	if (head->wb_this_page != head) {
543d4581383SWeston Andros Adamson 		/* destroy list will be terminated by head */
544d4581383SWeston Andros Adamson 		destroy_list = head->wb_this_page;
545d4581383SWeston Andros Adamson 		head->wb_this_page = head;
546d4581383SWeston Andros Adamson 	}
547d4581383SWeston Andros Adamson 
548b66aaa8dSTrond Myklebust 	nfs_destroy_unlinked_subrequests(destroy_list, head, inode);
549b5bab9bfSTrond Myklebust }
550b5bab9bfSTrond Myklebust 
551e00ed89dSTrond Myklebust /*
552e00ed89dSTrond Myklebust  * nfs_lock_and_join_requests - join all subreqs to the head req
5530c493b5cSTrond Myklebust  * @folio: the folio used to lookup the "page group" of nfs_page structures
554e00ed89dSTrond Myklebust  *
555e00ed89dSTrond Myklebust  * This function joins all sub requests to the head request by first
556e00ed89dSTrond Myklebust  * locking all requests in the group, cancelling any pending operations
557e00ed89dSTrond Myklebust  * and finally updating the head request to cover the whole range covered by
558e00ed89dSTrond Myklebust  * the (former) group.  All subrequests are removed from any write or commit
559e00ed89dSTrond Myklebust  * lists, unlinked from the group and destroyed.
560e00ed89dSTrond Myklebust  *
561e00ed89dSTrond Myklebust  * Returns a locked, referenced pointer to the head request - which after
562e00ed89dSTrond Myklebust  * this call is guaranteed to be the only request associated with the page.
5630c493b5cSTrond Myklebust  * Returns NULL if no requests are found for @folio, or a ERR_PTR if an
564e00ed89dSTrond Myklebust  * error was encountered.
565e00ed89dSTrond Myklebust  */
5660c493b5cSTrond Myklebust static struct nfs_page *nfs_lock_and_join_requests(struct folio *folio)
567e00ed89dSTrond Myklebust {
5680c493b5cSTrond Myklebust 	struct inode *inode = folio_file_mapping(folio)->host;
569e00ed89dSTrond Myklebust 	struct nfs_page *head;
570b193a78dSTrond Myklebust 	struct nfs_commit_info cinfo;
571e00ed89dSTrond Myklebust 	int ret;
572e00ed89dSTrond Myklebust 
573b193a78dSTrond Myklebust 	nfs_init_cinfo_from_inode(&cinfo, inode);
574e00ed89dSTrond Myklebust 	/*
575e00ed89dSTrond Myklebust 	 * A reference is taken only on the head request which acts as a
576e00ed89dSTrond Myklebust 	 * reference to the whole page group - the group will not be destroyed
577e00ed89dSTrond Myklebust 	 * until the head reference is released.
578e00ed89dSTrond Myklebust 	 */
5790c493b5cSTrond Myklebust 	head = nfs_folio_find_and_lock_request(folio);
580e00ed89dSTrond Myklebust 	if (IS_ERR_OR_NULL(head))
581d4581383SWeston Andros Adamson 		return head;
5820671d8f1SMarkus Elfring 
583e00ed89dSTrond Myklebust 	/* lock each request in the page group */
584e00ed89dSTrond Myklebust 	ret = nfs_page_group_lock_subrequests(head);
585e00ed89dSTrond Myklebust 	if (ret < 0) {
5860671d8f1SMarkus Elfring 		nfs_unlock_and_release_request(head);
5870671d8f1SMarkus Elfring 		return ERR_PTR(ret);
588612c9384STrond Myklebust 	}
589074cc1deSTrond Myklebust 
590b193a78dSTrond Myklebust 	nfs_join_page_group(head, &cinfo, inode);
591e00ed89dSTrond Myklebust 
592e00ed89dSTrond Myklebust 	return head;
593e00ed89dSTrond Myklebust }
594e00ed89dSTrond Myklebust 
5956fbda89bSTrond Myklebust static void nfs_write_error(struct nfs_page *req, int error)
5960bcbf039SPeng Tao {
5976dd85e83STrond Myklebust 	trace_nfs_write_error(nfs_page_to_inode(req), req, error);
5980c493b5cSTrond Myklebust 	nfs_mapping_set_error(nfs_page_to_folio(req), error);
59906c9fdf3STrond Myklebust 	nfs_inode_remove_request(req);
6000c493b5cSTrond Myklebust 	nfs_page_end_writeback(req);
6011f84ccdfSFred Isaman 	nfs_release_request(req);
6020bcbf039SPeng Tao }
6030bcbf039SPeng Tao 
604074cc1deSTrond Myklebust /*
605074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
606074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
607074cc1deSTrond Myklebust  */
6080c493b5cSTrond Myklebust static int nfs_page_async_flush(struct folio *folio,
609c6fd3511STrond Myklebust 				struct writeback_control *wbc,
610c6fd3511STrond Myklebust 				struct nfs_pageio_descriptor *pgio)
611074cc1deSTrond Myklebust {
612074cc1deSTrond Myklebust 	struct nfs_page *req;
613074cc1deSTrond Myklebust 	int ret = 0;
614074cc1deSTrond Myklebust 
6150c493b5cSTrond Myklebust 	req = nfs_lock_and_join_requests(folio);
616074cc1deSTrond Myklebust 	if (!req)
617074cc1deSTrond Myklebust 		goto out;
618074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
619074cc1deSTrond Myklebust 	if (IS_ERR(req))
620074cc1deSTrond Myklebust 		goto out;
621074cc1deSTrond Myklebust 
6220c493b5cSTrond Myklebust 	nfs_folio_set_writeback(folio);
623deed85e7STrond Myklebust 	WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
624074cc1deSTrond Myklebust 
625a6598813STrond Myklebust 	/* If there is a fatal error that covers this write, just exit */
62696c41455STrond Myklebust 	ret = pgio->pg_error;
62796c41455STrond Myklebust 	if (nfs_error_is_fatal_on_server(ret))
628a6598813STrond Myklebust 		goto out_launder;
629a6598813STrond Myklebust 
63096c41455STrond Myklebust 	ret = 0;
631f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
632074cc1deSTrond Myklebust 		ret = pgio->pg_error;
6330bcbf039SPeng Tao 		/*
634c373fff7STrond Myklebust 		 * Remove the problematic req upon fatal errors on the server
6350bcbf039SPeng Tao 		 */
636c373fff7STrond Myklebust 		if (nfs_error_is_fatal_on_server(ret))
637a6598813STrond Myklebust 			goto out_launder;
638c6fd3511STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE)
639c6fd3511STrond Myklebust 			ret = AOP_WRITEPAGE_ACTIVATE;
6400c493b5cSTrond Myklebust 		folio_redirty_for_writepage(wbc, folio);
6418fc75bedSTrond Myklebust 		nfs_redirty_request(req);
64296c41455STrond Myklebust 		pgio->pg_error = 0;
64340f90271STrond Myklebust 	} else
6440c493b5cSTrond Myklebust 		nfs_add_stats(folio_file_mapping(folio)->host,
64540f90271STrond Myklebust 			      NFSIOS_WRITEPAGES, 1);
646074cc1deSTrond Myklebust out:
647074cc1deSTrond Myklebust 	return ret;
648a6598813STrond Myklebust out_launder:
6496fbda89bSTrond Myklebust 	nfs_write_error(req, ret);
65014bebe3cSTrond Myklebust 	return 0;
651e261f51fSTrond Myklebust }
652e261f51fSTrond Myklebust 
6530c493b5cSTrond Myklebust static int nfs_do_writepage(struct folio *folio, struct writeback_control *wbc,
654c373fff7STrond Myklebust 			    struct nfs_pageio_descriptor *pgio)
655f758c885STrond Myklebust {
6560c493b5cSTrond Myklebust 	nfs_pageio_cond_complete(pgio, folio_index(folio));
6570c493b5cSTrond Myklebust 	return nfs_page_async_flush(folio, wbc, pgio);
658f758c885STrond Myklebust }
659f758c885STrond Myklebust 
660e261f51fSTrond Myklebust /*
6611da177e4SLinus Torvalds  * Write an mmapped page to the server.
6621da177e4SLinus Torvalds  */
6630c493b5cSTrond Myklebust static int nfs_writepage_locked(struct folio *folio,
664c373fff7STrond Myklebust 				struct writeback_control *wbc)
6651da177e4SLinus Torvalds {
666f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
6670c493b5cSTrond Myklebust 	struct inode *inode = folio_file_mapping(folio)->host;
668e261f51fSTrond Myklebust 	int err;
6691da177e4SLinus Torvalds 
67040f90271STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
6710c493b5cSTrond Myklebust 	nfs_pageio_init_write(&pgio, inode, 0, false,
6720c493b5cSTrond Myklebust 			      &nfs_async_write_completion_ops);
6730c493b5cSTrond Myklebust 	err = nfs_do_writepage(folio, wbc, &pgio);
67496c41455STrond Myklebust 	pgio.pg_error = 0;
675f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
6764d770ccfSTrond Myklebust 	return err;
6774d770ccfSTrond Myklebust }
6784d770ccfSTrond Myklebust 
679d585bdbeSMatthew Wilcox (Oracle) static int nfs_writepages_callback(struct folio *folio,
6800c493b5cSTrond Myklebust 				   struct writeback_control *wbc, void *data)
681f758c885STrond Myklebust {
682f758c885STrond Myklebust 	int ret;
683f758c885STrond Myklebust 
6840c493b5cSTrond Myklebust 	ret = nfs_do_writepage(folio, wbc, data);
68596c41455STrond Myklebust 	if (ret != AOP_WRITEPAGE_ACTIVATE)
686d585bdbeSMatthew Wilcox (Oracle) 		folio_unlock(folio);
687f758c885STrond Myklebust 	return ret;
6881da177e4SLinus Torvalds }
6891da177e4SLinus Torvalds 
690919e3bd9STrond Myklebust static void nfs_io_completion_commit(void *inode)
691919e3bd9STrond Myklebust {
692919e3bd9STrond Myklebust 	nfs_commit_inode(inode, 0);
693919e3bd9STrond Myklebust }
694919e3bd9STrond Myklebust 
6951da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
6961da177e4SLinus Torvalds {
6971da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
698c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
699ed7bcdb3STrond Myklebust 	struct nfs_io_completion *ioc = NULL;
700ed7bcdb3STrond Myklebust 	unsigned int mntflags = NFS_SERVER(inode)->flags;
701ed7bcdb3STrond Myklebust 	int priority = 0;
7021da177e4SLinus Torvalds 	int err;
7031da177e4SLinus Torvalds 
7046df25e58SNeilBrown 	if (wbc->sync_mode == WB_SYNC_NONE &&
7056df25e58SNeilBrown 	    NFS_SERVER(inode)->write_congested)
7066df25e58SNeilBrown 		return 0;
7076df25e58SNeilBrown 
70891d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
70991d5b470SChuck Lever 
710ed7bcdb3STrond Myklebust 	if (!(mntflags & NFS_MOUNT_WRITE_EAGER) || wbc->for_kupdate ||
711ed7bcdb3STrond Myklebust 	    wbc->for_background || wbc->for_sync || wbc->for_reclaim) {
7122b17d725STrond Myklebust 		ioc = nfs_io_completion_alloc(GFP_KERNEL);
713919e3bd9STrond Myklebust 		if (ioc)
714ed7bcdb3STrond Myklebust 			nfs_io_completion_init(ioc, nfs_io_completion_commit,
715ed7bcdb3STrond Myklebust 					       inode);
716ed7bcdb3STrond Myklebust 		priority = wb_priority(wbc);
717ed7bcdb3STrond Myklebust 	}
718919e3bd9STrond Myklebust 
719c6fd3511STrond Myklebust 	do {
720ed7bcdb3STrond Myklebust 		nfs_pageio_init_write(&pgio, inode, priority, false,
721a20c93e3SChristoph Hellwig 				      &nfs_async_write_completion_ops);
722919e3bd9STrond Myklebust 		pgio.pg_io_completion = ioc;
723c6fd3511STrond Myklebust 		err = write_cache_pages(mapping, wbc, nfs_writepages_callback,
724c6fd3511STrond Myklebust 					&pgio);
72596c41455STrond Myklebust 		pgio.pg_error = 0;
726c63c7b05STrond Myklebust 		nfs_pageio_complete(&pgio);
7276e7434abSTrond Myklebust 		if (err == -EAGAIN && mntflags & NFS_MOUNT_SOFTERR)
7286e7434abSTrond Myklebust 			break;
729c6fd3511STrond Myklebust 	} while (err < 0 && !nfs_error_is_fatal(err));
730919e3bd9STrond Myklebust 	nfs_io_completion_put(ioc);
73172cb77f4STrond Myklebust 
732f758c885STrond Myklebust 	if (err < 0)
73372cb77f4STrond Myklebust 		goto out_err;
734c63c7b05STrond Myklebust 	return 0;
73572cb77f4STrond Myklebust out_err:
73672cb77f4STrond Myklebust 	return err;
7371da177e4SLinus Torvalds }
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds /*
7401da177e4SLinus Torvalds  * Insert a write request into an inode
7411da177e4SLinus Torvalds  */
7420c493b5cSTrond Myklebust static void nfs_inode_add_request(struct nfs_page *req)
7431da177e4SLinus Torvalds {
7440c493b5cSTrond Myklebust 	struct folio *folio = nfs_page_to_folio(req);
7450c493b5cSTrond Myklebust 	struct address_space *mapping = folio_file_mapping(folio);
7460c493b5cSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(mapping->host);
747e7d39069STrond Myklebust 
7482bfc6e56SWeston Andros Adamson 	WARN_ON_ONCE(req->wb_this_page != req);
7492bfc6e56SWeston Andros Adamson 
750e7d39069STrond Myklebust 	/* Lock the request! */
7517ad84aa9STrond Myklebust 	nfs_lock_request(req);
752e7d39069STrond Myklebust 
75329418aa4SMel Gorman 	/*
75429418aa4SMel Gorman 	 * Swap-space should not get truncated. Hence no need to plug the race
75529418aa4SMel Gorman 	 * with invalidate/truncate.
75629418aa4SMel Gorman 	 */
757600f111eSMatthew Wilcox (Oracle) 	spin_lock(&mapping->i_private_lock);
7580c493b5cSTrond Myklebust 	if (likely(!folio_test_swapcache(folio))) {
7592df485a7STrond Myklebust 		set_bit(PG_MAPPED, &req->wb_flags);
7600c493b5cSTrond Myklebust 		folio_set_private(folio);
7610c493b5cSTrond Myklebust 		folio->private = req;
76229418aa4SMel Gorman 	}
763600f111eSMatthew Wilcox (Oracle) 	spin_unlock(&mapping->i_private_lock);
764a6b6d5b8STrond Myklebust 	atomic_long_inc(&nfsi->nrequests);
76517089a29SWeston Andros Adamson 	/* this a head request for a page group - mark it as having an
766cb1410c7SWeston Andros Adamson 	 * extra reference so sub groups can follow suit.
767cb1410c7SWeston Andros Adamson 	 * This flag also informs pgio layer when to bump nrequests when
768cb1410c7SWeston Andros Adamson 	 * adding subrequests. */
76917089a29SWeston Andros Adamson 	WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags));
770c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
7711da177e4SLinus Torvalds }
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds /*
77489a09141SPeter Zijlstra  * Remove a write request from an inode
7751da177e4SLinus Torvalds  */
7761da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
7771da177e4SLinus Torvalds {
7786a6d4644SScott Mayhew 	struct nfs_inode *nfsi = NFS_I(nfs_page_to_inode(req));
7796a6d4644SScott Mayhew 
78020633f04SWeston Andros Adamson 	if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) {
7810c493b5cSTrond Myklebust 		struct folio *folio = nfs_page_to_folio(req->wb_head);
7820c493b5cSTrond Myklebust 		struct address_space *mapping = folio_file_mapping(folio);
7831da177e4SLinus Torvalds 
784600f111eSMatthew Wilcox (Oracle) 		spin_lock(&mapping->i_private_lock);
7850c493b5cSTrond Myklebust 		if (likely(folio && !folio_test_swapcache(folio))) {
7860c493b5cSTrond Myklebust 			folio->private = NULL;
7870c493b5cSTrond Myklebust 			folio_clear_private(folio);
7880c493b5cSTrond Myklebust 			clear_bit(PG_MAPPED, &req->wb_head->wb_flags);
78929418aa4SMel Gorman 		}
790600f111eSMatthew Wilcox (Oracle) 		spin_unlock(&mapping->i_private_lock);
79120633f04SWeston Andros Adamson 	}
79217089a29SWeston Andros Adamson 
79333ea5aaaSZhangXiaoxu 	if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags)) {
7946a6d4644SScott Mayhew 		atomic_long_dec(&nfsi->nrequests);
795dd1b2026SJeff Layton 		nfs_release_request(req);
79633ea5aaaSZhangXiaoxu 	}
7971da177e4SLinus Torvalds }
7981da177e4SLinus Torvalds 
7990c493b5cSTrond Myklebust static void nfs_mark_request_dirty(struct nfs_page *req)
80061822ab5STrond Myklebust {
8010c493b5cSTrond Myklebust 	struct folio *folio = nfs_page_to_folio(req);
8020c493b5cSTrond Myklebust 	if (folio)
8030c493b5cSTrond Myklebust 		filemap_dirty_folio(folio_mapping(folio), folio);
80461822ab5STrond Myklebust }
80561822ab5STrond Myklebust 
8063a3908c8STrond Myklebust /*
8073a3908c8STrond Myklebust  * nfs_page_search_commits_for_head_request_locked
8083a3908c8STrond Myklebust  *
8090c493b5cSTrond Myklebust  * Search through commit lists on @inode for the head request for @folio.
8103a3908c8STrond Myklebust  * Must be called while holding the inode (which is cinfo) lock.
8113a3908c8STrond Myklebust  *
8123a3908c8STrond Myklebust  * Returns the head request if found, or NULL if not found.
8133a3908c8STrond Myklebust  */
8143a3908c8STrond Myklebust static struct nfs_page *
8153a3908c8STrond Myklebust nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
8160c493b5cSTrond Myklebust 						struct folio *folio)
8173a3908c8STrond Myklebust {
8183a3908c8STrond Myklebust 	struct nfs_page *freq, *t;
8193a3908c8STrond Myklebust 	struct nfs_commit_info cinfo;
8203a3908c8STrond Myklebust 	struct inode *inode = &nfsi->vfs_inode;
8213a3908c8STrond Myklebust 
8223a3908c8STrond Myklebust 	nfs_init_cinfo_from_inode(&cinfo, inode);
8233a3908c8STrond Myklebust 
8243a3908c8STrond Myklebust 	/* search through pnfs commit lists */
8250c493b5cSTrond Myklebust 	freq = pnfs_search_commit_reqs(inode, &cinfo, folio);
8263a3908c8STrond Myklebust 	if (freq)
8273a3908c8STrond Myklebust 		return freq->wb_head;
8283a3908c8STrond Myklebust 
8293a3908c8STrond Myklebust 	/* Linearly search the commit list for the correct request */
8303a3908c8STrond Myklebust 	list_for_each_entry_safe(freq, t, &cinfo.mds->list, wb_list) {
8310c493b5cSTrond Myklebust 		if (nfs_page_to_folio(freq) == folio)
8323a3908c8STrond Myklebust 			return freq->wb_head;
8333a3908c8STrond Myklebust 	}
8343a3908c8STrond Myklebust 
8353a3908c8STrond Myklebust 	return NULL;
8363a3908c8STrond Myklebust }
8373a3908c8STrond Myklebust 
8388dd37758STrond Myklebust /**
83986d80f97STrond Myklebust  * nfs_request_add_commit_list_locked - add request to a commit list
84086d80f97STrond Myklebust  * @req: pointer to a struct nfs_page
84186d80f97STrond Myklebust  * @dst: commit list head
84286d80f97STrond Myklebust  * @cinfo: holds list lock and accounting info
84386d80f97STrond Myklebust  *
84486d80f97STrond Myklebust  * This sets the PG_CLEAN bit, updates the cinfo count of
84586d80f97STrond Myklebust  * number of outstanding requests requiring a commit as well as
84686d80f97STrond Myklebust  * the MM page stats.
84786d80f97STrond Myklebust  *
848e824f99aSTrond Myklebust  * The caller must hold NFS_I(cinfo->inode)->commit_mutex, and the
849e824f99aSTrond Myklebust  * nfs_page lock.
85086d80f97STrond Myklebust  */
85186d80f97STrond Myklebust void
85286d80f97STrond Myklebust nfs_request_add_commit_list_locked(struct nfs_page *req, struct list_head *dst,
85386d80f97STrond Myklebust 			    struct nfs_commit_info *cinfo)
85486d80f97STrond Myklebust {
85586d80f97STrond Myklebust 	set_bit(PG_CLEAN, &req->wb_flags);
85686d80f97STrond Myklebust 	nfs_list_add_request(req, dst);
8575cb953d4STrond Myklebust 	atomic_long_inc(&cinfo->mds->ncommit);
85886d80f97STrond Myklebust }
85986d80f97STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked);
86086d80f97STrond Myklebust 
86186d80f97STrond Myklebust /**
8628dd37758STrond Myklebust  * nfs_request_add_commit_list - add request to a commit list
8638dd37758STrond Myklebust  * @req: pointer to a struct nfs_page
864ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
8658dd37758STrond Myklebust  *
866ea2cf228SFred Isaman  * This sets the PG_CLEAN bit, updates the cinfo count of
8678dd37758STrond Myklebust  * number of outstanding requests requiring a commit as well as
8688dd37758STrond Myklebust  * the MM page stats.
8698dd37758STrond Myklebust  *
870ea2cf228SFred Isaman  * The caller must _not_ hold the cinfo->lock, but must be
8718dd37758STrond Myklebust  * holding the nfs_page lock.
8728dd37758STrond Myklebust  */
8738dd37758STrond Myklebust void
8746272dcc6SAnna Schumaker nfs_request_add_commit_list(struct nfs_page *req, struct nfs_commit_info *cinfo)
8758dd37758STrond Myklebust {
876e824f99aSTrond Myklebust 	mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
8776272dcc6SAnna Schumaker 	nfs_request_add_commit_list_locked(req, &cinfo->mds->list, cinfo);
878e824f99aSTrond Myklebust 	mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
8790c493b5cSTrond Myklebust 	nfs_folio_mark_unstable(nfs_page_to_folio(req), cinfo);
8808dd37758STrond Myklebust }
8818dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
8828dd37758STrond Myklebust 
8838dd37758STrond Myklebust /**
8848dd37758STrond Myklebust  * nfs_request_remove_commit_list - Remove request from a commit list
8858dd37758STrond Myklebust  * @req: pointer to a nfs_page
886ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
8878dd37758STrond Myklebust  *
888ea2cf228SFred Isaman  * This clears the PG_CLEAN bit, and updates the cinfo's count of
8898dd37758STrond Myklebust  * number of outstanding requests requiring a commit
8908dd37758STrond Myklebust  * It does not update the MM page stats.
8918dd37758STrond Myklebust  *
892ea2cf228SFred Isaman  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
8938dd37758STrond Myklebust  */
8948dd37758STrond Myklebust void
895ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req,
896ea2cf228SFred Isaman 			       struct nfs_commit_info *cinfo)
8978dd37758STrond Myklebust {
8988dd37758STrond Myklebust 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
8998dd37758STrond Myklebust 		return;
9008dd37758STrond Myklebust 	nfs_list_remove_request(req);
9015cb953d4STrond Myklebust 	atomic_long_dec(&cinfo->mds->ncommit);
9028dd37758STrond Myklebust }
9038dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
9048dd37758STrond Myklebust 
905ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
906ea2cf228SFred Isaman 				      struct inode *inode)
907ea2cf228SFred Isaman {
908fe238e60SDave Wysochanski 	cinfo->inode = inode;
909ea2cf228SFred Isaman 	cinfo->mds = &NFS_I(inode)->commit_info;
910ea2cf228SFred Isaman 	cinfo->ds = pnfs_get_ds_info(inode);
911b359f9d0SFred Isaman 	cinfo->dreq = NULL;
912f453a54aSFred Isaman 	cinfo->completion_ops = &nfs_commit_completion_ops;
913ea2cf228SFred Isaman }
914ea2cf228SFred Isaman 
915ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
916ea2cf228SFred Isaman 		    struct inode *inode,
917ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq)
918ea2cf228SFred Isaman {
9191763da12SFred Isaman 	if (dreq)
9201763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
9211763da12SFred Isaman 	else
922ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(cinfo, inode);
923ea2cf228SFred Isaman }
924ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo);
9258dd37758STrond Myklebust 
9261da177e4SLinus Torvalds /*
9271da177e4SLinus Torvalds  * Add a request to the inode's commit list.
9281da177e4SLinus Torvalds  */
9291763da12SFred Isaman void
930ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
931b57ff130SWeston Andros Adamson 			struct nfs_commit_info *cinfo, u32 ds_commit_idx)
9321da177e4SLinus Torvalds {
933b57ff130SWeston Andros Adamson 	if (pnfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx))
9348dd37758STrond Myklebust 		return;
9356272dcc6SAnna Schumaker 	nfs_request_add_commit_list(req, cinfo);
9361da177e4SLinus Torvalds }
9378e821cadSTrond Myklebust 
9380c493b5cSTrond Myklebust static void nfs_folio_clear_commit(struct folio *folio)
939e468bae9STrond Myklebust {
9400c493b5cSTrond Myklebust 	if (folio) {
9410c493b5cSTrond Myklebust 		long nr = folio_nr_pages(folio);
9420c493b5cSTrond Myklebust 
9430c493b5cSTrond Myklebust 		node_stat_mod_folio(folio, NR_WRITEBACK, -nr);
9440c493b5cSTrond Myklebust 		wb_stat_mod(&inode_to_bdi(folio_file_mapping(folio)->host)->wb,
9450c493b5cSTrond Myklebust 			    WB_WRITEBACK, -nr);
9460c493b5cSTrond Myklebust 	}
947e468bae9STrond Myklebust }
948d6d6dc7cSFred Isaman 
949b5bab9bfSTrond Myklebust /* Called holding the request lock on @req */
950b193a78dSTrond Myklebust static void nfs_clear_request_commit(struct nfs_commit_info *cinfo,
951b193a78dSTrond Myklebust 				     struct nfs_page *req)
952d6d6dc7cSFred Isaman {
9538dd37758STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
9549fcd5960STrond Myklebust 		struct nfs_open_context *ctx = nfs_req_openctx(req);
9559fcd5960STrond Myklebust 		struct inode *inode = d_inode(ctx->dentry);
956d6d6dc7cSFred Isaman 
957e824f99aSTrond Myklebust 		mutex_lock(&NFS_I(inode)->commit_mutex);
958b193a78dSTrond Myklebust 		if (!pnfs_clear_request_commit(req, cinfo)) {
959b193a78dSTrond Myklebust 			nfs_request_remove_commit_list(req, cinfo);
960d6d6dc7cSFred Isaman 		}
961e824f99aSTrond Myklebust 		mutex_unlock(&NFS_I(inode)->commit_mutex);
9620c493b5cSTrond Myklebust 		nfs_folio_clear_commit(nfs_page_to_folio(req));
9638dd37758STrond Myklebust 	}
964e468bae9STrond Myklebust }
965e468bae9STrond Myklebust 
966d45f60c6SWeston Andros Adamson int nfs_write_need_commit(struct nfs_pgio_header *hdr)
9678e821cadSTrond Myklebust {
968c65e6254SWeston Andros Adamson 	if (hdr->verf.committed == NFS_DATA_SYNC)
969d45f60c6SWeston Andros Adamson 		return hdr->lseg == NULL;
970c65e6254SWeston Andros Adamson 	return hdr->verf.committed != NFS_FILE_SYNC;
9718e821cadSTrond Myklebust }
9728e821cadSTrond Myklebust 
973919e3bd9STrond Myklebust static void nfs_async_write_init(struct nfs_pgio_header *hdr)
974919e3bd9STrond Myklebust {
975919e3bd9STrond Myklebust 	nfs_io_completion_get(hdr->io_completion);
976919e3bd9STrond Myklebust }
977919e3bd9STrond Myklebust 
978061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
9796c75dc0dSFred Isaman {
980ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
9816c75dc0dSFred Isaman 	unsigned long bytes = 0;
9826c75dc0dSFred Isaman 
9836c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
9846c75dc0dSFred Isaman 		goto out;
985ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
9866c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
9876c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
9886c75dc0dSFred Isaman 
9896c75dc0dSFred Isaman 		bytes += req->wb_bytes;
9906c75dc0dSFred Isaman 		nfs_list_remove_request(req);
9916c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
9926c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
993af887e43STrond Myklebust 			trace_nfs_comp_error(hdr->inode, req, hdr->error);
9940c493b5cSTrond Myklebust 			nfs_mapping_set_error(nfs_page_to_folio(req),
9950c493b5cSTrond Myklebust 					      hdr->error);
9966c75dc0dSFred Isaman 			goto remove_req;
9976c75dc0dSFred Isaman 		}
998c65e6254SWeston Andros Adamson 		if (nfs_write_need_commit(hdr)) {
99933344e0fSTrond Myklebust 			/* Reset wb_nio, since the write was successful. */
100033344e0fSTrond Myklebust 			req->wb_nio = 0;
1001f79d06f5SAnna Schumaker 			memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
1002b57ff130SWeston Andros Adamson 			nfs_mark_request_commit(req, hdr->lseg, &cinfo,
1003a7d42ddbSWeston Andros Adamson 				hdr->pgio_mirror_idx);
10046c75dc0dSFred Isaman 			goto next;
10056c75dc0dSFred Isaman 		}
10066c75dc0dSFred Isaman remove_req:
10076c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
10086c75dc0dSFred Isaman next:
10090c493b5cSTrond Myklebust 		nfs_page_end_writeback(req);
10103aff4ebbSTrond Myklebust 		nfs_release_request(req);
10116c75dc0dSFred Isaman 	}
10126c75dc0dSFred Isaman out:
1013919e3bd9STrond Myklebust 	nfs_io_completion_put(hdr->io_completion);
10146c75dc0dSFred Isaman 	hdr->release(hdr);
10156c75dc0dSFred Isaman }
10166c75dc0dSFred Isaman 
1017ce59515cSAnna Schumaker unsigned long
1018ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
1019fb8a1f11STrond Myklebust {
10205cb953d4STrond Myklebust 	return atomic_long_read(&cinfo->mds->ncommit);
1021fb8a1f11STrond Myklebust }
1022fb8a1f11STrond Myklebust 
1023e824f99aSTrond Myklebust /* NFS_I(cinfo->inode)->commit_mutex held by caller */
10241763da12SFred Isaman int
1025ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
1026ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
1027d6d6dc7cSFred Isaman {
1028137da553STrond Myklebust 	struct nfs_page *req, *tmp;
1029d6d6dc7cSFred Isaman 	int ret = 0;
1030d6d6dc7cSFred Isaman 
1031137da553STrond Myklebust 	list_for_each_entry_safe(req, tmp, src, wb_list) {
10327ad84aa9STrond Myklebust 		kref_get(&req->wb_kref);
10332ce209c4STrond Myklebust 		if (!nfs_lock_request(req)) {
1034137da553STrond Myklebust 			nfs_release_request(req);
1035137da553STrond Myklebust 			continue;
1036137da553STrond Myklebust 		}
1037ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
10385d2a9d9dSTrond Myklebust 		clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
10398dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
1040d6d6dc7cSFred Isaman 		ret++;
10411763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
1042d6d6dc7cSFred Isaman 			break;
1043e824f99aSTrond Myklebust 		cond_resched();
1044d6d6dc7cSFred Isaman 	}
1045d6d6dc7cSFred Isaman 	return ret;
1046d6d6dc7cSFred Isaman }
10475d2a9d9dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_scan_commit_list);
1048d6d6dc7cSFred Isaman 
10491da177e4SLinus Torvalds /*
10501da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
10511da177e4SLinus Torvalds  * @inode: NFS inode to scan
1052ea2cf228SFred Isaman  * @dst: mds destination list
1053ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
10541da177e4SLinus Torvalds  *
10551da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
10561da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
10571da177e4SLinus Torvalds  */
10581763da12SFred Isaman int
1059ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
1060ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
10611da177e4SLinus Torvalds {
1062d6d6dc7cSFred Isaman 	int ret = 0;
1063fb8a1f11STrond Myklebust 
10645cb953d4STrond Myklebust 	if (!atomic_long_read(&cinfo->mds->ncommit))
10655cb953d4STrond Myklebust 		return 0;
1066e824f99aSTrond Myklebust 	mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
10675cb953d4STrond Myklebust 	if (atomic_long_read(&cinfo->mds->ncommit) > 0) {
10688dd37758STrond Myklebust 		const int max = INT_MAX;
1069d6d6dc7cSFred Isaman 
1070ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
1071ea2cf228SFred Isaman 					   cinfo, max);
1072ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
1073d6d6dc7cSFred Isaman 	}
1074e824f99aSTrond Myklebust 	mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
1075ff778d02STrond Myklebust 	return ret;
10761da177e4SLinus Torvalds }
1077d6d6dc7cSFred Isaman 
10781da177e4SLinus Torvalds /*
1079e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
1080e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
10811da177e4SLinus Torvalds  *
1082e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
1083e7d39069STrond Myklebust  * to disk.
10841da177e4SLinus Torvalds  */
10850c493b5cSTrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct folio *folio,
1086e7d39069STrond Myklebust 						  unsigned int offset,
1087e7d39069STrond Myklebust 						  unsigned int bytes)
10881da177e4SLinus Torvalds {
1089e7d39069STrond Myklebust 	struct nfs_page *req;
1090e7d39069STrond Myklebust 	unsigned int rqend;
1091e7d39069STrond Myklebust 	unsigned int end;
10921da177e4SLinus Torvalds 	int error;
1093277459d2STrond Myklebust 
1094e7d39069STrond Myklebust 	end = offset + bytes;
1095e7d39069STrond Myklebust 
10960c493b5cSTrond Myklebust 	req = nfs_lock_and_join_requests(folio);
1097f6032f21STrond Myklebust 	if (IS_ERR_OR_NULL(req))
1098f6032f21STrond Myklebust 		return req;
10992bfc6e56SWeston Andros Adamson 
1100e7d39069STrond Myklebust 	rqend = req->wb_offset + req->wb_bytes;
1101e7d39069STrond Myklebust 	/*
1102e7d39069STrond Myklebust 	 * Tell the caller to flush out the request if
1103e7d39069STrond Myklebust 	 * the offsets are non-contiguous.
1104e7d39069STrond Myklebust 	 * Note: nfs_flush_incompatible() will already
1105e7d39069STrond Myklebust 	 * have flushed out requests having wrong owners.
1106e7d39069STrond Myklebust 	 */
1107f6032f21STrond Myklebust 	if (offset > rqend || end < req->wb_offset)
1108e7d39069STrond Myklebust 		goto out_flushme;
1109e7d39069STrond Myklebust 
11101da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
11111da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
11121da177e4SLinus Torvalds 		req->wb_offset = offset;
11131da177e4SLinus Torvalds 		req->wb_pgbase = offset;
11141da177e4SLinus Torvalds 	}
11151da177e4SLinus Torvalds 	if (end > rqend)
11161da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
1117e7d39069STrond Myklebust 	else
1118e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
111933344e0fSTrond Myklebust 	req->wb_nio = 0;
1120e7d39069STrond Myklebust 	return req;
1121e7d39069STrond Myklebust out_flushme:
1122f6032f21STrond Myklebust 	/*
1123f6032f21STrond Myklebust 	 * Note: we mark the request dirty here because
1124f6032f21STrond Myklebust 	 * nfs_lock_and_join_requests() cannot preserve
1125f6032f21STrond Myklebust 	 * commit flags, so we have to replay the write.
1126f6032f21STrond Myklebust 	 */
1127f6032f21STrond Myklebust 	nfs_mark_request_dirty(req);
1128f6032f21STrond Myklebust 	nfs_unlock_and_release_request(req);
11290c493b5cSTrond Myklebust 	error = nfs_wb_folio(folio_file_mapping(folio)->host, folio);
1130f6032f21STrond Myklebust 	return (error < 0) ? ERR_PTR(error) : NULL;
1131e7d39069STrond Myklebust }
11321da177e4SLinus Torvalds 
1133e7d39069STrond Myklebust /*
1134e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
1135e7d39069STrond Myklebust  *
1136e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
1137e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
1138e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
1139e7d39069STrond Myklebust  */
1140e7d39069STrond Myklebust static struct nfs_page *nfs_setup_write_request(struct nfs_open_context *ctx,
11410c493b5cSTrond Myklebust 						struct folio *folio,
11420c493b5cSTrond Myklebust 						unsigned int offset,
11430c493b5cSTrond Myklebust 						unsigned int bytes)
1144e7d39069STrond Myklebust {
1145e7d39069STrond Myklebust 	struct nfs_page *req;
1146e7d39069STrond Myklebust 
11470c493b5cSTrond Myklebust 	req = nfs_try_to_update_request(folio, offset, bytes);
1148e7d39069STrond Myklebust 	if (req != NULL)
1149e7d39069STrond Myklebust 		goto out;
11500c493b5cSTrond Myklebust 	req = nfs_page_create_from_folio(ctx, folio, offset, bytes);
1151e7d39069STrond Myklebust 	if (IS_ERR(req))
1152e7d39069STrond Myklebust 		goto out;
11530c493b5cSTrond Myklebust 	nfs_inode_add_request(req);
1154efc91ed0STrond Myklebust out:
115561e930a9STrond Myklebust 	return req;
11561da177e4SLinus Torvalds }
11571da177e4SLinus Torvalds 
11580c493b5cSTrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx,
11590c493b5cSTrond Myklebust 			       struct folio *folio, unsigned int offset,
11600c493b5cSTrond Myklebust 			       unsigned int count)
1161e7d39069STrond Myklebust {
1162e7d39069STrond Myklebust 	struct nfs_page *req;
1163e7d39069STrond Myklebust 
11640c493b5cSTrond Myklebust 	req = nfs_setup_write_request(ctx, folio, offset, count);
1165e7d39069STrond Myklebust 	if (IS_ERR(req))
1166e7d39069STrond Myklebust 		return PTR_ERR(req);
1167e7d39069STrond Myklebust 	/* Update file length */
11680c493b5cSTrond Myklebust 	nfs_grow_file(folio, offset, count);
1169d72ddcbaSWeston Andros Adamson 	nfs_mark_uptodate(req);
1170a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
11711d1afcbcSTrond Myklebust 	nfs_unlock_and_release_request(req);
1172e7d39069STrond Myklebust 	return 0;
1173e7d39069STrond Myklebust }
1174e7d39069STrond Myklebust 
11750c493b5cSTrond Myklebust int nfs_flush_incompatible(struct file *file, struct folio *folio)
11761da177e4SLinus Torvalds {
1177cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
11782a369153STrond Myklebust 	struct nfs_lock_context *l_ctx;
117917b985deSJeff Layton 	struct file_lock_context *flctx = locks_inode_context(file_inode(file));
11801da177e4SLinus Torvalds 	struct nfs_page	*req;
11811a54533eSTrond Myklebust 	int do_flush, status;
11821da177e4SLinus Torvalds 	/*
11831da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
11841da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
11851da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
11861da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
11871da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
11881da177e4SLinus Torvalds 	 * dropped page.
11891da177e4SLinus Torvalds 	 */
11901a54533eSTrond Myklebust 	do {
11910c493b5cSTrond Myklebust 		req = nfs_folio_find_head_request(folio);
11921a54533eSTrond Myklebust 		if (req == NULL)
11931a54533eSTrond Myklebust 			return 0;
11942a369153STrond Myklebust 		l_ctx = req->wb_lock_context;
11950c493b5cSTrond Myklebust 		do_flush = nfs_page_to_folio(req) != folio ||
11969fcd5960STrond Myklebust 			   !nfs_match_open_context(nfs_req_openctx(req), ctx);
1197bd61e0a9SJeff Layton 		if (l_ctx && flctx &&
1198bd61e0a9SJeff Layton 		    !(list_empty_careful(&flctx->flc_posix) &&
1199bd61e0a9SJeff Layton 		      list_empty_careful(&flctx->flc_flock))) {
1200d51fdb87SNeilBrown 			do_flush |= l_ctx->lockowner != current->files;
12015263e31eSJeff Layton 		}
12021da177e4SLinus Torvalds 		nfs_release_request(req);
12031a54533eSTrond Myklebust 		if (!do_flush)
12041a54533eSTrond Myklebust 			return 0;
12050c493b5cSTrond Myklebust 		status = nfs_wb_folio(folio_file_mapping(folio)->host, folio);
12061a54533eSTrond Myklebust 	} while (status == 0);
12071a54533eSTrond Myklebust 	return status;
12081da177e4SLinus Torvalds }
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds /*
1211dc24826bSAndy Adamson  * Avoid buffered writes when a open context credential's key would
1212dc24826bSAndy Adamson  * expire soon.
1213dc24826bSAndy Adamson  *
1214dc24826bSAndy Adamson  * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
1215dc24826bSAndy Adamson  *
1216dc24826bSAndy Adamson  * Return 0 and set a credential flag which triggers the inode to flush
1217dc24826bSAndy Adamson  * and performs  NFS_FILE_SYNC writes if the key will expired within
1218dc24826bSAndy Adamson  * RPC_KEY_EXPIRE_TIMEO.
1219dc24826bSAndy Adamson  */
1220dc24826bSAndy Adamson int
1221dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode)
1222dc24826bSAndy Adamson {
1223dc24826bSAndy Adamson 	struct nfs_open_context *ctx = nfs_file_open_context(filp);
1224dc24826bSAndy Adamson 
1225ddf529eeSNeilBrown 	if (nfs_ctx_key_to_expire(ctx, inode) &&
1226ca05cbaeSTrond Myklebust 	    !rcu_access_pointer(ctx->ll_cred))
1227ddf529eeSNeilBrown 		/* Already expired! */
1228ddf529eeSNeilBrown 		return -EACCES;
1229ddf529eeSNeilBrown 	return 0;
1230dc24826bSAndy Adamson }
1231dc24826bSAndy Adamson 
1232dc24826bSAndy Adamson /*
1233dc24826bSAndy Adamson  * Test if the open context credential key is marked to expire soon.
1234dc24826bSAndy Adamson  */
1235ce52914eSScott Mayhew bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode)
1236dc24826bSAndy Adamson {
1237ce52914eSScott Mayhew 	struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
1238ca05cbaeSTrond Myklebust 	struct rpc_cred *cred, *new, *old = NULL;
1239ddf529eeSNeilBrown 	struct auth_cred acred = {
1240a52458b4SNeilBrown 		.cred = ctx->cred,
1241ddf529eeSNeilBrown 	};
1242ca05cbaeSTrond Myklebust 	bool ret = false;
1243ce52914eSScott Mayhew 
1244ca05cbaeSTrond Myklebust 	rcu_read_lock();
1245ca05cbaeSTrond Myklebust 	cred = rcu_dereference(ctx->ll_cred);
1246ca05cbaeSTrond Myklebust 	if (cred && !(cred->cr_ops->crkey_timeout &&
1247ca05cbaeSTrond Myklebust 		      cred->cr_ops->crkey_timeout(cred)))
1248ca05cbaeSTrond Myklebust 		goto out;
1249ca05cbaeSTrond Myklebust 	rcu_read_unlock();
1250ca05cbaeSTrond Myklebust 
1251ca05cbaeSTrond Myklebust 	new = auth->au_ops->lookup_cred(auth, &acred, 0);
1252ca05cbaeSTrond Myklebust 	if (new == cred) {
1253ca05cbaeSTrond Myklebust 		put_rpccred(new);
1254ddf529eeSNeilBrown 		return true;
1255ca05cbaeSTrond Myklebust 	}
1256ca05cbaeSTrond Myklebust 	if (IS_ERR_OR_NULL(new)) {
1257ca05cbaeSTrond Myklebust 		new = NULL;
1258ca05cbaeSTrond Myklebust 		ret = true;
1259ca05cbaeSTrond Myklebust 	} else if (new->cr_ops->crkey_timeout &&
1260ca05cbaeSTrond Myklebust 		   new->cr_ops->crkey_timeout(new))
1261ca05cbaeSTrond Myklebust 		ret = true;
1262ca05cbaeSTrond Myklebust 
1263ca05cbaeSTrond Myklebust 	rcu_read_lock();
1264ca05cbaeSTrond Myklebust 	old = rcu_dereference_protected(xchg(&ctx->ll_cred,
1265ca05cbaeSTrond Myklebust 					     RCU_INITIALIZER(new)), 1);
1266ca05cbaeSTrond Myklebust out:
1267ca05cbaeSTrond Myklebust 	rcu_read_unlock();
1268ca05cbaeSTrond Myklebust 	put_rpccred(old);
1269ca05cbaeSTrond Myklebust 	return ret;
1270dc24826bSAndy Adamson }
1271dc24826bSAndy Adamson 
1272dc24826bSAndy Adamson /*
12735d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
12745d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
12755d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
12765d47a356STrond Myklebust  */
12770c493b5cSTrond Myklebust static bool nfs_folio_write_uptodate(struct folio *folio, unsigned int pagelen)
12785d47a356STrond Myklebust {
12790c493b5cSTrond Myklebust 	struct inode *inode = folio_file_mapping(folio)->host;
1280d529ef83SJeff Layton 	struct nfs_inode *nfsi = NFS_I(inode);
1281d529ef83SJeff Layton 
12828d197a56STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
12838d197a56STrond Myklebust 		goto out;
1284fc9dc401STrond Myklebust 	if (nfsi->cache_validity &
128513c0b082STrond Myklebust 	    (NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE))
1286d529ef83SJeff Layton 		return false;
12874db72b40SJeff Layton 	smp_rmb();
1288fc9dc401STrond Myklebust 	if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags) && pagelen != 0)
12898d197a56STrond Myklebust 		return false;
12908d197a56STrond Myklebust out:
1291fc9dc401STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_INVALID_DATA && pagelen != 0)
129218dd78c4SScott Mayhew 		return false;
12930c493b5cSTrond Myklebust 	return folio_test_uptodate(folio) != 0;
12945d47a356STrond Myklebust }
12955d47a356STrond Myklebust 
12965263e31eSJeff Layton static bool
12975263e31eSJeff Layton is_whole_file_wrlock(struct file_lock *fl)
12985263e31eSJeff Layton {
12995263e31eSJeff Layton 	return fl->fl_start == 0 && fl->fl_end == OFFSET_MAX &&
1300d7c9616bSJeff Layton 			lock_is_write(fl);
13015263e31eSJeff Layton }
13025263e31eSJeff Layton 
1303c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or
1304c7559663SScott Mayhew  * if we have the whole file locked for writing), it may be more efficient to
1305c7559663SScott Mayhew  * extend the write to cover the entire page in order to avoid fragmentation
1306c7559663SScott Mayhew  * inefficiencies.
1307c7559663SScott Mayhew  *
1308263b4509SScott Mayhew  * If the file is opened for synchronous writes then we can just skip the rest
1309263b4509SScott Mayhew  * of the checks.
1310c7559663SScott Mayhew  */
13110c493b5cSTrond Myklebust static int nfs_can_extend_write(struct file *file, struct folio *folio,
13120c493b5cSTrond Myklebust 				unsigned int pagelen)
1313c7559663SScott Mayhew {
13140c493b5cSTrond Myklebust 	struct inode *inode = file_inode(file);
131517b985deSJeff Layton 	struct file_lock_context *flctx = locks_inode_context(inode);
13165263e31eSJeff Layton 	struct file_lock *fl;
13170c493b5cSTrond Myklebust 	int ret;
13185263e31eSJeff Layton 
1319c7559663SScott Mayhew 	if (file->f_flags & O_DSYNC)
1320c7559663SScott Mayhew 		return 0;
13210c493b5cSTrond Myklebust 	if (!nfs_folio_write_uptodate(folio, pagelen))
1322263b4509SScott Mayhew 		return 0;
1323*4201916fSTrond Myklebust 	if (nfs_have_write_delegation(inode))
1324c7559663SScott Mayhew 		return 1;
1325bd61e0a9SJeff Layton 	if (!flctx || (list_empty_careful(&flctx->flc_flock) &&
1326bd61e0a9SJeff Layton 		       list_empty_careful(&flctx->flc_posix)))
13278fa4592aSTrond Myklebust 		return 1;
13285263e31eSJeff Layton 
13295263e31eSJeff Layton 	/* Check to see if there are whole file write locks */
13305263e31eSJeff Layton 	ret = 0;
13316109c850SJeff Layton 	spin_lock(&flctx->flc_lock);
1332bd61e0a9SJeff Layton 	if (!list_empty(&flctx->flc_posix)) {
1333bd61e0a9SJeff Layton 		fl = list_first_entry(&flctx->flc_posix, struct file_lock,
1334dd1fac6aSJeff Layton 					c.flc_list);
1335bd61e0a9SJeff Layton 		if (is_whole_file_wrlock(fl))
13365263e31eSJeff Layton 			ret = 1;
1337bd61e0a9SJeff Layton 	} else if (!list_empty(&flctx->flc_flock)) {
13385263e31eSJeff Layton 		fl = list_first_entry(&flctx->flc_flock, struct file_lock,
1339dd1fac6aSJeff Layton 					c.flc_list);
1340d7c9616bSJeff Layton 		if (lock_is_write(fl))
13415263e31eSJeff Layton 			ret = 1;
13425263e31eSJeff Layton 	}
13436109c850SJeff Layton 	spin_unlock(&flctx->flc_lock);
13445263e31eSJeff Layton 	return ret;
1345c7559663SScott Mayhew }
1346c7559663SScott Mayhew 
13475d47a356STrond Myklebust /*
13481da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
13491da177e4SLinus Torvalds  *
13501da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
13511da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
13521da177e4SLinus Torvalds  */
13530c493b5cSTrond Myklebust int nfs_update_folio(struct file *file, struct folio *folio,
13541da177e4SLinus Torvalds 		     unsigned int offset, unsigned int count)
13551da177e4SLinus Torvalds {
1356cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
13570c493b5cSTrond Myklebust 	struct address_space *mapping = folio_file_mapping(folio);
1358d2ceb7e5SBenjamin Coddington 	struct inode *inode = mapping->host;
13590c493b5cSTrond Myklebust 	unsigned int pagelen = nfs_folio_length(folio);
13601da177e4SLinus Torvalds 	int		status = 0;
13611da177e4SLinus Torvalds 
136291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
136391d5b470SChuck Lever 
13640c493b5cSTrond Myklebust 	dprintk("NFS:       nfs_update_folio(%pD2 %d@%lld)\n", file, count,
13650c493b5cSTrond Myklebust 		(long long)(folio_file_pos(folio) + offset));
13661da177e4SLinus Torvalds 
1367149a4fddSBenjamin Coddington 	if (!count)
1368149a4fddSBenjamin Coddington 		goto out;
1369149a4fddSBenjamin Coddington 
13700c493b5cSTrond Myklebust 	if (nfs_can_extend_write(file, folio, pagelen)) {
1371fc9dc401STrond Myklebust 		count = max(count + offset, pagelen);
13721da177e4SLinus Torvalds 		offset = 0;
13731da177e4SLinus Torvalds 	}
13741da177e4SLinus Torvalds 
13750c493b5cSTrond Myklebust 	status = nfs_writepage_setup(ctx, folio, offset, count);
137603fa9e84STrond Myklebust 	if (status < 0)
1377d2ceb7e5SBenjamin Coddington 		nfs_set_pageerror(mapping);
1378149a4fddSBenjamin Coddington out:
13790c493b5cSTrond Myklebust 	dprintk("NFS:       nfs_update_folio returns %d (isize %lld)\n",
13801da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
13811da177e4SLinus Torvalds 	return status;
13821da177e4SLinus Torvalds }
13831da177e4SLinus Torvalds 
13843ff7576dSTrond Myklebust static int flush_task_priority(int how)
13851da177e4SLinus Torvalds {
13861da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
13871da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
13881da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
13891da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
13901da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
13911da177e4SLinus Torvalds 	}
13921da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
13931da177e4SLinus Torvalds }
13941da177e4SLinus Torvalds 
1395d45f60c6SWeston Andros Adamson static void nfs_initiate_write(struct nfs_pgio_header *hdr,
1396d45f60c6SWeston Andros Adamson 			       struct rpc_message *msg,
1397abde71f4STom Haynes 			       const struct nfs_rpc_ops *rpc_ops,
13981ed26f33SAnna Schumaker 			       struct rpc_task_setup *task_setup_data, int how)
13991da177e4SLinus Torvalds {
14003ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
14011da177e4SLinus Torvalds 
14028db55a03SNeilBrown 	if (IS_SWAPFILE(hdr->inode))
14038db55a03SNeilBrown 		task_setup_data->flags |= RPC_TASK_SWAPPER;
14041ed26f33SAnna Schumaker 	task_setup_data->priority = priority;
1405fb91fb0eSAnna Schumaker 	rpc_ops->write_setup(hdr, msg, &task_setup_data->rpc_client);
14065bb2a7cbSTrond Myklebust 	trace_nfs_initiate_write(hdr);
1407275acaafSTrond Myklebust }
1408275acaafSTrond Myklebust 
14096d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
14106d884e8fSFred  * call this on each, which will prepare them to be retried on next
14116d884e8fSFred  * writeback using standard nfs.
14126d884e8fSFred  */
14136d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
14146d884e8fSFred {
14156dd85e83STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(nfs_page_to_inode(req));
141667f4b5dcSTrond Myklebust 
141733344e0fSTrond Myklebust 	/* Bump the transmission count */
141833344e0fSTrond Myklebust 	req->wb_nio++;
14196d884e8fSFred 	nfs_mark_request_dirty(req);
142067f4b5dcSTrond Myklebust 	atomic_long_inc(&nfsi->redirtied_pages);
14210c493b5cSTrond Myklebust 	nfs_page_end_writeback(req);
14223aff4ebbSTrond Myklebust 	nfs_release_request(req);
14236d884e8fSFred }
14246d884e8fSFred 
1425df3accb8STrond Myklebust static void nfs_async_write_error(struct list_head *head, int error)
14266c75dc0dSFred Isaman {
14276c75dc0dSFred Isaman 	struct nfs_page	*req;
14286c75dc0dSFred Isaman 
14296c75dc0dSFred Isaman 	while (!list_empty(head)) {
14306c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
14316c75dc0dSFred Isaman 		nfs_list_remove_request(req);
1432cea9ba72STrond Myklebust 		if (nfs_error_is_fatal_on_server(error))
14336fbda89bSTrond Myklebust 			nfs_write_error(req, error);
14346fbda89bSTrond Myklebust 		else
14356c75dc0dSFred Isaman 			nfs_redirty_request(req);
14366c75dc0dSFred Isaman 	}
14376c75dc0dSFred Isaman }
14386c75dc0dSFred Isaman 
1439dc602dd7STrond Myklebust static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr)
1440dc602dd7STrond Myklebust {
1441df3accb8STrond Myklebust 	nfs_async_write_error(&hdr->pages, 0);
1442dc602dd7STrond Myklebust }
1443dc602dd7STrond Myklebust 
1444061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1445919e3bd9STrond Myklebust 	.init_hdr = nfs_async_write_init,
1446061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1447061ae2edSFred Isaman 	.completion = nfs_write_completion,
1448dc602dd7STrond Myklebust 	.reschedule_io = nfs_async_write_reschedule_io,
1449061ae2edSFred Isaman };
1450061ae2edSFred Isaman 
145157208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1452a20c93e3SChristoph Hellwig 			       struct inode *inode, int ioflags, bool force_mds,
1453061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
14541751c363STrond Myklebust {
1455a20c93e3SChristoph Hellwig 	struct nfs_server *server = NFS_SERVER(inode);
145641d8d5b7SAnna Schumaker 	const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
1457a20c93e3SChristoph Hellwig 
1458a20c93e3SChristoph Hellwig #ifdef CONFIG_NFS_V4_1
1459a20c93e3SChristoph Hellwig 	if (server->pnfs_curr_ld && !force_mds)
1460a20c93e3SChristoph Hellwig 		pg_ops = server->pnfs_curr_ld->pg_write_ops;
1461a20c93e3SChristoph Hellwig #endif
14624a0de55cSAnna Schumaker 	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
14633bde7afdSTrond Myklebust 			server->wsize, ioflags);
14641751c363STrond Myklebust }
1465ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
14661751c363STrond Myklebust 
1467dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1468dce81290STrond Myklebust {
1469a7d42ddbSWeston Andros Adamson 	struct nfs_pgio_mirror *mirror;
1470a7d42ddbSWeston Andros Adamson 
14716f29b9bbSKinglong Mee 	if (pgio->pg_ops && pgio->pg_ops->pg_cleanup)
14726f29b9bbSKinglong Mee 		pgio->pg_ops->pg_cleanup(pgio);
14736f29b9bbSKinglong Mee 
147441d8d5b7SAnna Schumaker 	pgio->pg_ops = &nfs_pgio_rw_ops;
1475a7d42ddbSWeston Andros Adamson 
1476a7d42ddbSWeston Andros Adamson 	nfs_pageio_stop_mirroring(pgio);
1477a7d42ddbSWeston Andros Adamson 
1478a7d42ddbSWeston Andros Adamson 	mirror = &pgio->pg_mirrors[0];
1479a7d42ddbSWeston Andros Adamson 	mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1480dce81290STrond Myklebust }
14811f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1482dce81290STrond Myklebust 
14831da177e4SLinus Torvalds 
14840b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
14850b7c0153SFred Isaman {
14860b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
14870b7c0153SFred Isaman 
14880b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
14890b7c0153SFred Isaman }
14900b7c0153SFred Isaman 
1491a08a8cd3STrond Myklebust static void nfs_writeback_check_extend(struct nfs_pgio_header *hdr,
1492a08a8cd3STrond Myklebust 		struct nfs_fattr *fattr)
1493a08a8cd3STrond Myklebust {
1494a08a8cd3STrond Myklebust 	struct nfs_pgio_args *argp = &hdr->args;
1495a08a8cd3STrond Myklebust 	struct nfs_pgio_res *resp = &hdr->res;
14962b83d3deSTrond Myklebust 	u64 size = argp->offset + resp->count;
1497a08a8cd3STrond Myklebust 
1498a08a8cd3STrond Myklebust 	if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
14992b83d3deSTrond Myklebust 		fattr->size = size;
15002b83d3deSTrond Myklebust 	if (nfs_size_to_loff_t(fattr->size) < i_size_read(hdr->inode)) {
15012b83d3deSTrond Myklebust 		fattr->valid &= ~NFS_ATTR_FATTR_SIZE;
1502a08a8cd3STrond Myklebust 		return;
15032b83d3deSTrond Myklebust 	}
15042b83d3deSTrond Myklebust 	if (size != fattr->size)
1505a08a8cd3STrond Myklebust 		return;
1506a08a8cd3STrond Myklebust 	/* Set attribute barrier */
1507a08a8cd3STrond Myklebust 	nfs_fattr_set_barrier(fattr);
15082b83d3deSTrond Myklebust 	/* ...and update size */
15092b83d3deSTrond Myklebust 	fattr->valid |= NFS_ATTR_FATTR_SIZE;
1510a08a8cd3STrond Myklebust }
1511a08a8cd3STrond Myklebust 
1512a08a8cd3STrond Myklebust void nfs_writeback_update_inode(struct nfs_pgio_header *hdr)
1513a08a8cd3STrond Myklebust {
15142b83d3deSTrond Myklebust 	struct nfs_fattr *fattr = &hdr->fattr;
1515a08a8cd3STrond Myklebust 	struct inode *inode = hdr->inode;
1516a08a8cd3STrond Myklebust 
1517a08a8cd3STrond Myklebust 	spin_lock(&inode->i_lock);
1518a08a8cd3STrond Myklebust 	nfs_writeback_check_extend(hdr, fattr);
1519a08a8cd3STrond Myklebust 	nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
1520a08a8cd3STrond Myklebust 	spin_unlock(&inode->i_lock);
1521a08a8cd3STrond Myklebust }
1522a08a8cd3STrond Myklebust EXPORT_SYMBOL_GPL(nfs_writeback_update_inode);
1523a08a8cd3STrond Myklebust 
15241da177e4SLinus Torvalds /*
15251da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
15261da177e4SLinus Torvalds  */
1527d45f60c6SWeston Andros Adamson static int nfs_writeback_done(struct rpc_task *task,
1528d45f60c6SWeston Andros Adamson 			      struct nfs_pgio_header *hdr,
15290eecb214SAnna Schumaker 			      struct inode *inode)
15301da177e4SLinus Torvalds {
1531788e7a89STrond Myklebust 	int status;
15321da177e4SLinus Torvalds 
1533f551e44fSChuck Lever 	/*
1534f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1535f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1536f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1537f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1538f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1539f551e44fSChuck Lever 	 */
1540d45f60c6SWeston Andros Adamson 	status = NFS_PROTO(inode)->write_done(task, hdr);
1541788e7a89STrond Myklebust 	if (status != 0)
15420eecb214SAnna Schumaker 		return status;
15438224b273SChuck Lever 
1544d45f60c6SWeston Andros Adamson 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count);
15455bb2a7cbSTrond Myklebust 	trace_nfs_writeback_done(task, hdr);
154691d5b470SChuck Lever 
154769d96651SJeff Layton 	if (task->tk_status >= 0) {
154869d96651SJeff Layton 		enum nfs3_stable_how committed = hdr->res.verf->committed;
154969d96651SJeff Layton 
155069d96651SJeff Layton 		if (committed == NFS_UNSTABLE) {
155169d96651SJeff Layton 			/*
155269d96651SJeff Layton 			 * We have some uncommitted data on the server at
155369d96651SJeff Layton 			 * this point, so ensure that we keep track of that
155469d96651SJeff Layton 			 * fact irrespective of what later writes do.
155569d96651SJeff Layton 			 */
155669d96651SJeff Layton 			set_bit(NFS_IOHDR_UNSTABLE_WRITES, &hdr->flags);
155769d96651SJeff Layton 		}
155869d96651SJeff Layton 
155969d96651SJeff Layton 		if (committed < hdr->args.stable) {
15601da177e4SLinus Torvalds 			/* We tried a write call, but the server did not
15611da177e4SLinus Torvalds 			 * commit data to stable storage even though we
15621da177e4SLinus Torvalds 			 * requested it.
15631da177e4SLinus Torvalds 			 * Note: There is a known bug in Tru64 < 5.0 in which
15641da177e4SLinus Torvalds 			 *	 the server reports NFS_DATA_SYNC, but performs
15651da177e4SLinus Torvalds 			 *	 NFS_FILE_SYNC. We therefore implement this checking
15661da177e4SLinus Torvalds 			 *	 as a dprintk() in order to avoid filling syslog.
15671da177e4SLinus Torvalds 			 */
15681da177e4SLinus Torvalds 			static unsigned long    complain;
15691da177e4SLinus Torvalds 
1570a69aef14SFred Isaman 			/* Note this will print the MDS for a DS write */
15711da177e4SLinus Torvalds 			if (time_before(complain, jiffies)) {
15721da177e4SLinus Torvalds 				dprintk("NFS:       faulty NFS server %s:"
15731da177e4SLinus Torvalds 					" (committed = %d) != (stable = %d)\n",
1574cd841605SFred Isaman 					NFS_SERVER(inode)->nfs_client->cl_hostname,
157569d96651SJeff Layton 					committed, hdr->args.stable);
15761da177e4SLinus Torvalds 				complain = jiffies + 300 * HZ;
15771da177e4SLinus Torvalds 			}
15781da177e4SLinus Torvalds 		}
157969d96651SJeff Layton 	}
15801f2edbe3STrond Myklebust 
15811f2edbe3STrond Myklebust 	/* Deal with the suid/sgid bit corner case */
158216e14375STrond Myklebust 	if (nfs_should_remove_suid(inode)) {
158316e14375STrond Myklebust 		spin_lock(&inode->i_lock);
1584720869ebSTrond Myklebust 		nfs_set_cache_invalid(inode, NFS_INO_INVALID_MODE);
158516e14375STrond Myklebust 		spin_unlock(&inode->i_lock);
158616e14375STrond Myklebust 	}
15870eecb214SAnna Schumaker 	return 0;
15880eecb214SAnna Schumaker }
15890eecb214SAnna Schumaker 
15900eecb214SAnna Schumaker /*
15910eecb214SAnna Schumaker  * This function is called when the WRITE call is complete.
15920eecb214SAnna Schumaker  */
1593d45f60c6SWeston Andros Adamson static void nfs_writeback_result(struct rpc_task *task,
1594d45f60c6SWeston Andros Adamson 				 struct nfs_pgio_header *hdr)
15950eecb214SAnna Schumaker {
1596d45f60c6SWeston Andros Adamson 	struct nfs_pgio_args	*argp = &hdr->args;
1597d45f60c6SWeston Andros Adamson 	struct nfs_pgio_res	*resp = &hdr->res;
15981f2edbe3STrond Myklebust 
15991f2edbe3STrond Myklebust 	if (resp->count < argp->count) {
16001da177e4SLinus Torvalds 		static unsigned long    complain;
16011da177e4SLinus Torvalds 
16026c75dc0dSFred Isaman 		/* This a short write! */
1603d45f60c6SWeston Andros Adamson 		nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE);
160491d5b470SChuck Lever 
16051da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
16066c75dc0dSFred Isaman 		if (resp->count == 0) {
16076c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
16086c75dc0dSFred Isaman 				printk(KERN_WARNING
16096c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
16106c75dc0dSFred Isaman 				       argp->count);
16116c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
16126c75dc0dSFred Isaman 			}
1613d45f60c6SWeston Andros Adamson 			nfs_set_pgio_error(hdr, -EIO, argp->offset);
16146c75dc0dSFred Isaman 			task->tk_status = -EIO;
16156c75dc0dSFred Isaman 			return;
16166c75dc0dSFred Isaman 		}
1617f8417b48SKinglong Mee 
1618f8417b48SKinglong Mee 		/* For non rpc-based layout drivers, retry-through-MDS */
1619f8417b48SKinglong Mee 		if (!task->tk_ops) {
1620f8417b48SKinglong Mee 			hdr->pnfs_error = -EAGAIN;
1621f8417b48SKinglong Mee 			return;
1622f8417b48SKinglong Mee 		}
1623f8417b48SKinglong Mee 
16241da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
16251da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
16261da177e4SLinus Torvalds 			/* Resend from where the server left off */
1627d45f60c6SWeston Andros Adamson 			hdr->mds_offset += resp->count;
16281da177e4SLinus Torvalds 			argp->offset += resp->count;
16291da177e4SLinus Torvalds 			argp->pgbase += resp->count;
16301da177e4SLinus Torvalds 			argp->count -= resp->count;
16311da177e4SLinus Torvalds 		} else {
16321da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
16331da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
16341da177e4SLinus Torvalds 			 */
16351da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
16361da177e4SLinus Torvalds 		}
16378c9cb714STrond Myklebust 		resp->count = 0;
16388c9cb714STrond Myklebust 		resp->verf->committed = 0;
1639d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
16401da177e4SLinus Torvalds 	}
16411da177e4SLinus Torvalds }
16421da177e4SLinus Torvalds 
1643af7cf057STrond Myklebust static int wait_on_commit(struct nfs_mds_commit_info *cinfo)
164471d0a611STrond Myklebust {
1645723c921eSPeter Zijlstra 	return wait_var_event_killable(&cinfo->rpcs_out,
1646723c921eSPeter Zijlstra 				       !atomic_read(&cinfo->rpcs_out));
1647af7cf057STrond Myklebust }
1648af7cf057STrond Myklebust 
164917f46b80SJosef Bacik void nfs_commit_begin(struct nfs_mds_commit_info *cinfo)
1650af7cf057STrond Myklebust {
1651af7cf057STrond Myklebust 	atomic_inc(&cinfo->rpcs_out);
1652af7cf057STrond Myklebust }
1653af7cf057STrond Myklebust 
1654133a48abSTrond Myklebust bool nfs_commit_end(struct nfs_mds_commit_info *cinfo)
1655af7cf057STrond Myklebust {
1656133a48abSTrond Myklebust 	if (atomic_dec_and_test(&cinfo->rpcs_out)) {
1657723c921eSPeter Zijlstra 		wake_up_var(&cinfo->rpcs_out);
1658133a48abSTrond Myklebust 		return true;
1659133a48abSTrond Myklebust 	}
1660133a48abSTrond Myklebust 	return false;
166171d0a611STrond Myklebust }
166271d0a611STrond Myklebust 
16630b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
16641da177e4SLinus Torvalds {
16650b7c0153SFred Isaman 	put_nfs_open_context(data->context);
16660b7c0153SFred Isaman 	nfs_commit_free(data);
16671da177e4SLinus Torvalds }
1668e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
16691da177e4SLinus Torvalds 
16700b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
1671c36aae9aSPeng Tao 			const struct nfs_rpc_ops *nfs_ops,
16729ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
16739f0ec176SAndy Adamson 			int how, int flags)
16741da177e4SLinus Torvalds {
167507737691STrond Myklebust 	struct rpc_task *task;
16769ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1677bdc7f021STrond Myklebust 	struct rpc_message msg = {
1678bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1679bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
16809ace33cdSFred Isaman 		.rpc_cred = data->cred,
1681bdc7f021STrond Myklebust 	};
168284115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
168307737691STrond Myklebust 		.task = &data->task,
16849ace33cdSFred Isaman 		.rpc_client = clnt,
1685bdc7f021STrond Myklebust 		.rpc_message = &msg,
16869ace33cdSFred Isaman 		.callback_ops = call_ops,
168784115e1cSTrond Myklebust 		.callback_data = data,
1688101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
16894fa7ef69STrond Myklebust 		.flags = RPC_TASK_ASYNC | flags,
16903ff7576dSTrond Myklebust 		.priority = priority,
169184115e1cSTrond Myklebust 	};
1692118f09edSOlga Kornievskaia 
1693118f09edSOlga Kornievskaia 	if (nfs_server_capable(data->inode, NFS_CAP_MOVEABLE))
1694118f09edSOlga Kornievskaia 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
1695118f09edSOlga Kornievskaia 
1696788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
1697e9ae1ee2SAnna Schumaker 	nfs_ops->commit_setup(data, &msg, &task_setup_data.rpc_client);
16988224b273SChuck Lever 	trace_nfs_initiate_commit(data);
16991da177e4SLinus Torvalds 
1700b4839ebeSKinglong Mee 	dprintk("NFS: initiated commit call\n");
1701bdc7f021STrond Myklebust 
170207737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1703dbae4c73STrond Myklebust 	if (IS_ERR(task))
1704dbae4c73STrond Myklebust 		return PTR_ERR(task);
1705d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1706d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
170707737691STrond Myklebust 	rpc_put_task(task);
1708dbae4c73STrond Myklebust 	return 0;
17091da177e4SLinus Torvalds }
1710e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
17111da177e4SLinus Torvalds 
1712378520b8SPeng Tao static loff_t nfs_get_lwb(struct list_head *head)
1713378520b8SPeng Tao {
1714378520b8SPeng Tao 	loff_t lwb = 0;
1715378520b8SPeng Tao 	struct nfs_page *req;
1716378520b8SPeng Tao 
1717378520b8SPeng Tao 	list_for_each_entry(req, head, wb_list)
1718378520b8SPeng Tao 		if (lwb < (req_offset(req) + req->wb_bytes))
1719378520b8SPeng Tao 			lwb = req_offset(req) + req->wb_bytes;
1720378520b8SPeng Tao 
1721378520b8SPeng Tao 	return lwb;
1722378520b8SPeng Tao }
1723378520b8SPeng Tao 
17241da177e4SLinus Torvalds /*
17259ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
17269ace33cdSFred Isaman  */
17270b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1728988b6dceSFred Isaman 		     struct list_head *head,
1729f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1730f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
17319ace33cdSFred Isaman {
173219573c93STrond Myklebust 	struct nfs_page *first;
173319573c93STrond Myklebust 	struct nfs_open_context *ctx;
173419573c93STrond Myklebust 	struct inode *inode;
17359ace33cdSFred Isaman 
17369ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
17379ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
17389ace33cdSFred Isaman 
173919573c93STrond Myklebust 	if (head)
17409ace33cdSFred Isaman 		list_splice_init(head, &data->pages);
17419ace33cdSFred Isaman 
174219573c93STrond Myklebust 	first = nfs_list_entry(data->pages.next);
174319573c93STrond Myklebust 	ctx = nfs_req_openctx(first);
174419573c93STrond Myklebust 	inode = d_inode(ctx->dentry);
174519573c93STrond Myklebust 
17469ace33cdSFred Isaman 	data->inode	  = inode;
17479fcd5960STrond Myklebust 	data->cred	  = ctx->cred;
1748988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
1749378520b8SPeng Tao 	/* only set lwb for pnfs commit */
1750378520b8SPeng Tao 	if (lseg)
1751378520b8SPeng Tao 		data->lwb = nfs_get_lwb(&data->pages);
17529ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1753f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1754b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
17559ace33cdSFred Isaman 
17569ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
17579ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
17589ace33cdSFred Isaman 	data->args.offset = 0;
17599ace33cdSFred Isaman 	data->args.count  = 0;
17609fcd5960STrond Myklebust 	data->context     = get_nfs_open_context(ctx);
17619ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
17629ace33cdSFred Isaman 	data->res.verf    = &data->verf;
17639ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
1764133a48abSTrond Myklebust 	nfs_commit_begin(cinfo->mds);
17659ace33cdSFred Isaman }
1766e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
17679ace33cdSFred Isaman 
1768e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1769ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1770b57ff130SWeston Andros Adamson 		      struct nfs_commit_info *cinfo,
1771b57ff130SWeston Andros Adamson 		      u32 ds_commit_idx)
177264bfeb49SFred Isaman {
177364bfeb49SFred Isaman 	struct nfs_page *req;
177464bfeb49SFred Isaman 
177564bfeb49SFred Isaman 	while (!list_empty(page_list)) {
177664bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
177764bfeb49SFred Isaman 		nfs_list_remove_request(req);
1778b57ff130SWeston Andros Adamson 		nfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx);
17790c493b5cSTrond Myklebust 		nfs_folio_clear_commit(nfs_page_to_folio(req));
17801d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
178164bfeb49SFred Isaman 	}
178264bfeb49SFred Isaman }
1783e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
178464bfeb49SFred Isaman 
17850c493b5cSTrond Myklebust static void nfs_commit_resched_write(struct nfs_commit_info *cinfo,
1786b20135d0STrond Myklebust 				     struct nfs_page *req)
1787b20135d0STrond Myklebust {
17880c493b5cSTrond Myklebust 	struct folio *folio = nfs_page_to_folio(req);
17890c493b5cSTrond Myklebust 
17900c493b5cSTrond Myklebust 	filemap_dirty_folio(folio_mapping(folio), folio);
1791b20135d0STrond Myklebust }
1792b20135d0STrond Myklebust 
17939ace33cdSFred Isaman /*
17941da177e4SLinus Torvalds  * Commit dirty pages
17951da177e4SLinus Torvalds  */
17961da177e4SLinus Torvalds static int
1797ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1798ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
17991da177e4SLinus Torvalds {
18000b7c0153SFred Isaman 	struct nfs_commit_data	*data;
180185e39feeSOlga Kornievskaia 	unsigned short task_flags = 0;
18021da177e4SLinus Torvalds 
1803ade8febdSWeston Andros Adamson 	/* another commit raced with us */
1804ade8febdSWeston Andros Adamson 	if (list_empty(head))
1805ade8febdSWeston Andros Adamson 		return 0;
1806ade8febdSWeston Andros Adamson 
1807515dcdcdSTrond Myklebust 	data = nfs_commitdata_alloc();
1808515dcdcdSTrond Myklebust 	if (!data) {
1809515dcdcdSTrond Myklebust 		nfs_retry_commit(head, NULL, cinfo, -1);
1810515dcdcdSTrond Myklebust 		return -ENOMEM;
1811515dcdcdSTrond Myklebust 	}
18121da177e4SLinus Torvalds 
18131da177e4SLinus Torvalds 	/* Set up the argument struct */
1814f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
181585e39feeSOlga Kornievskaia 	if (NFS_SERVER(inode)->nfs_client->cl_minorversion)
181685e39feeSOlga Kornievskaia 		task_flags = RPC_TASK_MOVEABLE;
1817c36aae9aSPeng Tao 	return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode),
181885e39feeSOlga Kornievskaia 				   data->mds_ops, how,
181985e39feeSOlga Kornievskaia 				   RPC_TASK_CRED_NOREF | task_flags);
18201da177e4SLinus Torvalds }
18211da177e4SLinus Torvalds 
18221da177e4SLinus Torvalds /*
18231da177e4SLinus Torvalds  * COMMIT call returned
18241da177e4SLinus Torvalds  */
1825788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
18261da177e4SLinus Torvalds {
18270b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
18281da177e4SLinus Torvalds 
1829788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1830c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
18317bdd297eSTrond Myklebust 	trace_nfs_commit_done(task, data);
1832c9d8f89dSTrond Myklebust }
1833c9d8f89dSTrond Myklebust 
1834f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1835c9d8f89dSTrond Myklebust {
1836221203ceSTrond Myklebust 	const struct nfs_writeverf *verf = data->res.verf;
1837c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1838c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1839f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
1840353db796SNeilBrown 	struct nfs_server *nfss;
18410c493b5cSTrond Myklebust 	struct folio *folio;
1842788e7a89STrond Myklebust 
18431da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
18441da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
18451da177e4SLinus Torvalds 		nfs_list_remove_request(req);
18460c493b5cSTrond Myklebust 		folio = nfs_page_to_folio(req);
18470c493b5cSTrond Myklebust 		nfs_folio_clear_commit(folio);
18481da177e4SLinus Torvalds 
18491e8968c5SNiels de Vos 		dprintk("NFS:       commit (%s/%llu %d@%lld)",
18509fcd5960STrond Myklebust 			nfs_req_openctx(req)->dentry->d_sb->s_id,
18519fcd5960STrond Myklebust 			(unsigned long long)NFS_FILEID(d_inode(nfs_req_openctx(req)->dentry)),
18521da177e4SLinus Torvalds 			req->wb_bytes,
18531da177e4SLinus Torvalds 			(long long)req_offset(req));
1854c9d8f89dSTrond Myklebust 		if (status < 0) {
18550c493b5cSTrond Myklebust 			if (folio) {
1856af887e43STrond Myklebust 				trace_nfs_commit_error(data->inode, req,
1857af887e43STrond Myklebust 						       status);
18580c493b5cSTrond Myklebust 				nfs_mapping_set_error(folio, status);
18591da177e4SLinus Torvalds 				nfs_inode_remove_request(req);
18606fbda89bSTrond Myklebust 			}
1861ddeaa637SJoe Perches 			dprintk_cont(", error = %d\n", status);
18621da177e4SLinus Torvalds 			goto next;
18631da177e4SLinus Torvalds 		}
18641da177e4SLinus Torvalds 
18651da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
18661da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
18671f28476dSTrond Myklebust 		if (nfs_write_match_verf(verf, req)) {
18681da177e4SLinus Torvalds 			/* We have a match */
18690c493b5cSTrond Myklebust 			if (folio)
18701da177e4SLinus Torvalds 				nfs_inode_remove_request(req);
1871ddeaa637SJoe Perches 			dprintk_cont(" OK\n");
18721da177e4SLinus Torvalds 			goto next;
18731da177e4SLinus Torvalds 		}
18741da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
1875ddeaa637SJoe Perches 		dprintk_cont(" mismatch\n");
18766d884e8fSFred 		nfs_mark_request_dirty(req);
187767f4b5dcSTrond Myklebust 		atomic_long_inc(&NFS_I(data->inode)->redirtied_pages);
18781da177e4SLinus Torvalds 	next:
18791d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
18807f1bda44STrond Myklebust 		/* Latency breaker */
18817f1bda44STrond Myklebust 		cond_resched();
18821da177e4SLinus Torvalds 	}
1883353db796SNeilBrown 	nfss = NFS_SERVER(data->inode);
1884353db796SNeilBrown 	if (atomic_long_read(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
18856df25e58SNeilBrown 		nfss->write_congested = 0;
1886353db796SNeilBrown 
1887f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1888af7cf057STrond Myklebust 	nfs_commit_end(cinfo.mds);
18895917ce84SFred Isaman }
18905917ce84SFred Isaman 
18915917ce84SFred Isaman static void nfs_commit_release(void *calldata)
18925917ce84SFred Isaman {
18930b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
18945917ce84SFred Isaman 
1895f453a54aSFred Isaman 	data->completion_ops->completion(data);
1896c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
18971da177e4SLinus Torvalds }
1898788e7a89STrond Myklebust 
1899788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
19000b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1901788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1902788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1903788e7a89STrond Myklebust };
19041da177e4SLinus Torvalds 
1905f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1906f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1907b20135d0STrond Myklebust 	.resched_write = nfs_commit_resched_write,
1908f453a54aSFred Isaman };
1909f453a54aSFred Isaman 
19101763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1911ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
191284c53ab5SFred Isaman {
191384c53ab5SFred Isaman 	int status;
191484c53ab5SFred Isaman 
1915ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
191684c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1917ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
191884c53ab5SFred Isaman 	return status;
191984c53ab5SFred Isaman }
192084c53ab5SFred Isaman 
1921c4f24df9STrond Myklebust static int __nfs_commit_inode(struct inode *inode, int how,
1922c4f24df9STrond Myklebust 		struct writeback_control *wbc)
19231da177e4SLinus Torvalds {
19241da177e4SLinus Torvalds 	LIST_HEAD(head);
1925ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
192671d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1927c4f24df9STrond Myklebust 	int ret, nscan;
19281da177e4SLinus Torvalds 
192964a93dbfSTrond Myklebust 	how &= ~FLUSH_SYNC;
1930ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1931af7cf057STrond Myklebust 	nfs_commit_begin(cinfo.mds);
1932c4f24df9STrond Myklebust 	for (;;) {
1933c4f24df9STrond Myklebust 		ret = nscan = nfs_scan_commit(inode, &head, &cinfo);
1934c4f24df9STrond Myklebust 		if (ret <= 0)
1935c4f24df9STrond Myklebust 			break;
1936c4f24df9STrond Myklebust 		ret = nfs_generic_commit_list(inode, &head, how, &cinfo);
1937c4f24df9STrond Myklebust 		if (ret < 0)
1938c4f24df9STrond Myklebust 			break;
1939c4f24df9STrond Myklebust 		ret = 0;
1940c4f24df9STrond Myklebust 		if (wbc && wbc->sync_mode == WB_SYNC_NONE) {
1941c4f24df9STrond Myklebust 			if (nscan < wbc->nr_to_write)
1942c4f24df9STrond Myklebust 				wbc->nr_to_write -= nscan;
1943c4f24df9STrond Myklebust 			else
1944c4f24df9STrond Myklebust 				wbc->nr_to_write = 0;
1945c4f24df9STrond Myklebust 		}
1946c4f24df9STrond Myklebust 		if (nscan < INT_MAX)
1947c4f24df9STrond Myklebust 			break;
1948c4f24df9STrond Myklebust 		cond_resched();
1949c4f24df9STrond Myklebust 	}
1950af7cf057STrond Myklebust 	nfs_commit_end(cinfo.mds);
1951c4f24df9STrond Myklebust 	if (ret || !may_wait)
1952c4f24df9STrond Myklebust 		return ret;
1953c4f24df9STrond Myklebust 	return wait_on_commit(cinfo.mds);
1954c4f24df9STrond Myklebust }
1955c4f24df9STrond Myklebust 
1956c4f24df9STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
1957c4f24df9STrond Myklebust {
1958c4f24df9STrond Myklebust 	return __nfs_commit_inode(inode, how, NULL);
19591da177e4SLinus Torvalds }
1960b20135d0STrond Myklebust EXPORT_SYMBOL_GPL(nfs_commit_inode);
19618fc795f7STrond Myklebust 
1962ae09c31fSAnna Schumaker int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
19638fc795f7STrond Myklebust {
1964420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1965420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1966420e3646STrond Myklebust 	int ret = 0;
19678fc795f7STrond Myklebust 
1968c4f24df9STrond Myklebust 	if (wbc->sync_mode == WB_SYNC_NONE) {
19693236c3e1SJeff Layton 		/* no commits means nothing needs to be done */
19705cb953d4STrond Myklebust 		if (!atomic_long_read(&nfsi->commit_info.ncommit))
1971c4f24df9STrond Myklebust 			goto check_requests_outstanding;
19723236c3e1SJeff Layton 
1973a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1974a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1975420e3646STrond Myklebust 		 */
19761a4edf0fSTrond Myklebust 		if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1977420e3646STrond Myklebust 			goto out_mark_dirty;
1978420e3646STrond Myklebust 
1979a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1980420e3646STrond Myklebust 		flags = 0;
1981a00dd6c0SJeff Layton 	}
1982a00dd6c0SJeff Layton 
1983c4f24df9STrond Myklebust 	ret = __nfs_commit_inode(inode, flags, wbc);
1984c4f24df9STrond Myklebust 	if (!ret) {
1985c4f24df9STrond Myklebust 		if (flags & FLUSH_SYNC)
19868fc795f7STrond Myklebust 			return 0;
1987c4f24df9STrond Myklebust 	} else if (atomic_long_read(&nfsi->commit_info.ncommit))
1988c4f24df9STrond Myklebust 		goto out_mark_dirty;
1989c4f24df9STrond Myklebust 
1990c4f24df9STrond Myklebust check_requests_outstanding:
1991c4f24df9STrond Myklebust 	if (!atomic_read(&nfsi->commit_info.rpcs_out))
1992c4f24df9STrond Myklebust 		return ret;
1993420e3646STrond Myklebust out_mark_dirty:
19948fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
19958fc795f7STrond Myklebust 	return ret;
19968fc795f7STrond Myklebust }
199789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode);
1998863a3c6cSAndy Adamson 
1999acdc53b2STrond Myklebust /*
2000837bb1d7STrond Myklebust  * Wrapper for filemap_write_and_wait_range()
2001837bb1d7STrond Myklebust  *
2002837bb1d7STrond Myklebust  * Needed for pNFS in order to ensure data becomes visible to the
2003837bb1d7STrond Myklebust  * client.
2004837bb1d7STrond Myklebust  */
2005837bb1d7STrond Myklebust int nfs_filemap_write_and_wait_range(struct address_space *mapping,
2006837bb1d7STrond Myklebust 		loff_t lstart, loff_t lend)
2007837bb1d7STrond Myklebust {
2008837bb1d7STrond Myklebust 	int ret;
2009837bb1d7STrond Myklebust 
2010837bb1d7STrond Myklebust 	ret = filemap_write_and_wait_range(mapping, lstart, lend);
2011837bb1d7STrond Myklebust 	if (ret == 0)
2012837bb1d7STrond Myklebust 		ret = pnfs_sync_inode(mapping->host, true);
2013837bb1d7STrond Myklebust 	return ret;
2014837bb1d7STrond Myklebust }
2015837bb1d7STrond Myklebust EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range);
2016837bb1d7STrond Myklebust 
2017837bb1d7STrond Myklebust /*
2018acdc53b2STrond Myklebust  * flush the inode to disk.
2019acdc53b2STrond Myklebust  */
2020acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
202134901f70STrond Myklebust {
2022f4ce1299STrond Myklebust 	int ret;
202334901f70STrond Myklebust 
2024f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_enter(inode);
2025f4ce1299STrond Myklebust 
20265bb89b47STrond Myklebust 	ret = filemap_write_and_wait(inode->i_mapping);
20276b196875SChuck Lever 	if (ret)
20286b196875SChuck Lever 		goto out;
20295bb89b47STrond Myklebust 	ret = nfs_commit_inode(inode, FLUSH_SYNC);
20306b196875SChuck Lever 	if (ret < 0)
20316b196875SChuck Lever 		goto out;
20325bb89b47STrond Myklebust 	pnfs_sync_inode(inode, true);
20336b196875SChuck Lever 	ret = 0;
2034f4ce1299STrond Myklebust 
20356b196875SChuck Lever out:
2036f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_exit(inode, ret);
2037f4ce1299STrond Myklebust 	return ret;
20381c75950bSTrond Myklebust }
2039ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all);
20401c75950bSTrond Myklebust 
20416d740c76SMatthew Wilcox (Oracle) int nfs_wb_folio_cancel(struct inode *inode, struct folio *folio)
20421b3b4a1aSTrond Myklebust {
20431b3b4a1aSTrond Myklebust 	struct nfs_page *req;
20441b3b4a1aSTrond Myklebust 	int ret = 0;
20451b3b4a1aSTrond Myklebust 
20466d740c76SMatthew Wilcox (Oracle) 	folio_wait_writeback(folio);
20473e217045SWeston Andros Adamson 
20483e217045SWeston Andros Adamson 	/* blocking call to cancel all requests and join to a single (head)
20493e217045SWeston Andros Adamson 	 * request */
20500c493b5cSTrond Myklebust 	req = nfs_lock_and_join_requests(folio);
20513e217045SWeston Andros Adamson 
20523e217045SWeston Andros Adamson 	if (IS_ERR(req)) {
20533e217045SWeston Andros Adamson 		ret = PTR_ERR(req);
20543e217045SWeston Andros Adamson 	} else if (req) {
20556d740c76SMatthew Wilcox (Oracle) 		/* all requests from this folio have been cancelled by
20563e217045SWeston Andros Adamson 		 * nfs_lock_and_join_requests, so just remove the head
20573e217045SWeston Andros Adamson 		 * request from the inode / page_private pointer and
20583e217045SWeston Andros Adamson 		 * release it */
20591b3b4a1aSTrond Myklebust 		nfs_inode_remove_request(req);
20601d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
20611b3b4a1aSTrond Myklebust 	}
20623e217045SWeston Andros Adamson 
20631b3b4a1aSTrond Myklebust 	return ret;
20641b3b4a1aSTrond Myklebust }
20651b3b4a1aSTrond Myklebust 
20665241060eSTrond Myklebust /**
20675241060eSTrond Myklebust  * nfs_wb_folio - Write back all requests on one page
20685241060eSTrond Myklebust  * @inode: pointer to page
20695241060eSTrond Myklebust  * @folio: pointer to folio
20705241060eSTrond Myklebust  *
20715241060eSTrond Myklebust  * Assumes that the folio has been locked by the caller, and will
20725241060eSTrond Myklebust  * not unlock it.
20731c75950bSTrond Myklebust  */
20745241060eSTrond Myklebust int nfs_wb_folio(struct inode *inode, struct folio *folio)
20751c75950bSTrond Myklebust {
20765241060eSTrond Myklebust 	loff_t range_start = folio_file_pos(folio);
20775241060eSTrond Myklebust 	loff_t range_end = range_start + (loff_t)folio_size(folio) - 1;
20787f2f12d9STrond Myklebust 	struct writeback_control wbc = {
20797f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
20807f2f12d9STrond Myklebust 		.nr_to_write = 0,
20817f2f12d9STrond Myklebust 		.range_start = range_start,
20827f2f12d9STrond Myklebust 		.range_end = range_end,
20837f2f12d9STrond Myklebust 	};
20847f2f12d9STrond Myklebust 	int ret;
20857f2f12d9STrond Myklebust 
2086256093feSTrond Myklebust 	trace_nfs_writeback_folio(inode, folio);
2087f4ce1299STrond Myklebust 
20880522f6adSTrond Myklebust 	for (;;) {
20895241060eSTrond Myklebust 		folio_wait_writeback(folio);
20905241060eSTrond Myklebust 		if (folio_clear_dirty_for_io(folio)) {
20910c493b5cSTrond Myklebust 			ret = nfs_writepage_locked(folio, &wbc);
20927f2f12d9STrond Myklebust 			if (ret < 0)
20937f2f12d9STrond Myklebust 				goto out_error;
20940522f6adSTrond Myklebust 			continue;
20957f2f12d9STrond Myklebust 		}
2096f4ce1299STrond Myklebust 		ret = 0;
20975241060eSTrond Myklebust 		if (!folio_test_private(folio))
20980522f6adSTrond Myklebust 			break;
20990522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
21007f2f12d9STrond Myklebust 		if (ret < 0)
21017f2f12d9STrond Myklebust 			goto out_error;
21027f2f12d9STrond Myklebust 	}
21037f2f12d9STrond Myklebust out_error:
2104256093feSTrond Myklebust 	trace_nfs_writeback_folio_done(inode, folio, ret);
21057f2f12d9STrond Myklebust 	return ret;
21061c75950bSTrond Myklebust }
21071c75950bSTrond Myklebust 
2108074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
21094ae84a80SMatthew Wilcox (Oracle) int nfs_migrate_folio(struct address_space *mapping, struct folio *dst,
21104ae84a80SMatthew Wilcox (Oracle) 		struct folio *src, enum migrate_mode mode)
2111074cc1deSTrond Myklebust {
21122da95652SJeff Layton 	/*
21134ae84a80SMatthew Wilcox (Oracle) 	 * If the private flag is set, the folio is currently associated with
21142da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
21152da95652SJeff Layton 	 *
21162da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
21172da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
21184ae84a80SMatthew Wilcox (Oracle) 	 *        the folio lock.
21192da95652SJeff Layton 	 */
21204ae84a80SMatthew Wilcox (Oracle) 	if (folio_test_private(src))
21212da95652SJeff Layton 		return -EBUSY;
2122074cc1deSTrond Myklebust 
21232e9d7e4bSDavid Howells 	if (folio_test_private_2(src)) { /* [DEPRECATED] */
212416f2f4e6SDavid Howells 		if (mode == MIGRATE_ASYNC)
21258c209ce7SDavid Howells 			return -EBUSY;
21262e9d7e4bSDavid Howells 		folio_wait_private_2(src);
212716f2f4e6SDavid Howells 	}
2128074cc1deSTrond Myklebust 
212954184650SMatthew Wilcox (Oracle) 	return migrate_folio(mapping, dst, src, mode);
2130074cc1deSTrond Myklebust }
2131074cc1deSTrond Myklebust #endif
2132074cc1deSTrond Myklebust 
2133f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
21341da177e4SLinus Torvalds {
21351da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
21361e7f3a48SWeston Andros Adamson 					     sizeof(struct nfs_pgio_header),
21371da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
213820c2df83SPaul Mundt 					     NULL);
21391da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
21401da177e4SLinus Torvalds 		return -ENOMEM;
21411da177e4SLinus Torvalds 
214293d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
21431da177e4SLinus Torvalds 						     nfs_wdata_cachep);
21441da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
21453dd4765fSJeff Layton 		goto out_destroy_write_cache;
21461da177e4SLinus Torvalds 
21470b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
21480b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
21490b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
21500b7c0153SFred Isaman 					     NULL);
21510b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
21523dd4765fSJeff Layton 		goto out_destroy_write_mempool;
21530b7c0153SFred Isaman 
215493d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
21554c100210SYanchuan Nian 						      nfs_cdata_cachep);
21561da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
21573dd4765fSJeff Layton 		goto out_destroy_commit_cache;
21581da177e4SLinus Torvalds 
215989a09141SPeter Zijlstra 	/*
216089a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
216189a09141SPeter Zijlstra 	 *
216289a09141SPeter Zijlstra 	 *  64MB:    8192k
216389a09141SPeter Zijlstra 	 * 128MB:   11585k
216489a09141SPeter Zijlstra 	 * 256MB:   16384k
216589a09141SPeter Zijlstra 	 * 512MB:   23170k
216689a09141SPeter Zijlstra 	 *   1GB:   32768k
216789a09141SPeter Zijlstra 	 *   2GB:   46340k
216889a09141SPeter Zijlstra 	 *   4GB:   65536k
216989a09141SPeter Zijlstra 	 *   8GB:   92681k
217089a09141SPeter Zijlstra 	 *  16GB:  131072k
217189a09141SPeter Zijlstra 	 *
217289a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
217389a09141SPeter Zijlstra 	 * Limit the default to 256M
217489a09141SPeter Zijlstra 	 */
2175ca79b0c2SArun KS 	nfs_congestion_kb = (16*int_sqrt(totalram_pages())) << (PAGE_SHIFT-10);
217689a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
217789a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
217889a09141SPeter Zijlstra 
21791da177e4SLinus Torvalds 	return 0;
21803dd4765fSJeff Layton 
21813dd4765fSJeff Layton out_destroy_commit_cache:
21823dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
21833dd4765fSJeff Layton out_destroy_write_mempool:
21843dd4765fSJeff Layton 	mempool_destroy(nfs_wdata_mempool);
21853dd4765fSJeff Layton out_destroy_write_cache:
21863dd4765fSJeff Layton 	kmem_cache_destroy(nfs_wdata_cachep);
21873dd4765fSJeff Layton 	return -ENOMEM;
21881da177e4SLinus Torvalds }
21891da177e4SLinus Torvalds 
2190266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
21911da177e4SLinus Torvalds {
21921da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
21933dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
21941da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
21951a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
21961da177e4SLinus Torvalds }
21971da177e4SLinus Torvalds 
21984a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops = {
21994a0de55cSAnna Schumaker 	.rw_alloc_header	= nfs_writehdr_alloc,
22004a0de55cSAnna Schumaker 	.rw_free_header		= nfs_writehdr_free,
22010eecb214SAnna Schumaker 	.rw_done		= nfs_writeback_done,
22020eecb214SAnna Schumaker 	.rw_result		= nfs_writeback_result,
22031ed26f33SAnna Schumaker 	.rw_initiate		= nfs_initiate_write,
22044a0de55cSAnna Schumaker };
2205