xref: /linux/fs/nfs/write.c (revision 70b9ecbdb9c5fdc731f8780bffd45d9519020c4a)
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/config.h>
501da177e4SLinus Torvalds #include <linux/types.h>
511da177e4SLinus Torvalds #include <linux/slab.h>
521da177e4SLinus Torvalds #include <linux/mm.h>
531da177e4SLinus Torvalds #include <linux/pagemap.h>
541da177e4SLinus Torvalds #include <linux/file.h>
551da177e4SLinus Torvalds #include <linux/mpage.h>
561da177e4SLinus Torvalds #include <linux/writeback.h>
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
591da177e4SLinus Torvalds #include <linux/nfs_fs.h>
601da177e4SLinus Torvalds #include <linux/nfs_mount.h>
611da177e4SLinus Torvalds #include <linux/nfs_page.h>
621da177e4SLinus Torvalds #include <asm/uaccess.h>
631da177e4SLinus Torvalds #include <linux/smp_lock.h>
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds #include "delegation.h"
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds #define NFSDBG_FACILITY		NFSDBG_PAGECACHE
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #define MIN_POOL_WRITE		(32)
701da177e4SLinus Torvalds #define MIN_POOL_COMMIT		(4)
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds /*
731da177e4SLinus Torvalds  * Local function declarations
741da177e4SLinus Torvalds  */
751da177e4SLinus Torvalds static struct nfs_page * nfs_update_request(struct nfs_open_context*,
761da177e4SLinus Torvalds 					    struct inode *,
771da177e4SLinus Torvalds 					    struct page *,
781da177e4SLinus Torvalds 					    unsigned int, unsigned int);
791da177e4SLinus Torvalds static void nfs_writeback_done_partial(struct nfs_write_data *, int);
801da177e4SLinus Torvalds static void nfs_writeback_done_full(struct nfs_write_data *, 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);
831da177e4SLinus Torvalds static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
841da177e4SLinus Torvalds 			   unsigned int npages, int how);
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds static kmem_cache_t *nfs_wdata_cachep;
871da177e4SLinus Torvalds mempool_t *nfs_wdata_mempool;
881da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
911da177e4SLinus Torvalds 
9240859d7eSChuck Lever static inline struct nfs_write_data *nfs_commit_alloc(unsigned int pagecount)
931da177e4SLinus Torvalds {
941da177e4SLinus Torvalds 	struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS);
9540859d7eSChuck Lever 
961da177e4SLinus Torvalds 	if (p) {
971da177e4SLinus Torvalds 		memset(p, 0, sizeof(*p));
981da177e4SLinus Torvalds 		INIT_LIST_HEAD(&p->pages);
9940859d7eSChuck Lever 		if (pagecount < NFS_PAGEVEC_SIZE)
10040859d7eSChuck Lever 			p->pagevec = &p->page_array[0];
10140859d7eSChuck Lever 		else {
10240859d7eSChuck Lever 			size_t size = ++pagecount * sizeof(struct page *);
10340859d7eSChuck Lever 			p->pagevec = kmalloc(size, GFP_NOFS);
10440859d7eSChuck Lever 			if (p->pagevec) {
10540859d7eSChuck Lever 				memset(p->pagevec, 0, size);
10640859d7eSChuck Lever 			} else {
10740859d7eSChuck Lever 				mempool_free(p, nfs_commit_mempool);
10840859d7eSChuck Lever 				p = NULL;
10940859d7eSChuck Lever 			}
11040859d7eSChuck Lever 		}
1111da177e4SLinus Torvalds 	}
1121da177e4SLinus Torvalds 	return p;
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds static inline void nfs_commit_free(struct nfs_write_data *p)
1161da177e4SLinus Torvalds {
11740859d7eSChuck Lever 	if (p && (p->pagevec != &p->page_array[0]))
11840859d7eSChuck Lever 		kfree(p->pagevec);
1191da177e4SLinus Torvalds 	mempool_free(p, nfs_commit_mempool);
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds 
122963d8fe5STrond Myklebust void nfs_writedata_release(void *wdata)
1231da177e4SLinus Torvalds {
1241da177e4SLinus Torvalds 	nfs_writedata_free(wdata);
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */
1281da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
1291da177e4SLinus Torvalds {
1301da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
1311da177e4SLinus Torvalds 	loff_t end, i_size = i_size_read(inode);
1321da177e4SLinus Torvalds 	unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	if (i_size > 0 && page->index < end_index)
1351da177e4SLinus Torvalds 		return;
1361da177e4SLinus Torvalds 	end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
1371da177e4SLinus Torvalds 	if (i_size >= end)
1381da177e4SLinus Torvalds 		return;
1391da177e4SLinus Torvalds 	i_size_write(inode, end);
1401da177e4SLinus Torvalds }
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request
1431da177e4SLinus Torvalds  * covers the full page.
1441da177e4SLinus Torvalds  */
1451da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
1461da177e4SLinus Torvalds {
1471da177e4SLinus Torvalds 	loff_t end_offs;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	if (PageUptodate(page))
1501da177e4SLinus Torvalds 		return;
1511da177e4SLinus Torvalds 	if (base != 0)
1521da177e4SLinus Torvalds 		return;
1531da177e4SLinus Torvalds 	if (count == PAGE_CACHE_SIZE) {
1541da177e4SLinus Torvalds 		SetPageUptodate(page);
1551da177e4SLinus Torvalds 		return;
1561da177e4SLinus Torvalds 	}
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	end_offs = i_size_read(page->mapping->host) - 1;
1591da177e4SLinus Torvalds 	if (end_offs < 0)
1601da177e4SLinus Torvalds 		return;
1611da177e4SLinus Torvalds 	/* Is this the last page? */
1621da177e4SLinus Torvalds 	if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT))
1631da177e4SLinus Torvalds 		return;
1641da177e4SLinus Torvalds 	/* This is the last page: set PG_uptodate if we cover the entire
1651da177e4SLinus Torvalds 	 * extent of the data, then zero the rest of the page.
1661da177e4SLinus Torvalds 	 */
1671da177e4SLinus Torvalds 	if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) {
1681da177e4SLinus Torvalds 		memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
1691da177e4SLinus Torvalds 		SetPageUptodate(page);
1701da177e4SLinus Torvalds 	}
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds /*
1741da177e4SLinus Torvalds  * Write a page synchronously.
1751da177e4SLinus Torvalds  * Offset is the data offset within the page.
1761da177e4SLinus Torvalds  */
1771da177e4SLinus Torvalds static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode,
1781da177e4SLinus Torvalds 		struct page *page, unsigned int offset, unsigned int count,
1791da177e4SLinus Torvalds 		int how)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	unsigned int	wsize = NFS_SERVER(inode)->wsize;
1821da177e4SLinus Torvalds 	int		result, written = 0;
1831da177e4SLinus Torvalds 	struct nfs_write_data *wdata;
1841da177e4SLinus Torvalds 
18540859d7eSChuck Lever 	wdata = nfs_writedata_alloc(1);
1861da177e4SLinus Torvalds 	if (!wdata)
1871da177e4SLinus Torvalds 		return -ENOMEM;
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	wdata->flags = how;
1901da177e4SLinus Torvalds 	wdata->cred = ctx->cred;
1911da177e4SLinus Torvalds 	wdata->inode = inode;
1921da177e4SLinus Torvalds 	wdata->args.fh = NFS_FH(inode);
1931da177e4SLinus Torvalds 	wdata->args.context = ctx;
1941da177e4SLinus Torvalds 	wdata->args.pages = &page;
1951da177e4SLinus Torvalds 	wdata->args.stable = NFS_FILE_SYNC;
1961da177e4SLinus Torvalds 	wdata->args.pgbase = offset;
1971da177e4SLinus Torvalds 	wdata->args.count = wsize;
1981da177e4SLinus Torvalds 	wdata->res.fattr = &wdata->fattr;
1991da177e4SLinus Torvalds 	wdata->res.verf = &wdata->verf;
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	dprintk("NFS:      nfs_writepage_sync(%s/%Ld %d@%Ld)\n",
2021da177e4SLinus Torvalds 		inode->i_sb->s_id,
2031da177e4SLinus Torvalds 		(long long)NFS_FILEID(inode),
2041da177e4SLinus Torvalds 		count, (long long)(page_offset(page) + offset));
2051da177e4SLinus Torvalds 
206bb713d6dSTrond Myklebust 	set_page_writeback(page);
2071da177e4SLinus Torvalds 	nfs_begin_data_update(inode);
2081da177e4SLinus Torvalds 	do {
2091da177e4SLinus Torvalds 		if (count < wsize)
2101da177e4SLinus Torvalds 			wdata->args.count = count;
2111da177e4SLinus Torvalds 		wdata->args.offset = page_offset(page) + wdata->args.pgbase;
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 		result = NFS_PROTO(inode)->write(wdata);
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 		if (result < 0) {
2161da177e4SLinus Torvalds 			/* Must mark the page invalid after I/O error */
2171da177e4SLinus Torvalds 			ClearPageUptodate(page);
2181da177e4SLinus Torvalds 			goto io_error;
2191da177e4SLinus Torvalds 		}
2201da177e4SLinus Torvalds 		if (result < wdata->args.count)
2211da177e4SLinus Torvalds 			printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n",
2221da177e4SLinus Torvalds 					wdata->args.count, result);
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 		wdata->args.offset += result;
2251da177e4SLinus Torvalds 	        wdata->args.pgbase += result;
2261da177e4SLinus Torvalds 		written += result;
2271da177e4SLinus Torvalds 		count -= result;
2281da177e4SLinus Torvalds 	} while (count);
2291da177e4SLinus Torvalds 	/* Update file length */
2301da177e4SLinus Torvalds 	nfs_grow_file(page, offset, written);
2311da177e4SLinus Torvalds 	/* Set the PG_uptodate flag? */
2321da177e4SLinus Torvalds 	nfs_mark_uptodate(page, offset, written);
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	if (PageError(page))
2351da177e4SLinus Torvalds 		ClearPageError(page);
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds io_error:
238951a143bSTrond Myklebust 	nfs_end_data_update(inode);
239bb713d6dSTrond Myklebust 	end_page_writeback(page);
2401da177e4SLinus Torvalds 	nfs_writedata_free(wdata);
2411da177e4SLinus Torvalds 	return written ? written : result;
2421da177e4SLinus Torvalds }
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds static int nfs_writepage_async(struct nfs_open_context *ctx,
2451da177e4SLinus Torvalds 		struct inode *inode, struct page *page,
2461da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
2471da177e4SLinus Torvalds {
2481da177e4SLinus Torvalds 	struct nfs_page	*req;
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	req = nfs_update_request(ctx, inode, page, offset, count);
251abd3e641STrond Myklebust 	if (IS_ERR(req))
252abd3e641STrond Myklebust 		return PTR_ERR(req);
2531da177e4SLinus Torvalds 	/* Update file length */
2541da177e4SLinus Torvalds 	nfs_grow_file(page, offset, count);
2551da177e4SLinus Torvalds 	/* Set the PG_uptodate flag? */
2561da177e4SLinus Torvalds 	nfs_mark_uptodate(page, offset, count);
2571da177e4SLinus Torvalds 	nfs_unlock_request(req);
258abd3e641STrond Myklebust 	return 0;
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc)
2621da177e4SLinus Torvalds {
2631da177e4SLinus Torvalds 	if (wbc->for_reclaim)
2641da177e4SLinus Torvalds 		return FLUSH_HIGHPRI;
2651da177e4SLinus Torvalds 	if (wbc->for_kupdate)
2661da177e4SLinus Torvalds 		return FLUSH_LOWPRI;
2671da177e4SLinus Torvalds 	return 0;
2681da177e4SLinus Torvalds }
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds /*
2711da177e4SLinus Torvalds  * Write an mmapped page to the server.
2721da177e4SLinus Torvalds  */
2731da177e4SLinus Torvalds int nfs_writepage(struct page *page, struct writeback_control *wbc)
2741da177e4SLinus Torvalds {
2751da177e4SLinus Torvalds 	struct nfs_open_context *ctx;
2761da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
2771da177e4SLinus Torvalds 	unsigned long end_index;
2781da177e4SLinus Torvalds 	unsigned offset = PAGE_CACHE_SIZE;
2791da177e4SLinus Torvalds 	loff_t i_size = i_size_read(inode);
2801da177e4SLinus Torvalds 	int inode_referenced = 0;
2811da177e4SLinus Torvalds 	int priority = wb_priority(wbc);
2821da177e4SLinus Torvalds 	int err;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	/*
2851da177e4SLinus Torvalds 	 * Note: We need to ensure that we have a reference to the inode
2861da177e4SLinus Torvalds 	 *       if we are to do asynchronous writes. If not, waiting
2871da177e4SLinus Torvalds 	 *       in nfs_wait_on_request() may deadlock with clear_inode().
2881da177e4SLinus Torvalds 	 *
2891da177e4SLinus Torvalds 	 *       If igrab() fails here, then it is in any case safe to
2901da177e4SLinus Torvalds 	 *       call nfs_wb_page(), since there will be no pending writes.
2911da177e4SLinus Torvalds 	 */
2921da177e4SLinus Torvalds 	if (igrab(inode) != 0)
2931da177e4SLinus Torvalds 		inode_referenced = 1;
2941da177e4SLinus Torvalds 	end_index = i_size >> PAGE_CACHE_SHIFT;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	/* Ensure we've flushed out any previous writes */
2971da177e4SLinus Torvalds 	nfs_wb_page_priority(inode, page, priority);
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	/* easy case */
3001da177e4SLinus Torvalds 	if (page->index < end_index)
3011da177e4SLinus Torvalds 		goto do_it;
3021da177e4SLinus Torvalds 	/* things got complicated... */
3031da177e4SLinus Torvalds 	offset = i_size & (PAGE_CACHE_SIZE-1);
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	/* OK, are we completely out? */
3061da177e4SLinus Torvalds 	err = 0; /* potential race with truncate - ignore */
3071da177e4SLinus Torvalds 	if (page->index >= end_index+1 || !offset)
3081da177e4SLinus Torvalds 		goto out;
3091da177e4SLinus Torvalds do_it:
310d530838bSTrond Myklebust 	ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
3111da177e4SLinus Torvalds 	if (ctx == NULL) {
3121da177e4SLinus Torvalds 		err = -EBADF;
3131da177e4SLinus Torvalds 		goto out;
3141da177e4SLinus Torvalds 	}
3151da177e4SLinus Torvalds 	lock_kernel();
3161da177e4SLinus Torvalds 	if (!IS_SYNC(inode) && inode_referenced) {
3171da177e4SLinus Torvalds 		err = nfs_writepage_async(ctx, inode, page, 0, offset);
318abd3e641STrond Myklebust 		if (!wbc->for_writepages)
319abd3e641STrond Myklebust 			nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
3201da177e4SLinus Torvalds 	} else {
3211da177e4SLinus Torvalds 		err = nfs_writepage_sync(ctx, inode, page, 0,
3221da177e4SLinus Torvalds 						offset, priority);
3231da177e4SLinus Torvalds 		if (err >= 0) {
3241da177e4SLinus Torvalds 			if (err != offset)
3251da177e4SLinus Torvalds 				redirty_page_for_writepage(wbc, page);
3261da177e4SLinus Torvalds 			err = 0;
3271da177e4SLinus Torvalds 		}
3281da177e4SLinus Torvalds 	}
3291da177e4SLinus Torvalds 	unlock_kernel();
3301da177e4SLinus Torvalds 	put_nfs_open_context(ctx);
3311da177e4SLinus Torvalds out:
3321da177e4SLinus Torvalds 	unlock_page(page);
3331da177e4SLinus Torvalds 	if (inode_referenced)
3341da177e4SLinus Torvalds 		iput(inode);
3351da177e4SLinus Torvalds 	return err;
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds /*
3391da177e4SLinus Torvalds  * Note: causes nfs_update_request() to block on the assumption
3401da177e4SLinus Torvalds  * 	 that the writeback is generated due to memory pressure.
3411da177e4SLinus Torvalds  */
3421da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	struct backing_dev_info *bdi = mapping->backing_dev_info;
3451da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
3461da177e4SLinus Torvalds 	int err;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	err = generic_writepages(mapping, wbc);
3491da177e4SLinus Torvalds 	if (err)
3501da177e4SLinus Torvalds 		return err;
3511da177e4SLinus Torvalds 	while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
3521da177e4SLinus Torvalds 		if (wbc->nonblocking)
3531da177e4SLinus Torvalds 			return 0;
3541da177e4SLinus Torvalds 		nfs_wait_on_write_congestion(mapping, 0);
3551da177e4SLinus Torvalds 	}
3561da177e4SLinus Torvalds 	err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc));
3571da177e4SLinus Torvalds 	if (err < 0)
3581da177e4SLinus Torvalds 		goto out;
3591da177e4SLinus Torvalds 	wbc->nr_to_write -= err;
3601da177e4SLinus Torvalds 	if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
3611da177e4SLinus Torvalds 		err = nfs_wait_on_requests(inode, 0, 0);
3621da177e4SLinus Torvalds 		if (err < 0)
3631da177e4SLinus Torvalds 			goto out;
3641da177e4SLinus Torvalds 	}
3653da28eb1STrond Myklebust 	err = nfs_commit_inode(inode, wb_priority(wbc));
3661da177e4SLinus Torvalds 	if (err > 0) {
3671da177e4SLinus Torvalds 		wbc->nr_to_write -= err;
3681da177e4SLinus Torvalds 		err = 0;
3691da177e4SLinus Torvalds 	}
3701da177e4SLinus Torvalds out:
3711da177e4SLinus Torvalds 	clear_bit(BDI_write_congested, &bdi->state);
3721da177e4SLinus Torvalds 	wake_up_all(&nfs_write_congestion);
3731da177e4SLinus Torvalds 	return err;
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds /*
3771da177e4SLinus Torvalds  * Insert a write request into an inode
3781da177e4SLinus Torvalds  */
3791da177e4SLinus Torvalds static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
3801da177e4SLinus Torvalds {
3811da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
3821da177e4SLinus Torvalds 	int error;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
3851da177e4SLinus Torvalds 	BUG_ON(error == -EEXIST);
3861da177e4SLinus Torvalds 	if (error)
3871da177e4SLinus Torvalds 		return error;
3881da177e4SLinus Torvalds 	if (!nfsi->npages) {
3891da177e4SLinus Torvalds 		igrab(inode);
3901da177e4SLinus Torvalds 		nfs_begin_data_update(inode);
3911da177e4SLinus Torvalds 		if (nfs_have_delegation(inode, FMODE_WRITE))
3921da177e4SLinus Torvalds 			nfsi->change_attr++;
3931da177e4SLinus Torvalds 	}
3941da177e4SLinus Torvalds 	nfsi->npages++;
3951da177e4SLinus Torvalds 	atomic_inc(&req->wb_count);
3961da177e4SLinus Torvalds 	return 0;
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds /*
4001da177e4SLinus Torvalds  * Insert a write request into an inode
4011da177e4SLinus Torvalds  */
4021da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req)
4031da177e4SLinus Torvalds {
4041da177e4SLinus Torvalds 	struct inode *inode = req->wb_context->dentry->d_inode;
4051da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	BUG_ON (!NFS_WBACK_BUSY(req));
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 	spin_lock(&nfsi->req_lock);
4101da177e4SLinus Torvalds 	radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
4111da177e4SLinus Torvalds 	nfsi->npages--;
4121da177e4SLinus Torvalds 	if (!nfsi->npages) {
4131da177e4SLinus Torvalds 		spin_unlock(&nfsi->req_lock);
414951a143bSTrond Myklebust 		nfs_end_data_update(inode);
4151da177e4SLinus Torvalds 		iput(inode);
4161da177e4SLinus Torvalds 	} else
4171da177e4SLinus Torvalds 		spin_unlock(&nfsi->req_lock);
4181da177e4SLinus Torvalds 	nfs_clear_request(req);
4191da177e4SLinus Torvalds 	nfs_release_request(req);
4201da177e4SLinus Torvalds }
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds /*
4231da177e4SLinus Torvalds  * Find a request
4241da177e4SLinus Torvalds  */
4251da177e4SLinus Torvalds static inline struct nfs_page *
4261da177e4SLinus Torvalds _nfs_find_request(struct inode *inode, unsigned long index)
4271da177e4SLinus Torvalds {
4281da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4291da177e4SLinus Torvalds 	struct nfs_page *req;
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index);
4321da177e4SLinus Torvalds 	if (req)
4331da177e4SLinus Torvalds 		atomic_inc(&req->wb_count);
4341da177e4SLinus Torvalds 	return req;
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds static struct nfs_page *
4381da177e4SLinus Torvalds nfs_find_request(struct inode *inode, unsigned long index)
4391da177e4SLinus Torvalds {
4401da177e4SLinus Torvalds 	struct nfs_page		*req;
4411da177e4SLinus Torvalds 	struct nfs_inode	*nfsi = NFS_I(inode);
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	spin_lock(&nfsi->req_lock);
4441da177e4SLinus Torvalds 	req = _nfs_find_request(inode, index);
4451da177e4SLinus Torvalds 	spin_unlock(&nfsi->req_lock);
4461da177e4SLinus Torvalds 	return req;
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds /*
4501da177e4SLinus Torvalds  * Add a request to the inode's dirty list.
4511da177e4SLinus Torvalds  */
4521da177e4SLinus Torvalds static void
4531da177e4SLinus Torvalds nfs_mark_request_dirty(struct nfs_page *req)
4541da177e4SLinus Torvalds {
4551da177e4SLinus Torvalds 	struct inode *inode = req->wb_context->dentry->d_inode;
4561da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	spin_lock(&nfsi->req_lock);
4593da28eb1STrond Myklebust 	radix_tree_tag_set(&nfsi->nfs_page_tree,
4603da28eb1STrond Myklebust 			req->wb_index, NFS_PAGE_TAG_DIRTY);
4611da177e4SLinus Torvalds 	nfs_list_add_request(req, &nfsi->dirty);
4621da177e4SLinus Torvalds 	nfsi->ndirty++;
4631da177e4SLinus Torvalds 	spin_unlock(&nfsi->req_lock);
4641da177e4SLinus Torvalds 	inc_page_state(nr_dirty);
4651da177e4SLinus Torvalds 	mark_inode_dirty(inode);
4661da177e4SLinus Torvalds }
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds /*
4691da177e4SLinus Torvalds  * Check if a request is dirty
4701da177e4SLinus Torvalds  */
4711da177e4SLinus Torvalds static inline int
4721da177e4SLinus Torvalds nfs_dirty_request(struct nfs_page *req)
4731da177e4SLinus Torvalds {
4741da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode);
4751da177e4SLinus Torvalds 	return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty;
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
4791da177e4SLinus Torvalds /*
4801da177e4SLinus Torvalds  * Add a request to the inode's commit list.
4811da177e4SLinus Torvalds  */
4821da177e4SLinus Torvalds static void
4831da177e4SLinus Torvalds nfs_mark_request_commit(struct nfs_page *req)
4841da177e4SLinus Torvalds {
4851da177e4SLinus Torvalds 	struct inode *inode = req->wb_context->dentry->d_inode;
4861da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	spin_lock(&nfsi->req_lock);
4891da177e4SLinus Torvalds 	nfs_list_add_request(req, &nfsi->commit);
4901da177e4SLinus Torvalds 	nfsi->ncommit++;
4911da177e4SLinus Torvalds 	spin_unlock(&nfsi->req_lock);
4921da177e4SLinus Torvalds 	inc_page_state(nr_unstable);
4931da177e4SLinus Torvalds 	mark_inode_dirty(inode);
4941da177e4SLinus Torvalds }
4951da177e4SLinus Torvalds #endif
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds /*
4981da177e4SLinus Torvalds  * Wait for a request to complete.
4991da177e4SLinus Torvalds  *
5001da177e4SLinus Torvalds  * Interruptible by signals only if mounted with intr flag.
5011da177e4SLinus Torvalds  */
5021da177e4SLinus Torvalds static int
5031da177e4SLinus Torvalds nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
5041da177e4SLinus Torvalds {
5051da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
5061da177e4SLinus Torvalds 	struct nfs_page *req;
5071da177e4SLinus Torvalds 	unsigned long		idx_end, next;
5081da177e4SLinus Torvalds 	unsigned int		res = 0;
5091da177e4SLinus Torvalds 	int			error;
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	if (npages == 0)
5121da177e4SLinus Torvalds 		idx_end = ~0;
5131da177e4SLinus Torvalds 	else
5141da177e4SLinus Torvalds 		idx_end = idx_start + npages - 1;
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	spin_lock(&nfsi->req_lock);
5171da177e4SLinus Torvalds 	next = idx_start;
518c6a556b8STrond Myklebust 	while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
5191da177e4SLinus Torvalds 		if (req->wb_index > idx_end)
5201da177e4SLinus Torvalds 			break;
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 		next = req->wb_index + 1;
523c6a556b8STrond Myklebust 		BUG_ON(!NFS_WBACK_BUSY(req));
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 		atomic_inc(&req->wb_count);
5261da177e4SLinus Torvalds 		spin_unlock(&nfsi->req_lock);
5271da177e4SLinus Torvalds 		error = nfs_wait_on_request(req);
5281da177e4SLinus Torvalds 		nfs_release_request(req);
5291da177e4SLinus Torvalds 		if (error < 0)
5301da177e4SLinus Torvalds 			return error;
5311da177e4SLinus Torvalds 		spin_lock(&nfsi->req_lock);
5321da177e4SLinus Torvalds 		res++;
5331da177e4SLinus Torvalds 	}
5341da177e4SLinus Torvalds 	spin_unlock(&nfsi->req_lock);
5351da177e4SLinus Torvalds 	return res;
5361da177e4SLinus Torvalds }
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds /*
5391da177e4SLinus Torvalds  * nfs_scan_dirty - Scan an inode for dirty requests
5401da177e4SLinus Torvalds  * @inode: NFS inode to scan
5411da177e4SLinus Torvalds  * @dst: destination list
5421da177e4SLinus Torvalds  * @idx_start: lower bound of page->index to scan.
5431da177e4SLinus Torvalds  * @npages: idx_start + npages sets the upper bound to scan.
5441da177e4SLinus Torvalds  *
5451da177e4SLinus Torvalds  * Moves requests from the inode's dirty page list.
5461da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
5471da177e4SLinus Torvalds  */
5481da177e4SLinus Torvalds static int
5491da177e4SLinus Torvalds nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
5501da177e4SLinus Torvalds {
5511da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
5523da28eb1STrond Myklebust 	int res = 0;
5533da28eb1STrond Myklebust 
5543da28eb1STrond Myklebust 	if (nfsi->ndirty != 0) {
5553da28eb1STrond Myklebust 		res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages);
5561da177e4SLinus Torvalds 		nfsi->ndirty -= res;
5571da177e4SLinus Torvalds 		sub_page_state(nr_dirty,res);
5581da177e4SLinus Torvalds 		if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty))
5591da177e4SLinus Torvalds 			printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n");
5603da28eb1STrond Myklebust 	}
5611da177e4SLinus Torvalds 	return res;
5621da177e4SLinus Torvalds }
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
5651da177e4SLinus Torvalds /*
5661da177e4SLinus Torvalds  * nfs_scan_commit - Scan an inode for commit requests
5671da177e4SLinus Torvalds  * @inode: NFS inode to scan
5681da177e4SLinus Torvalds  * @dst: destination list
5691da177e4SLinus Torvalds  * @idx_start: lower bound of page->index to scan.
5701da177e4SLinus Torvalds  * @npages: idx_start + npages sets the upper bound to scan.
5711da177e4SLinus Torvalds  *
5721da177e4SLinus Torvalds  * Moves requests from the inode's 'commit' request list.
5731da177e4SLinus Torvalds  * The requests are *not* checked to ensure that they form a contiguous set.
5741da177e4SLinus Torvalds  */
5751da177e4SLinus Torvalds static int
5761da177e4SLinus Torvalds nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
5771da177e4SLinus Torvalds {
5781da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
5793da28eb1STrond Myklebust 	int res = 0;
5803da28eb1STrond Myklebust 
5813da28eb1STrond Myklebust 	if (nfsi->ncommit != 0) {
5821da177e4SLinus Torvalds 		res = nfs_scan_list(&nfsi->commit, dst, idx_start, npages);
5831da177e4SLinus Torvalds 		nfsi->ncommit -= res;
5841da177e4SLinus Torvalds 		if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
5851da177e4SLinus Torvalds 			printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
5863da28eb1STrond Myklebust 	}
5871da177e4SLinus Torvalds 	return res;
5881da177e4SLinus Torvalds }
5891da177e4SLinus Torvalds #endif
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
5921da177e4SLinus Torvalds {
5931da177e4SLinus Torvalds 	struct backing_dev_info *bdi = mapping->backing_dev_info;
5941da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
5951da177e4SLinus Torvalds 	int ret = 0;
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	might_sleep();
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	if (!bdi_write_congested(bdi))
6001da177e4SLinus Torvalds 		return 0;
6011da177e4SLinus Torvalds 	if (intr) {
6021da177e4SLinus Torvalds 		struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
6031da177e4SLinus Torvalds 		sigset_t oldset;
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 		rpc_clnt_sigmask(clnt, &oldset);
6061da177e4SLinus Torvalds 		prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
6071da177e4SLinus Torvalds 		if (bdi_write_congested(bdi)) {
6081da177e4SLinus Torvalds 			if (signalled())
6091da177e4SLinus Torvalds 				ret = -ERESTARTSYS;
6101da177e4SLinus Torvalds 			else
6111da177e4SLinus Torvalds 				schedule();
6121da177e4SLinus Torvalds 		}
6131da177e4SLinus Torvalds 		rpc_clnt_sigunmask(clnt, &oldset);
6141da177e4SLinus Torvalds 	} else {
6151da177e4SLinus Torvalds 		prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
6161da177e4SLinus Torvalds 		if (bdi_write_congested(bdi))
6171da177e4SLinus Torvalds 			schedule();
6181da177e4SLinus Torvalds 	}
6191da177e4SLinus Torvalds 	finish_wait(&nfs_write_congestion, &wait);
6201da177e4SLinus Torvalds 	return ret;
6211da177e4SLinus Torvalds }
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds /*
6251da177e4SLinus Torvalds  * Try to update any existing write request, or create one if there is none.
6261da177e4SLinus Torvalds  * In order to match, the request's credentials must match those of
6271da177e4SLinus Torvalds  * the calling process.
6281da177e4SLinus Torvalds  *
6291da177e4SLinus Torvalds  * Note: Should always be called with the Page Lock held!
6301da177e4SLinus Torvalds  */
6311da177e4SLinus Torvalds static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
6321da177e4SLinus Torvalds 		struct inode *inode, struct page *page,
6331da177e4SLinus Torvalds 		unsigned int offset, unsigned int bytes)
6341da177e4SLinus Torvalds {
6351da177e4SLinus Torvalds 	struct nfs_server *server = NFS_SERVER(inode);
6361da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
6371da177e4SLinus Torvalds 	struct nfs_page		*req, *new = NULL;
6381da177e4SLinus Torvalds 	unsigned long		rqend, end;
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	end = offset + bytes;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR))
6431da177e4SLinus Torvalds 		return ERR_PTR(-ERESTARTSYS);
6441da177e4SLinus Torvalds 	for (;;) {
6451da177e4SLinus Torvalds 		/* Loop over all inode entries and see if we find
6461da177e4SLinus Torvalds 		 * A request for the page we wish to update
6471da177e4SLinus Torvalds 		 */
6481da177e4SLinus Torvalds 		spin_lock(&nfsi->req_lock);
6491da177e4SLinus Torvalds 		req = _nfs_find_request(inode, page->index);
6501da177e4SLinus Torvalds 		if (req) {
6511da177e4SLinus Torvalds 			if (!nfs_lock_request_dontget(req)) {
6521da177e4SLinus Torvalds 				int error;
6531da177e4SLinus Torvalds 				spin_unlock(&nfsi->req_lock);
6541da177e4SLinus Torvalds 				error = nfs_wait_on_request(req);
6551da177e4SLinus Torvalds 				nfs_release_request(req);
6561da177e4SLinus Torvalds 				if (error < 0)
6571da177e4SLinus Torvalds 					return ERR_PTR(error);
6581da177e4SLinus Torvalds 				continue;
6591da177e4SLinus Torvalds 			}
6601da177e4SLinus Torvalds 			spin_unlock(&nfsi->req_lock);
6611da177e4SLinus Torvalds 			if (new)
6621da177e4SLinus Torvalds 				nfs_release_request(new);
6631da177e4SLinus Torvalds 			break;
6641da177e4SLinus Torvalds 		}
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 		if (new) {
6671da177e4SLinus Torvalds 			int error;
6681da177e4SLinus Torvalds 			nfs_lock_request_dontget(new);
6691da177e4SLinus Torvalds 			error = nfs_inode_add_request(inode, new);
6701da177e4SLinus Torvalds 			if (error) {
6711da177e4SLinus Torvalds 				spin_unlock(&nfsi->req_lock);
6721da177e4SLinus Torvalds 				nfs_unlock_request(new);
6731da177e4SLinus Torvalds 				return ERR_PTR(error);
6741da177e4SLinus Torvalds 			}
6751da177e4SLinus Torvalds 			spin_unlock(&nfsi->req_lock);
6761da177e4SLinus Torvalds 			nfs_mark_request_dirty(new);
6771da177e4SLinus Torvalds 			return new;
6781da177e4SLinus Torvalds 		}
6791da177e4SLinus Torvalds 		spin_unlock(&nfsi->req_lock);
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 		new = nfs_create_request(ctx, inode, page, offset, bytes);
6821da177e4SLinus Torvalds 		if (IS_ERR(new))
6831da177e4SLinus Torvalds 			return new;
6841da177e4SLinus Torvalds 	}
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 	/* We have a request for our page.
6871da177e4SLinus Torvalds 	 * If the creds don't match, or the
6881da177e4SLinus Torvalds 	 * page addresses don't match,
6891da177e4SLinus Torvalds 	 * tell the caller to wait on the conflicting
6901da177e4SLinus Torvalds 	 * request.
6911da177e4SLinus Torvalds 	 */
6921da177e4SLinus Torvalds 	rqend = req->wb_offset + req->wb_bytes;
6931da177e4SLinus Torvalds 	if (req->wb_context != ctx
6941da177e4SLinus Torvalds 	    || req->wb_page != page
6951da177e4SLinus Torvalds 	    || !nfs_dirty_request(req)
6961da177e4SLinus Torvalds 	    || offset > rqend || end < req->wb_offset) {
6971da177e4SLinus Torvalds 		nfs_unlock_request(req);
6981da177e4SLinus Torvalds 		return ERR_PTR(-EBUSY);
6991da177e4SLinus Torvalds 	}
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	/* Okay, the request matches. Update the region */
7021da177e4SLinus Torvalds 	if (offset < req->wb_offset) {
7031da177e4SLinus Torvalds 		req->wb_offset = offset;
7041da177e4SLinus Torvalds 		req->wb_pgbase = offset;
7051da177e4SLinus Torvalds 		req->wb_bytes = rqend - req->wb_offset;
7061da177e4SLinus Torvalds 	}
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 	if (end > rqend)
7091da177e4SLinus Torvalds 		req->wb_bytes = end - req->wb_offset;
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 	return req;
7121da177e4SLinus Torvalds }
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page)
7151da177e4SLinus Torvalds {
7161da177e4SLinus Torvalds 	struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
7171da177e4SLinus Torvalds 	struct inode	*inode = page->mapping->host;
7181da177e4SLinus Torvalds 	struct nfs_page	*req;
7191da177e4SLinus Torvalds 	int		status = 0;
7201da177e4SLinus Torvalds 	/*
7211da177e4SLinus Torvalds 	 * Look for a request corresponding to this page. If there
7221da177e4SLinus Torvalds 	 * is one, and it belongs to another file, we flush it out
7231da177e4SLinus Torvalds 	 * before we try to copy anything into the page. Do this
7241da177e4SLinus Torvalds 	 * due to the lack of an ACCESS-type call in NFSv2.
7251da177e4SLinus Torvalds 	 * Also do the same if we find a request from an existing
7261da177e4SLinus Torvalds 	 * dropped page.
7271da177e4SLinus Torvalds 	 */
7281da177e4SLinus Torvalds 	req = nfs_find_request(inode, page->index);
7291da177e4SLinus Torvalds 	if (req) {
7301da177e4SLinus Torvalds 		if (req->wb_page != page || ctx != req->wb_context)
7311da177e4SLinus Torvalds 			status = nfs_wb_page(inode, page);
7321da177e4SLinus Torvalds 		nfs_release_request(req);
7331da177e4SLinus Torvalds 	}
7341da177e4SLinus Torvalds 	return (status < 0) ? status : 0;
7351da177e4SLinus Torvalds }
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds /*
7381da177e4SLinus Torvalds  * Update and possibly write a cached page of an NFS file.
7391da177e4SLinus Torvalds  *
7401da177e4SLinus Torvalds  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
7411da177e4SLinus Torvalds  * things with a page scheduled for an RPC call (e.g. invalidate it).
7421da177e4SLinus Torvalds  */
7431da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page,
7441da177e4SLinus Torvalds 		unsigned int offset, unsigned int count)
7451da177e4SLinus Torvalds {
7461da177e4SLinus Torvalds 	struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
7471da177e4SLinus Torvalds 	struct inode	*inode = page->mapping->host;
7481da177e4SLinus Torvalds 	struct nfs_page	*req;
7491da177e4SLinus Torvalds 	int		status = 0;
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 	dprintk("NFS:      nfs_updatepage(%s/%s %d@%Ld)\n",
7520bbacc40SChuck Lever 		file->f_dentry->d_parent->d_name.name,
7530bbacc40SChuck Lever 		file->f_dentry->d_name.name, count,
7540bbacc40SChuck Lever 		(long long)(page_offset(page) +offset));
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 	if (IS_SYNC(inode)) {
7571da177e4SLinus Torvalds 		status = nfs_writepage_sync(ctx, inode, page, offset, count, 0);
7581da177e4SLinus Torvalds 		if (status > 0) {
7591da177e4SLinus Torvalds 			if (offset == 0 && status == PAGE_CACHE_SIZE)
7601da177e4SLinus Torvalds 				SetPageUptodate(page);
7611da177e4SLinus Torvalds 			return 0;
7621da177e4SLinus Torvalds 		}
7631da177e4SLinus Torvalds 		return status;
7641da177e4SLinus Torvalds 	}
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 	/* If we're not using byte range locks, and we know the page
7671da177e4SLinus Torvalds 	 * is entirely in cache, it may be more efficient to avoid
7681da177e4SLinus Torvalds 	 * fragmenting write requests.
7691da177e4SLinus Torvalds 	 */
770ab0a3dbeSTrond Myklebust 	if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
7711da177e4SLinus Torvalds 		loff_t end_offs = i_size_read(inode) - 1;
7721da177e4SLinus Torvalds 		unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT;
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 		count += offset;
7751da177e4SLinus Torvalds 		offset = 0;
7761da177e4SLinus Torvalds 		if (unlikely(end_offs < 0)) {
7771da177e4SLinus Torvalds 			/* Do nothing */
7781da177e4SLinus Torvalds 		} else if (page->index == end_index) {
7791da177e4SLinus Torvalds 			unsigned int pglen;
7801da177e4SLinus Torvalds 			pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1;
7811da177e4SLinus Torvalds 			if (count < pglen)
7821da177e4SLinus Torvalds 				count = pglen;
7831da177e4SLinus Torvalds 		} else if (page->index < end_index)
7841da177e4SLinus Torvalds 			count = PAGE_CACHE_SIZE;
7851da177e4SLinus Torvalds 	}
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	/*
7881da177e4SLinus Torvalds 	 * Try to find an NFS request corresponding to this page
7891da177e4SLinus Torvalds 	 * and update it.
7901da177e4SLinus Torvalds 	 * If the existing request cannot be updated, we must flush
7911da177e4SLinus Torvalds 	 * it out now.
7921da177e4SLinus Torvalds 	 */
7931da177e4SLinus Torvalds 	do {
7941da177e4SLinus Torvalds 		req = nfs_update_request(ctx, inode, page, offset, count);
7951da177e4SLinus Torvalds 		status = (IS_ERR(req)) ? PTR_ERR(req) : 0;
7961da177e4SLinus Torvalds 		if (status != -EBUSY)
7971da177e4SLinus Torvalds 			break;
7981da177e4SLinus Torvalds 		/* Request could not be updated. Flush it out and try again */
7991da177e4SLinus Torvalds 		status = nfs_wb_page(inode, page);
8001da177e4SLinus Torvalds 	} while (status >= 0);
8011da177e4SLinus Torvalds 	if (status < 0)
8021da177e4SLinus Torvalds 		goto done;
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	status = 0;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 	/* Update file length */
8071da177e4SLinus Torvalds 	nfs_grow_file(page, offset, count);
8081da177e4SLinus Torvalds 	/* Set the PG_uptodate flag? */
8091da177e4SLinus Torvalds 	nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
8101da177e4SLinus Torvalds 	nfs_unlock_request(req);
8111da177e4SLinus Torvalds done:
8121da177e4SLinus Torvalds         dprintk("NFS:      nfs_updatepage returns %d (isize %Ld)\n",
8131da177e4SLinus Torvalds 			status, (long long)i_size_read(inode));
8141da177e4SLinus Torvalds 	if (status < 0)
8151da177e4SLinus Torvalds 		ClearPageUptodate(page);
8161da177e4SLinus Torvalds 	return status;
8171da177e4SLinus Torvalds }
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds static void nfs_writepage_release(struct nfs_page *req)
8201da177e4SLinus Torvalds {
8211da177e4SLinus Torvalds 	end_page_writeback(req->wb_page);
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
8241da177e4SLinus Torvalds 	if (!PageError(req->wb_page)) {
8251da177e4SLinus Torvalds 		if (NFS_NEED_RESCHED(req)) {
8261da177e4SLinus Torvalds 			nfs_mark_request_dirty(req);
8271da177e4SLinus Torvalds 			goto out;
8281da177e4SLinus Torvalds 		} else if (NFS_NEED_COMMIT(req)) {
8291da177e4SLinus Torvalds 			nfs_mark_request_commit(req);
8301da177e4SLinus Torvalds 			goto out;
8311da177e4SLinus Torvalds 		}
8321da177e4SLinus Torvalds 	}
8331da177e4SLinus Torvalds 	nfs_inode_remove_request(req);
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds out:
8361da177e4SLinus Torvalds 	nfs_clear_commit(req);
8371da177e4SLinus Torvalds 	nfs_clear_reschedule(req);
8381da177e4SLinus Torvalds #else
8391da177e4SLinus Torvalds 	nfs_inode_remove_request(req);
8401da177e4SLinus Torvalds #endif
841c6a556b8STrond Myklebust 	nfs_clear_page_writeback(req);
8421da177e4SLinus Torvalds }
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds static inline int flush_task_priority(int how)
8451da177e4SLinus Torvalds {
8461da177e4SLinus Torvalds 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
8471da177e4SLinus Torvalds 		case FLUSH_HIGHPRI:
8481da177e4SLinus Torvalds 			return RPC_PRIORITY_HIGH;
8491da177e4SLinus Torvalds 		case FLUSH_LOWPRI:
8501da177e4SLinus Torvalds 			return RPC_PRIORITY_LOW;
8511da177e4SLinus Torvalds 	}
8521da177e4SLinus Torvalds 	return RPC_PRIORITY_NORMAL;
8531da177e4SLinus Torvalds }
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds /*
8561da177e4SLinus Torvalds  * Set up the argument/result storage required for the RPC call.
8571da177e4SLinus Torvalds  */
8581da177e4SLinus Torvalds static void nfs_write_rpcsetup(struct nfs_page *req,
8591da177e4SLinus Torvalds 		struct nfs_write_data *data,
8601da177e4SLinus Torvalds 		unsigned int count, unsigned int offset,
8611da177e4SLinus Torvalds 		int how)
8621da177e4SLinus Torvalds {
8631da177e4SLinus Torvalds 	struct inode		*inode;
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
8661da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	data->req = req;
8691da177e4SLinus Torvalds 	data->inode = inode = req->wb_context->dentry->d_inode;
8701da177e4SLinus Torvalds 	data->cred = req->wb_context->cred;
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(inode);
8731da177e4SLinus Torvalds 	data->args.offset = req_offset(req) + offset;
8741da177e4SLinus Torvalds 	data->args.pgbase = req->wb_pgbase + offset;
8751da177e4SLinus Torvalds 	data->args.pages  = data->pagevec;
8761da177e4SLinus Torvalds 	data->args.count  = count;
8771da177e4SLinus Torvalds 	data->args.context = req->wb_context;
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
8801da177e4SLinus Torvalds 	data->res.count   = count;
8811da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
8820e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	NFS_PROTO(inode)->write_setup(data, how);
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	data->task.tk_priority = flush_task_priority(how);
8871da177e4SLinus Torvalds 	data->task.tk_cookie = (unsigned long)inode;
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
8900bbacc40SChuck Lever 		data->task.tk_pid,
8911da177e4SLinus Torvalds 		inode->i_sb->s_id,
8921da177e4SLinus Torvalds 		(long long)NFS_FILEID(inode),
8931da177e4SLinus Torvalds 		count,
8941da177e4SLinus Torvalds 		(unsigned long long)data->args.offset);
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds static void nfs_execute_write(struct nfs_write_data *data)
8981da177e4SLinus Torvalds {
8991da177e4SLinus Torvalds 	struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
9001da177e4SLinus Torvalds 	sigset_t oldset;
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	rpc_clnt_sigmask(clnt, &oldset);
9031da177e4SLinus Torvalds 	lock_kernel();
9041da177e4SLinus Torvalds 	rpc_execute(&data->task);
9051da177e4SLinus Torvalds 	unlock_kernel();
9061da177e4SLinus Torvalds 	rpc_clnt_sigunmask(clnt, &oldset);
9071da177e4SLinus Torvalds }
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds /*
9101da177e4SLinus Torvalds  * Generate multiple small requests to write out a single
9111da177e4SLinus Torvalds  * contiguous dirty area on one page.
9121da177e4SLinus Torvalds  */
9131da177e4SLinus Torvalds static int nfs_flush_multi(struct list_head *head, struct inode *inode, int how)
9141da177e4SLinus Torvalds {
9151da177e4SLinus Torvalds 	struct nfs_page *req = nfs_list_entry(head->next);
9161da177e4SLinus Torvalds 	struct page *page = req->wb_page;
9171da177e4SLinus Torvalds 	struct nfs_write_data *data;
9181da177e4SLinus Torvalds 	unsigned int wsize = NFS_SERVER(inode)->wsize;
9191da177e4SLinus Torvalds 	unsigned int nbytes, offset;
9201da177e4SLinus Torvalds 	int requests = 0;
9211da177e4SLinus Torvalds 	LIST_HEAD(list);
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds 	nfs_list_remove_request(req);
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	nbytes = req->wb_bytes;
9261da177e4SLinus Torvalds 	for (;;) {
92740859d7eSChuck Lever 		data = nfs_writedata_alloc(1);
9281da177e4SLinus Torvalds 		if (!data)
9291da177e4SLinus Torvalds 			goto out_bad;
9301da177e4SLinus Torvalds 		list_add(&data->pages, &list);
9311da177e4SLinus Torvalds 		requests++;
9321da177e4SLinus Torvalds 		if (nbytes <= wsize)
9331da177e4SLinus Torvalds 			break;
9341da177e4SLinus Torvalds 		nbytes -= wsize;
9351da177e4SLinus Torvalds 	}
9361da177e4SLinus Torvalds 	atomic_set(&req->wb_complete, requests);
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds 	ClearPageError(page);
939bb713d6dSTrond Myklebust 	set_page_writeback(page);
9401da177e4SLinus Torvalds 	offset = 0;
9411da177e4SLinus Torvalds 	nbytes = req->wb_bytes;
9421da177e4SLinus Torvalds 	do {
9431da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
9441da177e4SLinus Torvalds 		list_del_init(&data->pages);
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 		data->pagevec[0] = page;
9471da177e4SLinus Torvalds 		data->complete = nfs_writeback_done_partial;
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 		if (nbytes > wsize) {
9501da177e4SLinus Torvalds 			nfs_write_rpcsetup(req, data, wsize, offset, how);
9511da177e4SLinus Torvalds 			offset += wsize;
9521da177e4SLinus Torvalds 			nbytes -= wsize;
9531da177e4SLinus Torvalds 		} else {
9541da177e4SLinus Torvalds 			nfs_write_rpcsetup(req, data, nbytes, offset, how);
9551da177e4SLinus Torvalds 			nbytes = 0;
9561da177e4SLinus Torvalds 		}
9571da177e4SLinus Torvalds 		nfs_execute_write(data);
9581da177e4SLinus Torvalds 	} while (nbytes != 0);
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	return 0;
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds out_bad:
9631da177e4SLinus Torvalds 	while (!list_empty(&list)) {
9641da177e4SLinus Torvalds 		data = list_entry(list.next, struct nfs_write_data, pages);
9651da177e4SLinus Torvalds 		list_del(&data->pages);
9661da177e4SLinus Torvalds 		nfs_writedata_free(data);
9671da177e4SLinus Torvalds 	}
9681da177e4SLinus Torvalds 	nfs_mark_request_dirty(req);
969c6a556b8STrond Myklebust 	nfs_clear_page_writeback(req);
9701da177e4SLinus Torvalds 	return -ENOMEM;
9711da177e4SLinus Torvalds }
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds /*
9741da177e4SLinus Torvalds  * Create an RPC task for the given write request and kick it.
9751da177e4SLinus Torvalds  * The page must have been locked by the caller.
9761da177e4SLinus Torvalds  *
9771da177e4SLinus Torvalds  * It may happen that the page we're passed is not marked dirty.
9781da177e4SLinus Torvalds  * This is the case if nfs_updatepage detects a conflicting request
9791da177e4SLinus Torvalds  * that has been written but not committed.
9801da177e4SLinus Torvalds  */
9811da177e4SLinus Torvalds static int nfs_flush_one(struct list_head *head, struct inode *inode, int how)
9821da177e4SLinus Torvalds {
9831da177e4SLinus Torvalds 	struct nfs_page		*req;
9841da177e4SLinus Torvalds 	struct page		**pages;
9851da177e4SLinus Torvalds 	struct nfs_write_data	*data;
9861da177e4SLinus Torvalds 	unsigned int		count;
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 	if (NFS_SERVER(inode)->wsize < PAGE_CACHE_SIZE)
9891da177e4SLinus Torvalds 		return nfs_flush_multi(head, inode, how);
9901da177e4SLinus Torvalds 
99140859d7eSChuck Lever 	data = nfs_writedata_alloc(NFS_SERVER(inode)->wpages);
9921da177e4SLinus Torvalds 	if (!data)
9931da177e4SLinus Torvalds 		goto out_bad;
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	pages = data->pagevec;
9961da177e4SLinus Torvalds 	count = 0;
9971da177e4SLinus Torvalds 	while (!list_empty(head)) {
9981da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
9991da177e4SLinus Torvalds 		nfs_list_remove_request(req);
10001da177e4SLinus Torvalds 		nfs_list_add_request(req, &data->pages);
10011da177e4SLinus Torvalds 		ClearPageError(req->wb_page);
1002bb713d6dSTrond Myklebust 		set_page_writeback(req->wb_page);
10031da177e4SLinus Torvalds 		*pages++ = req->wb_page;
10041da177e4SLinus Torvalds 		count += req->wb_bytes;
10051da177e4SLinus Torvalds 	}
10061da177e4SLinus Torvalds 	req = nfs_list_entry(data->pages.next);
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 	data->complete = nfs_writeback_done_full;
10091da177e4SLinus Torvalds 	/* Set up the argument struct */
10101da177e4SLinus Torvalds 	nfs_write_rpcsetup(req, data, count, 0, how);
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds 	nfs_execute_write(data);
10131da177e4SLinus Torvalds 	return 0;
10141da177e4SLinus Torvalds  out_bad:
10151da177e4SLinus Torvalds 	while (!list_empty(head)) {
10161da177e4SLinus Torvalds 		struct nfs_page *req = nfs_list_entry(head->next);
10171da177e4SLinus Torvalds 		nfs_list_remove_request(req);
10181da177e4SLinus Torvalds 		nfs_mark_request_dirty(req);
1019c6a556b8STrond Myklebust 		nfs_clear_page_writeback(req);
10201da177e4SLinus Torvalds 	}
10211da177e4SLinus Torvalds 	return -ENOMEM;
10221da177e4SLinus Torvalds }
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds static int
10251da177e4SLinus Torvalds nfs_flush_list(struct list_head *head, int wpages, int how)
10261da177e4SLinus Torvalds {
10271da177e4SLinus Torvalds 	LIST_HEAD(one_request);
10281da177e4SLinus Torvalds 	struct nfs_page		*req;
10291da177e4SLinus Torvalds 	int			error = 0;
10301da177e4SLinus Torvalds 	unsigned int		pages = 0;
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 	while (!list_empty(head)) {
10331da177e4SLinus Torvalds 		pages += nfs_coalesce_requests(head, &one_request, wpages);
10341da177e4SLinus Torvalds 		req = nfs_list_entry(one_request.next);
10351da177e4SLinus Torvalds 		error = nfs_flush_one(&one_request, req->wb_context->dentry->d_inode, how);
10361da177e4SLinus Torvalds 		if (error < 0)
10371da177e4SLinus Torvalds 			break;
10381da177e4SLinus Torvalds 	}
10391da177e4SLinus Torvalds 	if (error >= 0)
10401da177e4SLinus Torvalds 		return pages;
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 	while (!list_empty(head)) {
10431da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
10441da177e4SLinus Torvalds 		nfs_list_remove_request(req);
10451da177e4SLinus Torvalds 		nfs_mark_request_dirty(req);
1046c6a556b8STrond Myklebust 		nfs_clear_page_writeback(req);
10471da177e4SLinus Torvalds 	}
10481da177e4SLinus Torvalds 	return error;
10491da177e4SLinus Torvalds }
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds /*
10521da177e4SLinus Torvalds  * Handle a write reply that flushed part of a page.
10531da177e4SLinus Torvalds  */
10541da177e4SLinus Torvalds static void nfs_writeback_done_partial(struct nfs_write_data *data, int status)
10551da177e4SLinus Torvalds {
10561da177e4SLinus Torvalds 	struct nfs_page		*req = data->req;
10571da177e4SLinus Torvalds 	struct page		*page = req->wb_page;
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 	dprintk("NFS: write (%s/%Ld %d@%Ld)",
10601da177e4SLinus Torvalds 		req->wb_context->dentry->d_inode->i_sb->s_id,
10611da177e4SLinus Torvalds 		(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
10621da177e4SLinus Torvalds 		req->wb_bytes,
10631da177e4SLinus Torvalds 		(long long)req_offset(req));
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 	if (status < 0) {
10661da177e4SLinus Torvalds 		ClearPageUptodate(page);
10671da177e4SLinus Torvalds 		SetPageError(page);
10681da177e4SLinus Torvalds 		req->wb_context->error = status;
10691da177e4SLinus Torvalds 		dprintk(", error = %d\n", status);
10701da177e4SLinus Torvalds 	} else {
10711da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
10721da177e4SLinus Torvalds 		if (data->verf.committed < NFS_FILE_SYNC) {
10731da177e4SLinus Torvalds 			if (!NFS_NEED_COMMIT(req)) {
10741da177e4SLinus Torvalds 				nfs_defer_commit(req);
10751da177e4SLinus Torvalds 				memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
10761da177e4SLinus Torvalds 				dprintk(" defer commit\n");
10771da177e4SLinus Torvalds 			} else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
10781da177e4SLinus Torvalds 				nfs_defer_reschedule(req);
10791da177e4SLinus Torvalds 				dprintk(" server reboot detected\n");
10801da177e4SLinus Torvalds 			}
10811da177e4SLinus Torvalds 		} else
10821da177e4SLinus Torvalds #endif
10831da177e4SLinus Torvalds 			dprintk(" OK\n");
10841da177e4SLinus Torvalds 	}
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 	if (atomic_dec_and_test(&req->wb_complete))
10871da177e4SLinus Torvalds 		nfs_writepage_release(req);
10881da177e4SLinus Torvalds }
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds /*
10911da177e4SLinus Torvalds  * Handle a write reply that flushes a whole page.
10921da177e4SLinus Torvalds  *
10931da177e4SLinus Torvalds  * FIXME: There is an inherent race with invalidate_inode_pages and
10941da177e4SLinus Torvalds  *	  writebacks since the page->count is kept > 1 for as long
10951da177e4SLinus Torvalds  *	  as the page has a write request pending.
10961da177e4SLinus Torvalds  */
10971da177e4SLinus Torvalds static void nfs_writeback_done_full(struct nfs_write_data *data, int status)
10981da177e4SLinus Torvalds {
10991da177e4SLinus Torvalds 	struct nfs_page		*req;
11001da177e4SLinus Torvalds 	struct page		*page;
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds 	/* Update attributes as result of writeback. */
11031da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
11041da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
11051da177e4SLinus Torvalds 		nfs_list_remove_request(req);
11061da177e4SLinus Torvalds 		page = req->wb_page;
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds 		dprintk("NFS: write (%s/%Ld %d@%Ld)",
11091da177e4SLinus Torvalds 			req->wb_context->dentry->d_inode->i_sb->s_id,
11101da177e4SLinus Torvalds 			(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
11111da177e4SLinus Torvalds 			req->wb_bytes,
11121da177e4SLinus Torvalds 			(long long)req_offset(req));
11131da177e4SLinus Torvalds 
11141da177e4SLinus Torvalds 		if (status < 0) {
11151da177e4SLinus Torvalds 			ClearPageUptodate(page);
11161da177e4SLinus Torvalds 			SetPageError(page);
11171da177e4SLinus Torvalds 			req->wb_context->error = status;
11181da177e4SLinus Torvalds 			end_page_writeback(page);
11191da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
11201da177e4SLinus Torvalds 			dprintk(", error = %d\n", status);
11211da177e4SLinus Torvalds 			goto next;
11221da177e4SLinus Torvalds 		}
11231da177e4SLinus Torvalds 		end_page_writeback(page);
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
11261da177e4SLinus Torvalds 		if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
11271da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
11281da177e4SLinus Torvalds 			dprintk(" OK\n");
11291da177e4SLinus Torvalds 			goto next;
11301da177e4SLinus Torvalds 		}
11311da177e4SLinus Torvalds 		memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
11321da177e4SLinus Torvalds 		nfs_mark_request_commit(req);
11331da177e4SLinus Torvalds 		dprintk(" marked for commit\n");
11341da177e4SLinus Torvalds #else
11351da177e4SLinus Torvalds 		nfs_inode_remove_request(req);
11361da177e4SLinus Torvalds #endif
11371da177e4SLinus Torvalds 	next:
1138c6a556b8STrond Myklebust 		nfs_clear_page_writeback(req);
11391da177e4SLinus Torvalds 	}
11401da177e4SLinus Torvalds }
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds /*
11431da177e4SLinus Torvalds  * This function is called when the WRITE call is complete.
11441da177e4SLinus Torvalds  */
1145963d8fe5STrond Myklebust void nfs_writeback_done(struct rpc_task *task, void *calldata)
11461da177e4SLinus Torvalds {
1147963d8fe5STrond Myklebust 	struct nfs_write_data	*data = calldata;
11481da177e4SLinus Torvalds 	struct nfs_writeargs	*argp = &data->args;
11491da177e4SLinus Torvalds 	struct nfs_writeres	*resp = &data->res;
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 	dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
11521da177e4SLinus Torvalds 		task->tk_pid, task->tk_status);
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
11551da177e4SLinus Torvalds 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
11561da177e4SLinus Torvalds 		/* We tried a write call, but the server did not
11571da177e4SLinus Torvalds 		 * commit data to stable storage even though we
11581da177e4SLinus Torvalds 		 * requested it.
11591da177e4SLinus Torvalds 		 * Note: There is a known bug in Tru64 < 5.0 in which
11601da177e4SLinus Torvalds 		 *	 the server reports NFS_DATA_SYNC, but performs
11611da177e4SLinus Torvalds 		 *	 NFS_FILE_SYNC. We therefore implement this checking
11621da177e4SLinus Torvalds 		 *	 as a dprintk() in order to avoid filling syslog.
11631da177e4SLinus Torvalds 		 */
11641da177e4SLinus Torvalds 		static unsigned long    complain;
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
11671da177e4SLinus Torvalds 			dprintk("NFS: faulty NFS server %s:"
11681da177e4SLinus Torvalds 				" (committed = %d) != (stable = %d)\n",
11691da177e4SLinus Torvalds 				NFS_SERVER(data->inode)->hostname,
11701da177e4SLinus Torvalds 				resp->verf->committed, argp->stable);
11711da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
11721da177e4SLinus Torvalds 		}
11731da177e4SLinus Torvalds 	}
11741da177e4SLinus Torvalds #endif
11751da177e4SLinus Torvalds 	/* Is this a short write? */
11761da177e4SLinus Torvalds 	if (task->tk_status >= 0 && resp->count < argp->count) {
11771da177e4SLinus Torvalds 		static unsigned long    complain;
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds 		/* Has the server at least made some progress? */
11801da177e4SLinus Torvalds 		if (resp->count != 0) {
11811da177e4SLinus Torvalds 			/* Was this an NFSv2 write or an NFSv3 stable write? */
11821da177e4SLinus Torvalds 			if (resp->verf->committed != NFS_UNSTABLE) {
11831da177e4SLinus Torvalds 				/* Resend from where the server left off */
11841da177e4SLinus Torvalds 				argp->offset += resp->count;
11851da177e4SLinus Torvalds 				argp->pgbase += resp->count;
11861da177e4SLinus Torvalds 				argp->count -= resp->count;
11871da177e4SLinus Torvalds 			} else {
11881da177e4SLinus Torvalds 				/* Resend as a stable write in order to avoid
11891da177e4SLinus Torvalds 				 * headaches in the case of a server crash.
11901da177e4SLinus Torvalds 				 */
11911da177e4SLinus Torvalds 				argp->stable = NFS_FILE_SYNC;
11921da177e4SLinus Torvalds 			}
11931da177e4SLinus Torvalds 			rpc_restart_call(task);
11941da177e4SLinus Torvalds 			return;
11951da177e4SLinus Torvalds 		}
11961da177e4SLinus Torvalds 		if (time_before(complain, jiffies)) {
11971da177e4SLinus Torvalds 			printk(KERN_WARNING
11981da177e4SLinus Torvalds 			       "NFS: Server wrote zero bytes, expected %u.\n",
11991da177e4SLinus Torvalds 					argp->count);
12001da177e4SLinus Torvalds 			complain = jiffies + 300 * HZ;
12011da177e4SLinus Torvalds 		}
12021da177e4SLinus Torvalds 		/* Can't do anything about it except throw an error. */
12031da177e4SLinus Torvalds 		task->tk_status = -EIO;
12041da177e4SLinus Torvalds 	}
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds 	/*
12071da177e4SLinus Torvalds 	 * Process the nfs_page list
12081da177e4SLinus Torvalds 	 */
12091da177e4SLinus Torvalds 	data->complete(data, task->tk_status);
12101da177e4SLinus Torvalds }
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
1214963d8fe5STrond Myklebust void nfs_commit_release(void *wdata)
12151da177e4SLinus Torvalds {
12161da177e4SLinus Torvalds 	nfs_commit_free(wdata);
12171da177e4SLinus Torvalds }
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds /*
12201da177e4SLinus Torvalds  * Set up the argument/result storage required for the RPC call.
12211da177e4SLinus Torvalds  */
12221da177e4SLinus Torvalds static void nfs_commit_rpcsetup(struct list_head *head,
12231da177e4SLinus Torvalds 		struct nfs_write_data *data, int how)
12241da177e4SLinus Torvalds {
12253da28eb1STrond Myklebust 	struct nfs_page		*first;
12261da177e4SLinus Torvalds 	struct inode		*inode;
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 	/* Set up the RPC argument and reply structs
12291da177e4SLinus Torvalds 	 * NB: take care not to mess about with data->commit et al. */
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 	list_splice_init(head, &data->pages);
12321da177e4SLinus Torvalds 	first = nfs_list_entry(data->pages.next);
12331da177e4SLinus Torvalds 	inode = first->wb_context->dentry->d_inode;
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds 	data->inode	  = inode;
12361da177e4SLinus Torvalds 	data->cred	  = first->wb_context->cred;
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds 	data->args.fh     = NFS_FH(data->inode);
12393da28eb1STrond Myklebust 	/* Note: we always request a commit of the entire inode */
12403da28eb1STrond Myklebust 	data->args.offset = 0;
12413da28eb1STrond Myklebust 	data->args.count  = 0;
12423da28eb1STrond Myklebust 	data->res.count   = 0;
12431da177e4SLinus Torvalds 	data->res.fattr   = &data->fattr;
12441da177e4SLinus Torvalds 	data->res.verf    = &data->verf;
12450e574af1STrond Myklebust 	nfs_fattr_init(&data->fattr);
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds 	NFS_PROTO(inode)->commit_setup(data, how);
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 	data->task.tk_priority = flush_task_priority(how);
12501da177e4SLinus Torvalds 	data->task.tk_cookie = (unsigned long)inode;
12511da177e4SLinus Torvalds 
12520bbacc40SChuck Lever 	dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
12531da177e4SLinus Torvalds }
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds /*
12561da177e4SLinus Torvalds  * Commit dirty pages
12571da177e4SLinus Torvalds  */
12581da177e4SLinus Torvalds static int
125940859d7eSChuck Lever nfs_commit_list(struct inode *inode, struct list_head *head, int how)
12601da177e4SLinus Torvalds {
12611da177e4SLinus Torvalds 	struct nfs_write_data	*data;
12621da177e4SLinus Torvalds 	struct nfs_page         *req;
12631da177e4SLinus Torvalds 
126440859d7eSChuck Lever 	data = nfs_commit_alloc(NFS_SERVER(inode)->wpages);
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 	if (!data)
12671da177e4SLinus Torvalds 		goto out_bad;
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds 	/* Set up the argument struct */
12701da177e4SLinus Torvalds 	nfs_commit_rpcsetup(head, data, how);
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 	nfs_execute_write(data);
12731da177e4SLinus Torvalds 	return 0;
12741da177e4SLinus Torvalds  out_bad:
12751da177e4SLinus Torvalds 	while (!list_empty(head)) {
12761da177e4SLinus Torvalds 		req = nfs_list_entry(head->next);
12771da177e4SLinus Torvalds 		nfs_list_remove_request(req);
12781da177e4SLinus Torvalds 		nfs_mark_request_commit(req);
1279c6a556b8STrond Myklebust 		nfs_clear_page_writeback(req);
12801da177e4SLinus Torvalds 	}
12811da177e4SLinus Torvalds 	return -ENOMEM;
12821da177e4SLinus Torvalds }
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds /*
12851da177e4SLinus Torvalds  * COMMIT call returned
12861da177e4SLinus Torvalds  */
1287963d8fe5STrond Myklebust void nfs_commit_done(struct rpc_task *task, void *calldata)
12881da177e4SLinus Torvalds {
1289963d8fe5STrond Myklebust 	struct nfs_write_data	*data = calldata;
12901da177e4SLinus Torvalds 	struct nfs_page		*req;
12911da177e4SLinus Torvalds 	int res = 0;
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds         dprintk("NFS: %4d nfs_commit_done (status %d)\n",
12941da177e4SLinus Torvalds                                 task->tk_pid, task->tk_status);
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 	while (!list_empty(&data->pages)) {
12971da177e4SLinus Torvalds 		req = nfs_list_entry(data->pages.next);
12981da177e4SLinus Torvalds 		nfs_list_remove_request(req);
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds 		dprintk("NFS: commit (%s/%Ld %d@%Ld)",
13011da177e4SLinus Torvalds 			req->wb_context->dentry->d_inode->i_sb->s_id,
13021da177e4SLinus Torvalds 			(long long)NFS_FILEID(req->wb_context->dentry->d_inode),
13031da177e4SLinus Torvalds 			req->wb_bytes,
13041da177e4SLinus Torvalds 			(long long)req_offset(req));
13051da177e4SLinus Torvalds 		if (task->tk_status < 0) {
13061da177e4SLinus Torvalds 			req->wb_context->error = task->tk_status;
13071da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
13081da177e4SLinus Torvalds 			dprintk(", error = %d\n", task->tk_status);
13091da177e4SLinus Torvalds 			goto next;
13101da177e4SLinus Torvalds 		}
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds 		/* Okay, COMMIT succeeded, apparently. Check the verifier
13131da177e4SLinus Torvalds 		 * returned by the server against all stored verfs. */
13141da177e4SLinus Torvalds 		if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
13151da177e4SLinus Torvalds 			/* We have a match */
13161da177e4SLinus Torvalds 			nfs_inode_remove_request(req);
13171da177e4SLinus Torvalds 			dprintk(" OK\n");
13181da177e4SLinus Torvalds 			goto next;
13191da177e4SLinus Torvalds 		}
13201da177e4SLinus Torvalds 		/* We have a mismatch. Write the page again */
13211da177e4SLinus Torvalds 		dprintk(" mismatch\n");
13221da177e4SLinus Torvalds 		nfs_mark_request_dirty(req);
13231da177e4SLinus Torvalds 	next:
1324c6a556b8STrond Myklebust 		nfs_clear_page_writeback(req);
13251da177e4SLinus Torvalds 		res++;
13261da177e4SLinus Torvalds 	}
13271da177e4SLinus Torvalds 	sub_page_state(nr_unstable,res);
13281da177e4SLinus Torvalds }
13291da177e4SLinus Torvalds #endif
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds static int nfs_flush_inode(struct inode *inode, unsigned long idx_start,
13321da177e4SLinus Torvalds 			   unsigned int npages, int how)
13331da177e4SLinus Torvalds {
13341da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
13351da177e4SLinus Torvalds 	LIST_HEAD(head);
13361da177e4SLinus Torvalds 	int			res,
13371da177e4SLinus Torvalds 				error = 0;
13381da177e4SLinus Torvalds 
13391da177e4SLinus Torvalds 	spin_lock(&nfsi->req_lock);
13401da177e4SLinus Torvalds 	res = nfs_scan_dirty(inode, &head, idx_start, npages);
13411da177e4SLinus Torvalds 	spin_unlock(&nfsi->req_lock);
1342ab0a3dbeSTrond Myklebust 	if (res) {
1343ab0a3dbeSTrond Myklebust 		struct nfs_server *server = NFS_SERVER(inode);
1344ab0a3dbeSTrond Myklebust 
1345ab0a3dbeSTrond Myklebust 		/* For single writes, FLUSH_STABLE is more efficient */
1346ab0a3dbeSTrond Myklebust 		if (res == nfsi->npages && nfsi->npages <= server->wpages) {
1347ab0a3dbeSTrond Myklebust 			if (res > 1 || nfs_list_entry(head.next)->wb_bytes <= server->wsize)
1348ab0a3dbeSTrond Myklebust 				how |= FLUSH_STABLE;
1349ab0a3dbeSTrond Myklebust 		}
1350ab0a3dbeSTrond Myklebust 		error = nfs_flush_list(&head, server->wpages, how);
1351ab0a3dbeSTrond Myklebust 	}
13521da177e4SLinus Torvalds 	if (error < 0)
13531da177e4SLinus Torvalds 		return error;
13541da177e4SLinus Torvalds 	return res;
13551da177e4SLinus Torvalds }
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
13583da28eb1STrond Myklebust int nfs_commit_inode(struct inode *inode, int how)
13591da177e4SLinus Torvalds {
13601da177e4SLinus Torvalds 	struct nfs_inode *nfsi = NFS_I(inode);
13611da177e4SLinus Torvalds 	LIST_HEAD(head);
13621da177e4SLinus Torvalds 	int			res,
13631da177e4SLinus Torvalds 				error = 0;
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds 	spin_lock(&nfsi->req_lock);
13663da28eb1STrond Myklebust 	res = nfs_scan_commit(inode, &head, 0, 0);
13673da28eb1STrond Myklebust 	spin_unlock(&nfsi->req_lock);
13681da177e4SLinus Torvalds 	if (res) {
136940859d7eSChuck Lever 		error = nfs_commit_list(inode, &head, how);
13701da177e4SLinus Torvalds 		if (error < 0)
13711da177e4SLinus Torvalds 			return error;
13723da28eb1STrond Myklebust 	}
13731da177e4SLinus Torvalds 	return res;
13741da177e4SLinus Torvalds }
13751da177e4SLinus Torvalds #endif
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds int nfs_sync_inode(struct inode *inode, unsigned long idx_start,
13781da177e4SLinus Torvalds 		  unsigned int npages, int how)
13791da177e4SLinus Torvalds {
1380*70b9ecbdSTrond Myklebust 	int nocommit = how & FLUSH_NOCOMMIT;
1381*70b9ecbdSTrond Myklebust 	int wait = how & FLUSH_WAIT;
1382*70b9ecbdSTrond Myklebust 	int error;
13831da177e4SLinus Torvalds 
1384*70b9ecbdSTrond Myklebust 	how &= ~(FLUSH_WAIT|FLUSH_NOCOMMIT);
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds 	do {
1387*70b9ecbdSTrond Myklebust 		if (wait) {
13881da177e4SLinus Torvalds 			error = nfs_wait_on_requests(inode, idx_start, npages);
1389*70b9ecbdSTrond Myklebust 			if (error != 0)
1390*70b9ecbdSTrond Myklebust 				continue;
1391*70b9ecbdSTrond Myklebust 		}
13921da177e4SLinus Torvalds 		error = nfs_flush_inode(inode, idx_start, npages, how);
1393*70b9ecbdSTrond Myklebust 		if (error != 0)
1394*70b9ecbdSTrond Myklebust 			continue;
1395*70b9ecbdSTrond Myklebust 		if (!nocommit)
13963da28eb1STrond Myklebust 			error = nfs_commit_inode(inode, how);
13971da177e4SLinus Torvalds 	} while (error > 0);
13981da177e4SLinus Torvalds 	return error;
13991da177e4SLinus Torvalds }
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds int nfs_init_writepagecache(void)
14021da177e4SLinus Torvalds {
14031da177e4SLinus Torvalds 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
14041da177e4SLinus Torvalds 					     sizeof(struct nfs_write_data),
14051da177e4SLinus Torvalds 					     0, SLAB_HWCACHE_ALIGN,
14061da177e4SLinus Torvalds 					     NULL, NULL);
14071da177e4SLinus Torvalds 	if (nfs_wdata_cachep == NULL)
14081da177e4SLinus Torvalds 		return -ENOMEM;
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds 	nfs_wdata_mempool = mempool_create(MIN_POOL_WRITE,
14111da177e4SLinus Torvalds 					   mempool_alloc_slab,
14121da177e4SLinus Torvalds 					   mempool_free_slab,
14131da177e4SLinus Torvalds 					   nfs_wdata_cachep);
14141da177e4SLinus Torvalds 	if (nfs_wdata_mempool == NULL)
14151da177e4SLinus Torvalds 		return -ENOMEM;
14161da177e4SLinus Torvalds 
14171da177e4SLinus Torvalds 	nfs_commit_mempool = mempool_create(MIN_POOL_COMMIT,
14181da177e4SLinus Torvalds 					   mempool_alloc_slab,
14191da177e4SLinus Torvalds 					   mempool_free_slab,
14201da177e4SLinus Torvalds 					   nfs_wdata_cachep);
14211da177e4SLinus Torvalds 	if (nfs_commit_mempool == NULL)
14221da177e4SLinus Torvalds 		return -ENOMEM;
14231da177e4SLinus Torvalds 
14241da177e4SLinus Torvalds 	return 0;
14251da177e4SLinus Torvalds }
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds void nfs_destroy_writepagecache(void)
14281da177e4SLinus Torvalds {
14291da177e4SLinus Torvalds 	mempool_destroy(nfs_commit_mempool);
14301da177e4SLinus Torvalds 	mempool_destroy(nfs_wdata_mempool);
14311da177e4SLinus Torvalds 	if (kmem_cache_destroy(nfs_wdata_cachep))
14321da177e4SLinus Torvalds 		printk(KERN_INFO "nfs_write_data: not all structures were freed\n");
14331da177e4SLinus Torvalds }
14341da177e4SLinus Torvalds 
1435