xref: /linux/fs/nfs/write.c (revision 8ce160c5ef06cc89c2b6b26bfa5ef7a5ce2c93e0)
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 
100dce81290STrond Myklebust 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);
3924d65c520STrond Myklebust 	if (!nfsi->npages && nfs_have_delegation(inode, FMODE_WRITE))
3931da177e4SLinus Torvalds 		nfsi->change_attr++;
3942df485a7STrond Myklebust 	set_bit(PG_MAPPED, &req->wb_flags);
395deb7d638STrond Myklebust 	SetPagePrivate(req->wb_page);
396277459d2STrond Myklebust 	set_page_private(req->wb_page, (unsigned long)req);
3971da177e4SLinus Torvalds 	nfsi->npages++;
398c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
39927852596SNick Piggin 	radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
40027852596SNick Piggin 				NFS_PAGE_TAG_LOCKED);
401e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
402e7d39069STrond Myklebust 	radix_tree_preload_end();
403e7d39069STrond Myklebust out:
404e7d39069STrond Myklebust 	return error;
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds /*
40889a09141SPeter Zijlstra  * Remove a write request from an inode
4091da177e4SLinus Torvalds  */
4101da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
4111da177e4SLinus Torvalds {
4123d4ff43dSAl Viro 	struct inode *inode = req->wb_context->dentry->d_inode;
4131da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	BUG_ON (!NFS_WBACK_BUSY(req));
4161da177e4SLinus Torvalds 
417587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
418277459d2STrond Myklebust 	set_page_private(req->wb_page, 0);
419deb7d638STrond Myklebust 	ClearPagePrivate(req->wb_page);
4202df485a7STrond Myklebust 	clear_bit(PG_MAPPED, &req->wb_flags);
4211da177e4SLinus Torvalds 	radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
4221da177e4SLinus Torvalds 	nfsi->npages--;
423587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
4241da177e4SLinus Torvalds 	nfs_release_request(req);
4251da177e4SLinus Torvalds }
4261da177e4SLinus Torvalds 
42761822ab5STrond Myklebust static void
4286d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
42961822ab5STrond Myklebust {
43061822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
43161822ab5STrond Myklebust }
43261822ab5STrond Myklebust 
4331da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
4341da177e4SLinus Torvalds /*
4351da177e4SLinus Torvalds  * Add a request to the inode's commit list.
4361da177e4SLinus Torvalds  */
4371da177e4SLinus Torvalds static void
438a861a1e1SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg)
4391da177e4SLinus Torvalds {
4403d4ff43dSAl Viro 	struct inode *inode = req->wb_context->dentry->d_inode;
4411da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4421da177e4SLinus Torvalds 
443587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
444e468bae9STrond Myklebust 	set_bit(PG_CLEAN, &(req)->wb_flags);
4455c369683STrond Myklebust 	radix_tree_tag_set(&nfsi->nfs_page_tree,
4465c369683STrond Myklebust 			req->wb_index,
4475c369683STrond Myklebust 			NFS_PAGE_TAG_COMMIT);
448ff778d02STrond Myklebust 	nfsi->ncommit++;
449587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
450a861a1e1SFred Isaman 	pnfs_mark_request_commit(req, lseg);
451fd39fc85SChristoph Lameter 	inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
452c9e51e41SPeter Zijlstra 	inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
453a1803044STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
4541da177e4SLinus Torvalds }
4558e821cadSTrond Myklebust 
456e468bae9STrond Myklebust static int
457e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
458e468bae9STrond Myklebust {
459e468bae9STrond Myklebust 	struct page *page = req->wb_page;
460e468bae9STrond Myklebust 
461e468bae9STrond Myklebust 	if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) {
462e468bae9STrond Myklebust 		dec_zone_page_state(page, NR_UNSTABLE_NFS);
463e468bae9STrond Myklebust 		dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
464e468bae9STrond Myklebust 		return 1;
465e468bae9STrond Myklebust 	}
466e468bae9STrond Myklebust 	return 0;
467e468bae9STrond Myklebust }
468e468bae9STrond Myklebust 
4698e821cadSTrond Myklebust static inline
4708e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
4718e821cadSTrond Myklebust {
472465d5243SFred Isaman 	if (data->verf.committed == NFS_DATA_SYNC)
473465d5243SFred Isaman 		return data->lseg == NULL;
474465d5243SFred Isaman 	else
4758e821cadSTrond Myklebust 		return data->verf.committed != NFS_FILE_SYNC;
4768e821cadSTrond Myklebust }
4778e821cadSTrond Myklebust 
4788e821cadSTrond Myklebust static inline
479a861a1e1SFred Isaman int nfs_reschedule_unstable_write(struct nfs_page *req,
480a861a1e1SFred Isaman 				  struct nfs_write_data *data)
4818e821cadSTrond Myklebust {
482e468bae9STrond Myklebust 	if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) {
483a861a1e1SFred Isaman 		nfs_mark_request_commit(req, data->lseg);
4848e821cadSTrond Myklebust 		return 1;
4858e821cadSTrond Myklebust 	}
4868e821cadSTrond Myklebust 	if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
4876d884e8fSFred 		nfs_mark_request_dirty(req);
4888e821cadSTrond Myklebust 		return 1;
4898e821cadSTrond Myklebust 	}
4908e821cadSTrond Myklebust 	return 0;
4918e821cadSTrond Myklebust }
4928e821cadSTrond Myklebust #else
4938e821cadSTrond Myklebust static inline void
494a861a1e1SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg)
4958e821cadSTrond Myklebust {
4968e821cadSTrond Myklebust }
4978e821cadSTrond Myklebust 
498e468bae9STrond Myklebust static inline int
499e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
500e468bae9STrond Myklebust {
501e468bae9STrond Myklebust 	return 0;
502e468bae9STrond Myklebust }
503e468bae9STrond Myklebust 
5048e821cadSTrond Myklebust static inline
5058e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
5068e821cadSTrond Myklebust {
5078e821cadSTrond Myklebust 	return 0;
5088e821cadSTrond Myklebust }
5098e821cadSTrond Myklebust 
5108e821cadSTrond Myklebust static inline
511a861a1e1SFred Isaman int nfs_reschedule_unstable_write(struct nfs_page *req,
512a861a1e1SFred Isaman 				  struct nfs_write_data *data)
5138e821cadSTrond Myklebust {
5148e821cadSTrond Myklebust 	return 0;
5158e821cadSTrond Myklebust }
5161da177e4SLinus Torvalds #endif
5171da177e4SLinus Torvalds 
51847c62564STrond Myklebust #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
519fb8a1f11STrond Myklebust static int
520fb8a1f11STrond Myklebust nfs_need_commit(struct nfs_inode *nfsi)
521fb8a1f11STrond Myklebust {
522fb8a1f11STrond Myklebust 	return radix_tree_tagged(&nfsi->nfs_page_tree, NFS_PAGE_TAG_COMMIT);
523fb8a1f11STrond Myklebust }
524fb8a1f11STrond Myklebust 
5251da177e4SLinus Torvalds /*
5261da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
5271da177e4SLinus Torvalds  * @inode: NFS inode to scan
5281da177e4SLinus Torvalds  * @dst: destination list
5291da177e4SLinus Torvalds  * @idx_start: lower bound of page->index to scan.
5301da177e4SLinus Torvalds  * @npages: idx_start + npages sets the upper bound to scan.
5311da177e4SLinus Torvalds  *
5321da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
5331da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
5341da177e4SLinus Torvalds  */
5351da177e4SLinus Torvalds static int
536ca52fec1STrond Myklebust nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
5371da177e4SLinus Torvalds {
5381da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
539ff778d02STrond Myklebust 	int ret;
5403da28eb1STrond Myklebust 
541fb8a1f11STrond Myklebust 	if (!nfs_need_commit(nfsi))
542fb8a1f11STrond Myklebust 		return 0;
543fb8a1f11STrond Myklebust 
5440d88f6e8SDave Chinner 	spin_lock(&inode->i_lock);
545ff778d02STrond Myklebust 	ret = nfs_scan_list(nfsi, dst, idx_start, npages, NFS_PAGE_TAG_COMMIT);
546ff778d02STrond Myklebust 	if (ret > 0)
547ff778d02STrond Myklebust 		nfsi->ncommit -= ret;
5480d88f6e8SDave Chinner 	spin_unlock(&inode->i_lock);
5490d88f6e8SDave Chinner 
5502928db1fSTrond Myklebust 	if (nfs_need_commit(NFS_I(inode)))
5512928db1fSTrond Myklebust 		__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
5520d88f6e8SDave Chinner 
553ff778d02STrond Myklebust 	return ret;
5541da177e4SLinus Torvalds }
555c42de9ddSTrond Myklebust #else
556fb8a1f11STrond Myklebust static inline int nfs_need_commit(struct nfs_inode *nfsi)
557fb8a1f11STrond Myklebust {
558fb8a1f11STrond Myklebust 	return 0;
559fb8a1f11STrond Myklebust }
560fb8a1f11STrond Myklebust 
561ca52fec1STrond Myklebust static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
562c42de9ddSTrond Myklebust {
563c42de9ddSTrond Myklebust 	return 0;
564c42de9ddSTrond Myklebust }
5651da177e4SLinus Torvalds #endif
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds /*
568e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
569e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
5701da177e4SLinus Torvalds  *
571e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
572e7d39069STrond Myklebust  * to disk.
5731da177e4SLinus Torvalds  */
574e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
575e7d39069STrond Myklebust 		struct page *page,
576e7d39069STrond Myklebust 		unsigned int offset,
577e7d39069STrond Myklebust 		unsigned int bytes)
5781da177e4SLinus Torvalds {
579e7d39069STrond Myklebust 	struct nfs_page *req;
580e7d39069STrond Myklebust 	unsigned int rqend;
581e7d39069STrond Myklebust 	unsigned int end;
5821da177e4SLinus Torvalds 	int error;
583277459d2STrond Myklebust 
584e7d39069STrond Myklebust 	if (!PagePrivate(page))
585e7d39069STrond Myklebust 		return NULL;
586e7d39069STrond Myklebust 
587e7d39069STrond Myklebust 	end = offset + bytes;
588e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
589e7d39069STrond Myklebust 
590e7d39069STrond Myklebust 	for (;;) {
591e7d39069STrond Myklebust 		req = nfs_page_find_request_locked(page);
592e7d39069STrond Myklebust 		if (req == NULL)
593e7d39069STrond Myklebust 			goto out_unlock;
594e7d39069STrond Myklebust 
595e7d39069STrond Myklebust 		rqend = req->wb_offset + req->wb_bytes;
596e7d39069STrond Myklebust 		/*
597e7d39069STrond Myklebust 		 * Tell the caller to flush out the request if
598e7d39069STrond Myklebust 		 * the offsets are non-contiguous.
599e7d39069STrond Myklebust 		 * Note: nfs_flush_incompatible() will already
600e7d39069STrond Myklebust 		 * have flushed out requests having wrong owners.
601e7d39069STrond Myklebust 		 */
602e468bae9STrond Myklebust 		if (offset > rqend
603e7d39069STrond Myklebust 		    || end < req->wb_offset)
604e7d39069STrond Myklebust 			goto out_flushme;
605e7d39069STrond Myklebust 
606e7d39069STrond Myklebust 		if (nfs_set_page_tag_locked(req))
607e7d39069STrond Myklebust 			break;
608e7d39069STrond Myklebust 
609e7d39069STrond Myklebust 		/* The request is locked, so wait and then retry */
610587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
6111da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
6121da177e4SLinus Torvalds 		nfs_release_request(req);
613e7d39069STrond Myklebust 		if (error != 0)
614e7d39069STrond Myklebust 			goto out_err;
615e7d39069STrond Myklebust 		spin_lock(&inode->i_lock);
6161da177e4SLinus Torvalds 	}
6171da177e4SLinus Torvalds 
618ff778d02STrond Myklebust 	if (nfs_clear_request_commit(req) &&
619e468bae9STrond Myklebust 	    radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree,
620a861a1e1SFred Isaman 				 req->wb_index, NFS_PAGE_TAG_COMMIT) != NULL) {
621ff778d02STrond Myklebust 		NFS_I(inode)->ncommit--;
622a861a1e1SFred Isaman 		pnfs_clear_request_commit(req);
623a861a1e1SFred Isaman 	}
624e468bae9STrond Myklebust 
6251da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
6261da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
6271da177e4SLinus Torvalds 		req->wb_offset = offset;
6281da177e4SLinus Torvalds 		req->wb_pgbase = offset;
6291da177e4SLinus Torvalds 	}
6301da177e4SLinus Torvalds 	if (end > rqend)
6311da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
632e7d39069STrond Myklebust 	else
633e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
634e7d39069STrond Myklebust out_unlock:
635e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
636e7d39069STrond Myklebust 	return req;
637e7d39069STrond Myklebust out_flushme:
638e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
639e7d39069STrond Myklebust 	nfs_release_request(req);
640e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
641e7d39069STrond Myklebust out_err:
642e7d39069STrond Myklebust 	return ERR_PTR(error);
643e7d39069STrond Myklebust }
6441da177e4SLinus Torvalds 
645e7d39069STrond Myklebust /*
646e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
647e7d39069STrond Myklebust  *
648e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
649e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
650e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
651e7d39069STrond Myklebust  */
652e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
653e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
654e7d39069STrond Myklebust {
655e7d39069STrond Myklebust 	struct inode *inode = page->mapping->host;
656e7d39069STrond Myklebust 	struct nfs_page	*req;
657e7d39069STrond Myklebust 	int error;
658e7d39069STrond Myklebust 
659e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
660e7d39069STrond Myklebust 	if (req != NULL)
661e7d39069STrond Myklebust 		goto out;
662e7d39069STrond Myklebust 	req = nfs_create_request(ctx, inode, page, offset, bytes);
663e7d39069STrond Myklebust 	if (IS_ERR(req))
664e7d39069STrond Myklebust 		goto out;
665e7d39069STrond Myklebust 	error = nfs_inode_add_request(inode, req);
666e7d39069STrond Myklebust 	if (error != 0) {
667e7d39069STrond Myklebust 		nfs_release_request(req);
668e7d39069STrond Myklebust 		req = ERR_PTR(error);
669e7d39069STrond Myklebust 	}
670efc91ed0STrond Myklebust out:
67161e930a9STrond Myklebust 	return req;
6721da177e4SLinus Torvalds }
6731da177e4SLinus Torvalds 
674e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
675e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
676e7d39069STrond Myklebust {
677e7d39069STrond Myklebust 	struct nfs_page	*req;
678e7d39069STrond Myklebust 
679e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
680e7d39069STrond Myklebust 	if (IS_ERR(req))
681e7d39069STrond Myklebust 		return PTR_ERR(req);
682e7d39069STrond Myklebust 	/* Update file length */
683e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
684e7d39069STrond Myklebust 	nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
685a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
686e7d39069STrond Myklebust 	nfs_clear_page_tag_locked(req);
687e7d39069STrond Myklebust 	return 0;
688e7d39069STrond Myklebust }
689e7d39069STrond Myklebust 
6901da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
6911da177e4SLinus Torvalds {
692cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
6931da177e4SLinus Torvalds 	struct nfs_page	*req;
6941a54533eSTrond Myklebust 	int do_flush, status;
6951da177e4SLinus Torvalds 	/*
6961da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
6971da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
6981da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
6991da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
7001da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
7011da177e4SLinus Torvalds 	 * dropped page.
7021da177e4SLinus Torvalds 	 */
7031a54533eSTrond Myklebust 	do {
704277459d2STrond Myklebust 		req = nfs_page_find_request(page);
7051a54533eSTrond Myklebust 		if (req == NULL)
7061a54533eSTrond Myklebust 			return 0;
707f11ac8dbSTrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx ||
708f11ac8dbSTrond Myklebust 			req->wb_lock_context->lockowner != current->files ||
709f11ac8dbSTrond Myklebust 			req->wb_lock_context->pid != current->tgid;
7101da177e4SLinus Torvalds 		nfs_release_request(req);
7111a54533eSTrond Myklebust 		if (!do_flush)
7121a54533eSTrond Myklebust 			return 0;
713277459d2STrond Myklebust 		status = nfs_wb_page(page->mapping->host, page);
7141a54533eSTrond Myklebust 	} while (status == 0);
7151a54533eSTrond Myklebust 	return status;
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds /*
7195d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
7205d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
7215d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
7225d47a356STrond Myklebust  */
7235d47a356STrond Myklebust static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
7245d47a356STrond Myklebust {
7255d47a356STrond Myklebust 	return PageUptodate(page) &&
7265d47a356STrond Myklebust 		!(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
7275d47a356STrond Myklebust }
7285d47a356STrond Myklebust 
7295d47a356STrond Myklebust /*
7301da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
7311da177e4SLinus Torvalds  *
7321da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
7331da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
7341da177e4SLinus Torvalds  */
7351da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
7361da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
7371da177e4SLinus Torvalds {
738cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
7391da177e4SLinus Torvalds 	struct inode	*inode = page->mapping->host;
7401da177e4SLinus Torvalds 	int		status = 0;
7411da177e4SLinus Torvalds 
74291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
74391d5b470SChuck Lever 
74448186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage(%s/%s %d@%lld)\n",
74501cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
74601cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name, count,
7470bbacc40SChuck Lever 		(long long)(page_offset(page) + offset));
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 	/* If we're not using byte range locks, and we know the page
7505d47a356STrond Myklebust 	 * is up to date, it may be more efficient to extend the write
7515d47a356STrond Myklebust 	 * to cover the entire page in order to avoid fragmentation
7525d47a356STrond Myklebust 	 * inefficiencies.
7531da177e4SLinus Torvalds 	 */
7545d47a356STrond Myklebust 	if (nfs_write_pageuptodate(page, inode) &&
7555d47a356STrond Myklebust 			inode->i_flock == NULL &&
7566b2f3d1fSChristoph Hellwig 			!(file->f_flags & O_DSYNC)) {
75749a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
7581da177e4SLinus Torvalds 		offset = 0;
7591da177e4SLinus Torvalds 	}
7601da177e4SLinus Torvalds 
761e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
76203fa9e84STrond Myklebust 	if (status < 0)
76303fa9e84STrond Myklebust 		nfs_set_pageerror(page);
76459b7c05fSTrond Myklebust 	else
76559b7c05fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
7661da177e4SLinus Torvalds 
76748186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
7681da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
7691da177e4SLinus Torvalds 	return status;
7701da177e4SLinus Torvalds }
7711da177e4SLinus Torvalds 
772a861a1e1SFred Isaman static void nfs_writepage_release(struct nfs_page *req,
773a861a1e1SFred Isaman 				  struct nfs_write_data *data)
7741da177e4SLinus Torvalds {
775a6305ddbSTrond Myklebust 	struct page *page = req->wb_page;
7768e821cadSTrond Myklebust 
777a861a1e1SFred Isaman 	if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req, data))
7781da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
7799fd367f0STrond Myklebust 	nfs_clear_page_tag_locked(req);
780a6305ddbSTrond Myklebust 	nfs_end_page_writeback(page);
7811da177e4SLinus Torvalds }
7821da177e4SLinus Torvalds 
7833ff7576dSTrond Myklebust static int flush_task_priority(int how)
7841da177e4SLinus Torvalds {
7851da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
7861da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
7871da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
7881da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
7891da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
7901da177e4SLinus Torvalds 	}
7911da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
7921da177e4SLinus Torvalds }
7931da177e4SLinus Torvalds 
794a69aef14SFred Isaman int nfs_initiate_write(struct nfs_write_data *data,
795d138d5d1SAndy Adamson 		       struct rpc_clnt *clnt,
796788e7a89STrond Myklebust 		       const struct rpc_call_ops *call_ops,
7971da177e4SLinus Torvalds 		       int how)
7981da177e4SLinus Torvalds {
799d138d5d1SAndy Adamson 	struct inode *inode = data->inode;
8003ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
80107737691STrond Myklebust 	struct rpc_task *task;
802bdc7f021STrond Myklebust 	struct rpc_message msg = {
803bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
804bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
805d138d5d1SAndy Adamson 		.rpc_cred = data->cred,
806bdc7f021STrond Myklebust 	};
80784115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
808d138d5d1SAndy Adamson 		.rpc_client = clnt,
80907737691STrond Myklebust 		.task = &data->task,
810bdc7f021STrond Myklebust 		.rpc_message = &msg,
81184115e1cSTrond Myklebust 		.callback_ops = call_ops,
81284115e1cSTrond Myklebust 		.callback_data = data,
813101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
8142c61be0aSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
8153ff7576dSTrond Myklebust 		.priority = priority,
81684115e1cSTrond Myklebust 	};
8172c61be0aSTrond Myklebust 	int ret = 0;
8181da177e4SLinus Torvalds 
819d138d5d1SAndy Adamson 	/* Set up the initial task struct.  */
820d138d5d1SAndy Adamson 	NFS_PROTO(inode)->write_setup(data, &msg);
821d138d5d1SAndy Adamson 
822d138d5d1SAndy Adamson 	dprintk("NFS: %5u initiated write call "
823d138d5d1SAndy Adamson 		"(req %s/%lld, %u bytes @ offset %llu)\n",
824d138d5d1SAndy Adamson 		data->task.tk_pid,
825d138d5d1SAndy Adamson 		inode->i_sb->s_id,
826d138d5d1SAndy Adamson 		(long long)NFS_FILEID(inode),
827d138d5d1SAndy Adamson 		data->args.count,
828d138d5d1SAndy Adamson 		(unsigned long long)data->args.offset);
829d138d5d1SAndy Adamson 
830d138d5d1SAndy Adamson 	task = rpc_run_task(&task_setup_data);
831d138d5d1SAndy Adamson 	if (IS_ERR(task)) {
832d138d5d1SAndy Adamson 		ret = PTR_ERR(task);
833d138d5d1SAndy Adamson 		goto out;
834d138d5d1SAndy Adamson 	}
835d138d5d1SAndy Adamson 	if (how & FLUSH_SYNC) {
836d138d5d1SAndy Adamson 		ret = rpc_wait_for_completion_task(task);
837d138d5d1SAndy Adamson 		if (ret == 0)
838d138d5d1SAndy Adamson 			ret = task->tk_status;
839d138d5d1SAndy Adamson 	}
840d138d5d1SAndy Adamson 	rpc_put_task(task);
841d138d5d1SAndy Adamson out:
842d138d5d1SAndy Adamson 	return ret;
843d138d5d1SAndy Adamson }
844a69aef14SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_write);
845d138d5d1SAndy Adamson 
846d138d5d1SAndy Adamson /*
847d138d5d1SAndy Adamson  * Set up the argument/result storage required for the RPC call.
848d138d5d1SAndy Adamson  */
8496e4efd56STrond Myklebust static void nfs_write_rpcsetup(struct nfs_page *req,
850d138d5d1SAndy Adamson 		struct nfs_write_data *data,
851d138d5d1SAndy Adamson 		unsigned int count, unsigned int offset,
852d138d5d1SAndy Adamson 		int how)
853d138d5d1SAndy Adamson {
8543d4ff43dSAl Viro 	struct inode *inode = req->wb_context->dentry->d_inode;
855d138d5d1SAndy Adamson 
8561da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
8571da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	data->req = req;
8603d4ff43dSAl Viro 	data->inode = inode = req->wb_context->dentry->d_inode;
861d138d5d1SAndy Adamson 	data->cred = req->wb_context->cred;
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(inode);
8641da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
8652bea038cSBoaz Harrosh 	/* pnfs_set_layoutcommit needs this */
8662bea038cSBoaz Harrosh 	data->mds_offset = data->args.offset;
8671da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
8681da177e4SLinus Torvalds 	data->args.pages  = data->pagevec;
8691da177e4SLinus Torvalds 	data->args.count  = count;
870383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
871f11ac8dbSTrond Myklebust 	data->args.lock_context = req->wb_lock_context;
872bdc7f021STrond Myklebust 	data->args.stable  = NFS_UNSTABLE;
87387ed5eb4STrond Myklebust 	switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
87487ed5eb4STrond Myklebust 	case 0:
87587ed5eb4STrond Myklebust 		break;
87687ed5eb4STrond Myklebust 	case FLUSH_COND_STABLE:
87787ed5eb4STrond Myklebust 		if (nfs_need_commit(NFS_I(inode)))
87887ed5eb4STrond Myklebust 			break;
87987ed5eb4STrond Myklebust 	default:
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);
8876e4efd56STrond Myklebust }
8881da177e4SLinus Torvalds 
8896e4efd56STrond Myklebust static int nfs_do_write(struct nfs_write_data *data,
8906e4efd56STrond Myklebust 		const struct rpc_call_ops *call_ops,
8916e4efd56STrond Myklebust 		int how)
8926e4efd56STrond Myklebust {
8935f00bcb3SStephen Rothwell 	struct inode *inode = data->args.context->dentry->d_inode;
8940382b744SAndy Adamson 
895d138d5d1SAndy Adamson 	return nfs_initiate_write(data, NFS_CLIENT(inode), call_ops, how);
8961da177e4SLinus Torvalds }
8971da177e4SLinus Torvalds 
898275acaafSTrond Myklebust static int nfs_do_multiple_writes(struct list_head *head,
899275acaafSTrond Myklebust 		const struct rpc_call_ops *call_ops,
900275acaafSTrond Myklebust 		int how)
901275acaafSTrond Myklebust {
902275acaafSTrond Myklebust 	struct nfs_write_data *data;
903275acaafSTrond Myklebust 	int ret = 0;
904275acaafSTrond Myklebust 
905275acaafSTrond Myklebust 	while (!list_empty(head)) {
906275acaafSTrond Myklebust 		int ret2;
907275acaafSTrond Myklebust 
908275acaafSTrond Myklebust 		data = list_entry(head->next, struct nfs_write_data, list);
909275acaafSTrond Myklebust 		list_del_init(&data->list);
910275acaafSTrond Myklebust 
911dce81290STrond Myklebust 		ret2 = nfs_do_write(data, call_ops, how);
912275acaafSTrond Myklebust 		 if (ret == 0)
913275acaafSTrond Myklebust 			 ret = ret2;
914275acaafSTrond Myklebust 	}
915275acaafSTrond Myklebust 	return ret;
916275acaafSTrond Myklebust }
917275acaafSTrond Myklebust 
9186d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
9196d884e8fSFred  * call this on each, which will prepare them to be retried on next
9206d884e8fSFred  * writeback using standard nfs.
9216d884e8fSFred  */
9226d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
9236d884e8fSFred {
924a6305ddbSTrond Myklebust 	struct page *page = req->wb_page;
925a6305ddbSTrond Myklebust 
9266d884e8fSFred 	nfs_mark_request_dirty(req);
9276d884e8fSFred 	nfs_clear_page_tag_locked(req);
928a6305ddbSTrond Myklebust 	nfs_end_page_writeback(page);
9296d884e8fSFred }
9306d884e8fSFred 
9311da177e4SLinus Torvalds /*
9321da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
9331da177e4SLinus Torvalds  * contiguous dirty area on one page.
9341da177e4SLinus Torvalds  */
935275acaafSTrond Myklebust static int nfs_flush_multi(struct nfs_pageio_descriptor *desc, struct list_head *res)
9361da177e4SLinus Torvalds {
937c76069bdSFred Isaman 	struct nfs_page *req = nfs_list_entry(desc->pg_list.next);
9381da177e4SLinus Torvalds 	struct page *page = req->wb_page;
9391da177e4SLinus Torvalds 	struct nfs_write_data *data;
940d097971dSTrond Myklebust 	size_t wsize = desc->pg_bsize, nbytes;
941e9f7bee1STrond Myklebust 	unsigned int offset;
9421da177e4SLinus Torvalds 	int requests = 0;
943dbae4c73STrond Myklebust 	int ret = 0;
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	nfs_list_remove_request(req);
9461da177e4SLinus Torvalds 
947b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
948b31268acSTrond Myklebust 	    (desc->pg_moreio || NFS_I(desc->pg_inode)->ncommit ||
949b31268acSTrond Myklebust 	     desc->pg_count > wsize))
950b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
951b31268acSTrond Myklebust 
952b31268acSTrond Myklebust 
953275acaafSTrond Myklebust 	offset = 0;
954c76069bdSFred Isaman 	nbytes = desc->pg_count;
955e9f7bee1STrond Myklebust 	do {
956e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
957e9f7bee1STrond Myklebust 
9588d5658c9STrond Myklebust 		data = nfs_writedata_alloc(1);
9591da177e4SLinus Torvalds 		if (!data)
9601da177e4SLinus Torvalds 			goto out_bad;
961275acaafSTrond Myklebust 		data->pagevec[0] = page;
962f13c3620STrond Myklebust 		nfs_write_rpcsetup(req, data, len, offset, desc->pg_ioflags);
963275acaafSTrond Myklebust 		list_add(&data->list, res);
9641da177e4SLinus Torvalds 		requests++;
965e9f7bee1STrond Myklebust 		nbytes -= len;
966275acaafSTrond Myklebust 		offset += len;
967e9f7bee1STrond Myklebust 	} while (nbytes != 0);
9681da177e4SLinus Torvalds 	atomic_set(&req->wb_complete, requests);
96950828d7eSTrond Myklebust 	desc->pg_rpc_callops = &nfs_write_partial_ops;
970dbae4c73STrond Myklebust 	return ret;
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds out_bad:
973275acaafSTrond Myklebust 	while (!list_empty(res)) {
974275acaafSTrond Myklebust 		data = list_entry(res->next, struct nfs_write_data, list);
9756e4efd56STrond Myklebust 		list_del(&data->list);
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  */
990275acaafSTrond Myklebust static int nfs_flush_one(struct nfs_pageio_descriptor *desc, struct list_head *res)
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;
9963b609184SPeng Tao 	int ret = 0;
9971da177e4SLinus Torvalds 
998c76069bdSFred Isaman 	data = nfs_writedata_alloc(nfs_page_array_len(desc->pg_base,
999c76069bdSFred Isaman 						      desc->pg_count));
100044b83799SFred Isaman 	if (!data) {
100144b83799SFred Isaman 		while (!list_empty(head)) {
100244b83799SFred Isaman 			req = nfs_list_entry(head->next);
100344b83799SFred Isaman 			nfs_list_remove_request(req);
100444b83799SFred Isaman 			nfs_redirty_request(req);
100544b83799SFred Isaman 		}
100644b83799SFred Isaman 		ret = -ENOMEM;
100744b83799SFred Isaman 		goto out;
100844b83799SFred Isaman 	}
10091da177e4SLinus Torvalds 	pages = data->pagevec;
10101da177e4SLinus Torvalds 	while (!list_empty(head)) {
10111da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
10121da177e4SLinus Torvalds 		nfs_list_remove_request(req);
10131da177e4SLinus Torvalds 		nfs_list_add_request(req, &data->pages);
10141da177e4SLinus Torvalds 		ClearPageError(req->wb_page);
10151da177e4SLinus Torvalds 		*pages++ = req->wb_page;
10161da177e4SLinus Torvalds 	}
10171da177e4SLinus Torvalds 	req = nfs_list_entry(data->pages.next);
10181da177e4SLinus Torvalds 
1019b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1020b31268acSTrond Myklebust 	    (desc->pg_moreio || NFS_I(desc->pg_inode)->ncommit))
1021b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1022b31268acSTrond Myklebust 
10231da177e4SLinus Torvalds 	/* Set up the argument struct */
10246e4efd56STrond Myklebust 	nfs_write_rpcsetup(req, data, desc->pg_count, 0, desc->pg_ioflags);
1025275acaafSTrond Myklebust 	list_add(&data->list, res);
102650828d7eSTrond Myklebust 	desc->pg_rpc_callops = &nfs_write_full_ops;
102744b83799SFred Isaman out:
102844b83799SFred Isaman 	return ret;
10291da177e4SLinus Torvalds }
10301da177e4SLinus Torvalds 
1031dce81290STrond Myklebust int nfs_generic_flush(struct nfs_pageio_descriptor *desc, struct list_head *head)
1032dce81290STrond Myklebust {
1033dce81290STrond Myklebust 	if (desc->pg_bsize < PAGE_CACHE_SIZE)
1034dce81290STrond Myklebust 		return nfs_flush_multi(desc, head);
1035dce81290STrond Myklebust 	return nfs_flush_one(desc, head);
1036dce81290STrond Myklebust }
1037dce81290STrond Myklebust 
1038dce81290STrond Myklebust static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
10391751c363STrond Myklebust {
1040275acaafSTrond Myklebust 	LIST_HEAD(head);
1041275acaafSTrond Myklebust 	int ret;
1042275acaafSTrond Myklebust 
1043dce81290STrond Myklebust 	ret = nfs_generic_flush(desc, &head);
1044275acaafSTrond Myklebust 	if (ret == 0)
104550828d7eSTrond Myklebust 		ret = nfs_do_multiple_writes(&head, desc->pg_rpc_callops,
1046dce81290STrond Myklebust 				desc->pg_ioflags);
1047275acaafSTrond Myklebust 	return ret;
10481751c363STrond Myklebust }
10491751c363STrond Myklebust 
10501751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_write_ops = {
10511751c363STrond Myklebust 	.pg_test = nfs_generic_pg_test,
10521751c363STrond Myklebust 	.pg_doio = nfs_generic_pg_writepages,
10531751c363STrond Myklebust };
10541751c363STrond Myklebust 
10551f945357STrond Myklebust static void nfs_pageio_init_write_mds(struct nfs_pageio_descriptor *pgio,
10561751c363STrond Myklebust 				  struct inode *inode, int ioflags)
10571751c363STrond Myklebust {
10581751c363STrond Myklebust 	nfs_pageio_init(pgio, inode, &nfs_pageio_write_ops,
10591751c363STrond Myklebust 				NFS_SERVER(inode)->wsize, ioflags);
10601751c363STrond Myklebust }
10611751c363STrond Myklebust 
1062dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1063dce81290STrond Myklebust {
1064dce81290STrond Myklebust 	pgio->pg_ops = &nfs_pageio_write_ops;
1065dce81290STrond Myklebust 	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1066dce81290STrond Myklebust }
10671f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1068dce81290STrond Myklebust 
1069c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1070c63c7b05STrond Myklebust 				  struct inode *inode, int ioflags)
10711da177e4SLinus Torvalds {
10721751c363STrond Myklebust 	if (!pnfs_pageio_init_write(pgio, inode, ioflags))
10731751c363STrond Myklebust 		nfs_pageio_init_write_mds(pgio, inode, ioflags);
10741da177e4SLinus Torvalds }
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds /*
10771da177e4SLinus Torvalds  * Handle a write reply that flushed part of a page.
10781da177e4SLinus Torvalds  */
1079788e7a89STrond Myklebust static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
10801da177e4SLinus Torvalds {
1081788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
10821da177e4SLinus Torvalds 
108348186c7dSChuck Lever 	dprintk("NFS: %5u write(%s/%lld %d@%lld)",
108448186c7dSChuck Lever 		task->tk_pid,
10853d4ff43dSAl Viro 		data->req->wb_context->dentry->d_inode->i_sb->s_id,
108648186c7dSChuck Lever 		(long long)
10873d4ff43dSAl Viro 		  NFS_FILEID(data->req->wb_context->dentry->d_inode),
108848186c7dSChuck Lever 		data->req->wb_bytes, (long long)req_offset(data->req));
10891da177e4SLinus Torvalds 
1090c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
1091c9d8f89dSTrond Myklebust }
1092788e7a89STrond Myklebust 
1093c9d8f89dSTrond Myklebust static void nfs_writeback_release_partial(void *calldata)
1094c9d8f89dSTrond Myklebust {
1095c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1096c9d8f89dSTrond Myklebust 	struct nfs_page		*req = data->req;
1097c9d8f89dSTrond Myklebust 	struct page		*page = req->wb_page;
1098c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1099c9d8f89dSTrond Myklebust 
1100c9d8f89dSTrond Myklebust 	if (status < 0) {
1101a301b777STrond Myklebust 		nfs_set_pageerror(page);
1102c9d8f89dSTrond Myklebust 		nfs_context_set_write_error(req->wb_context, status);
1103c9d8f89dSTrond Myklebust 		dprintk(", error = %d\n", status);
11048e821cadSTrond Myklebust 		goto out;
11058e821cadSTrond Myklebust 	}
11068e821cadSTrond Myklebust 
11078e821cadSTrond Myklebust 	if (nfs_write_need_commit(data)) {
1108587142f8STrond Myklebust 		struct inode *inode = page->mapping->host;
11098e821cadSTrond Myklebust 
1110587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
11118e821cadSTrond Myklebust 		if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
11128e821cadSTrond Myklebust 			/* Do nothing we need to resend the writes */
11138e821cadSTrond Myklebust 		} else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
11141da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
11151da177e4SLinus Torvalds 			dprintk(" defer commit\n");
11161da177e4SLinus Torvalds 		} else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
11178e821cadSTrond Myklebust 			set_bit(PG_NEED_RESCHED, &req->wb_flags);
11188e821cadSTrond Myklebust 			clear_bit(PG_NEED_COMMIT, &req->wb_flags);
11191da177e4SLinus Torvalds 			dprintk(" server reboot detected\n");
11201da177e4SLinus Torvalds 		}
1121587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
11221da177e4SLinus Torvalds 	} else
11231da177e4SLinus Torvalds 		dprintk(" OK\n");
11241da177e4SLinus Torvalds 
11258e821cadSTrond Myklebust out:
11261da177e4SLinus Torvalds 	if (atomic_dec_and_test(&req->wb_complete))
1127a861a1e1SFred Isaman 		nfs_writepage_release(req, data);
1128c9d8f89dSTrond Myklebust 	nfs_writedata_release(calldata);
11291da177e4SLinus Torvalds }
11301da177e4SLinus Torvalds 
1131def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1)
1132def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata)
1133def6ed7eSAndy Adamson {
1134def6ed7eSAndy Adamson 	struct nfs_write_data *data = calldata;
1135def6ed7eSAndy Adamson 
1136035168abSTrond Myklebust 	if (nfs4_setup_sequence(NFS_SERVER(data->inode),
1137035168abSTrond Myklebust 				&data->args.seq_args,
1138def6ed7eSAndy Adamson 				&data->res.seq_res, 1, task))
1139def6ed7eSAndy Adamson 		return;
1140def6ed7eSAndy Adamson 	rpc_call_start(task);
1141def6ed7eSAndy Adamson }
1142def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1143def6ed7eSAndy Adamson 
1144788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops = {
1145def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1)
1146def6ed7eSAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
1147def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1148788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_partial,
1149c9d8f89dSTrond Myklebust 	.rpc_release = nfs_writeback_release_partial,
1150788e7a89STrond Myklebust };
1151788e7a89STrond Myklebust 
11521da177e4SLinus Torvalds /*
11531da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
11541da177e4SLinus Torvalds  *
11551da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
11561da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
11571da177e4SLinus Torvalds  *	  as the page has a write request pending.
11581da177e4SLinus Torvalds  */
1159788e7a89STrond Myklebust static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
11601da177e4SLinus Torvalds {
1161788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
11621da177e4SLinus Torvalds 
1163c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
1164c9d8f89dSTrond Myklebust }
1165c9d8f89dSTrond Myklebust 
1166c9d8f89dSTrond Myklebust static void nfs_writeback_release_full(void *calldata)
1167c9d8f89dSTrond Myklebust {
1168c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1169*8ce160c5SPeng Tao 	int ret, status = data->task.tk_status;
1170*8ce160c5SPeng Tao 	struct nfs_pageio_descriptor pgio;
1171*8ce160c5SPeng Tao 
1172*8ce160c5SPeng Tao 	if (data->pnfs_error) {
1173*8ce160c5SPeng Tao 		nfs_pageio_init_write_mds(&pgio, data->inode, FLUSH_STABLE);
1174*8ce160c5SPeng Tao 		pgio.pg_recoalesce = 1;
1175*8ce160c5SPeng Tao 	}
1176788e7a89STrond Myklebust 
11771da177e4SLinus Torvalds 	/* Update attributes as result of writeback. */
11781da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
1179c9d8f89dSTrond Myklebust 		struct nfs_page *req = nfs_list_entry(data->pages.next);
1180c9d8f89dSTrond Myklebust 		struct page *page = req->wb_page;
1181c9d8f89dSTrond Myklebust 
11821da177e4SLinus Torvalds 		nfs_list_remove_request(req);
11831da177e4SLinus Torvalds 
118448186c7dSChuck Lever 		dprintk("NFS: %5u write (%s/%lld %d@%lld)",
118548186c7dSChuck Lever 			data->task.tk_pid,
11863d4ff43dSAl Viro 			req->wb_context->dentry->d_inode->i_sb->s_id,
11873d4ff43dSAl Viro 			(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
11881da177e4SLinus Torvalds 			req->wb_bytes,
11891da177e4SLinus Torvalds 			(long long)req_offset(req));
11901da177e4SLinus Torvalds 
1191*8ce160c5SPeng Tao 		if (data->pnfs_error) {
1192*8ce160c5SPeng Tao 			dprintk(", pnfs error = %d\n", data->pnfs_error);
1193*8ce160c5SPeng Tao 			goto next;
1194*8ce160c5SPeng Tao 		}
1195*8ce160c5SPeng Tao 
1196c9d8f89dSTrond Myklebust 		if (status < 0) {
1197a301b777STrond Myklebust 			nfs_set_pageerror(page);
1198c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
1199c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
12008e821cadSTrond Myklebust 			goto remove_request;
12011da177e4SLinus Torvalds 		}
12021da177e4SLinus Torvalds 
12038e821cadSTrond Myklebust 		if (nfs_write_need_commit(data)) {
12041da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1205a861a1e1SFred Isaman 			nfs_mark_request_commit(req, data->lseg);
12061da177e4SLinus Torvalds 			dprintk(" marked for commit\n");
12078e821cadSTrond Myklebust 			goto next;
12088e821cadSTrond Myklebust 		}
12098e821cadSTrond Myklebust 		dprintk(" OK\n");
12108e821cadSTrond Myklebust remove_request:
12111da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
12121da177e4SLinus Torvalds 	next:
12139fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
1214a6305ddbSTrond Myklebust 		nfs_end_page_writeback(page);
1215*8ce160c5SPeng Tao 		if (data->pnfs_error) {
1216*8ce160c5SPeng Tao 			lock_page(page);
1217*8ce160c5SPeng Tao 			nfs_pageio_cond_complete(&pgio, page->index);
1218*8ce160c5SPeng Tao 			ret = nfs_page_async_flush(&pgio, page, 0);
1219*8ce160c5SPeng Tao 			if (ret) {
1220*8ce160c5SPeng Tao 				nfs_set_pageerror(page);
1221*8ce160c5SPeng Tao 				dprintk("rewrite to MDS error = %d\n", ret);
12221da177e4SLinus Torvalds 			}
1223*8ce160c5SPeng Tao 			unlock_page(page);
1224*8ce160c5SPeng Tao 		}
1225*8ce160c5SPeng Tao 	}
1226*8ce160c5SPeng Tao 	if (data->pnfs_error)
1227*8ce160c5SPeng Tao 		nfs_pageio_complete(&pgio);
1228c9d8f89dSTrond Myklebust 	nfs_writedata_release(calldata);
12291da177e4SLinus Torvalds }
12301da177e4SLinus Torvalds 
1231788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops = {
1232def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1)
1233def6ed7eSAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
1234def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1235788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_full,
1236c9d8f89dSTrond Myklebust 	.rpc_release = nfs_writeback_release_full,
1237788e7a89STrond Myklebust };
1238788e7a89STrond Myklebust 
1239788e7a89STrond Myklebust 
12401da177e4SLinus Torvalds /*
12411da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
12421da177e4SLinus Torvalds  */
124313602896SFred Isaman void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
12441da177e4SLinus Torvalds {
12451da177e4SLinus Torvalds 	struct nfs_writeargs	*argp = &data->args;
12461da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
1247eedc020eSAndy Adamson 	struct nfs_server	*server = NFS_SERVER(data->inode);
1248788e7a89STrond Myklebust 	int status;
12491da177e4SLinus Torvalds 
1250a3f565b1SChuck Lever 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
12511da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
12521da177e4SLinus Torvalds 
1253f551e44fSChuck Lever 	/*
1254f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1255f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1256f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1257f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1258f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1259f551e44fSChuck Lever 	 */
1260788e7a89STrond Myklebust 	status = NFS_PROTO(data->inode)->write_done(task, data);
1261788e7a89STrond Myklebust 	if (status != 0)
126213602896SFred Isaman 		return;
126391d5b470SChuck Lever 	nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
126491d5b470SChuck Lever 
12651da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
12661da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
12671da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
12681da177e4SLinus Torvalds 		 * commit data to stable storage even though we
12691da177e4SLinus Torvalds 		 * requested it.
12701da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
12711da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
12721da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
12731da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
12741da177e4SLinus Torvalds 		 */
12751da177e4SLinus Torvalds 		static unsigned long    complain;
12761da177e4SLinus Torvalds 
1277a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
12781da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
12791da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
12801da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1281eedc020eSAndy Adamson 				server->nfs_client->cl_hostname,
12821da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
12831da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
12841da177e4SLinus Torvalds 		}
12851da177e4SLinus Torvalds 	}
12861da177e4SLinus Torvalds #endif
12871da177e4SLinus Torvalds 	/* Is this a short write? */
12881da177e4SLinus Torvalds 	if (task->tk_status >= 0 && resp->count < argp->count) {
12891da177e4SLinus Torvalds 		static unsigned long    complain;
12901da177e4SLinus Torvalds 
129191d5b470SChuck Lever 		nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
129291d5b470SChuck Lever 
12931da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
12941da177e4SLinus Torvalds 		if (resp->count != 0) {
12951da177e4SLinus Torvalds 			/* Was this an NFSv2 write or an NFSv3 stable write? */
12961da177e4SLinus Torvalds 			if (resp->verf->committed != NFS_UNSTABLE) {
12971da177e4SLinus Torvalds 				/* Resend from where the server left off */
1298a69aef14SFred Isaman 				data->mds_offset += resp->count;
12991da177e4SLinus Torvalds 				argp->offset += resp->count;
13001da177e4SLinus Torvalds 				argp->pgbase += resp->count;
13011da177e4SLinus Torvalds 				argp->count -= resp->count;
13021da177e4SLinus Torvalds 			} else {
13031da177e4SLinus Torvalds 				/* Resend as a stable write in order to avoid
13041da177e4SLinus Torvalds 				 * headaches in the case of a server crash.
13051da177e4SLinus Torvalds 				 */
13061da177e4SLinus Torvalds 				argp->stable = NFS_FILE_SYNC;
13071da177e4SLinus Torvalds 			}
13080110ee15STrond Myklebust 			nfs_restart_rpc(task, server->nfs_client);
130913602896SFred Isaman 			return;
13101da177e4SLinus Torvalds 		}
13111da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
13121da177e4SLinus Torvalds 			printk(KERN_WARNING
13131da177e4SLinus Torvalds 			       "NFS: Server wrote zero bytes, expected %u.\n",
13141da177e4SLinus Torvalds 					argp->count);
13151da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
13161da177e4SLinus Torvalds 		}
13171da177e4SLinus Torvalds 		/* Can't do anything about it except throw an error. */
13181da177e4SLinus Torvalds 		task->tk_status = -EIO;
13191da177e4SLinus Torvalds 	}
132013602896SFred Isaman 	return;
13211da177e4SLinus Torvalds }
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
132571d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
132671d0a611STrond Myklebust {
1327b8413f98STrond Myklebust 	int ret;
1328b8413f98STrond Myklebust 
132971d0a611STrond Myklebust 	if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
133071d0a611STrond Myklebust 		return 1;
1331b8413f98STrond Myklebust 	if (!may_wait)
133271d0a611STrond Myklebust 		return 0;
1333b8413f98STrond Myklebust 	ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1334b8413f98STrond Myklebust 				NFS_INO_COMMIT,
1335b8413f98STrond Myklebust 				nfs_wait_bit_killable,
1336b8413f98STrond Myklebust 				TASK_KILLABLE);
1337b8413f98STrond Myklebust 	return (ret < 0) ? ret : 1;
133871d0a611STrond Myklebust }
133971d0a611STrond Myklebust 
1340e0c2b380SFred Isaman void nfs_commit_clear_lock(struct nfs_inode *nfsi)
134171d0a611STrond Myklebust {
134271d0a611STrond Myklebust 	clear_bit(NFS_INO_COMMIT, &nfsi->flags);
134371d0a611STrond Myklebust 	smp_mb__after_clear_bit();
134471d0a611STrond Myklebust 	wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
134571d0a611STrond Myklebust }
1346e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_clear_lock);
134771d0a611STrond Myklebust 
1348e0c2b380SFred Isaman void nfs_commitdata_release(void *data)
13491da177e4SLinus Torvalds {
1350383ba719STrond Myklebust 	struct nfs_write_data *wdata = data;
1351383ba719STrond Myklebust 
1352988b6dceSFred Isaman 	put_lseg(wdata->lseg);
1353383ba719STrond Myklebust 	put_nfs_open_context(wdata->args.context);
13541da177e4SLinus Torvalds 	nfs_commit_free(wdata);
13551da177e4SLinus Torvalds }
1356e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
13571da177e4SLinus Torvalds 
1358e0c2b380SFred Isaman int nfs_initiate_commit(struct nfs_write_data *data, struct rpc_clnt *clnt,
13599ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
1360788e7a89STrond Myklebust 			int how)
13611da177e4SLinus Torvalds {
136207737691STrond Myklebust 	struct rpc_task *task;
13639ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1364bdc7f021STrond Myklebust 	struct rpc_message msg = {
1365bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1366bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
13679ace33cdSFred Isaman 		.rpc_cred = data->cred,
1368bdc7f021STrond Myklebust 	};
136984115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
137007737691STrond Myklebust 		.task = &data->task,
13719ace33cdSFred Isaman 		.rpc_client = clnt,
1372bdc7f021STrond Myklebust 		.rpc_message = &msg,
13739ace33cdSFred Isaman 		.callback_ops = call_ops,
137484115e1cSTrond Myklebust 		.callback_data = data,
1375101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
13762c61be0aSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
13773ff7576dSTrond Myklebust 		.priority = priority,
137884115e1cSTrond Myklebust 	};
1379788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
13809ace33cdSFred Isaman 	NFS_PROTO(data->inode)->commit_setup(data, &msg);
13811da177e4SLinus Torvalds 
1382a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1383bdc7f021STrond Myklebust 
138407737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1385dbae4c73STrond Myklebust 	if (IS_ERR(task))
1386dbae4c73STrond Myklebust 		return PTR_ERR(task);
1387d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1388d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
138907737691STrond Myklebust 	rpc_put_task(task);
1390dbae4c73STrond Myklebust 	return 0;
13911da177e4SLinus Torvalds }
1392e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds /*
13959ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
13969ace33cdSFred Isaman  */
1397e0c2b380SFred Isaman void nfs_init_commit(struct nfs_write_data *data,
1398988b6dceSFred Isaman 			    struct list_head *head,
1399988b6dceSFred Isaman 			    struct pnfs_layout_segment *lseg)
14009ace33cdSFred Isaman {
14019ace33cdSFred Isaman 	struct nfs_page *first = nfs_list_entry(head->next);
14023d4ff43dSAl Viro 	struct inode *inode = first->wb_context->dentry->d_inode;
14039ace33cdSFred Isaman 
14049ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
14059ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
14069ace33cdSFred Isaman 
14079ace33cdSFred Isaman 	list_splice_init(head, &data->pages);
14089ace33cdSFred Isaman 
14099ace33cdSFred Isaman 	data->inode	  = inode;
14109ace33cdSFred Isaman 	data->cred	  = first->wb_context->cred;
1411988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
14129ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
14139ace33cdSFred Isaman 
14149ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
14159ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
14169ace33cdSFred Isaman 	data->args.offset = 0;
14179ace33cdSFred Isaman 	data->args.count  = 0;
14189ace33cdSFred Isaman 	data->args.context = get_nfs_open_context(first->wb_context);
14199ace33cdSFred Isaman 	data->res.count   = 0;
14209ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
14219ace33cdSFred Isaman 	data->res.verf    = &data->verf;
14229ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
14239ace33cdSFred Isaman }
1424e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
14259ace33cdSFred Isaman 
1426e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1427a861a1e1SFred Isaman 		      struct pnfs_layout_segment *lseg)
142864bfeb49SFred Isaman {
142964bfeb49SFred Isaman 	struct nfs_page *req;
143064bfeb49SFred Isaman 
143164bfeb49SFred Isaman 	while (!list_empty(page_list)) {
143264bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
143364bfeb49SFred Isaman 		nfs_list_remove_request(req);
1434a861a1e1SFred Isaman 		nfs_mark_request_commit(req, lseg);
143564bfeb49SFred Isaman 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
143664bfeb49SFred Isaman 		dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
143764bfeb49SFred Isaman 			     BDI_RECLAIMABLE);
143864bfeb49SFred Isaman 		nfs_clear_page_tag_locked(req);
143964bfeb49SFred Isaman 	}
144064bfeb49SFred Isaman }
1441e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
144264bfeb49SFred Isaman 
14439ace33cdSFred Isaman /*
14441da177e4SLinus Torvalds  * Commit dirty pages
14451da177e4SLinus Torvalds  */
14461da177e4SLinus Torvalds static int
144740859d7eSChuck Lever nfs_commit_list(struct inode *inode, struct list_head *head, int how)
14481da177e4SLinus Torvalds {
14491da177e4SLinus Torvalds 	struct nfs_write_data	*data;
14501da177e4SLinus Torvalds 
1451c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds 	if (!data)
14541da177e4SLinus Torvalds 		goto out_bad;
14551da177e4SLinus Torvalds 
14561da177e4SLinus Torvalds 	/* Set up the argument struct */
1457988b6dceSFred Isaman 	nfs_init_commit(data, head, NULL);
14589ace33cdSFred Isaman 	return nfs_initiate_commit(data, NFS_CLIENT(inode), data->mds_ops, how);
14591da177e4SLinus Torvalds  out_bad:
1460a861a1e1SFred Isaman 	nfs_retry_commit(head, NULL);
146171d0a611STrond Myklebust 	nfs_commit_clear_lock(NFS_I(inode));
14621da177e4SLinus Torvalds 	return -ENOMEM;
14631da177e4SLinus Torvalds }
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds /*
14661da177e4SLinus Torvalds  * COMMIT call returned
14671da177e4SLinus Torvalds  */
1468788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
14691da177e4SLinus Torvalds {
1470963d8fe5STrond Myklebust 	struct nfs_write_data	*data = calldata;
14711da177e4SLinus Torvalds 
1472a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
14731da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
14741da177e4SLinus Torvalds 
1475788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1476c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
1477c9d8f89dSTrond Myklebust }
1478c9d8f89dSTrond Myklebust 
1479e0c2b380SFred Isaman void nfs_commit_release_pages(struct nfs_write_data *data)
1480c9d8f89dSTrond Myklebust {
1481c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1482c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1483788e7a89STrond Myklebust 
14841da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
14851da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
14861da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1487e468bae9STrond Myklebust 		nfs_clear_request_commit(req);
14881da177e4SLinus Torvalds 
148948186c7dSChuck Lever 		dprintk("NFS:       commit (%s/%lld %d@%lld)",
14903d4ff43dSAl Viro 			req->wb_context->dentry->d_sb->s_id,
14913d4ff43dSAl Viro 			(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
14921da177e4SLinus Torvalds 			req->wb_bytes,
14931da177e4SLinus Torvalds 			(long long)req_offset(req));
1494c9d8f89dSTrond Myklebust 		if (status < 0) {
1495c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
14961da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1497c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
14981da177e4SLinus Torvalds 			goto next;
14991da177e4SLinus Torvalds 		}
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
15021da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
15031da177e4SLinus Torvalds 		if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
15041da177e4SLinus Torvalds 			/* We have a match */
15051da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
15061da177e4SLinus Torvalds 			dprintk(" OK\n");
15071da177e4SLinus Torvalds 			goto next;
15081da177e4SLinus Torvalds 		}
15091da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
15101da177e4SLinus Torvalds 		dprintk(" mismatch\n");
15116d884e8fSFred 		nfs_mark_request_dirty(req);
15121da177e4SLinus Torvalds 	next:
15139fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
15141da177e4SLinus Torvalds 	}
15155917ce84SFred Isaman }
1516e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_release_pages);
15175917ce84SFred Isaman 
15185917ce84SFred Isaman static void nfs_commit_release(void *calldata)
15195917ce84SFred Isaman {
15205917ce84SFred Isaman 	struct nfs_write_data *data = calldata;
15215917ce84SFred Isaman 
15225917ce84SFred Isaman 	nfs_commit_release_pages(data);
152371d0a611STrond Myklebust 	nfs_commit_clear_lock(NFS_I(data->inode));
1524c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
15251da177e4SLinus Torvalds }
1526788e7a89STrond Myklebust 
1527788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
152821d9a851SAndy Adamson #if defined(CONFIG_NFS_V4_1)
152921d9a851SAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
153021d9a851SAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1531788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1532788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1533788e7a89STrond Myklebust };
15341da177e4SLinus Torvalds 
1535b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
15361da177e4SLinus Torvalds {
15371da177e4SLinus Torvalds 	LIST_HEAD(head);
153871d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1539b8413f98STrond Myklebust 	int res;
15401da177e4SLinus Torvalds 
1541b8413f98STrond Myklebust 	res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1542b8413f98STrond Myklebust 	if (res <= 0)
1543c5efa5fcSTrond Myklebust 		goto out_mark_dirty;
15443da28eb1STrond Myklebust 	res = nfs_scan_commit(inode, &head, 0, 0);
15451da177e4SLinus Torvalds 	if (res) {
1546a861a1e1SFred Isaman 		int error;
1547a861a1e1SFred Isaman 
1548a861a1e1SFred Isaman 		error = pnfs_commit_list(inode, &head, how);
1549a861a1e1SFred Isaman 		if (error == PNFS_NOT_ATTEMPTED)
1550a861a1e1SFred Isaman 			error = nfs_commit_list(inode, &head, how);
15511da177e4SLinus Torvalds 		if (error < 0)
15521da177e4SLinus Torvalds 			return error;
1553b8413f98STrond Myklebust 		if (!may_wait)
1554b8413f98STrond Myklebust 			goto out_mark_dirty;
1555b8413f98STrond Myklebust 		error = wait_on_bit(&NFS_I(inode)->flags,
1556b8413f98STrond Myklebust 				NFS_INO_COMMIT,
155771d0a611STrond Myklebust 				nfs_wait_bit_killable,
155871d0a611STrond Myklebust 				TASK_KILLABLE);
1559b8413f98STrond Myklebust 		if (error < 0)
1560b8413f98STrond Myklebust 			return error;
156171d0a611STrond Myklebust 	} else
156271d0a611STrond Myklebust 		nfs_commit_clear_lock(NFS_I(inode));
1563c5efa5fcSTrond Myklebust 	return res;
1564c5efa5fcSTrond Myklebust 	/* Note: If we exit without ensuring that the commit is complete,
1565c5efa5fcSTrond Myklebust 	 * we must mark the inode as dirty. Otherwise, future calls to
1566c5efa5fcSTrond Myklebust 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1567c5efa5fcSTrond Myklebust 	 * that the data is on the disk.
1568c5efa5fcSTrond Myklebust 	 */
1569c5efa5fcSTrond Myklebust out_mark_dirty:
1570c5efa5fcSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
15711da177e4SLinus Torvalds 	return res;
15721da177e4SLinus Torvalds }
15738fc795f7STrond Myklebust 
15748fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
15758fc795f7STrond Myklebust {
1576420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1577420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1578420e3646STrond Myklebust 	int ret = 0;
15798fc795f7STrond Myklebust 
15803236c3e1SJeff Layton 	/* no commits means nothing needs to be done */
15813236c3e1SJeff Layton 	if (!nfsi->ncommit)
15823236c3e1SJeff Layton 		return ret;
15833236c3e1SJeff Layton 
1584a00dd6c0SJeff Layton 	if (wbc->sync_mode == WB_SYNC_NONE) {
1585a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1586a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1587420e3646STrond Myklebust 		 */
1588a00dd6c0SJeff Layton 		if (nfsi->ncommit <= (nfsi->npages >> 1))
1589420e3646STrond Myklebust 			goto out_mark_dirty;
1590420e3646STrond Myklebust 
1591a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1592420e3646STrond Myklebust 		flags = 0;
1593a00dd6c0SJeff Layton 	}
1594a00dd6c0SJeff Layton 
1595420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1596420e3646STrond Myklebust 	if (ret >= 0) {
1597420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1598420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1599420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1600420e3646STrond Myklebust 			else
1601420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1602420e3646STrond Myklebust 		}
16038fc795f7STrond Myklebust 		return 0;
1604420e3646STrond Myklebust 	}
1605420e3646STrond Myklebust out_mark_dirty:
16068fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
16078fc795f7STrond Myklebust 	return ret;
16088fc795f7STrond Myklebust }
1609c63c7b05STrond Myklebust #else
16108fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
16118fc795f7STrond Myklebust {
16128fc795f7STrond Myklebust 	return 0;
16138fc795f7STrond Myklebust }
16141da177e4SLinus Torvalds #endif
16151da177e4SLinus Torvalds 
16168fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
16178fc795f7STrond Myklebust {
1618863a3c6cSAndy Adamson 	int ret;
1619863a3c6cSAndy Adamson 
1620863a3c6cSAndy Adamson 	ret = nfs_commit_unstable_pages(inode, wbc);
1621863a3c6cSAndy Adamson 	if (ret >= 0 && test_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(inode)->flags)) {
1622ef311537SAndy Adamson 		int status;
1623ef311537SAndy Adamson 		bool sync = true;
1624863a3c6cSAndy Adamson 
1625846d5a09SWu Fengguang 		if (wbc->sync_mode == WB_SYNC_NONE)
1626ef311537SAndy Adamson 			sync = false;
1627863a3c6cSAndy Adamson 
1628863a3c6cSAndy Adamson 		status = pnfs_layoutcommit_inode(inode, sync);
1629863a3c6cSAndy Adamson 		if (status < 0)
1630863a3c6cSAndy Adamson 			return status;
1631863a3c6cSAndy Adamson 	}
1632863a3c6cSAndy Adamson 	return ret;
16338fc795f7STrond Myklebust }
16348fc795f7STrond Myklebust 
1635acdc53b2STrond Myklebust /*
1636acdc53b2STrond Myklebust  * flush the inode to disk.
1637acdc53b2STrond Myklebust  */
1638acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
163934901f70STrond Myklebust {
164034901f70STrond Myklebust 	struct writeback_control wbc = {
164172cb77f4STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
164234901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
1643d7fb1207STrond Myklebust 		.range_start = 0,
1644d7fb1207STrond Myklebust 		.range_end = LLONG_MAX,
164534901f70STrond Myklebust 	};
164634901f70STrond Myklebust 
1647acdc53b2STrond Myklebust 	return sync_inode(inode, &wbc);
16481c75950bSTrond Myklebust }
16491c75950bSTrond Myklebust 
16501b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
16511b3b4a1aSTrond Myklebust {
16521b3b4a1aSTrond Myklebust 	struct nfs_page *req;
16531b3b4a1aSTrond Myklebust 	int ret = 0;
16541b3b4a1aSTrond Myklebust 
16551b3b4a1aSTrond Myklebust 	BUG_ON(!PageLocked(page));
16561b3b4a1aSTrond Myklebust 	for (;;) {
1657ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
16581b3b4a1aSTrond Myklebust 		req = nfs_page_find_request(page);
16591b3b4a1aSTrond Myklebust 		if (req == NULL)
16601b3b4a1aSTrond Myklebust 			break;
16611b3b4a1aSTrond Myklebust 		if (nfs_lock_request_dontget(req)) {
16621b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
16631b3b4a1aSTrond Myklebust 			/*
16641b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
16651b3b4a1aSTrond Myklebust 			 * page as being dirty
16661b3b4a1aSTrond Myklebust 			 */
16671b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
16681b3b4a1aSTrond Myklebust 			nfs_unlock_request(req);
16691b3b4a1aSTrond Myklebust 			break;
16701b3b4a1aSTrond Myklebust 		}
16711b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
1672c9edda71STrond Myklebust 		nfs_release_request(req);
16731b3b4a1aSTrond Myklebust 		if (ret < 0)
1674c988950eSTrond Myklebust 			break;
16751b3b4a1aSTrond Myklebust 	}
16761b3b4a1aSTrond Myklebust 	return ret;
16771b3b4a1aSTrond Myklebust }
16781b3b4a1aSTrond Myklebust 
16791c75950bSTrond Myklebust /*
16801c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
16811c75950bSTrond Myklebust  */
16821c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
16831c75950bSTrond Myklebust {
16847f2f12d9STrond Myklebust 	loff_t range_start = page_offset(page);
16857f2f12d9STrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
16867f2f12d9STrond Myklebust 	struct writeback_control wbc = {
16877f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
16887f2f12d9STrond Myklebust 		.nr_to_write = 0,
16897f2f12d9STrond Myklebust 		.range_start = range_start,
16907f2f12d9STrond Myklebust 		.range_end = range_end,
16917f2f12d9STrond Myklebust 	};
16927f2f12d9STrond Myklebust 	int ret;
16937f2f12d9STrond Myklebust 
16940522f6adSTrond Myklebust 	for (;;) {
1695ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
16967f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
16977f2f12d9STrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
16987f2f12d9STrond Myklebust 			if (ret < 0)
16997f2f12d9STrond Myklebust 				goto out_error;
17000522f6adSTrond Myklebust 			continue;
17017f2f12d9STrond Myklebust 		}
17020522f6adSTrond Myklebust 		if (!PagePrivate(page))
17030522f6adSTrond Myklebust 			break;
17040522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
17057f2f12d9STrond Myklebust 		if (ret < 0)
17067f2f12d9STrond Myklebust 			goto out_error;
17077f2f12d9STrond Myklebust 	}
17087f2f12d9STrond Myklebust 	return 0;
17097f2f12d9STrond Myklebust out_error:
17107f2f12d9STrond Myklebust 	return ret;
17111c75950bSTrond Myklebust }
17121c75950bSTrond Myklebust 
1713074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
1714074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1715074cc1deSTrond Myklebust 		struct page *page)
1716074cc1deSTrond Myklebust {
17172da95652SJeff Layton 	/*
17182da95652SJeff Layton 	 * If PagePrivate is set, then the page is currently associated with
17192da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
17202da95652SJeff Layton 	 *
17212da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
17222da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
17232da95652SJeff Layton 	 *        the page lock.
17242da95652SJeff Layton 	 */
17252da95652SJeff Layton 	if (PagePrivate(page))
17262da95652SJeff Layton 		return -EBUSY;
1727074cc1deSTrond Myklebust 
1728074cc1deSTrond Myklebust 	nfs_fscache_release_page(page, GFP_KERNEL);
1729074cc1deSTrond Myklebust 
17302da95652SJeff Layton 	return migrate_page(mapping, newpage, page);
1731074cc1deSTrond Myklebust }
1732074cc1deSTrond Myklebust #endif
1733074cc1deSTrond Myklebust 
1734f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
17351da177e4SLinus Torvalds {
17361da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
17371da177e4SLinus Torvalds 					     sizeof(struct nfs_write_data),
17381da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
173920c2df83SPaul Mundt 					     NULL);
17401da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
17411da177e4SLinus Torvalds 		return -ENOMEM;
17421da177e4SLinus Torvalds 
174393d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
17441da177e4SLinus Torvalds 						     nfs_wdata_cachep);
17451da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
17461da177e4SLinus Torvalds 		return -ENOMEM;
17471da177e4SLinus Torvalds 
174893d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
17491da177e4SLinus Torvalds 						      nfs_wdata_cachep);
17501da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
17511da177e4SLinus Torvalds 		return -ENOMEM;
17521da177e4SLinus Torvalds 
175389a09141SPeter Zijlstra 	/*
175489a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
175589a09141SPeter Zijlstra 	 *
175689a09141SPeter Zijlstra 	 *  64MB:    8192k
175789a09141SPeter Zijlstra 	 * 128MB:   11585k
175889a09141SPeter Zijlstra 	 * 256MB:   16384k
175989a09141SPeter Zijlstra 	 * 512MB:   23170k
176089a09141SPeter Zijlstra 	 *   1GB:   32768k
176189a09141SPeter Zijlstra 	 *   2GB:   46340k
176289a09141SPeter Zijlstra 	 *   4GB:   65536k
176389a09141SPeter Zijlstra 	 *   8GB:   92681k
176489a09141SPeter Zijlstra 	 *  16GB:  131072k
176589a09141SPeter Zijlstra 	 *
176689a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
176789a09141SPeter Zijlstra 	 * Limit the default to 256M
176889a09141SPeter Zijlstra 	 */
176989a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
177089a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
177189a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
177289a09141SPeter Zijlstra 
17731da177e4SLinus Torvalds 	return 0;
17741da177e4SLinus Torvalds }
17751da177e4SLinus Torvalds 
1776266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
17771da177e4SLinus Torvalds {
17781da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
17791da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
17801a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
17811da177e4SLinus Torvalds }
17821da177e4SLinus Torvalds 
1783