xref: /openbmc/linux/fs/nfs/write.c (revision a301b777)
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>
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
171da177e4SLinus Torvalds #include <linux/nfs_fs.h>
181da177e4SLinus Torvalds #include <linux/nfs_mount.h>
191da177e4SLinus Torvalds #include <linux/nfs_page.h>
203fcfab16SAndrew Morton #include <linux/backing-dev.h>
213fcfab16SAndrew Morton 
221da177e4SLinus Torvalds #include <asm/uaccess.h>
231da177e4SLinus Torvalds #include <linux/smp_lock.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);
40e261f51fSTrond Myklebust static void nfs_mark_request_dirty(struct nfs_page *req);
411da177e4SLinus Torvalds static int nfs_wait_on_write_congestion(struct address_space *, int);
423f442547STrond Myklebust static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how);
43788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops;
44788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops;
45788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops;
461da177e4SLinus Torvalds 
47e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep;
483feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
491da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
521da177e4SLinus Torvalds 
53e9f7bee1STrond Myklebust struct nfs_write_data *nfs_commit_alloc(void)
541da177e4SLinus Torvalds {
55e6b4f8daSChristoph Lameter 	struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
5640859d7eSChuck Lever 
571da177e4SLinus Torvalds 	if (p) {
581da177e4SLinus Torvalds 		memset(p, 0, sizeof(*p));
591da177e4SLinus Torvalds 		INIT_LIST_HEAD(&p->pages);
601da177e4SLinus Torvalds 	}
611da177e4SLinus Torvalds 	return p;
621da177e4SLinus Torvalds }
631da177e4SLinus Torvalds 
648aca67f0STrond Myklebust void nfs_commit_rcu_free(struct rcu_head *head)
651da177e4SLinus Torvalds {
668aca67f0STrond Myklebust 	struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
6740859d7eSChuck Lever 	if (p && (p->pagevec != &p->page_array[0]))
6840859d7eSChuck Lever 		kfree(p->pagevec);
691da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
701da177e4SLinus Torvalds }
711da177e4SLinus Torvalds 
728aca67f0STrond Myklebust void nfs_commit_free(struct nfs_write_data *wdata)
738aca67f0STrond Myklebust {
748aca67f0STrond Myklebust 	call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
758aca67f0STrond Myklebust }
768aca67f0STrond Myklebust 
77e9f7bee1STrond Myklebust struct nfs_write_data *nfs_writedata_alloc(size_t len)
783feb2d49STrond Myklebust {
79e9f7bee1STrond Myklebust 	unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
80e6b4f8daSChristoph Lameter 	struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
813feb2d49STrond Myklebust 
823feb2d49STrond Myklebust 	if (p) {
833feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
843feb2d49STrond Myklebust 		INIT_LIST_HEAD(&p->pages);
85e9f7bee1STrond Myklebust 		p->npages = pagecount;
860d0b5cb3SChuck Lever 		if (pagecount <= ARRAY_SIZE(p->page_array))
870d0b5cb3SChuck Lever 			p->pagevec = p->page_array;
883feb2d49STrond Myklebust 		else {
890d0b5cb3SChuck Lever 			p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
900d0b5cb3SChuck Lever 			if (!p->pagevec) {
913feb2d49STrond Myklebust 				mempool_free(p, nfs_wdata_mempool);
923feb2d49STrond Myklebust 				p = NULL;
933feb2d49STrond Myklebust 			}
943feb2d49STrond Myklebust 		}
953feb2d49STrond Myklebust 	}
963feb2d49STrond Myklebust 	return p;
973feb2d49STrond Myklebust }
983feb2d49STrond Myklebust 
998aca67f0STrond Myklebust static void nfs_writedata_rcu_free(struct rcu_head *head)
1003feb2d49STrond Myklebust {
1018aca67f0STrond Myklebust 	struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
1023feb2d49STrond Myklebust 	if (p && (p->pagevec != &p->page_array[0]))
1033feb2d49STrond Myklebust 		kfree(p->pagevec);
1043feb2d49STrond Myklebust 	mempool_free(p, nfs_wdata_mempool);
1053feb2d49STrond Myklebust }
1063feb2d49STrond Myklebust 
1078aca67f0STrond Myklebust static void nfs_writedata_free(struct nfs_write_data *wdata)
1088aca67f0STrond Myklebust {
1098aca67f0STrond Myklebust 	call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
1108aca67f0STrond Myklebust }
1118aca67f0STrond Myklebust 
112963d8fe5STrond Myklebust void nfs_writedata_release(void *wdata)
1131da177e4SLinus Torvalds {
1141da177e4SLinus Torvalds 	nfs_writedata_free(wdata);
1151da177e4SLinus Torvalds }
1161da177e4SLinus Torvalds 
117277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request_locked(struct page *page)
118277459d2STrond Myklebust {
119277459d2STrond Myklebust 	struct nfs_page *req = NULL;
120277459d2STrond Myklebust 
121277459d2STrond Myklebust 	if (PagePrivate(page)) {
122277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
123277459d2STrond Myklebust 		if (req != NULL)
124277459d2STrond Myklebust 			atomic_inc(&req->wb_count);
125277459d2STrond Myklebust 	}
126277459d2STrond Myklebust 	return req;
127277459d2STrond Myklebust }
128277459d2STrond Myklebust 
129277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page)
130277459d2STrond Myklebust {
131277459d2STrond Myklebust 	struct nfs_page *req = NULL;
132277459d2STrond Myklebust 	spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
133277459d2STrond Myklebust 
134277459d2STrond Myklebust 	spin_lock(req_lock);
135277459d2STrond Myklebust 	req = nfs_page_find_request_locked(page);
136277459d2STrond Myklebust 	spin_unlock(req_lock);
137277459d2STrond Myklebust 	return req;
138277459d2STrond Myklebust }
139277459d2STrond Myklebust 
1401da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1411da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1421da177e4SLinus Torvalds {
1431da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
1441da177e4SLinus Torvalds 	loff_t end, i_size = i_size_read(inode);
1451da177e4SLinus Torvalds 	unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	if (i_size > 0 && page->index < end_index)
1481da177e4SLinus Torvalds 		return;
1491da177e4SLinus Torvalds 	end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
1501da177e4SLinus Torvalds 	if (i_size >= end)
1511da177e4SLinus Torvalds 		return;
15291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
1531da177e4SLinus Torvalds 	i_size_write(inode, end);
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
156a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */
157a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page)
158a301b777STrond Myklebust {
159a301b777STrond Myklebust 	SetPageError(page);
160a301b777STrond Myklebust 	nfs_zap_mapping(page->mapping->host, page->mapping);
161a301b777STrond Myklebust }
162a301b777STrond Myklebust 
1631da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
1641da177e4SLinus Torvalds  * covers the full page.
1651da177e4SLinus Torvalds  */
1661da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
1671da177e4SLinus Torvalds {
1681da177e4SLinus Torvalds 	if (PageUptodate(page))
1691da177e4SLinus Torvalds 		return;
1701da177e4SLinus Torvalds 	if (base != 0)
1711da177e4SLinus Torvalds 		return;
17249a70f27STrond Myklebust 	if (count != nfs_page_length(page))
1731da177e4SLinus Torvalds 		return;
17449a70f27STrond Myklebust 	if (count != PAGE_CACHE_SIZE)
1751da177e4SLinus Torvalds 		memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
1761da177e4SLinus Torvalds 	SetPageUptodate(page);
1771da177e4SLinus Torvalds }
1781da177e4SLinus Torvalds 
179e21195a7STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
1801da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
1811da177e4SLinus Torvalds {
1821da177e4SLinus Torvalds 	struct nfs_page	*req;
183e21195a7STrond Myklebust 	int ret;
1841da177e4SLinus Torvalds 
185e21195a7STrond Myklebust 	for (;;) {
186e21195a7STrond Myklebust 		req = nfs_update_request(ctx, page, offset, count);
187e21195a7STrond Myklebust 		if (!IS_ERR(req))
188e21195a7STrond Myklebust 			break;
189e21195a7STrond Myklebust 		ret = PTR_ERR(req);
190e21195a7STrond Myklebust 		if (ret != -EBUSY)
191e21195a7STrond Myklebust 			return ret;
192e21195a7STrond Myklebust 		ret = nfs_wb_page(page->mapping->host, page);
193e21195a7STrond Myklebust 		if (ret != 0)
194e21195a7STrond Myklebust 			return ret;
195e21195a7STrond Myklebust 	}
1961da177e4SLinus Torvalds 	/* Update file length */
1971da177e4SLinus Torvalds 	nfs_grow_file(page, offset, count);
1981da177e4SLinus Torvalds 	/* Set the PG_uptodate flag? */
1991da177e4SLinus Torvalds 	nfs_mark_uptodate(page, offset, count);
2001da177e4SLinus Torvalds 	nfs_unlock_request(req);
201abd3e641STrond Myklebust 	return 0;
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
2051da177e4SLinus Torvalds {
2061da177e4SLinus Torvalds 	if (wbc->for_reclaim)
2071da177e4SLinus Torvalds 		return FLUSH_HIGHPRI;
2081da177e4SLinus Torvalds 	if (wbc->for_kupdate)
2091da177e4SLinus Torvalds 		return FLUSH_LOWPRI;
2101da177e4SLinus Torvalds 	return 0;
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds /*
214e261f51fSTrond Myklebust  * Find an associated nfs write request, and prepare to flush it out
215e261f51fSTrond Myklebust  * Returns 1 if there was no write request, or if the request was
216e261f51fSTrond Myklebust  * already tagged by nfs_set_page_dirty.Returns 0 if the request
217e261f51fSTrond Myklebust  * was not tagged.
218e261f51fSTrond Myklebust  * May also return an error if the user signalled nfs_wait_on_request().
219e261f51fSTrond Myklebust  */
220e261f51fSTrond Myklebust static int nfs_page_mark_flush(struct page *page)
221e261f51fSTrond Myklebust {
222e261f51fSTrond Myklebust 	struct nfs_page *req;
223e261f51fSTrond Myklebust 	spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
224e261f51fSTrond Myklebust 	int ret;
225e261f51fSTrond Myklebust 
226e261f51fSTrond Myklebust 	spin_lock(req_lock);
227e261f51fSTrond Myklebust 	for(;;) {
228e261f51fSTrond Myklebust 		req = nfs_page_find_request_locked(page);
229e261f51fSTrond Myklebust 		if (req == NULL) {
230e261f51fSTrond Myklebust 			spin_unlock(req_lock);
231e261f51fSTrond Myklebust 			return 1;
232e261f51fSTrond Myklebust 		}
233e261f51fSTrond Myklebust 		if (nfs_lock_request_dontget(req))
234e261f51fSTrond Myklebust 			break;
235e261f51fSTrond Myklebust 		/* Note: If we hold the page lock, as is the case in nfs_writepage,
236e261f51fSTrond Myklebust 		 *	 then the call to nfs_lock_request_dontget() will always
237e261f51fSTrond Myklebust 		 *	 succeed provided that someone hasn't already marked the
238e261f51fSTrond Myklebust 		 *	 request as dirty (in which case we don't care).
239e261f51fSTrond Myklebust 		 */
240e261f51fSTrond Myklebust 		spin_unlock(req_lock);
241e261f51fSTrond Myklebust 		ret = nfs_wait_on_request(req);
242e261f51fSTrond Myklebust 		nfs_release_request(req);
243e261f51fSTrond Myklebust 		if (ret != 0)
244e261f51fSTrond Myklebust 			return ret;
245e261f51fSTrond Myklebust 		spin_lock(req_lock);
246e261f51fSTrond Myklebust 	}
247e261f51fSTrond Myklebust 	spin_unlock(req_lock);
24861822ab5STrond Myklebust 	if (test_and_set_bit(PG_FLUSHING, &req->wb_flags) == 0) {
249e261f51fSTrond Myklebust 		nfs_mark_request_dirty(req);
25061822ab5STrond Myklebust 		set_page_writeback(page);
25161822ab5STrond Myklebust 	}
252e261f51fSTrond Myklebust 	ret = test_bit(PG_NEED_FLUSH, &req->wb_flags);
253e261f51fSTrond Myklebust 	nfs_unlock_request(req);
254e261f51fSTrond Myklebust 	return ret;
255e261f51fSTrond Myklebust }
256e261f51fSTrond Myklebust 
257e261f51fSTrond Myklebust /*
2581da177e4SLinus Torvalds  * Write an mmapped page to the server.
2591da177e4SLinus Torvalds  */
2604d770ccfSTrond Myklebust static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
2611da177e4SLinus Torvalds {
2621da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
2631da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
26449a70f27STrond Myklebust 	unsigned offset;
265e261f51fSTrond Myklebust 	int err;
2661da177e4SLinus Torvalds 
26791d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
26891d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
26991d5b470SChuck Lever 
270e261f51fSTrond Myklebust 	err = nfs_page_mark_flush(page);
271e261f51fSTrond Myklebust 	if (err <= 0)
2721a54533eSTrond Myklebust 		goto out;
273e261f51fSTrond Myklebust 	err = 0;
27449a70f27STrond Myklebust 	offset = nfs_page_length(page);
27549a70f27STrond Myklebust 	if (!offset)
2761da177e4SLinus Torvalds 		goto out;
27749a70f27STrond Myklebust 
278d530838bSTrond Myklebust 	ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
2791da177e4SLinus Torvalds 	if (ctx == NULL) {
2801da177e4SLinus Torvalds 		err = -EBADF;
2811da177e4SLinus Torvalds 		goto out;
2821da177e4SLinus Torvalds 	}
283e21195a7STrond Myklebust 	err = nfs_writepage_setup(ctx, page, 0, offset);
284200baa21STrond Myklebust 	put_nfs_open_context(ctx);
285e261f51fSTrond Myklebust 	if (err != 0)
286e261f51fSTrond Myklebust 		goto out;
287e261f51fSTrond Myklebust 	err = nfs_page_mark_flush(page);
288e261f51fSTrond Myklebust 	if (err > 0)
289e261f51fSTrond Myklebust 		err = 0;
290200baa21STrond Myklebust out:
291abd3e641STrond Myklebust 	if (!wbc->for_writepages)
29202241bc4STrond Myklebust 		nfs_flush_mapping(page->mapping, wbc, FLUSH_STABLE|wb_priority(wbc));
2934d770ccfSTrond Myklebust 	return err;
2944d770ccfSTrond Myklebust }
2954d770ccfSTrond Myklebust 
2964d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc)
2974d770ccfSTrond Myklebust {
2984d770ccfSTrond Myklebust 	int err;
2994d770ccfSTrond Myklebust 
3004d770ccfSTrond Myklebust 	err = nfs_writepage_locked(page, wbc);
3011da177e4SLinus Torvalds 	unlock_page(page);
3021da177e4SLinus Torvalds 	return err;
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds /*
3061da177e4SLinus Torvalds  * Note: causes nfs_update_request() to block on the assumption
3071da177e4SLinus Torvalds  * 	 that the writeback is generated due to memory pressure.
3081da177e4SLinus Torvalds  */
3091da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3101da177e4SLinus Torvalds {
3111da177e4SLinus Torvalds 	struct backing_dev_info *bdi = mapping->backing_dev_info;
3121da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
3131da177e4SLinus Torvalds 	int err;
3141da177e4SLinus Torvalds 
31591d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
31691d5b470SChuck Lever 
3171da177e4SLinus Torvalds 	err = generic_writepages(mapping, wbc);
3181da177e4SLinus Torvalds 	if (err)
3191da177e4SLinus Torvalds 		return err;
3201da177e4SLinus Torvalds 	while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
3211da177e4SLinus Torvalds 		if (wbc->nonblocking)
3221da177e4SLinus Torvalds 			return 0;
3231da177e4SLinus Torvalds 		nfs_wait_on_write_congestion(mapping, 0);
3241da177e4SLinus Torvalds 	}
32528c6925fSTrond Myklebust 	err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
3261da177e4SLinus Torvalds 	if (err < 0)
3271da177e4SLinus Torvalds 		goto out;
32891d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
3291da177e4SLinus Torvalds 	err = 0;
3301da177e4SLinus Torvalds out:
3311da177e4SLinus Torvalds 	clear_bit(BDI_write_congested, &bdi->state);
3321da177e4SLinus Torvalds 	wake_up_all(&nfs_write_congestion);
3333fcfab16SAndrew Morton 	congestion_end(WRITE);
3341da177e4SLinus Torvalds 	return err;
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds /*
3381da177e4SLinus Torvalds  * Insert a write request into an inode
3391da177e4SLinus Torvalds  */
3401da177e4SLinus Torvalds static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
3411da177e4SLinus Torvalds {
3421da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
3431da177e4SLinus Torvalds 	int error;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
3461da177e4SLinus Torvalds 	BUG_ON(error == -EEXIST);
3471da177e4SLinus Torvalds 	if (error)
3481da177e4SLinus Torvalds 		return error;
3491da177e4SLinus Torvalds 	if (!nfsi->npages) {
3501da177e4SLinus Torvalds 		igrab(inode);
3511da177e4SLinus Torvalds 		nfs_begin_data_update(inode);
3521da177e4SLinus Torvalds 		if (nfs_have_delegation(inode, FMODE_WRITE))
3531da177e4SLinus Torvalds 			nfsi->change_attr++;
3541da177e4SLinus Torvalds 	}
355deb7d638STrond Myklebust 	SetPagePrivate(req->wb_page);
356277459d2STrond Myklebust 	set_page_private(req->wb_page, (unsigned long)req);
3571da177e4SLinus Torvalds 	nfsi->npages++;
3581da177e4SLinus Torvalds 	atomic_inc(&req->wb_count);
3591da177e4SLinus Torvalds 	return 0;
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds /*
3631da177e4SLinus Torvalds  * Insert a write request into an inode
3641da177e4SLinus Torvalds  */
3651da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
3661da177e4SLinus Torvalds {
3671da177e4SLinus Torvalds 	struct inode *inode = req->wb_context->dentry->d_inode;
3681da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	BUG_ON (!NFS_WBACK_BUSY(req));
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	spin_lock(&nfsi->req_lock);
373277459d2STrond Myklebust 	set_page_private(req->wb_page, 0);
374deb7d638STrond Myklebust 	ClearPagePrivate(req->wb_page);
3751da177e4SLinus Torvalds 	radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
3761da177e4SLinus Torvalds 	nfsi->npages--;
3771da177e4SLinus Torvalds 	if (!nfsi->npages) {
3781da177e4SLinus Torvalds 		spin_unlock(&nfsi->req_lock);
379951a143bSTrond Myklebust 		nfs_end_data_update(inode);
3801da177e4SLinus Torvalds 		iput(inode);
3811da177e4SLinus Torvalds 	} else
3821da177e4SLinus Torvalds 		spin_unlock(&nfsi->req_lock);
3831da177e4SLinus Torvalds 	nfs_clear_request(req);
3841da177e4SLinus Torvalds 	nfs_release_request(req);
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds /*
3881da177e4SLinus Torvalds  * Add a request to the inode's dirty list.
3891da177e4SLinus Torvalds  */
3901da177e4SLinus Torvalds static void
3911da177e4SLinus Torvalds nfs_mark_request_dirty(struct nfs_page *req)
3921da177e4SLinus Torvalds {
3931da177e4SLinus Torvalds 	struct inode *inode = req->wb_context->dentry->d_inode;
3941da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	spin_lock(&nfsi->req_lock);
3973da28eb1STrond Myklebust 	radix_tree_tag_set(&nfsi->nfs_page_tree,
3983da28eb1STrond Myklebust 			req->wb_index, NFS_PAGE_TAG_DIRTY);
3991da177e4SLinus Torvalds 	nfs_list_add_request(req, &nfsi->dirty);
4001da177e4SLinus Torvalds 	nfsi->ndirty++;
4011da177e4SLinus Torvalds 	spin_unlock(&nfsi->req_lock);
402a1803044STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_PAGES);
4031da177e4SLinus Torvalds }
4041da177e4SLinus Torvalds 
40561822ab5STrond Myklebust static void
40661822ab5STrond Myklebust nfs_redirty_request(struct nfs_page *req)
40761822ab5STrond Myklebust {
40861822ab5STrond Myklebust 	clear_bit(PG_FLUSHING, &req->wb_flags);
40961822ab5STrond Myklebust 	__set_page_dirty_nobuffers(req->wb_page);
41061822ab5STrond Myklebust }
41161822ab5STrond Myklebust 
4121da177e4SLinus Torvalds /*
4131da177e4SLinus Torvalds  * Check if a request is dirty
4141da177e4SLinus Torvalds  */
4151da177e4SLinus Torvalds static inline int
4161da177e4SLinus Torvalds nfs_dirty_request(struct nfs_page *req)
4171da177e4SLinus Torvalds {
418e261f51fSTrond Myklebust 	return test_bit(PG_FLUSHING, &req->wb_flags) == 0;
4191da177e4SLinus Torvalds }
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
4221da177e4SLinus Torvalds /*
4231da177e4SLinus Torvalds  * Add a request to the inode's commit list.
4241da177e4SLinus Torvalds  */
4251da177e4SLinus Torvalds static void
4261da177e4SLinus Torvalds nfs_mark_request_commit(struct nfs_page *req)
4271da177e4SLinus Torvalds {
4281da177e4SLinus Torvalds 	struct inode *inode = req->wb_context->dentry->d_inode;
4291da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	spin_lock(&nfsi->req_lock);
4321da177e4SLinus Torvalds 	nfs_list_add_request(req, &nfsi->commit);
4331da177e4SLinus Torvalds 	nfsi->ncommit++;
4341da177e4SLinus Torvalds 	spin_unlock(&nfsi->req_lock);
435fd39fc85SChristoph Lameter 	inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
436a1803044STrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
4371da177e4SLinus Torvalds }
4381da177e4SLinus Torvalds #endif
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds /*
4411da177e4SLinus Torvalds  * Wait for a request to complete.
4421da177e4SLinus Torvalds  *
4431da177e4SLinus Torvalds  * Interruptible by signals only if mounted with intr flag.
4441da177e4SLinus Torvalds  */
445c42de9ddSTrond Myklebust static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
4461da177e4SLinus Torvalds {
4471da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4481da177e4SLinus Torvalds 	struct nfs_page *req;
4491da177e4SLinus Torvalds 	unsigned long		idx_end, next;
4501da177e4SLinus Torvalds 	unsigned int		res = 0;
4511da177e4SLinus Torvalds 	int			error;
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	if (npages == 0)
4541da177e4SLinus Torvalds 		idx_end = ~0;
4551da177e4SLinus Torvalds 	else
4561da177e4SLinus Torvalds 		idx_end = idx_start + npages - 1;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	next = idx_start;
459c6a556b8STrond Myklebust 	while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
4601da177e4SLinus Torvalds 		if (req->wb_index > idx_end)
4611da177e4SLinus Torvalds 			break;
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 		next = req->wb_index + 1;
464c6a556b8STrond Myklebust 		BUG_ON(!NFS_WBACK_BUSY(req));
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 		atomic_inc(&req->wb_count);
4671da177e4SLinus Torvalds 		spin_unlock(&nfsi->req_lock);
4681da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
4691da177e4SLinus Torvalds 		nfs_release_request(req);
470c42de9ddSTrond Myklebust 		spin_lock(&nfsi->req_lock);
4711da177e4SLinus Torvalds 		if (error < 0)
4721da177e4SLinus Torvalds 			return error;
4731da177e4SLinus Torvalds 		res++;
4741da177e4SLinus Torvalds 	}
4751da177e4SLinus Torvalds 	return res;
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds 
47883715ad5STrond Myklebust static void nfs_cancel_dirty_list(struct list_head *head)
479d2ccddf0STrond Myklebust {
480d2ccddf0STrond Myklebust 	struct nfs_page *req;
481d2ccddf0STrond Myklebust 	while(!list_empty(head)) {
482d2ccddf0STrond Myklebust 		req = nfs_list_entry(head->next);
483d2ccddf0STrond Myklebust 		nfs_list_remove_request(req);
484d2ccddf0STrond Myklebust 		nfs_inode_remove_request(req);
485d2ccddf0STrond Myklebust 		nfs_clear_page_writeback(req);
486d2ccddf0STrond Myklebust 	}
487d2ccddf0STrond Myklebust }
488d2ccddf0STrond Myklebust 
48983715ad5STrond Myklebust static void nfs_cancel_commit_list(struct list_head *head)
49083715ad5STrond Myklebust {
49183715ad5STrond Myklebust 	struct nfs_page *req;
49283715ad5STrond Myklebust 
49383715ad5STrond Myklebust 	while(!list_empty(head)) {
49483715ad5STrond Myklebust 		req = nfs_list_entry(head->next);
495b6dff26aSTrond Myklebust 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
49683715ad5STrond Myklebust 		nfs_list_remove_request(req);
49783715ad5STrond Myklebust 		nfs_inode_remove_request(req);
498b6dff26aSTrond Myklebust 		nfs_unlock_request(req);
49983715ad5STrond Myklebust 	}
50083715ad5STrond Myklebust }
50183715ad5STrond Myklebust 
5021da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
5031da177e4SLinus Torvalds /*
5041da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
5051da177e4SLinus Torvalds  * @inode: NFS inode to scan
5061da177e4SLinus Torvalds  * @dst: destination list
5071da177e4SLinus Torvalds  * @idx_start: lower bound of page->index to scan.
5081da177e4SLinus Torvalds  * @npages: idx_start + npages sets the upper bound to scan.
5091da177e4SLinus Torvalds  *
5101da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
5111da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
5121da177e4SLinus Torvalds  */
5131da177e4SLinus Torvalds static int
5141da177e4SLinus Torvalds nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
5151da177e4SLinus Torvalds {
5161da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
5173da28eb1STrond Myklebust 	int res = 0;
5183da28eb1STrond Myklebust 
5193da28eb1STrond Myklebust 	if (nfsi->ncommit != 0) {
520d2ccddf0STrond Myklebust 		res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
5211da177e4SLinus Torvalds 		nfsi->ncommit -= res;
5221da177e4SLinus Torvalds 		if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
5231da177e4SLinus Torvalds 			printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
5243da28eb1STrond Myklebust 	}
5251da177e4SLinus Torvalds 	return res;
5261da177e4SLinus Torvalds }
527c42de9ddSTrond Myklebust #else
528c42de9ddSTrond Myklebust static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
529c42de9ddSTrond Myklebust {
530c42de9ddSTrond Myklebust 	return 0;
531c42de9ddSTrond Myklebust }
5321da177e4SLinus Torvalds #endif
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
5351da177e4SLinus Torvalds {
5361da177e4SLinus Torvalds 	struct backing_dev_info *bdi = mapping->backing_dev_info;
5371da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
5381da177e4SLinus Torvalds 	int ret = 0;
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 	might_sleep();
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	if (!bdi_write_congested(bdi))
5431da177e4SLinus Torvalds 		return 0;
54491d5b470SChuck Lever 
54591d5b470SChuck Lever 	nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
54691d5b470SChuck Lever 
5471da177e4SLinus Torvalds 	if (intr) {
5481da177e4SLinus Torvalds 		struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
5491da177e4SLinus Torvalds 		sigset_t oldset;
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 		rpc_clnt_sigmask(clnt, &oldset);
5521da177e4SLinus Torvalds 		prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
5531da177e4SLinus Torvalds 		if (bdi_write_congested(bdi)) {
5541da177e4SLinus Torvalds 			if (signalled())
5551da177e4SLinus Torvalds 				ret = -ERESTARTSYS;
5561da177e4SLinus Torvalds 			else
5571da177e4SLinus Torvalds 				schedule();
5581da177e4SLinus Torvalds 		}
5591da177e4SLinus Torvalds 		rpc_clnt_sigunmask(clnt, &oldset);
5601da177e4SLinus Torvalds 	} else {
5611da177e4SLinus Torvalds 		prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
5621da177e4SLinus Torvalds 		if (bdi_write_congested(bdi))
5631da177e4SLinus Torvalds 			schedule();
5641da177e4SLinus Torvalds 	}
5651da177e4SLinus Torvalds 	finish_wait(&nfs_write_congestion, &wait);
5661da177e4SLinus Torvalds 	return ret;
5671da177e4SLinus Torvalds }
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds /*
5711da177e4SLinus Torvalds  * Try to update any existing write request, or create one if there is none.
5721da177e4SLinus Torvalds  * In order to match, the request's credentials must match those of
5731da177e4SLinus Torvalds  * the calling process.
5741da177e4SLinus Torvalds  *
5751da177e4SLinus Torvalds  * Note: Should always be called with the Page Lock held!
5761da177e4SLinus Torvalds  */
5771da177e4SLinus Torvalds static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
578e21195a7STrond Myklebust 		struct page *page, unsigned int offset, unsigned int bytes)
5791da177e4SLinus Torvalds {
580e21195a7STrond Myklebust 	struct inode *inode = page->mapping->host;
5811da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
5821da177e4SLinus Torvalds 	struct nfs_page		*req, *new = NULL;
5831da177e4SLinus Torvalds 	unsigned long		rqend, end;
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	end = offset + bytes;
5861da177e4SLinus Torvalds 
587e21195a7STrond Myklebust 	if (nfs_wait_on_write_congestion(page->mapping, NFS_SERVER(inode)->flags & NFS_MOUNT_INTR))
5881da177e4SLinus Torvalds 		return ERR_PTR(-ERESTARTSYS);
5891da177e4SLinus Torvalds 	for (;;) {
5901da177e4SLinus Torvalds 		/* Loop over all inode entries and see if we find
5911da177e4SLinus Torvalds 		 * A request for the page we wish to update
5921da177e4SLinus Torvalds 		 */
5931da177e4SLinus Torvalds 		spin_lock(&nfsi->req_lock);
594277459d2STrond Myklebust 		req = nfs_page_find_request_locked(page);
5951da177e4SLinus Torvalds 		if (req) {
5961da177e4SLinus Torvalds 			if (!nfs_lock_request_dontget(req)) {
5971da177e4SLinus Torvalds 				int error;
598277459d2STrond Myklebust 
5991da177e4SLinus Torvalds 				spin_unlock(&nfsi->req_lock);
6001da177e4SLinus Torvalds 				error = nfs_wait_on_request(req);
6011da177e4SLinus Torvalds 				nfs_release_request(req);
6021dd594b2SNeil Brown 				if (error < 0) {
6031dd594b2SNeil Brown 					if (new)
6041dd594b2SNeil Brown 						nfs_release_request(new);
6051da177e4SLinus Torvalds 					return ERR_PTR(error);
6061dd594b2SNeil Brown 				}
6071da177e4SLinus Torvalds 				continue;
6081da177e4SLinus Torvalds 			}
6091da177e4SLinus Torvalds 			spin_unlock(&nfsi->req_lock);
6101da177e4SLinus Torvalds 			if (new)
6111da177e4SLinus Torvalds 				nfs_release_request(new);
6121da177e4SLinus Torvalds 			break;
6131da177e4SLinus Torvalds 		}
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 		if (new) {
6161da177e4SLinus Torvalds 			int error;
6171da177e4SLinus Torvalds 			nfs_lock_request_dontget(new);
6181da177e4SLinus Torvalds 			error = nfs_inode_add_request(inode, new);
6191da177e4SLinus Torvalds 			if (error) {
6201da177e4SLinus Torvalds 				spin_unlock(&nfsi->req_lock);
6211da177e4SLinus Torvalds 				nfs_unlock_request(new);
6221da177e4SLinus Torvalds 				return ERR_PTR(error);
6231da177e4SLinus Torvalds 			}
6241da177e4SLinus Torvalds 			spin_unlock(&nfsi->req_lock);
6251da177e4SLinus Torvalds 			return new;
6261da177e4SLinus Torvalds 		}
6271da177e4SLinus Torvalds 		spin_unlock(&nfsi->req_lock);
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 		new = nfs_create_request(ctx, inode, page, offset, bytes);
6301da177e4SLinus Torvalds 		if (IS_ERR(new))
6311da177e4SLinus Torvalds 			return new;
6321da177e4SLinus Torvalds 	}
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	/* We have a request for our page.
6351da177e4SLinus Torvalds 	 * If the creds don't match, or the
6361da177e4SLinus Torvalds 	 * page addresses don't match,
6371da177e4SLinus Torvalds 	 * tell the caller to wait on the conflicting
6381da177e4SLinus Torvalds 	 * request.
6391da177e4SLinus Torvalds 	 */
6401da177e4SLinus Torvalds 	rqend = req->wb_offset + req->wb_bytes;
6411da177e4SLinus Torvalds 	if (req->wb_context != ctx
6421da177e4SLinus Torvalds 	    || req->wb_page != page
6431da177e4SLinus Torvalds 	    || !nfs_dirty_request(req)
6441da177e4SLinus Torvalds 	    || offset > rqend || end < req->wb_offset) {
6451da177e4SLinus Torvalds 		nfs_unlock_request(req);
6461da177e4SLinus Torvalds 		return ERR_PTR(-EBUSY);
6471da177e4SLinus Torvalds 	}
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
6501da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
6511da177e4SLinus Torvalds 		req->wb_offset = offset;
6521da177e4SLinus Torvalds 		req->wb_pgbase = offset;
6531da177e4SLinus Torvalds 		req->wb_bytes = rqend - req->wb_offset;
6541da177e4SLinus Torvalds 	}
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	if (end > rqend)
6571da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	return req;
6601da177e4SLinus Torvalds }
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
6631da177e4SLinus Torvalds {
6641da177e4SLinus Torvalds 	struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
6651da177e4SLinus Torvalds 	struct nfs_page	*req;
6661a54533eSTrond Myklebust 	int do_flush, status;
6671da177e4SLinus Torvalds 	/*
6681da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
6691da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
6701da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
6711da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
6721da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
6731da177e4SLinus Torvalds 	 * dropped page.
6741da177e4SLinus Torvalds 	 */
6751a54533eSTrond Myklebust 	do {
676277459d2STrond Myklebust 		req = nfs_page_find_request(page);
6771a54533eSTrond Myklebust 		if (req == NULL)
6781a54533eSTrond Myklebust 			return 0;
6791a54533eSTrond Myklebust 		do_flush = req->wb_page != page || req->wb_context != ctx
680e261f51fSTrond Myklebust 			|| !nfs_dirty_request(req);
6811da177e4SLinus Torvalds 		nfs_release_request(req);
6821a54533eSTrond Myklebust 		if (!do_flush)
6831a54533eSTrond Myklebust 			return 0;
684277459d2STrond Myklebust 		status = nfs_wb_page(page->mapping->host, page);
6851a54533eSTrond Myklebust 	} while (status == 0);
6861a54533eSTrond Myklebust 	return status;
6871da177e4SLinus Torvalds }
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds /*
6901da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
6911da177e4SLinus Torvalds  *
6921da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
6931da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
6941da177e4SLinus Torvalds  */
6951da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
6961da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
6971da177e4SLinus Torvalds {
6981da177e4SLinus Torvalds 	struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
6991da177e4SLinus Torvalds 	struct inode	*inode = page->mapping->host;
7001da177e4SLinus Torvalds 	int		status = 0;
7011da177e4SLinus Torvalds 
70291d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
70391d5b470SChuck Lever 
7041da177e4SLinus Torvalds 	dprintk("NFS:      nfs_updatepage(%s/%s %d@%Ld)\n",
70501cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_parent->d_name.name,
70601cce933SJosef "Jeff" Sipek 		file->f_path.dentry->d_name.name, count,
7070bbacc40SChuck Lever 		(long long)(page_offset(page) +offset));
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	/* If we're not using byte range locks, and we know the page
7101da177e4SLinus Torvalds 	 * is entirely in cache, it may be more efficient to avoid
7111da177e4SLinus Torvalds 	 * fragmenting write requests.
7121da177e4SLinus Torvalds 	 */
713ab0a3dbeSTrond Myklebust 	if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
71449a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
7151da177e4SLinus Torvalds 		offset = 0;
7161da177e4SLinus Torvalds 	}
7171da177e4SLinus Torvalds 
718e21195a7STrond Myklebust 	status = nfs_writepage_setup(ctx, page, offset, count);
719e261f51fSTrond Myklebust 	__set_page_dirty_nobuffers(page);
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds         dprintk("NFS:      nfs_updatepage returns %d (isize %Ld)\n",
7221da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
7231da177e4SLinus Torvalds 	if (status < 0)
724a301b777STrond Myklebust 		nfs_set_pageerror(page);
7251da177e4SLinus Torvalds 	return status;
7261da177e4SLinus Torvalds }
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds static void nfs_writepage_release(struct nfs_page *req)
7291da177e4SLinus Torvalds {
7301da177e4SLinus Torvalds 	end_page_writeback(req->wb_page);
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
7331da177e4SLinus Torvalds 	if (!PageError(req->wb_page)) {
7341da177e4SLinus Torvalds 		if (NFS_NEED_RESCHED(req)) {
73561822ab5STrond Myklebust 			nfs_redirty_request(req);
7361da177e4SLinus Torvalds 			goto out;
7371da177e4SLinus Torvalds 		} else if (NFS_NEED_COMMIT(req)) {
7381da177e4SLinus Torvalds 			nfs_mark_request_commit(req);
7391da177e4SLinus Torvalds 			goto out;
7401da177e4SLinus Torvalds 		}
7411da177e4SLinus Torvalds 	}
7421da177e4SLinus Torvalds 	nfs_inode_remove_request(req);
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds out:
7451da177e4SLinus Torvalds 	nfs_clear_commit(req);
7461da177e4SLinus Torvalds 	nfs_clear_reschedule(req);
7471da177e4SLinus Torvalds #else
7481da177e4SLinus Torvalds 	nfs_inode_remove_request(req);
7491da177e4SLinus Torvalds #endif
750c6a556b8STrond Myklebust 	nfs_clear_page_writeback(req);
7511da177e4SLinus Torvalds }
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds static inline int flush_task_priority(int how)
7541da177e4SLinus Torvalds {
7551da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
7561da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
7571da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
7581da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
7591da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
7601da177e4SLinus Torvalds 	}
7611da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
7621da177e4SLinus Torvalds }
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds /*
7651da177e4SLinus Torvalds  * Set up the argument/result storage required for the RPC call.
7661da177e4SLinus Torvalds  */
7671da177e4SLinus Torvalds static void nfs_write_rpcsetup(struct nfs_page *req,
7681da177e4SLinus Torvalds 		struct nfs_write_data *data,
769788e7a89STrond Myklebust 		const struct rpc_call_ops *call_ops,
7701da177e4SLinus Torvalds 		unsigned int count, unsigned int offset,
7711da177e4SLinus Torvalds 		int how)
7721da177e4SLinus Torvalds {
7731da177e4SLinus Torvalds 	struct inode		*inode;
774788e7a89STrond Myklebust 	int flags;
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
7771da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 	data->req = req;
7801da177e4SLinus Torvalds 	data->inode = inode = req->wb_context->dentry->d_inode;
7811da177e4SLinus Torvalds 	data->cred = req->wb_context->cred;
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(inode);
7841da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
7851da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
7861da177e4SLinus Torvalds 	data->args.pages  = data->pagevec;
7871da177e4SLinus Torvalds 	data->args.count  = count;
7881da177e4SLinus Torvalds 	data->args.context = req->wb_context;
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
7911da177e4SLinus Torvalds 	data->res.count   = count;
7921da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
7930e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
7941da177e4SLinus Torvalds 
795788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
796788e7a89STrond Myklebust 	flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
797788e7a89STrond Myklebust 	rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
7981da177e4SLinus Torvalds 	NFS_PROTO(inode)->write_setup(data, how);
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 	data->task.tk_priority = flush_task_priority(how);
8011da177e4SLinus Torvalds 	data->task.tk_cookie = (unsigned long)inode;
8021da177e4SLinus Torvalds 
803a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated write call "
804a3f565b1SChuck Lever 		"(req %s/%Ld, %u bytes @ offset %Lu)\n",
8050bbacc40SChuck Lever 		data->task.tk_pid,
8061da177e4SLinus Torvalds 		inode->i_sb->s_id,
8071da177e4SLinus Torvalds 		(long long)NFS_FILEID(inode),
8081da177e4SLinus Torvalds 		count,
8091da177e4SLinus Torvalds 		(unsigned long long)data->args.offset);
8101da177e4SLinus Torvalds }
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds static void nfs_execute_write(struct nfs_write_data *data)
8131da177e4SLinus Torvalds {
8141da177e4SLinus Torvalds 	struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
8151da177e4SLinus Torvalds 	sigset_t oldset;
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	rpc_clnt_sigmask(clnt, &oldset);
8181da177e4SLinus Torvalds 	rpc_execute(&data->task);
8191da177e4SLinus Torvalds 	rpc_clnt_sigunmask(clnt, &oldset);
8201da177e4SLinus Torvalds }
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds /*
8231da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
8241da177e4SLinus Torvalds  * contiguous dirty area on one page.
8251da177e4SLinus Torvalds  */
8267d46a49fSTrond Myklebust static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
8271da177e4SLinus Torvalds {
8281da177e4SLinus Torvalds 	struct nfs_page *req = nfs_list_entry(head->next);
8291da177e4SLinus Torvalds 	struct page *page = req->wb_page;
8301da177e4SLinus Torvalds 	struct nfs_write_data *data;
831e9f7bee1STrond Myklebust 	size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
832e9f7bee1STrond Myklebust 	unsigned int offset;
8331da177e4SLinus Torvalds 	int requests = 0;
8341da177e4SLinus Torvalds 	LIST_HEAD(list);
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	nfs_list_remove_request(req);
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	nbytes = req->wb_bytes;
839e9f7bee1STrond Myklebust 	do {
840e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
841e9f7bee1STrond Myklebust 
842e9f7bee1STrond Myklebust 		data = nfs_writedata_alloc(len);
8431da177e4SLinus Torvalds 		if (!data)
8441da177e4SLinus Torvalds 			goto out_bad;
8451da177e4SLinus Torvalds 		list_add(&data->pages, &list);
8461da177e4SLinus Torvalds 		requests++;
847e9f7bee1STrond Myklebust 		nbytes -= len;
848e9f7bee1STrond Myklebust 	} while (nbytes != 0);
8491da177e4SLinus Torvalds 	atomic_set(&req->wb_complete, requests);
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 	ClearPageError(page);
8521da177e4SLinus Torvalds 	offset = 0;
8531da177e4SLinus Torvalds 	nbytes = req->wb_bytes;
8541da177e4SLinus Torvalds 	do {
8551da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
8561da177e4SLinus Torvalds 		list_del_init(&data->pages);
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 		data->pagevec[0] = page;
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds 		if (nbytes > wsize) {
861788e7a89STrond Myklebust 			nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
862788e7a89STrond Myklebust 					wsize, offset, how);
8631da177e4SLinus Torvalds 			offset += wsize;
8641da177e4SLinus Torvalds 			nbytes -= wsize;
8651da177e4SLinus Torvalds 		} else {
866788e7a89STrond Myklebust 			nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
867788e7a89STrond Myklebust 					nbytes, offset, how);
8681da177e4SLinus Torvalds 			nbytes = 0;
8691da177e4SLinus Torvalds 		}
8701da177e4SLinus Torvalds 		nfs_execute_write(data);
8711da177e4SLinus Torvalds 	} while (nbytes != 0);
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	return 0;
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds out_bad:
8761da177e4SLinus Torvalds 	while (!list_empty(&list)) {
8771da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
8781da177e4SLinus Torvalds 		list_del(&data->pages);
8798aca67f0STrond Myklebust 		nfs_writedata_release(data);
8801da177e4SLinus Torvalds 	}
88161822ab5STrond Myklebust 	nfs_redirty_request(req);
882c6a556b8STrond Myklebust 	nfs_clear_page_writeback(req);
8831da177e4SLinus Torvalds 	return -ENOMEM;
8841da177e4SLinus Torvalds }
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds /*
8871da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
8881da177e4SLinus Torvalds  * The page must have been locked by the caller.
8891da177e4SLinus Torvalds  *
8901da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
8911da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
8921da177e4SLinus Torvalds  * that has been written but not committed.
8931da177e4SLinus Torvalds  */
8947d46a49fSTrond Myklebust static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
8951da177e4SLinus Torvalds {
8961da177e4SLinus Torvalds 	struct nfs_page		*req;
8971da177e4SLinus Torvalds 	struct page		**pages;
8981da177e4SLinus Torvalds 	struct nfs_write_data	*data;
8991da177e4SLinus Torvalds 	unsigned int		count;
9001da177e4SLinus Torvalds 
901e9f7bee1STrond Myklebust 	data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
9021da177e4SLinus Torvalds 	if (!data)
9031da177e4SLinus Torvalds 		goto out_bad;
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	pages = data->pagevec;
9061da177e4SLinus Torvalds 	count = 0;
9071da177e4SLinus Torvalds 	while (!list_empty(head)) {
9081da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
9091da177e4SLinus Torvalds 		nfs_list_remove_request(req);
9101da177e4SLinus Torvalds 		nfs_list_add_request(req, &data->pages);
9111da177e4SLinus Torvalds 		ClearPageError(req->wb_page);
9121da177e4SLinus Torvalds 		*pages++ = req->wb_page;
9131da177e4SLinus Torvalds 		count += req->wb_bytes;
9141da177e4SLinus Torvalds 	}
9151da177e4SLinus Torvalds 	req = nfs_list_entry(data->pages.next);
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 	/* Set up the argument struct */
918788e7a89STrond Myklebust 	nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 	nfs_execute_write(data);
9211da177e4SLinus Torvalds 	return 0;
9221da177e4SLinus Torvalds  out_bad:
9231da177e4SLinus Torvalds 	while (!list_empty(head)) {
9241da177e4SLinus Torvalds 		struct nfs_page *req = nfs_list_entry(head->next);
9251da177e4SLinus Torvalds 		nfs_list_remove_request(req);
92661822ab5STrond Myklebust 		nfs_redirty_request(req);
927c6a556b8STrond Myklebust 		nfs_clear_page_writeback(req);
9281da177e4SLinus Torvalds 	}
9291da177e4SLinus Torvalds 	return -ENOMEM;
9301da177e4SLinus Torvalds }
9311da177e4SLinus Torvalds 
9327d46a49fSTrond Myklebust static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
9331da177e4SLinus Torvalds {
9341da177e4SLinus Torvalds 	LIST_HEAD(one_request);
9357d46a49fSTrond Myklebust 	int (*flush_one)(struct inode *, struct list_head *, int);
9361da177e4SLinus Torvalds 	struct nfs_page	*req;
9377d46a49fSTrond Myklebust 	int wpages = NFS_SERVER(inode)->wpages;
9387d46a49fSTrond Myklebust 	int wsize = NFS_SERVER(inode)->wsize;
9397d46a49fSTrond Myklebust 	int error;
9401da177e4SLinus Torvalds 
9417d46a49fSTrond Myklebust 	flush_one = nfs_flush_one;
9427d46a49fSTrond Myklebust 	if (wsize < PAGE_CACHE_SIZE)
9437d46a49fSTrond Myklebust 		flush_one = nfs_flush_multi;
9447d46a49fSTrond Myklebust 	/* For single writes, FLUSH_STABLE is more efficient */
9457d46a49fSTrond Myklebust 	if (npages <= wpages && npages == NFS_I(inode)->npages
9467d46a49fSTrond Myklebust 			&& nfs_list_entry(head->next)->wb_bytes <= wsize)
9477d46a49fSTrond Myklebust 		how |= FLUSH_STABLE;
9487d46a49fSTrond Myklebust 
9497d46a49fSTrond Myklebust 	do {
9507d46a49fSTrond Myklebust 		nfs_coalesce_requests(head, &one_request, wpages);
9511da177e4SLinus Torvalds 		req = nfs_list_entry(one_request.next);
9527d46a49fSTrond Myklebust 		error = flush_one(inode, &one_request, how);
9531da177e4SLinus Torvalds 		if (error < 0)
9547d46a49fSTrond Myklebust 			goto out_err;
9557d46a49fSTrond Myklebust 	} while (!list_empty(head));
9567d46a49fSTrond Myklebust 	return 0;
9577d46a49fSTrond Myklebust out_err:
9581da177e4SLinus Torvalds 	while (!list_empty(head)) {
9591da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
9601da177e4SLinus Torvalds 		nfs_list_remove_request(req);
96161822ab5STrond Myklebust 		nfs_redirty_request(req);
962c6a556b8STrond Myklebust 		nfs_clear_page_writeback(req);
9631da177e4SLinus Torvalds 	}
9641da177e4SLinus Torvalds 	return error;
9651da177e4SLinus Torvalds }
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds /*
9681da177e4SLinus Torvalds  * Handle a write reply that flushed part of a page.
9691da177e4SLinus Torvalds  */
970788e7a89STrond Myklebust static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
9711da177e4SLinus Torvalds {
972788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
9731da177e4SLinus Torvalds 	struct nfs_page		*req = data->req;
9741da177e4SLinus Torvalds 	struct page		*page = req->wb_page;
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds 	dprintk("NFS: write (%s/%Ld %d@%Ld)",
9771da177e4SLinus Torvalds 		req->wb_context->dentry->d_inode->i_sb->s_id,
9781da177e4SLinus Torvalds 		(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
9791da177e4SLinus Torvalds 		req->wb_bytes,
9801da177e4SLinus Torvalds 		(long long)req_offset(req));
9811da177e4SLinus Torvalds 
982788e7a89STrond Myklebust 	if (nfs_writeback_done(task, data) != 0)
983788e7a89STrond Myklebust 		return;
984788e7a89STrond Myklebust 
985788e7a89STrond Myklebust 	if (task->tk_status < 0) {
986a301b777STrond Myklebust 		nfs_set_pageerror(page);
987788e7a89STrond Myklebust 		req->wb_context->error = task->tk_status;
988788e7a89STrond Myklebust 		dprintk(", error = %d\n", task->tk_status);
9891da177e4SLinus Torvalds 	} else {
9901da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
9911da177e4SLinus Torvalds 		if (data->verf.committed < NFS_FILE_SYNC) {
9921da177e4SLinus Torvalds 			if (!NFS_NEED_COMMIT(req)) {
9931da177e4SLinus Torvalds 				nfs_defer_commit(req);
9941da177e4SLinus Torvalds 				memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
9951da177e4SLinus Torvalds 				dprintk(" defer commit\n");
9961da177e4SLinus Torvalds 			} else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
9971da177e4SLinus Torvalds 				nfs_defer_reschedule(req);
9981da177e4SLinus Torvalds 				dprintk(" server reboot detected\n");
9991da177e4SLinus Torvalds 			}
10001da177e4SLinus Torvalds 		} else
10011da177e4SLinus Torvalds #endif
10021da177e4SLinus Torvalds 			dprintk(" OK\n");
10031da177e4SLinus Torvalds 	}
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	if (atomic_dec_and_test(&req->wb_complete))
10061da177e4SLinus Torvalds 		nfs_writepage_release(req);
10071da177e4SLinus Torvalds }
10081da177e4SLinus Torvalds 
1009788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops = {
1010788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_partial,
1011788e7a89STrond Myklebust 	.rpc_release = nfs_writedata_release,
1012788e7a89STrond Myklebust };
1013788e7a89STrond Myklebust 
10141da177e4SLinus Torvalds /*
10151da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
10161da177e4SLinus Torvalds  *
10171da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
10181da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
10191da177e4SLinus Torvalds  *	  as the page has a write request pending.
10201da177e4SLinus Torvalds  */
1021788e7a89STrond Myklebust static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
10221da177e4SLinus Torvalds {
1023788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
10241da177e4SLinus Torvalds 	struct nfs_page		*req;
10251da177e4SLinus Torvalds 	struct page		*page;
10261da177e4SLinus Torvalds 
1027788e7a89STrond Myklebust 	if (nfs_writeback_done(task, data) != 0)
1028788e7a89STrond Myklebust 		return;
1029788e7a89STrond Myklebust 
10301da177e4SLinus Torvalds 	/* Update attributes as result of writeback. */
10311da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
10321da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
10331da177e4SLinus Torvalds 		nfs_list_remove_request(req);
10341da177e4SLinus Torvalds 		page = req->wb_page;
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 		dprintk("NFS: write (%s/%Ld %d@%Ld)",
10371da177e4SLinus Torvalds 			req->wb_context->dentry->d_inode->i_sb->s_id,
10381da177e4SLinus Torvalds 			(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
10391da177e4SLinus Torvalds 			req->wb_bytes,
10401da177e4SLinus Torvalds 			(long long)req_offset(req));
10411da177e4SLinus Torvalds 
1042788e7a89STrond Myklebust 		if (task->tk_status < 0) {
1043a301b777STrond Myklebust 			nfs_set_pageerror(page);
1044788e7a89STrond Myklebust 			req->wb_context->error = task->tk_status;
10451da177e4SLinus Torvalds 			end_page_writeback(page);
10461da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1047788e7a89STrond Myklebust 			dprintk(", error = %d\n", task->tk_status);
10481da177e4SLinus Torvalds 			goto next;
10491da177e4SLinus Torvalds 		}
10501da177e4SLinus Torvalds 		end_page_writeback(page);
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
10531da177e4SLinus Torvalds 		if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
10541da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
10551da177e4SLinus Torvalds 			dprintk(" OK\n");
10561da177e4SLinus Torvalds 			goto next;
10571da177e4SLinus Torvalds 		}
10581da177e4SLinus Torvalds 		memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
10591da177e4SLinus Torvalds 		nfs_mark_request_commit(req);
10601da177e4SLinus Torvalds 		dprintk(" marked for commit\n");
10611da177e4SLinus Torvalds #else
10621da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
10631da177e4SLinus Torvalds #endif
10641da177e4SLinus Torvalds 	next:
1065c6a556b8STrond Myklebust 		nfs_clear_page_writeback(req);
10661da177e4SLinus Torvalds 	}
10671da177e4SLinus Torvalds }
10681da177e4SLinus Torvalds 
1069788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops = {
1070788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_full,
1071788e7a89STrond Myklebust 	.rpc_release = nfs_writedata_release,
1072788e7a89STrond Myklebust };
1073788e7a89STrond Myklebust 
1074788e7a89STrond Myklebust 
10751da177e4SLinus Torvalds /*
10761da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
10771da177e4SLinus Torvalds  */
1078462d5b32SChuck Lever int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
10791da177e4SLinus Torvalds {
10801da177e4SLinus Torvalds 	struct nfs_writeargs	*argp = &data->args;
10811da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
1082788e7a89STrond Myklebust 	int status;
10831da177e4SLinus Torvalds 
1084a3f565b1SChuck Lever 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n",
10851da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
10861da177e4SLinus Torvalds 
1087f551e44fSChuck Lever 	/*
1088f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1089f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1090f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1091f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1092f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1093f551e44fSChuck Lever 	 */
1094788e7a89STrond Myklebust 	status = NFS_PROTO(data->inode)->write_done(task, data);
1095788e7a89STrond Myklebust 	if (status != 0)
1096788e7a89STrond Myklebust 		return status;
109791d5b470SChuck Lever 	nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
109891d5b470SChuck Lever 
10991da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
11001da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
11011da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
11021da177e4SLinus Torvalds 		 * commit data to stable storage even though we
11031da177e4SLinus Torvalds 		 * requested it.
11041da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
11051da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
11061da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
11071da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
11081da177e4SLinus Torvalds 		 */
11091da177e4SLinus Torvalds 		static unsigned long    complain;
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
11121da177e4SLinus Torvalds 			dprintk("NFS: faulty NFS server %s:"
11131da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
111454ceac45SDavid Howells 				NFS_SERVER(data->inode)->nfs_client->cl_hostname,
11151da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
11161da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
11171da177e4SLinus Torvalds 		}
11181da177e4SLinus Torvalds 	}
11191da177e4SLinus Torvalds #endif
11201da177e4SLinus Torvalds 	/* Is this a short write? */
11211da177e4SLinus Torvalds 	if (task->tk_status >= 0 && resp->count < argp->count) {
11221da177e4SLinus Torvalds 		static unsigned long    complain;
11231da177e4SLinus Torvalds 
112491d5b470SChuck Lever 		nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
112591d5b470SChuck Lever 
11261da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
11271da177e4SLinus Torvalds 		if (resp->count != 0) {
11281da177e4SLinus Torvalds 			/* Was this an NFSv2 write or an NFSv3 stable write? */
11291da177e4SLinus Torvalds 			if (resp->verf->committed != NFS_UNSTABLE) {
11301da177e4SLinus Torvalds 				/* Resend from where the server left off */
11311da177e4SLinus Torvalds 				argp->offset += resp->count;
11321da177e4SLinus Torvalds 				argp->pgbase += resp->count;
11331da177e4SLinus Torvalds 				argp->count -= resp->count;
11341da177e4SLinus Torvalds 			} else {
11351da177e4SLinus Torvalds 				/* Resend as a stable write in order to avoid
11361da177e4SLinus Torvalds 				 * headaches in the case of a server crash.
11371da177e4SLinus Torvalds 				 */
11381da177e4SLinus Torvalds 				argp->stable = NFS_FILE_SYNC;
11391da177e4SLinus Torvalds 			}
11401da177e4SLinus Torvalds 			rpc_restart_call(task);
1141788e7a89STrond Myklebust 			return -EAGAIN;
11421da177e4SLinus Torvalds 		}
11431da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
11441da177e4SLinus Torvalds 			printk(KERN_WARNING
11451da177e4SLinus Torvalds 			       "NFS: Server wrote zero bytes, expected %u.\n",
11461da177e4SLinus Torvalds 					argp->count);
11471da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
11481da177e4SLinus Torvalds 		}
11491da177e4SLinus Torvalds 		/* Can't do anything about it except throw an error. */
11501da177e4SLinus Torvalds 		task->tk_status = -EIO;
11511da177e4SLinus Torvalds 	}
1152788e7a89STrond Myklebust 	return 0;
11531da177e4SLinus Torvalds }
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1157963d8fe5STrond Myklebust void nfs_commit_release(void *wdata)
11581da177e4SLinus Torvalds {
11591da177e4SLinus Torvalds 	nfs_commit_free(wdata);
11601da177e4SLinus Torvalds }
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds /*
11631da177e4SLinus Torvalds  * Set up the argument/result storage required for the RPC call.
11641da177e4SLinus Torvalds  */
11651da177e4SLinus Torvalds static void nfs_commit_rpcsetup(struct list_head *head,
1166788e7a89STrond Myklebust 		struct nfs_write_data *data,
1167788e7a89STrond Myklebust 		int how)
11681da177e4SLinus Torvalds {
11693da28eb1STrond Myklebust 	struct nfs_page		*first;
11701da177e4SLinus Torvalds 	struct inode		*inode;
1171788e7a89STrond Myklebust 	int flags;
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
11741da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 	list_splice_init(head, &data->pages);
11771da177e4SLinus Torvalds 	first = nfs_list_entry(data->pages.next);
11781da177e4SLinus Torvalds 	inode = first->wb_context->dentry->d_inode;
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds 	data->inode	  = inode;
11811da177e4SLinus Torvalds 	data->cred	  = first->wb_context->cred;
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(data->inode);
11843da28eb1STrond Myklebust 	/* Note: we always request a commit of the entire inode */
11853da28eb1STrond Myklebust 	data->args.offset = 0;
11863da28eb1STrond Myklebust 	data->args.count  = 0;
11873da28eb1STrond Myklebust 	data->res.count   = 0;
11881da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
11891da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
11900e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
11911da177e4SLinus Torvalds 
1192788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
1193788e7a89STrond Myklebust 	flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1194788e7a89STrond Myklebust 	rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
11951da177e4SLinus Torvalds 	NFS_PROTO(inode)->commit_setup(data, how);
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds 	data->task.tk_priority = flush_task_priority(how);
11981da177e4SLinus Torvalds 	data->task.tk_cookie = (unsigned long)inode;
11991da177e4SLinus Torvalds 
1200a3f565b1SChuck Lever 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid);
12011da177e4SLinus Torvalds }
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds /*
12041da177e4SLinus Torvalds  * Commit dirty pages
12051da177e4SLinus Torvalds  */
12061da177e4SLinus Torvalds static int
120740859d7eSChuck Lever nfs_commit_list(struct inode *inode, struct list_head *head, int how)
12081da177e4SLinus Torvalds {
12091da177e4SLinus Torvalds 	struct nfs_write_data	*data;
12101da177e4SLinus Torvalds 	struct nfs_page         *req;
12111da177e4SLinus Torvalds 
1212e9f7bee1STrond Myklebust 	data = nfs_commit_alloc();
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 	if (!data)
12151da177e4SLinus Torvalds 		goto out_bad;
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 	/* Set up the argument struct */
12181da177e4SLinus Torvalds 	nfs_commit_rpcsetup(head, data, how);
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds 	nfs_execute_write(data);
12211da177e4SLinus Torvalds 	return 0;
12221da177e4SLinus Torvalds  out_bad:
12231da177e4SLinus Torvalds 	while (!list_empty(head)) {
12241da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
12251da177e4SLinus Torvalds 		nfs_list_remove_request(req);
12261da177e4SLinus Torvalds 		nfs_mark_request_commit(req);
122783715ad5STrond Myklebust 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
12285c2d97cbSTrond Myklebust 		nfs_clear_page_writeback(req);
12291da177e4SLinus Torvalds 	}
12301da177e4SLinus Torvalds 	return -ENOMEM;
12311da177e4SLinus Torvalds }
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds /*
12341da177e4SLinus Torvalds  * COMMIT call returned
12351da177e4SLinus Torvalds  */
1236788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
12371da177e4SLinus Torvalds {
1238963d8fe5STrond Myklebust 	struct nfs_write_data	*data = calldata;
12391da177e4SLinus Torvalds 	struct nfs_page		*req;
12401da177e4SLinus Torvalds 
1241a3f565b1SChuck Lever         dprintk("NFS: %5u nfs_commit_done (status %d)\n",
12421da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
12431da177e4SLinus Torvalds 
1244788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1245788e7a89STrond Myklebust 	if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1246788e7a89STrond Myklebust 		return;
1247788e7a89STrond Myklebust 
12481da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
12491da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
12501da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1251fd39fc85SChristoph Lameter 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds 		dprintk("NFS: commit (%s/%Ld %d@%Ld)",
12541da177e4SLinus Torvalds 			req->wb_context->dentry->d_inode->i_sb->s_id,
12551da177e4SLinus Torvalds 			(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
12561da177e4SLinus Torvalds 			req->wb_bytes,
12571da177e4SLinus Torvalds 			(long long)req_offset(req));
12581da177e4SLinus Torvalds 		if (task->tk_status < 0) {
12591da177e4SLinus Torvalds 			req->wb_context->error = task->tk_status;
12601da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
12611da177e4SLinus Torvalds 			dprintk(", error = %d\n", task->tk_status);
12621da177e4SLinus Torvalds 			goto next;
12631da177e4SLinus Torvalds 		}
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
12661da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
12671da177e4SLinus Torvalds 		if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
12681da177e4SLinus Torvalds 			/* We have a match */
12691da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
12701da177e4SLinus Torvalds 			dprintk(" OK\n");
12711da177e4SLinus Torvalds 			goto next;
12721da177e4SLinus Torvalds 		}
12731da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
12741da177e4SLinus Torvalds 		dprintk(" mismatch\n");
127561822ab5STrond Myklebust 		nfs_redirty_request(req);
12761da177e4SLinus Torvalds 	next:
1277c6a556b8STrond Myklebust 		nfs_clear_page_writeback(req);
12781da177e4SLinus Torvalds 	}
12791da177e4SLinus Torvalds }
1280788e7a89STrond Myklebust 
1281788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
1282788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1283788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1284788e7a89STrond Myklebust };
1285c42de9ddSTrond Myklebust #else
1286c42de9ddSTrond Myklebust static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1287c42de9ddSTrond Myklebust {
1288c42de9ddSTrond Myklebust 	return 0;
1289c42de9ddSTrond Myklebust }
12901da177e4SLinus Torvalds #endif
12911da177e4SLinus Torvalds 
12923f442547STrond Myklebust static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
12931da177e4SLinus Torvalds {
129428c6925fSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(mapping->host);
12951da177e4SLinus Torvalds 	LIST_HEAD(head);
12963f442547STrond Myklebust 	long res;
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds 	spin_lock(&nfsi->req_lock);
12993f442547STrond Myklebust 	res = nfs_scan_dirty(mapping, wbc, &head);
13001da177e4SLinus Torvalds 	spin_unlock(&nfsi->req_lock);
1301ab0a3dbeSTrond Myklebust 	if (res) {
130228c6925fSTrond Myklebust 		int error = nfs_flush_list(mapping->host, &head, res, how);
13031da177e4SLinus Torvalds 		if (error < 0)
13041da177e4SLinus Torvalds 			return error;
13057d46a49fSTrond Myklebust 	}
13061da177e4SLinus Torvalds 	return res;
13071da177e4SLinus Torvalds }
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
13103da28eb1STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
13111da177e4SLinus Torvalds {
13121da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
13131da177e4SLinus Torvalds 	LIST_HEAD(head);
13147d46a49fSTrond Myklebust 	int res;
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds 	spin_lock(&nfsi->req_lock);
13173da28eb1STrond Myklebust 	res = nfs_scan_commit(inode, &head, 0, 0);
13183da28eb1STrond Myklebust 	spin_unlock(&nfsi->req_lock);
13191da177e4SLinus Torvalds 	if (res) {
13207d46a49fSTrond Myklebust 		int error = nfs_commit_list(inode, &head, how);
13211da177e4SLinus Torvalds 		if (error < 0)
13221da177e4SLinus Torvalds 			return error;
13233da28eb1STrond Myklebust 	}
13241da177e4SLinus Torvalds 	return res;
13251da177e4SLinus Torvalds }
13261da177e4SLinus Torvalds #endif
13271da177e4SLinus Torvalds 
13281c75950bSTrond Myklebust long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
13291da177e4SLinus Torvalds {
13301c75950bSTrond Myklebust 	struct inode *inode = mapping->host;
1331c42de9ddSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
13321c75950bSTrond Myklebust 	unsigned long idx_start, idx_end;
13331c75950bSTrond Myklebust 	unsigned int npages = 0;
1334c42de9ddSTrond Myklebust 	LIST_HEAD(head);
133570b9ecbdSTrond Myklebust 	int nocommit = how & FLUSH_NOCOMMIT;
13363f442547STrond Myklebust 	long pages, ret;
13371da177e4SLinus Torvalds 
13381c75950bSTrond Myklebust 	/* FIXME */
13391c75950bSTrond Myklebust 	if (wbc->range_cyclic)
13401c75950bSTrond Myklebust 		idx_start = 0;
13411c75950bSTrond Myklebust 	else {
13421c75950bSTrond Myklebust 		idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
13431c75950bSTrond Myklebust 		idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
13441c75950bSTrond Myklebust 		if (idx_end > idx_start) {
13451c75950bSTrond Myklebust 			unsigned long l_npages = 1 + idx_end - idx_start;
13461c75950bSTrond Myklebust 			npages = l_npages;
13471c75950bSTrond Myklebust 			if (sizeof(npages) != sizeof(l_npages) &&
13481c75950bSTrond Myklebust 					(unsigned long)npages != l_npages)
13491c75950bSTrond Myklebust 				npages = 0;
13501c75950bSTrond Myklebust 		}
13511c75950bSTrond Myklebust 	}
1352c42de9ddSTrond Myklebust 	how &= ~FLUSH_NOCOMMIT;
1353c42de9ddSTrond Myklebust 	spin_lock(&nfsi->req_lock);
13541da177e4SLinus Torvalds 	do {
13551c75950bSTrond Myklebust 		wbc->pages_skipped = 0;
1356c42de9ddSTrond Myklebust 		ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1357c42de9ddSTrond Myklebust 		if (ret != 0)
1358c42de9ddSTrond Myklebust 			continue;
13591c75950bSTrond Myklebust 		pages = nfs_scan_dirty(mapping, wbc, &head);
1360c42de9ddSTrond Myklebust 		if (pages != 0) {
1361c42de9ddSTrond Myklebust 			spin_unlock(&nfsi->req_lock);
1362e8e058e8STrond Myklebust 			if (how & FLUSH_INVALIDATE) {
136383715ad5STrond Myklebust 				nfs_cancel_dirty_list(&head);
1364e8e058e8STrond Myklebust 				ret = pages;
1365e8e058e8STrond Myklebust 			} else
1366c42de9ddSTrond Myklebust 				ret = nfs_flush_list(inode, &head, pages, how);
1367c42de9ddSTrond Myklebust 			spin_lock(&nfsi->req_lock);
136870b9ecbdSTrond Myklebust 			continue;
136970b9ecbdSTrond Myklebust 		}
13701c75950bSTrond Myklebust 		if (wbc->pages_skipped != 0)
13711c75950bSTrond Myklebust 			continue;
1372c42de9ddSTrond Myklebust 		if (nocommit)
1373c42de9ddSTrond Myklebust 			break;
1374d2ccddf0STrond Myklebust 		pages = nfs_scan_commit(inode, &head, idx_start, npages);
13751c75950bSTrond Myklebust 		if (pages == 0) {
13761c75950bSTrond Myklebust 			if (wbc->pages_skipped != 0)
13771c75950bSTrond Myklebust 				continue;
1378c42de9ddSTrond Myklebust 			break;
13791c75950bSTrond Myklebust 		}
1380d2ccddf0STrond Myklebust 		if (how & FLUSH_INVALIDATE) {
1381d2ccddf0STrond Myklebust 			spin_unlock(&nfsi->req_lock);
138283715ad5STrond Myklebust 			nfs_cancel_commit_list(&head);
1383e8e058e8STrond Myklebust 			ret = pages;
1384d2ccddf0STrond Myklebust 			spin_lock(&nfsi->req_lock);
1385d2ccddf0STrond Myklebust 			continue;
1386d2ccddf0STrond Myklebust 		}
1387d2ccddf0STrond Myklebust 		pages += nfs_scan_commit(inode, &head, 0, 0);
1388c42de9ddSTrond Myklebust 		spin_unlock(&nfsi->req_lock);
1389c42de9ddSTrond Myklebust 		ret = nfs_commit_list(inode, &head, how);
1390c42de9ddSTrond Myklebust 		spin_lock(&nfsi->req_lock);
1391c42de9ddSTrond Myklebust 	} while (ret >= 0);
1392c42de9ddSTrond Myklebust 	spin_unlock(&nfsi->req_lock);
1393c42de9ddSTrond Myklebust 	return ret;
13941da177e4SLinus Torvalds }
13951da177e4SLinus Torvalds 
13961c75950bSTrond Myklebust /*
13971c75950bSTrond Myklebust  * flush the inode to disk.
13981c75950bSTrond Myklebust  */
13991c75950bSTrond Myklebust int nfs_wb_all(struct inode *inode)
14001c75950bSTrond Myklebust {
14011c75950bSTrond Myklebust 	struct address_space *mapping = inode->i_mapping;
14021c75950bSTrond Myklebust 	struct writeback_control wbc = {
14031c75950bSTrond Myklebust 		.bdi = mapping->backing_dev_info,
14041c75950bSTrond Myklebust 		.sync_mode = WB_SYNC_ALL,
14051c75950bSTrond Myklebust 		.nr_to_write = LONG_MAX,
140661822ab5STrond Myklebust 		.for_writepages = 1,
14071c75950bSTrond Myklebust 		.range_cyclic = 1,
14081c75950bSTrond Myklebust 	};
14091c75950bSTrond Myklebust 	int ret;
14101c75950bSTrond Myklebust 
141161822ab5STrond Myklebust 	ret = generic_writepages(mapping, &wbc);
141261822ab5STrond Myklebust 	if (ret < 0)
141361822ab5STrond Myklebust 		goto out;
14141c75950bSTrond Myklebust 	ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
14151c75950bSTrond Myklebust 	if (ret >= 0)
14161c75950bSTrond Myklebust 		return 0;
141761822ab5STrond Myklebust out:
1418e507d9ebSTrond Myklebust 	__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
14191c75950bSTrond Myklebust 	return ret;
14201c75950bSTrond Myklebust }
14211c75950bSTrond Myklebust 
14221c75950bSTrond Myklebust int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
14231c75950bSTrond Myklebust {
14241c75950bSTrond Myklebust 	struct writeback_control wbc = {
14251c75950bSTrond Myklebust 		.bdi = mapping->backing_dev_info,
14261c75950bSTrond Myklebust 		.sync_mode = WB_SYNC_ALL,
14271c75950bSTrond Myklebust 		.nr_to_write = LONG_MAX,
14281c75950bSTrond Myklebust 		.range_start = range_start,
14291c75950bSTrond Myklebust 		.range_end = range_end,
143061822ab5STrond Myklebust 		.for_writepages = 1,
14311c75950bSTrond Myklebust 	};
14321c75950bSTrond Myklebust 	int ret;
14331c75950bSTrond Myklebust 
143461822ab5STrond Myklebust 	if (!(how & FLUSH_NOWRITEPAGE)) {
143561822ab5STrond Myklebust 		ret = generic_writepages(mapping, &wbc);
143661822ab5STrond Myklebust 		if (ret < 0)
143761822ab5STrond Myklebust 			goto out;
143861822ab5STrond Myklebust 	}
14391c75950bSTrond Myklebust 	ret = nfs_sync_mapping_wait(mapping, &wbc, how);
14401c75950bSTrond Myklebust 	if (ret >= 0)
14411c75950bSTrond Myklebust 		return 0;
144261822ab5STrond Myklebust out:
1443e507d9ebSTrond Myklebust 	__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
14441c75950bSTrond Myklebust 	return ret;
14451c75950bSTrond Myklebust }
14461c75950bSTrond Myklebust 
144761822ab5STrond Myklebust int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
14481c75950bSTrond Myklebust {
14491c75950bSTrond Myklebust 	loff_t range_start = page_offset(page);
14501c75950bSTrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
14514d770ccfSTrond Myklebust 	struct writeback_control wbc = {
14524d770ccfSTrond Myklebust 		.bdi = page->mapping->backing_dev_info,
14534d770ccfSTrond Myklebust 		.sync_mode = WB_SYNC_ALL,
14544d770ccfSTrond Myklebust 		.nr_to_write = LONG_MAX,
14554d770ccfSTrond Myklebust 		.range_start = range_start,
14564d770ccfSTrond Myklebust 		.range_end = range_end,
14574d770ccfSTrond Myklebust 	};
14584d770ccfSTrond Myklebust 	int ret;
14591c75950bSTrond Myklebust 
14604d770ccfSTrond Myklebust 	BUG_ON(!PageLocked(page));
14614d770ccfSTrond Myklebust 	if (!(how & FLUSH_NOWRITEPAGE) && clear_page_dirty_for_io(page)) {
14624d770ccfSTrond Myklebust 		ret = nfs_writepage_locked(page, &wbc);
14634d770ccfSTrond Myklebust 		if (ret < 0)
14644d770ccfSTrond Myklebust 			goto out;
14654d770ccfSTrond Myklebust 	}
1466f40313acSTrond Myklebust 	if (!PagePrivate(page))
1467f40313acSTrond Myklebust 		return 0;
14684d770ccfSTrond Myklebust 	ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
14694d770ccfSTrond Myklebust 	if (ret >= 0)
14704d770ccfSTrond Myklebust 		return 0;
14714d770ccfSTrond Myklebust out:
1472e507d9ebSTrond Myklebust 	__mark_inode_dirty(inode, I_DIRTY_PAGES);
14734d770ccfSTrond Myklebust 	return ret;
14741c75950bSTrond Myklebust }
14751c75950bSTrond Myklebust 
14761c75950bSTrond Myklebust /*
14771c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
14781c75950bSTrond Myklebust  */
14791c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page* page)
14801c75950bSTrond Myklebust {
14814d770ccfSTrond Myklebust 	return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
14821c75950bSTrond Myklebust }
14831c75950bSTrond Myklebust 
14841a54533eSTrond Myklebust int nfs_set_page_dirty(struct page *page)
14851a54533eSTrond Myklebust {
14861a54533eSTrond Myklebust 	struct nfs_page *req;
14871a54533eSTrond Myklebust 
14881a54533eSTrond Myklebust 	req = nfs_page_find_request(page);
14891a54533eSTrond Myklebust 	if (req != NULL) {
14901a54533eSTrond Myklebust 		/* Mark any existing write requests for flushing */
14911a54533eSTrond Myklebust 		set_bit(PG_NEED_FLUSH, &req->wb_flags);
14921a54533eSTrond Myklebust 		nfs_release_request(req);
14931a54533eSTrond Myklebust 	}
14941a54533eSTrond Myklebust 	return __set_page_dirty_nobuffers(page);
14951a54533eSTrond Myklebust }
14961a54533eSTrond Myklebust 
14971c75950bSTrond Myklebust 
1498f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
14991da177e4SLinus Torvalds {
15001da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
15011da177e4SLinus Torvalds 					     sizeof(struct nfs_write_data),
15021da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
15031da177e4SLinus Torvalds 					     NULL, NULL);
15041da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
15051da177e4SLinus Torvalds 		return -ENOMEM;
15061da177e4SLinus Torvalds 
150793d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
15081da177e4SLinus Torvalds 						     nfs_wdata_cachep);
15091da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
15101da177e4SLinus Torvalds 		return -ENOMEM;
15111da177e4SLinus Torvalds 
151293d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
15131da177e4SLinus Torvalds 						      nfs_wdata_cachep);
15141da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
15151da177e4SLinus Torvalds 		return -ENOMEM;
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds 	return 0;
15181da177e4SLinus Torvalds }
15191da177e4SLinus Torvalds 
1520266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
15211da177e4SLinus Torvalds {
15221da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
15231da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
15241a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
15251da177e4SLinus Torvalds }
15261da177e4SLinus Torvalds 
1527