xref: /linux/fs/nfs/write.c (revision 4e857c58efeb99393cba5a5d0d8ec7117183137c)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/fs/nfs/write.c
31da177e4SLinus Torvalds  *
47c85d900STrond Myklebust  * Write file data over NFS.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/types.h>
101da177e4SLinus Torvalds #include <linux/slab.h>
111da177e4SLinus Torvalds #include <linux/mm.h>
121da177e4SLinus Torvalds #include <linux/pagemap.h>
131da177e4SLinus Torvalds #include <linux/file.h>
141da177e4SLinus Torvalds #include <linux/writeback.h>
1589a09141SPeter Zijlstra #include <linux/swap.h>
16074cc1deSTrond Myklebust #include <linux/migrate.h>
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
191da177e4SLinus Torvalds #include <linux/nfs_fs.h>
201da177e4SLinus Torvalds #include <linux/nfs_mount.h>
211da177e4SLinus Torvalds #include <linux/nfs_page.h>
223fcfab16SAndrew Morton #include <linux/backing-dev.h>
23afeacc8cSPaul Gortmaker #include <linux/export.h>
243fcfab16SAndrew Morton 
251da177e4SLinus Torvalds #include <asm/uaccess.h>
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #include "delegation.h"
2849a70f27STrond Myklebust #include "internal.h"
2991d5b470SChuck Lever #include "iostat.h"
30def6ed7eSAndy Adamson #include "nfs4_fs.h"
31074cc1deSTrond Myklebust #include "fscache.h"
3294ad1c80SFred Isaman #include "pnfs.h"
331da177e4SLinus Torvalds 
34f4ce1299STrond Myklebust #include "nfstrace.h"
35f4ce1299STrond Myklebust 
361da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #define MIN_POOL_WRITE		(32)
391da177e4SLinus Torvalds #define MIN_POOL_COMMIT		(4)
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds /*
421da177e4SLinus Torvalds  * Local function declarations
431da177e4SLinus Torvalds  */
44f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req);
456c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops;
46788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops;
47061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops;
48f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops;
491da177e4SLinus Torvalds 
50e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep;
513feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
520b7c0153SFred Isaman static struct kmem_cache *nfs_cdata_cachep;
531da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
541da177e4SLinus Torvalds 
550b7c0153SFred Isaman struct nfs_commit_data *nfs_commitdata_alloc(void)
561da177e4SLinus Torvalds {
57192e501bSMel Gorman 	struct nfs_commit_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOIO);
5840859d7eSChuck Lever 
591da177e4SLinus Torvalds 	if (p) {
601da177e4SLinus Torvalds 		memset(p, 0, sizeof(*p));
611da177e4SLinus Torvalds 		INIT_LIST_HEAD(&p->pages);
621da177e4SLinus Torvalds 	}
631da177e4SLinus Torvalds 	return p;
641da177e4SLinus Torvalds }
65e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
661da177e4SLinus Torvalds 
670b7c0153SFred Isaman void nfs_commit_free(struct nfs_commit_data *p)
681da177e4SLinus Torvalds {
691da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
701da177e4SLinus Torvalds }
71e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free);
721da177e4SLinus Torvalds 
736c75dc0dSFred Isaman struct nfs_write_header *nfs_writehdr_alloc(void)
743feb2d49STrond Myklebust {
75192e501bSMel Gorman 	struct nfs_write_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOIO);
763feb2d49STrond Myklebust 
773feb2d49STrond Myklebust 	if (p) {
78cd841605SFred Isaman 		struct nfs_pgio_header *hdr = &p->header;
79cd841605SFred Isaman 
803feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
81cd841605SFred Isaman 		INIT_LIST_HEAD(&hdr->pages);
826c75dc0dSFred Isaman 		INIT_LIST_HEAD(&hdr->rpc_list);
836c75dc0dSFred Isaman 		spin_lock_init(&hdr->lock);
846c75dc0dSFred Isaman 		atomic_set(&hdr->refcnt, 0);
859bce008bSTrond Myklebust 		hdr->verf = &p->verf;
863feb2d49STrond Myklebust 	}
873feb2d49STrond Myklebust 	return p;
883feb2d49STrond Myklebust }
8989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_writehdr_alloc);
903feb2d49STrond Myklebust 
911763da12SFred Isaman static struct nfs_write_data *nfs_writedata_alloc(struct nfs_pgio_header *hdr,
926c75dc0dSFred Isaman 						  unsigned int pagecount)
936c75dc0dSFred Isaman {
946c75dc0dSFred Isaman 	struct nfs_write_data *data, *prealloc;
956c75dc0dSFred Isaman 
966c75dc0dSFred Isaman 	prealloc = &container_of(hdr, struct nfs_write_header, header)->rpc_data;
976c75dc0dSFred Isaman 	if (prealloc->header == NULL)
986c75dc0dSFred Isaman 		data = prealloc;
996c75dc0dSFred Isaman 	else
1006c75dc0dSFred Isaman 		data = kzalloc(sizeof(*data), GFP_KERNEL);
1016c75dc0dSFred Isaman 	if (!data)
1026c75dc0dSFred Isaman 		goto out;
1036c75dc0dSFred Isaman 
1046c75dc0dSFred Isaman 	if (nfs_pgarray_set(&data->pages, pagecount)) {
1056c75dc0dSFred Isaman 		data->header = hdr;
1066c75dc0dSFred Isaman 		atomic_inc(&hdr->refcnt);
1076c75dc0dSFred Isaman 	} else {
1086c75dc0dSFred Isaman 		if (data != prealloc)
1096c75dc0dSFred Isaman 			kfree(data);
1106c75dc0dSFred Isaman 		data = NULL;
1116c75dc0dSFred Isaman 	}
1126c75dc0dSFred Isaman out:
1136c75dc0dSFred Isaman 	return data;
1146c75dc0dSFred Isaman }
1156c75dc0dSFred Isaman 
116cd841605SFred Isaman void nfs_writehdr_free(struct nfs_pgio_header *hdr)
1173feb2d49STrond Myklebust {
118cd841605SFred Isaman 	struct nfs_write_header *whdr = container_of(hdr, struct nfs_write_header, header);
119cd841605SFred Isaman 	mempool_free(whdr, nfs_wdata_mempool);
1203feb2d49STrond Myklebust }
12189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_writehdr_free);
1223feb2d49STrond Myklebust 
123dce81290STrond Myklebust void nfs_writedata_release(struct nfs_write_data *wdata)
1241da177e4SLinus Torvalds {
1256c75dc0dSFred Isaman 	struct nfs_pgio_header *hdr = wdata->header;
1266c75dc0dSFred Isaman 	struct nfs_write_header *write_header = container_of(hdr, struct nfs_write_header, header);
1276c75dc0dSFred Isaman 
128383ba719STrond Myklebust 	put_nfs_open_context(wdata->args.context);
12930dd374fSFred Isaman 	if (wdata->pages.pagevec != wdata->pages.page_array)
13030dd374fSFred Isaman 		kfree(wdata->pages.pagevec);
1316db6dd7dSTrond Myklebust 	if (wdata == &write_header->rpc_data) {
1326c75dc0dSFred Isaman 		wdata->header = NULL;
1336db6dd7dSTrond Myklebust 		wdata = NULL;
1346db6dd7dSTrond Myklebust 	}
1356c75dc0dSFred Isaman 	if (atomic_dec_and_test(&hdr->refcnt))
136061ae2edSFred Isaman 		hdr->completion_ops->completion(hdr);
1376db6dd7dSTrond Myklebust 	/* Note: we only free the rpc_task after callbacks are done.
1386db6dd7dSTrond Myklebust 	 * See the comment in rpc_free_task() for why
1396db6dd7dSTrond Myklebust 	 */
1406db6dd7dSTrond Myklebust 	kfree(wdata);
1411da177e4SLinus Torvalds }
14289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_writedata_release);
1431da177e4SLinus Torvalds 
1447b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
1457b159fc1STrond Myklebust {
1467b159fc1STrond Myklebust 	ctx->error = error;
1477b159fc1STrond Myklebust 	smp_wmb();
1487b159fc1STrond Myklebust 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
1497b159fc1STrond Myklebust }
1507b159fc1STrond Myklebust 
15129418aa4SMel Gorman static struct nfs_page *
15229418aa4SMel Gorman nfs_page_find_request_locked(struct nfs_inode *nfsi, struct page *page)
153277459d2STrond Myklebust {
154277459d2STrond Myklebust 	struct nfs_page *req = NULL;
155277459d2STrond Myklebust 
15629418aa4SMel Gorman 	if (PagePrivate(page))
157277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
15829418aa4SMel Gorman 	else if (unlikely(PageSwapCache(page))) {
15929418aa4SMel Gorman 		struct nfs_page *freq, *t;
16029418aa4SMel Gorman 
16129418aa4SMel Gorman 		/* Linearly search the commit list for the correct req */
16229418aa4SMel Gorman 		list_for_each_entry_safe(freq, t, &nfsi->commit_info.list, wb_list) {
16329418aa4SMel Gorman 			if (freq->wb_page == page) {
16429418aa4SMel Gorman 				req = freq;
16529418aa4SMel Gorman 				break;
166277459d2STrond Myklebust 			}
16729418aa4SMel Gorman 		}
16829418aa4SMel Gorman 	}
16929418aa4SMel Gorman 
17029418aa4SMel Gorman 	if (req)
17129418aa4SMel Gorman 		kref_get(&req->wb_kref);
17229418aa4SMel Gorman 
173277459d2STrond Myklebust 	return req;
174277459d2STrond Myklebust }
175277459d2STrond Myklebust 
176277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page)
177277459d2STrond Myklebust {
178d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
179277459d2STrond Myklebust 	struct nfs_page *req = NULL;
180277459d2STrond Myklebust 
181587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
18229418aa4SMel Gorman 	req = nfs_page_find_request_locked(NFS_I(inode), page);
183587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
184277459d2STrond Myklebust 	return req;
185277459d2STrond Myklebust }
186277459d2STrond Myklebust 
1871da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1881da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1891da177e4SLinus Torvalds {
190d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
191a3d01454STrond Myklebust 	loff_t end, i_size;
192a3d01454STrond Myklebust 	pgoff_t end_index;
1931da177e4SLinus Torvalds 
194a3d01454STrond Myklebust 	spin_lock(&inode->i_lock);
195a3d01454STrond Myklebust 	i_size = i_size_read(inode);
196a3d01454STrond Myklebust 	end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
197d56b4ddfSMel Gorman 	if (i_size > 0 && page_file_index(page) < end_index)
198a3d01454STrond Myklebust 		goto out;
199d56b4ddfSMel Gorman 	end = page_file_offset(page) + ((loff_t)offset+count);
2001da177e4SLinus Torvalds 	if (i_size >= end)
201a3d01454STrond Myklebust 		goto out;
2021da177e4SLinus Torvalds 	i_size_write(inode, end);
203a3d01454STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
204a3d01454STrond Myklebust out:
205a3d01454STrond Myklebust 	spin_unlock(&inode->i_lock);
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
208a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
209a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
210a301b777STrond Myklebust {
211d56b4ddfSMel Gorman 	nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page));
212a301b777STrond Myklebust }
213a301b777STrond Myklebust 
2141da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
2151da177e4SLinus Torvalds  * covers the full page.
2161da177e4SLinus Torvalds  */
2171da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
2181da177e4SLinus Torvalds {
2191da177e4SLinus Torvalds 	if (PageUptodate(page))
2201da177e4SLinus Torvalds 		return;
2211da177e4SLinus Torvalds 	if (base != 0)
2221da177e4SLinus Torvalds 		return;
22349a70f27STrond Myklebust 	if (count != nfs_page_length(page))
2241da177e4SLinus Torvalds 		return;
2251da177e4SLinus Torvalds 	SetPageUptodate(page);
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
2291da177e4SLinus Torvalds {
2301da177e4SLinus Torvalds 	if (wbc->for_reclaim)
231c63c7b05STrond Myklebust 		return FLUSH_HIGHPRI | FLUSH_STABLE;
232b17621feSWu Fengguang 	if (wbc->for_kupdate || wbc->for_background)
233b31268acSTrond Myklebust 		return FLUSH_LOWPRI | FLUSH_COND_STABLE;
234b31268acSTrond Myklebust 	return FLUSH_COND_STABLE;
2351da177e4SLinus Torvalds }
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds /*
23889a09141SPeter Zijlstra  * NFS congestion control
23989a09141SPeter Zijlstra  */
24089a09141SPeter Zijlstra 
24189a09141SPeter Zijlstra int nfs_congestion_kb;
24289a09141SPeter Zijlstra 
24389a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
24489a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
24589a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
24689a09141SPeter Zijlstra 
247deed85e7STrond Myklebust static void nfs_set_page_writeback(struct page *page)
24889a09141SPeter Zijlstra {
249deed85e7STrond Myklebust 	struct nfs_server *nfss = NFS_SERVER(page_file_mapping(page)->host);
2505a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
2515a6d41b3STrond Myklebust 
252deed85e7STrond Myklebust 	WARN_ON_ONCE(ret != 0);
25389a09141SPeter Zijlstra 
254277866a0SPeter Zijlstra 	if (atomic_long_inc_return(&nfss->writeback) >
2558aa7e847SJens Axboe 			NFS_CONGESTION_ON_THRESH) {
2568aa7e847SJens Axboe 		set_bdi_congested(&nfss->backing_dev_info,
2578aa7e847SJens Axboe 					BLK_RW_ASYNC);
2588aa7e847SJens Axboe 	}
25989a09141SPeter Zijlstra }
26089a09141SPeter Zijlstra 
26189a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page)
26289a09141SPeter Zijlstra {
263d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
26489a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
26589a09141SPeter Zijlstra 
26689a09141SPeter Zijlstra 	end_page_writeback(page);
267c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
2688aa7e847SJens Axboe 		clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
26989a09141SPeter Zijlstra }
27089a09141SPeter Zijlstra 
271cfb506e1STrond Myklebust static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock)
272e261f51fSTrond Myklebust {
273d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
274e261f51fSTrond Myklebust 	struct nfs_page *req;
275e261f51fSTrond Myklebust 	int ret;
276e261f51fSTrond Myklebust 
277587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
278e261f51fSTrond Myklebust 	for (;;) {
27929418aa4SMel Gorman 		req = nfs_page_find_request_locked(NFS_I(inode), page);
280074cc1deSTrond Myklebust 		if (req == NULL)
281074cc1deSTrond Myklebust 			break;
2827ad84aa9STrond Myklebust 		if (nfs_lock_request(req))
283e261f51fSTrond Myklebust 			break;
284e261f51fSTrond Myklebust 		/* Note: If we hold the page lock, as is the case in nfs_writepage,
2857ad84aa9STrond Myklebust 		 *	 then the call to nfs_lock_request() will always
286e261f51fSTrond Myklebust 		 *	 succeed provided that someone hasn't already marked the
287e261f51fSTrond Myklebust 		 *	 request as dirty (in which case we don't care).
288e261f51fSTrond Myklebust 		 */
289587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
290cfb506e1STrond Myklebust 		if (!nonblock)
291e261f51fSTrond Myklebust 			ret = nfs_wait_on_request(req);
292cfb506e1STrond Myklebust 		else
293cfb506e1STrond Myklebust 			ret = -EAGAIN;
294e261f51fSTrond Myklebust 		nfs_release_request(req);
295e261f51fSTrond Myklebust 		if (ret != 0)
296074cc1deSTrond Myklebust 			return ERR_PTR(ret);
297587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
298e261f51fSTrond Myklebust 	}
299587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
300074cc1deSTrond Myklebust 	return req;
301612c9384STrond Myklebust }
302074cc1deSTrond Myklebust 
303074cc1deSTrond Myklebust /*
304074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
305074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
306074cc1deSTrond Myklebust  */
307074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
308cfb506e1STrond Myklebust 				struct page *page, bool nonblock)
309074cc1deSTrond Myklebust {
310074cc1deSTrond Myklebust 	struct nfs_page *req;
311074cc1deSTrond Myklebust 	int ret = 0;
312074cc1deSTrond Myklebust 
313cfb506e1STrond Myklebust 	req = nfs_find_and_lock_request(page, nonblock);
314074cc1deSTrond Myklebust 	if (!req)
315074cc1deSTrond Myklebust 		goto out;
316074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
317074cc1deSTrond Myklebust 	if (IS_ERR(req))
318074cc1deSTrond Myklebust 		goto out;
319074cc1deSTrond Myklebust 
320deed85e7STrond Myklebust 	nfs_set_page_writeback(page);
321deed85e7STrond Myklebust 	WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
322074cc1deSTrond Myklebust 
323deed85e7STrond Myklebust 	ret = 0;
324f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
325f8512ad0SFred Isaman 		nfs_redirty_request(req);
326074cc1deSTrond Myklebust 		ret = pgio->pg_error;
327f8512ad0SFred Isaman 	}
328074cc1deSTrond Myklebust out:
329074cc1deSTrond Myklebust 	return ret;
330e261f51fSTrond Myklebust }
331e261f51fSTrond Myklebust 
332f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
333f758c885STrond Myklebust {
334d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
335cfb506e1STrond Myklebust 	int ret;
336f758c885STrond Myklebust 
337f758c885STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
338f758c885STrond Myklebust 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
339f758c885STrond Myklebust 
340d56b4ddfSMel Gorman 	nfs_pageio_cond_complete(pgio, page_file_index(page));
3411b430beeSWu Fengguang 	ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
342cfb506e1STrond Myklebust 	if (ret == -EAGAIN) {
343cfb506e1STrond Myklebust 		redirty_page_for_writepage(wbc, page);
344cfb506e1STrond Myklebust 		ret = 0;
345cfb506e1STrond Myklebust 	}
346cfb506e1STrond Myklebust 	return ret;
347f758c885STrond Myklebust }
348f758c885STrond Myklebust 
349e261f51fSTrond Myklebust /*
3501da177e4SLinus Torvalds  * Write an mmapped page to the server.
3511da177e4SLinus Torvalds  */
3524d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
3531da177e4SLinus Torvalds {
354f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
355e261f51fSTrond Myklebust 	int err;
3561da177e4SLinus Torvalds 
357d56b4ddfSMel Gorman 	NFS_PROTO(page_file_mapping(page)->host)->write_pageio_init(&pgio,
35857208fa7SBryan Schumaker 							  page->mapping->host,
35957208fa7SBryan Schumaker 							  wb_priority(wbc),
360061ae2edSFred Isaman 							  &nfs_async_write_completion_ops);
361f758c885STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
362f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
363f758c885STrond Myklebust 	if (err < 0)
3644d770ccfSTrond Myklebust 		return err;
365f758c885STrond Myklebust 	if (pgio.pg_error < 0)
366f758c885STrond Myklebust 		return pgio.pg_error;
367f758c885STrond Myklebust 	return 0;
3684d770ccfSTrond Myklebust }
3694d770ccfSTrond Myklebust 
3704d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
3714d770ccfSTrond Myklebust {
372f758c885STrond Myklebust 	int ret;
3734d770ccfSTrond Myklebust 
374f758c885STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
3751da177e4SLinus Torvalds 	unlock_page(page);
376f758c885STrond Myklebust 	return ret;
377f758c885STrond Myklebust }
378f758c885STrond Myklebust 
379f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
380f758c885STrond Myklebust {
381f758c885STrond Myklebust 	int ret;
382f758c885STrond Myklebust 
383f758c885STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
384f758c885STrond Myklebust 	unlock_page(page);
385f758c885STrond Myklebust 	return ret;
3861da177e4SLinus Torvalds }
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3891da177e4SLinus Torvalds {
3901da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
39172cb77f4STrond Myklebust 	unsigned long *bitlock = &NFS_I(inode)->flags;
392c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
3931da177e4SLinus Torvalds 	int err;
3941da177e4SLinus Torvalds 
39572cb77f4STrond Myklebust 	/* Stop dirtying of new pages while we sync */
39672cb77f4STrond Myklebust 	err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
39772cb77f4STrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
39872cb77f4STrond Myklebust 	if (err)
39972cb77f4STrond Myklebust 		goto out_err;
40072cb77f4STrond Myklebust 
40191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
40291d5b470SChuck Lever 
40357208fa7SBryan Schumaker 	NFS_PROTO(inode)->write_pageio_init(&pgio, inode, wb_priority(wbc), &nfs_async_write_completion_ops);
404f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
405c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
40672cb77f4STrond Myklebust 
40772cb77f4STrond Myklebust 	clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
408*4e857c58SPeter Zijlstra 	smp_mb__after_atomic();
40972cb77f4STrond Myklebust 	wake_up_bit(bitlock, NFS_INO_FLUSHING);
41072cb77f4STrond Myklebust 
411f758c885STrond Myklebust 	if (err < 0)
41272cb77f4STrond Myklebust 		goto out_err;
41372cb77f4STrond Myklebust 	err = pgio.pg_error;
41472cb77f4STrond Myklebust 	if (err < 0)
41572cb77f4STrond Myklebust 		goto out_err;
416c63c7b05STrond Myklebust 	return 0;
41772cb77f4STrond Myklebust out_err:
41872cb77f4STrond Myklebust 	return err;
4191da177e4SLinus Torvalds }
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds /*
4221da177e4SLinus Torvalds  * Insert a write request into an inode
4231da177e4SLinus Torvalds  */
424d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
4251da177e4SLinus Torvalds {
4261da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
427e7d39069STrond Myklebust 
428e7d39069STrond Myklebust 	/* Lock the request! */
4297ad84aa9STrond Myklebust 	nfs_lock_request(req);
430e7d39069STrond Myklebust 
431e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
432011e2a7fSBryan Schumaker 	if (!nfsi->npages && NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
433a9a4a87aSTrond Myklebust 		inode->i_version++;
43429418aa4SMel Gorman 	/*
43529418aa4SMel Gorman 	 * Swap-space should not get truncated. Hence no need to plug the race
43629418aa4SMel Gorman 	 * with invalidate/truncate.
43729418aa4SMel Gorman 	 */
43829418aa4SMel Gorman 	if (likely(!PageSwapCache(req->wb_page))) {
4392df485a7STrond Myklebust 		set_bit(PG_MAPPED, &req->wb_flags);
440deb7d638STrond Myklebust 		SetPagePrivate(req->wb_page);
441277459d2STrond Myklebust 		set_page_private(req->wb_page, (unsigned long)req);
44229418aa4SMel Gorman 	}
4431da177e4SLinus Torvalds 	nfsi->npages++;
444c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
445e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds /*
44989a09141SPeter Zijlstra  * Remove a write request from an inode
4501da177e4SLinus Torvalds  */
4511da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
4521da177e4SLinus Torvalds {
4533d4ff43dSAl Viro 	struct inode *inode = req->wb_context->dentry->d_inode;
4541da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4551da177e4SLinus Torvalds 
456587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
45729418aa4SMel Gorman 	if (likely(!PageSwapCache(req->wb_page))) {
458277459d2STrond Myklebust 		set_page_private(req->wb_page, 0);
459deb7d638STrond Myklebust 		ClearPagePrivate(req->wb_page);
4602df485a7STrond Myklebust 		clear_bit(PG_MAPPED, &req->wb_flags);
46129418aa4SMel Gorman 	}
4621da177e4SLinus Torvalds 	nfsi->npages--;
463587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
4641da177e4SLinus Torvalds 	nfs_release_request(req);
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds 
46761822ab5STrond Myklebust static void
4686d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
46961822ab5STrond Myklebust {
47061822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
47161822ab5STrond Myklebust }
47261822ab5STrond Myklebust 
47389d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
4748dd37758STrond Myklebust /**
4758dd37758STrond Myklebust  * nfs_request_add_commit_list - add request to a commit list
4768dd37758STrond Myklebust  * @req: pointer to a struct nfs_page
477ea2cf228SFred Isaman  * @dst: commit list head
478ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
4798dd37758STrond Myklebust  *
480ea2cf228SFred Isaman  * This sets the PG_CLEAN bit, updates the cinfo count of
4818dd37758STrond Myklebust  * number of outstanding requests requiring a commit as well as
4828dd37758STrond Myklebust  * the MM page stats.
4838dd37758STrond Myklebust  *
484ea2cf228SFred Isaman  * The caller must _not_ hold the cinfo->lock, but must be
4858dd37758STrond Myklebust  * holding the nfs_page lock.
4868dd37758STrond Myklebust  */
4878dd37758STrond Myklebust void
488ea2cf228SFred Isaman nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst,
489ea2cf228SFred Isaman 			    struct nfs_commit_info *cinfo)
4908dd37758STrond Myklebust {
4918dd37758STrond Myklebust 	set_bit(PG_CLEAN, &(req)->wb_flags);
492ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
493ea2cf228SFred Isaman 	nfs_list_add_request(req, dst);
494ea2cf228SFred Isaman 	cinfo->mds->ncommit++;
495ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
49656f9cd68SFred Isaman 	if (!cinfo->dreq) {
4978dd37758STrond Myklebust 		inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
498d56b4ddfSMel Gorman 		inc_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
49956f9cd68SFred Isaman 			     BDI_RECLAIMABLE);
50056f9cd68SFred Isaman 		__mark_inode_dirty(req->wb_context->dentry->d_inode,
50156f9cd68SFred Isaman 				   I_DIRTY_DATASYNC);
50256f9cd68SFred Isaman 	}
5038dd37758STrond Myklebust }
5048dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
5058dd37758STrond Myklebust 
5068dd37758STrond Myklebust /**
5078dd37758STrond Myklebust  * nfs_request_remove_commit_list - Remove request from a commit list
5088dd37758STrond Myklebust  * @req: pointer to a nfs_page
509ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
5108dd37758STrond Myklebust  *
511ea2cf228SFred Isaman  * This clears the PG_CLEAN bit, and updates the cinfo's count of
5128dd37758STrond Myklebust  * number of outstanding requests requiring a commit
5138dd37758STrond Myklebust  * It does not update the MM page stats.
5148dd37758STrond Myklebust  *
515ea2cf228SFred Isaman  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
5168dd37758STrond Myklebust  */
5178dd37758STrond Myklebust void
518ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req,
519ea2cf228SFred Isaman 			       struct nfs_commit_info *cinfo)
5208dd37758STrond Myklebust {
5218dd37758STrond Myklebust 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
5228dd37758STrond Myklebust 		return;
5238dd37758STrond Myklebust 	nfs_list_remove_request(req);
524ea2cf228SFred Isaman 	cinfo->mds->ncommit--;
5258dd37758STrond Myklebust }
5268dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
5278dd37758STrond Myklebust 
528ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
529ea2cf228SFred Isaman 				      struct inode *inode)
530ea2cf228SFred Isaman {
531ea2cf228SFred Isaman 	cinfo->lock = &inode->i_lock;
532ea2cf228SFred Isaman 	cinfo->mds = &NFS_I(inode)->commit_info;
533ea2cf228SFred Isaman 	cinfo->ds = pnfs_get_ds_info(inode);
534b359f9d0SFred Isaman 	cinfo->dreq = NULL;
535f453a54aSFred Isaman 	cinfo->completion_ops = &nfs_commit_completion_ops;
536ea2cf228SFred Isaman }
537ea2cf228SFred Isaman 
538ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
539ea2cf228SFred Isaman 		    struct inode *inode,
540ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq)
541ea2cf228SFred Isaman {
5421763da12SFred Isaman 	if (dreq)
5431763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
5441763da12SFred Isaman 	else
545ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(cinfo, inode);
546ea2cf228SFred Isaman }
547ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo);
5488dd37758STrond Myklebust 
5491da177e4SLinus Torvalds /*
5501da177e4SLinus Torvalds  * Add a request to the inode's commit list.
5511da177e4SLinus Torvalds  */
5521763da12SFred Isaman void
553ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
554ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
5551da177e4SLinus Torvalds {
556ea2cf228SFred Isaman 	if (pnfs_mark_request_commit(req, lseg, cinfo))
5578dd37758STrond Myklebust 		return;
558ea2cf228SFred Isaman 	nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo);
5591da177e4SLinus Torvalds }
5608e821cadSTrond Myklebust 
561d6d6dc7cSFred Isaman static void
562d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page)
563e468bae9STrond Myklebust {
564e468bae9STrond Myklebust 	dec_zone_page_state(page, NR_UNSTABLE_NFS);
565d56b4ddfSMel Gorman 	dec_bdi_stat(page_file_mapping(page)->backing_dev_info, BDI_RECLAIMABLE);
566e468bae9STrond Myklebust }
567d6d6dc7cSFred Isaman 
5688dd37758STrond Myklebust static void
569d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req)
570d6d6dc7cSFred Isaman {
5718dd37758STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
5728dd37758STrond Myklebust 		struct inode *inode = req->wb_context->dentry->d_inode;
573ea2cf228SFred Isaman 		struct nfs_commit_info cinfo;
574d6d6dc7cSFred Isaman 
575ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(&cinfo, inode);
576ea2cf228SFred Isaman 		if (!pnfs_clear_request_commit(req, &cinfo)) {
577ea2cf228SFred Isaman 			spin_lock(cinfo.lock);
578ea2cf228SFred Isaman 			nfs_request_remove_commit_list(req, &cinfo);
579ea2cf228SFred Isaman 			spin_unlock(cinfo.lock);
580d6d6dc7cSFred Isaman 		}
5818dd37758STrond Myklebust 		nfs_clear_page_commit(req->wb_page);
5828dd37758STrond Myklebust 	}
583e468bae9STrond Myklebust }
584e468bae9STrond Myklebust 
5858e821cadSTrond Myklebust static inline
5868e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
5878e821cadSTrond Myklebust {
588465d5243SFred Isaman 	if (data->verf.committed == NFS_DATA_SYNC)
589cd841605SFred Isaman 		return data->header->lseg == NULL;
5908e821cadSTrond Myklebust 	return data->verf.committed != NFS_FILE_SYNC;
5918e821cadSTrond Myklebust }
5928e821cadSTrond Myklebust 
5938e821cadSTrond Myklebust #else
59468cd6fa4SBryan Schumaker static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
59568cd6fa4SBryan Schumaker 				      struct inode *inode)
59668cd6fa4SBryan Schumaker {
59768cd6fa4SBryan Schumaker }
59868cd6fa4SBryan Schumaker 
59968cd6fa4SBryan Schumaker void nfs_init_cinfo(struct nfs_commit_info *cinfo,
60068cd6fa4SBryan Schumaker 		    struct inode *inode,
60168cd6fa4SBryan Schumaker 		    struct nfs_direct_req *dreq)
60268cd6fa4SBryan Schumaker {
60368cd6fa4SBryan Schumaker }
60468cd6fa4SBryan Schumaker 
6051763da12SFred Isaman void
606ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
607ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
6088e821cadSTrond Myklebust {
6098e821cadSTrond Myklebust }
6108e821cadSTrond Myklebust 
6118dd37758STrond Myklebust static void
612e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
613e468bae9STrond Myklebust {
614e468bae9STrond Myklebust }
615e468bae9STrond Myklebust 
6168e821cadSTrond Myklebust static inline
6178e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
6188e821cadSTrond Myklebust {
6198e821cadSTrond Myklebust 	return 0;
6208e821cadSTrond Myklebust }
6218e821cadSTrond Myklebust 
6221da177e4SLinus Torvalds #endif
6231da177e4SLinus Torvalds 
624061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
6256c75dc0dSFred Isaman {
626ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
6276c75dc0dSFred Isaman 	unsigned long bytes = 0;
6286c75dc0dSFred Isaman 
6296c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
6306c75dc0dSFred Isaman 		goto out;
631ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
6326c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
6336c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
6346c75dc0dSFred Isaman 
6356c75dc0dSFred Isaman 		bytes += req->wb_bytes;
6366c75dc0dSFred Isaman 		nfs_list_remove_request(req);
6376c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
6386c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
639d1182b33STrond Myklebust 			nfs_set_pageerror(req->wb_page);
6406c75dc0dSFred Isaman 			nfs_context_set_write_error(req->wb_context, hdr->error);
6416c75dc0dSFred Isaman 			goto remove_req;
6426c75dc0dSFred Isaman 		}
6436c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
6446c75dc0dSFred Isaman 			nfs_mark_request_dirty(req);
6456c75dc0dSFred Isaman 			goto next;
6466c75dc0dSFred Isaman 		}
6476c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
6482f2c63bcSTrond Myklebust 			memcpy(&req->wb_verf, &hdr->verf->verifier, sizeof(req->wb_verf));
649ea2cf228SFred Isaman 			nfs_mark_request_commit(req, hdr->lseg, &cinfo);
6506c75dc0dSFred Isaman 			goto next;
6516c75dc0dSFred Isaman 		}
6526c75dc0dSFred Isaman remove_req:
6536c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
6546c75dc0dSFred Isaman next:
6551d1afcbcSTrond Myklebust 		nfs_unlock_request(req);
656d1182b33STrond Myklebust 		nfs_end_page_writeback(req->wb_page);
6573aff4ebbSTrond Myklebust 		nfs_release_request(req);
6586c75dc0dSFred Isaman 	}
6596c75dc0dSFred Isaman out:
6606c75dc0dSFred Isaman 	hdr->release(hdr);
6616c75dc0dSFred Isaman }
6626c75dc0dSFred Isaman 
66389d77c8fSBryan Schumaker #if  IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
664ea2cf228SFred Isaman static unsigned long
665ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
666fb8a1f11STrond Myklebust {
667ea2cf228SFred Isaman 	return cinfo->mds->ncommit;
668fb8a1f11STrond Myklebust }
669fb8a1f11STrond Myklebust 
670ea2cf228SFred Isaman /* cinfo->lock held by caller */
6711763da12SFred Isaman int
672ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
673ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
674d6d6dc7cSFred Isaman {
675d6d6dc7cSFred Isaman 	struct nfs_page *req, *tmp;
676d6d6dc7cSFred Isaman 	int ret = 0;
677d6d6dc7cSFred Isaman 
678d6d6dc7cSFred Isaman 	list_for_each_entry_safe(req, tmp, src, wb_list) {
6798dd37758STrond Myklebust 		if (!nfs_lock_request(req))
6808dd37758STrond Myklebust 			continue;
6817ad84aa9STrond Myklebust 		kref_get(&req->wb_kref);
682ea2cf228SFred Isaman 		if (cond_resched_lock(cinfo->lock))
6833b3be88dSTrond Myklebust 			list_safe_reset_next(req, tmp, wb_list);
684ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
6858dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
686d6d6dc7cSFred Isaman 		ret++;
6871763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
688d6d6dc7cSFred Isaman 			break;
689d6d6dc7cSFred Isaman 	}
690d6d6dc7cSFred Isaman 	return ret;
691d6d6dc7cSFred Isaman }
692d6d6dc7cSFred Isaman 
6931da177e4SLinus Torvalds /*
6941da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
6951da177e4SLinus Torvalds  * @inode: NFS inode to scan
696ea2cf228SFred Isaman  * @dst: mds destination list
697ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
6981da177e4SLinus Torvalds  *
6991da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
7001da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
7011da177e4SLinus Torvalds  */
7021763da12SFred Isaman int
703ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
704ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
7051da177e4SLinus Torvalds {
706d6d6dc7cSFred Isaman 	int ret = 0;
707fb8a1f11STrond Myklebust 
708ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
709ea2cf228SFred Isaman 	if (cinfo->mds->ncommit > 0) {
7108dd37758STrond Myklebust 		const int max = INT_MAX;
711d6d6dc7cSFred Isaman 
712ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
713ea2cf228SFred Isaman 					   cinfo, max);
714ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
715d6d6dc7cSFred Isaman 	}
716ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
717ff778d02STrond Myklebust 	return ret;
7181da177e4SLinus Torvalds }
719d6d6dc7cSFred Isaman 
720c42de9ddSTrond Myklebust #else
721ea2cf228SFred Isaman static unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
722fb8a1f11STrond Myklebust {
723fb8a1f11STrond Myklebust 	return 0;
724fb8a1f11STrond Myklebust }
725fb8a1f11STrond Myklebust 
7261763da12SFred Isaman int nfs_scan_commit(struct inode *inode, struct list_head *dst,
727ea2cf228SFred Isaman 		    struct nfs_commit_info *cinfo)
728c42de9ddSTrond Myklebust {
729c42de9ddSTrond Myklebust 	return 0;
730c42de9ddSTrond Myklebust }
7311da177e4SLinus Torvalds #endif
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds /*
734e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
735e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
7361da177e4SLinus Torvalds  *
737e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
738e7d39069STrond Myklebust  * to disk.
7391da177e4SLinus Torvalds  */
740e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
741e7d39069STrond Myklebust 		struct page *page,
742e7d39069STrond Myklebust 		unsigned int offset,
743e7d39069STrond Myklebust 		unsigned int bytes)
7441da177e4SLinus Torvalds {
745e7d39069STrond Myklebust 	struct nfs_page *req;
746e7d39069STrond Myklebust 	unsigned int rqend;
747e7d39069STrond Myklebust 	unsigned int end;
7481da177e4SLinus Torvalds 	int error;
749277459d2STrond Myklebust 
750e7d39069STrond Myklebust 	if (!PagePrivate(page))
751e7d39069STrond Myklebust 		return NULL;
752e7d39069STrond Myklebust 
753e7d39069STrond Myklebust 	end = offset + bytes;
754e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
755e7d39069STrond Myklebust 
756e7d39069STrond Myklebust 	for (;;) {
75729418aa4SMel Gorman 		req = nfs_page_find_request_locked(NFS_I(inode), page);
758e7d39069STrond Myklebust 		if (req == NULL)
759e7d39069STrond Myklebust 			goto out_unlock;
760e7d39069STrond Myklebust 
761e7d39069STrond Myklebust 		rqend = req->wb_offset + req->wb_bytes;
762e7d39069STrond Myklebust 		/*
763e7d39069STrond Myklebust 		 * Tell the caller to flush out the request if
764e7d39069STrond Myklebust 		 * the offsets are non-contiguous.
765e7d39069STrond Myklebust 		 * Note: nfs_flush_incompatible() will already
766e7d39069STrond Myklebust 		 * have flushed out requests having wrong owners.
767e7d39069STrond Myklebust 		 */
768e468bae9STrond Myklebust 		if (offset > rqend
769e7d39069STrond Myklebust 		    || end < req->wb_offset)
770e7d39069STrond Myklebust 			goto out_flushme;
771e7d39069STrond Myklebust 
7727ad84aa9STrond Myklebust 		if (nfs_lock_request(req))
773e7d39069STrond Myklebust 			break;
774e7d39069STrond Myklebust 
775e7d39069STrond Myklebust 		/* The request is locked, so wait and then retry */
776587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
7771da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
7781da177e4SLinus Torvalds 		nfs_release_request(req);
779e7d39069STrond Myklebust 		if (error != 0)
780e7d39069STrond Myklebust 			goto out_err;
781e7d39069STrond Myklebust 		spin_lock(&inode->i_lock);
7821da177e4SLinus Torvalds 	}
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
7851da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
7861da177e4SLinus Torvalds 		req->wb_offset = offset;
7871da177e4SLinus Torvalds 		req->wb_pgbase = offset;
7881da177e4SLinus Torvalds 	}
7891da177e4SLinus Torvalds 	if (end > rqend)
7901da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
791e7d39069STrond Myklebust 	else
792e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
793e7d39069STrond Myklebust out_unlock:
794e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
795ca138f36SFred Isaman 	if (req)
7968dd37758STrond Myklebust 		nfs_clear_request_commit(req);
797e7d39069STrond Myklebust 	return req;
798e7d39069STrond Myklebust out_flushme:
799e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
800e7d39069STrond Myklebust 	nfs_release_request(req);
801e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
802e7d39069STrond Myklebust out_err:
803e7d39069STrond Myklebust 	return ERR_PTR(error);
804e7d39069STrond Myklebust }
8051da177e4SLinus Torvalds 
806e7d39069STrond Myklebust /*
807e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
808e7d39069STrond Myklebust  *
809e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
810e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
811e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
812e7d39069STrond Myklebust  */
813e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
814e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
815e7d39069STrond Myklebust {
816d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
817e7d39069STrond Myklebust 	struct nfs_page	*req;
818e7d39069STrond Myklebust 
819e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
820e7d39069STrond Myklebust 	if (req != NULL)
821e7d39069STrond Myklebust 		goto out;
822e7d39069STrond Myklebust 	req = nfs_create_request(ctx, inode, page, offset, bytes);
823e7d39069STrond Myklebust 	if (IS_ERR(req))
824e7d39069STrond Myklebust 		goto out;
825d6d6dc7cSFred Isaman 	nfs_inode_add_request(inode, req);
826efc91ed0STrond Myklebust out:
82761e930a9STrond Myklebust 	return req;
8281da177e4SLinus Torvalds }
8291da177e4SLinus Torvalds 
830e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
831e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
832e7d39069STrond Myklebust {
833e7d39069STrond Myklebust 	struct nfs_page	*req;
834e7d39069STrond Myklebust 
835e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
836e7d39069STrond Myklebust 	if (IS_ERR(req))
837e7d39069STrond Myklebust 		return PTR_ERR(req);
838e7d39069STrond Myklebust 	/* Update file length */
839e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
840e7d39069STrond Myklebust 	nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
841a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
8421d1afcbcSTrond Myklebust 	nfs_unlock_and_release_request(req);
843e7d39069STrond Myklebust 	return 0;
844e7d39069STrond Myklebust }
845e7d39069STrond Myklebust 
8461da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
8471da177e4SLinus Torvalds {
848cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
8492a369153STrond Myklebust 	struct nfs_lock_context *l_ctx;
8501da177e4SLinus Torvalds 	struct nfs_page	*req;
8511a54533eSTrond Myklebust 	int do_flush, status;
8521da177e4SLinus Torvalds 	/*
8531da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
8541da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
8551da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
8561da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
8571da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
8581da177e4SLinus Torvalds 	 * dropped page.
8591da177e4SLinus Torvalds 	 */
8601a54533eSTrond Myklebust 	do {
861277459d2STrond Myklebust 		req = nfs_page_find_request(page);
8621a54533eSTrond Myklebust 		if (req == NULL)
8631a54533eSTrond Myklebust 			return 0;
8642a369153STrond Myklebust 		l_ctx = req->wb_lock_context;
8652a369153STrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx;
8660f1d2605STrond Myklebust 		if (l_ctx && ctx->dentry->d_inode->i_flock != NULL) {
8672a369153STrond Myklebust 			do_flush |= l_ctx->lockowner.l_owner != current->files
8682a369153STrond Myklebust 				|| l_ctx->lockowner.l_pid != current->tgid;
8692a369153STrond Myklebust 		}
8701da177e4SLinus Torvalds 		nfs_release_request(req);
8711a54533eSTrond Myklebust 		if (!do_flush)
8721a54533eSTrond Myklebust 			return 0;
873d56b4ddfSMel Gorman 		status = nfs_wb_page(page_file_mapping(page)->host, page);
8741a54533eSTrond Myklebust 	} while (status == 0);
8751a54533eSTrond Myklebust 	return status;
8761da177e4SLinus Torvalds }
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds /*
879dc24826bSAndy Adamson  * Avoid buffered writes when a open context credential's key would
880dc24826bSAndy Adamson  * expire soon.
881dc24826bSAndy Adamson  *
882dc24826bSAndy Adamson  * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
883dc24826bSAndy Adamson  *
884dc24826bSAndy Adamson  * Return 0 and set a credential flag which triggers the inode to flush
885dc24826bSAndy Adamson  * and performs  NFS_FILE_SYNC writes if the key will expired within
886dc24826bSAndy Adamson  * RPC_KEY_EXPIRE_TIMEO.
887dc24826bSAndy Adamson  */
888dc24826bSAndy Adamson int
889dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode)
890dc24826bSAndy Adamson {
891dc24826bSAndy Adamson 	struct nfs_open_context *ctx = nfs_file_open_context(filp);
892dc24826bSAndy Adamson 	struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
893dc24826bSAndy Adamson 
894dc24826bSAndy Adamson 	return rpcauth_key_timeout_notify(auth, ctx->cred);
895dc24826bSAndy Adamson }
896dc24826bSAndy Adamson 
897dc24826bSAndy Adamson /*
898dc24826bSAndy Adamson  * Test if the open context credential key is marked to expire soon.
899dc24826bSAndy Adamson  */
900dc24826bSAndy Adamson bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx)
901dc24826bSAndy Adamson {
902dc24826bSAndy Adamson 	return rpcauth_cred_key_to_expire(ctx->cred);
903dc24826bSAndy Adamson }
904dc24826bSAndy Adamson 
905dc24826bSAndy Adamson /*
9065d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
9075d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
9085d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
9095d47a356STrond Myklebust  */
9108d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
9115d47a356STrond Myklebust {
912d529ef83SJeff Layton 	struct nfs_inode *nfsi = NFS_I(inode);
913d529ef83SJeff Layton 
9148d197a56STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
9158d197a56STrond Myklebust 		goto out;
916d529ef83SJeff Layton 	if (nfsi->cache_validity & (NFS_INO_INVALID_DATA|NFS_INO_REVAL_PAGECACHE))
917d529ef83SJeff Layton 		return false;
9184db72b40SJeff Layton 	smp_rmb();
919d529ef83SJeff Layton 	if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags))
9208d197a56STrond Myklebust 		return false;
9218d197a56STrond Myklebust out:
9228d197a56STrond Myklebust 	return PageUptodate(page) != 0;
9235d47a356STrond Myklebust }
9245d47a356STrond Myklebust 
925c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or
926c7559663SScott Mayhew  * if we have the whole file locked for writing), it may be more efficient to
927c7559663SScott Mayhew  * extend the write to cover the entire page in order to avoid fragmentation
928c7559663SScott Mayhew  * inefficiencies.
929c7559663SScott Mayhew  *
930263b4509SScott Mayhew  * If the file is opened for synchronous writes then we can just skip the rest
931263b4509SScott Mayhew  * of the checks.
932c7559663SScott Mayhew  */
933c7559663SScott Mayhew static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode)
934c7559663SScott Mayhew {
935c7559663SScott Mayhew 	if (file->f_flags & O_DSYNC)
936c7559663SScott Mayhew 		return 0;
937263b4509SScott Mayhew 	if (!nfs_write_pageuptodate(page, inode))
938263b4509SScott Mayhew 		return 0;
939c7559663SScott Mayhew 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
940c7559663SScott Mayhew 		return 1;
941263b4509SScott Mayhew 	if (inode->i_flock == NULL || (inode->i_flock->fl_start == 0 &&
942c7559663SScott Mayhew 			inode->i_flock->fl_end == OFFSET_MAX &&
943263b4509SScott Mayhew 			inode->i_flock->fl_type != F_RDLCK))
944c7559663SScott Mayhew 		return 1;
945c7559663SScott Mayhew 	return 0;
946c7559663SScott Mayhew }
947c7559663SScott Mayhew 
9485d47a356STrond Myklebust /*
9491da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
9501da177e4SLinus Torvalds  *
9511da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
9521da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
9531da177e4SLinus Torvalds  */
9541da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
9551da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
9561da177e4SLinus Torvalds {
957cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
958d56b4ddfSMel Gorman 	struct inode	*inode = page_file_mapping(page)->host;
9591da177e4SLinus Torvalds 	int		status = 0;
9601da177e4SLinus Torvalds 
96191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
96291d5b470SChuck Lever 
9636de1472fSAl Viro 	dprintk("NFS:       nfs_updatepage(%pD2 %d@%lld)\n",
9646de1472fSAl Viro 		file, count, (long long)(page_file_offset(page) + offset));
9651da177e4SLinus Torvalds 
966c7559663SScott Mayhew 	if (nfs_can_extend_write(file, page, inode)) {
96749a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
9681da177e4SLinus Torvalds 		offset = 0;
9691da177e4SLinus Torvalds 	}
9701da177e4SLinus Torvalds 
971e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
97203fa9e84STrond Myklebust 	if (status < 0)
97303fa9e84STrond Myklebust 		nfs_set_pageerror(page);
97459b7c05fSTrond Myklebust 	else
97559b7c05fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
9761da177e4SLinus Torvalds 
97748186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
9781da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
9791da177e4SLinus Torvalds 	return status;
9801da177e4SLinus Torvalds }
9811da177e4SLinus Torvalds 
9823ff7576dSTrond Myklebust static int flush_task_priority(int how)
9831da177e4SLinus Torvalds {
9841da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
9851da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
9861da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
9871da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
9881da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
9891da177e4SLinus Torvalds 	}
9901da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
9911da177e4SLinus Torvalds }
9921da177e4SLinus Torvalds 
993c5996c4eSFred Isaman int nfs_initiate_write(struct rpc_clnt *clnt,
994c5996c4eSFred Isaman 		       struct nfs_write_data *data,
995788e7a89STrond Myklebust 		       const struct rpc_call_ops *call_ops,
9969f0ec176SAndy Adamson 		       int how, int flags)
9971da177e4SLinus Torvalds {
998cd841605SFred Isaman 	struct inode *inode = data->header->inode;
9993ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
100007737691STrond Myklebust 	struct rpc_task *task;
1001bdc7f021STrond Myklebust 	struct rpc_message msg = {
1002bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1003bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
1004cd841605SFred Isaman 		.rpc_cred = data->header->cred,
1005bdc7f021STrond Myklebust 	};
100684115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
1007d138d5d1SAndy Adamson 		.rpc_client = clnt,
100807737691STrond Myklebust 		.task = &data->task,
1009bdc7f021STrond Myklebust 		.rpc_message = &msg,
101084115e1cSTrond Myklebust 		.callback_ops = call_ops,
101184115e1cSTrond Myklebust 		.callback_data = data,
1012101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
10139f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
10143ff7576dSTrond Myklebust 		.priority = priority,
101584115e1cSTrond Myklebust 	};
10162c61be0aSTrond Myklebust 	int ret = 0;
10171da177e4SLinus Torvalds 
1018d138d5d1SAndy Adamson 	/* Set up the initial task struct.  */
1019d138d5d1SAndy Adamson 	NFS_PROTO(inode)->write_setup(data, &msg);
1020d138d5d1SAndy Adamson 
1021d138d5d1SAndy Adamson 	dprintk("NFS: %5u initiated write call "
10221e8968c5SNiels de Vos 		"(req %s/%llu, %u bytes @ offset %llu)\n",
1023d138d5d1SAndy Adamson 		data->task.tk_pid,
1024d138d5d1SAndy Adamson 		inode->i_sb->s_id,
10251e8968c5SNiels de Vos 		(unsigned long long)NFS_FILEID(inode),
1026d138d5d1SAndy Adamson 		data->args.count,
1027d138d5d1SAndy Adamson 		(unsigned long long)data->args.offset);
1028d138d5d1SAndy Adamson 
10298c21c62cSWeston Andros Adamson 	nfs4_state_protect_write(NFS_SERVER(inode)->nfs_client,
10308c21c62cSWeston Andros Adamson 				 &task_setup_data.rpc_client, &msg, data);
10318c21c62cSWeston Andros Adamson 
1032d138d5d1SAndy Adamson 	task = rpc_run_task(&task_setup_data);
1033d138d5d1SAndy Adamson 	if (IS_ERR(task)) {
1034d138d5d1SAndy Adamson 		ret = PTR_ERR(task);
1035d138d5d1SAndy Adamson 		goto out;
1036d138d5d1SAndy Adamson 	}
1037d138d5d1SAndy Adamson 	if (how & FLUSH_SYNC) {
1038d138d5d1SAndy Adamson 		ret = rpc_wait_for_completion_task(task);
1039d138d5d1SAndy Adamson 		if (ret == 0)
1040d138d5d1SAndy Adamson 			ret = task->tk_status;
1041d138d5d1SAndy Adamson 	}
1042d138d5d1SAndy Adamson 	rpc_put_task(task);
1043d138d5d1SAndy Adamson out:
1044d138d5d1SAndy Adamson 	return ret;
1045d138d5d1SAndy Adamson }
1046a69aef14SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_write);
1047d138d5d1SAndy Adamson 
1048d138d5d1SAndy Adamson /*
1049d138d5d1SAndy Adamson  * Set up the argument/result storage required for the RPC call.
1050d138d5d1SAndy Adamson  */
10516c75dc0dSFred Isaman static void nfs_write_rpcsetup(struct nfs_write_data *data,
1052d138d5d1SAndy Adamson 		unsigned int count, unsigned int offset,
1053ea2cf228SFred Isaman 		int how, struct nfs_commit_info *cinfo)
1054d138d5d1SAndy Adamson {
10556c75dc0dSFred Isaman 	struct nfs_page *req = data->header->req;
1056d138d5d1SAndy Adamson 
10571da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
10581da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
10591da177e4SLinus Torvalds 
10606c75dc0dSFred Isaman 	data->args.fh     = NFS_FH(data->header->inode);
10611da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
10622bea038cSBoaz Harrosh 	/* pnfs_set_layoutcommit needs this */
10632bea038cSBoaz Harrosh 	data->mds_offset = data->args.offset;
10641da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
106530dd374fSFred Isaman 	data->args.pages  = data->pages.pagevec;
10661da177e4SLinus Torvalds 	data->args.count  = count;
1067383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
1068f11ac8dbSTrond Myklebust 	data->args.lock_context = req->wb_lock_context;
1069bdc7f021STrond Myklebust 	data->args.stable  = NFS_UNSTABLE;
107087ed5eb4STrond Myklebust 	switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
107187ed5eb4STrond Myklebust 	case 0:
107287ed5eb4STrond Myklebust 		break;
107387ed5eb4STrond Myklebust 	case FLUSH_COND_STABLE:
1074ea2cf228SFred Isaman 		if (nfs_reqs_to_commit(cinfo))
107587ed5eb4STrond Myklebust 			break;
107687ed5eb4STrond Myklebust 	default:
1077bdc7f021STrond Myklebust 		data->args.stable = NFS_FILE_SYNC;
1078bdc7f021STrond Myklebust 	}
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
10811da177e4SLinus Torvalds 	data->res.count   = count;
10821da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
10830e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
10846e4efd56STrond Myklebust }
10851da177e4SLinus Torvalds 
10866e4efd56STrond Myklebust static int nfs_do_write(struct nfs_write_data *data,
10876e4efd56STrond Myklebust 		const struct rpc_call_ops *call_ops,
10886e4efd56STrond Myklebust 		int how)
10896e4efd56STrond Myklebust {
1090cd841605SFred Isaman 	struct inode *inode = data->header->inode;
10910382b744SAndy Adamson 
10929f0ec176SAndy Adamson 	return nfs_initiate_write(NFS_CLIENT(inode), data, call_ops, how, 0);
10931da177e4SLinus Torvalds }
10941da177e4SLinus Torvalds 
1095275acaafSTrond Myklebust static int nfs_do_multiple_writes(struct list_head *head,
1096275acaafSTrond Myklebust 		const struct rpc_call_ops *call_ops,
1097275acaafSTrond Myklebust 		int how)
1098275acaafSTrond Myklebust {
1099275acaafSTrond Myklebust 	struct nfs_write_data *data;
1100275acaafSTrond Myklebust 	int ret = 0;
1101275acaafSTrond Myklebust 
1102275acaafSTrond Myklebust 	while (!list_empty(head)) {
1103275acaafSTrond Myklebust 		int ret2;
1104275acaafSTrond Myklebust 
11056c75dc0dSFred Isaman 		data = list_first_entry(head, struct nfs_write_data, list);
1106275acaafSTrond Myklebust 		list_del_init(&data->list);
1107275acaafSTrond Myklebust 
1108dce81290STrond Myklebust 		ret2 = nfs_do_write(data, call_ops, how);
1109275acaafSTrond Myklebust 		 if (ret == 0)
1110275acaafSTrond Myklebust 			 ret = ret2;
1111275acaafSTrond Myklebust 	}
1112275acaafSTrond Myklebust 	return ret;
1113275acaafSTrond Myklebust }
1114275acaafSTrond Myklebust 
11156d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
11166d884e8fSFred  * call this on each, which will prepare them to be retried on next
11176d884e8fSFred  * writeback using standard nfs.
11186d884e8fSFred  */
11196d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
11206d884e8fSFred {
11216d884e8fSFred 	nfs_mark_request_dirty(req);
11221d1afcbcSTrond Myklebust 	nfs_unlock_request(req);
1123d1182b33STrond Myklebust 	nfs_end_page_writeback(req->wb_page);
11243aff4ebbSTrond Myklebust 	nfs_release_request(req);
11256d884e8fSFred }
11266d884e8fSFred 
1127061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head)
11286c75dc0dSFred Isaman {
11296c75dc0dSFred Isaman 	struct nfs_page	*req;
11306c75dc0dSFred Isaman 
11316c75dc0dSFred Isaman 	while (!list_empty(head)) {
11326c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
11336c75dc0dSFred Isaman 		nfs_list_remove_request(req);
11346c75dc0dSFred Isaman 		nfs_redirty_request(req);
11356c75dc0dSFred Isaman 	}
11366c75dc0dSFred Isaman }
11376c75dc0dSFred Isaman 
1138061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1139061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1140061ae2edSFred Isaman 	.completion = nfs_write_completion,
1141061ae2edSFred Isaman };
1142061ae2edSFred Isaman 
114325b11dcdSTrond Myklebust static void nfs_flush_error(struct nfs_pageio_descriptor *desc,
114425b11dcdSTrond Myklebust 		struct nfs_pgio_header *hdr)
114525b11dcdSTrond Myklebust {
114625b11dcdSTrond Myklebust 	set_bit(NFS_IOHDR_REDO, &hdr->flags);
114725b11dcdSTrond Myklebust 	while (!list_empty(&hdr->rpc_list)) {
114825b11dcdSTrond Myklebust 		struct nfs_write_data *data = list_first_entry(&hdr->rpc_list,
114925b11dcdSTrond Myklebust 				struct nfs_write_data, list);
115025b11dcdSTrond Myklebust 		list_del(&data->list);
115125b11dcdSTrond Myklebust 		nfs_writedata_release(data);
115225b11dcdSTrond Myklebust 	}
115325b11dcdSTrond Myklebust 	desc->pg_completion_ops->error_cleanup(&desc->pg_list);
115425b11dcdSTrond Myklebust }
115525b11dcdSTrond Myklebust 
11561da177e4SLinus Torvalds /*
11571da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
11581da177e4SLinus Torvalds  * contiguous dirty area on one page.
11591da177e4SLinus Torvalds  */
11606c75dc0dSFred Isaman static int nfs_flush_multi(struct nfs_pageio_descriptor *desc,
11616c75dc0dSFred Isaman 			   struct nfs_pgio_header *hdr)
11621da177e4SLinus Torvalds {
11636c75dc0dSFred Isaman 	struct nfs_page *req = hdr->req;
11641da177e4SLinus Torvalds 	struct page *page = req->wb_page;
11651da177e4SLinus Torvalds 	struct nfs_write_data *data;
1166d097971dSTrond Myklebust 	size_t wsize = desc->pg_bsize, nbytes;
1167e9f7bee1STrond Myklebust 	unsigned int offset;
11681da177e4SLinus Torvalds 	int requests = 0;
1169ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
11701da177e4SLinus Torvalds 
1171ea2cf228SFred Isaman 	nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
11721da177e4SLinus Torvalds 
1173b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1174ea2cf228SFred Isaman 	    (desc->pg_moreio || nfs_reqs_to_commit(&cinfo) ||
1175b31268acSTrond Myklebust 	     desc->pg_count > wsize))
1176b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1177b31268acSTrond Myklebust 
1178b31268acSTrond Myklebust 
1179275acaafSTrond Myklebust 	offset = 0;
1180c76069bdSFred Isaman 	nbytes = desc->pg_count;
1181e9f7bee1STrond Myklebust 	do {
1182e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
1183e9f7bee1STrond Myklebust 
11846c75dc0dSFred Isaman 		data = nfs_writedata_alloc(hdr, 1);
118525b11dcdSTrond Myklebust 		if (!data) {
118625b11dcdSTrond Myklebust 			nfs_flush_error(desc, hdr);
118725b11dcdSTrond Myklebust 			return -ENOMEM;
118825b11dcdSTrond Myklebust 		}
118930dd374fSFred Isaman 		data->pages.pagevec[0] = page;
1190ea2cf228SFred Isaman 		nfs_write_rpcsetup(data, len, offset, desc->pg_ioflags, &cinfo);
11916c75dc0dSFred Isaman 		list_add(&data->list, &hdr->rpc_list);
11921da177e4SLinus Torvalds 		requests++;
1193e9f7bee1STrond Myklebust 		nbytes -= len;
1194275acaafSTrond Myklebust 		offset += len;
1195e9f7bee1STrond Myklebust 	} while (nbytes != 0);
119625b11dcdSTrond Myklebust 	nfs_list_remove_request(req);
119725b11dcdSTrond Myklebust 	nfs_list_add_request(req, &hdr->pages);
11986c75dc0dSFred Isaman 	desc->pg_rpc_callops = &nfs_write_common_ops;
119925b11dcdSTrond Myklebust 	return 0;
12001da177e4SLinus Torvalds }
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds /*
12031da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
12041da177e4SLinus Torvalds  * The page must have been locked by the caller.
12051da177e4SLinus Torvalds  *
12061da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
12071da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
12081da177e4SLinus Torvalds  * that has been written but not committed.
12091da177e4SLinus Torvalds  */
12106c75dc0dSFred Isaman static int nfs_flush_one(struct nfs_pageio_descriptor *desc,
12116c75dc0dSFred Isaman 			 struct nfs_pgio_header *hdr)
12121da177e4SLinus Torvalds {
12131da177e4SLinus Torvalds 	struct nfs_page		*req;
12141da177e4SLinus Torvalds 	struct page		**pages;
12151da177e4SLinus Torvalds 	struct nfs_write_data	*data;
1216c76069bdSFred Isaman 	struct list_head *head = &desc->pg_list;
1217ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
12181da177e4SLinus Torvalds 
12196c75dc0dSFred Isaman 	data = nfs_writedata_alloc(hdr, nfs_page_array_len(desc->pg_base,
1220c76069bdSFred Isaman 							   desc->pg_count));
12216c75dc0dSFred Isaman 	if (!data) {
122225b11dcdSTrond Myklebust 		nfs_flush_error(desc, hdr);
122325b11dcdSTrond Myklebust 		return -ENOMEM;
122444b83799SFred Isaman 	}
12256c75dc0dSFred Isaman 
1226ea2cf228SFred Isaman 	nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
122730dd374fSFred Isaman 	pages = data->pages.pagevec;
12281da177e4SLinus Torvalds 	while (!list_empty(head)) {
12291da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
12301da177e4SLinus Torvalds 		nfs_list_remove_request(req);
12316c75dc0dSFred Isaman 		nfs_list_add_request(req, &hdr->pages);
12321da177e4SLinus Torvalds 		*pages++ = req->wb_page;
12331da177e4SLinus Torvalds 	}
12341da177e4SLinus Torvalds 
1235b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1236ea2cf228SFred Isaman 	    (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
1237b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1238b31268acSTrond Myklebust 
12391da177e4SLinus Torvalds 	/* Set up the argument struct */
1240ea2cf228SFred Isaman 	nfs_write_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo);
12416c75dc0dSFred Isaman 	list_add(&data->list, &hdr->rpc_list);
12426c75dc0dSFred Isaman 	desc->pg_rpc_callops = &nfs_write_common_ops;
124325b11dcdSTrond Myklebust 	return 0;
12441da177e4SLinus Torvalds }
12451da177e4SLinus Torvalds 
12466c75dc0dSFred Isaman int nfs_generic_flush(struct nfs_pageio_descriptor *desc,
12476c75dc0dSFred Isaman 		      struct nfs_pgio_header *hdr)
1248dce81290STrond Myklebust {
1249dce81290STrond Myklebust 	if (desc->pg_bsize < PAGE_CACHE_SIZE)
12506c75dc0dSFred Isaman 		return nfs_flush_multi(desc, hdr);
12516c75dc0dSFred Isaman 	return nfs_flush_one(desc, hdr);
1252dce81290STrond Myklebust }
125389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_generic_flush);
1254dce81290STrond Myklebust 
1255dce81290STrond Myklebust static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
12561751c363STrond Myklebust {
12576c75dc0dSFred Isaman 	struct nfs_write_header *whdr;
12586c75dc0dSFred Isaman 	struct nfs_pgio_header *hdr;
1259275acaafSTrond Myklebust 	int ret;
1260275acaafSTrond Myklebust 
12616c75dc0dSFred Isaman 	whdr = nfs_writehdr_alloc();
12626c75dc0dSFred Isaman 	if (!whdr) {
12639b5415b5STrond Myklebust 		desc->pg_completion_ops->error_cleanup(&desc->pg_list);
12646c75dc0dSFred Isaman 		return -ENOMEM;
12656c75dc0dSFred Isaman 	}
12666c75dc0dSFred Isaman 	hdr = &whdr->header;
12676c75dc0dSFred Isaman 	nfs_pgheader_init(desc, hdr, nfs_writehdr_free);
12686c75dc0dSFred Isaman 	atomic_inc(&hdr->refcnt);
12696c75dc0dSFred Isaman 	ret = nfs_generic_flush(desc, hdr);
1270275acaafSTrond Myklebust 	if (ret == 0)
12716c75dc0dSFred Isaman 		ret = nfs_do_multiple_writes(&hdr->rpc_list,
12726c75dc0dSFred Isaman 					     desc->pg_rpc_callops,
1273dce81290STrond Myklebust 					     desc->pg_ioflags);
12746c75dc0dSFred Isaman 	if (atomic_dec_and_test(&hdr->refcnt))
1275061ae2edSFred Isaman 		hdr->completion_ops->completion(hdr);
1276275acaafSTrond Myklebust 	return ret;
12771751c363STrond Myklebust }
12781751c363STrond Myklebust 
12791751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_write_ops = {
12801751c363STrond Myklebust 	.pg_test = nfs_generic_pg_test,
12811751c363STrond Myklebust 	.pg_doio = nfs_generic_pg_writepages,
12821751c363STrond Myklebust };
12831751c363STrond Myklebust 
128457208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1285061ae2edSFred Isaman 			       struct inode *inode, int ioflags,
1286061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
12871751c363STrond Myklebust {
1288061ae2edSFred Isaman 	nfs_pageio_init(pgio, inode, &nfs_pageio_write_ops, compl_ops,
12891751c363STrond Myklebust 				NFS_SERVER(inode)->wsize, ioflags);
12901751c363STrond Myklebust }
1291ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
12921751c363STrond Myklebust 
1293dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1294dce81290STrond Myklebust {
1295dce81290STrond Myklebust 	pgio->pg_ops = &nfs_pageio_write_ops;
1296dce81290STrond Myklebust 	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1297dce81290STrond Myklebust }
12981f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1299dce81290STrond Myklebust 
13001da177e4SLinus Torvalds 
1301def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata)
1302def6ed7eSAndy Adamson {
1303def6ed7eSAndy Adamson 	struct nfs_write_data *data = calldata;
1304ef1820f9SNeilBrown 	int err;
1305ef1820f9SNeilBrown 	err = NFS_PROTO(data->header->inode)->write_rpc_prepare(task, data);
1306ef1820f9SNeilBrown 	if (err)
1307ef1820f9SNeilBrown 		rpc_exit(task, err);
1308def6ed7eSAndy Adamson }
1309def6ed7eSAndy Adamson 
13100b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
13110b7c0153SFred Isaman {
13120b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
13130b7c0153SFred Isaman 
13140b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
13150b7c0153SFred Isaman }
13160b7c0153SFred Isaman 
13171da177e4SLinus Torvalds /*
13181da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
13191da177e4SLinus Torvalds  *
13201da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
13211da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
13221da177e4SLinus Torvalds  *	  as the page has a write request pending.
13231da177e4SLinus Torvalds  */
13246c75dc0dSFred Isaman static void nfs_writeback_done_common(struct rpc_task *task, void *calldata)
13251da177e4SLinus Torvalds {
1326788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
13271da177e4SLinus Torvalds 
1328c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
1329c9d8f89dSTrond Myklebust }
1330c9d8f89dSTrond Myklebust 
13316c75dc0dSFred Isaman static void nfs_writeback_release_common(void *calldata)
1332c9d8f89dSTrond Myklebust {
1333c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1334cd841605SFred Isaman 	struct nfs_pgio_header *hdr = data->header;
1335e2fecb21STrond Myklebust 	int status = data->task.tk_status;
1336788e7a89STrond Myklebust 
13376c75dc0dSFred Isaman 	if ((status >= 0) && nfs_write_need_commit(data)) {
13386c75dc0dSFred Isaman 		spin_lock(&hdr->lock);
13396c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags))
13406c75dc0dSFred Isaman 			; /* Do nothing */
13416c75dc0dSFred Isaman 		else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags))
13429bce008bSTrond Myklebust 			memcpy(hdr->verf, &data->verf, sizeof(*hdr->verf));
13439bce008bSTrond Myklebust 		else if (memcmp(hdr->verf, &data->verf, sizeof(*hdr->verf)))
13446c75dc0dSFred Isaman 			set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags);
13456c75dc0dSFred Isaman 		spin_unlock(&hdr->lock);
13461da177e4SLinus Torvalds 	}
1347cd841605SFred Isaman 	nfs_writedata_release(data);
13481da177e4SLinus Torvalds }
13491da177e4SLinus Torvalds 
13506c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops = {
1351def6ed7eSAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
13526c75dc0dSFred Isaman 	.rpc_call_done = nfs_writeback_done_common,
13536c75dc0dSFred Isaman 	.rpc_release = nfs_writeback_release_common,
1354788e7a89STrond Myklebust };
1355788e7a89STrond Myklebust 
1356788e7a89STrond Myklebust 
13571da177e4SLinus Torvalds /*
13581da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
13591da177e4SLinus Torvalds  */
136013602896SFred Isaman void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
13611da177e4SLinus Torvalds {
13621da177e4SLinus Torvalds 	struct nfs_writeargs	*argp = &data->args;
13631da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
1364cd841605SFred Isaman 	struct inode		*inode = data->header->inode;
1365788e7a89STrond Myklebust 	int status;
13661da177e4SLinus Torvalds 
1367a3f565b1SChuck Lever 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
13681da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
13691da177e4SLinus Torvalds 
1370f551e44fSChuck Lever 	/*
1371f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1372f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1373f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1374f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1375f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1376f551e44fSChuck Lever 	 */
1377cd841605SFred Isaman 	status = NFS_PROTO(inode)->write_done(task, data);
1378788e7a89STrond Myklebust 	if (status != 0)
137913602896SFred Isaman 		return;
1380cd841605SFred Isaman 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
138191d5b470SChuck Lever 
138289d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
13831da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
13841da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
13851da177e4SLinus Torvalds 		 * commit data to stable storage even though we
13861da177e4SLinus Torvalds 		 * requested it.
13871da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
13881da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
13891da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
13901da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
13911da177e4SLinus Torvalds 		 */
13921da177e4SLinus Torvalds 		static unsigned long    complain;
13931da177e4SLinus Torvalds 
1394a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
13951da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
13961da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
13971da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1398cd841605SFred Isaman 				NFS_SERVER(inode)->nfs_client->cl_hostname,
13991da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
14001da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
14011da177e4SLinus Torvalds 		}
14021da177e4SLinus Torvalds 	}
14031da177e4SLinus Torvalds #endif
14046c75dc0dSFred Isaman 	if (task->tk_status < 0)
14056c75dc0dSFred Isaman 		nfs_set_pgio_error(data->header, task->tk_status, argp->offset);
14066c75dc0dSFred Isaman 	else if (resp->count < argp->count) {
14071da177e4SLinus Torvalds 		static unsigned long    complain;
14081da177e4SLinus Torvalds 
14096c75dc0dSFred Isaman 		/* This a short write! */
1410cd841605SFred Isaman 		nfs_inc_stats(inode, NFSIOS_SHORTWRITE);
141191d5b470SChuck Lever 
14121da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
14136c75dc0dSFred Isaman 		if (resp->count == 0) {
14146c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
14156c75dc0dSFred Isaman 				printk(KERN_WARNING
14166c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
14176c75dc0dSFred Isaman 				       argp->count);
14186c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
14196c75dc0dSFred Isaman 			}
14206c75dc0dSFred Isaman 			nfs_set_pgio_error(data->header, -EIO, argp->offset);
14216c75dc0dSFred Isaman 			task->tk_status = -EIO;
14226c75dc0dSFred Isaman 			return;
14236c75dc0dSFred Isaman 		}
14241da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
14251da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
14261da177e4SLinus Torvalds 			/* Resend from where the server left off */
1427a69aef14SFred Isaman 			data->mds_offset += resp->count;
14281da177e4SLinus Torvalds 			argp->offset += resp->count;
14291da177e4SLinus Torvalds 			argp->pgbase += resp->count;
14301da177e4SLinus Torvalds 			argp->count -= resp->count;
14311da177e4SLinus Torvalds 		} else {
14321da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
14331da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
14341da177e4SLinus Torvalds 			 */
14351da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
14361da177e4SLinus Torvalds 		}
1437d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
14381da177e4SLinus Torvalds 	}
14391da177e4SLinus Torvalds }
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds 
144289d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
144371d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
144471d0a611STrond Myklebust {
1445b8413f98STrond Myklebust 	int ret;
1446b8413f98STrond Myklebust 
144771d0a611STrond Myklebust 	if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
144871d0a611STrond Myklebust 		return 1;
1449b8413f98STrond Myklebust 	if (!may_wait)
145071d0a611STrond Myklebust 		return 0;
1451b8413f98STrond Myklebust 	ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1452b8413f98STrond Myklebust 				NFS_INO_COMMIT,
1453b8413f98STrond Myklebust 				nfs_wait_bit_killable,
1454b8413f98STrond Myklebust 				TASK_KILLABLE);
1455b8413f98STrond Myklebust 	return (ret < 0) ? ret : 1;
145671d0a611STrond Myklebust }
145771d0a611STrond Myklebust 
1458f453a54aSFred Isaman static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
145971d0a611STrond Myklebust {
146071d0a611STrond Myklebust 	clear_bit(NFS_INO_COMMIT, &nfsi->flags);
1461*4e857c58SPeter Zijlstra 	smp_mb__after_atomic();
146271d0a611STrond Myklebust 	wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
146371d0a611STrond Myklebust }
146471d0a611STrond Myklebust 
14650b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
14661da177e4SLinus Torvalds {
14670b7c0153SFred Isaman 	put_nfs_open_context(data->context);
14680b7c0153SFred Isaman 	nfs_commit_free(data);
14691da177e4SLinus Torvalds }
1470e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
14711da177e4SLinus Torvalds 
14720b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
14739ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
14749f0ec176SAndy Adamson 			int how, int flags)
14751da177e4SLinus Torvalds {
147607737691STrond Myklebust 	struct rpc_task *task;
14779ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1478bdc7f021STrond Myklebust 	struct rpc_message msg = {
1479bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1480bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
14819ace33cdSFred Isaman 		.rpc_cred = data->cred,
1482bdc7f021STrond Myklebust 	};
148384115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
148407737691STrond Myklebust 		.task = &data->task,
14859ace33cdSFred Isaman 		.rpc_client = clnt,
1486bdc7f021STrond Myklebust 		.rpc_message = &msg,
14879ace33cdSFred Isaman 		.callback_ops = call_ops,
148884115e1cSTrond Myklebust 		.callback_data = data,
1489101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
14909f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
14913ff7576dSTrond Myklebust 		.priority = priority,
149284115e1cSTrond Myklebust 	};
1493788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
14949ace33cdSFred Isaman 	NFS_PROTO(data->inode)->commit_setup(data, &msg);
14951da177e4SLinus Torvalds 
1496a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1497bdc7f021STrond Myklebust 
14988c21c62cSWeston Andros Adamson 	nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client,
14998c21c62cSWeston Andros Adamson 		NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg);
15008c21c62cSWeston Andros Adamson 
150107737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1502dbae4c73STrond Myklebust 	if (IS_ERR(task))
1503dbae4c73STrond Myklebust 		return PTR_ERR(task);
1504d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1505d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
150607737691STrond Myklebust 	rpc_put_task(task);
1507dbae4c73STrond Myklebust 	return 0;
15081da177e4SLinus Torvalds }
1509e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds /*
15129ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
15139ace33cdSFred Isaman  */
15140b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1515988b6dceSFred Isaman 		     struct list_head *head,
1516f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1517f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
15189ace33cdSFred Isaman {
15199ace33cdSFred Isaman 	struct nfs_page *first = nfs_list_entry(head->next);
15203d4ff43dSAl Viro 	struct inode *inode = first->wb_context->dentry->d_inode;
15219ace33cdSFred Isaman 
15229ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
15239ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
15249ace33cdSFred Isaman 
15259ace33cdSFred Isaman 	list_splice_init(head, &data->pages);
15269ace33cdSFred Isaman 
15279ace33cdSFred Isaman 	data->inode	  = inode;
15289ace33cdSFred Isaman 	data->cred	  = first->wb_context->cred;
1529988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
15309ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1531f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1532b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
15339ace33cdSFred Isaman 
15349ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
15359ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
15369ace33cdSFred Isaman 	data->args.offset = 0;
15379ace33cdSFred Isaman 	data->args.count  = 0;
15380b7c0153SFred Isaman 	data->context     = get_nfs_open_context(first->wb_context);
15399ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
15409ace33cdSFred Isaman 	data->res.verf    = &data->verf;
15419ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
15429ace33cdSFred Isaman }
1543e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
15449ace33cdSFred Isaman 
1545e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1546ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1547ea2cf228SFred Isaman 		      struct nfs_commit_info *cinfo)
154864bfeb49SFred Isaman {
154964bfeb49SFred Isaman 	struct nfs_page *req;
155064bfeb49SFred Isaman 
155164bfeb49SFred Isaman 	while (!list_empty(page_list)) {
155264bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
155364bfeb49SFred Isaman 		nfs_list_remove_request(req);
1554ea2cf228SFred Isaman 		nfs_mark_request_commit(req, lseg, cinfo);
155556f9cd68SFred Isaman 		if (!cinfo->dreq) {
155664bfeb49SFred Isaman 			dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1557d56b4ddfSMel Gorman 			dec_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
155864bfeb49SFred Isaman 				     BDI_RECLAIMABLE);
155956f9cd68SFred Isaman 		}
15601d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
156164bfeb49SFred Isaman 	}
156264bfeb49SFred Isaman }
1563e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
156464bfeb49SFred Isaman 
15659ace33cdSFred Isaman /*
15661da177e4SLinus Torvalds  * Commit dirty pages
15671da177e4SLinus Torvalds  */
15681da177e4SLinus Torvalds static int
1569ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1570ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
15711da177e4SLinus Torvalds {
15720b7c0153SFred Isaman 	struct nfs_commit_data	*data;
15731da177e4SLinus Torvalds 
1574c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
15751da177e4SLinus Torvalds 
15761da177e4SLinus Torvalds 	if (!data)
15771da177e4SLinus Torvalds 		goto out_bad;
15781da177e4SLinus Torvalds 
15791da177e4SLinus Torvalds 	/* Set up the argument struct */
1580f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
1581f453a54aSFred Isaman 	atomic_inc(&cinfo->mds->rpcs_out);
15829f0ec176SAndy Adamson 	return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops,
15839f0ec176SAndy Adamson 				   how, 0);
15841da177e4SLinus Torvalds  out_bad:
1585ea2cf228SFred Isaman 	nfs_retry_commit(head, NULL, cinfo);
1586f453a54aSFred Isaman 	cinfo->completion_ops->error_cleanup(NFS_I(inode));
15871da177e4SLinus Torvalds 	return -ENOMEM;
15881da177e4SLinus Torvalds }
15891da177e4SLinus Torvalds 
15901da177e4SLinus Torvalds /*
15911da177e4SLinus Torvalds  * COMMIT call returned
15921da177e4SLinus Torvalds  */
1593788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
15941da177e4SLinus Torvalds {
15950b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
15961da177e4SLinus Torvalds 
1597a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
15981da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
15991da177e4SLinus Torvalds 
1600788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1601c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
1602c9d8f89dSTrond Myklebust }
1603c9d8f89dSTrond Myklebust 
1604f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1605c9d8f89dSTrond Myklebust {
1606c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1607c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1608f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
1609788e7a89STrond Myklebust 
16101da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
16111da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
16121da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1613d6d6dc7cSFred Isaman 		nfs_clear_page_commit(req->wb_page);
16141da177e4SLinus Torvalds 
16151e8968c5SNiels de Vos 		dprintk("NFS:       commit (%s/%llu %d@%lld)",
16163d4ff43dSAl Viro 			req->wb_context->dentry->d_sb->s_id,
16171e8968c5SNiels de Vos 			(unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
16181da177e4SLinus Torvalds 			req->wb_bytes,
16191da177e4SLinus Torvalds 			(long long)req_offset(req));
1620c9d8f89dSTrond Myklebust 		if (status < 0) {
1621c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
16221da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1623c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
16241da177e4SLinus Torvalds 			goto next;
16251da177e4SLinus Torvalds 		}
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
16281da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
16292f2c63bcSTrond Myklebust 		if (!memcmp(&req->wb_verf, &data->verf.verifier, sizeof(req->wb_verf))) {
16301da177e4SLinus Torvalds 			/* We have a match */
16311da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
16321da177e4SLinus Torvalds 			dprintk(" OK\n");
16331da177e4SLinus Torvalds 			goto next;
16341da177e4SLinus Torvalds 		}
16351da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
16361da177e4SLinus Torvalds 		dprintk(" mismatch\n");
16376d884e8fSFred 		nfs_mark_request_dirty(req);
163805990d1bSTrond Myklebust 		set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
16391da177e4SLinus Torvalds 	next:
16401d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
16411da177e4SLinus Torvalds 	}
1642f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1643f453a54aSFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
1644f453a54aSFred Isaman 		nfs_commit_clear_lock(NFS_I(data->inode));
16455917ce84SFred Isaman }
16465917ce84SFred Isaman 
16475917ce84SFred Isaman static void nfs_commit_release(void *calldata)
16485917ce84SFred Isaman {
16490b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
16505917ce84SFred Isaman 
1651f453a54aSFred Isaman 	data->completion_ops->completion(data);
1652c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
16531da177e4SLinus Torvalds }
1654788e7a89STrond Myklebust 
1655788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
16560b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1657788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1658788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1659788e7a89STrond Myklebust };
16601da177e4SLinus Torvalds 
1661f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1662f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1663f453a54aSFred Isaman 	.error_cleanup = nfs_commit_clear_lock,
1664f453a54aSFred Isaman };
1665f453a54aSFred Isaman 
16661763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1667ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
166884c53ab5SFred Isaman {
166984c53ab5SFred Isaman 	int status;
167084c53ab5SFred Isaman 
1671ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
167284c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1673ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
167484c53ab5SFred Isaman 	return status;
167584c53ab5SFred Isaman }
167684c53ab5SFred Isaman 
1677b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
16781da177e4SLinus Torvalds {
16791da177e4SLinus Torvalds 	LIST_HEAD(head);
1680ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
168171d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1682b8413f98STrond Myklebust 	int res;
16831da177e4SLinus Torvalds 
1684b8413f98STrond Myklebust 	res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1685b8413f98STrond Myklebust 	if (res <= 0)
1686c5efa5fcSTrond Myklebust 		goto out_mark_dirty;
1687ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1688ea2cf228SFred Isaman 	res = nfs_scan_commit(inode, &head, &cinfo);
16891da177e4SLinus Torvalds 	if (res) {
1690a861a1e1SFred Isaman 		int error;
1691a861a1e1SFred Isaman 
1692ea2cf228SFred Isaman 		error = nfs_generic_commit_list(inode, &head, how, &cinfo);
16931da177e4SLinus Torvalds 		if (error < 0)
16941da177e4SLinus Torvalds 			return error;
1695b8413f98STrond Myklebust 		if (!may_wait)
1696b8413f98STrond Myklebust 			goto out_mark_dirty;
1697b8413f98STrond Myklebust 		error = wait_on_bit(&NFS_I(inode)->flags,
1698b8413f98STrond Myklebust 				NFS_INO_COMMIT,
169971d0a611STrond Myklebust 				nfs_wait_bit_killable,
170071d0a611STrond Myklebust 				TASK_KILLABLE);
1701b8413f98STrond Myklebust 		if (error < 0)
1702b8413f98STrond Myklebust 			return error;
170371d0a611STrond Myklebust 	} else
170471d0a611STrond Myklebust 		nfs_commit_clear_lock(NFS_I(inode));
1705c5efa5fcSTrond Myklebust 	return res;
1706c5efa5fcSTrond Myklebust 	/* Note: If we exit without ensuring that the commit is complete,
1707c5efa5fcSTrond Myklebust 	 * we must mark the inode as dirty. Otherwise, future calls to
1708c5efa5fcSTrond Myklebust 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1709c5efa5fcSTrond Myklebust 	 * that the data is on the disk.
1710c5efa5fcSTrond Myklebust 	 */
1711c5efa5fcSTrond Myklebust out_mark_dirty:
1712c5efa5fcSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
17131da177e4SLinus Torvalds 	return res;
17141da177e4SLinus Torvalds }
17158fc795f7STrond Myklebust 
17168fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
17178fc795f7STrond Myklebust {
1718420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1719420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1720420e3646STrond Myklebust 	int ret = 0;
17218fc795f7STrond Myklebust 
17223236c3e1SJeff Layton 	/* no commits means nothing needs to be done */
1723ea2cf228SFred Isaman 	if (!nfsi->commit_info.ncommit)
17243236c3e1SJeff Layton 		return ret;
17253236c3e1SJeff Layton 
1726a00dd6c0SJeff Layton 	if (wbc->sync_mode == WB_SYNC_NONE) {
1727a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1728a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1729420e3646STrond Myklebust 		 */
1730ea2cf228SFred Isaman 		if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1))
1731420e3646STrond Myklebust 			goto out_mark_dirty;
1732420e3646STrond Myklebust 
1733a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1734420e3646STrond Myklebust 		flags = 0;
1735a00dd6c0SJeff Layton 	}
1736a00dd6c0SJeff Layton 
1737420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1738420e3646STrond Myklebust 	if (ret >= 0) {
1739420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1740420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1741420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1742420e3646STrond Myklebust 			else
1743420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1744420e3646STrond Myklebust 		}
17458fc795f7STrond Myklebust 		return 0;
1746420e3646STrond Myklebust 	}
1747420e3646STrond Myklebust out_mark_dirty:
17488fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
17498fc795f7STrond Myklebust 	return ret;
17508fc795f7STrond Myklebust }
1751c63c7b05STrond Myklebust #else
17528fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
17538fc795f7STrond Myklebust {
17548fc795f7STrond Myklebust 	return 0;
17558fc795f7STrond Myklebust }
17561da177e4SLinus Torvalds #endif
17571da177e4SLinus Torvalds 
17588fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
17598fc795f7STrond Myklebust {
1760a8d8f02cSBryan Schumaker 	return nfs_commit_unstable_pages(inode, wbc);
1761a8d8f02cSBryan Schumaker }
176289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode);
1763863a3c6cSAndy Adamson 
1764acdc53b2STrond Myklebust /*
1765acdc53b2STrond Myklebust  * flush the inode to disk.
1766acdc53b2STrond Myklebust  */
1767acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
176834901f70STrond Myklebust {
176934901f70STrond Myklebust 	struct writeback_control wbc = {
177072cb77f4STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
177134901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
1772d7fb1207STrond Myklebust 		.range_start = 0,
1773d7fb1207STrond Myklebust 		.range_end = LLONG_MAX,
177434901f70STrond Myklebust 	};
1775f4ce1299STrond Myklebust 	int ret;
177634901f70STrond Myklebust 
1777f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_enter(inode);
1778f4ce1299STrond Myklebust 
1779f4ce1299STrond Myklebust 	ret = sync_inode(inode, &wbc);
1780f4ce1299STrond Myklebust 
1781f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_exit(inode, ret);
1782f4ce1299STrond Myklebust 	return ret;
17831c75950bSTrond Myklebust }
1784ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all);
17851c75950bSTrond Myklebust 
17861b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
17871b3b4a1aSTrond Myklebust {
17881b3b4a1aSTrond Myklebust 	struct nfs_page *req;
17891b3b4a1aSTrond Myklebust 	int ret = 0;
17901b3b4a1aSTrond Myklebust 
17911b3b4a1aSTrond Myklebust 	for (;;) {
1792ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
17931b3b4a1aSTrond Myklebust 		req = nfs_page_find_request(page);
17941b3b4a1aSTrond Myklebust 		if (req == NULL)
17951b3b4a1aSTrond Myklebust 			break;
17967ad84aa9STrond Myklebust 		if (nfs_lock_request(req)) {
17978dd37758STrond Myklebust 			nfs_clear_request_commit(req);
17981b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
17991b3b4a1aSTrond Myklebust 			/*
18001b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
18011b3b4a1aSTrond Myklebust 			 * page as being dirty
18021b3b4a1aSTrond Myklebust 			 */
18031b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
18041d1afcbcSTrond Myklebust 			nfs_unlock_and_release_request(req);
18051b3b4a1aSTrond Myklebust 			break;
18061b3b4a1aSTrond Myklebust 		}
18071b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
1808c9edda71STrond Myklebust 		nfs_release_request(req);
18091b3b4a1aSTrond Myklebust 		if (ret < 0)
1810c988950eSTrond Myklebust 			break;
18111b3b4a1aSTrond Myklebust 	}
18121b3b4a1aSTrond Myklebust 	return ret;
18131b3b4a1aSTrond Myklebust }
18141b3b4a1aSTrond Myklebust 
18151c75950bSTrond Myklebust /*
18161c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
18171c75950bSTrond Myklebust  */
18181c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
18191c75950bSTrond Myklebust {
182029418aa4SMel Gorman 	loff_t range_start = page_file_offset(page);
18217f2f12d9STrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
18227f2f12d9STrond Myklebust 	struct writeback_control wbc = {
18237f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
18247f2f12d9STrond Myklebust 		.nr_to_write = 0,
18257f2f12d9STrond Myklebust 		.range_start = range_start,
18267f2f12d9STrond Myklebust 		.range_end = range_end,
18277f2f12d9STrond Myklebust 	};
18287f2f12d9STrond Myklebust 	int ret;
18297f2f12d9STrond Myklebust 
1830f4ce1299STrond Myklebust 	trace_nfs_writeback_page_enter(inode);
1831f4ce1299STrond Myklebust 
18320522f6adSTrond Myklebust 	for (;;) {
1833ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
18347f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
18357f2f12d9STrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
18367f2f12d9STrond Myklebust 			if (ret < 0)
18377f2f12d9STrond Myklebust 				goto out_error;
18380522f6adSTrond Myklebust 			continue;
18397f2f12d9STrond Myklebust 		}
1840f4ce1299STrond Myklebust 		ret = 0;
18410522f6adSTrond Myklebust 		if (!PagePrivate(page))
18420522f6adSTrond Myklebust 			break;
18430522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
18447f2f12d9STrond Myklebust 		if (ret < 0)
18457f2f12d9STrond Myklebust 			goto out_error;
18467f2f12d9STrond Myklebust 	}
18477f2f12d9STrond Myklebust out_error:
1848f4ce1299STrond Myklebust 	trace_nfs_writeback_page_exit(inode, ret);
18497f2f12d9STrond Myklebust 	return ret;
18501c75950bSTrond Myklebust }
18511c75950bSTrond Myklebust 
1852074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
1853074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1854a6bc32b8SMel Gorman 		struct page *page, enum migrate_mode mode)
1855074cc1deSTrond Myklebust {
18562da95652SJeff Layton 	/*
18572da95652SJeff Layton 	 * If PagePrivate is set, then the page is currently associated with
18582da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
18592da95652SJeff Layton 	 *
18602da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
18612da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
18622da95652SJeff Layton 	 *        the page lock.
18632da95652SJeff Layton 	 */
18642da95652SJeff Layton 	if (PagePrivate(page))
18652da95652SJeff Layton 		return -EBUSY;
1866074cc1deSTrond Myklebust 
18678c209ce7SDavid Howells 	if (!nfs_fscache_release_page(page, GFP_KERNEL))
18688c209ce7SDavid Howells 		return -EBUSY;
1869074cc1deSTrond Myklebust 
1870a6bc32b8SMel Gorman 	return migrate_page(mapping, newpage, page, mode);
1871074cc1deSTrond Myklebust }
1872074cc1deSTrond Myklebust #endif
1873074cc1deSTrond Myklebust 
1874f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
18751da177e4SLinus Torvalds {
18761da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1877cd841605SFred Isaman 					     sizeof(struct nfs_write_header),
18781da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
187920c2df83SPaul Mundt 					     NULL);
18801da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
18811da177e4SLinus Torvalds 		return -ENOMEM;
18821da177e4SLinus Torvalds 
188393d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
18841da177e4SLinus Torvalds 						     nfs_wdata_cachep);
18851da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
18863dd4765fSJeff Layton 		goto out_destroy_write_cache;
18871da177e4SLinus Torvalds 
18880b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
18890b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
18900b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
18910b7c0153SFred Isaman 					     NULL);
18920b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
18933dd4765fSJeff Layton 		goto out_destroy_write_mempool;
18940b7c0153SFred Isaman 
189593d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
18964c100210SYanchuan Nian 						      nfs_cdata_cachep);
18971da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
18983dd4765fSJeff Layton 		goto out_destroy_commit_cache;
18991da177e4SLinus Torvalds 
190089a09141SPeter Zijlstra 	/*
190189a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
190289a09141SPeter Zijlstra 	 *
190389a09141SPeter Zijlstra 	 *  64MB:    8192k
190489a09141SPeter Zijlstra 	 * 128MB:   11585k
190589a09141SPeter Zijlstra 	 * 256MB:   16384k
190689a09141SPeter Zijlstra 	 * 512MB:   23170k
190789a09141SPeter Zijlstra 	 *   1GB:   32768k
190889a09141SPeter Zijlstra 	 *   2GB:   46340k
190989a09141SPeter Zijlstra 	 *   4GB:   65536k
191089a09141SPeter Zijlstra 	 *   8GB:   92681k
191189a09141SPeter Zijlstra 	 *  16GB:  131072k
191289a09141SPeter Zijlstra 	 *
191389a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
191489a09141SPeter Zijlstra 	 * Limit the default to 256M
191589a09141SPeter Zijlstra 	 */
191689a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
191789a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
191889a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
191989a09141SPeter Zijlstra 
19201da177e4SLinus Torvalds 	return 0;
19213dd4765fSJeff Layton 
19223dd4765fSJeff Layton out_destroy_commit_cache:
19233dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
19243dd4765fSJeff Layton out_destroy_write_mempool:
19253dd4765fSJeff Layton 	mempool_destroy(nfs_wdata_mempool);
19263dd4765fSJeff Layton out_destroy_write_cache:
19273dd4765fSJeff Layton 	kmem_cache_destroy(nfs_wdata_cachep);
19283dd4765fSJeff Layton 	return -ENOMEM;
19291da177e4SLinus Torvalds }
19301da177e4SLinus Torvalds 
1931266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
19321da177e4SLinus Torvalds {
19331da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
19343dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
19351da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
19361a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
19371da177e4SLinus Torvalds }
19381da177e4SLinus Torvalds 
1939