xref: /linux/fs/nfs/write.c (revision 5e4424af9a1f062c6451681dff24a26e27741cc6)
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);
42788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops;
43788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops;
44788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops;
451da177e4SLinus Torvalds 
46e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep;
473feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
481da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
491da177e4SLinus Torvalds 
50e9f7bee1STrond Myklebust struct nfs_write_data *nfs_commit_alloc(void)
511da177e4SLinus Torvalds {
52e6b4f8daSChristoph Lameter 	struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
5340859d7eSChuck Lever 
541da177e4SLinus Torvalds 	if (p) {
551da177e4SLinus Torvalds 		memset(p, 0, sizeof(*p));
561da177e4SLinus Torvalds 		INIT_LIST_HEAD(&p->pages);
571da177e4SLinus Torvalds 	}
581da177e4SLinus Torvalds 	return p;
591da177e4SLinus Torvalds }
601da177e4SLinus Torvalds 
61*5e4424afSTrond Myklebust void nfs_commit_free(struct nfs_write_data *p)
621da177e4SLinus Torvalds {
6340859d7eSChuck Lever 	if (p && (p->pagevec != &p->page_array[0]))
6440859d7eSChuck Lever 		kfree(p->pagevec);
651da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
661da177e4SLinus Torvalds }
671da177e4SLinus Torvalds 
688d5658c9STrond Myklebust struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
693feb2d49STrond Myklebust {
70e6b4f8daSChristoph Lameter 	struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
713feb2d49STrond Myklebust 
723feb2d49STrond Myklebust 	if (p) {
733feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
743feb2d49STrond Myklebust 		INIT_LIST_HEAD(&p->pages);
75e9f7bee1STrond Myklebust 		p->npages = pagecount;
760d0b5cb3SChuck Lever 		if (pagecount <= ARRAY_SIZE(p->page_array))
770d0b5cb3SChuck Lever 			p->pagevec = p->page_array;
783feb2d49STrond Myklebust 		else {
790d0b5cb3SChuck Lever 			p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
800d0b5cb3SChuck Lever 			if (!p->pagevec) {
813feb2d49STrond Myklebust 				mempool_free(p, nfs_wdata_mempool);
823feb2d49STrond Myklebust 				p = NULL;
833feb2d49STrond Myklebust 			}
843feb2d49STrond Myklebust 		}
853feb2d49STrond Myklebust 	}
863feb2d49STrond Myklebust 	return p;
873feb2d49STrond Myklebust }
883feb2d49STrond Myklebust 
89*5e4424afSTrond Myklebust static void nfs_writedata_free(struct nfs_write_data *p)
903feb2d49STrond Myklebust {
913feb2d49STrond Myklebust 	if (p && (p->pagevec != &p->page_array[0]))
923feb2d49STrond Myklebust 		kfree(p->pagevec);
933feb2d49STrond Myklebust 	mempool_free(p, nfs_wdata_mempool);
943feb2d49STrond Myklebust }
953feb2d49STrond Myklebust 
96383ba719STrond Myklebust void nfs_writedata_release(void *data)
971da177e4SLinus Torvalds {
98383ba719STrond Myklebust 	struct nfs_write_data *wdata = data;
99383ba719STrond Myklebust 
100383ba719STrond Myklebust 	put_nfs_open_context(wdata->args.context);
1011da177e4SLinus Torvalds 	nfs_writedata_free(wdata);
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
1047b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
1057b159fc1STrond Myklebust {
1067b159fc1STrond Myklebust 	ctx->error = error;
1077b159fc1STrond Myklebust 	smp_wmb();
1087b159fc1STrond Myklebust 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
1097b159fc1STrond Myklebust }
1107b159fc1STrond Myklebust 
111277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request_locked(struct page *page)
112277459d2STrond Myklebust {
113277459d2STrond Myklebust 	struct nfs_page *req = NULL;
114277459d2STrond Myklebust 
115277459d2STrond Myklebust 	if (PagePrivate(page)) {
116277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
117277459d2STrond Myklebust 		if (req != NULL)
118c03b4024STrond Myklebust 			kref_get(&req->wb_kref);
119277459d2STrond Myklebust 	}
120277459d2STrond Myklebust 	return req;
121277459d2STrond Myklebust }
122277459d2STrond Myklebust 
123277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page)
124277459d2STrond Myklebust {
125587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
126277459d2STrond Myklebust 	struct nfs_page *req = NULL;
127277459d2STrond Myklebust 
128587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
129277459d2STrond Myklebust 	req = nfs_page_find_request_locked(page);
130587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
131277459d2STrond Myklebust 	return req;
132277459d2STrond Myklebust }
133277459d2STrond Myklebust 
1341da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1351da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1361da177e4SLinus Torvalds {
1371da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
1381da177e4SLinus Torvalds 	loff_t end, i_size = i_size_read(inode);
139ca52fec1STrond Myklebust 	pgoff_t end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 	if (i_size > 0 && page->index < end_index)
1421da177e4SLinus Torvalds 		return;
1431da177e4SLinus Torvalds 	end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
1441da177e4SLinus Torvalds 	if (i_size >= end)
1451da177e4SLinus Torvalds 		return;
14691d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
1471da177e4SLinus Torvalds 	i_size_write(inode, end);
1481da177e4SLinus Torvalds }
1491da177e4SLinus Torvalds 
150a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
151a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
152a301b777STrond Myklebust {
153a301b777STrond Myklebust 	SetPageError(page);
154a301b777STrond Myklebust 	nfs_zap_mapping(page->mapping->host, page->mapping);
155a301b777STrond Myklebust }
156a301b777STrond Myklebust 
1571da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
1581da177e4SLinus Torvalds  * covers the full page.
1591da177e4SLinus Torvalds  */
1601da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
1611da177e4SLinus Torvalds {
1621da177e4SLinus Torvalds 	if (PageUptodate(page))
1631da177e4SLinus Torvalds 		return;
1641da177e4SLinus Torvalds 	if (base != 0)
1651da177e4SLinus Torvalds 		return;
16649a70f27STrond Myklebust 	if (count != nfs_page_length(page))
1671da177e4SLinus Torvalds 		return;
1681da177e4SLinus Torvalds 	SetPageUptodate(page);
1691da177e4SLinus Torvalds }
1701da177e4SLinus Torvalds 
171e21195a7STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
1721da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
1731da177e4SLinus Torvalds {
1741da177e4SLinus Torvalds 	struct nfs_page	*req;
175e21195a7STrond Myklebust 	int ret;
1761da177e4SLinus Torvalds 
177e21195a7STrond Myklebust 	for (;;) {
178e21195a7STrond Myklebust 		req = nfs_update_request(ctx, page, offset, count);
179e21195a7STrond Myklebust 		if (!IS_ERR(req))
180e21195a7STrond Myklebust 			break;
181e21195a7STrond Myklebust 		ret = PTR_ERR(req);
182e21195a7STrond Myklebust 		if (ret != -EBUSY)
183e21195a7STrond Myklebust 			return ret;
184e21195a7STrond Myklebust 		ret = nfs_wb_page(page->mapping->host, page);
185e21195a7STrond Myklebust 		if (ret != 0)
186e21195a7STrond Myklebust 			return ret;
187e21195a7STrond Myklebust 	}
1881da177e4SLinus Torvalds 	/* Update file length */
1891da177e4SLinus Torvalds 	nfs_grow_file(page, offset, count);
190acee478aSTrond Myklebust 	nfs_clear_page_tag_locked(req);
191abd3e641STrond Myklebust 	return 0;
1921da177e4SLinus Torvalds }
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
1951da177e4SLinus Torvalds {
1961da177e4SLinus Torvalds 	if (wbc->for_reclaim)
197c63c7b05STrond Myklebust 		return FLUSH_HIGHPRI | FLUSH_STABLE;
1981da177e4SLinus Torvalds 	if (wbc->for_kupdate)
1991da177e4SLinus Torvalds 		return FLUSH_LOWPRI;
2001da177e4SLinus Torvalds 	return 0;
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds /*
20489a09141SPeter Zijlstra  * NFS congestion control
20589a09141SPeter Zijlstra  */
20689a09141SPeter Zijlstra 
20789a09141SPeter Zijlstra int nfs_congestion_kb;
20889a09141SPeter Zijlstra 
20989a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
21089a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
21189a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
21289a09141SPeter Zijlstra 
2135a6d41b3STrond Myklebust static int nfs_set_page_writeback(struct page *page)
21489a09141SPeter Zijlstra {
2155a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
2165a6d41b3STrond Myklebust 
2175a6d41b3STrond Myklebust 	if (!ret) {
21889a09141SPeter Zijlstra 		struct inode *inode = page->mapping->host;
21989a09141SPeter Zijlstra 		struct nfs_server *nfss = NFS_SERVER(inode);
22089a09141SPeter Zijlstra 
221277866a0SPeter Zijlstra 		if (atomic_long_inc_return(&nfss->writeback) >
22289a09141SPeter Zijlstra 				NFS_CONGESTION_ON_THRESH)
22389a09141SPeter Zijlstra 			set_bdi_congested(&nfss->backing_dev_info, WRITE);
22489a09141SPeter Zijlstra 	}
2255a6d41b3STrond Myklebust 	return ret;
22689a09141SPeter Zijlstra }
22789a09141SPeter Zijlstra 
22889a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page)
22989a09141SPeter Zijlstra {
23089a09141SPeter Zijlstra 	struct inode *inode = page->mapping->host;
23189a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
23289a09141SPeter Zijlstra 
23389a09141SPeter Zijlstra 	end_page_writeback(page);
234c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
23589a09141SPeter Zijlstra 		clear_bdi_congested(&nfss->backing_dev_info, WRITE);
23689a09141SPeter Zijlstra }
23789a09141SPeter Zijlstra 
23889a09141SPeter Zijlstra /*
239e261f51fSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
2409cccef95STrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
241e261f51fSTrond Myklebust  */
242c63c7b05STrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
243c63c7b05STrond Myklebust 				struct page *page)
244e261f51fSTrond Myklebust {
245587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
246e261f51fSTrond Myklebust 	struct nfs_page *req;
247e261f51fSTrond Myklebust 	int ret;
248e261f51fSTrond Myklebust 
249587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
250e261f51fSTrond Myklebust 	for(;;) {
251e261f51fSTrond Myklebust 		req = nfs_page_find_request_locked(page);
252e261f51fSTrond Myklebust 		if (req == NULL) {
253587142f8STrond Myklebust 			spin_unlock(&inode->i_lock);
2549cccef95STrond Myklebust 			return 0;
255e261f51fSTrond Myklebust 		}
256acee478aSTrond Myklebust 		if (nfs_set_page_tag_locked(req))
257e261f51fSTrond Myklebust 			break;
258e261f51fSTrond Myklebust 		/* Note: If we hold the page lock, as is the case in nfs_writepage,
259acee478aSTrond Myklebust 		 *	 then the call to nfs_set_page_tag_locked() will always
260e261f51fSTrond Myklebust 		 *	 succeed provided that someone hasn't already marked the
261e261f51fSTrond Myklebust 		 *	 request as dirty (in which case we don't care).
262e261f51fSTrond Myklebust 		 */
263587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
264e261f51fSTrond Myklebust 		ret = nfs_wait_on_request(req);
265e261f51fSTrond Myklebust 		nfs_release_request(req);
266e261f51fSTrond Myklebust 		if (ret != 0)
267e261f51fSTrond Myklebust 			return ret;
268587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
269e261f51fSTrond Myklebust 	}
270612c9384STrond Myklebust 	if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
271612c9384STrond Myklebust 		/* This request is marked for commit */
272587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
273acee478aSTrond Myklebust 		nfs_clear_page_tag_locked(req);
274c63c7b05STrond Myklebust 		nfs_pageio_complete(pgio);
2759cccef95STrond Myklebust 		return 0;
276612c9384STrond Myklebust 	}
277c63c7b05STrond Myklebust 	if (nfs_set_page_writeback(page) != 0) {
278587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
279c63c7b05STrond Myklebust 		BUG();
280c63c7b05STrond Myklebust 	}
281587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
282c63c7b05STrond Myklebust 	nfs_pageio_add_request(pgio, req);
2839cccef95STrond Myklebust 	return 0;
284e261f51fSTrond Myklebust }
285e261f51fSTrond Myklebust 
286f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
287f758c885STrond Myklebust {
288f758c885STrond Myklebust 	struct inode *inode = page->mapping->host;
289f758c885STrond Myklebust 
290f758c885STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
291f758c885STrond Myklebust 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
292f758c885STrond Myklebust 
293f758c885STrond Myklebust 	nfs_pageio_cond_complete(pgio, page->index);
294f758c885STrond Myklebust 	return nfs_page_async_flush(pgio, page);
295f758c885STrond Myklebust }
296f758c885STrond Myklebust 
297e261f51fSTrond Myklebust /*
2981da177e4SLinus Torvalds  * Write an mmapped page to the server.
2991da177e4SLinus Torvalds  */
3004d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
3011da177e4SLinus Torvalds {
302f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
303e261f51fSTrond Myklebust 	int err;
3041da177e4SLinus Torvalds 
305f758c885STrond Myklebust 	nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
306f758c885STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
307f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
308f758c885STrond Myklebust 	if (err < 0)
3094d770ccfSTrond Myklebust 		return err;
310f758c885STrond Myklebust 	if (pgio.pg_error < 0)
311f758c885STrond Myklebust 		return pgio.pg_error;
312f758c885STrond Myklebust 	return 0;
3134d770ccfSTrond Myklebust }
3144d770ccfSTrond Myklebust 
3154d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
3164d770ccfSTrond Myklebust {
317f758c885STrond Myklebust 	int ret;
3184d770ccfSTrond Myklebust 
319f758c885STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
3201da177e4SLinus Torvalds 	unlock_page(page);
321f758c885STrond Myklebust 	return ret;
322f758c885STrond Myklebust }
323f758c885STrond Myklebust 
324f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
325f758c885STrond Myklebust {
326f758c885STrond Myklebust 	int ret;
327f758c885STrond Myklebust 
328f758c885STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
329f758c885STrond Myklebust 	unlock_page(page);
330f758c885STrond Myklebust 	return ret;
3311da177e4SLinus Torvalds }
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3341da177e4SLinus Torvalds {
3351da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
336c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
3371da177e4SLinus Torvalds 	int err;
3381da177e4SLinus Torvalds 
33991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
34091d5b470SChuck Lever 
341c63c7b05STrond Myklebust 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
342f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
343c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
344f758c885STrond Myklebust 	if (err < 0)
3451da177e4SLinus Torvalds 		return err;
346f758c885STrond Myklebust 	if (pgio.pg_error < 0)
347c63c7b05STrond Myklebust 		return pgio.pg_error;
348c63c7b05STrond Myklebust 	return 0;
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds /*
3521da177e4SLinus Torvalds  * Insert a write request into an inode
3531da177e4SLinus Torvalds  */
3541da177e4SLinus Torvalds static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
3551da177e4SLinus Torvalds {
3561da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
3571da177e4SLinus Torvalds 	int error;
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
3601da177e4SLinus Torvalds 	BUG_ON(error == -EEXIST);
3611da177e4SLinus Torvalds 	if (error)
3621da177e4SLinus Torvalds 		return error;
3631da177e4SLinus Torvalds 	if (!nfsi->npages) {
3641da177e4SLinus Torvalds 		igrab(inode);
3651da177e4SLinus Torvalds 		if (nfs_have_delegation(inode, FMODE_WRITE))
3661da177e4SLinus Torvalds 			nfsi->change_attr++;
3671da177e4SLinus Torvalds 	}
368deb7d638STrond Myklebust 	SetPagePrivate(req->wb_page);
369277459d2STrond Myklebust 	set_page_private(req->wb_page, (unsigned long)req);
3701da177e4SLinus Torvalds 	nfsi->npages++;
371c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
372acee478aSTrond Myklebust 	radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_LOCKED);
3731da177e4SLinus Torvalds 	return 0;
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds /*
37789a09141SPeter Zijlstra  * Remove a write request from an inode
3781da177e4SLinus Torvalds  */
3791da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
3801da177e4SLinus Torvalds {
38188be9f99STrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
3821da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	BUG_ON (!NFS_WBACK_BUSY(req));
3851da177e4SLinus Torvalds 
386587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
387277459d2STrond Myklebust 	set_page_private(req->wb_page, 0);
388deb7d638STrond Myklebust 	ClearPagePrivate(req->wb_page);
3891da177e4SLinus Torvalds 	radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
3901da177e4SLinus Torvalds 	nfsi->npages--;
3911da177e4SLinus Torvalds 	if (!nfsi->npages) {
392587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
3931da177e4SLinus Torvalds 		iput(inode);
3941da177e4SLinus Torvalds 	} else
395587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
3961da177e4SLinus Torvalds 	nfs_clear_request(req);
3971da177e4SLinus Torvalds 	nfs_release_request(req);
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds 
40061822ab5STrond Myklebust static void
40161822ab5STrond Myklebust nfs_redirty_request(struct nfs_page *req)
40261822ab5STrond Myklebust {
40361822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
40461822ab5STrond Myklebust }
40561822ab5STrond Myklebust 
4061da177e4SLinus Torvalds /*
4071da177e4SLinus Torvalds  * Check if a request is dirty
4081da177e4SLinus Torvalds  */
4091da177e4SLinus Torvalds static inline int
4101da177e4SLinus Torvalds nfs_dirty_request(struct nfs_page *req)
4111da177e4SLinus Torvalds {
4125a6d41b3STrond Myklebust 	struct page *page = req->wb_page;
4135a6d41b3STrond Myklebust 
414612c9384STrond Myklebust 	if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
4155a6d41b3STrond Myklebust 		return 0;
4165a6d41b3STrond Myklebust 	return !PageWriteback(req->wb_page);
4171da177e4SLinus Torvalds }
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
4201da177e4SLinus Torvalds /*
4211da177e4SLinus Torvalds  * Add a request to the inode's commit list.
4221da177e4SLinus Torvalds  */
4231da177e4SLinus Torvalds static void
4241da177e4SLinus Torvalds nfs_mark_request_commit(struct nfs_page *req)
4251da177e4SLinus Torvalds {
42688be9f99STrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
4271da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4281da177e4SLinus Torvalds 
429587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
4301da177e4SLinus Torvalds 	nfsi->ncommit++;
431612c9384STrond Myklebust 	set_bit(PG_NEED_COMMIT, &(req)->wb_flags);
4325c369683STrond Myklebust 	radix_tree_tag_set(&nfsi->nfs_page_tree,
4335c369683STrond Myklebust 			req->wb_index,
4345c369683STrond Myklebust 			NFS_PAGE_TAG_COMMIT);
435587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
436fd39fc85SChristoph Lameter 	inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
437c9e51e41SPeter Zijlstra 	inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
438a1803044STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
4391da177e4SLinus Torvalds }
4408e821cadSTrond Myklebust 
4418e821cadSTrond Myklebust static inline
4428e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
4438e821cadSTrond Myklebust {
4448e821cadSTrond Myklebust 	return data->verf.committed != NFS_FILE_SYNC;
4458e821cadSTrond Myklebust }
4468e821cadSTrond Myklebust 
4478e821cadSTrond Myklebust static inline
4488e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req)
4498e821cadSTrond Myklebust {
450612c9384STrond Myklebust 	if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
4518e821cadSTrond Myklebust 		nfs_mark_request_commit(req);
4528e821cadSTrond Myklebust 		return 1;
4538e821cadSTrond Myklebust 	}
4548e821cadSTrond Myklebust 	if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
4558e821cadSTrond Myklebust 		nfs_redirty_request(req);
4568e821cadSTrond Myklebust 		return 1;
4578e821cadSTrond Myklebust 	}
4588e821cadSTrond Myklebust 	return 0;
4598e821cadSTrond Myklebust }
4608e821cadSTrond Myklebust #else
4618e821cadSTrond Myklebust static inline void
4628e821cadSTrond Myklebust nfs_mark_request_commit(struct nfs_page *req)
4638e821cadSTrond Myklebust {
4648e821cadSTrond Myklebust }
4658e821cadSTrond Myklebust 
4668e821cadSTrond Myklebust static inline
4678e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
4688e821cadSTrond Myklebust {
4698e821cadSTrond Myklebust 	return 0;
4708e821cadSTrond Myklebust }
4718e821cadSTrond Myklebust 
4728e821cadSTrond Myklebust static inline
4738e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req)
4748e821cadSTrond Myklebust {
4758e821cadSTrond Myklebust 	return 0;
4768e821cadSTrond Myklebust }
4771da177e4SLinus Torvalds #endif
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds /*
4801da177e4SLinus Torvalds  * Wait for a request to complete.
4811da177e4SLinus Torvalds  *
482150030b7SMatthew Wilcox  * Interruptible by fatal signals only.
4831da177e4SLinus Torvalds  */
484ca52fec1STrond Myklebust static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
4851da177e4SLinus Torvalds {
4861da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4871da177e4SLinus Torvalds 	struct nfs_page *req;
488ca52fec1STrond Myklebust 	pgoff_t idx_end, next;
4891da177e4SLinus Torvalds 	unsigned int		res = 0;
4901da177e4SLinus Torvalds 	int			error;
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	if (npages == 0)
4931da177e4SLinus Torvalds 		idx_end = ~0;
4941da177e4SLinus Torvalds 	else
4951da177e4SLinus Torvalds 		idx_end = idx_start + npages - 1;
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	next = idx_start;
4989fd367f0STrond Myklebust 	while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) {
4991da177e4SLinus Torvalds 		if (req->wb_index > idx_end)
5001da177e4SLinus Torvalds 			break;
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 		next = req->wb_index + 1;
503c6a556b8STrond Myklebust 		BUG_ON(!NFS_WBACK_BUSY(req));
5041da177e4SLinus Torvalds 
505c03b4024STrond Myklebust 		kref_get(&req->wb_kref);
506587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
5071da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
5081da177e4SLinus Torvalds 		nfs_release_request(req);
509587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
5101da177e4SLinus Torvalds 		if (error < 0)
5111da177e4SLinus Torvalds 			return error;
5121da177e4SLinus Torvalds 		res++;
5131da177e4SLinus Torvalds 	}
5141da177e4SLinus Torvalds 	return res;
5151da177e4SLinus Torvalds }
5161da177e4SLinus Torvalds 
51783715ad5STrond Myklebust static void nfs_cancel_commit_list(struct list_head *head)
51883715ad5STrond Myklebust {
51983715ad5STrond Myklebust 	struct nfs_page *req;
52083715ad5STrond Myklebust 
52183715ad5STrond Myklebust 	while(!list_empty(head)) {
52283715ad5STrond Myklebust 		req = nfs_list_entry(head->next);
523b6dff26aSTrond Myklebust 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
524c9e51e41SPeter Zijlstra 		dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
525c9e51e41SPeter Zijlstra 				BDI_RECLAIMABLE);
52683715ad5STrond Myklebust 		nfs_list_remove_request(req);
527612c9384STrond Myklebust 		clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
52883715ad5STrond Myklebust 		nfs_inode_remove_request(req);
529b6dff26aSTrond Myklebust 		nfs_unlock_request(req);
53083715ad5STrond Myklebust 	}
53183715ad5STrond Myklebust }
53283715ad5STrond Myklebust 
5331da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
5341da177e4SLinus Torvalds /*
5351da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
5361da177e4SLinus Torvalds  * @inode: NFS inode to scan
5371da177e4SLinus Torvalds  * @dst: destination list
5381da177e4SLinus Torvalds  * @idx_start: lower bound of page->index to scan.
5391da177e4SLinus Torvalds  * @npages: idx_start + npages sets the upper bound to scan.
5401da177e4SLinus Torvalds  *
5411da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
5421da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
5431da177e4SLinus Torvalds  */
5441da177e4SLinus Torvalds static int
545ca52fec1STrond Myklebust nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
5461da177e4SLinus Torvalds {
5471da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
5483da28eb1STrond Myklebust 	int res = 0;
5493da28eb1STrond Myklebust 
5503da28eb1STrond Myklebust 	if (nfsi->ncommit != 0) {
5515c369683STrond Myklebust 		res = nfs_scan_list(nfsi, dst, idx_start, npages,
5525c369683STrond Myklebust 				NFS_PAGE_TAG_COMMIT);
5531da177e4SLinus Torvalds 		nfsi->ncommit -= res;
5543da28eb1STrond Myklebust 	}
5551da177e4SLinus Torvalds 	return res;
5561da177e4SLinus Torvalds }
557c42de9ddSTrond Myklebust #else
558ca52fec1STrond Myklebust static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
559c42de9ddSTrond Myklebust {
560c42de9ddSTrond Myklebust 	return 0;
561c42de9ddSTrond Myklebust }
5621da177e4SLinus Torvalds #endif
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds /*
5651da177e4SLinus Torvalds  * Try to update any existing write request, or create one if there is none.
5661da177e4SLinus Torvalds  * In order to match, the request's credentials must match those of
5671da177e4SLinus Torvalds  * the calling process.
5681da177e4SLinus Torvalds  *
5691da177e4SLinus Torvalds  * Note: Should always be called with the Page Lock held!
5701da177e4SLinus Torvalds  */
5711da177e4SLinus Torvalds static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
572e21195a7STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
5731da177e4SLinus Torvalds {
57489a09141SPeter Zijlstra 	struct address_space *mapping = page->mapping;
57589a09141SPeter Zijlstra 	struct inode *inode = mapping->host;
5761da177e4SLinus Torvalds 	struct nfs_page		*req, *new = NULL;
577ca52fec1STrond Myklebust 	pgoff_t		rqend, end;
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	end = offset + bytes;
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	for (;;) {
5821da177e4SLinus Torvalds 		/* Loop over all inode entries and see if we find
5831da177e4SLinus Torvalds 		 * A request for the page we wish to update
5841da177e4SLinus Torvalds 		 */
585587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
586277459d2STrond Myklebust 		req = nfs_page_find_request_locked(page);
5871da177e4SLinus Torvalds 		if (req) {
588acee478aSTrond Myklebust 			if (!nfs_set_page_tag_locked(req)) {
5891da177e4SLinus Torvalds 				int error;
590277459d2STrond Myklebust 
591587142f8STrond Myklebust 				spin_unlock(&inode->i_lock);
5921da177e4SLinus Torvalds 				error = nfs_wait_on_request(req);
5931da177e4SLinus Torvalds 				nfs_release_request(req);
5941dd594b2SNeil Brown 				if (error < 0) {
5951dd594b2SNeil Brown 					if (new)
5961dd594b2SNeil Brown 						nfs_release_request(new);
5971da177e4SLinus Torvalds 					return ERR_PTR(error);
5981dd594b2SNeil Brown 				}
5991da177e4SLinus Torvalds 				continue;
6001da177e4SLinus Torvalds 			}
601587142f8STrond Myklebust 			spin_unlock(&inode->i_lock);
6021da177e4SLinus Torvalds 			if (new)
6031da177e4SLinus Torvalds 				nfs_release_request(new);
6041da177e4SLinus Torvalds 			break;
6051da177e4SLinus Torvalds 		}
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 		if (new) {
6081da177e4SLinus Torvalds 			int error;
6091da177e4SLinus Torvalds 			nfs_lock_request_dontget(new);
6101da177e4SLinus Torvalds 			error = nfs_inode_add_request(inode, new);
6111da177e4SLinus Torvalds 			if (error) {
612587142f8STrond Myklebust 				spin_unlock(&inode->i_lock);
6131da177e4SLinus Torvalds 				nfs_unlock_request(new);
6141da177e4SLinus Torvalds 				return ERR_PTR(error);
6151da177e4SLinus Torvalds 			}
616587142f8STrond Myklebust 			spin_unlock(&inode->i_lock);
61761e930a9STrond Myklebust 			req = new;
61861e930a9STrond Myklebust 			goto zero_page;
6191da177e4SLinus Torvalds 		}
620587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 		new = nfs_create_request(ctx, inode, page, offset, bytes);
6231da177e4SLinus Torvalds 		if (IS_ERR(new))
6241da177e4SLinus Torvalds 			return new;
6251da177e4SLinus Torvalds 	}
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	/* We have a request for our page.
6281da177e4SLinus Torvalds 	 * If the creds don't match, or the
6291da177e4SLinus Torvalds 	 * page addresses don't match,
6301da177e4SLinus Torvalds 	 * tell the caller to wait on the conflicting
6311da177e4SLinus Torvalds 	 * request.
6321da177e4SLinus Torvalds 	 */
6331da177e4SLinus Torvalds 	rqend = req->wb_offset + req->wb_bytes;
6341da177e4SLinus Torvalds 	if (req->wb_context != ctx
6351da177e4SLinus Torvalds 	    || req->wb_page != page
6361da177e4SLinus Torvalds 	    || !nfs_dirty_request(req)
6371da177e4SLinus Torvalds 	    || offset > rqend || end < req->wb_offset) {
638acee478aSTrond Myklebust 		nfs_clear_page_tag_locked(req);
6391da177e4SLinus Torvalds 		return ERR_PTR(-EBUSY);
6401da177e4SLinus Torvalds 	}
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
6431da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
6441da177e4SLinus Torvalds 		req->wb_offset = offset;
6451da177e4SLinus Torvalds 		req->wb_pgbase = offset;
64661e930a9STrond Myklebust 		req->wb_bytes = max(end, rqend) - req->wb_offset;
64761e930a9STrond Myklebust 		goto zero_page;
6481da177e4SLinus Torvalds 	}
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	if (end > rqend)
6511da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	return req;
65461e930a9STrond Myklebust zero_page:
65561e930a9STrond Myklebust 	/* If this page might potentially be marked as up to date,
65661e930a9STrond Myklebust 	 * then we need to zero any uninitalised data. */
65761e930a9STrond Myklebust 	if (req->wb_pgbase == 0 && req->wb_bytes != PAGE_CACHE_SIZE
65861e930a9STrond Myklebust 			&& !PageUptodate(req->wb_page))
659eebd2aa3SChristoph Lameter 		zero_user_segment(req->wb_page, req->wb_bytes, PAGE_CACHE_SIZE);
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);
734e261f51fSTrond Myklebust 	__set_page_dirty_nobuffers(page);
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds         dprintk("NFS:      nfs_updatepage returns %d (isize %Ld)\n",
7371da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
7381da177e4SLinus Torvalds 	if (status < 0)
739a301b777STrond Myklebust 		nfs_set_pageerror(page);
7401da177e4SLinus Torvalds 	return status;
7411da177e4SLinus Torvalds }
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds static void nfs_writepage_release(struct nfs_page *req)
7441da177e4SLinus Torvalds {
7458e821cadSTrond Myklebust 
74644dd151dSTrond Myklebust 	if (PageError(req->wb_page)) {
74744dd151dSTrond Myklebust 		nfs_end_page_writeback(req->wb_page);
74844dd151dSTrond Myklebust 		nfs_inode_remove_request(req);
74944dd151dSTrond Myklebust 	} else if (!nfs_reschedule_unstable_write(req)) {
75044dd151dSTrond Myklebust 		/* Set the PG_uptodate flag */
75144dd151dSTrond Myklebust 		nfs_mark_uptodate(req->wb_page, req->wb_pgbase, req->wb_bytes);
75289a09141SPeter Zijlstra 		nfs_end_page_writeback(req->wb_page);
7531da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
7548e821cadSTrond Myklebust 	} else
7558e821cadSTrond Myklebust 		nfs_end_page_writeback(req->wb_page);
7569fd367f0STrond Myklebust 	nfs_clear_page_tag_locked(req);
7571da177e4SLinus Torvalds }
7581da177e4SLinus Torvalds 
7593ff7576dSTrond Myklebust static int flush_task_priority(int how)
7601da177e4SLinus Torvalds {
7611da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
7621da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
7631da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
7641da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
7651da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
7661da177e4SLinus Torvalds 	}
7671da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
7681da177e4SLinus Torvalds }
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds /*
7711da177e4SLinus Torvalds  * Set up the argument/result storage required for the RPC call.
7721da177e4SLinus Torvalds  */
7731da177e4SLinus Torvalds static void nfs_write_rpcsetup(struct nfs_page *req,
7741da177e4SLinus Torvalds 		struct nfs_write_data *data,
775788e7a89STrond Myklebust 		const struct rpc_call_ops *call_ops,
7761da177e4SLinus Torvalds 		unsigned int count, unsigned int offset,
7771da177e4SLinus Torvalds 		int how)
7781da177e4SLinus Torvalds {
77984115e1cSTrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
78084115e1cSTrond Myklebust 	int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
7813ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
78207737691STrond Myklebust 	struct rpc_task *task;
783bdc7f021STrond Myklebust 	struct rpc_message msg = {
784bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
785bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
786bdc7f021STrond Myklebust 		.rpc_cred = req->wb_context->cred,
787bdc7f021STrond Myklebust 	};
78884115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
78984115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(inode),
79007737691STrond Myklebust 		.task = &data->task,
791bdc7f021STrond Myklebust 		.rpc_message = &msg,
79284115e1cSTrond Myklebust 		.callback_ops = call_ops,
79384115e1cSTrond Myklebust 		.callback_data = data,
794101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
79584115e1cSTrond Myklebust 		.flags = flags,
7963ff7576dSTrond Myklebust 		.priority = priority,
79784115e1cSTrond Myklebust 	};
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
8001da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	data->req = req;
80388be9f99STrond Myklebust 	data->inode = inode = req->wb_context->path.dentry->d_inode;
804bdc7f021STrond Myklebust 	data->cred = msg.rpc_cred;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(inode);
8071da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
8081da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
8091da177e4SLinus Torvalds 	data->args.pages  = data->pagevec;
8101da177e4SLinus Torvalds 	data->args.count  = count;
811383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
812bdc7f021STrond Myklebust 	data->args.stable  = NFS_UNSTABLE;
813bdc7f021STrond Myklebust 	if (how & FLUSH_STABLE) {
814bdc7f021STrond Myklebust 		data->args.stable = NFS_DATA_SYNC;
815bdc7f021STrond Myklebust 		if (!NFS_I(inode)->ncommit)
816bdc7f021STrond Myklebust 			data->args.stable = NFS_FILE_SYNC;
817bdc7f021STrond Myklebust 	}
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
8201da177e4SLinus Torvalds 	data->res.count   = count;
8211da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
8220e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
8231da177e4SLinus Torvalds 
824788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
825bdc7f021STrond Myklebust 	NFS_PROTO(inode)->write_setup(data, &msg);
8261da177e4SLinus Torvalds 
827a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated write call "
828a3f565b1SChuck Lever 		"(req %s/%Ld, %u bytes @ offset %Lu)\n",
8290bbacc40SChuck Lever 		data->task.tk_pid,
8301da177e4SLinus Torvalds 		inode->i_sb->s_id,
8311da177e4SLinus Torvalds 		(long long)NFS_FILEID(inode),
8321da177e4SLinus Torvalds 		count,
8331da177e4SLinus Torvalds 		(unsigned long long)data->args.offset);
8341da177e4SLinus Torvalds 
83507737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
83607737691STrond Myklebust 	if (!IS_ERR(task))
83707737691STrond Myklebust 		rpc_put_task(task);
8381da177e4SLinus Torvalds }
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds /*
8411da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
8421da177e4SLinus Torvalds  * contiguous dirty area on one page.
8431da177e4SLinus Torvalds  */
8448d5658c9STrond Myklebust static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
8451da177e4SLinus Torvalds {
8461da177e4SLinus Torvalds 	struct nfs_page *req = nfs_list_entry(head->next);
8471da177e4SLinus Torvalds 	struct page *page = req->wb_page;
8481da177e4SLinus Torvalds 	struct nfs_write_data *data;
849e9f7bee1STrond Myklebust 	size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
850e9f7bee1STrond Myklebust 	unsigned int offset;
8511da177e4SLinus Torvalds 	int requests = 0;
8521da177e4SLinus Torvalds 	LIST_HEAD(list);
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	nfs_list_remove_request(req);
8551da177e4SLinus Torvalds 
856bcb71bbaSTrond Myklebust 	nbytes = count;
857e9f7bee1STrond Myklebust 	do {
858e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
859e9f7bee1STrond Myklebust 
8608d5658c9STrond Myklebust 		data = nfs_writedata_alloc(1);
8611da177e4SLinus Torvalds 		if (!data)
8621da177e4SLinus Torvalds 			goto out_bad;
8631da177e4SLinus Torvalds 		list_add(&data->pages, &list);
8641da177e4SLinus Torvalds 		requests++;
865e9f7bee1STrond Myklebust 		nbytes -= len;
866e9f7bee1STrond Myklebust 	} while (nbytes != 0);
8671da177e4SLinus Torvalds 	atomic_set(&req->wb_complete, requests);
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	ClearPageError(page);
8701da177e4SLinus Torvalds 	offset = 0;
871bcb71bbaSTrond Myklebust 	nbytes = count;
8721da177e4SLinus Torvalds 	do {
8731da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
8741da177e4SLinus Torvalds 		list_del_init(&data->pages);
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 		data->pagevec[0] = page;
8771da177e4SLinus Torvalds 
878bcb71bbaSTrond Myklebust 		if (nbytes < wsize)
879bcb71bbaSTrond Myklebust 			wsize = nbytes;
880788e7a89STrond Myklebust 		nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
881788e7a89STrond Myklebust 				   wsize, offset, how);
8821da177e4SLinus Torvalds 		offset += wsize;
8831da177e4SLinus Torvalds 		nbytes -= wsize;
8841da177e4SLinus Torvalds 	} while (nbytes != 0);
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	return 0;
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds out_bad:
8891da177e4SLinus Torvalds 	while (!list_empty(&list)) {
8901da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
8911da177e4SLinus Torvalds 		list_del(&data->pages);
8928aca67f0STrond Myklebust 		nfs_writedata_release(data);
8931da177e4SLinus Torvalds 	}
89461822ab5STrond Myklebust 	nfs_redirty_request(req);
8956d677e35STrond Myklebust 	nfs_end_page_writeback(req->wb_page);
8969fd367f0STrond Myklebust 	nfs_clear_page_tag_locked(req);
8971da177e4SLinus Torvalds 	return -ENOMEM;
8981da177e4SLinus Torvalds }
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds /*
9011da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
9021da177e4SLinus Torvalds  * The page must have been locked by the caller.
9031da177e4SLinus Torvalds  *
9041da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
9051da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
9061da177e4SLinus Torvalds  * that has been written but not committed.
9071da177e4SLinus Torvalds  */
9088d5658c9STrond Myklebust static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
9091da177e4SLinus Torvalds {
9101da177e4SLinus Torvalds 	struct nfs_page		*req;
9111da177e4SLinus Torvalds 	struct page		**pages;
9121da177e4SLinus Torvalds 	struct nfs_write_data	*data;
9131da177e4SLinus Torvalds 
9148d5658c9STrond Myklebust 	data = nfs_writedata_alloc(npages);
9151da177e4SLinus Torvalds 	if (!data)
9161da177e4SLinus Torvalds 		goto out_bad;
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	pages = data->pagevec;
9191da177e4SLinus Torvalds 	while (!list_empty(head)) {
9201da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
9211da177e4SLinus Torvalds 		nfs_list_remove_request(req);
9221da177e4SLinus Torvalds 		nfs_list_add_request(req, &data->pages);
9231da177e4SLinus Torvalds 		ClearPageError(req->wb_page);
9241da177e4SLinus Torvalds 		*pages++ = req->wb_page;
9251da177e4SLinus Torvalds 	}
9261da177e4SLinus Torvalds 	req = nfs_list_entry(data->pages.next);
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 	/* Set up the argument struct */
929788e7a89STrond Myklebust 	nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	return 0;
9321da177e4SLinus Torvalds  out_bad:
9331da177e4SLinus Torvalds 	while (!list_empty(head)) {
93410afec90STrond Myklebust 		req = nfs_list_entry(head->next);
9351da177e4SLinus Torvalds 		nfs_list_remove_request(req);
93661822ab5STrond Myklebust 		nfs_redirty_request(req);
9376d677e35STrond Myklebust 		nfs_end_page_writeback(req->wb_page);
9389fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
9391da177e4SLinus Torvalds 	}
9401da177e4SLinus Torvalds 	return -ENOMEM;
9411da177e4SLinus Torvalds }
9421da177e4SLinus Torvalds 
943c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
944c63c7b05STrond Myklebust 				  struct inode *inode, int ioflags)
9451da177e4SLinus Torvalds {
946bf4285e7SChuck Lever 	size_t wsize = NFS_SERVER(inode)->wsize;
9471da177e4SLinus Torvalds 
948bcb71bbaSTrond Myklebust 	if (wsize < PAGE_CACHE_SIZE)
949c63c7b05STrond Myklebust 		nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
950bcb71bbaSTrond Myklebust 	else
951c63c7b05STrond Myklebust 		nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
9521da177e4SLinus Torvalds }
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds /*
9551da177e4SLinus Torvalds  * Handle a write reply that flushed part of a page.
9561da177e4SLinus Torvalds  */
957788e7a89STrond Myklebust static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
9581da177e4SLinus Torvalds {
959788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
9601da177e4SLinus Torvalds 	struct nfs_page		*req = data->req;
9611da177e4SLinus Torvalds 	struct page		*page = req->wb_page;
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 	dprintk("NFS: write (%s/%Ld %d@%Ld)",
96488be9f99STrond Myklebust 		req->wb_context->path.dentry->d_inode->i_sb->s_id,
96588be9f99STrond Myklebust 		(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
9661da177e4SLinus Torvalds 		req->wb_bytes,
9671da177e4SLinus Torvalds 		(long long)req_offset(req));
9681da177e4SLinus Torvalds 
969788e7a89STrond Myklebust 	if (nfs_writeback_done(task, data) != 0)
970788e7a89STrond Myklebust 		return;
971788e7a89STrond Myklebust 
972788e7a89STrond Myklebust 	if (task->tk_status < 0) {
973a301b777STrond Myklebust 		nfs_set_pageerror(page);
9747b159fc1STrond Myklebust 		nfs_context_set_write_error(req->wb_context, task->tk_status);
975788e7a89STrond Myklebust 		dprintk(", error = %d\n", task->tk_status);
9768e821cadSTrond Myklebust 		goto out;
9778e821cadSTrond Myklebust 	}
9788e821cadSTrond Myklebust 
9798e821cadSTrond Myklebust 	if (nfs_write_need_commit(data)) {
980587142f8STrond Myklebust 		struct inode *inode = page->mapping->host;
9818e821cadSTrond Myklebust 
982587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
9838e821cadSTrond Myklebust 		if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
9848e821cadSTrond Myklebust 			/* Do nothing we need to resend the writes */
9858e821cadSTrond Myklebust 		} else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
9861da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
9871da177e4SLinus Torvalds 			dprintk(" defer commit\n");
9881da177e4SLinus Torvalds 		} else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
9898e821cadSTrond Myklebust 			set_bit(PG_NEED_RESCHED, &req->wb_flags);
9908e821cadSTrond Myklebust 			clear_bit(PG_NEED_COMMIT, &req->wb_flags);
9911da177e4SLinus Torvalds 			dprintk(" server reboot detected\n");
9921da177e4SLinus Torvalds 		}
993587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
9941da177e4SLinus Torvalds 	} else
9951da177e4SLinus Torvalds 		dprintk(" OK\n");
9961da177e4SLinus Torvalds 
9978e821cadSTrond Myklebust out:
9981da177e4SLinus Torvalds 	if (atomic_dec_and_test(&req->wb_complete))
9991da177e4SLinus Torvalds 		nfs_writepage_release(req);
10001da177e4SLinus Torvalds }
10011da177e4SLinus Torvalds 
1002788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops = {
1003788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_partial,
1004788e7a89STrond Myklebust 	.rpc_release = nfs_writedata_release,
1005788e7a89STrond Myklebust };
1006788e7a89STrond Myklebust 
10071da177e4SLinus Torvalds /*
10081da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
10091da177e4SLinus Torvalds  *
10101da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
10111da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
10121da177e4SLinus Torvalds  *	  as the page has a write request pending.
10131da177e4SLinus Torvalds  */
1014788e7a89STrond Myklebust static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
10151da177e4SLinus Torvalds {
1016788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
10171da177e4SLinus Torvalds 	struct nfs_page		*req;
10181da177e4SLinus Torvalds 	struct page		*page;
10191da177e4SLinus Torvalds 
1020788e7a89STrond Myklebust 	if (nfs_writeback_done(task, data) != 0)
1021788e7a89STrond Myklebust 		return;
1022788e7a89STrond Myklebust 
10231da177e4SLinus Torvalds 	/* Update attributes as result of writeback. */
10241da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
10251da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
10261da177e4SLinus Torvalds 		nfs_list_remove_request(req);
10271da177e4SLinus Torvalds 		page = req->wb_page;
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 		dprintk("NFS: write (%s/%Ld %d@%Ld)",
103088be9f99STrond Myklebust 			req->wb_context->path.dentry->d_inode->i_sb->s_id,
103188be9f99STrond Myklebust 			(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
10321da177e4SLinus Torvalds 			req->wb_bytes,
10331da177e4SLinus Torvalds 			(long long)req_offset(req));
10341da177e4SLinus Torvalds 
1035788e7a89STrond Myklebust 		if (task->tk_status < 0) {
1036a301b777STrond Myklebust 			nfs_set_pageerror(page);
10377b159fc1STrond Myklebust 			nfs_context_set_write_error(req->wb_context, task->tk_status);
1038788e7a89STrond Myklebust 			dprintk(", error = %d\n", task->tk_status);
10398e821cadSTrond Myklebust 			goto remove_request;
10401da177e4SLinus Torvalds 		}
10411da177e4SLinus Torvalds 
10428e821cadSTrond Myklebust 		if (nfs_write_need_commit(data)) {
10431da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
10441da177e4SLinus Torvalds 			nfs_mark_request_commit(req);
10458e821cadSTrond Myklebust 			nfs_end_page_writeback(page);
10461da177e4SLinus Torvalds 			dprintk(" marked for commit\n");
10478e821cadSTrond Myklebust 			goto next;
10488e821cadSTrond Myklebust 		}
104944dd151dSTrond Myklebust 		/* Set the PG_uptodate flag? */
105044dd151dSTrond Myklebust 		nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
10518e821cadSTrond Myklebust 		dprintk(" OK\n");
10528e821cadSTrond Myklebust remove_request:
10538e821cadSTrond Myklebust 		nfs_end_page_writeback(page);
10541da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
10551da177e4SLinus Torvalds 	next:
10569fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
10571da177e4SLinus Torvalds 	}
10581da177e4SLinus Torvalds }
10591da177e4SLinus Torvalds 
1060788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops = {
1061788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_full,
1062788e7a89STrond Myklebust 	.rpc_release = nfs_writedata_release,
1063788e7a89STrond Myklebust };
1064788e7a89STrond Myklebust 
1065788e7a89STrond Myklebust 
10661da177e4SLinus Torvalds /*
10671da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
10681da177e4SLinus Torvalds  */
1069462d5b32SChuck Lever int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
10701da177e4SLinus Torvalds {
10711da177e4SLinus Torvalds 	struct nfs_writeargs	*argp = &data->args;
10721da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
1073788e7a89STrond Myklebust 	int status;
10741da177e4SLinus Torvalds 
1075a3f565b1SChuck Lever 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
10761da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
10771da177e4SLinus Torvalds 
1078f551e44fSChuck Lever 	/*
1079f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1080f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1081f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1082f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1083f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1084f551e44fSChuck Lever 	 */
1085788e7a89STrond Myklebust 	status = NFS_PROTO(data->inode)->write_done(task, data);
1086788e7a89STrond Myklebust 	if (status != 0)
1087788e7a89STrond Myklebust 		return status;
108891d5b470SChuck Lever 	nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
108991d5b470SChuck Lever 
10901da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
10911da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
10921da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
10931da177e4SLinus Torvalds 		 * commit data to stable storage even though we
10941da177e4SLinus Torvalds 		 * requested it.
10951da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
10961da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
10971da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
10981da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
10991da177e4SLinus Torvalds 		 */
11001da177e4SLinus Torvalds 		static unsigned long    complain;
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
11031da177e4SLinus Torvalds 			dprintk("NFS: faulty NFS server %s:"
11041da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
110554ceac45SDavid Howells 				NFS_SERVER(data->inode)->nfs_client->cl_hostname,
11061da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
11071da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
11081da177e4SLinus Torvalds 		}
11091da177e4SLinus Torvalds 	}
11101da177e4SLinus Torvalds #endif
11111da177e4SLinus Torvalds 	/* Is this a short write? */
11121da177e4SLinus Torvalds 	if (task->tk_status >= 0 && resp->count < argp->count) {
11131da177e4SLinus Torvalds 		static unsigned long    complain;
11141da177e4SLinus Torvalds 
111591d5b470SChuck Lever 		nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
111691d5b470SChuck Lever 
11171da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
11181da177e4SLinus Torvalds 		if (resp->count != 0) {
11191da177e4SLinus Torvalds 			/* Was this an NFSv2 write or an NFSv3 stable write? */
11201da177e4SLinus Torvalds 			if (resp->verf->committed != NFS_UNSTABLE) {
11211da177e4SLinus Torvalds 				/* Resend from where the server left off */
11221da177e4SLinus Torvalds 				argp->offset += resp->count;
11231da177e4SLinus Torvalds 				argp->pgbase += resp->count;
11241da177e4SLinus Torvalds 				argp->count -= resp->count;
11251da177e4SLinus Torvalds 			} else {
11261da177e4SLinus Torvalds 				/* Resend as a stable write in order to avoid
11271da177e4SLinus Torvalds 				 * headaches in the case of a server crash.
11281da177e4SLinus Torvalds 				 */
11291da177e4SLinus Torvalds 				argp->stable = NFS_FILE_SYNC;
11301da177e4SLinus Torvalds 			}
11311da177e4SLinus Torvalds 			rpc_restart_call(task);
1132788e7a89STrond Myklebust 			return -EAGAIN;
11331da177e4SLinus Torvalds 		}
11341da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
11351da177e4SLinus Torvalds 			printk(KERN_WARNING
11361da177e4SLinus Torvalds 			       "NFS: Server wrote zero bytes, expected %u.\n",
11371da177e4SLinus Torvalds 					argp->count);
11381da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
11391da177e4SLinus Torvalds 		}
11401da177e4SLinus Torvalds 		/* Can't do anything about it except throw an error. */
11411da177e4SLinus Torvalds 		task->tk_status = -EIO;
11421da177e4SLinus Torvalds 	}
1143788e7a89STrond Myklebust 	return 0;
11441da177e4SLinus Torvalds }
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1148383ba719STrond Myklebust void nfs_commit_release(void *data)
11491da177e4SLinus Torvalds {
1150383ba719STrond Myklebust 	struct nfs_write_data *wdata = data;
1151383ba719STrond Myklebust 
1152383ba719STrond Myklebust 	put_nfs_open_context(wdata->args.context);
11531da177e4SLinus Torvalds 	nfs_commit_free(wdata);
11541da177e4SLinus Torvalds }
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds /*
11571da177e4SLinus Torvalds  * Set up the argument/result storage required for the RPC call.
11581da177e4SLinus Torvalds  */
11591da177e4SLinus Torvalds static void nfs_commit_rpcsetup(struct list_head *head,
1160788e7a89STrond Myklebust 		struct nfs_write_data *data,
1161788e7a89STrond Myklebust 		int how)
11621da177e4SLinus Torvalds {
116384115e1cSTrond Myklebust 	struct nfs_page *first = nfs_list_entry(head->next);
116484115e1cSTrond Myklebust 	struct inode *inode = first->wb_context->path.dentry->d_inode;
116584115e1cSTrond Myklebust 	int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
11663ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
116707737691STrond Myklebust 	struct rpc_task *task;
1168bdc7f021STrond Myklebust 	struct rpc_message msg = {
1169bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1170bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
1171bdc7f021STrond Myklebust 		.rpc_cred = first->wb_context->cred,
1172bdc7f021STrond Myklebust 	};
117384115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
117407737691STrond Myklebust 		.task = &data->task,
117584115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(inode),
1176bdc7f021STrond Myklebust 		.rpc_message = &msg,
117784115e1cSTrond Myklebust 		.callback_ops = &nfs_commit_ops,
117884115e1cSTrond Myklebust 		.callback_data = data,
1179101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
118084115e1cSTrond Myklebust 		.flags = flags,
11813ff7576dSTrond Myklebust 		.priority = priority,
118284115e1cSTrond Myklebust 	};
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
11851da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
11861da177e4SLinus Torvalds 
11871da177e4SLinus Torvalds 	list_splice_init(head, &data->pages);
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 	data->inode	  = inode;
1190bdc7f021STrond Myklebust 	data->cred	  = msg.rpc_cred;
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(data->inode);
11933da28eb1STrond Myklebust 	/* Note: we always request a commit of the entire inode */
11943da28eb1STrond Myklebust 	data->args.offset = 0;
11953da28eb1STrond Myklebust 	data->args.count  = 0;
1196383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(first->wb_context);
11973da28eb1STrond Myklebust 	data->res.count   = 0;
11981da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
11991da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
12000e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
12011da177e4SLinus Torvalds 
1202788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
1203bdc7f021STrond Myklebust 	NFS_PROTO(inode)->commit_setup(data, &msg);
12041da177e4SLinus Torvalds 
1205a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1206bdc7f021STrond Myklebust 
120707737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
120807737691STrond Myklebust 	if (!IS_ERR(task))
120907737691STrond Myklebust 		rpc_put_task(task);
12101da177e4SLinus Torvalds }
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds /*
12131da177e4SLinus Torvalds  * Commit dirty pages
12141da177e4SLinus Torvalds  */
12151da177e4SLinus Torvalds static int
121640859d7eSChuck Lever nfs_commit_list(struct inode *inode, struct list_head *head, int how)
12171da177e4SLinus Torvalds {
12181da177e4SLinus Torvalds 	struct nfs_write_data	*data;
12191da177e4SLinus Torvalds 	struct nfs_page         *req;
12201da177e4SLinus Torvalds 
1221e9f7bee1STrond Myklebust 	data = nfs_commit_alloc();
12221da177e4SLinus Torvalds 
12231da177e4SLinus Torvalds 	if (!data)
12241da177e4SLinus Torvalds 		goto out_bad;
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 	/* Set up the argument struct */
12271da177e4SLinus Torvalds 	nfs_commit_rpcsetup(head, data, how);
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds 	return 0;
12301da177e4SLinus Torvalds  out_bad:
12311da177e4SLinus Torvalds 	while (!list_empty(head)) {
12321da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
12331da177e4SLinus Torvalds 		nfs_list_remove_request(req);
12341da177e4SLinus Torvalds 		nfs_mark_request_commit(req);
123583715ad5STrond Myklebust 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1236c9e51e41SPeter Zijlstra 		dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1237c9e51e41SPeter Zijlstra 				BDI_RECLAIMABLE);
12389fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
12391da177e4SLinus Torvalds 	}
12401da177e4SLinus Torvalds 	return -ENOMEM;
12411da177e4SLinus Torvalds }
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds /*
12441da177e4SLinus Torvalds  * COMMIT call returned
12451da177e4SLinus Torvalds  */
1246788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
12471da177e4SLinus Torvalds {
1248963d8fe5STrond Myklebust 	struct nfs_write_data	*data = calldata;
12491da177e4SLinus Torvalds 	struct nfs_page		*req;
12501da177e4SLinus Torvalds 
1251a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
12521da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
12531da177e4SLinus Torvalds 
1254788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1255788e7a89STrond Myklebust 	if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1256788e7a89STrond Myklebust 		return;
1257788e7a89STrond Myklebust 
12581da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
12591da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
12601da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1261612c9384STrond Myklebust 		clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
1262fd39fc85SChristoph Lameter 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1263c9e51e41SPeter Zijlstra 		dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1264c9e51e41SPeter Zijlstra 				BDI_RECLAIMABLE);
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 		dprintk("NFS: commit (%s/%Ld %d@%Ld)",
126788be9f99STrond Myklebust 			req->wb_context->path.dentry->d_inode->i_sb->s_id,
126888be9f99STrond Myklebust 			(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
12691da177e4SLinus Torvalds 			req->wb_bytes,
12701da177e4SLinus Torvalds 			(long long)req_offset(req));
12711da177e4SLinus Torvalds 		if (task->tk_status < 0) {
12727b159fc1STrond Myklebust 			nfs_context_set_write_error(req->wb_context, task->tk_status);
12731da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
12741da177e4SLinus Torvalds 			dprintk(", error = %d\n", task->tk_status);
12751da177e4SLinus Torvalds 			goto next;
12761da177e4SLinus Torvalds 		}
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
12791da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
12801da177e4SLinus Torvalds 		if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
12811da177e4SLinus Torvalds 			/* We have a match */
128244dd151dSTrond Myklebust 			/* Set the PG_uptodate flag */
128344dd151dSTrond Myklebust 			nfs_mark_uptodate(req->wb_page, req->wb_pgbase,
128444dd151dSTrond Myklebust 					req->wb_bytes);
12851da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
12861da177e4SLinus Torvalds 			dprintk(" OK\n");
12871da177e4SLinus Torvalds 			goto next;
12881da177e4SLinus Torvalds 		}
12891da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
12901da177e4SLinus Torvalds 		dprintk(" mismatch\n");
129161822ab5STrond Myklebust 		nfs_redirty_request(req);
12921da177e4SLinus Torvalds 	next:
12939fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
12941da177e4SLinus Torvalds 	}
12951da177e4SLinus Torvalds }
1296788e7a89STrond Myklebust 
1297788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
1298788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1299788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1300788e7a89STrond Myklebust };
13011da177e4SLinus Torvalds 
13023da28eb1STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
13031da177e4SLinus Torvalds {
13041da177e4SLinus Torvalds 	LIST_HEAD(head);
13057d46a49fSTrond Myklebust 	int res;
13061da177e4SLinus Torvalds 
1307587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
13083da28eb1STrond Myklebust 	res = nfs_scan_commit(inode, &head, 0, 0);
1309587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
13101da177e4SLinus Torvalds 	if (res) {
13117d46a49fSTrond Myklebust 		int error = nfs_commit_list(inode, &head, how);
13121da177e4SLinus Torvalds 		if (error < 0)
13131da177e4SLinus Torvalds 			return error;
13143da28eb1STrond Myklebust 	}
13151da177e4SLinus Torvalds 	return res;
13161da177e4SLinus Torvalds }
1317c63c7b05STrond Myklebust #else
1318c63c7b05STrond Myklebust static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1319c63c7b05STrond Myklebust {
1320c63c7b05STrond Myklebust 	return 0;
1321c63c7b05STrond Myklebust }
13221da177e4SLinus Torvalds #endif
13231da177e4SLinus Torvalds 
13241c75950bSTrond Myklebust long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
13251da177e4SLinus Torvalds {
13261c75950bSTrond Myklebust 	struct inode *inode = mapping->host;
1327ca52fec1STrond Myklebust 	pgoff_t idx_start, idx_end;
13281c75950bSTrond Myklebust 	unsigned int npages = 0;
1329c42de9ddSTrond Myklebust 	LIST_HEAD(head);
133070b9ecbdSTrond Myklebust 	int nocommit = how & FLUSH_NOCOMMIT;
13313f442547STrond Myklebust 	long pages, ret;
13321da177e4SLinus Torvalds 
13331c75950bSTrond Myklebust 	/* FIXME */
13341c75950bSTrond Myklebust 	if (wbc->range_cyclic)
13351c75950bSTrond Myklebust 		idx_start = 0;
13361c75950bSTrond Myklebust 	else {
13371c75950bSTrond Myklebust 		idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
13381c75950bSTrond Myklebust 		idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
13391c75950bSTrond Myklebust 		if (idx_end > idx_start) {
1340ca52fec1STrond Myklebust 			pgoff_t l_npages = 1 + idx_end - idx_start;
13411c75950bSTrond Myklebust 			npages = l_npages;
13421c75950bSTrond Myklebust 			if (sizeof(npages) != sizeof(l_npages) &&
1343ca52fec1STrond Myklebust 					(pgoff_t)npages != l_npages)
13441c75950bSTrond Myklebust 				npages = 0;
13451c75950bSTrond Myklebust 		}
13461c75950bSTrond Myklebust 	}
1347c42de9ddSTrond Myklebust 	how &= ~FLUSH_NOCOMMIT;
1348587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
13491da177e4SLinus Torvalds 	do {
1350c42de9ddSTrond Myklebust 		ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1351c42de9ddSTrond Myklebust 		if (ret != 0)
1352c42de9ddSTrond Myklebust 			continue;
1353c42de9ddSTrond Myklebust 		if (nocommit)
1354c42de9ddSTrond Myklebust 			break;
1355d2ccddf0STrond Myklebust 		pages = nfs_scan_commit(inode, &head, idx_start, npages);
1356724c439cSTrond Myklebust 		if (pages == 0)
1357c42de9ddSTrond Myklebust 			break;
1358d2ccddf0STrond Myklebust 		if (how & FLUSH_INVALIDATE) {
1359587142f8STrond Myklebust 			spin_unlock(&inode->i_lock);
136083715ad5STrond Myklebust 			nfs_cancel_commit_list(&head);
1361e8e058e8STrond Myklebust 			ret = pages;
1362587142f8STrond Myklebust 			spin_lock(&inode->i_lock);
1363d2ccddf0STrond Myklebust 			continue;
1364d2ccddf0STrond Myklebust 		}
1365d2ccddf0STrond Myklebust 		pages += nfs_scan_commit(inode, &head, 0, 0);
1366587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
1367c42de9ddSTrond Myklebust 		ret = nfs_commit_list(inode, &head, how);
1368587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
1369587142f8STrond Myklebust 
1370c42de9ddSTrond Myklebust 	} while (ret >= 0);
1371587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
1372c42de9ddSTrond Myklebust 	return ret;
13731da177e4SLinus Torvalds }
13741da177e4SLinus Torvalds 
137534901f70STrond Myklebust static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
13761c75950bSTrond Myklebust {
13771c75950bSTrond Myklebust 	int ret;
13781c75950bSTrond Myklebust 
137934901f70STrond Myklebust 	ret = nfs_writepages(mapping, wbc);
138061822ab5STrond Myklebust 	if (ret < 0)
138161822ab5STrond Myklebust 		goto out;
138234901f70STrond Myklebust 	ret = nfs_sync_mapping_wait(mapping, wbc, how);
1383ed90ef51STrond Myklebust 	if (ret < 0)
1384ed90ef51STrond Myklebust 		goto out;
13851c75950bSTrond Myklebust 	return 0;
138661822ab5STrond Myklebust out:
1387e507d9ebSTrond Myklebust 	__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
13881c75950bSTrond Myklebust 	return ret;
13891c75950bSTrond Myklebust }
13901c75950bSTrond Myklebust 
139134901f70STrond Myklebust /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
139234901f70STrond Myklebust static int nfs_write_mapping(struct address_space *mapping, int how)
139334901f70STrond Myklebust {
139434901f70STrond Myklebust 	struct writeback_control wbc = {
139534901f70STrond Myklebust 		.bdi = mapping->backing_dev_info,
139634901f70STrond Myklebust 		.sync_mode = WB_SYNC_NONE,
139734901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
139834901f70STrond Myklebust 		.for_writepages = 1,
139934901f70STrond Myklebust 		.range_cyclic = 1,
140034901f70STrond Myklebust 	};
140134901f70STrond Myklebust 	int ret;
140234901f70STrond Myklebust 
140334901f70STrond Myklebust 	ret = __nfs_write_mapping(mapping, &wbc, how);
140434901f70STrond Myklebust 	if (ret < 0)
140534901f70STrond Myklebust 		return ret;
140634901f70STrond Myklebust 	wbc.sync_mode = WB_SYNC_ALL;
140734901f70STrond Myklebust 	return __nfs_write_mapping(mapping, &wbc, how);
140834901f70STrond Myklebust }
140934901f70STrond Myklebust 
1410ed90ef51STrond Myklebust /*
1411ed90ef51STrond Myklebust  * flush the inode to disk.
1412ed90ef51STrond Myklebust  */
1413ed90ef51STrond Myklebust int nfs_wb_all(struct inode *inode)
14141c75950bSTrond Myklebust {
1415ed90ef51STrond Myklebust 	return nfs_write_mapping(inode->i_mapping, 0);
1416ed90ef51STrond Myklebust }
14171c75950bSTrond Myklebust 
1418ed90ef51STrond Myklebust int nfs_wb_nocommit(struct inode *inode)
1419ed90ef51STrond Myklebust {
1420ed90ef51STrond Myklebust 	return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
14211c75950bSTrond Myklebust }
14221c75950bSTrond Myklebust 
14231b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
14241b3b4a1aSTrond Myklebust {
14251b3b4a1aSTrond Myklebust 	struct nfs_page *req;
14261b3b4a1aSTrond Myklebust 	loff_t range_start = page_offset(page);
14271b3b4a1aSTrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
14281b3b4a1aSTrond Myklebust 	struct writeback_control wbc = {
14291b3b4a1aSTrond Myklebust 		.bdi = page->mapping->backing_dev_info,
14301b3b4a1aSTrond Myklebust 		.sync_mode = WB_SYNC_ALL,
14311b3b4a1aSTrond Myklebust 		.nr_to_write = LONG_MAX,
14321b3b4a1aSTrond Myklebust 		.range_start = range_start,
14331b3b4a1aSTrond Myklebust 		.range_end = range_end,
14341b3b4a1aSTrond Myklebust 	};
14351b3b4a1aSTrond Myklebust 	int ret = 0;
14361b3b4a1aSTrond Myklebust 
14371b3b4a1aSTrond Myklebust 	BUG_ON(!PageLocked(page));
14381b3b4a1aSTrond Myklebust 	for (;;) {
14391b3b4a1aSTrond Myklebust 		req = nfs_page_find_request(page);
14401b3b4a1aSTrond Myklebust 		if (req == NULL)
14411b3b4a1aSTrond Myklebust 			goto out;
14421b3b4a1aSTrond Myklebust 		if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
14431b3b4a1aSTrond Myklebust 			nfs_release_request(req);
14441b3b4a1aSTrond Myklebust 			break;
14451b3b4a1aSTrond Myklebust 		}
14461b3b4a1aSTrond Myklebust 		if (nfs_lock_request_dontget(req)) {
14471b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
14481b3b4a1aSTrond Myklebust 			/*
14491b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
14501b3b4a1aSTrond Myklebust 			 * page as being dirty
14511b3b4a1aSTrond Myklebust 			 */
14521b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
14531b3b4a1aSTrond Myklebust 			nfs_unlock_request(req);
14541b3b4a1aSTrond Myklebust 			break;
14551b3b4a1aSTrond Myklebust 		}
14561b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
14571b3b4a1aSTrond Myklebust 		if (ret < 0)
14581b3b4a1aSTrond Myklebust 			goto out;
14591b3b4a1aSTrond Myklebust 	}
14601b3b4a1aSTrond Myklebust 	if (!PagePrivate(page))
14611b3b4a1aSTrond Myklebust 		return 0;
14621b3b4a1aSTrond Myklebust 	ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
14631b3b4a1aSTrond Myklebust out:
14641b3b4a1aSTrond Myklebust 	return ret;
14651b3b4a1aSTrond Myklebust }
14661b3b4a1aSTrond Myklebust 
14675334eb13SAdrian Bunk static int nfs_wb_page_priority(struct inode *inode, struct page *page,
14685334eb13SAdrian Bunk 				int how)
14691c75950bSTrond Myklebust {
14701c75950bSTrond Myklebust 	loff_t range_start = page_offset(page);
14711c75950bSTrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
14724d770ccfSTrond Myklebust 	struct writeback_control wbc = {
14734d770ccfSTrond Myklebust 		.bdi = page->mapping->backing_dev_info,
14744d770ccfSTrond Myklebust 		.sync_mode = WB_SYNC_ALL,
14754d770ccfSTrond Myklebust 		.nr_to_write = LONG_MAX,
14764d770ccfSTrond Myklebust 		.range_start = range_start,
14774d770ccfSTrond Myklebust 		.range_end = range_end,
14784d770ccfSTrond Myklebust 	};
14794d770ccfSTrond Myklebust 	int ret;
14801c75950bSTrond Myklebust 
14814d770ccfSTrond Myklebust 	BUG_ON(!PageLocked(page));
1482c63c7b05STrond Myklebust 	if (clear_page_dirty_for_io(page)) {
14834d770ccfSTrond Myklebust 		ret = nfs_writepage_locked(page, &wbc);
14844d770ccfSTrond Myklebust 		if (ret < 0)
14854d770ccfSTrond Myklebust 			goto out;
14864d770ccfSTrond Myklebust 	}
1487f40313acSTrond Myklebust 	if (!PagePrivate(page))
1488f40313acSTrond Myklebust 		return 0;
14894d770ccfSTrond Myklebust 	ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
14904d770ccfSTrond Myklebust 	if (ret >= 0)
14914d770ccfSTrond Myklebust 		return 0;
14924d770ccfSTrond Myklebust out:
1493e507d9ebSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_PAGES);
14944d770ccfSTrond Myklebust 	return ret;
14951c75950bSTrond Myklebust }
14961c75950bSTrond Myklebust 
14971c75950bSTrond Myklebust /*
14981c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
14991c75950bSTrond Myklebust  */
15001c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page* page)
15011c75950bSTrond Myklebust {
15024d770ccfSTrond Myklebust 	return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
15031c75950bSTrond Myklebust }
15041c75950bSTrond Myklebust 
1505f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
15061da177e4SLinus Torvalds {
15071da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
15081da177e4SLinus Torvalds 					     sizeof(struct nfs_write_data),
15091da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
151020c2df83SPaul Mundt 					     NULL);
15111da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
15121da177e4SLinus Torvalds 		return -ENOMEM;
15131da177e4SLinus Torvalds 
151493d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
15151da177e4SLinus Torvalds 						     nfs_wdata_cachep);
15161da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
15171da177e4SLinus Torvalds 		return -ENOMEM;
15181da177e4SLinus Torvalds 
151993d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
15201da177e4SLinus Torvalds 						      nfs_wdata_cachep);
15211da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
15221da177e4SLinus Torvalds 		return -ENOMEM;
15231da177e4SLinus Torvalds 
152489a09141SPeter Zijlstra 	/*
152589a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
152689a09141SPeter Zijlstra 	 *
152789a09141SPeter Zijlstra 	 *  64MB:    8192k
152889a09141SPeter Zijlstra 	 * 128MB:   11585k
152989a09141SPeter Zijlstra 	 * 256MB:   16384k
153089a09141SPeter Zijlstra 	 * 512MB:   23170k
153189a09141SPeter Zijlstra 	 *   1GB:   32768k
153289a09141SPeter Zijlstra 	 *   2GB:   46340k
153389a09141SPeter Zijlstra 	 *   4GB:   65536k
153489a09141SPeter Zijlstra 	 *   8GB:   92681k
153589a09141SPeter Zijlstra 	 *  16GB:  131072k
153689a09141SPeter Zijlstra 	 *
153789a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
153889a09141SPeter Zijlstra 	 * Limit the default to 256M
153989a09141SPeter Zijlstra 	 */
154089a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
154189a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
154289a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
154389a09141SPeter Zijlstra 
15441da177e4SLinus Torvalds 	return 0;
15451da177e4SLinus Torvalds }
15461da177e4SLinus Torvalds 
1547266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
15481da177e4SLinus Torvalds {
15491da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
15501da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
15511a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
15521da177e4SLinus Torvalds }
15531da177e4SLinus Torvalds 
1554