xref: /linux/fs/nfs/write.c (revision 00bfa30abe86982ce1929e9cabd703e5546106bd)
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);
456c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops;
46788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops;
47061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops;
48f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_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 
73c0752cdfSAnna Schumaker 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 
773feb2d49STrond Myklebust 	if (p) {
78cd841605SFred Isaman 		struct nfs_pgio_header *hdr = &p->header;
79cd841605SFred Isaman 
803feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
81cd841605SFred Isaman 		INIT_LIST_HEAD(&hdr->pages);
826c75dc0dSFred Isaman 		INIT_LIST_HEAD(&hdr->rpc_list);
836c75dc0dSFred Isaman 		spin_lock_init(&hdr->lock);
846c75dc0dSFred Isaman 		atomic_set(&hdr->refcnt, 0);
853feb2d49STrond Myklebust 	}
863feb2d49STrond Myklebust 	return p;
873feb2d49STrond Myklebust }
8889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_writehdr_alloc);
893feb2d49STrond Myklebust 
90cd841605SFred Isaman void nfs_writehdr_free(struct nfs_pgio_header *hdr)
913feb2d49STrond Myklebust {
92c0752cdfSAnna Schumaker 	struct nfs_rw_header *whdr = container_of(hdr, struct nfs_rw_header, header);
93cd841605SFred Isaman 	mempool_free(whdr, nfs_wdata_mempool);
943feb2d49STrond Myklebust }
9589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_writehdr_free);
963feb2d49STrond Myklebust 
977b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
987b159fc1STrond Myklebust {
997b159fc1STrond Myklebust 	ctx->error = error;
1007b159fc1STrond Myklebust 	smp_wmb();
1017b159fc1STrond Myklebust 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
1027b159fc1STrond Myklebust }
1037b159fc1STrond Myklebust 
10429418aa4SMel Gorman static struct nfs_page *
10529418aa4SMel Gorman nfs_page_find_request_locked(struct nfs_inode *nfsi, struct page *page)
106277459d2STrond Myklebust {
107277459d2STrond Myklebust 	struct nfs_page *req = NULL;
108277459d2STrond Myklebust 
10929418aa4SMel Gorman 	if (PagePrivate(page))
110277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
11129418aa4SMel Gorman 	else if (unlikely(PageSwapCache(page))) {
11229418aa4SMel Gorman 		struct nfs_page *freq, *t;
11329418aa4SMel Gorman 
11429418aa4SMel Gorman 		/* Linearly search the commit list for the correct req */
11529418aa4SMel Gorman 		list_for_each_entry_safe(freq, t, &nfsi->commit_info.list, wb_list) {
11629418aa4SMel Gorman 			if (freq->wb_page == page) {
11729418aa4SMel Gorman 				req = freq;
11829418aa4SMel Gorman 				break;
119277459d2STrond Myklebust 			}
12029418aa4SMel Gorman 		}
12129418aa4SMel Gorman 	}
12229418aa4SMel Gorman 
12329418aa4SMel Gorman 	if (req)
12429418aa4SMel Gorman 		kref_get(&req->wb_kref);
12529418aa4SMel Gorman 
126277459d2STrond Myklebust 	return req;
127277459d2STrond Myklebust }
128277459d2STrond Myklebust 
129277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page)
130277459d2STrond Myklebust {
131d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
132277459d2STrond Myklebust 	struct nfs_page *req = NULL;
133277459d2STrond Myklebust 
134587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
13529418aa4SMel Gorman 	req = nfs_page_find_request_locked(NFS_I(inode), page);
136587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
137277459d2STrond Myklebust 	return req;
138277459d2STrond Myklebust }
139277459d2STrond Myklebust 
1401da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1411da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1421da177e4SLinus Torvalds {
143d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
144a3d01454STrond Myklebust 	loff_t end, i_size;
145a3d01454STrond Myklebust 	pgoff_t end_index;
1461da177e4SLinus Torvalds 
147a3d01454STrond Myklebust 	spin_lock(&inode->i_lock);
148a3d01454STrond Myklebust 	i_size = i_size_read(inode);
149a3d01454STrond Myklebust 	end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
150d56b4ddfSMel Gorman 	if (i_size > 0 && page_file_index(page) < end_index)
151a3d01454STrond Myklebust 		goto out;
152d56b4ddfSMel Gorman 	end = page_file_offset(page) + ((loff_t)offset+count);
1531da177e4SLinus Torvalds 	if (i_size >= end)
154a3d01454STrond Myklebust 		goto out;
1551da177e4SLinus Torvalds 	i_size_write(inode, end);
156a3d01454STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
157a3d01454STrond Myklebust out:
158a3d01454STrond Myklebust 	spin_unlock(&inode->i_lock);
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds 
161a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
162a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
163a301b777STrond Myklebust {
164d56b4ddfSMel Gorman 	nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page));
165a301b777STrond Myklebust }
166a301b777STrond Myklebust 
1671da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
1681da177e4SLinus Torvalds  * covers the full page.
1691da177e4SLinus Torvalds  */
1701da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
1711da177e4SLinus Torvalds {
1721da177e4SLinus Torvalds 	if (PageUptodate(page))
1731da177e4SLinus Torvalds 		return;
1741da177e4SLinus Torvalds 	if (base != 0)
1751da177e4SLinus Torvalds 		return;
17649a70f27STrond Myklebust 	if (count != nfs_page_length(page))
1771da177e4SLinus Torvalds 		return;
1781da177e4SLinus Torvalds 	SetPageUptodate(page);
1791da177e4SLinus Torvalds }
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
1821da177e4SLinus Torvalds {
1831da177e4SLinus Torvalds 	if (wbc->for_reclaim)
184c63c7b05STrond Myklebust 		return FLUSH_HIGHPRI | FLUSH_STABLE;
185b17621feSWu Fengguang 	if (wbc->for_kupdate || wbc->for_background)
186b31268acSTrond Myklebust 		return FLUSH_LOWPRI | FLUSH_COND_STABLE;
187b31268acSTrond Myklebust 	return FLUSH_COND_STABLE;
1881da177e4SLinus Torvalds }
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds /*
19189a09141SPeter Zijlstra  * NFS congestion control
19289a09141SPeter Zijlstra  */
19389a09141SPeter Zijlstra 
19489a09141SPeter Zijlstra int nfs_congestion_kb;
19589a09141SPeter Zijlstra 
19689a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
19789a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
19889a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
19989a09141SPeter Zijlstra 
200deed85e7STrond Myklebust static void nfs_set_page_writeback(struct page *page)
20189a09141SPeter Zijlstra {
202deed85e7STrond Myklebust 	struct nfs_server *nfss = NFS_SERVER(page_file_mapping(page)->host);
2035a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
2045a6d41b3STrond Myklebust 
205deed85e7STrond Myklebust 	WARN_ON_ONCE(ret != 0);
20689a09141SPeter Zijlstra 
207277866a0SPeter Zijlstra 	if (atomic_long_inc_return(&nfss->writeback) >
2088aa7e847SJens Axboe 			NFS_CONGESTION_ON_THRESH) {
2098aa7e847SJens Axboe 		set_bdi_congested(&nfss->backing_dev_info,
2108aa7e847SJens Axboe 					BLK_RW_ASYNC);
2118aa7e847SJens Axboe 	}
21289a09141SPeter Zijlstra }
21389a09141SPeter Zijlstra 
21489a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page)
21589a09141SPeter Zijlstra {
216d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
21789a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
21889a09141SPeter Zijlstra 
21989a09141SPeter Zijlstra 	end_page_writeback(page);
220c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
2218aa7e847SJens Axboe 		clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
22289a09141SPeter Zijlstra }
22389a09141SPeter Zijlstra 
224cfb506e1STrond Myklebust static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock)
225e261f51fSTrond Myklebust {
226d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
227e261f51fSTrond Myklebust 	struct nfs_page *req;
228e261f51fSTrond Myklebust 	int ret;
229e261f51fSTrond Myklebust 
230587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
231e261f51fSTrond Myklebust 	for (;;) {
23229418aa4SMel Gorman 		req = nfs_page_find_request_locked(NFS_I(inode), page);
233074cc1deSTrond Myklebust 		if (req == NULL)
234074cc1deSTrond Myklebust 			break;
2357ad84aa9STrond Myklebust 		if (nfs_lock_request(req))
236e261f51fSTrond Myklebust 			break;
237e261f51fSTrond Myklebust 		/* Note: If we hold the page lock, as is the case in nfs_writepage,
2387ad84aa9STrond Myklebust 		 *	 then the call to nfs_lock_request() will always
239e261f51fSTrond Myklebust 		 *	 succeed provided that someone hasn't already marked the
240e261f51fSTrond Myklebust 		 *	 request as dirty (in which case we don't care).
241e261f51fSTrond Myklebust 		 */
242587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
243cfb506e1STrond Myklebust 		if (!nonblock)
244e261f51fSTrond Myklebust 			ret = nfs_wait_on_request(req);
245cfb506e1STrond Myklebust 		else
246cfb506e1STrond Myklebust 			ret = -EAGAIN;
247e261f51fSTrond Myklebust 		nfs_release_request(req);
248e261f51fSTrond Myklebust 		if (ret != 0)
249074cc1deSTrond Myklebust 			return ERR_PTR(ret);
250587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
251e261f51fSTrond Myklebust 	}
252587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
253074cc1deSTrond Myklebust 	return req;
254612c9384STrond Myklebust }
255074cc1deSTrond Myklebust 
256074cc1deSTrond Myklebust /*
257074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
258074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
259074cc1deSTrond Myklebust  */
260074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
261cfb506e1STrond Myklebust 				struct page *page, bool nonblock)
262074cc1deSTrond Myklebust {
263074cc1deSTrond Myklebust 	struct nfs_page *req;
264074cc1deSTrond Myklebust 	int ret = 0;
265074cc1deSTrond Myklebust 
266cfb506e1STrond Myklebust 	req = nfs_find_and_lock_request(page, nonblock);
267074cc1deSTrond Myklebust 	if (!req)
268074cc1deSTrond Myklebust 		goto out;
269074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
270074cc1deSTrond Myklebust 	if (IS_ERR(req))
271074cc1deSTrond Myklebust 		goto out;
272074cc1deSTrond Myklebust 
273deed85e7STrond Myklebust 	nfs_set_page_writeback(page);
274deed85e7STrond Myklebust 	WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
275074cc1deSTrond Myklebust 
276deed85e7STrond Myklebust 	ret = 0;
277f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
278f8512ad0SFred Isaman 		nfs_redirty_request(req);
279074cc1deSTrond Myklebust 		ret = pgio->pg_error;
280f8512ad0SFred Isaman 	}
281074cc1deSTrond Myklebust out:
282074cc1deSTrond Myklebust 	return ret;
283e261f51fSTrond Myklebust }
284e261f51fSTrond Myklebust 
285f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
286f758c885STrond Myklebust {
287d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
288cfb506e1STrond Myklebust 	int ret;
289f758c885STrond Myklebust 
290f758c885STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
291f758c885STrond Myklebust 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
292f758c885STrond Myklebust 
293d56b4ddfSMel Gorman 	nfs_pageio_cond_complete(pgio, page_file_index(page));
2941b430beeSWu Fengguang 	ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
295cfb506e1STrond Myklebust 	if (ret == -EAGAIN) {
296cfb506e1STrond Myklebust 		redirty_page_for_writepage(wbc, page);
297cfb506e1STrond Myklebust 		ret = 0;
298cfb506e1STrond Myklebust 	}
299cfb506e1STrond Myklebust 	return ret;
300f758c885STrond Myklebust }
301f758c885STrond Myklebust 
302e261f51fSTrond Myklebust /*
3031da177e4SLinus Torvalds  * Write an mmapped page to the server.
3041da177e4SLinus Torvalds  */
3054d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
3061da177e4SLinus Torvalds {
307f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
308e261f51fSTrond Myklebust 	int err;
3091da177e4SLinus Torvalds 
310a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc),
311a20c93e3SChristoph Hellwig 				false, &nfs_async_write_completion_ops);
312f758c885STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
313f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
314f758c885STrond Myklebust 	if (err < 0)
3154d770ccfSTrond Myklebust 		return err;
316f758c885STrond Myklebust 	if (pgio.pg_error < 0)
317f758c885STrond Myklebust 		return pgio.pg_error;
318f758c885STrond Myklebust 	return 0;
3194d770ccfSTrond Myklebust }
3204d770ccfSTrond Myklebust 
3214d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
3224d770ccfSTrond Myklebust {
323f758c885STrond Myklebust 	int ret;
3244d770ccfSTrond Myklebust 
325f758c885STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
3261da177e4SLinus Torvalds 	unlock_page(page);
327f758c885STrond Myklebust 	return ret;
328f758c885STrond Myklebust }
329f758c885STrond Myklebust 
330f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
331f758c885STrond Myklebust {
332f758c885STrond Myklebust 	int ret;
333f758c885STrond Myklebust 
334f758c885STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
335f758c885STrond Myklebust 	unlock_page(page);
336f758c885STrond Myklebust 	return ret;
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3401da177e4SLinus Torvalds {
3411da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
34272cb77f4STrond Myklebust 	unsigned long *bitlock = &NFS_I(inode)->flags;
343c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
3441da177e4SLinus Torvalds 	int err;
3451da177e4SLinus Torvalds 
34672cb77f4STrond Myklebust 	/* Stop dirtying of new pages while we sync */
34772cb77f4STrond Myklebust 	err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
34872cb77f4STrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
34972cb77f4STrond Myklebust 	if (err)
35072cb77f4STrond Myklebust 		goto out_err;
35172cb77f4STrond Myklebust 
35291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
35391d5b470SChuck Lever 
354a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false,
355a20c93e3SChristoph Hellwig 				&nfs_async_write_completion_ops);
356f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
357c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
35872cb77f4STrond Myklebust 
35972cb77f4STrond Myklebust 	clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
36072cb77f4STrond Myklebust 	smp_mb__after_clear_bit();
36172cb77f4STrond Myklebust 	wake_up_bit(bitlock, NFS_INO_FLUSHING);
36272cb77f4STrond Myklebust 
363f758c885STrond Myklebust 	if (err < 0)
36472cb77f4STrond Myklebust 		goto out_err;
36572cb77f4STrond Myklebust 	err = pgio.pg_error;
36672cb77f4STrond Myklebust 	if (err < 0)
36772cb77f4STrond Myklebust 		goto out_err;
368c63c7b05STrond Myklebust 	return 0;
36972cb77f4STrond Myklebust out_err:
37072cb77f4STrond Myklebust 	return err;
3711da177e4SLinus Torvalds }
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds /*
3741da177e4SLinus Torvalds  * Insert a write request into an inode
3751da177e4SLinus Torvalds  */
376d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
3771da177e4SLinus Torvalds {
3781da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
379e7d39069STrond Myklebust 
380e7d39069STrond Myklebust 	/* Lock the request! */
3817ad84aa9STrond Myklebust 	nfs_lock_request(req);
382e7d39069STrond Myklebust 
383e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
384011e2a7fSBryan Schumaker 	if (!nfsi->npages && NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
385a9a4a87aSTrond Myklebust 		inode->i_version++;
38629418aa4SMel Gorman 	/*
38729418aa4SMel Gorman 	 * Swap-space should not get truncated. Hence no need to plug the race
38829418aa4SMel Gorman 	 * with invalidate/truncate.
38929418aa4SMel Gorman 	 */
39029418aa4SMel Gorman 	if (likely(!PageSwapCache(req->wb_page))) {
3912df485a7STrond Myklebust 		set_bit(PG_MAPPED, &req->wb_flags);
392deb7d638STrond Myklebust 		SetPagePrivate(req->wb_page);
393277459d2STrond Myklebust 		set_page_private(req->wb_page, (unsigned long)req);
39429418aa4SMel Gorman 	}
3951da177e4SLinus Torvalds 	nfsi->npages++;
396c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
397e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds /*
40189a09141SPeter Zijlstra  * Remove a write request from an inode
4021da177e4SLinus Torvalds  */
4031da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
4041da177e4SLinus Torvalds {
4053d4ff43dSAl Viro 	struct inode *inode = req->wb_context->dentry->d_inode;
4061da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4071da177e4SLinus Torvalds 
408587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
40929418aa4SMel Gorman 	if (likely(!PageSwapCache(req->wb_page))) {
410277459d2STrond Myklebust 		set_page_private(req->wb_page, 0);
411deb7d638STrond Myklebust 		ClearPagePrivate(req->wb_page);
4122df485a7STrond Myklebust 		clear_bit(PG_MAPPED, &req->wb_flags);
41329418aa4SMel Gorman 	}
4141da177e4SLinus Torvalds 	nfsi->npages--;
415587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
4161da177e4SLinus Torvalds 	nfs_release_request(req);
4171da177e4SLinus Torvalds }
4181da177e4SLinus Torvalds 
41961822ab5STrond Myklebust static void
4206d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
42161822ab5STrond Myklebust {
42261822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
42361822ab5STrond Myklebust }
42461822ab5STrond Myklebust 
42589d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
4268dd37758STrond Myklebust /**
4278dd37758STrond Myklebust  * nfs_request_add_commit_list - add request to a commit list
4288dd37758STrond Myklebust  * @req: pointer to a struct nfs_page
429ea2cf228SFred Isaman  * @dst: commit list head
430ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
4318dd37758STrond Myklebust  *
432ea2cf228SFred Isaman  * This sets the PG_CLEAN bit, updates the cinfo count of
4338dd37758STrond Myklebust  * number of outstanding requests requiring a commit as well as
4348dd37758STrond Myklebust  * the MM page stats.
4358dd37758STrond Myklebust  *
436ea2cf228SFred Isaman  * The caller must _not_ hold the cinfo->lock, but must be
4378dd37758STrond Myklebust  * holding the nfs_page lock.
4388dd37758STrond Myklebust  */
4398dd37758STrond Myklebust void
440ea2cf228SFred Isaman nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst,
441ea2cf228SFred Isaman 			    struct nfs_commit_info *cinfo)
4428dd37758STrond Myklebust {
4438dd37758STrond Myklebust 	set_bit(PG_CLEAN, &(req)->wb_flags);
444ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
445ea2cf228SFred Isaman 	nfs_list_add_request(req, dst);
446ea2cf228SFred Isaman 	cinfo->mds->ncommit++;
447ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
44856f9cd68SFred Isaman 	if (!cinfo->dreq) {
4498dd37758STrond Myklebust 		inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
450d56b4ddfSMel Gorman 		inc_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
45156f9cd68SFred Isaman 			     BDI_RECLAIMABLE);
45256f9cd68SFred Isaman 		__mark_inode_dirty(req->wb_context->dentry->d_inode,
45356f9cd68SFred Isaman 				   I_DIRTY_DATASYNC);
45456f9cd68SFred Isaman 	}
4558dd37758STrond Myklebust }
4568dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
4578dd37758STrond Myklebust 
4588dd37758STrond Myklebust /**
4598dd37758STrond Myklebust  * nfs_request_remove_commit_list - Remove request from a commit list
4608dd37758STrond Myklebust  * @req: pointer to a nfs_page
461ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
4628dd37758STrond Myklebust  *
463ea2cf228SFred Isaman  * This clears the PG_CLEAN bit, and updates the cinfo's count of
4648dd37758STrond Myklebust  * number of outstanding requests requiring a commit
4658dd37758STrond Myklebust  * It does not update the MM page stats.
4668dd37758STrond Myklebust  *
467ea2cf228SFred Isaman  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
4688dd37758STrond Myklebust  */
4698dd37758STrond Myklebust void
470ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req,
471ea2cf228SFred Isaman 			       struct nfs_commit_info *cinfo)
4728dd37758STrond Myklebust {
4738dd37758STrond Myklebust 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
4748dd37758STrond Myklebust 		return;
4758dd37758STrond Myklebust 	nfs_list_remove_request(req);
476ea2cf228SFred Isaman 	cinfo->mds->ncommit--;
4778dd37758STrond Myklebust }
4788dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
4798dd37758STrond Myklebust 
480ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
481ea2cf228SFred Isaman 				      struct inode *inode)
482ea2cf228SFred Isaman {
483ea2cf228SFred Isaman 	cinfo->lock = &inode->i_lock;
484ea2cf228SFred Isaman 	cinfo->mds = &NFS_I(inode)->commit_info;
485ea2cf228SFred Isaman 	cinfo->ds = pnfs_get_ds_info(inode);
486b359f9d0SFred Isaman 	cinfo->dreq = NULL;
487f453a54aSFred Isaman 	cinfo->completion_ops = &nfs_commit_completion_ops;
488ea2cf228SFred Isaman }
489ea2cf228SFred Isaman 
490ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
491ea2cf228SFred Isaman 		    struct inode *inode,
492ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq)
493ea2cf228SFred Isaman {
4941763da12SFred Isaman 	if (dreq)
4951763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
4961763da12SFred Isaman 	else
497ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(cinfo, inode);
498ea2cf228SFred Isaman }
499ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo);
5008dd37758STrond Myklebust 
5011da177e4SLinus Torvalds /*
5021da177e4SLinus Torvalds  * Add a request to the inode's commit list.
5031da177e4SLinus Torvalds  */
5041763da12SFred Isaman void
505ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
506ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
5071da177e4SLinus Torvalds {
508ea2cf228SFred Isaman 	if (pnfs_mark_request_commit(req, lseg, cinfo))
5098dd37758STrond Myklebust 		return;
510ea2cf228SFred Isaman 	nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo);
5111da177e4SLinus Torvalds }
5128e821cadSTrond Myklebust 
513d6d6dc7cSFred Isaman static void
514d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page)
515e468bae9STrond Myklebust {
516e468bae9STrond Myklebust 	dec_zone_page_state(page, NR_UNSTABLE_NFS);
517d56b4ddfSMel Gorman 	dec_bdi_stat(page_file_mapping(page)->backing_dev_info, BDI_RECLAIMABLE);
518e468bae9STrond Myklebust }
519d6d6dc7cSFred Isaman 
5208dd37758STrond Myklebust static void
521d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req)
522d6d6dc7cSFred Isaman {
5238dd37758STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
5248dd37758STrond Myklebust 		struct inode *inode = req->wb_context->dentry->d_inode;
525ea2cf228SFred Isaman 		struct nfs_commit_info cinfo;
526d6d6dc7cSFred Isaman 
527ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(&cinfo, inode);
528ea2cf228SFred Isaman 		if (!pnfs_clear_request_commit(req, &cinfo)) {
529ea2cf228SFred Isaman 			spin_lock(cinfo.lock);
530ea2cf228SFred Isaman 			nfs_request_remove_commit_list(req, &cinfo);
531ea2cf228SFred Isaman 			spin_unlock(cinfo.lock);
532d6d6dc7cSFred Isaman 		}
5338dd37758STrond Myklebust 		nfs_clear_page_commit(req->wb_page);
5348dd37758STrond Myklebust 	}
535e468bae9STrond Myklebust }
536e468bae9STrond Myklebust 
5378e821cadSTrond Myklebust static inline
5389c7e1b3dSAnna Schumaker int nfs_write_need_commit(struct nfs_pgio_data *data)
5398e821cadSTrond Myklebust {
540465d5243SFred Isaman 	if (data->verf.committed == NFS_DATA_SYNC)
541cd841605SFred Isaman 		return data->header->lseg == NULL;
5428e821cadSTrond Myklebust 	return data->verf.committed != NFS_FILE_SYNC;
5438e821cadSTrond Myklebust }
5448e821cadSTrond Myklebust 
5458e821cadSTrond Myklebust #else
54668cd6fa4SBryan Schumaker static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
54768cd6fa4SBryan Schumaker 				      struct inode *inode)
54868cd6fa4SBryan Schumaker {
54968cd6fa4SBryan Schumaker }
55068cd6fa4SBryan Schumaker 
55168cd6fa4SBryan Schumaker void nfs_init_cinfo(struct nfs_commit_info *cinfo,
55268cd6fa4SBryan Schumaker 		    struct inode *inode,
55368cd6fa4SBryan Schumaker 		    struct nfs_direct_req *dreq)
55468cd6fa4SBryan Schumaker {
55568cd6fa4SBryan Schumaker }
55668cd6fa4SBryan Schumaker 
5571763da12SFred Isaman void
558ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
559ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
5608e821cadSTrond Myklebust {
5618e821cadSTrond Myklebust }
5628e821cadSTrond Myklebust 
5638dd37758STrond Myklebust static void
564e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
565e468bae9STrond Myklebust {
566e468bae9STrond Myklebust }
567e468bae9STrond Myklebust 
5688e821cadSTrond Myklebust static inline
5699c7e1b3dSAnna Schumaker int nfs_write_need_commit(struct nfs_pgio_data *data)
5708e821cadSTrond Myklebust {
5718e821cadSTrond Myklebust 	return 0;
5728e821cadSTrond Myklebust }
5738e821cadSTrond Myklebust 
5741da177e4SLinus Torvalds #endif
5751da177e4SLinus Torvalds 
576061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
5776c75dc0dSFred Isaman {
578ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
5796c75dc0dSFred Isaman 	unsigned long bytes = 0;
5806c75dc0dSFred Isaman 
5816c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
5826c75dc0dSFred Isaman 		goto out;
583ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
5846c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
5856c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
5866c75dc0dSFred Isaman 
5876c75dc0dSFred Isaman 		bytes += req->wb_bytes;
5886c75dc0dSFred Isaman 		nfs_list_remove_request(req);
5896c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
5906c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
591d1182b33STrond Myklebust 			nfs_set_pageerror(req->wb_page);
5926c75dc0dSFred Isaman 			nfs_context_set_write_error(req->wb_context, hdr->error);
5936c75dc0dSFred Isaman 			goto remove_req;
5946c75dc0dSFred Isaman 		}
5956c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
5966c75dc0dSFred Isaman 			nfs_mark_request_dirty(req);
5976c75dc0dSFred Isaman 			goto next;
5986c75dc0dSFred Isaman 		}
5996c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
600f79d06f5SAnna Schumaker 			memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
601ea2cf228SFred Isaman 			nfs_mark_request_commit(req, hdr->lseg, &cinfo);
6026c75dc0dSFred Isaman 			goto next;
6036c75dc0dSFred Isaman 		}
6046c75dc0dSFred Isaman remove_req:
6056c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
6066c75dc0dSFred Isaman next:
6071d1afcbcSTrond Myklebust 		nfs_unlock_request(req);
608d1182b33STrond Myklebust 		nfs_end_page_writeback(req->wb_page);
6093aff4ebbSTrond Myklebust 		nfs_release_request(req);
6106c75dc0dSFred Isaman 	}
6116c75dc0dSFred Isaman out:
6126c75dc0dSFred Isaman 	hdr->release(hdr);
6136c75dc0dSFred Isaman }
6146c75dc0dSFred Isaman 
61589d77c8fSBryan Schumaker #if  IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
616ea2cf228SFred Isaman static unsigned long
617ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
618fb8a1f11STrond Myklebust {
619ea2cf228SFred Isaman 	return cinfo->mds->ncommit;
620fb8a1f11STrond Myklebust }
621fb8a1f11STrond Myklebust 
622ea2cf228SFred Isaman /* cinfo->lock held by caller */
6231763da12SFred Isaman int
624ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
625ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
626d6d6dc7cSFred Isaman {
627d6d6dc7cSFred Isaman 	struct nfs_page *req, *tmp;
628d6d6dc7cSFred Isaman 	int ret = 0;
629d6d6dc7cSFred Isaman 
630d6d6dc7cSFred Isaman 	list_for_each_entry_safe(req, tmp, src, wb_list) {
6318dd37758STrond Myklebust 		if (!nfs_lock_request(req))
6328dd37758STrond Myklebust 			continue;
6337ad84aa9STrond Myklebust 		kref_get(&req->wb_kref);
634ea2cf228SFred Isaman 		if (cond_resched_lock(cinfo->lock))
6353b3be88dSTrond Myklebust 			list_safe_reset_next(req, tmp, wb_list);
636ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
6378dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
638d6d6dc7cSFred Isaman 		ret++;
6391763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
640d6d6dc7cSFred Isaman 			break;
641d6d6dc7cSFred Isaman 	}
642d6d6dc7cSFred Isaman 	return ret;
643d6d6dc7cSFred Isaman }
644d6d6dc7cSFred Isaman 
6451da177e4SLinus Torvalds /*
6461da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
6471da177e4SLinus Torvalds  * @inode: NFS inode to scan
648ea2cf228SFred Isaman  * @dst: mds destination list
649ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
6501da177e4SLinus Torvalds  *
6511da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
6521da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
6531da177e4SLinus Torvalds  */
6541763da12SFred Isaman int
655ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
656ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
6571da177e4SLinus Torvalds {
658d6d6dc7cSFred Isaman 	int ret = 0;
659fb8a1f11STrond Myklebust 
660ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
661ea2cf228SFred Isaman 	if (cinfo->mds->ncommit > 0) {
6628dd37758STrond Myklebust 		const int max = INT_MAX;
663d6d6dc7cSFred Isaman 
664ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
665ea2cf228SFred Isaman 					   cinfo, max);
666ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
667d6d6dc7cSFred Isaman 	}
668ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
669ff778d02STrond Myklebust 	return ret;
6701da177e4SLinus Torvalds }
671d6d6dc7cSFred Isaman 
672c42de9ddSTrond Myklebust #else
673ea2cf228SFred Isaman static unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
674fb8a1f11STrond Myklebust {
675fb8a1f11STrond Myklebust 	return 0;
676fb8a1f11STrond Myklebust }
677fb8a1f11STrond Myklebust 
6781763da12SFred Isaman int nfs_scan_commit(struct inode *inode, struct list_head *dst,
679ea2cf228SFred Isaman 		    struct nfs_commit_info *cinfo)
680c42de9ddSTrond Myklebust {
681c42de9ddSTrond Myklebust 	return 0;
682c42de9ddSTrond Myklebust }
6831da177e4SLinus Torvalds #endif
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds /*
686e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
687e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
6881da177e4SLinus Torvalds  *
689e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
690e7d39069STrond Myklebust  * to disk.
6911da177e4SLinus Torvalds  */
692e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
693e7d39069STrond Myklebust 		struct page *page,
694e7d39069STrond Myklebust 		unsigned int offset,
695e7d39069STrond Myklebust 		unsigned int bytes)
6961da177e4SLinus Torvalds {
697e7d39069STrond Myklebust 	struct nfs_page *req;
698e7d39069STrond Myklebust 	unsigned int rqend;
699e7d39069STrond Myklebust 	unsigned int end;
7001da177e4SLinus Torvalds 	int error;
701277459d2STrond Myklebust 
702e7d39069STrond Myklebust 	if (!PagePrivate(page))
703e7d39069STrond Myklebust 		return NULL;
704e7d39069STrond Myklebust 
705e7d39069STrond Myklebust 	end = offset + bytes;
706e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
707e7d39069STrond Myklebust 
708e7d39069STrond Myklebust 	for (;;) {
70929418aa4SMel Gorman 		req = nfs_page_find_request_locked(NFS_I(inode), page);
710e7d39069STrond Myklebust 		if (req == NULL)
711e7d39069STrond Myklebust 			goto out_unlock;
712e7d39069STrond Myklebust 
713e7d39069STrond Myklebust 		rqend = req->wb_offset + req->wb_bytes;
714e7d39069STrond Myklebust 		/*
715e7d39069STrond Myklebust 		 * Tell the caller to flush out the request if
716e7d39069STrond Myklebust 		 * the offsets are non-contiguous.
717e7d39069STrond Myklebust 		 * Note: nfs_flush_incompatible() will already
718e7d39069STrond Myklebust 		 * have flushed out requests having wrong owners.
719e7d39069STrond Myklebust 		 */
720e468bae9STrond Myklebust 		if (offset > rqend
721e7d39069STrond Myklebust 		    || end < req->wb_offset)
722e7d39069STrond Myklebust 			goto out_flushme;
723e7d39069STrond Myklebust 
7247ad84aa9STrond Myklebust 		if (nfs_lock_request(req))
725e7d39069STrond Myklebust 			break;
726e7d39069STrond Myklebust 
727e7d39069STrond Myklebust 		/* The request is locked, so wait and then retry */
728587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
7291da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
7301da177e4SLinus Torvalds 		nfs_release_request(req);
731e7d39069STrond Myklebust 		if (error != 0)
732e7d39069STrond Myklebust 			goto out_err;
733e7d39069STrond Myklebust 		spin_lock(&inode->i_lock);
7341da177e4SLinus Torvalds 	}
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
7371da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
7381da177e4SLinus Torvalds 		req->wb_offset = offset;
7391da177e4SLinus Torvalds 		req->wb_pgbase = offset;
7401da177e4SLinus Torvalds 	}
7411da177e4SLinus Torvalds 	if (end > rqend)
7421da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
743e7d39069STrond Myklebust 	else
744e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
745e7d39069STrond Myklebust out_unlock:
746e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
747ca138f36SFred Isaman 	if (req)
7488dd37758STrond Myklebust 		nfs_clear_request_commit(req);
749e7d39069STrond Myklebust 	return req;
750e7d39069STrond Myklebust out_flushme:
751e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
752e7d39069STrond Myklebust 	nfs_release_request(req);
753e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
754e7d39069STrond Myklebust out_err:
755e7d39069STrond Myklebust 	return ERR_PTR(error);
756e7d39069STrond Myklebust }
7571da177e4SLinus Torvalds 
758e7d39069STrond Myklebust /*
759e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
760e7d39069STrond Myklebust  *
761e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
762e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
763e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
764e7d39069STrond Myklebust  */
765e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
766e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
767e7d39069STrond Myklebust {
768d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
769e7d39069STrond Myklebust 	struct nfs_page	*req;
770e7d39069STrond Myklebust 
771e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
772e7d39069STrond Myklebust 	if (req != NULL)
773e7d39069STrond Myklebust 		goto out;
774e7d39069STrond Myklebust 	req = nfs_create_request(ctx, inode, page, offset, bytes);
775e7d39069STrond Myklebust 	if (IS_ERR(req))
776e7d39069STrond Myklebust 		goto out;
777d6d6dc7cSFred Isaman 	nfs_inode_add_request(inode, req);
778efc91ed0STrond Myklebust out:
77961e930a9STrond Myklebust 	return req;
7801da177e4SLinus Torvalds }
7811da177e4SLinus Torvalds 
782e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
783e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
784e7d39069STrond Myklebust {
785e7d39069STrond Myklebust 	struct nfs_page	*req;
786e7d39069STrond Myklebust 
787e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
788e7d39069STrond Myklebust 	if (IS_ERR(req))
789e7d39069STrond Myklebust 		return PTR_ERR(req);
790e7d39069STrond Myklebust 	/* Update file length */
791e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
792e7d39069STrond Myklebust 	nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
793a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
7941d1afcbcSTrond Myklebust 	nfs_unlock_and_release_request(req);
795e7d39069STrond Myklebust 	return 0;
796e7d39069STrond Myklebust }
797e7d39069STrond Myklebust 
7981da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
7991da177e4SLinus Torvalds {
800cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
8012a369153STrond Myklebust 	struct nfs_lock_context *l_ctx;
8021da177e4SLinus Torvalds 	struct nfs_page	*req;
8031a54533eSTrond Myklebust 	int do_flush, status;
8041da177e4SLinus Torvalds 	/*
8051da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
8061da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
8071da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
8081da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
8091da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
8101da177e4SLinus Torvalds 	 * dropped page.
8111da177e4SLinus Torvalds 	 */
8121a54533eSTrond Myklebust 	do {
813277459d2STrond Myklebust 		req = nfs_page_find_request(page);
8141a54533eSTrond Myklebust 		if (req == NULL)
8151a54533eSTrond Myklebust 			return 0;
8162a369153STrond Myklebust 		l_ctx = req->wb_lock_context;
8172a369153STrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx;
8180f1d2605STrond Myklebust 		if (l_ctx && ctx->dentry->d_inode->i_flock != NULL) {
8192a369153STrond Myklebust 			do_flush |= l_ctx->lockowner.l_owner != current->files
8202a369153STrond Myklebust 				|| l_ctx->lockowner.l_pid != current->tgid;
8212a369153STrond Myklebust 		}
8221da177e4SLinus Torvalds 		nfs_release_request(req);
8231a54533eSTrond Myklebust 		if (!do_flush)
8241a54533eSTrond Myklebust 			return 0;
825d56b4ddfSMel Gorman 		status = nfs_wb_page(page_file_mapping(page)->host, page);
8261a54533eSTrond Myklebust 	} while (status == 0);
8271a54533eSTrond Myklebust 	return status;
8281da177e4SLinus Torvalds }
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds /*
831dc24826bSAndy Adamson  * Avoid buffered writes when a open context credential's key would
832dc24826bSAndy Adamson  * expire soon.
833dc24826bSAndy Adamson  *
834dc24826bSAndy Adamson  * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
835dc24826bSAndy Adamson  *
836dc24826bSAndy Adamson  * Return 0 and set a credential flag which triggers the inode to flush
837dc24826bSAndy Adamson  * and performs  NFS_FILE_SYNC writes if the key will expired within
838dc24826bSAndy Adamson  * RPC_KEY_EXPIRE_TIMEO.
839dc24826bSAndy Adamson  */
840dc24826bSAndy Adamson int
841dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode)
842dc24826bSAndy Adamson {
843dc24826bSAndy Adamson 	struct nfs_open_context *ctx = nfs_file_open_context(filp);
844dc24826bSAndy Adamson 	struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
845dc24826bSAndy Adamson 
846dc24826bSAndy Adamson 	return rpcauth_key_timeout_notify(auth, ctx->cred);
847dc24826bSAndy Adamson }
848dc24826bSAndy Adamson 
849dc24826bSAndy Adamson /*
850dc24826bSAndy Adamson  * Test if the open context credential key is marked to expire soon.
851dc24826bSAndy Adamson  */
852dc24826bSAndy Adamson bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx)
853dc24826bSAndy Adamson {
854dc24826bSAndy Adamson 	return rpcauth_cred_key_to_expire(ctx->cred);
855dc24826bSAndy Adamson }
856dc24826bSAndy Adamson 
857dc24826bSAndy Adamson /*
8585d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
8595d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
8605d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
8615d47a356STrond Myklebust  */
8628d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
8635d47a356STrond Myklebust {
864d529ef83SJeff Layton 	struct nfs_inode *nfsi = NFS_I(inode);
865d529ef83SJeff Layton 
8668d197a56STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
8678d197a56STrond Myklebust 		goto out;
868d529ef83SJeff Layton 	if (nfsi->cache_validity & (NFS_INO_INVALID_DATA|NFS_INO_REVAL_PAGECACHE))
869d529ef83SJeff Layton 		return false;
8704db72b40SJeff Layton 	smp_rmb();
871d529ef83SJeff Layton 	if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags))
8728d197a56STrond Myklebust 		return false;
8738d197a56STrond Myklebust out:
8748d197a56STrond Myklebust 	return PageUptodate(page) != 0;
8755d47a356STrond Myklebust }
8765d47a356STrond Myklebust 
877c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or
878c7559663SScott Mayhew  * if we have the whole file locked for writing), it may be more efficient to
879c7559663SScott Mayhew  * extend the write to cover the entire page in order to avoid fragmentation
880c7559663SScott Mayhew  * inefficiencies.
881c7559663SScott Mayhew  *
882263b4509SScott Mayhew  * If the file is opened for synchronous writes then we can just skip the rest
883263b4509SScott Mayhew  * of the checks.
884c7559663SScott Mayhew  */
885c7559663SScott Mayhew static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode)
886c7559663SScott Mayhew {
887c7559663SScott Mayhew 	if (file->f_flags & O_DSYNC)
888c7559663SScott Mayhew 		return 0;
889263b4509SScott Mayhew 	if (!nfs_write_pageuptodate(page, inode))
890263b4509SScott Mayhew 		return 0;
891c7559663SScott Mayhew 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
892c7559663SScott Mayhew 		return 1;
893263b4509SScott Mayhew 	if (inode->i_flock == NULL || (inode->i_flock->fl_start == 0 &&
894c7559663SScott Mayhew 			inode->i_flock->fl_end == OFFSET_MAX &&
895263b4509SScott Mayhew 			inode->i_flock->fl_type != F_RDLCK))
896c7559663SScott Mayhew 		return 1;
897c7559663SScott Mayhew 	return 0;
898c7559663SScott Mayhew }
899c7559663SScott Mayhew 
9005d47a356STrond Myklebust /*
9011da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
9021da177e4SLinus Torvalds  *
9031da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
9041da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
9051da177e4SLinus Torvalds  */
9061da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
9071da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
9081da177e4SLinus Torvalds {
909cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
910d56b4ddfSMel Gorman 	struct inode	*inode = page_file_mapping(page)->host;
9111da177e4SLinus Torvalds 	int		status = 0;
9121da177e4SLinus Torvalds 
91391d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
91491d5b470SChuck Lever 
9156de1472fSAl Viro 	dprintk("NFS:       nfs_updatepage(%pD2 %d@%lld)\n",
9166de1472fSAl Viro 		file, count, (long long)(page_file_offset(page) + offset));
9171da177e4SLinus Torvalds 
918c7559663SScott Mayhew 	if (nfs_can_extend_write(file, page, inode)) {
91949a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
9201da177e4SLinus Torvalds 		offset = 0;
9211da177e4SLinus Torvalds 	}
9221da177e4SLinus Torvalds 
923e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
92403fa9e84STrond Myklebust 	if (status < 0)
92503fa9e84STrond Myklebust 		nfs_set_pageerror(page);
92659b7c05fSTrond Myklebust 	else
92759b7c05fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
9281da177e4SLinus Torvalds 
92948186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
9301da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
9311da177e4SLinus Torvalds 	return status;
9321da177e4SLinus Torvalds }
9331da177e4SLinus Torvalds 
9343ff7576dSTrond Myklebust static int flush_task_priority(int how)
9351da177e4SLinus Torvalds {
9361da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
9371da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
9381da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
9391da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
9401da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
9411da177e4SLinus Torvalds 	}
9421da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
9431da177e4SLinus Torvalds }
9441da177e4SLinus Torvalds 
945c5996c4eSFred Isaman int nfs_initiate_write(struct rpc_clnt *clnt,
9469c7e1b3dSAnna Schumaker 		       struct nfs_pgio_data *data,
947788e7a89STrond Myklebust 		       const struct rpc_call_ops *call_ops,
9489f0ec176SAndy Adamson 		       int how, int flags)
9491da177e4SLinus Torvalds {
950cd841605SFred Isaman 	struct inode *inode = data->header->inode;
9513ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
95207737691STrond Myklebust 	struct rpc_task *task;
953bdc7f021STrond Myklebust 	struct rpc_message msg = {
954bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
955bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
956cd841605SFred Isaman 		.rpc_cred = data->header->cred,
957bdc7f021STrond Myklebust 	};
95884115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
959d138d5d1SAndy Adamson 		.rpc_client = clnt,
96007737691STrond Myklebust 		.task = &data->task,
961bdc7f021STrond Myklebust 		.rpc_message = &msg,
96284115e1cSTrond Myklebust 		.callback_ops = call_ops,
96384115e1cSTrond Myklebust 		.callback_data = data,
964101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
9659f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
9663ff7576dSTrond Myklebust 		.priority = priority,
96784115e1cSTrond Myklebust 	};
9682c61be0aSTrond Myklebust 	int ret = 0;
9691da177e4SLinus Torvalds 
970d138d5d1SAndy Adamson 	/* Set up the initial task struct.  */
971d138d5d1SAndy Adamson 	NFS_PROTO(inode)->write_setup(data, &msg);
972d138d5d1SAndy Adamson 
973d138d5d1SAndy Adamson 	dprintk("NFS: %5u initiated write call "
9741e8968c5SNiels de Vos 		"(req %s/%llu, %u bytes @ offset %llu)\n",
975d138d5d1SAndy Adamson 		data->task.tk_pid,
976d138d5d1SAndy Adamson 		inode->i_sb->s_id,
9771e8968c5SNiels de Vos 		(unsigned long long)NFS_FILEID(inode),
978d138d5d1SAndy Adamson 		data->args.count,
979d138d5d1SAndy Adamson 		(unsigned long long)data->args.offset);
980d138d5d1SAndy Adamson 
9818c21c62cSWeston Andros Adamson 	nfs4_state_protect_write(NFS_SERVER(inode)->nfs_client,
9828c21c62cSWeston Andros Adamson 				 &task_setup_data.rpc_client, &msg, data);
9838c21c62cSWeston Andros Adamson 
984d138d5d1SAndy Adamson 	task = rpc_run_task(&task_setup_data);
985d138d5d1SAndy Adamson 	if (IS_ERR(task)) {
986d138d5d1SAndy Adamson 		ret = PTR_ERR(task);
987d138d5d1SAndy Adamson 		goto out;
988d138d5d1SAndy Adamson 	}
989d138d5d1SAndy Adamson 	if (how & FLUSH_SYNC) {
990d138d5d1SAndy Adamson 		ret = rpc_wait_for_completion_task(task);
991d138d5d1SAndy Adamson 		if (ret == 0)
992d138d5d1SAndy Adamson 			ret = task->tk_status;
993d138d5d1SAndy Adamson 	}
994d138d5d1SAndy Adamson 	rpc_put_task(task);
995d138d5d1SAndy Adamson out:
996d138d5d1SAndy Adamson 	return ret;
997d138d5d1SAndy Adamson }
998a69aef14SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_write);
999d138d5d1SAndy Adamson 
1000d138d5d1SAndy Adamson /*
1001d138d5d1SAndy Adamson  * Set up the argument/result storage required for the RPC call.
1002d138d5d1SAndy Adamson  */
10039c7e1b3dSAnna Schumaker static void nfs_write_rpcsetup(struct nfs_pgio_data *data,
1004d138d5d1SAndy Adamson 		unsigned int count, unsigned int offset,
1005ea2cf228SFred Isaman 		int how, struct nfs_commit_info *cinfo)
1006d138d5d1SAndy Adamson {
10076c75dc0dSFred Isaman 	struct nfs_page *req = data->header->req;
1008d138d5d1SAndy Adamson 
10091da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
10101da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
10111da177e4SLinus Torvalds 
10126c75dc0dSFred Isaman 	data->args.fh     = NFS_FH(data->header->inode);
10131da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
10142bea038cSBoaz Harrosh 	/* pnfs_set_layoutcommit needs this */
10152bea038cSBoaz Harrosh 	data->mds_offset = data->args.offset;
10161da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
101730dd374fSFred Isaman 	data->args.pages  = data->pages.pagevec;
10181da177e4SLinus Torvalds 	data->args.count  = count;
1019383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
1020f11ac8dbSTrond Myklebust 	data->args.lock_context = req->wb_lock_context;
1021bdc7f021STrond Myklebust 	data->args.stable  = NFS_UNSTABLE;
102287ed5eb4STrond Myklebust 	switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
102387ed5eb4STrond Myklebust 	case 0:
102487ed5eb4STrond Myklebust 		break;
102587ed5eb4STrond Myklebust 	case FLUSH_COND_STABLE:
1026ea2cf228SFred Isaman 		if (nfs_reqs_to_commit(cinfo))
102787ed5eb4STrond Myklebust 			break;
102887ed5eb4STrond Myklebust 	default:
1029bdc7f021STrond Myklebust 		data->args.stable = NFS_FILE_SYNC;
1030bdc7f021STrond Myklebust 	}
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
10331da177e4SLinus Torvalds 	data->res.count   = count;
10341da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
10350e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
10366e4efd56STrond Myklebust }
10371da177e4SLinus Torvalds 
10389c7e1b3dSAnna Schumaker static int nfs_do_write(struct nfs_pgio_data *data,
10396e4efd56STrond Myklebust 		const struct rpc_call_ops *call_ops,
10406e4efd56STrond Myklebust 		int how)
10416e4efd56STrond Myklebust {
1042cd841605SFred Isaman 	struct inode *inode = data->header->inode;
10430382b744SAndy Adamson 
10449f0ec176SAndy Adamson 	return nfs_initiate_write(NFS_CLIENT(inode), data, call_ops, how, 0);
10451da177e4SLinus Torvalds }
10461da177e4SLinus Torvalds 
1047275acaafSTrond Myklebust static int nfs_do_multiple_writes(struct list_head *head,
1048275acaafSTrond Myklebust 		const struct rpc_call_ops *call_ops,
1049275acaafSTrond Myklebust 		int how)
1050275acaafSTrond Myklebust {
10519c7e1b3dSAnna Schumaker 	struct nfs_pgio_data *data;
1052275acaafSTrond Myklebust 	int ret = 0;
1053275acaafSTrond Myklebust 
1054275acaafSTrond Myklebust 	while (!list_empty(head)) {
1055275acaafSTrond Myklebust 		int ret2;
1056275acaafSTrond Myklebust 
10579c7e1b3dSAnna Schumaker 		data = list_first_entry(head, struct nfs_pgio_data, list);
1058275acaafSTrond Myklebust 		list_del_init(&data->list);
1059275acaafSTrond Myklebust 
1060dce81290STrond Myklebust 		ret2 = nfs_do_write(data, call_ops, how);
1061275acaafSTrond Myklebust 		 if (ret == 0)
1062275acaafSTrond Myklebust 			 ret = ret2;
1063275acaafSTrond Myklebust 	}
1064275acaafSTrond Myklebust 	return ret;
1065275acaafSTrond Myklebust }
1066275acaafSTrond Myklebust 
10676d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
10686d884e8fSFred  * call this on each, which will prepare them to be retried on next
10696d884e8fSFred  * writeback using standard nfs.
10706d884e8fSFred  */
10716d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
10726d884e8fSFred {
10736d884e8fSFred 	nfs_mark_request_dirty(req);
10741d1afcbcSTrond Myklebust 	nfs_unlock_request(req);
1075d1182b33STrond Myklebust 	nfs_end_page_writeback(req->wb_page);
10763aff4ebbSTrond Myklebust 	nfs_release_request(req);
10776d884e8fSFred }
10786d884e8fSFred 
1079061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head)
10806c75dc0dSFred Isaman {
10816c75dc0dSFred Isaman 	struct nfs_page	*req;
10826c75dc0dSFred Isaman 
10836c75dc0dSFred Isaman 	while (!list_empty(head)) {
10846c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
10856c75dc0dSFred Isaman 		nfs_list_remove_request(req);
10866c75dc0dSFred Isaman 		nfs_redirty_request(req);
10876c75dc0dSFred Isaman 	}
10886c75dc0dSFred Isaman }
10896c75dc0dSFred Isaman 
1090061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1091061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1092061ae2edSFred Isaman 	.completion = nfs_write_completion,
1093061ae2edSFred Isaman };
1094061ae2edSFred Isaman 
109525b11dcdSTrond Myklebust static void nfs_flush_error(struct nfs_pageio_descriptor *desc,
109625b11dcdSTrond Myklebust 		struct nfs_pgio_header *hdr)
109725b11dcdSTrond Myklebust {
109825b11dcdSTrond Myklebust 	set_bit(NFS_IOHDR_REDO, &hdr->flags);
109925b11dcdSTrond Myklebust 	while (!list_empty(&hdr->rpc_list)) {
11009c7e1b3dSAnna Schumaker 		struct nfs_pgio_data *data = list_first_entry(&hdr->rpc_list,
11019c7e1b3dSAnna Schumaker 				struct nfs_pgio_data, list);
110225b11dcdSTrond Myklebust 		list_del(&data->list);
1103*00bfa30aSAnna Schumaker 		nfs_pgio_data_release(data);
110425b11dcdSTrond Myklebust 	}
110525b11dcdSTrond Myklebust 	desc->pg_completion_ops->error_cleanup(&desc->pg_list);
110625b11dcdSTrond Myklebust }
110725b11dcdSTrond Myklebust 
11081da177e4SLinus Torvalds /*
11091da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
11101da177e4SLinus Torvalds  * contiguous dirty area on one page.
11111da177e4SLinus Torvalds  */
11126c75dc0dSFred Isaman static int nfs_flush_multi(struct nfs_pageio_descriptor *desc,
11136c75dc0dSFred Isaman 			   struct nfs_pgio_header *hdr)
11141da177e4SLinus Torvalds {
11156c75dc0dSFred Isaman 	struct nfs_page *req = hdr->req;
11161da177e4SLinus Torvalds 	struct page *page = req->wb_page;
11179c7e1b3dSAnna Schumaker 	struct nfs_pgio_data *data;
1118d097971dSTrond Myklebust 	size_t wsize = desc->pg_bsize, nbytes;
1119e9f7bee1STrond Myklebust 	unsigned int offset;
11201da177e4SLinus Torvalds 	int requests = 0;
1121ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
11221da177e4SLinus Torvalds 
1123ea2cf228SFred Isaman 	nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
11241da177e4SLinus Torvalds 
1125b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1126ea2cf228SFred Isaman 	    (desc->pg_moreio || nfs_reqs_to_commit(&cinfo) ||
1127b31268acSTrond Myklebust 	     desc->pg_count > wsize))
1128b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1129b31268acSTrond Myklebust 
1130b31268acSTrond Myklebust 
1131275acaafSTrond Myklebust 	offset = 0;
1132c76069bdSFred Isaman 	nbytes = desc->pg_count;
1133e9f7bee1STrond Myklebust 	do {
1134e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
1135e9f7bee1STrond Myklebust 
1136*00bfa30aSAnna Schumaker 		data = nfs_pgio_data_alloc(hdr, 1);
113725b11dcdSTrond Myklebust 		if (!data) {
113825b11dcdSTrond Myklebust 			nfs_flush_error(desc, hdr);
113925b11dcdSTrond Myklebust 			return -ENOMEM;
114025b11dcdSTrond Myklebust 		}
114130dd374fSFred Isaman 		data->pages.pagevec[0] = page;
1142ea2cf228SFred Isaman 		nfs_write_rpcsetup(data, len, offset, desc->pg_ioflags, &cinfo);
11436c75dc0dSFred Isaman 		list_add(&data->list, &hdr->rpc_list);
11441da177e4SLinus Torvalds 		requests++;
1145e9f7bee1STrond Myklebust 		nbytes -= len;
1146275acaafSTrond Myklebust 		offset += len;
1147e9f7bee1STrond Myklebust 	} while (nbytes != 0);
114825b11dcdSTrond Myklebust 	nfs_list_remove_request(req);
114925b11dcdSTrond Myklebust 	nfs_list_add_request(req, &hdr->pages);
11506c75dc0dSFred Isaman 	desc->pg_rpc_callops = &nfs_write_common_ops;
115125b11dcdSTrond Myklebust 	return 0;
11521da177e4SLinus Torvalds }
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds /*
11551da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
11561da177e4SLinus Torvalds  * The page must have been locked by the caller.
11571da177e4SLinus Torvalds  *
11581da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
11591da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
11601da177e4SLinus Torvalds  * that has been written but not committed.
11611da177e4SLinus Torvalds  */
11626c75dc0dSFred Isaman static int nfs_flush_one(struct nfs_pageio_descriptor *desc,
11636c75dc0dSFred Isaman 			 struct nfs_pgio_header *hdr)
11641da177e4SLinus Torvalds {
11651da177e4SLinus Torvalds 	struct nfs_page		*req;
11661da177e4SLinus Torvalds 	struct page		**pages;
11679c7e1b3dSAnna Schumaker 	struct nfs_pgio_data	*data;
1168c76069bdSFred Isaman 	struct list_head *head = &desc->pg_list;
1169ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
11701da177e4SLinus Torvalds 
1171*00bfa30aSAnna Schumaker 	data = nfs_pgio_data_alloc(hdr, nfs_page_array_len(desc->pg_base,
1172c76069bdSFred Isaman 							   desc->pg_count));
11736c75dc0dSFred Isaman 	if (!data) {
117425b11dcdSTrond Myklebust 		nfs_flush_error(desc, hdr);
117525b11dcdSTrond Myklebust 		return -ENOMEM;
117644b83799SFred Isaman 	}
11776c75dc0dSFred Isaman 
1178ea2cf228SFred Isaman 	nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
117930dd374fSFred Isaman 	pages = data->pages.pagevec;
11801da177e4SLinus Torvalds 	while (!list_empty(head)) {
11811da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
11821da177e4SLinus Torvalds 		nfs_list_remove_request(req);
11836c75dc0dSFred Isaman 		nfs_list_add_request(req, &hdr->pages);
11841da177e4SLinus Torvalds 		*pages++ = req->wb_page;
11851da177e4SLinus Torvalds 	}
11861da177e4SLinus Torvalds 
1187b31268acSTrond Myklebust 	if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
1188ea2cf228SFred Isaman 	    (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
1189b31268acSTrond Myklebust 		desc->pg_ioflags &= ~FLUSH_COND_STABLE;
1190b31268acSTrond Myklebust 
11911da177e4SLinus Torvalds 	/* Set up the argument struct */
1192ea2cf228SFred Isaman 	nfs_write_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo);
11936c75dc0dSFred Isaman 	list_add(&data->list, &hdr->rpc_list);
11946c75dc0dSFred Isaman 	desc->pg_rpc_callops = &nfs_write_common_ops;
119525b11dcdSTrond Myklebust 	return 0;
11961da177e4SLinus Torvalds }
11971da177e4SLinus Torvalds 
11986c75dc0dSFred Isaman int nfs_generic_flush(struct nfs_pageio_descriptor *desc,
11996c75dc0dSFred Isaman 		      struct nfs_pgio_header *hdr)
1200dce81290STrond Myklebust {
1201dce81290STrond Myklebust 	if (desc->pg_bsize < PAGE_CACHE_SIZE)
12026c75dc0dSFred Isaman 		return nfs_flush_multi(desc, hdr);
12036c75dc0dSFred Isaman 	return nfs_flush_one(desc, hdr);
1204dce81290STrond Myklebust }
120589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_generic_flush);
1206dce81290STrond Myklebust 
1207dce81290STrond Myklebust static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
12081751c363STrond Myklebust {
1209c0752cdfSAnna Schumaker 	struct nfs_rw_header *whdr;
12106c75dc0dSFred Isaman 	struct nfs_pgio_header *hdr;
1211275acaafSTrond Myklebust 	int ret;
1212275acaafSTrond Myklebust 
12136c75dc0dSFred Isaman 	whdr = nfs_writehdr_alloc();
12146c75dc0dSFred Isaman 	if (!whdr) {
12159b5415b5STrond Myklebust 		desc->pg_completion_ops->error_cleanup(&desc->pg_list);
12166c75dc0dSFred Isaman 		return -ENOMEM;
12176c75dc0dSFred Isaman 	}
12186c75dc0dSFred Isaman 	hdr = &whdr->header;
12196c75dc0dSFred Isaman 	nfs_pgheader_init(desc, hdr, nfs_writehdr_free);
12206c75dc0dSFred Isaman 	atomic_inc(&hdr->refcnt);
12216c75dc0dSFred Isaman 	ret = nfs_generic_flush(desc, hdr);
1222275acaafSTrond Myklebust 	if (ret == 0)
12236c75dc0dSFred Isaman 		ret = nfs_do_multiple_writes(&hdr->rpc_list,
12246c75dc0dSFred Isaman 					     desc->pg_rpc_callops,
1225dce81290STrond Myklebust 					     desc->pg_ioflags);
12266c75dc0dSFred Isaman 	if (atomic_dec_and_test(&hdr->refcnt))
1227061ae2edSFred Isaman 		hdr->completion_ops->completion(hdr);
1228275acaafSTrond Myklebust 	return ret;
12291751c363STrond Myklebust }
12301751c363STrond Myklebust 
12311751c363STrond Myklebust static const struct nfs_pageio_ops nfs_pageio_write_ops = {
12321751c363STrond Myklebust 	.pg_test = nfs_generic_pg_test,
12331751c363STrond Myklebust 	.pg_doio = nfs_generic_pg_writepages,
12341751c363STrond Myklebust };
12351751c363STrond Myklebust 
123657208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1237a20c93e3SChristoph Hellwig 			       struct inode *inode, int ioflags, bool force_mds,
1238061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
12391751c363STrond Myklebust {
1240a20c93e3SChristoph Hellwig 	struct nfs_server *server = NFS_SERVER(inode);
1241a20c93e3SChristoph Hellwig 	const struct nfs_pageio_ops *pg_ops = &nfs_pageio_write_ops;
1242a20c93e3SChristoph Hellwig 
1243a20c93e3SChristoph Hellwig #ifdef CONFIG_NFS_V4_1
1244a20c93e3SChristoph Hellwig 	if (server->pnfs_curr_ld && !force_mds)
1245a20c93e3SChristoph Hellwig 		pg_ops = server->pnfs_curr_ld->pg_write_ops;
1246a20c93e3SChristoph Hellwig #endif
1247a20c93e3SChristoph Hellwig 	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, server->wsize, ioflags);
12481751c363STrond Myklebust }
1249ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
12501751c363STrond Myklebust 
1251dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1252dce81290STrond Myklebust {
1253dce81290STrond Myklebust 	pgio->pg_ops = &nfs_pageio_write_ops;
1254dce81290STrond Myklebust 	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1255dce81290STrond Myklebust }
12561f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1257dce81290STrond Myklebust 
12581da177e4SLinus Torvalds 
1259def6ed7eSAndy Adamson void nfs_write_prepare(struct rpc_task *task, void *calldata)
1260def6ed7eSAndy Adamson {
12619c7e1b3dSAnna Schumaker 	struct nfs_pgio_data *data = calldata;
1262ef1820f9SNeilBrown 	int err;
1263ef1820f9SNeilBrown 	err = NFS_PROTO(data->header->inode)->write_rpc_prepare(task, data);
1264ef1820f9SNeilBrown 	if (err)
1265ef1820f9SNeilBrown 		rpc_exit(task, err);
1266def6ed7eSAndy Adamson }
1267def6ed7eSAndy Adamson 
12680b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
12690b7c0153SFred Isaman {
12700b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
12710b7c0153SFred Isaman 
12720b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
12730b7c0153SFred Isaman }
12740b7c0153SFred Isaman 
12751da177e4SLinus Torvalds /*
12761da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
12771da177e4SLinus Torvalds  *
12781da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
12791da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
12801da177e4SLinus Torvalds  *	  as the page has a write request pending.
12811da177e4SLinus Torvalds  */
12826c75dc0dSFred Isaman static void nfs_writeback_done_common(struct rpc_task *task, void *calldata)
12831da177e4SLinus Torvalds {
12849c7e1b3dSAnna Schumaker 	struct nfs_pgio_data	*data = calldata;
12851da177e4SLinus Torvalds 
1286c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
1287c9d8f89dSTrond Myklebust }
1288c9d8f89dSTrond Myklebust 
12896c75dc0dSFred Isaman static void nfs_writeback_release_common(void *calldata)
1290c9d8f89dSTrond Myklebust {
12919c7e1b3dSAnna Schumaker 	struct nfs_pgio_data	*data = calldata;
1292cd841605SFred Isaman 	struct nfs_pgio_header *hdr = data->header;
1293e2fecb21STrond Myklebust 	int status = data->task.tk_status;
1294788e7a89STrond Myklebust 
12956c75dc0dSFred Isaman 	if ((status >= 0) && nfs_write_need_commit(data)) {
12966c75dc0dSFred Isaman 		spin_lock(&hdr->lock);
12976c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags))
12986c75dc0dSFred Isaman 			; /* Do nothing */
12996c75dc0dSFred Isaman 		else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags))
1300f79d06f5SAnna Schumaker 			memcpy(&hdr->verf, &data->verf, sizeof(hdr->verf));
1301f79d06f5SAnna Schumaker 		else if (memcmp(&hdr->verf, &data->verf, sizeof(hdr->verf)))
13026c75dc0dSFred Isaman 			set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags);
13036c75dc0dSFred Isaman 		spin_unlock(&hdr->lock);
13041da177e4SLinus Torvalds 	}
1305*00bfa30aSAnna Schumaker 	nfs_pgio_data_release(data);
13061da177e4SLinus Torvalds }
13071da177e4SLinus Torvalds 
13086c75dc0dSFred Isaman static const struct rpc_call_ops nfs_write_common_ops = {
1309def6ed7eSAndy Adamson 	.rpc_call_prepare = nfs_write_prepare,
13106c75dc0dSFred Isaman 	.rpc_call_done = nfs_writeback_done_common,
13116c75dc0dSFred Isaman 	.rpc_release = nfs_writeback_release_common,
1312788e7a89STrond Myklebust };
1313788e7a89STrond Myklebust 
13141f2edbe3STrond Myklebust /*
13151f2edbe3STrond Myklebust  * Special version of should_remove_suid() that ignores capabilities.
13161f2edbe3STrond Myklebust  */
13171f2edbe3STrond Myklebust static int nfs_should_remove_suid(const struct inode *inode)
13181f2edbe3STrond Myklebust {
13191f2edbe3STrond Myklebust 	umode_t mode = inode->i_mode;
13201f2edbe3STrond Myklebust 	int kill = 0;
13211f2edbe3STrond Myklebust 
13221f2edbe3STrond Myklebust 	/* suid always must be killed */
13231f2edbe3STrond Myklebust 	if (unlikely(mode & S_ISUID))
13241f2edbe3STrond Myklebust 		kill = ATTR_KILL_SUID;
13251f2edbe3STrond Myklebust 
13261f2edbe3STrond Myklebust 	/*
13271f2edbe3STrond Myklebust 	 * sgid without any exec bits is just a mandatory locking mark; leave
13281f2edbe3STrond Myklebust 	 * it alone.  If some exec bits are set, it's a real sgid; kill it.
13291f2edbe3STrond Myklebust 	 */
13301f2edbe3STrond Myklebust 	if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
13311f2edbe3STrond Myklebust 		kill |= ATTR_KILL_SGID;
13321f2edbe3STrond Myklebust 
13331f2edbe3STrond Myklebust 	if (unlikely(kill && S_ISREG(mode)))
13341f2edbe3STrond Myklebust 		return kill;
13351f2edbe3STrond Myklebust 
13361f2edbe3STrond Myklebust 	return 0;
13371f2edbe3STrond Myklebust }
1338788e7a89STrond Myklebust 
13391da177e4SLinus Torvalds /*
13401da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
13411da177e4SLinus Torvalds  */
13429c7e1b3dSAnna Schumaker void nfs_writeback_done(struct rpc_task *task, struct nfs_pgio_data *data)
13431da177e4SLinus Torvalds {
13443c6b899cSAnna Schumaker 	struct nfs_pgio_args	*argp = &data->args;
13459137bdf3SAnna Schumaker 	struct nfs_pgio_res	*resp = &data->res;
1346cd841605SFred Isaman 	struct inode		*inode = data->header->inode;
1347788e7a89STrond Myklebust 	int status;
13481da177e4SLinus Torvalds 
1349a3f565b1SChuck Lever 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
13501da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
13511da177e4SLinus Torvalds 
1352f551e44fSChuck Lever 	/*
1353f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1354f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1355f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1356f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1357f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1358f551e44fSChuck Lever 	 */
1359cd841605SFred Isaman 	status = NFS_PROTO(inode)->write_done(task, data);
1360788e7a89STrond Myklebust 	if (status != 0)
136113602896SFred Isaman 		return;
1362cd841605SFred Isaman 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
136391d5b470SChuck Lever 
136489d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
13651da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
13661da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
13671da177e4SLinus Torvalds 		 * commit data to stable storage even though we
13681da177e4SLinus Torvalds 		 * requested it.
13691da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
13701da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
13711da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
13721da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
13731da177e4SLinus Torvalds 		 */
13741da177e4SLinus Torvalds 		static unsigned long    complain;
13751da177e4SLinus Torvalds 
1376a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
13771da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
13781da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
13791da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1380cd841605SFred Isaman 				NFS_SERVER(inode)->nfs_client->cl_hostname,
13811da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
13821da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
13831da177e4SLinus Torvalds 		}
13841da177e4SLinus Torvalds 	}
13851da177e4SLinus Torvalds #endif
13861f2edbe3STrond Myklebust 	if (task->tk_status < 0) {
13876c75dc0dSFred Isaman 		nfs_set_pgio_error(data->header, task->tk_status, argp->offset);
13881f2edbe3STrond Myklebust 		return;
13891f2edbe3STrond Myklebust 	}
13901f2edbe3STrond Myklebust 
13911f2edbe3STrond Myklebust 	/* Deal with the suid/sgid bit corner case */
13921f2edbe3STrond Myklebust 	if (nfs_should_remove_suid(inode))
13931f2edbe3STrond Myklebust 		nfs_mark_for_revalidate(inode);
13941f2edbe3STrond Myklebust 
13951f2edbe3STrond Myklebust 	if (resp->count < argp->count) {
13961da177e4SLinus Torvalds 		static unsigned long    complain;
13971da177e4SLinus Torvalds 
13986c75dc0dSFred Isaman 		/* This a short write! */
1399cd841605SFred Isaman 		nfs_inc_stats(inode, NFSIOS_SHORTWRITE);
140091d5b470SChuck Lever 
14011da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
14026c75dc0dSFred Isaman 		if (resp->count == 0) {
14036c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
14046c75dc0dSFred Isaman 				printk(KERN_WARNING
14056c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
14066c75dc0dSFred Isaman 				       argp->count);
14076c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
14086c75dc0dSFred Isaman 			}
14096c75dc0dSFred Isaman 			nfs_set_pgio_error(data->header, -EIO, argp->offset);
14106c75dc0dSFred Isaman 			task->tk_status = -EIO;
14116c75dc0dSFred Isaman 			return;
14126c75dc0dSFred Isaman 		}
14131da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
14141da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
14151da177e4SLinus Torvalds 			/* Resend from where the server left off */
1416a69aef14SFred Isaman 			data->mds_offset += resp->count;
14171da177e4SLinus Torvalds 			argp->offset += resp->count;
14181da177e4SLinus Torvalds 			argp->pgbase += resp->count;
14191da177e4SLinus Torvalds 			argp->count -= resp->count;
14201da177e4SLinus Torvalds 		} else {
14211da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
14221da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
14231da177e4SLinus Torvalds 			 */
14241da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
14251da177e4SLinus Torvalds 		}
1426d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
14271da177e4SLinus Torvalds 	}
14281da177e4SLinus Torvalds }
14291da177e4SLinus Torvalds 
14301da177e4SLinus Torvalds 
143189d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
143271d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
143371d0a611STrond Myklebust {
1434b8413f98STrond Myklebust 	int ret;
1435b8413f98STrond Myklebust 
143671d0a611STrond Myklebust 	if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
143771d0a611STrond Myklebust 		return 1;
1438b8413f98STrond Myklebust 	if (!may_wait)
143971d0a611STrond Myklebust 		return 0;
1440b8413f98STrond Myklebust 	ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1441b8413f98STrond Myklebust 				NFS_INO_COMMIT,
1442b8413f98STrond Myklebust 				nfs_wait_bit_killable,
1443b8413f98STrond Myklebust 				TASK_KILLABLE);
1444b8413f98STrond Myklebust 	return (ret < 0) ? ret : 1;
144571d0a611STrond Myklebust }
144671d0a611STrond Myklebust 
1447f453a54aSFred Isaman static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
144871d0a611STrond Myklebust {
144971d0a611STrond Myklebust 	clear_bit(NFS_INO_COMMIT, &nfsi->flags);
145071d0a611STrond Myklebust 	smp_mb__after_clear_bit();
145171d0a611STrond Myklebust 	wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
145271d0a611STrond Myklebust }
145371d0a611STrond Myklebust 
14540b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
14551da177e4SLinus Torvalds {
14560b7c0153SFred Isaman 	put_nfs_open_context(data->context);
14570b7c0153SFred Isaman 	nfs_commit_free(data);
14581da177e4SLinus Torvalds }
1459e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
14601da177e4SLinus Torvalds 
14610b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
14629ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
14639f0ec176SAndy Adamson 			int how, int flags)
14641da177e4SLinus Torvalds {
146507737691STrond Myklebust 	struct rpc_task *task;
14669ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1467bdc7f021STrond Myklebust 	struct rpc_message msg = {
1468bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1469bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
14709ace33cdSFred Isaman 		.rpc_cred = data->cred,
1471bdc7f021STrond Myklebust 	};
147284115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
147307737691STrond Myklebust 		.task = &data->task,
14749ace33cdSFred Isaman 		.rpc_client = clnt,
1475bdc7f021STrond Myklebust 		.rpc_message = &msg,
14769ace33cdSFred Isaman 		.callback_ops = call_ops,
147784115e1cSTrond Myklebust 		.callback_data = data,
1478101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
14799f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
14803ff7576dSTrond Myklebust 		.priority = priority,
148184115e1cSTrond Myklebust 	};
1482788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
14839ace33cdSFred Isaman 	NFS_PROTO(data->inode)->commit_setup(data, &msg);
14841da177e4SLinus Torvalds 
1485a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1486bdc7f021STrond Myklebust 
14878c21c62cSWeston Andros Adamson 	nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client,
14888c21c62cSWeston Andros Adamson 		NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg);
14898c21c62cSWeston Andros Adamson 
149007737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1491dbae4c73STrond Myklebust 	if (IS_ERR(task))
1492dbae4c73STrond Myklebust 		return PTR_ERR(task);
1493d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1494d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
149507737691STrond Myklebust 	rpc_put_task(task);
1496dbae4c73STrond Myklebust 	return 0;
14971da177e4SLinus Torvalds }
1498e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
14991da177e4SLinus Torvalds 
15001da177e4SLinus Torvalds /*
15019ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
15029ace33cdSFred Isaman  */
15030b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1504988b6dceSFred Isaman 		     struct list_head *head,
1505f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1506f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
15079ace33cdSFred Isaman {
15089ace33cdSFred Isaman 	struct nfs_page *first = nfs_list_entry(head->next);
15093d4ff43dSAl Viro 	struct inode *inode = first->wb_context->dentry->d_inode;
15109ace33cdSFred Isaman 
15119ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
15129ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
15139ace33cdSFred Isaman 
15149ace33cdSFred Isaman 	list_splice_init(head, &data->pages);
15159ace33cdSFred Isaman 
15169ace33cdSFred Isaman 	data->inode	  = inode;
15179ace33cdSFred Isaman 	data->cred	  = first->wb_context->cred;
1518988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
15199ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1520f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1521b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
15229ace33cdSFred Isaman 
15239ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
15249ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
15259ace33cdSFred Isaman 	data->args.offset = 0;
15269ace33cdSFred Isaman 	data->args.count  = 0;
15270b7c0153SFred Isaman 	data->context     = get_nfs_open_context(first->wb_context);
15289ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
15299ace33cdSFred Isaman 	data->res.verf    = &data->verf;
15309ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
15319ace33cdSFred Isaman }
1532e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
15339ace33cdSFred Isaman 
1534e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1535ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1536ea2cf228SFred Isaman 		      struct nfs_commit_info *cinfo)
153764bfeb49SFred Isaman {
153864bfeb49SFred Isaman 	struct nfs_page *req;
153964bfeb49SFred Isaman 
154064bfeb49SFred Isaman 	while (!list_empty(page_list)) {
154164bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
154264bfeb49SFred Isaman 		nfs_list_remove_request(req);
1543ea2cf228SFred Isaman 		nfs_mark_request_commit(req, lseg, cinfo);
154456f9cd68SFred Isaman 		if (!cinfo->dreq) {
154564bfeb49SFred Isaman 			dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1546d56b4ddfSMel Gorman 			dec_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
154764bfeb49SFred Isaman 				     BDI_RECLAIMABLE);
154856f9cd68SFred Isaman 		}
15491d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
155064bfeb49SFred Isaman 	}
155164bfeb49SFred Isaman }
1552e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
155364bfeb49SFred Isaman 
15549ace33cdSFred Isaman /*
15551da177e4SLinus Torvalds  * Commit dirty pages
15561da177e4SLinus Torvalds  */
15571da177e4SLinus Torvalds static int
1558ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1559ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
15601da177e4SLinus Torvalds {
15610b7c0153SFred Isaman 	struct nfs_commit_data	*data;
15621da177e4SLinus Torvalds 
1563c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds 	if (!data)
15661da177e4SLinus Torvalds 		goto out_bad;
15671da177e4SLinus Torvalds 
15681da177e4SLinus Torvalds 	/* Set up the argument struct */
1569f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
1570f453a54aSFred Isaman 	atomic_inc(&cinfo->mds->rpcs_out);
15719f0ec176SAndy Adamson 	return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops,
15729f0ec176SAndy Adamson 				   how, 0);
15731da177e4SLinus Torvalds  out_bad:
1574ea2cf228SFred Isaman 	nfs_retry_commit(head, NULL, cinfo);
1575f453a54aSFred Isaman 	cinfo->completion_ops->error_cleanup(NFS_I(inode));
15761da177e4SLinus Torvalds 	return -ENOMEM;
15771da177e4SLinus Torvalds }
15781da177e4SLinus Torvalds 
15791da177e4SLinus Torvalds /*
15801da177e4SLinus Torvalds  * COMMIT call returned
15811da177e4SLinus Torvalds  */
1582788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
15831da177e4SLinus Torvalds {
15840b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
15851da177e4SLinus Torvalds 
1586a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
15871da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
15881da177e4SLinus Torvalds 
1589788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1590c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
1591c9d8f89dSTrond Myklebust }
1592c9d8f89dSTrond Myklebust 
1593f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1594c9d8f89dSTrond Myklebust {
1595c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1596c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1597f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
1598788e7a89STrond Myklebust 
15991da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
16001da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
16011da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1602d6d6dc7cSFred Isaman 		nfs_clear_page_commit(req->wb_page);
16031da177e4SLinus Torvalds 
16041e8968c5SNiels de Vos 		dprintk("NFS:       commit (%s/%llu %d@%lld)",
16053d4ff43dSAl Viro 			req->wb_context->dentry->d_sb->s_id,
16061e8968c5SNiels de Vos 			(unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
16071da177e4SLinus Torvalds 			req->wb_bytes,
16081da177e4SLinus Torvalds 			(long long)req_offset(req));
1609c9d8f89dSTrond Myklebust 		if (status < 0) {
1610c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
16111da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1612c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
16131da177e4SLinus Torvalds 			goto next;
16141da177e4SLinus Torvalds 		}
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
16171da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
16182f2c63bcSTrond Myklebust 		if (!memcmp(&req->wb_verf, &data->verf.verifier, sizeof(req->wb_verf))) {
16191da177e4SLinus Torvalds 			/* We have a match */
16201da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
16211da177e4SLinus Torvalds 			dprintk(" OK\n");
16221da177e4SLinus Torvalds 			goto next;
16231da177e4SLinus Torvalds 		}
16241da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
16251da177e4SLinus Torvalds 		dprintk(" mismatch\n");
16266d884e8fSFred 		nfs_mark_request_dirty(req);
162705990d1bSTrond Myklebust 		set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
16281da177e4SLinus Torvalds 	next:
16291d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
16301da177e4SLinus Torvalds 	}
1631f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1632f453a54aSFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
1633f453a54aSFred Isaman 		nfs_commit_clear_lock(NFS_I(data->inode));
16345917ce84SFred Isaman }
16355917ce84SFred Isaman 
16365917ce84SFred Isaman static void nfs_commit_release(void *calldata)
16375917ce84SFred Isaman {
16380b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
16395917ce84SFred Isaman 
1640f453a54aSFred Isaman 	data->completion_ops->completion(data);
1641c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
16421da177e4SLinus Torvalds }
1643788e7a89STrond Myklebust 
1644788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
16450b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1646788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1647788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1648788e7a89STrond Myklebust };
16491da177e4SLinus Torvalds 
1650f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1651f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1652f453a54aSFred Isaman 	.error_cleanup = nfs_commit_clear_lock,
1653f453a54aSFred Isaman };
1654f453a54aSFred Isaman 
16551763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1656ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
165784c53ab5SFred Isaman {
165884c53ab5SFred Isaman 	int status;
165984c53ab5SFred Isaman 
1660ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
166184c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1662ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
166384c53ab5SFred Isaman 	return status;
166484c53ab5SFred Isaman }
166584c53ab5SFred Isaman 
1666b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
16671da177e4SLinus Torvalds {
16681da177e4SLinus Torvalds 	LIST_HEAD(head);
1669ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
167071d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1671b8413f98STrond Myklebust 	int res;
16721da177e4SLinus Torvalds 
1673b8413f98STrond Myklebust 	res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1674b8413f98STrond Myklebust 	if (res <= 0)
1675c5efa5fcSTrond Myklebust 		goto out_mark_dirty;
1676ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1677ea2cf228SFred Isaman 	res = nfs_scan_commit(inode, &head, &cinfo);
16781da177e4SLinus Torvalds 	if (res) {
1679a861a1e1SFred Isaman 		int error;
1680a861a1e1SFred Isaman 
1681ea2cf228SFred Isaman 		error = nfs_generic_commit_list(inode, &head, how, &cinfo);
16821da177e4SLinus Torvalds 		if (error < 0)
16831da177e4SLinus Torvalds 			return error;
1684b8413f98STrond Myklebust 		if (!may_wait)
1685b8413f98STrond Myklebust 			goto out_mark_dirty;
1686b8413f98STrond Myklebust 		error = wait_on_bit(&NFS_I(inode)->flags,
1687b8413f98STrond Myklebust 				NFS_INO_COMMIT,
168871d0a611STrond Myklebust 				nfs_wait_bit_killable,
168971d0a611STrond Myklebust 				TASK_KILLABLE);
1690b8413f98STrond Myklebust 		if (error < 0)
1691b8413f98STrond Myklebust 			return error;
169271d0a611STrond Myklebust 	} else
169371d0a611STrond Myklebust 		nfs_commit_clear_lock(NFS_I(inode));
1694c5efa5fcSTrond Myklebust 	return res;
1695c5efa5fcSTrond Myklebust 	/* Note: If we exit without ensuring that the commit is complete,
1696c5efa5fcSTrond Myklebust 	 * we must mark the inode as dirty. Otherwise, future calls to
1697c5efa5fcSTrond Myklebust 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1698c5efa5fcSTrond Myklebust 	 * that the data is on the disk.
1699c5efa5fcSTrond Myklebust 	 */
1700c5efa5fcSTrond Myklebust out_mark_dirty:
1701c5efa5fcSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
17021da177e4SLinus Torvalds 	return res;
17031da177e4SLinus Torvalds }
17048fc795f7STrond Myklebust 
17058fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
17068fc795f7STrond Myklebust {
1707420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1708420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1709420e3646STrond Myklebust 	int ret = 0;
17108fc795f7STrond Myklebust 
17113236c3e1SJeff Layton 	/* no commits means nothing needs to be done */
1712ea2cf228SFred Isaman 	if (!nfsi->commit_info.ncommit)
17133236c3e1SJeff Layton 		return ret;
17143236c3e1SJeff Layton 
1715a00dd6c0SJeff Layton 	if (wbc->sync_mode == WB_SYNC_NONE) {
1716a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1717a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1718420e3646STrond Myklebust 		 */
1719ea2cf228SFred Isaman 		if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1))
1720420e3646STrond Myklebust 			goto out_mark_dirty;
1721420e3646STrond Myklebust 
1722a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1723420e3646STrond Myklebust 		flags = 0;
1724a00dd6c0SJeff Layton 	}
1725a00dd6c0SJeff Layton 
1726420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1727420e3646STrond Myklebust 	if (ret >= 0) {
1728420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1729420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1730420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1731420e3646STrond Myklebust 			else
1732420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1733420e3646STrond Myklebust 		}
17348fc795f7STrond Myklebust 		return 0;
1735420e3646STrond Myklebust 	}
1736420e3646STrond Myklebust out_mark_dirty:
17378fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
17388fc795f7STrond Myklebust 	return ret;
17398fc795f7STrond Myklebust }
1740c63c7b05STrond Myklebust #else
17418fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
17428fc795f7STrond Myklebust {
17438fc795f7STrond Myklebust 	return 0;
17448fc795f7STrond Myklebust }
17451da177e4SLinus Torvalds #endif
17461da177e4SLinus Torvalds 
17478fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
17488fc795f7STrond Myklebust {
1749a8d8f02cSBryan Schumaker 	return nfs_commit_unstable_pages(inode, wbc);
1750a8d8f02cSBryan Schumaker }
175189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode);
1752863a3c6cSAndy Adamson 
1753acdc53b2STrond Myklebust /*
1754acdc53b2STrond Myklebust  * flush the inode to disk.
1755acdc53b2STrond Myklebust  */
1756acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
175734901f70STrond Myklebust {
175834901f70STrond Myklebust 	struct writeback_control wbc = {
175972cb77f4STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
176034901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
1761d7fb1207STrond Myklebust 		.range_start = 0,
1762d7fb1207STrond Myklebust 		.range_end = LLONG_MAX,
176334901f70STrond Myklebust 	};
1764f4ce1299STrond Myklebust 	int ret;
176534901f70STrond Myklebust 
1766f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_enter(inode);
1767f4ce1299STrond Myklebust 
1768f4ce1299STrond Myklebust 	ret = sync_inode(inode, &wbc);
1769f4ce1299STrond Myklebust 
1770f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_exit(inode, ret);
1771f4ce1299STrond Myklebust 	return ret;
17721c75950bSTrond Myklebust }
1773ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all);
17741c75950bSTrond Myklebust 
17751b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
17761b3b4a1aSTrond Myklebust {
17771b3b4a1aSTrond Myklebust 	struct nfs_page *req;
17781b3b4a1aSTrond Myklebust 	int ret = 0;
17791b3b4a1aSTrond Myklebust 
17801b3b4a1aSTrond Myklebust 	for (;;) {
1781ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
17821b3b4a1aSTrond Myklebust 		req = nfs_page_find_request(page);
17831b3b4a1aSTrond Myklebust 		if (req == NULL)
17841b3b4a1aSTrond Myklebust 			break;
17857ad84aa9STrond Myklebust 		if (nfs_lock_request(req)) {
17868dd37758STrond Myklebust 			nfs_clear_request_commit(req);
17871b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
17881b3b4a1aSTrond Myklebust 			/*
17891b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
17901b3b4a1aSTrond Myklebust 			 * page as being dirty
17911b3b4a1aSTrond Myklebust 			 */
17921b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
17931d1afcbcSTrond Myklebust 			nfs_unlock_and_release_request(req);
17941b3b4a1aSTrond Myklebust 			break;
17951b3b4a1aSTrond Myklebust 		}
17961b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
1797c9edda71STrond Myklebust 		nfs_release_request(req);
17981b3b4a1aSTrond Myklebust 		if (ret < 0)
1799c988950eSTrond Myklebust 			break;
18001b3b4a1aSTrond Myklebust 	}
18011b3b4a1aSTrond Myklebust 	return ret;
18021b3b4a1aSTrond Myklebust }
18031b3b4a1aSTrond Myklebust 
18041c75950bSTrond Myklebust /*
18051c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
18061c75950bSTrond Myklebust  */
18071c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
18081c75950bSTrond Myklebust {
180929418aa4SMel Gorman 	loff_t range_start = page_file_offset(page);
18107f2f12d9STrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
18117f2f12d9STrond Myklebust 	struct writeback_control wbc = {
18127f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
18137f2f12d9STrond Myklebust 		.nr_to_write = 0,
18147f2f12d9STrond Myklebust 		.range_start = range_start,
18157f2f12d9STrond Myklebust 		.range_end = range_end,
18167f2f12d9STrond Myklebust 	};
18177f2f12d9STrond Myklebust 	int ret;
18187f2f12d9STrond Myklebust 
1819f4ce1299STrond Myklebust 	trace_nfs_writeback_page_enter(inode);
1820f4ce1299STrond Myklebust 
18210522f6adSTrond Myklebust 	for (;;) {
1822ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
18237f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
18247f2f12d9STrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
18257f2f12d9STrond Myklebust 			if (ret < 0)
18267f2f12d9STrond Myklebust 				goto out_error;
18270522f6adSTrond Myklebust 			continue;
18287f2f12d9STrond Myklebust 		}
1829f4ce1299STrond Myklebust 		ret = 0;
18300522f6adSTrond Myklebust 		if (!PagePrivate(page))
18310522f6adSTrond Myklebust 			break;
18320522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
18337f2f12d9STrond Myklebust 		if (ret < 0)
18347f2f12d9STrond Myklebust 			goto out_error;
18357f2f12d9STrond Myklebust 	}
18367f2f12d9STrond Myklebust out_error:
1837f4ce1299STrond Myklebust 	trace_nfs_writeback_page_exit(inode, ret);
18387f2f12d9STrond Myklebust 	return ret;
18391c75950bSTrond Myklebust }
18401c75950bSTrond Myklebust 
1841074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
1842074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1843a6bc32b8SMel Gorman 		struct page *page, enum migrate_mode mode)
1844074cc1deSTrond Myklebust {
18452da95652SJeff Layton 	/*
18462da95652SJeff Layton 	 * If PagePrivate is set, then the page is currently associated with
18472da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
18482da95652SJeff Layton 	 *
18492da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
18502da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
18512da95652SJeff Layton 	 *        the page lock.
18522da95652SJeff Layton 	 */
18532da95652SJeff Layton 	if (PagePrivate(page))
18542da95652SJeff Layton 		return -EBUSY;
1855074cc1deSTrond Myklebust 
18568c209ce7SDavid Howells 	if (!nfs_fscache_release_page(page, GFP_KERNEL))
18578c209ce7SDavid Howells 		return -EBUSY;
1858074cc1deSTrond Myklebust 
1859a6bc32b8SMel Gorman 	return migrate_page(mapping, newpage, page, mode);
1860074cc1deSTrond Myklebust }
1861074cc1deSTrond Myklebust #endif
1862074cc1deSTrond Myklebust 
1863f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
18641da177e4SLinus Torvalds {
18651da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1866c0752cdfSAnna Schumaker 					     sizeof(struct nfs_rw_header),
18671da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
186820c2df83SPaul Mundt 					     NULL);
18691da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
18701da177e4SLinus Torvalds 		return -ENOMEM;
18711da177e4SLinus Torvalds 
187293d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
18731da177e4SLinus Torvalds 						     nfs_wdata_cachep);
18741da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
18753dd4765fSJeff Layton 		goto out_destroy_write_cache;
18761da177e4SLinus Torvalds 
18770b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
18780b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
18790b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
18800b7c0153SFred Isaman 					     NULL);
18810b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
18823dd4765fSJeff Layton 		goto out_destroy_write_mempool;
18830b7c0153SFred Isaman 
188493d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
18854c100210SYanchuan Nian 						      nfs_cdata_cachep);
18861da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
18873dd4765fSJeff Layton 		goto out_destroy_commit_cache;
18881da177e4SLinus Torvalds 
188989a09141SPeter Zijlstra 	/*
189089a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
189189a09141SPeter Zijlstra 	 *
189289a09141SPeter Zijlstra 	 *  64MB:    8192k
189389a09141SPeter Zijlstra 	 * 128MB:   11585k
189489a09141SPeter Zijlstra 	 * 256MB:   16384k
189589a09141SPeter Zijlstra 	 * 512MB:   23170k
189689a09141SPeter Zijlstra 	 *   1GB:   32768k
189789a09141SPeter Zijlstra 	 *   2GB:   46340k
189889a09141SPeter Zijlstra 	 *   4GB:   65536k
189989a09141SPeter Zijlstra 	 *   8GB:   92681k
190089a09141SPeter Zijlstra 	 *  16GB:  131072k
190189a09141SPeter Zijlstra 	 *
190289a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
190389a09141SPeter Zijlstra 	 * Limit the default to 256M
190489a09141SPeter Zijlstra 	 */
190589a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
190689a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
190789a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
190889a09141SPeter Zijlstra 
19091da177e4SLinus Torvalds 	return 0;
19103dd4765fSJeff Layton 
19113dd4765fSJeff Layton out_destroy_commit_cache:
19123dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
19133dd4765fSJeff Layton out_destroy_write_mempool:
19143dd4765fSJeff Layton 	mempool_destroy(nfs_wdata_mempool);
19153dd4765fSJeff Layton out_destroy_write_cache:
19163dd4765fSJeff Layton 	kmem_cache_destroy(nfs_wdata_cachep);
19173dd4765fSJeff Layton 	return -ENOMEM;
19181da177e4SLinus Torvalds }
19191da177e4SLinus Torvalds 
1920266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
19211da177e4SLinus Torvalds {
19221da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
19233dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
19241da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
19251a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
19261da177e4SLinus Torvalds }
19271da177e4SLinus Torvalds 
1928