xref: /linux/fs/nfs/write.c (revision 8cd797887ae0a73313ba248e027e59c0a597d693)
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 
271da177e4SLinus Torvalds #include <asm/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 
431da177e4SLinus Torvalds /*
441da177e4SLinus Torvalds  * Local function declarations
451da177e4SLinus Torvalds  */
46f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req);
47788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops;
48061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops;
49f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops;
504a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops;
51d4581383SWeston Andros Adamson static void nfs_clear_request_commit(struct nfs_page *req);
5202d1426cSWeston Andros Adamson static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
5302d1426cSWeston Andros Adamson 				      struct inode *inode);
543a3908c8STrond Myklebust static struct nfs_page *
553a3908c8STrond Myklebust nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
563a3908c8STrond Myklebust 						struct page *page);
571da177e4SLinus Torvalds 
58e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep;
593feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
600b7c0153SFred Isaman static struct kmem_cache *nfs_cdata_cachep;
611da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
621da177e4SLinus Torvalds 
630b7c0153SFred Isaman struct nfs_commit_data *nfs_commitdata_alloc(void)
641da177e4SLinus Torvalds {
65192e501bSMel Gorman 	struct nfs_commit_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOIO);
6640859d7eSChuck Lever 
671da177e4SLinus Torvalds 	if (p) {
681da177e4SLinus Torvalds 		memset(p, 0, sizeof(*p));
691da177e4SLinus Torvalds 		INIT_LIST_HEAD(&p->pages);
701da177e4SLinus Torvalds 	}
711da177e4SLinus Torvalds 	return p;
721da177e4SLinus Torvalds }
73e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
741da177e4SLinus Torvalds 
750b7c0153SFred Isaman void nfs_commit_free(struct nfs_commit_data *p)
761da177e4SLinus Torvalds {
771da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
781da177e4SLinus Torvalds }
79e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free);
801da177e4SLinus Torvalds 
811e7f3a48SWeston Andros Adamson static struct nfs_pgio_header *nfs_writehdr_alloc(void)
823feb2d49STrond Myklebust {
831e7f3a48SWeston Andros Adamson 	struct nfs_pgio_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOIO);
843feb2d49STrond Myklebust 
854a0de55cSAnna Schumaker 	if (p)
863feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
873feb2d49STrond Myklebust 	return p;
883feb2d49STrond Myklebust }
893feb2d49STrond Myklebust 
901e7f3a48SWeston Andros Adamson static void nfs_writehdr_free(struct nfs_pgio_header *hdr)
916c75dc0dSFred Isaman {
921e7f3a48SWeston Andros Adamson 	mempool_free(hdr, nfs_wdata_mempool);
933feb2d49STrond Myklebust }
941da177e4SLinus Torvalds 
957b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
967b159fc1STrond Myklebust {
977b159fc1STrond Myklebust 	ctx->error = error;
987b159fc1STrond Myklebust 	smp_wmb();
997b159fc1STrond Myklebust 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
1007b159fc1STrond Myklebust }
1017b159fc1STrond Myklebust 
10284d3a9a9SWeston Andros Adamson /*
10384d3a9a9SWeston Andros Adamson  * nfs_page_find_head_request_locked - find head request associated with @page
10484d3a9a9SWeston Andros Adamson  *
10584d3a9a9SWeston Andros Adamson  * must be called while holding the inode lock.
10684d3a9a9SWeston Andros Adamson  *
10784d3a9a9SWeston Andros Adamson  * returns matching head request with reference held, or NULL if not found.
10884d3a9a9SWeston Andros Adamson  */
10929418aa4SMel Gorman static struct nfs_page *
11084d3a9a9SWeston Andros Adamson nfs_page_find_head_request_locked(struct nfs_inode *nfsi, struct page *page)
111277459d2STrond Myklebust {
112277459d2STrond Myklebust 	struct nfs_page *req = NULL;
113277459d2STrond Myklebust 
11429418aa4SMel Gorman 	if (PagePrivate(page))
115277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
11602d1426cSWeston Andros Adamson 	else if (unlikely(PageSwapCache(page)))
11702d1426cSWeston Andros Adamson 		req = nfs_page_search_commits_for_head_request_locked(nfsi,
11802d1426cSWeston Andros Adamson 			page);
11929418aa4SMel Gorman 
12084d3a9a9SWeston Andros Adamson 	if (req) {
12184d3a9a9SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_head != req);
12229418aa4SMel Gorman 		kref_get(&req->wb_kref);
12384d3a9a9SWeston Andros Adamson 	}
12429418aa4SMel Gorman 
125277459d2STrond Myklebust 	return req;
126277459d2STrond Myklebust }
127277459d2STrond Myklebust 
12884d3a9a9SWeston Andros Adamson /*
12984d3a9a9SWeston Andros Adamson  * nfs_page_find_head_request - find head request associated with @page
13084d3a9a9SWeston Andros Adamson  *
13184d3a9a9SWeston Andros Adamson  * returns matching head request with reference held, or NULL if not found.
13284d3a9a9SWeston Andros Adamson  */
13384d3a9a9SWeston Andros Adamson static struct nfs_page *nfs_page_find_head_request(struct page *page)
134277459d2STrond Myklebust {
135d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
136277459d2STrond Myklebust 	struct nfs_page *req = NULL;
137277459d2STrond Myklebust 
138587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
13984d3a9a9SWeston Andros Adamson 	req = nfs_page_find_head_request_locked(NFS_I(inode), page);
140587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
141277459d2STrond Myklebust 	return req;
142277459d2STrond Myklebust }
143277459d2STrond Myklebust 
1441da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1451da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1461da177e4SLinus Torvalds {
147d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
148a3d01454STrond Myklebust 	loff_t end, i_size;
149a3d01454STrond Myklebust 	pgoff_t end_index;
1501da177e4SLinus Torvalds 
151a3d01454STrond Myklebust 	spin_lock(&inode->i_lock);
152a3d01454STrond Myklebust 	i_size = i_size_read(inode);
15309cbfeafSKirill A. Shutemov 	end_index = (i_size - 1) >> PAGE_SHIFT;
154*8cd79788SHuang Ying 	if (i_size > 0 && page_index(page) < end_index)
155a3d01454STrond Myklebust 		goto out;
156d56b4ddfSMel Gorman 	end = page_file_offset(page) + ((loff_t)offset+count);
1571da177e4SLinus Torvalds 	if (i_size >= end)
158a3d01454STrond Myklebust 		goto out;
1591da177e4SLinus Torvalds 	i_size_write(inode, end);
160a3d01454STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
161a3d01454STrond Myklebust out:
162a3d01454STrond Myklebust 	spin_unlock(&inode->i_lock);
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds 
165a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
166a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
167a301b777STrond Myklebust {
168d56b4ddfSMel Gorman 	nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page));
169a301b777STrond Myklebust }
170a301b777STrond Myklebust 
171d72ddcbaSWeston Andros Adamson /*
172d72ddcbaSWeston Andros Adamson  * nfs_page_group_search_locked
173d72ddcbaSWeston Andros Adamson  * @head - head request of page group
174d72ddcbaSWeston Andros Adamson  * @page_offset - offset into page
175d72ddcbaSWeston Andros Adamson  *
176d72ddcbaSWeston Andros Adamson  * Search page group with head @head to find a request that contains the
177d72ddcbaSWeston Andros Adamson  * page offset @page_offset.
178d72ddcbaSWeston Andros Adamson  *
179d72ddcbaSWeston Andros Adamson  * Returns a pointer to the first matching nfs request, or NULL if no
180d72ddcbaSWeston Andros Adamson  * match is found.
181d72ddcbaSWeston Andros Adamson  *
182d72ddcbaSWeston Andros Adamson  * Must be called with the page group lock held
183d72ddcbaSWeston Andros Adamson  */
184d72ddcbaSWeston Andros Adamson static struct nfs_page *
185d72ddcbaSWeston Andros Adamson nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset)
186d72ddcbaSWeston Andros Adamson {
187d72ddcbaSWeston Andros Adamson 	struct nfs_page *req;
188d72ddcbaSWeston Andros Adamson 
189d72ddcbaSWeston Andros Adamson 	WARN_ON_ONCE(head != head->wb_head);
190d72ddcbaSWeston Andros Adamson 	WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_head->wb_flags));
191d72ddcbaSWeston Andros Adamson 
192d72ddcbaSWeston Andros Adamson 	req = head;
193d72ddcbaSWeston Andros Adamson 	do {
194d72ddcbaSWeston Andros Adamson 		if (page_offset >= req->wb_pgbase &&
195d72ddcbaSWeston Andros Adamson 		    page_offset < (req->wb_pgbase + req->wb_bytes))
196d72ddcbaSWeston Andros Adamson 			return req;
197d72ddcbaSWeston Andros Adamson 
198d72ddcbaSWeston Andros Adamson 		req = req->wb_this_page;
199d72ddcbaSWeston Andros Adamson 	} while (req != head);
200d72ddcbaSWeston Andros Adamson 
201d72ddcbaSWeston Andros Adamson 	return NULL;
202d72ddcbaSWeston Andros Adamson }
203d72ddcbaSWeston Andros Adamson 
204d72ddcbaSWeston Andros Adamson /*
205d72ddcbaSWeston Andros Adamson  * nfs_page_group_covers_page
206d72ddcbaSWeston Andros Adamson  * @head - head request of page group
207d72ddcbaSWeston Andros Adamson  *
208d72ddcbaSWeston Andros Adamson  * Return true if the page group with head @head covers the whole page,
209d72ddcbaSWeston Andros Adamson  * returns false otherwise
210d72ddcbaSWeston Andros Adamson  */
211d72ddcbaSWeston Andros Adamson static bool nfs_page_group_covers_page(struct nfs_page *req)
212d72ddcbaSWeston Andros Adamson {
213d72ddcbaSWeston Andros Adamson 	struct nfs_page *tmp;
214d72ddcbaSWeston Andros Adamson 	unsigned int pos = 0;
215d72ddcbaSWeston Andros Adamson 	unsigned int len = nfs_page_length(req->wb_page);
216d72ddcbaSWeston Andros Adamson 
217fd2f3a06SWeston Andros Adamson 	nfs_page_group_lock(req, false);
218d72ddcbaSWeston Andros Adamson 
219d72ddcbaSWeston Andros Adamson 	do {
220d72ddcbaSWeston Andros Adamson 		tmp = nfs_page_group_search_locked(req->wb_head, pos);
221d72ddcbaSWeston Andros Adamson 		if (tmp) {
222d72ddcbaSWeston Andros Adamson 			/* no way this should happen */
223d72ddcbaSWeston Andros Adamson 			WARN_ON_ONCE(tmp->wb_pgbase != pos);
224d72ddcbaSWeston Andros Adamson 			pos += tmp->wb_bytes - (pos - tmp->wb_pgbase);
225d72ddcbaSWeston Andros Adamson 		}
226d72ddcbaSWeston Andros Adamson 	} while (tmp && pos < len);
227d72ddcbaSWeston Andros Adamson 
228d72ddcbaSWeston Andros Adamson 	nfs_page_group_unlock(req);
229d72ddcbaSWeston Andros Adamson 	WARN_ON_ONCE(pos > len);
230d72ddcbaSWeston Andros Adamson 	return pos == len;
231d72ddcbaSWeston Andros Adamson }
232d72ddcbaSWeston Andros Adamson 
2331da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
2341da177e4SLinus Torvalds  * covers the full page.
2351da177e4SLinus Torvalds  */
236d72ddcbaSWeston Andros Adamson static void nfs_mark_uptodate(struct nfs_page *req)
2371da177e4SLinus Torvalds {
238d72ddcbaSWeston Andros Adamson 	if (PageUptodate(req->wb_page))
2391da177e4SLinus Torvalds 		return;
240d72ddcbaSWeston Andros Adamson 	if (!nfs_page_group_covers_page(req))
2411da177e4SLinus Torvalds 		return;
242d72ddcbaSWeston Andros Adamson 	SetPageUptodate(req->wb_page);
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
2461da177e4SLinus Torvalds {
247e87b4c7aSNeilBrown 	int ret = 0;
248cca588d6STrond Myklebust 
249e87b4c7aSNeilBrown 	if (wbc->sync_mode == WB_SYNC_ALL)
250e87b4c7aSNeilBrown 		ret = FLUSH_COND_STABLE;
251e87b4c7aSNeilBrown 	return ret;
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds /*
25589a09141SPeter Zijlstra  * NFS congestion control
25689a09141SPeter Zijlstra  */
25789a09141SPeter Zijlstra 
25889a09141SPeter Zijlstra int nfs_congestion_kb;
25989a09141SPeter Zijlstra 
26089a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
26189a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
26289a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
26389a09141SPeter Zijlstra 
264deed85e7STrond Myklebust static void nfs_set_page_writeback(struct page *page)
26589a09141SPeter Zijlstra {
266deed85e7STrond Myklebust 	struct nfs_server *nfss = NFS_SERVER(page_file_mapping(page)->host);
2675a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
2685a6d41b3STrond Myklebust 
269deed85e7STrond Myklebust 	WARN_ON_ONCE(ret != 0);
27089a09141SPeter Zijlstra 
271277866a0SPeter Zijlstra 	if (atomic_long_inc_return(&nfss->writeback) >
2728aa7e847SJens Axboe 			NFS_CONGESTION_ON_THRESH) {
2738aa7e847SJens Axboe 		set_bdi_congested(&nfss->backing_dev_info,
2748aa7e847SJens Axboe 					BLK_RW_ASYNC);
2758aa7e847SJens Axboe 	}
27689a09141SPeter Zijlstra }
27789a09141SPeter Zijlstra 
27820633f04SWeston Andros Adamson static void nfs_end_page_writeback(struct nfs_page *req)
27989a09141SPeter Zijlstra {
28020633f04SWeston Andros Adamson 	struct inode *inode = page_file_mapping(req->wb_page)->host;
28189a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
28289a09141SPeter Zijlstra 
28320633f04SWeston Andros Adamson 	if (!nfs_page_group_sync_on_bit(req, PG_WB_END))
28420633f04SWeston Andros Adamson 		return;
28520633f04SWeston Andros Adamson 
28620633f04SWeston Andros Adamson 	end_page_writeback(req->wb_page);
287c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
2888aa7e847SJens Axboe 		clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
28989a09141SPeter Zijlstra }
29089a09141SPeter Zijlstra 
291d4581383SWeston Andros Adamson 
292d4581383SWeston Andros Adamson /* nfs_page_group_clear_bits
293d4581383SWeston Andros Adamson  *   @req - an nfs request
294d4581383SWeston Andros Adamson  * clears all page group related bits from @req
295d4581383SWeston Andros Adamson  */
296d4581383SWeston Andros Adamson static void
297d4581383SWeston Andros Adamson nfs_page_group_clear_bits(struct nfs_page *req)
298e261f51fSTrond Myklebust {
299d4581383SWeston Andros Adamson 	clear_bit(PG_TEARDOWN, &req->wb_flags);
300d4581383SWeston Andros Adamson 	clear_bit(PG_UNLOCKPAGE, &req->wb_flags);
301d4581383SWeston Andros Adamson 	clear_bit(PG_UPTODATE, &req->wb_flags);
302d4581383SWeston Andros Adamson 	clear_bit(PG_WB_END, &req->wb_flags);
303d4581383SWeston Andros Adamson 	clear_bit(PG_REMOVE, &req->wb_flags);
304d4581383SWeston Andros Adamson }
305d4581383SWeston Andros Adamson 
306d4581383SWeston Andros Adamson 
307d4581383SWeston Andros Adamson /*
308d4581383SWeston Andros Adamson  * nfs_unroll_locks_and_wait -  unlock all newly locked reqs and wait on @req
309d4581383SWeston Andros Adamson  *
310d4581383SWeston Andros Adamson  * this is a helper function for nfs_lock_and_join_requests
311d4581383SWeston Andros Adamson  *
312d4581383SWeston Andros Adamson  * @inode - inode associated with request page group, must be holding inode lock
313d4581383SWeston Andros Adamson  * @head  - head request of page group, must be holding head lock
314d4581383SWeston Andros Adamson  * @req   - request that couldn't lock and needs to wait on the req bit lock
315d4581383SWeston Andros Adamson  * @nonblock - if true, don't actually wait
316d4581383SWeston Andros Adamson  *
317d4581383SWeston Andros Adamson  * NOTE: this must be called holding page_group bit lock and inode spin lock
318d4581383SWeston Andros Adamson  *       and BOTH will be released before returning.
319d4581383SWeston Andros Adamson  *
320d4581383SWeston Andros Adamson  * returns 0 on success, < 0 on error.
321d4581383SWeston Andros Adamson  */
322d4581383SWeston Andros Adamson static int
323d4581383SWeston Andros Adamson nfs_unroll_locks_and_wait(struct inode *inode, struct nfs_page *head,
324d4581383SWeston Andros Adamson 			  struct nfs_page *req, bool nonblock)
325d4581383SWeston Andros Adamson 	__releases(&inode->i_lock)
326d4581383SWeston Andros Adamson {
327d4581383SWeston Andros Adamson 	struct nfs_page *tmp;
328e261f51fSTrond Myklebust 	int ret;
329e261f51fSTrond Myklebust 
330d4581383SWeston Andros Adamson 	/* relinquish all the locks successfully grabbed this run */
331d4581383SWeston Andros Adamson 	for (tmp = head ; tmp != req; tmp = tmp->wb_this_page)
332d4581383SWeston Andros Adamson 		nfs_unlock_request(tmp);
333d4581383SWeston Andros Adamson 
334d4581383SWeston Andros Adamson 	WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
335d4581383SWeston Andros Adamson 
336d4581383SWeston Andros Adamson 	/* grab a ref on the request that will be waited on */
337d4581383SWeston Andros Adamson 	kref_get(&req->wb_kref);
338d4581383SWeston Andros Adamson 
339d4581383SWeston Andros Adamson 	nfs_page_group_unlock(head);
340587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
341d4581383SWeston Andros Adamson 
342d4581383SWeston Andros Adamson 	/* release ref from nfs_page_find_head_request_locked */
343d4581383SWeston Andros Adamson 	nfs_release_request(head);
344d4581383SWeston Andros Adamson 
345cfb506e1STrond Myklebust 	if (!nonblock)
346e261f51fSTrond Myklebust 		ret = nfs_wait_on_request(req);
347cfb506e1STrond Myklebust 	else
348cfb506e1STrond Myklebust 		ret = -EAGAIN;
349e261f51fSTrond Myklebust 	nfs_release_request(req);
350d4581383SWeston Andros Adamson 
351d4581383SWeston Andros Adamson 	return ret;
352e261f51fSTrond Myklebust }
353d4581383SWeston Andros Adamson 
354d4581383SWeston Andros Adamson /*
355d4581383SWeston Andros Adamson  * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests
356d4581383SWeston Andros Adamson  *
357d4581383SWeston Andros Adamson  * @destroy_list - request list (using wb_this_page) terminated by @old_head
358d4581383SWeston Andros Adamson  * @old_head - the old head of the list
359d4581383SWeston Andros Adamson  *
360d4581383SWeston Andros Adamson  * All subrequests must be locked and removed from all lists, so at this point
361d4581383SWeston Andros Adamson  * they are only "active" in this function, and possibly in nfs_wait_on_request
362d4581383SWeston Andros Adamson  * with a reference held by some other context.
363d4581383SWeston Andros Adamson  */
364d4581383SWeston Andros Adamson static void
365d4581383SWeston Andros Adamson nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list,
366d4581383SWeston Andros Adamson 				 struct nfs_page *old_head)
367d4581383SWeston Andros Adamson {
368d4581383SWeston Andros Adamson 	while (destroy_list) {
369d4581383SWeston Andros Adamson 		struct nfs_page *subreq = destroy_list;
370d4581383SWeston Andros Adamson 
371d4581383SWeston Andros Adamson 		destroy_list = (subreq->wb_this_page == old_head) ?
372d4581383SWeston Andros Adamson 				   NULL : subreq->wb_this_page;
373d4581383SWeston Andros Adamson 
374d4581383SWeston Andros Adamson 		WARN_ON_ONCE(old_head != subreq->wb_head);
375d4581383SWeston Andros Adamson 
376d4581383SWeston Andros Adamson 		/* make sure old group is not used */
377d4581383SWeston Andros Adamson 		subreq->wb_head = subreq;
378d4581383SWeston Andros Adamson 		subreq->wb_this_page = subreq;
379d4581383SWeston Andros Adamson 
380d4581383SWeston Andros Adamson 		/* subreq is now totally disconnected from page group or any
381d4581383SWeston Andros Adamson 		 * write / commit lists. last chance to wake any waiters */
382d4581383SWeston Andros Adamson 		nfs_unlock_request(subreq);
383d4581383SWeston Andros Adamson 
384d4581383SWeston Andros Adamson 		if (!test_bit(PG_TEARDOWN, &subreq->wb_flags)) {
385d4581383SWeston Andros Adamson 			/* release ref on old head request */
386d4581383SWeston Andros Adamson 			nfs_release_request(old_head);
387d4581383SWeston Andros Adamson 
388d4581383SWeston Andros Adamson 			nfs_page_group_clear_bits(subreq);
389d4581383SWeston Andros Adamson 
390d4581383SWeston Andros Adamson 			/* release the PG_INODE_REF reference */
391d4581383SWeston Andros Adamson 			if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags))
392d4581383SWeston Andros Adamson 				nfs_release_request(subreq);
393d4581383SWeston Andros Adamson 			else
394d4581383SWeston Andros Adamson 				WARN_ON_ONCE(1);
395d4581383SWeston Andros Adamson 		} else {
396d4581383SWeston Andros Adamson 			WARN_ON_ONCE(test_bit(PG_CLEAN, &subreq->wb_flags));
397d4581383SWeston Andros Adamson 			/* zombie requests have already released the last
398d4581383SWeston Andros Adamson 			 * reference and were waiting on the rest of the
399d4581383SWeston Andros Adamson 			 * group to complete. Since it's no longer part of a
400d4581383SWeston Andros Adamson 			 * group, simply free the request */
401d4581383SWeston Andros Adamson 			nfs_page_group_clear_bits(subreq);
402d4581383SWeston Andros Adamson 			nfs_free_request(subreq);
403d4581383SWeston Andros Adamson 		}
404d4581383SWeston Andros Adamson 	}
405d4581383SWeston Andros Adamson }
406d4581383SWeston Andros Adamson 
407d4581383SWeston Andros Adamson /*
408d4581383SWeston Andros Adamson  * nfs_lock_and_join_requests - join all subreqs to the head req and return
409d4581383SWeston Andros Adamson  *                              a locked reference, cancelling any pending
410d4581383SWeston Andros Adamson  *                              operations for this page.
411d4581383SWeston Andros Adamson  *
412d4581383SWeston Andros Adamson  * @page - the page used to lookup the "page group" of nfs_page structures
413d4581383SWeston Andros Adamson  * @nonblock - if true, don't block waiting for request locks
414d4581383SWeston Andros Adamson  *
415d4581383SWeston Andros Adamson  * This function joins all sub requests to the head request by first
416d4581383SWeston Andros Adamson  * locking all requests in the group, cancelling any pending operations
417d4581383SWeston Andros Adamson  * and finally updating the head request to cover the whole range covered by
418d4581383SWeston Andros Adamson  * the (former) group.  All subrequests are removed from any write or commit
419d4581383SWeston Andros Adamson  * lists, unlinked from the group and destroyed.
420d4581383SWeston Andros Adamson  *
421d4581383SWeston Andros Adamson  * Returns a locked, referenced pointer to the head request - which after
422d4581383SWeston Andros Adamson  * this call is guaranteed to be the only request associated with the page.
423d4581383SWeston Andros Adamson  * Returns NULL if no requests are found for @page, or a ERR_PTR if an
424d4581383SWeston Andros Adamson  * error was encountered.
425d4581383SWeston Andros Adamson  */
426d4581383SWeston Andros Adamson static struct nfs_page *
427d4581383SWeston Andros Adamson nfs_lock_and_join_requests(struct page *page, bool nonblock)
428d4581383SWeston Andros Adamson {
429d4581383SWeston Andros Adamson 	struct inode *inode = page_file_mapping(page)->host;
430d4581383SWeston Andros Adamson 	struct nfs_page *head, *subreq;
431d4581383SWeston Andros Adamson 	struct nfs_page *destroy_list = NULL;
432d4581383SWeston Andros Adamson 	unsigned int total_bytes;
433d4581383SWeston Andros Adamson 	int ret;
434d4581383SWeston Andros Adamson 
435d4581383SWeston Andros Adamson try_again:
436d4581383SWeston Andros Adamson 	total_bytes = 0;
437d4581383SWeston Andros Adamson 
438d4581383SWeston Andros Adamson 	WARN_ON_ONCE(destroy_list);
439d4581383SWeston Andros Adamson 
440d4581383SWeston Andros Adamson 	spin_lock(&inode->i_lock);
441d4581383SWeston Andros Adamson 
442d4581383SWeston Andros Adamson 	/*
443d4581383SWeston Andros Adamson 	 * A reference is taken only on the head request which acts as a
444d4581383SWeston Andros Adamson 	 * reference to the whole page group - the group will not be destroyed
445d4581383SWeston Andros Adamson 	 * until the head reference is released.
446d4581383SWeston Andros Adamson 	 */
447d4581383SWeston Andros Adamson 	head = nfs_page_find_head_request_locked(NFS_I(inode), page);
448d4581383SWeston Andros Adamson 
449d4581383SWeston Andros Adamson 	if (!head) {
450587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
451d4581383SWeston Andros Adamson 		return NULL;
452d4581383SWeston Andros Adamson 	}
453d4581383SWeston Andros Adamson 
4547c3af975SWeston Andros Adamson 	/* holding inode lock, so always make a non-blocking call to try the
4557c3af975SWeston Andros Adamson 	 * page group lock */
456fd2f3a06SWeston Andros Adamson 	ret = nfs_page_group_lock(head, true);
45794970014SWeston Andros Adamson 	if (ret < 0) {
45894970014SWeston Andros Adamson 		spin_unlock(&inode->i_lock);
4597c3af975SWeston Andros Adamson 
4607c3af975SWeston Andros Adamson 		if (!nonblock && ret == -EAGAIN) {
4617c3af975SWeston Andros Adamson 			nfs_page_group_lock_wait(head);
4627c3af975SWeston Andros Adamson 			nfs_release_request(head);
4637c3af975SWeston Andros Adamson 			goto try_again;
4647c3af975SWeston Andros Adamson 		}
4657c3af975SWeston Andros Adamson 
46694970014SWeston Andros Adamson 		nfs_release_request(head);
467e7029206SWeston Andros Adamson 		return ERR_PTR(ret);
46894970014SWeston Andros Adamson 	}
4697c3af975SWeston Andros Adamson 
4707c3af975SWeston Andros Adamson 	/* lock each request in the page group */
471d4581383SWeston Andros Adamson 	subreq = head;
472d4581383SWeston Andros Adamson 	do {
473d4581383SWeston Andros Adamson 		/*
474d4581383SWeston Andros Adamson 		 * Subrequests are always contiguous, non overlapping
475309a1d65SWeston Andros Adamson 		 * and in order - but may be repeated (mirrored writes).
476d4581383SWeston Andros Adamson 		 */
477309a1d65SWeston Andros Adamson 		if (subreq->wb_offset == (head->wb_offset + total_bytes)) {
478d4581383SWeston Andros Adamson 			/* keep track of how many bytes this group covers */
479d4581383SWeston Andros Adamson 			total_bytes += subreq->wb_bytes;
480309a1d65SWeston Andros Adamson 		} else if (WARN_ON_ONCE(subreq->wb_offset < head->wb_offset ||
481309a1d65SWeston Andros Adamson 			    ((subreq->wb_offset + subreq->wb_bytes) >
482309a1d65SWeston Andros Adamson 			     (head->wb_offset + total_bytes)))) {
483309a1d65SWeston Andros Adamson 			nfs_page_group_unlock(head);
484309a1d65SWeston Andros Adamson 			spin_unlock(&inode->i_lock);
485309a1d65SWeston Andros Adamson 			return ERR_PTR(-EIO);
486309a1d65SWeston Andros Adamson 		}
487d4581383SWeston Andros Adamson 
488d4581383SWeston Andros Adamson 		if (!nfs_lock_request(subreq)) {
489d4581383SWeston Andros Adamson 			/* releases page group bit lock and
490d4581383SWeston Andros Adamson 			 * inode spin lock and all references */
491d4581383SWeston Andros Adamson 			ret = nfs_unroll_locks_and_wait(inode, head,
492d4581383SWeston Andros Adamson 				subreq, nonblock);
493d4581383SWeston Andros Adamson 
494d4581383SWeston Andros Adamson 			if (ret == 0)
495d4581383SWeston Andros Adamson 				goto try_again;
496d4581383SWeston Andros Adamson 
497d4581383SWeston Andros Adamson 			return ERR_PTR(ret);
498d4581383SWeston Andros Adamson 		}
499d4581383SWeston Andros Adamson 
500d4581383SWeston Andros Adamson 		subreq = subreq->wb_this_page;
501d4581383SWeston Andros Adamson 	} while (subreq != head);
502d4581383SWeston Andros Adamson 
503d4581383SWeston Andros Adamson 	/* Now that all requests are locked, make sure they aren't on any list.
504d4581383SWeston Andros Adamson 	 * Commit list removal accounting is done after locks are dropped */
505d4581383SWeston Andros Adamson 	subreq = head;
506d4581383SWeston Andros Adamson 	do {
507411a99adSWeston Andros Adamson 		nfs_clear_request_commit(subreq);
508d4581383SWeston Andros Adamson 		subreq = subreq->wb_this_page;
509d4581383SWeston Andros Adamson 	} while (subreq != head);
510d4581383SWeston Andros Adamson 
511d4581383SWeston Andros Adamson 	/* unlink subrequests from head, destroy them later */
512d4581383SWeston Andros Adamson 	if (head->wb_this_page != head) {
513d4581383SWeston Andros Adamson 		/* destroy list will be terminated by head */
514d4581383SWeston Andros Adamson 		destroy_list = head->wb_this_page;
515d4581383SWeston Andros Adamson 		head->wb_this_page = head;
516d4581383SWeston Andros Adamson 
517d4581383SWeston Andros Adamson 		/* change head request to cover whole range that
518d4581383SWeston Andros Adamson 		 * the former page group covered */
519d4581383SWeston Andros Adamson 		head->wb_bytes = total_bytes;
520d4581383SWeston Andros Adamson 	}
521d4581383SWeston Andros Adamson 
522d4581383SWeston Andros Adamson 	/*
523d4581383SWeston Andros Adamson 	 * prepare head request to be added to new pgio descriptor
524d4581383SWeston Andros Adamson 	 */
525d4581383SWeston Andros Adamson 	nfs_page_group_clear_bits(head);
526d4581383SWeston Andros Adamson 
527d4581383SWeston Andros Adamson 	/*
528d4581383SWeston Andros Adamson 	 * some part of the group was still on the inode list - otherwise
529d4581383SWeston Andros Adamson 	 * the group wouldn't be involved in async write.
530d4581383SWeston Andros Adamson 	 * grab a reference for the head request, iff it needs one.
531d4581383SWeston Andros Adamson 	 */
532d4581383SWeston Andros Adamson 	if (!test_and_set_bit(PG_INODE_REF, &head->wb_flags))
533d4581383SWeston Andros Adamson 		kref_get(&head->wb_kref);
534d4581383SWeston Andros Adamson 
535d4581383SWeston Andros Adamson 	nfs_page_group_unlock(head);
536d4581383SWeston Andros Adamson 
537411a99adSWeston Andros Adamson 	/* drop lock to clean uprequests on destroy list */
538d4581383SWeston Andros Adamson 	spin_unlock(&inode->i_lock);
539d4581383SWeston Andros Adamson 
540d4581383SWeston Andros Adamson 	nfs_destroy_unlinked_subrequests(destroy_list, head);
541d4581383SWeston Andros Adamson 
542d4581383SWeston Andros Adamson 	/* still holds ref on head from nfs_page_find_head_request_locked
543d4581383SWeston Andros Adamson 	 * and still has lock on head from lock loop */
544d4581383SWeston Andros Adamson 	return head;
545612c9384STrond Myklebust }
546074cc1deSTrond Myklebust 
5470bcbf039SPeng Tao static void nfs_write_error_remove_page(struct nfs_page *req)
5480bcbf039SPeng Tao {
5490bcbf039SPeng Tao 	nfs_unlock_request(req);
5500bcbf039SPeng Tao 	nfs_end_page_writeback(req);
5510bcbf039SPeng Tao 	nfs_release_request(req);
5520bcbf039SPeng Tao 	generic_error_remove_page(page_file_mapping(req->wb_page),
5530bcbf039SPeng Tao 				  req->wb_page);
5540bcbf039SPeng Tao }
5550bcbf039SPeng Tao 
556074cc1deSTrond Myklebust /*
557074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
558074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
559074cc1deSTrond Myklebust  */
560074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
561d6c843b9SPeng Tao 				struct page *page, bool nonblock,
562d6c843b9SPeng Tao 				bool launder)
563074cc1deSTrond Myklebust {
564074cc1deSTrond Myklebust 	struct nfs_page *req;
565074cc1deSTrond Myklebust 	int ret = 0;
566074cc1deSTrond Myklebust 
567d4581383SWeston Andros Adamson 	req = nfs_lock_and_join_requests(page, nonblock);
568074cc1deSTrond Myklebust 	if (!req)
569074cc1deSTrond Myklebust 		goto out;
570074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
571074cc1deSTrond Myklebust 	if (IS_ERR(req))
572074cc1deSTrond Myklebust 		goto out;
573074cc1deSTrond Myklebust 
574deed85e7STrond Myklebust 	nfs_set_page_writeback(page);
575deed85e7STrond Myklebust 	WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
576074cc1deSTrond Myklebust 
577deed85e7STrond Myklebust 	ret = 0;
578f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
579074cc1deSTrond Myklebust 		ret = pgio->pg_error;
5800bcbf039SPeng Tao 		/*
581d6c843b9SPeng Tao 		 * Remove the problematic req upon fatal errors
582d6c843b9SPeng Tao 		 * in launder case, while other dirty pages can
583d6c843b9SPeng Tao 		 * still be around until they get flushed.
5840bcbf039SPeng Tao 		 */
5850bcbf039SPeng Tao 		if (nfs_error_is_fatal(ret)) {
5860bcbf039SPeng Tao 			nfs_context_set_write_error(req->wb_context, ret);
587d6c843b9SPeng Tao 			if (launder) {
5880bcbf039SPeng Tao 				nfs_write_error_remove_page(req);
589d6c843b9SPeng Tao 				goto out;
590d6c843b9SPeng Tao 			}
591d6c843b9SPeng Tao 		}
5920bcbf039SPeng Tao 		nfs_redirty_request(req);
5930bcbf039SPeng Tao 		ret = -EAGAIN;
59440f90271STrond Myklebust 	} else
59540f90271STrond Myklebust 		nfs_add_stats(page_file_mapping(page)->host,
59640f90271STrond Myklebust 				NFSIOS_WRITEPAGES, 1);
597074cc1deSTrond Myklebust out:
598074cc1deSTrond Myklebust 	return ret;
599e261f51fSTrond Myklebust }
600e261f51fSTrond Myklebust 
601d6c843b9SPeng Tao static int nfs_do_writepage(struct page *page, struct writeback_control *wbc,
602d6c843b9SPeng Tao 			    struct nfs_pageio_descriptor *pgio, bool launder)
603f758c885STrond Myklebust {
604cfb506e1STrond Myklebust 	int ret;
605f758c885STrond Myklebust 
606*8cd79788SHuang Ying 	nfs_pageio_cond_complete(pgio, page_index(page));
607d6c843b9SPeng Tao 	ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE,
608d6c843b9SPeng Tao 				   launder);
609cfb506e1STrond Myklebust 	if (ret == -EAGAIN) {
610cfb506e1STrond Myklebust 		redirty_page_for_writepage(wbc, page);
611cfb506e1STrond Myklebust 		ret = 0;
612cfb506e1STrond Myklebust 	}
613cfb506e1STrond Myklebust 	return ret;
614f758c885STrond Myklebust }
615f758c885STrond Myklebust 
616e261f51fSTrond Myklebust /*
6171da177e4SLinus Torvalds  * Write an mmapped page to the server.
6181da177e4SLinus Torvalds  */
619d6c843b9SPeng Tao static int nfs_writepage_locked(struct page *page,
620d6c843b9SPeng Tao 				struct writeback_control *wbc,
621d6c843b9SPeng Tao 				bool launder)
6221da177e4SLinus Torvalds {
623f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
62440f90271STrond Myklebust 	struct inode *inode = page_file_mapping(page)->host;
625e261f51fSTrond Myklebust 	int err;
6261da177e4SLinus Torvalds 
62740f90271STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
628811ed92eSTrond Myklebust 	nfs_pageio_init_write(&pgio, inode, 0,
629a20c93e3SChristoph Hellwig 				false, &nfs_async_write_completion_ops);
630d6c843b9SPeng Tao 	err = nfs_do_writepage(page, wbc, &pgio, launder);
631f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
632f758c885STrond Myklebust 	if (err < 0)
6334d770ccfSTrond Myklebust 		return err;
634f758c885STrond Myklebust 	if (pgio.pg_error < 0)
635f758c885STrond Myklebust 		return pgio.pg_error;
636f758c885STrond Myklebust 	return 0;
6374d770ccfSTrond Myklebust }
6384d770ccfSTrond Myklebust 
6394d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
6404d770ccfSTrond Myklebust {
641f758c885STrond Myklebust 	int ret;
6424d770ccfSTrond Myklebust 
643d6c843b9SPeng Tao 	ret = nfs_writepage_locked(page, wbc, false);
6441da177e4SLinus Torvalds 	unlock_page(page);
645f758c885STrond Myklebust 	return ret;
646f758c885STrond Myklebust }
647f758c885STrond Myklebust 
648f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
649f758c885STrond Myklebust {
650f758c885STrond Myklebust 	int ret;
651f758c885STrond Myklebust 
652d6c843b9SPeng Tao 	ret = nfs_do_writepage(page, wbc, data, false);
653f758c885STrond Myklebust 	unlock_page(page);
654f758c885STrond Myklebust 	return ret;
6551da177e4SLinus Torvalds }
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
6581da177e4SLinus Torvalds {
6591da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
660c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
6611da177e4SLinus Torvalds 	int err;
6621da177e4SLinus Torvalds 
66391d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
66491d5b470SChuck Lever 
665a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false,
666a20c93e3SChristoph Hellwig 				&nfs_async_write_completion_ops);
667f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
668c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
66972cb77f4STrond Myklebust 
670f758c885STrond Myklebust 	if (err < 0)
67172cb77f4STrond Myklebust 		goto out_err;
67272cb77f4STrond Myklebust 	err = pgio.pg_error;
67372cb77f4STrond Myklebust 	if (err < 0)
67472cb77f4STrond Myklebust 		goto out_err;
675c63c7b05STrond Myklebust 	return 0;
67672cb77f4STrond Myklebust out_err:
67772cb77f4STrond Myklebust 	return err;
6781da177e4SLinus Torvalds }
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds /*
6811da177e4SLinus Torvalds  * Insert a write request into an inode
6821da177e4SLinus Torvalds  */
683d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
6841da177e4SLinus Torvalds {
6851da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
686e7d39069STrond Myklebust 
6872bfc6e56SWeston Andros Adamson 	WARN_ON_ONCE(req->wb_this_page != req);
6882bfc6e56SWeston Andros Adamson 
689e7d39069STrond Myklebust 	/* Lock the request! */
6907ad84aa9STrond Myklebust 	nfs_lock_request(req);
691e7d39069STrond Myklebust 
692e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
693cb1410c7SWeston Andros Adamson 	if (!nfsi->nrequests &&
694cb1410c7SWeston Andros Adamson 	    NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
695a9a4a87aSTrond Myklebust 		inode->i_version++;
69629418aa4SMel Gorman 	/*
69729418aa4SMel Gorman 	 * Swap-space should not get truncated. Hence no need to plug the race
69829418aa4SMel Gorman 	 * with invalidate/truncate.
69929418aa4SMel Gorman 	 */
70029418aa4SMel Gorman 	if (likely(!PageSwapCache(req->wb_page))) {
7012df485a7STrond Myklebust 		set_bit(PG_MAPPED, &req->wb_flags);
702deb7d638STrond Myklebust 		SetPagePrivate(req->wb_page);
703277459d2STrond Myklebust 		set_page_private(req->wb_page, (unsigned long)req);
70429418aa4SMel Gorman 	}
705cb1410c7SWeston Andros Adamson 	nfsi->nrequests++;
70617089a29SWeston Andros Adamson 	/* this a head request for a page group - mark it as having an
707cb1410c7SWeston Andros Adamson 	 * extra reference so sub groups can follow suit.
708cb1410c7SWeston Andros Adamson 	 * This flag also informs pgio layer when to bump nrequests when
709cb1410c7SWeston Andros Adamson 	 * adding subrequests. */
71017089a29SWeston Andros Adamson 	WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags));
711c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
712e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
7131da177e4SLinus Torvalds }
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds /*
71689a09141SPeter Zijlstra  * Remove a write request from an inode
7171da177e4SLinus Torvalds  */
7181da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
7191da177e4SLinus Torvalds {
7202b0143b5SDavid Howells 	struct inode *inode = d_inode(req->wb_context->dentry);
7211da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
72220633f04SWeston Andros Adamson 	struct nfs_page *head;
72320633f04SWeston Andros Adamson 
72420633f04SWeston Andros Adamson 	if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) {
72520633f04SWeston Andros Adamson 		head = req->wb_head;
7261da177e4SLinus Torvalds 
727587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
72867911c8fSAnna Schumaker 		if (likely(head->wb_page && !PageSwapCache(head->wb_page))) {
72920633f04SWeston Andros Adamson 			set_page_private(head->wb_page, 0);
73020633f04SWeston Andros Adamson 			ClearPagePrivate(head->wb_page);
73195905446SNeilBrown 			smp_mb__after_atomic();
73295905446SNeilBrown 			wake_up_page(head->wb_page, PG_private);
73320633f04SWeston Andros Adamson 			clear_bit(PG_MAPPED, &head->wb_flags);
73429418aa4SMel Gorman 		}
735cb1410c7SWeston Andros Adamson 		nfsi->nrequests--;
736cb1410c7SWeston Andros Adamson 		spin_unlock(&inode->i_lock);
737cb1410c7SWeston Andros Adamson 	} else {
738cb1410c7SWeston Andros Adamson 		spin_lock(&inode->i_lock);
739cb1410c7SWeston Andros Adamson 		nfsi->nrequests--;
740587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
74120633f04SWeston Andros Adamson 	}
74217089a29SWeston Andros Adamson 
74317089a29SWeston Andros Adamson 	if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags))
7441da177e4SLinus Torvalds 		nfs_release_request(req);
7451da177e4SLinus Torvalds }
7461da177e4SLinus Torvalds 
74761822ab5STrond Myklebust static void
7486d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
74961822ab5STrond Myklebust {
75067911c8fSAnna Schumaker 	if (req->wb_page)
75161822ab5STrond Myklebust 		__set_page_dirty_nobuffers(req->wb_page);
75261822ab5STrond Myklebust }
75361822ab5STrond Myklebust 
7543a3908c8STrond Myklebust /*
7553a3908c8STrond Myklebust  * nfs_page_search_commits_for_head_request_locked
7563a3908c8STrond Myklebust  *
7573a3908c8STrond Myklebust  * Search through commit lists on @inode for the head request for @page.
7583a3908c8STrond Myklebust  * Must be called while holding the inode (which is cinfo) lock.
7593a3908c8STrond Myklebust  *
7603a3908c8STrond Myklebust  * Returns the head request if found, or NULL if not found.
7613a3908c8STrond Myklebust  */
7623a3908c8STrond Myklebust static struct nfs_page *
7633a3908c8STrond Myklebust nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
7643a3908c8STrond Myklebust 						struct page *page)
7653a3908c8STrond Myklebust {
7663a3908c8STrond Myklebust 	struct nfs_page *freq, *t;
7673a3908c8STrond Myklebust 	struct nfs_commit_info cinfo;
7683a3908c8STrond Myklebust 	struct inode *inode = &nfsi->vfs_inode;
7693a3908c8STrond Myklebust 
7703a3908c8STrond Myklebust 	nfs_init_cinfo_from_inode(&cinfo, inode);
7713a3908c8STrond Myklebust 
7723a3908c8STrond Myklebust 	/* search through pnfs commit lists */
7733a3908c8STrond Myklebust 	freq = pnfs_search_commit_reqs(inode, &cinfo, page);
7743a3908c8STrond Myklebust 	if (freq)
7753a3908c8STrond Myklebust 		return freq->wb_head;
7763a3908c8STrond Myklebust 
7773a3908c8STrond Myklebust 	/* Linearly search the commit list for the correct request */
7783a3908c8STrond Myklebust 	list_for_each_entry_safe(freq, t, &cinfo.mds->list, wb_list) {
7793a3908c8STrond Myklebust 		if (freq->wb_page == page)
7803a3908c8STrond Myklebust 			return freq->wb_head;
7813a3908c8STrond Myklebust 	}
7823a3908c8STrond Myklebust 
7833a3908c8STrond Myklebust 	return NULL;
7843a3908c8STrond Myklebust }
7853a3908c8STrond Myklebust 
7868dd37758STrond Myklebust /**
78786d80f97STrond Myklebust  * nfs_request_add_commit_list_locked - add request to a commit list
78886d80f97STrond Myklebust  * @req: pointer to a struct nfs_page
78986d80f97STrond Myklebust  * @dst: commit list head
79086d80f97STrond Myklebust  * @cinfo: holds list lock and accounting info
79186d80f97STrond Myklebust  *
79286d80f97STrond Myklebust  * This sets the PG_CLEAN bit, updates the cinfo count of
79386d80f97STrond Myklebust  * number of outstanding requests requiring a commit as well as
79486d80f97STrond Myklebust  * the MM page stats.
79586d80f97STrond Myklebust  *
796fe238e60SDave Wysochanski  * The caller must hold cinfo->inode->i_lock, and the nfs_page lock.
79786d80f97STrond Myklebust  */
79886d80f97STrond Myklebust void
79986d80f97STrond Myklebust nfs_request_add_commit_list_locked(struct nfs_page *req, struct list_head *dst,
80086d80f97STrond Myklebust 			    struct nfs_commit_info *cinfo)
80186d80f97STrond Myklebust {
80286d80f97STrond Myklebust 	set_bit(PG_CLEAN, &req->wb_flags);
80386d80f97STrond Myklebust 	nfs_list_add_request(req, dst);
80486d80f97STrond Myklebust 	cinfo->mds->ncommit++;
80586d80f97STrond Myklebust }
80686d80f97STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked);
80786d80f97STrond Myklebust 
80886d80f97STrond Myklebust /**
8098dd37758STrond Myklebust  * nfs_request_add_commit_list - add request to a commit list
8108dd37758STrond Myklebust  * @req: pointer to a struct nfs_page
811ea2cf228SFred Isaman  * @dst: commit list head
812ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
8138dd37758STrond Myklebust  *
814ea2cf228SFred Isaman  * This sets the PG_CLEAN bit, updates the cinfo count of
8158dd37758STrond Myklebust  * number of outstanding requests requiring a commit as well as
8168dd37758STrond Myklebust  * the MM page stats.
8178dd37758STrond Myklebust  *
818ea2cf228SFred Isaman  * The caller must _not_ hold the cinfo->lock, but must be
8198dd37758STrond Myklebust  * holding the nfs_page lock.
8208dd37758STrond Myklebust  */
8218dd37758STrond Myklebust void
8226272dcc6SAnna Schumaker nfs_request_add_commit_list(struct nfs_page *req, struct nfs_commit_info *cinfo)
8238dd37758STrond Myklebust {
824fe238e60SDave Wysochanski 	spin_lock(&cinfo->inode->i_lock);
8256272dcc6SAnna Schumaker 	nfs_request_add_commit_list_locked(req, &cinfo->mds->list, cinfo);
826fe238e60SDave Wysochanski 	spin_unlock(&cinfo->inode->i_lock);
82767911c8fSAnna Schumaker 	if (req->wb_page)
82886d80f97STrond Myklebust 		nfs_mark_page_unstable(req->wb_page, cinfo);
8298dd37758STrond Myklebust }
8308dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
8318dd37758STrond Myklebust 
8328dd37758STrond Myklebust /**
8338dd37758STrond Myklebust  * nfs_request_remove_commit_list - Remove request from a commit list
8348dd37758STrond Myklebust  * @req: pointer to a nfs_page
835ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
8368dd37758STrond Myklebust  *
837ea2cf228SFred Isaman  * This clears the PG_CLEAN bit, and updates the cinfo's count of
8388dd37758STrond Myklebust  * number of outstanding requests requiring a commit
8398dd37758STrond Myklebust  * It does not update the MM page stats.
8408dd37758STrond Myklebust  *
841ea2cf228SFred Isaman  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
8428dd37758STrond Myklebust  */
8438dd37758STrond Myklebust void
844ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req,
845ea2cf228SFred Isaman 			       struct nfs_commit_info *cinfo)
8468dd37758STrond Myklebust {
8478dd37758STrond Myklebust 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
8488dd37758STrond Myklebust 		return;
8498dd37758STrond Myklebust 	nfs_list_remove_request(req);
850ea2cf228SFred Isaman 	cinfo->mds->ncommit--;
8518dd37758STrond Myklebust }
8528dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
8538dd37758STrond Myklebust 
854ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
855ea2cf228SFred Isaman 				      struct inode *inode)
856ea2cf228SFred Isaman {
857fe238e60SDave Wysochanski 	cinfo->inode = inode;
858ea2cf228SFred Isaman 	cinfo->mds = &NFS_I(inode)->commit_info;
859ea2cf228SFred Isaman 	cinfo->ds = pnfs_get_ds_info(inode);
860b359f9d0SFred Isaman 	cinfo->dreq = NULL;
861f453a54aSFred Isaman 	cinfo->completion_ops = &nfs_commit_completion_ops;
862ea2cf228SFred Isaman }
863ea2cf228SFred Isaman 
864ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
865ea2cf228SFred Isaman 		    struct inode *inode,
866ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq)
867ea2cf228SFred Isaman {
8681763da12SFred Isaman 	if (dreq)
8691763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
8701763da12SFred Isaman 	else
871ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(cinfo, inode);
872ea2cf228SFred Isaman }
873ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo);
8748dd37758STrond Myklebust 
8751da177e4SLinus Torvalds /*
8761da177e4SLinus Torvalds  * Add a request to the inode's commit list.
8771da177e4SLinus Torvalds  */
8781763da12SFred Isaman void
879ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
880b57ff130SWeston Andros Adamson 			struct nfs_commit_info *cinfo, u32 ds_commit_idx)
8811da177e4SLinus Torvalds {
882b57ff130SWeston Andros Adamson 	if (pnfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx))
8838dd37758STrond Myklebust 		return;
8846272dcc6SAnna Schumaker 	nfs_request_add_commit_list(req, cinfo);
8851da177e4SLinus Torvalds }
8868e821cadSTrond Myklebust 
887d6d6dc7cSFred Isaman static void
888d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page)
889e468bae9STrond Myklebust {
89011fb9989SMel Gorman 	dec_node_page_state(page, NR_UNSTABLE_NFS);
89193f78d88STejun Heo 	dec_wb_stat(&inode_to_bdi(page_file_mapping(page)->host)->wb,
89293f78d88STejun Heo 		    WB_RECLAIMABLE);
893e468bae9STrond Myklebust }
894d6d6dc7cSFred Isaman 
895411a99adSWeston Andros Adamson /* Called holding inode (/cinfo) lock */
8968dd37758STrond Myklebust static void
897d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req)
898d6d6dc7cSFred Isaman {
8998dd37758STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
9002b0143b5SDavid Howells 		struct inode *inode = d_inode(req->wb_context->dentry);
901ea2cf228SFred Isaman 		struct nfs_commit_info cinfo;
902d6d6dc7cSFred Isaman 
903ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(&cinfo, inode);
904ea2cf228SFred Isaman 		if (!pnfs_clear_request_commit(req, &cinfo)) {
905ea2cf228SFred Isaman 			nfs_request_remove_commit_list(req, &cinfo);
906d6d6dc7cSFred Isaman 		}
9078dd37758STrond Myklebust 		nfs_clear_page_commit(req->wb_page);
9088dd37758STrond Myklebust 	}
909e468bae9STrond Myklebust }
910e468bae9STrond Myklebust 
911d45f60c6SWeston Andros Adamson int nfs_write_need_commit(struct nfs_pgio_header *hdr)
9128e821cadSTrond Myklebust {
913c65e6254SWeston Andros Adamson 	if (hdr->verf.committed == NFS_DATA_SYNC)
914d45f60c6SWeston Andros Adamson 		return hdr->lseg == NULL;
915c65e6254SWeston Andros Adamson 	return hdr->verf.committed != NFS_FILE_SYNC;
9168e821cadSTrond Myklebust }
9178e821cadSTrond Myklebust 
918061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
9196c75dc0dSFred Isaman {
920ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
9216c75dc0dSFred Isaman 	unsigned long bytes = 0;
9226c75dc0dSFred Isaman 
9236c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
9246c75dc0dSFred Isaman 		goto out;
925ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
9266c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
9276c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
9286c75dc0dSFred Isaman 
9296c75dc0dSFred Isaman 		bytes += req->wb_bytes;
9306c75dc0dSFred Isaman 		nfs_list_remove_request(req);
9316c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
9326c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
933d1182b33STrond Myklebust 			nfs_set_pageerror(req->wb_page);
9346c75dc0dSFred Isaman 			nfs_context_set_write_error(req->wb_context, hdr->error);
9356c75dc0dSFred Isaman 			goto remove_req;
9366c75dc0dSFred Isaman 		}
937c65e6254SWeston Andros Adamson 		if (nfs_write_need_commit(hdr)) {
938f79d06f5SAnna Schumaker 			memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
939b57ff130SWeston Andros Adamson 			nfs_mark_request_commit(req, hdr->lseg, &cinfo,
940a7d42ddbSWeston Andros Adamson 				hdr->pgio_mirror_idx);
9416c75dc0dSFred Isaman 			goto next;
9426c75dc0dSFred Isaman 		}
9436c75dc0dSFred Isaman remove_req:
9446c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
9456c75dc0dSFred Isaman next:
9461d1afcbcSTrond Myklebust 		nfs_unlock_request(req);
94720633f04SWeston Andros Adamson 		nfs_end_page_writeback(req);
9483aff4ebbSTrond Myklebust 		nfs_release_request(req);
9496c75dc0dSFred Isaman 	}
9506c75dc0dSFred Isaman out:
9516c75dc0dSFred Isaman 	hdr->release(hdr);
9526c75dc0dSFred Isaman }
9536c75dc0dSFred Isaman 
954ce59515cSAnna Schumaker unsigned long
955ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
956fb8a1f11STrond Myklebust {
957ea2cf228SFred Isaman 	return cinfo->mds->ncommit;
958fb8a1f11STrond Myklebust }
959fb8a1f11STrond Myklebust 
960fe238e60SDave Wysochanski /* cinfo->inode->i_lock held by caller */
9611763da12SFred Isaman int
962ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
963ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
964d6d6dc7cSFred Isaman {
965d6d6dc7cSFred Isaman 	struct nfs_page *req, *tmp;
966d6d6dc7cSFred Isaman 	int ret = 0;
967d6d6dc7cSFred Isaman 
968d6d6dc7cSFred Isaman 	list_for_each_entry_safe(req, tmp, src, wb_list) {
9698dd37758STrond Myklebust 		if (!nfs_lock_request(req))
9708dd37758STrond Myklebust 			continue;
9717ad84aa9STrond Myklebust 		kref_get(&req->wb_kref);
972fe238e60SDave Wysochanski 		if (cond_resched_lock(&cinfo->inode->i_lock))
9733b3be88dSTrond Myklebust 			list_safe_reset_next(req, tmp, wb_list);
974ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
9758dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
976d6d6dc7cSFred Isaman 		ret++;
9771763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
978d6d6dc7cSFred Isaman 			break;
979d6d6dc7cSFred Isaman 	}
980d6d6dc7cSFred Isaman 	return ret;
981d6d6dc7cSFred Isaman }
982d6d6dc7cSFred Isaman 
9831da177e4SLinus Torvalds /*
9841da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
9851da177e4SLinus Torvalds  * @inode: NFS inode to scan
986ea2cf228SFred Isaman  * @dst: mds destination list
987ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
9881da177e4SLinus Torvalds  *
9891da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
9901da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
9911da177e4SLinus Torvalds  */
9921763da12SFred Isaman int
993ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
994ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
9951da177e4SLinus Torvalds {
996d6d6dc7cSFred Isaman 	int ret = 0;
997fb8a1f11STrond Myklebust 
998fe238e60SDave Wysochanski 	spin_lock(&cinfo->inode->i_lock);
999ea2cf228SFred Isaman 	if (cinfo->mds->ncommit > 0) {
10008dd37758STrond Myklebust 		const int max = INT_MAX;
1001d6d6dc7cSFred Isaman 
1002ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
1003ea2cf228SFred Isaman 					   cinfo, max);
1004ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
1005d6d6dc7cSFred Isaman 	}
1006fe238e60SDave Wysochanski 	spin_unlock(&cinfo->inode->i_lock);
1007ff778d02STrond Myklebust 	return ret;
10081da177e4SLinus Torvalds }
1009d6d6dc7cSFred Isaman 
10101da177e4SLinus Torvalds /*
1011e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
1012e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
10131da177e4SLinus Torvalds  *
1014e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
1015e7d39069STrond Myklebust  * to disk.
10161da177e4SLinus Torvalds  */
1017e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
1018e7d39069STrond Myklebust 		struct page *page,
1019e7d39069STrond Myklebust 		unsigned int offset,
1020e7d39069STrond Myklebust 		unsigned int bytes)
10211da177e4SLinus Torvalds {
1022e7d39069STrond Myklebust 	struct nfs_page *req;
1023e7d39069STrond Myklebust 	unsigned int rqend;
1024e7d39069STrond Myklebust 	unsigned int end;
10251da177e4SLinus Torvalds 	int error;
1026277459d2STrond Myklebust 
1027e7d39069STrond Myklebust 	if (!PagePrivate(page))
1028e7d39069STrond Myklebust 		return NULL;
1029e7d39069STrond Myklebust 
1030e7d39069STrond Myklebust 	end = offset + bytes;
1031e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
1032e7d39069STrond Myklebust 
1033e7d39069STrond Myklebust 	for (;;) {
103484d3a9a9SWeston Andros Adamson 		req = nfs_page_find_head_request_locked(NFS_I(inode), page);
1035e7d39069STrond Myklebust 		if (req == NULL)
1036e7d39069STrond Myklebust 			goto out_unlock;
1037e7d39069STrond Myklebust 
10382bfc6e56SWeston Andros Adamson 		/* should be handled by nfs_flush_incompatible */
10392bfc6e56SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_head != req);
10402bfc6e56SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_this_page != req);
10412bfc6e56SWeston Andros Adamson 
1042e7d39069STrond Myklebust 		rqend = req->wb_offset + req->wb_bytes;
1043e7d39069STrond Myklebust 		/*
1044e7d39069STrond Myklebust 		 * Tell the caller to flush out the request if
1045e7d39069STrond Myklebust 		 * the offsets are non-contiguous.
1046e7d39069STrond Myklebust 		 * Note: nfs_flush_incompatible() will already
1047e7d39069STrond Myklebust 		 * have flushed out requests having wrong owners.
1048e7d39069STrond Myklebust 		 */
1049e468bae9STrond Myklebust 		if (offset > rqend
1050e7d39069STrond Myklebust 		    || end < req->wb_offset)
1051e7d39069STrond Myklebust 			goto out_flushme;
1052e7d39069STrond Myklebust 
10537ad84aa9STrond Myklebust 		if (nfs_lock_request(req))
1054e7d39069STrond Myklebust 			break;
1055e7d39069STrond Myklebust 
1056e7d39069STrond Myklebust 		/* The request is locked, so wait and then retry */
1057587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
10581da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
10591da177e4SLinus Torvalds 		nfs_release_request(req);
1060e7d39069STrond Myklebust 		if (error != 0)
1061e7d39069STrond Myklebust 			goto out_err;
1062e7d39069STrond Myklebust 		spin_lock(&inode->i_lock);
10631da177e4SLinus Torvalds 	}
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
10661da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
10671da177e4SLinus Torvalds 		req->wb_offset = offset;
10681da177e4SLinus Torvalds 		req->wb_pgbase = offset;
10691da177e4SLinus Torvalds 	}
10701da177e4SLinus Torvalds 	if (end > rqend)
10711da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
1072e7d39069STrond Myklebust 	else
1073e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
1074e7d39069STrond Myklebust out_unlock:
1075ca138f36SFred Isaman 	if (req)
10768dd37758STrond Myklebust 		nfs_clear_request_commit(req);
1077411a99adSWeston Andros Adamson 	spin_unlock(&inode->i_lock);
1078e7d39069STrond Myklebust 	return req;
1079e7d39069STrond Myklebust out_flushme:
1080e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
1081e7d39069STrond Myklebust 	nfs_release_request(req);
1082e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
1083e7d39069STrond Myklebust out_err:
1084e7d39069STrond Myklebust 	return ERR_PTR(error);
1085e7d39069STrond Myklebust }
10861da177e4SLinus Torvalds 
1087e7d39069STrond Myklebust /*
1088e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
1089e7d39069STrond Myklebust  *
1090e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
1091e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
1092e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
1093e7d39069STrond Myklebust  */
1094e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
1095e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
1096e7d39069STrond Myklebust {
1097d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
1098e7d39069STrond Myklebust 	struct nfs_page	*req;
1099e7d39069STrond Myklebust 
1100e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
1101e7d39069STrond Myklebust 	if (req != NULL)
1102e7d39069STrond Myklebust 		goto out;
11032bfc6e56SWeston Andros Adamson 	req = nfs_create_request(ctx, page, NULL, offset, bytes);
1104e7d39069STrond Myklebust 	if (IS_ERR(req))
1105e7d39069STrond Myklebust 		goto out;
1106d6d6dc7cSFred Isaman 	nfs_inode_add_request(inode, req);
1107efc91ed0STrond Myklebust out:
110861e930a9STrond Myklebust 	return req;
11091da177e4SLinus Torvalds }
11101da177e4SLinus Torvalds 
1111e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
1112e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
1113e7d39069STrond Myklebust {
1114e7d39069STrond Myklebust 	struct nfs_page	*req;
1115e7d39069STrond Myklebust 
1116e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
1117e7d39069STrond Myklebust 	if (IS_ERR(req))
1118e7d39069STrond Myklebust 		return PTR_ERR(req);
1119e7d39069STrond Myklebust 	/* Update file length */
1120e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
1121d72ddcbaSWeston Andros Adamson 	nfs_mark_uptodate(req);
1122a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
11231d1afcbcSTrond Myklebust 	nfs_unlock_and_release_request(req);
1124e7d39069STrond Myklebust 	return 0;
1125e7d39069STrond Myklebust }
1126e7d39069STrond Myklebust 
11271da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
11281da177e4SLinus Torvalds {
1129cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
11302a369153STrond Myklebust 	struct nfs_lock_context *l_ctx;
1131bd61e0a9SJeff Layton 	struct file_lock_context *flctx = file_inode(file)->i_flctx;
11321da177e4SLinus Torvalds 	struct nfs_page	*req;
11331a54533eSTrond Myklebust 	int do_flush, status;
11341da177e4SLinus Torvalds 	/*
11351da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
11361da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
11371da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
11381da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
11391da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
11401da177e4SLinus Torvalds 	 * dropped page.
11411da177e4SLinus Torvalds 	 */
11421a54533eSTrond Myklebust 	do {
114384d3a9a9SWeston Andros Adamson 		req = nfs_page_find_head_request(page);
11441a54533eSTrond Myklebust 		if (req == NULL)
11451a54533eSTrond Myklebust 			return 0;
11462a369153STrond Myklebust 		l_ctx = req->wb_lock_context;
1147138a2935STrond Myklebust 		do_flush = req->wb_page != page ||
1148138a2935STrond Myklebust 			!nfs_match_open_context(req->wb_context, ctx);
11492bfc6e56SWeston Andros Adamson 		/* for now, flush if more than 1 request in page_group */
11502bfc6e56SWeston Andros Adamson 		do_flush |= req->wb_this_page != req;
1151bd61e0a9SJeff Layton 		if (l_ctx && flctx &&
1152bd61e0a9SJeff Layton 		    !(list_empty_careful(&flctx->flc_posix) &&
1153bd61e0a9SJeff Layton 		      list_empty_careful(&flctx->flc_flock))) {
11545263e31eSJeff Layton 			do_flush |= l_ctx->lockowner.l_owner != current->files
11555263e31eSJeff Layton 				|| l_ctx->lockowner.l_pid != current->tgid;
11565263e31eSJeff Layton 		}
11571da177e4SLinus Torvalds 		nfs_release_request(req);
11581a54533eSTrond Myklebust 		if (!do_flush)
11591a54533eSTrond Myklebust 			return 0;
1160d56b4ddfSMel Gorman 		status = nfs_wb_page(page_file_mapping(page)->host, page);
11611a54533eSTrond Myklebust 	} while (status == 0);
11621a54533eSTrond Myklebust 	return status;
11631da177e4SLinus Torvalds }
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds /*
1166dc24826bSAndy Adamson  * Avoid buffered writes when a open context credential's key would
1167dc24826bSAndy Adamson  * expire soon.
1168dc24826bSAndy Adamson  *
1169dc24826bSAndy Adamson  * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
1170dc24826bSAndy Adamson  *
1171dc24826bSAndy Adamson  * Return 0 and set a credential flag which triggers the inode to flush
1172dc24826bSAndy Adamson  * and performs  NFS_FILE_SYNC writes if the key will expired within
1173dc24826bSAndy Adamson  * RPC_KEY_EXPIRE_TIMEO.
1174dc24826bSAndy Adamson  */
1175dc24826bSAndy Adamson int
1176dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode)
1177dc24826bSAndy Adamson {
1178dc24826bSAndy Adamson 	struct nfs_open_context *ctx = nfs_file_open_context(filp);
1179dc24826bSAndy Adamson 	struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
1180dc24826bSAndy Adamson 
1181dc24826bSAndy Adamson 	return rpcauth_key_timeout_notify(auth, ctx->cred);
1182dc24826bSAndy Adamson }
1183dc24826bSAndy Adamson 
1184dc24826bSAndy Adamson /*
1185dc24826bSAndy Adamson  * Test if the open context credential key is marked to expire soon.
1186dc24826bSAndy Adamson  */
1187ce52914eSScott Mayhew bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode)
1188dc24826bSAndy Adamson {
1189ce52914eSScott Mayhew 	struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
1190ce52914eSScott Mayhew 
1191ce52914eSScott Mayhew 	return rpcauth_cred_key_to_expire(auth, ctx->cred);
1192dc24826bSAndy Adamson }
1193dc24826bSAndy Adamson 
1194dc24826bSAndy Adamson /*
11955d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
11965d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
11975d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
11985d47a356STrond Myklebust  */
11998d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
12005d47a356STrond Myklebust {
1201d529ef83SJeff Layton 	struct nfs_inode *nfsi = NFS_I(inode);
1202d529ef83SJeff Layton 
12038d197a56STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
12048d197a56STrond Myklebust 		goto out;
120518dd78c4SScott Mayhew 	if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
1206d529ef83SJeff Layton 		return false;
12074db72b40SJeff Layton 	smp_rmb();
1208d529ef83SJeff Layton 	if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags))
12098d197a56STrond Myklebust 		return false;
12108d197a56STrond Myklebust out:
121118dd78c4SScott Mayhew 	if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
121218dd78c4SScott Mayhew 		return false;
12138d197a56STrond Myklebust 	return PageUptodate(page) != 0;
12145d47a356STrond Myklebust }
12155d47a356STrond Myklebust 
12165263e31eSJeff Layton static bool
12175263e31eSJeff Layton is_whole_file_wrlock(struct file_lock *fl)
12185263e31eSJeff Layton {
12195263e31eSJeff Layton 	return fl->fl_start == 0 && fl->fl_end == OFFSET_MAX &&
12205263e31eSJeff Layton 			fl->fl_type == F_WRLCK;
12215263e31eSJeff Layton }
12225263e31eSJeff Layton 
1223c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or
1224c7559663SScott Mayhew  * if we have the whole file locked for writing), it may be more efficient to
1225c7559663SScott Mayhew  * extend the write to cover the entire page in order to avoid fragmentation
1226c7559663SScott Mayhew  * inefficiencies.
1227c7559663SScott Mayhew  *
1228263b4509SScott Mayhew  * If the file is opened for synchronous writes then we can just skip the rest
1229263b4509SScott Mayhew  * of the checks.
1230c7559663SScott Mayhew  */
1231c7559663SScott Mayhew static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode)
1232c7559663SScott Mayhew {
12335263e31eSJeff Layton 	int ret;
12345263e31eSJeff Layton 	struct file_lock_context *flctx = inode->i_flctx;
12355263e31eSJeff Layton 	struct file_lock *fl;
12365263e31eSJeff Layton 
1237c7559663SScott Mayhew 	if (file->f_flags & O_DSYNC)
1238c7559663SScott Mayhew 		return 0;
1239263b4509SScott Mayhew 	if (!nfs_write_pageuptodate(page, inode))
1240263b4509SScott Mayhew 		return 0;
1241c7559663SScott Mayhew 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
1242c7559663SScott Mayhew 		return 1;
1243bd61e0a9SJeff Layton 	if (!flctx || (list_empty_careful(&flctx->flc_flock) &&
1244bd61e0a9SJeff Layton 		       list_empty_careful(&flctx->flc_posix)))
12458fa4592aSTrond Myklebust 		return 1;
12465263e31eSJeff Layton 
12475263e31eSJeff Layton 	/* Check to see if there are whole file write locks */
12485263e31eSJeff Layton 	ret = 0;
12496109c850SJeff Layton 	spin_lock(&flctx->flc_lock);
1250bd61e0a9SJeff Layton 	if (!list_empty(&flctx->flc_posix)) {
1251bd61e0a9SJeff Layton 		fl = list_first_entry(&flctx->flc_posix, struct file_lock,
1252bd61e0a9SJeff Layton 					fl_list);
1253bd61e0a9SJeff Layton 		if (is_whole_file_wrlock(fl))
12545263e31eSJeff Layton 			ret = 1;
1255bd61e0a9SJeff Layton 	} else if (!list_empty(&flctx->flc_flock)) {
12565263e31eSJeff Layton 		fl = list_first_entry(&flctx->flc_flock, struct file_lock,
12575263e31eSJeff Layton 					fl_list);
12585263e31eSJeff Layton 		if (fl->fl_type == F_WRLCK)
12595263e31eSJeff Layton 			ret = 1;
12605263e31eSJeff Layton 	}
12616109c850SJeff Layton 	spin_unlock(&flctx->flc_lock);
12625263e31eSJeff Layton 	return ret;
1263c7559663SScott Mayhew }
1264c7559663SScott Mayhew 
12655d47a356STrond Myklebust /*
12661da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
12671da177e4SLinus Torvalds  *
12681da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
12691da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
12701da177e4SLinus Torvalds  */
12711da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
12721da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
12731da177e4SLinus Torvalds {
1274cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
1275d56b4ddfSMel Gorman 	struct inode	*inode = page_file_mapping(page)->host;
12761da177e4SLinus Torvalds 	int		status = 0;
12771da177e4SLinus Torvalds 
127891d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
127991d5b470SChuck Lever 
12806de1472fSAl Viro 	dprintk("NFS:       nfs_updatepage(%pD2 %d@%lld)\n",
12816de1472fSAl Viro 		file, count, (long long)(page_file_offset(page) + offset));
12821da177e4SLinus Torvalds 
1283149a4fddSBenjamin Coddington 	if (!count)
1284149a4fddSBenjamin Coddington 		goto out;
1285149a4fddSBenjamin Coddington 
1286c7559663SScott Mayhew 	if (nfs_can_extend_write(file, page, inode)) {
128749a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
12881da177e4SLinus Torvalds 		offset = 0;
12891da177e4SLinus Torvalds 	}
12901da177e4SLinus Torvalds 
1291e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
129203fa9e84STrond Myklebust 	if (status < 0)
129303fa9e84STrond Myklebust 		nfs_set_pageerror(page);
129459b7c05fSTrond Myklebust 	else
129559b7c05fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
1296149a4fddSBenjamin Coddington out:
129748186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
12981da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
12991da177e4SLinus Torvalds 	return status;
13001da177e4SLinus Torvalds }
13011da177e4SLinus Torvalds 
13023ff7576dSTrond Myklebust static int flush_task_priority(int how)
13031da177e4SLinus Torvalds {
13041da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
13051da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
13061da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
13071da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
13081da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
13091da177e4SLinus Torvalds 	}
13101da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
13111da177e4SLinus Torvalds }
13121da177e4SLinus Torvalds 
1313d45f60c6SWeston Andros Adamson static void nfs_initiate_write(struct nfs_pgio_header *hdr,
1314d45f60c6SWeston Andros Adamson 			       struct rpc_message *msg,
1315abde71f4STom Haynes 			       const struct nfs_rpc_ops *rpc_ops,
13161ed26f33SAnna Schumaker 			       struct rpc_task_setup *task_setup_data, int how)
13171da177e4SLinus Torvalds {
13183ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
13191da177e4SLinus Torvalds 
13201ed26f33SAnna Schumaker 	task_setup_data->priority = priority;
1321abde71f4STom Haynes 	rpc_ops->write_setup(hdr, msg);
1322d138d5d1SAndy Adamson 
1323abde71f4STom Haynes 	nfs4_state_protect_write(NFS_SERVER(hdr->inode)->nfs_client,
1324d45f60c6SWeston Andros Adamson 				 &task_setup_data->rpc_client, msg, hdr);
1325275acaafSTrond Myklebust }
1326275acaafSTrond Myklebust 
13276d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
13286d884e8fSFred  * call this on each, which will prepare them to be retried on next
13296d884e8fSFred  * writeback using standard nfs.
13306d884e8fSFred  */
13316d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
13326d884e8fSFred {
13336d884e8fSFred 	nfs_mark_request_dirty(req);
1334c7070113STrond Myklebust 	set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
13351d1afcbcSTrond Myklebust 	nfs_unlock_request(req);
133620633f04SWeston Andros Adamson 	nfs_end_page_writeback(req);
13373aff4ebbSTrond Myklebust 	nfs_release_request(req);
13386d884e8fSFred }
13396d884e8fSFred 
1340061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head)
13416c75dc0dSFred Isaman {
13426c75dc0dSFred Isaman 	struct nfs_page	*req;
13436c75dc0dSFred Isaman 
13446c75dc0dSFred Isaman 	while (!list_empty(head)) {
13456c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
13466c75dc0dSFred Isaman 		nfs_list_remove_request(req);
13476c75dc0dSFred Isaman 		nfs_redirty_request(req);
13486c75dc0dSFred Isaman 	}
13496c75dc0dSFred Isaman }
13506c75dc0dSFred Isaman 
1351dc602dd7STrond Myklebust static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr)
1352dc602dd7STrond Myklebust {
1353dc602dd7STrond Myklebust 	nfs_async_write_error(&hdr->pages);
1354dc602dd7STrond Myklebust }
1355dc602dd7STrond Myklebust 
1356061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1357061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1358061ae2edSFred Isaman 	.completion = nfs_write_completion,
1359dc602dd7STrond Myklebust 	.reschedule_io = nfs_async_write_reschedule_io,
1360061ae2edSFred Isaman };
1361061ae2edSFred Isaman 
136257208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1363a20c93e3SChristoph Hellwig 			       struct inode *inode, int ioflags, bool force_mds,
1364061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
13651751c363STrond Myklebust {
1366a20c93e3SChristoph Hellwig 	struct nfs_server *server = NFS_SERVER(inode);
136741d8d5b7SAnna Schumaker 	const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
1368a20c93e3SChristoph Hellwig 
1369a20c93e3SChristoph Hellwig #ifdef CONFIG_NFS_V4_1
1370a20c93e3SChristoph Hellwig 	if (server->pnfs_curr_ld && !force_mds)
1371a20c93e3SChristoph Hellwig 		pg_ops = server->pnfs_curr_ld->pg_write_ops;
1372a20c93e3SChristoph Hellwig #endif
13734a0de55cSAnna Schumaker 	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
13744a0de55cSAnna Schumaker 			server->wsize, ioflags);
13751751c363STrond Myklebust }
1376ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
13771751c363STrond Myklebust 
1378dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1379dce81290STrond Myklebust {
1380a7d42ddbSWeston Andros Adamson 	struct nfs_pgio_mirror *mirror;
1381a7d42ddbSWeston Andros Adamson 
13826f29b9bbSKinglong Mee 	if (pgio->pg_ops && pgio->pg_ops->pg_cleanup)
13836f29b9bbSKinglong Mee 		pgio->pg_ops->pg_cleanup(pgio);
13846f29b9bbSKinglong Mee 
138541d8d5b7SAnna Schumaker 	pgio->pg_ops = &nfs_pgio_rw_ops;
1386a7d42ddbSWeston Andros Adamson 
1387a7d42ddbSWeston Andros Adamson 	nfs_pageio_stop_mirroring(pgio);
1388a7d42ddbSWeston Andros Adamson 
1389a7d42ddbSWeston Andros Adamson 	mirror = &pgio->pg_mirrors[0];
1390a7d42ddbSWeston Andros Adamson 	mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1391dce81290STrond Myklebust }
13921f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1393dce81290STrond Myklebust 
13941da177e4SLinus Torvalds 
13950b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
13960b7c0153SFred Isaman {
13970b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
13980b7c0153SFred Isaman 
13990b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
14000b7c0153SFred Isaman }
14010b7c0153SFred Isaman 
14021f2edbe3STrond Myklebust /*
14031f2edbe3STrond Myklebust  * Special version of should_remove_suid() that ignores capabilities.
14041f2edbe3STrond Myklebust  */
14051f2edbe3STrond Myklebust static int nfs_should_remove_suid(const struct inode *inode)
14061f2edbe3STrond Myklebust {
14071f2edbe3STrond Myklebust 	umode_t mode = inode->i_mode;
14081f2edbe3STrond Myklebust 	int kill = 0;
1409788e7a89STrond Myklebust 
14101f2edbe3STrond Myklebust 	/* suid always must be killed */
14111f2edbe3STrond Myklebust 	if (unlikely(mode & S_ISUID))
14121f2edbe3STrond Myklebust 		kill = ATTR_KILL_SUID;
14131f2edbe3STrond Myklebust 
14141f2edbe3STrond Myklebust 	/*
14151f2edbe3STrond Myklebust 	 * sgid without any exec bits is just a mandatory locking mark; leave
14161f2edbe3STrond Myklebust 	 * it alone.  If some exec bits are set, it's a real sgid; kill it.
14171f2edbe3STrond Myklebust 	 */
14181f2edbe3STrond Myklebust 	if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
14191f2edbe3STrond Myklebust 		kill |= ATTR_KILL_SGID;
14201f2edbe3STrond Myklebust 
14211f2edbe3STrond Myklebust 	if (unlikely(kill && S_ISREG(mode)))
14221f2edbe3STrond Myklebust 		return kill;
14231f2edbe3STrond Myklebust 
14241f2edbe3STrond Myklebust 	return 0;
14251f2edbe3STrond Myklebust }
1426788e7a89STrond Myklebust 
1427a08a8cd3STrond Myklebust static void nfs_writeback_check_extend(struct nfs_pgio_header *hdr,
1428a08a8cd3STrond Myklebust 		struct nfs_fattr *fattr)
1429a08a8cd3STrond Myklebust {
1430a08a8cd3STrond Myklebust 	struct nfs_pgio_args *argp = &hdr->args;
1431a08a8cd3STrond Myklebust 	struct nfs_pgio_res *resp = &hdr->res;
14322b83d3deSTrond Myklebust 	u64 size = argp->offset + resp->count;
1433a08a8cd3STrond Myklebust 
1434a08a8cd3STrond Myklebust 	if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
14352b83d3deSTrond Myklebust 		fattr->size = size;
14362b83d3deSTrond Myklebust 	if (nfs_size_to_loff_t(fattr->size) < i_size_read(hdr->inode)) {
14372b83d3deSTrond Myklebust 		fattr->valid &= ~NFS_ATTR_FATTR_SIZE;
1438a08a8cd3STrond Myklebust 		return;
14392b83d3deSTrond Myklebust 	}
14402b83d3deSTrond Myklebust 	if (size != fattr->size)
1441a08a8cd3STrond Myklebust 		return;
1442a08a8cd3STrond Myklebust 	/* Set attribute barrier */
1443a08a8cd3STrond Myklebust 	nfs_fattr_set_barrier(fattr);
14442b83d3deSTrond Myklebust 	/* ...and update size */
14452b83d3deSTrond Myklebust 	fattr->valid |= NFS_ATTR_FATTR_SIZE;
1446a08a8cd3STrond Myklebust }
1447a08a8cd3STrond Myklebust 
1448a08a8cd3STrond Myklebust void nfs_writeback_update_inode(struct nfs_pgio_header *hdr)
1449a08a8cd3STrond Myklebust {
14502b83d3deSTrond Myklebust 	struct nfs_fattr *fattr = &hdr->fattr;
1451a08a8cd3STrond Myklebust 	struct inode *inode = hdr->inode;
1452a08a8cd3STrond Myklebust 
1453a08a8cd3STrond Myklebust 	spin_lock(&inode->i_lock);
1454a08a8cd3STrond Myklebust 	nfs_writeback_check_extend(hdr, fattr);
1455a08a8cd3STrond Myklebust 	nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
1456a08a8cd3STrond Myklebust 	spin_unlock(&inode->i_lock);
1457a08a8cd3STrond Myklebust }
1458a08a8cd3STrond Myklebust EXPORT_SYMBOL_GPL(nfs_writeback_update_inode);
1459a08a8cd3STrond Myklebust 
14601da177e4SLinus Torvalds /*
14611da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
14621da177e4SLinus Torvalds  */
1463d45f60c6SWeston Andros Adamson static int nfs_writeback_done(struct rpc_task *task,
1464d45f60c6SWeston Andros Adamson 			      struct nfs_pgio_header *hdr,
14650eecb214SAnna Schumaker 			      struct inode *inode)
14661da177e4SLinus Torvalds {
1467788e7a89STrond Myklebust 	int status;
14681da177e4SLinus Torvalds 
1469f551e44fSChuck Lever 	/*
1470f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1471f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1472f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1473f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1474f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1475f551e44fSChuck Lever 	 */
1476d45f60c6SWeston Andros Adamson 	status = NFS_PROTO(inode)->write_done(task, hdr);
1477788e7a89STrond Myklebust 	if (status != 0)
14780eecb214SAnna Schumaker 		return status;
1479d45f60c6SWeston Andros Adamson 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count);
148091d5b470SChuck Lever 
1481d45f60c6SWeston Andros Adamson 	if (hdr->res.verf->committed < hdr->args.stable &&
1482d45f60c6SWeston Andros Adamson 	    task->tk_status >= 0) {
14831da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
14841da177e4SLinus Torvalds 		 * commit data to stable storage even though we
14851da177e4SLinus Torvalds 		 * requested it.
14861da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
14871da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
14881da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
14891da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
14901da177e4SLinus Torvalds 		 */
14911da177e4SLinus Torvalds 		static unsigned long    complain;
14921da177e4SLinus Torvalds 
1493a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
14941da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
14951da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
14961da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1497cd841605SFred Isaman 				NFS_SERVER(inode)->nfs_client->cl_hostname,
1498d45f60c6SWeston Andros Adamson 				hdr->res.verf->committed, hdr->args.stable);
14991da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
15001da177e4SLinus Torvalds 		}
15011da177e4SLinus Torvalds 	}
15021f2edbe3STrond Myklebust 
15031f2edbe3STrond Myklebust 	/* Deal with the suid/sgid bit corner case */
15041f2edbe3STrond Myklebust 	if (nfs_should_remove_suid(inode))
15051f2edbe3STrond Myklebust 		nfs_mark_for_revalidate(inode);
15060eecb214SAnna Schumaker 	return 0;
15070eecb214SAnna Schumaker }
15080eecb214SAnna Schumaker 
15090eecb214SAnna Schumaker /*
15100eecb214SAnna Schumaker  * This function is called when the WRITE call is complete.
15110eecb214SAnna Schumaker  */
1512d45f60c6SWeston Andros Adamson static void nfs_writeback_result(struct rpc_task *task,
1513d45f60c6SWeston Andros Adamson 				 struct nfs_pgio_header *hdr)
15140eecb214SAnna Schumaker {
1515d45f60c6SWeston Andros Adamson 	struct nfs_pgio_args	*argp = &hdr->args;
1516d45f60c6SWeston Andros Adamson 	struct nfs_pgio_res	*resp = &hdr->res;
15171f2edbe3STrond Myklebust 
15181f2edbe3STrond Myklebust 	if (resp->count < argp->count) {
15191da177e4SLinus Torvalds 		static unsigned long    complain;
15201da177e4SLinus Torvalds 
15216c75dc0dSFred Isaman 		/* This a short write! */
1522d45f60c6SWeston Andros Adamson 		nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE);
152391d5b470SChuck Lever 
15241da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
15256c75dc0dSFred Isaman 		if (resp->count == 0) {
15266c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
15276c75dc0dSFred Isaman 				printk(KERN_WARNING
15286c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
15296c75dc0dSFred Isaman 				       argp->count);
15306c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
15316c75dc0dSFred Isaman 			}
1532d45f60c6SWeston Andros Adamson 			nfs_set_pgio_error(hdr, -EIO, argp->offset);
15336c75dc0dSFred Isaman 			task->tk_status = -EIO;
15346c75dc0dSFred Isaman 			return;
15356c75dc0dSFred Isaman 		}
1536f8417b48SKinglong Mee 
1537f8417b48SKinglong Mee 		/* For non rpc-based layout drivers, retry-through-MDS */
1538f8417b48SKinglong Mee 		if (!task->tk_ops) {
1539f8417b48SKinglong Mee 			hdr->pnfs_error = -EAGAIN;
1540f8417b48SKinglong Mee 			return;
1541f8417b48SKinglong Mee 		}
1542f8417b48SKinglong Mee 
15431da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
15441da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
15451da177e4SLinus Torvalds 			/* Resend from where the server left off */
1546d45f60c6SWeston Andros Adamson 			hdr->mds_offset += resp->count;
15471da177e4SLinus Torvalds 			argp->offset += resp->count;
15481da177e4SLinus Torvalds 			argp->pgbase += resp->count;
15491da177e4SLinus Torvalds 			argp->count -= resp->count;
15501da177e4SLinus Torvalds 		} else {
15511da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
15521da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
15531da177e4SLinus Torvalds 			 */
15541da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
15551da177e4SLinus Torvalds 		}
1556d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
15571da177e4SLinus Torvalds 	}
15581da177e4SLinus Torvalds }
15591da177e4SLinus Torvalds 
1560af7cf057STrond Myklebust static int wait_on_commit(struct nfs_mds_commit_info *cinfo)
156171d0a611STrond Myklebust {
1562af7cf057STrond Myklebust 	return wait_on_atomic_t(&cinfo->rpcs_out,
1563af7cf057STrond Myklebust 			nfs_wait_atomic_killable, TASK_KILLABLE);
1564af7cf057STrond Myklebust }
1565af7cf057STrond Myklebust 
1566af7cf057STrond Myklebust static void nfs_commit_begin(struct nfs_mds_commit_info *cinfo)
1567af7cf057STrond Myklebust {
1568af7cf057STrond Myklebust 	atomic_inc(&cinfo->rpcs_out);
1569af7cf057STrond Myklebust }
1570af7cf057STrond Myklebust 
1571af7cf057STrond Myklebust static void nfs_commit_end(struct nfs_mds_commit_info *cinfo)
1572af7cf057STrond Myklebust {
1573af7cf057STrond Myklebust 	if (atomic_dec_and_test(&cinfo->rpcs_out))
1574af7cf057STrond Myklebust 		wake_up_atomic_t(&cinfo->rpcs_out);
157571d0a611STrond Myklebust }
157671d0a611STrond Myklebust 
15770b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
15781da177e4SLinus Torvalds {
15790b7c0153SFred Isaman 	put_nfs_open_context(data->context);
15800b7c0153SFred Isaman 	nfs_commit_free(data);
15811da177e4SLinus Torvalds }
1582e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
15831da177e4SLinus Torvalds 
15840b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
1585c36aae9aSPeng Tao 			const struct nfs_rpc_ops *nfs_ops,
15869ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
15879f0ec176SAndy Adamson 			int how, int flags)
15881da177e4SLinus Torvalds {
158907737691STrond Myklebust 	struct rpc_task *task;
15909ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1591bdc7f021STrond Myklebust 	struct rpc_message msg = {
1592bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1593bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
15949ace33cdSFred Isaman 		.rpc_cred = data->cred,
1595bdc7f021STrond Myklebust 	};
159684115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
159707737691STrond Myklebust 		.task = &data->task,
15989ace33cdSFred Isaman 		.rpc_client = clnt,
1599bdc7f021STrond Myklebust 		.rpc_message = &msg,
16009ace33cdSFred Isaman 		.callback_ops = call_ops,
160184115e1cSTrond Myklebust 		.callback_data = data,
1602101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
16039f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
16043ff7576dSTrond Myklebust 		.priority = priority,
160584115e1cSTrond Myklebust 	};
1606788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
1607c36aae9aSPeng Tao 	nfs_ops->commit_setup(data, &msg);
16081da177e4SLinus Torvalds 
1609b4839ebeSKinglong Mee 	dprintk("NFS: initiated commit call\n");
1610bdc7f021STrond Myklebust 
16118c21c62cSWeston Andros Adamson 	nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client,
16128c21c62cSWeston Andros Adamson 		NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg);
16138c21c62cSWeston Andros Adamson 
161407737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1615dbae4c73STrond Myklebust 	if (IS_ERR(task))
1616dbae4c73STrond Myklebust 		return PTR_ERR(task);
1617d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1618d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
161907737691STrond Myklebust 	rpc_put_task(task);
1620dbae4c73STrond Myklebust 	return 0;
16211da177e4SLinus Torvalds }
1622e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
16231da177e4SLinus Torvalds 
1624378520b8SPeng Tao static loff_t nfs_get_lwb(struct list_head *head)
1625378520b8SPeng Tao {
1626378520b8SPeng Tao 	loff_t lwb = 0;
1627378520b8SPeng Tao 	struct nfs_page *req;
1628378520b8SPeng Tao 
1629378520b8SPeng Tao 	list_for_each_entry(req, head, wb_list)
1630378520b8SPeng Tao 		if (lwb < (req_offset(req) + req->wb_bytes))
1631378520b8SPeng Tao 			lwb = req_offset(req) + req->wb_bytes;
1632378520b8SPeng Tao 
1633378520b8SPeng Tao 	return lwb;
1634378520b8SPeng Tao }
1635378520b8SPeng Tao 
16361da177e4SLinus Torvalds /*
16379ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
16389ace33cdSFred Isaman  */
16390b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1640988b6dceSFred Isaman 		     struct list_head *head,
1641f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1642f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
16439ace33cdSFred Isaman {
16449ace33cdSFred Isaman 	struct nfs_page *first = nfs_list_entry(head->next);
16452b0143b5SDavid Howells 	struct inode *inode = d_inode(first->wb_context->dentry);
16469ace33cdSFred Isaman 
16479ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
16489ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
16499ace33cdSFred Isaman 
16509ace33cdSFred Isaman 	list_splice_init(head, &data->pages);
16519ace33cdSFred Isaman 
16529ace33cdSFred Isaman 	data->inode	  = inode;
16539ace33cdSFred Isaman 	data->cred	  = first->wb_context->cred;
1654988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
1655378520b8SPeng Tao 	/* only set lwb for pnfs commit */
1656378520b8SPeng Tao 	if (lseg)
1657378520b8SPeng Tao 		data->lwb = nfs_get_lwb(&data->pages);
16589ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1659f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1660b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
16619ace33cdSFred Isaman 
16629ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
16639ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
16649ace33cdSFred Isaman 	data->args.offset = 0;
16659ace33cdSFred Isaman 	data->args.count  = 0;
16660b7c0153SFred Isaman 	data->context     = get_nfs_open_context(first->wb_context);
16679ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
16689ace33cdSFred Isaman 	data->res.verf    = &data->verf;
16699ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
16709ace33cdSFred Isaman }
1671e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
16729ace33cdSFred Isaman 
1673e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1674ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1675b57ff130SWeston Andros Adamson 		      struct nfs_commit_info *cinfo,
1676b57ff130SWeston Andros Adamson 		      u32 ds_commit_idx)
167764bfeb49SFred Isaman {
167864bfeb49SFred Isaman 	struct nfs_page *req;
167964bfeb49SFred Isaman 
168064bfeb49SFred Isaman 	while (!list_empty(page_list)) {
168164bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
168264bfeb49SFred Isaman 		nfs_list_remove_request(req);
1683b57ff130SWeston Andros Adamson 		nfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx);
1684487b9b8aSTom Haynes 		if (!cinfo->dreq)
1685487b9b8aSTom Haynes 			nfs_clear_page_commit(req->wb_page);
16861d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
168764bfeb49SFred Isaman 	}
168864bfeb49SFred Isaman }
1689e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
169064bfeb49SFred Isaman 
1691b20135d0STrond Myklebust static void
1692b20135d0STrond Myklebust nfs_commit_resched_write(struct nfs_commit_info *cinfo,
1693b20135d0STrond Myklebust 		struct nfs_page *req)
1694b20135d0STrond Myklebust {
1695b20135d0STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
1696b20135d0STrond Myklebust }
1697b20135d0STrond Myklebust 
16989ace33cdSFred Isaman /*
16991da177e4SLinus Torvalds  * Commit dirty pages
17001da177e4SLinus Torvalds  */
17011da177e4SLinus Torvalds static int
1702ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1703ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
17041da177e4SLinus Torvalds {
17050b7c0153SFred Isaman 	struct nfs_commit_data	*data;
17061da177e4SLinus Torvalds 
1707ade8febdSWeston Andros Adamson 	/* another commit raced with us */
1708ade8febdSWeston Andros Adamson 	if (list_empty(head))
1709ade8febdSWeston Andros Adamson 		return 0;
1710ade8febdSWeston Andros Adamson 
1711c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
17121da177e4SLinus Torvalds 
17131da177e4SLinus Torvalds 	if (!data)
17141da177e4SLinus Torvalds 		goto out_bad;
17151da177e4SLinus Torvalds 
17161da177e4SLinus Torvalds 	/* Set up the argument struct */
1717f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
1718f453a54aSFred Isaman 	atomic_inc(&cinfo->mds->rpcs_out);
1719c36aae9aSPeng Tao 	return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode),
1720c36aae9aSPeng Tao 				   data->mds_ops, how, 0);
17211da177e4SLinus Torvalds  out_bad:
1722b57ff130SWeston Andros Adamson 	nfs_retry_commit(head, NULL, cinfo, 0);
17231da177e4SLinus Torvalds 	return -ENOMEM;
17241da177e4SLinus Torvalds }
17251da177e4SLinus Torvalds 
172667911c8fSAnna Schumaker int nfs_commit_file(struct file *file, struct nfs_write_verifier *verf)
172767911c8fSAnna Schumaker {
172867911c8fSAnna Schumaker 	struct inode *inode = file_inode(file);
172967911c8fSAnna Schumaker 	struct nfs_open_context *open;
173067911c8fSAnna Schumaker 	struct nfs_commit_info cinfo;
173167911c8fSAnna Schumaker 	struct nfs_page *req;
173267911c8fSAnna Schumaker 	int ret;
173367911c8fSAnna Schumaker 
173467911c8fSAnna Schumaker 	open = get_nfs_open_context(nfs_file_open_context(file));
173567911c8fSAnna Schumaker 	req  = nfs_create_request(open, NULL, NULL, 0, i_size_read(inode));
17362997bfd0SDan Carpenter 	if (IS_ERR(req)) {
17372997bfd0SDan Carpenter 		ret = PTR_ERR(req);
173867911c8fSAnna Schumaker 		goto out_put;
173967911c8fSAnna Schumaker 	}
174067911c8fSAnna Schumaker 
174167911c8fSAnna Schumaker 	nfs_init_cinfo_from_inode(&cinfo, inode);
174267911c8fSAnna Schumaker 
174367911c8fSAnna Schumaker 	memcpy(&req->wb_verf, verf, sizeof(struct nfs_write_verifier));
174467911c8fSAnna Schumaker 	nfs_request_add_commit_list(req, &cinfo);
174567911c8fSAnna Schumaker 	ret = nfs_commit_inode(inode, FLUSH_SYNC);
174667911c8fSAnna Schumaker 	if (ret > 0)
174767911c8fSAnna Schumaker 		ret = 0;
174867911c8fSAnna Schumaker 
174967911c8fSAnna Schumaker 	nfs_free_request(req);
175067911c8fSAnna Schumaker out_put:
175167911c8fSAnna Schumaker 	put_nfs_open_context(open);
175267911c8fSAnna Schumaker 	return ret;
175367911c8fSAnna Schumaker }
175467911c8fSAnna Schumaker EXPORT_SYMBOL_GPL(nfs_commit_file);
175567911c8fSAnna Schumaker 
17561da177e4SLinus Torvalds /*
17571da177e4SLinus Torvalds  * COMMIT call returned
17581da177e4SLinus Torvalds  */
1759788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
17601da177e4SLinus Torvalds {
17610b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
17621da177e4SLinus Torvalds 
1763a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
17641da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
17651da177e4SLinus Torvalds 
1766788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1767c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
1768c9d8f89dSTrond Myklebust }
1769c9d8f89dSTrond Myklebust 
1770f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1771c9d8f89dSTrond Myklebust {
1772c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1773c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1774f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
1775353db796SNeilBrown 	struct nfs_server *nfss;
1776788e7a89STrond Myklebust 
17771da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
17781da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
17791da177e4SLinus Torvalds 		nfs_list_remove_request(req);
178067911c8fSAnna Schumaker 		if (req->wb_page)
1781d6d6dc7cSFred Isaman 			nfs_clear_page_commit(req->wb_page);
17821da177e4SLinus Torvalds 
17831e8968c5SNiels de Vos 		dprintk("NFS:       commit (%s/%llu %d@%lld)",
17843d4ff43dSAl Viro 			req->wb_context->dentry->d_sb->s_id,
17852b0143b5SDavid Howells 			(unsigned long long)NFS_FILEID(d_inode(req->wb_context->dentry)),
17861da177e4SLinus Torvalds 			req->wb_bytes,
17871da177e4SLinus Torvalds 			(long long)req_offset(req));
1788c9d8f89dSTrond Myklebust 		if (status < 0) {
1789c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
17901da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1791c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
17921da177e4SLinus Torvalds 			goto next;
17931da177e4SLinus Torvalds 		}
17941da177e4SLinus Torvalds 
17951da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
17961da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
17978fc3c386STrond Myklebust 		if (!nfs_write_verifier_cmp(&req->wb_verf, &data->verf.verifier)) {
17981da177e4SLinus Torvalds 			/* We have a match */
17991da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
18001da177e4SLinus Torvalds 			dprintk(" OK\n");
18011da177e4SLinus Torvalds 			goto next;
18021da177e4SLinus Torvalds 		}
18031da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
18041da177e4SLinus Torvalds 		dprintk(" mismatch\n");
18056d884e8fSFred 		nfs_mark_request_dirty(req);
180605990d1bSTrond Myklebust 		set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
18071da177e4SLinus Torvalds 	next:
18081d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
18091da177e4SLinus Torvalds 	}
1810353db796SNeilBrown 	nfss = NFS_SERVER(data->inode);
1811353db796SNeilBrown 	if (atomic_long_read(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
1812353db796SNeilBrown 		clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
1813353db796SNeilBrown 
1814f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1815af7cf057STrond Myklebust 	nfs_commit_end(cinfo.mds);
18165917ce84SFred Isaman }
18175917ce84SFred Isaman 
18185917ce84SFred Isaman static void nfs_commit_release(void *calldata)
18195917ce84SFred Isaman {
18200b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
18215917ce84SFred Isaman 
1822f453a54aSFred Isaman 	data->completion_ops->completion(data);
1823c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
18241da177e4SLinus Torvalds }
1825788e7a89STrond Myklebust 
1826788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
18270b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1828788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1829788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1830788e7a89STrond Myklebust };
18311da177e4SLinus Torvalds 
1832f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1833f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1834b20135d0STrond Myklebust 	.resched_write = nfs_commit_resched_write,
1835f453a54aSFred Isaman };
1836f453a54aSFred Isaman 
18371763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1838ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
183984c53ab5SFred Isaman {
184084c53ab5SFred Isaman 	int status;
184184c53ab5SFred Isaman 
1842ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
184384c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1844ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
184584c53ab5SFred Isaman 	return status;
184684c53ab5SFred Isaman }
184784c53ab5SFred Isaman 
1848b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
18491da177e4SLinus Torvalds {
18501da177e4SLinus Torvalds 	LIST_HEAD(head);
1851ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
185271d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1853af7cf057STrond Myklebust 	int error = 0;
1854b8413f98STrond Myklebust 	int res;
18551da177e4SLinus Torvalds 
1856ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1857af7cf057STrond Myklebust 	nfs_commit_begin(cinfo.mds);
1858ea2cf228SFred Isaman 	res = nfs_scan_commit(inode, &head, &cinfo);
1859af7cf057STrond Myklebust 	if (res)
1860ea2cf228SFred Isaman 		error = nfs_generic_commit_list(inode, &head, how, &cinfo);
1861af7cf057STrond Myklebust 	nfs_commit_end(cinfo.mds);
18621da177e4SLinus Torvalds 	if (error < 0)
1863af7cf057STrond Myklebust 		goto out_error;
1864b8413f98STrond Myklebust 	if (!may_wait)
1865b8413f98STrond Myklebust 		goto out_mark_dirty;
1866af7cf057STrond Myklebust 	error = wait_on_commit(cinfo.mds);
1867b8413f98STrond Myklebust 	if (error < 0)
1868b8413f98STrond Myklebust 		return error;
1869c5efa5fcSTrond Myklebust 	return res;
1870af7cf057STrond Myklebust out_error:
1871af7cf057STrond Myklebust 	res = error;
1872c5efa5fcSTrond Myklebust 	/* Note: If we exit without ensuring that the commit is complete,
1873c5efa5fcSTrond Myklebust 	 * we must mark the inode as dirty. Otherwise, future calls to
1874c5efa5fcSTrond Myklebust 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1875c5efa5fcSTrond Myklebust 	 * that the data is on the disk.
1876c5efa5fcSTrond Myklebust 	 */
1877c5efa5fcSTrond Myklebust out_mark_dirty:
1878c5efa5fcSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
18791da177e4SLinus Torvalds 	return res;
18801da177e4SLinus Torvalds }
1881b20135d0STrond Myklebust EXPORT_SYMBOL_GPL(nfs_commit_inode);
18828fc795f7STrond Myklebust 
1883ae09c31fSAnna Schumaker int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
18848fc795f7STrond Myklebust {
1885420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1886420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1887420e3646STrond Myklebust 	int ret = 0;
18888fc795f7STrond Myklebust 
18893236c3e1SJeff Layton 	/* no commits means nothing needs to be done */
1890ea2cf228SFred Isaman 	if (!nfsi->commit_info.ncommit)
18913236c3e1SJeff Layton 		return ret;
18923236c3e1SJeff Layton 
1893a00dd6c0SJeff Layton 	if (wbc->sync_mode == WB_SYNC_NONE) {
1894a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1895a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1896420e3646STrond Myklebust 		 */
1897cb1410c7SWeston Andros Adamson 		if (nfsi->commit_info.ncommit <= (nfsi->nrequests >> 1))
1898420e3646STrond Myklebust 			goto out_mark_dirty;
1899420e3646STrond Myklebust 
1900a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1901420e3646STrond Myklebust 		flags = 0;
1902a00dd6c0SJeff Layton 	}
1903a00dd6c0SJeff Layton 
1904420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1905420e3646STrond Myklebust 	if (ret >= 0) {
1906420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1907420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1908420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1909420e3646STrond Myklebust 			else
1910420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1911420e3646STrond Myklebust 		}
19128fc795f7STrond Myklebust 		return 0;
1913420e3646STrond Myklebust 	}
1914420e3646STrond Myklebust out_mark_dirty:
19158fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
19168fc795f7STrond Myklebust 	return ret;
19178fc795f7STrond Myklebust }
191889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode);
1919863a3c6cSAndy Adamson 
1920acdc53b2STrond Myklebust /*
1921837bb1d7STrond Myklebust  * Wrapper for filemap_write_and_wait_range()
1922837bb1d7STrond Myklebust  *
1923837bb1d7STrond Myklebust  * Needed for pNFS in order to ensure data becomes visible to the
1924837bb1d7STrond Myklebust  * client.
1925837bb1d7STrond Myklebust  */
1926837bb1d7STrond Myklebust int nfs_filemap_write_and_wait_range(struct address_space *mapping,
1927837bb1d7STrond Myklebust 		loff_t lstart, loff_t lend)
1928837bb1d7STrond Myklebust {
1929837bb1d7STrond Myklebust 	int ret;
1930837bb1d7STrond Myklebust 
1931837bb1d7STrond Myklebust 	ret = filemap_write_and_wait_range(mapping, lstart, lend);
1932837bb1d7STrond Myklebust 	if (ret == 0)
1933837bb1d7STrond Myklebust 		ret = pnfs_sync_inode(mapping->host, true);
1934837bb1d7STrond Myklebust 	return ret;
1935837bb1d7STrond Myklebust }
1936837bb1d7STrond Myklebust EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range);
1937837bb1d7STrond Myklebust 
1938837bb1d7STrond Myklebust /*
1939acdc53b2STrond Myklebust  * flush the inode to disk.
1940acdc53b2STrond Myklebust  */
1941acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
194234901f70STrond Myklebust {
1943f4ce1299STrond Myklebust 	int ret;
194434901f70STrond Myklebust 
1945f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_enter(inode);
1946f4ce1299STrond Myklebust 
19475bb89b47STrond Myklebust 	ret = filemap_write_and_wait(inode->i_mapping);
19486b196875SChuck Lever 	if (ret)
19496b196875SChuck Lever 		goto out;
19505bb89b47STrond Myklebust 	ret = nfs_commit_inode(inode, FLUSH_SYNC);
19516b196875SChuck Lever 	if (ret < 0)
19526b196875SChuck Lever 		goto out;
19535bb89b47STrond Myklebust 	pnfs_sync_inode(inode, true);
19546b196875SChuck Lever 	ret = 0;
1955f4ce1299STrond Myklebust 
19566b196875SChuck Lever out:
1957f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_exit(inode, ret);
1958f4ce1299STrond Myklebust 	return ret;
19591c75950bSTrond Myklebust }
1960ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all);
19611c75950bSTrond Myklebust 
19621b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
19631b3b4a1aSTrond Myklebust {
19641b3b4a1aSTrond Myklebust 	struct nfs_page *req;
19651b3b4a1aSTrond Myklebust 	int ret = 0;
19661b3b4a1aSTrond Myklebust 
1967ba8b06e6STrond Myklebust 	wait_on_page_writeback(page);
19683e217045SWeston Andros Adamson 
19693e217045SWeston Andros Adamson 	/* blocking call to cancel all requests and join to a single (head)
19703e217045SWeston Andros Adamson 	 * request */
19713e217045SWeston Andros Adamson 	req = nfs_lock_and_join_requests(page, false);
19723e217045SWeston Andros Adamson 
19733e217045SWeston Andros Adamson 	if (IS_ERR(req)) {
19743e217045SWeston Andros Adamson 		ret = PTR_ERR(req);
19753e217045SWeston Andros Adamson 	} else if (req) {
19763e217045SWeston Andros Adamson 		/* all requests from this page have been cancelled by
19773e217045SWeston Andros Adamson 		 * nfs_lock_and_join_requests, so just remove the head
19783e217045SWeston Andros Adamson 		 * request from the inode / page_private pointer and
19793e217045SWeston Andros Adamson 		 * release it */
19801b3b4a1aSTrond Myklebust 		nfs_inode_remove_request(req);
19811d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
19821b3b4a1aSTrond Myklebust 	}
19833e217045SWeston Andros Adamson 
19841b3b4a1aSTrond Myklebust 	return ret;
19851b3b4a1aSTrond Myklebust }
19861b3b4a1aSTrond Myklebust 
19871c75950bSTrond Myklebust /*
19881c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
19891c75950bSTrond Myklebust  */
1990d6c843b9SPeng Tao int nfs_wb_single_page(struct inode *inode, struct page *page, bool launder)
19911c75950bSTrond Myklebust {
199229418aa4SMel Gorman 	loff_t range_start = page_file_offset(page);
199309cbfeafSKirill A. Shutemov 	loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1);
19947f2f12d9STrond Myklebust 	struct writeback_control wbc = {
19957f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
19967f2f12d9STrond Myklebust 		.nr_to_write = 0,
19977f2f12d9STrond Myklebust 		.range_start = range_start,
19987f2f12d9STrond Myklebust 		.range_end = range_end,
19997f2f12d9STrond Myklebust 	};
20007f2f12d9STrond Myklebust 	int ret;
20017f2f12d9STrond Myklebust 
2002f4ce1299STrond Myklebust 	trace_nfs_writeback_page_enter(inode);
2003f4ce1299STrond Myklebust 
20040522f6adSTrond Myklebust 	for (;;) {
2005ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
20067f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
2007d6c843b9SPeng Tao 			ret = nfs_writepage_locked(page, &wbc, launder);
20087f2f12d9STrond Myklebust 			if (ret < 0)
20097f2f12d9STrond Myklebust 				goto out_error;
20100522f6adSTrond Myklebust 			continue;
20117f2f12d9STrond Myklebust 		}
2012f4ce1299STrond Myklebust 		ret = 0;
20130522f6adSTrond Myklebust 		if (!PagePrivate(page))
20140522f6adSTrond Myklebust 			break;
20150522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
20167f2f12d9STrond Myklebust 		if (ret < 0)
20177f2f12d9STrond Myklebust 			goto out_error;
20187f2f12d9STrond Myklebust 	}
20197f2f12d9STrond Myklebust out_error:
2020f4ce1299STrond Myklebust 	trace_nfs_writeback_page_exit(inode, ret);
20217f2f12d9STrond Myklebust 	return ret;
20221c75950bSTrond Myklebust }
20231c75950bSTrond Myklebust 
2024074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
2025074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
2026a6bc32b8SMel Gorman 		struct page *page, enum migrate_mode mode)
2027074cc1deSTrond Myklebust {
20282da95652SJeff Layton 	/*
20292da95652SJeff Layton 	 * If PagePrivate is set, then the page is currently associated with
20302da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
20312da95652SJeff Layton 	 *
20322da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
20332da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
20342da95652SJeff Layton 	 *        the page lock.
20352da95652SJeff Layton 	 */
20362da95652SJeff Layton 	if (PagePrivate(page))
20372da95652SJeff Layton 		return -EBUSY;
2038074cc1deSTrond Myklebust 
20398c209ce7SDavid Howells 	if (!nfs_fscache_release_page(page, GFP_KERNEL))
20408c209ce7SDavid Howells 		return -EBUSY;
2041074cc1deSTrond Myklebust 
2042a6bc32b8SMel Gorman 	return migrate_page(mapping, newpage, page, mode);
2043074cc1deSTrond Myklebust }
2044074cc1deSTrond Myklebust #endif
2045074cc1deSTrond Myklebust 
2046f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
20471da177e4SLinus Torvalds {
20481da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
20491e7f3a48SWeston Andros Adamson 					     sizeof(struct nfs_pgio_header),
20501da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
205120c2df83SPaul Mundt 					     NULL);
20521da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
20531da177e4SLinus Torvalds 		return -ENOMEM;
20541da177e4SLinus Torvalds 
205593d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
20561da177e4SLinus Torvalds 						     nfs_wdata_cachep);
20571da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
20583dd4765fSJeff Layton 		goto out_destroy_write_cache;
20591da177e4SLinus Torvalds 
20600b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
20610b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
20620b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
20630b7c0153SFred Isaman 					     NULL);
20640b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
20653dd4765fSJeff Layton 		goto out_destroy_write_mempool;
20660b7c0153SFred Isaman 
206793d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
20684c100210SYanchuan Nian 						      nfs_cdata_cachep);
20691da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
20703dd4765fSJeff Layton 		goto out_destroy_commit_cache;
20711da177e4SLinus Torvalds 
207289a09141SPeter Zijlstra 	/*
207389a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
207489a09141SPeter Zijlstra 	 *
207589a09141SPeter Zijlstra 	 *  64MB:    8192k
207689a09141SPeter Zijlstra 	 * 128MB:   11585k
207789a09141SPeter Zijlstra 	 * 256MB:   16384k
207889a09141SPeter Zijlstra 	 * 512MB:   23170k
207989a09141SPeter Zijlstra 	 *   1GB:   32768k
208089a09141SPeter Zijlstra 	 *   2GB:   46340k
208189a09141SPeter Zijlstra 	 *   4GB:   65536k
208289a09141SPeter Zijlstra 	 *   8GB:   92681k
208389a09141SPeter Zijlstra 	 *  16GB:  131072k
208489a09141SPeter Zijlstra 	 *
208589a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
208689a09141SPeter Zijlstra 	 * Limit the default to 256M
208789a09141SPeter Zijlstra 	 */
208889a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
208989a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
209089a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
209189a09141SPeter Zijlstra 
20921da177e4SLinus Torvalds 	return 0;
20933dd4765fSJeff Layton 
20943dd4765fSJeff Layton out_destroy_commit_cache:
20953dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
20963dd4765fSJeff Layton out_destroy_write_mempool:
20973dd4765fSJeff Layton 	mempool_destroy(nfs_wdata_mempool);
20983dd4765fSJeff Layton out_destroy_write_cache:
20993dd4765fSJeff Layton 	kmem_cache_destroy(nfs_wdata_cachep);
21003dd4765fSJeff Layton 	return -ENOMEM;
21011da177e4SLinus Torvalds }
21021da177e4SLinus Torvalds 
2103266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
21041da177e4SLinus Torvalds {
21051da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
21063dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
21071da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
21081a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
21091da177e4SLinus Torvalds }
21101da177e4SLinus Torvalds 
21114a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops = {
2112a4cdda59SAnna Schumaker 	.rw_mode		= FMODE_WRITE,
21134a0de55cSAnna Schumaker 	.rw_alloc_header	= nfs_writehdr_alloc,
21144a0de55cSAnna Schumaker 	.rw_free_header		= nfs_writehdr_free,
21150eecb214SAnna Schumaker 	.rw_done		= nfs_writeback_done,
21160eecb214SAnna Schumaker 	.rw_result		= nfs_writeback_result,
21171ed26f33SAnna Schumaker 	.rw_initiate		= nfs_initiate_write,
21184a0de55cSAnna Schumaker };
2119