xref: /linux/fs/nfs/write.c (revision c9e51e4180696aa67915ec5665e4ec74125565de)
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 
251da177e4SLinus Torvalds #include "delegation.h"
2649a70f27STrond Myklebust #include "internal.h"
2791d5b470SChuck Lever #include "iostat.h"
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #define MIN_POOL_WRITE		(32)
321da177e4SLinus Torvalds #define MIN_POOL_COMMIT		(4)
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds /*
351da177e4SLinus Torvalds  * Local function declarations
361da177e4SLinus Torvalds  */
371da177e4SLinus Torvalds static struct nfs_page * nfs_update_request(struct nfs_open_context*,
381da177e4SLinus Torvalds 					    struct page *,
391da177e4SLinus Torvalds 					    unsigned int, unsigned int);
40c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc,
41c63c7b05STrond Myklebust 				  struct inode *inode, int ioflags);
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 
6110afec90STrond Myklebust static 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 
748d5658c9STrond Myklebust struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
753feb2d49STrond Myklebust {
76e6b4f8daSChristoph Lameter 	struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
773feb2d49STrond Myklebust 
783feb2d49STrond Myklebust 	if (p) {
793feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
803feb2d49STrond Myklebust 		INIT_LIST_HEAD(&p->pages);
81e9f7bee1STrond Myklebust 		p->npages = pagecount;
820d0b5cb3SChuck Lever 		if (pagecount <= ARRAY_SIZE(p->page_array))
830d0b5cb3SChuck Lever 			p->pagevec = p->page_array;
843feb2d49STrond Myklebust 		else {
850d0b5cb3SChuck Lever 			p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
860d0b5cb3SChuck Lever 			if (!p->pagevec) {
873feb2d49STrond Myklebust 				mempool_free(p, nfs_wdata_mempool);
883feb2d49STrond Myklebust 				p = NULL;
893feb2d49STrond Myklebust 			}
903feb2d49STrond Myklebust 		}
913feb2d49STrond Myklebust 	}
923feb2d49STrond Myklebust 	return p;
933feb2d49STrond Myklebust }
943feb2d49STrond Myklebust 
958aca67f0STrond Myklebust static void nfs_writedata_rcu_free(struct rcu_head *head)
963feb2d49STrond Myklebust {
978aca67f0STrond Myklebust 	struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
983feb2d49STrond Myklebust 	if (p && (p->pagevec != &p->page_array[0]))
993feb2d49STrond Myklebust 		kfree(p->pagevec);
1003feb2d49STrond Myklebust 	mempool_free(p, nfs_wdata_mempool);
1013feb2d49STrond Myklebust }
1023feb2d49STrond Myklebust 
1038aca67f0STrond Myklebust static void nfs_writedata_free(struct nfs_write_data *wdata)
1048aca67f0STrond Myklebust {
1058aca67f0STrond Myklebust 	call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
1068aca67f0STrond Myklebust }
1078aca67f0STrond Myklebust 
108963d8fe5STrond Myklebust void nfs_writedata_release(void *wdata)
1091da177e4SLinus Torvalds {
1101da177e4SLinus Torvalds 	nfs_writedata_free(wdata);
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
1137b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
1147b159fc1STrond Myklebust {
1157b159fc1STrond Myklebust 	ctx->error = error;
1167b159fc1STrond Myklebust 	smp_wmb();
1177b159fc1STrond Myklebust 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
1187b159fc1STrond Myklebust }
1197b159fc1STrond Myklebust 
120277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request_locked(struct page *page)
121277459d2STrond Myklebust {
122277459d2STrond Myklebust 	struct nfs_page *req = NULL;
123277459d2STrond Myklebust 
124277459d2STrond Myklebust 	if (PagePrivate(page)) {
125277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
126277459d2STrond Myklebust 		if (req != NULL)
127c03b4024STrond Myklebust 			kref_get(&req->wb_kref);
128277459d2STrond Myklebust 	}
129277459d2STrond Myklebust 	return req;
130277459d2STrond Myklebust }
131277459d2STrond Myklebust 
132277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page)
133277459d2STrond Myklebust {
134587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
135277459d2STrond Myklebust 	struct nfs_page *req = NULL;
136277459d2STrond Myklebust 
137587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
138277459d2STrond Myklebust 	req = nfs_page_find_request_locked(page);
139587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
140277459d2STrond Myklebust 	return req;
141277459d2STrond Myklebust }
142277459d2STrond Myklebust 
1431da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1441da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1451da177e4SLinus Torvalds {
1461da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
1471da177e4SLinus Torvalds 	loff_t end, i_size = i_size_read(inode);
148ca52fec1STrond Myklebust 	pgoff_t end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	if (i_size > 0 && page->index < end_index)
1511da177e4SLinus Torvalds 		return;
1521da177e4SLinus Torvalds 	end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
1531da177e4SLinus Torvalds 	if (i_size >= end)
1541da177e4SLinus Torvalds 		return;
15591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
1561da177e4SLinus Torvalds 	i_size_write(inode, end);
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
159a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
160a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
161a301b777STrond Myklebust {
162a301b777STrond Myklebust 	SetPageError(page);
163a301b777STrond Myklebust 	nfs_zap_mapping(page->mapping->host, page->mapping);
164a301b777STrond Myklebust }
165a301b777STrond Myklebust 
1661da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
1671da177e4SLinus Torvalds  * covers the full page.
1681da177e4SLinus Torvalds  */
1691da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
1701da177e4SLinus Torvalds {
1711da177e4SLinus Torvalds 	if (PageUptodate(page))
1721da177e4SLinus Torvalds 		return;
1731da177e4SLinus Torvalds 	if (base != 0)
1741da177e4SLinus Torvalds 		return;
17549a70f27STrond Myklebust 	if (count != nfs_page_length(page))
1761da177e4SLinus Torvalds 		return;
17749a70f27STrond Myklebust 	if (count != PAGE_CACHE_SIZE)
17860945cb7SNate Diller 		zero_user_page(page, count, PAGE_CACHE_SIZE - count, KM_USER0);
1791da177e4SLinus Torvalds 	SetPageUptodate(page);
1801da177e4SLinus Torvalds }
1811da177e4SLinus Torvalds 
182e21195a7STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
1831da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
1841da177e4SLinus Torvalds {
1851da177e4SLinus Torvalds 	struct nfs_page	*req;
186e21195a7STrond Myklebust 	int ret;
1871da177e4SLinus Torvalds 
188e21195a7STrond Myklebust 	for (;;) {
189e21195a7STrond Myklebust 		req = nfs_update_request(ctx, page, offset, count);
190e21195a7STrond Myklebust 		if (!IS_ERR(req))
191e21195a7STrond Myklebust 			break;
192e21195a7STrond Myklebust 		ret = PTR_ERR(req);
193e21195a7STrond Myklebust 		if (ret != -EBUSY)
194e21195a7STrond Myklebust 			return ret;
195e21195a7STrond Myklebust 		ret = nfs_wb_page(page->mapping->host, page);
196e21195a7STrond Myklebust 		if (ret != 0)
197e21195a7STrond Myklebust 			return ret;
198e21195a7STrond Myklebust 	}
1991da177e4SLinus Torvalds 	/* Update file length */
2001da177e4SLinus Torvalds 	nfs_grow_file(page, offset, count);
2011da177e4SLinus Torvalds 	nfs_unlock_request(req);
202abd3e641STrond Myklebust 	return 0;
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
2061da177e4SLinus Torvalds {
2071da177e4SLinus Torvalds 	if (wbc->for_reclaim)
208c63c7b05STrond Myklebust 		return FLUSH_HIGHPRI | FLUSH_STABLE;
2091da177e4SLinus Torvalds 	if (wbc->for_kupdate)
2101da177e4SLinus Torvalds 		return FLUSH_LOWPRI;
2111da177e4SLinus Torvalds 	return 0;
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds /*
21589a09141SPeter Zijlstra  * NFS congestion control
21689a09141SPeter Zijlstra  */
21789a09141SPeter Zijlstra 
21889a09141SPeter Zijlstra int nfs_congestion_kb;
21989a09141SPeter Zijlstra 
22089a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
22189a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
22289a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
22389a09141SPeter Zijlstra 
2245a6d41b3STrond Myklebust static int nfs_set_page_writeback(struct page *page)
22589a09141SPeter Zijlstra {
2265a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
2275a6d41b3STrond Myklebust 
2285a6d41b3STrond Myklebust 	if (!ret) {
22989a09141SPeter Zijlstra 		struct inode *inode = page->mapping->host;
23089a09141SPeter Zijlstra 		struct nfs_server *nfss = NFS_SERVER(inode);
23189a09141SPeter Zijlstra 
232277866a0SPeter Zijlstra 		if (atomic_long_inc_return(&nfss->writeback) >
23389a09141SPeter Zijlstra 				NFS_CONGESTION_ON_THRESH)
23489a09141SPeter Zijlstra 			set_bdi_congested(&nfss->backing_dev_info, WRITE);
23589a09141SPeter Zijlstra 	}
2365a6d41b3STrond Myklebust 	return ret;
23789a09141SPeter Zijlstra }
23889a09141SPeter Zijlstra 
23989a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page)
24089a09141SPeter Zijlstra {
24189a09141SPeter Zijlstra 	struct inode *inode = page->mapping->host;
24289a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
24389a09141SPeter Zijlstra 
24489a09141SPeter Zijlstra 	end_page_writeback(page);
245c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
24689a09141SPeter Zijlstra 		clear_bdi_congested(&nfss->backing_dev_info, WRITE);
24789a09141SPeter Zijlstra }
24889a09141SPeter Zijlstra 
24989a09141SPeter Zijlstra /*
250e261f51fSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
2519cccef95STrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
252e261f51fSTrond Myklebust  */
253c63c7b05STrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
254c63c7b05STrond Myklebust 				struct page *page)
255e261f51fSTrond Myklebust {
256587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
257587142f8STrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
258e261f51fSTrond Myklebust 	struct nfs_page *req;
259e261f51fSTrond Myklebust 	int ret;
260e261f51fSTrond Myklebust 
261587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
262e261f51fSTrond Myklebust 	for(;;) {
263e261f51fSTrond Myklebust 		req = nfs_page_find_request_locked(page);
264e261f51fSTrond Myklebust 		if (req == NULL) {
265587142f8STrond Myklebust 			spin_unlock(&inode->i_lock);
2669cccef95STrond Myklebust 			return 0;
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 		 */
275587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
276e261f51fSTrond Myklebust 		ret = nfs_wait_on_request(req);
277e261f51fSTrond Myklebust 		nfs_release_request(req);
278e261f51fSTrond Myklebust 		if (ret != 0)
279e261f51fSTrond Myklebust 			return ret;
280587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
281e261f51fSTrond Myklebust 	}
282612c9384STrond Myklebust 	if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
283612c9384STrond Myklebust 		/* This request is marked for commit */
284587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
285612c9384STrond Myklebust 		nfs_unlock_request(req);
286c63c7b05STrond Myklebust 		nfs_pageio_complete(pgio);
2879cccef95STrond Myklebust 		return 0;
288612c9384STrond Myklebust 	}
289c63c7b05STrond Myklebust 	if (nfs_set_page_writeback(page) != 0) {
290587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
291c63c7b05STrond Myklebust 		BUG();
292c63c7b05STrond Myklebust 	}
293c63c7b05STrond Myklebust 	radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
2949fd367f0STrond Myklebust 			NFS_PAGE_TAG_LOCKED);
295587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
296c63c7b05STrond Myklebust 	nfs_pageio_add_request(pgio, req);
2979cccef95STrond Myklebust 	return 0;
298e261f51fSTrond Myklebust }
299e261f51fSTrond Myklebust 
300f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
301f758c885STrond Myklebust {
302f758c885STrond Myklebust 	struct inode *inode = page->mapping->host;
303f758c885STrond Myklebust 
304f758c885STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
305f758c885STrond Myklebust 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
306f758c885STrond Myklebust 
307f758c885STrond Myklebust 	nfs_pageio_cond_complete(pgio, page->index);
308f758c885STrond Myklebust 	return nfs_page_async_flush(pgio, page);
309f758c885STrond Myklebust }
310f758c885STrond Myklebust 
311e261f51fSTrond Myklebust /*
3121da177e4SLinus Torvalds  * Write an mmapped page to the server.
3131da177e4SLinus Torvalds  */
3144d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
3151da177e4SLinus Torvalds {
316f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
317e261f51fSTrond Myklebust 	int err;
3181da177e4SLinus Torvalds 
319f758c885STrond Myklebust 	nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
320f758c885STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
321f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
322f758c885STrond Myklebust 	if (err < 0)
3234d770ccfSTrond Myklebust 		return err;
324f758c885STrond Myklebust 	if (pgio.pg_error < 0)
325f758c885STrond Myklebust 		return pgio.pg_error;
326f758c885STrond Myklebust 	return 0;
3274d770ccfSTrond Myklebust }
3284d770ccfSTrond Myklebust 
3294d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
3304d770ccfSTrond Myklebust {
331f758c885STrond Myklebust 	int ret;
3324d770ccfSTrond Myklebust 
333f758c885STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
3341da177e4SLinus Torvalds 	unlock_page(page);
335f758c885STrond Myklebust 	return ret;
336f758c885STrond Myklebust }
337f758c885STrond Myklebust 
338f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
339f758c885STrond Myklebust {
340f758c885STrond Myklebust 	int ret;
341f758c885STrond Myklebust 
342f758c885STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
343f758c885STrond Myklebust 	unlock_page(page);
344f758c885STrond Myklebust 	return ret;
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3481da177e4SLinus Torvalds {
3491da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
350c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
3511da177e4SLinus Torvalds 	int err;
3521da177e4SLinus Torvalds 
35391d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
35491d5b470SChuck Lever 
355c63c7b05STrond Myklebust 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
356f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
357c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
358f758c885STrond Myklebust 	if (err < 0)
3591da177e4SLinus Torvalds 		return err;
360f758c885STrond Myklebust 	if (pgio.pg_error < 0)
361c63c7b05STrond Myklebust 		return pgio.pg_error;
362c63c7b05STrond Myklebust 	return 0;
3631da177e4SLinus Torvalds }
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds /*
3661da177e4SLinus Torvalds  * Insert a write request into an inode
3671da177e4SLinus Torvalds  */
3681da177e4SLinus Torvalds static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
3691da177e4SLinus Torvalds {
3701da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
3711da177e4SLinus Torvalds 	int error;
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
3741da177e4SLinus Torvalds 	BUG_ON(error == -EEXIST);
3751da177e4SLinus Torvalds 	if (error)
3761da177e4SLinus Torvalds 		return error;
3771da177e4SLinus Torvalds 	if (!nfsi->npages) {
3781da177e4SLinus Torvalds 		igrab(inode);
3791da177e4SLinus Torvalds 		if (nfs_have_delegation(inode, FMODE_WRITE))
3801da177e4SLinus Torvalds 			nfsi->change_attr++;
3811da177e4SLinus Torvalds 	}
382deb7d638STrond Myklebust 	SetPagePrivate(req->wb_page);
383277459d2STrond Myklebust 	set_page_private(req->wb_page, (unsigned long)req);
3841da177e4SLinus Torvalds 	nfsi->npages++;
385c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
3861da177e4SLinus Torvalds 	return 0;
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds /*
39089a09141SPeter Zijlstra  * Remove a write request from an inode
3911da177e4SLinus Torvalds  */
3921da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
3931da177e4SLinus Torvalds {
39488be9f99STrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
3951da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	BUG_ON (!NFS_WBACK_BUSY(req));
3981da177e4SLinus Torvalds 
399587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
400277459d2STrond Myklebust 	set_page_private(req->wb_page, 0);
401deb7d638STrond Myklebust 	ClearPagePrivate(req->wb_page);
4021da177e4SLinus Torvalds 	radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
4031da177e4SLinus Torvalds 	nfsi->npages--;
4041da177e4SLinus Torvalds 	if (!nfsi->npages) {
405587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
4061da177e4SLinus Torvalds 		iput(inode);
4071da177e4SLinus Torvalds 	} else
408587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
4091da177e4SLinus Torvalds 	nfs_clear_request(req);
4101da177e4SLinus Torvalds 	nfs_release_request(req);
4111da177e4SLinus Torvalds }
4121da177e4SLinus Torvalds 
41361822ab5STrond Myklebust static void
41461822ab5STrond Myklebust nfs_redirty_request(struct nfs_page *req)
41561822ab5STrond Myklebust {
41661822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
41761822ab5STrond Myklebust }
41861822ab5STrond Myklebust 
4191da177e4SLinus Torvalds /*
4201da177e4SLinus Torvalds  * Check if a request is dirty
4211da177e4SLinus Torvalds  */
4221da177e4SLinus Torvalds static inline int
4231da177e4SLinus Torvalds nfs_dirty_request(struct nfs_page *req)
4241da177e4SLinus Torvalds {
4255a6d41b3STrond Myklebust 	struct page *page = req->wb_page;
4265a6d41b3STrond Myklebust 
427612c9384STrond Myklebust 	if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
4285a6d41b3STrond Myklebust 		return 0;
4295a6d41b3STrond Myklebust 	return !PageWriteback(req->wb_page);
4301da177e4SLinus Torvalds }
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
4331da177e4SLinus Torvalds /*
4341da177e4SLinus Torvalds  * Add a request to the inode's commit list.
4351da177e4SLinus Torvalds  */
4361da177e4SLinus Torvalds static void
4371da177e4SLinus Torvalds nfs_mark_request_commit(struct nfs_page *req)
4381da177e4SLinus Torvalds {
43988be9f99STrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
4401da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4411da177e4SLinus Torvalds 
442587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
4431da177e4SLinus Torvalds 	nfsi->ncommit++;
444612c9384STrond Myklebust 	set_bit(PG_NEED_COMMIT, &(req)->wb_flags);
4455c369683STrond Myklebust 	radix_tree_tag_set(&nfsi->nfs_page_tree,
4465c369683STrond Myklebust 			req->wb_index,
4475c369683STrond Myklebust 			NFS_PAGE_TAG_COMMIT);
448587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
449fd39fc85SChristoph Lameter 	inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
450*c9e51e41SPeter Zijlstra 	inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
451a1803044STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
4521da177e4SLinus Torvalds }
4538e821cadSTrond Myklebust 
4548e821cadSTrond Myklebust static inline
4558e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
4568e821cadSTrond Myklebust {
4578e821cadSTrond Myklebust 	return data->verf.committed != NFS_FILE_SYNC;
4588e821cadSTrond Myklebust }
4598e821cadSTrond Myklebust 
4608e821cadSTrond Myklebust static inline
4618e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req)
4628e821cadSTrond Myklebust {
463612c9384STrond Myklebust 	if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
4648e821cadSTrond Myklebust 		nfs_mark_request_commit(req);
4658e821cadSTrond Myklebust 		return 1;
4668e821cadSTrond Myklebust 	}
4678e821cadSTrond Myklebust 	if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
4688e821cadSTrond Myklebust 		nfs_redirty_request(req);
4698e821cadSTrond Myklebust 		return 1;
4708e821cadSTrond Myklebust 	}
4718e821cadSTrond Myklebust 	return 0;
4728e821cadSTrond Myklebust }
4738e821cadSTrond Myklebust #else
4748e821cadSTrond Myklebust static inline void
4758e821cadSTrond Myklebust nfs_mark_request_commit(struct nfs_page *req)
4768e821cadSTrond Myklebust {
4778e821cadSTrond Myklebust }
4788e821cadSTrond Myklebust 
4798e821cadSTrond Myklebust static inline
4808e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
4818e821cadSTrond Myklebust {
4828e821cadSTrond Myklebust 	return 0;
4838e821cadSTrond Myklebust }
4848e821cadSTrond Myklebust 
4858e821cadSTrond Myklebust static inline
4868e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req)
4878e821cadSTrond Myklebust {
4888e821cadSTrond Myklebust 	return 0;
4898e821cadSTrond Myklebust }
4901da177e4SLinus Torvalds #endif
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds /*
4931da177e4SLinus Torvalds  * Wait for a request to complete.
4941da177e4SLinus Torvalds  *
4951da177e4SLinus Torvalds  * Interruptible by signals only if mounted with intr flag.
4961da177e4SLinus Torvalds  */
497ca52fec1STrond Myklebust static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
4981da177e4SLinus Torvalds {
4991da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
5001da177e4SLinus Torvalds 	struct nfs_page *req;
501ca52fec1STrond Myklebust 	pgoff_t idx_end, next;
5021da177e4SLinus Torvalds 	unsigned int		res = 0;
5031da177e4SLinus Torvalds 	int			error;
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	if (npages == 0)
5061da177e4SLinus Torvalds 		idx_end = ~0;
5071da177e4SLinus Torvalds 	else
5081da177e4SLinus Torvalds 		idx_end = idx_start + npages - 1;
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	next = idx_start;
5119fd367f0STrond Myklebust 	while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) {
5121da177e4SLinus Torvalds 		if (req->wb_index > idx_end)
5131da177e4SLinus Torvalds 			break;
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 		next = req->wb_index + 1;
516c6a556b8STrond Myklebust 		BUG_ON(!NFS_WBACK_BUSY(req));
5171da177e4SLinus Torvalds 
518c03b4024STrond Myklebust 		kref_get(&req->wb_kref);
519587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
5201da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
5211da177e4SLinus Torvalds 		nfs_release_request(req);
522587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
5231da177e4SLinus Torvalds 		if (error < 0)
5241da177e4SLinus Torvalds 			return error;
5251da177e4SLinus Torvalds 		res++;
5261da177e4SLinus Torvalds 	}
5271da177e4SLinus Torvalds 	return res;
5281da177e4SLinus Torvalds }
5291da177e4SLinus Torvalds 
53083715ad5STrond Myklebust static void nfs_cancel_commit_list(struct list_head *head)
53183715ad5STrond Myklebust {
53283715ad5STrond Myklebust 	struct nfs_page *req;
53383715ad5STrond Myklebust 
53483715ad5STrond Myklebust 	while(!list_empty(head)) {
53583715ad5STrond Myklebust 		req = nfs_list_entry(head->next);
536b6dff26aSTrond Myklebust 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
537*c9e51e41SPeter Zijlstra 		dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
538*c9e51e41SPeter Zijlstra 				BDI_RECLAIMABLE);
53983715ad5STrond Myklebust 		nfs_list_remove_request(req);
540612c9384STrond Myklebust 		clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
54183715ad5STrond Myklebust 		nfs_inode_remove_request(req);
542b6dff26aSTrond Myklebust 		nfs_unlock_request(req);
54383715ad5STrond Myklebust 	}
54483715ad5STrond Myklebust }
54583715ad5STrond Myklebust 
5461da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
5471da177e4SLinus Torvalds /*
5481da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
5491da177e4SLinus Torvalds  * @inode: NFS inode to scan
5501da177e4SLinus Torvalds  * @dst: destination list
5511da177e4SLinus Torvalds  * @idx_start: lower bound of page->index to scan.
5521da177e4SLinus Torvalds  * @npages: idx_start + npages sets the upper bound to scan.
5531da177e4SLinus Torvalds  *
5541da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
5551da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
5561da177e4SLinus Torvalds  */
5571da177e4SLinus Torvalds static int
558ca52fec1STrond Myklebust nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
5591da177e4SLinus Torvalds {
5601da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
5613da28eb1STrond Myklebust 	int res = 0;
5623da28eb1STrond Myklebust 
5633da28eb1STrond Myklebust 	if (nfsi->ncommit != 0) {
5645c369683STrond Myklebust 		res = nfs_scan_list(nfsi, dst, idx_start, npages,
5655c369683STrond Myklebust 				NFS_PAGE_TAG_COMMIT);
5661da177e4SLinus Torvalds 		nfsi->ncommit -= res;
5673da28eb1STrond Myklebust 	}
5681da177e4SLinus Torvalds 	return res;
5691da177e4SLinus Torvalds }
570c42de9ddSTrond Myklebust #else
571ca52fec1STrond Myklebust static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
572c42de9ddSTrond Myklebust {
573c42de9ddSTrond Myklebust 	return 0;
574c42de9ddSTrond Myklebust }
5751da177e4SLinus Torvalds #endif
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds /*
5781da177e4SLinus Torvalds  * Try to update any existing write request, or create one if there is none.
5791da177e4SLinus Torvalds  * In order to match, the request's credentials must match those of
5801da177e4SLinus Torvalds  * the calling process.
5811da177e4SLinus Torvalds  *
5821da177e4SLinus Torvalds  * Note: Should always be called with the Page Lock held!
5831da177e4SLinus Torvalds  */
5841da177e4SLinus Torvalds static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
585e21195a7STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
5861da177e4SLinus Torvalds {
58789a09141SPeter Zijlstra 	struct address_space *mapping = page->mapping;
58889a09141SPeter Zijlstra 	struct inode *inode = mapping->host;
5891da177e4SLinus Torvalds 	struct nfs_page		*req, *new = NULL;
590ca52fec1STrond Myklebust 	pgoff_t		rqend, end;
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	end = offset + bytes;
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	for (;;) {
5951da177e4SLinus Torvalds 		/* Loop over all inode entries and see if we find
5961da177e4SLinus Torvalds 		 * A request for the page we wish to update
5971da177e4SLinus Torvalds 		 */
598587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
599277459d2STrond Myklebust 		req = nfs_page_find_request_locked(page);
6001da177e4SLinus Torvalds 		if (req) {
6011da177e4SLinus Torvalds 			if (!nfs_lock_request_dontget(req)) {
6021da177e4SLinus Torvalds 				int error;
603277459d2STrond Myklebust 
604587142f8STrond Myklebust 				spin_unlock(&inode->i_lock);
6051da177e4SLinus Torvalds 				error = nfs_wait_on_request(req);
6061da177e4SLinus Torvalds 				nfs_release_request(req);
6071dd594b2SNeil Brown 				if (error < 0) {
6081dd594b2SNeil Brown 					if (new)
6091dd594b2SNeil Brown 						nfs_release_request(new);
6101da177e4SLinus Torvalds 					return ERR_PTR(error);
6111dd594b2SNeil Brown 				}
6121da177e4SLinus Torvalds 				continue;
6131da177e4SLinus Torvalds 			}
614587142f8STrond Myklebust 			spin_unlock(&inode->i_lock);
6151da177e4SLinus Torvalds 			if (new)
6161da177e4SLinus Torvalds 				nfs_release_request(new);
6171da177e4SLinus Torvalds 			break;
6181da177e4SLinus Torvalds 		}
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 		if (new) {
6211da177e4SLinus Torvalds 			int error;
6221da177e4SLinus Torvalds 			nfs_lock_request_dontget(new);
6231da177e4SLinus Torvalds 			error = nfs_inode_add_request(inode, new);
6241da177e4SLinus Torvalds 			if (error) {
625587142f8STrond Myklebust 				spin_unlock(&inode->i_lock);
6261da177e4SLinus Torvalds 				nfs_unlock_request(new);
6271da177e4SLinus Torvalds 				return ERR_PTR(error);
6281da177e4SLinus Torvalds 			}
629587142f8STrond Myklebust 			spin_unlock(&inode->i_lock);
6301da177e4SLinus Torvalds 			return new;
6311da177e4SLinus Torvalds 		}
632587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 		new = nfs_create_request(ctx, inode, page, offset, bytes);
6351da177e4SLinus Torvalds 		if (IS_ERR(new))
6361da177e4SLinus Torvalds 			return new;
6371da177e4SLinus Torvalds 	}
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	/* We have a request for our page.
6401da177e4SLinus Torvalds 	 * If the creds don't match, or the
6411da177e4SLinus Torvalds 	 * page addresses don't match,
6421da177e4SLinus Torvalds 	 * tell the caller to wait on the conflicting
6431da177e4SLinus Torvalds 	 * request.
6441da177e4SLinus Torvalds 	 */
6451da177e4SLinus Torvalds 	rqend = req->wb_offset + req->wb_bytes;
6461da177e4SLinus Torvalds 	if (req->wb_context != ctx
6471da177e4SLinus Torvalds 	    || req->wb_page != page
6481da177e4SLinus Torvalds 	    || !nfs_dirty_request(req)
6491da177e4SLinus Torvalds 	    || offset > rqend || end < req->wb_offset) {
6501da177e4SLinus Torvalds 		nfs_unlock_request(req);
6511da177e4SLinus Torvalds 		return ERR_PTR(-EBUSY);
6521da177e4SLinus Torvalds 	}
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
6551da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
6561da177e4SLinus Torvalds 		req->wb_offset = offset;
6571da177e4SLinus Torvalds 		req->wb_pgbase = offset;
6581da177e4SLinus Torvalds 		req->wb_bytes = rqend - req->wb_offset;
6591da177e4SLinus Torvalds 	}
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	if (end > rqend)
6621da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	return req;
6651da177e4SLinus Torvalds }
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
6681da177e4SLinus Torvalds {
669cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
6701da177e4SLinus Torvalds 	struct nfs_page	*req;
6711a54533eSTrond Myklebust 	int do_flush, status;
6721da177e4SLinus Torvalds 	/*
6731da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
6741da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
6751da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
6761da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
6771da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
6781da177e4SLinus Torvalds 	 * dropped page.
6791da177e4SLinus Torvalds 	 */
6801a54533eSTrond Myklebust 	do {
681277459d2STrond Myklebust 		req = nfs_page_find_request(page);
6821a54533eSTrond Myklebust 		if (req == NULL)
6831a54533eSTrond Myklebust 			return 0;
6841a54533eSTrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx
685e261f51fSTrond Myklebust 			|| !nfs_dirty_request(req);
6861da177e4SLinus Torvalds 		nfs_release_request(req);
6871a54533eSTrond Myklebust 		if (!do_flush)
6881a54533eSTrond Myklebust 			return 0;
689277459d2STrond Myklebust 		status = nfs_wb_page(page->mapping->host, page);
6901a54533eSTrond Myklebust 	} while (status == 0);
6911a54533eSTrond Myklebust 	return status;
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds /*
6951da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
6961da177e4SLinus Torvalds  *
6971da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
6981da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
6991da177e4SLinus Torvalds  */
7001da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
7011da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
7021da177e4SLinus Torvalds {
703cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
7041da177e4SLinus Torvalds 	struct inode	*inode = page->mapping->host;
7051da177e4SLinus Torvalds 	int		status = 0;
7061da177e4SLinus Torvalds 
70791d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
70891d5b470SChuck Lever 
7091da177e4SLinus Torvalds 	dprintk("NFS:      nfs_updatepage(%s/%s %d@%Ld)\n",
71001cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
71101cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name, count,
7120bbacc40SChuck Lever 		(long long)(page_offset(page) +offset));
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	/* If we're not using byte range locks, and we know the page
7151da177e4SLinus Torvalds 	 * is entirely in cache, it may be more efficient to avoid
7161da177e4SLinus Torvalds 	 * fragmenting write requests.
7171da177e4SLinus Torvalds 	 */
718ab0a3dbeSTrond Myklebust 	if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
71949a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
7201da177e4SLinus Torvalds 		offset = 0;
7211da177e4SLinus Torvalds 	}
7221da177e4SLinus Torvalds 
723e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
724e261f51fSTrond Myklebust 	__set_page_dirty_nobuffers(page);
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds         dprintk("NFS:      nfs_updatepage returns %d (isize %Ld)\n",
7271da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
7281da177e4SLinus Torvalds 	if (status < 0)
729a301b777STrond Myklebust 		nfs_set_pageerror(page);
7301da177e4SLinus Torvalds 	return status;
7311da177e4SLinus Torvalds }
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds static void nfs_writepage_release(struct nfs_page *req)
7341da177e4SLinus Torvalds {
7358e821cadSTrond Myklebust 
73644dd151dSTrond Myklebust 	if (PageError(req->wb_page)) {
73744dd151dSTrond Myklebust 		nfs_end_page_writeback(req->wb_page);
73844dd151dSTrond Myklebust 		nfs_inode_remove_request(req);
73944dd151dSTrond Myklebust 	} else if (!nfs_reschedule_unstable_write(req)) {
74044dd151dSTrond Myklebust 		/* Set the PG_uptodate flag */
74144dd151dSTrond Myklebust 		nfs_mark_uptodate(req->wb_page, req->wb_pgbase, req->wb_bytes);
74289a09141SPeter Zijlstra 		nfs_end_page_writeback(req->wb_page);
7431da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
7448e821cadSTrond Myklebust 	} else
7458e821cadSTrond Myklebust 		nfs_end_page_writeback(req->wb_page);
7469fd367f0STrond Myklebust 	nfs_clear_page_tag_locked(req);
7471da177e4SLinus Torvalds }
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds static inline int flush_task_priority(int how)
7501da177e4SLinus Torvalds {
7511da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
7521da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
7531da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
7541da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
7551da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
7561da177e4SLinus Torvalds 	}
7571da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
7581da177e4SLinus Torvalds }
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds /*
7611da177e4SLinus Torvalds  * Set up the argument/result storage required for the RPC call.
7621da177e4SLinus Torvalds  */
7631da177e4SLinus Torvalds static void nfs_write_rpcsetup(struct nfs_page *req,
7641da177e4SLinus Torvalds 		struct nfs_write_data *data,
765788e7a89STrond Myklebust 		const struct rpc_call_ops *call_ops,
7661da177e4SLinus Torvalds 		unsigned int count, unsigned int offset,
7671da177e4SLinus Torvalds 		int how)
7681da177e4SLinus Torvalds {
7691da177e4SLinus Torvalds 	struct inode		*inode;
770788e7a89STrond Myklebust 	int flags;
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
7731da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	data->req = req;
77688be9f99STrond Myklebust 	data->inode = inode = req->wb_context->path.dentry->d_inode;
7771da177e4SLinus Torvalds 	data->cred = req->wb_context->cred;
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(inode);
7801da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
7811da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
7821da177e4SLinus Torvalds 	data->args.pages  = data->pagevec;
7831da177e4SLinus Torvalds 	data->args.count  = count;
7841da177e4SLinus Torvalds 	data->args.context = req->wb_context;
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
7871da177e4SLinus Torvalds 	data->res.count   = count;
7881da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
7890e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
7901da177e4SLinus Torvalds 
791788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
792788e7a89STrond Myklebust 	flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
793788e7a89STrond Myklebust 	rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
7941da177e4SLinus Torvalds 	NFS_PROTO(inode)->write_setup(data, how);
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	data->task.tk_priority = flush_task_priority(how);
7971da177e4SLinus Torvalds 	data->task.tk_cookie = (unsigned long)inode;
7981da177e4SLinus Torvalds 
799a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated write call "
800a3f565b1SChuck Lever 		"(req %s/%Ld, %u bytes @ offset %Lu)\n",
8010bbacc40SChuck Lever 		data->task.tk_pid,
8021da177e4SLinus Torvalds 		inode->i_sb->s_id,
8031da177e4SLinus Torvalds 		(long long)NFS_FILEID(inode),
8041da177e4SLinus Torvalds 		count,
8051da177e4SLinus Torvalds 		(unsigned long long)data->args.offset);
8061da177e4SLinus Torvalds }
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds static void nfs_execute_write(struct nfs_write_data *data)
8091da177e4SLinus Torvalds {
8101da177e4SLinus Torvalds 	struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
8111da177e4SLinus Torvalds 	sigset_t oldset;
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 	rpc_clnt_sigmask(clnt, &oldset);
8141da177e4SLinus Torvalds 	rpc_execute(&data->task);
8151da177e4SLinus Torvalds 	rpc_clnt_sigunmask(clnt, &oldset);
8161da177e4SLinus Torvalds }
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds /*
8191da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
8201da177e4SLinus Torvalds  * contiguous dirty area on one page.
8211da177e4SLinus Torvalds  */
8228d5658c9STrond Myklebust static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
8231da177e4SLinus Torvalds {
8241da177e4SLinus Torvalds 	struct nfs_page *req = nfs_list_entry(head->next);
8251da177e4SLinus Torvalds 	struct page *page = req->wb_page;
8261da177e4SLinus Torvalds 	struct nfs_write_data *data;
827e9f7bee1STrond Myklebust 	size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
828e9f7bee1STrond Myklebust 	unsigned int offset;
8291da177e4SLinus Torvalds 	int requests = 0;
8301da177e4SLinus Torvalds 	LIST_HEAD(list);
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 	nfs_list_remove_request(req);
8331da177e4SLinus Torvalds 
834bcb71bbaSTrond Myklebust 	nbytes = count;
835e9f7bee1STrond Myklebust 	do {
836e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
837e9f7bee1STrond Myklebust 
8388d5658c9STrond Myklebust 		data = nfs_writedata_alloc(1);
8391da177e4SLinus Torvalds 		if (!data)
8401da177e4SLinus Torvalds 			goto out_bad;
8411da177e4SLinus Torvalds 		list_add(&data->pages, &list);
8421da177e4SLinus Torvalds 		requests++;
843e9f7bee1STrond Myklebust 		nbytes -= len;
844e9f7bee1STrond Myklebust 	} while (nbytes != 0);
8451da177e4SLinus Torvalds 	atomic_set(&req->wb_complete, requests);
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	ClearPageError(page);
8481da177e4SLinus Torvalds 	offset = 0;
849bcb71bbaSTrond Myklebust 	nbytes = count;
8501da177e4SLinus Torvalds 	do {
8511da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
8521da177e4SLinus Torvalds 		list_del_init(&data->pages);
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 		data->pagevec[0] = page;
8551da177e4SLinus Torvalds 
856bcb71bbaSTrond Myklebust 		if (nbytes < wsize)
857bcb71bbaSTrond Myklebust 			wsize = nbytes;
858788e7a89STrond Myklebust 		nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
859788e7a89STrond Myklebust 				   wsize, offset, how);
8601da177e4SLinus Torvalds 		offset += wsize;
8611da177e4SLinus Torvalds 		nbytes -= wsize;
8621da177e4SLinus Torvalds 		nfs_execute_write(data);
8631da177e4SLinus Torvalds 	} while (nbytes != 0);
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	return 0;
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds out_bad:
8681da177e4SLinus Torvalds 	while (!list_empty(&list)) {
8691da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
8701da177e4SLinus Torvalds 		list_del(&data->pages);
8718aca67f0STrond Myklebust 		nfs_writedata_release(data);
8721da177e4SLinus Torvalds 	}
87361822ab5STrond Myklebust 	nfs_redirty_request(req);
8746d677e35STrond Myklebust 	nfs_end_page_writeback(req->wb_page);
8759fd367f0STrond Myklebust 	nfs_clear_page_tag_locked(req);
8761da177e4SLinus Torvalds 	return -ENOMEM;
8771da177e4SLinus Torvalds }
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds /*
8801da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
8811da177e4SLinus Torvalds  * The page must have been locked by the caller.
8821da177e4SLinus Torvalds  *
8831da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
8841da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
8851da177e4SLinus Torvalds  * that has been written but not committed.
8861da177e4SLinus Torvalds  */
8878d5658c9STrond Myklebust static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
8881da177e4SLinus Torvalds {
8891da177e4SLinus Torvalds 	struct nfs_page		*req;
8901da177e4SLinus Torvalds 	struct page		**pages;
8911da177e4SLinus Torvalds 	struct nfs_write_data	*data;
8921da177e4SLinus Torvalds 
8938d5658c9STrond Myklebust 	data = nfs_writedata_alloc(npages);
8941da177e4SLinus Torvalds 	if (!data)
8951da177e4SLinus Torvalds 		goto out_bad;
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 	pages = data->pagevec;
8981da177e4SLinus Torvalds 	while (!list_empty(head)) {
8991da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
9001da177e4SLinus Torvalds 		nfs_list_remove_request(req);
9011da177e4SLinus Torvalds 		nfs_list_add_request(req, &data->pages);
9021da177e4SLinus Torvalds 		ClearPageError(req->wb_page);
9031da177e4SLinus Torvalds 		*pages++ = req->wb_page;
9041da177e4SLinus Torvalds 	}
9051da177e4SLinus Torvalds 	req = nfs_list_entry(data->pages.next);
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	/* Set up the argument struct */
908788e7a89STrond Myklebust 	nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 	nfs_execute_write(data);
9111da177e4SLinus Torvalds 	return 0;
9121da177e4SLinus Torvalds  out_bad:
9131da177e4SLinus Torvalds 	while (!list_empty(head)) {
91410afec90STrond Myklebust 		req = nfs_list_entry(head->next);
9151da177e4SLinus Torvalds 		nfs_list_remove_request(req);
91661822ab5STrond Myklebust 		nfs_redirty_request(req);
9176d677e35STrond Myklebust 		nfs_end_page_writeback(req->wb_page);
9189fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
9191da177e4SLinus Torvalds 	}
9201da177e4SLinus Torvalds 	return -ENOMEM;
9211da177e4SLinus Torvalds }
9221da177e4SLinus Torvalds 
923c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
924c63c7b05STrond Myklebust 				  struct inode *inode, int ioflags)
9251da177e4SLinus Torvalds {
9267d46a49fSTrond Myklebust 	int wsize = NFS_SERVER(inode)->wsize;
9271da177e4SLinus Torvalds 
928bcb71bbaSTrond Myklebust 	if (wsize < PAGE_CACHE_SIZE)
929c63c7b05STrond Myklebust 		nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
930bcb71bbaSTrond Myklebust 	else
931c63c7b05STrond Myklebust 		nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
9321da177e4SLinus Torvalds }
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds /*
9351da177e4SLinus Torvalds  * Handle a write reply that flushed part of a page.
9361da177e4SLinus Torvalds  */
937788e7a89STrond Myklebust static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
9381da177e4SLinus Torvalds {
939788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
9401da177e4SLinus Torvalds 	struct nfs_page		*req = data->req;
9411da177e4SLinus Torvalds 	struct page		*page = req->wb_page;
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	dprintk("NFS: write (%s/%Ld %d@%Ld)",
94488be9f99STrond Myklebust 		req->wb_context->path.dentry->d_inode->i_sb->s_id,
94588be9f99STrond Myklebust 		(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
9461da177e4SLinus Torvalds 		req->wb_bytes,
9471da177e4SLinus Torvalds 		(long long)req_offset(req));
9481da177e4SLinus Torvalds 
949788e7a89STrond Myklebust 	if (nfs_writeback_done(task, data) != 0)
950788e7a89STrond Myklebust 		return;
951788e7a89STrond Myklebust 
952788e7a89STrond Myklebust 	if (task->tk_status < 0) {
953a301b777STrond Myklebust 		nfs_set_pageerror(page);
9547b159fc1STrond Myklebust 		nfs_context_set_write_error(req->wb_context, task->tk_status);
955788e7a89STrond Myklebust 		dprintk(", error = %d\n", task->tk_status);
9568e821cadSTrond Myklebust 		goto out;
9578e821cadSTrond Myklebust 	}
9588e821cadSTrond Myklebust 
9598e821cadSTrond Myklebust 	if (nfs_write_need_commit(data)) {
960587142f8STrond Myklebust 		struct inode *inode = page->mapping->host;
9618e821cadSTrond Myklebust 
962587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
9638e821cadSTrond Myklebust 		if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
9648e821cadSTrond Myklebust 			/* Do nothing we need to resend the writes */
9658e821cadSTrond Myklebust 		} else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
9661da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
9671da177e4SLinus Torvalds 			dprintk(" defer commit\n");
9681da177e4SLinus Torvalds 		} else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
9698e821cadSTrond Myklebust 			set_bit(PG_NEED_RESCHED, &req->wb_flags);
9708e821cadSTrond Myklebust 			clear_bit(PG_NEED_COMMIT, &req->wb_flags);
9711da177e4SLinus Torvalds 			dprintk(" server reboot detected\n");
9721da177e4SLinus Torvalds 		}
973587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
9741da177e4SLinus Torvalds 	} else
9751da177e4SLinus Torvalds 		dprintk(" OK\n");
9761da177e4SLinus Torvalds 
9778e821cadSTrond Myklebust out:
9781da177e4SLinus Torvalds 	if (atomic_dec_and_test(&req->wb_complete))
9791da177e4SLinus Torvalds 		nfs_writepage_release(req);
9801da177e4SLinus Torvalds }
9811da177e4SLinus Torvalds 
982788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops = {
983788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_partial,
984788e7a89STrond Myklebust 	.rpc_release = nfs_writedata_release,
985788e7a89STrond Myklebust };
986788e7a89STrond Myklebust 
9871da177e4SLinus Torvalds /*
9881da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
9891da177e4SLinus Torvalds  *
9901da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
9911da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
9921da177e4SLinus Torvalds  *	  as the page has a write request pending.
9931da177e4SLinus Torvalds  */
994788e7a89STrond Myklebust static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
9951da177e4SLinus Torvalds {
996788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
9971da177e4SLinus Torvalds 	struct nfs_page		*req;
9981da177e4SLinus Torvalds 	struct page		*page;
9991da177e4SLinus Torvalds 
1000788e7a89STrond Myklebust 	if (nfs_writeback_done(task, data) != 0)
1001788e7a89STrond Myklebust 		return;
1002788e7a89STrond Myklebust 
10031da177e4SLinus Torvalds 	/* Update attributes as result of writeback. */
10041da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
10051da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
10061da177e4SLinus Torvalds 		nfs_list_remove_request(req);
10071da177e4SLinus Torvalds 		page = req->wb_page;
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 		dprintk("NFS: write (%s/%Ld %d@%Ld)",
101088be9f99STrond Myklebust 			req->wb_context->path.dentry->d_inode->i_sb->s_id,
101188be9f99STrond Myklebust 			(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
10121da177e4SLinus Torvalds 			req->wb_bytes,
10131da177e4SLinus Torvalds 			(long long)req_offset(req));
10141da177e4SLinus Torvalds 
1015788e7a89STrond Myklebust 		if (task->tk_status < 0) {
1016a301b777STrond Myklebust 			nfs_set_pageerror(page);
10177b159fc1STrond Myklebust 			nfs_context_set_write_error(req->wb_context, task->tk_status);
1018788e7a89STrond Myklebust 			dprintk(", error = %d\n", task->tk_status);
10198e821cadSTrond Myklebust 			goto remove_request;
10201da177e4SLinus Torvalds 		}
10211da177e4SLinus Torvalds 
10228e821cadSTrond Myklebust 		if (nfs_write_need_commit(data)) {
10231da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
10241da177e4SLinus Torvalds 			nfs_mark_request_commit(req);
10258e821cadSTrond Myklebust 			nfs_end_page_writeback(page);
10261da177e4SLinus Torvalds 			dprintk(" marked for commit\n");
10278e821cadSTrond Myklebust 			goto next;
10288e821cadSTrond Myklebust 		}
102944dd151dSTrond Myklebust 		/* Set the PG_uptodate flag? */
103044dd151dSTrond Myklebust 		nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
10318e821cadSTrond Myklebust 		dprintk(" OK\n");
10328e821cadSTrond Myklebust remove_request:
10338e821cadSTrond Myklebust 		nfs_end_page_writeback(page);
10341da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
10351da177e4SLinus Torvalds 	next:
10369fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
10371da177e4SLinus Torvalds 	}
10381da177e4SLinus Torvalds }
10391da177e4SLinus Torvalds 
1040788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops = {
1041788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_full,
1042788e7a89STrond Myklebust 	.rpc_release = nfs_writedata_release,
1043788e7a89STrond Myklebust };
1044788e7a89STrond Myklebust 
1045788e7a89STrond Myklebust 
10461da177e4SLinus Torvalds /*
10471da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
10481da177e4SLinus Torvalds  */
1049462d5b32SChuck Lever int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
10501da177e4SLinus Torvalds {
10511da177e4SLinus Torvalds 	struct nfs_writeargs	*argp = &data->args;
10521da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
1053788e7a89STrond Myklebust 	int status;
10541da177e4SLinus Torvalds 
1055a3f565b1SChuck Lever 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
10561da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
10571da177e4SLinus Torvalds 
1058f551e44fSChuck Lever 	/*
1059f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1060f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1061f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1062f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1063f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1064f551e44fSChuck Lever 	 */
1065788e7a89STrond Myklebust 	status = NFS_PROTO(data->inode)->write_done(task, data);
1066788e7a89STrond Myklebust 	if (status != 0)
1067788e7a89STrond Myklebust 		return status;
106891d5b470SChuck Lever 	nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
106991d5b470SChuck Lever 
10701da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
10711da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
10721da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
10731da177e4SLinus Torvalds 		 * commit data to stable storage even though we
10741da177e4SLinus Torvalds 		 * requested it.
10751da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
10761da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
10771da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
10781da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
10791da177e4SLinus Torvalds 		 */
10801da177e4SLinus Torvalds 		static unsigned long    complain;
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
10831da177e4SLinus Torvalds 			dprintk("NFS: faulty NFS server %s:"
10841da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
108554ceac45SDavid Howells 				NFS_SERVER(data->inode)->nfs_client->cl_hostname,
10861da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
10871da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
10881da177e4SLinus Torvalds 		}
10891da177e4SLinus Torvalds 	}
10901da177e4SLinus Torvalds #endif
10911da177e4SLinus Torvalds 	/* Is this a short write? */
10921da177e4SLinus Torvalds 	if (task->tk_status >= 0 && resp->count < argp->count) {
10931da177e4SLinus Torvalds 		static unsigned long    complain;
10941da177e4SLinus Torvalds 
109591d5b470SChuck Lever 		nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
109691d5b470SChuck Lever 
10971da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
10981da177e4SLinus Torvalds 		if (resp->count != 0) {
10991da177e4SLinus Torvalds 			/* Was this an NFSv2 write or an NFSv3 stable write? */
11001da177e4SLinus Torvalds 			if (resp->verf->committed != NFS_UNSTABLE) {
11011da177e4SLinus Torvalds 				/* Resend from where the server left off */
11021da177e4SLinus Torvalds 				argp->offset += resp->count;
11031da177e4SLinus Torvalds 				argp->pgbase += resp->count;
11041da177e4SLinus Torvalds 				argp->count -= resp->count;
11051da177e4SLinus Torvalds 			} else {
11061da177e4SLinus Torvalds 				/* Resend as a stable write in order to avoid
11071da177e4SLinus Torvalds 				 * headaches in the case of a server crash.
11081da177e4SLinus Torvalds 				 */
11091da177e4SLinus Torvalds 				argp->stable = NFS_FILE_SYNC;
11101da177e4SLinus Torvalds 			}
11111da177e4SLinus Torvalds 			rpc_restart_call(task);
1112788e7a89STrond Myklebust 			return -EAGAIN;
11131da177e4SLinus Torvalds 		}
11141da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
11151da177e4SLinus Torvalds 			printk(KERN_WARNING
11161da177e4SLinus Torvalds 			       "NFS: Server wrote zero bytes, expected %u.\n",
11171da177e4SLinus Torvalds 					argp->count);
11181da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
11191da177e4SLinus Torvalds 		}
11201da177e4SLinus Torvalds 		/* Can't do anything about it except throw an error. */
11211da177e4SLinus Torvalds 		task->tk_status = -EIO;
11221da177e4SLinus Torvalds 	}
1123788e7a89STrond Myklebust 	return 0;
11241da177e4SLinus Torvalds }
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1128963d8fe5STrond Myklebust void nfs_commit_release(void *wdata)
11291da177e4SLinus Torvalds {
11301da177e4SLinus Torvalds 	nfs_commit_free(wdata);
11311da177e4SLinus Torvalds }
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds /*
11341da177e4SLinus Torvalds  * Set up the argument/result storage required for the RPC call.
11351da177e4SLinus Torvalds  */
11361da177e4SLinus Torvalds static void nfs_commit_rpcsetup(struct list_head *head,
1137788e7a89STrond Myklebust 		struct nfs_write_data *data,
1138788e7a89STrond Myklebust 		int how)
11391da177e4SLinus Torvalds {
11403da28eb1STrond Myklebust 	struct nfs_page		*first;
11411da177e4SLinus Torvalds 	struct inode		*inode;
1142788e7a89STrond Myklebust 	int flags;
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
11451da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds 	list_splice_init(head, &data->pages);
11481da177e4SLinus Torvalds 	first = nfs_list_entry(data->pages.next);
114988be9f99STrond Myklebust 	inode = first->wb_context->path.dentry->d_inode;
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 	data->inode	  = inode;
11521da177e4SLinus Torvalds 	data->cred	  = first->wb_context->cred;
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(data->inode);
11553da28eb1STrond Myklebust 	/* Note: we always request a commit of the entire inode */
11563da28eb1STrond Myklebust 	data->args.offset = 0;
11573da28eb1STrond Myklebust 	data->args.count  = 0;
11583da28eb1STrond Myklebust 	data->res.count   = 0;
11591da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
11601da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
11610e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
11621da177e4SLinus Torvalds 
1163788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
1164788e7a89STrond Myklebust 	flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1165788e7a89STrond Myklebust 	rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
11661da177e4SLinus Torvalds 	NFS_PROTO(inode)->commit_setup(data, how);
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds 	data->task.tk_priority = flush_task_priority(how);
11691da177e4SLinus Torvalds 	data->task.tk_cookie = (unsigned long)inode;
11701da177e4SLinus Torvalds 
1171a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
11721da177e4SLinus Torvalds }
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds /*
11751da177e4SLinus Torvalds  * Commit dirty pages
11761da177e4SLinus Torvalds  */
11771da177e4SLinus Torvalds static int
117840859d7eSChuck Lever nfs_commit_list(struct inode *inode, struct list_head *head, int how)
11791da177e4SLinus Torvalds {
11801da177e4SLinus Torvalds 	struct nfs_write_data	*data;
11811da177e4SLinus Torvalds 	struct nfs_page         *req;
11821da177e4SLinus Torvalds 
1183e9f7bee1STrond Myklebust 	data = nfs_commit_alloc();
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 	if (!data)
11861da177e4SLinus Torvalds 		goto out_bad;
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 	/* Set up the argument struct */
11891da177e4SLinus Torvalds 	nfs_commit_rpcsetup(head, data, how);
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds 	nfs_execute_write(data);
11921da177e4SLinus Torvalds 	return 0;
11931da177e4SLinus Torvalds  out_bad:
11941da177e4SLinus Torvalds 	while (!list_empty(head)) {
11951da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
11961da177e4SLinus Torvalds 		nfs_list_remove_request(req);
11971da177e4SLinus Torvalds 		nfs_mark_request_commit(req);
119883715ad5STrond Myklebust 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1199*c9e51e41SPeter Zijlstra 		dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1200*c9e51e41SPeter Zijlstra 				BDI_RECLAIMABLE);
12019fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
12021da177e4SLinus Torvalds 	}
12031da177e4SLinus Torvalds 	return -ENOMEM;
12041da177e4SLinus Torvalds }
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds /*
12071da177e4SLinus Torvalds  * COMMIT call returned
12081da177e4SLinus Torvalds  */
1209788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
12101da177e4SLinus Torvalds {
1211963d8fe5STrond Myklebust 	struct nfs_write_data	*data = calldata;
12121da177e4SLinus Torvalds 	struct nfs_page		*req;
12131da177e4SLinus Torvalds 
1214a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
12151da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
12161da177e4SLinus Torvalds 
1217788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1218788e7a89STrond Myklebust 	if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1219788e7a89STrond Myklebust 		return;
1220788e7a89STrond Myklebust 
12211da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
12221da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
12231da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1224612c9384STrond Myklebust 		clear_bit(PG_NEED_COMMIT, &(req)->wb_flags);
1225fd39fc85SChristoph Lameter 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1226*c9e51e41SPeter Zijlstra 		dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1227*c9e51e41SPeter Zijlstra 				BDI_RECLAIMABLE);
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds 		dprintk("NFS: commit (%s/%Ld %d@%Ld)",
123088be9f99STrond Myklebust 			req->wb_context->path.dentry->d_inode->i_sb->s_id,
123188be9f99STrond Myklebust 			(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
12321da177e4SLinus Torvalds 			req->wb_bytes,
12331da177e4SLinus Torvalds 			(long long)req_offset(req));
12341da177e4SLinus Torvalds 		if (task->tk_status < 0) {
12357b159fc1STrond Myklebust 			nfs_context_set_write_error(req->wb_context, task->tk_status);
12361da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
12371da177e4SLinus Torvalds 			dprintk(", error = %d\n", task->tk_status);
12381da177e4SLinus Torvalds 			goto next;
12391da177e4SLinus Torvalds 		}
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
12421da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
12431da177e4SLinus Torvalds 		if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
12441da177e4SLinus Torvalds 			/* We have a match */
124544dd151dSTrond Myklebust 			/* Set the PG_uptodate flag */
124644dd151dSTrond Myklebust 			nfs_mark_uptodate(req->wb_page, req->wb_pgbase,
124744dd151dSTrond Myklebust 					req->wb_bytes);
12481da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
12491da177e4SLinus Torvalds 			dprintk(" OK\n");
12501da177e4SLinus Torvalds 			goto next;
12511da177e4SLinus Torvalds 		}
12521da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
12531da177e4SLinus Torvalds 		dprintk(" mismatch\n");
125461822ab5STrond Myklebust 		nfs_redirty_request(req);
12551da177e4SLinus Torvalds 	next:
12569fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
12571da177e4SLinus Torvalds 	}
12581da177e4SLinus Torvalds }
1259788e7a89STrond Myklebust 
1260788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
1261788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1262788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1263788e7a89STrond Myklebust };
12641da177e4SLinus Torvalds 
12653da28eb1STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
12661da177e4SLinus Torvalds {
12671da177e4SLinus Torvalds 	LIST_HEAD(head);
12687d46a49fSTrond Myklebust 	int res;
12691da177e4SLinus Torvalds 
1270587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
12713da28eb1STrond Myklebust 	res = nfs_scan_commit(inode, &head, 0, 0);
1272587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
12731da177e4SLinus Torvalds 	if (res) {
12747d46a49fSTrond Myklebust 		int error = nfs_commit_list(inode, &head, how);
12751da177e4SLinus Torvalds 		if (error < 0)
12761da177e4SLinus Torvalds 			return error;
12773da28eb1STrond Myklebust 	}
12781da177e4SLinus Torvalds 	return res;
12791da177e4SLinus Torvalds }
1280c63c7b05STrond Myklebust #else
1281c63c7b05STrond Myklebust static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1282c63c7b05STrond Myklebust {
1283c63c7b05STrond Myklebust 	return 0;
1284c63c7b05STrond Myklebust }
12851da177e4SLinus Torvalds #endif
12861da177e4SLinus Torvalds 
12871c75950bSTrond Myklebust long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
12881da177e4SLinus Torvalds {
12891c75950bSTrond Myklebust 	struct inode *inode = mapping->host;
1290ca52fec1STrond Myklebust 	pgoff_t idx_start, idx_end;
12911c75950bSTrond Myklebust 	unsigned int npages = 0;
1292c42de9ddSTrond Myklebust 	LIST_HEAD(head);
129370b9ecbdSTrond Myklebust 	int nocommit = how & FLUSH_NOCOMMIT;
12943f442547STrond Myklebust 	long pages, ret;
12951da177e4SLinus Torvalds 
12961c75950bSTrond Myklebust 	/* FIXME */
12971c75950bSTrond Myklebust 	if (wbc->range_cyclic)
12981c75950bSTrond Myklebust 		idx_start = 0;
12991c75950bSTrond Myklebust 	else {
13001c75950bSTrond Myklebust 		idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
13011c75950bSTrond Myklebust 		idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
13021c75950bSTrond Myklebust 		if (idx_end > idx_start) {
1303ca52fec1STrond Myklebust 			pgoff_t l_npages = 1 + idx_end - idx_start;
13041c75950bSTrond Myklebust 			npages = l_npages;
13051c75950bSTrond Myklebust 			if (sizeof(npages) != sizeof(l_npages) &&
1306ca52fec1STrond Myklebust 					(pgoff_t)npages != l_npages)
13071c75950bSTrond Myklebust 				npages = 0;
13081c75950bSTrond Myklebust 		}
13091c75950bSTrond Myklebust 	}
1310c42de9ddSTrond Myklebust 	how &= ~FLUSH_NOCOMMIT;
1311587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
13121da177e4SLinus Torvalds 	do {
1313c42de9ddSTrond Myklebust 		ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1314c42de9ddSTrond Myklebust 		if (ret != 0)
1315c42de9ddSTrond Myklebust 			continue;
1316c42de9ddSTrond Myklebust 		if (nocommit)
1317c42de9ddSTrond Myklebust 			break;
1318d2ccddf0STrond Myklebust 		pages = nfs_scan_commit(inode, &head, idx_start, npages);
1319724c439cSTrond Myklebust 		if (pages == 0)
1320c42de9ddSTrond Myklebust 			break;
1321d2ccddf0STrond Myklebust 		if (how & FLUSH_INVALIDATE) {
1322587142f8STrond Myklebust 			spin_unlock(&inode->i_lock);
132383715ad5STrond Myklebust 			nfs_cancel_commit_list(&head);
1324e8e058e8STrond Myklebust 			ret = pages;
1325587142f8STrond Myklebust 			spin_lock(&inode->i_lock);
1326d2ccddf0STrond Myklebust 			continue;
1327d2ccddf0STrond Myklebust 		}
1328d2ccddf0STrond Myklebust 		pages += nfs_scan_commit(inode, &head, 0, 0);
1329587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
1330c42de9ddSTrond Myklebust 		ret = nfs_commit_list(inode, &head, how);
1331587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
1332587142f8STrond Myklebust 
1333c42de9ddSTrond Myklebust 	} while (ret >= 0);
1334587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
1335c42de9ddSTrond Myklebust 	return ret;
13361da177e4SLinus Torvalds }
13371da177e4SLinus Torvalds 
133834901f70STrond Myklebust static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
13391c75950bSTrond Myklebust {
13401c75950bSTrond Myklebust 	int ret;
13411c75950bSTrond Myklebust 
134234901f70STrond Myklebust 	ret = nfs_writepages(mapping, wbc);
134361822ab5STrond Myklebust 	if (ret < 0)
134461822ab5STrond Myklebust 		goto out;
134534901f70STrond Myklebust 	ret = nfs_sync_mapping_wait(mapping, wbc, how);
1346ed90ef51STrond Myklebust 	if (ret < 0)
1347ed90ef51STrond Myklebust 		goto out;
13481c75950bSTrond Myklebust 	return 0;
134961822ab5STrond Myklebust out:
1350e507d9ebSTrond Myklebust 	__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
13511c75950bSTrond Myklebust 	return ret;
13521c75950bSTrond Myklebust }
13531c75950bSTrond Myklebust 
135434901f70STrond Myklebust /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
135534901f70STrond Myklebust static int nfs_write_mapping(struct address_space *mapping, int how)
135634901f70STrond Myklebust {
135734901f70STrond Myklebust 	struct writeback_control wbc = {
135834901f70STrond Myklebust 		.bdi = mapping->backing_dev_info,
135934901f70STrond Myklebust 		.sync_mode = WB_SYNC_NONE,
136034901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
136134901f70STrond Myklebust 		.for_writepages = 1,
136234901f70STrond Myklebust 		.range_cyclic = 1,
136334901f70STrond Myklebust 	};
136434901f70STrond Myklebust 	int ret;
136534901f70STrond Myklebust 
136634901f70STrond Myklebust 	ret = __nfs_write_mapping(mapping, &wbc, how);
136734901f70STrond Myklebust 	if (ret < 0)
136834901f70STrond Myklebust 		return ret;
136934901f70STrond Myklebust 	wbc.sync_mode = WB_SYNC_ALL;
137034901f70STrond Myklebust 	return __nfs_write_mapping(mapping, &wbc, how);
137134901f70STrond Myklebust }
137234901f70STrond Myklebust 
1373ed90ef51STrond Myklebust /*
1374ed90ef51STrond Myklebust  * flush the inode to disk.
1375ed90ef51STrond Myklebust  */
1376ed90ef51STrond Myklebust int nfs_wb_all(struct inode *inode)
13771c75950bSTrond Myklebust {
1378ed90ef51STrond Myklebust 	return nfs_write_mapping(inode->i_mapping, 0);
1379ed90ef51STrond Myklebust }
13801c75950bSTrond Myklebust 
1381ed90ef51STrond Myklebust int nfs_wb_nocommit(struct inode *inode)
1382ed90ef51STrond Myklebust {
1383ed90ef51STrond Myklebust 	return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
13841c75950bSTrond Myklebust }
13851c75950bSTrond Myklebust 
13861b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
13871b3b4a1aSTrond Myklebust {
13881b3b4a1aSTrond Myklebust 	struct nfs_page *req;
13891b3b4a1aSTrond Myklebust 	loff_t range_start = page_offset(page);
13901b3b4a1aSTrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
13911b3b4a1aSTrond Myklebust 	struct writeback_control wbc = {
13921b3b4a1aSTrond Myklebust 		.bdi = page->mapping->backing_dev_info,
13931b3b4a1aSTrond Myklebust 		.sync_mode = WB_SYNC_ALL,
13941b3b4a1aSTrond Myklebust 		.nr_to_write = LONG_MAX,
13951b3b4a1aSTrond Myklebust 		.range_start = range_start,
13961b3b4a1aSTrond Myklebust 		.range_end = range_end,
13971b3b4a1aSTrond Myklebust 	};
13981b3b4a1aSTrond Myklebust 	int ret = 0;
13991b3b4a1aSTrond Myklebust 
14001b3b4a1aSTrond Myklebust 	BUG_ON(!PageLocked(page));
14011b3b4a1aSTrond Myklebust 	for (;;) {
14021b3b4a1aSTrond Myklebust 		req = nfs_page_find_request(page);
14031b3b4a1aSTrond Myklebust 		if (req == NULL)
14041b3b4a1aSTrond Myklebust 			goto out;
14051b3b4a1aSTrond Myklebust 		if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) {
14061b3b4a1aSTrond Myklebust 			nfs_release_request(req);
14071b3b4a1aSTrond Myklebust 			break;
14081b3b4a1aSTrond Myklebust 		}
14091b3b4a1aSTrond Myklebust 		if (nfs_lock_request_dontget(req)) {
14101b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
14111b3b4a1aSTrond Myklebust 			/*
14121b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
14131b3b4a1aSTrond Myklebust 			 * page as being dirty
14141b3b4a1aSTrond Myklebust 			 */
14151b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
14161b3b4a1aSTrond Myklebust 			nfs_unlock_request(req);
14171b3b4a1aSTrond Myklebust 			break;
14181b3b4a1aSTrond Myklebust 		}
14191b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
14201b3b4a1aSTrond Myklebust 		if (ret < 0)
14211b3b4a1aSTrond Myklebust 			goto out;
14221b3b4a1aSTrond Myklebust 	}
14231b3b4a1aSTrond Myklebust 	if (!PagePrivate(page))
14241b3b4a1aSTrond Myklebust 		return 0;
14251b3b4a1aSTrond Myklebust 	ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
14261b3b4a1aSTrond Myklebust out:
14271b3b4a1aSTrond Myklebust 	return ret;
14281b3b4a1aSTrond Myklebust }
14291b3b4a1aSTrond Myklebust 
143061822ab5STrond Myklebust int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
14311c75950bSTrond Myklebust {
14321c75950bSTrond Myklebust 	loff_t range_start = page_offset(page);
14331c75950bSTrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
14344d770ccfSTrond Myklebust 	struct writeback_control wbc = {
14354d770ccfSTrond Myklebust 		.bdi = page->mapping->backing_dev_info,
14364d770ccfSTrond Myklebust 		.sync_mode = WB_SYNC_ALL,
14374d770ccfSTrond Myklebust 		.nr_to_write = LONG_MAX,
14384d770ccfSTrond Myklebust 		.range_start = range_start,
14394d770ccfSTrond Myklebust 		.range_end = range_end,
14404d770ccfSTrond Myklebust 	};
14414d770ccfSTrond Myklebust 	int ret;
14421c75950bSTrond Myklebust 
14434d770ccfSTrond Myklebust 	BUG_ON(!PageLocked(page));
1444c63c7b05STrond Myklebust 	if (clear_page_dirty_for_io(page)) {
14454d770ccfSTrond Myklebust 		ret = nfs_writepage_locked(page, &wbc);
14464d770ccfSTrond Myklebust 		if (ret < 0)
14474d770ccfSTrond Myklebust 			goto out;
14484d770ccfSTrond Myklebust 	}
1449f40313acSTrond Myklebust 	if (!PagePrivate(page))
1450f40313acSTrond Myklebust 		return 0;
14514d770ccfSTrond Myklebust 	ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
14524d770ccfSTrond Myklebust 	if (ret >= 0)
14534d770ccfSTrond Myklebust 		return 0;
14544d770ccfSTrond Myklebust out:
1455e507d9ebSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_PAGES);
14564d770ccfSTrond Myklebust 	return ret;
14571c75950bSTrond Myklebust }
14581c75950bSTrond Myklebust 
14591c75950bSTrond Myklebust /*
14601c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
14611c75950bSTrond Myklebust  */
14621c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page* page)
14631c75950bSTrond Myklebust {
14644d770ccfSTrond Myklebust 	return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
14651c75950bSTrond Myklebust }
14661c75950bSTrond Myklebust 
1467f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
14681da177e4SLinus Torvalds {
14691da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
14701da177e4SLinus Torvalds 					     sizeof(struct nfs_write_data),
14711da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
147220c2df83SPaul Mundt 					     NULL);
14731da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
14741da177e4SLinus Torvalds 		return -ENOMEM;
14751da177e4SLinus Torvalds 
147693d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
14771da177e4SLinus Torvalds 						     nfs_wdata_cachep);
14781da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
14791da177e4SLinus Torvalds 		return -ENOMEM;
14801da177e4SLinus Torvalds 
148193d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
14821da177e4SLinus Torvalds 						      nfs_wdata_cachep);
14831da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
14841da177e4SLinus Torvalds 		return -ENOMEM;
14851da177e4SLinus Torvalds 
148689a09141SPeter Zijlstra 	/*
148789a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
148889a09141SPeter Zijlstra 	 *
148989a09141SPeter Zijlstra 	 *  64MB:    8192k
149089a09141SPeter Zijlstra 	 * 128MB:   11585k
149189a09141SPeter Zijlstra 	 * 256MB:   16384k
149289a09141SPeter Zijlstra 	 * 512MB:   23170k
149389a09141SPeter Zijlstra 	 *   1GB:   32768k
149489a09141SPeter Zijlstra 	 *   2GB:   46340k
149589a09141SPeter Zijlstra 	 *   4GB:   65536k
149689a09141SPeter Zijlstra 	 *   8GB:   92681k
149789a09141SPeter Zijlstra 	 *  16GB:  131072k
149889a09141SPeter Zijlstra 	 *
149989a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
150089a09141SPeter Zijlstra 	 * Limit the default to 256M
150189a09141SPeter Zijlstra 	 */
150289a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
150389a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
150489a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
150589a09141SPeter Zijlstra 
15061da177e4SLinus Torvalds 	return 0;
15071da177e4SLinus Torvalds }
15081da177e4SLinus Torvalds 
1509266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
15101da177e4SLinus Torvalds {
15111da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
15121da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
15131a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
15141da177e4SLinus Torvalds }
15151da177e4SLinus Torvalds 
1516