xref: /linux/fs/nfs/write.c (revision c988950eb6dd6f8e6d98503ca094622729e9aa13)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/fs/nfs/write.c
31da177e4SLinus Torvalds  *
47c85d900STrond Myklebust  * Write file data over NFS.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/types.h>
101da177e4SLinus Torvalds #include <linux/slab.h>
111da177e4SLinus Torvalds #include <linux/mm.h>
121da177e4SLinus Torvalds #include <linux/pagemap.h>
131da177e4SLinus Torvalds #include <linux/file.h>
141da177e4SLinus Torvalds #include <linux/writeback.h>
1589a09141SPeter Zijlstra #include <linux/swap.h>
16074cc1deSTrond Myklebust #include <linux/migrate.h>
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
191da177e4SLinus Torvalds #include <linux/nfs_fs.h>
201da177e4SLinus Torvalds #include <linux/nfs_mount.h>
211da177e4SLinus Torvalds #include <linux/nfs_page.h>
223fcfab16SAndrew Morton #include <linux/backing-dev.h>
233fcfab16SAndrew Morton 
241da177e4SLinus Torvalds #include <asm/uaccess.h>
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #include "delegation.h"
2749a70f27STrond Myklebust #include "internal.h"
2891d5b470SChuck Lever #include "iostat.h"
29def6ed7eSAndy Adamson #include "nfs4_fs.h"
30074cc1deSTrond Myklebust #include "fscache.h"
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds #define MIN_POOL_WRITE		(32)
351da177e4SLinus Torvalds #define MIN_POOL_COMMIT		(4)
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds /*
381da177e4SLinus Torvalds  * Local function declarations
391da177e4SLinus Torvalds  */
40c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc,
41c63c7b05STrond Myklebust 				  struct inode *inode, int ioflags);
42f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req);
43788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops;
44788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops;
45788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops;
461da177e4SLinus Torvalds 
47e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep;
483feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
491da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
501da177e4SLinus Torvalds 
51c9d8f89dSTrond Myklebust struct nfs_write_data *nfs_commitdata_alloc(void)
521da177e4SLinus Torvalds {
53e6b4f8daSChristoph Lameter 	struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
5440859d7eSChuck Lever 
551da177e4SLinus Torvalds 	if (p) {
561da177e4SLinus Torvalds 		memset(p, 0, sizeof(*p));
571da177e4SLinus Torvalds 		INIT_LIST_HEAD(&p->pages);
585f7dbd5cSAndy Adamson 		p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
591da177e4SLinus Torvalds 	}
601da177e4SLinus Torvalds 	return p;
611da177e4SLinus Torvalds }
621da177e4SLinus Torvalds 
635e4424afSTrond Myklebust void nfs_commit_free(struct nfs_write_data *p)
641da177e4SLinus Torvalds {
6540859d7eSChuck Lever 	if (p && (p->pagevec != &p->page_array[0]))
6640859d7eSChuck Lever 		kfree(p->pagevec);
671da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
681da177e4SLinus Torvalds }
691da177e4SLinus Torvalds 
708d5658c9STrond Myklebust struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
713feb2d49STrond Myklebust {
72e6b4f8daSChristoph Lameter 	struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
733feb2d49STrond Myklebust 
743feb2d49STrond Myklebust 	if (p) {
753feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
763feb2d49STrond Myklebust 		INIT_LIST_HEAD(&p->pages);
77e9f7bee1STrond Myklebust 		p->npages = pagecount;
785f7dbd5cSAndy Adamson 		p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
790d0b5cb3SChuck Lever 		if (pagecount <= ARRAY_SIZE(p->page_array))
800d0b5cb3SChuck Lever 			p->pagevec = p->page_array;
813feb2d49STrond Myklebust 		else {
820d0b5cb3SChuck Lever 			p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
830d0b5cb3SChuck Lever 			if (!p->pagevec) {
843feb2d49STrond Myklebust 				mempool_free(p, nfs_wdata_mempool);
853feb2d49STrond Myklebust 				p = NULL;
863feb2d49STrond Myklebust 			}
873feb2d49STrond Myklebust 		}
883feb2d49STrond Myklebust 	}
893feb2d49STrond Myklebust 	return p;
903feb2d49STrond Myklebust }
913feb2d49STrond Myklebust 
921ae88b2eSTrond Myklebust void nfs_writedata_free(struct nfs_write_data *p)
933feb2d49STrond Myklebust {
943feb2d49STrond Myklebust 	if (p && (p->pagevec != &p->page_array[0]))
953feb2d49STrond Myklebust 		kfree(p->pagevec);
963feb2d49STrond Myklebust 	mempool_free(p, nfs_wdata_mempool);
973feb2d49STrond Myklebust }
983feb2d49STrond Myklebust 
991ae88b2eSTrond Myklebust static void nfs_writedata_release(struct nfs_write_data *wdata)
1001da177e4SLinus Torvalds {
101383ba719STrond Myklebust 	put_nfs_open_context(wdata->args.context);
1021da177e4SLinus Torvalds 	nfs_writedata_free(wdata);
1031da177e4SLinus Torvalds }
1041da177e4SLinus Torvalds 
1057b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
1067b159fc1STrond Myklebust {
1077b159fc1STrond Myklebust 	ctx->error = error;
1087b159fc1STrond Myklebust 	smp_wmb();
1097b159fc1STrond Myklebust 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
1107b159fc1STrond Myklebust }
1117b159fc1STrond Myklebust 
112277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request_locked(struct page *page)
113277459d2STrond Myklebust {
114277459d2STrond Myklebust 	struct nfs_page *req = NULL;
115277459d2STrond Myklebust 
116277459d2STrond Myklebust 	if (PagePrivate(page)) {
117277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
118277459d2STrond Myklebust 		if (req != NULL)
119c03b4024STrond Myklebust 			kref_get(&req->wb_kref);
120277459d2STrond Myklebust 	}
121277459d2STrond Myklebust 	return req;
122277459d2STrond Myklebust }
123277459d2STrond Myklebust 
124277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page)
125277459d2STrond Myklebust {
126587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
127277459d2STrond Myklebust 	struct nfs_page *req = NULL;
128277459d2STrond Myklebust 
129587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
130277459d2STrond Myklebust 	req = nfs_page_find_request_locked(page);
131587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
132277459d2STrond Myklebust 	return req;
133277459d2STrond Myklebust }
134277459d2STrond Myklebust 
1351da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1361da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1371da177e4SLinus Torvalds {
1381da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
139a3d01454STrond Myklebust 	loff_t end, i_size;
140a3d01454STrond Myklebust 	pgoff_t end_index;
1411da177e4SLinus Torvalds 
142a3d01454STrond Myklebust 	spin_lock(&inode->i_lock);
143a3d01454STrond Myklebust 	i_size = i_size_read(inode);
144a3d01454STrond Myklebust 	end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
1451da177e4SLinus Torvalds 	if (i_size > 0 && page->index < end_index)
146a3d01454STrond Myklebust 		goto out;
1471da177e4SLinus Torvalds 	end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
1481da177e4SLinus Torvalds 	if (i_size >= end)
149a3d01454STrond Myklebust 		goto out;
1501da177e4SLinus Torvalds 	i_size_write(inode, end);
151a3d01454STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
152a3d01454STrond Myklebust out:
153a3d01454STrond Myklebust 	spin_unlock(&inode->i_lock);
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
156a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
157a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
158a301b777STrond Myklebust {
159a301b777STrond Myklebust 	SetPageError(page);
160a301b777STrond Myklebust 	nfs_zap_mapping(page->mapping->host, page->mapping);
161a301b777STrond Myklebust }
162a301b777STrond Myklebust 
1631da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
1641da177e4SLinus Torvalds  * covers the full page.
1651da177e4SLinus Torvalds  */
1661da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
1671da177e4SLinus Torvalds {
1681da177e4SLinus Torvalds 	if (PageUptodate(page))
1691da177e4SLinus Torvalds 		return;
1701da177e4SLinus Torvalds 	if (base != 0)
1711da177e4SLinus Torvalds 		return;
17249a70f27STrond Myklebust 	if (count != nfs_page_length(page))
1731da177e4SLinus Torvalds 		return;
1741da177e4SLinus Torvalds 	SetPageUptodate(page);
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
1781da177e4SLinus Torvalds {
1791da177e4SLinus Torvalds 	if (wbc->for_reclaim)
180c63c7b05STrond Myklebust 		return FLUSH_HIGHPRI | FLUSH_STABLE;
181b17621feSWu Fengguang 	if (wbc->for_kupdate || wbc->for_background)
1821da177e4SLinus Torvalds 		return FLUSH_LOWPRI;
1831da177e4SLinus Torvalds 	return 0;
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds /*
18789a09141SPeter Zijlstra  * NFS congestion control
18889a09141SPeter Zijlstra  */
18989a09141SPeter Zijlstra 
19089a09141SPeter Zijlstra int nfs_congestion_kb;
19189a09141SPeter Zijlstra 
19289a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
19389a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
19489a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
19589a09141SPeter Zijlstra 
1965a6d41b3STrond Myklebust static int nfs_set_page_writeback(struct page *page)
19789a09141SPeter Zijlstra {
1985a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
1995a6d41b3STrond Myklebust 
2005a6d41b3STrond Myklebust 	if (!ret) {
20189a09141SPeter Zijlstra 		struct inode *inode = page->mapping->host;
20289a09141SPeter Zijlstra 		struct nfs_server *nfss = NFS_SERVER(inode);
20389a09141SPeter Zijlstra 
204277866a0SPeter Zijlstra 		if (atomic_long_inc_return(&nfss->writeback) >
2058aa7e847SJens Axboe 				NFS_CONGESTION_ON_THRESH) {
2068aa7e847SJens Axboe 			set_bdi_congested(&nfss->backing_dev_info,
2078aa7e847SJens Axboe 						BLK_RW_ASYNC);
2088aa7e847SJens Axboe 		}
20989a09141SPeter Zijlstra 	}
2105a6d41b3STrond Myklebust 	return ret;
21189a09141SPeter Zijlstra }
21289a09141SPeter Zijlstra 
21389a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page)
21489a09141SPeter Zijlstra {
21589a09141SPeter Zijlstra 	struct inode *inode = page->mapping->host;
21689a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
21789a09141SPeter Zijlstra 
21889a09141SPeter Zijlstra 	end_page_writeback(page);
219c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
2208aa7e847SJens Axboe 		clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
22189a09141SPeter Zijlstra }
22289a09141SPeter Zijlstra 
223074cc1deSTrond Myklebust static struct nfs_page *nfs_find_and_lock_request(struct page *page)
224e261f51fSTrond Myklebust {
225587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
226e261f51fSTrond Myklebust 	struct nfs_page *req;
227e261f51fSTrond Myklebust 	int ret;
228e261f51fSTrond Myklebust 
229587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
230e261f51fSTrond Myklebust 	for (;;) {
231e261f51fSTrond Myklebust 		req = nfs_page_find_request_locked(page);
232074cc1deSTrond Myklebust 		if (req == NULL)
233074cc1deSTrond Myklebust 			break;
234acee478aSTrond Myklebust 		if (nfs_set_page_tag_locked(req))
235e261f51fSTrond Myklebust 			break;
236e261f51fSTrond Myklebust 		/* Note: If we hold the page lock, as is the case in nfs_writepage,
237acee478aSTrond Myklebust 		 *	 then the call to nfs_set_page_tag_locked() will always
238e261f51fSTrond Myklebust 		 *	 succeed provided that someone hasn't already marked the
239e261f51fSTrond Myklebust 		 *	 request as dirty (in which case we don't care).
240e261f51fSTrond Myklebust 		 */
241587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
242e261f51fSTrond Myklebust 		ret = nfs_wait_on_request(req);
243e261f51fSTrond Myklebust 		nfs_release_request(req);
244e261f51fSTrond Myklebust 		if (ret != 0)
245074cc1deSTrond Myklebust 			return ERR_PTR(ret);
246587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
247e261f51fSTrond Myklebust 	}
248587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
249074cc1deSTrond Myklebust 	return req;
250612c9384STrond Myklebust }
251074cc1deSTrond Myklebust 
252074cc1deSTrond Myklebust /*
253074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
254074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
255074cc1deSTrond Myklebust  */
256074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
257074cc1deSTrond Myklebust 				struct page *page)
258074cc1deSTrond Myklebust {
259074cc1deSTrond Myklebust 	struct nfs_page *req;
260074cc1deSTrond Myklebust 	int ret = 0;
261074cc1deSTrond Myklebust 
262074cc1deSTrond Myklebust 	req = nfs_find_and_lock_request(page);
263074cc1deSTrond Myklebust 	if (!req)
264074cc1deSTrond Myklebust 		goto out;
265074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
266074cc1deSTrond Myklebust 	if (IS_ERR(req))
267074cc1deSTrond Myklebust 		goto out;
268074cc1deSTrond Myklebust 
269074cc1deSTrond Myklebust 	ret = nfs_set_page_writeback(page);
270074cc1deSTrond Myklebust 	BUG_ON(ret != 0);
271074cc1deSTrond Myklebust 	BUG_ON(test_bit(PG_CLEAN, &req->wb_flags));
272074cc1deSTrond Myklebust 
273f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
274f8512ad0SFred Isaman 		nfs_redirty_request(req);
275074cc1deSTrond Myklebust 		ret = pgio->pg_error;
276f8512ad0SFred Isaman 	}
277074cc1deSTrond Myklebust out:
278074cc1deSTrond Myklebust 	return ret;
279e261f51fSTrond Myklebust }
280e261f51fSTrond Myklebust 
281f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
282f758c885STrond Myklebust {
283f758c885STrond Myklebust 	struct inode *inode = page->mapping->host;
284f758c885STrond Myklebust 
285f758c885STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
286f758c885STrond Myklebust 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
287f758c885STrond Myklebust 
288f758c885STrond Myklebust 	nfs_pageio_cond_complete(pgio, page->index);
289f758c885STrond Myklebust 	return nfs_page_async_flush(pgio, page);
290f758c885STrond Myklebust }
291f758c885STrond Myklebust 
292e261f51fSTrond Myklebust /*
2931da177e4SLinus Torvalds  * Write an mmapped page to the server.
2941da177e4SLinus Torvalds  */
2954d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
2961da177e4SLinus Torvalds {
297f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
298e261f51fSTrond Myklebust 	int err;
2991da177e4SLinus Torvalds 
300f758c885STrond Myklebust 	nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
301f758c885STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
302f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
303f758c885STrond Myklebust 	if (err < 0)
3044d770ccfSTrond Myklebust 		return err;
305f758c885STrond Myklebust 	if (pgio.pg_error < 0)
306f758c885STrond Myklebust 		return pgio.pg_error;
307f758c885STrond Myklebust 	return 0;
3084d770ccfSTrond Myklebust }
3094d770ccfSTrond Myklebust 
3104d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
3114d770ccfSTrond Myklebust {
312f758c885STrond Myklebust 	int ret;
3134d770ccfSTrond Myklebust 
314f758c885STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
3151da177e4SLinus Torvalds 	unlock_page(page);
316f758c885STrond Myklebust 	return ret;
317f758c885STrond Myklebust }
318f758c885STrond Myklebust 
319f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
320f758c885STrond Myklebust {
321f758c885STrond Myklebust 	int ret;
322f758c885STrond Myklebust 
323f758c885STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
324f758c885STrond Myklebust 	unlock_page(page);
325f758c885STrond Myklebust 	return ret;
3261da177e4SLinus Torvalds }
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3291da177e4SLinus Torvalds {
3301da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
33172cb77f4STrond Myklebust 	unsigned long *bitlock = &NFS_I(inode)->flags;
332c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
3331da177e4SLinus Torvalds 	int err;
3341da177e4SLinus Torvalds 
33572cb77f4STrond Myklebust 	/* Stop dirtying of new pages while we sync */
33672cb77f4STrond Myklebust 	err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
33772cb77f4STrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
33872cb77f4STrond Myklebust 	if (err)
33972cb77f4STrond Myklebust 		goto out_err;
34072cb77f4STrond Myklebust 
34191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
34291d5b470SChuck Lever 
343c63c7b05STrond Myklebust 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
344f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
345c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
34672cb77f4STrond Myklebust 
34772cb77f4STrond Myklebust 	clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
34872cb77f4STrond Myklebust 	smp_mb__after_clear_bit();
34972cb77f4STrond Myklebust 	wake_up_bit(bitlock, NFS_INO_FLUSHING);
35072cb77f4STrond Myklebust 
351f758c885STrond Myklebust 	if (err < 0)
35272cb77f4STrond Myklebust 		goto out_err;
35372cb77f4STrond Myklebust 	err = pgio.pg_error;
35472cb77f4STrond Myklebust 	if (err < 0)
35572cb77f4STrond Myklebust 		goto out_err;
356c63c7b05STrond Myklebust 	return 0;
35772cb77f4STrond Myklebust out_err:
35872cb77f4STrond Myklebust 	return err;
3591da177e4SLinus Torvalds }
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds /*
3621da177e4SLinus Torvalds  * Insert a write request into an inode
3631da177e4SLinus Torvalds  */
364e7d39069STrond Myklebust static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
3651da177e4SLinus Torvalds {
3661da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
3671da177e4SLinus Torvalds 	int error;
3681da177e4SLinus Torvalds 
369e7d39069STrond Myklebust 	error = radix_tree_preload(GFP_NOFS);
370e7d39069STrond Myklebust 	if (error != 0)
371e7d39069STrond Myklebust 		goto out;
372e7d39069STrond Myklebust 
373e7d39069STrond Myklebust 	/* Lock the request! */
374e7d39069STrond Myklebust 	nfs_lock_request_dontget(req);
375e7d39069STrond Myklebust 
376e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
3771da177e4SLinus Torvalds 	error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
37827852596SNick Piggin 	BUG_ON(error);
3791da177e4SLinus Torvalds 	if (!nfsi->npages) {
3801da177e4SLinus Torvalds 		igrab(inode);
3811da177e4SLinus Torvalds 		if (nfs_have_delegation(inode, FMODE_WRITE))
3821da177e4SLinus Torvalds 			nfsi->change_attr++;
3831da177e4SLinus Torvalds 	}
384deb7d638STrond Myklebust 	SetPagePrivate(req->wb_page);
385277459d2STrond Myklebust 	set_page_private(req->wb_page, (unsigned long)req);
3861da177e4SLinus Torvalds 	nfsi->npages++;
387c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
38827852596SNick Piggin 	radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
38927852596SNick Piggin 				NFS_PAGE_TAG_LOCKED);
390e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
391e7d39069STrond Myklebust 	radix_tree_preload_end();
392e7d39069STrond Myklebust out:
393e7d39069STrond Myklebust 	return error;
3941da177e4SLinus Torvalds }
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds /*
39789a09141SPeter Zijlstra  * Remove a write request from an inode
3981da177e4SLinus Torvalds  */
3991da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
4001da177e4SLinus Torvalds {
40188be9f99STrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
4021da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 	BUG_ON (!NFS_WBACK_BUSY(req));
4051da177e4SLinus Torvalds 
406587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
407277459d2STrond Myklebust 	set_page_private(req->wb_page, 0);
408deb7d638STrond Myklebust 	ClearPagePrivate(req->wb_page);
4091da177e4SLinus Torvalds 	radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
4101da177e4SLinus Torvalds 	nfsi->npages--;
4111da177e4SLinus Torvalds 	if (!nfsi->npages) {
412587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
4131da177e4SLinus Torvalds 		iput(inode);
4141da177e4SLinus Torvalds 	} else
415587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
4161da177e4SLinus Torvalds 	nfs_clear_request(req);
4171da177e4SLinus Torvalds 	nfs_release_request(req);
4181da177e4SLinus Torvalds }
4191da177e4SLinus Torvalds 
42061822ab5STrond Myklebust static void
4216d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
42261822ab5STrond Myklebust {
42361822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
42461822ab5STrond Myklebust }
42561822ab5STrond Myklebust 
4261da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
4271da177e4SLinus Torvalds /*
4281da177e4SLinus Torvalds  * Add a request to the inode's commit list.
4291da177e4SLinus Torvalds  */
4301da177e4SLinus Torvalds static void
4311da177e4SLinus Torvalds nfs_mark_request_commit(struct nfs_page *req)
4321da177e4SLinus Torvalds {
43388be9f99STrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
4341da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4351da177e4SLinus Torvalds 
436587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
437e468bae9STrond Myklebust 	set_bit(PG_CLEAN, &(req)->wb_flags);
4385c369683STrond Myklebust 	radix_tree_tag_set(&nfsi->nfs_page_tree,
4395c369683STrond Myklebust 			req->wb_index,
4405c369683STrond Myklebust 			NFS_PAGE_TAG_COMMIT);
441ff778d02STrond Myklebust 	nfsi->ncommit++;
442587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
443fd39fc85SChristoph Lameter 	inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
444c9e51e41SPeter Zijlstra 	inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
445a1803044STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
4461da177e4SLinus Torvalds }
4478e821cadSTrond Myklebust 
448e468bae9STrond Myklebust static int
449e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
450e468bae9STrond Myklebust {
451e468bae9STrond Myklebust 	struct page *page = req->wb_page;
452e468bae9STrond Myklebust 
453e468bae9STrond Myklebust 	if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) {
454e468bae9STrond Myklebust 		dec_zone_page_state(page, NR_UNSTABLE_NFS);
455e468bae9STrond Myklebust 		dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
456e468bae9STrond Myklebust 		return 1;
457e468bae9STrond Myklebust 	}
458e468bae9STrond Myklebust 	return 0;
459e468bae9STrond Myklebust }
460e468bae9STrond Myklebust 
4618e821cadSTrond Myklebust static inline
4628e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
4638e821cadSTrond Myklebust {
4648e821cadSTrond Myklebust 	return data->verf.committed != NFS_FILE_SYNC;
4658e821cadSTrond Myklebust }
4668e821cadSTrond Myklebust 
4678e821cadSTrond Myklebust static inline
4688e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req)
4698e821cadSTrond Myklebust {
470e468bae9STrond Myklebust 	if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) {
4718e821cadSTrond Myklebust 		nfs_mark_request_commit(req);
4728e821cadSTrond Myklebust 		return 1;
4738e821cadSTrond Myklebust 	}
4748e821cadSTrond Myklebust 	if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
4756d884e8fSFred 		nfs_mark_request_dirty(req);
4768e821cadSTrond Myklebust 		return 1;
4778e821cadSTrond Myklebust 	}
4788e821cadSTrond Myklebust 	return 0;
4798e821cadSTrond Myklebust }
4808e821cadSTrond Myklebust #else
4818e821cadSTrond Myklebust static inline void
4828e821cadSTrond Myklebust nfs_mark_request_commit(struct nfs_page *req)
4838e821cadSTrond Myklebust {
4848e821cadSTrond Myklebust }
4858e821cadSTrond Myklebust 
486e468bae9STrond Myklebust static inline int
487e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
488e468bae9STrond Myklebust {
489e468bae9STrond Myklebust 	return 0;
490e468bae9STrond Myklebust }
491e468bae9STrond Myklebust 
4928e821cadSTrond Myklebust static inline
4938e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
4948e821cadSTrond Myklebust {
4958e821cadSTrond Myklebust 	return 0;
4968e821cadSTrond Myklebust }
4978e821cadSTrond Myklebust 
4988e821cadSTrond Myklebust static inline
4998e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req)
5008e821cadSTrond Myklebust {
5018e821cadSTrond Myklebust 	return 0;
5028e821cadSTrond Myklebust }
5031da177e4SLinus Torvalds #endif
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds /*
5061da177e4SLinus Torvalds  * Wait for a request to complete.
5071da177e4SLinus Torvalds  *
508150030b7SMatthew Wilcox  * Interruptible by fatal signals only.
5091da177e4SLinus Torvalds  */
510ca52fec1STrond Myklebust static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
5111da177e4SLinus Torvalds {
5121da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
5131da177e4SLinus Torvalds 	struct nfs_page *req;
514ca52fec1STrond Myklebust 	pgoff_t idx_end, next;
5151da177e4SLinus Torvalds 	unsigned int		res = 0;
5161da177e4SLinus Torvalds 	int			error;
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	if (npages == 0)
5191da177e4SLinus Torvalds 		idx_end = ~0;
5201da177e4SLinus Torvalds 	else
5211da177e4SLinus Torvalds 		idx_end = idx_start + npages - 1;
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 	next = idx_start;
5249fd367f0STrond Myklebust 	while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) {
5251da177e4SLinus Torvalds 		if (req->wb_index > idx_end)
5261da177e4SLinus Torvalds 			break;
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 		next = req->wb_index + 1;
529c6a556b8STrond Myklebust 		BUG_ON(!NFS_WBACK_BUSY(req));
5301da177e4SLinus Torvalds 
531c03b4024STrond Myklebust 		kref_get(&req->wb_kref);
532587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
5331da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
5341da177e4SLinus Torvalds 		nfs_release_request(req);
535587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
5361da177e4SLinus Torvalds 		if (error < 0)
5371da177e4SLinus Torvalds 			return error;
5381da177e4SLinus Torvalds 		res++;
5391da177e4SLinus Torvalds 	}
5401da177e4SLinus Torvalds 	return res;
5411da177e4SLinus Torvalds }
5421da177e4SLinus Torvalds 
54347c62564STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
544fb8a1f11STrond Myklebust static int
545fb8a1f11STrond Myklebust nfs_need_commit(struct nfs_inode *nfsi)
546fb8a1f11STrond Myklebust {
547fb8a1f11STrond Myklebust 	return radix_tree_tagged(&nfsi->nfs_page_tree, NFS_PAGE_TAG_COMMIT);
548fb8a1f11STrond Myklebust }
549fb8a1f11STrond Myklebust 
5501da177e4SLinus Torvalds /*
5511da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
5521da177e4SLinus Torvalds  * @inode: NFS inode to scan
5531da177e4SLinus Torvalds  * @dst: destination list
5541da177e4SLinus Torvalds  * @idx_start: lower bound of page->index to scan.
5551da177e4SLinus Torvalds  * @npages: idx_start + npages sets the upper bound to scan.
5561da177e4SLinus Torvalds  *
5571da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
5581da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
5591da177e4SLinus Torvalds  */
5601da177e4SLinus Torvalds static int
561ca52fec1STrond Myklebust nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
5621da177e4SLinus Torvalds {
5631da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
564ff778d02STrond Myklebust 	int ret;
5653da28eb1STrond Myklebust 
566fb8a1f11STrond Myklebust 	if (!nfs_need_commit(nfsi))
567fb8a1f11STrond Myklebust 		return 0;
568fb8a1f11STrond Myklebust 
569ff778d02STrond Myklebust 	ret = nfs_scan_list(nfsi, dst, idx_start, npages, NFS_PAGE_TAG_COMMIT);
570ff778d02STrond Myklebust 	if (ret > 0)
571ff778d02STrond Myklebust 		nfsi->ncommit -= ret;
5722928db1fSTrond Myklebust 	if (nfs_need_commit(NFS_I(inode)))
5732928db1fSTrond Myklebust 		__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
574ff778d02STrond Myklebust 	return ret;
5751da177e4SLinus Torvalds }
576c42de9ddSTrond Myklebust #else
577fb8a1f11STrond Myklebust static inline int nfs_need_commit(struct nfs_inode *nfsi)
578fb8a1f11STrond Myklebust {
579fb8a1f11STrond Myklebust 	return 0;
580fb8a1f11STrond Myklebust }
581fb8a1f11STrond Myklebust 
582ca52fec1STrond Myklebust static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
583c42de9ddSTrond Myklebust {
584c42de9ddSTrond Myklebust 	return 0;
585c42de9ddSTrond Myklebust }
5861da177e4SLinus Torvalds #endif
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds /*
589e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
590e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
5911da177e4SLinus Torvalds  *
592e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
593e7d39069STrond Myklebust  * to disk.
5941da177e4SLinus Torvalds  */
595e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
596e7d39069STrond Myklebust 		struct page *page,
597e7d39069STrond Myklebust 		unsigned int offset,
598e7d39069STrond Myklebust 		unsigned int bytes)
5991da177e4SLinus Torvalds {
600e7d39069STrond Myklebust 	struct nfs_page *req;
601e7d39069STrond Myklebust 	unsigned int rqend;
602e7d39069STrond Myklebust 	unsigned int end;
6031da177e4SLinus Torvalds 	int error;
604277459d2STrond Myklebust 
605e7d39069STrond Myklebust 	if (!PagePrivate(page))
606e7d39069STrond Myklebust 		return NULL;
607e7d39069STrond Myklebust 
608e7d39069STrond Myklebust 	end = offset + bytes;
609e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
610e7d39069STrond Myklebust 
611e7d39069STrond Myklebust 	for (;;) {
612e7d39069STrond Myklebust 		req = nfs_page_find_request_locked(page);
613e7d39069STrond Myklebust 		if (req == NULL)
614e7d39069STrond Myklebust 			goto out_unlock;
615e7d39069STrond Myklebust 
616e7d39069STrond Myklebust 		rqend = req->wb_offset + req->wb_bytes;
617e7d39069STrond Myklebust 		/*
618e7d39069STrond Myklebust 		 * Tell the caller to flush out the request if
619e7d39069STrond Myklebust 		 * the offsets are non-contiguous.
620e7d39069STrond Myklebust 		 * Note: nfs_flush_incompatible() will already
621e7d39069STrond Myklebust 		 * have flushed out requests having wrong owners.
622e7d39069STrond Myklebust 		 */
623e468bae9STrond Myklebust 		if (offset > rqend
624e7d39069STrond Myklebust 		    || end < req->wb_offset)
625e7d39069STrond Myklebust 			goto out_flushme;
626e7d39069STrond Myklebust 
627e7d39069STrond Myklebust 		if (nfs_set_page_tag_locked(req))
628e7d39069STrond Myklebust 			break;
629e7d39069STrond Myklebust 
630e7d39069STrond Myklebust 		/* The request is locked, so wait and then retry */
631587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
6321da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
6331da177e4SLinus Torvalds 		nfs_release_request(req);
634e7d39069STrond Myklebust 		if (error != 0)
635e7d39069STrond Myklebust 			goto out_err;
636e7d39069STrond Myklebust 		spin_lock(&inode->i_lock);
6371da177e4SLinus Torvalds 	}
6381da177e4SLinus Torvalds 
639ff778d02STrond Myklebust 	if (nfs_clear_request_commit(req) &&
640e468bae9STrond Myklebust 			radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree,
641ff778d02STrond Myklebust 				req->wb_index, NFS_PAGE_TAG_COMMIT) != NULL)
642ff778d02STrond Myklebust 		NFS_I(inode)->ncommit--;
643e468bae9STrond Myklebust 
6441da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
6451da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
6461da177e4SLinus Torvalds 		req->wb_offset = offset;
6471da177e4SLinus Torvalds 		req->wb_pgbase = offset;
6481da177e4SLinus Torvalds 	}
6491da177e4SLinus Torvalds 	if (end > rqend)
6501da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
651e7d39069STrond Myklebust 	else
652e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
653e7d39069STrond Myklebust out_unlock:
654e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
655e7d39069STrond Myklebust 	return req;
656e7d39069STrond Myklebust out_flushme:
657e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
658e7d39069STrond Myklebust 	nfs_release_request(req);
659e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
660e7d39069STrond Myklebust out_err:
661e7d39069STrond Myklebust 	return ERR_PTR(error);
662e7d39069STrond Myklebust }
6631da177e4SLinus Torvalds 
664e7d39069STrond Myklebust /*
665e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
666e7d39069STrond Myklebust  *
667e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
668e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
669e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
670e7d39069STrond Myklebust  */
671e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
672e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
673e7d39069STrond Myklebust {
674e7d39069STrond Myklebust 	struct inode *inode = page->mapping->host;
675e7d39069STrond Myklebust 	struct nfs_page	*req;
676e7d39069STrond Myklebust 	int error;
677e7d39069STrond Myklebust 
678e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
679e7d39069STrond Myklebust 	if (req != NULL)
680e7d39069STrond Myklebust 		goto out;
681e7d39069STrond Myklebust 	req = nfs_create_request(ctx, inode, page, offset, bytes);
682e7d39069STrond Myklebust 	if (IS_ERR(req))
683e7d39069STrond Myklebust 		goto out;
684e7d39069STrond Myklebust 	error = nfs_inode_add_request(inode, req);
685e7d39069STrond Myklebust 	if (error != 0) {
686e7d39069STrond Myklebust 		nfs_release_request(req);
687e7d39069STrond Myklebust 		req = ERR_PTR(error);
688e7d39069STrond Myklebust 	}
689efc91ed0STrond Myklebust out:
69061e930a9STrond Myklebust 	return req;
6911da177e4SLinus Torvalds }
6921da177e4SLinus Torvalds 
693e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
694e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
695e7d39069STrond Myklebust {
696e7d39069STrond Myklebust 	struct nfs_page	*req;
697e7d39069STrond Myklebust 
698e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
699e7d39069STrond Myklebust 	if (IS_ERR(req))
700e7d39069STrond Myklebust 		return PTR_ERR(req);
701e7d39069STrond Myklebust 	/* Update file length */
702e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
703e7d39069STrond Myklebust 	nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
704e7d39069STrond Myklebust 	nfs_clear_page_tag_locked(req);
705e7d39069STrond Myklebust 	return 0;
706e7d39069STrond Myklebust }
707e7d39069STrond Myklebust 
7081da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
7091da177e4SLinus Torvalds {
710cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
7111da177e4SLinus Torvalds 	struct nfs_page	*req;
7121a54533eSTrond Myklebust 	int do_flush, status;
7131da177e4SLinus Torvalds 	/*
7141da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
7151da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
7161da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
7171da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
7181da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
7191da177e4SLinus Torvalds 	 * dropped page.
7201da177e4SLinus Torvalds 	 */
7211a54533eSTrond Myklebust 	do {
722277459d2STrond Myklebust 		req = nfs_page_find_request(page);
7231a54533eSTrond Myklebust 		if (req == NULL)
7241a54533eSTrond Myklebust 			return 0;
725e468bae9STrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx;
7261da177e4SLinus Torvalds 		nfs_release_request(req);
7271a54533eSTrond Myklebust 		if (!do_flush)
7281a54533eSTrond Myklebust 			return 0;
729277459d2STrond Myklebust 		status = nfs_wb_page(page->mapping->host, page);
7301a54533eSTrond Myklebust 	} while (status == 0);
7311a54533eSTrond Myklebust 	return status;
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds /*
7355d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
7365d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
7375d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
7385d47a356STrond Myklebust  */
7395d47a356STrond Myklebust static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
7405d47a356STrond Myklebust {
7415d47a356STrond Myklebust 	return PageUptodate(page) &&
7425d47a356STrond Myklebust 		!(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
7435d47a356STrond Myklebust }
7445d47a356STrond Myklebust 
7455d47a356STrond Myklebust /*
7461da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
7471da177e4SLinus Torvalds  *
7481da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
7491da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
7501da177e4SLinus Torvalds  */
7511da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
7521da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
7531da177e4SLinus Torvalds {
754cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
7551da177e4SLinus Torvalds 	struct inode	*inode = page->mapping->host;
7561da177e4SLinus Torvalds 	int		status = 0;
7571da177e4SLinus Torvalds 
75891d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
75991d5b470SChuck Lever 
76048186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage(%s/%s %d@%lld)\n",
76101cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
76201cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name, count,
7630bbacc40SChuck Lever 		(long long)(page_offset(page) + offset));
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	/* If we're not using byte range locks, and we know the page
7665d47a356STrond Myklebust 	 * is up to date, it may be more efficient to extend the write
7675d47a356STrond Myklebust 	 * to cover the entire page in order to avoid fragmentation
7685d47a356STrond Myklebust 	 * inefficiencies.
7691da177e4SLinus Torvalds 	 */
7705d47a356STrond Myklebust 	if (nfs_write_pageuptodate(page, inode) &&
7715d47a356STrond Myklebust 			inode->i_flock == NULL &&
7726b2f3d1fSChristoph Hellwig 			!(file->f_flags & O_DSYNC)) {
77349a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
7741da177e4SLinus Torvalds 		offset = 0;
7751da177e4SLinus Torvalds 	}
7761da177e4SLinus Torvalds 
777e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
77803fa9e84STrond Myklebust 	if (status < 0)
77903fa9e84STrond Myklebust 		nfs_set_pageerror(page);
78003fa9e84STrond Myklebust 	else
781e261f51fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
7821da177e4SLinus Torvalds 
78348186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
7841da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
7851da177e4SLinus Torvalds 	return status;
7861da177e4SLinus Torvalds }
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds static void nfs_writepage_release(struct nfs_page *req)
7891da177e4SLinus Torvalds {
7908e821cadSTrond Myklebust 
7917e5f6146STrond Myklebust 	if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req)) {
79289a09141SPeter Zijlstra 		nfs_end_page_writeback(req->wb_page);
7931da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
7948e821cadSTrond Myklebust 	} else
7958e821cadSTrond Myklebust 		nfs_end_page_writeback(req->wb_page);
7969fd367f0STrond Myklebust 	nfs_clear_page_tag_locked(req);
7971da177e4SLinus Torvalds }
7981da177e4SLinus Torvalds 
7993ff7576dSTrond Myklebust static int flush_task_priority(int how)
8001da177e4SLinus Torvalds {
8011da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
8021da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
8031da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
8041da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
8051da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
8061da177e4SLinus Torvalds 	}
8071da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
8081da177e4SLinus Torvalds }
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds /*
8111da177e4SLinus Torvalds  * Set up the argument/result storage required for the RPC call.
8121da177e4SLinus Torvalds  */
813dbae4c73STrond Myklebust static int nfs_write_rpcsetup(struct nfs_page *req,
8141da177e4SLinus Torvalds 		struct nfs_write_data *data,
815788e7a89STrond Myklebust 		const struct rpc_call_ops *call_ops,
8161da177e4SLinus Torvalds 		unsigned int count, unsigned int offset,
8171da177e4SLinus Torvalds 		int how)
8181da177e4SLinus Torvalds {
81984115e1cSTrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
82084115e1cSTrond Myklebust 	int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
8213ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
82207737691STrond Myklebust 	struct rpc_task *task;
823bdc7f021STrond Myklebust 	struct rpc_message msg = {
824bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
825bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
826bdc7f021STrond Myklebust 		.rpc_cred = req->wb_context->cred,
827bdc7f021STrond Myklebust 	};
82884115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
82984115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(inode),
83007737691STrond Myklebust 		.task = &data->task,
831bdc7f021STrond Myklebust 		.rpc_message = &msg,
83284115e1cSTrond Myklebust 		.callback_ops = call_ops,
83384115e1cSTrond Myklebust 		.callback_data = data,
834101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
83584115e1cSTrond Myklebust 		.flags = flags,
8363ff7576dSTrond Myklebust 		.priority = priority,
83784115e1cSTrond Myklebust 	};
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
8401da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	data->req = req;
84388be9f99STrond Myklebust 	data->inode = inode = req->wb_context->path.dentry->d_inode;
844bdc7f021STrond Myklebust 	data->cred = msg.rpc_cred;
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(inode);
8471da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
8481da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
8491da177e4SLinus Torvalds 	data->args.pages  = data->pagevec;
8501da177e4SLinus Torvalds 	data->args.count  = count;
851383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
852bdc7f021STrond Myklebust 	data->args.stable  = NFS_UNSTABLE;
853bdc7f021STrond Myklebust 	if (how & FLUSH_STABLE) {
854bdc7f021STrond Myklebust 		data->args.stable = NFS_DATA_SYNC;
855fb8a1f11STrond Myklebust 		if (!nfs_need_commit(NFS_I(inode)))
856bdc7f021STrond Myklebust 			data->args.stable = NFS_FILE_SYNC;
857bdc7f021STrond Myklebust 	}
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
8601da177e4SLinus Torvalds 	data->res.count   = count;
8611da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
8620e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
8631da177e4SLinus Torvalds 
864788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
865bdc7f021STrond Myklebust 	NFS_PROTO(inode)->write_setup(data, &msg);
8661da177e4SLinus Torvalds 
867a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated write call "
86848186c7dSChuck Lever 		"(req %s/%lld, %u bytes @ offset %llu)\n",
8690bbacc40SChuck Lever 		data->task.tk_pid,
8701da177e4SLinus Torvalds 		inode->i_sb->s_id,
8711da177e4SLinus Torvalds 		(long long)NFS_FILEID(inode),
8721da177e4SLinus Torvalds 		count,
8731da177e4SLinus Torvalds 		(unsigned long long)data->args.offset);
8741da177e4SLinus Torvalds 
87507737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
876dbae4c73STrond Myklebust 	if (IS_ERR(task))
877dbae4c73STrond Myklebust 		return PTR_ERR(task);
87807737691STrond Myklebust 	rpc_put_task(task);
879dbae4c73STrond Myklebust 	return 0;
8801da177e4SLinus Torvalds }
8811da177e4SLinus Torvalds 
8826d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
8836d884e8fSFred  * call this on each, which will prepare them to be retried on next
8846d884e8fSFred  * writeback using standard nfs.
8856d884e8fSFred  */
8866d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
8876d884e8fSFred {
8886d884e8fSFred 	nfs_mark_request_dirty(req);
8896d884e8fSFred 	nfs_end_page_writeback(req->wb_page);
8906d884e8fSFred 	nfs_clear_page_tag_locked(req);
8916d884e8fSFred }
8926d884e8fSFred 
8931da177e4SLinus Torvalds /*
8941da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
8951da177e4SLinus Torvalds  * contiguous dirty area on one page.
8961da177e4SLinus Torvalds  */
8978d5658c9STrond Myklebust static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
8981da177e4SLinus Torvalds {
8991da177e4SLinus Torvalds 	struct nfs_page *req = nfs_list_entry(head->next);
9001da177e4SLinus Torvalds 	struct page *page = req->wb_page;
9011da177e4SLinus Torvalds 	struct nfs_write_data *data;
902e9f7bee1STrond Myklebust 	size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
903e9f7bee1STrond Myklebust 	unsigned int offset;
9041da177e4SLinus Torvalds 	int requests = 0;
905dbae4c73STrond Myklebust 	int ret = 0;
9061da177e4SLinus Torvalds 	LIST_HEAD(list);
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	nfs_list_remove_request(req);
9091da177e4SLinus Torvalds 
910bcb71bbaSTrond Myklebust 	nbytes = count;
911e9f7bee1STrond Myklebust 	do {
912e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
913e9f7bee1STrond Myklebust 
9148d5658c9STrond Myklebust 		data = nfs_writedata_alloc(1);
9151da177e4SLinus Torvalds 		if (!data)
9161da177e4SLinus Torvalds 			goto out_bad;
9171da177e4SLinus Torvalds 		list_add(&data->pages, &list);
9181da177e4SLinus Torvalds 		requests++;
919e9f7bee1STrond Myklebust 		nbytes -= len;
920e9f7bee1STrond Myklebust 	} while (nbytes != 0);
9211da177e4SLinus Torvalds 	atomic_set(&req->wb_complete, requests);
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds 	ClearPageError(page);
9241da177e4SLinus Torvalds 	offset = 0;
925bcb71bbaSTrond Myklebust 	nbytes = count;
9261da177e4SLinus Torvalds 	do {
927dbae4c73STrond Myklebust 		int ret2;
928dbae4c73STrond Myklebust 
9291da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
9301da177e4SLinus Torvalds 		list_del_init(&data->pages);
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 		data->pagevec[0] = page;
9331da177e4SLinus Torvalds 
934bcb71bbaSTrond Myklebust 		if (nbytes < wsize)
935bcb71bbaSTrond Myklebust 			wsize = nbytes;
936dbae4c73STrond Myklebust 		ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
937788e7a89STrond Myklebust 				   wsize, offset, how);
938dbae4c73STrond Myklebust 		if (ret == 0)
939dbae4c73STrond Myklebust 			ret = ret2;
9401da177e4SLinus Torvalds 		offset += wsize;
9411da177e4SLinus Torvalds 		nbytes -= wsize;
9421da177e4SLinus Torvalds 	} while (nbytes != 0);
9431da177e4SLinus Torvalds 
944dbae4c73STrond Myklebust 	return ret;
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds out_bad:
9471da177e4SLinus Torvalds 	while (!list_empty(&list)) {
9481da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
9491da177e4SLinus Torvalds 		list_del(&data->pages);
9508aca67f0STrond Myklebust 		nfs_writedata_release(data);
9511da177e4SLinus Torvalds 	}
95261822ab5STrond Myklebust 	nfs_redirty_request(req);
9531da177e4SLinus Torvalds 	return -ENOMEM;
9541da177e4SLinus Torvalds }
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds /*
9571da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
9581da177e4SLinus Torvalds  * The page must have been locked by the caller.
9591da177e4SLinus Torvalds  *
9601da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
9611da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
9621da177e4SLinus Torvalds  * that has been written but not committed.
9631da177e4SLinus Torvalds  */
9648d5658c9STrond Myklebust static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
9651da177e4SLinus Torvalds {
9661da177e4SLinus Torvalds 	struct nfs_page		*req;
9671da177e4SLinus Torvalds 	struct page		**pages;
9681da177e4SLinus Torvalds 	struct nfs_write_data	*data;
9691da177e4SLinus Torvalds 
9708d5658c9STrond Myklebust 	data = nfs_writedata_alloc(npages);
9711da177e4SLinus Torvalds 	if (!data)
9721da177e4SLinus Torvalds 		goto out_bad;
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 	pages = data->pagevec;
9751da177e4SLinus Torvalds 	while (!list_empty(head)) {
9761da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
9771da177e4SLinus Torvalds 		nfs_list_remove_request(req);
9781da177e4SLinus Torvalds 		nfs_list_add_request(req, &data->pages);
9791da177e4SLinus Torvalds 		ClearPageError(req->wb_page);
9801da177e4SLinus Torvalds 		*pages++ = req->wb_page;
9811da177e4SLinus Torvalds 	}
9821da177e4SLinus Torvalds 	req = nfs_list_entry(data->pages.next);
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 	/* Set up the argument struct */
985dbae4c73STrond Myklebust 	return nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
9861da177e4SLinus Torvalds  out_bad:
9871da177e4SLinus Torvalds 	while (!list_empty(head)) {
98810afec90STrond Myklebust 		req = nfs_list_entry(head->next);
9891da177e4SLinus Torvalds 		nfs_list_remove_request(req);
99061822ab5STrond Myklebust 		nfs_redirty_request(req);
9911da177e4SLinus Torvalds 	}
9921da177e4SLinus Torvalds 	return -ENOMEM;
9931da177e4SLinus Torvalds }
9941da177e4SLinus Torvalds 
995c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
996c63c7b05STrond Myklebust 				  struct inode *inode, int ioflags)
9971da177e4SLinus Torvalds {
998bf4285e7SChuck Lever 	size_t wsize = NFS_SERVER(inode)->wsize;
9991da177e4SLinus Torvalds 
1000bcb71bbaSTrond Myklebust 	if (wsize < PAGE_CACHE_SIZE)
1001c63c7b05STrond Myklebust 		nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
1002bcb71bbaSTrond Myklebust 	else
1003c63c7b05STrond Myklebust 		nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
10041da177e4SLinus Torvalds }
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds /*
10071da177e4SLinus Torvalds  * Handle a write reply that flushed part of a page.
10081da177e4SLinus Torvalds  */
1009788e7a89STrond Myklebust static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
10101da177e4SLinus Torvalds {
1011788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
10121da177e4SLinus Torvalds 
101348186c7dSChuck Lever 	dprintk("NFS: %5u write(%s/%lld %d@%lld)",
101448186c7dSChuck Lever 		task->tk_pid,
101548186c7dSChuck Lever 		data->req->wb_context->path.dentry->d_inode->i_sb->s_id,
101648186c7dSChuck Lever 		(long long)
101748186c7dSChuck Lever 		  NFS_FILEID(data->req->wb_context->path.dentry->d_inode),
101848186c7dSChuck Lever 		data->req->wb_bytes, (long long)req_offset(data->req));
10191da177e4SLinus Torvalds 
1020c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
1021c9d8f89dSTrond Myklebust }
1022788e7a89STrond Myklebust 
1023c9d8f89dSTrond Myklebust static void nfs_writeback_release_partial(void *calldata)
1024c9d8f89dSTrond Myklebust {
1025c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1026c9d8f89dSTrond Myklebust 	struct nfs_page		*req = data->req;
1027c9d8f89dSTrond Myklebust 	struct page		*page = req->wb_page;
1028c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1029c9d8f89dSTrond Myklebust 
1030c9d8f89dSTrond Myklebust 	if (status < 0) {
1031a301b777STrond Myklebust 		nfs_set_pageerror(page);
1032c9d8f89dSTrond Myklebust 		nfs_context_set_write_error(req->wb_context, status);
1033c9d8f89dSTrond Myklebust 		dprintk(", error = %d\n", status);
10348e821cadSTrond Myklebust 		goto out;
10358e821cadSTrond Myklebust 	}
10368e821cadSTrond Myklebust 
10378e821cadSTrond Myklebust 	if (nfs_write_need_commit(data)) {
1038587142f8STrond Myklebust 		struct inode *inode = page->mapping->host;
10398e821cadSTrond Myklebust 
1040587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
10418e821cadSTrond Myklebust 		if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
10428e821cadSTrond Myklebust 			/* Do nothing we need to resend the writes */
10438e821cadSTrond Myklebust 		} else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
10441da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
10451da177e4SLinus Torvalds 			dprintk(" defer commit\n");
10461da177e4SLinus Torvalds 		} else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
10478e821cadSTrond Myklebust 			set_bit(PG_NEED_RESCHED, &req->wb_flags);
10488e821cadSTrond Myklebust 			clear_bit(PG_NEED_COMMIT, &req->wb_flags);
10491da177e4SLinus Torvalds 			dprintk(" server reboot detected\n");
10501da177e4SLinus Torvalds 		}
1051587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
10521da177e4SLinus Torvalds 	} else
10531da177e4SLinus Torvalds 		dprintk(" OK\n");
10541da177e4SLinus Torvalds 
10558e821cadSTrond Myklebust out:
10561da177e4SLinus Torvalds 	if (atomic_dec_and_test(&req->wb_complete))
10571da177e4SLinus Torvalds 		nfs_writepage_release(req);
1058c9d8f89dSTrond Myklebust 	nfs_writedata_release(calldata);
10591da177e4SLinus Torvalds }
10601da177e4SLinus Torvalds 
1061def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1)
1062def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata)
1063def6ed7eSAndy Adamson {
1064def6ed7eSAndy Adamson 	struct nfs_write_data *data = calldata;
1065def6ed7eSAndy Adamson 	struct nfs_client *clp = (NFS_SERVER(data->inode))->nfs_client;
1066def6ed7eSAndy Adamson 
1067def6ed7eSAndy Adamson 	if (nfs4_setup_sequence(clp, &data->args.seq_args,
1068def6ed7eSAndy Adamson 				&data->res.seq_res, 1, task))
1069def6ed7eSAndy Adamson 		return;
1070def6ed7eSAndy Adamson 	rpc_call_start(task);
1071def6ed7eSAndy Adamson }
1072def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1073def6ed7eSAndy Adamson 
1074788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops = {
1075def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1)
1076def6ed7eSAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
1077def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1078788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_partial,
1079c9d8f89dSTrond Myklebust 	.rpc_release = nfs_writeback_release_partial,
1080788e7a89STrond Myklebust };
1081788e7a89STrond Myklebust 
10821da177e4SLinus Torvalds /*
10831da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
10841da177e4SLinus Torvalds  *
10851da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
10861da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
10871da177e4SLinus Torvalds  *	  as the page has a write request pending.
10881da177e4SLinus Torvalds  */
1089788e7a89STrond Myklebust static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
10901da177e4SLinus Torvalds {
1091788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
10921da177e4SLinus Torvalds 
1093c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
1094c9d8f89dSTrond Myklebust }
1095c9d8f89dSTrond Myklebust 
1096c9d8f89dSTrond Myklebust static void nfs_writeback_release_full(void *calldata)
1097c9d8f89dSTrond Myklebust {
1098c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1099c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1100788e7a89STrond Myklebust 
11011da177e4SLinus Torvalds 	/* Update attributes as result of writeback. */
11021da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
1103c9d8f89dSTrond Myklebust 		struct nfs_page *req = nfs_list_entry(data->pages.next);
1104c9d8f89dSTrond Myklebust 		struct page *page = req->wb_page;
1105c9d8f89dSTrond Myklebust 
11061da177e4SLinus Torvalds 		nfs_list_remove_request(req);
11071da177e4SLinus Torvalds 
110848186c7dSChuck Lever 		dprintk("NFS: %5u write (%s/%lld %d@%lld)",
110948186c7dSChuck Lever 			data->task.tk_pid,
111088be9f99STrond Myklebust 			req->wb_context->path.dentry->d_inode->i_sb->s_id,
111188be9f99STrond Myklebust 			(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
11121da177e4SLinus Torvalds 			req->wb_bytes,
11131da177e4SLinus Torvalds 			(long long)req_offset(req));
11141da177e4SLinus Torvalds 
1115c9d8f89dSTrond Myklebust 		if (status < 0) {
1116a301b777STrond Myklebust 			nfs_set_pageerror(page);
1117c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
1118c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
11198e821cadSTrond Myklebust 			goto remove_request;
11201da177e4SLinus Torvalds 		}
11211da177e4SLinus Torvalds 
11228e821cadSTrond Myklebust 		if (nfs_write_need_commit(data)) {
11231da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
11241da177e4SLinus Torvalds 			nfs_mark_request_commit(req);
11258e821cadSTrond Myklebust 			nfs_end_page_writeback(page);
11261da177e4SLinus Torvalds 			dprintk(" marked for commit\n");
11278e821cadSTrond Myklebust 			goto next;
11288e821cadSTrond Myklebust 		}
11298e821cadSTrond Myklebust 		dprintk(" OK\n");
11308e821cadSTrond Myklebust remove_request:
11318e821cadSTrond Myklebust 		nfs_end_page_writeback(page);
11321da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
11331da177e4SLinus Torvalds 	next:
11349fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
11351da177e4SLinus Torvalds 	}
1136c9d8f89dSTrond Myklebust 	nfs_writedata_release(calldata);
11371da177e4SLinus Torvalds }
11381da177e4SLinus Torvalds 
1139788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops = {
1140def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1)
1141def6ed7eSAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
1142def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1143788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_full,
1144c9d8f89dSTrond Myklebust 	.rpc_release = nfs_writeback_release_full,
1145788e7a89STrond Myklebust };
1146788e7a89STrond Myklebust 
1147788e7a89STrond Myklebust 
11481da177e4SLinus Torvalds /*
11491da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
11501da177e4SLinus Torvalds  */
1151462d5b32SChuck Lever int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
11521da177e4SLinus Torvalds {
11531da177e4SLinus Torvalds 	struct nfs_writeargs	*argp = &data->args;
11541da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
1155eedc020eSAndy Adamson 	struct nfs_server	*server = NFS_SERVER(data->inode);
1156788e7a89STrond Myklebust 	int status;
11571da177e4SLinus Torvalds 
1158a3f565b1SChuck Lever 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
11591da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
11601da177e4SLinus Torvalds 
1161f551e44fSChuck Lever 	/*
1162f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1163f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1164f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1165f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1166f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1167f551e44fSChuck Lever 	 */
1168788e7a89STrond Myklebust 	status = NFS_PROTO(data->inode)->write_done(task, data);
1169788e7a89STrond Myklebust 	if (status != 0)
1170788e7a89STrond Myklebust 		return status;
117191d5b470SChuck Lever 	nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
117291d5b470SChuck Lever 
11731da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
11741da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
11751da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
11761da177e4SLinus Torvalds 		 * commit data to stable storage even though we
11771da177e4SLinus Torvalds 		 * requested it.
11781da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
11791da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
11801da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
11811da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
11821da177e4SLinus Torvalds 		 */
11831da177e4SLinus Torvalds 		static unsigned long    complain;
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
11861da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
11871da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1188eedc020eSAndy Adamson 				server->nfs_client->cl_hostname,
11891da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
11901da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
11911da177e4SLinus Torvalds 		}
11921da177e4SLinus Torvalds 	}
11931da177e4SLinus Torvalds #endif
11941da177e4SLinus Torvalds 	/* Is this a short write? */
11951da177e4SLinus Torvalds 	if (task->tk_status >= 0 && resp->count < argp->count) {
11961da177e4SLinus Torvalds 		static unsigned long    complain;
11971da177e4SLinus Torvalds 
119891d5b470SChuck Lever 		nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
119991d5b470SChuck Lever 
12001da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
12011da177e4SLinus Torvalds 		if (resp->count != 0) {
12021da177e4SLinus Torvalds 			/* Was this an NFSv2 write or an NFSv3 stable write? */
12031da177e4SLinus Torvalds 			if (resp->verf->committed != NFS_UNSTABLE) {
12041da177e4SLinus Torvalds 				/* Resend from where the server left off */
12051da177e4SLinus Torvalds 				argp->offset += resp->count;
12061da177e4SLinus Torvalds 				argp->pgbase += resp->count;
12071da177e4SLinus Torvalds 				argp->count -= resp->count;
12081da177e4SLinus Torvalds 			} else {
12091da177e4SLinus Torvalds 				/* Resend as a stable write in order to avoid
12101da177e4SLinus Torvalds 				 * headaches in the case of a server crash.
12111da177e4SLinus Torvalds 				 */
12121da177e4SLinus Torvalds 				argp->stable = NFS_FILE_SYNC;
12131da177e4SLinus Torvalds 			}
12140110ee15STrond Myklebust 			nfs_restart_rpc(task, server->nfs_client);
1215788e7a89STrond Myklebust 			return -EAGAIN;
12161da177e4SLinus Torvalds 		}
12171da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
12181da177e4SLinus Torvalds 			printk(KERN_WARNING
12191da177e4SLinus Torvalds 			       "NFS: Server wrote zero bytes, expected %u.\n",
12201da177e4SLinus Torvalds 					argp->count);
12211da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
12221da177e4SLinus Torvalds 		}
12231da177e4SLinus Torvalds 		/* Can't do anything about it except throw an error. */
12241da177e4SLinus Torvalds 		task->tk_status = -EIO;
12251da177e4SLinus Torvalds 	}
1226788e7a89STrond Myklebust 	return 0;
12271da177e4SLinus Torvalds }
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
12310aa05887SH Hartley Sweeten static void nfs_commitdata_release(void *data)
12321da177e4SLinus Torvalds {
1233383ba719STrond Myklebust 	struct nfs_write_data *wdata = data;
1234383ba719STrond Myklebust 
1235383ba719STrond Myklebust 	put_nfs_open_context(wdata->args.context);
12361da177e4SLinus Torvalds 	nfs_commit_free(wdata);
12371da177e4SLinus Torvalds }
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds /*
12401da177e4SLinus Torvalds  * Set up the argument/result storage required for the RPC call.
12411da177e4SLinus Torvalds  */
1242dbae4c73STrond Myklebust static int nfs_commit_rpcsetup(struct list_head *head,
1243788e7a89STrond Myklebust 		struct nfs_write_data *data,
1244788e7a89STrond Myklebust 		int how)
12451da177e4SLinus Torvalds {
124684115e1cSTrond Myklebust 	struct nfs_page *first = nfs_list_entry(head->next);
124784115e1cSTrond Myklebust 	struct inode *inode = first->wb_context->path.dentry->d_inode;
124884115e1cSTrond Myklebust 	int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
12493ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
125007737691STrond Myklebust 	struct rpc_task *task;
1251bdc7f021STrond Myklebust 	struct rpc_message msg = {
1252bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1253bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
1254bdc7f021STrond Myklebust 		.rpc_cred = first->wb_context->cred,
1255bdc7f021STrond Myklebust 	};
125684115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
125707737691STrond Myklebust 		.task = &data->task,
125884115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(inode),
1259bdc7f021STrond Myklebust 		.rpc_message = &msg,
126084115e1cSTrond Myklebust 		.callback_ops = &nfs_commit_ops,
126184115e1cSTrond Myklebust 		.callback_data = data,
1262101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
126384115e1cSTrond Myklebust 		.flags = flags,
12643ff7576dSTrond Myklebust 		.priority = priority,
126584115e1cSTrond Myklebust 	};
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
12681da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds 	list_splice_init(head, &data->pages);
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 	data->inode	  = inode;
1273bdc7f021STrond Myklebust 	data->cred	  = msg.rpc_cred;
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(data->inode);
12763da28eb1STrond Myklebust 	/* Note: we always request a commit of the entire inode */
12773da28eb1STrond Myklebust 	data->args.offset = 0;
12783da28eb1STrond Myklebust 	data->args.count  = 0;
1279383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(first->wb_context);
12803da28eb1STrond Myklebust 	data->res.count   = 0;
12811da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
12821da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
12830e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
12841da177e4SLinus Torvalds 
1285788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
1286bdc7f021STrond Myklebust 	NFS_PROTO(inode)->commit_setup(data, &msg);
12871da177e4SLinus Torvalds 
1288a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1289bdc7f021STrond Myklebust 
129007737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1291dbae4c73STrond Myklebust 	if (IS_ERR(task))
1292dbae4c73STrond Myklebust 		return PTR_ERR(task);
129307737691STrond Myklebust 	rpc_put_task(task);
1294dbae4c73STrond Myklebust 	return 0;
12951da177e4SLinus Torvalds }
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds /*
12981da177e4SLinus Torvalds  * Commit dirty pages
12991da177e4SLinus Torvalds  */
13001da177e4SLinus Torvalds static int
130140859d7eSChuck Lever nfs_commit_list(struct inode *inode, struct list_head *head, int how)
13021da177e4SLinus Torvalds {
13031da177e4SLinus Torvalds 	struct nfs_write_data	*data;
13041da177e4SLinus Torvalds 	struct nfs_page         *req;
13051da177e4SLinus Torvalds 
1306c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 	if (!data)
13091da177e4SLinus Torvalds 		goto out_bad;
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 	/* Set up the argument struct */
1312dbae4c73STrond Myklebust 	return nfs_commit_rpcsetup(head, data, how);
13131da177e4SLinus Torvalds  out_bad:
13141da177e4SLinus Torvalds 	while (!list_empty(head)) {
13151da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
13161da177e4SLinus Torvalds 		nfs_list_remove_request(req);
13171da177e4SLinus Torvalds 		nfs_mark_request_commit(req);
131883715ad5STrond Myklebust 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1319c9e51e41SPeter Zijlstra 		dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1320c9e51e41SPeter Zijlstra 				BDI_RECLAIMABLE);
13219fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
13221da177e4SLinus Torvalds 	}
13231da177e4SLinus Torvalds 	return -ENOMEM;
13241da177e4SLinus Torvalds }
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds /*
13271da177e4SLinus Torvalds  * COMMIT call returned
13281da177e4SLinus Torvalds  */
1329788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
13301da177e4SLinus Torvalds {
1331963d8fe5STrond Myklebust 	struct nfs_write_data	*data = calldata;
13321da177e4SLinus Torvalds 
1333a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
13341da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
13351da177e4SLinus Torvalds 
1336788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1337788e7a89STrond Myklebust 	if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1338788e7a89STrond Myklebust 		return;
1339c9d8f89dSTrond Myklebust }
1340c9d8f89dSTrond Myklebust 
1341c9d8f89dSTrond Myklebust static void nfs_commit_release(void *calldata)
1342c9d8f89dSTrond Myklebust {
1343c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1344c9d8f89dSTrond Myklebust 	struct nfs_page		*req;
1345c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1346788e7a89STrond Myklebust 
13471da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
13481da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
13491da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1350e468bae9STrond Myklebust 		nfs_clear_request_commit(req);
13511da177e4SLinus Torvalds 
135248186c7dSChuck Lever 		dprintk("NFS:       commit (%s/%lld %d@%lld)",
135388be9f99STrond Myklebust 			req->wb_context->path.dentry->d_inode->i_sb->s_id,
135488be9f99STrond Myklebust 			(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
13551da177e4SLinus Torvalds 			req->wb_bytes,
13561da177e4SLinus Torvalds 			(long long)req_offset(req));
1357c9d8f89dSTrond Myklebust 		if (status < 0) {
1358c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
13591da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1360c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
13611da177e4SLinus Torvalds 			goto next;
13621da177e4SLinus Torvalds 		}
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
13651da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
13661da177e4SLinus Torvalds 		if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
13671da177e4SLinus Torvalds 			/* We have a match */
13681da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
13691da177e4SLinus Torvalds 			dprintk(" OK\n");
13701da177e4SLinus Torvalds 			goto next;
13711da177e4SLinus Torvalds 		}
13721da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
13731da177e4SLinus Torvalds 		dprintk(" mismatch\n");
13746d884e8fSFred 		nfs_mark_request_dirty(req);
13751da177e4SLinus Torvalds 	next:
13769fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
13771da177e4SLinus Torvalds 	}
1378c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
13791da177e4SLinus Torvalds }
1380788e7a89STrond Myklebust 
1381788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
138221d9a851SAndy Adamson #if defined(CONFIG_NFS_V4_1)
138321d9a851SAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
138421d9a851SAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1385788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1386788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1387788e7a89STrond Myklebust };
13881da177e4SLinus Torvalds 
13898fc795f7STrond Myklebust static int nfs_commit_inode(struct inode *inode, int how)
13901da177e4SLinus Torvalds {
13911da177e4SLinus Torvalds 	LIST_HEAD(head);
13927d46a49fSTrond Myklebust 	int res;
13931da177e4SLinus Torvalds 
1394587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
13953da28eb1STrond Myklebust 	res = nfs_scan_commit(inode, &head, 0, 0);
1396587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
13971da177e4SLinus Torvalds 	if (res) {
13987d46a49fSTrond Myklebust 		int error = nfs_commit_list(inode, &head, how);
13991da177e4SLinus Torvalds 		if (error < 0)
14001da177e4SLinus Torvalds 			return error;
14013da28eb1STrond Myklebust 	}
14021da177e4SLinus Torvalds 	return res;
14031da177e4SLinus Torvalds }
14048fc795f7STrond Myklebust 
14058fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
14068fc795f7STrond Myklebust {
1407420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1408420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1409420e3646STrond Myklebust 	int ret = 0;
14108fc795f7STrond Myklebust 
1411420e3646STrond Myklebust 	/* Don't commit yet if this is a non-blocking flush and there are
1412420e3646STrond Myklebust 	 * lots of outstanding writes for this mapping.
1413420e3646STrond Myklebust 	 */
1414420e3646STrond Myklebust 	if (wbc->sync_mode == WB_SYNC_NONE &&
1415420e3646STrond Myklebust 	    nfsi->ncommit <= (nfsi->npages >> 1))
1416420e3646STrond Myklebust 		goto out_mark_dirty;
1417420e3646STrond Myklebust 
14185bad5abeSTrond Myklebust 	if (wbc->nonblocking || wbc->for_background)
1419420e3646STrond Myklebust 		flags = 0;
1420420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1421420e3646STrond Myklebust 	if (ret >= 0) {
1422420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1423420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1424420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1425420e3646STrond Myklebust 			else
1426420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1427420e3646STrond Myklebust 		}
14288fc795f7STrond Myklebust 		return 0;
1429420e3646STrond Myklebust 	}
1430420e3646STrond Myklebust out_mark_dirty:
14318fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
14328fc795f7STrond Myklebust 	return ret;
14338fc795f7STrond Myklebust }
1434c63c7b05STrond Myklebust #else
1435c63c7b05STrond Myklebust static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1436c63c7b05STrond Myklebust {
1437c63c7b05STrond Myklebust 	return 0;
1438c63c7b05STrond Myklebust }
14398fc795f7STrond Myklebust 
14408fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
14418fc795f7STrond Myklebust {
14428fc795f7STrond Myklebust 	return 0;
14438fc795f7STrond Myklebust }
14441da177e4SLinus Torvalds #endif
14451da177e4SLinus Torvalds 
14468fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
14478fc795f7STrond Myklebust {
14488fc795f7STrond Myklebust 	return nfs_commit_unstable_pages(inode, wbc);
14498fc795f7STrond Myklebust }
14508fc795f7STrond Myklebust 
14511c75950bSTrond Myklebust long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
14521da177e4SLinus Torvalds {
14531c75950bSTrond Myklebust 	struct inode *inode = mapping->host;
1454ca52fec1STrond Myklebust 	pgoff_t idx_start, idx_end;
14551c75950bSTrond Myklebust 	unsigned int npages = 0;
1456c42de9ddSTrond Myklebust 	LIST_HEAD(head);
145770b9ecbdSTrond Myklebust 	int nocommit = how & FLUSH_NOCOMMIT;
14583f442547STrond Myklebust 	long pages, ret;
14591da177e4SLinus Torvalds 
14601c75950bSTrond Myklebust 	/* FIXME */
14611c75950bSTrond Myklebust 	if (wbc->range_cyclic)
14621c75950bSTrond Myklebust 		idx_start = 0;
14631c75950bSTrond Myklebust 	else {
14641c75950bSTrond Myklebust 		idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
14651c75950bSTrond Myklebust 		idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
14661c75950bSTrond Myklebust 		if (idx_end > idx_start) {
1467ca52fec1STrond Myklebust 			pgoff_t l_npages = 1 + idx_end - idx_start;
14681c75950bSTrond Myklebust 			npages = l_npages;
14691c75950bSTrond Myklebust 			if (sizeof(npages) != sizeof(l_npages) &&
1470ca52fec1STrond Myklebust 					(pgoff_t)npages != l_npages)
14711c75950bSTrond Myklebust 				npages = 0;
14721c75950bSTrond Myklebust 		}
14731c75950bSTrond Myklebust 	}
1474c42de9ddSTrond Myklebust 	how &= ~FLUSH_NOCOMMIT;
1475587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
14761da177e4SLinus Torvalds 	do {
1477c42de9ddSTrond Myklebust 		ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1478c42de9ddSTrond Myklebust 		if (ret != 0)
1479c42de9ddSTrond Myklebust 			continue;
1480c42de9ddSTrond Myklebust 		if (nocommit)
1481c42de9ddSTrond Myklebust 			break;
1482d2ccddf0STrond Myklebust 		pages = nfs_scan_commit(inode, &head, idx_start, npages);
1483724c439cSTrond Myklebust 		if (pages == 0)
1484c42de9ddSTrond Myklebust 			break;
1485d2ccddf0STrond Myklebust 		pages += nfs_scan_commit(inode, &head, 0, 0);
1486587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
1487c42de9ddSTrond Myklebust 		ret = nfs_commit_list(inode, &head, how);
1488587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
1489587142f8STrond Myklebust 
1490c42de9ddSTrond Myklebust 	} while (ret >= 0);
1491587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
1492c42de9ddSTrond Myklebust 	return ret;
14931da177e4SLinus Torvalds }
14941da177e4SLinus Torvalds 
149534901f70STrond Myklebust static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
14961c75950bSTrond Myklebust {
14971c75950bSTrond Myklebust 	int ret;
14981c75950bSTrond Myklebust 
149934901f70STrond Myklebust 	ret = nfs_writepages(mapping, wbc);
150061822ab5STrond Myklebust 	if (ret < 0)
150161822ab5STrond Myklebust 		goto out;
150234901f70STrond Myklebust 	ret = nfs_sync_mapping_wait(mapping, wbc, how);
1503ed90ef51STrond Myklebust 	if (ret < 0)
1504ed90ef51STrond Myklebust 		goto out;
15051c75950bSTrond Myklebust 	return 0;
150661822ab5STrond Myklebust out:
1507e507d9ebSTrond Myklebust 	__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
15081c75950bSTrond Myklebust 	return ret;
15091c75950bSTrond Myklebust }
15101c75950bSTrond Myklebust 
151134901f70STrond Myklebust /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
151234901f70STrond Myklebust static int nfs_write_mapping(struct address_space *mapping, int how)
151334901f70STrond Myklebust {
151434901f70STrond Myklebust 	struct writeback_control wbc = {
151534901f70STrond Myklebust 		.bdi = mapping->backing_dev_info,
151672cb77f4STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
151734901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
1518d7fb1207STrond Myklebust 		.range_start = 0,
1519d7fb1207STrond Myklebust 		.range_end = LLONG_MAX,
152034901f70STrond Myklebust 	};
152134901f70STrond Myklebust 
152234901f70STrond Myklebust 	return __nfs_write_mapping(mapping, &wbc, how);
152334901f70STrond Myklebust }
152434901f70STrond Myklebust 
1525ed90ef51STrond Myklebust /*
1526ed90ef51STrond Myklebust  * flush the inode to disk.
1527ed90ef51STrond Myklebust  */
1528ed90ef51STrond Myklebust int nfs_wb_all(struct inode *inode)
15291c75950bSTrond Myklebust {
1530ed90ef51STrond Myklebust 	return nfs_write_mapping(inode->i_mapping, 0);
1531ed90ef51STrond Myklebust }
15321c75950bSTrond Myklebust 
1533ed90ef51STrond Myklebust int nfs_wb_nocommit(struct inode *inode)
1534ed90ef51STrond Myklebust {
1535ed90ef51STrond Myklebust 	return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
15361c75950bSTrond Myklebust }
15371c75950bSTrond Myklebust 
15381b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
15391b3b4a1aSTrond Myklebust {
15401b3b4a1aSTrond Myklebust 	struct nfs_page *req;
15411b3b4a1aSTrond Myklebust 	int ret = 0;
15421b3b4a1aSTrond Myklebust 
15431b3b4a1aSTrond Myklebust 	BUG_ON(!PageLocked(page));
15441b3b4a1aSTrond Myklebust 	for (;;) {
15451b3b4a1aSTrond Myklebust 		req = nfs_page_find_request(page);
15461b3b4a1aSTrond Myklebust 		if (req == NULL)
15471b3b4a1aSTrond Myklebust 			break;
15481b3b4a1aSTrond Myklebust 		if (nfs_lock_request_dontget(req)) {
15491b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
15501b3b4a1aSTrond Myklebust 			/*
15511b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
15521b3b4a1aSTrond Myklebust 			 * page as being dirty
15531b3b4a1aSTrond Myklebust 			 */
15541b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
15551b3b4a1aSTrond Myklebust 			nfs_unlock_request(req);
15561b3b4a1aSTrond Myklebust 			break;
15571b3b4a1aSTrond Myklebust 		}
15581b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
1559c9edda71STrond Myklebust 		nfs_release_request(req);
15601b3b4a1aSTrond Myklebust 		if (ret < 0)
1561*c988950eSTrond Myklebust 			break;
15621b3b4a1aSTrond Myklebust 	}
15631b3b4a1aSTrond Myklebust 	return ret;
15641b3b4a1aSTrond Myklebust }
15651b3b4a1aSTrond Myklebust 
15665334eb13SAdrian Bunk static int nfs_wb_page_priority(struct inode *inode, struct page *page,
15675334eb13SAdrian Bunk 				int how)
15681c75950bSTrond Myklebust {
15691c75950bSTrond Myklebust 	loff_t range_start = page_offset(page);
15701c75950bSTrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
15714d770ccfSTrond Myklebust 	struct writeback_control wbc = {
15724d770ccfSTrond Myklebust 		.bdi = page->mapping->backing_dev_info,
15734d770ccfSTrond Myklebust 		.sync_mode = WB_SYNC_ALL,
15744d770ccfSTrond Myklebust 		.nr_to_write = LONG_MAX,
15754d770ccfSTrond Myklebust 		.range_start = range_start,
15764d770ccfSTrond Myklebust 		.range_end = range_end,
15774d770ccfSTrond Myklebust 	};
15784d770ccfSTrond Myklebust 	int ret;
15791c75950bSTrond Myklebust 
158073e3302fSTrond Myklebust 	do {
1581c63c7b05STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
15824d770ccfSTrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
15834d770ccfSTrond Myklebust 			if (ret < 0)
158473e3302fSTrond Myklebust 				goto out_error;
158573e3302fSTrond Myklebust 		} else if (!PagePrivate(page))
158673e3302fSTrond Myklebust 			break;
15874d770ccfSTrond Myklebust 		ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
158873e3302fSTrond Myklebust 		if (ret < 0)
158973e3302fSTrond Myklebust 			goto out_error;
159073e3302fSTrond Myklebust 	} while (PagePrivate(page));
15914d770ccfSTrond Myklebust 	return 0;
159273e3302fSTrond Myklebust out_error:
1593e507d9ebSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_PAGES);
15944d770ccfSTrond Myklebust 	return ret;
15951c75950bSTrond Myklebust }
15961c75950bSTrond Myklebust 
15971c75950bSTrond Myklebust /*
15981c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
15991c75950bSTrond Myklebust  */
16001c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page* page)
16011c75950bSTrond Myklebust {
16024d770ccfSTrond Myklebust 	return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
16031c75950bSTrond Myklebust }
16041c75950bSTrond Myklebust 
1605074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
1606074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1607074cc1deSTrond Myklebust 		struct page *page)
1608074cc1deSTrond Myklebust {
1609074cc1deSTrond Myklebust 	struct nfs_page *req;
1610074cc1deSTrond Myklebust 	int ret;
1611074cc1deSTrond Myklebust 
1612074cc1deSTrond Myklebust 	nfs_fscache_release_page(page, GFP_KERNEL);
1613074cc1deSTrond Myklebust 
1614074cc1deSTrond Myklebust 	req = nfs_find_and_lock_request(page);
1615074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
1616074cc1deSTrond Myklebust 	if (IS_ERR(req))
1617074cc1deSTrond Myklebust 		goto out;
1618074cc1deSTrond Myklebust 
1619074cc1deSTrond Myklebust 	ret = migrate_page(mapping, newpage, page);
1620074cc1deSTrond Myklebust 	if (!req)
1621074cc1deSTrond Myklebust 		goto out;
1622074cc1deSTrond Myklebust 	if (ret)
1623074cc1deSTrond Myklebust 		goto out_unlock;
1624074cc1deSTrond Myklebust 	page_cache_get(newpage);
1625190f38e5STrond Myklebust 	spin_lock(&mapping->host->i_lock);
1626074cc1deSTrond Myklebust 	req->wb_page = newpage;
1627074cc1deSTrond Myklebust 	SetPagePrivate(newpage);
1628190f38e5STrond Myklebust 	set_page_private(newpage, (unsigned long)req);
1629074cc1deSTrond Myklebust 	ClearPagePrivate(page);
1630074cc1deSTrond Myklebust 	set_page_private(page, 0);
1631190f38e5STrond Myklebust 	spin_unlock(&mapping->host->i_lock);
1632074cc1deSTrond Myklebust 	page_cache_release(page);
1633074cc1deSTrond Myklebust out_unlock:
1634074cc1deSTrond Myklebust 	nfs_clear_page_tag_locked(req);
1635074cc1deSTrond Myklebust out:
1636074cc1deSTrond Myklebust 	return ret;
1637074cc1deSTrond Myklebust }
1638074cc1deSTrond Myklebust #endif
1639074cc1deSTrond Myklebust 
1640f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
16411da177e4SLinus Torvalds {
16421da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
16431da177e4SLinus Torvalds 					     sizeof(struct nfs_write_data),
16441da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
164520c2df83SPaul Mundt 					     NULL);
16461da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
16471da177e4SLinus Torvalds 		return -ENOMEM;
16481da177e4SLinus Torvalds 
164993d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
16501da177e4SLinus Torvalds 						     nfs_wdata_cachep);
16511da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
16521da177e4SLinus Torvalds 		return -ENOMEM;
16531da177e4SLinus Torvalds 
165493d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
16551da177e4SLinus Torvalds 						      nfs_wdata_cachep);
16561da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
16571da177e4SLinus Torvalds 		return -ENOMEM;
16581da177e4SLinus Torvalds 
165989a09141SPeter Zijlstra 	/*
166089a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
166189a09141SPeter Zijlstra 	 *
166289a09141SPeter Zijlstra 	 *  64MB:    8192k
166389a09141SPeter Zijlstra 	 * 128MB:   11585k
166489a09141SPeter Zijlstra 	 * 256MB:   16384k
166589a09141SPeter Zijlstra 	 * 512MB:   23170k
166689a09141SPeter Zijlstra 	 *   1GB:   32768k
166789a09141SPeter Zijlstra 	 *   2GB:   46340k
166889a09141SPeter Zijlstra 	 *   4GB:   65536k
166989a09141SPeter Zijlstra 	 *   8GB:   92681k
167089a09141SPeter Zijlstra 	 *  16GB:  131072k
167189a09141SPeter Zijlstra 	 *
167289a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
167389a09141SPeter Zijlstra 	 * Limit the default to 256M
167489a09141SPeter Zijlstra 	 */
167589a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
167689a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
167789a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
167889a09141SPeter Zijlstra 
16791da177e4SLinus Torvalds 	return 0;
16801da177e4SLinus Torvalds }
16811da177e4SLinus Torvalds 
1682266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
16831da177e4SLinus Torvalds {
16841da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
16851da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
16861a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
16871da177e4SLinus Torvalds }
16881da177e4SLinus Torvalds 
1689