xref: /linux/fs/nfs/write.c (revision 7ad84aa9448571678c243f0c5ef383fbe5b50f4f)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/fs/nfs/write.c
31da177e4SLinus Torvalds  *
47c85d900STrond Myklebust  * Write file data over NFS.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/types.h>
101da177e4SLinus Torvalds #include <linux/slab.h>
111da177e4SLinus Torvalds #include <linux/mm.h>
121da177e4SLinus Torvalds #include <linux/pagemap.h>
131da177e4SLinus Torvalds #include <linux/file.h>
141da177e4SLinus Torvalds #include <linux/writeback.h>
1589a09141SPeter Zijlstra #include <linux/swap.h>
16074cc1deSTrond Myklebust #include <linux/migrate.h>
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
191da177e4SLinus Torvalds #include <linux/nfs_fs.h>
201da177e4SLinus Torvalds #include <linux/nfs_mount.h>
211da177e4SLinus Torvalds #include <linux/nfs_page.h>
223fcfab16SAndrew Morton #include <linux/backing-dev.h>
23afeacc8cSPaul Gortmaker #include <linux/export.h>
243fcfab16SAndrew Morton 
251da177e4SLinus Torvalds #include <asm/uaccess.h>
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #include "delegation.h"
2849a70f27STrond Myklebust #include "internal.h"
2991d5b470SChuck Lever #include "iostat.h"
30def6ed7eSAndy Adamson #include "nfs4_fs.h"
31074cc1deSTrond Myklebust #include "fscache.h"
3294ad1c80SFred Isaman #include "pnfs.h"
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #define MIN_POOL_WRITE		(32)
371da177e4SLinus Torvalds #define MIN_POOL_COMMIT		(4)
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds /*
401da177e4SLinus Torvalds  * Local function declarations
411da177e4SLinus Torvalds  */
42f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req);
436c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops;
44788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops;
45061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops;
46f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops;
471da177e4SLinus Torvalds 
48e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep;
493feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
500b7c0153SFred Isaman static struct kmem_cache *nfs_cdata_cachep;
511da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
521da177e4SLinus Torvalds 
530b7c0153SFred Isaman struct nfs_commit_data *nfs_commitdata_alloc(void)
541da177e4SLinus Torvalds {
550b7c0153SFred Isaman 	struct nfs_commit_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
5640859d7eSChuck Lever 
571da177e4SLinus Torvalds 	if (p) {
581da177e4SLinus Torvalds 		memset(p, 0, sizeof(*p));
591da177e4SLinus Torvalds 		INIT_LIST_HEAD(&p->pages);
601da177e4SLinus Torvalds 	}
611da177e4SLinus Torvalds 	return p;
621da177e4SLinus Torvalds }
63e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
641da177e4SLinus Torvalds 
650b7c0153SFred Isaman void nfs_commit_free(struct nfs_commit_data *p)
661da177e4SLinus Torvalds {
671da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
681da177e4SLinus Torvalds }
69e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free);
701da177e4SLinus Torvalds 
716c75dc0dSFred Isaman struct nfs_write_header *nfs_writehdr_alloc(void)
723feb2d49STrond Myklebust {
73cd841605SFred Isaman 	struct nfs_write_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
743feb2d49STrond Myklebust 
753feb2d49STrond Myklebust 	if (p) {
76cd841605SFred Isaman 		struct nfs_pgio_header *hdr = &p->header;
77cd841605SFred Isaman 
783feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
79cd841605SFred Isaman 		INIT_LIST_HEAD(&hdr->pages);
806c75dc0dSFred Isaman 		INIT_LIST_HEAD(&hdr->rpc_list);
816c75dc0dSFred Isaman 		spin_lock_init(&hdr->lock);
826c75dc0dSFred Isaman 		atomic_set(&hdr->refcnt, 0);
833feb2d49STrond Myklebust 	}
843feb2d49STrond Myklebust 	return p;
853feb2d49STrond Myklebust }
863feb2d49STrond Myklebust 
871763da12SFred Isaman static struct nfs_write_data *nfs_writedata_alloc(struct nfs_pgio_header *hdr,
886c75dc0dSFred Isaman 						  unsigned int pagecount)
896c75dc0dSFred Isaman {
906c75dc0dSFred Isaman 	struct nfs_write_data *data, *prealloc;
916c75dc0dSFred Isaman 
926c75dc0dSFred Isaman 	prealloc = &container_of(hdr, struct nfs_write_header, header)->rpc_data;
936c75dc0dSFred Isaman 	if (prealloc->header == NULL)
946c75dc0dSFred Isaman 		data = prealloc;
956c75dc0dSFred Isaman 	else
966c75dc0dSFred Isaman 		data = kzalloc(sizeof(*data), GFP_KERNEL);
976c75dc0dSFred Isaman 	if (!data)
986c75dc0dSFred Isaman 		goto out;
996c75dc0dSFred Isaman 
1006c75dc0dSFred Isaman 	if (nfs_pgarray_set(&data->pages, pagecount)) {
1016c75dc0dSFred Isaman 		data->header = hdr;
1026c75dc0dSFred Isaman 		atomic_inc(&hdr->refcnt);
1036c75dc0dSFred Isaman 	} else {
1046c75dc0dSFred Isaman 		if (data != prealloc)
1056c75dc0dSFred Isaman 			kfree(data);
1066c75dc0dSFred Isaman 		data = NULL;
1076c75dc0dSFred Isaman 	}
1086c75dc0dSFred Isaman out:
1096c75dc0dSFred Isaman 	return data;
1106c75dc0dSFred Isaman }
1116c75dc0dSFred Isaman 
112cd841605SFred Isaman void nfs_writehdr_free(struct nfs_pgio_header *hdr)
1133feb2d49STrond Myklebust {
114cd841605SFred Isaman 	struct nfs_write_header *whdr = container_of(hdr, struct nfs_write_header, header);
115cd841605SFred Isaman 	mempool_free(whdr, nfs_wdata_mempool);
1163feb2d49STrond Myklebust }
1173feb2d49STrond Myklebust 
118dce81290STrond Myklebust void nfs_writedata_release(struct nfs_write_data *wdata)
1191da177e4SLinus Torvalds {
1206c75dc0dSFred Isaman 	struct nfs_pgio_header *hdr = wdata->header;
1216c75dc0dSFred Isaman 	struct nfs_write_header *write_header = container_of(hdr, struct nfs_write_header, header);
1226c75dc0dSFred Isaman 
123383ba719STrond Myklebust 	put_nfs_open_context(wdata->args.context);
12430dd374fSFred Isaman 	if (wdata->pages.pagevec != wdata->pages.page_array)
12530dd374fSFred Isaman 		kfree(wdata->pages.pagevec);
1266c75dc0dSFred Isaman 	if (wdata != &write_header->rpc_data)
1276c75dc0dSFred Isaman 		kfree(wdata);
1286c75dc0dSFred Isaman 	else
1296c75dc0dSFred Isaman 		wdata->header = NULL;
1306c75dc0dSFred Isaman 	if (atomic_dec_and_test(&hdr->refcnt))
131061ae2edSFred Isaman 		hdr->completion_ops->completion(hdr);
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds 
1347b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
1357b159fc1STrond Myklebust {
1367b159fc1STrond Myklebust 	ctx->error = error;
1377b159fc1STrond Myklebust 	smp_wmb();
1387b159fc1STrond Myklebust 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
1397b159fc1STrond Myklebust }
1407b159fc1STrond Myklebust 
141277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request_locked(struct page *page)
142277459d2STrond Myklebust {
143277459d2STrond Myklebust 	struct nfs_page *req = NULL;
144277459d2STrond Myklebust 
145277459d2STrond Myklebust 	if (PagePrivate(page)) {
146277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
147277459d2STrond Myklebust 		if (req != NULL)
148c03b4024STrond Myklebust 			kref_get(&req->wb_kref);
149277459d2STrond Myklebust 	}
150277459d2STrond Myklebust 	return req;
151277459d2STrond Myklebust }
152277459d2STrond Myklebust 
153277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page)
154277459d2STrond Myklebust {
155587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
156277459d2STrond Myklebust 	struct nfs_page *req = NULL;
157277459d2STrond Myklebust 
158587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
159277459d2STrond Myklebust 	req = nfs_page_find_request_locked(page);
160587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
161277459d2STrond Myklebust 	return req;
162277459d2STrond Myklebust }
163277459d2STrond Myklebust 
1641da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1651da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1661da177e4SLinus Torvalds {
1671da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
168a3d01454STrond Myklebust 	loff_t end, i_size;
169a3d01454STrond Myklebust 	pgoff_t end_index;
1701da177e4SLinus Torvalds 
171a3d01454STrond Myklebust 	spin_lock(&inode->i_lock);
172a3d01454STrond Myklebust 	i_size = i_size_read(inode);
173a3d01454STrond Myklebust 	end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
1741da177e4SLinus Torvalds 	if (i_size > 0 && page->index < end_index)
175a3d01454STrond Myklebust 		goto out;
1761da177e4SLinus Torvalds 	end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
1771da177e4SLinus Torvalds 	if (i_size >= end)
178a3d01454STrond Myklebust 		goto out;
1791da177e4SLinus Torvalds 	i_size_write(inode, end);
180a3d01454STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
181a3d01454STrond Myklebust out:
182a3d01454STrond Myklebust 	spin_unlock(&inode->i_lock);
1831da177e4SLinus Torvalds }
1841da177e4SLinus Torvalds 
185a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
186a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
187a301b777STrond Myklebust {
188a301b777STrond Myklebust 	SetPageError(page);
189a301b777STrond Myklebust 	nfs_zap_mapping(page->mapping->host, page->mapping);
190a301b777STrond Myklebust }
191a301b777STrond Myklebust 
1921da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
1931da177e4SLinus Torvalds  * covers the full page.
1941da177e4SLinus Torvalds  */
1951da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
1961da177e4SLinus Torvalds {
1971da177e4SLinus Torvalds 	if (PageUptodate(page))
1981da177e4SLinus Torvalds 		return;
1991da177e4SLinus Torvalds 	if (base != 0)
2001da177e4SLinus Torvalds 		return;
20149a70f27STrond Myklebust 	if (count != nfs_page_length(page))
2021da177e4SLinus Torvalds 		return;
2031da177e4SLinus Torvalds 	SetPageUptodate(page);
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
2071da177e4SLinus Torvalds {
2081da177e4SLinus Torvalds 	if (wbc->for_reclaim)
209c63c7b05STrond Myklebust 		return FLUSH_HIGHPRI | FLUSH_STABLE;
210b17621feSWu Fengguang 	if (wbc->for_kupdate || wbc->for_background)
211b31268acSTrond Myklebust 		return FLUSH_LOWPRI | FLUSH_COND_STABLE;
212b31268acSTrond Myklebust 	return FLUSH_COND_STABLE;
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds /*
21689a09141SPeter Zijlstra  * NFS congestion control
21789a09141SPeter Zijlstra  */
21889a09141SPeter Zijlstra 
21989a09141SPeter Zijlstra int nfs_congestion_kb;
22089a09141SPeter Zijlstra 
22189a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
22289a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
22389a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
22489a09141SPeter Zijlstra 
2255a6d41b3STrond Myklebust static int nfs_set_page_writeback(struct page *page)
22689a09141SPeter Zijlstra {
2275a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
2285a6d41b3STrond Myklebust 
2295a6d41b3STrond Myklebust 	if (!ret) {
23089a09141SPeter Zijlstra 		struct inode *inode = page->mapping->host;
23189a09141SPeter Zijlstra 		struct nfs_server *nfss = NFS_SERVER(inode);
23289a09141SPeter Zijlstra 
233277866a0SPeter Zijlstra 		if (atomic_long_inc_return(&nfss->writeback) >
2348aa7e847SJens Axboe 				NFS_CONGESTION_ON_THRESH) {
2358aa7e847SJens Axboe 			set_bdi_congested(&nfss->backing_dev_info,
2368aa7e847SJens Axboe 						BLK_RW_ASYNC);
2378aa7e847SJens Axboe 		}
23889a09141SPeter Zijlstra 	}
2395a6d41b3STrond Myklebust 	return ret;
24089a09141SPeter Zijlstra }
24189a09141SPeter Zijlstra 
24289a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page)
24389a09141SPeter Zijlstra {
24489a09141SPeter Zijlstra 	struct inode *inode = page->mapping->host;
24589a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
24689a09141SPeter Zijlstra 
24789a09141SPeter Zijlstra 	end_page_writeback(page);
248c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
2498aa7e847SJens Axboe 		clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
25089a09141SPeter Zijlstra }
25189a09141SPeter Zijlstra 
252cfb506e1STrond Myklebust static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock)
253e261f51fSTrond Myklebust {
254587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
255e261f51fSTrond Myklebust 	struct nfs_page *req;
256e261f51fSTrond Myklebust 	int ret;
257e261f51fSTrond Myklebust 
258587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
259e261f51fSTrond Myklebust 	for (;;) {
260e261f51fSTrond Myklebust 		req = nfs_page_find_request_locked(page);
261074cc1deSTrond Myklebust 		if (req == NULL)
262074cc1deSTrond Myklebust 			break;
263*7ad84aa9STrond Myklebust 		if (nfs_lock_request(req))
264e261f51fSTrond Myklebust 			break;
265e261f51fSTrond Myklebust 		/* Note: If we hold the page lock, as is the case in nfs_writepage,
266*7ad84aa9STrond Myklebust 		 *	 then the call to nfs_lock_request() will always
267e261f51fSTrond Myklebust 		 *	 succeed provided that someone hasn't already marked the
268e261f51fSTrond Myklebust 		 *	 request as dirty (in which case we don't care).
269e261f51fSTrond Myklebust 		 */
270587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
271cfb506e1STrond Myklebust 		if (!nonblock)
272e261f51fSTrond Myklebust 			ret = nfs_wait_on_request(req);
273cfb506e1STrond Myklebust 		else
274cfb506e1STrond Myklebust 			ret = -EAGAIN;
275e261f51fSTrond Myklebust 		nfs_release_request(req);
276e261f51fSTrond Myklebust 		if (ret != 0)
277074cc1deSTrond Myklebust 			return ERR_PTR(ret);
278587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
279e261f51fSTrond Myklebust 	}
280587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
281074cc1deSTrond Myklebust 	return req;
282612c9384STrond Myklebust }
283074cc1deSTrond Myklebust 
284074cc1deSTrond Myklebust /*
285074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
286074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
287074cc1deSTrond Myklebust  */
288074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
289cfb506e1STrond Myklebust 				struct page *page, bool nonblock)
290074cc1deSTrond Myklebust {
291074cc1deSTrond Myklebust 	struct nfs_page *req;
292074cc1deSTrond Myklebust 	int ret = 0;
293074cc1deSTrond Myklebust 
294cfb506e1STrond Myklebust 	req = nfs_find_and_lock_request(page, nonblock);
295074cc1deSTrond Myklebust 	if (!req)
296074cc1deSTrond Myklebust 		goto out;
297074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
298074cc1deSTrond Myklebust 	if (IS_ERR(req))
299074cc1deSTrond Myklebust 		goto out;
300074cc1deSTrond Myklebust 
301074cc1deSTrond Myklebust 	ret = nfs_set_page_writeback(page);
302074cc1deSTrond Myklebust 	BUG_ON(ret != 0);
303074cc1deSTrond Myklebust 	BUG_ON(test_bit(PG_CLEAN, &req->wb_flags));
304074cc1deSTrond Myklebust 
305f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
306f8512ad0SFred Isaman 		nfs_redirty_request(req);
307074cc1deSTrond Myklebust 		ret = pgio->pg_error;
308f8512ad0SFred Isaman 	}
309074cc1deSTrond Myklebust out:
310074cc1deSTrond Myklebust 	return ret;
311e261f51fSTrond Myklebust }
312e261f51fSTrond Myklebust 
313f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
314f758c885STrond Myklebust {
315f758c885STrond Myklebust 	struct inode *inode = page->mapping->host;
316cfb506e1STrond Myklebust 	int ret;
317f758c885STrond Myklebust 
318f758c885STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
319f758c885STrond Myklebust 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
320f758c885STrond Myklebust 
321f758c885STrond Myklebust 	nfs_pageio_cond_complete(pgio, page->index);
3221b430beeSWu Fengguang 	ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
323cfb506e1STrond Myklebust 	if (ret == -EAGAIN) {
324cfb506e1STrond Myklebust 		redirty_page_for_writepage(wbc, page);
325cfb506e1STrond Myklebust 		ret = 0;
326cfb506e1STrond Myklebust 	}
327cfb506e1STrond Myklebust 	return ret;
328f758c885STrond Myklebust }
329f758c885STrond Myklebust 
330e261f51fSTrond Myklebust /*
3311da177e4SLinus Torvalds  * Write an mmapped page to the server.
3321da177e4SLinus Torvalds  */
3334d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
3341da177e4SLinus Torvalds {
335f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
336e261f51fSTrond Myklebust 	int err;
3371da177e4SLinus Torvalds 
338061ae2edSFred Isaman 	nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc),
339061ae2edSFred Isaman 			      &nfs_async_write_completion_ops);
340f758c885STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
341f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
342f758c885STrond Myklebust 	if (err < 0)
3434d770ccfSTrond Myklebust 		return err;
344f758c885STrond Myklebust 	if (pgio.pg_error < 0)
345f758c885STrond Myklebust 		return pgio.pg_error;
346f758c885STrond Myklebust 	return 0;
3474d770ccfSTrond Myklebust }
3484d770ccfSTrond Myklebust 
3494d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
3504d770ccfSTrond Myklebust {
351f758c885STrond Myklebust 	int ret;
3524d770ccfSTrond Myklebust 
353f758c885STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
3541da177e4SLinus Torvalds 	unlock_page(page);
355f758c885STrond Myklebust 	return ret;
356f758c885STrond Myklebust }
357f758c885STrond Myklebust 
358f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
359f758c885STrond Myklebust {
360f758c885STrond Myklebust 	int ret;
361f758c885STrond Myklebust 
362f758c885STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
363f758c885STrond Myklebust 	unlock_page(page);
364f758c885STrond Myklebust 	return ret;
3651da177e4SLinus Torvalds }
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3681da177e4SLinus Torvalds {
3691da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
37072cb77f4STrond Myklebust 	unsigned long *bitlock = &NFS_I(inode)->flags;
371c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
3721da177e4SLinus Torvalds 	int err;
3731da177e4SLinus Torvalds 
37472cb77f4STrond Myklebust 	/* Stop dirtying of new pages while we sync */
37572cb77f4STrond Myklebust 	err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
37672cb77f4STrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
37772cb77f4STrond Myklebust 	if (err)
37872cb77f4STrond Myklebust 		goto out_err;
37972cb77f4STrond Myklebust 
38091d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
38191d5b470SChuck Lever 
382061ae2edSFred Isaman 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc),
383061ae2edSFred Isaman 			      &nfs_async_write_completion_ops);
384f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
385c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
38672cb77f4STrond Myklebust 
38772cb77f4STrond Myklebust 	clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
38872cb77f4STrond Myklebust 	smp_mb__after_clear_bit();
38972cb77f4STrond Myklebust 	wake_up_bit(bitlock, NFS_INO_FLUSHING);
39072cb77f4STrond Myklebust 
391f758c885STrond Myklebust 	if (err < 0)
39272cb77f4STrond Myklebust 		goto out_err;
39372cb77f4STrond Myklebust 	err = pgio.pg_error;
39472cb77f4STrond Myklebust 	if (err < 0)
39572cb77f4STrond Myklebust 		goto out_err;
396c63c7b05STrond Myklebust 	return 0;
39772cb77f4STrond Myklebust out_err:
39872cb77f4STrond Myklebust 	return err;
3991da177e4SLinus Torvalds }
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds /*
4021da177e4SLinus Torvalds  * Insert a write request into an inode
4031da177e4SLinus Torvalds  */
404d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
4051da177e4SLinus Torvalds {
4061da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
407e7d39069STrond Myklebust 
408e7d39069STrond Myklebust 	/* Lock the request! */
409*7ad84aa9STrond Myklebust 	nfs_lock_request(req);
410e7d39069STrond Myklebust 
411e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
4124d65c520STrond Myklebust 	if (!nfsi->npages && nfs_have_delegation(inode, FMODE_WRITE))
413a9a4a87aSTrond Myklebust 		inode->i_version++;
4142df485a7STrond Myklebust 	set_bit(PG_MAPPED, &req->wb_flags);
415deb7d638STrond Myklebust 	SetPagePrivate(req->wb_page);
416277459d2STrond Myklebust 	set_page_private(req->wb_page, (unsigned long)req);
4171da177e4SLinus Torvalds 	nfsi->npages++;
418c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
419e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
4201da177e4SLinus Torvalds }
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds /*
42389a09141SPeter Zijlstra  * Remove a write request from an inode
4241da177e4SLinus Torvalds  */
4251da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
4261da177e4SLinus Torvalds {
4273d4ff43dSAl Viro 	struct inode *inode = req->wb_context->dentry->d_inode;
4281da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	BUG_ON (!NFS_WBACK_BUSY(req));
4311da177e4SLinus Torvalds 
432587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
433277459d2STrond Myklebust 	set_page_private(req->wb_page, 0);
434deb7d638STrond Myklebust 	ClearPagePrivate(req->wb_page);
4352df485a7STrond Myklebust 	clear_bit(PG_MAPPED, &req->wb_flags);
4361da177e4SLinus Torvalds 	nfsi->npages--;
437587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
4381da177e4SLinus Torvalds 	nfs_release_request(req);
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds 
44161822ab5STrond Myklebust static void
4426d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
44361822ab5STrond Myklebust {
44461822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
44561822ab5STrond Myklebust }
44661822ab5STrond Myklebust 
4471da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
4488dd37758STrond Myklebust /**
4498dd37758STrond Myklebust  * nfs_request_add_commit_list - add request to a commit list
4508dd37758STrond Myklebust  * @req: pointer to a struct nfs_page
451ea2cf228SFred Isaman  * @dst: commit list head
452ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
4538dd37758STrond Myklebust  *
454ea2cf228SFred Isaman  * This sets the PG_CLEAN bit, updates the cinfo count of
4558dd37758STrond Myklebust  * number of outstanding requests requiring a commit as well as
4568dd37758STrond Myklebust  * the MM page stats.
4578dd37758STrond Myklebust  *
458ea2cf228SFred Isaman  * The caller must _not_ hold the cinfo->lock, but must be
4598dd37758STrond Myklebust  * holding the nfs_page lock.
4608dd37758STrond Myklebust  */
4618dd37758STrond Myklebust void
462ea2cf228SFred Isaman nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst,
463ea2cf228SFred Isaman 			    struct nfs_commit_info *cinfo)
4648dd37758STrond Myklebust {
4658dd37758STrond Myklebust 	set_bit(PG_CLEAN, &(req)->wb_flags);
466ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
467ea2cf228SFred Isaman 	nfs_list_add_request(req, dst);
468ea2cf228SFred Isaman 	cinfo->mds->ncommit++;
469ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
47056f9cd68SFred Isaman 	if (!cinfo->dreq) {
4718dd37758STrond Myklebust 		inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
47256f9cd68SFred Isaman 		inc_bdi_stat(req->wb_page->mapping->backing_dev_info,
47356f9cd68SFred Isaman 			     BDI_RECLAIMABLE);
47456f9cd68SFred Isaman 		__mark_inode_dirty(req->wb_context->dentry->d_inode,
47556f9cd68SFred Isaman 				   I_DIRTY_DATASYNC);
47656f9cd68SFred Isaman 	}
4778dd37758STrond Myklebust }
4788dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
4798dd37758STrond Myklebust 
4808dd37758STrond Myklebust /**
4818dd37758STrond Myklebust  * nfs_request_remove_commit_list - Remove request from a commit list
4828dd37758STrond Myklebust  * @req: pointer to a nfs_page
483ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
4848dd37758STrond Myklebust  *
485ea2cf228SFred Isaman  * This clears the PG_CLEAN bit, and updates the cinfo's count of
4868dd37758STrond Myklebust  * number of outstanding requests requiring a commit
4878dd37758STrond Myklebust  * It does not update the MM page stats.
4888dd37758STrond Myklebust  *
489ea2cf228SFred Isaman  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
4908dd37758STrond Myklebust  */
4918dd37758STrond Myklebust void
492ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req,
493ea2cf228SFred Isaman 			       struct nfs_commit_info *cinfo)
4948dd37758STrond Myklebust {
4958dd37758STrond Myklebust 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
4968dd37758STrond Myklebust 		return;
4978dd37758STrond Myklebust 	nfs_list_remove_request(req);
498ea2cf228SFred Isaman 	cinfo->mds->ncommit--;
4998dd37758STrond Myklebust }
5008dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
5018dd37758STrond Myklebust 
502ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
503ea2cf228SFred Isaman 				      struct inode *inode)
504ea2cf228SFred Isaman {
505ea2cf228SFred Isaman 	cinfo->lock = &inode->i_lock;
506ea2cf228SFred Isaman 	cinfo->mds = &NFS_I(inode)->commit_info;
507ea2cf228SFred Isaman 	cinfo->ds = pnfs_get_ds_info(inode);
508b359f9d0SFred Isaman 	cinfo->dreq = NULL;
509f453a54aSFred Isaman 	cinfo->completion_ops = &nfs_commit_completion_ops;
510ea2cf228SFred Isaman }
511ea2cf228SFred Isaman 
512ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
513ea2cf228SFred Isaman 		    struct inode *inode,
514ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq)
515ea2cf228SFred Isaman {
5161763da12SFred Isaman 	if (dreq)
5171763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
5181763da12SFred Isaman 	else
519ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(cinfo, inode);
520ea2cf228SFred Isaman }
521ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo);
5228dd37758STrond Myklebust 
5231da177e4SLinus Torvalds /*
5241da177e4SLinus Torvalds  * Add a request to the inode's commit list.
5251da177e4SLinus Torvalds  */
5261763da12SFred Isaman void
527ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
528ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
5291da177e4SLinus Torvalds {
530ea2cf228SFred Isaman 	if (pnfs_mark_request_commit(req, lseg, cinfo))
5318dd37758STrond Myklebust 		return;
532ea2cf228SFred Isaman 	nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo);
5331da177e4SLinus Torvalds }
5348e821cadSTrond Myklebust 
535d6d6dc7cSFred Isaman static void
536d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page)
537e468bae9STrond Myklebust {
538e468bae9STrond Myklebust 	dec_zone_page_state(page, NR_UNSTABLE_NFS);
539e468bae9STrond Myklebust 	dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
540e468bae9STrond Myklebust }
541d6d6dc7cSFred Isaman 
5428dd37758STrond Myklebust static void
543d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req)
544d6d6dc7cSFred Isaman {
5458dd37758STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
5468dd37758STrond Myklebust 		struct inode *inode = req->wb_context->dentry->d_inode;
547ea2cf228SFred Isaman 		struct nfs_commit_info cinfo;
548d6d6dc7cSFred Isaman 
549ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(&cinfo, inode);
550ea2cf228SFred Isaman 		if (!pnfs_clear_request_commit(req, &cinfo)) {
551ea2cf228SFred Isaman 			spin_lock(cinfo.lock);
552ea2cf228SFred Isaman 			nfs_request_remove_commit_list(req, &cinfo);
553ea2cf228SFred Isaman 			spin_unlock(cinfo.lock);
554d6d6dc7cSFred Isaman 		}
5558dd37758STrond Myklebust 		nfs_clear_page_commit(req->wb_page);
5568dd37758STrond Myklebust 	}
557e468bae9STrond Myklebust }
558e468bae9STrond Myklebust 
5598e821cadSTrond Myklebust static inline
5608e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
5618e821cadSTrond Myklebust {
562465d5243SFred Isaman 	if (data->verf.committed == NFS_DATA_SYNC)
563cd841605SFred Isaman 		return data->header->lseg == NULL;
5648e821cadSTrond Myklebust 	return data->verf.committed != NFS_FILE_SYNC;
5658e821cadSTrond Myklebust }
5668e821cadSTrond Myklebust 
5678e821cadSTrond Myklebust #else
56868cd6fa4SBryan Schumaker static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
56968cd6fa4SBryan Schumaker 				      struct inode *inode)
57068cd6fa4SBryan Schumaker {
57168cd6fa4SBryan Schumaker }
57268cd6fa4SBryan Schumaker 
57368cd6fa4SBryan Schumaker void nfs_init_cinfo(struct nfs_commit_info *cinfo,
57468cd6fa4SBryan Schumaker 		    struct inode *inode,
57568cd6fa4SBryan Schumaker 		    struct nfs_direct_req *dreq)
57668cd6fa4SBryan Schumaker {
57768cd6fa4SBryan Schumaker }
57868cd6fa4SBryan Schumaker 
5791763da12SFred Isaman void
580ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
581ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
5828e821cadSTrond Myklebust {
5838e821cadSTrond Myklebust }
5848e821cadSTrond Myklebust 
5858dd37758STrond Myklebust static void
586e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
587e468bae9STrond Myklebust {
588e468bae9STrond Myklebust }
589e468bae9STrond Myklebust 
5908e821cadSTrond Myklebust static inline
5918e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
5928e821cadSTrond Myklebust {
5938e821cadSTrond Myklebust 	return 0;
5948e821cadSTrond Myklebust }
5958e821cadSTrond Myklebust 
5961da177e4SLinus Torvalds #endif
5971da177e4SLinus Torvalds 
598061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
5996c75dc0dSFred Isaman {
600ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
6016c75dc0dSFred Isaman 	unsigned long bytes = 0;
6026c75dc0dSFred Isaman 
6036c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
6046c75dc0dSFred Isaman 		goto out;
605ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
6066c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
6076c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
6086c75dc0dSFred Isaman 
6096c75dc0dSFred Isaman 		bytes += req->wb_bytes;
6106c75dc0dSFred Isaman 		nfs_list_remove_request(req);
6116c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
6126c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
613d1182b33STrond Myklebust 			nfs_set_pageerror(req->wb_page);
6146c75dc0dSFred Isaman 			nfs_context_set_write_error(req->wb_context, hdr->error);
6156c75dc0dSFred Isaman 			goto remove_req;
6166c75dc0dSFred Isaman 		}
6176c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
6186c75dc0dSFred Isaman 			nfs_mark_request_dirty(req);
6196c75dc0dSFred Isaman 			goto next;
6206c75dc0dSFred Isaman 		}
6216c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
622ea2cf228SFred Isaman 			nfs_mark_request_commit(req, hdr->lseg, &cinfo);
6236c75dc0dSFred Isaman 			goto next;
6246c75dc0dSFred Isaman 		}
6256c75dc0dSFred Isaman remove_req:
6266c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
6276c75dc0dSFred Isaman next:
6283aff4ebbSTrond Myklebust 		nfs_unlock_request_dont_release(req);
629d1182b33STrond Myklebust 		nfs_end_page_writeback(req->wb_page);
6303aff4ebbSTrond Myklebust 		nfs_release_request(req);
6316c75dc0dSFred Isaman 	}
6326c75dc0dSFred Isaman out:
6336c75dc0dSFred Isaman 	hdr->release(hdr);
6346c75dc0dSFred Isaman }
6356c75dc0dSFred Isaman 
63647c62564STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
637ea2cf228SFred Isaman static unsigned long
638ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
639fb8a1f11STrond Myklebust {
640ea2cf228SFred Isaman 	return cinfo->mds->ncommit;
641fb8a1f11STrond Myklebust }
642fb8a1f11STrond Myklebust 
643ea2cf228SFred Isaman /* cinfo->lock held by caller */
6441763da12SFred Isaman int
645ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
646ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
647d6d6dc7cSFred Isaman {
648d6d6dc7cSFred Isaman 	struct nfs_page *req, *tmp;
649d6d6dc7cSFred Isaman 	int ret = 0;
650d6d6dc7cSFred Isaman 
651d6d6dc7cSFred Isaman 	list_for_each_entry_safe(req, tmp, src, wb_list) {
6528dd37758STrond Myklebust 		if (!nfs_lock_request(req))
6538dd37758STrond Myklebust 			continue;
654*7ad84aa9STrond Myklebust 		kref_get(&req->wb_kref);
655ea2cf228SFred Isaman 		if (cond_resched_lock(cinfo->lock))
6563b3be88dSTrond Myklebust 			list_safe_reset_next(req, tmp, wb_list);
657ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
6588dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
659d6d6dc7cSFred Isaman 		ret++;
6601763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
661d6d6dc7cSFred Isaman 			break;
662d6d6dc7cSFred Isaman 	}
663d6d6dc7cSFred Isaman 	return ret;
664d6d6dc7cSFred Isaman }
665d6d6dc7cSFred Isaman 
6661da177e4SLinus Torvalds /*
6671da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
6681da177e4SLinus Torvalds  * @inode: NFS inode to scan
669ea2cf228SFred Isaman  * @dst: mds destination list
670ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
6711da177e4SLinus Torvalds  *
6721da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
6731da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
6741da177e4SLinus Torvalds  */
6751763da12SFred Isaman int
676ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
677ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
6781da177e4SLinus Torvalds {
679d6d6dc7cSFred Isaman 	int ret = 0;
680fb8a1f11STrond Myklebust 
681ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
682ea2cf228SFred Isaman 	if (cinfo->mds->ncommit > 0) {
6838dd37758STrond Myklebust 		const int max = INT_MAX;
684d6d6dc7cSFred Isaman 
685ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
686ea2cf228SFred Isaman 					   cinfo, max);
687ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
688d6d6dc7cSFred Isaman 	}
689ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
690ff778d02STrond Myklebust 	return ret;
6911da177e4SLinus Torvalds }
692d6d6dc7cSFred Isaman 
693c42de9ddSTrond Myklebust #else
694ea2cf228SFred Isaman static unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
695fb8a1f11STrond Myklebust {
696fb8a1f11STrond Myklebust 	return 0;
697fb8a1f11STrond Myklebust }
698fb8a1f11STrond Myklebust 
6991763da12SFred Isaman int nfs_scan_commit(struct inode *inode, struct list_head *dst,
700ea2cf228SFred Isaman 		    struct nfs_commit_info *cinfo)
701c42de9ddSTrond Myklebust {
702c42de9ddSTrond Myklebust 	return 0;
703c42de9ddSTrond Myklebust }
7041da177e4SLinus Torvalds #endif
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds /*
707e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
708e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
7091da177e4SLinus Torvalds  *
710e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
711e7d39069STrond Myklebust  * to disk.
7121da177e4SLinus Torvalds  */
713e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
714e7d39069STrond Myklebust 		struct page *page,
715e7d39069STrond Myklebust 		unsigned int offset,
716e7d39069STrond Myklebust 		unsigned int bytes)
7171da177e4SLinus Torvalds {
718e7d39069STrond Myklebust 	struct nfs_page *req;
719e7d39069STrond Myklebust 	unsigned int rqend;
720e7d39069STrond Myklebust 	unsigned int end;
7211da177e4SLinus Torvalds 	int error;
722277459d2STrond Myklebust 
723e7d39069STrond Myklebust 	if (!PagePrivate(page))
724e7d39069STrond Myklebust 		return NULL;
725e7d39069STrond Myklebust 
726e7d39069STrond Myklebust 	end = offset + bytes;
727e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
728e7d39069STrond Myklebust 
729e7d39069STrond Myklebust 	for (;;) {
730e7d39069STrond Myklebust 		req = nfs_page_find_request_locked(page);
731e7d39069STrond Myklebust 		if (req == NULL)
732e7d39069STrond Myklebust 			goto out_unlock;
733e7d39069STrond Myklebust 
734e7d39069STrond Myklebust 		rqend = req->wb_offset + req->wb_bytes;
735e7d39069STrond Myklebust 		/*
736e7d39069STrond Myklebust 		 * Tell the caller to flush out the request if
737e7d39069STrond Myklebust 		 * the offsets are non-contiguous.
738e7d39069STrond Myklebust 		 * Note: nfs_flush_incompatible() will already
739e7d39069STrond Myklebust 		 * have flushed out requests having wrong owners.
740e7d39069STrond Myklebust 		 */
741e468bae9STrond Myklebust 		if (offset > rqend
742e7d39069STrond Myklebust 		    || end < req->wb_offset)
743e7d39069STrond Myklebust 			goto out_flushme;
744e7d39069STrond Myklebust 
745*7ad84aa9STrond Myklebust 		if (nfs_lock_request(req))
746e7d39069STrond Myklebust 			break;
747e7d39069STrond Myklebust 
748e7d39069STrond Myklebust 		/* The request is locked, so wait and then retry */
749587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
7501da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
7511da177e4SLinus Torvalds 		nfs_release_request(req);
752e7d39069STrond Myklebust 		if (error != 0)
753e7d39069STrond Myklebust 			goto out_err;
754e7d39069STrond Myklebust 		spin_lock(&inode->i_lock);
7551da177e4SLinus Torvalds 	}
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
7581da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
7591da177e4SLinus Torvalds 		req->wb_offset = offset;
7601da177e4SLinus Torvalds 		req->wb_pgbase = offset;
7611da177e4SLinus Torvalds 	}
7621da177e4SLinus Torvalds 	if (end > rqend)
7631da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
764e7d39069STrond Myklebust 	else
765e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
766e7d39069STrond Myklebust out_unlock:
767e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
768ca138f36SFred Isaman 	if (req)
7698dd37758STrond Myklebust 		nfs_clear_request_commit(req);
770e7d39069STrond Myklebust 	return req;
771e7d39069STrond Myklebust out_flushme:
772e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
773e7d39069STrond Myklebust 	nfs_release_request(req);
774e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
775e7d39069STrond Myklebust out_err:
776e7d39069STrond Myklebust 	return ERR_PTR(error);
777e7d39069STrond Myklebust }
7781da177e4SLinus Torvalds 
779e7d39069STrond Myklebust /*
780e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
781e7d39069STrond Myklebust  *
782e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
783e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
784e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
785e7d39069STrond Myklebust  */
786e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
787e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
788e7d39069STrond Myklebust {
789e7d39069STrond Myklebust 	struct inode *inode = page->mapping->host;
790e7d39069STrond Myklebust 	struct nfs_page	*req;
791e7d39069STrond Myklebust 
792e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
793e7d39069STrond Myklebust 	if (req != NULL)
794e7d39069STrond Myklebust 		goto out;
795e7d39069STrond Myklebust 	req = nfs_create_request(ctx, inode, page, offset, bytes);
796e7d39069STrond Myklebust 	if (IS_ERR(req))
797e7d39069STrond Myklebust 		goto out;
798d6d6dc7cSFred Isaman 	nfs_inode_add_request(inode, req);
799efc91ed0STrond Myklebust out:
80061e930a9STrond Myklebust 	return req;
8011da177e4SLinus Torvalds }
8021da177e4SLinus Torvalds 
803e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
804e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
805e7d39069STrond Myklebust {
806e7d39069STrond Myklebust 	struct nfs_page	*req;
807e7d39069STrond Myklebust 
808e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
809e7d39069STrond Myklebust 	if (IS_ERR(req))
810e7d39069STrond Myklebust 		return PTR_ERR(req);
811e7d39069STrond Myklebust 	/* Update file length */
812e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
813e7d39069STrond Myklebust 	nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
814a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
8159994b62bSFred Isaman 	nfs_unlock_request(req);
816e7d39069STrond Myklebust 	return 0;
817e7d39069STrond Myklebust }
818e7d39069STrond Myklebust 
8191da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
8201da177e4SLinus Torvalds {
821cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
8221da177e4SLinus Torvalds 	struct nfs_page	*req;
8231a54533eSTrond Myklebust 	int do_flush, status;
8241da177e4SLinus Torvalds 	/*
8251da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
8261da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
8271da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
8281da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
8291da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
8301da177e4SLinus Torvalds 	 * dropped page.
8311da177e4SLinus Torvalds 	 */
8321a54533eSTrond Myklebust 	do {
833277459d2STrond Myklebust 		req = nfs_page_find_request(page);
8341a54533eSTrond Myklebust 		if (req == NULL)
8351a54533eSTrond Myklebust 			return 0;
836f11ac8dbSTrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx ||
837f11ac8dbSTrond Myklebust 			req->wb_lock_context->lockowner != current->files ||
838f11ac8dbSTrond Myklebust 			req->wb_lock_context->pid != current->tgid;
8391da177e4SLinus Torvalds 		nfs_release_request(req);
8401a54533eSTrond Myklebust 		if (!do_flush)
8411a54533eSTrond Myklebust 			return 0;
842277459d2STrond Myklebust 		status = nfs_wb_page(page->mapping->host, page);
8431a54533eSTrond Myklebust 	} while (status == 0);
8441a54533eSTrond Myklebust 	return status;
8451da177e4SLinus Torvalds }
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds /*
8485d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
8495d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
8505d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
8515d47a356STrond Myklebust  */
8528d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
8535d47a356STrond Myklebust {
8548d197a56STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
8558d197a56STrond Myklebust 		goto out;
8568d197a56STrond Myklebust 	if (NFS_I(inode)->cache_validity & NFS_INO_REVAL_PAGECACHE)
8578d197a56STrond Myklebust 		return false;
8588d197a56STrond Myklebust out:
8598d197a56STrond Myklebust 	return PageUptodate(page) != 0;
8605d47a356STrond Myklebust }
8615d47a356STrond Myklebust 
8625d47a356STrond Myklebust /*
8631da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
8641da177e4SLinus Torvalds  *
8651da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
8661da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
8671da177e4SLinus Torvalds  */
8681da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
8691da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
8701da177e4SLinus Torvalds {
871cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
8721da177e4SLinus Torvalds 	struct inode	*inode = page->mapping->host;
8731da177e4SLinus Torvalds 	int		status = 0;
8741da177e4SLinus Torvalds 
87591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
87691d5b470SChuck Lever 
87748186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage(%s/%s %d@%lld)\n",
87801cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
87901cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name, count,
8800bbacc40SChuck Lever 		(long long)(page_offset(page) + offset));
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	/* If we're not using byte range locks, and we know the page
8835d47a356STrond Myklebust 	 * is up to date, it may be more efficient to extend the write
8845d47a356STrond Myklebust 	 * to cover the entire page in order to avoid fragmentation
8855d47a356STrond Myklebust 	 * inefficiencies.
8861da177e4SLinus Torvalds 	 */
8875d47a356STrond Myklebust 	if (nfs_write_pageuptodate(page, inode) &&
8885d47a356STrond Myklebust 			inode->i_flock == NULL &&
8896b2f3d1fSChristoph Hellwig 			!(file->f_flags & O_DSYNC)) {
89049a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
8911da177e4SLinus Torvalds 		offset = 0;
8921da177e4SLinus Torvalds 	}
8931da177e4SLinus Torvalds 
894e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
89503fa9e84STrond Myklebust 	if (status < 0)
89603fa9e84STrond Myklebust 		nfs_set_pageerror(page);
89759b7c05fSTrond Myklebust 	else
89859b7c05fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
8991da177e4SLinus Torvalds 
90048186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
9011da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
9021da177e4SLinus Torvalds 	return status;
9031da177e4SLinus Torvalds }
9041da177e4SLinus Torvalds 
9053ff7576dSTrond Myklebust static int flush_task_priority(int how)
9061da177e4SLinus Torvalds {
9071da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
9081da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
9091da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
9101da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
9111da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
9121da177e4SLinus Torvalds 	}
9131da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
9141da177e4SLinus Torvalds }
9151da177e4SLinus Torvalds 
916c5996c4eSFred Isaman int nfs_initiate_write(struct rpc_clnt *clnt,
917c5996c4eSFred Isaman 		       struct nfs_write_data *data,
918788e7a89STrond Myklebust 		       const struct rpc_call_ops *call_ops,
9191da177e4SLinus Torvalds 		       int how)
9201da177e4SLinus Torvalds {
921cd841605SFred Isaman 	struct inode *inode = data->header->inode;
9223ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
92307737691STrond Myklebust 	struct rpc_task *task;
924bdc7f021STrond Myklebust 	struct rpc_message msg = {
925bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
926bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
927cd841605SFred Isaman 		.rpc_cred = data->header->cred,
928bdc7f021STrond Myklebust 	};
92984115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
930d138d5d1SAndy Adamson 		.rpc_client = clnt,
93107737691STrond Myklebust 		.task = &data->task,
932bdc7f021STrond Myklebust 		.rpc_message = &msg,
93384115e1cSTrond Myklebust 		.callback_ops = call_ops,
93484115e1cSTrond Myklebust 		.callback_data = data,
935101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
9362c61be0aSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
9373ff7576dSTrond Myklebust 		.priority = priority,
93884115e1cSTrond Myklebust 	};
9392c61be0aSTrond Myklebust 	int ret = 0;
9401da177e4SLinus Torvalds 
941d138d5d1SAndy Adamson 	/* Set up the initial task struct.  */
942d138d5d1SAndy Adamson 	NFS_PROTO(inode)->write_setup(data, &msg);
943d138d5d1SAndy Adamson 
944d138d5d1SAndy Adamson 	dprintk("NFS: %5u initiated write call "
945d138d5d1SAndy Adamson 		"(req %s/%lld, %u bytes @ offset %llu)\n",
946d138d5d1SAndy Adamson 		data->task.tk_pid,
947d138d5d1SAndy Adamson 		inode->i_sb->s_id,
948d138d5d1SAndy Adamson 		(long long)NFS_FILEID(inode),
949d138d5d1SAndy Adamson 		data->args.count,
950d138d5d1SAndy Adamson 		(unsigned long long)data->args.offset);
951d138d5d1SAndy Adamson 
952d138d5d1SAndy Adamson 	task = rpc_run_task(&task_setup_data);
953d138d5d1SAndy Adamson 	if (IS_ERR(task)) {
954d138d5d1SAndy Adamson 		ret = PTR_ERR(task);
955d138d5d1SAndy Adamson 		goto out;
956d138d5d1SAndy Adamson 	}
957d138d5d1SAndy Adamson 	if (how & FLUSH_SYNC) {
958d138d5d1SAndy Adamson 		ret = rpc_wait_for_completion_task(task);
959d138d5d1SAndy Adamson 		if (ret == 0)
960d138d5d1SAndy Adamson 			ret = task->tk_status;
961d138d5d1SAndy Adamson 	}
962d138d5d1SAndy Adamson 	rpc_put_task(task);
963d138d5d1SAndy Adamson out:
964d138d5d1SAndy Adamson 	return ret;
965d138d5d1SAndy Adamson }
966a69aef14SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_write);
967d138d5d1SAndy Adamson 
968d138d5d1SAndy Adamson /*
969d138d5d1SAndy Adamson  * Set up the argument/result storage required for the RPC call.
970d138d5d1SAndy Adamson  */
9716c75dc0dSFred Isaman static void nfs_write_rpcsetup(struct nfs_write_data *data,
972d138d5d1SAndy Adamson 		unsigned int count, unsigned int offset,
973ea2cf228SFred Isaman 		int how, struct nfs_commit_info *cinfo)
974d138d5d1SAndy Adamson {
9756c75dc0dSFred Isaman 	struct nfs_page *req = data->header->req;
976d138d5d1SAndy Adamson 
9771da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
9781da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
9791da177e4SLinus Torvalds 
9806c75dc0dSFred Isaman 	data->args.fh     = NFS_FH(data->header->inode);
9811da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
9822bea038cSBoaz Harrosh 	/* pnfs_set_layoutcommit needs this */
9832bea038cSBoaz Harrosh 	data->mds_offset = data->args.offset;
9841da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
98530dd374fSFred Isaman 	data->args.pages  = data->pages.pagevec;
9861da177e4SLinus Torvalds 	data->args.count  = count;
987383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
988f11ac8dbSTrond Myklebust 	data->args.lock_context = req->wb_lock_context;
989bdc7f021STrond Myklebust 	data->args.stable  = NFS_UNSTABLE;
99087ed5eb4STrond Myklebust 	switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
99187ed5eb4STrond Myklebust 	case 0:
99287ed5eb4STrond Myklebust 		break;
99387ed5eb4STrond Myklebust 	case FLUSH_COND_STABLE:
994ea2cf228SFred Isaman 		if (nfs_reqs_to_commit(cinfo))
99587ed5eb4STrond Myklebust 			break;
99687ed5eb4STrond Myklebust 	default:
997bdc7f021STrond Myklebust 		data->args.stable = NFS_FILE_SYNC;
998bdc7f021STrond Myklebust 	}
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
10011da177e4SLinus Torvalds 	data->res.count   = count;
10021da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
10030e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
10046e4efd56STrond Myklebust }
10051da177e4SLinus Torvalds 
10066e4efd56STrond Myklebust static int nfs_do_write(struct nfs_write_data *data,
10076e4efd56STrond Myklebust 		const struct rpc_call_ops *call_ops,
10086e4efd56STrond Myklebust 		int how)
10096e4efd56STrond Myklebust {
1010cd841605SFred Isaman 	struct inode *inode = data->header->inode;
10110382b744SAndy Adamson 
1012c5996c4eSFred Isaman 	return nfs_initiate_write(NFS_CLIENT(inode), data, call_ops, how);
10131da177e4SLinus Torvalds }
10141da177e4SLinus Torvalds 
1015275acaafSTrond Myklebust static int nfs_do_multiple_writes(struct list_head *head,
1016275acaafSTrond Myklebust 		const struct rpc_call_ops *call_ops,
1017275acaafSTrond Myklebust 		int how)
1018275acaafSTrond Myklebust {
1019275acaafSTrond Myklebust 	struct nfs_write_data *data;
1020275acaafSTrond Myklebust 	int ret = 0;
1021275acaafSTrond Myklebust 
1022275acaafSTrond Myklebust 	while (!list_empty(head)) {
1023275acaafSTrond Myklebust 		int ret2;
1024275acaafSTrond Myklebust 
10256c75dc0dSFred Isaman 		data = list_first_entry(head, struct nfs_write_data, list);
1026275acaafSTrond Myklebust 		list_del_init(&data->list);
1027275acaafSTrond Myklebust 
1028dce81290STrond Myklebust 		ret2 = nfs_do_write(data, call_ops, how);
1029275acaafSTrond Myklebust 		 if (ret == 0)
1030275acaafSTrond Myklebust 			 ret = ret2;
1031275acaafSTrond Myklebust 	}
1032275acaafSTrond Myklebust 	return ret;
1033275acaafSTrond Myklebust }
1034275acaafSTrond Myklebust 
10356d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
10366d884e8fSFred  * call this on each, which will prepare them to be retried on next
10376d884e8fSFred  * writeback using standard nfs.
10386d884e8fSFred  */
10396d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
10406d884e8fSFred {
10416d884e8fSFred 	nfs_mark_request_dirty(req);
10423aff4ebbSTrond Myklebust 	nfs_unlock_request_dont_release(req);
1043d1182b33STrond Myklebust 	nfs_end_page_writeback(req->wb_page);
10443aff4ebbSTrond Myklebust 	nfs_release_request(req);
10456d884e8fSFred }
10466d884e8fSFred 
1047061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head)
10486c75dc0dSFred Isaman {
10496c75dc0dSFred Isaman 	struct nfs_page	*req;
10506c75dc0dSFred Isaman 
10516c75dc0dSFred Isaman 	while (!list_empty(head)) {
10526c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
10536c75dc0dSFred Isaman 		nfs_list_remove_request(req);
10546c75dc0dSFred Isaman 		nfs_redirty_request(req);
10556c75dc0dSFred Isaman 	}
10566c75dc0dSFred Isaman }
10576c75dc0dSFred Isaman 
1058061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1059061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1060061ae2edSFred Isaman 	.completion = nfs_write_completion,
1061061ae2edSFred Isaman };
1062061ae2edSFred Isaman 
106325b11dcdSTrond Myklebust static void nfs_flush_error(struct nfs_pageio_descriptor *desc,
106425b11dcdSTrond Myklebust 		struct nfs_pgio_header *hdr)
106525b11dcdSTrond Myklebust {
106625b11dcdSTrond Myklebust 	set_bit(NFS_IOHDR_REDO, &hdr->flags);
106725b11dcdSTrond Myklebust 	while (!list_empty(&hdr->rpc_list)) {
106825b11dcdSTrond Myklebust 		struct nfs_write_data *data = list_first_entry(&hdr->rpc_list,
106925b11dcdSTrond Myklebust 				struct nfs_write_data, list);
107025b11dcdSTrond Myklebust 		list_del(&data->list);
107125b11dcdSTrond Myklebust 		nfs_writedata_release(data);
107225b11dcdSTrond Myklebust 	}
107325b11dcdSTrond Myklebust 	desc->pg_completion_ops->error_cleanup(&desc->pg_list);
107425b11dcdSTrond Myklebust }
107525b11dcdSTrond Myklebust 
10761da177e4SLinus Torvalds /*
10771da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
10781da177e4SLinus Torvalds  * contiguous dirty area on one page.
10791da177e4SLinus Torvalds  */
10806c75dc0dSFred Isaman static int nfs_flush_multi(struct nfs_pageio_descriptor *desc,
10816c75dc0dSFred Isaman 			   struct nfs_pgio_header *hdr)
10821da177e4SLinus Torvalds {
10836c75dc0dSFred Isaman 	struct nfs_page *req = hdr->req;
10841da177e4SLinus Torvalds 	struct page *page = req->wb_page;
10851da177e4SLinus Torvalds 	struct nfs_write_data *data;
1086d097971dSTrond Myklebust 	size_t wsize = desc->pg_bsize, nbytes;
1087e9f7bee1STrond Myklebust 	unsigned int offset;
10881da177e4SLinus Torvalds 	int requests = 0;
1089ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
10901da177e4SLinus Torvalds 
1091ea2cf228SFred Isaman 	nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
10921da177e4SLinus Torvalds 
1093b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1094ea2cf228SFred Isaman 	    (desc->pg_moreio || nfs_reqs_to_commit(&cinfo) ||
1095b31268acSTrond Myklebust 	     desc->pg_count > wsize))
1096b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1097b31268acSTrond Myklebust 
1098b31268acSTrond Myklebust 
1099275acaafSTrond Myklebust 	offset = 0;
1100c76069bdSFred Isaman 	nbytes = desc->pg_count;
1101e9f7bee1STrond Myklebust 	do {
1102e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
1103e9f7bee1STrond Myklebust 
11046c75dc0dSFred Isaman 		data = nfs_writedata_alloc(hdr, 1);
110525b11dcdSTrond Myklebust 		if (!data) {
110625b11dcdSTrond Myklebust 			nfs_flush_error(desc, hdr);
110725b11dcdSTrond Myklebust 			return -ENOMEM;
110825b11dcdSTrond Myklebust 		}
110930dd374fSFred Isaman 		data->pages.pagevec[0] = page;
1110ea2cf228SFred Isaman 		nfs_write_rpcsetup(data, len, offset, desc->pg_ioflags, &cinfo);
11116c75dc0dSFred Isaman 		list_add(&data->list, &hdr->rpc_list);
11121da177e4SLinus Torvalds 		requests++;
1113e9f7bee1STrond Myklebust 		nbytes -= len;
1114275acaafSTrond Myklebust 		offset += len;
1115e9f7bee1STrond Myklebust 	} while (nbytes != 0);
111625b11dcdSTrond Myklebust 	nfs_list_remove_request(req);
111725b11dcdSTrond Myklebust 	nfs_list_add_request(req, &hdr->pages);
11186c75dc0dSFred Isaman 	desc->pg_rpc_callops = &nfs_write_common_ops;
111925b11dcdSTrond Myklebust 	return 0;
11201da177e4SLinus Torvalds }
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds /*
11231da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
11241da177e4SLinus Torvalds  * The page must have been locked by the caller.
11251da177e4SLinus Torvalds  *
11261da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
11271da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
11281da177e4SLinus Torvalds  * that has been written but not committed.
11291da177e4SLinus Torvalds  */
11306c75dc0dSFred Isaman static int nfs_flush_one(struct nfs_pageio_descriptor *desc,
11316c75dc0dSFred Isaman 			 struct nfs_pgio_header *hdr)
11321da177e4SLinus Torvalds {
11331da177e4SLinus Torvalds 	struct nfs_page		*req;
11341da177e4SLinus Torvalds 	struct page		**pages;
11351da177e4SLinus Torvalds 	struct nfs_write_data	*data;
1136c76069bdSFred Isaman 	struct list_head *head = &desc->pg_list;
1137ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
11381da177e4SLinus Torvalds 
11396c75dc0dSFred Isaman 	data = nfs_writedata_alloc(hdr, nfs_page_array_len(desc->pg_base,
1140c76069bdSFred Isaman 							   desc->pg_count));
11416c75dc0dSFred Isaman 	if (!data) {
114225b11dcdSTrond Myklebust 		nfs_flush_error(desc, hdr);
114325b11dcdSTrond Myklebust 		return -ENOMEM;
114444b83799SFred Isaman 	}
11456c75dc0dSFred Isaman 
1146ea2cf228SFred Isaman 	nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
114730dd374fSFred Isaman 	pages = data->pages.pagevec;
11481da177e4SLinus Torvalds 	while (!list_empty(head)) {
11491da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
11501da177e4SLinus Torvalds 		nfs_list_remove_request(req);
11516c75dc0dSFred Isaman 		nfs_list_add_request(req, &hdr->pages);
11521da177e4SLinus Torvalds 		*pages++ = req->wb_page;
11531da177e4SLinus Torvalds 	}
11541da177e4SLinus Torvalds 
1155b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1156ea2cf228SFred Isaman 	    (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
1157b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1158b31268acSTrond Myklebust 
11591da177e4SLinus Torvalds 	/* Set up the argument struct */
1160ea2cf228SFred Isaman 	nfs_write_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo);
11616c75dc0dSFred Isaman 	list_add(&data->list, &hdr->rpc_list);
11626c75dc0dSFred Isaman 	desc->pg_rpc_callops = &nfs_write_common_ops;
116325b11dcdSTrond Myklebust 	return 0;
11641da177e4SLinus Torvalds }
11651da177e4SLinus Torvalds 
11666c75dc0dSFred Isaman int nfs_generic_flush(struct nfs_pageio_descriptor *desc,
11676c75dc0dSFred Isaman 		      struct nfs_pgio_header *hdr)
1168dce81290STrond Myklebust {
1169dce81290STrond Myklebust 	if (desc->pg_bsize < PAGE_CACHE_SIZE)
11706c75dc0dSFred Isaman 		return nfs_flush_multi(desc, hdr);
11716c75dc0dSFred Isaman 	return nfs_flush_one(desc, hdr);
1172dce81290STrond Myklebust }
1173dce81290STrond Myklebust 
1174dce81290STrond Myklebust static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
11751751c363STrond Myklebust {
11766c75dc0dSFred Isaman 	struct nfs_write_header *whdr;
11776c75dc0dSFred Isaman 	struct nfs_pgio_header *hdr;
1178275acaafSTrond Myklebust 	int ret;
1179275acaafSTrond Myklebust 
11806c75dc0dSFred Isaman 	whdr = nfs_writehdr_alloc();
11816c75dc0dSFred Isaman 	if (!whdr) {
11829b5415b5STrond Myklebust 		desc->pg_completion_ops->error_cleanup(&desc->pg_list);
11836c75dc0dSFred Isaman 		return -ENOMEM;
11846c75dc0dSFred Isaman 	}
11856c75dc0dSFred Isaman 	hdr = &whdr->header;
11866c75dc0dSFred Isaman 	nfs_pgheader_init(desc, hdr, nfs_writehdr_free);
11876c75dc0dSFred Isaman 	atomic_inc(&hdr->refcnt);
11886c75dc0dSFred Isaman 	ret = nfs_generic_flush(desc, hdr);
1189275acaafSTrond Myklebust 	if (ret == 0)
11906c75dc0dSFred Isaman 		ret = nfs_do_multiple_writes(&hdr->rpc_list,
11916c75dc0dSFred Isaman 					     desc->pg_rpc_callops,
1192dce81290STrond Myklebust 					     desc->pg_ioflags);
11936c75dc0dSFred Isaman 	if (atomic_dec_and_test(&hdr->refcnt))
1194061ae2edSFred Isaman 		hdr->completion_ops->completion(hdr);
1195275acaafSTrond Myklebust 	return ret;
11961751c363STrond Myklebust }
11971751c363STrond Myklebust 
11981751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_write_ops = {
11991751c363STrond Myklebust 	.pg_test = nfs_generic_pg_test,
12001751c363STrond Myklebust 	.pg_doio = nfs_generic_pg_writepages,
12011751c363STrond Myklebust };
12021751c363STrond Myklebust 
1203e2fecb21STrond Myklebust void nfs_pageio_init_write_mds(struct nfs_pageio_descriptor *pgio,
1204061ae2edSFred Isaman 			       struct inode *inode, int ioflags,
1205061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
12061751c363STrond Myklebust {
1207061ae2edSFred Isaman 	nfs_pageio_init(pgio, inode, &nfs_pageio_write_ops, compl_ops,
12081751c363STrond Myklebust 				NFS_SERVER(inode)->wsize, ioflags);
12091751c363STrond Myklebust }
12101751c363STrond Myklebust 
1211dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1212dce81290STrond Myklebust {
1213dce81290STrond Myklebust 	pgio->pg_ops = &nfs_pageio_write_ops;
1214dce81290STrond Myklebust 	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1215dce81290STrond Myklebust }
12161f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1217dce81290STrond Myklebust 
12181763da12SFred Isaman void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1219061ae2edSFred Isaman 			   struct inode *inode, int ioflags,
1220061ae2edSFred Isaman 			   const struct nfs_pgio_completion_ops *compl_ops)
12211da177e4SLinus Torvalds {
1222061ae2edSFred Isaman 	if (!pnfs_pageio_init_write(pgio, inode, ioflags, compl_ops))
1223061ae2edSFred Isaman 		nfs_pageio_init_write_mds(pgio, inode, ioflags, compl_ops);
12241da177e4SLinus Torvalds }
12251da177e4SLinus Torvalds 
1226def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata)
1227def6ed7eSAndy Adamson {
1228def6ed7eSAndy Adamson 	struct nfs_write_data *data = calldata;
1229cd841605SFred Isaman 	NFS_PROTO(data->header->inode)->write_rpc_prepare(task, data);
1230def6ed7eSAndy Adamson }
1231def6ed7eSAndy Adamson 
12320b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
12330b7c0153SFred Isaman {
12340b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
12350b7c0153SFred Isaman 
12360b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
12370b7c0153SFred Isaman }
12380b7c0153SFred Isaman 
12391da177e4SLinus Torvalds /*
12401da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
12411da177e4SLinus Torvalds  *
12421da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
12431da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
12441da177e4SLinus Torvalds  *	  as the page has a write request pending.
12451da177e4SLinus Torvalds  */
12466c75dc0dSFred Isaman static void nfs_writeback_done_common(struct rpc_task *task, void *calldata)
12471da177e4SLinus Torvalds {
1248788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
12491da177e4SLinus Torvalds 
1250c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
1251c9d8f89dSTrond Myklebust }
1252c9d8f89dSTrond Myklebust 
12536c75dc0dSFred Isaman static void nfs_writeback_release_common(void *calldata)
1254c9d8f89dSTrond Myklebust {
1255c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1256cd841605SFred Isaman 	struct nfs_pgio_header *hdr = data->header;
1257e2fecb21STrond Myklebust 	int status = data->task.tk_status;
12586c75dc0dSFred Isaman 	struct nfs_page *req = hdr->req;
1259788e7a89STrond Myklebust 
12606c75dc0dSFred Isaman 	if ((status >= 0) && nfs_write_need_commit(data)) {
12616c75dc0dSFred Isaman 		spin_lock(&hdr->lock);
12626c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags))
12636c75dc0dSFred Isaman 			; /* Do nothing */
12646c75dc0dSFred Isaman 		else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags))
12651da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
12666c75dc0dSFred Isaman 		else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf)))
12676c75dc0dSFred Isaman 			set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags);
12686c75dc0dSFred Isaman 		spin_unlock(&hdr->lock);
12691da177e4SLinus Torvalds 	}
1270cd841605SFred Isaman 	nfs_writedata_release(data);
12711da177e4SLinus Torvalds }
12721da177e4SLinus Torvalds 
12736c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops = {
1274def6ed7eSAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
12756c75dc0dSFred Isaman 	.rpc_call_done = nfs_writeback_done_common,
12766c75dc0dSFred Isaman 	.rpc_release = nfs_writeback_release_common,
1277788e7a89STrond Myklebust };
1278788e7a89STrond Myklebust 
1279788e7a89STrond Myklebust 
12801da177e4SLinus Torvalds /*
12811da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
12821da177e4SLinus Torvalds  */
128313602896SFred Isaman void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
12841da177e4SLinus Torvalds {
12851da177e4SLinus Torvalds 	struct nfs_writeargs	*argp = &data->args;
12861da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
1287cd841605SFred Isaman 	struct inode		*inode = data->header->inode;
1288788e7a89STrond Myklebust 	int status;
12891da177e4SLinus Torvalds 
1290a3f565b1SChuck Lever 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
12911da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
12921da177e4SLinus Torvalds 
1293f551e44fSChuck Lever 	/*
1294f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1295f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1296f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1297f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1298f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1299f551e44fSChuck Lever 	 */
1300cd841605SFred Isaman 	status = NFS_PROTO(inode)->write_done(task, data);
1301788e7a89STrond Myklebust 	if (status != 0)
130213602896SFred Isaman 		return;
1303cd841605SFred Isaman 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
130491d5b470SChuck Lever 
13051da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
13061da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
13071da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
13081da177e4SLinus Torvalds 		 * commit data to stable storage even though we
13091da177e4SLinus Torvalds 		 * requested it.
13101da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
13111da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
13121da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
13131da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
13141da177e4SLinus Torvalds 		 */
13151da177e4SLinus Torvalds 		static unsigned long    complain;
13161da177e4SLinus Torvalds 
1317a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
13181da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
13191da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
13201da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1321cd841605SFred Isaman 				NFS_SERVER(inode)->nfs_client->cl_hostname,
13221da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
13231da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
13241da177e4SLinus Torvalds 		}
13251da177e4SLinus Torvalds 	}
13261da177e4SLinus Torvalds #endif
13276c75dc0dSFred Isaman 	if (task->tk_status < 0)
13286c75dc0dSFred Isaman 		nfs_set_pgio_error(data->header, task->tk_status, argp->offset);
13296c75dc0dSFred Isaman 	else if (resp->count < argp->count) {
13301da177e4SLinus Torvalds 		static unsigned long    complain;
13311da177e4SLinus Torvalds 
13326c75dc0dSFred Isaman 		/* This a short write! */
1333cd841605SFred Isaman 		nfs_inc_stats(inode, NFSIOS_SHORTWRITE);
133491d5b470SChuck Lever 
13351da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
13366c75dc0dSFred Isaman 		if (resp->count == 0) {
13376c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
13386c75dc0dSFred Isaman 				printk(KERN_WARNING
13396c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
13406c75dc0dSFred Isaman 				       argp->count);
13416c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
13426c75dc0dSFred Isaman 			}
13436c75dc0dSFred Isaman 			nfs_set_pgio_error(data->header, -EIO, argp->offset);
13446c75dc0dSFred Isaman 			task->tk_status = -EIO;
13456c75dc0dSFred Isaman 			return;
13466c75dc0dSFred Isaman 		}
13471da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
13481da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
13491da177e4SLinus Torvalds 			/* Resend from where the server left off */
1350a69aef14SFred Isaman 			data->mds_offset += resp->count;
13511da177e4SLinus Torvalds 			argp->offset += resp->count;
13521da177e4SLinus Torvalds 			argp->pgbase += resp->count;
13531da177e4SLinus Torvalds 			argp->count -= resp->count;
13541da177e4SLinus Torvalds 		} else {
13551da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
13561da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
13571da177e4SLinus Torvalds 			 */
13581da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
13591da177e4SLinus Torvalds 		}
1360d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
13611da177e4SLinus Torvalds 	}
13621da177e4SLinus Torvalds }
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
136671d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
136771d0a611STrond Myklebust {
1368b8413f98STrond Myklebust 	int ret;
1369b8413f98STrond Myklebust 
137071d0a611STrond Myklebust 	if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
137171d0a611STrond Myklebust 		return 1;
1372b8413f98STrond Myklebust 	if (!may_wait)
137371d0a611STrond Myklebust 		return 0;
1374b8413f98STrond Myklebust 	ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1375b8413f98STrond Myklebust 				NFS_INO_COMMIT,
1376b8413f98STrond Myklebust 				nfs_wait_bit_killable,
1377b8413f98STrond Myklebust 				TASK_KILLABLE);
1378b8413f98STrond Myklebust 	return (ret < 0) ? ret : 1;
137971d0a611STrond Myklebust }
138071d0a611STrond Myklebust 
1381f453a54aSFred Isaman static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
138271d0a611STrond Myklebust {
138371d0a611STrond Myklebust 	clear_bit(NFS_INO_COMMIT, &nfsi->flags);
138471d0a611STrond Myklebust 	smp_mb__after_clear_bit();
138571d0a611STrond Myklebust 	wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
138671d0a611STrond Myklebust }
138771d0a611STrond Myklebust 
13880b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
13891da177e4SLinus Torvalds {
13900b7c0153SFred Isaman 	put_nfs_open_context(data->context);
13910b7c0153SFred Isaman 	nfs_commit_free(data);
13921da177e4SLinus Torvalds }
1393e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
13941da177e4SLinus Torvalds 
13950b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
13969ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
1397788e7a89STrond Myklebust 			int how)
13981da177e4SLinus Torvalds {
139907737691STrond Myklebust 	struct rpc_task *task;
14009ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1401bdc7f021STrond Myklebust 	struct rpc_message msg = {
1402bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1403bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
14049ace33cdSFred Isaman 		.rpc_cred = data->cred,
1405bdc7f021STrond Myklebust 	};
140684115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
140707737691STrond Myklebust 		.task = &data->task,
14089ace33cdSFred Isaman 		.rpc_client = clnt,
1409bdc7f021STrond Myklebust 		.rpc_message = &msg,
14109ace33cdSFred Isaman 		.callback_ops = call_ops,
141184115e1cSTrond Myklebust 		.callback_data = data,
1412101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
14132c61be0aSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
14143ff7576dSTrond Myklebust 		.priority = priority,
141584115e1cSTrond Myklebust 	};
1416788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
14179ace33cdSFred Isaman 	NFS_PROTO(data->inode)->commit_setup(data, &msg);
14181da177e4SLinus Torvalds 
1419a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1420bdc7f021STrond Myklebust 
142107737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1422dbae4c73STrond Myklebust 	if (IS_ERR(task))
1423dbae4c73STrond Myklebust 		return PTR_ERR(task);
1424d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1425d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
142607737691STrond Myklebust 	rpc_put_task(task);
1427dbae4c73STrond Myklebust 	return 0;
14281da177e4SLinus Torvalds }
1429e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
14301da177e4SLinus Torvalds 
14311da177e4SLinus Torvalds /*
14329ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
14339ace33cdSFred Isaman  */
14340b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1435988b6dceSFred Isaman 		     struct list_head *head,
1436f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1437f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
14389ace33cdSFred Isaman {
14399ace33cdSFred Isaman 	struct nfs_page *first = nfs_list_entry(head->next);
14403d4ff43dSAl Viro 	struct inode *inode = first->wb_context->dentry->d_inode;
14419ace33cdSFred Isaman 
14429ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
14439ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
14449ace33cdSFred Isaman 
14459ace33cdSFred Isaman 	list_splice_init(head, &data->pages);
14469ace33cdSFred Isaman 
14479ace33cdSFred Isaman 	data->inode	  = inode;
14489ace33cdSFred Isaman 	data->cred	  = first->wb_context->cred;
1449988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
14509ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1451f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1452b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
14539ace33cdSFred Isaman 
14549ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
14559ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
14569ace33cdSFred Isaman 	data->args.offset = 0;
14579ace33cdSFred Isaman 	data->args.count  = 0;
14580b7c0153SFred Isaman 	data->context     = get_nfs_open_context(first->wb_context);
14599ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
14609ace33cdSFred Isaman 	data->res.verf    = &data->verf;
14619ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
14629ace33cdSFred Isaman }
1463e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
14649ace33cdSFred Isaman 
1465e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1466ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1467ea2cf228SFred Isaman 		      struct nfs_commit_info *cinfo)
146864bfeb49SFred Isaman {
146964bfeb49SFred Isaman 	struct nfs_page *req;
147064bfeb49SFred Isaman 
147164bfeb49SFred Isaman 	while (!list_empty(page_list)) {
147264bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
147364bfeb49SFred Isaman 		nfs_list_remove_request(req);
1474ea2cf228SFred Isaman 		nfs_mark_request_commit(req, lseg, cinfo);
147556f9cd68SFred Isaman 		if (!cinfo->dreq) {
147664bfeb49SFred Isaman 			dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
147764bfeb49SFred Isaman 			dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
147864bfeb49SFred Isaman 				     BDI_RECLAIMABLE);
147956f9cd68SFred Isaman 		}
14809994b62bSFred Isaman 		nfs_unlock_request(req);
148164bfeb49SFred Isaman 	}
148264bfeb49SFred Isaman }
1483e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
148464bfeb49SFred Isaman 
14859ace33cdSFred Isaman /*
14861da177e4SLinus Torvalds  * Commit dirty pages
14871da177e4SLinus Torvalds  */
14881da177e4SLinus Torvalds static int
1489ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1490ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
14911da177e4SLinus Torvalds {
14920b7c0153SFred Isaman 	struct nfs_commit_data	*data;
14931da177e4SLinus Torvalds 
1494c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
14951da177e4SLinus Torvalds 
14961da177e4SLinus Torvalds 	if (!data)
14971da177e4SLinus Torvalds 		goto out_bad;
14981da177e4SLinus Torvalds 
14991da177e4SLinus Torvalds 	/* Set up the argument struct */
1500f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
1501f453a54aSFred Isaman 	atomic_inc(&cinfo->mds->rpcs_out);
15020b7c0153SFred Isaman 	return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops, how);
15031da177e4SLinus Torvalds  out_bad:
1504ea2cf228SFred Isaman 	nfs_retry_commit(head, NULL, cinfo);
1505f453a54aSFred Isaman 	cinfo->completion_ops->error_cleanup(NFS_I(inode));
15061da177e4SLinus Torvalds 	return -ENOMEM;
15071da177e4SLinus Torvalds }
15081da177e4SLinus Torvalds 
15091da177e4SLinus Torvalds /*
15101da177e4SLinus Torvalds  * COMMIT call returned
15111da177e4SLinus Torvalds  */
1512788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
15131da177e4SLinus Torvalds {
15140b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
15151da177e4SLinus Torvalds 
1516a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
15171da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
15181da177e4SLinus Torvalds 
1519788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1520c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
1521c9d8f89dSTrond Myklebust }
1522c9d8f89dSTrond Myklebust 
1523f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1524c9d8f89dSTrond Myklebust {
1525c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1526c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1527f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
1528788e7a89STrond Myklebust 
15291da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
15301da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
15311da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1532d6d6dc7cSFred Isaman 		nfs_clear_page_commit(req->wb_page);
15331da177e4SLinus Torvalds 
153448186c7dSChuck Lever 		dprintk("NFS:       commit (%s/%lld %d@%lld)",
15353d4ff43dSAl Viro 			req->wb_context->dentry->d_sb->s_id,
15363d4ff43dSAl Viro 			(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
15371da177e4SLinus Torvalds 			req->wb_bytes,
15381da177e4SLinus Torvalds 			(long long)req_offset(req));
1539c9d8f89dSTrond Myklebust 		if (status < 0) {
1540c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
15411da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1542c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
15431da177e4SLinus Torvalds 			goto next;
15441da177e4SLinus Torvalds 		}
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
15471da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
15481da177e4SLinus Torvalds 		if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
15491da177e4SLinus Torvalds 			/* We have a match */
15501da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
15511da177e4SLinus Torvalds 			dprintk(" OK\n");
15521da177e4SLinus Torvalds 			goto next;
15531da177e4SLinus Torvalds 		}
15541da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
15551da177e4SLinus Torvalds 		dprintk(" mismatch\n");
15566d884e8fSFred 		nfs_mark_request_dirty(req);
15571da177e4SLinus Torvalds 	next:
15589994b62bSFred Isaman 		nfs_unlock_request(req);
15591da177e4SLinus Torvalds 	}
1560f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1561f453a54aSFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
1562f453a54aSFred Isaman 		nfs_commit_clear_lock(NFS_I(data->inode));
15635917ce84SFred Isaman }
15645917ce84SFred Isaman 
15655917ce84SFred Isaman static void nfs_commit_release(void *calldata)
15665917ce84SFred Isaman {
15670b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
15685917ce84SFred Isaman 
1569f453a54aSFred Isaman 	data->completion_ops->completion(data);
1570c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
15711da177e4SLinus Torvalds }
1572788e7a89STrond Myklebust 
1573788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
15740b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1575788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1576788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1577788e7a89STrond Myklebust };
15781da177e4SLinus Torvalds 
1579f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1580f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1581f453a54aSFred Isaman 	.error_cleanup = nfs_commit_clear_lock,
1582f453a54aSFred Isaman };
1583f453a54aSFred Isaman 
15841763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1585ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
158684c53ab5SFred Isaman {
158784c53ab5SFred Isaman 	int status;
158884c53ab5SFred Isaman 
1589ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
159084c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1591ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
159284c53ab5SFred Isaman 	return status;
159384c53ab5SFred Isaman }
159484c53ab5SFred Isaman 
1595b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
15961da177e4SLinus Torvalds {
15971da177e4SLinus Torvalds 	LIST_HEAD(head);
1598ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
159971d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1600b8413f98STrond Myklebust 	int res;
16011da177e4SLinus Torvalds 
1602b8413f98STrond Myklebust 	res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1603b8413f98STrond Myklebust 	if (res <= 0)
1604c5efa5fcSTrond Myklebust 		goto out_mark_dirty;
1605ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1606ea2cf228SFred Isaman 	res = nfs_scan_commit(inode, &head, &cinfo);
16071da177e4SLinus Torvalds 	if (res) {
1608a861a1e1SFred Isaman 		int error;
1609a861a1e1SFred Isaman 
1610ea2cf228SFred Isaman 		error = nfs_generic_commit_list(inode, &head, how, &cinfo);
16111da177e4SLinus Torvalds 		if (error < 0)
16121da177e4SLinus Torvalds 			return error;
1613b8413f98STrond Myklebust 		if (!may_wait)
1614b8413f98STrond Myklebust 			goto out_mark_dirty;
1615b8413f98STrond Myklebust 		error = wait_on_bit(&NFS_I(inode)->flags,
1616b8413f98STrond Myklebust 				NFS_INO_COMMIT,
161771d0a611STrond Myklebust 				nfs_wait_bit_killable,
161871d0a611STrond Myklebust 				TASK_KILLABLE);
1619b8413f98STrond Myklebust 		if (error < 0)
1620b8413f98STrond Myklebust 			return error;
162171d0a611STrond Myklebust 	} else
162271d0a611STrond Myklebust 		nfs_commit_clear_lock(NFS_I(inode));
1623c5efa5fcSTrond Myklebust 	return res;
1624c5efa5fcSTrond Myklebust 	/* Note: If we exit without ensuring that the commit is complete,
1625c5efa5fcSTrond Myklebust 	 * we must mark the inode as dirty. Otherwise, future calls to
1626c5efa5fcSTrond Myklebust 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1627c5efa5fcSTrond Myklebust 	 * that the data is on the disk.
1628c5efa5fcSTrond Myklebust 	 */
1629c5efa5fcSTrond Myklebust out_mark_dirty:
1630c5efa5fcSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
16311da177e4SLinus Torvalds 	return res;
16321da177e4SLinus Torvalds }
16338fc795f7STrond Myklebust 
16348fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
16358fc795f7STrond Myklebust {
1636420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1637420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1638420e3646STrond Myklebust 	int ret = 0;
16398fc795f7STrond Myklebust 
16403236c3e1SJeff Layton 	/* no commits means nothing needs to be done */
1641ea2cf228SFred Isaman 	if (!nfsi->commit_info.ncommit)
16423236c3e1SJeff Layton 		return ret;
16433236c3e1SJeff Layton 
1644a00dd6c0SJeff Layton 	if (wbc->sync_mode == WB_SYNC_NONE) {
1645a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1646a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1647420e3646STrond Myklebust 		 */
1648ea2cf228SFred Isaman 		if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1))
1649420e3646STrond Myklebust 			goto out_mark_dirty;
1650420e3646STrond Myklebust 
1651a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1652420e3646STrond Myklebust 		flags = 0;
1653a00dd6c0SJeff Layton 	}
1654a00dd6c0SJeff Layton 
1655420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1656420e3646STrond Myklebust 	if (ret >= 0) {
1657420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1658420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1659420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1660420e3646STrond Myklebust 			else
1661420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1662420e3646STrond Myklebust 		}
16638fc795f7STrond Myklebust 		return 0;
1664420e3646STrond Myklebust 	}
1665420e3646STrond Myklebust out_mark_dirty:
16668fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
16678fc795f7STrond Myklebust 	return ret;
16688fc795f7STrond Myklebust }
1669c63c7b05STrond Myklebust #else
16708fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
16718fc795f7STrond Myklebust {
16728fc795f7STrond Myklebust 	return 0;
16738fc795f7STrond Myklebust }
16741da177e4SLinus Torvalds #endif
16751da177e4SLinus Torvalds 
16768fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
16778fc795f7STrond Myklebust {
1678863a3c6cSAndy Adamson 	int ret;
1679863a3c6cSAndy Adamson 
1680863a3c6cSAndy Adamson 	ret = nfs_commit_unstable_pages(inode, wbc);
1681863a3c6cSAndy Adamson 	if (ret >= 0 && test_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(inode)->flags)) {
1682ef311537SAndy Adamson 		int status;
1683ef311537SAndy Adamson 		bool sync = true;
1684863a3c6cSAndy Adamson 
1685846d5a09SWu Fengguang 		if (wbc->sync_mode == WB_SYNC_NONE)
1686ef311537SAndy Adamson 			sync = false;
1687863a3c6cSAndy Adamson 
1688863a3c6cSAndy Adamson 		status = pnfs_layoutcommit_inode(inode, sync);
1689863a3c6cSAndy Adamson 		if (status < 0)
1690863a3c6cSAndy Adamson 			return status;
1691863a3c6cSAndy Adamson 	}
1692863a3c6cSAndy Adamson 	return ret;
16938fc795f7STrond Myklebust }
16948fc795f7STrond Myklebust 
1695acdc53b2STrond Myklebust /*
1696acdc53b2STrond Myklebust  * flush the inode to disk.
1697acdc53b2STrond Myklebust  */
1698acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
169934901f70STrond Myklebust {
170034901f70STrond Myklebust 	struct writeback_control wbc = {
170172cb77f4STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
170234901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
1703d7fb1207STrond Myklebust 		.range_start = 0,
1704d7fb1207STrond Myklebust 		.range_end = LLONG_MAX,
170534901f70STrond Myklebust 	};
170634901f70STrond Myklebust 
1707acdc53b2STrond Myklebust 	return sync_inode(inode, &wbc);
17081c75950bSTrond Myklebust }
17091c75950bSTrond Myklebust 
17101b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
17111b3b4a1aSTrond Myklebust {
17121b3b4a1aSTrond Myklebust 	struct nfs_page *req;
17131b3b4a1aSTrond Myklebust 	int ret = 0;
17141b3b4a1aSTrond Myklebust 
17151b3b4a1aSTrond Myklebust 	BUG_ON(!PageLocked(page));
17161b3b4a1aSTrond Myklebust 	for (;;) {
1717ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
17181b3b4a1aSTrond Myklebust 		req = nfs_page_find_request(page);
17191b3b4a1aSTrond Myklebust 		if (req == NULL)
17201b3b4a1aSTrond Myklebust 			break;
1721*7ad84aa9STrond Myklebust 		if (nfs_lock_request(req)) {
17228dd37758STrond Myklebust 			nfs_clear_request_commit(req);
17231b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
17241b3b4a1aSTrond Myklebust 			/*
17251b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
17261b3b4a1aSTrond Myklebust 			 * page as being dirty
17271b3b4a1aSTrond Myklebust 			 */
17281b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
17291b3b4a1aSTrond Myklebust 			nfs_unlock_request(req);
17301b3b4a1aSTrond Myklebust 			break;
17311b3b4a1aSTrond Myklebust 		}
17321b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
1733c9edda71STrond Myklebust 		nfs_release_request(req);
17341b3b4a1aSTrond Myklebust 		if (ret < 0)
1735c988950eSTrond Myklebust 			break;
17361b3b4a1aSTrond Myklebust 	}
17371b3b4a1aSTrond Myklebust 	return ret;
17381b3b4a1aSTrond Myklebust }
17391b3b4a1aSTrond Myklebust 
17401c75950bSTrond Myklebust /*
17411c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
17421c75950bSTrond Myklebust  */
17431c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
17441c75950bSTrond Myklebust {
17457f2f12d9STrond Myklebust 	loff_t range_start = page_offset(page);
17467f2f12d9STrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
17477f2f12d9STrond Myklebust 	struct writeback_control wbc = {
17487f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
17497f2f12d9STrond Myklebust 		.nr_to_write = 0,
17507f2f12d9STrond Myklebust 		.range_start = range_start,
17517f2f12d9STrond Myklebust 		.range_end = range_end,
17527f2f12d9STrond Myklebust 	};
17537f2f12d9STrond Myklebust 	int ret;
17547f2f12d9STrond Myklebust 
17550522f6adSTrond Myklebust 	for (;;) {
1756ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
17577f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
17587f2f12d9STrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
17597f2f12d9STrond Myklebust 			if (ret < 0)
17607f2f12d9STrond Myklebust 				goto out_error;
17610522f6adSTrond Myklebust 			continue;
17627f2f12d9STrond Myklebust 		}
17630522f6adSTrond Myklebust 		if (!PagePrivate(page))
17640522f6adSTrond Myklebust 			break;
17650522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
17667f2f12d9STrond Myklebust 		if (ret < 0)
17677f2f12d9STrond Myklebust 			goto out_error;
17687f2f12d9STrond Myklebust 	}
17697f2f12d9STrond Myklebust 	return 0;
17707f2f12d9STrond Myklebust out_error:
17717f2f12d9STrond Myklebust 	return ret;
17721c75950bSTrond Myklebust }
17731c75950bSTrond Myklebust 
1774074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
1775074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1776a6bc32b8SMel Gorman 		struct page *page, enum migrate_mode mode)
1777074cc1deSTrond Myklebust {
17782da95652SJeff Layton 	/*
17792da95652SJeff Layton 	 * If PagePrivate is set, then the page is currently associated with
17802da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
17812da95652SJeff Layton 	 *
17822da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
17832da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
17842da95652SJeff Layton 	 *        the page lock.
17852da95652SJeff Layton 	 */
17862da95652SJeff Layton 	if (PagePrivate(page))
17872da95652SJeff Layton 		return -EBUSY;
1788074cc1deSTrond Myklebust 
1789074cc1deSTrond Myklebust 	nfs_fscache_release_page(page, GFP_KERNEL);
1790074cc1deSTrond Myklebust 
1791a6bc32b8SMel Gorman 	return migrate_page(mapping, newpage, page, mode);
1792074cc1deSTrond Myklebust }
1793074cc1deSTrond Myklebust #endif
1794074cc1deSTrond Myklebust 
1795f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
17961da177e4SLinus Torvalds {
17971da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1798cd841605SFred Isaman 					     sizeof(struct nfs_write_header),
17991da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
180020c2df83SPaul Mundt 					     NULL);
18011da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
18021da177e4SLinus Torvalds 		return -ENOMEM;
18031da177e4SLinus Torvalds 
180493d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
18051da177e4SLinus Torvalds 						     nfs_wdata_cachep);
18061da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
18071da177e4SLinus Torvalds 		return -ENOMEM;
18081da177e4SLinus Torvalds 
18090b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
18100b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
18110b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
18120b7c0153SFred Isaman 					     NULL);
18130b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
18140b7c0153SFred Isaman 		return -ENOMEM;
18150b7c0153SFred Isaman 
181693d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
18171da177e4SLinus Torvalds 						      nfs_wdata_cachep);
18181da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
18191da177e4SLinus Torvalds 		return -ENOMEM;
18201da177e4SLinus Torvalds 
182189a09141SPeter Zijlstra 	/*
182289a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
182389a09141SPeter Zijlstra 	 *
182489a09141SPeter Zijlstra 	 *  64MB:    8192k
182589a09141SPeter Zijlstra 	 * 128MB:   11585k
182689a09141SPeter Zijlstra 	 * 256MB:   16384k
182789a09141SPeter Zijlstra 	 * 512MB:   23170k
182889a09141SPeter Zijlstra 	 *   1GB:   32768k
182989a09141SPeter Zijlstra 	 *   2GB:   46340k
183089a09141SPeter Zijlstra 	 *   4GB:   65536k
183189a09141SPeter Zijlstra 	 *   8GB:   92681k
183289a09141SPeter Zijlstra 	 *  16GB:  131072k
183389a09141SPeter Zijlstra 	 *
183489a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
183589a09141SPeter Zijlstra 	 * Limit the default to 256M
183689a09141SPeter Zijlstra 	 */
183789a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
183889a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
183989a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
184089a09141SPeter Zijlstra 
18411da177e4SLinus Torvalds 	return 0;
18421da177e4SLinus Torvalds }
18431da177e4SLinus Torvalds 
1844266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
18451da177e4SLinus Torvalds {
18461da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
18471da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
18481a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
18491da177e4SLinus Torvalds }
18501da177e4SLinus Torvalds 
1851