xref: /linux/fs/nfs/write.c (revision ef31153786bc1e4304e6b9422cc8b9efef455611)
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 }
62e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
631da177e4SLinus Torvalds 
645e4424afSTrond Myklebust void nfs_commit_free(struct nfs_write_data *p)
651da177e4SLinus Torvalds {
6640859d7eSChuck Lever 	if (p && (p->pagevec != &p->page_array[0]))
6740859d7eSChuck Lever 		kfree(p->pagevec);
681da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
691da177e4SLinus Torvalds }
70e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free);
711da177e4SLinus Torvalds 
728d5658c9STrond Myklebust struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
733feb2d49STrond Myklebust {
74e6b4f8daSChristoph Lameter 	struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
753feb2d49STrond Myklebust 
763feb2d49STrond Myklebust 	if (p) {
773feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
783feb2d49STrond Myklebust 		INIT_LIST_HEAD(&p->pages);
79e9f7bee1STrond Myklebust 		p->npages = pagecount;
800d0b5cb3SChuck Lever 		if (pagecount <= ARRAY_SIZE(p->page_array))
810d0b5cb3SChuck Lever 			p->pagevec = p->page_array;
823feb2d49STrond Myklebust 		else {
830d0b5cb3SChuck Lever 			p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
840d0b5cb3SChuck Lever 			if (!p->pagevec) {
853feb2d49STrond Myklebust 				mempool_free(p, nfs_wdata_mempool);
863feb2d49STrond Myklebust 				p = NULL;
873feb2d49STrond Myklebust 			}
883feb2d49STrond Myklebust 		}
893feb2d49STrond Myklebust 	}
903feb2d49STrond Myklebust 	return p;
913feb2d49STrond Myklebust }
923feb2d49STrond Myklebust 
931ae88b2eSTrond Myklebust void nfs_writedata_free(struct nfs_write_data *p)
943feb2d49STrond Myklebust {
953feb2d49STrond Myklebust 	if (p && (p->pagevec != &p->page_array[0]))
963feb2d49STrond Myklebust 		kfree(p->pagevec);
973feb2d49STrond Myklebust 	mempool_free(p, nfs_wdata_mempool);
983feb2d49STrond Myklebust }
993feb2d49STrond Myklebust 
1001ae88b2eSTrond Myklebust static void nfs_writedata_release(struct nfs_write_data *wdata)
1011da177e4SLinus Torvalds {
1025053aa56SFred Isaman 	put_lseg(wdata->lseg);
103383ba719STrond Myklebust 	put_nfs_open_context(wdata->args.context);
1041da177e4SLinus Torvalds 	nfs_writedata_free(wdata);
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds 
1077b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
1087b159fc1STrond Myklebust {
1097b159fc1STrond Myklebust 	ctx->error = error;
1107b159fc1STrond Myklebust 	smp_wmb();
1117b159fc1STrond Myklebust 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
1127b159fc1STrond Myklebust }
1137b159fc1STrond Myklebust 
114277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request_locked(struct page *page)
115277459d2STrond Myklebust {
116277459d2STrond Myklebust 	struct nfs_page *req = NULL;
117277459d2STrond Myklebust 
118277459d2STrond Myklebust 	if (PagePrivate(page)) {
119277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
120277459d2STrond Myklebust 		if (req != NULL)
121c03b4024STrond Myklebust 			kref_get(&req->wb_kref);
122277459d2STrond Myklebust 	}
123277459d2STrond Myklebust 	return req;
124277459d2STrond Myklebust }
125277459d2STrond Myklebust 
126277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page)
127277459d2STrond Myklebust {
128587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
129277459d2STrond Myklebust 	struct nfs_page *req = NULL;
130277459d2STrond Myklebust 
131587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
132277459d2STrond Myklebust 	req = nfs_page_find_request_locked(page);
133587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
134277459d2STrond Myklebust 	return req;
135277459d2STrond Myklebust }
136277459d2STrond Myklebust 
1371da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1381da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1391da177e4SLinus Torvalds {
1401da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
141a3d01454STrond Myklebust 	loff_t end, i_size;
142a3d01454STrond Myklebust 	pgoff_t end_index;
1431da177e4SLinus Torvalds 
144a3d01454STrond Myklebust 	spin_lock(&inode->i_lock);
145a3d01454STrond Myklebust 	i_size = i_size_read(inode);
146a3d01454STrond Myklebust 	end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
1471da177e4SLinus Torvalds 	if (i_size > 0 && page->index < end_index)
148a3d01454STrond Myklebust 		goto out;
1491da177e4SLinus Torvalds 	end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
1501da177e4SLinus Torvalds 	if (i_size >= end)
151a3d01454STrond Myklebust 		goto out;
1521da177e4SLinus Torvalds 	i_size_write(inode, end);
153a3d01454STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
154a3d01454STrond Myklebust out:
155a3d01454STrond Myklebust 	spin_unlock(&inode->i_lock);
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
158a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
159a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
160a301b777STrond Myklebust {
161a301b777STrond Myklebust 	SetPageError(page);
162a301b777STrond Myklebust 	nfs_zap_mapping(page->mapping->host, page->mapping);
163a301b777STrond Myklebust }
164a301b777STrond Myklebust 
1651da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
1661da177e4SLinus Torvalds  * covers the full page.
1671da177e4SLinus Torvalds  */
1681da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
1691da177e4SLinus Torvalds {
1701da177e4SLinus Torvalds 	if (PageUptodate(page))
1711da177e4SLinus Torvalds 		return;
1721da177e4SLinus Torvalds 	if (base != 0)
1731da177e4SLinus Torvalds 		return;
17449a70f27STrond Myklebust 	if (count != nfs_page_length(page))
1751da177e4SLinus Torvalds 		return;
1761da177e4SLinus Torvalds 	SetPageUptodate(page);
1771da177e4SLinus Torvalds }
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	if (wbc->for_reclaim)
182c63c7b05STrond Myklebust 		return FLUSH_HIGHPRI | FLUSH_STABLE;
183b17621feSWu Fengguang 	if (wbc->for_kupdate || wbc->for_background)
184b31268acSTrond Myklebust 		return FLUSH_LOWPRI | FLUSH_COND_STABLE;
185b31268acSTrond Myklebust 	return FLUSH_COND_STABLE;
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds /*
18989a09141SPeter Zijlstra  * NFS congestion control
19089a09141SPeter Zijlstra  */
19189a09141SPeter Zijlstra 
19289a09141SPeter Zijlstra int nfs_congestion_kb;
19389a09141SPeter Zijlstra 
19489a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
19589a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
19689a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
19789a09141SPeter Zijlstra 
1985a6d41b3STrond Myklebust static int nfs_set_page_writeback(struct page *page)
19989a09141SPeter Zijlstra {
2005a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
2015a6d41b3STrond Myklebust 
2025a6d41b3STrond Myklebust 	if (!ret) {
20389a09141SPeter Zijlstra 		struct inode *inode = page->mapping->host;
20489a09141SPeter Zijlstra 		struct nfs_server *nfss = NFS_SERVER(inode);
20589a09141SPeter Zijlstra 
206a6305ddbSTrond Myklebust 		page_cache_get(page);
207277866a0SPeter Zijlstra 		if (atomic_long_inc_return(&nfss->writeback) >
2088aa7e847SJens Axboe 				NFS_CONGESTION_ON_THRESH) {
2098aa7e847SJens Axboe 			set_bdi_congested(&nfss->backing_dev_info,
2108aa7e847SJens Axboe 						BLK_RW_ASYNC);
2118aa7e847SJens Axboe 		}
21289a09141SPeter Zijlstra 	}
2135a6d41b3STrond Myklebust 	return ret;
21489a09141SPeter Zijlstra }
21589a09141SPeter Zijlstra 
21689a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page)
21789a09141SPeter Zijlstra {
21889a09141SPeter Zijlstra 	struct inode *inode = page->mapping->host;
21989a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
22089a09141SPeter Zijlstra 
22189a09141SPeter Zijlstra 	end_page_writeback(page);
222a6305ddbSTrond Myklebust 	page_cache_release(page);
223c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
2248aa7e847SJens Axboe 		clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
22589a09141SPeter Zijlstra }
22689a09141SPeter Zijlstra 
227cfb506e1STrond Myklebust static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock)
228e261f51fSTrond Myklebust {
229587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
230e261f51fSTrond Myklebust 	struct nfs_page *req;
231e261f51fSTrond Myklebust 	int ret;
232e261f51fSTrond Myklebust 
233587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
234e261f51fSTrond Myklebust 	for (;;) {
235e261f51fSTrond Myklebust 		req = nfs_page_find_request_locked(page);
236074cc1deSTrond Myklebust 		if (req == NULL)
237074cc1deSTrond Myklebust 			break;
238acee478aSTrond Myklebust 		if (nfs_set_page_tag_locked(req))
239e261f51fSTrond Myklebust 			break;
240e261f51fSTrond Myklebust 		/* Note: If we hold the page lock, as is the case in nfs_writepage,
241acee478aSTrond Myklebust 		 *	 then the call to nfs_set_page_tag_locked() will always
242e261f51fSTrond Myklebust 		 *	 succeed provided that someone hasn't already marked the
243e261f51fSTrond Myklebust 		 *	 request as dirty (in which case we don't care).
244e261f51fSTrond Myklebust 		 */
245587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
246cfb506e1STrond Myklebust 		if (!nonblock)
247e261f51fSTrond Myklebust 			ret = nfs_wait_on_request(req);
248cfb506e1STrond Myklebust 		else
249cfb506e1STrond Myklebust 			ret = -EAGAIN;
250e261f51fSTrond Myklebust 		nfs_release_request(req);
251e261f51fSTrond Myklebust 		if (ret != 0)
252074cc1deSTrond Myklebust 			return ERR_PTR(ret);
253587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
254e261f51fSTrond Myklebust 	}
255587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
256074cc1deSTrond Myklebust 	return req;
257612c9384STrond Myklebust }
258074cc1deSTrond Myklebust 
259074cc1deSTrond Myklebust /*
260074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
261074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
262074cc1deSTrond Myklebust  */
263074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
264cfb506e1STrond Myklebust 				struct page *page, bool nonblock)
265074cc1deSTrond Myklebust {
266074cc1deSTrond Myklebust 	struct nfs_page *req;
267074cc1deSTrond Myklebust 	int ret = 0;
268074cc1deSTrond Myklebust 
269cfb506e1STrond Myklebust 	req = nfs_find_and_lock_request(page, nonblock);
270074cc1deSTrond Myklebust 	if (!req)
271074cc1deSTrond Myklebust 		goto out;
272074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
273074cc1deSTrond Myklebust 	if (IS_ERR(req))
274074cc1deSTrond Myklebust 		goto out;
275074cc1deSTrond Myklebust 
276074cc1deSTrond Myklebust 	ret = nfs_set_page_writeback(page);
277074cc1deSTrond Myklebust 	BUG_ON(ret != 0);
278074cc1deSTrond Myklebust 	BUG_ON(test_bit(PG_CLEAN, &req->wb_flags));
279074cc1deSTrond Myklebust 
280f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
281f8512ad0SFred Isaman 		nfs_redirty_request(req);
282074cc1deSTrond Myklebust 		ret = pgio->pg_error;
283f8512ad0SFred Isaman 	}
284074cc1deSTrond Myklebust out:
285074cc1deSTrond Myklebust 	return ret;
286e261f51fSTrond Myklebust }
287e261f51fSTrond Myklebust 
288f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
289f758c885STrond Myklebust {
290f758c885STrond Myklebust 	struct inode *inode = page->mapping->host;
291cfb506e1STrond Myklebust 	int ret;
292f758c885STrond Myklebust 
293f758c885STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
294f758c885STrond Myklebust 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
295f758c885STrond Myklebust 
296f758c885STrond Myklebust 	nfs_pageio_cond_complete(pgio, page->index);
2971b430beeSWu Fengguang 	ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
298cfb506e1STrond Myklebust 	if (ret == -EAGAIN) {
299cfb506e1STrond Myklebust 		redirty_page_for_writepage(wbc, page);
300cfb506e1STrond Myklebust 		ret = 0;
301cfb506e1STrond Myklebust 	}
302cfb506e1STrond Myklebust 	return ret;
303f758c885STrond Myklebust }
304f758c885STrond Myklebust 
305e261f51fSTrond Myklebust /*
3061da177e4SLinus Torvalds  * Write an mmapped page to the server.
3071da177e4SLinus Torvalds  */
3084d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
3091da177e4SLinus Torvalds {
310f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
311e261f51fSTrond Myklebust 	int err;
3121da177e4SLinus Torvalds 
313f758c885STrond Myklebust 	nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
314f758c885STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
315f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
316f758c885STrond Myklebust 	if (err < 0)
3174d770ccfSTrond Myklebust 		return err;
318f758c885STrond Myklebust 	if (pgio.pg_error < 0)
319f758c885STrond Myklebust 		return pgio.pg_error;
320f758c885STrond Myklebust 	return 0;
3214d770ccfSTrond Myklebust }
3224d770ccfSTrond Myklebust 
3234d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
3244d770ccfSTrond Myklebust {
325f758c885STrond Myklebust 	int ret;
3264d770ccfSTrond Myklebust 
327f758c885STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
3281da177e4SLinus Torvalds 	unlock_page(page);
329f758c885STrond Myklebust 	return ret;
330f758c885STrond Myklebust }
331f758c885STrond Myklebust 
332f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
333f758c885STrond Myklebust {
334f758c885STrond Myklebust 	int ret;
335f758c885STrond Myklebust 
336f758c885STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
337f758c885STrond Myklebust 	unlock_page(page);
338f758c885STrond Myklebust 	return ret;
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3421da177e4SLinus Torvalds {
3431da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
34472cb77f4STrond Myklebust 	unsigned long *bitlock = &NFS_I(inode)->flags;
345c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
3461da177e4SLinus Torvalds 	int err;
3471da177e4SLinus Torvalds 
34872cb77f4STrond Myklebust 	/* Stop dirtying of new pages while we sync */
34972cb77f4STrond Myklebust 	err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
35072cb77f4STrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
35172cb77f4STrond Myklebust 	if (err)
35272cb77f4STrond Myklebust 		goto out_err;
35372cb77f4STrond Myklebust 
35491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
35591d5b470SChuck Lever 
356c63c7b05STrond Myklebust 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
357f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
358c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
35972cb77f4STrond Myklebust 
36072cb77f4STrond Myklebust 	clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
36172cb77f4STrond Myklebust 	smp_mb__after_clear_bit();
36272cb77f4STrond Myklebust 	wake_up_bit(bitlock, NFS_INO_FLUSHING);
36372cb77f4STrond Myklebust 
364f758c885STrond Myklebust 	if (err < 0)
36572cb77f4STrond Myklebust 		goto out_err;
36672cb77f4STrond Myklebust 	err = pgio.pg_error;
36772cb77f4STrond Myklebust 	if (err < 0)
36872cb77f4STrond Myklebust 		goto out_err;
369c63c7b05STrond Myklebust 	return 0;
37072cb77f4STrond Myklebust out_err:
37172cb77f4STrond Myklebust 	return err;
3721da177e4SLinus Torvalds }
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds /*
3751da177e4SLinus Torvalds  * Insert a write request into an inode
3761da177e4SLinus Torvalds  */
377e7d39069STrond Myklebust static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
3781da177e4SLinus Torvalds {
3791da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
3801da177e4SLinus Torvalds 	int error;
3811da177e4SLinus Torvalds 
382e7d39069STrond Myklebust 	error = radix_tree_preload(GFP_NOFS);
383e7d39069STrond Myklebust 	if (error != 0)
384e7d39069STrond Myklebust 		goto out;
385e7d39069STrond Myklebust 
386e7d39069STrond Myklebust 	/* Lock the request! */
387e7d39069STrond Myklebust 	nfs_lock_request_dontget(req);
388e7d39069STrond Myklebust 
389e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
3901da177e4SLinus Torvalds 	error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
39127852596SNick Piggin 	BUG_ON(error);
3921da177e4SLinus Torvalds 	if (!nfsi->npages) {
3931da177e4SLinus Torvalds 		igrab(inode);
3941da177e4SLinus Torvalds 		if (nfs_have_delegation(inode, FMODE_WRITE))
3951da177e4SLinus Torvalds 			nfsi->change_attr++;
3961da177e4SLinus Torvalds 	}
3972df485a7STrond Myklebust 	set_bit(PG_MAPPED, &req->wb_flags);
398deb7d638STrond Myklebust 	SetPagePrivate(req->wb_page);
399277459d2STrond Myklebust 	set_page_private(req->wb_page, (unsigned long)req);
4001da177e4SLinus Torvalds 	nfsi->npages++;
401c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
40227852596SNick Piggin 	radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
40327852596SNick Piggin 				NFS_PAGE_TAG_LOCKED);
404e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
405e7d39069STrond Myklebust 	radix_tree_preload_end();
406e7d39069STrond Myklebust out:
407e7d39069STrond Myklebust 	return error;
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds /*
41189a09141SPeter Zijlstra  * Remove a write request from an inode
4121da177e4SLinus Torvalds  */
4131da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
4141da177e4SLinus Torvalds {
41588be9f99STrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
4161da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	BUG_ON (!NFS_WBACK_BUSY(req));
4191da177e4SLinus Torvalds 
420587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
421277459d2STrond Myklebust 	set_page_private(req->wb_page, 0);
422deb7d638STrond Myklebust 	ClearPagePrivate(req->wb_page);
4232df485a7STrond Myklebust 	clear_bit(PG_MAPPED, &req->wb_flags);
4241da177e4SLinus Torvalds 	radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
4251da177e4SLinus Torvalds 	nfsi->npages--;
4261da177e4SLinus Torvalds 	if (!nfsi->npages) {
427587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
4281da177e4SLinus Torvalds 		iput(inode);
4291da177e4SLinus Torvalds 	} else
430587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
4311da177e4SLinus Torvalds 	nfs_release_request(req);
4321da177e4SLinus Torvalds }
4331da177e4SLinus Torvalds 
43461822ab5STrond Myklebust static void
4356d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
43661822ab5STrond Myklebust {
43761822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
438b80c3cb6STrond Myklebust 	__mark_inode_dirty(req->wb_page->mapping->host, I_DIRTY_DATASYNC);
43961822ab5STrond Myklebust }
44061822ab5STrond Myklebust 
4411da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
4421da177e4SLinus Torvalds /*
4431da177e4SLinus Torvalds  * Add a request to the inode's commit list.
4441da177e4SLinus Torvalds  */
4451da177e4SLinus Torvalds static void
446a861a1e1SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg)
4471da177e4SLinus Torvalds {
44888be9f99STrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
4491da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4501da177e4SLinus Torvalds 
451587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
452e468bae9STrond Myklebust 	set_bit(PG_CLEAN, &(req)->wb_flags);
4535c369683STrond Myklebust 	radix_tree_tag_set(&nfsi->nfs_page_tree,
4545c369683STrond Myklebust 			req->wb_index,
4555c369683STrond Myklebust 			NFS_PAGE_TAG_COMMIT);
456ff778d02STrond Myklebust 	nfsi->ncommit++;
457587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
458a861a1e1SFred Isaman 	pnfs_mark_request_commit(req, lseg);
459fd39fc85SChristoph Lameter 	inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
460c9e51e41SPeter Zijlstra 	inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
461a1803044STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
4621da177e4SLinus Torvalds }
4638e821cadSTrond Myklebust 
464e468bae9STrond Myklebust static int
465e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
466e468bae9STrond Myklebust {
467e468bae9STrond Myklebust 	struct page *page = req->wb_page;
468e468bae9STrond Myklebust 
469e468bae9STrond Myklebust 	if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) {
470e468bae9STrond Myklebust 		dec_zone_page_state(page, NR_UNSTABLE_NFS);
471e468bae9STrond Myklebust 		dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
472e468bae9STrond Myklebust 		return 1;
473e468bae9STrond Myklebust 	}
474e468bae9STrond Myklebust 	return 0;
475e468bae9STrond Myklebust }
476e468bae9STrond Myklebust 
4778e821cadSTrond Myklebust static inline
4788e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
4798e821cadSTrond Myklebust {
480465d5243SFred Isaman 	if (data->verf.committed == NFS_DATA_SYNC)
481465d5243SFred Isaman 		return data->lseg == NULL;
482465d5243SFred Isaman 	else
4838e821cadSTrond Myklebust 		return data->verf.committed != NFS_FILE_SYNC;
4848e821cadSTrond Myklebust }
4858e821cadSTrond Myklebust 
4868e821cadSTrond Myklebust static inline
487a861a1e1SFred Isaman int nfs_reschedule_unstable_write(struct nfs_page *req,
488a861a1e1SFred Isaman 				  struct nfs_write_data *data)
4898e821cadSTrond Myklebust {
490e468bae9STrond Myklebust 	if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) {
491a861a1e1SFred Isaman 		nfs_mark_request_commit(req, data->lseg);
4928e821cadSTrond Myklebust 		return 1;
4938e821cadSTrond Myklebust 	}
4948e821cadSTrond Myklebust 	if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
4956d884e8fSFred 		nfs_mark_request_dirty(req);
4968e821cadSTrond Myklebust 		return 1;
4978e821cadSTrond Myklebust 	}
4988e821cadSTrond Myklebust 	return 0;
4998e821cadSTrond Myklebust }
5008e821cadSTrond Myklebust #else
5018e821cadSTrond Myklebust static inline void
502a861a1e1SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg)
5038e821cadSTrond Myklebust {
5048e821cadSTrond Myklebust }
5058e821cadSTrond Myklebust 
506e468bae9STrond Myklebust static inline int
507e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
508e468bae9STrond Myklebust {
509e468bae9STrond Myklebust 	return 0;
510e468bae9STrond Myklebust }
511e468bae9STrond Myklebust 
5128e821cadSTrond Myklebust static inline
5138e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
5148e821cadSTrond Myklebust {
5158e821cadSTrond Myklebust 	return 0;
5168e821cadSTrond Myklebust }
5178e821cadSTrond Myklebust 
5188e821cadSTrond Myklebust static inline
519a861a1e1SFred Isaman int nfs_reschedule_unstable_write(struct nfs_page *req,
520a861a1e1SFred Isaman 				  struct nfs_write_data *data)
5218e821cadSTrond Myklebust {
5228e821cadSTrond Myklebust 	return 0;
5238e821cadSTrond Myklebust }
5241da177e4SLinus Torvalds #endif
5251da177e4SLinus Torvalds 
52647c62564STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
527fb8a1f11STrond Myklebust static int
528fb8a1f11STrond Myklebust nfs_need_commit(struct nfs_inode *nfsi)
529fb8a1f11STrond Myklebust {
530fb8a1f11STrond Myklebust 	return radix_tree_tagged(&nfsi->nfs_page_tree, NFS_PAGE_TAG_COMMIT);
531fb8a1f11STrond Myklebust }
532fb8a1f11STrond Myklebust 
5331da177e4SLinus Torvalds /*
5341da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
5351da177e4SLinus Torvalds  * @inode: NFS inode to scan
5361da177e4SLinus Torvalds  * @dst: destination list
5371da177e4SLinus Torvalds  * @idx_start: lower bound of page->index to scan.
5381da177e4SLinus Torvalds  * @npages: idx_start + npages sets the upper bound to scan.
5391da177e4SLinus Torvalds  *
5401da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
5411da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
5421da177e4SLinus Torvalds  */
5431da177e4SLinus Torvalds static int
544ca52fec1STrond Myklebust nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
5451da177e4SLinus Torvalds {
5461da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
547ff778d02STrond Myklebust 	int ret;
5483da28eb1STrond Myklebust 
549fb8a1f11STrond Myklebust 	if (!nfs_need_commit(nfsi))
550fb8a1f11STrond Myklebust 		return 0;
551fb8a1f11STrond Myklebust 
552ff778d02STrond Myklebust 	ret = nfs_scan_list(nfsi, dst, idx_start, npages, NFS_PAGE_TAG_COMMIT);
553ff778d02STrond Myklebust 	if (ret > 0)
554ff778d02STrond Myklebust 		nfsi->ncommit -= ret;
5552928db1fSTrond Myklebust 	if (nfs_need_commit(NFS_I(inode)))
5562928db1fSTrond Myklebust 		__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
557ff778d02STrond Myklebust 	return ret;
5581da177e4SLinus Torvalds }
559c42de9ddSTrond Myklebust #else
560fb8a1f11STrond Myklebust static inline int nfs_need_commit(struct nfs_inode *nfsi)
561fb8a1f11STrond Myklebust {
562fb8a1f11STrond Myklebust 	return 0;
563fb8a1f11STrond Myklebust }
564fb8a1f11STrond Myklebust 
565ca52fec1STrond Myklebust static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
566c42de9ddSTrond Myklebust {
567c42de9ddSTrond Myklebust 	return 0;
568c42de9ddSTrond Myklebust }
5691da177e4SLinus Torvalds #endif
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds /*
572e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
573e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
5741da177e4SLinus Torvalds  *
575e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
576e7d39069STrond Myklebust  * to disk.
5771da177e4SLinus Torvalds  */
578e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
579e7d39069STrond Myklebust 		struct page *page,
580e7d39069STrond Myklebust 		unsigned int offset,
581e7d39069STrond Myklebust 		unsigned int bytes)
5821da177e4SLinus Torvalds {
583e7d39069STrond Myklebust 	struct nfs_page *req;
584e7d39069STrond Myklebust 	unsigned int rqend;
585e7d39069STrond Myklebust 	unsigned int end;
5861da177e4SLinus Torvalds 	int error;
587277459d2STrond Myklebust 
588e7d39069STrond Myklebust 	if (!PagePrivate(page))
589e7d39069STrond Myklebust 		return NULL;
590e7d39069STrond Myklebust 
591e7d39069STrond Myklebust 	end = offset + bytes;
592e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
593e7d39069STrond Myklebust 
594e7d39069STrond Myklebust 	for (;;) {
595e7d39069STrond Myklebust 		req = nfs_page_find_request_locked(page);
596e7d39069STrond Myklebust 		if (req == NULL)
597e7d39069STrond Myklebust 			goto out_unlock;
598e7d39069STrond Myklebust 
599e7d39069STrond Myklebust 		rqend = req->wb_offset + req->wb_bytes;
600e7d39069STrond Myklebust 		/*
601e7d39069STrond Myklebust 		 * Tell the caller to flush out the request if
602e7d39069STrond Myklebust 		 * the offsets are non-contiguous.
603e7d39069STrond Myklebust 		 * Note: nfs_flush_incompatible() will already
604e7d39069STrond Myklebust 		 * have flushed out requests having wrong owners.
605e7d39069STrond Myklebust 		 */
606e468bae9STrond Myklebust 		if (offset > rqend
607e7d39069STrond Myklebust 		    || end < req->wb_offset)
608e7d39069STrond Myklebust 			goto out_flushme;
609e7d39069STrond Myklebust 
610e7d39069STrond Myklebust 		if (nfs_set_page_tag_locked(req))
611e7d39069STrond Myklebust 			break;
612e7d39069STrond Myklebust 
613e7d39069STrond Myklebust 		/* The request is locked, so wait and then retry */
614587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
6151da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
6161da177e4SLinus Torvalds 		nfs_release_request(req);
617e7d39069STrond Myklebust 		if (error != 0)
618e7d39069STrond Myklebust 			goto out_err;
619e7d39069STrond Myklebust 		spin_lock(&inode->i_lock);
6201da177e4SLinus Torvalds 	}
6211da177e4SLinus Torvalds 
622ff778d02STrond Myklebust 	if (nfs_clear_request_commit(req) &&
623e468bae9STrond Myklebust 	    radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree,
624a861a1e1SFred Isaman 				 req->wb_index, NFS_PAGE_TAG_COMMIT) != NULL) {
625ff778d02STrond Myklebust 		NFS_I(inode)->ncommit--;
626a861a1e1SFred Isaman 		pnfs_clear_request_commit(req);
627a861a1e1SFred Isaman 	}
628e468bae9STrond Myklebust 
6291da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
6301da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
6311da177e4SLinus Torvalds 		req->wb_offset = offset;
6321da177e4SLinus Torvalds 		req->wb_pgbase = offset;
6331da177e4SLinus Torvalds 	}
6341da177e4SLinus Torvalds 	if (end > rqend)
6351da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
636e7d39069STrond Myklebust 	else
637e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
638e7d39069STrond Myklebust out_unlock:
639e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
640e7d39069STrond Myklebust 	return req;
641e7d39069STrond Myklebust out_flushme:
642e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
643e7d39069STrond Myklebust 	nfs_release_request(req);
644e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
645e7d39069STrond Myklebust out_err:
646e7d39069STrond Myklebust 	return ERR_PTR(error);
647e7d39069STrond Myklebust }
6481da177e4SLinus Torvalds 
649e7d39069STrond Myklebust /*
650e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
651e7d39069STrond Myklebust  *
652e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
653e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
654e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
655e7d39069STrond Myklebust  */
656e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
657e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
658e7d39069STrond Myklebust {
659e7d39069STrond Myklebust 	struct inode *inode = page->mapping->host;
660e7d39069STrond Myklebust 	struct nfs_page	*req;
661e7d39069STrond Myklebust 	int error;
662e7d39069STrond Myklebust 
663e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
664e7d39069STrond Myklebust 	if (req != NULL)
665e7d39069STrond Myklebust 		goto out;
666e7d39069STrond Myklebust 	req = nfs_create_request(ctx, inode, page, offset, bytes);
667e7d39069STrond Myklebust 	if (IS_ERR(req))
668e7d39069STrond Myklebust 		goto out;
669e7d39069STrond Myklebust 	error = nfs_inode_add_request(inode, req);
670e7d39069STrond Myklebust 	if (error != 0) {
671e7d39069STrond Myklebust 		nfs_release_request(req);
672e7d39069STrond Myklebust 		req = ERR_PTR(error);
673e7d39069STrond Myklebust 	}
674efc91ed0STrond Myklebust out:
67561e930a9STrond Myklebust 	return req;
6761da177e4SLinus Torvalds }
6771da177e4SLinus Torvalds 
678e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
679e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
680e7d39069STrond Myklebust {
681e7d39069STrond Myklebust 	struct nfs_page	*req;
682e7d39069STrond Myklebust 
683e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
684e7d39069STrond Myklebust 	if (IS_ERR(req))
685e7d39069STrond Myklebust 		return PTR_ERR(req);
686b80c3cb6STrond Myklebust 	nfs_mark_request_dirty(req);
687e7d39069STrond Myklebust 	/* Update file length */
688e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
689e7d39069STrond Myklebust 	nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
690a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
691e7d39069STrond Myklebust 	nfs_clear_page_tag_locked(req);
692e7d39069STrond Myklebust 	return 0;
693e7d39069STrond Myklebust }
694e7d39069STrond Myklebust 
6951da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
6961da177e4SLinus Torvalds {
697cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
6981da177e4SLinus Torvalds 	struct nfs_page	*req;
6991a54533eSTrond Myklebust 	int do_flush, status;
7001da177e4SLinus Torvalds 	/*
7011da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
7021da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
7031da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
7041da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
7051da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
7061da177e4SLinus Torvalds 	 * dropped page.
7071da177e4SLinus Torvalds 	 */
7081a54533eSTrond Myklebust 	do {
709277459d2STrond Myklebust 		req = nfs_page_find_request(page);
7101a54533eSTrond Myklebust 		if (req == NULL)
7111a54533eSTrond Myklebust 			return 0;
712f11ac8dbSTrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx ||
713f11ac8dbSTrond Myklebust 			req->wb_lock_context->lockowner != current->files ||
714f11ac8dbSTrond Myklebust 			req->wb_lock_context->pid != current->tgid;
7151da177e4SLinus Torvalds 		nfs_release_request(req);
7161a54533eSTrond Myklebust 		if (!do_flush)
7171a54533eSTrond Myklebust 			return 0;
718277459d2STrond Myklebust 		status = nfs_wb_page(page->mapping->host, page);
7191a54533eSTrond Myklebust 	} while (status == 0);
7201a54533eSTrond Myklebust 	return status;
7211da177e4SLinus Torvalds }
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds /*
7245d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
7255d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
7265d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
7275d47a356STrond Myklebust  */
7285d47a356STrond Myklebust static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
7295d47a356STrond Myklebust {
7305d47a356STrond Myklebust 	return PageUptodate(page) &&
7315d47a356STrond Myklebust 		!(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
7325d47a356STrond Myklebust }
7335d47a356STrond Myklebust 
7345d47a356STrond Myklebust /*
7351da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
7361da177e4SLinus Torvalds  *
7371da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
7381da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
7391da177e4SLinus Torvalds  */
7401da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
7411da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
7421da177e4SLinus Torvalds {
743cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
7441da177e4SLinus Torvalds 	struct inode	*inode = page->mapping->host;
7451da177e4SLinus Torvalds 	int		status = 0;
7461da177e4SLinus Torvalds 
74791d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
74891d5b470SChuck Lever 
74948186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage(%s/%s %d@%lld)\n",
75001cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
75101cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name, count,
7520bbacc40SChuck Lever 		(long long)(page_offset(page) + offset));
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 	/* If we're not using byte range locks, and we know the page
7555d47a356STrond Myklebust 	 * is up to date, it may be more efficient to extend the write
7565d47a356STrond Myklebust 	 * to cover the entire page in order to avoid fragmentation
7575d47a356STrond Myklebust 	 * inefficiencies.
7581da177e4SLinus Torvalds 	 */
7595d47a356STrond Myklebust 	if (nfs_write_pageuptodate(page, inode) &&
7605d47a356STrond Myklebust 			inode->i_flock == NULL &&
7616b2f3d1fSChristoph Hellwig 			!(file->f_flags & O_DSYNC)) {
76249a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
7631da177e4SLinus Torvalds 		offset = 0;
7641da177e4SLinus Torvalds 	}
7651da177e4SLinus Torvalds 
766e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
76703fa9e84STrond Myklebust 	if (status < 0)
76803fa9e84STrond Myklebust 		nfs_set_pageerror(page);
7691da177e4SLinus Torvalds 
77048186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
7711da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
7721da177e4SLinus Torvalds 	return status;
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
775a861a1e1SFred Isaman static void nfs_writepage_release(struct nfs_page *req,
776a861a1e1SFred Isaman 				  struct nfs_write_data *data)
7771da177e4SLinus Torvalds {
778a6305ddbSTrond Myklebust 	struct page *page = req->wb_page;
7798e821cadSTrond Myklebust 
780a861a1e1SFred Isaman 	if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req, data))
7811da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
7829fd367f0STrond Myklebust 	nfs_clear_page_tag_locked(req);
783a6305ddbSTrond Myklebust 	nfs_end_page_writeback(page);
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds 
7863ff7576dSTrond Myklebust static int flush_task_priority(int how)
7871da177e4SLinus Torvalds {
7881da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
7891da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
7901da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
7911da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
7921da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
7931da177e4SLinus Torvalds 	}
7941da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
7951da177e4SLinus Torvalds }
7961da177e4SLinus Torvalds 
797a69aef14SFred Isaman int nfs_initiate_write(struct nfs_write_data *data,
798d138d5d1SAndy Adamson 		       struct rpc_clnt *clnt,
799788e7a89STrond Myklebust 		       const struct rpc_call_ops *call_ops,
8001da177e4SLinus Torvalds 		       int how)
8011da177e4SLinus Torvalds {
802d138d5d1SAndy Adamson 	struct inode *inode = data->inode;
8033ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
80407737691STrond Myklebust 	struct rpc_task *task;
805bdc7f021STrond Myklebust 	struct rpc_message msg = {
806bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
807bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
808d138d5d1SAndy Adamson 		.rpc_cred = data->cred,
809bdc7f021STrond Myklebust 	};
81084115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
811d138d5d1SAndy Adamson 		.rpc_client = clnt,
81207737691STrond Myklebust 		.task = &data->task,
813bdc7f021STrond Myklebust 		.rpc_message = &msg,
81484115e1cSTrond Myklebust 		.callback_ops = call_ops,
81584115e1cSTrond Myklebust 		.callback_data = data,
816101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
8172c61be0aSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
8183ff7576dSTrond Myklebust 		.priority = priority,
81984115e1cSTrond Myklebust 	};
8202c61be0aSTrond Myklebust 	int ret = 0;
8211da177e4SLinus Torvalds 
822d138d5d1SAndy Adamson 	/* Set up the initial task struct.  */
823d138d5d1SAndy Adamson 	NFS_PROTO(inode)->write_setup(data, &msg);
824d138d5d1SAndy Adamson 
825d138d5d1SAndy Adamson 	dprintk("NFS: %5u initiated write call "
826d138d5d1SAndy Adamson 		"(req %s/%lld, %u bytes @ offset %llu)\n",
827d138d5d1SAndy Adamson 		data->task.tk_pid,
828d138d5d1SAndy Adamson 		inode->i_sb->s_id,
829d138d5d1SAndy Adamson 		(long long)NFS_FILEID(inode),
830d138d5d1SAndy Adamson 		data->args.count,
831d138d5d1SAndy Adamson 		(unsigned long long)data->args.offset);
832d138d5d1SAndy Adamson 
833d138d5d1SAndy Adamson 	task = rpc_run_task(&task_setup_data);
834d138d5d1SAndy Adamson 	if (IS_ERR(task)) {
835d138d5d1SAndy Adamson 		ret = PTR_ERR(task);
836d138d5d1SAndy Adamson 		goto out;
837d138d5d1SAndy Adamson 	}
838d138d5d1SAndy Adamson 	if (how & FLUSH_SYNC) {
839d138d5d1SAndy Adamson 		ret = rpc_wait_for_completion_task(task);
840d138d5d1SAndy Adamson 		if (ret == 0)
841d138d5d1SAndy Adamson 			ret = task->tk_status;
842d138d5d1SAndy Adamson 	}
843d138d5d1SAndy Adamson 	rpc_put_task(task);
844d138d5d1SAndy Adamson out:
845d138d5d1SAndy Adamson 	return ret;
846d138d5d1SAndy Adamson }
847a69aef14SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_write);
848d138d5d1SAndy Adamson 
849d138d5d1SAndy Adamson /*
850d138d5d1SAndy Adamson  * Set up the argument/result storage required for the RPC call.
851d138d5d1SAndy Adamson  */
852d138d5d1SAndy Adamson static int nfs_write_rpcsetup(struct nfs_page *req,
853d138d5d1SAndy Adamson 		struct nfs_write_data *data,
854d138d5d1SAndy Adamson 		const struct rpc_call_ops *call_ops,
855d138d5d1SAndy Adamson 		unsigned int count, unsigned int offset,
8565053aa56SFred Isaman 		struct pnfs_layout_segment *lseg,
857d138d5d1SAndy Adamson 		int how)
858d138d5d1SAndy Adamson {
859d138d5d1SAndy Adamson 	struct inode *inode = req->wb_context->path.dentry->d_inode;
860d138d5d1SAndy Adamson 
8611da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
8621da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	data->req = req;
86588be9f99STrond Myklebust 	data->inode = inode = req->wb_context->path.dentry->d_inode;
866d138d5d1SAndy Adamson 	data->cred = req->wb_context->cred;
8675053aa56SFred Isaman 	data->lseg = get_lseg(lseg);
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(inode);
8701da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
8711da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
8721da177e4SLinus Torvalds 	data->args.pages  = data->pagevec;
8731da177e4SLinus Torvalds 	data->args.count  = count;
874383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
875f11ac8dbSTrond Myklebust 	data->args.lock_context = req->wb_lock_context;
876bdc7f021STrond Myklebust 	data->args.stable  = NFS_UNSTABLE;
877b31268acSTrond Myklebust 	if (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
878bdc7f021STrond Myklebust 		data->args.stable = NFS_DATA_SYNC;
879fb8a1f11STrond Myklebust 		if (!nfs_need_commit(NFS_I(inode)))
880bdc7f021STrond Myklebust 			data->args.stable = NFS_FILE_SYNC;
881bdc7f021STrond Myklebust 	}
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
8841da177e4SLinus Torvalds 	data->res.count   = count;
8851da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
8860e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
8871da177e4SLinus Torvalds 
8880382b744SAndy Adamson 	if (data->lseg &&
8890382b744SAndy Adamson 	    (pnfs_try_to_write_data(data, call_ops, how) == PNFS_ATTEMPTED))
8900382b744SAndy Adamson 		return 0;
8910382b744SAndy Adamson 
892d138d5d1SAndy Adamson 	return nfs_initiate_write(data, NFS_CLIENT(inode), call_ops, how);
8931da177e4SLinus Torvalds }
8941da177e4SLinus Torvalds 
8956d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
8966d884e8fSFred  * call this on each, which will prepare them to be retried on next
8976d884e8fSFred  * writeback using standard nfs.
8986d884e8fSFred  */
8996d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
9006d884e8fSFred {
901a6305ddbSTrond Myklebust 	struct page *page = req->wb_page;
902a6305ddbSTrond Myklebust 
9036d884e8fSFred 	nfs_mark_request_dirty(req);
9046d884e8fSFred 	nfs_clear_page_tag_locked(req);
905a6305ddbSTrond Myklebust 	nfs_end_page_writeback(page);
9066d884e8fSFred }
9076d884e8fSFred 
9081da177e4SLinus Torvalds /*
9091da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
9101da177e4SLinus Torvalds  * contiguous dirty area on one page.
9111da177e4SLinus Torvalds  */
912c76069bdSFred Isaman static int nfs_flush_multi(struct nfs_pageio_descriptor *desc)
9131da177e4SLinus Torvalds {
914c76069bdSFred Isaman 	struct nfs_page *req = nfs_list_entry(desc->pg_list.next);
9151da177e4SLinus Torvalds 	struct page *page = req->wb_page;
9161da177e4SLinus Torvalds 	struct nfs_write_data *data;
917c76069bdSFred Isaman 	size_t wsize = NFS_SERVER(desc->pg_inode)->wsize, nbytes;
918e9f7bee1STrond Myklebust 	unsigned int offset;
9191da177e4SLinus Torvalds 	int requests = 0;
920dbae4c73STrond Myklebust 	int ret = 0;
921c76069bdSFred Isaman 	struct pnfs_layout_segment *lseg;
9221da177e4SLinus Torvalds 	LIST_HEAD(list);
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds 	nfs_list_remove_request(req);
9251da177e4SLinus Torvalds 
926b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
927b31268acSTrond Myklebust 	    (desc->pg_moreio || NFS_I(desc->pg_inode)->ncommit ||
928b31268acSTrond Myklebust 	     desc->pg_count > wsize))
929b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
930b31268acSTrond Myklebust 
931b31268acSTrond Myklebust 
932c76069bdSFred Isaman 	nbytes = desc->pg_count;
933e9f7bee1STrond Myklebust 	do {
934e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
935e9f7bee1STrond Myklebust 
9368d5658c9STrond Myklebust 		data = nfs_writedata_alloc(1);
9371da177e4SLinus Torvalds 		if (!data)
9381da177e4SLinus Torvalds 			goto out_bad;
9391da177e4SLinus Torvalds 		list_add(&data->pages, &list);
9401da177e4SLinus Torvalds 		requests++;
941e9f7bee1STrond Myklebust 		nbytes -= len;
942e9f7bee1STrond Myklebust 	} while (nbytes != 0);
9431da177e4SLinus Torvalds 	atomic_set(&req->wb_complete, requests);
9441da177e4SLinus Torvalds 
945c76069bdSFred Isaman 	BUG_ON(desc->pg_lseg);
946c76069bdSFred Isaman 	lseg = pnfs_update_layout(desc->pg_inode, req->wb_context, IOMODE_RW);
9471da177e4SLinus Torvalds 	ClearPageError(page);
9481da177e4SLinus Torvalds 	offset = 0;
949c76069bdSFred Isaman 	nbytes = desc->pg_count;
9501da177e4SLinus Torvalds 	do {
951dbae4c73STrond Myklebust 		int ret2;
952dbae4c73STrond Myklebust 
9531da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
9541da177e4SLinus Torvalds 		list_del_init(&data->pages);
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 		data->pagevec[0] = page;
9571da177e4SLinus Torvalds 
958bcb71bbaSTrond Myklebust 		if (nbytes < wsize)
959bcb71bbaSTrond Myklebust 			wsize = nbytes;
960dbae4c73STrond Myklebust 		ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
961c76069bdSFred Isaman 					  wsize, offset, lseg, desc->pg_ioflags);
962dbae4c73STrond Myklebust 		if (ret == 0)
963dbae4c73STrond Myklebust 			ret = ret2;
9641da177e4SLinus Torvalds 		offset += wsize;
9651da177e4SLinus Torvalds 		nbytes -= wsize;
9661da177e4SLinus Torvalds 	} while (nbytes != 0);
9671da177e4SLinus Torvalds 
96844b83799SFred Isaman 	put_lseg(lseg);
96936fe432dSFred Isaman 	desc->pg_lseg = NULL;
970dbae4c73STrond Myklebust 	return ret;
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds out_bad:
9731da177e4SLinus Torvalds 	while (!list_empty(&list)) {
9741da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
9751da177e4SLinus Torvalds 		list_del(&data->pages);
9760da2a4acSFred Isaman 		nfs_writedata_free(data);
9771da177e4SLinus Torvalds 	}
97861822ab5STrond Myklebust 	nfs_redirty_request(req);
9791da177e4SLinus Torvalds 	return -ENOMEM;
9801da177e4SLinus Torvalds }
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds /*
9831da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
9841da177e4SLinus Torvalds  * The page must have been locked by the caller.
9851da177e4SLinus Torvalds  *
9861da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
9871da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
9881da177e4SLinus Torvalds  * that has been written but not committed.
9891da177e4SLinus Torvalds  */
990c76069bdSFred Isaman static int nfs_flush_one(struct nfs_pageio_descriptor *desc)
9911da177e4SLinus Torvalds {
9921da177e4SLinus Torvalds 	struct nfs_page		*req;
9931da177e4SLinus Torvalds 	struct page		**pages;
9941da177e4SLinus Torvalds 	struct nfs_write_data	*data;
995c76069bdSFred Isaman 	struct list_head *head = &desc->pg_list;
996c76069bdSFred Isaman 	struct pnfs_layout_segment *lseg = desc->pg_lseg;
99744b83799SFred Isaman 	int ret;
9981da177e4SLinus Torvalds 
999c76069bdSFred Isaman 	data = nfs_writedata_alloc(nfs_page_array_len(desc->pg_base,
1000c76069bdSFred Isaman 						      desc->pg_count));
100144b83799SFred Isaman 	if (!data) {
100244b83799SFred Isaman 		while (!list_empty(head)) {
100344b83799SFred Isaman 			req = nfs_list_entry(head->next);
100444b83799SFred Isaman 			nfs_list_remove_request(req);
100544b83799SFred Isaman 			nfs_redirty_request(req);
100644b83799SFred Isaman 		}
100744b83799SFred Isaman 		ret = -ENOMEM;
100844b83799SFred Isaman 		goto out;
100944b83799SFred Isaman 	}
10101da177e4SLinus Torvalds 	pages = data->pagevec;
10111da177e4SLinus Torvalds 	while (!list_empty(head)) {
10121da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
10131da177e4SLinus Torvalds 		nfs_list_remove_request(req);
10141da177e4SLinus Torvalds 		nfs_list_add_request(req, &data->pages);
10151da177e4SLinus Torvalds 		ClearPageError(req->wb_page);
10161da177e4SLinus Torvalds 		*pages++ = req->wb_page;
10171da177e4SLinus Torvalds 	}
10181da177e4SLinus Torvalds 	req = nfs_list_entry(data->pages.next);
101944b83799SFred Isaman 	if ((!lseg) && list_is_singular(&data->pages))
1020c76069bdSFred Isaman 		lseg = pnfs_update_layout(desc->pg_inode, req->wb_context, IOMODE_RW);
10211da177e4SLinus Torvalds 
1022b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1023b31268acSTrond Myklebust 	    (desc->pg_moreio || NFS_I(desc->pg_inode)->ncommit))
1024b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1025b31268acSTrond Myklebust 
10261da177e4SLinus Torvalds 	/* Set up the argument struct */
1027c76069bdSFred Isaman 	ret = nfs_write_rpcsetup(req, data, &nfs_write_full_ops, desc->pg_count, 0, lseg, desc->pg_ioflags);
102844b83799SFred Isaman out:
102944b83799SFred Isaman 	put_lseg(lseg); /* Cleans any gotten in ->pg_test */
103036fe432dSFred Isaman 	desc->pg_lseg = NULL;
103144b83799SFred Isaman 	return ret;
10321da177e4SLinus Torvalds }
10331da177e4SLinus Torvalds 
1034c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1035c63c7b05STrond Myklebust 				  struct inode *inode, int ioflags)
10361da177e4SLinus Torvalds {
1037bf4285e7SChuck Lever 	size_t wsize = NFS_SERVER(inode)->wsize;
10381da177e4SLinus Torvalds 
103944b83799SFred Isaman 	pnfs_pageio_init_write(pgio, inode);
104094ad1c80SFred Isaman 
1041bcb71bbaSTrond Myklebust 	if (wsize < PAGE_CACHE_SIZE)
1042c63c7b05STrond Myklebust 		nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
1043bcb71bbaSTrond Myklebust 	else
1044c63c7b05STrond Myklebust 		nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
10451da177e4SLinus Torvalds }
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds /*
10481da177e4SLinus Torvalds  * Handle a write reply that flushed part of a page.
10491da177e4SLinus Torvalds  */
1050788e7a89STrond Myklebust static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
10511da177e4SLinus Torvalds {
1052788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
10531da177e4SLinus Torvalds 
105448186c7dSChuck Lever 	dprintk("NFS: %5u write(%s/%lld %d@%lld)",
105548186c7dSChuck Lever 		task->tk_pid,
105648186c7dSChuck Lever 		data->req->wb_context->path.dentry->d_inode->i_sb->s_id,
105748186c7dSChuck Lever 		(long long)
105848186c7dSChuck Lever 		  NFS_FILEID(data->req->wb_context->path.dentry->d_inode),
105948186c7dSChuck Lever 		data->req->wb_bytes, (long long)req_offset(data->req));
10601da177e4SLinus Torvalds 
1061c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
1062c9d8f89dSTrond Myklebust }
1063788e7a89STrond Myklebust 
1064c9d8f89dSTrond Myklebust static void nfs_writeback_release_partial(void *calldata)
1065c9d8f89dSTrond Myklebust {
1066c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1067c9d8f89dSTrond Myklebust 	struct nfs_page		*req = data->req;
1068c9d8f89dSTrond Myklebust 	struct page		*page = req->wb_page;
1069c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1070c9d8f89dSTrond Myklebust 
1071c9d8f89dSTrond Myklebust 	if (status < 0) {
1072a301b777STrond Myklebust 		nfs_set_pageerror(page);
1073c9d8f89dSTrond Myklebust 		nfs_context_set_write_error(req->wb_context, status);
1074c9d8f89dSTrond Myklebust 		dprintk(", error = %d\n", status);
10758e821cadSTrond Myklebust 		goto out;
10768e821cadSTrond Myklebust 	}
10778e821cadSTrond Myklebust 
10788e821cadSTrond Myklebust 	if (nfs_write_need_commit(data)) {
1079587142f8STrond Myklebust 		struct inode *inode = page->mapping->host;
10808e821cadSTrond Myklebust 
1081587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
10828e821cadSTrond Myklebust 		if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
10838e821cadSTrond Myklebust 			/* Do nothing we need to resend the writes */
10848e821cadSTrond Myklebust 		} else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
10851da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
10861da177e4SLinus Torvalds 			dprintk(" defer commit\n");
10871da177e4SLinus Torvalds 		} else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
10888e821cadSTrond Myklebust 			set_bit(PG_NEED_RESCHED, &req->wb_flags);
10898e821cadSTrond Myklebust 			clear_bit(PG_NEED_COMMIT, &req->wb_flags);
10901da177e4SLinus Torvalds 			dprintk(" server reboot detected\n");
10911da177e4SLinus Torvalds 		}
1092587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
10931da177e4SLinus Torvalds 	} else
10941da177e4SLinus Torvalds 		dprintk(" OK\n");
10951da177e4SLinus Torvalds 
10968e821cadSTrond Myklebust out:
10971da177e4SLinus Torvalds 	if (atomic_dec_and_test(&req->wb_complete))
1098a861a1e1SFred Isaman 		nfs_writepage_release(req, data);
1099c9d8f89dSTrond Myklebust 	nfs_writedata_release(calldata);
11001da177e4SLinus Torvalds }
11011da177e4SLinus Torvalds 
1102def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1)
1103def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata)
1104def6ed7eSAndy Adamson {
1105def6ed7eSAndy Adamson 	struct nfs_write_data *data = calldata;
1106def6ed7eSAndy Adamson 
1107035168abSTrond Myklebust 	if (nfs4_setup_sequence(NFS_SERVER(data->inode),
1108035168abSTrond Myklebust 				&data->args.seq_args,
1109def6ed7eSAndy Adamson 				&data->res.seq_res, 1, task))
1110def6ed7eSAndy Adamson 		return;
1111def6ed7eSAndy Adamson 	rpc_call_start(task);
1112def6ed7eSAndy Adamson }
1113def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1114def6ed7eSAndy Adamson 
1115788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops = {
1116def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1)
1117def6ed7eSAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
1118def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1119788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_partial,
1120c9d8f89dSTrond Myklebust 	.rpc_release = nfs_writeback_release_partial,
1121788e7a89STrond Myklebust };
1122788e7a89STrond Myklebust 
11231da177e4SLinus Torvalds /*
11241da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
11251da177e4SLinus Torvalds  *
11261da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
11271da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
11281da177e4SLinus Torvalds  *	  as the page has a write request pending.
11291da177e4SLinus Torvalds  */
1130788e7a89STrond Myklebust static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
11311da177e4SLinus Torvalds {
1132788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
11331da177e4SLinus Torvalds 
1134c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
1135c9d8f89dSTrond Myklebust }
1136c9d8f89dSTrond Myklebust 
1137c9d8f89dSTrond Myklebust static void nfs_writeback_release_full(void *calldata)
1138c9d8f89dSTrond Myklebust {
1139c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1140c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1141788e7a89STrond Myklebust 
11421da177e4SLinus Torvalds 	/* Update attributes as result of writeback. */
11431da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
1144c9d8f89dSTrond Myklebust 		struct nfs_page *req = nfs_list_entry(data->pages.next);
1145c9d8f89dSTrond Myklebust 		struct page *page = req->wb_page;
1146c9d8f89dSTrond Myklebust 
11471da177e4SLinus Torvalds 		nfs_list_remove_request(req);
11481da177e4SLinus Torvalds 
114948186c7dSChuck Lever 		dprintk("NFS: %5u write (%s/%lld %d@%lld)",
115048186c7dSChuck Lever 			data->task.tk_pid,
115188be9f99STrond Myklebust 			req->wb_context->path.dentry->d_inode->i_sb->s_id,
115288be9f99STrond Myklebust 			(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
11531da177e4SLinus Torvalds 			req->wb_bytes,
11541da177e4SLinus Torvalds 			(long long)req_offset(req));
11551da177e4SLinus Torvalds 
1156c9d8f89dSTrond Myklebust 		if (status < 0) {
1157a301b777STrond Myklebust 			nfs_set_pageerror(page);
1158c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
1159c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
11608e821cadSTrond Myklebust 			goto remove_request;
11611da177e4SLinus Torvalds 		}
11621da177e4SLinus Torvalds 
11638e821cadSTrond Myklebust 		if (nfs_write_need_commit(data)) {
11641da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1165a861a1e1SFred Isaman 			nfs_mark_request_commit(req, data->lseg);
11661da177e4SLinus Torvalds 			dprintk(" marked for commit\n");
11678e821cadSTrond Myklebust 			goto next;
11688e821cadSTrond Myklebust 		}
11698e821cadSTrond Myklebust 		dprintk(" OK\n");
11708e821cadSTrond Myklebust remove_request:
11711da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
11721da177e4SLinus Torvalds 	next:
11739fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
1174a6305ddbSTrond Myklebust 		nfs_end_page_writeback(page);
11751da177e4SLinus Torvalds 	}
1176c9d8f89dSTrond Myklebust 	nfs_writedata_release(calldata);
11771da177e4SLinus Torvalds }
11781da177e4SLinus Torvalds 
1179788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops = {
1180def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1)
1181def6ed7eSAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
1182def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1183788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_full,
1184c9d8f89dSTrond Myklebust 	.rpc_release = nfs_writeback_release_full,
1185788e7a89STrond Myklebust };
1186788e7a89STrond Myklebust 
1187788e7a89STrond Myklebust 
11881da177e4SLinus Torvalds /*
11891da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
11901da177e4SLinus Torvalds  */
119113602896SFred Isaman void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
11921da177e4SLinus Torvalds {
11931da177e4SLinus Torvalds 	struct nfs_writeargs	*argp = &data->args;
11941da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
1195eedc020eSAndy Adamson 	struct nfs_server	*server = NFS_SERVER(data->inode);
1196788e7a89STrond Myklebust 	int status;
11971da177e4SLinus Torvalds 
1198a3f565b1SChuck Lever 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
11991da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
12001da177e4SLinus Torvalds 
1201f551e44fSChuck Lever 	/*
1202f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1203f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1204f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1205f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1206f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1207f551e44fSChuck Lever 	 */
1208788e7a89STrond Myklebust 	status = NFS_PROTO(data->inode)->write_done(task, data);
1209788e7a89STrond Myklebust 	if (status != 0)
121013602896SFred Isaman 		return;
121191d5b470SChuck Lever 	nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
121291d5b470SChuck Lever 
12131da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
12141da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
12151da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
12161da177e4SLinus Torvalds 		 * commit data to stable storage even though we
12171da177e4SLinus Torvalds 		 * requested it.
12181da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
12191da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
12201da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
12211da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
12221da177e4SLinus Torvalds 		 */
12231da177e4SLinus Torvalds 		static unsigned long    complain;
12241da177e4SLinus Torvalds 
1225a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
12261da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
12271da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
12281da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1229eedc020eSAndy Adamson 				server->nfs_client->cl_hostname,
12301da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
12311da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
12321da177e4SLinus Torvalds 		}
12331da177e4SLinus Torvalds 	}
12341da177e4SLinus Torvalds #endif
12351da177e4SLinus Torvalds 	/* Is this a short write? */
12361da177e4SLinus Torvalds 	if (task->tk_status >= 0 && resp->count < argp->count) {
12371da177e4SLinus Torvalds 		static unsigned long    complain;
12381da177e4SLinus Torvalds 
123991d5b470SChuck Lever 		nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
124091d5b470SChuck Lever 
12411da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
12421da177e4SLinus Torvalds 		if (resp->count != 0) {
12431da177e4SLinus Torvalds 			/* Was this an NFSv2 write or an NFSv3 stable write? */
12441da177e4SLinus Torvalds 			if (resp->verf->committed != NFS_UNSTABLE) {
12451da177e4SLinus Torvalds 				/* Resend from where the server left off */
1246a69aef14SFred Isaman 				data->mds_offset += resp->count;
12471da177e4SLinus Torvalds 				argp->offset += resp->count;
12481da177e4SLinus Torvalds 				argp->pgbase += resp->count;
12491da177e4SLinus Torvalds 				argp->count -= resp->count;
12501da177e4SLinus Torvalds 			} else {
12511da177e4SLinus Torvalds 				/* Resend as a stable write in order to avoid
12521da177e4SLinus Torvalds 				 * headaches in the case of a server crash.
12531da177e4SLinus Torvalds 				 */
12541da177e4SLinus Torvalds 				argp->stable = NFS_FILE_SYNC;
12551da177e4SLinus Torvalds 			}
12560110ee15STrond Myklebust 			nfs_restart_rpc(task, server->nfs_client);
125713602896SFred Isaman 			return;
12581da177e4SLinus Torvalds 		}
12591da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
12601da177e4SLinus Torvalds 			printk(KERN_WARNING
12611da177e4SLinus Torvalds 			       "NFS: Server wrote zero bytes, expected %u.\n",
12621da177e4SLinus Torvalds 					argp->count);
12631da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
12641da177e4SLinus Torvalds 		}
12651da177e4SLinus Torvalds 		/* Can't do anything about it except throw an error. */
12661da177e4SLinus Torvalds 		task->tk_status = -EIO;
12671da177e4SLinus Torvalds 	}
126813602896SFred Isaman 	return;
12691da177e4SLinus Torvalds }
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
127371d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
127471d0a611STrond Myklebust {
1275b8413f98STrond Myklebust 	int ret;
1276b8413f98STrond Myklebust 
127771d0a611STrond Myklebust 	if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
127871d0a611STrond Myklebust 		return 1;
1279b8413f98STrond Myklebust 	if (!may_wait)
128071d0a611STrond Myklebust 		return 0;
1281b8413f98STrond Myklebust 	ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1282b8413f98STrond Myklebust 				NFS_INO_COMMIT,
1283b8413f98STrond Myklebust 				nfs_wait_bit_killable,
1284b8413f98STrond Myklebust 				TASK_KILLABLE);
1285b8413f98STrond Myklebust 	return (ret < 0) ? ret : 1;
128671d0a611STrond Myklebust }
128771d0a611STrond Myklebust 
1288e0c2b380SFred Isaman void nfs_commit_clear_lock(struct nfs_inode *nfsi)
128971d0a611STrond Myklebust {
129071d0a611STrond Myklebust 	clear_bit(NFS_INO_COMMIT, &nfsi->flags);
129171d0a611STrond Myklebust 	smp_mb__after_clear_bit();
129271d0a611STrond Myklebust 	wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
129371d0a611STrond Myklebust }
1294e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_clear_lock);
129571d0a611STrond Myklebust 
1296e0c2b380SFred Isaman void nfs_commitdata_release(void *data)
12971da177e4SLinus Torvalds {
1298383ba719STrond Myklebust 	struct nfs_write_data *wdata = data;
1299383ba719STrond Myklebust 
1300988b6dceSFred Isaman 	put_lseg(wdata->lseg);
1301383ba719STrond Myklebust 	put_nfs_open_context(wdata->args.context);
13021da177e4SLinus Torvalds 	nfs_commit_free(wdata);
13031da177e4SLinus Torvalds }
1304e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
13051da177e4SLinus Torvalds 
1306e0c2b380SFred Isaman int nfs_initiate_commit(struct nfs_write_data *data, struct rpc_clnt *clnt,
13079ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
1308788e7a89STrond Myklebust 			int how)
13091da177e4SLinus Torvalds {
131007737691STrond Myklebust 	struct rpc_task *task;
13119ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1312bdc7f021STrond Myklebust 	struct rpc_message msg = {
1313bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1314bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
13159ace33cdSFred Isaman 		.rpc_cred = data->cred,
1316bdc7f021STrond Myklebust 	};
131784115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
131807737691STrond Myklebust 		.task = &data->task,
13199ace33cdSFred Isaman 		.rpc_client = clnt,
1320bdc7f021STrond Myklebust 		.rpc_message = &msg,
13219ace33cdSFred Isaman 		.callback_ops = call_ops,
132284115e1cSTrond Myklebust 		.callback_data = data,
1323101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
13242c61be0aSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
13253ff7576dSTrond Myklebust 		.priority = priority,
132684115e1cSTrond Myklebust 	};
1327788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
13289ace33cdSFred Isaman 	NFS_PROTO(data->inode)->commit_setup(data, &msg);
13291da177e4SLinus Torvalds 
1330a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1331bdc7f021STrond Myklebust 
133207737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1333dbae4c73STrond Myklebust 	if (IS_ERR(task))
1334dbae4c73STrond Myklebust 		return PTR_ERR(task);
1335d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1336d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
133707737691STrond Myklebust 	rpc_put_task(task);
1338dbae4c73STrond Myklebust 	return 0;
13391da177e4SLinus Torvalds }
1340e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds /*
13439ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
13449ace33cdSFred Isaman  */
1345e0c2b380SFred Isaman void nfs_init_commit(struct nfs_write_data *data,
1346988b6dceSFred Isaman 			    struct list_head *head,
1347988b6dceSFred Isaman 			    struct pnfs_layout_segment *lseg)
13489ace33cdSFred Isaman {
13499ace33cdSFred Isaman 	struct nfs_page *first = nfs_list_entry(head->next);
13509ace33cdSFred Isaman 	struct inode *inode = first->wb_context->path.dentry->d_inode;
13519ace33cdSFred Isaman 
13529ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
13539ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
13549ace33cdSFred Isaman 
13559ace33cdSFred Isaman 	list_splice_init(head, &data->pages);
13569ace33cdSFred Isaman 
13579ace33cdSFred Isaman 	data->inode	  = inode;
13589ace33cdSFred Isaman 	data->cred	  = first->wb_context->cred;
1359988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
13609ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
13619ace33cdSFred Isaman 
13629ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
13639ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
13649ace33cdSFred Isaman 	data->args.offset = 0;
13659ace33cdSFred Isaman 	data->args.count  = 0;
13669ace33cdSFred Isaman 	data->args.context = get_nfs_open_context(first->wb_context);
13679ace33cdSFred Isaman 	data->res.count   = 0;
13689ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
13699ace33cdSFred Isaman 	data->res.verf    = &data->verf;
13709ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
13719ace33cdSFred Isaman }
1372e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
13739ace33cdSFred Isaman 
1374e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1375a861a1e1SFred Isaman 		      struct pnfs_layout_segment *lseg)
137664bfeb49SFred Isaman {
137764bfeb49SFred Isaman 	struct nfs_page *req;
137864bfeb49SFred Isaman 
137964bfeb49SFred Isaman 	while (!list_empty(page_list)) {
138064bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
138164bfeb49SFred Isaman 		nfs_list_remove_request(req);
1382a861a1e1SFred Isaman 		nfs_mark_request_commit(req, lseg);
138364bfeb49SFred Isaman 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
138464bfeb49SFred Isaman 		dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
138564bfeb49SFred Isaman 			     BDI_RECLAIMABLE);
138664bfeb49SFred Isaman 		nfs_clear_page_tag_locked(req);
138764bfeb49SFred Isaman 	}
138864bfeb49SFred Isaman }
1389e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
139064bfeb49SFred Isaman 
13919ace33cdSFred Isaman /*
13921da177e4SLinus Torvalds  * Commit dirty pages
13931da177e4SLinus Torvalds  */
13941da177e4SLinus Torvalds static int
139540859d7eSChuck Lever nfs_commit_list(struct inode *inode, struct list_head *head, int how)
13961da177e4SLinus Torvalds {
13971da177e4SLinus Torvalds 	struct nfs_write_data	*data;
13981da177e4SLinus Torvalds 
1399c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 	if (!data)
14021da177e4SLinus Torvalds 		goto out_bad;
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds 	/* Set up the argument struct */
1405988b6dceSFred Isaman 	nfs_init_commit(data, head, NULL);
14069ace33cdSFred Isaman 	return nfs_initiate_commit(data, NFS_CLIENT(inode), data->mds_ops, how);
14071da177e4SLinus Torvalds  out_bad:
1408a861a1e1SFred Isaman 	nfs_retry_commit(head, NULL);
140971d0a611STrond Myklebust 	nfs_commit_clear_lock(NFS_I(inode));
14101da177e4SLinus Torvalds 	return -ENOMEM;
14111da177e4SLinus Torvalds }
14121da177e4SLinus Torvalds 
14131da177e4SLinus Torvalds /*
14141da177e4SLinus Torvalds  * COMMIT call returned
14151da177e4SLinus Torvalds  */
1416788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
14171da177e4SLinus Torvalds {
1418963d8fe5STrond Myklebust 	struct nfs_write_data	*data = calldata;
14191da177e4SLinus Torvalds 
1420a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
14211da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
14221da177e4SLinus Torvalds 
1423788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1424788e7a89STrond Myklebust 	if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1425788e7a89STrond Myklebust 		return;
1426c9d8f89dSTrond Myklebust }
1427c9d8f89dSTrond Myklebust 
1428e0c2b380SFred Isaman void nfs_commit_release_pages(struct nfs_write_data *data)
1429c9d8f89dSTrond Myklebust {
1430c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1431c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1432788e7a89STrond Myklebust 
14331da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
14341da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
14351da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1436e468bae9STrond Myklebust 		nfs_clear_request_commit(req);
14371da177e4SLinus Torvalds 
143848186c7dSChuck Lever 		dprintk("NFS:       commit (%s/%lld %d@%lld)",
143988be9f99STrond Myklebust 			req->wb_context->path.dentry->d_inode->i_sb->s_id,
144088be9f99STrond Myklebust 			(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
14411da177e4SLinus Torvalds 			req->wb_bytes,
14421da177e4SLinus Torvalds 			(long long)req_offset(req));
1443c9d8f89dSTrond Myklebust 		if (status < 0) {
1444c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
14451da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1446c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
14471da177e4SLinus Torvalds 			goto next;
14481da177e4SLinus Torvalds 		}
14491da177e4SLinus Torvalds 
14501da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
14511da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
14521da177e4SLinus Torvalds 		if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
14531da177e4SLinus Torvalds 			/* We have a match */
14541da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
14551da177e4SLinus Torvalds 			dprintk(" OK\n");
14561da177e4SLinus Torvalds 			goto next;
14571da177e4SLinus Torvalds 		}
14581da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
14591da177e4SLinus Torvalds 		dprintk(" mismatch\n");
14606d884e8fSFred 		nfs_mark_request_dirty(req);
14611da177e4SLinus Torvalds 	next:
14629fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
14631da177e4SLinus Torvalds 	}
14645917ce84SFred Isaman }
1465e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_release_pages);
14665917ce84SFred Isaman 
14675917ce84SFred Isaman static void nfs_commit_release(void *calldata)
14685917ce84SFred Isaman {
14695917ce84SFred Isaman 	struct nfs_write_data *data = calldata;
14705917ce84SFred Isaman 
14715917ce84SFred Isaman 	nfs_commit_release_pages(data);
147271d0a611STrond Myklebust 	nfs_commit_clear_lock(NFS_I(data->inode));
1473c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
14741da177e4SLinus Torvalds }
1475788e7a89STrond Myklebust 
1476788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
147721d9a851SAndy Adamson #if defined(CONFIG_NFS_V4_1)
147821d9a851SAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
147921d9a851SAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1480788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1481788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1482788e7a89STrond Myklebust };
14831da177e4SLinus Torvalds 
1484b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
14851da177e4SLinus Torvalds {
14861da177e4SLinus Torvalds 	LIST_HEAD(head);
148771d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1488b8413f98STrond Myklebust 	int res;
14891da177e4SLinus Torvalds 
1490b8413f98STrond Myklebust 	res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1491b8413f98STrond Myklebust 	if (res <= 0)
1492c5efa5fcSTrond Myklebust 		goto out_mark_dirty;
1493587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
14943da28eb1STrond Myklebust 	res = nfs_scan_commit(inode, &head, 0, 0);
1495587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
14961da177e4SLinus Torvalds 	if (res) {
1497a861a1e1SFred Isaman 		int error;
1498a861a1e1SFred Isaman 
1499a861a1e1SFred Isaman 		error = pnfs_commit_list(inode, &head, how);
1500a861a1e1SFred Isaman 		if (error == PNFS_NOT_ATTEMPTED)
1501a861a1e1SFred Isaman 			error = nfs_commit_list(inode, &head, how);
15021da177e4SLinus Torvalds 		if (error < 0)
15031da177e4SLinus Torvalds 			return error;
1504b8413f98STrond Myklebust 		if (!may_wait)
1505b8413f98STrond Myklebust 			goto out_mark_dirty;
1506b8413f98STrond Myklebust 		error = wait_on_bit(&NFS_I(inode)->flags,
1507b8413f98STrond Myklebust 				NFS_INO_COMMIT,
150871d0a611STrond Myklebust 				nfs_wait_bit_killable,
150971d0a611STrond Myklebust 				TASK_KILLABLE);
1510b8413f98STrond Myklebust 		if (error < 0)
1511b8413f98STrond Myklebust 			return error;
151271d0a611STrond Myklebust 	} else
151371d0a611STrond Myklebust 		nfs_commit_clear_lock(NFS_I(inode));
1514c5efa5fcSTrond Myklebust 	return res;
1515c5efa5fcSTrond Myklebust 	/* Note: If we exit without ensuring that the commit is complete,
1516c5efa5fcSTrond Myklebust 	 * we must mark the inode as dirty. Otherwise, future calls to
1517c5efa5fcSTrond Myklebust 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1518c5efa5fcSTrond Myklebust 	 * that the data is on the disk.
1519c5efa5fcSTrond Myklebust 	 */
1520c5efa5fcSTrond Myklebust out_mark_dirty:
1521c5efa5fcSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
15221da177e4SLinus Torvalds 	return res;
15231da177e4SLinus Torvalds }
15248fc795f7STrond Myklebust 
15258fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
15268fc795f7STrond Myklebust {
1527420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1528420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1529420e3646STrond Myklebust 	int ret = 0;
15308fc795f7STrond Myklebust 
1531a00dd6c0SJeff Layton 	if (wbc->sync_mode == WB_SYNC_NONE) {
1532a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1533a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1534420e3646STrond Myklebust 		 */
1535a00dd6c0SJeff Layton 		if (nfsi->ncommit <= (nfsi->npages >> 1))
1536420e3646STrond Myklebust 			goto out_mark_dirty;
1537420e3646STrond Myklebust 
1538a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1539420e3646STrond Myklebust 		flags = 0;
1540a00dd6c0SJeff Layton 	}
1541a00dd6c0SJeff Layton 
1542420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1543420e3646STrond Myklebust 	if (ret >= 0) {
1544420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1545420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1546420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1547420e3646STrond Myklebust 			else
1548420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1549420e3646STrond Myklebust 		}
15508fc795f7STrond Myklebust 		return 0;
1551420e3646STrond Myklebust 	}
1552420e3646STrond Myklebust out_mark_dirty:
15538fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
15548fc795f7STrond Myklebust 	return ret;
15558fc795f7STrond Myklebust }
1556c63c7b05STrond Myklebust #else
15578fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
15588fc795f7STrond Myklebust {
15598fc795f7STrond Myklebust 	return 0;
15608fc795f7STrond Myklebust }
15611da177e4SLinus Torvalds #endif
15621da177e4SLinus Torvalds 
15638fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
15648fc795f7STrond Myklebust {
1565863a3c6cSAndy Adamson 	int ret;
1566863a3c6cSAndy Adamson 
1567863a3c6cSAndy Adamson 	ret = nfs_commit_unstable_pages(inode, wbc);
1568863a3c6cSAndy Adamson 	if (ret >= 0 && test_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(inode)->flags)) {
1569*ef311537SAndy Adamson 		int status;
1570*ef311537SAndy Adamson 		bool sync = true;
1571863a3c6cSAndy Adamson 
1572*ef311537SAndy Adamson 		if (wbc->sync_mode == WB_SYNC_NONE || wbc->nonblocking ||
1573*ef311537SAndy Adamson 		    wbc->for_background)
1574*ef311537SAndy Adamson 			sync = false;
1575863a3c6cSAndy Adamson 
1576863a3c6cSAndy Adamson 		status = pnfs_layoutcommit_inode(inode, sync);
1577863a3c6cSAndy Adamson 		if (status < 0)
1578863a3c6cSAndy Adamson 			return status;
1579863a3c6cSAndy Adamson 	}
1580863a3c6cSAndy Adamson 	return ret;
15818fc795f7STrond Myklebust }
15828fc795f7STrond Myklebust 
1583acdc53b2STrond Myklebust /*
1584acdc53b2STrond Myklebust  * flush the inode to disk.
1585acdc53b2STrond Myklebust  */
1586acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
158734901f70STrond Myklebust {
158834901f70STrond Myklebust 	struct writeback_control wbc = {
158972cb77f4STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
159034901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
1591d7fb1207STrond Myklebust 		.range_start = 0,
1592d7fb1207STrond Myklebust 		.range_end = LLONG_MAX,
159334901f70STrond Myklebust 	};
159434901f70STrond Myklebust 
1595acdc53b2STrond Myklebust 	return sync_inode(inode, &wbc);
15961c75950bSTrond Myklebust }
15971c75950bSTrond Myklebust 
15981b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
15991b3b4a1aSTrond Myklebust {
16001b3b4a1aSTrond Myklebust 	struct nfs_page *req;
16011b3b4a1aSTrond Myklebust 	int ret = 0;
16021b3b4a1aSTrond Myklebust 
16031b3b4a1aSTrond Myklebust 	BUG_ON(!PageLocked(page));
16041b3b4a1aSTrond Myklebust 	for (;;) {
1605ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
16061b3b4a1aSTrond Myklebust 		req = nfs_page_find_request(page);
16071b3b4a1aSTrond Myklebust 		if (req == NULL)
16081b3b4a1aSTrond Myklebust 			break;
16091b3b4a1aSTrond Myklebust 		if (nfs_lock_request_dontget(req)) {
16101b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
16111b3b4a1aSTrond Myklebust 			/*
16121b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
16131b3b4a1aSTrond Myklebust 			 * page as being dirty
16141b3b4a1aSTrond Myklebust 			 */
16151b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
16161b3b4a1aSTrond Myklebust 			nfs_unlock_request(req);
16171b3b4a1aSTrond Myklebust 			break;
16181b3b4a1aSTrond Myklebust 		}
16191b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
1620c9edda71STrond Myklebust 		nfs_release_request(req);
16211b3b4a1aSTrond Myklebust 		if (ret < 0)
1622c988950eSTrond Myklebust 			break;
16231b3b4a1aSTrond Myklebust 	}
16241b3b4a1aSTrond Myklebust 	return ret;
16251b3b4a1aSTrond Myklebust }
16261b3b4a1aSTrond Myklebust 
16271c75950bSTrond Myklebust /*
16281c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
16291c75950bSTrond Myklebust  */
16301c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
16311c75950bSTrond Myklebust {
16327f2f12d9STrond Myklebust 	loff_t range_start = page_offset(page);
16337f2f12d9STrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
16347f2f12d9STrond Myklebust 	struct writeback_control wbc = {
16357f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
16367f2f12d9STrond Myklebust 		.nr_to_write = 0,
16377f2f12d9STrond Myklebust 		.range_start = range_start,
16387f2f12d9STrond Myklebust 		.range_end = range_end,
16397f2f12d9STrond Myklebust 	};
16407f2f12d9STrond Myklebust 	int ret;
16417f2f12d9STrond Myklebust 
16420522f6adSTrond Myklebust 	for (;;) {
1643ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
16447f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
16457f2f12d9STrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
16467f2f12d9STrond Myklebust 			if (ret < 0)
16477f2f12d9STrond Myklebust 				goto out_error;
16480522f6adSTrond Myklebust 			continue;
16497f2f12d9STrond Myklebust 		}
16500522f6adSTrond Myklebust 		if (!PagePrivate(page))
16510522f6adSTrond Myklebust 			break;
16520522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
16537f2f12d9STrond Myklebust 		if (ret < 0)
16547f2f12d9STrond Myklebust 			goto out_error;
16557f2f12d9STrond Myklebust 	}
16567f2f12d9STrond Myklebust 	return 0;
16577f2f12d9STrond Myklebust out_error:
16587f2f12d9STrond Myklebust 	return ret;
16591c75950bSTrond Myklebust }
16601c75950bSTrond Myklebust 
1661074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
1662074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1663074cc1deSTrond Myklebust 		struct page *page)
1664074cc1deSTrond Myklebust {
1665074cc1deSTrond Myklebust 	struct nfs_page *req;
1666074cc1deSTrond Myklebust 	int ret;
1667074cc1deSTrond Myklebust 
1668074cc1deSTrond Myklebust 	nfs_fscache_release_page(page, GFP_KERNEL);
1669074cc1deSTrond Myklebust 
1670cfb506e1STrond Myklebust 	req = nfs_find_and_lock_request(page, false);
1671074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
1672074cc1deSTrond Myklebust 	if (IS_ERR(req))
1673074cc1deSTrond Myklebust 		goto out;
1674074cc1deSTrond Myklebust 
1675074cc1deSTrond Myklebust 	ret = migrate_page(mapping, newpage, page);
1676074cc1deSTrond Myklebust 	if (!req)
1677074cc1deSTrond Myklebust 		goto out;
1678074cc1deSTrond Myklebust 	if (ret)
1679074cc1deSTrond Myklebust 		goto out_unlock;
1680074cc1deSTrond Myklebust 	page_cache_get(newpage);
1681190f38e5STrond Myklebust 	spin_lock(&mapping->host->i_lock);
1682074cc1deSTrond Myklebust 	req->wb_page = newpage;
1683074cc1deSTrond Myklebust 	SetPagePrivate(newpage);
1684190f38e5STrond Myklebust 	set_page_private(newpage, (unsigned long)req);
1685074cc1deSTrond Myklebust 	ClearPagePrivate(page);
1686074cc1deSTrond Myklebust 	set_page_private(page, 0);
1687190f38e5STrond Myklebust 	spin_unlock(&mapping->host->i_lock);
1688074cc1deSTrond Myklebust 	page_cache_release(page);
1689074cc1deSTrond Myklebust out_unlock:
1690074cc1deSTrond Myklebust 	nfs_clear_page_tag_locked(req);
1691074cc1deSTrond Myklebust out:
1692074cc1deSTrond Myklebust 	return ret;
1693074cc1deSTrond Myklebust }
1694074cc1deSTrond Myklebust #endif
1695074cc1deSTrond Myklebust 
1696f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
16971da177e4SLinus Torvalds {
16981da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
16991da177e4SLinus Torvalds 					     sizeof(struct nfs_write_data),
17001da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
170120c2df83SPaul Mundt 					     NULL);
17021da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
17031da177e4SLinus Torvalds 		return -ENOMEM;
17041da177e4SLinus Torvalds 
170593d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
17061da177e4SLinus Torvalds 						     nfs_wdata_cachep);
17071da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
17081da177e4SLinus Torvalds 		return -ENOMEM;
17091da177e4SLinus Torvalds 
171093d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
17111da177e4SLinus Torvalds 						      nfs_wdata_cachep);
17121da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
17131da177e4SLinus Torvalds 		return -ENOMEM;
17141da177e4SLinus Torvalds 
171589a09141SPeter Zijlstra 	/*
171689a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
171789a09141SPeter Zijlstra 	 *
171889a09141SPeter Zijlstra 	 *  64MB:    8192k
171989a09141SPeter Zijlstra 	 * 128MB:   11585k
172089a09141SPeter Zijlstra 	 * 256MB:   16384k
172189a09141SPeter Zijlstra 	 * 512MB:   23170k
172289a09141SPeter Zijlstra 	 *   1GB:   32768k
172389a09141SPeter Zijlstra 	 *   2GB:   46340k
172489a09141SPeter Zijlstra 	 *   4GB:   65536k
172589a09141SPeter Zijlstra 	 *   8GB:   92681k
172689a09141SPeter Zijlstra 	 *  16GB:  131072k
172789a09141SPeter Zijlstra 	 *
172889a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
172989a09141SPeter Zijlstra 	 * Limit the default to 256M
173089a09141SPeter Zijlstra 	 */
173189a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
173289a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
173389a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
173489a09141SPeter Zijlstra 
17351da177e4SLinus Torvalds 	return 0;
17361da177e4SLinus Torvalds }
17371da177e4SLinus Torvalds 
1738266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
17391da177e4SLinus Torvalds {
17401da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
17411da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
17421a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
17431da177e4SLinus Torvalds }
17441da177e4SLinus Torvalds 
1745