xref: /linux/fs/nfs/write.c (revision 3c6b899c49e5e9c2803b59ee553eddaf69cea7f6)
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 
357a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc),
358a20c93e3SChristoph Hellwig 				false, &nfs_async_write_completion_ops);
359f758c885STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
360f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
361f758c885STrond Myklebust 	if (err < 0)
3624d770ccfSTrond Myklebust 		return err;
363f758c885STrond Myklebust 	if (pgio.pg_error < 0)
364f758c885STrond Myklebust 		return pgio.pg_error;
365f758c885STrond Myklebust 	return 0;
3664d770ccfSTrond Myklebust }
3674d770ccfSTrond Myklebust 
3684d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
3694d770ccfSTrond Myklebust {
370f758c885STrond Myklebust 	int ret;
3714d770ccfSTrond Myklebust 
372f758c885STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
3731da177e4SLinus Torvalds 	unlock_page(page);
374f758c885STrond Myklebust 	return ret;
375f758c885STrond Myklebust }
376f758c885STrond Myklebust 
377f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
378f758c885STrond Myklebust {
379f758c885STrond Myklebust 	int ret;
380f758c885STrond Myklebust 
381f758c885STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
382f758c885STrond Myklebust 	unlock_page(page);
383f758c885STrond Myklebust 	return ret;
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3871da177e4SLinus Torvalds {
3881da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
38972cb77f4STrond Myklebust 	unsigned long *bitlock = &NFS_I(inode)->flags;
390c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
3911da177e4SLinus Torvalds 	int err;
3921da177e4SLinus Torvalds 
39372cb77f4STrond Myklebust 	/* Stop dirtying of new pages while we sync */
39472cb77f4STrond Myklebust 	err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
39572cb77f4STrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
39672cb77f4STrond Myklebust 	if (err)
39772cb77f4STrond Myklebust 		goto out_err;
39872cb77f4STrond Myklebust 
39991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
40091d5b470SChuck Lever 
401a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false,
402a20c93e3SChristoph Hellwig 				&nfs_async_write_completion_ops);
403f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
404c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
40572cb77f4STrond Myklebust 
40672cb77f4STrond Myklebust 	clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
40772cb77f4STrond Myklebust 	smp_mb__after_clear_bit();
40872cb77f4STrond Myklebust 	wake_up_bit(bitlock, NFS_INO_FLUSHING);
40972cb77f4STrond Myklebust 
410f758c885STrond Myklebust 	if (err < 0)
41172cb77f4STrond Myklebust 		goto out_err;
41272cb77f4STrond Myklebust 	err = pgio.pg_error;
41372cb77f4STrond Myklebust 	if (err < 0)
41472cb77f4STrond Myklebust 		goto out_err;
415c63c7b05STrond Myklebust 	return 0;
41672cb77f4STrond Myklebust out_err:
41772cb77f4STrond Myklebust 	return err;
4181da177e4SLinus Torvalds }
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds /*
4211da177e4SLinus Torvalds  * Insert a write request into an inode
4221da177e4SLinus Torvalds  */
423d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
4241da177e4SLinus Torvalds {
4251da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
426e7d39069STrond Myklebust 
427e7d39069STrond Myklebust 	/* Lock the request! */
4287ad84aa9STrond Myklebust 	nfs_lock_request(req);
429e7d39069STrond Myklebust 
430e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
431011e2a7fSBryan Schumaker 	if (!nfsi->npages && NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
432a9a4a87aSTrond Myklebust 		inode->i_version++;
43329418aa4SMel Gorman 	/*
43429418aa4SMel Gorman 	 * Swap-space should not get truncated. Hence no need to plug the race
43529418aa4SMel Gorman 	 * with invalidate/truncate.
43629418aa4SMel Gorman 	 */
43729418aa4SMel Gorman 	if (likely(!PageSwapCache(req->wb_page))) {
4382df485a7STrond Myklebust 		set_bit(PG_MAPPED, &req->wb_flags);
439deb7d638STrond Myklebust 		SetPagePrivate(req->wb_page);
440277459d2STrond Myklebust 		set_page_private(req->wb_page, (unsigned long)req);
44129418aa4SMel Gorman 	}
4421da177e4SLinus Torvalds 	nfsi->npages++;
443c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
444e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds /*
44889a09141SPeter Zijlstra  * Remove a write request from an inode
4491da177e4SLinus Torvalds  */
4501da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
4511da177e4SLinus Torvalds {
4523d4ff43dSAl Viro 	struct inode *inode = req->wb_context->dentry->d_inode;
4531da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4541da177e4SLinus Torvalds 
455587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
45629418aa4SMel Gorman 	if (likely(!PageSwapCache(req->wb_page))) {
457277459d2STrond Myklebust 		set_page_private(req->wb_page, 0);
458deb7d638STrond Myklebust 		ClearPagePrivate(req->wb_page);
4592df485a7STrond Myklebust 		clear_bit(PG_MAPPED, &req->wb_flags);
46029418aa4SMel Gorman 	}
4611da177e4SLinus Torvalds 	nfsi->npages--;
462587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
4631da177e4SLinus Torvalds 	nfs_release_request(req);
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds 
46661822ab5STrond Myklebust static void
4676d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
46861822ab5STrond Myklebust {
46961822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
47061822ab5STrond Myklebust }
47161822ab5STrond Myklebust 
47289d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
4738dd37758STrond Myklebust /**
4748dd37758STrond Myklebust  * nfs_request_add_commit_list - add request to a commit list
4758dd37758STrond Myklebust  * @req: pointer to a struct nfs_page
476ea2cf228SFred Isaman  * @dst: commit list head
477ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
4788dd37758STrond Myklebust  *
479ea2cf228SFred Isaman  * This sets the PG_CLEAN bit, updates the cinfo count of
4808dd37758STrond Myklebust  * number of outstanding requests requiring a commit as well as
4818dd37758STrond Myklebust  * the MM page stats.
4828dd37758STrond Myklebust  *
483ea2cf228SFred Isaman  * The caller must _not_ hold the cinfo->lock, but must be
4848dd37758STrond Myklebust  * holding the nfs_page lock.
4858dd37758STrond Myklebust  */
4868dd37758STrond Myklebust void
487ea2cf228SFred Isaman nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst,
488ea2cf228SFred Isaman 			    struct nfs_commit_info *cinfo)
4898dd37758STrond Myklebust {
4908dd37758STrond Myklebust 	set_bit(PG_CLEAN, &(req)->wb_flags);
491ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
492ea2cf228SFred Isaman 	nfs_list_add_request(req, dst);
493ea2cf228SFred Isaman 	cinfo->mds->ncommit++;
494ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
49556f9cd68SFred Isaman 	if (!cinfo->dreq) {
4968dd37758STrond Myklebust 		inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
497d56b4ddfSMel Gorman 		inc_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
49856f9cd68SFred Isaman 			     BDI_RECLAIMABLE);
49956f9cd68SFred Isaman 		__mark_inode_dirty(req->wb_context->dentry->d_inode,
50056f9cd68SFred Isaman 				   I_DIRTY_DATASYNC);
50156f9cd68SFred Isaman 	}
5028dd37758STrond Myklebust }
5038dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
5048dd37758STrond Myklebust 
5058dd37758STrond Myklebust /**
5068dd37758STrond Myklebust  * nfs_request_remove_commit_list - Remove request from a commit list
5078dd37758STrond Myklebust  * @req: pointer to a nfs_page
508ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
5098dd37758STrond Myklebust  *
510ea2cf228SFred Isaman  * This clears the PG_CLEAN bit, and updates the cinfo's count of
5118dd37758STrond Myklebust  * number of outstanding requests requiring a commit
5128dd37758STrond Myklebust  * It does not update the MM page stats.
5138dd37758STrond Myklebust  *
514ea2cf228SFred Isaman  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
5158dd37758STrond Myklebust  */
5168dd37758STrond Myklebust void
517ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req,
518ea2cf228SFred Isaman 			       struct nfs_commit_info *cinfo)
5198dd37758STrond Myklebust {
5208dd37758STrond Myklebust 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
5218dd37758STrond Myklebust 		return;
5228dd37758STrond Myklebust 	nfs_list_remove_request(req);
523ea2cf228SFred Isaman 	cinfo->mds->ncommit--;
5248dd37758STrond Myklebust }
5258dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
5268dd37758STrond Myklebust 
527ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
528ea2cf228SFred Isaman 				      struct inode *inode)
529ea2cf228SFred Isaman {
530ea2cf228SFred Isaman 	cinfo->lock = &inode->i_lock;
531ea2cf228SFred Isaman 	cinfo->mds = &NFS_I(inode)->commit_info;
532ea2cf228SFred Isaman 	cinfo->ds = pnfs_get_ds_info(inode);
533b359f9d0SFred Isaman 	cinfo->dreq = NULL;
534f453a54aSFred Isaman 	cinfo->completion_ops = &nfs_commit_completion_ops;
535ea2cf228SFred Isaman }
536ea2cf228SFred Isaman 
537ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
538ea2cf228SFred Isaman 		    struct inode *inode,
539ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq)
540ea2cf228SFred Isaman {
5411763da12SFred Isaman 	if (dreq)
5421763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
5431763da12SFred Isaman 	else
544ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(cinfo, inode);
545ea2cf228SFred Isaman }
546ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo);
5478dd37758STrond Myklebust 
5481da177e4SLinus Torvalds /*
5491da177e4SLinus Torvalds  * Add a request to the inode's commit list.
5501da177e4SLinus Torvalds  */
5511763da12SFred Isaman void
552ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
553ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
5541da177e4SLinus Torvalds {
555ea2cf228SFred Isaman 	if (pnfs_mark_request_commit(req, lseg, cinfo))
5568dd37758STrond Myklebust 		return;
557ea2cf228SFred Isaman 	nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo);
5581da177e4SLinus Torvalds }
5598e821cadSTrond Myklebust 
560d6d6dc7cSFred Isaman static void
561d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page)
562e468bae9STrond Myklebust {
563e468bae9STrond Myklebust 	dec_zone_page_state(page, NR_UNSTABLE_NFS);
564d56b4ddfSMel Gorman 	dec_bdi_stat(page_file_mapping(page)->backing_dev_info, BDI_RECLAIMABLE);
565e468bae9STrond Myklebust }
566d6d6dc7cSFred Isaman 
5678dd37758STrond Myklebust static void
568d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req)
569d6d6dc7cSFred Isaman {
5708dd37758STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
5718dd37758STrond Myklebust 		struct inode *inode = req->wb_context->dentry->d_inode;
572ea2cf228SFred Isaman 		struct nfs_commit_info cinfo;
573d6d6dc7cSFred Isaman 
574ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(&cinfo, inode);
575ea2cf228SFred Isaman 		if (!pnfs_clear_request_commit(req, &cinfo)) {
576ea2cf228SFred Isaman 			spin_lock(cinfo.lock);
577ea2cf228SFred Isaman 			nfs_request_remove_commit_list(req, &cinfo);
578ea2cf228SFred Isaman 			spin_unlock(cinfo.lock);
579d6d6dc7cSFred Isaman 		}
5808dd37758STrond Myklebust 		nfs_clear_page_commit(req->wb_page);
5818dd37758STrond Myklebust 	}
582e468bae9STrond Myklebust }
583e468bae9STrond Myklebust 
5848e821cadSTrond Myklebust static inline
5858e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
5868e821cadSTrond Myklebust {
587465d5243SFred Isaman 	if (data->verf.committed == NFS_DATA_SYNC)
588cd841605SFred Isaman 		return data->header->lseg == NULL;
5898e821cadSTrond Myklebust 	return data->verf.committed != NFS_FILE_SYNC;
5908e821cadSTrond Myklebust }
5918e821cadSTrond Myklebust 
5928e821cadSTrond Myklebust #else
59368cd6fa4SBryan Schumaker static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
59468cd6fa4SBryan Schumaker 				      struct inode *inode)
59568cd6fa4SBryan Schumaker {
59668cd6fa4SBryan Schumaker }
59768cd6fa4SBryan Schumaker 
59868cd6fa4SBryan Schumaker void nfs_init_cinfo(struct nfs_commit_info *cinfo,
59968cd6fa4SBryan Schumaker 		    struct inode *inode,
60068cd6fa4SBryan Schumaker 		    struct nfs_direct_req *dreq)
60168cd6fa4SBryan Schumaker {
60268cd6fa4SBryan Schumaker }
60368cd6fa4SBryan Schumaker 
6041763da12SFred Isaman void
605ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
606ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
6078e821cadSTrond Myklebust {
6088e821cadSTrond Myklebust }
6098e821cadSTrond Myklebust 
6108dd37758STrond Myklebust static void
611e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
612e468bae9STrond Myklebust {
613e468bae9STrond Myklebust }
614e468bae9STrond Myklebust 
6158e821cadSTrond Myklebust static inline
6168e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
6178e821cadSTrond Myklebust {
6188e821cadSTrond Myklebust 	return 0;
6198e821cadSTrond Myklebust }
6208e821cadSTrond Myklebust 
6211da177e4SLinus Torvalds #endif
6221da177e4SLinus Torvalds 
623061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
6246c75dc0dSFred Isaman {
625ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
6266c75dc0dSFred Isaman 	unsigned long bytes = 0;
6276c75dc0dSFred Isaman 
6286c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
6296c75dc0dSFred Isaman 		goto out;
630ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
6316c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
6326c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
6336c75dc0dSFred Isaman 
6346c75dc0dSFred Isaman 		bytes += req->wb_bytes;
6356c75dc0dSFred Isaman 		nfs_list_remove_request(req);
6366c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
6376c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
638d1182b33STrond Myklebust 			nfs_set_pageerror(req->wb_page);
6396c75dc0dSFred Isaman 			nfs_context_set_write_error(req->wb_context, hdr->error);
6406c75dc0dSFred Isaman 			goto remove_req;
6416c75dc0dSFred Isaman 		}
6426c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
6436c75dc0dSFred Isaman 			nfs_mark_request_dirty(req);
6446c75dc0dSFred Isaman 			goto next;
6456c75dc0dSFred Isaman 		}
6466c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
6472f2c63bcSTrond Myklebust 			memcpy(&req->wb_verf, &hdr->verf->verifier, sizeof(req->wb_verf));
648ea2cf228SFred Isaman 			nfs_mark_request_commit(req, hdr->lseg, &cinfo);
6496c75dc0dSFred Isaman 			goto next;
6506c75dc0dSFred Isaman 		}
6516c75dc0dSFred Isaman remove_req:
6526c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
6536c75dc0dSFred Isaman next:
6541d1afcbcSTrond Myklebust 		nfs_unlock_request(req);
655d1182b33STrond Myklebust 		nfs_end_page_writeback(req->wb_page);
6563aff4ebbSTrond Myklebust 		nfs_release_request(req);
6576c75dc0dSFred Isaman 	}
6586c75dc0dSFred Isaman out:
6596c75dc0dSFred Isaman 	hdr->release(hdr);
6606c75dc0dSFred Isaman }
6616c75dc0dSFred Isaman 
66289d77c8fSBryan Schumaker #if  IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
663ea2cf228SFred Isaman static unsigned long
664ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
665fb8a1f11STrond Myklebust {
666ea2cf228SFred Isaman 	return cinfo->mds->ncommit;
667fb8a1f11STrond Myklebust }
668fb8a1f11STrond Myklebust 
669ea2cf228SFred Isaman /* cinfo->lock held by caller */
6701763da12SFred Isaman int
671ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
672ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
673d6d6dc7cSFred Isaman {
674d6d6dc7cSFred Isaman 	struct nfs_page *req, *tmp;
675d6d6dc7cSFred Isaman 	int ret = 0;
676d6d6dc7cSFred Isaman 
677d6d6dc7cSFred Isaman 	list_for_each_entry_safe(req, tmp, src, wb_list) {
6788dd37758STrond Myklebust 		if (!nfs_lock_request(req))
6798dd37758STrond Myklebust 			continue;
6807ad84aa9STrond Myklebust 		kref_get(&req->wb_kref);
681ea2cf228SFred Isaman 		if (cond_resched_lock(cinfo->lock))
6823b3be88dSTrond Myklebust 			list_safe_reset_next(req, tmp, wb_list);
683ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
6848dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
685d6d6dc7cSFred Isaman 		ret++;
6861763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
687d6d6dc7cSFred Isaman 			break;
688d6d6dc7cSFred Isaman 	}
689d6d6dc7cSFred Isaman 	return ret;
690d6d6dc7cSFred Isaman }
691d6d6dc7cSFred Isaman 
6921da177e4SLinus Torvalds /*
6931da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
6941da177e4SLinus Torvalds  * @inode: NFS inode to scan
695ea2cf228SFred Isaman  * @dst: mds destination list
696ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
6971da177e4SLinus Torvalds  *
6981da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
6991da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
7001da177e4SLinus Torvalds  */
7011763da12SFred Isaman int
702ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
703ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
7041da177e4SLinus Torvalds {
705d6d6dc7cSFred Isaman 	int ret = 0;
706fb8a1f11STrond Myklebust 
707ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
708ea2cf228SFred Isaman 	if (cinfo->mds->ncommit > 0) {
7098dd37758STrond Myklebust 		const int max = INT_MAX;
710d6d6dc7cSFred Isaman 
711ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
712ea2cf228SFred Isaman 					   cinfo, max);
713ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
714d6d6dc7cSFred Isaman 	}
715ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
716ff778d02STrond Myklebust 	return ret;
7171da177e4SLinus Torvalds }
718d6d6dc7cSFred Isaman 
719c42de9ddSTrond Myklebust #else
720ea2cf228SFred Isaman static unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
721fb8a1f11STrond Myklebust {
722fb8a1f11STrond Myklebust 	return 0;
723fb8a1f11STrond Myklebust }
724fb8a1f11STrond Myklebust 
7251763da12SFred Isaman int nfs_scan_commit(struct inode *inode, struct list_head *dst,
726ea2cf228SFred Isaman 		    struct nfs_commit_info *cinfo)
727c42de9ddSTrond Myklebust {
728c42de9ddSTrond Myklebust 	return 0;
729c42de9ddSTrond Myklebust }
7301da177e4SLinus Torvalds #endif
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds /*
733e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
734e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
7351da177e4SLinus Torvalds  *
736e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
737e7d39069STrond Myklebust  * to disk.
7381da177e4SLinus Torvalds  */
739e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
740e7d39069STrond Myklebust 		struct page *page,
741e7d39069STrond Myklebust 		unsigned int offset,
742e7d39069STrond Myklebust 		unsigned int bytes)
7431da177e4SLinus Torvalds {
744e7d39069STrond Myklebust 	struct nfs_page *req;
745e7d39069STrond Myklebust 	unsigned int rqend;
746e7d39069STrond Myklebust 	unsigned int end;
7471da177e4SLinus Torvalds 	int error;
748277459d2STrond Myklebust 
749e7d39069STrond Myklebust 	if (!PagePrivate(page))
750e7d39069STrond Myklebust 		return NULL;
751e7d39069STrond Myklebust 
752e7d39069STrond Myklebust 	end = offset + bytes;
753e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
754e7d39069STrond Myklebust 
755e7d39069STrond Myklebust 	for (;;) {
75629418aa4SMel Gorman 		req = nfs_page_find_request_locked(NFS_I(inode), page);
757e7d39069STrond Myklebust 		if (req == NULL)
758e7d39069STrond Myklebust 			goto out_unlock;
759e7d39069STrond Myklebust 
760e7d39069STrond Myklebust 		rqend = req->wb_offset + req->wb_bytes;
761e7d39069STrond Myklebust 		/*
762e7d39069STrond Myklebust 		 * Tell the caller to flush out the request if
763e7d39069STrond Myklebust 		 * the offsets are non-contiguous.
764e7d39069STrond Myklebust 		 * Note: nfs_flush_incompatible() will already
765e7d39069STrond Myklebust 		 * have flushed out requests having wrong owners.
766e7d39069STrond Myklebust 		 */
767e468bae9STrond Myklebust 		if (offset > rqend
768e7d39069STrond Myklebust 		    || end < req->wb_offset)
769e7d39069STrond Myklebust 			goto out_flushme;
770e7d39069STrond Myklebust 
7717ad84aa9STrond Myklebust 		if (nfs_lock_request(req))
772e7d39069STrond Myklebust 			break;
773e7d39069STrond Myklebust 
774e7d39069STrond Myklebust 		/* The request is locked, so wait and then retry */
775587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
7761da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
7771da177e4SLinus Torvalds 		nfs_release_request(req);
778e7d39069STrond Myklebust 		if (error != 0)
779e7d39069STrond Myklebust 			goto out_err;
780e7d39069STrond Myklebust 		spin_lock(&inode->i_lock);
7811da177e4SLinus Torvalds 	}
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
7841da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
7851da177e4SLinus Torvalds 		req->wb_offset = offset;
7861da177e4SLinus Torvalds 		req->wb_pgbase = offset;
7871da177e4SLinus Torvalds 	}
7881da177e4SLinus Torvalds 	if (end > rqend)
7891da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
790e7d39069STrond Myklebust 	else
791e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
792e7d39069STrond Myklebust out_unlock:
793e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
794ca138f36SFred Isaman 	if (req)
7958dd37758STrond Myklebust 		nfs_clear_request_commit(req);
796e7d39069STrond Myklebust 	return req;
797e7d39069STrond Myklebust out_flushme:
798e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
799e7d39069STrond Myklebust 	nfs_release_request(req);
800e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
801e7d39069STrond Myklebust out_err:
802e7d39069STrond Myklebust 	return ERR_PTR(error);
803e7d39069STrond Myklebust }
8041da177e4SLinus Torvalds 
805e7d39069STrond Myklebust /*
806e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
807e7d39069STrond Myklebust  *
808e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
809e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
810e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
811e7d39069STrond Myklebust  */
812e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
813e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
814e7d39069STrond Myklebust {
815d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
816e7d39069STrond Myklebust 	struct nfs_page	*req;
817e7d39069STrond Myklebust 
818e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
819e7d39069STrond Myklebust 	if (req != NULL)
820e7d39069STrond Myklebust 		goto out;
821e7d39069STrond Myklebust 	req = nfs_create_request(ctx, inode, page, offset, bytes);
822e7d39069STrond Myklebust 	if (IS_ERR(req))
823e7d39069STrond Myklebust 		goto out;
824d6d6dc7cSFred Isaman 	nfs_inode_add_request(inode, req);
825efc91ed0STrond Myklebust out:
82661e930a9STrond Myklebust 	return req;
8271da177e4SLinus Torvalds }
8281da177e4SLinus Torvalds 
829e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
830e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
831e7d39069STrond Myklebust {
832e7d39069STrond Myklebust 	struct nfs_page	*req;
833e7d39069STrond Myklebust 
834e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
835e7d39069STrond Myklebust 	if (IS_ERR(req))
836e7d39069STrond Myklebust 		return PTR_ERR(req);
837e7d39069STrond Myklebust 	/* Update file length */
838e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
839e7d39069STrond Myklebust 	nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
840a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
8411d1afcbcSTrond Myklebust 	nfs_unlock_and_release_request(req);
842e7d39069STrond Myklebust 	return 0;
843e7d39069STrond Myklebust }
844e7d39069STrond Myklebust 
8451da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
8461da177e4SLinus Torvalds {
847cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
8482a369153STrond Myklebust 	struct nfs_lock_context *l_ctx;
8491da177e4SLinus Torvalds 	struct nfs_page	*req;
8501a54533eSTrond Myklebust 	int do_flush, status;
8511da177e4SLinus Torvalds 	/*
8521da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
8531da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
8541da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
8551da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
8561da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
8571da177e4SLinus Torvalds 	 * dropped page.
8581da177e4SLinus Torvalds 	 */
8591a54533eSTrond Myklebust 	do {
860277459d2STrond Myklebust 		req = nfs_page_find_request(page);
8611a54533eSTrond Myklebust 		if (req == NULL)
8621a54533eSTrond Myklebust 			return 0;
8632a369153STrond Myklebust 		l_ctx = req->wb_lock_context;
8642a369153STrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx;
8650f1d2605STrond Myklebust 		if (l_ctx && ctx->dentry->d_inode->i_flock != NULL) {
8662a369153STrond Myklebust 			do_flush |= l_ctx->lockowner.l_owner != current->files
8672a369153STrond Myklebust 				|| l_ctx->lockowner.l_pid != current->tgid;
8682a369153STrond Myklebust 		}
8691da177e4SLinus Torvalds 		nfs_release_request(req);
8701a54533eSTrond Myklebust 		if (!do_flush)
8711a54533eSTrond Myklebust 			return 0;
872d56b4ddfSMel Gorman 		status = nfs_wb_page(page_file_mapping(page)->host, page);
8731a54533eSTrond Myklebust 	} while (status == 0);
8741a54533eSTrond Myklebust 	return status;
8751da177e4SLinus Torvalds }
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds /*
878dc24826bSAndy Adamson  * Avoid buffered writes when a open context credential's key would
879dc24826bSAndy Adamson  * expire soon.
880dc24826bSAndy Adamson  *
881dc24826bSAndy Adamson  * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
882dc24826bSAndy Adamson  *
883dc24826bSAndy Adamson  * Return 0 and set a credential flag which triggers the inode to flush
884dc24826bSAndy Adamson  * and performs  NFS_FILE_SYNC writes if the key will expired within
885dc24826bSAndy Adamson  * RPC_KEY_EXPIRE_TIMEO.
886dc24826bSAndy Adamson  */
887dc24826bSAndy Adamson int
888dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode)
889dc24826bSAndy Adamson {
890dc24826bSAndy Adamson 	struct nfs_open_context *ctx = nfs_file_open_context(filp);
891dc24826bSAndy Adamson 	struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
892dc24826bSAndy Adamson 
893dc24826bSAndy Adamson 	return rpcauth_key_timeout_notify(auth, ctx->cred);
894dc24826bSAndy Adamson }
895dc24826bSAndy Adamson 
896dc24826bSAndy Adamson /*
897dc24826bSAndy Adamson  * Test if the open context credential key is marked to expire soon.
898dc24826bSAndy Adamson  */
899dc24826bSAndy Adamson bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx)
900dc24826bSAndy Adamson {
901dc24826bSAndy Adamson 	return rpcauth_cred_key_to_expire(ctx->cred);
902dc24826bSAndy Adamson }
903dc24826bSAndy Adamson 
904dc24826bSAndy Adamson /*
9055d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
9065d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
9075d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
9085d47a356STrond Myklebust  */
9098d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
9105d47a356STrond Myklebust {
911d529ef83SJeff Layton 	struct nfs_inode *nfsi = NFS_I(inode);
912d529ef83SJeff Layton 
9138d197a56STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
9148d197a56STrond Myklebust 		goto out;
915d529ef83SJeff Layton 	if (nfsi->cache_validity & (NFS_INO_INVALID_DATA|NFS_INO_REVAL_PAGECACHE))
916d529ef83SJeff Layton 		return false;
9174db72b40SJeff Layton 	smp_rmb();
918d529ef83SJeff Layton 	if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags))
9198d197a56STrond Myklebust 		return false;
9208d197a56STrond Myklebust out:
9218d197a56STrond Myklebust 	return PageUptodate(page) != 0;
9225d47a356STrond Myklebust }
9235d47a356STrond Myklebust 
924c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or
925c7559663SScott Mayhew  * if we have the whole file locked for writing), it may be more efficient to
926c7559663SScott Mayhew  * extend the write to cover the entire page in order to avoid fragmentation
927c7559663SScott Mayhew  * inefficiencies.
928c7559663SScott Mayhew  *
929263b4509SScott Mayhew  * If the file is opened for synchronous writes then we can just skip the rest
930263b4509SScott Mayhew  * of the checks.
931c7559663SScott Mayhew  */
932c7559663SScott Mayhew static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode)
933c7559663SScott Mayhew {
934c7559663SScott Mayhew 	if (file->f_flags & O_DSYNC)
935c7559663SScott Mayhew 		return 0;
936263b4509SScott Mayhew 	if (!nfs_write_pageuptodate(page, inode))
937263b4509SScott Mayhew 		return 0;
938c7559663SScott Mayhew 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
939c7559663SScott Mayhew 		return 1;
940263b4509SScott Mayhew 	if (inode->i_flock == NULL || (inode->i_flock->fl_start == 0 &&
941c7559663SScott Mayhew 			inode->i_flock->fl_end == OFFSET_MAX &&
942263b4509SScott Mayhew 			inode->i_flock->fl_type != F_RDLCK))
943c7559663SScott Mayhew 		return 1;
944c7559663SScott Mayhew 	return 0;
945c7559663SScott Mayhew }
946c7559663SScott Mayhew 
9475d47a356STrond Myklebust /*
9481da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
9491da177e4SLinus Torvalds  *
9501da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
9511da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
9521da177e4SLinus Torvalds  */
9531da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
9541da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
9551da177e4SLinus Torvalds {
956cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
957d56b4ddfSMel Gorman 	struct inode	*inode = page_file_mapping(page)->host;
9581da177e4SLinus Torvalds 	int		status = 0;
9591da177e4SLinus Torvalds 
96091d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
96191d5b470SChuck Lever 
9626de1472fSAl Viro 	dprintk("NFS:       nfs_updatepage(%pD2 %d@%lld)\n",
9636de1472fSAl Viro 		file, count, (long long)(page_file_offset(page) + offset));
9641da177e4SLinus Torvalds 
965c7559663SScott Mayhew 	if (nfs_can_extend_write(file, page, inode)) {
96649a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
9671da177e4SLinus Torvalds 		offset = 0;
9681da177e4SLinus Torvalds 	}
9691da177e4SLinus Torvalds 
970e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
97103fa9e84STrond Myklebust 	if (status < 0)
97203fa9e84STrond Myklebust 		nfs_set_pageerror(page);
97359b7c05fSTrond Myklebust 	else
97459b7c05fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
9751da177e4SLinus Torvalds 
97648186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
9771da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
9781da177e4SLinus Torvalds 	return status;
9791da177e4SLinus Torvalds }
9801da177e4SLinus Torvalds 
9813ff7576dSTrond Myklebust static int flush_task_priority(int how)
9821da177e4SLinus Torvalds {
9831da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
9841da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
9851da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
9861da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
9871da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
9881da177e4SLinus Torvalds 	}
9891da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
9901da177e4SLinus Torvalds }
9911da177e4SLinus Torvalds 
992c5996c4eSFred Isaman int nfs_initiate_write(struct rpc_clnt *clnt,
993c5996c4eSFred Isaman 		       struct nfs_write_data *data,
994788e7a89STrond Myklebust 		       const struct rpc_call_ops *call_ops,
9959f0ec176SAndy Adamson 		       int how, int flags)
9961da177e4SLinus Torvalds {
997cd841605SFred Isaman 	struct inode *inode = data->header->inode;
9983ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
99907737691STrond Myklebust 	struct rpc_task *task;
1000bdc7f021STrond Myklebust 	struct rpc_message msg = {
1001bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1002bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
1003cd841605SFred Isaman 		.rpc_cred = data->header->cred,
1004bdc7f021STrond Myklebust 	};
100584115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
1006d138d5d1SAndy Adamson 		.rpc_client = clnt,
100707737691STrond Myklebust 		.task = &data->task,
1008bdc7f021STrond Myklebust 		.rpc_message = &msg,
100984115e1cSTrond Myklebust 		.callback_ops = call_ops,
101084115e1cSTrond Myklebust 		.callback_data = data,
1011101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
10129f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
10133ff7576dSTrond Myklebust 		.priority = priority,
101484115e1cSTrond Myklebust 	};
10152c61be0aSTrond Myklebust 	int ret = 0;
10161da177e4SLinus Torvalds 
1017d138d5d1SAndy Adamson 	/* Set up the initial task struct.  */
1018d138d5d1SAndy Adamson 	NFS_PROTO(inode)->write_setup(data, &msg);
1019d138d5d1SAndy Adamson 
1020d138d5d1SAndy Adamson 	dprintk("NFS: %5u initiated write call "
10211e8968c5SNiels de Vos 		"(req %s/%llu, %u bytes @ offset %llu)\n",
1022d138d5d1SAndy Adamson 		data->task.tk_pid,
1023d138d5d1SAndy Adamson 		inode->i_sb->s_id,
10241e8968c5SNiels de Vos 		(unsigned long long)NFS_FILEID(inode),
1025d138d5d1SAndy Adamson 		data->args.count,
1026d138d5d1SAndy Adamson 		(unsigned long long)data->args.offset);
1027d138d5d1SAndy Adamson 
10288c21c62cSWeston Andros Adamson 	nfs4_state_protect_write(NFS_SERVER(inode)->nfs_client,
10298c21c62cSWeston Andros Adamson 				 &task_setup_data.rpc_client, &msg, data);
10308c21c62cSWeston Andros Adamson 
1031d138d5d1SAndy Adamson 	task = rpc_run_task(&task_setup_data);
1032d138d5d1SAndy Adamson 	if (IS_ERR(task)) {
1033d138d5d1SAndy Adamson 		ret = PTR_ERR(task);
1034d138d5d1SAndy Adamson 		goto out;
1035d138d5d1SAndy Adamson 	}
1036d138d5d1SAndy Adamson 	if (how & FLUSH_SYNC) {
1037d138d5d1SAndy Adamson 		ret = rpc_wait_for_completion_task(task);
1038d138d5d1SAndy Adamson 		if (ret == 0)
1039d138d5d1SAndy Adamson 			ret = task->tk_status;
1040d138d5d1SAndy Adamson 	}
1041d138d5d1SAndy Adamson 	rpc_put_task(task);
1042d138d5d1SAndy Adamson out:
1043d138d5d1SAndy Adamson 	return ret;
1044d138d5d1SAndy Adamson }
1045a69aef14SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_write);
1046d138d5d1SAndy Adamson 
1047d138d5d1SAndy Adamson /*
1048d138d5d1SAndy Adamson  * Set up the argument/result storage required for the RPC call.
1049d138d5d1SAndy Adamson  */
10506c75dc0dSFred Isaman static void nfs_write_rpcsetup(struct nfs_write_data *data,
1051d138d5d1SAndy Adamson 		unsigned int count, unsigned int offset,
1052ea2cf228SFred Isaman 		int how, struct nfs_commit_info *cinfo)
1053d138d5d1SAndy Adamson {
10546c75dc0dSFred Isaman 	struct nfs_page *req = data->header->req;
1055d138d5d1SAndy Adamson 
10561da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
10571da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
10581da177e4SLinus Torvalds 
10596c75dc0dSFred Isaman 	data->args.fh     = NFS_FH(data->header->inode);
10601da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
10612bea038cSBoaz Harrosh 	/* pnfs_set_layoutcommit needs this */
10622bea038cSBoaz Harrosh 	data->mds_offset = data->args.offset;
10631da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
106430dd374fSFred Isaman 	data->args.pages  = data->pages.pagevec;
10651da177e4SLinus Torvalds 	data->args.count  = count;
1066383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
1067f11ac8dbSTrond Myklebust 	data->args.lock_context = req->wb_lock_context;
1068bdc7f021STrond Myklebust 	data->args.stable  = NFS_UNSTABLE;
106987ed5eb4STrond Myklebust 	switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
107087ed5eb4STrond Myklebust 	case 0:
107187ed5eb4STrond Myklebust 		break;
107287ed5eb4STrond Myklebust 	case FLUSH_COND_STABLE:
1073ea2cf228SFred Isaman 		if (nfs_reqs_to_commit(cinfo))
107487ed5eb4STrond Myklebust 			break;
107587ed5eb4STrond Myklebust 	default:
1076bdc7f021STrond Myklebust 		data->args.stable = NFS_FILE_SYNC;
1077bdc7f021STrond Myklebust 	}
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
10801da177e4SLinus Torvalds 	data->res.count   = count;
10811da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
10820e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
10836e4efd56STrond Myklebust }
10841da177e4SLinus Torvalds 
10856e4efd56STrond Myklebust static int nfs_do_write(struct nfs_write_data *data,
10866e4efd56STrond Myklebust 		const struct rpc_call_ops *call_ops,
10876e4efd56STrond Myklebust 		int how)
10886e4efd56STrond Myklebust {
1089cd841605SFred Isaman 	struct inode *inode = data->header->inode;
10900382b744SAndy Adamson 
10919f0ec176SAndy Adamson 	return nfs_initiate_write(NFS_CLIENT(inode), data, call_ops, how, 0);
10921da177e4SLinus Torvalds }
10931da177e4SLinus Torvalds 
1094275acaafSTrond Myklebust static int nfs_do_multiple_writes(struct list_head *head,
1095275acaafSTrond Myklebust 		const struct rpc_call_ops *call_ops,
1096275acaafSTrond Myklebust 		int how)
1097275acaafSTrond Myklebust {
1098275acaafSTrond Myklebust 	struct nfs_write_data *data;
1099275acaafSTrond Myklebust 	int ret = 0;
1100275acaafSTrond Myklebust 
1101275acaafSTrond Myklebust 	while (!list_empty(head)) {
1102275acaafSTrond Myklebust 		int ret2;
1103275acaafSTrond Myklebust 
11046c75dc0dSFred Isaman 		data = list_first_entry(head, struct nfs_write_data, list);
1105275acaafSTrond Myklebust 		list_del_init(&data->list);
1106275acaafSTrond Myklebust 
1107dce81290STrond Myklebust 		ret2 = nfs_do_write(data, call_ops, how);
1108275acaafSTrond Myklebust 		 if (ret == 0)
1109275acaafSTrond Myklebust 			 ret = ret2;
1110275acaafSTrond Myklebust 	}
1111275acaafSTrond Myklebust 	return ret;
1112275acaafSTrond Myklebust }
1113275acaafSTrond Myklebust 
11146d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
11156d884e8fSFred  * call this on each, which will prepare them to be retried on next
11166d884e8fSFred  * writeback using standard nfs.
11176d884e8fSFred  */
11186d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
11196d884e8fSFred {
11206d884e8fSFred 	nfs_mark_request_dirty(req);
11211d1afcbcSTrond Myklebust 	nfs_unlock_request(req);
1122d1182b33STrond Myklebust 	nfs_end_page_writeback(req->wb_page);
11233aff4ebbSTrond Myklebust 	nfs_release_request(req);
11246d884e8fSFred }
11256d884e8fSFred 
1126061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head)
11276c75dc0dSFred Isaman {
11286c75dc0dSFred Isaman 	struct nfs_page	*req;
11296c75dc0dSFred Isaman 
11306c75dc0dSFred Isaman 	while (!list_empty(head)) {
11316c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
11326c75dc0dSFred Isaman 		nfs_list_remove_request(req);
11336c75dc0dSFred Isaman 		nfs_redirty_request(req);
11346c75dc0dSFred Isaman 	}
11356c75dc0dSFred Isaman }
11366c75dc0dSFred Isaman 
1137061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1138061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1139061ae2edSFred Isaman 	.completion = nfs_write_completion,
1140061ae2edSFred Isaman };
1141061ae2edSFred Isaman 
114225b11dcdSTrond Myklebust static void nfs_flush_error(struct nfs_pageio_descriptor *desc,
114325b11dcdSTrond Myklebust 		struct nfs_pgio_header *hdr)
114425b11dcdSTrond Myklebust {
114525b11dcdSTrond Myklebust 	set_bit(NFS_IOHDR_REDO, &hdr->flags);
114625b11dcdSTrond Myklebust 	while (!list_empty(&hdr->rpc_list)) {
114725b11dcdSTrond Myklebust 		struct nfs_write_data *data = list_first_entry(&hdr->rpc_list,
114825b11dcdSTrond Myklebust 				struct nfs_write_data, list);
114925b11dcdSTrond Myklebust 		list_del(&data->list);
115025b11dcdSTrond Myklebust 		nfs_writedata_release(data);
115125b11dcdSTrond Myklebust 	}
115225b11dcdSTrond Myklebust 	desc->pg_completion_ops->error_cleanup(&desc->pg_list);
115325b11dcdSTrond Myklebust }
115425b11dcdSTrond Myklebust 
11551da177e4SLinus Torvalds /*
11561da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
11571da177e4SLinus Torvalds  * contiguous dirty area on one page.
11581da177e4SLinus Torvalds  */
11596c75dc0dSFred Isaman static int nfs_flush_multi(struct nfs_pageio_descriptor *desc,
11606c75dc0dSFred Isaman 			   struct nfs_pgio_header *hdr)
11611da177e4SLinus Torvalds {
11626c75dc0dSFred Isaman 	struct nfs_page *req = hdr->req;
11631da177e4SLinus Torvalds 	struct page *page = req->wb_page;
11641da177e4SLinus Torvalds 	struct nfs_write_data *data;
1165d097971dSTrond Myklebust 	size_t wsize = desc->pg_bsize, nbytes;
1166e9f7bee1STrond Myklebust 	unsigned int offset;
11671da177e4SLinus Torvalds 	int requests = 0;
1168ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
11691da177e4SLinus Torvalds 
1170ea2cf228SFred Isaman 	nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
11711da177e4SLinus Torvalds 
1172b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1173ea2cf228SFred Isaman 	    (desc->pg_moreio || nfs_reqs_to_commit(&cinfo) ||
1174b31268acSTrond Myklebust 	     desc->pg_count > wsize))
1175b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1176b31268acSTrond Myklebust 
1177b31268acSTrond Myklebust 
1178275acaafSTrond Myklebust 	offset = 0;
1179c76069bdSFred Isaman 	nbytes = desc->pg_count;
1180e9f7bee1STrond Myklebust 	do {
1181e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
1182e9f7bee1STrond Myklebust 
11836c75dc0dSFred Isaman 		data = nfs_writedata_alloc(hdr, 1);
118425b11dcdSTrond Myklebust 		if (!data) {
118525b11dcdSTrond Myklebust 			nfs_flush_error(desc, hdr);
118625b11dcdSTrond Myklebust 			return -ENOMEM;
118725b11dcdSTrond Myklebust 		}
118830dd374fSFred Isaman 		data->pages.pagevec[0] = page;
1189ea2cf228SFred Isaman 		nfs_write_rpcsetup(data, len, offset, desc->pg_ioflags, &cinfo);
11906c75dc0dSFred Isaman 		list_add(&data->list, &hdr->rpc_list);
11911da177e4SLinus Torvalds 		requests++;
1192e9f7bee1STrond Myklebust 		nbytes -= len;
1193275acaafSTrond Myklebust 		offset += len;
1194e9f7bee1STrond Myklebust 	} while (nbytes != 0);
119525b11dcdSTrond Myklebust 	nfs_list_remove_request(req);
119625b11dcdSTrond Myklebust 	nfs_list_add_request(req, &hdr->pages);
11976c75dc0dSFred Isaman 	desc->pg_rpc_callops = &nfs_write_common_ops;
119825b11dcdSTrond Myklebust 	return 0;
11991da177e4SLinus Torvalds }
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds /*
12021da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
12031da177e4SLinus Torvalds  * The page must have been locked by the caller.
12041da177e4SLinus Torvalds  *
12051da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
12061da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
12071da177e4SLinus Torvalds  * that has been written but not committed.
12081da177e4SLinus Torvalds  */
12096c75dc0dSFred Isaman static int nfs_flush_one(struct nfs_pageio_descriptor *desc,
12106c75dc0dSFred Isaman 			 struct nfs_pgio_header *hdr)
12111da177e4SLinus Torvalds {
12121da177e4SLinus Torvalds 	struct nfs_page		*req;
12131da177e4SLinus Torvalds 	struct page		**pages;
12141da177e4SLinus Torvalds 	struct nfs_write_data	*data;
1215c76069bdSFred Isaman 	struct list_head *head = &desc->pg_list;
1216ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
12171da177e4SLinus Torvalds 
12186c75dc0dSFred Isaman 	data = nfs_writedata_alloc(hdr, nfs_page_array_len(desc->pg_base,
1219c76069bdSFred Isaman 							   desc->pg_count));
12206c75dc0dSFred Isaman 	if (!data) {
122125b11dcdSTrond Myklebust 		nfs_flush_error(desc, hdr);
122225b11dcdSTrond Myklebust 		return -ENOMEM;
122344b83799SFred Isaman 	}
12246c75dc0dSFred Isaman 
1225ea2cf228SFred Isaman 	nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
122630dd374fSFred Isaman 	pages = data->pages.pagevec;
12271da177e4SLinus Torvalds 	while (!list_empty(head)) {
12281da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
12291da177e4SLinus Torvalds 		nfs_list_remove_request(req);
12306c75dc0dSFred Isaman 		nfs_list_add_request(req, &hdr->pages);
12311da177e4SLinus Torvalds 		*pages++ = req->wb_page;
12321da177e4SLinus Torvalds 	}
12331da177e4SLinus Torvalds 
1234b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1235ea2cf228SFred Isaman 	    (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
1236b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1237b31268acSTrond Myklebust 
12381da177e4SLinus Torvalds 	/* Set up the argument struct */
1239ea2cf228SFred Isaman 	nfs_write_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo);
12406c75dc0dSFred Isaman 	list_add(&data->list, &hdr->rpc_list);
12416c75dc0dSFred Isaman 	desc->pg_rpc_callops = &nfs_write_common_ops;
124225b11dcdSTrond Myklebust 	return 0;
12431da177e4SLinus Torvalds }
12441da177e4SLinus Torvalds 
12456c75dc0dSFred Isaman int nfs_generic_flush(struct nfs_pageio_descriptor *desc,
12466c75dc0dSFred Isaman 		      struct nfs_pgio_header *hdr)
1247dce81290STrond Myklebust {
1248dce81290STrond Myklebust 	if (desc->pg_bsize < PAGE_CACHE_SIZE)
12496c75dc0dSFred Isaman 		return nfs_flush_multi(desc, hdr);
12506c75dc0dSFred Isaman 	return nfs_flush_one(desc, hdr);
1251dce81290STrond Myklebust }
125289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_generic_flush);
1253dce81290STrond Myklebust 
1254dce81290STrond Myklebust static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
12551751c363STrond Myklebust {
12566c75dc0dSFred Isaman 	struct nfs_write_header *whdr;
12576c75dc0dSFred Isaman 	struct nfs_pgio_header *hdr;
1258275acaafSTrond Myklebust 	int ret;
1259275acaafSTrond Myklebust 
12606c75dc0dSFred Isaman 	whdr = nfs_writehdr_alloc();
12616c75dc0dSFred Isaman 	if (!whdr) {
12629b5415b5STrond Myklebust 		desc->pg_completion_ops->error_cleanup(&desc->pg_list);
12636c75dc0dSFred Isaman 		return -ENOMEM;
12646c75dc0dSFred Isaman 	}
12656c75dc0dSFred Isaman 	hdr = &whdr->header;
12666c75dc0dSFred Isaman 	nfs_pgheader_init(desc, hdr, nfs_writehdr_free);
12676c75dc0dSFred Isaman 	atomic_inc(&hdr->refcnt);
12686c75dc0dSFred Isaman 	ret = nfs_generic_flush(desc, hdr);
1269275acaafSTrond Myklebust 	if (ret == 0)
12706c75dc0dSFred Isaman 		ret = nfs_do_multiple_writes(&hdr->rpc_list,
12716c75dc0dSFred Isaman 					     desc->pg_rpc_callops,
1272dce81290STrond Myklebust 					     desc->pg_ioflags);
12736c75dc0dSFred Isaman 	if (atomic_dec_and_test(&hdr->refcnt))
1274061ae2edSFred Isaman 		hdr->completion_ops->completion(hdr);
1275275acaafSTrond Myklebust 	return ret;
12761751c363STrond Myklebust }
12771751c363STrond Myklebust 
12781751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_write_ops = {
12791751c363STrond Myklebust 	.pg_test = nfs_generic_pg_test,
12801751c363STrond Myklebust 	.pg_doio = nfs_generic_pg_writepages,
12811751c363STrond Myklebust };
12821751c363STrond Myklebust 
128357208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1284a20c93e3SChristoph Hellwig 			       struct inode *inode, int ioflags, bool force_mds,
1285061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
12861751c363STrond Myklebust {
1287a20c93e3SChristoph Hellwig 	struct nfs_server *server = NFS_SERVER(inode);
1288a20c93e3SChristoph Hellwig 	const struct nfs_pageio_ops *pg_ops = &nfs_pageio_write_ops;
1289a20c93e3SChristoph Hellwig 
1290a20c93e3SChristoph Hellwig #ifdef CONFIG_NFS_V4_1
1291a20c93e3SChristoph Hellwig 	if (server->pnfs_curr_ld && !force_mds)
1292a20c93e3SChristoph Hellwig 		pg_ops = server->pnfs_curr_ld->pg_write_ops;
1293a20c93e3SChristoph Hellwig #endif
1294a20c93e3SChristoph Hellwig 	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, server->wsize, ioflags);
12951751c363STrond Myklebust }
1296ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
12971751c363STrond Myklebust 
1298dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1299dce81290STrond Myklebust {
1300dce81290STrond Myklebust 	pgio->pg_ops = &nfs_pageio_write_ops;
1301dce81290STrond Myklebust 	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1302dce81290STrond Myklebust }
13031f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1304dce81290STrond Myklebust 
13051da177e4SLinus Torvalds 
1306def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata)
1307def6ed7eSAndy Adamson {
1308def6ed7eSAndy Adamson 	struct nfs_write_data *data = calldata;
1309ef1820f9SNeilBrown 	int err;
1310ef1820f9SNeilBrown 	err = NFS_PROTO(data->header->inode)->write_rpc_prepare(task, data);
1311ef1820f9SNeilBrown 	if (err)
1312ef1820f9SNeilBrown 		rpc_exit(task, err);
1313def6ed7eSAndy Adamson }
1314def6ed7eSAndy Adamson 
13150b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
13160b7c0153SFred Isaman {
13170b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
13180b7c0153SFred Isaman 
13190b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
13200b7c0153SFred Isaman }
13210b7c0153SFred Isaman 
13221da177e4SLinus Torvalds /*
13231da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
13241da177e4SLinus Torvalds  *
13251da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
13261da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
13271da177e4SLinus Torvalds  *	  as the page has a write request pending.
13281da177e4SLinus Torvalds  */
13296c75dc0dSFred Isaman static void nfs_writeback_done_common(struct rpc_task *task, void *calldata)
13301da177e4SLinus Torvalds {
1331788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
13321da177e4SLinus Torvalds 
1333c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
1334c9d8f89dSTrond Myklebust }
1335c9d8f89dSTrond Myklebust 
13366c75dc0dSFred Isaman static void nfs_writeback_release_common(void *calldata)
1337c9d8f89dSTrond Myklebust {
1338c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1339cd841605SFred Isaman 	struct nfs_pgio_header *hdr = data->header;
1340e2fecb21STrond Myklebust 	int status = data->task.tk_status;
1341788e7a89STrond Myklebust 
13426c75dc0dSFred Isaman 	if ((status >= 0) && nfs_write_need_commit(data)) {
13436c75dc0dSFred Isaman 		spin_lock(&hdr->lock);
13446c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags))
13456c75dc0dSFred Isaman 			; /* Do nothing */
13466c75dc0dSFred Isaman 		else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags))
13479bce008bSTrond Myklebust 			memcpy(hdr->verf, &data->verf, sizeof(*hdr->verf));
13489bce008bSTrond Myklebust 		else if (memcmp(hdr->verf, &data->verf, sizeof(*hdr->verf)))
13496c75dc0dSFred Isaman 			set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags);
13506c75dc0dSFred Isaman 		spin_unlock(&hdr->lock);
13511da177e4SLinus Torvalds 	}
1352cd841605SFred Isaman 	nfs_writedata_release(data);
13531da177e4SLinus Torvalds }
13541da177e4SLinus Torvalds 
13556c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops = {
1356def6ed7eSAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
13576c75dc0dSFred Isaman 	.rpc_call_done = nfs_writeback_done_common,
13586c75dc0dSFred Isaman 	.rpc_release = nfs_writeback_release_common,
1359788e7a89STrond Myklebust };
1360788e7a89STrond Myklebust 
13611f2edbe3STrond Myklebust /*
13621f2edbe3STrond Myklebust  * Special version of should_remove_suid() that ignores capabilities.
13631f2edbe3STrond Myklebust  */
13641f2edbe3STrond Myklebust static int nfs_should_remove_suid(const struct inode *inode)
13651f2edbe3STrond Myklebust {
13661f2edbe3STrond Myklebust 	umode_t mode = inode->i_mode;
13671f2edbe3STrond Myklebust 	int kill = 0;
13681f2edbe3STrond Myklebust 
13691f2edbe3STrond Myklebust 	/* suid always must be killed */
13701f2edbe3STrond Myklebust 	if (unlikely(mode & S_ISUID))
13711f2edbe3STrond Myklebust 		kill = ATTR_KILL_SUID;
13721f2edbe3STrond Myklebust 
13731f2edbe3STrond Myklebust 	/*
13741f2edbe3STrond Myklebust 	 * sgid without any exec bits is just a mandatory locking mark; leave
13751f2edbe3STrond Myklebust 	 * it alone.  If some exec bits are set, it's a real sgid; kill it.
13761f2edbe3STrond Myklebust 	 */
13771f2edbe3STrond Myklebust 	if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
13781f2edbe3STrond Myklebust 		kill |= ATTR_KILL_SGID;
13791f2edbe3STrond Myklebust 
13801f2edbe3STrond Myklebust 	if (unlikely(kill && S_ISREG(mode)))
13811f2edbe3STrond Myklebust 		return kill;
13821f2edbe3STrond Myklebust 
13831f2edbe3STrond Myklebust 	return 0;
13841f2edbe3STrond Myklebust }
1385788e7a89STrond Myklebust 
13861da177e4SLinus Torvalds /*
13871da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
13881da177e4SLinus Torvalds  */
138913602896SFred Isaman void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
13901da177e4SLinus Torvalds {
1391*3c6b899cSAnna Schumaker 	struct nfs_pgio_args	*argp = &data->args;
13921da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
1393cd841605SFred Isaman 	struct inode		*inode = data->header->inode;
1394788e7a89STrond Myklebust 	int status;
13951da177e4SLinus Torvalds 
1396a3f565b1SChuck Lever 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
13971da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
13981da177e4SLinus Torvalds 
1399f551e44fSChuck Lever 	/*
1400f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1401f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1402f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1403f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1404f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1405f551e44fSChuck Lever 	 */
1406cd841605SFred Isaman 	status = NFS_PROTO(inode)->write_done(task, data);
1407788e7a89STrond Myklebust 	if (status != 0)
140813602896SFred Isaman 		return;
1409cd841605SFred Isaman 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
141091d5b470SChuck Lever 
141189d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
14121da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
14131da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
14141da177e4SLinus Torvalds 		 * commit data to stable storage even though we
14151da177e4SLinus Torvalds 		 * requested it.
14161da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
14171da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
14181da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
14191da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
14201da177e4SLinus Torvalds 		 */
14211da177e4SLinus Torvalds 		static unsigned long    complain;
14221da177e4SLinus Torvalds 
1423a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
14241da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
14251da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
14261da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1427cd841605SFred Isaman 				NFS_SERVER(inode)->nfs_client->cl_hostname,
14281da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
14291da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
14301da177e4SLinus Torvalds 		}
14311da177e4SLinus Torvalds 	}
14321da177e4SLinus Torvalds #endif
14331f2edbe3STrond Myklebust 	if (task->tk_status < 0) {
14346c75dc0dSFred Isaman 		nfs_set_pgio_error(data->header, task->tk_status, argp->offset);
14351f2edbe3STrond Myklebust 		return;
14361f2edbe3STrond Myklebust 	}
14371f2edbe3STrond Myklebust 
14381f2edbe3STrond Myklebust 	/* Deal with the suid/sgid bit corner case */
14391f2edbe3STrond Myklebust 	if (nfs_should_remove_suid(inode))
14401f2edbe3STrond Myklebust 		nfs_mark_for_revalidate(inode);
14411f2edbe3STrond Myklebust 
14421f2edbe3STrond Myklebust 	if (resp->count < argp->count) {
14431da177e4SLinus Torvalds 		static unsigned long    complain;
14441da177e4SLinus Torvalds 
14456c75dc0dSFred Isaman 		/* This a short write! */
1446cd841605SFred Isaman 		nfs_inc_stats(inode, NFSIOS_SHORTWRITE);
144791d5b470SChuck Lever 
14481da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
14496c75dc0dSFred Isaman 		if (resp->count == 0) {
14506c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
14516c75dc0dSFred Isaman 				printk(KERN_WARNING
14526c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
14536c75dc0dSFred Isaman 				       argp->count);
14546c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
14556c75dc0dSFred Isaman 			}
14566c75dc0dSFred Isaman 			nfs_set_pgio_error(data->header, -EIO, argp->offset);
14576c75dc0dSFred Isaman 			task->tk_status = -EIO;
14586c75dc0dSFred Isaman 			return;
14596c75dc0dSFred Isaman 		}
14601da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
14611da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
14621da177e4SLinus Torvalds 			/* Resend from where the server left off */
1463a69aef14SFred Isaman 			data->mds_offset += resp->count;
14641da177e4SLinus Torvalds 			argp->offset += resp->count;
14651da177e4SLinus Torvalds 			argp->pgbase += resp->count;
14661da177e4SLinus Torvalds 			argp->count -= resp->count;
14671da177e4SLinus Torvalds 		} else {
14681da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
14691da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
14701da177e4SLinus Torvalds 			 */
14711da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
14721da177e4SLinus Torvalds 		}
1473d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
14741da177e4SLinus Torvalds 	}
14751da177e4SLinus Torvalds }
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 
147889d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
147971d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
148071d0a611STrond Myklebust {
1481b8413f98STrond Myklebust 	int ret;
1482b8413f98STrond Myklebust 
148371d0a611STrond Myklebust 	if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
148471d0a611STrond Myklebust 		return 1;
1485b8413f98STrond Myklebust 	if (!may_wait)
148671d0a611STrond Myklebust 		return 0;
1487b8413f98STrond Myklebust 	ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1488b8413f98STrond Myklebust 				NFS_INO_COMMIT,
1489b8413f98STrond Myklebust 				nfs_wait_bit_killable,
1490b8413f98STrond Myklebust 				TASK_KILLABLE);
1491b8413f98STrond Myklebust 	return (ret < 0) ? ret : 1;
149271d0a611STrond Myklebust }
149371d0a611STrond Myklebust 
1494f453a54aSFred Isaman static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
149571d0a611STrond Myklebust {
149671d0a611STrond Myklebust 	clear_bit(NFS_INO_COMMIT, &nfsi->flags);
149771d0a611STrond Myklebust 	smp_mb__after_clear_bit();
149871d0a611STrond Myklebust 	wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
149971d0a611STrond Myklebust }
150071d0a611STrond Myklebust 
15010b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
15021da177e4SLinus Torvalds {
15030b7c0153SFred Isaman 	put_nfs_open_context(data->context);
15040b7c0153SFred Isaman 	nfs_commit_free(data);
15051da177e4SLinus Torvalds }
1506e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
15071da177e4SLinus Torvalds 
15080b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
15099ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
15109f0ec176SAndy Adamson 			int how, int flags)
15111da177e4SLinus Torvalds {
151207737691STrond Myklebust 	struct rpc_task *task;
15139ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1514bdc7f021STrond Myklebust 	struct rpc_message msg = {
1515bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1516bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
15179ace33cdSFred Isaman 		.rpc_cred = data->cred,
1518bdc7f021STrond Myklebust 	};
151984115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
152007737691STrond Myklebust 		.task = &data->task,
15219ace33cdSFred Isaman 		.rpc_client = clnt,
1522bdc7f021STrond Myklebust 		.rpc_message = &msg,
15239ace33cdSFred Isaman 		.callback_ops = call_ops,
152484115e1cSTrond Myklebust 		.callback_data = data,
1525101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
15269f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
15273ff7576dSTrond Myklebust 		.priority = priority,
152884115e1cSTrond Myklebust 	};
1529788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
15309ace33cdSFred Isaman 	NFS_PROTO(data->inode)->commit_setup(data, &msg);
15311da177e4SLinus Torvalds 
1532a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1533bdc7f021STrond Myklebust 
15348c21c62cSWeston Andros Adamson 	nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client,
15358c21c62cSWeston Andros Adamson 		NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg);
15368c21c62cSWeston Andros Adamson 
153707737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1538dbae4c73STrond Myklebust 	if (IS_ERR(task))
1539dbae4c73STrond Myklebust 		return PTR_ERR(task);
1540d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1541d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
154207737691STrond Myklebust 	rpc_put_task(task);
1543dbae4c73STrond Myklebust 	return 0;
15441da177e4SLinus Torvalds }
1545e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds /*
15489ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
15499ace33cdSFred Isaman  */
15500b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1551988b6dceSFred Isaman 		     struct list_head *head,
1552f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1553f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
15549ace33cdSFred Isaman {
15559ace33cdSFred Isaman 	struct nfs_page *first = nfs_list_entry(head->next);
15563d4ff43dSAl Viro 	struct inode *inode = first->wb_context->dentry->d_inode;
15579ace33cdSFred Isaman 
15589ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
15599ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
15609ace33cdSFred Isaman 
15619ace33cdSFred Isaman 	list_splice_init(head, &data->pages);
15629ace33cdSFred Isaman 
15639ace33cdSFred Isaman 	data->inode	  = inode;
15649ace33cdSFred Isaman 	data->cred	  = first->wb_context->cred;
1565988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
15669ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1567f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1568b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
15699ace33cdSFred Isaman 
15709ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
15719ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
15729ace33cdSFred Isaman 	data->args.offset = 0;
15739ace33cdSFred Isaman 	data->args.count  = 0;
15740b7c0153SFred Isaman 	data->context     = get_nfs_open_context(first->wb_context);
15759ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
15769ace33cdSFred Isaman 	data->res.verf    = &data->verf;
15779ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
15789ace33cdSFred Isaman }
1579e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
15809ace33cdSFred Isaman 
1581e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1582ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1583ea2cf228SFred Isaman 		      struct nfs_commit_info *cinfo)
158464bfeb49SFred Isaman {
158564bfeb49SFred Isaman 	struct nfs_page *req;
158664bfeb49SFred Isaman 
158764bfeb49SFred Isaman 	while (!list_empty(page_list)) {
158864bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
158964bfeb49SFred Isaman 		nfs_list_remove_request(req);
1590ea2cf228SFred Isaman 		nfs_mark_request_commit(req, lseg, cinfo);
159156f9cd68SFred Isaman 		if (!cinfo->dreq) {
159264bfeb49SFred Isaman 			dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1593d56b4ddfSMel Gorman 			dec_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
159464bfeb49SFred Isaman 				     BDI_RECLAIMABLE);
159556f9cd68SFred Isaman 		}
15961d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
159764bfeb49SFred Isaman 	}
159864bfeb49SFred Isaman }
1599e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
160064bfeb49SFred Isaman 
16019ace33cdSFred Isaman /*
16021da177e4SLinus Torvalds  * Commit dirty pages
16031da177e4SLinus Torvalds  */
16041da177e4SLinus Torvalds static int
1605ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1606ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
16071da177e4SLinus Torvalds {
16080b7c0153SFred Isaman 	struct nfs_commit_data	*data;
16091da177e4SLinus Torvalds 
1610c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds 	if (!data)
16131da177e4SLinus Torvalds 		goto out_bad;
16141da177e4SLinus Torvalds 
16151da177e4SLinus Torvalds 	/* Set up the argument struct */
1616f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
1617f453a54aSFred Isaman 	atomic_inc(&cinfo->mds->rpcs_out);
16189f0ec176SAndy Adamson 	return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops,
16199f0ec176SAndy Adamson 				   how, 0);
16201da177e4SLinus Torvalds  out_bad:
1621ea2cf228SFred Isaman 	nfs_retry_commit(head, NULL, cinfo);
1622f453a54aSFred Isaman 	cinfo->completion_ops->error_cleanup(NFS_I(inode));
16231da177e4SLinus Torvalds 	return -ENOMEM;
16241da177e4SLinus Torvalds }
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds /*
16271da177e4SLinus Torvalds  * COMMIT call returned
16281da177e4SLinus Torvalds  */
1629788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
16301da177e4SLinus Torvalds {
16310b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
16321da177e4SLinus Torvalds 
1633a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
16341da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
16351da177e4SLinus Torvalds 
1636788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1637c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
1638c9d8f89dSTrond Myklebust }
1639c9d8f89dSTrond Myklebust 
1640f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1641c9d8f89dSTrond Myklebust {
1642c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1643c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1644f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
1645788e7a89STrond Myklebust 
16461da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
16471da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
16481da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1649d6d6dc7cSFred Isaman 		nfs_clear_page_commit(req->wb_page);
16501da177e4SLinus Torvalds 
16511e8968c5SNiels de Vos 		dprintk("NFS:       commit (%s/%llu %d@%lld)",
16523d4ff43dSAl Viro 			req->wb_context->dentry->d_sb->s_id,
16531e8968c5SNiels de Vos 			(unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
16541da177e4SLinus Torvalds 			req->wb_bytes,
16551da177e4SLinus Torvalds 			(long long)req_offset(req));
1656c9d8f89dSTrond Myklebust 		if (status < 0) {
1657c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
16581da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1659c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
16601da177e4SLinus Torvalds 			goto next;
16611da177e4SLinus Torvalds 		}
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
16641da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
16652f2c63bcSTrond Myklebust 		if (!memcmp(&req->wb_verf, &data->verf.verifier, sizeof(req->wb_verf))) {
16661da177e4SLinus Torvalds 			/* We have a match */
16671da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
16681da177e4SLinus Torvalds 			dprintk(" OK\n");
16691da177e4SLinus Torvalds 			goto next;
16701da177e4SLinus Torvalds 		}
16711da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
16721da177e4SLinus Torvalds 		dprintk(" mismatch\n");
16736d884e8fSFred 		nfs_mark_request_dirty(req);
167405990d1bSTrond Myklebust 		set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
16751da177e4SLinus Torvalds 	next:
16761d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
16771da177e4SLinus Torvalds 	}
1678f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1679f453a54aSFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
1680f453a54aSFred Isaman 		nfs_commit_clear_lock(NFS_I(data->inode));
16815917ce84SFred Isaman }
16825917ce84SFred Isaman 
16835917ce84SFred Isaman static void nfs_commit_release(void *calldata)
16845917ce84SFred Isaman {
16850b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
16865917ce84SFred Isaman 
1687f453a54aSFred Isaman 	data->completion_ops->completion(data);
1688c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
16891da177e4SLinus Torvalds }
1690788e7a89STrond Myklebust 
1691788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
16920b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1693788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1694788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1695788e7a89STrond Myklebust };
16961da177e4SLinus Torvalds 
1697f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1698f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1699f453a54aSFred Isaman 	.error_cleanup = nfs_commit_clear_lock,
1700f453a54aSFred Isaman };
1701f453a54aSFred Isaman 
17021763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1703ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
170484c53ab5SFred Isaman {
170584c53ab5SFred Isaman 	int status;
170684c53ab5SFred Isaman 
1707ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
170884c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1709ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
171084c53ab5SFred Isaman 	return status;
171184c53ab5SFred Isaman }
171284c53ab5SFred Isaman 
1713b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
17141da177e4SLinus Torvalds {
17151da177e4SLinus Torvalds 	LIST_HEAD(head);
1716ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
171771d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1718b8413f98STrond Myklebust 	int res;
17191da177e4SLinus Torvalds 
1720b8413f98STrond Myklebust 	res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1721b8413f98STrond Myklebust 	if (res <= 0)
1722c5efa5fcSTrond Myklebust 		goto out_mark_dirty;
1723ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1724ea2cf228SFred Isaman 	res = nfs_scan_commit(inode, &head, &cinfo);
17251da177e4SLinus Torvalds 	if (res) {
1726a861a1e1SFred Isaman 		int error;
1727a861a1e1SFred Isaman 
1728ea2cf228SFred Isaman 		error = nfs_generic_commit_list(inode, &head, how, &cinfo);
17291da177e4SLinus Torvalds 		if (error < 0)
17301da177e4SLinus Torvalds 			return error;
1731b8413f98STrond Myklebust 		if (!may_wait)
1732b8413f98STrond Myklebust 			goto out_mark_dirty;
1733b8413f98STrond Myklebust 		error = wait_on_bit(&NFS_I(inode)->flags,
1734b8413f98STrond Myklebust 				NFS_INO_COMMIT,
173571d0a611STrond Myklebust 				nfs_wait_bit_killable,
173671d0a611STrond Myklebust 				TASK_KILLABLE);
1737b8413f98STrond Myklebust 		if (error < 0)
1738b8413f98STrond Myklebust 			return error;
173971d0a611STrond Myklebust 	} else
174071d0a611STrond Myklebust 		nfs_commit_clear_lock(NFS_I(inode));
1741c5efa5fcSTrond Myklebust 	return res;
1742c5efa5fcSTrond Myklebust 	/* Note: If we exit without ensuring that the commit is complete,
1743c5efa5fcSTrond Myklebust 	 * we must mark the inode as dirty. Otherwise, future calls to
1744c5efa5fcSTrond Myklebust 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1745c5efa5fcSTrond Myklebust 	 * that the data is on the disk.
1746c5efa5fcSTrond Myklebust 	 */
1747c5efa5fcSTrond Myklebust out_mark_dirty:
1748c5efa5fcSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
17491da177e4SLinus Torvalds 	return res;
17501da177e4SLinus Torvalds }
17518fc795f7STrond Myklebust 
17528fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
17538fc795f7STrond Myklebust {
1754420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1755420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1756420e3646STrond Myklebust 	int ret = 0;
17578fc795f7STrond Myklebust 
17583236c3e1SJeff Layton 	/* no commits means nothing needs to be done */
1759ea2cf228SFred Isaman 	if (!nfsi->commit_info.ncommit)
17603236c3e1SJeff Layton 		return ret;
17613236c3e1SJeff Layton 
1762a00dd6c0SJeff Layton 	if (wbc->sync_mode == WB_SYNC_NONE) {
1763a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1764a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1765420e3646STrond Myklebust 		 */
1766ea2cf228SFred Isaman 		if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1))
1767420e3646STrond Myklebust 			goto out_mark_dirty;
1768420e3646STrond Myklebust 
1769a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1770420e3646STrond Myklebust 		flags = 0;
1771a00dd6c0SJeff Layton 	}
1772a00dd6c0SJeff Layton 
1773420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1774420e3646STrond Myklebust 	if (ret >= 0) {
1775420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1776420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1777420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1778420e3646STrond Myklebust 			else
1779420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1780420e3646STrond Myklebust 		}
17818fc795f7STrond Myklebust 		return 0;
1782420e3646STrond Myklebust 	}
1783420e3646STrond Myklebust out_mark_dirty:
17848fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
17858fc795f7STrond Myklebust 	return ret;
17868fc795f7STrond Myklebust }
1787c63c7b05STrond Myklebust #else
17888fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
17898fc795f7STrond Myklebust {
17908fc795f7STrond Myklebust 	return 0;
17918fc795f7STrond Myklebust }
17921da177e4SLinus Torvalds #endif
17931da177e4SLinus Torvalds 
17948fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
17958fc795f7STrond Myklebust {
1796a8d8f02cSBryan Schumaker 	return nfs_commit_unstable_pages(inode, wbc);
1797a8d8f02cSBryan Schumaker }
179889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode);
1799863a3c6cSAndy Adamson 
1800acdc53b2STrond Myklebust /*
1801acdc53b2STrond Myklebust  * flush the inode to disk.
1802acdc53b2STrond Myklebust  */
1803acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
180434901f70STrond Myklebust {
180534901f70STrond Myklebust 	struct writeback_control wbc = {
180672cb77f4STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
180734901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
1808d7fb1207STrond Myklebust 		.range_start = 0,
1809d7fb1207STrond Myklebust 		.range_end = LLONG_MAX,
181034901f70STrond Myklebust 	};
1811f4ce1299STrond Myklebust 	int ret;
181234901f70STrond Myklebust 
1813f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_enter(inode);
1814f4ce1299STrond Myklebust 
1815f4ce1299STrond Myklebust 	ret = sync_inode(inode, &wbc);
1816f4ce1299STrond Myklebust 
1817f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_exit(inode, ret);
1818f4ce1299STrond Myklebust 	return ret;
18191c75950bSTrond Myklebust }
1820ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all);
18211c75950bSTrond Myklebust 
18221b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
18231b3b4a1aSTrond Myklebust {
18241b3b4a1aSTrond Myklebust 	struct nfs_page *req;
18251b3b4a1aSTrond Myklebust 	int ret = 0;
18261b3b4a1aSTrond Myklebust 
18271b3b4a1aSTrond Myklebust 	for (;;) {
1828ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
18291b3b4a1aSTrond Myklebust 		req = nfs_page_find_request(page);
18301b3b4a1aSTrond Myklebust 		if (req == NULL)
18311b3b4a1aSTrond Myklebust 			break;
18327ad84aa9STrond Myklebust 		if (nfs_lock_request(req)) {
18338dd37758STrond Myklebust 			nfs_clear_request_commit(req);
18341b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
18351b3b4a1aSTrond Myklebust 			/*
18361b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
18371b3b4a1aSTrond Myklebust 			 * page as being dirty
18381b3b4a1aSTrond Myklebust 			 */
18391b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
18401d1afcbcSTrond Myklebust 			nfs_unlock_and_release_request(req);
18411b3b4a1aSTrond Myklebust 			break;
18421b3b4a1aSTrond Myklebust 		}
18431b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
1844c9edda71STrond Myklebust 		nfs_release_request(req);
18451b3b4a1aSTrond Myklebust 		if (ret < 0)
1846c988950eSTrond Myklebust 			break;
18471b3b4a1aSTrond Myklebust 	}
18481b3b4a1aSTrond Myklebust 	return ret;
18491b3b4a1aSTrond Myklebust }
18501b3b4a1aSTrond Myklebust 
18511c75950bSTrond Myklebust /*
18521c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
18531c75950bSTrond Myklebust  */
18541c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
18551c75950bSTrond Myklebust {
185629418aa4SMel Gorman 	loff_t range_start = page_file_offset(page);
18577f2f12d9STrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
18587f2f12d9STrond Myklebust 	struct writeback_control wbc = {
18597f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
18607f2f12d9STrond Myklebust 		.nr_to_write = 0,
18617f2f12d9STrond Myklebust 		.range_start = range_start,
18627f2f12d9STrond Myklebust 		.range_end = range_end,
18637f2f12d9STrond Myklebust 	};
18647f2f12d9STrond Myklebust 	int ret;
18657f2f12d9STrond Myklebust 
1866f4ce1299STrond Myklebust 	trace_nfs_writeback_page_enter(inode);
1867f4ce1299STrond Myklebust 
18680522f6adSTrond Myklebust 	for (;;) {
1869ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
18707f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
18717f2f12d9STrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
18727f2f12d9STrond Myklebust 			if (ret < 0)
18737f2f12d9STrond Myklebust 				goto out_error;
18740522f6adSTrond Myklebust 			continue;
18757f2f12d9STrond Myklebust 		}
1876f4ce1299STrond Myklebust 		ret = 0;
18770522f6adSTrond Myklebust 		if (!PagePrivate(page))
18780522f6adSTrond Myklebust 			break;
18790522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
18807f2f12d9STrond Myklebust 		if (ret < 0)
18817f2f12d9STrond Myklebust 			goto out_error;
18827f2f12d9STrond Myklebust 	}
18837f2f12d9STrond Myklebust out_error:
1884f4ce1299STrond Myklebust 	trace_nfs_writeback_page_exit(inode, ret);
18857f2f12d9STrond Myklebust 	return ret;
18861c75950bSTrond Myklebust }
18871c75950bSTrond Myklebust 
1888074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
1889074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1890a6bc32b8SMel Gorman 		struct page *page, enum migrate_mode mode)
1891074cc1deSTrond Myklebust {
18922da95652SJeff Layton 	/*
18932da95652SJeff Layton 	 * If PagePrivate is set, then the page is currently associated with
18942da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
18952da95652SJeff Layton 	 *
18962da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
18972da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
18982da95652SJeff Layton 	 *        the page lock.
18992da95652SJeff Layton 	 */
19002da95652SJeff Layton 	if (PagePrivate(page))
19012da95652SJeff Layton 		return -EBUSY;
1902074cc1deSTrond Myklebust 
19038c209ce7SDavid Howells 	if (!nfs_fscache_release_page(page, GFP_KERNEL))
19048c209ce7SDavid Howells 		return -EBUSY;
1905074cc1deSTrond Myklebust 
1906a6bc32b8SMel Gorman 	return migrate_page(mapping, newpage, page, mode);
1907074cc1deSTrond Myklebust }
1908074cc1deSTrond Myklebust #endif
1909074cc1deSTrond Myklebust 
1910f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
19111da177e4SLinus Torvalds {
19121da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1913cd841605SFred Isaman 					     sizeof(struct nfs_write_header),
19141da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
191520c2df83SPaul Mundt 					     NULL);
19161da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
19171da177e4SLinus Torvalds 		return -ENOMEM;
19181da177e4SLinus Torvalds 
191993d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
19201da177e4SLinus Torvalds 						     nfs_wdata_cachep);
19211da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
19223dd4765fSJeff Layton 		goto out_destroy_write_cache;
19231da177e4SLinus Torvalds 
19240b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
19250b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
19260b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
19270b7c0153SFred Isaman 					     NULL);
19280b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
19293dd4765fSJeff Layton 		goto out_destroy_write_mempool;
19300b7c0153SFred Isaman 
193193d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
19324c100210SYanchuan Nian 						      nfs_cdata_cachep);
19331da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
19343dd4765fSJeff Layton 		goto out_destroy_commit_cache;
19351da177e4SLinus Torvalds 
193689a09141SPeter Zijlstra 	/*
193789a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
193889a09141SPeter Zijlstra 	 *
193989a09141SPeter Zijlstra 	 *  64MB:    8192k
194089a09141SPeter Zijlstra 	 * 128MB:   11585k
194189a09141SPeter Zijlstra 	 * 256MB:   16384k
194289a09141SPeter Zijlstra 	 * 512MB:   23170k
194389a09141SPeter Zijlstra 	 *   1GB:   32768k
194489a09141SPeter Zijlstra 	 *   2GB:   46340k
194589a09141SPeter Zijlstra 	 *   4GB:   65536k
194689a09141SPeter Zijlstra 	 *   8GB:   92681k
194789a09141SPeter Zijlstra 	 *  16GB:  131072k
194889a09141SPeter Zijlstra 	 *
194989a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
195089a09141SPeter Zijlstra 	 * Limit the default to 256M
195189a09141SPeter Zijlstra 	 */
195289a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
195389a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
195489a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
195589a09141SPeter Zijlstra 
19561da177e4SLinus Torvalds 	return 0;
19573dd4765fSJeff Layton 
19583dd4765fSJeff Layton out_destroy_commit_cache:
19593dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
19603dd4765fSJeff Layton out_destroy_write_mempool:
19613dd4765fSJeff Layton 	mempool_destroy(nfs_wdata_mempool);
19623dd4765fSJeff Layton out_destroy_write_cache:
19633dd4765fSJeff Layton 	kmem_cache_destroy(nfs_wdata_cachep);
19643dd4765fSJeff Layton 	return -ENOMEM;
19651da177e4SLinus Torvalds }
19661da177e4SLinus Torvalds 
1967266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
19681da177e4SLinus Torvalds {
19691da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
19703dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
19711da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
19721a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
19731da177e4SLinus Torvalds }
19741da177e4SLinus Torvalds 
1975