xref: /openbmc/linux/fs/nfs/write.c (revision 353db796)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/fs/nfs/write.c
31da177e4SLinus Torvalds  *
47c85d900STrond Myklebust  * Write file data over NFS.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/types.h>
101da177e4SLinus Torvalds #include <linux/slab.h>
111da177e4SLinus Torvalds #include <linux/mm.h>
121da177e4SLinus Torvalds #include <linux/pagemap.h>
131da177e4SLinus Torvalds #include <linux/file.h>
141da177e4SLinus Torvalds #include <linux/writeback.h>
1589a09141SPeter Zijlstra #include <linux/swap.h>
16074cc1deSTrond Myklebust #include <linux/migrate.h>
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
191da177e4SLinus Torvalds #include <linux/nfs_fs.h>
201da177e4SLinus Torvalds #include <linux/nfs_mount.h>
211da177e4SLinus Torvalds #include <linux/nfs_page.h>
223fcfab16SAndrew Morton #include <linux/backing-dev.h>
23afeacc8cSPaul Gortmaker #include <linux/export.h>
243fcfab16SAndrew Morton 
251da177e4SLinus Torvalds #include <asm/uaccess.h>
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #include "delegation.h"
2849a70f27STrond Myklebust #include "internal.h"
2991d5b470SChuck Lever #include "iostat.h"
30def6ed7eSAndy Adamson #include "nfs4_fs.h"
31074cc1deSTrond Myklebust #include "fscache.h"
3294ad1c80SFred Isaman #include "pnfs.h"
331da177e4SLinus Torvalds 
34f4ce1299STrond Myklebust #include "nfstrace.h"
35f4ce1299STrond Myklebust 
361da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #define MIN_POOL_WRITE		(32)
391da177e4SLinus Torvalds #define MIN_POOL_COMMIT		(4)
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds /*
421da177e4SLinus Torvalds  * Local function declarations
431da177e4SLinus Torvalds  */
44f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req);
45788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops;
46061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops;
47f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops;
484a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops;
49d4581383SWeston Andros Adamson static void nfs_clear_request_commit(struct nfs_page *req);
5002d1426cSWeston Andros Adamson static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
5102d1426cSWeston Andros Adamson 				      struct inode *inode);
523a3908c8STrond Myklebust static struct nfs_page *
533a3908c8STrond Myklebust nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
543a3908c8STrond Myklebust 						struct page *page);
551da177e4SLinus Torvalds 
56e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep;
573feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
580b7c0153SFred Isaman static struct kmem_cache *nfs_cdata_cachep;
591da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
601da177e4SLinus Torvalds 
610b7c0153SFred Isaman struct nfs_commit_data *nfs_commitdata_alloc(void)
621da177e4SLinus Torvalds {
63192e501bSMel Gorman 	struct nfs_commit_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOIO);
6440859d7eSChuck Lever 
651da177e4SLinus Torvalds 	if (p) {
661da177e4SLinus Torvalds 		memset(p, 0, sizeof(*p));
671da177e4SLinus Torvalds 		INIT_LIST_HEAD(&p->pages);
681da177e4SLinus Torvalds 	}
691da177e4SLinus Torvalds 	return p;
701da177e4SLinus Torvalds }
71e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
721da177e4SLinus Torvalds 
730b7c0153SFred Isaman void nfs_commit_free(struct nfs_commit_data *p)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
761da177e4SLinus Torvalds }
77e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free);
781da177e4SLinus Torvalds 
791e7f3a48SWeston Andros Adamson static struct nfs_pgio_header *nfs_writehdr_alloc(void)
803feb2d49STrond Myklebust {
811e7f3a48SWeston Andros Adamson 	struct nfs_pgio_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOIO);
823feb2d49STrond Myklebust 
834a0de55cSAnna Schumaker 	if (p)
843feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
853feb2d49STrond Myklebust 	return p;
863feb2d49STrond Myklebust }
873feb2d49STrond Myklebust 
881e7f3a48SWeston Andros Adamson static void nfs_writehdr_free(struct nfs_pgio_header *hdr)
896c75dc0dSFred Isaman {
901e7f3a48SWeston Andros Adamson 	mempool_free(hdr, nfs_wdata_mempool);
913feb2d49STrond Myklebust }
921da177e4SLinus Torvalds 
937b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
947b159fc1STrond Myklebust {
957b159fc1STrond Myklebust 	ctx->error = error;
967b159fc1STrond Myklebust 	smp_wmb();
977b159fc1STrond Myklebust 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
987b159fc1STrond Myklebust }
997b159fc1STrond Myklebust 
10084d3a9a9SWeston Andros Adamson /*
10184d3a9a9SWeston Andros Adamson  * nfs_page_find_head_request_locked - find head request associated with @page
10284d3a9a9SWeston Andros Adamson  *
10384d3a9a9SWeston Andros Adamson  * must be called while holding the inode lock.
10484d3a9a9SWeston Andros Adamson  *
10584d3a9a9SWeston Andros Adamson  * returns matching head request with reference held, or NULL if not found.
10684d3a9a9SWeston Andros Adamson  */
10729418aa4SMel Gorman static struct nfs_page *
10884d3a9a9SWeston Andros Adamson nfs_page_find_head_request_locked(struct nfs_inode *nfsi, struct page *page)
109277459d2STrond Myklebust {
110277459d2STrond Myklebust 	struct nfs_page *req = NULL;
111277459d2STrond Myklebust 
11229418aa4SMel Gorman 	if (PagePrivate(page))
113277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
11402d1426cSWeston Andros Adamson 	else if (unlikely(PageSwapCache(page)))
11502d1426cSWeston Andros Adamson 		req = nfs_page_search_commits_for_head_request_locked(nfsi,
11602d1426cSWeston Andros Adamson 			page);
11729418aa4SMel Gorman 
11884d3a9a9SWeston Andros Adamson 	if (req) {
11984d3a9a9SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_head != req);
12029418aa4SMel Gorman 		kref_get(&req->wb_kref);
12184d3a9a9SWeston Andros Adamson 	}
12229418aa4SMel Gorman 
123277459d2STrond Myklebust 	return req;
124277459d2STrond Myklebust }
125277459d2STrond Myklebust 
12684d3a9a9SWeston Andros Adamson /*
12784d3a9a9SWeston Andros Adamson  * nfs_page_find_head_request - find head request associated with @page
12884d3a9a9SWeston Andros Adamson  *
12984d3a9a9SWeston Andros Adamson  * returns matching head request with reference held, or NULL if not found.
13084d3a9a9SWeston Andros Adamson  */
13184d3a9a9SWeston Andros Adamson static struct nfs_page *nfs_page_find_head_request(struct page *page)
132277459d2STrond Myklebust {
133d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
134277459d2STrond Myklebust 	struct nfs_page *req = NULL;
135277459d2STrond Myklebust 
136587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
13784d3a9a9SWeston Andros Adamson 	req = nfs_page_find_head_request_locked(NFS_I(inode), page);
138587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
139277459d2STrond Myklebust 	return req;
140277459d2STrond Myklebust }
141277459d2STrond Myklebust 
1421da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1431da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1441da177e4SLinus Torvalds {
145d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
146a3d01454STrond Myklebust 	loff_t end, i_size;
147a3d01454STrond Myklebust 	pgoff_t end_index;
1481da177e4SLinus Torvalds 
149a3d01454STrond Myklebust 	spin_lock(&inode->i_lock);
150a3d01454STrond Myklebust 	i_size = i_size_read(inode);
151a3d01454STrond Myklebust 	end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
152d56b4ddfSMel Gorman 	if (i_size > 0 && page_file_index(page) < end_index)
153a3d01454STrond Myklebust 		goto out;
154d56b4ddfSMel Gorman 	end = page_file_offset(page) + ((loff_t)offset+count);
1551da177e4SLinus Torvalds 	if (i_size >= end)
156a3d01454STrond Myklebust 		goto out;
1571da177e4SLinus Torvalds 	i_size_write(inode, end);
158a3d01454STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
159a3d01454STrond Myklebust out:
160a3d01454STrond Myklebust 	spin_unlock(&inode->i_lock);
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds 
163a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
164a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
165a301b777STrond Myklebust {
166d56b4ddfSMel Gorman 	nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page));
167a301b777STrond Myklebust }
168a301b777STrond Myklebust 
169d72ddcbaSWeston Andros Adamson /*
170d72ddcbaSWeston Andros Adamson  * nfs_page_group_search_locked
171d72ddcbaSWeston Andros Adamson  * @head - head request of page group
172d72ddcbaSWeston Andros Adamson  * @page_offset - offset into page
173d72ddcbaSWeston Andros Adamson  *
174d72ddcbaSWeston Andros Adamson  * Search page group with head @head to find a request that contains the
175d72ddcbaSWeston Andros Adamson  * page offset @page_offset.
176d72ddcbaSWeston Andros Adamson  *
177d72ddcbaSWeston Andros Adamson  * Returns a pointer to the first matching nfs request, or NULL if no
178d72ddcbaSWeston Andros Adamson  * match is found.
179d72ddcbaSWeston Andros Adamson  *
180d72ddcbaSWeston Andros Adamson  * Must be called with the page group lock held
181d72ddcbaSWeston Andros Adamson  */
182d72ddcbaSWeston Andros Adamson static struct nfs_page *
183d72ddcbaSWeston Andros Adamson nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset)
184d72ddcbaSWeston Andros Adamson {
185d72ddcbaSWeston Andros Adamson 	struct nfs_page *req;
186d72ddcbaSWeston Andros Adamson 
187d72ddcbaSWeston Andros Adamson 	WARN_ON_ONCE(head != head->wb_head);
188d72ddcbaSWeston Andros Adamson 	WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_head->wb_flags));
189d72ddcbaSWeston Andros Adamson 
190d72ddcbaSWeston Andros Adamson 	req = head;
191d72ddcbaSWeston Andros Adamson 	do {
192d72ddcbaSWeston Andros Adamson 		if (page_offset >= req->wb_pgbase &&
193d72ddcbaSWeston Andros Adamson 		    page_offset < (req->wb_pgbase + req->wb_bytes))
194d72ddcbaSWeston Andros Adamson 			return req;
195d72ddcbaSWeston Andros Adamson 
196d72ddcbaSWeston Andros Adamson 		req = req->wb_this_page;
197d72ddcbaSWeston Andros Adamson 	} while (req != head);
198d72ddcbaSWeston Andros Adamson 
199d72ddcbaSWeston Andros Adamson 	return NULL;
200d72ddcbaSWeston Andros Adamson }
201d72ddcbaSWeston Andros Adamson 
202d72ddcbaSWeston Andros Adamson /*
203d72ddcbaSWeston Andros Adamson  * nfs_page_group_covers_page
204d72ddcbaSWeston Andros Adamson  * @head - head request of page group
205d72ddcbaSWeston Andros Adamson  *
206d72ddcbaSWeston Andros Adamson  * Return true if the page group with head @head covers the whole page,
207d72ddcbaSWeston Andros Adamson  * returns false otherwise
208d72ddcbaSWeston Andros Adamson  */
209d72ddcbaSWeston Andros Adamson static bool nfs_page_group_covers_page(struct nfs_page *req)
210d72ddcbaSWeston Andros Adamson {
211d72ddcbaSWeston Andros Adamson 	struct nfs_page *tmp;
212d72ddcbaSWeston Andros Adamson 	unsigned int pos = 0;
213d72ddcbaSWeston Andros Adamson 	unsigned int len = nfs_page_length(req->wb_page);
214d72ddcbaSWeston Andros Adamson 
215fd2f3a06SWeston Andros Adamson 	nfs_page_group_lock(req, false);
216d72ddcbaSWeston Andros Adamson 
217d72ddcbaSWeston Andros Adamson 	do {
218d72ddcbaSWeston Andros Adamson 		tmp = nfs_page_group_search_locked(req->wb_head, pos);
219d72ddcbaSWeston Andros Adamson 		if (tmp) {
220d72ddcbaSWeston Andros Adamson 			/* no way this should happen */
221d72ddcbaSWeston Andros Adamson 			WARN_ON_ONCE(tmp->wb_pgbase != pos);
222d72ddcbaSWeston Andros Adamson 			pos += tmp->wb_bytes - (pos - tmp->wb_pgbase);
223d72ddcbaSWeston Andros Adamson 		}
224d72ddcbaSWeston Andros Adamson 	} while (tmp && pos < len);
225d72ddcbaSWeston Andros Adamson 
226d72ddcbaSWeston Andros Adamson 	nfs_page_group_unlock(req);
227d72ddcbaSWeston Andros Adamson 	WARN_ON_ONCE(pos > len);
228d72ddcbaSWeston Andros Adamson 	return pos == len;
229d72ddcbaSWeston Andros Adamson }
230d72ddcbaSWeston Andros Adamson 
2311da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
2321da177e4SLinus Torvalds  * covers the full page.
2331da177e4SLinus Torvalds  */
234d72ddcbaSWeston Andros Adamson static void nfs_mark_uptodate(struct nfs_page *req)
2351da177e4SLinus Torvalds {
236d72ddcbaSWeston Andros Adamson 	if (PageUptodate(req->wb_page))
2371da177e4SLinus Torvalds 		return;
238d72ddcbaSWeston Andros Adamson 	if (!nfs_page_group_covers_page(req))
2391da177e4SLinus Torvalds 		return;
240d72ddcbaSWeston Andros Adamson 	SetPageUptodate(req->wb_page);
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
2441da177e4SLinus Torvalds {
245e87b4c7aSNeilBrown 	int ret = 0;
2461da177e4SLinus Torvalds 	if (wbc->for_reclaim)
247c63c7b05STrond Myklebust 		return FLUSH_HIGHPRI | FLUSH_STABLE;
248e87b4c7aSNeilBrown 	if (wbc->sync_mode == WB_SYNC_ALL)
249e87b4c7aSNeilBrown 		ret = FLUSH_COND_STABLE;
250b17621feSWu Fengguang 	if (wbc->for_kupdate || wbc->for_background)
251e87b4c7aSNeilBrown 		ret |= FLUSH_LOWPRI;
252e87b4c7aSNeilBrown 	return ret;
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds /*
25689a09141SPeter Zijlstra  * NFS congestion control
25789a09141SPeter Zijlstra  */
25889a09141SPeter Zijlstra 
25989a09141SPeter Zijlstra int nfs_congestion_kb;
26089a09141SPeter Zijlstra 
26189a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
26289a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
26389a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
26489a09141SPeter Zijlstra 
265deed85e7STrond Myklebust static void nfs_set_page_writeback(struct page *page)
26689a09141SPeter Zijlstra {
267deed85e7STrond Myklebust 	struct nfs_server *nfss = NFS_SERVER(page_file_mapping(page)->host);
2685a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
2695a6d41b3STrond Myklebust 
270deed85e7STrond Myklebust 	WARN_ON_ONCE(ret != 0);
27189a09141SPeter Zijlstra 
272277866a0SPeter Zijlstra 	if (atomic_long_inc_return(&nfss->writeback) >
2738aa7e847SJens Axboe 			NFS_CONGESTION_ON_THRESH) {
2748aa7e847SJens Axboe 		set_bdi_congested(&nfss->backing_dev_info,
2758aa7e847SJens Axboe 					BLK_RW_ASYNC);
2768aa7e847SJens Axboe 	}
27789a09141SPeter Zijlstra }
27889a09141SPeter Zijlstra 
27920633f04SWeston Andros Adamson static void nfs_end_page_writeback(struct nfs_page *req)
28089a09141SPeter Zijlstra {
28120633f04SWeston Andros Adamson 	struct inode *inode = page_file_mapping(req->wb_page)->host;
28289a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
28389a09141SPeter Zijlstra 
28420633f04SWeston Andros Adamson 	if (!nfs_page_group_sync_on_bit(req, PG_WB_END))
28520633f04SWeston Andros Adamson 		return;
28620633f04SWeston Andros Adamson 
28720633f04SWeston Andros Adamson 	end_page_writeback(req->wb_page);
288c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
2898aa7e847SJens Axboe 		clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
29089a09141SPeter Zijlstra }
29189a09141SPeter Zijlstra 
292d4581383SWeston Andros Adamson 
293d4581383SWeston Andros Adamson /* nfs_page_group_clear_bits
294d4581383SWeston Andros Adamson  *   @req - an nfs request
295d4581383SWeston Andros Adamson  * clears all page group related bits from @req
296d4581383SWeston Andros Adamson  */
297d4581383SWeston Andros Adamson static void
298d4581383SWeston Andros Adamson nfs_page_group_clear_bits(struct nfs_page *req)
299e261f51fSTrond Myklebust {
300d4581383SWeston Andros Adamson 	clear_bit(PG_TEARDOWN, &req->wb_flags);
301d4581383SWeston Andros Adamson 	clear_bit(PG_UNLOCKPAGE, &req->wb_flags);
302d4581383SWeston Andros Adamson 	clear_bit(PG_UPTODATE, &req->wb_flags);
303d4581383SWeston Andros Adamson 	clear_bit(PG_WB_END, &req->wb_flags);
304d4581383SWeston Andros Adamson 	clear_bit(PG_REMOVE, &req->wb_flags);
305d4581383SWeston Andros Adamson }
306d4581383SWeston Andros Adamson 
307d4581383SWeston Andros Adamson 
308d4581383SWeston Andros Adamson /*
309d4581383SWeston Andros Adamson  * nfs_unroll_locks_and_wait -  unlock all newly locked reqs and wait on @req
310d4581383SWeston Andros Adamson  *
311d4581383SWeston Andros Adamson  * this is a helper function for nfs_lock_and_join_requests
312d4581383SWeston Andros Adamson  *
313d4581383SWeston Andros Adamson  * @inode - inode associated with request page group, must be holding inode lock
314d4581383SWeston Andros Adamson  * @head  - head request of page group, must be holding head lock
315d4581383SWeston Andros Adamson  * @req   - request that couldn't lock and needs to wait on the req bit lock
316d4581383SWeston Andros Adamson  * @nonblock - if true, don't actually wait
317d4581383SWeston Andros Adamson  *
318d4581383SWeston Andros Adamson  * NOTE: this must be called holding page_group bit lock and inode spin lock
319d4581383SWeston Andros Adamson  *       and BOTH will be released before returning.
320d4581383SWeston Andros Adamson  *
321d4581383SWeston Andros Adamson  * returns 0 on success, < 0 on error.
322d4581383SWeston Andros Adamson  */
323d4581383SWeston Andros Adamson static int
324d4581383SWeston Andros Adamson nfs_unroll_locks_and_wait(struct inode *inode, struct nfs_page *head,
325d4581383SWeston Andros Adamson 			  struct nfs_page *req, bool nonblock)
326d4581383SWeston Andros Adamson 	__releases(&inode->i_lock)
327d4581383SWeston Andros Adamson {
328d4581383SWeston Andros Adamson 	struct nfs_page *tmp;
329e261f51fSTrond Myklebust 	int ret;
330e261f51fSTrond Myklebust 
331d4581383SWeston Andros Adamson 	/* relinquish all the locks successfully grabbed this run */
332d4581383SWeston Andros Adamson 	for (tmp = head ; tmp != req; tmp = tmp->wb_this_page)
333d4581383SWeston Andros Adamson 		nfs_unlock_request(tmp);
334d4581383SWeston Andros Adamson 
335d4581383SWeston Andros Adamson 	WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
336d4581383SWeston Andros Adamson 
337d4581383SWeston Andros Adamson 	/* grab a ref on the request that will be waited on */
338d4581383SWeston Andros Adamson 	kref_get(&req->wb_kref);
339d4581383SWeston Andros Adamson 
340d4581383SWeston Andros Adamson 	nfs_page_group_unlock(head);
341587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
342d4581383SWeston Andros Adamson 
343d4581383SWeston Andros Adamson 	/* release ref from nfs_page_find_head_request_locked */
344d4581383SWeston Andros Adamson 	nfs_release_request(head);
345d4581383SWeston Andros Adamson 
346cfb506e1STrond Myklebust 	if (!nonblock)
347e261f51fSTrond Myklebust 		ret = nfs_wait_on_request(req);
348cfb506e1STrond Myklebust 	else
349cfb506e1STrond Myklebust 		ret = -EAGAIN;
350e261f51fSTrond Myklebust 	nfs_release_request(req);
351d4581383SWeston Andros Adamson 
352d4581383SWeston Andros Adamson 	return ret;
353e261f51fSTrond Myklebust }
354d4581383SWeston Andros Adamson 
355d4581383SWeston Andros Adamson /*
356d4581383SWeston Andros Adamson  * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests
357d4581383SWeston Andros Adamson  *
358d4581383SWeston Andros Adamson  * @destroy_list - request list (using wb_this_page) terminated by @old_head
359d4581383SWeston Andros Adamson  * @old_head - the old head of the list
360d4581383SWeston Andros Adamson  *
361d4581383SWeston Andros Adamson  * All subrequests must be locked and removed from all lists, so at this point
362d4581383SWeston Andros Adamson  * they are only "active" in this function, and possibly in nfs_wait_on_request
363d4581383SWeston Andros Adamson  * with a reference held by some other context.
364d4581383SWeston Andros Adamson  */
365d4581383SWeston Andros Adamson static void
366d4581383SWeston Andros Adamson nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list,
367d4581383SWeston Andros Adamson 				 struct nfs_page *old_head)
368d4581383SWeston Andros Adamson {
369d4581383SWeston Andros Adamson 	while (destroy_list) {
370d4581383SWeston Andros Adamson 		struct nfs_page *subreq = destroy_list;
371d4581383SWeston Andros Adamson 
372d4581383SWeston Andros Adamson 		destroy_list = (subreq->wb_this_page == old_head) ?
373d4581383SWeston Andros Adamson 				   NULL : subreq->wb_this_page;
374d4581383SWeston Andros Adamson 
375d4581383SWeston Andros Adamson 		WARN_ON_ONCE(old_head != subreq->wb_head);
376d4581383SWeston Andros Adamson 
377d4581383SWeston Andros Adamson 		/* make sure old group is not used */
378d4581383SWeston Andros Adamson 		subreq->wb_head = subreq;
379d4581383SWeston Andros Adamson 		subreq->wb_this_page = subreq;
380d4581383SWeston Andros Adamson 
381d4581383SWeston Andros Adamson 		/* subreq is now totally disconnected from page group or any
382d4581383SWeston Andros Adamson 		 * write / commit lists. last chance to wake any waiters */
383d4581383SWeston Andros Adamson 		nfs_unlock_request(subreq);
384d4581383SWeston Andros Adamson 
385d4581383SWeston Andros Adamson 		if (!test_bit(PG_TEARDOWN, &subreq->wb_flags)) {
386d4581383SWeston Andros Adamson 			/* release ref on old head request */
387d4581383SWeston Andros Adamson 			nfs_release_request(old_head);
388d4581383SWeston Andros Adamson 
389d4581383SWeston Andros Adamson 			nfs_page_group_clear_bits(subreq);
390d4581383SWeston Andros Adamson 
391d4581383SWeston Andros Adamson 			/* release the PG_INODE_REF reference */
392d4581383SWeston Andros Adamson 			if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags))
393d4581383SWeston Andros Adamson 				nfs_release_request(subreq);
394d4581383SWeston Andros Adamson 			else
395d4581383SWeston Andros Adamson 				WARN_ON_ONCE(1);
396d4581383SWeston Andros Adamson 		} else {
397d4581383SWeston Andros Adamson 			WARN_ON_ONCE(test_bit(PG_CLEAN, &subreq->wb_flags));
398d4581383SWeston Andros Adamson 			/* zombie requests have already released the last
399d4581383SWeston Andros Adamson 			 * reference and were waiting on the rest of the
400d4581383SWeston Andros Adamson 			 * group to complete. Since it's no longer part of a
401d4581383SWeston Andros Adamson 			 * group, simply free the request */
402d4581383SWeston Andros Adamson 			nfs_page_group_clear_bits(subreq);
403d4581383SWeston Andros Adamson 			nfs_free_request(subreq);
404d4581383SWeston Andros Adamson 		}
405d4581383SWeston Andros Adamson 	}
406d4581383SWeston Andros Adamson }
407d4581383SWeston Andros Adamson 
408d4581383SWeston Andros Adamson /*
409d4581383SWeston Andros Adamson  * nfs_lock_and_join_requests - join all subreqs to the head req and return
410d4581383SWeston Andros Adamson  *                              a locked reference, cancelling any pending
411d4581383SWeston Andros Adamson  *                              operations for this page.
412d4581383SWeston Andros Adamson  *
413d4581383SWeston Andros Adamson  * @page - the page used to lookup the "page group" of nfs_page structures
414d4581383SWeston Andros Adamson  * @nonblock - if true, don't block waiting for request locks
415d4581383SWeston Andros Adamson  *
416d4581383SWeston Andros Adamson  * This function joins all sub requests to the head request by first
417d4581383SWeston Andros Adamson  * locking all requests in the group, cancelling any pending operations
418d4581383SWeston Andros Adamson  * and finally updating the head request to cover the whole range covered by
419d4581383SWeston Andros Adamson  * the (former) group.  All subrequests are removed from any write or commit
420d4581383SWeston Andros Adamson  * lists, unlinked from the group and destroyed.
421d4581383SWeston Andros Adamson  *
422d4581383SWeston Andros Adamson  * Returns a locked, referenced pointer to the head request - which after
423d4581383SWeston Andros Adamson  * this call is guaranteed to be the only request associated with the page.
424d4581383SWeston Andros Adamson  * Returns NULL if no requests are found for @page, or a ERR_PTR if an
425d4581383SWeston Andros Adamson  * error was encountered.
426d4581383SWeston Andros Adamson  */
427d4581383SWeston Andros Adamson static struct nfs_page *
428d4581383SWeston Andros Adamson nfs_lock_and_join_requests(struct page *page, bool nonblock)
429d4581383SWeston Andros Adamson {
430d4581383SWeston Andros Adamson 	struct inode *inode = page_file_mapping(page)->host;
431d4581383SWeston Andros Adamson 	struct nfs_page *head, *subreq;
432d4581383SWeston Andros Adamson 	struct nfs_page *destroy_list = NULL;
433d4581383SWeston Andros Adamson 	unsigned int total_bytes;
434d4581383SWeston Andros Adamson 	int ret;
435d4581383SWeston Andros Adamson 
436d4581383SWeston Andros Adamson try_again:
437d4581383SWeston Andros Adamson 	total_bytes = 0;
438d4581383SWeston Andros Adamson 
439d4581383SWeston Andros Adamson 	WARN_ON_ONCE(destroy_list);
440d4581383SWeston Andros Adamson 
441d4581383SWeston Andros Adamson 	spin_lock(&inode->i_lock);
442d4581383SWeston Andros Adamson 
443d4581383SWeston Andros Adamson 	/*
444d4581383SWeston Andros Adamson 	 * A reference is taken only on the head request which acts as a
445d4581383SWeston Andros Adamson 	 * reference to the whole page group - the group will not be destroyed
446d4581383SWeston Andros Adamson 	 * until the head reference is released.
447d4581383SWeston Andros Adamson 	 */
448d4581383SWeston Andros Adamson 	head = nfs_page_find_head_request_locked(NFS_I(inode), page);
449d4581383SWeston Andros Adamson 
450d4581383SWeston Andros Adamson 	if (!head) {
451587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
452d4581383SWeston Andros Adamson 		return NULL;
453d4581383SWeston Andros Adamson 	}
454d4581383SWeston Andros Adamson 
4557c3af975SWeston Andros Adamson 	/* holding inode lock, so always make a non-blocking call to try the
4567c3af975SWeston Andros Adamson 	 * page group lock */
457fd2f3a06SWeston Andros Adamson 	ret = nfs_page_group_lock(head, true);
45894970014SWeston Andros Adamson 	if (ret < 0) {
45994970014SWeston Andros Adamson 		spin_unlock(&inode->i_lock);
4607c3af975SWeston Andros Adamson 
4617c3af975SWeston Andros Adamson 		if (!nonblock && ret == -EAGAIN) {
4627c3af975SWeston Andros Adamson 			nfs_page_group_lock_wait(head);
4637c3af975SWeston Andros Adamson 			nfs_release_request(head);
4647c3af975SWeston Andros Adamson 			goto try_again;
4657c3af975SWeston Andros Adamson 		}
4667c3af975SWeston Andros Adamson 
46794970014SWeston Andros Adamson 		nfs_release_request(head);
468e7029206SWeston Andros Adamson 		return ERR_PTR(ret);
46994970014SWeston Andros Adamson 	}
4707c3af975SWeston Andros Adamson 
4717c3af975SWeston Andros Adamson 	/* lock each request in the page group */
472d4581383SWeston Andros Adamson 	subreq = head;
473d4581383SWeston Andros Adamson 	do {
474d4581383SWeston Andros Adamson 		/*
475d4581383SWeston Andros Adamson 		 * Subrequests are always contiguous, non overlapping
476d4581383SWeston Andros Adamson 		 * and in order. If not, it's a programming error.
477d4581383SWeston Andros Adamson 		 */
478d4581383SWeston Andros Adamson 		WARN_ON_ONCE(subreq->wb_offset !=
479d4581383SWeston Andros Adamson 		     (head->wb_offset + total_bytes));
480d4581383SWeston Andros Adamson 
481d4581383SWeston Andros Adamson 		/* keep track of how many bytes this group covers */
482d4581383SWeston Andros Adamson 		total_bytes += subreq->wb_bytes;
483d4581383SWeston Andros Adamson 
484d4581383SWeston Andros Adamson 		if (!nfs_lock_request(subreq)) {
485d4581383SWeston Andros Adamson 			/* releases page group bit lock and
486d4581383SWeston Andros Adamson 			 * inode spin lock and all references */
487d4581383SWeston Andros Adamson 			ret = nfs_unroll_locks_and_wait(inode, head,
488d4581383SWeston Andros Adamson 				subreq, nonblock);
489d4581383SWeston Andros Adamson 
490d4581383SWeston Andros Adamson 			if (ret == 0)
491d4581383SWeston Andros Adamson 				goto try_again;
492d4581383SWeston Andros Adamson 
493d4581383SWeston Andros Adamson 			return ERR_PTR(ret);
494d4581383SWeston Andros Adamson 		}
495d4581383SWeston Andros Adamson 
496d4581383SWeston Andros Adamson 		subreq = subreq->wb_this_page;
497d4581383SWeston Andros Adamson 	} while (subreq != head);
498d4581383SWeston Andros Adamson 
499d4581383SWeston Andros Adamson 	/* Now that all requests are locked, make sure they aren't on any list.
500d4581383SWeston Andros Adamson 	 * Commit list removal accounting is done after locks are dropped */
501d4581383SWeston Andros Adamson 	subreq = head;
502d4581383SWeston Andros Adamson 	do {
503411a99adSWeston Andros Adamson 		nfs_clear_request_commit(subreq);
504d4581383SWeston Andros Adamson 		subreq = subreq->wb_this_page;
505d4581383SWeston Andros Adamson 	} while (subreq != head);
506d4581383SWeston Andros Adamson 
507d4581383SWeston Andros Adamson 	/* unlink subrequests from head, destroy them later */
508d4581383SWeston Andros Adamson 	if (head->wb_this_page != head) {
509d4581383SWeston Andros Adamson 		/* destroy list will be terminated by head */
510d4581383SWeston Andros Adamson 		destroy_list = head->wb_this_page;
511d4581383SWeston Andros Adamson 		head->wb_this_page = head;
512d4581383SWeston Andros Adamson 
513d4581383SWeston Andros Adamson 		/* change head request to cover whole range that
514d4581383SWeston Andros Adamson 		 * the former page group covered */
515d4581383SWeston Andros Adamson 		head->wb_bytes = total_bytes;
516d4581383SWeston Andros Adamson 	}
517d4581383SWeston Andros Adamson 
518d4581383SWeston Andros Adamson 	/*
519d4581383SWeston Andros Adamson 	 * prepare head request to be added to new pgio descriptor
520d4581383SWeston Andros Adamson 	 */
521d4581383SWeston Andros Adamson 	nfs_page_group_clear_bits(head);
522d4581383SWeston Andros Adamson 
523d4581383SWeston Andros Adamson 	/*
524d4581383SWeston Andros Adamson 	 * some part of the group was still on the inode list - otherwise
525d4581383SWeston Andros Adamson 	 * the group wouldn't be involved in async write.
526d4581383SWeston Andros Adamson 	 * grab a reference for the head request, iff it needs one.
527d4581383SWeston Andros Adamson 	 */
528d4581383SWeston Andros Adamson 	if (!test_and_set_bit(PG_INODE_REF, &head->wb_flags))
529d4581383SWeston Andros Adamson 		kref_get(&head->wb_kref);
530d4581383SWeston Andros Adamson 
531d4581383SWeston Andros Adamson 	nfs_page_group_unlock(head);
532d4581383SWeston Andros Adamson 
533411a99adSWeston Andros Adamson 	/* drop lock to clean uprequests on destroy list */
534d4581383SWeston Andros Adamson 	spin_unlock(&inode->i_lock);
535d4581383SWeston Andros Adamson 
536d4581383SWeston Andros Adamson 	nfs_destroy_unlinked_subrequests(destroy_list, head);
537d4581383SWeston Andros Adamson 
538d4581383SWeston Andros Adamson 	/* still holds ref on head from nfs_page_find_head_request_locked
539d4581383SWeston Andros Adamson 	 * and still has lock on head from lock loop */
540d4581383SWeston Andros Adamson 	return head;
541612c9384STrond Myklebust }
542074cc1deSTrond Myklebust 
543074cc1deSTrond Myklebust /*
544074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
545074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
546074cc1deSTrond Myklebust  */
547074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
548cfb506e1STrond Myklebust 				struct page *page, bool nonblock)
549074cc1deSTrond Myklebust {
550074cc1deSTrond Myklebust 	struct nfs_page *req;
551074cc1deSTrond Myklebust 	int ret = 0;
552074cc1deSTrond Myklebust 
553d4581383SWeston Andros Adamson 	req = nfs_lock_and_join_requests(page, nonblock);
554074cc1deSTrond Myklebust 	if (!req)
555074cc1deSTrond Myklebust 		goto out;
556074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
557074cc1deSTrond Myklebust 	if (IS_ERR(req))
558074cc1deSTrond Myklebust 		goto out;
559074cc1deSTrond Myklebust 
560deed85e7STrond Myklebust 	nfs_set_page_writeback(page);
561deed85e7STrond Myklebust 	WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
562074cc1deSTrond Myklebust 
563deed85e7STrond Myklebust 	ret = 0;
564f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
565f8512ad0SFred Isaman 		nfs_redirty_request(req);
566074cc1deSTrond Myklebust 		ret = pgio->pg_error;
567f8512ad0SFred Isaman 	}
568074cc1deSTrond Myklebust out:
569074cc1deSTrond Myklebust 	return ret;
570e261f51fSTrond Myklebust }
571e261f51fSTrond Myklebust 
572f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
573f758c885STrond Myklebust {
574d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
575cfb506e1STrond Myklebust 	int ret;
576f758c885STrond Myklebust 
577f758c885STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
578f758c885STrond Myklebust 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
579f758c885STrond Myklebust 
580d56b4ddfSMel Gorman 	nfs_pageio_cond_complete(pgio, page_file_index(page));
5811b430beeSWu Fengguang 	ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
582cfb506e1STrond Myklebust 	if (ret == -EAGAIN) {
583cfb506e1STrond Myklebust 		redirty_page_for_writepage(wbc, page);
584cfb506e1STrond Myklebust 		ret = 0;
585cfb506e1STrond Myklebust 	}
586cfb506e1STrond Myklebust 	return ret;
587f758c885STrond Myklebust }
588f758c885STrond Myklebust 
589e261f51fSTrond Myklebust /*
5901da177e4SLinus Torvalds  * Write an mmapped page to the server.
5911da177e4SLinus Torvalds  */
5924d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
5931da177e4SLinus Torvalds {
594f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
595e261f51fSTrond Myklebust 	int err;
5961da177e4SLinus Torvalds 
597a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc),
598a20c93e3SChristoph Hellwig 				false, &nfs_async_write_completion_ops);
599f758c885STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
600f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
601f758c885STrond Myklebust 	if (err < 0)
6024d770ccfSTrond Myklebust 		return err;
603f758c885STrond Myklebust 	if (pgio.pg_error < 0)
604f758c885STrond Myklebust 		return pgio.pg_error;
605f758c885STrond Myklebust 	return 0;
6064d770ccfSTrond Myklebust }
6074d770ccfSTrond Myklebust 
6084d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
6094d770ccfSTrond Myklebust {
610f758c885STrond Myklebust 	int ret;
6114d770ccfSTrond Myklebust 
612f758c885STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
6131da177e4SLinus Torvalds 	unlock_page(page);
614f758c885STrond Myklebust 	return ret;
615f758c885STrond Myklebust }
616f758c885STrond Myklebust 
617f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
618f758c885STrond Myklebust {
619f758c885STrond Myklebust 	int ret;
620f758c885STrond Myklebust 
621f758c885STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
622f758c885STrond Myklebust 	unlock_page(page);
623f758c885STrond Myklebust 	return ret;
6241da177e4SLinus Torvalds }
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
6271da177e4SLinus Torvalds {
6281da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
62972cb77f4STrond Myklebust 	unsigned long *bitlock = &NFS_I(inode)->flags;
630c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
6311da177e4SLinus Torvalds 	int err;
6321da177e4SLinus Torvalds 
63372cb77f4STrond Myklebust 	/* Stop dirtying of new pages while we sync */
63474316201SNeilBrown 	err = wait_on_bit_lock_action(bitlock, NFS_INO_FLUSHING,
63572cb77f4STrond Myklebust 			nfs_wait_bit_killable, TASK_KILLABLE);
63672cb77f4STrond Myklebust 	if (err)
63772cb77f4STrond Myklebust 		goto out_err;
63872cb77f4STrond Myklebust 
63991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
64091d5b470SChuck Lever 
641a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false,
642a20c93e3SChristoph Hellwig 				&nfs_async_write_completion_ops);
643f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
644c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
64572cb77f4STrond Myklebust 
64672cb77f4STrond Myklebust 	clear_bit_unlock(NFS_INO_FLUSHING, bitlock);
6474e857c58SPeter Zijlstra 	smp_mb__after_atomic();
64872cb77f4STrond Myklebust 	wake_up_bit(bitlock, NFS_INO_FLUSHING);
64972cb77f4STrond Myklebust 
650f758c885STrond Myklebust 	if (err < 0)
65172cb77f4STrond Myklebust 		goto out_err;
65272cb77f4STrond Myklebust 	err = pgio.pg_error;
65372cb77f4STrond Myklebust 	if (err < 0)
65472cb77f4STrond Myklebust 		goto out_err;
655c63c7b05STrond Myklebust 	return 0;
65672cb77f4STrond Myklebust out_err:
65772cb77f4STrond Myklebust 	return err;
6581da177e4SLinus Torvalds }
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds /*
6611da177e4SLinus Torvalds  * Insert a write request into an inode
6621da177e4SLinus Torvalds  */
663d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
6641da177e4SLinus Torvalds {
6651da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
666e7d39069STrond Myklebust 
6672bfc6e56SWeston Andros Adamson 	WARN_ON_ONCE(req->wb_this_page != req);
6682bfc6e56SWeston Andros Adamson 
669e7d39069STrond Myklebust 	/* Lock the request! */
6707ad84aa9STrond Myklebust 	nfs_lock_request(req);
671e7d39069STrond Myklebust 
672e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
673011e2a7fSBryan Schumaker 	if (!nfsi->npages && NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
674a9a4a87aSTrond Myklebust 		inode->i_version++;
67529418aa4SMel Gorman 	/*
67629418aa4SMel Gorman 	 * Swap-space should not get truncated. Hence no need to plug the race
67729418aa4SMel Gorman 	 * with invalidate/truncate.
67829418aa4SMel Gorman 	 */
67929418aa4SMel Gorman 	if (likely(!PageSwapCache(req->wb_page))) {
6802df485a7STrond Myklebust 		set_bit(PG_MAPPED, &req->wb_flags);
681deb7d638STrond Myklebust 		SetPagePrivate(req->wb_page);
682277459d2STrond Myklebust 		set_page_private(req->wb_page, (unsigned long)req);
68329418aa4SMel Gorman 	}
6841da177e4SLinus Torvalds 	nfsi->npages++;
68517089a29SWeston Andros Adamson 	/* this a head request for a page group - mark it as having an
68617089a29SWeston Andros Adamson 	 * extra reference so sub groups can follow suit */
68717089a29SWeston Andros Adamson 	WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags));
688c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
689e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
6901da177e4SLinus Torvalds }
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds /*
69389a09141SPeter Zijlstra  * Remove a write request from an inode
6941da177e4SLinus Torvalds  */
6951da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
6961da177e4SLinus Torvalds {
6973d4ff43dSAl Viro 	struct inode *inode = req->wb_context->dentry->d_inode;
6981da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
69920633f04SWeston Andros Adamson 	struct nfs_page *head;
70020633f04SWeston Andros Adamson 
70120633f04SWeston Andros Adamson 	if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) {
70220633f04SWeston Andros Adamson 		head = req->wb_head;
7031da177e4SLinus Torvalds 
704587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
70520633f04SWeston Andros Adamson 		if (likely(!PageSwapCache(head->wb_page))) {
70620633f04SWeston Andros Adamson 			set_page_private(head->wb_page, 0);
70720633f04SWeston Andros Adamson 			ClearPagePrivate(head->wb_page);
70895905446SNeilBrown 			smp_mb__after_atomic();
70995905446SNeilBrown 			wake_up_page(head->wb_page, PG_private);
71020633f04SWeston Andros Adamson 			clear_bit(PG_MAPPED, &head->wb_flags);
71129418aa4SMel Gorman 		}
7121da177e4SLinus Torvalds 		nfsi->npages--;
713587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
71420633f04SWeston Andros Adamson 	}
71517089a29SWeston Andros Adamson 
71617089a29SWeston Andros Adamson 	if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags))
7171da177e4SLinus Torvalds 		nfs_release_request(req);
718b412ddf0SWeston Andros Adamson 	else
719b412ddf0SWeston Andros Adamson 		WARN_ON_ONCE(1);
7201da177e4SLinus Torvalds }
7211da177e4SLinus Torvalds 
72261822ab5STrond Myklebust static void
7236d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
72461822ab5STrond Myklebust {
72561822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
72661822ab5STrond Myklebust }
72761822ab5STrond Myklebust 
7283a3908c8STrond Myklebust /*
7293a3908c8STrond Myklebust  * nfs_page_search_commits_for_head_request_locked
7303a3908c8STrond Myklebust  *
7313a3908c8STrond Myklebust  * Search through commit lists on @inode for the head request for @page.
7323a3908c8STrond Myklebust  * Must be called while holding the inode (which is cinfo) lock.
7333a3908c8STrond Myklebust  *
7343a3908c8STrond Myklebust  * Returns the head request if found, or NULL if not found.
7353a3908c8STrond Myklebust  */
7363a3908c8STrond Myklebust static struct nfs_page *
7373a3908c8STrond Myklebust nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
7383a3908c8STrond Myklebust 						struct page *page)
7393a3908c8STrond Myklebust {
7403a3908c8STrond Myklebust 	struct nfs_page *freq, *t;
7413a3908c8STrond Myklebust 	struct nfs_commit_info cinfo;
7423a3908c8STrond Myklebust 	struct inode *inode = &nfsi->vfs_inode;
7433a3908c8STrond Myklebust 
7443a3908c8STrond Myklebust 	nfs_init_cinfo_from_inode(&cinfo, inode);
7453a3908c8STrond Myklebust 
7463a3908c8STrond Myklebust 	/* search through pnfs commit lists */
7473a3908c8STrond Myklebust 	freq = pnfs_search_commit_reqs(inode, &cinfo, page);
7483a3908c8STrond Myklebust 	if (freq)
7493a3908c8STrond Myklebust 		return freq->wb_head;
7503a3908c8STrond Myklebust 
7513a3908c8STrond Myklebust 	/* Linearly search the commit list for the correct request */
7523a3908c8STrond Myklebust 	list_for_each_entry_safe(freq, t, &cinfo.mds->list, wb_list) {
7533a3908c8STrond Myklebust 		if (freq->wb_page == page)
7543a3908c8STrond Myklebust 			return freq->wb_head;
7553a3908c8STrond Myklebust 	}
7563a3908c8STrond Myklebust 
7573a3908c8STrond Myklebust 	return NULL;
7583a3908c8STrond Myklebust }
7593a3908c8STrond Myklebust 
7608dd37758STrond Myklebust /**
7618dd37758STrond Myklebust  * nfs_request_add_commit_list - add request to a commit list
7628dd37758STrond Myklebust  * @req: pointer to a struct nfs_page
763ea2cf228SFred Isaman  * @dst: commit list head
764ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
7658dd37758STrond Myklebust  *
766ea2cf228SFred Isaman  * This sets the PG_CLEAN bit, updates the cinfo count of
7678dd37758STrond Myklebust  * number of outstanding requests requiring a commit as well as
7688dd37758STrond Myklebust  * the MM page stats.
7698dd37758STrond Myklebust  *
770ea2cf228SFred Isaman  * The caller must _not_ hold the cinfo->lock, but must be
7718dd37758STrond Myklebust  * holding the nfs_page lock.
7728dd37758STrond Myklebust  */
7738dd37758STrond Myklebust void
774ea2cf228SFred Isaman nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst,
775ea2cf228SFred Isaman 			    struct nfs_commit_info *cinfo)
7768dd37758STrond Myklebust {
7778dd37758STrond Myklebust 	set_bit(PG_CLEAN, &(req)->wb_flags);
778ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
779ea2cf228SFred Isaman 	nfs_list_add_request(req, dst);
780ea2cf228SFred Isaman 	cinfo->mds->ncommit++;
781ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
78256f9cd68SFred Isaman 	if (!cinfo->dreq) {
7838dd37758STrond Myklebust 		inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
784d56b4ddfSMel Gorman 		inc_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
78556f9cd68SFred Isaman 			     BDI_RECLAIMABLE);
78656f9cd68SFred Isaman 		__mark_inode_dirty(req->wb_context->dentry->d_inode,
78756f9cd68SFred Isaman 				   I_DIRTY_DATASYNC);
78856f9cd68SFred Isaman 	}
7898dd37758STrond Myklebust }
7908dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
7918dd37758STrond Myklebust 
7928dd37758STrond Myklebust /**
7938dd37758STrond Myklebust  * nfs_request_remove_commit_list - Remove request from a commit list
7948dd37758STrond Myklebust  * @req: pointer to a nfs_page
795ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
7968dd37758STrond Myklebust  *
797ea2cf228SFred Isaman  * This clears the PG_CLEAN bit, and updates the cinfo's count of
7988dd37758STrond Myklebust  * number of outstanding requests requiring a commit
7998dd37758STrond Myklebust  * It does not update the MM page stats.
8008dd37758STrond Myklebust  *
801ea2cf228SFred Isaman  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
8028dd37758STrond Myklebust  */
8038dd37758STrond Myklebust void
804ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req,
805ea2cf228SFred Isaman 			       struct nfs_commit_info *cinfo)
8068dd37758STrond Myklebust {
8078dd37758STrond Myklebust 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
8088dd37758STrond Myklebust 		return;
8098dd37758STrond Myklebust 	nfs_list_remove_request(req);
810ea2cf228SFred Isaman 	cinfo->mds->ncommit--;
8118dd37758STrond Myklebust }
8128dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
8138dd37758STrond Myklebust 
814ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
815ea2cf228SFred Isaman 				      struct inode *inode)
816ea2cf228SFred Isaman {
817ea2cf228SFred Isaman 	cinfo->lock = &inode->i_lock;
818ea2cf228SFred Isaman 	cinfo->mds = &NFS_I(inode)->commit_info;
819ea2cf228SFred Isaman 	cinfo->ds = pnfs_get_ds_info(inode);
820b359f9d0SFred Isaman 	cinfo->dreq = NULL;
821f453a54aSFred Isaman 	cinfo->completion_ops = &nfs_commit_completion_ops;
822ea2cf228SFred Isaman }
823ea2cf228SFred Isaman 
824ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
825ea2cf228SFred Isaman 		    struct inode *inode,
826ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq)
827ea2cf228SFred Isaman {
8281763da12SFred Isaman 	if (dreq)
8291763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
8301763da12SFred Isaman 	else
831ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(cinfo, inode);
832ea2cf228SFred Isaman }
833ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo);
8348dd37758STrond Myklebust 
8351da177e4SLinus Torvalds /*
8361da177e4SLinus Torvalds  * Add a request to the inode's commit list.
8371da177e4SLinus Torvalds  */
8381763da12SFred Isaman void
839ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
840ea2cf228SFred Isaman 			struct nfs_commit_info *cinfo)
8411da177e4SLinus Torvalds {
842ea2cf228SFred Isaman 	if (pnfs_mark_request_commit(req, lseg, cinfo))
8438dd37758STrond Myklebust 		return;
844ea2cf228SFred Isaman 	nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo);
8451da177e4SLinus Torvalds }
8468e821cadSTrond Myklebust 
847d6d6dc7cSFred Isaman static void
848d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page)
849e468bae9STrond Myklebust {
850e468bae9STrond Myklebust 	dec_zone_page_state(page, NR_UNSTABLE_NFS);
851d56b4ddfSMel Gorman 	dec_bdi_stat(page_file_mapping(page)->backing_dev_info, BDI_RECLAIMABLE);
852e468bae9STrond Myklebust }
853d6d6dc7cSFred Isaman 
854411a99adSWeston Andros Adamson /* Called holding inode (/cinfo) lock */
8558dd37758STrond Myklebust static void
856d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req)
857d6d6dc7cSFred Isaman {
8588dd37758STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
8598dd37758STrond Myklebust 		struct inode *inode = req->wb_context->dentry->d_inode;
860ea2cf228SFred Isaman 		struct nfs_commit_info cinfo;
861d6d6dc7cSFred Isaman 
862ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(&cinfo, inode);
863ea2cf228SFred Isaman 		if (!pnfs_clear_request_commit(req, &cinfo)) {
864ea2cf228SFred Isaman 			nfs_request_remove_commit_list(req, &cinfo);
865d6d6dc7cSFred Isaman 		}
8668dd37758STrond Myklebust 		nfs_clear_page_commit(req->wb_page);
8678dd37758STrond Myklebust 	}
868e468bae9STrond Myklebust }
869e468bae9STrond Myklebust 
870d45f60c6SWeston Andros Adamson int nfs_write_need_commit(struct nfs_pgio_header *hdr)
8718e821cadSTrond Myklebust {
872c65e6254SWeston Andros Adamson 	if (hdr->verf.committed == NFS_DATA_SYNC)
873d45f60c6SWeston Andros Adamson 		return hdr->lseg == NULL;
874c65e6254SWeston Andros Adamson 	return hdr->verf.committed != NFS_FILE_SYNC;
8758e821cadSTrond Myklebust }
8768e821cadSTrond Myklebust 
877061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
8786c75dc0dSFred Isaman {
879ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
8806c75dc0dSFred Isaman 	unsigned long bytes = 0;
8816c75dc0dSFred Isaman 
8826c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
8836c75dc0dSFred Isaman 		goto out;
884ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
8856c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
8866c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
8876c75dc0dSFred Isaman 
8886c75dc0dSFred Isaman 		bytes += req->wb_bytes;
8896c75dc0dSFred Isaman 		nfs_list_remove_request(req);
8906c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
8916c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
892d1182b33STrond Myklebust 			nfs_set_pageerror(req->wb_page);
8936c75dc0dSFred Isaman 			nfs_context_set_write_error(req->wb_context, hdr->error);
8946c75dc0dSFred Isaman 			goto remove_req;
8956c75dc0dSFred Isaman 		}
896c65e6254SWeston Andros Adamson 		if (nfs_write_need_commit(hdr)) {
897f79d06f5SAnna Schumaker 			memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
898ea2cf228SFred Isaman 			nfs_mark_request_commit(req, hdr->lseg, &cinfo);
8996c75dc0dSFred Isaman 			goto next;
9006c75dc0dSFred Isaman 		}
9016c75dc0dSFred Isaman remove_req:
9026c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
9036c75dc0dSFred Isaman next:
9041d1afcbcSTrond Myklebust 		nfs_unlock_request(req);
90520633f04SWeston Andros Adamson 		nfs_end_page_writeback(req);
9063aff4ebbSTrond Myklebust 		nfs_release_request(req);
9076c75dc0dSFred Isaman 	}
9086c75dc0dSFred Isaman out:
9096c75dc0dSFred Isaman 	hdr->release(hdr);
9106c75dc0dSFred Isaman }
9116c75dc0dSFred Isaman 
912ce59515cSAnna Schumaker unsigned long
913ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
914fb8a1f11STrond Myklebust {
915ea2cf228SFred Isaman 	return cinfo->mds->ncommit;
916fb8a1f11STrond Myklebust }
917fb8a1f11STrond Myklebust 
918ea2cf228SFred Isaman /* cinfo->lock held by caller */
9191763da12SFred Isaman int
920ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
921ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
922d6d6dc7cSFred Isaman {
923d6d6dc7cSFred Isaman 	struct nfs_page *req, *tmp;
924d6d6dc7cSFred Isaman 	int ret = 0;
925d6d6dc7cSFred Isaman 
926d6d6dc7cSFred Isaman 	list_for_each_entry_safe(req, tmp, src, wb_list) {
9278dd37758STrond Myklebust 		if (!nfs_lock_request(req))
9288dd37758STrond Myklebust 			continue;
9297ad84aa9STrond Myklebust 		kref_get(&req->wb_kref);
930ea2cf228SFred Isaman 		if (cond_resched_lock(cinfo->lock))
9313b3be88dSTrond Myklebust 			list_safe_reset_next(req, tmp, wb_list);
932ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
9338dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
934d6d6dc7cSFred Isaman 		ret++;
9351763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
936d6d6dc7cSFred Isaman 			break;
937d6d6dc7cSFred Isaman 	}
938d6d6dc7cSFred Isaman 	return ret;
939d6d6dc7cSFred Isaman }
940d6d6dc7cSFred Isaman 
9411da177e4SLinus Torvalds /*
9421da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
9431da177e4SLinus Torvalds  * @inode: NFS inode to scan
944ea2cf228SFred Isaman  * @dst: mds destination list
945ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
9461da177e4SLinus Torvalds  *
9471da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
9481da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
9491da177e4SLinus Torvalds  */
9501763da12SFred Isaman int
951ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
952ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
9531da177e4SLinus Torvalds {
954d6d6dc7cSFred Isaman 	int ret = 0;
955fb8a1f11STrond Myklebust 
956ea2cf228SFred Isaman 	spin_lock(cinfo->lock);
957ea2cf228SFred Isaman 	if (cinfo->mds->ncommit > 0) {
9588dd37758STrond Myklebust 		const int max = INT_MAX;
959d6d6dc7cSFred Isaman 
960ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
961ea2cf228SFred Isaman 					   cinfo, max);
962ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
963d6d6dc7cSFred Isaman 	}
964ea2cf228SFred Isaman 	spin_unlock(cinfo->lock);
965ff778d02STrond Myklebust 	return ret;
9661da177e4SLinus Torvalds }
967d6d6dc7cSFred Isaman 
9681da177e4SLinus Torvalds /*
969e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
970e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
9711da177e4SLinus Torvalds  *
972e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
973e7d39069STrond Myklebust  * to disk.
9741da177e4SLinus Torvalds  */
975e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
976e7d39069STrond Myklebust 		struct page *page,
977e7d39069STrond Myklebust 		unsigned int offset,
978e7d39069STrond Myklebust 		unsigned int bytes)
9791da177e4SLinus Torvalds {
980e7d39069STrond Myklebust 	struct nfs_page *req;
981e7d39069STrond Myklebust 	unsigned int rqend;
982e7d39069STrond Myklebust 	unsigned int end;
9831da177e4SLinus Torvalds 	int error;
984277459d2STrond Myklebust 
985e7d39069STrond Myklebust 	if (!PagePrivate(page))
986e7d39069STrond Myklebust 		return NULL;
987e7d39069STrond Myklebust 
988e7d39069STrond Myklebust 	end = offset + bytes;
989e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
990e7d39069STrond Myklebust 
991e7d39069STrond Myklebust 	for (;;) {
99284d3a9a9SWeston Andros Adamson 		req = nfs_page_find_head_request_locked(NFS_I(inode), page);
993e7d39069STrond Myklebust 		if (req == NULL)
994e7d39069STrond Myklebust 			goto out_unlock;
995e7d39069STrond Myklebust 
9962bfc6e56SWeston Andros Adamson 		/* should be handled by nfs_flush_incompatible */
9972bfc6e56SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_head != req);
9982bfc6e56SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_this_page != req);
9992bfc6e56SWeston Andros Adamson 
1000e7d39069STrond Myklebust 		rqend = req->wb_offset + req->wb_bytes;
1001e7d39069STrond Myklebust 		/*
1002e7d39069STrond Myklebust 		 * Tell the caller to flush out the request if
1003e7d39069STrond Myklebust 		 * the offsets are non-contiguous.
1004e7d39069STrond Myklebust 		 * Note: nfs_flush_incompatible() will already
1005e7d39069STrond Myklebust 		 * have flushed out requests having wrong owners.
1006e7d39069STrond Myklebust 		 */
1007e468bae9STrond Myklebust 		if (offset > rqend
1008e7d39069STrond Myklebust 		    || end < req->wb_offset)
1009e7d39069STrond Myklebust 			goto out_flushme;
1010e7d39069STrond Myklebust 
10117ad84aa9STrond Myklebust 		if (nfs_lock_request(req))
1012e7d39069STrond Myklebust 			break;
1013e7d39069STrond Myklebust 
1014e7d39069STrond Myklebust 		/* The request is locked, so wait and then retry */
1015587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
10161da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
10171da177e4SLinus Torvalds 		nfs_release_request(req);
1018e7d39069STrond Myklebust 		if (error != 0)
1019e7d39069STrond Myklebust 			goto out_err;
1020e7d39069STrond Myklebust 		spin_lock(&inode->i_lock);
10211da177e4SLinus Torvalds 	}
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
10241da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
10251da177e4SLinus Torvalds 		req->wb_offset = offset;
10261da177e4SLinus Torvalds 		req->wb_pgbase = offset;
10271da177e4SLinus Torvalds 	}
10281da177e4SLinus Torvalds 	if (end > rqend)
10291da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
1030e7d39069STrond Myklebust 	else
1031e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
1032e7d39069STrond Myklebust out_unlock:
1033ca138f36SFred Isaman 	if (req)
10348dd37758STrond Myklebust 		nfs_clear_request_commit(req);
1035411a99adSWeston Andros Adamson 	spin_unlock(&inode->i_lock);
1036e7d39069STrond Myklebust 	return req;
1037e7d39069STrond Myklebust out_flushme:
1038e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
1039e7d39069STrond Myklebust 	nfs_release_request(req);
1040e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
1041e7d39069STrond Myklebust out_err:
1042e7d39069STrond Myklebust 	return ERR_PTR(error);
1043e7d39069STrond Myklebust }
10441da177e4SLinus Torvalds 
1045e7d39069STrond Myklebust /*
1046e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
1047e7d39069STrond Myklebust  *
1048e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
1049e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
1050e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
1051e7d39069STrond Myklebust  */
1052e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
1053e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
1054e7d39069STrond Myklebust {
1055d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
1056e7d39069STrond Myklebust 	struct nfs_page	*req;
1057e7d39069STrond Myklebust 
1058e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
1059e7d39069STrond Myklebust 	if (req != NULL)
1060e7d39069STrond Myklebust 		goto out;
10612bfc6e56SWeston Andros Adamson 	req = nfs_create_request(ctx, page, NULL, offset, bytes);
1062e7d39069STrond Myklebust 	if (IS_ERR(req))
1063e7d39069STrond Myklebust 		goto out;
1064d6d6dc7cSFred Isaman 	nfs_inode_add_request(inode, req);
1065efc91ed0STrond Myklebust out:
106661e930a9STrond Myklebust 	return req;
10671da177e4SLinus Torvalds }
10681da177e4SLinus Torvalds 
1069e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
1070e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
1071e7d39069STrond Myklebust {
1072e7d39069STrond Myklebust 	struct nfs_page	*req;
1073e7d39069STrond Myklebust 
1074e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
1075e7d39069STrond Myklebust 	if (IS_ERR(req))
1076e7d39069STrond Myklebust 		return PTR_ERR(req);
1077e7d39069STrond Myklebust 	/* Update file length */
1078e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
1079d72ddcbaSWeston Andros Adamson 	nfs_mark_uptodate(req);
1080a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
10811d1afcbcSTrond Myklebust 	nfs_unlock_and_release_request(req);
1082e7d39069STrond Myklebust 	return 0;
1083e7d39069STrond Myklebust }
1084e7d39069STrond Myklebust 
10851da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
10861da177e4SLinus Torvalds {
1087cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
10882a369153STrond Myklebust 	struct nfs_lock_context *l_ctx;
10891da177e4SLinus Torvalds 	struct nfs_page	*req;
10901a54533eSTrond Myklebust 	int do_flush, status;
10911da177e4SLinus Torvalds 	/*
10921da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
10931da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
10941da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
10951da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
10961da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
10971da177e4SLinus Torvalds 	 * dropped page.
10981da177e4SLinus Torvalds 	 */
10991a54533eSTrond Myklebust 	do {
110084d3a9a9SWeston Andros Adamson 		req = nfs_page_find_head_request(page);
11011a54533eSTrond Myklebust 		if (req == NULL)
11021a54533eSTrond Myklebust 			return 0;
11032a369153STrond Myklebust 		l_ctx = req->wb_lock_context;
11042a369153STrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx;
11052bfc6e56SWeston Andros Adamson 		/* for now, flush if more than 1 request in page_group */
11062bfc6e56SWeston Andros Adamson 		do_flush |= req->wb_this_page != req;
11070f1d2605STrond Myklebust 		if (l_ctx && ctx->dentry->d_inode->i_flock != NULL) {
11082a369153STrond Myklebust 			do_flush |= l_ctx->lockowner.l_owner != current->files
11092a369153STrond Myklebust 				|| l_ctx->lockowner.l_pid != current->tgid;
11102a369153STrond Myklebust 		}
11111da177e4SLinus Torvalds 		nfs_release_request(req);
11121a54533eSTrond Myklebust 		if (!do_flush)
11131a54533eSTrond Myklebust 			return 0;
1114d56b4ddfSMel Gorman 		status = nfs_wb_page(page_file_mapping(page)->host, page);
11151a54533eSTrond Myklebust 	} while (status == 0);
11161a54533eSTrond Myklebust 	return status;
11171da177e4SLinus Torvalds }
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds /*
1120dc24826bSAndy Adamson  * Avoid buffered writes when a open context credential's key would
1121dc24826bSAndy Adamson  * expire soon.
1122dc24826bSAndy Adamson  *
1123dc24826bSAndy Adamson  * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
1124dc24826bSAndy Adamson  *
1125dc24826bSAndy Adamson  * Return 0 and set a credential flag which triggers the inode to flush
1126dc24826bSAndy Adamson  * and performs  NFS_FILE_SYNC writes if the key will expired within
1127dc24826bSAndy Adamson  * RPC_KEY_EXPIRE_TIMEO.
1128dc24826bSAndy Adamson  */
1129dc24826bSAndy Adamson int
1130dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode)
1131dc24826bSAndy Adamson {
1132dc24826bSAndy Adamson 	struct nfs_open_context *ctx = nfs_file_open_context(filp);
1133dc24826bSAndy Adamson 	struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
1134dc24826bSAndy Adamson 
1135dc24826bSAndy Adamson 	return rpcauth_key_timeout_notify(auth, ctx->cred);
1136dc24826bSAndy Adamson }
1137dc24826bSAndy Adamson 
1138dc24826bSAndy Adamson /*
1139dc24826bSAndy Adamson  * Test if the open context credential key is marked to expire soon.
1140dc24826bSAndy Adamson  */
1141dc24826bSAndy Adamson bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx)
1142dc24826bSAndy Adamson {
1143dc24826bSAndy Adamson 	return rpcauth_cred_key_to_expire(ctx->cred);
1144dc24826bSAndy Adamson }
1145dc24826bSAndy Adamson 
1146dc24826bSAndy Adamson /*
11475d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
11485d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
11495d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
11505d47a356STrond Myklebust  */
11518d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
11525d47a356STrond Myklebust {
1153d529ef83SJeff Layton 	struct nfs_inode *nfsi = NFS_I(inode);
1154d529ef83SJeff Layton 
11558d197a56STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
11568d197a56STrond Myklebust 		goto out;
115718dd78c4SScott Mayhew 	if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
1158d529ef83SJeff Layton 		return false;
11594db72b40SJeff Layton 	smp_rmb();
1160d529ef83SJeff Layton 	if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags))
11618d197a56STrond Myklebust 		return false;
11628d197a56STrond Myklebust out:
116318dd78c4SScott Mayhew 	if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
116418dd78c4SScott Mayhew 		return false;
11658d197a56STrond Myklebust 	return PageUptodate(page) != 0;
11665d47a356STrond Myklebust }
11675d47a356STrond Myklebust 
1168c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or
1169c7559663SScott Mayhew  * if we have the whole file locked for writing), it may be more efficient to
1170c7559663SScott Mayhew  * extend the write to cover the entire page in order to avoid fragmentation
1171c7559663SScott Mayhew  * inefficiencies.
1172c7559663SScott Mayhew  *
1173263b4509SScott Mayhew  * If the file is opened for synchronous writes then we can just skip the rest
1174263b4509SScott Mayhew  * of the checks.
1175c7559663SScott Mayhew  */
1176c7559663SScott Mayhew static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode)
1177c7559663SScott Mayhew {
1178c7559663SScott Mayhew 	if (file->f_flags & O_DSYNC)
1179c7559663SScott Mayhew 		return 0;
1180263b4509SScott Mayhew 	if (!nfs_write_pageuptodate(page, inode))
1181263b4509SScott Mayhew 		return 0;
1182c7559663SScott Mayhew 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
1183c7559663SScott Mayhew 		return 1;
1184263b4509SScott Mayhew 	if (inode->i_flock == NULL || (inode->i_flock->fl_start == 0 &&
1185c7559663SScott Mayhew 			inode->i_flock->fl_end == OFFSET_MAX &&
1186263b4509SScott Mayhew 			inode->i_flock->fl_type != F_RDLCK))
1187c7559663SScott Mayhew 		return 1;
1188c7559663SScott Mayhew 	return 0;
1189c7559663SScott Mayhew }
1190c7559663SScott Mayhew 
11915d47a356STrond Myklebust /*
11921da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
11931da177e4SLinus Torvalds  *
11941da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
11951da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
11961da177e4SLinus Torvalds  */
11971da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
11981da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
11991da177e4SLinus Torvalds {
1200cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
1201d56b4ddfSMel Gorman 	struct inode	*inode = page_file_mapping(page)->host;
12021da177e4SLinus Torvalds 	int		status = 0;
12031da177e4SLinus Torvalds 
120491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
120591d5b470SChuck Lever 
12066de1472fSAl Viro 	dprintk("NFS:       nfs_updatepage(%pD2 %d@%lld)\n",
12076de1472fSAl Viro 		file, count, (long long)(page_file_offset(page) + offset));
12081da177e4SLinus Torvalds 
1209c7559663SScott Mayhew 	if (nfs_can_extend_write(file, page, inode)) {
121049a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
12111da177e4SLinus Torvalds 		offset = 0;
12121da177e4SLinus Torvalds 	}
12131da177e4SLinus Torvalds 
1214e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
121503fa9e84STrond Myklebust 	if (status < 0)
121603fa9e84STrond Myklebust 		nfs_set_pageerror(page);
121759b7c05fSTrond Myklebust 	else
121859b7c05fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
12191da177e4SLinus Torvalds 
122048186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
12211da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
12221da177e4SLinus Torvalds 	return status;
12231da177e4SLinus Torvalds }
12241da177e4SLinus Torvalds 
12253ff7576dSTrond Myklebust static int flush_task_priority(int how)
12261da177e4SLinus Torvalds {
12271da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
12281da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
12291da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
12301da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
12311da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
12321da177e4SLinus Torvalds 	}
12331da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
12341da177e4SLinus Torvalds }
12351da177e4SLinus Torvalds 
1236d45f60c6SWeston Andros Adamson static void nfs_initiate_write(struct nfs_pgio_header *hdr,
1237d45f60c6SWeston Andros Adamson 			       struct rpc_message *msg,
12381ed26f33SAnna Schumaker 			       struct rpc_task_setup *task_setup_data, int how)
12391da177e4SLinus Torvalds {
1240d45f60c6SWeston Andros Adamson 	struct inode *inode = hdr->inode;
12413ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
12421da177e4SLinus Torvalds 
12431ed26f33SAnna Schumaker 	task_setup_data->priority = priority;
1244d45f60c6SWeston Andros Adamson 	NFS_PROTO(inode)->write_setup(hdr, msg);
1245d138d5d1SAndy Adamson 
12468c21c62cSWeston Andros Adamson 	nfs4_state_protect_write(NFS_SERVER(inode)->nfs_client,
1247d45f60c6SWeston Andros Adamson 				 &task_setup_data->rpc_client, msg, hdr);
1248275acaafSTrond Myklebust }
1249275acaafSTrond Myklebust 
12506d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
12516d884e8fSFred  * call this on each, which will prepare them to be retried on next
12526d884e8fSFred  * writeback using standard nfs.
12536d884e8fSFred  */
12546d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
12556d884e8fSFred {
12566d884e8fSFred 	nfs_mark_request_dirty(req);
12571d1afcbcSTrond Myklebust 	nfs_unlock_request(req);
125820633f04SWeston Andros Adamson 	nfs_end_page_writeback(req);
12593aff4ebbSTrond Myklebust 	nfs_release_request(req);
12606d884e8fSFred }
12616d884e8fSFred 
1262061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head)
12636c75dc0dSFred Isaman {
12646c75dc0dSFred Isaman 	struct nfs_page	*req;
12656c75dc0dSFred Isaman 
12666c75dc0dSFred Isaman 	while (!list_empty(head)) {
12676c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
12686c75dc0dSFred Isaman 		nfs_list_remove_request(req);
12696c75dc0dSFred Isaman 		nfs_redirty_request(req);
12706c75dc0dSFred Isaman 	}
12716c75dc0dSFred Isaman }
12726c75dc0dSFred Isaman 
1273061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1274061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1275061ae2edSFred Isaman 	.completion = nfs_write_completion,
1276061ae2edSFred Isaman };
1277061ae2edSFred Isaman 
127857208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1279a20c93e3SChristoph Hellwig 			       struct inode *inode, int ioflags, bool force_mds,
1280061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
12811751c363STrond Myklebust {
1282a20c93e3SChristoph Hellwig 	struct nfs_server *server = NFS_SERVER(inode);
128341d8d5b7SAnna Schumaker 	const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
1284a20c93e3SChristoph Hellwig 
1285a20c93e3SChristoph Hellwig #ifdef CONFIG_NFS_V4_1
1286a20c93e3SChristoph Hellwig 	if (server->pnfs_curr_ld && !force_mds)
1287a20c93e3SChristoph Hellwig 		pg_ops = server->pnfs_curr_ld->pg_write_ops;
1288a20c93e3SChristoph Hellwig #endif
12894a0de55cSAnna Schumaker 	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
12904a0de55cSAnna Schumaker 			server->wsize, ioflags);
12911751c363STrond Myklebust }
1292ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
12931751c363STrond Myklebust 
1294dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1295dce81290STrond Myklebust {
129641d8d5b7SAnna Schumaker 	pgio->pg_ops = &nfs_pgio_rw_ops;
1297dce81290STrond Myklebust 	pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1298dce81290STrond Myklebust }
12991f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1300dce81290STrond Myklebust 
13011da177e4SLinus Torvalds 
13020b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
13030b7c0153SFred Isaman {
13040b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
13050b7c0153SFred Isaman 
13060b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
13070b7c0153SFred Isaman }
13080b7c0153SFred Isaman 
1309d45f60c6SWeston Andros Adamson static void nfs_writeback_release_common(struct nfs_pgio_header *hdr)
13101da177e4SLinus Torvalds {
1311c65e6254SWeston Andros Adamson 	/* do nothing! */
13121da177e4SLinus Torvalds }
13131da177e4SLinus Torvalds 
13141f2edbe3STrond Myklebust /*
13151f2edbe3STrond Myklebust  * Special version of should_remove_suid() that ignores capabilities.
13161f2edbe3STrond Myklebust  */
13171f2edbe3STrond Myklebust static int nfs_should_remove_suid(const struct inode *inode)
13181f2edbe3STrond Myklebust {
13191f2edbe3STrond Myklebust 	umode_t mode = inode->i_mode;
13201f2edbe3STrond Myklebust 	int kill = 0;
1321788e7a89STrond Myklebust 
13221f2edbe3STrond Myklebust 	/* suid always must be killed */
13231f2edbe3STrond Myklebust 	if (unlikely(mode & S_ISUID))
13241f2edbe3STrond Myklebust 		kill = ATTR_KILL_SUID;
13251f2edbe3STrond Myklebust 
13261f2edbe3STrond Myklebust 	/*
13271f2edbe3STrond Myklebust 	 * sgid without any exec bits is just a mandatory locking mark; leave
13281f2edbe3STrond Myklebust 	 * it alone.  If some exec bits are set, it's a real sgid; kill it.
13291f2edbe3STrond Myklebust 	 */
13301f2edbe3STrond Myklebust 	if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
13311f2edbe3STrond Myklebust 		kill |= ATTR_KILL_SGID;
13321f2edbe3STrond Myklebust 
13331f2edbe3STrond Myklebust 	if (unlikely(kill && S_ISREG(mode)))
13341f2edbe3STrond Myklebust 		return kill;
13351f2edbe3STrond Myklebust 
13361f2edbe3STrond Myklebust 	return 0;
13371f2edbe3STrond Myklebust }
1338788e7a89STrond Myklebust 
13391da177e4SLinus Torvalds /*
13401da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
13411da177e4SLinus Torvalds  */
1342d45f60c6SWeston Andros Adamson static int nfs_writeback_done(struct rpc_task *task,
1343d45f60c6SWeston Andros Adamson 			      struct nfs_pgio_header *hdr,
13440eecb214SAnna Schumaker 			      struct inode *inode)
13451da177e4SLinus Torvalds {
1346788e7a89STrond Myklebust 	int status;
13471da177e4SLinus Torvalds 
1348f551e44fSChuck Lever 	/*
1349f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1350f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1351f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1352f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1353f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1354f551e44fSChuck Lever 	 */
1355d45f60c6SWeston Andros Adamson 	status = NFS_PROTO(inode)->write_done(task, hdr);
1356788e7a89STrond Myklebust 	if (status != 0)
13570eecb214SAnna Schumaker 		return status;
1358d45f60c6SWeston Andros Adamson 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count);
135991d5b470SChuck Lever 
1360d45f60c6SWeston Andros Adamson 	if (hdr->res.verf->committed < hdr->args.stable &&
1361d45f60c6SWeston Andros Adamson 	    task->tk_status >= 0) {
13621da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
13631da177e4SLinus Torvalds 		 * commit data to stable storage even though we
13641da177e4SLinus Torvalds 		 * requested it.
13651da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
13661da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
13671da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
13681da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
13691da177e4SLinus Torvalds 		 */
13701da177e4SLinus Torvalds 		static unsigned long    complain;
13711da177e4SLinus Torvalds 
1372a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
13731da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
13741da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
13751da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1376cd841605SFred Isaman 				NFS_SERVER(inode)->nfs_client->cl_hostname,
1377d45f60c6SWeston Andros Adamson 				hdr->res.verf->committed, hdr->args.stable);
13781da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
13791da177e4SLinus Torvalds 		}
13801da177e4SLinus Torvalds 	}
13811f2edbe3STrond Myklebust 
13821f2edbe3STrond Myklebust 	/* Deal with the suid/sgid bit corner case */
13831f2edbe3STrond Myklebust 	if (nfs_should_remove_suid(inode))
13841f2edbe3STrond Myklebust 		nfs_mark_for_revalidate(inode);
13850eecb214SAnna Schumaker 	return 0;
13860eecb214SAnna Schumaker }
13870eecb214SAnna Schumaker 
13880eecb214SAnna Schumaker /*
13890eecb214SAnna Schumaker  * This function is called when the WRITE call is complete.
13900eecb214SAnna Schumaker  */
1391d45f60c6SWeston Andros Adamson static void nfs_writeback_result(struct rpc_task *task,
1392d45f60c6SWeston Andros Adamson 				 struct nfs_pgio_header *hdr)
13930eecb214SAnna Schumaker {
1394d45f60c6SWeston Andros Adamson 	struct nfs_pgio_args	*argp = &hdr->args;
1395d45f60c6SWeston Andros Adamson 	struct nfs_pgio_res	*resp = &hdr->res;
13961f2edbe3STrond Myklebust 
13971f2edbe3STrond Myklebust 	if (resp->count < argp->count) {
13981da177e4SLinus Torvalds 		static unsigned long    complain;
13991da177e4SLinus Torvalds 
14006c75dc0dSFred Isaman 		/* This a short write! */
1401d45f60c6SWeston Andros Adamson 		nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE);
140291d5b470SChuck Lever 
14031da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
14046c75dc0dSFred Isaman 		if (resp->count == 0) {
14056c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
14066c75dc0dSFred Isaman 				printk(KERN_WARNING
14076c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
14086c75dc0dSFred Isaman 				       argp->count);
14096c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
14106c75dc0dSFred Isaman 			}
1411d45f60c6SWeston Andros Adamson 			nfs_set_pgio_error(hdr, -EIO, argp->offset);
14126c75dc0dSFred Isaman 			task->tk_status = -EIO;
14136c75dc0dSFred Isaman 			return;
14146c75dc0dSFred Isaman 		}
14151da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
14161da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
14171da177e4SLinus Torvalds 			/* Resend from where the server left off */
1418d45f60c6SWeston Andros Adamson 			hdr->mds_offset += resp->count;
14191da177e4SLinus Torvalds 			argp->offset += resp->count;
14201da177e4SLinus Torvalds 			argp->pgbase += resp->count;
14211da177e4SLinus Torvalds 			argp->count -= resp->count;
14221da177e4SLinus Torvalds 		} else {
14231da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
14241da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
14251da177e4SLinus Torvalds 			 */
14261da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
14271da177e4SLinus Torvalds 		}
1428d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
14291da177e4SLinus Torvalds 	}
14301da177e4SLinus Torvalds }
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds 
143371d0a611STrond Myklebust static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait)
143471d0a611STrond Myklebust {
1435b8413f98STrond Myklebust 	int ret;
1436b8413f98STrond Myklebust 
143771d0a611STrond Myklebust 	if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags))
143871d0a611STrond Myklebust 		return 1;
1439b8413f98STrond Myklebust 	if (!may_wait)
144071d0a611STrond Myklebust 		return 0;
1441b8413f98STrond Myklebust 	ret = out_of_line_wait_on_bit_lock(&nfsi->flags,
1442b8413f98STrond Myklebust 				NFS_INO_COMMIT,
1443b8413f98STrond Myklebust 				nfs_wait_bit_killable,
1444b8413f98STrond Myklebust 				TASK_KILLABLE);
1445b8413f98STrond Myklebust 	return (ret < 0) ? ret : 1;
144671d0a611STrond Myklebust }
144771d0a611STrond Myklebust 
1448f453a54aSFred Isaman static void nfs_commit_clear_lock(struct nfs_inode *nfsi)
144971d0a611STrond Myklebust {
145071d0a611STrond Myklebust 	clear_bit(NFS_INO_COMMIT, &nfsi->flags);
14514e857c58SPeter Zijlstra 	smp_mb__after_atomic();
145271d0a611STrond Myklebust 	wake_up_bit(&nfsi->flags, NFS_INO_COMMIT);
145371d0a611STrond Myklebust }
145471d0a611STrond Myklebust 
14550b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
14561da177e4SLinus Torvalds {
14570b7c0153SFred Isaman 	put_nfs_open_context(data->context);
14580b7c0153SFred Isaman 	nfs_commit_free(data);
14591da177e4SLinus Torvalds }
1460e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
14611da177e4SLinus Torvalds 
14620b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
14639ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
14649f0ec176SAndy Adamson 			int how, int flags)
14651da177e4SLinus Torvalds {
146607737691STrond Myklebust 	struct rpc_task *task;
14679ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1468bdc7f021STrond Myklebust 	struct rpc_message msg = {
1469bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1470bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
14719ace33cdSFred Isaman 		.rpc_cred = data->cred,
1472bdc7f021STrond Myklebust 	};
147384115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
147407737691STrond Myklebust 		.task = &data->task,
14759ace33cdSFred Isaman 		.rpc_client = clnt,
1476bdc7f021STrond Myklebust 		.rpc_message = &msg,
14779ace33cdSFred Isaman 		.callback_ops = call_ops,
147884115e1cSTrond Myklebust 		.callback_data = data,
1479101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
14809f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
14813ff7576dSTrond Myklebust 		.priority = priority,
148284115e1cSTrond Myklebust 	};
1483788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
14849ace33cdSFred Isaman 	NFS_PROTO(data->inode)->commit_setup(data, &msg);
14851da177e4SLinus Torvalds 
1486a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1487bdc7f021STrond Myklebust 
14888c21c62cSWeston Andros Adamson 	nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client,
14898c21c62cSWeston Andros Adamson 		NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg);
14908c21c62cSWeston Andros Adamson 
149107737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1492dbae4c73STrond Myklebust 	if (IS_ERR(task))
1493dbae4c73STrond Myklebust 		return PTR_ERR(task);
1494d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1495d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
149607737691STrond Myklebust 	rpc_put_task(task);
1497dbae4c73STrond Myklebust 	return 0;
14981da177e4SLinus Torvalds }
1499e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
15001da177e4SLinus Torvalds 
1501378520b8SPeng Tao static loff_t nfs_get_lwb(struct list_head *head)
1502378520b8SPeng Tao {
1503378520b8SPeng Tao 	loff_t lwb = 0;
1504378520b8SPeng Tao 	struct nfs_page *req;
1505378520b8SPeng Tao 
1506378520b8SPeng Tao 	list_for_each_entry(req, head, wb_list)
1507378520b8SPeng Tao 		if (lwb < (req_offset(req) + req->wb_bytes))
1508378520b8SPeng Tao 			lwb = req_offset(req) + req->wb_bytes;
1509378520b8SPeng Tao 
1510378520b8SPeng Tao 	return lwb;
1511378520b8SPeng Tao }
1512378520b8SPeng Tao 
15131da177e4SLinus Torvalds /*
15149ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
15159ace33cdSFred Isaman  */
15160b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1517988b6dceSFred Isaman 		     struct list_head *head,
1518f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1519f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
15209ace33cdSFred Isaman {
15219ace33cdSFred Isaman 	struct nfs_page *first = nfs_list_entry(head->next);
15223d4ff43dSAl Viro 	struct inode *inode = first->wb_context->dentry->d_inode;
15239ace33cdSFred Isaman 
15249ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
15259ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
15269ace33cdSFred Isaman 
15279ace33cdSFred Isaman 	list_splice_init(head, &data->pages);
15289ace33cdSFred Isaman 
15299ace33cdSFred Isaman 	data->inode	  = inode;
15309ace33cdSFred Isaman 	data->cred	  = first->wb_context->cred;
1531988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
1532378520b8SPeng Tao 	/* only set lwb for pnfs commit */
1533378520b8SPeng Tao 	if (lseg)
1534378520b8SPeng Tao 		data->lwb = nfs_get_lwb(&data->pages);
15359ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1536f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1537b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
15389ace33cdSFred Isaman 
15399ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
15409ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
15419ace33cdSFred Isaman 	data->args.offset = 0;
15429ace33cdSFred Isaman 	data->args.count  = 0;
15430b7c0153SFred Isaman 	data->context     = get_nfs_open_context(first->wb_context);
15449ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
15459ace33cdSFred Isaman 	data->res.verf    = &data->verf;
15469ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
15479ace33cdSFred Isaman }
1548e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
15499ace33cdSFred Isaman 
1550e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1551ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1552ea2cf228SFred Isaman 		      struct nfs_commit_info *cinfo)
155364bfeb49SFred Isaman {
155464bfeb49SFred Isaman 	struct nfs_page *req;
155564bfeb49SFred Isaman 
155664bfeb49SFred Isaman 	while (!list_empty(page_list)) {
155764bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
155864bfeb49SFred Isaman 		nfs_list_remove_request(req);
1559ea2cf228SFred Isaman 		nfs_mark_request_commit(req, lseg, cinfo);
156056f9cd68SFred Isaman 		if (!cinfo->dreq) {
156164bfeb49SFred Isaman 			dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1562d56b4ddfSMel Gorman 			dec_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info,
156364bfeb49SFred Isaman 				     BDI_RECLAIMABLE);
156456f9cd68SFred Isaman 		}
15651d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
156664bfeb49SFred Isaman 	}
156764bfeb49SFred Isaman }
1568e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
156964bfeb49SFred Isaman 
15709ace33cdSFred Isaman /*
15711da177e4SLinus Torvalds  * Commit dirty pages
15721da177e4SLinus Torvalds  */
15731da177e4SLinus Torvalds static int
1574ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1575ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
15761da177e4SLinus Torvalds {
15770b7c0153SFred Isaman 	struct nfs_commit_data	*data;
15781da177e4SLinus Torvalds 
1579c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
15801da177e4SLinus Torvalds 
15811da177e4SLinus Torvalds 	if (!data)
15821da177e4SLinus Torvalds 		goto out_bad;
15831da177e4SLinus Torvalds 
15841da177e4SLinus Torvalds 	/* Set up the argument struct */
1585f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
1586f453a54aSFred Isaman 	atomic_inc(&cinfo->mds->rpcs_out);
15879f0ec176SAndy Adamson 	return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops,
15889f0ec176SAndy Adamson 				   how, 0);
15891da177e4SLinus Torvalds  out_bad:
1590ea2cf228SFred Isaman 	nfs_retry_commit(head, NULL, cinfo);
1591f453a54aSFred Isaman 	cinfo->completion_ops->error_cleanup(NFS_I(inode));
15921da177e4SLinus Torvalds 	return -ENOMEM;
15931da177e4SLinus Torvalds }
15941da177e4SLinus Torvalds 
15951da177e4SLinus Torvalds /*
15961da177e4SLinus Torvalds  * COMMIT call returned
15971da177e4SLinus Torvalds  */
1598788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
15991da177e4SLinus Torvalds {
16000b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
16011da177e4SLinus Torvalds 
1602a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
16031da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
16041da177e4SLinus Torvalds 
1605788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1606c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
1607c9d8f89dSTrond Myklebust }
1608c9d8f89dSTrond Myklebust 
1609f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1610c9d8f89dSTrond Myklebust {
1611c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1612c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1613f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
1614353db796SNeilBrown 	struct nfs_server *nfss;
1615788e7a89STrond Myklebust 
16161da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
16171da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
16181da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1619d6d6dc7cSFred Isaman 		nfs_clear_page_commit(req->wb_page);
16201da177e4SLinus Torvalds 
16211e8968c5SNiels de Vos 		dprintk("NFS:       commit (%s/%llu %d@%lld)",
16223d4ff43dSAl Viro 			req->wb_context->dentry->d_sb->s_id,
16231e8968c5SNiels de Vos 			(unsigned long long)NFS_FILEID(req->wb_context->dentry->d_inode),
16241da177e4SLinus Torvalds 			req->wb_bytes,
16251da177e4SLinus Torvalds 			(long long)req_offset(req));
1626c9d8f89dSTrond Myklebust 		if (status < 0) {
1627c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
16281da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1629c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
16301da177e4SLinus Torvalds 			goto next;
16311da177e4SLinus Torvalds 		}
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
16341da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
16352f2c63bcSTrond Myklebust 		if (!memcmp(&req->wb_verf, &data->verf.verifier, sizeof(req->wb_verf))) {
16361da177e4SLinus Torvalds 			/* We have a match */
16371da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
16381da177e4SLinus Torvalds 			dprintk(" OK\n");
16391da177e4SLinus Torvalds 			goto next;
16401da177e4SLinus Torvalds 		}
16411da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
16421da177e4SLinus Torvalds 		dprintk(" mismatch\n");
16436d884e8fSFred 		nfs_mark_request_dirty(req);
164405990d1bSTrond Myklebust 		set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
16451da177e4SLinus Torvalds 	next:
16461d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
16471da177e4SLinus Torvalds 	}
1648353db796SNeilBrown 	nfss = NFS_SERVER(data->inode);
1649353db796SNeilBrown 	if (atomic_long_read(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
1650353db796SNeilBrown 		clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
1651353db796SNeilBrown 
1652f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1653f453a54aSFred Isaman 	if (atomic_dec_and_test(&cinfo.mds->rpcs_out))
1654f453a54aSFred Isaman 		nfs_commit_clear_lock(NFS_I(data->inode));
16555917ce84SFred Isaman }
16565917ce84SFred Isaman 
16575917ce84SFred Isaman static void nfs_commit_release(void *calldata)
16585917ce84SFred Isaman {
16590b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
16605917ce84SFred Isaman 
1661f453a54aSFred Isaman 	data->completion_ops->completion(data);
1662c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
16631da177e4SLinus Torvalds }
1664788e7a89STrond Myklebust 
1665788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
16660b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1667788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1668788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1669788e7a89STrond Myklebust };
16701da177e4SLinus Torvalds 
1671f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1672f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1673f453a54aSFred Isaman 	.error_cleanup = nfs_commit_clear_lock,
1674f453a54aSFred Isaman };
1675f453a54aSFred Isaman 
16761763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1677ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
167884c53ab5SFred Isaman {
167984c53ab5SFred Isaman 	int status;
168084c53ab5SFred Isaman 
1681ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
168284c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1683ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
168484c53ab5SFred Isaman 	return status;
168584c53ab5SFred Isaman }
168684c53ab5SFred Isaman 
1687b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
16881da177e4SLinus Torvalds {
16891da177e4SLinus Torvalds 	LIST_HEAD(head);
1690ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
169171d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1692b8413f98STrond Myklebust 	int res;
16931da177e4SLinus Torvalds 
1694b8413f98STrond Myklebust 	res = nfs_commit_set_lock(NFS_I(inode), may_wait);
1695b8413f98STrond Myklebust 	if (res <= 0)
1696c5efa5fcSTrond Myklebust 		goto out_mark_dirty;
1697ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1698ea2cf228SFred Isaman 	res = nfs_scan_commit(inode, &head, &cinfo);
16991da177e4SLinus Torvalds 	if (res) {
1700a861a1e1SFred Isaman 		int error;
1701a861a1e1SFred Isaman 
1702ea2cf228SFred Isaman 		error = nfs_generic_commit_list(inode, &head, how, &cinfo);
17031da177e4SLinus Torvalds 		if (error < 0)
17041da177e4SLinus Torvalds 			return error;
1705b8413f98STrond Myklebust 		if (!may_wait)
1706b8413f98STrond Myklebust 			goto out_mark_dirty;
170774316201SNeilBrown 		error = wait_on_bit_action(&NFS_I(inode)->flags,
1708b8413f98STrond Myklebust 				NFS_INO_COMMIT,
170971d0a611STrond Myklebust 				nfs_wait_bit_killable,
171071d0a611STrond Myklebust 				TASK_KILLABLE);
1711b8413f98STrond Myklebust 		if (error < 0)
1712b8413f98STrond Myklebust 			return error;
171371d0a611STrond Myklebust 	} else
171471d0a611STrond Myklebust 		nfs_commit_clear_lock(NFS_I(inode));
1715c5efa5fcSTrond Myklebust 	return res;
1716c5efa5fcSTrond Myklebust 	/* Note: If we exit without ensuring that the commit is complete,
1717c5efa5fcSTrond Myklebust 	 * we must mark the inode as dirty. Otherwise, future calls to
1718c5efa5fcSTrond Myklebust 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1719c5efa5fcSTrond Myklebust 	 * that the data is on the disk.
1720c5efa5fcSTrond Myklebust 	 */
1721c5efa5fcSTrond Myklebust out_mark_dirty:
1722c5efa5fcSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
17231da177e4SLinus Torvalds 	return res;
17241da177e4SLinus Torvalds }
17258fc795f7STrond Myklebust 
17268fc795f7STrond Myklebust static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc)
17278fc795f7STrond Myklebust {
1728420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1729420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1730420e3646STrond Myklebust 	int ret = 0;
17318fc795f7STrond Myklebust 
17323236c3e1SJeff Layton 	/* no commits means nothing needs to be done */
1733ea2cf228SFred Isaman 	if (!nfsi->commit_info.ncommit)
17343236c3e1SJeff Layton 		return ret;
17353236c3e1SJeff Layton 
1736a00dd6c0SJeff Layton 	if (wbc->sync_mode == WB_SYNC_NONE) {
1737a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1738a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1739420e3646STrond Myklebust 		 */
1740ea2cf228SFred Isaman 		if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1))
1741420e3646STrond Myklebust 			goto out_mark_dirty;
1742420e3646STrond Myklebust 
1743a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1744420e3646STrond Myklebust 		flags = 0;
1745a00dd6c0SJeff Layton 	}
1746a00dd6c0SJeff Layton 
1747420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1748420e3646STrond Myklebust 	if (ret >= 0) {
1749420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1750420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1751420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1752420e3646STrond Myklebust 			else
1753420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1754420e3646STrond Myklebust 		}
17558fc795f7STrond Myklebust 		return 0;
1756420e3646STrond Myklebust 	}
1757420e3646STrond Myklebust out_mark_dirty:
17588fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
17598fc795f7STrond Myklebust 	return ret;
17608fc795f7STrond Myklebust }
17611da177e4SLinus Torvalds 
17628fc795f7STrond Myklebust int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
17638fc795f7STrond Myklebust {
1764a8d8f02cSBryan Schumaker 	return nfs_commit_unstable_pages(inode, wbc);
1765a8d8f02cSBryan Schumaker }
176689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode);
1767863a3c6cSAndy Adamson 
1768acdc53b2STrond Myklebust /*
1769acdc53b2STrond Myklebust  * flush the inode to disk.
1770acdc53b2STrond Myklebust  */
1771acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
177234901f70STrond Myklebust {
177334901f70STrond Myklebust 	struct writeback_control wbc = {
177472cb77f4STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
177534901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
1776d7fb1207STrond Myklebust 		.range_start = 0,
1777d7fb1207STrond Myklebust 		.range_end = LLONG_MAX,
177834901f70STrond Myklebust 	};
1779f4ce1299STrond Myklebust 	int ret;
178034901f70STrond Myklebust 
1781f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_enter(inode);
1782f4ce1299STrond Myklebust 
1783f4ce1299STrond Myklebust 	ret = sync_inode(inode, &wbc);
1784f4ce1299STrond Myklebust 
1785f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_exit(inode, ret);
1786f4ce1299STrond Myklebust 	return ret;
17871c75950bSTrond Myklebust }
1788ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all);
17891c75950bSTrond Myklebust 
17901b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
17911b3b4a1aSTrond Myklebust {
17921b3b4a1aSTrond Myklebust 	struct nfs_page *req;
17931b3b4a1aSTrond Myklebust 	int ret = 0;
17941b3b4a1aSTrond Myklebust 
1795ba8b06e6STrond Myklebust 	wait_on_page_writeback(page);
17963e217045SWeston Andros Adamson 
17973e217045SWeston Andros Adamson 	/* blocking call to cancel all requests and join to a single (head)
17983e217045SWeston Andros Adamson 	 * request */
17993e217045SWeston Andros Adamson 	req = nfs_lock_and_join_requests(page, false);
18003e217045SWeston Andros Adamson 
18013e217045SWeston Andros Adamson 	if (IS_ERR(req)) {
18023e217045SWeston Andros Adamson 		ret = PTR_ERR(req);
18033e217045SWeston Andros Adamson 	} else if (req) {
18043e217045SWeston Andros Adamson 		/* all requests from this page have been cancelled by
18053e217045SWeston Andros Adamson 		 * nfs_lock_and_join_requests, so just remove the head
18063e217045SWeston Andros Adamson 		 * request from the inode / page_private pointer and
18073e217045SWeston Andros Adamson 		 * release it */
18081b3b4a1aSTrond Myklebust 		nfs_inode_remove_request(req);
18091b3b4a1aSTrond Myklebust 		/*
18101b3b4a1aSTrond Myklebust 		 * In case nfs_inode_remove_request has marked the
18111b3b4a1aSTrond Myklebust 		 * page as being dirty
18121b3b4a1aSTrond Myklebust 		 */
18131b3b4a1aSTrond Myklebust 		cancel_dirty_page(page, PAGE_CACHE_SIZE);
18141d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
18151b3b4a1aSTrond Myklebust 	}
18163e217045SWeston Andros Adamson 
18171b3b4a1aSTrond Myklebust 	return ret;
18181b3b4a1aSTrond Myklebust }
18191b3b4a1aSTrond Myklebust 
18201c75950bSTrond Myklebust /*
18211c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
18221c75950bSTrond Myklebust  */
18231c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
18241c75950bSTrond Myklebust {
182529418aa4SMel Gorman 	loff_t range_start = page_file_offset(page);
18267f2f12d9STrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
18277f2f12d9STrond Myklebust 	struct writeback_control wbc = {
18287f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
18297f2f12d9STrond Myklebust 		.nr_to_write = 0,
18307f2f12d9STrond Myklebust 		.range_start = range_start,
18317f2f12d9STrond Myklebust 		.range_end = range_end,
18327f2f12d9STrond Myklebust 	};
18337f2f12d9STrond Myklebust 	int ret;
18347f2f12d9STrond Myklebust 
1835f4ce1299STrond Myklebust 	trace_nfs_writeback_page_enter(inode);
1836f4ce1299STrond Myklebust 
18370522f6adSTrond Myklebust 	for (;;) {
1838ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
18397f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
18407f2f12d9STrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
18417f2f12d9STrond Myklebust 			if (ret < 0)
18427f2f12d9STrond Myklebust 				goto out_error;
18430522f6adSTrond Myklebust 			continue;
18447f2f12d9STrond Myklebust 		}
1845f4ce1299STrond Myklebust 		ret = 0;
18460522f6adSTrond Myklebust 		if (!PagePrivate(page))
18470522f6adSTrond Myklebust 			break;
18480522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
18497f2f12d9STrond Myklebust 		if (ret < 0)
18507f2f12d9STrond Myklebust 			goto out_error;
18517f2f12d9STrond Myklebust 	}
18527f2f12d9STrond Myklebust out_error:
1853f4ce1299STrond Myklebust 	trace_nfs_writeback_page_exit(inode, ret);
18547f2f12d9STrond Myklebust 	return ret;
18551c75950bSTrond Myklebust }
18561c75950bSTrond Myklebust 
1857074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
1858074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
1859a6bc32b8SMel Gorman 		struct page *page, enum migrate_mode mode)
1860074cc1deSTrond Myklebust {
18612da95652SJeff Layton 	/*
18622da95652SJeff Layton 	 * If PagePrivate is set, then the page is currently associated with
18632da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
18642da95652SJeff Layton 	 *
18652da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
18662da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
18672da95652SJeff Layton 	 *        the page lock.
18682da95652SJeff Layton 	 */
18692da95652SJeff Layton 	if (PagePrivate(page))
18702da95652SJeff Layton 		return -EBUSY;
1871074cc1deSTrond Myklebust 
18728c209ce7SDavid Howells 	if (!nfs_fscache_release_page(page, GFP_KERNEL))
18738c209ce7SDavid Howells 		return -EBUSY;
1874074cc1deSTrond Myklebust 
1875a6bc32b8SMel Gorman 	return migrate_page(mapping, newpage, page, mode);
1876074cc1deSTrond Myklebust }
1877074cc1deSTrond Myklebust #endif
1878074cc1deSTrond Myklebust 
1879f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
18801da177e4SLinus Torvalds {
18811da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
18821e7f3a48SWeston Andros Adamson 					     sizeof(struct nfs_pgio_header),
18831da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
188420c2df83SPaul Mundt 					     NULL);
18851da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
18861da177e4SLinus Torvalds 		return -ENOMEM;
18871da177e4SLinus Torvalds 
188893d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
18891da177e4SLinus Torvalds 						     nfs_wdata_cachep);
18901da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
18913dd4765fSJeff Layton 		goto out_destroy_write_cache;
18921da177e4SLinus Torvalds 
18930b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
18940b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
18950b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
18960b7c0153SFred Isaman 					     NULL);
18970b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
18983dd4765fSJeff Layton 		goto out_destroy_write_mempool;
18990b7c0153SFred Isaman 
190093d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
19014c100210SYanchuan Nian 						      nfs_cdata_cachep);
19021da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
19033dd4765fSJeff Layton 		goto out_destroy_commit_cache;
19041da177e4SLinus Torvalds 
190589a09141SPeter Zijlstra 	/*
190689a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
190789a09141SPeter Zijlstra 	 *
190889a09141SPeter Zijlstra 	 *  64MB:    8192k
190989a09141SPeter Zijlstra 	 * 128MB:   11585k
191089a09141SPeter Zijlstra 	 * 256MB:   16384k
191189a09141SPeter Zijlstra 	 * 512MB:   23170k
191289a09141SPeter Zijlstra 	 *   1GB:   32768k
191389a09141SPeter Zijlstra 	 *   2GB:   46340k
191489a09141SPeter Zijlstra 	 *   4GB:   65536k
191589a09141SPeter Zijlstra 	 *   8GB:   92681k
191689a09141SPeter Zijlstra 	 *  16GB:  131072k
191789a09141SPeter Zijlstra 	 *
191889a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
191989a09141SPeter Zijlstra 	 * Limit the default to 256M
192089a09141SPeter Zijlstra 	 */
192189a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
192289a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
192389a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
192489a09141SPeter Zijlstra 
19251da177e4SLinus Torvalds 	return 0;
19263dd4765fSJeff Layton 
19273dd4765fSJeff Layton out_destroy_commit_cache:
19283dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
19293dd4765fSJeff Layton out_destroy_write_mempool:
19303dd4765fSJeff Layton 	mempool_destroy(nfs_wdata_mempool);
19313dd4765fSJeff Layton out_destroy_write_cache:
19323dd4765fSJeff Layton 	kmem_cache_destroy(nfs_wdata_cachep);
19333dd4765fSJeff Layton 	return -ENOMEM;
19341da177e4SLinus Torvalds }
19351da177e4SLinus Torvalds 
1936266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
19371da177e4SLinus Torvalds {
19381da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
19393dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
19401da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
19411a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
19421da177e4SLinus Torvalds }
19431da177e4SLinus Torvalds 
19444a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops = {
1945a4cdda59SAnna Schumaker 	.rw_mode		= FMODE_WRITE,
19464a0de55cSAnna Schumaker 	.rw_alloc_header	= nfs_writehdr_alloc,
19474a0de55cSAnna Schumaker 	.rw_free_header		= nfs_writehdr_free,
1948a4cdda59SAnna Schumaker 	.rw_release		= nfs_writeback_release_common,
19490eecb214SAnna Schumaker 	.rw_done		= nfs_writeback_done,
19500eecb214SAnna Schumaker 	.rw_result		= nfs_writeback_result,
19511ed26f33SAnna Schumaker 	.rw_initiate		= nfs_initiate_write,
19524a0de55cSAnna Schumaker };
1953