xref: /linux/fs/nfs/write.c (revision 1763da1234cba663b849476d451bdccac5147859)
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 
87*1763da12SFred 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 {
518*1763da12SFred Isaman 	if (dreq)
519*1763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
520*1763da12SFred 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  */
528*1763da12SFred 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
570*1763da12SFred Isaman void
571ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
572ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
5738e821cadSTrond Myklebust {
5748e821cadSTrond Myklebust }
5758e821cadSTrond Myklebust 
5768dd37758STrond Myklebust static void
577e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
578e468bae9STrond Myklebust {
579e468bae9STrond Myklebust }
580e468bae9STrond Myklebust 
5818e821cadSTrond Myklebust static inline
5828e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
5838e821cadSTrond Myklebust {
5848e821cadSTrond Myklebust 	return 0;
5858e821cadSTrond Myklebust }
5868e821cadSTrond Myklebust 
5871da177e4SLinus Torvalds #endif
5881da177e4SLinus Torvalds 
589061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
5906c75dc0dSFred Isaman {
591ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
5926c75dc0dSFred Isaman 	unsigned long bytes = 0;
5936c75dc0dSFred Isaman 
5946c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
5956c75dc0dSFred Isaman 		goto out;
596ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
5976c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
5986c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
5996c75dc0dSFred Isaman 		struct page *page = req->wb_page;
6006c75dc0dSFred Isaman 
6016c75dc0dSFred Isaman 		bytes += req->wb_bytes;
6026c75dc0dSFred Isaman 		nfs_list_remove_request(req);
6036c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
6046c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
6056c75dc0dSFred Isaman 			nfs_set_pageerror(page);
6066c75dc0dSFred Isaman 			nfs_context_set_write_error(req->wb_context, hdr->error);
6076c75dc0dSFred Isaman 			goto remove_req;
6086c75dc0dSFred Isaman 		}
6096c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
6106c75dc0dSFred Isaman 			nfs_mark_request_dirty(req);
6116c75dc0dSFred Isaman 			goto next;
6126c75dc0dSFred Isaman 		}
6136c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
614ea2cf228SFred Isaman 			nfs_mark_request_commit(req, hdr->lseg, &cinfo);
6156c75dc0dSFred Isaman 			goto next;
6166c75dc0dSFred Isaman 		}
6176c75dc0dSFred Isaman remove_req:
6186c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
6196c75dc0dSFred Isaman next:
6206c75dc0dSFred Isaman 		nfs_unlock_request(req);
6216c75dc0dSFred Isaman 		nfs_end_page_writeback(page);
6226c75dc0dSFred Isaman 	}
6236c75dc0dSFred Isaman out:
6246c75dc0dSFred Isaman 	hdr->release(hdr);
6256c75dc0dSFred Isaman }
6266c75dc0dSFred Isaman 
62747c62564STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
628ea2cf228SFred Isaman static unsigned long
629ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
630fb8a1f11STrond Myklebust {
631ea2cf228SFred Isaman 	return cinfo->mds->ncommit;
632fb8a1f11STrond Myklebust }
633fb8a1f11STrond Myklebust 
634ea2cf228SFred Isaman /* cinfo->lock held by caller */
635*1763da12SFred Isaman int
636ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
637ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
638d6d6dc7cSFred Isaman {
639d6d6dc7cSFred Isaman 	struct nfs_page *req, *tmp;
640d6d6dc7cSFred Isaman 	int ret = 0;
641d6d6dc7cSFred Isaman 
642d6d6dc7cSFred Isaman 	list_for_each_entry_safe(req, tmp, src, wb_list) {
6438dd37758STrond Myklebust 		if (!nfs_lock_request(req))
6448dd37758STrond Myklebust 			continue;
645ea2cf228SFred Isaman 		if (cond_resched_lock(cinfo->lock))
6463b3be88dSTrond Myklebust 			list_safe_reset_next(req, tmp, wb_list);
647ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
6488dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
649d6d6dc7cSFred Isaman 		ret++;
650*1763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
651d6d6dc7cSFred Isaman 			break;
652d6d6dc7cSFred Isaman 	}
653d6d6dc7cSFred Isaman 	return ret;
654d6d6dc7cSFred Isaman }
655d6d6dc7cSFred Isaman 
6561da177e4SLinus Torvalds /*
6571da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
6581da177e4SLinus Torvalds  * @inode: NFS inode to scan
659ea2cf228SFred Isaman  * @dst: mds destination list
660ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
6611da177e4SLinus Torvalds  *
6621da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
6631da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
6641da177e4SLinus Torvalds  */
665*1763da12SFred Isaman int
666ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
667ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
6681da177e4SLinus Torvalds {
669d6d6dc7cSFred Isaman 	int ret = 0;
670fb8a1f11STrond Myklebust 
671ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
672ea2cf228SFred Isaman 	if (cinfo->mds->ncommit > 0) {
6738dd37758STrond Myklebust 		const int max = INT_MAX;
674d6d6dc7cSFred Isaman 
675ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
676ea2cf228SFred Isaman 					   cinfo, max);
677ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
678d6d6dc7cSFred Isaman 	}
679ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
680ff778d02STrond Myklebust 	return ret;
6811da177e4SLinus Torvalds }
682d6d6dc7cSFred Isaman 
683c42de9ddSTrond Myklebust #else
684ea2cf228SFred Isaman static unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
685fb8a1f11STrond Myklebust {
686fb8a1f11STrond Myklebust 	return 0;
687fb8a1f11STrond Myklebust }
688fb8a1f11STrond Myklebust 
689*1763da12SFred Isaman int nfs_scan_commit(struct inode *inode, struct list_head *dst,
690ea2cf228SFred Isaman 		    struct nfs_commit_info *cinfo)
691c42de9ddSTrond Myklebust {
692c42de9ddSTrond Myklebust 	return 0;
693c42de9ddSTrond Myklebust }
6941da177e4SLinus Torvalds #endif
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds /*
697e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
698e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
6991da177e4SLinus Torvalds  *
700e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
701e7d39069STrond Myklebust  * to disk.
7021da177e4SLinus Torvalds  */
703e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
704e7d39069STrond Myklebust 		struct page *page,
705e7d39069STrond Myklebust 		unsigned int offset,
706e7d39069STrond Myklebust 		unsigned int bytes)
7071da177e4SLinus Torvalds {
708e7d39069STrond Myklebust 	struct nfs_page *req;
709e7d39069STrond Myklebust 	unsigned int rqend;
710e7d39069STrond Myklebust 	unsigned int end;
7111da177e4SLinus Torvalds 	int error;
712277459d2STrond Myklebust 
713e7d39069STrond Myklebust 	if (!PagePrivate(page))
714e7d39069STrond Myklebust 		return NULL;
715e7d39069STrond Myklebust 
716e7d39069STrond Myklebust 	end = offset + bytes;
717e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
718e7d39069STrond Myklebust 
719e7d39069STrond Myklebust 	for (;;) {
720e7d39069STrond Myklebust 		req = nfs_page_find_request_locked(page);
721e7d39069STrond Myklebust 		if (req == NULL)
722e7d39069STrond Myklebust 			goto out_unlock;
723e7d39069STrond Myklebust 
724e7d39069STrond Myklebust 		rqend = req->wb_offset + req->wb_bytes;
725e7d39069STrond Myklebust 		/*
726e7d39069STrond Myklebust 		 * Tell the caller to flush out the request if
727e7d39069STrond Myklebust 		 * the offsets are non-contiguous.
728e7d39069STrond Myklebust 		 * Note: nfs_flush_incompatible() will already
729e7d39069STrond Myklebust 		 * have flushed out requests having wrong owners.
730e7d39069STrond Myklebust 		 */
731e468bae9STrond Myklebust 		if (offset > rqend
732e7d39069STrond Myklebust 		    || end < req->wb_offset)
733e7d39069STrond Myklebust 			goto out_flushme;
734e7d39069STrond Myklebust 
7359994b62bSFred Isaman 		if (nfs_lock_request_dontget(req))
736e7d39069STrond Myklebust 			break;
737e7d39069STrond Myklebust 
738e7d39069STrond Myklebust 		/* The request is locked, so wait and then retry */
739587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
7401da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
7411da177e4SLinus Torvalds 		nfs_release_request(req);
742e7d39069STrond Myklebust 		if (error != 0)
743e7d39069STrond Myklebust 			goto out_err;
744e7d39069STrond Myklebust 		spin_lock(&inode->i_lock);
7451da177e4SLinus Torvalds 	}
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
7481da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
7491da177e4SLinus Torvalds 		req->wb_offset = offset;
7501da177e4SLinus Torvalds 		req->wb_pgbase = offset;
7511da177e4SLinus Torvalds 	}
7521da177e4SLinus Torvalds 	if (end > rqend)
7531da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
754e7d39069STrond Myklebust 	else
755e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
756e7d39069STrond Myklebust out_unlock:
757e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
758ca138f36SFred Isaman 	if (req)
7598dd37758STrond Myklebust 		nfs_clear_request_commit(req);
760e7d39069STrond Myklebust 	return req;
761e7d39069STrond Myklebust out_flushme:
762e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
763e7d39069STrond Myklebust 	nfs_release_request(req);
764e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
765e7d39069STrond Myklebust out_err:
766e7d39069STrond Myklebust 	return ERR_PTR(error);
767e7d39069STrond Myklebust }
7681da177e4SLinus Torvalds 
769e7d39069STrond Myklebust /*
770e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
771e7d39069STrond Myklebust  *
772e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
773e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
774e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
775e7d39069STrond Myklebust  */
776e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
777e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
778e7d39069STrond Myklebust {
779e7d39069STrond Myklebust 	struct inode *inode = page->mapping->host;
780e7d39069STrond Myklebust 	struct nfs_page	*req;
781e7d39069STrond Myklebust 
782e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
783e7d39069STrond Myklebust 	if (req != NULL)
784e7d39069STrond Myklebust 		goto out;
785e7d39069STrond Myklebust 	req = nfs_create_request(ctx, inode, page, offset, bytes);
786e7d39069STrond Myklebust 	if (IS_ERR(req))
787e7d39069STrond Myklebust 		goto out;
788d6d6dc7cSFred Isaman 	nfs_inode_add_request(inode, req);
789efc91ed0STrond Myklebust out:
79061e930a9STrond Myklebust 	return req;
7911da177e4SLinus Torvalds }
7921da177e4SLinus Torvalds 
793e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
794e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
795e7d39069STrond Myklebust {
796e7d39069STrond Myklebust 	struct nfs_page	*req;
797e7d39069STrond Myklebust 
798e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
799e7d39069STrond Myklebust 	if (IS_ERR(req))
800e7d39069STrond Myklebust 		return PTR_ERR(req);
801e7d39069STrond Myklebust 	/* Update file length */
802e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
803e7d39069STrond Myklebust 	nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
804a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
8059994b62bSFred Isaman 	nfs_unlock_request(req);
806e7d39069STrond Myklebust 	return 0;
807e7d39069STrond Myklebust }
808e7d39069STrond Myklebust 
8091da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
8101da177e4SLinus Torvalds {
811cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
8121da177e4SLinus Torvalds 	struct nfs_page	*req;
8131a54533eSTrond Myklebust 	int do_flush, status;
8141da177e4SLinus Torvalds 	/*
8151da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
8161da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
8171da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
8181da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
8191da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
8201da177e4SLinus Torvalds 	 * dropped page.
8211da177e4SLinus Torvalds 	 */
8221a54533eSTrond Myklebust 	do {
823277459d2STrond Myklebust 		req = nfs_page_find_request(page);
8241a54533eSTrond Myklebust 		if (req == NULL)
8251a54533eSTrond Myklebust 			return 0;
826f11ac8dbSTrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx ||
827f11ac8dbSTrond Myklebust 			req->wb_lock_context->lockowner != current->files ||
828f11ac8dbSTrond Myklebust 			req->wb_lock_context->pid != current->tgid;
8291da177e4SLinus Torvalds 		nfs_release_request(req);
8301a54533eSTrond Myklebust 		if (!do_flush)
8311a54533eSTrond Myklebust 			return 0;
832277459d2STrond Myklebust 		status = nfs_wb_page(page->mapping->host, page);
8331a54533eSTrond Myklebust 	} while (status == 0);
8341a54533eSTrond Myklebust 	return status;
8351da177e4SLinus Torvalds }
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds /*
8385d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
8395d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
8405d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
8415d47a356STrond Myklebust  */
8425d47a356STrond Myklebust static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
8435d47a356STrond Myklebust {
8445d47a356STrond Myklebust 	return PageUptodate(page) &&
8455d47a356STrond Myklebust 		!(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
8465d47a356STrond Myklebust }
8475d47a356STrond Myklebust 
8485d47a356STrond Myklebust /*
8491da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
8501da177e4SLinus Torvalds  *
8511da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
8521da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
8531da177e4SLinus Torvalds  */
8541da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
8551da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
8561da177e4SLinus Torvalds {
857cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
8581da177e4SLinus Torvalds 	struct inode	*inode = page->mapping->host;
8591da177e4SLinus Torvalds 	int		status = 0;
8601da177e4SLinus Torvalds 
86191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
86291d5b470SChuck Lever 
86348186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage(%s/%s %d@%lld)\n",
86401cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
86501cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name, count,
8660bbacc40SChuck Lever 		(long long)(page_offset(page) + offset));
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	/* If we're not using byte range locks, and we know the page
8695d47a356STrond Myklebust 	 * is up to date, it may be more efficient to extend the write
8705d47a356STrond Myklebust 	 * to cover the entire page in order to avoid fragmentation
8715d47a356STrond Myklebust 	 * inefficiencies.
8721da177e4SLinus Torvalds 	 */
8735d47a356STrond Myklebust 	if (nfs_write_pageuptodate(page, inode) &&
8745d47a356STrond Myklebust 			inode->i_flock == NULL &&
8756b2f3d1fSChristoph Hellwig 			!(file->f_flags & O_DSYNC)) {
87649a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
8771da177e4SLinus Torvalds 		offset = 0;
8781da177e4SLinus Torvalds 	}
8791da177e4SLinus Torvalds 
880e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
88103fa9e84STrond Myklebust 	if (status < 0)
88203fa9e84STrond Myklebust 		nfs_set_pageerror(page);
88359b7c05fSTrond Myklebust 	else
88459b7c05fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
8851da177e4SLinus Torvalds 
88648186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
8871da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
8881da177e4SLinus Torvalds 	return status;
8891da177e4SLinus Torvalds }
8901da177e4SLinus Torvalds 
8913ff7576dSTrond Myklebust static int flush_task_priority(int how)
8921da177e4SLinus Torvalds {
8931da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
8941da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
8951da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
8961da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
8971da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
8981da177e4SLinus Torvalds 	}
8991da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
9001da177e4SLinus Torvalds }
9011da177e4SLinus Torvalds 
902c5996c4eSFred Isaman int nfs_initiate_write(struct rpc_clnt *clnt,
903c5996c4eSFred Isaman 		       struct nfs_write_data *data,
904788e7a89STrond Myklebust 		       const struct rpc_call_ops *call_ops,
9051da177e4SLinus Torvalds 		       int how)
9061da177e4SLinus Torvalds {
907cd841605SFred Isaman 	struct inode *inode = data->header->inode;
9083ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
90907737691STrond Myklebust 	struct rpc_task *task;
910bdc7f021STrond Myklebust 	struct rpc_message msg = {
911bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
912bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
913cd841605SFred Isaman 		.rpc_cred = data->header->cred,
914bdc7f021STrond Myklebust 	};
91584115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
916d138d5d1SAndy Adamson 		.rpc_client = clnt,
91707737691STrond Myklebust 		.task = &data->task,
918bdc7f021STrond Myklebust 		.rpc_message = &msg,
91984115e1cSTrond Myklebust 		.callback_ops = call_ops,
92084115e1cSTrond Myklebust 		.callback_data = data,
921101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
9222c61be0aSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
9233ff7576dSTrond Myklebust 		.priority = priority,
92484115e1cSTrond Myklebust 	};
9252c61be0aSTrond Myklebust 	int ret = 0;
9261da177e4SLinus Torvalds 
927d138d5d1SAndy Adamson 	/* Set up the initial task struct.  */
928d138d5d1SAndy Adamson 	NFS_PROTO(inode)->write_setup(data, &msg);
929d138d5d1SAndy Adamson 
930d138d5d1SAndy Adamson 	dprintk("NFS: %5u initiated write call "
931d138d5d1SAndy Adamson 		"(req %s/%lld, %u bytes @ offset %llu)\n",
932d138d5d1SAndy Adamson 		data->task.tk_pid,
933d138d5d1SAndy Adamson 		inode->i_sb->s_id,
934d138d5d1SAndy Adamson 		(long long)NFS_FILEID(inode),
935d138d5d1SAndy Adamson 		data->args.count,
936d138d5d1SAndy Adamson 		(unsigned long long)data->args.offset);
937d138d5d1SAndy Adamson 
938d138d5d1SAndy Adamson 	task = rpc_run_task(&task_setup_data);
939d138d5d1SAndy Adamson 	if (IS_ERR(task)) {
940d138d5d1SAndy Adamson 		ret = PTR_ERR(task);
941d138d5d1SAndy Adamson 		goto out;
942d138d5d1SAndy Adamson 	}
943d138d5d1SAndy Adamson 	if (how & FLUSH_SYNC) {
944d138d5d1SAndy Adamson 		ret = rpc_wait_for_completion_task(task);
945d138d5d1SAndy Adamson 		if (ret == 0)
946d138d5d1SAndy Adamson 			ret = task->tk_status;
947d138d5d1SAndy Adamson 	}
948d138d5d1SAndy Adamson 	rpc_put_task(task);
949d138d5d1SAndy Adamson out:
950d138d5d1SAndy Adamson 	return ret;
951d138d5d1SAndy Adamson }
952a69aef14SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_write);
953d138d5d1SAndy Adamson 
954d138d5d1SAndy Adamson /*
955d138d5d1SAndy Adamson  * Set up the argument/result storage required for the RPC call.
956d138d5d1SAndy Adamson  */
9576c75dc0dSFred Isaman static void nfs_write_rpcsetup(struct nfs_write_data *data,
958d138d5d1SAndy Adamson 		unsigned int count, unsigned int offset,
959ea2cf228SFred Isaman 		int how, struct nfs_commit_info *cinfo)
960d138d5d1SAndy Adamson {
9616c75dc0dSFred Isaman 	struct nfs_page *req = data->header->req;
962d138d5d1SAndy Adamson 
9631da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
9641da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
9651da177e4SLinus Torvalds 
9666c75dc0dSFred Isaman 	data->args.fh     = NFS_FH(data->header->inode);
9671da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
9682bea038cSBoaz Harrosh 	/* pnfs_set_layoutcommit needs this */
9692bea038cSBoaz Harrosh 	data->mds_offset = data->args.offset;
9701da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
97130dd374fSFred Isaman 	data->args.pages  = data->pages.pagevec;
9721da177e4SLinus Torvalds 	data->args.count  = count;
973383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
974f11ac8dbSTrond Myklebust 	data->args.lock_context = req->wb_lock_context;
975bdc7f021STrond Myklebust 	data->args.stable  = NFS_UNSTABLE;
97687ed5eb4STrond Myklebust 	switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
97787ed5eb4STrond Myklebust 	case 0:
97887ed5eb4STrond Myklebust 		break;
97987ed5eb4STrond Myklebust 	case FLUSH_COND_STABLE:
980ea2cf228SFred Isaman 		if (nfs_reqs_to_commit(cinfo))
98187ed5eb4STrond Myklebust 			break;
98287ed5eb4STrond Myklebust 	default:
983bdc7f021STrond Myklebust 		data->args.stable = NFS_FILE_SYNC;
984bdc7f021STrond Myklebust 	}
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
9871da177e4SLinus Torvalds 	data->res.count   = count;
9881da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
9890e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
9906e4efd56STrond Myklebust }
9911da177e4SLinus Torvalds 
9926e4efd56STrond Myklebust static int nfs_do_write(struct nfs_write_data *data,
9936e4efd56STrond Myklebust 		const struct rpc_call_ops *call_ops,
9946e4efd56STrond Myklebust 		int how)
9956e4efd56STrond Myklebust {
996cd841605SFred Isaman 	struct inode *inode = data->header->inode;
9970382b744SAndy Adamson 
998c5996c4eSFred Isaman 	return nfs_initiate_write(NFS_CLIENT(inode), data, call_ops, how);
9991da177e4SLinus Torvalds }
10001da177e4SLinus Torvalds 
1001275acaafSTrond Myklebust static int nfs_do_multiple_writes(struct list_head *head,
1002275acaafSTrond Myklebust 		const struct rpc_call_ops *call_ops,
1003275acaafSTrond Myklebust 		int how)
1004275acaafSTrond Myklebust {
1005275acaafSTrond Myklebust 	struct nfs_write_data *data;
1006275acaafSTrond Myklebust 	int ret = 0;
1007275acaafSTrond Myklebust 
1008275acaafSTrond Myklebust 	while (!list_empty(head)) {
1009275acaafSTrond Myklebust 		int ret2;
1010275acaafSTrond Myklebust 
10116c75dc0dSFred Isaman 		data = list_first_entry(head, struct nfs_write_data, list);
1012275acaafSTrond Myklebust 		list_del_init(&data->list);
1013275acaafSTrond Myklebust 
1014dce81290STrond Myklebust 		ret2 = nfs_do_write(data, call_ops, how);
1015275acaafSTrond Myklebust 		 if (ret == 0)
1016275acaafSTrond Myklebust 			 ret = ret2;
1017275acaafSTrond Myklebust 	}
1018275acaafSTrond Myklebust 	return ret;
1019275acaafSTrond Myklebust }
1020275acaafSTrond Myklebust 
10216d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
10226d884e8fSFred  * call this on each, which will prepare them to be retried on next
10236d884e8fSFred  * writeback using standard nfs.
10246d884e8fSFred  */
10256d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
10266d884e8fSFred {
1027a6305ddbSTrond Myklebust 	struct page *page = req->wb_page;
1028a6305ddbSTrond Myklebust 
10296d884e8fSFred 	nfs_mark_request_dirty(req);
10309994b62bSFred Isaman 	nfs_unlock_request(req);
1031a6305ddbSTrond Myklebust 	nfs_end_page_writeback(page);
10326d884e8fSFred }
10336d884e8fSFred 
1034061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head)
10356c75dc0dSFred Isaman {
10366c75dc0dSFred Isaman 	struct nfs_page	*req;
10376c75dc0dSFred Isaman 
10386c75dc0dSFred Isaman 	while (!list_empty(head)) {
10396c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
10406c75dc0dSFred Isaman 		nfs_list_remove_request(req);
10416c75dc0dSFred Isaman 		nfs_redirty_request(req);
10426c75dc0dSFred Isaman 	}
10436c75dc0dSFred Isaman }
10446c75dc0dSFred Isaman 
1045061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1046061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1047061ae2edSFred Isaman 	.completion = nfs_write_completion,
1048061ae2edSFred Isaman };
1049061ae2edSFred Isaman 
10501da177e4SLinus Torvalds /*
10511da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
10521da177e4SLinus Torvalds  * contiguous dirty area on one page.
10531da177e4SLinus Torvalds  */
10546c75dc0dSFred Isaman static int nfs_flush_multi(struct nfs_pageio_descriptor *desc,
10556c75dc0dSFred Isaman 			   struct nfs_pgio_header *hdr)
10561da177e4SLinus Torvalds {
10576c75dc0dSFred Isaman 	struct nfs_page *req = hdr->req;
10581da177e4SLinus Torvalds 	struct page *page = req->wb_page;
10591da177e4SLinus Torvalds 	struct nfs_write_data *data;
1060d097971dSTrond Myklebust 	size_t wsize = desc->pg_bsize, nbytes;
1061e9f7bee1STrond Myklebust 	unsigned int offset;
10621da177e4SLinus Torvalds 	int requests = 0;
1063dbae4c73STrond Myklebust 	int ret = 0;
1064ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
10651da177e4SLinus Torvalds 
1066ea2cf228SFred Isaman 	nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
10671da177e4SLinus Torvalds 	nfs_list_remove_request(req);
10686c75dc0dSFred Isaman 	nfs_list_add_request(req, &hdr->pages);
10691da177e4SLinus Torvalds 
1070b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1071ea2cf228SFred Isaman 	    (desc->pg_moreio || nfs_reqs_to_commit(&cinfo) ||
1072b31268acSTrond Myklebust 	     desc->pg_count > wsize))
1073b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1074b31268acSTrond Myklebust 
1075b31268acSTrond Myklebust 
1076275acaafSTrond Myklebust 	offset = 0;
1077c76069bdSFred Isaman 	nbytes = desc->pg_count;
1078e9f7bee1STrond Myklebust 	do {
1079e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
1080e9f7bee1STrond Myklebust 
10816c75dc0dSFred Isaman 		data = nfs_writedata_alloc(hdr, 1);
10826c75dc0dSFred Isaman 		if (!data)
10831da177e4SLinus Torvalds 			goto out_bad;
108430dd374fSFred Isaman 		data->pages.pagevec[0] = page;
1085ea2cf228SFred Isaman 		nfs_write_rpcsetup(data, len, offset, desc->pg_ioflags, &cinfo);
10866c75dc0dSFred Isaman 		list_add(&data->list, &hdr->rpc_list);
10871da177e4SLinus Torvalds 		requests++;
1088e9f7bee1STrond Myklebust 		nbytes -= len;
1089275acaafSTrond Myklebust 		offset += len;
1090e9f7bee1STrond Myklebust 	} while (nbytes != 0);
10916c75dc0dSFred Isaman 	desc->pg_rpc_callops = &nfs_write_common_ops;
1092dbae4c73STrond Myklebust 	return ret;
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds out_bad:
10956c75dc0dSFred Isaman 	while (!list_empty(&hdr->rpc_list)) {
10966c75dc0dSFred Isaman 		data = list_first_entry(&hdr->rpc_list, struct nfs_write_data, list);
10976e4efd56STrond Myklebust 		list_del(&data->list);
10988ccd271fSFred Isaman 		nfs_writedata_release(data);
10991da177e4SLinus Torvalds 	}
1100061ae2edSFred Isaman 	desc->pg_completion_ops->error_cleanup(&hdr->pages);
11011da177e4SLinus Torvalds 	return -ENOMEM;
11021da177e4SLinus Torvalds }
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds /*
11051da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
11061da177e4SLinus Torvalds  * The page must have been locked by the caller.
11071da177e4SLinus Torvalds  *
11081da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
11091da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
11101da177e4SLinus Torvalds  * that has been written but not committed.
11111da177e4SLinus Torvalds  */
11126c75dc0dSFred Isaman static int nfs_flush_one(struct nfs_pageio_descriptor *desc,
11136c75dc0dSFred Isaman 			 struct nfs_pgio_header *hdr)
11141da177e4SLinus Torvalds {
11151da177e4SLinus Torvalds 	struct nfs_page		*req;
11161da177e4SLinus Torvalds 	struct page		**pages;
11171da177e4SLinus Torvalds 	struct nfs_write_data	*data;
1118c76069bdSFred Isaman 	struct list_head *head = &desc->pg_list;
11193b609184SPeng Tao 	int ret = 0;
1120ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
11211da177e4SLinus Torvalds 
11226c75dc0dSFred Isaman 	data = nfs_writedata_alloc(hdr, nfs_page_array_len(desc->pg_base,
1123c76069bdSFred Isaman 							   desc->pg_count));
11246c75dc0dSFred Isaman 	if (!data) {
1125061ae2edSFred Isaman 		desc->pg_completion_ops->error_cleanup(head);
112644b83799SFred Isaman 		ret = -ENOMEM;
112744b83799SFred Isaman 		goto out;
112844b83799SFred Isaman 	}
11296c75dc0dSFred Isaman 
1130ea2cf228SFred Isaman 	nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
113130dd374fSFred Isaman 	pages = data->pages.pagevec;
11321da177e4SLinus Torvalds 	while (!list_empty(head)) {
11331da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
11341da177e4SLinus Torvalds 		nfs_list_remove_request(req);
11356c75dc0dSFred Isaman 		nfs_list_add_request(req, &hdr->pages);
11361da177e4SLinus Torvalds 		*pages++ = req->wb_page;
11371da177e4SLinus Torvalds 	}
11381da177e4SLinus Torvalds 
1139b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1140ea2cf228SFred Isaman 	    (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
1141b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1142b31268acSTrond Myklebust 
11431da177e4SLinus Torvalds 	/* Set up the argument struct */
1144ea2cf228SFred Isaman 	nfs_write_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo);
11456c75dc0dSFred Isaman 	list_add(&data->list, &hdr->rpc_list);
11466c75dc0dSFred Isaman 	desc->pg_rpc_callops = &nfs_write_common_ops;
114744b83799SFred Isaman out:
114844b83799SFred Isaman 	return ret;
11491da177e4SLinus Torvalds }
11501da177e4SLinus Torvalds 
11516c75dc0dSFred Isaman int nfs_generic_flush(struct nfs_pageio_descriptor *desc,
11526c75dc0dSFred Isaman 		      struct nfs_pgio_header *hdr)
1153dce81290STrond Myklebust {
1154dce81290STrond Myklebust 	if (desc->pg_bsize < PAGE_CACHE_SIZE)
11556c75dc0dSFred Isaman 		return nfs_flush_multi(desc, hdr);
11566c75dc0dSFred Isaman 	return nfs_flush_one(desc, hdr);
1157dce81290STrond Myklebust }
1158dce81290STrond Myklebust 
1159dce81290STrond Myklebust static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
11601751c363STrond Myklebust {
11616c75dc0dSFred Isaman 	struct nfs_write_header *whdr;
11626c75dc0dSFred Isaman 	struct nfs_pgio_header *hdr;
1163275acaafSTrond Myklebust 	int ret;
1164275acaafSTrond Myklebust 
11656c75dc0dSFred Isaman 	whdr = nfs_writehdr_alloc();
11666c75dc0dSFred Isaman 	if (!whdr) {
1167061ae2edSFred Isaman 		desc->pg_completion_ops->error_cleanup(&hdr->pages);
11686c75dc0dSFred Isaman 		return -ENOMEM;
11696c75dc0dSFred Isaman 	}
11706c75dc0dSFred Isaman 	hdr = &whdr->header;
11716c75dc0dSFred Isaman 	nfs_pgheader_init(desc, hdr, nfs_writehdr_free);
11726c75dc0dSFred Isaman 	atomic_inc(&hdr->refcnt);
11736c75dc0dSFred Isaman 	ret = nfs_generic_flush(desc, hdr);
1174275acaafSTrond Myklebust 	if (ret == 0)
11756c75dc0dSFred Isaman 		ret = nfs_do_multiple_writes(&hdr->rpc_list,
11766c75dc0dSFred Isaman 					     desc->pg_rpc_callops,
1177dce81290STrond Myklebust 					     desc->pg_ioflags);
11786c75dc0dSFred Isaman 	else
11796c75dc0dSFred Isaman 		set_bit(NFS_IOHDR_REDO, &hdr->flags);
11806c75dc0dSFred Isaman 	if (atomic_dec_and_test(&hdr->refcnt))
1181061ae2edSFred Isaman 		hdr->completion_ops->completion(hdr);
1182275acaafSTrond Myklebust 	return ret;
11831751c363STrond Myklebust }
11841751c363STrond Myklebust 
11851751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_write_ops = {
11861751c363STrond Myklebust 	.pg_test = nfs_generic_pg_test,
11871751c363STrond Myklebust 	.pg_doio = nfs_generic_pg_writepages,
11881751c363STrond Myklebust };
11891751c363STrond Myklebust 
1190e2fecb21STrond Myklebust void nfs_pageio_init_write_mds(struct nfs_pageio_descriptor *pgio,
1191061ae2edSFred Isaman 			       struct inode *inode, int ioflags,
1192061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
11931751c363STrond Myklebust {
1194061ae2edSFred Isaman 	nfs_pageio_init(pgio, inode, &nfs_pageio_write_ops, compl_ops,
11951751c363STrond Myklebust 				NFS_SERVER(inode)->wsize, ioflags);
11961751c363STrond Myklebust }
11971751c363STrond Myklebust 
1198dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1199dce81290STrond Myklebust {
1200dce81290STrond Myklebust 	pgio->pg_ops = &nfs_pageio_write_ops;
1201dce81290STrond Myklebust 	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1202dce81290STrond Myklebust }
12031f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1204dce81290STrond Myklebust 
1205*1763da12SFred Isaman void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1206061ae2edSFred Isaman 			   struct inode *inode, int ioflags,
1207061ae2edSFred Isaman 			   const struct nfs_pgio_completion_ops *compl_ops)
12081da177e4SLinus Torvalds {
1209061ae2edSFred Isaman 	if (!pnfs_pageio_init_write(pgio, inode, ioflags, compl_ops))
1210061ae2edSFred Isaman 		nfs_pageio_init_write_mds(pgio, inode, ioflags, compl_ops);
12111da177e4SLinus Torvalds }
12121da177e4SLinus Torvalds 
1213def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata)
1214def6ed7eSAndy Adamson {
1215def6ed7eSAndy Adamson 	struct nfs_write_data *data = calldata;
1216cd841605SFred Isaman 	NFS_PROTO(data->header->inode)->write_rpc_prepare(task, data);
1217def6ed7eSAndy Adamson }
1218def6ed7eSAndy Adamson 
12190b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
12200b7c0153SFred Isaman {
12210b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
12220b7c0153SFred Isaman 
12230b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
12240b7c0153SFred Isaman }
12250b7c0153SFred Isaman 
12261da177e4SLinus Torvalds /*
12271da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
12281da177e4SLinus Torvalds  *
12291da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
12301da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
12311da177e4SLinus Torvalds  *	  as the page has a write request pending.
12321da177e4SLinus Torvalds  */
12336c75dc0dSFred Isaman static void nfs_writeback_done_common(struct rpc_task *task, void *calldata)
12341da177e4SLinus Torvalds {
1235788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
12361da177e4SLinus Torvalds 
1237c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
1238c9d8f89dSTrond Myklebust }
1239c9d8f89dSTrond Myklebust 
12406c75dc0dSFred Isaman static void nfs_writeback_release_common(void *calldata)
1241c9d8f89dSTrond Myklebust {
1242c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1243cd841605SFred Isaman 	struct nfs_pgio_header *hdr = data->header;
1244e2fecb21STrond Myklebust 	int status = data->task.tk_status;
12456c75dc0dSFred Isaman 	struct nfs_page *req = hdr->req;
1246788e7a89STrond Myklebust 
12476c75dc0dSFred Isaman 	if ((status >= 0) && nfs_write_need_commit(data)) {
12486c75dc0dSFred Isaman 		spin_lock(&hdr->lock);
12496c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags))
12506c75dc0dSFred Isaman 			; /* Do nothing */
12516c75dc0dSFred Isaman 		else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags))
12521da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
12536c75dc0dSFred Isaman 		else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf)))
12546c75dc0dSFred Isaman 			set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags);
12556c75dc0dSFred Isaman 		spin_unlock(&hdr->lock);
12561da177e4SLinus Torvalds 	}
1257cd841605SFred Isaman 	nfs_writedata_release(data);
12581da177e4SLinus Torvalds }
12591da177e4SLinus Torvalds 
12606c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops = {
1261def6ed7eSAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
12626c75dc0dSFred Isaman 	.rpc_call_done = nfs_writeback_done_common,
12636c75dc0dSFred Isaman 	.rpc_release = nfs_writeback_release_common,
1264788e7a89STrond Myklebust };
1265788e7a89STrond Myklebust 
1266788e7a89STrond Myklebust 
12671da177e4SLinus Torvalds /*
12681da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
12691da177e4SLinus Torvalds  */
127013602896SFred Isaman void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
12711da177e4SLinus Torvalds {
12721da177e4SLinus Torvalds 	struct nfs_writeargs	*argp = &data->args;
12731da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
1274cd841605SFred Isaman 	struct inode		*inode = data->header->inode;
1275788e7a89STrond Myklebust 	int status;
12761da177e4SLinus Torvalds 
1277a3f565b1SChuck Lever 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
12781da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
12791da177e4SLinus Torvalds 
1280f551e44fSChuck Lever 	/*
1281f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1282f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1283f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1284f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1285f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1286f551e44fSChuck Lever 	 */
1287cd841605SFred Isaman 	status = NFS_PROTO(inode)->write_done(task, data);
1288788e7a89STrond Myklebust 	if (status != 0)
128913602896SFred Isaman 		return;
1290cd841605SFred Isaman 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
129191d5b470SChuck Lever 
12921da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
12931da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
12941da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
12951da177e4SLinus Torvalds 		 * commit data to stable storage even though we
12961da177e4SLinus Torvalds 		 * requested it.
12971da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
12981da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
12991da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
13001da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
13011da177e4SLinus Torvalds 		 */
13021da177e4SLinus Torvalds 		static unsigned long    complain;
13031da177e4SLinus Torvalds 
1304a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
13051da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
13061da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
13071da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1308cd841605SFred Isaman 				NFS_SERVER(inode)->nfs_client->cl_hostname,
13091da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
13101da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
13111da177e4SLinus Torvalds 		}
13121da177e4SLinus Torvalds 	}
13131da177e4SLinus Torvalds #endif
13146c75dc0dSFred Isaman 	if (task->tk_status < 0)
13156c75dc0dSFred Isaman 		nfs_set_pgio_error(data->header, task->tk_status, argp->offset);
13166c75dc0dSFred Isaman 	else if (resp->count < argp->count) {
13171da177e4SLinus Torvalds 		static unsigned long    complain;
13181da177e4SLinus Torvalds 
13196c75dc0dSFred Isaman 		/* This a short write! */
1320cd841605SFred Isaman 		nfs_inc_stats(inode, NFSIOS_SHORTWRITE);
132191d5b470SChuck Lever 
13221da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
13236c75dc0dSFred Isaman 		if (resp->count == 0) {
13246c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
13256c75dc0dSFred Isaman 				printk(KERN_WARNING
13266c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
13276c75dc0dSFred Isaman 				       argp->count);
13286c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
13296c75dc0dSFred Isaman 			}
13306c75dc0dSFred Isaman 			nfs_set_pgio_error(data->header, -EIO, argp->offset);
13316c75dc0dSFred Isaman 			task->tk_status = -EIO;
13326c75dc0dSFred Isaman 			return;
13336c75dc0dSFred Isaman 		}
13341da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
13351da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
13361da177e4SLinus Torvalds 			/* Resend from where the server left off */
1337a69aef14SFred Isaman 			data->mds_offset += resp->count;
13381da177e4SLinus Torvalds 			argp->offset += resp->count;
13391da177e4SLinus Torvalds 			argp->pgbase += resp->count;
13401da177e4SLinus Torvalds 			argp->count -= resp->count;
13411da177e4SLinus Torvalds 		} else {
13421da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
13431da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
13441da177e4SLinus Torvalds 			 */
13451da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
13461da177e4SLinus Torvalds 		}
1347d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
13481da177e4SLinus Torvalds 	}
13491da177e4SLinus Torvalds }
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
135371d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
135471d0a611STrond Myklebust {
1355b8413f98STrond Myklebust 	int ret;
1356b8413f98STrond Myklebust 
135771d0a611STrond Myklebust 	if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
135871d0a611STrond Myklebust 		return 1;
1359b8413f98STrond Myklebust 	if (!may_wait)
136071d0a611STrond Myklebust 		return 0;
1361b8413f98STrond Myklebust 	ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1362b8413f98STrond Myklebust 				NFS_INO_COMMIT,
1363b8413f98STrond Myklebust 				nfs_wait_bit_killable,
1364b8413f98STrond Myklebust 				TASK_KILLABLE);
1365b8413f98STrond Myklebust 	return (ret < 0) ? ret : 1;
136671d0a611STrond Myklebust }
136771d0a611STrond Myklebust 
1368f453a54aSFred Isaman static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
136971d0a611STrond Myklebust {
137071d0a611STrond Myklebust 	clear_bit(NFS_INO_COMMIT, &nfsi->flags);
137171d0a611STrond Myklebust 	smp_mb__after_clear_bit();
137271d0a611STrond Myklebust 	wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
137371d0a611STrond Myklebust }
137471d0a611STrond Myklebust 
13750b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
13761da177e4SLinus Torvalds {
13770b7c0153SFred Isaman 	put_nfs_open_context(data->context);
13780b7c0153SFred Isaman 	nfs_commit_free(data);
13791da177e4SLinus Torvalds }
1380e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
13811da177e4SLinus Torvalds 
13820b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
13839ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
1384788e7a89STrond Myklebust 			int how)
13851da177e4SLinus Torvalds {
138607737691STrond Myklebust 	struct rpc_task *task;
13879ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1388bdc7f021STrond Myklebust 	struct rpc_message msg = {
1389bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1390bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
13919ace33cdSFred Isaman 		.rpc_cred = data->cred,
1392bdc7f021STrond Myklebust 	};
139384115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
139407737691STrond Myklebust 		.task = &data->task,
13959ace33cdSFred Isaman 		.rpc_client = clnt,
1396bdc7f021STrond Myklebust 		.rpc_message = &msg,
13979ace33cdSFred Isaman 		.callback_ops = call_ops,
139884115e1cSTrond Myklebust 		.callback_data = data,
1399101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
14002c61be0aSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
14013ff7576dSTrond Myklebust 		.priority = priority,
140284115e1cSTrond Myklebust 	};
1403788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
14049ace33cdSFred Isaman 	NFS_PROTO(data->inode)->commit_setup(data, &msg);
14051da177e4SLinus Torvalds 
1406a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1407bdc7f021STrond Myklebust 
140807737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1409dbae4c73STrond Myklebust 	if (IS_ERR(task))
1410dbae4c73STrond Myklebust 		return PTR_ERR(task);
1411d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1412d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
141307737691STrond Myklebust 	rpc_put_task(task);
1414dbae4c73STrond Myklebust 	return 0;
14151da177e4SLinus Torvalds }
1416e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds /*
14199ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
14209ace33cdSFred Isaman  */
14210b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1422988b6dceSFred Isaman 		     struct list_head *head,
1423f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1424f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
14259ace33cdSFred Isaman {
14269ace33cdSFred Isaman 	struct nfs_page *first = nfs_list_entry(head->next);
14273d4ff43dSAl Viro 	struct inode *inode = first->wb_context->dentry->d_inode;
14289ace33cdSFred Isaman 
14299ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
14309ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
14319ace33cdSFred Isaman 
14329ace33cdSFred Isaman 	list_splice_init(head, &data->pages);
14339ace33cdSFred Isaman 
14349ace33cdSFred Isaman 	data->inode	  = inode;
14359ace33cdSFred Isaman 	data->cred	  = first->wb_context->cred;
1436988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
14379ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1438f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1439b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
14409ace33cdSFred Isaman 
14419ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
14429ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
14439ace33cdSFred Isaman 	data->args.offset = 0;
14449ace33cdSFred Isaman 	data->args.count  = 0;
14450b7c0153SFred Isaman 	data->context     = get_nfs_open_context(first->wb_context);
14469ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
14479ace33cdSFred Isaman 	data->res.verf    = &data->verf;
14489ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
14499ace33cdSFred Isaman }
1450e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
14519ace33cdSFred Isaman 
1452e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1453ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1454ea2cf228SFred Isaman 		      struct nfs_commit_info *cinfo)
145564bfeb49SFred Isaman {
145664bfeb49SFred Isaman 	struct nfs_page *req;
145764bfeb49SFred Isaman 
145864bfeb49SFred Isaman 	while (!list_empty(page_list)) {
145964bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
146064bfeb49SFred Isaman 		nfs_list_remove_request(req);
1461ea2cf228SFred Isaman 		nfs_mark_request_commit(req, lseg, cinfo);
146256f9cd68SFred Isaman 		if (!cinfo->dreq) {
146364bfeb49SFred Isaman 			dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
146464bfeb49SFred Isaman 			dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
146564bfeb49SFred Isaman 				     BDI_RECLAIMABLE);
146656f9cd68SFred Isaman 		}
14679994b62bSFred Isaman 		nfs_unlock_request(req);
146864bfeb49SFred Isaman 	}
146964bfeb49SFred Isaman }
1470e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
147164bfeb49SFred Isaman 
14729ace33cdSFred Isaman /*
14731da177e4SLinus Torvalds  * Commit dirty pages
14741da177e4SLinus Torvalds  */
14751da177e4SLinus Torvalds static int
1476ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1477ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
14781da177e4SLinus Torvalds {
14790b7c0153SFred Isaman 	struct nfs_commit_data	*data;
14801da177e4SLinus Torvalds 
1481c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
14821da177e4SLinus Torvalds 
14831da177e4SLinus Torvalds 	if (!data)
14841da177e4SLinus Torvalds 		goto out_bad;
14851da177e4SLinus Torvalds 
14861da177e4SLinus Torvalds 	/* Set up the argument struct */
1487f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
1488f453a54aSFred Isaman 	atomic_inc(&cinfo->mds->rpcs_out);
14890b7c0153SFred Isaman 	return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops, how);
14901da177e4SLinus Torvalds  out_bad:
1491ea2cf228SFred Isaman 	nfs_retry_commit(head, NULL, cinfo);
1492f453a54aSFred Isaman 	cinfo->completion_ops->error_cleanup(NFS_I(inode));
14931da177e4SLinus Torvalds 	return -ENOMEM;
14941da177e4SLinus Torvalds }
14951da177e4SLinus Torvalds 
14961da177e4SLinus Torvalds /*
14971da177e4SLinus Torvalds  * COMMIT call returned
14981da177e4SLinus Torvalds  */
1499788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
15001da177e4SLinus Torvalds {
15010b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
15021da177e4SLinus Torvalds 
1503a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
15041da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
15051da177e4SLinus Torvalds 
1506788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1507c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
1508c9d8f89dSTrond Myklebust }
1509c9d8f89dSTrond Myklebust 
1510f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1511c9d8f89dSTrond Myklebust {
1512c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1513c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1514f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
1515788e7a89STrond Myklebust 
15161da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
15171da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
15181da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1519d6d6dc7cSFred Isaman 		nfs_clear_page_commit(req->wb_page);
15201da177e4SLinus Torvalds 
152148186c7dSChuck Lever 		dprintk("NFS:       commit (%s/%lld %d@%lld)",
15223d4ff43dSAl Viro 			req->wb_context->dentry->d_sb->s_id,
15233d4ff43dSAl Viro 			(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
15241da177e4SLinus Torvalds 			req->wb_bytes,
15251da177e4SLinus Torvalds 			(long long)req_offset(req));
1526c9d8f89dSTrond Myklebust 		if (status < 0) {
1527c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
15281da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1529c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
15301da177e4SLinus Torvalds 			goto next;
15311da177e4SLinus Torvalds 		}
15321da177e4SLinus Torvalds 
15331da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
15341da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
15351da177e4SLinus Torvalds 		if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
15361da177e4SLinus Torvalds 			/* We have a match */
15371da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
15381da177e4SLinus Torvalds 			dprintk(" OK\n");
15391da177e4SLinus Torvalds 			goto next;
15401da177e4SLinus Torvalds 		}
15411da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
15421da177e4SLinus Torvalds 		dprintk(" mismatch\n");
15436d884e8fSFred 		nfs_mark_request_dirty(req);
15441da177e4SLinus Torvalds 	next:
15459994b62bSFred Isaman 		nfs_unlock_request(req);
15461da177e4SLinus Torvalds 	}
1547f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1548f453a54aSFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
1549f453a54aSFred Isaman 		nfs_commit_clear_lock(NFS_I(data->inode));
15505917ce84SFred Isaman }
15515917ce84SFred Isaman 
15525917ce84SFred Isaman static void nfs_commit_release(void *calldata)
15535917ce84SFred Isaman {
15540b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
15555917ce84SFred Isaman 
1556f453a54aSFred Isaman 	data->completion_ops->completion(data);
1557c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
15581da177e4SLinus Torvalds }
1559788e7a89STrond Myklebust 
1560788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
15610b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1562788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1563788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1564788e7a89STrond Myklebust };
15651da177e4SLinus Torvalds 
1566f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1567f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1568f453a54aSFred Isaman 	.error_cleanup = nfs_commit_clear_lock,
1569f453a54aSFred Isaman };
1570f453a54aSFred Isaman 
1571*1763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1572ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
157384c53ab5SFred Isaman {
157484c53ab5SFred Isaman 	int status;
157584c53ab5SFred Isaman 
1576ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
157784c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1578ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
157984c53ab5SFred Isaman 	return status;
158084c53ab5SFred Isaman }
158184c53ab5SFred Isaman 
1582b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
15831da177e4SLinus Torvalds {
15841da177e4SLinus Torvalds 	LIST_HEAD(head);
1585ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
158671d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1587b8413f98STrond Myklebust 	int res;
15881da177e4SLinus Torvalds 
1589b8413f98STrond Myklebust 	res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1590b8413f98STrond Myklebust 	if (res <= 0)
1591c5efa5fcSTrond Myklebust 		goto out_mark_dirty;
1592ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1593ea2cf228SFred Isaman 	res = nfs_scan_commit(inode, &head, &cinfo);
15941da177e4SLinus Torvalds 	if (res) {
1595a861a1e1SFred Isaman 		int error;
1596a861a1e1SFred Isaman 
1597ea2cf228SFred Isaman 		error = nfs_generic_commit_list(inode, &head, how, &cinfo);
15981da177e4SLinus Torvalds 		if (error < 0)
15991da177e4SLinus Torvalds 			return error;
1600b8413f98STrond Myklebust 		if (!may_wait)
1601b8413f98STrond Myklebust 			goto out_mark_dirty;
1602b8413f98STrond Myklebust 		error = wait_on_bit(&NFS_I(inode)->flags,
1603b8413f98STrond Myklebust 				NFS_INO_COMMIT,
160471d0a611STrond Myklebust 				nfs_wait_bit_killable,
160571d0a611STrond Myklebust 				TASK_KILLABLE);
1606b8413f98STrond Myklebust 		if (error < 0)
1607b8413f98STrond Myklebust 			return error;
160871d0a611STrond Myklebust 	} else
160971d0a611STrond Myklebust 		nfs_commit_clear_lock(NFS_I(inode));
1610c5efa5fcSTrond Myklebust 	return res;
1611c5efa5fcSTrond Myklebust 	/* Note: If we exit without ensuring that the commit is complete,
1612c5efa5fcSTrond Myklebust 	 * we must mark the inode as dirty. Otherwise, future calls to
1613c5efa5fcSTrond Myklebust 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1614c5efa5fcSTrond Myklebust 	 * that the data is on the disk.
1615c5efa5fcSTrond Myklebust 	 */
1616c5efa5fcSTrond Myklebust out_mark_dirty:
1617c5efa5fcSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
16181da177e4SLinus Torvalds 	return res;
16191da177e4SLinus Torvalds }
16208fc795f7STrond Myklebust 
16218fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
16228fc795f7STrond Myklebust {
1623420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1624420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1625420e3646STrond Myklebust 	int ret = 0;
16268fc795f7STrond Myklebust 
16273236c3e1SJeff Layton 	/* no commits means nothing needs to be done */
1628ea2cf228SFred Isaman 	if (!nfsi->commit_info.ncommit)
16293236c3e1SJeff Layton 		return ret;
16303236c3e1SJeff Layton 
1631a00dd6c0SJeff Layton 	if (wbc->sync_mode == WB_SYNC_NONE) {
1632a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1633a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1634420e3646STrond Myklebust 		 */
1635ea2cf228SFred Isaman 		if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1))
1636420e3646STrond Myklebust 			goto out_mark_dirty;
1637420e3646STrond Myklebust 
1638a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1639420e3646STrond Myklebust 		flags = 0;
1640a00dd6c0SJeff Layton 	}
1641a00dd6c0SJeff Layton 
1642420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1643420e3646STrond Myklebust 	if (ret >= 0) {
1644420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1645420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1646420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1647420e3646STrond Myklebust 			else
1648420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1649420e3646STrond Myklebust 		}
16508fc795f7STrond Myklebust 		return 0;
1651420e3646STrond Myklebust 	}
1652420e3646STrond Myklebust out_mark_dirty:
16538fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
16548fc795f7STrond Myklebust 	return ret;
16558fc795f7STrond Myklebust }
1656c63c7b05STrond Myklebust #else
16578fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
16588fc795f7STrond Myklebust {
16598fc795f7STrond Myklebust 	return 0;
16608fc795f7STrond Myklebust }
16611da177e4SLinus Torvalds #endif
16621da177e4SLinus Torvalds 
16638fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
16648fc795f7STrond Myklebust {
1665863a3c6cSAndy Adamson 	int ret;
1666863a3c6cSAndy Adamson 
1667863a3c6cSAndy Adamson 	ret = nfs_commit_unstable_pages(inode, wbc);
1668863a3c6cSAndy Adamson 	if (ret >= 0 && test_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(inode)->flags)) {
1669ef311537SAndy Adamson 		int status;
1670ef311537SAndy Adamson 		bool sync = true;
1671863a3c6cSAndy Adamson 
1672846d5a09SWu Fengguang 		if (wbc->sync_mode == WB_SYNC_NONE)
1673ef311537SAndy Adamson 			sync = false;
1674863a3c6cSAndy Adamson 
1675863a3c6cSAndy Adamson 		status = pnfs_layoutcommit_inode(inode, sync);
1676863a3c6cSAndy Adamson 		if (status < 0)
1677863a3c6cSAndy Adamson 			return status;
1678863a3c6cSAndy Adamson 	}
1679863a3c6cSAndy Adamson 	return ret;
16808fc795f7STrond Myklebust }
16818fc795f7STrond Myklebust 
1682acdc53b2STrond Myklebust /*
1683acdc53b2STrond Myklebust  * flush the inode to disk.
1684acdc53b2STrond Myklebust  */
1685acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
168634901f70STrond Myklebust {
168734901f70STrond Myklebust 	struct writeback_control wbc = {
168872cb77f4STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
168934901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
1690d7fb1207STrond Myklebust 		.range_start = 0,
1691d7fb1207STrond Myklebust 		.range_end = LLONG_MAX,
169234901f70STrond Myklebust 	};
169334901f70STrond Myklebust 
1694acdc53b2STrond Myklebust 	return sync_inode(inode, &wbc);
16951c75950bSTrond Myklebust }
16961c75950bSTrond Myklebust 
16971b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
16981b3b4a1aSTrond Myklebust {
16991b3b4a1aSTrond Myklebust 	struct nfs_page *req;
17001b3b4a1aSTrond Myklebust 	int ret = 0;
17011b3b4a1aSTrond Myklebust 
17021b3b4a1aSTrond Myklebust 	BUG_ON(!PageLocked(page));
17031b3b4a1aSTrond Myklebust 	for (;;) {
1704ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
17051b3b4a1aSTrond Myklebust 		req = nfs_page_find_request(page);
17061b3b4a1aSTrond Myklebust 		if (req == NULL)
17071b3b4a1aSTrond Myklebust 			break;
17081b3b4a1aSTrond Myklebust 		if (nfs_lock_request_dontget(req)) {
17098dd37758STrond Myklebust 			nfs_clear_request_commit(req);
17101b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
17111b3b4a1aSTrond Myklebust 			/*
17121b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
17131b3b4a1aSTrond Myklebust 			 * page as being dirty
17141b3b4a1aSTrond Myklebust 			 */
17151b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
17161b3b4a1aSTrond Myklebust 			nfs_unlock_request(req);
17171b3b4a1aSTrond Myklebust 			break;
17181b3b4a1aSTrond Myklebust 		}
17191b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
1720c9edda71STrond Myklebust 		nfs_release_request(req);
17211b3b4a1aSTrond Myklebust 		if (ret < 0)
1722c988950eSTrond Myklebust 			break;
17231b3b4a1aSTrond Myklebust 	}
17241b3b4a1aSTrond Myklebust 	return ret;
17251b3b4a1aSTrond Myklebust }
17261b3b4a1aSTrond Myklebust 
17271c75950bSTrond Myklebust /*
17281c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
17291c75950bSTrond Myklebust  */
17301c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
17311c75950bSTrond Myklebust {
17327f2f12d9STrond Myklebust 	loff_t range_start = page_offset(page);
17337f2f12d9STrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
17347f2f12d9STrond Myklebust 	struct writeback_control wbc = {
17357f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
17367f2f12d9STrond Myklebust 		.nr_to_write = 0,
17377f2f12d9STrond Myklebust 		.range_start = range_start,
17387f2f12d9STrond Myklebust 		.range_end = range_end,
17397f2f12d9STrond Myklebust 	};
17407f2f12d9STrond Myklebust 	int ret;
17417f2f12d9STrond Myklebust 
17420522f6adSTrond Myklebust 	for (;;) {
1743ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
17447f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
17457f2f12d9STrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
17467f2f12d9STrond Myklebust 			if (ret < 0)
17477f2f12d9STrond Myklebust 				goto out_error;
17480522f6adSTrond Myklebust 			continue;
17497f2f12d9STrond Myklebust 		}
17500522f6adSTrond Myklebust 		if (!PagePrivate(page))
17510522f6adSTrond Myklebust 			break;
17520522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
17537f2f12d9STrond Myklebust 		if (ret < 0)
17547f2f12d9STrond Myklebust 			goto out_error;
17557f2f12d9STrond Myklebust 	}
17567f2f12d9STrond Myklebust 	return 0;
17577f2f12d9STrond Myklebust out_error:
17587f2f12d9STrond Myklebust 	return ret;
17591c75950bSTrond Myklebust }
17601c75950bSTrond Myklebust 
1761074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
1762074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1763a6bc32b8SMel Gorman 		struct page *page, enum migrate_mode mode)
1764074cc1deSTrond Myklebust {
17652da95652SJeff Layton 	/*
17662da95652SJeff Layton 	 * If PagePrivate is set, then the page is currently associated with
17672da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
17682da95652SJeff Layton 	 *
17692da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
17702da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
17712da95652SJeff Layton 	 *        the page lock.
17722da95652SJeff Layton 	 */
17732da95652SJeff Layton 	if (PagePrivate(page))
17742da95652SJeff Layton 		return -EBUSY;
1775074cc1deSTrond Myklebust 
1776074cc1deSTrond Myklebust 	nfs_fscache_release_page(page, GFP_KERNEL);
1777074cc1deSTrond Myklebust 
1778a6bc32b8SMel Gorman 	return migrate_page(mapping, newpage, page, mode);
1779074cc1deSTrond Myklebust }
1780074cc1deSTrond Myklebust #endif
1781074cc1deSTrond Myklebust 
1782f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
17831da177e4SLinus Torvalds {
17841da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1785cd841605SFred Isaman 					     sizeof(struct nfs_write_header),
17861da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
178720c2df83SPaul Mundt 					     NULL);
17881da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
17891da177e4SLinus Torvalds 		return -ENOMEM;
17901da177e4SLinus Torvalds 
179193d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
17921da177e4SLinus Torvalds 						     nfs_wdata_cachep);
17931da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
17941da177e4SLinus Torvalds 		return -ENOMEM;
17951da177e4SLinus Torvalds 
17960b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
17970b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
17980b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
17990b7c0153SFred Isaman 					     NULL);
18000b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
18010b7c0153SFred Isaman 		return -ENOMEM;
18020b7c0153SFred Isaman 
180393d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
18041da177e4SLinus Torvalds 						      nfs_wdata_cachep);
18051da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
18061da177e4SLinus Torvalds 		return -ENOMEM;
18071da177e4SLinus Torvalds 
180889a09141SPeter Zijlstra 	/*
180989a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
181089a09141SPeter Zijlstra 	 *
181189a09141SPeter Zijlstra 	 *  64MB:    8192k
181289a09141SPeter Zijlstra 	 * 128MB:   11585k
181389a09141SPeter Zijlstra 	 * 256MB:   16384k
181489a09141SPeter Zijlstra 	 * 512MB:   23170k
181589a09141SPeter Zijlstra 	 *   1GB:   32768k
181689a09141SPeter Zijlstra 	 *   2GB:   46340k
181789a09141SPeter Zijlstra 	 *   4GB:   65536k
181889a09141SPeter Zijlstra 	 *   8GB:   92681k
181989a09141SPeter Zijlstra 	 *  16GB:  131072k
182089a09141SPeter Zijlstra 	 *
182189a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
182289a09141SPeter Zijlstra 	 * Limit the default to 256M
182389a09141SPeter Zijlstra 	 */
182489a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
182589a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
182689a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
182789a09141SPeter Zijlstra 
18281da177e4SLinus Torvalds 	return 0;
18291da177e4SLinus Torvalds }
18301da177e4SLinus Torvalds 
1831266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
18321da177e4SLinus Torvalds {
18331da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
18341da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
18351a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
18361da177e4SLinus Torvalds }
18371da177e4SLinus Torvalds 
1838