xref: /openbmc/linux/fs/nfs/write.c (revision 7e5f6146)
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>
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
181da177e4SLinus Torvalds #include <linux/nfs_fs.h>
191da177e4SLinus Torvalds #include <linux/nfs_mount.h>
201da177e4SLinus Torvalds #include <linux/nfs_page.h>
213fcfab16SAndrew Morton #include <linux/backing-dev.h>
223fcfab16SAndrew Morton 
231da177e4SLinus Torvalds #include <asm/uaccess.h>
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds #include "delegation.h"
2649a70f27STrond Myklebust #include "internal.h"
2791d5b470SChuck Lever #include "iostat.h"
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #define MIN_POOL_WRITE		(32)
321da177e4SLinus Torvalds #define MIN_POOL_COMMIT		(4)
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds /*
351da177e4SLinus Torvalds  * Local function declarations
361da177e4SLinus Torvalds  */
371da177e4SLinus Torvalds static struct nfs_page * nfs_update_request(struct nfs_open_context*,
381da177e4SLinus Torvalds 					    struct page *,
391da177e4SLinus Torvalds 					    unsigned int, unsigned int);
40c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc,
41c63c7b05STrond Myklebust 				  struct inode *inode, int ioflags);
42f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req);
43788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops;
44788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops;
45788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops;
461da177e4SLinus Torvalds 
47e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep;
483feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
491da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
501da177e4SLinus Torvalds 
51c9d8f89dSTrond Myklebust struct nfs_write_data *nfs_commitdata_alloc(void)
521da177e4SLinus Torvalds {
53e6b4f8daSChristoph Lameter 	struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
5440859d7eSChuck Lever 
551da177e4SLinus Torvalds 	if (p) {
561da177e4SLinus Torvalds 		memset(p, 0, sizeof(*p));
571da177e4SLinus Torvalds 		INIT_LIST_HEAD(&p->pages);
581da177e4SLinus Torvalds 	}
591da177e4SLinus Torvalds 	return p;
601da177e4SLinus Torvalds }
611da177e4SLinus Torvalds 
625e4424afSTrond Myklebust void nfs_commit_free(struct nfs_write_data *p)
631da177e4SLinus Torvalds {
6440859d7eSChuck Lever 	if (p && (p->pagevec != &p->page_array[0]))
6540859d7eSChuck Lever 		kfree(p->pagevec);
661da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds 
698d5658c9STrond Myklebust struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
703feb2d49STrond Myklebust {
71e6b4f8daSChristoph Lameter 	struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
723feb2d49STrond Myklebust 
733feb2d49STrond Myklebust 	if (p) {
743feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
753feb2d49STrond Myklebust 		INIT_LIST_HEAD(&p->pages);
76e9f7bee1STrond Myklebust 		p->npages = pagecount;
770d0b5cb3SChuck Lever 		if (pagecount <= ARRAY_SIZE(p->page_array))
780d0b5cb3SChuck Lever 			p->pagevec = p->page_array;
793feb2d49STrond Myklebust 		else {
800d0b5cb3SChuck Lever 			p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
810d0b5cb3SChuck Lever 			if (!p->pagevec) {
823feb2d49STrond Myklebust 				mempool_free(p, nfs_wdata_mempool);
833feb2d49STrond Myklebust 				p = NULL;
843feb2d49STrond Myklebust 			}
853feb2d49STrond Myklebust 		}
863feb2d49STrond Myklebust 	}
873feb2d49STrond Myklebust 	return p;
883feb2d49STrond Myklebust }
893feb2d49STrond Myklebust 
905e4424afSTrond Myklebust static void nfs_writedata_free(struct nfs_write_data *p)
913feb2d49STrond Myklebust {
923feb2d49STrond Myklebust 	if (p && (p->pagevec != &p->page_array[0]))
933feb2d49STrond Myklebust 		kfree(p->pagevec);
943feb2d49STrond Myklebust 	mempool_free(p, nfs_wdata_mempool);
953feb2d49STrond Myklebust }
963feb2d49STrond Myklebust 
97383ba719STrond Myklebust void nfs_writedata_release(void *data)
981da177e4SLinus Torvalds {
99383ba719STrond Myklebust 	struct nfs_write_data *wdata = data;
100383ba719STrond Myklebust 
101383ba719STrond Myklebust 	put_nfs_open_context(wdata->args.context);
1021da177e4SLinus Torvalds 	nfs_writedata_free(wdata);
1031da177e4SLinus Torvalds }
1041da177e4SLinus Torvalds 
1057b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
1067b159fc1STrond Myklebust {
1077b159fc1STrond Myklebust 	ctx->error = error;
1087b159fc1STrond Myklebust 	smp_wmb();
1097b159fc1STrond Myklebust 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
1107b159fc1STrond Myklebust }
1117b159fc1STrond Myklebust 
112277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request_locked(struct page *page)
113277459d2STrond Myklebust {
114277459d2STrond Myklebust 	struct nfs_page *req = NULL;
115277459d2STrond Myklebust 
116277459d2STrond Myklebust 	if (PagePrivate(page)) {
117277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
118277459d2STrond Myklebust 		if (req != NULL)
119c03b4024STrond Myklebust 			kref_get(&req->wb_kref);
120277459d2STrond Myklebust 	}
121277459d2STrond Myklebust 	return req;
122277459d2STrond Myklebust }
123277459d2STrond Myklebust 
124277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page)
125277459d2STrond Myklebust {
126587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
127277459d2STrond Myklebust 	struct nfs_page *req = NULL;
128277459d2STrond Myklebust 
129587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
130277459d2STrond Myklebust 	req = nfs_page_find_request_locked(page);
131587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
132277459d2STrond Myklebust 	return req;
133277459d2STrond Myklebust }
134277459d2STrond Myklebust 
1351da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1361da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1371da177e4SLinus Torvalds {
1381da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
1391da177e4SLinus Torvalds 	loff_t end, i_size = i_size_read(inode);
140ca52fec1STrond Myklebust 	pgoff_t end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	if (i_size > 0 && page->index < end_index)
1431da177e4SLinus Torvalds 		return;
1441da177e4SLinus Torvalds 	end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
1451da177e4SLinus Torvalds 	if (i_size >= end)
1461da177e4SLinus Torvalds 		return;
14791d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
1481da177e4SLinus Torvalds 	i_size_write(inode, end);
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
151a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
152a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
153a301b777STrond Myklebust {
154a301b777STrond Myklebust 	SetPageError(page);
155a301b777STrond Myklebust 	nfs_zap_mapping(page->mapping->host, page->mapping);
156a301b777STrond Myklebust }
157a301b777STrond Myklebust 
1581da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
1591da177e4SLinus Torvalds  * covers the full page.
1601da177e4SLinus Torvalds  */
1611da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
1621da177e4SLinus Torvalds {
1631da177e4SLinus Torvalds 	if (PageUptodate(page))
1641da177e4SLinus Torvalds 		return;
1651da177e4SLinus Torvalds 	if (base != 0)
1661da177e4SLinus Torvalds 		return;
16749a70f27STrond Myklebust 	if (count != nfs_page_length(page))
1681da177e4SLinus Torvalds 		return;
1691da177e4SLinus Torvalds 	SetPageUptodate(page);
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds 
172e21195a7STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
1731da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds 	struct nfs_page	*req;
176e21195a7STrond Myklebust 	int ret;
1771da177e4SLinus Torvalds 
178e21195a7STrond Myklebust 	for (;;) {
179e21195a7STrond Myklebust 		req = nfs_update_request(ctx, page, offset, count);
180e21195a7STrond Myklebust 		if (!IS_ERR(req))
181e21195a7STrond Myklebust 			break;
182e21195a7STrond Myklebust 		ret = PTR_ERR(req);
183e21195a7STrond Myklebust 		if (ret != -EBUSY)
184e21195a7STrond Myklebust 			return ret;
185e21195a7STrond Myklebust 		ret = nfs_wb_page(page->mapping->host, page);
186e21195a7STrond Myklebust 		if (ret != 0)
187e21195a7STrond Myklebust 			return ret;
188e21195a7STrond Myklebust 	}
1891da177e4SLinus Torvalds 	/* Update file length */
1901da177e4SLinus Torvalds 	nfs_grow_file(page, offset, count);
1917e5f6146STrond Myklebust 	nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
192acee478aSTrond Myklebust 	nfs_clear_page_tag_locked(req);
193abd3e641STrond Myklebust 	return 0;
1941da177e4SLinus Torvalds }
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds 	if (wbc->for_reclaim)
199c63c7b05STrond Myklebust 		return FLUSH_HIGHPRI | FLUSH_STABLE;
2001da177e4SLinus Torvalds 	if (wbc->for_kupdate)
2011da177e4SLinus Torvalds 		return FLUSH_LOWPRI;
2021da177e4SLinus Torvalds 	return 0;
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds /*
20689a09141SPeter Zijlstra  * NFS congestion control
20789a09141SPeter Zijlstra  */
20889a09141SPeter Zijlstra 
20989a09141SPeter Zijlstra int nfs_congestion_kb;
21089a09141SPeter Zijlstra 
21189a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
21289a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
21389a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
21489a09141SPeter Zijlstra 
2155a6d41b3STrond Myklebust static int nfs_set_page_writeback(struct page *page)
21689a09141SPeter Zijlstra {
2175a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
2185a6d41b3STrond Myklebust 
2195a6d41b3STrond Myklebust 	if (!ret) {
22089a09141SPeter Zijlstra 		struct inode *inode = page->mapping->host;
22189a09141SPeter Zijlstra 		struct nfs_server *nfss = NFS_SERVER(inode);
22289a09141SPeter Zijlstra 
223277866a0SPeter Zijlstra 		if (atomic_long_inc_return(&nfss->writeback) >
22489a09141SPeter Zijlstra 				NFS_CONGESTION_ON_THRESH)
22589a09141SPeter Zijlstra 			set_bdi_congested(&nfss->backing_dev_info, WRITE);
22689a09141SPeter Zijlstra 	}
2275a6d41b3STrond Myklebust 	return ret;
22889a09141SPeter Zijlstra }
22989a09141SPeter Zijlstra 
23089a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page)
23189a09141SPeter Zijlstra {
23289a09141SPeter Zijlstra 	struct inode *inode = page->mapping->host;
23389a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
23489a09141SPeter Zijlstra 
23589a09141SPeter Zijlstra 	end_page_writeback(page);
236c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
23789a09141SPeter Zijlstra 		clear_bdi_congested(&nfss->backing_dev_info, WRITE);
23889a09141SPeter Zijlstra }
23989a09141SPeter Zijlstra 
24089a09141SPeter Zijlstra /*
241e261f51fSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
2429cccef95STrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
243e261f51fSTrond Myklebust  */
244c63c7b05STrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
245c63c7b05STrond Myklebust 				struct page *page)
246e261f51fSTrond Myklebust {
247587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
248e261f51fSTrond Myklebust 	struct nfs_page *req;
249e261f51fSTrond Myklebust 	int ret;
250e261f51fSTrond Myklebust 
251587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
252e261f51fSTrond Myklebust 	for(;;) {
253e261f51fSTrond Myklebust 		req = nfs_page_find_request_locked(page);
254e261f51fSTrond Myklebust 		if (req == NULL) {
255587142f8STrond Myklebust 			spin_unlock(&inode->i_lock);
2569cccef95STrond Myklebust 			return 0;
257e261f51fSTrond Myklebust 		}
258acee478aSTrond Myklebust 		if (nfs_set_page_tag_locked(req))
259e261f51fSTrond Myklebust 			break;
260e261f51fSTrond Myklebust 		/* Note: If we hold the page lock, as is the case in nfs_writepage,
261acee478aSTrond Myklebust 		 *	 then the call to nfs_set_page_tag_locked() will always
262e261f51fSTrond Myklebust 		 *	 succeed provided that someone hasn't already marked the
263e261f51fSTrond Myklebust 		 *	 request as dirty (in which case we don't care).
264e261f51fSTrond Myklebust 		 */
265587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
266e261f51fSTrond Myklebust 		ret = nfs_wait_on_request(req);
267e261f51fSTrond Myklebust 		nfs_release_request(req);
268e261f51fSTrond Myklebust 		if (ret != 0)
269e261f51fSTrond Myklebust 			return ret;
270587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
271e261f51fSTrond Myklebust 	}
272612c9384STrond Myklebust 	if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
273612c9384STrond Myklebust 		/* This request is marked for commit */
274587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
275acee478aSTrond Myklebust 		nfs_clear_page_tag_locked(req);
276c63c7b05STrond Myklebust 		nfs_pageio_complete(pgio);
2779cccef95STrond Myklebust 		return 0;
278612c9384STrond Myklebust 	}
279c63c7b05STrond Myklebust 	if (nfs_set_page_writeback(page) != 0) {
280587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
281c63c7b05STrond Myklebust 		BUG();
282c63c7b05STrond Myklebust 	}
283587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
284f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
285f8512ad0SFred Isaman 		nfs_redirty_request(req);
286f8512ad0SFred Isaman 		return pgio->pg_error;
287f8512ad0SFred Isaman 	}
2889cccef95STrond Myklebust 	return 0;
289e261f51fSTrond Myklebust }
290e261f51fSTrond Myklebust 
291f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
292f758c885STrond Myklebust {
293f758c885STrond Myklebust 	struct inode *inode = page->mapping->host;
294f758c885STrond Myklebust 
295f758c885STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
296f758c885STrond Myklebust 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
297f758c885STrond Myklebust 
298f758c885STrond Myklebust 	nfs_pageio_cond_complete(pgio, page->index);
299f758c885STrond Myklebust 	return nfs_page_async_flush(pgio, page);
300f758c885STrond Myklebust }
301f758c885STrond Myklebust 
302e261f51fSTrond Myklebust /*
3031da177e4SLinus Torvalds  * Write an mmapped page to the server.
3041da177e4SLinus Torvalds  */
3054d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
3061da177e4SLinus Torvalds {
307f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
308e261f51fSTrond Myklebust 	int err;
3091da177e4SLinus Torvalds 
310f758c885STrond Myklebust 	nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
311f758c885STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
312f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
313f758c885STrond Myklebust 	if (err < 0)
3144d770ccfSTrond Myklebust 		return err;
315f758c885STrond Myklebust 	if (pgio.pg_error < 0)
316f758c885STrond Myklebust 		return pgio.pg_error;
317f758c885STrond Myklebust 	return 0;
3184d770ccfSTrond Myklebust }
3194d770ccfSTrond Myklebust 
3204d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
3214d770ccfSTrond Myklebust {
322f758c885STrond Myklebust 	int ret;
3234d770ccfSTrond Myklebust 
324f758c885STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
3251da177e4SLinus Torvalds 	unlock_page(page);
326f758c885STrond Myklebust 	return ret;
327f758c885STrond Myklebust }
328f758c885STrond Myklebust 
329f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
330f758c885STrond Myklebust {
331f758c885STrond Myklebust 	int ret;
332f758c885STrond Myklebust 
333f758c885STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
334f758c885STrond Myklebust 	unlock_page(page);
335f758c885STrond Myklebust 	return ret;
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3391da177e4SLinus Torvalds {
3401da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
341c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
3421da177e4SLinus Torvalds 	int err;
3431da177e4SLinus Torvalds 
34491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
34591d5b470SChuck Lever 
346c63c7b05STrond Myklebust 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
347f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
348c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
349f758c885STrond Myklebust 	if (err < 0)
3501da177e4SLinus Torvalds 		return err;
351f758c885STrond Myklebust 	if (pgio.pg_error < 0)
352c63c7b05STrond Myklebust 		return pgio.pg_error;
353c63c7b05STrond Myklebust 	return 0;
3541da177e4SLinus Torvalds }
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds /*
3571da177e4SLinus Torvalds  * Insert a write request into an inode
3581da177e4SLinus Torvalds  */
35927852596SNick Piggin static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
3601da177e4SLinus Torvalds {
3611da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
3621da177e4SLinus Torvalds 	int error;
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
36527852596SNick Piggin 	BUG_ON(error);
3661da177e4SLinus Torvalds 	if (!nfsi->npages) {
3671da177e4SLinus Torvalds 		igrab(inode);
3681da177e4SLinus Torvalds 		if (nfs_have_delegation(inode, FMODE_WRITE))
3691da177e4SLinus Torvalds 			nfsi->change_attr++;
3701da177e4SLinus Torvalds 	}
371deb7d638STrond Myklebust 	SetPagePrivate(req->wb_page);
372277459d2STrond Myklebust 	set_page_private(req->wb_page, (unsigned long)req);
3731da177e4SLinus Torvalds 	nfsi->npages++;
374c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
37527852596SNick Piggin 	radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
37627852596SNick Piggin 				NFS_PAGE_TAG_LOCKED);
3771da177e4SLinus Torvalds }
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds /*
38089a09141SPeter Zijlstra  * Remove a write request from an inode
3811da177e4SLinus Torvalds  */
3821da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
3831da177e4SLinus Torvalds {
38488be9f99STrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
3851da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	BUG_ON (!NFS_WBACK_BUSY(req));
3881da177e4SLinus Torvalds 
389587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
390277459d2STrond Myklebust 	set_page_private(req->wb_page, 0);
391deb7d638STrond Myklebust 	ClearPagePrivate(req->wb_page);
3921da177e4SLinus Torvalds 	radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
3931da177e4SLinus Torvalds 	nfsi->npages--;
3941da177e4SLinus Torvalds 	if (!nfsi->npages) {
395587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
3961da177e4SLinus Torvalds 		iput(inode);
3971da177e4SLinus Torvalds 	} else
398587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
3991da177e4SLinus Torvalds 	nfs_clear_request(req);
4001da177e4SLinus Torvalds 	nfs_release_request(req);
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
40361822ab5STrond Myklebust static void
4046d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
40561822ab5STrond Myklebust {
40661822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
40761822ab5STrond Myklebust }
40861822ab5STrond Myklebust 
4091da177e4SLinus Torvalds /*
4101da177e4SLinus Torvalds  * Check if a request is dirty
4111da177e4SLinus Torvalds  */
4121da177e4SLinus Torvalds static inline int
4131da177e4SLinus Torvalds nfs_dirty_request(struct nfs_page *req)
4141da177e4SLinus Torvalds {
4155a6d41b3STrond Myklebust 	struct page *page = req->wb_page;
4165a6d41b3STrond Myklebust 
417612c9384STrond Myklebust 	if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
4185a6d41b3STrond Myklebust 		return 0;
41938def50fSFred Isaman 	return !PageWriteback(page);
4201da177e4SLinus Torvalds }
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
4231da177e4SLinus Torvalds /*
4241da177e4SLinus Torvalds  * Add a request to the inode's commit list.
4251da177e4SLinus Torvalds  */
4261da177e4SLinus Torvalds static void
4271da177e4SLinus Torvalds nfs_mark_request_commit(struct nfs_page *req)
4281da177e4SLinus Torvalds {
42988be9f99STrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
4301da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4311da177e4SLinus Torvalds 
432587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
4331da177e4SLinus Torvalds 	nfsi->ncommit++;
434612c9384STrond Myklebust 	set_bit(PG_NEED_COMMIT, &(req)->wb_flags);
4355c369683STrond Myklebust 	radix_tree_tag_set(&nfsi->nfs_page_tree,
4365c369683STrond Myklebust 			req->wb_index,
4375c369683STrond Myklebust 			NFS_PAGE_TAG_COMMIT);
438587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
439fd39fc85SChristoph Lameter 	inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
440c9e51e41SPeter Zijlstra 	inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
441a1803044STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
4421da177e4SLinus Torvalds }
4438e821cadSTrond Myklebust 
4448e821cadSTrond Myklebust static inline
4458e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
4468e821cadSTrond Myklebust {
4478e821cadSTrond Myklebust 	return data->verf.committed != NFS_FILE_SYNC;
4488e821cadSTrond Myklebust }
4498e821cadSTrond Myklebust 
4508e821cadSTrond Myklebust static inline
4518e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req)
4528e821cadSTrond Myklebust {
453612c9384STrond Myklebust 	if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
4548e821cadSTrond Myklebust 		nfs_mark_request_commit(req);
4558e821cadSTrond Myklebust 		return 1;
4568e821cadSTrond Myklebust 	}
4578e821cadSTrond Myklebust 	if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
4586d884e8fSFred 		nfs_mark_request_dirty(req);
4598e821cadSTrond Myklebust 		return 1;
4608e821cadSTrond Myklebust 	}
4618e821cadSTrond Myklebust 	return 0;
4628e821cadSTrond Myklebust }
4638e821cadSTrond Myklebust #else
4648e821cadSTrond Myklebust static inline void
4658e821cadSTrond Myklebust nfs_mark_request_commit(struct nfs_page *req)
4668e821cadSTrond Myklebust {
4678e821cadSTrond Myklebust }
4688e821cadSTrond Myklebust 
4698e821cadSTrond Myklebust static inline
4708e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
4718e821cadSTrond Myklebust {
4728e821cadSTrond Myklebust 	return 0;
4738e821cadSTrond Myklebust }
4748e821cadSTrond Myklebust 
4758e821cadSTrond Myklebust static inline
4768e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req)
4778e821cadSTrond Myklebust {
4788e821cadSTrond Myklebust 	return 0;
4798e821cadSTrond Myklebust }
4801da177e4SLinus Torvalds #endif
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds /*
4831da177e4SLinus Torvalds  * Wait for a request to complete.
4841da177e4SLinus Torvalds  *
485150030b7SMatthew Wilcox  * Interruptible by fatal signals only.
4861da177e4SLinus Torvalds  */
487ca52fec1STrond Myklebust static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
4881da177e4SLinus Torvalds {
4891da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4901da177e4SLinus Torvalds 	struct nfs_page *req;
491ca52fec1STrond Myklebust 	pgoff_t idx_end, next;
4921da177e4SLinus Torvalds 	unsigned int		res = 0;
4931da177e4SLinus Torvalds 	int			error;
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	if (npages == 0)
4961da177e4SLinus Torvalds 		idx_end = ~0;
4971da177e4SLinus Torvalds 	else
4981da177e4SLinus Torvalds 		idx_end = idx_start + npages - 1;
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 	next = idx_start;
5019fd367f0STrond Myklebust 	while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) {
5021da177e4SLinus Torvalds 		if (req->wb_index > idx_end)
5031da177e4SLinus Torvalds 			break;
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 		next = req->wb_index + 1;
506c6a556b8STrond Myklebust 		BUG_ON(!NFS_WBACK_BUSY(req));
5071da177e4SLinus Torvalds 
508c03b4024STrond Myklebust 		kref_get(&req->wb_kref);
509587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
5101da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
5111da177e4SLinus Torvalds 		nfs_release_request(req);
512587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
5131da177e4SLinus Torvalds 		if (error < 0)
5141da177e4SLinus Torvalds 			return error;
5151da177e4SLinus Torvalds 		res++;
5161da177e4SLinus Torvalds 	}
5171da177e4SLinus Torvalds 	return res;
5181da177e4SLinus Torvalds }
5191da177e4SLinus Torvalds 
52083715ad5STrond Myklebust static void nfs_cancel_commit_list(struct list_head *head)
52183715ad5STrond Myklebust {
52283715ad5STrond Myklebust 	struct nfs_page *req;
52383715ad5STrond Myklebust 
52483715ad5STrond Myklebust 	while(!list_empty(head)) {
52583715ad5STrond Myklebust 		req = nfs_list_entry(head->next);
526b6dff26aSTrond Myklebust 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
527c9e51e41SPeter Zijlstra 		dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
528c9e51e41SPeter Zijlstra 				BDI_RECLAIMABLE);
52983715ad5STrond Myklebust 		nfs_list_remove_request(req);
530612c9384STrond Myklebust 		clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
53183715ad5STrond Myklebust 		nfs_inode_remove_request(req);
532b6dff26aSTrond Myklebust 		nfs_unlock_request(req);
53383715ad5STrond Myklebust 	}
53483715ad5STrond Myklebust }
53583715ad5STrond Myklebust 
5361da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
5371da177e4SLinus Torvalds /*
5381da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
5391da177e4SLinus Torvalds  * @inode: NFS inode to scan
5401da177e4SLinus Torvalds  * @dst: destination list
5411da177e4SLinus Torvalds  * @idx_start: lower bound of page->index to scan.
5421da177e4SLinus Torvalds  * @npages: idx_start + npages sets the upper bound to scan.
5431da177e4SLinus Torvalds  *
5441da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
5451da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
5461da177e4SLinus Torvalds  */
5471da177e4SLinus Torvalds static int
548ca52fec1STrond Myklebust nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
5491da177e4SLinus Torvalds {
5501da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
5513da28eb1STrond Myklebust 	int res = 0;
5523da28eb1STrond Myklebust 
5533da28eb1STrond Myklebust 	if (nfsi->ncommit != 0) {
5545c369683STrond Myklebust 		res = nfs_scan_list(nfsi, dst, idx_start, npages,
5555c369683STrond Myklebust 				NFS_PAGE_TAG_COMMIT);
5561da177e4SLinus Torvalds 		nfsi->ncommit -= res;
5573da28eb1STrond Myklebust 	}
5581da177e4SLinus Torvalds 	return res;
5591da177e4SLinus Torvalds }
560c42de9ddSTrond Myklebust #else
561ca52fec1STrond Myklebust static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
562c42de9ddSTrond Myklebust {
563c42de9ddSTrond Myklebust 	return 0;
564c42de9ddSTrond Myklebust }
5651da177e4SLinus Torvalds #endif
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds /*
5681da177e4SLinus Torvalds  * Try to update any existing write request, or create one if there is none.
5691da177e4SLinus Torvalds  * In order to match, the request's credentials must match those of
5701da177e4SLinus Torvalds  * the calling process.
5711da177e4SLinus Torvalds  *
5721da177e4SLinus Torvalds  * Note: Should always be called with the Page Lock held!
5731da177e4SLinus Torvalds  */
5741da177e4SLinus Torvalds static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
575e21195a7STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
5761da177e4SLinus Torvalds {
57789a09141SPeter Zijlstra 	struct address_space *mapping = page->mapping;
57889a09141SPeter Zijlstra 	struct inode *inode = mapping->host;
5791da177e4SLinus Torvalds 	struct nfs_page		*req, *new = NULL;
580ca52fec1STrond Myklebust 	pgoff_t		rqend, end;
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	end = offset + bytes;
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	for (;;) {
5851da177e4SLinus Torvalds 		/* Loop over all inode entries and see if we find
5861da177e4SLinus Torvalds 		 * A request for the page we wish to update
5871da177e4SLinus Torvalds 		 */
588587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
589277459d2STrond Myklebust 		req = nfs_page_find_request_locked(page);
5901da177e4SLinus Torvalds 		if (req) {
591acee478aSTrond Myklebust 			if (!nfs_set_page_tag_locked(req)) {
5921da177e4SLinus Torvalds 				int error;
593277459d2STrond Myklebust 
594587142f8STrond Myklebust 				spin_unlock(&inode->i_lock);
5951da177e4SLinus Torvalds 				error = nfs_wait_on_request(req);
5961da177e4SLinus Torvalds 				nfs_release_request(req);
5971dd594b2SNeil Brown 				if (error < 0) {
59827852596SNick Piggin 					if (new) {
59927852596SNick Piggin 						radix_tree_preload_end();
6001dd594b2SNeil Brown 						nfs_release_request(new);
60127852596SNick Piggin 					}
6021da177e4SLinus Torvalds 					return ERR_PTR(error);
6031dd594b2SNeil Brown 				}
6041da177e4SLinus Torvalds 				continue;
6051da177e4SLinus Torvalds 			}
606587142f8STrond Myklebust 			spin_unlock(&inode->i_lock);
60727852596SNick Piggin 			if (new) {
60827852596SNick Piggin 				radix_tree_preload_end();
6091da177e4SLinus Torvalds 				nfs_release_request(new);
61027852596SNick Piggin 			}
6111da177e4SLinus Torvalds 			break;
6121da177e4SLinus Torvalds 		}
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 		if (new) {
6151da177e4SLinus Torvalds 			nfs_lock_request_dontget(new);
61627852596SNick Piggin 			nfs_inode_add_request(inode, new);
617587142f8STrond Myklebust 			spin_unlock(&inode->i_lock);
61827852596SNick Piggin 			radix_tree_preload_end();
61961e930a9STrond Myklebust 			req = new;
620efc91ed0STrond Myklebust 			goto out;
6211da177e4SLinus Torvalds 		}
622587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 		new = nfs_create_request(ctx, inode, page, offset, bytes);
6251da177e4SLinus Torvalds 		if (IS_ERR(new))
6261da177e4SLinus Torvalds 			return new;
627f3d47a3aSTrond Myklebust 		if (radix_tree_preload(GFP_NOFS)) {
628f3d47a3aSTrond Myklebust 			nfs_release_request(new);
629f3d47a3aSTrond Myklebust 			return ERR_PTR(-ENOMEM);
630f3d47a3aSTrond Myklebust 		}
6311da177e4SLinus Torvalds 	}
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	/* We have a request for our page.
6341da177e4SLinus Torvalds 	 * If the creds don't match, or the
6351da177e4SLinus Torvalds 	 * page addresses don't match,
6361da177e4SLinus Torvalds 	 * tell the caller to wait on the conflicting
6371da177e4SLinus Torvalds 	 * request.
6381da177e4SLinus Torvalds 	 */
6391da177e4SLinus Torvalds 	rqend = req->wb_offset + req->wb_bytes;
6401da177e4SLinus Torvalds 	if (req->wb_context != ctx
6411da177e4SLinus Torvalds 	    || req->wb_page != page
6421da177e4SLinus Torvalds 	    || !nfs_dirty_request(req)
6431da177e4SLinus Torvalds 	    || offset > rqend || end < req->wb_offset) {
644acee478aSTrond Myklebust 		nfs_clear_page_tag_locked(req);
6451da177e4SLinus Torvalds 		return ERR_PTR(-EBUSY);
6461da177e4SLinus Torvalds 	}
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
6491da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
6501da177e4SLinus Torvalds 		req->wb_offset = offset;
6511da177e4SLinus Torvalds 		req->wb_pgbase = offset;
65261e930a9STrond Myklebust 		req->wb_bytes = max(end, rqend) - req->wb_offset;
653efc91ed0STrond Myklebust 		goto out;
6541da177e4SLinus Torvalds 	}
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	if (end > rqend)
6571da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
6581da177e4SLinus Torvalds 
659efc91ed0STrond Myklebust out:
66061e930a9STrond Myklebust 	return req;
6611da177e4SLinus Torvalds }
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
6641da177e4SLinus Torvalds {
665cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
6661da177e4SLinus Torvalds 	struct nfs_page	*req;
6671a54533eSTrond Myklebust 	int do_flush, status;
6681da177e4SLinus Torvalds 	/*
6691da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
6701da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
6711da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
6721da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
6731da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
6741da177e4SLinus Torvalds 	 * dropped page.
6751da177e4SLinus Torvalds 	 */
6761a54533eSTrond Myklebust 	do {
677277459d2STrond Myklebust 		req = nfs_page_find_request(page);
6781a54533eSTrond Myklebust 		if (req == NULL)
6791a54533eSTrond Myklebust 			return 0;
6801a54533eSTrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx
681e261f51fSTrond Myklebust 			|| !nfs_dirty_request(req);
6821da177e4SLinus Torvalds 		nfs_release_request(req);
6831a54533eSTrond Myklebust 		if (!do_flush)
6841a54533eSTrond Myklebust 			return 0;
685277459d2STrond Myklebust 		status = nfs_wb_page(page->mapping->host, page);
6861a54533eSTrond Myklebust 	} while (status == 0);
6871a54533eSTrond Myklebust 	return status;
6881da177e4SLinus Torvalds }
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds /*
6915d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
6925d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
6935d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
6945d47a356STrond Myklebust  */
6955d47a356STrond Myklebust static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
6965d47a356STrond Myklebust {
6975d47a356STrond Myklebust 	return PageUptodate(page) &&
6985d47a356STrond Myklebust 		!(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
6995d47a356STrond Myklebust }
7005d47a356STrond Myklebust 
7015d47a356STrond Myklebust /*
7021da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
7031da177e4SLinus Torvalds  *
7041da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
7051da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
7061da177e4SLinus Torvalds  */
7071da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
7081da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
7091da177e4SLinus Torvalds {
710cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
7111da177e4SLinus Torvalds 	struct inode	*inode = page->mapping->host;
7121da177e4SLinus Torvalds 	int		status = 0;
7131da177e4SLinus Torvalds 
71491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
71591d5b470SChuck Lever 
7161da177e4SLinus Torvalds 	dprintk("NFS:      nfs_updatepage(%s/%s %d@%Ld)\n",
71701cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
71801cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name, count,
7190bbacc40SChuck Lever 		(long long)(page_offset(page) +offset));
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 	/* If we're not using byte range locks, and we know the page
7225d47a356STrond Myklebust 	 * is up to date, it may be more efficient to extend the write
7235d47a356STrond Myklebust 	 * to cover the entire page in order to avoid fragmentation
7245d47a356STrond Myklebust 	 * inefficiencies.
7251da177e4SLinus Torvalds 	 */
7265d47a356STrond Myklebust 	if (nfs_write_pageuptodate(page, inode) &&
7275d47a356STrond Myklebust 			inode->i_flock == NULL &&
7284b5621f6STrond Myklebust 			!(file->f_flags & O_SYNC)) {
72949a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
7301da177e4SLinus Torvalds 		offset = 0;
7311da177e4SLinus Torvalds 	}
7321da177e4SLinus Torvalds 
733e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
73403fa9e84STrond Myklebust 	if (status < 0)
73503fa9e84STrond Myklebust 		nfs_set_pageerror(page);
73603fa9e84STrond Myklebust 	else
737e261f51fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds         dprintk("NFS:      nfs_updatepage returns %d (isize %Ld)\n",
7401da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
7411da177e4SLinus Torvalds 	return status;
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds static void nfs_writepage_release(struct nfs_page *req)
7451da177e4SLinus Torvalds {
7468e821cadSTrond Myklebust 
7477e5f6146STrond Myklebust 	if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req)) {
74889a09141SPeter Zijlstra 		nfs_end_page_writeback(req->wb_page);
7491da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
7508e821cadSTrond Myklebust 	} else
7518e821cadSTrond Myklebust 		nfs_end_page_writeback(req->wb_page);
7529fd367f0STrond Myklebust 	nfs_clear_page_tag_locked(req);
7531da177e4SLinus Torvalds }
7541da177e4SLinus Torvalds 
7553ff7576dSTrond Myklebust static int flush_task_priority(int how)
7561da177e4SLinus Torvalds {
7571da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
7581da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
7591da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
7601da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
7611da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
7621da177e4SLinus Torvalds 	}
7631da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
7641da177e4SLinus Torvalds }
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds /*
7671da177e4SLinus Torvalds  * Set up the argument/result storage required for the RPC call.
7681da177e4SLinus Torvalds  */
769dbae4c73STrond Myklebust static int nfs_write_rpcsetup(struct nfs_page *req,
7701da177e4SLinus Torvalds 		struct nfs_write_data *data,
771788e7a89STrond Myklebust 		const struct rpc_call_ops *call_ops,
7721da177e4SLinus Torvalds 		unsigned int count, unsigned int offset,
7731da177e4SLinus Torvalds 		int how)
7741da177e4SLinus Torvalds {
77584115e1cSTrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
77684115e1cSTrond Myklebust 	int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
7773ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
77807737691STrond Myklebust 	struct rpc_task *task;
779bdc7f021STrond Myklebust 	struct rpc_message msg = {
780bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
781bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
782bdc7f021STrond Myklebust 		.rpc_cred = req->wb_context->cred,
783bdc7f021STrond Myklebust 	};
78484115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
78584115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(inode),
78607737691STrond Myklebust 		.task = &data->task,
787bdc7f021STrond Myklebust 		.rpc_message = &msg,
78884115e1cSTrond Myklebust 		.callback_ops = call_ops,
78984115e1cSTrond Myklebust 		.callback_data = data,
790101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
79184115e1cSTrond Myklebust 		.flags = flags,
7923ff7576dSTrond Myklebust 		.priority = priority,
79384115e1cSTrond Myklebust 	};
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
7961da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 	data->req = req;
79988be9f99STrond Myklebust 	data->inode = inode = req->wb_context->path.dentry->d_inode;
800bdc7f021STrond Myklebust 	data->cred = msg.rpc_cred;
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(inode);
8031da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
8041da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
8051da177e4SLinus Torvalds 	data->args.pages  = data->pagevec;
8061da177e4SLinus Torvalds 	data->args.count  = count;
807383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
808bdc7f021STrond Myklebust 	data->args.stable  = NFS_UNSTABLE;
809bdc7f021STrond Myklebust 	if (how & FLUSH_STABLE) {
810bdc7f021STrond Myklebust 		data->args.stable = NFS_DATA_SYNC;
811bdc7f021STrond Myklebust 		if (!NFS_I(inode)->ncommit)
812bdc7f021STrond Myklebust 			data->args.stable = NFS_FILE_SYNC;
813bdc7f021STrond Myklebust 	}
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
8161da177e4SLinus Torvalds 	data->res.count   = count;
8171da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
8180e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
8191da177e4SLinus Torvalds 
820788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
821bdc7f021STrond Myklebust 	NFS_PROTO(inode)->write_setup(data, &msg);
8221da177e4SLinus Torvalds 
823a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated write call "
824a3f565b1SChuck Lever 		"(req %s/%Ld, %u bytes @ offset %Lu)\n",
8250bbacc40SChuck Lever 		data->task.tk_pid,
8261da177e4SLinus Torvalds 		inode->i_sb->s_id,
8271da177e4SLinus Torvalds 		(long long)NFS_FILEID(inode),
8281da177e4SLinus Torvalds 		count,
8291da177e4SLinus Torvalds 		(unsigned long long)data->args.offset);
8301da177e4SLinus Torvalds 
83107737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
832dbae4c73STrond Myklebust 	if (IS_ERR(task))
833dbae4c73STrond Myklebust 		return PTR_ERR(task);
83407737691STrond Myklebust 	rpc_put_task(task);
835dbae4c73STrond Myklebust 	return 0;
8361da177e4SLinus Torvalds }
8371da177e4SLinus Torvalds 
8386d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
8396d884e8fSFred  * call this on each, which will prepare them to be retried on next
8406d884e8fSFred  * writeback using standard nfs.
8416d884e8fSFred  */
8426d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
8436d884e8fSFred {
8446d884e8fSFred 	nfs_mark_request_dirty(req);
8456d884e8fSFred 	nfs_end_page_writeback(req->wb_page);
8466d884e8fSFred 	nfs_clear_page_tag_locked(req);
8476d884e8fSFred }
8486d884e8fSFred 
8491da177e4SLinus Torvalds /*
8501da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
8511da177e4SLinus Torvalds  * contiguous dirty area on one page.
8521da177e4SLinus Torvalds  */
8538d5658c9STrond Myklebust static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
8541da177e4SLinus Torvalds {
8551da177e4SLinus Torvalds 	struct nfs_page *req = nfs_list_entry(head->next);
8561da177e4SLinus Torvalds 	struct page *page = req->wb_page;
8571da177e4SLinus Torvalds 	struct nfs_write_data *data;
858e9f7bee1STrond Myklebust 	size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
859e9f7bee1STrond Myklebust 	unsigned int offset;
8601da177e4SLinus Torvalds 	int requests = 0;
861dbae4c73STrond Myklebust 	int ret = 0;
8621da177e4SLinus Torvalds 	LIST_HEAD(list);
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	nfs_list_remove_request(req);
8651da177e4SLinus Torvalds 
866bcb71bbaSTrond Myklebust 	nbytes = count;
867e9f7bee1STrond Myklebust 	do {
868e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
869e9f7bee1STrond Myklebust 
8708d5658c9STrond Myklebust 		data = nfs_writedata_alloc(1);
8711da177e4SLinus Torvalds 		if (!data)
8721da177e4SLinus Torvalds 			goto out_bad;
8731da177e4SLinus Torvalds 		list_add(&data->pages, &list);
8741da177e4SLinus Torvalds 		requests++;
875e9f7bee1STrond Myklebust 		nbytes -= len;
876e9f7bee1STrond Myklebust 	} while (nbytes != 0);
8771da177e4SLinus Torvalds 	atomic_set(&req->wb_complete, requests);
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	ClearPageError(page);
8801da177e4SLinus Torvalds 	offset = 0;
881bcb71bbaSTrond Myklebust 	nbytes = count;
8821da177e4SLinus Torvalds 	do {
883dbae4c73STrond Myklebust 		int ret2;
884dbae4c73STrond Myklebust 
8851da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
8861da177e4SLinus Torvalds 		list_del_init(&data->pages);
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 		data->pagevec[0] = page;
8891da177e4SLinus Torvalds 
890bcb71bbaSTrond Myklebust 		if (nbytes < wsize)
891bcb71bbaSTrond Myklebust 			wsize = nbytes;
892dbae4c73STrond Myklebust 		ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
893788e7a89STrond Myklebust 				   wsize, offset, how);
894dbae4c73STrond Myklebust 		if (ret == 0)
895dbae4c73STrond Myklebust 			ret = ret2;
8961da177e4SLinus Torvalds 		offset += wsize;
8971da177e4SLinus Torvalds 		nbytes -= wsize;
8981da177e4SLinus Torvalds 	} while (nbytes != 0);
8991da177e4SLinus Torvalds 
900dbae4c73STrond Myklebust 	return ret;
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds out_bad:
9031da177e4SLinus Torvalds 	while (!list_empty(&list)) {
9041da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
9051da177e4SLinus Torvalds 		list_del(&data->pages);
9068aca67f0STrond Myklebust 		nfs_writedata_release(data);
9071da177e4SLinus Torvalds 	}
90861822ab5STrond Myklebust 	nfs_redirty_request(req);
9091da177e4SLinus Torvalds 	return -ENOMEM;
9101da177e4SLinus Torvalds }
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds /*
9131da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
9141da177e4SLinus Torvalds  * The page must have been locked by the caller.
9151da177e4SLinus Torvalds  *
9161da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
9171da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
9181da177e4SLinus Torvalds  * that has been written but not committed.
9191da177e4SLinus Torvalds  */
9208d5658c9STrond Myklebust static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
9211da177e4SLinus Torvalds {
9221da177e4SLinus Torvalds 	struct nfs_page		*req;
9231da177e4SLinus Torvalds 	struct page		**pages;
9241da177e4SLinus Torvalds 	struct nfs_write_data	*data;
9251da177e4SLinus Torvalds 
9268d5658c9STrond Myklebust 	data = nfs_writedata_alloc(npages);
9271da177e4SLinus Torvalds 	if (!data)
9281da177e4SLinus Torvalds 		goto out_bad;
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 	pages = data->pagevec;
9311da177e4SLinus Torvalds 	while (!list_empty(head)) {
9321da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
9331da177e4SLinus Torvalds 		nfs_list_remove_request(req);
9341da177e4SLinus Torvalds 		nfs_list_add_request(req, &data->pages);
9351da177e4SLinus Torvalds 		ClearPageError(req->wb_page);
9361da177e4SLinus Torvalds 		*pages++ = req->wb_page;
9371da177e4SLinus Torvalds 	}
9381da177e4SLinus Torvalds 	req = nfs_list_entry(data->pages.next);
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds 	/* Set up the argument struct */
941dbae4c73STrond Myklebust 	return nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
9421da177e4SLinus Torvalds  out_bad:
9431da177e4SLinus Torvalds 	while (!list_empty(head)) {
94410afec90STrond Myklebust 		req = nfs_list_entry(head->next);
9451da177e4SLinus Torvalds 		nfs_list_remove_request(req);
94661822ab5STrond Myklebust 		nfs_redirty_request(req);
9471da177e4SLinus Torvalds 	}
9481da177e4SLinus Torvalds 	return -ENOMEM;
9491da177e4SLinus Torvalds }
9501da177e4SLinus Torvalds 
951c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
952c63c7b05STrond Myklebust 				  struct inode *inode, int ioflags)
9531da177e4SLinus Torvalds {
954bf4285e7SChuck Lever 	size_t wsize = NFS_SERVER(inode)->wsize;
9551da177e4SLinus Torvalds 
956bcb71bbaSTrond Myklebust 	if (wsize < PAGE_CACHE_SIZE)
957c63c7b05STrond Myklebust 		nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
958bcb71bbaSTrond Myklebust 	else
959c63c7b05STrond Myklebust 		nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
9601da177e4SLinus Torvalds }
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds /*
9631da177e4SLinus Torvalds  * Handle a write reply that flushed part of a page.
9641da177e4SLinus Torvalds  */
965788e7a89STrond Myklebust static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
9661da177e4SLinus Torvalds {
967788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
9681da177e4SLinus Torvalds 	struct nfs_page		*req = data->req;
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	dprintk("NFS: write (%s/%Ld %d@%Ld)",
97188be9f99STrond Myklebust 		req->wb_context->path.dentry->d_inode->i_sb->s_id,
97288be9f99STrond Myklebust 		(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
9731da177e4SLinus Torvalds 		req->wb_bytes,
9741da177e4SLinus Torvalds 		(long long)req_offset(req));
9751da177e4SLinus Torvalds 
976c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
977c9d8f89dSTrond Myklebust }
978788e7a89STrond Myklebust 
979c9d8f89dSTrond Myklebust static void nfs_writeback_release_partial(void *calldata)
980c9d8f89dSTrond Myklebust {
981c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
982c9d8f89dSTrond Myklebust 	struct nfs_page		*req = data->req;
983c9d8f89dSTrond Myklebust 	struct page		*page = req->wb_page;
984c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
985c9d8f89dSTrond Myklebust 
986c9d8f89dSTrond Myklebust 	if (status < 0) {
987a301b777STrond Myklebust 		nfs_set_pageerror(page);
988c9d8f89dSTrond Myklebust 		nfs_context_set_write_error(req->wb_context, status);
989c9d8f89dSTrond Myklebust 		dprintk(", error = %d\n", status);
9908e821cadSTrond Myklebust 		goto out;
9918e821cadSTrond Myklebust 	}
9928e821cadSTrond Myklebust 
9938e821cadSTrond Myklebust 	if (nfs_write_need_commit(data)) {
994587142f8STrond Myklebust 		struct inode *inode = page->mapping->host;
9958e821cadSTrond Myklebust 
996587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
9978e821cadSTrond Myklebust 		if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
9988e821cadSTrond Myklebust 			/* Do nothing we need to resend the writes */
9998e821cadSTrond Myklebust 		} else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
10001da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
10011da177e4SLinus Torvalds 			dprintk(" defer commit\n");
10021da177e4SLinus Torvalds 		} else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
10038e821cadSTrond Myklebust 			set_bit(PG_NEED_RESCHED, &req->wb_flags);
10048e821cadSTrond Myklebust 			clear_bit(PG_NEED_COMMIT, &req->wb_flags);
10051da177e4SLinus Torvalds 			dprintk(" server reboot detected\n");
10061da177e4SLinus Torvalds 		}
1007587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
10081da177e4SLinus Torvalds 	} else
10091da177e4SLinus Torvalds 		dprintk(" OK\n");
10101da177e4SLinus Torvalds 
10118e821cadSTrond Myklebust out:
10121da177e4SLinus Torvalds 	if (atomic_dec_and_test(&req->wb_complete))
10131da177e4SLinus Torvalds 		nfs_writepage_release(req);
1014c9d8f89dSTrond Myklebust 	nfs_writedata_release(calldata);
10151da177e4SLinus Torvalds }
10161da177e4SLinus Torvalds 
1017788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops = {
1018788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_partial,
1019c9d8f89dSTrond Myklebust 	.rpc_release = nfs_writeback_release_partial,
1020788e7a89STrond Myklebust };
1021788e7a89STrond Myklebust 
10221da177e4SLinus Torvalds /*
10231da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
10241da177e4SLinus Torvalds  *
10251da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
10261da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
10271da177e4SLinus Torvalds  *	  as the page has a write request pending.
10281da177e4SLinus Torvalds  */
1029788e7a89STrond Myklebust static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
10301da177e4SLinus Torvalds {
1031788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
10321da177e4SLinus Torvalds 
1033c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
1034c9d8f89dSTrond Myklebust }
1035c9d8f89dSTrond Myklebust 
1036c9d8f89dSTrond Myklebust static void nfs_writeback_release_full(void *calldata)
1037c9d8f89dSTrond Myklebust {
1038c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1039c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1040788e7a89STrond Myklebust 
10411da177e4SLinus Torvalds 	/* Update attributes as result of writeback. */
10421da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
1043c9d8f89dSTrond Myklebust 		struct nfs_page *req = nfs_list_entry(data->pages.next);
1044c9d8f89dSTrond Myklebust 		struct page *page = req->wb_page;
1045c9d8f89dSTrond Myklebust 
10461da177e4SLinus Torvalds 		nfs_list_remove_request(req);
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds 		dprintk("NFS: write (%s/%Ld %d@%Ld)",
104988be9f99STrond Myklebust 			req->wb_context->path.dentry->d_inode->i_sb->s_id,
105088be9f99STrond Myklebust 			(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
10511da177e4SLinus Torvalds 			req->wb_bytes,
10521da177e4SLinus Torvalds 			(long long)req_offset(req));
10531da177e4SLinus Torvalds 
1054c9d8f89dSTrond Myklebust 		if (status < 0) {
1055a301b777STrond Myklebust 			nfs_set_pageerror(page);
1056c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
1057c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
10588e821cadSTrond Myklebust 			goto remove_request;
10591da177e4SLinus Torvalds 		}
10601da177e4SLinus Torvalds 
10618e821cadSTrond Myklebust 		if (nfs_write_need_commit(data)) {
10621da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
10631da177e4SLinus Torvalds 			nfs_mark_request_commit(req);
10648e821cadSTrond Myklebust 			nfs_end_page_writeback(page);
10651da177e4SLinus Torvalds 			dprintk(" marked for commit\n");
10668e821cadSTrond Myklebust 			goto next;
10678e821cadSTrond Myklebust 		}
10688e821cadSTrond Myklebust 		dprintk(" OK\n");
10698e821cadSTrond Myklebust remove_request:
10708e821cadSTrond Myklebust 		nfs_end_page_writeback(page);
10711da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
10721da177e4SLinus Torvalds 	next:
10739fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
10741da177e4SLinus Torvalds 	}
1075c9d8f89dSTrond Myklebust 	nfs_writedata_release(calldata);
10761da177e4SLinus Torvalds }
10771da177e4SLinus Torvalds 
1078788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops = {
1079788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_full,
1080c9d8f89dSTrond Myklebust 	.rpc_release = nfs_writeback_release_full,
1081788e7a89STrond Myklebust };
1082788e7a89STrond Myklebust 
1083788e7a89STrond Myklebust 
10841da177e4SLinus Torvalds /*
10851da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
10861da177e4SLinus Torvalds  */
1087462d5b32SChuck Lever int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
10881da177e4SLinus Torvalds {
10891da177e4SLinus Torvalds 	struct nfs_writeargs	*argp = &data->args;
10901da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
1091788e7a89STrond Myklebust 	int status;
10921da177e4SLinus Torvalds 
1093a3f565b1SChuck Lever 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
10941da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
10951da177e4SLinus Torvalds 
1096f551e44fSChuck Lever 	/*
1097f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1098f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1099f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1100f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1101f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1102f551e44fSChuck Lever 	 */
1103788e7a89STrond Myklebust 	status = NFS_PROTO(data->inode)->write_done(task, data);
1104788e7a89STrond Myklebust 	if (status != 0)
1105788e7a89STrond Myklebust 		return status;
110691d5b470SChuck Lever 	nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
110791d5b470SChuck Lever 
11081da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
11091da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
11101da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
11111da177e4SLinus Torvalds 		 * commit data to stable storage even though we
11121da177e4SLinus Torvalds 		 * requested it.
11131da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
11141da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
11151da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
11161da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
11171da177e4SLinus Torvalds 		 */
11181da177e4SLinus Torvalds 		static unsigned long    complain;
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
11211da177e4SLinus Torvalds 			dprintk("NFS: faulty NFS server %s:"
11221da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
112354ceac45SDavid Howells 				NFS_SERVER(data->inode)->nfs_client->cl_hostname,
11241da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
11251da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
11261da177e4SLinus Torvalds 		}
11271da177e4SLinus Torvalds 	}
11281da177e4SLinus Torvalds #endif
11291da177e4SLinus Torvalds 	/* Is this a short write? */
11301da177e4SLinus Torvalds 	if (task->tk_status >= 0 && resp->count < argp->count) {
11311da177e4SLinus Torvalds 		static unsigned long    complain;
11321da177e4SLinus Torvalds 
113391d5b470SChuck Lever 		nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
113491d5b470SChuck Lever 
11351da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
11361da177e4SLinus Torvalds 		if (resp->count != 0) {
11371da177e4SLinus Torvalds 			/* Was this an NFSv2 write or an NFSv3 stable write? */
11381da177e4SLinus Torvalds 			if (resp->verf->committed != NFS_UNSTABLE) {
11391da177e4SLinus Torvalds 				/* Resend from where the server left off */
11401da177e4SLinus Torvalds 				argp->offset += resp->count;
11411da177e4SLinus Torvalds 				argp->pgbase += resp->count;
11421da177e4SLinus Torvalds 				argp->count -= resp->count;
11431da177e4SLinus Torvalds 			} else {
11441da177e4SLinus Torvalds 				/* Resend as a stable write in order to avoid
11451da177e4SLinus Torvalds 				 * headaches in the case of a server crash.
11461da177e4SLinus Torvalds 				 */
11471da177e4SLinus Torvalds 				argp->stable = NFS_FILE_SYNC;
11481da177e4SLinus Torvalds 			}
11491da177e4SLinus Torvalds 			rpc_restart_call(task);
1150788e7a89STrond Myklebust 			return -EAGAIN;
11511da177e4SLinus Torvalds 		}
11521da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
11531da177e4SLinus Torvalds 			printk(KERN_WARNING
11541da177e4SLinus Torvalds 			       "NFS: Server wrote zero bytes, expected %u.\n",
11551da177e4SLinus Torvalds 					argp->count);
11561da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
11571da177e4SLinus Torvalds 		}
11581da177e4SLinus Torvalds 		/* Can't do anything about it except throw an error. */
11591da177e4SLinus Torvalds 		task->tk_status = -EIO;
11601da177e4SLinus Torvalds 	}
1161788e7a89STrond Myklebust 	return 0;
11621da177e4SLinus Torvalds }
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1166c9d8f89dSTrond Myklebust void nfs_commitdata_release(void *data)
11671da177e4SLinus Torvalds {
1168383ba719STrond Myklebust 	struct nfs_write_data *wdata = data;
1169383ba719STrond Myklebust 
1170383ba719STrond Myklebust 	put_nfs_open_context(wdata->args.context);
11711da177e4SLinus Torvalds 	nfs_commit_free(wdata);
11721da177e4SLinus Torvalds }
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds /*
11751da177e4SLinus Torvalds  * Set up the argument/result storage required for the RPC call.
11761da177e4SLinus Torvalds  */
1177dbae4c73STrond Myklebust static int nfs_commit_rpcsetup(struct list_head *head,
1178788e7a89STrond Myklebust 		struct nfs_write_data *data,
1179788e7a89STrond Myklebust 		int how)
11801da177e4SLinus Torvalds {
118184115e1cSTrond Myklebust 	struct nfs_page *first = nfs_list_entry(head->next);
118284115e1cSTrond Myklebust 	struct inode *inode = first->wb_context->path.dentry->d_inode;
118384115e1cSTrond Myklebust 	int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
11843ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
118507737691STrond Myklebust 	struct rpc_task *task;
1186bdc7f021STrond Myklebust 	struct rpc_message msg = {
1187bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1188bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
1189bdc7f021STrond Myklebust 		.rpc_cred = first->wb_context->cred,
1190bdc7f021STrond Myklebust 	};
119184115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
119207737691STrond Myklebust 		.task = &data->task,
119384115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(inode),
1194bdc7f021STrond Myklebust 		.rpc_message = &msg,
119584115e1cSTrond Myklebust 		.callback_ops = &nfs_commit_ops,
119684115e1cSTrond Myklebust 		.callback_data = data,
1197101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
119884115e1cSTrond Myklebust 		.flags = flags,
11993ff7576dSTrond Myklebust 		.priority = priority,
120084115e1cSTrond Myklebust 	};
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
12031da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds 	list_splice_init(head, &data->pages);
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds 	data->inode	  = inode;
1208bdc7f021STrond Myklebust 	data->cred	  = msg.rpc_cred;
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(data->inode);
12113da28eb1STrond Myklebust 	/* Note: we always request a commit of the entire inode */
12123da28eb1STrond Myklebust 	data->args.offset = 0;
12133da28eb1STrond Myklebust 	data->args.count  = 0;
1214383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(first->wb_context);
12153da28eb1STrond Myklebust 	data->res.count   = 0;
12161da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
12171da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
12180e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
12191da177e4SLinus Torvalds 
1220788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
1221bdc7f021STrond Myklebust 	NFS_PROTO(inode)->commit_setup(data, &msg);
12221da177e4SLinus Torvalds 
1223a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1224bdc7f021STrond Myklebust 
122507737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1226dbae4c73STrond Myklebust 	if (IS_ERR(task))
1227dbae4c73STrond Myklebust 		return PTR_ERR(task);
122807737691STrond Myklebust 	rpc_put_task(task);
1229dbae4c73STrond Myklebust 	return 0;
12301da177e4SLinus Torvalds }
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds /*
12331da177e4SLinus Torvalds  * Commit dirty pages
12341da177e4SLinus Torvalds  */
12351da177e4SLinus Torvalds static int
123640859d7eSChuck Lever nfs_commit_list(struct inode *inode, struct list_head *head, int how)
12371da177e4SLinus Torvalds {
12381da177e4SLinus Torvalds 	struct nfs_write_data	*data;
12391da177e4SLinus Torvalds 	struct nfs_page         *req;
12401da177e4SLinus Torvalds 
1241c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 	if (!data)
12441da177e4SLinus Torvalds 		goto out_bad;
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 	/* Set up the argument struct */
1247dbae4c73STrond Myklebust 	return nfs_commit_rpcsetup(head, data, how);
12481da177e4SLinus Torvalds  out_bad:
12491da177e4SLinus Torvalds 	while (!list_empty(head)) {
12501da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
12511da177e4SLinus Torvalds 		nfs_list_remove_request(req);
12521da177e4SLinus Torvalds 		nfs_mark_request_commit(req);
125383715ad5STrond Myklebust 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1254c9e51e41SPeter Zijlstra 		dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1255c9e51e41SPeter Zijlstra 				BDI_RECLAIMABLE);
12569fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
12571da177e4SLinus Torvalds 	}
12581da177e4SLinus Torvalds 	return -ENOMEM;
12591da177e4SLinus Torvalds }
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds /*
12621da177e4SLinus Torvalds  * COMMIT call returned
12631da177e4SLinus Torvalds  */
1264788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
12651da177e4SLinus Torvalds {
1266963d8fe5STrond Myklebust 	struct nfs_write_data	*data = calldata;
12671da177e4SLinus Torvalds 
1268a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
12691da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
12701da177e4SLinus Torvalds 
1271788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1272788e7a89STrond Myklebust 	if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1273788e7a89STrond Myklebust 		return;
1274c9d8f89dSTrond Myklebust }
1275c9d8f89dSTrond Myklebust 
1276c9d8f89dSTrond Myklebust static void nfs_commit_release(void *calldata)
1277c9d8f89dSTrond Myklebust {
1278c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1279c9d8f89dSTrond Myklebust 	struct nfs_page		*req;
1280c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1281788e7a89STrond Myklebust 
12821da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
12831da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
12841da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1285612c9384STrond Myklebust 		clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
1286fd39fc85SChristoph Lameter 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1287c9e51e41SPeter Zijlstra 		dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1288c9e51e41SPeter Zijlstra 				BDI_RECLAIMABLE);
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 		dprintk("NFS: commit (%s/%Ld %d@%Ld)",
129188be9f99STrond Myklebust 			req->wb_context->path.dentry->d_inode->i_sb->s_id,
129288be9f99STrond Myklebust 			(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
12931da177e4SLinus Torvalds 			req->wb_bytes,
12941da177e4SLinus Torvalds 			(long long)req_offset(req));
1295c9d8f89dSTrond Myklebust 		if (status < 0) {
1296c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
12971da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1298c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
12991da177e4SLinus Torvalds 			goto next;
13001da177e4SLinus Torvalds 		}
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
13031da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
13041da177e4SLinus Torvalds 		if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
13051da177e4SLinus Torvalds 			/* We have a match */
13061da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
13071da177e4SLinus Torvalds 			dprintk(" OK\n");
13081da177e4SLinus Torvalds 			goto next;
13091da177e4SLinus Torvalds 		}
13101da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
13111da177e4SLinus Torvalds 		dprintk(" mismatch\n");
13126d884e8fSFred 		nfs_mark_request_dirty(req);
13131da177e4SLinus Torvalds 	next:
13149fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
13151da177e4SLinus Torvalds 	}
1316c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
13171da177e4SLinus Torvalds }
1318788e7a89STrond Myklebust 
1319788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
1320788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1321788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1322788e7a89STrond Myklebust };
13231da177e4SLinus Torvalds 
13243da28eb1STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
13251da177e4SLinus Torvalds {
13261da177e4SLinus Torvalds 	LIST_HEAD(head);
13277d46a49fSTrond Myklebust 	int res;
13281da177e4SLinus Torvalds 
1329587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
13303da28eb1STrond Myklebust 	res = nfs_scan_commit(inode, &head, 0, 0);
1331587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
13321da177e4SLinus Torvalds 	if (res) {
13337d46a49fSTrond Myklebust 		int error = nfs_commit_list(inode, &head, how);
13341da177e4SLinus Torvalds 		if (error < 0)
13351da177e4SLinus Torvalds 			return error;
13363da28eb1STrond Myklebust 	}
13371da177e4SLinus Torvalds 	return res;
13381da177e4SLinus Torvalds }
1339c63c7b05STrond Myklebust #else
1340c63c7b05STrond Myklebust static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1341c63c7b05STrond Myklebust {
1342c63c7b05STrond Myklebust 	return 0;
1343c63c7b05STrond Myklebust }
13441da177e4SLinus Torvalds #endif
13451da177e4SLinus Torvalds 
13461c75950bSTrond Myklebust long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
13471da177e4SLinus Torvalds {
13481c75950bSTrond Myklebust 	struct inode *inode = mapping->host;
1349ca52fec1STrond Myklebust 	pgoff_t idx_start, idx_end;
13501c75950bSTrond Myklebust 	unsigned int npages = 0;
1351c42de9ddSTrond Myklebust 	LIST_HEAD(head);
135270b9ecbdSTrond Myklebust 	int nocommit = how & FLUSH_NOCOMMIT;
13533f442547STrond Myklebust 	long pages, ret;
13541da177e4SLinus Torvalds 
13551c75950bSTrond Myklebust 	/* FIXME */
13561c75950bSTrond Myklebust 	if (wbc->range_cyclic)
13571c75950bSTrond Myklebust 		idx_start = 0;
13581c75950bSTrond Myklebust 	else {
13591c75950bSTrond Myklebust 		idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
13601c75950bSTrond Myklebust 		idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
13611c75950bSTrond Myklebust 		if (idx_end > idx_start) {
1362ca52fec1STrond Myklebust 			pgoff_t l_npages = 1 + idx_end - idx_start;
13631c75950bSTrond Myklebust 			npages = l_npages;
13641c75950bSTrond Myklebust 			if (sizeof(npages) != sizeof(l_npages) &&
1365ca52fec1STrond Myklebust 					(pgoff_t)npages != l_npages)
13661c75950bSTrond Myklebust 				npages = 0;
13671c75950bSTrond Myklebust 		}
13681c75950bSTrond Myklebust 	}
1369c42de9ddSTrond Myklebust 	how &= ~FLUSH_NOCOMMIT;
1370587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
13711da177e4SLinus Torvalds 	do {
1372c42de9ddSTrond Myklebust 		ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1373c42de9ddSTrond Myklebust 		if (ret != 0)
1374c42de9ddSTrond Myklebust 			continue;
1375c42de9ddSTrond Myklebust 		if (nocommit)
1376c42de9ddSTrond Myklebust 			break;
1377d2ccddf0STrond Myklebust 		pages = nfs_scan_commit(inode, &head, idx_start, npages);
1378724c439cSTrond Myklebust 		if (pages == 0)
1379c42de9ddSTrond Myklebust 			break;
1380d2ccddf0STrond Myklebust 		if (how & FLUSH_INVALIDATE) {
1381587142f8STrond Myklebust 			spin_unlock(&inode->i_lock);
138283715ad5STrond Myklebust 			nfs_cancel_commit_list(&head);
1383e8e058e8STrond Myklebust 			ret = pages;
1384587142f8STrond Myklebust 			spin_lock(&inode->i_lock);
1385d2ccddf0STrond Myklebust 			continue;
1386d2ccddf0STrond Myklebust 		}
1387d2ccddf0STrond Myklebust 		pages += nfs_scan_commit(inode, &head, 0, 0);
1388587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
1389c42de9ddSTrond Myklebust 		ret = nfs_commit_list(inode, &head, how);
1390587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
1391587142f8STrond Myklebust 
1392c42de9ddSTrond Myklebust 	} while (ret >= 0);
1393587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
1394c42de9ddSTrond Myklebust 	return ret;
13951da177e4SLinus Torvalds }
13961da177e4SLinus Torvalds 
139734901f70STrond Myklebust static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
13981c75950bSTrond Myklebust {
13991c75950bSTrond Myklebust 	int ret;
14001c75950bSTrond Myklebust 
140134901f70STrond Myklebust 	ret = nfs_writepages(mapping, wbc);
140261822ab5STrond Myklebust 	if (ret < 0)
140361822ab5STrond Myklebust 		goto out;
140434901f70STrond Myklebust 	ret = nfs_sync_mapping_wait(mapping, wbc, how);
1405ed90ef51STrond Myklebust 	if (ret < 0)
1406ed90ef51STrond Myklebust 		goto out;
14071c75950bSTrond Myklebust 	return 0;
140861822ab5STrond Myklebust out:
1409e507d9ebSTrond Myklebust 	__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
14101c75950bSTrond Myklebust 	return ret;
14111c75950bSTrond Myklebust }
14121c75950bSTrond Myklebust 
141334901f70STrond Myklebust /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
141434901f70STrond Myklebust static int nfs_write_mapping(struct address_space *mapping, int how)
141534901f70STrond Myklebust {
141634901f70STrond Myklebust 	struct writeback_control wbc = {
141734901f70STrond Myklebust 		.bdi = mapping->backing_dev_info,
141834901f70STrond Myklebust 		.sync_mode = WB_SYNC_NONE,
141934901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
142034901f70STrond Myklebust 		.for_writepages = 1,
142134901f70STrond Myklebust 		.range_cyclic = 1,
142234901f70STrond Myklebust 	};
142334901f70STrond Myklebust 	int ret;
142434901f70STrond Myklebust 
142534901f70STrond Myklebust 	ret = __nfs_write_mapping(mapping, &wbc, how);
142634901f70STrond Myklebust 	if (ret < 0)
142734901f70STrond Myklebust 		return ret;
142834901f70STrond Myklebust 	wbc.sync_mode = WB_SYNC_ALL;
142934901f70STrond Myklebust 	return __nfs_write_mapping(mapping, &wbc, how);
143034901f70STrond Myklebust }
143134901f70STrond Myklebust 
1432ed90ef51STrond Myklebust /*
1433ed90ef51STrond Myklebust  * flush the inode to disk.
1434ed90ef51STrond Myklebust  */
1435ed90ef51STrond Myklebust int nfs_wb_all(struct inode *inode)
14361c75950bSTrond Myklebust {
1437ed90ef51STrond Myklebust 	return nfs_write_mapping(inode->i_mapping, 0);
1438ed90ef51STrond Myklebust }
14391c75950bSTrond Myklebust 
1440ed90ef51STrond Myklebust int nfs_wb_nocommit(struct inode *inode)
1441ed90ef51STrond Myklebust {
1442ed90ef51STrond Myklebust 	return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
14431c75950bSTrond Myklebust }
14441c75950bSTrond Myklebust 
14451b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
14461b3b4a1aSTrond Myklebust {
14471b3b4a1aSTrond Myklebust 	struct nfs_page *req;
14481b3b4a1aSTrond Myklebust 	loff_t range_start = page_offset(page);
14491b3b4a1aSTrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
14501b3b4a1aSTrond Myklebust 	struct writeback_control wbc = {
14511b3b4a1aSTrond Myklebust 		.bdi = page->mapping->backing_dev_info,
14521b3b4a1aSTrond Myklebust 		.sync_mode = WB_SYNC_ALL,
14531b3b4a1aSTrond Myklebust 		.nr_to_write = LONG_MAX,
14541b3b4a1aSTrond Myklebust 		.range_start = range_start,
14551b3b4a1aSTrond Myklebust 		.range_end = range_end,
14561b3b4a1aSTrond Myklebust 	};
14571b3b4a1aSTrond Myklebust 	int ret = 0;
14581b3b4a1aSTrond Myklebust 
14591b3b4a1aSTrond Myklebust 	BUG_ON(!PageLocked(page));
14601b3b4a1aSTrond Myklebust 	for (;;) {
14611b3b4a1aSTrond Myklebust 		req = nfs_page_find_request(page);
14621b3b4a1aSTrond Myklebust 		if (req == NULL)
14631b3b4a1aSTrond Myklebust 			goto out;
14641b3b4a1aSTrond Myklebust 		if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
14651b3b4a1aSTrond Myklebust 			nfs_release_request(req);
14661b3b4a1aSTrond Myklebust 			break;
14671b3b4a1aSTrond Myklebust 		}
14681b3b4a1aSTrond Myklebust 		if (nfs_lock_request_dontget(req)) {
14691b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
14701b3b4a1aSTrond Myklebust 			/*
14711b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
14721b3b4a1aSTrond Myklebust 			 * page as being dirty
14731b3b4a1aSTrond Myklebust 			 */
14741b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
14751b3b4a1aSTrond Myklebust 			nfs_unlock_request(req);
14761b3b4a1aSTrond Myklebust 			break;
14771b3b4a1aSTrond Myklebust 		}
14781b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
14791b3b4a1aSTrond Myklebust 		if (ret < 0)
14801b3b4a1aSTrond Myklebust 			goto out;
14811b3b4a1aSTrond Myklebust 	}
14821b3b4a1aSTrond Myklebust 	if (!PagePrivate(page))
14831b3b4a1aSTrond Myklebust 		return 0;
14841b3b4a1aSTrond Myklebust 	ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
14851b3b4a1aSTrond Myklebust out:
14861b3b4a1aSTrond Myklebust 	return ret;
14871b3b4a1aSTrond Myklebust }
14881b3b4a1aSTrond Myklebust 
14895334eb13SAdrian Bunk static int nfs_wb_page_priority(struct inode *inode, struct page *page,
14905334eb13SAdrian Bunk 				int how)
14911c75950bSTrond Myklebust {
14921c75950bSTrond Myklebust 	loff_t range_start = page_offset(page);
14931c75950bSTrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
14944d770ccfSTrond Myklebust 	struct writeback_control wbc = {
14954d770ccfSTrond Myklebust 		.bdi = page->mapping->backing_dev_info,
14964d770ccfSTrond Myklebust 		.sync_mode = WB_SYNC_ALL,
14974d770ccfSTrond Myklebust 		.nr_to_write = LONG_MAX,
14984d770ccfSTrond Myklebust 		.range_start = range_start,
14994d770ccfSTrond Myklebust 		.range_end = range_end,
15004d770ccfSTrond Myklebust 	};
15014d770ccfSTrond Myklebust 	int ret;
15021c75950bSTrond Myklebust 
150373e3302fSTrond Myklebust 	do {
1504c63c7b05STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
15054d770ccfSTrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
15064d770ccfSTrond Myklebust 			if (ret < 0)
150773e3302fSTrond Myklebust 				goto out_error;
150873e3302fSTrond Myklebust 		} else if (!PagePrivate(page))
150973e3302fSTrond Myklebust 			break;
15104d770ccfSTrond Myklebust 		ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
151173e3302fSTrond Myklebust 		if (ret < 0)
151273e3302fSTrond Myklebust 			goto out_error;
151373e3302fSTrond Myklebust 	} while (PagePrivate(page));
15144d770ccfSTrond Myklebust 	return 0;
151573e3302fSTrond Myklebust out_error:
1516e507d9ebSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_PAGES);
15174d770ccfSTrond Myklebust 	return ret;
15181c75950bSTrond Myklebust }
15191c75950bSTrond Myklebust 
15201c75950bSTrond Myklebust /*
15211c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
15221c75950bSTrond Myklebust  */
15231c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page* page)
15241c75950bSTrond Myklebust {
15254d770ccfSTrond Myklebust 	return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
15261c75950bSTrond Myklebust }
15271c75950bSTrond Myklebust 
1528f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
15291da177e4SLinus Torvalds {
15301da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
15311da177e4SLinus Torvalds 					     sizeof(struct nfs_write_data),
15321da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
153320c2df83SPaul Mundt 					     NULL);
15341da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
15351da177e4SLinus Torvalds 		return -ENOMEM;
15361da177e4SLinus Torvalds 
153793d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
15381da177e4SLinus Torvalds 						     nfs_wdata_cachep);
15391da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
15401da177e4SLinus Torvalds 		return -ENOMEM;
15411da177e4SLinus Torvalds 
154293d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
15431da177e4SLinus Torvalds 						      nfs_wdata_cachep);
15441da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
15451da177e4SLinus Torvalds 		return -ENOMEM;
15461da177e4SLinus Torvalds 
154789a09141SPeter Zijlstra 	/*
154889a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
154989a09141SPeter Zijlstra 	 *
155089a09141SPeter Zijlstra 	 *  64MB:    8192k
155189a09141SPeter Zijlstra 	 * 128MB:   11585k
155289a09141SPeter Zijlstra 	 * 256MB:   16384k
155389a09141SPeter Zijlstra 	 * 512MB:   23170k
155489a09141SPeter Zijlstra 	 *   1GB:   32768k
155589a09141SPeter Zijlstra 	 *   2GB:   46340k
155689a09141SPeter Zijlstra 	 *   4GB:   65536k
155789a09141SPeter Zijlstra 	 *   8GB:   92681k
155889a09141SPeter Zijlstra 	 *  16GB:  131072k
155989a09141SPeter Zijlstra 	 *
156089a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
156189a09141SPeter Zijlstra 	 * Limit the default to 256M
156289a09141SPeter Zijlstra 	 */
156389a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
156489a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
156589a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
156689a09141SPeter Zijlstra 
15671da177e4SLinus Torvalds 	return 0;
15681da177e4SLinus Torvalds }
15691da177e4SLinus Torvalds 
1570266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
15711da177e4SLinus Torvalds {
15721da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
15731da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
15741a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
15751da177e4SLinus Torvalds }
15761da177e4SLinus Torvalds 
1577