xref: /linux/fs/nfs/write.c (revision 2bfc6e566daa8386c9cffef2f7de17fc330d3835)
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 
34f4ce1299STrond Myklebust #include "nfstrace.h"
35f4ce1299STrond Myklebust 
361da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #define MIN_POOL_WRITE		(32)
391da177e4SLinus Torvalds #define MIN_POOL_COMMIT		(4)
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds /*
421da177e4SLinus Torvalds  * Local function declarations
431da177e4SLinus Torvalds  */
44f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req);
45788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops;
46061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops;
47f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops;
484a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops;
491da177e4SLinus Torvalds 
50e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep;
513feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
520b7c0153SFred Isaman static struct kmem_cache *nfs_cdata_cachep;
531da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
541da177e4SLinus Torvalds 
550b7c0153SFred Isaman struct nfs_commit_data *nfs_commitdata_alloc(void)
561da177e4SLinus Torvalds {
57192e501bSMel Gorman 	struct nfs_commit_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOIO);
5840859d7eSChuck Lever 
591da177e4SLinus Torvalds 	if (p) {
601da177e4SLinus Torvalds 		memset(p, 0, sizeof(*p));
611da177e4SLinus Torvalds 		INIT_LIST_HEAD(&p->pages);
621da177e4SLinus Torvalds 	}
631da177e4SLinus Torvalds 	return p;
641da177e4SLinus Torvalds }
65e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
661da177e4SLinus Torvalds 
670b7c0153SFred Isaman void nfs_commit_free(struct nfs_commit_data *p)
681da177e4SLinus Torvalds {
691da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
701da177e4SLinus Torvalds }
71e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free);
721da177e4SLinus Torvalds 
734a0de55cSAnna Schumaker static struct nfs_rw_header *nfs_writehdr_alloc(void)
743feb2d49STrond Myklebust {
75c0752cdfSAnna Schumaker 	struct nfs_rw_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOIO);
763feb2d49STrond Myklebust 
774a0de55cSAnna Schumaker 	if (p)
783feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
793feb2d49STrond Myklebust 	return p;
803feb2d49STrond Myklebust }
813feb2d49STrond Myklebust 
824a0de55cSAnna Schumaker static void nfs_writehdr_free(struct nfs_rw_header *whdr)
833feb2d49STrond Myklebust {
84cd841605SFred Isaman 	mempool_free(whdr, nfs_wdata_mempool);
853feb2d49STrond Myklebust }
863feb2d49STrond Myklebust 
877b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
887b159fc1STrond Myklebust {
897b159fc1STrond Myklebust 	ctx->error = error;
907b159fc1STrond Myklebust 	smp_wmb();
917b159fc1STrond Myklebust 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
927b159fc1STrond Myklebust }
937b159fc1STrond Myklebust 
9429418aa4SMel Gorman static struct nfs_page *
9529418aa4SMel Gorman nfs_page_find_request_locked(struct nfs_inode *nfsi, struct page *page)
96277459d2STrond Myklebust {
97277459d2STrond Myklebust 	struct nfs_page *req = NULL;
98277459d2STrond Myklebust 
9929418aa4SMel Gorman 	if (PagePrivate(page))
100277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
10129418aa4SMel Gorman 	else if (unlikely(PageSwapCache(page))) {
10229418aa4SMel Gorman 		struct nfs_page *freq, *t;
10329418aa4SMel Gorman 
10429418aa4SMel Gorman 		/* Linearly search the commit list for the correct req */
10529418aa4SMel Gorman 		list_for_each_entry_safe(freq, t, &nfsi->commit_info.list, wb_list) {
10629418aa4SMel Gorman 			if (freq->wb_page == page) {
10729418aa4SMel Gorman 				req = freq;
10829418aa4SMel Gorman 				break;
109277459d2STrond Myklebust 			}
11029418aa4SMel Gorman 		}
11129418aa4SMel Gorman 	}
11229418aa4SMel Gorman 
11329418aa4SMel Gorman 	if (req)
11429418aa4SMel Gorman 		kref_get(&req->wb_kref);
11529418aa4SMel Gorman 
116277459d2STrond Myklebust 	return req;
117277459d2STrond Myklebust }
118277459d2STrond Myklebust 
119277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page)
120277459d2STrond Myklebust {
121d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
122277459d2STrond Myklebust 	struct nfs_page *req = NULL;
123277459d2STrond Myklebust 
124587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
12529418aa4SMel Gorman 	req = nfs_page_find_request_locked(NFS_I(inode), page);
126587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
127277459d2STrond Myklebust 	return req;
128277459d2STrond Myklebust }
129277459d2STrond Myklebust 
1301da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1311da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1321da177e4SLinus Torvalds {
133d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
134a3d01454STrond Myklebust 	loff_t end, i_size;
135a3d01454STrond Myklebust 	pgoff_t end_index;
1361da177e4SLinus Torvalds 
137a3d01454STrond Myklebust 	spin_lock(&inode->i_lock);
138a3d01454STrond Myklebust 	i_size = i_size_read(inode);
139a3d01454STrond Myklebust 	end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
140d56b4ddfSMel Gorman 	if (i_size > 0 && page_file_index(page) < end_index)
141a3d01454STrond Myklebust 		goto out;
142d56b4ddfSMel Gorman 	end = page_file_offset(page) + ((loff_t)offset+count);
1431da177e4SLinus Torvalds 	if (i_size >= end)
144a3d01454STrond Myklebust 		goto out;
1451da177e4SLinus Torvalds 	i_size_write(inode, end);
146a3d01454STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
147a3d01454STrond Myklebust out:
148a3d01454STrond Myklebust 	spin_unlock(&inode->i_lock);
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
151a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
152a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
153a301b777STrond Myklebust {
154d56b4ddfSMel Gorman 	nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page));
155a301b777STrond Myklebust }
156a301b777STrond Myklebust 
1571da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
1581da177e4SLinus Torvalds  * covers the full page.
1591da177e4SLinus Torvalds  */
1601da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
1611da177e4SLinus Torvalds {
1621da177e4SLinus Torvalds 	if (PageUptodate(page))
1631da177e4SLinus Torvalds 		return;
1641da177e4SLinus Torvalds 	if (base != 0)
1651da177e4SLinus Torvalds 		return;
16649a70f27STrond Myklebust 	if (count != nfs_page_length(page))
1671da177e4SLinus Torvalds 		return;
1681da177e4SLinus Torvalds 	SetPageUptodate(page);
1691da177e4SLinus Torvalds }
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
1721da177e4SLinus Torvalds {
1731da177e4SLinus Torvalds 	if (wbc->for_reclaim)
174c63c7b05STrond Myklebust 		return FLUSH_HIGHPRI | FLUSH_STABLE;
175b17621feSWu Fengguang 	if (wbc->for_kupdate || wbc->for_background)
176b31268acSTrond Myklebust 		return FLUSH_LOWPRI | FLUSH_COND_STABLE;
177b31268acSTrond Myklebust 	return FLUSH_COND_STABLE;
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds /*
18189a09141SPeter Zijlstra  * NFS congestion control
18289a09141SPeter Zijlstra  */
18389a09141SPeter Zijlstra 
18489a09141SPeter Zijlstra int nfs_congestion_kb;
18589a09141SPeter Zijlstra 
18689a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
18789a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
18889a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
18989a09141SPeter Zijlstra 
190deed85e7STrond Myklebust static void nfs_set_page_writeback(struct page *page)
19189a09141SPeter Zijlstra {
192deed85e7STrond Myklebust 	struct nfs_server *nfss = NFS_SERVER(page_file_mapping(page)->host);
1935a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
1945a6d41b3STrond Myklebust 
195deed85e7STrond Myklebust 	WARN_ON_ONCE(ret != 0);
19689a09141SPeter Zijlstra 
197277866a0SPeter Zijlstra 	if (atomic_long_inc_return(&nfss->writeback) >
1988aa7e847SJens Axboe 			NFS_CONGESTION_ON_THRESH) {
1998aa7e847SJens Axboe 		set_bdi_congested(&nfss->backing_dev_info,
2008aa7e847SJens Axboe 					BLK_RW_ASYNC);
2018aa7e847SJens Axboe 	}
20289a09141SPeter Zijlstra }
20389a09141SPeter Zijlstra 
20489a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page)
20589a09141SPeter Zijlstra {
206d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
20789a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
20889a09141SPeter Zijlstra 
20989a09141SPeter Zijlstra 	end_page_writeback(page);
210c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
2118aa7e847SJens Axboe 		clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
21289a09141SPeter Zijlstra }
21389a09141SPeter Zijlstra 
214cfb506e1STrond Myklebust static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock)
215e261f51fSTrond Myklebust {
216d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
217e261f51fSTrond Myklebust 	struct nfs_page *req;
218e261f51fSTrond Myklebust 	int ret;
219e261f51fSTrond Myklebust 
220587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
221e261f51fSTrond Myklebust 	for (;;) {
22229418aa4SMel Gorman 		req = nfs_page_find_request_locked(NFS_I(inode), page);
223074cc1deSTrond Myklebust 		if (req == NULL)
224074cc1deSTrond Myklebust 			break;
2257ad84aa9STrond Myklebust 		if (nfs_lock_request(req))
226e261f51fSTrond Myklebust 			break;
227e261f51fSTrond Myklebust 		/* Note: If we hold the page lock, as is the case in nfs_writepage,
2287ad84aa9STrond Myklebust 		 *	 then the call to nfs_lock_request() will always
229e261f51fSTrond Myklebust 		 *	 succeed provided that someone hasn't already marked the
230e261f51fSTrond Myklebust 		 *	 request as dirty (in which case we don't care).
231e261f51fSTrond Myklebust 		 */
232587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
233cfb506e1STrond Myklebust 		if (!nonblock)
234e261f51fSTrond Myklebust 			ret = nfs_wait_on_request(req);
235cfb506e1STrond Myklebust 		else
236cfb506e1STrond Myklebust 			ret = -EAGAIN;
237e261f51fSTrond Myklebust 		nfs_release_request(req);
238e261f51fSTrond Myklebust 		if (ret != 0)
239074cc1deSTrond Myklebust 			return ERR_PTR(ret);
240587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
241e261f51fSTrond Myklebust 	}
242587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
243074cc1deSTrond Myklebust 	return req;
244612c9384STrond Myklebust }
245074cc1deSTrond Myklebust 
246074cc1deSTrond Myklebust /*
247074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
248074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
249074cc1deSTrond Myklebust  */
250074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
251cfb506e1STrond Myklebust 				struct page *page, bool nonblock)
252074cc1deSTrond Myklebust {
253074cc1deSTrond Myklebust 	struct nfs_page *req;
254074cc1deSTrond Myklebust 	int ret = 0;
255074cc1deSTrond Myklebust 
256cfb506e1STrond Myklebust 	req = nfs_find_and_lock_request(page, nonblock);
257074cc1deSTrond Myklebust 	if (!req)
258074cc1deSTrond Myklebust 		goto out;
259074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
260074cc1deSTrond Myklebust 	if (IS_ERR(req))
261074cc1deSTrond Myklebust 		goto out;
262074cc1deSTrond Myklebust 
263deed85e7STrond Myklebust 	nfs_set_page_writeback(page);
264deed85e7STrond Myklebust 	WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
265074cc1deSTrond Myklebust 
266deed85e7STrond Myklebust 	ret = 0;
267f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
268f8512ad0SFred Isaman 		nfs_redirty_request(req);
269074cc1deSTrond Myklebust 		ret = pgio->pg_error;
270f8512ad0SFred Isaman 	}
271074cc1deSTrond Myklebust out:
272074cc1deSTrond Myklebust 	return ret;
273e261f51fSTrond Myklebust }
274e261f51fSTrond Myklebust 
275f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
276f758c885STrond Myklebust {
277d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
278cfb506e1STrond Myklebust 	int ret;
279f758c885STrond Myklebust 
280f758c885STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
281f758c885STrond Myklebust 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
282f758c885STrond Myklebust 
283d56b4ddfSMel Gorman 	nfs_pageio_cond_complete(pgio, page_file_index(page));
2841b430beeSWu Fengguang 	ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
285cfb506e1STrond Myklebust 	if (ret == -EAGAIN) {
286cfb506e1STrond Myklebust 		redirty_page_for_writepage(wbc, page);
287cfb506e1STrond Myklebust 		ret = 0;
288cfb506e1STrond Myklebust 	}
289cfb506e1STrond Myklebust 	return ret;
290f758c885STrond Myklebust }
291f758c885STrond Myklebust 
292e261f51fSTrond Myklebust /*
2931da177e4SLinus Torvalds  * Write an mmapped page to the server.
2941da177e4SLinus Torvalds  */
2954d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
2961da177e4SLinus Torvalds {
297f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
298e261f51fSTrond Myklebust 	int err;
2991da177e4SLinus Torvalds 
300a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc),
301a20c93e3SChristoph Hellwig 				false, &nfs_async_write_completion_ops);
302f758c885STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
303f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
304f758c885STrond Myklebust 	if (err < 0)
3054d770ccfSTrond Myklebust 		return err;
306f758c885STrond Myklebust 	if (pgio.pg_error < 0)
307f758c885STrond Myklebust 		return pgio.pg_error;
308f758c885STrond Myklebust 	return 0;
3094d770ccfSTrond Myklebust }
3104d770ccfSTrond Myklebust 
3114d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
3124d770ccfSTrond Myklebust {
313f758c885STrond Myklebust 	int ret;
3144d770ccfSTrond Myklebust 
315f758c885STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
3161da177e4SLinus Torvalds 	unlock_page(page);
317f758c885STrond Myklebust 	return ret;
318f758c885STrond Myklebust }
319f758c885STrond Myklebust 
320f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
321f758c885STrond Myklebust {
322f758c885STrond Myklebust 	int ret;
323f758c885STrond Myklebust 
324f758c885STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
325f758c885STrond Myklebust 	unlock_page(page);
326f758c885STrond Myklebust 	return ret;
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3301da177e4SLinus Torvalds {
3311da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
33272cb77f4STrond Myklebust 	unsigned long *bitlock = &NFS_I(inode)->flags;
333c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
3341da177e4SLinus Torvalds 	int err;
3351da177e4SLinus Torvalds 
33672cb77f4STrond Myklebust 	/* Stop dirtying of new pages while we sync */
33772cb77f4STrond Myklebust 	err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
33872cb77f4STrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
33972cb77f4STrond Myklebust 	if (err)
34072cb77f4STrond Myklebust 		goto out_err;
34172cb77f4STrond Myklebust 
34291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
34391d5b470SChuck Lever 
344a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false,
345a20c93e3SChristoph Hellwig 				&nfs_async_write_completion_ops);
346f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
347c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
34872cb77f4STrond Myklebust 
34972cb77f4STrond Myklebust 	clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
35072cb77f4STrond Myklebust 	smp_mb__after_clear_bit();
35172cb77f4STrond Myklebust 	wake_up_bit(bitlock, NFS_INO_FLUSHING);
35272cb77f4STrond Myklebust 
353f758c885STrond Myklebust 	if (err < 0)
35472cb77f4STrond Myklebust 		goto out_err;
35572cb77f4STrond Myklebust 	err = pgio.pg_error;
35672cb77f4STrond Myklebust 	if (err < 0)
35772cb77f4STrond Myklebust 		goto out_err;
358c63c7b05STrond Myklebust 	return 0;
35972cb77f4STrond Myklebust out_err:
36072cb77f4STrond Myklebust 	return err;
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds /*
3641da177e4SLinus Torvalds  * Insert a write request into an inode
3651da177e4SLinus Torvalds  */
366d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
3671da177e4SLinus Torvalds {
3681da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
369e7d39069STrond Myklebust 
370*2bfc6e56SWeston Andros Adamson 	WARN_ON_ONCE(req->wb_this_page != req);
371*2bfc6e56SWeston Andros Adamson 
372e7d39069STrond Myklebust 	/* Lock the request! */
3737ad84aa9STrond Myklebust 	nfs_lock_request(req);
374e7d39069STrond Myklebust 
375e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
376011e2a7fSBryan Schumaker 	if (!nfsi->npages && NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
377a9a4a87aSTrond Myklebust 		inode->i_version++;
37829418aa4SMel Gorman 	/*
37929418aa4SMel Gorman 	 * Swap-space should not get truncated. Hence no need to plug the race
38029418aa4SMel Gorman 	 * with invalidate/truncate.
38129418aa4SMel Gorman 	 */
38229418aa4SMel Gorman 	if (likely(!PageSwapCache(req->wb_page))) {
3832df485a7STrond Myklebust 		set_bit(PG_MAPPED, &req->wb_flags);
384deb7d638STrond Myklebust 		SetPagePrivate(req->wb_page);
385277459d2STrond Myklebust 		set_page_private(req->wb_page, (unsigned long)req);
38629418aa4SMel Gorman 	}
3871da177e4SLinus Torvalds 	nfsi->npages++;
388*2bfc6e56SWeston Andros Adamson 	set_bit(PG_INODE_REF, &req->wb_flags);
389c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
390e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds /*
39489a09141SPeter Zijlstra  * Remove a write request from an inode
3951da177e4SLinus Torvalds  */
3961da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
3971da177e4SLinus Torvalds {
3983d4ff43dSAl Viro 	struct inode *inode = req->wb_context->dentry->d_inode;
3991da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4001da177e4SLinus Torvalds 
401587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
40229418aa4SMel Gorman 	if (likely(!PageSwapCache(req->wb_page))) {
403277459d2STrond Myklebust 		set_page_private(req->wb_page, 0);
404deb7d638STrond Myklebust 		ClearPagePrivate(req->wb_page);
4052df485a7STrond Myklebust 		clear_bit(PG_MAPPED, &req->wb_flags);
40629418aa4SMel Gorman 	}
4071da177e4SLinus Torvalds 	nfsi->npages--;
408587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
4091da177e4SLinus Torvalds 	nfs_release_request(req);
4101da177e4SLinus Torvalds }
4111da177e4SLinus Torvalds 
41261822ab5STrond Myklebust static void
4136d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
41461822ab5STrond Myklebust {
41561822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
41661822ab5STrond Myklebust }
41761822ab5STrond Myklebust 
41889d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
4198dd37758STrond Myklebust /**
4208dd37758STrond Myklebust  * nfs_request_add_commit_list - add request to a commit list
4218dd37758STrond Myklebust  * @req: pointer to a struct nfs_page
422ea2cf228SFred Isaman  * @dst: commit list head
423ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
4248dd37758STrond Myklebust  *
425ea2cf228SFred Isaman  * This sets the PG_CLEAN bit, updates the cinfo count of
4268dd37758STrond Myklebust  * number of outstanding requests requiring a commit as well as
4278dd37758STrond Myklebust  * the MM page stats.
4288dd37758STrond Myklebust  *
429ea2cf228SFred Isaman  * The caller must _not_ hold the cinfo->lock, but must be
4308dd37758STrond Myklebust  * holding the nfs_page lock.
4318dd37758STrond Myklebust  */
4328dd37758STrond Myklebust void
433ea2cf228SFred Isaman nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst,
434ea2cf228SFred Isaman 			    struct nfs_commit_info *cinfo)
4358dd37758STrond Myklebust {
4368dd37758STrond Myklebust 	set_bit(PG_CLEAN, &(req)->wb_flags);
437ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
438ea2cf228SFred Isaman 	nfs_list_add_request(req, dst);
439ea2cf228SFred Isaman 	cinfo->mds->ncommit++;
440ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
44156f9cd68SFred Isaman 	if (!cinfo->dreq) {
4428dd37758STrond Myklebust 		inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
443d56b4ddfSMel Gorman 		inc_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
44456f9cd68SFred Isaman 			     BDI_RECLAIMABLE);
44556f9cd68SFred Isaman 		__mark_inode_dirty(req->wb_context->dentry->d_inode,
44656f9cd68SFred Isaman 				   I_DIRTY_DATASYNC);
44756f9cd68SFred Isaman 	}
4488dd37758STrond Myklebust }
4498dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
4508dd37758STrond Myklebust 
4518dd37758STrond Myklebust /**
4528dd37758STrond Myklebust  * nfs_request_remove_commit_list - Remove request from a commit list
4538dd37758STrond Myklebust  * @req: pointer to a nfs_page
454ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
4558dd37758STrond Myklebust  *
456ea2cf228SFred Isaman  * This clears the PG_CLEAN bit, and updates the cinfo's count of
4578dd37758STrond Myklebust  * number of outstanding requests requiring a commit
4588dd37758STrond Myklebust  * It does not update the MM page stats.
4598dd37758STrond Myklebust  *
460ea2cf228SFred Isaman  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
4618dd37758STrond Myklebust  */
4628dd37758STrond Myklebust void
463ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req,
464ea2cf228SFred Isaman 			       struct nfs_commit_info *cinfo)
4658dd37758STrond Myklebust {
4668dd37758STrond Myklebust 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
4678dd37758STrond Myklebust 		return;
4688dd37758STrond Myklebust 	nfs_list_remove_request(req);
469ea2cf228SFred Isaman 	cinfo->mds->ncommit--;
4708dd37758STrond Myklebust }
4718dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
4728dd37758STrond Myklebust 
473ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
474ea2cf228SFred Isaman 				      struct inode *inode)
475ea2cf228SFred Isaman {
476ea2cf228SFred Isaman 	cinfo->lock = &inode->i_lock;
477ea2cf228SFred Isaman 	cinfo->mds = &NFS_I(inode)->commit_info;
478ea2cf228SFred Isaman 	cinfo->ds = pnfs_get_ds_info(inode);
479b359f9d0SFred Isaman 	cinfo->dreq = NULL;
480f453a54aSFred Isaman 	cinfo->completion_ops = &nfs_commit_completion_ops;
481ea2cf228SFred Isaman }
482ea2cf228SFred Isaman 
483ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
484ea2cf228SFred Isaman 		    struct inode *inode,
485ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq)
486ea2cf228SFred Isaman {
4871763da12SFred Isaman 	if (dreq)
4881763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
4891763da12SFred Isaman 	else
490ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(cinfo, inode);
491ea2cf228SFred Isaman }
492ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo);
4938dd37758STrond Myklebust 
4941da177e4SLinus Torvalds /*
4951da177e4SLinus Torvalds  * Add a request to the inode's commit list.
4961da177e4SLinus Torvalds  */
4971763da12SFred Isaman void
498ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
499ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
5001da177e4SLinus Torvalds {
501ea2cf228SFred Isaman 	if (pnfs_mark_request_commit(req, lseg, cinfo))
5028dd37758STrond Myklebust 		return;
503ea2cf228SFred Isaman 	nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo);
5041da177e4SLinus Torvalds }
5058e821cadSTrond Myklebust 
506d6d6dc7cSFred Isaman static void
507d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page)
508e468bae9STrond Myklebust {
509e468bae9STrond Myklebust 	dec_zone_page_state(page, NR_UNSTABLE_NFS);
510d56b4ddfSMel Gorman 	dec_bdi_stat(page_file_mapping(page)->backing_dev_info, BDI_RECLAIMABLE);
511e468bae9STrond Myklebust }
512d6d6dc7cSFred Isaman 
5138dd37758STrond Myklebust static void
514d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req)
515d6d6dc7cSFred Isaman {
5168dd37758STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
5178dd37758STrond Myklebust 		struct inode *inode = req->wb_context->dentry->d_inode;
518ea2cf228SFred Isaman 		struct nfs_commit_info cinfo;
519d6d6dc7cSFred Isaman 
520ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(&cinfo, inode);
521ea2cf228SFred Isaman 		if (!pnfs_clear_request_commit(req, &cinfo)) {
522ea2cf228SFred Isaman 			spin_lock(cinfo.lock);
523ea2cf228SFred Isaman 			nfs_request_remove_commit_list(req, &cinfo);
524ea2cf228SFred Isaman 			spin_unlock(cinfo.lock);
525d6d6dc7cSFred Isaman 		}
5268dd37758STrond Myklebust 		nfs_clear_page_commit(req->wb_page);
5278dd37758STrond Myklebust 	}
528e468bae9STrond Myklebust }
529e468bae9STrond Myklebust 
5308e821cadSTrond Myklebust static inline
5319c7e1b3dSAnna Schumaker int nfs_write_need_commit(struct nfs_pgio_data *data)
5328e821cadSTrond Myklebust {
533465d5243SFred Isaman 	if (data->verf.committed == NFS_DATA_SYNC)
534cd841605SFred Isaman 		return data->header->lseg == NULL;
5358e821cadSTrond Myklebust 	return data->verf.committed != NFS_FILE_SYNC;
5368e821cadSTrond Myklebust }
5378e821cadSTrond Myklebust 
5388e821cadSTrond Myklebust #else
53968cd6fa4SBryan Schumaker static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
54068cd6fa4SBryan Schumaker 				      struct inode *inode)
54168cd6fa4SBryan Schumaker {
54268cd6fa4SBryan Schumaker }
54368cd6fa4SBryan Schumaker 
54468cd6fa4SBryan Schumaker void nfs_init_cinfo(struct nfs_commit_info *cinfo,
54568cd6fa4SBryan Schumaker 		    struct inode *inode,
54668cd6fa4SBryan Schumaker 		    struct nfs_direct_req *dreq)
54768cd6fa4SBryan Schumaker {
54868cd6fa4SBryan Schumaker }
54968cd6fa4SBryan Schumaker 
5501763da12SFred Isaman void
551ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
552ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
5538e821cadSTrond Myklebust {
5548e821cadSTrond Myklebust }
5558e821cadSTrond Myklebust 
5568dd37758STrond Myklebust static void
557e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
558e468bae9STrond Myklebust {
559e468bae9STrond Myklebust }
560e468bae9STrond Myklebust 
5618e821cadSTrond Myklebust static inline
5629c7e1b3dSAnna Schumaker int nfs_write_need_commit(struct nfs_pgio_data *data)
5638e821cadSTrond Myklebust {
5648e821cadSTrond Myklebust 	return 0;
5658e821cadSTrond Myklebust }
5668e821cadSTrond Myklebust 
5671da177e4SLinus Torvalds #endif
5681da177e4SLinus Torvalds 
569061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
5706c75dc0dSFred Isaman {
571ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
5726c75dc0dSFred Isaman 	unsigned long bytes = 0;
573*2bfc6e56SWeston Andros Adamson 	bool do_destroy;
5746c75dc0dSFred Isaman 
5756c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
5766c75dc0dSFred Isaman 		goto out;
577ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
5786c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
5796c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
5806c75dc0dSFred Isaman 
5816c75dc0dSFred Isaman 		bytes += req->wb_bytes;
5826c75dc0dSFred Isaman 		nfs_list_remove_request(req);
5836c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
5846c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
585d1182b33STrond Myklebust 			nfs_set_pageerror(req->wb_page);
5866c75dc0dSFred Isaman 			nfs_context_set_write_error(req->wb_context, hdr->error);
5876c75dc0dSFred Isaman 			goto remove_req;
5886c75dc0dSFred Isaman 		}
5896c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
5906c75dc0dSFred Isaman 			nfs_mark_request_dirty(req);
5916c75dc0dSFred Isaman 			goto next;
5926c75dc0dSFred Isaman 		}
5936c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
594f79d06f5SAnna Schumaker 			memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
595ea2cf228SFred Isaman 			nfs_mark_request_commit(req, hdr->lseg, &cinfo);
5966c75dc0dSFred Isaman 			goto next;
5976c75dc0dSFred Isaman 		}
5986c75dc0dSFred Isaman remove_req:
5996c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
6006c75dc0dSFred Isaman next:
6011d1afcbcSTrond Myklebust 		nfs_unlock_request(req);
602d1182b33STrond Myklebust 		nfs_end_page_writeback(req->wb_page);
603*2bfc6e56SWeston Andros Adamson 		do_destroy = !test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags);
6043aff4ebbSTrond Myklebust 		nfs_release_request(req);
6056c75dc0dSFred Isaman 	}
6066c75dc0dSFred Isaman out:
6076c75dc0dSFred Isaman 	hdr->release(hdr);
6086c75dc0dSFred Isaman }
6096c75dc0dSFred Isaman 
61089d77c8fSBryan Schumaker #if  IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
611ce59515cSAnna Schumaker unsigned long
612ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
613fb8a1f11STrond Myklebust {
614ea2cf228SFred Isaman 	return cinfo->mds->ncommit;
615fb8a1f11STrond Myklebust }
616fb8a1f11STrond Myklebust 
617ea2cf228SFred Isaman /* cinfo->lock held by caller */
6181763da12SFred Isaman int
619ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
620ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
621d6d6dc7cSFred Isaman {
622d6d6dc7cSFred Isaman 	struct nfs_page *req, *tmp;
623d6d6dc7cSFred Isaman 	int ret = 0;
624d6d6dc7cSFred Isaman 
625d6d6dc7cSFred Isaman 	list_for_each_entry_safe(req, tmp, src, wb_list) {
6268dd37758STrond Myklebust 		if (!nfs_lock_request(req))
6278dd37758STrond Myklebust 			continue;
6287ad84aa9STrond Myklebust 		kref_get(&req->wb_kref);
629ea2cf228SFred Isaman 		if (cond_resched_lock(cinfo->lock))
6303b3be88dSTrond Myklebust 			list_safe_reset_next(req, tmp, wb_list);
631ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
6328dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
633d6d6dc7cSFred Isaman 		ret++;
6341763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
635d6d6dc7cSFred Isaman 			break;
636d6d6dc7cSFred Isaman 	}
637d6d6dc7cSFred Isaman 	return ret;
638d6d6dc7cSFred Isaman }
639d6d6dc7cSFred Isaman 
6401da177e4SLinus Torvalds /*
6411da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
6421da177e4SLinus Torvalds  * @inode: NFS inode to scan
643ea2cf228SFred Isaman  * @dst: mds destination list
644ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
6451da177e4SLinus Torvalds  *
6461da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
6471da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
6481da177e4SLinus Torvalds  */
6491763da12SFred Isaman int
650ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
651ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
6521da177e4SLinus Torvalds {
653d6d6dc7cSFred Isaman 	int ret = 0;
654fb8a1f11STrond Myklebust 
655ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
656ea2cf228SFred Isaman 	if (cinfo->mds->ncommit > 0) {
6578dd37758STrond Myklebust 		const int max = INT_MAX;
658d6d6dc7cSFred Isaman 
659ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
660ea2cf228SFred Isaman 					   cinfo, max);
661ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
662d6d6dc7cSFred Isaman 	}
663ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
664ff778d02STrond Myklebust 	return ret;
6651da177e4SLinus Torvalds }
666d6d6dc7cSFred Isaman 
667c42de9ddSTrond Myklebust #else
668ce59515cSAnna Schumaker unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
669fb8a1f11STrond Myklebust {
670fb8a1f11STrond Myklebust 	return 0;
671fb8a1f11STrond Myklebust }
672fb8a1f11STrond Myklebust 
6731763da12SFred Isaman int nfs_scan_commit(struct inode *inode, struct list_head *dst,
674ea2cf228SFred Isaman 		    struct nfs_commit_info *cinfo)
675c42de9ddSTrond Myklebust {
676c42de9ddSTrond Myklebust 	return 0;
677c42de9ddSTrond Myklebust }
6781da177e4SLinus Torvalds #endif
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds /*
681e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
682e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
6831da177e4SLinus Torvalds  *
684e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
685e7d39069STrond Myklebust  * to disk.
6861da177e4SLinus Torvalds  */
687e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
688e7d39069STrond Myklebust 		struct page *page,
689e7d39069STrond Myklebust 		unsigned int offset,
690e7d39069STrond Myklebust 		unsigned int bytes)
6911da177e4SLinus Torvalds {
692e7d39069STrond Myklebust 	struct nfs_page *req;
693e7d39069STrond Myklebust 	unsigned int rqend;
694e7d39069STrond Myklebust 	unsigned int end;
6951da177e4SLinus Torvalds 	int error;
696277459d2STrond Myklebust 
697e7d39069STrond Myklebust 	if (!PagePrivate(page))
698e7d39069STrond Myklebust 		return NULL;
699e7d39069STrond Myklebust 
700e7d39069STrond Myklebust 	end = offset + bytes;
701e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
702e7d39069STrond Myklebust 
703e7d39069STrond Myklebust 	for (;;) {
70429418aa4SMel Gorman 		req = nfs_page_find_request_locked(NFS_I(inode), page);
705e7d39069STrond Myklebust 		if (req == NULL)
706e7d39069STrond Myklebust 			goto out_unlock;
707e7d39069STrond Myklebust 
708*2bfc6e56SWeston Andros Adamson 		/* should be handled by nfs_flush_incompatible */
709*2bfc6e56SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_head != req);
710*2bfc6e56SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_this_page != req);
711*2bfc6e56SWeston Andros Adamson 
712e7d39069STrond Myklebust 		rqend = req->wb_offset + req->wb_bytes;
713e7d39069STrond Myklebust 		/*
714e7d39069STrond Myklebust 		 * Tell the caller to flush out the request if
715e7d39069STrond Myklebust 		 * the offsets are non-contiguous.
716e7d39069STrond Myklebust 		 * Note: nfs_flush_incompatible() will already
717e7d39069STrond Myklebust 		 * have flushed out requests having wrong owners.
718e7d39069STrond Myklebust 		 */
719e468bae9STrond Myklebust 		if (offset > rqend
720e7d39069STrond Myklebust 		    || end < req->wb_offset)
721e7d39069STrond Myklebust 			goto out_flushme;
722e7d39069STrond Myklebust 
7237ad84aa9STrond Myklebust 		if (nfs_lock_request(req))
724e7d39069STrond Myklebust 			break;
725e7d39069STrond Myklebust 
726e7d39069STrond Myklebust 		/* The request is locked, so wait and then retry */
727587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
7281da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
7291da177e4SLinus Torvalds 		nfs_release_request(req);
730e7d39069STrond Myklebust 		if (error != 0)
731e7d39069STrond Myklebust 			goto out_err;
732e7d39069STrond Myklebust 		spin_lock(&inode->i_lock);
7331da177e4SLinus Torvalds 	}
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
7361da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
7371da177e4SLinus Torvalds 		req->wb_offset = offset;
7381da177e4SLinus Torvalds 		req->wb_pgbase = offset;
7391da177e4SLinus Torvalds 	}
7401da177e4SLinus Torvalds 	if (end > rqend)
7411da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
742e7d39069STrond Myklebust 	else
743e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
744e7d39069STrond Myklebust out_unlock:
745e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
746ca138f36SFred Isaman 	if (req)
7478dd37758STrond Myklebust 		nfs_clear_request_commit(req);
748e7d39069STrond Myklebust 	return req;
749e7d39069STrond Myklebust out_flushme:
750e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
751e7d39069STrond Myklebust 	nfs_release_request(req);
752e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
753e7d39069STrond Myklebust out_err:
754e7d39069STrond Myklebust 	return ERR_PTR(error);
755e7d39069STrond Myklebust }
7561da177e4SLinus Torvalds 
757e7d39069STrond Myklebust /*
758e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
759e7d39069STrond Myklebust  *
760e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
761e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
762e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
763e7d39069STrond Myklebust  */
764e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
765e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
766e7d39069STrond Myklebust {
767d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
768e7d39069STrond Myklebust 	struct nfs_page	*req;
769e7d39069STrond Myklebust 
770e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
771e7d39069STrond Myklebust 	if (req != NULL)
772e7d39069STrond Myklebust 		goto out;
773*2bfc6e56SWeston Andros Adamson 	req = nfs_create_request(ctx, page, NULL, offset, bytes);
774e7d39069STrond Myklebust 	if (IS_ERR(req))
775e7d39069STrond Myklebust 		goto out;
776d6d6dc7cSFred Isaman 	nfs_inode_add_request(inode, req);
777efc91ed0STrond Myklebust out:
77861e930a9STrond Myklebust 	return req;
7791da177e4SLinus Torvalds }
7801da177e4SLinus Torvalds 
781e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
782e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
783e7d39069STrond Myklebust {
784e7d39069STrond Myklebust 	struct nfs_page	*req;
785e7d39069STrond Myklebust 
786e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
787e7d39069STrond Myklebust 	if (IS_ERR(req))
788e7d39069STrond Myklebust 		return PTR_ERR(req);
789e7d39069STrond Myklebust 	/* Update file length */
790e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
791e7d39069STrond Myklebust 	nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
792a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
7931d1afcbcSTrond Myklebust 	nfs_unlock_and_release_request(req);
794e7d39069STrond Myklebust 	return 0;
795e7d39069STrond Myklebust }
796e7d39069STrond Myklebust 
7971da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
7981da177e4SLinus Torvalds {
799cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
8002a369153STrond Myklebust 	struct nfs_lock_context *l_ctx;
8011da177e4SLinus Torvalds 	struct nfs_page	*req;
8021a54533eSTrond Myklebust 	int do_flush, status;
8031da177e4SLinus Torvalds 	/*
8041da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
8051da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
8061da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
8071da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
8081da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
8091da177e4SLinus Torvalds 	 * dropped page.
8101da177e4SLinus Torvalds 	 */
8111a54533eSTrond Myklebust 	do {
812277459d2STrond Myklebust 		req = nfs_page_find_request(page);
8131a54533eSTrond Myklebust 		if (req == NULL)
8141a54533eSTrond Myklebust 			return 0;
8152a369153STrond Myklebust 		l_ctx = req->wb_lock_context;
8162a369153STrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx;
817*2bfc6e56SWeston Andros Adamson 		/* for now, flush if more than 1 request in page_group */
818*2bfc6e56SWeston Andros Adamson 		do_flush |= req->wb_this_page != req;
8190f1d2605STrond Myklebust 		if (l_ctx && ctx->dentry->d_inode->i_flock != NULL) {
8202a369153STrond Myklebust 			do_flush |= l_ctx->lockowner.l_owner != current->files
8212a369153STrond Myklebust 				|| l_ctx->lockowner.l_pid != current->tgid;
8222a369153STrond Myklebust 		}
8231da177e4SLinus Torvalds 		nfs_release_request(req);
8241a54533eSTrond Myklebust 		if (!do_flush)
8251a54533eSTrond Myklebust 			return 0;
826d56b4ddfSMel Gorman 		status = nfs_wb_page(page_file_mapping(page)->host, page);
8271a54533eSTrond Myklebust 	} while (status == 0);
8281a54533eSTrond Myklebust 	return status;
8291da177e4SLinus Torvalds }
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds /*
832dc24826bSAndy Adamson  * Avoid buffered writes when a open context credential's key would
833dc24826bSAndy Adamson  * expire soon.
834dc24826bSAndy Adamson  *
835dc24826bSAndy Adamson  * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
836dc24826bSAndy Adamson  *
837dc24826bSAndy Adamson  * Return 0 and set a credential flag which triggers the inode to flush
838dc24826bSAndy Adamson  * and performs  NFS_FILE_SYNC writes if the key will expired within
839dc24826bSAndy Adamson  * RPC_KEY_EXPIRE_TIMEO.
840dc24826bSAndy Adamson  */
841dc24826bSAndy Adamson int
842dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode)
843dc24826bSAndy Adamson {
844dc24826bSAndy Adamson 	struct nfs_open_context *ctx = nfs_file_open_context(filp);
845dc24826bSAndy Adamson 	struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
846dc24826bSAndy Adamson 
847dc24826bSAndy Adamson 	return rpcauth_key_timeout_notify(auth, ctx->cred);
848dc24826bSAndy Adamson }
849dc24826bSAndy Adamson 
850dc24826bSAndy Adamson /*
851dc24826bSAndy Adamson  * Test if the open context credential key is marked to expire soon.
852dc24826bSAndy Adamson  */
853dc24826bSAndy Adamson bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx)
854dc24826bSAndy Adamson {
855dc24826bSAndy Adamson 	return rpcauth_cred_key_to_expire(ctx->cred);
856dc24826bSAndy Adamson }
857dc24826bSAndy Adamson 
858dc24826bSAndy Adamson /*
8595d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
8605d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
8615d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
8625d47a356STrond Myklebust  */
8638d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
8645d47a356STrond Myklebust {
865d529ef83SJeff Layton 	struct nfs_inode *nfsi = NFS_I(inode);
866d529ef83SJeff Layton 
8678d197a56STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
8688d197a56STrond Myklebust 		goto out;
869d529ef83SJeff Layton 	if (nfsi->cache_validity & (NFS_INO_INVALID_DATA|NFS_INO_REVAL_PAGECACHE))
870d529ef83SJeff Layton 		return false;
8714db72b40SJeff Layton 	smp_rmb();
872d529ef83SJeff Layton 	if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags))
8738d197a56STrond Myklebust 		return false;
8748d197a56STrond Myklebust out:
8758d197a56STrond Myklebust 	return PageUptodate(page) != 0;
8765d47a356STrond Myklebust }
8775d47a356STrond Myklebust 
878c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or
879c7559663SScott Mayhew  * if we have the whole file locked for writing), it may be more efficient to
880c7559663SScott Mayhew  * extend the write to cover the entire page in order to avoid fragmentation
881c7559663SScott Mayhew  * inefficiencies.
882c7559663SScott Mayhew  *
883263b4509SScott Mayhew  * If the file is opened for synchronous writes then we can just skip the rest
884263b4509SScott Mayhew  * of the checks.
885c7559663SScott Mayhew  */
886c7559663SScott Mayhew static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode)
887c7559663SScott Mayhew {
888c7559663SScott Mayhew 	if (file->f_flags & O_DSYNC)
889c7559663SScott Mayhew 		return 0;
890263b4509SScott Mayhew 	if (!nfs_write_pageuptodate(page, inode))
891263b4509SScott Mayhew 		return 0;
892c7559663SScott Mayhew 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
893c7559663SScott Mayhew 		return 1;
894263b4509SScott Mayhew 	if (inode->i_flock == NULL || (inode->i_flock->fl_start == 0 &&
895c7559663SScott Mayhew 			inode->i_flock->fl_end == OFFSET_MAX &&
896263b4509SScott Mayhew 			inode->i_flock->fl_type != F_RDLCK))
897c7559663SScott Mayhew 		return 1;
898c7559663SScott Mayhew 	return 0;
899c7559663SScott Mayhew }
900c7559663SScott Mayhew 
9015d47a356STrond Myklebust /*
9021da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
9031da177e4SLinus Torvalds  *
9041da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
9051da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
9061da177e4SLinus Torvalds  */
9071da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
9081da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
9091da177e4SLinus Torvalds {
910cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
911d56b4ddfSMel Gorman 	struct inode	*inode = page_file_mapping(page)->host;
9121da177e4SLinus Torvalds 	int		status = 0;
9131da177e4SLinus Torvalds 
91491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
91591d5b470SChuck Lever 
9166de1472fSAl Viro 	dprintk("NFS:       nfs_updatepage(%pD2 %d@%lld)\n",
9176de1472fSAl Viro 		file, count, (long long)(page_file_offset(page) + offset));
9181da177e4SLinus Torvalds 
919c7559663SScott Mayhew 	if (nfs_can_extend_write(file, page, inode)) {
92049a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
9211da177e4SLinus Torvalds 		offset = 0;
9221da177e4SLinus Torvalds 	}
9231da177e4SLinus Torvalds 
924e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
92503fa9e84STrond Myklebust 	if (status < 0)
92603fa9e84STrond Myklebust 		nfs_set_pageerror(page);
92759b7c05fSTrond Myklebust 	else
92859b7c05fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
9291da177e4SLinus Torvalds 
93048186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
9311da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
9321da177e4SLinus Torvalds 	return status;
9331da177e4SLinus Torvalds }
9341da177e4SLinus Torvalds 
9353ff7576dSTrond Myklebust static int flush_task_priority(int how)
9361da177e4SLinus Torvalds {
9371da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
9381da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
9391da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
9401da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
9411da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
9421da177e4SLinus Torvalds 	}
9431da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
9441da177e4SLinus Torvalds }
9451da177e4SLinus Torvalds 
9461ed26f33SAnna Schumaker static void nfs_initiate_write(struct nfs_pgio_data *data, struct rpc_message *msg,
9471ed26f33SAnna Schumaker 			       struct rpc_task_setup *task_setup_data, int how)
9481da177e4SLinus Torvalds {
949cd841605SFred Isaman 	struct inode *inode = data->header->inode;
9503ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
9511da177e4SLinus Torvalds 
9521ed26f33SAnna Schumaker 	task_setup_data->priority = priority;
9531ed26f33SAnna Schumaker 	NFS_PROTO(inode)->write_setup(data, msg);
954d138d5d1SAndy Adamson 
9558c21c62cSWeston Andros Adamson 	nfs4_state_protect_write(NFS_SERVER(inode)->nfs_client,
9561ed26f33SAnna Schumaker 				 &task_setup_data->rpc_client, msg, data);
957d138d5d1SAndy Adamson }
958d138d5d1SAndy Adamson 
9596d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
9606d884e8fSFred  * call this on each, which will prepare them to be retried on next
9616d884e8fSFred  * writeback using standard nfs.
9626d884e8fSFred  */
9636d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
9646d884e8fSFred {
9656d884e8fSFred 	nfs_mark_request_dirty(req);
9661d1afcbcSTrond Myklebust 	nfs_unlock_request(req);
967d1182b33STrond Myklebust 	nfs_end_page_writeback(req->wb_page);
9683aff4ebbSTrond Myklebust 	nfs_release_request(req);
9696d884e8fSFred }
9706d884e8fSFred 
971061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head)
9726c75dc0dSFred Isaman {
9736c75dc0dSFred Isaman 	struct nfs_page	*req;
9746c75dc0dSFred Isaman 
9756c75dc0dSFred Isaman 	while (!list_empty(head)) {
9766c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
9776c75dc0dSFred Isaman 		nfs_list_remove_request(req);
9786c75dc0dSFred Isaman 		nfs_redirty_request(req);
9796c75dc0dSFred Isaman 	}
9806c75dc0dSFred Isaman }
9816c75dc0dSFred Isaman 
982061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
983061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
984061ae2edSFred Isaman 	.completion = nfs_write_completion,
985061ae2edSFred Isaman };
986061ae2edSFred Isaman 
98757208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
988a20c93e3SChristoph Hellwig 			       struct inode *inode, int ioflags, bool force_mds,
989061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
9901751c363STrond Myklebust {
991a20c93e3SChristoph Hellwig 	struct nfs_server *server = NFS_SERVER(inode);
99241d8d5b7SAnna Schumaker 	const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
993a20c93e3SChristoph Hellwig 
994a20c93e3SChristoph Hellwig #ifdef CONFIG_NFS_V4_1
995a20c93e3SChristoph Hellwig 	if (server->pnfs_curr_ld && !force_mds)
996a20c93e3SChristoph Hellwig 		pg_ops = server->pnfs_curr_ld->pg_write_ops;
997a20c93e3SChristoph Hellwig #endif
9984a0de55cSAnna Schumaker 	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
9994a0de55cSAnna Schumaker 			server->wsize, ioflags);
10001751c363STrond Myklebust }
1001ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
10021751c363STrond Myklebust 
1003dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1004dce81290STrond Myklebust {
100541d8d5b7SAnna Schumaker 	pgio->pg_ops = &nfs_pgio_rw_ops;
1006dce81290STrond Myklebust 	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1007dce81290STrond Myklebust }
10081f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1009dce81290STrond Myklebust 
10101da177e4SLinus Torvalds 
10110b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
10120b7c0153SFred Isaman {
10130b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
10140b7c0153SFred Isaman 
10150b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
10160b7c0153SFred Isaman }
10170b7c0153SFred Isaman 
1018a4cdda59SAnna Schumaker static void nfs_writeback_release_common(struct nfs_pgio_data *data)
1019c9d8f89dSTrond Myklebust {
1020cd841605SFred Isaman 	struct nfs_pgio_header *hdr = data->header;
1021e2fecb21STrond Myklebust 	int status = data->task.tk_status;
1022788e7a89STrond Myklebust 
10236c75dc0dSFred Isaman 	if ((status >= 0) && nfs_write_need_commit(data)) {
10246c75dc0dSFred Isaman 		spin_lock(&hdr->lock);
10256c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags))
10266c75dc0dSFred Isaman 			; /* Do nothing */
10276c75dc0dSFred Isaman 		else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags))
1028f79d06f5SAnna Schumaker 			memcpy(&hdr->verf, &data->verf, sizeof(hdr->verf));
1029f79d06f5SAnna Schumaker 		else if (memcmp(&hdr->verf, &data->verf, sizeof(hdr->verf)))
10306c75dc0dSFred Isaman 			set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags);
10316c75dc0dSFred Isaman 		spin_unlock(&hdr->lock);
10321da177e4SLinus Torvalds 	}
10331da177e4SLinus Torvalds }
10341da177e4SLinus Torvalds 
10351f2edbe3STrond Myklebust /*
10361f2edbe3STrond Myklebust  * Special version of should_remove_suid() that ignores capabilities.
10371f2edbe3STrond Myklebust  */
10381f2edbe3STrond Myklebust static int nfs_should_remove_suid(const struct inode *inode)
10391f2edbe3STrond Myklebust {
10401f2edbe3STrond Myklebust 	umode_t mode = inode->i_mode;
10411f2edbe3STrond Myklebust 	int kill = 0;
10421f2edbe3STrond Myklebust 
10431f2edbe3STrond Myklebust 	/* suid always must be killed */
10441f2edbe3STrond Myklebust 	if (unlikely(mode & S_ISUID))
10451f2edbe3STrond Myklebust 		kill = ATTR_KILL_SUID;
10461f2edbe3STrond Myklebust 
10471f2edbe3STrond Myklebust 	/*
10481f2edbe3STrond Myklebust 	 * sgid without any exec bits is just a mandatory locking mark; leave
10491f2edbe3STrond Myklebust 	 * it alone.  If some exec bits are set, it's a real sgid; kill it.
10501f2edbe3STrond Myklebust 	 */
10511f2edbe3STrond Myklebust 	if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
10521f2edbe3STrond Myklebust 		kill |= ATTR_KILL_SGID;
10531f2edbe3STrond Myklebust 
10541f2edbe3STrond Myklebust 	if (unlikely(kill && S_ISREG(mode)))
10551f2edbe3STrond Myklebust 		return kill;
10561f2edbe3STrond Myklebust 
10571f2edbe3STrond Myklebust 	return 0;
10581f2edbe3STrond Myklebust }
1059788e7a89STrond Myklebust 
10601da177e4SLinus Torvalds /*
10611da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
10621da177e4SLinus Torvalds  */
10630eecb214SAnna Schumaker static int nfs_writeback_done(struct rpc_task *task, struct nfs_pgio_data *data,
10640eecb214SAnna Schumaker 			      struct inode *inode)
10651da177e4SLinus Torvalds {
1066788e7a89STrond Myklebust 	int status;
10671da177e4SLinus Torvalds 
1068f551e44fSChuck Lever 	/*
1069f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1070f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1071f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1072f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1073f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1074f551e44fSChuck Lever 	 */
1075cd841605SFred Isaman 	status = NFS_PROTO(inode)->write_done(task, data);
1076788e7a89STrond Myklebust 	if (status != 0)
10770eecb214SAnna Schumaker 		return status;
10780eecb214SAnna Schumaker 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, data->res.count);
107991d5b470SChuck Lever 
108089d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
10810eecb214SAnna Schumaker 	if (data->res.verf->committed < data->args.stable && task->tk_status >= 0) {
10821da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
10831da177e4SLinus Torvalds 		 * commit data to stable storage even though we
10841da177e4SLinus Torvalds 		 * requested it.
10851da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
10861da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
10871da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
10881da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
10891da177e4SLinus Torvalds 		 */
10901da177e4SLinus Torvalds 		static unsigned long    complain;
10911da177e4SLinus Torvalds 
1092a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
10931da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
10941da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
10951da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1096cd841605SFred Isaman 				NFS_SERVER(inode)->nfs_client->cl_hostname,
10970eecb214SAnna Schumaker 				data->res.verf->committed, data->args.stable);
10981da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
10991da177e4SLinus Torvalds 		}
11001da177e4SLinus Torvalds 	}
11011da177e4SLinus Torvalds #endif
11021f2edbe3STrond Myklebust 
11031f2edbe3STrond Myklebust 	/* Deal with the suid/sgid bit corner case */
11041f2edbe3STrond Myklebust 	if (nfs_should_remove_suid(inode))
11051f2edbe3STrond Myklebust 		nfs_mark_for_revalidate(inode);
11060eecb214SAnna Schumaker 	return 0;
11070eecb214SAnna Schumaker }
11080eecb214SAnna Schumaker 
11090eecb214SAnna Schumaker /*
11100eecb214SAnna Schumaker  * This function is called when the WRITE call is complete.
11110eecb214SAnna Schumaker  */
11120eecb214SAnna Schumaker static void nfs_writeback_result(struct rpc_task *task, struct nfs_pgio_data *data)
11130eecb214SAnna Schumaker {
11140eecb214SAnna Schumaker 	struct nfs_pgio_args	*argp = &data->args;
11150eecb214SAnna Schumaker 	struct nfs_pgio_res	*resp = &data->res;
11161f2edbe3STrond Myklebust 
11171f2edbe3STrond Myklebust 	if (resp->count < argp->count) {
11181da177e4SLinus Torvalds 		static unsigned long    complain;
11191da177e4SLinus Torvalds 
11206c75dc0dSFred Isaman 		/* This a short write! */
11210eecb214SAnna Schumaker 		nfs_inc_stats(data->header->inode, NFSIOS_SHORTWRITE);
112291d5b470SChuck Lever 
11231da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
11246c75dc0dSFred Isaman 		if (resp->count == 0) {
11256c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
11266c75dc0dSFred Isaman 				printk(KERN_WARNING
11276c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
11286c75dc0dSFred Isaman 				       argp->count);
11296c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
11306c75dc0dSFred Isaman 			}
11316c75dc0dSFred Isaman 			nfs_set_pgio_error(data->header, -EIO, argp->offset);
11326c75dc0dSFred Isaman 			task->tk_status = -EIO;
11336c75dc0dSFred Isaman 			return;
11346c75dc0dSFred Isaman 		}
11351da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
11361da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
11371da177e4SLinus Torvalds 			/* Resend from where the server left off */
1138a69aef14SFred Isaman 			data->mds_offset += resp->count;
11391da177e4SLinus Torvalds 			argp->offset += resp->count;
11401da177e4SLinus Torvalds 			argp->pgbase += resp->count;
11411da177e4SLinus Torvalds 			argp->count -= resp->count;
11421da177e4SLinus Torvalds 		} else {
11431da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
11441da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
11451da177e4SLinus Torvalds 			 */
11461da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
11471da177e4SLinus Torvalds 		}
1148d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
11491da177e4SLinus Torvalds 	}
11501da177e4SLinus Torvalds }
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds 
115389d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
115471d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
115571d0a611STrond Myklebust {
1156b8413f98STrond Myklebust 	int ret;
1157b8413f98STrond Myklebust 
115871d0a611STrond Myklebust 	if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
115971d0a611STrond Myklebust 		return 1;
1160b8413f98STrond Myklebust 	if (!may_wait)
116171d0a611STrond Myklebust 		return 0;
1162b8413f98STrond Myklebust 	ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1163b8413f98STrond Myklebust 				NFS_INO_COMMIT,
1164b8413f98STrond Myklebust 				nfs_wait_bit_killable,
1165b8413f98STrond Myklebust 				TASK_KILLABLE);
1166b8413f98STrond Myklebust 	return (ret < 0) ? ret : 1;
116771d0a611STrond Myklebust }
116871d0a611STrond Myklebust 
1169f453a54aSFred Isaman static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
117071d0a611STrond Myklebust {
117171d0a611STrond Myklebust 	clear_bit(NFS_INO_COMMIT, &nfsi->flags);
117271d0a611STrond Myklebust 	smp_mb__after_clear_bit();
117371d0a611STrond Myklebust 	wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
117471d0a611STrond Myklebust }
117571d0a611STrond Myklebust 
11760b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
11771da177e4SLinus Torvalds {
11780b7c0153SFred Isaman 	put_nfs_open_context(data->context);
11790b7c0153SFred Isaman 	nfs_commit_free(data);
11801da177e4SLinus Torvalds }
1181e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
11821da177e4SLinus Torvalds 
11830b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
11849ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
11859f0ec176SAndy Adamson 			int how, int flags)
11861da177e4SLinus Torvalds {
118707737691STrond Myklebust 	struct rpc_task *task;
11889ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1189bdc7f021STrond Myklebust 	struct rpc_message msg = {
1190bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1191bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
11929ace33cdSFred Isaman 		.rpc_cred = data->cred,
1193bdc7f021STrond Myklebust 	};
119484115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
119507737691STrond Myklebust 		.task = &data->task,
11969ace33cdSFred Isaman 		.rpc_client = clnt,
1197bdc7f021STrond Myklebust 		.rpc_message = &msg,
11989ace33cdSFred Isaman 		.callback_ops = call_ops,
119984115e1cSTrond Myklebust 		.callback_data = data,
1200101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
12019f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
12023ff7576dSTrond Myklebust 		.priority = priority,
120384115e1cSTrond Myklebust 	};
1204788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
12059ace33cdSFred Isaman 	NFS_PROTO(data->inode)->commit_setup(data, &msg);
12061da177e4SLinus Torvalds 
1207a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1208bdc7f021STrond Myklebust 
12098c21c62cSWeston Andros Adamson 	nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client,
12108c21c62cSWeston Andros Adamson 		NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg);
12118c21c62cSWeston Andros Adamson 
121207737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1213dbae4c73STrond Myklebust 	if (IS_ERR(task))
1214dbae4c73STrond Myklebust 		return PTR_ERR(task);
1215d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1216d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
121707737691STrond Myklebust 	rpc_put_task(task);
1218dbae4c73STrond Myklebust 	return 0;
12191da177e4SLinus Torvalds }
1220e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds /*
12239ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
12249ace33cdSFred Isaman  */
12250b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1226988b6dceSFred Isaman 		     struct list_head *head,
1227f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1228f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
12299ace33cdSFred Isaman {
12309ace33cdSFred Isaman 	struct nfs_page *first = nfs_list_entry(head->next);
12313d4ff43dSAl Viro 	struct inode *inode = first->wb_context->dentry->d_inode;
12329ace33cdSFred Isaman 
12339ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
12349ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
12359ace33cdSFred Isaman 
12369ace33cdSFred Isaman 	list_splice_init(head, &data->pages);
12379ace33cdSFred Isaman 
12389ace33cdSFred Isaman 	data->inode	  = inode;
12399ace33cdSFred Isaman 	data->cred	  = first->wb_context->cred;
1240988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
12419ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1242f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1243b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
12449ace33cdSFred Isaman 
12459ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
12469ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
12479ace33cdSFred Isaman 	data->args.offset = 0;
12489ace33cdSFred Isaman 	data->args.count  = 0;
12490b7c0153SFred Isaman 	data->context     = get_nfs_open_context(first->wb_context);
12509ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
12519ace33cdSFred Isaman 	data->res.verf    = &data->verf;
12529ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
12539ace33cdSFred Isaman }
1254e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
12559ace33cdSFred Isaman 
1256e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1257ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1258ea2cf228SFred Isaman 		      struct nfs_commit_info *cinfo)
125964bfeb49SFred Isaman {
126064bfeb49SFred Isaman 	struct nfs_page *req;
126164bfeb49SFred Isaman 
126264bfeb49SFred Isaman 	while (!list_empty(page_list)) {
126364bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
126464bfeb49SFred Isaman 		nfs_list_remove_request(req);
1265ea2cf228SFred Isaman 		nfs_mark_request_commit(req, lseg, cinfo);
126656f9cd68SFred Isaman 		if (!cinfo->dreq) {
126764bfeb49SFred Isaman 			dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1268d56b4ddfSMel Gorman 			dec_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
126964bfeb49SFred Isaman 				     BDI_RECLAIMABLE);
127056f9cd68SFred Isaman 		}
12711d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
127264bfeb49SFred Isaman 	}
127364bfeb49SFred Isaman }
1274e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
127564bfeb49SFred Isaman 
12769ace33cdSFred Isaman /*
12771da177e4SLinus Torvalds  * Commit dirty pages
12781da177e4SLinus Torvalds  */
12791da177e4SLinus Torvalds static int
1280ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1281ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
12821da177e4SLinus Torvalds {
12830b7c0153SFred Isaman 	struct nfs_commit_data	*data;
12841da177e4SLinus Torvalds 
1285c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 	if (!data)
12881da177e4SLinus Torvalds 		goto out_bad;
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 	/* Set up the argument struct */
1291f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
1292f453a54aSFred Isaman 	atomic_inc(&cinfo->mds->rpcs_out);
12939f0ec176SAndy Adamson 	return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops,
12949f0ec176SAndy Adamson 				   how, 0);
12951da177e4SLinus Torvalds  out_bad:
1296ea2cf228SFred Isaman 	nfs_retry_commit(head, NULL, cinfo);
1297f453a54aSFred Isaman 	cinfo->completion_ops->error_cleanup(NFS_I(inode));
12981da177e4SLinus Torvalds 	return -ENOMEM;
12991da177e4SLinus Torvalds }
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds /*
13021da177e4SLinus Torvalds  * COMMIT call returned
13031da177e4SLinus Torvalds  */
1304788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
13051da177e4SLinus Torvalds {
13060b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
13071da177e4SLinus Torvalds 
1308a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
13091da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
13101da177e4SLinus Torvalds 
1311788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1312c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
1313c9d8f89dSTrond Myklebust }
1314c9d8f89dSTrond Myklebust 
1315f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1316c9d8f89dSTrond Myklebust {
1317c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1318c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1319f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
1320788e7a89STrond Myklebust 
13211da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
13221da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
13231da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1324d6d6dc7cSFred Isaman 		nfs_clear_page_commit(req->wb_page);
13251da177e4SLinus Torvalds 
13261e8968c5SNiels de Vos 		dprintk("NFS:       commit (%s/%llu %d@%lld)",
13273d4ff43dSAl Viro 			req->wb_context->dentry->d_sb->s_id,
13281e8968c5SNiels de Vos 			(unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
13291da177e4SLinus Torvalds 			req->wb_bytes,
13301da177e4SLinus Torvalds 			(long long)req_offset(req));
1331c9d8f89dSTrond Myklebust 		if (status < 0) {
1332c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
13331da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1334c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
13351da177e4SLinus Torvalds 			goto next;
13361da177e4SLinus Torvalds 		}
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
13391da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
13402f2c63bcSTrond Myklebust 		if (!memcmp(&req->wb_verf, &data->verf.verifier, sizeof(req->wb_verf))) {
13411da177e4SLinus Torvalds 			/* We have a match */
13421da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
13431da177e4SLinus Torvalds 			dprintk(" OK\n");
13441da177e4SLinus Torvalds 			goto next;
13451da177e4SLinus Torvalds 		}
13461da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
13471da177e4SLinus Torvalds 		dprintk(" mismatch\n");
13486d884e8fSFred 		nfs_mark_request_dirty(req);
134905990d1bSTrond Myklebust 		set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
13501da177e4SLinus Torvalds 	next:
13511d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
13521da177e4SLinus Torvalds 	}
1353f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1354f453a54aSFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
1355f453a54aSFred Isaman 		nfs_commit_clear_lock(NFS_I(data->inode));
13565917ce84SFred Isaman }
13575917ce84SFred Isaman 
13585917ce84SFred Isaman static void nfs_commit_release(void *calldata)
13595917ce84SFred Isaman {
13600b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
13615917ce84SFred Isaman 
1362f453a54aSFred Isaman 	data->completion_ops->completion(data);
1363c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
13641da177e4SLinus Torvalds }
1365788e7a89STrond Myklebust 
1366788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
13670b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1368788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1369788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1370788e7a89STrond Myklebust };
13711da177e4SLinus Torvalds 
1372f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1373f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1374f453a54aSFred Isaman 	.error_cleanup = nfs_commit_clear_lock,
1375f453a54aSFred Isaman };
1376f453a54aSFred Isaman 
13771763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1378ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
137984c53ab5SFred Isaman {
138084c53ab5SFred Isaman 	int status;
138184c53ab5SFred Isaman 
1382ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
138384c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1384ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
138584c53ab5SFred Isaman 	return status;
138684c53ab5SFred Isaman }
138784c53ab5SFred Isaman 
1388b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
13891da177e4SLinus Torvalds {
13901da177e4SLinus Torvalds 	LIST_HEAD(head);
1391ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
139271d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1393b8413f98STrond Myklebust 	int res;
13941da177e4SLinus Torvalds 
1395b8413f98STrond Myklebust 	res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1396b8413f98STrond Myklebust 	if (res <= 0)
1397c5efa5fcSTrond Myklebust 		goto out_mark_dirty;
1398ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1399ea2cf228SFred Isaman 	res = nfs_scan_commit(inode, &head, &cinfo);
14001da177e4SLinus Torvalds 	if (res) {
1401a861a1e1SFred Isaman 		int error;
1402a861a1e1SFred Isaman 
1403ea2cf228SFred Isaman 		error = nfs_generic_commit_list(inode, &head, how, &cinfo);
14041da177e4SLinus Torvalds 		if (error < 0)
14051da177e4SLinus Torvalds 			return error;
1406b8413f98STrond Myklebust 		if (!may_wait)
1407b8413f98STrond Myklebust 			goto out_mark_dirty;
1408b8413f98STrond Myklebust 		error = wait_on_bit(&NFS_I(inode)->flags,
1409b8413f98STrond Myklebust 				NFS_INO_COMMIT,
141071d0a611STrond Myklebust 				nfs_wait_bit_killable,
141171d0a611STrond Myklebust 				TASK_KILLABLE);
1412b8413f98STrond Myklebust 		if (error < 0)
1413b8413f98STrond Myklebust 			return error;
141471d0a611STrond Myklebust 	} else
141571d0a611STrond Myklebust 		nfs_commit_clear_lock(NFS_I(inode));
1416c5efa5fcSTrond Myklebust 	return res;
1417c5efa5fcSTrond Myklebust 	/* Note: If we exit without ensuring that the commit is complete,
1418c5efa5fcSTrond Myklebust 	 * we must mark the inode as dirty. Otherwise, future calls to
1419c5efa5fcSTrond Myklebust 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1420c5efa5fcSTrond Myklebust 	 * that the data is on the disk.
1421c5efa5fcSTrond Myklebust 	 */
1422c5efa5fcSTrond Myklebust out_mark_dirty:
1423c5efa5fcSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
14241da177e4SLinus Torvalds 	return res;
14251da177e4SLinus Torvalds }
14268fc795f7STrond Myklebust 
14278fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
14288fc795f7STrond Myklebust {
1429420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1430420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1431420e3646STrond Myklebust 	int ret = 0;
14328fc795f7STrond Myklebust 
14333236c3e1SJeff Layton 	/* no commits means nothing needs to be done */
1434ea2cf228SFred Isaman 	if (!nfsi->commit_info.ncommit)
14353236c3e1SJeff Layton 		return ret;
14363236c3e1SJeff Layton 
1437a00dd6c0SJeff Layton 	if (wbc->sync_mode == WB_SYNC_NONE) {
1438a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1439a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1440420e3646STrond Myklebust 		 */
1441ea2cf228SFred Isaman 		if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1))
1442420e3646STrond Myklebust 			goto out_mark_dirty;
1443420e3646STrond Myklebust 
1444a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1445420e3646STrond Myklebust 		flags = 0;
1446a00dd6c0SJeff Layton 	}
1447a00dd6c0SJeff Layton 
1448420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1449420e3646STrond Myklebust 	if (ret >= 0) {
1450420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1451420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1452420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1453420e3646STrond Myklebust 			else
1454420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1455420e3646STrond Myklebust 		}
14568fc795f7STrond Myklebust 		return 0;
1457420e3646STrond Myklebust 	}
1458420e3646STrond Myklebust out_mark_dirty:
14598fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
14608fc795f7STrond Myklebust 	return ret;
14618fc795f7STrond Myklebust }
1462c63c7b05STrond Myklebust #else
14638fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
14648fc795f7STrond Myklebust {
14658fc795f7STrond Myklebust 	return 0;
14668fc795f7STrond Myklebust }
14671da177e4SLinus Torvalds #endif
14681da177e4SLinus Torvalds 
14698fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
14708fc795f7STrond Myklebust {
1471a8d8f02cSBryan Schumaker 	return nfs_commit_unstable_pages(inode, wbc);
1472a8d8f02cSBryan Schumaker }
147389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode);
1474863a3c6cSAndy Adamson 
1475acdc53b2STrond Myklebust /*
1476acdc53b2STrond Myklebust  * flush the inode to disk.
1477acdc53b2STrond Myklebust  */
1478acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
147934901f70STrond Myklebust {
148034901f70STrond Myklebust 	struct writeback_control wbc = {
148172cb77f4STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
148234901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
1483d7fb1207STrond Myklebust 		.range_start = 0,
1484d7fb1207STrond Myklebust 		.range_end = LLONG_MAX,
148534901f70STrond Myklebust 	};
1486f4ce1299STrond Myklebust 	int ret;
148734901f70STrond Myklebust 
1488f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_enter(inode);
1489f4ce1299STrond Myklebust 
1490f4ce1299STrond Myklebust 	ret = sync_inode(inode, &wbc);
1491f4ce1299STrond Myklebust 
1492f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_exit(inode, ret);
1493f4ce1299STrond Myklebust 	return ret;
14941c75950bSTrond Myklebust }
1495ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all);
14961c75950bSTrond Myklebust 
14971b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
14981b3b4a1aSTrond Myklebust {
14991b3b4a1aSTrond Myklebust 	struct nfs_page *req;
15001b3b4a1aSTrond Myklebust 	int ret = 0;
15011b3b4a1aSTrond Myklebust 
15021b3b4a1aSTrond Myklebust 	for (;;) {
1503ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
15041b3b4a1aSTrond Myklebust 		req = nfs_page_find_request(page);
15051b3b4a1aSTrond Myklebust 		if (req == NULL)
15061b3b4a1aSTrond Myklebust 			break;
15077ad84aa9STrond Myklebust 		if (nfs_lock_request(req)) {
15088dd37758STrond Myklebust 			nfs_clear_request_commit(req);
15091b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
15101b3b4a1aSTrond Myklebust 			/*
15111b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
15121b3b4a1aSTrond Myklebust 			 * page as being dirty
15131b3b4a1aSTrond Myklebust 			 */
15141b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
15151d1afcbcSTrond Myklebust 			nfs_unlock_and_release_request(req);
15161b3b4a1aSTrond Myklebust 			break;
15171b3b4a1aSTrond Myklebust 		}
15181b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
1519c9edda71STrond Myklebust 		nfs_release_request(req);
15201b3b4a1aSTrond Myklebust 		if (ret < 0)
1521c988950eSTrond Myklebust 			break;
15221b3b4a1aSTrond Myklebust 	}
15231b3b4a1aSTrond Myklebust 	return ret;
15241b3b4a1aSTrond Myklebust }
15251b3b4a1aSTrond Myklebust 
15261c75950bSTrond Myklebust /*
15271c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
15281c75950bSTrond Myklebust  */
15291c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
15301c75950bSTrond Myklebust {
153129418aa4SMel Gorman 	loff_t range_start = page_file_offset(page);
15327f2f12d9STrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
15337f2f12d9STrond Myklebust 	struct writeback_control wbc = {
15347f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
15357f2f12d9STrond Myklebust 		.nr_to_write = 0,
15367f2f12d9STrond Myklebust 		.range_start = range_start,
15377f2f12d9STrond Myklebust 		.range_end = range_end,
15387f2f12d9STrond Myklebust 	};
15397f2f12d9STrond Myklebust 	int ret;
15407f2f12d9STrond Myklebust 
1541f4ce1299STrond Myklebust 	trace_nfs_writeback_page_enter(inode);
1542f4ce1299STrond Myklebust 
15430522f6adSTrond Myklebust 	for (;;) {
1544ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
15457f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
15467f2f12d9STrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
15477f2f12d9STrond Myklebust 			if (ret < 0)
15487f2f12d9STrond Myklebust 				goto out_error;
15490522f6adSTrond Myklebust 			continue;
15507f2f12d9STrond Myklebust 		}
1551f4ce1299STrond Myklebust 		ret = 0;
15520522f6adSTrond Myklebust 		if (!PagePrivate(page))
15530522f6adSTrond Myklebust 			break;
15540522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
15557f2f12d9STrond Myklebust 		if (ret < 0)
15567f2f12d9STrond Myklebust 			goto out_error;
15577f2f12d9STrond Myklebust 	}
15587f2f12d9STrond Myklebust out_error:
1559f4ce1299STrond Myklebust 	trace_nfs_writeback_page_exit(inode, ret);
15607f2f12d9STrond Myklebust 	return ret;
15611c75950bSTrond Myklebust }
15621c75950bSTrond Myklebust 
1563074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
1564074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1565a6bc32b8SMel Gorman 		struct page *page, enum migrate_mode mode)
1566074cc1deSTrond Myklebust {
15672da95652SJeff Layton 	/*
15682da95652SJeff Layton 	 * If PagePrivate is set, then the page is currently associated with
15692da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
15702da95652SJeff Layton 	 *
15712da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
15722da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
15732da95652SJeff Layton 	 *        the page lock.
15742da95652SJeff Layton 	 */
15752da95652SJeff Layton 	if (PagePrivate(page))
15762da95652SJeff Layton 		return -EBUSY;
1577074cc1deSTrond Myklebust 
15788c209ce7SDavid Howells 	if (!nfs_fscache_release_page(page, GFP_KERNEL))
15798c209ce7SDavid Howells 		return -EBUSY;
1580074cc1deSTrond Myklebust 
1581a6bc32b8SMel Gorman 	return migrate_page(mapping, newpage, page, mode);
1582074cc1deSTrond Myklebust }
1583074cc1deSTrond Myklebust #endif
1584074cc1deSTrond Myklebust 
1585f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
15861da177e4SLinus Torvalds {
15871da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1588c0752cdfSAnna Schumaker 					     sizeof(struct nfs_rw_header),
15891da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
159020c2df83SPaul Mundt 					     NULL);
15911da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
15921da177e4SLinus Torvalds 		return -ENOMEM;
15931da177e4SLinus Torvalds 
159493d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
15951da177e4SLinus Torvalds 						     nfs_wdata_cachep);
15961da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
15973dd4765fSJeff Layton 		goto out_destroy_write_cache;
15981da177e4SLinus Torvalds 
15990b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
16000b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
16010b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
16020b7c0153SFred Isaman 					     NULL);
16030b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
16043dd4765fSJeff Layton 		goto out_destroy_write_mempool;
16050b7c0153SFred Isaman 
160693d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
16074c100210SYanchuan Nian 						      nfs_cdata_cachep);
16081da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
16093dd4765fSJeff Layton 		goto out_destroy_commit_cache;
16101da177e4SLinus Torvalds 
161189a09141SPeter Zijlstra 	/*
161289a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
161389a09141SPeter Zijlstra 	 *
161489a09141SPeter Zijlstra 	 *  64MB:    8192k
161589a09141SPeter Zijlstra 	 * 128MB:   11585k
161689a09141SPeter Zijlstra 	 * 256MB:   16384k
161789a09141SPeter Zijlstra 	 * 512MB:   23170k
161889a09141SPeter Zijlstra 	 *   1GB:   32768k
161989a09141SPeter Zijlstra 	 *   2GB:   46340k
162089a09141SPeter Zijlstra 	 *   4GB:   65536k
162189a09141SPeter Zijlstra 	 *   8GB:   92681k
162289a09141SPeter Zijlstra 	 *  16GB:  131072k
162389a09141SPeter Zijlstra 	 *
162489a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
162589a09141SPeter Zijlstra 	 * Limit the default to 256M
162689a09141SPeter Zijlstra 	 */
162789a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
162889a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
162989a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
163089a09141SPeter Zijlstra 
16311da177e4SLinus Torvalds 	return 0;
16323dd4765fSJeff Layton 
16333dd4765fSJeff Layton out_destroy_commit_cache:
16343dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
16353dd4765fSJeff Layton out_destroy_write_mempool:
16363dd4765fSJeff Layton 	mempool_destroy(nfs_wdata_mempool);
16373dd4765fSJeff Layton out_destroy_write_cache:
16383dd4765fSJeff Layton 	kmem_cache_destroy(nfs_wdata_cachep);
16393dd4765fSJeff Layton 	return -ENOMEM;
16401da177e4SLinus Torvalds }
16411da177e4SLinus Torvalds 
1642266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
16431da177e4SLinus Torvalds {
16441da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
16453dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
16461da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
16471a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
16481da177e4SLinus Torvalds }
16491da177e4SLinus Torvalds 
16504a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops = {
1651a4cdda59SAnna Schumaker 	.rw_mode		= FMODE_WRITE,
16524a0de55cSAnna Schumaker 	.rw_alloc_header	= nfs_writehdr_alloc,
16534a0de55cSAnna Schumaker 	.rw_free_header		= nfs_writehdr_free,
1654a4cdda59SAnna Schumaker 	.rw_release		= nfs_writeback_release_common,
16550eecb214SAnna Schumaker 	.rw_done		= nfs_writeback_done,
16560eecb214SAnna Schumaker 	.rw_result		= nfs_writeback_result,
16571ed26f33SAnna Schumaker 	.rw_initiate		= nfs_initiate_write,
16584a0de55cSAnna Schumaker };
1659