xref: /linux/fs/nfs/write.c (revision 0773769191d943358a8392fa86abd756d004c4b6)
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 
6110afec90STrond Myklebust static void nfs_commit_rcu_free(struct rcu_head *head)
621da177e4SLinus Torvalds {
638aca67f0STrond Myklebust 	struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
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 
698aca67f0STrond Myklebust void nfs_commit_free(struct nfs_write_data *wdata)
708aca67f0STrond Myklebust {
718aca67f0STrond Myklebust 	call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
728aca67f0STrond Myklebust }
738aca67f0STrond Myklebust 
748d5658c9STrond Myklebust struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
753feb2d49STrond Myklebust {
76e6b4f8daSChristoph Lameter 	struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
773feb2d49STrond Myklebust 
783feb2d49STrond Myklebust 	if (p) {
793feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
803feb2d49STrond Myklebust 		INIT_LIST_HEAD(&p->pages);
81e9f7bee1STrond Myklebust 		p->npages = pagecount;
820d0b5cb3SChuck Lever 		if (pagecount <= ARRAY_SIZE(p->page_array))
830d0b5cb3SChuck Lever 			p->pagevec = p->page_array;
843feb2d49STrond Myklebust 		else {
850d0b5cb3SChuck Lever 			p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
860d0b5cb3SChuck Lever 			if (!p->pagevec) {
873feb2d49STrond Myklebust 				mempool_free(p, nfs_wdata_mempool);
883feb2d49STrond Myklebust 				p = NULL;
893feb2d49STrond Myklebust 			}
903feb2d49STrond Myklebust 		}
913feb2d49STrond Myklebust 	}
923feb2d49STrond Myklebust 	return p;
933feb2d49STrond Myklebust }
943feb2d49STrond Myklebust 
958aca67f0STrond Myklebust static void nfs_writedata_rcu_free(struct rcu_head *head)
963feb2d49STrond Myklebust {
978aca67f0STrond Myklebust 	struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
983feb2d49STrond Myklebust 	if (p && (p->pagevec != &p->page_array[0]))
993feb2d49STrond Myklebust 		kfree(p->pagevec);
1003feb2d49STrond Myklebust 	mempool_free(p, nfs_wdata_mempool);
1013feb2d49STrond Myklebust }
1023feb2d49STrond Myklebust 
1038aca67f0STrond Myklebust static void nfs_writedata_free(struct nfs_write_data *wdata)
1048aca67f0STrond Myklebust {
1058aca67f0STrond Myklebust 	call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
1068aca67f0STrond Myklebust }
1078aca67f0STrond Myklebust 
108963d8fe5STrond Myklebust void nfs_writedata_release(void *wdata)
1091da177e4SLinus Torvalds {
1101da177e4SLinus Torvalds 	nfs_writedata_free(wdata);
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
1137b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
1147b159fc1STrond Myklebust {
1157b159fc1STrond Myklebust 	ctx->error = error;
1167b159fc1STrond Myklebust 	smp_wmb();
1177b159fc1STrond Myklebust 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
1187b159fc1STrond Myklebust }
1197b159fc1STrond Myklebust 
120277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request_locked(struct page *page)
121277459d2STrond Myklebust {
122277459d2STrond Myklebust 	struct nfs_page *req = NULL;
123277459d2STrond Myklebust 
124277459d2STrond Myklebust 	if (PagePrivate(page)) {
125277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
126277459d2STrond Myklebust 		if (req != NULL)
127c03b4024STrond Myklebust 			kref_get(&req->wb_kref);
128277459d2STrond Myklebust 	}
129277459d2STrond Myklebust 	return req;
130277459d2STrond Myklebust }
131277459d2STrond Myklebust 
132277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page)
133277459d2STrond Myklebust {
134587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
135277459d2STrond Myklebust 	struct nfs_page *req = NULL;
136277459d2STrond Myklebust 
137587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
138277459d2STrond Myklebust 	req = nfs_page_find_request_locked(page);
139587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
140277459d2STrond Myklebust 	return req;
141277459d2STrond Myklebust }
142277459d2STrond Myklebust 
1431da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1441da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1451da177e4SLinus Torvalds {
1461da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
1471da177e4SLinus Torvalds 	loff_t end, i_size = i_size_read(inode);
148ca52fec1STrond Myklebust 	pgoff_t end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	if (i_size > 0 && page->index < end_index)
1511da177e4SLinus Torvalds 		return;
1521da177e4SLinus Torvalds 	end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
1531da177e4SLinus Torvalds 	if (i_size >= end)
1541da177e4SLinus Torvalds 		return;
15591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
1561da177e4SLinus Torvalds 	i_size_write(inode, end);
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
159a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
160a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
161a301b777STrond Myklebust {
162a301b777STrond Myklebust 	SetPageError(page);
163a301b777STrond Myklebust 	nfs_zap_mapping(page->mapping->host, page->mapping);
164a301b777STrond Myklebust }
165a301b777STrond Myklebust 
1661da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
1671da177e4SLinus Torvalds  * covers the full page.
1681da177e4SLinus Torvalds  */
1691da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
1701da177e4SLinus Torvalds {
1711da177e4SLinus Torvalds 	if (PageUptodate(page))
1721da177e4SLinus Torvalds 		return;
1731da177e4SLinus Torvalds 	if (base != 0)
1741da177e4SLinus Torvalds 		return;
17549a70f27STrond Myklebust 	if (count != nfs_page_length(page))
1761da177e4SLinus Torvalds 		return;
1771da177e4SLinus Torvalds 	SetPageUptodate(page);
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds 
180e21195a7STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
1811da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
1821da177e4SLinus Torvalds {
1831da177e4SLinus Torvalds 	struct nfs_page	*req;
184e21195a7STrond Myklebust 	int ret;
1851da177e4SLinus Torvalds 
186e21195a7STrond Myklebust 	for (;;) {
187e21195a7STrond Myklebust 		req = nfs_update_request(ctx, page, offset, count);
188e21195a7STrond Myklebust 		if (!IS_ERR(req))
189e21195a7STrond Myklebust 			break;
190e21195a7STrond Myklebust 		ret = PTR_ERR(req);
191e21195a7STrond Myklebust 		if (ret != -EBUSY)
192e21195a7STrond Myklebust 			return ret;
193e21195a7STrond Myklebust 		ret = nfs_wb_page(page->mapping->host, page);
194e21195a7STrond Myklebust 		if (ret != 0)
195e21195a7STrond Myklebust 			return ret;
196e21195a7STrond Myklebust 	}
1971da177e4SLinus Torvalds 	/* Update file length */
1981da177e4SLinus Torvalds 	nfs_grow_file(page, offset, count);
199acee478aSTrond Myklebust 	nfs_clear_page_tag_locked(req);
200abd3e641STrond Myklebust 	return 0;
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
2041da177e4SLinus Torvalds {
2051da177e4SLinus Torvalds 	if (wbc->for_reclaim)
206c63c7b05STrond Myklebust 		return FLUSH_HIGHPRI | FLUSH_STABLE;
2071da177e4SLinus Torvalds 	if (wbc->for_kupdate)
2081da177e4SLinus Torvalds 		return FLUSH_LOWPRI;
2091da177e4SLinus Torvalds 	return 0;
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds /*
21389a09141SPeter Zijlstra  * NFS congestion control
21489a09141SPeter Zijlstra  */
21589a09141SPeter Zijlstra 
21689a09141SPeter Zijlstra int nfs_congestion_kb;
21789a09141SPeter Zijlstra 
21889a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
21989a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
22089a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
22189a09141SPeter Zijlstra 
2225a6d41b3STrond Myklebust static int nfs_set_page_writeback(struct page *page)
22389a09141SPeter Zijlstra {
2245a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
2255a6d41b3STrond Myklebust 
2265a6d41b3STrond Myklebust 	if (!ret) {
22789a09141SPeter Zijlstra 		struct inode *inode = page->mapping->host;
22889a09141SPeter Zijlstra 		struct nfs_server *nfss = NFS_SERVER(inode);
22989a09141SPeter Zijlstra 
230277866a0SPeter Zijlstra 		if (atomic_long_inc_return(&nfss->writeback) >
23189a09141SPeter Zijlstra 				NFS_CONGESTION_ON_THRESH)
23289a09141SPeter Zijlstra 			set_bdi_congested(&nfss->backing_dev_info, WRITE);
23389a09141SPeter Zijlstra 	}
2345a6d41b3STrond Myklebust 	return ret;
23589a09141SPeter Zijlstra }
23689a09141SPeter Zijlstra 
23789a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page)
23889a09141SPeter Zijlstra {
23989a09141SPeter Zijlstra 	struct inode *inode = page->mapping->host;
24089a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
24189a09141SPeter Zijlstra 
24289a09141SPeter Zijlstra 	end_page_writeback(page);
243c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
24489a09141SPeter Zijlstra 		clear_bdi_congested(&nfss->backing_dev_info, WRITE);
24589a09141SPeter Zijlstra }
24689a09141SPeter Zijlstra 
24789a09141SPeter Zijlstra /*
248e261f51fSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
2499cccef95STrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
250e261f51fSTrond Myklebust  */
251c63c7b05STrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
252c63c7b05STrond Myklebust 				struct page *page)
253e261f51fSTrond Myklebust {
254587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
255e261f51fSTrond Myklebust 	struct nfs_page *req;
256e261f51fSTrond Myklebust 	int ret;
257e261f51fSTrond Myklebust 
258587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
259e261f51fSTrond Myklebust 	for(;;) {
260e261f51fSTrond Myklebust 		req = nfs_page_find_request_locked(page);
261e261f51fSTrond Myklebust 		if (req == NULL) {
262587142f8STrond Myklebust 			spin_unlock(&inode->i_lock);
2639cccef95STrond Myklebust 			return 0;
264e261f51fSTrond Myklebust 		}
265acee478aSTrond Myklebust 		if (nfs_set_page_tag_locked(req))
266e261f51fSTrond Myklebust 			break;
267e261f51fSTrond Myklebust 		/* Note: If we hold the page lock, as is the case in nfs_writepage,
268acee478aSTrond Myklebust 		 *	 then the call to nfs_set_page_tag_locked() will always
269e261f51fSTrond Myklebust 		 *	 succeed provided that someone hasn't already marked the
270e261f51fSTrond Myklebust 		 *	 request as dirty (in which case we don't care).
271e261f51fSTrond Myklebust 		 */
272587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
273e261f51fSTrond Myklebust 		ret = nfs_wait_on_request(req);
274e261f51fSTrond Myklebust 		nfs_release_request(req);
275e261f51fSTrond Myklebust 		if (ret != 0)
276e261f51fSTrond Myklebust 			return ret;
277587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
278e261f51fSTrond Myklebust 	}
279612c9384STrond Myklebust 	if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
280612c9384STrond Myklebust 		/* This request is marked for commit */
281587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
282acee478aSTrond Myklebust 		nfs_clear_page_tag_locked(req);
283c63c7b05STrond Myklebust 		nfs_pageio_complete(pgio);
2849cccef95STrond Myklebust 		return 0;
285612c9384STrond Myklebust 	}
286c63c7b05STrond Myklebust 	if (nfs_set_page_writeback(page) != 0) {
287587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
288c63c7b05STrond Myklebust 		BUG();
289c63c7b05STrond Myklebust 	}
290587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
291c63c7b05STrond Myklebust 	nfs_pageio_add_request(pgio, req);
2929cccef95STrond Myklebust 	return 0;
293e261f51fSTrond Myklebust }
294e261f51fSTrond Myklebust 
295f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
296f758c885STrond Myklebust {
297f758c885STrond Myklebust 	struct inode *inode = page->mapping->host;
298f758c885STrond Myklebust 
299f758c885STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
300f758c885STrond Myklebust 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
301f758c885STrond Myklebust 
302f758c885STrond Myklebust 	nfs_pageio_cond_complete(pgio, page->index);
303f758c885STrond Myklebust 	return nfs_page_async_flush(pgio, page);
304f758c885STrond Myklebust }
305f758c885STrond Myklebust 
306e261f51fSTrond Myklebust /*
3071da177e4SLinus Torvalds  * Write an mmapped page to the server.
3081da177e4SLinus Torvalds  */
3094d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
3101da177e4SLinus Torvalds {
311f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
312e261f51fSTrond Myklebust 	int err;
3131da177e4SLinus Torvalds 
314f758c885STrond Myklebust 	nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
315f758c885STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
316f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
317f758c885STrond Myklebust 	if (err < 0)
3184d770ccfSTrond Myklebust 		return err;
319f758c885STrond Myklebust 	if (pgio.pg_error < 0)
320f758c885STrond Myklebust 		return pgio.pg_error;
321f758c885STrond Myklebust 	return 0;
3224d770ccfSTrond Myklebust }
3234d770ccfSTrond Myklebust 
3244d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
3254d770ccfSTrond Myklebust {
326f758c885STrond Myklebust 	int ret;
3274d770ccfSTrond Myklebust 
328f758c885STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
3291da177e4SLinus Torvalds 	unlock_page(page);
330f758c885STrond Myklebust 	return ret;
331f758c885STrond Myklebust }
332f758c885STrond Myklebust 
333f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
334f758c885STrond Myklebust {
335f758c885STrond Myklebust 	int ret;
336f758c885STrond Myklebust 
337f758c885STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
338f758c885STrond Myklebust 	unlock_page(page);
339f758c885STrond Myklebust 	return ret;
3401da177e4SLinus Torvalds }
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
345c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
3461da177e4SLinus Torvalds 	int err;
3471da177e4SLinus Torvalds 
34891d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
34991d5b470SChuck Lever 
350c63c7b05STrond Myklebust 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
351f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
352c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
353f758c885STrond Myklebust 	if (err < 0)
3541da177e4SLinus Torvalds 		return err;
355f758c885STrond Myklebust 	if (pgio.pg_error < 0)
356c63c7b05STrond Myklebust 		return pgio.pg_error;
357c63c7b05STrond Myklebust 	return 0;
3581da177e4SLinus Torvalds }
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds /*
3611da177e4SLinus Torvalds  * Insert a write request into an inode
3621da177e4SLinus Torvalds  */
3631da177e4SLinus Torvalds static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
3641da177e4SLinus Torvalds {
3651da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
3661da177e4SLinus Torvalds 	int error;
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
3691da177e4SLinus Torvalds 	BUG_ON(error == -EEXIST);
3701da177e4SLinus Torvalds 	if (error)
3711da177e4SLinus Torvalds 		return error;
3721da177e4SLinus Torvalds 	if (!nfsi->npages) {
3731da177e4SLinus Torvalds 		igrab(inode);
3741da177e4SLinus Torvalds 		if (nfs_have_delegation(inode, FMODE_WRITE))
3751da177e4SLinus Torvalds 			nfsi->change_attr++;
3761da177e4SLinus Torvalds 	}
377deb7d638STrond Myklebust 	SetPagePrivate(req->wb_page);
378277459d2STrond Myklebust 	set_page_private(req->wb_page, (unsigned long)req);
3791da177e4SLinus Torvalds 	nfsi->npages++;
380c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
381acee478aSTrond Myklebust 	radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_LOCKED);
3821da177e4SLinus Torvalds 	return 0;
3831da177e4SLinus Torvalds }
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds /*
38689a09141SPeter Zijlstra  * Remove a write request from an inode
3871da177e4SLinus Torvalds  */
3881da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
3891da177e4SLinus Torvalds {
39088be9f99STrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
3911da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	BUG_ON (!NFS_WBACK_BUSY(req));
3941da177e4SLinus Torvalds 
395587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
396277459d2STrond Myklebust 	set_page_private(req->wb_page, 0);
397deb7d638STrond Myklebust 	ClearPagePrivate(req->wb_page);
3981da177e4SLinus Torvalds 	radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
3991da177e4SLinus Torvalds 	nfsi->npages--;
4001da177e4SLinus Torvalds 	if (!nfsi->npages) {
401587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
4021da177e4SLinus Torvalds 		iput(inode);
4031da177e4SLinus Torvalds 	} else
404587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
4051da177e4SLinus Torvalds 	nfs_clear_request(req);
4061da177e4SLinus Torvalds 	nfs_release_request(req);
4071da177e4SLinus Torvalds }
4081da177e4SLinus Torvalds 
40961822ab5STrond Myklebust static void
41061822ab5STrond Myklebust nfs_redirty_request(struct nfs_page *req)
41161822ab5STrond Myklebust {
41261822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
41361822ab5STrond Myklebust }
41461822ab5STrond Myklebust 
4151da177e4SLinus Torvalds /*
4161da177e4SLinus Torvalds  * Check if a request is dirty
4171da177e4SLinus Torvalds  */
4181da177e4SLinus Torvalds static inline int
4191da177e4SLinus Torvalds nfs_dirty_request(struct nfs_page *req)
4201da177e4SLinus Torvalds {
4215a6d41b3STrond Myklebust 	struct page *page = req->wb_page;
4225a6d41b3STrond Myklebust 
423612c9384STrond Myklebust 	if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
4245a6d41b3STrond Myklebust 		return 0;
4255a6d41b3STrond Myklebust 	return !PageWriteback(req->wb_page);
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
4291da177e4SLinus Torvalds /*
4301da177e4SLinus Torvalds  * Add a request to the inode's commit list.
4311da177e4SLinus Torvalds  */
4321da177e4SLinus Torvalds static void
4331da177e4SLinus Torvalds nfs_mark_request_commit(struct nfs_page *req)
4341da177e4SLinus Torvalds {
43588be9f99STrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
4361da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4371da177e4SLinus Torvalds 
438587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
4391da177e4SLinus Torvalds 	nfsi->ncommit++;
440612c9384STrond Myklebust 	set_bit(PG_NEED_COMMIT, &(req)->wb_flags);
4415c369683STrond Myklebust 	radix_tree_tag_set(&nfsi->nfs_page_tree,
4425c369683STrond Myklebust 			req->wb_index,
4435c369683STrond Myklebust 			NFS_PAGE_TAG_COMMIT);
444587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
445fd39fc85SChristoph Lameter 	inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
446c9e51e41SPeter Zijlstra 	inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
447a1803044STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
4481da177e4SLinus Torvalds }
4498e821cadSTrond Myklebust 
4508e821cadSTrond Myklebust static inline
4518e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
4528e821cadSTrond Myklebust {
4538e821cadSTrond Myklebust 	return data->verf.committed != NFS_FILE_SYNC;
4548e821cadSTrond Myklebust }
4558e821cadSTrond Myklebust 
4568e821cadSTrond Myklebust static inline
4578e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req)
4588e821cadSTrond Myklebust {
459612c9384STrond Myklebust 	if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
4608e821cadSTrond Myklebust 		nfs_mark_request_commit(req);
4618e821cadSTrond Myklebust 		return 1;
4628e821cadSTrond Myklebust 	}
4638e821cadSTrond Myklebust 	if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
4648e821cadSTrond Myklebust 		nfs_redirty_request(req);
4658e821cadSTrond Myklebust 		return 1;
4668e821cadSTrond Myklebust 	}
4678e821cadSTrond Myklebust 	return 0;
4688e821cadSTrond Myklebust }
4698e821cadSTrond Myklebust #else
4708e821cadSTrond Myklebust static inline void
4718e821cadSTrond Myklebust nfs_mark_request_commit(struct nfs_page *req)
4728e821cadSTrond Myklebust {
4738e821cadSTrond Myklebust }
4748e821cadSTrond Myklebust 
4758e821cadSTrond Myklebust static inline
4768e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
4778e821cadSTrond Myklebust {
4788e821cadSTrond Myklebust 	return 0;
4798e821cadSTrond Myklebust }
4808e821cadSTrond Myklebust 
4818e821cadSTrond Myklebust static inline
4828e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req)
4838e821cadSTrond Myklebust {
4848e821cadSTrond Myklebust 	return 0;
4858e821cadSTrond Myklebust }
4861da177e4SLinus Torvalds #endif
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds /*
4891da177e4SLinus Torvalds  * Wait for a request to complete.
4901da177e4SLinus Torvalds  *
4911da177e4SLinus Torvalds  * Interruptible by signals only if mounted with intr flag.
4921da177e4SLinus Torvalds  */
493ca52fec1STrond Myklebust static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
4941da177e4SLinus Torvalds {
4951da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4961da177e4SLinus Torvalds 	struct nfs_page *req;
497ca52fec1STrond Myklebust 	pgoff_t idx_end, next;
4981da177e4SLinus Torvalds 	unsigned int		res = 0;
4991da177e4SLinus Torvalds 	int			error;
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	if (npages == 0)
5021da177e4SLinus Torvalds 		idx_end = ~0;
5031da177e4SLinus Torvalds 	else
5041da177e4SLinus Torvalds 		idx_end = idx_start + npages - 1;
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 	next = idx_start;
5079fd367f0STrond Myklebust 	while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) {
5081da177e4SLinus Torvalds 		if (req->wb_index > idx_end)
5091da177e4SLinus Torvalds 			break;
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 		next = req->wb_index + 1;
512c6a556b8STrond Myklebust 		BUG_ON(!NFS_WBACK_BUSY(req));
5131da177e4SLinus Torvalds 
514c03b4024STrond Myklebust 		kref_get(&req->wb_kref);
515587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
5161da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
5171da177e4SLinus Torvalds 		nfs_release_request(req);
518587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
5191da177e4SLinus Torvalds 		if (error < 0)
5201da177e4SLinus Torvalds 			return error;
5211da177e4SLinus Torvalds 		res++;
5221da177e4SLinus Torvalds 	}
5231da177e4SLinus Torvalds 	return res;
5241da177e4SLinus Torvalds }
5251da177e4SLinus Torvalds 
52683715ad5STrond Myklebust static void nfs_cancel_commit_list(struct list_head *head)
52783715ad5STrond Myklebust {
52883715ad5STrond Myklebust 	struct nfs_page *req;
52983715ad5STrond Myklebust 
53083715ad5STrond Myklebust 	while(!list_empty(head)) {
53183715ad5STrond Myklebust 		req = nfs_list_entry(head->next);
532b6dff26aSTrond Myklebust 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
533c9e51e41SPeter Zijlstra 		dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
534c9e51e41SPeter Zijlstra 				BDI_RECLAIMABLE);
53583715ad5STrond Myklebust 		nfs_list_remove_request(req);
536612c9384STrond Myklebust 		clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
53783715ad5STrond Myklebust 		nfs_inode_remove_request(req);
538b6dff26aSTrond Myklebust 		nfs_unlock_request(req);
53983715ad5STrond Myklebust 	}
54083715ad5STrond Myklebust }
54183715ad5STrond Myklebust 
5421da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
5431da177e4SLinus Torvalds /*
5441da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
5451da177e4SLinus Torvalds  * @inode: NFS inode to scan
5461da177e4SLinus Torvalds  * @dst: destination list
5471da177e4SLinus Torvalds  * @idx_start: lower bound of page->index to scan.
5481da177e4SLinus Torvalds  * @npages: idx_start + npages sets the upper bound to scan.
5491da177e4SLinus Torvalds  *
5501da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
5511da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
5521da177e4SLinus Torvalds  */
5531da177e4SLinus Torvalds static int
554ca52fec1STrond Myklebust nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
5551da177e4SLinus Torvalds {
5561da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
5573da28eb1STrond Myklebust 	int res = 0;
5583da28eb1STrond Myklebust 
5593da28eb1STrond Myklebust 	if (nfsi->ncommit != 0) {
5605c369683STrond Myklebust 		res = nfs_scan_list(nfsi, dst, idx_start, npages,
5615c369683STrond Myklebust 				NFS_PAGE_TAG_COMMIT);
5621da177e4SLinus Torvalds 		nfsi->ncommit -= res;
5633da28eb1STrond Myklebust 	}
5641da177e4SLinus Torvalds 	return res;
5651da177e4SLinus Torvalds }
566c42de9ddSTrond Myklebust #else
567ca52fec1STrond Myklebust static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
568c42de9ddSTrond Myklebust {
569c42de9ddSTrond Myklebust 	return 0;
570c42de9ddSTrond Myklebust }
5711da177e4SLinus Torvalds #endif
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds /*
5741da177e4SLinus Torvalds  * Try to update any existing write request, or create one if there is none.
5751da177e4SLinus Torvalds  * In order to match, the request's credentials must match those of
5761da177e4SLinus Torvalds  * the calling process.
5771da177e4SLinus Torvalds  *
5781da177e4SLinus Torvalds  * Note: Should always be called with the Page Lock held!
5791da177e4SLinus Torvalds  */
5801da177e4SLinus Torvalds static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
581e21195a7STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
5821da177e4SLinus Torvalds {
58389a09141SPeter Zijlstra 	struct address_space *mapping = page->mapping;
58489a09141SPeter Zijlstra 	struct inode *inode = mapping->host;
5851da177e4SLinus Torvalds 	struct nfs_page		*req, *new = NULL;
586ca52fec1STrond Myklebust 	pgoff_t		rqend, end;
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 	end = offset + bytes;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	for (;;) {
5911da177e4SLinus Torvalds 		/* Loop over all inode entries and see if we find
5921da177e4SLinus Torvalds 		 * A request for the page we wish to update
5931da177e4SLinus Torvalds 		 */
594587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
595277459d2STrond Myklebust 		req = nfs_page_find_request_locked(page);
5961da177e4SLinus Torvalds 		if (req) {
597acee478aSTrond Myklebust 			if (!nfs_set_page_tag_locked(req)) {
5981da177e4SLinus Torvalds 				int error;
599277459d2STrond Myklebust 
600587142f8STrond Myklebust 				spin_unlock(&inode->i_lock);
6011da177e4SLinus Torvalds 				error = nfs_wait_on_request(req);
6021da177e4SLinus Torvalds 				nfs_release_request(req);
6031dd594b2SNeil Brown 				if (error < 0) {
6041dd594b2SNeil Brown 					if (new)
6051dd594b2SNeil Brown 						nfs_release_request(new);
6061da177e4SLinus Torvalds 					return ERR_PTR(error);
6071dd594b2SNeil Brown 				}
6081da177e4SLinus Torvalds 				continue;
6091da177e4SLinus Torvalds 			}
610587142f8STrond Myklebust 			spin_unlock(&inode->i_lock);
6111da177e4SLinus Torvalds 			if (new)
6121da177e4SLinus Torvalds 				nfs_release_request(new);
6131da177e4SLinus Torvalds 			break;
6141da177e4SLinus Torvalds 		}
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 		if (new) {
6171da177e4SLinus Torvalds 			int error;
6181da177e4SLinus Torvalds 			nfs_lock_request_dontget(new);
6191da177e4SLinus Torvalds 			error = nfs_inode_add_request(inode, new);
6201da177e4SLinus Torvalds 			if (error) {
621587142f8STrond Myklebust 				spin_unlock(&inode->i_lock);
6221da177e4SLinus Torvalds 				nfs_unlock_request(new);
6231da177e4SLinus Torvalds 				return ERR_PTR(error);
6241da177e4SLinus Torvalds 			}
625587142f8STrond Myklebust 			spin_unlock(&inode->i_lock);
62661e930a9STrond Myklebust 			req = new;
62761e930a9STrond Myklebust 			goto zero_page;
6281da177e4SLinus Torvalds 		}
629587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 		new = nfs_create_request(ctx, inode, page, offset, bytes);
6321da177e4SLinus Torvalds 		if (IS_ERR(new))
6331da177e4SLinus Torvalds 			return new;
6341da177e4SLinus Torvalds 	}
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 	/* We have a request for our page.
6371da177e4SLinus Torvalds 	 * If the creds don't match, or the
6381da177e4SLinus Torvalds 	 * page addresses don't match,
6391da177e4SLinus Torvalds 	 * tell the caller to wait on the conflicting
6401da177e4SLinus Torvalds 	 * request.
6411da177e4SLinus Torvalds 	 */
6421da177e4SLinus Torvalds 	rqend = req->wb_offset + req->wb_bytes;
6431da177e4SLinus Torvalds 	if (req->wb_context != ctx
6441da177e4SLinus Torvalds 	    || req->wb_page != page
6451da177e4SLinus Torvalds 	    || !nfs_dirty_request(req)
6461da177e4SLinus Torvalds 	    || offset > rqend || end < req->wb_offset) {
647acee478aSTrond Myklebust 		nfs_clear_page_tag_locked(req);
6481da177e4SLinus Torvalds 		return ERR_PTR(-EBUSY);
6491da177e4SLinus Torvalds 	}
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
6521da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
6531da177e4SLinus Torvalds 		req->wb_offset = offset;
6541da177e4SLinus Torvalds 		req->wb_pgbase = offset;
65561e930a9STrond Myklebust 		req->wb_bytes = max(end, rqend) - req->wb_offset;
65661e930a9STrond Myklebust 		goto zero_page;
6571da177e4SLinus Torvalds 	}
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	if (end > rqend)
6601da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 	return req;
66361e930a9STrond Myklebust zero_page:
66461e930a9STrond Myklebust 	/* If this page might potentially be marked as up to date,
66561e930a9STrond Myklebust 	 * then we need to zero any uninitalised data. */
66661e930a9STrond Myklebust 	if (req->wb_pgbase == 0 && req->wb_bytes != PAGE_CACHE_SIZE
66761e930a9STrond Myklebust 			&& !PageUptodate(req->wb_page))
66861e930a9STrond Myklebust 		zero_user_page(req->wb_page, req->wb_bytes,
66961e930a9STrond Myklebust 				PAGE_CACHE_SIZE - req->wb_bytes,
67061e930a9STrond Myklebust 				KM_USER0);
67161e930a9STrond Myklebust 	return req;
6721da177e4SLinus Torvalds }
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
6751da177e4SLinus Torvalds {
676cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
6771da177e4SLinus Torvalds 	struct nfs_page	*req;
6781a54533eSTrond Myklebust 	int do_flush, status;
6791da177e4SLinus Torvalds 	/*
6801da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
6811da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
6821da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
6831da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
6841da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
6851da177e4SLinus Torvalds 	 * dropped page.
6861da177e4SLinus Torvalds 	 */
6871a54533eSTrond Myklebust 	do {
688277459d2STrond Myklebust 		req = nfs_page_find_request(page);
6891a54533eSTrond Myklebust 		if (req == NULL)
6901a54533eSTrond Myklebust 			return 0;
6911a54533eSTrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx
692e261f51fSTrond Myklebust 			|| !nfs_dirty_request(req);
6931da177e4SLinus Torvalds 		nfs_release_request(req);
6941a54533eSTrond Myklebust 		if (!do_flush)
6951a54533eSTrond Myklebust 			return 0;
696277459d2STrond Myklebust 		status = nfs_wb_page(page->mapping->host, page);
6971a54533eSTrond Myklebust 	} while (status == 0);
6981a54533eSTrond Myklebust 	return status;
6991da177e4SLinus Torvalds }
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds /*
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
7221da177e4SLinus Torvalds 	 * is entirely in cache, it may be more efficient to avoid
7231da177e4SLinus Torvalds 	 * fragmenting write requests.
7241da177e4SLinus Torvalds 	 */
725ab0a3dbeSTrond Myklebust 	if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
72649a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
7271da177e4SLinus Torvalds 		offset = 0;
7281da177e4SLinus Torvalds 	}
7291da177e4SLinus Torvalds 
730e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
731e261f51fSTrond Myklebust 	__set_page_dirty_nobuffers(page);
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds         dprintk("NFS:      nfs_updatepage returns %d (isize %Ld)\n",
7341da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
7351da177e4SLinus Torvalds 	if (status < 0)
736a301b777STrond Myklebust 		nfs_set_pageerror(page);
7371da177e4SLinus Torvalds 	return status;
7381da177e4SLinus Torvalds }
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds static void nfs_writepage_release(struct nfs_page *req)
7411da177e4SLinus Torvalds {
7428e821cadSTrond Myklebust 
74344dd151dSTrond Myklebust 	if (PageError(req->wb_page)) {
74444dd151dSTrond Myklebust 		nfs_end_page_writeback(req->wb_page);
74544dd151dSTrond Myklebust 		nfs_inode_remove_request(req);
74644dd151dSTrond Myklebust 	} else if (!nfs_reschedule_unstable_write(req)) {
74744dd151dSTrond Myklebust 		/* Set the PG_uptodate flag */
74844dd151dSTrond Myklebust 		nfs_mark_uptodate(req->wb_page, req->wb_pgbase, req->wb_bytes);
74989a09141SPeter Zijlstra 		nfs_end_page_writeback(req->wb_page);
7501da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
7518e821cadSTrond Myklebust 	} else
7528e821cadSTrond Myklebust 		nfs_end_page_writeback(req->wb_page);
7539fd367f0STrond Myklebust 	nfs_clear_page_tag_locked(req);
7541da177e4SLinus Torvalds }
7551da177e4SLinus Torvalds 
7563ff7576dSTrond Myklebust static int flush_task_priority(int how)
7571da177e4SLinus Torvalds {
7581da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
7591da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
7601da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
7611da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
7621da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
7631da177e4SLinus Torvalds 	}
7641da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
7651da177e4SLinus Torvalds }
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds /*
7681da177e4SLinus Torvalds  * Set up the argument/result storage required for the RPC call.
7691da177e4SLinus Torvalds  */
7701da177e4SLinus Torvalds static void nfs_write_rpcsetup(struct nfs_page *req,
7711da177e4SLinus Torvalds 		struct nfs_write_data *data,
772788e7a89STrond Myklebust 		const struct rpc_call_ops *call_ops,
7731da177e4SLinus Torvalds 		unsigned int count, unsigned int offset,
7741da177e4SLinus Torvalds 		int how)
7751da177e4SLinus Torvalds {
77684115e1cSTrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
77784115e1cSTrond Myklebust 	int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
7783ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
779*07737691STrond Myklebust 	struct rpc_task *task;
780bdc7f021STrond Myklebust 	struct rpc_message msg = {
781bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
782bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
783bdc7f021STrond Myklebust 		.rpc_cred = req->wb_context->cred,
784bdc7f021STrond Myklebust 	};
78584115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
78684115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(inode),
787*07737691STrond Myklebust 		.task = &data->task,
788bdc7f021STrond Myklebust 		.rpc_message = &msg,
78984115e1cSTrond Myklebust 		.callback_ops = call_ops,
79084115e1cSTrond Myklebust 		.callback_data = data,
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;
8071da177e4SLinus Torvalds 	data->args.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 
831*07737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
832*07737691STrond Myklebust 	if (!IS_ERR(task))
833*07737691STrond Myklebust 		rpc_put_task(task);
8341da177e4SLinus Torvalds }
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds /*
8371da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
8381da177e4SLinus Torvalds  * contiguous dirty area on one page.
8391da177e4SLinus Torvalds  */
8408d5658c9STrond Myklebust static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
8411da177e4SLinus Torvalds {
8421da177e4SLinus Torvalds 	struct nfs_page *req = nfs_list_entry(head->next);
8431da177e4SLinus Torvalds 	struct page *page = req->wb_page;
8441da177e4SLinus Torvalds 	struct nfs_write_data *data;
845e9f7bee1STrond Myklebust 	size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
846e9f7bee1STrond Myklebust 	unsigned int offset;
8471da177e4SLinus Torvalds 	int requests = 0;
8481da177e4SLinus Torvalds 	LIST_HEAD(list);
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 	nfs_list_remove_request(req);
8511da177e4SLinus Torvalds 
852bcb71bbaSTrond Myklebust 	nbytes = count;
853e9f7bee1STrond Myklebust 	do {
854e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
855e9f7bee1STrond Myklebust 
8568d5658c9STrond Myklebust 		data = nfs_writedata_alloc(1);
8571da177e4SLinus Torvalds 		if (!data)
8581da177e4SLinus Torvalds 			goto out_bad;
8591da177e4SLinus Torvalds 		list_add(&data->pages, &list);
8601da177e4SLinus Torvalds 		requests++;
861e9f7bee1STrond Myklebust 		nbytes -= len;
862e9f7bee1STrond Myklebust 	} while (nbytes != 0);
8631da177e4SLinus Torvalds 	atomic_set(&req->wb_complete, requests);
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	ClearPageError(page);
8661da177e4SLinus Torvalds 	offset = 0;
867bcb71bbaSTrond Myklebust 	nbytes = count;
8681da177e4SLinus Torvalds 	do {
8691da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
8701da177e4SLinus Torvalds 		list_del_init(&data->pages);
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 		data->pagevec[0] = page;
8731da177e4SLinus Torvalds 
874bcb71bbaSTrond Myklebust 		if (nbytes < wsize)
875bcb71bbaSTrond Myklebust 			wsize = nbytes;
876788e7a89STrond Myklebust 		nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
877788e7a89STrond Myklebust 				   wsize, offset, how);
8781da177e4SLinus Torvalds 		offset += wsize;
8791da177e4SLinus Torvalds 		nbytes -= wsize;
8801da177e4SLinus Torvalds 	} while (nbytes != 0);
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	return 0;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds out_bad:
8851da177e4SLinus Torvalds 	while (!list_empty(&list)) {
8861da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
8871da177e4SLinus Torvalds 		list_del(&data->pages);
8888aca67f0STrond Myklebust 		nfs_writedata_release(data);
8891da177e4SLinus Torvalds 	}
89061822ab5STrond Myklebust 	nfs_redirty_request(req);
8916d677e35STrond Myklebust 	nfs_end_page_writeback(req->wb_page);
8929fd367f0STrond Myklebust 	nfs_clear_page_tag_locked(req);
8931da177e4SLinus Torvalds 	return -ENOMEM;
8941da177e4SLinus Torvalds }
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds /*
8971da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
8981da177e4SLinus Torvalds  * The page must have been locked by the caller.
8991da177e4SLinus Torvalds  *
9001da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
9011da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
9021da177e4SLinus Torvalds  * that has been written but not committed.
9031da177e4SLinus Torvalds  */
9048d5658c9STrond Myklebust static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
9051da177e4SLinus Torvalds {
9061da177e4SLinus Torvalds 	struct nfs_page		*req;
9071da177e4SLinus Torvalds 	struct page		**pages;
9081da177e4SLinus Torvalds 	struct nfs_write_data	*data;
9091da177e4SLinus Torvalds 
9108d5658c9STrond Myklebust 	data = nfs_writedata_alloc(npages);
9111da177e4SLinus Torvalds 	if (!data)
9121da177e4SLinus Torvalds 		goto out_bad;
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 	pages = data->pagevec;
9151da177e4SLinus Torvalds 	while (!list_empty(head)) {
9161da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
9171da177e4SLinus Torvalds 		nfs_list_remove_request(req);
9181da177e4SLinus Torvalds 		nfs_list_add_request(req, &data->pages);
9191da177e4SLinus Torvalds 		ClearPageError(req->wb_page);
9201da177e4SLinus Torvalds 		*pages++ = req->wb_page;
9211da177e4SLinus Torvalds 	}
9221da177e4SLinus Torvalds 	req = nfs_list_entry(data->pages.next);
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds 	/* Set up the argument struct */
925788e7a89STrond Myklebust 	nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	return 0;
9281da177e4SLinus Torvalds  out_bad:
9291da177e4SLinus Torvalds 	while (!list_empty(head)) {
93010afec90STrond Myklebust 		req = nfs_list_entry(head->next);
9311da177e4SLinus Torvalds 		nfs_list_remove_request(req);
93261822ab5STrond Myklebust 		nfs_redirty_request(req);
9336d677e35STrond Myklebust 		nfs_end_page_writeback(req->wb_page);
9349fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
9351da177e4SLinus Torvalds 	}
9361da177e4SLinus Torvalds 	return -ENOMEM;
9371da177e4SLinus Torvalds }
9381da177e4SLinus Torvalds 
939c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
940c63c7b05STrond Myklebust 				  struct inode *inode, int ioflags)
9411da177e4SLinus Torvalds {
9427d46a49fSTrond Myklebust 	int wsize = NFS_SERVER(inode)->wsize;
9431da177e4SLinus Torvalds 
944bcb71bbaSTrond Myklebust 	if (wsize < PAGE_CACHE_SIZE)
945c63c7b05STrond Myklebust 		nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
946bcb71bbaSTrond Myklebust 	else
947c63c7b05STrond Myklebust 		nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
9481da177e4SLinus Torvalds }
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds /*
9511da177e4SLinus Torvalds  * Handle a write reply that flushed part of a page.
9521da177e4SLinus Torvalds  */
953788e7a89STrond Myklebust static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
9541da177e4SLinus Torvalds {
955788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
9561da177e4SLinus Torvalds 	struct nfs_page		*req = data->req;
9571da177e4SLinus Torvalds 	struct page		*page = req->wb_page;
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds 	dprintk("NFS: write (%s/%Ld %d@%Ld)",
96088be9f99STrond Myklebust 		req->wb_context->path.dentry->d_inode->i_sb->s_id,
96188be9f99STrond Myklebust 		(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
9621da177e4SLinus Torvalds 		req->wb_bytes,
9631da177e4SLinus Torvalds 		(long long)req_offset(req));
9641da177e4SLinus Torvalds 
965788e7a89STrond Myklebust 	if (nfs_writeback_done(task, data) != 0)
966788e7a89STrond Myklebust 		return;
967788e7a89STrond Myklebust 
968788e7a89STrond Myklebust 	if (task->tk_status < 0) {
969a301b777STrond Myklebust 		nfs_set_pageerror(page);
9707b159fc1STrond Myklebust 		nfs_context_set_write_error(req->wb_context, task->tk_status);
971788e7a89STrond Myklebust 		dprintk(", error = %d\n", task->tk_status);
9728e821cadSTrond Myklebust 		goto out;
9738e821cadSTrond Myklebust 	}
9748e821cadSTrond Myklebust 
9758e821cadSTrond Myklebust 	if (nfs_write_need_commit(data)) {
976587142f8STrond Myklebust 		struct inode *inode = page->mapping->host;
9778e821cadSTrond Myklebust 
978587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
9798e821cadSTrond Myklebust 		if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
9808e821cadSTrond Myklebust 			/* Do nothing we need to resend the writes */
9818e821cadSTrond Myklebust 		} else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
9821da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
9831da177e4SLinus Torvalds 			dprintk(" defer commit\n");
9841da177e4SLinus Torvalds 		} else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
9858e821cadSTrond Myklebust 			set_bit(PG_NEED_RESCHED, &req->wb_flags);
9868e821cadSTrond Myklebust 			clear_bit(PG_NEED_COMMIT, &req->wb_flags);
9871da177e4SLinus Torvalds 			dprintk(" server reboot detected\n");
9881da177e4SLinus Torvalds 		}
989587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
9901da177e4SLinus Torvalds 	} else
9911da177e4SLinus Torvalds 		dprintk(" OK\n");
9921da177e4SLinus Torvalds 
9938e821cadSTrond Myklebust out:
9941da177e4SLinus Torvalds 	if (atomic_dec_and_test(&req->wb_complete))
9951da177e4SLinus Torvalds 		nfs_writepage_release(req);
9961da177e4SLinus Torvalds }
9971da177e4SLinus Torvalds 
998788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops = {
999788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_partial,
1000788e7a89STrond Myklebust 	.rpc_release = nfs_writedata_release,
1001788e7a89STrond Myklebust };
1002788e7a89STrond Myklebust 
10031da177e4SLinus Torvalds /*
10041da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
10051da177e4SLinus Torvalds  *
10061da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
10071da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
10081da177e4SLinus Torvalds  *	  as the page has a write request pending.
10091da177e4SLinus Torvalds  */
1010788e7a89STrond Myklebust static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
10111da177e4SLinus Torvalds {
1012788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
10131da177e4SLinus Torvalds 	struct nfs_page		*req;
10141da177e4SLinus Torvalds 	struct page		*page;
10151da177e4SLinus Torvalds 
1016788e7a89STrond Myklebust 	if (nfs_writeback_done(task, data) != 0)
1017788e7a89STrond Myklebust 		return;
1018788e7a89STrond Myklebust 
10191da177e4SLinus Torvalds 	/* Update attributes as result of writeback. */
10201da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
10211da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
10221da177e4SLinus Torvalds 		nfs_list_remove_request(req);
10231da177e4SLinus Torvalds 		page = req->wb_page;
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 		dprintk("NFS: write (%s/%Ld %d@%Ld)",
102688be9f99STrond Myklebust 			req->wb_context->path.dentry->d_inode->i_sb->s_id,
102788be9f99STrond Myklebust 			(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
10281da177e4SLinus Torvalds 			req->wb_bytes,
10291da177e4SLinus Torvalds 			(long long)req_offset(req));
10301da177e4SLinus Torvalds 
1031788e7a89STrond Myklebust 		if (task->tk_status < 0) {
1032a301b777STrond Myklebust 			nfs_set_pageerror(page);
10337b159fc1STrond Myklebust 			nfs_context_set_write_error(req->wb_context, task->tk_status);
1034788e7a89STrond Myklebust 			dprintk(", error = %d\n", task->tk_status);
10358e821cadSTrond Myklebust 			goto remove_request;
10361da177e4SLinus Torvalds 		}
10371da177e4SLinus Torvalds 
10388e821cadSTrond Myklebust 		if (nfs_write_need_commit(data)) {
10391da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
10401da177e4SLinus Torvalds 			nfs_mark_request_commit(req);
10418e821cadSTrond Myklebust 			nfs_end_page_writeback(page);
10421da177e4SLinus Torvalds 			dprintk(" marked for commit\n");
10438e821cadSTrond Myklebust 			goto next;
10448e821cadSTrond Myklebust 		}
104544dd151dSTrond Myklebust 		/* Set the PG_uptodate flag? */
104644dd151dSTrond Myklebust 		nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
10478e821cadSTrond Myklebust 		dprintk(" OK\n");
10488e821cadSTrond Myklebust remove_request:
10498e821cadSTrond Myklebust 		nfs_end_page_writeback(page);
10501da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
10511da177e4SLinus Torvalds 	next:
10529fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
10531da177e4SLinus Torvalds 	}
10541da177e4SLinus Torvalds }
10551da177e4SLinus Torvalds 
1056788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops = {
1057788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_full,
1058788e7a89STrond Myklebust 	.rpc_release = nfs_writedata_release,
1059788e7a89STrond Myklebust };
1060788e7a89STrond Myklebust 
1061788e7a89STrond Myklebust 
10621da177e4SLinus Torvalds /*
10631da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
10641da177e4SLinus Torvalds  */
1065462d5b32SChuck Lever int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
10661da177e4SLinus Torvalds {
10671da177e4SLinus Torvalds 	struct nfs_writeargs	*argp = &data->args;
10681da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
1069788e7a89STrond Myklebust 	int status;
10701da177e4SLinus Torvalds 
1071a3f565b1SChuck Lever 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
10721da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
10731da177e4SLinus Torvalds 
1074f551e44fSChuck Lever 	/*
1075f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1076f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1077f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1078f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1079f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1080f551e44fSChuck Lever 	 */
1081788e7a89STrond Myklebust 	status = NFS_PROTO(data->inode)->write_done(task, data);
1082788e7a89STrond Myklebust 	if (status != 0)
1083788e7a89STrond Myklebust 		return status;
108491d5b470SChuck Lever 	nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
108591d5b470SChuck Lever 
10861da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
10871da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
10881da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
10891da177e4SLinus Torvalds 		 * commit data to stable storage even though we
10901da177e4SLinus Torvalds 		 * requested it.
10911da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
10921da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
10931da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
10941da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
10951da177e4SLinus Torvalds 		 */
10961da177e4SLinus Torvalds 		static unsigned long    complain;
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
10991da177e4SLinus Torvalds 			dprintk("NFS: faulty NFS server %s:"
11001da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
110154ceac45SDavid Howells 				NFS_SERVER(data->inode)->nfs_client->cl_hostname,
11021da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
11031da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
11041da177e4SLinus Torvalds 		}
11051da177e4SLinus Torvalds 	}
11061da177e4SLinus Torvalds #endif
11071da177e4SLinus Torvalds 	/* Is this a short write? */
11081da177e4SLinus Torvalds 	if (task->tk_status >= 0 && resp->count < argp->count) {
11091da177e4SLinus Torvalds 		static unsigned long    complain;
11101da177e4SLinus Torvalds 
111191d5b470SChuck Lever 		nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
111291d5b470SChuck Lever 
11131da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
11141da177e4SLinus Torvalds 		if (resp->count != 0) {
11151da177e4SLinus Torvalds 			/* Was this an NFSv2 write or an NFSv3 stable write? */
11161da177e4SLinus Torvalds 			if (resp->verf->committed != NFS_UNSTABLE) {
11171da177e4SLinus Torvalds 				/* Resend from where the server left off */
11181da177e4SLinus Torvalds 				argp->offset += resp->count;
11191da177e4SLinus Torvalds 				argp->pgbase += resp->count;
11201da177e4SLinus Torvalds 				argp->count -= resp->count;
11211da177e4SLinus Torvalds 			} else {
11221da177e4SLinus Torvalds 				/* Resend as a stable write in order to avoid
11231da177e4SLinus Torvalds 				 * headaches in the case of a server crash.
11241da177e4SLinus Torvalds 				 */
11251da177e4SLinus Torvalds 				argp->stable = NFS_FILE_SYNC;
11261da177e4SLinus Torvalds 			}
11271da177e4SLinus Torvalds 			rpc_restart_call(task);
1128788e7a89STrond Myklebust 			return -EAGAIN;
11291da177e4SLinus Torvalds 		}
11301da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
11311da177e4SLinus Torvalds 			printk(KERN_WARNING
11321da177e4SLinus Torvalds 			       "NFS: Server wrote zero bytes, expected %u.\n",
11331da177e4SLinus Torvalds 					argp->count);
11341da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
11351da177e4SLinus Torvalds 		}
11361da177e4SLinus Torvalds 		/* Can't do anything about it except throw an error. */
11371da177e4SLinus Torvalds 		task->tk_status = -EIO;
11381da177e4SLinus Torvalds 	}
1139788e7a89STrond Myklebust 	return 0;
11401da177e4SLinus Torvalds }
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1144963d8fe5STrond Myklebust void nfs_commit_release(void *wdata)
11451da177e4SLinus Torvalds {
11461da177e4SLinus Torvalds 	nfs_commit_free(wdata);
11471da177e4SLinus Torvalds }
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds /*
11501da177e4SLinus Torvalds  * Set up the argument/result storage required for the RPC call.
11511da177e4SLinus Torvalds  */
11521da177e4SLinus Torvalds static void nfs_commit_rpcsetup(struct list_head *head,
1153788e7a89STrond Myklebust 		struct nfs_write_data *data,
1154788e7a89STrond Myklebust 		int how)
11551da177e4SLinus Torvalds {
115684115e1cSTrond Myklebust 	struct nfs_page *first = nfs_list_entry(head->next);
115784115e1cSTrond Myklebust 	struct inode *inode = first->wb_context->path.dentry->d_inode;
115884115e1cSTrond Myklebust 	int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
11593ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
1160*07737691STrond Myklebust 	struct rpc_task *task;
1161bdc7f021STrond Myklebust 	struct rpc_message msg = {
1162bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1163bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
1164bdc7f021STrond Myklebust 		.rpc_cred = first->wb_context->cred,
1165bdc7f021STrond Myklebust 	};
116684115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
1167*07737691STrond Myklebust 		.task = &data->task,
116884115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(inode),
1169bdc7f021STrond Myklebust 		.rpc_message = &msg,
117084115e1cSTrond Myklebust 		.callback_ops = &nfs_commit_ops,
117184115e1cSTrond Myklebust 		.callback_data = data,
117284115e1cSTrond Myklebust 		.flags = flags,
11733ff7576dSTrond Myklebust 		.priority = priority,
117484115e1cSTrond Myklebust 	};
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
11771da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds 	list_splice_init(head, &data->pages);
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 	data->inode	  = inode;
1182bdc7f021STrond Myklebust 	data->cred	  = msg.rpc_cred;
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(data->inode);
11853da28eb1STrond Myklebust 	/* Note: we always request a commit of the entire inode */
11863da28eb1STrond Myklebust 	data->args.offset = 0;
11873da28eb1STrond Myklebust 	data->args.count  = 0;
11883da28eb1STrond Myklebust 	data->res.count   = 0;
11891da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
11901da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
11910e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
11921da177e4SLinus Torvalds 
1193788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
1194bdc7f021STrond Myklebust 	NFS_PROTO(inode)->commit_setup(data, &msg);
11951da177e4SLinus Torvalds 
1196a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1197bdc7f021STrond Myklebust 
1198*07737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1199*07737691STrond Myklebust 	if (!IS_ERR(task))
1200*07737691STrond Myklebust 		rpc_put_task(task);
12011da177e4SLinus Torvalds }
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds /*
12041da177e4SLinus Torvalds  * Commit dirty pages
12051da177e4SLinus Torvalds  */
12061da177e4SLinus Torvalds static int
120740859d7eSChuck Lever nfs_commit_list(struct inode *inode, struct list_head *head, int how)
12081da177e4SLinus Torvalds {
12091da177e4SLinus Torvalds 	struct nfs_write_data	*data;
12101da177e4SLinus Torvalds 	struct nfs_page         *req;
12111da177e4SLinus Torvalds 
1212e9f7bee1STrond Myklebust 	data = nfs_commit_alloc();
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 	if (!data)
12151da177e4SLinus Torvalds 		goto out_bad;
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 	/* Set up the argument struct */
12181da177e4SLinus Torvalds 	nfs_commit_rpcsetup(head, data, how);
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds 	return 0;
12211da177e4SLinus Torvalds  out_bad:
12221da177e4SLinus Torvalds 	while (!list_empty(head)) {
12231da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
12241da177e4SLinus Torvalds 		nfs_list_remove_request(req);
12251da177e4SLinus Torvalds 		nfs_mark_request_commit(req);
122683715ad5STrond Myklebust 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1227c9e51e41SPeter Zijlstra 		dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1228c9e51e41SPeter Zijlstra 				BDI_RECLAIMABLE);
12299fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
12301da177e4SLinus Torvalds 	}
12311da177e4SLinus Torvalds 	return -ENOMEM;
12321da177e4SLinus Torvalds }
12331da177e4SLinus Torvalds 
12341da177e4SLinus Torvalds /*
12351da177e4SLinus Torvalds  * COMMIT call returned
12361da177e4SLinus Torvalds  */
1237788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
12381da177e4SLinus Torvalds {
1239963d8fe5STrond Myklebust 	struct nfs_write_data	*data = calldata;
12401da177e4SLinus Torvalds 	struct nfs_page		*req;
12411da177e4SLinus Torvalds 
1242a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
12431da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
12441da177e4SLinus Torvalds 
1245788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1246788e7a89STrond Myklebust 	if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1247788e7a89STrond Myklebust 		return;
1248788e7a89STrond Myklebust 
12491da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
12501da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
12511da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1252612c9384STrond Myklebust 		clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
1253fd39fc85SChristoph Lameter 		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);
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds 		dprintk("NFS: commit (%s/%Ld %d@%Ld)",
125888be9f99STrond Myklebust 			req->wb_context->path.dentry->d_inode->i_sb->s_id,
125988be9f99STrond Myklebust 			(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
12601da177e4SLinus Torvalds 			req->wb_bytes,
12611da177e4SLinus Torvalds 			(long long)req_offset(req));
12621da177e4SLinus Torvalds 		if (task->tk_status < 0) {
12637b159fc1STrond Myklebust 			nfs_context_set_write_error(req->wb_context, task->tk_status);
12641da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
12651da177e4SLinus Torvalds 			dprintk(", error = %d\n", task->tk_status);
12661da177e4SLinus Torvalds 			goto next;
12671da177e4SLinus Torvalds 		}
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
12701da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
12711da177e4SLinus Torvalds 		if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
12721da177e4SLinus Torvalds 			/* We have a match */
127344dd151dSTrond Myklebust 			/* Set the PG_uptodate flag */
127444dd151dSTrond Myklebust 			nfs_mark_uptodate(req->wb_page, req->wb_pgbase,
127544dd151dSTrond Myklebust 					req->wb_bytes);
12761da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
12771da177e4SLinus Torvalds 			dprintk(" OK\n");
12781da177e4SLinus Torvalds 			goto next;
12791da177e4SLinus Torvalds 		}
12801da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
12811da177e4SLinus Torvalds 		dprintk(" mismatch\n");
128261822ab5STrond Myklebust 		nfs_redirty_request(req);
12831da177e4SLinus Torvalds 	next:
12849fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
12851da177e4SLinus Torvalds 	}
12861da177e4SLinus Torvalds }
1287788e7a89STrond Myklebust 
1288788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
1289788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1290788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1291788e7a89STrond Myklebust };
12921da177e4SLinus Torvalds 
12933da28eb1STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
12941da177e4SLinus Torvalds {
12951da177e4SLinus Torvalds 	LIST_HEAD(head);
12967d46a49fSTrond Myklebust 	int res;
12971da177e4SLinus Torvalds 
1298587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
12993da28eb1STrond Myklebust 	res = nfs_scan_commit(inode, &head, 0, 0);
1300587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
13011da177e4SLinus Torvalds 	if (res) {
13027d46a49fSTrond Myklebust 		int error = nfs_commit_list(inode, &head, how);
13031da177e4SLinus Torvalds 		if (error < 0)
13041da177e4SLinus Torvalds 			return error;
13053da28eb1STrond Myklebust 	}
13061da177e4SLinus Torvalds 	return res;
13071da177e4SLinus Torvalds }
1308c63c7b05STrond Myklebust #else
1309c63c7b05STrond Myklebust static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1310c63c7b05STrond Myklebust {
1311c63c7b05STrond Myklebust 	return 0;
1312c63c7b05STrond Myklebust }
13131da177e4SLinus Torvalds #endif
13141da177e4SLinus Torvalds 
13151c75950bSTrond Myklebust long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
13161da177e4SLinus Torvalds {
13171c75950bSTrond Myklebust 	struct inode *inode = mapping->host;
1318ca52fec1STrond Myklebust 	pgoff_t idx_start, idx_end;
13191c75950bSTrond Myklebust 	unsigned int npages = 0;
1320c42de9ddSTrond Myklebust 	LIST_HEAD(head);
132170b9ecbdSTrond Myklebust 	int nocommit = how & FLUSH_NOCOMMIT;
13223f442547STrond Myklebust 	long pages, ret;
13231da177e4SLinus Torvalds 
13241c75950bSTrond Myklebust 	/* FIXME */
13251c75950bSTrond Myklebust 	if (wbc->range_cyclic)
13261c75950bSTrond Myklebust 		idx_start = 0;
13271c75950bSTrond Myklebust 	else {
13281c75950bSTrond Myklebust 		idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
13291c75950bSTrond Myklebust 		idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
13301c75950bSTrond Myklebust 		if (idx_end > idx_start) {
1331ca52fec1STrond Myklebust 			pgoff_t l_npages = 1 + idx_end - idx_start;
13321c75950bSTrond Myklebust 			npages = l_npages;
13331c75950bSTrond Myklebust 			if (sizeof(npages) != sizeof(l_npages) &&
1334ca52fec1STrond Myklebust 					(pgoff_t)npages != l_npages)
13351c75950bSTrond Myklebust 				npages = 0;
13361c75950bSTrond Myklebust 		}
13371c75950bSTrond Myklebust 	}
1338c42de9ddSTrond Myklebust 	how &= ~FLUSH_NOCOMMIT;
1339587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
13401da177e4SLinus Torvalds 	do {
1341c42de9ddSTrond Myklebust 		ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1342c42de9ddSTrond Myklebust 		if (ret != 0)
1343c42de9ddSTrond Myklebust 			continue;
1344c42de9ddSTrond Myklebust 		if (nocommit)
1345c42de9ddSTrond Myklebust 			break;
1346d2ccddf0STrond Myklebust 		pages = nfs_scan_commit(inode, &head, idx_start, npages);
1347724c439cSTrond Myklebust 		if (pages == 0)
1348c42de9ddSTrond Myklebust 			break;
1349d2ccddf0STrond Myklebust 		if (how & FLUSH_INVALIDATE) {
1350587142f8STrond Myklebust 			spin_unlock(&inode->i_lock);
135183715ad5STrond Myklebust 			nfs_cancel_commit_list(&head);
1352e8e058e8STrond Myklebust 			ret = pages;
1353587142f8STrond Myklebust 			spin_lock(&inode->i_lock);
1354d2ccddf0STrond Myklebust 			continue;
1355d2ccddf0STrond Myklebust 		}
1356d2ccddf0STrond Myklebust 		pages += nfs_scan_commit(inode, &head, 0, 0);
1357587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
1358c42de9ddSTrond Myklebust 		ret = nfs_commit_list(inode, &head, how);
1359587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
1360587142f8STrond Myklebust 
1361c42de9ddSTrond Myklebust 	} while (ret >= 0);
1362587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
1363c42de9ddSTrond Myklebust 	return ret;
13641da177e4SLinus Torvalds }
13651da177e4SLinus Torvalds 
136634901f70STrond Myklebust static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
13671c75950bSTrond Myklebust {
13681c75950bSTrond Myklebust 	int ret;
13691c75950bSTrond Myklebust 
137034901f70STrond Myklebust 	ret = nfs_writepages(mapping, wbc);
137161822ab5STrond Myklebust 	if (ret < 0)
137261822ab5STrond Myklebust 		goto out;
137334901f70STrond Myklebust 	ret = nfs_sync_mapping_wait(mapping, wbc, how);
1374ed90ef51STrond Myklebust 	if (ret < 0)
1375ed90ef51STrond Myklebust 		goto out;
13761c75950bSTrond Myklebust 	return 0;
137761822ab5STrond Myklebust out:
1378e507d9ebSTrond Myklebust 	__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
13791c75950bSTrond Myklebust 	return ret;
13801c75950bSTrond Myklebust }
13811c75950bSTrond Myklebust 
138234901f70STrond Myklebust /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
138334901f70STrond Myklebust static int nfs_write_mapping(struct address_space *mapping, int how)
138434901f70STrond Myklebust {
138534901f70STrond Myklebust 	struct writeback_control wbc = {
138634901f70STrond Myklebust 		.bdi = mapping->backing_dev_info,
138734901f70STrond Myklebust 		.sync_mode = WB_SYNC_NONE,
138834901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
138934901f70STrond Myklebust 		.for_writepages = 1,
139034901f70STrond Myklebust 		.range_cyclic = 1,
139134901f70STrond Myklebust 	};
139234901f70STrond Myklebust 	int ret;
139334901f70STrond Myklebust 
139434901f70STrond Myklebust 	ret = __nfs_write_mapping(mapping, &wbc, how);
139534901f70STrond Myklebust 	if (ret < 0)
139634901f70STrond Myklebust 		return ret;
139734901f70STrond Myklebust 	wbc.sync_mode = WB_SYNC_ALL;
139834901f70STrond Myklebust 	return __nfs_write_mapping(mapping, &wbc, how);
139934901f70STrond Myklebust }
140034901f70STrond Myklebust 
1401ed90ef51STrond Myklebust /*
1402ed90ef51STrond Myklebust  * flush the inode to disk.
1403ed90ef51STrond Myklebust  */
1404ed90ef51STrond Myklebust int nfs_wb_all(struct inode *inode)
14051c75950bSTrond Myklebust {
1406ed90ef51STrond Myklebust 	return nfs_write_mapping(inode->i_mapping, 0);
1407ed90ef51STrond Myklebust }
14081c75950bSTrond Myklebust 
1409ed90ef51STrond Myklebust int nfs_wb_nocommit(struct inode *inode)
1410ed90ef51STrond Myklebust {
1411ed90ef51STrond Myklebust 	return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
14121c75950bSTrond Myklebust }
14131c75950bSTrond Myklebust 
14141b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
14151b3b4a1aSTrond Myklebust {
14161b3b4a1aSTrond Myklebust 	struct nfs_page *req;
14171b3b4a1aSTrond Myklebust 	loff_t range_start = page_offset(page);
14181b3b4a1aSTrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
14191b3b4a1aSTrond Myklebust 	struct writeback_control wbc = {
14201b3b4a1aSTrond Myklebust 		.bdi = page->mapping->backing_dev_info,
14211b3b4a1aSTrond Myklebust 		.sync_mode = WB_SYNC_ALL,
14221b3b4a1aSTrond Myklebust 		.nr_to_write = LONG_MAX,
14231b3b4a1aSTrond Myklebust 		.range_start = range_start,
14241b3b4a1aSTrond Myklebust 		.range_end = range_end,
14251b3b4a1aSTrond Myklebust 	};
14261b3b4a1aSTrond Myklebust 	int ret = 0;
14271b3b4a1aSTrond Myklebust 
14281b3b4a1aSTrond Myklebust 	BUG_ON(!PageLocked(page));
14291b3b4a1aSTrond Myklebust 	for (;;) {
14301b3b4a1aSTrond Myklebust 		req = nfs_page_find_request(page);
14311b3b4a1aSTrond Myklebust 		if (req == NULL)
14321b3b4a1aSTrond Myklebust 			goto out;
14331b3b4a1aSTrond Myklebust 		if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
14341b3b4a1aSTrond Myklebust 			nfs_release_request(req);
14351b3b4a1aSTrond Myklebust 			break;
14361b3b4a1aSTrond Myklebust 		}
14371b3b4a1aSTrond Myklebust 		if (nfs_lock_request_dontget(req)) {
14381b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
14391b3b4a1aSTrond Myklebust 			/*
14401b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
14411b3b4a1aSTrond Myklebust 			 * page as being dirty
14421b3b4a1aSTrond Myklebust 			 */
14431b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
14441b3b4a1aSTrond Myklebust 			nfs_unlock_request(req);
14451b3b4a1aSTrond Myklebust 			break;
14461b3b4a1aSTrond Myklebust 		}
14471b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
14481b3b4a1aSTrond Myklebust 		if (ret < 0)
14491b3b4a1aSTrond Myklebust 			goto out;
14501b3b4a1aSTrond Myklebust 	}
14511b3b4a1aSTrond Myklebust 	if (!PagePrivate(page))
14521b3b4a1aSTrond Myklebust 		return 0;
14531b3b4a1aSTrond Myklebust 	ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
14541b3b4a1aSTrond Myklebust out:
14551b3b4a1aSTrond Myklebust 	return ret;
14561b3b4a1aSTrond Myklebust }
14571b3b4a1aSTrond Myklebust 
14585334eb13SAdrian Bunk static int nfs_wb_page_priority(struct inode *inode, struct page *page,
14595334eb13SAdrian Bunk 				int how)
14601c75950bSTrond Myklebust {
14611c75950bSTrond Myklebust 	loff_t range_start = page_offset(page);
14621c75950bSTrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
14634d770ccfSTrond Myklebust 	struct writeback_control wbc = {
14644d770ccfSTrond Myklebust 		.bdi = page->mapping->backing_dev_info,
14654d770ccfSTrond Myklebust 		.sync_mode = WB_SYNC_ALL,
14664d770ccfSTrond Myklebust 		.nr_to_write = LONG_MAX,
14674d770ccfSTrond Myklebust 		.range_start = range_start,
14684d770ccfSTrond Myklebust 		.range_end = range_end,
14694d770ccfSTrond Myklebust 	};
14704d770ccfSTrond Myklebust 	int ret;
14711c75950bSTrond Myklebust 
14724d770ccfSTrond Myklebust 	BUG_ON(!PageLocked(page));
1473c63c7b05STrond Myklebust 	if (clear_page_dirty_for_io(page)) {
14744d770ccfSTrond Myklebust 		ret = nfs_writepage_locked(page, &wbc);
14754d770ccfSTrond Myklebust 		if (ret < 0)
14764d770ccfSTrond Myklebust 			goto out;
14774d770ccfSTrond Myklebust 	}
1478f40313acSTrond Myklebust 	if (!PagePrivate(page))
1479f40313acSTrond Myklebust 		return 0;
14804d770ccfSTrond Myklebust 	ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
14814d770ccfSTrond Myklebust 	if (ret >= 0)
14824d770ccfSTrond Myklebust 		return 0;
14834d770ccfSTrond Myklebust out:
1484e507d9ebSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_PAGES);
14854d770ccfSTrond Myklebust 	return ret;
14861c75950bSTrond Myklebust }
14871c75950bSTrond Myklebust 
14881c75950bSTrond Myklebust /*
14891c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
14901c75950bSTrond Myklebust  */
14911c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page* page)
14921c75950bSTrond Myklebust {
14934d770ccfSTrond Myklebust 	return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
14941c75950bSTrond Myklebust }
14951c75950bSTrond Myklebust 
1496f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
14971da177e4SLinus Torvalds {
14981da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
14991da177e4SLinus Torvalds 					     sizeof(struct nfs_write_data),
15001da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
150120c2df83SPaul Mundt 					     NULL);
15021da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
15031da177e4SLinus Torvalds 		return -ENOMEM;
15041da177e4SLinus Torvalds 
150593d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
15061da177e4SLinus Torvalds 						     nfs_wdata_cachep);
15071da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
15081da177e4SLinus Torvalds 		return -ENOMEM;
15091da177e4SLinus Torvalds 
151093d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
15111da177e4SLinus Torvalds 						      nfs_wdata_cachep);
15121da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
15131da177e4SLinus Torvalds 		return -ENOMEM;
15141da177e4SLinus Torvalds 
151589a09141SPeter Zijlstra 	/*
151689a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
151789a09141SPeter Zijlstra 	 *
151889a09141SPeter Zijlstra 	 *  64MB:    8192k
151989a09141SPeter Zijlstra 	 * 128MB:   11585k
152089a09141SPeter Zijlstra 	 * 256MB:   16384k
152189a09141SPeter Zijlstra 	 * 512MB:   23170k
152289a09141SPeter Zijlstra 	 *   1GB:   32768k
152389a09141SPeter Zijlstra 	 *   2GB:   46340k
152489a09141SPeter Zijlstra 	 *   4GB:   65536k
152589a09141SPeter Zijlstra 	 *   8GB:   92681k
152689a09141SPeter Zijlstra 	 *  16GB:  131072k
152789a09141SPeter Zijlstra 	 *
152889a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
152989a09141SPeter Zijlstra 	 * Limit the default to 256M
153089a09141SPeter Zijlstra 	 */
153189a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
153289a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
153389a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
153489a09141SPeter Zijlstra 
15351da177e4SLinus Torvalds 	return 0;
15361da177e4SLinus Torvalds }
15371da177e4SLinus Torvalds 
1538266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
15391da177e4SLinus Torvalds {
15401da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
15411da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
15421a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
15431da177e4SLinus Torvalds }
15441da177e4SLinus Torvalds 
1545