xref: /openbmc/linux/fs/nfs/write.c (revision 6de1472f)
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);
40872cb77f4STrond Myklebust 	smp_mb__after_clear_bit();
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 {
9128d197a56STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
9138d197a56STrond Myklebust 		goto out;
91481d9bce5SJeff Layton 	if (NFS_I(inode)->cache_validity & (NFS_INO_INVALID_DATA|NFS_INO_REVAL_PAGECACHE))
9158d197a56STrond Myklebust 		return false;
9168d197a56STrond Myklebust out:
9178d197a56STrond Myklebust 	return PageUptodate(page) != 0;
9185d47a356STrond Myklebust }
9195d47a356STrond Myklebust 
920c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or
921c7559663SScott Mayhew  * if we have the whole file locked for writing), it may be more efficient to
922c7559663SScott Mayhew  * extend the write to cover the entire page in order to avoid fragmentation
923c7559663SScott Mayhew  * inefficiencies.
924c7559663SScott Mayhew  *
925c7559663SScott Mayhew  * If the file is opened for synchronous writes or if we have a write delegation
926c7559663SScott Mayhew  * from the server then we can just skip the rest of the checks.
927c7559663SScott Mayhew  */
928c7559663SScott Mayhew static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode)
929c7559663SScott Mayhew {
930c7559663SScott Mayhew 	if (file->f_flags & O_DSYNC)
931c7559663SScott Mayhew 		return 0;
932c7559663SScott Mayhew 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
933c7559663SScott Mayhew 		return 1;
934c7559663SScott Mayhew 	if (nfs_write_pageuptodate(page, inode) && (inode->i_flock == NULL ||
935c7559663SScott Mayhew 			(inode->i_flock->fl_start == 0 &&
936c7559663SScott Mayhew 			inode->i_flock->fl_end == OFFSET_MAX &&
937c7559663SScott Mayhew 			inode->i_flock->fl_type != F_RDLCK)))
938c7559663SScott Mayhew 		return 1;
939c7559663SScott Mayhew 	return 0;
940c7559663SScott Mayhew }
941c7559663SScott Mayhew 
9425d47a356STrond Myklebust /*
9431da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
9441da177e4SLinus Torvalds  *
9451da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
9461da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
9471da177e4SLinus Torvalds  */
9481da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
9491da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
9501da177e4SLinus Torvalds {
951cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
952d56b4ddfSMel Gorman 	struct inode	*inode = page_file_mapping(page)->host;
9531da177e4SLinus Torvalds 	int		status = 0;
9541da177e4SLinus Torvalds 
95591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
95691d5b470SChuck Lever 
9576de1472fSAl Viro 	dprintk("NFS:       nfs_updatepage(%pD2 %d@%lld)\n",
9586de1472fSAl Viro 		file, count, (long long)(page_file_offset(page) + offset));
9591da177e4SLinus Torvalds 
960c7559663SScott Mayhew 	if (nfs_can_extend_write(file, page, inode)) {
96149a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
9621da177e4SLinus Torvalds 		offset = 0;
9631da177e4SLinus Torvalds 	}
9641da177e4SLinus Torvalds 
965e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
96603fa9e84STrond Myklebust 	if (status < 0)
96703fa9e84STrond Myklebust 		nfs_set_pageerror(page);
96859b7c05fSTrond Myklebust 	else
96959b7c05fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
9701da177e4SLinus Torvalds 
97148186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
9721da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
9731da177e4SLinus Torvalds 	return status;
9741da177e4SLinus Torvalds }
9751da177e4SLinus Torvalds 
9763ff7576dSTrond Myklebust static int flush_task_priority(int how)
9771da177e4SLinus Torvalds {
9781da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
9791da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
9801da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
9811da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
9821da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
9831da177e4SLinus Torvalds 	}
9841da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
9851da177e4SLinus Torvalds }
9861da177e4SLinus Torvalds 
987c5996c4eSFred Isaman int nfs_initiate_write(struct rpc_clnt *clnt,
988c5996c4eSFred Isaman 		       struct nfs_write_data *data,
989788e7a89STrond Myklebust 		       const struct rpc_call_ops *call_ops,
9909f0ec176SAndy Adamson 		       int how, int flags)
9911da177e4SLinus Torvalds {
992cd841605SFred Isaman 	struct inode *inode = data->header->inode;
9933ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
99407737691STrond Myklebust 	struct rpc_task *task;
995bdc7f021STrond Myklebust 	struct rpc_message msg = {
996bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
997bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
998cd841605SFred Isaman 		.rpc_cred = data->header->cred,
999bdc7f021STrond Myklebust 	};
100084115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
1001d138d5d1SAndy Adamson 		.rpc_client = clnt,
100207737691STrond Myklebust 		.task = &data->task,
1003bdc7f021STrond Myklebust 		.rpc_message = &msg,
100484115e1cSTrond Myklebust 		.callback_ops = call_ops,
100584115e1cSTrond Myklebust 		.callback_data = data,
1006101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
10079f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
10083ff7576dSTrond Myklebust 		.priority = priority,
100984115e1cSTrond Myklebust 	};
10102c61be0aSTrond Myklebust 	int ret = 0;
10111da177e4SLinus Torvalds 
1012d138d5d1SAndy Adamson 	/* Set up the initial task struct.  */
1013d138d5d1SAndy Adamson 	NFS_PROTO(inode)->write_setup(data, &msg);
1014d138d5d1SAndy Adamson 
1015d138d5d1SAndy Adamson 	dprintk("NFS: %5u initiated write call "
1016d138d5d1SAndy Adamson 		"(req %s/%lld, %u bytes @ offset %llu)\n",
1017d138d5d1SAndy Adamson 		data->task.tk_pid,
1018d138d5d1SAndy Adamson 		inode->i_sb->s_id,
1019d138d5d1SAndy Adamson 		(long long)NFS_FILEID(inode),
1020d138d5d1SAndy Adamson 		data->args.count,
1021d138d5d1SAndy Adamson 		(unsigned long long)data->args.offset);
1022d138d5d1SAndy Adamson 
10238c21c62cSWeston Andros Adamson 	nfs4_state_protect_write(NFS_SERVER(inode)->nfs_client,
10248c21c62cSWeston Andros Adamson 				 &task_setup_data.rpc_client, &msg, data);
10258c21c62cSWeston Andros Adamson 
1026d138d5d1SAndy Adamson 	task = rpc_run_task(&task_setup_data);
1027d138d5d1SAndy Adamson 	if (IS_ERR(task)) {
1028d138d5d1SAndy Adamson 		ret = PTR_ERR(task);
1029d138d5d1SAndy Adamson 		goto out;
1030d138d5d1SAndy Adamson 	}
1031d138d5d1SAndy Adamson 	if (how & FLUSH_SYNC) {
1032d138d5d1SAndy Adamson 		ret = rpc_wait_for_completion_task(task);
1033d138d5d1SAndy Adamson 		if (ret == 0)
1034d138d5d1SAndy Adamson 			ret = task->tk_status;
1035d138d5d1SAndy Adamson 	}
1036d138d5d1SAndy Adamson 	rpc_put_task(task);
1037d138d5d1SAndy Adamson out:
1038d138d5d1SAndy Adamson 	return ret;
1039d138d5d1SAndy Adamson }
1040a69aef14SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_write);
1041d138d5d1SAndy Adamson 
1042d138d5d1SAndy Adamson /*
1043d138d5d1SAndy Adamson  * Set up the argument/result storage required for the RPC call.
1044d138d5d1SAndy Adamson  */
10456c75dc0dSFred Isaman static void nfs_write_rpcsetup(struct nfs_write_data *data,
1046d138d5d1SAndy Adamson 		unsigned int count, unsigned int offset,
1047ea2cf228SFred Isaman 		int how, struct nfs_commit_info *cinfo)
1048d138d5d1SAndy Adamson {
10496c75dc0dSFred Isaman 	struct nfs_page *req = data->header->req;
1050d138d5d1SAndy Adamson 
10511da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
10521da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
10531da177e4SLinus Torvalds 
10546c75dc0dSFred Isaman 	data->args.fh     = NFS_FH(data->header->inode);
10551da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
10562bea038cSBoaz Harrosh 	/* pnfs_set_layoutcommit needs this */
10572bea038cSBoaz Harrosh 	data->mds_offset = data->args.offset;
10581da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
105930dd374fSFred Isaman 	data->args.pages  = data->pages.pagevec;
10601da177e4SLinus Torvalds 	data->args.count  = count;
1061383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
1062f11ac8dbSTrond Myklebust 	data->args.lock_context = req->wb_lock_context;
1063bdc7f021STrond Myklebust 	data->args.stable  = NFS_UNSTABLE;
106487ed5eb4STrond Myklebust 	switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
106587ed5eb4STrond Myklebust 	case 0:
106687ed5eb4STrond Myklebust 		break;
106787ed5eb4STrond Myklebust 	case FLUSH_COND_STABLE:
1068ea2cf228SFred Isaman 		if (nfs_reqs_to_commit(cinfo))
106987ed5eb4STrond Myklebust 			break;
107087ed5eb4STrond Myklebust 	default:
1071bdc7f021STrond Myklebust 		data->args.stable = NFS_FILE_SYNC;
1072bdc7f021STrond Myklebust 	}
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
10751da177e4SLinus Torvalds 	data->res.count   = count;
10761da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
10770e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
10786e4efd56STrond Myklebust }
10791da177e4SLinus Torvalds 
10806e4efd56STrond Myklebust static int nfs_do_write(struct nfs_write_data *data,
10816e4efd56STrond Myklebust 		const struct rpc_call_ops *call_ops,
10826e4efd56STrond Myklebust 		int how)
10836e4efd56STrond Myklebust {
1084cd841605SFred Isaman 	struct inode *inode = data->header->inode;
10850382b744SAndy Adamson 
10869f0ec176SAndy Adamson 	return nfs_initiate_write(NFS_CLIENT(inode), data, call_ops, how, 0);
10871da177e4SLinus Torvalds }
10881da177e4SLinus Torvalds 
1089275acaafSTrond Myklebust static int nfs_do_multiple_writes(struct list_head *head,
1090275acaafSTrond Myklebust 		const struct rpc_call_ops *call_ops,
1091275acaafSTrond Myklebust 		int how)
1092275acaafSTrond Myklebust {
1093275acaafSTrond Myklebust 	struct nfs_write_data *data;
1094275acaafSTrond Myklebust 	int ret = 0;
1095275acaafSTrond Myklebust 
1096275acaafSTrond Myklebust 	while (!list_empty(head)) {
1097275acaafSTrond Myklebust 		int ret2;
1098275acaafSTrond Myklebust 
10996c75dc0dSFred Isaman 		data = list_first_entry(head, struct nfs_write_data, list);
1100275acaafSTrond Myklebust 		list_del_init(&data->list);
1101275acaafSTrond Myklebust 
1102dce81290STrond Myklebust 		ret2 = nfs_do_write(data, call_ops, how);
1103275acaafSTrond Myklebust 		 if (ret == 0)
1104275acaafSTrond Myklebust 			 ret = ret2;
1105275acaafSTrond Myklebust 	}
1106275acaafSTrond Myklebust 	return ret;
1107275acaafSTrond Myklebust }
1108275acaafSTrond Myklebust 
11096d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
11106d884e8fSFred  * call this on each, which will prepare them to be retried on next
11116d884e8fSFred  * writeback using standard nfs.
11126d884e8fSFred  */
11136d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
11146d884e8fSFred {
11156d884e8fSFred 	nfs_mark_request_dirty(req);
11161d1afcbcSTrond Myklebust 	nfs_unlock_request(req);
1117d1182b33STrond Myklebust 	nfs_end_page_writeback(req->wb_page);
11183aff4ebbSTrond Myklebust 	nfs_release_request(req);
11196d884e8fSFred }
11206d884e8fSFred 
1121061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head)
11226c75dc0dSFred Isaman {
11236c75dc0dSFred Isaman 	struct nfs_page	*req;
11246c75dc0dSFred Isaman 
11256c75dc0dSFred Isaman 	while (!list_empty(head)) {
11266c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
11276c75dc0dSFred Isaman 		nfs_list_remove_request(req);
11286c75dc0dSFred Isaman 		nfs_redirty_request(req);
11296c75dc0dSFred Isaman 	}
11306c75dc0dSFred Isaman }
11316c75dc0dSFred Isaman 
1132061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1133061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1134061ae2edSFred Isaman 	.completion = nfs_write_completion,
1135061ae2edSFred Isaman };
1136061ae2edSFred Isaman 
113725b11dcdSTrond Myklebust static void nfs_flush_error(struct nfs_pageio_descriptor *desc,
113825b11dcdSTrond Myklebust 		struct nfs_pgio_header *hdr)
113925b11dcdSTrond Myklebust {
114025b11dcdSTrond Myklebust 	set_bit(NFS_IOHDR_REDO, &hdr->flags);
114125b11dcdSTrond Myklebust 	while (!list_empty(&hdr->rpc_list)) {
114225b11dcdSTrond Myklebust 		struct nfs_write_data *data = list_first_entry(&hdr->rpc_list,
114325b11dcdSTrond Myklebust 				struct nfs_write_data, list);
114425b11dcdSTrond Myklebust 		list_del(&data->list);
114525b11dcdSTrond Myklebust 		nfs_writedata_release(data);
114625b11dcdSTrond Myklebust 	}
114725b11dcdSTrond Myklebust 	desc->pg_completion_ops->error_cleanup(&desc->pg_list);
114825b11dcdSTrond Myklebust }
114925b11dcdSTrond Myklebust 
11501da177e4SLinus Torvalds /*
11511da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
11521da177e4SLinus Torvalds  * contiguous dirty area on one page.
11531da177e4SLinus Torvalds  */
11546c75dc0dSFred Isaman static int nfs_flush_multi(struct nfs_pageio_descriptor *desc,
11556c75dc0dSFred Isaman 			   struct nfs_pgio_header *hdr)
11561da177e4SLinus Torvalds {
11576c75dc0dSFred Isaman 	struct nfs_page *req = hdr->req;
11581da177e4SLinus Torvalds 	struct page *page = req->wb_page;
11591da177e4SLinus Torvalds 	struct nfs_write_data *data;
1160d097971dSTrond Myklebust 	size_t wsize = desc->pg_bsize, nbytes;
1161e9f7bee1STrond Myklebust 	unsigned int offset;
11621da177e4SLinus Torvalds 	int requests = 0;
1163ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
11641da177e4SLinus Torvalds 
1165ea2cf228SFred Isaman 	nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
11661da177e4SLinus Torvalds 
1167b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1168ea2cf228SFred Isaman 	    (desc->pg_moreio || nfs_reqs_to_commit(&cinfo) ||
1169b31268acSTrond Myklebust 	     desc->pg_count > wsize))
1170b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1171b31268acSTrond Myklebust 
1172b31268acSTrond Myklebust 
1173275acaafSTrond Myklebust 	offset = 0;
1174c76069bdSFred Isaman 	nbytes = desc->pg_count;
1175e9f7bee1STrond Myklebust 	do {
1176e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
1177e9f7bee1STrond Myklebust 
11786c75dc0dSFred Isaman 		data = nfs_writedata_alloc(hdr, 1);
117925b11dcdSTrond Myklebust 		if (!data) {
118025b11dcdSTrond Myklebust 			nfs_flush_error(desc, hdr);
118125b11dcdSTrond Myklebust 			return -ENOMEM;
118225b11dcdSTrond Myklebust 		}
118330dd374fSFred Isaman 		data->pages.pagevec[0] = page;
1184ea2cf228SFred Isaman 		nfs_write_rpcsetup(data, len, offset, desc->pg_ioflags, &cinfo);
11856c75dc0dSFred Isaman 		list_add(&data->list, &hdr->rpc_list);
11861da177e4SLinus Torvalds 		requests++;
1187e9f7bee1STrond Myklebust 		nbytes -= len;
1188275acaafSTrond Myklebust 		offset += len;
1189e9f7bee1STrond Myklebust 	} while (nbytes != 0);
119025b11dcdSTrond Myklebust 	nfs_list_remove_request(req);
119125b11dcdSTrond Myklebust 	nfs_list_add_request(req, &hdr->pages);
11926c75dc0dSFred Isaman 	desc->pg_rpc_callops = &nfs_write_common_ops;
119325b11dcdSTrond Myklebust 	return 0;
11941da177e4SLinus Torvalds }
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds /*
11971da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
11981da177e4SLinus Torvalds  * The page must have been locked by the caller.
11991da177e4SLinus Torvalds  *
12001da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
12011da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
12021da177e4SLinus Torvalds  * that has been written but not committed.
12031da177e4SLinus Torvalds  */
12046c75dc0dSFred Isaman static int nfs_flush_one(struct nfs_pageio_descriptor *desc,
12056c75dc0dSFred Isaman 			 struct nfs_pgio_header *hdr)
12061da177e4SLinus Torvalds {
12071da177e4SLinus Torvalds 	struct nfs_page		*req;
12081da177e4SLinus Torvalds 	struct page		**pages;
12091da177e4SLinus Torvalds 	struct nfs_write_data	*data;
1210c76069bdSFred Isaman 	struct list_head *head = &desc->pg_list;
1211ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
12121da177e4SLinus Torvalds 
12136c75dc0dSFred Isaman 	data = nfs_writedata_alloc(hdr, nfs_page_array_len(desc->pg_base,
1214c76069bdSFred Isaman 							   desc->pg_count));
12156c75dc0dSFred Isaman 	if (!data) {
121625b11dcdSTrond Myklebust 		nfs_flush_error(desc, hdr);
121725b11dcdSTrond Myklebust 		return -ENOMEM;
121844b83799SFred Isaman 	}
12196c75dc0dSFred Isaman 
1220ea2cf228SFred Isaman 	nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
122130dd374fSFred Isaman 	pages = data->pages.pagevec;
12221da177e4SLinus Torvalds 	while (!list_empty(head)) {
12231da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
12241da177e4SLinus Torvalds 		nfs_list_remove_request(req);
12256c75dc0dSFred Isaman 		nfs_list_add_request(req, &hdr->pages);
12261da177e4SLinus Torvalds 		*pages++ = req->wb_page;
12271da177e4SLinus Torvalds 	}
12281da177e4SLinus Torvalds 
1229b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1230ea2cf228SFred Isaman 	    (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
1231b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1232b31268acSTrond Myklebust 
12331da177e4SLinus Torvalds 	/* Set up the argument struct */
1234ea2cf228SFred Isaman 	nfs_write_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo);
12356c75dc0dSFred Isaman 	list_add(&data->list, &hdr->rpc_list);
12366c75dc0dSFred Isaman 	desc->pg_rpc_callops = &nfs_write_common_ops;
123725b11dcdSTrond Myklebust 	return 0;
12381da177e4SLinus Torvalds }
12391da177e4SLinus Torvalds 
12406c75dc0dSFred Isaman int nfs_generic_flush(struct nfs_pageio_descriptor *desc,
12416c75dc0dSFred Isaman 		      struct nfs_pgio_header *hdr)
1242dce81290STrond Myklebust {
1243dce81290STrond Myklebust 	if (desc->pg_bsize < PAGE_CACHE_SIZE)
12446c75dc0dSFred Isaman 		return nfs_flush_multi(desc, hdr);
12456c75dc0dSFred Isaman 	return nfs_flush_one(desc, hdr);
1246dce81290STrond Myklebust }
124789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_generic_flush);
1248dce81290STrond Myklebust 
1249dce81290STrond Myklebust static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
12501751c363STrond Myklebust {
12516c75dc0dSFred Isaman 	struct nfs_write_header *whdr;
12526c75dc0dSFred Isaman 	struct nfs_pgio_header *hdr;
1253275acaafSTrond Myklebust 	int ret;
1254275acaafSTrond Myklebust 
12556c75dc0dSFred Isaman 	whdr = nfs_writehdr_alloc();
12566c75dc0dSFred Isaman 	if (!whdr) {
12579b5415b5STrond Myklebust 		desc->pg_completion_ops->error_cleanup(&desc->pg_list);
12586c75dc0dSFred Isaman 		return -ENOMEM;
12596c75dc0dSFred Isaman 	}
12606c75dc0dSFred Isaman 	hdr = &whdr->header;
12616c75dc0dSFred Isaman 	nfs_pgheader_init(desc, hdr, nfs_writehdr_free);
12626c75dc0dSFred Isaman 	atomic_inc(&hdr->refcnt);
12636c75dc0dSFred Isaman 	ret = nfs_generic_flush(desc, hdr);
1264275acaafSTrond Myklebust 	if (ret == 0)
12656c75dc0dSFred Isaman 		ret = nfs_do_multiple_writes(&hdr->rpc_list,
12666c75dc0dSFred Isaman 					     desc->pg_rpc_callops,
1267dce81290STrond Myklebust 					     desc->pg_ioflags);
12686c75dc0dSFred Isaman 	if (atomic_dec_and_test(&hdr->refcnt))
1269061ae2edSFred Isaman 		hdr->completion_ops->completion(hdr);
1270275acaafSTrond Myklebust 	return ret;
12711751c363STrond Myklebust }
12721751c363STrond Myklebust 
12731751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_write_ops = {
12741751c363STrond Myklebust 	.pg_test = nfs_generic_pg_test,
12751751c363STrond Myklebust 	.pg_doio = nfs_generic_pg_writepages,
12761751c363STrond Myklebust };
12771751c363STrond Myklebust 
127857208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1279061ae2edSFred Isaman 			       struct inode *inode, int ioflags,
1280061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
12811751c363STrond Myklebust {
1282061ae2edSFred Isaman 	nfs_pageio_init(pgio, inode, &nfs_pageio_write_ops, compl_ops,
12831751c363STrond Myklebust 				NFS_SERVER(inode)->wsize, ioflags);
12841751c363STrond Myklebust }
1285ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
12861751c363STrond Myklebust 
1287dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1288dce81290STrond Myklebust {
1289dce81290STrond Myklebust 	pgio->pg_ops = &nfs_pageio_write_ops;
1290dce81290STrond Myklebust 	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1291dce81290STrond Myklebust }
12921f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1293dce81290STrond Myklebust 
12941da177e4SLinus Torvalds 
1295def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata)
1296def6ed7eSAndy Adamson {
1297def6ed7eSAndy Adamson 	struct nfs_write_data *data = calldata;
1298ef1820f9SNeilBrown 	int err;
1299ef1820f9SNeilBrown 	err = NFS_PROTO(data->header->inode)->write_rpc_prepare(task, data);
1300ef1820f9SNeilBrown 	if (err)
1301ef1820f9SNeilBrown 		rpc_exit(task, err);
1302def6ed7eSAndy Adamson }
1303def6ed7eSAndy Adamson 
13040b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
13050b7c0153SFred Isaman {
13060b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
13070b7c0153SFred Isaman 
13080b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
13090b7c0153SFred Isaman }
13100b7c0153SFred Isaman 
13111da177e4SLinus Torvalds /*
13121da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
13131da177e4SLinus Torvalds  *
13141da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
13151da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
13161da177e4SLinus Torvalds  *	  as the page has a write request pending.
13171da177e4SLinus Torvalds  */
13186c75dc0dSFred Isaman static void nfs_writeback_done_common(struct rpc_task *task, void *calldata)
13191da177e4SLinus Torvalds {
1320788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
13211da177e4SLinus Torvalds 
1322c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
1323c9d8f89dSTrond Myklebust }
1324c9d8f89dSTrond Myklebust 
13256c75dc0dSFred Isaman static void nfs_writeback_release_common(void *calldata)
1326c9d8f89dSTrond Myklebust {
1327c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1328cd841605SFred Isaman 	struct nfs_pgio_header *hdr = data->header;
1329e2fecb21STrond Myklebust 	int status = data->task.tk_status;
1330788e7a89STrond Myklebust 
13316c75dc0dSFred Isaman 	if ((status >= 0) && nfs_write_need_commit(data)) {
13326c75dc0dSFred Isaman 		spin_lock(&hdr->lock);
13336c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags))
13346c75dc0dSFred Isaman 			; /* Do nothing */
13356c75dc0dSFred Isaman 		else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags))
13369bce008bSTrond Myklebust 			memcpy(hdr->verf, &data->verf, sizeof(*hdr->verf));
13379bce008bSTrond Myklebust 		else if (memcmp(hdr->verf, &data->verf, sizeof(*hdr->verf)))
13386c75dc0dSFred Isaman 			set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags);
13396c75dc0dSFred Isaman 		spin_unlock(&hdr->lock);
13401da177e4SLinus Torvalds 	}
1341cd841605SFred Isaman 	nfs_writedata_release(data);
13421da177e4SLinus Torvalds }
13431da177e4SLinus Torvalds 
13446c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops = {
1345def6ed7eSAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
13466c75dc0dSFred Isaman 	.rpc_call_done = nfs_writeback_done_common,
13476c75dc0dSFred Isaman 	.rpc_release = nfs_writeback_release_common,
1348788e7a89STrond Myklebust };
1349788e7a89STrond Myklebust 
1350788e7a89STrond Myklebust 
13511da177e4SLinus Torvalds /*
13521da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
13531da177e4SLinus Torvalds  */
135413602896SFred Isaman void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
13551da177e4SLinus Torvalds {
13561da177e4SLinus Torvalds 	struct nfs_writeargs	*argp = &data->args;
13571da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
1358cd841605SFred Isaman 	struct inode		*inode = data->header->inode;
1359788e7a89STrond Myklebust 	int status;
13601da177e4SLinus Torvalds 
1361a3f565b1SChuck Lever 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
13621da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
13631da177e4SLinus Torvalds 
1364f551e44fSChuck Lever 	/*
1365f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1366f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1367f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1368f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1369f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1370f551e44fSChuck Lever 	 */
1371cd841605SFred Isaman 	status = NFS_PROTO(inode)->write_done(task, data);
1372788e7a89STrond Myklebust 	if (status != 0)
137313602896SFred Isaman 		return;
1374cd841605SFred Isaman 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
137591d5b470SChuck Lever 
137689d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
13771da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
13781da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
13791da177e4SLinus Torvalds 		 * commit data to stable storage even though we
13801da177e4SLinus Torvalds 		 * requested it.
13811da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
13821da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
13831da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
13841da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
13851da177e4SLinus Torvalds 		 */
13861da177e4SLinus Torvalds 		static unsigned long    complain;
13871da177e4SLinus Torvalds 
1388a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
13891da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
13901da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
13911da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1392cd841605SFred Isaman 				NFS_SERVER(inode)->nfs_client->cl_hostname,
13931da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
13941da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
13951da177e4SLinus Torvalds 		}
13961da177e4SLinus Torvalds 	}
13971da177e4SLinus Torvalds #endif
13986c75dc0dSFred Isaman 	if (task->tk_status < 0)
13996c75dc0dSFred Isaman 		nfs_set_pgio_error(data->header, task->tk_status, argp->offset);
14006c75dc0dSFred Isaman 	else if (resp->count < argp->count) {
14011da177e4SLinus Torvalds 		static unsigned long    complain;
14021da177e4SLinus Torvalds 
14036c75dc0dSFred Isaman 		/* This a short write! */
1404cd841605SFred Isaman 		nfs_inc_stats(inode, NFSIOS_SHORTWRITE);
140591d5b470SChuck Lever 
14061da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
14076c75dc0dSFred Isaman 		if (resp->count == 0) {
14086c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
14096c75dc0dSFred Isaman 				printk(KERN_WARNING
14106c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
14116c75dc0dSFred Isaman 				       argp->count);
14126c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
14136c75dc0dSFred Isaman 			}
14146c75dc0dSFred Isaman 			nfs_set_pgio_error(data->header, -EIO, argp->offset);
14156c75dc0dSFred Isaman 			task->tk_status = -EIO;
14166c75dc0dSFred Isaman 			return;
14176c75dc0dSFred Isaman 		}
14181da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
14191da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
14201da177e4SLinus Torvalds 			/* Resend from where the server left off */
1421a69aef14SFred Isaman 			data->mds_offset += resp->count;
14221da177e4SLinus Torvalds 			argp->offset += resp->count;
14231da177e4SLinus Torvalds 			argp->pgbase += resp->count;
14241da177e4SLinus Torvalds 			argp->count -= resp->count;
14251da177e4SLinus Torvalds 		} else {
14261da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
14271da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
14281da177e4SLinus Torvalds 			 */
14291da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
14301da177e4SLinus Torvalds 		}
1431d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
14321da177e4SLinus Torvalds 	}
14331da177e4SLinus Torvalds }
14341da177e4SLinus Torvalds 
14351da177e4SLinus Torvalds 
143689d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
143771d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
143871d0a611STrond Myklebust {
1439b8413f98STrond Myklebust 	int ret;
1440b8413f98STrond Myklebust 
144171d0a611STrond Myklebust 	if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
144271d0a611STrond Myklebust 		return 1;
1443b8413f98STrond Myklebust 	if (!may_wait)
144471d0a611STrond Myklebust 		return 0;
1445b8413f98STrond Myklebust 	ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1446b8413f98STrond Myklebust 				NFS_INO_COMMIT,
1447b8413f98STrond Myklebust 				nfs_wait_bit_killable,
1448b8413f98STrond Myklebust 				TASK_KILLABLE);
1449b8413f98STrond Myklebust 	return (ret < 0) ? ret : 1;
145071d0a611STrond Myklebust }
145171d0a611STrond Myklebust 
1452f453a54aSFred Isaman static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
145371d0a611STrond Myklebust {
145471d0a611STrond Myklebust 	clear_bit(NFS_INO_COMMIT, &nfsi->flags);
145571d0a611STrond Myklebust 	smp_mb__after_clear_bit();
145671d0a611STrond Myklebust 	wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
145771d0a611STrond Myklebust }
145871d0a611STrond Myklebust 
14590b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
14601da177e4SLinus Torvalds {
14610b7c0153SFred Isaman 	put_nfs_open_context(data->context);
14620b7c0153SFred Isaman 	nfs_commit_free(data);
14631da177e4SLinus Torvalds }
1464e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
14651da177e4SLinus Torvalds 
14660b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
14679ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
14689f0ec176SAndy Adamson 			int how, int flags)
14691da177e4SLinus Torvalds {
147007737691STrond Myklebust 	struct rpc_task *task;
14719ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1472bdc7f021STrond Myklebust 	struct rpc_message msg = {
1473bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1474bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
14759ace33cdSFred Isaman 		.rpc_cred = data->cred,
1476bdc7f021STrond Myklebust 	};
147784115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
147807737691STrond Myklebust 		.task = &data->task,
14799ace33cdSFred Isaman 		.rpc_client = clnt,
1480bdc7f021STrond Myklebust 		.rpc_message = &msg,
14819ace33cdSFred Isaman 		.callback_ops = call_ops,
148284115e1cSTrond Myklebust 		.callback_data = data,
1483101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
14849f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
14853ff7576dSTrond Myklebust 		.priority = priority,
148684115e1cSTrond Myklebust 	};
1487788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
14889ace33cdSFred Isaman 	NFS_PROTO(data->inode)->commit_setup(data, &msg);
14891da177e4SLinus Torvalds 
1490a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1491bdc7f021STrond Myklebust 
14928c21c62cSWeston Andros Adamson 	nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client,
14938c21c62cSWeston Andros Adamson 		NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg);
14948c21c62cSWeston Andros Adamson 
149507737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1496dbae4c73STrond Myklebust 	if (IS_ERR(task))
1497dbae4c73STrond Myklebust 		return PTR_ERR(task);
1498d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1499d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
150007737691STrond Myklebust 	rpc_put_task(task);
1501dbae4c73STrond Myklebust 	return 0;
15021da177e4SLinus Torvalds }
1503e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds /*
15069ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
15079ace33cdSFred Isaman  */
15080b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1509988b6dceSFred Isaman 		     struct list_head *head,
1510f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1511f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
15129ace33cdSFred Isaman {
15139ace33cdSFred Isaman 	struct nfs_page *first = nfs_list_entry(head->next);
15143d4ff43dSAl Viro 	struct inode *inode = first->wb_context->dentry->d_inode;
15159ace33cdSFred Isaman 
15169ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
15179ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
15189ace33cdSFred Isaman 
15199ace33cdSFred Isaman 	list_splice_init(head, &data->pages);
15209ace33cdSFred Isaman 
15219ace33cdSFred Isaman 	data->inode	  = inode;
15229ace33cdSFred Isaman 	data->cred	  = first->wb_context->cred;
1523988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
15249ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1525f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1526b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
15279ace33cdSFred Isaman 
15289ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
15299ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
15309ace33cdSFred Isaman 	data->args.offset = 0;
15319ace33cdSFred Isaman 	data->args.count  = 0;
15320b7c0153SFred Isaman 	data->context     = get_nfs_open_context(first->wb_context);
15339ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
15349ace33cdSFred Isaman 	data->res.verf    = &data->verf;
15359ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
15369ace33cdSFred Isaman }
1537e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
15389ace33cdSFred Isaman 
1539e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1540ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1541ea2cf228SFred Isaman 		      struct nfs_commit_info *cinfo)
154264bfeb49SFred Isaman {
154364bfeb49SFred Isaman 	struct nfs_page *req;
154464bfeb49SFred Isaman 
154564bfeb49SFred Isaman 	while (!list_empty(page_list)) {
154664bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
154764bfeb49SFred Isaman 		nfs_list_remove_request(req);
1548ea2cf228SFred Isaman 		nfs_mark_request_commit(req, lseg, cinfo);
154956f9cd68SFred Isaman 		if (!cinfo->dreq) {
155064bfeb49SFred Isaman 			dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1551d56b4ddfSMel Gorman 			dec_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
155264bfeb49SFred Isaman 				     BDI_RECLAIMABLE);
155356f9cd68SFred Isaman 		}
15541d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
155564bfeb49SFred Isaman 	}
155664bfeb49SFred Isaman }
1557e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
155864bfeb49SFred Isaman 
15599ace33cdSFred Isaman /*
15601da177e4SLinus Torvalds  * Commit dirty pages
15611da177e4SLinus Torvalds  */
15621da177e4SLinus Torvalds static int
1563ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1564ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
15651da177e4SLinus Torvalds {
15660b7c0153SFred Isaman 	struct nfs_commit_data	*data;
15671da177e4SLinus Torvalds 
1568c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
15691da177e4SLinus Torvalds 
15701da177e4SLinus Torvalds 	if (!data)
15711da177e4SLinus Torvalds 		goto out_bad;
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds 	/* Set up the argument struct */
1574f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
1575f453a54aSFred Isaman 	atomic_inc(&cinfo->mds->rpcs_out);
15769f0ec176SAndy Adamson 	return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops,
15779f0ec176SAndy Adamson 				   how, 0);
15781da177e4SLinus Torvalds  out_bad:
1579ea2cf228SFred Isaman 	nfs_retry_commit(head, NULL, cinfo);
1580f453a54aSFred Isaman 	cinfo->completion_ops->error_cleanup(NFS_I(inode));
15811da177e4SLinus Torvalds 	return -ENOMEM;
15821da177e4SLinus Torvalds }
15831da177e4SLinus Torvalds 
15841da177e4SLinus Torvalds /*
15851da177e4SLinus Torvalds  * COMMIT call returned
15861da177e4SLinus Torvalds  */
1587788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
15881da177e4SLinus Torvalds {
15890b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
15901da177e4SLinus Torvalds 
1591a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
15921da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
15931da177e4SLinus Torvalds 
1594788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1595c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
1596c9d8f89dSTrond Myklebust }
1597c9d8f89dSTrond Myklebust 
1598f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1599c9d8f89dSTrond Myklebust {
1600c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1601c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1602f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
1603788e7a89STrond Myklebust 
16041da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
16051da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
16061da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1607d6d6dc7cSFred Isaman 		nfs_clear_page_commit(req->wb_page);
16081da177e4SLinus Torvalds 
160948186c7dSChuck Lever 		dprintk("NFS:       commit (%s/%lld %d@%lld)",
16103d4ff43dSAl Viro 			req->wb_context->dentry->d_sb->s_id,
16113d4ff43dSAl Viro 			(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
16121da177e4SLinus Torvalds 			req->wb_bytes,
16131da177e4SLinus Torvalds 			(long long)req_offset(req));
1614c9d8f89dSTrond Myklebust 		if (status < 0) {
1615c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
16161da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1617c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
16181da177e4SLinus Torvalds 			goto next;
16191da177e4SLinus Torvalds 		}
16201da177e4SLinus Torvalds 
16211da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
16221da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
16232f2c63bcSTrond Myklebust 		if (!memcmp(&req->wb_verf, &data->verf.verifier, sizeof(req->wb_verf))) {
16241da177e4SLinus Torvalds 			/* We have a match */
16251da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
16261da177e4SLinus Torvalds 			dprintk(" OK\n");
16271da177e4SLinus Torvalds 			goto next;
16281da177e4SLinus Torvalds 		}
16291da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
16301da177e4SLinus Torvalds 		dprintk(" mismatch\n");
16316d884e8fSFred 		nfs_mark_request_dirty(req);
163205990d1bSTrond Myklebust 		set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
16331da177e4SLinus Torvalds 	next:
16341d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
16351da177e4SLinus Torvalds 	}
1636f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1637f453a54aSFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
1638f453a54aSFred Isaman 		nfs_commit_clear_lock(NFS_I(data->inode));
16395917ce84SFred Isaman }
16405917ce84SFred Isaman 
16415917ce84SFred Isaman static void nfs_commit_release(void *calldata)
16425917ce84SFred Isaman {
16430b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
16445917ce84SFred Isaman 
1645f453a54aSFred Isaman 	data->completion_ops->completion(data);
1646c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
16471da177e4SLinus Torvalds }
1648788e7a89STrond Myklebust 
1649788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
16500b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1651788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1652788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1653788e7a89STrond Myklebust };
16541da177e4SLinus Torvalds 
1655f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1656f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1657f453a54aSFred Isaman 	.error_cleanup = nfs_commit_clear_lock,
1658f453a54aSFred Isaman };
1659f453a54aSFred Isaman 
16601763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1661ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
166284c53ab5SFred Isaman {
166384c53ab5SFred Isaman 	int status;
166484c53ab5SFred Isaman 
1665ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
166684c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1667ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
166884c53ab5SFred Isaman 	return status;
166984c53ab5SFred Isaman }
167084c53ab5SFred Isaman 
1671b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
16721da177e4SLinus Torvalds {
16731da177e4SLinus Torvalds 	LIST_HEAD(head);
1674ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
167571d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1676b8413f98STrond Myklebust 	int res;
16771da177e4SLinus Torvalds 
1678b8413f98STrond Myklebust 	res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1679b8413f98STrond Myklebust 	if (res <= 0)
1680c5efa5fcSTrond Myklebust 		goto out_mark_dirty;
1681ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1682ea2cf228SFred Isaman 	res = nfs_scan_commit(inode, &head, &cinfo);
16831da177e4SLinus Torvalds 	if (res) {
1684a861a1e1SFred Isaman 		int error;
1685a861a1e1SFred Isaman 
1686ea2cf228SFred Isaman 		error = nfs_generic_commit_list(inode, &head, how, &cinfo);
16871da177e4SLinus Torvalds 		if (error < 0)
16881da177e4SLinus Torvalds 			return error;
1689b8413f98STrond Myklebust 		if (!may_wait)
1690b8413f98STrond Myklebust 			goto out_mark_dirty;
1691b8413f98STrond Myklebust 		error = wait_on_bit(&NFS_I(inode)->flags,
1692b8413f98STrond Myklebust 				NFS_INO_COMMIT,
169371d0a611STrond Myklebust 				nfs_wait_bit_killable,
169471d0a611STrond Myklebust 				TASK_KILLABLE);
1695b8413f98STrond Myklebust 		if (error < 0)
1696b8413f98STrond Myklebust 			return error;
169771d0a611STrond Myklebust 	} else
169871d0a611STrond Myklebust 		nfs_commit_clear_lock(NFS_I(inode));
1699c5efa5fcSTrond Myklebust 	return res;
1700c5efa5fcSTrond Myklebust 	/* Note: If we exit without ensuring that the commit is complete,
1701c5efa5fcSTrond Myklebust 	 * we must mark the inode as dirty. Otherwise, future calls to
1702c5efa5fcSTrond Myklebust 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1703c5efa5fcSTrond Myklebust 	 * that the data is on the disk.
1704c5efa5fcSTrond Myklebust 	 */
1705c5efa5fcSTrond Myklebust out_mark_dirty:
1706c5efa5fcSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
17071da177e4SLinus Torvalds 	return res;
17081da177e4SLinus Torvalds }
17098fc795f7STrond Myklebust 
17108fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
17118fc795f7STrond Myklebust {
1712420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1713420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1714420e3646STrond Myklebust 	int ret = 0;
17158fc795f7STrond Myklebust 
17163236c3e1SJeff Layton 	/* no commits means nothing needs to be done */
1717ea2cf228SFred Isaman 	if (!nfsi->commit_info.ncommit)
17183236c3e1SJeff Layton 		return ret;
17193236c3e1SJeff Layton 
1720a00dd6c0SJeff Layton 	if (wbc->sync_mode == WB_SYNC_NONE) {
1721a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1722a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1723420e3646STrond Myklebust 		 */
1724ea2cf228SFred Isaman 		if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1))
1725420e3646STrond Myklebust 			goto out_mark_dirty;
1726420e3646STrond Myklebust 
1727a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1728420e3646STrond Myklebust 		flags = 0;
1729a00dd6c0SJeff Layton 	}
1730a00dd6c0SJeff Layton 
1731420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1732420e3646STrond Myklebust 	if (ret >= 0) {
1733420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1734420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1735420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1736420e3646STrond Myklebust 			else
1737420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1738420e3646STrond Myklebust 		}
17398fc795f7STrond Myklebust 		return 0;
1740420e3646STrond Myklebust 	}
1741420e3646STrond Myklebust out_mark_dirty:
17428fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
17438fc795f7STrond Myklebust 	return ret;
17448fc795f7STrond Myklebust }
1745c63c7b05STrond Myklebust #else
17468fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
17478fc795f7STrond Myklebust {
17488fc795f7STrond Myklebust 	return 0;
17498fc795f7STrond Myklebust }
17501da177e4SLinus Torvalds #endif
17511da177e4SLinus Torvalds 
17528fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
17538fc795f7STrond Myklebust {
1754a8d8f02cSBryan Schumaker 	return nfs_commit_unstable_pages(inode, wbc);
1755a8d8f02cSBryan Schumaker }
175689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode);
1757863a3c6cSAndy Adamson 
1758acdc53b2STrond Myklebust /*
1759acdc53b2STrond Myklebust  * flush the inode to disk.
1760acdc53b2STrond Myklebust  */
1761acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
176234901f70STrond Myklebust {
176334901f70STrond Myklebust 	struct writeback_control wbc = {
176472cb77f4STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
176534901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
1766d7fb1207STrond Myklebust 		.range_start = 0,
1767d7fb1207STrond Myklebust 		.range_end = LLONG_MAX,
176834901f70STrond Myklebust 	};
1769f4ce1299STrond Myklebust 	int ret;
177034901f70STrond Myklebust 
1771f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_enter(inode);
1772f4ce1299STrond Myklebust 
1773f4ce1299STrond Myklebust 	ret = sync_inode(inode, &wbc);
1774f4ce1299STrond Myklebust 
1775f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_exit(inode, ret);
1776f4ce1299STrond Myklebust 	return ret;
17771c75950bSTrond Myklebust }
1778ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all);
17791c75950bSTrond Myklebust 
17801b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
17811b3b4a1aSTrond Myklebust {
17821b3b4a1aSTrond Myklebust 	struct nfs_page *req;
17831b3b4a1aSTrond Myklebust 	int ret = 0;
17841b3b4a1aSTrond Myklebust 
17851b3b4a1aSTrond Myklebust 	for (;;) {
1786ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
17871b3b4a1aSTrond Myklebust 		req = nfs_page_find_request(page);
17881b3b4a1aSTrond Myklebust 		if (req == NULL)
17891b3b4a1aSTrond Myklebust 			break;
17907ad84aa9STrond Myklebust 		if (nfs_lock_request(req)) {
17918dd37758STrond Myklebust 			nfs_clear_request_commit(req);
17921b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
17931b3b4a1aSTrond Myklebust 			/*
17941b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
17951b3b4a1aSTrond Myklebust 			 * page as being dirty
17961b3b4a1aSTrond Myklebust 			 */
17971b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
17981d1afcbcSTrond Myklebust 			nfs_unlock_and_release_request(req);
17991b3b4a1aSTrond Myklebust 			break;
18001b3b4a1aSTrond Myklebust 		}
18011b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
1802c9edda71STrond Myklebust 		nfs_release_request(req);
18031b3b4a1aSTrond Myklebust 		if (ret < 0)
1804c988950eSTrond Myklebust 			break;
18051b3b4a1aSTrond Myklebust 	}
18061b3b4a1aSTrond Myklebust 	return ret;
18071b3b4a1aSTrond Myklebust }
18081b3b4a1aSTrond Myklebust 
18091c75950bSTrond Myklebust /*
18101c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
18111c75950bSTrond Myklebust  */
18121c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
18131c75950bSTrond Myklebust {
181429418aa4SMel Gorman 	loff_t range_start = page_file_offset(page);
18157f2f12d9STrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
18167f2f12d9STrond Myklebust 	struct writeback_control wbc = {
18177f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
18187f2f12d9STrond Myklebust 		.nr_to_write = 0,
18197f2f12d9STrond Myklebust 		.range_start = range_start,
18207f2f12d9STrond Myklebust 		.range_end = range_end,
18217f2f12d9STrond Myklebust 	};
18227f2f12d9STrond Myklebust 	int ret;
18237f2f12d9STrond Myklebust 
1824f4ce1299STrond Myklebust 	trace_nfs_writeback_page_enter(inode);
1825f4ce1299STrond Myklebust 
18260522f6adSTrond Myklebust 	for (;;) {
1827ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
18287f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
18297f2f12d9STrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
18307f2f12d9STrond Myklebust 			if (ret < 0)
18317f2f12d9STrond Myklebust 				goto out_error;
18320522f6adSTrond Myklebust 			continue;
18337f2f12d9STrond Myklebust 		}
1834f4ce1299STrond Myklebust 		ret = 0;
18350522f6adSTrond Myklebust 		if (!PagePrivate(page))
18360522f6adSTrond Myklebust 			break;
18370522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
18387f2f12d9STrond Myklebust 		if (ret < 0)
18397f2f12d9STrond Myklebust 			goto out_error;
18407f2f12d9STrond Myklebust 	}
18417f2f12d9STrond Myklebust out_error:
1842f4ce1299STrond Myklebust 	trace_nfs_writeback_page_exit(inode, ret);
18437f2f12d9STrond Myklebust 	return ret;
18441c75950bSTrond Myklebust }
18451c75950bSTrond Myklebust 
1846074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
1847074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1848a6bc32b8SMel Gorman 		struct page *page, enum migrate_mode mode)
1849074cc1deSTrond Myklebust {
18502da95652SJeff Layton 	/*
18512da95652SJeff Layton 	 * If PagePrivate is set, then the page is currently associated with
18522da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
18532da95652SJeff Layton 	 *
18542da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
18552da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
18562da95652SJeff Layton 	 *        the page lock.
18572da95652SJeff Layton 	 */
18582da95652SJeff Layton 	if (PagePrivate(page))
18592da95652SJeff Layton 		return -EBUSY;
1860074cc1deSTrond Myklebust 
18618c209ce7SDavid Howells 	if (!nfs_fscache_release_page(page, GFP_KERNEL))
18628c209ce7SDavid Howells 		return -EBUSY;
1863074cc1deSTrond Myklebust 
1864a6bc32b8SMel Gorman 	return migrate_page(mapping, newpage, page, mode);
1865074cc1deSTrond Myklebust }
1866074cc1deSTrond Myklebust #endif
1867074cc1deSTrond Myklebust 
1868f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
18691da177e4SLinus Torvalds {
18701da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1871cd841605SFred Isaman 					     sizeof(struct nfs_write_header),
18721da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
187320c2df83SPaul Mundt 					     NULL);
18741da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
18751da177e4SLinus Torvalds 		return -ENOMEM;
18761da177e4SLinus Torvalds 
187793d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
18781da177e4SLinus Torvalds 						     nfs_wdata_cachep);
18791da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
18803dd4765fSJeff Layton 		goto out_destroy_write_cache;
18811da177e4SLinus Torvalds 
18820b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
18830b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
18840b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
18850b7c0153SFred Isaman 					     NULL);
18860b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
18873dd4765fSJeff Layton 		goto out_destroy_write_mempool;
18880b7c0153SFred Isaman 
188993d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
18904c100210SYanchuan Nian 						      nfs_cdata_cachep);
18911da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
18923dd4765fSJeff Layton 		goto out_destroy_commit_cache;
18931da177e4SLinus Torvalds 
189489a09141SPeter Zijlstra 	/*
189589a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
189689a09141SPeter Zijlstra 	 *
189789a09141SPeter Zijlstra 	 *  64MB:    8192k
189889a09141SPeter Zijlstra 	 * 128MB:   11585k
189989a09141SPeter Zijlstra 	 * 256MB:   16384k
190089a09141SPeter Zijlstra 	 * 512MB:   23170k
190189a09141SPeter Zijlstra 	 *   1GB:   32768k
190289a09141SPeter Zijlstra 	 *   2GB:   46340k
190389a09141SPeter Zijlstra 	 *   4GB:   65536k
190489a09141SPeter Zijlstra 	 *   8GB:   92681k
190589a09141SPeter Zijlstra 	 *  16GB:  131072k
190689a09141SPeter Zijlstra 	 *
190789a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
190889a09141SPeter Zijlstra 	 * Limit the default to 256M
190989a09141SPeter Zijlstra 	 */
191089a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
191189a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
191289a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
191389a09141SPeter Zijlstra 
19141da177e4SLinus Torvalds 	return 0;
19153dd4765fSJeff Layton 
19163dd4765fSJeff Layton out_destroy_commit_cache:
19173dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
19183dd4765fSJeff Layton out_destroy_write_mempool:
19193dd4765fSJeff Layton 	mempool_destroy(nfs_wdata_mempool);
19203dd4765fSJeff Layton out_destroy_write_cache:
19213dd4765fSJeff Layton 	kmem_cache_destroy(nfs_wdata_cachep);
19223dd4765fSJeff Layton 	return -ENOMEM;
19231da177e4SLinus Torvalds }
19241da177e4SLinus Torvalds 
1925266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
19261da177e4SLinus Torvalds {
19271da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
19283dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
19291da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
19301a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
19311da177e4SLinus Torvalds }
19321da177e4SLinus Torvalds 
1933