xref: /openbmc/linux/fs/nfs/write.c (revision 5241060e)
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>
283fcfab16SAndrew Morton 
297c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
30875bc3fbSTrond Myklebust #include <linux/sched/mm.h>
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #include "delegation.h"
3349a70f27STrond Myklebust #include "internal.h"
3491d5b470SChuck Lever #include "iostat.h"
35def6ed7eSAndy Adamson #include "nfs4_fs.h"
36074cc1deSTrond Myklebust #include "fscache.h"
3794ad1c80SFred Isaman #include "pnfs.h"
381da177e4SLinus Torvalds 
39f4ce1299STrond Myklebust #include "nfstrace.h"
40f4ce1299STrond Myklebust 
411da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #define MIN_POOL_WRITE		(32)
441da177e4SLinus Torvalds #define MIN_POOL_COMMIT		(4)
451da177e4SLinus Torvalds 
46919e3bd9STrond Myklebust struct nfs_io_completion {
47919e3bd9STrond Myklebust 	void (*complete)(void *data);
48919e3bd9STrond Myklebust 	void *data;
49919e3bd9STrond Myklebust 	struct kref refcount;
50919e3bd9STrond Myklebust };
51919e3bd9STrond Myklebust 
521da177e4SLinus Torvalds /*
531da177e4SLinus Torvalds  * Local function declarations
541da177e4SLinus Torvalds  */
55f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req);
56788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops;
57061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops;
58f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops;
594a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops;
6006c9fdf3STrond Myklebust static void nfs_inode_remove_request(struct nfs_page *req);
61d4581383SWeston Andros Adamson static void nfs_clear_request_commit(struct nfs_page *req);
6202d1426cSWeston Andros Adamson static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
6302d1426cSWeston Andros Adamson 				      struct inode *inode);
643a3908c8STrond Myklebust static struct nfs_page *
653a3908c8STrond Myklebust nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
663a3908c8STrond Myklebust 						struct page *page);
671da177e4SLinus Torvalds 
68e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep;
693feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
700b7c0153SFred Isaman static struct kmem_cache *nfs_cdata_cachep;
711da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
721da177e4SLinus Torvalds 
73515dcdcdSTrond Myklebust struct nfs_commit_data *nfs_commitdata_alloc(void)
741da177e4SLinus Torvalds {
75518662e0SNeilBrown 	struct nfs_commit_data *p;
7640859d7eSChuck Lever 
77515dcdcdSTrond Myklebust 	p = kmem_cache_zalloc(nfs_cdata_cachep, nfs_io_gfp_mask());
78515dcdcdSTrond Myklebust 	if (!p) {
79518662e0SNeilBrown 		p = mempool_alloc(nfs_commit_mempool, GFP_NOWAIT);
80518662e0SNeilBrown 		if (!p)
81518662e0SNeilBrown 			return NULL;
821da177e4SLinus Torvalds 		memset(p, 0, sizeof(*p));
83515dcdcdSTrond Myklebust 	}
841da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->pages);
851da177e4SLinus Torvalds 	return p;
861da177e4SLinus Torvalds }
87e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
881da177e4SLinus Torvalds 
890b7c0153SFred Isaman void nfs_commit_free(struct nfs_commit_data *p)
901da177e4SLinus Torvalds {
911da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
921da177e4SLinus Torvalds }
93e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free);
941da177e4SLinus Torvalds 
951e7f3a48SWeston Andros Adamson static struct nfs_pgio_header *nfs_writehdr_alloc(void)
963feb2d49STrond Myklebust {
970bae835bSTrond Myklebust 	struct nfs_pgio_header *p;
983feb2d49STrond Myklebust 
990bae835bSTrond Myklebust 	p = kmem_cache_zalloc(nfs_wdata_cachep, nfs_io_gfp_mask());
1000bae835bSTrond Myklebust 	if (!p) {
1010bae835bSTrond Myklebust 		p = mempool_alloc(nfs_wdata_mempool, GFP_NOWAIT);
1020bae835bSTrond Myklebust 		if (!p)
1030bae835bSTrond Myklebust 			return NULL;
1043feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
1050bae835bSTrond Myklebust 	}
106fbe77c30SBenjamin Coddington 	p->rw_mode = FMODE_WRITE;
1073feb2d49STrond Myklebust 	return p;
1083feb2d49STrond Myklebust }
1093feb2d49STrond Myklebust 
1101e7f3a48SWeston Andros Adamson static void nfs_writehdr_free(struct nfs_pgio_header *hdr)
1116c75dc0dSFred Isaman {
1121e7f3a48SWeston Andros Adamson 	mempool_free(hdr, nfs_wdata_mempool);
1133feb2d49STrond Myklebust }
1141da177e4SLinus Torvalds 
115919e3bd9STrond Myklebust static struct nfs_io_completion *nfs_io_completion_alloc(gfp_t gfp_flags)
116919e3bd9STrond Myklebust {
117919e3bd9STrond Myklebust 	return kmalloc(sizeof(struct nfs_io_completion), gfp_flags);
118919e3bd9STrond Myklebust }
119919e3bd9STrond Myklebust 
120919e3bd9STrond Myklebust static void nfs_io_completion_init(struct nfs_io_completion *ioc,
121919e3bd9STrond Myklebust 		void (*complete)(void *), void *data)
122919e3bd9STrond Myklebust {
123919e3bd9STrond Myklebust 	ioc->complete = complete;
124919e3bd9STrond Myklebust 	ioc->data = data;
125919e3bd9STrond Myklebust 	kref_init(&ioc->refcount);
126919e3bd9STrond Myklebust }
127919e3bd9STrond Myklebust 
128919e3bd9STrond Myklebust static void nfs_io_completion_release(struct kref *kref)
129919e3bd9STrond Myklebust {
130919e3bd9STrond Myklebust 	struct nfs_io_completion *ioc = container_of(kref,
131919e3bd9STrond Myklebust 			struct nfs_io_completion, refcount);
132919e3bd9STrond Myklebust 	ioc->complete(ioc->data);
133919e3bd9STrond Myklebust 	kfree(ioc);
134919e3bd9STrond Myklebust }
135919e3bd9STrond Myklebust 
136919e3bd9STrond Myklebust static void nfs_io_completion_get(struct nfs_io_completion *ioc)
137919e3bd9STrond Myklebust {
138919e3bd9STrond Myklebust 	if (ioc != NULL)
139919e3bd9STrond Myklebust 		kref_get(&ioc->refcount);
140919e3bd9STrond Myklebust }
141919e3bd9STrond Myklebust 
142919e3bd9STrond Myklebust static void nfs_io_completion_put(struct nfs_io_completion *ioc)
143919e3bd9STrond Myklebust {
144919e3bd9STrond Myklebust 	if (ioc != NULL)
145919e3bd9STrond Myklebust 		kref_put(&ioc->refcount, nfs_io_completion_release);
146919e3bd9STrond Myklebust }
147919e3bd9STrond Myklebust 
148e00ed89dSTrond Myklebust static void
149e00ed89dSTrond Myklebust nfs_page_set_inode_ref(struct nfs_page *req, struct inode *inode)
150e00ed89dSTrond Myklebust {
151e00ed89dSTrond Myklebust 	if (!test_and_set_bit(PG_INODE_REF, &req->wb_flags)) {
152e00ed89dSTrond Myklebust 		kref_get(&req->wb_kref);
153e00ed89dSTrond Myklebust 		atomic_long_inc(&NFS_I(inode)->nrequests);
154e00ed89dSTrond Myklebust 	}
155e00ed89dSTrond Myklebust }
156e00ed89dSTrond Myklebust 
157e00ed89dSTrond Myklebust static int
158e00ed89dSTrond Myklebust nfs_cancel_remove_inode(struct nfs_page *req, struct inode *inode)
159e00ed89dSTrond Myklebust {
160e00ed89dSTrond Myklebust 	int ret;
161e00ed89dSTrond Myklebust 
162e00ed89dSTrond Myklebust 	if (!test_bit(PG_REMOVE, &req->wb_flags))
163e00ed89dSTrond Myklebust 		return 0;
164e00ed89dSTrond Myklebust 	ret = nfs_page_group_lock(req);
165e00ed89dSTrond Myklebust 	if (ret)
166e00ed89dSTrond Myklebust 		return ret;
167e00ed89dSTrond Myklebust 	if (test_and_clear_bit(PG_REMOVE, &req->wb_flags))
168e00ed89dSTrond Myklebust 		nfs_page_set_inode_ref(req, inode);
169e00ed89dSTrond Myklebust 	nfs_page_group_unlock(req);
170e00ed89dSTrond Myklebust 	return 0;
171e00ed89dSTrond Myklebust }
172e00ed89dSTrond Myklebust 
173bd37d6fcSTrond Myklebust static struct nfs_page *
174bd37d6fcSTrond Myklebust nfs_page_private_request(struct page *page)
175bd37d6fcSTrond Myklebust {
176bd37d6fcSTrond Myklebust 	if (!PagePrivate(page))
177bd37d6fcSTrond Myklebust 		return NULL;
178bd37d6fcSTrond Myklebust 	return (struct nfs_page *)page_private(page);
179bd37d6fcSTrond Myklebust }
180bd37d6fcSTrond Myklebust 
18184d3a9a9SWeston Andros Adamson /*
18284d3a9a9SWeston Andros Adamson  * nfs_page_find_head_request_locked - find head request associated with @page
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  */
18829418aa4SMel Gorman static struct nfs_page *
189b30d2f04STrond Myklebust nfs_page_find_private_request(struct page *page)
190277459d2STrond Myklebust {
1914b9bb25bSTrond Myklebust 	struct address_space *mapping = page_file_mapping(page);
192bd37d6fcSTrond Myklebust 	struct nfs_page *req;
193277459d2STrond Myklebust 
194b30d2f04STrond Myklebust 	if (!PagePrivate(page))
195b30d2f04STrond Myklebust 		return NULL;
1964b9bb25bSTrond Myklebust 	spin_lock(&mapping->private_lock);
197bd37d6fcSTrond Myklebust 	req = nfs_page_private_request(page);
19884d3a9a9SWeston Andros Adamson 	if (req) {
19984d3a9a9SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_head != req);
20029418aa4SMel Gorman 		kref_get(&req->wb_kref);
20184d3a9a9SWeston Andros Adamson 	}
2024b9bb25bSTrond Myklebust 	spin_unlock(&mapping->private_lock);
203b30d2f04STrond Myklebust 	return req;
204b30d2f04STrond Myklebust }
20529418aa4SMel Gorman 
206b30d2f04STrond Myklebust static struct nfs_page *
207b30d2f04STrond Myklebust nfs_page_find_swap_request(struct page *page)
208b30d2f04STrond Myklebust {
209b30d2f04STrond Myklebust 	struct inode *inode = page_file_mapping(page)->host;
210b30d2f04STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
211b30d2f04STrond Myklebust 	struct nfs_page *req = NULL;
212b30d2f04STrond Myklebust 	if (!PageSwapCache(page))
213b30d2f04STrond Myklebust 		return NULL;
214e824f99aSTrond Myklebust 	mutex_lock(&nfsi->commit_mutex);
215b30d2f04STrond Myklebust 	if (PageSwapCache(page)) {
216b30d2f04STrond Myklebust 		req = nfs_page_search_commits_for_head_request_locked(nfsi,
217b30d2f04STrond Myklebust 			page);
218b30d2f04STrond Myklebust 		if (req) {
219b30d2f04STrond Myklebust 			WARN_ON_ONCE(req->wb_head != req);
220b30d2f04STrond Myklebust 			kref_get(&req->wb_kref);
221b30d2f04STrond Myklebust 		}
222b30d2f04STrond Myklebust 	}
223e824f99aSTrond Myklebust 	mutex_unlock(&nfsi->commit_mutex);
224277459d2STrond Myklebust 	return req;
225277459d2STrond Myklebust }
226277459d2STrond Myklebust 
22784d3a9a9SWeston Andros Adamson /*
22884d3a9a9SWeston Andros Adamson  * nfs_page_find_head_request - find head request associated with @page
22984d3a9a9SWeston Andros Adamson  *
23084d3a9a9SWeston Andros Adamson  * returns matching head request with reference held, or NULL if not found.
23184d3a9a9SWeston Andros Adamson  */
23284d3a9a9SWeston Andros Adamson static struct nfs_page *nfs_page_find_head_request(struct page *page)
233277459d2STrond Myklebust {
234b30d2f04STrond Myklebust 	struct nfs_page *req;
235277459d2STrond Myklebust 
236b30d2f04STrond Myklebust 	req = nfs_page_find_private_request(page);
237b30d2f04STrond Myklebust 	if (!req)
238b30d2f04STrond Myklebust 		req = nfs_page_find_swap_request(page);
239277459d2STrond Myklebust 	return req;
240277459d2STrond Myklebust }
241277459d2STrond Myklebust 
242e00ed89dSTrond Myklebust static struct nfs_page *nfs_find_and_lock_page_request(struct page *page)
243e00ed89dSTrond Myklebust {
244e00ed89dSTrond Myklebust 	struct inode *inode = page_file_mapping(page)->host;
245e00ed89dSTrond Myklebust 	struct nfs_page *req, *head;
246e00ed89dSTrond Myklebust 	int ret;
247e00ed89dSTrond Myklebust 
248e00ed89dSTrond Myklebust 	for (;;) {
249e00ed89dSTrond Myklebust 		req = nfs_page_find_head_request(page);
250e00ed89dSTrond Myklebust 		if (!req)
251e00ed89dSTrond Myklebust 			return req;
252e00ed89dSTrond Myklebust 		head = nfs_page_group_lock_head(req);
253e00ed89dSTrond Myklebust 		if (head != req)
254e00ed89dSTrond Myklebust 			nfs_release_request(req);
255e00ed89dSTrond Myklebust 		if (IS_ERR(head))
256e00ed89dSTrond Myklebust 			return head;
257e00ed89dSTrond Myklebust 		ret = nfs_cancel_remove_inode(head, inode);
258e00ed89dSTrond Myklebust 		if (ret < 0) {
259e00ed89dSTrond Myklebust 			nfs_unlock_and_release_request(head);
260e00ed89dSTrond Myklebust 			return ERR_PTR(ret);
261e00ed89dSTrond Myklebust 		}
262e00ed89dSTrond Myklebust 		/* Ensure that nobody removed the request before we locked it */
263e00ed89dSTrond Myklebust 		if (head == nfs_page_private_request(page))
264e00ed89dSTrond Myklebust 			break;
265e00ed89dSTrond Myklebust 		if (PageSwapCache(page))
266e00ed89dSTrond Myklebust 			break;
267e00ed89dSTrond Myklebust 		nfs_unlock_and_release_request(head);
268e00ed89dSTrond Myklebust 	}
269e00ed89dSTrond Myklebust 	return head;
270e00ed89dSTrond Myklebust }
271e00ed89dSTrond Myklebust 
2721da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
2731da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
2741da177e4SLinus Torvalds {
275d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->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);
28109cbfeafSKirill A. Shutemov 	end_index = (i_size - 1) >> PAGE_SHIFT;
2828cd79788SHuang Ying 	if (i_size > 0 && page_index(page) < end_index)
283a3d01454STrond Myklebust 		goto out;
284d56b4ddfSMel Gorman 	end = page_file_offset(page) + ((loff_t)offset+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 
3106fbda89bSTrond Myklebust static void nfs_mapping_set_error(struct page *page, int error)
3116fbda89bSTrond Myklebust {
312b8946d7bSTrond Myklebust 	struct address_space *mapping = page_file_mapping(page);
313b8946d7bSTrond Myklebust 
3146fbda89bSTrond Myklebust 	SetPageError(page);
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 {
361d72ddcbaSWeston Andros Adamson 	struct nfs_page *tmp;
362d72ddcbaSWeston Andros Adamson 	unsigned int pos = 0;
363d72ddcbaSWeston Andros Adamson 	unsigned int len = nfs_page_length(req->wb_page);
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 {
383d72ddcbaSWeston Andros Adamson 	if (PageUptodate(req->wb_page))
3841da177e4SLinus Torvalds 		return;
385d72ddcbaSWeston Andros Adamson 	if (!nfs_page_group_covers_page(req))
3861da177e4SLinus Torvalds 		return;
387d72ddcbaSWeston Andros Adamson 	SetPageUptodate(req->wb_page);
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
3911da177e4SLinus Torvalds {
392e87b4c7aSNeilBrown 	int ret = 0;
393cca588d6STrond Myklebust 
394e87b4c7aSNeilBrown 	if (wbc->sync_mode == WB_SYNC_ALL)
395e87b4c7aSNeilBrown 		ret = FLUSH_COND_STABLE;
396e87b4c7aSNeilBrown 	return ret;
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds /*
40089a09141SPeter Zijlstra  * NFS congestion control
40189a09141SPeter Zijlstra  */
40289a09141SPeter Zijlstra 
40389a09141SPeter Zijlstra int nfs_congestion_kb;
40489a09141SPeter Zijlstra 
40589a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
40689a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
40789a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
40889a09141SPeter Zijlstra 
409deed85e7STrond Myklebust static void nfs_set_page_writeback(struct page *page)
41089a09141SPeter Zijlstra {
4110db10944SJan Kara 	struct inode *inode = page_file_mapping(page)->host;
4120db10944SJan Kara 	struct nfs_server *nfss = NFS_SERVER(inode);
4135a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
4145a6d41b3STrond Myklebust 
415deed85e7STrond Myklebust 	WARN_ON_ONCE(ret != 0);
41689a09141SPeter Zijlstra 
417277866a0SPeter Zijlstra 	if (atomic_long_inc_return(&nfss->writeback) >
4180db10944SJan Kara 			NFS_CONGESTION_ON_THRESH)
4196df25e58SNeilBrown 		nfss->write_congested = 1;
42089a09141SPeter Zijlstra }
42189a09141SPeter Zijlstra 
42220633f04SWeston Andros Adamson static void nfs_end_page_writeback(struct nfs_page *req)
42389a09141SPeter Zijlstra {
4246dd85e83STrond Myklebust 	struct inode *inode = nfs_page_to_inode(req);
42589a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
42631a01f09STrond Myklebust 	bool is_done;
42789a09141SPeter Zijlstra 
42831a01f09STrond Myklebust 	is_done = nfs_page_group_sync_on_bit(req, PG_WB_END);
42931a01f09STrond Myklebust 	nfs_unlock_request(req);
43031a01f09STrond Myklebust 	if (!is_done)
43120633f04SWeston Andros Adamson 		return;
43220633f04SWeston Andros Adamson 
43320633f04SWeston Andros Adamson 	end_page_writeback(req->wb_page);
434c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
4356df25e58SNeilBrown 		nfss->write_congested = 0;
43689a09141SPeter Zijlstra }
43789a09141SPeter Zijlstra 
438d4581383SWeston Andros Adamson /*
439d4581383SWeston Andros Adamson  * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests
440d4581383SWeston Andros Adamson  *
441d4581383SWeston Andros Adamson  * @destroy_list - request list (using wb_this_page) terminated by @old_head
442d4581383SWeston Andros Adamson  * @old_head - the old head of the list
443d4581383SWeston Andros Adamson  *
444d4581383SWeston Andros Adamson  * All subrequests must be locked and removed from all lists, so at this point
445d4581383SWeston Andros Adamson  * they are only "active" in this function, and possibly in nfs_wait_on_request
446d4581383SWeston Andros Adamson  * with a reference held by some other context.
447d4581383SWeston Andros Adamson  */
448d4581383SWeston Andros Adamson static void
449d4581383SWeston Andros Adamson nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list,
450b66aaa8dSTrond Myklebust 				 struct nfs_page *old_head,
451b66aaa8dSTrond Myklebust 				 struct inode *inode)
452d4581383SWeston Andros Adamson {
453d4581383SWeston Andros Adamson 	while (destroy_list) {
454d4581383SWeston Andros Adamson 		struct nfs_page *subreq = destroy_list;
455d4581383SWeston Andros Adamson 
456d4581383SWeston Andros Adamson 		destroy_list = (subreq->wb_this_page == old_head) ?
457d4581383SWeston Andros Adamson 				   NULL : subreq->wb_this_page;
458d4581383SWeston Andros Adamson 
45908ca8b21STrond Myklebust 		/* Note: lock subreq in order to change subreq->wb_head */
46008ca8b21STrond Myklebust 		nfs_page_set_headlock(subreq);
461d4581383SWeston Andros Adamson 		WARN_ON_ONCE(old_head != subreq->wb_head);
462d4581383SWeston Andros Adamson 
463d4581383SWeston Andros Adamson 		/* make sure old group is not used */
464d4581383SWeston Andros Adamson 		subreq->wb_this_page = subreq;
46508ca8b21STrond Myklebust 		subreq->wb_head = subreq;
466d4581383SWeston Andros Adamson 
467902a4c00STrond Myklebust 		clear_bit(PG_REMOVE, &subreq->wb_flags);
468902a4c00STrond Myklebust 
4695b2b5187STrond Myklebust 		/* Note: races with nfs_page_group_destroy() */
4705b2b5187STrond Myklebust 		if (!kref_read(&subreq->wb_kref)) {
4715b2b5187STrond Myklebust 			/* Check if we raced with nfs_page_group_destroy() */
47208ca8b21STrond Myklebust 			if (test_and_clear_bit(PG_TEARDOWN, &subreq->wb_flags)) {
47308ca8b21STrond Myklebust 				nfs_page_clear_headlock(subreq);
4745b2b5187STrond Myklebust 				nfs_free_request(subreq);
47508ca8b21STrond Myklebust 			} else
47608ca8b21STrond Myklebust 				nfs_page_clear_headlock(subreq);
4775b2b5187STrond Myklebust 			continue;
4785b2b5187STrond Myklebust 		}
47908ca8b21STrond Myklebust 		nfs_page_clear_headlock(subreq);
480d4581383SWeston Andros Adamson 
481add42de3STrond Myklebust 		nfs_release_request(old_head);
4825b2b5187STrond Myklebust 
483b66aaa8dSTrond Myklebust 		if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags)) {
484d4581383SWeston Andros Adamson 			nfs_release_request(subreq);
485a6b6d5b8STrond Myklebust 			atomic_long_dec(&NFS_I(inode)->nrequests);
486d4581383SWeston Andros Adamson 		}
4875b2b5187STrond Myklebust 
4885b2b5187STrond Myklebust 		/* subreq is now totally disconnected from page group or any
4895b2b5187STrond Myklebust 		 * write / commit lists. last chance to wake any waiters */
4905b2b5187STrond Myklebust 		nfs_unlock_and_release_request(subreq);
491d4581383SWeston Andros Adamson 	}
492d4581383SWeston Andros Adamson }
493d4581383SWeston Andros Adamson 
494d4581383SWeston Andros Adamson /*
495e00ed89dSTrond Myklebust  * nfs_join_page_group - destroy subrequests of the head req
496e00ed89dSTrond Myklebust  * @head: the page used to lookup the "page group" of nfs_page structures
497e00ed89dSTrond Myklebust  * @inode: Inode to which the request belongs.
498d4581383SWeston Andros Adamson  *
499d4581383SWeston Andros Adamson  * This function joins all sub requests to the head request by first
500d4581383SWeston Andros Adamson  * locking all requests in the group, cancelling any pending operations
501d4581383SWeston Andros Adamson  * and finally updating the head request to cover the whole range covered by
502d4581383SWeston Andros Adamson  * the (former) group.  All subrequests are removed from any write or commit
503d4581383SWeston Andros Adamson  * lists, unlinked from the group and destroyed.
504d4581383SWeston Andros Adamson  */
505ed5d588fSTrond Myklebust void
506e00ed89dSTrond Myklebust nfs_join_page_group(struct nfs_page *head, struct inode *inode)
507d4581383SWeston Andros Adamson {
508e00ed89dSTrond Myklebust 	struct nfs_page *subreq;
509d4581383SWeston Andros Adamson 	struct nfs_page *destroy_list = NULL;
510a62f8e3bSTrond Myklebust 	unsigned int pgbase, off, bytes;
511a62f8e3bSTrond Myklebust 
512a62f8e3bSTrond Myklebust 	pgbase = head->wb_pgbase;
513a62f8e3bSTrond Myklebust 	bytes = head->wb_bytes;
514a62f8e3bSTrond Myklebust 	off = head->wb_offset;
515a0e265bcSTrond Myklebust 	for (subreq = head->wb_this_page; subreq != head;
516a0e265bcSTrond Myklebust 			subreq = subreq->wb_this_page) {
517a62f8e3bSTrond Myklebust 		/* Subrequests should always form a contiguous range */
518a62f8e3bSTrond Myklebust 		if (pgbase > subreq->wb_pgbase) {
519a62f8e3bSTrond Myklebust 			off -= pgbase - subreq->wb_pgbase;
520a62f8e3bSTrond Myklebust 			bytes += pgbase - subreq->wb_pgbase;
521a62f8e3bSTrond Myklebust 			pgbase = subreq->wb_pgbase;
522a62f8e3bSTrond Myklebust 		}
523a62f8e3bSTrond Myklebust 		bytes = max(subreq->wb_pgbase + subreq->wb_bytes
524a62f8e3bSTrond Myklebust 				- pgbase, bytes);
5251bd5d6d0STrond Myklebust 	}
5261bd5d6d0STrond Myklebust 
527a62f8e3bSTrond Myklebust 	/* Set the head request's range to cover the former page group */
528a62f8e3bSTrond Myklebust 	head->wb_pgbase = pgbase;
529a62f8e3bSTrond Myklebust 	head->wb_bytes = bytes;
530a62f8e3bSTrond Myklebust 	head->wb_offset = off;
531d4581383SWeston Andros Adamson 
532d4581383SWeston Andros Adamson 	/* Now that all requests are locked, make sure they aren't on any list.
533d4581383SWeston Andros Adamson 	 * Commit list removal accounting is done after locks are dropped */
534d4581383SWeston Andros Adamson 	subreq = head;
535d4581383SWeston Andros Adamson 	do {
536411a99adSWeston Andros Adamson 		nfs_clear_request_commit(subreq);
537d4581383SWeston Andros Adamson 		subreq = subreq->wb_this_page;
538d4581383SWeston Andros Adamson 	} while (subreq != head);
539d4581383SWeston Andros Adamson 
540d4581383SWeston Andros Adamson 	/* unlink subrequests from head, destroy them later */
541d4581383SWeston Andros Adamson 	if (head->wb_this_page != head) {
542d4581383SWeston Andros Adamson 		/* destroy list will be terminated by head */
543d4581383SWeston Andros Adamson 		destroy_list = head->wb_this_page;
544d4581383SWeston Andros Adamson 		head->wb_this_page = head;
545d4581383SWeston Andros Adamson 	}
546d4581383SWeston Andros Adamson 
547b66aaa8dSTrond Myklebust 	nfs_destroy_unlinked_subrequests(destroy_list, head, inode);
548b5bab9bfSTrond Myklebust }
549b5bab9bfSTrond Myklebust 
550e00ed89dSTrond Myklebust /*
551e00ed89dSTrond Myklebust  * nfs_lock_and_join_requests - join all subreqs to the head req
552e00ed89dSTrond Myklebust  * @page: the page used to lookup the "page group" of nfs_page structures
553e00ed89dSTrond Myklebust  *
554e00ed89dSTrond Myklebust  * This function joins all sub requests to the head request by first
555e00ed89dSTrond Myklebust  * locking all requests in the group, cancelling any pending operations
556e00ed89dSTrond Myklebust  * and finally updating the head request to cover the whole range covered by
557e00ed89dSTrond Myklebust  * the (former) group.  All subrequests are removed from any write or commit
558e00ed89dSTrond Myklebust  * lists, unlinked from the group and destroyed.
559e00ed89dSTrond Myklebust  *
560e00ed89dSTrond Myklebust  * Returns a locked, referenced pointer to the head request - which after
561e00ed89dSTrond Myklebust  * this call is guaranteed to be the only request associated with the page.
562e00ed89dSTrond Myklebust  * Returns NULL if no requests are found for @page, or a ERR_PTR if an
563e00ed89dSTrond Myklebust  * error was encountered.
564e00ed89dSTrond Myklebust  */
565e00ed89dSTrond Myklebust static struct nfs_page *
566e00ed89dSTrond Myklebust nfs_lock_and_join_requests(struct page *page)
567e00ed89dSTrond Myklebust {
568e00ed89dSTrond Myklebust 	struct inode *inode = page_file_mapping(page)->host;
569e00ed89dSTrond Myklebust 	struct nfs_page *head;
570e00ed89dSTrond Myklebust 	int ret;
571e00ed89dSTrond Myklebust 
572e00ed89dSTrond Myklebust 	/*
573e00ed89dSTrond Myklebust 	 * A reference is taken only on the head request which acts as a
574e00ed89dSTrond Myklebust 	 * reference to the whole page group - the group will not be destroyed
575e00ed89dSTrond Myklebust 	 * until the head reference is released.
576e00ed89dSTrond Myklebust 	 */
577e00ed89dSTrond Myklebust 	head = nfs_find_and_lock_page_request(page);
578e00ed89dSTrond Myklebust 	if (IS_ERR_OR_NULL(head))
579d4581383SWeston Andros Adamson 		return head;
5800671d8f1SMarkus Elfring 
581e00ed89dSTrond Myklebust 	/* lock each request in the page group */
582e00ed89dSTrond Myklebust 	ret = nfs_page_group_lock_subrequests(head);
583e00ed89dSTrond Myklebust 	if (ret < 0) {
5840671d8f1SMarkus Elfring 		nfs_unlock_and_release_request(head);
5850671d8f1SMarkus Elfring 		return ERR_PTR(ret);
586612c9384STrond Myklebust 	}
587074cc1deSTrond Myklebust 
588e00ed89dSTrond Myklebust 	nfs_join_page_group(head, inode);
589e00ed89dSTrond Myklebust 
590e00ed89dSTrond Myklebust 	return head;
591e00ed89dSTrond Myklebust }
592e00ed89dSTrond Myklebust 
5936fbda89bSTrond Myklebust static void nfs_write_error(struct nfs_page *req, int error)
5940bcbf039SPeng Tao {
5956dd85e83STrond Myklebust 	trace_nfs_write_error(nfs_page_to_inode(req), req, error);
5966fbda89bSTrond Myklebust 	nfs_mapping_set_error(req->wb_page, error);
59706c9fdf3STrond Myklebust 	nfs_inode_remove_request(req);
5980bcbf039SPeng Tao 	nfs_end_page_writeback(req);
5991f84ccdfSFred Isaman 	nfs_release_request(req);
6000bcbf039SPeng Tao }
6010bcbf039SPeng Tao 
602074cc1deSTrond Myklebust /*
603074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
604074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
605074cc1deSTrond Myklebust  */
606c6fd3511STrond Myklebust static int nfs_page_async_flush(struct page *page,
607c6fd3511STrond Myklebust 				struct writeback_control *wbc,
608c6fd3511STrond Myklebust 				struct nfs_pageio_descriptor *pgio)
609074cc1deSTrond Myklebust {
610074cc1deSTrond Myklebust 	struct nfs_page *req;
611074cc1deSTrond Myklebust 	int ret = 0;
612074cc1deSTrond Myklebust 
6136d17d653STrond Myklebust 	req = nfs_lock_and_join_requests(page);
614074cc1deSTrond Myklebust 	if (!req)
615074cc1deSTrond Myklebust 		goto out;
616074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
617074cc1deSTrond Myklebust 	if (IS_ERR(req))
618074cc1deSTrond Myklebust 		goto out;
619074cc1deSTrond Myklebust 
620deed85e7STrond Myklebust 	nfs_set_page_writeback(page);
621deed85e7STrond Myklebust 	WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
622074cc1deSTrond Myklebust 
623a6598813STrond Myklebust 	/* If there is a fatal error that covers this write, just exit */
62496c41455STrond Myklebust 	ret = pgio->pg_error;
62596c41455STrond Myklebust 	if (nfs_error_is_fatal_on_server(ret))
626a6598813STrond Myklebust 		goto out_launder;
627a6598813STrond Myklebust 
62896c41455STrond Myklebust 	ret = 0;
629f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
630074cc1deSTrond Myklebust 		ret = pgio->pg_error;
6310bcbf039SPeng Tao 		/*
632c373fff7STrond Myklebust 		 * Remove the problematic req upon fatal errors on the server
6330bcbf039SPeng Tao 		 */
634c373fff7STrond Myklebust 		if (nfs_error_is_fatal_on_server(ret))
635a6598813STrond Myklebust 			goto out_launder;
636c6fd3511STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE)
637c6fd3511STrond Myklebust 			ret = AOP_WRITEPAGE_ACTIVATE;
638c6fd3511STrond Myklebust 		redirty_page_for_writepage(wbc, page);
6398fc75bedSTrond Myklebust 		nfs_redirty_request(req);
64096c41455STrond Myklebust 		pgio->pg_error = 0;
64140f90271STrond Myklebust 	} else
64240f90271STrond Myklebust 		nfs_add_stats(page_file_mapping(page)->host,
64340f90271STrond Myklebust 				NFSIOS_WRITEPAGES, 1);
644074cc1deSTrond Myklebust out:
645074cc1deSTrond Myklebust 	return ret;
646a6598813STrond Myklebust out_launder:
6476fbda89bSTrond Myklebust 	nfs_write_error(req, ret);
64814bebe3cSTrond Myklebust 	return 0;
649e261f51fSTrond Myklebust }
650e261f51fSTrond Myklebust 
651d6c843b9SPeng Tao static int nfs_do_writepage(struct page *page, struct writeback_control *wbc,
652c373fff7STrond Myklebust 			    struct nfs_pageio_descriptor *pgio)
653f758c885STrond Myklebust {
6548cd79788SHuang Ying 	nfs_pageio_cond_complete(pgio, page_index(page));
655c6fd3511STrond Myklebust 	return nfs_page_async_flush(page, wbc, pgio);
656f758c885STrond Myklebust }
657f758c885STrond Myklebust 
658e261f51fSTrond Myklebust /*
6591da177e4SLinus Torvalds  * Write an mmapped page to the server.
6601da177e4SLinus Torvalds  */
661d6c843b9SPeng Tao static int nfs_writepage_locked(struct page *page,
662c373fff7STrond Myklebust 				struct writeback_control *wbc)
6631da177e4SLinus Torvalds {
664f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
66540f90271STrond Myklebust 	struct inode *inode = page_file_mapping(page)->host;
666e261f51fSTrond Myklebust 	int err;
6671da177e4SLinus Torvalds 
6686df25e58SNeilBrown 	if (wbc->sync_mode == WB_SYNC_NONE &&
6696df25e58SNeilBrown 	    NFS_SERVER(inode)->write_congested)
6706df25e58SNeilBrown 		return AOP_WRITEPAGE_ACTIVATE;
6716df25e58SNeilBrown 
67240f90271STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
673811ed92eSTrond Myklebust 	nfs_pageio_init_write(&pgio, inode, 0,
674a20c93e3SChristoph Hellwig 				false, &nfs_async_write_completion_ops);
675c373fff7STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
67696c41455STrond Myklebust 	pgio.pg_error = 0;
677f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
6784d770ccfSTrond Myklebust 	return err;
6794d770ccfSTrond Myklebust }
6804d770ccfSTrond Myklebust 
6814d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
6824d770ccfSTrond Myklebust {
683f758c885STrond Myklebust 	int ret;
6844d770ccfSTrond Myklebust 
685c373fff7STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
68696c41455STrond Myklebust 	if (ret != AOP_WRITEPAGE_ACTIVATE)
6871da177e4SLinus Torvalds 		unlock_page(page);
688f758c885STrond Myklebust 	return ret;
689f758c885STrond Myklebust }
690f758c885STrond Myklebust 
691f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
692f758c885STrond Myklebust {
693f758c885STrond Myklebust 	int ret;
694f758c885STrond Myklebust 
695c373fff7STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
69696c41455STrond Myklebust 	if (ret != AOP_WRITEPAGE_ACTIVATE)
697f758c885STrond Myklebust 		unlock_page(page);
698f758c885STrond Myklebust 	return ret;
6991da177e4SLinus Torvalds }
7001da177e4SLinus Torvalds 
701919e3bd9STrond Myklebust static void nfs_io_completion_commit(void *inode)
702919e3bd9STrond Myklebust {
703919e3bd9STrond Myklebust 	nfs_commit_inode(inode, 0);
704919e3bd9STrond Myklebust }
705919e3bd9STrond Myklebust 
7061da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
7071da177e4SLinus Torvalds {
7081da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
709c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
710ed7bcdb3STrond Myklebust 	struct nfs_io_completion *ioc = NULL;
711ed7bcdb3STrond Myklebust 	unsigned int mntflags = NFS_SERVER(inode)->flags;
712ed7bcdb3STrond Myklebust 	int priority = 0;
7131da177e4SLinus Torvalds 	int err;
7141da177e4SLinus Torvalds 
7156df25e58SNeilBrown 	if (wbc->sync_mode == WB_SYNC_NONE &&
7166df25e58SNeilBrown 	    NFS_SERVER(inode)->write_congested)
7176df25e58SNeilBrown 		return 0;
7186df25e58SNeilBrown 
71991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
72091d5b470SChuck Lever 
721ed7bcdb3STrond Myklebust 	if (!(mntflags & NFS_MOUNT_WRITE_EAGER) || wbc->for_kupdate ||
722ed7bcdb3STrond Myklebust 	    wbc->for_background || wbc->for_sync || wbc->for_reclaim) {
7232b17d725STrond Myklebust 		ioc = nfs_io_completion_alloc(GFP_KERNEL);
724919e3bd9STrond Myklebust 		if (ioc)
725ed7bcdb3STrond Myklebust 			nfs_io_completion_init(ioc, nfs_io_completion_commit,
726ed7bcdb3STrond Myklebust 					       inode);
727ed7bcdb3STrond Myklebust 		priority = wb_priority(wbc);
728ed7bcdb3STrond Myklebust 	}
729919e3bd9STrond Myklebust 
730c6fd3511STrond Myklebust 	do {
731ed7bcdb3STrond Myklebust 		nfs_pageio_init_write(&pgio, inode, priority, false,
732a20c93e3SChristoph Hellwig 				      &nfs_async_write_completion_ops);
733919e3bd9STrond Myklebust 		pgio.pg_io_completion = ioc;
734c6fd3511STrond Myklebust 		err = write_cache_pages(mapping, wbc, nfs_writepages_callback,
735c6fd3511STrond Myklebust 					&pgio);
73696c41455STrond Myklebust 		pgio.pg_error = 0;
737c63c7b05STrond Myklebust 		nfs_pageio_complete(&pgio);
738c6fd3511STrond Myklebust 	} while (err < 0 && !nfs_error_is_fatal(err));
739919e3bd9STrond Myklebust 	nfs_io_completion_put(ioc);
74072cb77f4STrond Myklebust 
741f758c885STrond Myklebust 	if (err < 0)
74272cb77f4STrond Myklebust 		goto out_err;
743c63c7b05STrond Myklebust 	return 0;
74472cb77f4STrond Myklebust out_err:
74572cb77f4STrond Myklebust 	return err;
7461da177e4SLinus Torvalds }
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds /*
7491da177e4SLinus Torvalds  * Insert a write request into an inode
7501da177e4SLinus Torvalds  */
751d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
7521da177e4SLinus Torvalds {
7534b9bb25bSTrond Myklebust 	struct address_space *mapping = page_file_mapping(req->wb_page);
7541da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
755e7d39069STrond Myklebust 
7562bfc6e56SWeston Andros Adamson 	WARN_ON_ONCE(req->wb_this_page != req);
7572bfc6e56SWeston Andros Adamson 
758e7d39069STrond Myklebust 	/* Lock the request! */
7597ad84aa9STrond Myklebust 	nfs_lock_request(req);
760e7d39069STrond Myklebust 
76129418aa4SMel Gorman 	/*
76229418aa4SMel Gorman 	 * Swap-space should not get truncated. Hence no need to plug the race
76329418aa4SMel Gorman 	 * with invalidate/truncate.
76429418aa4SMel Gorman 	 */
7654b9bb25bSTrond Myklebust 	spin_lock(&mapping->private_lock);
76629418aa4SMel Gorman 	if (likely(!PageSwapCache(req->wb_page))) {
7672df485a7STrond Myklebust 		set_bit(PG_MAPPED, &req->wb_flags);
768deb7d638STrond Myklebust 		SetPagePrivate(req->wb_page);
769277459d2STrond Myklebust 		set_page_private(req->wb_page, (unsigned long)req);
77029418aa4SMel Gorman 	}
7714b9bb25bSTrond Myklebust 	spin_unlock(&mapping->private_lock);
772a6b6d5b8STrond Myklebust 	atomic_long_inc(&nfsi->nrequests);
77317089a29SWeston Andros Adamson 	/* this a head request for a page group - mark it as having an
774cb1410c7SWeston Andros Adamson 	 * extra reference so sub groups can follow suit.
775cb1410c7SWeston Andros Adamson 	 * This flag also informs pgio layer when to bump nrequests when
776cb1410c7SWeston Andros Adamson 	 * adding subrequests. */
77717089a29SWeston Andros Adamson 	WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags));
778c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
7791da177e4SLinus Torvalds }
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds /*
78289a09141SPeter Zijlstra  * Remove a write request from an inode
7831da177e4SLinus Torvalds  */
7841da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
7851da177e4SLinus Torvalds {
7864b9bb25bSTrond Myklebust 	struct address_space *mapping = page_file_mapping(req->wb_page);
7874b9bb25bSTrond Myklebust 	struct inode *inode = mapping->host;
7881da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
78920633f04SWeston Andros Adamson 	struct nfs_page *head;
79020633f04SWeston Andros Adamson 
79120633f04SWeston Andros Adamson 	if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) {
79220633f04SWeston Andros Adamson 		head = req->wb_head;
7931da177e4SLinus Torvalds 
7944b9bb25bSTrond Myklebust 		spin_lock(&mapping->private_lock);
79567911c8fSAnna Schumaker 		if (likely(head->wb_page && !PageSwapCache(head->wb_page))) {
79620633f04SWeston Andros Adamson 			set_page_private(head->wb_page, 0);
79720633f04SWeston Andros Adamson 			ClearPagePrivate(head->wb_page);
79820633f04SWeston Andros Adamson 			clear_bit(PG_MAPPED, &head->wb_flags);
79929418aa4SMel Gorman 		}
8004b9bb25bSTrond Myklebust 		spin_unlock(&mapping->private_lock);
80120633f04SWeston Andros Adamson 	}
80217089a29SWeston Andros Adamson 
80333ea5aaaSZhangXiaoxu 	if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags)) {
8041da177e4SLinus Torvalds 		nfs_release_request(req);
80533ea5aaaSZhangXiaoxu 		atomic_long_dec(&nfsi->nrequests);
80633ea5aaaSZhangXiaoxu 	}
8071da177e4SLinus Torvalds }
8081da177e4SLinus Torvalds 
80961822ab5STrond Myklebust static void
8106d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
81161822ab5STrond Myklebust {
81267911c8fSAnna Schumaker 	if (req->wb_page)
81361822ab5STrond Myklebust 		__set_page_dirty_nobuffers(req->wb_page);
81461822ab5STrond Myklebust }
81561822ab5STrond Myklebust 
8163a3908c8STrond Myklebust /*
8173a3908c8STrond Myklebust  * nfs_page_search_commits_for_head_request_locked
8183a3908c8STrond Myklebust  *
8193a3908c8STrond Myklebust  * Search through commit lists on @inode for the head request for @page.
8203a3908c8STrond Myklebust  * Must be called while holding the inode (which is cinfo) lock.
8213a3908c8STrond Myklebust  *
8223a3908c8STrond Myklebust  * Returns the head request if found, or NULL if not found.
8233a3908c8STrond Myklebust  */
8243a3908c8STrond Myklebust static struct nfs_page *
8253a3908c8STrond Myklebust nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
8263a3908c8STrond Myklebust 						struct page *page)
8273a3908c8STrond Myklebust {
8283a3908c8STrond Myklebust 	struct nfs_page *freq, *t;
8293a3908c8STrond Myklebust 	struct nfs_commit_info cinfo;
8303a3908c8STrond Myklebust 	struct inode *inode = &nfsi->vfs_inode;
8313a3908c8STrond Myklebust 
8323a3908c8STrond Myklebust 	nfs_init_cinfo_from_inode(&cinfo, inode);
8333a3908c8STrond Myklebust 
8343a3908c8STrond Myklebust 	/* search through pnfs commit lists */
8353a3908c8STrond Myklebust 	freq = pnfs_search_commit_reqs(inode, &cinfo, page);
8363a3908c8STrond Myklebust 	if (freq)
8373a3908c8STrond Myklebust 		return freq->wb_head;
8383a3908c8STrond Myklebust 
8393a3908c8STrond Myklebust 	/* Linearly search the commit list for the correct request */
8403a3908c8STrond Myklebust 	list_for_each_entry_safe(freq, t, &cinfo.mds->list, wb_list) {
8413a3908c8STrond Myklebust 		if (freq->wb_page == page)
8423a3908c8STrond Myklebust 			return freq->wb_head;
8433a3908c8STrond Myklebust 	}
8443a3908c8STrond Myklebust 
8453a3908c8STrond Myklebust 	return NULL;
8463a3908c8STrond Myklebust }
8473a3908c8STrond Myklebust 
8488dd37758STrond Myklebust /**
84986d80f97STrond Myklebust  * nfs_request_add_commit_list_locked - add request to a commit list
85086d80f97STrond Myklebust  * @req: pointer to a struct nfs_page
85186d80f97STrond Myklebust  * @dst: commit list head
85286d80f97STrond Myklebust  * @cinfo: holds list lock and accounting info
85386d80f97STrond Myklebust  *
85486d80f97STrond Myklebust  * This sets the PG_CLEAN bit, updates the cinfo count of
85586d80f97STrond Myklebust  * number of outstanding requests requiring a commit as well as
85686d80f97STrond Myklebust  * the MM page stats.
85786d80f97STrond Myklebust  *
858e824f99aSTrond Myklebust  * The caller must hold NFS_I(cinfo->inode)->commit_mutex, and the
859e824f99aSTrond Myklebust  * nfs_page lock.
86086d80f97STrond Myklebust  */
86186d80f97STrond Myklebust void
86286d80f97STrond Myklebust nfs_request_add_commit_list_locked(struct nfs_page *req, struct list_head *dst,
86386d80f97STrond Myklebust 			    struct nfs_commit_info *cinfo)
86486d80f97STrond Myklebust {
86586d80f97STrond Myklebust 	set_bit(PG_CLEAN, &req->wb_flags);
86686d80f97STrond Myklebust 	nfs_list_add_request(req, dst);
8675cb953d4STrond Myklebust 	atomic_long_inc(&cinfo->mds->ncommit);
86886d80f97STrond Myklebust }
86986d80f97STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked);
87086d80f97STrond Myklebust 
87186d80f97STrond Myklebust /**
8728dd37758STrond Myklebust  * nfs_request_add_commit_list - add request to a commit list
8738dd37758STrond Myklebust  * @req: pointer to a struct nfs_page
874ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
8758dd37758STrond Myklebust  *
876ea2cf228SFred Isaman  * This sets the PG_CLEAN bit, updates the cinfo count of
8778dd37758STrond Myklebust  * number of outstanding requests requiring a commit as well as
8788dd37758STrond Myklebust  * the MM page stats.
8798dd37758STrond Myklebust  *
880ea2cf228SFred Isaman  * The caller must _not_ hold the cinfo->lock, but must be
8818dd37758STrond Myklebust  * holding the nfs_page lock.
8828dd37758STrond Myklebust  */
8838dd37758STrond Myklebust void
8846272dcc6SAnna Schumaker nfs_request_add_commit_list(struct nfs_page *req, struct nfs_commit_info *cinfo)
8858dd37758STrond Myklebust {
886e824f99aSTrond Myklebust 	mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
8876272dcc6SAnna Schumaker 	nfs_request_add_commit_list_locked(req, &cinfo->mds->list, cinfo);
888e824f99aSTrond Myklebust 	mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
88967911c8fSAnna Schumaker 	if (req->wb_page)
89086d80f97STrond Myklebust 		nfs_mark_page_unstable(req->wb_page, cinfo);
8918dd37758STrond Myklebust }
8928dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
8938dd37758STrond Myklebust 
8948dd37758STrond Myklebust /**
8958dd37758STrond Myklebust  * nfs_request_remove_commit_list - Remove request from a commit list
8968dd37758STrond Myklebust  * @req: pointer to a nfs_page
897ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
8988dd37758STrond Myklebust  *
899ea2cf228SFred Isaman  * This clears the PG_CLEAN bit, and updates the cinfo's count of
9008dd37758STrond Myklebust  * number of outstanding requests requiring a commit
9018dd37758STrond Myklebust  * It does not update the MM page stats.
9028dd37758STrond Myklebust  *
903ea2cf228SFred Isaman  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
9048dd37758STrond Myklebust  */
9058dd37758STrond Myklebust void
906ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req,
907ea2cf228SFred Isaman 			       struct nfs_commit_info *cinfo)
9088dd37758STrond Myklebust {
9098dd37758STrond Myklebust 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
9108dd37758STrond Myklebust 		return;
9118dd37758STrond Myklebust 	nfs_list_remove_request(req);
9125cb953d4STrond Myklebust 	atomic_long_dec(&cinfo->mds->ncommit);
9138dd37758STrond Myklebust }
9148dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
9158dd37758STrond Myklebust 
916ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
917ea2cf228SFred Isaman 				      struct inode *inode)
918ea2cf228SFred Isaman {
919fe238e60SDave Wysochanski 	cinfo->inode = inode;
920ea2cf228SFred Isaman 	cinfo->mds = &NFS_I(inode)->commit_info;
921ea2cf228SFred Isaman 	cinfo->ds = pnfs_get_ds_info(inode);
922b359f9d0SFred Isaman 	cinfo->dreq = NULL;
923f453a54aSFred Isaman 	cinfo->completion_ops = &nfs_commit_completion_ops;
924ea2cf228SFred Isaman }
925ea2cf228SFred Isaman 
926ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
927ea2cf228SFred Isaman 		    struct inode *inode,
928ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq)
929ea2cf228SFred Isaman {
9301763da12SFred Isaman 	if (dreq)
9311763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
9321763da12SFred Isaman 	else
933ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(cinfo, inode);
934ea2cf228SFred Isaman }
935ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo);
9368dd37758STrond Myklebust 
9371da177e4SLinus Torvalds /*
9381da177e4SLinus Torvalds  * Add a request to the inode's commit list.
9391da177e4SLinus Torvalds  */
9401763da12SFred Isaman void
941ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
942b57ff130SWeston Andros Adamson 			struct nfs_commit_info *cinfo, u32 ds_commit_idx)
9431da177e4SLinus Torvalds {
944b57ff130SWeston Andros Adamson 	if (pnfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx))
9458dd37758STrond Myklebust 		return;
9466272dcc6SAnna Schumaker 	nfs_request_add_commit_list(req, cinfo);
9471da177e4SLinus Torvalds }
9488e821cadSTrond Myklebust 
949d6d6dc7cSFred Isaman static void
950d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page)
951e468bae9STrond Myklebust {
9528d92890bSNeilBrown 	dec_node_page_state(page, NR_WRITEBACK);
95393f78d88STejun Heo 	dec_wb_stat(&inode_to_bdi(page_file_mapping(page)->host)->wb,
9548d92890bSNeilBrown 		    WB_WRITEBACK);
955e468bae9STrond Myklebust }
956d6d6dc7cSFred Isaman 
957b5bab9bfSTrond Myklebust /* Called holding the request lock on @req */
9588dd37758STrond Myklebust static void
959d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req)
960d6d6dc7cSFred Isaman {
9618dd37758STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
9629fcd5960STrond Myklebust 		struct nfs_open_context *ctx = nfs_req_openctx(req);
9639fcd5960STrond Myklebust 		struct inode *inode = d_inode(ctx->dentry);
964ea2cf228SFred Isaman 		struct nfs_commit_info cinfo;
965d6d6dc7cSFred Isaman 
966ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(&cinfo, inode);
967e824f99aSTrond Myklebust 		mutex_lock(&NFS_I(inode)->commit_mutex);
968ea2cf228SFred Isaman 		if (!pnfs_clear_request_commit(req, &cinfo)) {
969ea2cf228SFred Isaman 			nfs_request_remove_commit_list(req, &cinfo);
970d6d6dc7cSFred Isaman 		}
971e824f99aSTrond Myklebust 		mutex_unlock(&NFS_I(inode)->commit_mutex);
9728dd37758STrond Myklebust 		nfs_clear_page_commit(req->wb_page);
9738dd37758STrond Myklebust 	}
974e468bae9STrond Myklebust }
975e468bae9STrond Myklebust 
976d45f60c6SWeston Andros Adamson int nfs_write_need_commit(struct nfs_pgio_header *hdr)
9778e821cadSTrond Myklebust {
978c65e6254SWeston Andros Adamson 	if (hdr->verf.committed == NFS_DATA_SYNC)
979d45f60c6SWeston Andros Adamson 		return hdr->lseg == NULL;
980c65e6254SWeston Andros Adamson 	return hdr->verf.committed != NFS_FILE_SYNC;
9818e821cadSTrond Myklebust }
9828e821cadSTrond Myklebust 
983919e3bd9STrond Myklebust static void nfs_async_write_init(struct nfs_pgio_header *hdr)
984919e3bd9STrond Myklebust {
985919e3bd9STrond Myklebust 	nfs_io_completion_get(hdr->io_completion);
986919e3bd9STrond Myklebust }
987919e3bd9STrond Myklebust 
988061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
9896c75dc0dSFred Isaman {
990ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
9916c75dc0dSFred Isaman 	unsigned long bytes = 0;
9926c75dc0dSFred Isaman 
9936c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
9946c75dc0dSFred Isaman 		goto out;
995ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
9966c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
9976c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
9986c75dc0dSFred Isaman 
9996c75dc0dSFred Isaman 		bytes += req->wb_bytes;
10006c75dc0dSFred Isaman 		nfs_list_remove_request(req);
10016c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
10026c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
1003af887e43STrond Myklebust 			trace_nfs_comp_error(hdr->inode, req, hdr->error);
10046fbda89bSTrond Myklebust 			nfs_mapping_set_error(req->wb_page, hdr->error);
10056c75dc0dSFred Isaman 			goto remove_req;
10066c75dc0dSFred Isaman 		}
1007c65e6254SWeston Andros Adamson 		if (nfs_write_need_commit(hdr)) {
100833344e0fSTrond Myklebust 			/* Reset wb_nio, since the write was successful. */
100933344e0fSTrond Myklebust 			req->wb_nio = 0;
1010f79d06f5SAnna Schumaker 			memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
1011b57ff130SWeston Andros Adamson 			nfs_mark_request_commit(req, hdr->lseg, &cinfo,
1012a7d42ddbSWeston Andros Adamson 				hdr->pgio_mirror_idx);
10136c75dc0dSFred Isaman 			goto next;
10146c75dc0dSFred Isaman 		}
10156c75dc0dSFred Isaman remove_req:
10166c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
10176c75dc0dSFred Isaman next:
101820633f04SWeston Andros Adamson 		nfs_end_page_writeback(req);
10193aff4ebbSTrond Myklebust 		nfs_release_request(req);
10206c75dc0dSFred Isaman 	}
10216c75dc0dSFred Isaman out:
1022919e3bd9STrond Myklebust 	nfs_io_completion_put(hdr->io_completion);
10236c75dc0dSFred Isaman 	hdr->release(hdr);
10246c75dc0dSFred Isaman }
10256c75dc0dSFred Isaman 
1026ce59515cSAnna Schumaker unsigned long
1027ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
1028fb8a1f11STrond Myklebust {
10295cb953d4STrond Myklebust 	return atomic_long_read(&cinfo->mds->ncommit);
1030fb8a1f11STrond Myklebust }
1031fb8a1f11STrond Myklebust 
1032e824f99aSTrond Myklebust /* NFS_I(cinfo->inode)->commit_mutex held by caller */
10331763da12SFred Isaman int
1034ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
1035ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
1036d6d6dc7cSFred Isaman {
1037137da553STrond Myklebust 	struct nfs_page *req, *tmp;
1038d6d6dc7cSFred Isaman 	int ret = 0;
1039d6d6dc7cSFred Isaman 
1040137da553STrond Myklebust 	list_for_each_entry_safe(req, tmp, src, wb_list) {
10417ad84aa9STrond Myklebust 		kref_get(&req->wb_kref);
10422ce209c4STrond Myklebust 		if (!nfs_lock_request(req)) {
1043137da553STrond Myklebust 			nfs_release_request(req);
1044137da553STrond Myklebust 			continue;
1045137da553STrond Myklebust 		}
1046ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
10475d2a9d9dSTrond Myklebust 		clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
10488dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
1049d6d6dc7cSFred Isaman 		ret++;
10501763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
1051d6d6dc7cSFred Isaman 			break;
1052e824f99aSTrond Myklebust 		cond_resched();
1053d6d6dc7cSFred Isaman 	}
1054d6d6dc7cSFred Isaman 	return ret;
1055d6d6dc7cSFred Isaman }
10565d2a9d9dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_scan_commit_list);
1057d6d6dc7cSFred Isaman 
10581da177e4SLinus Torvalds /*
10591da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
10601da177e4SLinus Torvalds  * @inode: NFS inode to scan
1061ea2cf228SFred Isaman  * @dst: mds destination list
1062ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
10631da177e4SLinus Torvalds  *
10641da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
10651da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
10661da177e4SLinus Torvalds  */
10671763da12SFred Isaman int
1068ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
1069ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
10701da177e4SLinus Torvalds {
1071d6d6dc7cSFred Isaman 	int ret = 0;
1072fb8a1f11STrond Myklebust 
10735cb953d4STrond Myklebust 	if (!atomic_long_read(&cinfo->mds->ncommit))
10745cb953d4STrond Myklebust 		return 0;
1075e824f99aSTrond Myklebust 	mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
10765cb953d4STrond Myklebust 	if (atomic_long_read(&cinfo->mds->ncommit) > 0) {
10778dd37758STrond Myklebust 		const int max = INT_MAX;
1078d6d6dc7cSFred Isaman 
1079ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
1080ea2cf228SFred Isaman 					   cinfo, max);
1081ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
1082d6d6dc7cSFred Isaman 	}
1083e824f99aSTrond Myklebust 	mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
1084ff778d02STrond Myklebust 	return ret;
10851da177e4SLinus Torvalds }
1086d6d6dc7cSFred Isaman 
10871da177e4SLinus Torvalds /*
1088e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
1089e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
10901da177e4SLinus Torvalds  *
1091e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
1092e7d39069STrond Myklebust  * to disk.
10931da177e4SLinus Torvalds  */
1094e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
1095e7d39069STrond Myklebust 		struct page *page,
1096e7d39069STrond Myklebust 		unsigned int offset,
1097e7d39069STrond Myklebust 		unsigned int bytes)
10981da177e4SLinus Torvalds {
1099e7d39069STrond Myklebust 	struct nfs_page *req;
1100e7d39069STrond Myklebust 	unsigned int rqend;
1101e7d39069STrond Myklebust 	unsigned int end;
11021da177e4SLinus Torvalds 	int error;
1103277459d2STrond Myklebust 
1104e7d39069STrond Myklebust 	end = offset + bytes;
1105e7d39069STrond Myklebust 
1106f6032f21STrond Myklebust 	req = nfs_lock_and_join_requests(page);
1107f6032f21STrond Myklebust 	if (IS_ERR_OR_NULL(req))
1108f6032f21STrond Myklebust 		return req;
11092bfc6e56SWeston Andros Adamson 
1110e7d39069STrond Myklebust 	rqend = req->wb_offset + req->wb_bytes;
1111e7d39069STrond Myklebust 	/*
1112e7d39069STrond Myklebust 	 * Tell the caller to flush out the request if
1113e7d39069STrond Myklebust 	 * the offsets are non-contiguous.
1114e7d39069STrond Myklebust 	 * Note: nfs_flush_incompatible() will already
1115e7d39069STrond Myklebust 	 * have flushed out requests having wrong owners.
1116e7d39069STrond Myklebust 	 */
1117f6032f21STrond Myklebust 	if (offset > rqend || end < req->wb_offset)
1118e7d39069STrond Myklebust 		goto out_flushme;
1119e7d39069STrond Myklebust 
11201da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
11211da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
11221da177e4SLinus Torvalds 		req->wb_offset = offset;
11231da177e4SLinus Torvalds 		req->wb_pgbase = offset;
11241da177e4SLinus Torvalds 	}
11251da177e4SLinus Torvalds 	if (end > rqend)
11261da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
1127e7d39069STrond Myklebust 	else
1128e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
112933344e0fSTrond Myklebust 	req->wb_nio = 0;
1130e7d39069STrond Myklebust 	return req;
1131e7d39069STrond Myklebust out_flushme:
1132f6032f21STrond Myklebust 	/*
1133f6032f21STrond Myklebust 	 * Note: we mark the request dirty here because
1134f6032f21STrond Myklebust 	 * nfs_lock_and_join_requests() cannot preserve
1135f6032f21STrond Myklebust 	 * commit flags, so we have to replay the write.
1136f6032f21STrond Myklebust 	 */
1137f6032f21STrond Myklebust 	nfs_mark_request_dirty(req);
1138f6032f21STrond Myklebust 	nfs_unlock_and_release_request(req);
1139e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
1140f6032f21STrond Myklebust 	return (error < 0) ? ERR_PTR(error) : NULL;
1141e7d39069STrond Myklebust }
11421da177e4SLinus Torvalds 
1143e7d39069STrond Myklebust /*
1144e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
1145e7d39069STrond Myklebust  *
1146e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
1147e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
1148e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
1149e7d39069STrond Myklebust  */
1150e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
1151e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
1152e7d39069STrond Myklebust {
1153d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
1154e7d39069STrond Myklebust 	struct nfs_page	*req;
1155e7d39069STrond Myklebust 
1156e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
1157e7d39069STrond Myklebust 	if (req != NULL)
1158e7d39069STrond Myklebust 		goto out;
115928b1d3f5STrond Myklebust 	req = nfs_create_request(ctx, page, offset, bytes);
1160e7d39069STrond Myklebust 	if (IS_ERR(req))
1161e7d39069STrond Myklebust 		goto out;
1162d6d6dc7cSFred Isaman 	nfs_inode_add_request(inode, req);
1163efc91ed0STrond Myklebust out:
116461e930a9STrond Myklebust 	return req;
11651da177e4SLinus Torvalds }
11661da177e4SLinus Torvalds 
1167e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
1168e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
1169e7d39069STrond Myklebust {
1170e7d39069STrond Myklebust 	struct nfs_page	*req;
1171e7d39069STrond Myklebust 
1172e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
1173e7d39069STrond Myklebust 	if (IS_ERR(req))
1174e7d39069STrond Myklebust 		return PTR_ERR(req);
1175e7d39069STrond Myklebust 	/* Update file length */
1176e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
1177d72ddcbaSWeston Andros Adamson 	nfs_mark_uptodate(req);
1178a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
11791d1afcbcSTrond Myklebust 	nfs_unlock_and_release_request(req);
1180e7d39069STrond Myklebust 	return 0;
1181e7d39069STrond Myklebust }
1182e7d39069STrond Myklebust 
11831da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
11841da177e4SLinus Torvalds {
1185cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
11862a369153STrond Myklebust 	struct nfs_lock_context *l_ctx;
118717b985deSJeff Layton 	struct file_lock_context *flctx = locks_inode_context(file_inode(file));
11881da177e4SLinus Torvalds 	struct nfs_page	*req;
11891a54533eSTrond Myklebust 	int do_flush, status;
11901da177e4SLinus Torvalds 	/*
11911da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
11921da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
11931da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
11941da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
11951da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
11961da177e4SLinus Torvalds 	 * dropped page.
11971da177e4SLinus Torvalds 	 */
11981a54533eSTrond Myklebust 	do {
119984d3a9a9SWeston Andros Adamson 		req = nfs_page_find_head_request(page);
12001a54533eSTrond Myklebust 		if (req == NULL)
12011a54533eSTrond Myklebust 			return 0;
12022a369153STrond Myklebust 		l_ctx = req->wb_lock_context;
1203138a2935STrond Myklebust 		do_flush = req->wb_page != page ||
12049fcd5960STrond Myklebust 			!nfs_match_open_context(nfs_req_openctx(req), ctx);
1205bd61e0a9SJeff Layton 		if (l_ctx && flctx &&
1206bd61e0a9SJeff Layton 		    !(list_empty_careful(&flctx->flc_posix) &&
1207bd61e0a9SJeff Layton 		      list_empty_careful(&flctx->flc_flock))) {
1208d51fdb87SNeilBrown 			do_flush |= l_ctx->lockowner != current->files;
12095263e31eSJeff Layton 		}
12101da177e4SLinus Torvalds 		nfs_release_request(req);
12111a54533eSTrond Myklebust 		if (!do_flush)
12121a54533eSTrond Myklebust 			return 0;
1213d56b4ddfSMel Gorman 		status = nfs_wb_page(page_file_mapping(page)->host, page);
12141a54533eSTrond Myklebust 	} while (status == 0);
12151a54533eSTrond Myklebust 	return status;
12161da177e4SLinus Torvalds }
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds /*
1219dc24826bSAndy Adamson  * Avoid buffered writes when a open context credential's key would
1220dc24826bSAndy Adamson  * expire soon.
1221dc24826bSAndy Adamson  *
1222dc24826bSAndy Adamson  * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
1223dc24826bSAndy Adamson  *
1224dc24826bSAndy Adamson  * Return 0 and set a credential flag which triggers the inode to flush
1225dc24826bSAndy Adamson  * and performs  NFS_FILE_SYNC writes if the key will expired within
1226dc24826bSAndy Adamson  * RPC_KEY_EXPIRE_TIMEO.
1227dc24826bSAndy Adamson  */
1228dc24826bSAndy Adamson int
1229dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode)
1230dc24826bSAndy Adamson {
1231dc24826bSAndy Adamson 	struct nfs_open_context *ctx = nfs_file_open_context(filp);
1232dc24826bSAndy Adamson 
1233ddf529eeSNeilBrown 	if (nfs_ctx_key_to_expire(ctx, inode) &&
1234ca05cbaeSTrond Myklebust 	    !rcu_access_pointer(ctx->ll_cred))
1235ddf529eeSNeilBrown 		/* Already expired! */
1236ddf529eeSNeilBrown 		return -EACCES;
1237ddf529eeSNeilBrown 	return 0;
1238dc24826bSAndy Adamson }
1239dc24826bSAndy Adamson 
1240dc24826bSAndy Adamson /*
1241dc24826bSAndy Adamson  * Test if the open context credential key is marked to expire soon.
1242dc24826bSAndy Adamson  */
1243ce52914eSScott Mayhew bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode)
1244dc24826bSAndy Adamson {
1245ce52914eSScott Mayhew 	struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
1246ca05cbaeSTrond Myklebust 	struct rpc_cred *cred, *new, *old = NULL;
1247ddf529eeSNeilBrown 	struct auth_cred acred = {
1248a52458b4SNeilBrown 		.cred = ctx->cred,
1249ddf529eeSNeilBrown 	};
1250ca05cbaeSTrond Myklebust 	bool ret = false;
1251ce52914eSScott Mayhew 
1252ca05cbaeSTrond Myklebust 	rcu_read_lock();
1253ca05cbaeSTrond Myklebust 	cred = rcu_dereference(ctx->ll_cred);
1254ca05cbaeSTrond Myklebust 	if (cred && !(cred->cr_ops->crkey_timeout &&
1255ca05cbaeSTrond Myklebust 		      cred->cr_ops->crkey_timeout(cred)))
1256ca05cbaeSTrond Myklebust 		goto out;
1257ca05cbaeSTrond Myklebust 	rcu_read_unlock();
1258ca05cbaeSTrond Myklebust 
1259ca05cbaeSTrond Myklebust 	new = auth->au_ops->lookup_cred(auth, &acred, 0);
1260ca05cbaeSTrond Myklebust 	if (new == cred) {
1261ca05cbaeSTrond Myklebust 		put_rpccred(new);
1262ddf529eeSNeilBrown 		return true;
1263ca05cbaeSTrond Myklebust 	}
1264ca05cbaeSTrond Myklebust 	if (IS_ERR_OR_NULL(new)) {
1265ca05cbaeSTrond Myklebust 		new = NULL;
1266ca05cbaeSTrond Myklebust 		ret = true;
1267ca05cbaeSTrond Myklebust 	} else if (new->cr_ops->crkey_timeout &&
1268ca05cbaeSTrond Myklebust 		   new->cr_ops->crkey_timeout(new))
1269ca05cbaeSTrond Myklebust 		ret = true;
1270ca05cbaeSTrond Myklebust 
1271ca05cbaeSTrond Myklebust 	rcu_read_lock();
1272ca05cbaeSTrond Myklebust 	old = rcu_dereference_protected(xchg(&ctx->ll_cred,
1273ca05cbaeSTrond Myklebust 					     RCU_INITIALIZER(new)), 1);
1274ca05cbaeSTrond Myklebust out:
1275ca05cbaeSTrond Myklebust 	rcu_read_unlock();
1276ca05cbaeSTrond Myklebust 	put_rpccred(old);
1277ca05cbaeSTrond Myklebust 	return ret;
1278dc24826bSAndy Adamson }
1279dc24826bSAndy Adamson 
1280dc24826bSAndy Adamson /*
12815d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
12825d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
12835d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
12845d47a356STrond Myklebust  */
1285fc9dc401STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode,
1286fc9dc401STrond Myklebust 				   unsigned int pagelen)
12875d47a356STrond Myklebust {
1288d529ef83SJeff Layton 	struct nfs_inode *nfsi = NFS_I(inode);
1289d529ef83SJeff Layton 
12908d197a56STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
12918d197a56STrond Myklebust 		goto out;
1292fc9dc401STrond Myklebust 	if (nfsi->cache_validity &
129313c0b082STrond Myklebust 	    (NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE))
1294d529ef83SJeff Layton 		return false;
12954db72b40SJeff Layton 	smp_rmb();
1296fc9dc401STrond Myklebust 	if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags) && pagelen != 0)
12978d197a56STrond Myklebust 		return false;
12988d197a56STrond Myklebust out:
1299fc9dc401STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_INVALID_DATA && pagelen != 0)
130018dd78c4SScott Mayhew 		return false;
13018d197a56STrond Myklebust 	return PageUptodate(page) != 0;
13025d47a356STrond Myklebust }
13035d47a356STrond Myklebust 
13045263e31eSJeff Layton static bool
13055263e31eSJeff Layton is_whole_file_wrlock(struct file_lock *fl)
13065263e31eSJeff Layton {
13075263e31eSJeff Layton 	return fl->fl_start == 0 && fl->fl_end == OFFSET_MAX &&
13085263e31eSJeff Layton 			fl->fl_type == F_WRLCK;
13095263e31eSJeff Layton }
13105263e31eSJeff Layton 
1311c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or
1312c7559663SScott Mayhew  * if we have the whole file locked for writing), it may be more efficient to
1313c7559663SScott Mayhew  * extend the write to cover the entire page in order to avoid fragmentation
1314c7559663SScott Mayhew  * inefficiencies.
1315c7559663SScott Mayhew  *
1316263b4509SScott Mayhew  * If the file is opened for synchronous writes then we can just skip the rest
1317263b4509SScott Mayhew  * of the checks.
1318c7559663SScott Mayhew  */
1319fc9dc401STrond Myklebust static int nfs_can_extend_write(struct file *file, struct page *page,
1320fc9dc401STrond Myklebust 				struct inode *inode, unsigned int pagelen)
1321c7559663SScott Mayhew {
13225263e31eSJeff Layton 	int ret;
132317b985deSJeff Layton 	struct file_lock_context *flctx = locks_inode_context(inode);
13245263e31eSJeff Layton 	struct file_lock *fl;
13255263e31eSJeff Layton 
1326c7559663SScott Mayhew 	if (file->f_flags & O_DSYNC)
1327c7559663SScott Mayhew 		return 0;
1328fc9dc401STrond Myklebust 	if (!nfs_write_pageuptodate(page, inode, pagelen))
1329263b4509SScott Mayhew 		return 0;
1330c7559663SScott Mayhew 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
1331c7559663SScott Mayhew 		return 1;
1332bd61e0a9SJeff Layton 	if (!flctx || (list_empty_careful(&flctx->flc_flock) &&
1333bd61e0a9SJeff Layton 		       list_empty_careful(&flctx->flc_posix)))
13348fa4592aSTrond Myklebust 		return 1;
13355263e31eSJeff Layton 
13365263e31eSJeff Layton 	/* Check to see if there are whole file write locks */
13375263e31eSJeff Layton 	ret = 0;
13386109c850SJeff Layton 	spin_lock(&flctx->flc_lock);
1339bd61e0a9SJeff Layton 	if (!list_empty(&flctx->flc_posix)) {
1340bd61e0a9SJeff Layton 		fl = list_first_entry(&flctx->flc_posix, struct file_lock,
1341bd61e0a9SJeff Layton 					fl_list);
1342bd61e0a9SJeff Layton 		if (is_whole_file_wrlock(fl))
13435263e31eSJeff Layton 			ret = 1;
1344bd61e0a9SJeff Layton 	} else if (!list_empty(&flctx->flc_flock)) {
13455263e31eSJeff Layton 		fl = list_first_entry(&flctx->flc_flock, struct file_lock,
13465263e31eSJeff Layton 					fl_list);
13475263e31eSJeff Layton 		if (fl->fl_type == F_WRLCK)
13485263e31eSJeff Layton 			ret = 1;
13495263e31eSJeff Layton 	}
13506109c850SJeff Layton 	spin_unlock(&flctx->flc_lock);
13515263e31eSJeff Layton 	return ret;
1352c7559663SScott Mayhew }
1353c7559663SScott Mayhew 
13545d47a356STrond Myklebust /*
13551da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
13561da177e4SLinus Torvalds  *
13571da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
13581da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
13591da177e4SLinus Torvalds  */
13601da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
13611da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
13621da177e4SLinus Torvalds {
1363cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
1364d2ceb7e5SBenjamin Coddington 	struct address_space *mapping = page_file_mapping(page);
1365d2ceb7e5SBenjamin Coddington 	struct inode	*inode = mapping->host;
1366fc9dc401STrond Myklebust 	unsigned int	pagelen = nfs_page_length(page);
13671da177e4SLinus Torvalds 	int		status = 0;
13681da177e4SLinus Torvalds 
136991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
137091d5b470SChuck Lever 
13716de1472fSAl Viro 	dprintk("NFS:       nfs_updatepage(%pD2 %d@%lld)\n",
13726de1472fSAl Viro 		file, count, (long long)(page_file_offset(page) + offset));
13731da177e4SLinus Torvalds 
1374149a4fddSBenjamin Coddington 	if (!count)
1375149a4fddSBenjamin Coddington 		goto out;
1376149a4fddSBenjamin Coddington 
1377fc9dc401STrond Myklebust 	if (nfs_can_extend_write(file, page, inode, pagelen)) {
1378fc9dc401STrond Myklebust 		count = max(count + offset, pagelen);
13791da177e4SLinus Torvalds 		offset = 0;
13801da177e4SLinus Torvalds 	}
13811da177e4SLinus Torvalds 
1382e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
138303fa9e84STrond Myklebust 	if (status < 0)
1384d2ceb7e5SBenjamin Coddington 		nfs_set_pageerror(mapping);
1385149a4fddSBenjamin Coddington out:
138648186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
13871da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
13881da177e4SLinus Torvalds 	return status;
13891da177e4SLinus Torvalds }
13901da177e4SLinus Torvalds 
13913ff7576dSTrond Myklebust static int flush_task_priority(int how)
13921da177e4SLinus Torvalds {
13931da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
13941da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
13951da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
13961da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
13971da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
13981da177e4SLinus Torvalds 	}
13991da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
14001da177e4SLinus Torvalds }
14011da177e4SLinus Torvalds 
1402d45f60c6SWeston Andros Adamson static void nfs_initiate_write(struct nfs_pgio_header *hdr,
1403d45f60c6SWeston Andros Adamson 			       struct rpc_message *msg,
1404abde71f4STom Haynes 			       const struct nfs_rpc_ops *rpc_ops,
14051ed26f33SAnna Schumaker 			       struct rpc_task_setup *task_setup_data, int how)
14061da177e4SLinus Torvalds {
14073ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
14081da177e4SLinus Torvalds 
14098db55a03SNeilBrown 	if (IS_SWAPFILE(hdr->inode))
14108db55a03SNeilBrown 		task_setup_data->flags |= RPC_TASK_SWAPPER;
14111ed26f33SAnna Schumaker 	task_setup_data->priority = priority;
1412fb91fb0eSAnna Schumaker 	rpc_ops->write_setup(hdr, msg, &task_setup_data->rpc_client);
14135bb2a7cbSTrond Myklebust 	trace_nfs_initiate_write(hdr);
1414275acaafSTrond Myklebust }
1415275acaafSTrond Myklebust 
14166d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
14176d884e8fSFred  * call this on each, which will prepare them to be retried on next
14186d884e8fSFred  * writeback using standard nfs.
14196d884e8fSFred  */
14206d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
14216d884e8fSFred {
14226dd85e83STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(nfs_page_to_inode(req));
142367f4b5dcSTrond Myklebust 
142433344e0fSTrond Myklebust 	/* Bump the transmission count */
142533344e0fSTrond Myklebust 	req->wb_nio++;
14266d884e8fSFred 	nfs_mark_request_dirty(req);
142767f4b5dcSTrond Myklebust 	atomic_long_inc(&nfsi->redirtied_pages);
142820633f04SWeston Andros Adamson 	nfs_end_page_writeback(req);
14293aff4ebbSTrond Myklebust 	nfs_release_request(req);
14306d884e8fSFred }
14316d884e8fSFred 
1432df3accb8STrond Myklebust static void nfs_async_write_error(struct list_head *head, int error)
14336c75dc0dSFred Isaman {
14346c75dc0dSFred Isaman 	struct nfs_page	*req;
14356c75dc0dSFred Isaman 
14366c75dc0dSFred Isaman 	while (!list_empty(head)) {
14376c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
14386c75dc0dSFred Isaman 		nfs_list_remove_request(req);
1439cea9ba72STrond Myklebust 		if (nfs_error_is_fatal_on_server(error))
14406fbda89bSTrond Myklebust 			nfs_write_error(req, error);
14416fbda89bSTrond Myklebust 		else
14426c75dc0dSFred Isaman 			nfs_redirty_request(req);
14436c75dc0dSFred Isaman 	}
14446c75dc0dSFred Isaman }
14456c75dc0dSFred Isaman 
1446dc602dd7STrond Myklebust static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr)
1447dc602dd7STrond Myklebust {
1448df3accb8STrond Myklebust 	nfs_async_write_error(&hdr->pages, 0);
1449dc602dd7STrond Myklebust }
1450dc602dd7STrond Myklebust 
1451061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1452919e3bd9STrond Myklebust 	.init_hdr = nfs_async_write_init,
1453061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1454061ae2edSFred Isaman 	.completion = nfs_write_completion,
1455dc602dd7STrond Myklebust 	.reschedule_io = nfs_async_write_reschedule_io,
1456061ae2edSFred Isaman };
1457061ae2edSFred Isaman 
145857208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1459a20c93e3SChristoph Hellwig 			       struct inode *inode, int ioflags, bool force_mds,
1460061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
14611751c363STrond Myklebust {
1462a20c93e3SChristoph Hellwig 	struct nfs_server *server = NFS_SERVER(inode);
146341d8d5b7SAnna Schumaker 	const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
1464a20c93e3SChristoph Hellwig 
1465a20c93e3SChristoph Hellwig #ifdef CONFIG_NFS_V4_1
1466a20c93e3SChristoph Hellwig 	if (server->pnfs_curr_ld && !force_mds)
1467a20c93e3SChristoph Hellwig 		pg_ops = server->pnfs_curr_ld->pg_write_ops;
1468a20c93e3SChristoph Hellwig #endif
14694a0de55cSAnna Schumaker 	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
14703bde7afdSTrond Myklebust 			server->wsize, ioflags);
14711751c363STrond Myklebust }
1472ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
14731751c363STrond Myklebust 
1474dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1475dce81290STrond Myklebust {
1476a7d42ddbSWeston Andros Adamson 	struct nfs_pgio_mirror *mirror;
1477a7d42ddbSWeston Andros Adamson 
14786f29b9bbSKinglong Mee 	if (pgio->pg_ops && pgio->pg_ops->pg_cleanup)
14796f29b9bbSKinglong Mee 		pgio->pg_ops->pg_cleanup(pgio);
14806f29b9bbSKinglong Mee 
148141d8d5b7SAnna Schumaker 	pgio->pg_ops = &nfs_pgio_rw_ops;
1482a7d42ddbSWeston Andros Adamson 
1483a7d42ddbSWeston Andros Adamson 	nfs_pageio_stop_mirroring(pgio);
1484a7d42ddbSWeston Andros Adamson 
1485a7d42ddbSWeston Andros Adamson 	mirror = &pgio->pg_mirrors[0];
1486a7d42ddbSWeston Andros Adamson 	mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1487dce81290STrond Myklebust }
14881f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1489dce81290STrond Myklebust 
14901da177e4SLinus Torvalds 
14910b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
14920b7c0153SFred Isaman {
14930b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
14940b7c0153SFred Isaman 
14950b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
14960b7c0153SFred Isaman }
14970b7c0153SFred Isaman 
1498a08a8cd3STrond Myklebust static void nfs_writeback_check_extend(struct nfs_pgio_header *hdr,
1499a08a8cd3STrond Myklebust 		struct nfs_fattr *fattr)
1500a08a8cd3STrond Myklebust {
1501a08a8cd3STrond Myklebust 	struct nfs_pgio_args *argp = &hdr->args;
1502a08a8cd3STrond Myklebust 	struct nfs_pgio_res *resp = &hdr->res;
15032b83d3deSTrond Myklebust 	u64 size = argp->offset + resp->count;
1504a08a8cd3STrond Myklebust 
1505a08a8cd3STrond Myklebust 	if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
15062b83d3deSTrond Myklebust 		fattr->size = size;
15072b83d3deSTrond Myklebust 	if (nfs_size_to_loff_t(fattr->size) < i_size_read(hdr->inode)) {
15082b83d3deSTrond Myklebust 		fattr->valid &= ~NFS_ATTR_FATTR_SIZE;
1509a08a8cd3STrond Myklebust 		return;
15102b83d3deSTrond Myklebust 	}
15112b83d3deSTrond Myklebust 	if (size != fattr->size)
1512a08a8cd3STrond Myklebust 		return;
1513a08a8cd3STrond Myklebust 	/* Set attribute barrier */
1514a08a8cd3STrond Myklebust 	nfs_fattr_set_barrier(fattr);
15152b83d3deSTrond Myklebust 	/* ...and update size */
15162b83d3deSTrond Myklebust 	fattr->valid |= NFS_ATTR_FATTR_SIZE;
1517a08a8cd3STrond Myklebust }
1518a08a8cd3STrond Myklebust 
1519a08a8cd3STrond Myklebust void nfs_writeback_update_inode(struct nfs_pgio_header *hdr)
1520a08a8cd3STrond Myklebust {
15212b83d3deSTrond Myklebust 	struct nfs_fattr *fattr = &hdr->fattr;
1522a08a8cd3STrond Myklebust 	struct inode *inode = hdr->inode;
1523a08a8cd3STrond Myklebust 
1524a08a8cd3STrond Myklebust 	spin_lock(&inode->i_lock);
1525a08a8cd3STrond Myklebust 	nfs_writeback_check_extend(hdr, fattr);
1526a08a8cd3STrond Myklebust 	nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
1527a08a8cd3STrond Myklebust 	spin_unlock(&inode->i_lock);
1528a08a8cd3STrond Myklebust }
1529a08a8cd3STrond Myklebust EXPORT_SYMBOL_GPL(nfs_writeback_update_inode);
1530a08a8cd3STrond Myklebust 
15311da177e4SLinus Torvalds /*
15321da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
15331da177e4SLinus Torvalds  */
1534d45f60c6SWeston Andros Adamson static int nfs_writeback_done(struct rpc_task *task,
1535d45f60c6SWeston Andros Adamson 			      struct nfs_pgio_header *hdr,
15360eecb214SAnna Schumaker 			      struct inode *inode)
15371da177e4SLinus Torvalds {
1538788e7a89STrond Myklebust 	int status;
15391da177e4SLinus Torvalds 
1540f551e44fSChuck Lever 	/*
1541f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1542f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1543f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1544f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1545f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1546f551e44fSChuck Lever 	 */
1547d45f60c6SWeston Andros Adamson 	status = NFS_PROTO(inode)->write_done(task, hdr);
1548788e7a89STrond Myklebust 	if (status != 0)
15490eecb214SAnna Schumaker 		return status;
15508224b273SChuck Lever 
1551d45f60c6SWeston Andros Adamson 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count);
15525bb2a7cbSTrond Myklebust 	trace_nfs_writeback_done(task, hdr);
155391d5b470SChuck Lever 
155469d96651SJeff Layton 	if (task->tk_status >= 0) {
155569d96651SJeff Layton 		enum nfs3_stable_how committed = hdr->res.verf->committed;
155669d96651SJeff Layton 
155769d96651SJeff Layton 		if (committed == NFS_UNSTABLE) {
155869d96651SJeff Layton 			/*
155969d96651SJeff Layton 			 * We have some uncommitted data on the server at
156069d96651SJeff Layton 			 * this point, so ensure that we keep track of that
156169d96651SJeff Layton 			 * fact irrespective of what later writes do.
156269d96651SJeff Layton 			 */
156369d96651SJeff Layton 			set_bit(NFS_IOHDR_UNSTABLE_WRITES, &hdr->flags);
156469d96651SJeff Layton 		}
156569d96651SJeff Layton 
156669d96651SJeff Layton 		if (committed < hdr->args.stable) {
15671da177e4SLinus Torvalds 			/* We tried a write call, but the server did not
15681da177e4SLinus Torvalds 			 * commit data to stable storage even though we
15691da177e4SLinus Torvalds 			 * requested it.
15701da177e4SLinus Torvalds 			 * Note: There is a known bug in Tru64 < 5.0 in which
15711da177e4SLinus Torvalds 			 *	 the server reports NFS_DATA_SYNC, but performs
15721da177e4SLinus Torvalds 			 *	 NFS_FILE_SYNC. We therefore implement this checking
15731da177e4SLinus Torvalds 			 *	 as a dprintk() in order to avoid filling syslog.
15741da177e4SLinus Torvalds 			 */
15751da177e4SLinus Torvalds 			static unsigned long    complain;
15761da177e4SLinus Torvalds 
1577a69aef14SFred Isaman 			/* Note this will print the MDS for a DS write */
15781da177e4SLinus Torvalds 			if (time_before(complain, jiffies)) {
15791da177e4SLinus Torvalds 				dprintk("NFS:       faulty NFS server %s:"
15801da177e4SLinus Torvalds 					" (committed = %d) != (stable = %d)\n",
1581cd841605SFred Isaman 					NFS_SERVER(inode)->nfs_client->cl_hostname,
158269d96651SJeff Layton 					committed, hdr->args.stable);
15831da177e4SLinus Torvalds 				complain = jiffies + 300 * HZ;
15841da177e4SLinus Torvalds 			}
15851da177e4SLinus Torvalds 		}
158669d96651SJeff Layton 	}
15871f2edbe3STrond Myklebust 
15881f2edbe3STrond Myklebust 	/* Deal with the suid/sgid bit corner case */
158916e14375STrond Myklebust 	if (nfs_should_remove_suid(inode)) {
159016e14375STrond Myklebust 		spin_lock(&inode->i_lock);
1591720869ebSTrond Myklebust 		nfs_set_cache_invalid(inode, NFS_INO_INVALID_MODE);
159216e14375STrond Myklebust 		spin_unlock(&inode->i_lock);
159316e14375STrond Myklebust 	}
15940eecb214SAnna Schumaker 	return 0;
15950eecb214SAnna Schumaker }
15960eecb214SAnna Schumaker 
15970eecb214SAnna Schumaker /*
15980eecb214SAnna Schumaker  * This function is called when the WRITE call is complete.
15990eecb214SAnna Schumaker  */
1600d45f60c6SWeston Andros Adamson static void nfs_writeback_result(struct rpc_task *task,
1601d45f60c6SWeston Andros Adamson 				 struct nfs_pgio_header *hdr)
16020eecb214SAnna Schumaker {
1603d45f60c6SWeston Andros Adamson 	struct nfs_pgio_args	*argp = &hdr->args;
1604d45f60c6SWeston Andros Adamson 	struct nfs_pgio_res	*resp = &hdr->res;
16051f2edbe3STrond Myklebust 
16061f2edbe3STrond Myklebust 	if (resp->count < argp->count) {
16071da177e4SLinus Torvalds 		static unsigned long    complain;
16081da177e4SLinus Torvalds 
16096c75dc0dSFred Isaman 		/* This a short write! */
1610d45f60c6SWeston Andros Adamson 		nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE);
161191d5b470SChuck Lever 
16121da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
16136c75dc0dSFred Isaman 		if (resp->count == 0) {
16146c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
16156c75dc0dSFred Isaman 				printk(KERN_WARNING
16166c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
16176c75dc0dSFred Isaman 				       argp->count);
16186c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
16196c75dc0dSFred Isaman 			}
1620d45f60c6SWeston Andros Adamson 			nfs_set_pgio_error(hdr, -EIO, argp->offset);
16216c75dc0dSFred Isaman 			task->tk_status = -EIO;
16226c75dc0dSFred Isaman 			return;
16236c75dc0dSFred Isaman 		}
1624f8417b48SKinglong Mee 
1625f8417b48SKinglong Mee 		/* For non rpc-based layout drivers, retry-through-MDS */
1626f8417b48SKinglong Mee 		if (!task->tk_ops) {
1627f8417b48SKinglong Mee 			hdr->pnfs_error = -EAGAIN;
1628f8417b48SKinglong Mee 			return;
1629f8417b48SKinglong Mee 		}
1630f8417b48SKinglong Mee 
16311da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
16321da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
16331da177e4SLinus Torvalds 			/* Resend from where the server left off */
1634d45f60c6SWeston Andros Adamson 			hdr->mds_offset += resp->count;
16351da177e4SLinus Torvalds 			argp->offset += resp->count;
16361da177e4SLinus Torvalds 			argp->pgbase += resp->count;
16371da177e4SLinus Torvalds 			argp->count -= resp->count;
16381da177e4SLinus Torvalds 		} else {
16391da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
16401da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
16411da177e4SLinus Torvalds 			 */
16421da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
16431da177e4SLinus Torvalds 		}
16448c9cb714STrond Myklebust 		resp->count = 0;
16458c9cb714STrond Myklebust 		resp->verf->committed = 0;
1646d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
16471da177e4SLinus Torvalds 	}
16481da177e4SLinus Torvalds }
16491da177e4SLinus Torvalds 
1650af7cf057STrond Myklebust static int wait_on_commit(struct nfs_mds_commit_info *cinfo)
165171d0a611STrond Myklebust {
1652723c921eSPeter Zijlstra 	return wait_var_event_killable(&cinfo->rpcs_out,
1653723c921eSPeter Zijlstra 				       !atomic_read(&cinfo->rpcs_out));
1654af7cf057STrond Myklebust }
1655af7cf057STrond Myklebust 
1656af7cf057STrond Myklebust static void nfs_commit_begin(struct nfs_mds_commit_info *cinfo)
1657af7cf057STrond Myklebust {
1658af7cf057STrond Myklebust 	atomic_inc(&cinfo->rpcs_out);
1659af7cf057STrond Myklebust }
1660af7cf057STrond Myklebust 
1661133a48abSTrond Myklebust bool nfs_commit_end(struct nfs_mds_commit_info *cinfo)
1662af7cf057STrond Myklebust {
1663133a48abSTrond Myklebust 	if (atomic_dec_and_test(&cinfo->rpcs_out)) {
1664723c921eSPeter Zijlstra 		wake_up_var(&cinfo->rpcs_out);
1665133a48abSTrond Myklebust 		return true;
1666133a48abSTrond Myklebust 	}
1667133a48abSTrond Myklebust 	return false;
166871d0a611STrond Myklebust }
166971d0a611STrond Myklebust 
16700b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
16711da177e4SLinus Torvalds {
16720b7c0153SFred Isaman 	put_nfs_open_context(data->context);
16730b7c0153SFred Isaman 	nfs_commit_free(data);
16741da177e4SLinus Torvalds }
1675e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
16761da177e4SLinus Torvalds 
16770b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
1678c36aae9aSPeng Tao 			const struct nfs_rpc_ops *nfs_ops,
16799ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
16809f0ec176SAndy Adamson 			int how, int flags)
16811da177e4SLinus Torvalds {
168207737691STrond Myklebust 	struct rpc_task *task;
16839ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1684bdc7f021STrond Myklebust 	struct rpc_message msg = {
1685bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1686bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
16879ace33cdSFred Isaman 		.rpc_cred = data->cred,
1688bdc7f021STrond Myklebust 	};
168984115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
169007737691STrond Myklebust 		.task = &data->task,
16919ace33cdSFred Isaman 		.rpc_client = clnt,
1692bdc7f021STrond Myklebust 		.rpc_message = &msg,
16939ace33cdSFred Isaman 		.callback_ops = call_ops,
169484115e1cSTrond Myklebust 		.callback_data = data,
1695101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
16964fa7ef69STrond Myklebust 		.flags = RPC_TASK_ASYNC | flags,
16973ff7576dSTrond Myklebust 		.priority = priority,
169884115e1cSTrond Myklebust 	};
1699118f09edSOlga Kornievskaia 
1700118f09edSOlga Kornievskaia 	if (nfs_server_capable(data->inode, NFS_CAP_MOVEABLE))
1701118f09edSOlga Kornievskaia 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
1702118f09edSOlga Kornievskaia 
1703788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
1704e9ae1ee2SAnna Schumaker 	nfs_ops->commit_setup(data, &msg, &task_setup_data.rpc_client);
17058224b273SChuck Lever 	trace_nfs_initiate_commit(data);
17061da177e4SLinus Torvalds 
1707b4839ebeSKinglong Mee 	dprintk("NFS: initiated commit call\n");
1708bdc7f021STrond Myklebust 
170907737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1710dbae4c73STrond Myklebust 	if (IS_ERR(task))
1711dbae4c73STrond Myklebust 		return PTR_ERR(task);
1712d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1713d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
171407737691STrond Myklebust 	rpc_put_task(task);
1715dbae4c73STrond Myklebust 	return 0;
17161da177e4SLinus Torvalds }
1717e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
17181da177e4SLinus Torvalds 
1719378520b8SPeng Tao static loff_t nfs_get_lwb(struct list_head *head)
1720378520b8SPeng Tao {
1721378520b8SPeng Tao 	loff_t lwb = 0;
1722378520b8SPeng Tao 	struct nfs_page *req;
1723378520b8SPeng Tao 
1724378520b8SPeng Tao 	list_for_each_entry(req, head, wb_list)
1725378520b8SPeng Tao 		if (lwb < (req_offset(req) + req->wb_bytes))
1726378520b8SPeng Tao 			lwb = req_offset(req) + req->wb_bytes;
1727378520b8SPeng Tao 
1728378520b8SPeng Tao 	return lwb;
1729378520b8SPeng Tao }
1730378520b8SPeng Tao 
17311da177e4SLinus Torvalds /*
17329ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
17339ace33cdSFred Isaman  */
17340b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1735988b6dceSFred Isaman 		     struct list_head *head,
1736f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1737f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
17389ace33cdSFred Isaman {
173919573c93STrond Myklebust 	struct nfs_page *first;
174019573c93STrond Myklebust 	struct nfs_open_context *ctx;
174119573c93STrond Myklebust 	struct inode *inode;
17429ace33cdSFred Isaman 
17439ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
17449ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
17459ace33cdSFred Isaman 
174619573c93STrond Myklebust 	if (head)
17479ace33cdSFred Isaman 		list_splice_init(head, &data->pages);
17489ace33cdSFred Isaman 
174919573c93STrond Myklebust 	first = nfs_list_entry(data->pages.next);
175019573c93STrond Myklebust 	ctx = nfs_req_openctx(first);
175119573c93STrond Myklebust 	inode = d_inode(ctx->dentry);
175219573c93STrond Myklebust 
17539ace33cdSFred Isaman 	data->inode	  = inode;
17549fcd5960STrond Myklebust 	data->cred	  = ctx->cred;
1755988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
1756378520b8SPeng Tao 	/* only set lwb for pnfs commit */
1757378520b8SPeng Tao 	if (lseg)
1758378520b8SPeng Tao 		data->lwb = nfs_get_lwb(&data->pages);
17599ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1760f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1761b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
17629ace33cdSFred Isaman 
17639ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
17649ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
17659ace33cdSFred Isaman 	data->args.offset = 0;
17669ace33cdSFred Isaman 	data->args.count  = 0;
17679fcd5960STrond Myklebust 	data->context     = get_nfs_open_context(ctx);
17689ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
17699ace33cdSFred Isaman 	data->res.verf    = &data->verf;
17709ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
1771133a48abSTrond Myklebust 	nfs_commit_begin(cinfo->mds);
17729ace33cdSFred Isaman }
1773e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
17749ace33cdSFred Isaman 
1775e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1776ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1777b57ff130SWeston Andros Adamson 		      struct nfs_commit_info *cinfo,
1778b57ff130SWeston Andros Adamson 		      u32 ds_commit_idx)
177964bfeb49SFred Isaman {
178064bfeb49SFred Isaman 	struct nfs_page *req;
178164bfeb49SFred Isaman 
178264bfeb49SFred Isaman 	while (!list_empty(page_list)) {
178364bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
178464bfeb49SFred Isaman 		nfs_list_remove_request(req);
1785b57ff130SWeston Andros Adamson 		nfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx);
1786487b9b8aSTom Haynes 		if (!cinfo->dreq)
1787487b9b8aSTom Haynes 			nfs_clear_page_commit(req->wb_page);
17881d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
178964bfeb49SFred Isaman 	}
179064bfeb49SFred Isaman }
1791e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
179264bfeb49SFred Isaman 
1793b20135d0STrond Myklebust static void
1794b20135d0STrond Myklebust nfs_commit_resched_write(struct nfs_commit_info *cinfo,
1795b20135d0STrond Myklebust 		struct nfs_page *req)
1796b20135d0STrond Myklebust {
1797b20135d0STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
1798b20135d0STrond Myklebust }
1799b20135d0STrond Myklebust 
18009ace33cdSFred Isaman /*
18011da177e4SLinus Torvalds  * Commit dirty pages
18021da177e4SLinus Torvalds  */
18031da177e4SLinus Torvalds static int
1804ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1805ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
18061da177e4SLinus Torvalds {
18070b7c0153SFred Isaman 	struct nfs_commit_data	*data;
180885e39feeSOlga Kornievskaia 	unsigned short task_flags = 0;
18091da177e4SLinus Torvalds 
1810ade8febdSWeston Andros Adamson 	/* another commit raced with us */
1811ade8febdSWeston Andros Adamson 	if (list_empty(head))
1812ade8febdSWeston Andros Adamson 		return 0;
1813ade8febdSWeston Andros Adamson 
1814515dcdcdSTrond Myklebust 	data = nfs_commitdata_alloc();
1815515dcdcdSTrond Myklebust 	if (!data) {
1816515dcdcdSTrond Myklebust 		nfs_retry_commit(head, NULL, cinfo, -1);
1817515dcdcdSTrond Myklebust 		return -ENOMEM;
1818515dcdcdSTrond Myklebust 	}
18191da177e4SLinus Torvalds 
18201da177e4SLinus Torvalds 	/* Set up the argument struct */
1821f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
182285e39feeSOlga Kornievskaia 	if (NFS_SERVER(inode)->nfs_client->cl_minorversion)
182385e39feeSOlga Kornievskaia 		task_flags = RPC_TASK_MOVEABLE;
1824c36aae9aSPeng Tao 	return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode),
182585e39feeSOlga Kornievskaia 				   data->mds_ops, how,
182685e39feeSOlga Kornievskaia 				   RPC_TASK_CRED_NOREF | task_flags);
18271da177e4SLinus Torvalds }
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds /*
18301da177e4SLinus Torvalds  * COMMIT call returned
18311da177e4SLinus Torvalds  */
1832788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
18331da177e4SLinus Torvalds {
18340b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
18351da177e4SLinus Torvalds 
1836788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1837c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
18387bdd297eSTrond Myklebust 	trace_nfs_commit_done(task, data);
1839c9d8f89dSTrond Myklebust }
1840c9d8f89dSTrond Myklebust 
1841f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1842c9d8f89dSTrond Myklebust {
1843221203ceSTrond Myklebust 	const struct nfs_writeverf *verf = data->res.verf;
1844c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1845c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1846f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
1847353db796SNeilBrown 	struct nfs_server *nfss;
1848788e7a89STrond Myklebust 
18491da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
18501da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
18511da177e4SLinus Torvalds 		nfs_list_remove_request(req);
185267911c8fSAnna Schumaker 		if (req->wb_page)
1853d6d6dc7cSFred Isaman 			nfs_clear_page_commit(req->wb_page);
18541da177e4SLinus Torvalds 
18551e8968c5SNiels de Vos 		dprintk("NFS:       commit (%s/%llu %d@%lld)",
18569fcd5960STrond Myklebust 			nfs_req_openctx(req)->dentry->d_sb->s_id,
18579fcd5960STrond Myklebust 			(unsigned long long)NFS_FILEID(d_inode(nfs_req_openctx(req)->dentry)),
18581da177e4SLinus Torvalds 			req->wb_bytes,
18591da177e4SLinus Torvalds 			(long long)req_offset(req));
1860c9d8f89dSTrond Myklebust 		if (status < 0) {
18616fbda89bSTrond Myklebust 			if (req->wb_page) {
1862af887e43STrond Myklebust 				trace_nfs_commit_error(data->inode, req,
1863af887e43STrond Myklebust 						       status);
18646fbda89bSTrond Myklebust 				nfs_mapping_set_error(req->wb_page, status);
18651da177e4SLinus Torvalds 				nfs_inode_remove_request(req);
18666fbda89bSTrond Myklebust 			}
1867ddeaa637SJoe Perches 			dprintk_cont(", error = %d\n", status);
18681da177e4SLinus Torvalds 			goto next;
18691da177e4SLinus Torvalds 		}
18701da177e4SLinus Torvalds 
18711da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
18721da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
18731f28476dSTrond Myklebust 		if (nfs_write_match_verf(verf, req)) {
18741da177e4SLinus Torvalds 			/* We have a match */
187538a33101SKinglong Mee 			if (req->wb_page)
18761da177e4SLinus Torvalds 				nfs_inode_remove_request(req);
1877ddeaa637SJoe Perches 			dprintk_cont(" OK\n");
18781da177e4SLinus Torvalds 			goto next;
18791da177e4SLinus Torvalds 		}
18801da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
1881ddeaa637SJoe Perches 		dprintk_cont(" mismatch\n");
18826d884e8fSFred 		nfs_mark_request_dirty(req);
188367f4b5dcSTrond Myklebust 		atomic_long_inc(&NFS_I(data->inode)->redirtied_pages);
18841da177e4SLinus Torvalds 	next:
18851d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
18867f1bda44STrond Myklebust 		/* Latency breaker */
18877f1bda44STrond Myklebust 		cond_resched();
18881da177e4SLinus Torvalds 	}
1889353db796SNeilBrown 	nfss = NFS_SERVER(data->inode);
1890353db796SNeilBrown 	if (atomic_long_read(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
18916df25e58SNeilBrown 		nfss->write_congested = 0;
1892353db796SNeilBrown 
1893f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1894af7cf057STrond Myklebust 	nfs_commit_end(cinfo.mds);
18955917ce84SFred Isaman }
18965917ce84SFred Isaman 
18975917ce84SFred Isaman static void nfs_commit_release(void *calldata)
18985917ce84SFred Isaman {
18990b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
19005917ce84SFred Isaman 
1901f453a54aSFred Isaman 	data->completion_ops->completion(data);
1902c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
19031da177e4SLinus Torvalds }
1904788e7a89STrond Myklebust 
1905788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
19060b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1907788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1908788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1909788e7a89STrond Myklebust };
19101da177e4SLinus Torvalds 
1911f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1912f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1913b20135d0STrond Myklebust 	.resched_write = nfs_commit_resched_write,
1914f453a54aSFred Isaman };
1915f453a54aSFred Isaman 
19161763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1917ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
191884c53ab5SFred Isaman {
191984c53ab5SFred Isaman 	int status;
192084c53ab5SFred Isaman 
1921ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
192284c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1923ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
192484c53ab5SFred Isaman 	return status;
192584c53ab5SFred Isaman }
192684c53ab5SFred Isaman 
1927c4f24df9STrond Myklebust static int __nfs_commit_inode(struct inode *inode, int how,
1928c4f24df9STrond Myklebust 		struct writeback_control *wbc)
19291da177e4SLinus Torvalds {
19301da177e4SLinus Torvalds 	LIST_HEAD(head);
1931ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
193271d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1933c4f24df9STrond Myklebust 	int ret, nscan;
19341da177e4SLinus Torvalds 
193564a93dbfSTrond Myklebust 	how &= ~FLUSH_SYNC;
1936ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1937af7cf057STrond Myklebust 	nfs_commit_begin(cinfo.mds);
1938c4f24df9STrond Myklebust 	for (;;) {
1939c4f24df9STrond Myklebust 		ret = nscan = nfs_scan_commit(inode, &head, &cinfo);
1940c4f24df9STrond Myklebust 		if (ret <= 0)
1941c4f24df9STrond Myklebust 			break;
1942c4f24df9STrond Myklebust 		ret = nfs_generic_commit_list(inode, &head, how, &cinfo);
1943c4f24df9STrond Myklebust 		if (ret < 0)
1944c4f24df9STrond Myklebust 			break;
1945c4f24df9STrond Myklebust 		ret = 0;
1946c4f24df9STrond Myklebust 		if (wbc && wbc->sync_mode == WB_SYNC_NONE) {
1947c4f24df9STrond Myklebust 			if (nscan < wbc->nr_to_write)
1948c4f24df9STrond Myklebust 				wbc->nr_to_write -= nscan;
1949c4f24df9STrond Myklebust 			else
1950c4f24df9STrond Myklebust 				wbc->nr_to_write = 0;
1951c4f24df9STrond Myklebust 		}
1952c4f24df9STrond Myklebust 		if (nscan < INT_MAX)
1953c4f24df9STrond Myklebust 			break;
1954c4f24df9STrond Myklebust 		cond_resched();
1955c4f24df9STrond Myklebust 	}
1956af7cf057STrond Myklebust 	nfs_commit_end(cinfo.mds);
1957c4f24df9STrond Myklebust 	if (ret || !may_wait)
1958c4f24df9STrond Myklebust 		return ret;
1959c4f24df9STrond Myklebust 	return wait_on_commit(cinfo.mds);
1960c4f24df9STrond Myklebust }
1961c4f24df9STrond Myklebust 
1962c4f24df9STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
1963c4f24df9STrond Myklebust {
1964c4f24df9STrond Myklebust 	return __nfs_commit_inode(inode, how, NULL);
19651da177e4SLinus Torvalds }
1966b20135d0STrond Myklebust EXPORT_SYMBOL_GPL(nfs_commit_inode);
19678fc795f7STrond Myklebust 
1968ae09c31fSAnna Schumaker int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
19698fc795f7STrond Myklebust {
1970420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1971420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1972420e3646STrond Myklebust 	int ret = 0;
19738fc795f7STrond Myklebust 
1974c4f24df9STrond Myklebust 	if (wbc->sync_mode == WB_SYNC_NONE) {
19753236c3e1SJeff Layton 		/* no commits means nothing needs to be done */
19765cb953d4STrond Myklebust 		if (!atomic_long_read(&nfsi->commit_info.ncommit))
1977c4f24df9STrond Myklebust 			goto check_requests_outstanding;
19783236c3e1SJeff Layton 
1979a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1980a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1981420e3646STrond Myklebust 		 */
19821a4edf0fSTrond Myklebust 		if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1983420e3646STrond Myklebust 			goto out_mark_dirty;
1984420e3646STrond Myklebust 
1985a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1986420e3646STrond Myklebust 		flags = 0;
1987a00dd6c0SJeff Layton 	}
1988a00dd6c0SJeff Layton 
1989c4f24df9STrond Myklebust 	ret = __nfs_commit_inode(inode, flags, wbc);
1990c4f24df9STrond Myklebust 	if (!ret) {
1991c4f24df9STrond Myklebust 		if (flags & FLUSH_SYNC)
19928fc795f7STrond Myklebust 			return 0;
1993c4f24df9STrond Myklebust 	} else if (atomic_long_read(&nfsi->commit_info.ncommit))
1994c4f24df9STrond Myklebust 		goto out_mark_dirty;
1995c4f24df9STrond Myklebust 
1996c4f24df9STrond Myklebust check_requests_outstanding:
1997c4f24df9STrond Myklebust 	if (!atomic_read(&nfsi->commit_info.rpcs_out))
1998c4f24df9STrond Myklebust 		return ret;
1999420e3646STrond Myklebust out_mark_dirty:
20008fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
20018fc795f7STrond Myklebust 	return ret;
20028fc795f7STrond Myklebust }
200389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode);
2004863a3c6cSAndy Adamson 
2005acdc53b2STrond Myklebust /*
2006837bb1d7STrond Myklebust  * Wrapper for filemap_write_and_wait_range()
2007837bb1d7STrond Myklebust  *
2008837bb1d7STrond Myklebust  * Needed for pNFS in order to ensure data becomes visible to the
2009837bb1d7STrond Myklebust  * client.
2010837bb1d7STrond Myklebust  */
2011837bb1d7STrond Myklebust int nfs_filemap_write_and_wait_range(struct address_space *mapping,
2012837bb1d7STrond Myklebust 		loff_t lstart, loff_t lend)
2013837bb1d7STrond Myklebust {
2014837bb1d7STrond Myklebust 	int ret;
2015837bb1d7STrond Myklebust 
2016837bb1d7STrond Myklebust 	ret = filemap_write_and_wait_range(mapping, lstart, lend);
2017837bb1d7STrond Myklebust 	if (ret == 0)
2018837bb1d7STrond Myklebust 		ret = pnfs_sync_inode(mapping->host, true);
2019837bb1d7STrond Myklebust 	return ret;
2020837bb1d7STrond Myklebust }
2021837bb1d7STrond Myklebust EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range);
2022837bb1d7STrond Myklebust 
2023837bb1d7STrond Myklebust /*
2024acdc53b2STrond Myklebust  * flush the inode to disk.
2025acdc53b2STrond Myklebust  */
2026acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
202734901f70STrond Myklebust {
2028f4ce1299STrond Myklebust 	int ret;
202934901f70STrond Myklebust 
2030f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_enter(inode);
2031f4ce1299STrond Myklebust 
20325bb89b47STrond Myklebust 	ret = filemap_write_and_wait(inode->i_mapping);
20336b196875SChuck Lever 	if (ret)
20346b196875SChuck Lever 		goto out;
20355bb89b47STrond Myklebust 	ret = nfs_commit_inode(inode, FLUSH_SYNC);
20366b196875SChuck Lever 	if (ret < 0)
20376b196875SChuck Lever 		goto out;
20385bb89b47STrond Myklebust 	pnfs_sync_inode(inode, true);
20396b196875SChuck Lever 	ret = 0;
2040f4ce1299STrond Myklebust 
20416b196875SChuck Lever out:
2042f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_exit(inode, ret);
2043f4ce1299STrond Myklebust 	return ret;
20441c75950bSTrond Myklebust }
2045ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all);
20461c75950bSTrond Myklebust 
20476d740c76SMatthew Wilcox (Oracle) int nfs_wb_folio_cancel(struct inode *inode, struct folio *folio)
20481b3b4a1aSTrond Myklebust {
20491b3b4a1aSTrond Myklebust 	struct nfs_page *req;
20501b3b4a1aSTrond Myklebust 	int ret = 0;
20511b3b4a1aSTrond Myklebust 
20526d740c76SMatthew Wilcox (Oracle) 	folio_wait_writeback(folio);
20533e217045SWeston Andros Adamson 
20543e217045SWeston Andros Adamson 	/* blocking call to cancel all requests and join to a single (head)
20553e217045SWeston Andros Adamson 	 * request */
20566d740c76SMatthew Wilcox (Oracle) 	req = nfs_lock_and_join_requests(&folio->page);
20573e217045SWeston Andros Adamson 
20583e217045SWeston Andros Adamson 	if (IS_ERR(req)) {
20593e217045SWeston Andros Adamson 		ret = PTR_ERR(req);
20603e217045SWeston Andros Adamson 	} else if (req) {
20616d740c76SMatthew Wilcox (Oracle) 		/* all requests from this folio have been cancelled by
20623e217045SWeston Andros Adamson 		 * nfs_lock_and_join_requests, so just remove the head
20633e217045SWeston Andros Adamson 		 * request from the inode / page_private pointer and
20643e217045SWeston Andros Adamson 		 * release it */
20651b3b4a1aSTrond Myklebust 		nfs_inode_remove_request(req);
20661d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
20671b3b4a1aSTrond Myklebust 	}
20683e217045SWeston Andros Adamson 
20691b3b4a1aSTrond Myklebust 	return ret;
20701b3b4a1aSTrond Myklebust }
20711b3b4a1aSTrond Myklebust 
2072*5241060eSTrond Myklebust /**
2073*5241060eSTrond Myklebust  * nfs_wb_folio - Write back all requests on one page
2074*5241060eSTrond Myklebust  * @inode: pointer to page
2075*5241060eSTrond Myklebust  * @folio: pointer to folio
2076*5241060eSTrond Myklebust  *
2077*5241060eSTrond Myklebust  * Assumes that the folio has been locked by the caller, and will
2078*5241060eSTrond Myklebust  * not unlock it.
20791c75950bSTrond Myklebust  */
2080*5241060eSTrond Myklebust int nfs_wb_folio(struct inode *inode, struct folio *folio)
20811c75950bSTrond Myklebust {
2082*5241060eSTrond Myklebust 	loff_t range_start = folio_file_pos(folio);
2083*5241060eSTrond Myklebust 	loff_t range_end = range_start + (loff_t)folio_size(folio) - 1;
20847f2f12d9STrond Myklebust 	struct writeback_control wbc = {
20857f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
20867f2f12d9STrond Myklebust 		.nr_to_write = 0,
20877f2f12d9STrond Myklebust 		.range_start = range_start,
20887f2f12d9STrond Myklebust 		.range_end = range_end,
20897f2f12d9STrond Myklebust 	};
20907f2f12d9STrond Myklebust 	int ret;
20917f2f12d9STrond Myklebust 
2092f4ce1299STrond Myklebust 	trace_nfs_writeback_page_enter(inode);
2093f4ce1299STrond Myklebust 
20940522f6adSTrond Myklebust 	for (;;) {
2095*5241060eSTrond Myklebust 		folio_wait_writeback(folio);
2096*5241060eSTrond Myklebust 		if (folio_clear_dirty_for_io(folio)) {
2097*5241060eSTrond Myklebust 			ret = nfs_writepage_locked(&folio->page, &wbc);
20987f2f12d9STrond Myklebust 			if (ret < 0)
20997f2f12d9STrond Myklebust 				goto out_error;
21000522f6adSTrond Myklebust 			continue;
21017f2f12d9STrond Myklebust 		}
2102f4ce1299STrond Myklebust 		ret = 0;
2103*5241060eSTrond Myklebust 		if (!folio_test_private(folio))
21040522f6adSTrond Myklebust 			break;
21050522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
21067f2f12d9STrond Myklebust 		if (ret < 0)
21077f2f12d9STrond Myklebust 			goto out_error;
21087f2f12d9STrond Myklebust 	}
21097f2f12d9STrond Myklebust out_error:
2110f4ce1299STrond Myklebust 	trace_nfs_writeback_page_exit(inode, ret);
21117f2f12d9STrond Myklebust 	return ret;
21121c75950bSTrond Myklebust }
21131c75950bSTrond Myklebust 
2114*5241060eSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
21154b27232aSTrond Myklebust {
2116*5241060eSTrond Myklebust 	return nfs_wb_folio(inode, page_folio(page));
21174b27232aSTrond Myklebust }
21184b27232aSTrond Myklebust 
2119074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
21204ae84a80SMatthew Wilcox (Oracle) int nfs_migrate_folio(struct address_space *mapping, struct folio *dst,
21214ae84a80SMatthew Wilcox (Oracle) 		struct folio *src, enum migrate_mode mode)
2122074cc1deSTrond Myklebust {
21232da95652SJeff Layton 	/*
21244ae84a80SMatthew Wilcox (Oracle) 	 * If the private flag is set, the folio is currently associated with
21252da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
21262da95652SJeff Layton 	 *
21272da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
21282da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
21294ae84a80SMatthew Wilcox (Oracle) 	 *        the folio lock.
21302da95652SJeff Layton 	 */
21314ae84a80SMatthew Wilcox (Oracle) 	if (folio_test_private(src))
21322da95652SJeff Layton 		return -EBUSY;
2133074cc1deSTrond Myklebust 
21344ae84a80SMatthew Wilcox (Oracle) 	if (folio_test_fscache(src)) {
213516f2f4e6SDavid Howells 		if (mode == MIGRATE_ASYNC)
21368c209ce7SDavid Howells 			return -EBUSY;
21374ae84a80SMatthew Wilcox (Oracle) 		folio_wait_fscache(src);
213816f2f4e6SDavid Howells 	}
2139074cc1deSTrond Myklebust 
214054184650SMatthew Wilcox (Oracle) 	return migrate_folio(mapping, dst, src, mode);
2141074cc1deSTrond Myklebust }
2142074cc1deSTrond Myklebust #endif
2143074cc1deSTrond Myklebust 
2144f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
21451da177e4SLinus Torvalds {
21461da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
21471e7f3a48SWeston Andros Adamson 					     sizeof(struct nfs_pgio_header),
21481da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
214920c2df83SPaul Mundt 					     NULL);
21501da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
21511da177e4SLinus Torvalds 		return -ENOMEM;
21521da177e4SLinus Torvalds 
215393d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
21541da177e4SLinus Torvalds 						     nfs_wdata_cachep);
21551da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
21563dd4765fSJeff Layton 		goto out_destroy_write_cache;
21571da177e4SLinus Torvalds 
21580b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
21590b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
21600b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
21610b7c0153SFred Isaman 					     NULL);
21620b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
21633dd4765fSJeff Layton 		goto out_destroy_write_mempool;
21640b7c0153SFred Isaman 
216593d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
21664c100210SYanchuan Nian 						      nfs_cdata_cachep);
21671da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
21683dd4765fSJeff Layton 		goto out_destroy_commit_cache;
21691da177e4SLinus Torvalds 
217089a09141SPeter Zijlstra 	/*
217189a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
217289a09141SPeter Zijlstra 	 *
217389a09141SPeter Zijlstra 	 *  64MB:    8192k
217489a09141SPeter Zijlstra 	 * 128MB:   11585k
217589a09141SPeter Zijlstra 	 * 256MB:   16384k
217689a09141SPeter Zijlstra 	 * 512MB:   23170k
217789a09141SPeter Zijlstra 	 *   1GB:   32768k
217889a09141SPeter Zijlstra 	 *   2GB:   46340k
217989a09141SPeter Zijlstra 	 *   4GB:   65536k
218089a09141SPeter Zijlstra 	 *   8GB:   92681k
218189a09141SPeter Zijlstra 	 *  16GB:  131072k
218289a09141SPeter Zijlstra 	 *
218389a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
218489a09141SPeter Zijlstra 	 * Limit the default to 256M
218589a09141SPeter Zijlstra 	 */
2186ca79b0c2SArun KS 	nfs_congestion_kb = (16*int_sqrt(totalram_pages())) << (PAGE_SHIFT-10);
218789a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
218889a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
218989a09141SPeter Zijlstra 
21901da177e4SLinus Torvalds 	return 0;
21913dd4765fSJeff Layton 
21923dd4765fSJeff Layton out_destroy_commit_cache:
21933dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
21943dd4765fSJeff Layton out_destroy_write_mempool:
21953dd4765fSJeff Layton 	mempool_destroy(nfs_wdata_mempool);
21963dd4765fSJeff Layton out_destroy_write_cache:
21973dd4765fSJeff Layton 	kmem_cache_destroy(nfs_wdata_cachep);
21983dd4765fSJeff Layton 	return -ENOMEM;
21991da177e4SLinus Torvalds }
22001da177e4SLinus Torvalds 
2201266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
22021da177e4SLinus Torvalds {
22031da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
22043dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
22051da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
22061a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
22071da177e4SLinus Torvalds }
22081da177e4SLinus Torvalds 
22094a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops = {
22104a0de55cSAnna Schumaker 	.rw_alloc_header	= nfs_writehdr_alloc,
22114a0de55cSAnna Schumaker 	.rw_free_header		= nfs_writehdr_free,
22120eecb214SAnna Schumaker 	.rw_done		= nfs_writeback_done,
22130eecb214SAnna Schumaker 	.rw_result		= nfs_writeback_result,
22141ed26f33SAnna Schumaker 	.rw_initiate		= nfs_initiate_write,
22154a0de55cSAnna Schumaker };
2216