xref: /openbmc/linux/fs/nfs/write.c (revision d6d6dc7c)
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>
23afeacc8cSPaul Gortmaker #include <linux/export.h>
243fcfab16SAndrew Morton 
251da177e4SLinus Torvalds #include <asm/uaccess.h>
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #include "delegation.h"
2849a70f27STrond Myklebust #include "internal.h"
2991d5b470SChuck Lever #include "iostat.h"
30def6ed7eSAndy Adamson #include "nfs4_fs.h"
31074cc1deSTrond Myklebust #include "fscache.h"
3294ad1c80SFred Isaman #include "pnfs.h"
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #define MIN_POOL_WRITE		(32)
371da177e4SLinus Torvalds #define MIN_POOL_COMMIT		(4)
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds /*
401da177e4SLinus Torvalds  * Local function declarations
411da177e4SLinus Torvalds  */
42c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc,
43c63c7b05STrond Myklebust 				  struct inode *inode, int ioflags);
44f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req);
45788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops;
46788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops;
47788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops;
481da177e4SLinus Torvalds 
49e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep;
503feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
511da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
521da177e4SLinus Torvalds 
53c9d8f89dSTrond Myklebust struct nfs_write_data *nfs_commitdata_alloc(void)
541da177e4SLinus Torvalds {
55e6b4f8daSChristoph Lameter 	struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
5640859d7eSChuck Lever 
571da177e4SLinus Torvalds 	if (p) {
581da177e4SLinus Torvalds 		memset(p, 0, sizeof(*p));
591da177e4SLinus Torvalds 		INIT_LIST_HEAD(&p->pages);
601da177e4SLinus Torvalds 	}
611da177e4SLinus Torvalds 	return p;
621da177e4SLinus Torvalds }
63e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
641da177e4SLinus Torvalds 
655e4424afSTrond Myklebust void nfs_commit_free(struct nfs_write_data *p)
661da177e4SLinus Torvalds {
6740859d7eSChuck Lever 	if (p && (p->pagevec != &p->page_array[0]))
6840859d7eSChuck Lever 		kfree(p->pagevec);
691da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
701da177e4SLinus Torvalds }
71e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free);
721da177e4SLinus Torvalds 
738d5658c9STrond Myklebust struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
743feb2d49STrond Myklebust {
75e6b4f8daSChristoph Lameter 	struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
763feb2d49STrond Myklebust 
773feb2d49STrond Myklebust 	if (p) {
783feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
793feb2d49STrond Myklebust 		INIT_LIST_HEAD(&p->pages);
80e9f7bee1STrond Myklebust 		p->npages = pagecount;
810d0b5cb3SChuck Lever 		if (pagecount <= ARRAY_SIZE(p->page_array))
820d0b5cb3SChuck Lever 			p->pagevec = p->page_array;
833feb2d49STrond Myklebust 		else {
840d0b5cb3SChuck Lever 			p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
850d0b5cb3SChuck Lever 			if (!p->pagevec) {
863feb2d49STrond Myklebust 				mempool_free(p, nfs_wdata_mempool);
873feb2d49STrond Myklebust 				p = NULL;
883feb2d49STrond Myklebust 			}
893feb2d49STrond Myklebust 		}
903feb2d49STrond Myklebust 	}
913feb2d49STrond Myklebust 	return p;
923feb2d49STrond Myklebust }
933feb2d49STrond Myklebust 
941ae88b2eSTrond Myklebust void nfs_writedata_free(struct nfs_write_data *p)
953feb2d49STrond Myklebust {
963feb2d49STrond Myklebust 	if (p && (p->pagevec != &p->page_array[0]))
973feb2d49STrond Myklebust 		kfree(p->pagevec);
983feb2d49STrond Myklebust 	mempool_free(p, nfs_wdata_mempool);
993feb2d49STrond Myklebust }
1003feb2d49STrond Myklebust 
101dce81290STrond Myklebust void nfs_writedata_release(struct nfs_write_data *wdata)
1021da177e4SLinus Torvalds {
1035053aa56SFred Isaman 	put_lseg(wdata->lseg);
104383ba719STrond Myklebust 	put_nfs_open_context(wdata->args.context);
1051da177e4SLinus Torvalds 	nfs_writedata_free(wdata);
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds 
1087b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
1097b159fc1STrond Myklebust {
1107b159fc1STrond Myklebust 	ctx->error = error;
1117b159fc1STrond Myklebust 	smp_wmb();
1127b159fc1STrond Myklebust 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
1137b159fc1STrond Myklebust }
1147b159fc1STrond Myklebust 
115277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request_locked(struct page *page)
116277459d2STrond Myklebust {
117277459d2STrond Myklebust 	struct nfs_page *req = NULL;
118277459d2STrond Myklebust 
119277459d2STrond Myklebust 	if (PagePrivate(page)) {
120277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
121277459d2STrond Myklebust 		if (req != NULL)
122c03b4024STrond Myklebust 			kref_get(&req->wb_kref);
123277459d2STrond Myklebust 	}
124277459d2STrond Myklebust 	return req;
125277459d2STrond Myklebust }
126277459d2STrond Myklebust 
127277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page)
128277459d2STrond Myklebust {
129587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
130277459d2STrond Myklebust 	struct nfs_page *req = NULL;
131277459d2STrond Myklebust 
132587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
133277459d2STrond Myklebust 	req = nfs_page_find_request_locked(page);
134587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
135277459d2STrond Myklebust 	return req;
136277459d2STrond Myklebust }
137277459d2STrond Myklebust 
1381da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1391da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
142a3d01454STrond Myklebust 	loff_t end, i_size;
143a3d01454STrond Myklebust 	pgoff_t end_index;
1441da177e4SLinus Torvalds 
145a3d01454STrond Myklebust 	spin_lock(&inode->i_lock);
146a3d01454STrond Myklebust 	i_size = i_size_read(inode);
147a3d01454STrond Myklebust 	end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
1481da177e4SLinus Torvalds 	if (i_size > 0 && page->index < end_index)
149a3d01454STrond Myklebust 		goto out;
1501da177e4SLinus Torvalds 	end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
1511da177e4SLinus Torvalds 	if (i_size >= end)
152a3d01454STrond Myklebust 		goto out;
1531da177e4SLinus Torvalds 	i_size_write(inode, end);
154a3d01454STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
155a3d01454STrond Myklebust out:
156a3d01454STrond Myklebust 	spin_unlock(&inode->i_lock);
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
159a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
160a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
161a301b777STrond Myklebust {
162a301b777STrond Myklebust 	SetPageError(page);
163a301b777STrond Myklebust 	nfs_zap_mapping(page->mapping->host, page->mapping);
164a301b777STrond Myklebust }
165a301b777STrond Myklebust 
1661da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
1671da177e4SLinus Torvalds  * covers the full page.
1681da177e4SLinus Torvalds  */
1691da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
1701da177e4SLinus Torvalds {
1711da177e4SLinus Torvalds 	if (PageUptodate(page))
1721da177e4SLinus Torvalds 		return;
1731da177e4SLinus Torvalds 	if (base != 0)
1741da177e4SLinus Torvalds 		return;
17549a70f27STrond Myklebust 	if (count != nfs_page_length(page))
1761da177e4SLinus Torvalds 		return;
1771da177e4SLinus Torvalds 	SetPageUptodate(page);
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
1811da177e4SLinus Torvalds {
1821da177e4SLinus Torvalds 	if (wbc->for_reclaim)
183c63c7b05STrond Myklebust 		return FLUSH_HIGHPRI | FLUSH_STABLE;
184b17621feSWu Fengguang 	if (wbc->for_kupdate || wbc->for_background)
185b31268acSTrond Myklebust 		return FLUSH_LOWPRI | FLUSH_COND_STABLE;
186b31268acSTrond Myklebust 	return FLUSH_COND_STABLE;
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds /*
19089a09141SPeter Zijlstra  * NFS congestion control
19189a09141SPeter Zijlstra  */
19289a09141SPeter Zijlstra 
19389a09141SPeter Zijlstra int nfs_congestion_kb;
19489a09141SPeter Zijlstra 
19589a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
19689a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
19789a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
19889a09141SPeter Zijlstra 
1995a6d41b3STrond Myklebust static int nfs_set_page_writeback(struct page *page)
20089a09141SPeter Zijlstra {
2015a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
2025a6d41b3STrond Myklebust 
2035a6d41b3STrond Myklebust 	if (!ret) {
20489a09141SPeter Zijlstra 		struct inode *inode = page->mapping->host;
20589a09141SPeter Zijlstra 		struct nfs_server *nfss = NFS_SERVER(inode);
20689a09141SPeter Zijlstra 
207a6305ddbSTrond Myklebust 		page_cache_get(page);
208277866a0SPeter Zijlstra 		if (atomic_long_inc_return(&nfss->writeback) >
2098aa7e847SJens Axboe 				NFS_CONGESTION_ON_THRESH) {
2108aa7e847SJens Axboe 			set_bdi_congested(&nfss->backing_dev_info,
2118aa7e847SJens Axboe 						BLK_RW_ASYNC);
2128aa7e847SJens Axboe 		}
21389a09141SPeter Zijlstra 	}
2145a6d41b3STrond Myklebust 	return ret;
21589a09141SPeter Zijlstra }
21689a09141SPeter Zijlstra 
21789a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page)
21889a09141SPeter Zijlstra {
21989a09141SPeter Zijlstra 	struct inode *inode = page->mapping->host;
22089a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
22189a09141SPeter Zijlstra 
22289a09141SPeter Zijlstra 	end_page_writeback(page);
223a6305ddbSTrond Myklebust 	page_cache_release(page);
224c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
2258aa7e847SJens Axboe 		clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
22689a09141SPeter Zijlstra }
22789a09141SPeter Zijlstra 
228cfb506e1STrond Myklebust static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock)
229e261f51fSTrond Myklebust {
230587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
231e261f51fSTrond Myklebust 	struct nfs_page *req;
232e261f51fSTrond Myklebust 	int ret;
233e261f51fSTrond Myklebust 
234587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
235e261f51fSTrond Myklebust 	for (;;) {
236e261f51fSTrond Myklebust 		req = nfs_page_find_request_locked(page);
237074cc1deSTrond Myklebust 		if (req == NULL)
238074cc1deSTrond Myklebust 			break;
2399994b62bSFred Isaman 		if (nfs_lock_request_dontget(req))
240e261f51fSTrond Myklebust 			break;
241e261f51fSTrond Myklebust 		/* Note: If we hold the page lock, as is the case in nfs_writepage,
2429994b62bSFred Isaman 		 *	 then the call to nfs_lock_request_dontget() will always
243e261f51fSTrond Myklebust 		 *	 succeed provided that someone hasn't already marked the
244e261f51fSTrond Myklebust 		 *	 request as dirty (in which case we don't care).
245e261f51fSTrond Myklebust 		 */
246587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
247cfb506e1STrond Myklebust 		if (!nonblock)
248e261f51fSTrond Myklebust 			ret = nfs_wait_on_request(req);
249cfb506e1STrond Myklebust 		else
250cfb506e1STrond Myklebust 			ret = -EAGAIN;
251e261f51fSTrond Myklebust 		nfs_release_request(req);
252e261f51fSTrond Myklebust 		if (ret != 0)
253074cc1deSTrond Myklebust 			return ERR_PTR(ret);
254587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
255e261f51fSTrond Myklebust 	}
256587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
257074cc1deSTrond Myklebust 	return req;
258612c9384STrond Myklebust }
259074cc1deSTrond Myklebust 
260074cc1deSTrond Myklebust /*
261074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
262074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
263074cc1deSTrond Myklebust  */
264074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
265cfb506e1STrond Myklebust 				struct page *page, bool nonblock)
266074cc1deSTrond Myklebust {
267074cc1deSTrond Myklebust 	struct nfs_page *req;
268074cc1deSTrond Myklebust 	int ret = 0;
269074cc1deSTrond Myklebust 
270cfb506e1STrond Myklebust 	req = nfs_find_and_lock_request(page, nonblock);
271074cc1deSTrond Myklebust 	if (!req)
272074cc1deSTrond Myklebust 		goto out;
273074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
274074cc1deSTrond Myklebust 	if (IS_ERR(req))
275074cc1deSTrond Myklebust 		goto out;
276074cc1deSTrond Myklebust 
277074cc1deSTrond Myklebust 	ret = nfs_set_page_writeback(page);
278074cc1deSTrond Myklebust 	BUG_ON(ret != 0);
279074cc1deSTrond Myklebust 	BUG_ON(test_bit(PG_CLEAN, &req->wb_flags));
280074cc1deSTrond Myklebust 
281f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
282f8512ad0SFred Isaman 		nfs_redirty_request(req);
283074cc1deSTrond Myklebust 		ret = pgio->pg_error;
284f8512ad0SFred Isaman 	}
285074cc1deSTrond Myklebust out:
286074cc1deSTrond Myklebust 	return ret;
287e261f51fSTrond Myklebust }
288e261f51fSTrond Myklebust 
289f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
290f758c885STrond Myklebust {
291f758c885STrond Myklebust 	struct inode *inode = page->mapping->host;
292cfb506e1STrond Myklebust 	int ret;
293f758c885STrond Myklebust 
294f758c885STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
295f758c885STrond Myklebust 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
296f758c885STrond Myklebust 
297f758c885STrond Myklebust 	nfs_pageio_cond_complete(pgio, page->index);
2981b430beeSWu Fengguang 	ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
299cfb506e1STrond Myklebust 	if (ret == -EAGAIN) {
300cfb506e1STrond Myklebust 		redirty_page_for_writepage(wbc, page);
301cfb506e1STrond Myklebust 		ret = 0;
302cfb506e1STrond Myklebust 	}
303cfb506e1STrond Myklebust 	return ret;
304f758c885STrond Myklebust }
305f758c885STrond Myklebust 
306e261f51fSTrond Myklebust /*
3071da177e4SLinus Torvalds  * Write an mmapped page to the server.
3081da177e4SLinus Torvalds  */
3094d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
3101da177e4SLinus Torvalds {
311f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
312e261f51fSTrond Myklebust 	int err;
3131da177e4SLinus Torvalds 
314f758c885STrond Myklebust 	nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
315f758c885STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
316f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
317f758c885STrond Myklebust 	if (err < 0)
3184d770ccfSTrond Myklebust 		return err;
319f758c885STrond Myklebust 	if (pgio.pg_error < 0)
320f758c885STrond Myklebust 		return pgio.pg_error;
321f758c885STrond Myklebust 	return 0;
3224d770ccfSTrond Myklebust }
3234d770ccfSTrond Myklebust 
3244d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
3254d770ccfSTrond Myklebust {
326f758c885STrond Myklebust 	int ret;
3274d770ccfSTrond Myklebust 
328f758c885STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
3291da177e4SLinus Torvalds 	unlock_page(page);
330f758c885STrond Myklebust 	return ret;
331f758c885STrond Myklebust }
332f758c885STrond Myklebust 
333f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
334f758c885STrond Myklebust {
335f758c885STrond Myklebust 	int ret;
336f758c885STrond Myklebust 
337f758c885STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
338f758c885STrond Myklebust 	unlock_page(page);
339f758c885STrond Myklebust 	return ret;
3401da177e4SLinus Torvalds }
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
34572cb77f4STrond Myklebust 	unsigned long *bitlock = &NFS_I(inode)->flags;
346c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
3471da177e4SLinus Torvalds 	int err;
3481da177e4SLinus Torvalds 
34972cb77f4STrond Myklebust 	/* Stop dirtying of new pages while we sync */
35072cb77f4STrond Myklebust 	err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
35172cb77f4STrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
35272cb77f4STrond Myklebust 	if (err)
35372cb77f4STrond Myklebust 		goto out_err;
35472cb77f4STrond Myklebust 
35591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
35691d5b470SChuck Lever 
357c63c7b05STrond Myklebust 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
358f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
359c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
36072cb77f4STrond Myklebust 
36172cb77f4STrond Myklebust 	clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
36272cb77f4STrond Myklebust 	smp_mb__after_clear_bit();
36372cb77f4STrond Myklebust 	wake_up_bit(bitlock, NFS_INO_FLUSHING);
36472cb77f4STrond Myklebust 
365f758c885STrond Myklebust 	if (err < 0)
36672cb77f4STrond Myklebust 		goto out_err;
36772cb77f4STrond Myklebust 	err = pgio.pg_error;
36872cb77f4STrond Myklebust 	if (err < 0)
36972cb77f4STrond Myklebust 		goto out_err;
370c63c7b05STrond Myklebust 	return 0;
37172cb77f4STrond Myklebust out_err:
37272cb77f4STrond Myklebust 	return err;
3731da177e4SLinus Torvalds }
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds /*
3761da177e4SLinus Torvalds  * Insert a write request into an inode
3771da177e4SLinus Torvalds  */
378d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
3791da177e4SLinus Torvalds {
3801da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
381e7d39069STrond Myklebust 
382e7d39069STrond Myklebust 	/* Lock the request! */
383e7d39069STrond Myklebust 	nfs_lock_request_dontget(req);
384e7d39069STrond Myklebust 
385e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
3864d65c520STrond Myklebust 	if (!nfsi->npages && nfs_have_delegation(inode, FMODE_WRITE))
387a9a4a87aSTrond Myklebust 		inode->i_version++;
3882df485a7STrond Myklebust 	set_bit(PG_MAPPED, &req->wb_flags);
389deb7d638STrond Myklebust 	SetPagePrivate(req->wb_page);
390277459d2STrond Myklebust 	set_page_private(req->wb_page, (unsigned long)req);
3911da177e4SLinus Torvalds 	nfsi->npages++;
392c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
393e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
3941da177e4SLinus Torvalds }
3951da177e4SLinus Torvalds 
396d6d6dc7cSFred Isaman static struct pnfs_layout_segment *nfs_clear_request_commit(struct nfs_page *req);
397d6d6dc7cSFred Isaman 
3981da177e4SLinus Torvalds /*
39989a09141SPeter Zijlstra  * Remove a write request from an inode
4001da177e4SLinus Torvalds  */
4011da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
4021da177e4SLinus Torvalds {
4033d4ff43dSAl Viro 	struct inode *inode = req->wb_context->dentry->d_inode;
4041da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
405d6d6dc7cSFred Isaman 	struct pnfs_layout_segment *lseg;
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	BUG_ON (!NFS_WBACK_BUSY(req));
4081da177e4SLinus Torvalds 
409587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
410d6d6dc7cSFred Isaman 	lseg = nfs_clear_request_commit(req);
411277459d2STrond Myklebust 	set_page_private(req->wb_page, 0);
412deb7d638STrond Myklebust 	ClearPagePrivate(req->wb_page);
4132df485a7STrond Myklebust 	clear_bit(PG_MAPPED, &req->wb_flags);
4141da177e4SLinus Torvalds 	nfsi->npages--;
415587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
416d6d6dc7cSFred Isaman 	put_lseg(lseg);
4171da177e4SLinus Torvalds 	nfs_release_request(req);
4181da177e4SLinus Torvalds }
4191da177e4SLinus Torvalds 
42061822ab5STrond Myklebust static void
4216d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
42261822ab5STrond Myklebust {
42361822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
42461822ab5STrond Myklebust }
42561822ab5STrond Myklebust 
4261da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
4271da177e4SLinus Torvalds /*
4281da177e4SLinus Torvalds  * Add a request to the inode's commit list.
4291da177e4SLinus Torvalds  */
4301da177e4SLinus Torvalds static void
431a861a1e1SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg)
4321da177e4SLinus Torvalds {
4333d4ff43dSAl Viro 	struct inode *inode = req->wb_context->dentry->d_inode;
4341da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
435d6d6dc7cSFred Isaman 	struct list_head *clist;
4361da177e4SLinus Torvalds 
437d6d6dc7cSFred Isaman 	clist = pnfs_choose_commit_list(req, lseg);
438587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
439e468bae9STrond Myklebust 	set_bit(PG_CLEAN, &(req)->wb_flags);
440d6d6dc7cSFred Isaman 	nfs_list_add_request(req, clist);
441ff778d02STrond Myklebust 	nfsi->ncommit++;
442587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
443fd39fc85SChristoph Lameter 	inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
444c9e51e41SPeter Zijlstra 	inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
445a1803044STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
4461da177e4SLinus Torvalds }
4478e821cadSTrond Myklebust 
448d6d6dc7cSFred Isaman static void
449d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page)
450e468bae9STrond Myklebust {
451e468bae9STrond Myklebust 	dec_zone_page_state(page, NR_UNSTABLE_NFS);
452e468bae9STrond Myklebust 	dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
453e468bae9STrond Myklebust }
454d6d6dc7cSFred Isaman 
455d6d6dc7cSFred Isaman static struct pnfs_layout_segment *
456d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req)
457d6d6dc7cSFred Isaman {
458d6d6dc7cSFred Isaman 	struct pnfs_layout_segment *lseg = NULL;
459d6d6dc7cSFred Isaman 
460d6d6dc7cSFred Isaman 	if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) {
461d6d6dc7cSFred Isaman 		nfs_clear_page_commit(req->wb_page);
462d6d6dc7cSFred Isaman 		lseg = pnfs_clear_request_commit(req);
463d6d6dc7cSFred Isaman 		NFS_I(req->wb_context->dentry->d_inode)->ncommit--;
464d6d6dc7cSFred Isaman 		list_del(&req->wb_list);
465d6d6dc7cSFred Isaman 	}
466d6d6dc7cSFred Isaman 	return lseg;
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 
498d6d6dc7cSFred Isaman static inline struct pnfs_layout_segment *
499e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
500e468bae9STrond Myklebust {
501d6d6dc7cSFred Isaman 	return NULL;
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 {
522d6d6dc7cSFred Isaman 	return nfsi->ncommit > 0;
523fb8a1f11STrond Myklebust }
524fb8a1f11STrond Myklebust 
525d6d6dc7cSFred Isaman /* i_lock held by caller */
526d6d6dc7cSFred Isaman int
527d6d6dc7cSFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst, int max)
528d6d6dc7cSFred Isaman {
529d6d6dc7cSFred Isaman 	struct nfs_page *req, *tmp;
530d6d6dc7cSFred Isaman 	int ret = 0;
531d6d6dc7cSFred Isaman 
532d6d6dc7cSFred Isaman 	list_for_each_entry_safe(req, tmp, src, wb_list) {
533d6d6dc7cSFred Isaman 		if (nfs_lock_request_dontget(req)) {
534d6d6dc7cSFred Isaman 			kref_get(&req->wb_kref);
535d6d6dc7cSFred Isaman 			list_move_tail(&req->wb_list, dst);
536d6d6dc7cSFred Isaman 			clear_bit(PG_CLEAN, &(req)->wb_flags);
537d6d6dc7cSFred Isaman 			ret++;
538d6d6dc7cSFred Isaman 			if (ret == max)
539d6d6dc7cSFred Isaman 				break;
540d6d6dc7cSFred Isaman 		}
541d6d6dc7cSFred Isaman 	}
542d6d6dc7cSFred Isaman 	return ret;
543d6d6dc7cSFred Isaman }
544d6d6dc7cSFred Isaman EXPORT_SYMBOL_GPL(nfs_scan_commit_list);
545d6d6dc7cSFred Isaman 
5461da177e4SLinus Torvalds /*
5471da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
5481da177e4SLinus Torvalds  * @inode: NFS inode to scan
5491da177e4SLinus Torvalds  * @dst: destination list
5501da177e4SLinus Torvalds  *
5511da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
5521da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
5531da177e4SLinus Torvalds  */
5541da177e4SLinus Torvalds static int
555d6d6dc7cSFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst)
5561da177e4SLinus Torvalds {
5571da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
558d6d6dc7cSFred Isaman 	int ret = 0;
559fb8a1f11STrond Myklebust 
5600d88f6e8SDave Chinner 	spin_lock(&inode->i_lock);
561d6d6dc7cSFred Isaman 	if (nfsi->ncommit > 0) {
562d6d6dc7cSFred Isaman 		int pnfs_ret;
563d6d6dc7cSFred Isaman 
564d6d6dc7cSFred Isaman 		ret = nfs_scan_commit_list(&nfsi->commit_list, dst, INT_MAX);
565d6d6dc7cSFred Isaman 		pnfs_ret = pnfs_scan_commit_lists(inode, INT_MAX - ret);
566d6d6dc7cSFred Isaman 		if (pnfs_ret) {
567d6d6dc7cSFred Isaman 			ret += pnfs_ret;
568d6d6dc7cSFred Isaman 			set_bit(NFS_INO_PNFS_COMMIT, &nfsi->flags);
569d6d6dc7cSFred Isaman 		}
570ff778d02STrond Myklebust 		nfsi->ncommit -= ret;
571d6d6dc7cSFred Isaman 	}
5720d88f6e8SDave Chinner 	spin_unlock(&inode->i_lock);
573ff778d02STrond Myklebust 	return ret;
5741da177e4SLinus Torvalds }
575d6d6dc7cSFred Isaman 
576c42de9ddSTrond Myklebust #else
577fb8a1f11STrond Myklebust static inline int nfs_need_commit(struct nfs_inode *nfsi)
578fb8a1f11STrond Myklebust {
579fb8a1f11STrond Myklebust 	return 0;
580fb8a1f11STrond Myklebust }
581fb8a1f11STrond Myklebust 
582d6d6dc7cSFred Isaman static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst)
583c42de9ddSTrond Myklebust {
584c42de9ddSTrond Myklebust 	return 0;
585c42de9ddSTrond Myklebust }
5861da177e4SLinus Torvalds #endif
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds /*
589e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
590e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
5911da177e4SLinus Torvalds  *
592e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
593e7d39069STrond Myklebust  * to disk.
5941da177e4SLinus Torvalds  */
595e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
596e7d39069STrond Myklebust 		struct page *page,
597e7d39069STrond Myklebust 		unsigned int offset,
598e7d39069STrond Myklebust 		unsigned int bytes)
5991da177e4SLinus Torvalds {
600e7d39069STrond Myklebust 	struct nfs_page *req;
601e7d39069STrond Myklebust 	unsigned int rqend;
602e7d39069STrond Myklebust 	unsigned int end;
6031da177e4SLinus Torvalds 	int error;
604d6d6dc7cSFred Isaman 	struct pnfs_layout_segment *lseg = NULL;
605277459d2STrond Myklebust 
606e7d39069STrond Myklebust 	if (!PagePrivate(page))
607e7d39069STrond Myklebust 		return NULL;
608e7d39069STrond Myklebust 
609e7d39069STrond Myklebust 	end = offset + bytes;
610e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
611e7d39069STrond Myklebust 
612e7d39069STrond Myklebust 	for (;;) {
613e7d39069STrond Myklebust 		req = nfs_page_find_request_locked(page);
614e7d39069STrond Myklebust 		if (req == NULL)
615e7d39069STrond Myklebust 			goto out_unlock;
616e7d39069STrond Myklebust 
617e7d39069STrond Myklebust 		rqend = req->wb_offset + req->wb_bytes;
618e7d39069STrond Myklebust 		/*
619e7d39069STrond Myklebust 		 * Tell the caller to flush out the request if
620e7d39069STrond Myklebust 		 * the offsets are non-contiguous.
621e7d39069STrond Myklebust 		 * Note: nfs_flush_incompatible() will already
622e7d39069STrond Myklebust 		 * have flushed out requests having wrong owners.
623e7d39069STrond Myklebust 		 */
624e468bae9STrond Myklebust 		if (offset > rqend
625e7d39069STrond Myklebust 		    || end < req->wb_offset)
626e7d39069STrond Myklebust 			goto out_flushme;
627e7d39069STrond Myklebust 
6289994b62bSFred Isaman 		if (nfs_lock_request_dontget(req))
629e7d39069STrond Myklebust 			break;
630e7d39069STrond Myklebust 
631e7d39069STrond Myklebust 		/* The request is locked, so wait and then retry */
632587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
6331da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
6341da177e4SLinus Torvalds 		nfs_release_request(req);
635e7d39069STrond Myklebust 		if (error != 0)
636e7d39069STrond Myklebust 			goto out_err;
637e7d39069STrond Myklebust 		spin_lock(&inode->i_lock);
6381da177e4SLinus Torvalds 	}
6391da177e4SLinus Torvalds 
640d6d6dc7cSFred Isaman 	lseg = nfs_clear_request_commit(req);
641e468bae9STrond Myklebust 
6421da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
6431da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
6441da177e4SLinus Torvalds 		req->wb_offset = offset;
6451da177e4SLinus Torvalds 		req->wb_pgbase = offset;
6461da177e4SLinus Torvalds 	}
6471da177e4SLinus Torvalds 	if (end > rqend)
6481da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
649e7d39069STrond Myklebust 	else
650e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
651e7d39069STrond Myklebust out_unlock:
652e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
653d6d6dc7cSFred Isaman 	put_lseg(lseg);
654e7d39069STrond Myklebust 	return req;
655e7d39069STrond Myklebust out_flushme:
656e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
657e7d39069STrond Myklebust 	nfs_release_request(req);
658e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
659e7d39069STrond Myklebust out_err:
660e7d39069STrond Myklebust 	return ERR_PTR(error);
661e7d39069STrond Myklebust }
6621da177e4SLinus Torvalds 
663e7d39069STrond Myklebust /*
664e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
665e7d39069STrond Myklebust  *
666e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
667e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
668e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
669e7d39069STrond Myklebust  */
670e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
671e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
672e7d39069STrond Myklebust {
673e7d39069STrond Myklebust 	struct inode *inode = page->mapping->host;
674e7d39069STrond Myklebust 	struct nfs_page	*req;
675e7d39069STrond Myklebust 
676e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
677e7d39069STrond Myklebust 	if (req != NULL)
678e7d39069STrond Myklebust 		goto out;
679e7d39069STrond Myklebust 	req = nfs_create_request(ctx, inode, page, offset, bytes);
680e7d39069STrond Myklebust 	if (IS_ERR(req))
681e7d39069STrond Myklebust 		goto out;
682d6d6dc7cSFred Isaman 	nfs_inode_add_request(inode, req);
683efc91ed0STrond Myklebust out:
68461e930a9STrond Myklebust 	return req;
6851da177e4SLinus Torvalds }
6861da177e4SLinus Torvalds 
687e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
688e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
689e7d39069STrond Myklebust {
690e7d39069STrond Myklebust 	struct nfs_page	*req;
691e7d39069STrond Myklebust 
692e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
693e7d39069STrond Myklebust 	if (IS_ERR(req))
694e7d39069STrond Myklebust 		return PTR_ERR(req);
695e7d39069STrond Myklebust 	/* Update file length */
696e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
697e7d39069STrond Myklebust 	nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
698a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
6999994b62bSFred Isaman 	nfs_unlock_request(req);
700e7d39069STrond Myklebust 	return 0;
701e7d39069STrond Myklebust }
702e7d39069STrond Myklebust 
7031da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
7041da177e4SLinus Torvalds {
705cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
7061da177e4SLinus Torvalds 	struct nfs_page	*req;
7071a54533eSTrond Myklebust 	int do_flush, status;
7081da177e4SLinus Torvalds 	/*
7091da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
7101da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
7111da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
7121da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
7131da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
7141da177e4SLinus Torvalds 	 * dropped page.
7151da177e4SLinus Torvalds 	 */
7161a54533eSTrond Myklebust 	do {
717277459d2STrond Myklebust 		req = nfs_page_find_request(page);
7181a54533eSTrond Myklebust 		if (req == NULL)
7191a54533eSTrond Myklebust 			return 0;
720f11ac8dbSTrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx ||
721f11ac8dbSTrond Myklebust 			req->wb_lock_context->lockowner != current->files ||
722f11ac8dbSTrond Myklebust 			req->wb_lock_context->pid != current->tgid;
7231da177e4SLinus Torvalds 		nfs_release_request(req);
7241a54533eSTrond Myklebust 		if (!do_flush)
7251a54533eSTrond Myklebust 			return 0;
726277459d2STrond Myklebust 		status = nfs_wb_page(page->mapping->host, page);
7271a54533eSTrond Myklebust 	} while (status == 0);
7281a54533eSTrond Myklebust 	return status;
7291da177e4SLinus Torvalds }
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds /*
7325d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
7335d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
7345d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
7355d47a356STrond Myklebust  */
7365d47a356STrond Myklebust static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
7375d47a356STrond Myklebust {
7385d47a356STrond Myklebust 	return PageUptodate(page) &&
7395d47a356STrond Myklebust 		!(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
7405d47a356STrond Myklebust }
7415d47a356STrond Myklebust 
7425d47a356STrond Myklebust /*
7431da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
7441da177e4SLinus Torvalds  *
7451da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
7461da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
7471da177e4SLinus Torvalds  */
7481da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
7491da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
7501da177e4SLinus Torvalds {
751cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
7521da177e4SLinus Torvalds 	struct inode	*inode = page->mapping->host;
7531da177e4SLinus Torvalds 	int		status = 0;
7541da177e4SLinus Torvalds 
75591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
75691d5b470SChuck Lever 
75748186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage(%s/%s %d@%lld)\n",
75801cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
75901cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name, count,
7600bbacc40SChuck Lever 		(long long)(page_offset(page) + offset));
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	/* If we're not using byte range locks, and we know the page
7635d47a356STrond Myklebust 	 * is up to date, it may be more efficient to extend the write
7645d47a356STrond Myklebust 	 * to cover the entire page in order to avoid fragmentation
7655d47a356STrond Myklebust 	 * inefficiencies.
7661da177e4SLinus Torvalds 	 */
7675d47a356STrond Myklebust 	if (nfs_write_pageuptodate(page, inode) &&
7685d47a356STrond Myklebust 			inode->i_flock == NULL &&
7696b2f3d1fSChristoph Hellwig 			!(file->f_flags & O_DSYNC)) {
77049a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
7711da177e4SLinus Torvalds 		offset = 0;
7721da177e4SLinus Torvalds 	}
7731da177e4SLinus Torvalds 
774e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
77503fa9e84STrond Myklebust 	if (status < 0)
77603fa9e84STrond Myklebust 		nfs_set_pageerror(page);
77759b7c05fSTrond Myklebust 	else
77859b7c05fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
7791da177e4SLinus Torvalds 
78048186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
7811da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
7821da177e4SLinus Torvalds 	return status;
7831da177e4SLinus Torvalds }
7841da177e4SLinus Torvalds 
785a861a1e1SFred Isaman static void nfs_writepage_release(struct nfs_page *req,
786a861a1e1SFred Isaman 				  struct nfs_write_data *data)
7871da177e4SLinus Torvalds {
788a6305ddbSTrond Myklebust 	struct page *page = req->wb_page;
7898e821cadSTrond Myklebust 
790a861a1e1SFred Isaman 	if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req, data))
7911da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
7929994b62bSFred Isaman 	nfs_unlock_request(req);
793a6305ddbSTrond Myklebust 	nfs_end_page_writeback(page);
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
7963ff7576dSTrond Myklebust static int flush_task_priority(int how)
7971da177e4SLinus Torvalds {
7981da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
7991da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
8001da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
8011da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
8021da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
8031da177e4SLinus Torvalds 	}
8041da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
8051da177e4SLinus Torvalds }
8061da177e4SLinus Torvalds 
807a69aef14SFred Isaman int nfs_initiate_write(struct nfs_write_data *data,
808d138d5d1SAndy Adamson 		       struct rpc_clnt *clnt,
809788e7a89STrond Myklebust 		       const struct rpc_call_ops *call_ops,
8101da177e4SLinus Torvalds 		       int how)
8111da177e4SLinus Torvalds {
812d138d5d1SAndy Adamson 	struct inode *inode = data->inode;
8133ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
81407737691STrond Myklebust 	struct rpc_task *task;
815bdc7f021STrond Myklebust 	struct rpc_message msg = {
816bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
817bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
818d138d5d1SAndy Adamson 		.rpc_cred = data->cred,
819bdc7f021STrond Myklebust 	};
82084115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
821d138d5d1SAndy Adamson 		.rpc_client = clnt,
82207737691STrond Myklebust 		.task = &data->task,
823bdc7f021STrond Myklebust 		.rpc_message = &msg,
82484115e1cSTrond Myklebust 		.callback_ops = call_ops,
82584115e1cSTrond Myklebust 		.callback_data = data,
826101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
8272c61be0aSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
8283ff7576dSTrond Myklebust 		.priority = priority,
82984115e1cSTrond Myklebust 	};
8302c61be0aSTrond Myklebust 	int ret = 0;
8311da177e4SLinus Torvalds 
832d138d5d1SAndy Adamson 	/* Set up the initial task struct.  */
833d138d5d1SAndy Adamson 	NFS_PROTO(inode)->write_setup(data, &msg);
834d138d5d1SAndy Adamson 
835d138d5d1SAndy Adamson 	dprintk("NFS: %5u initiated write call "
836d138d5d1SAndy Adamson 		"(req %s/%lld, %u bytes @ offset %llu)\n",
837d138d5d1SAndy Adamson 		data->task.tk_pid,
838d138d5d1SAndy Adamson 		inode->i_sb->s_id,
839d138d5d1SAndy Adamson 		(long long)NFS_FILEID(inode),
840d138d5d1SAndy Adamson 		data->args.count,
841d138d5d1SAndy Adamson 		(unsigned long long)data->args.offset);
842d138d5d1SAndy Adamson 
843d138d5d1SAndy Adamson 	task = rpc_run_task(&task_setup_data);
844d138d5d1SAndy Adamson 	if (IS_ERR(task)) {
845d138d5d1SAndy Adamson 		ret = PTR_ERR(task);
846d138d5d1SAndy Adamson 		goto out;
847d138d5d1SAndy Adamson 	}
848d138d5d1SAndy Adamson 	if (how & FLUSH_SYNC) {
849d138d5d1SAndy Adamson 		ret = rpc_wait_for_completion_task(task);
850d138d5d1SAndy Adamson 		if (ret == 0)
851d138d5d1SAndy Adamson 			ret = task->tk_status;
852d138d5d1SAndy Adamson 	}
853d138d5d1SAndy Adamson 	rpc_put_task(task);
854d138d5d1SAndy Adamson out:
855d138d5d1SAndy Adamson 	return ret;
856d138d5d1SAndy Adamson }
857a69aef14SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_write);
858d138d5d1SAndy Adamson 
859d138d5d1SAndy Adamson /*
860d138d5d1SAndy Adamson  * Set up the argument/result storage required for the RPC call.
861d138d5d1SAndy Adamson  */
8626e4efd56STrond Myklebust static void nfs_write_rpcsetup(struct nfs_page *req,
863d138d5d1SAndy Adamson 		struct nfs_write_data *data,
864d138d5d1SAndy Adamson 		unsigned int count, unsigned int offset,
865d138d5d1SAndy Adamson 		int how)
866d138d5d1SAndy Adamson {
8673d4ff43dSAl Viro 	struct inode *inode = req->wb_context->dentry->d_inode;
868d138d5d1SAndy Adamson 
8691da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
8701da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 	data->req = req;
8733d4ff43dSAl Viro 	data->inode = inode = req->wb_context->dentry->d_inode;
874d138d5d1SAndy Adamson 	data->cred = req->wb_context->cred;
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(inode);
8771da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
8782bea038cSBoaz Harrosh 	/* pnfs_set_layoutcommit needs this */
8792bea038cSBoaz Harrosh 	data->mds_offset = data->args.offset;
8801da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
8811da177e4SLinus Torvalds 	data->args.pages  = data->pagevec;
8821da177e4SLinus Torvalds 	data->args.count  = count;
883383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
884f11ac8dbSTrond Myklebust 	data->args.lock_context = req->wb_lock_context;
885bdc7f021STrond Myklebust 	data->args.stable  = NFS_UNSTABLE;
88687ed5eb4STrond Myklebust 	switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
88787ed5eb4STrond Myklebust 	case 0:
88887ed5eb4STrond Myklebust 		break;
88987ed5eb4STrond Myklebust 	case FLUSH_COND_STABLE:
89087ed5eb4STrond Myklebust 		if (nfs_need_commit(NFS_I(inode)))
89187ed5eb4STrond Myklebust 			break;
89287ed5eb4STrond Myklebust 	default:
893bdc7f021STrond Myklebust 		data->args.stable = NFS_FILE_SYNC;
894bdc7f021STrond Myklebust 	}
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
8971da177e4SLinus Torvalds 	data->res.count   = count;
8981da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
8990e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
9006e4efd56STrond Myklebust }
9011da177e4SLinus Torvalds 
9026e4efd56STrond Myklebust static int nfs_do_write(struct nfs_write_data *data,
9036e4efd56STrond Myklebust 		const struct rpc_call_ops *call_ops,
9046e4efd56STrond Myklebust 		int how)
9056e4efd56STrond Myklebust {
9065f00bcb3SStephen Rothwell 	struct inode *inode = data->args.context->dentry->d_inode;
9070382b744SAndy Adamson 
908d138d5d1SAndy Adamson 	return nfs_initiate_write(data, NFS_CLIENT(inode), call_ops, how);
9091da177e4SLinus Torvalds }
9101da177e4SLinus Torvalds 
911275acaafSTrond Myklebust static int nfs_do_multiple_writes(struct list_head *head,
912275acaafSTrond Myklebust 		const struct rpc_call_ops *call_ops,
913275acaafSTrond Myklebust 		int how)
914275acaafSTrond Myklebust {
915275acaafSTrond Myklebust 	struct nfs_write_data *data;
916275acaafSTrond Myklebust 	int ret = 0;
917275acaafSTrond Myklebust 
918275acaafSTrond Myklebust 	while (!list_empty(head)) {
919275acaafSTrond Myklebust 		int ret2;
920275acaafSTrond Myklebust 
921275acaafSTrond Myklebust 		data = list_entry(head->next, struct nfs_write_data, list);
922275acaafSTrond Myklebust 		list_del_init(&data->list);
923275acaafSTrond Myklebust 
924dce81290STrond Myklebust 		ret2 = nfs_do_write(data, call_ops, how);
925275acaafSTrond Myklebust 		 if (ret == 0)
926275acaafSTrond Myklebust 			 ret = ret2;
927275acaafSTrond Myklebust 	}
928275acaafSTrond Myklebust 	return ret;
929275acaafSTrond Myklebust }
930275acaafSTrond Myklebust 
9316d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
9326d884e8fSFred  * call this on each, which will prepare them to be retried on next
9336d884e8fSFred  * writeback using standard nfs.
9346d884e8fSFred  */
9356d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
9366d884e8fSFred {
937a6305ddbSTrond Myklebust 	struct page *page = req->wb_page;
938a6305ddbSTrond Myklebust 
9396d884e8fSFred 	nfs_mark_request_dirty(req);
9409994b62bSFred Isaman 	nfs_unlock_request(req);
941a6305ddbSTrond Myklebust 	nfs_end_page_writeback(page);
9426d884e8fSFred }
9436d884e8fSFred 
9441da177e4SLinus Torvalds /*
9451da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
9461da177e4SLinus Torvalds  * contiguous dirty area on one page.
9471da177e4SLinus Torvalds  */
948275acaafSTrond Myklebust static int nfs_flush_multi(struct nfs_pageio_descriptor *desc, struct list_head *res)
9491da177e4SLinus Torvalds {
950c76069bdSFred Isaman 	struct nfs_page *req = nfs_list_entry(desc->pg_list.next);
9511da177e4SLinus Torvalds 	struct page *page = req->wb_page;
9521da177e4SLinus Torvalds 	struct nfs_write_data *data;
953d097971dSTrond Myklebust 	size_t wsize = desc->pg_bsize, nbytes;
954e9f7bee1STrond Myklebust 	unsigned int offset;
9551da177e4SLinus Torvalds 	int requests = 0;
956dbae4c73STrond Myklebust 	int ret = 0;
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	nfs_list_remove_request(req);
9591da177e4SLinus Torvalds 
960b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
961b31268acSTrond Myklebust 	    (desc->pg_moreio || NFS_I(desc->pg_inode)->ncommit ||
962b31268acSTrond Myklebust 	     desc->pg_count > wsize))
963b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
964b31268acSTrond Myklebust 
965b31268acSTrond Myklebust 
966275acaafSTrond Myklebust 	offset = 0;
967c76069bdSFred Isaman 	nbytes = desc->pg_count;
968e9f7bee1STrond Myklebust 	do {
969e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
970e9f7bee1STrond Myklebust 
9718d5658c9STrond Myklebust 		data = nfs_writedata_alloc(1);
9721da177e4SLinus Torvalds 		if (!data)
9731da177e4SLinus Torvalds 			goto out_bad;
974275acaafSTrond Myklebust 		data->pagevec[0] = page;
975f13c3620STrond Myklebust 		nfs_write_rpcsetup(req, data, len, offset, desc->pg_ioflags);
976275acaafSTrond Myklebust 		list_add(&data->list, res);
9771da177e4SLinus Torvalds 		requests++;
978e9f7bee1STrond Myklebust 		nbytes -= len;
979275acaafSTrond Myklebust 		offset += len;
980e9f7bee1STrond Myklebust 	} while (nbytes != 0);
9811da177e4SLinus Torvalds 	atomic_set(&req->wb_complete, requests);
98250828d7eSTrond Myklebust 	desc->pg_rpc_callops = &nfs_write_partial_ops;
983dbae4c73STrond Myklebust 	return ret;
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds out_bad:
986275acaafSTrond Myklebust 	while (!list_empty(res)) {
987275acaafSTrond Myklebust 		data = list_entry(res->next, struct nfs_write_data, list);
9886e4efd56STrond Myklebust 		list_del(&data->list);
9890da2a4acSFred Isaman 		nfs_writedata_free(data);
9901da177e4SLinus Torvalds 	}
99161822ab5STrond Myklebust 	nfs_redirty_request(req);
9921da177e4SLinus Torvalds 	return -ENOMEM;
9931da177e4SLinus Torvalds }
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds /*
9961da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
9971da177e4SLinus Torvalds  * The page must have been locked by the caller.
9981da177e4SLinus Torvalds  *
9991da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
10001da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
10011da177e4SLinus Torvalds  * that has been written but not committed.
10021da177e4SLinus Torvalds  */
1003275acaafSTrond Myklebust static int nfs_flush_one(struct nfs_pageio_descriptor *desc, struct list_head *res)
10041da177e4SLinus Torvalds {
10051da177e4SLinus Torvalds 	struct nfs_page		*req;
10061da177e4SLinus Torvalds 	struct page		**pages;
10071da177e4SLinus Torvalds 	struct nfs_write_data	*data;
1008c76069bdSFred Isaman 	struct list_head *head = &desc->pg_list;
10093b609184SPeng Tao 	int ret = 0;
10101da177e4SLinus Torvalds 
1011c76069bdSFred Isaman 	data = nfs_writedata_alloc(nfs_page_array_len(desc->pg_base,
1012c76069bdSFred Isaman 						      desc->pg_count));
101344b83799SFred Isaman 	if (!data) {
101444b83799SFred Isaman 		while (!list_empty(head)) {
101544b83799SFred Isaman 			req = nfs_list_entry(head->next);
101644b83799SFred Isaman 			nfs_list_remove_request(req);
101744b83799SFred Isaman 			nfs_redirty_request(req);
101844b83799SFred Isaman 		}
101944b83799SFred Isaman 		ret = -ENOMEM;
102044b83799SFred Isaman 		goto out;
102144b83799SFred Isaman 	}
10221da177e4SLinus Torvalds 	pages = data->pagevec;
10231da177e4SLinus Torvalds 	while (!list_empty(head)) {
10241da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
10251da177e4SLinus Torvalds 		nfs_list_remove_request(req);
10261da177e4SLinus Torvalds 		nfs_list_add_request(req, &data->pages);
10271da177e4SLinus Torvalds 		*pages++ = req->wb_page;
10281da177e4SLinus Torvalds 	}
10291da177e4SLinus Torvalds 	req = nfs_list_entry(data->pages.next);
10301da177e4SLinus Torvalds 
1031b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1032b31268acSTrond Myklebust 	    (desc->pg_moreio || NFS_I(desc->pg_inode)->ncommit))
1033b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1034b31268acSTrond Myklebust 
10351da177e4SLinus Torvalds 	/* Set up the argument struct */
10366e4efd56STrond Myklebust 	nfs_write_rpcsetup(req, data, desc->pg_count, 0, desc->pg_ioflags);
1037275acaafSTrond Myklebust 	list_add(&data->list, res);
103850828d7eSTrond Myklebust 	desc->pg_rpc_callops = &nfs_write_full_ops;
103944b83799SFred Isaman out:
104044b83799SFred Isaman 	return ret;
10411da177e4SLinus Torvalds }
10421da177e4SLinus Torvalds 
1043dce81290STrond Myklebust int nfs_generic_flush(struct nfs_pageio_descriptor *desc, struct list_head *head)
1044dce81290STrond Myklebust {
1045dce81290STrond Myklebust 	if (desc->pg_bsize < PAGE_CACHE_SIZE)
1046dce81290STrond Myklebust 		return nfs_flush_multi(desc, head);
1047dce81290STrond Myklebust 	return nfs_flush_one(desc, head);
1048dce81290STrond Myklebust }
1049dce81290STrond Myklebust 
1050dce81290STrond Myklebust static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
10511751c363STrond Myklebust {
1052275acaafSTrond Myklebust 	LIST_HEAD(head);
1053275acaafSTrond Myklebust 	int ret;
1054275acaafSTrond Myklebust 
1055dce81290STrond Myklebust 	ret = nfs_generic_flush(desc, &head);
1056275acaafSTrond Myklebust 	if (ret == 0)
105750828d7eSTrond Myklebust 		ret = nfs_do_multiple_writes(&head, desc->pg_rpc_callops,
1058dce81290STrond Myklebust 				desc->pg_ioflags);
1059275acaafSTrond Myklebust 	return ret;
10601751c363STrond Myklebust }
10611751c363STrond Myklebust 
10621751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_write_ops = {
10631751c363STrond Myklebust 	.pg_test = nfs_generic_pg_test,
10641751c363STrond Myklebust 	.pg_doio = nfs_generic_pg_writepages,
10651751c363STrond Myklebust };
10661751c363STrond Myklebust 
1067e2fecb21STrond Myklebust void nfs_pageio_init_write_mds(struct nfs_pageio_descriptor *pgio,
10681751c363STrond Myklebust 				  struct inode *inode, int ioflags)
10691751c363STrond Myklebust {
10701751c363STrond Myklebust 	nfs_pageio_init(pgio, inode, &nfs_pageio_write_ops,
10711751c363STrond Myklebust 				NFS_SERVER(inode)->wsize, ioflags);
10721751c363STrond Myklebust }
10731751c363STrond Myklebust 
1074dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1075dce81290STrond Myklebust {
1076dce81290STrond Myklebust 	pgio->pg_ops = &nfs_pageio_write_ops;
1077dce81290STrond Myklebust 	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1078dce81290STrond Myklebust }
10791f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1080dce81290STrond Myklebust 
1081c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1082c63c7b05STrond Myklebust 				  struct inode *inode, int ioflags)
10831da177e4SLinus Torvalds {
10841751c363STrond Myklebust 	if (!pnfs_pageio_init_write(pgio, inode, ioflags))
10851751c363STrond Myklebust 		nfs_pageio_init_write_mds(pgio, inode, ioflags);
10861da177e4SLinus Torvalds }
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds /*
10891da177e4SLinus Torvalds  * Handle a write reply that flushed part of a page.
10901da177e4SLinus Torvalds  */
1091788e7a89STrond Myklebust static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
10921da177e4SLinus Torvalds {
1093788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
10941da177e4SLinus Torvalds 
109548186c7dSChuck Lever 	dprintk("NFS: %5u write(%s/%lld %d@%lld)",
109648186c7dSChuck Lever 		task->tk_pid,
10973d4ff43dSAl Viro 		data->req->wb_context->dentry->d_inode->i_sb->s_id,
109848186c7dSChuck Lever 		(long long)
10993d4ff43dSAl Viro 		  NFS_FILEID(data->req->wb_context->dentry->d_inode),
110048186c7dSChuck Lever 		data->req->wb_bytes, (long long)req_offset(data->req));
11011da177e4SLinus Torvalds 
1102c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
1103c9d8f89dSTrond Myklebust }
1104788e7a89STrond Myklebust 
1105c9d8f89dSTrond Myklebust static void nfs_writeback_release_partial(void *calldata)
1106c9d8f89dSTrond Myklebust {
1107c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1108c9d8f89dSTrond Myklebust 	struct nfs_page		*req = data->req;
1109c9d8f89dSTrond Myklebust 	struct page		*page = req->wb_page;
1110c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1111c9d8f89dSTrond Myklebust 
1112c9d8f89dSTrond Myklebust 	if (status < 0) {
1113a301b777STrond Myklebust 		nfs_set_pageerror(page);
1114c9d8f89dSTrond Myklebust 		nfs_context_set_write_error(req->wb_context, status);
1115c9d8f89dSTrond Myklebust 		dprintk(", error = %d\n", status);
11168e821cadSTrond Myklebust 		goto out;
11178e821cadSTrond Myklebust 	}
11188e821cadSTrond Myklebust 
11198e821cadSTrond Myklebust 	if (nfs_write_need_commit(data)) {
1120587142f8STrond Myklebust 		struct inode *inode = page->mapping->host;
11218e821cadSTrond Myklebust 
1122587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
11238e821cadSTrond Myklebust 		if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
11248e821cadSTrond Myklebust 			/* Do nothing we need to resend the writes */
11258e821cadSTrond Myklebust 		} else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
11261da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
11271da177e4SLinus Torvalds 			dprintk(" defer commit\n");
11281da177e4SLinus Torvalds 		} else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
11298e821cadSTrond Myklebust 			set_bit(PG_NEED_RESCHED, &req->wb_flags);
11308e821cadSTrond Myklebust 			clear_bit(PG_NEED_COMMIT, &req->wb_flags);
11311da177e4SLinus Torvalds 			dprintk(" server reboot detected\n");
11321da177e4SLinus Torvalds 		}
1133587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
11341da177e4SLinus Torvalds 	} else
11351da177e4SLinus Torvalds 		dprintk(" OK\n");
11361da177e4SLinus Torvalds 
11378e821cadSTrond Myklebust out:
11381da177e4SLinus Torvalds 	if (atomic_dec_and_test(&req->wb_complete))
1139a861a1e1SFred Isaman 		nfs_writepage_release(req, data);
1140c9d8f89dSTrond Myklebust 	nfs_writedata_release(calldata);
11411da177e4SLinus Torvalds }
11421da177e4SLinus Torvalds 
1143def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1)
1144def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata)
1145def6ed7eSAndy Adamson {
1146def6ed7eSAndy Adamson 	struct nfs_write_data *data = calldata;
1147def6ed7eSAndy Adamson 
1148035168abSTrond Myklebust 	if (nfs4_setup_sequence(NFS_SERVER(data->inode),
1149035168abSTrond Myklebust 				&data->args.seq_args,
11509d12b216STrond Myklebust 				&data->res.seq_res, task))
1151def6ed7eSAndy Adamson 		return;
1152def6ed7eSAndy Adamson 	rpc_call_start(task);
1153def6ed7eSAndy Adamson }
1154def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1155def6ed7eSAndy Adamson 
1156788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops = {
1157def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1)
1158def6ed7eSAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
1159def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1160788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_partial,
1161c9d8f89dSTrond Myklebust 	.rpc_release = nfs_writeback_release_partial,
1162788e7a89STrond Myklebust };
1163788e7a89STrond Myklebust 
11641da177e4SLinus Torvalds /*
11651da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
11661da177e4SLinus Torvalds  *
11671da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
11681da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
11691da177e4SLinus Torvalds  *	  as the page has a write request pending.
11701da177e4SLinus Torvalds  */
1171788e7a89STrond Myklebust static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
11721da177e4SLinus Torvalds {
1173788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
11741da177e4SLinus Torvalds 
1175c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
1176c9d8f89dSTrond Myklebust }
1177c9d8f89dSTrond Myklebust 
1178c9d8f89dSTrond Myklebust static void nfs_writeback_release_full(void *calldata)
1179c9d8f89dSTrond Myklebust {
1180c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1181e2fecb21STrond Myklebust 	int status = data->task.tk_status;
1182788e7a89STrond Myklebust 
11831da177e4SLinus Torvalds 	/* Update attributes as result of writeback. */
11841da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
1185c9d8f89dSTrond Myklebust 		struct nfs_page *req = nfs_list_entry(data->pages.next);
1186c9d8f89dSTrond Myklebust 		struct page *page = req->wb_page;
1187c9d8f89dSTrond Myklebust 
11881da177e4SLinus Torvalds 		nfs_list_remove_request(req);
11891da177e4SLinus Torvalds 
119048186c7dSChuck Lever 		dprintk("NFS: %5u write (%s/%lld %d@%lld)",
119148186c7dSChuck Lever 			data->task.tk_pid,
11923d4ff43dSAl Viro 			req->wb_context->dentry->d_inode->i_sb->s_id,
11933d4ff43dSAl Viro 			(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
11941da177e4SLinus Torvalds 			req->wb_bytes,
11951da177e4SLinus Torvalds 			(long long)req_offset(req));
11961da177e4SLinus Torvalds 
1197c9d8f89dSTrond Myklebust 		if (status < 0) {
1198a301b777STrond Myklebust 			nfs_set_pageerror(page);
1199c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
1200c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
12018e821cadSTrond Myklebust 			goto remove_request;
12021da177e4SLinus Torvalds 		}
12031da177e4SLinus Torvalds 
12048e821cadSTrond Myklebust 		if (nfs_write_need_commit(data)) {
12051da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
1206a861a1e1SFred Isaman 			nfs_mark_request_commit(req, data->lseg);
12071da177e4SLinus Torvalds 			dprintk(" marked for commit\n");
12088e821cadSTrond Myklebust 			goto next;
12098e821cadSTrond Myklebust 		}
12108e821cadSTrond Myklebust 		dprintk(" OK\n");
12118e821cadSTrond Myklebust remove_request:
12121da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
12131da177e4SLinus Torvalds 	next:
12149994b62bSFred Isaman 		nfs_unlock_request(req);
1215a6305ddbSTrond Myklebust 		nfs_end_page_writeback(page);
12161da177e4SLinus Torvalds 	}
1217c9d8f89dSTrond Myklebust 	nfs_writedata_release(calldata);
12181da177e4SLinus Torvalds }
12191da177e4SLinus Torvalds 
1220788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops = {
1221def6ed7eSAndy Adamson #if defined(CONFIG_NFS_V4_1)
1222def6ed7eSAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
1223def6ed7eSAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1224788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_full,
1225c9d8f89dSTrond Myklebust 	.rpc_release = nfs_writeback_release_full,
1226788e7a89STrond Myklebust };
1227788e7a89STrond Myklebust 
1228788e7a89STrond Myklebust 
12291da177e4SLinus Torvalds /*
12301da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
12311da177e4SLinus Torvalds  */
123213602896SFred Isaman void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
12331da177e4SLinus Torvalds {
12341da177e4SLinus Torvalds 	struct nfs_writeargs	*argp = &data->args;
12351da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
1236788e7a89STrond Myklebust 	int status;
12371da177e4SLinus Torvalds 
1238a3f565b1SChuck Lever 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
12391da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
12401da177e4SLinus Torvalds 
1241f551e44fSChuck Lever 	/*
1242f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1243f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1244f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1245f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1246f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1247f551e44fSChuck Lever 	 */
1248788e7a89STrond Myklebust 	status = NFS_PROTO(data->inode)->write_done(task, data);
1249788e7a89STrond Myklebust 	if (status != 0)
125013602896SFred Isaman 		return;
125191d5b470SChuck Lever 	nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
125291d5b470SChuck Lever 
12531da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
12541da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
12551da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
12561da177e4SLinus Torvalds 		 * commit data to stable storage even though we
12571da177e4SLinus Torvalds 		 * requested it.
12581da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
12591da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
12601da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
12611da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
12621da177e4SLinus Torvalds 		 */
12631da177e4SLinus Torvalds 		static unsigned long    complain;
12641da177e4SLinus Torvalds 
1265a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
12661da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
12671da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
12681da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
12692b72c9ccSRakib Mullick 				NFS_SERVER(data->inode)->nfs_client->cl_hostname,
12701da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
12711da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
12721da177e4SLinus Torvalds 		}
12731da177e4SLinus Torvalds 	}
12741da177e4SLinus Torvalds #endif
12751da177e4SLinus Torvalds 	/* Is this a short write? */
12761da177e4SLinus Torvalds 	if (task->tk_status >= 0 && resp->count < argp->count) {
12771da177e4SLinus Torvalds 		static unsigned long    complain;
12781da177e4SLinus Torvalds 
127991d5b470SChuck Lever 		nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
128091d5b470SChuck Lever 
12811da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
12821da177e4SLinus Torvalds 		if (resp->count != 0) {
12831da177e4SLinus Torvalds 			/* Was this an NFSv2 write or an NFSv3 stable write? */
12841da177e4SLinus Torvalds 			if (resp->verf->committed != NFS_UNSTABLE) {
12851da177e4SLinus Torvalds 				/* Resend from where the server left off */
1286a69aef14SFred Isaman 				data->mds_offset += resp->count;
12871da177e4SLinus Torvalds 				argp->offset += resp->count;
12881da177e4SLinus Torvalds 				argp->pgbase += resp->count;
12891da177e4SLinus Torvalds 				argp->count -= resp->count;
12901da177e4SLinus Torvalds 			} else {
12911da177e4SLinus Torvalds 				/* Resend as a stable write in order to avoid
12921da177e4SLinus Torvalds 				 * headaches in the case of a server crash.
12931da177e4SLinus Torvalds 				 */
12941da177e4SLinus Torvalds 				argp->stable = NFS_FILE_SYNC;
12951da177e4SLinus Torvalds 			}
1296d00c5d43STrond Myklebust 			rpc_restart_call_prepare(task);
129713602896SFred Isaman 			return;
12981da177e4SLinus Torvalds 		}
12991da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
13001da177e4SLinus Torvalds 			printk(KERN_WARNING
13011da177e4SLinus Torvalds 			       "NFS: Server wrote zero bytes, expected %u.\n",
13021da177e4SLinus Torvalds 					argp->count);
13031da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
13041da177e4SLinus Torvalds 		}
13051da177e4SLinus Torvalds 		/* Can't do anything about it except throw an error. */
13061da177e4SLinus Torvalds 		task->tk_status = -EIO;
13071da177e4SLinus Torvalds 	}
130813602896SFred Isaman 	return;
13091da177e4SLinus Torvalds }
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
131371d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
131471d0a611STrond Myklebust {
1315b8413f98STrond Myklebust 	int ret;
1316b8413f98STrond Myklebust 
131771d0a611STrond Myklebust 	if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
131871d0a611STrond Myklebust 		return 1;
1319b8413f98STrond Myklebust 	if (!may_wait)
132071d0a611STrond Myklebust 		return 0;
1321b8413f98STrond Myklebust 	ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1322b8413f98STrond Myklebust 				NFS_INO_COMMIT,
1323b8413f98STrond Myklebust 				nfs_wait_bit_killable,
1324b8413f98STrond Myklebust 				TASK_KILLABLE);
1325b8413f98STrond Myklebust 	return (ret < 0) ? ret : 1;
132671d0a611STrond Myklebust }
132771d0a611STrond Myklebust 
1328e0c2b380SFred Isaman void nfs_commit_clear_lock(struct nfs_inode *nfsi)
132971d0a611STrond Myklebust {
133071d0a611STrond Myklebust 	clear_bit(NFS_INO_COMMIT, &nfsi->flags);
133171d0a611STrond Myklebust 	smp_mb__after_clear_bit();
133271d0a611STrond Myklebust 	wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
133371d0a611STrond Myklebust }
1334e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_clear_lock);
133571d0a611STrond Myklebust 
1336e0c2b380SFred Isaman void nfs_commitdata_release(void *data)
13371da177e4SLinus Torvalds {
1338383ba719STrond Myklebust 	struct nfs_write_data *wdata = data;
1339383ba719STrond Myklebust 
1340988b6dceSFred Isaman 	put_lseg(wdata->lseg);
1341383ba719STrond Myklebust 	put_nfs_open_context(wdata->args.context);
13421da177e4SLinus Torvalds 	nfs_commit_free(wdata);
13431da177e4SLinus Torvalds }
1344e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
13451da177e4SLinus Torvalds 
1346e0c2b380SFred Isaman int nfs_initiate_commit(struct nfs_write_data *data, struct rpc_clnt *clnt,
13479ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
1348788e7a89STrond Myklebust 			int how)
13491da177e4SLinus Torvalds {
135007737691STrond Myklebust 	struct rpc_task *task;
13519ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1352bdc7f021STrond Myklebust 	struct rpc_message msg = {
1353bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1354bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
13559ace33cdSFred Isaman 		.rpc_cred = data->cred,
1356bdc7f021STrond Myklebust 	};
135784115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
135807737691STrond Myklebust 		.task = &data->task,
13599ace33cdSFred Isaman 		.rpc_client = clnt,
1360bdc7f021STrond Myklebust 		.rpc_message = &msg,
13619ace33cdSFred Isaman 		.callback_ops = call_ops,
136284115e1cSTrond Myklebust 		.callback_data = data,
1363101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
13642c61be0aSTrond Myklebust 		.flags = RPC_TASK_ASYNC,
13653ff7576dSTrond Myklebust 		.priority = priority,
136684115e1cSTrond Myklebust 	};
1367788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
13689ace33cdSFred Isaman 	NFS_PROTO(data->inode)->commit_setup(data, &msg);
13691da177e4SLinus Torvalds 
1370a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1371bdc7f021STrond Myklebust 
137207737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1373dbae4c73STrond Myklebust 	if (IS_ERR(task))
1374dbae4c73STrond Myklebust 		return PTR_ERR(task);
1375d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1376d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
137707737691STrond Myklebust 	rpc_put_task(task);
1378dbae4c73STrond Myklebust 	return 0;
13791da177e4SLinus Torvalds }
1380e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds /*
13839ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
13849ace33cdSFred Isaman  */
1385e0c2b380SFred Isaman void nfs_init_commit(struct nfs_write_data *data,
1386988b6dceSFred Isaman 			    struct list_head *head,
1387988b6dceSFred Isaman 			    struct pnfs_layout_segment *lseg)
13889ace33cdSFred Isaman {
13899ace33cdSFred Isaman 	struct nfs_page *first = nfs_list_entry(head->next);
13903d4ff43dSAl Viro 	struct inode *inode = first->wb_context->dentry->d_inode;
13919ace33cdSFred Isaman 
13929ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
13939ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
13949ace33cdSFred Isaman 
13959ace33cdSFred Isaman 	list_splice_init(head, &data->pages);
13969ace33cdSFred Isaman 
13979ace33cdSFred Isaman 	data->inode	  = inode;
13989ace33cdSFred Isaman 	data->cred	  = first->wb_context->cred;
1399988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
14009ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
14019ace33cdSFred Isaman 
14029ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
14039ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
14049ace33cdSFred Isaman 	data->args.offset = 0;
14059ace33cdSFred Isaman 	data->args.count  = 0;
14069ace33cdSFred Isaman 	data->args.context = get_nfs_open_context(first->wb_context);
14079ace33cdSFred Isaman 	data->res.count   = 0;
14089ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
14099ace33cdSFred Isaman 	data->res.verf    = &data->verf;
14109ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
14119ace33cdSFred Isaman }
1412e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
14139ace33cdSFred Isaman 
1414e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1415a861a1e1SFred Isaman 		      struct pnfs_layout_segment *lseg)
141664bfeb49SFred Isaman {
141764bfeb49SFred Isaman 	struct nfs_page *req;
141864bfeb49SFred Isaman 
141964bfeb49SFred Isaman 	while (!list_empty(page_list)) {
142064bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
142164bfeb49SFred Isaman 		nfs_list_remove_request(req);
1422a861a1e1SFred Isaman 		nfs_mark_request_commit(req, lseg);
142364bfeb49SFred Isaman 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
142464bfeb49SFred Isaman 		dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
142564bfeb49SFred Isaman 			     BDI_RECLAIMABLE);
14269994b62bSFred Isaman 		nfs_unlock_request(req);
142764bfeb49SFred Isaman 	}
142864bfeb49SFred Isaman }
1429e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
143064bfeb49SFred Isaman 
14319ace33cdSFred Isaman /*
14321da177e4SLinus Torvalds  * Commit dirty pages
14331da177e4SLinus Torvalds  */
14341da177e4SLinus Torvalds static int
143540859d7eSChuck Lever nfs_commit_list(struct inode *inode, struct list_head *head, int how)
14361da177e4SLinus Torvalds {
14371da177e4SLinus Torvalds 	struct nfs_write_data	*data;
14381da177e4SLinus Torvalds 
1439c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds 	if (!data)
14421da177e4SLinus Torvalds 		goto out_bad;
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds 	/* Set up the argument struct */
1445988b6dceSFred Isaman 	nfs_init_commit(data, head, NULL);
14469ace33cdSFred Isaman 	return nfs_initiate_commit(data, NFS_CLIENT(inode), data->mds_ops, how);
14471da177e4SLinus Torvalds  out_bad:
1448a861a1e1SFred Isaman 	nfs_retry_commit(head, NULL);
144971d0a611STrond Myklebust 	nfs_commit_clear_lock(NFS_I(inode));
14501da177e4SLinus Torvalds 	return -ENOMEM;
14511da177e4SLinus Torvalds }
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds /*
14541da177e4SLinus Torvalds  * COMMIT call returned
14551da177e4SLinus Torvalds  */
1456788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
14571da177e4SLinus Torvalds {
1458963d8fe5STrond Myklebust 	struct nfs_write_data	*data = calldata;
14591da177e4SLinus Torvalds 
1460a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
14611da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
14621da177e4SLinus Torvalds 
1463788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1464c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
1465c9d8f89dSTrond Myklebust }
1466c9d8f89dSTrond Myklebust 
1467e0c2b380SFred Isaman void nfs_commit_release_pages(struct nfs_write_data *data)
1468c9d8f89dSTrond Myklebust {
1469c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1470c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1471788e7a89STrond Myklebust 
14721da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
14731da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
14741da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1475d6d6dc7cSFred Isaman 		nfs_clear_page_commit(req->wb_page);
14761da177e4SLinus Torvalds 
147748186c7dSChuck Lever 		dprintk("NFS:       commit (%s/%lld %d@%lld)",
14783d4ff43dSAl Viro 			req->wb_context->dentry->d_sb->s_id,
14793d4ff43dSAl Viro 			(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
14801da177e4SLinus Torvalds 			req->wb_bytes,
14811da177e4SLinus Torvalds 			(long long)req_offset(req));
1482c9d8f89dSTrond Myklebust 		if (status < 0) {
1483c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
14841da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1485c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
14861da177e4SLinus Torvalds 			goto next;
14871da177e4SLinus Torvalds 		}
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
14901da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
14911da177e4SLinus Torvalds 		if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
14921da177e4SLinus Torvalds 			/* We have a match */
14931da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
14941da177e4SLinus Torvalds 			dprintk(" OK\n");
14951da177e4SLinus Torvalds 			goto next;
14961da177e4SLinus Torvalds 		}
14971da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
14981da177e4SLinus Torvalds 		dprintk(" mismatch\n");
14996d884e8fSFred 		nfs_mark_request_dirty(req);
15001da177e4SLinus Torvalds 	next:
15019994b62bSFred Isaman 		nfs_unlock_request(req);
15021da177e4SLinus Torvalds 	}
15035917ce84SFred Isaman }
1504e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_release_pages);
15055917ce84SFred Isaman 
15065917ce84SFred Isaman static void nfs_commit_release(void *calldata)
15075917ce84SFred Isaman {
15085917ce84SFred Isaman 	struct nfs_write_data *data = calldata;
15095917ce84SFred Isaman 
15105917ce84SFred Isaman 	nfs_commit_release_pages(data);
151171d0a611STrond Myklebust 	nfs_commit_clear_lock(NFS_I(data->inode));
1512c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
15131da177e4SLinus Torvalds }
1514788e7a89STrond Myklebust 
1515788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
151621d9a851SAndy Adamson #if defined(CONFIG_NFS_V4_1)
151721d9a851SAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
151821d9a851SAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1519788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1520788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1521788e7a89STrond Myklebust };
15221da177e4SLinus Torvalds 
1523b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
15241da177e4SLinus Torvalds {
15251da177e4SLinus Torvalds 	LIST_HEAD(head);
152671d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1527b8413f98STrond Myklebust 	int res;
15281da177e4SLinus Torvalds 
1529b8413f98STrond Myklebust 	res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1530b8413f98STrond Myklebust 	if (res <= 0)
1531c5efa5fcSTrond Myklebust 		goto out_mark_dirty;
1532d6d6dc7cSFred Isaman 	res = nfs_scan_commit(inode, &head);
15331da177e4SLinus Torvalds 	if (res) {
1534a861a1e1SFred Isaman 		int error;
1535a861a1e1SFred Isaman 
1536a861a1e1SFred Isaman 		error = pnfs_commit_list(inode, &head, how);
1537a861a1e1SFred Isaman 		if (error == PNFS_NOT_ATTEMPTED)
1538a861a1e1SFred Isaman 			error = nfs_commit_list(inode, &head, how);
15391da177e4SLinus Torvalds 		if (error < 0)
15401da177e4SLinus Torvalds 			return error;
1541b8413f98STrond Myklebust 		if (!may_wait)
1542b8413f98STrond Myklebust 			goto out_mark_dirty;
1543b8413f98STrond Myklebust 		error = wait_on_bit(&NFS_I(inode)->flags,
1544b8413f98STrond Myklebust 				NFS_INO_COMMIT,
154571d0a611STrond Myklebust 				nfs_wait_bit_killable,
154671d0a611STrond Myklebust 				TASK_KILLABLE);
1547b8413f98STrond Myklebust 		if (error < 0)
1548b8413f98STrond Myklebust 			return error;
154971d0a611STrond Myklebust 	} else
155071d0a611STrond Myklebust 		nfs_commit_clear_lock(NFS_I(inode));
1551c5efa5fcSTrond Myklebust 	return res;
1552c5efa5fcSTrond Myklebust 	/* Note: If we exit without ensuring that the commit is complete,
1553c5efa5fcSTrond Myklebust 	 * we must mark the inode as dirty. Otherwise, future calls to
1554c5efa5fcSTrond Myklebust 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1555c5efa5fcSTrond Myklebust 	 * that the data is on the disk.
1556c5efa5fcSTrond Myklebust 	 */
1557c5efa5fcSTrond Myklebust out_mark_dirty:
1558c5efa5fcSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
15591da177e4SLinus Torvalds 	return res;
15601da177e4SLinus Torvalds }
15618fc795f7STrond Myklebust 
15628fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
15638fc795f7STrond Myklebust {
1564420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1565420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1566420e3646STrond Myklebust 	int ret = 0;
15678fc795f7STrond Myklebust 
15683236c3e1SJeff Layton 	/* no commits means nothing needs to be done */
15693236c3e1SJeff Layton 	if (!nfsi->ncommit)
15703236c3e1SJeff Layton 		return ret;
15713236c3e1SJeff Layton 
1572a00dd6c0SJeff Layton 	if (wbc->sync_mode == WB_SYNC_NONE) {
1573a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1574a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1575420e3646STrond Myklebust 		 */
1576a00dd6c0SJeff Layton 		if (nfsi->ncommit <= (nfsi->npages >> 1))
1577420e3646STrond Myklebust 			goto out_mark_dirty;
1578420e3646STrond Myklebust 
1579a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1580420e3646STrond Myklebust 		flags = 0;
1581a00dd6c0SJeff Layton 	}
1582a00dd6c0SJeff Layton 
1583420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1584420e3646STrond Myklebust 	if (ret >= 0) {
1585420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1586420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1587420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1588420e3646STrond Myklebust 			else
1589420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1590420e3646STrond Myklebust 		}
15918fc795f7STrond Myklebust 		return 0;
1592420e3646STrond Myklebust 	}
1593420e3646STrond Myklebust out_mark_dirty:
15948fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
15958fc795f7STrond Myklebust 	return ret;
15968fc795f7STrond Myklebust }
1597c63c7b05STrond Myklebust #else
15988fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
15998fc795f7STrond Myklebust {
16008fc795f7STrond Myklebust 	return 0;
16018fc795f7STrond Myklebust }
16021da177e4SLinus Torvalds #endif
16031da177e4SLinus Torvalds 
16048fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
16058fc795f7STrond Myklebust {
1606863a3c6cSAndy Adamson 	int ret;
1607863a3c6cSAndy Adamson 
1608863a3c6cSAndy Adamson 	ret = nfs_commit_unstable_pages(inode, wbc);
1609863a3c6cSAndy Adamson 	if (ret >= 0 && test_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(inode)->flags)) {
1610ef311537SAndy Adamson 		int status;
1611ef311537SAndy Adamson 		bool sync = true;
1612863a3c6cSAndy Adamson 
1613846d5a09SWu Fengguang 		if (wbc->sync_mode == WB_SYNC_NONE)
1614ef311537SAndy Adamson 			sync = false;
1615863a3c6cSAndy Adamson 
1616863a3c6cSAndy Adamson 		status = pnfs_layoutcommit_inode(inode, sync);
1617863a3c6cSAndy Adamson 		if (status < 0)
1618863a3c6cSAndy Adamson 			return status;
1619863a3c6cSAndy Adamson 	}
1620863a3c6cSAndy Adamson 	return ret;
16218fc795f7STrond Myklebust }
16228fc795f7STrond Myklebust 
1623acdc53b2STrond Myklebust /*
1624acdc53b2STrond Myklebust  * flush the inode to disk.
1625acdc53b2STrond Myklebust  */
1626acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
162734901f70STrond Myklebust {
162834901f70STrond Myklebust 	struct writeback_control wbc = {
162972cb77f4STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
163034901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
1631d7fb1207STrond Myklebust 		.range_start = 0,
1632d7fb1207STrond Myklebust 		.range_end = LLONG_MAX,
163334901f70STrond Myklebust 	};
163434901f70STrond Myklebust 
1635acdc53b2STrond Myklebust 	return sync_inode(inode, &wbc);
16361c75950bSTrond Myklebust }
16371c75950bSTrond Myklebust 
16381b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
16391b3b4a1aSTrond Myklebust {
16401b3b4a1aSTrond Myklebust 	struct nfs_page *req;
16411b3b4a1aSTrond Myklebust 	int ret = 0;
16421b3b4a1aSTrond Myklebust 
16431b3b4a1aSTrond Myklebust 	BUG_ON(!PageLocked(page));
16441b3b4a1aSTrond Myklebust 	for (;;) {
1645ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
16461b3b4a1aSTrond Myklebust 		req = nfs_page_find_request(page);
16471b3b4a1aSTrond Myklebust 		if (req == NULL)
16481b3b4a1aSTrond Myklebust 			break;
16491b3b4a1aSTrond Myklebust 		if (nfs_lock_request_dontget(req)) {
16501b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
16511b3b4a1aSTrond Myklebust 			/*
16521b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
16531b3b4a1aSTrond Myklebust 			 * page as being dirty
16541b3b4a1aSTrond Myklebust 			 */
16551b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
16561b3b4a1aSTrond Myklebust 			nfs_unlock_request(req);
16571b3b4a1aSTrond Myklebust 			break;
16581b3b4a1aSTrond Myklebust 		}
16591b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
1660c9edda71STrond Myklebust 		nfs_release_request(req);
16611b3b4a1aSTrond Myklebust 		if (ret < 0)
1662c988950eSTrond Myklebust 			break;
16631b3b4a1aSTrond Myklebust 	}
16641b3b4a1aSTrond Myklebust 	return ret;
16651b3b4a1aSTrond Myklebust }
16661b3b4a1aSTrond Myklebust 
16671c75950bSTrond Myklebust /*
16681c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
16691c75950bSTrond Myklebust  */
16701c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
16711c75950bSTrond Myklebust {
16727f2f12d9STrond Myklebust 	loff_t range_start = page_offset(page);
16737f2f12d9STrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
16747f2f12d9STrond Myklebust 	struct writeback_control wbc = {
16757f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
16767f2f12d9STrond Myklebust 		.nr_to_write = 0,
16777f2f12d9STrond Myklebust 		.range_start = range_start,
16787f2f12d9STrond Myklebust 		.range_end = range_end,
16797f2f12d9STrond Myklebust 	};
16807f2f12d9STrond Myklebust 	int ret;
16817f2f12d9STrond Myklebust 
16820522f6adSTrond Myklebust 	for (;;) {
1683ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
16847f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
16857f2f12d9STrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
16867f2f12d9STrond Myklebust 			if (ret < 0)
16877f2f12d9STrond Myklebust 				goto out_error;
16880522f6adSTrond Myklebust 			continue;
16897f2f12d9STrond Myklebust 		}
16900522f6adSTrond Myklebust 		if (!PagePrivate(page))
16910522f6adSTrond Myklebust 			break;
16920522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
16937f2f12d9STrond Myklebust 		if (ret < 0)
16947f2f12d9STrond Myklebust 			goto out_error;
16957f2f12d9STrond Myklebust 	}
16967f2f12d9STrond Myklebust 	return 0;
16977f2f12d9STrond Myklebust out_error:
16987f2f12d9STrond Myklebust 	return ret;
16991c75950bSTrond Myklebust }
17001c75950bSTrond Myklebust 
1701074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
1702074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1703a6bc32b8SMel Gorman 		struct page *page, enum migrate_mode mode)
1704074cc1deSTrond Myklebust {
17052da95652SJeff Layton 	/*
17062da95652SJeff Layton 	 * If PagePrivate is set, then the page is currently associated with
17072da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
17082da95652SJeff Layton 	 *
17092da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
17102da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
17112da95652SJeff Layton 	 *        the page lock.
17122da95652SJeff Layton 	 */
17132da95652SJeff Layton 	if (PagePrivate(page))
17142da95652SJeff Layton 		return -EBUSY;
1715074cc1deSTrond Myklebust 
1716074cc1deSTrond Myklebust 	nfs_fscache_release_page(page, GFP_KERNEL);
1717074cc1deSTrond Myklebust 
1718a6bc32b8SMel Gorman 	return migrate_page(mapping, newpage, page, mode);
1719074cc1deSTrond Myklebust }
1720074cc1deSTrond Myklebust #endif
1721074cc1deSTrond Myklebust 
1722f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
17231da177e4SLinus Torvalds {
17241da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
17251da177e4SLinus Torvalds 					     sizeof(struct nfs_write_data),
17261da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
172720c2df83SPaul Mundt 					     NULL);
17281da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
17291da177e4SLinus Torvalds 		return -ENOMEM;
17301da177e4SLinus Torvalds 
173193d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
17321da177e4SLinus Torvalds 						     nfs_wdata_cachep);
17331da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
17341da177e4SLinus Torvalds 		return -ENOMEM;
17351da177e4SLinus Torvalds 
173693d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
17371da177e4SLinus Torvalds 						      nfs_wdata_cachep);
17381da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
17391da177e4SLinus Torvalds 		return -ENOMEM;
17401da177e4SLinus Torvalds 
174189a09141SPeter Zijlstra 	/*
174289a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
174389a09141SPeter Zijlstra 	 *
174489a09141SPeter Zijlstra 	 *  64MB:    8192k
174589a09141SPeter Zijlstra 	 * 128MB:   11585k
174689a09141SPeter Zijlstra 	 * 256MB:   16384k
174789a09141SPeter Zijlstra 	 * 512MB:   23170k
174889a09141SPeter Zijlstra 	 *   1GB:   32768k
174989a09141SPeter Zijlstra 	 *   2GB:   46340k
175089a09141SPeter Zijlstra 	 *   4GB:   65536k
175189a09141SPeter Zijlstra 	 *   8GB:   92681k
175289a09141SPeter Zijlstra 	 *  16GB:  131072k
175389a09141SPeter Zijlstra 	 *
175489a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
175589a09141SPeter Zijlstra 	 * Limit the default to 256M
175689a09141SPeter Zijlstra 	 */
175789a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
175889a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
175989a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
176089a09141SPeter Zijlstra 
17611da177e4SLinus Torvalds 	return 0;
17621da177e4SLinus Torvalds }
17631da177e4SLinus Torvalds 
1764266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
17651da177e4SLinus Torvalds {
17661da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
17671da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
17681a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
17691da177e4SLinus Torvalds }
17701da177e4SLinus Torvalds 
1771