xref: /linux/fs/nfs/write.c (revision 2b17d725f9be59a1bfa0583af690c463fca1f385)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * linux/fs/nfs/write.c
41da177e4SLinus Torvalds  *
57c85d900STrond Myklebust  * Write file data over NFS.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <linux/types.h>
111da177e4SLinus Torvalds #include <linux/slab.h>
121da177e4SLinus Torvalds #include <linux/mm.h>
131da177e4SLinus Torvalds #include <linux/pagemap.h>
141da177e4SLinus Torvalds #include <linux/file.h>
151da177e4SLinus Torvalds #include <linux/writeback.h>
1689a09141SPeter Zijlstra #include <linux/swap.h>
17074cc1deSTrond Myklebust #include <linux/migrate.h>
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
201da177e4SLinus Torvalds #include <linux/nfs_fs.h>
211da177e4SLinus Torvalds #include <linux/nfs_mount.h>
221da177e4SLinus Torvalds #include <linux/nfs_page.h>
233fcfab16SAndrew Morton #include <linux/backing-dev.h>
24afeacc8cSPaul Gortmaker #include <linux/export.h>
25af7cf057STrond Myklebust #include <linux/freezer.h>
26af7cf057STrond Myklebust #include <linux/wait.h>
271eb5d98fSJeff Layton #include <linux/iversion.h>
283fcfab16SAndrew Morton 
297c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
30875bc3fbSTrond Myklebust #include <linux/sched/mm.h>
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #include "delegation.h"
3349a70f27STrond Myklebust #include "internal.h"
3491d5b470SChuck Lever #include "iostat.h"
35def6ed7eSAndy Adamson #include "nfs4_fs.h"
36074cc1deSTrond Myklebust #include "fscache.h"
3794ad1c80SFred Isaman #include "pnfs.h"
381da177e4SLinus Torvalds 
39f4ce1299STrond Myklebust #include "nfstrace.h"
40f4ce1299STrond Myklebust 
411da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #define MIN_POOL_WRITE		(32)
441da177e4SLinus Torvalds #define MIN_POOL_COMMIT		(4)
451da177e4SLinus Torvalds 
46919e3bd9STrond Myklebust struct nfs_io_completion {
47919e3bd9STrond Myklebust 	void (*complete)(void *data);
48919e3bd9STrond Myklebust 	void *data;
49919e3bd9STrond Myklebust 	struct kref refcount;
50919e3bd9STrond Myklebust };
51919e3bd9STrond Myklebust 
521da177e4SLinus Torvalds /*
531da177e4SLinus Torvalds  * Local function declarations
541da177e4SLinus Torvalds  */
55f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req);
56788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops;
57061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops;
58f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops;
594a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops;
60d4581383SWeston Andros Adamson static void nfs_clear_request_commit(struct nfs_page *req);
6102d1426cSWeston Andros Adamson static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
6202d1426cSWeston Andros Adamson 				      struct inode *inode);
633a3908c8STrond Myklebust static struct nfs_page *
643a3908c8STrond Myklebust nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
653a3908c8STrond Myklebust 						struct page *page);
661da177e4SLinus Torvalds 
67e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep;
683feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
690b7c0153SFred Isaman static struct kmem_cache *nfs_cdata_cachep;
701da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
711da177e4SLinus Torvalds 
72518662e0SNeilBrown struct nfs_commit_data *nfs_commitdata_alloc(bool never_fail)
731da177e4SLinus Torvalds {
74518662e0SNeilBrown 	struct nfs_commit_data *p;
7540859d7eSChuck Lever 
76518662e0SNeilBrown 	if (never_fail)
77518662e0SNeilBrown 		p = mempool_alloc(nfs_commit_mempool, GFP_NOIO);
78518662e0SNeilBrown 	else {
79518662e0SNeilBrown 		/* It is OK to do some reclaim, not no safe to wait
80518662e0SNeilBrown 		 * for anything to be returned to the pool.
81518662e0SNeilBrown 		 * mempool_alloc() cannot handle that particular combination,
82518662e0SNeilBrown 		 * so we need two separate attempts.
83518662e0SNeilBrown 		 */
84518662e0SNeilBrown 		p = mempool_alloc(nfs_commit_mempool, GFP_NOWAIT);
85518662e0SNeilBrown 		if (!p)
86518662e0SNeilBrown 			p = kmem_cache_alloc(nfs_cdata_cachep, GFP_NOIO |
87518662e0SNeilBrown 					     __GFP_NOWARN | __GFP_NORETRY);
88518662e0SNeilBrown 		if (!p)
89518662e0SNeilBrown 			return NULL;
90518662e0SNeilBrown 	}
91518662e0SNeilBrown 
921da177e4SLinus Torvalds 	memset(p, 0, sizeof(*p));
931da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->pages);
941da177e4SLinus Torvalds 	return p;
951da177e4SLinus Torvalds }
96e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
971da177e4SLinus Torvalds 
980b7c0153SFred Isaman void nfs_commit_free(struct nfs_commit_data *p)
991da177e4SLinus Torvalds {
1001da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
1011da177e4SLinus Torvalds }
102e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free);
1031da177e4SLinus Torvalds 
1041e7f3a48SWeston Andros Adamson static struct nfs_pgio_header *nfs_writehdr_alloc(void)
1053feb2d49STrond Myklebust {
106*2b17d725STrond Myklebust 	struct nfs_pgio_header *p = mempool_alloc(nfs_wdata_mempool, GFP_KERNEL);
1073feb2d49STrond Myklebust 
1083feb2d49STrond Myklebust 	memset(p, 0, sizeof(*p));
109fbe77c30SBenjamin Coddington 	p->rw_mode = FMODE_WRITE;
1103feb2d49STrond Myklebust 	return p;
1113feb2d49STrond Myklebust }
1123feb2d49STrond Myklebust 
1131e7f3a48SWeston Andros Adamson static void nfs_writehdr_free(struct nfs_pgio_header *hdr)
1146c75dc0dSFred Isaman {
1151e7f3a48SWeston Andros Adamson 	mempool_free(hdr, nfs_wdata_mempool);
1163feb2d49STrond Myklebust }
1171da177e4SLinus Torvalds 
118919e3bd9STrond Myklebust static struct nfs_io_completion *nfs_io_completion_alloc(gfp_t gfp_flags)
119919e3bd9STrond Myklebust {
120919e3bd9STrond Myklebust 	return kmalloc(sizeof(struct nfs_io_completion), gfp_flags);
121919e3bd9STrond Myklebust }
122919e3bd9STrond Myklebust 
123919e3bd9STrond Myklebust static void nfs_io_completion_init(struct nfs_io_completion *ioc,
124919e3bd9STrond Myklebust 		void (*complete)(void *), void *data)
125919e3bd9STrond Myklebust {
126919e3bd9STrond Myklebust 	ioc->complete = complete;
127919e3bd9STrond Myklebust 	ioc->data = data;
128919e3bd9STrond Myklebust 	kref_init(&ioc->refcount);
129919e3bd9STrond Myklebust }
130919e3bd9STrond Myklebust 
131919e3bd9STrond Myklebust static void nfs_io_completion_release(struct kref *kref)
132919e3bd9STrond Myklebust {
133919e3bd9STrond Myklebust 	struct nfs_io_completion *ioc = container_of(kref,
134919e3bd9STrond Myklebust 			struct nfs_io_completion, refcount);
135919e3bd9STrond Myklebust 	ioc->complete(ioc->data);
136919e3bd9STrond Myklebust 	kfree(ioc);
137919e3bd9STrond Myklebust }
138919e3bd9STrond Myklebust 
139919e3bd9STrond Myklebust static void nfs_io_completion_get(struct nfs_io_completion *ioc)
140919e3bd9STrond Myklebust {
141919e3bd9STrond Myklebust 	if (ioc != NULL)
142919e3bd9STrond Myklebust 		kref_get(&ioc->refcount);
143919e3bd9STrond Myklebust }
144919e3bd9STrond Myklebust 
145919e3bd9STrond Myklebust static void nfs_io_completion_put(struct nfs_io_completion *ioc)
146919e3bd9STrond Myklebust {
147919e3bd9STrond Myklebust 	if (ioc != NULL)
148919e3bd9STrond Myklebust 		kref_put(&ioc->refcount, nfs_io_completion_release);
149919e3bd9STrond Myklebust }
150919e3bd9STrond Myklebust 
151bd37d6fcSTrond Myklebust static struct nfs_page *
152bd37d6fcSTrond Myklebust nfs_page_private_request(struct page *page)
153bd37d6fcSTrond Myklebust {
154bd37d6fcSTrond Myklebust 	if (!PagePrivate(page))
155bd37d6fcSTrond Myklebust 		return NULL;
156bd37d6fcSTrond Myklebust 	return (struct nfs_page *)page_private(page);
157bd37d6fcSTrond Myklebust }
158bd37d6fcSTrond Myklebust 
15984d3a9a9SWeston Andros Adamson /*
16084d3a9a9SWeston Andros Adamson  * nfs_page_find_head_request_locked - find head request associated with @page
16184d3a9a9SWeston Andros Adamson  *
16284d3a9a9SWeston Andros Adamson  * must be called while holding the inode lock.
16384d3a9a9SWeston Andros Adamson  *
16484d3a9a9SWeston Andros Adamson  * returns matching head request with reference held, or NULL if not found.
16584d3a9a9SWeston Andros Adamson  */
16629418aa4SMel Gorman static struct nfs_page *
167b30d2f04STrond Myklebust nfs_page_find_private_request(struct page *page)
168277459d2STrond Myklebust {
1694b9bb25bSTrond Myklebust 	struct address_space *mapping = page_file_mapping(page);
170bd37d6fcSTrond Myklebust 	struct nfs_page *req;
171277459d2STrond Myklebust 
172b30d2f04STrond Myklebust 	if (!PagePrivate(page))
173b30d2f04STrond Myklebust 		return NULL;
1744b9bb25bSTrond Myklebust 	spin_lock(&mapping->private_lock);
175bd37d6fcSTrond Myklebust 	req = nfs_page_private_request(page);
17684d3a9a9SWeston Andros Adamson 	if (req) {
17784d3a9a9SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_head != req);
17829418aa4SMel Gorman 		kref_get(&req->wb_kref);
17984d3a9a9SWeston Andros Adamson 	}
1804b9bb25bSTrond Myklebust 	spin_unlock(&mapping->private_lock);
181b30d2f04STrond Myklebust 	return req;
182b30d2f04STrond Myklebust }
18329418aa4SMel Gorman 
184b30d2f04STrond Myklebust static struct nfs_page *
185b30d2f04STrond Myklebust nfs_page_find_swap_request(struct page *page)
186b30d2f04STrond Myklebust {
187b30d2f04STrond Myklebust 	struct inode *inode = page_file_mapping(page)->host;
188b30d2f04STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
189b30d2f04STrond Myklebust 	struct nfs_page *req = NULL;
190b30d2f04STrond Myklebust 	if (!PageSwapCache(page))
191b30d2f04STrond Myklebust 		return NULL;
192e824f99aSTrond Myklebust 	mutex_lock(&nfsi->commit_mutex);
193b30d2f04STrond Myklebust 	if (PageSwapCache(page)) {
194b30d2f04STrond Myklebust 		req = nfs_page_search_commits_for_head_request_locked(nfsi,
195b30d2f04STrond Myklebust 			page);
196b30d2f04STrond Myklebust 		if (req) {
197b30d2f04STrond Myklebust 			WARN_ON_ONCE(req->wb_head != req);
198b30d2f04STrond Myklebust 			kref_get(&req->wb_kref);
199b30d2f04STrond Myklebust 		}
200b30d2f04STrond Myklebust 	}
201e824f99aSTrond Myklebust 	mutex_unlock(&nfsi->commit_mutex);
202277459d2STrond Myklebust 	return req;
203277459d2STrond Myklebust }
204277459d2STrond Myklebust 
20584d3a9a9SWeston Andros Adamson /*
20684d3a9a9SWeston Andros Adamson  * nfs_page_find_head_request - find head request associated with @page
20784d3a9a9SWeston Andros Adamson  *
20884d3a9a9SWeston Andros Adamson  * returns matching head request with reference held, or NULL if not found.
20984d3a9a9SWeston Andros Adamson  */
21084d3a9a9SWeston Andros Adamson static struct nfs_page *nfs_page_find_head_request(struct page *page)
211277459d2STrond Myklebust {
212b30d2f04STrond Myklebust 	struct nfs_page *req;
213277459d2STrond Myklebust 
214b30d2f04STrond Myklebust 	req = nfs_page_find_private_request(page);
215b30d2f04STrond Myklebust 	if (!req)
216b30d2f04STrond Myklebust 		req = nfs_page_find_swap_request(page);
217277459d2STrond Myklebust 	return req;
218277459d2STrond Myklebust }
219277459d2STrond Myklebust 
2201da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
2211da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
2221da177e4SLinus Torvalds {
223d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
224a3d01454STrond Myklebust 	loff_t end, i_size;
225a3d01454STrond Myklebust 	pgoff_t end_index;
2261da177e4SLinus Torvalds 
227a3d01454STrond Myklebust 	spin_lock(&inode->i_lock);
228a3d01454STrond Myklebust 	i_size = i_size_read(inode);
22909cbfeafSKirill A. Shutemov 	end_index = (i_size - 1) >> PAGE_SHIFT;
2308cd79788SHuang Ying 	if (i_size > 0 && page_index(page) < end_index)
231a3d01454STrond Myklebust 		goto out;
232d56b4ddfSMel Gorman 	end = page_file_offset(page) + ((loff_t)offset+count);
2331da177e4SLinus Torvalds 	if (i_size >= end)
234a3d01454STrond Myklebust 		goto out;
2351da177e4SLinus Torvalds 	i_size_write(inode, end);
236f6cdfa6dSTrond Myklebust 	NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE;
237a3d01454STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
238a3d01454STrond Myklebust out:
239a3d01454STrond Myklebust 	spin_unlock(&inode->i_lock);
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds 
242a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
243d2ceb7e5SBenjamin Coddington static void nfs_set_pageerror(struct address_space *mapping)
244a301b777STrond Myklebust {
245d2ceb7e5SBenjamin Coddington 	nfs_zap_mapping(mapping->host, mapping);
246a301b777STrond Myklebust }
247a301b777STrond Myklebust 
2486fbda89bSTrond Myklebust static void nfs_mapping_set_error(struct page *page, int error)
2496fbda89bSTrond Myklebust {
2506fbda89bSTrond Myklebust 	SetPageError(page);
2516fbda89bSTrond Myklebust 	mapping_set_error(page_file_mapping(page), error);
2526fbda89bSTrond Myklebust }
2536fbda89bSTrond Myklebust 
254d72ddcbaSWeston Andros Adamson /*
255d72ddcbaSWeston Andros Adamson  * nfs_page_group_search_locked
256d72ddcbaSWeston Andros Adamson  * @head - head request of page group
257d72ddcbaSWeston Andros Adamson  * @page_offset - offset into page
258d72ddcbaSWeston Andros Adamson  *
259d72ddcbaSWeston Andros Adamson  * Search page group with head @head to find a request that contains the
260d72ddcbaSWeston Andros Adamson  * page offset @page_offset.
261d72ddcbaSWeston Andros Adamson  *
262d72ddcbaSWeston Andros Adamson  * Returns a pointer to the first matching nfs request, or NULL if no
263d72ddcbaSWeston Andros Adamson  * match is found.
264d72ddcbaSWeston Andros Adamson  *
265d72ddcbaSWeston Andros Adamson  * Must be called with the page group lock held
266d72ddcbaSWeston Andros Adamson  */
267d72ddcbaSWeston Andros Adamson static struct nfs_page *
268d72ddcbaSWeston Andros Adamson nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset)
269d72ddcbaSWeston Andros Adamson {
270d72ddcbaSWeston Andros Adamson 	struct nfs_page *req;
271d72ddcbaSWeston Andros Adamson 
272d72ddcbaSWeston Andros Adamson 	req = head;
273d72ddcbaSWeston Andros Adamson 	do {
274d72ddcbaSWeston Andros Adamson 		if (page_offset >= req->wb_pgbase &&
275d72ddcbaSWeston Andros Adamson 		    page_offset < (req->wb_pgbase + req->wb_bytes))
276d72ddcbaSWeston Andros Adamson 			return req;
277d72ddcbaSWeston Andros Adamson 
278d72ddcbaSWeston Andros Adamson 		req = req->wb_this_page;
279d72ddcbaSWeston Andros Adamson 	} while (req != head);
280d72ddcbaSWeston Andros Adamson 
281d72ddcbaSWeston Andros Adamson 	return NULL;
282d72ddcbaSWeston Andros Adamson }
283d72ddcbaSWeston Andros Adamson 
284d72ddcbaSWeston Andros Adamson /*
285d72ddcbaSWeston Andros Adamson  * nfs_page_group_covers_page
286d72ddcbaSWeston Andros Adamson  * @head - head request of page group
287d72ddcbaSWeston Andros Adamson  *
288d72ddcbaSWeston Andros Adamson  * Return true if the page group with head @head covers the whole page,
289d72ddcbaSWeston Andros Adamson  * returns false otherwise
290d72ddcbaSWeston Andros Adamson  */
291d72ddcbaSWeston Andros Adamson static bool nfs_page_group_covers_page(struct nfs_page *req)
292d72ddcbaSWeston Andros Adamson {
293d72ddcbaSWeston Andros Adamson 	struct nfs_page *tmp;
294d72ddcbaSWeston Andros Adamson 	unsigned int pos = 0;
295d72ddcbaSWeston Andros Adamson 	unsigned int len = nfs_page_length(req->wb_page);
296d72ddcbaSWeston Andros Adamson 
2971344b7eaSTrond Myklebust 	nfs_page_group_lock(req);
298d72ddcbaSWeston Andros Adamson 
2997e8a30f8STrond Myklebust 	for (;;) {
300d72ddcbaSWeston Andros Adamson 		tmp = nfs_page_group_search_locked(req->wb_head, pos);
3017e8a30f8STrond Myklebust 		if (!tmp)
3027e8a30f8STrond Myklebust 			break;
3037e8a30f8STrond Myklebust 		pos = tmp->wb_pgbase + tmp->wb_bytes;
304d72ddcbaSWeston Andros Adamson 	}
305d72ddcbaSWeston Andros Adamson 
306d72ddcbaSWeston Andros Adamson 	nfs_page_group_unlock(req);
3077e8a30f8STrond Myklebust 	return pos >= len;
308d72ddcbaSWeston Andros Adamson }
309d72ddcbaSWeston Andros Adamson 
3101da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
3111da177e4SLinus Torvalds  * covers the full page.
3121da177e4SLinus Torvalds  */
313d72ddcbaSWeston Andros Adamson static void nfs_mark_uptodate(struct nfs_page *req)
3141da177e4SLinus Torvalds {
315d72ddcbaSWeston Andros Adamson 	if (PageUptodate(req->wb_page))
3161da177e4SLinus Torvalds 		return;
317d72ddcbaSWeston Andros Adamson 	if (!nfs_page_group_covers_page(req))
3181da177e4SLinus Torvalds 		return;
319d72ddcbaSWeston Andros Adamson 	SetPageUptodate(req->wb_page);
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
3231da177e4SLinus Torvalds {
324e87b4c7aSNeilBrown 	int ret = 0;
325cca588d6STrond Myklebust 
326e87b4c7aSNeilBrown 	if (wbc->sync_mode == WB_SYNC_ALL)
327e87b4c7aSNeilBrown 		ret = FLUSH_COND_STABLE;
328e87b4c7aSNeilBrown 	return ret;
3291da177e4SLinus Torvalds }
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds /*
33289a09141SPeter Zijlstra  * NFS congestion control
33389a09141SPeter Zijlstra  */
33489a09141SPeter Zijlstra 
33589a09141SPeter Zijlstra int nfs_congestion_kb;
33689a09141SPeter Zijlstra 
33789a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
33889a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
33989a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
34089a09141SPeter Zijlstra 
341deed85e7STrond Myklebust static void nfs_set_page_writeback(struct page *page)
34289a09141SPeter Zijlstra {
3430db10944SJan Kara 	struct inode *inode = page_file_mapping(page)->host;
3440db10944SJan Kara 	struct nfs_server *nfss = NFS_SERVER(inode);
3455a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
3465a6d41b3STrond Myklebust 
347deed85e7STrond Myklebust 	WARN_ON_ONCE(ret != 0);
34889a09141SPeter Zijlstra 
349277866a0SPeter Zijlstra 	if (atomic_long_inc_return(&nfss->writeback) >
3500db10944SJan Kara 			NFS_CONGESTION_ON_THRESH)
3510db10944SJan Kara 		set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
35289a09141SPeter Zijlstra }
35389a09141SPeter Zijlstra 
35420633f04SWeston Andros Adamson static void nfs_end_page_writeback(struct nfs_page *req)
35589a09141SPeter Zijlstra {
35620633f04SWeston Andros Adamson 	struct inode *inode = page_file_mapping(req->wb_page)->host;
35789a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
35831a01f09STrond Myklebust 	bool is_done;
35989a09141SPeter Zijlstra 
36031a01f09STrond Myklebust 	is_done = nfs_page_group_sync_on_bit(req, PG_WB_END);
36131a01f09STrond Myklebust 	nfs_unlock_request(req);
36231a01f09STrond Myklebust 	if (!is_done)
36320633f04SWeston Andros Adamson 		return;
36420633f04SWeston Andros Adamson 
36520633f04SWeston Andros Adamson 	end_page_writeback(req->wb_page);
366c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
3670db10944SJan Kara 		clear_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
36889a09141SPeter Zijlstra }
36989a09141SPeter Zijlstra 
370d4581383SWeston Andros Adamson /*
371d4581383SWeston Andros Adamson  * nfs_unroll_locks_and_wait -  unlock all newly locked reqs and wait on @req
372d4581383SWeston Andros Adamson  *
373d4581383SWeston Andros Adamson  * this is a helper function for nfs_lock_and_join_requests
374d4581383SWeston Andros Adamson  *
375d4581383SWeston Andros Adamson  * @inode - inode associated with request page group, must be holding inode lock
376d4581383SWeston Andros Adamson  * @head  - head request of page group, must be holding head lock
377d4581383SWeston Andros Adamson  * @req   - request that couldn't lock and needs to wait on the req bit lock
378d4581383SWeston Andros Adamson  *
379b5bab9bfSTrond Myklebust  * NOTE: this must be called holding page_group bit lock
380b5bab9bfSTrond Myklebust  *       which will be released before returning.
381d4581383SWeston Andros Adamson  *
382d4581383SWeston Andros Adamson  * returns 0 on success, < 0 on error.
383d4581383SWeston Andros Adamson  */
38474a6d4b5STrond Myklebust static void
38574a6d4b5STrond Myklebust nfs_unroll_locks(struct inode *inode, struct nfs_page *head,
3866d17d653STrond Myklebust 			  struct nfs_page *req)
387d4581383SWeston Andros Adamson {
388d4581383SWeston Andros Adamson 	struct nfs_page *tmp;
389e261f51fSTrond Myklebust 
390d4581383SWeston Andros Adamson 	/* relinquish all the locks successfully grabbed this run */
3915b2b5187STrond Myklebust 	for (tmp = head->wb_this_page ; tmp != req; tmp = tmp->wb_this_page) {
3925b2b5187STrond Myklebust 		if (!kref_read(&tmp->wb_kref))
3935b2b5187STrond Myklebust 			continue;
3945b2b5187STrond Myklebust 		nfs_unlock_and_release_request(tmp);
3955b2b5187STrond Myklebust 	}
396e261f51fSTrond Myklebust }
397d4581383SWeston Andros Adamson 
398d4581383SWeston Andros Adamson /*
399d4581383SWeston Andros Adamson  * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests
400d4581383SWeston Andros Adamson  *
401d4581383SWeston Andros Adamson  * @destroy_list - request list (using wb_this_page) terminated by @old_head
402d4581383SWeston Andros Adamson  * @old_head - the old head of the list
403d4581383SWeston Andros Adamson  *
404d4581383SWeston Andros Adamson  * All subrequests must be locked and removed from all lists, so at this point
405d4581383SWeston Andros Adamson  * they are only "active" in this function, and possibly in nfs_wait_on_request
406d4581383SWeston Andros Adamson  * with a reference held by some other context.
407d4581383SWeston Andros Adamson  */
408d4581383SWeston Andros Adamson static void
409d4581383SWeston Andros Adamson nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list,
410b66aaa8dSTrond Myklebust 				 struct nfs_page *old_head,
411b66aaa8dSTrond Myklebust 				 struct inode *inode)
412d4581383SWeston Andros Adamson {
413d4581383SWeston Andros Adamson 	while (destroy_list) {
414d4581383SWeston Andros Adamson 		struct nfs_page *subreq = destroy_list;
415d4581383SWeston Andros Adamson 
416d4581383SWeston Andros Adamson 		destroy_list = (subreq->wb_this_page == old_head) ?
417d4581383SWeston Andros Adamson 				   NULL : subreq->wb_this_page;
418d4581383SWeston Andros Adamson 
419d4581383SWeston Andros Adamson 		WARN_ON_ONCE(old_head != subreq->wb_head);
420d4581383SWeston Andros Adamson 
421d4581383SWeston Andros Adamson 		/* make sure old group is not used */
422d4581383SWeston Andros Adamson 		subreq->wb_this_page = subreq;
423d4581383SWeston Andros Adamson 
424902a4c00STrond Myklebust 		clear_bit(PG_REMOVE, &subreq->wb_flags);
425902a4c00STrond Myklebust 
4265b2b5187STrond Myklebust 		/* Note: races with nfs_page_group_destroy() */
4275b2b5187STrond Myklebust 		if (!kref_read(&subreq->wb_kref)) {
4285b2b5187STrond Myklebust 			/* Check if we raced with nfs_page_group_destroy() */
429902a4c00STrond Myklebust 			if (test_and_clear_bit(PG_TEARDOWN, &subreq->wb_flags))
4305b2b5187STrond Myklebust 				nfs_free_request(subreq);
4315b2b5187STrond Myklebust 			continue;
4325b2b5187STrond Myklebust 		}
433d4581383SWeston Andros Adamson 
4345b2b5187STrond Myklebust 		subreq->wb_head = subreq;
4355b2b5187STrond Myklebust 
436b66aaa8dSTrond Myklebust 		if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags)) {
437d4581383SWeston Andros Adamson 			nfs_release_request(subreq);
438a6b6d5b8STrond Myklebust 			atomic_long_dec(&NFS_I(inode)->nrequests);
439d4581383SWeston Andros Adamson 		}
4405b2b5187STrond Myklebust 
4415b2b5187STrond Myklebust 		/* subreq is now totally disconnected from page group or any
4425b2b5187STrond Myklebust 		 * write / commit lists. last chance to wake any waiters */
4435b2b5187STrond Myklebust 		nfs_unlock_and_release_request(subreq);
444d4581383SWeston Andros Adamson 	}
445d4581383SWeston Andros Adamson }
446d4581383SWeston Andros Adamson 
447d4581383SWeston Andros Adamson /*
448d4581383SWeston Andros Adamson  * nfs_lock_and_join_requests - join all subreqs to the head req and return
449d4581383SWeston Andros Adamson  *                              a locked reference, cancelling any pending
450d4581383SWeston Andros Adamson  *                              operations for this page.
451d4581383SWeston Andros Adamson  *
452d4581383SWeston Andros Adamson  * @page - the page used to lookup the "page group" of nfs_page structures
453d4581383SWeston Andros Adamson  *
454d4581383SWeston Andros Adamson  * This function joins all sub requests to the head request by first
455d4581383SWeston Andros Adamson  * locking all requests in the group, cancelling any pending operations
456d4581383SWeston Andros Adamson  * and finally updating the head request to cover the whole range covered by
457d4581383SWeston Andros Adamson  * the (former) group.  All subrequests are removed from any write or commit
458d4581383SWeston Andros Adamson  * lists, unlinked from the group and destroyed.
459d4581383SWeston Andros Adamson  *
460d4581383SWeston Andros Adamson  * Returns a locked, referenced pointer to the head request - which after
461d4581383SWeston Andros Adamson  * this call is guaranteed to be the only request associated with the page.
462d4581383SWeston Andros Adamson  * Returns NULL if no requests are found for @page, or a ERR_PTR if an
463d4581383SWeston Andros Adamson  * error was encountered.
464d4581383SWeston Andros Adamson  */
465d4581383SWeston Andros Adamson static struct nfs_page *
4666d17d653STrond Myklebust nfs_lock_and_join_requests(struct page *page)
467d4581383SWeston Andros Adamson {
468d4581383SWeston Andros Adamson 	struct inode *inode = page_file_mapping(page)->host;
469d4581383SWeston Andros Adamson 	struct nfs_page *head, *subreq;
470d4581383SWeston Andros Adamson 	struct nfs_page *destroy_list = NULL;
471d4581383SWeston Andros Adamson 	unsigned int total_bytes;
472d4581383SWeston Andros Adamson 	int ret;
473d4581383SWeston Andros Adamson 
474d4581383SWeston Andros Adamson try_again:
475d4581383SWeston Andros Adamson 	/*
476d4581383SWeston Andros Adamson 	 * A reference is taken only on the head request which acts as a
477d4581383SWeston Andros Adamson 	 * reference to the whole page group - the group will not be destroyed
478d4581383SWeston Andros Adamson 	 * until the head reference is released.
479d4581383SWeston Andros Adamson 	 */
480bd37d6fcSTrond Myklebust 	head = nfs_page_find_head_request(page);
481bd37d6fcSTrond Myklebust 	if (!head)
482d4581383SWeston Andros Adamson 		return NULL;
483d4581383SWeston Andros Adamson 
484a0e265bcSTrond Myklebust 	/* lock the page head first in order to avoid an ABBA inefficiency */
485a0e265bcSTrond Myklebust 	if (!nfs_lock_request(head)) {
486a0e265bcSTrond Myklebust 		ret = nfs_wait_on_request(head);
487a0e265bcSTrond Myklebust 		nfs_release_request(head);
488a0e265bcSTrond Myklebust 		if (ret < 0)
489a0e265bcSTrond Myklebust 			return ERR_PTR(ret);
490a0e265bcSTrond Myklebust 		goto try_again;
491a0e265bcSTrond Myklebust 	}
492bd37d6fcSTrond Myklebust 
493bd37d6fcSTrond Myklebust 	/* Ensure that nobody removed the request before we locked it */
494bd37d6fcSTrond Myklebust 	if (head != nfs_page_private_request(page) && !PageSwapCache(page)) {
495bd37d6fcSTrond Myklebust 		nfs_unlock_and_release_request(head);
496bd37d6fcSTrond Myklebust 		goto try_again;
497bd37d6fcSTrond Myklebust 	}
4987c3af975SWeston Andros Adamson 
4991344b7eaSTrond Myklebust 	ret = nfs_page_group_lock(head);
5000671d8f1SMarkus Elfring 	if (ret < 0)
5010671d8f1SMarkus Elfring 		goto release_request;
5027c3af975SWeston Andros Adamson 
5037c3af975SWeston Andros Adamson 	/* lock each request in the page group */
504a0e265bcSTrond Myklebust 	total_bytes = head->wb_bytes;
505a0e265bcSTrond Myklebust 	for (subreq = head->wb_this_page; subreq != head;
506a0e265bcSTrond Myklebust 			subreq = subreq->wb_this_page) {
50774a6d4b5STrond Myklebust 
5081bd5d6d0STrond Myklebust 		if (!kref_get_unless_zero(&subreq->wb_kref)) {
5091bd5d6d0STrond Myklebust 			if (subreq->wb_offset == head->wb_offset + total_bytes)
5101bd5d6d0STrond Myklebust 				total_bytes += subreq->wb_bytes;
5115b2b5187STrond Myklebust 			continue;
5121bd5d6d0STrond Myklebust 		}
5131bd5d6d0STrond Myklebust 
51474a6d4b5STrond Myklebust 		while (!nfs_lock_request(subreq)) {
515b5bab9bfSTrond Myklebust 			/*
51674a6d4b5STrond Myklebust 			 * Unlock page to allow nfs_page_group_sync_on_bit()
51774a6d4b5STrond Myklebust 			 * to succeed
518b5bab9bfSTrond Myklebust 			 */
51974a6d4b5STrond Myklebust 			nfs_page_group_unlock(head);
52074a6d4b5STrond Myklebust 			ret = nfs_wait_on_request(subreq);
52174a6d4b5STrond Myklebust 			if (!ret)
5221344b7eaSTrond Myklebust 				ret = nfs_page_group_lock(head);
52374a6d4b5STrond Myklebust 			if (ret < 0) {
52474a6d4b5STrond Myklebust 				nfs_unroll_locks(inode, head, subreq);
5255b2b5187STrond Myklebust 				nfs_release_request(subreq);
5260671d8f1SMarkus Elfring 				goto release_request;
527d4581383SWeston Andros Adamson 			}
52874a6d4b5STrond Myklebust 		}
529e14bebf6STrond Myklebust 		/*
530e14bebf6STrond Myklebust 		 * Subrequests are always contiguous, non overlapping
531e14bebf6STrond Myklebust 		 * and in order - but may be repeated (mirrored writes).
532e14bebf6STrond Myklebust 		 */
533e14bebf6STrond Myklebust 		if (subreq->wb_offset == (head->wb_offset + total_bytes)) {
534e14bebf6STrond Myklebust 			/* keep track of how many bytes this group covers */
535e14bebf6STrond Myklebust 			total_bytes += subreq->wb_bytes;
536e14bebf6STrond Myklebust 		} else if (WARN_ON_ONCE(subreq->wb_offset < head->wb_offset ||
537e14bebf6STrond Myklebust 			    ((subreq->wb_offset + subreq->wb_bytes) >
538e14bebf6STrond Myklebust 			     (head->wb_offset + total_bytes)))) {
5398b77484fSTrond Myklebust 			nfs_page_group_unlock(head);
54074a6d4b5STrond Myklebust 			nfs_unroll_locks(inode, head, subreq);
5415b2b5187STrond Myklebust 			nfs_unlock_and_release_request(subreq);
5420671d8f1SMarkus Elfring 			ret = -EIO;
5430671d8f1SMarkus Elfring 			goto release_request;
544e14bebf6STrond Myklebust 		}
545a0e265bcSTrond Myklebust 	}
546d4581383SWeston Andros Adamson 
547d4581383SWeston Andros Adamson 	/* Now that all requests are locked, make sure they aren't on any list.
548d4581383SWeston Andros Adamson 	 * Commit list removal accounting is done after locks are dropped */
549d4581383SWeston Andros Adamson 	subreq = head;
550d4581383SWeston Andros Adamson 	do {
551411a99adSWeston Andros Adamson 		nfs_clear_request_commit(subreq);
552d4581383SWeston Andros Adamson 		subreq = subreq->wb_this_page;
553d4581383SWeston Andros Adamson 	} while (subreq != head);
554d4581383SWeston Andros Adamson 
555d4581383SWeston Andros Adamson 	/* unlink subrequests from head, destroy them later */
556d4581383SWeston Andros Adamson 	if (head->wb_this_page != head) {
557d4581383SWeston Andros Adamson 		/* destroy list will be terminated by head */
558d4581383SWeston Andros Adamson 		destroy_list = head->wb_this_page;
559d4581383SWeston Andros Adamson 		head->wb_this_page = head;
560d4581383SWeston Andros Adamson 
561d4581383SWeston Andros Adamson 		/* change head request to cover whole range that
562d4581383SWeston Andros Adamson 		 * the former page group covered */
563d4581383SWeston Andros Adamson 		head->wb_bytes = total_bytes;
564d4581383SWeston Andros Adamson 	}
565d4581383SWeston Andros Adamson 
566b66aaa8dSTrond Myklebust 	/* Postpone destruction of this request */
567b66aaa8dSTrond Myklebust 	if (test_and_clear_bit(PG_REMOVE, &head->wb_flags)) {
568b66aaa8dSTrond Myklebust 		set_bit(PG_INODE_REF, &head->wb_flags);
569b66aaa8dSTrond Myklebust 		kref_get(&head->wb_kref);
570a6b6d5b8STrond Myklebust 		atomic_long_inc(&NFS_I(inode)->nrequests);
571b66aaa8dSTrond Myklebust 	}
572b66aaa8dSTrond Myklebust 
573d4581383SWeston Andros Adamson 	nfs_page_group_unlock(head);
574d4581383SWeston Andros Adamson 
575b66aaa8dSTrond Myklebust 	nfs_destroy_unlinked_subrequests(destroy_list, head, inode);
576d4581383SWeston Andros Adamson 
577b5bab9bfSTrond Myklebust 	/* Did we lose a race with nfs_inode_remove_request()? */
578b5bab9bfSTrond Myklebust 	if (!(PagePrivate(page) || PageSwapCache(page))) {
579b5bab9bfSTrond Myklebust 		nfs_unlock_and_release_request(head);
580b5bab9bfSTrond Myklebust 		return NULL;
581b5bab9bfSTrond Myklebust 	}
582b5bab9bfSTrond Myklebust 
583bd37d6fcSTrond Myklebust 	/* still holds ref on head from nfs_page_find_head_request
584d4581383SWeston Andros Adamson 	 * and still has lock on head from lock loop */
585d4581383SWeston Andros Adamson 	return head;
5860671d8f1SMarkus Elfring 
5870671d8f1SMarkus Elfring release_request:
5880671d8f1SMarkus Elfring 	nfs_unlock_and_release_request(head);
5890671d8f1SMarkus Elfring 	return ERR_PTR(ret);
590612c9384STrond Myklebust }
591074cc1deSTrond Myklebust 
5926fbda89bSTrond Myklebust static void nfs_write_error(struct nfs_page *req, int error)
5930bcbf039SPeng Tao {
5946fbda89bSTrond Myklebust 	nfs_mapping_set_error(req->wb_page, error);
5950bcbf039SPeng Tao 	nfs_end_page_writeback(req);
5961f84ccdfSFred Isaman 	nfs_release_request(req);
5970bcbf039SPeng Tao }
5980bcbf039SPeng Tao 
599a6598813STrond Myklebust static bool
600a6598813STrond Myklebust nfs_error_is_fatal_on_server(int err)
601a6598813STrond Myklebust {
602a6598813STrond Myklebust 	switch (err) {
603a6598813STrond Myklebust 	case 0:
604a6598813STrond Myklebust 	case -ERESTARTSYS:
605a6598813STrond Myklebust 	case -EINTR:
606a6598813STrond Myklebust 		return false;
607a6598813STrond Myklebust 	}
608a6598813STrond Myklebust 	return nfs_error_is_fatal(err);
609e261f51fSTrond Myklebust }
610074cc1deSTrond Myklebust 
611074cc1deSTrond Myklebust /*
612074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
613074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
614074cc1deSTrond Myklebust  */
615074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
6166d17d653STrond Myklebust 				struct page *page)
617074cc1deSTrond Myklebust {
6186fbda89bSTrond Myklebust 	struct address_space *mapping;
619074cc1deSTrond Myklebust 	struct nfs_page *req;
620074cc1deSTrond Myklebust 	int ret = 0;
621074cc1deSTrond Myklebust 
6226d17d653STrond Myklebust 	req = nfs_lock_and_join_requests(page);
623074cc1deSTrond Myklebust 	if (!req)
624074cc1deSTrond Myklebust 		goto out;
625074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
626074cc1deSTrond Myklebust 	if (IS_ERR(req))
627074cc1deSTrond Myklebust 		goto out;
628074cc1deSTrond Myklebust 
629deed85e7STrond Myklebust 	nfs_set_page_writeback(page);
630deed85e7STrond Myklebust 	WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
631074cc1deSTrond Myklebust 
632a6598813STrond Myklebust 	/* If there is a fatal error that covers this write, just exit */
6336fbda89bSTrond Myklebust 	ret = 0;
6346fbda89bSTrond Myklebust 	mapping = page_file_mapping(page);
6356fbda89bSTrond Myklebust 	if (test_bit(AS_ENOSPC, &mapping->flags) ||
6366fbda89bSTrond Myklebust 	    test_bit(AS_EIO, &mapping->flags))
637a6598813STrond Myklebust 		goto out_launder;
638a6598813STrond Myklebust 
639f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
640074cc1deSTrond Myklebust 		ret = pgio->pg_error;
6410bcbf039SPeng Tao 		/*
642c373fff7STrond Myklebust 		 * Remove the problematic req upon fatal errors on the server
6430bcbf039SPeng Tao 		 */
6440bcbf039SPeng Tao 		if (nfs_error_is_fatal(ret)) {
645c373fff7STrond Myklebust 			if (nfs_error_is_fatal_on_server(ret))
646a6598813STrond Myklebust 				goto out_launder;
6478fc75bedSTrond Myklebust 		} else
6480bcbf039SPeng Tao 			ret = -EAGAIN;
6498fc75bedSTrond Myklebust 		nfs_redirty_request(req);
65040f90271STrond Myklebust 	} else
65140f90271STrond Myklebust 		nfs_add_stats(page_file_mapping(page)->host,
65240f90271STrond Myklebust 				NFSIOS_WRITEPAGES, 1);
653074cc1deSTrond Myklebust out:
654074cc1deSTrond Myklebust 	return ret;
655a6598813STrond Myklebust out_launder:
6566fbda89bSTrond Myklebust 	nfs_write_error(req, ret);
65714bebe3cSTrond Myklebust 	return 0;
658e261f51fSTrond Myklebust }
659e261f51fSTrond Myklebust 
660d6c843b9SPeng Tao static int nfs_do_writepage(struct page *page, struct writeback_control *wbc,
661c373fff7STrond Myklebust 			    struct nfs_pageio_descriptor *pgio)
662f758c885STrond Myklebust {
663cfb506e1STrond Myklebust 	int ret;
664f758c885STrond Myklebust 
6658cd79788SHuang Ying 	nfs_pageio_cond_complete(pgio, page_index(page));
6666d17d653STrond Myklebust 	ret = nfs_page_async_flush(pgio, page);
667cfb506e1STrond Myklebust 	if (ret == -EAGAIN) {
668cfb506e1STrond Myklebust 		redirty_page_for_writepage(wbc, page);
669cfb506e1STrond Myklebust 		ret = 0;
670cfb506e1STrond Myklebust 	}
671cfb506e1STrond Myklebust 	return ret;
672f758c885STrond Myklebust }
673f758c885STrond Myklebust 
674e261f51fSTrond Myklebust /*
6751da177e4SLinus Torvalds  * Write an mmapped page to the server.
6761da177e4SLinus Torvalds  */
677d6c843b9SPeng Tao static int nfs_writepage_locked(struct page *page,
678c373fff7STrond Myklebust 				struct writeback_control *wbc)
6791da177e4SLinus Torvalds {
680f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
68140f90271STrond Myklebust 	struct inode *inode = page_file_mapping(page)->host;
682e261f51fSTrond Myklebust 	int err;
6831da177e4SLinus Torvalds 
68440f90271STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
685811ed92eSTrond Myklebust 	nfs_pageio_init_write(&pgio, inode, 0,
686a20c93e3SChristoph Hellwig 				false, &nfs_async_write_completion_ops);
687c373fff7STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
688f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
689f758c885STrond Myklebust 	if (err < 0)
6904d770ccfSTrond Myklebust 		return err;
691f758c885STrond Myklebust 	if (pgio.pg_error < 0)
692f758c885STrond Myklebust 		return pgio.pg_error;
693f758c885STrond Myklebust 	return 0;
6944d770ccfSTrond Myklebust }
6954d770ccfSTrond Myklebust 
6964d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
6974d770ccfSTrond Myklebust {
698f758c885STrond Myklebust 	int ret;
6994d770ccfSTrond Myklebust 
700c373fff7STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
7011da177e4SLinus Torvalds 	unlock_page(page);
702f758c885STrond Myklebust 	return ret;
703f758c885STrond Myklebust }
704f758c885STrond Myklebust 
705f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
706f758c885STrond Myklebust {
707f758c885STrond Myklebust 	int ret;
708f758c885STrond Myklebust 
709c373fff7STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
710f758c885STrond Myklebust 	unlock_page(page);
711f758c885STrond Myklebust 	return ret;
7121da177e4SLinus Torvalds }
7131da177e4SLinus Torvalds 
714919e3bd9STrond Myklebust static void nfs_io_completion_commit(void *inode)
715919e3bd9STrond Myklebust {
716919e3bd9STrond Myklebust 	nfs_commit_inode(inode, 0);
717919e3bd9STrond Myklebust }
718919e3bd9STrond Myklebust 
7191da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
7201da177e4SLinus Torvalds {
7211da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
722c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
723875bc3fbSTrond Myklebust 	struct nfs_io_completion *ioc;
7241da177e4SLinus Torvalds 	int err;
7251da177e4SLinus Torvalds 
72691d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
72791d5b470SChuck Lever 
728*2b17d725STrond Myklebust 	ioc = nfs_io_completion_alloc(GFP_KERNEL);
729919e3bd9STrond Myklebust 	if (ioc)
730919e3bd9STrond Myklebust 		nfs_io_completion_init(ioc, nfs_io_completion_commit, inode);
731919e3bd9STrond Myklebust 
732a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false,
733a20c93e3SChristoph Hellwig 				&nfs_async_write_completion_ops);
734919e3bd9STrond Myklebust 	pgio.pg_io_completion = ioc;
735f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
736c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
737919e3bd9STrond Myklebust 	nfs_io_completion_put(ioc);
73872cb77f4STrond Myklebust 
739f758c885STrond Myklebust 	if (err < 0)
74072cb77f4STrond Myklebust 		goto out_err;
74172cb77f4STrond Myklebust 	err = pgio.pg_error;
74272cb77f4STrond Myklebust 	if (err < 0)
74372cb77f4STrond Myklebust 		goto out_err;
744c63c7b05STrond Myklebust 	return 0;
74572cb77f4STrond Myklebust out_err:
74672cb77f4STrond Myklebust 	return err;
7471da177e4SLinus Torvalds }
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds /*
7501da177e4SLinus Torvalds  * Insert a write request into an inode
7511da177e4SLinus Torvalds  */
752d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
7531da177e4SLinus Torvalds {
7544b9bb25bSTrond Myklebust 	struct address_space *mapping = page_file_mapping(req->wb_page);
7551da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
756e7d39069STrond Myklebust 
7572bfc6e56SWeston Andros Adamson 	WARN_ON_ONCE(req->wb_this_page != req);
7582bfc6e56SWeston Andros Adamson 
759e7d39069STrond Myklebust 	/* Lock the request! */
7607ad84aa9STrond Myklebust 	nfs_lock_request(req);
761e7d39069STrond Myklebust 
76229418aa4SMel Gorman 	/*
76329418aa4SMel Gorman 	 * Swap-space should not get truncated. Hence no need to plug the race
76429418aa4SMel Gorman 	 * with invalidate/truncate.
76529418aa4SMel Gorman 	 */
7664b9bb25bSTrond Myklebust 	spin_lock(&mapping->private_lock);
7674b9bb25bSTrond Myklebust 	if (!nfs_have_writebacks(inode) &&
7681eb5d98fSJeff Layton 	    NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
7691eb5d98fSJeff Layton 		inode_inc_iversion_raw(inode);
77029418aa4SMel Gorman 	if (likely(!PageSwapCache(req->wb_page))) {
7712df485a7STrond Myklebust 		set_bit(PG_MAPPED, &req->wb_flags);
772deb7d638STrond Myklebust 		SetPagePrivate(req->wb_page);
773277459d2STrond Myklebust 		set_page_private(req->wb_page, (unsigned long)req);
77429418aa4SMel Gorman 	}
7754b9bb25bSTrond Myklebust 	spin_unlock(&mapping->private_lock);
776a6b6d5b8STrond Myklebust 	atomic_long_inc(&nfsi->nrequests);
77717089a29SWeston Andros Adamson 	/* this a head request for a page group - mark it as having an
778cb1410c7SWeston Andros Adamson 	 * extra reference so sub groups can follow suit.
779cb1410c7SWeston Andros Adamson 	 * This flag also informs pgio layer when to bump nrequests when
780cb1410c7SWeston Andros Adamson 	 * adding subrequests. */
78117089a29SWeston Andros Adamson 	WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags));
782c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
7831da177e4SLinus Torvalds }
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds /*
78689a09141SPeter Zijlstra  * Remove a write request from an inode
7871da177e4SLinus Torvalds  */
7881da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
7891da177e4SLinus Torvalds {
7904b9bb25bSTrond Myklebust 	struct address_space *mapping = page_file_mapping(req->wb_page);
7914b9bb25bSTrond Myklebust 	struct inode *inode = mapping->host;
7921da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
79320633f04SWeston Andros Adamson 	struct nfs_page *head;
79420633f04SWeston Andros Adamson 
795a6b6d5b8STrond Myklebust 	atomic_long_dec(&nfsi->nrequests);
79620633f04SWeston Andros Adamson 	if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) {
79720633f04SWeston Andros Adamson 		head = req->wb_head;
7981da177e4SLinus Torvalds 
7994b9bb25bSTrond Myklebust 		spin_lock(&mapping->private_lock);
80067911c8fSAnna Schumaker 		if (likely(head->wb_page && !PageSwapCache(head->wb_page))) {
80120633f04SWeston Andros Adamson 			set_page_private(head->wb_page, 0);
80220633f04SWeston Andros Adamson 			ClearPagePrivate(head->wb_page);
80320633f04SWeston Andros Adamson 			clear_bit(PG_MAPPED, &head->wb_flags);
80429418aa4SMel Gorman 		}
8054b9bb25bSTrond Myklebust 		spin_unlock(&mapping->private_lock);
80620633f04SWeston Andros Adamson 	}
80717089a29SWeston Andros Adamson 
80817089a29SWeston Andros Adamson 	if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags))
8091da177e4SLinus Torvalds 		nfs_release_request(req);
8101da177e4SLinus Torvalds }
8111da177e4SLinus Torvalds 
81261822ab5STrond Myklebust static void
8136d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
81461822ab5STrond Myklebust {
81567911c8fSAnna Schumaker 	if (req->wb_page)
81661822ab5STrond Myklebust 		__set_page_dirty_nobuffers(req->wb_page);
81761822ab5STrond Myklebust }
81861822ab5STrond Myklebust 
8193a3908c8STrond Myklebust /*
8203a3908c8STrond Myklebust  * nfs_page_search_commits_for_head_request_locked
8213a3908c8STrond Myklebust  *
8223a3908c8STrond Myklebust  * Search through commit lists on @inode for the head request for @page.
8233a3908c8STrond Myklebust  * Must be called while holding the inode (which is cinfo) lock.
8243a3908c8STrond Myklebust  *
8253a3908c8STrond Myklebust  * Returns the head request if found, or NULL if not found.
8263a3908c8STrond Myklebust  */
8273a3908c8STrond Myklebust static struct nfs_page *
8283a3908c8STrond Myklebust nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
8293a3908c8STrond Myklebust 						struct page *page)
8303a3908c8STrond Myklebust {
8313a3908c8STrond Myklebust 	struct nfs_page *freq, *t;
8323a3908c8STrond Myklebust 	struct nfs_commit_info cinfo;
8333a3908c8STrond Myklebust 	struct inode *inode = &nfsi->vfs_inode;
8343a3908c8STrond Myklebust 
8353a3908c8STrond Myklebust 	nfs_init_cinfo_from_inode(&cinfo, inode);
8363a3908c8STrond Myklebust 
8373a3908c8STrond Myklebust 	/* search through pnfs commit lists */
8383a3908c8STrond Myklebust 	freq = pnfs_search_commit_reqs(inode, &cinfo, page);
8393a3908c8STrond Myklebust 	if (freq)
8403a3908c8STrond Myklebust 		return freq->wb_head;
8413a3908c8STrond Myklebust 
8423a3908c8STrond Myklebust 	/* Linearly search the commit list for the correct request */
8433a3908c8STrond Myklebust 	list_for_each_entry_safe(freq, t, &cinfo.mds->list, wb_list) {
8443a3908c8STrond Myklebust 		if (freq->wb_page == page)
8453a3908c8STrond Myklebust 			return freq->wb_head;
8463a3908c8STrond Myklebust 	}
8473a3908c8STrond Myklebust 
8483a3908c8STrond Myklebust 	return NULL;
8493a3908c8STrond Myklebust }
8503a3908c8STrond Myklebust 
8518dd37758STrond Myklebust /**
85286d80f97STrond Myklebust  * nfs_request_add_commit_list_locked - add request to a commit list
85386d80f97STrond Myklebust  * @req: pointer to a struct nfs_page
85486d80f97STrond Myklebust  * @dst: commit list head
85586d80f97STrond Myklebust  * @cinfo: holds list lock and accounting info
85686d80f97STrond Myklebust  *
85786d80f97STrond Myklebust  * This sets the PG_CLEAN bit, updates the cinfo count of
85886d80f97STrond Myklebust  * number of outstanding requests requiring a commit as well as
85986d80f97STrond Myklebust  * the MM page stats.
86086d80f97STrond Myklebust  *
861e824f99aSTrond Myklebust  * The caller must hold NFS_I(cinfo->inode)->commit_mutex, and the
862e824f99aSTrond Myklebust  * nfs_page lock.
86386d80f97STrond Myklebust  */
86486d80f97STrond Myklebust void
86586d80f97STrond Myklebust nfs_request_add_commit_list_locked(struct nfs_page *req, struct list_head *dst,
86686d80f97STrond Myklebust 			    struct nfs_commit_info *cinfo)
86786d80f97STrond Myklebust {
86886d80f97STrond Myklebust 	set_bit(PG_CLEAN, &req->wb_flags);
86986d80f97STrond Myklebust 	nfs_list_add_request(req, dst);
8705cb953d4STrond Myklebust 	atomic_long_inc(&cinfo->mds->ncommit);
87186d80f97STrond Myklebust }
87286d80f97STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked);
87386d80f97STrond Myklebust 
87486d80f97STrond Myklebust /**
8758dd37758STrond Myklebust  * nfs_request_add_commit_list - add request to a commit list
8768dd37758STrond Myklebust  * @req: pointer to a struct nfs_page
877ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
8788dd37758STrond Myklebust  *
879ea2cf228SFred Isaman  * This sets the PG_CLEAN bit, updates the cinfo count of
8808dd37758STrond Myklebust  * number of outstanding requests requiring a commit as well as
8818dd37758STrond Myklebust  * the MM page stats.
8828dd37758STrond Myklebust  *
883ea2cf228SFred Isaman  * The caller must _not_ hold the cinfo->lock, but must be
8848dd37758STrond Myklebust  * holding the nfs_page lock.
8858dd37758STrond Myklebust  */
8868dd37758STrond Myklebust void
8876272dcc6SAnna Schumaker nfs_request_add_commit_list(struct nfs_page *req, struct nfs_commit_info *cinfo)
8888dd37758STrond Myklebust {
889e824f99aSTrond Myklebust 	mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
8906272dcc6SAnna Schumaker 	nfs_request_add_commit_list_locked(req, &cinfo->mds->list, cinfo);
891e824f99aSTrond Myklebust 	mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
89267911c8fSAnna Schumaker 	if (req->wb_page)
89386d80f97STrond Myklebust 		nfs_mark_page_unstable(req->wb_page, cinfo);
8948dd37758STrond Myklebust }
8958dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
8968dd37758STrond Myklebust 
8978dd37758STrond Myklebust /**
8988dd37758STrond Myklebust  * nfs_request_remove_commit_list - Remove request from a commit list
8998dd37758STrond Myklebust  * @req: pointer to a nfs_page
900ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
9018dd37758STrond Myklebust  *
902ea2cf228SFred Isaman  * This clears the PG_CLEAN bit, and updates the cinfo's count of
9038dd37758STrond Myklebust  * number of outstanding requests requiring a commit
9048dd37758STrond Myklebust  * It does not update the MM page stats.
9058dd37758STrond Myklebust  *
906ea2cf228SFred Isaman  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
9078dd37758STrond Myklebust  */
9088dd37758STrond Myklebust void
909ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req,
910ea2cf228SFred Isaman 			       struct nfs_commit_info *cinfo)
9118dd37758STrond Myklebust {
9128dd37758STrond Myklebust 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
9138dd37758STrond Myklebust 		return;
9148dd37758STrond Myklebust 	nfs_list_remove_request(req);
9155cb953d4STrond Myklebust 	atomic_long_dec(&cinfo->mds->ncommit);
9168dd37758STrond Myklebust }
9178dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
9188dd37758STrond Myklebust 
919ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
920ea2cf228SFred Isaman 				      struct inode *inode)
921ea2cf228SFred Isaman {
922fe238e60SDave Wysochanski 	cinfo->inode = inode;
923ea2cf228SFred Isaman 	cinfo->mds = &NFS_I(inode)->commit_info;
924ea2cf228SFred Isaman 	cinfo->ds = pnfs_get_ds_info(inode);
925b359f9d0SFred Isaman 	cinfo->dreq = NULL;
926f453a54aSFred Isaman 	cinfo->completion_ops = &nfs_commit_completion_ops;
927ea2cf228SFred Isaman }
928ea2cf228SFred Isaman 
929ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
930ea2cf228SFred Isaman 		    struct inode *inode,
931ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq)
932ea2cf228SFred Isaman {
9331763da12SFred Isaman 	if (dreq)
9341763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
9351763da12SFred Isaman 	else
936ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(cinfo, inode);
937ea2cf228SFred Isaman }
938ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo);
9398dd37758STrond Myklebust 
9401da177e4SLinus Torvalds /*
9411da177e4SLinus Torvalds  * Add a request to the inode's commit list.
9421da177e4SLinus Torvalds  */
9431763da12SFred Isaman void
944ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
945b57ff130SWeston Andros Adamson 			struct nfs_commit_info *cinfo, u32 ds_commit_idx)
9461da177e4SLinus Torvalds {
947b57ff130SWeston Andros Adamson 	if (pnfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx))
9488dd37758STrond Myklebust 		return;
9496272dcc6SAnna Schumaker 	nfs_request_add_commit_list(req, cinfo);
9501da177e4SLinus Torvalds }
9518e821cadSTrond Myklebust 
952d6d6dc7cSFred Isaman static void
953d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page)
954e468bae9STrond Myklebust {
95511fb9989SMel Gorman 	dec_node_page_state(page, NR_UNSTABLE_NFS);
95693f78d88STejun Heo 	dec_wb_stat(&inode_to_bdi(page_file_mapping(page)->host)->wb,
95793f78d88STejun Heo 		    WB_RECLAIMABLE);
958e468bae9STrond Myklebust }
959d6d6dc7cSFred Isaman 
960b5bab9bfSTrond Myklebust /* Called holding the request lock on @req */
9618dd37758STrond Myklebust static void
962d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req)
963d6d6dc7cSFred Isaman {
9648dd37758STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
9659fcd5960STrond Myklebust 		struct nfs_open_context *ctx = nfs_req_openctx(req);
9669fcd5960STrond Myklebust 		struct inode *inode = d_inode(ctx->dentry);
967ea2cf228SFred Isaman 		struct nfs_commit_info cinfo;
968d6d6dc7cSFred Isaman 
969ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(&cinfo, inode);
970e824f99aSTrond Myklebust 		mutex_lock(&NFS_I(inode)->commit_mutex);
971ea2cf228SFred Isaman 		if (!pnfs_clear_request_commit(req, &cinfo)) {
972ea2cf228SFred Isaman 			nfs_request_remove_commit_list(req, &cinfo);
973d6d6dc7cSFred Isaman 		}
974e824f99aSTrond Myklebust 		mutex_unlock(&NFS_I(inode)->commit_mutex);
9758dd37758STrond Myklebust 		nfs_clear_page_commit(req->wb_page);
9768dd37758STrond Myklebust 	}
977e468bae9STrond Myklebust }
978e468bae9STrond Myklebust 
979d45f60c6SWeston Andros Adamson int nfs_write_need_commit(struct nfs_pgio_header *hdr)
9808e821cadSTrond Myklebust {
981c65e6254SWeston Andros Adamson 	if (hdr->verf.committed == NFS_DATA_SYNC)
982d45f60c6SWeston Andros Adamson 		return hdr->lseg == NULL;
983c65e6254SWeston Andros Adamson 	return hdr->verf.committed != NFS_FILE_SYNC;
9848e821cadSTrond Myklebust }
9858e821cadSTrond Myklebust 
986919e3bd9STrond Myklebust static void nfs_async_write_init(struct nfs_pgio_header *hdr)
987919e3bd9STrond Myklebust {
988919e3bd9STrond Myklebust 	nfs_io_completion_get(hdr->io_completion);
989919e3bd9STrond Myklebust }
990919e3bd9STrond Myklebust 
991061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
9926c75dc0dSFred Isaman {
993ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
9946c75dc0dSFred Isaman 	unsigned long bytes = 0;
9956c75dc0dSFred Isaman 
9966c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
9976c75dc0dSFred Isaman 		goto out;
998ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
9996c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
10006c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
10016c75dc0dSFred Isaman 
10026c75dc0dSFred Isaman 		bytes += req->wb_bytes;
10036c75dc0dSFred Isaman 		nfs_list_remove_request(req);
10046c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
10056c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
1006d2ceb7e5SBenjamin Coddington 			nfs_set_pageerror(page_file_mapping(req->wb_page));
10076fbda89bSTrond Myklebust 			nfs_mapping_set_error(req->wb_page, hdr->error);
10086c75dc0dSFred Isaman 			goto remove_req;
10096c75dc0dSFred Isaman 		}
1010c65e6254SWeston Andros Adamson 		if (nfs_write_need_commit(hdr)) {
101133344e0fSTrond Myklebust 			/* Reset wb_nio, since the write was successful. */
101233344e0fSTrond Myklebust 			req->wb_nio = 0;
1013f79d06f5SAnna Schumaker 			memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
1014b57ff130SWeston Andros Adamson 			nfs_mark_request_commit(req, hdr->lseg, &cinfo,
1015a7d42ddbSWeston Andros Adamson 				hdr->pgio_mirror_idx);
10166c75dc0dSFred Isaman 			goto next;
10176c75dc0dSFred Isaman 		}
10186c75dc0dSFred Isaman remove_req:
10196c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
10206c75dc0dSFred Isaman next:
102120633f04SWeston Andros Adamson 		nfs_end_page_writeback(req);
10223aff4ebbSTrond Myklebust 		nfs_release_request(req);
10236c75dc0dSFred Isaman 	}
10246c75dc0dSFred Isaman out:
1025919e3bd9STrond Myklebust 	nfs_io_completion_put(hdr->io_completion);
10266c75dc0dSFred Isaman 	hdr->release(hdr);
10276c75dc0dSFred Isaman }
10286c75dc0dSFred Isaman 
1029ce59515cSAnna Schumaker unsigned long
1030ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
1031fb8a1f11STrond Myklebust {
10325cb953d4STrond Myklebust 	return atomic_long_read(&cinfo->mds->ncommit);
1033fb8a1f11STrond Myklebust }
1034fb8a1f11STrond Myklebust 
1035e824f99aSTrond Myklebust /* NFS_I(cinfo->inode)->commit_mutex held by caller */
10361763da12SFred Isaman int
1037ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
1038ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
1039d6d6dc7cSFred Isaman {
1040137da553STrond Myklebust 	struct nfs_page *req, *tmp;
1041d6d6dc7cSFred Isaman 	int ret = 0;
1042d6d6dc7cSFred Isaman 
1043137da553STrond Myklebust restart:
1044137da553STrond Myklebust 	list_for_each_entry_safe(req, tmp, src, wb_list) {
10457ad84aa9STrond Myklebust 		kref_get(&req->wb_kref);
10462ce209c4STrond Myklebust 		if (!nfs_lock_request(req)) {
10472ce209c4STrond Myklebust 			int status;
1048137da553STrond Myklebust 
1049137da553STrond Myklebust 			/* Prevent deadlock with nfs_lock_and_join_requests */
1050137da553STrond Myklebust 			if (!list_empty(dst)) {
1051137da553STrond Myklebust 				nfs_release_request(req);
1052137da553STrond Myklebust 				continue;
1053137da553STrond Myklebust 			}
1054137da553STrond Myklebust 			/* Ensure we make progress to prevent livelock */
10552ce209c4STrond Myklebust 			mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
10562ce209c4STrond Myklebust 			status = nfs_wait_on_request(req);
10572ce209c4STrond Myklebust 			nfs_release_request(req);
10582ce209c4STrond Myklebust 			mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
10592ce209c4STrond Myklebust 			if (status < 0)
10602ce209c4STrond Myklebust 				break;
1061137da553STrond Myklebust 			goto restart;
10622ce209c4STrond Myklebust 		}
1063ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
10645d2a9d9dSTrond Myklebust 		clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
10658dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
1066d6d6dc7cSFred Isaman 		ret++;
10671763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
1068d6d6dc7cSFred Isaman 			break;
1069e824f99aSTrond Myklebust 		cond_resched();
1070d6d6dc7cSFred Isaman 	}
1071d6d6dc7cSFred Isaman 	return ret;
1072d6d6dc7cSFred Isaman }
10735d2a9d9dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_scan_commit_list);
1074d6d6dc7cSFred Isaman 
10751da177e4SLinus Torvalds /*
10761da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
10771da177e4SLinus Torvalds  * @inode: NFS inode to scan
1078ea2cf228SFred Isaman  * @dst: mds destination list
1079ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
10801da177e4SLinus Torvalds  *
10811da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
10821da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
10831da177e4SLinus Torvalds  */
10841763da12SFred Isaman int
1085ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
1086ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
10871da177e4SLinus Torvalds {
1088d6d6dc7cSFred Isaman 	int ret = 0;
1089fb8a1f11STrond Myklebust 
10905cb953d4STrond Myklebust 	if (!atomic_long_read(&cinfo->mds->ncommit))
10915cb953d4STrond Myklebust 		return 0;
1092e824f99aSTrond Myklebust 	mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
10935cb953d4STrond Myklebust 	if (atomic_long_read(&cinfo->mds->ncommit) > 0) {
10948dd37758STrond Myklebust 		const int max = INT_MAX;
1095d6d6dc7cSFred Isaman 
1096ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
1097ea2cf228SFred Isaman 					   cinfo, max);
1098ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
1099d6d6dc7cSFred Isaman 	}
1100e824f99aSTrond Myklebust 	mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
1101ff778d02STrond Myklebust 	return ret;
11021da177e4SLinus Torvalds }
1103d6d6dc7cSFred Isaman 
11041da177e4SLinus Torvalds /*
1105e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
1106e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
11071da177e4SLinus Torvalds  *
1108e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
1109e7d39069STrond Myklebust  * to disk.
11101da177e4SLinus Torvalds  */
1111e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
1112e7d39069STrond Myklebust 		struct page *page,
1113e7d39069STrond Myklebust 		unsigned int offset,
1114e7d39069STrond Myklebust 		unsigned int bytes)
11151da177e4SLinus Torvalds {
1116e7d39069STrond Myklebust 	struct nfs_page *req;
1117e7d39069STrond Myklebust 	unsigned int rqend;
1118e7d39069STrond Myklebust 	unsigned int end;
11191da177e4SLinus Torvalds 	int error;
1120277459d2STrond Myklebust 
1121e7d39069STrond Myklebust 	end = offset + bytes;
1122e7d39069STrond Myklebust 
1123f6032f21STrond Myklebust 	req = nfs_lock_and_join_requests(page);
1124f6032f21STrond Myklebust 	if (IS_ERR_OR_NULL(req))
1125f6032f21STrond Myklebust 		return req;
11262bfc6e56SWeston Andros Adamson 
1127e7d39069STrond Myklebust 	rqend = req->wb_offset + req->wb_bytes;
1128e7d39069STrond Myklebust 	/*
1129e7d39069STrond Myklebust 	 * Tell the caller to flush out the request if
1130e7d39069STrond Myklebust 	 * the offsets are non-contiguous.
1131e7d39069STrond Myklebust 	 * Note: nfs_flush_incompatible() will already
1132e7d39069STrond Myklebust 	 * have flushed out requests having wrong owners.
1133e7d39069STrond Myklebust 	 */
1134f6032f21STrond Myklebust 	if (offset > rqend || end < req->wb_offset)
1135e7d39069STrond Myklebust 		goto out_flushme;
1136e7d39069STrond Myklebust 
11371da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
11381da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
11391da177e4SLinus Torvalds 		req->wb_offset = offset;
11401da177e4SLinus Torvalds 		req->wb_pgbase = offset;
11411da177e4SLinus Torvalds 	}
11421da177e4SLinus Torvalds 	if (end > rqend)
11431da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
1144e7d39069STrond Myklebust 	else
1145e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
114633344e0fSTrond Myklebust 	req->wb_nio = 0;
1147e7d39069STrond Myklebust 	return req;
1148e7d39069STrond Myklebust out_flushme:
1149f6032f21STrond Myklebust 	/*
1150f6032f21STrond Myklebust 	 * Note: we mark the request dirty here because
1151f6032f21STrond Myklebust 	 * nfs_lock_and_join_requests() cannot preserve
1152f6032f21STrond Myklebust 	 * commit flags, so we have to replay the write.
1153f6032f21STrond Myklebust 	 */
1154f6032f21STrond Myklebust 	nfs_mark_request_dirty(req);
1155f6032f21STrond Myklebust 	nfs_unlock_and_release_request(req);
1156e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
1157f6032f21STrond Myklebust 	return (error < 0) ? ERR_PTR(error) : NULL;
1158e7d39069STrond Myklebust }
11591da177e4SLinus Torvalds 
1160e7d39069STrond Myklebust /*
1161e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
1162e7d39069STrond Myklebust  *
1163e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
1164e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
1165e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
1166e7d39069STrond Myklebust  */
1167e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
1168e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
1169e7d39069STrond Myklebust {
1170d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
1171e7d39069STrond Myklebust 	struct nfs_page	*req;
1172e7d39069STrond Myklebust 
1173e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
1174e7d39069STrond Myklebust 	if (req != NULL)
1175e7d39069STrond Myklebust 		goto out;
117628b1d3f5STrond Myklebust 	req = nfs_create_request(ctx, page, offset, bytes);
1177e7d39069STrond Myklebust 	if (IS_ERR(req))
1178e7d39069STrond Myklebust 		goto out;
1179d6d6dc7cSFred Isaman 	nfs_inode_add_request(inode, req);
1180efc91ed0STrond Myklebust out:
118161e930a9STrond Myklebust 	return req;
11821da177e4SLinus Torvalds }
11831da177e4SLinus Torvalds 
1184e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
1185e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
1186e7d39069STrond Myklebust {
1187e7d39069STrond Myklebust 	struct nfs_page	*req;
1188e7d39069STrond Myklebust 
1189e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
1190e7d39069STrond Myklebust 	if (IS_ERR(req))
1191e7d39069STrond Myklebust 		return PTR_ERR(req);
1192e7d39069STrond Myklebust 	/* Update file length */
1193e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
1194d72ddcbaSWeston Andros Adamson 	nfs_mark_uptodate(req);
1195a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
11961d1afcbcSTrond Myklebust 	nfs_unlock_and_release_request(req);
1197e7d39069STrond Myklebust 	return 0;
1198e7d39069STrond Myklebust }
1199e7d39069STrond Myklebust 
12001da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
12011da177e4SLinus Torvalds {
1202cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
12032a369153STrond Myklebust 	struct nfs_lock_context *l_ctx;
1204bd61e0a9SJeff Layton 	struct file_lock_context *flctx = file_inode(file)->i_flctx;
12051da177e4SLinus Torvalds 	struct nfs_page	*req;
12061a54533eSTrond Myklebust 	int do_flush, status;
12071da177e4SLinus Torvalds 	/*
12081da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
12091da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
12101da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
12111da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
12121da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
12131da177e4SLinus Torvalds 	 * dropped page.
12141da177e4SLinus Torvalds 	 */
12151a54533eSTrond Myklebust 	do {
121684d3a9a9SWeston Andros Adamson 		req = nfs_page_find_head_request(page);
12171a54533eSTrond Myklebust 		if (req == NULL)
12181a54533eSTrond Myklebust 			return 0;
12192a369153STrond Myklebust 		l_ctx = req->wb_lock_context;
1220138a2935STrond Myklebust 		do_flush = req->wb_page != page ||
12219fcd5960STrond Myklebust 			!nfs_match_open_context(nfs_req_openctx(req), ctx);
1222bd61e0a9SJeff Layton 		if (l_ctx && flctx &&
1223bd61e0a9SJeff Layton 		    !(list_empty_careful(&flctx->flc_posix) &&
1224bd61e0a9SJeff Layton 		      list_empty_careful(&flctx->flc_flock))) {
1225d51fdb87SNeilBrown 			do_flush |= l_ctx->lockowner != current->files;
12265263e31eSJeff Layton 		}
12271da177e4SLinus Torvalds 		nfs_release_request(req);
12281a54533eSTrond Myklebust 		if (!do_flush)
12291a54533eSTrond Myklebust 			return 0;
1230d56b4ddfSMel Gorman 		status = nfs_wb_page(page_file_mapping(page)->host, page);
12311a54533eSTrond Myklebust 	} while (status == 0);
12321a54533eSTrond Myklebust 	return status;
12331da177e4SLinus Torvalds }
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds /*
1236dc24826bSAndy Adamson  * Avoid buffered writes when a open context credential's key would
1237dc24826bSAndy Adamson  * expire soon.
1238dc24826bSAndy Adamson  *
1239dc24826bSAndy Adamson  * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
1240dc24826bSAndy Adamson  *
1241dc24826bSAndy Adamson  * Return 0 and set a credential flag which triggers the inode to flush
1242dc24826bSAndy Adamson  * and performs  NFS_FILE_SYNC writes if the key will expired within
1243dc24826bSAndy Adamson  * RPC_KEY_EXPIRE_TIMEO.
1244dc24826bSAndy Adamson  */
1245dc24826bSAndy Adamson int
1246dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode)
1247dc24826bSAndy Adamson {
1248dc24826bSAndy Adamson 	struct nfs_open_context *ctx = nfs_file_open_context(filp);
1249dc24826bSAndy Adamson 
1250ddf529eeSNeilBrown 	if (nfs_ctx_key_to_expire(ctx, inode) &&
1251ddf529eeSNeilBrown 	    !ctx->ll_cred)
1252ddf529eeSNeilBrown 		/* Already expired! */
1253ddf529eeSNeilBrown 		return -EACCES;
1254ddf529eeSNeilBrown 	return 0;
1255dc24826bSAndy Adamson }
1256dc24826bSAndy Adamson 
1257dc24826bSAndy Adamson /*
1258dc24826bSAndy Adamson  * Test if the open context credential key is marked to expire soon.
1259dc24826bSAndy Adamson  */
1260ce52914eSScott Mayhew bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode)
1261dc24826bSAndy Adamson {
1262ce52914eSScott Mayhew 	struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
1263ddf529eeSNeilBrown 	struct rpc_cred *cred = ctx->ll_cred;
1264ddf529eeSNeilBrown 	struct auth_cred acred = {
1265a52458b4SNeilBrown 		.cred = ctx->cred,
1266ddf529eeSNeilBrown 	};
1267ce52914eSScott Mayhew 
1268ddf529eeSNeilBrown 	if (cred && !cred->cr_ops->crmatch(&acred, cred, 0)) {
1269ddf529eeSNeilBrown 		put_rpccred(cred);
1270ddf529eeSNeilBrown 		ctx->ll_cred = NULL;
1271ddf529eeSNeilBrown 		cred = NULL;
1272ddf529eeSNeilBrown 	}
1273ddf529eeSNeilBrown 	if (!cred)
1274ddf529eeSNeilBrown 		cred = auth->au_ops->lookup_cred(auth, &acred, 0);
1275ddf529eeSNeilBrown 	if (!cred || IS_ERR(cred))
1276ddf529eeSNeilBrown 		return true;
1277ddf529eeSNeilBrown 	ctx->ll_cred = cred;
1278ddf529eeSNeilBrown 	return !!(cred->cr_ops->crkey_timeout &&
1279ddf529eeSNeilBrown 		  cred->cr_ops->crkey_timeout(cred));
1280dc24826bSAndy Adamson }
1281dc24826bSAndy Adamson 
1282dc24826bSAndy Adamson /*
12835d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
12845d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
12855d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
12865d47a356STrond Myklebust  */
12878d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
12885d47a356STrond Myklebust {
1289d529ef83SJeff Layton 	struct nfs_inode *nfsi = NFS_I(inode);
1290d529ef83SJeff Layton 
12918d197a56STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
12928d197a56STrond Myklebust 		goto out;
129318dd78c4SScott Mayhew 	if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
1294d529ef83SJeff Layton 		return false;
12954db72b40SJeff Layton 	smp_rmb();
1296d529ef83SJeff Layton 	if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags))
12978d197a56STrond Myklebust 		return false;
12988d197a56STrond Myklebust out:
129918dd78c4SScott Mayhew 	if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
130018dd78c4SScott Mayhew 		return false;
13018d197a56STrond Myklebust 	return PageUptodate(page) != 0;
13025d47a356STrond Myklebust }
13035d47a356STrond Myklebust 
13045263e31eSJeff Layton static bool
13055263e31eSJeff Layton is_whole_file_wrlock(struct file_lock *fl)
13065263e31eSJeff Layton {
13075263e31eSJeff Layton 	return fl->fl_start == 0 && fl->fl_end == OFFSET_MAX &&
13085263e31eSJeff Layton 			fl->fl_type == F_WRLCK;
13095263e31eSJeff Layton }
13105263e31eSJeff Layton 
1311c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or
1312c7559663SScott Mayhew  * if we have the whole file locked for writing), it may be more efficient to
1313c7559663SScott Mayhew  * extend the write to cover the entire page in order to avoid fragmentation
1314c7559663SScott Mayhew  * inefficiencies.
1315c7559663SScott Mayhew  *
1316263b4509SScott Mayhew  * If the file is opened for synchronous writes then we can just skip the rest
1317263b4509SScott Mayhew  * of the checks.
1318c7559663SScott Mayhew  */
1319c7559663SScott Mayhew static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode)
1320c7559663SScott Mayhew {
13215263e31eSJeff Layton 	int ret;
13225263e31eSJeff Layton 	struct file_lock_context *flctx = inode->i_flctx;
13235263e31eSJeff Layton 	struct file_lock *fl;
13245263e31eSJeff Layton 
1325c7559663SScott Mayhew 	if (file->f_flags & O_DSYNC)
1326c7559663SScott Mayhew 		return 0;
1327263b4509SScott Mayhew 	if (!nfs_write_pageuptodate(page, inode))
1328263b4509SScott Mayhew 		return 0;
1329c7559663SScott Mayhew 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
1330c7559663SScott Mayhew 		return 1;
1331bd61e0a9SJeff Layton 	if (!flctx || (list_empty_careful(&flctx->flc_flock) &&
1332bd61e0a9SJeff Layton 		       list_empty_careful(&flctx->flc_posix)))
13338fa4592aSTrond Myklebust 		return 1;
13345263e31eSJeff Layton 
13355263e31eSJeff Layton 	/* Check to see if there are whole file write locks */
13365263e31eSJeff Layton 	ret = 0;
13376109c850SJeff Layton 	spin_lock(&flctx->flc_lock);
1338bd61e0a9SJeff Layton 	if (!list_empty(&flctx->flc_posix)) {
1339bd61e0a9SJeff Layton 		fl = list_first_entry(&flctx->flc_posix, struct file_lock,
1340bd61e0a9SJeff Layton 					fl_list);
1341bd61e0a9SJeff Layton 		if (is_whole_file_wrlock(fl))
13425263e31eSJeff Layton 			ret = 1;
1343bd61e0a9SJeff Layton 	} else if (!list_empty(&flctx->flc_flock)) {
13445263e31eSJeff Layton 		fl = list_first_entry(&flctx->flc_flock, struct file_lock,
13455263e31eSJeff Layton 					fl_list);
13465263e31eSJeff Layton 		if (fl->fl_type == F_WRLCK)
13475263e31eSJeff Layton 			ret = 1;
13485263e31eSJeff Layton 	}
13496109c850SJeff Layton 	spin_unlock(&flctx->flc_lock);
13505263e31eSJeff Layton 	return ret;
1351c7559663SScott Mayhew }
1352c7559663SScott Mayhew 
13535d47a356STrond Myklebust /*
13541da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
13551da177e4SLinus Torvalds  *
13561da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
13571da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
13581da177e4SLinus Torvalds  */
13591da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
13601da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
13611da177e4SLinus Torvalds {
1362cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
1363d2ceb7e5SBenjamin Coddington 	struct address_space *mapping = page_file_mapping(page);
1364d2ceb7e5SBenjamin Coddington 	struct inode	*inode = mapping->host;
13651da177e4SLinus Torvalds 	int		status = 0;
13661da177e4SLinus Torvalds 
136791d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
136891d5b470SChuck Lever 
13696de1472fSAl Viro 	dprintk("NFS:       nfs_updatepage(%pD2 %d@%lld)\n",
13706de1472fSAl Viro 		file, count, (long long)(page_file_offset(page) + offset));
13711da177e4SLinus Torvalds 
1372149a4fddSBenjamin Coddington 	if (!count)
1373149a4fddSBenjamin Coddington 		goto out;
1374149a4fddSBenjamin Coddington 
1375c7559663SScott Mayhew 	if (nfs_can_extend_write(file, page, inode)) {
137649a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
13771da177e4SLinus Torvalds 		offset = 0;
13781da177e4SLinus Torvalds 	}
13791da177e4SLinus Torvalds 
1380e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
138103fa9e84STrond Myklebust 	if (status < 0)
1382d2ceb7e5SBenjamin Coddington 		nfs_set_pageerror(mapping);
138359b7c05fSTrond Myklebust 	else
138459b7c05fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
1385149a4fddSBenjamin Coddington out:
138648186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
13871da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
13881da177e4SLinus Torvalds 	return status;
13891da177e4SLinus Torvalds }
13901da177e4SLinus Torvalds 
13913ff7576dSTrond Myklebust static int flush_task_priority(int how)
13921da177e4SLinus Torvalds {
13931da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
13941da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
13951da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
13961da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
13971da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
13981da177e4SLinus Torvalds 	}
13991da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
14001da177e4SLinus Torvalds }
14011da177e4SLinus Torvalds 
1402d45f60c6SWeston Andros Adamson static void nfs_initiate_write(struct nfs_pgio_header *hdr,
1403d45f60c6SWeston Andros Adamson 			       struct rpc_message *msg,
1404abde71f4STom Haynes 			       const struct nfs_rpc_ops *rpc_ops,
14051ed26f33SAnna Schumaker 			       struct rpc_task_setup *task_setup_data, int how)
14061da177e4SLinus Torvalds {
14073ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
14081da177e4SLinus Torvalds 
14091ed26f33SAnna Schumaker 	task_setup_data->priority = priority;
1410fb91fb0eSAnna Schumaker 	rpc_ops->write_setup(hdr, msg, &task_setup_data->rpc_client);
14118224b273SChuck Lever 	trace_nfs_initiate_write(hdr->inode, hdr->io_start, hdr->good_bytes,
14128224b273SChuck Lever 				 hdr->args.stable);
1413275acaafSTrond Myklebust }
1414275acaafSTrond Myklebust 
14156d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
14166d884e8fSFred  * call this on each, which will prepare them to be retried on next
14176d884e8fSFred  * writeback using standard nfs.
14186d884e8fSFred  */
14196d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
14206d884e8fSFred {
142133344e0fSTrond Myklebust 	/* Bump the transmission count */
142233344e0fSTrond Myklebust 	req->wb_nio++;
14236d884e8fSFred 	nfs_mark_request_dirty(req);
14249fcd5960STrond Myklebust 	set_bit(NFS_CONTEXT_RESEND_WRITES, &nfs_req_openctx(req)->flags);
142520633f04SWeston Andros Adamson 	nfs_end_page_writeback(req);
14263aff4ebbSTrond Myklebust 	nfs_release_request(req);
14276d884e8fSFred }
14286d884e8fSFred 
1429df3accb8STrond Myklebust static void nfs_async_write_error(struct list_head *head, int error)
14306c75dc0dSFred Isaman {
14316c75dc0dSFred Isaman 	struct nfs_page	*req;
14326c75dc0dSFred Isaman 
14336c75dc0dSFred Isaman 	while (!list_empty(head)) {
14346c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
14356c75dc0dSFred Isaman 		nfs_list_remove_request(req);
14366fbda89bSTrond Myklebust 		if (nfs_error_is_fatal(error))
14376fbda89bSTrond Myklebust 			nfs_write_error(req, error);
14386fbda89bSTrond Myklebust 		else
14396c75dc0dSFred Isaman 			nfs_redirty_request(req);
14406c75dc0dSFred Isaman 	}
14416c75dc0dSFred Isaman }
14426c75dc0dSFred Isaman 
1443dc602dd7STrond Myklebust static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr)
1444dc602dd7STrond Myklebust {
1445df3accb8STrond Myklebust 	nfs_async_write_error(&hdr->pages, 0);
14467be7b3caSTrond Myklebust 	filemap_fdatawrite_range(hdr->inode->i_mapping, hdr->args.offset,
14477be7b3caSTrond Myklebust 			hdr->args.offset + hdr->args.count - 1);
1448dc602dd7STrond Myklebust }
1449dc602dd7STrond Myklebust 
1450061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1451919e3bd9STrond Myklebust 	.init_hdr = nfs_async_write_init,
1452061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1453061ae2edSFred Isaman 	.completion = nfs_write_completion,
1454dc602dd7STrond Myklebust 	.reschedule_io = nfs_async_write_reschedule_io,
1455061ae2edSFred Isaman };
1456061ae2edSFred Isaman 
145757208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1458a20c93e3SChristoph Hellwig 			       struct inode *inode, int ioflags, bool force_mds,
1459061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
14601751c363STrond Myklebust {
1461a20c93e3SChristoph Hellwig 	struct nfs_server *server = NFS_SERVER(inode);
146241d8d5b7SAnna Schumaker 	const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
1463a20c93e3SChristoph Hellwig 
1464a20c93e3SChristoph Hellwig #ifdef CONFIG_NFS_V4_1
1465a20c93e3SChristoph Hellwig 	if (server->pnfs_curr_ld && !force_mds)
1466a20c93e3SChristoph Hellwig 		pg_ops = server->pnfs_curr_ld->pg_write_ops;
1467a20c93e3SChristoph Hellwig #endif
14684a0de55cSAnna Schumaker 	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
14693bde7afdSTrond Myklebust 			server->wsize, ioflags);
14701751c363STrond Myklebust }
1471ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
14721751c363STrond Myklebust 
1473dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1474dce81290STrond Myklebust {
1475a7d42ddbSWeston Andros Adamson 	struct nfs_pgio_mirror *mirror;
1476a7d42ddbSWeston Andros Adamson 
14776f29b9bbSKinglong Mee 	if (pgio->pg_ops && pgio->pg_ops->pg_cleanup)
14786f29b9bbSKinglong Mee 		pgio->pg_ops->pg_cleanup(pgio);
14796f29b9bbSKinglong Mee 
148041d8d5b7SAnna Schumaker 	pgio->pg_ops = &nfs_pgio_rw_ops;
1481a7d42ddbSWeston Andros Adamson 
1482a7d42ddbSWeston Andros Adamson 	nfs_pageio_stop_mirroring(pgio);
1483a7d42ddbSWeston Andros Adamson 
1484a7d42ddbSWeston Andros Adamson 	mirror = &pgio->pg_mirrors[0];
1485a7d42ddbSWeston Andros Adamson 	mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1486dce81290STrond Myklebust }
14871f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1488dce81290STrond Myklebust 
14891da177e4SLinus Torvalds 
14900b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
14910b7c0153SFred Isaman {
14920b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
14930b7c0153SFred Isaman 
14940b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
14950b7c0153SFred Isaman }
14960b7c0153SFred Isaman 
14971f2edbe3STrond Myklebust /*
14981f2edbe3STrond Myklebust  * Special version of should_remove_suid() that ignores capabilities.
14991f2edbe3STrond Myklebust  */
15001f2edbe3STrond Myklebust static int nfs_should_remove_suid(const struct inode *inode)
15011f2edbe3STrond Myklebust {
15021f2edbe3STrond Myklebust 	umode_t mode = inode->i_mode;
15031f2edbe3STrond Myklebust 	int kill = 0;
1504788e7a89STrond Myklebust 
15051f2edbe3STrond Myklebust 	/* suid always must be killed */
15061f2edbe3STrond Myklebust 	if (unlikely(mode & S_ISUID))
15071f2edbe3STrond Myklebust 		kill = ATTR_KILL_SUID;
15081f2edbe3STrond Myklebust 
15091f2edbe3STrond Myklebust 	/*
15101f2edbe3STrond Myklebust 	 * sgid without any exec bits is just a mandatory locking mark; leave
15111f2edbe3STrond Myklebust 	 * it alone.  If some exec bits are set, it's a real sgid; kill it.
15121f2edbe3STrond Myklebust 	 */
15131f2edbe3STrond Myklebust 	if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
15141f2edbe3STrond Myklebust 		kill |= ATTR_KILL_SGID;
15151f2edbe3STrond Myklebust 
15161f2edbe3STrond Myklebust 	if (unlikely(kill && S_ISREG(mode)))
15171f2edbe3STrond Myklebust 		return kill;
15181f2edbe3STrond Myklebust 
15191f2edbe3STrond Myklebust 	return 0;
15201f2edbe3STrond Myklebust }
1521788e7a89STrond Myklebust 
1522a08a8cd3STrond Myklebust static void nfs_writeback_check_extend(struct nfs_pgio_header *hdr,
1523a08a8cd3STrond Myklebust 		struct nfs_fattr *fattr)
1524a08a8cd3STrond Myklebust {
1525a08a8cd3STrond Myklebust 	struct nfs_pgio_args *argp = &hdr->args;
1526a08a8cd3STrond Myklebust 	struct nfs_pgio_res *resp = &hdr->res;
15272b83d3deSTrond Myklebust 	u64 size = argp->offset + resp->count;
1528a08a8cd3STrond Myklebust 
1529a08a8cd3STrond Myklebust 	if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
15302b83d3deSTrond Myklebust 		fattr->size = size;
15312b83d3deSTrond Myklebust 	if (nfs_size_to_loff_t(fattr->size) < i_size_read(hdr->inode)) {
15322b83d3deSTrond Myklebust 		fattr->valid &= ~NFS_ATTR_FATTR_SIZE;
1533a08a8cd3STrond Myklebust 		return;
15342b83d3deSTrond Myklebust 	}
15352b83d3deSTrond Myklebust 	if (size != fattr->size)
1536a08a8cd3STrond Myklebust 		return;
1537a08a8cd3STrond Myklebust 	/* Set attribute barrier */
1538a08a8cd3STrond Myklebust 	nfs_fattr_set_barrier(fattr);
15392b83d3deSTrond Myklebust 	/* ...and update size */
15402b83d3deSTrond Myklebust 	fattr->valid |= NFS_ATTR_FATTR_SIZE;
1541a08a8cd3STrond Myklebust }
1542a08a8cd3STrond Myklebust 
1543a08a8cd3STrond Myklebust void nfs_writeback_update_inode(struct nfs_pgio_header *hdr)
1544a08a8cd3STrond Myklebust {
15452b83d3deSTrond Myklebust 	struct nfs_fattr *fattr = &hdr->fattr;
1546a08a8cd3STrond Myklebust 	struct inode *inode = hdr->inode;
1547a08a8cd3STrond Myklebust 
1548a08a8cd3STrond Myklebust 	spin_lock(&inode->i_lock);
1549a08a8cd3STrond Myklebust 	nfs_writeback_check_extend(hdr, fattr);
1550a08a8cd3STrond Myklebust 	nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
1551a08a8cd3STrond Myklebust 	spin_unlock(&inode->i_lock);
1552a08a8cd3STrond Myklebust }
1553a08a8cd3STrond Myklebust EXPORT_SYMBOL_GPL(nfs_writeback_update_inode);
1554a08a8cd3STrond Myklebust 
15551da177e4SLinus Torvalds /*
15561da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
15571da177e4SLinus Torvalds  */
1558d45f60c6SWeston Andros Adamson static int nfs_writeback_done(struct rpc_task *task,
1559d45f60c6SWeston Andros Adamson 			      struct nfs_pgio_header *hdr,
15600eecb214SAnna Schumaker 			      struct inode *inode)
15611da177e4SLinus Torvalds {
1562788e7a89STrond Myklebust 	int status;
15631da177e4SLinus Torvalds 
1564f551e44fSChuck Lever 	/*
1565f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1566f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1567f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1568f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1569f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1570f551e44fSChuck Lever 	 */
1571d45f60c6SWeston Andros Adamson 	status = NFS_PROTO(inode)->write_done(task, hdr);
1572788e7a89STrond Myklebust 	if (status != 0)
15730eecb214SAnna Schumaker 		return status;
15748224b273SChuck Lever 
1575d45f60c6SWeston Andros Adamson 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count);
15768224b273SChuck Lever 	trace_nfs_writeback_done(inode, task->tk_status,
15778224b273SChuck Lever 				 hdr->args.offset, hdr->res.verf);
157891d5b470SChuck Lever 
1579d45f60c6SWeston Andros Adamson 	if (hdr->res.verf->committed < hdr->args.stable &&
1580d45f60c6SWeston Andros Adamson 	    task->tk_status >= 0) {
15811da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
15821da177e4SLinus Torvalds 		 * commit data to stable storage even though we
15831da177e4SLinus Torvalds 		 * requested it.
15841da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
15851da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
15861da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
15871da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
15881da177e4SLinus Torvalds 		 */
15891da177e4SLinus Torvalds 		static unsigned long    complain;
15901da177e4SLinus Torvalds 
1591a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
15921da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
15931da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
15941da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1595cd841605SFred Isaman 				NFS_SERVER(inode)->nfs_client->cl_hostname,
1596d45f60c6SWeston Andros Adamson 				hdr->res.verf->committed, hdr->args.stable);
15971da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
15981da177e4SLinus Torvalds 		}
15991da177e4SLinus Torvalds 	}
16001f2edbe3STrond Myklebust 
16011f2edbe3STrond Myklebust 	/* Deal with the suid/sgid bit corner case */
160216e14375STrond Myklebust 	if (nfs_should_remove_suid(inode)) {
160316e14375STrond Myklebust 		spin_lock(&inode->i_lock);
160416e14375STrond Myklebust 		NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
160516e14375STrond Myklebust 		spin_unlock(&inode->i_lock);
160616e14375STrond Myklebust 	}
16070eecb214SAnna Schumaker 	return 0;
16080eecb214SAnna Schumaker }
16090eecb214SAnna Schumaker 
16100eecb214SAnna Schumaker /*
16110eecb214SAnna Schumaker  * This function is called when the WRITE call is complete.
16120eecb214SAnna Schumaker  */
1613d45f60c6SWeston Andros Adamson static void nfs_writeback_result(struct rpc_task *task,
1614d45f60c6SWeston Andros Adamson 				 struct nfs_pgio_header *hdr)
16150eecb214SAnna Schumaker {
1616d45f60c6SWeston Andros Adamson 	struct nfs_pgio_args	*argp = &hdr->args;
1617d45f60c6SWeston Andros Adamson 	struct nfs_pgio_res	*resp = &hdr->res;
16181f2edbe3STrond Myklebust 
16191f2edbe3STrond Myklebust 	if (resp->count < argp->count) {
16201da177e4SLinus Torvalds 		static unsigned long    complain;
16211da177e4SLinus Torvalds 
16226c75dc0dSFred Isaman 		/* This a short write! */
1623d45f60c6SWeston Andros Adamson 		nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE);
162491d5b470SChuck Lever 
16251da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
16266c75dc0dSFred Isaman 		if (resp->count == 0) {
16276c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
16286c75dc0dSFred Isaman 				printk(KERN_WARNING
16296c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
16306c75dc0dSFred Isaman 				       argp->count);
16316c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
16326c75dc0dSFred Isaman 			}
1633d45f60c6SWeston Andros Adamson 			nfs_set_pgio_error(hdr, -EIO, argp->offset);
16346c75dc0dSFred Isaman 			task->tk_status = -EIO;
16356c75dc0dSFred Isaman 			return;
16366c75dc0dSFred Isaman 		}
1637f8417b48SKinglong Mee 
1638f8417b48SKinglong Mee 		/* For non rpc-based layout drivers, retry-through-MDS */
1639f8417b48SKinglong Mee 		if (!task->tk_ops) {
1640f8417b48SKinglong Mee 			hdr->pnfs_error = -EAGAIN;
1641f8417b48SKinglong Mee 			return;
1642f8417b48SKinglong Mee 		}
1643f8417b48SKinglong Mee 
16441da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
16451da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
16461da177e4SLinus Torvalds 			/* Resend from where the server left off */
1647d45f60c6SWeston Andros Adamson 			hdr->mds_offset += resp->count;
16481da177e4SLinus Torvalds 			argp->offset += resp->count;
16491da177e4SLinus Torvalds 			argp->pgbase += resp->count;
16501da177e4SLinus Torvalds 			argp->count -= resp->count;
16511da177e4SLinus Torvalds 		} else {
16521da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
16531da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
16541da177e4SLinus Torvalds 			 */
16551da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
16561da177e4SLinus Torvalds 		}
1657d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
16581da177e4SLinus Torvalds 	}
16591da177e4SLinus Torvalds }
16601da177e4SLinus Torvalds 
1661af7cf057STrond Myklebust static int wait_on_commit(struct nfs_mds_commit_info *cinfo)
166271d0a611STrond Myklebust {
1663723c921eSPeter Zijlstra 	return wait_var_event_killable(&cinfo->rpcs_out,
1664723c921eSPeter Zijlstra 				       !atomic_read(&cinfo->rpcs_out));
1665af7cf057STrond Myklebust }
1666af7cf057STrond Myklebust 
1667af7cf057STrond Myklebust static void nfs_commit_begin(struct nfs_mds_commit_info *cinfo)
1668af7cf057STrond Myklebust {
1669af7cf057STrond Myklebust 	atomic_inc(&cinfo->rpcs_out);
1670af7cf057STrond Myklebust }
1671af7cf057STrond Myklebust 
1672af7cf057STrond Myklebust static void nfs_commit_end(struct nfs_mds_commit_info *cinfo)
1673af7cf057STrond Myklebust {
1674af7cf057STrond Myklebust 	if (atomic_dec_and_test(&cinfo->rpcs_out))
1675723c921eSPeter Zijlstra 		wake_up_var(&cinfo->rpcs_out);
167671d0a611STrond Myklebust }
167771d0a611STrond Myklebust 
16780b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
16791da177e4SLinus Torvalds {
16800b7c0153SFred Isaman 	put_nfs_open_context(data->context);
16810b7c0153SFred Isaman 	nfs_commit_free(data);
16821da177e4SLinus Torvalds }
1683e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
16841da177e4SLinus Torvalds 
16850b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
1686c36aae9aSPeng Tao 			const struct nfs_rpc_ops *nfs_ops,
16879ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
16889f0ec176SAndy Adamson 			int how, int flags)
16891da177e4SLinus Torvalds {
169007737691STrond Myklebust 	struct rpc_task *task;
16919ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1692bdc7f021STrond Myklebust 	struct rpc_message msg = {
1693bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1694bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
16959ace33cdSFred Isaman 		.rpc_cred = data->cred,
1696bdc7f021STrond Myklebust 	};
169784115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
169807737691STrond Myklebust 		.task = &data->task,
16999ace33cdSFred Isaman 		.rpc_client = clnt,
1700bdc7f021STrond Myklebust 		.rpc_message = &msg,
17019ace33cdSFred Isaman 		.callback_ops = call_ops,
170284115e1cSTrond Myklebust 		.callback_data = data,
1703101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
17049f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
17053ff7576dSTrond Myklebust 		.priority = priority,
170684115e1cSTrond Myklebust 	};
1707788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
1708e9ae1ee2SAnna Schumaker 	nfs_ops->commit_setup(data, &msg, &task_setup_data.rpc_client);
17098224b273SChuck Lever 	trace_nfs_initiate_commit(data);
17101da177e4SLinus Torvalds 
1711b4839ebeSKinglong Mee 	dprintk("NFS: initiated commit call\n");
1712bdc7f021STrond Myklebust 
171307737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1714dbae4c73STrond Myklebust 	if (IS_ERR(task))
1715dbae4c73STrond Myklebust 		return PTR_ERR(task);
1716d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1717d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
171807737691STrond Myklebust 	rpc_put_task(task);
1719dbae4c73STrond Myklebust 	return 0;
17201da177e4SLinus Torvalds }
1721e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
17221da177e4SLinus Torvalds 
1723378520b8SPeng Tao static loff_t nfs_get_lwb(struct list_head *head)
1724378520b8SPeng Tao {
1725378520b8SPeng Tao 	loff_t lwb = 0;
1726378520b8SPeng Tao 	struct nfs_page *req;
1727378520b8SPeng Tao 
1728378520b8SPeng Tao 	list_for_each_entry(req, head, wb_list)
1729378520b8SPeng Tao 		if (lwb < (req_offset(req) + req->wb_bytes))
1730378520b8SPeng Tao 			lwb = req_offset(req) + req->wb_bytes;
1731378520b8SPeng Tao 
1732378520b8SPeng Tao 	return lwb;
1733378520b8SPeng Tao }
1734378520b8SPeng Tao 
17351da177e4SLinus Torvalds /*
17369ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
17379ace33cdSFred Isaman  */
17380b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1739988b6dceSFred Isaman 		     struct list_head *head,
1740f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1741f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
17429ace33cdSFred Isaman {
17439ace33cdSFred Isaman 	struct nfs_page *first = nfs_list_entry(head->next);
17449fcd5960STrond Myklebust 	struct nfs_open_context *ctx = nfs_req_openctx(first);
17459fcd5960STrond Myklebust 	struct inode *inode = d_inode(ctx->dentry);
17469ace33cdSFred Isaman 
17479ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
17489ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
17499ace33cdSFred Isaman 
17509ace33cdSFred Isaman 	list_splice_init(head, &data->pages);
17519ace33cdSFred Isaman 
17529ace33cdSFred Isaman 	data->inode	  = inode;
17539fcd5960STrond Myklebust 	data->cred	  = ctx->cred;
1754988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
1755378520b8SPeng Tao 	/* only set lwb for pnfs commit */
1756378520b8SPeng Tao 	if (lseg)
1757378520b8SPeng Tao 		data->lwb = nfs_get_lwb(&data->pages);
17589ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1759f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1760b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
17619ace33cdSFred Isaman 
17629ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
17639ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
17649ace33cdSFred Isaman 	data->args.offset = 0;
17659ace33cdSFred Isaman 	data->args.count  = 0;
17669fcd5960STrond Myklebust 	data->context     = get_nfs_open_context(ctx);
17679ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
17689ace33cdSFred Isaman 	data->res.verf    = &data->verf;
17699ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
17709ace33cdSFred Isaman }
1771e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
17729ace33cdSFred Isaman 
1773e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1774ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1775b57ff130SWeston Andros Adamson 		      struct nfs_commit_info *cinfo,
1776b57ff130SWeston Andros Adamson 		      u32 ds_commit_idx)
177764bfeb49SFred Isaman {
177864bfeb49SFred Isaman 	struct nfs_page *req;
177964bfeb49SFred Isaman 
178064bfeb49SFred Isaman 	while (!list_empty(page_list)) {
178164bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
178264bfeb49SFred Isaman 		nfs_list_remove_request(req);
1783b57ff130SWeston Andros Adamson 		nfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx);
1784487b9b8aSTom Haynes 		if (!cinfo->dreq)
1785487b9b8aSTom Haynes 			nfs_clear_page_commit(req->wb_page);
17861d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
178764bfeb49SFred Isaman 	}
178864bfeb49SFred Isaman }
1789e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
179064bfeb49SFred Isaman 
1791b20135d0STrond Myklebust static void
1792b20135d0STrond Myklebust nfs_commit_resched_write(struct nfs_commit_info *cinfo,
1793b20135d0STrond Myklebust 		struct nfs_page *req)
1794b20135d0STrond Myklebust {
1795b20135d0STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
1796b20135d0STrond Myklebust }
1797b20135d0STrond Myklebust 
17989ace33cdSFred Isaman /*
17991da177e4SLinus Torvalds  * Commit dirty pages
18001da177e4SLinus Torvalds  */
18011da177e4SLinus Torvalds static int
1802ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1803ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
18041da177e4SLinus Torvalds {
18050b7c0153SFred Isaman 	struct nfs_commit_data	*data;
18061da177e4SLinus Torvalds 
1807ade8febdSWeston Andros Adamson 	/* another commit raced with us */
1808ade8febdSWeston Andros Adamson 	if (list_empty(head))
1809ade8febdSWeston Andros Adamson 		return 0;
1810ade8febdSWeston Andros Adamson 
1811518662e0SNeilBrown 	data = nfs_commitdata_alloc(true);
18121da177e4SLinus Torvalds 
18131da177e4SLinus Torvalds 	/* Set up the argument struct */
1814f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
1815f453a54aSFred Isaman 	atomic_inc(&cinfo->mds->rpcs_out);
1816c36aae9aSPeng Tao 	return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode),
1817c36aae9aSPeng Tao 				   data->mds_ops, how, 0);
18181da177e4SLinus Torvalds }
18191da177e4SLinus Torvalds 
18201da177e4SLinus Torvalds /*
18211da177e4SLinus Torvalds  * COMMIT call returned
18221da177e4SLinus Torvalds  */
1823788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
18241da177e4SLinus Torvalds {
18250b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
18261da177e4SLinus Torvalds 
1827a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
18281da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
18291da177e4SLinus Torvalds 
1830788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1831c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
18328224b273SChuck Lever 	trace_nfs_commit_done(data);
1833c9d8f89dSTrond Myklebust }
1834c9d8f89dSTrond Myklebust 
1835f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1836c9d8f89dSTrond Myklebust {
1837c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1838c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1839f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
1840353db796SNeilBrown 	struct nfs_server *nfss;
1841788e7a89STrond Myklebust 
18421da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
18431da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
18441da177e4SLinus Torvalds 		nfs_list_remove_request(req);
184567911c8fSAnna Schumaker 		if (req->wb_page)
1846d6d6dc7cSFred Isaman 			nfs_clear_page_commit(req->wb_page);
18471da177e4SLinus Torvalds 
18481e8968c5SNiels de Vos 		dprintk("NFS:       commit (%s/%llu %d@%lld)",
18499fcd5960STrond Myklebust 			nfs_req_openctx(req)->dentry->d_sb->s_id,
18509fcd5960STrond Myklebust 			(unsigned long long)NFS_FILEID(d_inode(nfs_req_openctx(req)->dentry)),
18511da177e4SLinus Torvalds 			req->wb_bytes,
18521da177e4SLinus Torvalds 			(long long)req_offset(req));
1853c9d8f89dSTrond Myklebust 		if (status < 0) {
18546fbda89bSTrond Myklebust 			if (req->wb_page) {
18556fbda89bSTrond Myklebust 				nfs_mapping_set_error(req->wb_page, status);
18561da177e4SLinus Torvalds 				nfs_inode_remove_request(req);
18576fbda89bSTrond Myklebust 			}
1858ddeaa637SJoe Perches 			dprintk_cont(", error = %d\n", status);
18591da177e4SLinus Torvalds 			goto next;
18601da177e4SLinus Torvalds 		}
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
18631da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
18648fc3c386STrond Myklebust 		if (!nfs_write_verifier_cmp(&req->wb_verf, &data->verf.verifier)) {
18651da177e4SLinus Torvalds 			/* We have a match */
186638a33101SKinglong Mee 			if (req->wb_page)
18671da177e4SLinus Torvalds 				nfs_inode_remove_request(req);
1868ddeaa637SJoe Perches 			dprintk_cont(" OK\n");
18691da177e4SLinus Torvalds 			goto next;
18701da177e4SLinus Torvalds 		}
18711da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
1872ddeaa637SJoe Perches 		dprintk_cont(" mismatch\n");
18736d884e8fSFred 		nfs_mark_request_dirty(req);
18749fcd5960STrond Myklebust 		set_bit(NFS_CONTEXT_RESEND_WRITES, &nfs_req_openctx(req)->flags);
18751da177e4SLinus Torvalds 	next:
18761d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
18777f1bda44STrond Myklebust 		/* Latency breaker */
18787f1bda44STrond Myklebust 		cond_resched();
18791da177e4SLinus Torvalds 	}
1880353db796SNeilBrown 	nfss = NFS_SERVER(data->inode);
1881353db796SNeilBrown 	if (atomic_long_read(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
18820db10944SJan Kara 		clear_bdi_congested(inode_to_bdi(data->inode), BLK_RW_ASYNC);
1883353db796SNeilBrown 
1884f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1885af7cf057STrond Myklebust 	nfs_commit_end(cinfo.mds);
18865917ce84SFred Isaman }
18875917ce84SFred Isaman 
18885917ce84SFred Isaman static void nfs_commit_release(void *calldata)
18895917ce84SFred Isaman {
18900b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
18915917ce84SFred Isaman 
1892f453a54aSFred Isaman 	data->completion_ops->completion(data);
1893c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
18941da177e4SLinus Torvalds }
1895788e7a89STrond Myklebust 
1896788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
18970b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1898788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1899788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1900788e7a89STrond Myklebust };
19011da177e4SLinus Torvalds 
1902f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1903f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1904b20135d0STrond Myklebust 	.resched_write = nfs_commit_resched_write,
1905f453a54aSFred Isaman };
1906f453a54aSFred Isaman 
19071763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1908ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
190984c53ab5SFred Isaman {
191084c53ab5SFred Isaman 	int status;
191184c53ab5SFred Isaman 
1912ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
191384c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1914ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
191584c53ab5SFred Isaman 	return status;
191684c53ab5SFred Isaman }
191784c53ab5SFred Isaman 
1918c4f24df9STrond Myklebust static int __nfs_commit_inode(struct inode *inode, int how,
1919c4f24df9STrond Myklebust 		struct writeback_control *wbc)
19201da177e4SLinus Torvalds {
19211da177e4SLinus Torvalds 	LIST_HEAD(head);
1922ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
192371d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1924c4f24df9STrond Myklebust 	int ret, nscan;
19251da177e4SLinus Torvalds 
1926ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1927af7cf057STrond Myklebust 	nfs_commit_begin(cinfo.mds);
1928c4f24df9STrond Myklebust 	for (;;) {
1929c4f24df9STrond Myklebust 		ret = nscan = nfs_scan_commit(inode, &head, &cinfo);
1930c4f24df9STrond Myklebust 		if (ret <= 0)
1931c4f24df9STrond Myklebust 			break;
1932c4f24df9STrond Myklebust 		ret = nfs_generic_commit_list(inode, &head, how, &cinfo);
1933c4f24df9STrond Myklebust 		if (ret < 0)
1934c4f24df9STrond Myklebust 			break;
1935c4f24df9STrond Myklebust 		ret = 0;
1936c4f24df9STrond Myklebust 		if (wbc && wbc->sync_mode == WB_SYNC_NONE) {
1937c4f24df9STrond Myklebust 			if (nscan < wbc->nr_to_write)
1938c4f24df9STrond Myklebust 				wbc->nr_to_write -= nscan;
1939c4f24df9STrond Myklebust 			else
1940c4f24df9STrond Myklebust 				wbc->nr_to_write = 0;
1941c4f24df9STrond Myklebust 		}
1942c4f24df9STrond Myklebust 		if (nscan < INT_MAX)
1943c4f24df9STrond Myklebust 			break;
1944c4f24df9STrond Myklebust 		cond_resched();
1945c4f24df9STrond Myklebust 	}
1946af7cf057STrond Myklebust 	nfs_commit_end(cinfo.mds);
1947c4f24df9STrond Myklebust 	if (ret || !may_wait)
1948c4f24df9STrond Myklebust 		return ret;
1949c4f24df9STrond Myklebust 	return wait_on_commit(cinfo.mds);
1950c4f24df9STrond Myklebust }
1951c4f24df9STrond Myklebust 
1952c4f24df9STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
1953c4f24df9STrond Myklebust {
1954c4f24df9STrond Myklebust 	return __nfs_commit_inode(inode, how, NULL);
19551da177e4SLinus Torvalds }
1956b20135d0STrond Myklebust EXPORT_SYMBOL_GPL(nfs_commit_inode);
19578fc795f7STrond Myklebust 
1958ae09c31fSAnna Schumaker int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
19598fc795f7STrond Myklebust {
1960420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1961420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1962420e3646STrond Myklebust 	int ret = 0;
19638fc795f7STrond Myklebust 
1964c4f24df9STrond Myklebust 	if (wbc->sync_mode == WB_SYNC_NONE) {
19653236c3e1SJeff Layton 		/* no commits means nothing needs to be done */
19665cb953d4STrond Myklebust 		if (!atomic_long_read(&nfsi->commit_info.ncommit))
1967c4f24df9STrond Myklebust 			goto check_requests_outstanding;
19683236c3e1SJeff Layton 
1969a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1970a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1971420e3646STrond Myklebust 		 */
19721a4edf0fSTrond Myklebust 		if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1973420e3646STrond Myklebust 			goto out_mark_dirty;
1974420e3646STrond Myklebust 
1975a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1976420e3646STrond Myklebust 		flags = 0;
1977a00dd6c0SJeff Layton 	}
1978a00dd6c0SJeff Layton 
1979c4f24df9STrond Myklebust 	ret = __nfs_commit_inode(inode, flags, wbc);
1980c4f24df9STrond Myklebust 	if (!ret) {
1981c4f24df9STrond Myklebust 		if (flags & FLUSH_SYNC)
19828fc795f7STrond Myklebust 			return 0;
1983c4f24df9STrond Myklebust 	} else if (atomic_long_read(&nfsi->commit_info.ncommit))
1984c4f24df9STrond Myklebust 		goto out_mark_dirty;
1985c4f24df9STrond Myklebust 
1986c4f24df9STrond Myklebust check_requests_outstanding:
1987c4f24df9STrond Myklebust 	if (!atomic_read(&nfsi->commit_info.rpcs_out))
1988c4f24df9STrond Myklebust 		return ret;
1989420e3646STrond Myklebust out_mark_dirty:
19908fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
19918fc795f7STrond Myklebust 	return ret;
19928fc795f7STrond Myklebust }
199389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode);
1994863a3c6cSAndy Adamson 
1995acdc53b2STrond Myklebust /*
1996837bb1d7STrond Myklebust  * Wrapper for filemap_write_and_wait_range()
1997837bb1d7STrond Myklebust  *
1998837bb1d7STrond Myklebust  * Needed for pNFS in order to ensure data becomes visible to the
1999837bb1d7STrond Myklebust  * client.
2000837bb1d7STrond Myklebust  */
2001837bb1d7STrond Myklebust int nfs_filemap_write_and_wait_range(struct address_space *mapping,
2002837bb1d7STrond Myklebust 		loff_t lstart, loff_t lend)
2003837bb1d7STrond Myklebust {
2004837bb1d7STrond Myklebust 	int ret;
2005837bb1d7STrond Myklebust 
2006837bb1d7STrond Myklebust 	ret = filemap_write_and_wait_range(mapping, lstart, lend);
2007837bb1d7STrond Myklebust 	if (ret == 0)
2008837bb1d7STrond Myklebust 		ret = pnfs_sync_inode(mapping->host, true);
2009837bb1d7STrond Myklebust 	return ret;
2010837bb1d7STrond Myklebust }
2011837bb1d7STrond Myklebust EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range);
2012837bb1d7STrond Myklebust 
2013837bb1d7STrond Myklebust /*
2014acdc53b2STrond Myklebust  * flush the inode to disk.
2015acdc53b2STrond Myklebust  */
2016acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
201734901f70STrond Myklebust {
2018f4ce1299STrond Myklebust 	int ret;
201934901f70STrond Myklebust 
2020f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_enter(inode);
2021f4ce1299STrond Myklebust 
20225bb89b47STrond Myklebust 	ret = filemap_write_and_wait(inode->i_mapping);
20236b196875SChuck Lever 	if (ret)
20246b196875SChuck Lever 		goto out;
20255bb89b47STrond Myklebust 	ret = nfs_commit_inode(inode, FLUSH_SYNC);
20266b196875SChuck Lever 	if (ret < 0)
20276b196875SChuck Lever 		goto out;
20285bb89b47STrond Myklebust 	pnfs_sync_inode(inode, true);
20296b196875SChuck Lever 	ret = 0;
2030f4ce1299STrond Myklebust 
20316b196875SChuck Lever out:
2032f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_exit(inode, ret);
2033f4ce1299STrond Myklebust 	return ret;
20341c75950bSTrond Myklebust }
2035ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all);
20361c75950bSTrond Myklebust 
20371b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
20381b3b4a1aSTrond Myklebust {
20391b3b4a1aSTrond Myklebust 	struct nfs_page *req;
20401b3b4a1aSTrond Myklebust 	int ret = 0;
20411b3b4a1aSTrond Myklebust 
2042ba8b06e6STrond Myklebust 	wait_on_page_writeback(page);
20433e217045SWeston Andros Adamson 
20443e217045SWeston Andros Adamson 	/* blocking call to cancel all requests and join to a single (head)
20453e217045SWeston Andros Adamson 	 * request */
20466d17d653STrond Myklebust 	req = nfs_lock_and_join_requests(page);
20473e217045SWeston Andros Adamson 
20483e217045SWeston Andros Adamson 	if (IS_ERR(req)) {
20493e217045SWeston Andros Adamson 		ret = PTR_ERR(req);
20503e217045SWeston Andros Adamson 	} else if (req) {
20513e217045SWeston Andros Adamson 		/* all requests from this page have been cancelled by
20523e217045SWeston Andros Adamson 		 * nfs_lock_and_join_requests, so just remove the head
20533e217045SWeston Andros Adamson 		 * request from the inode / page_private pointer and
20543e217045SWeston Andros Adamson 		 * release it */
20551b3b4a1aSTrond Myklebust 		nfs_inode_remove_request(req);
20561d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
20571b3b4a1aSTrond Myklebust 	}
20583e217045SWeston Andros Adamson 
20591b3b4a1aSTrond Myklebust 	return ret;
20601b3b4a1aSTrond Myklebust }
20611b3b4a1aSTrond Myklebust 
20621c75950bSTrond Myklebust /*
20631c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
20641c75950bSTrond Myklebust  */
2065c373fff7STrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
20661c75950bSTrond Myklebust {
206729418aa4SMel Gorman 	loff_t range_start = page_file_offset(page);
206809cbfeafSKirill A. Shutemov 	loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1);
20697f2f12d9STrond Myklebust 	struct writeback_control wbc = {
20707f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
20717f2f12d9STrond Myklebust 		.nr_to_write = 0,
20727f2f12d9STrond Myklebust 		.range_start = range_start,
20737f2f12d9STrond Myklebust 		.range_end = range_end,
20747f2f12d9STrond Myklebust 	};
20757f2f12d9STrond Myklebust 	int ret;
20767f2f12d9STrond Myklebust 
2077f4ce1299STrond Myklebust 	trace_nfs_writeback_page_enter(inode);
2078f4ce1299STrond Myklebust 
20790522f6adSTrond Myklebust 	for (;;) {
2080ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
20817f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
2082c373fff7STrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
20837f2f12d9STrond Myklebust 			if (ret < 0)
20847f2f12d9STrond Myklebust 				goto out_error;
20850522f6adSTrond Myklebust 			continue;
20867f2f12d9STrond Myklebust 		}
2087f4ce1299STrond Myklebust 		ret = 0;
20880522f6adSTrond Myklebust 		if (!PagePrivate(page))
20890522f6adSTrond Myklebust 			break;
20900522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
20917f2f12d9STrond Myklebust 		if (ret < 0)
20927f2f12d9STrond Myklebust 			goto out_error;
20937f2f12d9STrond Myklebust 	}
20947f2f12d9STrond Myklebust out_error:
2095f4ce1299STrond Myklebust 	trace_nfs_writeback_page_exit(inode, ret);
20967f2f12d9STrond Myklebust 	return ret;
20971c75950bSTrond Myklebust }
20981c75950bSTrond Myklebust 
2099074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
2100074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
2101a6bc32b8SMel Gorman 		struct page *page, enum migrate_mode mode)
2102074cc1deSTrond Myklebust {
21032da95652SJeff Layton 	/*
21042da95652SJeff Layton 	 * If PagePrivate is set, then the page is currently associated with
21052da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
21062da95652SJeff Layton 	 *
21072da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
21082da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
21092da95652SJeff Layton 	 *        the page lock.
21102da95652SJeff Layton 	 */
21112da95652SJeff Layton 	if (PagePrivate(page))
21122da95652SJeff Layton 		return -EBUSY;
2113074cc1deSTrond Myklebust 
21148c209ce7SDavid Howells 	if (!nfs_fscache_release_page(page, GFP_KERNEL))
21158c209ce7SDavid Howells 		return -EBUSY;
2116074cc1deSTrond Myklebust 
2117a6bc32b8SMel Gorman 	return migrate_page(mapping, newpage, page, mode);
2118074cc1deSTrond Myklebust }
2119074cc1deSTrond Myklebust #endif
2120074cc1deSTrond Myklebust 
2121f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
21221da177e4SLinus Torvalds {
21231da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
21241e7f3a48SWeston Andros Adamson 					     sizeof(struct nfs_pgio_header),
21251da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
212620c2df83SPaul Mundt 					     NULL);
21271da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
21281da177e4SLinus Torvalds 		return -ENOMEM;
21291da177e4SLinus Torvalds 
213093d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
21311da177e4SLinus Torvalds 						     nfs_wdata_cachep);
21321da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
21333dd4765fSJeff Layton 		goto out_destroy_write_cache;
21341da177e4SLinus Torvalds 
21350b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
21360b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
21370b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
21380b7c0153SFred Isaman 					     NULL);
21390b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
21403dd4765fSJeff Layton 		goto out_destroy_write_mempool;
21410b7c0153SFred Isaman 
214293d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
21434c100210SYanchuan Nian 						      nfs_cdata_cachep);
21441da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
21453dd4765fSJeff Layton 		goto out_destroy_commit_cache;
21461da177e4SLinus Torvalds 
214789a09141SPeter Zijlstra 	/*
214889a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
214989a09141SPeter Zijlstra 	 *
215089a09141SPeter Zijlstra 	 *  64MB:    8192k
215189a09141SPeter Zijlstra 	 * 128MB:   11585k
215289a09141SPeter Zijlstra 	 * 256MB:   16384k
215389a09141SPeter Zijlstra 	 * 512MB:   23170k
215489a09141SPeter Zijlstra 	 *   1GB:   32768k
215589a09141SPeter Zijlstra 	 *   2GB:   46340k
215689a09141SPeter Zijlstra 	 *   4GB:   65536k
215789a09141SPeter Zijlstra 	 *   8GB:   92681k
215889a09141SPeter Zijlstra 	 *  16GB:  131072k
215989a09141SPeter Zijlstra 	 *
216089a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
216189a09141SPeter Zijlstra 	 * Limit the default to 256M
216289a09141SPeter Zijlstra 	 */
2163ca79b0c2SArun KS 	nfs_congestion_kb = (16*int_sqrt(totalram_pages())) << (PAGE_SHIFT-10);
216489a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
216589a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
216689a09141SPeter Zijlstra 
21671da177e4SLinus Torvalds 	return 0;
21683dd4765fSJeff Layton 
21693dd4765fSJeff Layton out_destroy_commit_cache:
21703dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
21713dd4765fSJeff Layton out_destroy_write_mempool:
21723dd4765fSJeff Layton 	mempool_destroy(nfs_wdata_mempool);
21733dd4765fSJeff Layton out_destroy_write_cache:
21743dd4765fSJeff Layton 	kmem_cache_destroy(nfs_wdata_cachep);
21753dd4765fSJeff Layton 	return -ENOMEM;
21761da177e4SLinus Torvalds }
21771da177e4SLinus Torvalds 
2178266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
21791da177e4SLinus Torvalds {
21801da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
21813dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
21821da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
21831a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
21841da177e4SLinus Torvalds }
21851da177e4SLinus Torvalds 
21864a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops = {
21874a0de55cSAnna Schumaker 	.rw_alloc_header	= nfs_writehdr_alloc,
21884a0de55cSAnna Schumaker 	.rw_free_header		= nfs_writehdr_free,
21890eecb214SAnna Schumaker 	.rw_done		= nfs_writeback_done,
21900eecb214SAnna Schumaker 	.rw_result		= nfs_writeback_result,
21911ed26f33SAnna Schumaker 	.rw_initiate		= nfs_initiate_write,
21924a0de55cSAnna Schumaker };
2193