xref: /linux/fs/nfs/write.c (revision 2a369153c82e0c83621b3e71d8f0c53394705bda)
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 {
205a301b777STrond Myklebust 	SetPageError(page);
206d56b4ddfSMel Gorman 	nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page));
207a301b777STrond Myklebust }
208a301b777STrond Myklebust 
2091da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
2101da177e4SLinus Torvalds  * covers the full page.
2111da177e4SLinus Torvalds  */
2121da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
2131da177e4SLinus Torvalds {
2141da177e4SLinus Torvalds 	if (PageUptodate(page))
2151da177e4SLinus Torvalds 		return;
2161da177e4SLinus Torvalds 	if (base != 0)
2171da177e4SLinus Torvalds 		return;
21849a70f27STrond Myklebust 	if (count != nfs_page_length(page))
2191da177e4SLinus Torvalds 		return;
2201da177e4SLinus Torvalds 	SetPageUptodate(page);
2211da177e4SLinus Torvalds }
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
2241da177e4SLinus Torvalds {
2251da177e4SLinus Torvalds 	if (wbc->for_reclaim)
226c63c7b05STrond Myklebust 		return FLUSH_HIGHPRI | FLUSH_STABLE;
227b17621feSWu Fengguang 	if (wbc->for_kupdate || wbc->for_background)
228b31268acSTrond Myklebust 		return FLUSH_LOWPRI | FLUSH_COND_STABLE;
229b31268acSTrond Myklebust 	return FLUSH_COND_STABLE;
2301da177e4SLinus Torvalds }
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds /*
23389a09141SPeter Zijlstra  * NFS congestion control
23489a09141SPeter Zijlstra  */
23589a09141SPeter Zijlstra 
23689a09141SPeter Zijlstra int nfs_congestion_kb;
23789a09141SPeter Zijlstra 
23889a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
23989a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
24089a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
24189a09141SPeter Zijlstra 
2425a6d41b3STrond Myklebust static int nfs_set_page_writeback(struct page *page)
24389a09141SPeter Zijlstra {
2445a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
2455a6d41b3STrond Myklebust 
2465a6d41b3STrond Myklebust 	if (!ret) {
247d56b4ddfSMel Gorman 		struct inode *inode = page_file_mapping(page)->host;
24889a09141SPeter Zijlstra 		struct nfs_server *nfss = NFS_SERVER(inode);
24989a09141SPeter Zijlstra 
250277866a0SPeter Zijlstra 		if (atomic_long_inc_return(&nfss->writeback) >
2518aa7e847SJens Axboe 				NFS_CONGESTION_ON_THRESH) {
2528aa7e847SJens Axboe 			set_bdi_congested(&nfss->backing_dev_info,
2538aa7e847SJens Axboe 						BLK_RW_ASYNC);
2548aa7e847SJens Axboe 		}
25589a09141SPeter Zijlstra 	}
2565a6d41b3STrond Myklebust 	return ret;
25789a09141SPeter Zijlstra }
25889a09141SPeter Zijlstra 
25989a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page)
26089a09141SPeter Zijlstra {
261d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
26289a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
26389a09141SPeter Zijlstra 
26489a09141SPeter Zijlstra 	end_page_writeback(page);
265c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
2668aa7e847SJens Axboe 		clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
26789a09141SPeter Zijlstra }
26889a09141SPeter Zijlstra 
269cfb506e1STrond Myklebust static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock)
270e261f51fSTrond Myklebust {
271d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
272e261f51fSTrond Myklebust 	struct nfs_page *req;
273e261f51fSTrond Myklebust 	int ret;
274e261f51fSTrond Myklebust 
275587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
276e261f51fSTrond Myklebust 	for (;;) {
27729418aa4SMel Gorman 		req = nfs_page_find_request_locked(NFS_I(inode), page);
278074cc1deSTrond Myklebust 		if (req == NULL)
279074cc1deSTrond Myklebust 			break;
2807ad84aa9STrond Myklebust 		if (nfs_lock_request(req))
281e261f51fSTrond Myklebust 			break;
282e261f51fSTrond Myklebust 		/* Note: If we hold the page lock, as is the case in nfs_writepage,
2837ad84aa9STrond Myklebust 		 *	 then the call to nfs_lock_request() will always
284e261f51fSTrond Myklebust 		 *	 succeed provided that someone hasn't already marked the
285e261f51fSTrond Myklebust 		 *	 request as dirty (in which case we don't care).
286e261f51fSTrond Myklebust 		 */
287587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
288cfb506e1STrond Myklebust 		if (!nonblock)
289e261f51fSTrond Myklebust 			ret = nfs_wait_on_request(req);
290cfb506e1STrond Myklebust 		else
291cfb506e1STrond Myklebust 			ret = -EAGAIN;
292e261f51fSTrond Myklebust 		nfs_release_request(req);
293e261f51fSTrond Myklebust 		if (ret != 0)
294074cc1deSTrond Myklebust 			return ERR_PTR(ret);
295587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
296e261f51fSTrond Myklebust 	}
297587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
298074cc1deSTrond Myklebust 	return req;
299612c9384STrond Myklebust }
300074cc1deSTrond Myklebust 
301074cc1deSTrond Myklebust /*
302074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
303074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
304074cc1deSTrond Myklebust  */
305074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
306cfb506e1STrond Myklebust 				struct page *page, bool nonblock)
307074cc1deSTrond Myklebust {
308074cc1deSTrond Myklebust 	struct nfs_page *req;
309074cc1deSTrond Myklebust 	int ret = 0;
310074cc1deSTrond Myklebust 
311cfb506e1STrond Myklebust 	req = nfs_find_and_lock_request(page, nonblock);
312074cc1deSTrond Myklebust 	if (!req)
313074cc1deSTrond Myklebust 		goto out;
314074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
315074cc1deSTrond Myklebust 	if (IS_ERR(req))
316074cc1deSTrond Myklebust 		goto out;
317074cc1deSTrond Myklebust 
318074cc1deSTrond Myklebust 	ret = nfs_set_page_writeback(page);
319074cc1deSTrond Myklebust 	BUG_ON(ret != 0);
320074cc1deSTrond Myklebust 	BUG_ON(test_bit(PG_CLEAN, &req->wb_flags));
321074cc1deSTrond Myklebust 
322f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
323f8512ad0SFred Isaman 		nfs_redirty_request(req);
324074cc1deSTrond Myklebust 		ret = pgio->pg_error;
325f8512ad0SFred Isaman 	}
326074cc1deSTrond Myklebust out:
327074cc1deSTrond Myklebust 	return ret;
328e261f51fSTrond Myklebust }
329e261f51fSTrond Myklebust 
330f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
331f758c885STrond Myklebust {
332d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
333cfb506e1STrond Myklebust 	int ret;
334f758c885STrond Myklebust 
335f758c885STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
336f758c885STrond Myklebust 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
337f758c885STrond Myklebust 
338d56b4ddfSMel Gorman 	nfs_pageio_cond_complete(pgio, page_file_index(page));
3391b430beeSWu Fengguang 	ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
340cfb506e1STrond Myklebust 	if (ret == -EAGAIN) {
341cfb506e1STrond Myklebust 		redirty_page_for_writepage(wbc, page);
342cfb506e1STrond Myklebust 		ret = 0;
343cfb506e1STrond Myklebust 	}
344cfb506e1STrond Myklebust 	return ret;
345f758c885STrond Myklebust }
346f758c885STrond Myklebust 
347e261f51fSTrond Myklebust /*
3481da177e4SLinus Torvalds  * Write an mmapped page to the server.
3491da177e4SLinus Torvalds  */
3504d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
3511da177e4SLinus Torvalds {
352f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
353e261f51fSTrond Myklebust 	int err;
3541da177e4SLinus Torvalds 
355d56b4ddfSMel Gorman 	NFS_PROTO(page_file_mapping(page)->host)->write_pageio_init(&pgio,
35657208fa7SBryan Schumaker 							  page->mapping->host,
35757208fa7SBryan Schumaker 							  wb_priority(wbc),
358061ae2edSFred Isaman 							  &nfs_async_write_completion_ops);
359f758c885STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
360f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
361f758c885STrond Myklebust 	if (err < 0)
3624d770ccfSTrond Myklebust 		return err;
363f758c885STrond Myklebust 	if (pgio.pg_error < 0)
364f758c885STrond Myklebust 		return pgio.pg_error;
365f758c885STrond Myklebust 	return 0;
3664d770ccfSTrond Myklebust }
3674d770ccfSTrond Myklebust 
3684d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
3694d770ccfSTrond Myklebust {
370f758c885STrond Myklebust 	int ret;
3714d770ccfSTrond Myklebust 
372f758c885STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
3731da177e4SLinus Torvalds 	unlock_page(page);
374f758c885STrond Myklebust 	return ret;
375f758c885STrond Myklebust }
376f758c885STrond Myklebust 
377f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
378f758c885STrond Myklebust {
379f758c885STrond Myklebust 	int ret;
380f758c885STrond Myklebust 
381f758c885STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
382f758c885STrond Myklebust 	unlock_page(page);
383f758c885STrond Myklebust 	return ret;
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3871da177e4SLinus Torvalds {
3881da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
38972cb77f4STrond Myklebust 	unsigned long *bitlock = &NFS_I(inode)->flags;
390c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
3911da177e4SLinus Torvalds 	int err;
3921da177e4SLinus Torvalds 
39372cb77f4STrond Myklebust 	/* Stop dirtying of new pages while we sync */
39472cb77f4STrond Myklebust 	err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
39572cb77f4STrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
39672cb77f4STrond Myklebust 	if (err)
39772cb77f4STrond Myklebust 		goto out_err;
39872cb77f4STrond Myklebust 
39991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
40091d5b470SChuck Lever 
40157208fa7SBryan Schumaker 	NFS_PROTO(inode)->write_pageio_init(&pgio, inode, wb_priority(wbc), &nfs_async_write_completion_ops);
402f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
403c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
40472cb77f4STrond Myklebust 
40572cb77f4STrond Myklebust 	clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
40672cb77f4STrond Myklebust 	smp_mb__after_clear_bit();
40772cb77f4STrond Myklebust 	wake_up_bit(bitlock, NFS_INO_FLUSHING);
40872cb77f4STrond Myklebust 
409f758c885STrond Myklebust 	if (err < 0)
41072cb77f4STrond Myklebust 		goto out_err;
41172cb77f4STrond Myklebust 	err = pgio.pg_error;
41272cb77f4STrond Myklebust 	if (err < 0)
41372cb77f4STrond Myklebust 		goto out_err;
414c63c7b05STrond Myklebust 	return 0;
41572cb77f4STrond Myklebust out_err:
41672cb77f4STrond Myklebust 	return err;
4171da177e4SLinus Torvalds }
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds /*
4201da177e4SLinus Torvalds  * Insert a write request into an inode
4211da177e4SLinus Torvalds  */
422d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
4231da177e4SLinus Torvalds {
4241da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
425e7d39069STrond Myklebust 
426e7d39069STrond Myklebust 	/* Lock the request! */
4277ad84aa9STrond Myklebust 	nfs_lock_request(req);
428e7d39069STrond Myklebust 
429e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
430011e2a7fSBryan Schumaker 	if (!nfsi->npages && NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
431a9a4a87aSTrond Myklebust 		inode->i_version++;
43229418aa4SMel Gorman 	/*
43329418aa4SMel Gorman 	 * Swap-space should not get truncated. Hence no need to plug the race
43429418aa4SMel Gorman 	 * with invalidate/truncate.
43529418aa4SMel Gorman 	 */
43629418aa4SMel Gorman 	if (likely(!PageSwapCache(req->wb_page))) {
4372df485a7STrond Myklebust 		set_bit(PG_MAPPED, &req->wb_flags);
438deb7d638STrond Myklebust 		SetPagePrivate(req->wb_page);
439277459d2STrond Myklebust 		set_page_private(req->wb_page, (unsigned long)req);
44029418aa4SMel Gorman 	}
4411da177e4SLinus Torvalds 	nfsi->npages++;
442c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
443e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
4441da177e4SLinus Torvalds }
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds /*
44789a09141SPeter Zijlstra  * Remove a write request from an inode
4481da177e4SLinus Torvalds  */
4491da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
4501da177e4SLinus Torvalds {
4513d4ff43dSAl Viro 	struct inode *inode = req->wb_context->dentry->d_inode;
4521da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	BUG_ON (!NFS_WBACK_BUSY(req));
4551da177e4SLinus Torvalds 
456587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
45729418aa4SMel Gorman 	if (likely(!PageSwapCache(req->wb_page))) {
458277459d2STrond Myklebust 		set_page_private(req->wb_page, 0);
459deb7d638STrond Myklebust 		ClearPagePrivate(req->wb_page);
4602df485a7STrond Myklebust 		clear_bit(PG_MAPPED, &req->wb_flags);
46129418aa4SMel Gorman 	}
4621da177e4SLinus Torvalds 	nfsi->npages--;
463587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
4641da177e4SLinus Torvalds 	nfs_release_request(req);
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds 
46761822ab5STrond Myklebust static void
4686d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
46961822ab5STrond Myklebust {
47061822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
47161822ab5STrond Myklebust }
47261822ab5STrond Myklebust 
47389d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
4748dd37758STrond Myklebust /**
4758dd37758STrond Myklebust  * nfs_request_add_commit_list - add request to a commit list
4768dd37758STrond Myklebust  * @req: pointer to a struct nfs_page
477ea2cf228SFred Isaman  * @dst: commit list head
478ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
4798dd37758STrond Myklebust  *
480ea2cf228SFred Isaman  * This sets the PG_CLEAN bit, updates the cinfo count of
4818dd37758STrond Myklebust  * number of outstanding requests requiring a commit as well as
4828dd37758STrond Myklebust  * the MM page stats.
4838dd37758STrond Myklebust  *
484ea2cf228SFred Isaman  * The caller must _not_ hold the cinfo->lock, but must be
4858dd37758STrond Myklebust  * holding the nfs_page lock.
4868dd37758STrond Myklebust  */
4878dd37758STrond Myklebust void
488ea2cf228SFred Isaman nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst,
489ea2cf228SFred Isaman 			    struct nfs_commit_info *cinfo)
4908dd37758STrond Myklebust {
4918dd37758STrond Myklebust 	set_bit(PG_CLEAN, &(req)->wb_flags);
492ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
493ea2cf228SFred Isaman 	nfs_list_add_request(req, dst);
494ea2cf228SFred Isaman 	cinfo->mds->ncommit++;
495ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
49656f9cd68SFred Isaman 	if (!cinfo->dreq) {
4978dd37758STrond Myklebust 		inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
498d56b4ddfSMel Gorman 		inc_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
49956f9cd68SFred Isaman 			     BDI_RECLAIMABLE);
50056f9cd68SFred Isaman 		__mark_inode_dirty(req->wb_context->dentry->d_inode,
50156f9cd68SFred Isaman 				   I_DIRTY_DATASYNC);
50256f9cd68SFred Isaman 	}
5038dd37758STrond Myklebust }
5048dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
5058dd37758STrond Myklebust 
5068dd37758STrond Myklebust /**
5078dd37758STrond Myklebust  * nfs_request_remove_commit_list - Remove request from a commit list
5088dd37758STrond Myklebust  * @req: pointer to a nfs_page
509ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
5108dd37758STrond Myklebust  *
511ea2cf228SFred Isaman  * This clears the PG_CLEAN bit, and updates the cinfo's count of
5128dd37758STrond Myklebust  * number of outstanding requests requiring a commit
5138dd37758STrond Myklebust  * It does not update the MM page stats.
5148dd37758STrond Myklebust  *
515ea2cf228SFred Isaman  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
5168dd37758STrond Myklebust  */
5178dd37758STrond Myklebust void
518ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req,
519ea2cf228SFred Isaman 			       struct nfs_commit_info *cinfo)
5208dd37758STrond Myklebust {
5218dd37758STrond Myklebust 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
5228dd37758STrond Myklebust 		return;
5238dd37758STrond Myklebust 	nfs_list_remove_request(req);
524ea2cf228SFred Isaman 	cinfo->mds->ncommit--;
5258dd37758STrond Myklebust }
5268dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
5278dd37758STrond Myklebust 
528ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
529ea2cf228SFred Isaman 				      struct inode *inode)
530ea2cf228SFred Isaman {
531ea2cf228SFred Isaman 	cinfo->lock = &inode->i_lock;
532ea2cf228SFred Isaman 	cinfo->mds = &NFS_I(inode)->commit_info;
533ea2cf228SFred Isaman 	cinfo->ds = pnfs_get_ds_info(inode);
534b359f9d0SFred Isaman 	cinfo->dreq = NULL;
535f453a54aSFred Isaman 	cinfo->completion_ops = &nfs_commit_completion_ops;
536ea2cf228SFred Isaman }
537ea2cf228SFred Isaman 
538ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
539ea2cf228SFred Isaman 		    struct inode *inode,
540ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq)
541ea2cf228SFred Isaman {
5421763da12SFred Isaman 	if (dreq)
5431763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
5441763da12SFred Isaman 	else
545ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(cinfo, inode);
546ea2cf228SFred Isaman }
547ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo);
5488dd37758STrond Myklebust 
5491da177e4SLinus Torvalds /*
5501da177e4SLinus Torvalds  * Add a request to the inode's commit list.
5511da177e4SLinus Torvalds  */
5521763da12SFred Isaman void
553ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
554ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
5551da177e4SLinus Torvalds {
556ea2cf228SFred Isaman 	if (pnfs_mark_request_commit(req, lseg, cinfo))
5578dd37758STrond Myklebust 		return;
558ea2cf228SFred Isaman 	nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo);
5591da177e4SLinus Torvalds }
5608e821cadSTrond Myklebust 
561d6d6dc7cSFred Isaman static void
562d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page)
563e468bae9STrond Myklebust {
564e468bae9STrond Myklebust 	dec_zone_page_state(page, NR_UNSTABLE_NFS);
565d56b4ddfSMel Gorman 	dec_bdi_stat(page_file_mapping(page)->backing_dev_info, BDI_RECLAIMABLE);
566e468bae9STrond Myklebust }
567d6d6dc7cSFred Isaman 
5688dd37758STrond Myklebust static void
569d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req)
570d6d6dc7cSFred Isaman {
5718dd37758STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
5728dd37758STrond Myklebust 		struct inode *inode = req->wb_context->dentry->d_inode;
573ea2cf228SFred Isaman 		struct nfs_commit_info cinfo;
574d6d6dc7cSFred Isaman 
575ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(&cinfo, inode);
576ea2cf228SFred Isaman 		if (!pnfs_clear_request_commit(req, &cinfo)) {
577ea2cf228SFred Isaman 			spin_lock(cinfo.lock);
578ea2cf228SFred Isaman 			nfs_request_remove_commit_list(req, &cinfo);
579ea2cf228SFred Isaman 			spin_unlock(cinfo.lock);
580d6d6dc7cSFred Isaman 		}
5818dd37758STrond Myklebust 		nfs_clear_page_commit(req->wb_page);
5828dd37758STrond Myklebust 	}
583e468bae9STrond Myklebust }
584e468bae9STrond Myklebust 
5858e821cadSTrond Myklebust static inline
5868e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
5878e821cadSTrond Myklebust {
588465d5243SFred Isaman 	if (data->verf.committed == NFS_DATA_SYNC)
589cd841605SFred Isaman 		return data->header->lseg == NULL;
5908e821cadSTrond Myklebust 	return data->verf.committed != NFS_FILE_SYNC;
5918e821cadSTrond Myklebust }
5928e821cadSTrond Myklebust 
5938e821cadSTrond Myklebust #else
59468cd6fa4SBryan Schumaker static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
59568cd6fa4SBryan Schumaker 				      struct inode *inode)
59668cd6fa4SBryan Schumaker {
59768cd6fa4SBryan Schumaker }
59868cd6fa4SBryan Schumaker 
59968cd6fa4SBryan Schumaker void nfs_init_cinfo(struct nfs_commit_info *cinfo,
60068cd6fa4SBryan Schumaker 		    struct inode *inode,
60168cd6fa4SBryan Schumaker 		    struct nfs_direct_req *dreq)
60268cd6fa4SBryan Schumaker {
60368cd6fa4SBryan Schumaker }
60468cd6fa4SBryan Schumaker 
6051763da12SFred Isaman void
606ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
607ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
6088e821cadSTrond Myklebust {
6098e821cadSTrond Myklebust }
6108e821cadSTrond Myklebust 
6118dd37758STrond Myklebust static void
612e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
613e468bae9STrond Myklebust {
614e468bae9STrond Myklebust }
615e468bae9STrond Myklebust 
6168e821cadSTrond Myklebust static inline
6178e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
6188e821cadSTrond Myklebust {
6198e821cadSTrond Myklebust 	return 0;
6208e821cadSTrond Myklebust }
6218e821cadSTrond Myklebust 
6221da177e4SLinus Torvalds #endif
6231da177e4SLinus Torvalds 
624061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
6256c75dc0dSFred Isaman {
626ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
6276c75dc0dSFred Isaman 	unsigned long bytes = 0;
6286c75dc0dSFred Isaman 
6296c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
6306c75dc0dSFred Isaman 		goto out;
631ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
6326c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
6336c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
6346c75dc0dSFred Isaman 
6356c75dc0dSFred Isaman 		bytes += req->wb_bytes;
6366c75dc0dSFred Isaman 		nfs_list_remove_request(req);
6376c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
6386c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
639d1182b33STrond Myklebust 			nfs_set_pageerror(req->wb_page);
6406c75dc0dSFred Isaman 			nfs_context_set_write_error(req->wb_context, hdr->error);
6416c75dc0dSFred Isaman 			goto remove_req;
6426c75dc0dSFred Isaman 		}
6436c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
6446c75dc0dSFred Isaman 			nfs_mark_request_dirty(req);
6456c75dc0dSFred Isaman 			goto next;
6466c75dc0dSFred Isaman 		}
6476c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
6482f2c63bcSTrond Myklebust 			memcpy(&req->wb_verf, &hdr->verf->verifier, sizeof(req->wb_verf));
649ea2cf228SFred Isaman 			nfs_mark_request_commit(req, hdr->lseg, &cinfo);
6506c75dc0dSFred Isaman 			goto next;
6516c75dc0dSFred Isaman 		}
6526c75dc0dSFred Isaman remove_req:
6536c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
6546c75dc0dSFred Isaman next:
6551d1afcbcSTrond Myklebust 		nfs_unlock_request(req);
656d1182b33STrond Myklebust 		nfs_end_page_writeback(req->wb_page);
6573aff4ebbSTrond Myklebust 		nfs_release_request(req);
6586c75dc0dSFred Isaman 	}
6596c75dc0dSFred Isaman out:
6606c75dc0dSFred Isaman 	hdr->release(hdr);
6616c75dc0dSFred Isaman }
6626c75dc0dSFred Isaman 
66389d77c8fSBryan Schumaker #if  IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
664ea2cf228SFred Isaman static unsigned long
665ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
666fb8a1f11STrond Myklebust {
667ea2cf228SFred Isaman 	return cinfo->mds->ncommit;
668fb8a1f11STrond Myklebust }
669fb8a1f11STrond Myklebust 
670ea2cf228SFred Isaman /* cinfo->lock held by caller */
6711763da12SFred Isaman int
672ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
673ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
674d6d6dc7cSFred Isaman {
675d6d6dc7cSFred Isaman 	struct nfs_page *req, *tmp;
676d6d6dc7cSFred Isaman 	int ret = 0;
677d6d6dc7cSFred Isaman 
678d6d6dc7cSFred Isaman 	list_for_each_entry_safe(req, tmp, src, wb_list) {
6798dd37758STrond Myklebust 		if (!nfs_lock_request(req))
6808dd37758STrond Myklebust 			continue;
6817ad84aa9STrond Myklebust 		kref_get(&req->wb_kref);
682ea2cf228SFred Isaman 		if (cond_resched_lock(cinfo->lock))
6833b3be88dSTrond Myklebust 			list_safe_reset_next(req, tmp, wb_list);
684ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
6858dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
686d6d6dc7cSFred Isaman 		ret++;
6871763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
688d6d6dc7cSFred Isaman 			break;
689d6d6dc7cSFred Isaman 	}
690d6d6dc7cSFred Isaman 	return ret;
691d6d6dc7cSFred Isaman }
692d6d6dc7cSFred Isaman 
6931da177e4SLinus Torvalds /*
6941da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
6951da177e4SLinus Torvalds  * @inode: NFS inode to scan
696ea2cf228SFred Isaman  * @dst: mds destination list
697ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
6981da177e4SLinus Torvalds  *
6991da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
7001da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
7011da177e4SLinus Torvalds  */
7021763da12SFred Isaman int
703ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
704ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
7051da177e4SLinus Torvalds {
706d6d6dc7cSFred Isaman 	int ret = 0;
707fb8a1f11STrond Myklebust 
708ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
709ea2cf228SFred Isaman 	if (cinfo->mds->ncommit > 0) {
7108dd37758STrond Myklebust 		const int max = INT_MAX;
711d6d6dc7cSFred Isaman 
712ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
713ea2cf228SFred Isaman 					   cinfo, max);
714ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
715d6d6dc7cSFred Isaman 	}
716ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
717ff778d02STrond Myklebust 	return ret;
7181da177e4SLinus Torvalds }
719d6d6dc7cSFred Isaman 
720c42de9ddSTrond Myklebust #else
721ea2cf228SFred Isaman static unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
722fb8a1f11STrond Myklebust {
723fb8a1f11STrond Myklebust 	return 0;
724fb8a1f11STrond Myklebust }
725fb8a1f11STrond Myklebust 
7261763da12SFred Isaman int nfs_scan_commit(struct inode *inode, struct list_head *dst,
727ea2cf228SFred Isaman 		    struct nfs_commit_info *cinfo)
728c42de9ddSTrond Myklebust {
729c42de9ddSTrond Myklebust 	return 0;
730c42de9ddSTrond Myklebust }
7311da177e4SLinus Torvalds #endif
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds /*
734e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
735e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
7361da177e4SLinus Torvalds  *
737e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
738e7d39069STrond Myklebust  * to disk.
7391da177e4SLinus Torvalds  */
740e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
741e7d39069STrond Myklebust 		struct page *page,
742e7d39069STrond Myklebust 		unsigned int offset,
743e7d39069STrond Myklebust 		unsigned int bytes)
7441da177e4SLinus Torvalds {
745e7d39069STrond Myklebust 	struct nfs_page *req;
746e7d39069STrond Myklebust 	unsigned int rqend;
747e7d39069STrond Myklebust 	unsigned int end;
7481da177e4SLinus Torvalds 	int error;
749277459d2STrond Myklebust 
750e7d39069STrond Myklebust 	if (!PagePrivate(page))
751e7d39069STrond Myklebust 		return NULL;
752e7d39069STrond Myklebust 
753e7d39069STrond Myklebust 	end = offset + bytes;
754e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
755e7d39069STrond Myklebust 
756e7d39069STrond Myklebust 	for (;;) {
75729418aa4SMel Gorman 		req = nfs_page_find_request_locked(NFS_I(inode), page);
758e7d39069STrond Myklebust 		if (req == NULL)
759e7d39069STrond Myklebust 			goto out_unlock;
760e7d39069STrond Myklebust 
761e7d39069STrond Myklebust 		rqend = req->wb_offset + req->wb_bytes;
762e7d39069STrond Myklebust 		/*
763e7d39069STrond Myklebust 		 * Tell the caller to flush out the request if
764e7d39069STrond Myklebust 		 * the offsets are non-contiguous.
765e7d39069STrond Myklebust 		 * Note: nfs_flush_incompatible() will already
766e7d39069STrond Myklebust 		 * have flushed out requests having wrong owners.
767e7d39069STrond Myklebust 		 */
768e468bae9STrond Myklebust 		if (offset > rqend
769e7d39069STrond Myklebust 		    || end < req->wb_offset)
770e7d39069STrond Myklebust 			goto out_flushme;
771e7d39069STrond Myklebust 
7727ad84aa9STrond Myklebust 		if (nfs_lock_request(req))
773e7d39069STrond Myklebust 			break;
774e7d39069STrond Myklebust 
775e7d39069STrond Myklebust 		/* The request is locked, so wait and then retry */
776587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
7771da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
7781da177e4SLinus Torvalds 		nfs_release_request(req);
779e7d39069STrond Myklebust 		if (error != 0)
780e7d39069STrond Myklebust 			goto out_err;
781e7d39069STrond Myklebust 		spin_lock(&inode->i_lock);
7821da177e4SLinus Torvalds 	}
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
7851da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
7861da177e4SLinus Torvalds 		req->wb_offset = offset;
7871da177e4SLinus Torvalds 		req->wb_pgbase = offset;
7881da177e4SLinus Torvalds 	}
7891da177e4SLinus Torvalds 	if (end > rqend)
7901da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
791e7d39069STrond Myklebust 	else
792e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
793e7d39069STrond Myklebust out_unlock:
794e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
795ca138f36SFred Isaman 	if (req)
7968dd37758STrond Myklebust 		nfs_clear_request_commit(req);
797e7d39069STrond Myklebust 	return req;
798e7d39069STrond Myklebust out_flushme:
799e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
800e7d39069STrond Myklebust 	nfs_release_request(req);
801e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
802e7d39069STrond Myklebust out_err:
803e7d39069STrond Myklebust 	return ERR_PTR(error);
804e7d39069STrond Myklebust }
8051da177e4SLinus Torvalds 
806e7d39069STrond Myklebust /*
807e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
808e7d39069STrond Myklebust  *
809e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
810e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
811e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
812e7d39069STrond Myklebust  */
813e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
814e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
815e7d39069STrond Myklebust {
816d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
817e7d39069STrond Myklebust 	struct nfs_page	*req;
818e7d39069STrond Myklebust 
819e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
820e7d39069STrond Myklebust 	if (req != NULL)
821e7d39069STrond Myklebust 		goto out;
822e7d39069STrond Myklebust 	req = nfs_create_request(ctx, inode, page, offset, bytes);
823e7d39069STrond Myklebust 	if (IS_ERR(req))
824e7d39069STrond Myklebust 		goto out;
825d6d6dc7cSFred Isaman 	nfs_inode_add_request(inode, req);
826efc91ed0STrond Myklebust out:
82761e930a9STrond Myklebust 	return req;
8281da177e4SLinus Torvalds }
8291da177e4SLinus Torvalds 
830e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
831e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
832e7d39069STrond Myklebust {
833e7d39069STrond Myklebust 	struct nfs_page	*req;
834e7d39069STrond Myklebust 
835e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
836e7d39069STrond Myklebust 	if (IS_ERR(req))
837e7d39069STrond Myklebust 		return PTR_ERR(req);
838e7d39069STrond Myklebust 	/* Update file length */
839e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
840e7d39069STrond Myklebust 	nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
841a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
8421d1afcbcSTrond Myklebust 	nfs_unlock_and_release_request(req);
843e7d39069STrond Myklebust 	return 0;
844e7d39069STrond Myklebust }
845e7d39069STrond Myklebust 
8461da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
8471da177e4SLinus Torvalds {
848cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
849*2a369153STrond Myklebust 	struct nfs_lock_context *l_ctx;
8501da177e4SLinus Torvalds 	struct nfs_page	*req;
8511a54533eSTrond Myklebust 	int do_flush, status;
8521da177e4SLinus Torvalds 	/*
8531da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
8541da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
8551da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
8561da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
8571da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
8581da177e4SLinus Torvalds 	 * dropped page.
8591da177e4SLinus Torvalds 	 */
8601a54533eSTrond Myklebust 	do {
861277459d2STrond Myklebust 		req = nfs_page_find_request(page);
8621a54533eSTrond Myklebust 		if (req == NULL)
8631a54533eSTrond Myklebust 			return 0;
864*2a369153STrond Myklebust 		l_ctx = req->wb_lock_context;
865*2a369153STrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx;
866*2a369153STrond Myklebust 		if (l_ctx) {
867*2a369153STrond Myklebust 			do_flush |= l_ctx->lockowner.l_owner != current->files
868*2a369153STrond Myklebust 				|| l_ctx->lockowner.l_pid != current->tgid;
869*2a369153STrond Myklebust 		}
8701da177e4SLinus Torvalds 		nfs_release_request(req);
8711a54533eSTrond Myklebust 		if (!do_flush)
8721a54533eSTrond Myklebust 			return 0;
873d56b4ddfSMel Gorman 		status = nfs_wb_page(page_file_mapping(page)->host, page);
8741a54533eSTrond Myklebust 	} while (status == 0);
8751a54533eSTrond Myklebust 	return status;
8761da177e4SLinus Torvalds }
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds /*
8795d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
8805d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
8815d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
8825d47a356STrond Myklebust  */
8838d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
8845d47a356STrond Myklebust {
8858d197a56STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
8868d197a56STrond Myklebust 		goto out;
8878d197a56STrond Myklebust 	if (NFS_I(inode)->cache_validity & NFS_INO_REVAL_PAGECACHE)
8888d197a56STrond Myklebust 		return false;
8898d197a56STrond Myklebust out:
8908d197a56STrond Myklebust 	return PageUptodate(page) != 0;
8915d47a356STrond Myklebust }
8925d47a356STrond Myklebust 
8935d47a356STrond Myklebust /*
8941da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
8951da177e4SLinus Torvalds  *
8961da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
8971da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
8981da177e4SLinus Torvalds  */
8991da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
9001da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
9011da177e4SLinus Torvalds {
902cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
903d56b4ddfSMel Gorman 	struct inode	*inode = page_file_mapping(page)->host;
9041da177e4SLinus Torvalds 	int		status = 0;
9051da177e4SLinus Torvalds 
90691d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
90791d5b470SChuck Lever 
90848186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage(%s/%s %d@%lld)\n",
90901cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
91001cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name, count,
911d56b4ddfSMel Gorman 		(long long)(page_file_offset(page) + offset));
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 	/* If we're not using byte range locks, and we know the page
9145d47a356STrond Myklebust 	 * is up to date, it may be more efficient to extend the write
9155d47a356STrond Myklebust 	 * to cover the entire page in order to avoid fragmentation
9165d47a356STrond Myklebust 	 * inefficiencies.
9171da177e4SLinus Torvalds 	 */
9185d47a356STrond Myklebust 	if (nfs_write_pageuptodate(page, inode) &&
9195d47a356STrond Myklebust 			inode->i_flock == NULL &&
9206b2f3d1fSChristoph Hellwig 			!(file->f_flags & O_DSYNC)) {
92149a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
9221da177e4SLinus Torvalds 		offset = 0;
9231da177e4SLinus Torvalds 	}
9241da177e4SLinus Torvalds 
925e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
92603fa9e84STrond Myklebust 	if (status < 0)
92703fa9e84STrond Myklebust 		nfs_set_pageerror(page);
92859b7c05fSTrond Myklebust 	else
92959b7c05fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
9301da177e4SLinus Torvalds 
93148186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
9321da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
9331da177e4SLinus Torvalds 	return status;
9341da177e4SLinus Torvalds }
9351da177e4SLinus Torvalds 
9363ff7576dSTrond Myklebust static int flush_task_priority(int how)
9371da177e4SLinus Torvalds {
9381da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
9391da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
9401da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
9411da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
9421da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
9431da177e4SLinus Torvalds 	}
9441da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
9451da177e4SLinus Torvalds }
9461da177e4SLinus Torvalds 
947c5996c4eSFred Isaman int nfs_initiate_write(struct rpc_clnt *clnt,
948c5996c4eSFred Isaman 		       struct nfs_write_data *data,
949788e7a89STrond Myklebust 		       const struct rpc_call_ops *call_ops,
9509f0ec176SAndy Adamson 		       int how, int flags)
9511da177e4SLinus Torvalds {
952cd841605SFred Isaman 	struct inode *inode = data->header->inode;
9533ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
95407737691STrond Myklebust 	struct rpc_task *task;
955bdc7f021STrond Myklebust 	struct rpc_message msg = {
956bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
957bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
958cd841605SFred Isaman 		.rpc_cred = data->header->cred,
959bdc7f021STrond Myklebust 	};
96084115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
961d138d5d1SAndy Adamson 		.rpc_client = clnt,
96207737691STrond Myklebust 		.task = &data->task,
963bdc7f021STrond Myklebust 		.rpc_message = &msg,
96484115e1cSTrond Myklebust 		.callback_ops = call_ops,
96584115e1cSTrond Myklebust 		.callback_data = data,
966101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
9679f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
9683ff7576dSTrond Myklebust 		.priority = priority,
96984115e1cSTrond Myklebust 	};
9702c61be0aSTrond Myklebust 	int ret = 0;
9711da177e4SLinus Torvalds 
972d138d5d1SAndy Adamson 	/* Set up the initial task struct.  */
973d138d5d1SAndy Adamson 	NFS_PROTO(inode)->write_setup(data, &msg);
974d138d5d1SAndy Adamson 
975d138d5d1SAndy Adamson 	dprintk("NFS: %5u initiated write call "
976d138d5d1SAndy Adamson 		"(req %s/%lld, %u bytes @ offset %llu)\n",
977d138d5d1SAndy Adamson 		data->task.tk_pid,
978d138d5d1SAndy Adamson 		inode->i_sb->s_id,
979d138d5d1SAndy Adamson 		(long long)NFS_FILEID(inode),
980d138d5d1SAndy Adamson 		data->args.count,
981d138d5d1SAndy Adamson 		(unsigned long long)data->args.offset);
982d138d5d1SAndy Adamson 
983d138d5d1SAndy Adamson 	task = rpc_run_task(&task_setup_data);
984d138d5d1SAndy Adamson 	if (IS_ERR(task)) {
985d138d5d1SAndy Adamson 		ret = PTR_ERR(task);
986d138d5d1SAndy Adamson 		goto out;
987d138d5d1SAndy Adamson 	}
988d138d5d1SAndy Adamson 	if (how & FLUSH_SYNC) {
989d138d5d1SAndy Adamson 		ret = rpc_wait_for_completion_task(task);
990d138d5d1SAndy Adamson 		if (ret == 0)
991d138d5d1SAndy Adamson 			ret = task->tk_status;
992d138d5d1SAndy Adamson 	}
993d138d5d1SAndy Adamson 	rpc_put_task(task);
994d138d5d1SAndy Adamson out:
995d138d5d1SAndy Adamson 	return ret;
996d138d5d1SAndy Adamson }
997a69aef14SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_write);
998d138d5d1SAndy Adamson 
999d138d5d1SAndy Adamson /*
1000d138d5d1SAndy Adamson  * Set up the argument/result storage required for the RPC call.
1001d138d5d1SAndy Adamson  */
10026c75dc0dSFred Isaman static void nfs_write_rpcsetup(struct nfs_write_data *data,
1003d138d5d1SAndy Adamson 		unsigned int count, unsigned int offset,
1004ea2cf228SFred Isaman 		int how, struct nfs_commit_info *cinfo)
1005d138d5d1SAndy Adamson {
10066c75dc0dSFred Isaman 	struct nfs_page *req = data->header->req;
1007d138d5d1SAndy Adamson 
10081da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
10091da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
10101da177e4SLinus Torvalds 
10116c75dc0dSFred Isaman 	data->args.fh     = NFS_FH(data->header->inode);
10121da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
10132bea038cSBoaz Harrosh 	/* pnfs_set_layoutcommit needs this */
10142bea038cSBoaz Harrosh 	data->mds_offset = data->args.offset;
10151da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
101630dd374fSFred Isaman 	data->args.pages  = data->pages.pagevec;
10171da177e4SLinus Torvalds 	data->args.count  = count;
1018383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
1019f11ac8dbSTrond Myklebust 	data->args.lock_context = req->wb_lock_context;
1020bdc7f021STrond Myklebust 	data->args.stable  = NFS_UNSTABLE;
102187ed5eb4STrond Myklebust 	switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
102287ed5eb4STrond Myklebust 	case 0:
102387ed5eb4STrond Myklebust 		break;
102487ed5eb4STrond Myklebust 	case FLUSH_COND_STABLE:
1025ea2cf228SFred Isaman 		if (nfs_reqs_to_commit(cinfo))
102687ed5eb4STrond Myklebust 			break;
102787ed5eb4STrond Myklebust 	default:
1028bdc7f021STrond Myklebust 		data->args.stable = NFS_FILE_SYNC;
1029bdc7f021STrond Myklebust 	}
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
10321da177e4SLinus Torvalds 	data->res.count   = count;
10331da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
10340e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
10356e4efd56STrond Myklebust }
10361da177e4SLinus Torvalds 
10376e4efd56STrond Myklebust static int nfs_do_write(struct nfs_write_data *data,
10386e4efd56STrond Myklebust 		const struct rpc_call_ops *call_ops,
10396e4efd56STrond Myklebust 		int how)
10406e4efd56STrond Myklebust {
1041cd841605SFred Isaman 	struct inode *inode = data->header->inode;
10420382b744SAndy Adamson 
10439f0ec176SAndy Adamson 	return nfs_initiate_write(NFS_CLIENT(inode), data, call_ops, how, 0);
10441da177e4SLinus Torvalds }
10451da177e4SLinus Torvalds 
1046275acaafSTrond Myklebust static int nfs_do_multiple_writes(struct list_head *head,
1047275acaafSTrond Myklebust 		const struct rpc_call_ops *call_ops,
1048275acaafSTrond Myklebust 		int how)
1049275acaafSTrond Myklebust {
1050275acaafSTrond Myklebust 	struct nfs_write_data *data;
1051275acaafSTrond Myklebust 	int ret = 0;
1052275acaafSTrond Myklebust 
1053275acaafSTrond Myklebust 	while (!list_empty(head)) {
1054275acaafSTrond Myklebust 		int ret2;
1055275acaafSTrond Myklebust 
10566c75dc0dSFred Isaman 		data = list_first_entry(head, struct nfs_write_data, list);
1057275acaafSTrond Myklebust 		list_del_init(&data->list);
1058275acaafSTrond Myklebust 
1059dce81290STrond Myklebust 		ret2 = nfs_do_write(data, call_ops, how);
1060275acaafSTrond Myklebust 		 if (ret == 0)
1061275acaafSTrond Myklebust 			 ret = ret2;
1062275acaafSTrond Myklebust 	}
1063275acaafSTrond Myklebust 	return ret;
1064275acaafSTrond Myklebust }
1065275acaafSTrond Myklebust 
10666d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
10676d884e8fSFred  * call this on each, which will prepare them to be retried on next
10686d884e8fSFred  * writeback using standard nfs.
10696d884e8fSFred  */
10706d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
10716d884e8fSFred {
10726d884e8fSFred 	nfs_mark_request_dirty(req);
10731d1afcbcSTrond Myklebust 	nfs_unlock_request(req);
1074d1182b33STrond Myklebust 	nfs_end_page_writeback(req->wb_page);
10753aff4ebbSTrond Myklebust 	nfs_release_request(req);
10766d884e8fSFred }
10776d884e8fSFred 
1078061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head)
10796c75dc0dSFred Isaman {
10806c75dc0dSFred Isaman 	struct nfs_page	*req;
10816c75dc0dSFred Isaman 
10826c75dc0dSFred Isaman 	while (!list_empty(head)) {
10836c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
10846c75dc0dSFred Isaman 		nfs_list_remove_request(req);
10856c75dc0dSFred Isaman 		nfs_redirty_request(req);
10866c75dc0dSFred Isaman 	}
10876c75dc0dSFred Isaman }
10886c75dc0dSFred Isaman 
1089061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1090061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1091061ae2edSFred Isaman 	.completion = nfs_write_completion,
1092061ae2edSFred Isaman };
1093061ae2edSFred Isaman 
109425b11dcdSTrond Myklebust static void nfs_flush_error(struct nfs_pageio_descriptor *desc,
109525b11dcdSTrond Myklebust 		struct nfs_pgio_header *hdr)
109625b11dcdSTrond Myklebust {
109725b11dcdSTrond Myklebust 	set_bit(NFS_IOHDR_REDO, &hdr->flags);
109825b11dcdSTrond Myklebust 	while (!list_empty(&hdr->rpc_list)) {
109925b11dcdSTrond Myklebust 		struct nfs_write_data *data = list_first_entry(&hdr->rpc_list,
110025b11dcdSTrond Myklebust 				struct nfs_write_data, list);
110125b11dcdSTrond Myklebust 		list_del(&data->list);
110225b11dcdSTrond Myklebust 		nfs_writedata_release(data);
110325b11dcdSTrond Myklebust 	}
110425b11dcdSTrond Myklebust 	desc->pg_completion_ops->error_cleanup(&desc->pg_list);
110525b11dcdSTrond Myklebust }
110625b11dcdSTrond Myklebust 
11071da177e4SLinus Torvalds /*
11081da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
11091da177e4SLinus Torvalds  * contiguous dirty area on one page.
11101da177e4SLinus Torvalds  */
11116c75dc0dSFred Isaman static int nfs_flush_multi(struct nfs_pageio_descriptor *desc,
11126c75dc0dSFred Isaman 			   struct nfs_pgio_header *hdr)
11131da177e4SLinus Torvalds {
11146c75dc0dSFred Isaman 	struct nfs_page *req = hdr->req;
11151da177e4SLinus Torvalds 	struct page *page = req->wb_page;
11161da177e4SLinus Torvalds 	struct nfs_write_data *data;
1117d097971dSTrond Myklebust 	size_t wsize = desc->pg_bsize, nbytes;
1118e9f7bee1STrond Myklebust 	unsigned int offset;
11191da177e4SLinus Torvalds 	int requests = 0;
1120ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
11211da177e4SLinus Torvalds 
1122ea2cf228SFred Isaman 	nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
11231da177e4SLinus Torvalds 
1124b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1125ea2cf228SFred Isaman 	    (desc->pg_moreio || nfs_reqs_to_commit(&cinfo) ||
1126b31268acSTrond Myklebust 	     desc->pg_count > wsize))
1127b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1128b31268acSTrond Myklebust 
1129b31268acSTrond Myklebust 
1130275acaafSTrond Myklebust 	offset = 0;
1131c76069bdSFred Isaman 	nbytes = desc->pg_count;
1132e9f7bee1STrond Myklebust 	do {
1133e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
1134e9f7bee1STrond Myklebust 
11356c75dc0dSFred Isaman 		data = nfs_writedata_alloc(hdr, 1);
113625b11dcdSTrond Myklebust 		if (!data) {
113725b11dcdSTrond Myklebust 			nfs_flush_error(desc, hdr);
113825b11dcdSTrond Myklebust 			return -ENOMEM;
113925b11dcdSTrond Myklebust 		}
114030dd374fSFred Isaman 		data->pages.pagevec[0] = page;
1141ea2cf228SFred Isaman 		nfs_write_rpcsetup(data, len, offset, desc->pg_ioflags, &cinfo);
11426c75dc0dSFred Isaman 		list_add(&data->list, &hdr->rpc_list);
11431da177e4SLinus Torvalds 		requests++;
1144e9f7bee1STrond Myklebust 		nbytes -= len;
1145275acaafSTrond Myklebust 		offset += len;
1146e9f7bee1STrond Myklebust 	} while (nbytes != 0);
114725b11dcdSTrond Myklebust 	nfs_list_remove_request(req);
114825b11dcdSTrond Myklebust 	nfs_list_add_request(req, &hdr->pages);
11496c75dc0dSFred Isaman 	desc->pg_rpc_callops = &nfs_write_common_ops;
115025b11dcdSTrond Myklebust 	return 0;
11511da177e4SLinus Torvalds }
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds /*
11541da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
11551da177e4SLinus Torvalds  * The page must have been locked by the caller.
11561da177e4SLinus Torvalds  *
11571da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
11581da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
11591da177e4SLinus Torvalds  * that has been written but not committed.
11601da177e4SLinus Torvalds  */
11616c75dc0dSFred Isaman static int nfs_flush_one(struct nfs_pageio_descriptor *desc,
11626c75dc0dSFred Isaman 			 struct nfs_pgio_header *hdr)
11631da177e4SLinus Torvalds {
11641da177e4SLinus Torvalds 	struct nfs_page		*req;
11651da177e4SLinus Torvalds 	struct page		**pages;
11661da177e4SLinus Torvalds 	struct nfs_write_data	*data;
1167c76069bdSFred Isaman 	struct list_head *head = &desc->pg_list;
1168ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
11691da177e4SLinus Torvalds 
11706c75dc0dSFred Isaman 	data = nfs_writedata_alloc(hdr, nfs_page_array_len(desc->pg_base,
1171c76069bdSFred Isaman 							   desc->pg_count));
11726c75dc0dSFred Isaman 	if (!data) {
117325b11dcdSTrond Myklebust 		nfs_flush_error(desc, hdr);
117425b11dcdSTrond Myklebust 		return -ENOMEM;
117544b83799SFred Isaman 	}
11766c75dc0dSFred Isaman 
1177ea2cf228SFred Isaman 	nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
117830dd374fSFred Isaman 	pages = data->pages.pagevec;
11791da177e4SLinus Torvalds 	while (!list_empty(head)) {
11801da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
11811da177e4SLinus Torvalds 		nfs_list_remove_request(req);
11826c75dc0dSFred Isaman 		nfs_list_add_request(req, &hdr->pages);
11831da177e4SLinus Torvalds 		*pages++ = req->wb_page;
11841da177e4SLinus Torvalds 	}
11851da177e4SLinus Torvalds 
1186b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1187ea2cf228SFred Isaman 	    (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
1188b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1189b31268acSTrond Myklebust 
11901da177e4SLinus Torvalds 	/* Set up the argument struct */
1191ea2cf228SFred Isaman 	nfs_write_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo);
11926c75dc0dSFred Isaman 	list_add(&data->list, &hdr->rpc_list);
11936c75dc0dSFred Isaman 	desc->pg_rpc_callops = &nfs_write_common_ops;
119425b11dcdSTrond Myklebust 	return 0;
11951da177e4SLinus Torvalds }
11961da177e4SLinus Torvalds 
11976c75dc0dSFred Isaman int nfs_generic_flush(struct nfs_pageio_descriptor *desc,
11986c75dc0dSFred Isaman 		      struct nfs_pgio_header *hdr)
1199dce81290STrond Myklebust {
1200dce81290STrond Myklebust 	if (desc->pg_bsize < PAGE_CACHE_SIZE)
12016c75dc0dSFred Isaman 		return nfs_flush_multi(desc, hdr);
12026c75dc0dSFred Isaman 	return nfs_flush_one(desc, hdr);
1203dce81290STrond Myklebust }
120489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_generic_flush);
1205dce81290STrond Myklebust 
1206dce81290STrond Myklebust static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
12071751c363STrond Myklebust {
12086c75dc0dSFred Isaman 	struct nfs_write_header *whdr;
12096c75dc0dSFred Isaman 	struct nfs_pgio_header *hdr;
1210275acaafSTrond Myklebust 	int ret;
1211275acaafSTrond Myklebust 
12126c75dc0dSFred Isaman 	whdr = nfs_writehdr_alloc();
12136c75dc0dSFred Isaman 	if (!whdr) {
12149b5415b5STrond Myklebust 		desc->pg_completion_ops->error_cleanup(&desc->pg_list);
12156c75dc0dSFred Isaman 		return -ENOMEM;
12166c75dc0dSFred Isaman 	}
12176c75dc0dSFred Isaman 	hdr = &whdr->header;
12186c75dc0dSFred Isaman 	nfs_pgheader_init(desc, hdr, nfs_writehdr_free);
12196c75dc0dSFred Isaman 	atomic_inc(&hdr->refcnt);
12206c75dc0dSFred Isaman 	ret = nfs_generic_flush(desc, hdr);
1221275acaafSTrond Myklebust 	if (ret == 0)
12226c75dc0dSFred Isaman 		ret = nfs_do_multiple_writes(&hdr->rpc_list,
12236c75dc0dSFred Isaman 					     desc->pg_rpc_callops,
1224dce81290STrond Myklebust 					     desc->pg_ioflags);
12256c75dc0dSFred Isaman 	if (atomic_dec_and_test(&hdr->refcnt))
1226061ae2edSFred Isaman 		hdr->completion_ops->completion(hdr);
1227275acaafSTrond Myklebust 	return ret;
12281751c363STrond Myklebust }
12291751c363STrond Myklebust 
12301751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_write_ops = {
12311751c363STrond Myklebust 	.pg_test = nfs_generic_pg_test,
12321751c363STrond Myklebust 	.pg_doio = nfs_generic_pg_writepages,
12331751c363STrond Myklebust };
12341751c363STrond Myklebust 
123557208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1236061ae2edSFred Isaman 			       struct inode *inode, int ioflags,
1237061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
12381751c363STrond Myklebust {
1239061ae2edSFred Isaman 	nfs_pageio_init(pgio, inode, &nfs_pageio_write_ops, compl_ops,
12401751c363STrond Myklebust 				NFS_SERVER(inode)->wsize, ioflags);
12411751c363STrond Myklebust }
1242ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
12431751c363STrond Myklebust 
1244dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1245dce81290STrond Myklebust {
1246dce81290STrond Myklebust 	pgio->pg_ops = &nfs_pageio_write_ops;
1247dce81290STrond Myklebust 	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1248dce81290STrond Myklebust }
12491f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1250dce81290STrond Myklebust 
12511da177e4SLinus Torvalds 
1252def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata)
1253def6ed7eSAndy Adamson {
1254def6ed7eSAndy Adamson 	struct nfs_write_data *data = calldata;
1255cd841605SFred Isaman 	NFS_PROTO(data->header->inode)->write_rpc_prepare(task, data);
1256def6ed7eSAndy Adamson }
1257def6ed7eSAndy Adamson 
12580b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
12590b7c0153SFred Isaman {
12600b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
12610b7c0153SFred Isaman 
12620b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
12630b7c0153SFred Isaman }
12640b7c0153SFred Isaman 
12651da177e4SLinus Torvalds /*
12661da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
12671da177e4SLinus Torvalds  *
12681da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
12691da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
12701da177e4SLinus Torvalds  *	  as the page has a write request pending.
12711da177e4SLinus Torvalds  */
12726c75dc0dSFred Isaman static void nfs_writeback_done_common(struct rpc_task *task, void *calldata)
12731da177e4SLinus Torvalds {
1274788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
12751da177e4SLinus Torvalds 
1276c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
1277c9d8f89dSTrond Myklebust }
1278c9d8f89dSTrond Myklebust 
12796c75dc0dSFred Isaman static void nfs_writeback_release_common(void *calldata)
1280c9d8f89dSTrond Myklebust {
1281c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1282cd841605SFred Isaman 	struct nfs_pgio_header *hdr = data->header;
1283e2fecb21STrond Myklebust 	int status = data->task.tk_status;
1284788e7a89STrond Myklebust 
12856c75dc0dSFred Isaman 	if ((status >= 0) && nfs_write_need_commit(data)) {
12866c75dc0dSFred Isaman 		spin_lock(&hdr->lock);
12876c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags))
12886c75dc0dSFred Isaman 			; /* Do nothing */
12896c75dc0dSFred Isaman 		else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags))
12909bce008bSTrond Myklebust 			memcpy(hdr->verf, &data->verf, sizeof(*hdr->verf));
12919bce008bSTrond Myklebust 		else if (memcmp(hdr->verf, &data->verf, sizeof(*hdr->verf)))
12926c75dc0dSFred Isaman 			set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags);
12936c75dc0dSFred Isaman 		spin_unlock(&hdr->lock);
12941da177e4SLinus Torvalds 	}
1295cd841605SFred Isaman 	nfs_writedata_release(data);
12961da177e4SLinus Torvalds }
12971da177e4SLinus Torvalds 
12986c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops = {
1299def6ed7eSAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
13006c75dc0dSFred Isaman 	.rpc_call_done = nfs_writeback_done_common,
13016c75dc0dSFred Isaman 	.rpc_release = nfs_writeback_release_common,
1302788e7a89STrond Myklebust };
1303788e7a89STrond Myklebust 
1304788e7a89STrond Myklebust 
13051da177e4SLinus Torvalds /*
13061da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
13071da177e4SLinus Torvalds  */
130813602896SFred Isaman void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
13091da177e4SLinus Torvalds {
13101da177e4SLinus Torvalds 	struct nfs_writeargs	*argp = &data->args;
13111da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
1312cd841605SFred Isaman 	struct inode		*inode = data->header->inode;
1313788e7a89STrond Myklebust 	int status;
13141da177e4SLinus Torvalds 
1315a3f565b1SChuck Lever 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
13161da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
13171da177e4SLinus Torvalds 
1318f551e44fSChuck Lever 	/*
1319f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1320f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1321f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1322f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1323f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1324f551e44fSChuck Lever 	 */
1325cd841605SFred Isaman 	status = NFS_PROTO(inode)->write_done(task, data);
1326788e7a89STrond Myklebust 	if (status != 0)
132713602896SFred Isaman 		return;
1328cd841605SFred Isaman 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
132991d5b470SChuck Lever 
133089d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
13311da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
13321da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
13331da177e4SLinus Torvalds 		 * commit data to stable storage even though we
13341da177e4SLinus Torvalds 		 * requested it.
13351da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
13361da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
13371da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
13381da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
13391da177e4SLinus Torvalds 		 */
13401da177e4SLinus Torvalds 		static unsigned long    complain;
13411da177e4SLinus Torvalds 
1342a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
13431da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
13441da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
13451da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1346cd841605SFred Isaman 				NFS_SERVER(inode)->nfs_client->cl_hostname,
13471da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
13481da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
13491da177e4SLinus Torvalds 		}
13501da177e4SLinus Torvalds 	}
13511da177e4SLinus Torvalds #endif
13526c75dc0dSFred Isaman 	if (task->tk_status < 0)
13536c75dc0dSFred Isaman 		nfs_set_pgio_error(data->header, task->tk_status, argp->offset);
13546c75dc0dSFred Isaman 	else if (resp->count < argp->count) {
13551da177e4SLinus Torvalds 		static unsigned long    complain;
13561da177e4SLinus Torvalds 
13576c75dc0dSFred Isaman 		/* This a short write! */
1358cd841605SFred Isaman 		nfs_inc_stats(inode, NFSIOS_SHORTWRITE);
135991d5b470SChuck Lever 
13601da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
13616c75dc0dSFred Isaman 		if (resp->count == 0) {
13626c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
13636c75dc0dSFred Isaman 				printk(KERN_WARNING
13646c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
13656c75dc0dSFred Isaman 				       argp->count);
13666c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
13676c75dc0dSFred Isaman 			}
13686c75dc0dSFred Isaman 			nfs_set_pgio_error(data->header, -EIO, argp->offset);
13696c75dc0dSFred Isaman 			task->tk_status = -EIO;
13706c75dc0dSFred Isaman 			return;
13716c75dc0dSFred Isaman 		}
13721da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
13731da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
13741da177e4SLinus Torvalds 			/* Resend from where the server left off */
1375a69aef14SFred Isaman 			data->mds_offset += resp->count;
13761da177e4SLinus Torvalds 			argp->offset += resp->count;
13771da177e4SLinus Torvalds 			argp->pgbase += resp->count;
13781da177e4SLinus Torvalds 			argp->count -= resp->count;
13791da177e4SLinus Torvalds 		} else {
13801da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
13811da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
13821da177e4SLinus Torvalds 			 */
13831da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
13841da177e4SLinus Torvalds 		}
1385d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
13861da177e4SLinus Torvalds 	}
13871da177e4SLinus Torvalds }
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds 
139089d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
139171d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
139271d0a611STrond Myklebust {
1393b8413f98STrond Myklebust 	int ret;
1394b8413f98STrond Myklebust 
139571d0a611STrond Myklebust 	if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
139671d0a611STrond Myklebust 		return 1;
1397b8413f98STrond Myklebust 	if (!may_wait)
139871d0a611STrond Myklebust 		return 0;
1399b8413f98STrond Myklebust 	ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1400b8413f98STrond Myklebust 				NFS_INO_COMMIT,
1401b8413f98STrond Myklebust 				nfs_wait_bit_killable,
1402b8413f98STrond Myklebust 				TASK_KILLABLE);
1403b8413f98STrond Myklebust 	return (ret < 0) ? ret : 1;
140471d0a611STrond Myklebust }
140571d0a611STrond Myklebust 
1406f453a54aSFred Isaman static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
140771d0a611STrond Myklebust {
140871d0a611STrond Myklebust 	clear_bit(NFS_INO_COMMIT, &nfsi->flags);
140971d0a611STrond Myklebust 	smp_mb__after_clear_bit();
141071d0a611STrond Myklebust 	wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
141171d0a611STrond Myklebust }
141271d0a611STrond Myklebust 
14130b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
14141da177e4SLinus Torvalds {
14150b7c0153SFred Isaman 	put_nfs_open_context(data->context);
14160b7c0153SFred Isaman 	nfs_commit_free(data);
14171da177e4SLinus Torvalds }
1418e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
14191da177e4SLinus Torvalds 
14200b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
14219ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
14229f0ec176SAndy Adamson 			int how, int flags)
14231da177e4SLinus Torvalds {
142407737691STrond Myklebust 	struct rpc_task *task;
14259ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1426bdc7f021STrond Myklebust 	struct rpc_message msg = {
1427bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1428bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
14299ace33cdSFred Isaman 		.rpc_cred = data->cred,
1430bdc7f021STrond Myklebust 	};
143184115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
143207737691STrond Myklebust 		.task = &data->task,
14339ace33cdSFred Isaman 		.rpc_client = clnt,
1434bdc7f021STrond Myklebust 		.rpc_message = &msg,
14359ace33cdSFred Isaman 		.callback_ops = call_ops,
143684115e1cSTrond Myklebust 		.callback_data = data,
1437101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
14389f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
14393ff7576dSTrond Myklebust 		.priority = priority,
144084115e1cSTrond Myklebust 	};
1441788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
14429ace33cdSFred Isaman 	NFS_PROTO(data->inode)->commit_setup(data, &msg);
14431da177e4SLinus Torvalds 
1444a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1445bdc7f021STrond Myklebust 
144607737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1447dbae4c73STrond Myklebust 	if (IS_ERR(task))
1448dbae4c73STrond Myklebust 		return PTR_ERR(task);
1449d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1450d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
145107737691STrond Myklebust 	rpc_put_task(task);
1452dbae4c73STrond Myklebust 	return 0;
14531da177e4SLinus Torvalds }
1454e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
14551da177e4SLinus Torvalds 
14561da177e4SLinus Torvalds /*
14579ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
14589ace33cdSFred Isaman  */
14590b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1460988b6dceSFred Isaman 		     struct list_head *head,
1461f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1462f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
14639ace33cdSFred Isaman {
14649ace33cdSFred Isaman 	struct nfs_page *first = nfs_list_entry(head->next);
14653d4ff43dSAl Viro 	struct inode *inode = first->wb_context->dentry->d_inode;
14669ace33cdSFred Isaman 
14679ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
14689ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
14699ace33cdSFred Isaman 
14709ace33cdSFred Isaman 	list_splice_init(head, &data->pages);
14719ace33cdSFred Isaman 
14729ace33cdSFred Isaman 	data->inode	  = inode;
14739ace33cdSFred Isaman 	data->cred	  = first->wb_context->cred;
1474988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
14759ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1476f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1477b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
14789ace33cdSFred Isaman 
14799ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
14809ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
14819ace33cdSFred Isaman 	data->args.offset = 0;
14829ace33cdSFred Isaman 	data->args.count  = 0;
14830b7c0153SFred Isaman 	data->context     = get_nfs_open_context(first->wb_context);
14849ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
14859ace33cdSFred Isaman 	data->res.verf    = &data->verf;
14869ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
14879ace33cdSFred Isaman }
1488e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
14899ace33cdSFred Isaman 
1490e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1491ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1492ea2cf228SFred Isaman 		      struct nfs_commit_info *cinfo)
149364bfeb49SFred Isaman {
149464bfeb49SFred Isaman 	struct nfs_page *req;
149564bfeb49SFred Isaman 
149664bfeb49SFred Isaman 	while (!list_empty(page_list)) {
149764bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
149864bfeb49SFred Isaman 		nfs_list_remove_request(req);
1499ea2cf228SFred Isaman 		nfs_mark_request_commit(req, lseg, cinfo);
150056f9cd68SFred Isaman 		if (!cinfo->dreq) {
150164bfeb49SFred Isaman 			dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1502d56b4ddfSMel Gorman 			dec_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
150364bfeb49SFred Isaman 				     BDI_RECLAIMABLE);
150456f9cd68SFred Isaman 		}
15051d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
150664bfeb49SFred Isaman 	}
150764bfeb49SFred Isaman }
1508e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
150964bfeb49SFred Isaman 
15109ace33cdSFred Isaman /*
15111da177e4SLinus Torvalds  * Commit dirty pages
15121da177e4SLinus Torvalds  */
15131da177e4SLinus Torvalds static int
1514ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1515ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
15161da177e4SLinus Torvalds {
15170b7c0153SFred Isaman 	struct nfs_commit_data	*data;
15181da177e4SLinus Torvalds 
1519c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds 	if (!data)
15221da177e4SLinus Torvalds 		goto out_bad;
15231da177e4SLinus Torvalds 
15241da177e4SLinus Torvalds 	/* Set up the argument struct */
1525f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
1526f453a54aSFred Isaman 	atomic_inc(&cinfo->mds->rpcs_out);
15279f0ec176SAndy Adamson 	return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops,
15289f0ec176SAndy Adamson 				   how, 0);
15291da177e4SLinus Torvalds  out_bad:
1530ea2cf228SFred Isaman 	nfs_retry_commit(head, NULL, cinfo);
1531f453a54aSFred Isaman 	cinfo->completion_ops->error_cleanup(NFS_I(inode));
15321da177e4SLinus Torvalds 	return -ENOMEM;
15331da177e4SLinus Torvalds }
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds /*
15361da177e4SLinus Torvalds  * COMMIT call returned
15371da177e4SLinus Torvalds  */
1538788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
15391da177e4SLinus Torvalds {
15400b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
15411da177e4SLinus Torvalds 
1542a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
15431da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
15441da177e4SLinus Torvalds 
1545788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1546c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
1547c9d8f89dSTrond Myklebust }
1548c9d8f89dSTrond Myklebust 
1549f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1550c9d8f89dSTrond Myklebust {
1551c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1552c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1553f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
1554788e7a89STrond Myklebust 
15551da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
15561da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
15571da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1558d6d6dc7cSFred Isaman 		nfs_clear_page_commit(req->wb_page);
15591da177e4SLinus Torvalds 
156048186c7dSChuck Lever 		dprintk("NFS:       commit (%s/%lld %d@%lld)",
15613d4ff43dSAl Viro 			req->wb_context->dentry->d_sb->s_id,
15623d4ff43dSAl Viro 			(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
15631da177e4SLinus Torvalds 			req->wb_bytes,
15641da177e4SLinus Torvalds 			(long long)req_offset(req));
1565c9d8f89dSTrond Myklebust 		if (status < 0) {
1566c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
15671da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1568c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
15691da177e4SLinus Torvalds 			goto next;
15701da177e4SLinus Torvalds 		}
15711da177e4SLinus Torvalds 
15721da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
15731da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
15742f2c63bcSTrond Myklebust 		if (!memcmp(&req->wb_verf, &data->verf.verifier, sizeof(req->wb_verf))) {
15751da177e4SLinus Torvalds 			/* We have a match */
15761da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
15771da177e4SLinus Torvalds 			dprintk(" OK\n");
15781da177e4SLinus Torvalds 			goto next;
15791da177e4SLinus Torvalds 		}
15801da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
15811da177e4SLinus Torvalds 		dprintk(" mismatch\n");
15826d884e8fSFred 		nfs_mark_request_dirty(req);
15831da177e4SLinus Torvalds 	next:
15841d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
15851da177e4SLinus Torvalds 	}
1586f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1587f453a54aSFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
1588f453a54aSFred Isaman 		nfs_commit_clear_lock(NFS_I(data->inode));
15895917ce84SFred Isaman }
15905917ce84SFred Isaman 
15915917ce84SFred Isaman static void nfs_commit_release(void *calldata)
15925917ce84SFred Isaman {
15930b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
15945917ce84SFred Isaman 
1595f453a54aSFred Isaman 	data->completion_ops->completion(data);
1596c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
15971da177e4SLinus Torvalds }
1598788e7a89STrond Myklebust 
1599788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
16000b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1601788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1602788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1603788e7a89STrond Myklebust };
16041da177e4SLinus Torvalds 
1605f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1606f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1607f453a54aSFred Isaman 	.error_cleanup = nfs_commit_clear_lock,
1608f453a54aSFred Isaman };
1609f453a54aSFred Isaman 
16101763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1611ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
161284c53ab5SFred Isaman {
161384c53ab5SFred Isaman 	int status;
161484c53ab5SFred Isaman 
1615ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
161684c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1617ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
161884c53ab5SFred Isaman 	return status;
161984c53ab5SFred Isaman }
162084c53ab5SFred Isaman 
1621b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
16221da177e4SLinus Torvalds {
16231da177e4SLinus Torvalds 	LIST_HEAD(head);
1624ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
162571d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1626b8413f98STrond Myklebust 	int res;
16271da177e4SLinus Torvalds 
1628b8413f98STrond Myklebust 	res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1629b8413f98STrond Myklebust 	if (res <= 0)
1630c5efa5fcSTrond Myklebust 		goto out_mark_dirty;
1631ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1632ea2cf228SFred Isaman 	res = nfs_scan_commit(inode, &head, &cinfo);
16331da177e4SLinus Torvalds 	if (res) {
1634a861a1e1SFred Isaman 		int error;
1635a861a1e1SFred Isaman 
1636ea2cf228SFred Isaman 		error = nfs_generic_commit_list(inode, &head, how, &cinfo);
16371da177e4SLinus Torvalds 		if (error < 0)
16381da177e4SLinus Torvalds 			return error;
1639b8413f98STrond Myklebust 		if (!may_wait)
1640b8413f98STrond Myklebust 			goto out_mark_dirty;
1641b8413f98STrond Myklebust 		error = wait_on_bit(&NFS_I(inode)->flags,
1642b8413f98STrond Myklebust 				NFS_INO_COMMIT,
164371d0a611STrond Myklebust 				nfs_wait_bit_killable,
164471d0a611STrond Myklebust 				TASK_KILLABLE);
1645b8413f98STrond Myklebust 		if (error < 0)
1646b8413f98STrond Myklebust 			return error;
164771d0a611STrond Myklebust 	} else
164871d0a611STrond Myklebust 		nfs_commit_clear_lock(NFS_I(inode));
1649c5efa5fcSTrond Myklebust 	return res;
1650c5efa5fcSTrond Myklebust 	/* Note: If we exit without ensuring that the commit is complete,
1651c5efa5fcSTrond Myklebust 	 * we must mark the inode as dirty. Otherwise, future calls to
1652c5efa5fcSTrond Myklebust 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1653c5efa5fcSTrond Myklebust 	 * that the data is on the disk.
1654c5efa5fcSTrond Myklebust 	 */
1655c5efa5fcSTrond Myklebust out_mark_dirty:
1656c5efa5fcSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
16571da177e4SLinus Torvalds 	return res;
16581da177e4SLinus Torvalds }
16598fc795f7STrond Myklebust 
16608fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
16618fc795f7STrond Myklebust {
1662420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1663420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1664420e3646STrond Myklebust 	int ret = 0;
16658fc795f7STrond Myklebust 
16663236c3e1SJeff Layton 	/* no commits means nothing needs to be done */
1667ea2cf228SFred Isaman 	if (!nfsi->commit_info.ncommit)
16683236c3e1SJeff Layton 		return ret;
16693236c3e1SJeff Layton 
1670a00dd6c0SJeff Layton 	if (wbc->sync_mode == WB_SYNC_NONE) {
1671a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1672a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1673420e3646STrond Myklebust 		 */
1674ea2cf228SFred Isaman 		if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1))
1675420e3646STrond Myklebust 			goto out_mark_dirty;
1676420e3646STrond Myklebust 
1677a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1678420e3646STrond Myklebust 		flags = 0;
1679a00dd6c0SJeff Layton 	}
1680a00dd6c0SJeff Layton 
1681420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1682420e3646STrond Myklebust 	if (ret >= 0) {
1683420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1684420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1685420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1686420e3646STrond Myklebust 			else
1687420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1688420e3646STrond Myklebust 		}
16898fc795f7STrond Myklebust 		return 0;
1690420e3646STrond Myklebust 	}
1691420e3646STrond Myklebust out_mark_dirty:
16928fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
16938fc795f7STrond Myklebust 	return ret;
16948fc795f7STrond Myklebust }
1695c63c7b05STrond Myklebust #else
16968fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
16978fc795f7STrond Myklebust {
16988fc795f7STrond Myklebust 	return 0;
16998fc795f7STrond Myklebust }
17001da177e4SLinus Torvalds #endif
17011da177e4SLinus Torvalds 
17028fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
17038fc795f7STrond Myklebust {
1704a8d8f02cSBryan Schumaker 	return nfs_commit_unstable_pages(inode, wbc);
1705a8d8f02cSBryan Schumaker }
170689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode);
1707863a3c6cSAndy Adamson 
1708acdc53b2STrond Myklebust /*
1709acdc53b2STrond Myklebust  * flush the inode to disk.
1710acdc53b2STrond Myklebust  */
1711acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
171234901f70STrond Myklebust {
171334901f70STrond Myklebust 	struct writeback_control wbc = {
171472cb77f4STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
171534901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
1716d7fb1207STrond Myklebust 		.range_start = 0,
1717d7fb1207STrond Myklebust 		.range_end = LLONG_MAX,
171834901f70STrond Myklebust 	};
171934901f70STrond Myklebust 
1720acdc53b2STrond Myklebust 	return sync_inode(inode, &wbc);
17211c75950bSTrond Myklebust }
1722ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all);
17231c75950bSTrond Myklebust 
17241b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
17251b3b4a1aSTrond Myklebust {
17261b3b4a1aSTrond Myklebust 	struct nfs_page *req;
17271b3b4a1aSTrond Myklebust 	int ret = 0;
17281b3b4a1aSTrond Myklebust 
17291b3b4a1aSTrond Myklebust 	BUG_ON(!PageLocked(page));
17301b3b4a1aSTrond Myklebust 	for (;;) {
1731ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
17321b3b4a1aSTrond Myklebust 		req = nfs_page_find_request(page);
17331b3b4a1aSTrond Myklebust 		if (req == NULL)
17341b3b4a1aSTrond Myklebust 			break;
17357ad84aa9STrond Myklebust 		if (nfs_lock_request(req)) {
17368dd37758STrond Myklebust 			nfs_clear_request_commit(req);
17371b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
17381b3b4a1aSTrond Myklebust 			/*
17391b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
17401b3b4a1aSTrond Myklebust 			 * page as being dirty
17411b3b4a1aSTrond Myklebust 			 */
17421b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
17431d1afcbcSTrond Myklebust 			nfs_unlock_and_release_request(req);
17441b3b4a1aSTrond Myklebust 			break;
17451b3b4a1aSTrond Myklebust 		}
17461b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
1747c9edda71STrond Myklebust 		nfs_release_request(req);
17481b3b4a1aSTrond Myklebust 		if (ret < 0)
1749c988950eSTrond Myklebust 			break;
17501b3b4a1aSTrond Myklebust 	}
17511b3b4a1aSTrond Myklebust 	return ret;
17521b3b4a1aSTrond Myklebust }
17531b3b4a1aSTrond Myklebust 
17541c75950bSTrond Myklebust /*
17551c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
17561c75950bSTrond Myklebust  */
17571c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
17581c75950bSTrond Myklebust {
175929418aa4SMel Gorman 	loff_t range_start = page_file_offset(page);
17607f2f12d9STrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
17617f2f12d9STrond Myklebust 	struct writeback_control wbc = {
17627f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
17637f2f12d9STrond Myklebust 		.nr_to_write = 0,
17647f2f12d9STrond Myklebust 		.range_start = range_start,
17657f2f12d9STrond Myklebust 		.range_end = range_end,
17667f2f12d9STrond Myklebust 	};
17677f2f12d9STrond Myklebust 	int ret;
17687f2f12d9STrond Myklebust 
17690522f6adSTrond Myklebust 	for (;;) {
1770ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
17717f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
17727f2f12d9STrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
17737f2f12d9STrond Myklebust 			if (ret < 0)
17747f2f12d9STrond Myklebust 				goto out_error;
17750522f6adSTrond Myklebust 			continue;
17767f2f12d9STrond Myklebust 		}
17770522f6adSTrond Myklebust 		if (!PagePrivate(page))
17780522f6adSTrond Myklebust 			break;
17790522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
17807f2f12d9STrond Myklebust 		if (ret < 0)
17817f2f12d9STrond Myklebust 			goto out_error;
17827f2f12d9STrond Myklebust 	}
17837f2f12d9STrond Myklebust 	return 0;
17847f2f12d9STrond Myklebust out_error:
17857f2f12d9STrond Myklebust 	return ret;
17861c75950bSTrond Myklebust }
17871c75950bSTrond Myklebust 
1788074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
1789074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1790a6bc32b8SMel Gorman 		struct page *page, enum migrate_mode mode)
1791074cc1deSTrond Myklebust {
17922da95652SJeff Layton 	/*
17932da95652SJeff Layton 	 * If PagePrivate is set, then the page is currently associated with
17942da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
17952da95652SJeff Layton 	 *
17962da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
17972da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
17982da95652SJeff Layton 	 *        the page lock.
17992da95652SJeff Layton 	 */
18002da95652SJeff Layton 	if (PagePrivate(page))
18012da95652SJeff Layton 		return -EBUSY;
1802074cc1deSTrond Myklebust 
1803074cc1deSTrond Myklebust 	nfs_fscache_release_page(page, GFP_KERNEL);
1804074cc1deSTrond Myklebust 
1805a6bc32b8SMel Gorman 	return migrate_page(mapping, newpage, page, mode);
1806074cc1deSTrond Myklebust }
1807074cc1deSTrond Myklebust #endif
1808074cc1deSTrond Myklebust 
1809f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
18101da177e4SLinus Torvalds {
18111da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1812cd841605SFred Isaman 					     sizeof(struct nfs_write_header),
18131da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
181420c2df83SPaul Mundt 					     NULL);
18151da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
18161da177e4SLinus Torvalds 		return -ENOMEM;
18171da177e4SLinus Torvalds 
181893d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
18191da177e4SLinus Torvalds 						     nfs_wdata_cachep);
18201da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
18213dd4765fSJeff Layton 		goto out_destroy_write_cache;
18221da177e4SLinus Torvalds 
18230b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
18240b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
18250b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
18260b7c0153SFred Isaman 					     NULL);
18270b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
18283dd4765fSJeff Layton 		goto out_destroy_write_mempool;
18290b7c0153SFred Isaman 
183093d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
18311da177e4SLinus Torvalds 						      nfs_wdata_cachep);
18321da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
18333dd4765fSJeff Layton 		goto out_destroy_commit_cache;
18341da177e4SLinus Torvalds 
183589a09141SPeter Zijlstra 	/*
183689a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
183789a09141SPeter Zijlstra 	 *
183889a09141SPeter Zijlstra 	 *  64MB:    8192k
183989a09141SPeter Zijlstra 	 * 128MB:   11585k
184089a09141SPeter Zijlstra 	 * 256MB:   16384k
184189a09141SPeter Zijlstra 	 * 512MB:   23170k
184289a09141SPeter Zijlstra 	 *   1GB:   32768k
184389a09141SPeter Zijlstra 	 *   2GB:   46340k
184489a09141SPeter Zijlstra 	 *   4GB:   65536k
184589a09141SPeter Zijlstra 	 *   8GB:   92681k
184689a09141SPeter Zijlstra 	 *  16GB:  131072k
184789a09141SPeter Zijlstra 	 *
184889a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
184989a09141SPeter Zijlstra 	 * Limit the default to 256M
185089a09141SPeter Zijlstra 	 */
185189a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
185289a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
185389a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
185489a09141SPeter Zijlstra 
18551da177e4SLinus Torvalds 	return 0;
18563dd4765fSJeff Layton 
18573dd4765fSJeff Layton out_destroy_commit_cache:
18583dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
18593dd4765fSJeff Layton out_destroy_write_mempool:
18603dd4765fSJeff Layton 	mempool_destroy(nfs_wdata_mempool);
18613dd4765fSJeff Layton out_destroy_write_cache:
18623dd4765fSJeff Layton 	kmem_cache_destroy(nfs_wdata_cachep);
18633dd4765fSJeff Layton 	return -ENOMEM;
18641da177e4SLinus Torvalds }
18651da177e4SLinus Torvalds 
1866266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
18671da177e4SLinus Torvalds {
18681da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
18693dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
18701da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
18711a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
18721da177e4SLinus Torvalds }
18731da177e4SLinus Torvalds 
1874