xref: /linux/fs/nfs/write.c (revision 383ba71938519959be8e0b598ec658f0c211ff45)
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 
108*383ba719STrond Myklebust void nfs_writedata_release(void *data)
1091da177e4SLinus Torvalds {
110*383ba719STrond Myklebust 	struct nfs_write_data *wdata = data;
111*383ba719STrond Myklebust 
112*383ba719STrond Myklebust 	put_nfs_open_context(wdata->args.context);
1131da177e4SLinus Torvalds 	nfs_writedata_free(wdata);
1141da177e4SLinus Torvalds }
1151da177e4SLinus Torvalds 
1167b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
1177b159fc1STrond Myklebust {
1187b159fc1STrond Myklebust 	ctx->error = error;
1197b159fc1STrond Myklebust 	smp_wmb();
1207b159fc1STrond Myklebust 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
1217b159fc1STrond Myklebust }
1227b159fc1STrond Myklebust 
123277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request_locked(struct page *page)
124277459d2STrond Myklebust {
125277459d2STrond Myklebust 	struct nfs_page *req = NULL;
126277459d2STrond Myklebust 
127277459d2STrond Myklebust 	if (PagePrivate(page)) {
128277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
129277459d2STrond Myklebust 		if (req != NULL)
130c03b4024STrond Myklebust 			kref_get(&req->wb_kref);
131277459d2STrond Myklebust 	}
132277459d2STrond Myklebust 	return req;
133277459d2STrond Myklebust }
134277459d2STrond Myklebust 
135277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page)
136277459d2STrond Myklebust {
137587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
138277459d2STrond Myklebust 	struct nfs_page *req = NULL;
139277459d2STrond Myklebust 
140587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
141277459d2STrond Myklebust 	req = nfs_page_find_request_locked(page);
142587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
143277459d2STrond Myklebust 	return req;
144277459d2STrond Myklebust }
145277459d2STrond Myklebust 
1461da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1471da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1481da177e4SLinus Torvalds {
1491da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
1501da177e4SLinus Torvalds 	loff_t end, i_size = i_size_read(inode);
151ca52fec1STrond Myklebust 	pgoff_t end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	if (i_size > 0 && page->index < end_index)
1541da177e4SLinus Torvalds 		return;
1551da177e4SLinus Torvalds 	end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
1561da177e4SLinus Torvalds 	if (i_size >= end)
1571da177e4SLinus Torvalds 		return;
15891d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
1591da177e4SLinus Torvalds 	i_size_write(inode, end);
1601da177e4SLinus Torvalds }
1611da177e4SLinus Torvalds 
162a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
163a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
164a301b777STrond Myklebust {
165a301b777STrond Myklebust 	SetPageError(page);
166a301b777STrond Myklebust 	nfs_zap_mapping(page->mapping->host, page->mapping);
167a301b777STrond Myklebust }
168a301b777STrond Myklebust 
1691da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
1701da177e4SLinus Torvalds  * covers the full page.
1711da177e4SLinus Torvalds  */
1721da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
1731da177e4SLinus Torvalds {
1741da177e4SLinus Torvalds 	if (PageUptodate(page))
1751da177e4SLinus Torvalds 		return;
1761da177e4SLinus Torvalds 	if (base != 0)
1771da177e4SLinus Torvalds 		return;
17849a70f27STrond Myklebust 	if (count != nfs_page_length(page))
1791da177e4SLinus Torvalds 		return;
1801da177e4SLinus Torvalds 	SetPageUptodate(page);
1811da177e4SLinus Torvalds }
1821da177e4SLinus Torvalds 
183e21195a7STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
1841da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
1851da177e4SLinus Torvalds {
1861da177e4SLinus Torvalds 	struct nfs_page	*req;
187e21195a7STrond Myklebust 	int ret;
1881da177e4SLinus Torvalds 
189e21195a7STrond Myklebust 	for (;;) {
190e21195a7STrond Myklebust 		req = nfs_update_request(ctx, page, offset, count);
191e21195a7STrond Myklebust 		if (!IS_ERR(req))
192e21195a7STrond Myklebust 			break;
193e21195a7STrond Myklebust 		ret = PTR_ERR(req);
194e21195a7STrond Myklebust 		if (ret != -EBUSY)
195e21195a7STrond Myklebust 			return ret;
196e21195a7STrond Myklebust 		ret = nfs_wb_page(page->mapping->host, page);
197e21195a7STrond Myklebust 		if (ret != 0)
198e21195a7STrond Myklebust 			return ret;
199e21195a7STrond Myklebust 	}
2001da177e4SLinus Torvalds 	/* Update file length */
2011da177e4SLinus Torvalds 	nfs_grow_file(page, offset, count);
202acee478aSTrond Myklebust 	nfs_clear_page_tag_locked(req);
203abd3e641STrond Myklebust 	return 0;
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
2071da177e4SLinus Torvalds {
2081da177e4SLinus Torvalds 	if (wbc->for_reclaim)
209c63c7b05STrond Myklebust 		return FLUSH_HIGHPRI | FLUSH_STABLE;
2101da177e4SLinus Torvalds 	if (wbc->for_kupdate)
2111da177e4SLinus Torvalds 		return FLUSH_LOWPRI;
2121da177e4SLinus Torvalds 	return 0;
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds /*
21689a09141SPeter Zijlstra  * NFS congestion control
21789a09141SPeter Zijlstra  */
21889a09141SPeter Zijlstra 
21989a09141SPeter Zijlstra int nfs_congestion_kb;
22089a09141SPeter Zijlstra 
22189a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
22289a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
22389a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
22489a09141SPeter Zijlstra 
2255a6d41b3STrond Myklebust static int nfs_set_page_writeback(struct page *page)
22689a09141SPeter Zijlstra {
2275a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
2285a6d41b3STrond Myklebust 
2295a6d41b3STrond Myklebust 	if (!ret) {
23089a09141SPeter Zijlstra 		struct inode *inode = page->mapping->host;
23189a09141SPeter Zijlstra 		struct nfs_server *nfss = NFS_SERVER(inode);
23289a09141SPeter Zijlstra 
233277866a0SPeter Zijlstra 		if (atomic_long_inc_return(&nfss->writeback) >
23489a09141SPeter Zijlstra 				NFS_CONGESTION_ON_THRESH)
23589a09141SPeter Zijlstra 			set_bdi_congested(&nfss->backing_dev_info, WRITE);
23689a09141SPeter Zijlstra 	}
2375a6d41b3STrond Myklebust 	return ret;
23889a09141SPeter Zijlstra }
23989a09141SPeter Zijlstra 
24089a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page)
24189a09141SPeter Zijlstra {
24289a09141SPeter Zijlstra 	struct inode *inode = page->mapping->host;
24389a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
24489a09141SPeter Zijlstra 
24589a09141SPeter Zijlstra 	end_page_writeback(page);
246c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
24789a09141SPeter Zijlstra 		clear_bdi_congested(&nfss->backing_dev_info, WRITE);
24889a09141SPeter Zijlstra }
24989a09141SPeter Zijlstra 
25089a09141SPeter Zijlstra /*
251e261f51fSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
2529cccef95STrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
253e261f51fSTrond Myklebust  */
254c63c7b05STrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
255c63c7b05STrond Myklebust 				struct page *page)
256e261f51fSTrond Myklebust {
257587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
258e261f51fSTrond Myklebust 	struct nfs_page *req;
259e261f51fSTrond Myklebust 	int ret;
260e261f51fSTrond Myklebust 
261587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
262e261f51fSTrond Myklebust 	for(;;) {
263e261f51fSTrond Myklebust 		req = nfs_page_find_request_locked(page);
264e261f51fSTrond Myklebust 		if (req == NULL) {
265587142f8STrond Myklebust 			spin_unlock(&inode->i_lock);
2669cccef95STrond Myklebust 			return 0;
267e261f51fSTrond Myklebust 		}
268acee478aSTrond Myklebust 		if (nfs_set_page_tag_locked(req))
269e261f51fSTrond Myklebust 			break;
270e261f51fSTrond Myklebust 		/* Note: If we hold the page lock, as is the case in nfs_writepage,
271acee478aSTrond Myklebust 		 *	 then the call to nfs_set_page_tag_locked() will always
272e261f51fSTrond Myklebust 		 *	 succeed provided that someone hasn't already marked the
273e261f51fSTrond Myklebust 		 *	 request as dirty (in which case we don't care).
274e261f51fSTrond Myklebust 		 */
275587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
276e261f51fSTrond Myklebust 		ret = nfs_wait_on_request(req);
277e261f51fSTrond Myklebust 		nfs_release_request(req);
278e261f51fSTrond Myklebust 		if (ret != 0)
279e261f51fSTrond Myklebust 			return ret;
280587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
281e261f51fSTrond Myklebust 	}
282612c9384STrond Myklebust 	if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
283612c9384STrond Myklebust 		/* This request is marked for commit */
284587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
285acee478aSTrond Myklebust 		nfs_clear_page_tag_locked(req);
286c63c7b05STrond Myklebust 		nfs_pageio_complete(pgio);
2879cccef95STrond Myklebust 		return 0;
288612c9384STrond Myklebust 	}
289c63c7b05STrond Myklebust 	if (nfs_set_page_writeback(page) != 0) {
290587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
291c63c7b05STrond Myklebust 		BUG();
292c63c7b05STrond Myklebust 	}
293587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
294c63c7b05STrond Myklebust 	nfs_pageio_add_request(pgio, req);
2959cccef95STrond Myklebust 	return 0;
296e261f51fSTrond Myklebust }
297e261f51fSTrond Myklebust 
298f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
299f758c885STrond Myklebust {
300f758c885STrond Myklebust 	struct inode *inode = page->mapping->host;
301f758c885STrond Myklebust 
302f758c885STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
303f758c885STrond Myklebust 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
304f758c885STrond Myklebust 
305f758c885STrond Myklebust 	nfs_pageio_cond_complete(pgio, page->index);
306f758c885STrond Myklebust 	return nfs_page_async_flush(pgio, page);
307f758c885STrond Myklebust }
308f758c885STrond Myklebust 
309e261f51fSTrond Myklebust /*
3101da177e4SLinus Torvalds  * Write an mmapped page to the server.
3111da177e4SLinus Torvalds  */
3124d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
3131da177e4SLinus Torvalds {
314f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
315e261f51fSTrond Myklebust 	int err;
3161da177e4SLinus Torvalds 
317f758c885STrond Myklebust 	nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
318f758c885STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
319f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
320f758c885STrond Myklebust 	if (err < 0)
3214d770ccfSTrond Myklebust 		return err;
322f758c885STrond Myklebust 	if (pgio.pg_error < 0)
323f758c885STrond Myklebust 		return pgio.pg_error;
324f758c885STrond Myklebust 	return 0;
3254d770ccfSTrond Myklebust }
3264d770ccfSTrond Myklebust 
3274d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
3284d770ccfSTrond Myklebust {
329f758c885STrond Myklebust 	int ret;
3304d770ccfSTrond Myklebust 
331f758c885STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
3321da177e4SLinus Torvalds 	unlock_page(page);
333f758c885STrond Myklebust 	return ret;
334f758c885STrond Myklebust }
335f758c885STrond Myklebust 
336f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
337f758c885STrond Myklebust {
338f758c885STrond Myklebust 	int ret;
339f758c885STrond Myklebust 
340f758c885STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
341f758c885STrond Myklebust 	unlock_page(page);
342f758c885STrond Myklebust 	return ret;
3431da177e4SLinus Torvalds }
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3461da177e4SLinus Torvalds {
3471da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
348c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
3491da177e4SLinus Torvalds 	int err;
3501da177e4SLinus Torvalds 
35191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
35291d5b470SChuck Lever 
353c63c7b05STrond Myklebust 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
354f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
355c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
356f758c885STrond Myklebust 	if (err < 0)
3571da177e4SLinus Torvalds 		return err;
358f758c885STrond Myklebust 	if (pgio.pg_error < 0)
359c63c7b05STrond Myklebust 		return pgio.pg_error;
360c63c7b05STrond Myklebust 	return 0;
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds /*
3641da177e4SLinus Torvalds  * Insert a write request into an inode
3651da177e4SLinus Torvalds  */
3661da177e4SLinus Torvalds static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
3671da177e4SLinus Torvalds {
3681da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
3691da177e4SLinus Torvalds 	int error;
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
3721da177e4SLinus Torvalds 	BUG_ON(error == -EEXIST);
3731da177e4SLinus Torvalds 	if (error)
3741da177e4SLinus Torvalds 		return error;
3751da177e4SLinus Torvalds 	if (!nfsi->npages) {
3761da177e4SLinus Torvalds 		igrab(inode);
3771da177e4SLinus Torvalds 		if (nfs_have_delegation(inode, FMODE_WRITE))
3781da177e4SLinus Torvalds 			nfsi->change_attr++;
3791da177e4SLinus Torvalds 	}
380deb7d638STrond Myklebust 	SetPagePrivate(req->wb_page);
381277459d2STrond Myklebust 	set_page_private(req->wb_page, (unsigned long)req);
3821da177e4SLinus Torvalds 	nfsi->npages++;
383c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
384acee478aSTrond Myklebust 	radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, NFS_PAGE_TAG_LOCKED);
3851da177e4SLinus Torvalds 	return 0;
3861da177e4SLinus Torvalds }
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds /*
38989a09141SPeter Zijlstra  * Remove a write request from an inode
3901da177e4SLinus Torvalds  */
3911da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
3921da177e4SLinus Torvalds {
39388be9f99STrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
3941da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	BUG_ON (!NFS_WBACK_BUSY(req));
3971da177e4SLinus Torvalds 
398587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
399277459d2STrond Myklebust 	set_page_private(req->wb_page, 0);
400deb7d638STrond Myklebust 	ClearPagePrivate(req->wb_page);
4011da177e4SLinus Torvalds 	radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
4021da177e4SLinus Torvalds 	nfsi->npages--;
4031da177e4SLinus Torvalds 	if (!nfsi->npages) {
404587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
4051da177e4SLinus Torvalds 		iput(inode);
4061da177e4SLinus Torvalds 	} else
407587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
4081da177e4SLinus Torvalds 	nfs_clear_request(req);
4091da177e4SLinus Torvalds 	nfs_release_request(req);
4101da177e4SLinus Torvalds }
4111da177e4SLinus Torvalds 
41261822ab5STrond Myklebust static void
41361822ab5STrond Myklebust nfs_redirty_request(struct nfs_page *req)
41461822ab5STrond Myklebust {
41561822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
41661822ab5STrond Myklebust }
41761822ab5STrond Myklebust 
4181da177e4SLinus Torvalds /*
4191da177e4SLinus Torvalds  * Check if a request is dirty
4201da177e4SLinus Torvalds  */
4211da177e4SLinus Torvalds static inline int
4221da177e4SLinus Torvalds nfs_dirty_request(struct nfs_page *req)
4231da177e4SLinus Torvalds {
4245a6d41b3STrond Myklebust 	struct page *page = req->wb_page;
4255a6d41b3STrond Myklebust 
426612c9384STrond Myklebust 	if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
4275a6d41b3STrond Myklebust 		return 0;
4285a6d41b3STrond Myklebust 	return !PageWriteback(req->wb_page);
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
4321da177e4SLinus Torvalds /*
4331da177e4SLinus Torvalds  * Add a request to the inode's commit list.
4341da177e4SLinus Torvalds  */
4351da177e4SLinus Torvalds static void
4361da177e4SLinus Torvalds nfs_mark_request_commit(struct nfs_page *req)
4371da177e4SLinus Torvalds {
43888be9f99STrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
4391da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4401da177e4SLinus Torvalds 
441587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
4421da177e4SLinus Torvalds 	nfsi->ncommit++;
443612c9384STrond Myklebust 	set_bit(PG_NEED_COMMIT, &(req)->wb_flags);
4445c369683STrond Myklebust 	radix_tree_tag_set(&nfsi->nfs_page_tree,
4455c369683STrond Myklebust 			req->wb_index,
4465c369683STrond Myklebust 			NFS_PAGE_TAG_COMMIT);
447587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
448fd39fc85SChristoph Lameter 	inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
449c9e51e41SPeter Zijlstra 	inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
450a1803044STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
4511da177e4SLinus Torvalds }
4528e821cadSTrond Myklebust 
4538e821cadSTrond Myklebust static inline
4548e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
4558e821cadSTrond Myklebust {
4568e821cadSTrond Myklebust 	return data->verf.committed != NFS_FILE_SYNC;
4578e821cadSTrond Myklebust }
4588e821cadSTrond Myklebust 
4598e821cadSTrond Myklebust static inline
4608e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req)
4618e821cadSTrond Myklebust {
462612c9384STrond Myklebust 	if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
4638e821cadSTrond Myklebust 		nfs_mark_request_commit(req);
4648e821cadSTrond Myklebust 		return 1;
4658e821cadSTrond Myklebust 	}
4668e821cadSTrond Myklebust 	if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
4678e821cadSTrond Myklebust 		nfs_redirty_request(req);
4688e821cadSTrond Myklebust 		return 1;
4698e821cadSTrond Myklebust 	}
4708e821cadSTrond Myklebust 	return 0;
4718e821cadSTrond Myklebust }
4728e821cadSTrond Myklebust #else
4738e821cadSTrond Myklebust static inline void
4748e821cadSTrond Myklebust nfs_mark_request_commit(struct nfs_page *req)
4758e821cadSTrond Myklebust {
4768e821cadSTrond Myklebust }
4778e821cadSTrond Myklebust 
4788e821cadSTrond Myklebust static inline
4798e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
4808e821cadSTrond Myklebust {
4818e821cadSTrond Myklebust 	return 0;
4828e821cadSTrond Myklebust }
4838e821cadSTrond Myklebust 
4848e821cadSTrond Myklebust static inline
4858e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req)
4868e821cadSTrond Myklebust {
4878e821cadSTrond Myklebust 	return 0;
4888e821cadSTrond Myklebust }
4891da177e4SLinus Torvalds #endif
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds /*
4921da177e4SLinus Torvalds  * Wait for a request to complete.
4931da177e4SLinus Torvalds  *
494150030b7SMatthew Wilcox  * Interruptible by fatal signals only.
4951da177e4SLinus Torvalds  */
496ca52fec1STrond Myklebust static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
4971da177e4SLinus Torvalds {
4981da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4991da177e4SLinus Torvalds 	struct nfs_page *req;
500ca52fec1STrond Myklebust 	pgoff_t idx_end, next;
5011da177e4SLinus Torvalds 	unsigned int		res = 0;
5021da177e4SLinus Torvalds 	int			error;
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	if (npages == 0)
5051da177e4SLinus Torvalds 		idx_end = ~0;
5061da177e4SLinus Torvalds 	else
5071da177e4SLinus Torvalds 		idx_end = idx_start + npages - 1;
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	next = idx_start;
5109fd367f0STrond Myklebust 	while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) {
5111da177e4SLinus Torvalds 		if (req->wb_index > idx_end)
5121da177e4SLinus Torvalds 			break;
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 		next = req->wb_index + 1;
515c6a556b8STrond Myklebust 		BUG_ON(!NFS_WBACK_BUSY(req));
5161da177e4SLinus Torvalds 
517c03b4024STrond Myklebust 		kref_get(&req->wb_kref);
518587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
5191da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
5201da177e4SLinus Torvalds 		nfs_release_request(req);
521587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
5221da177e4SLinus Torvalds 		if (error < 0)
5231da177e4SLinus Torvalds 			return error;
5241da177e4SLinus Torvalds 		res++;
5251da177e4SLinus Torvalds 	}
5261da177e4SLinus Torvalds 	return res;
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds 
52983715ad5STrond Myklebust static void nfs_cancel_commit_list(struct list_head *head)
53083715ad5STrond Myklebust {
53183715ad5STrond Myklebust 	struct nfs_page *req;
53283715ad5STrond Myklebust 
53383715ad5STrond Myklebust 	while(!list_empty(head)) {
53483715ad5STrond Myklebust 		req = nfs_list_entry(head->next);
535b6dff26aSTrond Myklebust 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
536c9e51e41SPeter Zijlstra 		dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
537c9e51e41SPeter Zijlstra 				BDI_RECLAIMABLE);
53883715ad5STrond Myklebust 		nfs_list_remove_request(req);
539612c9384STrond Myklebust 		clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
54083715ad5STrond Myklebust 		nfs_inode_remove_request(req);
541b6dff26aSTrond Myklebust 		nfs_unlock_request(req);
54283715ad5STrond Myklebust 	}
54383715ad5STrond Myklebust }
54483715ad5STrond Myklebust 
5451da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
5461da177e4SLinus Torvalds /*
5471da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
5481da177e4SLinus Torvalds  * @inode: NFS inode to scan
5491da177e4SLinus Torvalds  * @dst: destination list
5501da177e4SLinus Torvalds  * @idx_start: lower bound of page->index to scan.
5511da177e4SLinus Torvalds  * @npages: idx_start + npages sets the upper bound to scan.
5521da177e4SLinus Torvalds  *
5531da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
5541da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
5551da177e4SLinus Torvalds  */
5561da177e4SLinus Torvalds static int
557ca52fec1STrond Myklebust nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
5581da177e4SLinus Torvalds {
5591da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
5603da28eb1STrond Myklebust 	int res = 0;
5613da28eb1STrond Myklebust 
5623da28eb1STrond Myklebust 	if (nfsi->ncommit != 0) {
5635c369683STrond Myklebust 		res = nfs_scan_list(nfsi, dst, idx_start, npages,
5645c369683STrond Myklebust 				NFS_PAGE_TAG_COMMIT);
5651da177e4SLinus Torvalds 		nfsi->ncommit -= res;
5663da28eb1STrond Myklebust 	}
5671da177e4SLinus Torvalds 	return res;
5681da177e4SLinus Torvalds }
569c42de9ddSTrond Myklebust #else
570ca52fec1STrond Myklebust static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
571c42de9ddSTrond Myklebust {
572c42de9ddSTrond Myklebust 	return 0;
573c42de9ddSTrond Myklebust }
5741da177e4SLinus Torvalds #endif
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds /*
5771da177e4SLinus Torvalds  * Try to update any existing write request, or create one if there is none.
5781da177e4SLinus Torvalds  * In order to match, the request's credentials must match those of
5791da177e4SLinus Torvalds  * the calling process.
5801da177e4SLinus Torvalds  *
5811da177e4SLinus Torvalds  * Note: Should always be called with the Page Lock held!
5821da177e4SLinus Torvalds  */
5831da177e4SLinus Torvalds static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
584e21195a7STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
5851da177e4SLinus Torvalds {
58689a09141SPeter Zijlstra 	struct address_space *mapping = page->mapping;
58789a09141SPeter Zijlstra 	struct inode *inode = mapping->host;
5881da177e4SLinus Torvalds 	struct nfs_page		*req, *new = NULL;
589ca52fec1STrond Myklebust 	pgoff_t		rqend, end;
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	end = offset + bytes;
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 	for (;;) {
5941da177e4SLinus Torvalds 		/* Loop over all inode entries and see if we find
5951da177e4SLinus Torvalds 		 * A request for the page we wish to update
5961da177e4SLinus Torvalds 		 */
597587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
598277459d2STrond Myklebust 		req = nfs_page_find_request_locked(page);
5991da177e4SLinus Torvalds 		if (req) {
600acee478aSTrond Myklebust 			if (!nfs_set_page_tag_locked(req)) {
6011da177e4SLinus Torvalds 				int error;
602277459d2STrond Myklebust 
603587142f8STrond Myklebust 				spin_unlock(&inode->i_lock);
6041da177e4SLinus Torvalds 				error = nfs_wait_on_request(req);
6051da177e4SLinus Torvalds 				nfs_release_request(req);
6061dd594b2SNeil Brown 				if (error < 0) {
6071dd594b2SNeil Brown 					if (new)
6081dd594b2SNeil Brown 						nfs_release_request(new);
6091da177e4SLinus Torvalds 					return ERR_PTR(error);
6101dd594b2SNeil Brown 				}
6111da177e4SLinus Torvalds 				continue;
6121da177e4SLinus Torvalds 			}
613587142f8STrond Myklebust 			spin_unlock(&inode->i_lock);
6141da177e4SLinus Torvalds 			if (new)
6151da177e4SLinus Torvalds 				nfs_release_request(new);
6161da177e4SLinus Torvalds 			break;
6171da177e4SLinus Torvalds 		}
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 		if (new) {
6201da177e4SLinus Torvalds 			int error;
6211da177e4SLinus Torvalds 			nfs_lock_request_dontget(new);
6221da177e4SLinus Torvalds 			error = nfs_inode_add_request(inode, new);
6231da177e4SLinus Torvalds 			if (error) {
624587142f8STrond Myklebust 				spin_unlock(&inode->i_lock);
6251da177e4SLinus Torvalds 				nfs_unlock_request(new);
6261da177e4SLinus Torvalds 				return ERR_PTR(error);
6271da177e4SLinus Torvalds 			}
628587142f8STrond Myklebust 			spin_unlock(&inode->i_lock);
62961e930a9STrond Myklebust 			req = new;
63061e930a9STrond Myklebust 			goto zero_page;
6311da177e4SLinus Torvalds 		}
632587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 		new = nfs_create_request(ctx, inode, page, offset, bytes);
6351da177e4SLinus Torvalds 		if (IS_ERR(new))
6361da177e4SLinus Torvalds 			return new;
6371da177e4SLinus Torvalds 	}
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	/* We have a request for our page.
6401da177e4SLinus Torvalds 	 * If the creds don't match, or the
6411da177e4SLinus Torvalds 	 * page addresses don't match,
6421da177e4SLinus Torvalds 	 * tell the caller to wait on the conflicting
6431da177e4SLinus Torvalds 	 * request.
6441da177e4SLinus Torvalds 	 */
6451da177e4SLinus Torvalds 	rqend = req->wb_offset + req->wb_bytes;
6461da177e4SLinus Torvalds 	if (req->wb_context != ctx
6471da177e4SLinus Torvalds 	    || req->wb_page != page
6481da177e4SLinus Torvalds 	    || !nfs_dirty_request(req)
6491da177e4SLinus Torvalds 	    || offset > rqend || end < req->wb_offset) {
650acee478aSTrond Myklebust 		nfs_clear_page_tag_locked(req);
6511da177e4SLinus Torvalds 		return ERR_PTR(-EBUSY);
6521da177e4SLinus Torvalds 	}
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
6551da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
6561da177e4SLinus Torvalds 		req->wb_offset = offset;
6571da177e4SLinus Torvalds 		req->wb_pgbase = offset;
65861e930a9STrond Myklebust 		req->wb_bytes = max(end, rqend) - req->wb_offset;
65961e930a9STrond Myklebust 		goto zero_page;
6601da177e4SLinus Torvalds 	}
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 	if (end > rqend)
6631da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 	return req;
66661e930a9STrond Myklebust zero_page:
66761e930a9STrond Myklebust 	/* If this page might potentially be marked as up to date,
66861e930a9STrond Myklebust 	 * then we need to zero any uninitalised data. */
66961e930a9STrond Myklebust 	if (req->wb_pgbase == 0 && req->wb_bytes != PAGE_CACHE_SIZE
67061e930a9STrond Myklebust 			&& !PageUptodate(req->wb_page))
671eebd2aa3SChristoph Lameter 		zero_user_segment(req->wb_page, req->wb_bytes, PAGE_CACHE_SIZE);
67261e930a9STrond Myklebust 	return req;
6731da177e4SLinus Torvalds }
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
6761da177e4SLinus Torvalds {
677cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
6781da177e4SLinus Torvalds 	struct nfs_page	*req;
6791a54533eSTrond Myklebust 	int do_flush, status;
6801da177e4SLinus Torvalds 	/*
6811da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
6821da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
6831da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
6841da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
6851da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
6861da177e4SLinus Torvalds 	 * dropped page.
6871da177e4SLinus Torvalds 	 */
6881a54533eSTrond Myklebust 	do {
689277459d2STrond Myklebust 		req = nfs_page_find_request(page);
6901a54533eSTrond Myklebust 		if (req == NULL)
6911a54533eSTrond Myklebust 			return 0;
6921a54533eSTrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx
693e261f51fSTrond Myklebust 			|| !nfs_dirty_request(req);
6941da177e4SLinus Torvalds 		nfs_release_request(req);
6951a54533eSTrond Myklebust 		if (!do_flush)
6961a54533eSTrond Myklebust 			return 0;
697277459d2STrond Myklebust 		status = nfs_wb_page(page->mapping->host, page);
6981a54533eSTrond Myklebust 	} while (status == 0);
6991a54533eSTrond Myklebust 	return status;
7001da177e4SLinus Torvalds }
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds /*
7035d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
7045d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
7055d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
7065d47a356STrond Myklebust  */
7075d47a356STrond Myklebust static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
7085d47a356STrond Myklebust {
7095d47a356STrond Myklebust 	return PageUptodate(page) &&
7105d47a356STrond Myklebust 		!(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
7115d47a356STrond Myklebust }
7125d47a356STrond Myklebust 
7135d47a356STrond Myklebust /*
7141da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
7151da177e4SLinus Torvalds  *
7161da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
7171da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
7181da177e4SLinus Torvalds  */
7191da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
7201da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
7211da177e4SLinus Torvalds {
722cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
7231da177e4SLinus Torvalds 	struct inode	*inode = page->mapping->host;
7241da177e4SLinus Torvalds 	int		status = 0;
7251da177e4SLinus Torvalds 
72691d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
72791d5b470SChuck Lever 
7281da177e4SLinus Torvalds 	dprintk("NFS:      nfs_updatepage(%s/%s %d@%Ld)\n",
72901cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
73001cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name, count,
7310bbacc40SChuck Lever 		(long long)(page_offset(page) +offset));
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 	/* If we're not using byte range locks, and we know the page
7345d47a356STrond Myklebust 	 * is up to date, it may be more efficient to extend the write
7355d47a356STrond Myklebust 	 * to cover the entire page in order to avoid fragmentation
7365d47a356STrond Myklebust 	 * inefficiencies.
7371da177e4SLinus Torvalds 	 */
7385d47a356STrond Myklebust 	if (nfs_write_pageuptodate(page, inode) &&
7395d47a356STrond Myklebust 			inode->i_flock == NULL &&
7404b5621f6STrond Myklebust 			!(file->f_flags & O_SYNC)) {
74149a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
7421da177e4SLinus Torvalds 		offset = 0;
7431da177e4SLinus Torvalds 	}
7441da177e4SLinus Torvalds 
745e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
746e261f51fSTrond Myklebust 	__set_page_dirty_nobuffers(page);
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds         dprintk("NFS:      nfs_updatepage returns %d (isize %Ld)\n",
7491da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
7501da177e4SLinus Torvalds 	if (status < 0)
751a301b777STrond Myklebust 		nfs_set_pageerror(page);
7521da177e4SLinus Torvalds 	return status;
7531da177e4SLinus Torvalds }
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds static void nfs_writepage_release(struct nfs_page *req)
7561da177e4SLinus Torvalds {
7578e821cadSTrond Myklebust 
75844dd151dSTrond Myklebust 	if (PageError(req->wb_page)) {
75944dd151dSTrond Myklebust 		nfs_end_page_writeback(req->wb_page);
76044dd151dSTrond Myklebust 		nfs_inode_remove_request(req);
76144dd151dSTrond Myklebust 	} else if (!nfs_reschedule_unstable_write(req)) {
76244dd151dSTrond Myklebust 		/* Set the PG_uptodate flag */
76344dd151dSTrond Myklebust 		nfs_mark_uptodate(req->wb_page, req->wb_pgbase, req->wb_bytes);
76489a09141SPeter Zijlstra 		nfs_end_page_writeback(req->wb_page);
7651da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
7668e821cadSTrond Myklebust 	} else
7678e821cadSTrond Myklebust 		nfs_end_page_writeback(req->wb_page);
7689fd367f0STrond Myklebust 	nfs_clear_page_tag_locked(req);
7691da177e4SLinus Torvalds }
7701da177e4SLinus Torvalds 
7713ff7576dSTrond Myklebust static int flush_task_priority(int how)
7721da177e4SLinus Torvalds {
7731da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
7741da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
7751da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
7761da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
7771da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
7781da177e4SLinus Torvalds 	}
7791da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
7801da177e4SLinus Torvalds }
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds /*
7831da177e4SLinus Torvalds  * Set up the argument/result storage required for the RPC call.
7841da177e4SLinus Torvalds  */
7851da177e4SLinus Torvalds static void nfs_write_rpcsetup(struct nfs_page *req,
7861da177e4SLinus Torvalds 		struct nfs_write_data *data,
787788e7a89STrond Myklebust 		const struct rpc_call_ops *call_ops,
7881da177e4SLinus Torvalds 		unsigned int count, unsigned int offset,
7891da177e4SLinus Torvalds 		int how)
7901da177e4SLinus Torvalds {
79184115e1cSTrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
79284115e1cSTrond Myklebust 	int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
7933ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
79407737691STrond Myklebust 	struct rpc_task *task;
795bdc7f021STrond Myklebust 	struct rpc_message msg = {
796bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
797bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
798bdc7f021STrond Myklebust 		.rpc_cred = req->wb_context->cred,
799bdc7f021STrond Myklebust 	};
80084115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
80184115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(inode),
80207737691STrond Myklebust 		.task = &data->task,
803bdc7f021STrond Myklebust 		.rpc_message = &msg,
80484115e1cSTrond Myklebust 		.callback_ops = call_ops,
80584115e1cSTrond Myklebust 		.callback_data = data,
80684115e1cSTrond Myklebust 		.flags = flags,
8073ff7576dSTrond Myklebust 		.priority = priority,
80884115e1cSTrond Myklebust 	};
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
8111da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 	data->req = req;
81488be9f99STrond Myklebust 	data->inode = inode = req->wb_context->path.dentry->d_inode;
815bdc7f021STrond Myklebust 	data->cred = msg.rpc_cred;
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(inode);
8181da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
8191da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
8201da177e4SLinus Torvalds 	data->args.pages  = data->pagevec;
8211da177e4SLinus Torvalds 	data->args.count  = count;
822*383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
823bdc7f021STrond Myklebust 	data->args.stable  = NFS_UNSTABLE;
824bdc7f021STrond Myklebust 	if (how & FLUSH_STABLE) {
825bdc7f021STrond Myklebust 		data->args.stable = NFS_DATA_SYNC;
826bdc7f021STrond Myklebust 		if (!NFS_I(inode)->ncommit)
827bdc7f021STrond Myklebust 			data->args.stable = NFS_FILE_SYNC;
828bdc7f021STrond Myklebust 	}
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
8311da177e4SLinus Torvalds 	data->res.count   = count;
8321da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
8330e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
8341da177e4SLinus Torvalds 
835788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
836bdc7f021STrond Myklebust 	NFS_PROTO(inode)->write_setup(data, &msg);
8371da177e4SLinus Torvalds 
838a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated write call "
839a3f565b1SChuck Lever 		"(req %s/%Ld, %u bytes @ offset %Lu)\n",
8400bbacc40SChuck Lever 		data->task.tk_pid,
8411da177e4SLinus Torvalds 		inode->i_sb->s_id,
8421da177e4SLinus Torvalds 		(long long)NFS_FILEID(inode),
8431da177e4SLinus Torvalds 		count,
8441da177e4SLinus Torvalds 		(unsigned long long)data->args.offset);
8451da177e4SLinus Torvalds 
84607737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
84707737691STrond Myklebust 	if (!IS_ERR(task))
84807737691STrond Myklebust 		rpc_put_task(task);
8491da177e4SLinus Torvalds }
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds /*
8521da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
8531da177e4SLinus Torvalds  * contiguous dirty area on one page.
8541da177e4SLinus Torvalds  */
8558d5658c9STrond Myklebust static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
8561da177e4SLinus Torvalds {
8571da177e4SLinus Torvalds 	struct nfs_page *req = nfs_list_entry(head->next);
8581da177e4SLinus Torvalds 	struct page *page = req->wb_page;
8591da177e4SLinus Torvalds 	struct nfs_write_data *data;
860e9f7bee1STrond Myklebust 	size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
861e9f7bee1STrond Myklebust 	unsigned int offset;
8621da177e4SLinus Torvalds 	int requests = 0;
8631da177e4SLinus Torvalds 	LIST_HEAD(list);
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	nfs_list_remove_request(req);
8661da177e4SLinus Torvalds 
867bcb71bbaSTrond Myklebust 	nbytes = count;
868e9f7bee1STrond Myklebust 	do {
869e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
870e9f7bee1STrond Myklebust 
8718d5658c9STrond Myklebust 		data = nfs_writedata_alloc(1);
8721da177e4SLinus Torvalds 		if (!data)
8731da177e4SLinus Torvalds 			goto out_bad;
8741da177e4SLinus Torvalds 		list_add(&data->pages, &list);
8751da177e4SLinus Torvalds 		requests++;
876e9f7bee1STrond Myklebust 		nbytes -= len;
877e9f7bee1STrond Myklebust 	} while (nbytes != 0);
8781da177e4SLinus Torvalds 	atomic_set(&req->wb_complete, requests);
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	ClearPageError(page);
8811da177e4SLinus Torvalds 	offset = 0;
882bcb71bbaSTrond Myklebust 	nbytes = count;
8831da177e4SLinus Torvalds 	do {
8841da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
8851da177e4SLinus Torvalds 		list_del_init(&data->pages);
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 		data->pagevec[0] = page;
8881da177e4SLinus Torvalds 
889bcb71bbaSTrond Myklebust 		if (nbytes < wsize)
890bcb71bbaSTrond Myklebust 			wsize = nbytes;
891788e7a89STrond Myklebust 		nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
892788e7a89STrond Myklebust 				   wsize, offset, how);
8931da177e4SLinus Torvalds 		offset += wsize;
8941da177e4SLinus Torvalds 		nbytes -= wsize;
8951da177e4SLinus Torvalds 	} while (nbytes != 0);
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 	return 0;
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds out_bad:
9001da177e4SLinus Torvalds 	while (!list_empty(&list)) {
9011da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
9021da177e4SLinus Torvalds 		list_del(&data->pages);
9038aca67f0STrond Myklebust 		nfs_writedata_release(data);
9041da177e4SLinus Torvalds 	}
90561822ab5STrond Myklebust 	nfs_redirty_request(req);
9066d677e35STrond Myklebust 	nfs_end_page_writeback(req->wb_page);
9079fd367f0STrond Myklebust 	nfs_clear_page_tag_locked(req);
9081da177e4SLinus Torvalds 	return -ENOMEM;
9091da177e4SLinus Torvalds }
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds /*
9121da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
9131da177e4SLinus Torvalds  * The page must have been locked by the caller.
9141da177e4SLinus Torvalds  *
9151da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
9161da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
9171da177e4SLinus Torvalds  * that has been written but not committed.
9181da177e4SLinus Torvalds  */
9198d5658c9STrond Myklebust static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
9201da177e4SLinus Torvalds {
9211da177e4SLinus Torvalds 	struct nfs_page		*req;
9221da177e4SLinus Torvalds 	struct page		**pages;
9231da177e4SLinus Torvalds 	struct nfs_write_data	*data;
9241da177e4SLinus Torvalds 
9258d5658c9STrond Myklebust 	data = nfs_writedata_alloc(npages);
9261da177e4SLinus Torvalds 	if (!data)
9271da177e4SLinus Torvalds 		goto out_bad;
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 	pages = data->pagevec;
9301da177e4SLinus Torvalds 	while (!list_empty(head)) {
9311da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
9321da177e4SLinus Torvalds 		nfs_list_remove_request(req);
9331da177e4SLinus Torvalds 		nfs_list_add_request(req, &data->pages);
9341da177e4SLinus Torvalds 		ClearPageError(req->wb_page);
9351da177e4SLinus Torvalds 		*pages++ = req->wb_page;
9361da177e4SLinus Torvalds 	}
9371da177e4SLinus Torvalds 	req = nfs_list_entry(data->pages.next);
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	/* Set up the argument struct */
940788e7a89STrond Myklebust 	nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 	return 0;
9431da177e4SLinus Torvalds  out_bad:
9441da177e4SLinus Torvalds 	while (!list_empty(head)) {
94510afec90STrond Myklebust 		req = nfs_list_entry(head->next);
9461da177e4SLinus Torvalds 		nfs_list_remove_request(req);
94761822ab5STrond Myklebust 		nfs_redirty_request(req);
9486d677e35STrond Myklebust 		nfs_end_page_writeback(req->wb_page);
9499fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
9501da177e4SLinus Torvalds 	}
9511da177e4SLinus Torvalds 	return -ENOMEM;
9521da177e4SLinus Torvalds }
9531da177e4SLinus Torvalds 
954c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
955c63c7b05STrond Myklebust 				  struct inode *inode, int ioflags)
9561da177e4SLinus Torvalds {
957bf4285e7SChuck Lever 	size_t wsize = NFS_SERVER(inode)->wsize;
9581da177e4SLinus Torvalds 
959bcb71bbaSTrond Myklebust 	if (wsize < PAGE_CACHE_SIZE)
960c63c7b05STrond Myklebust 		nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
961bcb71bbaSTrond Myklebust 	else
962c63c7b05STrond Myklebust 		nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
9631da177e4SLinus Torvalds }
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds /*
9661da177e4SLinus Torvalds  * Handle a write reply that flushed part of a page.
9671da177e4SLinus Torvalds  */
968788e7a89STrond Myklebust static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
9691da177e4SLinus Torvalds {
970788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
9711da177e4SLinus Torvalds 	struct nfs_page		*req = data->req;
9721da177e4SLinus Torvalds 	struct page		*page = req->wb_page;
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 	dprintk("NFS: write (%s/%Ld %d@%Ld)",
97588be9f99STrond Myklebust 		req->wb_context->path.dentry->d_inode->i_sb->s_id,
97688be9f99STrond Myklebust 		(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
9771da177e4SLinus Torvalds 		req->wb_bytes,
9781da177e4SLinus Torvalds 		(long long)req_offset(req));
9791da177e4SLinus Torvalds 
980788e7a89STrond Myklebust 	if (nfs_writeback_done(task, data) != 0)
981788e7a89STrond Myklebust 		return;
982788e7a89STrond Myklebust 
983788e7a89STrond Myklebust 	if (task->tk_status < 0) {
984a301b777STrond Myklebust 		nfs_set_pageerror(page);
9857b159fc1STrond Myklebust 		nfs_context_set_write_error(req->wb_context, task->tk_status);
986788e7a89STrond Myklebust 		dprintk(", error = %d\n", task->tk_status);
9878e821cadSTrond Myklebust 		goto out;
9888e821cadSTrond Myklebust 	}
9898e821cadSTrond Myklebust 
9908e821cadSTrond Myklebust 	if (nfs_write_need_commit(data)) {
991587142f8STrond Myklebust 		struct inode *inode = page->mapping->host;
9928e821cadSTrond Myklebust 
993587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
9948e821cadSTrond Myklebust 		if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
9958e821cadSTrond Myklebust 			/* Do nothing we need to resend the writes */
9968e821cadSTrond Myklebust 		} else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
9971da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
9981da177e4SLinus Torvalds 			dprintk(" defer commit\n");
9991da177e4SLinus Torvalds 		} else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
10008e821cadSTrond Myklebust 			set_bit(PG_NEED_RESCHED, &req->wb_flags);
10018e821cadSTrond Myklebust 			clear_bit(PG_NEED_COMMIT, &req->wb_flags);
10021da177e4SLinus Torvalds 			dprintk(" server reboot detected\n");
10031da177e4SLinus Torvalds 		}
1004587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
10051da177e4SLinus Torvalds 	} else
10061da177e4SLinus Torvalds 		dprintk(" OK\n");
10071da177e4SLinus Torvalds 
10088e821cadSTrond Myklebust out:
10091da177e4SLinus Torvalds 	if (atomic_dec_and_test(&req->wb_complete))
10101da177e4SLinus Torvalds 		nfs_writepage_release(req);
10111da177e4SLinus Torvalds }
10121da177e4SLinus Torvalds 
1013788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops = {
1014788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_partial,
1015788e7a89STrond Myklebust 	.rpc_release = nfs_writedata_release,
1016788e7a89STrond Myklebust };
1017788e7a89STrond Myklebust 
10181da177e4SLinus Torvalds /*
10191da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
10201da177e4SLinus Torvalds  *
10211da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
10221da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
10231da177e4SLinus Torvalds  *	  as the page has a write request pending.
10241da177e4SLinus Torvalds  */
1025788e7a89STrond Myklebust static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
10261da177e4SLinus Torvalds {
1027788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
10281da177e4SLinus Torvalds 	struct nfs_page		*req;
10291da177e4SLinus Torvalds 	struct page		*page;
10301da177e4SLinus Torvalds 
1031788e7a89STrond Myklebust 	if (nfs_writeback_done(task, data) != 0)
1032788e7a89STrond Myklebust 		return;
1033788e7a89STrond Myklebust 
10341da177e4SLinus Torvalds 	/* Update attributes as result of writeback. */
10351da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
10361da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
10371da177e4SLinus Torvalds 		nfs_list_remove_request(req);
10381da177e4SLinus Torvalds 		page = req->wb_page;
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 		dprintk("NFS: write (%s/%Ld %d@%Ld)",
104188be9f99STrond Myklebust 			req->wb_context->path.dentry->d_inode->i_sb->s_id,
104288be9f99STrond Myklebust 			(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
10431da177e4SLinus Torvalds 			req->wb_bytes,
10441da177e4SLinus Torvalds 			(long long)req_offset(req));
10451da177e4SLinus Torvalds 
1046788e7a89STrond Myklebust 		if (task->tk_status < 0) {
1047a301b777STrond Myklebust 			nfs_set_pageerror(page);
10487b159fc1STrond Myklebust 			nfs_context_set_write_error(req->wb_context, task->tk_status);
1049788e7a89STrond Myklebust 			dprintk(", error = %d\n", task->tk_status);
10508e821cadSTrond Myklebust 			goto remove_request;
10511da177e4SLinus Torvalds 		}
10521da177e4SLinus Torvalds 
10538e821cadSTrond Myklebust 		if (nfs_write_need_commit(data)) {
10541da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
10551da177e4SLinus Torvalds 			nfs_mark_request_commit(req);
10568e821cadSTrond Myklebust 			nfs_end_page_writeback(page);
10571da177e4SLinus Torvalds 			dprintk(" marked for commit\n");
10588e821cadSTrond Myklebust 			goto next;
10598e821cadSTrond Myklebust 		}
106044dd151dSTrond Myklebust 		/* Set the PG_uptodate flag? */
106144dd151dSTrond Myklebust 		nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
10628e821cadSTrond Myklebust 		dprintk(" OK\n");
10638e821cadSTrond Myklebust remove_request:
10648e821cadSTrond Myklebust 		nfs_end_page_writeback(page);
10651da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
10661da177e4SLinus Torvalds 	next:
10679fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
10681da177e4SLinus Torvalds 	}
10691da177e4SLinus Torvalds }
10701da177e4SLinus Torvalds 
1071788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops = {
1072788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_full,
1073788e7a89STrond Myklebust 	.rpc_release = nfs_writedata_release,
1074788e7a89STrond Myklebust };
1075788e7a89STrond Myklebust 
1076788e7a89STrond Myklebust 
10771da177e4SLinus Torvalds /*
10781da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
10791da177e4SLinus Torvalds  */
1080462d5b32SChuck Lever int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
10811da177e4SLinus Torvalds {
10821da177e4SLinus Torvalds 	struct nfs_writeargs	*argp = &data->args;
10831da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
1084788e7a89STrond Myklebust 	int status;
10851da177e4SLinus Torvalds 
1086a3f565b1SChuck Lever 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
10871da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
10881da177e4SLinus Torvalds 
1089f551e44fSChuck Lever 	/*
1090f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1091f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1092f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1093f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1094f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1095f551e44fSChuck Lever 	 */
1096788e7a89STrond Myklebust 	status = NFS_PROTO(data->inode)->write_done(task, data);
1097788e7a89STrond Myklebust 	if (status != 0)
1098788e7a89STrond Myklebust 		return status;
109991d5b470SChuck Lever 	nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
110091d5b470SChuck Lever 
11011da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
11021da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
11031da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
11041da177e4SLinus Torvalds 		 * commit data to stable storage even though we
11051da177e4SLinus Torvalds 		 * requested it.
11061da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
11071da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
11081da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
11091da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
11101da177e4SLinus Torvalds 		 */
11111da177e4SLinus Torvalds 		static unsigned long    complain;
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
11141da177e4SLinus Torvalds 			dprintk("NFS: faulty NFS server %s:"
11151da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
111654ceac45SDavid Howells 				NFS_SERVER(data->inode)->nfs_client->cl_hostname,
11171da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
11181da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
11191da177e4SLinus Torvalds 		}
11201da177e4SLinus Torvalds 	}
11211da177e4SLinus Torvalds #endif
11221da177e4SLinus Torvalds 	/* Is this a short write? */
11231da177e4SLinus Torvalds 	if (task->tk_status >= 0 && resp->count < argp->count) {
11241da177e4SLinus Torvalds 		static unsigned long    complain;
11251da177e4SLinus Torvalds 
112691d5b470SChuck Lever 		nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
112791d5b470SChuck Lever 
11281da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
11291da177e4SLinus Torvalds 		if (resp->count != 0) {
11301da177e4SLinus Torvalds 			/* Was this an NFSv2 write or an NFSv3 stable write? */
11311da177e4SLinus Torvalds 			if (resp->verf->committed != NFS_UNSTABLE) {
11321da177e4SLinus Torvalds 				/* Resend from where the server left off */
11331da177e4SLinus Torvalds 				argp->offset += resp->count;
11341da177e4SLinus Torvalds 				argp->pgbase += resp->count;
11351da177e4SLinus Torvalds 				argp->count -= resp->count;
11361da177e4SLinus Torvalds 			} else {
11371da177e4SLinus Torvalds 				/* Resend as a stable write in order to avoid
11381da177e4SLinus Torvalds 				 * headaches in the case of a server crash.
11391da177e4SLinus Torvalds 				 */
11401da177e4SLinus Torvalds 				argp->stable = NFS_FILE_SYNC;
11411da177e4SLinus Torvalds 			}
11421da177e4SLinus Torvalds 			rpc_restart_call(task);
1143788e7a89STrond Myklebust 			return -EAGAIN;
11441da177e4SLinus Torvalds 		}
11451da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
11461da177e4SLinus Torvalds 			printk(KERN_WARNING
11471da177e4SLinus Torvalds 			       "NFS: Server wrote zero bytes, expected %u.\n",
11481da177e4SLinus Torvalds 					argp->count);
11491da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
11501da177e4SLinus Torvalds 		}
11511da177e4SLinus Torvalds 		/* Can't do anything about it except throw an error. */
11521da177e4SLinus Torvalds 		task->tk_status = -EIO;
11531da177e4SLinus Torvalds 	}
1154788e7a89STrond Myklebust 	return 0;
11551da177e4SLinus Torvalds }
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1159*383ba719STrond Myklebust void nfs_commit_release(void *data)
11601da177e4SLinus Torvalds {
1161*383ba719STrond Myklebust 	struct nfs_write_data *wdata = data;
1162*383ba719STrond Myklebust 
1163*383ba719STrond Myklebust 	put_nfs_open_context(wdata->args.context);
11641da177e4SLinus Torvalds 	nfs_commit_free(wdata);
11651da177e4SLinus Torvalds }
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds /*
11681da177e4SLinus Torvalds  * Set up the argument/result storage required for the RPC call.
11691da177e4SLinus Torvalds  */
11701da177e4SLinus Torvalds static void nfs_commit_rpcsetup(struct list_head *head,
1171788e7a89STrond Myklebust 		struct nfs_write_data *data,
1172788e7a89STrond Myklebust 		int how)
11731da177e4SLinus Torvalds {
117484115e1cSTrond Myklebust 	struct nfs_page *first = nfs_list_entry(head->next);
117584115e1cSTrond Myklebust 	struct inode *inode = first->wb_context->path.dentry->d_inode;
117684115e1cSTrond Myklebust 	int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
11773ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
117807737691STrond Myklebust 	struct rpc_task *task;
1179bdc7f021STrond Myklebust 	struct rpc_message msg = {
1180bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1181bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
1182bdc7f021STrond Myklebust 		.rpc_cred = first->wb_context->cred,
1183bdc7f021STrond Myklebust 	};
118484115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
118507737691STrond Myklebust 		.task = &data->task,
118684115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(inode),
1187bdc7f021STrond Myklebust 		.rpc_message = &msg,
118884115e1cSTrond Myklebust 		.callback_ops = &nfs_commit_ops,
118984115e1cSTrond Myklebust 		.callback_data = data,
119084115e1cSTrond Myklebust 		.flags = flags,
11913ff7576dSTrond Myklebust 		.priority = priority,
119284115e1cSTrond Myklebust 	};
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
11951da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds 	list_splice_init(head, &data->pages);
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds 	data->inode	  = inode;
1200bdc7f021STrond Myklebust 	data->cred	  = msg.rpc_cred;
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(data->inode);
12033da28eb1STrond Myklebust 	/* Note: we always request a commit of the entire inode */
12043da28eb1STrond Myklebust 	data->args.offset = 0;
12053da28eb1STrond Myklebust 	data->args.count  = 0;
1206*383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(first->wb_context);
12073da28eb1STrond Myklebust 	data->res.count   = 0;
12081da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
12091da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
12100e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
12111da177e4SLinus Torvalds 
1212788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
1213bdc7f021STrond Myklebust 	NFS_PROTO(inode)->commit_setup(data, &msg);
12141da177e4SLinus Torvalds 
1215a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1216bdc7f021STrond Myklebust 
121707737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
121807737691STrond Myklebust 	if (!IS_ERR(task))
121907737691STrond Myklebust 		rpc_put_task(task);
12201da177e4SLinus Torvalds }
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds /*
12231da177e4SLinus Torvalds  * Commit dirty pages
12241da177e4SLinus Torvalds  */
12251da177e4SLinus Torvalds static int
122640859d7eSChuck Lever nfs_commit_list(struct inode *inode, struct list_head *head, int how)
12271da177e4SLinus Torvalds {
12281da177e4SLinus Torvalds 	struct nfs_write_data	*data;
12291da177e4SLinus Torvalds 	struct nfs_page         *req;
12301da177e4SLinus Torvalds 
1231e9f7bee1STrond Myklebust 	data = nfs_commit_alloc();
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 	if (!data)
12341da177e4SLinus Torvalds 		goto out_bad;
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 	/* Set up the argument struct */
12371da177e4SLinus Torvalds 	nfs_commit_rpcsetup(head, data, how);
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 	return 0;
12401da177e4SLinus Torvalds  out_bad:
12411da177e4SLinus Torvalds 	while (!list_empty(head)) {
12421da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
12431da177e4SLinus Torvalds 		nfs_list_remove_request(req);
12441da177e4SLinus Torvalds 		nfs_mark_request_commit(req);
124583715ad5STrond Myklebust 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1246c9e51e41SPeter Zijlstra 		dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1247c9e51e41SPeter Zijlstra 				BDI_RECLAIMABLE);
12489fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
12491da177e4SLinus Torvalds 	}
12501da177e4SLinus Torvalds 	return -ENOMEM;
12511da177e4SLinus Torvalds }
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds /*
12541da177e4SLinus Torvalds  * COMMIT call returned
12551da177e4SLinus Torvalds  */
1256788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
12571da177e4SLinus Torvalds {
1258963d8fe5STrond Myklebust 	struct nfs_write_data	*data = calldata;
12591da177e4SLinus Torvalds 	struct nfs_page		*req;
12601da177e4SLinus Torvalds 
1261a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
12621da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
12631da177e4SLinus Torvalds 
1264788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1265788e7a89STrond Myklebust 	if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1266788e7a89STrond Myklebust 		return;
1267788e7a89STrond Myklebust 
12681da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
12691da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
12701da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1271612c9384STrond Myklebust 		clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
1272fd39fc85SChristoph Lameter 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1273c9e51e41SPeter Zijlstra 		dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1274c9e51e41SPeter Zijlstra 				BDI_RECLAIMABLE);
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds 		dprintk("NFS: commit (%s/%Ld %d@%Ld)",
127788be9f99STrond Myklebust 			req->wb_context->path.dentry->d_inode->i_sb->s_id,
127888be9f99STrond Myklebust 			(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
12791da177e4SLinus Torvalds 			req->wb_bytes,
12801da177e4SLinus Torvalds 			(long long)req_offset(req));
12811da177e4SLinus Torvalds 		if (task->tk_status < 0) {
12827b159fc1STrond Myklebust 			nfs_context_set_write_error(req->wb_context, task->tk_status);
12831da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
12841da177e4SLinus Torvalds 			dprintk(", error = %d\n", task->tk_status);
12851da177e4SLinus Torvalds 			goto next;
12861da177e4SLinus Torvalds 		}
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
12891da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
12901da177e4SLinus Torvalds 		if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
12911da177e4SLinus Torvalds 			/* We have a match */
129244dd151dSTrond Myklebust 			/* Set the PG_uptodate flag */
129344dd151dSTrond Myklebust 			nfs_mark_uptodate(req->wb_page, req->wb_pgbase,
129444dd151dSTrond Myklebust 					req->wb_bytes);
12951da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
12961da177e4SLinus Torvalds 			dprintk(" OK\n");
12971da177e4SLinus Torvalds 			goto next;
12981da177e4SLinus Torvalds 		}
12991da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
13001da177e4SLinus Torvalds 		dprintk(" mismatch\n");
130161822ab5STrond Myklebust 		nfs_redirty_request(req);
13021da177e4SLinus Torvalds 	next:
13039fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
13041da177e4SLinus Torvalds 	}
13051da177e4SLinus Torvalds }
1306788e7a89STrond Myklebust 
1307788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
1308788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1309788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1310788e7a89STrond Myklebust };
13111da177e4SLinus Torvalds 
13123da28eb1STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
13131da177e4SLinus Torvalds {
13141da177e4SLinus Torvalds 	LIST_HEAD(head);
13157d46a49fSTrond Myklebust 	int res;
13161da177e4SLinus Torvalds 
1317587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
13183da28eb1STrond Myklebust 	res = nfs_scan_commit(inode, &head, 0, 0);
1319587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
13201da177e4SLinus Torvalds 	if (res) {
13217d46a49fSTrond Myklebust 		int error = nfs_commit_list(inode, &head, how);
13221da177e4SLinus Torvalds 		if (error < 0)
13231da177e4SLinus Torvalds 			return error;
13243da28eb1STrond Myklebust 	}
13251da177e4SLinus Torvalds 	return res;
13261da177e4SLinus Torvalds }
1327c63c7b05STrond Myklebust #else
1328c63c7b05STrond Myklebust static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1329c63c7b05STrond Myklebust {
1330c63c7b05STrond Myklebust 	return 0;
1331c63c7b05STrond Myklebust }
13321da177e4SLinus Torvalds #endif
13331da177e4SLinus Torvalds 
13341c75950bSTrond Myklebust long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
13351da177e4SLinus Torvalds {
13361c75950bSTrond Myklebust 	struct inode *inode = mapping->host;
1337ca52fec1STrond Myklebust 	pgoff_t idx_start, idx_end;
13381c75950bSTrond Myklebust 	unsigned int npages = 0;
1339c42de9ddSTrond Myklebust 	LIST_HEAD(head);
134070b9ecbdSTrond Myklebust 	int nocommit = how & FLUSH_NOCOMMIT;
13413f442547STrond Myklebust 	long pages, ret;
13421da177e4SLinus Torvalds 
13431c75950bSTrond Myklebust 	/* FIXME */
13441c75950bSTrond Myklebust 	if (wbc->range_cyclic)
13451c75950bSTrond Myklebust 		idx_start = 0;
13461c75950bSTrond Myklebust 	else {
13471c75950bSTrond Myklebust 		idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
13481c75950bSTrond Myklebust 		idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
13491c75950bSTrond Myklebust 		if (idx_end > idx_start) {
1350ca52fec1STrond Myklebust 			pgoff_t l_npages = 1 + idx_end - idx_start;
13511c75950bSTrond Myklebust 			npages = l_npages;
13521c75950bSTrond Myklebust 			if (sizeof(npages) != sizeof(l_npages) &&
1353ca52fec1STrond Myklebust 					(pgoff_t)npages != l_npages)
13541c75950bSTrond Myklebust 				npages = 0;
13551c75950bSTrond Myklebust 		}
13561c75950bSTrond Myklebust 	}
1357c42de9ddSTrond Myklebust 	how &= ~FLUSH_NOCOMMIT;
1358587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
13591da177e4SLinus Torvalds 	do {
1360c42de9ddSTrond Myklebust 		ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1361c42de9ddSTrond Myklebust 		if (ret != 0)
1362c42de9ddSTrond Myklebust 			continue;
1363c42de9ddSTrond Myklebust 		if (nocommit)
1364c42de9ddSTrond Myklebust 			break;
1365d2ccddf0STrond Myklebust 		pages = nfs_scan_commit(inode, &head, idx_start, npages);
1366724c439cSTrond Myklebust 		if (pages == 0)
1367c42de9ddSTrond Myklebust 			break;
1368d2ccddf0STrond Myklebust 		if (how & FLUSH_INVALIDATE) {
1369587142f8STrond Myklebust 			spin_unlock(&inode->i_lock);
137083715ad5STrond Myklebust 			nfs_cancel_commit_list(&head);
1371e8e058e8STrond Myklebust 			ret = pages;
1372587142f8STrond Myklebust 			spin_lock(&inode->i_lock);
1373d2ccddf0STrond Myklebust 			continue;
1374d2ccddf0STrond Myklebust 		}
1375d2ccddf0STrond Myklebust 		pages += nfs_scan_commit(inode, &head, 0, 0);
1376587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
1377c42de9ddSTrond Myklebust 		ret = nfs_commit_list(inode, &head, how);
1378587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
1379587142f8STrond Myklebust 
1380c42de9ddSTrond Myklebust 	} while (ret >= 0);
1381587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
1382c42de9ddSTrond Myklebust 	return ret;
13831da177e4SLinus Torvalds }
13841da177e4SLinus Torvalds 
138534901f70STrond Myklebust static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
13861c75950bSTrond Myklebust {
13871c75950bSTrond Myklebust 	int ret;
13881c75950bSTrond Myklebust 
138934901f70STrond Myklebust 	ret = nfs_writepages(mapping, wbc);
139061822ab5STrond Myklebust 	if (ret < 0)
139161822ab5STrond Myklebust 		goto out;
139234901f70STrond Myklebust 	ret = nfs_sync_mapping_wait(mapping, wbc, how);
1393ed90ef51STrond Myklebust 	if (ret < 0)
1394ed90ef51STrond Myklebust 		goto out;
13951c75950bSTrond Myklebust 	return 0;
139661822ab5STrond Myklebust out:
1397e507d9ebSTrond Myklebust 	__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
13981c75950bSTrond Myklebust 	return ret;
13991c75950bSTrond Myklebust }
14001c75950bSTrond Myklebust 
140134901f70STrond Myklebust /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
140234901f70STrond Myklebust static int nfs_write_mapping(struct address_space *mapping, int how)
140334901f70STrond Myklebust {
140434901f70STrond Myklebust 	struct writeback_control wbc = {
140534901f70STrond Myklebust 		.bdi = mapping->backing_dev_info,
140634901f70STrond Myklebust 		.sync_mode = WB_SYNC_NONE,
140734901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
140834901f70STrond Myklebust 		.for_writepages = 1,
140934901f70STrond Myklebust 		.range_cyclic = 1,
141034901f70STrond Myklebust 	};
141134901f70STrond Myklebust 	int ret;
141234901f70STrond Myklebust 
141334901f70STrond Myklebust 	ret = __nfs_write_mapping(mapping, &wbc, how);
141434901f70STrond Myklebust 	if (ret < 0)
141534901f70STrond Myklebust 		return ret;
141634901f70STrond Myklebust 	wbc.sync_mode = WB_SYNC_ALL;
141734901f70STrond Myklebust 	return __nfs_write_mapping(mapping, &wbc, how);
141834901f70STrond Myklebust }
141934901f70STrond Myklebust 
1420ed90ef51STrond Myklebust /*
1421ed90ef51STrond Myklebust  * flush the inode to disk.
1422ed90ef51STrond Myklebust  */
1423ed90ef51STrond Myklebust int nfs_wb_all(struct inode *inode)
14241c75950bSTrond Myklebust {
1425ed90ef51STrond Myklebust 	return nfs_write_mapping(inode->i_mapping, 0);
1426ed90ef51STrond Myklebust }
14271c75950bSTrond Myklebust 
1428ed90ef51STrond Myklebust int nfs_wb_nocommit(struct inode *inode)
1429ed90ef51STrond Myklebust {
1430ed90ef51STrond Myklebust 	return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
14311c75950bSTrond Myklebust }
14321c75950bSTrond Myklebust 
14331b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
14341b3b4a1aSTrond Myklebust {
14351b3b4a1aSTrond Myklebust 	struct nfs_page *req;
14361b3b4a1aSTrond Myklebust 	loff_t range_start = page_offset(page);
14371b3b4a1aSTrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
14381b3b4a1aSTrond Myklebust 	struct writeback_control wbc = {
14391b3b4a1aSTrond Myklebust 		.bdi = page->mapping->backing_dev_info,
14401b3b4a1aSTrond Myklebust 		.sync_mode = WB_SYNC_ALL,
14411b3b4a1aSTrond Myklebust 		.nr_to_write = LONG_MAX,
14421b3b4a1aSTrond Myklebust 		.range_start = range_start,
14431b3b4a1aSTrond Myklebust 		.range_end = range_end,
14441b3b4a1aSTrond Myklebust 	};
14451b3b4a1aSTrond Myklebust 	int ret = 0;
14461b3b4a1aSTrond Myklebust 
14471b3b4a1aSTrond Myklebust 	BUG_ON(!PageLocked(page));
14481b3b4a1aSTrond Myklebust 	for (;;) {
14491b3b4a1aSTrond Myklebust 		req = nfs_page_find_request(page);
14501b3b4a1aSTrond Myklebust 		if (req == NULL)
14511b3b4a1aSTrond Myklebust 			goto out;
14521b3b4a1aSTrond Myklebust 		if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
14531b3b4a1aSTrond Myklebust 			nfs_release_request(req);
14541b3b4a1aSTrond Myklebust 			break;
14551b3b4a1aSTrond Myklebust 		}
14561b3b4a1aSTrond Myklebust 		if (nfs_lock_request_dontget(req)) {
14571b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
14581b3b4a1aSTrond Myklebust 			/*
14591b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
14601b3b4a1aSTrond Myklebust 			 * page as being dirty
14611b3b4a1aSTrond Myklebust 			 */
14621b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
14631b3b4a1aSTrond Myklebust 			nfs_unlock_request(req);
14641b3b4a1aSTrond Myklebust 			break;
14651b3b4a1aSTrond Myklebust 		}
14661b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
14671b3b4a1aSTrond Myklebust 		if (ret < 0)
14681b3b4a1aSTrond Myklebust 			goto out;
14691b3b4a1aSTrond Myklebust 	}
14701b3b4a1aSTrond Myklebust 	if (!PagePrivate(page))
14711b3b4a1aSTrond Myklebust 		return 0;
14721b3b4a1aSTrond Myklebust 	ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
14731b3b4a1aSTrond Myklebust out:
14741b3b4a1aSTrond Myklebust 	return ret;
14751b3b4a1aSTrond Myklebust }
14761b3b4a1aSTrond Myklebust 
14775334eb13SAdrian Bunk static int nfs_wb_page_priority(struct inode *inode, struct page *page,
14785334eb13SAdrian Bunk 				int how)
14791c75950bSTrond Myklebust {
14801c75950bSTrond Myklebust 	loff_t range_start = page_offset(page);
14811c75950bSTrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
14824d770ccfSTrond Myklebust 	struct writeback_control wbc = {
14834d770ccfSTrond Myklebust 		.bdi = page->mapping->backing_dev_info,
14844d770ccfSTrond Myklebust 		.sync_mode = WB_SYNC_ALL,
14854d770ccfSTrond Myklebust 		.nr_to_write = LONG_MAX,
14864d770ccfSTrond Myklebust 		.range_start = range_start,
14874d770ccfSTrond Myklebust 		.range_end = range_end,
14884d770ccfSTrond Myklebust 	};
14894d770ccfSTrond Myklebust 	int ret;
14901c75950bSTrond Myklebust 
14914d770ccfSTrond Myklebust 	BUG_ON(!PageLocked(page));
1492c63c7b05STrond Myklebust 	if (clear_page_dirty_for_io(page)) {
14934d770ccfSTrond Myklebust 		ret = nfs_writepage_locked(page, &wbc);
14944d770ccfSTrond Myklebust 		if (ret < 0)
14954d770ccfSTrond Myklebust 			goto out;
14964d770ccfSTrond Myklebust 	}
1497f40313acSTrond Myklebust 	if (!PagePrivate(page))
1498f40313acSTrond Myklebust 		return 0;
14994d770ccfSTrond Myklebust 	ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
15004d770ccfSTrond Myklebust 	if (ret >= 0)
15014d770ccfSTrond Myklebust 		return 0;
15024d770ccfSTrond Myklebust out:
1503e507d9ebSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_PAGES);
15044d770ccfSTrond Myklebust 	return ret;
15051c75950bSTrond Myklebust }
15061c75950bSTrond Myklebust 
15071c75950bSTrond Myklebust /*
15081c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
15091c75950bSTrond Myklebust  */
15101c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page* page)
15111c75950bSTrond Myklebust {
15124d770ccfSTrond Myklebust 	return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
15131c75950bSTrond Myklebust }
15141c75950bSTrond Myklebust 
1515f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
15161da177e4SLinus Torvalds {
15171da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
15181da177e4SLinus Torvalds 					     sizeof(struct nfs_write_data),
15191da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
152020c2df83SPaul Mundt 					     NULL);
15211da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
15221da177e4SLinus Torvalds 		return -ENOMEM;
15231da177e4SLinus Torvalds 
152493d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
15251da177e4SLinus Torvalds 						     nfs_wdata_cachep);
15261da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
15271da177e4SLinus Torvalds 		return -ENOMEM;
15281da177e4SLinus Torvalds 
152993d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
15301da177e4SLinus Torvalds 						      nfs_wdata_cachep);
15311da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
15321da177e4SLinus Torvalds 		return -ENOMEM;
15331da177e4SLinus Torvalds 
153489a09141SPeter Zijlstra 	/*
153589a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
153689a09141SPeter Zijlstra 	 *
153789a09141SPeter Zijlstra 	 *  64MB:    8192k
153889a09141SPeter Zijlstra 	 * 128MB:   11585k
153989a09141SPeter Zijlstra 	 * 256MB:   16384k
154089a09141SPeter Zijlstra 	 * 512MB:   23170k
154189a09141SPeter Zijlstra 	 *   1GB:   32768k
154289a09141SPeter Zijlstra 	 *   2GB:   46340k
154389a09141SPeter Zijlstra 	 *   4GB:   65536k
154489a09141SPeter Zijlstra 	 *   8GB:   92681k
154589a09141SPeter Zijlstra 	 *  16GB:  131072k
154689a09141SPeter Zijlstra 	 *
154789a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
154889a09141SPeter Zijlstra 	 * Limit the default to 256M
154989a09141SPeter Zijlstra 	 */
155089a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
155189a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
155289a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
155389a09141SPeter Zijlstra 
15541da177e4SLinus Torvalds 	return 0;
15551da177e4SLinus Torvalds }
15561da177e4SLinus Torvalds 
1557266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
15581da177e4SLinus Torvalds {
15591da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
15601da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
15611a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
15621da177e4SLinus Torvalds }
15631da177e4SLinus Torvalds 
1564