xref: /linux/fs/nfs/write.c (revision 7c3af975257383ece54b83c0505d3e0656cb7daf)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/fs/nfs/write.c
31da177e4SLinus Torvalds  *
47c85d900STrond Myklebust  * Write file data over NFS.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/types.h>
101da177e4SLinus Torvalds #include <linux/slab.h>
111da177e4SLinus Torvalds #include <linux/mm.h>
121da177e4SLinus Torvalds #include <linux/pagemap.h>
131da177e4SLinus Torvalds #include <linux/file.h>
141da177e4SLinus Torvalds #include <linux/writeback.h>
1589a09141SPeter Zijlstra #include <linux/swap.h>
16074cc1deSTrond Myklebust #include <linux/migrate.h>
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
191da177e4SLinus Torvalds #include <linux/nfs_fs.h>
201da177e4SLinus Torvalds #include <linux/nfs_mount.h>
211da177e4SLinus Torvalds #include <linux/nfs_page.h>
223fcfab16SAndrew Morton #include <linux/backing-dev.h>
23afeacc8cSPaul Gortmaker #include <linux/export.h>
243fcfab16SAndrew Morton 
251da177e4SLinus Torvalds #include <asm/uaccess.h>
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #include "delegation.h"
2849a70f27STrond Myklebust #include "internal.h"
2991d5b470SChuck Lever #include "iostat.h"
30def6ed7eSAndy Adamson #include "nfs4_fs.h"
31074cc1deSTrond Myklebust #include "fscache.h"
3294ad1c80SFred Isaman #include "pnfs.h"
331da177e4SLinus Torvalds 
34f4ce1299STrond Myklebust #include "nfstrace.h"
35f4ce1299STrond Myklebust 
361da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #define MIN_POOL_WRITE		(32)
391da177e4SLinus Torvalds #define MIN_POOL_COMMIT		(4)
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds /*
421da177e4SLinus Torvalds  * Local function declarations
431da177e4SLinus Torvalds  */
44f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req);
45788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops;
46061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops;
47f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops;
484a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops;
49d4581383SWeston Andros Adamson static void nfs_clear_request_commit(struct nfs_page *req);
5002d1426cSWeston Andros Adamson static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
5102d1426cSWeston Andros Adamson 				      struct inode *inode);
521da177e4SLinus Torvalds 
53e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep;
543feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
550b7c0153SFred Isaman static struct kmem_cache *nfs_cdata_cachep;
561da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
571da177e4SLinus Torvalds 
580b7c0153SFred Isaman struct nfs_commit_data *nfs_commitdata_alloc(void)
591da177e4SLinus Torvalds {
60192e501bSMel Gorman 	struct nfs_commit_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOIO);
6140859d7eSChuck Lever 
621da177e4SLinus Torvalds 	if (p) {
631da177e4SLinus Torvalds 		memset(p, 0, sizeof(*p));
641da177e4SLinus Torvalds 		INIT_LIST_HEAD(&p->pages);
651da177e4SLinus Torvalds 	}
661da177e4SLinus Torvalds 	return p;
671da177e4SLinus Torvalds }
68e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
691da177e4SLinus Torvalds 
700b7c0153SFred Isaman void nfs_commit_free(struct nfs_commit_data *p)
711da177e4SLinus Torvalds {
721da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
731da177e4SLinus Torvalds }
74e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free);
751da177e4SLinus Torvalds 
761e7f3a48SWeston Andros Adamson static struct nfs_pgio_header *nfs_writehdr_alloc(void)
773feb2d49STrond Myklebust {
781e7f3a48SWeston Andros Adamson 	struct nfs_pgio_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOIO);
793feb2d49STrond Myklebust 
804a0de55cSAnna Schumaker 	if (p)
813feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
823feb2d49STrond Myklebust 	return p;
833feb2d49STrond Myklebust }
843feb2d49STrond Myklebust 
851e7f3a48SWeston Andros Adamson static void nfs_writehdr_free(struct nfs_pgio_header *hdr)
866c75dc0dSFred Isaman {
871e7f3a48SWeston Andros Adamson 	mempool_free(hdr, nfs_wdata_mempool);
883feb2d49STrond Myklebust }
891da177e4SLinus Torvalds 
907b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
917b159fc1STrond Myklebust {
927b159fc1STrond Myklebust 	ctx->error = error;
937b159fc1STrond Myklebust 	smp_wmb();
947b159fc1STrond Myklebust 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
957b159fc1STrond Myklebust }
967b159fc1STrond Myklebust 
9784d3a9a9SWeston Andros Adamson /*
9802d1426cSWeston Andros Adamson  * nfs_page_search_commits_for_head_request_locked
9902d1426cSWeston Andros Adamson  *
10002d1426cSWeston Andros Adamson  * Search through commit lists on @inode for the head request for @page.
10102d1426cSWeston Andros Adamson  * Must be called while holding the inode (which is cinfo) lock.
10202d1426cSWeston Andros Adamson  *
10302d1426cSWeston Andros Adamson  * Returns the head request if found, or NULL if not found.
10402d1426cSWeston Andros Adamson  */
10502d1426cSWeston Andros Adamson static struct nfs_page *
10602d1426cSWeston Andros Adamson nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
10702d1426cSWeston Andros Adamson 						struct page *page)
10802d1426cSWeston Andros Adamson {
10902d1426cSWeston Andros Adamson 	struct nfs_page *freq, *t;
11002d1426cSWeston Andros Adamson 	struct nfs_commit_info cinfo;
11102d1426cSWeston Andros Adamson 	struct inode *inode = &nfsi->vfs_inode;
11202d1426cSWeston Andros Adamson 
11302d1426cSWeston Andros Adamson 	nfs_init_cinfo_from_inode(&cinfo, inode);
11402d1426cSWeston Andros Adamson 
11502d1426cSWeston Andros Adamson 	/* search through pnfs commit lists */
11602d1426cSWeston Andros Adamson 	freq = pnfs_search_commit_reqs(inode, &cinfo, page);
11702d1426cSWeston Andros Adamson 	if (freq)
11802d1426cSWeston Andros Adamson 		return freq->wb_head;
11902d1426cSWeston Andros Adamson 
12002d1426cSWeston Andros Adamson 	/* Linearly search the commit list for the correct request */
12102d1426cSWeston Andros Adamson 	list_for_each_entry_safe(freq, t, &cinfo.mds->list, wb_list) {
12202d1426cSWeston Andros Adamson 		if (freq->wb_page == page)
12302d1426cSWeston Andros Adamson 			return freq->wb_head;
12402d1426cSWeston Andros Adamson 	}
12502d1426cSWeston Andros Adamson 
12602d1426cSWeston Andros Adamson 	return NULL;
12702d1426cSWeston Andros Adamson }
12802d1426cSWeston Andros Adamson 
12902d1426cSWeston Andros Adamson /*
13084d3a9a9SWeston Andros Adamson  * nfs_page_find_head_request_locked - find head request associated with @page
13184d3a9a9SWeston Andros Adamson  *
13284d3a9a9SWeston Andros Adamson  * must be called while holding the inode lock.
13384d3a9a9SWeston Andros Adamson  *
13484d3a9a9SWeston Andros Adamson  * returns matching head request with reference held, or NULL if not found.
13584d3a9a9SWeston Andros Adamson  */
13629418aa4SMel Gorman static struct nfs_page *
13784d3a9a9SWeston Andros Adamson nfs_page_find_head_request_locked(struct nfs_inode *nfsi, struct page *page)
138277459d2STrond Myklebust {
139277459d2STrond Myklebust 	struct nfs_page *req = NULL;
140277459d2STrond Myklebust 
14129418aa4SMel Gorman 	if (PagePrivate(page))
142277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
14302d1426cSWeston Andros Adamson 	else if (unlikely(PageSwapCache(page)))
14402d1426cSWeston Andros Adamson 		req = nfs_page_search_commits_for_head_request_locked(nfsi,
14502d1426cSWeston Andros Adamson 			page);
14629418aa4SMel Gorman 
14784d3a9a9SWeston Andros Adamson 	if (req) {
14884d3a9a9SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_head != req);
14929418aa4SMel Gorman 		kref_get(&req->wb_kref);
15084d3a9a9SWeston Andros Adamson 	}
15129418aa4SMel Gorman 
152277459d2STrond Myklebust 	return req;
153277459d2STrond Myklebust }
154277459d2STrond Myklebust 
15584d3a9a9SWeston Andros Adamson /*
15684d3a9a9SWeston Andros Adamson  * nfs_page_find_head_request - find head request associated with @page
15784d3a9a9SWeston Andros Adamson  *
15884d3a9a9SWeston Andros Adamson  * returns matching head request with reference held, or NULL if not found.
15984d3a9a9SWeston Andros Adamson  */
16084d3a9a9SWeston Andros Adamson static struct nfs_page *nfs_page_find_head_request(struct page *page)
161277459d2STrond Myklebust {
162d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
163277459d2STrond Myklebust 	struct nfs_page *req = NULL;
164277459d2STrond Myklebust 
165587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
16684d3a9a9SWeston Andros Adamson 	req = nfs_page_find_head_request_locked(NFS_I(inode), page);
167587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
168277459d2STrond Myklebust 	return req;
169277459d2STrond Myklebust }
170277459d2STrond Myklebust 
1711da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1721da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1731da177e4SLinus Torvalds {
174d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
175a3d01454STrond Myklebust 	loff_t end, i_size;
176a3d01454STrond Myklebust 	pgoff_t end_index;
1771da177e4SLinus Torvalds 
178a3d01454STrond Myklebust 	spin_lock(&inode->i_lock);
179a3d01454STrond Myklebust 	i_size = i_size_read(inode);
180a3d01454STrond Myklebust 	end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
181d56b4ddfSMel Gorman 	if (i_size > 0 && page_file_index(page) < end_index)
182a3d01454STrond Myklebust 		goto out;
183d56b4ddfSMel Gorman 	end = page_file_offset(page) + ((loff_t)offset+count);
1841da177e4SLinus Torvalds 	if (i_size >= end)
185a3d01454STrond Myklebust 		goto out;
1861da177e4SLinus Torvalds 	i_size_write(inode, end);
187a3d01454STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
188a3d01454STrond Myklebust out:
189a3d01454STrond Myklebust 	spin_unlock(&inode->i_lock);
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
192a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
193a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
194a301b777STrond Myklebust {
195d56b4ddfSMel Gorman 	nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page));
196a301b777STrond Myklebust }
197a301b777STrond Myklebust 
198d72ddcbaSWeston Andros Adamson /*
199d72ddcbaSWeston Andros Adamson  * nfs_page_group_search_locked
200d72ddcbaSWeston Andros Adamson  * @head - head request of page group
201d72ddcbaSWeston Andros Adamson  * @page_offset - offset into page
202d72ddcbaSWeston Andros Adamson  *
203d72ddcbaSWeston Andros Adamson  * Search page group with head @head to find a request that contains the
204d72ddcbaSWeston Andros Adamson  * page offset @page_offset.
205d72ddcbaSWeston Andros Adamson  *
206d72ddcbaSWeston Andros Adamson  * Returns a pointer to the first matching nfs request, or NULL if no
207d72ddcbaSWeston Andros Adamson  * match is found.
208d72ddcbaSWeston Andros Adamson  *
209d72ddcbaSWeston Andros Adamson  * Must be called with the page group lock held
210d72ddcbaSWeston Andros Adamson  */
211d72ddcbaSWeston Andros Adamson static struct nfs_page *
212d72ddcbaSWeston Andros Adamson nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset)
213d72ddcbaSWeston Andros Adamson {
214d72ddcbaSWeston Andros Adamson 	struct nfs_page *req;
215d72ddcbaSWeston Andros Adamson 
216d72ddcbaSWeston Andros Adamson 	WARN_ON_ONCE(head != head->wb_head);
217d72ddcbaSWeston Andros Adamson 	WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_head->wb_flags));
218d72ddcbaSWeston Andros Adamson 
219d72ddcbaSWeston Andros Adamson 	req = head;
220d72ddcbaSWeston Andros Adamson 	do {
221d72ddcbaSWeston Andros Adamson 		if (page_offset >= req->wb_pgbase &&
222d72ddcbaSWeston Andros Adamson 		    page_offset < (req->wb_pgbase + req->wb_bytes))
223d72ddcbaSWeston Andros Adamson 			return req;
224d72ddcbaSWeston Andros Adamson 
225d72ddcbaSWeston Andros Adamson 		req = req->wb_this_page;
226d72ddcbaSWeston Andros Adamson 	} while (req != head);
227d72ddcbaSWeston Andros Adamson 
228d72ddcbaSWeston Andros Adamson 	return NULL;
229d72ddcbaSWeston Andros Adamson }
230d72ddcbaSWeston Andros Adamson 
231d72ddcbaSWeston Andros Adamson /*
232d72ddcbaSWeston Andros Adamson  * nfs_page_group_covers_page
233d72ddcbaSWeston Andros Adamson  * @head - head request of page group
234d72ddcbaSWeston Andros Adamson  *
235d72ddcbaSWeston Andros Adamson  * Return true if the page group with head @head covers the whole page,
236d72ddcbaSWeston Andros Adamson  * returns false otherwise
237d72ddcbaSWeston Andros Adamson  */
238d72ddcbaSWeston Andros Adamson static bool nfs_page_group_covers_page(struct nfs_page *req)
239d72ddcbaSWeston Andros Adamson {
240d72ddcbaSWeston Andros Adamson 	struct nfs_page *tmp;
241d72ddcbaSWeston Andros Adamson 	unsigned int pos = 0;
242d72ddcbaSWeston Andros Adamson 	unsigned int len = nfs_page_length(req->wb_page);
243d72ddcbaSWeston Andros Adamson 
244fd2f3a06SWeston Andros Adamson 	nfs_page_group_lock(req, false);
245d72ddcbaSWeston Andros Adamson 
246d72ddcbaSWeston Andros Adamson 	do {
247d72ddcbaSWeston Andros Adamson 		tmp = nfs_page_group_search_locked(req->wb_head, pos);
248d72ddcbaSWeston Andros Adamson 		if (tmp) {
249d72ddcbaSWeston Andros Adamson 			/* no way this should happen */
250d72ddcbaSWeston Andros Adamson 			WARN_ON_ONCE(tmp->wb_pgbase != pos);
251d72ddcbaSWeston Andros Adamson 			pos += tmp->wb_bytes - (pos - tmp->wb_pgbase);
252d72ddcbaSWeston Andros Adamson 		}
253d72ddcbaSWeston Andros Adamson 	} while (tmp && pos < len);
254d72ddcbaSWeston Andros Adamson 
255d72ddcbaSWeston Andros Adamson 	nfs_page_group_unlock(req);
256d72ddcbaSWeston Andros Adamson 	WARN_ON_ONCE(pos > len);
257d72ddcbaSWeston Andros Adamson 	return pos == len;
258d72ddcbaSWeston Andros Adamson }
259d72ddcbaSWeston Andros Adamson 
2601da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
2611da177e4SLinus Torvalds  * covers the full page.
2621da177e4SLinus Torvalds  */
263d72ddcbaSWeston Andros Adamson static void nfs_mark_uptodate(struct nfs_page *req)
2641da177e4SLinus Torvalds {
265d72ddcbaSWeston Andros Adamson 	if (PageUptodate(req->wb_page))
2661da177e4SLinus Torvalds 		return;
267d72ddcbaSWeston Andros Adamson 	if (!nfs_page_group_covers_page(req))
2681da177e4SLinus Torvalds 		return;
269d72ddcbaSWeston Andros Adamson 	SetPageUptodate(req->wb_page);
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
2731da177e4SLinus Torvalds {
2741da177e4SLinus Torvalds 	if (wbc->for_reclaim)
275c63c7b05STrond Myklebust 		return FLUSH_HIGHPRI | FLUSH_STABLE;
276b17621feSWu Fengguang 	if (wbc->for_kupdate || wbc->for_background)
277b31268acSTrond Myklebust 		return FLUSH_LOWPRI | FLUSH_COND_STABLE;
278b31268acSTrond Myklebust 	return FLUSH_COND_STABLE;
2791da177e4SLinus Torvalds }
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds /*
28289a09141SPeter Zijlstra  * NFS congestion control
28389a09141SPeter Zijlstra  */
28489a09141SPeter Zijlstra 
28589a09141SPeter Zijlstra int nfs_congestion_kb;
28689a09141SPeter Zijlstra 
28789a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
28889a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
28989a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
29089a09141SPeter Zijlstra 
291deed85e7STrond Myklebust static void nfs_set_page_writeback(struct page *page)
29289a09141SPeter Zijlstra {
293deed85e7STrond Myklebust 	struct nfs_server *nfss = NFS_SERVER(page_file_mapping(page)->host);
2945a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
2955a6d41b3STrond Myklebust 
296deed85e7STrond Myklebust 	WARN_ON_ONCE(ret != 0);
29789a09141SPeter Zijlstra 
298277866a0SPeter Zijlstra 	if (atomic_long_inc_return(&nfss->writeback) >
2998aa7e847SJens Axboe 			NFS_CONGESTION_ON_THRESH) {
3008aa7e847SJens Axboe 		set_bdi_congested(&nfss->backing_dev_info,
3018aa7e847SJens Axboe 					BLK_RW_ASYNC);
3028aa7e847SJens Axboe 	}
30389a09141SPeter Zijlstra }
30489a09141SPeter Zijlstra 
30520633f04SWeston Andros Adamson static void nfs_end_page_writeback(struct nfs_page *req)
30689a09141SPeter Zijlstra {
30720633f04SWeston Andros Adamson 	struct inode *inode = page_file_mapping(req->wb_page)->host;
30889a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
30989a09141SPeter Zijlstra 
31020633f04SWeston Andros Adamson 	if (!nfs_page_group_sync_on_bit(req, PG_WB_END))
31120633f04SWeston Andros Adamson 		return;
31220633f04SWeston Andros Adamson 
31320633f04SWeston Andros Adamson 	end_page_writeback(req->wb_page);
314c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
3158aa7e847SJens Axboe 		clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
31689a09141SPeter Zijlstra }
31789a09141SPeter Zijlstra 
318d4581383SWeston Andros Adamson 
319d4581383SWeston Andros Adamson /* nfs_page_group_clear_bits
320d4581383SWeston Andros Adamson  *   @req - an nfs request
321d4581383SWeston Andros Adamson  * clears all page group related bits from @req
322d4581383SWeston Andros Adamson  */
323d4581383SWeston Andros Adamson static void
324d4581383SWeston Andros Adamson nfs_page_group_clear_bits(struct nfs_page *req)
325e261f51fSTrond Myklebust {
326d4581383SWeston Andros Adamson 	clear_bit(PG_TEARDOWN, &req->wb_flags);
327d4581383SWeston Andros Adamson 	clear_bit(PG_UNLOCKPAGE, &req->wb_flags);
328d4581383SWeston Andros Adamson 	clear_bit(PG_UPTODATE, &req->wb_flags);
329d4581383SWeston Andros Adamson 	clear_bit(PG_WB_END, &req->wb_flags);
330d4581383SWeston Andros Adamson 	clear_bit(PG_REMOVE, &req->wb_flags);
331d4581383SWeston Andros Adamson }
332d4581383SWeston Andros Adamson 
333d4581383SWeston Andros Adamson 
334d4581383SWeston Andros Adamson /*
335d4581383SWeston Andros Adamson  * nfs_unroll_locks_and_wait -  unlock all newly locked reqs and wait on @req
336d4581383SWeston Andros Adamson  *
337d4581383SWeston Andros Adamson  * this is a helper function for nfs_lock_and_join_requests
338d4581383SWeston Andros Adamson  *
339d4581383SWeston Andros Adamson  * @inode - inode associated with request page group, must be holding inode lock
340d4581383SWeston Andros Adamson  * @head  - head request of page group, must be holding head lock
341d4581383SWeston Andros Adamson  * @req   - request that couldn't lock and needs to wait on the req bit lock
342d4581383SWeston Andros Adamson  * @nonblock - if true, don't actually wait
343d4581383SWeston Andros Adamson  *
344d4581383SWeston Andros Adamson  * NOTE: this must be called holding page_group bit lock and inode spin lock
345d4581383SWeston Andros Adamson  *       and BOTH will be released before returning.
346d4581383SWeston Andros Adamson  *
347d4581383SWeston Andros Adamson  * returns 0 on success, < 0 on error.
348d4581383SWeston Andros Adamson  */
349d4581383SWeston Andros Adamson static int
350d4581383SWeston Andros Adamson nfs_unroll_locks_and_wait(struct inode *inode, struct nfs_page *head,
351d4581383SWeston Andros Adamson 			  struct nfs_page *req, bool nonblock)
352d4581383SWeston Andros Adamson 	__releases(&inode->i_lock)
353d4581383SWeston Andros Adamson {
354d4581383SWeston Andros Adamson 	struct nfs_page *tmp;
355e261f51fSTrond Myklebust 	int ret;
356e261f51fSTrond Myklebust 
357d4581383SWeston Andros Adamson 	/* relinquish all the locks successfully grabbed this run */
358d4581383SWeston Andros Adamson 	for (tmp = head ; tmp != req; tmp = tmp->wb_this_page)
359d4581383SWeston Andros Adamson 		nfs_unlock_request(tmp);
360d4581383SWeston Andros Adamson 
361d4581383SWeston Andros Adamson 	WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
362d4581383SWeston Andros Adamson 
363d4581383SWeston Andros Adamson 	/* grab a ref on the request that will be waited on */
364d4581383SWeston Andros Adamson 	kref_get(&req->wb_kref);
365d4581383SWeston Andros Adamson 
366d4581383SWeston Andros Adamson 	nfs_page_group_unlock(head);
367587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
368d4581383SWeston Andros Adamson 
369d4581383SWeston Andros Adamson 	/* release ref from nfs_page_find_head_request_locked */
370d4581383SWeston Andros Adamson 	nfs_release_request(head);
371d4581383SWeston Andros Adamson 
372cfb506e1STrond Myklebust 	if (!nonblock)
373e261f51fSTrond Myklebust 		ret = nfs_wait_on_request(req);
374cfb506e1STrond Myklebust 	else
375cfb506e1STrond Myklebust 		ret = -EAGAIN;
376e261f51fSTrond Myklebust 	nfs_release_request(req);
377d4581383SWeston Andros Adamson 
378d4581383SWeston Andros Adamson 	return ret;
379e261f51fSTrond Myklebust }
380d4581383SWeston Andros Adamson 
381d4581383SWeston Andros Adamson /*
382d4581383SWeston Andros Adamson  * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests
383d4581383SWeston Andros Adamson  *
384d4581383SWeston Andros Adamson  * @destroy_list - request list (using wb_this_page) terminated by @old_head
385d4581383SWeston Andros Adamson  * @old_head - the old head of the list
386d4581383SWeston Andros Adamson  *
387d4581383SWeston Andros Adamson  * All subrequests must be locked and removed from all lists, so at this point
388d4581383SWeston Andros Adamson  * they are only "active" in this function, and possibly in nfs_wait_on_request
389d4581383SWeston Andros Adamson  * with a reference held by some other context.
390d4581383SWeston Andros Adamson  */
391d4581383SWeston Andros Adamson static void
392d4581383SWeston Andros Adamson nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list,
393d4581383SWeston Andros Adamson 				 struct nfs_page *old_head)
394d4581383SWeston Andros Adamson {
395d4581383SWeston Andros Adamson 	while (destroy_list) {
396d4581383SWeston Andros Adamson 		struct nfs_page *subreq = destroy_list;
397d4581383SWeston Andros Adamson 
398d4581383SWeston Andros Adamson 		destroy_list = (subreq->wb_this_page == old_head) ?
399d4581383SWeston Andros Adamson 				   NULL : subreq->wb_this_page;
400d4581383SWeston Andros Adamson 
401d4581383SWeston Andros Adamson 		WARN_ON_ONCE(old_head != subreq->wb_head);
402d4581383SWeston Andros Adamson 
403d4581383SWeston Andros Adamson 		/* make sure old group is not used */
404d4581383SWeston Andros Adamson 		subreq->wb_head = subreq;
405d4581383SWeston Andros Adamson 		subreq->wb_this_page = subreq;
406d4581383SWeston Andros Adamson 
407d4581383SWeston Andros Adamson 		/* subreq is now totally disconnected from page group or any
408d4581383SWeston Andros Adamson 		 * write / commit lists. last chance to wake any waiters */
409d4581383SWeston Andros Adamson 		nfs_unlock_request(subreq);
410d4581383SWeston Andros Adamson 
411d4581383SWeston Andros Adamson 		if (!test_bit(PG_TEARDOWN, &subreq->wb_flags)) {
412d4581383SWeston Andros Adamson 			/* release ref on old head request */
413d4581383SWeston Andros Adamson 			nfs_release_request(old_head);
414d4581383SWeston Andros Adamson 
415d4581383SWeston Andros Adamson 			nfs_page_group_clear_bits(subreq);
416d4581383SWeston Andros Adamson 
417d4581383SWeston Andros Adamson 			/* release the PG_INODE_REF reference */
418d4581383SWeston Andros Adamson 			if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags))
419d4581383SWeston Andros Adamson 				nfs_release_request(subreq);
420d4581383SWeston Andros Adamson 			else
421d4581383SWeston Andros Adamson 				WARN_ON_ONCE(1);
422d4581383SWeston Andros Adamson 		} else {
423d4581383SWeston Andros Adamson 			WARN_ON_ONCE(test_bit(PG_CLEAN, &subreq->wb_flags));
424d4581383SWeston Andros Adamson 			/* zombie requests have already released the last
425d4581383SWeston Andros Adamson 			 * reference and were waiting on the rest of the
426d4581383SWeston Andros Adamson 			 * group to complete. Since it's no longer part of a
427d4581383SWeston Andros Adamson 			 * group, simply free the request */
428d4581383SWeston Andros Adamson 			nfs_page_group_clear_bits(subreq);
429d4581383SWeston Andros Adamson 			nfs_free_request(subreq);
430d4581383SWeston Andros Adamson 		}
431d4581383SWeston Andros Adamson 	}
432d4581383SWeston Andros Adamson }
433d4581383SWeston Andros Adamson 
434d4581383SWeston Andros Adamson /*
435d4581383SWeston Andros Adamson  * nfs_lock_and_join_requests - join all subreqs to the head req and return
436d4581383SWeston Andros Adamson  *                              a locked reference, cancelling any pending
437d4581383SWeston Andros Adamson  *                              operations for this page.
438d4581383SWeston Andros Adamson  *
439d4581383SWeston Andros Adamson  * @page - the page used to lookup the "page group" of nfs_page structures
440d4581383SWeston Andros Adamson  * @nonblock - if true, don't block waiting for request locks
441d4581383SWeston Andros Adamson  *
442d4581383SWeston Andros Adamson  * This function joins all sub requests to the head request by first
443d4581383SWeston Andros Adamson  * locking all requests in the group, cancelling any pending operations
444d4581383SWeston Andros Adamson  * and finally updating the head request to cover the whole range covered by
445d4581383SWeston Andros Adamson  * the (former) group.  All subrequests are removed from any write or commit
446d4581383SWeston Andros Adamson  * lists, unlinked from the group and destroyed.
447d4581383SWeston Andros Adamson  *
448d4581383SWeston Andros Adamson  * Returns a locked, referenced pointer to the head request - which after
449d4581383SWeston Andros Adamson  * this call is guaranteed to be the only request associated with the page.
450d4581383SWeston Andros Adamson  * Returns NULL if no requests are found for @page, or a ERR_PTR if an
451d4581383SWeston Andros Adamson  * error was encountered.
452d4581383SWeston Andros Adamson  */
453d4581383SWeston Andros Adamson static struct nfs_page *
454d4581383SWeston Andros Adamson nfs_lock_and_join_requests(struct page *page, bool nonblock)
455d4581383SWeston Andros Adamson {
456d4581383SWeston Andros Adamson 	struct inode *inode = page_file_mapping(page)->host;
457d4581383SWeston Andros Adamson 	struct nfs_page *head, *subreq;
458d4581383SWeston Andros Adamson 	struct nfs_page *destroy_list = NULL;
459d4581383SWeston Andros Adamson 	unsigned int total_bytes;
460d4581383SWeston Andros Adamson 	int ret;
461d4581383SWeston Andros Adamson 
462d4581383SWeston Andros Adamson try_again:
463d4581383SWeston Andros Adamson 	total_bytes = 0;
464d4581383SWeston Andros Adamson 
465d4581383SWeston Andros Adamson 	WARN_ON_ONCE(destroy_list);
466d4581383SWeston Andros Adamson 
467d4581383SWeston Andros Adamson 	spin_lock(&inode->i_lock);
468d4581383SWeston Andros Adamson 
469d4581383SWeston Andros Adamson 	/*
470d4581383SWeston Andros Adamson 	 * A reference is taken only on the head request which acts as a
471d4581383SWeston Andros Adamson 	 * reference to the whole page group - the group will not be destroyed
472d4581383SWeston Andros Adamson 	 * until the head reference is released.
473d4581383SWeston Andros Adamson 	 */
474d4581383SWeston Andros Adamson 	head = nfs_page_find_head_request_locked(NFS_I(inode), page);
475d4581383SWeston Andros Adamson 
476d4581383SWeston Andros Adamson 	if (!head) {
477587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
478d4581383SWeston Andros Adamson 		return NULL;
479d4581383SWeston Andros Adamson 	}
480d4581383SWeston Andros Adamson 
481*7c3af975SWeston Andros Adamson 	/* holding inode lock, so always make a non-blocking call to try the
482*7c3af975SWeston Andros Adamson 	 * page group lock */
483fd2f3a06SWeston Andros Adamson 	ret = nfs_page_group_lock(head, true);
48494970014SWeston Andros Adamson 	if (ret < 0) {
48594970014SWeston Andros Adamson 		spin_unlock(&inode->i_lock);
486*7c3af975SWeston Andros Adamson 
487*7c3af975SWeston Andros Adamson 		if (!nonblock && ret == -EAGAIN) {
488*7c3af975SWeston Andros Adamson 			nfs_page_group_lock_wait(head);
489*7c3af975SWeston Andros Adamson 			nfs_release_request(head);
490*7c3af975SWeston Andros Adamson 			goto try_again;
491*7c3af975SWeston Andros Adamson 		}
492*7c3af975SWeston Andros Adamson 
49394970014SWeston Andros Adamson 		nfs_release_request(head);
494e7029206SWeston Andros Adamson 		return ERR_PTR(ret);
49594970014SWeston Andros Adamson 	}
496*7c3af975SWeston Andros Adamson 
497*7c3af975SWeston Andros Adamson 	/* lock each request in the page group */
498d4581383SWeston Andros Adamson 	subreq = head;
499d4581383SWeston Andros Adamson 	do {
500d4581383SWeston Andros Adamson 		/*
501d4581383SWeston Andros Adamson 		 * Subrequests are always contiguous, non overlapping
502d4581383SWeston Andros Adamson 		 * and in order. If not, it's a programming error.
503d4581383SWeston Andros Adamson 		 */
504d4581383SWeston Andros Adamson 		WARN_ON_ONCE(subreq->wb_offset !=
505d4581383SWeston Andros Adamson 		     (head->wb_offset + total_bytes));
506d4581383SWeston Andros Adamson 
507d4581383SWeston Andros Adamson 		/* keep track of how many bytes this group covers */
508d4581383SWeston Andros Adamson 		total_bytes += subreq->wb_bytes;
509d4581383SWeston Andros Adamson 
510d4581383SWeston Andros Adamson 		if (!nfs_lock_request(subreq)) {
511d4581383SWeston Andros Adamson 			/* releases page group bit lock and
512d4581383SWeston Andros Adamson 			 * inode spin lock and all references */
513d4581383SWeston Andros Adamson 			ret = nfs_unroll_locks_and_wait(inode, head,
514d4581383SWeston Andros Adamson 				subreq, nonblock);
515d4581383SWeston Andros Adamson 
516d4581383SWeston Andros Adamson 			if (ret == 0)
517d4581383SWeston Andros Adamson 				goto try_again;
518d4581383SWeston Andros Adamson 
519d4581383SWeston Andros Adamson 			return ERR_PTR(ret);
520d4581383SWeston Andros Adamson 		}
521d4581383SWeston Andros Adamson 
522d4581383SWeston Andros Adamson 		subreq = subreq->wb_this_page;
523d4581383SWeston Andros Adamson 	} while (subreq != head);
524d4581383SWeston Andros Adamson 
525d4581383SWeston Andros Adamson 	/* Now that all requests are locked, make sure they aren't on any list.
526d4581383SWeston Andros Adamson 	 * Commit list removal accounting is done after locks are dropped */
527d4581383SWeston Andros Adamson 	subreq = head;
528d4581383SWeston Andros Adamson 	do {
529411a99adSWeston Andros Adamson 		nfs_clear_request_commit(subreq);
530d4581383SWeston Andros Adamson 		subreq = subreq->wb_this_page;
531d4581383SWeston Andros Adamson 	} while (subreq != head);
532d4581383SWeston Andros Adamson 
533d4581383SWeston Andros Adamson 	/* unlink subrequests from head, destroy them later */
534d4581383SWeston Andros Adamson 	if (head->wb_this_page != head) {
535d4581383SWeston Andros Adamson 		/* destroy list will be terminated by head */
536d4581383SWeston Andros Adamson 		destroy_list = head->wb_this_page;
537d4581383SWeston Andros Adamson 		head->wb_this_page = head;
538d4581383SWeston Andros Adamson 
539d4581383SWeston Andros Adamson 		/* change head request to cover whole range that
540d4581383SWeston Andros Adamson 		 * the former page group covered */
541d4581383SWeston Andros Adamson 		head->wb_bytes = total_bytes;
542d4581383SWeston Andros Adamson 	}
543d4581383SWeston Andros Adamson 
544d4581383SWeston Andros Adamson 	/*
545d4581383SWeston Andros Adamson 	 * prepare head request to be added to new pgio descriptor
546d4581383SWeston Andros Adamson 	 */
547d4581383SWeston Andros Adamson 	nfs_page_group_clear_bits(head);
548d4581383SWeston Andros Adamson 
549d4581383SWeston Andros Adamson 	/*
550d4581383SWeston Andros Adamson 	 * some part of the group was still on the inode list - otherwise
551d4581383SWeston Andros Adamson 	 * the group wouldn't be involved in async write.
552d4581383SWeston Andros Adamson 	 * grab a reference for the head request, iff it needs one.
553d4581383SWeston Andros Adamson 	 */
554d4581383SWeston Andros Adamson 	if (!test_and_set_bit(PG_INODE_REF, &head->wb_flags))
555d4581383SWeston Andros Adamson 		kref_get(&head->wb_kref);
556d4581383SWeston Andros Adamson 
557d4581383SWeston Andros Adamson 	nfs_page_group_unlock(head);
558d4581383SWeston Andros Adamson 
559411a99adSWeston Andros Adamson 	/* drop lock to clean uprequests on destroy list */
560d4581383SWeston Andros Adamson 	spin_unlock(&inode->i_lock);
561d4581383SWeston Andros Adamson 
562d4581383SWeston Andros Adamson 	nfs_destroy_unlinked_subrequests(destroy_list, head);
563d4581383SWeston Andros Adamson 
564d4581383SWeston Andros Adamson 	/* still holds ref on head from nfs_page_find_head_request_locked
565d4581383SWeston Andros Adamson 	 * and still has lock on head from lock loop */
566d4581383SWeston Andros Adamson 	return head;
567612c9384STrond Myklebust }
568074cc1deSTrond Myklebust 
569074cc1deSTrond Myklebust /*
570074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
571074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
572074cc1deSTrond Myklebust  */
573074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
574cfb506e1STrond Myklebust 				struct page *page, bool nonblock)
575074cc1deSTrond Myklebust {
576074cc1deSTrond Myklebust 	struct nfs_page *req;
577074cc1deSTrond Myklebust 	int ret = 0;
578074cc1deSTrond Myklebust 
579d4581383SWeston Andros Adamson 	req = nfs_lock_and_join_requests(page, nonblock);
580074cc1deSTrond Myklebust 	if (!req)
581074cc1deSTrond Myklebust 		goto out;
582074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
583074cc1deSTrond Myklebust 	if (IS_ERR(req))
584074cc1deSTrond Myklebust 		goto out;
585074cc1deSTrond Myklebust 
586deed85e7STrond Myklebust 	nfs_set_page_writeback(page);
587deed85e7STrond Myklebust 	WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
588074cc1deSTrond Myklebust 
589deed85e7STrond Myklebust 	ret = 0;
590f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
591f8512ad0SFred Isaman 		nfs_redirty_request(req);
592074cc1deSTrond Myklebust 		ret = pgio->pg_error;
593f8512ad0SFred Isaman 	}
594074cc1deSTrond Myklebust out:
595074cc1deSTrond Myklebust 	return ret;
596e261f51fSTrond Myklebust }
597e261f51fSTrond Myklebust 
598f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
599f758c885STrond Myklebust {
600d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
601cfb506e1STrond Myklebust 	int ret;
602f758c885STrond Myklebust 
603f758c885STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
604f758c885STrond Myklebust 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
605f758c885STrond Myklebust 
606d56b4ddfSMel Gorman 	nfs_pageio_cond_complete(pgio, page_file_index(page));
6071b430beeSWu Fengguang 	ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
608cfb506e1STrond Myklebust 	if (ret == -EAGAIN) {
609cfb506e1STrond Myklebust 		redirty_page_for_writepage(wbc, page);
610cfb506e1STrond Myklebust 		ret = 0;
611cfb506e1STrond Myklebust 	}
612cfb506e1STrond Myklebust 	return ret;
613f758c885STrond Myklebust }
614f758c885STrond Myklebust 
615e261f51fSTrond Myklebust /*
6161da177e4SLinus Torvalds  * Write an mmapped page to the server.
6171da177e4SLinus Torvalds  */
6184d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
6191da177e4SLinus Torvalds {
620f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
621e261f51fSTrond Myklebust 	int err;
6221da177e4SLinus Torvalds 
623a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc),
624a20c93e3SChristoph Hellwig 				false, &nfs_async_write_completion_ops);
625f758c885STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
626f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
627f758c885STrond Myklebust 	if (err < 0)
6284d770ccfSTrond Myklebust 		return err;
629f758c885STrond Myklebust 	if (pgio.pg_error < 0)
630f758c885STrond Myklebust 		return pgio.pg_error;
631f758c885STrond Myklebust 	return 0;
6324d770ccfSTrond Myklebust }
6334d770ccfSTrond Myklebust 
6344d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
6354d770ccfSTrond Myklebust {
636f758c885STrond Myklebust 	int ret;
6374d770ccfSTrond Myklebust 
638f758c885STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
6391da177e4SLinus Torvalds 	unlock_page(page);
640f758c885STrond Myklebust 	return ret;
641f758c885STrond Myklebust }
642f758c885STrond Myklebust 
643f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
644f758c885STrond Myklebust {
645f758c885STrond Myklebust 	int ret;
646f758c885STrond Myklebust 
647f758c885STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
648f758c885STrond Myklebust 	unlock_page(page);
649f758c885STrond Myklebust 	return ret;
6501da177e4SLinus Torvalds }
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
6531da177e4SLinus Torvalds {
6541da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
65572cb77f4STrond Myklebust 	unsigned long *bitlock = &NFS_I(inode)->flags;
656c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
6571da177e4SLinus Torvalds 	int err;
6581da177e4SLinus Torvalds 
65972cb77f4STrond Myklebust 	/* Stop dirtying of new pages while we sync */
66074316201SNeilBrown 	err = wait_on_bit_lock_action(bitlock, NFS_INO_FLUSHING,
66172cb77f4STrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
66272cb77f4STrond Myklebust 	if (err)
66372cb77f4STrond Myklebust 		goto out_err;
66472cb77f4STrond Myklebust 
66591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
66691d5b470SChuck Lever 
667a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false,
668a20c93e3SChristoph Hellwig 				&nfs_async_write_completion_ops);
669f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
670c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
67172cb77f4STrond Myklebust 
67272cb77f4STrond Myklebust 	clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
6734e857c58SPeter Zijlstra 	smp_mb__after_atomic();
67472cb77f4STrond Myklebust 	wake_up_bit(bitlock, NFS_INO_FLUSHING);
67572cb77f4STrond Myklebust 
676f758c885STrond Myklebust 	if (err < 0)
67772cb77f4STrond Myklebust 		goto out_err;
67872cb77f4STrond Myklebust 	err = pgio.pg_error;
67972cb77f4STrond Myklebust 	if (err < 0)
68072cb77f4STrond Myklebust 		goto out_err;
681c63c7b05STrond Myklebust 	return 0;
68272cb77f4STrond Myklebust out_err:
68372cb77f4STrond Myklebust 	return err;
6841da177e4SLinus Torvalds }
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds /*
6871da177e4SLinus Torvalds  * Insert a write request into an inode
6881da177e4SLinus Torvalds  */
689d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
6901da177e4SLinus Torvalds {
6911da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
692e7d39069STrond Myklebust 
6932bfc6e56SWeston Andros Adamson 	WARN_ON_ONCE(req->wb_this_page != req);
6942bfc6e56SWeston Andros Adamson 
695e7d39069STrond Myklebust 	/* Lock the request! */
6967ad84aa9STrond Myklebust 	nfs_lock_request(req);
697e7d39069STrond Myklebust 
698e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
699011e2a7fSBryan Schumaker 	if (!nfsi->npages && NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
700a9a4a87aSTrond Myklebust 		inode->i_version++;
70129418aa4SMel Gorman 	/*
70229418aa4SMel Gorman 	 * Swap-space should not get truncated. Hence no need to plug the race
70329418aa4SMel Gorman 	 * with invalidate/truncate.
70429418aa4SMel Gorman 	 */
70529418aa4SMel Gorman 	if (likely(!PageSwapCache(req->wb_page))) {
7062df485a7STrond Myklebust 		set_bit(PG_MAPPED, &req->wb_flags);
707deb7d638STrond Myklebust 		SetPagePrivate(req->wb_page);
708277459d2STrond Myklebust 		set_page_private(req->wb_page, (unsigned long)req);
70929418aa4SMel Gorman 	}
7101da177e4SLinus Torvalds 	nfsi->npages++;
71117089a29SWeston Andros Adamson 	/* this a head request for a page group - mark it as having an
71217089a29SWeston Andros Adamson 	 * extra reference so sub groups can follow suit */
71317089a29SWeston Andros Adamson 	WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags));
714c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
715e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds /*
71989a09141SPeter Zijlstra  * Remove a write request from an inode
7201da177e4SLinus Torvalds  */
7211da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
7221da177e4SLinus Torvalds {
7233d4ff43dSAl Viro 	struct inode *inode = req->wb_context->dentry->d_inode;
7241da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
72520633f04SWeston Andros Adamson 	struct nfs_page *head;
72620633f04SWeston Andros Adamson 
72720633f04SWeston Andros Adamson 	if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) {
72820633f04SWeston Andros Adamson 		head = req->wb_head;
7291da177e4SLinus Torvalds 
730587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
73120633f04SWeston Andros Adamson 		if (likely(!PageSwapCache(head->wb_page))) {
73220633f04SWeston Andros Adamson 			set_page_private(head->wb_page, 0);
73320633f04SWeston Andros Adamson 			ClearPagePrivate(head->wb_page);
73420633f04SWeston Andros Adamson 			clear_bit(PG_MAPPED, &head->wb_flags);
73529418aa4SMel Gorman 		}
7361da177e4SLinus Torvalds 		nfsi->npages--;
737587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
73820633f04SWeston Andros Adamson 	}
73917089a29SWeston Andros Adamson 
74017089a29SWeston Andros Adamson 	if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags))
7411da177e4SLinus Torvalds 		nfs_release_request(req);
742b412ddf0SWeston Andros Adamson 	else
743b412ddf0SWeston Andros Adamson 		WARN_ON_ONCE(1);
7441da177e4SLinus Torvalds }
7451da177e4SLinus Torvalds 
74661822ab5STrond Myklebust static void
7476d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
74861822ab5STrond Myklebust {
74961822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
75061822ab5STrond Myklebust }
75161822ab5STrond Myklebust 
75289d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
7538dd37758STrond Myklebust /**
7548dd37758STrond Myklebust  * nfs_request_add_commit_list - add request to a commit list
7558dd37758STrond Myklebust  * @req: pointer to a struct nfs_page
756ea2cf228SFred Isaman  * @dst: commit list head
757ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
7588dd37758STrond Myklebust  *
759ea2cf228SFred Isaman  * This sets the PG_CLEAN bit, updates the cinfo count of
7608dd37758STrond Myklebust  * number of outstanding requests requiring a commit as well as
7618dd37758STrond Myklebust  * the MM page stats.
7628dd37758STrond Myklebust  *
763ea2cf228SFred Isaman  * The caller must _not_ hold the cinfo->lock, but must be
7648dd37758STrond Myklebust  * holding the nfs_page lock.
7658dd37758STrond Myklebust  */
7668dd37758STrond Myklebust void
767ea2cf228SFred Isaman nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst,
768ea2cf228SFred Isaman 			    struct nfs_commit_info *cinfo)
7698dd37758STrond Myklebust {
7708dd37758STrond Myklebust 	set_bit(PG_CLEAN, &(req)->wb_flags);
771ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
772ea2cf228SFred Isaman 	nfs_list_add_request(req, dst);
773ea2cf228SFred Isaman 	cinfo->mds->ncommit++;
774ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
77556f9cd68SFred Isaman 	if (!cinfo->dreq) {
7768dd37758STrond Myklebust 		inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
777d56b4ddfSMel Gorman 		inc_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
77856f9cd68SFred Isaman 			     BDI_RECLAIMABLE);
77956f9cd68SFred Isaman 		__mark_inode_dirty(req->wb_context->dentry->d_inode,
78056f9cd68SFred Isaman 				   I_DIRTY_DATASYNC);
78156f9cd68SFred Isaman 	}
7828dd37758STrond Myklebust }
7838dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
7848dd37758STrond Myklebust 
7858dd37758STrond Myklebust /**
7868dd37758STrond Myklebust  * nfs_request_remove_commit_list - Remove request from a commit list
7878dd37758STrond Myklebust  * @req: pointer to a nfs_page
788ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
7898dd37758STrond Myklebust  *
790ea2cf228SFred Isaman  * This clears the PG_CLEAN bit, and updates the cinfo's count of
7918dd37758STrond Myklebust  * number of outstanding requests requiring a commit
7928dd37758STrond Myklebust  * It does not update the MM page stats.
7938dd37758STrond Myklebust  *
794ea2cf228SFred Isaman  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
7958dd37758STrond Myklebust  */
7968dd37758STrond Myklebust void
797ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req,
798ea2cf228SFred Isaman 			       struct nfs_commit_info *cinfo)
7998dd37758STrond Myklebust {
8008dd37758STrond Myklebust 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
8018dd37758STrond Myklebust 		return;
8028dd37758STrond Myklebust 	nfs_list_remove_request(req);
803ea2cf228SFred Isaman 	cinfo->mds->ncommit--;
8048dd37758STrond Myklebust }
8058dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
8068dd37758STrond Myklebust 
807ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
808ea2cf228SFred Isaman 				      struct inode *inode)
809ea2cf228SFred Isaman {
810ea2cf228SFred Isaman 	cinfo->lock = &inode->i_lock;
811ea2cf228SFred Isaman 	cinfo->mds = &NFS_I(inode)->commit_info;
812ea2cf228SFred Isaman 	cinfo->ds = pnfs_get_ds_info(inode);
813b359f9d0SFred Isaman 	cinfo->dreq = NULL;
814f453a54aSFred Isaman 	cinfo->completion_ops = &nfs_commit_completion_ops;
815ea2cf228SFred Isaman }
816ea2cf228SFred Isaman 
817ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
818ea2cf228SFred Isaman 		    struct inode *inode,
819ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq)
820ea2cf228SFred Isaman {
8211763da12SFred Isaman 	if (dreq)
8221763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
8231763da12SFred Isaman 	else
824ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(cinfo, inode);
825ea2cf228SFred Isaman }
826ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo);
8278dd37758STrond Myklebust 
8281da177e4SLinus Torvalds /*
8291da177e4SLinus Torvalds  * Add a request to the inode's commit list.
8301da177e4SLinus Torvalds  */
8311763da12SFred Isaman void
832ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
833ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
8341da177e4SLinus Torvalds {
835ea2cf228SFred Isaman 	if (pnfs_mark_request_commit(req, lseg, cinfo))
8368dd37758STrond Myklebust 		return;
837ea2cf228SFred Isaman 	nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo);
8381da177e4SLinus Torvalds }
8398e821cadSTrond Myklebust 
840d6d6dc7cSFred Isaman static void
841d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page)
842e468bae9STrond Myklebust {
843e468bae9STrond Myklebust 	dec_zone_page_state(page, NR_UNSTABLE_NFS);
844d56b4ddfSMel Gorman 	dec_bdi_stat(page_file_mapping(page)->backing_dev_info, BDI_RECLAIMABLE);
845e468bae9STrond Myklebust }
846d6d6dc7cSFred Isaman 
847411a99adSWeston Andros Adamson /* Called holding inode (/cinfo) lock */
8488dd37758STrond Myklebust static void
849d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req)
850d6d6dc7cSFred Isaman {
8518dd37758STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
8528dd37758STrond Myklebust 		struct inode *inode = req->wb_context->dentry->d_inode;
853ea2cf228SFred Isaman 		struct nfs_commit_info cinfo;
854d6d6dc7cSFred Isaman 
855ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(&cinfo, inode);
856ea2cf228SFred Isaman 		if (!pnfs_clear_request_commit(req, &cinfo)) {
857ea2cf228SFred Isaman 			nfs_request_remove_commit_list(req, &cinfo);
858d6d6dc7cSFred Isaman 		}
8598dd37758STrond Myklebust 		nfs_clear_page_commit(req->wb_page);
8608dd37758STrond Myklebust 	}
861e468bae9STrond Myklebust }
862e468bae9STrond Myklebust 
863d45f60c6SWeston Andros Adamson int nfs_write_need_commit(struct nfs_pgio_header *hdr)
8648e821cadSTrond Myklebust {
865c65e6254SWeston Andros Adamson 	if (hdr->verf.committed == NFS_DATA_SYNC)
866d45f60c6SWeston Andros Adamson 		return hdr->lseg == NULL;
867c65e6254SWeston Andros Adamson 	return hdr->verf.committed != NFS_FILE_SYNC;
8688e821cadSTrond Myklebust }
8698e821cadSTrond Myklebust 
8708e821cadSTrond Myklebust #else
87168cd6fa4SBryan Schumaker static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
87268cd6fa4SBryan Schumaker 				      struct inode *inode)
87368cd6fa4SBryan Schumaker {
87468cd6fa4SBryan Schumaker }
87568cd6fa4SBryan Schumaker 
87668cd6fa4SBryan Schumaker void nfs_init_cinfo(struct nfs_commit_info *cinfo,
87768cd6fa4SBryan Schumaker 		    struct inode *inode,
87868cd6fa4SBryan Schumaker 		    struct nfs_direct_req *dreq)
87968cd6fa4SBryan Schumaker {
88068cd6fa4SBryan Schumaker }
88168cd6fa4SBryan Schumaker 
8821763da12SFred Isaman void
883ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
884ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
8858e821cadSTrond Myklebust {
8868e821cadSTrond Myklebust }
8878e821cadSTrond Myklebust 
8888dd37758STrond Myklebust static void
889e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
890e468bae9STrond Myklebust {
891e468bae9STrond Myklebust }
892e468bae9STrond Myklebust 
893d45f60c6SWeston Andros Adamson int nfs_write_need_commit(struct nfs_pgio_header *hdr)
8948e821cadSTrond Myklebust {
8958e821cadSTrond Myklebust 	return 0;
8968e821cadSTrond Myklebust }
8978e821cadSTrond Myklebust 
8981da177e4SLinus Torvalds #endif
8991da177e4SLinus Torvalds 
900061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
9016c75dc0dSFred Isaman {
902ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
9036c75dc0dSFred Isaman 	unsigned long bytes = 0;
9046c75dc0dSFred Isaman 
9056c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
9066c75dc0dSFred Isaman 		goto out;
907ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
9086c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
9096c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
9106c75dc0dSFred Isaman 
9116c75dc0dSFred Isaman 		bytes += req->wb_bytes;
9126c75dc0dSFred Isaman 		nfs_list_remove_request(req);
9136c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
9146c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
915d1182b33STrond Myklebust 			nfs_set_pageerror(req->wb_page);
9166c75dc0dSFred Isaman 			nfs_context_set_write_error(req->wb_context, hdr->error);
9176c75dc0dSFred Isaman 			goto remove_req;
9186c75dc0dSFred Isaman 		}
919c65e6254SWeston Andros Adamson 		if (nfs_write_need_commit(hdr)) {
920f79d06f5SAnna Schumaker 			memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
921ea2cf228SFred Isaman 			nfs_mark_request_commit(req, hdr->lseg, &cinfo);
9226c75dc0dSFred Isaman 			goto next;
9236c75dc0dSFred Isaman 		}
9246c75dc0dSFred Isaman remove_req:
9256c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
9266c75dc0dSFred Isaman next:
9271d1afcbcSTrond Myklebust 		nfs_unlock_request(req);
92820633f04SWeston Andros Adamson 		nfs_end_page_writeback(req);
9293aff4ebbSTrond Myklebust 		nfs_release_request(req);
9306c75dc0dSFred Isaman 	}
9316c75dc0dSFred Isaman out:
9326c75dc0dSFred Isaman 	hdr->release(hdr);
9336c75dc0dSFred Isaman }
9346c75dc0dSFred Isaman 
93589d77c8fSBryan Schumaker #if  IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
936ce59515cSAnna Schumaker unsigned long
937ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
938fb8a1f11STrond Myklebust {
939ea2cf228SFred Isaman 	return cinfo->mds->ncommit;
940fb8a1f11STrond Myklebust }
941fb8a1f11STrond Myklebust 
942ea2cf228SFred Isaman /* cinfo->lock held by caller */
9431763da12SFred Isaman int
944ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
945ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
946d6d6dc7cSFred Isaman {
947d6d6dc7cSFred Isaman 	struct nfs_page *req, *tmp;
948d6d6dc7cSFred Isaman 	int ret = 0;
949d6d6dc7cSFred Isaman 
950d6d6dc7cSFred Isaman 	list_for_each_entry_safe(req, tmp, src, wb_list) {
9518dd37758STrond Myklebust 		if (!nfs_lock_request(req))
9528dd37758STrond Myklebust 			continue;
9537ad84aa9STrond Myklebust 		kref_get(&req->wb_kref);
954ea2cf228SFred Isaman 		if (cond_resched_lock(cinfo->lock))
9553b3be88dSTrond Myklebust 			list_safe_reset_next(req, tmp, wb_list);
956ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
9578dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
958d6d6dc7cSFred Isaman 		ret++;
9591763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
960d6d6dc7cSFred Isaman 			break;
961d6d6dc7cSFred Isaman 	}
962d6d6dc7cSFred Isaman 	return ret;
963d6d6dc7cSFred Isaman }
964d6d6dc7cSFred Isaman 
9651da177e4SLinus Torvalds /*
9661da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
9671da177e4SLinus Torvalds  * @inode: NFS inode to scan
968ea2cf228SFred Isaman  * @dst: mds destination list
969ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
9701da177e4SLinus Torvalds  *
9711da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
9721da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
9731da177e4SLinus Torvalds  */
9741763da12SFred Isaman int
975ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
976ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
9771da177e4SLinus Torvalds {
978d6d6dc7cSFred Isaman 	int ret = 0;
979fb8a1f11STrond Myklebust 
980ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
981ea2cf228SFred Isaman 	if (cinfo->mds->ncommit > 0) {
9828dd37758STrond Myklebust 		const int max = INT_MAX;
983d6d6dc7cSFred Isaman 
984ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
985ea2cf228SFred Isaman 					   cinfo, max);
986ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
987d6d6dc7cSFred Isaman 	}
988ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
989ff778d02STrond Myklebust 	return ret;
9901da177e4SLinus Torvalds }
991d6d6dc7cSFred Isaman 
992c42de9ddSTrond Myklebust #else
993ce59515cSAnna Schumaker unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
994fb8a1f11STrond Myklebust {
995fb8a1f11STrond Myklebust 	return 0;
996fb8a1f11STrond Myklebust }
997fb8a1f11STrond Myklebust 
9981763da12SFred Isaman int nfs_scan_commit(struct inode *inode, struct list_head *dst,
999ea2cf228SFred Isaman 		    struct nfs_commit_info *cinfo)
1000c42de9ddSTrond Myklebust {
1001c42de9ddSTrond Myklebust 	return 0;
1002c42de9ddSTrond Myklebust }
10031da177e4SLinus Torvalds #endif
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds /*
1006e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
1007e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
10081da177e4SLinus Torvalds  *
1009e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
1010e7d39069STrond Myklebust  * to disk.
10111da177e4SLinus Torvalds  */
1012e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
1013e7d39069STrond Myklebust 		struct page *page,
1014e7d39069STrond Myklebust 		unsigned int offset,
1015e7d39069STrond Myklebust 		unsigned int bytes)
10161da177e4SLinus Torvalds {
1017e7d39069STrond Myklebust 	struct nfs_page *req;
1018e7d39069STrond Myklebust 	unsigned int rqend;
1019e7d39069STrond Myklebust 	unsigned int end;
10201da177e4SLinus Torvalds 	int error;
1021277459d2STrond Myklebust 
1022e7d39069STrond Myklebust 	if (!PagePrivate(page))
1023e7d39069STrond Myklebust 		return NULL;
1024e7d39069STrond Myklebust 
1025e7d39069STrond Myklebust 	end = offset + bytes;
1026e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
1027e7d39069STrond Myklebust 
1028e7d39069STrond Myklebust 	for (;;) {
102984d3a9a9SWeston Andros Adamson 		req = nfs_page_find_head_request_locked(NFS_I(inode), page);
1030e7d39069STrond Myklebust 		if (req == NULL)
1031e7d39069STrond Myklebust 			goto out_unlock;
1032e7d39069STrond Myklebust 
10332bfc6e56SWeston Andros Adamson 		/* should be handled by nfs_flush_incompatible */
10342bfc6e56SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_head != req);
10352bfc6e56SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_this_page != req);
10362bfc6e56SWeston Andros Adamson 
1037e7d39069STrond Myklebust 		rqend = req->wb_offset + req->wb_bytes;
1038e7d39069STrond Myklebust 		/*
1039e7d39069STrond Myklebust 		 * Tell the caller to flush out the request if
1040e7d39069STrond Myklebust 		 * the offsets are non-contiguous.
1041e7d39069STrond Myklebust 		 * Note: nfs_flush_incompatible() will already
1042e7d39069STrond Myklebust 		 * have flushed out requests having wrong owners.
1043e7d39069STrond Myklebust 		 */
1044e468bae9STrond Myklebust 		if (offset > rqend
1045e7d39069STrond Myklebust 		    || end < req->wb_offset)
1046e7d39069STrond Myklebust 			goto out_flushme;
1047e7d39069STrond Myklebust 
10487ad84aa9STrond Myklebust 		if (nfs_lock_request(req))
1049e7d39069STrond Myklebust 			break;
1050e7d39069STrond Myklebust 
1051e7d39069STrond Myklebust 		/* The request is locked, so wait and then retry */
1052587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
10531da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
10541da177e4SLinus Torvalds 		nfs_release_request(req);
1055e7d39069STrond Myklebust 		if (error != 0)
1056e7d39069STrond Myklebust 			goto out_err;
1057e7d39069STrond Myklebust 		spin_lock(&inode->i_lock);
10581da177e4SLinus Torvalds 	}
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
10611da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
10621da177e4SLinus Torvalds 		req->wb_offset = offset;
10631da177e4SLinus Torvalds 		req->wb_pgbase = offset;
10641da177e4SLinus Torvalds 	}
10651da177e4SLinus Torvalds 	if (end > rqend)
10661da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
1067e7d39069STrond Myklebust 	else
1068e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
1069e7d39069STrond Myklebust out_unlock:
1070ca138f36SFred Isaman 	if (req)
10718dd37758STrond Myklebust 		nfs_clear_request_commit(req);
1072411a99adSWeston Andros Adamson 	spin_unlock(&inode->i_lock);
1073e7d39069STrond Myklebust 	return req;
1074e7d39069STrond Myklebust out_flushme:
1075e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
1076e7d39069STrond Myklebust 	nfs_release_request(req);
1077e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
1078e7d39069STrond Myklebust out_err:
1079e7d39069STrond Myklebust 	return ERR_PTR(error);
1080e7d39069STrond Myklebust }
10811da177e4SLinus Torvalds 
1082e7d39069STrond Myklebust /*
1083e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
1084e7d39069STrond Myklebust  *
1085e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
1086e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
1087e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
1088e7d39069STrond Myklebust  */
1089e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
1090e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
1091e7d39069STrond Myklebust {
1092d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
1093e7d39069STrond Myklebust 	struct nfs_page	*req;
1094e7d39069STrond Myklebust 
1095e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
1096e7d39069STrond Myklebust 	if (req != NULL)
1097e7d39069STrond Myklebust 		goto out;
10982bfc6e56SWeston Andros Adamson 	req = nfs_create_request(ctx, page, NULL, offset, bytes);
1099e7d39069STrond Myklebust 	if (IS_ERR(req))
1100e7d39069STrond Myklebust 		goto out;
1101d6d6dc7cSFred Isaman 	nfs_inode_add_request(inode, req);
1102efc91ed0STrond Myklebust out:
110361e930a9STrond Myklebust 	return req;
11041da177e4SLinus Torvalds }
11051da177e4SLinus Torvalds 
1106e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
1107e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
1108e7d39069STrond Myklebust {
1109e7d39069STrond Myklebust 	struct nfs_page	*req;
1110e7d39069STrond Myklebust 
1111e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
1112e7d39069STrond Myklebust 	if (IS_ERR(req))
1113e7d39069STrond Myklebust 		return PTR_ERR(req);
1114e7d39069STrond Myklebust 	/* Update file length */
1115e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
1116d72ddcbaSWeston Andros Adamson 	nfs_mark_uptodate(req);
1117a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
11181d1afcbcSTrond Myklebust 	nfs_unlock_and_release_request(req);
1119e7d39069STrond Myklebust 	return 0;
1120e7d39069STrond Myklebust }
1121e7d39069STrond Myklebust 
11221da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
11231da177e4SLinus Torvalds {
1124cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
11252a369153STrond Myklebust 	struct nfs_lock_context *l_ctx;
11261da177e4SLinus Torvalds 	struct nfs_page	*req;
11271a54533eSTrond Myklebust 	int do_flush, status;
11281da177e4SLinus Torvalds 	/*
11291da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
11301da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
11311da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
11321da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
11331da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
11341da177e4SLinus Torvalds 	 * dropped page.
11351da177e4SLinus Torvalds 	 */
11361a54533eSTrond Myklebust 	do {
113784d3a9a9SWeston Andros Adamson 		req = nfs_page_find_head_request(page);
11381a54533eSTrond Myklebust 		if (req == NULL)
11391a54533eSTrond Myklebust 			return 0;
11402a369153STrond Myklebust 		l_ctx = req->wb_lock_context;
11412a369153STrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx;
11422bfc6e56SWeston Andros Adamson 		/* for now, flush if more than 1 request in page_group */
11432bfc6e56SWeston Andros Adamson 		do_flush |= req->wb_this_page != req;
11440f1d2605STrond Myklebust 		if (l_ctx && ctx->dentry->d_inode->i_flock != NULL) {
11452a369153STrond Myklebust 			do_flush |= l_ctx->lockowner.l_owner != current->files
11462a369153STrond Myklebust 				|| l_ctx->lockowner.l_pid != current->tgid;
11472a369153STrond Myklebust 		}
11481da177e4SLinus Torvalds 		nfs_release_request(req);
11491a54533eSTrond Myklebust 		if (!do_flush)
11501a54533eSTrond Myklebust 			return 0;
1151d56b4ddfSMel Gorman 		status = nfs_wb_page(page_file_mapping(page)->host, page);
11521a54533eSTrond Myklebust 	} while (status == 0);
11531a54533eSTrond Myklebust 	return status;
11541da177e4SLinus Torvalds }
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds /*
1157dc24826bSAndy Adamson  * Avoid buffered writes when a open context credential's key would
1158dc24826bSAndy Adamson  * expire soon.
1159dc24826bSAndy Adamson  *
1160dc24826bSAndy Adamson  * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
1161dc24826bSAndy Adamson  *
1162dc24826bSAndy Adamson  * Return 0 and set a credential flag which triggers the inode to flush
1163dc24826bSAndy Adamson  * and performs  NFS_FILE_SYNC writes if the key will expired within
1164dc24826bSAndy Adamson  * RPC_KEY_EXPIRE_TIMEO.
1165dc24826bSAndy Adamson  */
1166dc24826bSAndy Adamson int
1167dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode)
1168dc24826bSAndy Adamson {
1169dc24826bSAndy Adamson 	struct nfs_open_context *ctx = nfs_file_open_context(filp);
1170dc24826bSAndy Adamson 	struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
1171dc24826bSAndy Adamson 
1172dc24826bSAndy Adamson 	return rpcauth_key_timeout_notify(auth, ctx->cred);
1173dc24826bSAndy Adamson }
1174dc24826bSAndy Adamson 
1175dc24826bSAndy Adamson /*
1176dc24826bSAndy Adamson  * Test if the open context credential key is marked to expire soon.
1177dc24826bSAndy Adamson  */
1178dc24826bSAndy Adamson bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx)
1179dc24826bSAndy Adamson {
1180dc24826bSAndy Adamson 	return rpcauth_cred_key_to_expire(ctx->cred);
1181dc24826bSAndy Adamson }
1182dc24826bSAndy Adamson 
1183dc24826bSAndy Adamson /*
11845d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
11855d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
11865d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
11875d47a356STrond Myklebust  */
11888d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
11895d47a356STrond Myklebust {
1190d529ef83SJeff Layton 	struct nfs_inode *nfsi = NFS_I(inode);
1191d529ef83SJeff Layton 
11928d197a56STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
11938d197a56STrond Myklebust 		goto out;
119418dd78c4SScott Mayhew 	if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
1195d529ef83SJeff Layton 		return false;
11964db72b40SJeff Layton 	smp_rmb();
1197d529ef83SJeff Layton 	if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags))
11988d197a56STrond Myklebust 		return false;
11998d197a56STrond Myklebust out:
120018dd78c4SScott Mayhew 	if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
120118dd78c4SScott Mayhew 		return false;
12028d197a56STrond Myklebust 	return PageUptodate(page) != 0;
12035d47a356STrond Myklebust }
12045d47a356STrond Myklebust 
1205c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or
1206c7559663SScott Mayhew  * if we have the whole file locked for writing), it may be more efficient to
1207c7559663SScott Mayhew  * extend the write to cover the entire page in order to avoid fragmentation
1208c7559663SScott Mayhew  * inefficiencies.
1209c7559663SScott Mayhew  *
1210263b4509SScott Mayhew  * If the file is opened for synchronous writes then we can just skip the rest
1211263b4509SScott Mayhew  * of the checks.
1212c7559663SScott Mayhew  */
1213c7559663SScott Mayhew static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode)
1214c7559663SScott Mayhew {
1215c7559663SScott Mayhew 	if (file->f_flags & O_DSYNC)
1216c7559663SScott Mayhew 		return 0;
1217263b4509SScott Mayhew 	if (!nfs_write_pageuptodate(page, inode))
1218263b4509SScott Mayhew 		return 0;
1219c7559663SScott Mayhew 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
1220c7559663SScott Mayhew 		return 1;
1221263b4509SScott Mayhew 	if (inode->i_flock == NULL || (inode->i_flock->fl_start == 0 &&
1222c7559663SScott Mayhew 			inode->i_flock->fl_end == OFFSET_MAX &&
1223263b4509SScott Mayhew 			inode->i_flock->fl_type != F_RDLCK))
1224c7559663SScott Mayhew 		return 1;
1225c7559663SScott Mayhew 	return 0;
1226c7559663SScott Mayhew }
1227c7559663SScott Mayhew 
12285d47a356STrond Myklebust /*
12291da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
12301da177e4SLinus Torvalds  *
12311da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
12321da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
12331da177e4SLinus Torvalds  */
12341da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
12351da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
12361da177e4SLinus Torvalds {
1237cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
1238d56b4ddfSMel Gorman 	struct inode	*inode = page_file_mapping(page)->host;
12391da177e4SLinus Torvalds 	int		status = 0;
12401da177e4SLinus Torvalds 
124191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
124291d5b470SChuck Lever 
12436de1472fSAl Viro 	dprintk("NFS:       nfs_updatepage(%pD2 %d@%lld)\n",
12446de1472fSAl Viro 		file, count, (long long)(page_file_offset(page) + offset));
12451da177e4SLinus Torvalds 
1246c7559663SScott Mayhew 	if (nfs_can_extend_write(file, page, inode)) {
124749a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
12481da177e4SLinus Torvalds 		offset = 0;
12491da177e4SLinus Torvalds 	}
12501da177e4SLinus Torvalds 
1251e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
125203fa9e84STrond Myklebust 	if (status < 0)
125303fa9e84STrond Myklebust 		nfs_set_pageerror(page);
125459b7c05fSTrond Myklebust 	else
125559b7c05fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
12561da177e4SLinus Torvalds 
125748186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
12581da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
12591da177e4SLinus Torvalds 	return status;
12601da177e4SLinus Torvalds }
12611da177e4SLinus Torvalds 
12623ff7576dSTrond Myklebust static int flush_task_priority(int how)
12631da177e4SLinus Torvalds {
12641da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
12651da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
12661da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
12671da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
12681da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
12691da177e4SLinus Torvalds 	}
12701da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
12711da177e4SLinus Torvalds }
12721da177e4SLinus Torvalds 
1273d45f60c6SWeston Andros Adamson static void nfs_initiate_write(struct nfs_pgio_header *hdr,
1274d45f60c6SWeston Andros Adamson 			       struct rpc_message *msg,
12751ed26f33SAnna Schumaker 			       struct rpc_task_setup *task_setup_data, int how)
12761da177e4SLinus Torvalds {
1277d45f60c6SWeston Andros Adamson 	struct inode *inode = hdr->inode;
12783ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
12791da177e4SLinus Torvalds 
12801ed26f33SAnna Schumaker 	task_setup_data->priority = priority;
1281d45f60c6SWeston Andros Adamson 	NFS_PROTO(inode)->write_setup(hdr, msg);
1282d138d5d1SAndy Adamson 
12838c21c62cSWeston Andros Adamson 	nfs4_state_protect_write(NFS_SERVER(inode)->nfs_client,
1284d45f60c6SWeston Andros Adamson 				 &task_setup_data->rpc_client, msg, hdr);
1285275acaafSTrond Myklebust }
1286275acaafSTrond Myklebust 
12876d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
12886d884e8fSFred  * call this on each, which will prepare them to be retried on next
12896d884e8fSFred  * writeback using standard nfs.
12906d884e8fSFred  */
12916d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
12926d884e8fSFred {
12936d884e8fSFred 	nfs_mark_request_dirty(req);
12941d1afcbcSTrond Myklebust 	nfs_unlock_request(req);
129520633f04SWeston Andros Adamson 	nfs_end_page_writeback(req);
12963aff4ebbSTrond Myklebust 	nfs_release_request(req);
12976d884e8fSFred }
12986d884e8fSFred 
1299061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head)
13006c75dc0dSFred Isaman {
13016c75dc0dSFred Isaman 	struct nfs_page	*req;
13026c75dc0dSFred Isaman 
13036c75dc0dSFred Isaman 	while (!list_empty(head)) {
13046c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
13056c75dc0dSFred Isaman 		nfs_list_remove_request(req);
13066c75dc0dSFred Isaman 		nfs_redirty_request(req);
13076c75dc0dSFred Isaman 	}
13086c75dc0dSFred Isaman }
13096c75dc0dSFred Isaman 
1310061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1311061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1312061ae2edSFred Isaman 	.completion = nfs_write_completion,
1313061ae2edSFred Isaman };
1314061ae2edSFred Isaman 
131557208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1316a20c93e3SChristoph Hellwig 			       struct inode *inode, int ioflags, bool force_mds,
1317061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
13181751c363STrond Myklebust {
1319a20c93e3SChristoph Hellwig 	struct nfs_server *server = NFS_SERVER(inode);
132041d8d5b7SAnna Schumaker 	const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
1321a20c93e3SChristoph Hellwig 
1322a20c93e3SChristoph Hellwig #ifdef CONFIG_NFS_V4_1
1323a20c93e3SChristoph Hellwig 	if (server->pnfs_curr_ld && !force_mds)
1324a20c93e3SChristoph Hellwig 		pg_ops = server->pnfs_curr_ld->pg_write_ops;
1325a20c93e3SChristoph Hellwig #endif
13264a0de55cSAnna Schumaker 	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
13274a0de55cSAnna Schumaker 			server->wsize, ioflags);
13281751c363STrond Myklebust }
1329ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
13301751c363STrond Myklebust 
1331dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1332dce81290STrond Myklebust {
133341d8d5b7SAnna Schumaker 	pgio->pg_ops = &nfs_pgio_rw_ops;
1334dce81290STrond Myklebust 	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1335dce81290STrond Myklebust }
13361f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1337dce81290STrond Myklebust 
13381da177e4SLinus Torvalds 
13390b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
13400b7c0153SFred Isaman {
13410b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
13420b7c0153SFred Isaman 
13430b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
13440b7c0153SFred Isaman }
13450b7c0153SFred Isaman 
1346d45f60c6SWeston Andros Adamson static void nfs_writeback_release_common(struct nfs_pgio_header *hdr)
13471da177e4SLinus Torvalds {
1348c65e6254SWeston Andros Adamson 	/* do nothing! */
13491da177e4SLinus Torvalds }
13501da177e4SLinus Torvalds 
13511f2edbe3STrond Myklebust /*
13521f2edbe3STrond Myklebust  * Special version of should_remove_suid() that ignores capabilities.
13531f2edbe3STrond Myklebust  */
13541f2edbe3STrond Myklebust static int nfs_should_remove_suid(const struct inode *inode)
13551f2edbe3STrond Myklebust {
13561f2edbe3STrond Myklebust 	umode_t mode = inode->i_mode;
13571f2edbe3STrond Myklebust 	int kill = 0;
1358788e7a89STrond Myklebust 
13591f2edbe3STrond Myklebust 	/* suid always must be killed */
13601f2edbe3STrond Myklebust 	if (unlikely(mode & S_ISUID))
13611f2edbe3STrond Myklebust 		kill = ATTR_KILL_SUID;
13621f2edbe3STrond Myklebust 
13631f2edbe3STrond Myklebust 	/*
13641f2edbe3STrond Myklebust 	 * sgid without any exec bits is just a mandatory locking mark; leave
13651f2edbe3STrond Myklebust 	 * it alone.  If some exec bits are set, it's a real sgid; kill it.
13661f2edbe3STrond Myklebust 	 */
13671f2edbe3STrond Myklebust 	if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
13681f2edbe3STrond Myklebust 		kill |= ATTR_KILL_SGID;
13691f2edbe3STrond Myklebust 
13701f2edbe3STrond Myklebust 	if (unlikely(kill && S_ISREG(mode)))
13711f2edbe3STrond Myklebust 		return kill;
13721f2edbe3STrond Myklebust 
13731f2edbe3STrond Myklebust 	return 0;
13741f2edbe3STrond Myklebust }
1375788e7a89STrond Myklebust 
13761da177e4SLinus Torvalds /*
13771da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
13781da177e4SLinus Torvalds  */
1379d45f60c6SWeston Andros Adamson static int nfs_writeback_done(struct rpc_task *task,
1380d45f60c6SWeston Andros Adamson 			      struct nfs_pgio_header *hdr,
13810eecb214SAnna Schumaker 			      struct inode *inode)
13821da177e4SLinus Torvalds {
1383788e7a89STrond Myklebust 	int status;
13841da177e4SLinus Torvalds 
1385f551e44fSChuck Lever 	/*
1386f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1387f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1388f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1389f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1390f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1391f551e44fSChuck Lever 	 */
1392d45f60c6SWeston Andros Adamson 	status = NFS_PROTO(inode)->write_done(task, hdr);
1393788e7a89STrond Myklebust 	if (status != 0)
13940eecb214SAnna Schumaker 		return status;
1395d45f60c6SWeston Andros Adamson 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count);
139691d5b470SChuck Lever 
139789d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
1398d45f60c6SWeston Andros Adamson 	if (hdr->res.verf->committed < hdr->args.stable &&
1399d45f60c6SWeston Andros Adamson 	    task->tk_status >= 0) {
14001da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
14011da177e4SLinus Torvalds 		 * commit data to stable storage even though we
14021da177e4SLinus Torvalds 		 * requested it.
14031da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
14041da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
14051da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
14061da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
14071da177e4SLinus Torvalds 		 */
14081da177e4SLinus Torvalds 		static unsigned long    complain;
14091da177e4SLinus Torvalds 
1410a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
14111da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
14121da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
14131da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1414cd841605SFred Isaman 				NFS_SERVER(inode)->nfs_client->cl_hostname,
1415d45f60c6SWeston Andros Adamson 				hdr->res.verf->committed, hdr->args.stable);
14161da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
14171da177e4SLinus Torvalds 		}
14181da177e4SLinus Torvalds 	}
14191da177e4SLinus Torvalds #endif
14201f2edbe3STrond Myklebust 
14211f2edbe3STrond Myklebust 	/* Deal with the suid/sgid bit corner case */
14221f2edbe3STrond Myklebust 	if (nfs_should_remove_suid(inode))
14231f2edbe3STrond Myklebust 		nfs_mark_for_revalidate(inode);
14240eecb214SAnna Schumaker 	return 0;
14250eecb214SAnna Schumaker }
14260eecb214SAnna Schumaker 
14270eecb214SAnna Schumaker /*
14280eecb214SAnna Schumaker  * This function is called when the WRITE call is complete.
14290eecb214SAnna Schumaker  */
1430d45f60c6SWeston Andros Adamson static void nfs_writeback_result(struct rpc_task *task,
1431d45f60c6SWeston Andros Adamson 				 struct nfs_pgio_header *hdr)
14320eecb214SAnna Schumaker {
1433d45f60c6SWeston Andros Adamson 	struct nfs_pgio_args	*argp = &hdr->args;
1434d45f60c6SWeston Andros Adamson 	struct nfs_pgio_res	*resp = &hdr->res;
14351f2edbe3STrond Myklebust 
14361f2edbe3STrond Myklebust 	if (resp->count < argp->count) {
14371da177e4SLinus Torvalds 		static unsigned long    complain;
14381da177e4SLinus Torvalds 
14396c75dc0dSFred Isaman 		/* This a short write! */
1440d45f60c6SWeston Andros Adamson 		nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE);
144191d5b470SChuck Lever 
14421da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
14436c75dc0dSFred Isaman 		if (resp->count == 0) {
14446c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
14456c75dc0dSFred Isaman 				printk(KERN_WARNING
14466c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
14476c75dc0dSFred Isaman 				       argp->count);
14486c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
14496c75dc0dSFred Isaman 			}
1450d45f60c6SWeston Andros Adamson 			nfs_set_pgio_error(hdr, -EIO, argp->offset);
14516c75dc0dSFred Isaman 			task->tk_status = -EIO;
14526c75dc0dSFred Isaman 			return;
14536c75dc0dSFred Isaman 		}
14541da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
14551da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
14561da177e4SLinus Torvalds 			/* Resend from where the server left off */
1457d45f60c6SWeston Andros Adamson 			hdr->mds_offset += resp->count;
14581da177e4SLinus Torvalds 			argp->offset += resp->count;
14591da177e4SLinus Torvalds 			argp->pgbase += resp->count;
14601da177e4SLinus Torvalds 			argp->count -= resp->count;
14611da177e4SLinus Torvalds 		} else {
14621da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
14631da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
14641da177e4SLinus Torvalds 			 */
14651da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
14661da177e4SLinus Torvalds 		}
1467d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
14681da177e4SLinus Torvalds 	}
14691da177e4SLinus Torvalds }
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds 
147289d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
147371d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
147471d0a611STrond Myklebust {
1475b8413f98STrond Myklebust 	int ret;
1476b8413f98STrond Myklebust 
147771d0a611STrond Myklebust 	if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
147871d0a611STrond Myklebust 		return 1;
1479b8413f98STrond Myklebust 	if (!may_wait)
148071d0a611STrond Myklebust 		return 0;
1481b8413f98STrond Myklebust 	ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1482b8413f98STrond Myklebust 				NFS_INO_COMMIT,
1483b8413f98STrond Myklebust 				nfs_wait_bit_killable,
1484b8413f98STrond Myklebust 				TASK_KILLABLE);
1485b8413f98STrond Myklebust 	return (ret < 0) ? ret : 1;
148671d0a611STrond Myklebust }
148771d0a611STrond Myklebust 
1488f453a54aSFred Isaman static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
148971d0a611STrond Myklebust {
149071d0a611STrond Myklebust 	clear_bit(NFS_INO_COMMIT, &nfsi->flags);
14914e857c58SPeter Zijlstra 	smp_mb__after_atomic();
149271d0a611STrond Myklebust 	wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
149371d0a611STrond Myklebust }
149471d0a611STrond Myklebust 
14950b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
14961da177e4SLinus Torvalds {
14970b7c0153SFred Isaman 	put_nfs_open_context(data->context);
14980b7c0153SFred Isaman 	nfs_commit_free(data);
14991da177e4SLinus Torvalds }
1500e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
15011da177e4SLinus Torvalds 
15020b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
15039ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
15049f0ec176SAndy Adamson 			int how, int flags)
15051da177e4SLinus Torvalds {
150607737691STrond Myklebust 	struct rpc_task *task;
15079ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1508bdc7f021STrond Myklebust 	struct rpc_message msg = {
1509bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1510bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
15119ace33cdSFred Isaman 		.rpc_cred = data->cred,
1512bdc7f021STrond Myklebust 	};
151384115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
151407737691STrond Myklebust 		.task = &data->task,
15159ace33cdSFred Isaman 		.rpc_client = clnt,
1516bdc7f021STrond Myklebust 		.rpc_message = &msg,
15179ace33cdSFred Isaman 		.callback_ops = call_ops,
151884115e1cSTrond Myklebust 		.callback_data = data,
1519101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
15209f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
15213ff7576dSTrond Myklebust 		.priority = priority,
152284115e1cSTrond Myklebust 	};
1523788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
15249ace33cdSFred Isaman 	NFS_PROTO(data->inode)->commit_setup(data, &msg);
15251da177e4SLinus Torvalds 
1526a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1527bdc7f021STrond Myklebust 
15288c21c62cSWeston Andros Adamson 	nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client,
15298c21c62cSWeston Andros Adamson 		NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg);
15308c21c62cSWeston Andros Adamson 
153107737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1532dbae4c73STrond Myklebust 	if (IS_ERR(task))
1533dbae4c73STrond Myklebust 		return PTR_ERR(task);
1534d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1535d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
153607737691STrond Myklebust 	rpc_put_task(task);
1537dbae4c73STrond Myklebust 	return 0;
15381da177e4SLinus Torvalds }
1539e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
15401da177e4SLinus Torvalds 
15411da177e4SLinus Torvalds /*
15429ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
15439ace33cdSFred Isaman  */
15440b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1545988b6dceSFred Isaman 		     struct list_head *head,
1546f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1547f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
15489ace33cdSFred Isaman {
15499ace33cdSFred Isaman 	struct nfs_page *first = nfs_list_entry(head->next);
15503d4ff43dSAl Viro 	struct inode *inode = first->wb_context->dentry->d_inode;
15519ace33cdSFred Isaman 
15529ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
15539ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
15549ace33cdSFred Isaman 
15559ace33cdSFred Isaman 	list_splice_init(head, &data->pages);
15569ace33cdSFred Isaman 
15579ace33cdSFred Isaman 	data->inode	  = inode;
15589ace33cdSFred Isaman 	data->cred	  = first->wb_context->cred;
1559988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
15609ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1561f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1562b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
15639ace33cdSFred Isaman 
15649ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
15659ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
15669ace33cdSFred Isaman 	data->args.offset = 0;
15679ace33cdSFred Isaman 	data->args.count  = 0;
15680b7c0153SFred Isaman 	data->context     = get_nfs_open_context(first->wb_context);
15699ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
15709ace33cdSFred Isaman 	data->res.verf    = &data->verf;
15719ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
15729ace33cdSFred Isaman }
1573e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
15749ace33cdSFred Isaman 
1575e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1576ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1577ea2cf228SFred Isaman 		      struct nfs_commit_info *cinfo)
157864bfeb49SFred Isaman {
157964bfeb49SFred Isaman 	struct nfs_page *req;
158064bfeb49SFred Isaman 
158164bfeb49SFred Isaman 	while (!list_empty(page_list)) {
158264bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
158364bfeb49SFred Isaman 		nfs_list_remove_request(req);
1584ea2cf228SFred Isaman 		nfs_mark_request_commit(req, lseg, cinfo);
158556f9cd68SFred Isaman 		if (!cinfo->dreq) {
158664bfeb49SFred Isaman 			dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1587d56b4ddfSMel Gorman 			dec_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
158864bfeb49SFred Isaman 				     BDI_RECLAIMABLE);
158956f9cd68SFred Isaman 		}
15901d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
159164bfeb49SFred Isaman 	}
159264bfeb49SFred Isaman }
1593e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
159464bfeb49SFred Isaman 
15959ace33cdSFred Isaman /*
15961da177e4SLinus Torvalds  * Commit dirty pages
15971da177e4SLinus Torvalds  */
15981da177e4SLinus Torvalds static int
1599ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1600ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
16011da177e4SLinus Torvalds {
16020b7c0153SFred Isaman 	struct nfs_commit_data	*data;
16031da177e4SLinus Torvalds 
1604c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds 	if (!data)
16071da177e4SLinus Torvalds 		goto out_bad;
16081da177e4SLinus Torvalds 
16091da177e4SLinus Torvalds 	/* Set up the argument struct */
1610f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
1611f453a54aSFred Isaman 	atomic_inc(&cinfo->mds->rpcs_out);
16129f0ec176SAndy Adamson 	return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops,
16139f0ec176SAndy Adamson 				   how, 0);
16141da177e4SLinus Torvalds  out_bad:
1615ea2cf228SFred Isaman 	nfs_retry_commit(head, NULL, cinfo);
1616f453a54aSFred Isaman 	cinfo->completion_ops->error_cleanup(NFS_I(inode));
16171da177e4SLinus Torvalds 	return -ENOMEM;
16181da177e4SLinus Torvalds }
16191da177e4SLinus Torvalds 
16201da177e4SLinus Torvalds /*
16211da177e4SLinus Torvalds  * COMMIT call returned
16221da177e4SLinus Torvalds  */
1623788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
16241da177e4SLinus Torvalds {
16250b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
16261da177e4SLinus Torvalds 
1627a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
16281da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
16291da177e4SLinus Torvalds 
1630788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1631c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
1632c9d8f89dSTrond Myklebust }
1633c9d8f89dSTrond Myklebust 
1634f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1635c9d8f89dSTrond Myklebust {
1636c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1637c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1638f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
1639788e7a89STrond Myklebust 
16401da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
16411da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
16421da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1643d6d6dc7cSFred Isaman 		nfs_clear_page_commit(req->wb_page);
16441da177e4SLinus Torvalds 
16451e8968c5SNiels de Vos 		dprintk("NFS:       commit (%s/%llu %d@%lld)",
16463d4ff43dSAl Viro 			req->wb_context->dentry->d_sb->s_id,
16471e8968c5SNiels de Vos 			(unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
16481da177e4SLinus Torvalds 			req->wb_bytes,
16491da177e4SLinus Torvalds 			(long long)req_offset(req));
1650c9d8f89dSTrond Myklebust 		if (status < 0) {
1651c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
16521da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1653c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
16541da177e4SLinus Torvalds 			goto next;
16551da177e4SLinus Torvalds 		}
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
16581da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
16592f2c63bcSTrond Myklebust 		if (!memcmp(&req->wb_verf, &data->verf.verifier, sizeof(req->wb_verf))) {
16601da177e4SLinus Torvalds 			/* We have a match */
16611da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
16621da177e4SLinus Torvalds 			dprintk(" OK\n");
16631da177e4SLinus Torvalds 			goto next;
16641da177e4SLinus Torvalds 		}
16651da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
16661da177e4SLinus Torvalds 		dprintk(" mismatch\n");
16676d884e8fSFred 		nfs_mark_request_dirty(req);
166805990d1bSTrond Myklebust 		set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
16691da177e4SLinus Torvalds 	next:
16701d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
16711da177e4SLinus Torvalds 	}
1672f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1673f453a54aSFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
1674f453a54aSFred Isaman 		nfs_commit_clear_lock(NFS_I(data->inode));
16755917ce84SFred Isaman }
16765917ce84SFred Isaman 
16775917ce84SFred Isaman static void nfs_commit_release(void *calldata)
16785917ce84SFred Isaman {
16790b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
16805917ce84SFred Isaman 
1681f453a54aSFred Isaman 	data->completion_ops->completion(data);
1682c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
16831da177e4SLinus Torvalds }
1684788e7a89STrond Myklebust 
1685788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
16860b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1687788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1688788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1689788e7a89STrond Myklebust };
16901da177e4SLinus Torvalds 
1691f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1692f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1693f453a54aSFred Isaman 	.error_cleanup = nfs_commit_clear_lock,
1694f453a54aSFred Isaman };
1695f453a54aSFred Isaman 
16961763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1697ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
169884c53ab5SFred Isaman {
169984c53ab5SFred Isaman 	int status;
170084c53ab5SFred Isaman 
1701ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
170284c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1703ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
170484c53ab5SFred Isaman 	return status;
170584c53ab5SFred Isaman }
170684c53ab5SFred Isaman 
1707b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
17081da177e4SLinus Torvalds {
17091da177e4SLinus Torvalds 	LIST_HEAD(head);
1710ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
171171d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1712b8413f98STrond Myklebust 	int res;
17131da177e4SLinus Torvalds 
1714b8413f98STrond Myklebust 	res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1715b8413f98STrond Myklebust 	if (res <= 0)
1716c5efa5fcSTrond Myklebust 		goto out_mark_dirty;
1717ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1718ea2cf228SFred Isaman 	res = nfs_scan_commit(inode, &head, &cinfo);
17191da177e4SLinus Torvalds 	if (res) {
1720a861a1e1SFred Isaman 		int error;
1721a861a1e1SFred Isaman 
1722ea2cf228SFred Isaman 		error = nfs_generic_commit_list(inode, &head, how, &cinfo);
17231da177e4SLinus Torvalds 		if (error < 0)
17241da177e4SLinus Torvalds 			return error;
1725b8413f98STrond Myklebust 		if (!may_wait)
1726b8413f98STrond Myklebust 			goto out_mark_dirty;
172774316201SNeilBrown 		error = wait_on_bit_action(&NFS_I(inode)->flags,
1728b8413f98STrond Myklebust 				NFS_INO_COMMIT,
172971d0a611STrond Myklebust 				nfs_wait_bit_killable,
173071d0a611STrond Myklebust 				TASK_KILLABLE);
1731b8413f98STrond Myklebust 		if (error < 0)
1732b8413f98STrond Myklebust 			return error;
173371d0a611STrond Myklebust 	} else
173471d0a611STrond Myklebust 		nfs_commit_clear_lock(NFS_I(inode));
1735c5efa5fcSTrond Myklebust 	return res;
1736c5efa5fcSTrond Myklebust 	/* Note: If we exit without ensuring that the commit is complete,
1737c5efa5fcSTrond Myklebust 	 * we must mark the inode as dirty. Otherwise, future calls to
1738c5efa5fcSTrond Myklebust 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1739c5efa5fcSTrond Myklebust 	 * that the data is on the disk.
1740c5efa5fcSTrond Myklebust 	 */
1741c5efa5fcSTrond Myklebust out_mark_dirty:
1742c5efa5fcSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
17431da177e4SLinus Torvalds 	return res;
17441da177e4SLinus Torvalds }
17458fc795f7STrond Myklebust 
17468fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
17478fc795f7STrond Myklebust {
1748420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1749420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1750420e3646STrond Myklebust 	int ret = 0;
17518fc795f7STrond Myklebust 
17523236c3e1SJeff Layton 	/* no commits means nothing needs to be done */
1753ea2cf228SFred Isaman 	if (!nfsi->commit_info.ncommit)
17543236c3e1SJeff Layton 		return ret;
17553236c3e1SJeff Layton 
1756a00dd6c0SJeff Layton 	if (wbc->sync_mode == WB_SYNC_NONE) {
1757a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1758a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1759420e3646STrond Myklebust 		 */
1760ea2cf228SFred Isaman 		if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1))
1761420e3646STrond Myklebust 			goto out_mark_dirty;
1762420e3646STrond Myklebust 
1763a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1764420e3646STrond Myklebust 		flags = 0;
1765a00dd6c0SJeff Layton 	}
1766a00dd6c0SJeff Layton 
1767420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1768420e3646STrond Myklebust 	if (ret >= 0) {
1769420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1770420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1771420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1772420e3646STrond Myklebust 			else
1773420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1774420e3646STrond Myklebust 		}
17758fc795f7STrond Myklebust 		return 0;
1776420e3646STrond Myklebust 	}
1777420e3646STrond Myklebust out_mark_dirty:
17788fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
17798fc795f7STrond Myklebust 	return ret;
17808fc795f7STrond Myklebust }
1781c63c7b05STrond Myklebust #else
17828fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
17838fc795f7STrond Myklebust {
17848fc795f7STrond Myklebust 	return 0;
17858fc795f7STrond Myklebust }
17861da177e4SLinus Torvalds #endif
17871da177e4SLinus Torvalds 
17888fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
17898fc795f7STrond Myklebust {
1790a8d8f02cSBryan Schumaker 	return nfs_commit_unstable_pages(inode, wbc);
1791a8d8f02cSBryan Schumaker }
179289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode);
1793863a3c6cSAndy Adamson 
1794acdc53b2STrond Myklebust /*
1795acdc53b2STrond Myklebust  * flush the inode to disk.
1796acdc53b2STrond Myklebust  */
1797acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
179834901f70STrond Myklebust {
179934901f70STrond Myklebust 	struct writeback_control wbc = {
180072cb77f4STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
180134901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
1802d7fb1207STrond Myklebust 		.range_start = 0,
1803d7fb1207STrond Myklebust 		.range_end = LLONG_MAX,
180434901f70STrond Myklebust 	};
1805f4ce1299STrond Myklebust 	int ret;
180634901f70STrond Myklebust 
1807f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_enter(inode);
1808f4ce1299STrond Myklebust 
1809f4ce1299STrond Myklebust 	ret = sync_inode(inode, &wbc);
1810f4ce1299STrond Myklebust 
1811f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_exit(inode, ret);
1812f4ce1299STrond Myklebust 	return ret;
18131c75950bSTrond Myklebust }
1814ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all);
18151c75950bSTrond Myklebust 
18161b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
18171b3b4a1aSTrond Myklebust {
18181b3b4a1aSTrond Myklebust 	struct nfs_page *req;
18191b3b4a1aSTrond Myklebust 	int ret = 0;
18201b3b4a1aSTrond Myklebust 
1821ba8b06e6STrond Myklebust 	wait_on_page_writeback(page);
18223e217045SWeston Andros Adamson 
18233e217045SWeston Andros Adamson 	/* blocking call to cancel all requests and join to a single (head)
18243e217045SWeston Andros Adamson 	 * request */
18253e217045SWeston Andros Adamson 	req = nfs_lock_and_join_requests(page, false);
18263e217045SWeston Andros Adamson 
18273e217045SWeston Andros Adamson 	if (IS_ERR(req)) {
18283e217045SWeston Andros Adamson 		ret = PTR_ERR(req);
18293e217045SWeston Andros Adamson 	} else if (req) {
18303e217045SWeston Andros Adamson 		/* all requests from this page have been cancelled by
18313e217045SWeston Andros Adamson 		 * nfs_lock_and_join_requests, so just remove the head
18323e217045SWeston Andros Adamson 		 * request from the inode / page_private pointer and
18333e217045SWeston Andros Adamson 		 * release it */
18341b3b4a1aSTrond Myklebust 		nfs_inode_remove_request(req);
18351b3b4a1aSTrond Myklebust 		/*
18361b3b4a1aSTrond Myklebust 		 * In case nfs_inode_remove_request has marked the
18371b3b4a1aSTrond Myklebust 		 * page as being dirty
18381b3b4a1aSTrond Myklebust 		 */
18391b3b4a1aSTrond Myklebust 		cancel_dirty_page(page, PAGE_CACHE_SIZE);
18401d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
18411b3b4a1aSTrond Myklebust 	}
18423e217045SWeston Andros Adamson 
18431b3b4a1aSTrond Myklebust 	return ret;
18441b3b4a1aSTrond Myklebust }
18451b3b4a1aSTrond Myklebust 
18461c75950bSTrond Myklebust /*
18471c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
18481c75950bSTrond Myklebust  */
18491c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
18501c75950bSTrond Myklebust {
185129418aa4SMel Gorman 	loff_t range_start = page_file_offset(page);
18527f2f12d9STrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
18537f2f12d9STrond Myklebust 	struct writeback_control wbc = {
18547f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
18557f2f12d9STrond Myklebust 		.nr_to_write = 0,
18567f2f12d9STrond Myklebust 		.range_start = range_start,
18577f2f12d9STrond Myklebust 		.range_end = range_end,
18587f2f12d9STrond Myklebust 	};
18597f2f12d9STrond Myklebust 	int ret;
18607f2f12d9STrond Myklebust 
1861f4ce1299STrond Myklebust 	trace_nfs_writeback_page_enter(inode);
1862f4ce1299STrond Myklebust 
18630522f6adSTrond Myklebust 	for (;;) {
1864ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
18657f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
18667f2f12d9STrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
18677f2f12d9STrond Myklebust 			if (ret < 0)
18687f2f12d9STrond Myklebust 				goto out_error;
18690522f6adSTrond Myklebust 			continue;
18707f2f12d9STrond Myklebust 		}
1871f4ce1299STrond Myklebust 		ret = 0;
18720522f6adSTrond Myklebust 		if (!PagePrivate(page))
18730522f6adSTrond Myklebust 			break;
18740522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
18757f2f12d9STrond Myklebust 		if (ret < 0)
18767f2f12d9STrond Myklebust 			goto out_error;
18777f2f12d9STrond Myklebust 	}
18787f2f12d9STrond Myklebust out_error:
1879f4ce1299STrond Myklebust 	trace_nfs_writeback_page_exit(inode, ret);
18807f2f12d9STrond Myklebust 	return ret;
18811c75950bSTrond Myklebust }
18821c75950bSTrond Myklebust 
1883074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
1884074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1885a6bc32b8SMel Gorman 		struct page *page, enum migrate_mode mode)
1886074cc1deSTrond Myklebust {
18872da95652SJeff Layton 	/*
18882da95652SJeff Layton 	 * If PagePrivate is set, then the page is currently associated with
18892da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
18902da95652SJeff Layton 	 *
18912da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
18922da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
18932da95652SJeff Layton 	 *        the page lock.
18942da95652SJeff Layton 	 */
18952da95652SJeff Layton 	if (PagePrivate(page))
18962da95652SJeff Layton 		return -EBUSY;
1897074cc1deSTrond Myklebust 
18988c209ce7SDavid Howells 	if (!nfs_fscache_release_page(page, GFP_KERNEL))
18998c209ce7SDavid Howells 		return -EBUSY;
1900074cc1deSTrond Myklebust 
1901a6bc32b8SMel Gorman 	return migrate_page(mapping, newpage, page, mode);
1902074cc1deSTrond Myklebust }
1903074cc1deSTrond Myklebust #endif
1904074cc1deSTrond Myklebust 
1905f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
19061da177e4SLinus Torvalds {
19071da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
19081e7f3a48SWeston Andros Adamson 					     sizeof(struct nfs_pgio_header),
19091da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
191020c2df83SPaul Mundt 					     NULL);
19111da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
19121da177e4SLinus Torvalds 		return -ENOMEM;
19131da177e4SLinus Torvalds 
191493d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
19151da177e4SLinus Torvalds 						     nfs_wdata_cachep);
19161da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
19173dd4765fSJeff Layton 		goto out_destroy_write_cache;
19181da177e4SLinus Torvalds 
19190b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
19200b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
19210b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
19220b7c0153SFred Isaman 					     NULL);
19230b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
19243dd4765fSJeff Layton 		goto out_destroy_write_mempool;
19250b7c0153SFred Isaman 
192693d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
19274c100210SYanchuan Nian 						      nfs_cdata_cachep);
19281da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
19293dd4765fSJeff Layton 		goto out_destroy_commit_cache;
19301da177e4SLinus Torvalds 
193189a09141SPeter Zijlstra 	/*
193289a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
193389a09141SPeter Zijlstra 	 *
193489a09141SPeter Zijlstra 	 *  64MB:    8192k
193589a09141SPeter Zijlstra 	 * 128MB:   11585k
193689a09141SPeter Zijlstra 	 * 256MB:   16384k
193789a09141SPeter Zijlstra 	 * 512MB:   23170k
193889a09141SPeter Zijlstra 	 *   1GB:   32768k
193989a09141SPeter Zijlstra 	 *   2GB:   46340k
194089a09141SPeter Zijlstra 	 *   4GB:   65536k
194189a09141SPeter Zijlstra 	 *   8GB:   92681k
194289a09141SPeter Zijlstra 	 *  16GB:  131072k
194389a09141SPeter Zijlstra 	 *
194489a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
194589a09141SPeter Zijlstra 	 * Limit the default to 256M
194689a09141SPeter Zijlstra 	 */
194789a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
194889a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
194989a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
195089a09141SPeter Zijlstra 
19511da177e4SLinus Torvalds 	return 0;
19523dd4765fSJeff Layton 
19533dd4765fSJeff Layton out_destroy_commit_cache:
19543dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
19553dd4765fSJeff Layton out_destroy_write_mempool:
19563dd4765fSJeff Layton 	mempool_destroy(nfs_wdata_mempool);
19573dd4765fSJeff Layton out_destroy_write_cache:
19583dd4765fSJeff Layton 	kmem_cache_destroy(nfs_wdata_cachep);
19593dd4765fSJeff Layton 	return -ENOMEM;
19601da177e4SLinus Torvalds }
19611da177e4SLinus Torvalds 
1962266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
19631da177e4SLinus Torvalds {
19641da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
19653dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
19661da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
19671a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
19681da177e4SLinus Torvalds }
19691da177e4SLinus Torvalds 
19704a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops = {
1971a4cdda59SAnna Schumaker 	.rw_mode		= FMODE_WRITE,
19724a0de55cSAnna Schumaker 	.rw_alloc_header	= nfs_writehdr_alloc,
19734a0de55cSAnna Schumaker 	.rw_free_header		= nfs_writehdr_free,
1974a4cdda59SAnna Schumaker 	.rw_release		= nfs_writeback_release_common,
19750eecb214SAnna Schumaker 	.rw_done		= nfs_writeback_done,
19760eecb214SAnna Schumaker 	.rw_result		= nfs_writeback_result,
19771ed26f33SAnna Schumaker 	.rw_initiate		= nfs_initiate_write,
19784a0de55cSAnna Schumaker };
1979