xref: /openbmc/linux/fs/nfs/write.c (revision d7fb1207)
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  */
37c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc,
38c63c7b05STrond Myklebust 				  struct inode *inode, int ioflags);
39f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req);
40788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops;
41788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops;
42788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops;
431da177e4SLinus Torvalds 
44e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep;
453feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
461da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
471da177e4SLinus Torvalds 
48c9d8f89dSTrond Myklebust struct nfs_write_data *nfs_commitdata_alloc(void)
491da177e4SLinus Torvalds {
50e6b4f8daSChristoph Lameter 	struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
5140859d7eSChuck Lever 
521da177e4SLinus Torvalds 	if (p) {
531da177e4SLinus Torvalds 		memset(p, 0, sizeof(*p));
541da177e4SLinus Torvalds 		INIT_LIST_HEAD(&p->pages);
551da177e4SLinus Torvalds 	}
561da177e4SLinus Torvalds 	return p;
571da177e4SLinus Torvalds }
581da177e4SLinus Torvalds 
595e4424afSTrond Myklebust void nfs_commit_free(struct nfs_write_data *p)
601da177e4SLinus Torvalds {
6140859d7eSChuck Lever 	if (p && (p->pagevec != &p->page_array[0]))
6240859d7eSChuck Lever 		kfree(p->pagevec);
631da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
641da177e4SLinus Torvalds }
651da177e4SLinus Torvalds 
668d5658c9STrond Myklebust struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount)
673feb2d49STrond Myklebust {
68e6b4f8daSChristoph Lameter 	struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
693feb2d49STrond Myklebust 
703feb2d49STrond Myklebust 	if (p) {
713feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
723feb2d49STrond Myklebust 		INIT_LIST_HEAD(&p->pages);
73e9f7bee1STrond Myklebust 		p->npages = pagecount;
740d0b5cb3SChuck Lever 		if (pagecount <= ARRAY_SIZE(p->page_array))
750d0b5cb3SChuck Lever 			p->pagevec = p->page_array;
763feb2d49STrond Myklebust 		else {
770d0b5cb3SChuck Lever 			p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
780d0b5cb3SChuck Lever 			if (!p->pagevec) {
793feb2d49STrond Myklebust 				mempool_free(p, nfs_wdata_mempool);
803feb2d49STrond Myklebust 				p = NULL;
813feb2d49STrond Myklebust 			}
823feb2d49STrond Myklebust 		}
833feb2d49STrond Myklebust 	}
843feb2d49STrond Myklebust 	return p;
853feb2d49STrond Myklebust }
863feb2d49STrond Myklebust 
875e4424afSTrond Myklebust static void nfs_writedata_free(struct nfs_write_data *p)
883feb2d49STrond Myklebust {
893feb2d49STrond Myklebust 	if (p && (p->pagevec != &p->page_array[0]))
903feb2d49STrond Myklebust 		kfree(p->pagevec);
913feb2d49STrond Myklebust 	mempool_free(p, nfs_wdata_mempool);
923feb2d49STrond Myklebust }
933feb2d49STrond Myklebust 
94383ba719STrond Myklebust void nfs_writedata_release(void *data)
951da177e4SLinus Torvalds {
96383ba719STrond Myklebust 	struct nfs_write_data *wdata = data;
97383ba719STrond Myklebust 
98383ba719STrond Myklebust 	put_nfs_open_context(wdata->args.context);
991da177e4SLinus Torvalds 	nfs_writedata_free(wdata);
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds 
1027b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error)
1037b159fc1STrond Myklebust {
1047b159fc1STrond Myklebust 	ctx->error = error;
1057b159fc1STrond Myklebust 	smp_wmb();
1067b159fc1STrond Myklebust 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
1077b159fc1STrond Myklebust }
1087b159fc1STrond Myklebust 
109277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request_locked(struct page *page)
110277459d2STrond Myklebust {
111277459d2STrond Myklebust 	struct nfs_page *req = NULL;
112277459d2STrond Myklebust 
113277459d2STrond Myklebust 	if (PagePrivate(page)) {
114277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
115277459d2STrond Myklebust 		if (req != NULL)
116c03b4024STrond Myklebust 			kref_get(&req->wb_kref);
117277459d2STrond Myklebust 	}
118277459d2STrond Myklebust 	return req;
119277459d2STrond Myklebust }
120277459d2STrond Myklebust 
121277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page)
122277459d2STrond Myklebust {
123587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
124277459d2STrond Myklebust 	struct nfs_page *req = NULL;
125277459d2STrond Myklebust 
126587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
127277459d2STrond Myklebust 	req = nfs_page_find_request_locked(page);
128587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
129277459d2STrond Myklebust 	return req;
130277459d2STrond Myklebust }
131277459d2STrond Myklebust 
1321da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1331da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
136a3d01454STrond Myklebust 	loff_t end, i_size;
137a3d01454STrond Myklebust 	pgoff_t end_index;
1381da177e4SLinus Torvalds 
139a3d01454STrond Myklebust 	spin_lock(&inode->i_lock);
140a3d01454STrond Myklebust 	i_size = i_size_read(inode);
141a3d01454STrond Myklebust 	end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
1421da177e4SLinus Torvalds 	if (i_size > 0 && page->index < end_index)
143a3d01454STrond Myklebust 		goto out;
1441da177e4SLinus Torvalds 	end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
1451da177e4SLinus Torvalds 	if (i_size >= end)
146a3d01454STrond Myklebust 		goto out;
1471da177e4SLinus Torvalds 	i_size_write(inode, end);
148a3d01454STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
149a3d01454STrond Myklebust out:
150a3d01454STrond Myklebust 	spin_unlock(&inode->i_lock);
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;
1711da177e4SLinus Torvalds 	SetPageUptodate(page);
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
1751da177e4SLinus Torvalds {
1761da177e4SLinus Torvalds 	if (wbc->for_reclaim)
177c63c7b05STrond Myklebust 		return FLUSH_HIGHPRI | FLUSH_STABLE;
1781da177e4SLinus Torvalds 	if (wbc->for_kupdate)
1791da177e4SLinus Torvalds 		return FLUSH_LOWPRI;
1801da177e4SLinus Torvalds 	return 0;
1811da177e4SLinus Torvalds }
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds /*
18489a09141SPeter Zijlstra  * NFS congestion control
18589a09141SPeter Zijlstra  */
18689a09141SPeter Zijlstra 
18789a09141SPeter Zijlstra int nfs_congestion_kb;
18889a09141SPeter Zijlstra 
18989a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10))
19089a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH	\
19189a09141SPeter Zijlstra 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
19289a09141SPeter Zijlstra 
1935a6d41b3STrond Myklebust static int nfs_set_page_writeback(struct page *page)
19489a09141SPeter Zijlstra {
1955a6d41b3STrond Myklebust 	int ret = test_set_page_writeback(page);
1965a6d41b3STrond Myklebust 
1975a6d41b3STrond Myklebust 	if (!ret) {
19889a09141SPeter Zijlstra 		struct inode *inode = page->mapping->host;
19989a09141SPeter Zijlstra 		struct nfs_server *nfss = NFS_SERVER(inode);
20089a09141SPeter Zijlstra 
201277866a0SPeter Zijlstra 		if (atomic_long_inc_return(&nfss->writeback) >
20289a09141SPeter Zijlstra 				NFS_CONGESTION_ON_THRESH)
20389a09141SPeter Zijlstra 			set_bdi_congested(&nfss->backing_dev_info, WRITE);
20489a09141SPeter Zijlstra 	}
2055a6d41b3STrond Myklebust 	return ret;
20689a09141SPeter Zijlstra }
20789a09141SPeter Zijlstra 
20889a09141SPeter Zijlstra static void nfs_end_page_writeback(struct page *page)
20989a09141SPeter Zijlstra {
21089a09141SPeter Zijlstra 	struct inode *inode = page->mapping->host;
21189a09141SPeter Zijlstra 	struct nfs_server *nfss = NFS_SERVER(inode);
21289a09141SPeter Zijlstra 
21389a09141SPeter Zijlstra 	end_page_writeback(page);
214c4dc4beeSPeter Zijlstra 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
21589a09141SPeter Zijlstra 		clear_bdi_congested(&nfss->backing_dev_info, WRITE);
21689a09141SPeter Zijlstra }
21789a09141SPeter Zijlstra 
21889a09141SPeter Zijlstra /*
219e261f51fSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
2209cccef95STrond Myklebust  * May return an error if the user signalled nfs_wait_on_request().
221e261f51fSTrond Myklebust  */
222c63c7b05STrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
223c63c7b05STrond Myklebust 				struct page *page)
224e261f51fSTrond Myklebust {
225587142f8STrond Myklebust 	struct inode *inode = page->mapping->host;
226e261f51fSTrond Myklebust 	struct nfs_page *req;
227e261f51fSTrond Myklebust 	int ret;
228e261f51fSTrond Myklebust 
229587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
230e261f51fSTrond Myklebust 	for(;;) {
231e261f51fSTrond Myklebust 		req = nfs_page_find_request_locked(page);
232e261f51fSTrond Myklebust 		if (req == NULL) {
233587142f8STrond Myklebust 			spin_unlock(&inode->i_lock);
2349cccef95STrond Myklebust 			return 0;
235e261f51fSTrond Myklebust 		}
236acee478aSTrond Myklebust 		if (nfs_set_page_tag_locked(req))
237e261f51fSTrond Myklebust 			break;
238e261f51fSTrond Myklebust 		/* Note: If we hold the page lock, as is the case in nfs_writepage,
239acee478aSTrond Myklebust 		 *	 then the call to nfs_set_page_tag_locked() will always
240e261f51fSTrond Myklebust 		 *	 succeed provided that someone hasn't already marked the
241e261f51fSTrond Myklebust 		 *	 request as dirty (in which case we don't care).
242e261f51fSTrond Myklebust 		 */
243587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
244e261f51fSTrond Myklebust 		ret = nfs_wait_on_request(req);
245e261f51fSTrond Myklebust 		nfs_release_request(req);
246e261f51fSTrond Myklebust 		if (ret != 0)
247e261f51fSTrond Myklebust 			return ret;
248587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
249e261f51fSTrond Myklebust 	}
250e468bae9STrond Myklebust 	if (test_bit(PG_CLEAN, &req->wb_flags)) {
251587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
252e468bae9STrond Myklebust 		BUG();
253612c9384STrond Myklebust 	}
254c63c7b05STrond Myklebust 	if (nfs_set_page_writeback(page) != 0) {
255587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
256c63c7b05STrond Myklebust 		BUG();
257c63c7b05STrond Myklebust 	}
258587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
259f8512ad0SFred Isaman 	if (!nfs_pageio_add_request(pgio, req)) {
260f8512ad0SFred Isaman 		nfs_redirty_request(req);
261f8512ad0SFred Isaman 		return pgio->pg_error;
262f8512ad0SFred Isaman 	}
2639cccef95STrond Myklebust 	return 0;
264e261f51fSTrond Myklebust }
265e261f51fSTrond Myklebust 
266f758c885STrond Myklebust static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio)
267f758c885STrond Myklebust {
268f758c885STrond Myklebust 	struct inode *inode = page->mapping->host;
269f758c885STrond Myklebust 
270f758c885STrond Myklebust 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
271f758c885STrond Myklebust 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
272f758c885STrond Myklebust 
273f758c885STrond Myklebust 	nfs_pageio_cond_complete(pgio, page->index);
274f758c885STrond Myklebust 	return nfs_page_async_flush(pgio, page);
275f758c885STrond Myklebust }
276f758c885STrond Myklebust 
277e261f51fSTrond Myklebust /*
2781da177e4SLinus Torvalds  * Write an mmapped page to the server.
2791da177e4SLinus Torvalds  */
2804d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
2811da177e4SLinus Torvalds {
282f758c885STrond Myklebust 	struct nfs_pageio_descriptor pgio;
283e261f51fSTrond Myklebust 	int err;
2841da177e4SLinus Torvalds 
285f758c885STrond Myklebust 	nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc));
286f758c885STrond Myklebust 	err = nfs_do_writepage(page, wbc, &pgio);
287f758c885STrond Myklebust 	nfs_pageio_complete(&pgio);
288f758c885STrond Myklebust 	if (err < 0)
2894d770ccfSTrond Myklebust 		return err;
290f758c885STrond Myklebust 	if (pgio.pg_error < 0)
291f758c885STrond Myklebust 		return pgio.pg_error;
292f758c885STrond Myklebust 	return 0;
2934d770ccfSTrond Myklebust }
2944d770ccfSTrond Myklebust 
2954d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
2964d770ccfSTrond Myklebust {
297f758c885STrond Myklebust 	int ret;
2984d770ccfSTrond Myklebust 
299f758c885STrond Myklebust 	ret = nfs_writepage_locked(page, wbc);
3001da177e4SLinus Torvalds 	unlock_page(page);
301f758c885STrond Myklebust 	return ret;
302f758c885STrond Myklebust }
303f758c885STrond Myklebust 
304f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
305f758c885STrond Myklebust {
306f758c885STrond Myklebust 	int ret;
307f758c885STrond Myklebust 
308f758c885STrond Myklebust 	ret = nfs_do_writepage(page, wbc, data);
309f758c885STrond Myklebust 	unlock_page(page);
310f758c885STrond Myklebust 	return ret;
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3141da177e4SLinus Torvalds {
3151da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
316c63c7b05STrond Myklebust 	struct nfs_pageio_descriptor pgio;
3171da177e4SLinus Torvalds 	int err;
3181da177e4SLinus Torvalds 
31991d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
32091d5b470SChuck Lever 
321c63c7b05STrond Myklebust 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc));
322f758c885STrond Myklebust 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
323c63c7b05STrond Myklebust 	nfs_pageio_complete(&pgio);
324f758c885STrond Myklebust 	if (err < 0)
3251da177e4SLinus Torvalds 		return err;
326f758c885STrond Myklebust 	if (pgio.pg_error < 0)
327c63c7b05STrond Myklebust 		return pgio.pg_error;
328c63c7b05STrond Myklebust 	return 0;
3291da177e4SLinus Torvalds }
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds /*
3321da177e4SLinus Torvalds  * Insert a write request into an inode
3331da177e4SLinus Torvalds  */
334e7d39069STrond Myklebust static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
3351da177e4SLinus Torvalds {
3361da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
3371da177e4SLinus Torvalds 	int error;
3381da177e4SLinus Torvalds 
339e7d39069STrond Myklebust 	error = radix_tree_preload(GFP_NOFS);
340e7d39069STrond Myklebust 	if (error != 0)
341e7d39069STrond Myklebust 		goto out;
342e7d39069STrond Myklebust 
343e7d39069STrond Myklebust 	/* Lock the request! */
344e7d39069STrond Myklebust 	nfs_lock_request_dontget(req);
345e7d39069STrond Myklebust 
346e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
3471da177e4SLinus Torvalds 	error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
34827852596SNick Piggin 	BUG_ON(error);
3491da177e4SLinus Torvalds 	if (!nfsi->npages) {
3501da177e4SLinus Torvalds 		igrab(inode);
3511da177e4SLinus Torvalds 		if (nfs_have_delegation(inode, FMODE_WRITE))
3521da177e4SLinus Torvalds 			nfsi->change_attr++;
3531da177e4SLinus Torvalds 	}
354deb7d638STrond Myklebust 	SetPagePrivate(req->wb_page);
355277459d2STrond Myklebust 	set_page_private(req->wb_page, (unsigned long)req);
3561da177e4SLinus Torvalds 	nfsi->npages++;
357c03b4024STrond Myklebust 	kref_get(&req->wb_kref);
35827852596SNick Piggin 	radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
35927852596SNick Piggin 				NFS_PAGE_TAG_LOCKED);
360e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
361e7d39069STrond Myklebust 	radix_tree_preload_end();
362e7d39069STrond Myklebust out:
363e7d39069STrond Myklebust 	return error;
3641da177e4SLinus Torvalds }
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds /*
36789a09141SPeter Zijlstra  * Remove a write request from an inode
3681da177e4SLinus Torvalds  */
3691da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
3701da177e4SLinus Torvalds {
37188be9f99STrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
3721da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	BUG_ON (!NFS_WBACK_BUSY(req));
3751da177e4SLinus Torvalds 
376587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
377277459d2STrond Myklebust 	set_page_private(req->wb_page, 0);
378deb7d638STrond Myklebust 	ClearPagePrivate(req->wb_page);
3791da177e4SLinus Torvalds 	radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
3801da177e4SLinus Torvalds 	nfsi->npages--;
3811da177e4SLinus Torvalds 	if (!nfsi->npages) {
382587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
3831da177e4SLinus Torvalds 		iput(inode);
3841da177e4SLinus Torvalds 	} else
385587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
3861da177e4SLinus Torvalds 	nfs_clear_request(req);
3871da177e4SLinus Torvalds 	nfs_release_request(req);
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
39061822ab5STrond Myklebust static void
3916d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req)
39261822ab5STrond Myklebust {
39361822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
39461822ab5STrond Myklebust }
39561822ab5STrond Myklebust 
3961da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
3971da177e4SLinus Torvalds /*
3981da177e4SLinus Torvalds  * Add a request to the inode's commit list.
3991da177e4SLinus Torvalds  */
4001da177e4SLinus Torvalds static void
4011da177e4SLinus Torvalds nfs_mark_request_commit(struct nfs_page *req)
4021da177e4SLinus Torvalds {
40388be9f99STrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
4041da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4051da177e4SLinus Torvalds 
406587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
4071da177e4SLinus Torvalds 	nfsi->ncommit++;
408e468bae9STrond Myklebust 	set_bit(PG_CLEAN, &(req)->wb_flags);
4095c369683STrond Myklebust 	radix_tree_tag_set(&nfsi->nfs_page_tree,
4105c369683STrond Myklebust 			req->wb_index,
4115c369683STrond Myklebust 			NFS_PAGE_TAG_COMMIT);
412587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
413fd39fc85SChristoph Lameter 	inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
414c9e51e41SPeter Zijlstra 	inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE);
415a1803044STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
4161da177e4SLinus Torvalds }
4178e821cadSTrond Myklebust 
418e468bae9STrond Myklebust static int
419e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
420e468bae9STrond Myklebust {
421e468bae9STrond Myklebust 	struct page *page = req->wb_page;
422e468bae9STrond Myklebust 
423e468bae9STrond Myklebust 	if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) {
424e468bae9STrond Myklebust 		dec_zone_page_state(page, NR_UNSTABLE_NFS);
425e468bae9STrond Myklebust 		dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
426e468bae9STrond Myklebust 		return 1;
427e468bae9STrond Myklebust 	}
428e468bae9STrond Myklebust 	return 0;
429e468bae9STrond Myklebust }
430e468bae9STrond Myklebust 
4318e821cadSTrond Myklebust static inline
4328e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
4338e821cadSTrond Myklebust {
4348e821cadSTrond Myklebust 	return data->verf.committed != NFS_FILE_SYNC;
4358e821cadSTrond Myklebust }
4368e821cadSTrond Myklebust 
4378e821cadSTrond Myklebust static inline
4388e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req)
4398e821cadSTrond Myklebust {
440e468bae9STrond Myklebust 	if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) {
4418e821cadSTrond Myklebust 		nfs_mark_request_commit(req);
4428e821cadSTrond Myklebust 		return 1;
4438e821cadSTrond Myklebust 	}
4448e821cadSTrond Myklebust 	if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) {
4456d884e8fSFred 		nfs_mark_request_dirty(req);
4468e821cadSTrond Myklebust 		return 1;
4478e821cadSTrond Myklebust 	}
4488e821cadSTrond Myklebust 	return 0;
4498e821cadSTrond Myklebust }
4508e821cadSTrond Myklebust #else
4518e821cadSTrond Myklebust static inline void
4528e821cadSTrond Myklebust nfs_mark_request_commit(struct nfs_page *req)
4538e821cadSTrond Myklebust {
4548e821cadSTrond Myklebust }
4558e821cadSTrond Myklebust 
456e468bae9STrond Myklebust static inline int
457e468bae9STrond Myklebust nfs_clear_request_commit(struct nfs_page *req)
458e468bae9STrond Myklebust {
459e468bae9STrond Myklebust 	return 0;
460e468bae9STrond Myklebust }
461e468bae9STrond Myklebust 
4628e821cadSTrond Myklebust static inline
4638e821cadSTrond Myklebust int nfs_write_need_commit(struct nfs_write_data *data)
4648e821cadSTrond Myklebust {
4658e821cadSTrond Myklebust 	return 0;
4668e821cadSTrond Myklebust }
4678e821cadSTrond Myklebust 
4688e821cadSTrond Myklebust static inline
4698e821cadSTrond Myklebust int nfs_reschedule_unstable_write(struct nfs_page *req)
4708e821cadSTrond Myklebust {
4718e821cadSTrond Myklebust 	return 0;
4728e821cadSTrond Myklebust }
4731da177e4SLinus Torvalds #endif
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds /*
4761da177e4SLinus Torvalds  * Wait for a request to complete.
4771da177e4SLinus Torvalds  *
478150030b7SMatthew Wilcox  * Interruptible by fatal signals only.
4791da177e4SLinus Torvalds  */
480ca52fec1STrond Myklebust static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages)
4811da177e4SLinus Torvalds {
4821da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4831da177e4SLinus Torvalds 	struct nfs_page *req;
484ca52fec1STrond Myklebust 	pgoff_t idx_end, next;
4851da177e4SLinus Torvalds 	unsigned int		res = 0;
4861da177e4SLinus Torvalds 	int			error;
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	if (npages == 0)
4891da177e4SLinus Torvalds 		idx_end = ~0;
4901da177e4SLinus Torvalds 	else
4911da177e4SLinus Torvalds 		idx_end = idx_start + npages - 1;
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 	next = idx_start;
4949fd367f0STrond Myklebust 	while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) {
4951da177e4SLinus Torvalds 		if (req->wb_index > idx_end)
4961da177e4SLinus Torvalds 			break;
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 		next = req->wb_index + 1;
499c6a556b8STrond Myklebust 		BUG_ON(!NFS_WBACK_BUSY(req));
5001da177e4SLinus Torvalds 
501c03b4024STrond Myklebust 		kref_get(&req->wb_kref);
502587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
5031da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
5041da177e4SLinus Torvalds 		nfs_release_request(req);
505587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
5061da177e4SLinus Torvalds 		if (error < 0)
5071da177e4SLinus Torvalds 			return error;
5081da177e4SLinus Torvalds 		res++;
5091da177e4SLinus Torvalds 	}
5101da177e4SLinus Torvalds 	return res;
5111da177e4SLinus Torvalds }
5121da177e4SLinus Torvalds 
51383715ad5STrond Myklebust static void nfs_cancel_commit_list(struct list_head *head)
51483715ad5STrond Myklebust {
51583715ad5STrond Myklebust 	struct nfs_page *req;
51683715ad5STrond Myklebust 
51783715ad5STrond Myklebust 	while(!list_empty(head)) {
51883715ad5STrond Myklebust 		req = nfs_list_entry(head->next);
51983715ad5STrond Myklebust 		nfs_list_remove_request(req);
520e468bae9STrond Myklebust 		nfs_clear_request_commit(req);
52183715ad5STrond Myklebust 		nfs_inode_remove_request(req);
522b6dff26aSTrond Myklebust 		nfs_unlock_request(req);
52383715ad5STrond Myklebust 	}
52483715ad5STrond Myklebust }
52583715ad5STrond Myklebust 
5261da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
5271da177e4SLinus Torvalds /*
5281da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
5291da177e4SLinus Torvalds  * @inode: NFS inode to scan
5301da177e4SLinus Torvalds  * @dst: destination list
5311da177e4SLinus Torvalds  * @idx_start: lower bound of page->index to scan.
5321da177e4SLinus Torvalds  * @npages: idx_start + npages sets the upper bound to scan.
5331da177e4SLinus Torvalds  *
5341da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
5351da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
5361da177e4SLinus Torvalds  */
5371da177e4SLinus Torvalds static int
538ca52fec1STrond Myklebust nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
5391da177e4SLinus Torvalds {
5401da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
5413da28eb1STrond Myklebust 	int res = 0;
5423da28eb1STrond Myklebust 
5433da28eb1STrond Myklebust 	if (nfsi->ncommit != 0) {
5445c369683STrond Myklebust 		res = nfs_scan_list(nfsi, dst, idx_start, npages,
5455c369683STrond Myklebust 				NFS_PAGE_TAG_COMMIT);
5461da177e4SLinus Torvalds 		nfsi->ncommit -= res;
5473da28eb1STrond Myklebust 	}
5481da177e4SLinus Torvalds 	return res;
5491da177e4SLinus Torvalds }
550c42de9ddSTrond Myklebust #else
551ca52fec1STrond Myklebust static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages)
552c42de9ddSTrond Myklebust {
553c42de9ddSTrond Myklebust 	return 0;
554c42de9ddSTrond Myklebust }
5551da177e4SLinus Torvalds #endif
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds /*
558e7d39069STrond Myklebust  * Search for an existing write request, and attempt to update
559e7d39069STrond Myklebust  * it to reflect a new dirty region on a given page.
5601da177e4SLinus Torvalds  *
561e7d39069STrond Myklebust  * If the attempt fails, then the existing request is flushed out
562e7d39069STrond Myklebust  * to disk.
5631da177e4SLinus Torvalds  */
564e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
565e7d39069STrond Myklebust 		struct page *page,
566e7d39069STrond Myklebust 		unsigned int offset,
567e7d39069STrond Myklebust 		unsigned int bytes)
5681da177e4SLinus Torvalds {
569e7d39069STrond Myklebust 	struct nfs_page *req;
570e7d39069STrond Myklebust 	unsigned int rqend;
571e7d39069STrond Myklebust 	unsigned int end;
5721da177e4SLinus Torvalds 	int error;
573277459d2STrond Myklebust 
574e7d39069STrond Myklebust 	if (!PagePrivate(page))
575e7d39069STrond Myklebust 		return NULL;
576e7d39069STrond Myklebust 
577e7d39069STrond Myklebust 	end = offset + bytes;
578e7d39069STrond Myklebust 	spin_lock(&inode->i_lock);
579e7d39069STrond Myklebust 
580e7d39069STrond Myklebust 	for (;;) {
581e7d39069STrond Myklebust 		req = nfs_page_find_request_locked(page);
582e7d39069STrond Myklebust 		if (req == NULL)
583e7d39069STrond Myklebust 			goto out_unlock;
584e7d39069STrond Myklebust 
585e7d39069STrond Myklebust 		rqend = req->wb_offset + req->wb_bytes;
586e7d39069STrond Myklebust 		/*
587e7d39069STrond Myklebust 		 * Tell the caller to flush out the request if
588e7d39069STrond Myklebust 		 * the offsets are non-contiguous.
589e7d39069STrond Myklebust 		 * Note: nfs_flush_incompatible() will already
590e7d39069STrond Myklebust 		 * have flushed out requests having wrong owners.
591e7d39069STrond Myklebust 		 */
592e468bae9STrond Myklebust 		if (offset > rqend
593e7d39069STrond Myklebust 		    || end < req->wb_offset)
594e7d39069STrond Myklebust 			goto out_flushme;
595e7d39069STrond Myklebust 
596e7d39069STrond Myklebust 		if (nfs_set_page_tag_locked(req))
597e7d39069STrond Myklebust 			break;
598e7d39069STrond Myklebust 
599e7d39069STrond Myklebust 		/* The request is locked, so wait and then retry */
600587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
6011da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
6021da177e4SLinus Torvalds 		nfs_release_request(req);
603e7d39069STrond Myklebust 		if (error != 0)
604e7d39069STrond Myklebust 			goto out_err;
605e7d39069STrond Myklebust 		spin_lock(&inode->i_lock);
6061da177e4SLinus Torvalds 	}
6071da177e4SLinus Torvalds 
608e468bae9STrond Myklebust 	if (nfs_clear_request_commit(req))
609e468bae9STrond Myklebust 		radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree,
610e468bae9STrond Myklebust 				req->wb_index, NFS_PAGE_TAG_COMMIT);
611e468bae9STrond Myklebust 
6121da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
6131da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
6141da177e4SLinus Torvalds 		req->wb_offset = offset;
6151da177e4SLinus Torvalds 		req->wb_pgbase = offset;
6161da177e4SLinus Torvalds 	}
6171da177e4SLinus Torvalds 	if (end > rqend)
6181da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
619e7d39069STrond Myklebust 	else
620e7d39069STrond Myklebust 		req->wb_bytes = rqend - req->wb_offset;
621e7d39069STrond Myklebust out_unlock:
622e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
623e7d39069STrond Myklebust 	return req;
624e7d39069STrond Myklebust out_flushme:
625e7d39069STrond Myklebust 	spin_unlock(&inode->i_lock);
626e7d39069STrond Myklebust 	nfs_release_request(req);
627e7d39069STrond Myklebust 	error = nfs_wb_page(inode, page);
628e7d39069STrond Myklebust out_err:
629e7d39069STrond Myklebust 	return ERR_PTR(error);
630e7d39069STrond Myklebust }
6311da177e4SLinus Torvalds 
632e7d39069STrond Myklebust /*
633e7d39069STrond Myklebust  * Try to update an existing write request, or create one if there is none.
634e7d39069STrond Myklebust  *
635e7d39069STrond Myklebust  * Note: Should always be called with the Page Lock held to prevent races
636e7d39069STrond Myklebust  * if we have to add a new request. Also assumes that the caller has
637e7d39069STrond Myklebust  * already called nfs_flush_incompatible() if necessary.
638e7d39069STrond Myklebust  */
639e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
640e7d39069STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
641e7d39069STrond Myklebust {
642e7d39069STrond Myklebust 	struct inode *inode = page->mapping->host;
643e7d39069STrond Myklebust 	struct nfs_page	*req;
644e7d39069STrond Myklebust 	int error;
645e7d39069STrond Myklebust 
646e7d39069STrond Myklebust 	req = nfs_try_to_update_request(inode, page, offset, bytes);
647e7d39069STrond Myklebust 	if (req != NULL)
648e7d39069STrond Myklebust 		goto out;
649e7d39069STrond Myklebust 	req = nfs_create_request(ctx, inode, page, offset, bytes);
650e7d39069STrond Myklebust 	if (IS_ERR(req))
651e7d39069STrond Myklebust 		goto out;
652e7d39069STrond Myklebust 	error = nfs_inode_add_request(inode, req);
653e7d39069STrond Myklebust 	if (error != 0) {
654e7d39069STrond Myklebust 		nfs_release_request(req);
655e7d39069STrond Myklebust 		req = ERR_PTR(error);
656e7d39069STrond Myklebust 	}
657efc91ed0STrond Myklebust out:
65861e930a9STrond Myklebust 	return req;
6591da177e4SLinus Torvalds }
6601da177e4SLinus Torvalds 
661e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
662e7d39069STrond Myklebust 		unsigned int offset, unsigned int count)
663e7d39069STrond Myklebust {
664e7d39069STrond Myklebust 	struct nfs_page	*req;
665e7d39069STrond Myklebust 
666e7d39069STrond Myklebust 	req = nfs_setup_write_request(ctx, page, offset, count);
667e7d39069STrond Myklebust 	if (IS_ERR(req))
668e7d39069STrond Myklebust 		return PTR_ERR(req);
669e7d39069STrond Myklebust 	/* Update file length */
670e7d39069STrond Myklebust 	nfs_grow_file(page, offset, count);
671e7d39069STrond Myklebust 	nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
672e7d39069STrond Myklebust 	nfs_clear_page_tag_locked(req);
673e7d39069STrond Myklebust 	return 0;
674e7d39069STrond Myklebust }
675e7d39069STrond Myklebust 
6761da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
6771da177e4SLinus Torvalds {
678cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
6791da177e4SLinus Torvalds 	struct nfs_page	*req;
6801a54533eSTrond Myklebust 	int do_flush, status;
6811da177e4SLinus Torvalds 	/*
6821da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
6831da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
6841da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
6851da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
6861da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
6871da177e4SLinus Torvalds 	 * dropped page.
6881da177e4SLinus Torvalds 	 */
6891a54533eSTrond Myklebust 	do {
690277459d2STrond Myklebust 		req = nfs_page_find_request(page);
6911a54533eSTrond Myklebust 		if (req == NULL)
6921a54533eSTrond Myklebust 			return 0;
693e468bae9STrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx;
6941da177e4SLinus Torvalds 		nfs_release_request(req);
6951a54533eSTrond Myklebust 		if (!do_flush)
6961a54533eSTrond Myklebust 			return 0;
697277459d2STrond Myklebust 		status = nfs_wb_page(page->mapping->host, page);
6981a54533eSTrond Myklebust 	} while (status == 0);
6991a54533eSTrond Myklebust 	return status;
7001da177e4SLinus Torvalds }
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds /*
7035d47a356STrond Myklebust  * If the page cache is marked as unsafe or invalid, then we can't rely on
7045d47a356STrond Myklebust  * the PageUptodate() flag. In this case, we will need to turn off
7055d47a356STrond Myklebust  * write optimisations that depend on the page contents being correct.
7065d47a356STrond Myklebust  */
7075d47a356STrond Myklebust static int nfs_write_pageuptodate(struct page *page, struct inode *inode)
7085d47a356STrond Myklebust {
7095d47a356STrond Myklebust 	return PageUptodate(page) &&
7105d47a356STrond Myklebust 		!(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA));
7115d47a356STrond Myklebust }
7125d47a356STrond Myklebust 
7135d47a356STrond Myklebust /*
7141da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
7151da177e4SLinus Torvalds  *
7161da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
7171da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
7181da177e4SLinus Torvalds  */
7191da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
7201da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
7211da177e4SLinus Torvalds {
722cd3758e3STrond Myklebust 	struct nfs_open_context *ctx = nfs_file_open_context(file);
7231da177e4SLinus Torvalds 	struct inode	*inode = page->mapping->host;
7241da177e4SLinus Torvalds 	int		status = 0;
7251da177e4SLinus Torvalds 
72691d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
72791d5b470SChuck Lever 
72848186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage(%s/%s %d@%lld)\n",
72901cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
73001cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name, count,
7310bbacc40SChuck Lever 		(long long)(page_offset(page) + offset));
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 	/* If we're not using byte range locks, and we know the page
7345d47a356STrond Myklebust 	 * is up to date, it may be more efficient to extend the write
7355d47a356STrond Myklebust 	 * to cover the entire page in order to avoid fragmentation
7365d47a356STrond Myklebust 	 * inefficiencies.
7371da177e4SLinus Torvalds 	 */
7385d47a356STrond Myklebust 	if (nfs_write_pageuptodate(page, inode) &&
7395d47a356STrond Myklebust 			inode->i_flock == NULL &&
7404b5621f6STrond Myklebust 			!(file->f_flags & O_SYNC)) {
74149a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
7421da177e4SLinus Torvalds 		offset = 0;
7431da177e4SLinus Torvalds 	}
7441da177e4SLinus Torvalds 
745e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
74603fa9e84STrond Myklebust 	if (status < 0)
74703fa9e84STrond Myklebust 		nfs_set_pageerror(page);
74803fa9e84STrond Myklebust 	else
749e261f51fSTrond Myklebust 		__set_page_dirty_nobuffers(page);
7501da177e4SLinus Torvalds 
75148186c7dSChuck Lever 	dprintk("NFS:       nfs_updatepage returns %d (isize %lld)\n",
7521da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
7531da177e4SLinus Torvalds 	return status;
7541da177e4SLinus Torvalds }
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds static void nfs_writepage_release(struct nfs_page *req)
7571da177e4SLinus Torvalds {
7588e821cadSTrond Myklebust 
7597e5f6146STrond Myklebust 	if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req)) {
76089a09141SPeter Zijlstra 		nfs_end_page_writeback(req->wb_page);
7611da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
7628e821cadSTrond Myklebust 	} else
7638e821cadSTrond Myklebust 		nfs_end_page_writeback(req->wb_page);
7649fd367f0STrond Myklebust 	nfs_clear_page_tag_locked(req);
7651da177e4SLinus Torvalds }
7661da177e4SLinus Torvalds 
7673ff7576dSTrond Myklebust static int flush_task_priority(int how)
7681da177e4SLinus Torvalds {
7691da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
7701da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
7711da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
7721da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
7731da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
7741da177e4SLinus Torvalds 	}
7751da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
7761da177e4SLinus Torvalds }
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds /*
7791da177e4SLinus Torvalds  * Set up the argument/result storage required for the RPC call.
7801da177e4SLinus Torvalds  */
781dbae4c73STrond Myklebust static int nfs_write_rpcsetup(struct nfs_page *req,
7821da177e4SLinus Torvalds 		struct nfs_write_data *data,
783788e7a89STrond Myklebust 		const struct rpc_call_ops *call_ops,
7841da177e4SLinus Torvalds 		unsigned int count, unsigned int offset,
7851da177e4SLinus Torvalds 		int how)
7861da177e4SLinus Torvalds {
78784115e1cSTrond Myklebust 	struct inode *inode = req->wb_context->path.dentry->d_inode;
78884115e1cSTrond Myklebust 	int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
7893ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
79007737691STrond Myklebust 	struct rpc_task *task;
791bdc7f021STrond Myklebust 	struct rpc_message msg = {
792bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
793bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
794bdc7f021STrond Myklebust 		.rpc_cred = req->wb_context->cred,
795bdc7f021STrond Myklebust 	};
79684115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
79784115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(inode),
79807737691STrond Myklebust 		.task = &data->task,
799bdc7f021STrond Myklebust 		.rpc_message = &msg,
80084115e1cSTrond Myklebust 		.callback_ops = call_ops,
80184115e1cSTrond Myklebust 		.callback_data = data,
802101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
80384115e1cSTrond Myklebust 		.flags = flags,
8043ff7576dSTrond Myklebust 		.priority = priority,
80584115e1cSTrond Myklebust 	};
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
8081da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	data->req = req;
81188be9f99STrond Myklebust 	data->inode = inode = req->wb_context->path.dentry->d_inode;
812bdc7f021STrond Myklebust 	data->cred = msg.rpc_cred;
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(inode);
8151da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
8161da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
8171da177e4SLinus Torvalds 	data->args.pages  = data->pagevec;
8181da177e4SLinus Torvalds 	data->args.count  = count;
819383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(req->wb_context);
820bdc7f021STrond Myklebust 	data->args.stable  = NFS_UNSTABLE;
821bdc7f021STrond Myklebust 	if (how & FLUSH_STABLE) {
822bdc7f021STrond Myklebust 		data->args.stable = NFS_DATA_SYNC;
823bdc7f021STrond Myklebust 		if (!NFS_I(inode)->ncommit)
824bdc7f021STrond Myklebust 			data->args.stable = NFS_FILE_SYNC;
825bdc7f021STrond Myklebust 	}
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
8281da177e4SLinus Torvalds 	data->res.count   = count;
8291da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
8300e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
8311da177e4SLinus Torvalds 
832788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
833bdc7f021STrond Myklebust 	NFS_PROTO(inode)->write_setup(data, &msg);
8341da177e4SLinus Torvalds 
835a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated write call "
83648186c7dSChuck Lever 		"(req %s/%lld, %u bytes @ offset %llu)\n",
8370bbacc40SChuck Lever 		data->task.tk_pid,
8381da177e4SLinus Torvalds 		inode->i_sb->s_id,
8391da177e4SLinus Torvalds 		(long long)NFS_FILEID(inode),
8401da177e4SLinus Torvalds 		count,
8411da177e4SLinus Torvalds 		(unsigned long long)data->args.offset);
8421da177e4SLinus Torvalds 
84307737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
844dbae4c73STrond Myklebust 	if (IS_ERR(task))
845dbae4c73STrond Myklebust 		return PTR_ERR(task);
84607737691STrond Myklebust 	rpc_put_task(task);
847dbae4c73STrond Myklebust 	return 0;
8481da177e4SLinus Torvalds }
8491da177e4SLinus Torvalds 
8506d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and
8516d884e8fSFred  * call this on each, which will prepare them to be retried on next
8526d884e8fSFred  * writeback using standard nfs.
8536d884e8fSFred  */
8546d884e8fSFred static void nfs_redirty_request(struct nfs_page *req)
8556d884e8fSFred {
8566d884e8fSFred 	nfs_mark_request_dirty(req);
8576d884e8fSFred 	nfs_end_page_writeback(req->wb_page);
8586d884e8fSFred 	nfs_clear_page_tag_locked(req);
8596d884e8fSFred }
8606d884e8fSFred 
8611da177e4SLinus Torvalds /*
8621da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
8631da177e4SLinus Torvalds  * contiguous dirty area on one page.
8641da177e4SLinus Torvalds  */
8658d5658c9STrond Myklebust static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
8661da177e4SLinus Torvalds {
8671da177e4SLinus Torvalds 	struct nfs_page *req = nfs_list_entry(head->next);
8681da177e4SLinus Torvalds 	struct page *page = req->wb_page;
8691da177e4SLinus Torvalds 	struct nfs_write_data *data;
870e9f7bee1STrond Myklebust 	size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
871e9f7bee1STrond Myklebust 	unsigned int offset;
8721da177e4SLinus Torvalds 	int requests = 0;
873dbae4c73STrond Myklebust 	int ret = 0;
8741da177e4SLinus Torvalds 	LIST_HEAD(list);
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	nfs_list_remove_request(req);
8771da177e4SLinus Torvalds 
878bcb71bbaSTrond Myklebust 	nbytes = count;
879e9f7bee1STrond Myklebust 	do {
880e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
881e9f7bee1STrond Myklebust 
8828d5658c9STrond Myklebust 		data = nfs_writedata_alloc(1);
8831da177e4SLinus Torvalds 		if (!data)
8841da177e4SLinus Torvalds 			goto out_bad;
8851da177e4SLinus Torvalds 		list_add(&data->pages, &list);
8861da177e4SLinus Torvalds 		requests++;
887e9f7bee1STrond Myklebust 		nbytes -= len;
888e9f7bee1STrond Myklebust 	} while (nbytes != 0);
8891da177e4SLinus Torvalds 	atomic_set(&req->wb_complete, requests);
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	ClearPageError(page);
8921da177e4SLinus Torvalds 	offset = 0;
893bcb71bbaSTrond Myklebust 	nbytes = count;
8941da177e4SLinus Torvalds 	do {
895dbae4c73STrond Myklebust 		int ret2;
896dbae4c73STrond Myklebust 
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 
902bcb71bbaSTrond Myklebust 		if (nbytes < wsize)
903bcb71bbaSTrond Myklebust 			wsize = nbytes;
904dbae4c73STrond Myklebust 		ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
905788e7a89STrond Myklebust 				   wsize, offset, how);
906dbae4c73STrond Myklebust 		if (ret == 0)
907dbae4c73STrond Myklebust 			ret = ret2;
9081da177e4SLinus Torvalds 		offset += wsize;
9091da177e4SLinus Torvalds 		nbytes -= wsize;
9101da177e4SLinus Torvalds 	} while (nbytes != 0);
9111da177e4SLinus Torvalds 
912dbae4c73STrond Myklebust 	return ret;
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds out_bad:
9151da177e4SLinus Torvalds 	while (!list_empty(&list)) {
9161da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
9171da177e4SLinus Torvalds 		list_del(&data->pages);
9188aca67f0STrond Myklebust 		nfs_writedata_release(data);
9191da177e4SLinus Torvalds 	}
92061822ab5STrond Myklebust 	nfs_redirty_request(req);
9211da177e4SLinus Torvalds 	return -ENOMEM;
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds /*
9251da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
9261da177e4SLinus Torvalds  * The page must have been locked by the caller.
9271da177e4SLinus Torvalds  *
9281da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
9291da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
9301da177e4SLinus Torvalds  * that has been written but not committed.
9311da177e4SLinus Torvalds  */
9328d5658c9STrond Myklebust static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how)
9331da177e4SLinus Torvalds {
9341da177e4SLinus Torvalds 	struct nfs_page		*req;
9351da177e4SLinus Torvalds 	struct page		**pages;
9361da177e4SLinus Torvalds 	struct nfs_write_data	*data;
9371da177e4SLinus Torvalds 
9388d5658c9STrond Myklebust 	data = nfs_writedata_alloc(npages);
9391da177e4SLinus Torvalds 	if (!data)
9401da177e4SLinus Torvalds 		goto out_bad;
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 	pages = data->pagevec;
9431da177e4SLinus Torvalds 	while (!list_empty(head)) {
9441da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
9451da177e4SLinus Torvalds 		nfs_list_remove_request(req);
9461da177e4SLinus Torvalds 		nfs_list_add_request(req, &data->pages);
9471da177e4SLinus Torvalds 		ClearPageError(req->wb_page);
9481da177e4SLinus Torvalds 		*pages++ = req->wb_page;
9491da177e4SLinus Torvalds 	}
9501da177e4SLinus Torvalds 	req = nfs_list_entry(data->pages.next);
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 	/* Set up the argument struct */
953dbae4c73STrond Myklebust 	return nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
9541da177e4SLinus Torvalds  out_bad:
9551da177e4SLinus Torvalds 	while (!list_empty(head)) {
95610afec90STrond Myklebust 		req = nfs_list_entry(head->next);
9571da177e4SLinus Torvalds 		nfs_list_remove_request(req);
95861822ab5STrond Myklebust 		nfs_redirty_request(req);
9591da177e4SLinus Torvalds 	}
9601da177e4SLinus Torvalds 	return -ENOMEM;
9611da177e4SLinus Torvalds }
9621da177e4SLinus Torvalds 
963c63c7b05STrond Myklebust static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
964c63c7b05STrond Myklebust 				  struct inode *inode, int ioflags)
9651da177e4SLinus Torvalds {
966bf4285e7SChuck Lever 	size_t wsize = NFS_SERVER(inode)->wsize;
9671da177e4SLinus Torvalds 
968bcb71bbaSTrond Myklebust 	if (wsize < PAGE_CACHE_SIZE)
969c63c7b05STrond Myklebust 		nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags);
970bcb71bbaSTrond Myklebust 	else
971c63c7b05STrond Myklebust 		nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags);
9721da177e4SLinus Torvalds }
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds /*
9751da177e4SLinus Torvalds  * Handle a write reply that flushed part of a page.
9761da177e4SLinus Torvalds  */
977788e7a89STrond Myklebust static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
9781da177e4SLinus Torvalds {
979788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
9801da177e4SLinus Torvalds 
98148186c7dSChuck Lever 	dprintk("NFS: %5u write(%s/%lld %d@%lld)",
98248186c7dSChuck Lever 		task->tk_pid,
98348186c7dSChuck Lever 		data->req->wb_context->path.dentry->d_inode->i_sb->s_id,
98448186c7dSChuck Lever 		(long long)
98548186c7dSChuck Lever 		  NFS_FILEID(data->req->wb_context->path.dentry->d_inode),
98648186c7dSChuck Lever 		data->req->wb_bytes, (long long)req_offset(data->req));
9871da177e4SLinus Torvalds 
988c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
989c9d8f89dSTrond Myklebust }
990788e7a89STrond Myklebust 
991c9d8f89dSTrond Myklebust static void nfs_writeback_release_partial(void *calldata)
992c9d8f89dSTrond Myklebust {
993c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
994c9d8f89dSTrond Myklebust 	struct nfs_page		*req = data->req;
995c9d8f89dSTrond Myklebust 	struct page		*page = req->wb_page;
996c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
997c9d8f89dSTrond Myklebust 
998c9d8f89dSTrond Myklebust 	if (status < 0) {
999a301b777STrond Myklebust 		nfs_set_pageerror(page);
1000c9d8f89dSTrond Myklebust 		nfs_context_set_write_error(req->wb_context, status);
1001c9d8f89dSTrond Myklebust 		dprintk(", error = %d\n", status);
10028e821cadSTrond Myklebust 		goto out;
10038e821cadSTrond Myklebust 	}
10048e821cadSTrond Myklebust 
10058e821cadSTrond Myklebust 	if (nfs_write_need_commit(data)) {
1006587142f8STrond Myklebust 		struct inode *inode = page->mapping->host;
10078e821cadSTrond Myklebust 
1008587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
10098e821cadSTrond Myklebust 		if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) {
10108e821cadSTrond Myklebust 			/* Do nothing we need to resend the writes */
10118e821cadSTrond Myklebust 		} else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) {
10121da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
10131da177e4SLinus Torvalds 			dprintk(" defer commit\n");
10141da177e4SLinus Torvalds 		} else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
10158e821cadSTrond Myklebust 			set_bit(PG_NEED_RESCHED, &req->wb_flags);
10168e821cadSTrond Myklebust 			clear_bit(PG_NEED_COMMIT, &req->wb_flags);
10171da177e4SLinus Torvalds 			dprintk(" server reboot detected\n");
10181da177e4SLinus Torvalds 		}
1019587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
10201da177e4SLinus Torvalds 	} else
10211da177e4SLinus Torvalds 		dprintk(" OK\n");
10221da177e4SLinus Torvalds 
10238e821cadSTrond Myklebust out:
10241da177e4SLinus Torvalds 	if (atomic_dec_and_test(&req->wb_complete))
10251da177e4SLinus Torvalds 		nfs_writepage_release(req);
1026c9d8f89dSTrond Myklebust 	nfs_writedata_release(calldata);
10271da177e4SLinus Torvalds }
10281da177e4SLinus Torvalds 
1029788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops = {
1030788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_partial,
1031c9d8f89dSTrond Myklebust 	.rpc_release = nfs_writeback_release_partial,
1032788e7a89STrond Myklebust };
1033788e7a89STrond Myklebust 
10341da177e4SLinus Torvalds /*
10351da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
10361da177e4SLinus Torvalds  *
10371da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
10381da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
10391da177e4SLinus Torvalds  *	  as the page has a write request pending.
10401da177e4SLinus Torvalds  */
1041788e7a89STrond Myklebust static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
10421da177e4SLinus Torvalds {
1043788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
10441da177e4SLinus Torvalds 
1045c9d8f89dSTrond Myklebust 	nfs_writeback_done(task, data);
1046c9d8f89dSTrond Myklebust }
1047c9d8f89dSTrond Myklebust 
1048c9d8f89dSTrond Myklebust static void nfs_writeback_release_full(void *calldata)
1049c9d8f89dSTrond Myklebust {
1050c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1051c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
1052788e7a89STrond Myklebust 
10531da177e4SLinus Torvalds 	/* Update attributes as result of writeback. */
10541da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
1055c9d8f89dSTrond Myklebust 		struct nfs_page *req = nfs_list_entry(data->pages.next);
1056c9d8f89dSTrond Myklebust 		struct page *page = req->wb_page;
1057c9d8f89dSTrond Myklebust 
10581da177e4SLinus Torvalds 		nfs_list_remove_request(req);
10591da177e4SLinus Torvalds 
106048186c7dSChuck Lever 		dprintk("NFS: %5u write (%s/%lld %d@%lld)",
106148186c7dSChuck Lever 			data->task.tk_pid,
106288be9f99STrond Myklebust 			req->wb_context->path.dentry->d_inode->i_sb->s_id,
106388be9f99STrond Myklebust 			(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
10641da177e4SLinus Torvalds 			req->wb_bytes,
10651da177e4SLinus Torvalds 			(long long)req_offset(req));
10661da177e4SLinus Torvalds 
1067c9d8f89dSTrond Myklebust 		if (status < 0) {
1068a301b777STrond Myklebust 			nfs_set_pageerror(page);
1069c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
1070c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
10718e821cadSTrond Myklebust 			goto remove_request;
10721da177e4SLinus Torvalds 		}
10731da177e4SLinus Torvalds 
10748e821cadSTrond Myklebust 		if (nfs_write_need_commit(data)) {
10751da177e4SLinus Torvalds 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
10761da177e4SLinus Torvalds 			nfs_mark_request_commit(req);
10778e821cadSTrond Myklebust 			nfs_end_page_writeback(page);
10781da177e4SLinus Torvalds 			dprintk(" marked for commit\n");
10798e821cadSTrond Myklebust 			goto next;
10808e821cadSTrond Myklebust 		}
10818e821cadSTrond Myklebust 		dprintk(" OK\n");
10828e821cadSTrond Myklebust remove_request:
10838e821cadSTrond Myklebust 		nfs_end_page_writeback(page);
10841da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
10851da177e4SLinus Torvalds 	next:
10869fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
10871da177e4SLinus Torvalds 	}
1088c9d8f89dSTrond Myklebust 	nfs_writedata_release(calldata);
10891da177e4SLinus Torvalds }
10901da177e4SLinus Torvalds 
1091788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops = {
1092788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_full,
1093c9d8f89dSTrond Myklebust 	.rpc_release = nfs_writeback_release_full,
1094788e7a89STrond Myklebust };
1095788e7a89STrond Myklebust 
1096788e7a89STrond Myklebust 
10971da177e4SLinus Torvalds /*
10981da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
10991da177e4SLinus Torvalds  */
1100462d5b32SChuck Lever int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
11011da177e4SLinus Torvalds {
11021da177e4SLinus Torvalds 	struct nfs_writeargs	*argp = &data->args;
11031da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
1104788e7a89STrond Myklebust 	int status;
11051da177e4SLinus Torvalds 
1106a3f565b1SChuck Lever 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
11071da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
11081da177e4SLinus Torvalds 
1109f551e44fSChuck Lever 	/*
1110f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1111f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1112f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1113f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1114f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1115f551e44fSChuck Lever 	 */
1116788e7a89STrond Myklebust 	status = NFS_PROTO(data->inode)->write_done(task, data);
1117788e7a89STrond Myklebust 	if (status != 0)
1118788e7a89STrond Myklebust 		return status;
111991d5b470SChuck Lever 	nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
112091d5b470SChuck Lever 
11211da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
11221da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
11231da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
11241da177e4SLinus Torvalds 		 * commit data to stable storage even though we
11251da177e4SLinus Torvalds 		 * requested it.
11261da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
11271da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
11281da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
11291da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
11301da177e4SLinus Torvalds 		 */
11311da177e4SLinus Torvalds 		static unsigned long    complain;
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
11341da177e4SLinus Torvalds 			dprintk("NFS:       faulty NFS server %s:"
11351da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
113654ceac45SDavid Howells 				NFS_SERVER(data->inode)->nfs_client->cl_hostname,
11371da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
11381da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
11391da177e4SLinus Torvalds 		}
11401da177e4SLinus Torvalds 	}
11411da177e4SLinus Torvalds #endif
11421da177e4SLinus Torvalds 	/* Is this a short write? */
11431da177e4SLinus Torvalds 	if (task->tk_status >= 0 && resp->count < argp->count) {
11441da177e4SLinus Torvalds 		static unsigned long    complain;
11451da177e4SLinus Torvalds 
114691d5b470SChuck Lever 		nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
114791d5b470SChuck Lever 
11481da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
11491da177e4SLinus Torvalds 		if (resp->count != 0) {
11501da177e4SLinus Torvalds 			/* Was this an NFSv2 write or an NFSv3 stable write? */
11511da177e4SLinus Torvalds 			if (resp->verf->committed != NFS_UNSTABLE) {
11521da177e4SLinus Torvalds 				/* Resend from where the server left off */
11531da177e4SLinus Torvalds 				argp->offset += resp->count;
11541da177e4SLinus Torvalds 				argp->pgbase += resp->count;
11551da177e4SLinus Torvalds 				argp->count -= resp->count;
11561da177e4SLinus Torvalds 			} else {
11571da177e4SLinus Torvalds 				/* Resend as a stable write in order to avoid
11581da177e4SLinus Torvalds 				 * headaches in the case of a server crash.
11591da177e4SLinus Torvalds 				 */
11601da177e4SLinus Torvalds 				argp->stable = NFS_FILE_SYNC;
11611da177e4SLinus Torvalds 			}
11621da177e4SLinus Torvalds 			rpc_restart_call(task);
1163788e7a89STrond Myklebust 			return -EAGAIN;
11641da177e4SLinus Torvalds 		}
11651da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
11661da177e4SLinus Torvalds 			printk(KERN_WARNING
11671da177e4SLinus Torvalds 			       "NFS: Server wrote zero bytes, expected %u.\n",
11681da177e4SLinus Torvalds 					argp->count);
11691da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
11701da177e4SLinus Torvalds 		}
11711da177e4SLinus Torvalds 		/* Can't do anything about it except throw an error. */
11721da177e4SLinus Torvalds 		task->tk_status = -EIO;
11731da177e4SLinus Torvalds 	}
1174788e7a89STrond Myklebust 	return 0;
11751da177e4SLinus Torvalds }
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1179c9d8f89dSTrond Myklebust void nfs_commitdata_release(void *data)
11801da177e4SLinus Torvalds {
1181383ba719STrond Myklebust 	struct nfs_write_data *wdata = data;
1182383ba719STrond Myklebust 
1183383ba719STrond Myklebust 	put_nfs_open_context(wdata->args.context);
11841da177e4SLinus Torvalds 	nfs_commit_free(wdata);
11851da177e4SLinus Torvalds }
11861da177e4SLinus Torvalds 
11871da177e4SLinus Torvalds /*
11881da177e4SLinus Torvalds  * Set up the argument/result storage required for the RPC call.
11891da177e4SLinus Torvalds  */
1190dbae4c73STrond Myklebust static int nfs_commit_rpcsetup(struct list_head *head,
1191788e7a89STrond Myklebust 		struct nfs_write_data *data,
1192788e7a89STrond Myklebust 		int how)
11931da177e4SLinus Torvalds {
119484115e1cSTrond Myklebust 	struct nfs_page *first = nfs_list_entry(head->next);
119584115e1cSTrond Myklebust 	struct inode *inode = first->wb_context->path.dentry->d_inode;
119684115e1cSTrond Myklebust 	int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
11973ff7576dSTrond Myklebust 	int priority = flush_task_priority(how);
119807737691STrond Myklebust 	struct rpc_task *task;
1199bdc7f021STrond Myklebust 	struct rpc_message msg = {
1200bdc7f021STrond Myklebust 		.rpc_argp = &data->args,
1201bdc7f021STrond Myklebust 		.rpc_resp = &data->res,
1202bdc7f021STrond Myklebust 		.rpc_cred = first->wb_context->cred,
1203bdc7f021STrond Myklebust 	};
120484115e1cSTrond Myklebust 	struct rpc_task_setup task_setup_data = {
120507737691STrond Myklebust 		.task = &data->task,
120684115e1cSTrond Myklebust 		.rpc_client = NFS_CLIENT(inode),
1207bdc7f021STrond Myklebust 		.rpc_message = &msg,
120884115e1cSTrond Myklebust 		.callback_ops = &nfs_commit_ops,
120984115e1cSTrond Myklebust 		.callback_data = data,
1210101070caSTrond Myklebust 		.workqueue = nfsiod_workqueue,
121184115e1cSTrond Myklebust 		.flags = flags,
12123ff7576dSTrond Myklebust 		.priority = priority,
121384115e1cSTrond Myklebust 	};
12141da177e4SLinus Torvalds 
12151da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
12161da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 	list_splice_init(head, &data->pages);
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds 	data->inode	  = inode;
1221bdc7f021STrond Myklebust 	data->cred	  = msg.rpc_cred;
12221da177e4SLinus Torvalds 
12231da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(data->inode);
12243da28eb1STrond Myklebust 	/* Note: we always request a commit of the entire inode */
12253da28eb1STrond Myklebust 	data->args.offset = 0;
12263da28eb1STrond Myklebust 	data->args.count  = 0;
1227383ba719STrond Myklebust 	data->args.context = get_nfs_open_context(first->wb_context);
12283da28eb1STrond Myklebust 	data->res.count   = 0;
12291da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
12301da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
12310e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
12321da177e4SLinus Torvalds 
1233788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
1234bdc7f021STrond Myklebust 	NFS_PROTO(inode)->commit_setup(data, &msg);
12351da177e4SLinus Torvalds 
1236a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
1237bdc7f021STrond Myklebust 
123807737691STrond Myklebust 	task = rpc_run_task(&task_setup_data);
1239dbae4c73STrond Myklebust 	if (IS_ERR(task))
1240dbae4c73STrond Myklebust 		return PTR_ERR(task);
124107737691STrond Myklebust 	rpc_put_task(task);
1242dbae4c73STrond Myklebust 	return 0;
12431da177e4SLinus Torvalds }
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds /*
12461da177e4SLinus Torvalds  * Commit dirty pages
12471da177e4SLinus Torvalds  */
12481da177e4SLinus Torvalds static int
124940859d7eSChuck Lever nfs_commit_list(struct inode *inode, struct list_head *head, int how)
12501da177e4SLinus Torvalds {
12511da177e4SLinus Torvalds 	struct nfs_write_data	*data;
12521da177e4SLinus Torvalds 	struct nfs_page         *req;
12531da177e4SLinus Torvalds 
1254c9d8f89dSTrond Myklebust 	data = nfs_commitdata_alloc();
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 	if (!data)
12571da177e4SLinus Torvalds 		goto out_bad;
12581da177e4SLinus Torvalds 
12591da177e4SLinus Torvalds 	/* Set up the argument struct */
1260dbae4c73STrond Myklebust 	return nfs_commit_rpcsetup(head, data, how);
12611da177e4SLinus Torvalds  out_bad:
12621da177e4SLinus Torvalds 	while (!list_empty(head)) {
12631da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
12641da177e4SLinus Torvalds 		nfs_list_remove_request(req);
12651da177e4SLinus Torvalds 		nfs_mark_request_commit(req);
126683715ad5STrond Myklebust 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1267c9e51e41SPeter Zijlstra 		dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1268c9e51e41SPeter Zijlstra 				BDI_RECLAIMABLE);
12699fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
12701da177e4SLinus Torvalds 	}
12711da177e4SLinus Torvalds 	return -ENOMEM;
12721da177e4SLinus Torvalds }
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds /*
12751da177e4SLinus Torvalds  * COMMIT call returned
12761da177e4SLinus Torvalds  */
1277788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
12781da177e4SLinus Torvalds {
1279963d8fe5STrond Myklebust 	struct nfs_write_data	*data = calldata;
12801da177e4SLinus Torvalds 
1281a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
12821da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
12831da177e4SLinus Torvalds 
1284788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1285788e7a89STrond Myklebust 	if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1286788e7a89STrond Myklebust 		return;
1287c9d8f89dSTrond Myklebust }
1288c9d8f89dSTrond Myklebust 
1289c9d8f89dSTrond Myklebust static void nfs_commit_release(void *calldata)
1290c9d8f89dSTrond Myklebust {
1291c9d8f89dSTrond Myklebust 	struct nfs_write_data	*data = calldata;
1292c9d8f89dSTrond Myklebust 	struct nfs_page		*req;
1293c9d8f89dSTrond Myklebust 	int status = data->task.tk_status;
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);
1298e468bae9STrond Myklebust 		nfs_clear_request_commit(req);
12991da177e4SLinus Torvalds 
130048186c7dSChuck Lever 		dprintk("NFS:       commit (%s/%lld %d@%lld)",
130188be9f99STrond Myklebust 			req->wb_context->path.dentry->d_inode->i_sb->s_id,
130288be9f99STrond Myklebust 			(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
13031da177e4SLinus Torvalds 			req->wb_bytes,
13041da177e4SLinus Torvalds 			(long long)req_offset(req));
1305c9d8f89dSTrond Myklebust 		if (status < 0) {
1306c9d8f89dSTrond Myklebust 			nfs_context_set_write_error(req->wb_context, status);
13071da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1308c9d8f89dSTrond Myklebust 			dprintk(", error = %d\n", status);
13091da177e4SLinus Torvalds 			goto next;
13101da177e4SLinus Torvalds 		}
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
13131da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
13141da177e4SLinus Torvalds 		if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
13151da177e4SLinus Torvalds 			/* We have a match */
13161da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
13171da177e4SLinus Torvalds 			dprintk(" OK\n");
13181da177e4SLinus Torvalds 			goto next;
13191da177e4SLinus Torvalds 		}
13201da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
13211da177e4SLinus Torvalds 		dprintk(" mismatch\n");
13226d884e8fSFred 		nfs_mark_request_dirty(req);
13231da177e4SLinus Torvalds 	next:
13249fd367f0STrond Myklebust 		nfs_clear_page_tag_locked(req);
13251da177e4SLinus Torvalds 	}
1326c9d8f89dSTrond Myklebust 	nfs_commitdata_release(calldata);
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 };
13331da177e4SLinus Torvalds 
13343da28eb1STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
13351da177e4SLinus Torvalds {
13361da177e4SLinus Torvalds 	LIST_HEAD(head);
13377d46a49fSTrond Myklebust 	int res;
13381da177e4SLinus Torvalds 
1339587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
13403da28eb1STrond Myklebust 	res = nfs_scan_commit(inode, &head, 0, 0);
1341587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
13421da177e4SLinus Torvalds 	if (res) {
13437d46a49fSTrond Myklebust 		int error = nfs_commit_list(inode, &head, how);
13441da177e4SLinus Torvalds 		if (error < 0)
13451da177e4SLinus Torvalds 			return error;
13463da28eb1STrond Myklebust 	}
13471da177e4SLinus Torvalds 	return res;
13481da177e4SLinus Torvalds }
1349c63c7b05STrond Myklebust #else
1350c63c7b05STrond Myklebust static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1351c63c7b05STrond Myklebust {
1352c63c7b05STrond Myklebust 	return 0;
1353c63c7b05STrond Myklebust }
13541da177e4SLinus Torvalds #endif
13551da177e4SLinus Torvalds 
13561c75950bSTrond Myklebust long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
13571da177e4SLinus Torvalds {
13581c75950bSTrond Myklebust 	struct inode *inode = mapping->host;
1359ca52fec1STrond Myklebust 	pgoff_t idx_start, idx_end;
13601c75950bSTrond Myklebust 	unsigned int npages = 0;
1361c42de9ddSTrond Myklebust 	LIST_HEAD(head);
136270b9ecbdSTrond Myklebust 	int nocommit = how & FLUSH_NOCOMMIT;
13633f442547STrond Myklebust 	long pages, ret;
13641da177e4SLinus Torvalds 
13651c75950bSTrond Myklebust 	/* FIXME */
13661c75950bSTrond Myklebust 	if (wbc->range_cyclic)
13671c75950bSTrond Myklebust 		idx_start = 0;
13681c75950bSTrond Myklebust 	else {
13691c75950bSTrond Myklebust 		idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
13701c75950bSTrond Myklebust 		idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
13711c75950bSTrond Myklebust 		if (idx_end > idx_start) {
1372ca52fec1STrond Myklebust 			pgoff_t l_npages = 1 + idx_end - idx_start;
13731c75950bSTrond Myklebust 			npages = l_npages;
13741c75950bSTrond Myklebust 			if (sizeof(npages) != sizeof(l_npages) &&
1375ca52fec1STrond Myklebust 					(pgoff_t)npages != l_npages)
13761c75950bSTrond Myklebust 				npages = 0;
13771c75950bSTrond Myklebust 		}
13781c75950bSTrond Myklebust 	}
1379c42de9ddSTrond Myklebust 	how &= ~FLUSH_NOCOMMIT;
1380587142f8STrond Myklebust 	spin_lock(&inode->i_lock);
13811da177e4SLinus Torvalds 	do {
1382c42de9ddSTrond Myklebust 		ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1383c42de9ddSTrond Myklebust 		if (ret != 0)
1384c42de9ddSTrond Myklebust 			continue;
1385c42de9ddSTrond Myklebust 		if (nocommit)
1386c42de9ddSTrond Myklebust 			break;
1387d2ccddf0STrond Myklebust 		pages = nfs_scan_commit(inode, &head, idx_start, npages);
1388724c439cSTrond Myklebust 		if (pages == 0)
1389c42de9ddSTrond Myklebust 			break;
1390d2ccddf0STrond Myklebust 		if (how & FLUSH_INVALIDATE) {
1391587142f8STrond Myklebust 			spin_unlock(&inode->i_lock);
139283715ad5STrond Myklebust 			nfs_cancel_commit_list(&head);
1393e8e058e8STrond Myklebust 			ret = pages;
1394587142f8STrond Myklebust 			spin_lock(&inode->i_lock);
1395d2ccddf0STrond Myklebust 			continue;
1396d2ccddf0STrond Myklebust 		}
1397d2ccddf0STrond Myklebust 		pages += nfs_scan_commit(inode, &head, 0, 0);
1398587142f8STrond Myklebust 		spin_unlock(&inode->i_lock);
1399c42de9ddSTrond Myklebust 		ret = nfs_commit_list(inode, &head, how);
1400587142f8STrond Myklebust 		spin_lock(&inode->i_lock);
1401587142f8STrond Myklebust 
1402c42de9ddSTrond Myklebust 	} while (ret >= 0);
1403587142f8STrond Myklebust 	spin_unlock(&inode->i_lock);
1404c42de9ddSTrond Myklebust 	return ret;
14051da177e4SLinus Torvalds }
14061da177e4SLinus Torvalds 
140734901f70STrond Myklebust static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
14081c75950bSTrond Myklebust {
14091c75950bSTrond Myklebust 	int ret;
14101c75950bSTrond Myklebust 
141134901f70STrond Myklebust 	ret = nfs_writepages(mapping, wbc);
141261822ab5STrond Myklebust 	if (ret < 0)
141361822ab5STrond Myklebust 		goto out;
141434901f70STrond Myklebust 	ret = nfs_sync_mapping_wait(mapping, wbc, how);
1415ed90ef51STrond Myklebust 	if (ret < 0)
1416ed90ef51STrond Myklebust 		goto out;
14171c75950bSTrond Myklebust 	return 0;
141861822ab5STrond Myklebust out:
1419e507d9ebSTrond Myklebust 	__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
14201c75950bSTrond Myklebust 	return ret;
14211c75950bSTrond Myklebust }
14221c75950bSTrond Myklebust 
142334901f70STrond Myklebust /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */
142434901f70STrond Myklebust static int nfs_write_mapping(struct address_space *mapping, int how)
142534901f70STrond Myklebust {
142634901f70STrond Myklebust 	struct writeback_control wbc = {
142734901f70STrond Myklebust 		.bdi = mapping->backing_dev_info,
142834901f70STrond Myklebust 		.sync_mode = WB_SYNC_NONE,
142934901f70STrond Myklebust 		.nr_to_write = LONG_MAX,
1430d7fb1207STrond Myklebust 		.range_start = 0,
1431d7fb1207STrond Myklebust 		.range_end = LLONG_MAX,
143234901f70STrond Myklebust 		.for_writepages = 1,
143334901f70STrond Myklebust 	};
143434901f70STrond Myklebust 	int ret;
143534901f70STrond Myklebust 
143634901f70STrond Myklebust 	ret = __nfs_write_mapping(mapping, &wbc, how);
143734901f70STrond Myklebust 	if (ret < 0)
143834901f70STrond Myklebust 		return ret;
143934901f70STrond Myklebust 	wbc.sync_mode = WB_SYNC_ALL;
144034901f70STrond Myklebust 	return __nfs_write_mapping(mapping, &wbc, how);
144134901f70STrond Myklebust }
144234901f70STrond Myklebust 
1443ed90ef51STrond Myklebust /*
1444ed90ef51STrond Myklebust  * flush the inode to disk.
1445ed90ef51STrond Myklebust  */
1446ed90ef51STrond Myklebust int nfs_wb_all(struct inode *inode)
14471c75950bSTrond Myklebust {
1448ed90ef51STrond Myklebust 	return nfs_write_mapping(inode->i_mapping, 0);
1449ed90ef51STrond Myklebust }
14501c75950bSTrond Myklebust 
1451ed90ef51STrond Myklebust int nfs_wb_nocommit(struct inode *inode)
1452ed90ef51STrond Myklebust {
1453ed90ef51STrond Myklebust 	return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT);
14541c75950bSTrond Myklebust }
14551c75950bSTrond Myklebust 
14561b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page)
14571b3b4a1aSTrond Myklebust {
14581b3b4a1aSTrond Myklebust 	struct nfs_page *req;
14591b3b4a1aSTrond Myklebust 	loff_t range_start = page_offset(page);
14601b3b4a1aSTrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
14611b3b4a1aSTrond Myklebust 	struct writeback_control wbc = {
14621b3b4a1aSTrond Myklebust 		.bdi = page->mapping->backing_dev_info,
14631b3b4a1aSTrond Myklebust 		.sync_mode = WB_SYNC_ALL,
14641b3b4a1aSTrond Myklebust 		.nr_to_write = LONG_MAX,
14651b3b4a1aSTrond Myklebust 		.range_start = range_start,
14661b3b4a1aSTrond Myklebust 		.range_end = range_end,
14671b3b4a1aSTrond Myklebust 	};
14681b3b4a1aSTrond Myklebust 	int ret = 0;
14691b3b4a1aSTrond Myklebust 
14701b3b4a1aSTrond Myklebust 	BUG_ON(!PageLocked(page));
14711b3b4a1aSTrond Myklebust 	for (;;) {
14721b3b4a1aSTrond Myklebust 		req = nfs_page_find_request(page);
14731b3b4a1aSTrond Myklebust 		if (req == NULL)
14741b3b4a1aSTrond Myklebust 			goto out;
1475e468bae9STrond Myklebust 		if (test_bit(PG_CLEAN, &req->wb_flags)) {
14761b3b4a1aSTrond Myklebust 			nfs_release_request(req);
14771b3b4a1aSTrond Myklebust 			break;
14781b3b4a1aSTrond Myklebust 		}
14791b3b4a1aSTrond Myklebust 		if (nfs_lock_request_dontget(req)) {
14801b3b4a1aSTrond Myklebust 			nfs_inode_remove_request(req);
14811b3b4a1aSTrond Myklebust 			/*
14821b3b4a1aSTrond Myklebust 			 * In case nfs_inode_remove_request has marked the
14831b3b4a1aSTrond Myklebust 			 * page as being dirty
14841b3b4a1aSTrond Myklebust 			 */
14851b3b4a1aSTrond Myklebust 			cancel_dirty_page(page, PAGE_CACHE_SIZE);
14861b3b4a1aSTrond Myklebust 			nfs_unlock_request(req);
14871b3b4a1aSTrond Myklebust 			break;
14881b3b4a1aSTrond Myklebust 		}
14891b3b4a1aSTrond Myklebust 		ret = nfs_wait_on_request(req);
14901b3b4a1aSTrond Myklebust 		if (ret < 0)
14911b3b4a1aSTrond Myklebust 			goto out;
14921b3b4a1aSTrond Myklebust 	}
14931b3b4a1aSTrond Myklebust 	if (!PagePrivate(page))
14941b3b4a1aSTrond Myklebust 		return 0;
14951b3b4a1aSTrond Myklebust 	ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE);
14961b3b4a1aSTrond Myklebust out:
14971b3b4a1aSTrond Myklebust 	return ret;
14981b3b4a1aSTrond Myklebust }
14991b3b4a1aSTrond Myklebust 
15005334eb13SAdrian Bunk static int nfs_wb_page_priority(struct inode *inode, struct page *page,
15015334eb13SAdrian Bunk 				int how)
15021c75950bSTrond Myklebust {
15031c75950bSTrond Myklebust 	loff_t range_start = page_offset(page);
15041c75950bSTrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
15054d770ccfSTrond Myklebust 	struct writeback_control wbc = {
15064d770ccfSTrond Myklebust 		.bdi = page->mapping->backing_dev_info,
15074d770ccfSTrond Myklebust 		.sync_mode = WB_SYNC_ALL,
15084d770ccfSTrond Myklebust 		.nr_to_write = LONG_MAX,
15094d770ccfSTrond Myklebust 		.range_start = range_start,
15104d770ccfSTrond Myklebust 		.range_end = range_end,
15114d770ccfSTrond Myklebust 	};
15124d770ccfSTrond Myklebust 	int ret;
15131c75950bSTrond Myklebust 
151473e3302fSTrond Myklebust 	do {
1515c63c7b05STrond Myklebust 		if (clear_page_dirty_for_io(page)) {
15164d770ccfSTrond Myklebust 			ret = nfs_writepage_locked(page, &wbc);
15174d770ccfSTrond Myklebust 			if (ret < 0)
151873e3302fSTrond Myklebust 				goto out_error;
151973e3302fSTrond Myklebust 		} else if (!PagePrivate(page))
152073e3302fSTrond Myklebust 			break;
15214d770ccfSTrond Myklebust 		ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
152273e3302fSTrond Myklebust 		if (ret < 0)
152373e3302fSTrond Myklebust 			goto out_error;
152473e3302fSTrond Myklebust 	} while (PagePrivate(page));
15254d770ccfSTrond Myklebust 	return 0;
152673e3302fSTrond Myklebust out_error:
1527e507d9ebSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_PAGES);
15284d770ccfSTrond Myklebust 	return ret;
15291c75950bSTrond Myklebust }
15301c75950bSTrond Myklebust 
15311c75950bSTrond Myklebust /*
15321c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
15331c75950bSTrond Myklebust  */
15341c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page* page)
15351c75950bSTrond Myklebust {
15364d770ccfSTrond Myklebust 	return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
15371c75950bSTrond Myklebust }
15381c75950bSTrond Myklebust 
1539f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
15401da177e4SLinus Torvalds {
15411da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
15421da177e4SLinus Torvalds 					     sizeof(struct nfs_write_data),
15431da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
154420c2df83SPaul Mundt 					     NULL);
15451da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
15461da177e4SLinus Torvalds 		return -ENOMEM;
15471da177e4SLinus Torvalds 
154893d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
15491da177e4SLinus Torvalds 						     nfs_wdata_cachep);
15501da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
15511da177e4SLinus Torvalds 		return -ENOMEM;
15521da177e4SLinus Torvalds 
155393d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
15541da177e4SLinus Torvalds 						      nfs_wdata_cachep);
15551da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
15561da177e4SLinus Torvalds 		return -ENOMEM;
15571da177e4SLinus Torvalds 
155889a09141SPeter Zijlstra 	/*
155989a09141SPeter Zijlstra 	 * NFS congestion size, scale with available memory.
156089a09141SPeter Zijlstra 	 *
156189a09141SPeter Zijlstra 	 *  64MB:    8192k
156289a09141SPeter Zijlstra 	 * 128MB:   11585k
156389a09141SPeter Zijlstra 	 * 256MB:   16384k
156489a09141SPeter Zijlstra 	 * 512MB:   23170k
156589a09141SPeter Zijlstra 	 *   1GB:   32768k
156689a09141SPeter Zijlstra 	 *   2GB:   46340k
156789a09141SPeter Zijlstra 	 *   4GB:   65536k
156889a09141SPeter Zijlstra 	 *   8GB:   92681k
156989a09141SPeter Zijlstra 	 *  16GB:  131072k
157089a09141SPeter Zijlstra 	 *
157189a09141SPeter Zijlstra 	 * This allows larger machines to have larger/more transfers.
157289a09141SPeter Zijlstra 	 * Limit the default to 256M
157389a09141SPeter Zijlstra 	 */
157489a09141SPeter Zijlstra 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
157589a09141SPeter Zijlstra 	if (nfs_congestion_kb > 256*1024)
157689a09141SPeter Zijlstra 		nfs_congestion_kb = 256*1024;
157789a09141SPeter Zijlstra 
15781da177e4SLinus Torvalds 	return 0;
15791da177e4SLinus Torvalds }
15801da177e4SLinus Torvalds 
1581266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
15821da177e4SLinus Torvalds {
15831da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
15841da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
15851a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
15861da177e4SLinus Torvalds }
15871da177e4SLinus Torvalds 
1588