xref: /openbmc/linux/fs/nfs/write.c (revision 6df25e58)
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;
6006c9fdf3STrond Myklebust static void nfs_inode_remove_request(struct nfs_page *req);
61d4581383SWeston Andros Adamson static void nfs_clear_request_commit(struct nfs_page *req);
6202d1426cSWeston Andros Adamson static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
6302d1426cSWeston Andros Adamson 				      struct inode *inode);
643a3908c8STrond Myklebust static struct nfs_page *
653a3908c8STrond Myklebust nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
663a3908c8STrond Myklebust 						struct page *page);
671da177e4SLinus Torvalds 
68e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep;
693feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
700b7c0153SFred Isaman static struct kmem_cache *nfs_cdata_cachep;
711da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
721da177e4SLinus Torvalds 
73518662e0SNeilBrown struct nfs_commit_data *nfs_commitdata_alloc(bool never_fail)
741da177e4SLinus Torvalds {
75518662e0SNeilBrown 	struct nfs_commit_data *p;
7640859d7eSChuck Lever 
77518662e0SNeilBrown 	if (never_fail)
78518662e0SNeilBrown 		p = mempool_alloc(nfs_commit_mempool, GFP_NOIO);
79518662e0SNeilBrown 	else {
80518662e0SNeilBrown 		/* It is OK to do some reclaim, not no safe to wait
81518662e0SNeilBrown 		 * for anything to be returned to the pool.
82518662e0SNeilBrown 		 * mempool_alloc() cannot handle that particular combination,
83518662e0SNeilBrown 		 * so we need two separate attempts.
84518662e0SNeilBrown 		 */
85518662e0SNeilBrown 		p = mempool_alloc(nfs_commit_mempool, GFP_NOWAIT);
86518662e0SNeilBrown 		if (!p)
87518662e0SNeilBrown 			p = kmem_cache_alloc(nfs_cdata_cachep, GFP_NOIO |
88518662e0SNeilBrown 					     __GFP_NOWARN | __GFP_NORETRY);
89518662e0SNeilBrown 		if (!p)
90518662e0SNeilBrown 			return NULL;
91518662e0SNeilBrown 	}
92518662e0SNeilBrown 
931da177e4SLinus Torvalds 	memset(p, 0, sizeof(*p));
941da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->pages);
951da177e4SLinus Torvalds 	return p;
961da177e4SLinus Torvalds }
97e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
981da177e4SLinus Torvalds 
990b7c0153SFred Isaman void nfs_commit_free(struct nfs_commit_data *p)
1001da177e4SLinus Torvalds {
1011da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
1021da177e4SLinus Torvalds }
103e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free);
1041da177e4SLinus Torvalds 
1051e7f3a48SWeston Andros Adamson static struct nfs_pgio_header *nfs_writehdr_alloc(void)
1063feb2d49STrond Myklebust {
1072b17d725STrond Myklebust 	struct nfs_pgio_header *p = mempool_alloc(nfs_wdata_mempool, GFP_KERNEL);
1083feb2d49STrond Myklebust 
1093feb2d49STrond Myklebust 	memset(p, 0, sizeof(*p));
110fbe77c30SBenjamin Coddington 	p->rw_mode = FMODE_WRITE;
1113feb2d49STrond Myklebust 	return p;
1123feb2d49STrond Myklebust }
1133feb2d49STrond Myklebust 
1141e7f3a48SWeston Andros Adamson static void nfs_writehdr_free(struct nfs_pgio_header *hdr)
1156c75dc0dSFred Isaman {
1161e7f3a48SWeston Andros Adamson 	mempool_free(hdr, nfs_wdata_mempool);
1173feb2d49STrond Myklebust }
1181da177e4SLinus Torvalds 
119919e3bd9STrond Myklebust static struct nfs_io_completion *nfs_io_completion_alloc(gfp_t gfp_flags)
120919e3bd9STrond Myklebust {
121919e3bd9STrond Myklebust 	return kmalloc(sizeof(struct nfs_io_completion), gfp_flags);
122919e3bd9STrond Myklebust }
123919e3bd9STrond Myklebust 
124919e3bd9STrond Myklebust static void nfs_io_completion_init(struct nfs_io_completion *ioc,
125919e3bd9STrond Myklebust 		void (*complete)(void *), void *data)
126919e3bd9STrond Myklebust {
127919e3bd9STrond Myklebust 	ioc->complete = complete;
128919e3bd9STrond Myklebust 	ioc->data = data;
129919e3bd9STrond Myklebust 	kref_init(&ioc->refcount);
130919e3bd9STrond Myklebust }
131919e3bd9STrond Myklebust 
132919e3bd9STrond Myklebust static void nfs_io_completion_release(struct kref *kref)
133919e3bd9STrond Myklebust {
134919e3bd9STrond Myklebust 	struct nfs_io_completion *ioc = container_of(kref,
135919e3bd9STrond Myklebust 			struct nfs_io_completion, refcount);
136919e3bd9STrond Myklebust 	ioc->complete(ioc->data);
137919e3bd9STrond Myklebust 	kfree(ioc);
138919e3bd9STrond Myklebust }
139919e3bd9STrond Myklebust 
140919e3bd9STrond Myklebust static void nfs_io_completion_get(struct nfs_io_completion *ioc)
141919e3bd9STrond Myklebust {
142919e3bd9STrond Myklebust 	if (ioc != NULL)
143919e3bd9STrond Myklebust 		kref_get(&ioc->refcount);
144919e3bd9STrond Myklebust }
145919e3bd9STrond Myklebust 
146919e3bd9STrond Myklebust static void nfs_io_completion_put(struct nfs_io_completion *ioc)
147919e3bd9STrond Myklebust {
148919e3bd9STrond Myklebust 	if (ioc != NULL)
149919e3bd9STrond Myklebust 		kref_put(&ioc->refcount, nfs_io_completion_release);
150919e3bd9STrond Myklebust }
151919e3bd9STrond Myklebust 
152e00ed89dSTrond Myklebust static void
153e00ed89dSTrond Myklebust nfs_page_set_inode_ref(struct nfs_page *req, struct inode *inode)
154e00ed89dSTrond Myklebust {
155e00ed89dSTrond Myklebust 	if (!test_and_set_bit(PG_INODE_REF, &req->wb_flags)) {
156e00ed89dSTrond Myklebust 		kref_get(&req->wb_kref);
157e00ed89dSTrond Myklebust 		atomic_long_inc(&NFS_I(inode)->nrequests);
158e00ed89dSTrond Myklebust 	}
159e00ed89dSTrond Myklebust }
160e00ed89dSTrond Myklebust 
161e00ed89dSTrond Myklebust static int
162e00ed89dSTrond Myklebust nfs_cancel_remove_inode(struct nfs_page *req, struct inode *inode)
163e00ed89dSTrond Myklebust {
164e00ed89dSTrond Myklebust 	int ret;
165e00ed89dSTrond Myklebust 
166e00ed89dSTrond Myklebust 	if (!test_bit(PG_REMOVE, &req->wb_flags))
167e00ed89dSTrond Myklebust 		return 0;
168e00ed89dSTrond Myklebust 	ret = nfs_page_group_lock(req);
169e00ed89dSTrond Myklebust 	if (ret)
170e00ed89dSTrond Myklebust 		return ret;
171e00ed89dSTrond Myklebust 	if (test_and_clear_bit(PG_REMOVE, &req->wb_flags))
172e00ed89dSTrond Myklebust 		nfs_page_set_inode_ref(req, inode);
173e00ed89dSTrond Myklebust 	nfs_page_group_unlock(req);
174e00ed89dSTrond Myklebust 	return 0;
175e00ed89dSTrond Myklebust }
176e00ed89dSTrond Myklebust 
177bd37d6fcSTrond Myklebust static struct nfs_page *
178bd37d6fcSTrond Myklebust nfs_page_private_request(struct page *page)
179bd37d6fcSTrond Myklebust {
180bd37d6fcSTrond Myklebust 	if (!PagePrivate(page))
181bd37d6fcSTrond Myklebust 		return NULL;
182bd37d6fcSTrond Myklebust 	return (struct nfs_page *)page_private(page);
183bd37d6fcSTrond Myklebust }
184bd37d6fcSTrond Myklebust 
18584d3a9a9SWeston Andros Adamson /*
18684d3a9a9SWeston Andros Adamson  * nfs_page_find_head_request_locked - find head request associated with @page
18784d3a9a9SWeston Andros Adamson  *
18884d3a9a9SWeston Andros Adamson  * must be called while holding the inode lock.
18984d3a9a9SWeston Andros Adamson  *
19084d3a9a9SWeston Andros Adamson  * returns matching head request with reference held, or NULL if not found.
19184d3a9a9SWeston Andros Adamson  */
19229418aa4SMel Gorman static struct nfs_page *
193b30d2f04STrond Myklebust nfs_page_find_private_request(struct page *page)
194277459d2STrond Myklebust {
1954b9bb25bSTrond Myklebust 	struct address_space *mapping = page_file_mapping(page);
196bd37d6fcSTrond Myklebust 	struct nfs_page *req;
197277459d2STrond Myklebust 
198b30d2f04STrond Myklebust 	if (!PagePrivate(page))
199b30d2f04STrond Myklebust 		return NULL;
2004b9bb25bSTrond Myklebust 	spin_lock(&mapping->private_lock);
201bd37d6fcSTrond Myklebust 	req = nfs_page_private_request(page);
20284d3a9a9SWeston Andros Adamson 	if (req) {
20384d3a9a9SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_head != req);
20429418aa4SMel Gorman 		kref_get(&req->wb_kref);
20584d3a9a9SWeston Andros Adamson 	}
2064b9bb25bSTrond Myklebust 	spin_unlock(&mapping->private_lock);
207b30d2f04STrond Myklebust 	return req;
208b30d2f04STrond Myklebust }
20929418aa4SMel Gorman 
210b30d2f04STrond Myklebust static struct nfs_page *
211b30d2f04STrond Myklebust nfs_page_find_swap_request(struct page *page)
212b30d2f04STrond Myklebust {
213b30d2f04STrond Myklebust 	struct inode *inode = page_file_mapping(page)->host;
214b30d2f04STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
215b30d2f04STrond Myklebust 	struct nfs_page *req = NULL;
216b30d2f04STrond Myklebust 	if (!PageSwapCache(page))
217b30d2f04STrond Myklebust 		return NULL;
218e824f99aSTrond Myklebust 	mutex_lock(&nfsi->commit_mutex);
219b30d2f04STrond Myklebust 	if (PageSwapCache(page)) {
220b30d2f04STrond Myklebust 		req = nfs_page_search_commits_for_head_request_locked(nfsi,
221b30d2f04STrond Myklebust 			page);
222b30d2f04STrond Myklebust 		if (req) {
223b30d2f04STrond Myklebust 			WARN_ON_ONCE(req->wb_head != req);
224b30d2f04STrond Myklebust 			kref_get(&req->wb_kref);
225b30d2f04STrond Myklebust 		}
226b30d2f04STrond Myklebust 	}
227e824f99aSTrond Myklebust 	mutex_unlock(&nfsi->commit_mutex);
228277459d2STrond Myklebust 	return req;
229277459d2STrond Myklebust }
230277459d2STrond Myklebust 
23184d3a9a9SWeston Andros Adamson /*
23284d3a9a9SWeston Andros Adamson  * nfs_page_find_head_request - find head request associated with @page
23384d3a9a9SWeston Andros Adamson  *
23484d3a9a9SWeston Andros Adamson  * returns matching head request with reference held, or NULL if not found.
23584d3a9a9SWeston Andros Adamson  */
23684d3a9a9SWeston Andros Adamson static struct nfs_page *nfs_page_find_head_request(struct page *page)
237277459d2STrond Myklebust {
238b30d2f04STrond Myklebust 	struct nfs_page *req;
239277459d2STrond Myklebust 
240b30d2f04STrond Myklebust 	req = nfs_page_find_private_request(page);
241b30d2f04STrond Myklebust 	if (!req)
242b30d2f04STrond Myklebust 		req = nfs_page_find_swap_request(page);
243277459d2STrond Myklebust 	return req;
244277459d2STrond Myklebust }
245277459d2STrond Myklebust 
246e00ed89dSTrond Myklebust static struct nfs_page *nfs_find_and_lock_page_request(struct page *page)
247e00ed89dSTrond Myklebust {
248e00ed89dSTrond Myklebust 	struct inode *inode = page_file_mapping(page)->host;
249e00ed89dSTrond Myklebust 	struct nfs_page *req, *head;
250e00ed89dSTrond Myklebust 	int ret;
251e00ed89dSTrond Myklebust 
252e00ed89dSTrond Myklebust 	for (;;) {
253e00ed89dSTrond Myklebust 		req = nfs_page_find_head_request(page);
254e00ed89dSTrond Myklebust 		if (!req)
255e00ed89dSTrond Myklebust 			return req;
256e00ed89dSTrond Myklebust 		head = nfs_page_group_lock_head(req);
257e00ed89dSTrond Myklebust 		if (head != req)
258e00ed89dSTrond Myklebust 			nfs_release_request(req);
259e00ed89dSTrond Myklebust 		if (IS_ERR(head))
260e00ed89dSTrond Myklebust 			return head;
261e00ed89dSTrond Myklebust 		ret = nfs_cancel_remove_inode(head, inode);
262e00ed89dSTrond Myklebust 		if (ret < 0) {
263e00ed89dSTrond Myklebust 			nfs_unlock_and_release_request(head);
264e00ed89dSTrond Myklebust 			return ERR_PTR(ret);
265e00ed89dSTrond Myklebust 		}
266e00ed89dSTrond Myklebust 		/* Ensure that nobody removed the request before we locked it */
267e00ed89dSTrond Myklebust 		if (head == nfs_page_private_request(page))
268e00ed89dSTrond Myklebust 			break;
269e00ed89dSTrond Myklebust 		if (PageSwapCache(page))
270e00ed89dSTrond Myklebust 			break;
271e00ed89dSTrond Myklebust 		nfs_unlock_and_release_request(head);
272e00ed89dSTrond Myklebust 	}
273e00ed89dSTrond Myklebust 	return head;
274e00ed89dSTrond Myklebust }
275e00ed89dSTrond Myklebust 
2761da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
2771da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
2781da177e4SLinus Torvalds {
279d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
280a3d01454STrond Myklebust 	loff_t end, i_size;
281a3d01454STrond Myklebust 	pgoff_t end_index;
2821da177e4SLinus Torvalds 
283a3d01454STrond Myklebust 	spin_lock(&inode->i_lock);
284a3d01454STrond Myklebust 	i_size = i_size_read(inode);
28509cbfeafSKirill A. Shutemov 	end_index = (i_size - 1) >> PAGE_SHIFT;
2868cd79788SHuang Ying 	if (i_size > 0 && page_index(page) < end_index)
287a3d01454STrond Myklebust 		goto out;
288d56b4ddfSMel Gorman 	end = page_file_offset(page) + ((loff_t)offset+count);
2891da177e4SLinus Torvalds 	if (i_size >= end)
290a3d01454STrond Myklebust 		goto out;
291110cb2d2SChuck Lever 	trace_nfs_size_grow(inode, end);
2921da177e4SLinus Torvalds 	i_size_write(inode, end);
293f6cdfa6dSTrond Myklebust 	NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE;
294a3d01454STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
295a3d01454STrond Myklebust out:
296a3d01454STrond Myklebust 	spin_unlock(&inode->i_lock);
297a6b5a28eSDave Wysochanski 	nfs_fscache_invalidate(inode, 0);
2981da177e4SLinus Torvalds }
2991da177e4SLinus Torvalds 
300a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
301d2ceb7e5SBenjamin Coddington static void nfs_set_pageerror(struct address_space *mapping)
302a301b777STrond Myklebust {
3030df68cedSTrond Myklebust 	struct inode *inode = mapping->host;
3040df68cedSTrond Myklebust 
305d2ceb7e5SBenjamin Coddington 	nfs_zap_mapping(mapping->host, mapping);
3060df68cedSTrond Myklebust 	/* Force file size revalidation */
3070df68cedSTrond Myklebust 	spin_lock(&inode->i_lock);
308ac46b3d7STrond Myklebust 	nfs_set_cache_invalid(inode, NFS_INO_REVAL_FORCED |
3090df68cedSTrond Myklebust 					     NFS_INO_REVAL_PAGECACHE |
310ac46b3d7STrond Myklebust 					     NFS_INO_INVALID_SIZE);
3110df68cedSTrond Myklebust 	spin_unlock(&inode->i_lock);
312a301b777STrond Myklebust }
313a301b777STrond Myklebust 
3146fbda89bSTrond Myklebust static void nfs_mapping_set_error(struct page *page, int error)
3156fbda89bSTrond Myklebust {
316b8946d7bSTrond Myklebust 	struct address_space *mapping = page_file_mapping(page);
317b8946d7bSTrond Myklebust 
3186fbda89bSTrond Myklebust 	SetPageError(page);
319b8946d7bSTrond Myklebust 	mapping_set_error(mapping, error);
320b8946d7bSTrond Myklebust 	nfs_set_pageerror(mapping);
3216fbda89bSTrond Myklebust }
3226fbda89bSTrond Myklebust 
323d72ddcbaSWeston Andros Adamson /*
324d72ddcbaSWeston Andros Adamson  * nfs_page_group_search_locked
325d72ddcbaSWeston Andros Adamson  * @head - head request of page group
326d72ddcbaSWeston Andros Adamson  * @page_offset - offset into page
327d72ddcbaSWeston Andros Adamson  *
328d72ddcbaSWeston Andros Adamson  * Search page group with head @head to find a request that contains the
329d72ddcbaSWeston Andros Adamson  * page offset @page_offset.
330d72ddcbaSWeston Andros Adamson  *
331d72ddcbaSWeston Andros Adamson  * Returns a pointer to the first matching nfs request, or NULL if no
332d72ddcbaSWeston Andros Adamson  * match is found.
333d72ddcbaSWeston Andros Adamson  *
334d72ddcbaSWeston Andros Adamson  * Must be called with the page group lock held
335d72ddcbaSWeston Andros Adamson  */
336d72ddcbaSWeston Andros Adamson static struct nfs_page *
337d72ddcbaSWeston Andros Adamson nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset)
338d72ddcbaSWeston Andros Adamson {
339d72ddcbaSWeston Andros Adamson 	struct nfs_page *req;
340d72ddcbaSWeston Andros Adamson 
341d72ddcbaSWeston Andros Adamson 	req = head;
342d72ddcbaSWeston Andros Adamson 	do {
343d72ddcbaSWeston Andros Adamson 		if (page_offset >= req->wb_pgbase &&
344d72ddcbaSWeston Andros Adamson 		    page_offset < (req->wb_pgbase + req->wb_bytes))
345d72ddcbaSWeston Andros Adamson 			return req;
346d72ddcbaSWeston Andros Adamson 
347d72ddcbaSWeston Andros Adamson 		req = req->wb_this_page;
348d72ddcbaSWeston Andros Adamson 	} while (req != head);
349d72ddcbaSWeston Andros Adamson 
350d72ddcbaSWeston Andros Adamson 	return NULL;
351d72ddcbaSWeston Andros Adamson }
352d72ddcbaSWeston Andros Adamson 
353d72ddcbaSWeston Andros Adamson /*
354d72ddcbaSWeston Andros Adamson  * nfs_page_group_covers_page
355d72ddcbaSWeston Andros Adamson  * @head - head request of page group
356d72ddcbaSWeston Andros Adamson  *
357d72ddcbaSWeston Andros Adamson  * Return true if the page group with head @head covers the whole page,
358d72ddcbaSWeston Andros Adamson  * returns false otherwise
359d72ddcbaSWeston Andros Adamson  */
360d72ddcbaSWeston Andros Adamson static bool nfs_page_group_covers_page(struct nfs_page *req)
361d72ddcbaSWeston Andros Adamson {
362d72ddcbaSWeston Andros Adamson 	struct nfs_page *tmp;
363d72ddcbaSWeston Andros Adamson 	unsigned int pos = 0;
364d72ddcbaSWeston Andros Adamson 	unsigned int len = nfs_page_length(req->wb_page);
365d72ddcbaSWeston Andros Adamson 
3661344b7eaSTrond Myklebust 	nfs_page_group_lock(req);
367d72ddcbaSWeston Andros Adamson 
3687e8a30f8STrond Myklebust 	for (;;) {
369d72ddcbaSWeston Andros Adamson 		tmp = nfs_page_group_search_locked(req->wb_head, pos);
3707e8a30f8STrond Myklebust 		if (!tmp)
3717e8a30f8STrond Myklebust 			break;
3727e8a30f8STrond Myklebust 		pos = tmp->wb_pgbase + tmp->wb_bytes;
373d72ddcbaSWeston Andros Adamson 	}
374d72ddcbaSWeston Andros Adamson 
375d72ddcbaSWeston Andros Adamson 	nfs_page_group_unlock(req);
3767e8a30f8STrond Myklebust 	return pos >= len;
377d72ddcbaSWeston Andros Adamson }
378d72ddcbaSWeston Andros Adamson 
3791da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
3801da177e4SLinus Torvalds  * covers the full page.
3811da177e4SLinus Torvalds  */
382d72ddcbaSWeston Andros Adamson static void nfs_mark_uptodate(struct nfs_page *req)
3831da177e4SLinus Torvalds {
384d72ddcbaSWeston Andros Adamson 	if (PageUptodate(req->wb_page))
3851da177e4SLinus Torvalds 		return;
386d72ddcbaSWeston Andros Adamson 	if (!nfs_page_group_covers_page(req))
3871da177e4SLinus Torvalds 		return;
388d72ddcbaSWeston Andros Adamson 	SetPageUptodate(req->wb_page);
3891da177e4SLinus Torvalds }
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
3921da177e4SLinus Torvalds {
393e87b4c7aSNeilBrown 	int ret = 0;
394cca588d6STrond Myklebust 
395e87b4c7aSNeilBrown 	if (wbc->sync_mode == WB_SYNC_ALL)
396e87b4c7aSNeilBrown 		ret = FLUSH_COND_STABLE;
397e87b4c7aSNeilBrown 	return ret;
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds /*
40189a09141SPeter Zijlstra  * NFS congestion control
40289a09141SPeter Zijlstra  */
40389a09141SPeter Zijlstra 
40489a09141SPeter Zijlstra int nfs_congestion_kb;
40589a09141SPeter Zijlstra 
40689a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
40789a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
40889a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
40989a09141SPeter Zijlstra 
410deed85e7STrond Myklebust static void nfs_set_page_writeback(struct page *page)
41189a09141SPeter Zijlstra {
4120db10944SJan Kara 	struct inode *inode = page_file_mapping(page)->host;
4130db10944SJan Kara 	struct nfs_server *nfss = NFS_SERVER(inode);
4145a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
4155a6d41b3STrond Myklebust 
416deed85e7STrond Myklebust 	WARN_ON_ONCE(ret != 0);
41789a09141SPeter Zijlstra 
418277866a0SPeter Zijlstra 	if (atomic_long_inc_return(&nfss->writeback) >
4190db10944SJan Kara 			NFS_CONGESTION_ON_THRESH)
420*6df25e58SNeilBrown 		nfss->write_congested = 1;
42189a09141SPeter Zijlstra }
42289a09141SPeter Zijlstra 
42320633f04SWeston Andros Adamson static void nfs_end_page_writeback(struct nfs_page *req)
42489a09141SPeter Zijlstra {
42520633f04SWeston Andros Adamson 	struct inode *inode = page_file_mapping(req->wb_page)->host;
42689a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
42731a01f09STrond Myklebust 	bool is_done;
42889a09141SPeter Zijlstra 
42931a01f09STrond Myklebust 	is_done = nfs_page_group_sync_on_bit(req, PG_WB_END);
43031a01f09STrond Myklebust 	nfs_unlock_request(req);
43131a01f09STrond Myklebust 	if (!is_done)
43220633f04SWeston Andros Adamson 		return;
43320633f04SWeston Andros Adamson 
43420633f04SWeston Andros Adamson 	end_page_writeback(req->wb_page);
435c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
436*6df25e58SNeilBrown 		nfss->write_congested = 0;
43789a09141SPeter Zijlstra }
43889a09141SPeter Zijlstra 
439d4581383SWeston Andros Adamson /*
440d4581383SWeston Andros Adamson  * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests
441d4581383SWeston Andros Adamson  *
442d4581383SWeston Andros Adamson  * @destroy_list - request list (using wb_this_page) terminated by @old_head
443d4581383SWeston Andros Adamson  * @old_head - the old head of the list
444d4581383SWeston Andros Adamson  *
445d4581383SWeston Andros Adamson  * All subrequests must be locked and removed from all lists, so at this point
446d4581383SWeston Andros Adamson  * they are only "active" in this function, and possibly in nfs_wait_on_request
447d4581383SWeston Andros Adamson  * with a reference held by some other context.
448d4581383SWeston Andros Adamson  */
449d4581383SWeston Andros Adamson static void
450d4581383SWeston Andros Adamson nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list,
451b66aaa8dSTrond Myklebust 				 struct nfs_page *old_head,
452b66aaa8dSTrond Myklebust 				 struct inode *inode)
453d4581383SWeston Andros Adamson {
454d4581383SWeston Andros Adamson 	while (destroy_list) {
455d4581383SWeston Andros Adamson 		struct nfs_page *subreq = destroy_list;
456d4581383SWeston Andros Adamson 
457d4581383SWeston Andros Adamson 		destroy_list = (subreq->wb_this_page == old_head) ?
458d4581383SWeston Andros Adamson 				   NULL : subreq->wb_this_page;
459d4581383SWeston Andros Adamson 
46008ca8b21STrond Myklebust 		/* Note: lock subreq in order to change subreq->wb_head */
46108ca8b21STrond Myklebust 		nfs_page_set_headlock(subreq);
462d4581383SWeston Andros Adamson 		WARN_ON_ONCE(old_head != subreq->wb_head);
463d4581383SWeston Andros Adamson 
464d4581383SWeston Andros Adamson 		/* make sure old group is not used */
465d4581383SWeston Andros Adamson 		subreq->wb_this_page = subreq;
46608ca8b21STrond Myklebust 		subreq->wb_head = subreq;
467d4581383SWeston Andros Adamson 
468902a4c00STrond Myklebust 		clear_bit(PG_REMOVE, &subreq->wb_flags);
469902a4c00STrond Myklebust 
4705b2b5187STrond Myklebust 		/* Note: races with nfs_page_group_destroy() */
4715b2b5187STrond Myklebust 		if (!kref_read(&subreq->wb_kref)) {
4725b2b5187STrond Myklebust 			/* Check if we raced with nfs_page_group_destroy() */
47308ca8b21STrond Myklebust 			if (test_and_clear_bit(PG_TEARDOWN, &subreq->wb_flags)) {
47408ca8b21STrond Myklebust 				nfs_page_clear_headlock(subreq);
4755b2b5187STrond Myklebust 				nfs_free_request(subreq);
47608ca8b21STrond Myklebust 			} else
47708ca8b21STrond Myklebust 				nfs_page_clear_headlock(subreq);
4785b2b5187STrond Myklebust 			continue;
4795b2b5187STrond Myklebust 		}
48008ca8b21STrond Myklebust 		nfs_page_clear_headlock(subreq);
481d4581383SWeston Andros Adamson 
482add42de3STrond Myklebust 		nfs_release_request(old_head);
4835b2b5187STrond Myklebust 
484b66aaa8dSTrond Myklebust 		if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags)) {
485d4581383SWeston Andros Adamson 			nfs_release_request(subreq);
486a6b6d5b8STrond Myklebust 			atomic_long_dec(&NFS_I(inode)->nrequests);
487d4581383SWeston Andros Adamson 		}
4885b2b5187STrond Myklebust 
4895b2b5187STrond Myklebust 		/* subreq is now totally disconnected from page group or any
4905b2b5187STrond Myklebust 		 * write / commit lists. last chance to wake any waiters */
4915b2b5187STrond Myklebust 		nfs_unlock_and_release_request(subreq);
492d4581383SWeston Andros Adamson 	}
493d4581383SWeston Andros Adamson }
494d4581383SWeston Andros Adamson 
495d4581383SWeston Andros Adamson /*
496e00ed89dSTrond Myklebust  * nfs_join_page_group - destroy subrequests of the head req
497e00ed89dSTrond Myklebust  * @head: the page used to lookup the "page group" of nfs_page structures
498e00ed89dSTrond Myklebust  * @inode: Inode to which the request belongs.
499d4581383SWeston Andros Adamson  *
500d4581383SWeston Andros Adamson  * This function joins all sub requests to the head request by first
501d4581383SWeston Andros Adamson  * locking all requests in the group, cancelling any pending operations
502d4581383SWeston Andros Adamson  * and finally updating the head request to cover the whole range covered by
503d4581383SWeston Andros Adamson  * the (former) group.  All subrequests are removed from any write or commit
504d4581383SWeston Andros Adamson  * lists, unlinked from the group and destroyed.
505d4581383SWeston Andros Adamson  */
506ed5d588fSTrond Myklebust void
507e00ed89dSTrond Myklebust nfs_join_page_group(struct nfs_page *head, struct inode *inode)
508d4581383SWeston Andros Adamson {
509e00ed89dSTrond Myklebust 	struct nfs_page *subreq;
510d4581383SWeston Andros Adamson 	struct nfs_page *destroy_list = NULL;
511a62f8e3bSTrond Myklebust 	unsigned int pgbase, off, bytes;
512a62f8e3bSTrond Myklebust 
513a62f8e3bSTrond Myklebust 	pgbase = head->wb_pgbase;
514a62f8e3bSTrond Myklebust 	bytes = head->wb_bytes;
515a62f8e3bSTrond Myklebust 	off = head->wb_offset;
516a0e265bcSTrond Myklebust 	for (subreq = head->wb_this_page; subreq != head;
517a0e265bcSTrond Myklebust 			subreq = subreq->wb_this_page) {
518a62f8e3bSTrond Myklebust 		/* Subrequests should always form a contiguous range */
519a62f8e3bSTrond Myklebust 		if (pgbase > subreq->wb_pgbase) {
520a62f8e3bSTrond Myklebust 			off -= pgbase - subreq->wb_pgbase;
521a62f8e3bSTrond Myklebust 			bytes += pgbase - subreq->wb_pgbase;
522a62f8e3bSTrond Myklebust 			pgbase = subreq->wb_pgbase;
523a62f8e3bSTrond Myklebust 		}
524a62f8e3bSTrond Myklebust 		bytes = max(subreq->wb_pgbase + subreq->wb_bytes
525a62f8e3bSTrond Myklebust 				- pgbase, bytes);
5261bd5d6d0STrond Myklebust 	}
5271bd5d6d0STrond Myklebust 
528a62f8e3bSTrond Myklebust 	/* Set the head request's range to cover the former page group */
529a62f8e3bSTrond Myklebust 	head->wb_pgbase = pgbase;
530a62f8e3bSTrond Myklebust 	head->wb_bytes = bytes;
531a62f8e3bSTrond Myklebust 	head->wb_offset = off;
532d4581383SWeston Andros Adamson 
533d4581383SWeston Andros Adamson 	/* Now that all requests are locked, make sure they aren't on any list.
534d4581383SWeston Andros Adamson 	 * Commit list removal accounting is done after locks are dropped */
535d4581383SWeston Andros Adamson 	subreq = head;
536d4581383SWeston Andros Adamson 	do {
537411a99adSWeston Andros Adamson 		nfs_clear_request_commit(subreq);
538d4581383SWeston Andros Adamson 		subreq = subreq->wb_this_page;
539d4581383SWeston Andros Adamson 	} while (subreq != head);
540d4581383SWeston Andros Adamson 
541d4581383SWeston Andros Adamson 	/* unlink subrequests from head, destroy them later */
542d4581383SWeston Andros Adamson 	if (head->wb_this_page != head) {
543d4581383SWeston Andros Adamson 		/* destroy list will be terminated by head */
544d4581383SWeston Andros Adamson 		destroy_list = head->wb_this_page;
545d4581383SWeston Andros Adamson 		head->wb_this_page = head;
546d4581383SWeston Andros Adamson 	}
547d4581383SWeston Andros Adamson 
548b66aaa8dSTrond Myklebust 	nfs_destroy_unlinked_subrequests(destroy_list, head, inode);
549b5bab9bfSTrond Myklebust }
550b5bab9bfSTrond Myklebust 
551e00ed89dSTrond Myklebust /*
552e00ed89dSTrond Myklebust  * nfs_lock_and_join_requests - join all subreqs to the head req
553e00ed89dSTrond Myklebust  * @page: the page used to lookup the "page group" of nfs_page structures
554e00ed89dSTrond Myklebust  *
555e00ed89dSTrond Myklebust  * This function joins all sub requests to the head request by first
556e00ed89dSTrond Myklebust  * locking all requests in the group, cancelling any pending operations
557e00ed89dSTrond Myklebust  * and finally updating the head request to cover the whole range covered by
558e00ed89dSTrond Myklebust  * the (former) group.  All subrequests are removed from any write or commit
559e00ed89dSTrond Myklebust  * lists, unlinked from the group and destroyed.
560e00ed89dSTrond Myklebust  *
561e00ed89dSTrond Myklebust  * Returns a locked, referenced pointer to the head request - which after
562e00ed89dSTrond Myklebust  * this call is guaranteed to be the only request associated with the page.
563e00ed89dSTrond Myklebust  * Returns NULL if no requests are found for @page, or a ERR_PTR if an
564e00ed89dSTrond Myklebust  * error was encountered.
565e00ed89dSTrond Myklebust  */
566e00ed89dSTrond Myklebust static struct nfs_page *
567e00ed89dSTrond Myklebust nfs_lock_and_join_requests(struct page *page)
568e00ed89dSTrond Myklebust {
569e00ed89dSTrond Myklebust 	struct inode *inode = page_file_mapping(page)->host;
570e00ed89dSTrond Myklebust 	struct nfs_page *head;
571e00ed89dSTrond Myklebust 	int ret;
572e00ed89dSTrond Myklebust 
573e00ed89dSTrond Myklebust 	/*
574e00ed89dSTrond Myklebust 	 * A reference is taken only on the head request which acts as a
575e00ed89dSTrond Myklebust 	 * reference to the whole page group - the group will not be destroyed
576e00ed89dSTrond Myklebust 	 * until the head reference is released.
577e00ed89dSTrond Myklebust 	 */
578e00ed89dSTrond Myklebust 	head = nfs_find_and_lock_page_request(page);
579e00ed89dSTrond Myklebust 	if (IS_ERR_OR_NULL(head))
580d4581383SWeston Andros Adamson 		return head;
5810671d8f1SMarkus Elfring 
582e00ed89dSTrond Myklebust 	/* lock each request in the page group */
583e00ed89dSTrond Myklebust 	ret = nfs_page_group_lock_subrequests(head);
584e00ed89dSTrond Myklebust 	if (ret < 0) {
5850671d8f1SMarkus Elfring 		nfs_unlock_and_release_request(head);
5860671d8f1SMarkus Elfring 		return ERR_PTR(ret);
587612c9384STrond Myklebust 	}
588074cc1deSTrond Myklebust 
589e00ed89dSTrond Myklebust 	nfs_join_page_group(head, inode);
590e00ed89dSTrond Myklebust 
591e00ed89dSTrond Myklebust 	return head;
592e00ed89dSTrond Myklebust }
593e00ed89dSTrond Myklebust 
5946fbda89bSTrond Myklebust static void nfs_write_error(struct nfs_page *req, int error)
5950bcbf039SPeng Tao {
596861e1671SChuck Lever 	trace_nfs_write_error(req, error);
5976fbda89bSTrond Myklebust 	nfs_mapping_set_error(req->wb_page, error);
59806c9fdf3STrond Myklebust 	nfs_inode_remove_request(req);
5990bcbf039SPeng Tao 	nfs_end_page_writeback(req);
6001f84ccdfSFred Isaman 	nfs_release_request(req);
6010bcbf039SPeng Tao }
6020bcbf039SPeng Tao 
603074cc1deSTrond Myklebust /*
604074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
605074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
606074cc1deSTrond Myklebust  */
607074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
6086d17d653STrond Myklebust 				struct page *page)
609074cc1deSTrond Myklebust {
610074cc1deSTrond Myklebust 	struct nfs_page *req;
611074cc1deSTrond Myklebust 	int ret = 0;
612074cc1deSTrond Myklebust 
6136d17d653STrond Myklebust 	req = nfs_lock_and_join_requests(page);
614074cc1deSTrond Myklebust 	if (!req)
615074cc1deSTrond Myklebust 		goto out;
616074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
617074cc1deSTrond Myklebust 	if (IS_ERR(req))
618074cc1deSTrond Myklebust 		goto out;
619074cc1deSTrond Myklebust 
620deed85e7STrond Myklebust 	nfs_set_page_writeback(page);
621deed85e7STrond Myklebust 	WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
622074cc1deSTrond Myklebust 
623a6598813STrond Myklebust 	/* If there is a fatal error that covers this write, just exit */
62496c41455STrond Myklebust 	ret = pgio->pg_error;
62596c41455STrond Myklebust 	if (nfs_error_is_fatal_on_server(ret))
626a6598813STrond Myklebust 		goto out_launder;
627a6598813STrond Myklebust 
62896c41455STrond Myklebust 	ret = 0;
629f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
630074cc1deSTrond Myklebust 		ret = pgio->pg_error;
6310bcbf039SPeng Tao 		/*
632c373fff7STrond Myklebust 		 * Remove the problematic req upon fatal errors on the server
6330bcbf039SPeng Tao 		 */
6340bcbf039SPeng Tao 		if (nfs_error_is_fatal(ret)) {
635c373fff7STrond Myklebust 			if (nfs_error_is_fatal_on_server(ret))
636a6598813STrond Myklebust 				goto out_launder;
6378fc75bedSTrond Myklebust 		} else
6380bcbf039SPeng Tao 			ret = -EAGAIN;
6398fc75bedSTrond Myklebust 		nfs_redirty_request(req);
64096c41455STrond Myklebust 		pgio->pg_error = 0;
64140f90271STrond Myklebust 	} else
64240f90271STrond Myklebust 		nfs_add_stats(page_file_mapping(page)->host,
64340f90271STrond Myklebust 				NFSIOS_WRITEPAGES, 1);
644074cc1deSTrond Myklebust out:
645074cc1deSTrond Myklebust 	return ret;
646a6598813STrond Myklebust out_launder:
6476fbda89bSTrond Myklebust 	nfs_write_error(req, ret);
64814bebe3cSTrond Myklebust 	return 0;
649e261f51fSTrond Myklebust }
650e261f51fSTrond Myklebust 
651d6c843b9SPeng Tao static int nfs_do_writepage(struct page *page, struct writeback_control *wbc,
652c373fff7STrond Myklebust 			    struct nfs_pageio_descriptor *pgio)
653f758c885STrond Myklebust {
654cfb506e1STrond Myklebust 	int ret;
655f758c885STrond Myklebust 
6568cd79788SHuang Ying 	nfs_pageio_cond_complete(pgio, page_index(page));
6576d17d653STrond Myklebust 	ret = nfs_page_async_flush(pgio, page);
658cfb506e1STrond Myklebust 	if (ret == -EAGAIN) {
659cfb506e1STrond Myklebust 		redirty_page_for_writepage(wbc, page);
66096c41455STrond Myklebust 		ret = AOP_WRITEPAGE_ACTIVATE;
661cfb506e1STrond Myklebust 	}
662cfb506e1STrond Myklebust 	return ret;
663f758c885STrond Myklebust }
664f758c885STrond Myklebust 
665e261f51fSTrond Myklebust /*
6661da177e4SLinus Torvalds  * Write an mmapped page to the server.
6671da177e4SLinus Torvalds  */
668d6c843b9SPeng Tao static int nfs_writepage_locked(struct page *page,
669c373fff7STrond Myklebust 				struct writeback_control *wbc)
6701da177e4SLinus Torvalds {
671f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
67240f90271STrond Myklebust 	struct inode *inode = page_file_mapping(page)->host;
673e261f51fSTrond Myklebust 	int err;
6741da177e4SLinus Torvalds 
675*6df25e58SNeilBrown 	if (wbc->sync_mode == WB_SYNC_NONE &&
676*6df25e58SNeilBrown 	    NFS_SERVER(inode)->write_congested)
677*6df25e58SNeilBrown 		return AOP_WRITEPAGE_ACTIVATE;
678*6df25e58SNeilBrown 
67940f90271STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
680811ed92eSTrond Myklebust 	nfs_pageio_init_write(&pgio, inode, 0,
681a20c93e3SChristoph Hellwig 				false, &nfs_async_write_completion_ops);
682c373fff7STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
68396c41455STrond Myklebust 	pgio.pg_error = 0;
684f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
685f758c885STrond Myklebust 	if (err < 0)
6864d770ccfSTrond Myklebust 		return err;
68796c41455STrond Myklebust 	if (nfs_error_is_fatal(pgio.pg_error))
688f758c885STrond Myklebust 		return pgio.pg_error;
689f758c885STrond Myklebust 	return 0;
6904d770ccfSTrond Myklebust }
6914d770ccfSTrond Myklebust 
6924d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
6934d770ccfSTrond Myklebust {
694f758c885STrond Myklebust 	int ret;
6954d770ccfSTrond Myklebust 
696c373fff7STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
69796c41455STrond Myklebust 	if (ret != AOP_WRITEPAGE_ACTIVATE)
6981da177e4SLinus Torvalds 		unlock_page(page);
699f758c885STrond Myklebust 	return ret;
700f758c885STrond Myklebust }
701f758c885STrond Myklebust 
702f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
703f758c885STrond Myklebust {
704f758c885STrond Myklebust 	int ret;
705f758c885STrond Myklebust 
706c373fff7STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
70796c41455STrond Myklebust 	if (ret != AOP_WRITEPAGE_ACTIVATE)
708f758c885STrond Myklebust 		unlock_page(page);
709f758c885STrond Myklebust 	return ret;
7101da177e4SLinus Torvalds }
7111da177e4SLinus Torvalds 
712919e3bd9STrond Myklebust static void nfs_io_completion_commit(void *inode)
713919e3bd9STrond Myklebust {
714919e3bd9STrond Myklebust 	nfs_commit_inode(inode, 0);
715919e3bd9STrond Myklebust }
716919e3bd9STrond Myklebust 
7171da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
7181da177e4SLinus Torvalds {
7191da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
720c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
721ed7bcdb3STrond Myklebust 	struct nfs_io_completion *ioc = NULL;
722ed7bcdb3STrond Myklebust 	unsigned int mntflags = NFS_SERVER(inode)->flags;
723ed7bcdb3STrond Myklebust 	int priority = 0;
7241da177e4SLinus Torvalds 	int err;
7251da177e4SLinus Torvalds 
726*6df25e58SNeilBrown 	if (wbc->sync_mode == WB_SYNC_NONE &&
727*6df25e58SNeilBrown 	    NFS_SERVER(inode)->write_congested)
728*6df25e58SNeilBrown 		return 0;
729*6df25e58SNeilBrown 
73091d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
73191d5b470SChuck Lever 
732ed7bcdb3STrond Myklebust 	if (!(mntflags & NFS_MOUNT_WRITE_EAGER) || wbc->for_kupdate ||
733ed7bcdb3STrond Myklebust 	    wbc->for_background || wbc->for_sync || wbc->for_reclaim) {
7342b17d725STrond Myklebust 		ioc = nfs_io_completion_alloc(GFP_KERNEL);
735919e3bd9STrond Myklebust 		if (ioc)
736ed7bcdb3STrond Myklebust 			nfs_io_completion_init(ioc, nfs_io_completion_commit,
737ed7bcdb3STrond Myklebust 					       inode);
738ed7bcdb3STrond Myklebust 		priority = wb_priority(wbc);
739ed7bcdb3STrond Myklebust 	}
740919e3bd9STrond Myklebust 
741ed7bcdb3STrond Myklebust 	nfs_pageio_init_write(&pgio, inode, priority, false,
742a20c93e3SChristoph Hellwig 				&nfs_async_write_completion_ops);
743919e3bd9STrond Myklebust 	pgio.pg_io_completion = ioc;
744f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
74596c41455STrond Myklebust 	pgio.pg_error = 0;
746c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
747919e3bd9STrond Myklebust 	nfs_io_completion_put(ioc);
74872cb77f4STrond Myklebust 
749f758c885STrond Myklebust 	if (err < 0)
75072cb77f4STrond Myklebust 		goto out_err;
75172cb77f4STrond Myklebust 	err = pgio.pg_error;
75296c41455STrond Myklebust 	if (nfs_error_is_fatal(err))
75372cb77f4STrond Myklebust 		goto out_err;
754c63c7b05STrond Myklebust 	return 0;
75572cb77f4STrond Myklebust out_err:
75672cb77f4STrond Myklebust 	return err;
7571da177e4SLinus Torvalds }
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds /*
7601da177e4SLinus Torvalds  * Insert a write request into an inode
7611da177e4SLinus Torvalds  */
762d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
7631da177e4SLinus Torvalds {
7644b9bb25bSTrond Myklebust 	struct address_space *mapping = page_file_mapping(req->wb_page);
7651da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
766e7d39069STrond Myklebust 
7672bfc6e56SWeston Andros Adamson 	WARN_ON_ONCE(req->wb_this_page != req);
7682bfc6e56SWeston Andros Adamson 
769e7d39069STrond Myklebust 	/* Lock the request! */
7707ad84aa9STrond Myklebust 	nfs_lock_request(req);
771e7d39069STrond Myklebust 
77229418aa4SMel Gorman 	/*
77329418aa4SMel Gorman 	 * Swap-space should not get truncated. Hence no need to plug the race
77429418aa4SMel Gorman 	 * with invalidate/truncate.
77529418aa4SMel Gorman 	 */
7764b9bb25bSTrond Myklebust 	spin_lock(&mapping->private_lock);
77729418aa4SMel Gorman 	if (likely(!PageSwapCache(req->wb_page))) {
7782df485a7STrond Myklebust 		set_bit(PG_MAPPED, &req->wb_flags);
779deb7d638STrond Myklebust 		SetPagePrivate(req->wb_page);
780277459d2STrond Myklebust 		set_page_private(req->wb_page, (unsigned long)req);
78129418aa4SMel Gorman 	}
7824b9bb25bSTrond Myklebust 	spin_unlock(&mapping->private_lock);
783a6b6d5b8STrond Myklebust 	atomic_long_inc(&nfsi->nrequests);
78417089a29SWeston Andros Adamson 	/* this a head request for a page group - mark it as having an
785cb1410c7SWeston Andros Adamson 	 * extra reference so sub groups can follow suit.
786cb1410c7SWeston Andros Adamson 	 * This flag also informs pgio layer when to bump nrequests when
787cb1410c7SWeston Andros Adamson 	 * adding subrequests. */
78817089a29SWeston Andros Adamson 	WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags));
789c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
7901da177e4SLinus Torvalds }
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds /*
79389a09141SPeter Zijlstra  * Remove a write request from an inode
7941da177e4SLinus Torvalds  */
7951da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
7961da177e4SLinus Torvalds {
7974b9bb25bSTrond Myklebust 	struct address_space *mapping = page_file_mapping(req->wb_page);
7984b9bb25bSTrond Myklebust 	struct inode *inode = mapping->host;
7991da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
80020633f04SWeston Andros Adamson 	struct nfs_page *head;
80120633f04SWeston Andros Adamson 
80220633f04SWeston Andros Adamson 	if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) {
80320633f04SWeston Andros Adamson 		head = req->wb_head;
8041da177e4SLinus Torvalds 
8054b9bb25bSTrond Myklebust 		spin_lock(&mapping->private_lock);
80667911c8fSAnna Schumaker 		if (likely(head->wb_page && !PageSwapCache(head->wb_page))) {
80720633f04SWeston Andros Adamson 			set_page_private(head->wb_page, 0);
80820633f04SWeston Andros Adamson 			ClearPagePrivate(head->wb_page);
80920633f04SWeston Andros Adamson 			clear_bit(PG_MAPPED, &head->wb_flags);
81029418aa4SMel Gorman 		}
8114b9bb25bSTrond Myklebust 		spin_unlock(&mapping->private_lock);
81220633f04SWeston Andros Adamson 	}
81317089a29SWeston Andros Adamson 
81433ea5aaaSZhangXiaoxu 	if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags)) {
8151da177e4SLinus Torvalds 		nfs_release_request(req);
81633ea5aaaSZhangXiaoxu 		atomic_long_dec(&nfsi->nrequests);
81733ea5aaaSZhangXiaoxu 	}
8181da177e4SLinus Torvalds }
8191da177e4SLinus Torvalds 
82061822ab5STrond Myklebust static void
8216d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
82261822ab5STrond Myklebust {
82367911c8fSAnna Schumaker 	if (req->wb_page)
82461822ab5STrond Myklebust 		__set_page_dirty_nobuffers(req->wb_page);
82561822ab5STrond Myklebust }
82661822ab5STrond Myklebust 
8273a3908c8STrond Myklebust /*
8283a3908c8STrond Myklebust  * nfs_page_search_commits_for_head_request_locked
8293a3908c8STrond Myklebust  *
8303a3908c8STrond Myklebust  * Search through commit lists on @inode for the head request for @page.
8313a3908c8STrond Myklebust  * Must be called while holding the inode (which is cinfo) lock.
8323a3908c8STrond Myklebust  *
8333a3908c8STrond Myklebust  * Returns the head request if found, or NULL if not found.
8343a3908c8STrond Myklebust  */
8353a3908c8STrond Myklebust static struct nfs_page *
8363a3908c8STrond Myklebust nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
8373a3908c8STrond Myklebust 						struct page *page)
8383a3908c8STrond Myklebust {
8393a3908c8STrond Myklebust 	struct nfs_page *freq, *t;
8403a3908c8STrond Myklebust 	struct nfs_commit_info cinfo;
8413a3908c8STrond Myklebust 	struct inode *inode = &nfsi->vfs_inode;
8423a3908c8STrond Myklebust 
8433a3908c8STrond Myklebust 	nfs_init_cinfo_from_inode(&cinfo, inode);
8443a3908c8STrond Myklebust 
8453a3908c8STrond Myklebust 	/* search through pnfs commit lists */
8463a3908c8STrond Myklebust 	freq = pnfs_search_commit_reqs(inode, &cinfo, page);
8473a3908c8STrond Myklebust 	if (freq)
8483a3908c8STrond Myklebust 		return freq->wb_head;
8493a3908c8STrond Myklebust 
8503a3908c8STrond Myklebust 	/* Linearly search the commit list for the correct request */
8513a3908c8STrond Myklebust 	list_for_each_entry_safe(freq, t, &cinfo.mds->list, wb_list) {
8523a3908c8STrond Myklebust 		if (freq->wb_page == page)
8533a3908c8STrond Myklebust 			return freq->wb_head;
8543a3908c8STrond Myklebust 	}
8553a3908c8STrond Myklebust 
8563a3908c8STrond Myklebust 	return NULL;
8573a3908c8STrond Myklebust }
8583a3908c8STrond Myklebust 
8598dd37758STrond Myklebust /**
86086d80f97STrond Myklebust  * nfs_request_add_commit_list_locked - add request to a commit list
86186d80f97STrond Myklebust  * @req: pointer to a struct nfs_page
86286d80f97STrond Myklebust  * @dst: commit list head
86386d80f97STrond Myklebust  * @cinfo: holds list lock and accounting info
86486d80f97STrond Myklebust  *
86586d80f97STrond Myklebust  * This sets the PG_CLEAN bit, updates the cinfo count of
86686d80f97STrond Myklebust  * number of outstanding requests requiring a commit as well as
86786d80f97STrond Myklebust  * the MM page stats.
86886d80f97STrond Myklebust  *
869e824f99aSTrond Myklebust  * The caller must hold NFS_I(cinfo->inode)->commit_mutex, and the
870e824f99aSTrond Myklebust  * nfs_page lock.
87186d80f97STrond Myklebust  */
87286d80f97STrond Myklebust void
87386d80f97STrond Myklebust nfs_request_add_commit_list_locked(struct nfs_page *req, struct list_head *dst,
87486d80f97STrond Myklebust 			    struct nfs_commit_info *cinfo)
87586d80f97STrond Myklebust {
87686d80f97STrond Myklebust 	set_bit(PG_CLEAN, &req->wb_flags);
87786d80f97STrond Myklebust 	nfs_list_add_request(req, dst);
8785cb953d4STrond Myklebust 	atomic_long_inc(&cinfo->mds->ncommit);
87986d80f97STrond Myklebust }
88086d80f97STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked);
88186d80f97STrond Myklebust 
88286d80f97STrond Myklebust /**
8838dd37758STrond Myklebust  * nfs_request_add_commit_list - add request to a commit list
8848dd37758STrond Myklebust  * @req: pointer to a struct nfs_page
885ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
8868dd37758STrond Myklebust  *
887ea2cf228SFred Isaman  * This sets the PG_CLEAN bit, updates the cinfo count of
8888dd37758STrond Myklebust  * number of outstanding requests requiring a commit as well as
8898dd37758STrond Myklebust  * the MM page stats.
8908dd37758STrond Myklebust  *
891ea2cf228SFred Isaman  * The caller must _not_ hold the cinfo->lock, but must be
8928dd37758STrond Myklebust  * holding the nfs_page lock.
8938dd37758STrond Myklebust  */
8948dd37758STrond Myklebust void
8956272dcc6SAnna Schumaker nfs_request_add_commit_list(struct nfs_page *req, struct nfs_commit_info *cinfo)
8968dd37758STrond Myklebust {
897e824f99aSTrond Myklebust 	mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
8986272dcc6SAnna Schumaker 	nfs_request_add_commit_list_locked(req, &cinfo->mds->list, cinfo);
899e824f99aSTrond Myklebust 	mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
90067911c8fSAnna Schumaker 	if (req->wb_page)
90186d80f97STrond Myklebust 		nfs_mark_page_unstable(req->wb_page, cinfo);
9028dd37758STrond Myklebust }
9038dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
9048dd37758STrond Myklebust 
9058dd37758STrond Myklebust /**
9068dd37758STrond Myklebust  * nfs_request_remove_commit_list - Remove request from a commit list
9078dd37758STrond Myklebust  * @req: pointer to a nfs_page
908ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
9098dd37758STrond Myklebust  *
910ea2cf228SFred Isaman  * This clears the PG_CLEAN bit, and updates the cinfo's count of
9118dd37758STrond Myklebust  * number of outstanding requests requiring a commit
9128dd37758STrond Myklebust  * It does not update the MM page stats.
9138dd37758STrond Myklebust  *
914ea2cf228SFred Isaman  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
9158dd37758STrond Myklebust  */
9168dd37758STrond Myklebust void
917ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req,
918ea2cf228SFred Isaman 			       struct nfs_commit_info *cinfo)
9198dd37758STrond Myklebust {
9208dd37758STrond Myklebust 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
9218dd37758STrond Myklebust 		return;
9228dd37758STrond Myklebust 	nfs_list_remove_request(req);
9235cb953d4STrond Myklebust 	atomic_long_dec(&cinfo->mds->ncommit);
9248dd37758STrond Myklebust }
9258dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
9268dd37758STrond Myklebust 
927ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
928ea2cf228SFred Isaman 				      struct inode *inode)
929ea2cf228SFred Isaman {
930fe238e60SDave Wysochanski 	cinfo->inode = inode;
931ea2cf228SFred Isaman 	cinfo->mds = &NFS_I(inode)->commit_info;
932ea2cf228SFred Isaman 	cinfo->ds = pnfs_get_ds_info(inode);
933b359f9d0SFred Isaman 	cinfo->dreq = NULL;
934f453a54aSFred Isaman 	cinfo->completion_ops = &nfs_commit_completion_ops;
935ea2cf228SFred Isaman }
936ea2cf228SFred Isaman 
937ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
938ea2cf228SFred Isaman 		    struct inode *inode,
939ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq)
940ea2cf228SFred Isaman {
9411763da12SFred Isaman 	if (dreq)
9421763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
9431763da12SFred Isaman 	else
944ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(cinfo, inode);
945ea2cf228SFred Isaman }
946ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo);
9478dd37758STrond Myklebust 
9481da177e4SLinus Torvalds /*
9491da177e4SLinus Torvalds  * Add a request to the inode's commit list.
9501da177e4SLinus Torvalds  */
9511763da12SFred Isaman void
952ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
953b57ff130SWeston Andros Adamson 			struct nfs_commit_info *cinfo, u32 ds_commit_idx)
9541da177e4SLinus Torvalds {
955b57ff130SWeston Andros Adamson 	if (pnfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx))
9568dd37758STrond Myklebust 		return;
9576272dcc6SAnna Schumaker 	nfs_request_add_commit_list(req, cinfo);
9581da177e4SLinus Torvalds }
9598e821cadSTrond Myklebust 
960d6d6dc7cSFred Isaman static void
961d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page)
962e468bae9STrond Myklebust {
9638d92890bSNeilBrown 	dec_node_page_state(page, NR_WRITEBACK);
96493f78d88STejun Heo 	dec_wb_stat(&inode_to_bdi(page_file_mapping(page)->host)->wb,
9658d92890bSNeilBrown 		    WB_WRITEBACK);
966e468bae9STrond Myklebust }
967d6d6dc7cSFred Isaman 
968b5bab9bfSTrond Myklebust /* Called holding the request lock on @req */
9698dd37758STrond Myklebust static void
970d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req)
971d6d6dc7cSFred Isaman {
9728dd37758STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
9739fcd5960STrond Myklebust 		struct nfs_open_context *ctx = nfs_req_openctx(req);
9749fcd5960STrond Myklebust 		struct inode *inode = d_inode(ctx->dentry);
975ea2cf228SFred Isaman 		struct nfs_commit_info cinfo;
976d6d6dc7cSFred Isaman 
977ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(&cinfo, inode);
978e824f99aSTrond Myklebust 		mutex_lock(&NFS_I(inode)->commit_mutex);
979ea2cf228SFred Isaman 		if (!pnfs_clear_request_commit(req, &cinfo)) {
980ea2cf228SFred Isaman 			nfs_request_remove_commit_list(req, &cinfo);
981d6d6dc7cSFred Isaman 		}
982e824f99aSTrond Myklebust 		mutex_unlock(&NFS_I(inode)->commit_mutex);
9838dd37758STrond Myklebust 		nfs_clear_page_commit(req->wb_page);
9848dd37758STrond Myklebust 	}
985e468bae9STrond Myklebust }
986e468bae9STrond Myklebust 
987d45f60c6SWeston Andros Adamson int nfs_write_need_commit(struct nfs_pgio_header *hdr)
9888e821cadSTrond Myklebust {
989c65e6254SWeston Andros Adamson 	if (hdr->verf.committed == NFS_DATA_SYNC)
990d45f60c6SWeston Andros Adamson 		return hdr->lseg == NULL;
991c65e6254SWeston Andros Adamson 	return hdr->verf.committed != NFS_FILE_SYNC;
9928e821cadSTrond Myklebust }
9938e821cadSTrond Myklebust 
994919e3bd9STrond Myklebust static void nfs_async_write_init(struct nfs_pgio_header *hdr)
995919e3bd9STrond Myklebust {
996919e3bd9STrond Myklebust 	nfs_io_completion_get(hdr->io_completion);
997919e3bd9STrond Myklebust }
998919e3bd9STrond Myklebust 
999061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
10006c75dc0dSFred Isaman {
1001ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
10026c75dc0dSFred Isaman 	unsigned long bytes = 0;
10036c75dc0dSFred Isaman 
10046c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
10056c75dc0dSFred Isaman 		goto out;
1006ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
10076c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
10086c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
10096c75dc0dSFred Isaman 
10106c75dc0dSFred Isaman 		bytes += req->wb_bytes;
10116c75dc0dSFred Isaman 		nfs_list_remove_request(req);
10126c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
10136c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
1014861e1671SChuck Lever 			trace_nfs_comp_error(req, hdr->error);
10156fbda89bSTrond Myklebust 			nfs_mapping_set_error(req->wb_page, hdr->error);
10166c75dc0dSFred Isaman 			goto remove_req;
10176c75dc0dSFred Isaman 		}
1018c65e6254SWeston Andros Adamson 		if (nfs_write_need_commit(hdr)) {
101933344e0fSTrond Myklebust 			/* Reset wb_nio, since the write was successful. */
102033344e0fSTrond Myklebust 			req->wb_nio = 0;
1021f79d06f5SAnna Schumaker 			memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
1022b57ff130SWeston Andros Adamson 			nfs_mark_request_commit(req, hdr->lseg, &cinfo,
1023a7d42ddbSWeston Andros Adamson 				hdr->pgio_mirror_idx);
10246c75dc0dSFred Isaman 			goto next;
10256c75dc0dSFred Isaman 		}
10266c75dc0dSFred Isaman remove_req:
10276c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
10286c75dc0dSFred Isaman next:
102920633f04SWeston Andros Adamson 		nfs_end_page_writeback(req);
10303aff4ebbSTrond Myklebust 		nfs_release_request(req);
10316c75dc0dSFred Isaman 	}
10326c75dc0dSFred Isaman out:
1033919e3bd9STrond Myklebust 	nfs_io_completion_put(hdr->io_completion);
10346c75dc0dSFred Isaman 	hdr->release(hdr);
10356c75dc0dSFred Isaman }
10366c75dc0dSFred Isaman 
1037ce59515cSAnna Schumaker unsigned long
1038ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
1039fb8a1f11STrond Myklebust {
10405cb953d4STrond Myklebust 	return atomic_long_read(&cinfo->mds->ncommit);
1041fb8a1f11STrond Myklebust }
1042fb8a1f11STrond Myklebust 
1043e824f99aSTrond Myklebust /* NFS_I(cinfo->inode)->commit_mutex held by caller */
10441763da12SFred Isaman int
1045ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
1046ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
1047d6d6dc7cSFred Isaman {
1048137da553STrond Myklebust 	struct nfs_page *req, *tmp;
1049d6d6dc7cSFred Isaman 	int ret = 0;
1050d6d6dc7cSFred Isaman 
1051137da553STrond Myklebust 	list_for_each_entry_safe(req, tmp, src, wb_list) {
10527ad84aa9STrond Myklebust 		kref_get(&req->wb_kref);
10532ce209c4STrond Myklebust 		if (!nfs_lock_request(req)) {
1054137da553STrond Myklebust 			nfs_release_request(req);
1055137da553STrond Myklebust 			continue;
1056137da553STrond Myklebust 		}
1057ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
10585d2a9d9dSTrond Myklebust 		clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
10598dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
1060d6d6dc7cSFred Isaman 		ret++;
10611763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
1062d6d6dc7cSFred Isaman 			break;
1063e824f99aSTrond Myklebust 		cond_resched();
1064d6d6dc7cSFred Isaman 	}
1065d6d6dc7cSFred Isaman 	return ret;
1066d6d6dc7cSFred Isaman }
10675d2a9d9dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_scan_commit_list);
1068d6d6dc7cSFred Isaman 
10691da177e4SLinus Torvalds /*
10701da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
10711da177e4SLinus Torvalds  * @inode: NFS inode to scan
1072ea2cf228SFred Isaman  * @dst: mds destination list
1073ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
10741da177e4SLinus Torvalds  *
10751da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
10761da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
10771da177e4SLinus Torvalds  */
10781763da12SFred Isaman int
1079ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
1080ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
10811da177e4SLinus Torvalds {
1082d6d6dc7cSFred Isaman 	int ret = 0;
1083fb8a1f11STrond Myklebust 
10845cb953d4STrond Myklebust 	if (!atomic_long_read(&cinfo->mds->ncommit))
10855cb953d4STrond Myklebust 		return 0;
1086e824f99aSTrond Myklebust 	mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
10875cb953d4STrond Myklebust 	if (atomic_long_read(&cinfo->mds->ncommit) > 0) {
10888dd37758STrond Myklebust 		const int max = INT_MAX;
1089d6d6dc7cSFred Isaman 
1090ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
1091ea2cf228SFred Isaman 					   cinfo, max);
1092ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
1093d6d6dc7cSFred Isaman 	}
1094e824f99aSTrond Myklebust 	mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
1095ff778d02STrond Myklebust 	return ret;
10961da177e4SLinus Torvalds }
1097d6d6dc7cSFred Isaman 
10981da177e4SLinus Torvalds /*
1099e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
1100e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
11011da177e4SLinus Torvalds  *
1102e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
1103e7d39069STrond Myklebust  * to disk.
11041da177e4SLinus Torvalds  */
1105e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
1106e7d39069STrond Myklebust 		struct page *page,
1107e7d39069STrond Myklebust 		unsigned int offset,
1108e7d39069STrond Myklebust 		unsigned int bytes)
11091da177e4SLinus Torvalds {
1110e7d39069STrond Myklebust 	struct nfs_page *req;
1111e7d39069STrond Myklebust 	unsigned int rqend;
1112e7d39069STrond Myklebust 	unsigned int end;
11131da177e4SLinus Torvalds 	int error;
1114277459d2STrond Myklebust 
1115e7d39069STrond Myklebust 	end = offset + bytes;
1116e7d39069STrond Myklebust 
1117f6032f21STrond Myklebust 	req = nfs_lock_and_join_requests(page);
1118f6032f21STrond Myklebust 	if (IS_ERR_OR_NULL(req))
1119f6032f21STrond Myklebust 		return req;
11202bfc6e56SWeston Andros Adamson 
1121e7d39069STrond Myklebust 	rqend = req->wb_offset + req->wb_bytes;
1122e7d39069STrond Myklebust 	/*
1123e7d39069STrond Myklebust 	 * Tell the caller to flush out the request if
1124e7d39069STrond Myklebust 	 * the offsets are non-contiguous.
1125e7d39069STrond Myklebust 	 * Note: nfs_flush_incompatible() will already
1126e7d39069STrond Myklebust 	 * have flushed out requests having wrong owners.
1127e7d39069STrond Myklebust 	 */
1128f6032f21STrond Myklebust 	if (offset > rqend || end < req->wb_offset)
1129e7d39069STrond Myklebust 		goto out_flushme;
1130e7d39069STrond Myklebust 
11311da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
11321da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
11331da177e4SLinus Torvalds 		req->wb_offset = offset;
11341da177e4SLinus Torvalds 		req->wb_pgbase = offset;
11351da177e4SLinus Torvalds 	}
11361da177e4SLinus Torvalds 	if (end > rqend)
11371da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
1138e7d39069STrond Myklebust 	else
1139e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
114033344e0fSTrond Myklebust 	req->wb_nio = 0;
1141e7d39069STrond Myklebust 	return req;
1142e7d39069STrond Myklebust out_flushme:
1143f6032f21STrond Myklebust 	/*
1144f6032f21STrond Myklebust 	 * Note: we mark the request dirty here because
1145f6032f21STrond Myklebust 	 * nfs_lock_and_join_requests() cannot preserve
1146f6032f21STrond Myklebust 	 * commit flags, so we have to replay the write.
1147f6032f21STrond Myklebust 	 */
1148f6032f21STrond Myklebust 	nfs_mark_request_dirty(req);
1149f6032f21STrond Myklebust 	nfs_unlock_and_release_request(req);
1150e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
1151f6032f21STrond Myklebust 	return (error < 0) ? ERR_PTR(error) : NULL;
1152e7d39069STrond Myklebust }
11531da177e4SLinus Torvalds 
1154e7d39069STrond Myklebust /*
1155e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
1156e7d39069STrond Myklebust  *
1157e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
1158e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
1159e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
1160e7d39069STrond Myklebust  */
1161e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
1162e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
1163e7d39069STrond Myklebust {
1164d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
1165e7d39069STrond Myklebust 	struct nfs_page	*req;
1166e7d39069STrond Myklebust 
1167e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
1168e7d39069STrond Myklebust 	if (req != NULL)
1169e7d39069STrond Myklebust 		goto out;
117028b1d3f5STrond Myklebust 	req = nfs_create_request(ctx, page, offset, bytes);
1171e7d39069STrond Myklebust 	if (IS_ERR(req))
1172e7d39069STrond Myklebust 		goto out;
1173d6d6dc7cSFred Isaman 	nfs_inode_add_request(inode, req);
1174efc91ed0STrond Myklebust out:
117561e930a9STrond Myklebust 	return req;
11761da177e4SLinus Torvalds }
11771da177e4SLinus Torvalds 
1178e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
1179e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
1180e7d39069STrond Myklebust {
1181e7d39069STrond Myklebust 	struct nfs_page	*req;
1182e7d39069STrond Myklebust 
1183e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
1184e7d39069STrond Myklebust 	if (IS_ERR(req))
1185e7d39069STrond Myklebust 		return PTR_ERR(req);
1186e7d39069STrond Myklebust 	/* Update file length */
1187e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
1188d72ddcbaSWeston Andros Adamson 	nfs_mark_uptodate(req);
1189a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
11901d1afcbcSTrond Myklebust 	nfs_unlock_and_release_request(req);
1191e7d39069STrond Myklebust 	return 0;
1192e7d39069STrond Myklebust }
1193e7d39069STrond Myklebust 
11941da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
11951da177e4SLinus Torvalds {
1196cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
11972a369153STrond Myklebust 	struct nfs_lock_context *l_ctx;
1198bd61e0a9SJeff Layton 	struct file_lock_context *flctx = file_inode(file)->i_flctx;
11991da177e4SLinus Torvalds 	struct nfs_page	*req;
12001a54533eSTrond Myklebust 	int do_flush, status;
12011da177e4SLinus Torvalds 	/*
12021da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
12031da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
12041da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
12051da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
12061da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
12071da177e4SLinus Torvalds 	 * dropped page.
12081da177e4SLinus Torvalds 	 */
12091a54533eSTrond Myklebust 	do {
121084d3a9a9SWeston Andros Adamson 		req = nfs_page_find_head_request(page);
12111a54533eSTrond Myklebust 		if (req == NULL)
12121a54533eSTrond Myklebust 			return 0;
12132a369153STrond Myklebust 		l_ctx = req->wb_lock_context;
1214138a2935STrond Myklebust 		do_flush = req->wb_page != page ||
12159fcd5960STrond Myklebust 			!nfs_match_open_context(nfs_req_openctx(req), ctx);
1216bd61e0a9SJeff Layton 		if (l_ctx && flctx &&
1217bd61e0a9SJeff Layton 		    !(list_empty_careful(&flctx->flc_posix) &&
1218bd61e0a9SJeff Layton 		      list_empty_careful(&flctx->flc_flock))) {
1219d51fdb87SNeilBrown 			do_flush |= l_ctx->lockowner != current->files;
12205263e31eSJeff Layton 		}
12211da177e4SLinus Torvalds 		nfs_release_request(req);
12221a54533eSTrond Myklebust 		if (!do_flush)
12231a54533eSTrond Myklebust 			return 0;
1224d56b4ddfSMel Gorman 		status = nfs_wb_page(page_file_mapping(page)->host, page);
12251a54533eSTrond Myklebust 	} while (status == 0);
12261a54533eSTrond Myklebust 	return status;
12271da177e4SLinus Torvalds }
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds /*
1230dc24826bSAndy Adamson  * Avoid buffered writes when a open context credential's key would
1231dc24826bSAndy Adamson  * expire soon.
1232dc24826bSAndy Adamson  *
1233dc24826bSAndy Adamson  * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
1234dc24826bSAndy Adamson  *
1235dc24826bSAndy Adamson  * Return 0 and set a credential flag which triggers the inode to flush
1236dc24826bSAndy Adamson  * and performs  NFS_FILE_SYNC writes if the key will expired within
1237dc24826bSAndy Adamson  * RPC_KEY_EXPIRE_TIMEO.
1238dc24826bSAndy Adamson  */
1239dc24826bSAndy Adamson int
1240dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode)
1241dc24826bSAndy Adamson {
1242dc24826bSAndy Adamson 	struct nfs_open_context *ctx = nfs_file_open_context(filp);
1243dc24826bSAndy Adamson 
1244ddf529eeSNeilBrown 	if (nfs_ctx_key_to_expire(ctx, inode) &&
1245ca05cbaeSTrond Myklebust 	    !rcu_access_pointer(ctx->ll_cred))
1246ddf529eeSNeilBrown 		/* Already expired! */
1247ddf529eeSNeilBrown 		return -EACCES;
1248ddf529eeSNeilBrown 	return 0;
1249dc24826bSAndy Adamson }
1250dc24826bSAndy Adamson 
1251dc24826bSAndy Adamson /*
1252dc24826bSAndy Adamson  * Test if the open context credential key is marked to expire soon.
1253dc24826bSAndy Adamson  */
1254ce52914eSScott Mayhew bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode)
1255dc24826bSAndy Adamson {
1256ce52914eSScott Mayhew 	struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
1257ca05cbaeSTrond Myklebust 	struct rpc_cred *cred, *new, *old = NULL;
1258ddf529eeSNeilBrown 	struct auth_cred acred = {
1259a52458b4SNeilBrown 		.cred = ctx->cred,
1260ddf529eeSNeilBrown 	};
1261ca05cbaeSTrond Myklebust 	bool ret = false;
1262ce52914eSScott Mayhew 
1263ca05cbaeSTrond Myklebust 	rcu_read_lock();
1264ca05cbaeSTrond Myklebust 	cred = rcu_dereference(ctx->ll_cred);
1265ca05cbaeSTrond Myklebust 	if (cred && !(cred->cr_ops->crkey_timeout &&
1266ca05cbaeSTrond Myklebust 		      cred->cr_ops->crkey_timeout(cred)))
1267ca05cbaeSTrond Myklebust 		goto out;
1268ca05cbaeSTrond Myklebust 	rcu_read_unlock();
1269ca05cbaeSTrond Myklebust 
1270ca05cbaeSTrond Myklebust 	new = auth->au_ops->lookup_cred(auth, &acred, 0);
1271ca05cbaeSTrond Myklebust 	if (new == cred) {
1272ca05cbaeSTrond Myklebust 		put_rpccred(new);
1273ddf529eeSNeilBrown 		return true;
1274ca05cbaeSTrond Myklebust 	}
1275ca05cbaeSTrond Myklebust 	if (IS_ERR_OR_NULL(new)) {
1276ca05cbaeSTrond Myklebust 		new = NULL;
1277ca05cbaeSTrond Myklebust 		ret = true;
1278ca05cbaeSTrond Myklebust 	} else if (new->cr_ops->crkey_timeout &&
1279ca05cbaeSTrond Myklebust 		   new->cr_ops->crkey_timeout(new))
1280ca05cbaeSTrond Myklebust 		ret = true;
1281ca05cbaeSTrond Myklebust 
1282ca05cbaeSTrond Myklebust 	rcu_read_lock();
1283ca05cbaeSTrond Myklebust 	old = rcu_dereference_protected(xchg(&ctx->ll_cred,
1284ca05cbaeSTrond Myklebust 					     RCU_INITIALIZER(new)), 1);
1285ca05cbaeSTrond Myklebust out:
1286ca05cbaeSTrond Myklebust 	rcu_read_unlock();
1287ca05cbaeSTrond Myklebust 	put_rpccred(old);
1288ca05cbaeSTrond Myklebust 	return ret;
1289dc24826bSAndy Adamson }
1290dc24826bSAndy Adamson 
1291dc24826bSAndy Adamson /*
12925d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
12935d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
12945d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
12955d47a356STrond Myklebust  */
1296fc9dc401STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode,
1297fc9dc401STrond Myklebust 				   unsigned int pagelen)
12985d47a356STrond Myklebust {
1299d529ef83SJeff Layton 	struct nfs_inode *nfsi = NFS_I(inode);
1300d529ef83SJeff Layton 
13018d197a56STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
13028d197a56STrond Myklebust 		goto out;
1303fc9dc401STrond Myklebust 	if (nfsi->cache_validity &
130413c0b082STrond Myklebust 	    (NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_SIZE))
1305d529ef83SJeff Layton 		return false;
13064db72b40SJeff Layton 	smp_rmb();
1307fc9dc401STrond Myklebust 	if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags) && pagelen != 0)
13088d197a56STrond Myklebust 		return false;
13098d197a56STrond Myklebust out:
1310fc9dc401STrond Myklebust 	if (nfsi->cache_validity & NFS_INO_INVALID_DATA && pagelen != 0)
131118dd78c4SScott Mayhew 		return false;
13128d197a56STrond Myklebust 	return PageUptodate(page) != 0;
13135d47a356STrond Myklebust }
13145d47a356STrond Myklebust 
13155263e31eSJeff Layton static bool
13165263e31eSJeff Layton is_whole_file_wrlock(struct file_lock *fl)
13175263e31eSJeff Layton {
13185263e31eSJeff Layton 	return fl->fl_start == 0 && fl->fl_end == OFFSET_MAX &&
13195263e31eSJeff Layton 			fl->fl_type == F_WRLCK;
13205263e31eSJeff Layton }
13215263e31eSJeff Layton 
1322c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or
1323c7559663SScott Mayhew  * if we have the whole file locked for writing), it may be more efficient to
1324c7559663SScott Mayhew  * extend the write to cover the entire page in order to avoid fragmentation
1325c7559663SScott Mayhew  * inefficiencies.
1326c7559663SScott Mayhew  *
1327263b4509SScott Mayhew  * If the file is opened for synchronous writes then we can just skip the rest
1328263b4509SScott Mayhew  * of the checks.
1329c7559663SScott Mayhew  */
1330fc9dc401STrond Myklebust static int nfs_can_extend_write(struct file *file, struct page *page,
1331fc9dc401STrond Myklebust 				struct inode *inode, unsigned int pagelen)
1332c7559663SScott Mayhew {
13335263e31eSJeff Layton 	int ret;
13345263e31eSJeff Layton 	struct file_lock_context *flctx = inode->i_flctx;
13355263e31eSJeff Layton 	struct file_lock *fl;
13365263e31eSJeff Layton 
1337c7559663SScott Mayhew 	if (file->f_flags & O_DSYNC)
1338c7559663SScott Mayhew 		return 0;
1339fc9dc401STrond Myklebust 	if (!nfs_write_pageuptodate(page, inode, pagelen))
1340263b4509SScott Mayhew 		return 0;
1341c7559663SScott Mayhew 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
1342c7559663SScott Mayhew 		return 1;
1343bd61e0a9SJeff Layton 	if (!flctx || (list_empty_careful(&flctx->flc_flock) &&
1344bd61e0a9SJeff Layton 		       list_empty_careful(&flctx->flc_posix)))
13458fa4592aSTrond Myklebust 		return 1;
13465263e31eSJeff Layton 
13475263e31eSJeff Layton 	/* Check to see if there are whole file write locks */
13485263e31eSJeff Layton 	ret = 0;
13496109c850SJeff Layton 	spin_lock(&flctx->flc_lock);
1350bd61e0a9SJeff Layton 	if (!list_empty(&flctx->flc_posix)) {
1351bd61e0a9SJeff Layton 		fl = list_first_entry(&flctx->flc_posix, struct file_lock,
1352bd61e0a9SJeff Layton 					fl_list);
1353bd61e0a9SJeff Layton 		if (is_whole_file_wrlock(fl))
13545263e31eSJeff Layton 			ret = 1;
1355bd61e0a9SJeff Layton 	} else if (!list_empty(&flctx->flc_flock)) {
13565263e31eSJeff Layton 		fl = list_first_entry(&flctx->flc_flock, struct file_lock,
13575263e31eSJeff Layton 					fl_list);
13585263e31eSJeff Layton 		if (fl->fl_type == F_WRLCK)
13595263e31eSJeff Layton 			ret = 1;
13605263e31eSJeff Layton 	}
13616109c850SJeff Layton 	spin_unlock(&flctx->flc_lock);
13625263e31eSJeff Layton 	return ret;
1363c7559663SScott Mayhew }
1364c7559663SScott Mayhew 
13655d47a356STrond Myklebust /*
13661da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
13671da177e4SLinus Torvalds  *
13681da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
13691da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
13701da177e4SLinus Torvalds  */
13711da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
13721da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
13731da177e4SLinus Torvalds {
1374cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
1375d2ceb7e5SBenjamin Coddington 	struct address_space *mapping = page_file_mapping(page);
1376d2ceb7e5SBenjamin Coddington 	struct inode	*inode = mapping->host;
1377fc9dc401STrond Myklebust 	unsigned int	pagelen = nfs_page_length(page);
13781da177e4SLinus Torvalds 	int		status = 0;
13791da177e4SLinus Torvalds 
138091d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
138191d5b470SChuck Lever 
13826de1472fSAl Viro 	dprintk("NFS:       nfs_updatepage(%pD2 %d@%lld)\n",
13836de1472fSAl Viro 		file, count, (long long)(page_file_offset(page) + offset));
13841da177e4SLinus Torvalds 
1385149a4fddSBenjamin Coddington 	if (!count)
1386149a4fddSBenjamin Coddington 		goto out;
1387149a4fddSBenjamin Coddington 
1388fc9dc401STrond Myklebust 	if (nfs_can_extend_write(file, page, inode, pagelen)) {
1389fc9dc401STrond Myklebust 		count = max(count + offset, pagelen);
13901da177e4SLinus Torvalds 		offset = 0;
13911da177e4SLinus Torvalds 	}
13921da177e4SLinus Torvalds 
1393e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
139403fa9e84STrond Myklebust 	if (status < 0)
1395d2ceb7e5SBenjamin Coddington 		nfs_set_pageerror(mapping);
1396149a4fddSBenjamin Coddington out:
139748186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
13981da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
13991da177e4SLinus Torvalds 	return status;
14001da177e4SLinus Torvalds }
14011da177e4SLinus Torvalds 
14023ff7576dSTrond Myklebust static int flush_task_priority(int how)
14031da177e4SLinus Torvalds {
14041da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
14051da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
14061da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
14071da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
14081da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
14091da177e4SLinus Torvalds 	}
14101da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
14111da177e4SLinus Torvalds }
14121da177e4SLinus Torvalds 
1413d45f60c6SWeston Andros Adamson static void nfs_initiate_write(struct nfs_pgio_header *hdr,
1414d45f60c6SWeston Andros Adamson 			       struct rpc_message *msg,
1415abde71f4STom Haynes 			       const struct nfs_rpc_ops *rpc_ops,
14161ed26f33SAnna Schumaker 			       struct rpc_task_setup *task_setup_data, int how)
14171da177e4SLinus Torvalds {
14183ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
14191da177e4SLinus Torvalds 
14201ed26f33SAnna Schumaker 	task_setup_data->priority = priority;
1421fb91fb0eSAnna Schumaker 	rpc_ops->write_setup(hdr, msg, &task_setup_data->rpc_client);
14225bb2a7cbSTrond Myklebust 	trace_nfs_initiate_write(hdr);
1423275acaafSTrond Myklebust }
1424275acaafSTrond Myklebust 
14256d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
14266d884e8fSFred  * call this on each, which will prepare them to be retried on next
14276d884e8fSFred  * writeback using standard nfs.
14286d884e8fSFred  */
14296d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
14306d884e8fSFred {
143133344e0fSTrond Myklebust 	/* Bump the transmission count */
143233344e0fSTrond Myklebust 	req->wb_nio++;
14336d884e8fSFred 	nfs_mark_request_dirty(req);
14349fcd5960STrond Myklebust 	set_bit(NFS_CONTEXT_RESEND_WRITES, &nfs_req_openctx(req)->flags);
143520633f04SWeston Andros Adamson 	nfs_end_page_writeback(req);
14363aff4ebbSTrond Myklebust 	nfs_release_request(req);
14376d884e8fSFred }
14386d884e8fSFred 
1439df3accb8STrond Myklebust static void nfs_async_write_error(struct list_head *head, int error)
14406c75dc0dSFred Isaman {
14416c75dc0dSFred Isaman 	struct nfs_page	*req;
14426c75dc0dSFred Isaman 
14436c75dc0dSFred Isaman 	while (!list_empty(head)) {
14446c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
14456c75dc0dSFred Isaman 		nfs_list_remove_request(req);
14466fbda89bSTrond Myklebust 		if (nfs_error_is_fatal(error))
14476fbda89bSTrond Myklebust 			nfs_write_error(req, error);
14486fbda89bSTrond Myklebust 		else
14496c75dc0dSFred Isaman 			nfs_redirty_request(req);
14506c75dc0dSFred Isaman 	}
14516c75dc0dSFred Isaman }
14526c75dc0dSFred Isaman 
1453dc602dd7STrond Myklebust static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr)
1454dc602dd7STrond Myklebust {
1455df3accb8STrond Myklebust 	nfs_async_write_error(&hdr->pages, 0);
14567be7b3caSTrond Myklebust 	filemap_fdatawrite_range(hdr->inode->i_mapping, hdr->args.offset,
14577be7b3caSTrond Myklebust 			hdr->args.offset + hdr->args.count - 1);
1458dc602dd7STrond Myklebust }
1459dc602dd7STrond Myklebust 
1460061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1461919e3bd9STrond Myklebust 	.init_hdr = nfs_async_write_init,
1462061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1463061ae2edSFred Isaman 	.completion = nfs_write_completion,
1464dc602dd7STrond Myklebust 	.reschedule_io = nfs_async_write_reschedule_io,
1465061ae2edSFred Isaman };
1466061ae2edSFred Isaman 
146757208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1468a20c93e3SChristoph Hellwig 			       struct inode *inode, int ioflags, bool force_mds,
1469061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
14701751c363STrond Myklebust {
1471a20c93e3SChristoph Hellwig 	struct nfs_server *server = NFS_SERVER(inode);
147241d8d5b7SAnna Schumaker 	const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
1473a20c93e3SChristoph Hellwig 
1474a20c93e3SChristoph Hellwig #ifdef CONFIG_NFS_V4_1
1475a20c93e3SChristoph Hellwig 	if (server->pnfs_curr_ld && !force_mds)
1476a20c93e3SChristoph Hellwig 		pg_ops = server->pnfs_curr_ld->pg_write_ops;
1477a20c93e3SChristoph Hellwig #endif
14784a0de55cSAnna Schumaker 	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
14793bde7afdSTrond Myklebust 			server->wsize, ioflags);
14801751c363STrond Myklebust }
1481ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
14821751c363STrond Myklebust 
1483dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1484dce81290STrond Myklebust {
1485a7d42ddbSWeston Andros Adamson 	struct nfs_pgio_mirror *mirror;
1486a7d42ddbSWeston Andros Adamson 
14876f29b9bbSKinglong Mee 	if (pgio->pg_ops && pgio->pg_ops->pg_cleanup)
14886f29b9bbSKinglong Mee 		pgio->pg_ops->pg_cleanup(pgio);
14896f29b9bbSKinglong Mee 
149041d8d5b7SAnna Schumaker 	pgio->pg_ops = &nfs_pgio_rw_ops;
1491a7d42ddbSWeston Andros Adamson 
1492a7d42ddbSWeston Andros Adamson 	nfs_pageio_stop_mirroring(pgio);
1493a7d42ddbSWeston Andros Adamson 
1494a7d42ddbSWeston Andros Adamson 	mirror = &pgio->pg_mirrors[0];
1495a7d42ddbSWeston Andros Adamson 	mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1496dce81290STrond Myklebust }
14971f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1498dce81290STrond Myklebust 
14991da177e4SLinus Torvalds 
15000b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
15010b7c0153SFred Isaman {
15020b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
15030b7c0153SFred Isaman 
15040b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
15050b7c0153SFred Isaman }
15060b7c0153SFred Isaman 
15071f2edbe3STrond Myklebust /*
15081f2edbe3STrond Myklebust  * Special version of should_remove_suid() that ignores capabilities.
15091f2edbe3STrond Myklebust  */
15101f2edbe3STrond Myklebust static int nfs_should_remove_suid(const struct inode *inode)
15111f2edbe3STrond Myklebust {
15121f2edbe3STrond Myklebust 	umode_t mode = inode->i_mode;
15131f2edbe3STrond Myklebust 	int kill = 0;
1514788e7a89STrond Myklebust 
15151f2edbe3STrond Myklebust 	/* suid always must be killed */
15161f2edbe3STrond Myklebust 	if (unlikely(mode & S_ISUID))
15171f2edbe3STrond Myklebust 		kill = ATTR_KILL_SUID;
15181f2edbe3STrond Myklebust 
15191f2edbe3STrond Myklebust 	/*
15201f2edbe3STrond Myklebust 	 * sgid without any exec bits is just a mandatory locking mark; leave
15211f2edbe3STrond Myklebust 	 * it alone.  If some exec bits are set, it's a real sgid; kill it.
15221f2edbe3STrond Myklebust 	 */
15231f2edbe3STrond Myklebust 	if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
15241f2edbe3STrond Myklebust 		kill |= ATTR_KILL_SGID;
15251f2edbe3STrond Myklebust 
15261f2edbe3STrond Myklebust 	if (unlikely(kill && S_ISREG(mode)))
15271f2edbe3STrond Myklebust 		return kill;
15281f2edbe3STrond Myklebust 
15291f2edbe3STrond Myklebust 	return 0;
15301f2edbe3STrond Myklebust }
1531788e7a89STrond Myklebust 
1532a08a8cd3STrond Myklebust static void nfs_writeback_check_extend(struct nfs_pgio_header *hdr,
1533a08a8cd3STrond Myklebust 		struct nfs_fattr *fattr)
1534a08a8cd3STrond Myklebust {
1535a08a8cd3STrond Myklebust 	struct nfs_pgio_args *argp = &hdr->args;
1536a08a8cd3STrond Myklebust 	struct nfs_pgio_res *resp = &hdr->res;
15372b83d3deSTrond Myklebust 	u64 size = argp->offset + resp->count;
1538a08a8cd3STrond Myklebust 
1539a08a8cd3STrond Myklebust 	if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
15402b83d3deSTrond Myklebust 		fattr->size = size;
15412b83d3deSTrond Myklebust 	if (nfs_size_to_loff_t(fattr->size) < i_size_read(hdr->inode)) {
15422b83d3deSTrond Myklebust 		fattr->valid &= ~NFS_ATTR_FATTR_SIZE;
1543a08a8cd3STrond Myklebust 		return;
15442b83d3deSTrond Myklebust 	}
15452b83d3deSTrond Myklebust 	if (size != fattr->size)
1546a08a8cd3STrond Myklebust 		return;
1547a08a8cd3STrond Myklebust 	/* Set attribute barrier */
1548a08a8cd3STrond Myklebust 	nfs_fattr_set_barrier(fattr);
15492b83d3deSTrond Myklebust 	/* ...and update size */
15502b83d3deSTrond Myklebust 	fattr->valid |= NFS_ATTR_FATTR_SIZE;
1551a08a8cd3STrond Myklebust }
1552a08a8cd3STrond Myklebust 
1553a08a8cd3STrond Myklebust void nfs_writeback_update_inode(struct nfs_pgio_header *hdr)
1554a08a8cd3STrond Myklebust {
15552b83d3deSTrond Myklebust 	struct nfs_fattr *fattr = &hdr->fattr;
1556a08a8cd3STrond Myklebust 	struct inode *inode = hdr->inode;
1557a08a8cd3STrond Myklebust 
1558a08a8cd3STrond Myklebust 	spin_lock(&inode->i_lock);
1559a08a8cd3STrond Myklebust 	nfs_writeback_check_extend(hdr, fattr);
1560a08a8cd3STrond Myklebust 	nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
1561a08a8cd3STrond Myklebust 	spin_unlock(&inode->i_lock);
1562a08a8cd3STrond Myklebust }
1563a08a8cd3STrond Myklebust EXPORT_SYMBOL_GPL(nfs_writeback_update_inode);
1564a08a8cd3STrond Myklebust 
15651da177e4SLinus Torvalds /*
15661da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
15671da177e4SLinus Torvalds  */
1568d45f60c6SWeston Andros Adamson static int nfs_writeback_done(struct rpc_task *task,
1569d45f60c6SWeston Andros Adamson 			      struct nfs_pgio_header *hdr,
15700eecb214SAnna Schumaker 			      struct inode *inode)
15711da177e4SLinus Torvalds {
1572788e7a89STrond Myklebust 	int status;
15731da177e4SLinus Torvalds 
1574f551e44fSChuck Lever 	/*
1575f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1576f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1577f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1578f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1579f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1580f551e44fSChuck Lever 	 */
1581d45f60c6SWeston Andros Adamson 	status = NFS_PROTO(inode)->write_done(task, hdr);
1582788e7a89STrond Myklebust 	if (status != 0)
15830eecb214SAnna Schumaker 		return status;
15848224b273SChuck Lever 
1585d45f60c6SWeston Andros Adamson 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count);
15865bb2a7cbSTrond Myklebust 	trace_nfs_writeback_done(task, hdr);
158791d5b470SChuck Lever 
1588d45f60c6SWeston Andros Adamson 	if (hdr->res.verf->committed < hdr->args.stable &&
1589d45f60c6SWeston Andros Adamson 	    task->tk_status >= 0) {
15901da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
15911da177e4SLinus Torvalds 		 * commit data to stable storage even though we
15921da177e4SLinus Torvalds 		 * requested it.
15931da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
15941da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
15951da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
15961da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
15971da177e4SLinus Torvalds 		 */
15981da177e4SLinus Torvalds 		static unsigned long    complain;
15991da177e4SLinus Torvalds 
1600a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
16011da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
16021da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
16031da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1604cd841605SFred Isaman 				NFS_SERVER(inode)->nfs_client->cl_hostname,
1605d45f60c6SWeston Andros Adamson 				hdr->res.verf->committed, hdr->args.stable);
16061da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
16071da177e4SLinus Torvalds 		}
16081da177e4SLinus Torvalds 	}
16091f2edbe3STrond Myklebust 
16101f2edbe3STrond Myklebust 	/* Deal with the suid/sgid bit corner case */
161116e14375STrond Myklebust 	if (nfs_should_remove_suid(inode)) {
161216e14375STrond Myklebust 		spin_lock(&inode->i_lock);
1613720869ebSTrond Myklebust 		nfs_set_cache_invalid(inode, NFS_INO_INVALID_MODE);
161416e14375STrond Myklebust 		spin_unlock(&inode->i_lock);
161516e14375STrond Myklebust 	}
16160eecb214SAnna Schumaker 	return 0;
16170eecb214SAnna Schumaker }
16180eecb214SAnna Schumaker 
16190eecb214SAnna Schumaker /*
16200eecb214SAnna Schumaker  * This function is called when the WRITE call is complete.
16210eecb214SAnna Schumaker  */
1622d45f60c6SWeston Andros Adamson static void nfs_writeback_result(struct rpc_task *task,
1623d45f60c6SWeston Andros Adamson 				 struct nfs_pgio_header *hdr)
16240eecb214SAnna Schumaker {
1625d45f60c6SWeston Andros Adamson 	struct nfs_pgio_args	*argp = &hdr->args;
1626d45f60c6SWeston Andros Adamson 	struct nfs_pgio_res	*resp = &hdr->res;
16271f2edbe3STrond Myklebust 
16281f2edbe3STrond Myklebust 	if (resp->count < argp->count) {
16291da177e4SLinus Torvalds 		static unsigned long    complain;
16301da177e4SLinus Torvalds 
16316c75dc0dSFred Isaman 		/* This a short write! */
1632d45f60c6SWeston Andros Adamson 		nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE);
163391d5b470SChuck Lever 
16341da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
16356c75dc0dSFred Isaman 		if (resp->count == 0) {
16366c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
16376c75dc0dSFred Isaman 				printk(KERN_WARNING
16386c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
16396c75dc0dSFred Isaman 				       argp->count);
16406c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
16416c75dc0dSFred Isaman 			}
1642d45f60c6SWeston Andros Adamson 			nfs_set_pgio_error(hdr, -EIO, argp->offset);
16436c75dc0dSFred Isaman 			task->tk_status = -EIO;
16446c75dc0dSFred Isaman 			return;
16456c75dc0dSFred Isaman 		}
1646f8417b48SKinglong Mee 
1647f8417b48SKinglong Mee 		/* For non rpc-based layout drivers, retry-through-MDS */
1648f8417b48SKinglong Mee 		if (!task->tk_ops) {
1649f8417b48SKinglong Mee 			hdr->pnfs_error = -EAGAIN;
1650f8417b48SKinglong Mee 			return;
1651f8417b48SKinglong Mee 		}
1652f8417b48SKinglong Mee 
16531da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
16541da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
16551da177e4SLinus Torvalds 			/* Resend from where the server left off */
1656d45f60c6SWeston Andros Adamson 			hdr->mds_offset += resp->count;
16571da177e4SLinus Torvalds 			argp->offset += resp->count;
16581da177e4SLinus Torvalds 			argp->pgbase += resp->count;
16591da177e4SLinus Torvalds 			argp->count -= resp->count;
16601da177e4SLinus Torvalds 		} else {
16611da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
16621da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
16631da177e4SLinus Torvalds 			 */
16641da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
16651da177e4SLinus Torvalds 		}
16668c9cb714STrond Myklebust 		resp->count = 0;
16678c9cb714STrond Myklebust 		resp->verf->committed = 0;
1668d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
16691da177e4SLinus Torvalds 	}
16701da177e4SLinus Torvalds }
16711da177e4SLinus Torvalds 
1672af7cf057STrond Myklebust static int wait_on_commit(struct nfs_mds_commit_info *cinfo)
167371d0a611STrond Myklebust {
1674723c921eSPeter Zijlstra 	return wait_var_event_killable(&cinfo->rpcs_out,
1675723c921eSPeter Zijlstra 				       !atomic_read(&cinfo->rpcs_out));
1676af7cf057STrond Myklebust }
1677af7cf057STrond Myklebust 
1678af7cf057STrond Myklebust static void nfs_commit_begin(struct nfs_mds_commit_info *cinfo)
1679af7cf057STrond Myklebust {
1680af7cf057STrond Myklebust 	atomic_inc(&cinfo->rpcs_out);
1681af7cf057STrond Myklebust }
1682af7cf057STrond Myklebust 
1683133a48abSTrond Myklebust bool nfs_commit_end(struct nfs_mds_commit_info *cinfo)
1684af7cf057STrond Myklebust {
1685133a48abSTrond Myklebust 	if (atomic_dec_and_test(&cinfo->rpcs_out)) {
1686723c921eSPeter Zijlstra 		wake_up_var(&cinfo->rpcs_out);
1687133a48abSTrond Myklebust 		return true;
1688133a48abSTrond Myklebust 	}
1689133a48abSTrond Myklebust 	return false;
169071d0a611STrond Myklebust }
169171d0a611STrond Myklebust 
16920b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
16931da177e4SLinus Torvalds {
16940b7c0153SFred Isaman 	put_nfs_open_context(data->context);
16950b7c0153SFred Isaman 	nfs_commit_free(data);
16961da177e4SLinus Torvalds }
1697e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
16981da177e4SLinus Torvalds 
16990b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
1700c36aae9aSPeng Tao 			const struct nfs_rpc_ops *nfs_ops,
17019ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
17029f0ec176SAndy Adamson 			int how, int flags)
17031da177e4SLinus Torvalds {
170407737691STrond Myklebust 	struct rpc_task *task;
17059ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1706bdc7f021STrond Myklebust 	struct rpc_message msg = {
1707bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1708bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
17099ace33cdSFred Isaman 		.rpc_cred = data->cred,
1710bdc7f021STrond Myklebust 	};
171184115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
171207737691STrond Myklebust 		.task = &data->task,
17139ace33cdSFred Isaman 		.rpc_client = clnt,
1714bdc7f021STrond Myklebust 		.rpc_message = &msg,
17159ace33cdSFred Isaman 		.callback_ops = call_ops,
171684115e1cSTrond Myklebust 		.callback_data = data,
1717101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
17184fa7ef69STrond Myklebust 		.flags = RPC_TASK_ASYNC | flags,
17193ff7576dSTrond Myklebust 		.priority = priority,
172084115e1cSTrond Myklebust 	};
1721788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
1722e9ae1ee2SAnna Schumaker 	nfs_ops->commit_setup(data, &msg, &task_setup_data.rpc_client);
17238224b273SChuck Lever 	trace_nfs_initiate_commit(data);
17241da177e4SLinus Torvalds 
1725b4839ebeSKinglong Mee 	dprintk("NFS: initiated commit call\n");
1726bdc7f021STrond Myklebust 
172707737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1728dbae4c73STrond Myklebust 	if (IS_ERR(task))
1729dbae4c73STrond Myklebust 		return PTR_ERR(task);
1730d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1731d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
173207737691STrond Myklebust 	rpc_put_task(task);
1733dbae4c73STrond Myklebust 	return 0;
17341da177e4SLinus Torvalds }
1735e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
17361da177e4SLinus Torvalds 
1737378520b8SPeng Tao static loff_t nfs_get_lwb(struct list_head *head)
1738378520b8SPeng Tao {
1739378520b8SPeng Tao 	loff_t lwb = 0;
1740378520b8SPeng Tao 	struct nfs_page *req;
1741378520b8SPeng Tao 
1742378520b8SPeng Tao 	list_for_each_entry(req, head, wb_list)
1743378520b8SPeng Tao 		if (lwb < (req_offset(req) + req->wb_bytes))
1744378520b8SPeng Tao 			lwb = req_offset(req) + req->wb_bytes;
1745378520b8SPeng Tao 
1746378520b8SPeng Tao 	return lwb;
1747378520b8SPeng Tao }
1748378520b8SPeng Tao 
17491da177e4SLinus Torvalds /*
17509ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
17519ace33cdSFred Isaman  */
17520b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1753988b6dceSFred Isaman 		     struct list_head *head,
1754f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1755f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
17569ace33cdSFred Isaman {
175719573c93STrond Myklebust 	struct nfs_page *first;
175819573c93STrond Myklebust 	struct nfs_open_context *ctx;
175919573c93STrond Myklebust 	struct inode *inode;
17609ace33cdSFred Isaman 
17619ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
17629ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
17639ace33cdSFred Isaman 
176419573c93STrond Myklebust 	if (head)
17659ace33cdSFred Isaman 		list_splice_init(head, &data->pages);
17669ace33cdSFred Isaman 
176719573c93STrond Myklebust 	first = nfs_list_entry(data->pages.next);
176819573c93STrond Myklebust 	ctx = nfs_req_openctx(first);
176919573c93STrond Myklebust 	inode = d_inode(ctx->dentry);
177019573c93STrond Myklebust 
17719ace33cdSFred Isaman 	data->inode	  = inode;
17729fcd5960STrond Myklebust 	data->cred	  = ctx->cred;
1773988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
1774378520b8SPeng Tao 	/* only set lwb for pnfs commit */
1775378520b8SPeng Tao 	if (lseg)
1776378520b8SPeng Tao 		data->lwb = nfs_get_lwb(&data->pages);
17779ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1778f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1779b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
17809ace33cdSFred Isaman 
17819ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
17829ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
17839ace33cdSFred Isaman 	data->args.offset = 0;
17849ace33cdSFred Isaman 	data->args.count  = 0;
17859fcd5960STrond Myklebust 	data->context     = get_nfs_open_context(ctx);
17869ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
17879ace33cdSFred Isaman 	data->res.verf    = &data->verf;
17889ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
1789133a48abSTrond Myklebust 	nfs_commit_begin(cinfo->mds);
17909ace33cdSFred Isaman }
1791e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
17929ace33cdSFred Isaman 
1793e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1794ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1795b57ff130SWeston Andros Adamson 		      struct nfs_commit_info *cinfo,
1796b57ff130SWeston Andros Adamson 		      u32 ds_commit_idx)
179764bfeb49SFred Isaman {
179864bfeb49SFred Isaman 	struct nfs_page *req;
179964bfeb49SFred Isaman 
180064bfeb49SFred Isaman 	while (!list_empty(page_list)) {
180164bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
180264bfeb49SFred Isaman 		nfs_list_remove_request(req);
1803b57ff130SWeston Andros Adamson 		nfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx);
1804487b9b8aSTom Haynes 		if (!cinfo->dreq)
1805487b9b8aSTom Haynes 			nfs_clear_page_commit(req->wb_page);
18061d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
180764bfeb49SFred Isaman 	}
180864bfeb49SFred Isaman }
1809e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
181064bfeb49SFred Isaman 
1811b20135d0STrond Myklebust static void
1812b20135d0STrond Myklebust nfs_commit_resched_write(struct nfs_commit_info *cinfo,
1813b20135d0STrond Myklebust 		struct nfs_page *req)
1814b20135d0STrond Myklebust {
1815b20135d0STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
1816b20135d0STrond Myklebust }
1817b20135d0STrond Myklebust 
18189ace33cdSFred Isaman /*
18191da177e4SLinus Torvalds  * Commit dirty pages
18201da177e4SLinus Torvalds  */
18211da177e4SLinus Torvalds static int
1822ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1823ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
18241da177e4SLinus Torvalds {
18250b7c0153SFred Isaman 	struct nfs_commit_data	*data;
182685e39feeSOlga Kornievskaia 	unsigned short task_flags = 0;
18271da177e4SLinus Torvalds 
1828ade8febdSWeston Andros Adamson 	/* another commit raced with us */
1829ade8febdSWeston Andros Adamson 	if (list_empty(head))
1830ade8febdSWeston Andros Adamson 		return 0;
1831ade8febdSWeston Andros Adamson 
1832518662e0SNeilBrown 	data = nfs_commitdata_alloc(true);
18331da177e4SLinus Torvalds 
18341da177e4SLinus Torvalds 	/* Set up the argument struct */
1835f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
183685e39feeSOlga Kornievskaia 	if (NFS_SERVER(inode)->nfs_client->cl_minorversion)
183785e39feeSOlga Kornievskaia 		task_flags = RPC_TASK_MOVEABLE;
1838c36aae9aSPeng Tao 	return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode),
183985e39feeSOlga Kornievskaia 				   data->mds_ops, how,
184085e39feeSOlga Kornievskaia 				   RPC_TASK_CRED_NOREF | task_flags);
18411da177e4SLinus Torvalds }
18421da177e4SLinus Torvalds 
18431da177e4SLinus Torvalds /*
18441da177e4SLinus Torvalds  * COMMIT call returned
18451da177e4SLinus Torvalds  */
1846788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
18471da177e4SLinus Torvalds {
18480b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
18491da177e4SLinus Torvalds 
1850788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1851c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
18527bdd297eSTrond Myklebust 	trace_nfs_commit_done(task, data);
1853c9d8f89dSTrond Myklebust }
1854c9d8f89dSTrond Myklebust 
1855f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1856c9d8f89dSTrond Myklebust {
1857221203ceSTrond Myklebust 	const struct nfs_writeverf *verf = data->res.verf;
1858c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1859c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1860f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
1861353db796SNeilBrown 	struct nfs_server *nfss;
1862788e7a89STrond Myklebust 
18631da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
18641da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
18651da177e4SLinus Torvalds 		nfs_list_remove_request(req);
186667911c8fSAnna Schumaker 		if (req->wb_page)
1867d6d6dc7cSFred Isaman 			nfs_clear_page_commit(req->wb_page);
18681da177e4SLinus Torvalds 
18691e8968c5SNiels de Vos 		dprintk("NFS:       commit (%s/%llu %d@%lld)",
18709fcd5960STrond Myklebust 			nfs_req_openctx(req)->dentry->d_sb->s_id,
18719fcd5960STrond Myklebust 			(unsigned long long)NFS_FILEID(d_inode(nfs_req_openctx(req)->dentry)),
18721da177e4SLinus Torvalds 			req->wb_bytes,
18731da177e4SLinus Torvalds 			(long long)req_offset(req));
1874c9d8f89dSTrond Myklebust 		if (status < 0) {
18756fbda89bSTrond Myklebust 			if (req->wb_page) {
1876861e1671SChuck Lever 				trace_nfs_commit_error(req, status);
18776fbda89bSTrond Myklebust 				nfs_mapping_set_error(req->wb_page, status);
18781da177e4SLinus Torvalds 				nfs_inode_remove_request(req);
18796fbda89bSTrond Myklebust 			}
1880ddeaa637SJoe Perches 			dprintk_cont(", error = %d\n", status);
18811da177e4SLinus Torvalds 			goto next;
18821da177e4SLinus Torvalds 		}
18831da177e4SLinus Torvalds 
18841da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
18851da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
18861f28476dSTrond Myklebust 		if (nfs_write_match_verf(verf, req)) {
18871da177e4SLinus Torvalds 			/* We have a match */
188838a33101SKinglong Mee 			if (req->wb_page)
18891da177e4SLinus Torvalds 				nfs_inode_remove_request(req);
1890ddeaa637SJoe Perches 			dprintk_cont(" OK\n");
18911da177e4SLinus Torvalds 			goto next;
18921da177e4SLinus Torvalds 		}
18931da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
1894ddeaa637SJoe Perches 		dprintk_cont(" mismatch\n");
18956d884e8fSFred 		nfs_mark_request_dirty(req);
18969fcd5960STrond Myklebust 		set_bit(NFS_CONTEXT_RESEND_WRITES, &nfs_req_openctx(req)->flags);
18971da177e4SLinus Torvalds 	next:
18981d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
18997f1bda44STrond Myklebust 		/* Latency breaker */
19007f1bda44STrond Myklebust 		cond_resched();
19011da177e4SLinus Torvalds 	}
1902353db796SNeilBrown 	nfss = NFS_SERVER(data->inode);
1903353db796SNeilBrown 	if (atomic_long_read(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
1904*6df25e58SNeilBrown 		nfss->write_congested = 0;
1905353db796SNeilBrown 
1906f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1907af7cf057STrond Myklebust 	nfs_commit_end(cinfo.mds);
19085917ce84SFred Isaman }
19095917ce84SFred Isaman 
19105917ce84SFred Isaman static void nfs_commit_release(void *calldata)
19115917ce84SFred Isaman {
19120b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
19135917ce84SFred Isaman 
1914f453a54aSFred Isaman 	data->completion_ops->completion(data);
1915c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
19161da177e4SLinus Torvalds }
1917788e7a89STrond Myklebust 
1918788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
19190b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1920788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1921788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1922788e7a89STrond Myklebust };
19231da177e4SLinus Torvalds 
1924f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1925f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1926b20135d0STrond Myklebust 	.resched_write = nfs_commit_resched_write,
1927f453a54aSFred Isaman };
1928f453a54aSFred Isaman 
19291763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1930ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
193184c53ab5SFred Isaman {
193284c53ab5SFred Isaman 	int status;
193384c53ab5SFred Isaman 
1934ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
193584c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1936ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
193784c53ab5SFred Isaman 	return status;
193884c53ab5SFred Isaman }
193984c53ab5SFred Isaman 
1940c4f24df9STrond Myklebust static int __nfs_commit_inode(struct inode *inode, int how,
1941c4f24df9STrond Myklebust 		struct writeback_control *wbc)
19421da177e4SLinus Torvalds {
19431da177e4SLinus Torvalds 	LIST_HEAD(head);
1944ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
194571d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1946c4f24df9STrond Myklebust 	int ret, nscan;
19471da177e4SLinus Torvalds 
194864a93dbfSTrond Myklebust 	how &= ~FLUSH_SYNC;
1949ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1950af7cf057STrond Myklebust 	nfs_commit_begin(cinfo.mds);
1951c4f24df9STrond Myklebust 	for (;;) {
1952c4f24df9STrond Myklebust 		ret = nscan = nfs_scan_commit(inode, &head, &cinfo);
1953c4f24df9STrond Myklebust 		if (ret <= 0)
1954c4f24df9STrond Myklebust 			break;
1955c4f24df9STrond Myklebust 		ret = nfs_generic_commit_list(inode, &head, how, &cinfo);
1956c4f24df9STrond Myklebust 		if (ret < 0)
1957c4f24df9STrond Myklebust 			break;
1958c4f24df9STrond Myklebust 		ret = 0;
1959c4f24df9STrond Myklebust 		if (wbc && wbc->sync_mode == WB_SYNC_NONE) {
1960c4f24df9STrond Myklebust 			if (nscan < wbc->nr_to_write)
1961c4f24df9STrond Myklebust 				wbc->nr_to_write -= nscan;
1962c4f24df9STrond Myklebust 			else
1963c4f24df9STrond Myklebust 				wbc->nr_to_write = 0;
1964c4f24df9STrond Myklebust 		}
1965c4f24df9STrond Myklebust 		if (nscan < INT_MAX)
1966c4f24df9STrond Myklebust 			break;
1967c4f24df9STrond Myklebust 		cond_resched();
1968c4f24df9STrond Myklebust 	}
1969af7cf057STrond Myklebust 	nfs_commit_end(cinfo.mds);
1970c4f24df9STrond Myklebust 	if (ret || !may_wait)
1971c4f24df9STrond Myklebust 		return ret;
1972c4f24df9STrond Myklebust 	return wait_on_commit(cinfo.mds);
1973c4f24df9STrond Myklebust }
1974c4f24df9STrond Myklebust 
1975c4f24df9STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
1976c4f24df9STrond Myklebust {
1977c4f24df9STrond Myklebust 	return __nfs_commit_inode(inode, how, NULL);
19781da177e4SLinus Torvalds }
1979b20135d0STrond Myklebust EXPORT_SYMBOL_GPL(nfs_commit_inode);
19808fc795f7STrond Myklebust 
1981ae09c31fSAnna Schumaker int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
19828fc795f7STrond Myklebust {
1983420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1984420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1985420e3646STrond Myklebust 	int ret = 0;
19868fc795f7STrond Myklebust 
1987c4f24df9STrond Myklebust 	if (wbc->sync_mode == WB_SYNC_NONE) {
19883236c3e1SJeff Layton 		/* no commits means nothing needs to be done */
19895cb953d4STrond Myklebust 		if (!atomic_long_read(&nfsi->commit_info.ncommit))
1990c4f24df9STrond Myklebust 			goto check_requests_outstanding;
19913236c3e1SJeff Layton 
1992a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1993a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1994420e3646STrond Myklebust 		 */
19951a4edf0fSTrond Myklebust 		if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1996420e3646STrond Myklebust 			goto out_mark_dirty;
1997420e3646STrond Myklebust 
1998a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1999420e3646STrond Myklebust 		flags = 0;
2000a00dd6c0SJeff Layton 	}
2001a00dd6c0SJeff Layton 
2002c4f24df9STrond Myklebust 	ret = __nfs_commit_inode(inode, flags, wbc);
2003c4f24df9STrond Myklebust 	if (!ret) {
2004c4f24df9STrond Myklebust 		if (flags & FLUSH_SYNC)
20058fc795f7STrond Myklebust 			return 0;
2006c4f24df9STrond Myklebust 	} else if (atomic_long_read(&nfsi->commit_info.ncommit))
2007c4f24df9STrond Myklebust 		goto out_mark_dirty;
2008c4f24df9STrond Myklebust 
2009c4f24df9STrond Myklebust check_requests_outstanding:
2010c4f24df9STrond Myklebust 	if (!atomic_read(&nfsi->commit_info.rpcs_out))
2011c4f24df9STrond Myklebust 		return ret;
2012420e3646STrond Myklebust out_mark_dirty:
20138fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
20148fc795f7STrond Myklebust 	return ret;
20158fc795f7STrond Myklebust }
201689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode);
2017863a3c6cSAndy Adamson 
2018acdc53b2STrond Myklebust /*
2019837bb1d7STrond Myklebust  * Wrapper for filemap_write_and_wait_range()
2020837bb1d7STrond Myklebust  *
2021837bb1d7STrond Myklebust  * Needed for pNFS in order to ensure data becomes visible to the
2022837bb1d7STrond Myklebust  * client.
2023837bb1d7STrond Myklebust  */
2024837bb1d7STrond Myklebust int nfs_filemap_write_and_wait_range(struct address_space *mapping,
2025837bb1d7STrond Myklebust 		loff_t lstart, loff_t lend)
2026837bb1d7STrond Myklebust {
2027837bb1d7STrond Myklebust 	int ret;
2028837bb1d7STrond Myklebust 
2029837bb1d7STrond Myklebust 	ret = filemap_write_and_wait_range(mapping, lstart, lend);
2030837bb1d7STrond Myklebust 	if (ret == 0)
2031837bb1d7STrond Myklebust 		ret = pnfs_sync_inode(mapping->host, true);
2032837bb1d7STrond Myklebust 	return ret;
2033837bb1d7STrond Myklebust }
2034837bb1d7STrond Myklebust EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range);
2035837bb1d7STrond Myklebust 
2036837bb1d7STrond Myklebust /*
2037acdc53b2STrond Myklebust  * flush the inode to disk.
2038acdc53b2STrond Myklebust  */
2039acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
204034901f70STrond Myklebust {
2041f4ce1299STrond Myklebust 	int ret;
204234901f70STrond Myklebust 
2043f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_enter(inode);
2044f4ce1299STrond Myklebust 
20455bb89b47STrond Myklebust 	ret = filemap_write_and_wait(inode->i_mapping);
20466b196875SChuck Lever 	if (ret)
20476b196875SChuck Lever 		goto out;
20485bb89b47STrond Myklebust 	ret = nfs_commit_inode(inode, FLUSH_SYNC);
20496b196875SChuck Lever 	if (ret < 0)
20506b196875SChuck Lever 		goto out;
20515bb89b47STrond Myklebust 	pnfs_sync_inode(inode, true);
20526b196875SChuck Lever 	ret = 0;
2053f4ce1299STrond Myklebust 
20546b196875SChuck Lever out:
2055f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_exit(inode, ret);
2056f4ce1299STrond Myklebust 	return ret;
20571c75950bSTrond Myklebust }
2058ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all);
20591c75950bSTrond Myklebust 
20601b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
20611b3b4a1aSTrond Myklebust {
20621b3b4a1aSTrond Myklebust 	struct nfs_page *req;
20631b3b4a1aSTrond Myklebust 	int ret = 0;
20641b3b4a1aSTrond Myklebust 
2065ba8b06e6STrond Myklebust 	wait_on_page_writeback(page);
20663e217045SWeston Andros Adamson 
20673e217045SWeston Andros Adamson 	/* blocking call to cancel all requests and join to a single (head)
20683e217045SWeston Andros Adamson 	 * request */
20696d17d653STrond Myklebust 	req = nfs_lock_and_join_requests(page);
20703e217045SWeston Andros Adamson 
20713e217045SWeston Andros Adamson 	if (IS_ERR(req)) {
20723e217045SWeston Andros Adamson 		ret = PTR_ERR(req);
20733e217045SWeston Andros Adamson 	} else if (req) {
20743e217045SWeston Andros Adamson 		/* all requests from this page have been cancelled by
20753e217045SWeston Andros Adamson 		 * nfs_lock_and_join_requests, so just remove the head
20763e217045SWeston Andros Adamson 		 * request from the inode / page_private pointer and
20773e217045SWeston Andros Adamson 		 * release it */
20781b3b4a1aSTrond Myklebust 		nfs_inode_remove_request(req);
20791d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
20801b3b4a1aSTrond Myklebust 	}
20813e217045SWeston Andros Adamson 
20821b3b4a1aSTrond Myklebust 	return ret;
20831b3b4a1aSTrond Myklebust }
20841b3b4a1aSTrond Myklebust 
20851c75950bSTrond Myklebust /*
20861c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
20871c75950bSTrond Myklebust  */
2088c373fff7STrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
20891c75950bSTrond Myklebust {
209029418aa4SMel Gorman 	loff_t range_start = page_file_offset(page);
209109cbfeafSKirill A. Shutemov 	loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1);
20927f2f12d9STrond Myklebust 	struct writeback_control wbc = {
20937f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
20947f2f12d9STrond Myklebust 		.nr_to_write = 0,
20957f2f12d9STrond Myklebust 		.range_start = range_start,
20967f2f12d9STrond Myklebust 		.range_end = range_end,
20977f2f12d9STrond Myklebust 	};
20987f2f12d9STrond Myklebust 	int ret;
20997f2f12d9STrond Myklebust 
2100f4ce1299STrond Myklebust 	trace_nfs_writeback_page_enter(inode);
2101f4ce1299STrond Myklebust 
21020522f6adSTrond Myklebust 	for (;;) {
2103ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
21047f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
2105c373fff7STrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
21067f2f12d9STrond Myklebust 			if (ret < 0)
21077f2f12d9STrond Myklebust 				goto out_error;
21080522f6adSTrond Myklebust 			continue;
21097f2f12d9STrond Myklebust 		}
2110f4ce1299STrond Myklebust 		ret = 0;
21110522f6adSTrond Myklebust 		if (!PagePrivate(page))
21120522f6adSTrond Myklebust 			break;
21130522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
21147f2f12d9STrond Myklebust 		if (ret < 0)
21157f2f12d9STrond Myklebust 			goto out_error;
21167f2f12d9STrond Myklebust 	}
21177f2f12d9STrond Myklebust out_error:
2118f4ce1299STrond Myklebust 	trace_nfs_writeback_page_exit(inode, ret);
21197f2f12d9STrond Myklebust 	return ret;
21201c75950bSTrond Myklebust }
21211c75950bSTrond Myklebust 
2122074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
2123074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
2124a6bc32b8SMel Gorman 		struct page *page, enum migrate_mode mode)
2125074cc1deSTrond Myklebust {
21262da95652SJeff Layton 	/*
21272da95652SJeff Layton 	 * If PagePrivate is set, then the page is currently associated with
21282da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
21292da95652SJeff Layton 	 *
21302da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
21312da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
21322da95652SJeff Layton 	 *        the page lock.
21332da95652SJeff Layton 	 */
21342da95652SJeff Layton 	if (PagePrivate(page))
21352da95652SJeff Layton 		return -EBUSY;
2136074cc1deSTrond Myklebust 
213716f2f4e6SDavid Howells 	if (PageFsCache(page)) {
213816f2f4e6SDavid Howells 		if (mode == MIGRATE_ASYNC)
21398c209ce7SDavid Howells 			return -EBUSY;
214016f2f4e6SDavid Howells 		wait_on_page_fscache(page);
214116f2f4e6SDavid Howells 	}
2142074cc1deSTrond Myklebust 
2143a6bc32b8SMel Gorman 	return migrate_page(mapping, newpage, page, mode);
2144074cc1deSTrond Myklebust }
2145074cc1deSTrond Myklebust #endif
2146074cc1deSTrond Myklebust 
2147f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
21481da177e4SLinus Torvalds {
21491da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
21501e7f3a48SWeston Andros Adamson 					     sizeof(struct nfs_pgio_header),
21511da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
215220c2df83SPaul Mundt 					     NULL);
21531da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
21541da177e4SLinus Torvalds 		return -ENOMEM;
21551da177e4SLinus Torvalds 
215693d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
21571da177e4SLinus Torvalds 						     nfs_wdata_cachep);
21581da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
21593dd4765fSJeff Layton 		goto out_destroy_write_cache;
21601da177e4SLinus Torvalds 
21610b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
21620b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
21630b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
21640b7c0153SFred Isaman 					     NULL);
21650b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
21663dd4765fSJeff Layton 		goto out_destroy_write_mempool;
21670b7c0153SFred Isaman 
216893d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
21694c100210SYanchuan Nian 						      nfs_cdata_cachep);
21701da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
21713dd4765fSJeff Layton 		goto out_destroy_commit_cache;
21721da177e4SLinus Torvalds 
217389a09141SPeter Zijlstra 	/*
217489a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
217589a09141SPeter Zijlstra 	 *
217689a09141SPeter Zijlstra 	 *  64MB:    8192k
217789a09141SPeter Zijlstra 	 * 128MB:   11585k
217889a09141SPeter Zijlstra 	 * 256MB:   16384k
217989a09141SPeter Zijlstra 	 * 512MB:   23170k
218089a09141SPeter Zijlstra 	 *   1GB:   32768k
218189a09141SPeter Zijlstra 	 *   2GB:   46340k
218289a09141SPeter Zijlstra 	 *   4GB:   65536k
218389a09141SPeter Zijlstra 	 *   8GB:   92681k
218489a09141SPeter Zijlstra 	 *  16GB:  131072k
218589a09141SPeter Zijlstra 	 *
218689a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
218789a09141SPeter Zijlstra 	 * Limit the default to 256M
218889a09141SPeter Zijlstra 	 */
2189ca79b0c2SArun KS 	nfs_congestion_kb = (16*int_sqrt(totalram_pages())) << (PAGE_SHIFT-10);
219089a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
219189a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
219289a09141SPeter Zijlstra 
21931da177e4SLinus Torvalds 	return 0;
21943dd4765fSJeff Layton 
21953dd4765fSJeff Layton out_destroy_commit_cache:
21963dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
21973dd4765fSJeff Layton out_destroy_write_mempool:
21983dd4765fSJeff Layton 	mempool_destroy(nfs_wdata_mempool);
21993dd4765fSJeff Layton out_destroy_write_cache:
22003dd4765fSJeff Layton 	kmem_cache_destroy(nfs_wdata_cachep);
22013dd4765fSJeff Layton 	return -ENOMEM;
22021da177e4SLinus Torvalds }
22031da177e4SLinus Torvalds 
2204266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
22051da177e4SLinus Torvalds {
22061da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
22073dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
22081da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
22091a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
22101da177e4SLinus Torvalds }
22111da177e4SLinus Torvalds 
22124a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops = {
22134a0de55cSAnna Schumaker 	.rw_alloc_header	= nfs_writehdr_alloc,
22144a0de55cSAnna Schumaker 	.rw_free_header		= nfs_writehdr_free,
22150eecb214SAnna Schumaker 	.rw_done		= nfs_writeback_done,
22160eecb214SAnna Schumaker 	.rw_result		= nfs_writeback_result,
22171ed26f33SAnna Schumaker 	.rw_initiate		= nfs_initiate_write,
22184a0de55cSAnna Schumaker };
2219