xref: /openbmc/linux/fs/nfs/write.c (revision 919e3bd9)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/fs/nfs/write.c
31da177e4SLinus Torvalds  *
47c85d900STrond Myklebust  * Write file data over NFS.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/types.h>
101da177e4SLinus Torvalds #include <linux/slab.h>
111da177e4SLinus Torvalds #include <linux/mm.h>
121da177e4SLinus Torvalds #include <linux/pagemap.h>
131da177e4SLinus Torvalds #include <linux/file.h>
141da177e4SLinus Torvalds #include <linux/writeback.h>
1589a09141SPeter Zijlstra #include <linux/swap.h>
16074cc1deSTrond Myklebust #include <linux/migrate.h>
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
191da177e4SLinus Torvalds #include <linux/nfs_fs.h>
201da177e4SLinus Torvalds #include <linux/nfs_mount.h>
211da177e4SLinus Torvalds #include <linux/nfs_page.h>
223fcfab16SAndrew Morton #include <linux/backing-dev.h>
23afeacc8cSPaul Gortmaker #include <linux/export.h>
24af7cf057STrond Myklebust #include <linux/freezer.h>
25af7cf057STrond Myklebust #include <linux/wait.h>
263fcfab16SAndrew Morton 
277c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #include "delegation.h"
3049a70f27STrond Myklebust #include "internal.h"
3191d5b470SChuck Lever #include "iostat.h"
32def6ed7eSAndy Adamson #include "nfs4_fs.h"
33074cc1deSTrond Myklebust #include "fscache.h"
3494ad1c80SFred Isaman #include "pnfs.h"
351da177e4SLinus Torvalds 
36f4ce1299STrond Myklebust #include "nfstrace.h"
37f4ce1299STrond Myklebust 
381da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #define MIN_POOL_WRITE		(32)
411da177e4SLinus Torvalds #define MIN_POOL_COMMIT		(4)
421da177e4SLinus Torvalds 
43919e3bd9STrond Myklebust struct nfs_io_completion {
44919e3bd9STrond Myklebust 	void (*complete)(void *data);
45919e3bd9STrond Myklebust 	void *data;
46919e3bd9STrond Myklebust 	struct kref refcount;
47919e3bd9STrond Myklebust };
48919e3bd9STrond Myklebust 
491da177e4SLinus Torvalds /*
501da177e4SLinus Torvalds  * Local function declarations
511da177e4SLinus Torvalds  */
52f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req);
53788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops;
54061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops;
55f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops;
564a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops;
57d4581383SWeston Andros Adamson static void nfs_clear_request_commit(struct nfs_page *req);
5802d1426cSWeston Andros Adamson static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
5902d1426cSWeston Andros Adamson 				      struct inode *inode);
603a3908c8STrond Myklebust static struct nfs_page *
613a3908c8STrond Myklebust nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
623a3908c8STrond Myklebust 						struct page *page);
631da177e4SLinus Torvalds 
64e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep;
653feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
660b7c0153SFred Isaman static struct kmem_cache *nfs_cdata_cachep;
671da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
681da177e4SLinus Torvalds 
69518662e0SNeilBrown struct nfs_commit_data *nfs_commitdata_alloc(bool never_fail)
701da177e4SLinus Torvalds {
71518662e0SNeilBrown 	struct nfs_commit_data *p;
7240859d7eSChuck Lever 
73518662e0SNeilBrown 	if (never_fail)
74518662e0SNeilBrown 		p = mempool_alloc(nfs_commit_mempool, GFP_NOIO);
75518662e0SNeilBrown 	else {
76518662e0SNeilBrown 		/* It is OK to do some reclaim, not no safe to wait
77518662e0SNeilBrown 		 * for anything to be returned to the pool.
78518662e0SNeilBrown 		 * mempool_alloc() cannot handle that particular combination,
79518662e0SNeilBrown 		 * so we need two separate attempts.
80518662e0SNeilBrown 		 */
81518662e0SNeilBrown 		p = mempool_alloc(nfs_commit_mempool, GFP_NOWAIT);
82518662e0SNeilBrown 		if (!p)
83518662e0SNeilBrown 			p = kmem_cache_alloc(nfs_cdata_cachep, GFP_NOIO |
84518662e0SNeilBrown 					     __GFP_NOWARN | __GFP_NORETRY);
85518662e0SNeilBrown 		if (!p)
86518662e0SNeilBrown 			return NULL;
87518662e0SNeilBrown 	}
88518662e0SNeilBrown 
891da177e4SLinus Torvalds 	memset(p, 0, sizeof(*p));
901da177e4SLinus Torvalds 	INIT_LIST_HEAD(&p->pages);
911da177e4SLinus Torvalds 	return p;
921da177e4SLinus Torvalds }
93e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
941da177e4SLinus Torvalds 
950b7c0153SFred Isaman void nfs_commit_free(struct nfs_commit_data *p)
961da177e4SLinus Torvalds {
971da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
981da177e4SLinus Torvalds }
99e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free);
1001da177e4SLinus Torvalds 
1011e7f3a48SWeston Andros Adamson static struct nfs_pgio_header *nfs_writehdr_alloc(void)
1023feb2d49STrond Myklebust {
1031e7f3a48SWeston Andros Adamson 	struct nfs_pgio_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOIO);
1043feb2d49STrond Myklebust 
105fbe77c30SBenjamin Coddington 	if (p) {
1063feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
107fbe77c30SBenjamin Coddington 		p->rw_mode = FMODE_WRITE;
108fbe77c30SBenjamin Coddington 	}
1093feb2d49STrond Myklebust 	return p;
1103feb2d49STrond Myklebust }
1113feb2d49STrond Myklebust 
1121e7f3a48SWeston Andros Adamson static void nfs_writehdr_free(struct nfs_pgio_header *hdr)
1136c75dc0dSFred Isaman {
1141e7f3a48SWeston Andros Adamson 	mempool_free(hdr, nfs_wdata_mempool);
1153feb2d49STrond Myklebust }
1161da177e4SLinus Torvalds 
117919e3bd9STrond Myklebust static struct nfs_io_completion *nfs_io_completion_alloc(gfp_t gfp_flags)
118919e3bd9STrond Myklebust {
119919e3bd9STrond Myklebust 	return kmalloc(sizeof(struct nfs_io_completion), gfp_flags);
120919e3bd9STrond Myklebust }
121919e3bd9STrond Myklebust 
122919e3bd9STrond Myklebust static void nfs_io_completion_init(struct nfs_io_completion *ioc,
123919e3bd9STrond Myklebust 		void (*complete)(void *), void *data)
124919e3bd9STrond Myklebust {
125919e3bd9STrond Myklebust 	ioc->complete = complete;
126919e3bd9STrond Myklebust 	ioc->data = data;
127919e3bd9STrond Myklebust 	kref_init(&ioc->refcount);
128919e3bd9STrond Myklebust }
129919e3bd9STrond Myklebust 
130919e3bd9STrond Myklebust static void nfs_io_completion_release(struct kref *kref)
131919e3bd9STrond Myklebust {
132919e3bd9STrond Myklebust 	struct nfs_io_completion *ioc = container_of(kref,
133919e3bd9STrond Myklebust 			struct nfs_io_completion, refcount);
134919e3bd9STrond Myklebust 	ioc->complete(ioc->data);
135919e3bd9STrond Myklebust 	kfree(ioc);
136919e3bd9STrond Myklebust }
137919e3bd9STrond Myklebust 
138919e3bd9STrond Myklebust static void nfs_io_completion_get(struct nfs_io_completion *ioc)
139919e3bd9STrond Myklebust {
140919e3bd9STrond Myklebust 	if (ioc != NULL)
141919e3bd9STrond Myklebust 		kref_get(&ioc->refcount);
142919e3bd9STrond Myklebust }
143919e3bd9STrond Myklebust 
144919e3bd9STrond Myklebust static void nfs_io_completion_put(struct nfs_io_completion *ioc)
145919e3bd9STrond Myklebust {
146919e3bd9STrond Myklebust 	if (ioc != NULL)
147919e3bd9STrond Myklebust 		kref_put(&ioc->refcount, nfs_io_completion_release);
148919e3bd9STrond Myklebust }
149919e3bd9STrond Myklebust 
1507b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
1517b159fc1STrond Myklebust {
1527b159fc1STrond Myklebust 	ctx->error = error;
1537b159fc1STrond Myklebust 	smp_wmb();
1547b159fc1STrond Myklebust 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
1557b159fc1STrond Myklebust }
1567b159fc1STrond Myklebust 
15784d3a9a9SWeston Andros Adamson /*
15884d3a9a9SWeston Andros Adamson  * nfs_page_find_head_request_locked - find head request associated with @page
15984d3a9a9SWeston Andros Adamson  *
16084d3a9a9SWeston Andros Adamson  * must be called while holding the inode lock.
16184d3a9a9SWeston Andros Adamson  *
16284d3a9a9SWeston Andros Adamson  * returns matching head request with reference held, or NULL if not found.
16384d3a9a9SWeston Andros Adamson  */
16429418aa4SMel Gorman static struct nfs_page *
16584d3a9a9SWeston Andros Adamson nfs_page_find_head_request_locked(struct nfs_inode *nfsi, struct page *page)
166277459d2STrond Myklebust {
167277459d2STrond Myklebust 	struct nfs_page *req = NULL;
168277459d2STrond Myklebust 
16929418aa4SMel Gorman 	if (PagePrivate(page))
170277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
17102d1426cSWeston Andros Adamson 	else if (unlikely(PageSwapCache(page)))
17202d1426cSWeston Andros Adamson 		req = nfs_page_search_commits_for_head_request_locked(nfsi,
17302d1426cSWeston Andros Adamson 			page);
17429418aa4SMel Gorman 
17584d3a9a9SWeston Andros Adamson 	if (req) {
17684d3a9a9SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_head != req);
17729418aa4SMel Gorman 		kref_get(&req->wb_kref);
17884d3a9a9SWeston Andros Adamson 	}
17929418aa4SMel Gorman 
180277459d2STrond Myklebust 	return req;
181277459d2STrond Myklebust }
182277459d2STrond Myklebust 
18384d3a9a9SWeston Andros Adamson /*
18484d3a9a9SWeston Andros Adamson  * nfs_page_find_head_request - find head request associated with @page
18584d3a9a9SWeston Andros Adamson  *
18684d3a9a9SWeston Andros Adamson  * returns matching head request with reference held, or NULL if not found.
18784d3a9a9SWeston Andros Adamson  */
18884d3a9a9SWeston Andros Adamson static struct nfs_page *nfs_page_find_head_request(struct page *page)
189277459d2STrond Myklebust {
190d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
191277459d2STrond Myklebust 	struct nfs_page *req = NULL;
192277459d2STrond Myklebust 
193587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
19484d3a9a9SWeston Andros Adamson 	req = nfs_page_find_head_request_locked(NFS_I(inode), page);
195587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
196277459d2STrond Myklebust 	return req;
197277459d2STrond Myklebust }
198277459d2STrond Myklebust 
1991da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
2001da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
2011da177e4SLinus Torvalds {
202d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
203a3d01454STrond Myklebust 	loff_t end, i_size;
204a3d01454STrond Myklebust 	pgoff_t end_index;
2051da177e4SLinus Torvalds 
206a3d01454STrond Myklebust 	spin_lock(&inode->i_lock);
207a3d01454STrond Myklebust 	i_size = i_size_read(inode);
20809cbfeafSKirill A. Shutemov 	end_index = (i_size - 1) >> PAGE_SHIFT;
2098cd79788SHuang Ying 	if (i_size > 0 && page_index(page) < end_index)
210a3d01454STrond Myklebust 		goto out;
211d56b4ddfSMel Gorman 	end = page_file_offset(page) + ((loff_t)offset+count);
2121da177e4SLinus Torvalds 	if (i_size >= end)
213a3d01454STrond Myklebust 		goto out;
2141da177e4SLinus Torvalds 	i_size_write(inode, end);
215a3d01454STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
216a3d01454STrond Myklebust out:
217a3d01454STrond Myklebust 	spin_unlock(&inode->i_lock);
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
220a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
221a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
222a301b777STrond Myklebust {
223d56b4ddfSMel Gorman 	nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page));
224a301b777STrond Myklebust }
225a301b777STrond Myklebust 
226d72ddcbaSWeston Andros Adamson /*
227d72ddcbaSWeston Andros Adamson  * nfs_page_group_search_locked
228d72ddcbaSWeston Andros Adamson  * @head - head request of page group
229d72ddcbaSWeston Andros Adamson  * @page_offset - offset into page
230d72ddcbaSWeston Andros Adamson  *
231d72ddcbaSWeston Andros Adamson  * Search page group with head @head to find a request that contains the
232d72ddcbaSWeston Andros Adamson  * page offset @page_offset.
233d72ddcbaSWeston Andros Adamson  *
234d72ddcbaSWeston Andros Adamson  * Returns a pointer to the first matching nfs request, or NULL if no
235d72ddcbaSWeston Andros Adamson  * match is found.
236d72ddcbaSWeston Andros Adamson  *
237d72ddcbaSWeston Andros Adamson  * Must be called with the page group lock held
238d72ddcbaSWeston Andros Adamson  */
239d72ddcbaSWeston Andros Adamson static struct nfs_page *
240d72ddcbaSWeston Andros Adamson nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset)
241d72ddcbaSWeston Andros Adamson {
242d72ddcbaSWeston Andros Adamson 	struct nfs_page *req;
243d72ddcbaSWeston Andros Adamson 
244d72ddcbaSWeston Andros Adamson 	WARN_ON_ONCE(head != head->wb_head);
245d72ddcbaSWeston Andros Adamson 	WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_head->wb_flags));
246d72ddcbaSWeston Andros Adamson 
247d72ddcbaSWeston Andros Adamson 	req = head;
248d72ddcbaSWeston Andros Adamson 	do {
249d72ddcbaSWeston Andros Adamson 		if (page_offset >= req->wb_pgbase &&
250d72ddcbaSWeston Andros Adamson 		    page_offset < (req->wb_pgbase + req->wb_bytes))
251d72ddcbaSWeston Andros Adamson 			return req;
252d72ddcbaSWeston Andros Adamson 
253d72ddcbaSWeston Andros Adamson 		req = req->wb_this_page;
254d72ddcbaSWeston Andros Adamson 	} while (req != head);
255d72ddcbaSWeston Andros Adamson 
256d72ddcbaSWeston Andros Adamson 	return NULL;
257d72ddcbaSWeston Andros Adamson }
258d72ddcbaSWeston Andros Adamson 
259d72ddcbaSWeston Andros Adamson /*
260d72ddcbaSWeston Andros Adamson  * nfs_page_group_covers_page
261d72ddcbaSWeston Andros Adamson  * @head - head request of page group
262d72ddcbaSWeston Andros Adamson  *
263d72ddcbaSWeston Andros Adamson  * Return true if the page group with head @head covers the whole page,
264d72ddcbaSWeston Andros Adamson  * returns false otherwise
265d72ddcbaSWeston Andros Adamson  */
266d72ddcbaSWeston Andros Adamson static bool nfs_page_group_covers_page(struct nfs_page *req)
267d72ddcbaSWeston Andros Adamson {
268d72ddcbaSWeston Andros Adamson 	struct nfs_page *tmp;
269d72ddcbaSWeston Andros Adamson 	unsigned int pos = 0;
270d72ddcbaSWeston Andros Adamson 	unsigned int len = nfs_page_length(req->wb_page);
271d72ddcbaSWeston Andros Adamson 
272fd2f3a06SWeston Andros Adamson 	nfs_page_group_lock(req, false);
273d72ddcbaSWeston Andros Adamson 
274d72ddcbaSWeston Andros Adamson 	do {
275d72ddcbaSWeston Andros Adamson 		tmp = nfs_page_group_search_locked(req->wb_head, pos);
276d72ddcbaSWeston Andros Adamson 		if (tmp) {
277d72ddcbaSWeston Andros Adamson 			/* no way this should happen */
278d72ddcbaSWeston Andros Adamson 			WARN_ON_ONCE(tmp->wb_pgbase != pos);
279d72ddcbaSWeston Andros Adamson 			pos += tmp->wb_bytes - (pos - tmp->wb_pgbase);
280d72ddcbaSWeston Andros Adamson 		}
281d72ddcbaSWeston Andros Adamson 	} while (tmp && pos < len);
282d72ddcbaSWeston Andros Adamson 
283d72ddcbaSWeston Andros Adamson 	nfs_page_group_unlock(req);
284d72ddcbaSWeston Andros Adamson 	WARN_ON_ONCE(pos > len);
285d72ddcbaSWeston Andros Adamson 	return pos == len;
286d72ddcbaSWeston Andros Adamson }
287d72ddcbaSWeston Andros Adamson 
2881da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
2891da177e4SLinus Torvalds  * covers the full page.
2901da177e4SLinus Torvalds  */
291d72ddcbaSWeston Andros Adamson static void nfs_mark_uptodate(struct nfs_page *req)
2921da177e4SLinus Torvalds {
293d72ddcbaSWeston Andros Adamson 	if (PageUptodate(req->wb_page))
2941da177e4SLinus Torvalds 		return;
295d72ddcbaSWeston Andros Adamson 	if (!nfs_page_group_covers_page(req))
2961da177e4SLinus Torvalds 		return;
297d72ddcbaSWeston Andros Adamson 	SetPageUptodate(req->wb_page);
2981da177e4SLinus Torvalds }
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
3011da177e4SLinus Torvalds {
302e87b4c7aSNeilBrown 	int ret = 0;
303cca588d6STrond Myklebust 
304e87b4c7aSNeilBrown 	if (wbc->sync_mode == WB_SYNC_ALL)
305e87b4c7aSNeilBrown 		ret = FLUSH_COND_STABLE;
306e87b4c7aSNeilBrown 	return ret;
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds /*
31089a09141SPeter Zijlstra  * NFS congestion control
31189a09141SPeter Zijlstra  */
31289a09141SPeter Zijlstra 
31389a09141SPeter Zijlstra int nfs_congestion_kb;
31489a09141SPeter Zijlstra 
31589a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
31689a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
31789a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
31889a09141SPeter Zijlstra 
319deed85e7STrond Myklebust static void nfs_set_page_writeback(struct page *page)
32089a09141SPeter Zijlstra {
3210db10944SJan Kara 	struct inode *inode = page_file_mapping(page)->host;
3220db10944SJan Kara 	struct nfs_server *nfss = NFS_SERVER(inode);
3235a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
3245a6d41b3STrond Myklebust 
325deed85e7STrond Myklebust 	WARN_ON_ONCE(ret != 0);
32689a09141SPeter Zijlstra 
327277866a0SPeter Zijlstra 	if (atomic_long_inc_return(&nfss->writeback) >
3280db10944SJan Kara 			NFS_CONGESTION_ON_THRESH)
3290db10944SJan Kara 		set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
33089a09141SPeter Zijlstra }
33189a09141SPeter Zijlstra 
33220633f04SWeston Andros Adamson static void nfs_end_page_writeback(struct nfs_page *req)
33389a09141SPeter Zijlstra {
33420633f04SWeston Andros Adamson 	struct inode *inode = page_file_mapping(req->wb_page)->host;
33589a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
33689a09141SPeter Zijlstra 
33720633f04SWeston Andros Adamson 	if (!nfs_page_group_sync_on_bit(req, PG_WB_END))
33820633f04SWeston Andros Adamson 		return;
33920633f04SWeston Andros Adamson 
34020633f04SWeston Andros Adamson 	end_page_writeback(req->wb_page);
341c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
3420db10944SJan Kara 		clear_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
34389a09141SPeter Zijlstra }
34489a09141SPeter Zijlstra 
345d4581383SWeston Andros Adamson 
346d4581383SWeston Andros Adamson /* nfs_page_group_clear_bits
347d4581383SWeston Andros Adamson  *   @req - an nfs request
348d4581383SWeston Andros Adamson  * clears all page group related bits from @req
349d4581383SWeston Andros Adamson  */
350d4581383SWeston Andros Adamson static void
351d4581383SWeston Andros Adamson nfs_page_group_clear_bits(struct nfs_page *req)
352e261f51fSTrond Myklebust {
353d4581383SWeston Andros Adamson 	clear_bit(PG_TEARDOWN, &req->wb_flags);
354d4581383SWeston Andros Adamson 	clear_bit(PG_UNLOCKPAGE, &req->wb_flags);
355d4581383SWeston Andros Adamson 	clear_bit(PG_UPTODATE, &req->wb_flags);
356d4581383SWeston Andros Adamson 	clear_bit(PG_WB_END, &req->wb_flags);
357d4581383SWeston Andros Adamson 	clear_bit(PG_REMOVE, &req->wb_flags);
358d4581383SWeston Andros Adamson }
359d4581383SWeston Andros Adamson 
360d4581383SWeston Andros Adamson 
361d4581383SWeston Andros Adamson /*
362d4581383SWeston Andros Adamson  * nfs_unroll_locks_and_wait -  unlock all newly locked reqs and wait on @req
363d4581383SWeston Andros Adamson  *
364d4581383SWeston Andros Adamson  * this is a helper function for nfs_lock_and_join_requests
365d4581383SWeston Andros Adamson  *
366d4581383SWeston Andros Adamson  * @inode - inode associated with request page group, must be holding inode lock
367d4581383SWeston Andros Adamson  * @head  - head request of page group, must be holding head lock
368d4581383SWeston Andros Adamson  * @req   - request that couldn't lock and needs to wait on the req bit lock
369d4581383SWeston Andros Adamson  * @nonblock - if true, don't actually wait
370d4581383SWeston Andros Adamson  *
371d4581383SWeston Andros Adamson  * NOTE: this must be called holding page_group bit lock and inode spin lock
372d4581383SWeston Andros Adamson  *       and BOTH will be released before returning.
373d4581383SWeston Andros Adamson  *
374d4581383SWeston Andros Adamson  * returns 0 on success, < 0 on error.
375d4581383SWeston Andros Adamson  */
376d4581383SWeston Andros Adamson static int
377d4581383SWeston Andros Adamson nfs_unroll_locks_and_wait(struct inode *inode, struct nfs_page *head,
378d4581383SWeston Andros Adamson 			  struct nfs_page *req, bool nonblock)
379d4581383SWeston Andros Adamson 	__releases(&inode->i_lock)
380d4581383SWeston Andros Adamson {
381d4581383SWeston Andros Adamson 	struct nfs_page *tmp;
382e261f51fSTrond Myklebust 	int ret;
383e261f51fSTrond Myklebust 
384d4581383SWeston Andros Adamson 	/* relinquish all the locks successfully grabbed this run */
385d4581383SWeston Andros Adamson 	for (tmp = head ; tmp != req; tmp = tmp->wb_this_page)
386d4581383SWeston Andros Adamson 		nfs_unlock_request(tmp);
387d4581383SWeston Andros Adamson 
388d4581383SWeston Andros Adamson 	WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
389d4581383SWeston Andros Adamson 
390d4581383SWeston Andros Adamson 	/* grab a ref on the request that will be waited on */
391d4581383SWeston Andros Adamson 	kref_get(&req->wb_kref);
392d4581383SWeston Andros Adamson 
393d4581383SWeston Andros Adamson 	nfs_page_group_unlock(head);
394587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
395d4581383SWeston Andros Adamson 
396d4581383SWeston Andros Adamson 	/* release ref from nfs_page_find_head_request_locked */
397d4581383SWeston Andros Adamson 	nfs_release_request(head);
398d4581383SWeston Andros Adamson 
399cfb506e1STrond Myklebust 	if (!nonblock)
400e261f51fSTrond Myklebust 		ret = nfs_wait_on_request(req);
401cfb506e1STrond Myklebust 	else
402cfb506e1STrond Myklebust 		ret = -EAGAIN;
403e261f51fSTrond Myklebust 	nfs_release_request(req);
404d4581383SWeston Andros Adamson 
405d4581383SWeston Andros Adamson 	return ret;
406e261f51fSTrond Myklebust }
407d4581383SWeston Andros Adamson 
408d4581383SWeston Andros Adamson /*
409d4581383SWeston Andros Adamson  * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests
410d4581383SWeston Andros Adamson  *
411d4581383SWeston Andros Adamson  * @destroy_list - request list (using wb_this_page) terminated by @old_head
412d4581383SWeston Andros Adamson  * @old_head - the old head of the list
413d4581383SWeston Andros Adamson  *
414d4581383SWeston Andros Adamson  * All subrequests must be locked and removed from all lists, so at this point
415d4581383SWeston Andros Adamson  * they are only "active" in this function, and possibly in nfs_wait_on_request
416d4581383SWeston Andros Adamson  * with a reference held by some other context.
417d4581383SWeston Andros Adamson  */
418d4581383SWeston Andros Adamson static void
419d4581383SWeston Andros Adamson nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list,
420d4581383SWeston Andros Adamson 				 struct nfs_page *old_head)
421d4581383SWeston Andros Adamson {
422d4581383SWeston Andros Adamson 	while (destroy_list) {
423d4581383SWeston Andros Adamson 		struct nfs_page *subreq = destroy_list;
424d4581383SWeston Andros Adamson 
425d4581383SWeston Andros Adamson 		destroy_list = (subreq->wb_this_page == old_head) ?
426d4581383SWeston Andros Adamson 				   NULL : subreq->wb_this_page;
427d4581383SWeston Andros Adamson 
428d4581383SWeston Andros Adamson 		WARN_ON_ONCE(old_head != subreq->wb_head);
429d4581383SWeston Andros Adamson 
430d4581383SWeston Andros Adamson 		/* make sure old group is not used */
431d4581383SWeston Andros Adamson 		subreq->wb_head = subreq;
432d4581383SWeston Andros Adamson 		subreq->wb_this_page = subreq;
433d4581383SWeston Andros Adamson 
434d4581383SWeston Andros Adamson 		/* subreq is now totally disconnected from page group or any
435d4581383SWeston Andros Adamson 		 * write / commit lists. last chance to wake any waiters */
436d4581383SWeston Andros Adamson 		nfs_unlock_request(subreq);
437d4581383SWeston Andros Adamson 
438d4581383SWeston Andros Adamson 		if (!test_bit(PG_TEARDOWN, &subreq->wb_flags)) {
439d4581383SWeston Andros Adamson 			/* release ref on old head request */
440d4581383SWeston Andros Adamson 			nfs_release_request(old_head);
441d4581383SWeston Andros Adamson 
442d4581383SWeston Andros Adamson 			nfs_page_group_clear_bits(subreq);
443d4581383SWeston Andros Adamson 
444d4581383SWeston Andros Adamson 			/* release the PG_INODE_REF reference */
445d4581383SWeston Andros Adamson 			if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags))
446d4581383SWeston Andros Adamson 				nfs_release_request(subreq);
447d4581383SWeston Andros Adamson 			else
448d4581383SWeston Andros Adamson 				WARN_ON_ONCE(1);
449d4581383SWeston Andros Adamson 		} else {
450d4581383SWeston Andros Adamson 			WARN_ON_ONCE(test_bit(PG_CLEAN, &subreq->wb_flags));
451d4581383SWeston Andros Adamson 			/* zombie requests have already released the last
452d4581383SWeston Andros Adamson 			 * reference and were waiting on the rest of the
453d4581383SWeston Andros Adamson 			 * group to complete. Since it's no longer part of a
454d4581383SWeston Andros Adamson 			 * group, simply free the request */
455d4581383SWeston Andros Adamson 			nfs_page_group_clear_bits(subreq);
456d4581383SWeston Andros Adamson 			nfs_free_request(subreq);
457d4581383SWeston Andros Adamson 		}
458d4581383SWeston Andros Adamson 	}
459d4581383SWeston Andros Adamson }
460d4581383SWeston Andros Adamson 
461d4581383SWeston Andros Adamson /*
462d4581383SWeston Andros Adamson  * nfs_lock_and_join_requests - join all subreqs to the head req and return
463d4581383SWeston Andros Adamson  *                              a locked reference, cancelling any pending
464d4581383SWeston Andros Adamson  *                              operations for this page.
465d4581383SWeston Andros Adamson  *
466d4581383SWeston Andros Adamson  * @page - the page used to lookup the "page group" of nfs_page structures
467d4581383SWeston Andros Adamson  * @nonblock - if true, don't block waiting for request locks
468d4581383SWeston Andros Adamson  *
469d4581383SWeston Andros Adamson  * This function joins all sub requests to the head request by first
470d4581383SWeston Andros Adamson  * locking all requests in the group, cancelling any pending operations
471d4581383SWeston Andros Adamson  * and finally updating the head request to cover the whole range covered by
472d4581383SWeston Andros Adamson  * the (former) group.  All subrequests are removed from any write or commit
473d4581383SWeston Andros Adamson  * lists, unlinked from the group and destroyed.
474d4581383SWeston Andros Adamson  *
475d4581383SWeston Andros Adamson  * Returns a locked, referenced pointer to the head request - which after
476d4581383SWeston Andros Adamson  * this call is guaranteed to be the only request associated with the page.
477d4581383SWeston Andros Adamson  * Returns NULL if no requests are found for @page, or a ERR_PTR if an
478d4581383SWeston Andros Adamson  * error was encountered.
479d4581383SWeston Andros Adamson  */
480d4581383SWeston Andros Adamson static struct nfs_page *
481d4581383SWeston Andros Adamson nfs_lock_and_join_requests(struct page *page, bool nonblock)
482d4581383SWeston Andros Adamson {
483d4581383SWeston Andros Adamson 	struct inode *inode = page_file_mapping(page)->host;
484d4581383SWeston Andros Adamson 	struct nfs_page *head, *subreq;
485d4581383SWeston Andros Adamson 	struct nfs_page *destroy_list = NULL;
486d4581383SWeston Andros Adamson 	unsigned int total_bytes;
487d4581383SWeston Andros Adamson 	int ret;
488d4581383SWeston Andros Adamson 
489d4581383SWeston Andros Adamson try_again:
490d4581383SWeston Andros Adamson 	total_bytes = 0;
491d4581383SWeston Andros Adamson 
492d4581383SWeston Andros Adamson 	WARN_ON_ONCE(destroy_list);
493d4581383SWeston Andros Adamson 
494d4581383SWeston Andros Adamson 	spin_lock(&inode->i_lock);
495d4581383SWeston Andros Adamson 
496d4581383SWeston Andros Adamson 	/*
497d4581383SWeston Andros Adamson 	 * A reference is taken only on the head request which acts as a
498d4581383SWeston Andros Adamson 	 * reference to the whole page group - the group will not be destroyed
499d4581383SWeston Andros Adamson 	 * until the head reference is released.
500d4581383SWeston Andros Adamson 	 */
501d4581383SWeston Andros Adamson 	head = nfs_page_find_head_request_locked(NFS_I(inode), page);
502d4581383SWeston Andros Adamson 
503d4581383SWeston Andros Adamson 	if (!head) {
504587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
505d4581383SWeston Andros Adamson 		return NULL;
506d4581383SWeston Andros Adamson 	}
507d4581383SWeston Andros Adamson 
5087c3af975SWeston Andros Adamson 	/* holding inode lock, so always make a non-blocking call to try the
5097c3af975SWeston Andros Adamson 	 * page group lock */
510fd2f3a06SWeston Andros Adamson 	ret = nfs_page_group_lock(head, true);
51194970014SWeston Andros Adamson 	if (ret < 0) {
51294970014SWeston Andros Adamson 		spin_unlock(&inode->i_lock);
5137c3af975SWeston Andros Adamson 
5147c3af975SWeston Andros Adamson 		if (!nonblock && ret == -EAGAIN) {
5157c3af975SWeston Andros Adamson 			nfs_page_group_lock_wait(head);
5167c3af975SWeston Andros Adamson 			nfs_release_request(head);
5177c3af975SWeston Andros Adamson 			goto try_again;
5187c3af975SWeston Andros Adamson 		}
5197c3af975SWeston Andros Adamson 
52094970014SWeston Andros Adamson 		nfs_release_request(head);
521e7029206SWeston Andros Adamson 		return ERR_PTR(ret);
52294970014SWeston Andros Adamson 	}
5237c3af975SWeston Andros Adamson 
5247c3af975SWeston Andros Adamson 	/* lock each request in the page group */
525d4581383SWeston Andros Adamson 	subreq = head;
526d4581383SWeston Andros Adamson 	do {
527d4581383SWeston Andros Adamson 		/*
528d4581383SWeston Andros Adamson 		 * Subrequests are always contiguous, non overlapping
529309a1d65SWeston Andros Adamson 		 * and in order - but may be repeated (mirrored writes).
530d4581383SWeston Andros Adamson 		 */
531309a1d65SWeston Andros Adamson 		if (subreq->wb_offset == (head->wb_offset + total_bytes)) {
532d4581383SWeston Andros Adamson 			/* keep track of how many bytes this group covers */
533d4581383SWeston Andros Adamson 			total_bytes += subreq->wb_bytes;
534309a1d65SWeston Andros Adamson 		} else if (WARN_ON_ONCE(subreq->wb_offset < head->wb_offset ||
535309a1d65SWeston Andros Adamson 			    ((subreq->wb_offset + subreq->wb_bytes) >
536309a1d65SWeston Andros Adamson 			     (head->wb_offset + total_bytes)))) {
537309a1d65SWeston Andros Adamson 			nfs_page_group_unlock(head);
538309a1d65SWeston Andros Adamson 			spin_unlock(&inode->i_lock);
539309a1d65SWeston Andros Adamson 			return ERR_PTR(-EIO);
540309a1d65SWeston Andros Adamson 		}
541d4581383SWeston Andros Adamson 
542d4581383SWeston Andros Adamson 		if (!nfs_lock_request(subreq)) {
543d4581383SWeston Andros Adamson 			/* releases page group bit lock and
544d4581383SWeston Andros Adamson 			 * inode spin lock and all references */
545d4581383SWeston Andros Adamson 			ret = nfs_unroll_locks_and_wait(inode, head,
546d4581383SWeston Andros Adamson 				subreq, nonblock);
547d4581383SWeston Andros Adamson 
548d4581383SWeston Andros Adamson 			if (ret == 0)
549d4581383SWeston Andros Adamson 				goto try_again;
550d4581383SWeston Andros Adamson 
551d4581383SWeston Andros Adamson 			return ERR_PTR(ret);
552d4581383SWeston Andros Adamson 		}
553d4581383SWeston Andros Adamson 
554d4581383SWeston Andros Adamson 		subreq = subreq->wb_this_page;
555d4581383SWeston Andros Adamson 	} while (subreq != head);
556d4581383SWeston Andros Adamson 
557d4581383SWeston Andros Adamson 	/* Now that all requests are locked, make sure they aren't on any list.
558d4581383SWeston Andros Adamson 	 * Commit list removal accounting is done after locks are dropped */
559d4581383SWeston Andros Adamson 	subreq = head;
560d4581383SWeston Andros Adamson 	do {
561411a99adSWeston Andros Adamson 		nfs_clear_request_commit(subreq);
562d4581383SWeston Andros Adamson 		subreq = subreq->wb_this_page;
563d4581383SWeston Andros Adamson 	} while (subreq != head);
564d4581383SWeston Andros Adamson 
565d4581383SWeston Andros Adamson 	/* unlink subrequests from head, destroy them later */
566d4581383SWeston Andros Adamson 	if (head->wb_this_page != head) {
567d4581383SWeston Andros Adamson 		/* destroy list will be terminated by head */
568d4581383SWeston Andros Adamson 		destroy_list = head->wb_this_page;
569d4581383SWeston Andros Adamson 		head->wb_this_page = head;
570d4581383SWeston Andros Adamson 
571d4581383SWeston Andros Adamson 		/* change head request to cover whole range that
572d4581383SWeston Andros Adamson 		 * the former page group covered */
573d4581383SWeston Andros Adamson 		head->wb_bytes = total_bytes;
574d4581383SWeston Andros Adamson 	}
575d4581383SWeston Andros Adamson 
576d4581383SWeston Andros Adamson 	/*
577d4581383SWeston Andros Adamson 	 * prepare head request to be added to new pgio descriptor
578d4581383SWeston Andros Adamson 	 */
579d4581383SWeston Andros Adamson 	nfs_page_group_clear_bits(head);
580d4581383SWeston Andros Adamson 
581d4581383SWeston Andros Adamson 	/*
582d4581383SWeston Andros Adamson 	 * some part of the group was still on the inode list - otherwise
583d4581383SWeston Andros Adamson 	 * the group wouldn't be involved in async write.
584d4581383SWeston Andros Adamson 	 * grab a reference for the head request, iff it needs one.
585d4581383SWeston Andros Adamson 	 */
586d4581383SWeston Andros Adamson 	if (!test_and_set_bit(PG_INODE_REF, &head->wb_flags))
587d4581383SWeston Andros Adamson 		kref_get(&head->wb_kref);
588d4581383SWeston Andros Adamson 
589d4581383SWeston Andros Adamson 	nfs_page_group_unlock(head);
590d4581383SWeston Andros Adamson 
591411a99adSWeston Andros Adamson 	/* drop lock to clean uprequests on destroy list */
592d4581383SWeston Andros Adamson 	spin_unlock(&inode->i_lock);
593d4581383SWeston Andros Adamson 
594d4581383SWeston Andros Adamson 	nfs_destroy_unlinked_subrequests(destroy_list, head);
595d4581383SWeston Andros Adamson 
596d4581383SWeston Andros Adamson 	/* still holds ref on head from nfs_page_find_head_request_locked
597d4581383SWeston Andros Adamson 	 * and still has lock on head from lock loop */
598d4581383SWeston Andros Adamson 	return head;
599612c9384STrond Myklebust }
600074cc1deSTrond Myklebust 
6010bcbf039SPeng Tao static void nfs_write_error_remove_page(struct nfs_page *req)
6020bcbf039SPeng Tao {
6030bcbf039SPeng Tao 	nfs_unlock_request(req);
6040bcbf039SPeng Tao 	nfs_end_page_writeback(req);
6050bcbf039SPeng Tao 	generic_error_remove_page(page_file_mapping(req->wb_page),
6060bcbf039SPeng Tao 				  req->wb_page);
6071f84ccdfSFred Isaman 	nfs_release_request(req);
6080bcbf039SPeng Tao }
6090bcbf039SPeng Tao 
610a6598813STrond Myklebust static bool
611a6598813STrond Myklebust nfs_error_is_fatal_on_server(int err)
612a6598813STrond Myklebust {
613a6598813STrond Myklebust 	switch (err) {
614a6598813STrond Myklebust 	case 0:
615a6598813STrond Myklebust 	case -ERESTARTSYS:
616a6598813STrond Myklebust 	case -EINTR:
617a6598813STrond Myklebust 		return false;
618a6598813STrond Myklebust 	}
619a6598813STrond Myklebust 	return nfs_error_is_fatal(err);
620e261f51fSTrond Myklebust }
621074cc1deSTrond Myklebust 
622074cc1deSTrond Myklebust /*
623074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
624074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
625074cc1deSTrond Myklebust  */
626074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
627c373fff7STrond Myklebust 				struct page *page, bool nonblock)
628074cc1deSTrond Myklebust {
629074cc1deSTrond Myklebust 	struct nfs_page *req;
630074cc1deSTrond Myklebust 	int ret = 0;
631074cc1deSTrond Myklebust 
632d4581383SWeston Andros Adamson 	req = nfs_lock_and_join_requests(page, nonblock);
633074cc1deSTrond Myklebust 	if (!req)
634074cc1deSTrond Myklebust 		goto out;
635074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
636074cc1deSTrond Myklebust 	if (IS_ERR(req))
637074cc1deSTrond Myklebust 		goto out;
638074cc1deSTrond Myklebust 
639deed85e7STrond Myklebust 	nfs_set_page_writeback(page);
640deed85e7STrond Myklebust 	WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
641074cc1deSTrond Myklebust 
642deed85e7STrond Myklebust 	ret = 0;
643a6598813STrond Myklebust 	/* If there is a fatal error that covers this write, just exit */
644a6598813STrond Myklebust 	if (nfs_error_is_fatal_on_server(req->wb_context->error))
645a6598813STrond Myklebust 		goto out_launder;
646a6598813STrond Myklebust 
647f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
648074cc1deSTrond Myklebust 		ret = pgio->pg_error;
6490bcbf039SPeng Tao 		/*
650c373fff7STrond Myklebust 		 * Remove the problematic req upon fatal errors on the server
6510bcbf039SPeng Tao 		 */
6520bcbf039SPeng Tao 		if (nfs_error_is_fatal(ret)) {
6530bcbf039SPeng Tao 			nfs_context_set_write_error(req->wb_context, ret);
654c373fff7STrond Myklebust 			if (nfs_error_is_fatal_on_server(ret))
655a6598813STrond Myklebust 				goto out_launder;
656d6c843b9SPeng Tao 		}
6570bcbf039SPeng Tao 		nfs_redirty_request(req);
6580bcbf039SPeng Tao 		ret = -EAGAIN;
65940f90271STrond Myklebust 	} else
66040f90271STrond Myklebust 		nfs_add_stats(page_file_mapping(page)->host,
66140f90271STrond Myklebust 				NFSIOS_WRITEPAGES, 1);
662074cc1deSTrond Myklebust out:
663074cc1deSTrond Myklebust 	return ret;
664a6598813STrond Myklebust out_launder:
665a6598813STrond Myklebust 	nfs_write_error_remove_page(req);
666a6598813STrond Myklebust 	return ret;
667e261f51fSTrond Myklebust }
668e261f51fSTrond Myklebust 
669d6c843b9SPeng Tao static int nfs_do_writepage(struct page *page, struct writeback_control *wbc,
670c373fff7STrond Myklebust 			    struct nfs_pageio_descriptor *pgio)
671f758c885STrond Myklebust {
672cfb506e1STrond Myklebust 	int ret;
673f758c885STrond Myklebust 
6748cd79788SHuang Ying 	nfs_pageio_cond_complete(pgio, page_index(page));
675c373fff7STrond Myklebust 	ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE);
676cfb506e1STrond Myklebust 	if (ret == -EAGAIN) {
677cfb506e1STrond Myklebust 		redirty_page_for_writepage(wbc, page);
678cfb506e1STrond Myklebust 		ret = 0;
679cfb506e1STrond Myklebust 	}
680cfb506e1STrond Myklebust 	return ret;
681f758c885STrond Myklebust }
682f758c885STrond Myklebust 
683e261f51fSTrond Myklebust /*
6841da177e4SLinus Torvalds  * Write an mmapped page to the server.
6851da177e4SLinus Torvalds  */
686d6c843b9SPeng Tao static int nfs_writepage_locked(struct page *page,
687c373fff7STrond Myklebust 				struct writeback_control *wbc)
6881da177e4SLinus Torvalds {
689f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
69040f90271STrond Myklebust 	struct inode *inode = page_file_mapping(page)->host;
691e261f51fSTrond Myklebust 	int err;
6921da177e4SLinus Torvalds 
69340f90271STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
694811ed92eSTrond Myklebust 	nfs_pageio_init_write(&pgio, inode, 0,
695a20c93e3SChristoph Hellwig 				false, &nfs_async_write_completion_ops);
696c373fff7STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
697f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
698f758c885STrond Myklebust 	if (err < 0)
6994d770ccfSTrond Myklebust 		return err;
700f758c885STrond Myklebust 	if (pgio.pg_error < 0)
701f758c885STrond Myklebust 		return pgio.pg_error;
702f758c885STrond Myklebust 	return 0;
7034d770ccfSTrond Myklebust }
7044d770ccfSTrond Myklebust 
7054d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
7064d770ccfSTrond Myklebust {
707f758c885STrond Myklebust 	int ret;
7084d770ccfSTrond Myklebust 
709c373fff7STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
7101da177e4SLinus Torvalds 	unlock_page(page);
711f758c885STrond Myklebust 	return ret;
712f758c885STrond Myklebust }
713f758c885STrond Myklebust 
714f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
715f758c885STrond Myklebust {
716f758c885STrond Myklebust 	int ret;
717f758c885STrond Myklebust 
718c373fff7STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
719f758c885STrond Myklebust 	unlock_page(page);
720f758c885STrond Myklebust 	return ret;
7211da177e4SLinus Torvalds }
7221da177e4SLinus Torvalds 
723919e3bd9STrond Myklebust static void nfs_io_completion_commit(void *inode)
724919e3bd9STrond Myklebust {
725919e3bd9STrond Myklebust 	nfs_commit_inode(inode, 0);
726919e3bd9STrond Myklebust }
727919e3bd9STrond Myklebust 
7281da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
7291da177e4SLinus Torvalds {
7301da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
731c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
732919e3bd9STrond Myklebust 	struct nfs_io_completion *ioc = nfs_io_completion_alloc(GFP_NOFS);
7331da177e4SLinus Torvalds 	int err;
7341da177e4SLinus Torvalds 
73591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
73691d5b470SChuck Lever 
737919e3bd9STrond Myklebust 	if (ioc)
738919e3bd9STrond Myklebust 		nfs_io_completion_init(ioc, nfs_io_completion_commit, inode);
739919e3bd9STrond Myklebust 
740a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false,
741a20c93e3SChristoph Hellwig 				&nfs_async_write_completion_ops);
742919e3bd9STrond Myklebust 	pgio.pg_io_completion = ioc;
743f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
744c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
745919e3bd9STrond Myklebust 	nfs_io_completion_put(ioc);
74672cb77f4STrond Myklebust 
747f758c885STrond Myklebust 	if (err < 0)
74872cb77f4STrond Myklebust 		goto out_err;
74972cb77f4STrond Myklebust 	err = pgio.pg_error;
75072cb77f4STrond Myklebust 	if (err < 0)
75172cb77f4STrond Myklebust 		goto out_err;
752c63c7b05STrond Myklebust 	return 0;
75372cb77f4STrond Myklebust out_err:
75472cb77f4STrond Myklebust 	return err;
7551da177e4SLinus Torvalds }
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds /*
7581da177e4SLinus Torvalds  * Insert a write request into an inode
7591da177e4SLinus Torvalds  */
760d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
7611da177e4SLinus Torvalds {
7621da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
763e7d39069STrond Myklebust 
7642bfc6e56SWeston Andros Adamson 	WARN_ON_ONCE(req->wb_this_page != req);
7652bfc6e56SWeston Andros Adamson 
766e7d39069STrond Myklebust 	/* Lock the request! */
7677ad84aa9STrond Myklebust 	nfs_lock_request(req);
768e7d39069STrond Myklebust 
769e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
770cb1410c7SWeston Andros Adamson 	if (!nfsi->nrequests &&
771cb1410c7SWeston Andros Adamson 	    NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
772a9a4a87aSTrond Myklebust 		inode->i_version++;
77329418aa4SMel Gorman 	/*
77429418aa4SMel Gorman 	 * Swap-space should not get truncated. Hence no need to plug the race
77529418aa4SMel Gorman 	 * with invalidate/truncate.
77629418aa4SMel Gorman 	 */
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 	}
782cb1410c7SWeston Andros Adamson 	nfsi->nrequests++;
78317089a29SWeston Andros Adamson 	/* this a head request for a page group - mark it as having an
784cb1410c7SWeston Andros Adamson 	 * extra reference so sub groups can follow suit.
785cb1410c7SWeston Andros Adamson 	 * This flag also informs pgio layer when to bump nrequests when
786cb1410c7SWeston Andros Adamson 	 * adding subrequests. */
78717089a29SWeston Andros Adamson 	WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags));
788c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
789e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
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 {
7972b0143b5SDavid Howells 	struct inode *inode = d_inode(req->wb_context->dentry);
7981da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
79920633f04SWeston Andros Adamson 	struct nfs_page *head;
80020633f04SWeston Andros Adamson 
80120633f04SWeston Andros Adamson 	if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) {
80220633f04SWeston Andros Adamson 		head = req->wb_head;
8031da177e4SLinus Torvalds 
804587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
80567911c8fSAnna Schumaker 		if (likely(head->wb_page && !PageSwapCache(head->wb_page))) {
80620633f04SWeston Andros Adamson 			set_page_private(head->wb_page, 0);
80720633f04SWeston Andros Adamson 			ClearPagePrivate(head->wb_page);
80820633f04SWeston Andros Adamson 			clear_bit(PG_MAPPED, &head->wb_flags);
80929418aa4SMel Gorman 		}
810cb1410c7SWeston Andros Adamson 		nfsi->nrequests--;
811cb1410c7SWeston Andros Adamson 		spin_unlock(&inode->i_lock);
812cb1410c7SWeston Andros Adamson 	} else {
813cb1410c7SWeston Andros Adamson 		spin_lock(&inode->i_lock);
814cb1410c7SWeston Andros Adamson 		nfsi->nrequests--;
815587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
81620633f04SWeston Andros Adamson 	}
81717089a29SWeston Andros Adamson 
81817089a29SWeston Andros Adamson 	if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags))
8191da177e4SLinus Torvalds 		nfs_release_request(req);
8201da177e4SLinus Torvalds }
8211da177e4SLinus Torvalds 
82261822ab5STrond Myklebust static void
8236d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
82461822ab5STrond Myklebust {
82567911c8fSAnna Schumaker 	if (req->wb_page)
82661822ab5STrond Myklebust 		__set_page_dirty_nobuffers(req->wb_page);
82761822ab5STrond Myklebust }
82861822ab5STrond Myklebust 
8293a3908c8STrond Myklebust /*
8303a3908c8STrond Myklebust  * nfs_page_search_commits_for_head_request_locked
8313a3908c8STrond Myklebust  *
8323a3908c8STrond Myklebust  * Search through commit lists on @inode for the head request for @page.
8333a3908c8STrond Myklebust  * Must be called while holding the inode (which is cinfo) lock.
8343a3908c8STrond Myklebust  *
8353a3908c8STrond Myklebust  * Returns the head request if found, or NULL if not found.
8363a3908c8STrond Myklebust  */
8373a3908c8STrond Myklebust static struct nfs_page *
8383a3908c8STrond Myklebust nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
8393a3908c8STrond Myklebust 						struct page *page)
8403a3908c8STrond Myklebust {
8413a3908c8STrond Myklebust 	struct nfs_page *freq, *t;
8423a3908c8STrond Myklebust 	struct nfs_commit_info cinfo;
8433a3908c8STrond Myklebust 	struct inode *inode = &nfsi->vfs_inode;
8443a3908c8STrond Myklebust 
8453a3908c8STrond Myklebust 	nfs_init_cinfo_from_inode(&cinfo, inode);
8463a3908c8STrond Myklebust 
8473a3908c8STrond Myklebust 	/* search through pnfs commit lists */
8483a3908c8STrond Myklebust 	freq = pnfs_search_commit_reqs(inode, &cinfo, page);
8493a3908c8STrond Myklebust 	if (freq)
8503a3908c8STrond Myklebust 		return freq->wb_head;
8513a3908c8STrond Myklebust 
8523a3908c8STrond Myklebust 	/* Linearly search the commit list for the correct request */
8533a3908c8STrond Myklebust 	list_for_each_entry_safe(freq, t, &cinfo.mds->list, wb_list) {
8543a3908c8STrond Myklebust 		if (freq->wb_page == page)
8553a3908c8STrond Myklebust 			return freq->wb_head;
8563a3908c8STrond Myklebust 	}
8573a3908c8STrond Myklebust 
8583a3908c8STrond Myklebust 	return NULL;
8593a3908c8STrond Myklebust }
8603a3908c8STrond Myklebust 
8618dd37758STrond Myklebust /**
86286d80f97STrond Myklebust  * nfs_request_add_commit_list_locked - add request to a commit list
86386d80f97STrond Myklebust  * @req: pointer to a struct nfs_page
86486d80f97STrond Myklebust  * @dst: commit list head
86586d80f97STrond Myklebust  * @cinfo: holds list lock and accounting info
86686d80f97STrond Myklebust  *
86786d80f97STrond Myklebust  * This sets the PG_CLEAN bit, updates the cinfo count of
86886d80f97STrond Myklebust  * number of outstanding requests requiring a commit as well as
86986d80f97STrond Myklebust  * the MM page stats.
87086d80f97STrond Myklebust  *
871fe238e60SDave Wysochanski  * The caller must hold cinfo->inode->i_lock, and the nfs_page lock.
87286d80f97STrond Myklebust  */
87386d80f97STrond Myklebust void
87486d80f97STrond Myklebust nfs_request_add_commit_list_locked(struct nfs_page *req, struct list_head *dst,
87586d80f97STrond Myklebust 			    struct nfs_commit_info *cinfo)
87686d80f97STrond Myklebust {
87786d80f97STrond Myklebust 	set_bit(PG_CLEAN, &req->wb_flags);
87886d80f97STrond Myklebust 	nfs_list_add_request(req, dst);
87986d80f97STrond Myklebust 	cinfo->mds->ncommit++;
88086d80f97STrond Myklebust }
88186d80f97STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked);
88286d80f97STrond Myklebust 
88386d80f97STrond Myklebust /**
8848dd37758STrond Myklebust  * nfs_request_add_commit_list - add request to a commit list
8858dd37758STrond Myklebust  * @req: pointer to a struct nfs_page
886ea2cf228SFred Isaman  * @dst: commit list head
887ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
8888dd37758STrond Myklebust  *
889ea2cf228SFred Isaman  * This sets the PG_CLEAN bit, updates the cinfo count of
8908dd37758STrond Myklebust  * number of outstanding requests requiring a commit as well as
8918dd37758STrond Myklebust  * the MM page stats.
8928dd37758STrond Myklebust  *
893ea2cf228SFred Isaman  * The caller must _not_ hold the cinfo->lock, but must be
8948dd37758STrond Myklebust  * holding the nfs_page lock.
8958dd37758STrond Myklebust  */
8968dd37758STrond Myklebust void
8976272dcc6SAnna Schumaker nfs_request_add_commit_list(struct nfs_page *req, struct nfs_commit_info *cinfo)
8988dd37758STrond Myklebust {
899fe238e60SDave Wysochanski 	spin_lock(&cinfo->inode->i_lock);
9006272dcc6SAnna Schumaker 	nfs_request_add_commit_list_locked(req, &cinfo->mds->list, cinfo);
901fe238e60SDave Wysochanski 	spin_unlock(&cinfo->inode->i_lock);
90267911c8fSAnna Schumaker 	if (req->wb_page)
90386d80f97STrond Myklebust 		nfs_mark_page_unstable(req->wb_page, cinfo);
9048dd37758STrond Myklebust }
9058dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
9068dd37758STrond Myklebust 
9078dd37758STrond Myklebust /**
9088dd37758STrond Myklebust  * nfs_request_remove_commit_list - Remove request from a commit list
9098dd37758STrond Myklebust  * @req: pointer to a nfs_page
910ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
9118dd37758STrond Myklebust  *
912ea2cf228SFred Isaman  * This clears the PG_CLEAN bit, and updates the cinfo's count of
9138dd37758STrond Myklebust  * number of outstanding requests requiring a commit
9148dd37758STrond Myklebust  * It does not update the MM page stats.
9158dd37758STrond Myklebust  *
916ea2cf228SFred Isaman  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
9178dd37758STrond Myklebust  */
9188dd37758STrond Myklebust void
919ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req,
920ea2cf228SFred Isaman 			       struct nfs_commit_info *cinfo)
9218dd37758STrond Myklebust {
9228dd37758STrond Myklebust 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
9238dd37758STrond Myklebust 		return;
9248dd37758STrond Myklebust 	nfs_list_remove_request(req);
925ea2cf228SFred Isaman 	cinfo->mds->ncommit--;
9268dd37758STrond Myklebust }
9278dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
9288dd37758STrond Myklebust 
929ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
930ea2cf228SFred Isaman 				      struct inode *inode)
931ea2cf228SFred Isaman {
932fe238e60SDave Wysochanski 	cinfo->inode = inode;
933ea2cf228SFred Isaman 	cinfo->mds = &NFS_I(inode)->commit_info;
934ea2cf228SFred Isaman 	cinfo->ds = pnfs_get_ds_info(inode);
935b359f9d0SFred Isaman 	cinfo->dreq = NULL;
936f453a54aSFred Isaman 	cinfo->completion_ops = &nfs_commit_completion_ops;
937ea2cf228SFred Isaman }
938ea2cf228SFred Isaman 
939ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
940ea2cf228SFred Isaman 		    struct inode *inode,
941ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq)
942ea2cf228SFred Isaman {
9431763da12SFred Isaman 	if (dreq)
9441763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
9451763da12SFred Isaman 	else
946ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(cinfo, inode);
947ea2cf228SFred Isaman }
948ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo);
9498dd37758STrond Myklebust 
9501da177e4SLinus Torvalds /*
9511da177e4SLinus Torvalds  * Add a request to the inode's commit list.
9521da177e4SLinus Torvalds  */
9531763da12SFred Isaman void
954ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
955b57ff130SWeston Andros Adamson 			struct nfs_commit_info *cinfo, u32 ds_commit_idx)
9561da177e4SLinus Torvalds {
957b57ff130SWeston Andros Adamson 	if (pnfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx))
9588dd37758STrond Myklebust 		return;
9596272dcc6SAnna Schumaker 	nfs_request_add_commit_list(req, cinfo);
9601da177e4SLinus Torvalds }
9618e821cadSTrond Myklebust 
962d6d6dc7cSFred Isaman static void
963d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page)
964e468bae9STrond Myklebust {
96511fb9989SMel Gorman 	dec_node_page_state(page, NR_UNSTABLE_NFS);
96693f78d88STejun Heo 	dec_wb_stat(&inode_to_bdi(page_file_mapping(page)->host)->wb,
96793f78d88STejun Heo 		    WB_RECLAIMABLE);
968e468bae9STrond Myklebust }
969d6d6dc7cSFred Isaman 
970411a99adSWeston Andros Adamson /* Called holding inode (/cinfo) lock */
9718dd37758STrond Myklebust static void
972d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req)
973d6d6dc7cSFred Isaman {
9748dd37758STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
9752b0143b5SDavid Howells 		struct inode *inode = d_inode(req->wb_context->dentry);
976ea2cf228SFred Isaman 		struct nfs_commit_info cinfo;
977d6d6dc7cSFred Isaman 
978ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(&cinfo, inode);
979ea2cf228SFred Isaman 		if (!pnfs_clear_request_commit(req, &cinfo)) {
980ea2cf228SFred Isaman 			nfs_request_remove_commit_list(req, &cinfo);
981d6d6dc7cSFred Isaman 		}
9828dd37758STrond Myklebust 		nfs_clear_page_commit(req->wb_page);
9838dd37758STrond Myklebust 	}
984e468bae9STrond Myklebust }
985e468bae9STrond Myklebust 
986d45f60c6SWeston Andros Adamson int nfs_write_need_commit(struct nfs_pgio_header *hdr)
9878e821cadSTrond Myklebust {
988c65e6254SWeston Andros Adamson 	if (hdr->verf.committed == NFS_DATA_SYNC)
989d45f60c6SWeston Andros Adamson 		return hdr->lseg == NULL;
990c65e6254SWeston Andros Adamson 	return hdr->verf.committed != NFS_FILE_SYNC;
9918e821cadSTrond Myklebust }
9928e821cadSTrond Myklebust 
993919e3bd9STrond Myklebust static void nfs_async_write_init(struct nfs_pgio_header *hdr)
994919e3bd9STrond Myklebust {
995919e3bd9STrond Myklebust 	nfs_io_completion_get(hdr->io_completion);
996919e3bd9STrond Myklebust }
997919e3bd9STrond Myklebust 
998061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
9996c75dc0dSFred Isaman {
1000ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
10016c75dc0dSFred Isaman 	unsigned long bytes = 0;
10026c75dc0dSFred Isaman 
10036c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
10046c75dc0dSFred Isaman 		goto out;
1005ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
10066c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
10076c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
10086c75dc0dSFred Isaman 
10096c75dc0dSFred Isaman 		bytes += req->wb_bytes;
10106c75dc0dSFred Isaman 		nfs_list_remove_request(req);
10116c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
10126c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
1013d1182b33STrond Myklebust 			nfs_set_pageerror(req->wb_page);
10146c75dc0dSFred Isaman 			nfs_context_set_write_error(req->wb_context, hdr->error);
10156c75dc0dSFred Isaman 			goto remove_req;
10166c75dc0dSFred Isaman 		}
1017c65e6254SWeston Andros Adamson 		if (nfs_write_need_commit(hdr)) {
1018f79d06f5SAnna Schumaker 			memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
1019b57ff130SWeston Andros Adamson 			nfs_mark_request_commit(req, hdr->lseg, &cinfo,
1020a7d42ddbSWeston Andros Adamson 				hdr->pgio_mirror_idx);
10216c75dc0dSFred Isaman 			goto next;
10226c75dc0dSFred Isaman 		}
10236c75dc0dSFred Isaman remove_req:
10246c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
10256c75dc0dSFred Isaman next:
10261d1afcbcSTrond Myklebust 		nfs_unlock_request(req);
102720633f04SWeston Andros Adamson 		nfs_end_page_writeback(req);
10283aff4ebbSTrond Myklebust 		nfs_release_request(req);
10296c75dc0dSFred Isaman 	}
10306c75dc0dSFred Isaman out:
1031919e3bd9STrond Myklebust 	nfs_io_completion_put(hdr->io_completion);
10326c75dc0dSFred Isaman 	hdr->release(hdr);
10336c75dc0dSFred Isaman }
10346c75dc0dSFred Isaman 
1035ce59515cSAnna Schumaker unsigned long
1036ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
1037fb8a1f11STrond Myklebust {
1038ea2cf228SFred Isaman 	return cinfo->mds->ncommit;
1039fb8a1f11STrond Myklebust }
1040fb8a1f11STrond Myklebust 
1041fe238e60SDave Wysochanski /* cinfo->inode->i_lock held by caller */
10421763da12SFred Isaman int
1043ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
1044ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
1045d6d6dc7cSFred Isaman {
1046d6d6dc7cSFred Isaman 	struct nfs_page *req, *tmp;
1047d6d6dc7cSFred Isaman 	int ret = 0;
1048d6d6dc7cSFred Isaman 
1049d6d6dc7cSFred Isaman 	list_for_each_entry_safe(req, tmp, src, wb_list) {
10508dd37758STrond Myklebust 		if (!nfs_lock_request(req))
10518dd37758STrond Myklebust 			continue;
10527ad84aa9STrond Myklebust 		kref_get(&req->wb_kref);
1053fe238e60SDave Wysochanski 		if (cond_resched_lock(&cinfo->inode->i_lock))
10543b3be88dSTrond Myklebust 			list_safe_reset_next(req, tmp, wb_list);
1055ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
10568dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
1057d6d6dc7cSFred Isaman 		ret++;
10581763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
1059d6d6dc7cSFred Isaman 			break;
1060d6d6dc7cSFred Isaman 	}
1061d6d6dc7cSFred Isaman 	return ret;
1062d6d6dc7cSFred Isaman }
1063d6d6dc7cSFred Isaman 
10641da177e4SLinus Torvalds /*
10651da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
10661da177e4SLinus Torvalds  * @inode: NFS inode to scan
1067ea2cf228SFred Isaman  * @dst: mds destination list
1068ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
10691da177e4SLinus Torvalds  *
10701da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
10711da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
10721da177e4SLinus Torvalds  */
10731763da12SFred Isaman int
1074ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
1075ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
10761da177e4SLinus Torvalds {
1077d6d6dc7cSFred Isaman 	int ret = 0;
1078fb8a1f11STrond Myklebust 
1079fe238e60SDave Wysochanski 	spin_lock(&cinfo->inode->i_lock);
1080ea2cf228SFred Isaman 	if (cinfo->mds->ncommit > 0) {
10818dd37758STrond Myklebust 		const int max = INT_MAX;
1082d6d6dc7cSFred Isaman 
1083ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
1084ea2cf228SFred Isaman 					   cinfo, max);
1085ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
1086d6d6dc7cSFred Isaman 	}
1087fe238e60SDave Wysochanski 	spin_unlock(&cinfo->inode->i_lock);
1088ff778d02STrond Myklebust 	return ret;
10891da177e4SLinus Torvalds }
1090d6d6dc7cSFred Isaman 
10911da177e4SLinus Torvalds /*
1092e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
1093e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
10941da177e4SLinus Torvalds  *
1095e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
1096e7d39069STrond Myklebust  * to disk.
10971da177e4SLinus Torvalds  */
1098e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
1099e7d39069STrond Myklebust 		struct page *page,
1100e7d39069STrond Myklebust 		unsigned int offset,
1101e7d39069STrond Myklebust 		unsigned int bytes)
11021da177e4SLinus Torvalds {
1103e7d39069STrond Myklebust 	struct nfs_page *req;
1104e7d39069STrond Myklebust 	unsigned int rqend;
1105e7d39069STrond Myklebust 	unsigned int end;
11061da177e4SLinus Torvalds 	int error;
1107277459d2STrond Myklebust 
1108e7d39069STrond Myklebust 	if (!PagePrivate(page))
1109e7d39069STrond Myklebust 		return NULL;
1110e7d39069STrond Myklebust 
1111e7d39069STrond Myklebust 	end = offset + bytes;
1112e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
1113e7d39069STrond Myklebust 
1114e7d39069STrond Myklebust 	for (;;) {
111584d3a9a9SWeston Andros Adamson 		req = nfs_page_find_head_request_locked(NFS_I(inode), page);
1116e7d39069STrond Myklebust 		if (req == NULL)
1117e7d39069STrond Myklebust 			goto out_unlock;
1118e7d39069STrond Myklebust 
11192bfc6e56SWeston Andros Adamson 		/* should be handled by nfs_flush_incompatible */
11202bfc6e56SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_head != req);
11212bfc6e56SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_this_page != req);
11222bfc6e56SWeston Andros Adamson 
1123e7d39069STrond Myklebust 		rqend = req->wb_offset + req->wb_bytes;
1124e7d39069STrond Myklebust 		/*
1125e7d39069STrond Myklebust 		 * Tell the caller to flush out the request if
1126e7d39069STrond Myklebust 		 * the offsets are non-contiguous.
1127e7d39069STrond Myklebust 		 * Note: nfs_flush_incompatible() will already
1128e7d39069STrond Myklebust 		 * have flushed out requests having wrong owners.
1129e7d39069STrond Myklebust 		 */
1130e468bae9STrond Myklebust 		if (offset > rqend
1131e7d39069STrond Myklebust 		    || end < req->wb_offset)
1132e7d39069STrond Myklebust 			goto out_flushme;
1133e7d39069STrond Myklebust 
11347ad84aa9STrond Myklebust 		if (nfs_lock_request(req))
1135e7d39069STrond Myklebust 			break;
1136e7d39069STrond Myklebust 
1137e7d39069STrond Myklebust 		/* The request is locked, so wait and then retry */
1138587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
11391da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
11401da177e4SLinus Torvalds 		nfs_release_request(req);
1141e7d39069STrond Myklebust 		if (error != 0)
1142e7d39069STrond Myklebust 			goto out_err;
1143e7d39069STrond Myklebust 		spin_lock(&inode->i_lock);
11441da177e4SLinus Torvalds 	}
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
11471da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
11481da177e4SLinus Torvalds 		req->wb_offset = offset;
11491da177e4SLinus Torvalds 		req->wb_pgbase = offset;
11501da177e4SLinus Torvalds 	}
11511da177e4SLinus Torvalds 	if (end > rqend)
11521da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
1153e7d39069STrond Myklebust 	else
1154e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
1155e7d39069STrond Myklebust out_unlock:
1156ca138f36SFred Isaman 	if (req)
11578dd37758STrond Myklebust 		nfs_clear_request_commit(req);
1158411a99adSWeston Andros Adamson 	spin_unlock(&inode->i_lock);
1159e7d39069STrond Myklebust 	return req;
1160e7d39069STrond Myklebust out_flushme:
1161e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
1162e7d39069STrond Myklebust 	nfs_release_request(req);
1163e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
1164e7d39069STrond Myklebust out_err:
1165e7d39069STrond Myklebust 	return ERR_PTR(error);
1166e7d39069STrond Myklebust }
11671da177e4SLinus Torvalds 
1168e7d39069STrond Myklebust /*
1169e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
1170e7d39069STrond Myklebust  *
1171e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
1172e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
1173e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
1174e7d39069STrond Myklebust  */
1175e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
1176e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
1177e7d39069STrond Myklebust {
1178d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
1179e7d39069STrond Myklebust 	struct nfs_page	*req;
1180e7d39069STrond Myklebust 
1181e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
1182e7d39069STrond Myklebust 	if (req != NULL)
1183e7d39069STrond Myklebust 		goto out;
11842bfc6e56SWeston Andros Adamson 	req = nfs_create_request(ctx, page, NULL, offset, bytes);
1185e7d39069STrond Myklebust 	if (IS_ERR(req))
1186e7d39069STrond Myklebust 		goto out;
1187d6d6dc7cSFred Isaman 	nfs_inode_add_request(inode, req);
1188efc91ed0STrond Myklebust out:
118961e930a9STrond Myklebust 	return req;
11901da177e4SLinus Torvalds }
11911da177e4SLinus Torvalds 
1192e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
1193e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
1194e7d39069STrond Myklebust {
1195e7d39069STrond Myklebust 	struct nfs_page	*req;
1196e7d39069STrond Myklebust 
1197e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
1198e7d39069STrond Myklebust 	if (IS_ERR(req))
1199e7d39069STrond Myklebust 		return PTR_ERR(req);
1200e7d39069STrond Myklebust 	/* Update file length */
1201e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
1202d72ddcbaSWeston Andros Adamson 	nfs_mark_uptodate(req);
1203a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
12041d1afcbcSTrond Myklebust 	nfs_unlock_and_release_request(req);
1205e7d39069STrond Myklebust 	return 0;
1206e7d39069STrond Myklebust }
1207e7d39069STrond Myklebust 
12081da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
12091da177e4SLinus Torvalds {
1210cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
12112a369153STrond Myklebust 	struct nfs_lock_context *l_ctx;
1212bd61e0a9SJeff Layton 	struct file_lock_context *flctx = file_inode(file)->i_flctx;
12131da177e4SLinus Torvalds 	struct nfs_page	*req;
12141a54533eSTrond Myklebust 	int do_flush, status;
12151da177e4SLinus Torvalds 	/*
12161da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
12171da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
12181da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
12191da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
12201da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
12211da177e4SLinus Torvalds 	 * dropped page.
12221da177e4SLinus Torvalds 	 */
12231a54533eSTrond Myklebust 	do {
122484d3a9a9SWeston Andros Adamson 		req = nfs_page_find_head_request(page);
12251a54533eSTrond Myklebust 		if (req == NULL)
12261a54533eSTrond Myklebust 			return 0;
12272a369153STrond Myklebust 		l_ctx = req->wb_lock_context;
1228138a2935STrond Myklebust 		do_flush = req->wb_page != page ||
1229138a2935STrond Myklebust 			!nfs_match_open_context(req->wb_context, ctx);
12302bfc6e56SWeston Andros Adamson 		/* for now, flush if more than 1 request in page_group */
12312bfc6e56SWeston Andros Adamson 		do_flush |= req->wb_this_page != req;
1232bd61e0a9SJeff Layton 		if (l_ctx && flctx &&
1233bd61e0a9SJeff Layton 		    !(list_empty_careful(&flctx->flc_posix) &&
1234bd61e0a9SJeff Layton 		      list_empty_careful(&flctx->flc_flock))) {
1235d51fdb87SNeilBrown 			do_flush |= l_ctx->lockowner != current->files;
12365263e31eSJeff Layton 		}
12371da177e4SLinus Torvalds 		nfs_release_request(req);
12381a54533eSTrond Myklebust 		if (!do_flush)
12391a54533eSTrond Myklebust 			return 0;
1240d56b4ddfSMel Gorman 		status = nfs_wb_page(page_file_mapping(page)->host, page);
12411a54533eSTrond Myklebust 	} while (status == 0);
12421a54533eSTrond Myklebust 	return status;
12431da177e4SLinus Torvalds }
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds /*
1246dc24826bSAndy Adamson  * Avoid buffered writes when a open context credential's key would
1247dc24826bSAndy Adamson  * expire soon.
1248dc24826bSAndy Adamson  *
1249dc24826bSAndy Adamson  * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
1250dc24826bSAndy Adamson  *
1251dc24826bSAndy Adamson  * Return 0 and set a credential flag which triggers the inode to flush
1252dc24826bSAndy Adamson  * and performs  NFS_FILE_SYNC writes if the key will expired within
1253dc24826bSAndy Adamson  * RPC_KEY_EXPIRE_TIMEO.
1254dc24826bSAndy Adamson  */
1255dc24826bSAndy Adamson int
1256dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode)
1257dc24826bSAndy Adamson {
1258dc24826bSAndy Adamson 	struct nfs_open_context *ctx = nfs_file_open_context(filp);
1259dc24826bSAndy Adamson 	struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
1260dc24826bSAndy Adamson 
1261dc24826bSAndy Adamson 	return rpcauth_key_timeout_notify(auth, ctx->cred);
1262dc24826bSAndy Adamson }
1263dc24826bSAndy Adamson 
1264dc24826bSAndy Adamson /*
1265dc24826bSAndy Adamson  * Test if the open context credential key is marked to expire soon.
1266dc24826bSAndy Adamson  */
1267ce52914eSScott Mayhew bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode)
1268dc24826bSAndy Adamson {
1269ce52914eSScott Mayhew 	struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
1270ce52914eSScott Mayhew 
1271ce52914eSScott Mayhew 	return rpcauth_cred_key_to_expire(auth, ctx->cred);
1272dc24826bSAndy Adamson }
1273dc24826bSAndy Adamson 
1274dc24826bSAndy Adamson /*
12755d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
12765d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
12775d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
12785d47a356STrond Myklebust  */
12798d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
12805d47a356STrond Myklebust {
1281d529ef83SJeff Layton 	struct nfs_inode *nfsi = NFS_I(inode);
1282d529ef83SJeff Layton 
12838d197a56STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
12848d197a56STrond Myklebust 		goto out;
128518dd78c4SScott Mayhew 	if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
1286d529ef83SJeff Layton 		return false;
12874db72b40SJeff Layton 	smp_rmb();
1288d529ef83SJeff Layton 	if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags))
12898d197a56STrond Myklebust 		return false;
12908d197a56STrond Myklebust out:
129118dd78c4SScott Mayhew 	if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
129218dd78c4SScott Mayhew 		return false;
12938d197a56STrond Myklebust 	return PageUptodate(page) != 0;
12945d47a356STrond Myklebust }
12955d47a356STrond Myklebust 
12965263e31eSJeff Layton static bool
12975263e31eSJeff Layton is_whole_file_wrlock(struct file_lock *fl)
12985263e31eSJeff Layton {
12995263e31eSJeff Layton 	return fl->fl_start == 0 && fl->fl_end == OFFSET_MAX &&
13005263e31eSJeff Layton 			fl->fl_type == F_WRLCK;
13015263e31eSJeff Layton }
13025263e31eSJeff Layton 
1303c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or
1304c7559663SScott Mayhew  * if we have the whole file locked for writing), it may be more efficient to
1305c7559663SScott Mayhew  * extend the write to cover the entire page in order to avoid fragmentation
1306c7559663SScott Mayhew  * inefficiencies.
1307c7559663SScott Mayhew  *
1308263b4509SScott Mayhew  * If the file is opened for synchronous writes then we can just skip the rest
1309263b4509SScott Mayhew  * of the checks.
1310c7559663SScott Mayhew  */
1311c7559663SScott Mayhew static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode)
1312c7559663SScott Mayhew {
13135263e31eSJeff Layton 	int ret;
13145263e31eSJeff Layton 	struct file_lock_context *flctx = inode->i_flctx;
13155263e31eSJeff Layton 	struct file_lock *fl;
13165263e31eSJeff Layton 
1317c7559663SScott Mayhew 	if (file->f_flags & O_DSYNC)
1318c7559663SScott Mayhew 		return 0;
1319263b4509SScott Mayhew 	if (!nfs_write_pageuptodate(page, inode))
1320263b4509SScott Mayhew 		return 0;
1321c7559663SScott Mayhew 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
1322c7559663SScott Mayhew 		return 1;
1323bd61e0a9SJeff Layton 	if (!flctx || (list_empty_careful(&flctx->flc_flock) &&
1324bd61e0a9SJeff Layton 		       list_empty_careful(&flctx->flc_posix)))
13258fa4592aSTrond Myklebust 		return 1;
13265263e31eSJeff Layton 
13275263e31eSJeff Layton 	/* Check to see if there are whole file write locks */
13285263e31eSJeff Layton 	ret = 0;
13296109c850SJeff Layton 	spin_lock(&flctx->flc_lock);
1330bd61e0a9SJeff Layton 	if (!list_empty(&flctx->flc_posix)) {
1331bd61e0a9SJeff Layton 		fl = list_first_entry(&flctx->flc_posix, struct file_lock,
1332bd61e0a9SJeff Layton 					fl_list);
1333bd61e0a9SJeff Layton 		if (is_whole_file_wrlock(fl))
13345263e31eSJeff Layton 			ret = 1;
1335bd61e0a9SJeff Layton 	} else if (!list_empty(&flctx->flc_flock)) {
13365263e31eSJeff Layton 		fl = list_first_entry(&flctx->flc_flock, struct file_lock,
13375263e31eSJeff Layton 					fl_list);
13385263e31eSJeff Layton 		if (fl->fl_type == F_WRLCK)
13395263e31eSJeff Layton 			ret = 1;
13405263e31eSJeff Layton 	}
13416109c850SJeff Layton 	spin_unlock(&flctx->flc_lock);
13425263e31eSJeff Layton 	return ret;
1343c7559663SScott Mayhew }
1344c7559663SScott Mayhew 
13455d47a356STrond Myklebust /*
13461da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
13471da177e4SLinus Torvalds  *
13481da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
13491da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
13501da177e4SLinus Torvalds  */
13511da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
13521da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
13531da177e4SLinus Torvalds {
1354cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
1355d56b4ddfSMel Gorman 	struct inode	*inode = page_file_mapping(page)->host;
13561da177e4SLinus Torvalds 	int		status = 0;
13571da177e4SLinus Torvalds 
135891d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
135991d5b470SChuck Lever 
13606de1472fSAl Viro 	dprintk("NFS:       nfs_updatepage(%pD2 %d@%lld)\n",
13616de1472fSAl Viro 		file, count, (long long)(page_file_offset(page) + offset));
13621da177e4SLinus Torvalds 
1363149a4fddSBenjamin Coddington 	if (!count)
1364149a4fddSBenjamin Coddington 		goto out;
1365149a4fddSBenjamin Coddington 
1366c7559663SScott Mayhew 	if (nfs_can_extend_write(file, page, inode)) {
136749a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
13681da177e4SLinus Torvalds 		offset = 0;
13691da177e4SLinus Torvalds 	}
13701da177e4SLinus Torvalds 
1371e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
137203fa9e84STrond Myklebust 	if (status < 0)
137303fa9e84STrond Myklebust 		nfs_set_pageerror(page);
137459b7c05fSTrond Myklebust 	else
137559b7c05fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
1376149a4fddSBenjamin Coddington out:
137748186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
13781da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
13791da177e4SLinus Torvalds 	return status;
13801da177e4SLinus Torvalds }
13811da177e4SLinus Torvalds 
13823ff7576dSTrond Myklebust static int flush_task_priority(int how)
13831da177e4SLinus Torvalds {
13841da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
13851da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
13861da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
13871da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
13881da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
13891da177e4SLinus Torvalds 	}
13901da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
13911da177e4SLinus Torvalds }
13921da177e4SLinus Torvalds 
1393d45f60c6SWeston Andros Adamson static void nfs_initiate_write(struct nfs_pgio_header *hdr,
1394d45f60c6SWeston Andros Adamson 			       struct rpc_message *msg,
1395abde71f4STom Haynes 			       const struct nfs_rpc_ops *rpc_ops,
13961ed26f33SAnna Schumaker 			       struct rpc_task_setup *task_setup_data, int how)
13971da177e4SLinus Torvalds {
13983ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
13991da177e4SLinus Torvalds 
14001ed26f33SAnna Schumaker 	task_setup_data->priority = priority;
1401abde71f4STom Haynes 	rpc_ops->write_setup(hdr, msg);
1402d138d5d1SAndy Adamson 
1403abde71f4STom Haynes 	nfs4_state_protect_write(NFS_SERVER(hdr->inode)->nfs_client,
1404d45f60c6SWeston Andros Adamson 				 &task_setup_data->rpc_client, msg, hdr);
1405275acaafSTrond Myklebust }
1406275acaafSTrond Myklebust 
14076d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
14086d884e8fSFred  * call this on each, which will prepare them to be retried on next
14096d884e8fSFred  * writeback using standard nfs.
14106d884e8fSFred  */
14116d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
14126d884e8fSFred {
14136d884e8fSFred 	nfs_mark_request_dirty(req);
1414c7070113STrond Myklebust 	set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
14151d1afcbcSTrond Myklebust 	nfs_unlock_request(req);
141620633f04SWeston Andros Adamson 	nfs_end_page_writeback(req);
14173aff4ebbSTrond Myklebust 	nfs_release_request(req);
14186d884e8fSFred }
14196d884e8fSFred 
1420061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head)
14216c75dc0dSFred Isaman {
14226c75dc0dSFred Isaman 	struct nfs_page	*req;
14236c75dc0dSFred Isaman 
14246c75dc0dSFred Isaman 	while (!list_empty(head)) {
14256c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
14266c75dc0dSFred Isaman 		nfs_list_remove_request(req);
14276c75dc0dSFred Isaman 		nfs_redirty_request(req);
14286c75dc0dSFred Isaman 	}
14296c75dc0dSFred Isaman }
14306c75dc0dSFred Isaman 
1431dc602dd7STrond Myklebust static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr)
1432dc602dd7STrond Myklebust {
1433dc602dd7STrond Myklebust 	nfs_async_write_error(&hdr->pages);
1434dc602dd7STrond Myklebust }
1435dc602dd7STrond Myklebust 
1436061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1437919e3bd9STrond Myklebust 	.init_hdr = nfs_async_write_init,
1438061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1439061ae2edSFred Isaman 	.completion = nfs_write_completion,
1440dc602dd7STrond Myklebust 	.reschedule_io = nfs_async_write_reschedule_io,
1441061ae2edSFred Isaman };
1442061ae2edSFred Isaman 
144357208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1444a20c93e3SChristoph Hellwig 			       struct inode *inode, int ioflags, bool force_mds,
1445061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
14461751c363STrond Myklebust {
1447a20c93e3SChristoph Hellwig 	struct nfs_server *server = NFS_SERVER(inode);
144841d8d5b7SAnna Schumaker 	const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
1449a20c93e3SChristoph Hellwig 
1450a20c93e3SChristoph Hellwig #ifdef CONFIG_NFS_V4_1
1451a20c93e3SChristoph Hellwig 	if (server->pnfs_curr_ld && !force_mds)
1452a20c93e3SChristoph Hellwig 		pg_ops = server->pnfs_curr_ld->pg_write_ops;
1453a20c93e3SChristoph Hellwig #endif
14544a0de55cSAnna Schumaker 	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
1455fbe77c30SBenjamin Coddington 			server->wsize, ioflags, GFP_NOIO);
14561751c363STrond Myklebust }
1457ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
14581751c363STrond Myklebust 
1459dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1460dce81290STrond Myklebust {
1461a7d42ddbSWeston Andros Adamson 	struct nfs_pgio_mirror *mirror;
1462a7d42ddbSWeston Andros Adamson 
14636f29b9bbSKinglong Mee 	if (pgio->pg_ops && pgio->pg_ops->pg_cleanup)
14646f29b9bbSKinglong Mee 		pgio->pg_ops->pg_cleanup(pgio);
14656f29b9bbSKinglong Mee 
146641d8d5b7SAnna Schumaker 	pgio->pg_ops = &nfs_pgio_rw_ops;
1467a7d42ddbSWeston Andros Adamson 
1468a7d42ddbSWeston Andros Adamson 	nfs_pageio_stop_mirroring(pgio);
1469a7d42ddbSWeston Andros Adamson 
1470a7d42ddbSWeston Andros Adamson 	mirror = &pgio->pg_mirrors[0];
1471a7d42ddbSWeston Andros Adamson 	mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1472dce81290STrond Myklebust }
14731f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1474dce81290STrond Myklebust 
14751da177e4SLinus Torvalds 
14760b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
14770b7c0153SFred Isaman {
14780b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
14790b7c0153SFred Isaman 
14800b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
14810b7c0153SFred Isaman }
14820b7c0153SFred Isaman 
14831f2edbe3STrond Myklebust /*
14841f2edbe3STrond Myklebust  * Special version of should_remove_suid() that ignores capabilities.
14851f2edbe3STrond Myklebust  */
14861f2edbe3STrond Myklebust static int nfs_should_remove_suid(const struct inode *inode)
14871f2edbe3STrond Myklebust {
14881f2edbe3STrond Myklebust 	umode_t mode = inode->i_mode;
14891f2edbe3STrond Myklebust 	int kill = 0;
1490788e7a89STrond Myklebust 
14911f2edbe3STrond Myklebust 	/* suid always must be killed */
14921f2edbe3STrond Myklebust 	if (unlikely(mode & S_ISUID))
14931f2edbe3STrond Myklebust 		kill = ATTR_KILL_SUID;
14941f2edbe3STrond Myklebust 
14951f2edbe3STrond Myklebust 	/*
14961f2edbe3STrond Myklebust 	 * sgid without any exec bits is just a mandatory locking mark; leave
14971f2edbe3STrond Myklebust 	 * it alone.  If some exec bits are set, it's a real sgid; kill it.
14981f2edbe3STrond Myklebust 	 */
14991f2edbe3STrond Myklebust 	if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
15001f2edbe3STrond Myklebust 		kill |= ATTR_KILL_SGID;
15011f2edbe3STrond Myklebust 
15021f2edbe3STrond Myklebust 	if (unlikely(kill && S_ISREG(mode)))
15031f2edbe3STrond Myklebust 		return kill;
15041f2edbe3STrond Myklebust 
15051f2edbe3STrond Myklebust 	return 0;
15061f2edbe3STrond Myklebust }
1507788e7a89STrond Myklebust 
1508a08a8cd3STrond Myklebust static void nfs_writeback_check_extend(struct nfs_pgio_header *hdr,
1509a08a8cd3STrond Myklebust 		struct nfs_fattr *fattr)
1510a08a8cd3STrond Myklebust {
1511a08a8cd3STrond Myklebust 	struct nfs_pgio_args *argp = &hdr->args;
1512a08a8cd3STrond Myklebust 	struct nfs_pgio_res *resp = &hdr->res;
15132b83d3deSTrond Myklebust 	u64 size = argp->offset + resp->count;
1514a08a8cd3STrond Myklebust 
1515a08a8cd3STrond Myklebust 	if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
15162b83d3deSTrond Myklebust 		fattr->size = size;
15172b83d3deSTrond Myklebust 	if (nfs_size_to_loff_t(fattr->size) < i_size_read(hdr->inode)) {
15182b83d3deSTrond Myklebust 		fattr->valid &= ~NFS_ATTR_FATTR_SIZE;
1519a08a8cd3STrond Myklebust 		return;
15202b83d3deSTrond Myklebust 	}
15212b83d3deSTrond Myklebust 	if (size != fattr->size)
1522a08a8cd3STrond Myklebust 		return;
1523a08a8cd3STrond Myklebust 	/* Set attribute barrier */
1524a08a8cd3STrond Myklebust 	nfs_fattr_set_barrier(fattr);
15252b83d3deSTrond Myklebust 	/* ...and update size */
15262b83d3deSTrond Myklebust 	fattr->valid |= NFS_ATTR_FATTR_SIZE;
1527a08a8cd3STrond Myklebust }
1528a08a8cd3STrond Myklebust 
1529a08a8cd3STrond Myklebust void nfs_writeback_update_inode(struct nfs_pgio_header *hdr)
1530a08a8cd3STrond Myklebust {
15312b83d3deSTrond Myklebust 	struct nfs_fattr *fattr = &hdr->fattr;
1532a08a8cd3STrond Myklebust 	struct inode *inode = hdr->inode;
1533a08a8cd3STrond Myklebust 
1534a08a8cd3STrond Myklebust 	spin_lock(&inode->i_lock);
1535a08a8cd3STrond Myklebust 	nfs_writeback_check_extend(hdr, fattr);
1536a08a8cd3STrond Myklebust 	nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
1537a08a8cd3STrond Myklebust 	spin_unlock(&inode->i_lock);
1538a08a8cd3STrond Myklebust }
1539a08a8cd3STrond Myklebust EXPORT_SYMBOL_GPL(nfs_writeback_update_inode);
1540a08a8cd3STrond Myklebust 
15411da177e4SLinus Torvalds /*
15421da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
15431da177e4SLinus Torvalds  */
1544d45f60c6SWeston Andros Adamson static int nfs_writeback_done(struct rpc_task *task,
1545d45f60c6SWeston Andros Adamson 			      struct nfs_pgio_header *hdr,
15460eecb214SAnna Schumaker 			      struct inode *inode)
15471da177e4SLinus Torvalds {
1548788e7a89STrond Myklebust 	int status;
15491da177e4SLinus Torvalds 
1550f551e44fSChuck Lever 	/*
1551f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1552f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1553f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1554f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1555f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1556f551e44fSChuck Lever 	 */
1557d45f60c6SWeston Andros Adamson 	status = NFS_PROTO(inode)->write_done(task, hdr);
1558788e7a89STrond Myklebust 	if (status != 0)
15590eecb214SAnna Schumaker 		return status;
1560d45f60c6SWeston Andros Adamson 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count);
156191d5b470SChuck Lever 
1562d45f60c6SWeston Andros Adamson 	if (hdr->res.verf->committed < hdr->args.stable &&
1563d45f60c6SWeston Andros Adamson 	    task->tk_status >= 0) {
15641da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
15651da177e4SLinus Torvalds 		 * commit data to stable storage even though we
15661da177e4SLinus Torvalds 		 * requested it.
15671da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
15681da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
15691da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
15701da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
15711da177e4SLinus Torvalds 		 */
15721da177e4SLinus Torvalds 		static unsigned long    complain;
15731da177e4SLinus Torvalds 
1574a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
15751da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
15761da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
15771da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1578cd841605SFred Isaman 				NFS_SERVER(inode)->nfs_client->cl_hostname,
1579d45f60c6SWeston Andros Adamson 				hdr->res.verf->committed, hdr->args.stable);
15801da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
15811da177e4SLinus Torvalds 		}
15821da177e4SLinus Torvalds 	}
15831f2edbe3STrond Myklebust 
15841f2edbe3STrond Myklebust 	/* Deal with the suid/sgid bit corner case */
15851f2edbe3STrond Myklebust 	if (nfs_should_remove_suid(inode))
15861f2edbe3STrond Myklebust 		nfs_mark_for_revalidate(inode);
15870eecb214SAnna Schumaker 	return 0;
15880eecb214SAnna Schumaker }
15890eecb214SAnna Schumaker 
15900eecb214SAnna Schumaker /*
15910eecb214SAnna Schumaker  * This function is called when the WRITE call is complete.
15920eecb214SAnna Schumaker  */
1593d45f60c6SWeston Andros Adamson static void nfs_writeback_result(struct rpc_task *task,
1594d45f60c6SWeston Andros Adamson 				 struct nfs_pgio_header *hdr)
15950eecb214SAnna Schumaker {
1596d45f60c6SWeston Andros Adamson 	struct nfs_pgio_args	*argp = &hdr->args;
1597d45f60c6SWeston Andros Adamson 	struct nfs_pgio_res	*resp = &hdr->res;
15981f2edbe3STrond Myklebust 
15991f2edbe3STrond Myklebust 	if (resp->count < argp->count) {
16001da177e4SLinus Torvalds 		static unsigned long    complain;
16011da177e4SLinus Torvalds 
16026c75dc0dSFred Isaman 		/* This a short write! */
1603d45f60c6SWeston Andros Adamson 		nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE);
160491d5b470SChuck Lever 
16051da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
16066c75dc0dSFred Isaman 		if (resp->count == 0) {
16076c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
16086c75dc0dSFred Isaman 				printk(KERN_WARNING
16096c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
16106c75dc0dSFred Isaman 				       argp->count);
16116c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
16126c75dc0dSFred Isaman 			}
1613d45f60c6SWeston Andros Adamson 			nfs_set_pgio_error(hdr, -EIO, argp->offset);
16146c75dc0dSFred Isaman 			task->tk_status = -EIO;
16156c75dc0dSFred Isaman 			return;
16166c75dc0dSFred Isaman 		}
1617f8417b48SKinglong Mee 
1618f8417b48SKinglong Mee 		/* For non rpc-based layout drivers, retry-through-MDS */
1619f8417b48SKinglong Mee 		if (!task->tk_ops) {
1620f8417b48SKinglong Mee 			hdr->pnfs_error = -EAGAIN;
1621f8417b48SKinglong Mee 			return;
1622f8417b48SKinglong Mee 		}
1623f8417b48SKinglong Mee 
16241da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
16251da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
16261da177e4SLinus Torvalds 			/* Resend from where the server left off */
1627d45f60c6SWeston Andros Adamson 			hdr->mds_offset += resp->count;
16281da177e4SLinus Torvalds 			argp->offset += resp->count;
16291da177e4SLinus Torvalds 			argp->pgbase += resp->count;
16301da177e4SLinus Torvalds 			argp->count -= resp->count;
16311da177e4SLinus Torvalds 		} else {
16321da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
16331da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
16341da177e4SLinus Torvalds 			 */
16351da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
16361da177e4SLinus Torvalds 		}
1637d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
16381da177e4SLinus Torvalds 	}
16391da177e4SLinus Torvalds }
16401da177e4SLinus Torvalds 
1641af7cf057STrond Myklebust static int wait_on_commit(struct nfs_mds_commit_info *cinfo)
164271d0a611STrond Myklebust {
1643af7cf057STrond Myklebust 	return wait_on_atomic_t(&cinfo->rpcs_out,
1644af7cf057STrond Myklebust 			nfs_wait_atomic_killable, TASK_KILLABLE);
1645af7cf057STrond Myklebust }
1646af7cf057STrond Myklebust 
1647af7cf057STrond Myklebust static void nfs_commit_begin(struct nfs_mds_commit_info *cinfo)
1648af7cf057STrond Myklebust {
1649af7cf057STrond Myklebust 	atomic_inc(&cinfo->rpcs_out);
1650af7cf057STrond Myklebust }
1651af7cf057STrond Myklebust 
1652af7cf057STrond Myklebust static void nfs_commit_end(struct nfs_mds_commit_info *cinfo)
1653af7cf057STrond Myklebust {
1654af7cf057STrond Myklebust 	if (atomic_dec_and_test(&cinfo->rpcs_out))
1655af7cf057STrond Myklebust 		wake_up_atomic_t(&cinfo->rpcs_out);
165671d0a611STrond Myklebust }
165771d0a611STrond Myklebust 
16580b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
16591da177e4SLinus Torvalds {
16600b7c0153SFred Isaman 	put_nfs_open_context(data->context);
16610b7c0153SFred Isaman 	nfs_commit_free(data);
16621da177e4SLinus Torvalds }
1663e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
16641da177e4SLinus Torvalds 
16650b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
1666c36aae9aSPeng Tao 			const struct nfs_rpc_ops *nfs_ops,
16679ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
16689f0ec176SAndy Adamson 			int how, int flags)
16691da177e4SLinus Torvalds {
167007737691STrond Myklebust 	struct rpc_task *task;
16719ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1672bdc7f021STrond Myklebust 	struct rpc_message msg = {
1673bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1674bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
16759ace33cdSFred Isaman 		.rpc_cred = data->cred,
1676bdc7f021STrond Myklebust 	};
167784115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
167807737691STrond Myklebust 		.task = &data->task,
16799ace33cdSFred Isaman 		.rpc_client = clnt,
1680bdc7f021STrond Myklebust 		.rpc_message = &msg,
16819ace33cdSFred Isaman 		.callback_ops = call_ops,
168284115e1cSTrond Myklebust 		.callback_data = data,
1683101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
16849f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
16853ff7576dSTrond Myklebust 		.priority = priority,
168684115e1cSTrond Myklebust 	};
1687788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
1688c36aae9aSPeng Tao 	nfs_ops->commit_setup(data, &msg);
16891da177e4SLinus Torvalds 
1690b4839ebeSKinglong Mee 	dprintk("NFS: initiated commit call\n");
1691bdc7f021STrond Myklebust 
16928c21c62cSWeston Andros Adamson 	nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client,
16938c21c62cSWeston Andros Adamson 		NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg);
16948c21c62cSWeston Andros Adamson 
169507737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1696dbae4c73STrond Myklebust 	if (IS_ERR(task))
1697dbae4c73STrond Myklebust 		return PTR_ERR(task);
1698d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1699d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
170007737691STrond Myklebust 	rpc_put_task(task);
1701dbae4c73STrond Myklebust 	return 0;
17021da177e4SLinus Torvalds }
1703e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
17041da177e4SLinus Torvalds 
1705378520b8SPeng Tao static loff_t nfs_get_lwb(struct list_head *head)
1706378520b8SPeng Tao {
1707378520b8SPeng Tao 	loff_t lwb = 0;
1708378520b8SPeng Tao 	struct nfs_page *req;
1709378520b8SPeng Tao 
1710378520b8SPeng Tao 	list_for_each_entry(req, head, wb_list)
1711378520b8SPeng Tao 		if (lwb < (req_offset(req) + req->wb_bytes))
1712378520b8SPeng Tao 			lwb = req_offset(req) + req->wb_bytes;
1713378520b8SPeng Tao 
1714378520b8SPeng Tao 	return lwb;
1715378520b8SPeng Tao }
1716378520b8SPeng Tao 
17171da177e4SLinus Torvalds /*
17189ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
17199ace33cdSFred Isaman  */
17200b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1721988b6dceSFred Isaman 		     struct list_head *head,
1722f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1723f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
17249ace33cdSFred Isaman {
17259ace33cdSFred Isaman 	struct nfs_page *first = nfs_list_entry(head->next);
17262b0143b5SDavid Howells 	struct inode *inode = d_inode(first->wb_context->dentry);
17279ace33cdSFred Isaman 
17289ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
17299ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
17309ace33cdSFred Isaman 
17319ace33cdSFred Isaman 	list_splice_init(head, &data->pages);
17329ace33cdSFred Isaman 
17339ace33cdSFred Isaman 	data->inode	  = inode;
17349ace33cdSFred Isaman 	data->cred	  = first->wb_context->cred;
1735988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
1736378520b8SPeng Tao 	/* only set lwb for pnfs commit */
1737378520b8SPeng Tao 	if (lseg)
1738378520b8SPeng Tao 		data->lwb = nfs_get_lwb(&data->pages);
17399ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1740f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1741b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
17429ace33cdSFred Isaman 
17439ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
17449ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
17459ace33cdSFred Isaman 	data->args.offset = 0;
17469ace33cdSFred Isaman 	data->args.count  = 0;
17470b7c0153SFred Isaman 	data->context     = get_nfs_open_context(first->wb_context);
17489ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
17499ace33cdSFred Isaman 	data->res.verf    = &data->verf;
17509ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
17519ace33cdSFred Isaman }
1752e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
17539ace33cdSFred Isaman 
1754e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1755ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1756b57ff130SWeston Andros Adamson 		      struct nfs_commit_info *cinfo,
1757b57ff130SWeston Andros Adamson 		      u32 ds_commit_idx)
175864bfeb49SFred Isaman {
175964bfeb49SFred Isaman 	struct nfs_page *req;
176064bfeb49SFred Isaman 
176164bfeb49SFred Isaman 	while (!list_empty(page_list)) {
176264bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
176364bfeb49SFred Isaman 		nfs_list_remove_request(req);
1764b57ff130SWeston Andros Adamson 		nfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx);
1765487b9b8aSTom Haynes 		if (!cinfo->dreq)
1766487b9b8aSTom Haynes 			nfs_clear_page_commit(req->wb_page);
17671d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
176864bfeb49SFred Isaman 	}
176964bfeb49SFred Isaman }
1770e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
177164bfeb49SFred Isaman 
1772b20135d0STrond Myklebust static void
1773b20135d0STrond Myklebust nfs_commit_resched_write(struct nfs_commit_info *cinfo,
1774b20135d0STrond Myklebust 		struct nfs_page *req)
1775b20135d0STrond Myklebust {
1776b20135d0STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
1777b20135d0STrond Myklebust }
1778b20135d0STrond Myklebust 
17799ace33cdSFred Isaman /*
17801da177e4SLinus Torvalds  * Commit dirty pages
17811da177e4SLinus Torvalds  */
17821da177e4SLinus Torvalds static int
1783ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1784ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
17851da177e4SLinus Torvalds {
17860b7c0153SFred Isaman 	struct nfs_commit_data	*data;
17871da177e4SLinus Torvalds 
1788ade8febdSWeston Andros Adamson 	/* another commit raced with us */
1789ade8febdSWeston Andros Adamson 	if (list_empty(head))
1790ade8febdSWeston Andros Adamson 		return 0;
1791ade8febdSWeston Andros Adamson 
1792518662e0SNeilBrown 	data = nfs_commitdata_alloc(true);
17931da177e4SLinus Torvalds 
17941da177e4SLinus Torvalds 	/* Set up the argument struct */
1795f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
1796f453a54aSFred Isaman 	atomic_inc(&cinfo->mds->rpcs_out);
1797c36aae9aSPeng Tao 	return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode),
1798c36aae9aSPeng Tao 				   data->mds_ops, how, 0);
17991da177e4SLinus Torvalds }
18001da177e4SLinus Torvalds 
18011da177e4SLinus Torvalds /*
18021da177e4SLinus Torvalds  * COMMIT call returned
18031da177e4SLinus Torvalds  */
1804788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
18051da177e4SLinus Torvalds {
18060b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
18071da177e4SLinus Torvalds 
1808a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
18091da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
18101da177e4SLinus Torvalds 
1811788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1812c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
1813c9d8f89dSTrond Myklebust }
1814c9d8f89dSTrond Myklebust 
1815f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1816c9d8f89dSTrond Myklebust {
1817c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1818c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1819f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
1820353db796SNeilBrown 	struct nfs_server *nfss;
1821788e7a89STrond Myklebust 
18221da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
18231da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
18241da177e4SLinus Torvalds 		nfs_list_remove_request(req);
182567911c8fSAnna Schumaker 		if (req->wb_page)
1826d6d6dc7cSFred Isaman 			nfs_clear_page_commit(req->wb_page);
18271da177e4SLinus Torvalds 
18281e8968c5SNiels de Vos 		dprintk("NFS:       commit (%s/%llu %d@%lld)",
18293d4ff43dSAl Viro 			req->wb_context->dentry->d_sb->s_id,
18302b0143b5SDavid Howells 			(unsigned long long)NFS_FILEID(d_inode(req->wb_context->dentry)),
18311da177e4SLinus Torvalds 			req->wb_bytes,
18321da177e4SLinus Torvalds 			(long long)req_offset(req));
1833c9d8f89dSTrond Myklebust 		if (status < 0) {
1834c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
183538a33101SKinglong Mee 			if (req->wb_page)
18361da177e4SLinus Torvalds 				nfs_inode_remove_request(req);
1837ddeaa637SJoe Perches 			dprintk_cont(", error = %d\n", status);
18381da177e4SLinus Torvalds 			goto next;
18391da177e4SLinus Torvalds 		}
18401da177e4SLinus Torvalds 
18411da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
18421da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
18438fc3c386STrond Myklebust 		if (!nfs_write_verifier_cmp(&req->wb_verf, &data->verf.verifier)) {
18441da177e4SLinus Torvalds 			/* We have a match */
184538a33101SKinglong Mee 			if (req->wb_page)
18461da177e4SLinus Torvalds 				nfs_inode_remove_request(req);
1847ddeaa637SJoe Perches 			dprintk_cont(" OK\n");
18481da177e4SLinus Torvalds 			goto next;
18491da177e4SLinus Torvalds 		}
18501da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
1851ddeaa637SJoe Perches 		dprintk_cont(" mismatch\n");
18526d884e8fSFred 		nfs_mark_request_dirty(req);
185305990d1bSTrond Myklebust 		set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
18541da177e4SLinus Torvalds 	next:
18551d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
18561da177e4SLinus Torvalds 	}
1857353db796SNeilBrown 	nfss = NFS_SERVER(data->inode);
1858353db796SNeilBrown 	if (atomic_long_read(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
18590db10944SJan Kara 		clear_bdi_congested(inode_to_bdi(data->inode), BLK_RW_ASYNC);
1860353db796SNeilBrown 
1861f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1862af7cf057STrond Myklebust 	nfs_commit_end(cinfo.mds);
18635917ce84SFred Isaman }
18645917ce84SFred Isaman 
18655917ce84SFred Isaman static void nfs_commit_release(void *calldata)
18665917ce84SFred Isaman {
18670b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
18685917ce84SFred Isaman 
1869f453a54aSFred Isaman 	data->completion_ops->completion(data);
1870c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
18711da177e4SLinus Torvalds }
1872788e7a89STrond Myklebust 
1873788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
18740b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1875788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1876788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1877788e7a89STrond Myklebust };
18781da177e4SLinus Torvalds 
1879f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1880f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1881b20135d0STrond Myklebust 	.resched_write = nfs_commit_resched_write,
1882f453a54aSFred Isaman };
1883f453a54aSFred Isaman 
18841763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1885ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
188684c53ab5SFred Isaman {
188784c53ab5SFred Isaman 	int status;
188884c53ab5SFred Isaman 
1889ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
189084c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1891ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
189284c53ab5SFred Isaman 	return status;
189384c53ab5SFred Isaman }
189484c53ab5SFred Isaman 
1895b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
18961da177e4SLinus Torvalds {
18971da177e4SLinus Torvalds 	LIST_HEAD(head);
1898ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
189971d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1900af7cf057STrond Myklebust 	int error = 0;
1901b8413f98STrond Myklebust 	int res;
19021da177e4SLinus Torvalds 
1903ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1904af7cf057STrond Myklebust 	nfs_commit_begin(cinfo.mds);
1905ea2cf228SFred Isaman 	res = nfs_scan_commit(inode, &head, &cinfo);
1906af7cf057STrond Myklebust 	if (res)
1907ea2cf228SFred Isaman 		error = nfs_generic_commit_list(inode, &head, how, &cinfo);
1908af7cf057STrond Myklebust 	nfs_commit_end(cinfo.mds);
19091da177e4SLinus Torvalds 	if (error < 0)
1910af7cf057STrond Myklebust 		goto out_error;
1911b8413f98STrond Myklebust 	if (!may_wait)
1912b8413f98STrond Myklebust 		goto out_mark_dirty;
1913af7cf057STrond Myklebust 	error = wait_on_commit(cinfo.mds);
1914b8413f98STrond Myklebust 	if (error < 0)
1915b8413f98STrond Myklebust 		return error;
1916c5efa5fcSTrond Myklebust 	return res;
1917af7cf057STrond Myklebust out_error:
1918af7cf057STrond Myklebust 	res = error;
1919c5efa5fcSTrond Myklebust 	/* Note: If we exit without ensuring that the commit is complete,
1920c5efa5fcSTrond Myklebust 	 * we must mark the inode as dirty. Otherwise, future calls to
1921c5efa5fcSTrond Myklebust 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1922c5efa5fcSTrond Myklebust 	 * that the data is on the disk.
1923c5efa5fcSTrond Myklebust 	 */
1924c5efa5fcSTrond Myklebust out_mark_dirty:
1925c5efa5fcSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
19261da177e4SLinus Torvalds 	return res;
19271da177e4SLinus Torvalds }
1928b20135d0STrond Myklebust EXPORT_SYMBOL_GPL(nfs_commit_inode);
19298fc795f7STrond Myklebust 
1930ae09c31fSAnna Schumaker int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
19318fc795f7STrond Myklebust {
1932420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1933420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1934420e3646STrond Myklebust 	int ret = 0;
19358fc795f7STrond Myklebust 
19363236c3e1SJeff Layton 	/* no commits means nothing needs to be done */
1937ea2cf228SFred Isaman 	if (!nfsi->commit_info.ncommit)
19383236c3e1SJeff Layton 		return ret;
19393236c3e1SJeff Layton 
1940a00dd6c0SJeff Layton 	if (wbc->sync_mode == WB_SYNC_NONE) {
1941a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1942a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1943420e3646STrond Myklebust 		 */
1944cb1410c7SWeston Andros Adamson 		if (nfsi->commit_info.ncommit <= (nfsi->nrequests >> 1))
1945420e3646STrond Myklebust 			goto out_mark_dirty;
1946420e3646STrond Myklebust 
1947a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1948420e3646STrond Myklebust 		flags = 0;
1949a00dd6c0SJeff Layton 	}
1950a00dd6c0SJeff Layton 
1951420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1952420e3646STrond Myklebust 	if (ret >= 0) {
1953420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1954420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1955420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1956420e3646STrond Myklebust 			else
1957420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1958420e3646STrond Myklebust 		}
19598fc795f7STrond Myklebust 		return 0;
1960420e3646STrond Myklebust 	}
1961420e3646STrond Myklebust out_mark_dirty:
19628fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
19638fc795f7STrond Myklebust 	return ret;
19648fc795f7STrond Myklebust }
196589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode);
1966863a3c6cSAndy Adamson 
1967acdc53b2STrond Myklebust /*
1968837bb1d7STrond Myklebust  * Wrapper for filemap_write_and_wait_range()
1969837bb1d7STrond Myklebust  *
1970837bb1d7STrond Myklebust  * Needed for pNFS in order to ensure data becomes visible to the
1971837bb1d7STrond Myklebust  * client.
1972837bb1d7STrond Myklebust  */
1973837bb1d7STrond Myklebust int nfs_filemap_write_and_wait_range(struct address_space *mapping,
1974837bb1d7STrond Myklebust 		loff_t lstart, loff_t lend)
1975837bb1d7STrond Myklebust {
1976837bb1d7STrond Myklebust 	int ret;
1977837bb1d7STrond Myklebust 
1978837bb1d7STrond Myklebust 	ret = filemap_write_and_wait_range(mapping, lstart, lend);
1979837bb1d7STrond Myklebust 	if (ret == 0)
1980837bb1d7STrond Myklebust 		ret = pnfs_sync_inode(mapping->host, true);
1981837bb1d7STrond Myklebust 	return ret;
1982837bb1d7STrond Myklebust }
1983837bb1d7STrond Myklebust EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range);
1984837bb1d7STrond Myklebust 
1985837bb1d7STrond Myklebust /*
1986acdc53b2STrond Myklebust  * flush the inode to disk.
1987acdc53b2STrond Myklebust  */
1988acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
198934901f70STrond Myklebust {
1990f4ce1299STrond Myklebust 	int ret;
199134901f70STrond Myklebust 
1992f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_enter(inode);
1993f4ce1299STrond Myklebust 
19945bb89b47STrond Myklebust 	ret = filemap_write_and_wait(inode->i_mapping);
19956b196875SChuck Lever 	if (ret)
19966b196875SChuck Lever 		goto out;
19975bb89b47STrond Myklebust 	ret = nfs_commit_inode(inode, FLUSH_SYNC);
19986b196875SChuck Lever 	if (ret < 0)
19996b196875SChuck Lever 		goto out;
20005bb89b47STrond Myklebust 	pnfs_sync_inode(inode, true);
20016b196875SChuck Lever 	ret = 0;
2002f4ce1299STrond Myklebust 
20036b196875SChuck Lever out:
2004f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_exit(inode, ret);
2005f4ce1299STrond Myklebust 	return ret;
20061c75950bSTrond Myklebust }
2007ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all);
20081c75950bSTrond Myklebust 
20091b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
20101b3b4a1aSTrond Myklebust {
20111b3b4a1aSTrond Myklebust 	struct nfs_page *req;
20121b3b4a1aSTrond Myklebust 	int ret = 0;
20131b3b4a1aSTrond Myklebust 
2014ba8b06e6STrond Myklebust 	wait_on_page_writeback(page);
20153e217045SWeston Andros Adamson 
20163e217045SWeston Andros Adamson 	/* blocking call to cancel all requests and join to a single (head)
20173e217045SWeston Andros Adamson 	 * request */
20183e217045SWeston Andros Adamson 	req = nfs_lock_and_join_requests(page, false);
20193e217045SWeston Andros Adamson 
20203e217045SWeston Andros Adamson 	if (IS_ERR(req)) {
20213e217045SWeston Andros Adamson 		ret = PTR_ERR(req);
20223e217045SWeston Andros Adamson 	} else if (req) {
20233e217045SWeston Andros Adamson 		/* all requests from this page have been cancelled by
20243e217045SWeston Andros Adamson 		 * nfs_lock_and_join_requests, so just remove the head
20253e217045SWeston Andros Adamson 		 * request from the inode / page_private pointer and
20263e217045SWeston Andros Adamson 		 * release it */
20271b3b4a1aSTrond Myklebust 		nfs_inode_remove_request(req);
20281d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
20291b3b4a1aSTrond Myklebust 	}
20303e217045SWeston Andros Adamson 
20311b3b4a1aSTrond Myklebust 	return ret;
20321b3b4a1aSTrond Myklebust }
20331b3b4a1aSTrond Myklebust 
20341c75950bSTrond Myklebust /*
20351c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
20361c75950bSTrond Myklebust  */
2037c373fff7STrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
20381c75950bSTrond Myklebust {
203929418aa4SMel Gorman 	loff_t range_start = page_file_offset(page);
204009cbfeafSKirill A. Shutemov 	loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1);
20417f2f12d9STrond Myklebust 	struct writeback_control wbc = {
20427f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
20437f2f12d9STrond Myklebust 		.nr_to_write = 0,
20447f2f12d9STrond Myklebust 		.range_start = range_start,
20457f2f12d9STrond Myklebust 		.range_end = range_end,
20467f2f12d9STrond Myklebust 	};
20477f2f12d9STrond Myklebust 	int ret;
20487f2f12d9STrond Myklebust 
2049f4ce1299STrond Myklebust 	trace_nfs_writeback_page_enter(inode);
2050f4ce1299STrond Myklebust 
20510522f6adSTrond Myklebust 	for (;;) {
2052ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
20537f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
2054c373fff7STrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
20557f2f12d9STrond Myklebust 			if (ret < 0)
20567f2f12d9STrond Myklebust 				goto out_error;
20570522f6adSTrond Myklebust 			continue;
20587f2f12d9STrond Myklebust 		}
2059f4ce1299STrond Myklebust 		ret = 0;
20600522f6adSTrond Myklebust 		if (!PagePrivate(page))
20610522f6adSTrond Myklebust 			break;
20620522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
20637f2f12d9STrond Myklebust 		if (ret < 0)
20647f2f12d9STrond Myklebust 			goto out_error;
20657f2f12d9STrond Myklebust 	}
20667f2f12d9STrond Myklebust out_error:
2067f4ce1299STrond Myklebust 	trace_nfs_writeback_page_exit(inode, ret);
20687f2f12d9STrond Myklebust 	return ret;
20691c75950bSTrond Myklebust }
20701c75950bSTrond Myklebust 
2071074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
2072074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
2073a6bc32b8SMel Gorman 		struct page *page, enum migrate_mode mode)
2074074cc1deSTrond Myklebust {
20752da95652SJeff Layton 	/*
20762da95652SJeff Layton 	 * If PagePrivate is set, then the page is currently associated with
20772da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
20782da95652SJeff Layton 	 *
20792da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
20802da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
20812da95652SJeff Layton 	 *        the page lock.
20822da95652SJeff Layton 	 */
20832da95652SJeff Layton 	if (PagePrivate(page))
20842da95652SJeff Layton 		return -EBUSY;
2085074cc1deSTrond Myklebust 
20868c209ce7SDavid Howells 	if (!nfs_fscache_release_page(page, GFP_KERNEL))
20878c209ce7SDavid Howells 		return -EBUSY;
2088074cc1deSTrond Myklebust 
2089a6bc32b8SMel Gorman 	return migrate_page(mapping, newpage, page, mode);
2090074cc1deSTrond Myklebust }
2091074cc1deSTrond Myklebust #endif
2092074cc1deSTrond Myklebust 
2093f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
20941da177e4SLinus Torvalds {
20951da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
20961e7f3a48SWeston Andros Adamson 					     sizeof(struct nfs_pgio_header),
20971da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
209820c2df83SPaul Mundt 					     NULL);
20991da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
21001da177e4SLinus Torvalds 		return -ENOMEM;
21011da177e4SLinus Torvalds 
210293d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
21031da177e4SLinus Torvalds 						     nfs_wdata_cachep);
21041da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
21053dd4765fSJeff Layton 		goto out_destroy_write_cache;
21061da177e4SLinus Torvalds 
21070b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
21080b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
21090b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
21100b7c0153SFred Isaman 					     NULL);
21110b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
21123dd4765fSJeff Layton 		goto out_destroy_write_mempool;
21130b7c0153SFred Isaman 
211493d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
21154c100210SYanchuan Nian 						      nfs_cdata_cachep);
21161da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
21173dd4765fSJeff Layton 		goto out_destroy_commit_cache;
21181da177e4SLinus Torvalds 
211989a09141SPeter Zijlstra 	/*
212089a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
212189a09141SPeter Zijlstra 	 *
212289a09141SPeter Zijlstra 	 *  64MB:    8192k
212389a09141SPeter Zijlstra 	 * 128MB:   11585k
212489a09141SPeter Zijlstra 	 * 256MB:   16384k
212589a09141SPeter Zijlstra 	 * 512MB:   23170k
212689a09141SPeter Zijlstra 	 *   1GB:   32768k
212789a09141SPeter Zijlstra 	 *   2GB:   46340k
212889a09141SPeter Zijlstra 	 *   4GB:   65536k
212989a09141SPeter Zijlstra 	 *   8GB:   92681k
213089a09141SPeter Zijlstra 	 *  16GB:  131072k
213189a09141SPeter Zijlstra 	 *
213289a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
213389a09141SPeter Zijlstra 	 * Limit the default to 256M
213489a09141SPeter Zijlstra 	 */
213589a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
213689a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
213789a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
213889a09141SPeter Zijlstra 
21391da177e4SLinus Torvalds 	return 0;
21403dd4765fSJeff Layton 
21413dd4765fSJeff Layton out_destroy_commit_cache:
21423dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
21433dd4765fSJeff Layton out_destroy_write_mempool:
21443dd4765fSJeff Layton 	mempool_destroy(nfs_wdata_mempool);
21453dd4765fSJeff Layton out_destroy_write_cache:
21463dd4765fSJeff Layton 	kmem_cache_destroy(nfs_wdata_cachep);
21473dd4765fSJeff Layton 	return -ENOMEM;
21481da177e4SLinus Torvalds }
21491da177e4SLinus Torvalds 
2150266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
21511da177e4SLinus Torvalds {
21521da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
21533dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
21541da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
21551a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
21561da177e4SLinus Torvalds }
21571da177e4SLinus Torvalds 
21584a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops = {
21594a0de55cSAnna Schumaker 	.rw_alloc_header	= nfs_writehdr_alloc,
21604a0de55cSAnna Schumaker 	.rw_free_header		= nfs_writehdr_free,
21610eecb214SAnna Schumaker 	.rw_done		= nfs_writeback_done,
21620eecb214SAnna Schumaker 	.rw_result		= nfs_writeback_result,
21631ed26f33SAnna Schumaker 	.rw_initiate		= nfs_initiate_write,
21644a0de55cSAnna Schumaker };
2165