xref: /linux/fs/nfs/write.c (revision 465d52437d5ce8d4eb9da0d3e3818b51cff163a1)
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"
3194ad1c80SFred Isaman #include "pnfs.h"
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #define MIN_POOL_WRITE		(32)
361da177e4SLinus Torvalds #define MIN_POOL_COMMIT		(4)
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds /*
391da177e4SLinus Torvalds  * Local function declarations
401da177e4SLinus Torvalds  */
41c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc,
42c63c7b05STrond Myklebust 				  struct inode *inode, int ioflags);
43f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req);
44788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops;
45788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops;
46788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops;
471da177e4SLinus Torvalds 
48e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep;
493feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
501da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
511da177e4SLinus Torvalds 
52c9d8f89dSTrond Myklebust struct nfs_write_data *nfs_commitdata_alloc(void)
531da177e4SLinus Torvalds {
54e6b4f8daSChristoph Lameter 	struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
5540859d7eSChuck Lever 
561da177e4SLinus Torvalds 	if (p) {
571da177e4SLinus Torvalds 		memset(p, 0, sizeof(*p));
581da177e4SLinus Torvalds 		INIT_LIST_HEAD(&p->pages);
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;
780d0b5cb3SChuck Lever 		if (pagecount <= ARRAY_SIZE(p->page_array))
790d0b5cb3SChuck Lever 			p->pagevec = p->page_array;
803feb2d49STrond Myklebust 		else {
810d0b5cb3SChuck Lever 			p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
820d0b5cb3SChuck Lever 			if (!p->pagevec) {
833feb2d49STrond Myklebust 				mempool_free(p, nfs_wdata_mempool);
843feb2d49STrond Myklebust 				p = NULL;
853feb2d49STrond Myklebust 			}
863feb2d49STrond Myklebust 		}
873feb2d49STrond Myklebust 	}
883feb2d49STrond Myklebust 	return p;
893feb2d49STrond Myklebust }
903feb2d49STrond Myklebust 
911ae88b2eSTrond Myklebust void nfs_writedata_free(struct nfs_write_data *p)
923feb2d49STrond Myklebust {
933feb2d49STrond Myklebust 	if (p && (p->pagevec != &p->page_array[0]))
943feb2d49STrond Myklebust 		kfree(p->pagevec);
953feb2d49STrond Myklebust 	mempool_free(p, nfs_wdata_mempool);
963feb2d49STrond Myklebust }
973feb2d49STrond Myklebust 
981ae88b2eSTrond Myklebust static void nfs_writedata_release(struct nfs_write_data *wdata)
991da177e4SLinus Torvalds {
1005053aa56SFred Isaman 	put_lseg(wdata->lseg);
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)
182b31268acSTrond Myklebust 		return FLUSH_LOWPRI | FLUSH_COND_STABLE;
183b31268acSTrond Myklebust 	return FLUSH_COND_STABLE;
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 
204a6305ddbSTrond Myklebust 		page_cache_get(page);
205277866a0SPeter Zijlstra 		if (atomic_long_inc_return(&nfss->writeback) >
2068aa7e847SJens Axboe 				NFS_CONGESTION_ON_THRESH) {
2078aa7e847SJens Axboe 			set_bdi_congested(&nfss->backing_dev_info,
2088aa7e847SJens Axboe 						BLK_RW_ASYNC);
2098aa7e847SJens Axboe 		}
21089a09141SPeter Zijlstra 	}
2115a6d41b3STrond Myklebust 	return ret;
21289a09141SPeter Zijlstra }
21389a09141SPeter Zijlstra 
21489a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page)
21589a09141SPeter Zijlstra {
21689a09141SPeter Zijlstra 	struct inode *inode = page->mapping->host;
21789a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
21889a09141SPeter Zijlstra 
21989a09141SPeter Zijlstra 	end_page_writeback(page);
220a6305ddbSTrond Myklebust 	page_cache_release(page);
221c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
2228aa7e847SJens Axboe 		clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
22389a09141SPeter Zijlstra }
22489a09141SPeter Zijlstra 
225cfb506e1STrond Myklebust static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock)
226e261f51fSTrond Myklebust {
227587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
228e261f51fSTrond Myklebust 	struct nfs_page *req;
229e261f51fSTrond Myklebust 	int ret;
230e261f51fSTrond Myklebust 
231587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
232e261f51fSTrond Myklebust 	for (;;) {
233e261f51fSTrond Myklebust 		req = nfs_page_find_request_locked(page);
234074cc1deSTrond Myklebust 		if (req == NULL)
235074cc1deSTrond Myklebust 			break;
236acee478aSTrond Myklebust 		if (nfs_set_page_tag_locked(req))
237e261f51fSTrond Myklebust 			break;
238e261f51fSTrond Myklebust 		/* Note: If we hold the page lock, as is the case in nfs_writepage,
239acee478aSTrond Myklebust 		 *	 then the call to nfs_set_page_tag_locked() will always
240e261f51fSTrond Myklebust 		 *	 succeed provided that someone hasn't already marked the
241e261f51fSTrond Myklebust 		 *	 request as dirty (in which case we don't care).
242e261f51fSTrond Myklebust 		 */
243587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
244cfb506e1STrond Myklebust 		if (!nonblock)
245e261f51fSTrond Myklebust 			ret = nfs_wait_on_request(req);
246cfb506e1STrond Myklebust 		else
247cfb506e1STrond Myklebust 			ret = -EAGAIN;
248e261f51fSTrond Myklebust 		nfs_release_request(req);
249e261f51fSTrond Myklebust 		if (ret != 0)
250074cc1deSTrond Myklebust 			return ERR_PTR(ret);
251587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
252e261f51fSTrond Myklebust 	}
253587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
254074cc1deSTrond Myklebust 	return req;
255612c9384STrond Myklebust }
256074cc1deSTrond Myklebust 
257074cc1deSTrond Myklebust /*
258074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
259074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
260074cc1deSTrond Myklebust  */
261074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
262cfb506e1STrond Myklebust 				struct page *page, bool nonblock)
263074cc1deSTrond Myklebust {
264074cc1deSTrond Myklebust 	struct nfs_page *req;
265074cc1deSTrond Myklebust 	int ret = 0;
266074cc1deSTrond Myklebust 
267cfb506e1STrond Myklebust 	req = nfs_find_and_lock_request(page, nonblock);
268074cc1deSTrond Myklebust 	if (!req)
269074cc1deSTrond Myklebust 		goto out;
270074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
271074cc1deSTrond Myklebust 	if (IS_ERR(req))
272074cc1deSTrond Myklebust 		goto out;
273074cc1deSTrond Myklebust 
274074cc1deSTrond Myklebust 	ret = nfs_set_page_writeback(page);
275074cc1deSTrond Myklebust 	BUG_ON(ret != 0);
276074cc1deSTrond Myklebust 	BUG_ON(test_bit(PG_CLEAN, &req->wb_flags));
277074cc1deSTrond Myklebust 
278f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
279f8512ad0SFred Isaman 		nfs_redirty_request(req);
280074cc1deSTrond Myklebust 		ret = pgio->pg_error;
281f8512ad0SFred Isaman 	}
282074cc1deSTrond Myklebust out:
283074cc1deSTrond Myklebust 	return ret;
284e261f51fSTrond Myklebust }
285e261f51fSTrond Myklebust 
286f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
287f758c885STrond Myklebust {
288f758c885STrond Myklebust 	struct inode *inode = page->mapping->host;
289cfb506e1STrond Myklebust 	int ret;
290f758c885STrond Myklebust 
291f758c885STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
292f758c885STrond Myklebust 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
293f758c885STrond Myklebust 
294f758c885STrond Myklebust 	nfs_pageio_cond_complete(pgio, page->index);
2951b430beeSWu Fengguang 	ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
296cfb506e1STrond Myklebust 	if (ret == -EAGAIN) {
297cfb506e1STrond Myklebust 		redirty_page_for_writepage(wbc, page);
298cfb506e1STrond Myklebust 		ret = 0;
299cfb506e1STrond Myklebust 	}
300cfb506e1STrond Myklebust 	return ret;
301f758c885STrond Myklebust }
302f758c885STrond Myklebust 
303e261f51fSTrond Myklebust /*
3041da177e4SLinus Torvalds  * Write an mmapped page to the server.
3051da177e4SLinus Torvalds  */
3064d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
3071da177e4SLinus Torvalds {
308f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
309e261f51fSTrond Myklebust 	int err;
3101da177e4SLinus Torvalds 
311f758c885STrond Myklebust 	nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
312f758c885STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
313f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
314f758c885STrond Myklebust 	if (err < 0)
3154d770ccfSTrond Myklebust 		return err;
316f758c885STrond Myklebust 	if (pgio.pg_error < 0)
317f758c885STrond Myklebust 		return pgio.pg_error;
318f758c885STrond Myklebust 	return 0;
3194d770ccfSTrond Myklebust }
3204d770ccfSTrond Myklebust 
3214d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
3224d770ccfSTrond Myklebust {
323f758c885STrond Myklebust 	int ret;
3244d770ccfSTrond Myklebust 
325f758c885STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
3261da177e4SLinus Torvalds 	unlock_page(page);
327f758c885STrond Myklebust 	return ret;
328f758c885STrond Myklebust }
329f758c885STrond Myklebust 
330f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
331f758c885STrond Myklebust {
332f758c885STrond Myklebust 	int ret;
333f758c885STrond Myklebust 
334f758c885STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
335f758c885STrond Myklebust 	unlock_page(page);
336f758c885STrond Myklebust 	return ret;
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3401da177e4SLinus Torvalds {
3411da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
34272cb77f4STrond Myklebust 	unsigned long *bitlock = &NFS_I(inode)->flags;
343c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
3441da177e4SLinus Torvalds 	int err;
3451da177e4SLinus Torvalds 
34672cb77f4STrond Myklebust 	/* Stop dirtying of new pages while we sync */
34772cb77f4STrond Myklebust 	err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
34872cb77f4STrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
34972cb77f4STrond Myklebust 	if (err)
35072cb77f4STrond Myklebust 		goto out_err;
35172cb77f4STrond Myklebust 
35291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
35391d5b470SChuck Lever 
354c63c7b05STrond Myklebust 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
355f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
356c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
35772cb77f4STrond Myklebust 
35872cb77f4STrond Myklebust 	clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
35972cb77f4STrond Myklebust 	smp_mb__after_clear_bit();
36072cb77f4STrond Myklebust 	wake_up_bit(bitlock, NFS_INO_FLUSHING);
36172cb77f4STrond Myklebust 
362f758c885STrond Myklebust 	if (err < 0)
36372cb77f4STrond Myklebust 		goto out_err;
36472cb77f4STrond Myklebust 	err = pgio.pg_error;
36572cb77f4STrond Myklebust 	if (err < 0)
36672cb77f4STrond Myklebust 		goto out_err;
367c63c7b05STrond Myklebust 	return 0;
36872cb77f4STrond Myklebust out_err:
36972cb77f4STrond Myklebust 	return err;
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds /*
3731da177e4SLinus Torvalds  * Insert a write request into an inode
3741da177e4SLinus Torvalds  */
375e7d39069STrond Myklebust static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
3761da177e4SLinus Torvalds {
3771da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
3781da177e4SLinus Torvalds 	int error;
3791da177e4SLinus Torvalds 
380e7d39069STrond Myklebust 	error = radix_tree_preload(GFP_NOFS);
381e7d39069STrond Myklebust 	if (error != 0)
382e7d39069STrond Myklebust 		goto out;
383e7d39069STrond Myklebust 
384e7d39069STrond Myklebust 	/* Lock the request! */
385e7d39069STrond Myklebust 	nfs_lock_request_dontget(req);
386e7d39069STrond Myklebust 
387e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
3881da177e4SLinus Torvalds 	error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
38927852596SNick Piggin 	BUG_ON(error);
3901da177e4SLinus Torvalds 	if (!nfsi->npages) {
3911da177e4SLinus Torvalds 		igrab(inode);
3921da177e4SLinus Torvalds 		if (nfs_have_delegation(inode, FMODE_WRITE))
3931da177e4SLinus Torvalds 			nfsi->change_attr++;
3941da177e4SLinus Torvalds 	}
3952df485a7STrond Myklebust 	set_bit(PG_MAPPED, &req->wb_flags);
396deb7d638STrond Myklebust 	SetPagePrivate(req->wb_page);
397277459d2STrond Myklebust 	set_page_private(req->wb_page, (unsigned long)req);
3981da177e4SLinus Torvalds 	nfsi->npages++;
399c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
40027852596SNick Piggin 	radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
40127852596SNick Piggin 				NFS_PAGE_TAG_LOCKED);
402e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
403e7d39069STrond Myklebust 	radix_tree_preload_end();
404e7d39069STrond Myklebust out:
405e7d39069STrond Myklebust 	return error;
4061da177e4SLinus Torvalds }
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds /*
40989a09141SPeter Zijlstra  * Remove a write request from an inode
4101da177e4SLinus Torvalds  */
4111da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
4121da177e4SLinus Torvalds {
41388be9f99STrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
4141da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	BUG_ON (!NFS_WBACK_BUSY(req));
4171da177e4SLinus Torvalds 
418587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
419277459d2STrond Myklebust 	set_page_private(req->wb_page, 0);
420deb7d638STrond Myklebust 	ClearPagePrivate(req->wb_page);
4212df485a7STrond Myklebust 	clear_bit(PG_MAPPED, &req->wb_flags);
4221da177e4SLinus Torvalds 	radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
4231da177e4SLinus Torvalds 	nfsi->npages--;
4241da177e4SLinus Torvalds 	if (!nfsi->npages) {
425587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
4261da177e4SLinus Torvalds 		iput(inode);
4271da177e4SLinus Torvalds 	} else
428587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
4291da177e4SLinus Torvalds 	nfs_release_request(req);
4301da177e4SLinus Torvalds }
4311da177e4SLinus Torvalds 
43261822ab5STrond Myklebust static void
4336d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
43461822ab5STrond Myklebust {
43561822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
436b80c3cb6STrond Myklebust 	__mark_inode_dirty(req->wb_page->mapping->host, I_DIRTY_DATASYNC);
43761822ab5STrond Myklebust }
43861822ab5STrond Myklebust 
4391da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
4401da177e4SLinus Torvalds /*
4411da177e4SLinus Torvalds  * Add a request to the inode's commit list.
4421da177e4SLinus Torvalds  */
4431da177e4SLinus Torvalds static void
4441da177e4SLinus Torvalds nfs_mark_request_commit(struct nfs_page *req)
4451da177e4SLinus Torvalds {
44688be9f99STrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
4471da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4481da177e4SLinus Torvalds 
449587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
450e468bae9STrond Myklebust 	set_bit(PG_CLEAN, &(req)->wb_flags);
4515c369683STrond Myklebust 	radix_tree_tag_set(&nfsi->nfs_page_tree,
4525c369683STrond Myklebust 			req->wb_index,
4535c369683STrond Myklebust 			NFS_PAGE_TAG_COMMIT);
454ff778d02STrond Myklebust 	nfsi->ncommit++;
455587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
456fd39fc85SChristoph Lameter 	inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
457c9e51e41SPeter Zijlstra 	inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
458a1803044STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
4591da177e4SLinus Torvalds }
4608e821cadSTrond Myklebust 
461e468bae9STrond Myklebust static int
462e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
463e468bae9STrond Myklebust {
464e468bae9STrond Myklebust 	struct page *page = req->wb_page;
465e468bae9STrond Myklebust 
466e468bae9STrond Myklebust 	if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) {
467e468bae9STrond Myklebust 		dec_zone_page_state(page, NR_UNSTABLE_NFS);
468e468bae9STrond Myklebust 		dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
469e468bae9STrond Myklebust 		return 1;
470e468bae9STrond Myklebust 	}
471e468bae9STrond Myklebust 	return 0;
472e468bae9STrond Myklebust }
473e468bae9STrond Myklebust 
4748e821cadSTrond Myklebust static inline
4758e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
4768e821cadSTrond Myklebust {
477*465d5243SFred Isaman 	if (data->verf.committed == NFS_DATA_SYNC)
478*465d5243SFred Isaman 		return data->lseg == NULL;
479*465d5243SFred Isaman 	else
4808e821cadSTrond Myklebust 		return data->verf.committed != NFS_FILE_SYNC;
4818e821cadSTrond Myklebust }
4828e821cadSTrond Myklebust 
4838e821cadSTrond Myklebust static inline
4848e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req)
4858e821cadSTrond Myklebust {
486e468bae9STrond Myklebust 	if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) {
4878e821cadSTrond Myklebust 		nfs_mark_request_commit(req);
4888e821cadSTrond Myklebust 		return 1;
4898e821cadSTrond Myklebust 	}
4908e821cadSTrond Myklebust 	if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
4916d884e8fSFred 		nfs_mark_request_dirty(req);
4928e821cadSTrond Myklebust 		return 1;
4938e821cadSTrond Myklebust 	}
4948e821cadSTrond Myklebust 	return 0;
4958e821cadSTrond Myklebust }
4968e821cadSTrond Myklebust #else
4978e821cadSTrond Myklebust static inline void
4988e821cadSTrond Myklebust nfs_mark_request_commit(struct nfs_page *req)
4998e821cadSTrond Myklebust {
5008e821cadSTrond Myklebust }
5018e821cadSTrond Myklebust 
502e468bae9STrond Myklebust static inline int
503e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
504e468bae9STrond Myklebust {
505e468bae9STrond Myklebust 	return 0;
506e468bae9STrond Myklebust }
507e468bae9STrond Myklebust 
5088e821cadSTrond Myklebust static inline
5098e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
5108e821cadSTrond Myklebust {
5118e821cadSTrond Myklebust 	return 0;
5128e821cadSTrond Myklebust }
5138e821cadSTrond Myklebust 
5148e821cadSTrond Myklebust static inline
5158e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req)
5168e821cadSTrond Myklebust {
5178e821cadSTrond Myklebust 	return 0;
5188e821cadSTrond Myklebust }
5191da177e4SLinus Torvalds #endif
5201da177e4SLinus Torvalds 
52147c62564STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
522fb8a1f11STrond Myklebust static int
523fb8a1f11STrond Myklebust nfs_need_commit(struct nfs_inode *nfsi)
524fb8a1f11STrond Myklebust {
525fb8a1f11STrond Myklebust 	return radix_tree_tagged(&nfsi->nfs_page_tree, NFS_PAGE_TAG_COMMIT);
526fb8a1f11STrond Myklebust }
527fb8a1f11STrond Myklebust 
5281da177e4SLinus Torvalds /*
5291da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
5301da177e4SLinus Torvalds  * @inode: NFS inode to scan
5311da177e4SLinus Torvalds  * @dst: destination list
5321da177e4SLinus Torvalds  * @idx_start: lower bound of page->index to scan.
5331da177e4SLinus Torvalds  * @npages: idx_start + npages sets the upper bound to scan.
5341da177e4SLinus Torvalds  *
5351da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
5361da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
5371da177e4SLinus Torvalds  */
5381da177e4SLinus Torvalds static int
539ca52fec1STrond Myklebust nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
5401da177e4SLinus Torvalds {
5411da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
542ff778d02STrond Myklebust 	int ret;
5433da28eb1STrond Myklebust 
544fb8a1f11STrond Myklebust 	if (!nfs_need_commit(nfsi))
545fb8a1f11STrond Myklebust 		return 0;
546fb8a1f11STrond Myklebust 
547ff778d02STrond Myklebust 	ret = nfs_scan_list(nfsi, dst, idx_start, npages, NFS_PAGE_TAG_COMMIT);
548ff778d02STrond Myklebust 	if (ret > 0)
549ff778d02STrond Myklebust 		nfsi->ncommit -= ret;
5502928db1fSTrond Myklebust 	if (nfs_need_commit(NFS_I(inode)))
5512928db1fSTrond Myklebust 		__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
552ff778d02STrond Myklebust 	return ret;
5531da177e4SLinus Torvalds }
554c42de9ddSTrond Myklebust #else
555fb8a1f11STrond Myklebust static inline int nfs_need_commit(struct nfs_inode *nfsi)
556fb8a1f11STrond Myklebust {
557fb8a1f11STrond Myklebust 	return 0;
558fb8a1f11STrond Myklebust }
559fb8a1f11STrond Myklebust 
560ca52fec1STrond Myklebust static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
561c42de9ddSTrond Myklebust {
562c42de9ddSTrond Myklebust 	return 0;
563c42de9ddSTrond Myklebust }
5641da177e4SLinus Torvalds #endif
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds /*
567e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
568e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
5691da177e4SLinus Torvalds  *
570e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
571e7d39069STrond Myklebust  * to disk.
5721da177e4SLinus Torvalds  */
573e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
574e7d39069STrond Myklebust 		struct page *page,
575e7d39069STrond Myklebust 		unsigned int offset,
576e7d39069STrond Myklebust 		unsigned int bytes)
5771da177e4SLinus Torvalds {
578e7d39069STrond Myklebust 	struct nfs_page *req;
579e7d39069STrond Myklebust 	unsigned int rqend;
580e7d39069STrond Myklebust 	unsigned int end;
5811da177e4SLinus Torvalds 	int error;
582277459d2STrond Myklebust 
583e7d39069STrond Myklebust 	if (!PagePrivate(page))
584e7d39069STrond Myklebust 		return NULL;
585e7d39069STrond Myklebust 
586e7d39069STrond Myklebust 	end = offset + bytes;
587e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
588e7d39069STrond Myklebust 
589e7d39069STrond Myklebust 	for (;;) {
590e7d39069STrond Myklebust 		req = nfs_page_find_request_locked(page);
591e7d39069STrond Myklebust 		if (req == NULL)
592e7d39069STrond Myklebust 			goto out_unlock;
593e7d39069STrond Myklebust 
594e7d39069STrond Myklebust 		rqend = req->wb_offset + req->wb_bytes;
595e7d39069STrond Myklebust 		/*
596e7d39069STrond Myklebust 		 * Tell the caller to flush out the request if
597e7d39069STrond Myklebust 		 * the offsets are non-contiguous.
598e7d39069STrond Myklebust 		 * Note: nfs_flush_incompatible() will already
599e7d39069STrond Myklebust 		 * have flushed out requests having wrong owners.
600e7d39069STrond Myklebust 		 */
601e468bae9STrond Myklebust 		if (offset > rqend
602e7d39069STrond Myklebust 		    || end < req->wb_offset)
603e7d39069STrond Myklebust 			goto out_flushme;
604e7d39069STrond Myklebust 
605e7d39069STrond Myklebust 		if (nfs_set_page_tag_locked(req))
606e7d39069STrond Myklebust 			break;
607e7d39069STrond Myklebust 
608e7d39069STrond Myklebust 		/* The request is locked, so wait and then retry */
609587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
6101da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
6111da177e4SLinus Torvalds 		nfs_release_request(req);
612e7d39069STrond Myklebust 		if (error != 0)
613e7d39069STrond Myklebust 			goto out_err;
614e7d39069STrond Myklebust 		spin_lock(&inode->i_lock);
6151da177e4SLinus Torvalds 	}
6161da177e4SLinus Torvalds 
617ff778d02STrond Myklebust 	if (nfs_clear_request_commit(req) &&
618e468bae9STrond Myklebust 			radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree,
619ff778d02STrond Myklebust 				req->wb_index, NFS_PAGE_TAG_COMMIT) != NULL)
620ff778d02STrond Myklebust 		NFS_I(inode)->ncommit--;
621e468bae9STrond Myklebust 
6221da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
6231da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
6241da177e4SLinus Torvalds 		req->wb_offset = offset;
6251da177e4SLinus Torvalds 		req->wb_pgbase = offset;
6261da177e4SLinus Torvalds 	}
6271da177e4SLinus Torvalds 	if (end > rqend)
6281da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
629e7d39069STrond Myklebust 	else
630e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
631e7d39069STrond Myklebust out_unlock:
632e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
633e7d39069STrond Myklebust 	return req;
634e7d39069STrond Myklebust out_flushme:
635e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
636e7d39069STrond Myklebust 	nfs_release_request(req);
637e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
638e7d39069STrond Myklebust out_err:
639e7d39069STrond Myklebust 	return ERR_PTR(error);
640e7d39069STrond Myklebust }
6411da177e4SLinus Torvalds 
642e7d39069STrond Myklebust /*
643e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
644e7d39069STrond Myklebust  *
645e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
646e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
647e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
648e7d39069STrond Myklebust  */
649e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
650e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
651e7d39069STrond Myklebust {
652e7d39069STrond Myklebust 	struct inode *inode = page->mapping->host;
653e7d39069STrond Myklebust 	struct nfs_page	*req;
654e7d39069STrond Myklebust 	int error;
655e7d39069STrond Myklebust 
656e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
657e7d39069STrond Myklebust 	if (req != NULL)
658e7d39069STrond Myklebust 		goto out;
659e7d39069STrond Myklebust 	req = nfs_create_request(ctx, inode, page, offset, bytes);
660e7d39069STrond Myklebust 	if (IS_ERR(req))
661e7d39069STrond Myklebust 		goto out;
662e7d39069STrond Myklebust 	error = nfs_inode_add_request(inode, req);
663e7d39069STrond Myklebust 	if (error != 0) {
664e7d39069STrond Myklebust 		nfs_release_request(req);
665e7d39069STrond Myklebust 		req = ERR_PTR(error);
666e7d39069STrond Myklebust 	}
667efc91ed0STrond Myklebust out:
66861e930a9STrond Myklebust 	return req;
6691da177e4SLinus Torvalds }
6701da177e4SLinus Torvalds 
671e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
672e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
673e7d39069STrond Myklebust {
674e7d39069STrond Myklebust 	struct nfs_page	*req;
675e7d39069STrond Myklebust 
676e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
677e7d39069STrond Myklebust 	if (IS_ERR(req))
678e7d39069STrond Myklebust 		return PTR_ERR(req);
679b80c3cb6STrond Myklebust 	nfs_mark_request_dirty(req);
680e7d39069STrond Myklebust 	/* Update file length */
681e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
682e7d39069STrond Myklebust 	nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
683a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
684e7d39069STrond Myklebust 	nfs_clear_page_tag_locked(req);
685e7d39069STrond Myklebust 	return 0;
686e7d39069STrond Myklebust }
687e7d39069STrond Myklebust 
6881da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
6891da177e4SLinus Torvalds {
690cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
6911da177e4SLinus Torvalds 	struct nfs_page	*req;
6921a54533eSTrond Myklebust 	int do_flush, status;
6931da177e4SLinus Torvalds 	/*
6941da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
6951da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
6961da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
6971da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
6981da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
6991da177e4SLinus Torvalds 	 * dropped page.
7001da177e4SLinus Torvalds 	 */
7011a54533eSTrond Myklebust 	do {
702277459d2STrond Myklebust 		req = nfs_page_find_request(page);
7031a54533eSTrond Myklebust 		if (req == NULL)
7041a54533eSTrond Myklebust 			return 0;
705f11ac8dbSTrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx ||
706f11ac8dbSTrond Myklebust 			req->wb_lock_context->lockowner != current->files ||
707f11ac8dbSTrond Myklebust 			req->wb_lock_context->pid != current->tgid;
7081da177e4SLinus Torvalds 		nfs_release_request(req);
7091a54533eSTrond Myklebust 		if (!do_flush)
7101a54533eSTrond Myklebust 			return 0;
711277459d2STrond Myklebust 		status = nfs_wb_page(page->mapping->host, page);
7121a54533eSTrond Myklebust 	} while (status == 0);
7131a54533eSTrond Myklebust 	return status;
7141da177e4SLinus Torvalds }
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds /*
7175d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
7185d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
7195d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
7205d47a356STrond Myklebust  */
7215d47a356STrond Myklebust static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
7225d47a356STrond Myklebust {
7235d47a356STrond Myklebust 	return PageUptodate(page) &&
7245d47a356STrond Myklebust 		!(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
7255d47a356STrond Myklebust }
7265d47a356STrond Myklebust 
7275d47a356STrond Myklebust /*
7281da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
7291da177e4SLinus Torvalds  *
7301da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
7311da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
7321da177e4SLinus Torvalds  */
7331da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
7341da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
7351da177e4SLinus Torvalds {
736cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
7371da177e4SLinus Torvalds 	struct inode	*inode = page->mapping->host;
7381da177e4SLinus Torvalds 	int		status = 0;
7391da177e4SLinus Torvalds 
74091d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
74191d5b470SChuck Lever 
74248186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage(%s/%s %d@%lld)\n",
74301cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
74401cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name, count,
7450bbacc40SChuck Lever 		(long long)(page_offset(page) + offset));
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 	/* If we're not using byte range locks, and we know the page
7485d47a356STrond Myklebust 	 * is up to date, it may be more efficient to extend the write
7495d47a356STrond Myklebust 	 * to cover the entire page in order to avoid fragmentation
7505d47a356STrond Myklebust 	 * inefficiencies.
7511da177e4SLinus Torvalds 	 */
7525d47a356STrond Myklebust 	if (nfs_write_pageuptodate(page, inode) &&
7535d47a356STrond Myklebust 			inode->i_flock == NULL &&
7546b2f3d1fSChristoph Hellwig 			!(file->f_flags & O_DSYNC)) {
75549a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
7561da177e4SLinus Torvalds 		offset = 0;
7571da177e4SLinus Torvalds 	}
7581da177e4SLinus Torvalds 
759e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
76003fa9e84STrond Myklebust 	if (status < 0)
76103fa9e84STrond Myklebust 		nfs_set_pageerror(page);
7621da177e4SLinus Torvalds 
76348186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
7641da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
7651da177e4SLinus Torvalds 	return status;
7661da177e4SLinus Torvalds }
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds static void nfs_writepage_release(struct nfs_page *req)
7691da177e4SLinus Torvalds {
770a6305ddbSTrond Myklebust 	struct page *page = req->wb_page;
7718e821cadSTrond Myklebust 
772a6305ddbSTrond Myklebust 	if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req))
7731da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
7749fd367f0STrond Myklebust 	nfs_clear_page_tag_locked(req);
775a6305ddbSTrond Myklebust 	nfs_end_page_writeback(page);
7761da177e4SLinus Torvalds }
7771da177e4SLinus Torvalds 
7783ff7576dSTrond Myklebust static int flush_task_priority(int how)
7791da177e4SLinus Torvalds {
7801da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
7811da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
7821da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
7831da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
7841da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
7851da177e4SLinus Torvalds 	}
7861da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
7871da177e4SLinus Torvalds }
7881da177e4SLinus Torvalds 
789a69aef14SFred Isaman int nfs_initiate_write(struct nfs_write_data *data,
790d138d5d1SAndy Adamson 		       struct rpc_clnt *clnt,
791788e7a89STrond Myklebust 		       const struct rpc_call_ops *call_ops,
7921da177e4SLinus Torvalds 		       int how)
7931da177e4SLinus Torvalds {
794d138d5d1SAndy Adamson 	struct inode *inode = data->inode;
7953ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
79607737691STrond Myklebust 	struct rpc_task *task;
797bdc7f021STrond Myklebust 	struct rpc_message msg = {
798bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
799bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
800d138d5d1SAndy Adamson 		.rpc_cred = data->cred,
801bdc7f021STrond Myklebust 	};
80284115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
803d138d5d1SAndy Adamson 		.rpc_client = clnt,
80407737691STrond Myklebust 		.task = &data->task,
805bdc7f021STrond Myklebust 		.rpc_message = &msg,
80684115e1cSTrond Myklebust 		.callback_ops = call_ops,
80784115e1cSTrond Myklebust 		.callback_data = data,
808101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
8092c61be0aSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
8103ff7576dSTrond Myklebust 		.priority = priority,
81184115e1cSTrond Myklebust 	};
8122c61be0aSTrond Myklebust 	int ret = 0;
8131da177e4SLinus Torvalds 
814d138d5d1SAndy Adamson 	/* Set up the initial task struct.  */
815d138d5d1SAndy Adamson 	NFS_PROTO(inode)->write_setup(data, &msg);
816d138d5d1SAndy Adamson 
817d138d5d1SAndy Adamson 	dprintk("NFS: %5u initiated write call "
818d138d5d1SAndy Adamson 		"(req %s/%lld, %u bytes @ offset %llu)\n",
819d138d5d1SAndy Adamson 		data->task.tk_pid,
820d138d5d1SAndy Adamson 		inode->i_sb->s_id,
821d138d5d1SAndy Adamson 		(long long)NFS_FILEID(inode),
822d138d5d1SAndy Adamson 		data->args.count,
823d138d5d1SAndy Adamson 		(unsigned long long)data->args.offset);
824d138d5d1SAndy Adamson 
825d138d5d1SAndy Adamson 	task = rpc_run_task(&task_setup_data);
826d138d5d1SAndy Adamson 	if (IS_ERR(task)) {
827d138d5d1SAndy Adamson 		ret = PTR_ERR(task);
828d138d5d1SAndy Adamson 		goto out;
829d138d5d1SAndy Adamson 	}
830d138d5d1SAndy Adamson 	if (how & FLUSH_SYNC) {
831d138d5d1SAndy Adamson 		ret = rpc_wait_for_completion_task(task);
832d138d5d1SAndy Adamson 		if (ret == 0)
833d138d5d1SAndy Adamson 			ret = task->tk_status;
834d138d5d1SAndy Adamson 	}
835d138d5d1SAndy Adamson 	rpc_put_task(task);
836d138d5d1SAndy Adamson out:
837d138d5d1SAndy Adamson 	return ret;
838d138d5d1SAndy Adamson }
839a69aef14SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_write);
840d138d5d1SAndy Adamson 
841d138d5d1SAndy Adamson /*
842d138d5d1SAndy Adamson  * Set up the argument/result storage required for the RPC call.
843d138d5d1SAndy Adamson  */
844d138d5d1SAndy Adamson static int nfs_write_rpcsetup(struct nfs_page *req,
845d138d5d1SAndy Adamson 		struct nfs_write_data *data,
846d138d5d1SAndy Adamson 		const struct rpc_call_ops *call_ops,
847d138d5d1SAndy Adamson 		unsigned int count, unsigned int offset,
8485053aa56SFred Isaman 		struct pnfs_layout_segment *lseg,
849d138d5d1SAndy Adamson 		int how)
850d138d5d1SAndy Adamson {
851d138d5d1SAndy Adamson 	struct inode *inode = req->wb_context->path.dentry->d_inode;
852d138d5d1SAndy Adamson 
8531da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
8541da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	data->req = req;
85788be9f99STrond Myklebust 	data->inode = inode = req->wb_context->path.dentry->d_inode;
858d138d5d1SAndy Adamson 	data->cred = req->wb_context->cred;
8595053aa56SFred Isaman 	data->lseg = get_lseg(lseg);
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(inode);
8621da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
8631da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
8641da177e4SLinus Torvalds 	data->args.pages  = data->pagevec;
8651da177e4SLinus Torvalds 	data->args.count  = count;
866383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
867f11ac8dbSTrond Myklebust 	data->args.lock_context = req->wb_lock_context;
868bdc7f021STrond Myklebust 	data->args.stable  = NFS_UNSTABLE;
869b31268acSTrond Myklebust 	if (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
870bdc7f021STrond Myklebust 		data->args.stable = NFS_DATA_SYNC;
871fb8a1f11STrond Myklebust 		if (!nfs_need_commit(NFS_I(inode)))
872bdc7f021STrond Myklebust 			data->args.stable = NFS_FILE_SYNC;
873bdc7f021STrond Myklebust 	}
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
8761da177e4SLinus Torvalds 	data->res.count   = count;
8771da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
8780e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
8791da177e4SLinus Torvalds 
8800382b744SAndy Adamson 	if (data->lseg &&
8810382b744SAndy Adamson 	    (pnfs_try_to_write_data(data, call_ops, how) == PNFS_ATTEMPTED))
8820382b744SAndy Adamson 		return 0;
8830382b744SAndy Adamson 
884d138d5d1SAndy Adamson 	return nfs_initiate_write(data, NFS_CLIENT(inode), call_ops, how);
8851da177e4SLinus Torvalds }
8861da177e4SLinus Torvalds 
8876d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
8886d884e8fSFred  * call this on each, which will prepare them to be retried on next
8896d884e8fSFred  * writeback using standard nfs.
8906d884e8fSFred  */
8916d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
8926d884e8fSFred {
893a6305ddbSTrond Myklebust 	struct page *page = req->wb_page;
894a6305ddbSTrond Myklebust 
8956d884e8fSFred 	nfs_mark_request_dirty(req);
8966d884e8fSFred 	nfs_clear_page_tag_locked(req);
897a6305ddbSTrond Myklebust 	nfs_end_page_writeback(page);
8986d884e8fSFred }
8996d884e8fSFred 
9001da177e4SLinus Torvalds /*
9011da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
9021da177e4SLinus Torvalds  * contiguous dirty area on one page.
9031da177e4SLinus Torvalds  */
904c76069bdSFred Isaman static int nfs_flush_multi(struct nfs_pageio_descriptor *desc)
9051da177e4SLinus Torvalds {
906c76069bdSFred Isaman 	struct nfs_page *req = nfs_list_entry(desc->pg_list.next);
9071da177e4SLinus Torvalds 	struct page *page = req->wb_page;
9081da177e4SLinus Torvalds 	struct nfs_write_data *data;
909c76069bdSFred Isaman 	size_t wsize = NFS_SERVER(desc->pg_inode)->wsize, nbytes;
910e9f7bee1STrond Myklebust 	unsigned int offset;
9111da177e4SLinus Torvalds 	int requests = 0;
912dbae4c73STrond Myklebust 	int ret = 0;
913c76069bdSFred Isaman 	struct pnfs_layout_segment *lseg;
9141da177e4SLinus Torvalds 	LIST_HEAD(list);
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 	nfs_list_remove_request(req);
9171da177e4SLinus Torvalds 
918b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
919b31268acSTrond Myklebust 	    (desc->pg_moreio || NFS_I(desc->pg_inode)->ncommit ||
920b31268acSTrond Myklebust 	     desc->pg_count > wsize))
921b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
922b31268acSTrond Myklebust 
923b31268acSTrond Myklebust 
924c76069bdSFred Isaman 	nbytes = desc->pg_count;
925e9f7bee1STrond Myklebust 	do {
926e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
927e9f7bee1STrond Myklebust 
9288d5658c9STrond Myklebust 		data = nfs_writedata_alloc(1);
9291da177e4SLinus Torvalds 		if (!data)
9301da177e4SLinus Torvalds 			goto out_bad;
9311da177e4SLinus Torvalds 		list_add(&data->pages, &list);
9321da177e4SLinus Torvalds 		requests++;
933e9f7bee1STrond Myklebust 		nbytes -= len;
934e9f7bee1STrond Myklebust 	} while (nbytes != 0);
9351da177e4SLinus Torvalds 	atomic_set(&req->wb_complete, requests);
9361da177e4SLinus Torvalds 
937c76069bdSFred Isaman 	BUG_ON(desc->pg_lseg);
938c76069bdSFred Isaman 	lseg = pnfs_update_layout(desc->pg_inode, req->wb_context, IOMODE_RW);
9391da177e4SLinus Torvalds 	ClearPageError(page);
9401da177e4SLinus Torvalds 	offset = 0;
941c76069bdSFred Isaman 	nbytes = desc->pg_count;
9421da177e4SLinus Torvalds 	do {
943dbae4c73STrond Myklebust 		int ret2;
944dbae4c73STrond Myklebust 
9451da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
9461da177e4SLinus Torvalds 		list_del_init(&data->pages);
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds 		data->pagevec[0] = page;
9491da177e4SLinus Torvalds 
950bcb71bbaSTrond Myklebust 		if (nbytes < wsize)
951bcb71bbaSTrond Myklebust 			wsize = nbytes;
952dbae4c73STrond Myklebust 		ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
953c76069bdSFred Isaman 					  wsize, offset, lseg, desc->pg_ioflags);
954dbae4c73STrond Myklebust 		if (ret == 0)
955dbae4c73STrond Myklebust 			ret = ret2;
9561da177e4SLinus Torvalds 		offset += wsize;
9571da177e4SLinus Torvalds 		nbytes -= wsize;
9581da177e4SLinus Torvalds 	} while (nbytes != 0);
9591da177e4SLinus Torvalds 
96044b83799SFred Isaman 	put_lseg(lseg);
96136fe432dSFred Isaman 	desc->pg_lseg = NULL;
962dbae4c73STrond Myklebust 	return ret;
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds out_bad:
9651da177e4SLinus Torvalds 	while (!list_empty(&list)) {
9661da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
9671da177e4SLinus Torvalds 		list_del(&data->pages);
9680da2a4acSFred Isaman 		nfs_writedata_free(data);
9691da177e4SLinus Torvalds 	}
97061822ab5STrond Myklebust 	nfs_redirty_request(req);
9711da177e4SLinus Torvalds 	return -ENOMEM;
9721da177e4SLinus Torvalds }
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds /*
9751da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
9761da177e4SLinus Torvalds  * The page must have been locked by the caller.
9771da177e4SLinus Torvalds  *
9781da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
9791da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
9801da177e4SLinus Torvalds  * that has been written but not committed.
9811da177e4SLinus Torvalds  */
982c76069bdSFred Isaman static int nfs_flush_one(struct nfs_pageio_descriptor *desc)
9831da177e4SLinus Torvalds {
9841da177e4SLinus Torvalds 	struct nfs_page		*req;
9851da177e4SLinus Torvalds 	struct page		**pages;
9861da177e4SLinus Torvalds 	struct nfs_write_data	*data;
987c76069bdSFred Isaman 	struct list_head *head = &desc->pg_list;
988c76069bdSFred Isaman 	struct pnfs_layout_segment *lseg = desc->pg_lseg;
98944b83799SFred Isaman 	int ret;
9901da177e4SLinus Torvalds 
991c76069bdSFred Isaman 	data = nfs_writedata_alloc(nfs_page_array_len(desc->pg_base,
992c76069bdSFred Isaman 						      desc->pg_count));
99344b83799SFred Isaman 	if (!data) {
99444b83799SFred Isaman 		while (!list_empty(head)) {
99544b83799SFred Isaman 			req = nfs_list_entry(head->next);
99644b83799SFred Isaman 			nfs_list_remove_request(req);
99744b83799SFred Isaman 			nfs_redirty_request(req);
99844b83799SFred Isaman 		}
99944b83799SFred Isaman 		ret = -ENOMEM;
100044b83799SFred Isaman 		goto out;
100144b83799SFred Isaman 	}
10021da177e4SLinus Torvalds 	pages = data->pagevec;
10031da177e4SLinus Torvalds 	while (!list_empty(head)) {
10041da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
10051da177e4SLinus Torvalds 		nfs_list_remove_request(req);
10061da177e4SLinus Torvalds 		nfs_list_add_request(req, &data->pages);
10071da177e4SLinus Torvalds 		ClearPageError(req->wb_page);
10081da177e4SLinus Torvalds 		*pages++ = req->wb_page;
10091da177e4SLinus Torvalds 	}
10101da177e4SLinus Torvalds 	req = nfs_list_entry(data->pages.next);
101144b83799SFred Isaman 	if ((!lseg) && list_is_singular(&data->pages))
1012c76069bdSFred Isaman 		lseg = pnfs_update_layout(desc->pg_inode, req->wb_context, IOMODE_RW);
10131da177e4SLinus Torvalds 
1014b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1015b31268acSTrond Myklebust 	    (desc->pg_moreio || NFS_I(desc->pg_inode)->ncommit))
1016b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1017b31268acSTrond Myklebust 
10181da177e4SLinus Torvalds 	/* Set up the argument struct */
1019c76069bdSFred Isaman 	ret = nfs_write_rpcsetup(req, data, &nfs_write_full_ops, desc->pg_count, 0, lseg, desc->pg_ioflags);
102044b83799SFred Isaman out:
102144b83799SFred Isaman 	put_lseg(lseg); /* Cleans any gotten in ->pg_test */
102236fe432dSFred Isaman 	desc->pg_lseg = NULL;
102344b83799SFred Isaman 	return ret;
10241da177e4SLinus Torvalds }
10251da177e4SLinus Torvalds 
1026c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1027c63c7b05STrond Myklebust 				  struct inode *inode, int ioflags)
10281da177e4SLinus Torvalds {
1029bf4285e7SChuck Lever 	size_t wsize = NFS_SERVER(inode)->wsize;
10301da177e4SLinus Torvalds 
103144b83799SFred Isaman 	pnfs_pageio_init_write(pgio, inode);
103294ad1c80SFred Isaman 
1033bcb71bbaSTrond Myklebust 	if (wsize < PAGE_CACHE_SIZE)
1034c63c7b05STrond Myklebust 		nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
1035bcb71bbaSTrond Myklebust 	else
1036c63c7b05STrond Myklebust 		nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
10371da177e4SLinus Torvalds }
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds /*
10401da177e4SLinus Torvalds  * Handle a write reply that flushed part of a page.
10411da177e4SLinus Torvalds  */
1042788e7a89STrond Myklebust static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
10431da177e4SLinus Torvalds {
1044788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
10451da177e4SLinus Torvalds 
104648186c7dSChuck Lever 	dprintk("NFS: %5u write(%s/%lld %d@%lld)",
104748186c7dSChuck Lever 		task->tk_pid,
104848186c7dSChuck Lever 		data->req->wb_context->path.dentry->d_inode->i_sb->s_id,
104948186c7dSChuck Lever 		(long long)
105048186c7dSChuck Lever 		  NFS_FILEID(data->req->wb_context->path.dentry->d_inode),
105148186c7dSChuck Lever 		data->req->wb_bytes, (long long)req_offset(data->req));
10521da177e4SLinus Torvalds 
1053c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
1054c9d8f89dSTrond Myklebust }
1055788e7a89STrond Myklebust 
1056c9d8f89dSTrond Myklebust static void nfs_writeback_release_partial(void *calldata)
1057c9d8f89dSTrond Myklebust {
1058c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1059c9d8f89dSTrond Myklebust 	struct nfs_page		*req = data->req;
1060c9d8f89dSTrond Myklebust 	struct page		*page = req->wb_page;
1061c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1062c9d8f89dSTrond Myklebust 
1063c9d8f89dSTrond Myklebust 	if (status < 0) {
1064a301b777STrond Myklebust 		nfs_set_pageerror(page);
1065c9d8f89dSTrond Myklebust 		nfs_context_set_write_error(req->wb_context, status);
1066c9d8f89dSTrond Myklebust 		dprintk(", error = %d\n", status);
10678e821cadSTrond Myklebust 		goto out;
10688e821cadSTrond Myklebust 	}
10698e821cadSTrond Myklebust 
10708e821cadSTrond Myklebust 	if (nfs_write_need_commit(data)) {
1071587142f8STrond Myklebust 		struct inode *inode = page->mapping->host;
10728e821cadSTrond Myklebust 
1073587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
10748e821cadSTrond Myklebust 		if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
10758e821cadSTrond Myklebust 			/* Do nothing we need to resend the writes */
10768e821cadSTrond Myklebust 		} else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
10771da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
10781da177e4SLinus Torvalds 			dprintk(" defer commit\n");
10791da177e4SLinus Torvalds 		} else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
10808e821cadSTrond Myklebust 			set_bit(PG_NEED_RESCHED, &req->wb_flags);
10818e821cadSTrond Myklebust 			clear_bit(PG_NEED_COMMIT, &req->wb_flags);
10821da177e4SLinus Torvalds 			dprintk(" server reboot detected\n");
10831da177e4SLinus Torvalds 		}
1084587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
10851da177e4SLinus Torvalds 	} else
10861da177e4SLinus Torvalds 		dprintk(" OK\n");
10871da177e4SLinus Torvalds 
10888e821cadSTrond Myklebust out:
10891da177e4SLinus Torvalds 	if (atomic_dec_and_test(&req->wb_complete))
10901da177e4SLinus Torvalds 		nfs_writepage_release(req);
1091c9d8f89dSTrond Myklebust 	nfs_writedata_release(calldata);
10921da177e4SLinus Torvalds }
10931da177e4SLinus Torvalds 
1094def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1)
1095def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata)
1096def6ed7eSAndy Adamson {
1097def6ed7eSAndy Adamson 	struct nfs_write_data *data = calldata;
1098def6ed7eSAndy Adamson 
1099035168abSTrond Myklebust 	if (nfs4_setup_sequence(NFS_SERVER(data->inode),
1100035168abSTrond Myklebust 				&data->args.seq_args,
1101def6ed7eSAndy Adamson 				&data->res.seq_res, 1, task))
1102def6ed7eSAndy Adamson 		return;
1103def6ed7eSAndy Adamson 	rpc_call_start(task);
1104def6ed7eSAndy Adamson }
1105def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1106def6ed7eSAndy Adamson 
1107788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops = {
1108def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1)
1109def6ed7eSAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
1110def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1111788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_partial,
1112c9d8f89dSTrond Myklebust 	.rpc_release = nfs_writeback_release_partial,
1113788e7a89STrond Myklebust };
1114788e7a89STrond Myklebust 
11151da177e4SLinus Torvalds /*
11161da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
11171da177e4SLinus Torvalds  *
11181da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
11191da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
11201da177e4SLinus Torvalds  *	  as the page has a write request pending.
11211da177e4SLinus Torvalds  */
1122788e7a89STrond Myklebust static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
11231da177e4SLinus Torvalds {
1124788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
11251da177e4SLinus Torvalds 
1126c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
1127c9d8f89dSTrond Myklebust }
1128c9d8f89dSTrond Myklebust 
1129c9d8f89dSTrond Myklebust static void nfs_writeback_release_full(void *calldata)
1130c9d8f89dSTrond Myklebust {
1131c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1132c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1133788e7a89STrond Myklebust 
11341da177e4SLinus Torvalds 	/* Update attributes as result of writeback. */
11351da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
1136c9d8f89dSTrond Myklebust 		struct nfs_page *req = nfs_list_entry(data->pages.next);
1137c9d8f89dSTrond Myklebust 		struct page *page = req->wb_page;
1138c9d8f89dSTrond Myklebust 
11391da177e4SLinus Torvalds 		nfs_list_remove_request(req);
11401da177e4SLinus Torvalds 
114148186c7dSChuck Lever 		dprintk("NFS: %5u write (%s/%lld %d@%lld)",
114248186c7dSChuck Lever 			data->task.tk_pid,
114388be9f99STrond Myklebust 			req->wb_context->path.dentry->d_inode->i_sb->s_id,
114488be9f99STrond Myklebust 			(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
11451da177e4SLinus Torvalds 			req->wb_bytes,
11461da177e4SLinus Torvalds 			(long long)req_offset(req));
11471da177e4SLinus Torvalds 
1148c9d8f89dSTrond Myklebust 		if (status < 0) {
1149a301b777STrond Myklebust 			nfs_set_pageerror(page);
1150c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
1151c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
11528e821cadSTrond Myklebust 			goto remove_request;
11531da177e4SLinus Torvalds 		}
11541da177e4SLinus Torvalds 
11558e821cadSTrond Myklebust 		if (nfs_write_need_commit(data)) {
11561da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
11571da177e4SLinus Torvalds 			nfs_mark_request_commit(req);
11581da177e4SLinus Torvalds 			dprintk(" marked for commit\n");
11598e821cadSTrond Myklebust 			goto next;
11608e821cadSTrond Myklebust 		}
11618e821cadSTrond Myklebust 		dprintk(" OK\n");
11628e821cadSTrond Myklebust remove_request:
11631da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
11641da177e4SLinus Torvalds 	next:
11659fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
1166a6305ddbSTrond Myklebust 		nfs_end_page_writeback(page);
11671da177e4SLinus Torvalds 	}
1168c9d8f89dSTrond Myklebust 	nfs_writedata_release(calldata);
11691da177e4SLinus Torvalds }
11701da177e4SLinus Torvalds 
1171788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops = {
1172def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1)
1173def6ed7eSAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
1174def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1175788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_full,
1176c9d8f89dSTrond Myklebust 	.rpc_release = nfs_writeback_release_full,
1177788e7a89STrond Myklebust };
1178788e7a89STrond Myklebust 
1179788e7a89STrond Myklebust 
11801da177e4SLinus Torvalds /*
11811da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
11821da177e4SLinus Torvalds  */
118313602896SFred Isaman void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
11841da177e4SLinus Torvalds {
11851da177e4SLinus Torvalds 	struct nfs_writeargs	*argp = &data->args;
11861da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
1187eedc020eSAndy Adamson 	struct nfs_server	*server = NFS_SERVER(data->inode);
1188788e7a89STrond Myklebust 	int status;
11891da177e4SLinus Torvalds 
1190a3f565b1SChuck Lever 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
11911da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
11921da177e4SLinus Torvalds 
1193f551e44fSChuck Lever 	/*
1194f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1195f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1196f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1197f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1198f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1199f551e44fSChuck Lever 	 */
1200788e7a89STrond Myklebust 	status = NFS_PROTO(data->inode)->write_done(task, data);
1201788e7a89STrond Myklebust 	if (status != 0)
120213602896SFred Isaman 		return;
120391d5b470SChuck Lever 	nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
120491d5b470SChuck Lever 
12051da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
12061da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
12071da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
12081da177e4SLinus Torvalds 		 * commit data to stable storage even though we
12091da177e4SLinus Torvalds 		 * requested it.
12101da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
12111da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
12121da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
12131da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
12141da177e4SLinus Torvalds 		 */
12151da177e4SLinus Torvalds 		static unsigned long    complain;
12161da177e4SLinus Torvalds 
1217a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
12181da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
12191da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
12201da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1221eedc020eSAndy Adamson 				server->nfs_client->cl_hostname,
12221da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
12231da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
12241da177e4SLinus Torvalds 		}
12251da177e4SLinus Torvalds 	}
12261da177e4SLinus Torvalds #endif
12271da177e4SLinus Torvalds 	/* Is this a short write? */
12281da177e4SLinus Torvalds 	if (task->tk_status >= 0 && resp->count < argp->count) {
12291da177e4SLinus Torvalds 		static unsigned long    complain;
12301da177e4SLinus Torvalds 
123191d5b470SChuck Lever 		nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
123291d5b470SChuck Lever 
12331da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
12341da177e4SLinus Torvalds 		if (resp->count != 0) {
12351da177e4SLinus Torvalds 			/* Was this an NFSv2 write or an NFSv3 stable write? */
12361da177e4SLinus Torvalds 			if (resp->verf->committed != NFS_UNSTABLE) {
12371da177e4SLinus Torvalds 				/* Resend from where the server left off */
1238a69aef14SFred Isaman 				data->mds_offset += resp->count;
12391da177e4SLinus Torvalds 				argp->offset += resp->count;
12401da177e4SLinus Torvalds 				argp->pgbase += resp->count;
12411da177e4SLinus Torvalds 				argp->count -= resp->count;
12421da177e4SLinus Torvalds 			} else {
12431da177e4SLinus Torvalds 				/* Resend as a stable write in order to avoid
12441da177e4SLinus Torvalds 				 * headaches in the case of a server crash.
12451da177e4SLinus Torvalds 				 */
12461da177e4SLinus Torvalds 				argp->stable = NFS_FILE_SYNC;
12471da177e4SLinus Torvalds 			}
12480110ee15STrond Myklebust 			nfs_restart_rpc(task, server->nfs_client);
124913602896SFred Isaman 			return;
12501da177e4SLinus Torvalds 		}
12511da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
12521da177e4SLinus Torvalds 			printk(KERN_WARNING
12531da177e4SLinus Torvalds 			       "NFS: Server wrote zero bytes, expected %u.\n",
12541da177e4SLinus Torvalds 					argp->count);
12551da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
12561da177e4SLinus Torvalds 		}
12571da177e4SLinus Torvalds 		/* Can't do anything about it except throw an error. */
12581da177e4SLinus Torvalds 		task->tk_status = -EIO;
12591da177e4SLinus Torvalds 	}
126013602896SFred Isaman 	return;
12611da177e4SLinus Torvalds }
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
126571d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
126671d0a611STrond Myklebust {
1267b8413f98STrond Myklebust 	int ret;
1268b8413f98STrond Myklebust 
126971d0a611STrond Myklebust 	if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
127071d0a611STrond Myklebust 		return 1;
1271b8413f98STrond Myklebust 	if (!may_wait)
127271d0a611STrond Myklebust 		return 0;
1273b8413f98STrond Myklebust 	ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1274b8413f98STrond Myklebust 				NFS_INO_COMMIT,
1275b8413f98STrond Myklebust 				nfs_wait_bit_killable,
1276b8413f98STrond Myklebust 				TASK_KILLABLE);
1277b8413f98STrond Myklebust 	return (ret < 0) ? ret : 1;
127871d0a611STrond Myklebust }
127971d0a611STrond Myklebust 
128071d0a611STrond Myklebust static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
128171d0a611STrond Myklebust {
128271d0a611STrond Myklebust 	clear_bit(NFS_INO_COMMIT, &nfsi->flags);
128371d0a611STrond Myklebust 	smp_mb__after_clear_bit();
128471d0a611STrond Myklebust 	wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
128571d0a611STrond Myklebust }
128671d0a611STrond Myklebust 
128771d0a611STrond Myklebust 
12880aa05887SH Hartley Sweeten static void nfs_commitdata_release(void *data)
12891da177e4SLinus Torvalds {
1290383ba719STrond Myklebust 	struct nfs_write_data *wdata = data;
1291383ba719STrond Myklebust 
1292383ba719STrond Myklebust 	put_nfs_open_context(wdata->args.context);
12931da177e4SLinus Torvalds 	nfs_commit_free(wdata);
12941da177e4SLinus Torvalds }
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds /*
12971da177e4SLinus Torvalds  * Set up the argument/result storage required for the RPC call.
12981da177e4SLinus Torvalds  */
1299dbae4c73STrond Myklebust static int nfs_commit_rpcsetup(struct list_head *head,
1300788e7a89STrond Myklebust 		struct nfs_write_data *data,
1301788e7a89STrond Myklebust 		int how)
13021da177e4SLinus Torvalds {
130384115e1cSTrond Myklebust 	struct nfs_page *first = nfs_list_entry(head->next);
130484115e1cSTrond Myklebust 	struct inode *inode = first->wb_context->path.dentry->d_inode;
13053ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
130607737691STrond Myklebust 	struct rpc_task *task;
1307bdc7f021STrond Myklebust 	struct rpc_message msg = {
1308bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1309bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
1310bdc7f021STrond Myklebust 		.rpc_cred = first->wb_context->cred,
1311bdc7f021STrond Myklebust 	};
131284115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
131307737691STrond Myklebust 		.task = &data->task,
131484115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(inode),
1315bdc7f021STrond Myklebust 		.rpc_message = &msg,
131684115e1cSTrond Myklebust 		.callback_ops = &nfs_commit_ops,
131784115e1cSTrond Myklebust 		.callback_data = data,
1318101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
13192c61be0aSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
13203ff7576dSTrond Myklebust 		.priority = priority,
132184115e1cSTrond Myklebust 	};
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
13241da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds 	list_splice_init(head, &data->pages);
13271da177e4SLinus Torvalds 
13281da177e4SLinus Torvalds 	data->inode	  = inode;
1329bdc7f021STrond Myklebust 	data->cred	  = msg.rpc_cred;
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(data->inode);
13323da28eb1STrond Myklebust 	/* Note: we always request a commit of the entire inode */
13333da28eb1STrond Myklebust 	data->args.offset = 0;
13343da28eb1STrond Myklebust 	data->args.count  = 0;
1335383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(first->wb_context);
13363da28eb1STrond Myklebust 	data->res.count   = 0;
13371da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
13381da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
13390e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
13401da177e4SLinus Torvalds 
1341788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
1342bdc7f021STrond Myklebust 	NFS_PROTO(inode)->commit_setup(data, &msg);
13431da177e4SLinus Torvalds 
1344a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1345bdc7f021STrond Myklebust 
134607737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1347dbae4c73STrond Myklebust 	if (IS_ERR(task))
1348dbae4c73STrond Myklebust 		return PTR_ERR(task);
1349d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1350d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
135107737691STrond Myklebust 	rpc_put_task(task);
1352dbae4c73STrond Myklebust 	return 0;
13531da177e4SLinus Torvalds }
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds /*
13561da177e4SLinus Torvalds  * Commit dirty pages
13571da177e4SLinus Torvalds  */
13581da177e4SLinus Torvalds static int
135940859d7eSChuck Lever nfs_commit_list(struct inode *inode, struct list_head *head, int how)
13601da177e4SLinus Torvalds {
13611da177e4SLinus Torvalds 	struct nfs_write_data	*data;
13621da177e4SLinus Torvalds 	struct nfs_page         *req;
13631da177e4SLinus Torvalds 
1364c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds 	if (!data)
13671da177e4SLinus Torvalds 		goto out_bad;
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds 	/* Set up the argument struct */
1370dbae4c73STrond Myklebust 	return nfs_commit_rpcsetup(head, data, how);
13711da177e4SLinus Torvalds  out_bad:
13721da177e4SLinus Torvalds 	while (!list_empty(head)) {
13731da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
13741da177e4SLinus Torvalds 		nfs_list_remove_request(req);
13751da177e4SLinus Torvalds 		nfs_mark_request_commit(req);
137683715ad5STrond Myklebust 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1377c9e51e41SPeter Zijlstra 		dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1378c9e51e41SPeter Zijlstra 				BDI_RECLAIMABLE);
13799fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
13801da177e4SLinus Torvalds 	}
138171d0a611STrond Myklebust 	nfs_commit_clear_lock(NFS_I(inode));
13821da177e4SLinus Torvalds 	return -ENOMEM;
13831da177e4SLinus Torvalds }
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds /*
13861da177e4SLinus Torvalds  * COMMIT call returned
13871da177e4SLinus Torvalds  */
1388788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
13891da177e4SLinus Torvalds {
1390963d8fe5STrond Myklebust 	struct nfs_write_data	*data = calldata;
13911da177e4SLinus Torvalds 
1392a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
13931da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
13941da177e4SLinus Torvalds 
1395788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1396788e7a89STrond Myklebust 	if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1397788e7a89STrond Myklebust 		return;
1398c9d8f89dSTrond Myklebust }
1399c9d8f89dSTrond Myklebust 
1400c9d8f89dSTrond Myklebust static void nfs_commit_release(void *calldata)
1401c9d8f89dSTrond Myklebust {
1402c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1403c9d8f89dSTrond Myklebust 	struct nfs_page		*req;
1404c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1405788e7a89STrond Myklebust 
14061da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
14071da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
14081da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1409e468bae9STrond Myklebust 		nfs_clear_request_commit(req);
14101da177e4SLinus Torvalds 
141148186c7dSChuck Lever 		dprintk("NFS:       commit (%s/%lld %d@%lld)",
141288be9f99STrond Myklebust 			req->wb_context->path.dentry->d_inode->i_sb->s_id,
141388be9f99STrond Myklebust 			(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
14141da177e4SLinus Torvalds 			req->wb_bytes,
14151da177e4SLinus Torvalds 			(long long)req_offset(req));
1416c9d8f89dSTrond Myklebust 		if (status < 0) {
1417c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
14181da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1419c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
14201da177e4SLinus Torvalds 			goto next;
14211da177e4SLinus Torvalds 		}
14221da177e4SLinus Torvalds 
14231da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
14241da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
14251da177e4SLinus Torvalds 		if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
14261da177e4SLinus Torvalds 			/* We have a match */
14271da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
14281da177e4SLinus Torvalds 			dprintk(" OK\n");
14291da177e4SLinus Torvalds 			goto next;
14301da177e4SLinus Torvalds 		}
14311da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
14321da177e4SLinus Torvalds 		dprintk(" mismatch\n");
14336d884e8fSFred 		nfs_mark_request_dirty(req);
14341da177e4SLinus Torvalds 	next:
14359fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
14361da177e4SLinus Torvalds 	}
143771d0a611STrond Myklebust 	nfs_commit_clear_lock(NFS_I(data->inode));
1438c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
14391da177e4SLinus Torvalds }
1440788e7a89STrond Myklebust 
1441788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
144221d9a851SAndy Adamson #if defined(CONFIG_NFS_V4_1)
144321d9a851SAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
144421d9a851SAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1445788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1446788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1447788e7a89STrond Myklebust };
14481da177e4SLinus Torvalds 
1449b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
14501da177e4SLinus Torvalds {
14511da177e4SLinus Torvalds 	LIST_HEAD(head);
145271d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1453b8413f98STrond Myklebust 	int res;
14541da177e4SLinus Torvalds 
1455b8413f98STrond Myklebust 	res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1456b8413f98STrond Myklebust 	if (res <= 0)
1457c5efa5fcSTrond Myklebust 		goto out_mark_dirty;
1458587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
14593da28eb1STrond Myklebust 	res = nfs_scan_commit(inode, &head, 0, 0);
1460587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
14611da177e4SLinus Torvalds 	if (res) {
14627d46a49fSTrond Myklebust 		int error = nfs_commit_list(inode, &head, how);
14631da177e4SLinus Torvalds 		if (error < 0)
14641da177e4SLinus Torvalds 			return error;
1465b8413f98STrond Myklebust 		if (!may_wait)
1466b8413f98STrond Myklebust 			goto out_mark_dirty;
1467b8413f98STrond Myklebust 		error = wait_on_bit(&NFS_I(inode)->flags,
1468b8413f98STrond Myklebust 				NFS_INO_COMMIT,
146971d0a611STrond Myklebust 				nfs_wait_bit_killable,
147071d0a611STrond Myklebust 				TASK_KILLABLE);
1471b8413f98STrond Myklebust 		if (error < 0)
1472b8413f98STrond Myklebust 			return error;
147371d0a611STrond Myklebust 	} else
147471d0a611STrond Myklebust 		nfs_commit_clear_lock(NFS_I(inode));
1475c5efa5fcSTrond Myklebust 	return res;
1476c5efa5fcSTrond Myklebust 	/* Note: If we exit without ensuring that the commit is complete,
1477c5efa5fcSTrond Myklebust 	 * we must mark the inode as dirty. Otherwise, future calls to
1478c5efa5fcSTrond Myklebust 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1479c5efa5fcSTrond Myklebust 	 * that the data is on the disk.
1480c5efa5fcSTrond Myklebust 	 */
1481c5efa5fcSTrond Myklebust out_mark_dirty:
1482c5efa5fcSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
14831da177e4SLinus Torvalds 	return res;
14841da177e4SLinus Torvalds }
14858fc795f7STrond Myklebust 
14868fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
14878fc795f7STrond Myklebust {
1488420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1489420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1490420e3646STrond Myklebust 	int ret = 0;
14918fc795f7STrond Myklebust 
1492a00dd6c0SJeff Layton 	if (wbc->sync_mode == WB_SYNC_NONE) {
1493a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1494a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1495420e3646STrond Myklebust 		 */
1496a00dd6c0SJeff Layton 		if (nfsi->ncommit <= (nfsi->npages >> 1))
1497420e3646STrond Myklebust 			goto out_mark_dirty;
1498420e3646STrond Myklebust 
1499a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1500420e3646STrond Myklebust 		flags = 0;
1501a00dd6c0SJeff Layton 	}
1502a00dd6c0SJeff Layton 
1503420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1504420e3646STrond Myklebust 	if (ret >= 0) {
1505420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1506420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1507420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1508420e3646STrond Myklebust 			else
1509420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1510420e3646STrond Myklebust 		}
15118fc795f7STrond Myklebust 		return 0;
1512420e3646STrond Myklebust 	}
1513420e3646STrond Myklebust out_mark_dirty:
15148fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
15158fc795f7STrond Myklebust 	return ret;
15168fc795f7STrond Myklebust }
1517c63c7b05STrond Myklebust #else
15188fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
15198fc795f7STrond Myklebust {
15208fc795f7STrond Myklebust 	return 0;
15218fc795f7STrond Myklebust }
15221da177e4SLinus Torvalds #endif
15231da177e4SLinus Torvalds 
15248fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
15258fc795f7STrond Myklebust {
15268fc795f7STrond Myklebust 	return nfs_commit_unstable_pages(inode, wbc);
15278fc795f7STrond Myklebust }
15288fc795f7STrond Myklebust 
1529acdc53b2STrond Myklebust /*
1530acdc53b2STrond Myklebust  * flush the inode to disk.
1531acdc53b2STrond Myklebust  */
1532acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
153334901f70STrond Myklebust {
153434901f70STrond Myklebust 	struct writeback_control wbc = {
153572cb77f4STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
153634901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
1537d7fb1207STrond Myklebust 		.range_start = 0,
1538d7fb1207STrond Myklebust 		.range_end = LLONG_MAX,
153934901f70STrond Myklebust 	};
154034901f70STrond Myklebust 
1541acdc53b2STrond Myklebust 	return sync_inode(inode, &wbc);
15421c75950bSTrond Myklebust }
15431c75950bSTrond Myklebust 
15441b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
15451b3b4a1aSTrond Myklebust {
15461b3b4a1aSTrond Myklebust 	struct nfs_page *req;
15471b3b4a1aSTrond Myklebust 	int ret = 0;
15481b3b4a1aSTrond Myklebust 
15491b3b4a1aSTrond Myklebust 	BUG_ON(!PageLocked(page));
15501b3b4a1aSTrond Myklebust 	for (;;) {
1551ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
15521b3b4a1aSTrond Myklebust 		req = nfs_page_find_request(page);
15531b3b4a1aSTrond Myklebust 		if (req == NULL)
15541b3b4a1aSTrond Myklebust 			break;
15551b3b4a1aSTrond Myklebust 		if (nfs_lock_request_dontget(req)) {
15561b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
15571b3b4a1aSTrond Myklebust 			/*
15581b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
15591b3b4a1aSTrond Myklebust 			 * page as being dirty
15601b3b4a1aSTrond Myklebust 			 */
15611b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
15621b3b4a1aSTrond Myklebust 			nfs_unlock_request(req);
15631b3b4a1aSTrond Myklebust 			break;
15641b3b4a1aSTrond Myklebust 		}
15651b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
1566c9edda71STrond Myklebust 		nfs_release_request(req);
15671b3b4a1aSTrond Myklebust 		if (ret < 0)
1568c988950eSTrond Myklebust 			break;
15691b3b4a1aSTrond Myklebust 	}
15701b3b4a1aSTrond Myklebust 	return ret;
15711b3b4a1aSTrond Myklebust }
15721b3b4a1aSTrond Myklebust 
15731c75950bSTrond Myklebust /*
15741c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
15751c75950bSTrond Myklebust  */
15761c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
15771c75950bSTrond Myklebust {
15787f2f12d9STrond Myklebust 	loff_t range_start = page_offset(page);
15797f2f12d9STrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
15807f2f12d9STrond Myklebust 	struct writeback_control wbc = {
15817f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
15827f2f12d9STrond Myklebust 		.nr_to_write = 0,
15837f2f12d9STrond Myklebust 		.range_start = range_start,
15847f2f12d9STrond Myklebust 		.range_end = range_end,
15857f2f12d9STrond Myklebust 	};
15867f2f12d9STrond Myklebust 	int ret;
15877f2f12d9STrond Myklebust 
15880522f6adSTrond Myklebust 	for (;;) {
1589ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
15907f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
15917f2f12d9STrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
15927f2f12d9STrond Myklebust 			if (ret < 0)
15937f2f12d9STrond Myklebust 				goto out_error;
15940522f6adSTrond Myklebust 			continue;
15957f2f12d9STrond Myklebust 		}
15960522f6adSTrond Myklebust 		if (!PagePrivate(page))
15970522f6adSTrond Myklebust 			break;
15980522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
15997f2f12d9STrond Myklebust 		if (ret < 0)
16007f2f12d9STrond Myklebust 			goto out_error;
16017f2f12d9STrond Myklebust 	}
16027f2f12d9STrond Myklebust 	return 0;
16037f2f12d9STrond Myklebust out_error:
16047f2f12d9STrond Myklebust 	return ret;
16051c75950bSTrond Myklebust }
16061c75950bSTrond Myklebust 
1607074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
1608074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1609074cc1deSTrond Myklebust 		struct page *page)
1610074cc1deSTrond Myklebust {
1611074cc1deSTrond Myklebust 	struct nfs_page *req;
1612074cc1deSTrond Myklebust 	int ret;
1613074cc1deSTrond Myklebust 
1614074cc1deSTrond Myklebust 	nfs_fscache_release_page(page, GFP_KERNEL);
1615074cc1deSTrond Myklebust 
1616cfb506e1STrond Myklebust 	req = nfs_find_and_lock_request(page, false);
1617074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
1618074cc1deSTrond Myklebust 	if (IS_ERR(req))
1619074cc1deSTrond Myklebust 		goto out;
1620074cc1deSTrond Myklebust 
1621074cc1deSTrond Myklebust 	ret = migrate_page(mapping, newpage, page);
1622074cc1deSTrond Myklebust 	if (!req)
1623074cc1deSTrond Myklebust 		goto out;
1624074cc1deSTrond Myklebust 	if (ret)
1625074cc1deSTrond Myklebust 		goto out_unlock;
1626074cc1deSTrond Myklebust 	page_cache_get(newpage);
1627190f38e5STrond Myklebust 	spin_lock(&mapping->host->i_lock);
1628074cc1deSTrond Myklebust 	req->wb_page = newpage;
1629074cc1deSTrond Myklebust 	SetPagePrivate(newpage);
1630190f38e5STrond Myklebust 	set_page_private(newpage, (unsigned long)req);
1631074cc1deSTrond Myklebust 	ClearPagePrivate(page);
1632074cc1deSTrond Myklebust 	set_page_private(page, 0);
1633190f38e5STrond Myklebust 	spin_unlock(&mapping->host->i_lock);
1634074cc1deSTrond Myklebust 	page_cache_release(page);
1635074cc1deSTrond Myklebust out_unlock:
1636074cc1deSTrond Myklebust 	nfs_clear_page_tag_locked(req);
1637074cc1deSTrond Myklebust out:
1638074cc1deSTrond Myklebust 	return ret;
1639074cc1deSTrond Myklebust }
1640074cc1deSTrond Myklebust #endif
1641074cc1deSTrond Myklebust 
1642f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
16431da177e4SLinus Torvalds {
16441da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
16451da177e4SLinus Torvalds 					     sizeof(struct nfs_write_data),
16461da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
164720c2df83SPaul Mundt 					     NULL);
16481da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
16491da177e4SLinus Torvalds 		return -ENOMEM;
16501da177e4SLinus Torvalds 
165193d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
16521da177e4SLinus Torvalds 						     nfs_wdata_cachep);
16531da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
16541da177e4SLinus Torvalds 		return -ENOMEM;
16551da177e4SLinus Torvalds 
165693d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
16571da177e4SLinus Torvalds 						      nfs_wdata_cachep);
16581da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
16591da177e4SLinus Torvalds 		return -ENOMEM;
16601da177e4SLinus Torvalds 
166189a09141SPeter Zijlstra 	/*
166289a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
166389a09141SPeter Zijlstra 	 *
166489a09141SPeter Zijlstra 	 *  64MB:    8192k
166589a09141SPeter Zijlstra 	 * 128MB:   11585k
166689a09141SPeter Zijlstra 	 * 256MB:   16384k
166789a09141SPeter Zijlstra 	 * 512MB:   23170k
166889a09141SPeter Zijlstra 	 *   1GB:   32768k
166989a09141SPeter Zijlstra 	 *   2GB:   46340k
167089a09141SPeter Zijlstra 	 *   4GB:   65536k
167189a09141SPeter Zijlstra 	 *   8GB:   92681k
167289a09141SPeter Zijlstra 	 *  16GB:  131072k
167389a09141SPeter Zijlstra 	 *
167489a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
167589a09141SPeter Zijlstra 	 * Limit the default to 256M
167689a09141SPeter Zijlstra 	 */
167789a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
167889a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
167989a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
168089a09141SPeter Zijlstra 
16811da177e4SLinus Torvalds 	return 0;
16821da177e4SLinus Torvalds }
16831da177e4SLinus Torvalds 
1684266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
16851da177e4SLinus Torvalds {
16861da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
16871da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
16881a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
16891da177e4SLinus Torvalds }
16901da177e4SLinus Torvalds 
1691