xref: /openbmc/linux/fs/nfs/write.c (revision 2b82f190)
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>
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
181da177e4SLinus Torvalds #include <linux/nfs_fs.h>
191da177e4SLinus Torvalds #include <linux/nfs_mount.h>
201da177e4SLinus Torvalds #include <linux/nfs_page.h>
213fcfab16SAndrew Morton #include <linux/backing-dev.h>
223fcfab16SAndrew Morton 
231da177e4SLinus Torvalds #include <asm/uaccess.h>
241da177e4SLinus Torvalds #include <linux/smp_lock.h>
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #include "delegation.h"
2749a70f27STrond Myklebust #include "internal.h"
2891d5b470SChuck Lever #include "iostat.h"
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #define MIN_POOL_WRITE		(32)
331da177e4SLinus Torvalds #define MIN_POOL_COMMIT		(4)
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds /*
361da177e4SLinus Torvalds  * Local function declarations
371da177e4SLinus Torvalds  */
381da177e4SLinus Torvalds static struct nfs_page * nfs_update_request(struct nfs_open_context*,
391da177e4SLinus Torvalds 					    struct page *,
401da177e4SLinus Torvalds 					    unsigned int, unsigned int);
413f442547STrond Myklebust static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how);
42788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops;
43788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops;
44788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops;
451da177e4SLinus Torvalds 
46e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep;
473feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
481da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
491da177e4SLinus Torvalds 
50e9f7bee1STrond Myklebust struct nfs_write_data *nfs_commit_alloc(void)
511da177e4SLinus Torvalds {
52e6b4f8daSChristoph Lameter 	struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
5340859d7eSChuck Lever 
541da177e4SLinus Torvalds 	if (p) {
551da177e4SLinus Torvalds 		memset(p, 0, sizeof(*p));
561da177e4SLinus Torvalds 		INIT_LIST_HEAD(&p->pages);
571da177e4SLinus Torvalds 	}
581da177e4SLinus Torvalds 	return p;
591da177e4SLinus Torvalds }
601da177e4SLinus Torvalds 
618aca67f0STrond Myklebust void nfs_commit_rcu_free(struct rcu_head *head)
621da177e4SLinus Torvalds {
638aca67f0STrond Myklebust 	struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
6440859d7eSChuck Lever 	if (p && (p->pagevec != &p->page_array[0]))
6540859d7eSChuck Lever 		kfree(p->pagevec);
661da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds 
698aca67f0STrond Myklebust void nfs_commit_free(struct nfs_write_data *wdata)
708aca67f0STrond Myklebust {
718aca67f0STrond Myklebust 	call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
728aca67f0STrond Myklebust }
738aca67f0STrond Myklebust 
74e9f7bee1STrond Myklebust struct nfs_write_data *nfs_writedata_alloc(size_t len)
753feb2d49STrond Myklebust {
76e9f7bee1STrond Myklebust 	unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
77e6b4f8daSChristoph Lameter 	struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
783feb2d49STrond Myklebust 
793feb2d49STrond Myklebust 	if (p) {
803feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
813feb2d49STrond Myklebust 		INIT_LIST_HEAD(&p->pages);
82e9f7bee1STrond Myklebust 		p->npages = pagecount;
830d0b5cb3SChuck Lever 		if (pagecount <= ARRAY_SIZE(p->page_array))
840d0b5cb3SChuck Lever 			p->pagevec = p->page_array;
853feb2d49STrond Myklebust 		else {
860d0b5cb3SChuck Lever 			p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
870d0b5cb3SChuck Lever 			if (!p->pagevec) {
883feb2d49STrond Myklebust 				mempool_free(p, nfs_wdata_mempool);
893feb2d49STrond Myklebust 				p = NULL;
903feb2d49STrond Myklebust 			}
913feb2d49STrond Myklebust 		}
923feb2d49STrond Myklebust 	}
933feb2d49STrond Myklebust 	return p;
943feb2d49STrond Myklebust }
953feb2d49STrond Myklebust 
968aca67f0STrond Myklebust static void nfs_writedata_rcu_free(struct rcu_head *head)
973feb2d49STrond Myklebust {
988aca67f0STrond Myklebust 	struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
993feb2d49STrond Myklebust 	if (p && (p->pagevec != &p->page_array[0]))
1003feb2d49STrond Myklebust 		kfree(p->pagevec);
1013feb2d49STrond Myklebust 	mempool_free(p, nfs_wdata_mempool);
1023feb2d49STrond Myklebust }
1033feb2d49STrond Myklebust 
1048aca67f0STrond Myklebust static void nfs_writedata_free(struct nfs_write_data *wdata)
1058aca67f0STrond Myklebust {
1068aca67f0STrond Myklebust 	call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
1078aca67f0STrond Myklebust }
1088aca67f0STrond Myklebust 
109963d8fe5STrond Myklebust void nfs_writedata_release(void *wdata)
1101da177e4SLinus Torvalds {
1111da177e4SLinus Torvalds 	nfs_writedata_free(wdata);
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
114277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request_locked(struct page *page)
115277459d2STrond Myklebust {
116277459d2STrond Myklebust 	struct nfs_page *req = NULL;
117277459d2STrond Myklebust 
118277459d2STrond Myklebust 	if (PagePrivate(page)) {
119277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
120277459d2STrond Myklebust 		if (req != NULL)
121277459d2STrond Myklebust 			atomic_inc(&req->wb_count);
122277459d2STrond Myklebust 	}
123277459d2STrond Myklebust 	return req;
124277459d2STrond Myklebust }
125277459d2STrond Myklebust 
126277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page)
127277459d2STrond Myklebust {
128277459d2STrond Myklebust 	struct nfs_page *req = NULL;
129277459d2STrond Myklebust 	spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
130277459d2STrond Myklebust 
131277459d2STrond Myklebust 	spin_lock(req_lock);
132277459d2STrond Myklebust 	req = nfs_page_find_request_locked(page);
133277459d2STrond Myklebust 	spin_unlock(req_lock);
134277459d2STrond Myklebust 	return req;
135277459d2STrond Myklebust }
136277459d2STrond Myklebust 
1371da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1381da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1391da177e4SLinus Torvalds {
1401da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
1411da177e4SLinus Torvalds 	loff_t end, i_size = i_size_read(inode);
1421da177e4SLinus Torvalds 	unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	if (i_size > 0 && page->index < end_index)
1451da177e4SLinus Torvalds 		return;
1461da177e4SLinus Torvalds 	end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
1471da177e4SLinus Torvalds 	if (i_size >= end)
1481da177e4SLinus Torvalds 		return;
14991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
1501da177e4SLinus Torvalds 	i_size_write(inode, end);
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
153a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
154a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
155a301b777STrond Myklebust {
156a301b777STrond Myklebust 	SetPageError(page);
157a301b777STrond Myklebust 	nfs_zap_mapping(page->mapping->host, page->mapping);
158a301b777STrond Myklebust }
159a301b777STrond Myklebust 
1601da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
1611da177e4SLinus Torvalds  * covers the full page.
1621da177e4SLinus Torvalds  */
1631da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
1641da177e4SLinus Torvalds {
1651da177e4SLinus Torvalds 	if (PageUptodate(page))
1661da177e4SLinus Torvalds 		return;
1671da177e4SLinus Torvalds 	if (base != 0)
1681da177e4SLinus Torvalds 		return;
16949a70f27STrond Myklebust 	if (count != nfs_page_length(page))
1701da177e4SLinus Torvalds 		return;
17149a70f27STrond Myklebust 	if (count != PAGE_CACHE_SIZE)
1721da177e4SLinus Torvalds 		memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
1731da177e4SLinus Torvalds 	SetPageUptodate(page);
1741da177e4SLinus Torvalds }
1751da177e4SLinus Torvalds 
176e21195a7STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
1771da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
1781da177e4SLinus Torvalds {
1791da177e4SLinus Torvalds 	struct nfs_page	*req;
180e21195a7STrond Myklebust 	int ret;
1811da177e4SLinus Torvalds 
182e21195a7STrond Myklebust 	for (;;) {
183e21195a7STrond Myklebust 		req = nfs_update_request(ctx, page, offset, count);
184e21195a7STrond Myklebust 		if (!IS_ERR(req))
185e21195a7STrond Myklebust 			break;
186e21195a7STrond Myklebust 		ret = PTR_ERR(req);
187e21195a7STrond Myklebust 		if (ret != -EBUSY)
188e21195a7STrond Myklebust 			return ret;
189e21195a7STrond Myklebust 		ret = nfs_wb_page(page->mapping->host, page);
190e21195a7STrond Myklebust 		if (ret != 0)
191e21195a7STrond Myklebust 			return ret;
192e21195a7STrond Myklebust 	}
1931da177e4SLinus Torvalds 	/* Update file length */
1941da177e4SLinus Torvalds 	nfs_grow_file(page, offset, count);
1951da177e4SLinus Torvalds 	/* Set the PG_uptodate flag? */
1961da177e4SLinus Torvalds 	nfs_mark_uptodate(page, offset, count);
1971da177e4SLinus Torvalds 	nfs_unlock_request(req);
198abd3e641STrond Myklebust 	return 0;
1991da177e4SLinus Torvalds }
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
2021da177e4SLinus Torvalds {
2031da177e4SLinus Torvalds 	if (wbc->for_reclaim)
2041da177e4SLinus Torvalds 		return FLUSH_HIGHPRI;
2051da177e4SLinus Torvalds 	if (wbc->for_kupdate)
2061da177e4SLinus Torvalds 		return FLUSH_LOWPRI;
2071da177e4SLinus Torvalds 	return 0;
2081da177e4SLinus Torvalds }
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds /*
21189a09141SPeter Zijlstra  * NFS congestion control
21289a09141SPeter Zijlstra  */
21389a09141SPeter Zijlstra 
21489a09141SPeter Zijlstra int nfs_congestion_kb;
21589a09141SPeter Zijlstra 
21689a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
21789a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
21889a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
21989a09141SPeter Zijlstra 
2205a6d41b3STrond Myklebust static int nfs_set_page_writeback(struct page *page)
22189a09141SPeter Zijlstra {
2225a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
2235a6d41b3STrond Myklebust 
2245a6d41b3STrond Myklebust 	if (!ret) {
22589a09141SPeter Zijlstra 		struct inode *inode = page->mapping->host;
22689a09141SPeter Zijlstra 		struct nfs_server *nfss = NFS_SERVER(inode);
22789a09141SPeter Zijlstra 
22889a09141SPeter Zijlstra 		if (atomic_inc_return(&nfss->writeback) >
22989a09141SPeter Zijlstra 				NFS_CONGESTION_ON_THRESH)
23089a09141SPeter Zijlstra 			set_bdi_congested(&nfss->backing_dev_info, WRITE);
23189a09141SPeter Zijlstra 	}
2325a6d41b3STrond Myklebust 	return ret;
23389a09141SPeter Zijlstra }
23489a09141SPeter Zijlstra 
23589a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page)
23689a09141SPeter Zijlstra {
23789a09141SPeter Zijlstra 	struct inode *inode = page->mapping->host;
23889a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
23989a09141SPeter Zijlstra 
24089a09141SPeter Zijlstra 	end_page_writeback(page);
24189a09141SPeter Zijlstra 	if (atomic_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) {
24289a09141SPeter Zijlstra 		clear_bdi_congested(&nfss->backing_dev_info, WRITE);
24389a09141SPeter Zijlstra 		congestion_end(WRITE);
24489a09141SPeter Zijlstra 	}
24589a09141SPeter Zijlstra }
24689a09141SPeter Zijlstra 
24789a09141SPeter Zijlstra /*
248e261f51fSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
249e261f51fSTrond Myklebust  * Returns 1 if there was no write request, or if the request was
250e261f51fSTrond Myklebust  * already tagged by nfs_set_page_dirty.Returns 0 if the request
251e261f51fSTrond Myklebust  * was not tagged.
252e261f51fSTrond Myklebust  * May also return an error if the user signalled nfs_wait_on_request().
253e261f51fSTrond Myklebust  */
254e261f51fSTrond Myklebust static int nfs_page_mark_flush(struct page *page)
255e261f51fSTrond Myklebust {
256e261f51fSTrond Myklebust 	struct nfs_page *req;
257612c9384STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(page->mapping->host);
258612c9384STrond Myklebust 	spinlock_t *req_lock = &nfsi->req_lock;
259e261f51fSTrond Myklebust 	int ret;
260e261f51fSTrond Myklebust 
261e261f51fSTrond Myklebust 	spin_lock(req_lock);
262e261f51fSTrond Myklebust 	for(;;) {
263e261f51fSTrond Myklebust 		req = nfs_page_find_request_locked(page);
264e261f51fSTrond Myklebust 		if (req == NULL) {
265e261f51fSTrond Myklebust 			spin_unlock(req_lock);
266e261f51fSTrond Myklebust 			return 1;
267e261f51fSTrond Myklebust 		}
268e261f51fSTrond Myklebust 		if (nfs_lock_request_dontget(req))
269e261f51fSTrond Myklebust 			break;
270e261f51fSTrond Myklebust 		/* Note: If we hold the page lock, as is the case in nfs_writepage,
271e261f51fSTrond Myklebust 		 *	 then the call to nfs_lock_request_dontget() will always
272e261f51fSTrond Myklebust 		 *	 succeed provided that someone hasn't already marked the
273e261f51fSTrond Myklebust 		 *	 request as dirty (in which case we don't care).
274e261f51fSTrond Myklebust 		 */
275e261f51fSTrond Myklebust 		spin_unlock(req_lock);
276e261f51fSTrond Myklebust 		ret = nfs_wait_on_request(req);
277e261f51fSTrond Myklebust 		nfs_release_request(req);
278e261f51fSTrond Myklebust 		if (ret != 0)
279e261f51fSTrond Myklebust 			return ret;
280e261f51fSTrond Myklebust 		spin_lock(req_lock);
281e261f51fSTrond Myklebust 	}
282612c9384STrond Myklebust 	if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
283612c9384STrond Myklebust 		/* This request is marked for commit */
284e261f51fSTrond Myklebust 		spin_unlock(req_lock);
285612c9384STrond Myklebust 		nfs_unlock_request(req);
286612c9384STrond Myklebust 		return 1;
287612c9384STrond Myklebust 	}
288eb4cac10STrond Myklebust 	if (nfs_set_page_writeback(page) == 0) {
289eb4cac10STrond Myklebust 		nfs_list_remove_request(req);
290612c9384STrond Myklebust 		/* add the request to the inode's dirty list. */
291612c9384STrond Myklebust 		radix_tree_tag_set(&nfsi->nfs_page_tree,
292612c9384STrond Myklebust 				req->wb_index, NFS_PAGE_TAG_DIRTY);
293612c9384STrond Myklebust 		nfs_list_add_request(req, &nfsi->dirty);
294612c9384STrond Myklebust 		nfsi->ndirty++;
295612c9384STrond Myklebust 		spin_unlock(req_lock);
296612c9384STrond Myklebust 		__mark_inode_dirty(page->mapping->host, I_DIRTY_PAGES);
297612c9384STrond Myklebust 	} else
298612c9384STrond Myklebust 		spin_unlock(req_lock);
299e261f51fSTrond Myklebust 	ret = test_bit(PG_NEED_FLUSH, &req->wb_flags);
300e261f51fSTrond Myklebust 	nfs_unlock_request(req);
301e261f51fSTrond Myklebust 	return ret;
302e261f51fSTrond Myklebust }
303e261f51fSTrond Myklebust 
304e261f51fSTrond Myklebust /*
3051da177e4SLinus Torvalds  * Write an mmapped page to the server.
3061da177e4SLinus Torvalds  */
3074d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
3081da177e4SLinus Torvalds {
3091da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
3101da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
31149a70f27STrond Myklebust 	unsigned offset;
312e261f51fSTrond Myklebust 	int err;
3131da177e4SLinus Torvalds 
31491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
31591d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
31691d5b470SChuck Lever 
317e261f51fSTrond Myklebust 	err = nfs_page_mark_flush(page);
318e261f51fSTrond Myklebust 	if (err <= 0)
3191a54533eSTrond Myklebust 		goto out;
320e261f51fSTrond Myklebust 	err = 0;
32149a70f27STrond Myklebust 	offset = nfs_page_length(page);
32249a70f27STrond Myklebust 	if (!offset)
3231da177e4SLinus Torvalds 		goto out;
32449a70f27STrond Myklebust 
325d530838bSTrond Myklebust 	ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
3261da177e4SLinus Torvalds 	if (ctx == NULL) {
3271da177e4SLinus Torvalds 		err = -EBADF;
3281da177e4SLinus Torvalds 		goto out;
3291da177e4SLinus Torvalds 	}
330e21195a7STrond Myklebust 	err = nfs_writepage_setup(ctx, page, 0, offset);
331200baa21STrond Myklebust 	put_nfs_open_context(ctx);
332e261f51fSTrond Myklebust 	if (err != 0)
333e261f51fSTrond Myklebust 		goto out;
334e261f51fSTrond Myklebust 	err = nfs_page_mark_flush(page);
335e261f51fSTrond Myklebust 	if (err > 0)
336e261f51fSTrond Myklebust 		err = 0;
337200baa21STrond Myklebust out:
338abd3e641STrond Myklebust 	if (!wbc->for_writepages)
33902241bc4STrond Myklebust 		nfs_flush_mapping(page->mapping, wbc, FLUSH_STABLE|wb_priority(wbc));
3404d770ccfSTrond Myklebust 	return err;
3414d770ccfSTrond Myklebust }
3424d770ccfSTrond Myklebust 
3434d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
3444d770ccfSTrond Myklebust {
3454d770ccfSTrond Myklebust 	int err;
3464d770ccfSTrond Myklebust 
3474d770ccfSTrond Myklebust 	err = nfs_writepage_locked(page, wbc);
3481da177e4SLinus Torvalds 	unlock_page(page);
3491da177e4SLinus Torvalds 	return err;
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3531da177e4SLinus Torvalds {
3541da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
3551da177e4SLinus Torvalds 	int err;
3561da177e4SLinus Torvalds 
35791d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
35891d5b470SChuck Lever 
3591da177e4SLinus Torvalds 	err = generic_writepages(mapping, wbc);
3601da177e4SLinus Torvalds 	if (err)
3611da177e4SLinus Torvalds 		return err;
36228c6925fSTrond Myklebust 	err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
3631da177e4SLinus Torvalds 	if (err < 0)
3641da177e4SLinus Torvalds 		goto out;
36591d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
3661da177e4SLinus Torvalds 	err = 0;
3671da177e4SLinus Torvalds out:
3681da177e4SLinus Torvalds 	return err;
3691da177e4SLinus Torvalds }
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds /*
3721da177e4SLinus Torvalds  * Insert a write request into an inode
3731da177e4SLinus Torvalds  */
3741da177e4SLinus Torvalds static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
3751da177e4SLinus Torvalds {
3761da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
3771da177e4SLinus Torvalds 	int error;
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
3801da177e4SLinus Torvalds 	BUG_ON(error == -EEXIST);
3811da177e4SLinus Torvalds 	if (error)
3821da177e4SLinus Torvalds 		return error;
3831da177e4SLinus Torvalds 	if (!nfsi->npages) {
3841da177e4SLinus Torvalds 		igrab(inode);
3851da177e4SLinus Torvalds 		nfs_begin_data_update(inode);
3861da177e4SLinus Torvalds 		if (nfs_have_delegation(inode, FMODE_WRITE))
3871da177e4SLinus Torvalds 			nfsi->change_attr++;
3881da177e4SLinus Torvalds 	}
389deb7d638STrond Myklebust 	SetPagePrivate(req->wb_page);
390277459d2STrond Myklebust 	set_page_private(req->wb_page, (unsigned long)req);
3912b82f190STrond Myklebust 	if (PageDirty(req->wb_page))
3922b82f190STrond Myklebust 		set_bit(PG_NEED_FLUSH, &req->wb_flags);
3931da177e4SLinus Torvalds 	nfsi->npages++;
3941da177e4SLinus Torvalds 	atomic_inc(&req->wb_count);
3951da177e4SLinus Torvalds 	return 0;
3961da177e4SLinus Torvalds }
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds /*
39989a09141SPeter Zijlstra  * Remove a write request from an inode
4001da177e4SLinus Torvalds  */
4011da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
4021da177e4SLinus Torvalds {
4031da177e4SLinus Torvalds 	struct inode *inode = req->wb_context->dentry->d_inode;
4041da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	BUG_ON (!NFS_WBACK_BUSY(req));
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	spin_lock(&nfsi->req_lock);
409277459d2STrond Myklebust 	set_page_private(req->wb_page, 0);
410deb7d638STrond Myklebust 	ClearPagePrivate(req->wb_page);
4111da177e4SLinus Torvalds 	radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
4122b82f190STrond Myklebust 	if (test_and_clear_bit(PG_NEED_FLUSH, &req->wb_flags))
4132b82f190STrond Myklebust 		__set_page_dirty_nobuffers(req->wb_page);
4141da177e4SLinus Torvalds 	nfsi->npages--;
4151da177e4SLinus Torvalds 	if (!nfsi->npages) {
4161da177e4SLinus Torvalds 		spin_unlock(&nfsi->req_lock);
417951a143bSTrond Myklebust 		nfs_end_data_update(inode);
4181da177e4SLinus Torvalds 		iput(inode);
4191da177e4SLinus Torvalds 	} else
4201da177e4SLinus Torvalds 		spin_unlock(&nfsi->req_lock);
4211da177e4SLinus Torvalds 	nfs_clear_request(req);
4221da177e4SLinus Torvalds 	nfs_release_request(req);
4231da177e4SLinus Torvalds }
4241da177e4SLinus Torvalds 
42561822ab5STrond Myklebust static void
42661822ab5STrond Myklebust nfs_redirty_request(struct nfs_page *req)
42761822ab5STrond Myklebust {
42861822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
42961822ab5STrond Myklebust }
43061822ab5STrond Myklebust 
4311da177e4SLinus Torvalds /*
4321da177e4SLinus Torvalds  * Check if a request is dirty
4331da177e4SLinus Torvalds  */
4341da177e4SLinus Torvalds static inline int
4351da177e4SLinus Torvalds nfs_dirty_request(struct nfs_page *req)
4361da177e4SLinus Torvalds {
4375a6d41b3STrond Myklebust 	struct page *page = req->wb_page;
4385a6d41b3STrond Myklebust 
439612c9384STrond Myklebust 	if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
4405a6d41b3STrond Myklebust 		return 0;
4415a6d41b3STrond Myklebust 	return !PageWriteback(req->wb_page);
4421da177e4SLinus Torvalds }
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
4451da177e4SLinus Torvalds /*
4461da177e4SLinus Torvalds  * Add a request to the inode's commit list.
4471da177e4SLinus Torvalds  */
4481da177e4SLinus Torvalds static void
4491da177e4SLinus Torvalds nfs_mark_request_commit(struct nfs_page *req)
4501da177e4SLinus Torvalds {
4511da177e4SLinus Torvalds 	struct inode *inode = req->wb_context->dentry->d_inode;
4521da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	spin_lock(&nfsi->req_lock);
4551da177e4SLinus Torvalds 	nfs_list_add_request(req, &nfsi->commit);
4561da177e4SLinus Torvalds 	nfsi->ncommit++;
457612c9384STrond Myklebust 	set_bit(PG_NEED_COMMIT, &(req)->wb_flags);
4581da177e4SLinus Torvalds 	spin_unlock(&nfsi->req_lock);
459fd39fc85SChristoph Lameter 	inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
460a1803044STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
4611da177e4SLinus Torvalds }
4628e821cadSTrond Myklebust 
4638e821cadSTrond Myklebust static inline
4648e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
4658e821cadSTrond Myklebust {
4668e821cadSTrond Myklebust 	return data->verf.committed != NFS_FILE_SYNC;
4678e821cadSTrond Myklebust }
4688e821cadSTrond Myklebust 
4698e821cadSTrond Myklebust static inline
4708e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req)
4718e821cadSTrond Myklebust {
472612c9384STrond Myklebust 	if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
4738e821cadSTrond Myklebust 		nfs_mark_request_commit(req);
4748e821cadSTrond Myklebust 		return 1;
4758e821cadSTrond Myklebust 	}
4768e821cadSTrond Myklebust 	if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
4778e821cadSTrond Myklebust 		nfs_redirty_request(req);
4788e821cadSTrond Myklebust 		return 1;
4798e821cadSTrond Myklebust 	}
4808e821cadSTrond Myklebust 	return 0;
4818e821cadSTrond Myklebust }
4828e821cadSTrond Myklebust #else
4838e821cadSTrond Myklebust static inline void
4848e821cadSTrond Myklebust nfs_mark_request_commit(struct nfs_page *req)
4858e821cadSTrond Myklebust {
4868e821cadSTrond Myklebust }
4878e821cadSTrond Myklebust 
4888e821cadSTrond Myklebust static inline
4898e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
4908e821cadSTrond Myklebust {
4918e821cadSTrond Myklebust 	return 0;
4928e821cadSTrond Myklebust }
4938e821cadSTrond Myklebust 
4948e821cadSTrond Myklebust static inline
4958e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req)
4968e821cadSTrond Myklebust {
4978e821cadSTrond Myklebust 	return 0;
4988e821cadSTrond Myklebust }
4991da177e4SLinus Torvalds #endif
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds /*
5021da177e4SLinus Torvalds  * Wait for a request to complete.
5031da177e4SLinus Torvalds  *
5041da177e4SLinus Torvalds  * Interruptible by signals only if mounted with intr flag.
5051da177e4SLinus Torvalds  */
506c42de9ddSTrond Myklebust static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
5071da177e4SLinus Torvalds {
5081da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
5091da177e4SLinus Torvalds 	struct nfs_page *req;
5101da177e4SLinus Torvalds 	unsigned long		idx_end, next;
5111da177e4SLinus Torvalds 	unsigned int		res = 0;
5121da177e4SLinus Torvalds 	int			error;
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	if (npages == 0)
5151da177e4SLinus Torvalds 		idx_end = ~0;
5161da177e4SLinus Torvalds 	else
5171da177e4SLinus Torvalds 		idx_end = idx_start + npages - 1;
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 	next = idx_start;
520c6a556b8STrond Myklebust 	while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
5211da177e4SLinus Torvalds 		if (req->wb_index > idx_end)
5221da177e4SLinus Torvalds 			break;
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 		next = req->wb_index + 1;
525c6a556b8STrond Myklebust 		BUG_ON(!NFS_WBACK_BUSY(req));
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 		atomic_inc(&req->wb_count);
5281da177e4SLinus Torvalds 		spin_unlock(&nfsi->req_lock);
5291da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
5301da177e4SLinus Torvalds 		nfs_release_request(req);
531c42de9ddSTrond Myklebust 		spin_lock(&nfsi->req_lock);
5321da177e4SLinus Torvalds 		if (error < 0)
5331da177e4SLinus Torvalds 			return error;
5341da177e4SLinus Torvalds 		res++;
5351da177e4SLinus Torvalds 	}
5361da177e4SLinus Torvalds 	return res;
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds 
53983715ad5STrond Myklebust static void nfs_cancel_dirty_list(struct list_head *head)
540d2ccddf0STrond Myklebust {
541d2ccddf0STrond Myklebust 	struct nfs_page *req;
542d2ccddf0STrond Myklebust 	while(!list_empty(head)) {
543d2ccddf0STrond Myklebust 		req = nfs_list_entry(head->next);
544d2ccddf0STrond Myklebust 		nfs_list_remove_request(req);
5455a6d41b3STrond Myklebust 		nfs_end_page_writeback(req->wb_page);
546d2ccddf0STrond Myklebust 		nfs_inode_remove_request(req);
547d2ccddf0STrond Myklebust 		nfs_clear_page_writeback(req);
548d2ccddf0STrond Myklebust 	}
549d2ccddf0STrond Myklebust }
550d2ccddf0STrond Myklebust 
55183715ad5STrond Myklebust static void nfs_cancel_commit_list(struct list_head *head)
55283715ad5STrond Myklebust {
55383715ad5STrond Myklebust 	struct nfs_page *req;
55483715ad5STrond Myklebust 
55583715ad5STrond Myklebust 	while(!list_empty(head)) {
55683715ad5STrond Myklebust 		req = nfs_list_entry(head->next);
557b6dff26aSTrond Myklebust 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
55883715ad5STrond Myklebust 		nfs_list_remove_request(req);
559612c9384STrond Myklebust 		clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
56083715ad5STrond Myklebust 		nfs_inode_remove_request(req);
561b6dff26aSTrond Myklebust 		nfs_unlock_request(req);
56283715ad5STrond Myklebust 	}
56383715ad5STrond Myklebust }
56483715ad5STrond Myklebust 
5651da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
5661da177e4SLinus Torvalds /*
5671da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
5681da177e4SLinus Torvalds  * @inode: NFS inode to scan
5691da177e4SLinus Torvalds  * @dst: destination list
5701da177e4SLinus Torvalds  * @idx_start: lower bound of page->index to scan.
5711da177e4SLinus Torvalds  * @npages: idx_start + npages sets the upper bound to scan.
5721da177e4SLinus Torvalds  *
5731da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
5741da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
5751da177e4SLinus Torvalds  */
5761da177e4SLinus Torvalds static int
5771da177e4SLinus Torvalds nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
5781da177e4SLinus Torvalds {
5791da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
5803da28eb1STrond Myklebust 	int res = 0;
5813da28eb1STrond Myklebust 
5823da28eb1STrond Myklebust 	if (nfsi->ncommit != 0) {
583d2ccddf0STrond Myklebust 		res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
5841da177e4SLinus Torvalds 		nfsi->ncommit -= res;
5851da177e4SLinus Torvalds 		if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
5861da177e4SLinus Torvalds 			printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
5873da28eb1STrond Myklebust 	}
5881da177e4SLinus Torvalds 	return res;
5891da177e4SLinus Torvalds }
590c42de9ddSTrond Myklebust #else
591c42de9ddSTrond Myklebust static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
592c42de9ddSTrond Myklebust {
593c42de9ddSTrond Myklebust 	return 0;
594c42de9ddSTrond Myklebust }
5951da177e4SLinus Torvalds #endif
5961da177e4SLinus Torvalds 
59789a09141SPeter Zijlstra static int nfs_wait_on_write_congestion(struct address_space *mapping)
5981da177e4SLinus Torvalds {
59989a09141SPeter Zijlstra 	struct inode *inode = mapping->host;
6001da177e4SLinus Torvalds 	struct backing_dev_info *bdi = mapping->backing_dev_info;
6011da177e4SLinus Torvalds 	int ret = 0;
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	might_sleep();
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	if (!bdi_write_congested(bdi))
6061da177e4SLinus Torvalds 		return 0;
60791d5b470SChuck Lever 
60889a09141SPeter Zijlstra 	nfs_inc_stats(inode, NFSIOS_CONGESTIONWAIT);
60991d5b470SChuck Lever 
61089a09141SPeter Zijlstra 	do {
61189a09141SPeter Zijlstra 		struct rpc_clnt *clnt = NFS_CLIENT(inode);
6121da177e4SLinus Torvalds 		sigset_t oldset;
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 		rpc_clnt_sigmask(clnt, &oldset);
61589a09141SPeter Zijlstra 		ret = congestion_wait_interruptible(WRITE, HZ/10);
6161da177e4SLinus Torvalds 		rpc_clnt_sigunmask(clnt, &oldset);
61789a09141SPeter Zijlstra 		if (ret == -ERESTARTSYS)
61889a09141SPeter Zijlstra 			break;
61989a09141SPeter Zijlstra 		ret = 0;
62089a09141SPeter Zijlstra 	} while (bdi_write_congested(bdi));
62189a09141SPeter Zijlstra 
6221da177e4SLinus Torvalds 	return ret;
6231da177e4SLinus Torvalds }
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds /*
6261da177e4SLinus Torvalds  * Try to update any existing write request, or create one if there is none.
6271da177e4SLinus Torvalds  * In order to match, the request's credentials must match those of
6281da177e4SLinus Torvalds  * the calling process.
6291da177e4SLinus Torvalds  *
6301da177e4SLinus Torvalds  * Note: Should always be called with the Page Lock held!
6311da177e4SLinus Torvalds  */
6321da177e4SLinus Torvalds static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
633e21195a7STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
6341da177e4SLinus Torvalds {
63589a09141SPeter Zijlstra 	struct address_space *mapping = page->mapping;
63689a09141SPeter Zijlstra 	struct inode *inode = mapping->host;
6371da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
6381da177e4SLinus Torvalds 	struct nfs_page		*req, *new = NULL;
6391da177e4SLinus Torvalds 	unsigned long		rqend, end;
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	end = offset + bytes;
6421da177e4SLinus Torvalds 
64389a09141SPeter Zijlstra 	if (nfs_wait_on_write_congestion(mapping))
6441da177e4SLinus Torvalds 		return ERR_PTR(-ERESTARTSYS);
6451da177e4SLinus Torvalds 	for (;;) {
6461da177e4SLinus Torvalds 		/* Loop over all inode entries and see if we find
6471da177e4SLinus Torvalds 		 * A request for the page we wish to update
6481da177e4SLinus Torvalds 		 */
6491da177e4SLinus Torvalds 		spin_lock(&nfsi->req_lock);
650277459d2STrond Myklebust 		req = nfs_page_find_request_locked(page);
6511da177e4SLinus Torvalds 		if (req) {
6521da177e4SLinus Torvalds 			if (!nfs_lock_request_dontget(req)) {
6531da177e4SLinus Torvalds 				int error;
654277459d2STrond Myklebust 
6551da177e4SLinus Torvalds 				spin_unlock(&nfsi->req_lock);
6561da177e4SLinus Torvalds 				error = nfs_wait_on_request(req);
6571da177e4SLinus Torvalds 				nfs_release_request(req);
6581dd594b2SNeil Brown 				if (error < 0) {
6591dd594b2SNeil Brown 					if (new)
6601dd594b2SNeil Brown 						nfs_release_request(new);
6611da177e4SLinus Torvalds 					return ERR_PTR(error);
6621dd594b2SNeil Brown 				}
6631da177e4SLinus Torvalds 				continue;
6641da177e4SLinus Torvalds 			}
6651da177e4SLinus Torvalds 			spin_unlock(&nfsi->req_lock);
6661da177e4SLinus Torvalds 			if (new)
6671da177e4SLinus Torvalds 				nfs_release_request(new);
6681da177e4SLinus Torvalds 			break;
6691da177e4SLinus Torvalds 		}
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 		if (new) {
6721da177e4SLinus Torvalds 			int error;
6731da177e4SLinus Torvalds 			nfs_lock_request_dontget(new);
6741da177e4SLinus Torvalds 			error = nfs_inode_add_request(inode, new);
6751da177e4SLinus Torvalds 			if (error) {
6761da177e4SLinus Torvalds 				spin_unlock(&nfsi->req_lock);
6771da177e4SLinus Torvalds 				nfs_unlock_request(new);
6781da177e4SLinus Torvalds 				return ERR_PTR(error);
6791da177e4SLinus Torvalds 			}
6801da177e4SLinus Torvalds 			spin_unlock(&nfsi->req_lock);
6811da177e4SLinus Torvalds 			return new;
6821da177e4SLinus Torvalds 		}
6831da177e4SLinus Torvalds 		spin_unlock(&nfsi->req_lock);
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 		new = nfs_create_request(ctx, inode, page, offset, bytes);
6861da177e4SLinus Torvalds 		if (IS_ERR(new))
6871da177e4SLinus Torvalds 			return new;
6881da177e4SLinus Torvalds 	}
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 	/* We have a request for our page.
6911da177e4SLinus Torvalds 	 * If the creds don't match, or the
6921da177e4SLinus Torvalds 	 * page addresses don't match,
6931da177e4SLinus Torvalds 	 * tell the caller to wait on the conflicting
6941da177e4SLinus Torvalds 	 * request.
6951da177e4SLinus Torvalds 	 */
6961da177e4SLinus Torvalds 	rqend = req->wb_offset + req->wb_bytes;
6971da177e4SLinus Torvalds 	if (req->wb_context != ctx
6981da177e4SLinus Torvalds 	    || req->wb_page != page
6991da177e4SLinus Torvalds 	    || !nfs_dirty_request(req)
7001da177e4SLinus Torvalds 	    || offset > rqend || end < req->wb_offset) {
7011da177e4SLinus Torvalds 		nfs_unlock_request(req);
7021da177e4SLinus Torvalds 		return ERR_PTR(-EBUSY);
7031da177e4SLinus Torvalds 	}
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
7061da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
7071da177e4SLinus Torvalds 		req->wb_offset = offset;
7081da177e4SLinus Torvalds 		req->wb_pgbase = offset;
7091da177e4SLinus Torvalds 		req->wb_bytes = rqend - req->wb_offset;
7101da177e4SLinus Torvalds 	}
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 	if (end > rqend)
7131da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 	return req;
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
7191da177e4SLinus Torvalds {
7201da177e4SLinus Torvalds 	struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
7211da177e4SLinus Torvalds 	struct nfs_page	*req;
7221a54533eSTrond Myklebust 	int do_flush, status;
7231da177e4SLinus Torvalds 	/*
7241da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
7251da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
7261da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
7271da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
7281da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
7291da177e4SLinus Torvalds 	 * dropped page.
7301da177e4SLinus Torvalds 	 */
7311a54533eSTrond Myklebust 	do {
732277459d2STrond Myklebust 		req = nfs_page_find_request(page);
7331a54533eSTrond Myklebust 		if (req == NULL)
7341a54533eSTrond Myklebust 			return 0;
7351a54533eSTrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx
736e261f51fSTrond Myklebust 			|| !nfs_dirty_request(req);
7371da177e4SLinus Torvalds 		nfs_release_request(req);
7381a54533eSTrond Myklebust 		if (!do_flush)
7391a54533eSTrond Myklebust 			return 0;
740277459d2STrond Myklebust 		status = nfs_wb_page(page->mapping->host, page);
7411a54533eSTrond Myklebust 	} while (status == 0);
7421a54533eSTrond Myklebust 	return status;
7431da177e4SLinus Torvalds }
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds /*
7461da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
7471da177e4SLinus Torvalds  *
7481da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
7491da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
7501da177e4SLinus Torvalds  */
7511da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
7521da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
7531da177e4SLinus Torvalds {
7541da177e4SLinus Torvalds 	struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
7551da177e4SLinus Torvalds 	struct inode	*inode = page->mapping->host;
7561da177e4SLinus Torvalds 	int		status = 0;
7571da177e4SLinus Torvalds 
75891d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
75991d5b470SChuck Lever 
7601da177e4SLinus Torvalds 	dprintk("NFS:      nfs_updatepage(%s/%s %d@%Ld)\n",
76101cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
76201cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name, count,
7630bbacc40SChuck Lever 		(long long)(page_offset(page) +offset));
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	/* If we're not using byte range locks, and we know the page
7661da177e4SLinus Torvalds 	 * is entirely in cache, it may be more efficient to avoid
7671da177e4SLinus Torvalds 	 * fragmenting write requests.
7681da177e4SLinus Torvalds 	 */
769ab0a3dbeSTrond Myklebust 	if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
77049a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
7711da177e4SLinus Torvalds 		offset = 0;
7721da177e4SLinus Torvalds 	}
7731da177e4SLinus Torvalds 
774e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
775e261f51fSTrond Myklebust 	__set_page_dirty_nobuffers(page);
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds         dprintk("NFS:      nfs_updatepage returns %d (isize %Ld)\n",
7781da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
7791da177e4SLinus Torvalds 	if (status < 0)
780a301b777STrond Myklebust 		nfs_set_pageerror(page);
7811da177e4SLinus Torvalds 	return status;
7821da177e4SLinus Torvalds }
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds static void nfs_writepage_release(struct nfs_page *req)
7851da177e4SLinus Torvalds {
7868e821cadSTrond Myklebust 
7878e821cadSTrond Myklebust 	if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req)) {
78889a09141SPeter Zijlstra 		nfs_end_page_writeback(req->wb_page);
7891da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
7908e821cadSTrond Myklebust 	} else
7918e821cadSTrond Myklebust 		nfs_end_page_writeback(req->wb_page);
792c6a556b8STrond Myklebust 	nfs_clear_page_writeback(req);
7931da177e4SLinus Torvalds }
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds static inline int flush_task_priority(int how)
7961da177e4SLinus Torvalds {
7971da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
7981da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
7991da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
8001da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
8011da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
8021da177e4SLinus Torvalds 	}
8031da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
8041da177e4SLinus Torvalds }
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds /*
8071da177e4SLinus Torvalds  * Set up the argument/result storage required for the RPC call.
8081da177e4SLinus Torvalds  */
8091da177e4SLinus Torvalds static void nfs_write_rpcsetup(struct nfs_page *req,
8101da177e4SLinus Torvalds 		struct nfs_write_data *data,
811788e7a89STrond Myklebust 		const struct rpc_call_ops *call_ops,
8121da177e4SLinus Torvalds 		unsigned int count, unsigned int offset,
8131da177e4SLinus Torvalds 		int how)
8141da177e4SLinus Torvalds {
8151da177e4SLinus Torvalds 	struct inode		*inode;
816788e7a89STrond Myklebust 	int flags;
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
8191da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	data->req = req;
8221da177e4SLinus Torvalds 	data->inode = inode = req->wb_context->dentry->d_inode;
8231da177e4SLinus Torvalds 	data->cred = req->wb_context->cred;
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(inode);
8261da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
8271da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
8281da177e4SLinus Torvalds 	data->args.pages  = data->pagevec;
8291da177e4SLinus Torvalds 	data->args.count  = count;
8301da177e4SLinus Torvalds 	data->args.context = req->wb_context;
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
8331da177e4SLinus Torvalds 	data->res.count   = count;
8341da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
8350e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
8361da177e4SLinus Torvalds 
837788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
838788e7a89STrond Myklebust 	flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
839788e7a89STrond Myklebust 	rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
8401da177e4SLinus Torvalds 	NFS_PROTO(inode)->write_setup(data, how);
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	data->task.tk_priority = flush_task_priority(how);
8431da177e4SLinus Torvalds 	data->task.tk_cookie = (unsigned long)inode;
8441da177e4SLinus Torvalds 
845a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated write call "
846a3f565b1SChuck Lever 		"(req %s/%Ld, %u bytes @ offset %Lu)\n",
8470bbacc40SChuck Lever 		data->task.tk_pid,
8481da177e4SLinus Torvalds 		inode->i_sb->s_id,
8491da177e4SLinus Torvalds 		(long long)NFS_FILEID(inode),
8501da177e4SLinus Torvalds 		count,
8511da177e4SLinus Torvalds 		(unsigned long long)data->args.offset);
8521da177e4SLinus Torvalds }
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds static void nfs_execute_write(struct nfs_write_data *data)
8551da177e4SLinus Torvalds {
8561da177e4SLinus Torvalds 	struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
8571da177e4SLinus Torvalds 	sigset_t oldset;
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	rpc_clnt_sigmask(clnt, &oldset);
8601da177e4SLinus Torvalds 	rpc_execute(&data->task);
8611da177e4SLinus Torvalds 	rpc_clnt_sigunmask(clnt, &oldset);
8621da177e4SLinus Torvalds }
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds /*
8651da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
8661da177e4SLinus Torvalds  * contiguous dirty area on one page.
8671da177e4SLinus Torvalds  */
8687d46a49fSTrond Myklebust static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
8691da177e4SLinus Torvalds {
8701da177e4SLinus Torvalds 	struct nfs_page *req = nfs_list_entry(head->next);
8711da177e4SLinus Torvalds 	struct page *page = req->wb_page;
8721da177e4SLinus Torvalds 	struct nfs_write_data *data;
873e9f7bee1STrond Myklebust 	size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
874e9f7bee1STrond Myklebust 	unsigned int offset;
8751da177e4SLinus Torvalds 	int requests = 0;
8761da177e4SLinus Torvalds 	LIST_HEAD(list);
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 	nfs_list_remove_request(req);
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	nbytes = req->wb_bytes;
881e9f7bee1STrond Myklebust 	do {
882e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
883e9f7bee1STrond Myklebust 
884e9f7bee1STrond Myklebust 		data = nfs_writedata_alloc(len);
8851da177e4SLinus Torvalds 		if (!data)
8861da177e4SLinus Torvalds 			goto out_bad;
8871da177e4SLinus Torvalds 		list_add(&data->pages, &list);
8881da177e4SLinus Torvalds 		requests++;
889e9f7bee1STrond Myklebust 		nbytes -= len;
890e9f7bee1STrond Myklebust 	} while (nbytes != 0);
8911da177e4SLinus Torvalds 	atomic_set(&req->wb_complete, requests);
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 	ClearPageError(page);
8941da177e4SLinus Torvalds 	offset = 0;
8951da177e4SLinus Torvalds 	nbytes = req->wb_bytes;
8961da177e4SLinus Torvalds 	do {
8971da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
8981da177e4SLinus Torvalds 		list_del_init(&data->pages);
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds 		data->pagevec[0] = page;
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 		if (nbytes > wsize) {
903788e7a89STrond Myklebust 			nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
904788e7a89STrond Myklebust 					wsize, offset, how);
9051da177e4SLinus Torvalds 			offset += wsize;
9061da177e4SLinus Torvalds 			nbytes -= wsize;
9071da177e4SLinus Torvalds 		} else {
908788e7a89STrond Myklebust 			nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
909788e7a89STrond Myklebust 					nbytes, offset, how);
9101da177e4SLinus Torvalds 			nbytes = 0;
9111da177e4SLinus Torvalds 		}
9121da177e4SLinus Torvalds 		nfs_execute_write(data);
9131da177e4SLinus Torvalds 	} while (nbytes != 0);
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	return 0;
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds out_bad:
9181da177e4SLinus Torvalds 	while (!list_empty(&list)) {
9191da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
9201da177e4SLinus Torvalds 		list_del(&data->pages);
9218aca67f0STrond Myklebust 		nfs_writedata_release(data);
9221da177e4SLinus Torvalds 	}
92361822ab5STrond Myklebust 	nfs_redirty_request(req);
9246d677e35STrond Myklebust 	nfs_end_page_writeback(req->wb_page);
925c6a556b8STrond Myklebust 	nfs_clear_page_writeback(req);
9261da177e4SLinus Torvalds 	return -ENOMEM;
9271da177e4SLinus Torvalds }
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds /*
9301da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
9311da177e4SLinus Torvalds  * The page must have been locked by the caller.
9321da177e4SLinus Torvalds  *
9331da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
9341da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
9351da177e4SLinus Torvalds  * that has been written but not committed.
9361da177e4SLinus Torvalds  */
9377d46a49fSTrond Myklebust static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
9381da177e4SLinus Torvalds {
9391da177e4SLinus Torvalds 	struct nfs_page		*req;
9401da177e4SLinus Torvalds 	struct page		**pages;
9411da177e4SLinus Torvalds 	struct nfs_write_data	*data;
9421da177e4SLinus Torvalds 	unsigned int		count;
9431da177e4SLinus Torvalds 
944e9f7bee1STrond Myklebust 	data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
9451da177e4SLinus Torvalds 	if (!data)
9461da177e4SLinus Torvalds 		goto out_bad;
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds 	pages = data->pagevec;
9491da177e4SLinus Torvalds 	count = 0;
9501da177e4SLinus Torvalds 	while (!list_empty(head)) {
9511da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
9521da177e4SLinus Torvalds 		nfs_list_remove_request(req);
9531da177e4SLinus Torvalds 		nfs_list_add_request(req, &data->pages);
9541da177e4SLinus Torvalds 		ClearPageError(req->wb_page);
9551da177e4SLinus Torvalds 		*pages++ = req->wb_page;
9561da177e4SLinus Torvalds 		count += req->wb_bytes;
9571da177e4SLinus Torvalds 	}
9581da177e4SLinus Torvalds 	req = nfs_list_entry(data->pages.next);
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	/* Set up the argument struct */
961788e7a89STrond Myklebust 	nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 	nfs_execute_write(data);
9641da177e4SLinus Torvalds 	return 0;
9651da177e4SLinus Torvalds  out_bad:
9661da177e4SLinus Torvalds 	while (!list_empty(head)) {
9671da177e4SLinus Torvalds 		struct nfs_page *req = nfs_list_entry(head->next);
9681da177e4SLinus Torvalds 		nfs_list_remove_request(req);
96961822ab5STrond Myklebust 		nfs_redirty_request(req);
9706d677e35STrond Myklebust 		nfs_end_page_writeback(req->wb_page);
971c6a556b8STrond Myklebust 		nfs_clear_page_writeback(req);
9721da177e4SLinus Torvalds 	}
9731da177e4SLinus Torvalds 	return -ENOMEM;
9741da177e4SLinus Torvalds }
9751da177e4SLinus Torvalds 
9767d46a49fSTrond Myklebust static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
9771da177e4SLinus Torvalds {
9781da177e4SLinus Torvalds 	LIST_HEAD(one_request);
9797d46a49fSTrond Myklebust 	int (*flush_one)(struct inode *, struct list_head *, int);
9801da177e4SLinus Torvalds 	struct nfs_page	*req;
9817d46a49fSTrond Myklebust 	int wpages = NFS_SERVER(inode)->wpages;
9827d46a49fSTrond Myklebust 	int wsize = NFS_SERVER(inode)->wsize;
9837d46a49fSTrond Myklebust 	int error;
9841da177e4SLinus Torvalds 
9857d46a49fSTrond Myklebust 	flush_one = nfs_flush_one;
9867d46a49fSTrond Myklebust 	if (wsize < PAGE_CACHE_SIZE)
9877d46a49fSTrond Myklebust 		flush_one = nfs_flush_multi;
9887d46a49fSTrond Myklebust 	/* For single writes, FLUSH_STABLE is more efficient */
9897d46a49fSTrond Myklebust 	if (npages <= wpages && npages == NFS_I(inode)->npages
9907d46a49fSTrond Myklebust 			&& nfs_list_entry(head->next)->wb_bytes <= wsize)
9917d46a49fSTrond Myklebust 		how |= FLUSH_STABLE;
9927d46a49fSTrond Myklebust 
9937d46a49fSTrond Myklebust 	do {
9947d46a49fSTrond Myklebust 		nfs_coalesce_requests(head, &one_request, wpages);
9951da177e4SLinus Torvalds 		req = nfs_list_entry(one_request.next);
9967d46a49fSTrond Myklebust 		error = flush_one(inode, &one_request, how);
9971da177e4SLinus Torvalds 		if (error < 0)
9987d46a49fSTrond Myklebust 			goto out_err;
9997d46a49fSTrond Myklebust 	} while (!list_empty(head));
10007d46a49fSTrond Myklebust 	return 0;
10017d46a49fSTrond Myklebust out_err:
10021da177e4SLinus Torvalds 	while (!list_empty(head)) {
10031da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
10041da177e4SLinus Torvalds 		nfs_list_remove_request(req);
100561822ab5STrond Myklebust 		nfs_redirty_request(req);
10066d677e35STrond Myklebust 		nfs_end_page_writeback(req->wb_page);
1007c6a556b8STrond Myklebust 		nfs_clear_page_writeback(req);
10081da177e4SLinus Torvalds 	}
10091da177e4SLinus Torvalds 	return error;
10101da177e4SLinus Torvalds }
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds /*
10131da177e4SLinus Torvalds  * Handle a write reply that flushed part of a page.
10141da177e4SLinus Torvalds  */
1015788e7a89STrond Myklebust static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
10161da177e4SLinus Torvalds {
1017788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
10181da177e4SLinus Torvalds 	struct nfs_page		*req = data->req;
10191da177e4SLinus Torvalds 	struct page		*page = req->wb_page;
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 	dprintk("NFS: write (%s/%Ld %d@%Ld)",
10221da177e4SLinus Torvalds 		req->wb_context->dentry->d_inode->i_sb->s_id,
10231da177e4SLinus Torvalds 		(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
10241da177e4SLinus Torvalds 		req->wb_bytes,
10251da177e4SLinus Torvalds 		(long long)req_offset(req));
10261da177e4SLinus Torvalds 
1027788e7a89STrond Myklebust 	if (nfs_writeback_done(task, data) != 0)
1028788e7a89STrond Myklebust 		return;
1029788e7a89STrond Myklebust 
1030788e7a89STrond Myklebust 	if (task->tk_status < 0) {
1031a301b777STrond Myklebust 		nfs_set_pageerror(page);
1032788e7a89STrond Myklebust 		req->wb_context->error = task->tk_status;
1033788e7a89STrond Myklebust 		dprintk(", error = %d\n", task->tk_status);
10348e821cadSTrond Myklebust 		goto out;
10358e821cadSTrond Myklebust 	}
10368e821cadSTrond Myklebust 
10378e821cadSTrond Myklebust 	if (nfs_write_need_commit(data)) {
10388e821cadSTrond Myklebust 		spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
10398e821cadSTrond Myklebust 
10408e821cadSTrond Myklebust 		spin_lock(req_lock);
10418e821cadSTrond Myklebust 		if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
10428e821cadSTrond Myklebust 			/* Do nothing we need to resend the writes */
10438e821cadSTrond Myklebust 		} else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
10441da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
10451da177e4SLinus Torvalds 			dprintk(" defer commit\n");
10461da177e4SLinus Torvalds 		} else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
10478e821cadSTrond Myklebust 			set_bit(PG_NEED_RESCHED, &req->wb_flags);
10488e821cadSTrond Myklebust 			clear_bit(PG_NEED_COMMIT, &req->wb_flags);
10491da177e4SLinus Torvalds 			dprintk(" server reboot detected\n");
10501da177e4SLinus Torvalds 		}
10518e821cadSTrond Myklebust 		spin_unlock(req_lock);
10521da177e4SLinus Torvalds 	} else
10531da177e4SLinus Torvalds 		dprintk(" OK\n");
10541da177e4SLinus Torvalds 
10558e821cadSTrond Myklebust out:
10561da177e4SLinus Torvalds 	if (atomic_dec_and_test(&req->wb_complete))
10571da177e4SLinus Torvalds 		nfs_writepage_release(req);
10581da177e4SLinus Torvalds }
10591da177e4SLinus Torvalds 
1060788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops = {
1061788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_partial,
1062788e7a89STrond Myklebust 	.rpc_release = nfs_writedata_release,
1063788e7a89STrond Myklebust };
1064788e7a89STrond Myklebust 
10651da177e4SLinus Torvalds /*
10661da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
10671da177e4SLinus Torvalds  *
10681da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
10691da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
10701da177e4SLinus Torvalds  *	  as the page has a write request pending.
10711da177e4SLinus Torvalds  */
1072788e7a89STrond Myklebust static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
10731da177e4SLinus Torvalds {
1074788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
10751da177e4SLinus Torvalds 	struct nfs_page		*req;
10761da177e4SLinus Torvalds 	struct page		*page;
10771da177e4SLinus Torvalds 
1078788e7a89STrond Myklebust 	if (nfs_writeback_done(task, data) != 0)
1079788e7a89STrond Myklebust 		return;
1080788e7a89STrond Myklebust 
10811da177e4SLinus Torvalds 	/* Update attributes as result of writeback. */
10821da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
10831da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
10841da177e4SLinus Torvalds 		nfs_list_remove_request(req);
10851da177e4SLinus Torvalds 		page = req->wb_page;
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 		dprintk("NFS: write (%s/%Ld %d@%Ld)",
10881da177e4SLinus Torvalds 			req->wb_context->dentry->d_inode->i_sb->s_id,
10891da177e4SLinus Torvalds 			(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
10901da177e4SLinus Torvalds 			req->wb_bytes,
10911da177e4SLinus Torvalds 			(long long)req_offset(req));
10921da177e4SLinus Torvalds 
1093788e7a89STrond Myklebust 		if (task->tk_status < 0) {
1094a301b777STrond Myklebust 			nfs_set_pageerror(page);
1095788e7a89STrond Myklebust 			req->wb_context->error = task->tk_status;
1096788e7a89STrond Myklebust 			dprintk(", error = %d\n", task->tk_status);
10978e821cadSTrond Myklebust 			goto remove_request;
10981da177e4SLinus Torvalds 		}
10991da177e4SLinus Torvalds 
11008e821cadSTrond Myklebust 		if (nfs_write_need_commit(data)) {
11011da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
11021da177e4SLinus Torvalds 			nfs_mark_request_commit(req);
11038e821cadSTrond Myklebust 			nfs_end_page_writeback(page);
11041da177e4SLinus Torvalds 			dprintk(" marked for commit\n");
11058e821cadSTrond Myklebust 			goto next;
11068e821cadSTrond Myklebust 		}
11078e821cadSTrond Myklebust 		dprintk(" OK\n");
11088e821cadSTrond Myklebust remove_request:
11098e821cadSTrond Myklebust 		nfs_end_page_writeback(page);
11101da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
11111da177e4SLinus Torvalds 	next:
1112c6a556b8STrond Myklebust 		nfs_clear_page_writeback(req);
11131da177e4SLinus Torvalds 	}
11141da177e4SLinus Torvalds }
11151da177e4SLinus Torvalds 
1116788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops = {
1117788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_full,
1118788e7a89STrond Myklebust 	.rpc_release = nfs_writedata_release,
1119788e7a89STrond Myklebust };
1120788e7a89STrond Myklebust 
1121788e7a89STrond Myklebust 
11221da177e4SLinus Torvalds /*
11231da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
11241da177e4SLinus Torvalds  */
1125462d5b32SChuck Lever int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
11261da177e4SLinus Torvalds {
11271da177e4SLinus Torvalds 	struct nfs_writeargs	*argp = &data->args;
11281da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
1129788e7a89STrond Myklebust 	int status;
11301da177e4SLinus Torvalds 
1131a3f565b1SChuck Lever 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
11321da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
11331da177e4SLinus Torvalds 
1134f551e44fSChuck Lever 	/*
1135f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1136f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1137f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1138f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1139f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1140f551e44fSChuck Lever 	 */
1141788e7a89STrond Myklebust 	status = NFS_PROTO(data->inode)->write_done(task, data);
1142788e7a89STrond Myklebust 	if (status != 0)
1143788e7a89STrond Myklebust 		return status;
114491d5b470SChuck Lever 	nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
114591d5b470SChuck Lever 
11461da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
11471da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
11481da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
11491da177e4SLinus Torvalds 		 * commit data to stable storage even though we
11501da177e4SLinus Torvalds 		 * requested it.
11511da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
11521da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
11531da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
11541da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
11551da177e4SLinus Torvalds 		 */
11561da177e4SLinus Torvalds 		static unsigned long    complain;
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
11591da177e4SLinus Torvalds 			dprintk("NFS: faulty NFS server %s:"
11601da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
116154ceac45SDavid Howells 				NFS_SERVER(data->inode)->nfs_client->cl_hostname,
11621da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
11631da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
11641da177e4SLinus Torvalds 		}
11651da177e4SLinus Torvalds 	}
11661da177e4SLinus Torvalds #endif
11671da177e4SLinus Torvalds 	/* Is this a short write? */
11681da177e4SLinus Torvalds 	if (task->tk_status >= 0 && resp->count < argp->count) {
11691da177e4SLinus Torvalds 		static unsigned long    complain;
11701da177e4SLinus Torvalds 
117191d5b470SChuck Lever 		nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
117291d5b470SChuck Lever 
11731da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
11741da177e4SLinus Torvalds 		if (resp->count != 0) {
11751da177e4SLinus Torvalds 			/* Was this an NFSv2 write or an NFSv3 stable write? */
11761da177e4SLinus Torvalds 			if (resp->verf->committed != NFS_UNSTABLE) {
11771da177e4SLinus Torvalds 				/* Resend from where the server left off */
11781da177e4SLinus Torvalds 				argp->offset += resp->count;
11791da177e4SLinus Torvalds 				argp->pgbase += resp->count;
11801da177e4SLinus Torvalds 				argp->count -= resp->count;
11811da177e4SLinus Torvalds 			} else {
11821da177e4SLinus Torvalds 				/* Resend as a stable write in order to avoid
11831da177e4SLinus Torvalds 				 * headaches in the case of a server crash.
11841da177e4SLinus Torvalds 				 */
11851da177e4SLinus Torvalds 				argp->stable = NFS_FILE_SYNC;
11861da177e4SLinus Torvalds 			}
11871da177e4SLinus Torvalds 			rpc_restart_call(task);
1188788e7a89STrond Myklebust 			return -EAGAIN;
11891da177e4SLinus Torvalds 		}
11901da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
11911da177e4SLinus Torvalds 			printk(KERN_WARNING
11921da177e4SLinus Torvalds 			       "NFS: Server wrote zero bytes, expected %u.\n",
11931da177e4SLinus Torvalds 					argp->count);
11941da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
11951da177e4SLinus Torvalds 		}
11961da177e4SLinus Torvalds 		/* Can't do anything about it except throw an error. */
11971da177e4SLinus Torvalds 		task->tk_status = -EIO;
11981da177e4SLinus Torvalds 	}
1199788e7a89STrond Myklebust 	return 0;
12001da177e4SLinus Torvalds }
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1204963d8fe5STrond Myklebust void nfs_commit_release(void *wdata)
12051da177e4SLinus Torvalds {
12061da177e4SLinus Torvalds 	nfs_commit_free(wdata);
12071da177e4SLinus Torvalds }
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds /*
12101da177e4SLinus Torvalds  * Set up the argument/result storage required for the RPC call.
12111da177e4SLinus Torvalds  */
12121da177e4SLinus Torvalds static void nfs_commit_rpcsetup(struct list_head *head,
1213788e7a89STrond Myklebust 		struct nfs_write_data *data,
1214788e7a89STrond Myklebust 		int how)
12151da177e4SLinus Torvalds {
12163da28eb1STrond Myklebust 	struct nfs_page		*first;
12171da177e4SLinus Torvalds 	struct inode		*inode;
1218788e7a89STrond Myklebust 	int flags;
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
12211da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
12221da177e4SLinus Torvalds 
12231da177e4SLinus Torvalds 	list_splice_init(head, &data->pages);
12241da177e4SLinus Torvalds 	first = nfs_list_entry(data->pages.next);
12251da177e4SLinus Torvalds 	inode = first->wb_context->dentry->d_inode;
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 	data->inode	  = inode;
12281da177e4SLinus Torvalds 	data->cred	  = first->wb_context->cred;
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(data->inode);
12313da28eb1STrond Myklebust 	/* Note: we always request a commit of the entire inode */
12323da28eb1STrond Myklebust 	data->args.offset = 0;
12333da28eb1STrond Myklebust 	data->args.count  = 0;
12343da28eb1STrond Myklebust 	data->res.count   = 0;
12351da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
12361da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
12370e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
12381da177e4SLinus Torvalds 
1239788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
1240788e7a89STrond Myklebust 	flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1241788e7a89STrond Myklebust 	rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
12421da177e4SLinus Torvalds 	NFS_PROTO(inode)->commit_setup(data, how);
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds 	data->task.tk_priority = flush_task_priority(how);
12451da177e4SLinus Torvalds 	data->task.tk_cookie = (unsigned long)inode;
12461da177e4SLinus Torvalds 
1247a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
12481da177e4SLinus Torvalds }
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds /*
12511da177e4SLinus Torvalds  * Commit dirty pages
12521da177e4SLinus Torvalds  */
12531da177e4SLinus Torvalds static int
125440859d7eSChuck Lever nfs_commit_list(struct inode *inode, struct list_head *head, int how)
12551da177e4SLinus Torvalds {
12561da177e4SLinus Torvalds 	struct nfs_write_data	*data;
12571da177e4SLinus Torvalds 	struct nfs_page         *req;
12581da177e4SLinus Torvalds 
1259e9f7bee1STrond Myklebust 	data = nfs_commit_alloc();
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds 	if (!data)
12621da177e4SLinus Torvalds 		goto out_bad;
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds 	/* Set up the argument struct */
12651da177e4SLinus Torvalds 	nfs_commit_rpcsetup(head, data, how);
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 	nfs_execute_write(data);
12681da177e4SLinus Torvalds 	return 0;
12691da177e4SLinus Torvalds  out_bad:
12701da177e4SLinus Torvalds 	while (!list_empty(head)) {
12711da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
12721da177e4SLinus Torvalds 		nfs_list_remove_request(req);
12731da177e4SLinus Torvalds 		nfs_mark_request_commit(req);
127483715ad5STrond Myklebust 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
12755c2d97cbSTrond Myklebust 		nfs_clear_page_writeback(req);
12761da177e4SLinus Torvalds 	}
12771da177e4SLinus Torvalds 	return -ENOMEM;
12781da177e4SLinus Torvalds }
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds /*
12811da177e4SLinus Torvalds  * COMMIT call returned
12821da177e4SLinus Torvalds  */
1283788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
12841da177e4SLinus Torvalds {
1285963d8fe5STrond Myklebust 	struct nfs_write_data	*data = calldata;
12861da177e4SLinus Torvalds 	struct nfs_page		*req;
12871da177e4SLinus Torvalds 
1288a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
12891da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
12901da177e4SLinus Torvalds 
1291788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1292788e7a89STrond Myklebust 	if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1293788e7a89STrond Myklebust 		return;
1294788e7a89STrond Myklebust 
12951da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
12961da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
12971da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1298612c9384STrond Myklebust 		clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
1299fd39fc85SChristoph Lameter 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 		dprintk("NFS: commit (%s/%Ld %d@%Ld)",
13021da177e4SLinus Torvalds 			req->wb_context->dentry->d_inode->i_sb->s_id,
13031da177e4SLinus Torvalds 			(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
13041da177e4SLinus Torvalds 			req->wb_bytes,
13051da177e4SLinus Torvalds 			(long long)req_offset(req));
13061da177e4SLinus Torvalds 		if (task->tk_status < 0) {
13071da177e4SLinus Torvalds 			req->wb_context->error = task->tk_status;
13081da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
13091da177e4SLinus Torvalds 			dprintk(", error = %d\n", task->tk_status);
13101da177e4SLinus Torvalds 			goto next;
13111da177e4SLinus Torvalds 		}
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
13141da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
13151da177e4SLinus Torvalds 		if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
13161da177e4SLinus Torvalds 			/* We have a match */
13171da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
13181da177e4SLinus Torvalds 			dprintk(" OK\n");
13191da177e4SLinus Torvalds 			goto next;
13201da177e4SLinus Torvalds 		}
13211da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
13221da177e4SLinus Torvalds 		dprintk(" mismatch\n");
132361822ab5STrond Myklebust 		nfs_redirty_request(req);
13241da177e4SLinus Torvalds 	next:
1325c6a556b8STrond Myklebust 		nfs_clear_page_writeback(req);
13261da177e4SLinus Torvalds 	}
13271da177e4SLinus Torvalds }
1328788e7a89STrond Myklebust 
1329788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
1330788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1331788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1332788e7a89STrond Myklebust };
1333c42de9ddSTrond Myklebust #else
1334c42de9ddSTrond Myklebust static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1335c42de9ddSTrond Myklebust {
1336c42de9ddSTrond Myklebust 	return 0;
1337c42de9ddSTrond Myklebust }
13381da177e4SLinus Torvalds #endif
13391da177e4SLinus Torvalds 
13403f442547STrond Myklebust static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
13411da177e4SLinus Torvalds {
134228c6925fSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(mapping->host);
13431da177e4SLinus Torvalds 	LIST_HEAD(head);
13443f442547STrond Myklebust 	long res;
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 	spin_lock(&nfsi->req_lock);
13473f442547STrond Myklebust 	res = nfs_scan_dirty(mapping, wbc, &head);
13481da177e4SLinus Torvalds 	spin_unlock(&nfsi->req_lock);
1349ab0a3dbeSTrond Myklebust 	if (res) {
135028c6925fSTrond Myklebust 		int error = nfs_flush_list(mapping->host, &head, res, how);
13511da177e4SLinus Torvalds 		if (error < 0)
13521da177e4SLinus Torvalds 			return error;
13537d46a49fSTrond Myklebust 	}
13541da177e4SLinus Torvalds 	return res;
13551da177e4SLinus Torvalds }
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
13583da28eb1STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
13591da177e4SLinus Torvalds {
13601da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
13611da177e4SLinus Torvalds 	LIST_HEAD(head);
13627d46a49fSTrond Myklebust 	int res;
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 	spin_lock(&nfsi->req_lock);
13653da28eb1STrond Myklebust 	res = nfs_scan_commit(inode, &head, 0, 0);
13663da28eb1STrond Myklebust 	spin_unlock(&nfsi->req_lock);
13671da177e4SLinus Torvalds 	if (res) {
13687d46a49fSTrond Myklebust 		int error = nfs_commit_list(inode, &head, how);
13691da177e4SLinus Torvalds 		if (error < 0)
13701da177e4SLinus Torvalds 			return error;
13713da28eb1STrond Myklebust 	}
13721da177e4SLinus Torvalds 	return res;
13731da177e4SLinus Torvalds }
13741da177e4SLinus Torvalds #endif
13751da177e4SLinus Torvalds 
13761c75950bSTrond Myklebust long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
13771da177e4SLinus Torvalds {
13781c75950bSTrond Myklebust 	struct inode *inode = mapping->host;
1379c42de9ddSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
13801c75950bSTrond Myklebust 	unsigned long idx_start, idx_end;
13811c75950bSTrond Myklebust 	unsigned int npages = 0;
1382c42de9ddSTrond Myklebust 	LIST_HEAD(head);
138370b9ecbdSTrond Myklebust 	int nocommit = how & FLUSH_NOCOMMIT;
13843f442547STrond Myklebust 	long pages, ret;
13851da177e4SLinus Torvalds 
13861c75950bSTrond Myklebust 	/* FIXME */
13871c75950bSTrond Myklebust 	if (wbc->range_cyclic)
13881c75950bSTrond Myklebust 		idx_start = 0;
13891c75950bSTrond Myklebust 	else {
13901c75950bSTrond Myklebust 		idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
13911c75950bSTrond Myklebust 		idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
13921c75950bSTrond Myklebust 		if (idx_end > idx_start) {
13931c75950bSTrond Myklebust 			unsigned long l_npages = 1 + idx_end - idx_start;
13941c75950bSTrond Myklebust 			npages = l_npages;
13951c75950bSTrond Myklebust 			if (sizeof(npages) != sizeof(l_npages) &&
13961c75950bSTrond Myklebust 					(unsigned long)npages != l_npages)
13971c75950bSTrond Myklebust 				npages = 0;
13981c75950bSTrond Myklebust 		}
13991c75950bSTrond Myklebust 	}
1400c42de9ddSTrond Myklebust 	how &= ~FLUSH_NOCOMMIT;
1401c42de9ddSTrond Myklebust 	spin_lock(&nfsi->req_lock);
14021da177e4SLinus Torvalds 	do {
14031c75950bSTrond Myklebust 		wbc->pages_skipped = 0;
1404c42de9ddSTrond Myklebust 		ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1405c42de9ddSTrond Myklebust 		if (ret != 0)
1406c42de9ddSTrond Myklebust 			continue;
14071c75950bSTrond Myklebust 		pages = nfs_scan_dirty(mapping, wbc, &head);
1408c42de9ddSTrond Myklebust 		if (pages != 0) {
1409c42de9ddSTrond Myklebust 			spin_unlock(&nfsi->req_lock);
1410e8e058e8STrond Myklebust 			if (how & FLUSH_INVALIDATE) {
141183715ad5STrond Myklebust 				nfs_cancel_dirty_list(&head);
1412e8e058e8STrond Myklebust 				ret = pages;
1413e8e058e8STrond Myklebust 			} else
1414c42de9ddSTrond Myklebust 				ret = nfs_flush_list(inode, &head, pages, how);
1415c42de9ddSTrond Myklebust 			spin_lock(&nfsi->req_lock);
141670b9ecbdSTrond Myklebust 			continue;
141770b9ecbdSTrond Myklebust 		}
14181c75950bSTrond Myklebust 		if (wbc->pages_skipped != 0)
14191c75950bSTrond Myklebust 			continue;
1420c42de9ddSTrond Myklebust 		if (nocommit)
1421c42de9ddSTrond Myklebust 			break;
1422d2ccddf0STrond Myklebust 		pages = nfs_scan_commit(inode, &head, idx_start, npages);
14231c75950bSTrond Myklebust 		if (pages == 0) {
14241c75950bSTrond Myklebust 			if (wbc->pages_skipped != 0)
14251c75950bSTrond Myklebust 				continue;
1426c42de9ddSTrond Myklebust 			break;
14271c75950bSTrond Myklebust 		}
1428d2ccddf0STrond Myklebust 		if (how & FLUSH_INVALIDATE) {
1429d2ccddf0STrond Myklebust 			spin_unlock(&nfsi->req_lock);
143083715ad5STrond Myklebust 			nfs_cancel_commit_list(&head);
1431e8e058e8STrond Myklebust 			ret = pages;
1432d2ccddf0STrond Myklebust 			spin_lock(&nfsi->req_lock);
1433d2ccddf0STrond Myklebust 			continue;
1434d2ccddf0STrond Myklebust 		}
1435d2ccddf0STrond Myklebust 		pages += nfs_scan_commit(inode, &head, 0, 0);
1436c42de9ddSTrond Myklebust 		spin_unlock(&nfsi->req_lock);
1437c42de9ddSTrond Myklebust 		ret = nfs_commit_list(inode, &head, how);
1438c42de9ddSTrond Myklebust 		spin_lock(&nfsi->req_lock);
1439c42de9ddSTrond Myklebust 	} while (ret >= 0);
1440c42de9ddSTrond Myklebust 	spin_unlock(&nfsi->req_lock);
1441c42de9ddSTrond Myklebust 	return ret;
14421da177e4SLinus Torvalds }
14431da177e4SLinus Torvalds 
14441c75950bSTrond Myklebust /*
14451c75950bSTrond Myklebust  * flush the inode to disk.
14461c75950bSTrond Myklebust  */
14471c75950bSTrond Myklebust int nfs_wb_all(struct inode *inode)
14481c75950bSTrond Myklebust {
14491c75950bSTrond Myklebust 	struct address_space *mapping = inode->i_mapping;
14501c75950bSTrond Myklebust 	struct writeback_control wbc = {
14511c75950bSTrond Myklebust 		.bdi = mapping->backing_dev_info,
14521c75950bSTrond Myklebust 		.sync_mode = WB_SYNC_ALL,
14531c75950bSTrond Myklebust 		.nr_to_write = LONG_MAX,
145461822ab5STrond Myklebust 		.for_writepages = 1,
14551c75950bSTrond Myklebust 		.range_cyclic = 1,
14561c75950bSTrond Myklebust 	};
14571c75950bSTrond Myklebust 	int ret;
14581c75950bSTrond Myklebust 
145961822ab5STrond Myklebust 	ret = generic_writepages(mapping, &wbc);
146061822ab5STrond Myklebust 	if (ret < 0)
146161822ab5STrond Myklebust 		goto out;
14621c75950bSTrond Myklebust 	ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
14631c75950bSTrond Myklebust 	if (ret >= 0)
14641c75950bSTrond Myklebust 		return 0;
146561822ab5STrond Myklebust out:
1466e507d9ebSTrond Myklebust 	__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
14671c75950bSTrond Myklebust 	return ret;
14681c75950bSTrond Myklebust }
14691c75950bSTrond Myklebust 
14701c75950bSTrond Myklebust int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
14711c75950bSTrond Myklebust {
14721c75950bSTrond Myklebust 	struct writeback_control wbc = {
14731c75950bSTrond Myklebust 		.bdi = mapping->backing_dev_info,
14741c75950bSTrond Myklebust 		.sync_mode = WB_SYNC_ALL,
14751c75950bSTrond Myklebust 		.nr_to_write = LONG_MAX,
14761c75950bSTrond Myklebust 		.range_start = range_start,
14771c75950bSTrond Myklebust 		.range_end = range_end,
147861822ab5STrond Myklebust 		.for_writepages = 1,
14791c75950bSTrond Myklebust 	};
14801c75950bSTrond Myklebust 	int ret;
14811c75950bSTrond Myklebust 
148261822ab5STrond Myklebust 	if (!(how & FLUSH_NOWRITEPAGE)) {
148361822ab5STrond Myklebust 		ret = generic_writepages(mapping, &wbc);
148461822ab5STrond Myklebust 		if (ret < 0)
148561822ab5STrond Myklebust 			goto out;
148661822ab5STrond Myklebust 	}
14871c75950bSTrond Myklebust 	ret = nfs_sync_mapping_wait(mapping, &wbc, how);
14881c75950bSTrond Myklebust 	if (ret >= 0)
14891c75950bSTrond Myklebust 		return 0;
149061822ab5STrond Myklebust out:
1491e507d9ebSTrond Myklebust 	__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
14921c75950bSTrond Myklebust 	return ret;
14931c75950bSTrond Myklebust }
14941c75950bSTrond Myklebust 
149561822ab5STrond Myklebust int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
14961c75950bSTrond Myklebust {
14971c75950bSTrond Myklebust 	loff_t range_start = page_offset(page);
14981c75950bSTrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
14994d770ccfSTrond Myklebust 	struct writeback_control wbc = {
15004d770ccfSTrond Myklebust 		.bdi = page->mapping->backing_dev_info,
15014d770ccfSTrond Myklebust 		.sync_mode = WB_SYNC_ALL,
15024d770ccfSTrond Myklebust 		.nr_to_write = LONG_MAX,
15034d770ccfSTrond Myklebust 		.range_start = range_start,
15044d770ccfSTrond Myklebust 		.range_end = range_end,
15054d770ccfSTrond Myklebust 	};
15064d770ccfSTrond Myklebust 	int ret;
15071c75950bSTrond Myklebust 
15084d770ccfSTrond Myklebust 	BUG_ON(!PageLocked(page));
15094d770ccfSTrond Myklebust 	if (!(how & FLUSH_NOWRITEPAGE) && clear_page_dirty_for_io(page)) {
15104d770ccfSTrond Myklebust 		ret = nfs_writepage_locked(page, &wbc);
15114d770ccfSTrond Myklebust 		if (ret < 0)
15124d770ccfSTrond Myklebust 			goto out;
15134d770ccfSTrond Myklebust 	}
1514f40313acSTrond Myklebust 	if (!PagePrivate(page))
1515f40313acSTrond Myklebust 		return 0;
15164d770ccfSTrond Myklebust 	ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
15174d770ccfSTrond Myklebust 	if (ret >= 0)
15184d770ccfSTrond Myklebust 		return 0;
15194d770ccfSTrond Myklebust out:
1520e507d9ebSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_PAGES);
15214d770ccfSTrond Myklebust 	return ret;
15221c75950bSTrond Myklebust }
15231c75950bSTrond Myklebust 
15241c75950bSTrond Myklebust /*
15251c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
15261c75950bSTrond Myklebust  */
15271c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page* page)
15281c75950bSTrond Myklebust {
15294d770ccfSTrond Myklebust 	return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
15301c75950bSTrond Myklebust }
15311c75950bSTrond Myklebust 
15321a54533eSTrond Myklebust int nfs_set_page_dirty(struct page *page)
15331a54533eSTrond Myklebust {
15342b82f190STrond Myklebust 	spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
15351a54533eSTrond Myklebust 	struct nfs_page *req;
15362b82f190STrond Myklebust 	int ret;
15371a54533eSTrond Myklebust 
15382b82f190STrond Myklebust 	spin_lock(req_lock);
15392b82f190STrond Myklebust 	req = nfs_page_find_request_locked(page);
15401a54533eSTrond Myklebust 	if (req != NULL) {
15411a54533eSTrond Myklebust 		/* Mark any existing write requests for flushing */
15422b82f190STrond Myklebust 		ret = !test_and_set_bit(PG_NEED_FLUSH, &req->wb_flags);
15432b82f190STrond Myklebust 		spin_unlock(req_lock);
15441a54533eSTrond Myklebust 		nfs_release_request(req);
15452b82f190STrond Myklebust 		return ret;
15461a54533eSTrond Myklebust 	}
15472b82f190STrond Myklebust 	ret = __set_page_dirty_nobuffers(page);
15482b82f190STrond Myklebust 	spin_unlock(req_lock);
15492b82f190STrond Myklebust 	return ret;
15501a54533eSTrond Myklebust }
15511a54533eSTrond Myklebust 
15521c75950bSTrond Myklebust 
1553f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
15541da177e4SLinus Torvalds {
15551da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
15561da177e4SLinus Torvalds 					     sizeof(struct nfs_write_data),
15571da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
15581da177e4SLinus Torvalds 					     NULL, NULL);
15591da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
15601da177e4SLinus Torvalds 		return -ENOMEM;
15611da177e4SLinus Torvalds 
156293d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
15631da177e4SLinus Torvalds 						     nfs_wdata_cachep);
15641da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
15651da177e4SLinus Torvalds 		return -ENOMEM;
15661da177e4SLinus Torvalds 
156793d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
15681da177e4SLinus Torvalds 						      nfs_wdata_cachep);
15691da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
15701da177e4SLinus Torvalds 		return -ENOMEM;
15711da177e4SLinus Torvalds 
157289a09141SPeter Zijlstra 	/*
157389a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
157489a09141SPeter Zijlstra 	 *
157589a09141SPeter Zijlstra 	 *  64MB:    8192k
157689a09141SPeter Zijlstra 	 * 128MB:   11585k
157789a09141SPeter Zijlstra 	 * 256MB:   16384k
157889a09141SPeter Zijlstra 	 * 512MB:   23170k
157989a09141SPeter Zijlstra 	 *   1GB:   32768k
158089a09141SPeter Zijlstra 	 *   2GB:   46340k
158189a09141SPeter Zijlstra 	 *   4GB:   65536k
158289a09141SPeter Zijlstra 	 *   8GB:   92681k
158389a09141SPeter Zijlstra 	 *  16GB:  131072k
158489a09141SPeter Zijlstra 	 *
158589a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
158689a09141SPeter Zijlstra 	 * Limit the default to 256M
158789a09141SPeter Zijlstra 	 */
158889a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
158989a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
159089a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
159189a09141SPeter Zijlstra 
15921da177e4SLinus Torvalds 	return 0;
15931da177e4SLinus Torvalds }
15941da177e4SLinus Torvalds 
1595266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
15961da177e4SLinus Torvalds {
15971da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
15981da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
15991a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
16001da177e4SLinus Torvalds }
16011da177e4SLinus Torvalds 
1602