xref: /linux/fs/nfs/write.c (revision 743162013d40ca612b4cb53d3a200dff2d9ab26e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/fs/nfs/write.c
31da177e4SLinus Torvalds  *
47c85d900STrond Myklebust  * Write file data over NFS.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/types.h>
101da177e4SLinus Torvalds #include <linux/slab.h>
111da177e4SLinus Torvalds #include <linux/mm.h>
121da177e4SLinus Torvalds #include <linux/pagemap.h>
131da177e4SLinus Torvalds #include <linux/file.h>
141da177e4SLinus Torvalds #include <linux/writeback.h>
1589a09141SPeter Zijlstra #include <linux/swap.h>
16074cc1deSTrond Myklebust #include <linux/migrate.h>
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
191da177e4SLinus Torvalds #include <linux/nfs_fs.h>
201da177e4SLinus Torvalds #include <linux/nfs_mount.h>
211da177e4SLinus Torvalds #include <linux/nfs_page.h>
223fcfab16SAndrew Morton #include <linux/backing-dev.h>
23afeacc8cSPaul Gortmaker #include <linux/export.h>
243fcfab16SAndrew Morton 
251da177e4SLinus Torvalds #include <asm/uaccess.h>
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #include "delegation.h"
2849a70f27STrond Myklebust #include "internal.h"
2991d5b470SChuck Lever #include "iostat.h"
30def6ed7eSAndy Adamson #include "nfs4_fs.h"
31074cc1deSTrond Myklebust #include "fscache.h"
3294ad1c80SFred Isaman #include "pnfs.h"
331da177e4SLinus Torvalds 
34f4ce1299STrond Myklebust #include "nfstrace.h"
35f4ce1299STrond Myklebust 
361da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #define MIN_POOL_WRITE		(32)
391da177e4SLinus Torvalds #define MIN_POOL_COMMIT		(4)
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds /*
421da177e4SLinus Torvalds  * Local function declarations
431da177e4SLinus Torvalds  */
44f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req);
45788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops;
46061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops;
47f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops;
484a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops;
491da177e4SLinus Torvalds 
50e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep;
513feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
520b7c0153SFred Isaman static struct kmem_cache *nfs_cdata_cachep;
531da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
541da177e4SLinus Torvalds 
550b7c0153SFred Isaman struct nfs_commit_data *nfs_commitdata_alloc(void)
561da177e4SLinus Torvalds {
57192e501bSMel Gorman 	struct nfs_commit_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOIO);
5840859d7eSChuck Lever 
591da177e4SLinus Torvalds 	if (p) {
601da177e4SLinus Torvalds 		memset(p, 0, sizeof(*p));
611da177e4SLinus Torvalds 		INIT_LIST_HEAD(&p->pages);
621da177e4SLinus Torvalds 	}
631da177e4SLinus Torvalds 	return p;
641da177e4SLinus Torvalds }
65e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
661da177e4SLinus Torvalds 
670b7c0153SFred Isaman void nfs_commit_free(struct nfs_commit_data *p)
681da177e4SLinus Torvalds {
691da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
701da177e4SLinus Torvalds }
71e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free);
721da177e4SLinus Torvalds 
734a0de55cSAnna Schumaker static struct nfs_rw_header *nfs_writehdr_alloc(void)
743feb2d49STrond Myklebust {
75c0752cdfSAnna Schumaker 	struct nfs_rw_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOIO);
763feb2d49STrond Myklebust 
774a0de55cSAnna Schumaker 	if (p)
783feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
793feb2d49STrond Myklebust 	return p;
803feb2d49STrond Myklebust }
813feb2d49STrond Myklebust 
824a0de55cSAnna Schumaker static void nfs_writehdr_free(struct nfs_rw_header *whdr)
836c75dc0dSFred Isaman {
84cd841605SFred Isaman 	mempool_free(whdr, nfs_wdata_mempool);
853feb2d49STrond Myklebust }
861da177e4SLinus Torvalds 
877b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
887b159fc1STrond Myklebust {
897b159fc1STrond Myklebust 	ctx->error = error;
907b159fc1STrond Myklebust 	smp_wmb();
917b159fc1STrond Myklebust 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
927b159fc1STrond Myklebust }
937b159fc1STrond Myklebust 
9429418aa4SMel Gorman static struct nfs_page *
9529418aa4SMel Gorman nfs_page_find_request_locked(struct nfs_inode *nfsi, struct page *page)
96277459d2STrond Myklebust {
97277459d2STrond Myklebust 	struct nfs_page *req = NULL;
98277459d2STrond Myklebust 
9929418aa4SMel Gorman 	if (PagePrivate(page))
100277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
10129418aa4SMel Gorman 	else if (unlikely(PageSwapCache(page))) {
10229418aa4SMel Gorman 		struct nfs_page *freq, *t;
10329418aa4SMel Gorman 
10429418aa4SMel Gorman 		/* Linearly search the commit list for the correct req */
10529418aa4SMel Gorman 		list_for_each_entry_safe(freq, t, &nfsi->commit_info.list, wb_list) {
10629418aa4SMel Gorman 			if (freq->wb_page == page) {
10729418aa4SMel Gorman 				req = freq;
10829418aa4SMel Gorman 				break;
109277459d2STrond Myklebust 			}
11029418aa4SMel Gorman 		}
11129418aa4SMel Gorman 	}
11229418aa4SMel Gorman 
11329418aa4SMel Gorman 	if (req)
11429418aa4SMel Gorman 		kref_get(&req->wb_kref);
11529418aa4SMel Gorman 
116277459d2STrond Myklebust 	return req;
117277459d2STrond Myklebust }
118277459d2STrond Myklebust 
119277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page)
120277459d2STrond Myklebust {
121d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
122277459d2STrond Myklebust 	struct nfs_page *req = NULL;
123277459d2STrond Myklebust 
124587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
12529418aa4SMel Gorman 	req = nfs_page_find_request_locked(NFS_I(inode), page);
126587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
127277459d2STrond Myklebust 	return req;
128277459d2STrond Myklebust }
129277459d2STrond Myklebust 
1301da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1311da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1321da177e4SLinus Torvalds {
133d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
134a3d01454STrond Myklebust 	loff_t end, i_size;
135a3d01454STrond Myklebust 	pgoff_t end_index;
1361da177e4SLinus Torvalds 
137a3d01454STrond Myklebust 	spin_lock(&inode->i_lock);
138a3d01454STrond Myklebust 	i_size = i_size_read(inode);
139a3d01454STrond Myklebust 	end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
140d56b4ddfSMel Gorman 	if (i_size > 0 && page_file_index(page) < end_index)
141a3d01454STrond Myklebust 		goto out;
142d56b4ddfSMel Gorman 	end = page_file_offset(page) + ((loff_t)offset+count);
1431da177e4SLinus Torvalds 	if (i_size >= end)
144a3d01454STrond Myklebust 		goto out;
1451da177e4SLinus Torvalds 	i_size_write(inode, end);
146a3d01454STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
147a3d01454STrond Myklebust out:
148a3d01454STrond Myklebust 	spin_unlock(&inode->i_lock);
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
151a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
152a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
153a301b777STrond Myklebust {
154d56b4ddfSMel Gorman 	nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page));
155a301b777STrond Myklebust }
156a301b777STrond Myklebust 
157d72ddcbaSWeston Andros Adamson /*
158d72ddcbaSWeston Andros Adamson  * nfs_page_group_search_locked
159d72ddcbaSWeston Andros Adamson  * @head - head request of page group
160d72ddcbaSWeston Andros Adamson  * @page_offset - offset into page
161d72ddcbaSWeston Andros Adamson  *
162d72ddcbaSWeston Andros Adamson  * Search page group with head @head to find a request that contains the
163d72ddcbaSWeston Andros Adamson  * page offset @page_offset.
164d72ddcbaSWeston Andros Adamson  *
165d72ddcbaSWeston Andros Adamson  * Returns a pointer to the first matching nfs request, or NULL if no
166d72ddcbaSWeston Andros Adamson  * match is found.
167d72ddcbaSWeston Andros Adamson  *
168d72ddcbaSWeston Andros Adamson  * Must be called with the page group lock held
169d72ddcbaSWeston Andros Adamson  */
170d72ddcbaSWeston Andros Adamson static struct nfs_page *
171d72ddcbaSWeston Andros Adamson nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset)
172d72ddcbaSWeston Andros Adamson {
173d72ddcbaSWeston Andros Adamson 	struct nfs_page *req;
174d72ddcbaSWeston Andros Adamson 
175d72ddcbaSWeston Andros Adamson 	WARN_ON_ONCE(head != head->wb_head);
176d72ddcbaSWeston Andros Adamson 	WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_head->wb_flags));
177d72ddcbaSWeston Andros Adamson 
178d72ddcbaSWeston Andros Adamson 	req = head;
179d72ddcbaSWeston Andros Adamson 	do {
180d72ddcbaSWeston Andros Adamson 		if (page_offset >= req->wb_pgbase &&
181d72ddcbaSWeston Andros Adamson 		    page_offset < (req->wb_pgbase + req->wb_bytes))
182d72ddcbaSWeston Andros Adamson 			return req;
183d72ddcbaSWeston Andros Adamson 
184d72ddcbaSWeston Andros Adamson 		req = req->wb_this_page;
185d72ddcbaSWeston Andros Adamson 	} while (req != head);
186d72ddcbaSWeston Andros Adamson 
187d72ddcbaSWeston Andros Adamson 	return NULL;
188d72ddcbaSWeston Andros Adamson }
189d72ddcbaSWeston Andros Adamson 
190d72ddcbaSWeston Andros Adamson /*
191d72ddcbaSWeston Andros Adamson  * nfs_page_group_covers_page
192d72ddcbaSWeston Andros Adamson  * @head - head request of page group
193d72ddcbaSWeston Andros Adamson  *
194d72ddcbaSWeston Andros Adamson  * Return true if the page group with head @head covers the whole page,
195d72ddcbaSWeston Andros Adamson  * returns false otherwise
196d72ddcbaSWeston Andros Adamson  */
197d72ddcbaSWeston Andros Adamson static bool nfs_page_group_covers_page(struct nfs_page *req)
198d72ddcbaSWeston Andros Adamson {
199d72ddcbaSWeston Andros Adamson 	struct nfs_page *tmp;
200d72ddcbaSWeston Andros Adamson 	unsigned int pos = 0;
201d72ddcbaSWeston Andros Adamson 	unsigned int len = nfs_page_length(req->wb_page);
202d72ddcbaSWeston Andros Adamson 
203d72ddcbaSWeston Andros Adamson 	nfs_page_group_lock(req);
204d72ddcbaSWeston Andros Adamson 
205d72ddcbaSWeston Andros Adamson 	do {
206d72ddcbaSWeston Andros Adamson 		tmp = nfs_page_group_search_locked(req->wb_head, pos);
207d72ddcbaSWeston Andros Adamson 		if (tmp) {
208d72ddcbaSWeston Andros Adamson 			/* no way this should happen */
209d72ddcbaSWeston Andros Adamson 			WARN_ON_ONCE(tmp->wb_pgbase != pos);
210d72ddcbaSWeston Andros Adamson 			pos += tmp->wb_bytes - (pos - tmp->wb_pgbase);
211d72ddcbaSWeston Andros Adamson 		}
212d72ddcbaSWeston Andros Adamson 	} while (tmp && pos < len);
213d72ddcbaSWeston Andros Adamson 
214d72ddcbaSWeston Andros Adamson 	nfs_page_group_unlock(req);
215d72ddcbaSWeston Andros Adamson 	WARN_ON_ONCE(pos > len);
216d72ddcbaSWeston Andros Adamson 	return pos == len;
217d72ddcbaSWeston Andros Adamson }
218d72ddcbaSWeston Andros Adamson 
2191da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
2201da177e4SLinus Torvalds  * covers the full page.
2211da177e4SLinus Torvalds  */
222d72ddcbaSWeston Andros Adamson static void nfs_mark_uptodate(struct nfs_page *req)
2231da177e4SLinus Torvalds {
224d72ddcbaSWeston Andros Adamson 	if (PageUptodate(req->wb_page))
2251da177e4SLinus Torvalds 		return;
226d72ddcbaSWeston Andros Adamson 	if (!nfs_page_group_covers_page(req))
2271da177e4SLinus Torvalds 		return;
228d72ddcbaSWeston Andros Adamson 	SetPageUptodate(req->wb_page);
2291da177e4SLinus Torvalds }
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
2321da177e4SLinus Torvalds {
2331da177e4SLinus Torvalds 	if (wbc->for_reclaim)
234c63c7b05STrond Myklebust 		return FLUSH_HIGHPRI | FLUSH_STABLE;
235b17621feSWu Fengguang 	if (wbc->for_kupdate || wbc->for_background)
236b31268acSTrond Myklebust 		return FLUSH_LOWPRI | FLUSH_COND_STABLE;
237b31268acSTrond Myklebust 	return FLUSH_COND_STABLE;
2381da177e4SLinus Torvalds }
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds /*
24189a09141SPeter Zijlstra  * NFS congestion control
24289a09141SPeter Zijlstra  */
24389a09141SPeter Zijlstra 
24489a09141SPeter Zijlstra int nfs_congestion_kb;
24589a09141SPeter Zijlstra 
24689a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
24789a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
24889a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
24989a09141SPeter Zijlstra 
250deed85e7STrond Myklebust static void nfs_set_page_writeback(struct page *page)
25189a09141SPeter Zijlstra {
252deed85e7STrond Myklebust 	struct nfs_server *nfss = NFS_SERVER(page_file_mapping(page)->host);
2535a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
2545a6d41b3STrond Myklebust 
255deed85e7STrond Myklebust 	WARN_ON_ONCE(ret != 0);
25689a09141SPeter Zijlstra 
257277866a0SPeter Zijlstra 	if (atomic_long_inc_return(&nfss->writeback) >
2588aa7e847SJens Axboe 			NFS_CONGESTION_ON_THRESH) {
2598aa7e847SJens Axboe 		set_bdi_congested(&nfss->backing_dev_info,
2608aa7e847SJens Axboe 					BLK_RW_ASYNC);
2618aa7e847SJens Axboe 	}
26289a09141SPeter Zijlstra }
26389a09141SPeter Zijlstra 
26420633f04SWeston Andros Adamson static void nfs_end_page_writeback(struct nfs_page *req)
26589a09141SPeter Zijlstra {
26620633f04SWeston Andros Adamson 	struct inode *inode = page_file_mapping(req->wb_page)->host;
26789a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
26889a09141SPeter Zijlstra 
26920633f04SWeston Andros Adamson 	if (!nfs_page_group_sync_on_bit(req, PG_WB_END))
27020633f04SWeston Andros Adamson 		return;
27120633f04SWeston Andros Adamson 
27220633f04SWeston Andros Adamson 	end_page_writeback(req->wb_page);
273c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
2748aa7e847SJens Axboe 		clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
27589a09141SPeter Zijlstra }
27689a09141SPeter Zijlstra 
277cfb506e1STrond Myklebust static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock)
278e261f51fSTrond Myklebust {
279d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
280e261f51fSTrond Myklebust 	struct nfs_page *req;
281e261f51fSTrond Myklebust 	int ret;
282e261f51fSTrond Myklebust 
283587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
284e261f51fSTrond Myklebust 	for (;;) {
28529418aa4SMel Gorman 		req = nfs_page_find_request_locked(NFS_I(inode), page);
286074cc1deSTrond Myklebust 		if (req == NULL)
287074cc1deSTrond Myklebust 			break;
2887ad84aa9STrond Myklebust 		if (nfs_lock_request(req))
289e261f51fSTrond Myklebust 			break;
290e261f51fSTrond Myklebust 		/* Note: If we hold the page lock, as is the case in nfs_writepage,
2917ad84aa9STrond Myklebust 		 *	 then the call to nfs_lock_request() will always
292e261f51fSTrond Myklebust 		 *	 succeed provided that someone hasn't already marked the
293e261f51fSTrond Myklebust 		 *	 request as dirty (in which case we don't care).
294e261f51fSTrond Myklebust 		 */
295587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
296cfb506e1STrond Myklebust 		if (!nonblock)
297e261f51fSTrond Myklebust 			ret = nfs_wait_on_request(req);
298cfb506e1STrond Myklebust 		else
299cfb506e1STrond Myklebust 			ret = -EAGAIN;
300e261f51fSTrond Myklebust 		nfs_release_request(req);
301e261f51fSTrond Myklebust 		if (ret != 0)
302074cc1deSTrond Myklebust 			return ERR_PTR(ret);
303587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
304e261f51fSTrond Myklebust 	}
305587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
306074cc1deSTrond Myklebust 	return req;
307612c9384STrond Myklebust }
308074cc1deSTrond Myklebust 
309074cc1deSTrond Myklebust /*
310074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
311074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
312074cc1deSTrond Myklebust  */
313074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
314cfb506e1STrond Myklebust 				struct page *page, bool nonblock)
315074cc1deSTrond Myklebust {
316074cc1deSTrond Myklebust 	struct nfs_page *req;
317074cc1deSTrond Myklebust 	int ret = 0;
318074cc1deSTrond Myklebust 
319cfb506e1STrond Myklebust 	req = nfs_find_and_lock_request(page, nonblock);
320074cc1deSTrond Myklebust 	if (!req)
321074cc1deSTrond Myklebust 		goto out;
322074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
323074cc1deSTrond Myklebust 	if (IS_ERR(req))
324074cc1deSTrond Myklebust 		goto out;
325074cc1deSTrond Myklebust 
326deed85e7STrond Myklebust 	nfs_set_page_writeback(page);
327deed85e7STrond Myklebust 	WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
328074cc1deSTrond Myklebust 
329deed85e7STrond Myklebust 	ret = 0;
330f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
331f8512ad0SFred Isaman 		nfs_redirty_request(req);
332074cc1deSTrond Myklebust 		ret = pgio->pg_error;
333f8512ad0SFred Isaman 	}
334074cc1deSTrond Myklebust out:
335074cc1deSTrond Myklebust 	return ret;
336e261f51fSTrond Myklebust }
337e261f51fSTrond Myklebust 
338f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
339f758c885STrond Myklebust {
340d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
341cfb506e1STrond Myklebust 	int ret;
342f758c885STrond Myklebust 
343f758c885STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
344f758c885STrond Myklebust 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
345f758c885STrond Myklebust 
346d56b4ddfSMel Gorman 	nfs_pageio_cond_complete(pgio, page_file_index(page));
3471b430beeSWu Fengguang 	ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
348cfb506e1STrond Myklebust 	if (ret == -EAGAIN) {
349cfb506e1STrond Myklebust 		redirty_page_for_writepage(wbc, page);
350cfb506e1STrond Myklebust 		ret = 0;
351cfb506e1STrond Myklebust 	}
352cfb506e1STrond Myklebust 	return ret;
353f758c885STrond Myklebust }
354f758c885STrond Myklebust 
355e261f51fSTrond Myklebust /*
3561da177e4SLinus Torvalds  * Write an mmapped page to the server.
3571da177e4SLinus Torvalds  */
3584d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
3591da177e4SLinus Torvalds {
360f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
361e261f51fSTrond Myklebust 	int err;
3621da177e4SLinus Torvalds 
363a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc),
364a20c93e3SChristoph Hellwig 				false, &nfs_async_write_completion_ops);
365f758c885STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
366f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
367f758c885STrond Myklebust 	if (err < 0)
3684d770ccfSTrond Myklebust 		return err;
369f758c885STrond Myklebust 	if (pgio.pg_error < 0)
370f758c885STrond Myklebust 		return pgio.pg_error;
371f758c885STrond Myklebust 	return 0;
3724d770ccfSTrond Myklebust }
3734d770ccfSTrond Myklebust 
3744d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
3754d770ccfSTrond Myklebust {
376f758c885STrond Myklebust 	int ret;
3774d770ccfSTrond Myklebust 
378f758c885STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
3791da177e4SLinus Torvalds 	unlock_page(page);
380f758c885STrond Myklebust 	return ret;
381f758c885STrond Myklebust }
382f758c885STrond Myklebust 
383f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
384f758c885STrond Myklebust {
385f758c885STrond Myklebust 	int ret;
386f758c885STrond Myklebust 
387f758c885STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
388f758c885STrond Myklebust 	unlock_page(page);
389f758c885STrond Myklebust 	return ret;
3901da177e4SLinus Torvalds }
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3931da177e4SLinus Torvalds {
3941da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
39572cb77f4STrond Myklebust 	unsigned long *bitlock = &NFS_I(inode)->flags;
396c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
3971da177e4SLinus Torvalds 	int err;
3981da177e4SLinus Torvalds 
39972cb77f4STrond Myklebust 	/* Stop dirtying of new pages while we sync */
400*74316201SNeilBrown 	err = wait_on_bit_lock_action(bitlock, NFS_INO_FLUSHING,
40172cb77f4STrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
40272cb77f4STrond Myklebust 	if (err)
40372cb77f4STrond Myklebust 		goto out_err;
40472cb77f4STrond Myklebust 
40591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
40691d5b470SChuck Lever 
407a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false,
408a20c93e3SChristoph Hellwig 				&nfs_async_write_completion_ops);
409f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
410c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
41172cb77f4STrond Myklebust 
41272cb77f4STrond Myklebust 	clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
4134e857c58SPeter Zijlstra 	smp_mb__after_atomic();
41472cb77f4STrond Myklebust 	wake_up_bit(bitlock, NFS_INO_FLUSHING);
41572cb77f4STrond Myklebust 
416f758c885STrond Myklebust 	if (err < 0)
41772cb77f4STrond Myklebust 		goto out_err;
41872cb77f4STrond Myklebust 	err = pgio.pg_error;
41972cb77f4STrond Myklebust 	if (err < 0)
42072cb77f4STrond Myklebust 		goto out_err;
421c63c7b05STrond Myklebust 	return 0;
42272cb77f4STrond Myklebust out_err:
42372cb77f4STrond Myklebust 	return err;
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds /*
4271da177e4SLinus Torvalds  * Insert a write request into an inode
4281da177e4SLinus Torvalds  */
429d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
4301da177e4SLinus Torvalds {
4311da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
432e7d39069STrond Myklebust 
4332bfc6e56SWeston Andros Adamson 	WARN_ON_ONCE(req->wb_this_page != req);
4342bfc6e56SWeston Andros Adamson 
435e7d39069STrond Myklebust 	/* Lock the request! */
4367ad84aa9STrond Myklebust 	nfs_lock_request(req);
437e7d39069STrond Myklebust 
438e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
439011e2a7fSBryan Schumaker 	if (!nfsi->npages && NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
440a9a4a87aSTrond Myklebust 		inode->i_version++;
44129418aa4SMel Gorman 	/*
44229418aa4SMel Gorman 	 * Swap-space should not get truncated. Hence no need to plug the race
44329418aa4SMel Gorman 	 * with invalidate/truncate.
44429418aa4SMel Gorman 	 */
44529418aa4SMel Gorman 	if (likely(!PageSwapCache(req->wb_page))) {
4462df485a7STrond Myklebust 		set_bit(PG_MAPPED, &req->wb_flags);
447deb7d638STrond Myklebust 		SetPagePrivate(req->wb_page);
448277459d2STrond Myklebust 		set_page_private(req->wb_page, (unsigned long)req);
44929418aa4SMel Gorman 	}
4501da177e4SLinus Torvalds 	nfsi->npages++;
4512bfc6e56SWeston Andros Adamson 	set_bit(PG_INODE_REF, &req->wb_flags);
452c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
453e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
4541da177e4SLinus Torvalds }
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds /*
45789a09141SPeter Zijlstra  * Remove a write request from an inode
4581da177e4SLinus Torvalds  */
4591da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
4601da177e4SLinus Torvalds {
4613d4ff43dSAl Viro 	struct inode *inode = req->wb_context->dentry->d_inode;
4621da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
46320633f04SWeston Andros Adamson 	struct nfs_page *head;
46420633f04SWeston Andros Adamson 
46520633f04SWeston Andros Adamson 	if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) {
46620633f04SWeston Andros Adamson 		head = req->wb_head;
4671da177e4SLinus Torvalds 
468587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
46920633f04SWeston Andros Adamson 		if (likely(!PageSwapCache(head->wb_page))) {
47020633f04SWeston Andros Adamson 			set_page_private(head->wb_page, 0);
47120633f04SWeston Andros Adamson 			ClearPagePrivate(head->wb_page);
47220633f04SWeston Andros Adamson 			clear_bit(PG_MAPPED, &head->wb_flags);
47329418aa4SMel Gorman 		}
4741da177e4SLinus Torvalds 		nfsi->npages--;
475587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
47620633f04SWeston Andros Adamson 	}
4771da177e4SLinus Torvalds 	nfs_release_request(req);
4781da177e4SLinus Torvalds }
4791da177e4SLinus Torvalds 
48061822ab5STrond Myklebust static void
4816d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
48261822ab5STrond Myklebust {
48361822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
48461822ab5STrond Myklebust }
48561822ab5STrond Myklebust 
48689d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
4878dd37758STrond Myklebust /**
4888dd37758STrond Myklebust  * nfs_request_add_commit_list - add request to a commit list
4898dd37758STrond Myklebust  * @req: pointer to a struct nfs_page
490ea2cf228SFred Isaman  * @dst: commit list head
491ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
4928dd37758STrond Myklebust  *
493ea2cf228SFred Isaman  * This sets the PG_CLEAN bit, updates the cinfo count of
4948dd37758STrond Myklebust  * number of outstanding requests requiring a commit as well as
4958dd37758STrond Myklebust  * the MM page stats.
4968dd37758STrond Myklebust  *
497ea2cf228SFred Isaman  * The caller must _not_ hold the cinfo->lock, but must be
4988dd37758STrond Myklebust  * holding the nfs_page lock.
4998dd37758STrond Myklebust  */
5008dd37758STrond Myklebust void
501ea2cf228SFred Isaman nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst,
502ea2cf228SFred Isaman 			    struct nfs_commit_info *cinfo)
5038dd37758STrond Myklebust {
5048dd37758STrond Myklebust 	set_bit(PG_CLEAN, &(req)->wb_flags);
505ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
506ea2cf228SFred Isaman 	nfs_list_add_request(req, dst);
507ea2cf228SFred Isaman 	cinfo->mds->ncommit++;
508ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
50956f9cd68SFred Isaman 	if (!cinfo->dreq) {
5108dd37758STrond Myklebust 		inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
511d56b4ddfSMel Gorman 		inc_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
51256f9cd68SFred Isaman 			     BDI_RECLAIMABLE);
51356f9cd68SFred Isaman 		__mark_inode_dirty(req->wb_context->dentry->d_inode,
51456f9cd68SFred Isaman 				   I_DIRTY_DATASYNC);
51556f9cd68SFred Isaman 	}
5168dd37758STrond Myklebust }
5178dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
5188dd37758STrond Myklebust 
5198dd37758STrond Myklebust /**
5208dd37758STrond Myklebust  * nfs_request_remove_commit_list - Remove request from a commit list
5218dd37758STrond Myklebust  * @req: pointer to a nfs_page
522ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
5238dd37758STrond Myklebust  *
524ea2cf228SFred Isaman  * This clears the PG_CLEAN bit, and updates the cinfo's count of
5258dd37758STrond Myklebust  * number of outstanding requests requiring a commit
5268dd37758STrond Myklebust  * It does not update the MM page stats.
5278dd37758STrond Myklebust  *
528ea2cf228SFred Isaman  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
5298dd37758STrond Myklebust  */
5308dd37758STrond Myklebust void
531ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req,
532ea2cf228SFred Isaman 			       struct nfs_commit_info *cinfo)
5338dd37758STrond Myklebust {
5348dd37758STrond Myklebust 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
5358dd37758STrond Myklebust 		return;
5368dd37758STrond Myklebust 	nfs_list_remove_request(req);
537ea2cf228SFred Isaman 	cinfo->mds->ncommit--;
5388dd37758STrond Myklebust }
5398dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
5408dd37758STrond Myklebust 
541ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
542ea2cf228SFred Isaman 				      struct inode *inode)
543ea2cf228SFred Isaman {
544ea2cf228SFred Isaman 	cinfo->lock = &inode->i_lock;
545ea2cf228SFred Isaman 	cinfo->mds = &NFS_I(inode)->commit_info;
546ea2cf228SFred Isaman 	cinfo->ds = pnfs_get_ds_info(inode);
547b359f9d0SFred Isaman 	cinfo->dreq = NULL;
548f453a54aSFred Isaman 	cinfo->completion_ops = &nfs_commit_completion_ops;
549ea2cf228SFred Isaman }
550ea2cf228SFred Isaman 
551ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
552ea2cf228SFred Isaman 		    struct inode *inode,
553ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq)
554ea2cf228SFred Isaman {
5551763da12SFred Isaman 	if (dreq)
5561763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
5571763da12SFred Isaman 	else
558ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(cinfo, inode);
559ea2cf228SFred Isaman }
560ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo);
5618dd37758STrond Myklebust 
5621da177e4SLinus Torvalds /*
5631da177e4SLinus Torvalds  * Add a request to the inode's commit list.
5641da177e4SLinus Torvalds  */
5651763da12SFred Isaman void
566ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
567ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
5681da177e4SLinus Torvalds {
569ea2cf228SFred Isaman 	if (pnfs_mark_request_commit(req, lseg, cinfo))
5708dd37758STrond Myklebust 		return;
571ea2cf228SFred Isaman 	nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo);
5721da177e4SLinus Torvalds }
5738e821cadSTrond Myklebust 
574d6d6dc7cSFred Isaman static void
575d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page)
576e468bae9STrond Myklebust {
577e468bae9STrond Myklebust 	dec_zone_page_state(page, NR_UNSTABLE_NFS);
578d56b4ddfSMel Gorman 	dec_bdi_stat(page_file_mapping(page)->backing_dev_info, BDI_RECLAIMABLE);
579e468bae9STrond Myklebust }
580d6d6dc7cSFred Isaman 
5818dd37758STrond Myklebust static void
582d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req)
583d6d6dc7cSFred Isaman {
5848dd37758STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
5858dd37758STrond Myklebust 		struct inode *inode = req->wb_context->dentry->d_inode;
586ea2cf228SFred Isaman 		struct nfs_commit_info cinfo;
587d6d6dc7cSFred Isaman 
588ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(&cinfo, inode);
589ea2cf228SFred Isaman 		if (!pnfs_clear_request_commit(req, &cinfo)) {
590ea2cf228SFred Isaman 			spin_lock(cinfo.lock);
591ea2cf228SFred Isaman 			nfs_request_remove_commit_list(req, &cinfo);
592ea2cf228SFred Isaman 			spin_unlock(cinfo.lock);
593d6d6dc7cSFred Isaman 		}
5948dd37758STrond Myklebust 		nfs_clear_page_commit(req->wb_page);
5958dd37758STrond Myklebust 	}
596e468bae9STrond Myklebust }
597e468bae9STrond Myklebust 
5988e821cadSTrond Myklebust static inline
5999c7e1b3dSAnna Schumaker int nfs_write_need_commit(struct nfs_pgio_data *data)
6008e821cadSTrond Myklebust {
601465d5243SFred Isaman 	if (data->verf.committed == NFS_DATA_SYNC)
602cd841605SFred Isaman 		return data->header->lseg == NULL;
6038e821cadSTrond Myklebust 	return data->verf.committed != NFS_FILE_SYNC;
6048e821cadSTrond Myklebust }
6058e821cadSTrond Myklebust 
6068e821cadSTrond Myklebust #else
60768cd6fa4SBryan Schumaker static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
60868cd6fa4SBryan Schumaker 				      struct inode *inode)
60968cd6fa4SBryan Schumaker {
61068cd6fa4SBryan Schumaker }
61168cd6fa4SBryan Schumaker 
61268cd6fa4SBryan Schumaker void nfs_init_cinfo(struct nfs_commit_info *cinfo,
61368cd6fa4SBryan Schumaker 		    struct inode *inode,
61468cd6fa4SBryan Schumaker 		    struct nfs_direct_req *dreq)
61568cd6fa4SBryan Schumaker {
61668cd6fa4SBryan Schumaker }
61768cd6fa4SBryan Schumaker 
6181763da12SFred Isaman void
619ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
620ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
6218e821cadSTrond Myklebust {
6228e821cadSTrond Myklebust }
6238e821cadSTrond Myklebust 
6248dd37758STrond Myklebust static void
625e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
626e468bae9STrond Myklebust {
627e468bae9STrond Myklebust }
628e468bae9STrond Myklebust 
6298e821cadSTrond Myklebust static inline
6309c7e1b3dSAnna Schumaker int nfs_write_need_commit(struct nfs_pgio_data *data)
6318e821cadSTrond Myklebust {
6328e821cadSTrond Myklebust 	return 0;
6338e821cadSTrond Myklebust }
6348e821cadSTrond Myklebust 
6351da177e4SLinus Torvalds #endif
6361da177e4SLinus Torvalds 
637061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
6386c75dc0dSFred Isaman {
639ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
6406c75dc0dSFred Isaman 	unsigned long bytes = 0;
6412bfc6e56SWeston Andros Adamson 	bool do_destroy;
6426c75dc0dSFred Isaman 
6436c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
6446c75dc0dSFred Isaman 		goto out;
645ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
6466c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
6476c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
6486c75dc0dSFred Isaman 
6496c75dc0dSFred Isaman 		bytes += req->wb_bytes;
6506c75dc0dSFred Isaman 		nfs_list_remove_request(req);
6516c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
6526c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
653d1182b33STrond Myklebust 			nfs_set_pageerror(req->wb_page);
6546c75dc0dSFred Isaman 			nfs_context_set_write_error(req->wb_context, hdr->error);
6556c75dc0dSFred Isaman 			goto remove_req;
6566c75dc0dSFred Isaman 		}
6576c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) {
6586c75dc0dSFred Isaman 			nfs_mark_request_dirty(req);
6596c75dc0dSFred Isaman 			goto next;
6606c75dc0dSFred Isaman 		}
6616c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) {
662f79d06f5SAnna Schumaker 			memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
663ea2cf228SFred Isaman 			nfs_mark_request_commit(req, hdr->lseg, &cinfo);
6646c75dc0dSFred Isaman 			goto next;
6656c75dc0dSFred Isaman 		}
6666c75dc0dSFred Isaman remove_req:
6676c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
6686c75dc0dSFred Isaman next:
6691d1afcbcSTrond Myklebust 		nfs_unlock_request(req);
67020633f04SWeston Andros Adamson 		nfs_end_page_writeback(req);
6712bfc6e56SWeston Andros Adamson 		do_destroy = !test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags);
6723aff4ebbSTrond Myklebust 		nfs_release_request(req);
6736c75dc0dSFred Isaman 	}
6746c75dc0dSFred Isaman out:
6756c75dc0dSFred Isaman 	hdr->release(hdr);
6766c75dc0dSFred Isaman }
6776c75dc0dSFred Isaman 
67889d77c8fSBryan Schumaker #if  IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
679ce59515cSAnna Schumaker unsigned long
680ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
681fb8a1f11STrond Myklebust {
682ea2cf228SFred Isaman 	return cinfo->mds->ncommit;
683fb8a1f11STrond Myklebust }
684fb8a1f11STrond Myklebust 
685ea2cf228SFred Isaman /* cinfo->lock held by caller */
6861763da12SFred Isaman int
687ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
688ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
689d6d6dc7cSFred Isaman {
690d6d6dc7cSFred Isaman 	struct nfs_page *req, *tmp;
691d6d6dc7cSFred Isaman 	int ret = 0;
692d6d6dc7cSFred Isaman 
693d6d6dc7cSFred Isaman 	list_for_each_entry_safe(req, tmp, src, wb_list) {
6948dd37758STrond Myklebust 		if (!nfs_lock_request(req))
6958dd37758STrond Myklebust 			continue;
6967ad84aa9STrond Myklebust 		kref_get(&req->wb_kref);
697ea2cf228SFred Isaman 		if (cond_resched_lock(cinfo->lock))
6983b3be88dSTrond Myklebust 			list_safe_reset_next(req, tmp, wb_list);
699ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
7008dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
701d6d6dc7cSFred Isaman 		ret++;
7021763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
703d6d6dc7cSFred Isaman 			break;
704d6d6dc7cSFred Isaman 	}
705d6d6dc7cSFred Isaman 	return ret;
706d6d6dc7cSFred Isaman }
707d6d6dc7cSFred Isaman 
7081da177e4SLinus Torvalds /*
7091da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
7101da177e4SLinus Torvalds  * @inode: NFS inode to scan
711ea2cf228SFred Isaman  * @dst: mds destination list
712ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
7131da177e4SLinus Torvalds  *
7141da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
7151da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
7161da177e4SLinus Torvalds  */
7171763da12SFred Isaman int
718ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
719ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
7201da177e4SLinus Torvalds {
721d6d6dc7cSFred Isaman 	int ret = 0;
722fb8a1f11STrond Myklebust 
723ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
724ea2cf228SFred Isaman 	if (cinfo->mds->ncommit > 0) {
7258dd37758STrond Myklebust 		const int max = INT_MAX;
726d6d6dc7cSFred Isaman 
727ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
728ea2cf228SFred Isaman 					   cinfo, max);
729ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
730d6d6dc7cSFred Isaman 	}
731ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
732ff778d02STrond Myklebust 	return ret;
7331da177e4SLinus Torvalds }
734d6d6dc7cSFred Isaman 
735c42de9ddSTrond Myklebust #else
736ce59515cSAnna Schumaker unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
737fb8a1f11STrond Myklebust {
738fb8a1f11STrond Myklebust 	return 0;
739fb8a1f11STrond Myklebust }
740fb8a1f11STrond Myklebust 
7411763da12SFred Isaman int nfs_scan_commit(struct inode *inode, struct list_head *dst,
742ea2cf228SFred Isaman 		    struct nfs_commit_info *cinfo)
743c42de9ddSTrond Myklebust {
744c42de9ddSTrond Myklebust 	return 0;
745c42de9ddSTrond Myklebust }
7461da177e4SLinus Torvalds #endif
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds /*
749e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
750e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
7511da177e4SLinus Torvalds  *
752e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
753e7d39069STrond Myklebust  * to disk.
7541da177e4SLinus Torvalds  */
755e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
756e7d39069STrond Myklebust 		struct page *page,
757e7d39069STrond Myklebust 		unsigned int offset,
758e7d39069STrond Myklebust 		unsigned int bytes)
7591da177e4SLinus Torvalds {
760e7d39069STrond Myklebust 	struct nfs_page *req;
761e7d39069STrond Myklebust 	unsigned int rqend;
762e7d39069STrond Myklebust 	unsigned int end;
7631da177e4SLinus Torvalds 	int error;
764277459d2STrond Myklebust 
765e7d39069STrond Myklebust 	if (!PagePrivate(page))
766e7d39069STrond Myklebust 		return NULL;
767e7d39069STrond Myklebust 
768e7d39069STrond Myklebust 	end = offset + bytes;
769e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
770e7d39069STrond Myklebust 
771e7d39069STrond Myklebust 	for (;;) {
77229418aa4SMel Gorman 		req = nfs_page_find_request_locked(NFS_I(inode), page);
773e7d39069STrond Myklebust 		if (req == NULL)
774e7d39069STrond Myklebust 			goto out_unlock;
775e7d39069STrond Myklebust 
7762bfc6e56SWeston Andros Adamson 		/* should be handled by nfs_flush_incompatible */
7772bfc6e56SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_head != req);
7782bfc6e56SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_this_page != req);
7792bfc6e56SWeston Andros Adamson 
780e7d39069STrond Myklebust 		rqend = req->wb_offset + req->wb_bytes;
781e7d39069STrond Myklebust 		/*
782e7d39069STrond Myklebust 		 * Tell the caller to flush out the request if
783e7d39069STrond Myklebust 		 * the offsets are non-contiguous.
784e7d39069STrond Myklebust 		 * Note: nfs_flush_incompatible() will already
785e7d39069STrond Myklebust 		 * have flushed out requests having wrong owners.
786e7d39069STrond Myklebust 		 */
787e468bae9STrond Myklebust 		if (offset > rqend
788e7d39069STrond Myklebust 		    || end < req->wb_offset)
789e7d39069STrond Myklebust 			goto out_flushme;
790e7d39069STrond Myklebust 
7917ad84aa9STrond Myklebust 		if (nfs_lock_request(req))
792e7d39069STrond Myklebust 			break;
793e7d39069STrond Myklebust 
794e7d39069STrond Myklebust 		/* The request is locked, so wait and then retry */
795587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
7961da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
7971da177e4SLinus Torvalds 		nfs_release_request(req);
798e7d39069STrond Myklebust 		if (error != 0)
799e7d39069STrond Myklebust 			goto out_err;
800e7d39069STrond Myklebust 		spin_lock(&inode->i_lock);
8011da177e4SLinus Torvalds 	}
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
8041da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
8051da177e4SLinus Torvalds 		req->wb_offset = offset;
8061da177e4SLinus Torvalds 		req->wb_pgbase = offset;
8071da177e4SLinus Torvalds 	}
8081da177e4SLinus Torvalds 	if (end > rqend)
8091da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
810e7d39069STrond Myklebust 	else
811e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
812e7d39069STrond Myklebust out_unlock:
813e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
814ca138f36SFred Isaman 	if (req)
8158dd37758STrond Myklebust 		nfs_clear_request_commit(req);
816e7d39069STrond Myklebust 	return req;
817e7d39069STrond Myklebust out_flushme:
818e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
819e7d39069STrond Myklebust 	nfs_release_request(req);
820e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
821e7d39069STrond Myklebust out_err:
822e7d39069STrond Myklebust 	return ERR_PTR(error);
823e7d39069STrond Myklebust }
8241da177e4SLinus Torvalds 
825e7d39069STrond Myklebust /*
826e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
827e7d39069STrond Myklebust  *
828e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
829e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
830e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
831e7d39069STrond Myklebust  */
832e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
833e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
834e7d39069STrond Myklebust {
835d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
836e7d39069STrond Myklebust 	struct nfs_page	*req;
837e7d39069STrond Myklebust 
838e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
839e7d39069STrond Myklebust 	if (req != NULL)
840e7d39069STrond Myklebust 		goto out;
8412bfc6e56SWeston Andros Adamson 	req = nfs_create_request(ctx, page, NULL, offset, bytes);
842e7d39069STrond Myklebust 	if (IS_ERR(req))
843e7d39069STrond Myklebust 		goto out;
844d6d6dc7cSFred Isaman 	nfs_inode_add_request(inode, req);
845efc91ed0STrond Myklebust out:
84661e930a9STrond Myklebust 	return req;
8471da177e4SLinus Torvalds }
8481da177e4SLinus Torvalds 
849e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
850e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
851e7d39069STrond Myklebust {
852e7d39069STrond Myklebust 	struct nfs_page	*req;
853e7d39069STrond Myklebust 
854e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
855e7d39069STrond Myklebust 	if (IS_ERR(req))
856e7d39069STrond Myklebust 		return PTR_ERR(req);
857e7d39069STrond Myklebust 	/* Update file length */
858e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
859d72ddcbaSWeston Andros Adamson 	nfs_mark_uptodate(req);
860a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
8611d1afcbcSTrond Myklebust 	nfs_unlock_and_release_request(req);
862e7d39069STrond Myklebust 	return 0;
863e7d39069STrond Myklebust }
864e7d39069STrond Myklebust 
8651da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
8661da177e4SLinus Torvalds {
867cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
8682a369153STrond Myklebust 	struct nfs_lock_context *l_ctx;
8691da177e4SLinus Torvalds 	struct nfs_page	*req;
8701a54533eSTrond Myklebust 	int do_flush, status;
8711da177e4SLinus Torvalds 	/*
8721da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
8731da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
8741da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
8751da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
8761da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
8771da177e4SLinus Torvalds 	 * dropped page.
8781da177e4SLinus Torvalds 	 */
8791a54533eSTrond Myklebust 	do {
880277459d2STrond Myklebust 		req = nfs_page_find_request(page);
8811a54533eSTrond Myklebust 		if (req == NULL)
8821a54533eSTrond Myklebust 			return 0;
8832a369153STrond Myklebust 		l_ctx = req->wb_lock_context;
8842a369153STrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx;
8852bfc6e56SWeston Andros Adamson 		/* for now, flush if more than 1 request in page_group */
8862bfc6e56SWeston Andros Adamson 		do_flush |= req->wb_this_page != req;
8870f1d2605STrond Myklebust 		if (l_ctx && ctx->dentry->d_inode->i_flock != NULL) {
8882a369153STrond Myklebust 			do_flush |= l_ctx->lockowner.l_owner != current->files
8892a369153STrond Myklebust 				|| l_ctx->lockowner.l_pid != current->tgid;
8902a369153STrond Myklebust 		}
8911da177e4SLinus Torvalds 		nfs_release_request(req);
8921a54533eSTrond Myklebust 		if (!do_flush)
8931a54533eSTrond Myklebust 			return 0;
894d56b4ddfSMel Gorman 		status = nfs_wb_page(page_file_mapping(page)->host, page);
8951a54533eSTrond Myklebust 	} while (status == 0);
8961a54533eSTrond Myklebust 	return status;
8971da177e4SLinus Torvalds }
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds /*
900dc24826bSAndy Adamson  * Avoid buffered writes when a open context credential's key would
901dc24826bSAndy Adamson  * expire soon.
902dc24826bSAndy Adamson  *
903dc24826bSAndy Adamson  * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
904dc24826bSAndy Adamson  *
905dc24826bSAndy Adamson  * Return 0 and set a credential flag which triggers the inode to flush
906dc24826bSAndy Adamson  * and performs  NFS_FILE_SYNC writes if the key will expired within
907dc24826bSAndy Adamson  * RPC_KEY_EXPIRE_TIMEO.
908dc24826bSAndy Adamson  */
909dc24826bSAndy Adamson int
910dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode)
911dc24826bSAndy Adamson {
912dc24826bSAndy Adamson 	struct nfs_open_context *ctx = nfs_file_open_context(filp);
913dc24826bSAndy Adamson 	struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
914dc24826bSAndy Adamson 
915dc24826bSAndy Adamson 	return rpcauth_key_timeout_notify(auth, ctx->cred);
916dc24826bSAndy Adamson }
917dc24826bSAndy Adamson 
918dc24826bSAndy Adamson /*
919dc24826bSAndy Adamson  * Test if the open context credential key is marked to expire soon.
920dc24826bSAndy Adamson  */
921dc24826bSAndy Adamson bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx)
922dc24826bSAndy Adamson {
923dc24826bSAndy Adamson 	return rpcauth_cred_key_to_expire(ctx->cred);
924dc24826bSAndy Adamson }
925dc24826bSAndy Adamson 
926dc24826bSAndy Adamson /*
9275d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
9285d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
9295d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
9305d47a356STrond Myklebust  */
9318d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
9325d47a356STrond Myklebust {
933d529ef83SJeff Layton 	struct nfs_inode *nfsi = NFS_I(inode);
934d529ef83SJeff Layton 
9358d197a56STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
9368d197a56STrond Myklebust 		goto out;
93718dd78c4SScott Mayhew 	if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
938d529ef83SJeff Layton 		return false;
9394db72b40SJeff Layton 	smp_rmb();
940d529ef83SJeff Layton 	if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags))
9418d197a56STrond Myklebust 		return false;
9428d197a56STrond Myklebust out:
94318dd78c4SScott Mayhew 	if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
94418dd78c4SScott Mayhew 		return false;
9458d197a56STrond Myklebust 	return PageUptodate(page) != 0;
9465d47a356STrond Myklebust }
9475d47a356STrond Myklebust 
948c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or
949c7559663SScott Mayhew  * if we have the whole file locked for writing), it may be more efficient to
950c7559663SScott Mayhew  * extend the write to cover the entire page in order to avoid fragmentation
951c7559663SScott Mayhew  * inefficiencies.
952c7559663SScott Mayhew  *
953263b4509SScott Mayhew  * If the file is opened for synchronous writes then we can just skip the rest
954263b4509SScott Mayhew  * of the checks.
955c7559663SScott Mayhew  */
956c7559663SScott Mayhew static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode)
957c7559663SScott Mayhew {
958c7559663SScott Mayhew 	if (file->f_flags & O_DSYNC)
959c7559663SScott Mayhew 		return 0;
960263b4509SScott Mayhew 	if (!nfs_write_pageuptodate(page, inode))
961263b4509SScott Mayhew 		return 0;
962c7559663SScott Mayhew 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
963c7559663SScott Mayhew 		return 1;
964263b4509SScott Mayhew 	if (inode->i_flock == NULL || (inode->i_flock->fl_start == 0 &&
965c7559663SScott Mayhew 			inode->i_flock->fl_end == OFFSET_MAX &&
966263b4509SScott Mayhew 			inode->i_flock->fl_type != F_RDLCK))
967c7559663SScott Mayhew 		return 1;
968c7559663SScott Mayhew 	return 0;
969c7559663SScott Mayhew }
970c7559663SScott Mayhew 
9715d47a356STrond Myklebust /*
9721da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
9731da177e4SLinus Torvalds  *
9741da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
9751da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
9761da177e4SLinus Torvalds  */
9771da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
9781da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
9791da177e4SLinus Torvalds {
980cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
981d56b4ddfSMel Gorman 	struct inode	*inode = page_file_mapping(page)->host;
9821da177e4SLinus Torvalds 	int		status = 0;
9831da177e4SLinus Torvalds 
98491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
98591d5b470SChuck Lever 
9866de1472fSAl Viro 	dprintk("NFS:       nfs_updatepage(%pD2 %d@%lld)\n",
9876de1472fSAl Viro 		file, count, (long long)(page_file_offset(page) + offset));
9881da177e4SLinus Torvalds 
989c7559663SScott Mayhew 	if (nfs_can_extend_write(file, page, inode)) {
99049a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
9911da177e4SLinus Torvalds 		offset = 0;
9921da177e4SLinus Torvalds 	}
9931da177e4SLinus Torvalds 
994e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
99503fa9e84STrond Myklebust 	if (status < 0)
99603fa9e84STrond Myklebust 		nfs_set_pageerror(page);
99759b7c05fSTrond Myklebust 	else
99859b7c05fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
9991da177e4SLinus Torvalds 
100048186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
10011da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
10021da177e4SLinus Torvalds 	return status;
10031da177e4SLinus Torvalds }
10041da177e4SLinus Torvalds 
10053ff7576dSTrond Myklebust static int flush_task_priority(int how)
10061da177e4SLinus Torvalds {
10071da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
10081da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
10091da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
10101da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
10111da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
10121da177e4SLinus Torvalds 	}
10131da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
10141da177e4SLinus Torvalds }
10151da177e4SLinus Torvalds 
10161ed26f33SAnna Schumaker static void nfs_initiate_write(struct nfs_pgio_data *data, struct rpc_message *msg,
10171ed26f33SAnna Schumaker 			       struct rpc_task_setup *task_setup_data, int how)
10181da177e4SLinus Torvalds {
1019cd841605SFred Isaman 	struct inode *inode = data->header->inode;
10203ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
10211da177e4SLinus Torvalds 
10221ed26f33SAnna Schumaker 	task_setup_data->priority = priority;
10231ed26f33SAnna Schumaker 	NFS_PROTO(inode)->write_setup(data, msg);
1024d138d5d1SAndy Adamson 
10258c21c62cSWeston Andros Adamson 	nfs4_state_protect_write(NFS_SERVER(inode)->nfs_client,
10261ed26f33SAnna Schumaker 				 &task_setup_data->rpc_client, msg, data);
1027275acaafSTrond Myklebust }
1028275acaafSTrond Myklebust 
10296d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
10306d884e8fSFred  * call this on each, which will prepare them to be retried on next
10316d884e8fSFred  * writeback using standard nfs.
10326d884e8fSFred  */
10336d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
10346d884e8fSFred {
10356d884e8fSFred 	nfs_mark_request_dirty(req);
10361d1afcbcSTrond Myklebust 	nfs_unlock_request(req);
103720633f04SWeston Andros Adamson 	nfs_end_page_writeback(req);
10383aff4ebbSTrond Myklebust 	nfs_release_request(req);
10396d884e8fSFred }
10406d884e8fSFred 
1041061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head)
10426c75dc0dSFred Isaman {
10436c75dc0dSFred Isaman 	struct nfs_page	*req;
10446c75dc0dSFred Isaman 
10456c75dc0dSFred Isaman 	while (!list_empty(head)) {
10466c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
10476c75dc0dSFred Isaman 		nfs_list_remove_request(req);
10486c75dc0dSFred Isaman 		nfs_redirty_request(req);
10496c75dc0dSFred Isaman 	}
10506c75dc0dSFred Isaman }
10516c75dc0dSFred Isaman 
1052061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1053061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1054061ae2edSFred Isaman 	.completion = nfs_write_completion,
1055061ae2edSFred Isaman };
1056061ae2edSFred Isaman 
105757208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1058a20c93e3SChristoph Hellwig 			       struct inode *inode, int ioflags, bool force_mds,
1059061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
10601751c363STrond Myklebust {
1061a20c93e3SChristoph Hellwig 	struct nfs_server *server = NFS_SERVER(inode);
106241d8d5b7SAnna Schumaker 	const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
1063a20c93e3SChristoph Hellwig 
1064a20c93e3SChristoph Hellwig #ifdef CONFIG_NFS_V4_1
1065a20c93e3SChristoph Hellwig 	if (server->pnfs_curr_ld && !force_mds)
1066a20c93e3SChristoph Hellwig 		pg_ops = server->pnfs_curr_ld->pg_write_ops;
1067a20c93e3SChristoph Hellwig #endif
10684a0de55cSAnna Schumaker 	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
10694a0de55cSAnna Schumaker 			server->wsize, ioflags);
10701751c363STrond Myklebust }
1071ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
10721751c363STrond Myklebust 
1073dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1074dce81290STrond Myklebust {
107541d8d5b7SAnna Schumaker 	pgio->pg_ops = &nfs_pgio_rw_ops;
1076dce81290STrond Myklebust 	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1077dce81290STrond Myklebust }
10781f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1079dce81290STrond Myklebust 
10801da177e4SLinus Torvalds 
10810b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
10820b7c0153SFred Isaman {
10830b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
10840b7c0153SFred Isaman 
10850b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
10860b7c0153SFred Isaman }
10870b7c0153SFred Isaman 
1088a4cdda59SAnna Schumaker static void nfs_writeback_release_common(struct nfs_pgio_data *data)
10891da177e4SLinus Torvalds {
1090cd841605SFred Isaman 	struct nfs_pgio_header *hdr = data->header;
1091e2fecb21STrond Myklebust 	int status = data->task.tk_status;
1092788e7a89STrond Myklebust 
10936c75dc0dSFred Isaman 	if ((status >= 0) && nfs_write_need_commit(data)) {
10946c75dc0dSFred Isaman 		spin_lock(&hdr->lock);
10956c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags))
10966c75dc0dSFred Isaman 			; /* Do nothing */
10976c75dc0dSFred Isaman 		else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags))
1098f79d06f5SAnna Schumaker 			memcpy(&hdr->verf, &data->verf, sizeof(hdr->verf));
1099f79d06f5SAnna Schumaker 		else if (memcmp(&hdr->verf, &data->verf, sizeof(hdr->verf)))
11006c75dc0dSFred Isaman 			set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags);
11016c75dc0dSFred Isaman 		spin_unlock(&hdr->lock);
11021da177e4SLinus Torvalds 	}
11031da177e4SLinus Torvalds }
11041da177e4SLinus Torvalds 
11051f2edbe3STrond Myklebust /*
11061f2edbe3STrond Myklebust  * Special version of should_remove_suid() that ignores capabilities.
11071f2edbe3STrond Myklebust  */
11081f2edbe3STrond Myklebust static int nfs_should_remove_suid(const struct inode *inode)
11091f2edbe3STrond Myklebust {
11101f2edbe3STrond Myklebust 	umode_t mode = inode->i_mode;
11111f2edbe3STrond Myklebust 	int kill = 0;
1112788e7a89STrond Myklebust 
11131f2edbe3STrond Myklebust 	/* suid always must be killed */
11141f2edbe3STrond Myklebust 	if (unlikely(mode & S_ISUID))
11151f2edbe3STrond Myklebust 		kill = ATTR_KILL_SUID;
11161f2edbe3STrond Myklebust 
11171f2edbe3STrond Myklebust 	/*
11181f2edbe3STrond Myklebust 	 * sgid without any exec bits is just a mandatory locking mark; leave
11191f2edbe3STrond Myklebust 	 * it alone.  If some exec bits are set, it's a real sgid; kill it.
11201f2edbe3STrond Myklebust 	 */
11211f2edbe3STrond Myklebust 	if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
11221f2edbe3STrond Myklebust 		kill |= ATTR_KILL_SGID;
11231f2edbe3STrond Myklebust 
11241f2edbe3STrond Myklebust 	if (unlikely(kill && S_ISREG(mode)))
11251f2edbe3STrond Myklebust 		return kill;
11261f2edbe3STrond Myklebust 
11271f2edbe3STrond Myklebust 	return 0;
11281f2edbe3STrond Myklebust }
1129788e7a89STrond Myklebust 
11301da177e4SLinus Torvalds /*
11311da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
11321da177e4SLinus Torvalds  */
11330eecb214SAnna Schumaker static int nfs_writeback_done(struct rpc_task *task, struct nfs_pgio_data *data,
11340eecb214SAnna Schumaker 			      struct inode *inode)
11351da177e4SLinus Torvalds {
1136788e7a89STrond Myklebust 	int status;
11371da177e4SLinus Torvalds 
1138f551e44fSChuck Lever 	/*
1139f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1140f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1141f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1142f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1143f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1144f551e44fSChuck Lever 	 */
1145cd841605SFred Isaman 	status = NFS_PROTO(inode)->write_done(task, data);
1146788e7a89STrond Myklebust 	if (status != 0)
11470eecb214SAnna Schumaker 		return status;
11480eecb214SAnna Schumaker 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, data->res.count);
114991d5b470SChuck Lever 
115089d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
11510eecb214SAnna Schumaker 	if (data->res.verf->committed < data->args.stable && task->tk_status >= 0) {
11521da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
11531da177e4SLinus Torvalds 		 * commit data to stable storage even though we
11541da177e4SLinus Torvalds 		 * requested it.
11551da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
11561da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
11571da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
11581da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
11591da177e4SLinus Torvalds 		 */
11601da177e4SLinus Torvalds 		static unsigned long    complain;
11611da177e4SLinus Torvalds 
1162a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
11631da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
11641da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
11651da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1166cd841605SFred Isaman 				NFS_SERVER(inode)->nfs_client->cl_hostname,
11670eecb214SAnna Schumaker 				data->res.verf->committed, data->args.stable);
11681da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
11691da177e4SLinus Torvalds 		}
11701da177e4SLinus Torvalds 	}
11711da177e4SLinus Torvalds #endif
11721f2edbe3STrond Myklebust 
11731f2edbe3STrond Myklebust 	/* Deal with the suid/sgid bit corner case */
11741f2edbe3STrond Myklebust 	if (nfs_should_remove_suid(inode))
11751f2edbe3STrond Myklebust 		nfs_mark_for_revalidate(inode);
11760eecb214SAnna Schumaker 	return 0;
11770eecb214SAnna Schumaker }
11780eecb214SAnna Schumaker 
11790eecb214SAnna Schumaker /*
11800eecb214SAnna Schumaker  * This function is called when the WRITE call is complete.
11810eecb214SAnna Schumaker  */
11820eecb214SAnna Schumaker static void nfs_writeback_result(struct rpc_task *task, struct nfs_pgio_data *data)
11830eecb214SAnna Schumaker {
11840eecb214SAnna Schumaker 	struct nfs_pgio_args	*argp = &data->args;
11850eecb214SAnna Schumaker 	struct nfs_pgio_res	*resp = &data->res;
11861f2edbe3STrond Myklebust 
11871f2edbe3STrond Myklebust 	if (resp->count < argp->count) {
11881da177e4SLinus Torvalds 		static unsigned long    complain;
11891da177e4SLinus Torvalds 
11906c75dc0dSFred Isaman 		/* This a short write! */
11910eecb214SAnna Schumaker 		nfs_inc_stats(data->header->inode, NFSIOS_SHORTWRITE);
119291d5b470SChuck Lever 
11931da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
11946c75dc0dSFred Isaman 		if (resp->count == 0) {
11956c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
11966c75dc0dSFred Isaman 				printk(KERN_WARNING
11976c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
11986c75dc0dSFred Isaman 				       argp->count);
11996c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
12006c75dc0dSFred Isaman 			}
12016c75dc0dSFred Isaman 			nfs_set_pgio_error(data->header, -EIO, argp->offset);
12026c75dc0dSFred Isaman 			task->tk_status = -EIO;
12036c75dc0dSFred Isaman 			return;
12046c75dc0dSFred Isaman 		}
12051da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
12061da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
12071da177e4SLinus Torvalds 			/* Resend from where the server left off */
1208a69aef14SFred Isaman 			data->mds_offset += resp->count;
12091da177e4SLinus Torvalds 			argp->offset += resp->count;
12101da177e4SLinus Torvalds 			argp->pgbase += resp->count;
12111da177e4SLinus Torvalds 			argp->count -= resp->count;
12121da177e4SLinus Torvalds 		} else {
12131da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
12141da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
12151da177e4SLinus Torvalds 			 */
12161da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
12171da177e4SLinus Torvalds 		}
1218d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
12191da177e4SLinus Torvalds 	}
12201da177e4SLinus Torvalds }
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds 
122389d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V3) || IS_ENABLED(CONFIG_NFS_V4)
122471d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
122571d0a611STrond Myklebust {
1226b8413f98STrond Myklebust 	int ret;
1227b8413f98STrond Myklebust 
122871d0a611STrond Myklebust 	if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
122971d0a611STrond Myklebust 		return 1;
1230b8413f98STrond Myklebust 	if (!may_wait)
123171d0a611STrond Myklebust 		return 0;
1232b8413f98STrond Myklebust 	ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1233b8413f98STrond Myklebust 				NFS_INO_COMMIT,
1234b8413f98STrond Myklebust 				nfs_wait_bit_killable,
1235b8413f98STrond Myklebust 				TASK_KILLABLE);
1236b8413f98STrond Myklebust 	return (ret < 0) ? ret : 1;
123771d0a611STrond Myklebust }
123871d0a611STrond Myklebust 
1239f453a54aSFred Isaman static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
124071d0a611STrond Myklebust {
124171d0a611STrond Myklebust 	clear_bit(NFS_INO_COMMIT, &nfsi->flags);
12424e857c58SPeter Zijlstra 	smp_mb__after_atomic();
124371d0a611STrond Myklebust 	wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
124471d0a611STrond Myklebust }
124571d0a611STrond Myklebust 
12460b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
12471da177e4SLinus Torvalds {
12480b7c0153SFred Isaman 	put_nfs_open_context(data->context);
12490b7c0153SFred Isaman 	nfs_commit_free(data);
12501da177e4SLinus Torvalds }
1251e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
12521da177e4SLinus Torvalds 
12530b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
12549ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
12559f0ec176SAndy Adamson 			int how, int flags)
12561da177e4SLinus Torvalds {
125707737691STrond Myklebust 	struct rpc_task *task;
12589ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1259bdc7f021STrond Myklebust 	struct rpc_message msg = {
1260bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1261bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
12629ace33cdSFred Isaman 		.rpc_cred = data->cred,
1263bdc7f021STrond Myklebust 	};
126484115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
126507737691STrond Myklebust 		.task = &data->task,
12669ace33cdSFred Isaman 		.rpc_client = clnt,
1267bdc7f021STrond Myklebust 		.rpc_message = &msg,
12689ace33cdSFred Isaman 		.callback_ops = call_ops,
126984115e1cSTrond Myklebust 		.callback_data = data,
1270101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
12719f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
12723ff7576dSTrond Myklebust 		.priority = priority,
127384115e1cSTrond Myklebust 	};
1274788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
12759ace33cdSFred Isaman 	NFS_PROTO(data->inode)->commit_setup(data, &msg);
12761da177e4SLinus Torvalds 
1277a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1278bdc7f021STrond Myklebust 
12798c21c62cSWeston Andros Adamson 	nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client,
12808c21c62cSWeston Andros Adamson 		NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg);
12818c21c62cSWeston Andros Adamson 
128207737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1283dbae4c73STrond Myklebust 	if (IS_ERR(task))
1284dbae4c73STrond Myklebust 		return PTR_ERR(task);
1285d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1286d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
128707737691STrond Myklebust 	rpc_put_task(task);
1288dbae4c73STrond Myklebust 	return 0;
12891da177e4SLinus Torvalds }
1290e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds /*
12939ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
12949ace33cdSFred Isaman  */
12950b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1296988b6dceSFred Isaman 		     struct list_head *head,
1297f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1298f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
12999ace33cdSFred Isaman {
13009ace33cdSFred Isaman 	struct nfs_page *first = nfs_list_entry(head->next);
13013d4ff43dSAl Viro 	struct inode *inode = first->wb_context->dentry->d_inode;
13029ace33cdSFred Isaman 
13039ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
13049ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
13059ace33cdSFred Isaman 
13069ace33cdSFred Isaman 	list_splice_init(head, &data->pages);
13079ace33cdSFred Isaman 
13089ace33cdSFred Isaman 	data->inode	  = inode;
13099ace33cdSFred Isaman 	data->cred	  = first->wb_context->cred;
1310988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
13119ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1312f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1313b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
13149ace33cdSFred Isaman 
13159ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
13169ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
13179ace33cdSFred Isaman 	data->args.offset = 0;
13189ace33cdSFred Isaman 	data->args.count  = 0;
13190b7c0153SFred Isaman 	data->context     = get_nfs_open_context(first->wb_context);
13209ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
13219ace33cdSFred Isaman 	data->res.verf    = &data->verf;
13229ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
13239ace33cdSFred Isaman }
1324e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
13259ace33cdSFred Isaman 
1326e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1327ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1328ea2cf228SFred Isaman 		      struct nfs_commit_info *cinfo)
132964bfeb49SFred Isaman {
133064bfeb49SFred Isaman 	struct nfs_page *req;
133164bfeb49SFred Isaman 
133264bfeb49SFred Isaman 	while (!list_empty(page_list)) {
133364bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
133464bfeb49SFred Isaman 		nfs_list_remove_request(req);
1335ea2cf228SFred Isaman 		nfs_mark_request_commit(req, lseg, cinfo);
133656f9cd68SFred Isaman 		if (!cinfo->dreq) {
133764bfeb49SFred Isaman 			dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1338d56b4ddfSMel Gorman 			dec_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
133964bfeb49SFred Isaman 				     BDI_RECLAIMABLE);
134056f9cd68SFred Isaman 		}
13411d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
134264bfeb49SFred Isaman 	}
134364bfeb49SFred Isaman }
1344e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
134564bfeb49SFred Isaman 
13469ace33cdSFred Isaman /*
13471da177e4SLinus Torvalds  * Commit dirty pages
13481da177e4SLinus Torvalds  */
13491da177e4SLinus Torvalds static int
1350ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1351ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
13521da177e4SLinus Torvalds {
13530b7c0153SFred Isaman 	struct nfs_commit_data	*data;
13541da177e4SLinus Torvalds 
1355c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds 	if (!data)
13581da177e4SLinus Torvalds 		goto out_bad;
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 	/* Set up the argument struct */
1361f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
1362f453a54aSFred Isaman 	atomic_inc(&cinfo->mds->rpcs_out);
13639f0ec176SAndy Adamson 	return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops,
13649f0ec176SAndy Adamson 				   how, 0);
13651da177e4SLinus Torvalds  out_bad:
1366ea2cf228SFred Isaman 	nfs_retry_commit(head, NULL, cinfo);
1367f453a54aSFred Isaman 	cinfo->completion_ops->error_cleanup(NFS_I(inode));
13681da177e4SLinus Torvalds 	return -ENOMEM;
13691da177e4SLinus Torvalds }
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds /*
13721da177e4SLinus Torvalds  * COMMIT call returned
13731da177e4SLinus Torvalds  */
1374788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
13751da177e4SLinus Torvalds {
13760b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
13771da177e4SLinus Torvalds 
1378a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
13791da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
13801da177e4SLinus Torvalds 
1381788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1382c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
1383c9d8f89dSTrond Myklebust }
1384c9d8f89dSTrond Myklebust 
1385f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1386c9d8f89dSTrond Myklebust {
1387c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1388c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1389f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
1390788e7a89STrond Myklebust 
13911da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
13921da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
13931da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1394d6d6dc7cSFred Isaman 		nfs_clear_page_commit(req->wb_page);
13951da177e4SLinus Torvalds 
13961e8968c5SNiels de Vos 		dprintk("NFS:       commit (%s/%llu %d@%lld)",
13973d4ff43dSAl Viro 			req->wb_context->dentry->d_sb->s_id,
13981e8968c5SNiels de Vos 			(unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
13991da177e4SLinus Torvalds 			req->wb_bytes,
14001da177e4SLinus Torvalds 			(long long)req_offset(req));
1401c9d8f89dSTrond Myklebust 		if (status < 0) {
1402c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
14031da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1404c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
14051da177e4SLinus Torvalds 			goto next;
14061da177e4SLinus Torvalds 		}
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
14091da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
14102f2c63bcSTrond Myklebust 		if (!memcmp(&req->wb_verf, &data->verf.verifier, sizeof(req->wb_verf))) {
14111da177e4SLinus Torvalds 			/* We have a match */
14121da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
14131da177e4SLinus Torvalds 			dprintk(" OK\n");
14141da177e4SLinus Torvalds 			goto next;
14151da177e4SLinus Torvalds 		}
14161da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
14171da177e4SLinus Torvalds 		dprintk(" mismatch\n");
14186d884e8fSFred 		nfs_mark_request_dirty(req);
141905990d1bSTrond Myklebust 		set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
14201da177e4SLinus Torvalds 	next:
14211d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
14221da177e4SLinus Torvalds 	}
1423f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1424f453a54aSFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
1425f453a54aSFred Isaman 		nfs_commit_clear_lock(NFS_I(data->inode));
14265917ce84SFred Isaman }
14275917ce84SFred Isaman 
14285917ce84SFred Isaman static void nfs_commit_release(void *calldata)
14295917ce84SFred Isaman {
14300b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
14315917ce84SFred Isaman 
1432f453a54aSFred Isaman 	data->completion_ops->completion(data);
1433c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
14341da177e4SLinus Torvalds }
1435788e7a89STrond Myklebust 
1436788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
14370b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1438788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1439788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1440788e7a89STrond Myklebust };
14411da177e4SLinus Torvalds 
1442f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1443f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1444f453a54aSFred Isaman 	.error_cleanup = nfs_commit_clear_lock,
1445f453a54aSFred Isaman };
1446f453a54aSFred Isaman 
14471763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1448ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
144984c53ab5SFred Isaman {
145084c53ab5SFred Isaman 	int status;
145184c53ab5SFred Isaman 
1452ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
145384c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1454ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
145584c53ab5SFred Isaman 	return status;
145684c53ab5SFred Isaman }
145784c53ab5SFred Isaman 
1458b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
14591da177e4SLinus Torvalds {
14601da177e4SLinus Torvalds 	LIST_HEAD(head);
1461ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
146271d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1463b8413f98STrond Myklebust 	int res;
14641da177e4SLinus Torvalds 
1465b8413f98STrond Myklebust 	res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1466b8413f98STrond Myklebust 	if (res <= 0)
1467c5efa5fcSTrond Myklebust 		goto out_mark_dirty;
1468ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1469ea2cf228SFred Isaman 	res = nfs_scan_commit(inode, &head, &cinfo);
14701da177e4SLinus Torvalds 	if (res) {
1471a861a1e1SFred Isaman 		int error;
1472a861a1e1SFred Isaman 
1473ea2cf228SFred Isaman 		error = nfs_generic_commit_list(inode, &head, how, &cinfo);
14741da177e4SLinus Torvalds 		if (error < 0)
14751da177e4SLinus Torvalds 			return error;
1476b8413f98STrond Myklebust 		if (!may_wait)
1477b8413f98STrond Myklebust 			goto out_mark_dirty;
1478*74316201SNeilBrown 		error = wait_on_bit_action(&NFS_I(inode)->flags,
1479b8413f98STrond Myklebust 				NFS_INO_COMMIT,
148071d0a611STrond Myklebust 				nfs_wait_bit_killable,
148171d0a611STrond Myklebust 				TASK_KILLABLE);
1482b8413f98STrond Myklebust 		if (error < 0)
1483b8413f98STrond Myklebust 			return error;
148471d0a611STrond Myklebust 	} else
148571d0a611STrond Myklebust 		nfs_commit_clear_lock(NFS_I(inode));
1486c5efa5fcSTrond Myklebust 	return res;
1487c5efa5fcSTrond Myklebust 	/* Note: If we exit without ensuring that the commit is complete,
1488c5efa5fcSTrond Myklebust 	 * we must mark the inode as dirty. Otherwise, future calls to
1489c5efa5fcSTrond Myklebust 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1490c5efa5fcSTrond Myklebust 	 * that the data is on the disk.
1491c5efa5fcSTrond Myklebust 	 */
1492c5efa5fcSTrond Myklebust out_mark_dirty:
1493c5efa5fcSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
14941da177e4SLinus Torvalds 	return res;
14951da177e4SLinus Torvalds }
14968fc795f7STrond Myklebust 
14978fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
14988fc795f7STrond Myklebust {
1499420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1500420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1501420e3646STrond Myklebust 	int ret = 0;
15028fc795f7STrond Myklebust 
15033236c3e1SJeff Layton 	/* no commits means nothing needs to be done */
1504ea2cf228SFred Isaman 	if (!nfsi->commit_info.ncommit)
15053236c3e1SJeff Layton 		return ret;
15063236c3e1SJeff Layton 
1507a00dd6c0SJeff Layton 	if (wbc->sync_mode == WB_SYNC_NONE) {
1508a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1509a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1510420e3646STrond Myklebust 		 */
1511ea2cf228SFred Isaman 		if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1))
1512420e3646STrond Myklebust 			goto out_mark_dirty;
1513420e3646STrond Myklebust 
1514a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1515420e3646STrond Myklebust 		flags = 0;
1516a00dd6c0SJeff Layton 	}
1517a00dd6c0SJeff Layton 
1518420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1519420e3646STrond Myklebust 	if (ret >= 0) {
1520420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1521420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1522420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1523420e3646STrond Myklebust 			else
1524420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1525420e3646STrond Myklebust 		}
15268fc795f7STrond Myklebust 		return 0;
1527420e3646STrond Myklebust 	}
1528420e3646STrond Myklebust out_mark_dirty:
15298fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
15308fc795f7STrond Myklebust 	return ret;
15318fc795f7STrond Myklebust }
1532c63c7b05STrond Myklebust #else
15338fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
15348fc795f7STrond Myklebust {
15358fc795f7STrond Myklebust 	return 0;
15368fc795f7STrond Myklebust }
15371da177e4SLinus Torvalds #endif
15381da177e4SLinus Torvalds 
15398fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
15408fc795f7STrond Myklebust {
1541a8d8f02cSBryan Schumaker 	return nfs_commit_unstable_pages(inode, wbc);
1542a8d8f02cSBryan Schumaker }
154389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode);
1544863a3c6cSAndy Adamson 
1545acdc53b2STrond Myklebust /*
1546acdc53b2STrond Myklebust  * flush the inode to disk.
1547acdc53b2STrond Myklebust  */
1548acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
154934901f70STrond Myklebust {
155034901f70STrond Myklebust 	struct writeback_control wbc = {
155172cb77f4STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
155234901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
1553d7fb1207STrond Myklebust 		.range_start = 0,
1554d7fb1207STrond Myklebust 		.range_end = LLONG_MAX,
155534901f70STrond Myklebust 	};
1556f4ce1299STrond Myklebust 	int ret;
155734901f70STrond Myklebust 
1558f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_enter(inode);
1559f4ce1299STrond Myklebust 
1560f4ce1299STrond Myklebust 	ret = sync_inode(inode, &wbc);
1561f4ce1299STrond Myklebust 
1562f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_exit(inode, ret);
1563f4ce1299STrond Myklebust 	return ret;
15641c75950bSTrond Myklebust }
1565ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all);
15661c75950bSTrond Myklebust 
15671b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
15681b3b4a1aSTrond Myklebust {
15691b3b4a1aSTrond Myklebust 	struct nfs_page *req;
15701b3b4a1aSTrond Myklebust 	int ret = 0;
15711b3b4a1aSTrond Myklebust 
15721b3b4a1aSTrond Myklebust 	for (;;) {
1573ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
15741b3b4a1aSTrond Myklebust 		req = nfs_page_find_request(page);
15751b3b4a1aSTrond Myklebust 		if (req == NULL)
15761b3b4a1aSTrond Myklebust 			break;
15777ad84aa9STrond Myklebust 		if (nfs_lock_request(req)) {
15788dd37758STrond Myklebust 			nfs_clear_request_commit(req);
15791b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
15801b3b4a1aSTrond Myklebust 			/*
15811b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
15821b3b4a1aSTrond Myklebust 			 * page as being dirty
15831b3b4a1aSTrond Myklebust 			 */
15841b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
15851d1afcbcSTrond Myklebust 			nfs_unlock_and_release_request(req);
15861b3b4a1aSTrond Myklebust 			break;
15871b3b4a1aSTrond Myklebust 		}
15881b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
1589c9edda71STrond Myklebust 		nfs_release_request(req);
15901b3b4a1aSTrond Myklebust 		if (ret < 0)
1591c988950eSTrond Myklebust 			break;
15921b3b4a1aSTrond Myklebust 	}
15931b3b4a1aSTrond Myklebust 	return ret;
15941b3b4a1aSTrond Myklebust }
15951b3b4a1aSTrond Myklebust 
15961c75950bSTrond Myklebust /*
15971c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
15981c75950bSTrond Myklebust  */
15991c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
16001c75950bSTrond Myklebust {
160129418aa4SMel Gorman 	loff_t range_start = page_file_offset(page);
16027f2f12d9STrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
16037f2f12d9STrond Myklebust 	struct writeback_control wbc = {
16047f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
16057f2f12d9STrond Myklebust 		.nr_to_write = 0,
16067f2f12d9STrond Myklebust 		.range_start = range_start,
16077f2f12d9STrond Myklebust 		.range_end = range_end,
16087f2f12d9STrond Myklebust 	};
16097f2f12d9STrond Myklebust 	int ret;
16107f2f12d9STrond Myklebust 
1611f4ce1299STrond Myklebust 	trace_nfs_writeback_page_enter(inode);
1612f4ce1299STrond Myklebust 
16130522f6adSTrond Myklebust 	for (;;) {
1614ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
16157f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
16167f2f12d9STrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
16177f2f12d9STrond Myklebust 			if (ret < 0)
16187f2f12d9STrond Myklebust 				goto out_error;
16190522f6adSTrond Myklebust 			continue;
16207f2f12d9STrond Myklebust 		}
1621f4ce1299STrond Myklebust 		ret = 0;
16220522f6adSTrond Myklebust 		if (!PagePrivate(page))
16230522f6adSTrond Myklebust 			break;
16240522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
16257f2f12d9STrond Myklebust 		if (ret < 0)
16267f2f12d9STrond Myklebust 			goto out_error;
16277f2f12d9STrond Myklebust 	}
16287f2f12d9STrond Myklebust out_error:
1629f4ce1299STrond Myklebust 	trace_nfs_writeback_page_exit(inode, ret);
16307f2f12d9STrond Myklebust 	return ret;
16311c75950bSTrond Myklebust }
16321c75950bSTrond Myklebust 
1633074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
1634074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1635a6bc32b8SMel Gorman 		struct page *page, enum migrate_mode mode)
1636074cc1deSTrond Myklebust {
16372da95652SJeff Layton 	/*
16382da95652SJeff Layton 	 * If PagePrivate is set, then the page is currently associated with
16392da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
16402da95652SJeff Layton 	 *
16412da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
16422da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
16432da95652SJeff Layton 	 *        the page lock.
16442da95652SJeff Layton 	 */
16452da95652SJeff Layton 	if (PagePrivate(page))
16462da95652SJeff Layton 		return -EBUSY;
1647074cc1deSTrond Myklebust 
16488c209ce7SDavid Howells 	if (!nfs_fscache_release_page(page, GFP_KERNEL))
16498c209ce7SDavid Howells 		return -EBUSY;
1650074cc1deSTrond Myklebust 
1651a6bc32b8SMel Gorman 	return migrate_page(mapping, newpage, page, mode);
1652074cc1deSTrond Myklebust }
1653074cc1deSTrond Myklebust #endif
1654074cc1deSTrond Myklebust 
1655f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
16561da177e4SLinus Torvalds {
16571da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
1658c0752cdfSAnna Schumaker 					     sizeof(struct nfs_rw_header),
16591da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
166020c2df83SPaul Mundt 					     NULL);
16611da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
16621da177e4SLinus Torvalds 		return -ENOMEM;
16631da177e4SLinus Torvalds 
166493d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
16651da177e4SLinus Torvalds 						     nfs_wdata_cachep);
16661da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
16673dd4765fSJeff Layton 		goto out_destroy_write_cache;
16681da177e4SLinus Torvalds 
16690b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
16700b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
16710b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
16720b7c0153SFred Isaman 					     NULL);
16730b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
16743dd4765fSJeff Layton 		goto out_destroy_write_mempool;
16750b7c0153SFred Isaman 
167693d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
16774c100210SYanchuan Nian 						      nfs_cdata_cachep);
16781da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
16793dd4765fSJeff Layton 		goto out_destroy_commit_cache;
16801da177e4SLinus Torvalds 
168189a09141SPeter Zijlstra 	/*
168289a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
168389a09141SPeter Zijlstra 	 *
168489a09141SPeter Zijlstra 	 *  64MB:    8192k
168589a09141SPeter Zijlstra 	 * 128MB:   11585k
168689a09141SPeter Zijlstra 	 * 256MB:   16384k
168789a09141SPeter Zijlstra 	 * 512MB:   23170k
168889a09141SPeter Zijlstra 	 *   1GB:   32768k
168989a09141SPeter Zijlstra 	 *   2GB:   46340k
169089a09141SPeter Zijlstra 	 *   4GB:   65536k
169189a09141SPeter Zijlstra 	 *   8GB:   92681k
169289a09141SPeter Zijlstra 	 *  16GB:  131072k
169389a09141SPeter Zijlstra 	 *
169489a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
169589a09141SPeter Zijlstra 	 * Limit the default to 256M
169689a09141SPeter Zijlstra 	 */
169789a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
169889a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
169989a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
170089a09141SPeter Zijlstra 
17011da177e4SLinus Torvalds 	return 0;
17023dd4765fSJeff Layton 
17033dd4765fSJeff Layton out_destroy_commit_cache:
17043dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
17053dd4765fSJeff Layton out_destroy_write_mempool:
17063dd4765fSJeff Layton 	mempool_destroy(nfs_wdata_mempool);
17073dd4765fSJeff Layton out_destroy_write_cache:
17083dd4765fSJeff Layton 	kmem_cache_destroy(nfs_wdata_cachep);
17093dd4765fSJeff Layton 	return -ENOMEM;
17101da177e4SLinus Torvalds }
17111da177e4SLinus Torvalds 
1712266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
17131da177e4SLinus Torvalds {
17141da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
17153dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
17161da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
17171a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
17181da177e4SLinus Torvalds }
17191da177e4SLinus Torvalds 
17204a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops = {
1721a4cdda59SAnna Schumaker 	.rw_mode		= FMODE_WRITE,
17224a0de55cSAnna Schumaker 	.rw_alloc_header	= nfs_writehdr_alloc,
17234a0de55cSAnna Schumaker 	.rw_free_header		= nfs_writehdr_free,
1724a4cdda59SAnna Schumaker 	.rw_release		= nfs_writeback_release_common,
17250eecb214SAnna Schumaker 	.rw_done		= nfs_writeback_done,
17260eecb214SAnna Schumaker 	.rw_result		= nfs_writeback_result,
17271ed26f33SAnna Schumaker 	.rw_initiate		= nfs_initiate_write,
17284a0de55cSAnna Schumaker };
1729