xref: /linux/fs/nfs/write.c (revision 8c209ce721444a61b61d9e772746c721e4d8d1e8)
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 
341da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #define MIN_POOL_WRITE		(32)
371da177e4SLinus Torvalds #define MIN_POOL_COMMIT		(4)
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds /*
401da177e4SLinus Torvalds  * Local function declarations
411da177e4SLinus Torvalds  */
42f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req);
436c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops;
44788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops;
45061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops;
46f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops;
471da177e4SLinus Torvalds 
48e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep;
493feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
500b7c0153SFred Isaman static struct kmem_cache *nfs_cdata_cachep;
511da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
521da177e4SLinus Torvalds 
530b7c0153SFred Isaman struct nfs_commit_data *nfs_commitdata_alloc(void)
541da177e4SLinus Torvalds {
55192e501bSMel Gorman 	struct nfs_commit_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOIO);
5640859d7eSChuck Lever 
571da177e4SLinus Torvalds 	if (p) {
581da177e4SLinus Torvalds 		memset(p, 0, sizeof(*p));
591da177e4SLinus Torvalds 		INIT_LIST_HEAD(&p->pages);
601da177e4SLinus Torvalds 	}
611da177e4SLinus Torvalds 	return p;
621da177e4SLinus Torvalds }
63e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
641da177e4SLinus Torvalds 
650b7c0153SFred Isaman void nfs_commit_free(struct nfs_commit_data *p)
661da177e4SLinus Torvalds {
671da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
681da177e4SLinus Torvalds }
69e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free);
701da177e4SLinus Torvalds 
716c75dc0dSFred Isaman struct nfs_write_header *nfs_writehdr_alloc(void)
723feb2d49STrond Myklebust {
73192e501bSMel Gorman 	struct nfs_write_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOIO);
743feb2d49STrond Myklebust 
753feb2d49STrond Myklebust 	if (p) {
76cd841605SFred Isaman 		struct nfs_pgio_header *hdr = &p->header;
77cd841605SFred Isaman 
783feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
79cd841605SFred Isaman 		INIT_LIST_HEAD(&hdr->pages);
806c75dc0dSFred Isaman 		INIT_LIST_HEAD(&hdr->rpc_list);
816c75dc0dSFred Isaman 		spin_lock_init(&hdr->lock);
826c75dc0dSFred Isaman 		atomic_set(&hdr->refcnt, 0);
839bce008bSTrond Myklebust 		hdr->verf = &p->verf;
843feb2d49STrond Myklebust 	}
853feb2d49STrond Myklebust 	return p;
863feb2d49STrond Myklebust }
8789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_writehdr_alloc);
883feb2d49STrond Myklebust 
891763da12SFred Isaman static struct nfs_write_data *nfs_writedata_alloc(struct nfs_pgio_header *hdr,
906c75dc0dSFred Isaman 						  unsigned int pagecount)
916c75dc0dSFred Isaman {
926c75dc0dSFred Isaman 	struct nfs_write_data *data, *prealloc;
936c75dc0dSFred Isaman 
946c75dc0dSFred Isaman 	prealloc = &container_of(hdr, struct nfs_write_header, header)->rpc_data;
956c75dc0dSFred Isaman 	if (prealloc->header == NULL)
966c75dc0dSFred Isaman 		data = prealloc;
976c75dc0dSFred Isaman 	else
986c75dc0dSFred Isaman 		data = kzalloc(sizeof(*data), GFP_KERNEL);
996c75dc0dSFred Isaman 	if (!data)
1006c75dc0dSFred Isaman 		goto out;
1016c75dc0dSFred Isaman 
1026c75dc0dSFred Isaman 	if (nfs_pgarray_set(&data->pages, pagecount)) {
1036c75dc0dSFred Isaman 		data->header = hdr;
1046c75dc0dSFred Isaman 		atomic_inc(&hdr->refcnt);
1056c75dc0dSFred Isaman 	} else {
1066c75dc0dSFred Isaman 		if (data != prealloc)
1076c75dc0dSFred Isaman 			kfree(data);
1086c75dc0dSFred Isaman 		data = NULL;
1096c75dc0dSFred Isaman 	}
1106c75dc0dSFred Isaman out:
1116c75dc0dSFred Isaman 	return data;
1126c75dc0dSFred Isaman }
1136c75dc0dSFred Isaman 
114cd841605SFred Isaman void nfs_writehdr_free(struct nfs_pgio_header *hdr)
1153feb2d49STrond Myklebust {
116cd841605SFred Isaman 	struct nfs_write_header *whdr = container_of(hdr, struct nfs_write_header, header);
117cd841605SFred Isaman 	mempool_free(whdr, nfs_wdata_mempool);
1183feb2d49STrond Myklebust }
11989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_writehdr_free);
1203feb2d49STrond Myklebust 
121dce81290STrond Myklebust void nfs_writedata_release(struct nfs_write_data *wdata)
1221da177e4SLinus Torvalds {
1236c75dc0dSFred Isaman 	struct nfs_pgio_header *hdr = wdata->header;
1246c75dc0dSFred Isaman 	struct nfs_write_header *write_header = container_of(hdr, struct nfs_write_header, header);
1256c75dc0dSFred Isaman 
126383ba719STrond Myklebust 	put_nfs_open_context(wdata->args.context);
12730dd374fSFred Isaman 	if (wdata->pages.pagevec != wdata->pages.page_array)
12830dd374fSFred Isaman 		kfree(wdata->pages.pagevec);
1296c75dc0dSFred Isaman 	if (wdata != &write_header->rpc_data)
1306c75dc0dSFred Isaman 		kfree(wdata);
1316c75dc0dSFred Isaman 	else
1326c75dc0dSFred Isaman 		wdata->header = NULL;
1336c75dc0dSFred Isaman 	if (atomic_dec_and_test(&hdr->refcnt))
134061ae2edSFred Isaman 		hdr->completion_ops->completion(hdr);
1351da177e4SLinus Torvalds }
13689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_writedata_release);
1371da177e4SLinus Torvalds 
1387b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
1397b159fc1STrond Myklebust {
1407b159fc1STrond Myklebust 	ctx->error = error;
1417b159fc1STrond Myklebust 	smp_wmb();
1427b159fc1STrond Myklebust 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
1437b159fc1STrond Myklebust }
1447b159fc1STrond Myklebust 
14529418aa4SMel Gorman static struct nfs_page *
14629418aa4SMel Gorman nfs_page_find_request_locked(struct nfs_inode *nfsi, struct page *page)
147277459d2STrond Myklebust {
148277459d2STrond Myklebust 	struct nfs_page *req = NULL;
149277459d2STrond Myklebust 
15029418aa4SMel Gorman 	if (PagePrivate(page))
151277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
15229418aa4SMel Gorman 	else if (unlikely(PageSwapCache(page))) {
15329418aa4SMel Gorman 		struct nfs_page *freq, *t;
15429418aa4SMel Gorman 
15529418aa4SMel Gorman 		/* Linearly search the commit list for the correct req */
15629418aa4SMel Gorman 		list_for_each_entry_safe(freq, t, &nfsi->commit_info.list, wb_list) {
15729418aa4SMel Gorman 			if (freq->wb_page == page) {
15829418aa4SMel Gorman 				req = freq;
15929418aa4SMel Gorman 				break;
160277459d2STrond Myklebust 			}
16129418aa4SMel Gorman 		}
16229418aa4SMel Gorman 	}
16329418aa4SMel Gorman 
16429418aa4SMel Gorman 	if (req)
16529418aa4SMel Gorman 		kref_get(&req->wb_kref);
16629418aa4SMel Gorman 
167277459d2STrond Myklebust 	return req;
168277459d2STrond Myklebust }
169277459d2STrond Myklebust 
170277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page)
171277459d2STrond Myklebust {
172d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
173277459d2STrond Myklebust 	struct nfs_page *req = NULL;
174277459d2STrond Myklebust 
175587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
17629418aa4SMel Gorman 	req = nfs_page_find_request_locked(NFS_I(inode), page);
177587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
178277459d2STrond Myklebust 	return req;
179277459d2STrond Myklebust }
180277459d2STrond Myklebust 
1811da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1821da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1831da177e4SLinus Torvalds {
184d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
185a3d01454STrond Myklebust 	loff_t end, i_size;
186a3d01454STrond Myklebust 	pgoff_t end_index;
1871da177e4SLinus Torvalds 
188a3d01454STrond Myklebust 	spin_lock(&inode->i_lock);
189a3d01454STrond Myklebust 	i_size = i_size_read(inode);
190a3d01454STrond Myklebust 	end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
191d56b4ddfSMel Gorman 	if (i_size > 0 && page_file_index(page) < end_index)
192a3d01454STrond Myklebust 		goto out;
193d56b4ddfSMel Gorman 	end = page_file_offset(page) + ((loff_t)offset+count);
1941da177e4SLinus Torvalds 	if (i_size >= end)
195a3d01454STrond Myklebust 		goto out;
1961da177e4SLinus Torvalds 	i_size_write(inode, end);
197a3d01454STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
198a3d01454STrond Myklebust out:
199a3d01454STrond Myklebust 	spin_unlock(&inode->i_lock);
2001da177e4SLinus Torvalds }
2011da177e4SLinus Torvalds 
202a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
203a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
204a301b777STrond Myklebust {
205d56b4ddfSMel Gorman 	nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page));
206a301b777STrond Myklebust }
207a301b777STrond Myklebust 
2081da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
2091da177e4SLinus Torvalds  * covers the full page.
2101da177e4SLinus Torvalds  */
2111da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
2121da177e4SLinus Torvalds {
2131da177e4SLinus Torvalds 	if (PageUptodate(page))
2141da177e4SLinus Torvalds 		return;
2151da177e4SLinus Torvalds 	if (base != 0)
2161da177e4SLinus Torvalds 		return;
21749a70f27STrond Myklebust 	if (count != nfs_page_length(page))
2181da177e4SLinus Torvalds 		return;
2191da177e4SLinus Torvalds 	SetPageUptodate(page);
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
2231da177e4SLinus Torvalds {
2241da177e4SLinus Torvalds 	if (wbc->for_reclaim)
225c63c7b05STrond Myklebust 		return FLUSH_HIGHPRI | FLUSH_STABLE;
226b17621feSWu Fengguang 	if (wbc->for_kupdate || wbc->for_background)
227b31268acSTrond Myklebust 		return FLUSH_LOWPRI | FLUSH_COND_STABLE;
228b31268acSTrond Myklebust 	return FLUSH_COND_STABLE;
2291da177e4SLinus Torvalds }
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds /*
23289a09141SPeter Zijlstra  * NFS congestion control
23389a09141SPeter Zijlstra  */
23489a09141SPeter Zijlstra 
23589a09141SPeter Zijlstra int nfs_congestion_kb;
23689a09141SPeter Zijlstra 
23789a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
23889a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
23989a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
24089a09141SPeter Zijlstra 
241deed85e7STrond Myklebust static void nfs_set_page_writeback(struct page *page)
24289a09141SPeter Zijlstra {
243deed85e7STrond Myklebust 	struct nfs_server *nfss = NFS_SERVER(page_file_mapping(page)->host);
2445a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
2455a6d41b3STrond Myklebust 
246deed85e7STrond Myklebust 	WARN_ON_ONCE(ret != 0);
24789a09141SPeter Zijlstra 
248277866a0SPeter Zijlstra 	if (atomic_long_inc_return(&nfss->writeback) >
2498aa7e847SJens Axboe 			NFS_CONGESTION_ON_THRESH) {
2508aa7e847SJens Axboe 		set_bdi_congested(&nfss->backing_dev_info,
2518aa7e847SJens Axboe 					BLK_RW_ASYNC);
2528aa7e847SJens Axboe 	}
25389a09141SPeter Zijlstra }
25489a09141SPeter Zijlstra 
25589a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page)
25689a09141SPeter Zijlstra {
257d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
25889a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
25989a09141SPeter Zijlstra 
26089a09141SPeter Zijlstra 	end_page_writeback(page);
261c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
2628aa7e847SJens Axboe 		clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
26389a09141SPeter Zijlstra }
26489a09141SPeter Zijlstra 
265cfb506e1STrond Myklebust static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock)
266e261f51fSTrond Myklebust {
267d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
268e261f51fSTrond Myklebust 	struct nfs_page *req;
269e261f51fSTrond Myklebust 	int ret;
270e261f51fSTrond Myklebust 
271587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
272e261f51fSTrond Myklebust 	for (;;) {
27329418aa4SMel Gorman 		req = nfs_page_find_request_locked(NFS_I(inode), page);
274074cc1deSTrond Myklebust 		if (req == NULL)
275074cc1deSTrond Myklebust 			break;
2767ad84aa9STrond Myklebust 		if (nfs_lock_request(req))
277e261f51fSTrond Myklebust 			break;
278e261f51fSTrond Myklebust 		/* Note: If we hold the page lock, as is the case in nfs_writepage,
2797ad84aa9STrond Myklebust 		 *	 then the call to nfs_lock_request() will always
280e261f51fSTrond Myklebust 		 *	 succeed provided that someone hasn't already marked the
281e261f51fSTrond Myklebust 		 *	 request as dirty (in which case we don't care).
282e261f51fSTrond Myklebust 		 */
283587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
284cfb506e1STrond Myklebust 		if (!nonblock)
285e261f51fSTrond Myklebust 			ret = nfs_wait_on_request(req);
286cfb506e1STrond Myklebust 		else
287cfb506e1STrond Myklebust 			ret = -EAGAIN;
288e261f51fSTrond Myklebust 		nfs_release_request(req);
289e261f51fSTrond Myklebust 		if (ret != 0)
290074cc1deSTrond Myklebust 			return ERR_PTR(ret);
291587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
292e261f51fSTrond Myklebust 	}
293587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
294074cc1deSTrond Myklebust 	return req;
295612c9384STrond Myklebust }
296074cc1deSTrond Myklebust 
297074cc1deSTrond Myklebust /*
298074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
299074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
300074cc1deSTrond Myklebust  */
301074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
302cfb506e1STrond Myklebust 				struct page *page, bool nonblock)
303074cc1deSTrond Myklebust {
304074cc1deSTrond Myklebust 	struct nfs_page *req;
305074cc1deSTrond Myklebust 	int ret = 0;
306074cc1deSTrond Myklebust 
307cfb506e1STrond Myklebust 	req = nfs_find_and_lock_request(page, nonblock);
308074cc1deSTrond Myklebust 	if (!req)
309074cc1deSTrond Myklebust 		goto out;
310074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
311074cc1deSTrond Myklebust 	if (IS_ERR(req))
312074cc1deSTrond Myklebust 		goto out;
313074cc1deSTrond Myklebust 
314deed85e7STrond Myklebust 	nfs_set_page_writeback(page);
315deed85e7STrond Myklebust 	WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
316074cc1deSTrond Myklebust 
317deed85e7STrond Myklebust 	ret = 0;
318f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
319f8512ad0SFred Isaman 		nfs_redirty_request(req);
320074cc1deSTrond Myklebust 		ret = pgio->pg_error;
321f8512ad0SFred Isaman 	}
322074cc1deSTrond Myklebust out:
323074cc1deSTrond Myklebust 	return ret;
324e261f51fSTrond Myklebust }
325e261f51fSTrond Myklebust 
326f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
327f758c885STrond Myklebust {
328d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
329cfb506e1STrond Myklebust 	int ret;
330f758c885STrond Myklebust 
331f758c885STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
332f758c885STrond Myklebust 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
333f758c885STrond Myklebust 
334d56b4ddfSMel Gorman 	nfs_pageio_cond_complete(pgio, page_file_index(page));
3351b430beeSWu Fengguang 	ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
336cfb506e1STrond Myklebust 	if (ret == -EAGAIN) {
337cfb506e1STrond Myklebust 		redirty_page_for_writepage(wbc, page);
338cfb506e1STrond Myklebust 		ret = 0;
339cfb506e1STrond Myklebust 	}
340cfb506e1STrond Myklebust 	return ret;
341f758c885STrond Myklebust }
342f758c885STrond Myklebust 
343e261f51fSTrond Myklebust /*
3441da177e4SLinus Torvalds  * Write an mmapped page to the server.
3451da177e4SLinus Torvalds  */
3464d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
3471da177e4SLinus Torvalds {
348f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
349e261f51fSTrond Myklebust 	int err;
3501da177e4SLinus Torvalds 
351d56b4ddfSMel Gorman 	NFS_PROTO(page_file_mapping(page)->host)->write_pageio_init(&pgio,
35257208fa7SBryan Schumaker 							  page->mapping->host,
35357208fa7SBryan Schumaker 							  wb_priority(wbc),
354061ae2edSFred Isaman 							  &nfs_async_write_completion_ops);
355f758c885STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
356f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
357f758c885STrond Myklebust 	if (err < 0)
3584d770ccfSTrond Myklebust 		return err;
359f758c885STrond Myklebust 	if (pgio.pg_error < 0)
360f758c885STrond Myklebust 		return pgio.pg_error;
361f758c885STrond Myklebust 	return 0;
3624d770ccfSTrond Myklebust }
3634d770ccfSTrond Myklebust 
3644d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
3654d770ccfSTrond Myklebust {
366f758c885STrond Myklebust 	int ret;
3674d770ccfSTrond Myklebust 
368f758c885STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
3691da177e4SLinus Torvalds 	unlock_page(page);
370f758c885STrond Myklebust 	return ret;
371f758c885STrond Myklebust }
372f758c885STrond Myklebust 
373f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
374f758c885STrond Myklebust {
375f758c885STrond Myklebust 	int ret;
376f758c885STrond Myklebust 
377f758c885STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
378f758c885STrond Myklebust 	unlock_page(page);
379f758c885STrond Myklebust 	return ret;
3801da177e4SLinus Torvalds }
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3831da177e4SLinus Torvalds {
3841da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
38572cb77f4STrond Myklebust 	unsigned long *bitlock = &NFS_I(inode)->flags;
386c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
3871da177e4SLinus Torvalds 	int err;
3881da177e4SLinus Torvalds 
38972cb77f4STrond Myklebust 	/* Stop dirtying of new pages while we sync */
39072cb77f4STrond Myklebust 	err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
39172cb77f4STrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
39272cb77f4STrond Myklebust 	if (err)
39372cb77f4STrond Myklebust 		goto out_err;
39472cb77f4STrond Myklebust 
39591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
39691d5b470SChuck Lever 
39757208fa7SBryan Schumaker 	NFS_PROTO(inode)->write_pageio_init(&pgio, inode, wb_priority(wbc), &nfs_async_write_completion_ops);
398f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
399c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
40072cb77f4STrond Myklebust 
40172cb77f4STrond Myklebust 	clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
40272cb77f4STrond Myklebust 	smp_mb__after_clear_bit();
40372cb77f4STrond Myklebust 	wake_up_bit(bitlock, NFS_INO_FLUSHING);
40472cb77f4STrond Myklebust 
405f758c885STrond Myklebust 	if (err < 0)
40672cb77f4STrond Myklebust 		goto out_err;
40772cb77f4STrond Myklebust 	err = pgio.pg_error;
40872cb77f4STrond Myklebust 	if (err < 0)
40972cb77f4STrond Myklebust 		goto out_err;
410c63c7b05STrond Myklebust 	return 0;
41172cb77f4STrond Myklebust out_err:
41272cb77f4STrond Myklebust 	return err;
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds /*
4161da177e4SLinus Torvalds  * Insert a write request into an inode
4171da177e4SLinus Torvalds  */
418d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
4191da177e4SLinus Torvalds {
4201da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
421e7d39069STrond Myklebust 
422e7d39069STrond Myklebust 	/* Lock the request! */
4237ad84aa9STrond Myklebust 	nfs_lock_request(req);
424e7d39069STrond Myklebust 
425e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
426011e2a7fSBryan Schumaker 	if (!nfsi->npages && NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
427a9a4a87aSTrond Myklebust 		inode->i_version++;
42829418aa4SMel Gorman 	/*
42929418aa4SMel Gorman 	 * Swap-space should not get truncated. Hence no need to plug the race
43029418aa4SMel Gorman 	 * with invalidate/truncate.
43129418aa4SMel Gorman 	 */
43229418aa4SMel Gorman 	if (likely(!PageSwapCache(req->wb_page))) {
4332df485a7STrond Myklebust 		set_bit(PG_MAPPED, &req->wb_flags);
434deb7d638STrond Myklebust 		SetPagePrivate(req->wb_page);
435277459d2STrond Myklebust 		set_page_private(req->wb_page, (unsigned long)req);
43629418aa4SMel Gorman 	}
4371da177e4SLinus Torvalds 	nfsi->npages++;
438c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
439e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
4401da177e4SLinus Torvalds }
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds /*
44389a09141SPeter Zijlstra  * Remove a write request from an inode
4441da177e4SLinus Torvalds  */
4451da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
4461da177e4SLinus Torvalds {
4473d4ff43dSAl Viro 	struct inode *inode = req->wb_context->dentry->d_inode;
4481da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4491da177e4SLinus Torvalds 
450587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
45129418aa4SMel Gorman 	if (likely(!PageSwapCache(req->wb_page))) {
452277459d2STrond Myklebust 		set_page_private(req->wb_page, 0);
453deb7d638STrond Myklebust 		ClearPagePrivate(req->wb_page);
4542df485a7STrond Myklebust 		clear_bit(PG_MAPPED, &req->wb_flags);
45529418aa4SMel Gorman 	}
4561da177e4SLinus Torvalds 	nfsi->npages--;
457587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
4581da177e4SLinus Torvalds 	nfs_release_request(req);
4591da177e4SLinus Torvalds }
4601da177e4SLinus Torvalds 
46161822ab5STrond Myklebust static void
4626d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
46361822ab5STrond Myklebust {
46461822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
46561822ab5STrond Myklebust }
46661822ab5STrond Myklebust 
46789d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
4688dd37758STrond Myklebust /**
4698dd37758STrond Myklebust  * nfs_request_add_commit_list - add request to a commit list
4708dd37758STrond Myklebust  * @req: pointer to a struct nfs_page
471ea2cf228SFred Isaman  * @dst: commit list head
472ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
4738dd37758STrond Myklebust  *
474ea2cf228SFred Isaman  * This sets the PG_CLEAN bit, updates the cinfo count of
4758dd37758STrond Myklebust  * number of outstanding requests requiring a commit as well as
4768dd37758STrond Myklebust  * the MM page stats.
4778dd37758STrond Myklebust  *
478ea2cf228SFred Isaman  * The caller must _not_ hold the cinfo->lock, but must be
4798dd37758STrond Myklebust  * holding the nfs_page lock.
4808dd37758STrond Myklebust  */
4818dd37758STrond Myklebust void
482ea2cf228SFred Isaman nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst,
483ea2cf228SFred Isaman 			    struct nfs_commit_info *cinfo)
4848dd37758STrond Myklebust {
4858dd37758STrond Myklebust 	set_bit(PG_CLEAN, &(req)->wb_flags);
486ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
487ea2cf228SFred Isaman 	nfs_list_add_request(req, dst);
488ea2cf228SFred Isaman 	cinfo->mds->ncommit++;
489ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
49056f9cd68SFred Isaman 	if (!cinfo->dreq) {
4918dd37758STrond Myklebust 		inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
492d56b4ddfSMel Gorman 		inc_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
49356f9cd68SFred Isaman 			     BDI_RECLAIMABLE);
49456f9cd68SFred Isaman 		__mark_inode_dirty(req->wb_context->dentry->d_inode,
49556f9cd68SFred Isaman 				   I_DIRTY_DATASYNC);
49656f9cd68SFred Isaman 	}
4978dd37758STrond Myklebust }
4988dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
4998dd37758STrond Myklebust 
5008dd37758STrond Myklebust /**
5018dd37758STrond Myklebust  * nfs_request_remove_commit_list - Remove request from a commit list
5028dd37758STrond Myklebust  * @req: pointer to a nfs_page
503ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
5048dd37758STrond Myklebust  *
505ea2cf228SFred Isaman  * This clears the PG_CLEAN bit, and updates the cinfo's count of
5068dd37758STrond Myklebust  * number of outstanding requests requiring a commit
5078dd37758STrond Myklebust  * It does not update the MM page stats.
5088dd37758STrond Myklebust  *
509ea2cf228SFred Isaman  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
5108dd37758STrond Myklebust  */
5118dd37758STrond Myklebust void
512ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req,
513ea2cf228SFred Isaman 			       struct nfs_commit_info *cinfo)
5148dd37758STrond Myklebust {
5158dd37758STrond Myklebust 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
5168dd37758STrond Myklebust 		return;
5178dd37758STrond Myklebust 	nfs_list_remove_request(req);
518ea2cf228SFred Isaman 	cinfo->mds->ncommit--;
5198dd37758STrond Myklebust }
5208dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
5218dd37758STrond Myklebust 
522ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
523ea2cf228SFred Isaman 				      struct inode *inode)
524ea2cf228SFred Isaman {
525ea2cf228SFred Isaman 	cinfo->lock = &inode->i_lock;
526ea2cf228SFred Isaman 	cinfo->mds = &NFS_I(inode)->commit_info;
527ea2cf228SFred Isaman 	cinfo->ds = pnfs_get_ds_info(inode);
528b359f9d0SFred Isaman 	cinfo->dreq = NULL;
529f453a54aSFred Isaman 	cinfo->completion_ops = &nfs_commit_completion_ops;
530ea2cf228SFred Isaman }
531ea2cf228SFred Isaman 
532ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
533ea2cf228SFred Isaman 		    struct inode *inode,
534ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq)
535ea2cf228SFred Isaman {
5361763da12SFred Isaman 	if (dreq)
5371763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
5381763da12SFred Isaman 	else
539ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(cinfo, inode);
540ea2cf228SFred Isaman }
541ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo);
5428dd37758STrond Myklebust 
5431da177e4SLinus Torvalds /*
5441da177e4SLinus Torvalds  * Add a request to the inode's commit list.
5451da177e4SLinus Torvalds  */
5461763da12SFred Isaman void
547ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
548ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
5491da177e4SLinus Torvalds {
550ea2cf228SFred Isaman 	if (pnfs_mark_request_commit(req, lseg, cinfo))
5518dd37758STrond Myklebust 		return;
552ea2cf228SFred Isaman 	nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo);
5531da177e4SLinus Torvalds }
5548e821cadSTrond Myklebust 
555d6d6dc7cSFred Isaman static void
556d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page)
557e468bae9STrond Myklebust {
558e468bae9STrond Myklebust 	dec_zone_page_state(page, NR_UNSTABLE_NFS);
559d56b4ddfSMel Gorman 	dec_bdi_stat(page_file_mapping(page)->backing_dev_info, BDI_RECLAIMABLE);
560e468bae9STrond Myklebust }
561d6d6dc7cSFred Isaman 
5628dd37758STrond Myklebust static void
563d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req)
564d6d6dc7cSFred Isaman {
5658dd37758STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
5668dd37758STrond Myklebust 		struct inode *inode = req->wb_context->dentry->d_inode;
567ea2cf228SFred Isaman 		struct nfs_commit_info cinfo;
568d6d6dc7cSFred Isaman 
569ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(&cinfo, inode);
570ea2cf228SFred Isaman 		if (!pnfs_clear_request_commit(req, &cinfo)) {
571ea2cf228SFred Isaman 			spin_lock(cinfo.lock);
572ea2cf228SFred Isaman 			nfs_request_remove_commit_list(req, &cinfo);
573ea2cf228SFred Isaman 			spin_unlock(cinfo.lock);
574d6d6dc7cSFred Isaman 		}
5758dd37758STrond Myklebust 		nfs_clear_page_commit(req->wb_page);
5768dd37758STrond Myklebust 	}
577e468bae9STrond Myklebust }
578e468bae9STrond Myklebust 
5798e821cadSTrond Myklebust static inline
5808e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
5818e821cadSTrond Myklebust {
582465d5243SFred Isaman 	if (data->verf.committed == NFS_DATA_SYNC)
583cd841605SFred Isaman 		return data->header->lseg == NULL;
5848e821cadSTrond Myklebust 	return data->verf.committed != NFS_FILE_SYNC;
5858e821cadSTrond Myklebust }
5868e821cadSTrond Myklebust 
5878e821cadSTrond Myklebust #else
58868cd6fa4SBryan Schumaker static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
58968cd6fa4SBryan Schumaker 				      struct inode *inode)
59068cd6fa4SBryan Schumaker {
59168cd6fa4SBryan Schumaker }
59268cd6fa4SBryan Schumaker 
59368cd6fa4SBryan Schumaker void nfs_init_cinfo(struct nfs_commit_info *cinfo,
59468cd6fa4SBryan Schumaker 		    struct inode *inode,
59568cd6fa4SBryan Schumaker 		    struct nfs_direct_req *dreq)
59668cd6fa4SBryan Schumaker {
59768cd6fa4SBryan Schumaker }
59868cd6fa4SBryan Schumaker 
5991763da12SFred Isaman void
600ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
601ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
6028e821cadSTrond Myklebust {
6038e821cadSTrond Myklebust }
6048e821cadSTrond Myklebust 
6058dd37758STrond Myklebust static void
606e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
607e468bae9STrond Myklebust {
608e468bae9STrond Myklebust }
609e468bae9STrond Myklebust 
6108e821cadSTrond Myklebust static inline
6118e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
6128e821cadSTrond Myklebust {
6138e821cadSTrond Myklebust 	return 0;
6148e821cadSTrond Myklebust }
6158e821cadSTrond Myklebust 
6161da177e4SLinus Torvalds #endif
6171da177e4SLinus Torvalds 
618061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
6196c75dc0dSFred Isaman {
620ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
6216c75dc0dSFred Isaman 	unsigned long bytes = 0;
6226c75dc0dSFred Isaman 
6236c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
6246c75dc0dSFred Isaman 		goto out;
625ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
6266c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
6276c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
6286c75dc0dSFred Isaman 
6296c75dc0dSFred Isaman 		bytes += req->wb_bytes;
6306c75dc0dSFred Isaman 		nfs_list_remove_request(req);
6316c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
6326c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
633d1182b33STrond Myklebust 			nfs_set_pageerror(req->wb_page);
6346c75dc0dSFred Isaman 			nfs_context_set_write_error(req->wb_context, hdr->error);
6356c75dc0dSFred Isaman 			goto remove_req;
6366c75dc0dSFred Isaman 		}
6376c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
6386c75dc0dSFred Isaman 			nfs_mark_request_dirty(req);
6396c75dc0dSFred Isaman 			goto next;
6406c75dc0dSFred Isaman 		}
6416c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
6422f2c63bcSTrond Myklebust 			memcpy(&req->wb_verf, &hdr->verf->verifier, sizeof(req->wb_verf));
643ea2cf228SFred Isaman 			nfs_mark_request_commit(req, hdr->lseg, &cinfo);
6446c75dc0dSFred Isaman 			goto next;
6456c75dc0dSFred Isaman 		}
6466c75dc0dSFred Isaman remove_req:
6476c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
6486c75dc0dSFred Isaman next:
6491d1afcbcSTrond Myklebust 		nfs_unlock_request(req);
650d1182b33STrond Myklebust 		nfs_end_page_writeback(req->wb_page);
6513aff4ebbSTrond Myklebust 		nfs_release_request(req);
6526c75dc0dSFred Isaman 	}
6536c75dc0dSFred Isaman out:
6546c75dc0dSFred Isaman 	hdr->release(hdr);
6556c75dc0dSFred Isaman }
6566c75dc0dSFred Isaman 
65789d77c8fSBryan Schumaker #if  IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
658ea2cf228SFred Isaman static unsigned long
659ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
660fb8a1f11STrond Myklebust {
661ea2cf228SFred Isaman 	return cinfo->mds->ncommit;
662fb8a1f11STrond Myklebust }
663fb8a1f11STrond Myklebust 
664ea2cf228SFred Isaman /* cinfo->lock held by caller */
6651763da12SFred Isaman int
666ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
667ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
668d6d6dc7cSFred Isaman {
669d6d6dc7cSFred Isaman 	struct nfs_page *req, *tmp;
670d6d6dc7cSFred Isaman 	int ret = 0;
671d6d6dc7cSFred Isaman 
672d6d6dc7cSFred Isaman 	list_for_each_entry_safe(req, tmp, src, wb_list) {
6738dd37758STrond Myklebust 		if (!nfs_lock_request(req))
6748dd37758STrond Myklebust 			continue;
6757ad84aa9STrond Myklebust 		kref_get(&req->wb_kref);
676ea2cf228SFred Isaman 		if (cond_resched_lock(cinfo->lock))
6773b3be88dSTrond Myklebust 			list_safe_reset_next(req, tmp, wb_list);
678ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
6798dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
680d6d6dc7cSFred Isaman 		ret++;
6811763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
682d6d6dc7cSFred Isaman 			break;
683d6d6dc7cSFred Isaman 	}
684d6d6dc7cSFred Isaman 	return ret;
685d6d6dc7cSFred Isaman }
686d6d6dc7cSFred Isaman 
6871da177e4SLinus Torvalds /*
6881da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
6891da177e4SLinus Torvalds  * @inode: NFS inode to scan
690ea2cf228SFred Isaman  * @dst: mds destination list
691ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
6921da177e4SLinus Torvalds  *
6931da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
6941da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
6951da177e4SLinus Torvalds  */
6961763da12SFred Isaman int
697ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
698ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
6991da177e4SLinus Torvalds {
700d6d6dc7cSFred Isaman 	int ret = 0;
701fb8a1f11STrond Myklebust 
702ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
703ea2cf228SFred Isaman 	if (cinfo->mds->ncommit > 0) {
7048dd37758STrond Myklebust 		const int max = INT_MAX;
705d6d6dc7cSFred Isaman 
706ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
707ea2cf228SFred Isaman 					   cinfo, max);
708ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
709d6d6dc7cSFred Isaman 	}
710ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
711ff778d02STrond Myklebust 	return ret;
7121da177e4SLinus Torvalds }
713d6d6dc7cSFred Isaman 
714c42de9ddSTrond Myklebust #else
715ea2cf228SFred Isaman static unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
716fb8a1f11STrond Myklebust {
717fb8a1f11STrond Myklebust 	return 0;
718fb8a1f11STrond Myklebust }
719fb8a1f11STrond Myklebust 
7201763da12SFred Isaman int nfs_scan_commit(struct inode *inode, struct list_head *dst,
721ea2cf228SFred Isaman 		    struct nfs_commit_info *cinfo)
722c42de9ddSTrond Myklebust {
723c42de9ddSTrond Myklebust 	return 0;
724c42de9ddSTrond Myklebust }
7251da177e4SLinus Torvalds #endif
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds /*
728e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
729e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
7301da177e4SLinus Torvalds  *
731e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
732e7d39069STrond Myklebust  * to disk.
7331da177e4SLinus Torvalds  */
734e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
735e7d39069STrond Myklebust 		struct page *page,
736e7d39069STrond Myklebust 		unsigned int offset,
737e7d39069STrond Myklebust 		unsigned int bytes)
7381da177e4SLinus Torvalds {
739e7d39069STrond Myklebust 	struct nfs_page *req;
740e7d39069STrond Myklebust 	unsigned int rqend;
741e7d39069STrond Myklebust 	unsigned int end;
7421da177e4SLinus Torvalds 	int error;
743277459d2STrond Myklebust 
744e7d39069STrond Myklebust 	if (!PagePrivate(page))
745e7d39069STrond Myklebust 		return NULL;
746e7d39069STrond Myklebust 
747e7d39069STrond Myklebust 	end = offset + bytes;
748e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
749e7d39069STrond Myklebust 
750e7d39069STrond Myklebust 	for (;;) {
75129418aa4SMel Gorman 		req = nfs_page_find_request_locked(NFS_I(inode), page);
752e7d39069STrond Myklebust 		if (req == NULL)
753e7d39069STrond Myklebust 			goto out_unlock;
754e7d39069STrond Myklebust 
755e7d39069STrond Myklebust 		rqend = req->wb_offset + req->wb_bytes;
756e7d39069STrond Myklebust 		/*
757e7d39069STrond Myklebust 		 * Tell the caller to flush out the request if
758e7d39069STrond Myklebust 		 * the offsets are non-contiguous.
759e7d39069STrond Myklebust 		 * Note: nfs_flush_incompatible() will already
760e7d39069STrond Myklebust 		 * have flushed out requests having wrong owners.
761e7d39069STrond Myklebust 		 */
762e468bae9STrond Myklebust 		if (offset > rqend
763e7d39069STrond Myklebust 		    || end < req->wb_offset)
764e7d39069STrond Myklebust 			goto out_flushme;
765e7d39069STrond Myklebust 
7667ad84aa9STrond Myklebust 		if (nfs_lock_request(req))
767e7d39069STrond Myklebust 			break;
768e7d39069STrond Myklebust 
769e7d39069STrond Myklebust 		/* The request is locked, so wait and then retry */
770587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
7711da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
7721da177e4SLinus Torvalds 		nfs_release_request(req);
773e7d39069STrond Myklebust 		if (error != 0)
774e7d39069STrond Myklebust 			goto out_err;
775e7d39069STrond Myklebust 		spin_lock(&inode->i_lock);
7761da177e4SLinus Torvalds 	}
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
7791da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
7801da177e4SLinus Torvalds 		req->wb_offset = offset;
7811da177e4SLinus Torvalds 		req->wb_pgbase = offset;
7821da177e4SLinus Torvalds 	}
7831da177e4SLinus Torvalds 	if (end > rqend)
7841da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
785e7d39069STrond Myklebust 	else
786e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
787e7d39069STrond Myklebust out_unlock:
788e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
789ca138f36SFred Isaman 	if (req)
7908dd37758STrond Myklebust 		nfs_clear_request_commit(req);
791e7d39069STrond Myklebust 	return req;
792e7d39069STrond Myklebust out_flushme:
793e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
794e7d39069STrond Myklebust 	nfs_release_request(req);
795e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
796e7d39069STrond Myklebust out_err:
797e7d39069STrond Myklebust 	return ERR_PTR(error);
798e7d39069STrond Myklebust }
7991da177e4SLinus Torvalds 
800e7d39069STrond Myklebust /*
801e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
802e7d39069STrond Myklebust  *
803e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
804e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
805e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
806e7d39069STrond Myklebust  */
807e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
808e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
809e7d39069STrond Myklebust {
810d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
811e7d39069STrond Myklebust 	struct nfs_page	*req;
812e7d39069STrond Myklebust 
813e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
814e7d39069STrond Myklebust 	if (req != NULL)
815e7d39069STrond Myklebust 		goto out;
816e7d39069STrond Myklebust 	req = nfs_create_request(ctx, inode, page, offset, bytes);
817e7d39069STrond Myklebust 	if (IS_ERR(req))
818e7d39069STrond Myklebust 		goto out;
819d6d6dc7cSFred Isaman 	nfs_inode_add_request(inode, req);
820efc91ed0STrond Myklebust out:
82161e930a9STrond Myklebust 	return req;
8221da177e4SLinus Torvalds }
8231da177e4SLinus Torvalds 
824e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
825e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
826e7d39069STrond Myklebust {
827e7d39069STrond Myklebust 	struct nfs_page	*req;
828e7d39069STrond Myklebust 
829e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
830e7d39069STrond Myklebust 	if (IS_ERR(req))
831e7d39069STrond Myklebust 		return PTR_ERR(req);
832e7d39069STrond Myklebust 	/* Update file length */
833e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
834e7d39069STrond Myklebust 	nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
835a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
8361d1afcbcSTrond Myklebust 	nfs_unlock_and_release_request(req);
837e7d39069STrond Myklebust 	return 0;
838e7d39069STrond Myklebust }
839e7d39069STrond Myklebust 
8401da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
8411da177e4SLinus Torvalds {
842cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
8432a369153STrond Myklebust 	struct nfs_lock_context *l_ctx;
8441da177e4SLinus Torvalds 	struct nfs_page	*req;
8451a54533eSTrond Myklebust 	int do_flush, status;
8461da177e4SLinus Torvalds 	/*
8471da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
8481da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
8491da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
8501da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
8511da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
8521da177e4SLinus Torvalds 	 * dropped page.
8531da177e4SLinus Torvalds 	 */
8541a54533eSTrond Myklebust 	do {
855277459d2STrond Myklebust 		req = nfs_page_find_request(page);
8561a54533eSTrond Myklebust 		if (req == NULL)
8571a54533eSTrond Myklebust 			return 0;
8582a369153STrond Myklebust 		l_ctx = req->wb_lock_context;
8592a369153STrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx;
8602a369153STrond Myklebust 		if (l_ctx) {
8612a369153STrond Myklebust 			do_flush |= l_ctx->lockowner.l_owner != current->files
8622a369153STrond Myklebust 				|| l_ctx->lockowner.l_pid != current->tgid;
8632a369153STrond Myklebust 		}
8641da177e4SLinus Torvalds 		nfs_release_request(req);
8651a54533eSTrond Myklebust 		if (!do_flush)
8661a54533eSTrond Myklebust 			return 0;
867d56b4ddfSMel Gorman 		status = nfs_wb_page(page_file_mapping(page)->host, page);
8681a54533eSTrond Myklebust 	} while (status == 0);
8691a54533eSTrond Myklebust 	return status;
8701da177e4SLinus Torvalds }
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds /*
8735d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
8745d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
8755d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
8765d47a356STrond Myklebust  */
8778d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
8785d47a356STrond Myklebust {
8798d197a56STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
8808d197a56STrond Myklebust 		goto out;
88181d9bce5SJeff Layton 	if (NFS_I(inode)->cache_validity & (NFS_INO_INVALID_DATA|NFS_INO_REVAL_PAGECACHE))
8828d197a56STrond Myklebust 		return false;
8838d197a56STrond Myklebust out:
8848d197a56STrond Myklebust 	return PageUptodate(page) != 0;
8855d47a356STrond Myklebust }
8865d47a356STrond Myklebust 
8875d47a356STrond Myklebust /*
8881da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
8891da177e4SLinus Torvalds  *
8901da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
8911da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
8921da177e4SLinus Torvalds  */
8931da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
8941da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
8951da177e4SLinus Torvalds {
896cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
897d56b4ddfSMel Gorman 	struct inode	*inode = page_file_mapping(page)->host;
8981da177e4SLinus Torvalds 	int		status = 0;
8991da177e4SLinus Torvalds 
90091d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
90191d5b470SChuck Lever 
90248186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage(%s/%s %d@%lld)\n",
90301cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
90401cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name, count,
905d56b4ddfSMel Gorman 		(long long)(page_file_offset(page) + offset));
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	/* If we're not using byte range locks, and we know the page
9085d47a356STrond Myklebust 	 * is up to date, it may be more efficient to extend the write
9095d47a356STrond Myklebust 	 * to cover the entire page in order to avoid fragmentation
9105d47a356STrond Myklebust 	 * inefficiencies.
9111da177e4SLinus Torvalds 	 */
9125d47a356STrond Myklebust 	if (nfs_write_pageuptodate(page, inode) &&
9135d47a356STrond Myklebust 			inode->i_flock == NULL &&
9146b2f3d1fSChristoph Hellwig 			!(file->f_flags & O_DSYNC)) {
91549a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
9161da177e4SLinus Torvalds 		offset = 0;
9171da177e4SLinus Torvalds 	}
9181da177e4SLinus Torvalds 
919e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
92003fa9e84STrond Myklebust 	if (status < 0)
92103fa9e84STrond Myklebust 		nfs_set_pageerror(page);
92259b7c05fSTrond Myklebust 	else
92359b7c05fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
9241da177e4SLinus Torvalds 
92548186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
9261da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
9271da177e4SLinus Torvalds 	return status;
9281da177e4SLinus Torvalds }
9291da177e4SLinus Torvalds 
9303ff7576dSTrond Myklebust static int flush_task_priority(int how)
9311da177e4SLinus Torvalds {
9321da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
9331da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
9341da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
9351da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
9361da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
9371da177e4SLinus Torvalds 	}
9381da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
9391da177e4SLinus Torvalds }
9401da177e4SLinus Torvalds 
941c5996c4eSFred Isaman int nfs_initiate_write(struct rpc_clnt *clnt,
942c5996c4eSFred Isaman 		       struct nfs_write_data *data,
943788e7a89STrond Myklebust 		       const struct rpc_call_ops *call_ops,
9449f0ec176SAndy Adamson 		       int how, int flags)
9451da177e4SLinus Torvalds {
946cd841605SFred Isaman 	struct inode *inode = data->header->inode;
9473ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
94807737691STrond Myklebust 	struct rpc_task *task;
949bdc7f021STrond Myklebust 	struct rpc_message msg = {
950bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
951bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
952cd841605SFred Isaman 		.rpc_cred = data->header->cred,
953bdc7f021STrond Myklebust 	};
95484115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
955d138d5d1SAndy Adamson 		.rpc_client = clnt,
95607737691STrond Myklebust 		.task = &data->task,
957bdc7f021STrond Myklebust 		.rpc_message = &msg,
95884115e1cSTrond Myklebust 		.callback_ops = call_ops,
95984115e1cSTrond Myklebust 		.callback_data = data,
960101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
9619f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
9623ff7576dSTrond Myklebust 		.priority = priority,
96384115e1cSTrond Myklebust 	};
9642c61be0aSTrond Myklebust 	int ret = 0;
9651da177e4SLinus Torvalds 
966d138d5d1SAndy Adamson 	/* Set up the initial task struct.  */
967d138d5d1SAndy Adamson 	NFS_PROTO(inode)->write_setup(data, &msg);
968d138d5d1SAndy Adamson 
969d138d5d1SAndy Adamson 	dprintk("NFS: %5u initiated write call "
970d138d5d1SAndy Adamson 		"(req %s/%lld, %u bytes @ offset %llu)\n",
971d138d5d1SAndy Adamson 		data->task.tk_pid,
972d138d5d1SAndy Adamson 		inode->i_sb->s_id,
973d138d5d1SAndy Adamson 		(long long)NFS_FILEID(inode),
974d138d5d1SAndy Adamson 		data->args.count,
975d138d5d1SAndy Adamson 		(unsigned long long)data->args.offset);
976d138d5d1SAndy Adamson 
977d138d5d1SAndy Adamson 	task = rpc_run_task(&task_setup_data);
978d138d5d1SAndy Adamson 	if (IS_ERR(task)) {
979d138d5d1SAndy Adamson 		ret = PTR_ERR(task);
980d138d5d1SAndy Adamson 		goto out;
981d138d5d1SAndy Adamson 	}
982d138d5d1SAndy Adamson 	if (how & FLUSH_SYNC) {
983d138d5d1SAndy Adamson 		ret = rpc_wait_for_completion_task(task);
984d138d5d1SAndy Adamson 		if (ret == 0)
985d138d5d1SAndy Adamson 			ret = task->tk_status;
986d138d5d1SAndy Adamson 	}
987d138d5d1SAndy Adamson 	rpc_put_task(task);
988d138d5d1SAndy Adamson out:
989d138d5d1SAndy Adamson 	return ret;
990d138d5d1SAndy Adamson }
991a69aef14SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_write);
992d138d5d1SAndy Adamson 
993d138d5d1SAndy Adamson /*
994d138d5d1SAndy Adamson  * Set up the argument/result storage required for the RPC call.
995d138d5d1SAndy Adamson  */
9966c75dc0dSFred Isaman static void nfs_write_rpcsetup(struct nfs_write_data *data,
997d138d5d1SAndy Adamson 		unsigned int count, unsigned int offset,
998ea2cf228SFred Isaman 		int how, struct nfs_commit_info *cinfo)
999d138d5d1SAndy Adamson {
10006c75dc0dSFred Isaman 	struct nfs_page *req = data->header->req;
1001d138d5d1SAndy Adamson 
10021da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
10031da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
10041da177e4SLinus Torvalds 
10056c75dc0dSFred Isaman 	data->args.fh     = NFS_FH(data->header->inode);
10061da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
10072bea038cSBoaz Harrosh 	/* pnfs_set_layoutcommit needs this */
10082bea038cSBoaz Harrosh 	data->mds_offset = data->args.offset;
10091da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
101030dd374fSFred Isaman 	data->args.pages  = data->pages.pagevec;
10111da177e4SLinus Torvalds 	data->args.count  = count;
1012383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
1013f11ac8dbSTrond Myklebust 	data->args.lock_context = req->wb_lock_context;
1014bdc7f021STrond Myklebust 	data->args.stable  = NFS_UNSTABLE;
101587ed5eb4STrond Myklebust 	switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
101687ed5eb4STrond Myklebust 	case 0:
101787ed5eb4STrond Myklebust 		break;
101887ed5eb4STrond Myklebust 	case FLUSH_COND_STABLE:
1019ea2cf228SFred Isaman 		if (nfs_reqs_to_commit(cinfo))
102087ed5eb4STrond Myklebust 			break;
102187ed5eb4STrond Myklebust 	default:
1022bdc7f021STrond Myklebust 		data->args.stable = NFS_FILE_SYNC;
1023bdc7f021STrond Myklebust 	}
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
10261da177e4SLinus Torvalds 	data->res.count   = count;
10271da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
10280e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
10296e4efd56STrond Myklebust }
10301da177e4SLinus Torvalds 
10316e4efd56STrond Myklebust static int nfs_do_write(struct nfs_write_data *data,
10326e4efd56STrond Myklebust 		const struct rpc_call_ops *call_ops,
10336e4efd56STrond Myklebust 		int how)
10346e4efd56STrond Myklebust {
1035cd841605SFred Isaman 	struct inode *inode = data->header->inode;
10360382b744SAndy Adamson 
10379f0ec176SAndy Adamson 	return nfs_initiate_write(NFS_CLIENT(inode), data, call_ops, how, 0);
10381da177e4SLinus Torvalds }
10391da177e4SLinus Torvalds 
1040275acaafSTrond Myklebust static int nfs_do_multiple_writes(struct list_head *head,
1041275acaafSTrond Myklebust 		const struct rpc_call_ops *call_ops,
1042275acaafSTrond Myklebust 		int how)
1043275acaafSTrond Myklebust {
1044275acaafSTrond Myklebust 	struct nfs_write_data *data;
1045275acaafSTrond Myklebust 	int ret = 0;
1046275acaafSTrond Myklebust 
1047275acaafSTrond Myklebust 	while (!list_empty(head)) {
1048275acaafSTrond Myklebust 		int ret2;
1049275acaafSTrond Myklebust 
10506c75dc0dSFred Isaman 		data = list_first_entry(head, struct nfs_write_data, list);
1051275acaafSTrond Myklebust 		list_del_init(&data->list);
1052275acaafSTrond Myklebust 
1053dce81290STrond Myklebust 		ret2 = nfs_do_write(data, call_ops, how);
1054275acaafSTrond Myklebust 		 if (ret == 0)
1055275acaafSTrond Myklebust 			 ret = ret2;
1056275acaafSTrond Myklebust 	}
1057275acaafSTrond Myklebust 	return ret;
1058275acaafSTrond Myklebust }
1059275acaafSTrond Myklebust 
10606d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
10616d884e8fSFred  * call this on each, which will prepare them to be retried on next
10626d884e8fSFred  * writeback using standard nfs.
10636d884e8fSFred  */
10646d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
10656d884e8fSFred {
10666d884e8fSFred 	nfs_mark_request_dirty(req);
10671d1afcbcSTrond Myklebust 	nfs_unlock_request(req);
1068d1182b33STrond Myklebust 	nfs_end_page_writeback(req->wb_page);
10693aff4ebbSTrond Myklebust 	nfs_release_request(req);
10706d884e8fSFred }
10716d884e8fSFred 
1072061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head)
10736c75dc0dSFred Isaman {
10746c75dc0dSFred Isaman 	struct nfs_page	*req;
10756c75dc0dSFred Isaman 
10766c75dc0dSFred Isaman 	while (!list_empty(head)) {
10776c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
10786c75dc0dSFred Isaman 		nfs_list_remove_request(req);
10796c75dc0dSFred Isaman 		nfs_redirty_request(req);
10806c75dc0dSFred Isaman 	}
10816c75dc0dSFred Isaman }
10826c75dc0dSFred Isaman 
1083061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1084061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1085061ae2edSFred Isaman 	.completion = nfs_write_completion,
1086061ae2edSFred Isaman };
1087061ae2edSFred Isaman 
108825b11dcdSTrond Myklebust static void nfs_flush_error(struct nfs_pageio_descriptor *desc,
108925b11dcdSTrond Myklebust 		struct nfs_pgio_header *hdr)
109025b11dcdSTrond Myklebust {
109125b11dcdSTrond Myklebust 	set_bit(NFS_IOHDR_REDO, &hdr->flags);
109225b11dcdSTrond Myklebust 	while (!list_empty(&hdr->rpc_list)) {
109325b11dcdSTrond Myklebust 		struct nfs_write_data *data = list_first_entry(&hdr->rpc_list,
109425b11dcdSTrond Myklebust 				struct nfs_write_data, list);
109525b11dcdSTrond Myklebust 		list_del(&data->list);
109625b11dcdSTrond Myklebust 		nfs_writedata_release(data);
109725b11dcdSTrond Myklebust 	}
109825b11dcdSTrond Myklebust 	desc->pg_completion_ops->error_cleanup(&desc->pg_list);
109925b11dcdSTrond Myklebust }
110025b11dcdSTrond Myklebust 
11011da177e4SLinus Torvalds /*
11021da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
11031da177e4SLinus Torvalds  * contiguous dirty area on one page.
11041da177e4SLinus Torvalds  */
11056c75dc0dSFred Isaman static int nfs_flush_multi(struct nfs_pageio_descriptor *desc,
11066c75dc0dSFred Isaman 			   struct nfs_pgio_header *hdr)
11071da177e4SLinus Torvalds {
11086c75dc0dSFred Isaman 	struct nfs_page *req = hdr->req;
11091da177e4SLinus Torvalds 	struct page *page = req->wb_page;
11101da177e4SLinus Torvalds 	struct nfs_write_data *data;
1111d097971dSTrond Myklebust 	size_t wsize = desc->pg_bsize, nbytes;
1112e9f7bee1STrond Myklebust 	unsigned int offset;
11131da177e4SLinus Torvalds 	int requests = 0;
1114ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
11151da177e4SLinus Torvalds 
1116ea2cf228SFred Isaman 	nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
11171da177e4SLinus Torvalds 
1118b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1119ea2cf228SFred Isaman 	    (desc->pg_moreio || nfs_reqs_to_commit(&cinfo) ||
1120b31268acSTrond Myklebust 	     desc->pg_count > wsize))
1121b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1122b31268acSTrond Myklebust 
1123b31268acSTrond Myklebust 
1124275acaafSTrond Myklebust 	offset = 0;
1125c76069bdSFred Isaman 	nbytes = desc->pg_count;
1126e9f7bee1STrond Myklebust 	do {
1127e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
1128e9f7bee1STrond Myklebust 
11296c75dc0dSFred Isaman 		data = nfs_writedata_alloc(hdr, 1);
113025b11dcdSTrond Myklebust 		if (!data) {
113125b11dcdSTrond Myklebust 			nfs_flush_error(desc, hdr);
113225b11dcdSTrond Myklebust 			return -ENOMEM;
113325b11dcdSTrond Myklebust 		}
113430dd374fSFred Isaman 		data->pages.pagevec[0] = page;
1135ea2cf228SFred Isaman 		nfs_write_rpcsetup(data, len, offset, desc->pg_ioflags, &cinfo);
11366c75dc0dSFred Isaman 		list_add(&data->list, &hdr->rpc_list);
11371da177e4SLinus Torvalds 		requests++;
1138e9f7bee1STrond Myklebust 		nbytes -= len;
1139275acaafSTrond Myklebust 		offset += len;
1140e9f7bee1STrond Myklebust 	} while (nbytes != 0);
114125b11dcdSTrond Myklebust 	nfs_list_remove_request(req);
114225b11dcdSTrond Myklebust 	nfs_list_add_request(req, &hdr->pages);
11436c75dc0dSFred Isaman 	desc->pg_rpc_callops = &nfs_write_common_ops;
114425b11dcdSTrond Myklebust 	return 0;
11451da177e4SLinus Torvalds }
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds /*
11481da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
11491da177e4SLinus Torvalds  * The page must have been locked by the caller.
11501da177e4SLinus Torvalds  *
11511da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
11521da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
11531da177e4SLinus Torvalds  * that has been written but not committed.
11541da177e4SLinus Torvalds  */
11556c75dc0dSFred Isaman static int nfs_flush_one(struct nfs_pageio_descriptor *desc,
11566c75dc0dSFred Isaman 			 struct nfs_pgio_header *hdr)
11571da177e4SLinus Torvalds {
11581da177e4SLinus Torvalds 	struct nfs_page		*req;
11591da177e4SLinus Torvalds 	struct page		**pages;
11601da177e4SLinus Torvalds 	struct nfs_write_data	*data;
1161c76069bdSFred Isaman 	struct list_head *head = &desc->pg_list;
1162ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
11631da177e4SLinus Torvalds 
11646c75dc0dSFred Isaman 	data = nfs_writedata_alloc(hdr, nfs_page_array_len(desc->pg_base,
1165c76069bdSFred Isaman 							   desc->pg_count));
11666c75dc0dSFred Isaman 	if (!data) {
116725b11dcdSTrond Myklebust 		nfs_flush_error(desc, hdr);
116825b11dcdSTrond Myklebust 		return -ENOMEM;
116944b83799SFred Isaman 	}
11706c75dc0dSFred Isaman 
1171ea2cf228SFred Isaman 	nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
117230dd374fSFred Isaman 	pages = data->pages.pagevec;
11731da177e4SLinus Torvalds 	while (!list_empty(head)) {
11741da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
11751da177e4SLinus Torvalds 		nfs_list_remove_request(req);
11766c75dc0dSFred Isaman 		nfs_list_add_request(req, &hdr->pages);
11771da177e4SLinus Torvalds 		*pages++ = req->wb_page;
11781da177e4SLinus Torvalds 	}
11791da177e4SLinus Torvalds 
1180b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1181ea2cf228SFred Isaman 	    (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
1182b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1183b31268acSTrond Myklebust 
11841da177e4SLinus Torvalds 	/* Set up the argument struct */
1185ea2cf228SFred Isaman 	nfs_write_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo);
11866c75dc0dSFred Isaman 	list_add(&data->list, &hdr->rpc_list);
11876c75dc0dSFred Isaman 	desc->pg_rpc_callops = &nfs_write_common_ops;
118825b11dcdSTrond Myklebust 	return 0;
11891da177e4SLinus Torvalds }
11901da177e4SLinus Torvalds 
11916c75dc0dSFred Isaman int nfs_generic_flush(struct nfs_pageio_descriptor *desc,
11926c75dc0dSFred Isaman 		      struct nfs_pgio_header *hdr)
1193dce81290STrond Myklebust {
1194dce81290STrond Myklebust 	if (desc->pg_bsize < PAGE_CACHE_SIZE)
11956c75dc0dSFred Isaman 		return nfs_flush_multi(desc, hdr);
11966c75dc0dSFred Isaman 	return nfs_flush_one(desc, hdr);
1197dce81290STrond Myklebust }
119889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_generic_flush);
1199dce81290STrond Myklebust 
1200dce81290STrond Myklebust static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
12011751c363STrond Myklebust {
12026c75dc0dSFred Isaman 	struct nfs_write_header *whdr;
12036c75dc0dSFred Isaman 	struct nfs_pgio_header *hdr;
1204275acaafSTrond Myklebust 	int ret;
1205275acaafSTrond Myklebust 
12066c75dc0dSFred Isaman 	whdr = nfs_writehdr_alloc();
12076c75dc0dSFred Isaman 	if (!whdr) {
12089b5415b5STrond Myklebust 		desc->pg_completion_ops->error_cleanup(&desc->pg_list);
12096c75dc0dSFred Isaman 		return -ENOMEM;
12106c75dc0dSFred Isaman 	}
12116c75dc0dSFred Isaman 	hdr = &whdr->header;
12126c75dc0dSFred Isaman 	nfs_pgheader_init(desc, hdr, nfs_writehdr_free);
12136c75dc0dSFred Isaman 	atomic_inc(&hdr->refcnt);
12146c75dc0dSFred Isaman 	ret = nfs_generic_flush(desc, hdr);
1215275acaafSTrond Myklebust 	if (ret == 0)
12166c75dc0dSFred Isaman 		ret = nfs_do_multiple_writes(&hdr->rpc_list,
12176c75dc0dSFred Isaman 					     desc->pg_rpc_callops,
1218dce81290STrond Myklebust 					     desc->pg_ioflags);
12196c75dc0dSFred Isaman 	if (atomic_dec_and_test(&hdr->refcnt))
1220061ae2edSFred Isaman 		hdr->completion_ops->completion(hdr);
1221275acaafSTrond Myklebust 	return ret;
12221751c363STrond Myklebust }
12231751c363STrond Myklebust 
12241751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_write_ops = {
12251751c363STrond Myklebust 	.pg_test = nfs_generic_pg_test,
12261751c363STrond Myklebust 	.pg_doio = nfs_generic_pg_writepages,
12271751c363STrond Myklebust };
12281751c363STrond Myklebust 
122957208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1230061ae2edSFred Isaman 			       struct inode *inode, int ioflags,
1231061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
12321751c363STrond Myklebust {
1233061ae2edSFred Isaman 	nfs_pageio_init(pgio, inode, &nfs_pageio_write_ops, compl_ops,
12341751c363STrond Myklebust 				NFS_SERVER(inode)->wsize, ioflags);
12351751c363STrond Myklebust }
1236ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
12371751c363STrond Myklebust 
1238dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1239dce81290STrond Myklebust {
1240dce81290STrond Myklebust 	pgio->pg_ops = &nfs_pageio_write_ops;
1241dce81290STrond Myklebust 	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1242dce81290STrond Myklebust }
12431f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1244dce81290STrond Myklebust 
12451da177e4SLinus Torvalds 
1246def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata)
1247def6ed7eSAndy Adamson {
1248def6ed7eSAndy Adamson 	struct nfs_write_data *data = calldata;
1249cd841605SFred Isaman 	NFS_PROTO(data->header->inode)->write_rpc_prepare(task, data);
1250def6ed7eSAndy Adamson }
1251def6ed7eSAndy Adamson 
12520b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
12530b7c0153SFred Isaman {
12540b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
12550b7c0153SFred Isaman 
12560b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
12570b7c0153SFred Isaman }
12580b7c0153SFred Isaman 
12591da177e4SLinus Torvalds /*
12601da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
12611da177e4SLinus Torvalds  *
12621da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
12631da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
12641da177e4SLinus Torvalds  *	  as the page has a write request pending.
12651da177e4SLinus Torvalds  */
12666c75dc0dSFred Isaman static void nfs_writeback_done_common(struct rpc_task *task, void *calldata)
12671da177e4SLinus Torvalds {
1268788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
12691da177e4SLinus Torvalds 
1270c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
1271c9d8f89dSTrond Myklebust }
1272c9d8f89dSTrond Myklebust 
12736c75dc0dSFred Isaman static void nfs_writeback_release_common(void *calldata)
1274c9d8f89dSTrond Myklebust {
1275c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1276cd841605SFred Isaman 	struct nfs_pgio_header *hdr = data->header;
1277e2fecb21STrond Myklebust 	int status = data->task.tk_status;
1278788e7a89STrond Myklebust 
12796c75dc0dSFred Isaman 	if ((status >= 0) && nfs_write_need_commit(data)) {
12806c75dc0dSFred Isaman 		spin_lock(&hdr->lock);
12816c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags))
12826c75dc0dSFred Isaman 			; /* Do nothing */
12836c75dc0dSFred Isaman 		else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags))
12849bce008bSTrond Myklebust 			memcpy(hdr->verf, &data->verf, sizeof(*hdr->verf));
12859bce008bSTrond Myklebust 		else if (memcmp(hdr->verf, &data->verf, sizeof(*hdr->verf)))
12866c75dc0dSFred Isaman 			set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags);
12876c75dc0dSFred Isaman 		spin_unlock(&hdr->lock);
12881da177e4SLinus Torvalds 	}
1289cd841605SFred Isaman 	nfs_writedata_release(data);
12901da177e4SLinus Torvalds }
12911da177e4SLinus Torvalds 
12926c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops = {
1293def6ed7eSAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
12946c75dc0dSFred Isaman 	.rpc_call_done = nfs_writeback_done_common,
12956c75dc0dSFred Isaman 	.rpc_release = nfs_writeback_release_common,
1296788e7a89STrond Myklebust };
1297788e7a89STrond Myklebust 
1298788e7a89STrond Myklebust 
12991da177e4SLinus Torvalds /*
13001da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
13011da177e4SLinus Torvalds  */
130213602896SFred Isaman void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
13031da177e4SLinus Torvalds {
13041da177e4SLinus Torvalds 	struct nfs_writeargs	*argp = &data->args;
13051da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
1306cd841605SFred Isaman 	struct inode		*inode = data->header->inode;
1307788e7a89STrond Myklebust 	int status;
13081da177e4SLinus Torvalds 
1309a3f565b1SChuck Lever 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
13101da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
13111da177e4SLinus Torvalds 
1312f551e44fSChuck Lever 	/*
1313f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1314f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1315f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1316f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1317f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1318f551e44fSChuck Lever 	 */
1319cd841605SFred Isaman 	status = NFS_PROTO(inode)->write_done(task, data);
1320788e7a89STrond Myklebust 	if (status != 0)
132113602896SFred Isaman 		return;
1322cd841605SFred Isaman 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
132391d5b470SChuck Lever 
132489d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
13251da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
13261da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
13271da177e4SLinus Torvalds 		 * commit data to stable storage even though we
13281da177e4SLinus Torvalds 		 * requested it.
13291da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
13301da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
13311da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
13321da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
13331da177e4SLinus Torvalds 		 */
13341da177e4SLinus Torvalds 		static unsigned long    complain;
13351da177e4SLinus Torvalds 
1336a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
13371da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
13381da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
13391da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1340cd841605SFred Isaman 				NFS_SERVER(inode)->nfs_client->cl_hostname,
13411da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
13421da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
13431da177e4SLinus Torvalds 		}
13441da177e4SLinus Torvalds 	}
13451da177e4SLinus Torvalds #endif
13466c75dc0dSFred Isaman 	if (task->tk_status < 0)
13476c75dc0dSFred Isaman 		nfs_set_pgio_error(data->header, task->tk_status, argp->offset);
13486c75dc0dSFred Isaman 	else if (resp->count < argp->count) {
13491da177e4SLinus Torvalds 		static unsigned long    complain;
13501da177e4SLinus Torvalds 
13516c75dc0dSFred Isaman 		/* This a short write! */
1352cd841605SFred Isaman 		nfs_inc_stats(inode, NFSIOS_SHORTWRITE);
135391d5b470SChuck Lever 
13541da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
13556c75dc0dSFred Isaman 		if (resp->count == 0) {
13566c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
13576c75dc0dSFred Isaman 				printk(KERN_WARNING
13586c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
13596c75dc0dSFred Isaman 				       argp->count);
13606c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
13616c75dc0dSFred Isaman 			}
13626c75dc0dSFred Isaman 			nfs_set_pgio_error(data->header, -EIO, argp->offset);
13636c75dc0dSFred Isaman 			task->tk_status = -EIO;
13646c75dc0dSFred Isaman 			return;
13656c75dc0dSFred Isaman 		}
13661da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
13671da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
13681da177e4SLinus Torvalds 			/* Resend from where the server left off */
1369a69aef14SFred Isaman 			data->mds_offset += resp->count;
13701da177e4SLinus Torvalds 			argp->offset += resp->count;
13711da177e4SLinus Torvalds 			argp->pgbase += resp->count;
13721da177e4SLinus Torvalds 			argp->count -= resp->count;
13731da177e4SLinus Torvalds 		} else {
13741da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
13751da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
13761da177e4SLinus Torvalds 			 */
13771da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
13781da177e4SLinus Torvalds 		}
1379d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
13801da177e4SLinus Torvalds 	}
13811da177e4SLinus Torvalds }
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds 
138489d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
138571d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
138671d0a611STrond Myklebust {
1387b8413f98STrond Myklebust 	int ret;
1388b8413f98STrond Myklebust 
138971d0a611STrond Myklebust 	if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
139071d0a611STrond Myklebust 		return 1;
1391b8413f98STrond Myklebust 	if (!may_wait)
139271d0a611STrond Myklebust 		return 0;
1393b8413f98STrond Myklebust 	ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1394b8413f98STrond Myklebust 				NFS_INO_COMMIT,
1395b8413f98STrond Myklebust 				nfs_wait_bit_killable,
1396b8413f98STrond Myklebust 				TASK_KILLABLE);
1397b8413f98STrond Myklebust 	return (ret < 0) ? ret : 1;
139871d0a611STrond Myklebust }
139971d0a611STrond Myklebust 
1400f453a54aSFred Isaman static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
140171d0a611STrond Myklebust {
140271d0a611STrond Myklebust 	clear_bit(NFS_INO_COMMIT, &nfsi->flags);
140371d0a611STrond Myklebust 	smp_mb__after_clear_bit();
140471d0a611STrond Myklebust 	wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
140571d0a611STrond Myklebust }
140671d0a611STrond Myklebust 
14070b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
14081da177e4SLinus Torvalds {
14090b7c0153SFred Isaman 	put_nfs_open_context(data->context);
14100b7c0153SFred Isaman 	nfs_commit_free(data);
14111da177e4SLinus Torvalds }
1412e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
14131da177e4SLinus Torvalds 
14140b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
14159ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
14169f0ec176SAndy Adamson 			int how, int flags)
14171da177e4SLinus Torvalds {
141807737691STrond Myklebust 	struct rpc_task *task;
14199ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1420bdc7f021STrond Myklebust 	struct rpc_message msg = {
1421bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1422bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
14239ace33cdSFred Isaman 		.rpc_cred = data->cred,
1424bdc7f021STrond Myklebust 	};
142584115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
142607737691STrond Myklebust 		.task = &data->task,
14279ace33cdSFred Isaman 		.rpc_client = clnt,
1428bdc7f021STrond Myklebust 		.rpc_message = &msg,
14299ace33cdSFred Isaman 		.callback_ops = call_ops,
143084115e1cSTrond Myklebust 		.callback_data = data,
1431101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
14329f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
14333ff7576dSTrond Myklebust 		.priority = priority,
143484115e1cSTrond Myklebust 	};
1435788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
14369ace33cdSFred Isaman 	NFS_PROTO(data->inode)->commit_setup(data, &msg);
14371da177e4SLinus Torvalds 
1438a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1439bdc7f021STrond Myklebust 
144007737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1441dbae4c73STrond Myklebust 	if (IS_ERR(task))
1442dbae4c73STrond Myklebust 		return PTR_ERR(task);
1443d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1444d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
144507737691STrond Myklebust 	rpc_put_task(task);
1446dbae4c73STrond Myklebust 	return 0;
14471da177e4SLinus Torvalds }
1448e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
14491da177e4SLinus Torvalds 
14501da177e4SLinus Torvalds /*
14519ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
14529ace33cdSFred Isaman  */
14530b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1454988b6dceSFred Isaman 		     struct list_head *head,
1455f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1456f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
14579ace33cdSFred Isaman {
14589ace33cdSFred Isaman 	struct nfs_page *first = nfs_list_entry(head->next);
14593d4ff43dSAl Viro 	struct inode *inode = first->wb_context->dentry->d_inode;
14609ace33cdSFred Isaman 
14619ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
14629ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
14639ace33cdSFred Isaman 
14649ace33cdSFred Isaman 	list_splice_init(head, &data->pages);
14659ace33cdSFred Isaman 
14669ace33cdSFred Isaman 	data->inode	  = inode;
14679ace33cdSFred Isaman 	data->cred	  = first->wb_context->cred;
1468988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
14699ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1470f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1471b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
14729ace33cdSFred Isaman 
14739ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
14749ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
14759ace33cdSFred Isaman 	data->args.offset = 0;
14769ace33cdSFred Isaman 	data->args.count  = 0;
14770b7c0153SFred Isaman 	data->context     = get_nfs_open_context(first->wb_context);
14789ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
14799ace33cdSFred Isaman 	data->res.verf    = &data->verf;
14809ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
14819ace33cdSFred Isaman }
1482e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
14839ace33cdSFred Isaman 
1484e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1485ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1486ea2cf228SFred Isaman 		      struct nfs_commit_info *cinfo)
148764bfeb49SFred Isaman {
148864bfeb49SFred Isaman 	struct nfs_page *req;
148964bfeb49SFred Isaman 
149064bfeb49SFred Isaman 	while (!list_empty(page_list)) {
149164bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
149264bfeb49SFred Isaman 		nfs_list_remove_request(req);
1493ea2cf228SFred Isaman 		nfs_mark_request_commit(req, lseg, cinfo);
149456f9cd68SFred Isaman 		if (!cinfo->dreq) {
149564bfeb49SFred Isaman 			dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1496d56b4ddfSMel Gorman 			dec_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
149764bfeb49SFred Isaman 				     BDI_RECLAIMABLE);
149856f9cd68SFred Isaman 		}
14991d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
150064bfeb49SFred Isaman 	}
150164bfeb49SFred Isaman }
1502e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
150364bfeb49SFred Isaman 
15049ace33cdSFred Isaman /*
15051da177e4SLinus Torvalds  * Commit dirty pages
15061da177e4SLinus Torvalds  */
15071da177e4SLinus Torvalds static int
1508ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1509ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
15101da177e4SLinus Torvalds {
15110b7c0153SFred Isaman 	struct nfs_commit_data	*data;
15121da177e4SLinus Torvalds 
1513c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds 	if (!data)
15161da177e4SLinus Torvalds 		goto out_bad;
15171da177e4SLinus Torvalds 
15181da177e4SLinus Torvalds 	/* Set up the argument struct */
1519f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
1520f453a54aSFred Isaman 	atomic_inc(&cinfo->mds->rpcs_out);
15219f0ec176SAndy Adamson 	return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops,
15229f0ec176SAndy Adamson 				   how, 0);
15231da177e4SLinus Torvalds  out_bad:
1524ea2cf228SFred Isaman 	nfs_retry_commit(head, NULL, cinfo);
1525f453a54aSFred Isaman 	cinfo->completion_ops->error_cleanup(NFS_I(inode));
15261da177e4SLinus Torvalds 	return -ENOMEM;
15271da177e4SLinus Torvalds }
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds /*
15301da177e4SLinus Torvalds  * COMMIT call returned
15311da177e4SLinus Torvalds  */
1532788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
15331da177e4SLinus Torvalds {
15340b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
15351da177e4SLinus Torvalds 
1536a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
15371da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
15381da177e4SLinus Torvalds 
1539788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1540c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
1541c9d8f89dSTrond Myklebust }
1542c9d8f89dSTrond Myklebust 
1543f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1544c9d8f89dSTrond Myklebust {
1545c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1546c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1547f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
1548788e7a89STrond Myklebust 
15491da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
15501da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
15511da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1552d6d6dc7cSFred Isaman 		nfs_clear_page_commit(req->wb_page);
15531da177e4SLinus Torvalds 
155448186c7dSChuck Lever 		dprintk("NFS:       commit (%s/%lld %d@%lld)",
15553d4ff43dSAl Viro 			req->wb_context->dentry->d_sb->s_id,
15563d4ff43dSAl Viro 			(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
15571da177e4SLinus Torvalds 			req->wb_bytes,
15581da177e4SLinus Torvalds 			(long long)req_offset(req));
1559c9d8f89dSTrond Myklebust 		if (status < 0) {
1560c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
15611da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1562c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
15631da177e4SLinus Torvalds 			goto next;
15641da177e4SLinus Torvalds 		}
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
15671da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
15682f2c63bcSTrond Myklebust 		if (!memcmp(&req->wb_verf, &data->verf.verifier, sizeof(req->wb_verf))) {
15691da177e4SLinus Torvalds 			/* We have a match */
15701da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
15711da177e4SLinus Torvalds 			dprintk(" OK\n");
15721da177e4SLinus Torvalds 			goto next;
15731da177e4SLinus Torvalds 		}
15741da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
15751da177e4SLinus Torvalds 		dprintk(" mismatch\n");
15766d884e8fSFred 		nfs_mark_request_dirty(req);
157705990d1bSTrond Myklebust 		set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
15781da177e4SLinus Torvalds 	next:
15791d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
15801da177e4SLinus Torvalds 	}
1581f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1582f453a54aSFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
1583f453a54aSFred Isaman 		nfs_commit_clear_lock(NFS_I(data->inode));
15845917ce84SFred Isaman }
15855917ce84SFred Isaman 
15865917ce84SFred Isaman static void nfs_commit_release(void *calldata)
15875917ce84SFred Isaman {
15880b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
15895917ce84SFred Isaman 
1590f453a54aSFred Isaman 	data->completion_ops->completion(data);
1591c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
15921da177e4SLinus Torvalds }
1593788e7a89STrond Myklebust 
1594788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
15950b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1596788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1597788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1598788e7a89STrond Myklebust };
15991da177e4SLinus Torvalds 
1600f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1601f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1602f453a54aSFred Isaman 	.error_cleanup = nfs_commit_clear_lock,
1603f453a54aSFred Isaman };
1604f453a54aSFred Isaman 
16051763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1606ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
160784c53ab5SFred Isaman {
160884c53ab5SFred Isaman 	int status;
160984c53ab5SFred Isaman 
1610ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
161184c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1612ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
161384c53ab5SFred Isaman 	return status;
161484c53ab5SFred Isaman }
161584c53ab5SFred Isaman 
1616b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
16171da177e4SLinus Torvalds {
16181da177e4SLinus Torvalds 	LIST_HEAD(head);
1619ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
162071d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1621b8413f98STrond Myklebust 	int res;
16221da177e4SLinus Torvalds 
1623b8413f98STrond Myklebust 	res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1624b8413f98STrond Myklebust 	if (res <= 0)
1625c5efa5fcSTrond Myklebust 		goto out_mark_dirty;
1626ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1627ea2cf228SFred Isaman 	res = nfs_scan_commit(inode, &head, &cinfo);
16281da177e4SLinus Torvalds 	if (res) {
1629a861a1e1SFred Isaman 		int error;
1630a861a1e1SFred Isaman 
1631ea2cf228SFred Isaman 		error = nfs_generic_commit_list(inode, &head, how, &cinfo);
16321da177e4SLinus Torvalds 		if (error < 0)
16331da177e4SLinus Torvalds 			return error;
1634b8413f98STrond Myklebust 		if (!may_wait)
1635b8413f98STrond Myklebust 			goto out_mark_dirty;
1636b8413f98STrond Myklebust 		error = wait_on_bit(&NFS_I(inode)->flags,
1637b8413f98STrond Myklebust 				NFS_INO_COMMIT,
163871d0a611STrond Myklebust 				nfs_wait_bit_killable,
163971d0a611STrond Myklebust 				TASK_KILLABLE);
1640b8413f98STrond Myklebust 		if (error < 0)
1641b8413f98STrond Myklebust 			return error;
164271d0a611STrond Myklebust 	} else
164371d0a611STrond Myklebust 		nfs_commit_clear_lock(NFS_I(inode));
1644c5efa5fcSTrond Myklebust 	return res;
1645c5efa5fcSTrond Myklebust 	/* Note: If we exit without ensuring that the commit is complete,
1646c5efa5fcSTrond Myklebust 	 * we must mark the inode as dirty. Otherwise, future calls to
1647c5efa5fcSTrond Myklebust 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1648c5efa5fcSTrond Myklebust 	 * that the data is on the disk.
1649c5efa5fcSTrond Myklebust 	 */
1650c5efa5fcSTrond Myklebust out_mark_dirty:
1651c5efa5fcSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
16521da177e4SLinus Torvalds 	return res;
16531da177e4SLinus Torvalds }
16548fc795f7STrond Myklebust 
16558fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
16568fc795f7STrond Myklebust {
1657420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1658420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1659420e3646STrond Myklebust 	int ret = 0;
16608fc795f7STrond Myklebust 
16613236c3e1SJeff Layton 	/* no commits means nothing needs to be done */
1662ea2cf228SFred Isaman 	if (!nfsi->commit_info.ncommit)
16633236c3e1SJeff Layton 		return ret;
16643236c3e1SJeff Layton 
1665a00dd6c0SJeff Layton 	if (wbc->sync_mode == WB_SYNC_NONE) {
1666a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1667a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1668420e3646STrond Myklebust 		 */
1669ea2cf228SFred Isaman 		if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1))
1670420e3646STrond Myklebust 			goto out_mark_dirty;
1671420e3646STrond Myklebust 
1672a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1673420e3646STrond Myklebust 		flags = 0;
1674a00dd6c0SJeff Layton 	}
1675a00dd6c0SJeff Layton 
1676420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1677420e3646STrond Myklebust 	if (ret >= 0) {
1678420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1679420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1680420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1681420e3646STrond Myklebust 			else
1682420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1683420e3646STrond Myklebust 		}
16848fc795f7STrond Myklebust 		return 0;
1685420e3646STrond Myklebust 	}
1686420e3646STrond Myklebust out_mark_dirty:
16878fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
16888fc795f7STrond Myklebust 	return ret;
16898fc795f7STrond Myklebust }
1690c63c7b05STrond Myklebust #else
16918fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
16928fc795f7STrond Myklebust {
16938fc795f7STrond Myklebust 	return 0;
16948fc795f7STrond Myklebust }
16951da177e4SLinus Torvalds #endif
16961da177e4SLinus Torvalds 
16978fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
16988fc795f7STrond Myklebust {
1699a8d8f02cSBryan Schumaker 	return nfs_commit_unstable_pages(inode, wbc);
1700a8d8f02cSBryan Schumaker }
170189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode);
1702863a3c6cSAndy Adamson 
1703acdc53b2STrond Myklebust /*
1704acdc53b2STrond Myklebust  * flush the inode to disk.
1705acdc53b2STrond Myklebust  */
1706acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
170734901f70STrond Myklebust {
170834901f70STrond Myklebust 	struct writeback_control wbc = {
170972cb77f4STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
171034901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
1711d7fb1207STrond Myklebust 		.range_start = 0,
1712d7fb1207STrond Myklebust 		.range_end = LLONG_MAX,
171334901f70STrond Myklebust 	};
171434901f70STrond Myklebust 
1715acdc53b2STrond Myklebust 	return sync_inode(inode, &wbc);
17161c75950bSTrond Myklebust }
1717ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all);
17181c75950bSTrond Myklebust 
17191b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
17201b3b4a1aSTrond Myklebust {
17211b3b4a1aSTrond Myklebust 	struct nfs_page *req;
17221b3b4a1aSTrond Myklebust 	int ret = 0;
17231b3b4a1aSTrond Myklebust 
17241b3b4a1aSTrond Myklebust 	for (;;) {
1725ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
17261b3b4a1aSTrond Myklebust 		req = nfs_page_find_request(page);
17271b3b4a1aSTrond Myklebust 		if (req == NULL)
17281b3b4a1aSTrond Myklebust 			break;
17297ad84aa9STrond Myklebust 		if (nfs_lock_request(req)) {
17308dd37758STrond Myklebust 			nfs_clear_request_commit(req);
17311b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
17321b3b4a1aSTrond Myklebust 			/*
17331b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
17341b3b4a1aSTrond Myklebust 			 * page as being dirty
17351b3b4a1aSTrond Myklebust 			 */
17361b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
17371d1afcbcSTrond Myklebust 			nfs_unlock_and_release_request(req);
17381b3b4a1aSTrond Myklebust 			break;
17391b3b4a1aSTrond Myklebust 		}
17401b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
1741c9edda71STrond Myklebust 		nfs_release_request(req);
17421b3b4a1aSTrond Myklebust 		if (ret < 0)
1743c988950eSTrond Myklebust 			break;
17441b3b4a1aSTrond Myklebust 	}
17451b3b4a1aSTrond Myklebust 	return ret;
17461b3b4a1aSTrond Myklebust }
17471b3b4a1aSTrond Myklebust 
17481c75950bSTrond Myklebust /*
17491c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
17501c75950bSTrond Myklebust  */
17511c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
17521c75950bSTrond Myklebust {
175329418aa4SMel Gorman 	loff_t range_start = page_file_offset(page);
17547f2f12d9STrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
17557f2f12d9STrond Myklebust 	struct writeback_control wbc = {
17567f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
17577f2f12d9STrond Myklebust 		.nr_to_write = 0,
17587f2f12d9STrond Myklebust 		.range_start = range_start,
17597f2f12d9STrond Myklebust 		.range_end = range_end,
17607f2f12d9STrond Myklebust 	};
17617f2f12d9STrond Myklebust 	int ret;
17627f2f12d9STrond Myklebust 
17630522f6adSTrond Myklebust 	for (;;) {
1764ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
17657f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
17667f2f12d9STrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
17677f2f12d9STrond Myklebust 			if (ret < 0)
17687f2f12d9STrond Myklebust 				goto out_error;
17690522f6adSTrond Myklebust 			continue;
17707f2f12d9STrond Myklebust 		}
17710522f6adSTrond Myklebust 		if (!PagePrivate(page))
17720522f6adSTrond Myklebust 			break;
17730522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
17747f2f12d9STrond Myklebust 		if (ret < 0)
17757f2f12d9STrond Myklebust 			goto out_error;
17767f2f12d9STrond Myklebust 	}
17777f2f12d9STrond Myklebust 	return 0;
17787f2f12d9STrond Myklebust out_error:
17797f2f12d9STrond Myklebust 	return ret;
17801c75950bSTrond Myklebust }
17811c75950bSTrond Myklebust 
1782074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
1783074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1784a6bc32b8SMel Gorman 		struct page *page, enum migrate_mode mode)
1785074cc1deSTrond Myklebust {
17862da95652SJeff Layton 	/*
17872da95652SJeff Layton 	 * If PagePrivate is set, then the page is currently associated with
17882da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
17892da95652SJeff Layton 	 *
17902da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
17912da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
17922da95652SJeff Layton 	 *        the page lock.
17932da95652SJeff Layton 	 */
17942da95652SJeff Layton 	if (PagePrivate(page))
17952da95652SJeff Layton 		return -EBUSY;
1796074cc1deSTrond Myklebust 
1797*8c209ce7SDavid Howells 	if (!nfs_fscache_release_page(page, GFP_KERNEL))
1798*8c209ce7SDavid Howells 		return -EBUSY;
1799074cc1deSTrond Myklebust 
1800a6bc32b8SMel Gorman 	return migrate_page(mapping, newpage, page, mode);
1801074cc1deSTrond Myklebust }
1802074cc1deSTrond Myklebust #endif
1803074cc1deSTrond Myklebust 
1804f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
18051da177e4SLinus Torvalds {
18061da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1807cd841605SFred Isaman 					     sizeof(struct nfs_write_header),
18081da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
180920c2df83SPaul Mundt 					     NULL);
18101da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
18111da177e4SLinus Torvalds 		return -ENOMEM;
18121da177e4SLinus Torvalds 
181393d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
18141da177e4SLinus Torvalds 						     nfs_wdata_cachep);
18151da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
18163dd4765fSJeff Layton 		goto out_destroy_write_cache;
18171da177e4SLinus Torvalds 
18180b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
18190b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
18200b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
18210b7c0153SFred Isaman 					     NULL);
18220b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
18233dd4765fSJeff Layton 		goto out_destroy_write_mempool;
18240b7c0153SFred Isaman 
182593d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
18264c100210SYanchuan Nian 						      nfs_cdata_cachep);
18271da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
18283dd4765fSJeff Layton 		goto out_destroy_commit_cache;
18291da177e4SLinus Torvalds 
183089a09141SPeter Zijlstra 	/*
183189a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
183289a09141SPeter Zijlstra 	 *
183389a09141SPeter Zijlstra 	 *  64MB:    8192k
183489a09141SPeter Zijlstra 	 * 128MB:   11585k
183589a09141SPeter Zijlstra 	 * 256MB:   16384k
183689a09141SPeter Zijlstra 	 * 512MB:   23170k
183789a09141SPeter Zijlstra 	 *   1GB:   32768k
183889a09141SPeter Zijlstra 	 *   2GB:   46340k
183989a09141SPeter Zijlstra 	 *   4GB:   65536k
184089a09141SPeter Zijlstra 	 *   8GB:   92681k
184189a09141SPeter Zijlstra 	 *  16GB:  131072k
184289a09141SPeter Zijlstra 	 *
184389a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
184489a09141SPeter Zijlstra 	 * Limit the default to 256M
184589a09141SPeter Zijlstra 	 */
184689a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
184789a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
184889a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
184989a09141SPeter Zijlstra 
18501da177e4SLinus Torvalds 	return 0;
18513dd4765fSJeff Layton 
18523dd4765fSJeff Layton out_destroy_commit_cache:
18533dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
18543dd4765fSJeff Layton out_destroy_write_mempool:
18553dd4765fSJeff Layton 	mempool_destroy(nfs_wdata_mempool);
18563dd4765fSJeff Layton out_destroy_write_cache:
18573dd4765fSJeff Layton 	kmem_cache_destroy(nfs_wdata_cachep);
18583dd4765fSJeff Layton 	return -ENOMEM;
18591da177e4SLinus Torvalds }
18601da177e4SLinus Torvalds 
1861266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
18621da177e4SLinus Torvalds {
18631da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
18643dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
18651da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
18661a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
18671da177e4SLinus Torvalds }
18681da177e4SLinus Torvalds 
1869