xref: /linux/fs/nfs/write.c (revision 49a70f278658894d2899824cd4037095fb6711fe)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/fs/nfs/write.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Writing file data over NFS.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * We do it like this: When a (user) process wishes to write data to an
71da177e4SLinus Torvalds  * NFS file, a write request is allocated that contains the RPC task data
81da177e4SLinus Torvalds  * plus some info on the page to be written, and added to the inode's
91da177e4SLinus Torvalds  * write chain. If the process writes past the end of the page, an async
101da177e4SLinus Torvalds  * RPC call to write the page is scheduled immediately; otherwise, the call
111da177e4SLinus Torvalds  * is delayed for a few seconds.
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * Just like readahead, no async I/O is performed if wsize < PAGE_SIZE.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * Write requests are kept on the inode's writeback list. Each entry in
161da177e4SLinus Torvalds  * that list references the page (portion) to be written. When the
171da177e4SLinus Torvalds  * cache timeout has expired, the RPC task is woken up, and tries to
181da177e4SLinus Torvalds  * lock the page. As soon as it manages to do so, the request is moved
191da177e4SLinus Torvalds  * from the writeback list to the writelock list.
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  * Note: we must make sure never to confuse the inode passed in the
221da177e4SLinus Torvalds  * write_page request with the one in page->inode. As far as I understand
231da177e4SLinus Torvalds  * it, these are different when doing a swap-out.
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  * To understand everything that goes on here and in the NFS read code,
261da177e4SLinus Torvalds  * one should be aware that a page is locked in exactly one of the following
271da177e4SLinus Torvalds  * cases:
281da177e4SLinus Torvalds  *
291da177e4SLinus Torvalds  *  -	A write request is in progress.
301da177e4SLinus Torvalds  *  -	A user process is in generic_file_write/nfs_update_page
311da177e4SLinus Torvalds  *  -	A user process is in generic_file_read
321da177e4SLinus Torvalds  *
331da177e4SLinus Torvalds  * Also note that because of the way pages are invalidated in
341da177e4SLinus Torvalds  * nfs_revalidate_inode, the following assertions hold:
351da177e4SLinus Torvalds  *
361da177e4SLinus Torvalds  *  -	If a page is dirty, there will be no read requests (a page will
371da177e4SLinus Torvalds  *	not be re-read unless invalidated by nfs_revalidate_inode).
381da177e4SLinus Torvalds  *  -	If the page is not uptodate, there will be no pending write
391da177e4SLinus Torvalds  *	requests, and no process will be in nfs_update_page.
401da177e4SLinus Torvalds  *
411da177e4SLinus Torvalds  * FIXME: Interaction with the vmscan routines is not optimal yet.
421da177e4SLinus Torvalds  * Either vmscan must be made nfs-savvy, or we need a different page
431da177e4SLinus Torvalds  * reclaim concept that supports something like FS-independent
441da177e4SLinus Torvalds  * buffer_heads with a b_ops-> field.
451da177e4SLinus Torvalds  *
461da177e4SLinus Torvalds  * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
471da177e4SLinus Torvalds  */
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds #include <linux/types.h>
501da177e4SLinus Torvalds #include <linux/slab.h>
511da177e4SLinus Torvalds #include <linux/mm.h>
521da177e4SLinus Torvalds #include <linux/pagemap.h>
531da177e4SLinus Torvalds #include <linux/file.h>
541da177e4SLinus Torvalds #include <linux/writeback.h>
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
571da177e4SLinus Torvalds #include <linux/nfs_fs.h>
581da177e4SLinus Torvalds #include <linux/nfs_mount.h>
591da177e4SLinus Torvalds #include <linux/nfs_page.h>
603fcfab16SAndrew Morton #include <linux/backing-dev.h>
613fcfab16SAndrew Morton 
621da177e4SLinus Torvalds #include <asm/uaccess.h>
631da177e4SLinus Torvalds #include <linux/smp_lock.h>
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds #include "delegation.h"
66*49a70f27STrond Myklebust #include "internal.h"
6791d5b470SChuck Lever #include "iostat.h"
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds #define MIN_POOL_WRITE		(32)
721da177e4SLinus Torvalds #define MIN_POOL_COMMIT		(4)
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /*
751da177e4SLinus Torvalds  * Local function declarations
761da177e4SLinus Torvalds  */
771da177e4SLinus Torvalds static struct nfs_page * nfs_update_request(struct nfs_open_context*,
781da177e4SLinus Torvalds 					    struct inode *,
791da177e4SLinus Torvalds 					    struct page *,
801da177e4SLinus Torvalds 					    unsigned int, unsigned int);
811da177e4SLinus Torvalds static int nfs_wait_on_write_congestion(struct address_space *, int);
821da177e4SLinus Torvalds static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
833f442547STrond Myklebust static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how);
841c75950bSTrond Myklebust static int nfs_wb_page_priority(struct inode *inode, struct page *page, int how);
85788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops;
86788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops;
87788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops;
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds static kmem_cache_t *nfs_wdata_cachep;
903feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool;
911da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
941da177e4SLinus Torvalds 
95e9f7bee1STrond Myklebust struct nfs_write_data *nfs_commit_alloc(void)
961da177e4SLinus Torvalds {
971da177e4SLinus Torvalds 	struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
9840859d7eSChuck Lever 
991da177e4SLinus Torvalds 	if (p) {
1001da177e4SLinus Torvalds 		memset(p, 0, sizeof(*p));
1011da177e4SLinus Torvalds 		INIT_LIST_HEAD(&p->pages);
1021da177e4SLinus Torvalds 	}
1031da177e4SLinus Torvalds 	return p;
1041da177e4SLinus Torvalds }
1051da177e4SLinus Torvalds 
1068aca67f0STrond Myklebust void nfs_commit_rcu_free(struct rcu_head *head)
1071da177e4SLinus Torvalds {
1088aca67f0STrond Myklebust 	struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
10940859d7eSChuck Lever 	if (p && (p->pagevec != &p->page_array[0]))
11040859d7eSChuck Lever 		kfree(p->pagevec);
1111da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
1148aca67f0STrond Myklebust void nfs_commit_free(struct nfs_write_data *wdata)
1158aca67f0STrond Myklebust {
1168aca67f0STrond Myklebust 	call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
1178aca67f0STrond Myklebust }
1188aca67f0STrond Myklebust 
119e9f7bee1STrond Myklebust struct nfs_write_data *nfs_writedata_alloc(size_t len)
1203feb2d49STrond Myklebust {
121e9f7bee1STrond Myklebust 	unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1223feb2d49STrond Myklebust 	struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS);
1233feb2d49STrond Myklebust 
1243feb2d49STrond Myklebust 	if (p) {
1253feb2d49STrond Myklebust 		memset(p, 0, sizeof(*p));
1263feb2d49STrond Myklebust 		INIT_LIST_HEAD(&p->pages);
127e9f7bee1STrond Myklebust 		p->npages = pagecount;
1280d0b5cb3SChuck Lever 		if (pagecount <= ARRAY_SIZE(p->page_array))
1290d0b5cb3SChuck Lever 			p->pagevec = p->page_array;
1303feb2d49STrond Myklebust 		else {
1310d0b5cb3SChuck Lever 			p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
1320d0b5cb3SChuck Lever 			if (!p->pagevec) {
1333feb2d49STrond Myklebust 				mempool_free(p, nfs_wdata_mempool);
1343feb2d49STrond Myklebust 				p = NULL;
1353feb2d49STrond Myklebust 			}
1363feb2d49STrond Myklebust 		}
1373feb2d49STrond Myklebust 	}
1383feb2d49STrond Myklebust 	return p;
1393feb2d49STrond Myklebust }
1403feb2d49STrond Myklebust 
1418aca67f0STrond Myklebust static void nfs_writedata_rcu_free(struct rcu_head *head)
1423feb2d49STrond Myklebust {
1438aca67f0STrond Myklebust 	struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
1443feb2d49STrond Myklebust 	if (p && (p->pagevec != &p->page_array[0]))
1453feb2d49STrond Myklebust 		kfree(p->pagevec);
1463feb2d49STrond Myklebust 	mempool_free(p, nfs_wdata_mempool);
1473feb2d49STrond Myklebust }
1483feb2d49STrond Myklebust 
1498aca67f0STrond Myklebust static void nfs_writedata_free(struct nfs_write_data *wdata)
1508aca67f0STrond Myklebust {
1518aca67f0STrond Myklebust 	call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
1528aca67f0STrond Myklebust }
1538aca67f0STrond Myklebust 
154963d8fe5STrond Myklebust void nfs_writedata_release(void *wdata)
1551da177e4SLinus Torvalds {
1561da177e4SLinus Torvalds 	nfs_writedata_free(wdata);
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
159277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request_locked(struct page *page)
160277459d2STrond Myklebust {
161277459d2STrond Myklebust 	struct nfs_page *req = NULL;
162277459d2STrond Myklebust 
163277459d2STrond Myklebust 	if (PagePrivate(page)) {
164277459d2STrond Myklebust 		req = (struct nfs_page *)page_private(page);
165277459d2STrond Myklebust 		if (req != NULL)
166277459d2STrond Myklebust 			atomic_inc(&req->wb_count);
167277459d2STrond Myklebust 	}
168277459d2STrond Myklebust 	return req;
169277459d2STrond Myklebust }
170277459d2STrond Myklebust 
171277459d2STrond Myklebust static struct nfs_page *nfs_page_find_request(struct page *page)
172277459d2STrond Myklebust {
173277459d2STrond Myklebust 	struct nfs_page *req = NULL;
174277459d2STrond Myklebust 	spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
175277459d2STrond Myklebust 
176277459d2STrond Myklebust 	spin_lock(req_lock);
177277459d2STrond Myklebust 	req = nfs_page_find_request_locked(page);
178277459d2STrond Myklebust 	spin_unlock(req_lock);
179277459d2STrond Myklebust 	return req;
180277459d2STrond Myklebust }
181277459d2STrond Myklebust 
1821da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1831da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1841da177e4SLinus Torvalds {
1851da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
1861da177e4SLinus Torvalds 	loff_t end, i_size = i_size_read(inode);
1871da177e4SLinus Torvalds 	unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	if (i_size > 0 && page->index < end_index)
1901da177e4SLinus Torvalds 		return;
1911da177e4SLinus Torvalds 	end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
1921da177e4SLinus Torvalds 	if (i_size >= end)
1931da177e4SLinus Torvalds 		return;
19491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
1951da177e4SLinus Torvalds 	i_size_write(inode, end);
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
1991da177e4SLinus Torvalds  * covers the full page.
2001da177e4SLinus Torvalds  */
2011da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
2021da177e4SLinus Torvalds {
2031da177e4SLinus Torvalds 	if (PageUptodate(page))
2041da177e4SLinus Torvalds 		return;
2051da177e4SLinus Torvalds 	if (base != 0)
2061da177e4SLinus Torvalds 		return;
207*49a70f27STrond Myklebust 	if (count != nfs_page_length(page))
2081da177e4SLinus Torvalds 		return;
209*49a70f27STrond Myklebust 	if (count != PAGE_CACHE_SIZE)
2101da177e4SLinus Torvalds 		memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
2111da177e4SLinus Torvalds 	SetPageUptodate(page);
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds /*
2151da177e4SLinus Torvalds  * Write a page synchronously.
2161da177e4SLinus Torvalds  * Offset is the data offset within the page.
2171da177e4SLinus Torvalds  */
2181da177e4SLinus Torvalds static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
2191da177e4SLinus Torvalds 		struct page *page, unsigned int offset, unsigned int count,
2201da177e4SLinus Torvalds 		int how)
2211da177e4SLinus Torvalds {
2221da177e4SLinus Torvalds 	unsigned int	wsize = NFS_SERVER(inode)->wsize;
2231da177e4SLinus Torvalds 	int		result, written = 0;
2241da177e4SLinus Torvalds 	struct nfs_write_data *wdata;
2251da177e4SLinus Torvalds 
226e9f7bee1STrond Myklebust 	wdata = nfs_writedata_alloc(wsize);
2271da177e4SLinus Torvalds 	if (!wdata)
2281da177e4SLinus Torvalds 		return -ENOMEM;
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	wdata->flags = how;
2311da177e4SLinus Torvalds 	wdata->cred = ctx->cred;
2321da177e4SLinus Torvalds 	wdata->inode = inode;
2331da177e4SLinus Torvalds 	wdata->args.fh = NFS_FH(inode);
2341da177e4SLinus Torvalds 	wdata->args.context = ctx;
2351da177e4SLinus Torvalds 	wdata->args.pages = &page;
2361da177e4SLinus Torvalds 	wdata->args.stable = NFS_FILE_SYNC;
2371da177e4SLinus Torvalds 	wdata->args.pgbase = offset;
2381da177e4SLinus Torvalds 	wdata->args.count = wsize;
2391da177e4SLinus Torvalds 	wdata->res.fattr = &wdata->fattr;
2401da177e4SLinus Torvalds 	wdata->res.verf = &wdata->verf;
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	dprintk("NFS:      nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
2431da177e4SLinus Torvalds 		inode->i_sb->s_id,
2441da177e4SLinus Torvalds 		(long long)NFS_FILEID(inode),
2451da177e4SLinus Torvalds 		count, (long long)(page_offset(page) + offset));
2461da177e4SLinus Torvalds 
247bb713d6dSTrond Myklebust 	set_page_writeback(page);
2481da177e4SLinus Torvalds 	nfs_begin_data_update(inode);
2491da177e4SLinus Torvalds 	do {
2501da177e4SLinus Torvalds 		if (count < wsize)
2511da177e4SLinus Torvalds 			wdata->args.count = count;
2521da177e4SLinus Torvalds 		wdata->args.offset = page_offset(page) + wdata->args.pgbase;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 		result = NFS_PROTO(inode)->write(wdata);
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 		if (result < 0) {
2571da177e4SLinus Torvalds 			/* Must mark the page invalid after I/O error */
2581da177e4SLinus Torvalds 			ClearPageUptodate(page);
2591da177e4SLinus Torvalds 			goto io_error;
2601da177e4SLinus Torvalds 		}
2611da177e4SLinus Torvalds 		if (result < wdata->args.count)
2621da177e4SLinus Torvalds 			printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
2631da177e4SLinus Torvalds 					wdata->args.count, result);
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 		wdata->args.offset += result;
2661da177e4SLinus Torvalds 	        wdata->args.pgbase += result;
2671da177e4SLinus Torvalds 		written += result;
2681da177e4SLinus Torvalds 		count -= result;
26991d5b470SChuck Lever 		nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result);
2701da177e4SLinus Torvalds 	} while (count);
2711da177e4SLinus Torvalds 	/* Update file length */
2721da177e4SLinus Torvalds 	nfs_grow_file(page, offset, written);
2731da177e4SLinus Torvalds 	/* Set the PG_uptodate flag? */
2741da177e4SLinus Torvalds 	nfs_mark_uptodate(page, offset, written);
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 	if (PageError(page))
2771da177e4SLinus Torvalds 		ClearPageError(page);
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds io_error:
280951a143bSTrond Myklebust 	nfs_end_data_update(inode);
281bb713d6dSTrond Myklebust 	end_page_writeback(page);
2828aca67f0STrond Myklebust 	nfs_writedata_release(wdata);
2831da177e4SLinus Torvalds 	return written ? written : result;
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds static int nfs_writepage_async(struct nfs_open_context *ctx,
2871da177e4SLinus Torvalds 		struct inode *inode, struct page *page,
2881da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
2891da177e4SLinus Torvalds {
2901da177e4SLinus Torvalds 	struct nfs_page	*req;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	req = nfs_update_request(ctx, inode, page, offset, count);
293abd3e641STrond Myklebust 	if (IS_ERR(req))
294abd3e641STrond Myklebust 		return PTR_ERR(req);
2951da177e4SLinus Torvalds 	/* Update file length */
2961da177e4SLinus Torvalds 	nfs_grow_file(page, offset, count);
2971da177e4SLinus Torvalds 	/* Set the PG_uptodate flag? */
2981da177e4SLinus Torvalds 	nfs_mark_uptodate(page, offset, count);
2991da177e4SLinus Torvalds 	nfs_unlock_request(req);
300abd3e641STrond Myklebust 	return 0;
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
3041da177e4SLinus Torvalds {
3051da177e4SLinus Torvalds 	if (wbc->for_reclaim)
3061da177e4SLinus Torvalds 		return FLUSH_HIGHPRI;
3071da177e4SLinus Torvalds 	if (wbc->for_kupdate)
3081da177e4SLinus Torvalds 		return FLUSH_LOWPRI;
3091da177e4SLinus Torvalds 	return 0;
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds /*
3131da177e4SLinus Torvalds  * Write an mmapped page to the server.
3141da177e4SLinus Torvalds  */
3151da177e4SLinus Torvalds int nfs_writepage(struct page *page, struct writeback_control *wbc)
3161da177e4SLinus Torvalds {
3171da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
3181da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
319*49a70f27STrond Myklebust 	unsigned offset;
3201da177e4SLinus Torvalds 	int inode_referenced = 0;
3211da177e4SLinus Torvalds 	int priority = wb_priority(wbc);
3221da177e4SLinus Torvalds 	int err;
3231da177e4SLinus Torvalds 
32491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
32591d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
32691d5b470SChuck Lever 
3271da177e4SLinus Torvalds 	/*
3281da177e4SLinus Torvalds 	 * Note: We need to ensure that we have a reference to the inode
3291da177e4SLinus Torvalds 	 *       if we are to do asynchronous writes. If not, waiting
3301da177e4SLinus Torvalds 	 *       in nfs_wait_on_request() may deadlock with clear_inode().
3311da177e4SLinus Torvalds 	 *
3321da177e4SLinus Torvalds 	 *       If igrab() fails here, then it is in any case safe to
3331da177e4SLinus Torvalds 	 *       call nfs_wb_page(), since there will be no pending writes.
3341da177e4SLinus Torvalds 	 */
3351da177e4SLinus Torvalds 	if (igrab(inode) != 0)
3361da177e4SLinus Torvalds 		inode_referenced = 1;
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	/* Ensure we've flushed out any previous writes */
3391da177e4SLinus Torvalds 	nfs_wb_page_priority(inode, page, priority);
3401da177e4SLinus Torvalds 
341*49a70f27STrond Myklebust 	err = 0;
342*49a70f27STrond Myklebust 	offset = nfs_page_length(page);
343*49a70f27STrond Myklebust 	if (!offset)
3441da177e4SLinus Torvalds 		goto out;
345*49a70f27STrond Myklebust 
346d530838bSTrond Myklebust 	ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
3471da177e4SLinus Torvalds 	if (ctx == NULL) {
3481da177e4SLinus Torvalds 		err = -EBADF;
3491da177e4SLinus Torvalds 		goto out;
3501da177e4SLinus Torvalds 	}
3511da177e4SLinus Torvalds 	lock_kernel();
3521da177e4SLinus Torvalds 	if (!IS_SYNC(inode) && inode_referenced) {
3531da177e4SLinus Torvalds 		err = nfs_writepage_async(ctx, inode, page, 0, offset);
354abd3e641STrond Myklebust 		if (!wbc->for_writepages)
35528c6925fSTrond Myklebust 			nfs_flush_mapping(page->mapping, wbc, wb_priority(wbc));
3561da177e4SLinus Torvalds 	} else {
3571da177e4SLinus Torvalds 		err = nfs_writepage_sync(ctx, inode, page, 0,
3581da177e4SLinus Torvalds 						offset, priority);
3591da177e4SLinus Torvalds 		if (err >= 0) {
3601da177e4SLinus Torvalds 			if (err != offset)
3611da177e4SLinus Torvalds 				redirty_page_for_writepage(wbc, page);
3621da177e4SLinus Torvalds 			err = 0;
3631da177e4SLinus Torvalds 		}
3641da177e4SLinus Torvalds 	}
3651da177e4SLinus Torvalds 	unlock_kernel();
3661da177e4SLinus Torvalds 	put_nfs_open_context(ctx);
3671da177e4SLinus Torvalds out:
3681da177e4SLinus Torvalds 	unlock_page(page);
3691da177e4SLinus Torvalds 	if (inode_referenced)
3701da177e4SLinus Torvalds 		iput(inode);
3711da177e4SLinus Torvalds 	return err;
3721da177e4SLinus Torvalds }
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds /*
3751da177e4SLinus Torvalds  * Note: causes nfs_update_request() to block on the assumption
3761da177e4SLinus Torvalds  * 	 that the writeback is generated due to memory pressure.
3771da177e4SLinus Torvalds  */
3781da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3791da177e4SLinus Torvalds {
3801da177e4SLinus Torvalds 	struct backing_dev_info *bdi = mapping->backing_dev_info;
3811da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
3821da177e4SLinus Torvalds 	int err;
3831da177e4SLinus Torvalds 
38491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
38591d5b470SChuck Lever 
3861da177e4SLinus Torvalds 	err = generic_writepages(mapping, wbc);
3871da177e4SLinus Torvalds 	if (err)
3881da177e4SLinus Torvalds 		return err;
3891da177e4SLinus Torvalds 	while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
3901da177e4SLinus Torvalds 		if (wbc->nonblocking)
3911da177e4SLinus Torvalds 			return 0;
3921da177e4SLinus Torvalds 		nfs_wait_on_write_congestion(mapping, 0);
3931da177e4SLinus Torvalds 	}
39428c6925fSTrond Myklebust 	err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
3951da177e4SLinus Torvalds 	if (err < 0)
3961da177e4SLinus Torvalds 		goto out;
39791d5b470SChuck Lever 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
3981da177e4SLinus Torvalds 	if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
3991da177e4SLinus Torvalds 		err = nfs_wait_on_requests(inode, 0, 0);
4001da177e4SLinus Torvalds 		if (err < 0)
4011da177e4SLinus Torvalds 			goto out;
4021da177e4SLinus Torvalds 	}
4033da28eb1STrond Myklebust 	err = nfs_commit_inode(inode, wb_priority(wbc));
4043f442547STrond Myklebust 	if (err > 0)
4051da177e4SLinus Torvalds 		err = 0;
4061da177e4SLinus Torvalds out:
4071da177e4SLinus Torvalds 	clear_bit(BDI_write_congested, &bdi->state);
4081da177e4SLinus Torvalds 	wake_up_all(&nfs_write_congestion);
4093fcfab16SAndrew Morton 	congestion_end(WRITE);
4101da177e4SLinus Torvalds 	return err;
4111da177e4SLinus Torvalds }
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds /*
4141da177e4SLinus Torvalds  * Insert a write request into an inode
4151da177e4SLinus Torvalds  */
4161da177e4SLinus Torvalds static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
4171da177e4SLinus Torvalds {
4181da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4191da177e4SLinus Torvalds 	int error;
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
4221da177e4SLinus Torvalds 	BUG_ON(error == -EEXIST);
4231da177e4SLinus Torvalds 	if (error)
4241da177e4SLinus Torvalds 		return error;
4251da177e4SLinus Torvalds 	if (!nfsi->npages) {
4261da177e4SLinus Torvalds 		igrab(inode);
4271da177e4SLinus Torvalds 		nfs_begin_data_update(inode);
4281da177e4SLinus Torvalds 		if (nfs_have_delegation(inode, FMODE_WRITE))
4291da177e4SLinus Torvalds 			nfsi->change_attr++;
4301da177e4SLinus Torvalds 	}
431deb7d638STrond Myklebust 	SetPagePrivate(req->wb_page);
432277459d2STrond Myklebust 	set_page_private(req->wb_page, (unsigned long)req);
4331da177e4SLinus Torvalds 	nfsi->npages++;
4341da177e4SLinus Torvalds 	atomic_inc(&req->wb_count);
4351da177e4SLinus Torvalds 	return 0;
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds /*
4391da177e4SLinus Torvalds  * Insert a write request into an inode
4401da177e4SLinus Torvalds  */
4411da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
4421da177e4SLinus Torvalds {
4431da177e4SLinus Torvalds 	struct inode *inode = req->wb_context->dentry->d_inode;
4441da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	BUG_ON (!NFS_WBACK_BUSY(req));
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 	spin_lock(&nfsi->req_lock);
449277459d2STrond Myklebust 	set_page_private(req->wb_page, 0);
450deb7d638STrond Myklebust 	ClearPagePrivate(req->wb_page);
4511da177e4SLinus Torvalds 	radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
4521da177e4SLinus Torvalds 	nfsi->npages--;
4531da177e4SLinus Torvalds 	if (!nfsi->npages) {
4541da177e4SLinus Torvalds 		spin_unlock(&nfsi->req_lock);
455951a143bSTrond Myklebust 		nfs_end_data_update(inode);
4561da177e4SLinus Torvalds 		iput(inode);
4571da177e4SLinus Torvalds 	} else
4581da177e4SLinus Torvalds 		spin_unlock(&nfsi->req_lock);
4591da177e4SLinus Torvalds 	nfs_clear_request(req);
4601da177e4SLinus Torvalds 	nfs_release_request(req);
4611da177e4SLinus Torvalds }
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds /*
4641da177e4SLinus Torvalds  * Add a request to the inode's dirty list.
4651da177e4SLinus Torvalds  */
4661da177e4SLinus Torvalds static void
4671da177e4SLinus Torvalds nfs_mark_request_dirty(struct nfs_page *req)
4681da177e4SLinus Torvalds {
4691da177e4SLinus Torvalds 	struct inode *inode = req->wb_context->dentry->d_inode;
4701da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	spin_lock(&nfsi->req_lock);
4733da28eb1STrond Myklebust 	radix_tree_tag_set(&nfsi->nfs_page_tree,
4743da28eb1STrond Myklebust 			req->wb_index, NFS_PAGE_TAG_DIRTY);
4751da177e4SLinus Torvalds 	nfs_list_add_request(req, &nfsi->dirty);
4761da177e4SLinus Torvalds 	nfsi->ndirty++;
4771da177e4SLinus Torvalds 	spin_unlock(&nfsi->req_lock);
478b1e7a8fdSChristoph Lameter 	inc_zone_page_state(req->wb_page, NR_FILE_DIRTY);
4791da177e4SLinus Torvalds 	mark_inode_dirty(inode);
4801da177e4SLinus Torvalds }
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds /*
4831da177e4SLinus Torvalds  * Check if a request is dirty
4841da177e4SLinus Torvalds  */
4851da177e4SLinus Torvalds static inline int
4861da177e4SLinus Torvalds nfs_dirty_request(struct nfs_page *req)
4871da177e4SLinus Torvalds {
4881da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
4891da177e4SLinus Torvalds 	return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
4931da177e4SLinus Torvalds /*
4941da177e4SLinus Torvalds  * Add a request to the inode's commit list.
4951da177e4SLinus Torvalds  */
4961da177e4SLinus Torvalds static void
4971da177e4SLinus Torvalds nfs_mark_request_commit(struct nfs_page *req)
4981da177e4SLinus Torvalds {
4991da177e4SLinus Torvalds 	struct inode *inode = req->wb_context->dentry->d_inode;
5001da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	spin_lock(&nfsi->req_lock);
5031da177e4SLinus Torvalds 	nfs_list_add_request(req, &nfsi->commit);
5041da177e4SLinus Torvalds 	nfsi->ncommit++;
5051da177e4SLinus Torvalds 	spin_unlock(&nfsi->req_lock);
506fd39fc85SChristoph Lameter 	inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
5071da177e4SLinus Torvalds 	mark_inode_dirty(inode);
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds #endif
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds /*
5121da177e4SLinus Torvalds  * Wait for a request to complete.
5131da177e4SLinus Torvalds  *
5141da177e4SLinus Torvalds  * Interruptible by signals only if mounted with intr flag.
5151da177e4SLinus Torvalds  */
516c42de9ddSTrond Myklebust static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
5171da177e4SLinus Torvalds {
5181da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
5191da177e4SLinus Torvalds 	struct nfs_page *req;
5201da177e4SLinus Torvalds 	unsigned long		idx_end, next;
5211da177e4SLinus Torvalds 	unsigned int		res = 0;
5221da177e4SLinus Torvalds 	int			error;
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 	if (npages == 0)
5251da177e4SLinus Torvalds 		idx_end = ~0;
5261da177e4SLinus Torvalds 	else
5271da177e4SLinus Torvalds 		idx_end = idx_start + npages - 1;
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	next = idx_start;
530c6a556b8STrond Myklebust 	while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
5311da177e4SLinus Torvalds 		if (req->wb_index > idx_end)
5321da177e4SLinus Torvalds 			break;
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 		next = req->wb_index + 1;
535c6a556b8STrond Myklebust 		BUG_ON(!NFS_WBACK_BUSY(req));
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 		atomic_inc(&req->wb_count);
5381da177e4SLinus Torvalds 		spin_unlock(&nfsi->req_lock);
5391da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
5401da177e4SLinus Torvalds 		nfs_release_request(req);
541c42de9ddSTrond Myklebust 		spin_lock(&nfsi->req_lock);
5421da177e4SLinus Torvalds 		if (error < 0)
5431da177e4SLinus Torvalds 			return error;
5441da177e4SLinus Torvalds 		res++;
5451da177e4SLinus Torvalds 	}
5461da177e4SLinus Torvalds 	return res;
5471da177e4SLinus Torvalds }
5481da177e4SLinus Torvalds 
549c42de9ddSTrond Myklebust static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
550c42de9ddSTrond Myklebust {
551c42de9ddSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
552c42de9ddSTrond Myklebust 	int ret;
553c42de9ddSTrond Myklebust 
554c42de9ddSTrond Myklebust 	spin_lock(&nfsi->req_lock);
555c42de9ddSTrond Myklebust 	ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
556c42de9ddSTrond Myklebust 	spin_unlock(&nfsi->req_lock);
557c42de9ddSTrond Myklebust 	return ret;
558c42de9ddSTrond Myklebust }
559c42de9ddSTrond Myklebust 
56083715ad5STrond Myklebust static void nfs_cancel_dirty_list(struct list_head *head)
561d2ccddf0STrond Myklebust {
562d2ccddf0STrond Myklebust 	struct nfs_page *req;
563d2ccddf0STrond Myklebust 	while(!list_empty(head)) {
564d2ccddf0STrond Myklebust 		req = nfs_list_entry(head->next);
565d2ccddf0STrond Myklebust 		nfs_list_remove_request(req);
566d2ccddf0STrond Myklebust 		nfs_inode_remove_request(req);
567d2ccddf0STrond Myklebust 		nfs_clear_page_writeback(req);
568d2ccddf0STrond Myklebust 	}
569d2ccddf0STrond Myklebust }
570d2ccddf0STrond Myklebust 
57183715ad5STrond Myklebust static void nfs_cancel_commit_list(struct list_head *head)
57283715ad5STrond Myklebust {
57383715ad5STrond Myklebust 	struct nfs_page *req;
57483715ad5STrond Myklebust 
57583715ad5STrond Myklebust 	while(!list_empty(head)) {
57683715ad5STrond Myklebust 		req = nfs_list_entry(head->next);
577b6dff26aSTrond Myklebust 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
57883715ad5STrond Myklebust 		nfs_list_remove_request(req);
57983715ad5STrond Myklebust 		nfs_inode_remove_request(req);
580b6dff26aSTrond Myklebust 		nfs_unlock_request(req);
58183715ad5STrond Myklebust 	}
58283715ad5STrond Myklebust }
58383715ad5STrond Myklebust 
5841da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
5851da177e4SLinus Torvalds /*
5861da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
5871da177e4SLinus Torvalds  * @inode: NFS inode to scan
5881da177e4SLinus Torvalds  * @dst: destination list
5891da177e4SLinus Torvalds  * @idx_start: lower bound of page->index to scan.
5901da177e4SLinus Torvalds  * @npages: idx_start + npages sets the upper bound to scan.
5911da177e4SLinus Torvalds  *
5921da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
5931da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
5941da177e4SLinus Torvalds  */
5951da177e4SLinus Torvalds static int
5961da177e4SLinus Torvalds nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
5971da177e4SLinus Torvalds {
5981da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
5993da28eb1STrond Myklebust 	int res = 0;
6003da28eb1STrond Myklebust 
6013da28eb1STrond Myklebust 	if (nfsi->ncommit != 0) {
602d2ccddf0STrond Myklebust 		res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
6031da177e4SLinus Torvalds 		nfsi->ncommit -= res;
6041da177e4SLinus Torvalds 		if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
6051da177e4SLinus Torvalds 			printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
6063da28eb1STrond Myklebust 	}
6071da177e4SLinus Torvalds 	return res;
6081da177e4SLinus Torvalds }
609c42de9ddSTrond Myklebust #else
610c42de9ddSTrond Myklebust static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
611c42de9ddSTrond Myklebust {
612c42de9ddSTrond Myklebust 	return 0;
613c42de9ddSTrond Myklebust }
6141da177e4SLinus Torvalds #endif
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
6171da177e4SLinus Torvalds {
6181da177e4SLinus Torvalds 	struct backing_dev_info *bdi = mapping->backing_dev_info;
6191da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
6201da177e4SLinus Torvalds 	int ret = 0;
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	might_sleep();
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	if (!bdi_write_congested(bdi))
6251da177e4SLinus Torvalds 		return 0;
62691d5b470SChuck Lever 
62791d5b470SChuck Lever 	nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
62891d5b470SChuck Lever 
6291da177e4SLinus Torvalds 	if (intr) {
6301da177e4SLinus Torvalds 		struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
6311da177e4SLinus Torvalds 		sigset_t oldset;
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 		rpc_clnt_sigmask(clnt, &oldset);
6341da177e4SLinus Torvalds 		prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
6351da177e4SLinus Torvalds 		if (bdi_write_congested(bdi)) {
6361da177e4SLinus Torvalds 			if (signalled())
6371da177e4SLinus Torvalds 				ret = -ERESTARTSYS;
6381da177e4SLinus Torvalds 			else
6391da177e4SLinus Torvalds 				schedule();
6401da177e4SLinus Torvalds 		}
6411da177e4SLinus Torvalds 		rpc_clnt_sigunmask(clnt, &oldset);
6421da177e4SLinus Torvalds 	} else {
6431da177e4SLinus Torvalds 		prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
6441da177e4SLinus Torvalds 		if (bdi_write_congested(bdi))
6451da177e4SLinus Torvalds 			schedule();
6461da177e4SLinus Torvalds 	}
6471da177e4SLinus Torvalds 	finish_wait(&nfs_write_congestion, &wait);
6481da177e4SLinus Torvalds 	return ret;
6491da177e4SLinus Torvalds }
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds /*
6531da177e4SLinus Torvalds  * Try to update any existing write request, or create one if there is none.
6541da177e4SLinus Torvalds  * In order to match, the request's credentials must match those of
6551da177e4SLinus Torvalds  * the calling process.
6561da177e4SLinus Torvalds  *
6571da177e4SLinus Torvalds  * Note: Should always be called with the Page Lock held!
6581da177e4SLinus Torvalds  */
6591da177e4SLinus Torvalds static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
6601da177e4SLinus Torvalds 		struct inode *inode, struct page *page,
6611da177e4SLinus Torvalds 		unsigned int offset, unsigned int bytes)
6621da177e4SLinus Torvalds {
6631da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
6641da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
6651da177e4SLinus Torvalds 	struct nfs_page		*req, *new = NULL;
6661da177e4SLinus Torvalds 	unsigned long		rqend, end;
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	end = offset + bytes;
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 	if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
6711da177e4SLinus Torvalds 		return ERR_PTR(-ERESTARTSYS);
6721da177e4SLinus Torvalds 	for (;;) {
6731da177e4SLinus Torvalds 		/* Loop over all inode entries and see if we find
6741da177e4SLinus Torvalds 		 * A request for the page we wish to update
6751da177e4SLinus Torvalds 		 */
6761da177e4SLinus Torvalds 		spin_lock(&nfsi->req_lock);
677277459d2STrond Myklebust 		req = nfs_page_find_request_locked(page);
6781da177e4SLinus Torvalds 		if (req) {
6791da177e4SLinus Torvalds 			if (!nfs_lock_request_dontget(req)) {
6801da177e4SLinus Torvalds 				int error;
681277459d2STrond Myklebust 
6821da177e4SLinus Torvalds 				spin_unlock(&nfsi->req_lock);
6831da177e4SLinus Torvalds 				error = nfs_wait_on_request(req);
6841da177e4SLinus Torvalds 				nfs_release_request(req);
6851dd594b2SNeil Brown 				if (error < 0) {
6861dd594b2SNeil Brown 					if (new)
6871dd594b2SNeil Brown 						nfs_release_request(new);
6881da177e4SLinus Torvalds 					return ERR_PTR(error);
6891dd594b2SNeil Brown 				}
6901da177e4SLinus Torvalds 				continue;
6911da177e4SLinus Torvalds 			}
6921da177e4SLinus Torvalds 			spin_unlock(&nfsi->req_lock);
6931da177e4SLinus Torvalds 			if (new)
6941da177e4SLinus Torvalds 				nfs_release_request(new);
6951da177e4SLinus Torvalds 			break;
6961da177e4SLinus Torvalds 		}
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 		if (new) {
6991da177e4SLinus Torvalds 			int error;
7001da177e4SLinus Torvalds 			nfs_lock_request_dontget(new);
7011da177e4SLinus Torvalds 			error = nfs_inode_add_request(inode, new);
7021da177e4SLinus Torvalds 			if (error) {
7031da177e4SLinus Torvalds 				spin_unlock(&nfsi->req_lock);
7041da177e4SLinus Torvalds 				nfs_unlock_request(new);
7051da177e4SLinus Torvalds 				return ERR_PTR(error);
7061da177e4SLinus Torvalds 			}
7071da177e4SLinus Torvalds 			spin_unlock(&nfsi->req_lock);
7081da177e4SLinus Torvalds 			nfs_mark_request_dirty(new);
7091da177e4SLinus Torvalds 			return new;
7101da177e4SLinus Torvalds 		}
7111da177e4SLinus Torvalds 		spin_unlock(&nfsi->req_lock);
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 		new = nfs_create_request(ctx, inode, page, offset, bytes);
7141da177e4SLinus Torvalds 		if (IS_ERR(new))
7151da177e4SLinus Torvalds 			return new;
7161da177e4SLinus Torvalds 	}
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	/* We have a request for our page.
7191da177e4SLinus Torvalds 	 * If the creds don't match, or the
7201da177e4SLinus Torvalds 	 * page addresses don't match,
7211da177e4SLinus Torvalds 	 * tell the caller to wait on the conflicting
7221da177e4SLinus Torvalds 	 * request.
7231da177e4SLinus Torvalds 	 */
7241da177e4SLinus Torvalds 	rqend = req->wb_offset + req->wb_bytes;
7251da177e4SLinus Torvalds 	if (req->wb_context != ctx
7261da177e4SLinus Torvalds 	    || req->wb_page != page
7271da177e4SLinus Torvalds 	    || !nfs_dirty_request(req)
7281da177e4SLinus Torvalds 	    || offset > rqend || end < req->wb_offset) {
7291da177e4SLinus Torvalds 		nfs_unlock_request(req);
7301da177e4SLinus Torvalds 		return ERR_PTR(-EBUSY);
7311da177e4SLinus Torvalds 	}
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
7341da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
7351da177e4SLinus Torvalds 		req->wb_offset = offset;
7361da177e4SLinus Torvalds 		req->wb_pgbase = offset;
7371da177e4SLinus Torvalds 		req->wb_bytes = rqend - req->wb_offset;
7381da177e4SLinus Torvalds 	}
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	if (end > rqend)
7411da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds 	return req;
7441da177e4SLinus Torvalds }
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
7471da177e4SLinus Torvalds {
7481da177e4SLinus Torvalds 	struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
7491da177e4SLinus Torvalds 	struct nfs_page	*req;
7501da177e4SLinus Torvalds 	int		status = 0;
7511da177e4SLinus Torvalds 	/*
7521da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
7531da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
7541da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
7551da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
7561da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
7571da177e4SLinus Torvalds 	 * dropped page.
7581da177e4SLinus Torvalds 	 */
759277459d2STrond Myklebust 	req = nfs_page_find_request(page);
760277459d2STrond Myklebust 	if (req != NULL) {
761277459d2STrond Myklebust 		int do_flush = req->wb_page != page || req->wb_context != ctx;
762277459d2STrond Myklebust 
7631da177e4SLinus Torvalds 		nfs_release_request(req);
764277459d2STrond Myklebust 		if (do_flush)
765277459d2STrond Myklebust 			status = nfs_wb_page(page->mapping->host, page);
7661da177e4SLinus Torvalds 	}
7671da177e4SLinus Torvalds 	return (status < 0) ? status : 0;
7681da177e4SLinus Torvalds }
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds /*
7711da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
7721da177e4SLinus Torvalds  *
7731da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
7741da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
7751da177e4SLinus Torvalds  */
7761da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
7771da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
7781da177e4SLinus Torvalds {
7791da177e4SLinus Torvalds 	struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
7801da177e4SLinus Torvalds 	struct inode	*inode = page->mapping->host;
7811da177e4SLinus Torvalds 	struct nfs_page	*req;
7821da177e4SLinus Torvalds 	int		status = 0;
7831da177e4SLinus Torvalds 
78491d5b470SChuck Lever 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
78591d5b470SChuck Lever 
7861da177e4SLinus Torvalds 	dprintk("NFS:      nfs_updatepage(%s/%s %d@%Ld)\n",
7870bbacc40SChuck Lever 		file->f_dentry->d_parent->d_name.name,
7880bbacc40SChuck Lever 		file->f_dentry->d_name.name, count,
7890bbacc40SChuck Lever 		(long long)(page_offset(page) +offset));
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 	if (IS_SYNC(inode)) {
7921da177e4SLinus Torvalds 		status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
7931da177e4SLinus Torvalds 		if (status > 0) {
7941da177e4SLinus Torvalds 			if (offset == 0 && status == PAGE_CACHE_SIZE)
7951da177e4SLinus Torvalds 				SetPageUptodate(page);
7961da177e4SLinus Torvalds 			return 0;
7971da177e4SLinus Torvalds 		}
7981da177e4SLinus Torvalds 		return status;
7991da177e4SLinus Torvalds 	}
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	/* If we're not using byte range locks, and we know the page
8021da177e4SLinus Torvalds 	 * is entirely in cache, it may be more efficient to avoid
8031da177e4SLinus Torvalds 	 * fragmenting write requests.
8041da177e4SLinus Torvalds 	 */
805ab0a3dbeSTrond Myklebust 	if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
806*49a70f27STrond Myklebust 		count = max(count + offset, nfs_page_length(page));
8071da177e4SLinus Torvalds 		offset = 0;
8081da177e4SLinus Torvalds 	}
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	/*
8111da177e4SLinus Torvalds 	 * Try to find an NFS request corresponding to this page
8121da177e4SLinus Torvalds 	 * and update it.
8131da177e4SLinus Torvalds 	 * If the existing request cannot be updated, we must flush
8141da177e4SLinus Torvalds 	 * it out now.
8151da177e4SLinus Torvalds 	 */
8161da177e4SLinus Torvalds 	do {
8171da177e4SLinus Torvalds 		req = nfs_update_request(ctx, inode, page, offset, count);
8181da177e4SLinus Torvalds 		status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
8191da177e4SLinus Torvalds 		if (status != -EBUSY)
8201da177e4SLinus Torvalds 			break;
8211da177e4SLinus Torvalds 		/* Request could not be updated. Flush it out and try again */
8221da177e4SLinus Torvalds 		status = nfs_wb_page(inode, page);
8231da177e4SLinus Torvalds 	} while (status >= 0);
8241da177e4SLinus Torvalds 	if (status < 0)
8251da177e4SLinus Torvalds 		goto done;
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 	status = 0;
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds 	/* Update file length */
8301da177e4SLinus Torvalds 	nfs_grow_file(page, offset, count);
8311da177e4SLinus Torvalds 	/* Set the PG_uptodate flag? */
8321da177e4SLinus Torvalds 	nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
8331da177e4SLinus Torvalds 	nfs_unlock_request(req);
8341da177e4SLinus Torvalds done:
8351da177e4SLinus Torvalds         dprintk("NFS:      nfs_updatepage returns %d (isize %Ld)\n",
8361da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
8371da177e4SLinus Torvalds 	if (status < 0)
8381da177e4SLinus Torvalds 		ClearPageUptodate(page);
8391da177e4SLinus Torvalds 	return status;
8401da177e4SLinus Torvalds }
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds static void nfs_writepage_release(struct nfs_page *req)
8431da177e4SLinus Torvalds {
8441da177e4SLinus Torvalds 	end_page_writeback(req->wb_page);
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
8471da177e4SLinus Torvalds 	if (!PageError(req->wb_page)) {
8481da177e4SLinus Torvalds 		if (NFS_NEED_RESCHED(req)) {
8491da177e4SLinus Torvalds 			nfs_mark_request_dirty(req);
8501da177e4SLinus Torvalds 			goto out;
8511da177e4SLinus Torvalds 		} else if (NFS_NEED_COMMIT(req)) {
8521da177e4SLinus Torvalds 			nfs_mark_request_commit(req);
8531da177e4SLinus Torvalds 			goto out;
8541da177e4SLinus Torvalds 		}
8551da177e4SLinus Torvalds 	}
8561da177e4SLinus Torvalds 	nfs_inode_remove_request(req);
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds out:
8591da177e4SLinus Torvalds 	nfs_clear_commit(req);
8601da177e4SLinus Torvalds 	nfs_clear_reschedule(req);
8611da177e4SLinus Torvalds #else
8621da177e4SLinus Torvalds 	nfs_inode_remove_request(req);
8631da177e4SLinus Torvalds #endif
864c6a556b8STrond Myklebust 	nfs_clear_page_writeback(req);
8651da177e4SLinus Torvalds }
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds static inline int flush_task_priority(int how)
8681da177e4SLinus Torvalds {
8691da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
8701da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
8711da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
8721da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
8731da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
8741da177e4SLinus Torvalds 	}
8751da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
8761da177e4SLinus Torvalds }
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds /*
8791da177e4SLinus Torvalds  * Set up the argument/result storage required for the RPC call.
8801da177e4SLinus Torvalds  */
8811da177e4SLinus Torvalds static void nfs_write_rpcsetup(struct nfs_page *req,
8821da177e4SLinus Torvalds 		struct nfs_write_data *data,
883788e7a89STrond Myklebust 		const struct rpc_call_ops *call_ops,
8841da177e4SLinus Torvalds 		unsigned int count, unsigned int offset,
8851da177e4SLinus Torvalds 		int how)
8861da177e4SLinus Torvalds {
8871da177e4SLinus Torvalds 	struct inode		*inode;
888788e7a89STrond Myklebust 	int flags;
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
8911da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 	data->req = req;
8941da177e4SLinus Torvalds 	data->inode = inode = req->wb_context->dentry->d_inode;
8951da177e4SLinus Torvalds 	data->cred = req->wb_context->cred;
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(inode);
8981da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
8991da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
9001da177e4SLinus Torvalds 	data->args.pages  = data->pagevec;
9011da177e4SLinus Torvalds 	data->args.count  = count;
9021da177e4SLinus Torvalds 	data->args.context = req->wb_context;
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
9051da177e4SLinus Torvalds 	data->res.count   = count;
9061da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
9070e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
9081da177e4SLinus Torvalds 
909788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
910788e7a89STrond Myklebust 	flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
911788e7a89STrond Myklebust 	rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
9121da177e4SLinus Torvalds 	NFS_PROTO(inode)->write_setup(data, how);
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 	data->task.tk_priority = flush_task_priority(how);
9151da177e4SLinus Torvalds 	data->task.tk_cookie = (unsigned long)inode;
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 	dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
9180bbacc40SChuck Lever 		data->task.tk_pid,
9191da177e4SLinus Torvalds 		inode->i_sb->s_id,
9201da177e4SLinus Torvalds 		(long long)NFS_FILEID(inode),
9211da177e4SLinus Torvalds 		count,
9221da177e4SLinus Torvalds 		(unsigned long long)data->args.offset);
9231da177e4SLinus Torvalds }
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds static void nfs_execute_write(struct nfs_write_data *data)
9261da177e4SLinus Torvalds {
9271da177e4SLinus Torvalds 	struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
9281da177e4SLinus Torvalds 	sigset_t oldset;
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 	rpc_clnt_sigmask(clnt, &oldset);
9311da177e4SLinus Torvalds 	rpc_execute(&data->task);
9321da177e4SLinus Torvalds 	rpc_clnt_sigunmask(clnt, &oldset);
9331da177e4SLinus Torvalds }
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds /*
9361da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
9371da177e4SLinus Torvalds  * contiguous dirty area on one page.
9381da177e4SLinus Torvalds  */
9397d46a49fSTrond Myklebust static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
9401da177e4SLinus Torvalds {
9411da177e4SLinus Torvalds 	struct nfs_page *req = nfs_list_entry(head->next);
9421da177e4SLinus Torvalds 	struct page *page = req->wb_page;
9431da177e4SLinus Torvalds 	struct nfs_write_data *data;
944e9f7bee1STrond Myklebust 	size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
945e9f7bee1STrond Myklebust 	unsigned int offset;
9461da177e4SLinus Torvalds 	int requests = 0;
9471da177e4SLinus Torvalds 	LIST_HEAD(list);
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	nfs_list_remove_request(req);
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 	nbytes = req->wb_bytes;
952e9f7bee1STrond Myklebust 	do {
953e9f7bee1STrond Myklebust 		size_t len = min(nbytes, wsize);
954e9f7bee1STrond Myklebust 
955e9f7bee1STrond Myklebust 		data = nfs_writedata_alloc(len);
9561da177e4SLinus Torvalds 		if (!data)
9571da177e4SLinus Torvalds 			goto out_bad;
9581da177e4SLinus Torvalds 		list_add(&data->pages, &list);
9591da177e4SLinus Torvalds 		requests++;
960e9f7bee1STrond Myklebust 		nbytes -= len;
961e9f7bee1STrond Myklebust 	} while (nbytes != 0);
9621da177e4SLinus Torvalds 	atomic_set(&req->wb_complete, requests);
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	ClearPageError(page);
965bb713d6dSTrond Myklebust 	set_page_writeback(page);
9661da177e4SLinus Torvalds 	offset = 0;
9671da177e4SLinus Torvalds 	nbytes = req->wb_bytes;
9681da177e4SLinus Torvalds 	do {
9691da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
9701da177e4SLinus Torvalds 		list_del_init(&data->pages);
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 		data->pagevec[0] = page;
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 		if (nbytes > wsize) {
975788e7a89STrond Myklebust 			nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
976788e7a89STrond Myklebust 					wsize, offset, how);
9771da177e4SLinus Torvalds 			offset += wsize;
9781da177e4SLinus Torvalds 			nbytes -= wsize;
9791da177e4SLinus Torvalds 		} else {
980788e7a89STrond Myklebust 			nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
981788e7a89STrond Myklebust 					nbytes, offset, how);
9821da177e4SLinus Torvalds 			nbytes = 0;
9831da177e4SLinus Torvalds 		}
9841da177e4SLinus Torvalds 		nfs_execute_write(data);
9851da177e4SLinus Torvalds 	} while (nbytes != 0);
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds 	return 0;
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds out_bad:
9901da177e4SLinus Torvalds 	while (!list_empty(&list)) {
9911da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
9921da177e4SLinus Torvalds 		list_del(&data->pages);
9938aca67f0STrond Myklebust 		nfs_writedata_release(data);
9941da177e4SLinus Torvalds 	}
9951da177e4SLinus Torvalds 	nfs_mark_request_dirty(req);
996c6a556b8STrond Myklebust 	nfs_clear_page_writeback(req);
9971da177e4SLinus Torvalds 	return -ENOMEM;
9981da177e4SLinus Torvalds }
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds /*
10011da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
10021da177e4SLinus Torvalds  * The page must have been locked by the caller.
10031da177e4SLinus Torvalds  *
10041da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
10051da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
10061da177e4SLinus Torvalds  * that has been written but not committed.
10071da177e4SLinus Torvalds  */
10087d46a49fSTrond Myklebust static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
10091da177e4SLinus Torvalds {
10101da177e4SLinus Torvalds 	struct nfs_page		*req;
10111da177e4SLinus Torvalds 	struct page		**pages;
10121da177e4SLinus Torvalds 	struct nfs_write_data	*data;
10131da177e4SLinus Torvalds 	unsigned int		count;
10141da177e4SLinus Torvalds 
1015e9f7bee1STrond Myklebust 	data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
10161da177e4SLinus Torvalds 	if (!data)
10171da177e4SLinus Torvalds 		goto out_bad;
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds 	pages = data->pagevec;
10201da177e4SLinus Torvalds 	count = 0;
10211da177e4SLinus Torvalds 	while (!list_empty(head)) {
10221da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
10231da177e4SLinus Torvalds 		nfs_list_remove_request(req);
10241da177e4SLinus Torvalds 		nfs_list_add_request(req, &data->pages);
10251da177e4SLinus Torvalds 		ClearPageError(req->wb_page);
1026bb713d6dSTrond Myklebust 		set_page_writeback(req->wb_page);
10271da177e4SLinus Torvalds 		*pages++ = req->wb_page;
10281da177e4SLinus Torvalds 		count += req->wb_bytes;
10291da177e4SLinus Torvalds 	}
10301da177e4SLinus Torvalds 	req = nfs_list_entry(data->pages.next);
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 	/* Set up the argument struct */
1033788e7a89STrond Myklebust 	nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	nfs_execute_write(data);
10361da177e4SLinus Torvalds 	return 0;
10371da177e4SLinus Torvalds  out_bad:
10381da177e4SLinus Torvalds 	while (!list_empty(head)) {
10391da177e4SLinus Torvalds 		struct nfs_page *req = nfs_list_entry(head->next);
10401da177e4SLinus Torvalds 		nfs_list_remove_request(req);
10411da177e4SLinus Torvalds 		nfs_mark_request_dirty(req);
1042c6a556b8STrond Myklebust 		nfs_clear_page_writeback(req);
10431da177e4SLinus Torvalds 	}
10441da177e4SLinus Torvalds 	return -ENOMEM;
10451da177e4SLinus Torvalds }
10461da177e4SLinus Torvalds 
10477d46a49fSTrond Myklebust static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
10481da177e4SLinus Torvalds {
10491da177e4SLinus Torvalds 	LIST_HEAD(one_request);
10507d46a49fSTrond Myklebust 	int (*flush_one)(struct inode *, struct list_head *, int);
10511da177e4SLinus Torvalds 	struct nfs_page	*req;
10527d46a49fSTrond Myklebust 	int wpages = NFS_SERVER(inode)->wpages;
10537d46a49fSTrond Myklebust 	int wsize = NFS_SERVER(inode)->wsize;
10547d46a49fSTrond Myklebust 	int error;
10551da177e4SLinus Torvalds 
10567d46a49fSTrond Myklebust 	flush_one = nfs_flush_one;
10577d46a49fSTrond Myklebust 	if (wsize < PAGE_CACHE_SIZE)
10587d46a49fSTrond Myklebust 		flush_one = nfs_flush_multi;
10597d46a49fSTrond Myklebust 	/* For single writes, FLUSH_STABLE is more efficient */
10607d46a49fSTrond Myklebust 	if (npages <= wpages && npages == NFS_I(inode)->npages
10617d46a49fSTrond Myklebust 			&& nfs_list_entry(head->next)->wb_bytes <= wsize)
10627d46a49fSTrond Myklebust 		how |= FLUSH_STABLE;
10637d46a49fSTrond Myklebust 
10647d46a49fSTrond Myklebust 	do {
10657d46a49fSTrond Myklebust 		nfs_coalesce_requests(head, &one_request, wpages);
10661da177e4SLinus Torvalds 		req = nfs_list_entry(one_request.next);
10677d46a49fSTrond Myklebust 		error = flush_one(inode, &one_request, how);
10681da177e4SLinus Torvalds 		if (error < 0)
10697d46a49fSTrond Myklebust 			goto out_err;
10707d46a49fSTrond Myklebust 	} while (!list_empty(head));
10717d46a49fSTrond Myklebust 	return 0;
10727d46a49fSTrond Myklebust out_err:
10731da177e4SLinus Torvalds 	while (!list_empty(head)) {
10741da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
10751da177e4SLinus Torvalds 		nfs_list_remove_request(req);
10761da177e4SLinus Torvalds 		nfs_mark_request_dirty(req);
1077c6a556b8STrond Myklebust 		nfs_clear_page_writeback(req);
10781da177e4SLinus Torvalds 	}
10791da177e4SLinus Torvalds 	return error;
10801da177e4SLinus Torvalds }
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds /*
10831da177e4SLinus Torvalds  * Handle a write reply that flushed part of a page.
10841da177e4SLinus Torvalds  */
1085788e7a89STrond Myklebust static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
10861da177e4SLinus Torvalds {
1087788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
10881da177e4SLinus Torvalds 	struct nfs_page		*req = data->req;
10891da177e4SLinus Torvalds 	struct page		*page = req->wb_page;
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 	dprintk("NFS: write (%s/%Ld %d@%Ld)",
10921da177e4SLinus Torvalds 		req->wb_context->dentry->d_inode->i_sb->s_id,
10931da177e4SLinus Torvalds 		(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
10941da177e4SLinus Torvalds 		req->wb_bytes,
10951da177e4SLinus Torvalds 		(long long)req_offset(req));
10961da177e4SLinus Torvalds 
1097788e7a89STrond Myklebust 	if (nfs_writeback_done(task, data) != 0)
1098788e7a89STrond Myklebust 		return;
1099788e7a89STrond Myklebust 
1100788e7a89STrond Myklebust 	if (task->tk_status < 0) {
11011da177e4SLinus Torvalds 		ClearPageUptodate(page);
11021da177e4SLinus Torvalds 		SetPageError(page);
1103788e7a89STrond Myklebust 		req->wb_context->error = task->tk_status;
1104788e7a89STrond Myklebust 		dprintk(", error = %d\n", task->tk_status);
11051da177e4SLinus Torvalds 	} else {
11061da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
11071da177e4SLinus Torvalds 		if (data->verf.committed < NFS_FILE_SYNC) {
11081da177e4SLinus Torvalds 			if (!NFS_NEED_COMMIT(req)) {
11091da177e4SLinus Torvalds 				nfs_defer_commit(req);
11101da177e4SLinus Torvalds 				memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
11111da177e4SLinus Torvalds 				dprintk(" defer commit\n");
11121da177e4SLinus Torvalds 			} else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
11131da177e4SLinus Torvalds 				nfs_defer_reschedule(req);
11141da177e4SLinus Torvalds 				dprintk(" server reboot detected\n");
11151da177e4SLinus Torvalds 			}
11161da177e4SLinus Torvalds 		} else
11171da177e4SLinus Torvalds #endif
11181da177e4SLinus Torvalds 			dprintk(" OK\n");
11191da177e4SLinus Torvalds 	}
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	if (atomic_dec_and_test(&req->wb_complete))
11221da177e4SLinus Torvalds 		nfs_writepage_release(req);
11231da177e4SLinus Torvalds }
11241da177e4SLinus Torvalds 
1125788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops = {
1126788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_partial,
1127788e7a89STrond Myklebust 	.rpc_release = nfs_writedata_release,
1128788e7a89STrond Myklebust };
1129788e7a89STrond Myklebust 
11301da177e4SLinus Torvalds /*
11311da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
11321da177e4SLinus Torvalds  *
11331da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
11341da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
11351da177e4SLinus Torvalds  *	  as the page has a write request pending.
11361da177e4SLinus Torvalds  */
1137788e7a89STrond Myklebust static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
11381da177e4SLinus Torvalds {
1139788e7a89STrond Myklebust 	struct nfs_write_data	*data = calldata;
11401da177e4SLinus Torvalds 	struct nfs_page		*req;
11411da177e4SLinus Torvalds 	struct page		*page;
11421da177e4SLinus Torvalds 
1143788e7a89STrond Myklebust 	if (nfs_writeback_done(task, data) != 0)
1144788e7a89STrond Myklebust 		return;
1145788e7a89STrond Myklebust 
11461da177e4SLinus Torvalds 	/* Update attributes as result of writeback. */
11471da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
11481da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
11491da177e4SLinus Torvalds 		nfs_list_remove_request(req);
11501da177e4SLinus Torvalds 		page = req->wb_page;
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds 		dprintk("NFS: write (%s/%Ld %d@%Ld)",
11531da177e4SLinus Torvalds 			req->wb_context->dentry->d_inode->i_sb->s_id,
11541da177e4SLinus Torvalds 			(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
11551da177e4SLinus Torvalds 			req->wb_bytes,
11561da177e4SLinus Torvalds 			(long long)req_offset(req));
11571da177e4SLinus Torvalds 
1158788e7a89STrond Myklebust 		if (task->tk_status < 0) {
11591da177e4SLinus Torvalds 			ClearPageUptodate(page);
11601da177e4SLinus Torvalds 			SetPageError(page);
1161788e7a89STrond Myklebust 			req->wb_context->error = task->tk_status;
11621da177e4SLinus Torvalds 			end_page_writeback(page);
11631da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
1164788e7a89STrond Myklebust 			dprintk(", error = %d\n", task->tk_status);
11651da177e4SLinus Torvalds 			goto next;
11661da177e4SLinus Torvalds 		}
11671da177e4SLinus Torvalds 		end_page_writeback(page);
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
11701da177e4SLinus Torvalds 		if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
11711da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
11721da177e4SLinus Torvalds 			dprintk(" OK\n");
11731da177e4SLinus Torvalds 			goto next;
11741da177e4SLinus Torvalds 		}
11751da177e4SLinus Torvalds 		memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
11761da177e4SLinus Torvalds 		nfs_mark_request_commit(req);
11771da177e4SLinus Torvalds 		dprintk(" marked for commit\n");
11781da177e4SLinus Torvalds #else
11791da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
11801da177e4SLinus Torvalds #endif
11811da177e4SLinus Torvalds 	next:
1182c6a556b8STrond Myklebust 		nfs_clear_page_writeback(req);
11831da177e4SLinus Torvalds 	}
11841da177e4SLinus Torvalds }
11851da177e4SLinus Torvalds 
1186788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops = {
1187788e7a89STrond Myklebust 	.rpc_call_done = nfs_writeback_done_full,
1188788e7a89STrond Myklebust 	.rpc_release = nfs_writedata_release,
1189788e7a89STrond Myklebust };
1190788e7a89STrond Myklebust 
1191788e7a89STrond Myklebust 
11921da177e4SLinus Torvalds /*
11931da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
11941da177e4SLinus Torvalds  */
1195462d5b32SChuck Lever int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
11961da177e4SLinus Torvalds {
11971da177e4SLinus Torvalds 	struct nfs_writeargs	*argp = &data->args;
11981da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
1199788e7a89STrond Myklebust 	int status;
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds 	dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
12021da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
12031da177e4SLinus Torvalds 
1204f551e44fSChuck Lever 	/*
1205f551e44fSChuck Lever 	 * ->write_done will attempt to use post-op attributes to detect
1206f551e44fSChuck Lever 	 * conflicting writes by other clients.  A strict interpretation
1207f551e44fSChuck Lever 	 * of close-to-open would allow us to continue caching even if
1208f551e44fSChuck Lever 	 * another writer had changed the file, but some applications
1209f551e44fSChuck Lever 	 * depend on tighter cache coherency when writing.
1210f551e44fSChuck Lever 	 */
1211788e7a89STrond Myklebust 	status = NFS_PROTO(data->inode)->write_done(task, data);
1212788e7a89STrond Myklebust 	if (status != 0)
1213788e7a89STrond Myklebust 		return status;
121491d5b470SChuck Lever 	nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
121591d5b470SChuck Lever 
12161da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
12171da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
12181da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
12191da177e4SLinus Torvalds 		 * commit data to stable storage even though we
12201da177e4SLinus Torvalds 		 * requested it.
12211da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
12221da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
12231da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
12241da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
12251da177e4SLinus Torvalds 		 */
12261da177e4SLinus Torvalds 		static unsigned long    complain;
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
12291da177e4SLinus Torvalds 			dprintk("NFS: faulty NFS server %s:"
12301da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
123154ceac45SDavid Howells 				NFS_SERVER(data->inode)->nfs_client->cl_hostname,
12321da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
12331da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
12341da177e4SLinus Torvalds 		}
12351da177e4SLinus Torvalds 	}
12361da177e4SLinus Torvalds #endif
12371da177e4SLinus Torvalds 	/* Is this a short write? */
12381da177e4SLinus Torvalds 	if (task->tk_status >= 0 && resp->count < argp->count) {
12391da177e4SLinus Torvalds 		static unsigned long    complain;
12401da177e4SLinus Torvalds 
124191d5b470SChuck Lever 		nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
124291d5b470SChuck Lever 
12431da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
12441da177e4SLinus Torvalds 		if (resp->count != 0) {
12451da177e4SLinus Torvalds 			/* Was this an NFSv2 write or an NFSv3 stable write? */
12461da177e4SLinus Torvalds 			if (resp->verf->committed != NFS_UNSTABLE) {
12471da177e4SLinus Torvalds 				/* Resend from where the server left off */
12481da177e4SLinus Torvalds 				argp->offset += resp->count;
12491da177e4SLinus Torvalds 				argp->pgbase += resp->count;
12501da177e4SLinus Torvalds 				argp->count -= resp->count;
12511da177e4SLinus Torvalds 			} else {
12521da177e4SLinus Torvalds 				/* Resend as a stable write in order to avoid
12531da177e4SLinus Torvalds 				 * headaches in the case of a server crash.
12541da177e4SLinus Torvalds 				 */
12551da177e4SLinus Torvalds 				argp->stable = NFS_FILE_SYNC;
12561da177e4SLinus Torvalds 			}
12571da177e4SLinus Torvalds 			rpc_restart_call(task);
1258788e7a89STrond Myklebust 			return -EAGAIN;
12591da177e4SLinus Torvalds 		}
12601da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
12611da177e4SLinus Torvalds 			printk(KERN_WARNING
12621da177e4SLinus Torvalds 			       "NFS: Server wrote zero bytes, expected %u.\n",
12631da177e4SLinus Torvalds 					argp->count);
12641da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
12651da177e4SLinus Torvalds 		}
12661da177e4SLinus Torvalds 		/* Can't do anything about it except throw an error. */
12671da177e4SLinus Torvalds 		task->tk_status = -EIO;
12681da177e4SLinus Torvalds 	}
1269788e7a89STrond Myklebust 	return 0;
12701da177e4SLinus Torvalds }
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1274963d8fe5STrond Myklebust void nfs_commit_release(void *wdata)
12751da177e4SLinus Torvalds {
12761da177e4SLinus Torvalds 	nfs_commit_free(wdata);
12771da177e4SLinus Torvalds }
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds /*
12801da177e4SLinus Torvalds  * Set up the argument/result storage required for the RPC call.
12811da177e4SLinus Torvalds  */
12821da177e4SLinus Torvalds static void nfs_commit_rpcsetup(struct list_head *head,
1283788e7a89STrond Myklebust 		struct nfs_write_data *data,
1284788e7a89STrond Myklebust 		int how)
12851da177e4SLinus Torvalds {
12863da28eb1STrond Myklebust 	struct nfs_page		*first;
12871da177e4SLinus Torvalds 	struct inode		*inode;
1288788e7a89STrond Myklebust 	int flags;
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
12911da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds 	list_splice_init(head, &data->pages);
12941da177e4SLinus Torvalds 	first = nfs_list_entry(data->pages.next);
12951da177e4SLinus Torvalds 	inode = first->wb_context->dentry->d_inode;
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds 	data->inode	  = inode;
12981da177e4SLinus Torvalds 	data->cred	  = first->wb_context->cred;
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(data->inode);
13013da28eb1STrond Myklebust 	/* Note: we always request a commit of the entire inode */
13023da28eb1STrond Myklebust 	data->args.offset = 0;
13033da28eb1STrond Myklebust 	data->args.count  = 0;
13043da28eb1STrond Myklebust 	data->res.count   = 0;
13051da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
13061da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
13070e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
13081da177e4SLinus Torvalds 
1309788e7a89STrond Myklebust 	/* Set up the initial task struct.  */
1310788e7a89STrond Myklebust 	flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1311788e7a89STrond Myklebust 	rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
13121da177e4SLinus Torvalds 	NFS_PROTO(inode)->commit_setup(data, how);
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 	data->task.tk_priority = flush_task_priority(how);
13151da177e4SLinus Torvalds 	data->task.tk_cookie = (unsigned long)inode;
13161da177e4SLinus Torvalds 
13170bbacc40SChuck Lever 	dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
13181da177e4SLinus Torvalds }
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds /*
13211da177e4SLinus Torvalds  * Commit dirty pages
13221da177e4SLinus Torvalds  */
13231da177e4SLinus Torvalds static int
132440859d7eSChuck Lever nfs_commit_list(struct inode *inode, struct list_head *head, int how)
13251da177e4SLinus Torvalds {
13261da177e4SLinus Torvalds 	struct nfs_write_data	*data;
13271da177e4SLinus Torvalds 	struct nfs_page         *req;
13281da177e4SLinus Torvalds 
1329e9f7bee1STrond Myklebust 	data = nfs_commit_alloc();
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds 	if (!data)
13321da177e4SLinus Torvalds 		goto out_bad;
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds 	/* Set up the argument struct */
13351da177e4SLinus Torvalds 	nfs_commit_rpcsetup(head, data, how);
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds 	nfs_execute_write(data);
13381da177e4SLinus Torvalds 	return 0;
13391da177e4SLinus Torvalds  out_bad:
13401da177e4SLinus Torvalds 	while (!list_empty(head)) {
13411da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
13421da177e4SLinus Torvalds 		nfs_list_remove_request(req);
13431da177e4SLinus Torvalds 		nfs_mark_request_commit(req);
134483715ad5STrond Myklebust 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
13455c2d97cbSTrond Myklebust 		nfs_clear_page_writeback(req);
13461da177e4SLinus Torvalds 	}
13471da177e4SLinus Torvalds 	return -ENOMEM;
13481da177e4SLinus Torvalds }
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds /*
13511da177e4SLinus Torvalds  * COMMIT call returned
13521da177e4SLinus Torvalds  */
1353788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata)
13541da177e4SLinus Torvalds {
1355963d8fe5STrond Myklebust 	struct nfs_write_data	*data = calldata;
13561da177e4SLinus Torvalds 	struct nfs_page		*req;
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds         dprintk("NFS: %4d nfs_commit_done (status %d)\n",
13591da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
13601da177e4SLinus Torvalds 
1361788e7a89STrond Myklebust 	/* Call the NFS version-specific code */
1362788e7a89STrond Myklebust 	if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
1363788e7a89STrond Myklebust 		return;
1364788e7a89STrond Myklebust 
13651da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
13661da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
13671da177e4SLinus Torvalds 		nfs_list_remove_request(req);
1368fd39fc85SChristoph Lameter 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds 		dprintk("NFS: commit (%s/%Ld %d@%Ld)",
13711da177e4SLinus Torvalds 			req->wb_context->dentry->d_inode->i_sb->s_id,
13721da177e4SLinus Torvalds 			(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
13731da177e4SLinus Torvalds 			req->wb_bytes,
13741da177e4SLinus Torvalds 			(long long)req_offset(req));
13751da177e4SLinus Torvalds 		if (task->tk_status < 0) {
13761da177e4SLinus Torvalds 			req->wb_context->error = task->tk_status;
13771da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
13781da177e4SLinus Torvalds 			dprintk(", error = %d\n", task->tk_status);
13791da177e4SLinus Torvalds 			goto next;
13801da177e4SLinus Torvalds 		}
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
13831da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
13841da177e4SLinus Torvalds 		if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
13851da177e4SLinus Torvalds 			/* We have a match */
13861da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
13871da177e4SLinus Torvalds 			dprintk(" OK\n");
13881da177e4SLinus Torvalds 			goto next;
13891da177e4SLinus Torvalds 		}
13901da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
13911da177e4SLinus Torvalds 		dprintk(" mismatch\n");
13921da177e4SLinus Torvalds 		nfs_mark_request_dirty(req);
13931da177e4SLinus Torvalds 	next:
1394c6a556b8STrond Myklebust 		nfs_clear_page_writeback(req);
13951da177e4SLinus Torvalds 	}
13961da177e4SLinus Torvalds }
1397788e7a89STrond Myklebust 
1398788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = {
1399788e7a89STrond Myklebust 	.rpc_call_done = nfs_commit_done,
1400788e7a89STrond Myklebust 	.rpc_release = nfs_commit_release,
1401788e7a89STrond Myklebust };
1402c42de9ddSTrond Myklebust #else
1403c42de9ddSTrond Myklebust static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
1404c42de9ddSTrond Myklebust {
1405c42de9ddSTrond Myklebust 	return 0;
1406c42de9ddSTrond Myklebust }
14071da177e4SLinus Torvalds #endif
14081da177e4SLinus Torvalds 
14093f442547STrond Myklebust static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
14101da177e4SLinus Torvalds {
141128c6925fSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(mapping->host);
14121da177e4SLinus Torvalds 	LIST_HEAD(head);
14133f442547STrond Myklebust 	long res;
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds 	spin_lock(&nfsi->req_lock);
14163f442547STrond Myklebust 	res = nfs_scan_dirty(mapping, wbc, &head);
14171da177e4SLinus Torvalds 	spin_unlock(&nfsi->req_lock);
1418ab0a3dbeSTrond Myklebust 	if (res) {
141928c6925fSTrond Myklebust 		int error = nfs_flush_list(mapping->host, &head, res, how);
14201da177e4SLinus Torvalds 		if (error < 0)
14211da177e4SLinus Torvalds 			return error;
14227d46a49fSTrond Myklebust 	}
14231da177e4SLinus Torvalds 	return res;
14241da177e4SLinus Torvalds }
14251da177e4SLinus Torvalds 
14261da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
14273da28eb1STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
14281da177e4SLinus Torvalds {
14291da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
14301da177e4SLinus Torvalds 	LIST_HEAD(head);
14317d46a49fSTrond Myklebust 	int res;
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds 	spin_lock(&nfsi->req_lock);
14343da28eb1STrond Myklebust 	res = nfs_scan_commit(inode, &head, 0, 0);
14353da28eb1STrond Myklebust 	spin_unlock(&nfsi->req_lock);
14361da177e4SLinus Torvalds 	if (res) {
14377d46a49fSTrond Myklebust 		int error = nfs_commit_list(inode, &head, how);
14381da177e4SLinus Torvalds 		if (error < 0)
14391da177e4SLinus Torvalds 			return error;
14403da28eb1STrond Myklebust 	}
14411da177e4SLinus Torvalds 	return res;
14421da177e4SLinus Torvalds }
14431da177e4SLinus Torvalds #endif
14441da177e4SLinus Torvalds 
14451c75950bSTrond Myklebust long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
14461da177e4SLinus Torvalds {
14471c75950bSTrond Myklebust 	struct inode *inode = mapping->host;
1448c42de9ddSTrond Myklebust 	struct nfs_inode *nfsi = NFS_I(inode);
14491c75950bSTrond Myklebust 	unsigned long idx_start, idx_end;
14501c75950bSTrond Myklebust 	unsigned int npages = 0;
1451c42de9ddSTrond Myklebust 	LIST_HEAD(head);
145270b9ecbdSTrond Myklebust 	int nocommit = how & FLUSH_NOCOMMIT;
14533f442547STrond Myklebust 	long pages, ret;
14541da177e4SLinus Torvalds 
14551c75950bSTrond Myklebust 	/* FIXME */
14561c75950bSTrond Myklebust 	if (wbc->range_cyclic)
14571c75950bSTrond Myklebust 		idx_start = 0;
14581c75950bSTrond Myklebust 	else {
14591c75950bSTrond Myklebust 		idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
14601c75950bSTrond Myklebust 		idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
14611c75950bSTrond Myklebust 		if (idx_end > idx_start) {
14621c75950bSTrond Myklebust 			unsigned long l_npages = 1 + idx_end - idx_start;
14631c75950bSTrond Myklebust 			npages = l_npages;
14641c75950bSTrond Myklebust 			if (sizeof(npages) != sizeof(l_npages) &&
14651c75950bSTrond Myklebust 					(unsigned long)npages != l_npages)
14661c75950bSTrond Myklebust 				npages = 0;
14671c75950bSTrond Myklebust 		}
14681c75950bSTrond Myklebust 	}
1469c42de9ddSTrond Myklebust 	how &= ~FLUSH_NOCOMMIT;
1470c42de9ddSTrond Myklebust 	spin_lock(&nfsi->req_lock);
14711da177e4SLinus Torvalds 	do {
14721c75950bSTrond Myklebust 		wbc->pages_skipped = 0;
1473c42de9ddSTrond Myklebust 		ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
1474c42de9ddSTrond Myklebust 		if (ret != 0)
1475c42de9ddSTrond Myklebust 			continue;
14761c75950bSTrond Myklebust 		pages = nfs_scan_dirty(mapping, wbc, &head);
1477c42de9ddSTrond Myklebust 		if (pages != 0) {
1478c42de9ddSTrond Myklebust 			spin_unlock(&nfsi->req_lock);
1479e8e058e8STrond Myklebust 			if (how & FLUSH_INVALIDATE) {
148083715ad5STrond Myklebust 				nfs_cancel_dirty_list(&head);
1481e8e058e8STrond Myklebust 				ret = pages;
1482e8e058e8STrond Myklebust 			} else
1483c42de9ddSTrond Myklebust 				ret = nfs_flush_list(inode, &head, pages, how);
1484c42de9ddSTrond Myklebust 			spin_lock(&nfsi->req_lock);
148570b9ecbdSTrond Myklebust 			continue;
148670b9ecbdSTrond Myklebust 		}
14871c75950bSTrond Myklebust 		if (wbc->pages_skipped != 0)
14881c75950bSTrond Myklebust 			continue;
1489c42de9ddSTrond Myklebust 		if (nocommit)
1490c42de9ddSTrond Myklebust 			break;
1491d2ccddf0STrond Myklebust 		pages = nfs_scan_commit(inode, &head, idx_start, npages);
14921c75950bSTrond Myklebust 		if (pages == 0) {
14931c75950bSTrond Myklebust 			if (wbc->pages_skipped != 0)
14941c75950bSTrond Myklebust 				continue;
1495c42de9ddSTrond Myklebust 			break;
14961c75950bSTrond Myklebust 		}
1497d2ccddf0STrond Myklebust 		if (how & FLUSH_INVALIDATE) {
1498d2ccddf0STrond Myklebust 			spin_unlock(&nfsi->req_lock);
149983715ad5STrond Myklebust 			nfs_cancel_commit_list(&head);
1500e8e058e8STrond Myklebust 			ret = pages;
1501d2ccddf0STrond Myklebust 			spin_lock(&nfsi->req_lock);
1502d2ccddf0STrond Myklebust 			continue;
1503d2ccddf0STrond Myklebust 		}
1504d2ccddf0STrond Myklebust 		pages += nfs_scan_commit(inode, &head, 0, 0);
1505c42de9ddSTrond Myklebust 		spin_unlock(&nfsi->req_lock);
1506c42de9ddSTrond Myklebust 		ret = nfs_commit_list(inode, &head, how);
1507c42de9ddSTrond Myklebust 		spin_lock(&nfsi->req_lock);
1508c42de9ddSTrond Myklebust 	} while (ret >= 0);
1509c42de9ddSTrond Myklebust 	spin_unlock(&nfsi->req_lock);
1510c42de9ddSTrond Myklebust 	return ret;
15111da177e4SLinus Torvalds }
15121da177e4SLinus Torvalds 
15131c75950bSTrond Myklebust /*
15141c75950bSTrond Myklebust  * flush the inode to disk.
15151c75950bSTrond Myklebust  */
15161c75950bSTrond Myklebust int nfs_wb_all(struct inode *inode)
15171c75950bSTrond Myklebust {
15181c75950bSTrond Myklebust 	struct address_space *mapping = inode->i_mapping;
15191c75950bSTrond Myklebust 	struct writeback_control wbc = {
15201c75950bSTrond Myklebust 		.bdi = mapping->backing_dev_info,
15211c75950bSTrond Myklebust 		.sync_mode = WB_SYNC_ALL,
15221c75950bSTrond Myklebust 		.nr_to_write = LONG_MAX,
15231c75950bSTrond Myklebust 		.range_cyclic = 1,
15241c75950bSTrond Myklebust 	};
15251c75950bSTrond Myklebust 	int ret;
15261c75950bSTrond Myklebust 
15271c75950bSTrond Myklebust 	ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
15281c75950bSTrond Myklebust 	if (ret >= 0)
15291c75950bSTrond Myklebust 		return 0;
15301c75950bSTrond Myklebust 	return ret;
15311c75950bSTrond Myklebust }
15321c75950bSTrond Myklebust 
15331c75950bSTrond Myklebust int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
15341c75950bSTrond Myklebust {
15351c75950bSTrond Myklebust 	struct writeback_control wbc = {
15361c75950bSTrond Myklebust 		.bdi = mapping->backing_dev_info,
15371c75950bSTrond Myklebust 		.sync_mode = WB_SYNC_ALL,
15381c75950bSTrond Myklebust 		.nr_to_write = LONG_MAX,
15391c75950bSTrond Myklebust 		.range_start = range_start,
15401c75950bSTrond Myklebust 		.range_end = range_end,
15411c75950bSTrond Myklebust 	};
15421c75950bSTrond Myklebust 	int ret;
15431c75950bSTrond Myklebust 
15441c75950bSTrond Myklebust 	ret = nfs_sync_mapping_wait(mapping, &wbc, how);
15451c75950bSTrond Myklebust 	if (ret >= 0)
15461c75950bSTrond Myklebust 		return 0;
15471c75950bSTrond Myklebust 	return ret;
15481c75950bSTrond Myklebust }
15491c75950bSTrond Myklebust 
15501c75950bSTrond Myklebust static int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
15511c75950bSTrond Myklebust {
15521c75950bSTrond Myklebust 	loff_t range_start = page_offset(page);
15531c75950bSTrond Myklebust 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
15541c75950bSTrond Myklebust 
15551c75950bSTrond Myklebust 	return nfs_sync_mapping_range(inode->i_mapping, range_start, range_end, how | FLUSH_STABLE);
15561c75950bSTrond Myklebust }
15571c75950bSTrond Myklebust 
15581c75950bSTrond Myklebust /*
15591c75950bSTrond Myklebust  * Write back all requests on one page - we do this before reading it.
15601c75950bSTrond Myklebust  */
15611c75950bSTrond Myklebust int nfs_wb_page(struct inode *inode, struct page* page)
15621c75950bSTrond Myklebust {
15631c75950bSTrond Myklebust 	return nfs_wb_page_priority(inode, page, 0);
15641c75950bSTrond Myklebust }
15651c75950bSTrond Myklebust 
15661c75950bSTrond Myklebust 
1567f7b422b1SDavid Howells int __init nfs_init_writepagecache(void)
15681da177e4SLinus Torvalds {
15691da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
15701da177e4SLinus Torvalds 					     sizeof(struct nfs_write_data),
15711da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
15721da177e4SLinus Torvalds 					     NULL, NULL);
15731da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
15741da177e4SLinus Torvalds 		return -ENOMEM;
15751da177e4SLinus Torvalds 
157693d2341cSMatthew Dobson 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
15771da177e4SLinus Torvalds 						     nfs_wdata_cachep);
15781da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
15791da177e4SLinus Torvalds 		return -ENOMEM;
15801da177e4SLinus Torvalds 
158193d2341cSMatthew Dobson 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
15821da177e4SLinus Torvalds 						      nfs_wdata_cachep);
15831da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
15841da177e4SLinus Torvalds 		return -ENOMEM;
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds 	return 0;
15871da177e4SLinus Torvalds }
15881da177e4SLinus Torvalds 
1589266bee88SDavid Brownell void nfs_destroy_writepagecache(void)
15901da177e4SLinus Torvalds {
15911da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
15921da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
15931a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(nfs_wdata_cachep);
15941da177e4SLinus Torvalds }
15951da177e4SLinus Torvalds 
1596