xref: /linux/fs/nfs/write.c (revision 25b11dcdbfcad69a5ec03265e2dce19e5eca936b)
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 {
550b7c0153SFred Isaman 	struct nfs_commit_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
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 {
73cd841605SFred Isaman 	struct nfs_write_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
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);
833feb2d49STrond Myklebust 	}
843feb2d49STrond Myklebust 	return p;
853feb2d49STrond Myklebust }
863feb2d49STrond Myklebust 
871763da12SFred Isaman static struct nfs_write_data *nfs_writedata_alloc(struct nfs_pgio_header *hdr,
886c75dc0dSFred Isaman 						  unsigned int pagecount)
896c75dc0dSFred Isaman {
906c75dc0dSFred Isaman 	struct nfs_write_data *data, *prealloc;
916c75dc0dSFred Isaman 
926c75dc0dSFred Isaman 	prealloc = &container_of(hdr, struct nfs_write_header, header)->rpc_data;
936c75dc0dSFred Isaman 	if (prealloc->header == NULL)
946c75dc0dSFred Isaman 		data = prealloc;
956c75dc0dSFred Isaman 	else
966c75dc0dSFred Isaman 		data = kzalloc(sizeof(*data), GFP_KERNEL);
976c75dc0dSFred Isaman 	if (!data)
986c75dc0dSFred Isaman 		goto out;
996c75dc0dSFred Isaman 
1006c75dc0dSFred Isaman 	if (nfs_pgarray_set(&data->pages, pagecount)) {
1016c75dc0dSFred Isaman 		data->header = hdr;
1026c75dc0dSFred Isaman 		atomic_inc(&hdr->refcnt);
1036c75dc0dSFred Isaman 	} else {
1046c75dc0dSFred Isaman 		if (data != prealloc)
1056c75dc0dSFred Isaman 			kfree(data);
1066c75dc0dSFred Isaman 		data = NULL;
1076c75dc0dSFred Isaman 	}
1086c75dc0dSFred Isaman out:
1096c75dc0dSFred Isaman 	return data;
1106c75dc0dSFred Isaman }
1116c75dc0dSFred Isaman 
112cd841605SFred Isaman void nfs_writehdr_free(struct nfs_pgio_header *hdr)
1133feb2d49STrond Myklebust {
114cd841605SFred Isaman 	struct nfs_write_header *whdr = container_of(hdr, struct nfs_write_header, header);
115cd841605SFred Isaman 	mempool_free(whdr, nfs_wdata_mempool);
1163feb2d49STrond Myklebust }
1173feb2d49STrond Myklebust 
118dce81290STrond Myklebust void nfs_writedata_release(struct nfs_write_data *wdata)
1191da177e4SLinus Torvalds {
1206c75dc0dSFred Isaman 	struct nfs_pgio_header *hdr = wdata->header;
1216c75dc0dSFred Isaman 	struct nfs_write_header *write_header = container_of(hdr, struct nfs_write_header, header);
1226c75dc0dSFred Isaman 
123383ba719STrond Myklebust 	put_nfs_open_context(wdata->args.context);
12430dd374fSFred Isaman 	if (wdata->pages.pagevec != wdata->pages.page_array)
12530dd374fSFred Isaman 		kfree(wdata->pages.pagevec);
1266c75dc0dSFred Isaman 	if (wdata != &write_header->rpc_data)
1276c75dc0dSFred Isaman 		kfree(wdata);
1286c75dc0dSFred Isaman 	else
1296c75dc0dSFred Isaman 		wdata->header = NULL;
1306c75dc0dSFred Isaman 	if (atomic_dec_and_test(&hdr->refcnt))
131061ae2edSFred Isaman 		hdr->completion_ops->completion(hdr);
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds 
1347b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
1357b159fc1STrond Myklebust {
1367b159fc1STrond Myklebust 	ctx->error = error;
1377b159fc1STrond Myklebust 	smp_wmb();
1387b159fc1STrond Myklebust 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
1397b159fc1STrond Myklebust }
1407b159fc1STrond Myklebust 
141277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request_locked(struct page *page)
142277459d2STrond Myklebust {
143277459d2STrond Myklebust 	struct nfs_page *req = NULL;
144277459d2STrond Myklebust 
145277459d2STrond Myklebust 	if (PagePrivate(page)) {
146277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
147277459d2STrond Myklebust 		if (req != NULL)
148c03b4024STrond Myklebust 			kref_get(&req->wb_kref);
149277459d2STrond Myklebust 	}
150277459d2STrond Myklebust 	return req;
151277459d2STrond Myklebust }
152277459d2STrond Myklebust 
153277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page)
154277459d2STrond Myklebust {
155587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
156277459d2STrond Myklebust 	struct nfs_page *req = NULL;
157277459d2STrond Myklebust 
158587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
159277459d2STrond Myklebust 	req = nfs_page_find_request_locked(page);
160587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
161277459d2STrond Myklebust 	return req;
162277459d2STrond Myklebust }
163277459d2STrond Myklebust 
1641da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1651da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1661da177e4SLinus Torvalds {
1671da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
168a3d01454STrond Myklebust 	loff_t end, i_size;
169a3d01454STrond Myklebust 	pgoff_t end_index;
1701da177e4SLinus Torvalds 
171a3d01454STrond Myklebust 	spin_lock(&inode->i_lock);
172a3d01454STrond Myklebust 	i_size = i_size_read(inode);
173a3d01454STrond Myklebust 	end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
1741da177e4SLinus Torvalds 	if (i_size > 0 && page->index < end_index)
175a3d01454STrond Myklebust 		goto out;
1761da177e4SLinus Torvalds 	end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
1771da177e4SLinus Torvalds 	if (i_size >= end)
178a3d01454STrond Myklebust 		goto out;
1791da177e4SLinus Torvalds 	i_size_write(inode, end);
180a3d01454STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
181a3d01454STrond Myklebust out:
182a3d01454STrond Myklebust 	spin_unlock(&inode->i_lock);
1831da177e4SLinus Torvalds }
1841da177e4SLinus Torvalds 
185a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
186a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
187a301b777STrond Myklebust {
188a301b777STrond Myklebust 	SetPageError(page);
189a301b777STrond Myklebust 	nfs_zap_mapping(page->mapping->host, page->mapping);
190a301b777STrond Myklebust }
191a301b777STrond Myklebust 
1921da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
1931da177e4SLinus Torvalds  * covers the full page.
1941da177e4SLinus Torvalds  */
1951da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
1961da177e4SLinus Torvalds {
1971da177e4SLinus Torvalds 	if (PageUptodate(page))
1981da177e4SLinus Torvalds 		return;
1991da177e4SLinus Torvalds 	if (base != 0)
2001da177e4SLinus Torvalds 		return;
20149a70f27STrond Myklebust 	if (count != nfs_page_length(page))
2021da177e4SLinus Torvalds 		return;
2031da177e4SLinus Torvalds 	SetPageUptodate(page);
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
2071da177e4SLinus Torvalds {
2081da177e4SLinus Torvalds 	if (wbc->for_reclaim)
209c63c7b05STrond Myklebust 		return FLUSH_HIGHPRI | FLUSH_STABLE;
210b17621feSWu Fengguang 	if (wbc->for_kupdate || wbc->for_background)
211b31268acSTrond Myklebust 		return FLUSH_LOWPRI | FLUSH_COND_STABLE;
212b31268acSTrond Myklebust 	return FLUSH_COND_STABLE;
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds /*
21689a09141SPeter Zijlstra  * NFS congestion control
21789a09141SPeter Zijlstra  */
21889a09141SPeter Zijlstra 
21989a09141SPeter Zijlstra int nfs_congestion_kb;
22089a09141SPeter Zijlstra 
22189a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
22289a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
22389a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
22489a09141SPeter Zijlstra 
2255a6d41b3STrond Myklebust static int nfs_set_page_writeback(struct page *page)
22689a09141SPeter Zijlstra {
2275a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
2285a6d41b3STrond Myklebust 
2295a6d41b3STrond Myklebust 	if (!ret) {
23089a09141SPeter Zijlstra 		struct inode *inode = page->mapping->host;
23189a09141SPeter Zijlstra 		struct nfs_server *nfss = NFS_SERVER(inode);
23289a09141SPeter Zijlstra 
233a6305ddbSTrond Myklebust 		page_cache_get(page);
234277866a0SPeter Zijlstra 		if (atomic_long_inc_return(&nfss->writeback) >
2358aa7e847SJens Axboe 				NFS_CONGESTION_ON_THRESH) {
2368aa7e847SJens Axboe 			set_bdi_congested(&nfss->backing_dev_info,
2378aa7e847SJens Axboe 						BLK_RW_ASYNC);
2388aa7e847SJens Axboe 		}
23989a09141SPeter Zijlstra 	}
2405a6d41b3STrond Myklebust 	return ret;
24189a09141SPeter Zijlstra }
24289a09141SPeter Zijlstra 
24389a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page)
24489a09141SPeter Zijlstra {
24589a09141SPeter Zijlstra 	struct inode *inode = page->mapping->host;
24689a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
24789a09141SPeter Zijlstra 
24889a09141SPeter Zijlstra 	end_page_writeback(page);
249a6305ddbSTrond Myklebust 	page_cache_release(page);
250c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
2518aa7e847SJens Axboe 		clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
25289a09141SPeter Zijlstra }
25389a09141SPeter Zijlstra 
254cfb506e1STrond Myklebust static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock)
255e261f51fSTrond Myklebust {
256587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
257e261f51fSTrond Myklebust 	struct nfs_page *req;
258e261f51fSTrond Myklebust 	int ret;
259e261f51fSTrond Myklebust 
260587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
261e261f51fSTrond Myklebust 	for (;;) {
262e261f51fSTrond Myklebust 		req = nfs_page_find_request_locked(page);
263074cc1deSTrond Myklebust 		if (req == NULL)
264074cc1deSTrond Myklebust 			break;
2659994b62bSFred Isaman 		if (nfs_lock_request_dontget(req))
266e261f51fSTrond Myklebust 			break;
267e261f51fSTrond Myklebust 		/* Note: If we hold the page lock, as is the case in nfs_writepage,
2689994b62bSFred Isaman 		 *	 then the call to nfs_lock_request_dontget() will always
269e261f51fSTrond Myklebust 		 *	 succeed provided that someone hasn't already marked the
270e261f51fSTrond Myklebust 		 *	 request as dirty (in which case we don't care).
271e261f51fSTrond Myklebust 		 */
272587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
273cfb506e1STrond Myklebust 		if (!nonblock)
274e261f51fSTrond Myklebust 			ret = nfs_wait_on_request(req);
275cfb506e1STrond Myklebust 		else
276cfb506e1STrond Myklebust 			ret = -EAGAIN;
277e261f51fSTrond Myklebust 		nfs_release_request(req);
278e261f51fSTrond Myklebust 		if (ret != 0)
279074cc1deSTrond Myklebust 			return ERR_PTR(ret);
280587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
281e261f51fSTrond Myklebust 	}
282587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
283074cc1deSTrond Myklebust 	return req;
284612c9384STrond Myklebust }
285074cc1deSTrond Myklebust 
286074cc1deSTrond Myklebust /*
287074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
288074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
289074cc1deSTrond Myklebust  */
290074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
291cfb506e1STrond Myklebust 				struct page *page, bool nonblock)
292074cc1deSTrond Myklebust {
293074cc1deSTrond Myklebust 	struct nfs_page *req;
294074cc1deSTrond Myklebust 	int ret = 0;
295074cc1deSTrond Myklebust 
296cfb506e1STrond Myklebust 	req = nfs_find_and_lock_request(page, nonblock);
297074cc1deSTrond Myklebust 	if (!req)
298074cc1deSTrond Myklebust 		goto out;
299074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
300074cc1deSTrond Myklebust 	if (IS_ERR(req))
301074cc1deSTrond Myklebust 		goto out;
302074cc1deSTrond Myklebust 
303074cc1deSTrond Myklebust 	ret = nfs_set_page_writeback(page);
304074cc1deSTrond Myklebust 	BUG_ON(ret != 0);
305074cc1deSTrond Myklebust 	BUG_ON(test_bit(PG_CLEAN, &req->wb_flags));
306074cc1deSTrond Myklebust 
307f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
308f8512ad0SFred Isaman 		nfs_redirty_request(req);
309074cc1deSTrond Myklebust 		ret = pgio->pg_error;
310f8512ad0SFred Isaman 	}
311074cc1deSTrond Myklebust out:
312074cc1deSTrond Myklebust 	return ret;
313e261f51fSTrond Myklebust }
314e261f51fSTrond Myklebust 
315f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
316f758c885STrond Myklebust {
317f758c885STrond Myklebust 	struct inode *inode = page->mapping->host;
318cfb506e1STrond Myklebust 	int ret;
319f758c885STrond Myklebust 
320f758c885STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
321f758c885STrond Myklebust 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
322f758c885STrond Myklebust 
323f758c885STrond Myklebust 	nfs_pageio_cond_complete(pgio, page->index);
3241b430beeSWu Fengguang 	ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
325cfb506e1STrond Myklebust 	if (ret == -EAGAIN) {
326cfb506e1STrond Myklebust 		redirty_page_for_writepage(wbc, page);
327cfb506e1STrond Myklebust 		ret = 0;
328cfb506e1STrond Myklebust 	}
329cfb506e1STrond Myklebust 	return ret;
330f758c885STrond Myklebust }
331f758c885STrond Myklebust 
332e261f51fSTrond Myklebust /*
3331da177e4SLinus Torvalds  * Write an mmapped page to the server.
3341da177e4SLinus Torvalds  */
3354d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
3361da177e4SLinus Torvalds {
337f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
338e261f51fSTrond Myklebust 	int err;
3391da177e4SLinus Torvalds 
340061ae2edSFred Isaman 	nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc),
341061ae2edSFred Isaman 			      &nfs_async_write_completion_ops);
342f758c885STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
343f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
344f758c885STrond Myklebust 	if (err < 0)
3454d770ccfSTrond Myklebust 		return err;
346f758c885STrond Myklebust 	if (pgio.pg_error < 0)
347f758c885STrond Myklebust 		return pgio.pg_error;
348f758c885STrond Myklebust 	return 0;
3494d770ccfSTrond Myklebust }
3504d770ccfSTrond Myklebust 
3514d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
3524d770ccfSTrond Myklebust {
353f758c885STrond Myklebust 	int ret;
3544d770ccfSTrond Myklebust 
355f758c885STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
3561da177e4SLinus Torvalds 	unlock_page(page);
357f758c885STrond Myklebust 	return ret;
358f758c885STrond Myklebust }
359f758c885STrond Myklebust 
360f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
361f758c885STrond Myklebust {
362f758c885STrond Myklebust 	int ret;
363f758c885STrond Myklebust 
364f758c885STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
365f758c885STrond Myklebust 	unlock_page(page);
366f758c885STrond Myklebust 	return ret;
3671da177e4SLinus Torvalds }
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3701da177e4SLinus Torvalds {
3711da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
37272cb77f4STrond Myklebust 	unsigned long *bitlock = &NFS_I(inode)->flags;
373c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
3741da177e4SLinus Torvalds 	int err;
3751da177e4SLinus Torvalds 
37672cb77f4STrond Myklebust 	/* Stop dirtying of new pages while we sync */
37772cb77f4STrond Myklebust 	err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
37872cb77f4STrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
37972cb77f4STrond Myklebust 	if (err)
38072cb77f4STrond Myklebust 		goto out_err;
38172cb77f4STrond Myklebust 
38291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
38391d5b470SChuck Lever 
384061ae2edSFred Isaman 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc),
385061ae2edSFred Isaman 			      &nfs_async_write_completion_ops);
386f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
387c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
38872cb77f4STrond Myklebust 
38972cb77f4STrond Myklebust 	clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
39072cb77f4STrond Myklebust 	smp_mb__after_clear_bit();
39172cb77f4STrond Myklebust 	wake_up_bit(bitlock, NFS_INO_FLUSHING);
39272cb77f4STrond Myklebust 
393f758c885STrond Myklebust 	if (err < 0)
39472cb77f4STrond Myklebust 		goto out_err;
39572cb77f4STrond Myklebust 	err = pgio.pg_error;
39672cb77f4STrond Myklebust 	if (err < 0)
39772cb77f4STrond Myklebust 		goto out_err;
398c63c7b05STrond Myklebust 	return 0;
39972cb77f4STrond Myklebust out_err:
40072cb77f4STrond Myklebust 	return err;
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds /*
4041da177e4SLinus Torvalds  * Insert a write request into an inode
4051da177e4SLinus Torvalds  */
406d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
4071da177e4SLinus Torvalds {
4081da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
409e7d39069STrond Myklebust 
410e7d39069STrond Myklebust 	/* Lock the request! */
411e7d39069STrond Myklebust 	nfs_lock_request_dontget(req);
412e7d39069STrond Myklebust 
413e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
4144d65c520STrond Myklebust 	if (!nfsi->npages && nfs_have_delegation(inode, FMODE_WRITE))
415a9a4a87aSTrond Myklebust 		inode->i_version++;
4162df485a7STrond Myklebust 	set_bit(PG_MAPPED, &req->wb_flags);
417deb7d638STrond Myklebust 	SetPagePrivate(req->wb_page);
418277459d2STrond Myklebust 	set_page_private(req->wb_page, (unsigned long)req);
4191da177e4SLinus Torvalds 	nfsi->npages++;
420c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
421e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
4221da177e4SLinus Torvalds }
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds /*
42589a09141SPeter Zijlstra  * Remove a write request from an inode
4261da177e4SLinus Torvalds  */
4271da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
4281da177e4SLinus Torvalds {
4293d4ff43dSAl Viro 	struct inode *inode = req->wb_context->dentry->d_inode;
4301da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	BUG_ON (!NFS_WBACK_BUSY(req));
4331da177e4SLinus Torvalds 
434587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
435277459d2STrond Myklebust 	set_page_private(req->wb_page, 0);
436deb7d638STrond Myklebust 	ClearPagePrivate(req->wb_page);
4372df485a7STrond Myklebust 	clear_bit(PG_MAPPED, &req->wb_flags);
4381da177e4SLinus Torvalds 	nfsi->npages--;
439587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
4401da177e4SLinus Torvalds 	nfs_release_request(req);
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
44361822ab5STrond Myklebust static void
4446d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
44561822ab5STrond Myklebust {
44661822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
44761822ab5STrond Myklebust }
44861822ab5STrond Myklebust 
4491da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
4508dd37758STrond Myklebust /**
4518dd37758STrond Myklebust  * nfs_request_add_commit_list - add request to a commit list
4528dd37758STrond Myklebust  * @req: pointer to a struct nfs_page
453ea2cf228SFred Isaman  * @dst: commit list head
454ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
4558dd37758STrond Myklebust  *
456ea2cf228SFred Isaman  * This sets the PG_CLEAN bit, updates the cinfo count of
4578dd37758STrond Myklebust  * number of outstanding requests requiring a commit as well as
4588dd37758STrond Myklebust  * the MM page stats.
4598dd37758STrond Myklebust  *
460ea2cf228SFred Isaman  * The caller must _not_ hold the cinfo->lock, but must be
4618dd37758STrond Myklebust  * holding the nfs_page lock.
4628dd37758STrond Myklebust  */
4638dd37758STrond Myklebust void
464ea2cf228SFred Isaman nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst,
465ea2cf228SFred Isaman 			    struct nfs_commit_info *cinfo)
4668dd37758STrond Myklebust {
4678dd37758STrond Myklebust 	set_bit(PG_CLEAN, &(req)->wb_flags);
468ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
469ea2cf228SFred Isaman 	nfs_list_add_request(req, dst);
470ea2cf228SFred Isaman 	cinfo->mds->ncommit++;
471ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
47256f9cd68SFred Isaman 	if (!cinfo->dreq) {
4738dd37758STrond Myklebust 		inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
47456f9cd68SFred Isaman 		inc_bdi_stat(req->wb_page->mapping->backing_dev_info,
47556f9cd68SFred Isaman 			     BDI_RECLAIMABLE);
47656f9cd68SFred Isaman 		__mark_inode_dirty(req->wb_context->dentry->d_inode,
47756f9cd68SFred Isaman 				   I_DIRTY_DATASYNC);
47856f9cd68SFred Isaman 	}
4798dd37758STrond Myklebust }
4808dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
4818dd37758STrond Myklebust 
4828dd37758STrond Myklebust /**
4838dd37758STrond Myklebust  * nfs_request_remove_commit_list - Remove request from a commit list
4848dd37758STrond Myklebust  * @req: pointer to a nfs_page
485ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
4868dd37758STrond Myklebust  *
487ea2cf228SFred Isaman  * This clears the PG_CLEAN bit, and updates the cinfo's count of
4888dd37758STrond Myklebust  * number of outstanding requests requiring a commit
4898dd37758STrond Myklebust  * It does not update the MM page stats.
4908dd37758STrond Myklebust  *
491ea2cf228SFred Isaman  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
4928dd37758STrond Myklebust  */
4938dd37758STrond Myklebust void
494ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req,
495ea2cf228SFred Isaman 			       struct nfs_commit_info *cinfo)
4968dd37758STrond Myklebust {
4978dd37758STrond Myklebust 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
4988dd37758STrond Myklebust 		return;
4998dd37758STrond Myklebust 	nfs_list_remove_request(req);
500ea2cf228SFred Isaman 	cinfo->mds->ncommit--;
5018dd37758STrond Myklebust }
5028dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
5038dd37758STrond Myklebust 
504ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
505ea2cf228SFred Isaman 				      struct inode *inode)
506ea2cf228SFred Isaman {
507ea2cf228SFred Isaman 	cinfo->lock = &inode->i_lock;
508ea2cf228SFred Isaman 	cinfo->mds = &NFS_I(inode)->commit_info;
509ea2cf228SFred Isaman 	cinfo->ds = pnfs_get_ds_info(inode);
510b359f9d0SFred Isaman 	cinfo->dreq = NULL;
511f453a54aSFred Isaman 	cinfo->completion_ops = &nfs_commit_completion_ops;
512ea2cf228SFred Isaman }
513ea2cf228SFred Isaman 
514ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
515ea2cf228SFred Isaman 		    struct inode *inode,
516ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq)
517ea2cf228SFred Isaman {
5181763da12SFred Isaman 	if (dreq)
5191763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
5201763da12SFred Isaman 	else
521ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(cinfo, inode);
522ea2cf228SFred Isaman }
523ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo);
5248dd37758STrond Myklebust 
5251da177e4SLinus Torvalds /*
5261da177e4SLinus Torvalds  * Add a request to the inode's commit list.
5271da177e4SLinus Torvalds  */
5281763da12SFred Isaman void
529ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
530ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
5311da177e4SLinus Torvalds {
532ea2cf228SFred Isaman 	if (pnfs_mark_request_commit(req, lseg, cinfo))
5338dd37758STrond Myklebust 		return;
534ea2cf228SFred Isaman 	nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo);
5351da177e4SLinus Torvalds }
5368e821cadSTrond Myklebust 
537d6d6dc7cSFred Isaman static void
538d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page)
539e468bae9STrond Myklebust {
540e468bae9STrond Myklebust 	dec_zone_page_state(page, NR_UNSTABLE_NFS);
541e468bae9STrond Myklebust 	dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
542e468bae9STrond Myklebust }
543d6d6dc7cSFred Isaman 
5448dd37758STrond Myklebust static void
545d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req)
546d6d6dc7cSFred Isaman {
5478dd37758STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
5488dd37758STrond Myklebust 		struct inode *inode = req->wb_context->dentry->d_inode;
549ea2cf228SFred Isaman 		struct nfs_commit_info cinfo;
550d6d6dc7cSFred Isaman 
551ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(&cinfo, inode);
552ea2cf228SFred Isaman 		if (!pnfs_clear_request_commit(req, &cinfo)) {
553ea2cf228SFred Isaman 			spin_lock(cinfo.lock);
554ea2cf228SFred Isaman 			nfs_request_remove_commit_list(req, &cinfo);
555ea2cf228SFred Isaman 			spin_unlock(cinfo.lock);
556d6d6dc7cSFred Isaman 		}
5578dd37758STrond Myklebust 		nfs_clear_page_commit(req->wb_page);
5588dd37758STrond Myklebust 	}
559e468bae9STrond Myklebust }
560e468bae9STrond Myklebust 
5618e821cadSTrond Myklebust static inline
5628e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
5638e821cadSTrond Myklebust {
564465d5243SFred Isaman 	if (data->verf.committed == NFS_DATA_SYNC)
565cd841605SFred Isaman 		return data->header->lseg == NULL;
5668e821cadSTrond Myklebust 	return data->verf.committed != NFS_FILE_SYNC;
5678e821cadSTrond Myklebust }
5688e821cadSTrond Myklebust 
5698e821cadSTrond Myklebust #else
57068cd6fa4SBryan Schumaker static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
57168cd6fa4SBryan Schumaker 				      struct inode *inode)
57268cd6fa4SBryan Schumaker {
57368cd6fa4SBryan Schumaker }
57468cd6fa4SBryan Schumaker 
57568cd6fa4SBryan Schumaker void nfs_init_cinfo(struct nfs_commit_info *cinfo,
57668cd6fa4SBryan Schumaker 		    struct inode *inode,
57768cd6fa4SBryan Schumaker 		    struct nfs_direct_req *dreq)
57868cd6fa4SBryan Schumaker {
57968cd6fa4SBryan Schumaker }
58068cd6fa4SBryan Schumaker 
5811763da12SFred Isaman void
582ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
583ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
5848e821cadSTrond Myklebust {
5858e821cadSTrond Myklebust }
5868e821cadSTrond Myklebust 
5878dd37758STrond Myklebust static void
588e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
589e468bae9STrond Myklebust {
590e468bae9STrond Myklebust }
591e468bae9STrond Myklebust 
5928e821cadSTrond Myklebust static inline
5938e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
5948e821cadSTrond Myklebust {
5958e821cadSTrond Myklebust 	return 0;
5968e821cadSTrond Myklebust }
5978e821cadSTrond Myklebust 
5981da177e4SLinus Torvalds #endif
5991da177e4SLinus Torvalds 
600061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
6016c75dc0dSFred Isaman {
602ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
6036c75dc0dSFred Isaman 	unsigned long bytes = 0;
6046c75dc0dSFred Isaman 
6056c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
6066c75dc0dSFred Isaman 		goto out;
607ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
6086c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
6096c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
6106c75dc0dSFred Isaman 		struct page *page = req->wb_page;
6116c75dc0dSFred Isaman 
6126c75dc0dSFred Isaman 		bytes += req->wb_bytes;
6136c75dc0dSFred Isaman 		nfs_list_remove_request(req);
6146c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
6156c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
6166c75dc0dSFred Isaman 			nfs_set_pageerror(page);
6176c75dc0dSFred Isaman 			nfs_context_set_write_error(req->wb_context, hdr->error);
6186c75dc0dSFred Isaman 			goto remove_req;
6196c75dc0dSFred Isaman 		}
6206c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
6216c75dc0dSFred Isaman 			nfs_mark_request_dirty(req);
6226c75dc0dSFred Isaman 			goto next;
6236c75dc0dSFred Isaman 		}
6246c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
625ea2cf228SFred Isaman 			nfs_mark_request_commit(req, hdr->lseg, &cinfo);
6266c75dc0dSFred Isaman 			goto next;
6276c75dc0dSFred Isaman 		}
6286c75dc0dSFred Isaman remove_req:
6296c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
6306c75dc0dSFred Isaman next:
6316c75dc0dSFred Isaman 		nfs_unlock_request(req);
6326c75dc0dSFred Isaman 		nfs_end_page_writeback(page);
6336c75dc0dSFred Isaman 	}
6346c75dc0dSFred Isaman out:
6356c75dc0dSFred Isaman 	hdr->release(hdr);
6366c75dc0dSFred Isaman }
6376c75dc0dSFred Isaman 
63847c62564STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
639ea2cf228SFred Isaman static unsigned long
640ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
641fb8a1f11STrond Myklebust {
642ea2cf228SFred Isaman 	return cinfo->mds->ncommit;
643fb8a1f11STrond Myklebust }
644fb8a1f11STrond Myklebust 
645ea2cf228SFred Isaman /* cinfo->lock held by caller */
6461763da12SFred Isaman int
647ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
648ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
649d6d6dc7cSFred Isaman {
650d6d6dc7cSFred Isaman 	struct nfs_page *req, *tmp;
651d6d6dc7cSFred Isaman 	int ret = 0;
652d6d6dc7cSFred Isaman 
653d6d6dc7cSFred Isaman 	list_for_each_entry_safe(req, tmp, src, wb_list) {
6548dd37758STrond Myklebust 		if (!nfs_lock_request(req))
6558dd37758STrond Myklebust 			continue;
656ea2cf228SFred Isaman 		if (cond_resched_lock(cinfo->lock))
6573b3be88dSTrond Myklebust 			list_safe_reset_next(req, tmp, wb_list);
658ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
6598dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
660d6d6dc7cSFred Isaman 		ret++;
6611763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
662d6d6dc7cSFred Isaman 			break;
663d6d6dc7cSFred Isaman 	}
664d6d6dc7cSFred Isaman 	return ret;
665d6d6dc7cSFred Isaman }
666d6d6dc7cSFred Isaman 
6671da177e4SLinus Torvalds /*
6681da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
6691da177e4SLinus Torvalds  * @inode: NFS inode to scan
670ea2cf228SFred Isaman  * @dst: mds destination list
671ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
6721da177e4SLinus Torvalds  *
6731da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
6741da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
6751da177e4SLinus Torvalds  */
6761763da12SFred Isaman int
677ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
678ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
6791da177e4SLinus Torvalds {
680d6d6dc7cSFred Isaman 	int ret = 0;
681fb8a1f11STrond Myklebust 
682ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
683ea2cf228SFred Isaman 	if (cinfo->mds->ncommit > 0) {
6848dd37758STrond Myklebust 		const int max = INT_MAX;
685d6d6dc7cSFred Isaman 
686ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
687ea2cf228SFred Isaman 					   cinfo, max);
688ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
689d6d6dc7cSFred Isaman 	}
690ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
691ff778d02STrond Myklebust 	return ret;
6921da177e4SLinus Torvalds }
693d6d6dc7cSFred Isaman 
694c42de9ddSTrond Myklebust #else
695ea2cf228SFred Isaman static unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
696fb8a1f11STrond Myklebust {
697fb8a1f11STrond Myklebust 	return 0;
698fb8a1f11STrond Myklebust }
699fb8a1f11STrond Myklebust 
7001763da12SFred Isaman int nfs_scan_commit(struct inode *inode, struct list_head *dst,
701ea2cf228SFred Isaman 		    struct nfs_commit_info *cinfo)
702c42de9ddSTrond Myklebust {
703c42de9ddSTrond Myklebust 	return 0;
704c42de9ddSTrond Myklebust }
7051da177e4SLinus Torvalds #endif
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds /*
708e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
709e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
7101da177e4SLinus Torvalds  *
711e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
712e7d39069STrond Myklebust  * to disk.
7131da177e4SLinus Torvalds  */
714e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
715e7d39069STrond Myklebust 		struct page *page,
716e7d39069STrond Myklebust 		unsigned int offset,
717e7d39069STrond Myklebust 		unsigned int bytes)
7181da177e4SLinus Torvalds {
719e7d39069STrond Myklebust 	struct nfs_page *req;
720e7d39069STrond Myklebust 	unsigned int rqend;
721e7d39069STrond Myklebust 	unsigned int end;
7221da177e4SLinus Torvalds 	int error;
723277459d2STrond Myklebust 
724e7d39069STrond Myklebust 	if (!PagePrivate(page))
725e7d39069STrond Myklebust 		return NULL;
726e7d39069STrond Myklebust 
727e7d39069STrond Myklebust 	end = offset + bytes;
728e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
729e7d39069STrond Myklebust 
730e7d39069STrond Myklebust 	for (;;) {
731e7d39069STrond Myklebust 		req = nfs_page_find_request_locked(page);
732e7d39069STrond Myklebust 		if (req == NULL)
733e7d39069STrond Myklebust 			goto out_unlock;
734e7d39069STrond Myklebust 
735e7d39069STrond Myklebust 		rqend = req->wb_offset + req->wb_bytes;
736e7d39069STrond Myklebust 		/*
737e7d39069STrond Myklebust 		 * Tell the caller to flush out the request if
738e7d39069STrond Myklebust 		 * the offsets are non-contiguous.
739e7d39069STrond Myklebust 		 * Note: nfs_flush_incompatible() will already
740e7d39069STrond Myklebust 		 * have flushed out requests having wrong owners.
741e7d39069STrond Myklebust 		 */
742e468bae9STrond Myklebust 		if (offset > rqend
743e7d39069STrond Myklebust 		    || end < req->wb_offset)
744e7d39069STrond Myklebust 			goto out_flushme;
745e7d39069STrond Myklebust 
7469994b62bSFred Isaman 		if (nfs_lock_request_dontget(req))
747e7d39069STrond Myklebust 			break;
748e7d39069STrond Myklebust 
749e7d39069STrond Myklebust 		/* The request is locked, so wait and then retry */
750587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
7511da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
7521da177e4SLinus Torvalds 		nfs_release_request(req);
753e7d39069STrond Myklebust 		if (error != 0)
754e7d39069STrond Myklebust 			goto out_err;
755e7d39069STrond Myklebust 		spin_lock(&inode->i_lock);
7561da177e4SLinus Torvalds 	}
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
7591da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
7601da177e4SLinus Torvalds 		req->wb_offset = offset;
7611da177e4SLinus Torvalds 		req->wb_pgbase = offset;
7621da177e4SLinus Torvalds 	}
7631da177e4SLinus Torvalds 	if (end > rqend)
7641da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
765e7d39069STrond Myklebust 	else
766e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
767e7d39069STrond Myklebust out_unlock:
768e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
769ca138f36SFred Isaman 	if (req)
7708dd37758STrond Myklebust 		nfs_clear_request_commit(req);
771e7d39069STrond Myklebust 	return req;
772e7d39069STrond Myklebust out_flushme:
773e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
774e7d39069STrond Myklebust 	nfs_release_request(req);
775e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
776e7d39069STrond Myklebust out_err:
777e7d39069STrond Myklebust 	return ERR_PTR(error);
778e7d39069STrond Myklebust }
7791da177e4SLinus Torvalds 
780e7d39069STrond Myklebust /*
781e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
782e7d39069STrond Myklebust  *
783e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
784e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
785e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
786e7d39069STrond Myklebust  */
787e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
788e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
789e7d39069STrond Myklebust {
790e7d39069STrond Myklebust 	struct inode *inode = page->mapping->host;
791e7d39069STrond Myklebust 	struct nfs_page	*req;
792e7d39069STrond Myklebust 
793e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
794e7d39069STrond Myklebust 	if (req != NULL)
795e7d39069STrond Myklebust 		goto out;
796e7d39069STrond Myklebust 	req = nfs_create_request(ctx, inode, page, offset, bytes);
797e7d39069STrond Myklebust 	if (IS_ERR(req))
798e7d39069STrond Myklebust 		goto out;
799d6d6dc7cSFred Isaman 	nfs_inode_add_request(inode, req);
800efc91ed0STrond Myklebust out:
80161e930a9STrond Myklebust 	return req;
8021da177e4SLinus Torvalds }
8031da177e4SLinus Torvalds 
804e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
805e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
806e7d39069STrond Myklebust {
807e7d39069STrond Myklebust 	struct nfs_page	*req;
808e7d39069STrond Myklebust 
809e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
810e7d39069STrond Myklebust 	if (IS_ERR(req))
811e7d39069STrond Myklebust 		return PTR_ERR(req);
812e7d39069STrond Myklebust 	/* Update file length */
813e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
814e7d39069STrond Myklebust 	nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
815a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
8169994b62bSFred Isaman 	nfs_unlock_request(req);
817e7d39069STrond Myklebust 	return 0;
818e7d39069STrond Myklebust }
819e7d39069STrond Myklebust 
8201da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
8211da177e4SLinus Torvalds {
822cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
8231da177e4SLinus Torvalds 	struct nfs_page	*req;
8241a54533eSTrond Myklebust 	int do_flush, status;
8251da177e4SLinus Torvalds 	/*
8261da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
8271da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
8281da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
8291da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
8301da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
8311da177e4SLinus Torvalds 	 * dropped page.
8321da177e4SLinus Torvalds 	 */
8331a54533eSTrond Myklebust 	do {
834277459d2STrond Myklebust 		req = nfs_page_find_request(page);
8351a54533eSTrond Myklebust 		if (req == NULL)
8361a54533eSTrond Myklebust 			return 0;
837f11ac8dbSTrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx ||
838f11ac8dbSTrond Myklebust 			req->wb_lock_context->lockowner != current->files ||
839f11ac8dbSTrond Myklebust 			req->wb_lock_context->pid != current->tgid;
8401da177e4SLinus Torvalds 		nfs_release_request(req);
8411a54533eSTrond Myklebust 		if (!do_flush)
8421a54533eSTrond Myklebust 			return 0;
843277459d2STrond Myklebust 		status = nfs_wb_page(page->mapping->host, page);
8441a54533eSTrond Myklebust 	} while (status == 0);
8451a54533eSTrond Myklebust 	return status;
8461da177e4SLinus Torvalds }
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds /*
8495d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
8505d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
8515d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
8525d47a356STrond Myklebust  */
8535d47a356STrond Myklebust static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
8545d47a356STrond Myklebust {
8555d47a356STrond Myklebust 	return PageUptodate(page) &&
8565d47a356STrond Myklebust 		!(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
8575d47a356STrond Myklebust }
8585d47a356STrond Myklebust 
8595d47a356STrond Myklebust /*
8601da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
8611da177e4SLinus Torvalds  *
8621da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
8631da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
8641da177e4SLinus Torvalds  */
8651da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
8661da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
8671da177e4SLinus Torvalds {
868cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
8691da177e4SLinus Torvalds 	struct inode	*inode = page->mapping->host;
8701da177e4SLinus Torvalds 	int		status = 0;
8711da177e4SLinus Torvalds 
87291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
87391d5b470SChuck Lever 
87448186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage(%s/%s %d@%lld)\n",
87501cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
87601cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name, count,
8770bbacc40SChuck Lever 		(long long)(page_offset(page) + offset));
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	/* If we're not using byte range locks, and we know the page
8805d47a356STrond Myklebust 	 * is up to date, it may be more efficient to extend the write
8815d47a356STrond Myklebust 	 * to cover the entire page in order to avoid fragmentation
8825d47a356STrond Myklebust 	 * inefficiencies.
8831da177e4SLinus Torvalds 	 */
8845d47a356STrond Myklebust 	if (nfs_write_pageuptodate(page, inode) &&
8855d47a356STrond Myklebust 			inode->i_flock == NULL &&
8866b2f3d1fSChristoph Hellwig 			!(file->f_flags & O_DSYNC)) {
88749a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
8881da177e4SLinus Torvalds 		offset = 0;
8891da177e4SLinus Torvalds 	}
8901da177e4SLinus Torvalds 
891e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
89203fa9e84STrond Myklebust 	if (status < 0)
89303fa9e84STrond Myklebust 		nfs_set_pageerror(page);
89459b7c05fSTrond Myklebust 	else
89559b7c05fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
8961da177e4SLinus Torvalds 
89748186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
8981da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
8991da177e4SLinus Torvalds 	return status;
9001da177e4SLinus Torvalds }
9011da177e4SLinus Torvalds 
9023ff7576dSTrond Myklebust static int flush_task_priority(int how)
9031da177e4SLinus Torvalds {
9041da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
9051da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
9061da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
9071da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
9081da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
9091da177e4SLinus Torvalds 	}
9101da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
9111da177e4SLinus Torvalds }
9121da177e4SLinus Torvalds 
913c5996c4eSFred Isaman int nfs_initiate_write(struct rpc_clnt *clnt,
914c5996c4eSFred Isaman 		       struct nfs_write_data *data,
915788e7a89STrond Myklebust 		       const struct rpc_call_ops *call_ops,
9161da177e4SLinus Torvalds 		       int how)
9171da177e4SLinus Torvalds {
918cd841605SFred Isaman 	struct inode *inode = data->header->inode;
9193ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
92007737691STrond Myklebust 	struct rpc_task *task;
921bdc7f021STrond Myklebust 	struct rpc_message msg = {
922bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
923bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
924cd841605SFred Isaman 		.rpc_cred = data->header->cred,
925bdc7f021STrond Myklebust 	};
92684115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
927d138d5d1SAndy Adamson 		.rpc_client = clnt,
92807737691STrond Myklebust 		.task = &data->task,
929bdc7f021STrond Myklebust 		.rpc_message = &msg,
93084115e1cSTrond Myklebust 		.callback_ops = call_ops,
93184115e1cSTrond Myklebust 		.callback_data = data,
932101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
9332c61be0aSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
9343ff7576dSTrond Myklebust 		.priority = priority,
93584115e1cSTrond Myklebust 	};
9362c61be0aSTrond Myklebust 	int ret = 0;
9371da177e4SLinus Torvalds 
938d138d5d1SAndy Adamson 	/* Set up the initial task struct.  */
939d138d5d1SAndy Adamson 	NFS_PROTO(inode)->write_setup(data, &msg);
940d138d5d1SAndy Adamson 
941d138d5d1SAndy Adamson 	dprintk("NFS: %5u initiated write call "
942d138d5d1SAndy Adamson 		"(req %s/%lld, %u bytes @ offset %llu)\n",
943d138d5d1SAndy Adamson 		data->task.tk_pid,
944d138d5d1SAndy Adamson 		inode->i_sb->s_id,
945d138d5d1SAndy Adamson 		(long long)NFS_FILEID(inode),
946d138d5d1SAndy Adamson 		data->args.count,
947d138d5d1SAndy Adamson 		(unsigned long long)data->args.offset);
948d138d5d1SAndy Adamson 
949d138d5d1SAndy Adamson 	task = rpc_run_task(&task_setup_data);
950d138d5d1SAndy Adamson 	if (IS_ERR(task)) {
951d138d5d1SAndy Adamson 		ret = PTR_ERR(task);
952d138d5d1SAndy Adamson 		goto out;
953d138d5d1SAndy Adamson 	}
954d138d5d1SAndy Adamson 	if (how & FLUSH_SYNC) {
955d138d5d1SAndy Adamson 		ret = rpc_wait_for_completion_task(task);
956d138d5d1SAndy Adamson 		if (ret == 0)
957d138d5d1SAndy Adamson 			ret = task->tk_status;
958d138d5d1SAndy Adamson 	}
959d138d5d1SAndy Adamson 	rpc_put_task(task);
960d138d5d1SAndy Adamson out:
961d138d5d1SAndy Adamson 	return ret;
962d138d5d1SAndy Adamson }
963a69aef14SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_write);
964d138d5d1SAndy Adamson 
965d138d5d1SAndy Adamson /*
966d138d5d1SAndy Adamson  * Set up the argument/result storage required for the RPC call.
967d138d5d1SAndy Adamson  */
9686c75dc0dSFred Isaman static void nfs_write_rpcsetup(struct nfs_write_data *data,
969d138d5d1SAndy Adamson 		unsigned int count, unsigned int offset,
970ea2cf228SFred Isaman 		int how, struct nfs_commit_info *cinfo)
971d138d5d1SAndy Adamson {
9726c75dc0dSFred Isaman 	struct nfs_page *req = data->header->req;
973d138d5d1SAndy Adamson 
9741da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
9751da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
9761da177e4SLinus Torvalds 
9776c75dc0dSFred Isaman 	data->args.fh     = NFS_FH(data->header->inode);
9781da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
9792bea038cSBoaz Harrosh 	/* pnfs_set_layoutcommit needs this */
9802bea038cSBoaz Harrosh 	data->mds_offset = data->args.offset;
9811da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
98230dd374fSFred Isaman 	data->args.pages  = data->pages.pagevec;
9831da177e4SLinus Torvalds 	data->args.count  = count;
984383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
985f11ac8dbSTrond Myklebust 	data->args.lock_context = req->wb_lock_context;
986bdc7f021STrond Myklebust 	data->args.stable  = NFS_UNSTABLE;
98787ed5eb4STrond Myklebust 	switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
98887ed5eb4STrond Myklebust 	case 0:
98987ed5eb4STrond Myklebust 		break;
99087ed5eb4STrond Myklebust 	case FLUSH_COND_STABLE:
991ea2cf228SFred Isaman 		if (nfs_reqs_to_commit(cinfo))
99287ed5eb4STrond Myklebust 			break;
99387ed5eb4STrond Myklebust 	default:
994bdc7f021STrond Myklebust 		data->args.stable = NFS_FILE_SYNC;
995bdc7f021STrond Myklebust 	}
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
9981da177e4SLinus Torvalds 	data->res.count   = count;
9991da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
10000e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
10016e4efd56STrond Myklebust }
10021da177e4SLinus Torvalds 
10036e4efd56STrond Myklebust static int nfs_do_write(struct nfs_write_data *data,
10046e4efd56STrond Myklebust 		const struct rpc_call_ops *call_ops,
10056e4efd56STrond Myklebust 		int how)
10066e4efd56STrond Myklebust {
1007cd841605SFred Isaman 	struct inode *inode = data->header->inode;
10080382b744SAndy Adamson 
1009c5996c4eSFred Isaman 	return nfs_initiate_write(NFS_CLIENT(inode), data, call_ops, how);
10101da177e4SLinus Torvalds }
10111da177e4SLinus Torvalds 
1012275acaafSTrond Myklebust static int nfs_do_multiple_writes(struct list_head *head,
1013275acaafSTrond Myklebust 		const struct rpc_call_ops *call_ops,
1014275acaafSTrond Myklebust 		int how)
1015275acaafSTrond Myklebust {
1016275acaafSTrond Myklebust 	struct nfs_write_data *data;
1017275acaafSTrond Myklebust 	int ret = 0;
1018275acaafSTrond Myklebust 
1019275acaafSTrond Myklebust 	while (!list_empty(head)) {
1020275acaafSTrond Myklebust 		int ret2;
1021275acaafSTrond Myklebust 
10226c75dc0dSFred Isaman 		data = list_first_entry(head, struct nfs_write_data, list);
1023275acaafSTrond Myklebust 		list_del_init(&data->list);
1024275acaafSTrond Myklebust 
1025dce81290STrond Myklebust 		ret2 = nfs_do_write(data, call_ops, how);
1026275acaafSTrond Myklebust 		 if (ret == 0)
1027275acaafSTrond Myklebust 			 ret = ret2;
1028275acaafSTrond Myklebust 	}
1029275acaafSTrond Myklebust 	return ret;
1030275acaafSTrond Myklebust }
1031275acaafSTrond Myklebust 
10326d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
10336d884e8fSFred  * call this on each, which will prepare them to be retried on next
10346d884e8fSFred  * writeback using standard nfs.
10356d884e8fSFred  */
10366d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
10376d884e8fSFred {
1038a6305ddbSTrond Myklebust 	struct page *page = req->wb_page;
1039a6305ddbSTrond Myklebust 
10406d884e8fSFred 	nfs_mark_request_dirty(req);
10419994b62bSFred Isaman 	nfs_unlock_request(req);
1042a6305ddbSTrond Myklebust 	nfs_end_page_writeback(page);
10436d884e8fSFred }
10446d884e8fSFred 
1045061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head)
10466c75dc0dSFred Isaman {
10476c75dc0dSFred Isaman 	struct nfs_page	*req;
10486c75dc0dSFred Isaman 
10496c75dc0dSFred Isaman 	while (!list_empty(head)) {
10506c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
10516c75dc0dSFred Isaman 		nfs_list_remove_request(req);
10526c75dc0dSFred Isaman 		nfs_redirty_request(req);
10536c75dc0dSFred Isaman 	}
10546c75dc0dSFred Isaman }
10556c75dc0dSFred Isaman 
1056061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1057061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1058061ae2edSFred Isaman 	.completion = nfs_write_completion,
1059061ae2edSFred Isaman };
1060061ae2edSFred Isaman 
1061*25b11dcdSTrond Myklebust static void nfs_flush_error(struct nfs_pageio_descriptor *desc,
1062*25b11dcdSTrond Myklebust 		struct nfs_pgio_header *hdr)
1063*25b11dcdSTrond Myklebust {
1064*25b11dcdSTrond Myklebust 	set_bit(NFS_IOHDR_REDO, &hdr->flags);
1065*25b11dcdSTrond Myklebust 	while (!list_empty(&hdr->rpc_list)) {
1066*25b11dcdSTrond Myklebust 		struct nfs_write_data *data = list_first_entry(&hdr->rpc_list,
1067*25b11dcdSTrond Myklebust 				struct nfs_write_data, list);
1068*25b11dcdSTrond Myklebust 		list_del(&data->list);
1069*25b11dcdSTrond Myklebust 		nfs_writedata_release(data);
1070*25b11dcdSTrond Myklebust 	}
1071*25b11dcdSTrond Myklebust 	desc->pg_completion_ops->error_cleanup(&desc->pg_list);
1072*25b11dcdSTrond Myklebust }
1073*25b11dcdSTrond Myklebust 
10741da177e4SLinus Torvalds /*
10751da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
10761da177e4SLinus Torvalds  * contiguous dirty area on one page.
10771da177e4SLinus Torvalds  */
10786c75dc0dSFred Isaman static int nfs_flush_multi(struct nfs_pageio_descriptor *desc,
10796c75dc0dSFred Isaman 			   struct nfs_pgio_header *hdr)
10801da177e4SLinus Torvalds {
10816c75dc0dSFred Isaman 	struct nfs_page *req = hdr->req;
10821da177e4SLinus Torvalds 	struct page *page = req->wb_page;
10831da177e4SLinus Torvalds 	struct nfs_write_data *data;
1084d097971dSTrond Myklebust 	size_t wsize = desc->pg_bsize, nbytes;
1085e9f7bee1STrond Myklebust 	unsigned int offset;
10861da177e4SLinus Torvalds 	int requests = 0;
1087ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
10881da177e4SLinus Torvalds 
1089ea2cf228SFred Isaman 	nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
10901da177e4SLinus Torvalds 
1091b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1092ea2cf228SFred Isaman 	    (desc->pg_moreio || nfs_reqs_to_commit(&cinfo) ||
1093b31268acSTrond Myklebust 	     desc->pg_count > wsize))
1094b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1095b31268acSTrond Myklebust 
1096b31268acSTrond Myklebust 
1097275acaafSTrond Myklebust 	offset = 0;
1098c76069bdSFred Isaman 	nbytes = desc->pg_count;
1099e9f7bee1STrond Myklebust 	do {
1100e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
1101e9f7bee1STrond Myklebust 
11026c75dc0dSFred Isaman 		data = nfs_writedata_alloc(hdr, 1);
1103*25b11dcdSTrond Myklebust 		if (!data) {
1104*25b11dcdSTrond Myklebust 			nfs_flush_error(desc, hdr);
1105*25b11dcdSTrond Myklebust 			return -ENOMEM;
1106*25b11dcdSTrond Myklebust 		}
110730dd374fSFred Isaman 		data->pages.pagevec[0] = page;
1108ea2cf228SFred Isaman 		nfs_write_rpcsetup(data, len, offset, desc->pg_ioflags, &cinfo);
11096c75dc0dSFred Isaman 		list_add(&data->list, &hdr->rpc_list);
11101da177e4SLinus Torvalds 		requests++;
1111e9f7bee1STrond Myklebust 		nbytes -= len;
1112275acaafSTrond Myklebust 		offset += len;
1113e9f7bee1STrond Myklebust 	} while (nbytes != 0);
1114*25b11dcdSTrond Myklebust 	nfs_list_remove_request(req);
1115*25b11dcdSTrond Myklebust 	nfs_list_add_request(req, &hdr->pages);
11166c75dc0dSFred Isaman 	desc->pg_rpc_callops = &nfs_write_common_ops;
1117*25b11dcdSTrond Myklebust 	return 0;
11181da177e4SLinus Torvalds }
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds /*
11211da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
11221da177e4SLinus Torvalds  * The page must have been locked by the caller.
11231da177e4SLinus Torvalds  *
11241da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
11251da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
11261da177e4SLinus Torvalds  * that has been written but not committed.
11271da177e4SLinus Torvalds  */
11286c75dc0dSFred Isaman static int nfs_flush_one(struct nfs_pageio_descriptor *desc,
11296c75dc0dSFred Isaman 			 struct nfs_pgio_header *hdr)
11301da177e4SLinus Torvalds {
11311da177e4SLinus Torvalds 	struct nfs_page		*req;
11321da177e4SLinus Torvalds 	struct page		**pages;
11331da177e4SLinus Torvalds 	struct nfs_write_data	*data;
1134c76069bdSFred Isaman 	struct list_head *head = &desc->pg_list;
1135ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
11361da177e4SLinus Torvalds 
11376c75dc0dSFred Isaman 	data = nfs_writedata_alloc(hdr, nfs_page_array_len(desc->pg_base,
1138c76069bdSFred Isaman 							   desc->pg_count));
11396c75dc0dSFred Isaman 	if (!data) {
1140*25b11dcdSTrond Myklebust 		nfs_flush_error(desc, hdr);
1141*25b11dcdSTrond Myklebust 		return -ENOMEM;
114244b83799SFred Isaman 	}
11436c75dc0dSFred Isaman 
1144ea2cf228SFred Isaman 	nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
114530dd374fSFred Isaman 	pages = data->pages.pagevec;
11461da177e4SLinus Torvalds 	while (!list_empty(head)) {
11471da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
11481da177e4SLinus Torvalds 		nfs_list_remove_request(req);
11496c75dc0dSFred Isaman 		nfs_list_add_request(req, &hdr->pages);
11501da177e4SLinus Torvalds 		*pages++ = req->wb_page;
11511da177e4SLinus Torvalds 	}
11521da177e4SLinus Torvalds 
1153b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1154ea2cf228SFred Isaman 	    (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
1155b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1156b31268acSTrond Myklebust 
11571da177e4SLinus Torvalds 	/* Set up the argument struct */
1158ea2cf228SFred Isaman 	nfs_write_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo);
11596c75dc0dSFred Isaman 	list_add(&data->list, &hdr->rpc_list);
11606c75dc0dSFred Isaman 	desc->pg_rpc_callops = &nfs_write_common_ops;
1161*25b11dcdSTrond Myklebust 	return 0;
11621da177e4SLinus Torvalds }
11631da177e4SLinus Torvalds 
11646c75dc0dSFred Isaman int nfs_generic_flush(struct nfs_pageio_descriptor *desc,
11656c75dc0dSFred Isaman 		      struct nfs_pgio_header *hdr)
1166dce81290STrond Myklebust {
1167dce81290STrond Myklebust 	if (desc->pg_bsize < PAGE_CACHE_SIZE)
11686c75dc0dSFred Isaman 		return nfs_flush_multi(desc, hdr);
11696c75dc0dSFred Isaman 	return nfs_flush_one(desc, hdr);
1170dce81290STrond Myklebust }
1171dce81290STrond Myklebust 
1172dce81290STrond Myklebust static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
11731751c363STrond Myklebust {
11746c75dc0dSFred Isaman 	struct nfs_write_header *whdr;
11756c75dc0dSFred Isaman 	struct nfs_pgio_header *hdr;
1176275acaafSTrond Myklebust 	int ret;
1177275acaafSTrond Myklebust 
11786c75dc0dSFred Isaman 	whdr = nfs_writehdr_alloc();
11796c75dc0dSFred Isaman 	if (!whdr) {
11809b5415b5STrond Myklebust 		desc->pg_completion_ops->error_cleanup(&desc->pg_list);
11816c75dc0dSFred Isaman 		return -ENOMEM;
11826c75dc0dSFred Isaman 	}
11836c75dc0dSFred Isaman 	hdr = &whdr->header;
11846c75dc0dSFred Isaman 	nfs_pgheader_init(desc, hdr, nfs_writehdr_free);
11856c75dc0dSFred Isaman 	atomic_inc(&hdr->refcnt);
11866c75dc0dSFred Isaman 	ret = nfs_generic_flush(desc, hdr);
1187275acaafSTrond Myklebust 	if (ret == 0)
11886c75dc0dSFred Isaman 		ret = nfs_do_multiple_writes(&hdr->rpc_list,
11896c75dc0dSFred Isaman 					     desc->pg_rpc_callops,
1190dce81290STrond Myklebust 					     desc->pg_ioflags);
11916c75dc0dSFred Isaman 	if (atomic_dec_and_test(&hdr->refcnt))
1192061ae2edSFred Isaman 		hdr->completion_ops->completion(hdr);
1193275acaafSTrond Myklebust 	return ret;
11941751c363STrond Myklebust }
11951751c363STrond Myklebust 
11961751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_write_ops = {
11971751c363STrond Myklebust 	.pg_test = nfs_generic_pg_test,
11981751c363STrond Myklebust 	.pg_doio = nfs_generic_pg_writepages,
11991751c363STrond Myklebust };
12001751c363STrond Myklebust 
1201e2fecb21STrond Myklebust void nfs_pageio_init_write_mds(struct nfs_pageio_descriptor *pgio,
1202061ae2edSFred Isaman 			       struct inode *inode, int ioflags,
1203061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
12041751c363STrond Myklebust {
1205061ae2edSFred Isaman 	nfs_pageio_init(pgio, inode, &nfs_pageio_write_ops, compl_ops,
12061751c363STrond Myklebust 				NFS_SERVER(inode)->wsize, ioflags);
12071751c363STrond Myklebust }
12081751c363STrond Myklebust 
1209dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1210dce81290STrond Myklebust {
1211dce81290STrond Myklebust 	pgio->pg_ops = &nfs_pageio_write_ops;
1212dce81290STrond Myklebust 	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1213dce81290STrond Myklebust }
12141f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1215dce81290STrond Myklebust 
12161763da12SFred Isaman void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1217061ae2edSFred Isaman 			   struct inode *inode, int ioflags,
1218061ae2edSFred Isaman 			   const struct nfs_pgio_completion_ops *compl_ops)
12191da177e4SLinus Torvalds {
1220061ae2edSFred Isaman 	if (!pnfs_pageio_init_write(pgio, inode, ioflags, compl_ops))
1221061ae2edSFred Isaman 		nfs_pageio_init_write_mds(pgio, inode, ioflags, compl_ops);
12221da177e4SLinus Torvalds }
12231da177e4SLinus Torvalds 
1224def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata)
1225def6ed7eSAndy Adamson {
1226def6ed7eSAndy Adamson 	struct nfs_write_data *data = calldata;
1227cd841605SFred Isaman 	NFS_PROTO(data->header->inode)->write_rpc_prepare(task, data);
1228def6ed7eSAndy Adamson }
1229def6ed7eSAndy Adamson 
12300b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
12310b7c0153SFred Isaman {
12320b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
12330b7c0153SFred Isaman 
12340b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
12350b7c0153SFred Isaman }
12360b7c0153SFred Isaman 
12371da177e4SLinus Torvalds /*
12381da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
12391da177e4SLinus Torvalds  *
12401da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
12411da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
12421da177e4SLinus Torvalds  *	  as the page has a write request pending.
12431da177e4SLinus Torvalds  */
12446c75dc0dSFred Isaman static void nfs_writeback_done_common(struct rpc_task *task, void *calldata)
12451da177e4SLinus Torvalds {
1246788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
12471da177e4SLinus Torvalds 
1248c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
1249c9d8f89dSTrond Myklebust }
1250c9d8f89dSTrond Myklebust 
12516c75dc0dSFred Isaman static void nfs_writeback_release_common(void *calldata)
1252c9d8f89dSTrond Myklebust {
1253c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1254cd841605SFred Isaman 	struct nfs_pgio_header *hdr = data->header;
1255e2fecb21STrond Myklebust 	int status = data->task.tk_status;
12566c75dc0dSFred Isaman 	struct nfs_page *req = hdr->req;
1257788e7a89STrond Myklebust 
12586c75dc0dSFred Isaman 	if ((status >= 0) && nfs_write_need_commit(data)) {
12596c75dc0dSFred Isaman 		spin_lock(&hdr->lock);
12606c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags))
12616c75dc0dSFred Isaman 			; /* Do nothing */
12626c75dc0dSFred Isaman 		else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags))
12631da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
12646c75dc0dSFred Isaman 		else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf)))
12656c75dc0dSFred Isaman 			set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags);
12666c75dc0dSFred Isaman 		spin_unlock(&hdr->lock);
12671da177e4SLinus Torvalds 	}
1268cd841605SFred Isaman 	nfs_writedata_release(data);
12691da177e4SLinus Torvalds }
12701da177e4SLinus Torvalds 
12716c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops = {
1272def6ed7eSAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
12736c75dc0dSFred Isaman 	.rpc_call_done = nfs_writeback_done_common,
12746c75dc0dSFred Isaman 	.rpc_release = nfs_writeback_release_common,
1275788e7a89STrond Myklebust };
1276788e7a89STrond Myklebust 
1277788e7a89STrond Myklebust 
12781da177e4SLinus Torvalds /*
12791da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
12801da177e4SLinus Torvalds  */
128113602896SFred Isaman void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
12821da177e4SLinus Torvalds {
12831da177e4SLinus Torvalds 	struct nfs_writeargs	*argp = &data->args;
12841da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
1285cd841605SFred Isaman 	struct inode		*inode = data->header->inode;
1286788e7a89STrond Myklebust 	int status;
12871da177e4SLinus Torvalds 
1288a3f565b1SChuck Lever 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
12891da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
12901da177e4SLinus Torvalds 
1291f551e44fSChuck Lever 	/*
1292f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1293f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1294f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1295f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1296f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1297f551e44fSChuck Lever 	 */
1298cd841605SFred Isaman 	status = NFS_PROTO(inode)->write_done(task, data);
1299788e7a89STrond Myklebust 	if (status != 0)
130013602896SFred Isaman 		return;
1301cd841605SFred Isaman 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
130291d5b470SChuck Lever 
13031da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
13041da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
13051da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
13061da177e4SLinus Torvalds 		 * commit data to stable storage even though we
13071da177e4SLinus Torvalds 		 * requested it.
13081da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
13091da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
13101da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
13111da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
13121da177e4SLinus Torvalds 		 */
13131da177e4SLinus Torvalds 		static unsigned long    complain;
13141da177e4SLinus Torvalds 
1315a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
13161da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
13171da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
13181da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1319cd841605SFred Isaman 				NFS_SERVER(inode)->nfs_client->cl_hostname,
13201da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
13211da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
13221da177e4SLinus Torvalds 		}
13231da177e4SLinus Torvalds 	}
13241da177e4SLinus Torvalds #endif
13256c75dc0dSFred Isaman 	if (task->tk_status < 0)
13266c75dc0dSFred Isaman 		nfs_set_pgio_error(data->header, task->tk_status, argp->offset);
13276c75dc0dSFred Isaman 	else if (resp->count < argp->count) {
13281da177e4SLinus Torvalds 		static unsigned long    complain;
13291da177e4SLinus Torvalds 
13306c75dc0dSFred Isaman 		/* This a short write! */
1331cd841605SFred Isaman 		nfs_inc_stats(inode, NFSIOS_SHORTWRITE);
133291d5b470SChuck Lever 
13331da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
13346c75dc0dSFred Isaman 		if (resp->count == 0) {
13356c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
13366c75dc0dSFred Isaman 				printk(KERN_WARNING
13376c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
13386c75dc0dSFred Isaman 				       argp->count);
13396c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
13406c75dc0dSFred Isaman 			}
13416c75dc0dSFred Isaman 			nfs_set_pgio_error(data->header, -EIO, argp->offset);
13426c75dc0dSFred Isaman 			task->tk_status = -EIO;
13436c75dc0dSFred Isaman 			return;
13446c75dc0dSFred Isaman 		}
13451da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
13461da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
13471da177e4SLinus Torvalds 			/* Resend from where the server left off */
1348a69aef14SFred Isaman 			data->mds_offset += resp->count;
13491da177e4SLinus Torvalds 			argp->offset += resp->count;
13501da177e4SLinus Torvalds 			argp->pgbase += resp->count;
13511da177e4SLinus Torvalds 			argp->count -= resp->count;
13521da177e4SLinus Torvalds 		} else {
13531da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
13541da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
13551da177e4SLinus Torvalds 			 */
13561da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
13571da177e4SLinus Torvalds 		}
1358d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
13591da177e4SLinus Torvalds 	}
13601da177e4SLinus Torvalds }
13611da177e4SLinus Torvalds 
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
136471d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
136571d0a611STrond Myklebust {
1366b8413f98STrond Myklebust 	int ret;
1367b8413f98STrond Myklebust 
136871d0a611STrond Myklebust 	if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
136971d0a611STrond Myklebust 		return 1;
1370b8413f98STrond Myklebust 	if (!may_wait)
137171d0a611STrond Myklebust 		return 0;
1372b8413f98STrond Myklebust 	ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1373b8413f98STrond Myklebust 				NFS_INO_COMMIT,
1374b8413f98STrond Myklebust 				nfs_wait_bit_killable,
1375b8413f98STrond Myklebust 				TASK_KILLABLE);
1376b8413f98STrond Myklebust 	return (ret < 0) ? ret : 1;
137771d0a611STrond Myklebust }
137871d0a611STrond Myklebust 
1379f453a54aSFred Isaman static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
138071d0a611STrond Myklebust {
138171d0a611STrond Myklebust 	clear_bit(NFS_INO_COMMIT, &nfsi->flags);
138271d0a611STrond Myklebust 	smp_mb__after_clear_bit();
138371d0a611STrond Myklebust 	wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
138471d0a611STrond Myklebust }
138571d0a611STrond Myklebust 
13860b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
13871da177e4SLinus Torvalds {
13880b7c0153SFred Isaman 	put_nfs_open_context(data->context);
13890b7c0153SFred Isaman 	nfs_commit_free(data);
13901da177e4SLinus Torvalds }
1391e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
13921da177e4SLinus Torvalds 
13930b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
13949ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
1395788e7a89STrond Myklebust 			int how)
13961da177e4SLinus Torvalds {
139707737691STrond Myklebust 	struct rpc_task *task;
13989ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1399bdc7f021STrond Myklebust 	struct rpc_message msg = {
1400bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1401bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
14029ace33cdSFred Isaman 		.rpc_cred = data->cred,
1403bdc7f021STrond Myklebust 	};
140484115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
140507737691STrond Myklebust 		.task = &data->task,
14069ace33cdSFred Isaman 		.rpc_client = clnt,
1407bdc7f021STrond Myklebust 		.rpc_message = &msg,
14089ace33cdSFred Isaman 		.callback_ops = call_ops,
140984115e1cSTrond Myklebust 		.callback_data = data,
1410101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
14112c61be0aSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
14123ff7576dSTrond Myklebust 		.priority = priority,
141384115e1cSTrond Myklebust 	};
1414788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
14159ace33cdSFred Isaman 	NFS_PROTO(data->inode)->commit_setup(data, &msg);
14161da177e4SLinus Torvalds 
1417a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1418bdc7f021STrond Myklebust 
141907737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1420dbae4c73STrond Myklebust 	if (IS_ERR(task))
1421dbae4c73STrond Myklebust 		return PTR_ERR(task);
1422d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1423d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
142407737691STrond Myklebust 	rpc_put_task(task);
1425dbae4c73STrond Myklebust 	return 0;
14261da177e4SLinus Torvalds }
1427e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
14281da177e4SLinus Torvalds 
14291da177e4SLinus Torvalds /*
14309ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
14319ace33cdSFred Isaman  */
14320b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1433988b6dceSFred Isaman 		     struct list_head *head,
1434f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1435f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
14369ace33cdSFred Isaman {
14379ace33cdSFred Isaman 	struct nfs_page *first = nfs_list_entry(head->next);
14383d4ff43dSAl Viro 	struct inode *inode = first->wb_context->dentry->d_inode;
14399ace33cdSFred Isaman 
14409ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
14419ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
14429ace33cdSFred Isaman 
14439ace33cdSFred Isaman 	list_splice_init(head, &data->pages);
14449ace33cdSFred Isaman 
14459ace33cdSFred Isaman 	data->inode	  = inode;
14469ace33cdSFred Isaman 	data->cred	  = first->wb_context->cred;
1447988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
14489ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1449f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1450b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
14519ace33cdSFred Isaman 
14529ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
14539ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
14549ace33cdSFred Isaman 	data->args.offset = 0;
14559ace33cdSFred Isaman 	data->args.count  = 0;
14560b7c0153SFred Isaman 	data->context     = get_nfs_open_context(first->wb_context);
14579ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
14589ace33cdSFred Isaman 	data->res.verf    = &data->verf;
14599ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
14609ace33cdSFred Isaman }
1461e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
14629ace33cdSFred Isaman 
1463e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1464ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1465ea2cf228SFred Isaman 		      struct nfs_commit_info *cinfo)
146664bfeb49SFred Isaman {
146764bfeb49SFred Isaman 	struct nfs_page *req;
146864bfeb49SFred Isaman 
146964bfeb49SFred Isaman 	while (!list_empty(page_list)) {
147064bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
147164bfeb49SFred Isaman 		nfs_list_remove_request(req);
1472ea2cf228SFred Isaman 		nfs_mark_request_commit(req, lseg, cinfo);
147356f9cd68SFred Isaman 		if (!cinfo->dreq) {
147464bfeb49SFred Isaman 			dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
147564bfeb49SFred Isaman 			dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
147664bfeb49SFred Isaman 				     BDI_RECLAIMABLE);
147756f9cd68SFred Isaman 		}
14789994b62bSFred Isaman 		nfs_unlock_request(req);
147964bfeb49SFred Isaman 	}
148064bfeb49SFred Isaman }
1481e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
148264bfeb49SFred Isaman 
14839ace33cdSFred Isaman /*
14841da177e4SLinus Torvalds  * Commit dirty pages
14851da177e4SLinus Torvalds  */
14861da177e4SLinus Torvalds static int
1487ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1488ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
14891da177e4SLinus Torvalds {
14900b7c0153SFred Isaman 	struct nfs_commit_data	*data;
14911da177e4SLinus Torvalds 
1492c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
14931da177e4SLinus Torvalds 
14941da177e4SLinus Torvalds 	if (!data)
14951da177e4SLinus Torvalds 		goto out_bad;
14961da177e4SLinus Torvalds 
14971da177e4SLinus Torvalds 	/* Set up the argument struct */
1498f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
1499f453a54aSFred Isaman 	atomic_inc(&cinfo->mds->rpcs_out);
15000b7c0153SFred Isaman 	return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops, how);
15011da177e4SLinus Torvalds  out_bad:
1502ea2cf228SFred Isaman 	nfs_retry_commit(head, NULL, cinfo);
1503f453a54aSFred Isaman 	cinfo->completion_ops->error_cleanup(NFS_I(inode));
15041da177e4SLinus Torvalds 	return -ENOMEM;
15051da177e4SLinus Torvalds }
15061da177e4SLinus Torvalds 
15071da177e4SLinus Torvalds /*
15081da177e4SLinus Torvalds  * COMMIT call returned
15091da177e4SLinus Torvalds  */
1510788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
15111da177e4SLinus Torvalds {
15120b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
15131da177e4SLinus Torvalds 
1514a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
15151da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
15161da177e4SLinus Torvalds 
1517788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1518c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
1519c9d8f89dSTrond Myklebust }
1520c9d8f89dSTrond Myklebust 
1521f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1522c9d8f89dSTrond Myklebust {
1523c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1524c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1525f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
1526788e7a89STrond Myklebust 
15271da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
15281da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
15291da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1530d6d6dc7cSFred Isaman 		nfs_clear_page_commit(req->wb_page);
15311da177e4SLinus Torvalds 
153248186c7dSChuck Lever 		dprintk("NFS:       commit (%s/%lld %d@%lld)",
15333d4ff43dSAl Viro 			req->wb_context->dentry->d_sb->s_id,
15343d4ff43dSAl Viro 			(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
15351da177e4SLinus Torvalds 			req->wb_bytes,
15361da177e4SLinus Torvalds 			(long long)req_offset(req));
1537c9d8f89dSTrond Myklebust 		if (status < 0) {
1538c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
15391da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1540c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
15411da177e4SLinus Torvalds 			goto next;
15421da177e4SLinus Torvalds 		}
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
15451da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
15461da177e4SLinus Torvalds 		if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
15471da177e4SLinus Torvalds 			/* We have a match */
15481da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
15491da177e4SLinus Torvalds 			dprintk(" OK\n");
15501da177e4SLinus Torvalds 			goto next;
15511da177e4SLinus Torvalds 		}
15521da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
15531da177e4SLinus Torvalds 		dprintk(" mismatch\n");
15546d884e8fSFred 		nfs_mark_request_dirty(req);
15551da177e4SLinus Torvalds 	next:
15569994b62bSFred Isaman 		nfs_unlock_request(req);
15571da177e4SLinus Torvalds 	}
1558f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1559f453a54aSFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
1560f453a54aSFred Isaman 		nfs_commit_clear_lock(NFS_I(data->inode));
15615917ce84SFred Isaman }
15625917ce84SFred Isaman 
15635917ce84SFred Isaman static void nfs_commit_release(void *calldata)
15645917ce84SFred Isaman {
15650b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
15665917ce84SFred Isaman 
1567f453a54aSFred Isaman 	data->completion_ops->completion(data);
1568c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
15691da177e4SLinus Torvalds }
1570788e7a89STrond Myklebust 
1571788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
15720b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1573788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1574788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1575788e7a89STrond Myklebust };
15761da177e4SLinus Torvalds 
1577f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1578f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1579f453a54aSFred Isaman 	.error_cleanup = nfs_commit_clear_lock,
1580f453a54aSFred Isaman };
1581f453a54aSFred Isaman 
15821763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1583ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
158484c53ab5SFred Isaman {
158584c53ab5SFred Isaman 	int status;
158684c53ab5SFred Isaman 
1587ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
158884c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1589ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
159084c53ab5SFred Isaman 	return status;
159184c53ab5SFred Isaman }
159284c53ab5SFred Isaman 
1593b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
15941da177e4SLinus Torvalds {
15951da177e4SLinus Torvalds 	LIST_HEAD(head);
1596ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
159771d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1598b8413f98STrond Myklebust 	int res;
15991da177e4SLinus Torvalds 
1600b8413f98STrond Myklebust 	res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1601b8413f98STrond Myklebust 	if (res <= 0)
1602c5efa5fcSTrond Myklebust 		goto out_mark_dirty;
1603ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1604ea2cf228SFred Isaman 	res = nfs_scan_commit(inode, &head, &cinfo);
16051da177e4SLinus Torvalds 	if (res) {
1606a861a1e1SFred Isaman 		int error;
1607a861a1e1SFred Isaman 
1608ea2cf228SFred Isaman 		error = nfs_generic_commit_list(inode, &head, how, &cinfo);
16091da177e4SLinus Torvalds 		if (error < 0)
16101da177e4SLinus Torvalds 			return error;
1611b8413f98STrond Myklebust 		if (!may_wait)
1612b8413f98STrond Myklebust 			goto out_mark_dirty;
1613b8413f98STrond Myklebust 		error = wait_on_bit(&NFS_I(inode)->flags,
1614b8413f98STrond Myklebust 				NFS_INO_COMMIT,
161571d0a611STrond Myklebust 				nfs_wait_bit_killable,
161671d0a611STrond Myklebust 				TASK_KILLABLE);
1617b8413f98STrond Myklebust 		if (error < 0)
1618b8413f98STrond Myklebust 			return error;
161971d0a611STrond Myklebust 	} else
162071d0a611STrond Myklebust 		nfs_commit_clear_lock(NFS_I(inode));
1621c5efa5fcSTrond Myklebust 	return res;
1622c5efa5fcSTrond Myklebust 	/* Note: If we exit without ensuring that the commit is complete,
1623c5efa5fcSTrond Myklebust 	 * we must mark the inode as dirty. Otherwise, future calls to
1624c5efa5fcSTrond Myklebust 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1625c5efa5fcSTrond Myklebust 	 * that the data is on the disk.
1626c5efa5fcSTrond Myklebust 	 */
1627c5efa5fcSTrond Myklebust out_mark_dirty:
1628c5efa5fcSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
16291da177e4SLinus Torvalds 	return res;
16301da177e4SLinus Torvalds }
16318fc795f7STrond Myklebust 
16328fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
16338fc795f7STrond Myklebust {
1634420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1635420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1636420e3646STrond Myklebust 	int ret = 0;
16378fc795f7STrond Myklebust 
16383236c3e1SJeff Layton 	/* no commits means nothing needs to be done */
1639ea2cf228SFred Isaman 	if (!nfsi->commit_info.ncommit)
16403236c3e1SJeff Layton 		return ret;
16413236c3e1SJeff Layton 
1642a00dd6c0SJeff Layton 	if (wbc->sync_mode == WB_SYNC_NONE) {
1643a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1644a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1645420e3646STrond Myklebust 		 */
1646ea2cf228SFred Isaman 		if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1))
1647420e3646STrond Myklebust 			goto out_mark_dirty;
1648420e3646STrond Myklebust 
1649a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1650420e3646STrond Myklebust 		flags = 0;
1651a00dd6c0SJeff Layton 	}
1652a00dd6c0SJeff Layton 
1653420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1654420e3646STrond Myklebust 	if (ret >= 0) {
1655420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1656420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1657420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1658420e3646STrond Myklebust 			else
1659420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1660420e3646STrond Myklebust 		}
16618fc795f7STrond Myklebust 		return 0;
1662420e3646STrond Myklebust 	}
1663420e3646STrond Myklebust out_mark_dirty:
16648fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
16658fc795f7STrond Myklebust 	return ret;
16668fc795f7STrond Myklebust }
1667c63c7b05STrond Myklebust #else
16688fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
16698fc795f7STrond Myklebust {
16708fc795f7STrond Myklebust 	return 0;
16718fc795f7STrond Myklebust }
16721da177e4SLinus Torvalds #endif
16731da177e4SLinus Torvalds 
16748fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
16758fc795f7STrond Myklebust {
1676863a3c6cSAndy Adamson 	int ret;
1677863a3c6cSAndy Adamson 
1678863a3c6cSAndy Adamson 	ret = nfs_commit_unstable_pages(inode, wbc);
1679863a3c6cSAndy Adamson 	if (ret >= 0 && test_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(inode)->flags)) {
1680ef311537SAndy Adamson 		int status;
1681ef311537SAndy Adamson 		bool sync = true;
1682863a3c6cSAndy Adamson 
1683846d5a09SWu Fengguang 		if (wbc->sync_mode == WB_SYNC_NONE)
1684ef311537SAndy Adamson 			sync = false;
1685863a3c6cSAndy Adamson 
1686863a3c6cSAndy Adamson 		status = pnfs_layoutcommit_inode(inode, sync);
1687863a3c6cSAndy Adamson 		if (status < 0)
1688863a3c6cSAndy Adamson 			return status;
1689863a3c6cSAndy Adamson 	}
1690863a3c6cSAndy Adamson 	return ret;
16918fc795f7STrond Myklebust }
16928fc795f7STrond Myklebust 
1693acdc53b2STrond Myklebust /*
1694acdc53b2STrond Myklebust  * flush the inode to disk.
1695acdc53b2STrond Myklebust  */
1696acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
169734901f70STrond Myklebust {
169834901f70STrond Myklebust 	struct writeback_control wbc = {
169972cb77f4STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
170034901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
1701d7fb1207STrond Myklebust 		.range_start = 0,
1702d7fb1207STrond Myklebust 		.range_end = LLONG_MAX,
170334901f70STrond Myklebust 	};
170434901f70STrond Myklebust 
1705acdc53b2STrond Myklebust 	return sync_inode(inode, &wbc);
17061c75950bSTrond Myklebust }
17071c75950bSTrond Myklebust 
17081b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
17091b3b4a1aSTrond Myklebust {
17101b3b4a1aSTrond Myklebust 	struct nfs_page *req;
17111b3b4a1aSTrond Myklebust 	int ret = 0;
17121b3b4a1aSTrond Myklebust 
17131b3b4a1aSTrond Myklebust 	BUG_ON(!PageLocked(page));
17141b3b4a1aSTrond Myklebust 	for (;;) {
1715ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
17161b3b4a1aSTrond Myklebust 		req = nfs_page_find_request(page);
17171b3b4a1aSTrond Myklebust 		if (req == NULL)
17181b3b4a1aSTrond Myklebust 			break;
17191b3b4a1aSTrond Myklebust 		if (nfs_lock_request_dontget(req)) {
17208dd37758STrond Myklebust 			nfs_clear_request_commit(req);
17211b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
17221b3b4a1aSTrond Myklebust 			/*
17231b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
17241b3b4a1aSTrond Myklebust 			 * page as being dirty
17251b3b4a1aSTrond Myklebust 			 */
17261b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
17271b3b4a1aSTrond Myklebust 			nfs_unlock_request(req);
17281b3b4a1aSTrond Myklebust 			break;
17291b3b4a1aSTrond Myklebust 		}
17301b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
1731c9edda71STrond Myklebust 		nfs_release_request(req);
17321b3b4a1aSTrond Myklebust 		if (ret < 0)
1733c988950eSTrond Myklebust 			break;
17341b3b4a1aSTrond Myklebust 	}
17351b3b4a1aSTrond Myklebust 	return ret;
17361b3b4a1aSTrond Myklebust }
17371b3b4a1aSTrond Myklebust 
17381c75950bSTrond Myklebust /*
17391c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
17401c75950bSTrond Myklebust  */
17411c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
17421c75950bSTrond Myklebust {
17437f2f12d9STrond Myklebust 	loff_t range_start = page_offset(page);
17447f2f12d9STrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
17457f2f12d9STrond Myklebust 	struct writeback_control wbc = {
17467f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
17477f2f12d9STrond Myklebust 		.nr_to_write = 0,
17487f2f12d9STrond Myklebust 		.range_start = range_start,
17497f2f12d9STrond Myklebust 		.range_end = range_end,
17507f2f12d9STrond Myklebust 	};
17517f2f12d9STrond Myklebust 	int ret;
17527f2f12d9STrond Myklebust 
17530522f6adSTrond Myklebust 	for (;;) {
1754ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
17557f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
17567f2f12d9STrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
17577f2f12d9STrond Myklebust 			if (ret < 0)
17587f2f12d9STrond Myklebust 				goto out_error;
17590522f6adSTrond Myklebust 			continue;
17607f2f12d9STrond Myklebust 		}
17610522f6adSTrond Myklebust 		if (!PagePrivate(page))
17620522f6adSTrond Myklebust 			break;
17630522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
17647f2f12d9STrond Myklebust 		if (ret < 0)
17657f2f12d9STrond Myklebust 			goto out_error;
17667f2f12d9STrond Myklebust 	}
17677f2f12d9STrond Myklebust 	return 0;
17687f2f12d9STrond Myklebust out_error:
17697f2f12d9STrond Myklebust 	return ret;
17701c75950bSTrond Myklebust }
17711c75950bSTrond Myklebust 
1772074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
1773074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1774a6bc32b8SMel Gorman 		struct page *page, enum migrate_mode mode)
1775074cc1deSTrond Myklebust {
17762da95652SJeff Layton 	/*
17772da95652SJeff Layton 	 * If PagePrivate is set, then the page is currently associated with
17782da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
17792da95652SJeff Layton 	 *
17802da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
17812da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
17822da95652SJeff Layton 	 *        the page lock.
17832da95652SJeff Layton 	 */
17842da95652SJeff Layton 	if (PagePrivate(page))
17852da95652SJeff Layton 		return -EBUSY;
1786074cc1deSTrond Myklebust 
1787074cc1deSTrond Myklebust 	nfs_fscache_release_page(page, GFP_KERNEL);
1788074cc1deSTrond Myklebust 
1789a6bc32b8SMel Gorman 	return migrate_page(mapping, newpage, page, mode);
1790074cc1deSTrond Myklebust }
1791074cc1deSTrond Myklebust #endif
1792074cc1deSTrond Myklebust 
1793f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
17941da177e4SLinus Torvalds {
17951da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1796cd841605SFred Isaman 					     sizeof(struct nfs_write_header),
17971da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
179820c2df83SPaul Mundt 					     NULL);
17991da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
18001da177e4SLinus Torvalds 		return -ENOMEM;
18011da177e4SLinus Torvalds 
180293d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
18031da177e4SLinus Torvalds 						     nfs_wdata_cachep);
18041da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
18051da177e4SLinus Torvalds 		return -ENOMEM;
18061da177e4SLinus Torvalds 
18070b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
18080b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
18090b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
18100b7c0153SFred Isaman 					     NULL);
18110b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
18120b7c0153SFred Isaman 		return -ENOMEM;
18130b7c0153SFred Isaman 
181493d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
18151da177e4SLinus Torvalds 						      nfs_wdata_cachep);
18161da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
18171da177e4SLinus Torvalds 		return -ENOMEM;
18181da177e4SLinus Torvalds 
181989a09141SPeter Zijlstra 	/*
182089a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
182189a09141SPeter Zijlstra 	 *
182289a09141SPeter Zijlstra 	 *  64MB:    8192k
182389a09141SPeter Zijlstra 	 * 128MB:   11585k
182489a09141SPeter Zijlstra 	 * 256MB:   16384k
182589a09141SPeter Zijlstra 	 * 512MB:   23170k
182689a09141SPeter Zijlstra 	 *   1GB:   32768k
182789a09141SPeter Zijlstra 	 *   2GB:   46340k
182889a09141SPeter Zijlstra 	 *   4GB:   65536k
182989a09141SPeter Zijlstra 	 *   8GB:   92681k
183089a09141SPeter Zijlstra 	 *  16GB:  131072k
183189a09141SPeter Zijlstra 	 *
183289a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
183389a09141SPeter Zijlstra 	 * Limit the default to 256M
183489a09141SPeter Zijlstra 	 */
183589a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
183689a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
183789a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
183889a09141SPeter Zijlstra 
18391da177e4SLinus Torvalds 	return 0;
18401da177e4SLinus Torvalds }
18411da177e4SLinus Torvalds 
1842266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
18431da177e4SLinus Torvalds {
18441da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
18451da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
18461a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
18471da177e4SLinus Torvalds }
18481da177e4SLinus Torvalds 
1849