xref: /linux/fs/nfs/write.c (revision d458138353726ea6dcbc53ae3597e489d0432c25)
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;
49*d4581383SWeston Andros Adamson static void nfs_clear_request_commit(struct nfs_page *req);
501da177e4SLinus Torvalds 
51e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep;
523feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
530b7c0153SFred Isaman static struct kmem_cache *nfs_cdata_cachep;
541da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
551da177e4SLinus Torvalds 
560b7c0153SFred Isaman struct nfs_commit_data *nfs_commitdata_alloc(void)
571da177e4SLinus Torvalds {
58192e501bSMel Gorman 	struct nfs_commit_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOIO);
5940859d7eSChuck Lever 
601da177e4SLinus Torvalds 	if (p) {
611da177e4SLinus Torvalds 		memset(p, 0, sizeof(*p));
621da177e4SLinus Torvalds 		INIT_LIST_HEAD(&p->pages);
631da177e4SLinus Torvalds 	}
641da177e4SLinus Torvalds 	return p;
651da177e4SLinus Torvalds }
66e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
671da177e4SLinus Torvalds 
680b7c0153SFred Isaman void nfs_commit_free(struct nfs_commit_data *p)
691da177e4SLinus Torvalds {
701da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
711da177e4SLinus Torvalds }
72e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free);
731da177e4SLinus Torvalds 
744a0de55cSAnna Schumaker static struct nfs_rw_header *nfs_writehdr_alloc(void)
753feb2d49STrond Myklebust {
76c0752cdfSAnna Schumaker 	struct nfs_rw_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOIO);
773feb2d49STrond Myklebust 
784a0de55cSAnna Schumaker 	if (p)
793feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
803feb2d49STrond Myklebust 	return p;
813feb2d49STrond Myklebust }
823feb2d49STrond Myklebust 
834a0de55cSAnna Schumaker static void nfs_writehdr_free(struct nfs_rw_header *whdr)
846c75dc0dSFred Isaman {
85cd841605SFred Isaman 	mempool_free(whdr, nfs_wdata_mempool);
863feb2d49STrond Myklebust }
871da177e4SLinus Torvalds 
887b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
897b159fc1STrond Myklebust {
907b159fc1STrond Myklebust 	ctx->error = error;
917b159fc1STrond Myklebust 	smp_wmb();
927b159fc1STrond Myklebust 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
937b159fc1STrond Myklebust }
947b159fc1STrond Myklebust 
9584d3a9a9SWeston Andros Adamson /*
9684d3a9a9SWeston Andros Adamson  * nfs_page_find_head_request_locked - find head request associated with @page
9784d3a9a9SWeston Andros Adamson  *
9884d3a9a9SWeston Andros Adamson  * must be called while holding the inode lock.
9984d3a9a9SWeston Andros Adamson  *
10084d3a9a9SWeston Andros Adamson  * returns matching head request with reference held, or NULL if not found.
10184d3a9a9SWeston Andros Adamson  */
10229418aa4SMel Gorman static struct nfs_page *
10384d3a9a9SWeston Andros Adamson nfs_page_find_head_request_locked(struct nfs_inode *nfsi, struct page *page)
104277459d2STrond Myklebust {
105277459d2STrond Myklebust 	struct nfs_page *req = NULL;
106277459d2STrond Myklebust 
10729418aa4SMel Gorman 	if (PagePrivate(page))
108277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
10929418aa4SMel Gorman 	else if (unlikely(PageSwapCache(page))) {
11029418aa4SMel Gorman 		struct nfs_page *freq, *t;
11129418aa4SMel Gorman 
11229418aa4SMel Gorman 		/* Linearly search the commit list for the correct req */
11329418aa4SMel Gorman 		list_for_each_entry_safe(freq, t, &nfsi->commit_info.list, wb_list) {
11429418aa4SMel Gorman 			if (freq->wb_page == page) {
11584d3a9a9SWeston Andros Adamson 				req = freq->wb_head;
11629418aa4SMel Gorman 				break;
117277459d2STrond Myklebust 			}
11829418aa4SMel Gorman 		}
11929418aa4SMel Gorman 	}
12029418aa4SMel Gorman 
12184d3a9a9SWeston Andros Adamson 	if (req) {
12284d3a9a9SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_head != req);
12384d3a9a9SWeston Andros Adamson 
12429418aa4SMel Gorman 		kref_get(&req->wb_kref);
12584d3a9a9SWeston Andros Adamson 	}
12629418aa4SMel Gorman 
127277459d2STrond Myklebust 	return req;
128277459d2STrond Myklebust }
129277459d2STrond Myklebust 
13084d3a9a9SWeston Andros Adamson /*
13184d3a9a9SWeston Andros Adamson  * nfs_page_find_head_request - find head request associated with @page
13284d3a9a9SWeston Andros Adamson  *
13384d3a9a9SWeston Andros Adamson  * returns matching head request with reference held, or NULL if not found.
13484d3a9a9SWeston Andros Adamson  */
13584d3a9a9SWeston Andros Adamson static struct nfs_page *nfs_page_find_head_request(struct page *page)
136277459d2STrond Myklebust {
137d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
138277459d2STrond Myklebust 	struct nfs_page *req = NULL;
139277459d2STrond Myklebust 
140587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
14184d3a9a9SWeston Andros Adamson 	req = nfs_page_find_head_request_locked(NFS_I(inode), page);
142587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
143277459d2STrond Myklebust 	return req;
144277459d2STrond Myklebust }
145277459d2STrond Myklebust 
1461da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1471da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1481da177e4SLinus Torvalds {
149d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
150a3d01454STrond Myklebust 	loff_t end, i_size;
151a3d01454STrond Myklebust 	pgoff_t end_index;
1521da177e4SLinus Torvalds 
153a3d01454STrond Myklebust 	spin_lock(&inode->i_lock);
154a3d01454STrond Myklebust 	i_size = i_size_read(inode);
155a3d01454STrond Myklebust 	end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
156d56b4ddfSMel Gorman 	if (i_size > 0 && page_file_index(page) < end_index)
157a3d01454STrond Myklebust 		goto out;
158d56b4ddfSMel Gorman 	end = page_file_offset(page) + ((loff_t)offset+count);
1591da177e4SLinus Torvalds 	if (i_size >= end)
160a3d01454STrond Myklebust 		goto out;
1611da177e4SLinus Torvalds 	i_size_write(inode, end);
162a3d01454STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
163a3d01454STrond Myklebust out:
164a3d01454STrond Myklebust 	spin_unlock(&inode->i_lock);
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds 
167a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
168a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
169a301b777STrond Myklebust {
170d56b4ddfSMel Gorman 	nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page));
171a301b777STrond Myklebust }
172a301b777STrond Myklebust 
173d72ddcbaSWeston Andros Adamson /*
174d72ddcbaSWeston Andros Adamson  * nfs_page_group_search_locked
175d72ddcbaSWeston Andros Adamson  * @head - head request of page group
176d72ddcbaSWeston Andros Adamson  * @page_offset - offset into page
177d72ddcbaSWeston Andros Adamson  *
178d72ddcbaSWeston Andros Adamson  * Search page group with head @head to find a request that contains the
179d72ddcbaSWeston Andros Adamson  * page offset @page_offset.
180d72ddcbaSWeston Andros Adamson  *
181d72ddcbaSWeston Andros Adamson  * Returns a pointer to the first matching nfs request, or NULL if no
182d72ddcbaSWeston Andros Adamson  * match is found.
183d72ddcbaSWeston Andros Adamson  *
184d72ddcbaSWeston Andros Adamson  * Must be called with the page group lock held
185d72ddcbaSWeston Andros Adamson  */
186d72ddcbaSWeston Andros Adamson static struct nfs_page *
187d72ddcbaSWeston Andros Adamson nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset)
188d72ddcbaSWeston Andros Adamson {
189d72ddcbaSWeston Andros Adamson 	struct nfs_page *req;
190d72ddcbaSWeston Andros Adamson 
191d72ddcbaSWeston Andros Adamson 	WARN_ON_ONCE(head != head->wb_head);
192d72ddcbaSWeston Andros Adamson 	WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_head->wb_flags));
193d72ddcbaSWeston Andros Adamson 
194d72ddcbaSWeston Andros Adamson 	req = head;
195d72ddcbaSWeston Andros Adamson 	do {
196d72ddcbaSWeston Andros Adamson 		if (page_offset >= req->wb_pgbase &&
197d72ddcbaSWeston Andros Adamson 		    page_offset < (req->wb_pgbase + req->wb_bytes))
198d72ddcbaSWeston Andros Adamson 			return req;
199d72ddcbaSWeston Andros Adamson 
200d72ddcbaSWeston Andros Adamson 		req = req->wb_this_page;
201d72ddcbaSWeston Andros Adamson 	} while (req != head);
202d72ddcbaSWeston Andros Adamson 
203d72ddcbaSWeston Andros Adamson 	return NULL;
204d72ddcbaSWeston Andros Adamson }
205d72ddcbaSWeston Andros Adamson 
206d72ddcbaSWeston Andros Adamson /*
207d72ddcbaSWeston Andros Adamson  * nfs_page_group_covers_page
208d72ddcbaSWeston Andros Adamson  * @head - head request of page group
209d72ddcbaSWeston Andros Adamson  *
210d72ddcbaSWeston Andros Adamson  * Return true if the page group with head @head covers the whole page,
211d72ddcbaSWeston Andros Adamson  * returns false otherwise
212d72ddcbaSWeston Andros Adamson  */
213d72ddcbaSWeston Andros Adamson static bool nfs_page_group_covers_page(struct nfs_page *req)
214d72ddcbaSWeston Andros Adamson {
215d72ddcbaSWeston Andros Adamson 	struct nfs_page *tmp;
216d72ddcbaSWeston Andros Adamson 	unsigned int pos = 0;
217d72ddcbaSWeston Andros Adamson 	unsigned int len = nfs_page_length(req->wb_page);
218d72ddcbaSWeston Andros Adamson 
219d72ddcbaSWeston Andros Adamson 	nfs_page_group_lock(req);
220d72ddcbaSWeston Andros Adamson 
221d72ddcbaSWeston Andros Adamson 	do {
222d72ddcbaSWeston Andros Adamson 		tmp = nfs_page_group_search_locked(req->wb_head, pos);
223d72ddcbaSWeston Andros Adamson 		if (tmp) {
224d72ddcbaSWeston Andros Adamson 			/* no way this should happen */
225d72ddcbaSWeston Andros Adamson 			WARN_ON_ONCE(tmp->wb_pgbase != pos);
226d72ddcbaSWeston Andros Adamson 			pos += tmp->wb_bytes - (pos - tmp->wb_pgbase);
227d72ddcbaSWeston Andros Adamson 		}
228d72ddcbaSWeston Andros Adamson 	} while (tmp && pos < len);
229d72ddcbaSWeston Andros Adamson 
230d72ddcbaSWeston Andros Adamson 	nfs_page_group_unlock(req);
231d72ddcbaSWeston Andros Adamson 	WARN_ON_ONCE(pos > len);
232d72ddcbaSWeston Andros Adamson 	return pos == len;
233d72ddcbaSWeston Andros Adamson }
234d72ddcbaSWeston Andros Adamson 
2351da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
2361da177e4SLinus Torvalds  * covers the full page.
2371da177e4SLinus Torvalds  */
238d72ddcbaSWeston Andros Adamson static void nfs_mark_uptodate(struct nfs_page *req)
2391da177e4SLinus Torvalds {
240d72ddcbaSWeston Andros Adamson 	if (PageUptodate(req->wb_page))
2411da177e4SLinus Torvalds 		return;
242d72ddcbaSWeston Andros Adamson 	if (!nfs_page_group_covers_page(req))
2431da177e4SLinus Torvalds 		return;
244d72ddcbaSWeston Andros Adamson 	SetPageUptodate(req->wb_page);
2451da177e4SLinus Torvalds }
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
2481da177e4SLinus Torvalds {
2491da177e4SLinus Torvalds 	if (wbc->for_reclaim)
250c63c7b05STrond Myklebust 		return FLUSH_HIGHPRI | FLUSH_STABLE;
251b17621feSWu Fengguang 	if (wbc->for_kupdate || wbc->for_background)
252b31268acSTrond Myklebust 		return FLUSH_LOWPRI | FLUSH_COND_STABLE;
253b31268acSTrond Myklebust 	return FLUSH_COND_STABLE;
2541da177e4SLinus Torvalds }
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds /*
25789a09141SPeter Zijlstra  * NFS congestion control
25889a09141SPeter Zijlstra  */
25989a09141SPeter Zijlstra 
26089a09141SPeter Zijlstra int nfs_congestion_kb;
26189a09141SPeter Zijlstra 
26289a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
26389a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
26489a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
26589a09141SPeter Zijlstra 
266deed85e7STrond Myklebust static void nfs_set_page_writeback(struct page *page)
26789a09141SPeter Zijlstra {
268deed85e7STrond Myklebust 	struct nfs_server *nfss = NFS_SERVER(page_file_mapping(page)->host);
2695a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
2705a6d41b3STrond Myklebust 
271deed85e7STrond Myklebust 	WARN_ON_ONCE(ret != 0);
27289a09141SPeter Zijlstra 
273277866a0SPeter Zijlstra 	if (atomic_long_inc_return(&nfss->writeback) >
2748aa7e847SJens Axboe 			NFS_CONGESTION_ON_THRESH) {
2758aa7e847SJens Axboe 		set_bdi_congested(&nfss->backing_dev_info,
2768aa7e847SJens Axboe 					BLK_RW_ASYNC);
2778aa7e847SJens Axboe 	}
27889a09141SPeter Zijlstra }
27989a09141SPeter Zijlstra 
28020633f04SWeston Andros Adamson static void nfs_end_page_writeback(struct nfs_page *req)
28189a09141SPeter Zijlstra {
28220633f04SWeston Andros Adamson 	struct inode *inode = page_file_mapping(req->wb_page)->host;
28389a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
28489a09141SPeter Zijlstra 
28520633f04SWeston Andros Adamson 	if (!nfs_page_group_sync_on_bit(req, PG_WB_END))
28620633f04SWeston Andros Adamson 		return;
28720633f04SWeston Andros Adamson 
28820633f04SWeston Andros Adamson 	end_page_writeback(req->wb_page);
289c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
2908aa7e847SJens Axboe 		clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
29189a09141SPeter Zijlstra }
29289a09141SPeter Zijlstra 
293*d4581383SWeston Andros Adamson 
294*d4581383SWeston Andros Adamson /* nfs_page_group_clear_bits
295*d4581383SWeston Andros Adamson  *   @req - an nfs request
296*d4581383SWeston Andros Adamson  * clears all page group related bits from @req
297*d4581383SWeston Andros Adamson  */
298*d4581383SWeston Andros Adamson static void
299*d4581383SWeston Andros Adamson nfs_page_group_clear_bits(struct nfs_page *req)
300e261f51fSTrond Myklebust {
301*d4581383SWeston Andros Adamson 	clear_bit(PG_TEARDOWN, &req->wb_flags);
302*d4581383SWeston Andros Adamson 	clear_bit(PG_UNLOCKPAGE, &req->wb_flags);
303*d4581383SWeston Andros Adamson 	clear_bit(PG_UPTODATE, &req->wb_flags);
304*d4581383SWeston Andros Adamson 	clear_bit(PG_WB_END, &req->wb_flags);
305*d4581383SWeston Andros Adamson 	clear_bit(PG_REMOVE, &req->wb_flags);
306*d4581383SWeston Andros Adamson }
307*d4581383SWeston Andros Adamson 
308*d4581383SWeston Andros Adamson 
309*d4581383SWeston Andros Adamson /*
310*d4581383SWeston Andros Adamson  * nfs_unroll_locks_and_wait -  unlock all newly locked reqs and wait on @req
311*d4581383SWeston Andros Adamson  *
312*d4581383SWeston Andros Adamson  * this is a helper function for nfs_lock_and_join_requests
313*d4581383SWeston Andros Adamson  *
314*d4581383SWeston Andros Adamson  * @inode - inode associated with request page group, must be holding inode lock
315*d4581383SWeston Andros Adamson  * @head  - head request of page group, must be holding head lock
316*d4581383SWeston Andros Adamson  * @req   - request that couldn't lock and needs to wait on the req bit lock
317*d4581383SWeston Andros Adamson  * @nonblock - if true, don't actually wait
318*d4581383SWeston Andros Adamson  *
319*d4581383SWeston Andros Adamson  * NOTE: this must be called holding page_group bit lock and inode spin lock
320*d4581383SWeston Andros Adamson  *       and BOTH will be released before returning.
321*d4581383SWeston Andros Adamson  *
322*d4581383SWeston Andros Adamson  * returns 0 on success, < 0 on error.
323*d4581383SWeston Andros Adamson  */
324*d4581383SWeston Andros Adamson static int
325*d4581383SWeston Andros Adamson nfs_unroll_locks_and_wait(struct inode *inode, struct nfs_page *head,
326*d4581383SWeston Andros Adamson 			  struct nfs_page *req, bool nonblock)
327*d4581383SWeston Andros Adamson 	__releases(&inode->i_lock)
328*d4581383SWeston Andros Adamson {
329*d4581383SWeston Andros Adamson 	struct nfs_page *tmp;
330e261f51fSTrond Myklebust 	int ret;
331e261f51fSTrond Myklebust 
332*d4581383SWeston Andros Adamson 	/* relinquish all the locks successfully grabbed this run */
333*d4581383SWeston Andros Adamson 	for (tmp = head ; tmp != req; tmp = tmp->wb_this_page)
334*d4581383SWeston Andros Adamson 		nfs_unlock_request(tmp);
335*d4581383SWeston Andros Adamson 
336*d4581383SWeston Andros Adamson 	WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
337*d4581383SWeston Andros Adamson 
338*d4581383SWeston Andros Adamson 	/* grab a ref on the request that will be waited on */
339*d4581383SWeston Andros Adamson 	kref_get(&req->wb_kref);
340*d4581383SWeston Andros Adamson 
341*d4581383SWeston Andros Adamson 	nfs_page_group_unlock(head);
342587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
343*d4581383SWeston Andros Adamson 
344*d4581383SWeston Andros Adamson 	/* release ref from nfs_page_find_head_request_locked */
345*d4581383SWeston Andros Adamson 	nfs_release_request(head);
346*d4581383SWeston Andros Adamson 
347cfb506e1STrond Myklebust 	if (!nonblock)
348e261f51fSTrond Myklebust 		ret = nfs_wait_on_request(req);
349cfb506e1STrond Myklebust 	else
350cfb506e1STrond Myklebust 		ret = -EAGAIN;
351e261f51fSTrond Myklebust 	nfs_release_request(req);
352*d4581383SWeston Andros Adamson 
353*d4581383SWeston Andros Adamson 	return ret;
354e261f51fSTrond Myklebust }
355*d4581383SWeston Andros Adamson 
356*d4581383SWeston Andros Adamson /*
357*d4581383SWeston Andros Adamson  * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests
358*d4581383SWeston Andros Adamson  *
359*d4581383SWeston Andros Adamson  * @destroy_list - request list (using wb_this_page) terminated by @old_head
360*d4581383SWeston Andros Adamson  * @old_head - the old head of the list
361*d4581383SWeston Andros Adamson  *
362*d4581383SWeston Andros Adamson  * All subrequests must be locked and removed from all lists, so at this point
363*d4581383SWeston Andros Adamson  * they are only "active" in this function, and possibly in nfs_wait_on_request
364*d4581383SWeston Andros Adamson  * with a reference held by some other context.
365*d4581383SWeston Andros Adamson  */
366*d4581383SWeston Andros Adamson static void
367*d4581383SWeston Andros Adamson nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list,
368*d4581383SWeston Andros Adamson 				 struct nfs_page *old_head)
369*d4581383SWeston Andros Adamson {
370*d4581383SWeston Andros Adamson 	while (destroy_list) {
371*d4581383SWeston Andros Adamson 		struct nfs_page *subreq = destroy_list;
372*d4581383SWeston Andros Adamson 
373*d4581383SWeston Andros Adamson 		destroy_list = (subreq->wb_this_page == old_head) ?
374*d4581383SWeston Andros Adamson 				   NULL : subreq->wb_this_page;
375*d4581383SWeston Andros Adamson 
376*d4581383SWeston Andros Adamson 		WARN_ON_ONCE(old_head != subreq->wb_head);
377*d4581383SWeston Andros Adamson 
378*d4581383SWeston Andros Adamson 		/* make sure old group is not used */
379*d4581383SWeston Andros Adamson 		subreq->wb_head = subreq;
380*d4581383SWeston Andros Adamson 		subreq->wb_this_page = subreq;
381*d4581383SWeston Andros Adamson 
382*d4581383SWeston Andros Adamson 		nfs_clear_request_commit(subreq);
383*d4581383SWeston Andros Adamson 
384*d4581383SWeston Andros Adamson 		/* subreq is now totally disconnected from page group or any
385*d4581383SWeston Andros Adamson 		 * write / commit lists. last chance to wake any waiters */
386*d4581383SWeston Andros Adamson 		nfs_unlock_request(subreq);
387*d4581383SWeston Andros Adamson 
388*d4581383SWeston Andros Adamson 		if (!test_bit(PG_TEARDOWN, &subreq->wb_flags)) {
389*d4581383SWeston Andros Adamson 			/* release ref on old head request */
390*d4581383SWeston Andros Adamson 			nfs_release_request(old_head);
391*d4581383SWeston Andros Adamson 
392*d4581383SWeston Andros Adamson 			nfs_page_group_clear_bits(subreq);
393*d4581383SWeston Andros Adamson 
394*d4581383SWeston Andros Adamson 			/* release the PG_INODE_REF reference */
395*d4581383SWeston Andros Adamson 			if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags))
396*d4581383SWeston Andros Adamson 				nfs_release_request(subreq);
397*d4581383SWeston Andros Adamson 			else
398*d4581383SWeston Andros Adamson 				WARN_ON_ONCE(1);
399*d4581383SWeston Andros Adamson 		} else {
400*d4581383SWeston Andros Adamson 			WARN_ON_ONCE(test_bit(PG_CLEAN, &subreq->wb_flags));
401*d4581383SWeston Andros Adamson 			/* zombie requests have already released the last
402*d4581383SWeston Andros Adamson 			 * reference and were waiting on the rest of the
403*d4581383SWeston Andros Adamson 			 * group to complete. Since it's no longer part of a
404*d4581383SWeston Andros Adamson 			 * group, simply free the request */
405*d4581383SWeston Andros Adamson 			nfs_page_group_clear_bits(subreq);
406*d4581383SWeston Andros Adamson 			nfs_free_request(subreq);
407*d4581383SWeston Andros Adamson 		}
408*d4581383SWeston Andros Adamson 	}
409*d4581383SWeston Andros Adamson }
410*d4581383SWeston Andros Adamson 
411*d4581383SWeston Andros Adamson /*
412*d4581383SWeston Andros Adamson  * nfs_lock_and_join_requests - join all subreqs to the head req and return
413*d4581383SWeston Andros Adamson  *                              a locked reference, cancelling any pending
414*d4581383SWeston Andros Adamson  *                              operations for this page.
415*d4581383SWeston Andros Adamson  *
416*d4581383SWeston Andros Adamson  * @page - the page used to lookup the "page group" of nfs_page structures
417*d4581383SWeston Andros Adamson  * @nonblock - if true, don't block waiting for request locks
418*d4581383SWeston Andros Adamson  *
419*d4581383SWeston Andros Adamson  * This function joins all sub requests to the head request by first
420*d4581383SWeston Andros Adamson  * locking all requests in the group, cancelling any pending operations
421*d4581383SWeston Andros Adamson  * and finally updating the head request to cover the whole range covered by
422*d4581383SWeston Andros Adamson  * the (former) group.  All subrequests are removed from any write or commit
423*d4581383SWeston Andros Adamson  * lists, unlinked from the group and destroyed.
424*d4581383SWeston Andros Adamson  *
425*d4581383SWeston Andros Adamson  * Returns a locked, referenced pointer to the head request - which after
426*d4581383SWeston Andros Adamson  * this call is guaranteed to be the only request associated with the page.
427*d4581383SWeston Andros Adamson  * Returns NULL if no requests are found for @page, or a ERR_PTR if an
428*d4581383SWeston Andros Adamson  * error was encountered.
429*d4581383SWeston Andros Adamson  */
430*d4581383SWeston Andros Adamson static struct nfs_page *
431*d4581383SWeston Andros Adamson nfs_lock_and_join_requests(struct page *page, bool nonblock)
432*d4581383SWeston Andros Adamson {
433*d4581383SWeston Andros Adamson 	struct inode *inode = page_file_mapping(page)->host;
434*d4581383SWeston Andros Adamson 	struct nfs_page *head, *subreq;
435*d4581383SWeston Andros Adamson 	struct nfs_page *destroy_list = NULL;
436*d4581383SWeston Andros Adamson 	unsigned int total_bytes;
437*d4581383SWeston Andros Adamson 	int ret;
438*d4581383SWeston Andros Adamson 
439*d4581383SWeston Andros Adamson try_again:
440*d4581383SWeston Andros Adamson 	total_bytes = 0;
441*d4581383SWeston Andros Adamson 
442*d4581383SWeston Andros Adamson 	WARN_ON_ONCE(destroy_list);
443*d4581383SWeston Andros Adamson 
444*d4581383SWeston Andros Adamson 	spin_lock(&inode->i_lock);
445*d4581383SWeston Andros Adamson 
446*d4581383SWeston Andros Adamson 	/*
447*d4581383SWeston Andros Adamson 	 * A reference is taken only on the head request which acts as a
448*d4581383SWeston Andros Adamson 	 * reference to the whole page group - the group will not be destroyed
449*d4581383SWeston Andros Adamson 	 * until the head reference is released.
450*d4581383SWeston Andros Adamson 	 */
451*d4581383SWeston Andros Adamson 	head = nfs_page_find_head_request_locked(NFS_I(inode), page);
452*d4581383SWeston Andros Adamson 
453*d4581383SWeston Andros Adamson 	if (!head) {
454587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
455*d4581383SWeston Andros Adamson 		return NULL;
456*d4581383SWeston Andros Adamson 	}
457*d4581383SWeston Andros Adamson 
458*d4581383SWeston Andros Adamson 	/* lock each request in the page group */
459*d4581383SWeston Andros Adamson 	nfs_page_group_lock(head);
460*d4581383SWeston Andros Adamson 	subreq = head;
461*d4581383SWeston Andros Adamson 	do {
462*d4581383SWeston Andros Adamson 		/*
463*d4581383SWeston Andros Adamson 		 * Subrequests are always contiguous, non overlapping
464*d4581383SWeston Andros Adamson 		 * and in order. If not, it's a programming error.
465*d4581383SWeston Andros Adamson 		 */
466*d4581383SWeston Andros Adamson 		WARN_ON_ONCE(subreq->wb_offset !=
467*d4581383SWeston Andros Adamson 		     (head->wb_offset + total_bytes));
468*d4581383SWeston Andros Adamson 
469*d4581383SWeston Andros Adamson 		/* keep track of how many bytes this group covers */
470*d4581383SWeston Andros Adamson 		total_bytes += subreq->wb_bytes;
471*d4581383SWeston Andros Adamson 
472*d4581383SWeston Andros Adamson 		if (!nfs_lock_request(subreq)) {
473*d4581383SWeston Andros Adamson 			/* releases page group bit lock and
474*d4581383SWeston Andros Adamson 			 * inode spin lock and all references */
475*d4581383SWeston Andros Adamson 			ret = nfs_unroll_locks_and_wait(inode, head,
476*d4581383SWeston Andros Adamson 				subreq, nonblock);
477*d4581383SWeston Andros Adamson 
478*d4581383SWeston Andros Adamson 			if (ret == 0)
479*d4581383SWeston Andros Adamson 				goto try_again;
480*d4581383SWeston Andros Adamson 
481*d4581383SWeston Andros Adamson 			return ERR_PTR(ret);
482*d4581383SWeston Andros Adamson 		}
483*d4581383SWeston Andros Adamson 
484*d4581383SWeston Andros Adamson 		subreq = subreq->wb_this_page;
485*d4581383SWeston Andros Adamson 	} while (subreq != head);
486*d4581383SWeston Andros Adamson 
487*d4581383SWeston Andros Adamson 	/* Now that all requests are locked, make sure they aren't on any list.
488*d4581383SWeston Andros Adamson 	 * Commit list removal accounting is done after locks are dropped */
489*d4581383SWeston Andros Adamson 	subreq = head;
490*d4581383SWeston Andros Adamson 	do {
491*d4581383SWeston Andros Adamson 		nfs_list_remove_request(subreq);
492*d4581383SWeston Andros Adamson 		subreq = subreq->wb_this_page;
493*d4581383SWeston Andros Adamson 	} while (subreq != head);
494*d4581383SWeston Andros Adamson 
495*d4581383SWeston Andros Adamson 	/* unlink subrequests from head, destroy them later */
496*d4581383SWeston Andros Adamson 	if (head->wb_this_page != head) {
497*d4581383SWeston Andros Adamson 		/* destroy list will be terminated by head */
498*d4581383SWeston Andros Adamson 		destroy_list = head->wb_this_page;
499*d4581383SWeston Andros Adamson 		head->wb_this_page = head;
500*d4581383SWeston Andros Adamson 
501*d4581383SWeston Andros Adamson 		/* change head request to cover whole range that
502*d4581383SWeston Andros Adamson 		 * the former page group covered */
503*d4581383SWeston Andros Adamson 		head->wb_bytes = total_bytes;
504*d4581383SWeston Andros Adamson 	}
505*d4581383SWeston Andros Adamson 
506*d4581383SWeston Andros Adamson 	/*
507*d4581383SWeston Andros Adamson 	 * prepare head request to be added to new pgio descriptor
508*d4581383SWeston Andros Adamson 	 */
509*d4581383SWeston Andros Adamson 	nfs_page_group_clear_bits(head);
510*d4581383SWeston Andros Adamson 
511*d4581383SWeston Andros Adamson 	/*
512*d4581383SWeston Andros Adamson 	 * some part of the group was still on the inode list - otherwise
513*d4581383SWeston Andros Adamson 	 * the group wouldn't be involved in async write.
514*d4581383SWeston Andros Adamson 	 * grab a reference for the head request, iff it needs one.
515*d4581383SWeston Andros Adamson 	 */
516*d4581383SWeston Andros Adamson 	if (!test_and_set_bit(PG_INODE_REF, &head->wb_flags))
517*d4581383SWeston Andros Adamson 		kref_get(&head->wb_kref);
518*d4581383SWeston Andros Adamson 
519*d4581383SWeston Andros Adamson 	nfs_page_group_unlock(head);
520*d4581383SWeston Andros Adamson 
521*d4581383SWeston Andros Adamson 	/* drop lock to clear_request_commit the head req and clean up
522*d4581383SWeston Andros Adamson 	 * requests on destroy list */
523*d4581383SWeston Andros Adamson 	spin_unlock(&inode->i_lock);
524*d4581383SWeston Andros Adamson 
525*d4581383SWeston Andros Adamson 	nfs_destroy_unlinked_subrequests(destroy_list, head);
526*d4581383SWeston Andros Adamson 
527*d4581383SWeston Andros Adamson 	/* clean up commit list state */
528*d4581383SWeston Andros Adamson 	nfs_clear_request_commit(head);
529*d4581383SWeston Andros Adamson 
530*d4581383SWeston Andros Adamson 	/* still holds ref on head from nfs_page_find_head_request_locked
531*d4581383SWeston Andros Adamson 	 * and still has lock on head from lock loop */
532*d4581383SWeston Andros Adamson 	return head;
533612c9384STrond Myklebust }
534074cc1deSTrond Myklebust 
535074cc1deSTrond Myklebust /*
536074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
537074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
538074cc1deSTrond Myklebust  */
539074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
540cfb506e1STrond Myklebust 				struct page *page, bool nonblock)
541074cc1deSTrond Myklebust {
542074cc1deSTrond Myklebust 	struct nfs_page *req;
543074cc1deSTrond Myklebust 	int ret = 0;
544074cc1deSTrond Myklebust 
545*d4581383SWeston Andros Adamson 	req = nfs_lock_and_join_requests(page, nonblock);
546074cc1deSTrond Myklebust 	if (!req)
547074cc1deSTrond Myklebust 		goto out;
548074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
549074cc1deSTrond Myklebust 	if (IS_ERR(req))
550074cc1deSTrond Myklebust 		goto out;
551074cc1deSTrond Myklebust 
552deed85e7STrond Myklebust 	nfs_set_page_writeback(page);
553deed85e7STrond Myklebust 	WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
554074cc1deSTrond Myklebust 
555deed85e7STrond Myklebust 	ret = 0;
556f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
557f8512ad0SFred Isaman 		nfs_redirty_request(req);
558074cc1deSTrond Myklebust 		ret = pgio->pg_error;
559f8512ad0SFred Isaman 	}
560074cc1deSTrond Myklebust out:
561074cc1deSTrond Myklebust 	return ret;
562e261f51fSTrond Myklebust }
563e261f51fSTrond Myklebust 
564f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
565f758c885STrond Myklebust {
566d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
567cfb506e1STrond Myklebust 	int ret;
568f758c885STrond Myklebust 
569f758c885STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
570f758c885STrond Myklebust 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
571f758c885STrond Myklebust 
572d56b4ddfSMel Gorman 	nfs_pageio_cond_complete(pgio, page_file_index(page));
5731b430beeSWu Fengguang 	ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
574cfb506e1STrond Myklebust 	if (ret == -EAGAIN) {
575cfb506e1STrond Myklebust 		redirty_page_for_writepage(wbc, page);
576cfb506e1STrond Myklebust 		ret = 0;
577cfb506e1STrond Myklebust 	}
578cfb506e1STrond Myklebust 	return ret;
579f758c885STrond Myklebust }
580f758c885STrond Myklebust 
581e261f51fSTrond Myklebust /*
5821da177e4SLinus Torvalds  * Write an mmapped page to the server.
5831da177e4SLinus Torvalds  */
5844d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
5851da177e4SLinus Torvalds {
586f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
587e261f51fSTrond Myklebust 	int err;
5881da177e4SLinus Torvalds 
589a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc),
590a20c93e3SChristoph Hellwig 				false, &nfs_async_write_completion_ops);
591f758c885STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
592f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
593f758c885STrond Myklebust 	if (err < 0)
5944d770ccfSTrond Myklebust 		return err;
595f758c885STrond Myklebust 	if (pgio.pg_error < 0)
596f758c885STrond Myklebust 		return pgio.pg_error;
597f758c885STrond Myklebust 	return 0;
5984d770ccfSTrond Myklebust }
5994d770ccfSTrond Myklebust 
6004d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
6014d770ccfSTrond Myklebust {
602f758c885STrond Myklebust 	int ret;
6034d770ccfSTrond Myklebust 
604f758c885STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
6051da177e4SLinus Torvalds 	unlock_page(page);
606f758c885STrond Myklebust 	return ret;
607f758c885STrond Myklebust }
608f758c885STrond Myklebust 
609f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
610f758c885STrond Myklebust {
611f758c885STrond Myklebust 	int ret;
612f758c885STrond Myklebust 
613f758c885STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
614f758c885STrond Myklebust 	unlock_page(page);
615f758c885STrond Myklebust 	return ret;
6161da177e4SLinus Torvalds }
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
6191da177e4SLinus Torvalds {
6201da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
62172cb77f4STrond Myklebust 	unsigned long *bitlock = &NFS_I(inode)->flags;
622c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
6231da177e4SLinus Torvalds 	int err;
6241da177e4SLinus Torvalds 
62572cb77f4STrond Myklebust 	/* Stop dirtying of new pages while we sync */
62672cb77f4STrond Myklebust 	err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING,
62772cb77f4STrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
62872cb77f4STrond Myklebust 	if (err)
62972cb77f4STrond Myklebust 		goto out_err;
63072cb77f4STrond Myklebust 
63191d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
63291d5b470SChuck Lever 
633a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false,
634a20c93e3SChristoph Hellwig 				&nfs_async_write_completion_ops);
635f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
636c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
63772cb77f4STrond Myklebust 
63872cb77f4STrond Myklebust 	clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
6394e857c58SPeter Zijlstra 	smp_mb__after_atomic();
64072cb77f4STrond Myklebust 	wake_up_bit(bitlock, NFS_INO_FLUSHING);
64172cb77f4STrond Myklebust 
642f758c885STrond Myklebust 	if (err < 0)
64372cb77f4STrond Myklebust 		goto out_err;
64472cb77f4STrond Myklebust 	err = pgio.pg_error;
64572cb77f4STrond Myklebust 	if (err < 0)
64672cb77f4STrond Myklebust 		goto out_err;
647c63c7b05STrond Myklebust 	return 0;
64872cb77f4STrond Myklebust out_err:
64972cb77f4STrond Myklebust 	return err;
6501da177e4SLinus Torvalds }
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds /*
6531da177e4SLinus Torvalds  * Insert a write request into an inode
6541da177e4SLinus Torvalds  */
655d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
6561da177e4SLinus Torvalds {
6571da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
658e7d39069STrond Myklebust 
6592bfc6e56SWeston Andros Adamson 	WARN_ON_ONCE(req->wb_this_page != req);
6602bfc6e56SWeston Andros Adamson 
661e7d39069STrond Myklebust 	/* Lock the request! */
6627ad84aa9STrond Myklebust 	nfs_lock_request(req);
663e7d39069STrond Myklebust 
664e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
665011e2a7fSBryan Schumaker 	if (!nfsi->npages && NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
666a9a4a87aSTrond Myklebust 		inode->i_version++;
66729418aa4SMel Gorman 	/*
66829418aa4SMel Gorman 	 * Swap-space should not get truncated. Hence no need to plug the race
66929418aa4SMel Gorman 	 * with invalidate/truncate.
67029418aa4SMel Gorman 	 */
67129418aa4SMel Gorman 	if (likely(!PageSwapCache(req->wb_page))) {
6722df485a7STrond Myklebust 		set_bit(PG_MAPPED, &req->wb_flags);
673deb7d638STrond Myklebust 		SetPagePrivate(req->wb_page);
674277459d2STrond Myklebust 		set_page_private(req->wb_page, (unsigned long)req);
67529418aa4SMel Gorman 	}
6761da177e4SLinus Torvalds 	nfsi->npages++;
67717089a29SWeston Andros Adamson 	/* this a head request for a page group - mark it as having an
67817089a29SWeston Andros Adamson 	 * extra reference so sub groups can follow suit */
67917089a29SWeston Andros Adamson 	WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags));
680c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
681e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
6821da177e4SLinus Torvalds }
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds /*
68589a09141SPeter Zijlstra  * Remove a write request from an inode
6861da177e4SLinus Torvalds  */
6871da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
6881da177e4SLinus Torvalds {
6893d4ff43dSAl Viro 	struct inode *inode = req->wb_context->dentry->d_inode;
6901da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
69120633f04SWeston Andros Adamson 	struct nfs_page *head;
69220633f04SWeston Andros Adamson 
69320633f04SWeston Andros Adamson 	if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) {
69420633f04SWeston Andros Adamson 		head = req->wb_head;
6951da177e4SLinus Torvalds 
696587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
69720633f04SWeston Andros Adamson 		if (likely(!PageSwapCache(head->wb_page))) {
69820633f04SWeston Andros Adamson 			set_page_private(head->wb_page, 0);
69920633f04SWeston Andros Adamson 			ClearPagePrivate(head->wb_page);
70020633f04SWeston Andros Adamson 			clear_bit(PG_MAPPED, &head->wb_flags);
70129418aa4SMel Gorman 		}
7021da177e4SLinus Torvalds 		nfsi->npages--;
703587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
70420633f04SWeston Andros Adamson 	}
70517089a29SWeston Andros Adamson 
70617089a29SWeston Andros Adamson 	if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags))
7071da177e4SLinus Torvalds 		nfs_release_request(req);
7081da177e4SLinus Torvalds }
7091da177e4SLinus Torvalds 
71061822ab5STrond Myklebust static void
7116d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
71261822ab5STrond Myklebust {
71361822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
71461822ab5STrond Myklebust }
71561822ab5STrond Myklebust 
71689d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
7178dd37758STrond Myklebust /**
7188dd37758STrond Myklebust  * nfs_request_add_commit_list - add request to a commit list
7198dd37758STrond Myklebust  * @req: pointer to a struct nfs_page
720ea2cf228SFred Isaman  * @dst: commit list head
721ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
7228dd37758STrond Myklebust  *
723ea2cf228SFred Isaman  * This sets the PG_CLEAN bit, updates the cinfo count of
7248dd37758STrond Myklebust  * number of outstanding requests requiring a commit as well as
7258dd37758STrond Myklebust  * the MM page stats.
7268dd37758STrond Myklebust  *
727ea2cf228SFred Isaman  * The caller must _not_ hold the cinfo->lock, but must be
7288dd37758STrond Myklebust  * holding the nfs_page lock.
7298dd37758STrond Myklebust  */
7308dd37758STrond Myklebust void
731ea2cf228SFred Isaman nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst,
732ea2cf228SFred Isaman 			    struct nfs_commit_info *cinfo)
7338dd37758STrond Myklebust {
7348dd37758STrond Myklebust 	set_bit(PG_CLEAN, &(req)->wb_flags);
735ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
736ea2cf228SFred Isaman 	nfs_list_add_request(req, dst);
737ea2cf228SFred Isaman 	cinfo->mds->ncommit++;
738ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
73956f9cd68SFred Isaman 	if (!cinfo->dreq) {
7408dd37758STrond Myklebust 		inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
741d56b4ddfSMel Gorman 		inc_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
74256f9cd68SFred Isaman 			     BDI_RECLAIMABLE);
74356f9cd68SFred Isaman 		__mark_inode_dirty(req->wb_context->dentry->d_inode,
74456f9cd68SFred Isaman 				   I_DIRTY_DATASYNC);
74556f9cd68SFred Isaman 	}
7468dd37758STrond Myklebust }
7478dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
7488dd37758STrond Myklebust 
7498dd37758STrond Myklebust /**
7508dd37758STrond Myklebust  * nfs_request_remove_commit_list - Remove request from a commit list
7518dd37758STrond Myklebust  * @req: pointer to a nfs_page
752ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
7538dd37758STrond Myklebust  *
754ea2cf228SFred Isaman  * This clears the PG_CLEAN bit, and updates the cinfo's count of
7558dd37758STrond Myklebust  * number of outstanding requests requiring a commit
7568dd37758STrond Myklebust  * It does not update the MM page stats.
7578dd37758STrond Myklebust  *
758ea2cf228SFred Isaman  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
7598dd37758STrond Myklebust  */
7608dd37758STrond Myklebust void
761ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req,
762ea2cf228SFred Isaman 			       struct nfs_commit_info *cinfo)
7638dd37758STrond Myklebust {
7648dd37758STrond Myklebust 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
7658dd37758STrond Myklebust 		return;
7668dd37758STrond Myklebust 	nfs_list_remove_request(req);
767ea2cf228SFred Isaman 	cinfo->mds->ncommit--;
7688dd37758STrond Myklebust }
7698dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
7708dd37758STrond Myklebust 
771ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
772ea2cf228SFred Isaman 				      struct inode *inode)
773ea2cf228SFred Isaman {
774ea2cf228SFred Isaman 	cinfo->lock = &inode->i_lock;
775ea2cf228SFred Isaman 	cinfo->mds = &NFS_I(inode)->commit_info;
776ea2cf228SFred Isaman 	cinfo->ds = pnfs_get_ds_info(inode);
777b359f9d0SFred Isaman 	cinfo->dreq = NULL;
778f453a54aSFred Isaman 	cinfo->completion_ops = &nfs_commit_completion_ops;
779ea2cf228SFred Isaman }
780ea2cf228SFred Isaman 
781ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
782ea2cf228SFred Isaman 		    struct inode *inode,
783ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq)
784ea2cf228SFred Isaman {
7851763da12SFred Isaman 	if (dreq)
7861763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
7871763da12SFred Isaman 	else
788ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(cinfo, inode);
789ea2cf228SFred Isaman }
790ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo);
7918dd37758STrond Myklebust 
7921da177e4SLinus Torvalds /*
7931da177e4SLinus Torvalds  * Add a request to the inode's commit list.
7941da177e4SLinus Torvalds  */
7951763da12SFred Isaman void
796ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
797ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
7981da177e4SLinus Torvalds {
799ea2cf228SFred Isaman 	if (pnfs_mark_request_commit(req, lseg, cinfo))
8008dd37758STrond Myklebust 		return;
801ea2cf228SFred Isaman 	nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo);
8021da177e4SLinus Torvalds }
8038e821cadSTrond Myklebust 
804d6d6dc7cSFred Isaman static void
805d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page)
806e468bae9STrond Myklebust {
807e468bae9STrond Myklebust 	dec_zone_page_state(page, NR_UNSTABLE_NFS);
808d56b4ddfSMel Gorman 	dec_bdi_stat(page_file_mapping(page)->backing_dev_info, BDI_RECLAIMABLE);
809e468bae9STrond Myklebust }
810d6d6dc7cSFred Isaman 
8118dd37758STrond Myklebust static void
812d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req)
813d6d6dc7cSFred Isaman {
8148dd37758STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
8158dd37758STrond Myklebust 		struct inode *inode = req->wb_context->dentry->d_inode;
816ea2cf228SFred Isaman 		struct nfs_commit_info cinfo;
817d6d6dc7cSFred Isaman 
818ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(&cinfo, inode);
819ea2cf228SFred Isaman 		if (!pnfs_clear_request_commit(req, &cinfo)) {
820ea2cf228SFred Isaman 			spin_lock(cinfo.lock);
821ea2cf228SFred Isaman 			nfs_request_remove_commit_list(req, &cinfo);
822ea2cf228SFred Isaman 			spin_unlock(cinfo.lock);
823d6d6dc7cSFred Isaman 		}
8248dd37758STrond Myklebust 		nfs_clear_page_commit(req->wb_page);
8258dd37758STrond Myklebust 	}
826e468bae9STrond Myklebust }
827e468bae9STrond Myklebust 
8288e821cadSTrond Myklebust static inline
8299c7e1b3dSAnna Schumaker int nfs_write_need_commit(struct nfs_pgio_data *data)
8308e821cadSTrond Myklebust {
831465d5243SFred Isaman 	if (data->verf.committed == NFS_DATA_SYNC)
832cd841605SFred Isaman 		return data->header->lseg == NULL;
8338e821cadSTrond Myklebust 	return data->verf.committed != NFS_FILE_SYNC;
8348e821cadSTrond Myklebust }
8358e821cadSTrond Myklebust 
8368e821cadSTrond Myklebust #else
83768cd6fa4SBryan Schumaker static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
83868cd6fa4SBryan Schumaker 				      struct inode *inode)
83968cd6fa4SBryan Schumaker {
84068cd6fa4SBryan Schumaker }
84168cd6fa4SBryan Schumaker 
84268cd6fa4SBryan Schumaker void nfs_init_cinfo(struct nfs_commit_info *cinfo,
84368cd6fa4SBryan Schumaker 		    struct inode *inode,
84468cd6fa4SBryan Schumaker 		    struct nfs_direct_req *dreq)
84568cd6fa4SBryan Schumaker {
84668cd6fa4SBryan Schumaker }
84768cd6fa4SBryan Schumaker 
8481763da12SFred Isaman void
849ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
850ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
8518e821cadSTrond Myklebust {
8528e821cadSTrond Myklebust }
8538e821cadSTrond Myklebust 
8548dd37758STrond Myklebust static void
855e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
856e468bae9STrond Myklebust {
857e468bae9STrond Myklebust }
858e468bae9STrond Myklebust 
8598e821cadSTrond Myklebust static inline
8609c7e1b3dSAnna Schumaker int nfs_write_need_commit(struct nfs_pgio_data *data)
8618e821cadSTrond Myklebust {
8628e821cadSTrond Myklebust 	return 0;
8638e821cadSTrond Myklebust }
8648e821cadSTrond Myklebust 
8651da177e4SLinus Torvalds #endif
8661da177e4SLinus Torvalds 
867061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
8686c75dc0dSFred Isaman {
869ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
8706c75dc0dSFred Isaman 	unsigned long bytes = 0;
8712bfc6e56SWeston Andros Adamson 	bool do_destroy;
8726c75dc0dSFred Isaman 
8736c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
8746c75dc0dSFred Isaman 		goto out;
875ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
8766c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
8776c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
8786c75dc0dSFred Isaman 
8796c75dc0dSFred Isaman 		bytes += req->wb_bytes;
8806c75dc0dSFred Isaman 		nfs_list_remove_request(req);
8816c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
8826c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
883d1182b33STrond Myklebust 			nfs_set_pageerror(req->wb_page);
8846c75dc0dSFred Isaman 			nfs_context_set_write_error(req->wb_context, hdr->error);
8856c75dc0dSFred Isaman 			goto remove_req;
8866c75dc0dSFred Isaman 		}
8876c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
8886c75dc0dSFred Isaman 			nfs_mark_request_dirty(req);
8896c75dc0dSFred Isaman 			goto next;
8906c75dc0dSFred Isaman 		}
8916c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
892f79d06f5SAnna Schumaker 			memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
893ea2cf228SFred Isaman 			nfs_mark_request_commit(req, hdr->lseg, &cinfo);
8946c75dc0dSFred Isaman 			goto next;
8956c75dc0dSFred Isaman 		}
8966c75dc0dSFred Isaman remove_req:
8976c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
8986c75dc0dSFred Isaman next:
8991d1afcbcSTrond Myklebust 		nfs_unlock_request(req);
90020633f04SWeston Andros Adamson 		nfs_end_page_writeback(req);
9012bfc6e56SWeston Andros Adamson 		do_destroy = !test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags);
9023aff4ebbSTrond Myklebust 		nfs_release_request(req);
9036c75dc0dSFred Isaman 	}
9046c75dc0dSFred Isaman out:
9056c75dc0dSFred Isaman 	hdr->release(hdr);
9066c75dc0dSFred Isaman }
9076c75dc0dSFred Isaman 
90889d77c8fSBryan Schumaker #if  IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
909ce59515cSAnna Schumaker unsigned long
910ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
911fb8a1f11STrond Myklebust {
912ea2cf228SFred Isaman 	return cinfo->mds->ncommit;
913fb8a1f11STrond Myklebust }
914fb8a1f11STrond Myklebust 
915ea2cf228SFred Isaman /* cinfo->lock held by caller */
9161763da12SFred Isaman int
917ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
918ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
919d6d6dc7cSFred Isaman {
920d6d6dc7cSFred Isaman 	struct nfs_page *req, *tmp;
921d6d6dc7cSFred Isaman 	int ret = 0;
922d6d6dc7cSFred Isaman 
923d6d6dc7cSFred Isaman 	list_for_each_entry_safe(req, tmp, src, wb_list) {
9248dd37758STrond Myklebust 		if (!nfs_lock_request(req))
9258dd37758STrond Myklebust 			continue;
9267ad84aa9STrond Myklebust 		kref_get(&req->wb_kref);
927ea2cf228SFred Isaman 		if (cond_resched_lock(cinfo->lock))
9283b3be88dSTrond Myklebust 			list_safe_reset_next(req, tmp, wb_list);
929ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
9308dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
931d6d6dc7cSFred Isaman 		ret++;
9321763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
933d6d6dc7cSFred Isaman 			break;
934d6d6dc7cSFred Isaman 	}
935d6d6dc7cSFred Isaman 	return ret;
936d6d6dc7cSFred Isaman }
937d6d6dc7cSFred Isaman 
9381da177e4SLinus Torvalds /*
9391da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
9401da177e4SLinus Torvalds  * @inode: NFS inode to scan
941ea2cf228SFred Isaman  * @dst: mds destination list
942ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
9431da177e4SLinus Torvalds  *
9441da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
9451da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
9461da177e4SLinus Torvalds  */
9471763da12SFred Isaman int
948ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
949ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
9501da177e4SLinus Torvalds {
951d6d6dc7cSFred Isaman 	int ret = 0;
952fb8a1f11STrond Myklebust 
953ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
954ea2cf228SFred Isaman 	if (cinfo->mds->ncommit > 0) {
9558dd37758STrond Myklebust 		const int max = INT_MAX;
956d6d6dc7cSFred Isaman 
957ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
958ea2cf228SFred Isaman 					   cinfo, max);
959ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
960d6d6dc7cSFred Isaman 	}
961ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
962ff778d02STrond Myklebust 	return ret;
9631da177e4SLinus Torvalds }
964d6d6dc7cSFred Isaman 
965c42de9ddSTrond Myklebust #else
966ce59515cSAnna Schumaker unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
967fb8a1f11STrond Myklebust {
968fb8a1f11STrond Myklebust 	return 0;
969fb8a1f11STrond Myklebust }
970fb8a1f11STrond Myklebust 
9711763da12SFred Isaman int nfs_scan_commit(struct inode *inode, struct list_head *dst,
972ea2cf228SFred Isaman 		    struct nfs_commit_info *cinfo)
973c42de9ddSTrond Myklebust {
974c42de9ddSTrond Myklebust 	return 0;
975c42de9ddSTrond Myklebust }
9761da177e4SLinus Torvalds #endif
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds /*
979e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
980e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
9811da177e4SLinus Torvalds  *
982e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
983e7d39069STrond Myklebust  * to disk.
9841da177e4SLinus Torvalds  */
985e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
986e7d39069STrond Myklebust 		struct page *page,
987e7d39069STrond Myklebust 		unsigned int offset,
988e7d39069STrond Myklebust 		unsigned int bytes)
9891da177e4SLinus Torvalds {
990e7d39069STrond Myklebust 	struct nfs_page *req;
991e7d39069STrond Myklebust 	unsigned int rqend;
992e7d39069STrond Myklebust 	unsigned int end;
9931da177e4SLinus Torvalds 	int error;
994277459d2STrond Myklebust 
995e7d39069STrond Myklebust 	if (!PagePrivate(page))
996e7d39069STrond Myklebust 		return NULL;
997e7d39069STrond Myklebust 
998e7d39069STrond Myklebust 	end = offset + bytes;
999e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
1000e7d39069STrond Myklebust 
1001e7d39069STrond Myklebust 	for (;;) {
100284d3a9a9SWeston Andros Adamson 		req = nfs_page_find_head_request_locked(NFS_I(inode), page);
1003e7d39069STrond Myklebust 		if (req == NULL)
1004e7d39069STrond Myklebust 			goto out_unlock;
1005e7d39069STrond Myklebust 
10062bfc6e56SWeston Andros Adamson 		/* should be handled by nfs_flush_incompatible */
10072bfc6e56SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_head != req);
10082bfc6e56SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_this_page != req);
10092bfc6e56SWeston Andros Adamson 
1010e7d39069STrond Myklebust 		rqend = req->wb_offset + req->wb_bytes;
1011e7d39069STrond Myklebust 		/*
1012e7d39069STrond Myklebust 		 * Tell the caller to flush out the request if
1013e7d39069STrond Myklebust 		 * the offsets are non-contiguous.
1014e7d39069STrond Myklebust 		 * Note: nfs_flush_incompatible() will already
1015e7d39069STrond Myklebust 		 * have flushed out requests having wrong owners.
1016e7d39069STrond Myklebust 		 */
1017e468bae9STrond Myklebust 		if (offset > rqend
1018e7d39069STrond Myklebust 		    || end < req->wb_offset)
1019e7d39069STrond Myklebust 			goto out_flushme;
1020e7d39069STrond Myklebust 
10217ad84aa9STrond Myklebust 		if (nfs_lock_request(req))
1022e7d39069STrond Myklebust 			break;
1023e7d39069STrond Myklebust 
1024e7d39069STrond Myklebust 		/* The request is locked, so wait and then retry */
1025587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
10261da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
10271da177e4SLinus Torvalds 		nfs_release_request(req);
1028e7d39069STrond Myklebust 		if (error != 0)
1029e7d39069STrond Myklebust 			goto out_err;
1030e7d39069STrond Myklebust 		spin_lock(&inode->i_lock);
10311da177e4SLinus Torvalds 	}
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
10341da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
10351da177e4SLinus Torvalds 		req->wb_offset = offset;
10361da177e4SLinus Torvalds 		req->wb_pgbase = offset;
10371da177e4SLinus Torvalds 	}
10381da177e4SLinus Torvalds 	if (end > rqend)
10391da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
1040e7d39069STrond Myklebust 	else
1041e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
1042e7d39069STrond Myklebust out_unlock:
1043e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
1044ca138f36SFred Isaman 	if (req)
10458dd37758STrond Myklebust 		nfs_clear_request_commit(req);
1046e7d39069STrond Myklebust 	return req;
1047e7d39069STrond Myklebust out_flushme:
1048e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
1049e7d39069STrond Myklebust 	nfs_release_request(req);
1050e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
1051e7d39069STrond Myklebust out_err:
1052e7d39069STrond Myklebust 	return ERR_PTR(error);
1053e7d39069STrond Myklebust }
10541da177e4SLinus Torvalds 
1055e7d39069STrond Myklebust /*
1056e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
1057e7d39069STrond Myklebust  *
1058e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
1059e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
1060e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
1061e7d39069STrond Myklebust  */
1062e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
1063e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
1064e7d39069STrond Myklebust {
1065d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
1066e7d39069STrond Myklebust 	struct nfs_page	*req;
1067e7d39069STrond Myklebust 
1068e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
1069e7d39069STrond Myklebust 	if (req != NULL)
1070e7d39069STrond Myklebust 		goto out;
10712bfc6e56SWeston Andros Adamson 	req = nfs_create_request(ctx, page, NULL, offset, bytes);
1072e7d39069STrond Myklebust 	if (IS_ERR(req))
1073e7d39069STrond Myklebust 		goto out;
1074d6d6dc7cSFred Isaman 	nfs_inode_add_request(inode, req);
1075efc91ed0STrond Myklebust out:
107661e930a9STrond Myklebust 	return req;
10771da177e4SLinus Torvalds }
10781da177e4SLinus Torvalds 
1079e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
1080e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
1081e7d39069STrond Myklebust {
1082e7d39069STrond Myklebust 	struct nfs_page	*req;
1083e7d39069STrond Myklebust 
1084e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
1085e7d39069STrond Myklebust 	if (IS_ERR(req))
1086e7d39069STrond Myklebust 		return PTR_ERR(req);
1087e7d39069STrond Myklebust 	/* Update file length */
1088e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
1089d72ddcbaSWeston Andros Adamson 	nfs_mark_uptodate(req);
1090a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
10911d1afcbcSTrond Myklebust 	nfs_unlock_and_release_request(req);
1092e7d39069STrond Myklebust 	return 0;
1093e7d39069STrond Myklebust }
1094e7d39069STrond Myklebust 
10951da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
10961da177e4SLinus Torvalds {
1097cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
10982a369153STrond Myklebust 	struct nfs_lock_context *l_ctx;
10991da177e4SLinus Torvalds 	struct nfs_page	*req;
11001a54533eSTrond Myklebust 	int do_flush, status;
11011da177e4SLinus Torvalds 	/*
11021da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
11031da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
11041da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
11051da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
11061da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
11071da177e4SLinus Torvalds 	 * dropped page.
11081da177e4SLinus Torvalds 	 */
11091a54533eSTrond Myklebust 	do {
111084d3a9a9SWeston Andros Adamson 		req = nfs_page_find_head_request(page);
11111a54533eSTrond Myklebust 		if (req == NULL)
11121a54533eSTrond Myklebust 			return 0;
11132a369153STrond Myklebust 		l_ctx = req->wb_lock_context;
11142a369153STrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx;
11152bfc6e56SWeston Andros Adamson 		/* for now, flush if more than 1 request in page_group */
11162bfc6e56SWeston Andros Adamson 		do_flush |= req->wb_this_page != req;
11170f1d2605STrond Myklebust 		if (l_ctx && ctx->dentry->d_inode->i_flock != NULL) {
11182a369153STrond Myklebust 			do_flush |= l_ctx->lockowner.l_owner != current->files
11192a369153STrond Myklebust 				|| l_ctx->lockowner.l_pid != current->tgid;
11202a369153STrond Myklebust 		}
11211da177e4SLinus Torvalds 		nfs_release_request(req);
11221a54533eSTrond Myklebust 		if (!do_flush)
11231a54533eSTrond Myklebust 			return 0;
1124d56b4ddfSMel Gorman 		status = nfs_wb_page(page_file_mapping(page)->host, page);
11251a54533eSTrond Myklebust 	} while (status == 0);
11261a54533eSTrond Myklebust 	return status;
11271da177e4SLinus Torvalds }
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds /*
1130dc24826bSAndy Adamson  * Avoid buffered writes when a open context credential's key would
1131dc24826bSAndy Adamson  * expire soon.
1132dc24826bSAndy Adamson  *
1133dc24826bSAndy Adamson  * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
1134dc24826bSAndy Adamson  *
1135dc24826bSAndy Adamson  * Return 0 and set a credential flag which triggers the inode to flush
1136dc24826bSAndy Adamson  * and performs  NFS_FILE_SYNC writes if the key will expired within
1137dc24826bSAndy Adamson  * RPC_KEY_EXPIRE_TIMEO.
1138dc24826bSAndy Adamson  */
1139dc24826bSAndy Adamson int
1140dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode)
1141dc24826bSAndy Adamson {
1142dc24826bSAndy Adamson 	struct nfs_open_context *ctx = nfs_file_open_context(filp);
1143dc24826bSAndy Adamson 	struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
1144dc24826bSAndy Adamson 
1145dc24826bSAndy Adamson 	return rpcauth_key_timeout_notify(auth, ctx->cred);
1146dc24826bSAndy Adamson }
1147dc24826bSAndy Adamson 
1148dc24826bSAndy Adamson /*
1149dc24826bSAndy Adamson  * Test if the open context credential key is marked to expire soon.
1150dc24826bSAndy Adamson  */
1151dc24826bSAndy Adamson bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx)
1152dc24826bSAndy Adamson {
1153dc24826bSAndy Adamson 	return rpcauth_cred_key_to_expire(ctx->cred);
1154dc24826bSAndy Adamson }
1155dc24826bSAndy Adamson 
1156dc24826bSAndy Adamson /*
11575d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
11585d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
11595d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
11605d47a356STrond Myklebust  */
11618d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
11625d47a356STrond Myklebust {
1163d529ef83SJeff Layton 	struct nfs_inode *nfsi = NFS_I(inode);
1164d529ef83SJeff Layton 
11658d197a56STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
11668d197a56STrond Myklebust 		goto out;
116718dd78c4SScott Mayhew 	if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
1168d529ef83SJeff Layton 		return false;
11694db72b40SJeff Layton 	smp_rmb();
1170d529ef83SJeff Layton 	if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags))
11718d197a56STrond Myklebust 		return false;
11728d197a56STrond Myklebust out:
117318dd78c4SScott Mayhew 	if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
117418dd78c4SScott Mayhew 		return false;
11758d197a56STrond Myklebust 	return PageUptodate(page) != 0;
11765d47a356STrond Myklebust }
11775d47a356STrond Myklebust 
1178c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or
1179c7559663SScott Mayhew  * if we have the whole file locked for writing), it may be more efficient to
1180c7559663SScott Mayhew  * extend the write to cover the entire page in order to avoid fragmentation
1181c7559663SScott Mayhew  * inefficiencies.
1182c7559663SScott Mayhew  *
1183263b4509SScott Mayhew  * If the file is opened for synchronous writes then we can just skip the rest
1184263b4509SScott Mayhew  * of the checks.
1185c7559663SScott Mayhew  */
1186c7559663SScott Mayhew static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode)
1187c7559663SScott Mayhew {
1188c7559663SScott Mayhew 	if (file->f_flags & O_DSYNC)
1189c7559663SScott Mayhew 		return 0;
1190263b4509SScott Mayhew 	if (!nfs_write_pageuptodate(page, inode))
1191263b4509SScott Mayhew 		return 0;
1192c7559663SScott Mayhew 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
1193c7559663SScott Mayhew 		return 1;
1194263b4509SScott Mayhew 	if (inode->i_flock == NULL || (inode->i_flock->fl_start == 0 &&
1195c7559663SScott Mayhew 			inode->i_flock->fl_end == OFFSET_MAX &&
1196263b4509SScott Mayhew 			inode->i_flock->fl_type != F_RDLCK))
1197c7559663SScott Mayhew 		return 1;
1198c7559663SScott Mayhew 	return 0;
1199c7559663SScott Mayhew }
1200c7559663SScott Mayhew 
12015d47a356STrond Myklebust /*
12021da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
12031da177e4SLinus Torvalds  *
12041da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
12051da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
12061da177e4SLinus Torvalds  */
12071da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
12081da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
12091da177e4SLinus Torvalds {
1210cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
1211d56b4ddfSMel Gorman 	struct inode	*inode = page_file_mapping(page)->host;
12121da177e4SLinus Torvalds 	int		status = 0;
12131da177e4SLinus Torvalds 
121491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
121591d5b470SChuck Lever 
12166de1472fSAl Viro 	dprintk("NFS:       nfs_updatepage(%pD2 %d@%lld)\n",
12176de1472fSAl Viro 		file, count, (long long)(page_file_offset(page) + offset));
12181da177e4SLinus Torvalds 
1219c7559663SScott Mayhew 	if (nfs_can_extend_write(file, page, inode)) {
122049a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
12211da177e4SLinus Torvalds 		offset = 0;
12221da177e4SLinus Torvalds 	}
12231da177e4SLinus Torvalds 
1224e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
122503fa9e84STrond Myklebust 	if (status < 0)
122603fa9e84STrond Myklebust 		nfs_set_pageerror(page);
122759b7c05fSTrond Myklebust 	else
122859b7c05fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
12291da177e4SLinus Torvalds 
123048186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
12311da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
12321da177e4SLinus Torvalds 	return status;
12331da177e4SLinus Torvalds }
12341da177e4SLinus Torvalds 
12353ff7576dSTrond Myklebust static int flush_task_priority(int how)
12361da177e4SLinus Torvalds {
12371da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
12381da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
12391da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
12401da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
12411da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
12421da177e4SLinus Torvalds 	}
12431da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
12441da177e4SLinus Torvalds }
12451da177e4SLinus Torvalds 
12461ed26f33SAnna Schumaker static void nfs_initiate_write(struct nfs_pgio_data *data, struct rpc_message *msg,
12471ed26f33SAnna Schumaker 			       struct rpc_task_setup *task_setup_data, int how)
12481da177e4SLinus Torvalds {
1249cd841605SFred Isaman 	struct inode *inode = data->header->inode;
12503ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
12511da177e4SLinus Torvalds 
12521ed26f33SAnna Schumaker 	task_setup_data->priority = priority;
12531ed26f33SAnna Schumaker 	NFS_PROTO(inode)->write_setup(data, msg);
1254d138d5d1SAndy Adamson 
12558c21c62cSWeston Andros Adamson 	nfs4_state_protect_write(NFS_SERVER(inode)->nfs_client,
12561ed26f33SAnna Schumaker 				 &task_setup_data->rpc_client, msg, data);
1257275acaafSTrond Myklebust }
1258275acaafSTrond Myklebust 
12596d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
12606d884e8fSFred  * call this on each, which will prepare them to be retried on next
12616d884e8fSFred  * writeback using standard nfs.
12626d884e8fSFred  */
12636d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
12646d884e8fSFred {
12656d884e8fSFred 	nfs_mark_request_dirty(req);
12661d1afcbcSTrond Myklebust 	nfs_unlock_request(req);
126720633f04SWeston Andros Adamson 	nfs_end_page_writeback(req);
12683aff4ebbSTrond Myklebust 	nfs_release_request(req);
12696d884e8fSFred }
12706d884e8fSFred 
1271061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head)
12726c75dc0dSFred Isaman {
12736c75dc0dSFred Isaman 	struct nfs_page	*req;
12746c75dc0dSFred Isaman 
12756c75dc0dSFred Isaman 	while (!list_empty(head)) {
12766c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
12776c75dc0dSFred Isaman 		nfs_list_remove_request(req);
12786c75dc0dSFred Isaman 		nfs_redirty_request(req);
12796c75dc0dSFred Isaman 	}
12806c75dc0dSFred Isaman }
12816c75dc0dSFred Isaman 
1282061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1283061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1284061ae2edSFred Isaman 	.completion = nfs_write_completion,
1285061ae2edSFred Isaman };
1286061ae2edSFred Isaman 
128757208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1288a20c93e3SChristoph Hellwig 			       struct inode *inode, int ioflags, bool force_mds,
1289061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
12901751c363STrond Myklebust {
1291a20c93e3SChristoph Hellwig 	struct nfs_server *server = NFS_SERVER(inode);
129241d8d5b7SAnna Schumaker 	const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
1293a20c93e3SChristoph Hellwig 
1294a20c93e3SChristoph Hellwig #ifdef CONFIG_NFS_V4_1
1295a20c93e3SChristoph Hellwig 	if (server->pnfs_curr_ld && !force_mds)
1296a20c93e3SChristoph Hellwig 		pg_ops = server->pnfs_curr_ld->pg_write_ops;
1297a20c93e3SChristoph Hellwig #endif
12984a0de55cSAnna Schumaker 	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
12994a0de55cSAnna Schumaker 			server->wsize, ioflags);
13001751c363STrond Myklebust }
1301ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
13021751c363STrond Myklebust 
1303dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1304dce81290STrond Myklebust {
130541d8d5b7SAnna Schumaker 	pgio->pg_ops = &nfs_pgio_rw_ops;
1306dce81290STrond Myklebust 	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1307dce81290STrond Myklebust }
13081f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1309dce81290STrond Myklebust 
13101da177e4SLinus Torvalds 
13110b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
13120b7c0153SFred Isaman {
13130b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
13140b7c0153SFred Isaman 
13150b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
13160b7c0153SFred Isaman }
13170b7c0153SFred Isaman 
1318a4cdda59SAnna Schumaker static void nfs_writeback_release_common(struct nfs_pgio_data *data)
13191da177e4SLinus Torvalds {
1320cd841605SFred Isaman 	struct nfs_pgio_header *hdr = data->header;
1321e2fecb21STrond Myklebust 	int status = data->task.tk_status;
1322788e7a89STrond Myklebust 
13236c75dc0dSFred Isaman 	if ((status >= 0) && nfs_write_need_commit(data)) {
13246c75dc0dSFred Isaman 		spin_lock(&hdr->lock);
13256c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags))
13266c75dc0dSFred Isaman 			; /* Do nothing */
13276c75dc0dSFred Isaman 		else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags))
1328f79d06f5SAnna Schumaker 			memcpy(&hdr->verf, &data->verf, sizeof(hdr->verf));
1329f79d06f5SAnna Schumaker 		else if (memcmp(&hdr->verf, &data->verf, sizeof(hdr->verf)))
13306c75dc0dSFred Isaman 			set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags);
13316c75dc0dSFred Isaman 		spin_unlock(&hdr->lock);
13321da177e4SLinus Torvalds 	}
13331da177e4SLinus Torvalds }
13341da177e4SLinus Torvalds 
13351f2edbe3STrond Myklebust /*
13361f2edbe3STrond Myklebust  * Special version of should_remove_suid() that ignores capabilities.
13371f2edbe3STrond Myklebust  */
13381f2edbe3STrond Myklebust static int nfs_should_remove_suid(const struct inode *inode)
13391f2edbe3STrond Myklebust {
13401f2edbe3STrond Myklebust 	umode_t mode = inode->i_mode;
13411f2edbe3STrond Myklebust 	int kill = 0;
1342788e7a89STrond Myklebust 
13431f2edbe3STrond Myklebust 	/* suid always must be killed */
13441f2edbe3STrond Myklebust 	if (unlikely(mode & S_ISUID))
13451f2edbe3STrond Myklebust 		kill = ATTR_KILL_SUID;
13461f2edbe3STrond Myklebust 
13471f2edbe3STrond Myklebust 	/*
13481f2edbe3STrond Myklebust 	 * sgid without any exec bits is just a mandatory locking mark; leave
13491f2edbe3STrond Myklebust 	 * it alone.  If some exec bits are set, it's a real sgid; kill it.
13501f2edbe3STrond Myklebust 	 */
13511f2edbe3STrond Myklebust 	if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
13521f2edbe3STrond Myklebust 		kill |= ATTR_KILL_SGID;
13531f2edbe3STrond Myklebust 
13541f2edbe3STrond Myklebust 	if (unlikely(kill && S_ISREG(mode)))
13551f2edbe3STrond Myklebust 		return kill;
13561f2edbe3STrond Myklebust 
13571f2edbe3STrond Myklebust 	return 0;
13581f2edbe3STrond Myklebust }
1359788e7a89STrond Myklebust 
13601da177e4SLinus Torvalds /*
13611da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
13621da177e4SLinus Torvalds  */
13630eecb214SAnna Schumaker static int nfs_writeback_done(struct rpc_task *task, struct nfs_pgio_data *data,
13640eecb214SAnna Schumaker 			      struct inode *inode)
13651da177e4SLinus Torvalds {
1366788e7a89STrond Myklebust 	int status;
13671da177e4SLinus Torvalds 
1368f551e44fSChuck Lever 	/*
1369f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1370f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1371f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1372f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1373f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1374f551e44fSChuck Lever 	 */
1375cd841605SFred Isaman 	status = NFS_PROTO(inode)->write_done(task, data);
1376788e7a89STrond Myklebust 	if (status != 0)
13770eecb214SAnna Schumaker 		return status;
13780eecb214SAnna Schumaker 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, data->res.count);
137991d5b470SChuck Lever 
138089d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
13810eecb214SAnna Schumaker 	if (data->res.verf->committed < data->args.stable && task->tk_status >= 0) {
13821da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
13831da177e4SLinus Torvalds 		 * commit data to stable storage even though we
13841da177e4SLinus Torvalds 		 * requested it.
13851da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
13861da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
13871da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
13881da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
13891da177e4SLinus Torvalds 		 */
13901da177e4SLinus Torvalds 		static unsigned long    complain;
13911da177e4SLinus Torvalds 
1392a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
13931da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
13941da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
13951da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1396cd841605SFred Isaman 				NFS_SERVER(inode)->nfs_client->cl_hostname,
13970eecb214SAnna Schumaker 				data->res.verf->committed, data->args.stable);
13981da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
13991da177e4SLinus Torvalds 		}
14001da177e4SLinus Torvalds 	}
14011da177e4SLinus Torvalds #endif
14021f2edbe3STrond Myklebust 
14031f2edbe3STrond Myklebust 	/* Deal with the suid/sgid bit corner case */
14041f2edbe3STrond Myklebust 	if (nfs_should_remove_suid(inode))
14051f2edbe3STrond Myklebust 		nfs_mark_for_revalidate(inode);
14060eecb214SAnna Schumaker 	return 0;
14070eecb214SAnna Schumaker }
14080eecb214SAnna Schumaker 
14090eecb214SAnna Schumaker /*
14100eecb214SAnna Schumaker  * This function is called when the WRITE call is complete.
14110eecb214SAnna Schumaker  */
14120eecb214SAnna Schumaker static void nfs_writeback_result(struct rpc_task *task, struct nfs_pgio_data *data)
14130eecb214SAnna Schumaker {
14140eecb214SAnna Schumaker 	struct nfs_pgio_args	*argp = &data->args;
14150eecb214SAnna Schumaker 	struct nfs_pgio_res	*resp = &data->res;
14161f2edbe3STrond Myklebust 
14171f2edbe3STrond Myklebust 	if (resp->count < argp->count) {
14181da177e4SLinus Torvalds 		static unsigned long    complain;
14191da177e4SLinus Torvalds 
14206c75dc0dSFred Isaman 		/* This a short write! */
14210eecb214SAnna Schumaker 		nfs_inc_stats(data->header->inode, NFSIOS_SHORTWRITE);
142291d5b470SChuck Lever 
14231da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
14246c75dc0dSFred Isaman 		if (resp->count == 0) {
14256c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
14266c75dc0dSFred Isaman 				printk(KERN_WARNING
14276c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
14286c75dc0dSFred Isaman 				       argp->count);
14296c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
14306c75dc0dSFred Isaman 			}
14316c75dc0dSFred Isaman 			nfs_set_pgio_error(data->header, -EIO, argp->offset);
14326c75dc0dSFred Isaman 			task->tk_status = -EIO;
14336c75dc0dSFred Isaman 			return;
14346c75dc0dSFred Isaman 		}
14351da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
14361da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
14371da177e4SLinus Torvalds 			/* Resend from where the server left off */
1438a69aef14SFred Isaman 			data->mds_offset += resp->count;
14391da177e4SLinus Torvalds 			argp->offset += resp->count;
14401da177e4SLinus Torvalds 			argp->pgbase += resp->count;
14411da177e4SLinus Torvalds 			argp->count -= resp->count;
14421da177e4SLinus Torvalds 		} else {
14431da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
14441da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
14451da177e4SLinus Torvalds 			 */
14461da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
14471da177e4SLinus Torvalds 		}
1448d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
14491da177e4SLinus Torvalds 	}
14501da177e4SLinus Torvalds }
14511da177e4SLinus Torvalds 
14521da177e4SLinus Torvalds 
145389d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
145471d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
145571d0a611STrond Myklebust {
1456b8413f98STrond Myklebust 	int ret;
1457b8413f98STrond Myklebust 
145871d0a611STrond Myklebust 	if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
145971d0a611STrond Myklebust 		return 1;
1460b8413f98STrond Myklebust 	if (!may_wait)
146171d0a611STrond Myklebust 		return 0;
1462b8413f98STrond Myklebust 	ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1463b8413f98STrond Myklebust 				NFS_INO_COMMIT,
1464b8413f98STrond Myklebust 				nfs_wait_bit_killable,
1465b8413f98STrond Myklebust 				TASK_KILLABLE);
1466b8413f98STrond Myklebust 	return (ret < 0) ? ret : 1;
146771d0a611STrond Myklebust }
146871d0a611STrond Myklebust 
1469f453a54aSFred Isaman static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
147071d0a611STrond Myklebust {
147171d0a611STrond Myklebust 	clear_bit(NFS_INO_COMMIT, &nfsi->flags);
14724e857c58SPeter Zijlstra 	smp_mb__after_atomic();
147371d0a611STrond Myklebust 	wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
147471d0a611STrond Myklebust }
147571d0a611STrond Myklebust 
14760b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
14771da177e4SLinus Torvalds {
14780b7c0153SFred Isaman 	put_nfs_open_context(data->context);
14790b7c0153SFred Isaman 	nfs_commit_free(data);
14801da177e4SLinus Torvalds }
1481e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
14821da177e4SLinus Torvalds 
14830b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
14849ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
14859f0ec176SAndy Adamson 			int how, int flags)
14861da177e4SLinus Torvalds {
148707737691STrond Myklebust 	struct rpc_task *task;
14889ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1489bdc7f021STrond Myklebust 	struct rpc_message msg = {
1490bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1491bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
14929ace33cdSFred Isaman 		.rpc_cred = data->cred,
1493bdc7f021STrond Myklebust 	};
149484115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
149507737691STrond Myklebust 		.task = &data->task,
14969ace33cdSFred Isaman 		.rpc_client = clnt,
1497bdc7f021STrond Myklebust 		.rpc_message = &msg,
14989ace33cdSFred Isaman 		.callback_ops = call_ops,
149984115e1cSTrond Myklebust 		.callback_data = data,
1500101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
15019f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
15023ff7576dSTrond Myklebust 		.priority = priority,
150384115e1cSTrond Myklebust 	};
1504788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
15059ace33cdSFred Isaman 	NFS_PROTO(data->inode)->commit_setup(data, &msg);
15061da177e4SLinus Torvalds 
1507a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1508bdc7f021STrond Myklebust 
15098c21c62cSWeston Andros Adamson 	nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client,
15108c21c62cSWeston Andros Adamson 		NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg);
15118c21c62cSWeston Andros Adamson 
151207737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1513dbae4c73STrond Myklebust 	if (IS_ERR(task))
1514dbae4c73STrond Myklebust 		return PTR_ERR(task);
1515d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1516d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
151707737691STrond Myklebust 	rpc_put_task(task);
1518dbae4c73STrond Myklebust 	return 0;
15191da177e4SLinus Torvalds }
1520e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds /*
15239ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
15249ace33cdSFred Isaman  */
15250b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1526988b6dceSFred Isaman 		     struct list_head *head,
1527f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1528f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
15299ace33cdSFred Isaman {
15309ace33cdSFred Isaman 	struct nfs_page *first = nfs_list_entry(head->next);
15313d4ff43dSAl Viro 	struct inode *inode = first->wb_context->dentry->d_inode;
15329ace33cdSFred Isaman 
15339ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
15349ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
15359ace33cdSFred Isaman 
15369ace33cdSFred Isaman 	list_splice_init(head, &data->pages);
15379ace33cdSFred Isaman 
15389ace33cdSFred Isaman 	data->inode	  = inode;
15399ace33cdSFred Isaman 	data->cred	  = first->wb_context->cred;
1540988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
15419ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1542f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1543b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
15449ace33cdSFred Isaman 
15459ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
15469ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
15479ace33cdSFred Isaman 	data->args.offset = 0;
15489ace33cdSFred Isaman 	data->args.count  = 0;
15490b7c0153SFred Isaman 	data->context     = get_nfs_open_context(first->wb_context);
15509ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
15519ace33cdSFred Isaman 	data->res.verf    = &data->verf;
15529ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
15539ace33cdSFred Isaman }
1554e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
15559ace33cdSFred Isaman 
1556e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1557ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1558ea2cf228SFred Isaman 		      struct nfs_commit_info *cinfo)
155964bfeb49SFred Isaman {
156064bfeb49SFred Isaman 	struct nfs_page *req;
156164bfeb49SFred Isaman 
156264bfeb49SFred Isaman 	while (!list_empty(page_list)) {
156364bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
156464bfeb49SFred Isaman 		nfs_list_remove_request(req);
1565ea2cf228SFred Isaman 		nfs_mark_request_commit(req, lseg, cinfo);
156656f9cd68SFred Isaman 		if (!cinfo->dreq) {
156764bfeb49SFred Isaman 			dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1568d56b4ddfSMel Gorman 			dec_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
156964bfeb49SFred Isaman 				     BDI_RECLAIMABLE);
157056f9cd68SFred Isaman 		}
15711d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
157264bfeb49SFred Isaman 	}
157364bfeb49SFred Isaman }
1574e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
157564bfeb49SFred Isaman 
15769ace33cdSFred Isaman /*
15771da177e4SLinus Torvalds  * Commit dirty pages
15781da177e4SLinus Torvalds  */
15791da177e4SLinus Torvalds static int
1580ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1581ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
15821da177e4SLinus Torvalds {
15830b7c0153SFred Isaman 	struct nfs_commit_data	*data;
15841da177e4SLinus Torvalds 
1585c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
15861da177e4SLinus Torvalds 
15871da177e4SLinus Torvalds 	if (!data)
15881da177e4SLinus Torvalds 		goto out_bad;
15891da177e4SLinus Torvalds 
15901da177e4SLinus Torvalds 	/* Set up the argument struct */
1591f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
1592f453a54aSFred Isaman 	atomic_inc(&cinfo->mds->rpcs_out);
15939f0ec176SAndy Adamson 	return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops,
15949f0ec176SAndy Adamson 				   how, 0);
15951da177e4SLinus Torvalds  out_bad:
1596ea2cf228SFred Isaman 	nfs_retry_commit(head, NULL, cinfo);
1597f453a54aSFred Isaman 	cinfo->completion_ops->error_cleanup(NFS_I(inode));
15981da177e4SLinus Torvalds 	return -ENOMEM;
15991da177e4SLinus Torvalds }
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds /*
16021da177e4SLinus Torvalds  * COMMIT call returned
16031da177e4SLinus Torvalds  */
1604788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
16051da177e4SLinus Torvalds {
16060b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
16071da177e4SLinus Torvalds 
1608a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
16091da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
16101da177e4SLinus Torvalds 
1611788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1612c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
1613c9d8f89dSTrond Myklebust }
1614c9d8f89dSTrond Myklebust 
1615f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1616c9d8f89dSTrond Myklebust {
1617c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1618c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1619f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
1620788e7a89STrond Myklebust 
16211da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
16221da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
16231da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1624d6d6dc7cSFred Isaman 		nfs_clear_page_commit(req->wb_page);
16251da177e4SLinus Torvalds 
16261e8968c5SNiels de Vos 		dprintk("NFS:       commit (%s/%llu %d@%lld)",
16273d4ff43dSAl Viro 			req->wb_context->dentry->d_sb->s_id,
16281e8968c5SNiels de Vos 			(unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
16291da177e4SLinus Torvalds 			req->wb_bytes,
16301da177e4SLinus Torvalds 			(long long)req_offset(req));
1631c9d8f89dSTrond Myklebust 		if (status < 0) {
1632c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
16331da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1634c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
16351da177e4SLinus Torvalds 			goto next;
16361da177e4SLinus Torvalds 		}
16371da177e4SLinus Torvalds 
16381da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
16391da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
16402f2c63bcSTrond Myklebust 		if (!memcmp(&req->wb_verf, &data->verf.verifier, sizeof(req->wb_verf))) {
16411da177e4SLinus Torvalds 			/* We have a match */
16421da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
16431da177e4SLinus Torvalds 			dprintk(" OK\n");
16441da177e4SLinus Torvalds 			goto next;
16451da177e4SLinus Torvalds 		}
16461da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
16471da177e4SLinus Torvalds 		dprintk(" mismatch\n");
16486d884e8fSFred 		nfs_mark_request_dirty(req);
164905990d1bSTrond Myklebust 		set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
16501da177e4SLinus Torvalds 	next:
16511d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
16521da177e4SLinus Torvalds 	}
1653f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1654f453a54aSFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
1655f453a54aSFred Isaman 		nfs_commit_clear_lock(NFS_I(data->inode));
16565917ce84SFred Isaman }
16575917ce84SFred Isaman 
16585917ce84SFred Isaman static void nfs_commit_release(void *calldata)
16595917ce84SFred Isaman {
16600b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
16615917ce84SFred Isaman 
1662f453a54aSFred Isaman 	data->completion_ops->completion(data);
1663c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
16641da177e4SLinus Torvalds }
1665788e7a89STrond Myklebust 
1666788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
16670b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1668788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1669788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1670788e7a89STrond Myklebust };
16711da177e4SLinus Torvalds 
1672f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1673f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1674f453a54aSFred Isaman 	.error_cleanup = nfs_commit_clear_lock,
1675f453a54aSFred Isaman };
1676f453a54aSFred Isaman 
16771763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1678ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
167984c53ab5SFred Isaman {
168084c53ab5SFred Isaman 	int status;
168184c53ab5SFred Isaman 
1682ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
168384c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1684ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
168584c53ab5SFred Isaman 	return status;
168684c53ab5SFred Isaman }
168784c53ab5SFred Isaman 
1688b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
16891da177e4SLinus Torvalds {
16901da177e4SLinus Torvalds 	LIST_HEAD(head);
1691ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
169271d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1693b8413f98STrond Myklebust 	int res;
16941da177e4SLinus Torvalds 
1695b8413f98STrond Myklebust 	res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1696b8413f98STrond Myklebust 	if (res <= 0)
1697c5efa5fcSTrond Myklebust 		goto out_mark_dirty;
1698ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1699ea2cf228SFred Isaman 	res = nfs_scan_commit(inode, &head, &cinfo);
17001da177e4SLinus Torvalds 	if (res) {
1701a861a1e1SFred Isaman 		int error;
1702a861a1e1SFred Isaman 
1703ea2cf228SFred Isaman 		error = nfs_generic_commit_list(inode, &head, how, &cinfo);
17041da177e4SLinus Torvalds 		if (error < 0)
17051da177e4SLinus Torvalds 			return error;
1706b8413f98STrond Myklebust 		if (!may_wait)
1707b8413f98STrond Myklebust 			goto out_mark_dirty;
1708b8413f98STrond Myklebust 		error = wait_on_bit(&NFS_I(inode)->flags,
1709b8413f98STrond Myklebust 				NFS_INO_COMMIT,
171071d0a611STrond Myklebust 				nfs_wait_bit_killable,
171171d0a611STrond Myklebust 				TASK_KILLABLE);
1712b8413f98STrond Myklebust 		if (error < 0)
1713b8413f98STrond Myklebust 			return error;
171471d0a611STrond Myklebust 	} else
171571d0a611STrond Myklebust 		nfs_commit_clear_lock(NFS_I(inode));
1716c5efa5fcSTrond Myklebust 	return res;
1717c5efa5fcSTrond Myklebust 	/* Note: If we exit without ensuring that the commit is complete,
1718c5efa5fcSTrond Myklebust 	 * we must mark the inode as dirty. Otherwise, future calls to
1719c5efa5fcSTrond Myklebust 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1720c5efa5fcSTrond Myklebust 	 * that the data is on the disk.
1721c5efa5fcSTrond Myklebust 	 */
1722c5efa5fcSTrond Myklebust out_mark_dirty:
1723c5efa5fcSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
17241da177e4SLinus Torvalds 	return res;
17251da177e4SLinus Torvalds }
17268fc795f7STrond Myklebust 
17278fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
17288fc795f7STrond Myklebust {
1729420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1730420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1731420e3646STrond Myklebust 	int ret = 0;
17328fc795f7STrond Myklebust 
17333236c3e1SJeff Layton 	/* no commits means nothing needs to be done */
1734ea2cf228SFred Isaman 	if (!nfsi->commit_info.ncommit)
17353236c3e1SJeff Layton 		return ret;
17363236c3e1SJeff Layton 
1737a00dd6c0SJeff Layton 	if (wbc->sync_mode == WB_SYNC_NONE) {
1738a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1739a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1740420e3646STrond Myklebust 		 */
1741ea2cf228SFred Isaman 		if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1))
1742420e3646STrond Myklebust 			goto out_mark_dirty;
1743420e3646STrond Myklebust 
1744a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1745420e3646STrond Myklebust 		flags = 0;
1746a00dd6c0SJeff Layton 	}
1747a00dd6c0SJeff Layton 
1748420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1749420e3646STrond Myklebust 	if (ret >= 0) {
1750420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1751420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1752420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1753420e3646STrond Myklebust 			else
1754420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1755420e3646STrond Myklebust 		}
17568fc795f7STrond Myklebust 		return 0;
1757420e3646STrond Myklebust 	}
1758420e3646STrond Myklebust out_mark_dirty:
17598fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
17608fc795f7STrond Myklebust 	return ret;
17618fc795f7STrond Myklebust }
1762c63c7b05STrond Myklebust #else
17638fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
17648fc795f7STrond Myklebust {
17658fc795f7STrond Myklebust 	return 0;
17668fc795f7STrond Myklebust }
17671da177e4SLinus Torvalds #endif
17681da177e4SLinus Torvalds 
17698fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
17708fc795f7STrond Myklebust {
1771a8d8f02cSBryan Schumaker 	return nfs_commit_unstable_pages(inode, wbc);
1772a8d8f02cSBryan Schumaker }
177389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode);
1774863a3c6cSAndy Adamson 
1775acdc53b2STrond Myklebust /*
1776acdc53b2STrond Myklebust  * flush the inode to disk.
1777acdc53b2STrond Myklebust  */
1778acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
177934901f70STrond Myklebust {
178034901f70STrond Myklebust 	struct writeback_control wbc = {
178172cb77f4STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
178234901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
1783d7fb1207STrond Myklebust 		.range_start = 0,
1784d7fb1207STrond Myklebust 		.range_end = LLONG_MAX,
178534901f70STrond Myklebust 	};
1786f4ce1299STrond Myklebust 	int ret;
178734901f70STrond Myklebust 
1788f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_enter(inode);
1789f4ce1299STrond Myklebust 
1790f4ce1299STrond Myklebust 	ret = sync_inode(inode, &wbc);
1791f4ce1299STrond Myklebust 
1792f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_exit(inode, ret);
1793f4ce1299STrond Myklebust 	return ret;
17941c75950bSTrond Myklebust }
1795ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all);
17961c75950bSTrond Myklebust 
17971b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
17981b3b4a1aSTrond Myklebust {
17991b3b4a1aSTrond Myklebust 	struct nfs_page *req;
18001b3b4a1aSTrond Myklebust 	int ret = 0;
18011b3b4a1aSTrond Myklebust 
18021b3b4a1aSTrond Myklebust 	for (;;) {
1803ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
180484d3a9a9SWeston Andros Adamson 		req = nfs_page_find_head_request(page);
18051b3b4a1aSTrond Myklebust 		if (req == NULL)
18061b3b4a1aSTrond Myklebust 			break;
18077ad84aa9STrond Myklebust 		if (nfs_lock_request(req)) {
18088dd37758STrond Myklebust 			nfs_clear_request_commit(req);
18091b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
18101b3b4a1aSTrond Myklebust 			/*
18111b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
18121b3b4a1aSTrond Myklebust 			 * page as being dirty
18131b3b4a1aSTrond Myklebust 			 */
18141b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
18151d1afcbcSTrond Myklebust 			nfs_unlock_and_release_request(req);
18161b3b4a1aSTrond Myklebust 			break;
18171b3b4a1aSTrond Myklebust 		}
18181b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
1819c9edda71STrond Myklebust 		nfs_release_request(req);
18201b3b4a1aSTrond Myklebust 		if (ret < 0)
1821c988950eSTrond Myklebust 			break;
18221b3b4a1aSTrond Myklebust 	}
18231b3b4a1aSTrond Myklebust 	return ret;
18241b3b4a1aSTrond Myklebust }
18251b3b4a1aSTrond Myklebust 
18261c75950bSTrond Myklebust /*
18271c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
18281c75950bSTrond Myklebust  */
18291c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
18301c75950bSTrond Myklebust {
183129418aa4SMel Gorman 	loff_t range_start = page_file_offset(page);
18327f2f12d9STrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
18337f2f12d9STrond Myklebust 	struct writeback_control wbc = {
18347f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
18357f2f12d9STrond Myklebust 		.nr_to_write = 0,
18367f2f12d9STrond Myklebust 		.range_start = range_start,
18377f2f12d9STrond Myklebust 		.range_end = range_end,
18387f2f12d9STrond Myklebust 	};
18397f2f12d9STrond Myklebust 	int ret;
18407f2f12d9STrond Myklebust 
1841f4ce1299STrond Myklebust 	trace_nfs_writeback_page_enter(inode);
1842f4ce1299STrond Myklebust 
18430522f6adSTrond Myklebust 	for (;;) {
1844ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
18457f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
18467f2f12d9STrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
18477f2f12d9STrond Myklebust 			if (ret < 0)
18487f2f12d9STrond Myklebust 				goto out_error;
18490522f6adSTrond Myklebust 			continue;
18507f2f12d9STrond Myklebust 		}
1851f4ce1299STrond Myklebust 		ret = 0;
18520522f6adSTrond Myklebust 		if (!PagePrivate(page))
18530522f6adSTrond Myklebust 			break;
18540522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
18557f2f12d9STrond Myklebust 		if (ret < 0)
18567f2f12d9STrond Myklebust 			goto out_error;
18577f2f12d9STrond Myklebust 	}
18587f2f12d9STrond Myklebust out_error:
1859f4ce1299STrond Myklebust 	trace_nfs_writeback_page_exit(inode, ret);
18607f2f12d9STrond Myklebust 	return ret;
18611c75950bSTrond Myklebust }
18621c75950bSTrond Myklebust 
1863074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
1864074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1865a6bc32b8SMel Gorman 		struct page *page, enum migrate_mode mode)
1866074cc1deSTrond Myklebust {
18672da95652SJeff Layton 	/*
18682da95652SJeff Layton 	 * If PagePrivate is set, then the page is currently associated with
18692da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
18702da95652SJeff Layton 	 *
18712da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
18722da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
18732da95652SJeff Layton 	 *        the page lock.
18742da95652SJeff Layton 	 */
18752da95652SJeff Layton 	if (PagePrivate(page))
18762da95652SJeff Layton 		return -EBUSY;
1877074cc1deSTrond Myklebust 
18788c209ce7SDavid Howells 	if (!nfs_fscache_release_page(page, GFP_KERNEL))
18798c209ce7SDavid Howells 		return -EBUSY;
1880074cc1deSTrond Myklebust 
1881a6bc32b8SMel Gorman 	return migrate_page(mapping, newpage, page, mode);
1882074cc1deSTrond Myklebust }
1883074cc1deSTrond Myklebust #endif
1884074cc1deSTrond Myklebust 
1885f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
18861da177e4SLinus Torvalds {
18871da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1888c0752cdfSAnna Schumaker 					     sizeof(struct nfs_rw_header),
18891da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
189020c2df83SPaul Mundt 					     NULL);
18911da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
18921da177e4SLinus Torvalds 		return -ENOMEM;
18931da177e4SLinus Torvalds 
189493d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
18951da177e4SLinus Torvalds 						     nfs_wdata_cachep);
18961da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
18973dd4765fSJeff Layton 		goto out_destroy_write_cache;
18981da177e4SLinus Torvalds 
18990b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
19000b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
19010b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
19020b7c0153SFred Isaman 					     NULL);
19030b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
19043dd4765fSJeff Layton 		goto out_destroy_write_mempool;
19050b7c0153SFred Isaman 
190693d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
19074c100210SYanchuan Nian 						      nfs_cdata_cachep);
19081da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
19093dd4765fSJeff Layton 		goto out_destroy_commit_cache;
19101da177e4SLinus Torvalds 
191189a09141SPeter Zijlstra 	/*
191289a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
191389a09141SPeter Zijlstra 	 *
191489a09141SPeter Zijlstra 	 *  64MB:    8192k
191589a09141SPeter Zijlstra 	 * 128MB:   11585k
191689a09141SPeter Zijlstra 	 * 256MB:   16384k
191789a09141SPeter Zijlstra 	 * 512MB:   23170k
191889a09141SPeter Zijlstra 	 *   1GB:   32768k
191989a09141SPeter Zijlstra 	 *   2GB:   46340k
192089a09141SPeter Zijlstra 	 *   4GB:   65536k
192189a09141SPeter Zijlstra 	 *   8GB:   92681k
192289a09141SPeter Zijlstra 	 *  16GB:  131072k
192389a09141SPeter Zijlstra 	 *
192489a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
192589a09141SPeter Zijlstra 	 * Limit the default to 256M
192689a09141SPeter Zijlstra 	 */
192789a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
192889a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
192989a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
193089a09141SPeter Zijlstra 
19311da177e4SLinus Torvalds 	return 0;
19323dd4765fSJeff Layton 
19333dd4765fSJeff Layton out_destroy_commit_cache:
19343dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
19353dd4765fSJeff Layton out_destroy_write_mempool:
19363dd4765fSJeff Layton 	mempool_destroy(nfs_wdata_mempool);
19373dd4765fSJeff Layton out_destroy_write_cache:
19383dd4765fSJeff Layton 	kmem_cache_destroy(nfs_wdata_cachep);
19393dd4765fSJeff Layton 	return -ENOMEM;
19401da177e4SLinus Torvalds }
19411da177e4SLinus Torvalds 
1942266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
19431da177e4SLinus Torvalds {
19441da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
19453dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
19461da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
19471a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
19481da177e4SLinus Torvalds }
19491da177e4SLinus Torvalds 
19504a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops = {
1951a4cdda59SAnna Schumaker 	.rw_mode		= FMODE_WRITE,
19524a0de55cSAnna Schumaker 	.rw_alloc_header	= nfs_writehdr_alloc,
19534a0de55cSAnna Schumaker 	.rw_free_header		= nfs_writehdr_free,
1954a4cdda59SAnna Schumaker 	.rw_release		= nfs_writeback_release_common,
19550eecb214SAnna Schumaker 	.rw_done		= nfs_writeback_done,
19560eecb214SAnna Schumaker 	.rw_result		= nfs_writeback_result,
19571ed26f33SAnna Schumaker 	.rw_initiate		= nfs_initiate_write,
19584a0de55cSAnna Schumaker };
1959