xref: /linux/fs/nfs/write.c (revision 9bce008bae8b57bc7b007bcc2071d1247a527120)
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);
83*9bce008bSTrond Myklebust 		hdr->verf = &p->verf;
843feb2d49STrond Myklebust 	}
853feb2d49STrond Myklebust 	return p;
863feb2d49STrond Myklebust }
873feb2d49STrond Myklebust 
881763da12SFred Isaman static struct nfs_write_data *nfs_writedata_alloc(struct nfs_pgio_header *hdr,
896c75dc0dSFred Isaman 						  unsigned int pagecount)
906c75dc0dSFred Isaman {
916c75dc0dSFred Isaman 	struct nfs_write_data *data, *prealloc;
926c75dc0dSFred Isaman 
936c75dc0dSFred Isaman 	prealloc = &container_of(hdr, struct nfs_write_header, header)->rpc_data;
946c75dc0dSFred Isaman 	if (prealloc->header == NULL)
956c75dc0dSFred Isaman 		data = prealloc;
966c75dc0dSFred Isaman 	else
976c75dc0dSFred Isaman 		data = kzalloc(sizeof(*data), GFP_KERNEL);
986c75dc0dSFred Isaman 	if (!data)
996c75dc0dSFred Isaman 		goto out;
1006c75dc0dSFred Isaman 
1016c75dc0dSFred Isaman 	if (nfs_pgarray_set(&data->pages, pagecount)) {
1026c75dc0dSFred Isaman 		data->header = hdr;
1036c75dc0dSFred Isaman 		atomic_inc(&hdr->refcnt);
1046c75dc0dSFred Isaman 	} else {
1056c75dc0dSFred Isaman 		if (data != prealloc)
1066c75dc0dSFred Isaman 			kfree(data);
1076c75dc0dSFred Isaman 		data = NULL;
1086c75dc0dSFred Isaman 	}
1096c75dc0dSFred Isaman out:
1106c75dc0dSFred Isaman 	return data;
1116c75dc0dSFred Isaman }
1126c75dc0dSFred Isaman 
113cd841605SFred Isaman void nfs_writehdr_free(struct nfs_pgio_header *hdr)
1143feb2d49STrond Myklebust {
115cd841605SFred Isaman 	struct nfs_write_header *whdr = container_of(hdr, struct nfs_write_header, header);
116cd841605SFred Isaman 	mempool_free(whdr, nfs_wdata_mempool);
1173feb2d49STrond Myklebust }
1183feb2d49STrond Myklebust 
119dce81290STrond Myklebust void nfs_writedata_release(struct nfs_write_data *wdata)
1201da177e4SLinus Torvalds {
1216c75dc0dSFred Isaman 	struct nfs_pgio_header *hdr = wdata->header;
1226c75dc0dSFred Isaman 	struct nfs_write_header *write_header = container_of(hdr, struct nfs_write_header, header);
1236c75dc0dSFred Isaman 
124383ba719STrond Myklebust 	put_nfs_open_context(wdata->args.context);
12530dd374fSFred Isaman 	if (wdata->pages.pagevec != wdata->pages.page_array)
12630dd374fSFred Isaman 		kfree(wdata->pages.pagevec);
1276c75dc0dSFred Isaman 	if (wdata != &write_header->rpc_data)
1286c75dc0dSFred Isaman 		kfree(wdata);
1296c75dc0dSFred Isaman 	else
1306c75dc0dSFred Isaman 		wdata->header = NULL;
1316c75dc0dSFred Isaman 	if (atomic_dec_and_test(&hdr->refcnt))
132061ae2edSFred Isaman 		hdr->completion_ops->completion(hdr);
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds 
1357b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
1367b159fc1STrond Myklebust {
1377b159fc1STrond Myklebust 	ctx->error = error;
1387b159fc1STrond Myklebust 	smp_wmb();
1397b159fc1STrond Myklebust 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
1407b159fc1STrond Myklebust }
1417b159fc1STrond Myklebust 
142277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request_locked(struct page *page)
143277459d2STrond Myklebust {
144277459d2STrond Myklebust 	struct nfs_page *req = NULL;
145277459d2STrond Myklebust 
146277459d2STrond Myklebust 	if (PagePrivate(page)) {
147277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
148277459d2STrond Myklebust 		if (req != NULL)
149c03b4024STrond Myklebust 			kref_get(&req->wb_kref);
150277459d2STrond Myklebust 	}
151277459d2STrond Myklebust 	return req;
152277459d2STrond Myklebust }
153277459d2STrond Myklebust 
154277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page)
155277459d2STrond Myklebust {
156587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
157277459d2STrond Myklebust 	struct nfs_page *req = NULL;
158277459d2STrond Myklebust 
159587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
160277459d2STrond Myklebust 	req = nfs_page_find_request_locked(page);
161587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
162277459d2STrond Myklebust 	return req;
163277459d2STrond Myklebust }
164277459d2STrond Myklebust 
1651da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1661da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1671da177e4SLinus Torvalds {
1681da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
169a3d01454STrond Myklebust 	loff_t end, i_size;
170a3d01454STrond Myklebust 	pgoff_t end_index;
1711da177e4SLinus Torvalds 
172a3d01454STrond Myklebust 	spin_lock(&inode->i_lock);
173a3d01454STrond Myklebust 	i_size = i_size_read(inode);
174a3d01454STrond Myklebust 	end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
1751da177e4SLinus Torvalds 	if (i_size > 0 && page->index < end_index)
176a3d01454STrond Myklebust 		goto out;
1771da177e4SLinus Torvalds 	end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
1781da177e4SLinus Torvalds 	if (i_size >= end)
179a3d01454STrond Myklebust 		goto out;
1801da177e4SLinus Torvalds 	i_size_write(inode, end);
181a3d01454STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
182a3d01454STrond Myklebust out:
183a3d01454STrond Myklebust 	spin_unlock(&inode->i_lock);
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
186a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
187a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
188a301b777STrond Myklebust {
189a301b777STrond Myklebust 	SetPageError(page);
190a301b777STrond Myklebust 	nfs_zap_mapping(page->mapping->host, page->mapping);
191a301b777STrond Myklebust }
192a301b777STrond Myklebust 
1931da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
1941da177e4SLinus Torvalds  * covers the full page.
1951da177e4SLinus Torvalds  */
1961da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds 	if (PageUptodate(page))
1991da177e4SLinus Torvalds 		return;
2001da177e4SLinus Torvalds 	if (base != 0)
2011da177e4SLinus Torvalds 		return;
20249a70f27STrond Myklebust 	if (count != nfs_page_length(page))
2031da177e4SLinus Torvalds 		return;
2041da177e4SLinus Torvalds 	SetPageUptodate(page);
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
2081da177e4SLinus Torvalds {
2091da177e4SLinus Torvalds 	if (wbc->for_reclaim)
210c63c7b05STrond Myklebust 		return FLUSH_HIGHPRI | FLUSH_STABLE;
211b17621feSWu Fengguang 	if (wbc->for_kupdate || wbc->for_background)
212b31268acSTrond Myklebust 		return FLUSH_LOWPRI | FLUSH_COND_STABLE;
213b31268acSTrond Myklebust 	return FLUSH_COND_STABLE;
2141da177e4SLinus Torvalds }
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds /*
21789a09141SPeter Zijlstra  * NFS congestion control
21889a09141SPeter Zijlstra  */
21989a09141SPeter Zijlstra 
22089a09141SPeter Zijlstra int nfs_congestion_kb;
22189a09141SPeter Zijlstra 
22289a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
22389a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
22489a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
22589a09141SPeter Zijlstra 
2265a6d41b3STrond Myklebust static int nfs_set_page_writeback(struct page *page)
22789a09141SPeter Zijlstra {
2285a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
2295a6d41b3STrond Myklebust 
2305a6d41b3STrond Myklebust 	if (!ret) {
23189a09141SPeter Zijlstra 		struct inode *inode = page->mapping->host;
23289a09141SPeter Zijlstra 		struct nfs_server *nfss = NFS_SERVER(inode);
23389a09141SPeter Zijlstra 
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);
249c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
2508aa7e847SJens Axboe 		clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
25189a09141SPeter Zijlstra }
25289a09141SPeter Zijlstra 
253cfb506e1STrond Myklebust static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock)
254e261f51fSTrond Myklebust {
255587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
256e261f51fSTrond Myklebust 	struct nfs_page *req;
257e261f51fSTrond Myklebust 	int ret;
258e261f51fSTrond Myklebust 
259587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
260e261f51fSTrond Myklebust 	for (;;) {
261e261f51fSTrond Myklebust 		req = nfs_page_find_request_locked(page);
262074cc1deSTrond Myklebust 		if (req == NULL)
263074cc1deSTrond Myklebust 			break;
2647ad84aa9STrond Myklebust 		if (nfs_lock_request(req))
265e261f51fSTrond Myklebust 			break;
266e261f51fSTrond Myklebust 		/* Note: If we hold the page lock, as is the case in nfs_writepage,
2677ad84aa9STrond Myklebust 		 *	 then the call to nfs_lock_request() will always
268e261f51fSTrond Myklebust 		 *	 succeed provided that someone hasn't already marked the
269e261f51fSTrond Myklebust 		 *	 request as dirty (in which case we don't care).
270e261f51fSTrond Myklebust 		 */
271587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
272cfb506e1STrond Myklebust 		if (!nonblock)
273e261f51fSTrond Myklebust 			ret = nfs_wait_on_request(req);
274cfb506e1STrond Myklebust 		else
275cfb506e1STrond Myklebust 			ret = -EAGAIN;
276e261f51fSTrond Myklebust 		nfs_release_request(req);
277e261f51fSTrond Myklebust 		if (ret != 0)
278074cc1deSTrond Myklebust 			return ERR_PTR(ret);
279587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
280e261f51fSTrond Myklebust 	}
281587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
282074cc1deSTrond Myklebust 	return req;
283612c9384STrond Myklebust }
284074cc1deSTrond Myklebust 
285074cc1deSTrond Myklebust /*
286074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
287074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
288074cc1deSTrond Myklebust  */
289074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
290cfb506e1STrond Myklebust 				struct page *page, bool nonblock)
291074cc1deSTrond Myklebust {
292074cc1deSTrond Myklebust 	struct nfs_page *req;
293074cc1deSTrond Myklebust 	int ret = 0;
294074cc1deSTrond Myklebust 
295cfb506e1STrond Myklebust 	req = nfs_find_and_lock_request(page, nonblock);
296074cc1deSTrond Myklebust 	if (!req)
297074cc1deSTrond Myklebust 		goto out;
298074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
299074cc1deSTrond Myklebust 	if (IS_ERR(req))
300074cc1deSTrond Myklebust 		goto out;
301074cc1deSTrond Myklebust 
302074cc1deSTrond Myklebust 	ret = nfs_set_page_writeback(page);
303074cc1deSTrond Myklebust 	BUG_ON(ret != 0);
304074cc1deSTrond Myklebust 	BUG_ON(test_bit(PG_CLEAN, &req->wb_flags));
305074cc1deSTrond Myklebust 
306f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
307f8512ad0SFred Isaman 		nfs_redirty_request(req);
308074cc1deSTrond Myklebust 		ret = pgio->pg_error;
309f8512ad0SFred Isaman 	}
310074cc1deSTrond Myklebust out:
311074cc1deSTrond Myklebust 	return ret;
312e261f51fSTrond Myklebust }
313e261f51fSTrond Myklebust 
314f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
315f758c885STrond Myklebust {
316f758c885STrond Myklebust 	struct inode *inode = page->mapping->host;
317cfb506e1STrond Myklebust 	int ret;
318f758c885STrond Myklebust 
319f758c885STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
320f758c885STrond Myklebust 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
321f758c885STrond Myklebust 
322f758c885STrond Myklebust 	nfs_pageio_cond_complete(pgio, page->index);
3231b430beeSWu Fengguang 	ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
324cfb506e1STrond Myklebust 	if (ret == -EAGAIN) {
325cfb506e1STrond Myklebust 		redirty_page_for_writepage(wbc, page);
326cfb506e1STrond Myklebust 		ret = 0;
327cfb506e1STrond Myklebust 	}
328cfb506e1STrond Myklebust 	return ret;
329f758c885STrond Myklebust }
330f758c885STrond Myklebust 
331e261f51fSTrond Myklebust /*
3321da177e4SLinus Torvalds  * Write an mmapped page to the server.
3331da177e4SLinus Torvalds  */
3344d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
3351da177e4SLinus Torvalds {
336f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
337e261f51fSTrond Myklebust 	int err;
3381da177e4SLinus Torvalds 
339061ae2edSFred Isaman 	nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc),
340061ae2edSFred Isaman 			      &nfs_async_write_completion_ops);
341f758c885STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
342f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
343f758c885STrond Myklebust 	if (err < 0)
3444d770ccfSTrond Myklebust 		return err;
345f758c885STrond Myklebust 	if (pgio.pg_error < 0)
346f758c885STrond Myklebust 		return pgio.pg_error;
347f758c885STrond Myklebust 	return 0;
3484d770ccfSTrond Myklebust }
3494d770ccfSTrond Myklebust 
3504d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
3514d770ccfSTrond Myklebust {
352f758c885STrond Myklebust 	int ret;
3534d770ccfSTrond Myklebust 
354f758c885STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
3551da177e4SLinus Torvalds 	unlock_page(page);
356f758c885STrond Myklebust 	return ret;
357f758c885STrond Myklebust }
358f758c885STrond Myklebust 
359f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
360f758c885STrond Myklebust {
361f758c885STrond Myklebust 	int ret;
362f758c885STrond Myklebust 
363f758c885STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
364f758c885STrond Myklebust 	unlock_page(page);
365f758c885STrond Myklebust 	return ret;
3661da177e4SLinus Torvalds }
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3691da177e4SLinus Torvalds {
3701da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
37172cb77f4STrond Myklebust 	unsigned long *bitlock = &NFS_I(inode)->flags;
372c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
3731da177e4SLinus Torvalds 	int err;
3741da177e4SLinus Torvalds 
37572cb77f4STrond Myklebust 	/* Stop dirtying of new pages while we sync */
37672cb77f4STrond Myklebust 	err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
37772cb77f4STrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
37872cb77f4STrond Myklebust 	if (err)
37972cb77f4STrond Myklebust 		goto out_err;
38072cb77f4STrond Myklebust 
38191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
38291d5b470SChuck Lever 
383061ae2edSFred Isaman 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc),
384061ae2edSFred Isaman 			      &nfs_async_write_completion_ops);
385f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
386c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
38772cb77f4STrond Myklebust 
38872cb77f4STrond Myklebust 	clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
38972cb77f4STrond Myklebust 	smp_mb__after_clear_bit();
39072cb77f4STrond Myklebust 	wake_up_bit(bitlock, NFS_INO_FLUSHING);
39172cb77f4STrond Myklebust 
392f758c885STrond Myklebust 	if (err < 0)
39372cb77f4STrond Myklebust 		goto out_err;
39472cb77f4STrond Myklebust 	err = pgio.pg_error;
39572cb77f4STrond Myklebust 	if (err < 0)
39672cb77f4STrond Myklebust 		goto out_err;
397c63c7b05STrond Myklebust 	return 0;
39872cb77f4STrond Myklebust out_err:
39972cb77f4STrond Myklebust 	return err;
4001da177e4SLinus Torvalds }
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds /*
4031da177e4SLinus Torvalds  * Insert a write request into an inode
4041da177e4SLinus Torvalds  */
405d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
4061da177e4SLinus Torvalds {
4071da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
408e7d39069STrond Myklebust 
409e7d39069STrond Myklebust 	/* Lock the request! */
4107ad84aa9STrond Myklebust 	nfs_lock_request(req);
411e7d39069STrond Myklebust 
412e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
4134d65c520STrond Myklebust 	if (!nfsi->npages && nfs_have_delegation(inode, FMODE_WRITE))
414a9a4a87aSTrond Myklebust 		inode->i_version++;
4152df485a7STrond Myklebust 	set_bit(PG_MAPPED, &req->wb_flags);
416deb7d638STrond Myklebust 	SetPagePrivate(req->wb_page);
417277459d2STrond Myklebust 	set_page_private(req->wb_page, (unsigned long)req);
4181da177e4SLinus Torvalds 	nfsi->npages++;
419c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
420e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds /*
42489a09141SPeter Zijlstra  * Remove a write request from an inode
4251da177e4SLinus Torvalds  */
4261da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
4271da177e4SLinus Torvalds {
4283d4ff43dSAl Viro 	struct inode *inode = req->wb_context->dentry->d_inode;
4291da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	BUG_ON (!NFS_WBACK_BUSY(req));
4321da177e4SLinus Torvalds 
433587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
434277459d2STrond Myklebust 	set_page_private(req->wb_page, 0);
435deb7d638STrond Myklebust 	ClearPagePrivate(req->wb_page);
4362df485a7STrond Myklebust 	clear_bit(PG_MAPPED, &req->wb_flags);
4371da177e4SLinus Torvalds 	nfsi->npages--;
438587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
4391da177e4SLinus Torvalds 	nfs_release_request(req);
4401da177e4SLinus Torvalds }
4411da177e4SLinus Torvalds 
44261822ab5STrond Myklebust static void
4436d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
44461822ab5STrond Myklebust {
44561822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
44661822ab5STrond Myklebust }
44761822ab5STrond Myklebust 
4481da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
4498dd37758STrond Myklebust /**
4508dd37758STrond Myklebust  * nfs_request_add_commit_list - add request to a commit list
4518dd37758STrond Myklebust  * @req: pointer to a struct nfs_page
452ea2cf228SFred Isaman  * @dst: commit list head
453ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
4548dd37758STrond Myklebust  *
455ea2cf228SFred Isaman  * This sets the PG_CLEAN bit, updates the cinfo count of
4568dd37758STrond Myklebust  * number of outstanding requests requiring a commit as well as
4578dd37758STrond Myklebust  * the MM page stats.
4588dd37758STrond Myklebust  *
459ea2cf228SFred Isaman  * The caller must _not_ hold the cinfo->lock, but must be
4608dd37758STrond Myklebust  * holding the nfs_page lock.
4618dd37758STrond Myklebust  */
4628dd37758STrond Myklebust void
463ea2cf228SFred Isaman nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst,
464ea2cf228SFred Isaman 			    struct nfs_commit_info *cinfo)
4658dd37758STrond Myklebust {
4668dd37758STrond Myklebust 	set_bit(PG_CLEAN, &(req)->wb_flags);
467ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
468ea2cf228SFred Isaman 	nfs_list_add_request(req, dst);
469ea2cf228SFred Isaman 	cinfo->mds->ncommit++;
470ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
47156f9cd68SFred Isaman 	if (!cinfo->dreq) {
4728dd37758STrond Myklebust 		inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
47356f9cd68SFred Isaman 		inc_bdi_stat(req->wb_page->mapping->backing_dev_info,
47456f9cd68SFred Isaman 			     BDI_RECLAIMABLE);
47556f9cd68SFred Isaman 		__mark_inode_dirty(req->wb_context->dentry->d_inode,
47656f9cd68SFred Isaman 				   I_DIRTY_DATASYNC);
47756f9cd68SFred Isaman 	}
4788dd37758STrond Myklebust }
4798dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
4808dd37758STrond Myklebust 
4818dd37758STrond Myklebust /**
4828dd37758STrond Myklebust  * nfs_request_remove_commit_list - Remove request from a commit list
4838dd37758STrond Myklebust  * @req: pointer to a nfs_page
484ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
4858dd37758STrond Myklebust  *
486ea2cf228SFred Isaman  * This clears the PG_CLEAN bit, and updates the cinfo's count of
4878dd37758STrond Myklebust  * number of outstanding requests requiring a commit
4888dd37758STrond Myklebust  * It does not update the MM page stats.
4898dd37758STrond Myklebust  *
490ea2cf228SFred Isaman  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
4918dd37758STrond Myklebust  */
4928dd37758STrond Myklebust void
493ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req,
494ea2cf228SFred Isaman 			       struct nfs_commit_info *cinfo)
4958dd37758STrond Myklebust {
4968dd37758STrond Myklebust 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
4978dd37758STrond Myklebust 		return;
4988dd37758STrond Myklebust 	nfs_list_remove_request(req);
499ea2cf228SFred Isaman 	cinfo->mds->ncommit--;
5008dd37758STrond Myklebust }
5018dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
5028dd37758STrond Myklebust 
503ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
504ea2cf228SFred Isaman 				      struct inode *inode)
505ea2cf228SFred Isaman {
506ea2cf228SFred Isaman 	cinfo->lock = &inode->i_lock;
507ea2cf228SFred Isaman 	cinfo->mds = &NFS_I(inode)->commit_info;
508ea2cf228SFred Isaman 	cinfo->ds = pnfs_get_ds_info(inode);
509b359f9d0SFred Isaman 	cinfo->dreq = NULL;
510f453a54aSFred Isaman 	cinfo->completion_ops = &nfs_commit_completion_ops;
511ea2cf228SFred Isaman }
512ea2cf228SFred Isaman 
513ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
514ea2cf228SFred Isaman 		    struct inode *inode,
515ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq)
516ea2cf228SFred Isaman {
5171763da12SFred Isaman 	if (dreq)
5181763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
5191763da12SFred Isaman 	else
520ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(cinfo, inode);
521ea2cf228SFred Isaman }
522ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo);
5238dd37758STrond Myklebust 
5241da177e4SLinus Torvalds /*
5251da177e4SLinus Torvalds  * Add a request to the inode's commit list.
5261da177e4SLinus Torvalds  */
5271763da12SFred Isaman void
528ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
529ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
5301da177e4SLinus Torvalds {
531ea2cf228SFred Isaman 	if (pnfs_mark_request_commit(req, lseg, cinfo))
5328dd37758STrond Myklebust 		return;
533ea2cf228SFred Isaman 	nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo);
5341da177e4SLinus Torvalds }
5358e821cadSTrond Myklebust 
536d6d6dc7cSFred Isaman static void
537d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page)
538e468bae9STrond Myklebust {
539e468bae9STrond Myklebust 	dec_zone_page_state(page, NR_UNSTABLE_NFS);
540e468bae9STrond Myklebust 	dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
541e468bae9STrond Myklebust }
542d6d6dc7cSFred Isaman 
5438dd37758STrond Myklebust static void
544d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req)
545d6d6dc7cSFred Isaman {
5468dd37758STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
5478dd37758STrond Myklebust 		struct inode *inode = req->wb_context->dentry->d_inode;
548ea2cf228SFred Isaman 		struct nfs_commit_info cinfo;
549d6d6dc7cSFred Isaman 
550ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(&cinfo, inode);
551ea2cf228SFred Isaman 		if (!pnfs_clear_request_commit(req, &cinfo)) {
552ea2cf228SFred Isaman 			spin_lock(cinfo.lock);
553ea2cf228SFred Isaman 			nfs_request_remove_commit_list(req, &cinfo);
554ea2cf228SFred Isaman 			spin_unlock(cinfo.lock);
555d6d6dc7cSFred Isaman 		}
5568dd37758STrond Myklebust 		nfs_clear_page_commit(req->wb_page);
5578dd37758STrond Myklebust 	}
558e468bae9STrond Myklebust }
559e468bae9STrond Myklebust 
5608e821cadSTrond Myklebust static inline
5618e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
5628e821cadSTrond Myklebust {
563465d5243SFred Isaman 	if (data->verf.committed == NFS_DATA_SYNC)
564cd841605SFred Isaman 		return data->header->lseg == NULL;
5658e821cadSTrond Myklebust 	return data->verf.committed != NFS_FILE_SYNC;
5668e821cadSTrond Myklebust }
5678e821cadSTrond Myklebust 
5688e821cadSTrond Myklebust #else
56968cd6fa4SBryan Schumaker static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
57068cd6fa4SBryan Schumaker 				      struct inode *inode)
57168cd6fa4SBryan Schumaker {
57268cd6fa4SBryan Schumaker }
57368cd6fa4SBryan Schumaker 
57468cd6fa4SBryan Schumaker void nfs_init_cinfo(struct nfs_commit_info *cinfo,
57568cd6fa4SBryan Schumaker 		    struct inode *inode,
57668cd6fa4SBryan Schumaker 		    struct nfs_direct_req *dreq)
57768cd6fa4SBryan Schumaker {
57868cd6fa4SBryan Schumaker }
57968cd6fa4SBryan Schumaker 
5801763da12SFred Isaman void
581ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
582ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
5838e821cadSTrond Myklebust {
5848e821cadSTrond Myklebust }
5858e821cadSTrond Myklebust 
5868dd37758STrond Myklebust static void
587e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
588e468bae9STrond Myklebust {
589e468bae9STrond Myklebust }
590e468bae9STrond Myklebust 
5918e821cadSTrond Myklebust static inline
5928e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
5938e821cadSTrond Myklebust {
5948e821cadSTrond Myklebust 	return 0;
5958e821cadSTrond Myklebust }
5968e821cadSTrond Myklebust 
5971da177e4SLinus Torvalds #endif
5981da177e4SLinus Torvalds 
599061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
6006c75dc0dSFred Isaman {
601ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
6026c75dc0dSFred Isaman 	unsigned long bytes = 0;
6036c75dc0dSFred Isaman 
6046c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
6056c75dc0dSFred Isaman 		goto out;
606ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
6076c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
6086c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
6096c75dc0dSFred Isaman 
6106c75dc0dSFred Isaman 		bytes += req->wb_bytes;
6116c75dc0dSFred Isaman 		nfs_list_remove_request(req);
6126c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
6136c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
614d1182b33STrond Myklebust 			nfs_set_pageerror(req->wb_page);
6156c75dc0dSFred Isaman 			nfs_context_set_write_error(req->wb_context, hdr->error);
6166c75dc0dSFred Isaman 			goto remove_req;
6176c75dc0dSFred Isaman 		}
6186c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
6196c75dc0dSFred Isaman 			nfs_mark_request_dirty(req);
6206c75dc0dSFred Isaman 			goto next;
6216c75dc0dSFred Isaman 		}
6226c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
623*9bce008bSTrond Myklebust 			memcpy(&req->wb_verf, hdr->verf, sizeof(req->wb_verf));
624ea2cf228SFred Isaman 			nfs_mark_request_commit(req, hdr->lseg, &cinfo);
6256c75dc0dSFred Isaman 			goto next;
6266c75dc0dSFred Isaman 		}
6276c75dc0dSFred Isaman remove_req:
6286c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
6296c75dc0dSFred Isaman next:
6301d1afcbcSTrond Myklebust 		nfs_unlock_request(req);
631d1182b33STrond Myklebust 		nfs_end_page_writeback(req->wb_page);
6323aff4ebbSTrond Myklebust 		nfs_release_request(req);
6336c75dc0dSFred Isaman 	}
6346c75dc0dSFred Isaman out:
6356c75dc0dSFred Isaman 	hdr->release(hdr);
6366c75dc0dSFred Isaman }
6376c75dc0dSFred Isaman 
63847c62564STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
639ea2cf228SFred Isaman static unsigned long
640ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
641fb8a1f11STrond Myklebust {
642ea2cf228SFred Isaman 	return cinfo->mds->ncommit;
643fb8a1f11STrond Myklebust }
644fb8a1f11STrond Myklebust 
645ea2cf228SFred Isaman /* cinfo->lock held by caller */
6461763da12SFred Isaman int
647ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
648ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
649d6d6dc7cSFred Isaman {
650d6d6dc7cSFred Isaman 	struct nfs_page *req, *tmp;
651d6d6dc7cSFred Isaman 	int ret = 0;
652d6d6dc7cSFred Isaman 
653d6d6dc7cSFred Isaman 	list_for_each_entry_safe(req, tmp, src, wb_list) {
6548dd37758STrond Myklebust 		if (!nfs_lock_request(req))
6558dd37758STrond Myklebust 			continue;
6567ad84aa9STrond Myklebust 		kref_get(&req->wb_kref);
657ea2cf228SFred Isaman 		if (cond_resched_lock(cinfo->lock))
6583b3be88dSTrond Myklebust 			list_safe_reset_next(req, tmp, wb_list);
659ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
6608dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
661d6d6dc7cSFred Isaman 		ret++;
6621763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
663d6d6dc7cSFred Isaman 			break;
664d6d6dc7cSFred Isaman 	}
665d6d6dc7cSFred Isaman 	return ret;
666d6d6dc7cSFred Isaman }
667d6d6dc7cSFred Isaman 
6681da177e4SLinus Torvalds /*
6691da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
6701da177e4SLinus Torvalds  * @inode: NFS inode to scan
671ea2cf228SFred Isaman  * @dst: mds destination list
672ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
6731da177e4SLinus Torvalds  *
6741da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
6751da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
6761da177e4SLinus Torvalds  */
6771763da12SFred Isaman int
678ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
679ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
6801da177e4SLinus Torvalds {
681d6d6dc7cSFred Isaman 	int ret = 0;
682fb8a1f11STrond Myklebust 
683ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
684ea2cf228SFred Isaman 	if (cinfo->mds->ncommit > 0) {
6858dd37758STrond Myklebust 		const int max = INT_MAX;
686d6d6dc7cSFred Isaman 
687ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
688ea2cf228SFred Isaman 					   cinfo, max);
689ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
690d6d6dc7cSFred Isaman 	}
691ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
692ff778d02STrond Myklebust 	return ret;
6931da177e4SLinus Torvalds }
694d6d6dc7cSFred Isaman 
695c42de9ddSTrond Myklebust #else
696ea2cf228SFred Isaman static unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
697fb8a1f11STrond Myklebust {
698fb8a1f11STrond Myklebust 	return 0;
699fb8a1f11STrond Myklebust }
700fb8a1f11STrond Myklebust 
7011763da12SFred Isaman int nfs_scan_commit(struct inode *inode, struct list_head *dst,
702ea2cf228SFred Isaman 		    struct nfs_commit_info *cinfo)
703c42de9ddSTrond Myklebust {
704c42de9ddSTrond Myklebust 	return 0;
705c42de9ddSTrond Myklebust }
7061da177e4SLinus Torvalds #endif
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds /*
709e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
710e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
7111da177e4SLinus Torvalds  *
712e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
713e7d39069STrond Myklebust  * to disk.
7141da177e4SLinus Torvalds  */
715e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
716e7d39069STrond Myklebust 		struct page *page,
717e7d39069STrond Myklebust 		unsigned int offset,
718e7d39069STrond Myklebust 		unsigned int bytes)
7191da177e4SLinus Torvalds {
720e7d39069STrond Myklebust 	struct nfs_page *req;
721e7d39069STrond Myklebust 	unsigned int rqend;
722e7d39069STrond Myklebust 	unsigned int end;
7231da177e4SLinus Torvalds 	int error;
724277459d2STrond Myklebust 
725e7d39069STrond Myklebust 	if (!PagePrivate(page))
726e7d39069STrond Myklebust 		return NULL;
727e7d39069STrond Myklebust 
728e7d39069STrond Myklebust 	end = offset + bytes;
729e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
730e7d39069STrond Myklebust 
731e7d39069STrond Myklebust 	for (;;) {
732e7d39069STrond Myklebust 		req = nfs_page_find_request_locked(page);
733e7d39069STrond Myklebust 		if (req == NULL)
734e7d39069STrond Myklebust 			goto out_unlock;
735e7d39069STrond Myklebust 
736e7d39069STrond Myklebust 		rqend = req->wb_offset + req->wb_bytes;
737e7d39069STrond Myklebust 		/*
738e7d39069STrond Myklebust 		 * Tell the caller to flush out the request if
739e7d39069STrond Myklebust 		 * the offsets are non-contiguous.
740e7d39069STrond Myklebust 		 * Note: nfs_flush_incompatible() will already
741e7d39069STrond Myklebust 		 * have flushed out requests having wrong owners.
742e7d39069STrond Myklebust 		 */
743e468bae9STrond Myklebust 		if (offset > rqend
744e7d39069STrond Myklebust 		    || end < req->wb_offset)
745e7d39069STrond Myklebust 			goto out_flushme;
746e7d39069STrond Myklebust 
7477ad84aa9STrond Myklebust 		if (nfs_lock_request(req))
748e7d39069STrond Myklebust 			break;
749e7d39069STrond Myklebust 
750e7d39069STrond Myklebust 		/* The request is locked, so wait and then retry */
751587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
7521da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
7531da177e4SLinus Torvalds 		nfs_release_request(req);
754e7d39069STrond Myklebust 		if (error != 0)
755e7d39069STrond Myklebust 			goto out_err;
756e7d39069STrond Myklebust 		spin_lock(&inode->i_lock);
7571da177e4SLinus Torvalds 	}
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
7601da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
7611da177e4SLinus Torvalds 		req->wb_offset = offset;
7621da177e4SLinus Torvalds 		req->wb_pgbase = offset;
7631da177e4SLinus Torvalds 	}
7641da177e4SLinus Torvalds 	if (end > rqend)
7651da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
766e7d39069STrond Myklebust 	else
767e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
768e7d39069STrond Myklebust out_unlock:
769e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
770ca138f36SFred Isaman 	if (req)
7718dd37758STrond Myklebust 		nfs_clear_request_commit(req);
772e7d39069STrond Myklebust 	return req;
773e7d39069STrond Myklebust out_flushme:
774e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
775e7d39069STrond Myklebust 	nfs_release_request(req);
776e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
777e7d39069STrond Myklebust out_err:
778e7d39069STrond Myklebust 	return ERR_PTR(error);
779e7d39069STrond Myklebust }
7801da177e4SLinus Torvalds 
781e7d39069STrond Myklebust /*
782e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
783e7d39069STrond Myklebust  *
784e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
785e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
786e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
787e7d39069STrond Myklebust  */
788e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
789e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
790e7d39069STrond Myklebust {
791e7d39069STrond Myklebust 	struct inode *inode = page->mapping->host;
792e7d39069STrond Myklebust 	struct nfs_page	*req;
793e7d39069STrond Myklebust 
794e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
795e7d39069STrond Myklebust 	if (req != NULL)
796e7d39069STrond Myklebust 		goto out;
797e7d39069STrond Myklebust 	req = nfs_create_request(ctx, inode, page, offset, bytes);
798e7d39069STrond Myklebust 	if (IS_ERR(req))
799e7d39069STrond Myklebust 		goto out;
800d6d6dc7cSFred Isaman 	nfs_inode_add_request(inode, req);
801efc91ed0STrond Myklebust out:
80261e930a9STrond Myklebust 	return req;
8031da177e4SLinus Torvalds }
8041da177e4SLinus Torvalds 
805e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
806e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
807e7d39069STrond Myklebust {
808e7d39069STrond Myklebust 	struct nfs_page	*req;
809e7d39069STrond Myklebust 
810e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
811e7d39069STrond Myklebust 	if (IS_ERR(req))
812e7d39069STrond Myklebust 		return PTR_ERR(req);
813e7d39069STrond Myklebust 	/* Update file length */
814e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
815e7d39069STrond Myklebust 	nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
816a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
8171d1afcbcSTrond Myklebust 	nfs_unlock_and_release_request(req);
818e7d39069STrond Myklebust 	return 0;
819e7d39069STrond Myklebust }
820e7d39069STrond Myklebust 
8211da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
8221da177e4SLinus Torvalds {
823cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
8241da177e4SLinus Torvalds 	struct nfs_page	*req;
8251a54533eSTrond Myklebust 	int do_flush, status;
8261da177e4SLinus Torvalds 	/*
8271da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
8281da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
8291da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
8301da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
8311da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
8321da177e4SLinus Torvalds 	 * dropped page.
8331da177e4SLinus Torvalds 	 */
8341a54533eSTrond Myklebust 	do {
835277459d2STrond Myklebust 		req = nfs_page_find_request(page);
8361a54533eSTrond Myklebust 		if (req == NULL)
8371a54533eSTrond Myklebust 			return 0;
838f11ac8dbSTrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx ||
839f11ac8dbSTrond Myklebust 			req->wb_lock_context->lockowner != current->files ||
840f11ac8dbSTrond Myklebust 			req->wb_lock_context->pid != current->tgid;
8411da177e4SLinus Torvalds 		nfs_release_request(req);
8421a54533eSTrond Myklebust 		if (!do_flush)
8431a54533eSTrond Myklebust 			return 0;
844277459d2STrond Myklebust 		status = nfs_wb_page(page->mapping->host, page);
8451a54533eSTrond Myklebust 	} while (status == 0);
8461a54533eSTrond Myklebust 	return status;
8471da177e4SLinus Torvalds }
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds /*
8505d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
8515d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
8525d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
8535d47a356STrond Myklebust  */
8548d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
8555d47a356STrond Myklebust {
8568d197a56STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
8578d197a56STrond Myklebust 		goto out;
8588d197a56STrond Myklebust 	if (NFS_I(inode)->cache_validity & NFS_INO_REVAL_PAGECACHE)
8598d197a56STrond Myklebust 		return false;
8608d197a56STrond Myklebust out:
8618d197a56STrond Myklebust 	return PageUptodate(page) != 0;
8625d47a356STrond Myklebust }
8635d47a356STrond Myklebust 
8645d47a356STrond Myklebust /*
8651da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
8661da177e4SLinus Torvalds  *
8671da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
8681da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
8691da177e4SLinus Torvalds  */
8701da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
8711da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
8721da177e4SLinus Torvalds {
873cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
8741da177e4SLinus Torvalds 	struct inode	*inode = page->mapping->host;
8751da177e4SLinus Torvalds 	int		status = 0;
8761da177e4SLinus Torvalds 
87791d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
87891d5b470SChuck Lever 
87948186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage(%s/%s %d@%lld)\n",
88001cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
88101cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name, count,
8820bbacc40SChuck Lever 		(long long)(page_offset(page) + offset));
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	/* If we're not using byte range locks, and we know the page
8855d47a356STrond Myklebust 	 * is up to date, it may be more efficient to extend the write
8865d47a356STrond Myklebust 	 * to cover the entire page in order to avoid fragmentation
8875d47a356STrond Myklebust 	 * inefficiencies.
8881da177e4SLinus Torvalds 	 */
8895d47a356STrond Myklebust 	if (nfs_write_pageuptodate(page, inode) &&
8905d47a356STrond Myklebust 			inode->i_flock == NULL &&
8916b2f3d1fSChristoph Hellwig 			!(file->f_flags & O_DSYNC)) {
89249a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
8931da177e4SLinus Torvalds 		offset = 0;
8941da177e4SLinus Torvalds 	}
8951da177e4SLinus Torvalds 
896e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
89703fa9e84STrond Myklebust 	if (status < 0)
89803fa9e84STrond Myklebust 		nfs_set_pageerror(page);
89959b7c05fSTrond Myklebust 	else
90059b7c05fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
9011da177e4SLinus Torvalds 
90248186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
9031da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
9041da177e4SLinus Torvalds 	return status;
9051da177e4SLinus Torvalds }
9061da177e4SLinus Torvalds 
9073ff7576dSTrond Myklebust static int flush_task_priority(int how)
9081da177e4SLinus Torvalds {
9091da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
9101da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
9111da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
9121da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
9131da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
9141da177e4SLinus Torvalds 	}
9151da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
9161da177e4SLinus Torvalds }
9171da177e4SLinus Torvalds 
918c5996c4eSFred Isaman int nfs_initiate_write(struct rpc_clnt *clnt,
919c5996c4eSFred Isaman 		       struct nfs_write_data *data,
920788e7a89STrond Myklebust 		       const struct rpc_call_ops *call_ops,
9219f0ec176SAndy Adamson 		       int how, int flags)
9221da177e4SLinus Torvalds {
923cd841605SFred Isaman 	struct inode *inode = data->header->inode;
9243ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
92507737691STrond Myklebust 	struct rpc_task *task;
926bdc7f021STrond Myklebust 	struct rpc_message msg = {
927bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
928bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
929cd841605SFred Isaman 		.rpc_cred = data->header->cred,
930bdc7f021STrond Myklebust 	};
93184115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
932d138d5d1SAndy Adamson 		.rpc_client = clnt,
93307737691STrond Myklebust 		.task = &data->task,
934bdc7f021STrond Myklebust 		.rpc_message = &msg,
93584115e1cSTrond Myklebust 		.callback_ops = call_ops,
93684115e1cSTrond Myklebust 		.callback_data = data,
937101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
9389f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
9393ff7576dSTrond Myklebust 		.priority = priority,
94084115e1cSTrond Myklebust 	};
9412c61be0aSTrond Myklebust 	int ret = 0;
9421da177e4SLinus Torvalds 
943d138d5d1SAndy Adamson 	/* Set up the initial task struct.  */
944d138d5d1SAndy Adamson 	NFS_PROTO(inode)->write_setup(data, &msg);
945d138d5d1SAndy Adamson 
946d138d5d1SAndy Adamson 	dprintk("NFS: %5u initiated write call "
947d138d5d1SAndy Adamson 		"(req %s/%lld, %u bytes @ offset %llu)\n",
948d138d5d1SAndy Adamson 		data->task.tk_pid,
949d138d5d1SAndy Adamson 		inode->i_sb->s_id,
950d138d5d1SAndy Adamson 		(long long)NFS_FILEID(inode),
951d138d5d1SAndy Adamson 		data->args.count,
952d138d5d1SAndy Adamson 		(unsigned long long)data->args.offset);
953d138d5d1SAndy Adamson 
954d138d5d1SAndy Adamson 	task = rpc_run_task(&task_setup_data);
955d138d5d1SAndy Adamson 	if (IS_ERR(task)) {
956d138d5d1SAndy Adamson 		ret = PTR_ERR(task);
957d138d5d1SAndy Adamson 		goto out;
958d138d5d1SAndy Adamson 	}
959d138d5d1SAndy Adamson 	if (how & FLUSH_SYNC) {
960d138d5d1SAndy Adamson 		ret = rpc_wait_for_completion_task(task);
961d138d5d1SAndy Adamson 		if (ret == 0)
962d138d5d1SAndy Adamson 			ret = task->tk_status;
963d138d5d1SAndy Adamson 	}
964d138d5d1SAndy Adamson 	rpc_put_task(task);
965d138d5d1SAndy Adamson out:
966d138d5d1SAndy Adamson 	return ret;
967d138d5d1SAndy Adamson }
968a69aef14SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_write);
969d138d5d1SAndy Adamson 
970d138d5d1SAndy Adamson /*
971d138d5d1SAndy Adamson  * Set up the argument/result storage required for the RPC call.
972d138d5d1SAndy Adamson  */
9736c75dc0dSFred Isaman static void nfs_write_rpcsetup(struct nfs_write_data *data,
974d138d5d1SAndy Adamson 		unsigned int count, unsigned int offset,
975ea2cf228SFred Isaman 		int how, struct nfs_commit_info *cinfo)
976d138d5d1SAndy Adamson {
9776c75dc0dSFred Isaman 	struct nfs_page *req = data->header->req;
978d138d5d1SAndy Adamson 
9791da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
9801da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
9811da177e4SLinus Torvalds 
9826c75dc0dSFred Isaman 	data->args.fh     = NFS_FH(data->header->inode);
9831da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
9842bea038cSBoaz Harrosh 	/* pnfs_set_layoutcommit needs this */
9852bea038cSBoaz Harrosh 	data->mds_offset = data->args.offset;
9861da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
98730dd374fSFred Isaman 	data->args.pages  = data->pages.pagevec;
9881da177e4SLinus Torvalds 	data->args.count  = count;
989383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
990f11ac8dbSTrond Myklebust 	data->args.lock_context = req->wb_lock_context;
991bdc7f021STrond Myklebust 	data->args.stable  = NFS_UNSTABLE;
99287ed5eb4STrond Myklebust 	switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
99387ed5eb4STrond Myklebust 	case 0:
99487ed5eb4STrond Myklebust 		break;
99587ed5eb4STrond Myklebust 	case FLUSH_COND_STABLE:
996ea2cf228SFred Isaman 		if (nfs_reqs_to_commit(cinfo))
99787ed5eb4STrond Myklebust 			break;
99887ed5eb4STrond Myklebust 	default:
999bdc7f021STrond Myklebust 		data->args.stable = NFS_FILE_SYNC;
1000bdc7f021STrond Myklebust 	}
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
10031da177e4SLinus Torvalds 	data->res.count   = count;
10041da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
10050e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
10066e4efd56STrond Myklebust }
10071da177e4SLinus Torvalds 
10086e4efd56STrond Myklebust static int nfs_do_write(struct nfs_write_data *data,
10096e4efd56STrond Myklebust 		const struct rpc_call_ops *call_ops,
10106e4efd56STrond Myklebust 		int how)
10116e4efd56STrond Myklebust {
1012cd841605SFred Isaman 	struct inode *inode = data->header->inode;
10130382b744SAndy Adamson 
10149f0ec176SAndy Adamson 	return nfs_initiate_write(NFS_CLIENT(inode), data, call_ops, how, 0);
10151da177e4SLinus Torvalds }
10161da177e4SLinus Torvalds 
1017275acaafSTrond Myklebust static int nfs_do_multiple_writes(struct list_head *head,
1018275acaafSTrond Myklebust 		const struct rpc_call_ops *call_ops,
1019275acaafSTrond Myklebust 		int how)
1020275acaafSTrond Myklebust {
1021275acaafSTrond Myklebust 	struct nfs_write_data *data;
1022275acaafSTrond Myklebust 	int ret = 0;
1023275acaafSTrond Myklebust 
1024275acaafSTrond Myklebust 	while (!list_empty(head)) {
1025275acaafSTrond Myklebust 		int ret2;
1026275acaafSTrond Myklebust 
10276c75dc0dSFred Isaman 		data = list_first_entry(head, struct nfs_write_data, list);
1028275acaafSTrond Myklebust 		list_del_init(&data->list);
1029275acaafSTrond Myklebust 
1030dce81290STrond Myklebust 		ret2 = nfs_do_write(data, call_ops, how);
1031275acaafSTrond Myklebust 		 if (ret == 0)
1032275acaafSTrond Myklebust 			 ret = ret2;
1033275acaafSTrond Myklebust 	}
1034275acaafSTrond Myklebust 	return ret;
1035275acaafSTrond Myklebust }
1036275acaafSTrond Myklebust 
10376d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
10386d884e8fSFred  * call this on each, which will prepare them to be retried on next
10396d884e8fSFred  * writeback using standard nfs.
10406d884e8fSFred  */
10416d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
10426d884e8fSFred {
10436d884e8fSFred 	nfs_mark_request_dirty(req);
10441d1afcbcSTrond Myklebust 	nfs_unlock_request(req);
1045d1182b33STrond Myklebust 	nfs_end_page_writeback(req->wb_page);
10463aff4ebbSTrond Myklebust 	nfs_release_request(req);
10476d884e8fSFred }
10486d884e8fSFred 
1049061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head)
10506c75dc0dSFred Isaman {
10516c75dc0dSFred Isaman 	struct nfs_page	*req;
10526c75dc0dSFred Isaman 
10536c75dc0dSFred Isaman 	while (!list_empty(head)) {
10546c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
10556c75dc0dSFred Isaman 		nfs_list_remove_request(req);
10566c75dc0dSFred Isaman 		nfs_redirty_request(req);
10576c75dc0dSFred Isaman 	}
10586c75dc0dSFred Isaman }
10596c75dc0dSFred Isaman 
1060061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1061061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1062061ae2edSFred Isaman 	.completion = nfs_write_completion,
1063061ae2edSFred Isaman };
1064061ae2edSFred Isaman 
106525b11dcdSTrond Myklebust static void nfs_flush_error(struct nfs_pageio_descriptor *desc,
106625b11dcdSTrond Myklebust 		struct nfs_pgio_header *hdr)
106725b11dcdSTrond Myklebust {
106825b11dcdSTrond Myklebust 	set_bit(NFS_IOHDR_REDO, &hdr->flags);
106925b11dcdSTrond Myklebust 	while (!list_empty(&hdr->rpc_list)) {
107025b11dcdSTrond Myklebust 		struct nfs_write_data *data = list_first_entry(&hdr->rpc_list,
107125b11dcdSTrond Myklebust 				struct nfs_write_data, list);
107225b11dcdSTrond Myklebust 		list_del(&data->list);
107325b11dcdSTrond Myklebust 		nfs_writedata_release(data);
107425b11dcdSTrond Myklebust 	}
107525b11dcdSTrond Myklebust 	desc->pg_completion_ops->error_cleanup(&desc->pg_list);
107625b11dcdSTrond Myklebust }
107725b11dcdSTrond Myklebust 
10781da177e4SLinus Torvalds /*
10791da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
10801da177e4SLinus Torvalds  * contiguous dirty area on one page.
10811da177e4SLinus Torvalds  */
10826c75dc0dSFred Isaman static int nfs_flush_multi(struct nfs_pageio_descriptor *desc,
10836c75dc0dSFred Isaman 			   struct nfs_pgio_header *hdr)
10841da177e4SLinus Torvalds {
10856c75dc0dSFred Isaman 	struct nfs_page *req = hdr->req;
10861da177e4SLinus Torvalds 	struct page *page = req->wb_page;
10871da177e4SLinus Torvalds 	struct nfs_write_data *data;
1088d097971dSTrond Myklebust 	size_t wsize = desc->pg_bsize, nbytes;
1089e9f7bee1STrond Myklebust 	unsigned int offset;
10901da177e4SLinus Torvalds 	int requests = 0;
1091ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
10921da177e4SLinus Torvalds 
1093ea2cf228SFred Isaman 	nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
10941da177e4SLinus Torvalds 
1095b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1096ea2cf228SFred Isaman 	    (desc->pg_moreio || nfs_reqs_to_commit(&cinfo) ||
1097b31268acSTrond Myklebust 	     desc->pg_count > wsize))
1098b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1099b31268acSTrond Myklebust 
1100b31268acSTrond Myklebust 
1101275acaafSTrond Myklebust 	offset = 0;
1102c76069bdSFred Isaman 	nbytes = desc->pg_count;
1103e9f7bee1STrond Myklebust 	do {
1104e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
1105e9f7bee1STrond Myklebust 
11066c75dc0dSFred Isaman 		data = nfs_writedata_alloc(hdr, 1);
110725b11dcdSTrond Myklebust 		if (!data) {
110825b11dcdSTrond Myklebust 			nfs_flush_error(desc, hdr);
110925b11dcdSTrond Myklebust 			return -ENOMEM;
111025b11dcdSTrond Myklebust 		}
111130dd374fSFred Isaman 		data->pages.pagevec[0] = page;
1112ea2cf228SFred Isaman 		nfs_write_rpcsetup(data, len, offset, desc->pg_ioflags, &cinfo);
11136c75dc0dSFred Isaman 		list_add(&data->list, &hdr->rpc_list);
11141da177e4SLinus Torvalds 		requests++;
1115e9f7bee1STrond Myklebust 		nbytes -= len;
1116275acaafSTrond Myklebust 		offset += len;
1117e9f7bee1STrond Myklebust 	} while (nbytes != 0);
111825b11dcdSTrond Myklebust 	nfs_list_remove_request(req);
111925b11dcdSTrond Myklebust 	nfs_list_add_request(req, &hdr->pages);
11206c75dc0dSFred Isaman 	desc->pg_rpc_callops = &nfs_write_common_ops;
112125b11dcdSTrond Myklebust 	return 0;
11221da177e4SLinus Torvalds }
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds /*
11251da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
11261da177e4SLinus Torvalds  * The page must have been locked by the caller.
11271da177e4SLinus Torvalds  *
11281da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
11291da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
11301da177e4SLinus Torvalds  * that has been written but not committed.
11311da177e4SLinus Torvalds  */
11326c75dc0dSFred Isaman static int nfs_flush_one(struct nfs_pageio_descriptor *desc,
11336c75dc0dSFred Isaman 			 struct nfs_pgio_header *hdr)
11341da177e4SLinus Torvalds {
11351da177e4SLinus Torvalds 	struct nfs_page		*req;
11361da177e4SLinus Torvalds 	struct page		**pages;
11371da177e4SLinus Torvalds 	struct nfs_write_data	*data;
1138c76069bdSFred Isaman 	struct list_head *head = &desc->pg_list;
1139ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
11401da177e4SLinus Torvalds 
11416c75dc0dSFred Isaman 	data = nfs_writedata_alloc(hdr, nfs_page_array_len(desc->pg_base,
1142c76069bdSFred Isaman 							   desc->pg_count));
11436c75dc0dSFred Isaman 	if (!data) {
114425b11dcdSTrond Myklebust 		nfs_flush_error(desc, hdr);
114525b11dcdSTrond Myklebust 		return -ENOMEM;
114644b83799SFred Isaman 	}
11476c75dc0dSFred Isaman 
1148ea2cf228SFred Isaman 	nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
114930dd374fSFred Isaman 	pages = data->pages.pagevec;
11501da177e4SLinus Torvalds 	while (!list_empty(head)) {
11511da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
11521da177e4SLinus Torvalds 		nfs_list_remove_request(req);
11536c75dc0dSFred Isaman 		nfs_list_add_request(req, &hdr->pages);
11541da177e4SLinus Torvalds 		*pages++ = req->wb_page;
11551da177e4SLinus Torvalds 	}
11561da177e4SLinus Torvalds 
1157b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1158ea2cf228SFred Isaman 	    (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
1159b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1160b31268acSTrond Myklebust 
11611da177e4SLinus Torvalds 	/* Set up the argument struct */
1162ea2cf228SFred Isaman 	nfs_write_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo);
11636c75dc0dSFred Isaman 	list_add(&data->list, &hdr->rpc_list);
11646c75dc0dSFred Isaman 	desc->pg_rpc_callops = &nfs_write_common_ops;
116525b11dcdSTrond Myklebust 	return 0;
11661da177e4SLinus Torvalds }
11671da177e4SLinus Torvalds 
11686c75dc0dSFred Isaman int nfs_generic_flush(struct nfs_pageio_descriptor *desc,
11696c75dc0dSFred Isaman 		      struct nfs_pgio_header *hdr)
1170dce81290STrond Myklebust {
1171dce81290STrond Myklebust 	if (desc->pg_bsize < PAGE_CACHE_SIZE)
11726c75dc0dSFred Isaman 		return nfs_flush_multi(desc, hdr);
11736c75dc0dSFred Isaman 	return nfs_flush_one(desc, hdr);
1174dce81290STrond Myklebust }
1175dce81290STrond Myklebust 
1176dce81290STrond Myklebust static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
11771751c363STrond Myklebust {
11786c75dc0dSFred Isaman 	struct nfs_write_header *whdr;
11796c75dc0dSFred Isaman 	struct nfs_pgio_header *hdr;
1180275acaafSTrond Myklebust 	int ret;
1181275acaafSTrond Myklebust 
11826c75dc0dSFred Isaman 	whdr = nfs_writehdr_alloc();
11836c75dc0dSFred Isaman 	if (!whdr) {
11849b5415b5STrond Myklebust 		desc->pg_completion_ops->error_cleanup(&desc->pg_list);
11856c75dc0dSFred Isaman 		return -ENOMEM;
11866c75dc0dSFred Isaman 	}
11876c75dc0dSFred Isaman 	hdr = &whdr->header;
11886c75dc0dSFred Isaman 	nfs_pgheader_init(desc, hdr, nfs_writehdr_free);
11896c75dc0dSFred Isaman 	atomic_inc(&hdr->refcnt);
11906c75dc0dSFred Isaman 	ret = nfs_generic_flush(desc, hdr);
1191275acaafSTrond Myklebust 	if (ret == 0)
11926c75dc0dSFred Isaman 		ret = nfs_do_multiple_writes(&hdr->rpc_list,
11936c75dc0dSFred Isaman 					     desc->pg_rpc_callops,
1194dce81290STrond Myklebust 					     desc->pg_ioflags);
11956c75dc0dSFred Isaman 	if (atomic_dec_and_test(&hdr->refcnt))
1196061ae2edSFred Isaman 		hdr->completion_ops->completion(hdr);
1197275acaafSTrond Myklebust 	return ret;
11981751c363STrond Myklebust }
11991751c363STrond Myklebust 
12001751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_write_ops = {
12011751c363STrond Myklebust 	.pg_test = nfs_generic_pg_test,
12021751c363STrond Myklebust 	.pg_doio = nfs_generic_pg_writepages,
12031751c363STrond Myklebust };
12041751c363STrond Myklebust 
1205e2fecb21STrond Myklebust void nfs_pageio_init_write_mds(struct nfs_pageio_descriptor *pgio,
1206061ae2edSFred Isaman 			       struct inode *inode, int ioflags,
1207061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
12081751c363STrond Myklebust {
1209061ae2edSFred Isaman 	nfs_pageio_init(pgio, inode, &nfs_pageio_write_ops, compl_ops,
12101751c363STrond Myklebust 				NFS_SERVER(inode)->wsize, ioflags);
12111751c363STrond Myklebust }
12121751c363STrond Myklebust 
1213dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1214dce81290STrond Myklebust {
1215dce81290STrond Myklebust 	pgio->pg_ops = &nfs_pageio_write_ops;
1216dce81290STrond Myklebust 	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1217dce81290STrond Myklebust }
12181f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1219dce81290STrond Myklebust 
12201763da12SFred Isaman void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1221061ae2edSFred Isaman 			   struct inode *inode, int ioflags,
1222061ae2edSFred Isaman 			   const struct nfs_pgio_completion_ops *compl_ops)
12231da177e4SLinus Torvalds {
1224061ae2edSFred Isaman 	if (!pnfs_pageio_init_write(pgio, inode, ioflags, compl_ops))
1225061ae2edSFred Isaman 		nfs_pageio_init_write_mds(pgio, inode, ioflags, compl_ops);
12261da177e4SLinus Torvalds }
12271da177e4SLinus Torvalds 
1228def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata)
1229def6ed7eSAndy Adamson {
1230def6ed7eSAndy Adamson 	struct nfs_write_data *data = calldata;
1231cd841605SFred Isaman 	NFS_PROTO(data->header->inode)->write_rpc_prepare(task, data);
1232def6ed7eSAndy Adamson }
1233def6ed7eSAndy Adamson 
12340b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
12350b7c0153SFred Isaman {
12360b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
12370b7c0153SFred Isaman 
12380b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
12390b7c0153SFred Isaman }
12400b7c0153SFred Isaman 
12411da177e4SLinus Torvalds /*
12421da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
12431da177e4SLinus Torvalds  *
12441da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
12451da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
12461da177e4SLinus Torvalds  *	  as the page has a write request pending.
12471da177e4SLinus Torvalds  */
12486c75dc0dSFred Isaman static void nfs_writeback_done_common(struct rpc_task *task, void *calldata)
12491da177e4SLinus Torvalds {
1250788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
12511da177e4SLinus Torvalds 
1252c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
1253c9d8f89dSTrond Myklebust }
1254c9d8f89dSTrond Myklebust 
12556c75dc0dSFred Isaman static void nfs_writeback_release_common(void *calldata)
1256c9d8f89dSTrond Myklebust {
1257c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1258cd841605SFred Isaman 	struct nfs_pgio_header *hdr = data->header;
1259e2fecb21STrond Myklebust 	int status = data->task.tk_status;
1260788e7a89STrond Myklebust 
12616c75dc0dSFred Isaman 	if ((status >= 0) && nfs_write_need_commit(data)) {
12626c75dc0dSFred Isaman 		spin_lock(&hdr->lock);
12636c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags))
12646c75dc0dSFred Isaman 			; /* Do nothing */
12656c75dc0dSFred Isaman 		else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags))
1266*9bce008bSTrond Myklebust 			memcpy(hdr->verf, &data->verf, sizeof(*hdr->verf));
1267*9bce008bSTrond Myklebust 		else if (memcmp(hdr->verf, &data->verf, sizeof(*hdr->verf)))
12686c75dc0dSFred Isaman 			set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags);
12696c75dc0dSFred Isaman 		spin_unlock(&hdr->lock);
12701da177e4SLinus Torvalds 	}
1271cd841605SFred Isaman 	nfs_writedata_release(data);
12721da177e4SLinus Torvalds }
12731da177e4SLinus Torvalds 
12746c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops = {
1275def6ed7eSAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
12766c75dc0dSFred Isaman 	.rpc_call_done = nfs_writeback_done_common,
12776c75dc0dSFred Isaman 	.rpc_release = nfs_writeback_release_common,
1278788e7a89STrond Myklebust };
1279788e7a89STrond Myklebust 
1280788e7a89STrond Myklebust 
12811da177e4SLinus Torvalds /*
12821da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
12831da177e4SLinus Torvalds  */
128413602896SFred Isaman void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
12851da177e4SLinus Torvalds {
12861da177e4SLinus Torvalds 	struct nfs_writeargs	*argp = &data->args;
12871da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
1288cd841605SFred Isaman 	struct inode		*inode = data->header->inode;
1289788e7a89STrond Myklebust 	int status;
12901da177e4SLinus Torvalds 
1291a3f565b1SChuck Lever 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
12921da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
12931da177e4SLinus Torvalds 
1294f551e44fSChuck Lever 	/*
1295f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1296f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1297f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1298f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1299f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1300f551e44fSChuck Lever 	 */
1301cd841605SFred Isaman 	status = NFS_PROTO(inode)->write_done(task, data);
1302788e7a89STrond Myklebust 	if (status != 0)
130313602896SFred Isaman 		return;
1304cd841605SFred Isaman 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
130591d5b470SChuck Lever 
13061da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
13071da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
13081da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
13091da177e4SLinus Torvalds 		 * commit data to stable storage even though we
13101da177e4SLinus Torvalds 		 * requested it.
13111da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
13121da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
13131da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
13141da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
13151da177e4SLinus Torvalds 		 */
13161da177e4SLinus Torvalds 		static unsigned long    complain;
13171da177e4SLinus Torvalds 
1318a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
13191da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
13201da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
13211da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1322cd841605SFred Isaman 				NFS_SERVER(inode)->nfs_client->cl_hostname,
13231da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
13241da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
13251da177e4SLinus Torvalds 		}
13261da177e4SLinus Torvalds 	}
13271da177e4SLinus Torvalds #endif
13286c75dc0dSFred Isaman 	if (task->tk_status < 0)
13296c75dc0dSFred Isaman 		nfs_set_pgio_error(data->header, task->tk_status, argp->offset);
13306c75dc0dSFred Isaman 	else if (resp->count < argp->count) {
13311da177e4SLinus Torvalds 		static unsigned long    complain;
13321da177e4SLinus Torvalds 
13336c75dc0dSFred Isaman 		/* This a short write! */
1334cd841605SFred Isaman 		nfs_inc_stats(inode, NFSIOS_SHORTWRITE);
133591d5b470SChuck Lever 
13361da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
13376c75dc0dSFred Isaman 		if (resp->count == 0) {
13386c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
13396c75dc0dSFred Isaman 				printk(KERN_WARNING
13406c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
13416c75dc0dSFred Isaman 				       argp->count);
13426c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
13436c75dc0dSFred Isaman 			}
13446c75dc0dSFred Isaman 			nfs_set_pgio_error(data->header, -EIO, argp->offset);
13456c75dc0dSFred Isaman 			task->tk_status = -EIO;
13466c75dc0dSFred Isaman 			return;
13476c75dc0dSFred Isaman 		}
13481da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
13491da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
13501da177e4SLinus Torvalds 			/* Resend from where the server left off */
1351a69aef14SFred Isaman 			data->mds_offset += resp->count;
13521da177e4SLinus Torvalds 			argp->offset += resp->count;
13531da177e4SLinus Torvalds 			argp->pgbase += resp->count;
13541da177e4SLinus Torvalds 			argp->count -= resp->count;
13551da177e4SLinus Torvalds 		} else {
13561da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
13571da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
13581da177e4SLinus Torvalds 			 */
13591da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
13601da177e4SLinus Torvalds 		}
1361d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
13621da177e4SLinus Torvalds 	}
13631da177e4SLinus Torvalds }
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
136771d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
136871d0a611STrond Myklebust {
1369b8413f98STrond Myklebust 	int ret;
1370b8413f98STrond Myklebust 
137171d0a611STrond Myklebust 	if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
137271d0a611STrond Myklebust 		return 1;
1373b8413f98STrond Myklebust 	if (!may_wait)
137471d0a611STrond Myklebust 		return 0;
1375b8413f98STrond Myklebust 	ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1376b8413f98STrond Myklebust 				NFS_INO_COMMIT,
1377b8413f98STrond Myklebust 				nfs_wait_bit_killable,
1378b8413f98STrond Myklebust 				TASK_KILLABLE);
1379b8413f98STrond Myklebust 	return (ret < 0) ? ret : 1;
138071d0a611STrond Myklebust }
138171d0a611STrond Myklebust 
1382f453a54aSFred Isaman static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
138371d0a611STrond Myklebust {
138471d0a611STrond Myklebust 	clear_bit(NFS_INO_COMMIT, &nfsi->flags);
138571d0a611STrond Myklebust 	smp_mb__after_clear_bit();
138671d0a611STrond Myklebust 	wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
138771d0a611STrond Myklebust }
138871d0a611STrond Myklebust 
13890b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
13901da177e4SLinus Torvalds {
13910b7c0153SFred Isaman 	put_nfs_open_context(data->context);
13920b7c0153SFred Isaman 	nfs_commit_free(data);
13931da177e4SLinus Torvalds }
1394e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
13951da177e4SLinus Torvalds 
13960b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
13979ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
13989f0ec176SAndy Adamson 			int how, int flags)
13991da177e4SLinus Torvalds {
140007737691STrond Myklebust 	struct rpc_task *task;
14019ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1402bdc7f021STrond Myklebust 	struct rpc_message msg = {
1403bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1404bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
14059ace33cdSFred Isaman 		.rpc_cred = data->cred,
1406bdc7f021STrond Myklebust 	};
140784115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
140807737691STrond Myklebust 		.task = &data->task,
14099ace33cdSFred Isaman 		.rpc_client = clnt,
1410bdc7f021STrond Myklebust 		.rpc_message = &msg,
14119ace33cdSFred Isaman 		.callback_ops = call_ops,
141284115e1cSTrond Myklebust 		.callback_data = data,
1413101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
14149f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
14153ff7576dSTrond Myklebust 		.priority = priority,
141684115e1cSTrond Myklebust 	};
1417788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
14189ace33cdSFred Isaman 	NFS_PROTO(data->inode)->commit_setup(data, &msg);
14191da177e4SLinus Torvalds 
1420a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1421bdc7f021STrond Myklebust 
142207737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1423dbae4c73STrond Myklebust 	if (IS_ERR(task))
1424dbae4c73STrond Myklebust 		return PTR_ERR(task);
1425d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1426d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
142707737691STrond Myklebust 	rpc_put_task(task);
1428dbae4c73STrond Myklebust 	return 0;
14291da177e4SLinus Torvalds }
1430e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds /*
14339ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
14349ace33cdSFred Isaman  */
14350b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1436988b6dceSFred Isaman 		     struct list_head *head,
1437f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1438f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
14399ace33cdSFred Isaman {
14409ace33cdSFred Isaman 	struct nfs_page *first = nfs_list_entry(head->next);
14413d4ff43dSAl Viro 	struct inode *inode = first->wb_context->dentry->d_inode;
14429ace33cdSFred Isaman 
14439ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
14449ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
14459ace33cdSFred Isaman 
14469ace33cdSFred Isaman 	list_splice_init(head, &data->pages);
14479ace33cdSFred Isaman 
14489ace33cdSFred Isaman 	data->inode	  = inode;
14499ace33cdSFred Isaman 	data->cred	  = first->wb_context->cred;
1450988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
14519ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1452f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1453b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
14549ace33cdSFred Isaman 
14559ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
14569ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
14579ace33cdSFred Isaman 	data->args.offset = 0;
14589ace33cdSFred Isaman 	data->args.count  = 0;
14590b7c0153SFred Isaman 	data->context     = get_nfs_open_context(first->wb_context);
14609ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
14619ace33cdSFred Isaman 	data->res.verf    = &data->verf;
14629ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
14639ace33cdSFred Isaman }
1464e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
14659ace33cdSFred Isaman 
1466e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1467ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1468ea2cf228SFred Isaman 		      struct nfs_commit_info *cinfo)
146964bfeb49SFred Isaman {
147064bfeb49SFred Isaman 	struct nfs_page *req;
147164bfeb49SFred Isaman 
147264bfeb49SFred Isaman 	while (!list_empty(page_list)) {
147364bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
147464bfeb49SFred Isaman 		nfs_list_remove_request(req);
1475ea2cf228SFred Isaman 		nfs_mark_request_commit(req, lseg, cinfo);
147656f9cd68SFred Isaman 		if (!cinfo->dreq) {
147764bfeb49SFred Isaman 			dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
147864bfeb49SFred Isaman 			dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
147964bfeb49SFred Isaman 				     BDI_RECLAIMABLE);
148056f9cd68SFred Isaman 		}
14811d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
148264bfeb49SFred Isaman 	}
148364bfeb49SFred Isaman }
1484e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
148564bfeb49SFred Isaman 
14869ace33cdSFred Isaman /*
14871da177e4SLinus Torvalds  * Commit dirty pages
14881da177e4SLinus Torvalds  */
14891da177e4SLinus Torvalds static int
1490ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1491ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
14921da177e4SLinus Torvalds {
14930b7c0153SFred Isaman 	struct nfs_commit_data	*data;
14941da177e4SLinus Torvalds 
1495c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
14961da177e4SLinus Torvalds 
14971da177e4SLinus Torvalds 	if (!data)
14981da177e4SLinus Torvalds 		goto out_bad;
14991da177e4SLinus Torvalds 
15001da177e4SLinus Torvalds 	/* Set up the argument struct */
1501f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
1502f453a54aSFred Isaman 	atomic_inc(&cinfo->mds->rpcs_out);
15039f0ec176SAndy Adamson 	return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops,
15049f0ec176SAndy Adamson 				   how, 0);
15051da177e4SLinus Torvalds  out_bad:
1506ea2cf228SFred Isaman 	nfs_retry_commit(head, NULL, cinfo);
1507f453a54aSFred Isaman 	cinfo->completion_ops->error_cleanup(NFS_I(inode));
15081da177e4SLinus Torvalds 	return -ENOMEM;
15091da177e4SLinus Torvalds }
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds /*
15121da177e4SLinus Torvalds  * COMMIT call returned
15131da177e4SLinus Torvalds  */
1514788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
15151da177e4SLinus Torvalds {
15160b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
15171da177e4SLinus Torvalds 
1518a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
15191da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
15201da177e4SLinus Torvalds 
1521788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1522c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
1523c9d8f89dSTrond Myklebust }
1524c9d8f89dSTrond Myklebust 
1525f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1526c9d8f89dSTrond Myklebust {
1527c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1528c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1529f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
1530788e7a89STrond Myklebust 
15311da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
15321da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
15331da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1534d6d6dc7cSFred Isaman 		nfs_clear_page_commit(req->wb_page);
15351da177e4SLinus Torvalds 
153648186c7dSChuck Lever 		dprintk("NFS:       commit (%s/%lld %d@%lld)",
15373d4ff43dSAl Viro 			req->wb_context->dentry->d_sb->s_id,
15383d4ff43dSAl Viro 			(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
15391da177e4SLinus Torvalds 			req->wb_bytes,
15401da177e4SLinus Torvalds 			(long long)req_offset(req));
1541c9d8f89dSTrond Myklebust 		if (status < 0) {
1542c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
15431da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1544c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
15451da177e4SLinus Torvalds 			goto next;
15461da177e4SLinus Torvalds 		}
15471da177e4SLinus Torvalds 
15481da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
15491da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
15501da177e4SLinus Torvalds 		if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
15511da177e4SLinus Torvalds 			/* We have a match */
15521da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
15531da177e4SLinus Torvalds 			dprintk(" OK\n");
15541da177e4SLinus Torvalds 			goto next;
15551da177e4SLinus Torvalds 		}
15561da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
15571da177e4SLinus Torvalds 		dprintk(" mismatch\n");
15586d884e8fSFred 		nfs_mark_request_dirty(req);
15591da177e4SLinus Torvalds 	next:
15601d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
15611da177e4SLinus Torvalds 	}
1562f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1563f453a54aSFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
1564f453a54aSFred Isaman 		nfs_commit_clear_lock(NFS_I(data->inode));
15655917ce84SFred Isaman }
15665917ce84SFred Isaman 
15675917ce84SFred Isaman static void nfs_commit_release(void *calldata)
15685917ce84SFred Isaman {
15690b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
15705917ce84SFred Isaman 
1571f453a54aSFred Isaman 	data->completion_ops->completion(data);
1572c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
15731da177e4SLinus Torvalds }
1574788e7a89STrond Myklebust 
1575788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
15760b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1577788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1578788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1579788e7a89STrond Myklebust };
15801da177e4SLinus Torvalds 
1581f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1582f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1583f453a54aSFred Isaman 	.error_cleanup = nfs_commit_clear_lock,
1584f453a54aSFred Isaman };
1585f453a54aSFred Isaman 
15861763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1587ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
158884c53ab5SFred Isaman {
158984c53ab5SFred Isaman 	int status;
159084c53ab5SFred Isaman 
1591ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
159284c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1593ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
159484c53ab5SFred Isaman 	return status;
159584c53ab5SFred Isaman }
159684c53ab5SFred Isaman 
1597b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
15981da177e4SLinus Torvalds {
15991da177e4SLinus Torvalds 	LIST_HEAD(head);
1600ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
160171d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1602b8413f98STrond Myklebust 	int res;
16031da177e4SLinus Torvalds 
1604b8413f98STrond Myklebust 	res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1605b8413f98STrond Myklebust 	if (res <= 0)
1606c5efa5fcSTrond Myklebust 		goto out_mark_dirty;
1607ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1608ea2cf228SFred Isaman 	res = nfs_scan_commit(inode, &head, &cinfo);
16091da177e4SLinus Torvalds 	if (res) {
1610a861a1e1SFred Isaman 		int error;
1611a861a1e1SFred Isaman 
1612ea2cf228SFred Isaman 		error = nfs_generic_commit_list(inode, &head, how, &cinfo);
16131da177e4SLinus Torvalds 		if (error < 0)
16141da177e4SLinus Torvalds 			return error;
1615b8413f98STrond Myklebust 		if (!may_wait)
1616b8413f98STrond Myklebust 			goto out_mark_dirty;
1617b8413f98STrond Myklebust 		error = wait_on_bit(&NFS_I(inode)->flags,
1618b8413f98STrond Myklebust 				NFS_INO_COMMIT,
161971d0a611STrond Myklebust 				nfs_wait_bit_killable,
162071d0a611STrond Myklebust 				TASK_KILLABLE);
1621b8413f98STrond Myklebust 		if (error < 0)
1622b8413f98STrond Myklebust 			return error;
162371d0a611STrond Myklebust 	} else
162471d0a611STrond Myklebust 		nfs_commit_clear_lock(NFS_I(inode));
1625c5efa5fcSTrond Myklebust 	return res;
1626c5efa5fcSTrond Myklebust 	/* Note: If we exit without ensuring that the commit is complete,
1627c5efa5fcSTrond Myklebust 	 * we must mark the inode as dirty. Otherwise, future calls to
1628c5efa5fcSTrond Myklebust 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1629c5efa5fcSTrond Myklebust 	 * that the data is on the disk.
1630c5efa5fcSTrond Myklebust 	 */
1631c5efa5fcSTrond Myklebust out_mark_dirty:
1632c5efa5fcSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
16331da177e4SLinus Torvalds 	return res;
16341da177e4SLinus Torvalds }
16358fc795f7STrond Myklebust 
16368fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
16378fc795f7STrond Myklebust {
1638420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1639420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1640420e3646STrond Myklebust 	int ret = 0;
16418fc795f7STrond Myklebust 
16423236c3e1SJeff Layton 	/* no commits means nothing needs to be done */
1643ea2cf228SFred Isaman 	if (!nfsi->commit_info.ncommit)
16443236c3e1SJeff Layton 		return ret;
16453236c3e1SJeff Layton 
1646a00dd6c0SJeff Layton 	if (wbc->sync_mode == WB_SYNC_NONE) {
1647a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1648a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1649420e3646STrond Myklebust 		 */
1650ea2cf228SFred Isaman 		if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1))
1651420e3646STrond Myklebust 			goto out_mark_dirty;
1652420e3646STrond Myklebust 
1653a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1654420e3646STrond Myklebust 		flags = 0;
1655a00dd6c0SJeff Layton 	}
1656a00dd6c0SJeff Layton 
1657420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1658420e3646STrond Myklebust 	if (ret >= 0) {
1659420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1660420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1661420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1662420e3646STrond Myklebust 			else
1663420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1664420e3646STrond Myklebust 		}
16658fc795f7STrond Myklebust 		return 0;
1666420e3646STrond Myklebust 	}
1667420e3646STrond Myklebust out_mark_dirty:
16688fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
16698fc795f7STrond Myklebust 	return ret;
16708fc795f7STrond Myklebust }
1671c63c7b05STrond Myklebust #else
16728fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
16738fc795f7STrond Myklebust {
16748fc795f7STrond Myklebust 	return 0;
16758fc795f7STrond Myklebust }
16761da177e4SLinus Torvalds #endif
16771da177e4SLinus Torvalds 
16788fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
16798fc795f7STrond Myklebust {
1680863a3c6cSAndy Adamson 	int ret;
1681863a3c6cSAndy Adamson 
1682863a3c6cSAndy Adamson 	ret = nfs_commit_unstable_pages(inode, wbc);
1683863a3c6cSAndy Adamson 	if (ret >= 0 && test_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(inode)->flags)) {
1684ef311537SAndy Adamson 		int status;
1685ef311537SAndy Adamson 		bool sync = true;
1686863a3c6cSAndy Adamson 
1687846d5a09SWu Fengguang 		if (wbc->sync_mode == WB_SYNC_NONE)
1688ef311537SAndy Adamson 			sync = false;
1689863a3c6cSAndy Adamson 
1690863a3c6cSAndy Adamson 		status = pnfs_layoutcommit_inode(inode, sync);
1691863a3c6cSAndy Adamson 		if (status < 0)
1692863a3c6cSAndy Adamson 			return status;
1693863a3c6cSAndy Adamson 	}
1694863a3c6cSAndy Adamson 	return ret;
16958fc795f7STrond Myklebust }
16968fc795f7STrond Myklebust 
1697acdc53b2STrond Myklebust /*
1698acdc53b2STrond Myklebust  * flush the inode to disk.
1699acdc53b2STrond Myklebust  */
1700acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
170134901f70STrond Myklebust {
170234901f70STrond Myklebust 	struct writeback_control wbc = {
170372cb77f4STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
170434901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
1705d7fb1207STrond Myklebust 		.range_start = 0,
1706d7fb1207STrond Myklebust 		.range_end = LLONG_MAX,
170734901f70STrond Myklebust 	};
170834901f70STrond Myklebust 
1709acdc53b2STrond Myklebust 	return sync_inode(inode, &wbc);
17101c75950bSTrond Myklebust }
17111c75950bSTrond Myklebust 
17121b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
17131b3b4a1aSTrond Myklebust {
17141b3b4a1aSTrond Myklebust 	struct nfs_page *req;
17151b3b4a1aSTrond Myklebust 	int ret = 0;
17161b3b4a1aSTrond Myklebust 
17171b3b4a1aSTrond Myklebust 	BUG_ON(!PageLocked(page));
17181b3b4a1aSTrond Myklebust 	for (;;) {
1719ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
17201b3b4a1aSTrond Myklebust 		req = nfs_page_find_request(page);
17211b3b4a1aSTrond Myklebust 		if (req == NULL)
17221b3b4a1aSTrond Myklebust 			break;
17237ad84aa9STrond Myklebust 		if (nfs_lock_request(req)) {
17248dd37758STrond Myklebust 			nfs_clear_request_commit(req);
17251b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
17261b3b4a1aSTrond Myklebust 			/*
17271b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
17281b3b4a1aSTrond Myklebust 			 * page as being dirty
17291b3b4a1aSTrond Myklebust 			 */
17301b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
17311d1afcbcSTrond Myklebust 			nfs_unlock_and_release_request(req);
17321b3b4a1aSTrond Myklebust 			break;
17331b3b4a1aSTrond Myklebust 		}
17341b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
1735c9edda71STrond Myklebust 		nfs_release_request(req);
17361b3b4a1aSTrond Myklebust 		if (ret < 0)
1737c988950eSTrond Myklebust 			break;
17381b3b4a1aSTrond Myklebust 	}
17391b3b4a1aSTrond Myklebust 	return ret;
17401b3b4a1aSTrond Myklebust }
17411b3b4a1aSTrond Myklebust 
17421c75950bSTrond Myklebust /*
17431c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
17441c75950bSTrond Myklebust  */
17451c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
17461c75950bSTrond Myklebust {
17477f2f12d9STrond Myklebust 	loff_t range_start = page_offset(page);
17487f2f12d9STrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
17497f2f12d9STrond Myklebust 	struct writeback_control wbc = {
17507f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
17517f2f12d9STrond Myklebust 		.nr_to_write = 0,
17527f2f12d9STrond Myklebust 		.range_start = range_start,
17537f2f12d9STrond Myklebust 		.range_end = range_end,
17547f2f12d9STrond Myklebust 	};
17557f2f12d9STrond Myklebust 	int ret;
17567f2f12d9STrond Myklebust 
17570522f6adSTrond Myklebust 	for (;;) {
1758ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
17597f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
17607f2f12d9STrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
17617f2f12d9STrond Myklebust 			if (ret < 0)
17627f2f12d9STrond Myklebust 				goto out_error;
17630522f6adSTrond Myklebust 			continue;
17647f2f12d9STrond Myklebust 		}
17650522f6adSTrond Myklebust 		if (!PagePrivate(page))
17660522f6adSTrond Myklebust 			break;
17670522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
17687f2f12d9STrond Myklebust 		if (ret < 0)
17697f2f12d9STrond Myklebust 			goto out_error;
17707f2f12d9STrond Myklebust 	}
17717f2f12d9STrond Myklebust 	return 0;
17727f2f12d9STrond Myklebust out_error:
17737f2f12d9STrond Myklebust 	return ret;
17741c75950bSTrond Myklebust }
17751c75950bSTrond Myklebust 
1776074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
1777074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1778a6bc32b8SMel Gorman 		struct page *page, enum migrate_mode mode)
1779074cc1deSTrond Myklebust {
17802da95652SJeff Layton 	/*
17812da95652SJeff Layton 	 * If PagePrivate is set, then the page is currently associated with
17822da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
17832da95652SJeff Layton 	 *
17842da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
17852da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
17862da95652SJeff Layton 	 *        the page lock.
17872da95652SJeff Layton 	 */
17882da95652SJeff Layton 	if (PagePrivate(page))
17892da95652SJeff Layton 		return -EBUSY;
1790074cc1deSTrond Myklebust 
1791074cc1deSTrond Myklebust 	nfs_fscache_release_page(page, GFP_KERNEL);
1792074cc1deSTrond Myklebust 
1793a6bc32b8SMel Gorman 	return migrate_page(mapping, newpage, page, mode);
1794074cc1deSTrond Myklebust }
1795074cc1deSTrond Myklebust #endif
1796074cc1deSTrond Myklebust 
1797f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
17981da177e4SLinus Torvalds {
17991da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1800cd841605SFred Isaman 					     sizeof(struct nfs_write_header),
18011da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
180220c2df83SPaul Mundt 					     NULL);
18031da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
18041da177e4SLinus Torvalds 		return -ENOMEM;
18051da177e4SLinus Torvalds 
180693d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
18071da177e4SLinus Torvalds 						     nfs_wdata_cachep);
18081da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
18091da177e4SLinus Torvalds 		return -ENOMEM;
18101da177e4SLinus Torvalds 
18110b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
18120b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
18130b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
18140b7c0153SFred Isaman 					     NULL);
18150b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
18160b7c0153SFred Isaman 		return -ENOMEM;
18170b7c0153SFred Isaman 
181893d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
18191da177e4SLinus Torvalds 						      nfs_wdata_cachep);
18201da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
18211da177e4SLinus Torvalds 		return -ENOMEM;
18221da177e4SLinus Torvalds 
182389a09141SPeter Zijlstra 	/*
182489a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
182589a09141SPeter Zijlstra 	 *
182689a09141SPeter Zijlstra 	 *  64MB:    8192k
182789a09141SPeter Zijlstra 	 * 128MB:   11585k
182889a09141SPeter Zijlstra 	 * 256MB:   16384k
182989a09141SPeter Zijlstra 	 * 512MB:   23170k
183089a09141SPeter Zijlstra 	 *   1GB:   32768k
183189a09141SPeter Zijlstra 	 *   2GB:   46340k
183289a09141SPeter Zijlstra 	 *   4GB:   65536k
183389a09141SPeter Zijlstra 	 *   8GB:   92681k
183489a09141SPeter Zijlstra 	 *  16GB:  131072k
183589a09141SPeter Zijlstra 	 *
183689a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
183789a09141SPeter Zijlstra 	 * Limit the default to 256M
183889a09141SPeter Zijlstra 	 */
183989a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
184089a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
184189a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
184289a09141SPeter Zijlstra 
18431da177e4SLinus Torvalds 	return 0;
18441da177e4SLinus Torvalds }
18451da177e4SLinus Torvalds 
1846266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
18471da177e4SLinus Torvalds {
18481da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
18491da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
18501a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
18511da177e4SLinus Torvalds }
18521da177e4SLinus Torvalds 
1853