xref: /linux/fs/nfs/write.c (revision 74a6d4b5ae4ec7e93c72a92decb2f8c16c812416)
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 
19382749dd4STrond Myklebust 	if (PagePrivate(page) || PageSwapCache(page)) {
194587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
19584d3a9a9SWeston Andros Adamson 		req = nfs_page_find_head_request_locked(NFS_I(inode), page);
196587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
19782749dd4STrond Myklebust 	}
198277459d2STrond Myklebust 	return req;
199277459d2STrond Myklebust }
200277459d2STrond Myklebust 
2011da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
2021da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
2031da177e4SLinus Torvalds {
204d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
205a3d01454STrond Myklebust 	loff_t end, i_size;
206a3d01454STrond Myklebust 	pgoff_t end_index;
2071da177e4SLinus Torvalds 
208a3d01454STrond Myklebust 	spin_lock(&inode->i_lock);
209a3d01454STrond Myklebust 	i_size = i_size_read(inode);
21009cbfeafSKirill A. Shutemov 	end_index = (i_size - 1) >> PAGE_SHIFT;
2118cd79788SHuang Ying 	if (i_size > 0 && page_index(page) < end_index)
212a3d01454STrond Myklebust 		goto out;
213d56b4ddfSMel Gorman 	end = page_file_offset(page) + ((loff_t)offset+count);
2141da177e4SLinus Torvalds 	if (i_size >= end)
215a3d01454STrond Myklebust 		goto out;
2161da177e4SLinus Torvalds 	i_size_write(inode, end);
217a3d01454STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
218a3d01454STrond Myklebust out:
219a3d01454STrond Myklebust 	spin_unlock(&inode->i_lock);
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds 
222a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
223a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
224a301b777STrond Myklebust {
225d56b4ddfSMel Gorman 	nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page));
226a301b777STrond Myklebust }
227a301b777STrond Myklebust 
228d72ddcbaSWeston Andros Adamson /*
229d72ddcbaSWeston Andros Adamson  * nfs_page_group_search_locked
230d72ddcbaSWeston Andros Adamson  * @head - head request of page group
231d72ddcbaSWeston Andros Adamson  * @page_offset - offset into page
232d72ddcbaSWeston Andros Adamson  *
233d72ddcbaSWeston Andros Adamson  * Search page group with head @head to find a request that contains the
234d72ddcbaSWeston Andros Adamson  * page offset @page_offset.
235d72ddcbaSWeston Andros Adamson  *
236d72ddcbaSWeston Andros Adamson  * Returns a pointer to the first matching nfs request, or NULL if no
237d72ddcbaSWeston Andros Adamson  * match is found.
238d72ddcbaSWeston Andros Adamson  *
239d72ddcbaSWeston Andros Adamson  * Must be called with the page group lock held
240d72ddcbaSWeston Andros Adamson  */
241d72ddcbaSWeston Andros Adamson static struct nfs_page *
242d72ddcbaSWeston Andros Adamson nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset)
243d72ddcbaSWeston Andros Adamson {
244d72ddcbaSWeston Andros Adamson 	struct nfs_page *req;
245d72ddcbaSWeston Andros Adamson 
246d72ddcbaSWeston Andros Adamson 	WARN_ON_ONCE(head != head->wb_head);
247d72ddcbaSWeston Andros Adamson 	WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_head->wb_flags));
248d72ddcbaSWeston Andros Adamson 
249d72ddcbaSWeston Andros Adamson 	req = head;
250d72ddcbaSWeston Andros Adamson 	do {
251d72ddcbaSWeston Andros Adamson 		if (page_offset >= req->wb_pgbase &&
252d72ddcbaSWeston Andros Adamson 		    page_offset < (req->wb_pgbase + req->wb_bytes))
253d72ddcbaSWeston Andros Adamson 			return req;
254d72ddcbaSWeston Andros Adamson 
255d72ddcbaSWeston Andros Adamson 		req = req->wb_this_page;
256d72ddcbaSWeston Andros Adamson 	} while (req != head);
257d72ddcbaSWeston Andros Adamson 
258d72ddcbaSWeston Andros Adamson 	return NULL;
259d72ddcbaSWeston Andros Adamson }
260d72ddcbaSWeston Andros Adamson 
261d72ddcbaSWeston Andros Adamson /*
262d72ddcbaSWeston Andros Adamson  * nfs_page_group_covers_page
263d72ddcbaSWeston Andros Adamson  * @head - head request of page group
264d72ddcbaSWeston Andros Adamson  *
265d72ddcbaSWeston Andros Adamson  * Return true if the page group with head @head covers the whole page,
266d72ddcbaSWeston Andros Adamson  * returns false otherwise
267d72ddcbaSWeston Andros Adamson  */
268d72ddcbaSWeston Andros Adamson static bool nfs_page_group_covers_page(struct nfs_page *req)
269d72ddcbaSWeston Andros Adamson {
270d72ddcbaSWeston Andros Adamson 	struct nfs_page *tmp;
271d72ddcbaSWeston Andros Adamson 	unsigned int pos = 0;
272d72ddcbaSWeston Andros Adamson 	unsigned int len = nfs_page_length(req->wb_page);
273d72ddcbaSWeston Andros Adamson 
274fd2f3a06SWeston Andros Adamson 	nfs_page_group_lock(req, false);
275d72ddcbaSWeston Andros Adamson 
276d72ddcbaSWeston Andros Adamson 	do {
277d72ddcbaSWeston Andros Adamson 		tmp = nfs_page_group_search_locked(req->wb_head, pos);
278d72ddcbaSWeston Andros Adamson 		if (tmp) {
279d72ddcbaSWeston Andros Adamson 			/* no way this should happen */
280d72ddcbaSWeston Andros Adamson 			WARN_ON_ONCE(tmp->wb_pgbase != pos);
281d72ddcbaSWeston Andros Adamson 			pos += tmp->wb_bytes - (pos - tmp->wb_pgbase);
282d72ddcbaSWeston Andros Adamson 		}
283d72ddcbaSWeston Andros Adamson 	} while (tmp && pos < len);
284d72ddcbaSWeston Andros Adamson 
285d72ddcbaSWeston Andros Adamson 	nfs_page_group_unlock(req);
286d72ddcbaSWeston Andros Adamson 	WARN_ON_ONCE(pos > len);
287d72ddcbaSWeston Andros Adamson 	return pos == len;
288d72ddcbaSWeston Andros Adamson }
289d72ddcbaSWeston Andros Adamson 
2901da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
2911da177e4SLinus Torvalds  * covers the full page.
2921da177e4SLinus Torvalds  */
293d72ddcbaSWeston Andros Adamson static void nfs_mark_uptodate(struct nfs_page *req)
2941da177e4SLinus Torvalds {
295d72ddcbaSWeston Andros Adamson 	if (PageUptodate(req->wb_page))
2961da177e4SLinus Torvalds 		return;
297d72ddcbaSWeston Andros Adamson 	if (!nfs_page_group_covers_page(req))
2981da177e4SLinus Torvalds 		return;
299d72ddcbaSWeston Andros Adamson 	SetPageUptodate(req->wb_page);
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
3031da177e4SLinus Torvalds {
304e87b4c7aSNeilBrown 	int ret = 0;
305cca588d6STrond Myklebust 
306e87b4c7aSNeilBrown 	if (wbc->sync_mode == WB_SYNC_ALL)
307e87b4c7aSNeilBrown 		ret = FLUSH_COND_STABLE;
308e87b4c7aSNeilBrown 	return ret;
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds /*
31289a09141SPeter Zijlstra  * NFS congestion control
31389a09141SPeter Zijlstra  */
31489a09141SPeter Zijlstra 
31589a09141SPeter Zijlstra int nfs_congestion_kb;
31689a09141SPeter Zijlstra 
31789a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
31889a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
31989a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
32089a09141SPeter Zijlstra 
321deed85e7STrond Myklebust static void nfs_set_page_writeback(struct page *page)
32289a09141SPeter Zijlstra {
3230db10944SJan Kara 	struct inode *inode = page_file_mapping(page)->host;
3240db10944SJan Kara 	struct nfs_server *nfss = NFS_SERVER(inode);
3255a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
3265a6d41b3STrond Myklebust 
327deed85e7STrond Myklebust 	WARN_ON_ONCE(ret != 0);
32889a09141SPeter Zijlstra 
329277866a0SPeter Zijlstra 	if (atomic_long_inc_return(&nfss->writeback) >
3300db10944SJan Kara 			NFS_CONGESTION_ON_THRESH)
3310db10944SJan Kara 		set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
33289a09141SPeter Zijlstra }
33389a09141SPeter Zijlstra 
33420633f04SWeston Andros Adamson static void nfs_end_page_writeback(struct nfs_page *req)
33589a09141SPeter Zijlstra {
33620633f04SWeston Andros Adamson 	struct inode *inode = page_file_mapping(req->wb_page)->host;
33789a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
33831a01f09STrond Myklebust 	bool is_done;
33989a09141SPeter Zijlstra 
34031a01f09STrond Myklebust 	is_done = nfs_page_group_sync_on_bit(req, PG_WB_END);
34131a01f09STrond Myklebust 	nfs_unlock_request(req);
34231a01f09STrond Myklebust 	if (!is_done)
34320633f04SWeston Andros Adamson 		return;
34420633f04SWeston Andros Adamson 
34520633f04SWeston Andros Adamson 	end_page_writeback(req->wb_page);
346c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
3470db10944SJan Kara 		clear_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
34889a09141SPeter Zijlstra }
34989a09141SPeter Zijlstra 
350d4581383SWeston Andros Adamson 
351d4581383SWeston Andros Adamson /* nfs_page_group_clear_bits
352d4581383SWeston Andros Adamson  *   @req - an nfs request
353d4581383SWeston Andros Adamson  * clears all page group related bits from @req
354d4581383SWeston Andros Adamson  */
355d4581383SWeston Andros Adamson static void
356d4581383SWeston Andros Adamson nfs_page_group_clear_bits(struct nfs_page *req)
357e261f51fSTrond Myklebust {
358d4581383SWeston Andros Adamson 	clear_bit(PG_TEARDOWN, &req->wb_flags);
359d4581383SWeston Andros Adamson 	clear_bit(PG_UNLOCKPAGE, &req->wb_flags);
360d4581383SWeston Andros Adamson 	clear_bit(PG_UPTODATE, &req->wb_flags);
361d4581383SWeston Andros Adamson 	clear_bit(PG_WB_END, &req->wb_flags);
362d4581383SWeston Andros Adamson 	clear_bit(PG_REMOVE, &req->wb_flags);
363d4581383SWeston Andros Adamson }
364d4581383SWeston Andros Adamson 
365d4581383SWeston Andros Adamson 
366d4581383SWeston Andros Adamson /*
367d4581383SWeston Andros Adamson  * nfs_unroll_locks_and_wait -  unlock all newly locked reqs and wait on @req
368d4581383SWeston Andros Adamson  *
369d4581383SWeston Andros Adamson  * this is a helper function for nfs_lock_and_join_requests
370d4581383SWeston Andros Adamson  *
371d4581383SWeston Andros Adamson  * @inode - inode associated with request page group, must be holding inode lock
372d4581383SWeston Andros Adamson  * @head  - head request of page group, must be holding head lock
373d4581383SWeston Andros Adamson  * @req   - request that couldn't lock and needs to wait on the req bit lock
374d4581383SWeston Andros Adamson  *
375b5bab9bfSTrond Myklebust  * NOTE: this must be called holding page_group bit lock
376b5bab9bfSTrond Myklebust  *       which will be released before returning.
377d4581383SWeston Andros Adamson  *
378d4581383SWeston Andros Adamson  * returns 0 on success, < 0 on error.
379d4581383SWeston Andros Adamson  */
380*74a6d4b5STrond Myklebust static void
381*74a6d4b5STrond Myklebust nfs_unroll_locks(struct inode *inode, struct nfs_page *head,
3826d17d653STrond Myklebust 			  struct nfs_page *req)
383d4581383SWeston Andros Adamson {
384d4581383SWeston Andros Adamson 	struct nfs_page *tmp;
385e261f51fSTrond Myklebust 
386d4581383SWeston Andros Adamson 	/* relinquish all the locks successfully grabbed this run */
387a0e265bcSTrond Myklebust 	for (tmp = head->wb_this_page ; tmp != req; tmp = tmp->wb_this_page)
388d4581383SWeston Andros Adamson 		nfs_unlock_request(tmp);
389d4581383SWeston Andros Adamson 
390d4581383SWeston Andros Adamson 	WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
391e261f51fSTrond Myklebust }
392d4581383SWeston Andros Adamson 
393d4581383SWeston Andros Adamson /*
394d4581383SWeston Andros Adamson  * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests
395d4581383SWeston Andros Adamson  *
396d4581383SWeston Andros Adamson  * @destroy_list - request list (using wb_this_page) terminated by @old_head
397d4581383SWeston Andros Adamson  * @old_head - the old head of the list
398d4581383SWeston Andros Adamson  *
399d4581383SWeston Andros Adamson  * All subrequests must be locked and removed from all lists, so at this point
400d4581383SWeston Andros Adamson  * they are only "active" in this function, and possibly in nfs_wait_on_request
401d4581383SWeston Andros Adamson  * with a reference held by some other context.
402d4581383SWeston Andros Adamson  */
403d4581383SWeston Andros Adamson static void
404d4581383SWeston Andros Adamson nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list,
405b66aaa8dSTrond Myklebust 				 struct nfs_page *old_head,
406b66aaa8dSTrond Myklebust 				 struct inode *inode)
407d4581383SWeston Andros Adamson {
408d4581383SWeston Andros Adamson 	while (destroy_list) {
409d4581383SWeston Andros Adamson 		struct nfs_page *subreq = destroy_list;
410d4581383SWeston Andros Adamson 
411d4581383SWeston Andros Adamson 		destroy_list = (subreq->wb_this_page == old_head) ?
412d4581383SWeston Andros Adamson 				   NULL : subreq->wb_this_page;
413d4581383SWeston Andros Adamson 
414d4581383SWeston Andros Adamson 		WARN_ON_ONCE(old_head != subreq->wb_head);
415d4581383SWeston Andros Adamson 
416d4581383SWeston Andros Adamson 		/* make sure old group is not used */
417d4581383SWeston Andros Adamson 		subreq->wb_head = subreq;
418d4581383SWeston Andros Adamson 		subreq->wb_this_page = subreq;
419d4581383SWeston Andros Adamson 
420d4581383SWeston Andros Adamson 		/* subreq is now totally disconnected from page group or any
421d4581383SWeston Andros Adamson 		 * write / commit lists. last chance to wake any waiters */
422d4581383SWeston Andros Adamson 		nfs_unlock_request(subreq);
423d4581383SWeston Andros Adamson 
424d4581383SWeston Andros Adamson 		if (!test_bit(PG_TEARDOWN, &subreq->wb_flags)) {
425d4581383SWeston Andros Adamson 			/* release ref on old head request */
426d4581383SWeston Andros Adamson 			nfs_release_request(old_head);
427d4581383SWeston Andros Adamson 
428d4581383SWeston Andros Adamson 			nfs_page_group_clear_bits(subreq);
429d4581383SWeston Andros Adamson 
430d4581383SWeston Andros Adamson 			/* release the PG_INODE_REF reference */
431b66aaa8dSTrond Myklebust 			if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags)) {
432d4581383SWeston Andros Adamson 				nfs_release_request(subreq);
433b66aaa8dSTrond Myklebust 				spin_lock(&inode->i_lock);
434b66aaa8dSTrond Myklebust 				NFS_I(inode)->nrequests--;
435b66aaa8dSTrond Myklebust 				spin_unlock(&inode->i_lock);
436b66aaa8dSTrond Myklebust 			} else
437d4581383SWeston Andros Adamson 				WARN_ON_ONCE(1);
438d4581383SWeston Andros Adamson 		} else {
439d4581383SWeston Andros Adamson 			WARN_ON_ONCE(test_bit(PG_CLEAN, &subreq->wb_flags));
440d4581383SWeston Andros Adamson 			/* zombie requests have already released the last
441d4581383SWeston Andros Adamson 			 * reference and were waiting on the rest of the
442d4581383SWeston Andros Adamson 			 * group to complete. Since it's no longer part of a
443d4581383SWeston Andros Adamson 			 * group, simply free the request */
444d4581383SWeston Andros Adamson 			nfs_page_group_clear_bits(subreq);
445d4581383SWeston Andros Adamson 			nfs_free_request(subreq);
446d4581383SWeston Andros Adamson 		}
447d4581383SWeston Andros Adamson 	}
448d4581383SWeston Andros Adamson }
449d4581383SWeston Andros Adamson 
450d4581383SWeston Andros Adamson /*
451d4581383SWeston Andros Adamson  * nfs_lock_and_join_requests - join all subreqs to the head req and return
452d4581383SWeston Andros Adamson  *                              a locked reference, cancelling any pending
453d4581383SWeston Andros Adamson  *                              operations for this page.
454d4581383SWeston Andros Adamson  *
455d4581383SWeston Andros Adamson  * @page - the page used to lookup the "page group" of nfs_page structures
456d4581383SWeston Andros Adamson  *
457d4581383SWeston Andros Adamson  * This function joins all sub requests to the head request by first
458d4581383SWeston Andros Adamson  * locking all requests in the group, cancelling any pending operations
459d4581383SWeston Andros Adamson  * and finally updating the head request to cover the whole range covered by
460d4581383SWeston Andros Adamson  * the (former) group.  All subrequests are removed from any write or commit
461d4581383SWeston Andros Adamson  * lists, unlinked from the group and destroyed.
462d4581383SWeston Andros Adamson  *
463d4581383SWeston Andros Adamson  * Returns a locked, referenced pointer to the head request - which after
464d4581383SWeston Andros Adamson  * this call is guaranteed to be the only request associated with the page.
465d4581383SWeston Andros Adamson  * Returns NULL if no requests are found for @page, or a ERR_PTR if an
466d4581383SWeston Andros Adamson  * error was encountered.
467d4581383SWeston Andros Adamson  */
468d4581383SWeston Andros Adamson static struct nfs_page *
4696d17d653STrond Myklebust nfs_lock_and_join_requests(struct page *page)
470d4581383SWeston Andros Adamson {
471d4581383SWeston Andros Adamson 	struct inode *inode = page_file_mapping(page)->host;
472d4581383SWeston Andros Adamson 	struct nfs_page *head, *subreq;
473d4581383SWeston Andros Adamson 	struct nfs_page *destroy_list = NULL;
474d4581383SWeston Andros Adamson 	unsigned int total_bytes;
475d4581383SWeston Andros Adamson 	int ret;
476d4581383SWeston Andros Adamson 
477d4581383SWeston Andros Adamson try_again:
478b5bab9bfSTrond Myklebust 	if (!(PagePrivate(page) || PageSwapCache(page)))
479b5bab9bfSTrond Myklebust 		return NULL;
480d4581383SWeston Andros Adamson 	spin_lock(&inode->i_lock);
481d4581383SWeston Andros Adamson 	/*
482d4581383SWeston Andros Adamson 	 * A reference is taken only on the head request which acts as a
483d4581383SWeston Andros Adamson 	 * reference to the whole page group - the group will not be destroyed
484d4581383SWeston Andros Adamson 	 * until the head reference is released.
485d4581383SWeston Andros Adamson 	 */
486d4581383SWeston Andros Adamson 	head = nfs_page_find_head_request_locked(NFS_I(inode), page);
487d4581383SWeston Andros Adamson 
488d4581383SWeston Andros Adamson 	if (!head) {
489587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
490d4581383SWeston Andros Adamson 		return NULL;
491d4581383SWeston Andros Adamson 	}
492d4581383SWeston Andros Adamson 
493a0e265bcSTrond Myklebust 	/* lock the page head first in order to avoid an ABBA inefficiency */
494a0e265bcSTrond Myklebust 	if (!nfs_lock_request(head)) {
495a0e265bcSTrond Myklebust 		spin_unlock(&inode->i_lock);
496a0e265bcSTrond Myklebust 		ret = nfs_wait_on_request(head);
497a0e265bcSTrond Myklebust 		nfs_release_request(head);
498a0e265bcSTrond Myklebust 		if (ret < 0)
499a0e265bcSTrond Myklebust 			return ERR_PTR(ret);
500a0e265bcSTrond Myklebust 		goto try_again;
501a0e265bcSTrond Myklebust 	}
50294970014SWeston Andros Adamson 	spin_unlock(&inode->i_lock);
5037c3af975SWeston Andros Adamson 
504b5bab9bfSTrond Myklebust 	ret = nfs_page_group_lock(head, false);
505b5bab9bfSTrond Myklebust 	if (ret < 0) {
506a0e265bcSTrond Myklebust 		nfs_unlock_and_release_request(head);
507b5bab9bfSTrond Myklebust 		return ERR_PTR(ret);
5087c3af975SWeston Andros Adamson 	}
5097c3af975SWeston Andros Adamson 
5107c3af975SWeston Andros Adamson 	/* lock each request in the page group */
511a0e265bcSTrond Myklebust 	total_bytes = head->wb_bytes;
512a0e265bcSTrond Myklebust 	for (subreq = head->wb_this_page; subreq != head;
513a0e265bcSTrond Myklebust 			subreq = subreq->wb_this_page) {
514*74a6d4b5STrond Myklebust 
515*74a6d4b5STrond Myklebust 		while (!nfs_lock_request(subreq)) {
516b5bab9bfSTrond Myklebust 			/*
517*74a6d4b5STrond Myklebust 			 * Unlock page to allow nfs_page_group_sync_on_bit()
518*74a6d4b5STrond Myklebust 			 * to succeed
519b5bab9bfSTrond Myklebust 			 */
520*74a6d4b5STrond Myklebust 			nfs_page_group_unlock(head);
521*74a6d4b5STrond Myklebust 			ret = nfs_wait_on_request(subreq);
522*74a6d4b5STrond Myklebust 			if (!ret)
523*74a6d4b5STrond Myklebust 				ret = nfs_page_group_lock(head, false);
524*74a6d4b5STrond Myklebust 			if (ret < 0) {
525*74a6d4b5STrond Myklebust 				nfs_unroll_locks(inode, head, subreq);
526*74a6d4b5STrond Myklebust 				nfs_unlock_and_release_request(head);
527d4581383SWeston Andros Adamson 				return ERR_PTR(ret);
528d4581383SWeston Andros Adamson 			}
529*74a6d4b5STrond Myklebust 		}
530e14bebf6STrond Myklebust 		/*
531e14bebf6STrond Myklebust 		 * Subrequests are always contiguous, non overlapping
532e14bebf6STrond Myklebust 		 * and in order - but may be repeated (mirrored writes).
533e14bebf6STrond Myklebust 		 */
534e14bebf6STrond Myklebust 		if (subreq->wb_offset == (head->wb_offset + total_bytes)) {
535e14bebf6STrond Myklebust 			/* keep track of how many bytes this group covers */
536e14bebf6STrond Myklebust 			total_bytes += subreq->wb_bytes;
537e14bebf6STrond Myklebust 		} else if (WARN_ON_ONCE(subreq->wb_offset < head->wb_offset ||
538e14bebf6STrond Myklebust 			    ((subreq->wb_offset + subreq->wb_bytes) >
539e14bebf6STrond Myklebust 			     (head->wb_offset + total_bytes)))) {
540e14bebf6STrond Myklebust 			nfs_unlock_request(subreq);
541*74a6d4b5STrond Myklebust 			nfs_unroll_locks(inode, head, subreq);
542*74a6d4b5STrond Myklebust 			nfs_page_group_unlock(head);
543*74a6d4b5STrond Myklebust 			nfs_unlock_and_release_request(head);
544e14bebf6STrond Myklebust 			return ERR_PTR(-EIO);
545e14bebf6STrond Myklebust 		}
546a0e265bcSTrond Myklebust 	}
547d4581383SWeston Andros Adamson 
548d4581383SWeston Andros Adamson 	/* Now that all requests are locked, make sure they aren't on any list.
549d4581383SWeston Andros Adamson 	 * Commit list removal accounting is done after locks are dropped */
550d4581383SWeston Andros Adamson 	subreq = head;
551d4581383SWeston Andros Adamson 	do {
552411a99adSWeston Andros Adamson 		nfs_clear_request_commit(subreq);
553d4581383SWeston Andros Adamson 		subreq = subreq->wb_this_page;
554d4581383SWeston Andros Adamson 	} while (subreq != head);
555d4581383SWeston Andros Adamson 
556d4581383SWeston Andros Adamson 	/* unlink subrequests from head, destroy them later */
557d4581383SWeston Andros Adamson 	if (head->wb_this_page != head) {
558d4581383SWeston Andros Adamson 		/* destroy list will be terminated by head */
559d4581383SWeston Andros Adamson 		destroy_list = head->wb_this_page;
560d4581383SWeston Andros Adamson 		head->wb_this_page = head;
561d4581383SWeston Andros Adamson 
562d4581383SWeston Andros Adamson 		/* change head request to cover whole range that
563d4581383SWeston Andros Adamson 		 * the former page group covered */
564d4581383SWeston Andros Adamson 		head->wb_bytes = total_bytes;
565d4581383SWeston Andros Adamson 	}
566d4581383SWeston Andros Adamson 
567b66aaa8dSTrond Myklebust 	/* Postpone destruction of this request */
568b66aaa8dSTrond Myklebust 	if (test_and_clear_bit(PG_REMOVE, &head->wb_flags)) {
569b66aaa8dSTrond Myklebust 		set_bit(PG_INODE_REF, &head->wb_flags);
570b66aaa8dSTrond Myklebust 		kref_get(&head->wb_kref);
571b5bab9bfSTrond Myklebust 		spin_lock(&inode->i_lock);
572b66aaa8dSTrond Myklebust 		NFS_I(inode)->nrequests++;
573b5bab9bfSTrond Myklebust 		spin_unlock(&inode->i_lock);
574b66aaa8dSTrond Myklebust 	}
575b66aaa8dSTrond Myklebust 
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 	nfs_page_group_unlock(head);
582d4581383SWeston Andros Adamson 
583b66aaa8dSTrond Myklebust 	nfs_destroy_unlinked_subrequests(destroy_list, head, inode);
584d4581383SWeston Andros Adamson 
585b5bab9bfSTrond Myklebust 	/* Did we lose a race with nfs_inode_remove_request()? */
586b5bab9bfSTrond Myklebust 	if (!(PagePrivate(page) || PageSwapCache(page))) {
587b5bab9bfSTrond Myklebust 		nfs_unlock_and_release_request(head);
588b5bab9bfSTrond Myklebust 		return NULL;
589b5bab9bfSTrond Myklebust 	}
590b5bab9bfSTrond Myklebust 
591d4581383SWeston Andros Adamson 	/* still holds ref on head from nfs_page_find_head_request_locked
592d4581383SWeston Andros Adamson 	 * and still has lock on head from lock loop */
593d4581383SWeston Andros Adamson 	return head;
594612c9384STrond Myklebust }
595074cc1deSTrond Myklebust 
5960bcbf039SPeng Tao static void nfs_write_error_remove_page(struct nfs_page *req)
5970bcbf039SPeng Tao {
5980bcbf039SPeng Tao 	nfs_end_page_writeback(req);
5990bcbf039SPeng Tao 	generic_error_remove_page(page_file_mapping(req->wb_page),
6000bcbf039SPeng Tao 				  req->wb_page);
6011f84ccdfSFred Isaman 	nfs_release_request(req);
6020bcbf039SPeng Tao }
6030bcbf039SPeng Tao 
604a6598813STrond Myklebust static bool
605a6598813STrond Myklebust nfs_error_is_fatal_on_server(int err)
606a6598813STrond Myklebust {
607a6598813STrond Myklebust 	switch (err) {
608a6598813STrond Myklebust 	case 0:
609a6598813STrond Myklebust 	case -ERESTARTSYS:
610a6598813STrond Myklebust 	case -EINTR:
611a6598813STrond Myklebust 		return false;
612a6598813STrond Myklebust 	}
613a6598813STrond Myklebust 	return nfs_error_is_fatal(err);
614e261f51fSTrond Myklebust }
615074cc1deSTrond Myklebust 
616074cc1deSTrond Myklebust /*
617074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
618074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
619074cc1deSTrond Myklebust  */
620074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
6216d17d653STrond Myklebust 				struct page *page)
622074cc1deSTrond Myklebust {
623074cc1deSTrond Myklebust 	struct nfs_page *req;
624074cc1deSTrond Myklebust 	int ret = 0;
625074cc1deSTrond Myklebust 
6266d17d653STrond Myklebust 	req = nfs_lock_and_join_requests(page);
627074cc1deSTrond Myklebust 	if (!req)
628074cc1deSTrond Myklebust 		goto out;
629074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
630074cc1deSTrond Myklebust 	if (IS_ERR(req))
631074cc1deSTrond Myklebust 		goto out;
632074cc1deSTrond Myklebust 
633deed85e7STrond Myklebust 	nfs_set_page_writeback(page);
634deed85e7STrond Myklebust 	WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
635074cc1deSTrond Myklebust 
636deed85e7STrond Myklebust 	ret = 0;
637a6598813STrond Myklebust 	/* If there is a fatal error that covers this write, just exit */
638a6598813STrond Myklebust 	if (nfs_error_is_fatal_on_server(req->wb_context->error))
639a6598813STrond Myklebust 		goto out_launder;
640a6598813STrond Myklebust 
641f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
642074cc1deSTrond Myklebust 		ret = pgio->pg_error;
6430bcbf039SPeng Tao 		/*
644c373fff7STrond Myklebust 		 * Remove the problematic req upon fatal errors on the server
6450bcbf039SPeng Tao 		 */
6460bcbf039SPeng Tao 		if (nfs_error_is_fatal(ret)) {
6470bcbf039SPeng Tao 			nfs_context_set_write_error(req->wb_context, ret);
648c373fff7STrond Myklebust 			if (nfs_error_is_fatal_on_server(ret))
649a6598813STrond Myklebust 				goto out_launder;
650d6c843b9SPeng Tao 		}
6510bcbf039SPeng Tao 		nfs_redirty_request(req);
6520bcbf039SPeng Tao 		ret = -EAGAIN;
65340f90271STrond Myklebust 	} else
65440f90271STrond Myklebust 		nfs_add_stats(page_file_mapping(page)->host,
65540f90271STrond Myklebust 				NFSIOS_WRITEPAGES, 1);
656074cc1deSTrond Myklebust out:
657074cc1deSTrond Myklebust 	return ret;
658a6598813STrond Myklebust out_launder:
659a6598813STrond Myklebust 	nfs_write_error_remove_page(req);
660a6598813STrond Myklebust 	return ret;
661e261f51fSTrond Myklebust }
662e261f51fSTrond Myklebust 
663d6c843b9SPeng Tao static int nfs_do_writepage(struct page *page, struct writeback_control *wbc,
664c373fff7STrond Myklebust 			    struct nfs_pageio_descriptor *pgio)
665f758c885STrond Myklebust {
666cfb506e1STrond Myklebust 	int ret;
667f758c885STrond Myklebust 
6688cd79788SHuang Ying 	nfs_pageio_cond_complete(pgio, page_index(page));
6696d17d653STrond Myklebust 	ret = nfs_page_async_flush(pgio, page);
670cfb506e1STrond Myklebust 	if (ret == -EAGAIN) {
671cfb506e1STrond Myklebust 		redirty_page_for_writepage(wbc, page);
672cfb506e1STrond Myklebust 		ret = 0;
673cfb506e1STrond Myklebust 	}
674cfb506e1STrond Myklebust 	return ret;
675f758c885STrond Myklebust }
676f758c885STrond Myklebust 
677e261f51fSTrond Myklebust /*
6781da177e4SLinus Torvalds  * Write an mmapped page to the server.
6791da177e4SLinus Torvalds  */
680d6c843b9SPeng Tao static int nfs_writepage_locked(struct page *page,
681c373fff7STrond Myklebust 				struct writeback_control *wbc)
6821da177e4SLinus Torvalds {
683f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
68440f90271STrond Myklebust 	struct inode *inode = page_file_mapping(page)->host;
685e261f51fSTrond Myklebust 	int err;
6861da177e4SLinus Torvalds 
68740f90271STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
688811ed92eSTrond Myklebust 	nfs_pageio_init_write(&pgio, inode, 0,
689a20c93e3SChristoph Hellwig 				false, &nfs_async_write_completion_ops);
690c373fff7STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
691f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
692f758c885STrond Myklebust 	if (err < 0)
6934d770ccfSTrond Myklebust 		return err;
694f758c885STrond Myklebust 	if (pgio.pg_error < 0)
695f758c885STrond Myklebust 		return pgio.pg_error;
696f758c885STrond Myklebust 	return 0;
6974d770ccfSTrond Myklebust }
6984d770ccfSTrond Myklebust 
6994d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
7004d770ccfSTrond Myklebust {
701f758c885STrond Myklebust 	int ret;
7024d770ccfSTrond Myklebust 
703c373fff7STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
7041da177e4SLinus Torvalds 	unlock_page(page);
705f758c885STrond Myklebust 	return ret;
706f758c885STrond Myklebust }
707f758c885STrond Myklebust 
708f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
709f758c885STrond Myklebust {
710f758c885STrond Myklebust 	int ret;
711f758c885STrond Myklebust 
712c373fff7STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
713f758c885STrond Myklebust 	unlock_page(page);
714f758c885STrond Myklebust 	return ret;
7151da177e4SLinus Torvalds }
7161da177e4SLinus Torvalds 
717919e3bd9STrond Myklebust static void nfs_io_completion_commit(void *inode)
718919e3bd9STrond Myklebust {
719919e3bd9STrond Myklebust 	nfs_commit_inode(inode, 0);
720919e3bd9STrond Myklebust }
721919e3bd9STrond Myklebust 
7221da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
7231da177e4SLinus Torvalds {
7241da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
725c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
726919e3bd9STrond Myklebust 	struct nfs_io_completion *ioc = nfs_io_completion_alloc(GFP_NOFS);
7271da177e4SLinus Torvalds 	int err;
7281da177e4SLinus Torvalds 
72991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
73091d5b470SChuck Lever 
731919e3bd9STrond Myklebust 	if (ioc)
732919e3bd9STrond Myklebust 		nfs_io_completion_init(ioc, nfs_io_completion_commit, inode);
733919e3bd9STrond Myklebust 
734a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false,
735a20c93e3SChristoph Hellwig 				&nfs_async_write_completion_ops);
736919e3bd9STrond Myklebust 	pgio.pg_io_completion = ioc;
737f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
738c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
739919e3bd9STrond Myklebust 	nfs_io_completion_put(ioc);
74072cb77f4STrond Myklebust 
741f758c885STrond Myklebust 	if (err < 0)
74272cb77f4STrond Myklebust 		goto out_err;
74372cb77f4STrond Myklebust 	err = pgio.pg_error;
74472cb77f4STrond Myklebust 	if (err < 0)
74572cb77f4STrond Myklebust 		goto out_err;
746c63c7b05STrond Myklebust 	return 0;
74772cb77f4STrond Myklebust out_err:
74872cb77f4STrond Myklebust 	return err;
7491da177e4SLinus Torvalds }
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds /*
7521da177e4SLinus Torvalds  * Insert a write request into an inode
7531da177e4SLinus Torvalds  */
754d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
7551da177e4SLinus Torvalds {
7561da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
757e7d39069STrond Myklebust 
7582bfc6e56SWeston Andros Adamson 	WARN_ON_ONCE(req->wb_this_page != req);
7592bfc6e56SWeston Andros Adamson 
760e7d39069STrond Myklebust 	/* Lock the request! */
7617ad84aa9STrond Myklebust 	nfs_lock_request(req);
762e7d39069STrond Myklebust 
763e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
764cb1410c7SWeston Andros Adamson 	if (!nfsi->nrequests &&
765cb1410c7SWeston Andros Adamson 	    NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
766a9a4a87aSTrond Myklebust 		inode->i_version++;
76729418aa4SMel Gorman 	/*
76829418aa4SMel Gorman 	 * Swap-space should not get truncated. Hence no need to plug the race
76929418aa4SMel Gorman 	 * with invalidate/truncate.
77029418aa4SMel Gorman 	 */
77129418aa4SMel Gorman 	if (likely(!PageSwapCache(req->wb_page))) {
7722df485a7STrond Myklebust 		set_bit(PG_MAPPED, &req->wb_flags);
773deb7d638STrond Myklebust 		SetPagePrivate(req->wb_page);
774277459d2STrond Myklebust 		set_page_private(req->wb_page, (unsigned long)req);
77529418aa4SMel Gorman 	}
776cb1410c7SWeston Andros Adamson 	nfsi->nrequests++;
77717089a29SWeston Andros Adamson 	/* this a head request for a page group - mark it as having an
778cb1410c7SWeston Andros Adamson 	 * extra reference so sub groups can follow suit.
779cb1410c7SWeston Andros Adamson 	 * This flag also informs pgio layer when to bump nrequests when
780cb1410c7SWeston Andros Adamson 	 * adding subrequests. */
78117089a29SWeston Andros Adamson 	WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags));
782c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
783e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds /*
78789a09141SPeter Zijlstra  * Remove a write request from an inode
7881da177e4SLinus Torvalds  */
7891da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
7901da177e4SLinus Torvalds {
7912b0143b5SDavid Howells 	struct inode *inode = d_inode(req->wb_context->dentry);
7921da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
79320633f04SWeston Andros Adamson 	struct nfs_page *head;
79420633f04SWeston Andros Adamson 
79520633f04SWeston Andros Adamson 	if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) {
79620633f04SWeston Andros Adamson 		head = req->wb_head;
7971da177e4SLinus Torvalds 
798587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
79967911c8fSAnna Schumaker 		if (likely(head->wb_page && !PageSwapCache(head->wb_page))) {
80020633f04SWeston Andros Adamson 			set_page_private(head->wb_page, 0);
80120633f04SWeston Andros Adamson 			ClearPagePrivate(head->wb_page);
80220633f04SWeston Andros Adamson 			clear_bit(PG_MAPPED, &head->wb_flags);
80329418aa4SMel Gorman 		}
804cb1410c7SWeston Andros Adamson 		nfsi->nrequests--;
805cb1410c7SWeston Andros Adamson 		spin_unlock(&inode->i_lock);
806cb1410c7SWeston Andros Adamson 	} else {
807cb1410c7SWeston Andros Adamson 		spin_lock(&inode->i_lock);
808cb1410c7SWeston Andros Adamson 		nfsi->nrequests--;
809587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
81020633f04SWeston Andros Adamson 	}
81117089a29SWeston Andros Adamson 
81217089a29SWeston Andros Adamson 	if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags))
8131da177e4SLinus Torvalds 		nfs_release_request(req);
8141da177e4SLinus Torvalds }
8151da177e4SLinus Torvalds 
81661822ab5STrond Myklebust static void
8176d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
81861822ab5STrond Myklebust {
81967911c8fSAnna Schumaker 	if (req->wb_page)
82061822ab5STrond Myklebust 		__set_page_dirty_nobuffers(req->wb_page);
82161822ab5STrond Myklebust }
82261822ab5STrond Myklebust 
8233a3908c8STrond Myklebust /*
8243a3908c8STrond Myklebust  * nfs_page_search_commits_for_head_request_locked
8253a3908c8STrond Myklebust  *
8263a3908c8STrond Myklebust  * Search through commit lists on @inode for the head request for @page.
8273a3908c8STrond Myklebust  * Must be called while holding the inode (which is cinfo) lock.
8283a3908c8STrond Myklebust  *
8293a3908c8STrond Myklebust  * Returns the head request if found, or NULL if not found.
8303a3908c8STrond Myklebust  */
8313a3908c8STrond Myklebust static struct nfs_page *
8323a3908c8STrond Myklebust nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
8333a3908c8STrond Myklebust 						struct page *page)
8343a3908c8STrond Myklebust {
8353a3908c8STrond Myklebust 	struct nfs_page *freq, *t;
8363a3908c8STrond Myklebust 	struct nfs_commit_info cinfo;
8373a3908c8STrond Myklebust 	struct inode *inode = &nfsi->vfs_inode;
8383a3908c8STrond Myklebust 
8393a3908c8STrond Myklebust 	nfs_init_cinfo_from_inode(&cinfo, inode);
8403a3908c8STrond Myklebust 
8413a3908c8STrond Myklebust 	/* search through pnfs commit lists */
8423a3908c8STrond Myklebust 	freq = pnfs_search_commit_reqs(inode, &cinfo, page);
8433a3908c8STrond Myklebust 	if (freq)
8443a3908c8STrond Myklebust 		return freq->wb_head;
8453a3908c8STrond Myklebust 
8463a3908c8STrond Myklebust 	/* Linearly search the commit list for the correct request */
8473a3908c8STrond Myklebust 	list_for_each_entry_safe(freq, t, &cinfo.mds->list, wb_list) {
8483a3908c8STrond Myklebust 		if (freq->wb_page == page)
8493a3908c8STrond Myklebust 			return freq->wb_head;
8503a3908c8STrond Myklebust 	}
8513a3908c8STrond Myklebust 
8523a3908c8STrond Myklebust 	return NULL;
8533a3908c8STrond Myklebust }
8543a3908c8STrond Myklebust 
8558dd37758STrond Myklebust /**
85686d80f97STrond Myklebust  * nfs_request_add_commit_list_locked - add request to a commit list
85786d80f97STrond Myklebust  * @req: pointer to a struct nfs_page
85886d80f97STrond Myklebust  * @dst: commit list head
85986d80f97STrond Myklebust  * @cinfo: holds list lock and accounting info
86086d80f97STrond Myklebust  *
86186d80f97STrond Myklebust  * This sets the PG_CLEAN bit, updates the cinfo count of
86286d80f97STrond Myklebust  * number of outstanding requests requiring a commit as well as
86386d80f97STrond Myklebust  * the MM page stats.
86486d80f97STrond Myklebust  *
865fe238e60SDave Wysochanski  * The caller must hold cinfo->inode->i_lock, and the nfs_page lock.
86686d80f97STrond Myklebust  */
86786d80f97STrond Myklebust void
86886d80f97STrond Myklebust nfs_request_add_commit_list_locked(struct nfs_page *req, struct list_head *dst,
86986d80f97STrond Myklebust 			    struct nfs_commit_info *cinfo)
87086d80f97STrond Myklebust {
87186d80f97STrond Myklebust 	set_bit(PG_CLEAN, &req->wb_flags);
87286d80f97STrond Myklebust 	nfs_list_add_request(req, dst);
87386d80f97STrond Myklebust 	cinfo->mds->ncommit++;
87486d80f97STrond Myklebust }
87586d80f97STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked);
87686d80f97STrond Myklebust 
87786d80f97STrond Myklebust /**
8788dd37758STrond Myklebust  * nfs_request_add_commit_list - add request to a commit list
8798dd37758STrond Myklebust  * @req: pointer to a struct nfs_page
880ea2cf228SFred Isaman  * @dst: commit list head
881ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
8828dd37758STrond Myklebust  *
883ea2cf228SFred Isaman  * This sets the PG_CLEAN bit, updates the cinfo count of
8848dd37758STrond Myklebust  * number of outstanding requests requiring a commit as well as
8858dd37758STrond Myklebust  * the MM page stats.
8868dd37758STrond Myklebust  *
887ea2cf228SFred Isaman  * The caller must _not_ hold the cinfo->lock, but must be
8888dd37758STrond Myklebust  * holding the nfs_page lock.
8898dd37758STrond Myklebust  */
8908dd37758STrond Myklebust void
8916272dcc6SAnna Schumaker nfs_request_add_commit_list(struct nfs_page *req, struct nfs_commit_info *cinfo)
8928dd37758STrond Myklebust {
893fe238e60SDave Wysochanski 	spin_lock(&cinfo->inode->i_lock);
8946272dcc6SAnna Schumaker 	nfs_request_add_commit_list_locked(req, &cinfo->mds->list, cinfo);
895fe238e60SDave Wysochanski 	spin_unlock(&cinfo->inode->i_lock);
89667911c8fSAnna Schumaker 	if (req->wb_page)
89786d80f97STrond Myklebust 		nfs_mark_page_unstable(req->wb_page, cinfo);
8988dd37758STrond Myklebust }
8998dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
9008dd37758STrond Myklebust 
9018dd37758STrond Myklebust /**
9028dd37758STrond Myklebust  * nfs_request_remove_commit_list - Remove request from a commit list
9038dd37758STrond Myklebust  * @req: pointer to a nfs_page
904ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
9058dd37758STrond Myklebust  *
906ea2cf228SFred Isaman  * This clears the PG_CLEAN bit, and updates the cinfo's count of
9078dd37758STrond Myklebust  * number of outstanding requests requiring a commit
9088dd37758STrond Myklebust  * It does not update the MM page stats.
9098dd37758STrond Myklebust  *
910ea2cf228SFred Isaman  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
9118dd37758STrond Myklebust  */
9128dd37758STrond Myklebust void
913ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req,
914ea2cf228SFred Isaman 			       struct nfs_commit_info *cinfo)
9158dd37758STrond Myklebust {
9168dd37758STrond Myklebust 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
9178dd37758STrond Myklebust 		return;
9188dd37758STrond Myklebust 	nfs_list_remove_request(req);
919ea2cf228SFred Isaman 	cinfo->mds->ncommit--;
9208dd37758STrond Myklebust }
9218dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
9228dd37758STrond Myklebust 
923ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
924ea2cf228SFred Isaman 				      struct inode *inode)
925ea2cf228SFred Isaman {
926fe238e60SDave Wysochanski 	cinfo->inode = inode;
927ea2cf228SFred Isaman 	cinfo->mds = &NFS_I(inode)->commit_info;
928ea2cf228SFred Isaman 	cinfo->ds = pnfs_get_ds_info(inode);
929b359f9d0SFred Isaman 	cinfo->dreq = NULL;
930f453a54aSFred Isaman 	cinfo->completion_ops = &nfs_commit_completion_ops;
931ea2cf228SFred Isaman }
932ea2cf228SFred Isaman 
933ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
934ea2cf228SFred Isaman 		    struct inode *inode,
935ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq)
936ea2cf228SFred Isaman {
9371763da12SFred Isaman 	if (dreq)
9381763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
9391763da12SFred Isaman 	else
940ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(cinfo, inode);
941ea2cf228SFred Isaman }
942ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo);
9438dd37758STrond Myklebust 
9441da177e4SLinus Torvalds /*
9451da177e4SLinus Torvalds  * Add a request to the inode's commit list.
9461da177e4SLinus Torvalds  */
9471763da12SFred Isaman void
948ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
949b57ff130SWeston Andros Adamson 			struct nfs_commit_info *cinfo, u32 ds_commit_idx)
9501da177e4SLinus Torvalds {
951b57ff130SWeston Andros Adamson 	if (pnfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx))
9528dd37758STrond Myklebust 		return;
9536272dcc6SAnna Schumaker 	nfs_request_add_commit_list(req, cinfo);
9541da177e4SLinus Torvalds }
9558e821cadSTrond Myklebust 
956d6d6dc7cSFred Isaman static void
957d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page)
958e468bae9STrond Myklebust {
95911fb9989SMel Gorman 	dec_node_page_state(page, NR_UNSTABLE_NFS);
96093f78d88STejun Heo 	dec_wb_stat(&inode_to_bdi(page_file_mapping(page)->host)->wb,
96193f78d88STejun Heo 		    WB_RECLAIMABLE);
962e468bae9STrond Myklebust }
963d6d6dc7cSFred Isaman 
964b5bab9bfSTrond Myklebust /* Called holding the request lock on @req */
9658dd37758STrond Myklebust static void
966d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req)
967d6d6dc7cSFred Isaman {
9688dd37758STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
9692b0143b5SDavid Howells 		struct inode *inode = d_inode(req->wb_context->dentry);
970ea2cf228SFred Isaman 		struct nfs_commit_info cinfo;
971d6d6dc7cSFred Isaman 
972ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(&cinfo, inode);
973b5bab9bfSTrond Myklebust 		spin_lock(&inode->i_lock);
974ea2cf228SFred Isaman 		if (!pnfs_clear_request_commit(req, &cinfo)) {
975ea2cf228SFred Isaman 			nfs_request_remove_commit_list(req, &cinfo);
976d6d6dc7cSFred Isaman 		}
977b5bab9bfSTrond Myklebust 		spin_unlock(&inode->i_lock);
9788dd37758STrond Myklebust 		nfs_clear_page_commit(req->wb_page);
9798dd37758STrond Myklebust 	}
980e468bae9STrond Myklebust }
981e468bae9STrond Myklebust 
982d45f60c6SWeston Andros Adamson int nfs_write_need_commit(struct nfs_pgio_header *hdr)
9838e821cadSTrond Myklebust {
984c65e6254SWeston Andros Adamson 	if (hdr->verf.committed == NFS_DATA_SYNC)
985d45f60c6SWeston Andros Adamson 		return hdr->lseg == NULL;
986c65e6254SWeston Andros Adamson 	return hdr->verf.committed != NFS_FILE_SYNC;
9878e821cadSTrond Myklebust }
9888e821cadSTrond Myklebust 
989919e3bd9STrond Myklebust static void nfs_async_write_init(struct nfs_pgio_header *hdr)
990919e3bd9STrond Myklebust {
991919e3bd9STrond Myklebust 	nfs_io_completion_get(hdr->io_completion);
992919e3bd9STrond Myklebust }
993919e3bd9STrond Myklebust 
994061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
9956c75dc0dSFred Isaman {
996ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
9976c75dc0dSFred Isaman 	unsigned long bytes = 0;
9986c75dc0dSFred Isaman 
9996c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
10006c75dc0dSFred Isaman 		goto out;
1001ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
10026c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
10036c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
10046c75dc0dSFred Isaman 
10056c75dc0dSFred Isaman 		bytes += req->wb_bytes;
10066c75dc0dSFred Isaman 		nfs_list_remove_request(req);
10076c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
10086c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
1009d1182b33STrond Myklebust 			nfs_set_pageerror(req->wb_page);
10106c75dc0dSFred Isaman 			nfs_context_set_write_error(req->wb_context, hdr->error);
10116c75dc0dSFred Isaman 			goto remove_req;
10126c75dc0dSFred Isaman 		}
1013c65e6254SWeston Andros Adamson 		if (nfs_write_need_commit(hdr)) {
1014f79d06f5SAnna Schumaker 			memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
1015b57ff130SWeston Andros Adamson 			nfs_mark_request_commit(req, hdr->lseg, &cinfo,
1016a7d42ddbSWeston Andros Adamson 				hdr->pgio_mirror_idx);
10176c75dc0dSFred Isaman 			goto next;
10186c75dc0dSFred Isaman 		}
10196c75dc0dSFred Isaman remove_req:
10206c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
10216c75dc0dSFred Isaman next:
102220633f04SWeston Andros Adamson 		nfs_end_page_writeback(req);
10233aff4ebbSTrond Myklebust 		nfs_release_request(req);
10246c75dc0dSFred Isaman 	}
10256c75dc0dSFred Isaman out:
1026919e3bd9STrond Myklebust 	nfs_io_completion_put(hdr->io_completion);
10276c75dc0dSFred Isaman 	hdr->release(hdr);
10286c75dc0dSFred Isaman }
10296c75dc0dSFred Isaman 
1030ce59515cSAnna Schumaker unsigned long
1031ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
1032fb8a1f11STrond Myklebust {
1033ea2cf228SFred Isaman 	return cinfo->mds->ncommit;
1034fb8a1f11STrond Myklebust }
1035fb8a1f11STrond Myklebust 
1036fe238e60SDave Wysochanski /* cinfo->inode->i_lock held by caller */
10371763da12SFred Isaman int
1038ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
1039ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
1040d6d6dc7cSFred Isaman {
1041d6d6dc7cSFred Isaman 	struct nfs_page *req, *tmp;
1042d6d6dc7cSFred Isaman 	int ret = 0;
1043d6d6dc7cSFred Isaman 
1044d6d6dc7cSFred Isaman 	list_for_each_entry_safe(req, tmp, src, wb_list) {
10458dd37758STrond Myklebust 		if (!nfs_lock_request(req))
10468dd37758STrond Myklebust 			continue;
10477ad84aa9STrond Myklebust 		kref_get(&req->wb_kref);
1048fe238e60SDave Wysochanski 		if (cond_resched_lock(&cinfo->inode->i_lock))
10493b3be88dSTrond Myklebust 			list_safe_reset_next(req, tmp, wb_list);
1050ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
10518dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
1052d6d6dc7cSFred Isaman 		ret++;
10531763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
1054d6d6dc7cSFred Isaman 			break;
1055d6d6dc7cSFred Isaman 	}
1056d6d6dc7cSFred Isaman 	return ret;
1057d6d6dc7cSFred Isaman }
1058d6d6dc7cSFred Isaman 
10591da177e4SLinus Torvalds /*
10601da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
10611da177e4SLinus Torvalds  * @inode: NFS inode to scan
1062ea2cf228SFred Isaman  * @dst: mds destination list
1063ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
10641da177e4SLinus Torvalds  *
10651da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
10661da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
10671da177e4SLinus Torvalds  */
10681763da12SFred Isaman int
1069ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
1070ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
10711da177e4SLinus Torvalds {
1072d6d6dc7cSFred Isaman 	int ret = 0;
1073fb8a1f11STrond Myklebust 
1074fe238e60SDave Wysochanski 	spin_lock(&cinfo->inode->i_lock);
1075ea2cf228SFred Isaman 	if (cinfo->mds->ncommit > 0) {
10768dd37758STrond Myklebust 		const int max = INT_MAX;
1077d6d6dc7cSFred Isaman 
1078ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
1079ea2cf228SFred Isaman 					   cinfo, max);
1080ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
1081d6d6dc7cSFred Isaman 	}
1082fe238e60SDave Wysochanski 	spin_unlock(&cinfo->inode->i_lock);
1083ff778d02STrond Myklebust 	return ret;
10841da177e4SLinus Torvalds }
1085d6d6dc7cSFred Isaman 
10861da177e4SLinus Torvalds /*
1087e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
1088e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
10891da177e4SLinus Torvalds  *
1090e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
1091e7d39069STrond Myklebust  * to disk.
10921da177e4SLinus Torvalds  */
1093e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
1094e7d39069STrond Myklebust 		struct page *page,
1095e7d39069STrond Myklebust 		unsigned int offset,
1096e7d39069STrond Myklebust 		unsigned int bytes)
10971da177e4SLinus Torvalds {
1098e7d39069STrond Myklebust 	struct nfs_page *req;
1099e7d39069STrond Myklebust 	unsigned int rqend;
1100e7d39069STrond Myklebust 	unsigned int end;
11011da177e4SLinus Torvalds 	int error;
1102277459d2STrond Myklebust 
1103e7d39069STrond Myklebust 	end = offset + bytes;
1104e7d39069STrond Myklebust 
1105f6032f21STrond Myklebust 	req = nfs_lock_and_join_requests(page);
1106f6032f21STrond Myklebust 	if (IS_ERR_OR_NULL(req))
1107f6032f21STrond Myklebust 		return req;
11082bfc6e56SWeston Andros Adamson 
1109e7d39069STrond Myklebust 	rqend = req->wb_offset + req->wb_bytes;
1110e7d39069STrond Myklebust 	/*
1111e7d39069STrond Myklebust 	 * Tell the caller to flush out the request if
1112e7d39069STrond Myklebust 	 * the offsets are non-contiguous.
1113e7d39069STrond Myklebust 	 * Note: nfs_flush_incompatible() will already
1114e7d39069STrond Myklebust 	 * have flushed out requests having wrong owners.
1115e7d39069STrond Myklebust 	 */
1116f6032f21STrond Myklebust 	if (offset > rqend || end < req->wb_offset)
1117e7d39069STrond Myklebust 		goto out_flushme;
1118e7d39069STrond Myklebust 
11191da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
11201da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
11211da177e4SLinus Torvalds 		req->wb_offset = offset;
11221da177e4SLinus Torvalds 		req->wb_pgbase = offset;
11231da177e4SLinus Torvalds 	}
11241da177e4SLinus Torvalds 	if (end > rqend)
11251da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
1126e7d39069STrond Myklebust 	else
1127e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
1128e7d39069STrond Myklebust 	return req;
1129e7d39069STrond Myklebust out_flushme:
1130f6032f21STrond Myklebust 	/*
1131f6032f21STrond Myklebust 	 * Note: we mark the request dirty here because
1132f6032f21STrond Myklebust 	 * nfs_lock_and_join_requests() cannot preserve
1133f6032f21STrond Myklebust 	 * commit flags, so we have to replay the write.
1134f6032f21STrond Myklebust 	 */
1135f6032f21STrond Myklebust 	nfs_mark_request_dirty(req);
1136f6032f21STrond Myklebust 	nfs_unlock_and_release_request(req);
1137e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
1138f6032f21STrond Myklebust 	return (error < 0) ? ERR_PTR(error) : NULL;
1139e7d39069STrond Myklebust }
11401da177e4SLinus Torvalds 
1141e7d39069STrond Myklebust /*
1142e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
1143e7d39069STrond Myklebust  *
1144e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
1145e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
1146e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
1147e7d39069STrond Myklebust  */
1148e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
1149e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
1150e7d39069STrond Myklebust {
1151d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
1152e7d39069STrond Myklebust 	struct nfs_page	*req;
1153e7d39069STrond Myklebust 
1154e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
1155e7d39069STrond Myklebust 	if (req != NULL)
1156e7d39069STrond Myklebust 		goto out;
11572bfc6e56SWeston Andros Adamson 	req = nfs_create_request(ctx, page, NULL, offset, bytes);
1158e7d39069STrond Myklebust 	if (IS_ERR(req))
1159e7d39069STrond Myklebust 		goto out;
1160d6d6dc7cSFred Isaman 	nfs_inode_add_request(inode, req);
1161efc91ed0STrond Myklebust out:
116261e930a9STrond Myklebust 	return req;
11631da177e4SLinus Torvalds }
11641da177e4SLinus Torvalds 
1165e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
1166e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
1167e7d39069STrond Myklebust {
1168e7d39069STrond Myklebust 	struct nfs_page	*req;
1169e7d39069STrond Myklebust 
1170e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
1171e7d39069STrond Myklebust 	if (IS_ERR(req))
1172e7d39069STrond Myklebust 		return PTR_ERR(req);
1173e7d39069STrond Myklebust 	/* Update file length */
1174e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
1175d72ddcbaSWeston Andros Adamson 	nfs_mark_uptodate(req);
1176a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
11771d1afcbcSTrond Myklebust 	nfs_unlock_and_release_request(req);
1178e7d39069STrond Myklebust 	return 0;
1179e7d39069STrond Myklebust }
1180e7d39069STrond Myklebust 
11811da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
11821da177e4SLinus Torvalds {
1183cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
11842a369153STrond Myklebust 	struct nfs_lock_context *l_ctx;
1185bd61e0a9SJeff Layton 	struct file_lock_context *flctx = file_inode(file)->i_flctx;
11861da177e4SLinus Torvalds 	struct nfs_page	*req;
11871a54533eSTrond Myklebust 	int do_flush, status;
11881da177e4SLinus Torvalds 	/*
11891da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
11901da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
11911da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
11921da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
11931da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
11941da177e4SLinus Torvalds 	 * dropped page.
11951da177e4SLinus Torvalds 	 */
11961a54533eSTrond Myklebust 	do {
119784d3a9a9SWeston Andros Adamson 		req = nfs_page_find_head_request(page);
11981a54533eSTrond Myklebust 		if (req == NULL)
11991a54533eSTrond Myklebust 			return 0;
12002a369153STrond Myklebust 		l_ctx = req->wb_lock_context;
1201138a2935STrond Myklebust 		do_flush = req->wb_page != page ||
1202138a2935STrond Myklebust 			!nfs_match_open_context(req->wb_context, ctx);
1203bd61e0a9SJeff Layton 		if (l_ctx && flctx &&
1204bd61e0a9SJeff Layton 		    !(list_empty_careful(&flctx->flc_posix) &&
1205bd61e0a9SJeff Layton 		      list_empty_careful(&flctx->flc_flock))) {
1206d51fdb87SNeilBrown 			do_flush |= l_ctx->lockowner != current->files;
12075263e31eSJeff Layton 		}
12081da177e4SLinus Torvalds 		nfs_release_request(req);
12091a54533eSTrond Myklebust 		if (!do_flush)
12101a54533eSTrond Myklebust 			return 0;
1211d56b4ddfSMel Gorman 		status = nfs_wb_page(page_file_mapping(page)->host, page);
12121a54533eSTrond Myklebust 	} while (status == 0);
12131a54533eSTrond Myklebust 	return status;
12141da177e4SLinus Torvalds }
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds /*
1217dc24826bSAndy Adamson  * Avoid buffered writes when a open context credential's key would
1218dc24826bSAndy Adamson  * expire soon.
1219dc24826bSAndy Adamson  *
1220dc24826bSAndy Adamson  * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
1221dc24826bSAndy Adamson  *
1222dc24826bSAndy Adamson  * Return 0 and set a credential flag which triggers the inode to flush
1223dc24826bSAndy Adamson  * and performs  NFS_FILE_SYNC writes if the key will expired within
1224dc24826bSAndy Adamson  * RPC_KEY_EXPIRE_TIMEO.
1225dc24826bSAndy Adamson  */
1226dc24826bSAndy Adamson int
1227dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode)
1228dc24826bSAndy Adamson {
1229dc24826bSAndy Adamson 	struct nfs_open_context *ctx = nfs_file_open_context(filp);
1230dc24826bSAndy Adamson 	struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
1231dc24826bSAndy Adamson 
1232dc24826bSAndy Adamson 	return rpcauth_key_timeout_notify(auth, ctx->cred);
1233dc24826bSAndy Adamson }
1234dc24826bSAndy Adamson 
1235dc24826bSAndy Adamson /*
1236dc24826bSAndy Adamson  * Test if the open context credential key is marked to expire soon.
1237dc24826bSAndy Adamson  */
1238ce52914eSScott Mayhew bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode)
1239dc24826bSAndy Adamson {
1240ce52914eSScott Mayhew 	struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
1241ce52914eSScott Mayhew 
1242ce52914eSScott Mayhew 	return rpcauth_cred_key_to_expire(auth, ctx->cred);
1243dc24826bSAndy Adamson }
1244dc24826bSAndy Adamson 
1245dc24826bSAndy Adamson /*
12465d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
12475d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
12485d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
12495d47a356STrond Myklebust  */
12508d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
12515d47a356STrond Myklebust {
1252d529ef83SJeff Layton 	struct nfs_inode *nfsi = NFS_I(inode);
1253d529ef83SJeff Layton 
12548d197a56STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
12558d197a56STrond Myklebust 		goto out;
125618dd78c4SScott Mayhew 	if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
1257d529ef83SJeff Layton 		return false;
12584db72b40SJeff Layton 	smp_rmb();
1259d529ef83SJeff Layton 	if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags))
12608d197a56STrond Myklebust 		return false;
12618d197a56STrond Myklebust out:
126218dd78c4SScott Mayhew 	if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
126318dd78c4SScott Mayhew 		return false;
12648d197a56STrond Myklebust 	return PageUptodate(page) != 0;
12655d47a356STrond Myklebust }
12665d47a356STrond Myklebust 
12675263e31eSJeff Layton static bool
12685263e31eSJeff Layton is_whole_file_wrlock(struct file_lock *fl)
12695263e31eSJeff Layton {
12705263e31eSJeff Layton 	return fl->fl_start == 0 && fl->fl_end == OFFSET_MAX &&
12715263e31eSJeff Layton 			fl->fl_type == F_WRLCK;
12725263e31eSJeff Layton }
12735263e31eSJeff Layton 
1274c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or
1275c7559663SScott Mayhew  * if we have the whole file locked for writing), it may be more efficient to
1276c7559663SScott Mayhew  * extend the write to cover the entire page in order to avoid fragmentation
1277c7559663SScott Mayhew  * inefficiencies.
1278c7559663SScott Mayhew  *
1279263b4509SScott Mayhew  * If the file is opened for synchronous writes then we can just skip the rest
1280263b4509SScott Mayhew  * of the checks.
1281c7559663SScott Mayhew  */
1282c7559663SScott Mayhew static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode)
1283c7559663SScott Mayhew {
12845263e31eSJeff Layton 	int ret;
12855263e31eSJeff Layton 	struct file_lock_context *flctx = inode->i_flctx;
12865263e31eSJeff Layton 	struct file_lock *fl;
12875263e31eSJeff Layton 
1288c7559663SScott Mayhew 	if (file->f_flags & O_DSYNC)
1289c7559663SScott Mayhew 		return 0;
1290263b4509SScott Mayhew 	if (!nfs_write_pageuptodate(page, inode))
1291263b4509SScott Mayhew 		return 0;
1292c7559663SScott Mayhew 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
1293c7559663SScott Mayhew 		return 1;
1294bd61e0a9SJeff Layton 	if (!flctx || (list_empty_careful(&flctx->flc_flock) &&
1295bd61e0a9SJeff Layton 		       list_empty_careful(&flctx->flc_posix)))
12968fa4592aSTrond Myklebust 		return 1;
12975263e31eSJeff Layton 
12985263e31eSJeff Layton 	/* Check to see if there are whole file write locks */
12995263e31eSJeff Layton 	ret = 0;
13006109c850SJeff Layton 	spin_lock(&flctx->flc_lock);
1301bd61e0a9SJeff Layton 	if (!list_empty(&flctx->flc_posix)) {
1302bd61e0a9SJeff Layton 		fl = list_first_entry(&flctx->flc_posix, struct file_lock,
1303bd61e0a9SJeff Layton 					fl_list);
1304bd61e0a9SJeff Layton 		if (is_whole_file_wrlock(fl))
13055263e31eSJeff Layton 			ret = 1;
1306bd61e0a9SJeff Layton 	} else if (!list_empty(&flctx->flc_flock)) {
13075263e31eSJeff Layton 		fl = list_first_entry(&flctx->flc_flock, struct file_lock,
13085263e31eSJeff Layton 					fl_list);
13095263e31eSJeff Layton 		if (fl->fl_type == F_WRLCK)
13105263e31eSJeff Layton 			ret = 1;
13115263e31eSJeff Layton 	}
13126109c850SJeff Layton 	spin_unlock(&flctx->flc_lock);
13135263e31eSJeff Layton 	return ret;
1314c7559663SScott Mayhew }
1315c7559663SScott Mayhew 
13165d47a356STrond Myklebust /*
13171da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
13181da177e4SLinus Torvalds  *
13191da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
13201da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
13211da177e4SLinus Torvalds  */
13221da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
13231da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
13241da177e4SLinus Torvalds {
1325cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
1326d56b4ddfSMel Gorman 	struct inode	*inode = page_file_mapping(page)->host;
13271da177e4SLinus Torvalds 	int		status = 0;
13281da177e4SLinus Torvalds 
132991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
133091d5b470SChuck Lever 
13316de1472fSAl Viro 	dprintk("NFS:       nfs_updatepage(%pD2 %d@%lld)\n",
13326de1472fSAl Viro 		file, count, (long long)(page_file_offset(page) + offset));
13331da177e4SLinus Torvalds 
1334149a4fddSBenjamin Coddington 	if (!count)
1335149a4fddSBenjamin Coddington 		goto out;
1336149a4fddSBenjamin Coddington 
1337c7559663SScott Mayhew 	if (nfs_can_extend_write(file, page, inode)) {
133849a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
13391da177e4SLinus Torvalds 		offset = 0;
13401da177e4SLinus Torvalds 	}
13411da177e4SLinus Torvalds 
1342e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
134303fa9e84STrond Myklebust 	if (status < 0)
134403fa9e84STrond Myklebust 		nfs_set_pageerror(page);
134559b7c05fSTrond Myklebust 	else
134659b7c05fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
1347149a4fddSBenjamin Coddington out:
134848186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
13491da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
13501da177e4SLinus Torvalds 	return status;
13511da177e4SLinus Torvalds }
13521da177e4SLinus Torvalds 
13533ff7576dSTrond Myklebust static int flush_task_priority(int how)
13541da177e4SLinus Torvalds {
13551da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
13561da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
13571da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
13581da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
13591da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
13601da177e4SLinus Torvalds 	}
13611da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
13621da177e4SLinus Torvalds }
13631da177e4SLinus Torvalds 
1364d45f60c6SWeston Andros Adamson static void nfs_initiate_write(struct nfs_pgio_header *hdr,
1365d45f60c6SWeston Andros Adamson 			       struct rpc_message *msg,
1366abde71f4STom Haynes 			       const struct nfs_rpc_ops *rpc_ops,
13671ed26f33SAnna Schumaker 			       struct rpc_task_setup *task_setup_data, int how)
13681da177e4SLinus Torvalds {
13693ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
13701da177e4SLinus Torvalds 
13711ed26f33SAnna Schumaker 	task_setup_data->priority = priority;
1372abde71f4STom Haynes 	rpc_ops->write_setup(hdr, msg);
1373d138d5d1SAndy Adamson 
1374abde71f4STom Haynes 	nfs4_state_protect_write(NFS_SERVER(hdr->inode)->nfs_client,
1375d45f60c6SWeston Andros Adamson 				 &task_setup_data->rpc_client, msg, hdr);
1376275acaafSTrond Myklebust }
1377275acaafSTrond Myklebust 
13786d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
13796d884e8fSFred  * call this on each, which will prepare them to be retried on next
13806d884e8fSFred  * writeback using standard nfs.
13816d884e8fSFred  */
13826d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
13836d884e8fSFred {
13846d884e8fSFred 	nfs_mark_request_dirty(req);
1385c7070113STrond Myklebust 	set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
138620633f04SWeston Andros Adamson 	nfs_end_page_writeback(req);
13873aff4ebbSTrond Myklebust 	nfs_release_request(req);
13886d884e8fSFred }
13896d884e8fSFred 
1390061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head)
13916c75dc0dSFred Isaman {
13926c75dc0dSFred Isaman 	struct nfs_page	*req;
13936c75dc0dSFred Isaman 
13946c75dc0dSFred Isaman 	while (!list_empty(head)) {
13956c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
13966c75dc0dSFred Isaman 		nfs_list_remove_request(req);
13976c75dc0dSFred Isaman 		nfs_redirty_request(req);
13986c75dc0dSFred Isaman 	}
13996c75dc0dSFred Isaman }
14006c75dc0dSFred Isaman 
1401dc602dd7STrond Myklebust static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr)
1402dc602dd7STrond Myklebust {
1403dc602dd7STrond Myklebust 	nfs_async_write_error(&hdr->pages);
1404dc602dd7STrond Myklebust }
1405dc602dd7STrond Myklebust 
1406061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1407919e3bd9STrond Myklebust 	.init_hdr = nfs_async_write_init,
1408061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1409061ae2edSFred Isaman 	.completion = nfs_write_completion,
1410dc602dd7STrond Myklebust 	.reschedule_io = nfs_async_write_reschedule_io,
1411061ae2edSFred Isaman };
1412061ae2edSFred Isaman 
141357208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1414a20c93e3SChristoph Hellwig 			       struct inode *inode, int ioflags, bool force_mds,
1415061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
14161751c363STrond Myklebust {
1417a20c93e3SChristoph Hellwig 	struct nfs_server *server = NFS_SERVER(inode);
141841d8d5b7SAnna Schumaker 	const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
1419a20c93e3SChristoph Hellwig 
1420a20c93e3SChristoph Hellwig #ifdef CONFIG_NFS_V4_1
1421a20c93e3SChristoph Hellwig 	if (server->pnfs_curr_ld && !force_mds)
1422a20c93e3SChristoph Hellwig 		pg_ops = server->pnfs_curr_ld->pg_write_ops;
1423a20c93e3SChristoph Hellwig #endif
14244a0de55cSAnna Schumaker 	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
1425fbe77c30SBenjamin Coddington 			server->wsize, ioflags, GFP_NOIO);
14261751c363STrond Myklebust }
1427ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
14281751c363STrond Myklebust 
1429dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1430dce81290STrond Myklebust {
1431a7d42ddbSWeston Andros Adamson 	struct nfs_pgio_mirror *mirror;
1432a7d42ddbSWeston Andros Adamson 
14336f29b9bbSKinglong Mee 	if (pgio->pg_ops && pgio->pg_ops->pg_cleanup)
14346f29b9bbSKinglong Mee 		pgio->pg_ops->pg_cleanup(pgio);
14356f29b9bbSKinglong Mee 
143641d8d5b7SAnna Schumaker 	pgio->pg_ops = &nfs_pgio_rw_ops;
1437a7d42ddbSWeston Andros Adamson 
1438a7d42ddbSWeston Andros Adamson 	nfs_pageio_stop_mirroring(pgio);
1439a7d42ddbSWeston Andros Adamson 
1440a7d42ddbSWeston Andros Adamson 	mirror = &pgio->pg_mirrors[0];
1441a7d42ddbSWeston Andros Adamson 	mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1442dce81290STrond Myklebust }
14431f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1444dce81290STrond Myklebust 
14451da177e4SLinus Torvalds 
14460b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
14470b7c0153SFred Isaman {
14480b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
14490b7c0153SFred Isaman 
14500b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
14510b7c0153SFred Isaman }
14520b7c0153SFred Isaman 
14531f2edbe3STrond Myklebust /*
14541f2edbe3STrond Myklebust  * Special version of should_remove_suid() that ignores capabilities.
14551f2edbe3STrond Myklebust  */
14561f2edbe3STrond Myklebust static int nfs_should_remove_suid(const struct inode *inode)
14571f2edbe3STrond Myklebust {
14581f2edbe3STrond Myklebust 	umode_t mode = inode->i_mode;
14591f2edbe3STrond Myklebust 	int kill = 0;
1460788e7a89STrond Myklebust 
14611f2edbe3STrond Myklebust 	/* suid always must be killed */
14621f2edbe3STrond Myklebust 	if (unlikely(mode & S_ISUID))
14631f2edbe3STrond Myklebust 		kill = ATTR_KILL_SUID;
14641f2edbe3STrond Myklebust 
14651f2edbe3STrond Myklebust 	/*
14661f2edbe3STrond Myklebust 	 * sgid without any exec bits is just a mandatory locking mark; leave
14671f2edbe3STrond Myklebust 	 * it alone.  If some exec bits are set, it's a real sgid; kill it.
14681f2edbe3STrond Myklebust 	 */
14691f2edbe3STrond Myklebust 	if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
14701f2edbe3STrond Myklebust 		kill |= ATTR_KILL_SGID;
14711f2edbe3STrond Myklebust 
14721f2edbe3STrond Myklebust 	if (unlikely(kill && S_ISREG(mode)))
14731f2edbe3STrond Myklebust 		return kill;
14741f2edbe3STrond Myklebust 
14751f2edbe3STrond Myklebust 	return 0;
14761f2edbe3STrond Myklebust }
1477788e7a89STrond Myklebust 
1478a08a8cd3STrond Myklebust static void nfs_writeback_check_extend(struct nfs_pgio_header *hdr,
1479a08a8cd3STrond Myklebust 		struct nfs_fattr *fattr)
1480a08a8cd3STrond Myklebust {
1481a08a8cd3STrond Myklebust 	struct nfs_pgio_args *argp = &hdr->args;
1482a08a8cd3STrond Myklebust 	struct nfs_pgio_res *resp = &hdr->res;
14832b83d3deSTrond Myklebust 	u64 size = argp->offset + resp->count;
1484a08a8cd3STrond Myklebust 
1485a08a8cd3STrond Myklebust 	if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
14862b83d3deSTrond Myklebust 		fattr->size = size;
14872b83d3deSTrond Myklebust 	if (nfs_size_to_loff_t(fattr->size) < i_size_read(hdr->inode)) {
14882b83d3deSTrond Myklebust 		fattr->valid &= ~NFS_ATTR_FATTR_SIZE;
1489a08a8cd3STrond Myklebust 		return;
14902b83d3deSTrond Myklebust 	}
14912b83d3deSTrond Myklebust 	if (size != fattr->size)
1492a08a8cd3STrond Myklebust 		return;
1493a08a8cd3STrond Myklebust 	/* Set attribute barrier */
1494a08a8cd3STrond Myklebust 	nfs_fattr_set_barrier(fattr);
14952b83d3deSTrond Myklebust 	/* ...and update size */
14962b83d3deSTrond Myklebust 	fattr->valid |= NFS_ATTR_FATTR_SIZE;
1497a08a8cd3STrond Myklebust }
1498a08a8cd3STrond Myklebust 
1499a08a8cd3STrond Myklebust void nfs_writeback_update_inode(struct nfs_pgio_header *hdr)
1500a08a8cd3STrond Myklebust {
15012b83d3deSTrond Myklebust 	struct nfs_fattr *fattr = &hdr->fattr;
1502a08a8cd3STrond Myklebust 	struct inode *inode = hdr->inode;
1503a08a8cd3STrond Myklebust 
1504a08a8cd3STrond Myklebust 	spin_lock(&inode->i_lock);
1505a08a8cd3STrond Myklebust 	nfs_writeback_check_extend(hdr, fattr);
1506a08a8cd3STrond Myklebust 	nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
1507a08a8cd3STrond Myklebust 	spin_unlock(&inode->i_lock);
1508a08a8cd3STrond Myklebust }
1509a08a8cd3STrond Myklebust EXPORT_SYMBOL_GPL(nfs_writeback_update_inode);
1510a08a8cd3STrond Myklebust 
15111da177e4SLinus Torvalds /*
15121da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
15131da177e4SLinus Torvalds  */
1514d45f60c6SWeston Andros Adamson static int nfs_writeback_done(struct rpc_task *task,
1515d45f60c6SWeston Andros Adamson 			      struct nfs_pgio_header *hdr,
15160eecb214SAnna Schumaker 			      struct inode *inode)
15171da177e4SLinus Torvalds {
1518788e7a89STrond Myklebust 	int status;
15191da177e4SLinus Torvalds 
1520f551e44fSChuck Lever 	/*
1521f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1522f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1523f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1524f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1525f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1526f551e44fSChuck Lever 	 */
1527d45f60c6SWeston Andros Adamson 	status = NFS_PROTO(inode)->write_done(task, hdr);
1528788e7a89STrond Myklebust 	if (status != 0)
15290eecb214SAnna Schumaker 		return status;
1530d45f60c6SWeston Andros Adamson 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count);
153191d5b470SChuck Lever 
1532d45f60c6SWeston Andros Adamson 	if (hdr->res.verf->committed < hdr->args.stable &&
1533d45f60c6SWeston Andros Adamson 	    task->tk_status >= 0) {
15341da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
15351da177e4SLinus Torvalds 		 * commit data to stable storage even though we
15361da177e4SLinus Torvalds 		 * requested it.
15371da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
15381da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
15391da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
15401da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
15411da177e4SLinus Torvalds 		 */
15421da177e4SLinus Torvalds 		static unsigned long    complain;
15431da177e4SLinus Torvalds 
1544a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
15451da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
15461da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
15471da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1548cd841605SFred Isaman 				NFS_SERVER(inode)->nfs_client->cl_hostname,
1549d45f60c6SWeston Andros Adamson 				hdr->res.verf->committed, hdr->args.stable);
15501da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
15511da177e4SLinus Torvalds 		}
15521da177e4SLinus Torvalds 	}
15531f2edbe3STrond Myklebust 
15541f2edbe3STrond Myklebust 	/* Deal with the suid/sgid bit corner case */
15551f2edbe3STrond Myklebust 	if (nfs_should_remove_suid(inode))
15561f2edbe3STrond Myklebust 		nfs_mark_for_revalidate(inode);
15570eecb214SAnna Schumaker 	return 0;
15580eecb214SAnna Schumaker }
15590eecb214SAnna Schumaker 
15600eecb214SAnna Schumaker /*
15610eecb214SAnna Schumaker  * This function is called when the WRITE call is complete.
15620eecb214SAnna Schumaker  */
1563d45f60c6SWeston Andros Adamson static void nfs_writeback_result(struct rpc_task *task,
1564d45f60c6SWeston Andros Adamson 				 struct nfs_pgio_header *hdr)
15650eecb214SAnna Schumaker {
1566d45f60c6SWeston Andros Adamson 	struct nfs_pgio_args	*argp = &hdr->args;
1567d45f60c6SWeston Andros Adamson 	struct nfs_pgio_res	*resp = &hdr->res;
15681f2edbe3STrond Myklebust 
15691f2edbe3STrond Myklebust 	if (resp->count < argp->count) {
15701da177e4SLinus Torvalds 		static unsigned long    complain;
15711da177e4SLinus Torvalds 
15726c75dc0dSFred Isaman 		/* This a short write! */
1573d45f60c6SWeston Andros Adamson 		nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE);
157491d5b470SChuck Lever 
15751da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
15766c75dc0dSFred Isaman 		if (resp->count == 0) {
15776c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
15786c75dc0dSFred Isaman 				printk(KERN_WARNING
15796c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
15806c75dc0dSFred Isaman 				       argp->count);
15816c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
15826c75dc0dSFred Isaman 			}
1583d45f60c6SWeston Andros Adamson 			nfs_set_pgio_error(hdr, -EIO, argp->offset);
15846c75dc0dSFred Isaman 			task->tk_status = -EIO;
15856c75dc0dSFred Isaman 			return;
15866c75dc0dSFred Isaman 		}
1587f8417b48SKinglong Mee 
1588f8417b48SKinglong Mee 		/* For non rpc-based layout drivers, retry-through-MDS */
1589f8417b48SKinglong Mee 		if (!task->tk_ops) {
1590f8417b48SKinglong Mee 			hdr->pnfs_error = -EAGAIN;
1591f8417b48SKinglong Mee 			return;
1592f8417b48SKinglong Mee 		}
1593f8417b48SKinglong Mee 
15941da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
15951da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
15961da177e4SLinus Torvalds 			/* Resend from where the server left off */
1597d45f60c6SWeston Andros Adamson 			hdr->mds_offset += resp->count;
15981da177e4SLinus Torvalds 			argp->offset += resp->count;
15991da177e4SLinus Torvalds 			argp->pgbase += resp->count;
16001da177e4SLinus Torvalds 			argp->count -= resp->count;
16011da177e4SLinus Torvalds 		} else {
16021da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
16031da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
16041da177e4SLinus Torvalds 			 */
16051da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
16061da177e4SLinus Torvalds 		}
1607d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
16081da177e4SLinus Torvalds 	}
16091da177e4SLinus Torvalds }
16101da177e4SLinus Torvalds 
1611af7cf057STrond Myklebust static int wait_on_commit(struct nfs_mds_commit_info *cinfo)
161271d0a611STrond Myklebust {
1613af7cf057STrond Myklebust 	return wait_on_atomic_t(&cinfo->rpcs_out,
1614af7cf057STrond Myklebust 			nfs_wait_atomic_killable, TASK_KILLABLE);
1615af7cf057STrond Myklebust }
1616af7cf057STrond Myklebust 
1617af7cf057STrond Myklebust static void nfs_commit_begin(struct nfs_mds_commit_info *cinfo)
1618af7cf057STrond Myklebust {
1619af7cf057STrond Myklebust 	atomic_inc(&cinfo->rpcs_out);
1620af7cf057STrond Myklebust }
1621af7cf057STrond Myklebust 
1622af7cf057STrond Myklebust static void nfs_commit_end(struct nfs_mds_commit_info *cinfo)
1623af7cf057STrond Myklebust {
1624af7cf057STrond Myklebust 	if (atomic_dec_and_test(&cinfo->rpcs_out))
1625af7cf057STrond Myklebust 		wake_up_atomic_t(&cinfo->rpcs_out);
162671d0a611STrond Myklebust }
162771d0a611STrond Myklebust 
16280b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
16291da177e4SLinus Torvalds {
16300b7c0153SFred Isaman 	put_nfs_open_context(data->context);
16310b7c0153SFred Isaman 	nfs_commit_free(data);
16321da177e4SLinus Torvalds }
1633e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
16341da177e4SLinus Torvalds 
16350b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
1636c36aae9aSPeng Tao 			const struct nfs_rpc_ops *nfs_ops,
16379ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
16389f0ec176SAndy Adamson 			int how, int flags)
16391da177e4SLinus Torvalds {
164007737691STrond Myklebust 	struct rpc_task *task;
16419ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1642bdc7f021STrond Myklebust 	struct rpc_message msg = {
1643bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1644bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
16459ace33cdSFred Isaman 		.rpc_cred = data->cred,
1646bdc7f021STrond Myklebust 	};
164784115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
164807737691STrond Myklebust 		.task = &data->task,
16499ace33cdSFred Isaman 		.rpc_client = clnt,
1650bdc7f021STrond Myklebust 		.rpc_message = &msg,
16519ace33cdSFred Isaman 		.callback_ops = call_ops,
165284115e1cSTrond Myklebust 		.callback_data = data,
1653101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
16549f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
16553ff7576dSTrond Myklebust 		.priority = priority,
165684115e1cSTrond Myklebust 	};
1657788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
1658c36aae9aSPeng Tao 	nfs_ops->commit_setup(data, &msg);
16591da177e4SLinus Torvalds 
1660b4839ebeSKinglong Mee 	dprintk("NFS: initiated commit call\n");
1661bdc7f021STrond Myklebust 
16628c21c62cSWeston Andros Adamson 	nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client,
16638c21c62cSWeston Andros Adamson 		NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg);
16648c21c62cSWeston Andros Adamson 
166507737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1666dbae4c73STrond Myklebust 	if (IS_ERR(task))
1667dbae4c73STrond Myklebust 		return PTR_ERR(task);
1668d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1669d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
167007737691STrond Myklebust 	rpc_put_task(task);
1671dbae4c73STrond Myklebust 	return 0;
16721da177e4SLinus Torvalds }
1673e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
16741da177e4SLinus Torvalds 
1675378520b8SPeng Tao static loff_t nfs_get_lwb(struct list_head *head)
1676378520b8SPeng Tao {
1677378520b8SPeng Tao 	loff_t lwb = 0;
1678378520b8SPeng Tao 	struct nfs_page *req;
1679378520b8SPeng Tao 
1680378520b8SPeng Tao 	list_for_each_entry(req, head, wb_list)
1681378520b8SPeng Tao 		if (lwb < (req_offset(req) + req->wb_bytes))
1682378520b8SPeng Tao 			lwb = req_offset(req) + req->wb_bytes;
1683378520b8SPeng Tao 
1684378520b8SPeng Tao 	return lwb;
1685378520b8SPeng Tao }
1686378520b8SPeng Tao 
16871da177e4SLinus Torvalds /*
16889ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
16899ace33cdSFred Isaman  */
16900b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1691988b6dceSFred Isaman 		     struct list_head *head,
1692f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1693f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
16949ace33cdSFred Isaman {
16959ace33cdSFred Isaman 	struct nfs_page *first = nfs_list_entry(head->next);
16962b0143b5SDavid Howells 	struct inode *inode = d_inode(first->wb_context->dentry);
16979ace33cdSFred Isaman 
16989ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
16999ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
17009ace33cdSFred Isaman 
17019ace33cdSFred Isaman 	list_splice_init(head, &data->pages);
17029ace33cdSFred Isaman 
17039ace33cdSFred Isaman 	data->inode	  = inode;
17049ace33cdSFred Isaman 	data->cred	  = first->wb_context->cred;
1705988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
1706378520b8SPeng Tao 	/* only set lwb for pnfs commit */
1707378520b8SPeng Tao 	if (lseg)
1708378520b8SPeng Tao 		data->lwb = nfs_get_lwb(&data->pages);
17099ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1710f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1711b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
17129ace33cdSFred Isaman 
17139ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
17149ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
17159ace33cdSFred Isaman 	data->args.offset = 0;
17169ace33cdSFred Isaman 	data->args.count  = 0;
17170b7c0153SFred Isaman 	data->context     = get_nfs_open_context(first->wb_context);
17189ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
17199ace33cdSFred Isaman 	data->res.verf    = &data->verf;
17209ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
17219ace33cdSFred Isaman }
1722e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
17239ace33cdSFred Isaman 
1724e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1725ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1726b57ff130SWeston Andros Adamson 		      struct nfs_commit_info *cinfo,
1727b57ff130SWeston Andros Adamson 		      u32 ds_commit_idx)
172864bfeb49SFred Isaman {
172964bfeb49SFred Isaman 	struct nfs_page *req;
173064bfeb49SFred Isaman 
173164bfeb49SFred Isaman 	while (!list_empty(page_list)) {
173264bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
173364bfeb49SFred Isaman 		nfs_list_remove_request(req);
1734b57ff130SWeston Andros Adamson 		nfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx);
1735487b9b8aSTom Haynes 		if (!cinfo->dreq)
1736487b9b8aSTom Haynes 			nfs_clear_page_commit(req->wb_page);
17371d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
173864bfeb49SFred Isaman 	}
173964bfeb49SFred Isaman }
1740e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
174164bfeb49SFred Isaman 
1742b20135d0STrond Myklebust static void
1743b20135d0STrond Myklebust nfs_commit_resched_write(struct nfs_commit_info *cinfo,
1744b20135d0STrond Myklebust 		struct nfs_page *req)
1745b20135d0STrond Myklebust {
1746b20135d0STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
1747b20135d0STrond Myklebust }
1748b20135d0STrond Myklebust 
17499ace33cdSFred Isaman /*
17501da177e4SLinus Torvalds  * Commit dirty pages
17511da177e4SLinus Torvalds  */
17521da177e4SLinus Torvalds static int
1753ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1754ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
17551da177e4SLinus Torvalds {
17560b7c0153SFred Isaman 	struct nfs_commit_data	*data;
17571da177e4SLinus Torvalds 
1758ade8febdSWeston Andros Adamson 	/* another commit raced with us */
1759ade8febdSWeston Andros Adamson 	if (list_empty(head))
1760ade8febdSWeston Andros Adamson 		return 0;
1761ade8febdSWeston Andros Adamson 
1762518662e0SNeilBrown 	data = nfs_commitdata_alloc(true);
17631da177e4SLinus Torvalds 
17641da177e4SLinus Torvalds 	/* Set up the argument struct */
1765f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
1766f453a54aSFred Isaman 	atomic_inc(&cinfo->mds->rpcs_out);
1767c36aae9aSPeng Tao 	return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode),
1768c36aae9aSPeng Tao 				   data->mds_ops, how, 0);
17691da177e4SLinus Torvalds }
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds /*
17721da177e4SLinus Torvalds  * COMMIT call returned
17731da177e4SLinus Torvalds  */
1774788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
17751da177e4SLinus Torvalds {
17760b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
17771da177e4SLinus Torvalds 
1778a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
17791da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
17801da177e4SLinus Torvalds 
1781788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1782c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
1783c9d8f89dSTrond Myklebust }
1784c9d8f89dSTrond Myklebust 
1785f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1786c9d8f89dSTrond Myklebust {
1787c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1788c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1789f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
1790353db796SNeilBrown 	struct nfs_server *nfss;
1791788e7a89STrond Myklebust 
17921da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
17931da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
17941da177e4SLinus Torvalds 		nfs_list_remove_request(req);
179567911c8fSAnna Schumaker 		if (req->wb_page)
1796d6d6dc7cSFred Isaman 			nfs_clear_page_commit(req->wb_page);
17971da177e4SLinus Torvalds 
17981e8968c5SNiels de Vos 		dprintk("NFS:       commit (%s/%llu %d@%lld)",
17993d4ff43dSAl Viro 			req->wb_context->dentry->d_sb->s_id,
18002b0143b5SDavid Howells 			(unsigned long long)NFS_FILEID(d_inode(req->wb_context->dentry)),
18011da177e4SLinus Torvalds 			req->wb_bytes,
18021da177e4SLinus Torvalds 			(long long)req_offset(req));
1803c9d8f89dSTrond Myklebust 		if (status < 0) {
1804c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
180538a33101SKinglong Mee 			if (req->wb_page)
18061da177e4SLinus Torvalds 				nfs_inode_remove_request(req);
1807ddeaa637SJoe Perches 			dprintk_cont(", error = %d\n", status);
18081da177e4SLinus Torvalds 			goto next;
18091da177e4SLinus Torvalds 		}
18101da177e4SLinus Torvalds 
18111da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
18121da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
18138fc3c386STrond Myklebust 		if (!nfs_write_verifier_cmp(&req->wb_verf, &data->verf.verifier)) {
18141da177e4SLinus Torvalds 			/* We have a match */
181538a33101SKinglong Mee 			if (req->wb_page)
18161da177e4SLinus Torvalds 				nfs_inode_remove_request(req);
1817ddeaa637SJoe Perches 			dprintk_cont(" OK\n");
18181da177e4SLinus Torvalds 			goto next;
18191da177e4SLinus Torvalds 		}
18201da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
1821ddeaa637SJoe Perches 		dprintk_cont(" mismatch\n");
18226d884e8fSFred 		nfs_mark_request_dirty(req);
182305990d1bSTrond Myklebust 		set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
18241da177e4SLinus Torvalds 	next:
18251d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
18261da177e4SLinus Torvalds 	}
1827353db796SNeilBrown 	nfss = NFS_SERVER(data->inode);
1828353db796SNeilBrown 	if (atomic_long_read(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
18290db10944SJan Kara 		clear_bdi_congested(inode_to_bdi(data->inode), BLK_RW_ASYNC);
1830353db796SNeilBrown 
1831f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1832af7cf057STrond Myklebust 	nfs_commit_end(cinfo.mds);
18335917ce84SFred Isaman }
18345917ce84SFred Isaman 
18355917ce84SFred Isaman static void nfs_commit_release(void *calldata)
18365917ce84SFred Isaman {
18370b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
18385917ce84SFred Isaman 
1839f453a54aSFred Isaman 	data->completion_ops->completion(data);
1840c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
18411da177e4SLinus Torvalds }
1842788e7a89STrond Myklebust 
1843788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
18440b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1845788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1846788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1847788e7a89STrond Myklebust };
18481da177e4SLinus Torvalds 
1849f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1850f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1851b20135d0STrond Myklebust 	.resched_write = nfs_commit_resched_write,
1852f453a54aSFred Isaman };
1853f453a54aSFred Isaman 
18541763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1855ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
185684c53ab5SFred Isaman {
185784c53ab5SFred Isaman 	int status;
185884c53ab5SFred Isaman 
1859ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
186084c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1861ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
186284c53ab5SFred Isaman 	return status;
186384c53ab5SFred Isaman }
186484c53ab5SFred Isaman 
1865b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
18661da177e4SLinus Torvalds {
18671da177e4SLinus Torvalds 	LIST_HEAD(head);
1868ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
186971d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1870af7cf057STrond Myklebust 	int error = 0;
1871b8413f98STrond Myklebust 	int res;
18721da177e4SLinus Torvalds 
1873ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1874af7cf057STrond Myklebust 	nfs_commit_begin(cinfo.mds);
1875ea2cf228SFred Isaman 	res = nfs_scan_commit(inode, &head, &cinfo);
1876af7cf057STrond Myklebust 	if (res)
1877ea2cf228SFred Isaman 		error = nfs_generic_commit_list(inode, &head, how, &cinfo);
1878af7cf057STrond Myklebust 	nfs_commit_end(cinfo.mds);
18791da177e4SLinus Torvalds 	if (error < 0)
1880af7cf057STrond Myklebust 		goto out_error;
1881b8413f98STrond Myklebust 	if (!may_wait)
1882b8413f98STrond Myklebust 		goto out_mark_dirty;
1883af7cf057STrond Myklebust 	error = wait_on_commit(cinfo.mds);
1884b8413f98STrond Myklebust 	if (error < 0)
1885b8413f98STrond Myklebust 		return error;
1886c5efa5fcSTrond Myklebust 	return res;
1887af7cf057STrond Myklebust out_error:
1888af7cf057STrond Myklebust 	res = error;
1889c5efa5fcSTrond Myklebust 	/* Note: If we exit without ensuring that the commit is complete,
1890c5efa5fcSTrond Myklebust 	 * we must mark the inode as dirty. Otherwise, future calls to
1891c5efa5fcSTrond Myklebust 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1892c5efa5fcSTrond Myklebust 	 * that the data is on the disk.
1893c5efa5fcSTrond Myklebust 	 */
1894c5efa5fcSTrond Myklebust out_mark_dirty:
1895c5efa5fcSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
18961da177e4SLinus Torvalds 	return res;
18971da177e4SLinus Torvalds }
1898b20135d0STrond Myklebust EXPORT_SYMBOL_GPL(nfs_commit_inode);
18998fc795f7STrond Myklebust 
1900ae09c31fSAnna Schumaker int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
19018fc795f7STrond Myklebust {
1902420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1903420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1904420e3646STrond Myklebust 	int ret = 0;
19058fc795f7STrond Myklebust 
19063236c3e1SJeff Layton 	/* no commits means nothing needs to be done */
1907ea2cf228SFred Isaman 	if (!nfsi->commit_info.ncommit)
19083236c3e1SJeff Layton 		return ret;
19093236c3e1SJeff Layton 
1910a00dd6c0SJeff Layton 	if (wbc->sync_mode == WB_SYNC_NONE) {
1911a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1912a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1913420e3646STrond Myklebust 		 */
19141a4edf0fSTrond Myklebust 		if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1915420e3646STrond Myklebust 			goto out_mark_dirty;
1916420e3646STrond Myklebust 
1917a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1918420e3646STrond Myklebust 		flags = 0;
1919a00dd6c0SJeff Layton 	}
1920a00dd6c0SJeff Layton 
1921420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1922420e3646STrond Myklebust 	if (ret >= 0) {
1923420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1924420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1925420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1926420e3646STrond Myklebust 			else
1927420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1928420e3646STrond Myklebust 		}
19298fc795f7STrond Myklebust 		return 0;
1930420e3646STrond Myklebust 	}
1931420e3646STrond Myklebust out_mark_dirty:
19328fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
19338fc795f7STrond Myklebust 	return ret;
19348fc795f7STrond Myklebust }
193589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode);
1936863a3c6cSAndy Adamson 
1937acdc53b2STrond Myklebust /*
1938837bb1d7STrond Myklebust  * Wrapper for filemap_write_and_wait_range()
1939837bb1d7STrond Myklebust  *
1940837bb1d7STrond Myklebust  * Needed for pNFS in order to ensure data becomes visible to the
1941837bb1d7STrond Myklebust  * client.
1942837bb1d7STrond Myklebust  */
1943837bb1d7STrond Myklebust int nfs_filemap_write_and_wait_range(struct address_space *mapping,
1944837bb1d7STrond Myklebust 		loff_t lstart, loff_t lend)
1945837bb1d7STrond Myklebust {
1946837bb1d7STrond Myklebust 	int ret;
1947837bb1d7STrond Myklebust 
1948837bb1d7STrond Myklebust 	ret = filemap_write_and_wait_range(mapping, lstart, lend);
1949837bb1d7STrond Myklebust 	if (ret == 0)
1950837bb1d7STrond Myklebust 		ret = pnfs_sync_inode(mapping->host, true);
1951837bb1d7STrond Myklebust 	return ret;
1952837bb1d7STrond Myklebust }
1953837bb1d7STrond Myklebust EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range);
1954837bb1d7STrond Myklebust 
1955837bb1d7STrond Myklebust /*
1956acdc53b2STrond Myklebust  * flush the inode to disk.
1957acdc53b2STrond Myklebust  */
1958acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
195934901f70STrond Myklebust {
1960f4ce1299STrond Myklebust 	int ret;
196134901f70STrond Myklebust 
1962f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_enter(inode);
1963f4ce1299STrond Myklebust 
19645bb89b47STrond Myklebust 	ret = filemap_write_and_wait(inode->i_mapping);
19656b196875SChuck Lever 	if (ret)
19666b196875SChuck Lever 		goto out;
19675bb89b47STrond Myklebust 	ret = nfs_commit_inode(inode, FLUSH_SYNC);
19686b196875SChuck Lever 	if (ret < 0)
19696b196875SChuck Lever 		goto out;
19705bb89b47STrond Myklebust 	pnfs_sync_inode(inode, true);
19716b196875SChuck Lever 	ret = 0;
1972f4ce1299STrond Myklebust 
19736b196875SChuck Lever out:
1974f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_exit(inode, ret);
1975f4ce1299STrond Myklebust 	return ret;
19761c75950bSTrond Myklebust }
1977ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all);
19781c75950bSTrond Myklebust 
19791b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
19801b3b4a1aSTrond Myklebust {
19811b3b4a1aSTrond Myklebust 	struct nfs_page *req;
19821b3b4a1aSTrond Myklebust 	int ret = 0;
19831b3b4a1aSTrond Myklebust 
1984ba8b06e6STrond Myklebust 	wait_on_page_writeback(page);
19853e217045SWeston Andros Adamson 
19863e217045SWeston Andros Adamson 	/* blocking call to cancel all requests and join to a single (head)
19873e217045SWeston Andros Adamson 	 * request */
19886d17d653STrond Myklebust 	req = nfs_lock_and_join_requests(page);
19893e217045SWeston Andros Adamson 
19903e217045SWeston Andros Adamson 	if (IS_ERR(req)) {
19913e217045SWeston Andros Adamson 		ret = PTR_ERR(req);
19923e217045SWeston Andros Adamson 	} else if (req) {
19933e217045SWeston Andros Adamson 		/* all requests from this page have been cancelled by
19943e217045SWeston Andros Adamson 		 * nfs_lock_and_join_requests, so just remove the head
19953e217045SWeston Andros Adamson 		 * request from the inode / page_private pointer and
19963e217045SWeston Andros Adamson 		 * release it */
19971b3b4a1aSTrond Myklebust 		nfs_inode_remove_request(req);
19981d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
19991b3b4a1aSTrond Myklebust 	}
20003e217045SWeston Andros Adamson 
20011b3b4a1aSTrond Myklebust 	return ret;
20021b3b4a1aSTrond Myklebust }
20031b3b4a1aSTrond Myklebust 
20041c75950bSTrond Myklebust /*
20051c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
20061c75950bSTrond Myklebust  */
2007c373fff7STrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page)
20081c75950bSTrond Myklebust {
200929418aa4SMel Gorman 	loff_t range_start = page_file_offset(page);
201009cbfeafSKirill A. Shutemov 	loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1);
20117f2f12d9STrond Myklebust 	struct writeback_control wbc = {
20127f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
20137f2f12d9STrond Myklebust 		.nr_to_write = 0,
20147f2f12d9STrond Myklebust 		.range_start = range_start,
20157f2f12d9STrond Myklebust 		.range_end = range_end,
20167f2f12d9STrond Myklebust 	};
20177f2f12d9STrond Myklebust 	int ret;
20187f2f12d9STrond Myklebust 
2019f4ce1299STrond Myklebust 	trace_nfs_writeback_page_enter(inode);
2020f4ce1299STrond Myklebust 
20210522f6adSTrond Myklebust 	for (;;) {
2022ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
20237f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
2024c373fff7STrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
20257f2f12d9STrond Myklebust 			if (ret < 0)
20267f2f12d9STrond Myklebust 				goto out_error;
20270522f6adSTrond Myklebust 			continue;
20287f2f12d9STrond Myklebust 		}
2029f4ce1299STrond Myklebust 		ret = 0;
20300522f6adSTrond Myklebust 		if (!PagePrivate(page))
20310522f6adSTrond Myklebust 			break;
20320522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
20337f2f12d9STrond Myklebust 		if (ret < 0)
20347f2f12d9STrond Myklebust 			goto out_error;
20357f2f12d9STrond Myklebust 	}
20367f2f12d9STrond Myklebust out_error:
2037f4ce1299STrond Myklebust 	trace_nfs_writeback_page_exit(inode, ret);
20387f2f12d9STrond Myklebust 	return ret;
20391c75950bSTrond Myklebust }
20401c75950bSTrond Myklebust 
2041074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
2042074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
2043a6bc32b8SMel Gorman 		struct page *page, enum migrate_mode mode)
2044074cc1deSTrond Myklebust {
20452da95652SJeff Layton 	/*
20462da95652SJeff Layton 	 * If PagePrivate is set, then the page is currently associated with
20472da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
20482da95652SJeff Layton 	 *
20492da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
20502da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
20512da95652SJeff Layton 	 *        the page lock.
20522da95652SJeff Layton 	 */
20532da95652SJeff Layton 	if (PagePrivate(page))
20542da95652SJeff Layton 		return -EBUSY;
2055074cc1deSTrond Myklebust 
20568c209ce7SDavid Howells 	if (!nfs_fscache_release_page(page, GFP_KERNEL))
20578c209ce7SDavid Howells 		return -EBUSY;
2058074cc1deSTrond Myklebust 
2059a6bc32b8SMel Gorman 	return migrate_page(mapping, newpage, page, mode);
2060074cc1deSTrond Myklebust }
2061074cc1deSTrond Myklebust #endif
2062074cc1deSTrond Myklebust 
2063f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
20641da177e4SLinus Torvalds {
20651da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
20661e7f3a48SWeston Andros Adamson 					     sizeof(struct nfs_pgio_header),
20671da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
206820c2df83SPaul Mundt 					     NULL);
20691da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
20701da177e4SLinus Torvalds 		return -ENOMEM;
20711da177e4SLinus Torvalds 
207293d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
20731da177e4SLinus Torvalds 						     nfs_wdata_cachep);
20741da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
20753dd4765fSJeff Layton 		goto out_destroy_write_cache;
20761da177e4SLinus Torvalds 
20770b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
20780b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
20790b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
20800b7c0153SFred Isaman 					     NULL);
20810b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
20823dd4765fSJeff Layton 		goto out_destroy_write_mempool;
20830b7c0153SFred Isaman 
208493d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
20854c100210SYanchuan Nian 						      nfs_cdata_cachep);
20861da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
20873dd4765fSJeff Layton 		goto out_destroy_commit_cache;
20881da177e4SLinus Torvalds 
208989a09141SPeter Zijlstra 	/*
209089a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
209189a09141SPeter Zijlstra 	 *
209289a09141SPeter Zijlstra 	 *  64MB:    8192k
209389a09141SPeter Zijlstra 	 * 128MB:   11585k
209489a09141SPeter Zijlstra 	 * 256MB:   16384k
209589a09141SPeter Zijlstra 	 * 512MB:   23170k
209689a09141SPeter Zijlstra 	 *   1GB:   32768k
209789a09141SPeter Zijlstra 	 *   2GB:   46340k
209889a09141SPeter Zijlstra 	 *   4GB:   65536k
209989a09141SPeter Zijlstra 	 *   8GB:   92681k
210089a09141SPeter Zijlstra 	 *  16GB:  131072k
210189a09141SPeter Zijlstra 	 *
210289a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
210389a09141SPeter Zijlstra 	 * Limit the default to 256M
210489a09141SPeter Zijlstra 	 */
210589a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
210689a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
210789a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
210889a09141SPeter Zijlstra 
21091da177e4SLinus Torvalds 	return 0;
21103dd4765fSJeff Layton 
21113dd4765fSJeff Layton out_destroy_commit_cache:
21123dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
21133dd4765fSJeff Layton out_destroy_write_mempool:
21143dd4765fSJeff Layton 	mempool_destroy(nfs_wdata_mempool);
21153dd4765fSJeff Layton out_destroy_write_cache:
21163dd4765fSJeff Layton 	kmem_cache_destroy(nfs_wdata_cachep);
21173dd4765fSJeff Layton 	return -ENOMEM;
21181da177e4SLinus Torvalds }
21191da177e4SLinus Torvalds 
2120266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
21211da177e4SLinus Torvalds {
21221da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
21233dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
21241da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
21251a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
21261da177e4SLinus Torvalds }
21271da177e4SLinus Torvalds 
21284a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops = {
21294a0de55cSAnna Schumaker 	.rw_alloc_header	= nfs_writehdr_alloc,
21304a0de55cSAnna Schumaker 	.rw_free_header		= nfs_writehdr_free,
21310eecb214SAnna Schumaker 	.rw_done		= nfs_writeback_done,
21320eecb214SAnna Schumaker 	.rw_result		= nfs_writeback_result,
21331ed26f33SAnna Schumaker 	.rw_initiate		= nfs_initiate_write,
21344a0de55cSAnna Schumaker };
2135