xref: /linux/fs/nfs/write.c (revision 0db10944a76ba09f37d43b99d0fe085a18307f22)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/fs/nfs/write.c
31da177e4SLinus Torvalds  *
47c85d900STrond Myklebust  * Write file data over NFS.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/types.h>
101da177e4SLinus Torvalds #include <linux/slab.h>
111da177e4SLinus Torvalds #include <linux/mm.h>
121da177e4SLinus Torvalds #include <linux/pagemap.h>
131da177e4SLinus Torvalds #include <linux/file.h>
141da177e4SLinus Torvalds #include <linux/writeback.h>
1589a09141SPeter Zijlstra #include <linux/swap.h>
16074cc1deSTrond Myklebust #include <linux/migrate.h>
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
191da177e4SLinus Torvalds #include <linux/nfs_fs.h>
201da177e4SLinus Torvalds #include <linux/nfs_mount.h>
211da177e4SLinus Torvalds #include <linux/nfs_page.h>
223fcfab16SAndrew Morton #include <linux/backing-dev.h>
23afeacc8cSPaul Gortmaker #include <linux/export.h>
24af7cf057STrond Myklebust #include <linux/freezer.h>
25af7cf057STrond Myklebust #include <linux/wait.h>
263fcfab16SAndrew Morton 
277c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #include "delegation.h"
3049a70f27STrond Myklebust #include "internal.h"
3191d5b470SChuck Lever #include "iostat.h"
32def6ed7eSAndy Adamson #include "nfs4_fs.h"
33074cc1deSTrond Myklebust #include "fscache.h"
3494ad1c80SFred Isaman #include "pnfs.h"
351da177e4SLinus Torvalds 
36f4ce1299STrond Myklebust #include "nfstrace.h"
37f4ce1299STrond Myklebust 
381da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #define MIN_POOL_WRITE		(32)
411da177e4SLinus Torvalds #define MIN_POOL_COMMIT		(4)
421da177e4SLinus Torvalds 
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;
1548cd79788SHuang 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 {
266*0db10944SJan Kara 	struct inode *inode = page_file_mapping(page)->host;
267*0db10944SJan Kara 	struct nfs_server *nfss = NFS_SERVER(inode);
2685a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
2695a6d41b3STrond Myklebust 
270deed85e7STrond Myklebust 	WARN_ON_ONCE(ret != 0);
27189a09141SPeter Zijlstra 
272277866a0SPeter Zijlstra 	if (atomic_long_inc_return(&nfss->writeback) >
273*0db10944SJan Kara 			NFS_CONGESTION_ON_THRESH)
274*0db10944SJan Kara 		set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
27589a09141SPeter Zijlstra }
27689a09141SPeter Zijlstra 
27720633f04SWeston Andros Adamson static void nfs_end_page_writeback(struct nfs_page *req)
27889a09141SPeter Zijlstra {
27920633f04SWeston Andros Adamson 	struct inode *inode = page_file_mapping(req->wb_page)->host;
28089a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
28189a09141SPeter Zijlstra 
28220633f04SWeston Andros Adamson 	if (!nfs_page_group_sync_on_bit(req, PG_WB_END))
28320633f04SWeston Andros Adamson 		return;
28420633f04SWeston Andros Adamson 
28520633f04SWeston Andros Adamson 	end_page_writeback(req->wb_page);
286c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
287*0db10944SJan Kara 		clear_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
28889a09141SPeter Zijlstra }
28989a09141SPeter Zijlstra 
290d4581383SWeston Andros Adamson 
291d4581383SWeston Andros Adamson /* nfs_page_group_clear_bits
292d4581383SWeston Andros Adamson  *   @req - an nfs request
293d4581383SWeston Andros Adamson  * clears all page group related bits from @req
294d4581383SWeston Andros Adamson  */
295d4581383SWeston Andros Adamson static void
296d4581383SWeston Andros Adamson nfs_page_group_clear_bits(struct nfs_page *req)
297e261f51fSTrond Myklebust {
298d4581383SWeston Andros Adamson 	clear_bit(PG_TEARDOWN, &req->wb_flags);
299d4581383SWeston Andros Adamson 	clear_bit(PG_UNLOCKPAGE, &req->wb_flags);
300d4581383SWeston Andros Adamson 	clear_bit(PG_UPTODATE, &req->wb_flags);
301d4581383SWeston Andros Adamson 	clear_bit(PG_WB_END, &req->wb_flags);
302d4581383SWeston Andros Adamson 	clear_bit(PG_REMOVE, &req->wb_flags);
303d4581383SWeston Andros Adamson }
304d4581383SWeston Andros Adamson 
305d4581383SWeston Andros Adamson 
306d4581383SWeston Andros Adamson /*
307d4581383SWeston Andros Adamson  * nfs_unroll_locks_and_wait -  unlock all newly locked reqs and wait on @req
308d4581383SWeston Andros Adamson  *
309d4581383SWeston Andros Adamson  * this is a helper function for nfs_lock_and_join_requests
310d4581383SWeston Andros Adamson  *
311d4581383SWeston Andros Adamson  * @inode - inode associated with request page group, must be holding inode lock
312d4581383SWeston Andros Adamson  * @head  - head request of page group, must be holding head lock
313d4581383SWeston Andros Adamson  * @req   - request that couldn't lock and needs to wait on the req bit lock
314d4581383SWeston Andros Adamson  * @nonblock - if true, don't actually wait
315d4581383SWeston Andros Adamson  *
316d4581383SWeston Andros Adamson  * NOTE: this must be called holding page_group bit lock and inode spin lock
317d4581383SWeston Andros Adamson  *       and BOTH will be released before returning.
318d4581383SWeston Andros Adamson  *
319d4581383SWeston Andros Adamson  * returns 0 on success, < 0 on error.
320d4581383SWeston Andros Adamson  */
321d4581383SWeston Andros Adamson static int
322d4581383SWeston Andros Adamson nfs_unroll_locks_and_wait(struct inode *inode, struct nfs_page *head,
323d4581383SWeston Andros Adamson 			  struct nfs_page *req, bool nonblock)
324d4581383SWeston Andros Adamson 	__releases(&inode->i_lock)
325d4581383SWeston Andros Adamson {
326d4581383SWeston Andros Adamson 	struct nfs_page *tmp;
327e261f51fSTrond Myklebust 	int ret;
328e261f51fSTrond Myklebust 
329d4581383SWeston Andros Adamson 	/* relinquish all the locks successfully grabbed this run */
330d4581383SWeston Andros Adamson 	for (tmp = head ; tmp != req; tmp = tmp->wb_this_page)
331d4581383SWeston Andros Adamson 		nfs_unlock_request(tmp);
332d4581383SWeston Andros Adamson 
333d4581383SWeston Andros Adamson 	WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
334d4581383SWeston Andros Adamson 
335d4581383SWeston Andros Adamson 	/* grab a ref on the request that will be waited on */
336d4581383SWeston Andros Adamson 	kref_get(&req->wb_kref);
337d4581383SWeston Andros Adamson 
338d4581383SWeston Andros Adamson 	nfs_page_group_unlock(head);
339587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
340d4581383SWeston Andros Adamson 
341d4581383SWeston Andros Adamson 	/* release ref from nfs_page_find_head_request_locked */
342d4581383SWeston Andros Adamson 	nfs_release_request(head);
343d4581383SWeston Andros Adamson 
344cfb506e1STrond Myklebust 	if (!nonblock)
345e261f51fSTrond Myklebust 		ret = nfs_wait_on_request(req);
346cfb506e1STrond Myklebust 	else
347cfb506e1STrond Myklebust 		ret = -EAGAIN;
348e261f51fSTrond Myklebust 	nfs_release_request(req);
349d4581383SWeston Andros Adamson 
350d4581383SWeston Andros Adamson 	return ret;
351e261f51fSTrond Myklebust }
352d4581383SWeston Andros Adamson 
353d4581383SWeston Andros Adamson /*
354d4581383SWeston Andros Adamson  * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests
355d4581383SWeston Andros Adamson  *
356d4581383SWeston Andros Adamson  * @destroy_list - request list (using wb_this_page) terminated by @old_head
357d4581383SWeston Andros Adamson  * @old_head - the old head of the list
358d4581383SWeston Andros Adamson  *
359d4581383SWeston Andros Adamson  * All subrequests must be locked and removed from all lists, so at this point
360d4581383SWeston Andros Adamson  * they are only "active" in this function, and possibly in nfs_wait_on_request
361d4581383SWeston Andros Adamson  * with a reference held by some other context.
362d4581383SWeston Andros Adamson  */
363d4581383SWeston Andros Adamson static void
364d4581383SWeston Andros Adamson nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list,
365d4581383SWeston Andros Adamson 				 struct nfs_page *old_head)
366d4581383SWeston Andros Adamson {
367d4581383SWeston Andros Adamson 	while (destroy_list) {
368d4581383SWeston Andros Adamson 		struct nfs_page *subreq = destroy_list;
369d4581383SWeston Andros Adamson 
370d4581383SWeston Andros Adamson 		destroy_list = (subreq->wb_this_page == old_head) ?
371d4581383SWeston Andros Adamson 				   NULL : subreq->wb_this_page;
372d4581383SWeston Andros Adamson 
373d4581383SWeston Andros Adamson 		WARN_ON_ONCE(old_head != subreq->wb_head);
374d4581383SWeston Andros Adamson 
375d4581383SWeston Andros Adamson 		/* make sure old group is not used */
376d4581383SWeston Andros Adamson 		subreq->wb_head = subreq;
377d4581383SWeston Andros Adamson 		subreq->wb_this_page = subreq;
378d4581383SWeston Andros Adamson 
379d4581383SWeston Andros Adamson 		/* subreq is now totally disconnected from page group or any
380d4581383SWeston Andros Adamson 		 * write / commit lists. last chance to wake any waiters */
381d4581383SWeston Andros Adamson 		nfs_unlock_request(subreq);
382d4581383SWeston Andros Adamson 
383d4581383SWeston Andros Adamson 		if (!test_bit(PG_TEARDOWN, &subreq->wb_flags)) {
384d4581383SWeston Andros Adamson 			/* release ref on old head request */
385d4581383SWeston Andros Adamson 			nfs_release_request(old_head);
386d4581383SWeston Andros Adamson 
387d4581383SWeston Andros Adamson 			nfs_page_group_clear_bits(subreq);
388d4581383SWeston Andros Adamson 
389d4581383SWeston Andros Adamson 			/* release the PG_INODE_REF reference */
390d4581383SWeston Andros Adamson 			if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags))
391d4581383SWeston Andros Adamson 				nfs_release_request(subreq);
392d4581383SWeston Andros Adamson 			else
393d4581383SWeston Andros Adamson 				WARN_ON_ONCE(1);
394d4581383SWeston Andros Adamson 		} else {
395d4581383SWeston Andros Adamson 			WARN_ON_ONCE(test_bit(PG_CLEAN, &subreq->wb_flags));
396d4581383SWeston Andros Adamson 			/* zombie requests have already released the last
397d4581383SWeston Andros Adamson 			 * reference and were waiting on the rest of the
398d4581383SWeston Andros Adamson 			 * group to complete. Since it's no longer part of a
399d4581383SWeston Andros Adamson 			 * group, simply free the request */
400d4581383SWeston Andros Adamson 			nfs_page_group_clear_bits(subreq);
401d4581383SWeston Andros Adamson 			nfs_free_request(subreq);
402d4581383SWeston Andros Adamson 		}
403d4581383SWeston Andros Adamson 	}
404d4581383SWeston Andros Adamson }
405d4581383SWeston Andros Adamson 
406d4581383SWeston Andros Adamson /*
407d4581383SWeston Andros Adamson  * nfs_lock_and_join_requests - join all subreqs to the head req and return
408d4581383SWeston Andros Adamson  *                              a locked reference, cancelling any pending
409d4581383SWeston Andros Adamson  *                              operations for this page.
410d4581383SWeston Andros Adamson  *
411d4581383SWeston Andros Adamson  * @page - the page used to lookup the "page group" of nfs_page structures
412d4581383SWeston Andros Adamson  * @nonblock - if true, don't block waiting for request locks
413d4581383SWeston Andros Adamson  *
414d4581383SWeston Andros Adamson  * This function joins all sub requests to the head request by first
415d4581383SWeston Andros Adamson  * locking all requests in the group, cancelling any pending operations
416d4581383SWeston Andros Adamson  * and finally updating the head request to cover the whole range covered by
417d4581383SWeston Andros Adamson  * the (former) group.  All subrequests are removed from any write or commit
418d4581383SWeston Andros Adamson  * lists, unlinked from the group and destroyed.
419d4581383SWeston Andros Adamson  *
420d4581383SWeston Andros Adamson  * Returns a locked, referenced pointer to the head request - which after
421d4581383SWeston Andros Adamson  * this call is guaranteed to be the only request associated with the page.
422d4581383SWeston Andros Adamson  * Returns NULL if no requests are found for @page, or a ERR_PTR if an
423d4581383SWeston Andros Adamson  * error was encountered.
424d4581383SWeston Andros Adamson  */
425d4581383SWeston Andros Adamson static struct nfs_page *
426d4581383SWeston Andros Adamson nfs_lock_and_join_requests(struct page *page, bool nonblock)
427d4581383SWeston Andros Adamson {
428d4581383SWeston Andros Adamson 	struct inode *inode = page_file_mapping(page)->host;
429d4581383SWeston Andros Adamson 	struct nfs_page *head, *subreq;
430d4581383SWeston Andros Adamson 	struct nfs_page *destroy_list = NULL;
431d4581383SWeston Andros Adamson 	unsigned int total_bytes;
432d4581383SWeston Andros Adamson 	int ret;
433d4581383SWeston Andros Adamson 
434d4581383SWeston Andros Adamson try_again:
435d4581383SWeston Andros Adamson 	total_bytes = 0;
436d4581383SWeston Andros Adamson 
437d4581383SWeston Andros Adamson 	WARN_ON_ONCE(destroy_list);
438d4581383SWeston Andros Adamson 
439d4581383SWeston Andros Adamson 	spin_lock(&inode->i_lock);
440d4581383SWeston Andros Adamson 
441d4581383SWeston Andros Adamson 	/*
442d4581383SWeston Andros Adamson 	 * A reference is taken only on the head request which acts as a
443d4581383SWeston Andros Adamson 	 * reference to the whole page group - the group will not be destroyed
444d4581383SWeston Andros Adamson 	 * until the head reference is released.
445d4581383SWeston Andros Adamson 	 */
446d4581383SWeston Andros Adamson 	head = nfs_page_find_head_request_locked(NFS_I(inode), page);
447d4581383SWeston Andros Adamson 
448d4581383SWeston Andros Adamson 	if (!head) {
449587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
450d4581383SWeston Andros Adamson 		return NULL;
451d4581383SWeston Andros Adamson 	}
452d4581383SWeston Andros Adamson 
4537c3af975SWeston Andros Adamson 	/* holding inode lock, so always make a non-blocking call to try the
4547c3af975SWeston Andros Adamson 	 * page group lock */
455fd2f3a06SWeston Andros Adamson 	ret = nfs_page_group_lock(head, true);
45694970014SWeston Andros Adamson 	if (ret < 0) {
45794970014SWeston Andros Adamson 		spin_unlock(&inode->i_lock);
4587c3af975SWeston Andros Adamson 
4597c3af975SWeston Andros Adamson 		if (!nonblock && ret == -EAGAIN) {
4607c3af975SWeston Andros Adamson 			nfs_page_group_lock_wait(head);
4617c3af975SWeston Andros Adamson 			nfs_release_request(head);
4627c3af975SWeston Andros Adamson 			goto try_again;
4637c3af975SWeston Andros Adamson 		}
4647c3af975SWeston Andros Adamson 
46594970014SWeston Andros Adamson 		nfs_release_request(head);
466e7029206SWeston Andros Adamson 		return ERR_PTR(ret);
46794970014SWeston Andros Adamson 	}
4687c3af975SWeston Andros Adamson 
4697c3af975SWeston Andros Adamson 	/* lock each request in the page group */
470d4581383SWeston Andros Adamson 	subreq = head;
471d4581383SWeston Andros Adamson 	do {
472d4581383SWeston Andros Adamson 		/*
473d4581383SWeston Andros Adamson 		 * Subrequests are always contiguous, non overlapping
474309a1d65SWeston Andros Adamson 		 * and in order - but may be repeated (mirrored writes).
475d4581383SWeston Andros Adamson 		 */
476309a1d65SWeston Andros Adamson 		if (subreq->wb_offset == (head->wb_offset + total_bytes)) {
477d4581383SWeston Andros Adamson 			/* keep track of how many bytes this group covers */
478d4581383SWeston Andros Adamson 			total_bytes += subreq->wb_bytes;
479309a1d65SWeston Andros Adamson 		} else if (WARN_ON_ONCE(subreq->wb_offset < head->wb_offset ||
480309a1d65SWeston Andros Adamson 			    ((subreq->wb_offset + subreq->wb_bytes) >
481309a1d65SWeston Andros Adamson 			     (head->wb_offset + total_bytes)))) {
482309a1d65SWeston Andros Adamson 			nfs_page_group_unlock(head);
483309a1d65SWeston Andros Adamson 			spin_unlock(&inode->i_lock);
484309a1d65SWeston Andros Adamson 			return ERR_PTR(-EIO);
485309a1d65SWeston Andros Adamson 		}
486d4581383SWeston Andros Adamson 
487d4581383SWeston Andros Adamson 		if (!nfs_lock_request(subreq)) {
488d4581383SWeston Andros Adamson 			/* releases page group bit lock and
489d4581383SWeston Andros Adamson 			 * inode spin lock and all references */
490d4581383SWeston Andros Adamson 			ret = nfs_unroll_locks_and_wait(inode, head,
491d4581383SWeston Andros Adamson 				subreq, nonblock);
492d4581383SWeston Andros Adamson 
493d4581383SWeston Andros Adamson 			if (ret == 0)
494d4581383SWeston Andros Adamson 				goto try_again;
495d4581383SWeston Andros Adamson 
496d4581383SWeston Andros Adamson 			return ERR_PTR(ret);
497d4581383SWeston Andros Adamson 		}
498d4581383SWeston Andros Adamson 
499d4581383SWeston Andros Adamson 		subreq = subreq->wb_this_page;
500d4581383SWeston Andros Adamson 	} while (subreq != head);
501d4581383SWeston Andros Adamson 
502d4581383SWeston Andros Adamson 	/* Now that all requests are locked, make sure they aren't on any list.
503d4581383SWeston Andros Adamson 	 * Commit list removal accounting is done after locks are dropped */
504d4581383SWeston Andros Adamson 	subreq = head;
505d4581383SWeston Andros Adamson 	do {
506411a99adSWeston Andros Adamson 		nfs_clear_request_commit(subreq);
507d4581383SWeston Andros Adamson 		subreq = subreq->wb_this_page;
508d4581383SWeston Andros Adamson 	} while (subreq != head);
509d4581383SWeston Andros Adamson 
510d4581383SWeston Andros Adamson 	/* unlink subrequests from head, destroy them later */
511d4581383SWeston Andros Adamson 	if (head->wb_this_page != head) {
512d4581383SWeston Andros Adamson 		/* destroy list will be terminated by head */
513d4581383SWeston Andros Adamson 		destroy_list = head->wb_this_page;
514d4581383SWeston Andros Adamson 		head->wb_this_page = head;
515d4581383SWeston Andros Adamson 
516d4581383SWeston Andros Adamson 		/* change head request to cover whole range that
517d4581383SWeston Andros Adamson 		 * the former page group covered */
518d4581383SWeston Andros Adamson 		head->wb_bytes = total_bytes;
519d4581383SWeston Andros Adamson 	}
520d4581383SWeston Andros Adamson 
521d4581383SWeston Andros Adamson 	/*
522d4581383SWeston Andros Adamson 	 * prepare head request to be added to new pgio descriptor
523d4581383SWeston Andros Adamson 	 */
524d4581383SWeston Andros Adamson 	nfs_page_group_clear_bits(head);
525d4581383SWeston Andros Adamson 
526d4581383SWeston Andros Adamson 	/*
527d4581383SWeston Andros Adamson 	 * some part of the group was still on the inode list - otherwise
528d4581383SWeston Andros Adamson 	 * the group wouldn't be involved in async write.
529d4581383SWeston Andros Adamson 	 * grab a reference for the head request, iff it needs one.
530d4581383SWeston Andros Adamson 	 */
531d4581383SWeston Andros Adamson 	if (!test_and_set_bit(PG_INODE_REF, &head->wb_flags))
532d4581383SWeston Andros Adamson 		kref_get(&head->wb_kref);
533d4581383SWeston Andros Adamson 
534d4581383SWeston Andros Adamson 	nfs_page_group_unlock(head);
535d4581383SWeston Andros Adamson 
536411a99adSWeston Andros Adamson 	/* drop lock to clean uprequests on destroy list */
537d4581383SWeston Andros Adamson 	spin_unlock(&inode->i_lock);
538d4581383SWeston Andros Adamson 
539d4581383SWeston Andros Adamson 	nfs_destroy_unlinked_subrequests(destroy_list, head);
540d4581383SWeston Andros Adamson 
541d4581383SWeston Andros Adamson 	/* still holds ref on head from nfs_page_find_head_request_locked
542d4581383SWeston Andros Adamson 	 * and still has lock on head from lock loop */
543d4581383SWeston Andros Adamson 	return head;
544612c9384STrond Myklebust }
545074cc1deSTrond Myklebust 
5460bcbf039SPeng Tao static void nfs_write_error_remove_page(struct nfs_page *req)
5470bcbf039SPeng Tao {
5480bcbf039SPeng Tao 	nfs_unlock_request(req);
5490bcbf039SPeng Tao 	nfs_end_page_writeback(req);
5500bcbf039SPeng Tao 	nfs_release_request(req);
5510bcbf039SPeng Tao 	generic_error_remove_page(page_file_mapping(req->wb_page),
5520bcbf039SPeng Tao 				  req->wb_page);
5530bcbf039SPeng Tao }
5540bcbf039SPeng Tao 
555074cc1deSTrond Myklebust /*
556074cc1deSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
557074cc1deSTrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
558074cc1deSTrond Myklebust  */
559074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
560d6c843b9SPeng Tao 				struct page *page, bool nonblock,
561d6c843b9SPeng Tao 				bool launder)
562074cc1deSTrond Myklebust {
563074cc1deSTrond Myklebust 	struct nfs_page *req;
564074cc1deSTrond Myklebust 	int ret = 0;
565074cc1deSTrond Myklebust 
566d4581383SWeston Andros Adamson 	req = nfs_lock_and_join_requests(page, nonblock);
567074cc1deSTrond Myklebust 	if (!req)
568074cc1deSTrond Myklebust 		goto out;
569074cc1deSTrond Myklebust 	ret = PTR_ERR(req);
570074cc1deSTrond Myklebust 	if (IS_ERR(req))
571074cc1deSTrond Myklebust 		goto out;
572074cc1deSTrond Myklebust 
573deed85e7STrond Myklebust 	nfs_set_page_writeback(page);
574deed85e7STrond Myklebust 	WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
575074cc1deSTrond Myklebust 
576deed85e7STrond Myklebust 	ret = 0;
577f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
578074cc1deSTrond Myklebust 		ret = pgio->pg_error;
5790bcbf039SPeng Tao 		/*
580d6c843b9SPeng Tao 		 * Remove the problematic req upon fatal errors
581d6c843b9SPeng Tao 		 * in launder case, while other dirty pages can
582d6c843b9SPeng Tao 		 * still be around until they get flushed.
5830bcbf039SPeng Tao 		 */
5840bcbf039SPeng Tao 		if (nfs_error_is_fatal(ret)) {
5850bcbf039SPeng Tao 			nfs_context_set_write_error(req->wb_context, ret);
586d6c843b9SPeng Tao 			if (launder) {
5870bcbf039SPeng Tao 				nfs_write_error_remove_page(req);
588d6c843b9SPeng Tao 				goto out;
589d6c843b9SPeng Tao 			}
590d6c843b9SPeng Tao 		}
5910bcbf039SPeng Tao 		nfs_redirty_request(req);
5920bcbf039SPeng Tao 		ret = -EAGAIN;
59340f90271STrond Myklebust 	} else
59440f90271STrond Myklebust 		nfs_add_stats(page_file_mapping(page)->host,
59540f90271STrond Myklebust 				NFSIOS_WRITEPAGES, 1);
596074cc1deSTrond Myklebust out:
597074cc1deSTrond Myklebust 	return ret;
598e261f51fSTrond Myklebust }
599e261f51fSTrond Myklebust 
600d6c843b9SPeng Tao static int nfs_do_writepage(struct page *page, struct writeback_control *wbc,
601d6c843b9SPeng Tao 			    struct nfs_pageio_descriptor *pgio, bool launder)
602f758c885STrond Myklebust {
603cfb506e1STrond Myklebust 	int ret;
604f758c885STrond Myklebust 
6058cd79788SHuang Ying 	nfs_pageio_cond_complete(pgio, page_index(page));
606d6c843b9SPeng Tao 	ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE,
607d6c843b9SPeng Tao 				   launder);
608cfb506e1STrond Myklebust 	if (ret == -EAGAIN) {
609cfb506e1STrond Myklebust 		redirty_page_for_writepage(wbc, page);
610cfb506e1STrond Myklebust 		ret = 0;
611cfb506e1STrond Myklebust 	}
612cfb506e1STrond Myklebust 	return ret;
613f758c885STrond Myklebust }
614f758c885STrond Myklebust 
615e261f51fSTrond Myklebust /*
6161da177e4SLinus Torvalds  * Write an mmapped page to the server.
6171da177e4SLinus Torvalds  */
618d6c843b9SPeng Tao static int nfs_writepage_locked(struct page *page,
619d6c843b9SPeng Tao 				struct writeback_control *wbc,
620d6c843b9SPeng Tao 				bool launder)
6211da177e4SLinus Torvalds {
622f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
62340f90271STrond Myklebust 	struct inode *inode = page_file_mapping(page)->host;
624e261f51fSTrond Myklebust 	int err;
6251da177e4SLinus Torvalds 
62640f90271STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
627811ed92eSTrond Myklebust 	nfs_pageio_init_write(&pgio, inode, 0,
628a20c93e3SChristoph Hellwig 				false, &nfs_async_write_completion_ops);
629d6c843b9SPeng Tao 	err = nfs_do_writepage(page, wbc, &pgio, launder);
630f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
631f758c885STrond Myklebust 	if (err < 0)
6324d770ccfSTrond Myklebust 		return err;
633f758c885STrond Myklebust 	if (pgio.pg_error < 0)
634f758c885STrond Myklebust 		return pgio.pg_error;
635f758c885STrond Myklebust 	return 0;
6364d770ccfSTrond Myklebust }
6374d770ccfSTrond Myklebust 
6384d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
6394d770ccfSTrond Myklebust {
640f758c885STrond Myklebust 	int ret;
6414d770ccfSTrond Myklebust 
642d6c843b9SPeng Tao 	ret = nfs_writepage_locked(page, wbc, false);
6431da177e4SLinus Torvalds 	unlock_page(page);
644f758c885STrond Myklebust 	return ret;
645f758c885STrond Myklebust }
646f758c885STrond Myklebust 
647f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
648f758c885STrond Myklebust {
649f758c885STrond Myklebust 	int ret;
650f758c885STrond Myklebust 
651d6c843b9SPeng Tao 	ret = nfs_do_writepage(page, wbc, data, false);
652f758c885STrond Myklebust 	unlock_page(page);
653f758c885STrond Myklebust 	return ret;
6541da177e4SLinus Torvalds }
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
6571da177e4SLinus Torvalds {
6581da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
659c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
6601da177e4SLinus Torvalds 	int err;
6611da177e4SLinus Torvalds 
66291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
66391d5b470SChuck Lever 
664a20c93e3SChristoph Hellwig 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false,
665a20c93e3SChristoph Hellwig 				&nfs_async_write_completion_ops);
666f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
667c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
66872cb77f4STrond Myklebust 
669f758c885STrond Myklebust 	if (err < 0)
67072cb77f4STrond Myklebust 		goto out_err;
67172cb77f4STrond Myklebust 	err = pgio.pg_error;
67272cb77f4STrond Myklebust 	if (err < 0)
67372cb77f4STrond Myklebust 		goto out_err;
674c63c7b05STrond Myklebust 	return 0;
67572cb77f4STrond Myklebust out_err:
67672cb77f4STrond Myklebust 	return err;
6771da177e4SLinus Torvalds }
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds /*
6801da177e4SLinus Torvalds  * Insert a write request into an inode
6811da177e4SLinus Torvalds  */
682d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
6831da177e4SLinus Torvalds {
6841da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
685e7d39069STrond Myklebust 
6862bfc6e56SWeston Andros Adamson 	WARN_ON_ONCE(req->wb_this_page != req);
6872bfc6e56SWeston Andros Adamson 
688e7d39069STrond Myklebust 	/* Lock the request! */
6897ad84aa9STrond Myklebust 	nfs_lock_request(req);
690e7d39069STrond Myklebust 
691e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
692cb1410c7SWeston Andros Adamson 	if (!nfsi->nrequests &&
693cb1410c7SWeston Andros Adamson 	    NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
694a9a4a87aSTrond Myklebust 		inode->i_version++;
69529418aa4SMel Gorman 	/*
69629418aa4SMel Gorman 	 * Swap-space should not get truncated. Hence no need to plug the race
69729418aa4SMel Gorman 	 * with invalidate/truncate.
69829418aa4SMel Gorman 	 */
69929418aa4SMel Gorman 	if (likely(!PageSwapCache(req->wb_page))) {
7002df485a7STrond Myklebust 		set_bit(PG_MAPPED, &req->wb_flags);
701deb7d638STrond Myklebust 		SetPagePrivate(req->wb_page);
702277459d2STrond Myklebust 		set_page_private(req->wb_page, (unsigned long)req);
70329418aa4SMel Gorman 	}
704cb1410c7SWeston Andros Adamson 	nfsi->nrequests++;
70517089a29SWeston Andros Adamson 	/* this a head request for a page group - mark it as having an
706cb1410c7SWeston Andros Adamson 	 * extra reference so sub groups can follow suit.
707cb1410c7SWeston Andros Adamson 	 * This flag also informs pgio layer when to bump nrequests when
708cb1410c7SWeston Andros Adamson 	 * adding subrequests. */
70917089a29SWeston Andros Adamson 	WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags));
710c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
711e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
7121da177e4SLinus Torvalds }
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds /*
71589a09141SPeter Zijlstra  * Remove a write request from an inode
7161da177e4SLinus Torvalds  */
7171da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
7181da177e4SLinus Torvalds {
7192b0143b5SDavid Howells 	struct inode *inode = d_inode(req->wb_context->dentry);
7201da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
72120633f04SWeston Andros Adamson 	struct nfs_page *head;
72220633f04SWeston Andros Adamson 
72320633f04SWeston Andros Adamson 	if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) {
72420633f04SWeston Andros Adamson 		head = req->wb_head;
7251da177e4SLinus Torvalds 
726587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
72767911c8fSAnna Schumaker 		if (likely(head->wb_page && !PageSwapCache(head->wb_page))) {
72820633f04SWeston Andros Adamson 			set_page_private(head->wb_page, 0);
72920633f04SWeston Andros Adamson 			ClearPagePrivate(head->wb_page);
73020633f04SWeston Andros Adamson 			clear_bit(PG_MAPPED, &head->wb_flags);
73129418aa4SMel Gorman 		}
732cb1410c7SWeston Andros Adamson 		nfsi->nrequests--;
733cb1410c7SWeston Andros Adamson 		spin_unlock(&inode->i_lock);
734cb1410c7SWeston Andros Adamson 	} else {
735cb1410c7SWeston Andros Adamson 		spin_lock(&inode->i_lock);
736cb1410c7SWeston Andros Adamson 		nfsi->nrequests--;
737587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
73820633f04SWeston Andros Adamson 	}
73917089a29SWeston Andros Adamson 
74017089a29SWeston Andros Adamson 	if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags))
7411da177e4SLinus Torvalds 		nfs_release_request(req);
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
74461822ab5STrond Myklebust static void
7456d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
74661822ab5STrond Myklebust {
74767911c8fSAnna Schumaker 	if (req->wb_page)
74861822ab5STrond Myklebust 		__set_page_dirty_nobuffers(req->wb_page);
74961822ab5STrond Myklebust }
75061822ab5STrond Myklebust 
7513a3908c8STrond Myklebust /*
7523a3908c8STrond Myklebust  * nfs_page_search_commits_for_head_request_locked
7533a3908c8STrond Myklebust  *
7543a3908c8STrond Myklebust  * Search through commit lists on @inode for the head request for @page.
7553a3908c8STrond Myklebust  * Must be called while holding the inode (which is cinfo) lock.
7563a3908c8STrond Myklebust  *
7573a3908c8STrond Myklebust  * Returns the head request if found, or NULL if not found.
7583a3908c8STrond Myklebust  */
7593a3908c8STrond Myklebust static struct nfs_page *
7603a3908c8STrond Myklebust nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
7613a3908c8STrond Myklebust 						struct page *page)
7623a3908c8STrond Myklebust {
7633a3908c8STrond Myklebust 	struct nfs_page *freq, *t;
7643a3908c8STrond Myklebust 	struct nfs_commit_info cinfo;
7653a3908c8STrond Myklebust 	struct inode *inode = &nfsi->vfs_inode;
7663a3908c8STrond Myklebust 
7673a3908c8STrond Myklebust 	nfs_init_cinfo_from_inode(&cinfo, inode);
7683a3908c8STrond Myklebust 
7693a3908c8STrond Myklebust 	/* search through pnfs commit lists */
7703a3908c8STrond Myklebust 	freq = pnfs_search_commit_reqs(inode, &cinfo, page);
7713a3908c8STrond Myklebust 	if (freq)
7723a3908c8STrond Myklebust 		return freq->wb_head;
7733a3908c8STrond Myklebust 
7743a3908c8STrond Myklebust 	/* Linearly search the commit list for the correct request */
7753a3908c8STrond Myklebust 	list_for_each_entry_safe(freq, t, &cinfo.mds->list, wb_list) {
7763a3908c8STrond Myklebust 		if (freq->wb_page == page)
7773a3908c8STrond Myklebust 			return freq->wb_head;
7783a3908c8STrond Myklebust 	}
7793a3908c8STrond Myklebust 
7803a3908c8STrond Myklebust 	return NULL;
7813a3908c8STrond Myklebust }
7823a3908c8STrond Myklebust 
7838dd37758STrond Myklebust /**
78486d80f97STrond Myklebust  * nfs_request_add_commit_list_locked - add request to a commit list
78586d80f97STrond Myklebust  * @req: pointer to a struct nfs_page
78686d80f97STrond Myklebust  * @dst: commit list head
78786d80f97STrond Myklebust  * @cinfo: holds list lock and accounting info
78886d80f97STrond Myklebust  *
78986d80f97STrond Myklebust  * This sets the PG_CLEAN bit, updates the cinfo count of
79086d80f97STrond Myklebust  * number of outstanding requests requiring a commit as well as
79186d80f97STrond Myklebust  * the MM page stats.
79286d80f97STrond Myklebust  *
793fe238e60SDave Wysochanski  * The caller must hold cinfo->inode->i_lock, and the nfs_page lock.
79486d80f97STrond Myklebust  */
79586d80f97STrond Myklebust void
79686d80f97STrond Myklebust nfs_request_add_commit_list_locked(struct nfs_page *req, struct list_head *dst,
79786d80f97STrond Myklebust 			    struct nfs_commit_info *cinfo)
79886d80f97STrond Myklebust {
79986d80f97STrond Myklebust 	set_bit(PG_CLEAN, &req->wb_flags);
80086d80f97STrond Myklebust 	nfs_list_add_request(req, dst);
80186d80f97STrond Myklebust 	cinfo->mds->ncommit++;
80286d80f97STrond Myklebust }
80386d80f97STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked);
80486d80f97STrond Myklebust 
80586d80f97STrond Myklebust /**
8068dd37758STrond Myklebust  * nfs_request_add_commit_list - add request to a commit list
8078dd37758STrond Myklebust  * @req: pointer to a struct nfs_page
808ea2cf228SFred Isaman  * @dst: commit list head
809ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
8108dd37758STrond Myklebust  *
811ea2cf228SFred Isaman  * This sets the PG_CLEAN bit, updates the cinfo count of
8128dd37758STrond Myklebust  * number of outstanding requests requiring a commit as well as
8138dd37758STrond Myklebust  * the MM page stats.
8148dd37758STrond Myklebust  *
815ea2cf228SFred Isaman  * The caller must _not_ hold the cinfo->lock, but must be
8168dd37758STrond Myklebust  * holding the nfs_page lock.
8178dd37758STrond Myklebust  */
8188dd37758STrond Myklebust void
8196272dcc6SAnna Schumaker nfs_request_add_commit_list(struct nfs_page *req, struct nfs_commit_info *cinfo)
8208dd37758STrond Myklebust {
821fe238e60SDave Wysochanski 	spin_lock(&cinfo->inode->i_lock);
8226272dcc6SAnna Schumaker 	nfs_request_add_commit_list_locked(req, &cinfo->mds->list, cinfo);
823fe238e60SDave Wysochanski 	spin_unlock(&cinfo->inode->i_lock);
82467911c8fSAnna Schumaker 	if (req->wb_page)
82586d80f97STrond Myklebust 		nfs_mark_page_unstable(req->wb_page, cinfo);
8268dd37758STrond Myklebust }
8278dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
8288dd37758STrond Myklebust 
8298dd37758STrond Myklebust /**
8308dd37758STrond Myklebust  * nfs_request_remove_commit_list - Remove request from a commit list
8318dd37758STrond Myklebust  * @req: pointer to a nfs_page
832ea2cf228SFred Isaman  * @cinfo: holds list lock and accounting info
8338dd37758STrond Myklebust  *
834ea2cf228SFred Isaman  * This clears the PG_CLEAN bit, and updates the cinfo's count of
8358dd37758STrond Myklebust  * number of outstanding requests requiring a commit
8368dd37758STrond Myklebust  * It does not update the MM page stats.
8378dd37758STrond Myklebust  *
838ea2cf228SFred Isaman  * The caller _must_ hold the cinfo->lock and the nfs_page lock.
8398dd37758STrond Myklebust  */
8408dd37758STrond Myklebust void
841ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req,
842ea2cf228SFred Isaman 			       struct nfs_commit_info *cinfo)
8438dd37758STrond Myklebust {
8448dd37758STrond Myklebust 	if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
8458dd37758STrond Myklebust 		return;
8468dd37758STrond Myklebust 	nfs_list_remove_request(req);
847ea2cf228SFred Isaman 	cinfo->mds->ncommit--;
8488dd37758STrond Myklebust }
8498dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
8508dd37758STrond Myklebust 
851ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
852ea2cf228SFred Isaman 				      struct inode *inode)
853ea2cf228SFred Isaman {
854fe238e60SDave Wysochanski 	cinfo->inode = inode;
855ea2cf228SFred Isaman 	cinfo->mds = &NFS_I(inode)->commit_info;
856ea2cf228SFred Isaman 	cinfo->ds = pnfs_get_ds_info(inode);
857b359f9d0SFred Isaman 	cinfo->dreq = NULL;
858f453a54aSFred Isaman 	cinfo->completion_ops = &nfs_commit_completion_ops;
859ea2cf228SFred Isaman }
860ea2cf228SFred Isaman 
861ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo,
862ea2cf228SFred Isaman 		    struct inode *inode,
863ea2cf228SFred Isaman 		    struct nfs_direct_req *dreq)
864ea2cf228SFred Isaman {
8651763da12SFred Isaman 	if (dreq)
8661763da12SFred Isaman 		nfs_init_cinfo_from_dreq(cinfo, dreq);
8671763da12SFred Isaman 	else
868ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(cinfo, inode);
869ea2cf228SFred Isaman }
870ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo);
8718dd37758STrond Myklebust 
8721da177e4SLinus Torvalds /*
8731da177e4SLinus Torvalds  * Add a request to the inode's commit list.
8741da177e4SLinus Torvalds  */
8751763da12SFred Isaman void
876ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
877b57ff130SWeston Andros Adamson 			struct nfs_commit_info *cinfo, u32 ds_commit_idx)
8781da177e4SLinus Torvalds {
879b57ff130SWeston Andros Adamson 	if (pnfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx))
8808dd37758STrond Myklebust 		return;
8816272dcc6SAnna Schumaker 	nfs_request_add_commit_list(req, cinfo);
8821da177e4SLinus Torvalds }
8838e821cadSTrond Myklebust 
884d6d6dc7cSFred Isaman static void
885d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page)
886e468bae9STrond Myklebust {
88711fb9989SMel Gorman 	dec_node_page_state(page, NR_UNSTABLE_NFS);
88893f78d88STejun Heo 	dec_wb_stat(&inode_to_bdi(page_file_mapping(page)->host)->wb,
88993f78d88STejun Heo 		    WB_RECLAIMABLE);
890e468bae9STrond Myklebust }
891d6d6dc7cSFred Isaman 
892411a99adSWeston Andros Adamson /* Called holding inode (/cinfo) lock */
8938dd37758STrond Myklebust static void
894d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req)
895d6d6dc7cSFred Isaman {
8968dd37758STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
8972b0143b5SDavid Howells 		struct inode *inode = d_inode(req->wb_context->dentry);
898ea2cf228SFred Isaman 		struct nfs_commit_info cinfo;
899d6d6dc7cSFred Isaman 
900ea2cf228SFred Isaman 		nfs_init_cinfo_from_inode(&cinfo, inode);
901ea2cf228SFred Isaman 		if (!pnfs_clear_request_commit(req, &cinfo)) {
902ea2cf228SFred Isaman 			nfs_request_remove_commit_list(req, &cinfo);
903d6d6dc7cSFred Isaman 		}
9048dd37758STrond Myklebust 		nfs_clear_page_commit(req->wb_page);
9058dd37758STrond Myklebust 	}
906e468bae9STrond Myklebust }
907e468bae9STrond Myklebust 
908d45f60c6SWeston Andros Adamson int nfs_write_need_commit(struct nfs_pgio_header *hdr)
9098e821cadSTrond Myklebust {
910c65e6254SWeston Andros Adamson 	if (hdr->verf.committed == NFS_DATA_SYNC)
911d45f60c6SWeston Andros Adamson 		return hdr->lseg == NULL;
912c65e6254SWeston Andros Adamson 	return hdr->verf.committed != NFS_FILE_SYNC;
9138e821cadSTrond Myklebust }
9148e821cadSTrond Myklebust 
915061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr)
9166c75dc0dSFred Isaman {
917ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
9186c75dc0dSFred Isaman 	unsigned long bytes = 0;
9196c75dc0dSFred Isaman 
9206c75dc0dSFred Isaman 	if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
9216c75dc0dSFred Isaman 		goto out;
922ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
9236c75dc0dSFred Isaman 	while (!list_empty(&hdr->pages)) {
9246c75dc0dSFred Isaman 		struct nfs_page *req = nfs_list_entry(hdr->pages.next);
9256c75dc0dSFred Isaman 
9266c75dc0dSFred Isaman 		bytes += req->wb_bytes;
9276c75dc0dSFred Isaman 		nfs_list_remove_request(req);
9286c75dc0dSFred Isaman 		if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
9296c75dc0dSFred Isaman 		    (hdr->good_bytes < bytes)) {
930d1182b33STrond Myklebust 			nfs_set_pageerror(req->wb_page);
9316c75dc0dSFred Isaman 			nfs_context_set_write_error(req->wb_context, hdr->error);
9326c75dc0dSFred Isaman 			goto remove_req;
9336c75dc0dSFred Isaman 		}
934c65e6254SWeston Andros Adamson 		if (nfs_write_need_commit(hdr)) {
935f79d06f5SAnna Schumaker 			memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
936b57ff130SWeston Andros Adamson 			nfs_mark_request_commit(req, hdr->lseg, &cinfo,
937a7d42ddbSWeston Andros Adamson 				hdr->pgio_mirror_idx);
9386c75dc0dSFred Isaman 			goto next;
9396c75dc0dSFred Isaman 		}
9406c75dc0dSFred Isaman remove_req:
9416c75dc0dSFred Isaman 		nfs_inode_remove_request(req);
9426c75dc0dSFred Isaman next:
9431d1afcbcSTrond Myklebust 		nfs_unlock_request(req);
94420633f04SWeston Andros Adamson 		nfs_end_page_writeback(req);
9453aff4ebbSTrond Myklebust 		nfs_release_request(req);
9466c75dc0dSFred Isaman 	}
9476c75dc0dSFred Isaman out:
9486c75dc0dSFred Isaman 	hdr->release(hdr);
9496c75dc0dSFred Isaman }
9506c75dc0dSFred Isaman 
951ce59515cSAnna Schumaker unsigned long
952ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
953fb8a1f11STrond Myklebust {
954ea2cf228SFred Isaman 	return cinfo->mds->ncommit;
955fb8a1f11STrond Myklebust }
956fb8a1f11STrond Myklebust 
957fe238e60SDave Wysochanski /* cinfo->inode->i_lock held by caller */
9581763da12SFred Isaman int
959ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
960ea2cf228SFred Isaman 		     struct nfs_commit_info *cinfo, int max)
961d6d6dc7cSFred Isaman {
962d6d6dc7cSFred Isaman 	struct nfs_page *req, *tmp;
963d6d6dc7cSFred Isaman 	int ret = 0;
964d6d6dc7cSFred Isaman 
965d6d6dc7cSFred Isaman 	list_for_each_entry_safe(req, tmp, src, wb_list) {
9668dd37758STrond Myklebust 		if (!nfs_lock_request(req))
9678dd37758STrond Myklebust 			continue;
9687ad84aa9STrond Myklebust 		kref_get(&req->wb_kref);
969fe238e60SDave Wysochanski 		if (cond_resched_lock(&cinfo->inode->i_lock))
9703b3be88dSTrond Myklebust 			list_safe_reset_next(req, tmp, wb_list);
971ea2cf228SFred Isaman 		nfs_request_remove_commit_list(req, cinfo);
9728dd37758STrond Myklebust 		nfs_list_add_request(req, dst);
973d6d6dc7cSFred Isaman 		ret++;
9741763da12SFred Isaman 		if ((ret == max) && !cinfo->dreq)
975d6d6dc7cSFred Isaman 			break;
976d6d6dc7cSFred Isaman 	}
977d6d6dc7cSFred Isaman 	return ret;
978d6d6dc7cSFred Isaman }
979d6d6dc7cSFred Isaman 
9801da177e4SLinus Torvalds /*
9811da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
9821da177e4SLinus Torvalds  * @inode: NFS inode to scan
983ea2cf228SFred Isaman  * @dst: mds destination list
984ea2cf228SFred Isaman  * @cinfo: mds and ds lists of reqs ready to commit
9851da177e4SLinus Torvalds  *
9861da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
9871da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
9881da177e4SLinus Torvalds  */
9891763da12SFred Isaman int
990ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst,
991ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
9921da177e4SLinus Torvalds {
993d6d6dc7cSFred Isaman 	int ret = 0;
994fb8a1f11STrond Myklebust 
995fe238e60SDave Wysochanski 	spin_lock(&cinfo->inode->i_lock);
996ea2cf228SFred Isaman 	if (cinfo->mds->ncommit > 0) {
9978dd37758STrond Myklebust 		const int max = INT_MAX;
998d6d6dc7cSFred Isaman 
999ea2cf228SFred Isaman 		ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
1000ea2cf228SFred Isaman 					   cinfo, max);
1001ea2cf228SFred Isaman 		ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
1002d6d6dc7cSFred Isaman 	}
1003fe238e60SDave Wysochanski 	spin_unlock(&cinfo->inode->i_lock);
1004ff778d02STrond Myklebust 	return ret;
10051da177e4SLinus Torvalds }
1006d6d6dc7cSFred Isaman 
10071da177e4SLinus Torvalds /*
1008e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
1009e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
10101da177e4SLinus Torvalds  *
1011e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
1012e7d39069STrond Myklebust  * to disk.
10131da177e4SLinus Torvalds  */
1014e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
1015e7d39069STrond Myklebust 		struct page *page,
1016e7d39069STrond Myklebust 		unsigned int offset,
1017e7d39069STrond Myklebust 		unsigned int bytes)
10181da177e4SLinus Torvalds {
1019e7d39069STrond Myklebust 	struct nfs_page *req;
1020e7d39069STrond Myklebust 	unsigned int rqend;
1021e7d39069STrond Myklebust 	unsigned int end;
10221da177e4SLinus Torvalds 	int error;
1023277459d2STrond Myklebust 
1024e7d39069STrond Myklebust 	if (!PagePrivate(page))
1025e7d39069STrond Myklebust 		return NULL;
1026e7d39069STrond Myklebust 
1027e7d39069STrond Myklebust 	end = offset + bytes;
1028e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
1029e7d39069STrond Myklebust 
1030e7d39069STrond Myklebust 	for (;;) {
103184d3a9a9SWeston Andros Adamson 		req = nfs_page_find_head_request_locked(NFS_I(inode), page);
1032e7d39069STrond Myklebust 		if (req == NULL)
1033e7d39069STrond Myklebust 			goto out_unlock;
1034e7d39069STrond Myklebust 
10352bfc6e56SWeston Andros Adamson 		/* should be handled by nfs_flush_incompatible */
10362bfc6e56SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_head != req);
10372bfc6e56SWeston Andros Adamson 		WARN_ON_ONCE(req->wb_this_page != req);
10382bfc6e56SWeston Andros Adamson 
1039e7d39069STrond Myklebust 		rqend = req->wb_offset + req->wb_bytes;
1040e7d39069STrond Myklebust 		/*
1041e7d39069STrond Myklebust 		 * Tell the caller to flush out the request if
1042e7d39069STrond Myklebust 		 * the offsets are non-contiguous.
1043e7d39069STrond Myklebust 		 * Note: nfs_flush_incompatible() will already
1044e7d39069STrond Myklebust 		 * have flushed out requests having wrong owners.
1045e7d39069STrond Myklebust 		 */
1046e468bae9STrond Myklebust 		if (offset > rqend
1047e7d39069STrond Myklebust 		    || end < req->wb_offset)
1048e7d39069STrond Myklebust 			goto out_flushme;
1049e7d39069STrond Myklebust 
10507ad84aa9STrond Myklebust 		if (nfs_lock_request(req))
1051e7d39069STrond Myklebust 			break;
1052e7d39069STrond Myklebust 
1053e7d39069STrond Myklebust 		/* The request is locked, so wait and then retry */
1054587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
10551da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
10561da177e4SLinus Torvalds 		nfs_release_request(req);
1057e7d39069STrond Myklebust 		if (error != 0)
1058e7d39069STrond Myklebust 			goto out_err;
1059e7d39069STrond Myklebust 		spin_lock(&inode->i_lock);
10601da177e4SLinus Torvalds 	}
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
10631da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
10641da177e4SLinus Torvalds 		req->wb_offset = offset;
10651da177e4SLinus Torvalds 		req->wb_pgbase = offset;
10661da177e4SLinus Torvalds 	}
10671da177e4SLinus Torvalds 	if (end > rqend)
10681da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
1069e7d39069STrond Myklebust 	else
1070e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
1071e7d39069STrond Myklebust out_unlock:
1072ca138f36SFred Isaman 	if (req)
10738dd37758STrond Myklebust 		nfs_clear_request_commit(req);
1074411a99adSWeston Andros Adamson 	spin_unlock(&inode->i_lock);
1075e7d39069STrond Myklebust 	return req;
1076e7d39069STrond Myklebust out_flushme:
1077e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
1078e7d39069STrond Myklebust 	nfs_release_request(req);
1079e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
1080e7d39069STrond Myklebust out_err:
1081e7d39069STrond Myklebust 	return ERR_PTR(error);
1082e7d39069STrond Myklebust }
10831da177e4SLinus Torvalds 
1084e7d39069STrond Myklebust /*
1085e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
1086e7d39069STrond Myklebust  *
1087e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
1088e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
1089e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
1090e7d39069STrond Myklebust  */
1091e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
1092e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
1093e7d39069STrond Myklebust {
1094d56b4ddfSMel Gorman 	struct inode *inode = page_file_mapping(page)->host;
1095e7d39069STrond Myklebust 	struct nfs_page	*req;
1096e7d39069STrond Myklebust 
1097e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
1098e7d39069STrond Myklebust 	if (req != NULL)
1099e7d39069STrond Myklebust 		goto out;
11002bfc6e56SWeston Andros Adamson 	req = nfs_create_request(ctx, page, NULL, offset, bytes);
1101e7d39069STrond Myklebust 	if (IS_ERR(req))
1102e7d39069STrond Myklebust 		goto out;
1103d6d6dc7cSFred Isaman 	nfs_inode_add_request(inode, req);
1104efc91ed0STrond Myklebust out:
110561e930a9STrond Myklebust 	return req;
11061da177e4SLinus Torvalds }
11071da177e4SLinus Torvalds 
1108e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
1109e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
1110e7d39069STrond Myklebust {
1111e7d39069STrond Myklebust 	struct nfs_page	*req;
1112e7d39069STrond Myklebust 
1113e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
1114e7d39069STrond Myklebust 	if (IS_ERR(req))
1115e7d39069STrond Myklebust 		return PTR_ERR(req);
1116e7d39069STrond Myklebust 	/* Update file length */
1117e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
1118d72ddcbaSWeston Andros Adamson 	nfs_mark_uptodate(req);
1119a6305ddbSTrond Myklebust 	nfs_mark_request_dirty(req);
11201d1afcbcSTrond Myklebust 	nfs_unlock_and_release_request(req);
1121e7d39069STrond Myklebust 	return 0;
1122e7d39069STrond Myklebust }
1123e7d39069STrond Myklebust 
11241da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
11251da177e4SLinus Torvalds {
1126cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
11272a369153STrond Myklebust 	struct nfs_lock_context *l_ctx;
1128bd61e0a9SJeff Layton 	struct file_lock_context *flctx = file_inode(file)->i_flctx;
11291da177e4SLinus Torvalds 	struct nfs_page	*req;
11301a54533eSTrond Myklebust 	int do_flush, status;
11311da177e4SLinus Torvalds 	/*
11321da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
11331da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
11341da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
11351da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
11361da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
11371da177e4SLinus Torvalds 	 * dropped page.
11381da177e4SLinus Torvalds 	 */
11391a54533eSTrond Myklebust 	do {
114084d3a9a9SWeston Andros Adamson 		req = nfs_page_find_head_request(page);
11411a54533eSTrond Myklebust 		if (req == NULL)
11421a54533eSTrond Myklebust 			return 0;
11432a369153STrond Myklebust 		l_ctx = req->wb_lock_context;
1144138a2935STrond Myklebust 		do_flush = req->wb_page != page ||
1145138a2935STrond Myklebust 			!nfs_match_open_context(req->wb_context, ctx);
11462bfc6e56SWeston Andros Adamson 		/* for now, flush if more than 1 request in page_group */
11472bfc6e56SWeston Andros Adamson 		do_flush |= req->wb_this_page != req;
1148bd61e0a9SJeff Layton 		if (l_ctx && flctx &&
1149bd61e0a9SJeff Layton 		    !(list_empty_careful(&flctx->flc_posix) &&
1150bd61e0a9SJeff Layton 		      list_empty_careful(&flctx->flc_flock))) {
1151d51fdb87SNeilBrown 			do_flush |= l_ctx->lockowner != current->files;
11525263e31eSJeff Layton 		}
11531da177e4SLinus Torvalds 		nfs_release_request(req);
11541a54533eSTrond Myklebust 		if (!do_flush)
11551a54533eSTrond Myklebust 			return 0;
1156d56b4ddfSMel Gorman 		status = nfs_wb_page(page_file_mapping(page)->host, page);
11571a54533eSTrond Myklebust 	} while (status == 0);
11581a54533eSTrond Myklebust 	return status;
11591da177e4SLinus Torvalds }
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds /*
1162dc24826bSAndy Adamson  * Avoid buffered writes when a open context credential's key would
1163dc24826bSAndy Adamson  * expire soon.
1164dc24826bSAndy Adamson  *
1165dc24826bSAndy Adamson  * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
1166dc24826bSAndy Adamson  *
1167dc24826bSAndy Adamson  * Return 0 and set a credential flag which triggers the inode to flush
1168dc24826bSAndy Adamson  * and performs  NFS_FILE_SYNC writes if the key will expired within
1169dc24826bSAndy Adamson  * RPC_KEY_EXPIRE_TIMEO.
1170dc24826bSAndy Adamson  */
1171dc24826bSAndy Adamson int
1172dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode)
1173dc24826bSAndy Adamson {
1174dc24826bSAndy Adamson 	struct nfs_open_context *ctx = nfs_file_open_context(filp);
1175dc24826bSAndy Adamson 	struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
1176dc24826bSAndy Adamson 
1177dc24826bSAndy Adamson 	return rpcauth_key_timeout_notify(auth, ctx->cred);
1178dc24826bSAndy Adamson }
1179dc24826bSAndy Adamson 
1180dc24826bSAndy Adamson /*
1181dc24826bSAndy Adamson  * Test if the open context credential key is marked to expire soon.
1182dc24826bSAndy Adamson  */
1183ce52914eSScott Mayhew bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode)
1184dc24826bSAndy Adamson {
1185ce52914eSScott Mayhew 	struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
1186ce52914eSScott Mayhew 
1187ce52914eSScott Mayhew 	return rpcauth_cred_key_to_expire(auth, ctx->cred);
1188dc24826bSAndy Adamson }
1189dc24826bSAndy Adamson 
1190dc24826bSAndy Adamson /*
11915d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
11925d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
11935d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
11945d47a356STrond Myklebust  */
11958d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
11965d47a356STrond Myklebust {
1197d529ef83SJeff Layton 	struct nfs_inode *nfsi = NFS_I(inode);
1198d529ef83SJeff Layton 
11998d197a56STrond Myklebust 	if (nfs_have_delegated_attributes(inode))
12008d197a56STrond Myklebust 		goto out;
120118dd78c4SScott Mayhew 	if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
1202d529ef83SJeff Layton 		return false;
12034db72b40SJeff Layton 	smp_rmb();
1204d529ef83SJeff Layton 	if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags))
12058d197a56STrond Myklebust 		return false;
12068d197a56STrond Myklebust out:
120718dd78c4SScott Mayhew 	if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
120818dd78c4SScott Mayhew 		return false;
12098d197a56STrond Myklebust 	return PageUptodate(page) != 0;
12105d47a356STrond Myklebust }
12115d47a356STrond Myklebust 
12125263e31eSJeff Layton static bool
12135263e31eSJeff Layton is_whole_file_wrlock(struct file_lock *fl)
12145263e31eSJeff Layton {
12155263e31eSJeff Layton 	return fl->fl_start == 0 && fl->fl_end == OFFSET_MAX &&
12165263e31eSJeff Layton 			fl->fl_type == F_WRLCK;
12175263e31eSJeff Layton }
12185263e31eSJeff Layton 
1219c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or
1220c7559663SScott Mayhew  * if we have the whole file locked for writing), it may be more efficient to
1221c7559663SScott Mayhew  * extend the write to cover the entire page in order to avoid fragmentation
1222c7559663SScott Mayhew  * inefficiencies.
1223c7559663SScott Mayhew  *
1224263b4509SScott Mayhew  * If the file is opened for synchronous writes then we can just skip the rest
1225263b4509SScott Mayhew  * of the checks.
1226c7559663SScott Mayhew  */
1227c7559663SScott Mayhew static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode)
1228c7559663SScott Mayhew {
12295263e31eSJeff Layton 	int ret;
12305263e31eSJeff Layton 	struct file_lock_context *flctx = inode->i_flctx;
12315263e31eSJeff Layton 	struct file_lock *fl;
12325263e31eSJeff Layton 
1233c7559663SScott Mayhew 	if (file->f_flags & O_DSYNC)
1234c7559663SScott Mayhew 		return 0;
1235263b4509SScott Mayhew 	if (!nfs_write_pageuptodate(page, inode))
1236263b4509SScott Mayhew 		return 0;
1237c7559663SScott Mayhew 	if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
1238c7559663SScott Mayhew 		return 1;
1239bd61e0a9SJeff Layton 	if (!flctx || (list_empty_careful(&flctx->flc_flock) &&
1240bd61e0a9SJeff Layton 		       list_empty_careful(&flctx->flc_posix)))
12418fa4592aSTrond Myklebust 		return 1;
12425263e31eSJeff Layton 
12435263e31eSJeff Layton 	/* Check to see if there are whole file write locks */
12445263e31eSJeff Layton 	ret = 0;
12456109c850SJeff Layton 	spin_lock(&flctx->flc_lock);
1246bd61e0a9SJeff Layton 	if (!list_empty(&flctx->flc_posix)) {
1247bd61e0a9SJeff Layton 		fl = list_first_entry(&flctx->flc_posix, struct file_lock,
1248bd61e0a9SJeff Layton 					fl_list);
1249bd61e0a9SJeff Layton 		if (is_whole_file_wrlock(fl))
12505263e31eSJeff Layton 			ret = 1;
1251bd61e0a9SJeff Layton 	} else if (!list_empty(&flctx->flc_flock)) {
12525263e31eSJeff Layton 		fl = list_first_entry(&flctx->flc_flock, struct file_lock,
12535263e31eSJeff Layton 					fl_list);
12545263e31eSJeff Layton 		if (fl->fl_type == F_WRLCK)
12555263e31eSJeff Layton 			ret = 1;
12565263e31eSJeff Layton 	}
12576109c850SJeff Layton 	spin_unlock(&flctx->flc_lock);
12585263e31eSJeff Layton 	return ret;
1259c7559663SScott Mayhew }
1260c7559663SScott Mayhew 
12615d47a356STrond Myklebust /*
12621da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
12631da177e4SLinus Torvalds  *
12641da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
12651da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
12661da177e4SLinus Torvalds  */
12671da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
12681da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
12691da177e4SLinus Torvalds {
1270cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
1271d56b4ddfSMel Gorman 	struct inode	*inode = page_file_mapping(page)->host;
12721da177e4SLinus Torvalds 	int		status = 0;
12731da177e4SLinus Torvalds 
127491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
127591d5b470SChuck Lever 
12766de1472fSAl Viro 	dprintk("NFS:       nfs_updatepage(%pD2 %d@%lld)\n",
12776de1472fSAl Viro 		file, count, (long long)(page_file_offset(page) + offset));
12781da177e4SLinus Torvalds 
1279149a4fddSBenjamin Coddington 	if (!count)
1280149a4fddSBenjamin Coddington 		goto out;
1281149a4fddSBenjamin Coddington 
1282c7559663SScott Mayhew 	if (nfs_can_extend_write(file, page, inode)) {
128349a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
12841da177e4SLinus Torvalds 		offset = 0;
12851da177e4SLinus Torvalds 	}
12861da177e4SLinus Torvalds 
1287e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
128803fa9e84STrond Myklebust 	if (status < 0)
128903fa9e84STrond Myklebust 		nfs_set_pageerror(page);
129059b7c05fSTrond Myklebust 	else
129159b7c05fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
1292149a4fddSBenjamin Coddington out:
129348186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
12941da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
12951da177e4SLinus Torvalds 	return status;
12961da177e4SLinus Torvalds }
12971da177e4SLinus Torvalds 
12983ff7576dSTrond Myklebust static int flush_task_priority(int how)
12991da177e4SLinus Torvalds {
13001da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
13011da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
13021da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
13031da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
13041da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
13051da177e4SLinus Torvalds 	}
13061da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
13071da177e4SLinus Torvalds }
13081da177e4SLinus Torvalds 
1309d45f60c6SWeston Andros Adamson static void nfs_initiate_write(struct nfs_pgio_header *hdr,
1310d45f60c6SWeston Andros Adamson 			       struct rpc_message *msg,
1311abde71f4STom Haynes 			       const struct nfs_rpc_ops *rpc_ops,
13121ed26f33SAnna Schumaker 			       struct rpc_task_setup *task_setup_data, int how)
13131da177e4SLinus Torvalds {
13143ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
13151da177e4SLinus Torvalds 
13161ed26f33SAnna Schumaker 	task_setup_data->priority = priority;
1317abde71f4STom Haynes 	rpc_ops->write_setup(hdr, msg);
1318d138d5d1SAndy Adamson 
1319abde71f4STom Haynes 	nfs4_state_protect_write(NFS_SERVER(hdr->inode)->nfs_client,
1320d45f60c6SWeston Andros Adamson 				 &task_setup_data->rpc_client, msg, hdr);
1321275acaafSTrond Myklebust }
1322275acaafSTrond Myklebust 
13236d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
13246d884e8fSFred  * call this on each, which will prepare them to be retried on next
13256d884e8fSFred  * writeback using standard nfs.
13266d884e8fSFred  */
13276d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
13286d884e8fSFred {
13296d884e8fSFred 	nfs_mark_request_dirty(req);
1330c7070113STrond Myklebust 	set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
13311d1afcbcSTrond Myklebust 	nfs_unlock_request(req);
133220633f04SWeston Andros Adamson 	nfs_end_page_writeback(req);
13333aff4ebbSTrond Myklebust 	nfs_release_request(req);
13346d884e8fSFred }
13356d884e8fSFred 
1336061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head)
13376c75dc0dSFred Isaman {
13386c75dc0dSFred Isaman 	struct nfs_page	*req;
13396c75dc0dSFred Isaman 
13406c75dc0dSFred Isaman 	while (!list_empty(head)) {
13416c75dc0dSFred Isaman 		req = nfs_list_entry(head->next);
13426c75dc0dSFred Isaman 		nfs_list_remove_request(req);
13436c75dc0dSFred Isaman 		nfs_redirty_request(req);
13446c75dc0dSFred Isaman 	}
13456c75dc0dSFred Isaman }
13466c75dc0dSFred Isaman 
1347dc602dd7STrond Myklebust static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr)
1348dc602dd7STrond Myklebust {
1349dc602dd7STrond Myklebust 	nfs_async_write_error(&hdr->pages);
1350dc602dd7STrond Myklebust }
1351dc602dd7STrond Myklebust 
1352061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
1353061ae2edSFred Isaman 	.error_cleanup = nfs_async_write_error,
1354061ae2edSFred Isaman 	.completion = nfs_write_completion,
1355dc602dd7STrond Myklebust 	.reschedule_io = nfs_async_write_reschedule_io,
1356061ae2edSFred Isaman };
1357061ae2edSFred Isaman 
135857208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
1359a20c93e3SChristoph Hellwig 			       struct inode *inode, int ioflags, bool force_mds,
1360061ae2edSFred Isaman 			       const struct nfs_pgio_completion_ops *compl_ops)
13611751c363STrond Myklebust {
1362a20c93e3SChristoph Hellwig 	struct nfs_server *server = NFS_SERVER(inode);
136341d8d5b7SAnna Schumaker 	const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
1364a20c93e3SChristoph Hellwig 
1365a20c93e3SChristoph Hellwig #ifdef CONFIG_NFS_V4_1
1366a20c93e3SChristoph Hellwig 	if (server->pnfs_curr_ld && !force_mds)
1367a20c93e3SChristoph Hellwig 		pg_ops = server->pnfs_curr_ld->pg_write_ops;
1368a20c93e3SChristoph Hellwig #endif
13694a0de55cSAnna Schumaker 	nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
13704a0de55cSAnna Schumaker 			server->wsize, ioflags);
13711751c363STrond Myklebust }
1372ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
13731751c363STrond Myklebust 
1374dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
1375dce81290STrond Myklebust {
1376a7d42ddbSWeston Andros Adamson 	struct nfs_pgio_mirror *mirror;
1377a7d42ddbSWeston Andros Adamson 
13786f29b9bbSKinglong Mee 	if (pgio->pg_ops && pgio->pg_ops->pg_cleanup)
13796f29b9bbSKinglong Mee 		pgio->pg_ops->pg_cleanup(pgio);
13806f29b9bbSKinglong Mee 
138141d8d5b7SAnna Schumaker 	pgio->pg_ops = &nfs_pgio_rw_ops;
1382a7d42ddbSWeston Andros Adamson 
1383a7d42ddbSWeston Andros Adamson 	nfs_pageio_stop_mirroring(pgio);
1384a7d42ddbSWeston Andros Adamson 
1385a7d42ddbSWeston Andros Adamson 	mirror = &pgio->pg_mirrors[0];
1386a7d42ddbSWeston Andros Adamson 	mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
1387dce81290STrond Myklebust }
13881f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
1389dce81290STrond Myklebust 
13901da177e4SLinus Torvalds 
13910b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata)
13920b7c0153SFred Isaman {
13930b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
13940b7c0153SFred Isaman 
13950b7c0153SFred Isaman 	NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
13960b7c0153SFred Isaman }
13970b7c0153SFred Isaman 
13981f2edbe3STrond Myklebust /*
13991f2edbe3STrond Myklebust  * Special version of should_remove_suid() that ignores capabilities.
14001f2edbe3STrond Myklebust  */
14011f2edbe3STrond Myklebust static int nfs_should_remove_suid(const struct inode *inode)
14021f2edbe3STrond Myklebust {
14031f2edbe3STrond Myklebust 	umode_t mode = inode->i_mode;
14041f2edbe3STrond Myklebust 	int kill = 0;
1405788e7a89STrond Myklebust 
14061f2edbe3STrond Myklebust 	/* suid always must be killed */
14071f2edbe3STrond Myklebust 	if (unlikely(mode & S_ISUID))
14081f2edbe3STrond Myklebust 		kill = ATTR_KILL_SUID;
14091f2edbe3STrond Myklebust 
14101f2edbe3STrond Myklebust 	/*
14111f2edbe3STrond Myklebust 	 * sgid without any exec bits is just a mandatory locking mark; leave
14121f2edbe3STrond Myklebust 	 * it alone.  If some exec bits are set, it's a real sgid; kill it.
14131f2edbe3STrond Myklebust 	 */
14141f2edbe3STrond Myklebust 	if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
14151f2edbe3STrond Myklebust 		kill |= ATTR_KILL_SGID;
14161f2edbe3STrond Myklebust 
14171f2edbe3STrond Myklebust 	if (unlikely(kill && S_ISREG(mode)))
14181f2edbe3STrond Myklebust 		return kill;
14191f2edbe3STrond Myklebust 
14201f2edbe3STrond Myklebust 	return 0;
14211f2edbe3STrond Myklebust }
1422788e7a89STrond Myklebust 
1423a08a8cd3STrond Myklebust static void nfs_writeback_check_extend(struct nfs_pgio_header *hdr,
1424a08a8cd3STrond Myklebust 		struct nfs_fattr *fattr)
1425a08a8cd3STrond Myklebust {
1426a08a8cd3STrond Myklebust 	struct nfs_pgio_args *argp = &hdr->args;
1427a08a8cd3STrond Myklebust 	struct nfs_pgio_res *resp = &hdr->res;
14282b83d3deSTrond Myklebust 	u64 size = argp->offset + resp->count;
1429a08a8cd3STrond Myklebust 
1430a08a8cd3STrond Myklebust 	if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
14312b83d3deSTrond Myklebust 		fattr->size = size;
14322b83d3deSTrond Myklebust 	if (nfs_size_to_loff_t(fattr->size) < i_size_read(hdr->inode)) {
14332b83d3deSTrond Myklebust 		fattr->valid &= ~NFS_ATTR_FATTR_SIZE;
1434a08a8cd3STrond Myklebust 		return;
14352b83d3deSTrond Myklebust 	}
14362b83d3deSTrond Myklebust 	if (size != fattr->size)
1437a08a8cd3STrond Myklebust 		return;
1438a08a8cd3STrond Myklebust 	/* Set attribute barrier */
1439a08a8cd3STrond Myklebust 	nfs_fattr_set_barrier(fattr);
14402b83d3deSTrond Myklebust 	/* ...and update size */
14412b83d3deSTrond Myklebust 	fattr->valid |= NFS_ATTR_FATTR_SIZE;
1442a08a8cd3STrond Myklebust }
1443a08a8cd3STrond Myklebust 
1444a08a8cd3STrond Myklebust void nfs_writeback_update_inode(struct nfs_pgio_header *hdr)
1445a08a8cd3STrond Myklebust {
14462b83d3deSTrond Myklebust 	struct nfs_fattr *fattr = &hdr->fattr;
1447a08a8cd3STrond Myklebust 	struct inode *inode = hdr->inode;
1448a08a8cd3STrond Myklebust 
1449a08a8cd3STrond Myklebust 	spin_lock(&inode->i_lock);
1450a08a8cd3STrond Myklebust 	nfs_writeback_check_extend(hdr, fattr);
1451a08a8cd3STrond Myklebust 	nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
1452a08a8cd3STrond Myklebust 	spin_unlock(&inode->i_lock);
1453a08a8cd3STrond Myklebust }
1454a08a8cd3STrond Myklebust EXPORT_SYMBOL_GPL(nfs_writeback_update_inode);
1455a08a8cd3STrond Myklebust 
14561da177e4SLinus Torvalds /*
14571da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
14581da177e4SLinus Torvalds  */
1459d45f60c6SWeston Andros Adamson static int nfs_writeback_done(struct rpc_task *task,
1460d45f60c6SWeston Andros Adamson 			      struct nfs_pgio_header *hdr,
14610eecb214SAnna Schumaker 			      struct inode *inode)
14621da177e4SLinus Torvalds {
1463788e7a89STrond Myklebust 	int status;
14641da177e4SLinus Torvalds 
1465f551e44fSChuck Lever 	/*
1466f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1467f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1468f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1469f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1470f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1471f551e44fSChuck Lever 	 */
1472d45f60c6SWeston Andros Adamson 	status = NFS_PROTO(inode)->write_done(task, hdr);
1473788e7a89STrond Myklebust 	if (status != 0)
14740eecb214SAnna Schumaker 		return status;
1475d45f60c6SWeston Andros Adamson 	nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count);
147691d5b470SChuck Lever 
1477d45f60c6SWeston Andros Adamson 	if (hdr->res.verf->committed < hdr->args.stable &&
1478d45f60c6SWeston Andros Adamson 	    task->tk_status >= 0) {
14791da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
14801da177e4SLinus Torvalds 		 * commit data to stable storage even though we
14811da177e4SLinus Torvalds 		 * requested it.
14821da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
14831da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
14841da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
14851da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
14861da177e4SLinus Torvalds 		 */
14871da177e4SLinus Torvalds 		static unsigned long    complain;
14881da177e4SLinus Torvalds 
1489a69aef14SFred Isaman 		/* Note this will print the MDS for a DS write */
14901da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
14911da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
14921da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
1493cd841605SFred Isaman 				NFS_SERVER(inode)->nfs_client->cl_hostname,
1494d45f60c6SWeston Andros Adamson 				hdr->res.verf->committed, hdr->args.stable);
14951da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
14961da177e4SLinus Torvalds 		}
14971da177e4SLinus Torvalds 	}
14981f2edbe3STrond Myklebust 
14991f2edbe3STrond Myklebust 	/* Deal with the suid/sgid bit corner case */
15001f2edbe3STrond Myklebust 	if (nfs_should_remove_suid(inode))
15011f2edbe3STrond Myklebust 		nfs_mark_for_revalidate(inode);
15020eecb214SAnna Schumaker 	return 0;
15030eecb214SAnna Schumaker }
15040eecb214SAnna Schumaker 
15050eecb214SAnna Schumaker /*
15060eecb214SAnna Schumaker  * This function is called when the WRITE call is complete.
15070eecb214SAnna Schumaker  */
1508d45f60c6SWeston Andros Adamson static void nfs_writeback_result(struct rpc_task *task,
1509d45f60c6SWeston Andros Adamson 				 struct nfs_pgio_header *hdr)
15100eecb214SAnna Schumaker {
1511d45f60c6SWeston Andros Adamson 	struct nfs_pgio_args	*argp = &hdr->args;
1512d45f60c6SWeston Andros Adamson 	struct nfs_pgio_res	*resp = &hdr->res;
15131f2edbe3STrond Myklebust 
15141f2edbe3STrond Myklebust 	if (resp->count < argp->count) {
15151da177e4SLinus Torvalds 		static unsigned long    complain;
15161da177e4SLinus Torvalds 
15176c75dc0dSFred Isaman 		/* This a short write! */
1518d45f60c6SWeston Andros Adamson 		nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE);
151991d5b470SChuck Lever 
15201da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
15216c75dc0dSFred Isaman 		if (resp->count == 0) {
15226c75dc0dSFred Isaman 			if (time_before(complain, jiffies)) {
15236c75dc0dSFred Isaman 				printk(KERN_WARNING
15246c75dc0dSFred Isaman 				       "NFS: Server wrote zero bytes, expected %u.\n",
15256c75dc0dSFred Isaman 				       argp->count);
15266c75dc0dSFred Isaman 				complain = jiffies + 300 * HZ;
15276c75dc0dSFred Isaman 			}
1528d45f60c6SWeston Andros Adamson 			nfs_set_pgio_error(hdr, -EIO, argp->offset);
15296c75dc0dSFred Isaman 			task->tk_status = -EIO;
15306c75dc0dSFred Isaman 			return;
15316c75dc0dSFred Isaman 		}
1532f8417b48SKinglong Mee 
1533f8417b48SKinglong Mee 		/* For non rpc-based layout drivers, retry-through-MDS */
1534f8417b48SKinglong Mee 		if (!task->tk_ops) {
1535f8417b48SKinglong Mee 			hdr->pnfs_error = -EAGAIN;
1536f8417b48SKinglong Mee 			return;
1537f8417b48SKinglong Mee 		}
1538f8417b48SKinglong Mee 
15391da177e4SLinus Torvalds 		/* Was this an NFSv2 write or an NFSv3 stable write? */
15401da177e4SLinus Torvalds 		if (resp->verf->committed != NFS_UNSTABLE) {
15411da177e4SLinus Torvalds 			/* Resend from where the server left off */
1542d45f60c6SWeston Andros Adamson 			hdr->mds_offset += resp->count;
15431da177e4SLinus Torvalds 			argp->offset += resp->count;
15441da177e4SLinus Torvalds 			argp->pgbase += resp->count;
15451da177e4SLinus Torvalds 			argp->count -= resp->count;
15461da177e4SLinus Torvalds 		} else {
15471da177e4SLinus Torvalds 			/* Resend as a stable write in order to avoid
15481da177e4SLinus Torvalds 			 * headaches in the case of a server crash.
15491da177e4SLinus Torvalds 			 */
15501da177e4SLinus Torvalds 			argp->stable = NFS_FILE_SYNC;
15511da177e4SLinus Torvalds 		}
1552d00c5d43STrond Myklebust 		rpc_restart_call_prepare(task);
15531da177e4SLinus Torvalds 	}
15541da177e4SLinus Torvalds }
15551da177e4SLinus Torvalds 
1556af7cf057STrond Myklebust static int wait_on_commit(struct nfs_mds_commit_info *cinfo)
155771d0a611STrond Myklebust {
1558af7cf057STrond Myklebust 	return wait_on_atomic_t(&cinfo->rpcs_out,
1559af7cf057STrond Myklebust 			nfs_wait_atomic_killable, TASK_KILLABLE);
1560af7cf057STrond Myklebust }
1561af7cf057STrond Myklebust 
1562af7cf057STrond Myklebust static void nfs_commit_begin(struct nfs_mds_commit_info *cinfo)
1563af7cf057STrond Myklebust {
1564af7cf057STrond Myklebust 	atomic_inc(&cinfo->rpcs_out);
1565af7cf057STrond Myklebust }
1566af7cf057STrond Myklebust 
1567af7cf057STrond Myklebust static void nfs_commit_end(struct nfs_mds_commit_info *cinfo)
1568af7cf057STrond Myklebust {
1569af7cf057STrond Myklebust 	if (atomic_dec_and_test(&cinfo->rpcs_out))
1570af7cf057STrond Myklebust 		wake_up_atomic_t(&cinfo->rpcs_out);
157171d0a611STrond Myklebust }
157271d0a611STrond Myklebust 
15730b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data)
15741da177e4SLinus Torvalds {
15750b7c0153SFred Isaman 	put_nfs_open_context(data->context);
15760b7c0153SFred Isaman 	nfs_commit_free(data);
15771da177e4SLinus Torvalds }
1578e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release);
15791da177e4SLinus Torvalds 
15800b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
1581c36aae9aSPeng Tao 			const struct nfs_rpc_ops *nfs_ops,
15829ace33cdSFred Isaman 			const struct rpc_call_ops *call_ops,
15839f0ec176SAndy Adamson 			int how, int flags)
15841da177e4SLinus Torvalds {
158507737691STrond Myklebust 	struct rpc_task *task;
15869ace33cdSFred Isaman 	int priority = flush_task_priority(how);
1587bdc7f021STrond Myklebust 	struct rpc_message msg = {
1588bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1589bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
15909ace33cdSFred Isaman 		.rpc_cred = data->cred,
1591bdc7f021STrond Myklebust 	};
159284115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
159307737691STrond Myklebust 		.task = &data->task,
15949ace33cdSFred Isaman 		.rpc_client = clnt,
1595bdc7f021STrond Myklebust 		.rpc_message = &msg,
15969ace33cdSFred Isaman 		.callback_ops = call_ops,
159784115e1cSTrond Myklebust 		.callback_data = data,
1598101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
15999f0ec176SAndy Adamson 		.flags = RPC_TASK_ASYNC | flags,
16003ff7576dSTrond Myklebust 		.priority = priority,
160184115e1cSTrond Myklebust 	};
1602788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
1603c36aae9aSPeng Tao 	nfs_ops->commit_setup(data, &msg);
16041da177e4SLinus Torvalds 
1605b4839ebeSKinglong Mee 	dprintk("NFS: initiated commit call\n");
1606bdc7f021STrond Myklebust 
16078c21c62cSWeston Andros Adamson 	nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client,
16088c21c62cSWeston Andros Adamson 		NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg);
16098c21c62cSWeston Andros Adamson 
161007737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1611dbae4c73STrond Myklebust 	if (IS_ERR(task))
1612dbae4c73STrond Myklebust 		return PTR_ERR(task);
1613d2224e7aSJeff Layton 	if (how & FLUSH_SYNC)
1614d2224e7aSJeff Layton 		rpc_wait_for_completion_task(task);
161507737691STrond Myklebust 	rpc_put_task(task);
1616dbae4c73STrond Myklebust 	return 0;
16171da177e4SLinus Torvalds }
1618e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit);
16191da177e4SLinus Torvalds 
1620378520b8SPeng Tao static loff_t nfs_get_lwb(struct list_head *head)
1621378520b8SPeng Tao {
1622378520b8SPeng Tao 	loff_t lwb = 0;
1623378520b8SPeng Tao 	struct nfs_page *req;
1624378520b8SPeng Tao 
1625378520b8SPeng Tao 	list_for_each_entry(req, head, wb_list)
1626378520b8SPeng Tao 		if (lwb < (req_offset(req) + req->wb_bytes))
1627378520b8SPeng Tao 			lwb = req_offset(req) + req->wb_bytes;
1628378520b8SPeng Tao 
1629378520b8SPeng Tao 	return lwb;
1630378520b8SPeng Tao }
1631378520b8SPeng Tao 
16321da177e4SLinus Torvalds /*
16339ace33cdSFred Isaman  * Set up the argument/result storage required for the RPC call.
16349ace33cdSFred Isaman  */
16350b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data,
1636988b6dceSFred Isaman 		     struct list_head *head,
1637f453a54aSFred Isaman 		     struct pnfs_layout_segment *lseg,
1638f453a54aSFred Isaman 		     struct nfs_commit_info *cinfo)
16399ace33cdSFred Isaman {
16409ace33cdSFred Isaman 	struct nfs_page *first = nfs_list_entry(head->next);
16412b0143b5SDavid Howells 	struct inode *inode = d_inode(first->wb_context->dentry);
16429ace33cdSFred Isaman 
16439ace33cdSFred Isaman 	/* Set up the RPC argument and reply structs
16449ace33cdSFred Isaman 	 * NB: take care not to mess about with data->commit et al. */
16459ace33cdSFred Isaman 
16469ace33cdSFred Isaman 	list_splice_init(head, &data->pages);
16479ace33cdSFred Isaman 
16489ace33cdSFred Isaman 	data->inode	  = inode;
16499ace33cdSFred Isaman 	data->cred	  = first->wb_context->cred;
1650988b6dceSFred Isaman 	data->lseg	  = lseg; /* reference transferred */
1651378520b8SPeng Tao 	/* only set lwb for pnfs commit */
1652378520b8SPeng Tao 	if (lseg)
1653378520b8SPeng Tao 		data->lwb = nfs_get_lwb(&data->pages);
16549ace33cdSFred Isaman 	data->mds_ops     = &nfs_commit_ops;
1655f453a54aSFred Isaman 	data->completion_ops = cinfo->completion_ops;
1656b359f9d0SFred Isaman 	data->dreq	  = cinfo->dreq;
16579ace33cdSFred Isaman 
16589ace33cdSFred Isaman 	data->args.fh     = NFS_FH(data->inode);
16599ace33cdSFred Isaman 	/* Note: we always request a commit of the entire inode */
16609ace33cdSFred Isaman 	data->args.offset = 0;
16619ace33cdSFred Isaman 	data->args.count  = 0;
16620b7c0153SFred Isaman 	data->context     = get_nfs_open_context(first->wb_context);
16639ace33cdSFred Isaman 	data->res.fattr   = &data->fattr;
16649ace33cdSFred Isaman 	data->res.verf    = &data->verf;
16659ace33cdSFred Isaman 	nfs_fattr_init(&data->fattr);
16669ace33cdSFred Isaman }
1667e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit);
16689ace33cdSFred Isaman 
1669e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list,
1670ea2cf228SFred Isaman 		      struct pnfs_layout_segment *lseg,
1671b57ff130SWeston Andros Adamson 		      struct nfs_commit_info *cinfo,
1672b57ff130SWeston Andros Adamson 		      u32 ds_commit_idx)
167364bfeb49SFred Isaman {
167464bfeb49SFred Isaman 	struct nfs_page *req;
167564bfeb49SFred Isaman 
167664bfeb49SFred Isaman 	while (!list_empty(page_list)) {
167764bfeb49SFred Isaman 		req = nfs_list_entry(page_list->next);
167864bfeb49SFred Isaman 		nfs_list_remove_request(req);
1679b57ff130SWeston Andros Adamson 		nfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx);
1680487b9b8aSTom Haynes 		if (!cinfo->dreq)
1681487b9b8aSTom Haynes 			nfs_clear_page_commit(req->wb_page);
16821d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
168364bfeb49SFred Isaman 	}
168464bfeb49SFred Isaman }
1685e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit);
168664bfeb49SFred Isaman 
1687b20135d0STrond Myklebust static void
1688b20135d0STrond Myklebust nfs_commit_resched_write(struct nfs_commit_info *cinfo,
1689b20135d0STrond Myklebust 		struct nfs_page *req)
1690b20135d0STrond Myklebust {
1691b20135d0STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
1692b20135d0STrond Myklebust }
1693b20135d0STrond Myklebust 
16949ace33cdSFred Isaman /*
16951da177e4SLinus Torvalds  * Commit dirty pages
16961da177e4SLinus Torvalds  */
16971da177e4SLinus Torvalds static int
1698ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how,
1699ea2cf228SFred Isaman 		struct nfs_commit_info *cinfo)
17001da177e4SLinus Torvalds {
17010b7c0153SFred Isaman 	struct nfs_commit_data	*data;
17021da177e4SLinus Torvalds 
1703ade8febdSWeston Andros Adamson 	/* another commit raced with us */
1704ade8febdSWeston Andros Adamson 	if (list_empty(head))
1705ade8febdSWeston Andros Adamson 		return 0;
1706ade8febdSWeston Andros Adamson 
1707c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
17081da177e4SLinus Torvalds 
17091da177e4SLinus Torvalds 	if (!data)
17101da177e4SLinus Torvalds 		goto out_bad;
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds 	/* Set up the argument struct */
1713f453a54aSFred Isaman 	nfs_init_commit(data, head, NULL, cinfo);
1714f453a54aSFred Isaman 	atomic_inc(&cinfo->mds->rpcs_out);
1715c36aae9aSPeng Tao 	return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode),
1716c36aae9aSPeng Tao 				   data->mds_ops, how, 0);
17171da177e4SLinus Torvalds  out_bad:
1718b57ff130SWeston Andros Adamson 	nfs_retry_commit(head, NULL, cinfo, 0);
17191da177e4SLinus Torvalds 	return -ENOMEM;
17201da177e4SLinus Torvalds }
17211da177e4SLinus Torvalds 
172267911c8fSAnna Schumaker int nfs_commit_file(struct file *file, struct nfs_write_verifier *verf)
172367911c8fSAnna Schumaker {
172467911c8fSAnna Schumaker 	struct inode *inode = file_inode(file);
172567911c8fSAnna Schumaker 	struct nfs_open_context *open;
172667911c8fSAnna Schumaker 	struct nfs_commit_info cinfo;
172767911c8fSAnna Schumaker 	struct nfs_page *req;
172867911c8fSAnna Schumaker 	int ret;
172967911c8fSAnna Schumaker 
173067911c8fSAnna Schumaker 	open = get_nfs_open_context(nfs_file_open_context(file));
173167911c8fSAnna Schumaker 	req  = nfs_create_request(open, NULL, NULL, 0, i_size_read(inode));
17322997bfd0SDan Carpenter 	if (IS_ERR(req)) {
17332997bfd0SDan Carpenter 		ret = PTR_ERR(req);
173467911c8fSAnna Schumaker 		goto out_put;
173567911c8fSAnna Schumaker 	}
173667911c8fSAnna Schumaker 
173767911c8fSAnna Schumaker 	nfs_init_cinfo_from_inode(&cinfo, inode);
173867911c8fSAnna Schumaker 
173967911c8fSAnna Schumaker 	memcpy(&req->wb_verf, verf, sizeof(struct nfs_write_verifier));
174067911c8fSAnna Schumaker 	nfs_request_add_commit_list(req, &cinfo);
174167911c8fSAnna Schumaker 	ret = nfs_commit_inode(inode, FLUSH_SYNC);
174267911c8fSAnna Schumaker 	if (ret > 0)
174367911c8fSAnna Schumaker 		ret = 0;
174467911c8fSAnna Schumaker 
174567911c8fSAnna Schumaker 	nfs_free_request(req);
174667911c8fSAnna Schumaker out_put:
174767911c8fSAnna Schumaker 	put_nfs_open_context(open);
174867911c8fSAnna Schumaker 	return ret;
174967911c8fSAnna Schumaker }
175067911c8fSAnna Schumaker EXPORT_SYMBOL_GPL(nfs_commit_file);
175167911c8fSAnna Schumaker 
17521da177e4SLinus Torvalds /*
17531da177e4SLinus Torvalds  * COMMIT call returned
17541da177e4SLinus Torvalds  */
1755788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
17561da177e4SLinus Torvalds {
17570b7c0153SFred Isaman 	struct nfs_commit_data	*data = calldata;
17581da177e4SLinus Torvalds 
1759a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
17601da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
17611da177e4SLinus Torvalds 
1762788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1763c0d0e96bSTrond Myklebust 	NFS_PROTO(data->inode)->commit_done(task, data);
1764c9d8f89dSTrond Myklebust }
1765c9d8f89dSTrond Myklebust 
1766f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data)
1767c9d8f89dSTrond Myklebust {
1768c9d8f89dSTrond Myklebust 	struct nfs_page	*req;
1769c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1770f453a54aSFred Isaman 	struct nfs_commit_info cinfo;
1771353db796SNeilBrown 	struct nfs_server *nfss;
1772788e7a89STrond Myklebust 
17731da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
17741da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
17751da177e4SLinus Torvalds 		nfs_list_remove_request(req);
177667911c8fSAnna Schumaker 		if (req->wb_page)
1777d6d6dc7cSFred Isaman 			nfs_clear_page_commit(req->wb_page);
17781da177e4SLinus Torvalds 
17791e8968c5SNiels de Vos 		dprintk("NFS:       commit (%s/%llu %d@%lld)",
17803d4ff43dSAl Viro 			req->wb_context->dentry->d_sb->s_id,
17812b0143b5SDavid Howells 			(unsigned long long)NFS_FILEID(d_inode(req->wb_context->dentry)),
17821da177e4SLinus Torvalds 			req->wb_bytes,
17831da177e4SLinus Torvalds 			(long long)req_offset(req));
1784c9d8f89dSTrond Myklebust 		if (status < 0) {
1785c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
178638a33101SKinglong Mee 			if (req->wb_page)
17871da177e4SLinus Torvalds 				nfs_inode_remove_request(req);
1788ddeaa637SJoe Perches 			dprintk_cont(", error = %d\n", status);
17891da177e4SLinus Torvalds 			goto next;
17901da177e4SLinus Torvalds 		}
17911da177e4SLinus Torvalds 
17921da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
17931da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
17948fc3c386STrond Myklebust 		if (!nfs_write_verifier_cmp(&req->wb_verf, &data->verf.verifier)) {
17951da177e4SLinus Torvalds 			/* We have a match */
179638a33101SKinglong Mee 			if (req->wb_page)
17971da177e4SLinus Torvalds 				nfs_inode_remove_request(req);
1798ddeaa637SJoe Perches 			dprintk_cont(" OK\n");
17991da177e4SLinus Torvalds 			goto next;
18001da177e4SLinus Torvalds 		}
18011da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
1802ddeaa637SJoe Perches 		dprintk_cont(" mismatch\n");
18036d884e8fSFred 		nfs_mark_request_dirty(req);
180405990d1bSTrond Myklebust 		set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
18051da177e4SLinus Torvalds 	next:
18061d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
18071da177e4SLinus Torvalds 	}
1808353db796SNeilBrown 	nfss = NFS_SERVER(data->inode);
1809353db796SNeilBrown 	if (atomic_long_read(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
1810*0db10944SJan Kara 		clear_bdi_congested(inode_to_bdi(data->inode), BLK_RW_ASYNC);
1811353db796SNeilBrown 
1812f453a54aSFred Isaman 	nfs_init_cinfo(&cinfo, data->inode, data->dreq);
1813af7cf057STrond Myklebust 	nfs_commit_end(cinfo.mds);
18145917ce84SFred Isaman }
18155917ce84SFred Isaman 
18165917ce84SFred Isaman static void nfs_commit_release(void *calldata)
18175917ce84SFred Isaman {
18180b7c0153SFred Isaman 	struct nfs_commit_data *data = calldata;
18195917ce84SFred Isaman 
1820f453a54aSFred Isaman 	data->completion_ops->completion(data);
1821c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
18221da177e4SLinus Torvalds }
1823788e7a89STrond Myklebust 
1824788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
18250b7c0153SFred Isaman 	.rpc_call_prepare = nfs_commit_prepare,
1826788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1827788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1828788e7a89STrond Myklebust };
18291da177e4SLinus Torvalds 
1830f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
1831f453a54aSFred Isaman 	.completion = nfs_commit_release_pages,
1832b20135d0STrond Myklebust 	.resched_write = nfs_commit_resched_write,
1833f453a54aSFred Isaman };
1834f453a54aSFred Isaman 
18351763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
1836ea2cf228SFred Isaman 			    int how, struct nfs_commit_info *cinfo)
183784c53ab5SFred Isaman {
183884c53ab5SFred Isaman 	int status;
183984c53ab5SFred Isaman 
1840ea2cf228SFred Isaman 	status = pnfs_commit_list(inode, head, how, cinfo);
184184c53ab5SFred Isaman 	if (status == PNFS_NOT_ATTEMPTED)
1842ea2cf228SFred Isaman 		status = nfs_commit_list(inode, head, how, cinfo);
184384c53ab5SFred Isaman 	return status;
184484c53ab5SFred Isaman }
184584c53ab5SFred Isaman 
1846b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
18471da177e4SLinus Torvalds {
18481da177e4SLinus Torvalds 	LIST_HEAD(head);
1849ea2cf228SFred Isaman 	struct nfs_commit_info cinfo;
185071d0a611STrond Myklebust 	int may_wait = how & FLUSH_SYNC;
1851af7cf057STrond Myklebust 	int error = 0;
1852b8413f98STrond Myklebust 	int res;
18531da177e4SLinus Torvalds 
1854ea2cf228SFred Isaman 	nfs_init_cinfo_from_inode(&cinfo, inode);
1855af7cf057STrond Myklebust 	nfs_commit_begin(cinfo.mds);
1856ea2cf228SFred Isaman 	res = nfs_scan_commit(inode, &head, &cinfo);
1857af7cf057STrond Myklebust 	if (res)
1858ea2cf228SFred Isaman 		error = nfs_generic_commit_list(inode, &head, how, &cinfo);
1859af7cf057STrond Myklebust 	nfs_commit_end(cinfo.mds);
18601da177e4SLinus Torvalds 	if (error < 0)
1861af7cf057STrond Myklebust 		goto out_error;
1862b8413f98STrond Myklebust 	if (!may_wait)
1863b8413f98STrond Myklebust 		goto out_mark_dirty;
1864af7cf057STrond Myklebust 	error = wait_on_commit(cinfo.mds);
1865b8413f98STrond Myklebust 	if (error < 0)
1866b8413f98STrond Myklebust 		return error;
1867c5efa5fcSTrond Myklebust 	return res;
1868af7cf057STrond Myklebust out_error:
1869af7cf057STrond Myklebust 	res = error;
1870c5efa5fcSTrond Myklebust 	/* Note: If we exit without ensuring that the commit is complete,
1871c5efa5fcSTrond Myklebust 	 * we must mark the inode as dirty. Otherwise, future calls to
1872c5efa5fcSTrond Myklebust 	 * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
1873c5efa5fcSTrond Myklebust 	 * that the data is on the disk.
1874c5efa5fcSTrond Myklebust 	 */
1875c5efa5fcSTrond Myklebust out_mark_dirty:
1876c5efa5fcSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
18771da177e4SLinus Torvalds 	return res;
18781da177e4SLinus Torvalds }
1879b20135d0STrond Myklebust EXPORT_SYMBOL_GPL(nfs_commit_inode);
18808fc795f7STrond Myklebust 
1881ae09c31fSAnna Schumaker int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
18828fc795f7STrond Myklebust {
1883420e3646STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
1884420e3646STrond Myklebust 	int flags = FLUSH_SYNC;
1885420e3646STrond Myklebust 	int ret = 0;
18868fc795f7STrond Myklebust 
18873236c3e1SJeff Layton 	/* no commits means nothing needs to be done */
1888ea2cf228SFred Isaman 	if (!nfsi->commit_info.ncommit)
18893236c3e1SJeff Layton 		return ret;
18903236c3e1SJeff Layton 
1891a00dd6c0SJeff Layton 	if (wbc->sync_mode == WB_SYNC_NONE) {
1892a00dd6c0SJeff Layton 		/* Don't commit yet if this is a non-blocking flush and there
1893a00dd6c0SJeff Layton 		 * are a lot of outstanding writes for this mapping.
1894420e3646STrond Myklebust 		 */
1895cb1410c7SWeston Andros Adamson 		if (nfsi->commit_info.ncommit <= (nfsi->nrequests >> 1))
1896420e3646STrond Myklebust 			goto out_mark_dirty;
1897420e3646STrond Myklebust 
1898a00dd6c0SJeff Layton 		/* don't wait for the COMMIT response */
1899420e3646STrond Myklebust 		flags = 0;
1900a00dd6c0SJeff Layton 	}
1901a00dd6c0SJeff Layton 
1902420e3646STrond Myklebust 	ret = nfs_commit_inode(inode, flags);
1903420e3646STrond Myklebust 	if (ret >= 0) {
1904420e3646STrond Myklebust 		if (wbc->sync_mode == WB_SYNC_NONE) {
1905420e3646STrond Myklebust 			if (ret < wbc->nr_to_write)
1906420e3646STrond Myklebust 				wbc->nr_to_write -= ret;
1907420e3646STrond Myklebust 			else
1908420e3646STrond Myklebust 				wbc->nr_to_write = 0;
1909420e3646STrond Myklebust 		}
19108fc795f7STrond Myklebust 		return 0;
1911420e3646STrond Myklebust 	}
1912420e3646STrond Myklebust out_mark_dirty:
19138fc795f7STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
19148fc795f7STrond Myklebust 	return ret;
19158fc795f7STrond Myklebust }
191689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode);
1917863a3c6cSAndy Adamson 
1918acdc53b2STrond Myklebust /*
1919837bb1d7STrond Myklebust  * Wrapper for filemap_write_and_wait_range()
1920837bb1d7STrond Myklebust  *
1921837bb1d7STrond Myklebust  * Needed for pNFS in order to ensure data becomes visible to the
1922837bb1d7STrond Myklebust  * client.
1923837bb1d7STrond Myklebust  */
1924837bb1d7STrond Myklebust int nfs_filemap_write_and_wait_range(struct address_space *mapping,
1925837bb1d7STrond Myklebust 		loff_t lstart, loff_t lend)
1926837bb1d7STrond Myklebust {
1927837bb1d7STrond Myklebust 	int ret;
1928837bb1d7STrond Myklebust 
1929837bb1d7STrond Myklebust 	ret = filemap_write_and_wait_range(mapping, lstart, lend);
1930837bb1d7STrond Myklebust 	if (ret == 0)
1931837bb1d7STrond Myklebust 		ret = pnfs_sync_inode(mapping->host, true);
1932837bb1d7STrond Myklebust 	return ret;
1933837bb1d7STrond Myklebust }
1934837bb1d7STrond Myklebust EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range);
1935837bb1d7STrond Myklebust 
1936837bb1d7STrond Myklebust /*
1937acdc53b2STrond Myklebust  * flush the inode to disk.
1938acdc53b2STrond Myklebust  */
1939acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode)
194034901f70STrond Myklebust {
1941f4ce1299STrond Myklebust 	int ret;
194234901f70STrond Myklebust 
1943f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_enter(inode);
1944f4ce1299STrond Myklebust 
19455bb89b47STrond Myklebust 	ret = filemap_write_and_wait(inode->i_mapping);
19466b196875SChuck Lever 	if (ret)
19476b196875SChuck Lever 		goto out;
19485bb89b47STrond Myklebust 	ret = nfs_commit_inode(inode, FLUSH_SYNC);
19496b196875SChuck Lever 	if (ret < 0)
19506b196875SChuck Lever 		goto out;
19515bb89b47STrond Myklebust 	pnfs_sync_inode(inode, true);
19526b196875SChuck Lever 	ret = 0;
1953f4ce1299STrond Myklebust 
19546b196875SChuck Lever out:
1955f4ce1299STrond Myklebust 	trace_nfs_writeback_inode_exit(inode, ret);
1956f4ce1299STrond Myklebust 	return ret;
19571c75950bSTrond Myklebust }
1958ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all);
19591c75950bSTrond Myklebust 
19601b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
19611b3b4a1aSTrond Myklebust {
19621b3b4a1aSTrond Myklebust 	struct nfs_page *req;
19631b3b4a1aSTrond Myklebust 	int ret = 0;
19641b3b4a1aSTrond Myklebust 
1965ba8b06e6STrond Myklebust 	wait_on_page_writeback(page);
19663e217045SWeston Andros Adamson 
19673e217045SWeston Andros Adamson 	/* blocking call to cancel all requests and join to a single (head)
19683e217045SWeston Andros Adamson 	 * request */
19693e217045SWeston Andros Adamson 	req = nfs_lock_and_join_requests(page, false);
19703e217045SWeston Andros Adamson 
19713e217045SWeston Andros Adamson 	if (IS_ERR(req)) {
19723e217045SWeston Andros Adamson 		ret = PTR_ERR(req);
19733e217045SWeston Andros Adamson 	} else if (req) {
19743e217045SWeston Andros Adamson 		/* all requests from this page have been cancelled by
19753e217045SWeston Andros Adamson 		 * nfs_lock_and_join_requests, so just remove the head
19763e217045SWeston Andros Adamson 		 * request from the inode / page_private pointer and
19773e217045SWeston Andros Adamson 		 * release it */
19781b3b4a1aSTrond Myklebust 		nfs_inode_remove_request(req);
19791d1afcbcSTrond Myklebust 		nfs_unlock_and_release_request(req);
19801b3b4a1aSTrond Myklebust 	}
19813e217045SWeston Andros Adamson 
19821b3b4a1aSTrond Myklebust 	return ret;
19831b3b4a1aSTrond Myklebust }
19841b3b4a1aSTrond Myklebust 
19851c75950bSTrond Myklebust /*
19861c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
19871c75950bSTrond Myklebust  */
1988d6c843b9SPeng Tao int nfs_wb_single_page(struct inode *inode, struct page *page, bool launder)
19891c75950bSTrond Myklebust {
199029418aa4SMel Gorman 	loff_t range_start = page_file_offset(page);
199109cbfeafSKirill A. Shutemov 	loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1);
19927f2f12d9STrond Myklebust 	struct writeback_control wbc = {
19937f2f12d9STrond Myklebust 		.sync_mode = WB_SYNC_ALL,
19947f2f12d9STrond Myklebust 		.nr_to_write = 0,
19957f2f12d9STrond Myklebust 		.range_start = range_start,
19967f2f12d9STrond Myklebust 		.range_end = range_end,
19977f2f12d9STrond Myklebust 	};
19987f2f12d9STrond Myklebust 	int ret;
19997f2f12d9STrond Myklebust 
2000f4ce1299STrond Myklebust 	trace_nfs_writeback_page_enter(inode);
2001f4ce1299STrond Myklebust 
20020522f6adSTrond Myklebust 	for (;;) {
2003ba8b06e6STrond Myklebust 		wait_on_page_writeback(page);
20047f2f12d9STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
2005d6c843b9SPeng Tao 			ret = nfs_writepage_locked(page, &wbc, launder);
20067f2f12d9STrond Myklebust 			if (ret < 0)
20077f2f12d9STrond Myklebust 				goto out_error;
20080522f6adSTrond Myklebust 			continue;
20097f2f12d9STrond Myklebust 		}
2010f4ce1299STrond Myklebust 		ret = 0;
20110522f6adSTrond Myklebust 		if (!PagePrivate(page))
20120522f6adSTrond Myklebust 			break;
20130522f6adSTrond Myklebust 		ret = nfs_commit_inode(inode, FLUSH_SYNC);
20147f2f12d9STrond Myklebust 		if (ret < 0)
20157f2f12d9STrond Myklebust 			goto out_error;
20167f2f12d9STrond Myklebust 	}
20177f2f12d9STrond Myklebust out_error:
2018f4ce1299STrond Myklebust 	trace_nfs_writeback_page_exit(inode, ret);
20197f2f12d9STrond Myklebust 	return ret;
20201c75950bSTrond Myklebust }
20211c75950bSTrond Myklebust 
2022074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION
2023074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
2024a6bc32b8SMel Gorman 		struct page *page, enum migrate_mode mode)
2025074cc1deSTrond Myklebust {
20262da95652SJeff Layton 	/*
20272da95652SJeff Layton 	 * If PagePrivate is set, then the page is currently associated with
20282da95652SJeff Layton 	 * an in-progress read or write request. Don't try to migrate it.
20292da95652SJeff Layton 	 *
20302da95652SJeff Layton 	 * FIXME: we could do this in principle, but we'll need a way to ensure
20312da95652SJeff Layton 	 *        that we can safely release the inode reference while holding
20322da95652SJeff Layton 	 *        the page lock.
20332da95652SJeff Layton 	 */
20342da95652SJeff Layton 	if (PagePrivate(page))
20352da95652SJeff Layton 		return -EBUSY;
2036074cc1deSTrond Myklebust 
20378c209ce7SDavid Howells 	if (!nfs_fscache_release_page(page, GFP_KERNEL))
20388c209ce7SDavid Howells 		return -EBUSY;
2039074cc1deSTrond Myklebust 
2040a6bc32b8SMel Gorman 	return migrate_page(mapping, newpage, page, mode);
2041074cc1deSTrond Myklebust }
2042074cc1deSTrond Myklebust #endif
2043074cc1deSTrond Myklebust 
2044f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
20451da177e4SLinus Torvalds {
20461da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
20471e7f3a48SWeston Andros Adamson 					     sizeof(struct nfs_pgio_header),
20481da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
204920c2df83SPaul Mundt 					     NULL);
20501da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
20511da177e4SLinus Torvalds 		return -ENOMEM;
20521da177e4SLinus Torvalds 
205393d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
20541da177e4SLinus Torvalds 						     nfs_wdata_cachep);
20551da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
20563dd4765fSJeff Layton 		goto out_destroy_write_cache;
20571da177e4SLinus Torvalds 
20580b7c0153SFred Isaman 	nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
20590b7c0153SFred Isaman 					     sizeof(struct nfs_commit_data),
20600b7c0153SFred Isaman 					     0, SLAB_HWCACHE_ALIGN,
20610b7c0153SFred Isaman 					     NULL);
20620b7c0153SFred Isaman 	if (nfs_cdata_cachep == NULL)
20633dd4765fSJeff Layton 		goto out_destroy_write_mempool;
20640b7c0153SFred Isaman 
206593d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
20664c100210SYanchuan Nian 						      nfs_cdata_cachep);
20671da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
20683dd4765fSJeff Layton 		goto out_destroy_commit_cache;
20691da177e4SLinus Torvalds 
207089a09141SPeter Zijlstra 	/*
207189a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
207289a09141SPeter Zijlstra 	 *
207389a09141SPeter Zijlstra 	 *  64MB:    8192k
207489a09141SPeter Zijlstra 	 * 128MB:   11585k
207589a09141SPeter Zijlstra 	 * 256MB:   16384k
207689a09141SPeter Zijlstra 	 * 512MB:   23170k
207789a09141SPeter Zijlstra 	 *   1GB:   32768k
207889a09141SPeter Zijlstra 	 *   2GB:   46340k
207989a09141SPeter Zijlstra 	 *   4GB:   65536k
208089a09141SPeter Zijlstra 	 *   8GB:   92681k
208189a09141SPeter Zijlstra 	 *  16GB:  131072k
208289a09141SPeter Zijlstra 	 *
208389a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
208489a09141SPeter Zijlstra 	 * Limit the default to 256M
208589a09141SPeter Zijlstra 	 */
208689a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
208789a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
208889a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
208989a09141SPeter Zijlstra 
20901da177e4SLinus Torvalds 	return 0;
20913dd4765fSJeff Layton 
20923dd4765fSJeff Layton out_destroy_commit_cache:
20933dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
20943dd4765fSJeff Layton out_destroy_write_mempool:
20953dd4765fSJeff Layton 	mempool_destroy(nfs_wdata_mempool);
20963dd4765fSJeff Layton out_destroy_write_cache:
20973dd4765fSJeff Layton 	kmem_cache_destroy(nfs_wdata_cachep);
20983dd4765fSJeff Layton 	return -ENOMEM;
20991da177e4SLinus Torvalds }
21001da177e4SLinus Torvalds 
2101266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
21021da177e4SLinus Torvalds {
21031da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
21043dd4765fSJeff Layton 	kmem_cache_destroy(nfs_cdata_cachep);
21051da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
21061a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
21071da177e4SLinus Torvalds }
21081da177e4SLinus Torvalds 
21094a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops = {
2110a4cdda59SAnna Schumaker 	.rw_mode		= FMODE_WRITE,
21114a0de55cSAnna Schumaker 	.rw_alloc_header	= nfs_writehdr_alloc,
21124a0de55cSAnna Schumaker 	.rw_free_header		= nfs_writehdr_free,
21130eecb214SAnna Schumaker 	.rw_done		= nfs_writeback_done,
21140eecb214SAnna Schumaker 	.rw_result		= nfs_writeback_result,
21151ed26f33SAnna Schumaker 	.rw_initiate		= nfs_initiate_write,
21164a0de55cSAnna Schumaker };
2117