| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * linux/fs/nfs/write.c | 
 | 3 |  * | 
| Trond Myklebust | 7c85d90 | 2006-12-13 15:23:48 -0500 | [diff] [blame] | 4 |  * Write file data over NFS. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 |  * | 
 | 6 |  * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de> | 
 | 7 |  */ | 
 | 8 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/types.h> | 
 | 10 | #include <linux/slab.h> | 
 | 11 | #include <linux/mm.h> | 
 | 12 | #include <linux/pagemap.h> | 
 | 13 | #include <linux/file.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/writeback.h> | 
| Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 15 | #include <linux/swap.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 |  | 
 | 17 | #include <linux/sunrpc/clnt.h> | 
 | 18 | #include <linux/nfs_fs.h> | 
 | 19 | #include <linux/nfs_mount.h> | 
 | 20 | #include <linux/nfs_page.h> | 
| Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 21 | #include <linux/backing-dev.h> | 
 | 22 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <asm/uaccess.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 |  | 
 | 25 | #include "delegation.h" | 
| Trond Myklebust | 49a70f2 | 2006-12-05 00:35:38 -0500 | [diff] [blame] | 26 | #include "internal.h" | 
| Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 27 | #include "iostat.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 |  | 
 | 29 | #define NFSDBG_FACILITY		NFSDBG_PAGECACHE | 
 | 30 |  | 
 | 31 | #define MIN_POOL_WRITE		(32) | 
 | 32 | #define MIN_POOL_COMMIT		(4) | 
 | 33 |  | 
 | 34 | /* | 
 | 35 |  * Local function declarations | 
 | 36 |  */ | 
 | 37 | static struct nfs_page * nfs_update_request(struct nfs_open_context*, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | 					    struct page *, | 
 | 39 | 					    unsigned int, unsigned int); | 
| Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 40 | static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc, | 
 | 41 | 				  struct inode *inode, int ioflags); | 
| Fred Isaman | f8512ad | 2008-03-19 11:24:39 -0400 | [diff] [blame] | 42 | static void nfs_redirty_request(struct nfs_page *req); | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 43 | static const struct rpc_call_ops nfs_write_partial_ops; | 
 | 44 | static const struct rpc_call_ops nfs_write_full_ops; | 
 | 45 | static const struct rpc_call_ops nfs_commit_ops; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 |  | 
| Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 47 | static struct kmem_cache *nfs_wdata_cachep; | 
| Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 48 | static mempool_t *nfs_wdata_mempool; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | static mempool_t *nfs_commit_mempool; | 
 | 50 |  | 
| Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 51 | struct nfs_write_data *nfs_commitdata_alloc(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { | 
| Christoph Lameter | e6b4f8d | 2006-12-06 20:33:14 -0800 | [diff] [blame] | 53 | 	struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS); | 
| Chuck Lever | 40859d7 | 2005-11-30 18:09:02 -0500 | [diff] [blame] | 54 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | 	if (p) { | 
 | 56 | 		memset(p, 0, sizeof(*p)); | 
 | 57 | 		INIT_LIST_HEAD(&p->pages); | 
 | 58 | 	} | 
 | 59 | 	return p; | 
 | 60 | } | 
 | 61 |  | 
| Trond Myklebust | 5e4424a | 2008-02-25 21:53:49 -0800 | [diff] [blame] | 62 | void nfs_commit_free(struct nfs_write_data *p) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { | 
| Chuck Lever | 40859d7 | 2005-11-30 18:09:02 -0500 | [diff] [blame] | 64 | 	if (p && (p->pagevec != &p->page_array[0])) | 
 | 65 | 		kfree(p->pagevec); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | 	mempool_free(p, nfs_commit_mempool); | 
 | 67 | } | 
 | 68 |  | 
| Trond Myklebust | 8d5658c | 2007-04-10 09:26:35 -0400 | [diff] [blame] | 69 | struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount) | 
| Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 70 | { | 
| Christoph Lameter | e6b4f8d | 2006-12-06 20:33:14 -0800 | [diff] [blame] | 71 | 	struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS); | 
| Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 72 |  | 
 | 73 | 	if (p) { | 
 | 74 | 		memset(p, 0, sizeof(*p)); | 
 | 75 | 		INIT_LIST_HEAD(&p->pages); | 
| Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 76 | 		p->npages = pagecount; | 
| Chuck Lever | 0d0b5cb | 2006-05-25 01:40:53 -0400 | [diff] [blame] | 77 | 		if (pagecount <= ARRAY_SIZE(p->page_array)) | 
 | 78 | 			p->pagevec = p->page_array; | 
| Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 79 | 		else { | 
| Chuck Lever | 0d0b5cb | 2006-05-25 01:40:53 -0400 | [diff] [blame] | 80 | 			p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS); | 
 | 81 | 			if (!p->pagevec) { | 
| Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 82 | 				mempool_free(p, nfs_wdata_mempool); | 
 | 83 | 				p = NULL; | 
 | 84 | 			} | 
 | 85 | 		} | 
 | 86 | 	} | 
 | 87 | 	return p; | 
 | 88 | } | 
 | 89 |  | 
| Trond Myklebust | 5e4424a | 2008-02-25 21:53:49 -0800 | [diff] [blame] | 90 | static void nfs_writedata_free(struct nfs_write_data *p) | 
| Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 91 | { | 
 | 92 | 	if (p && (p->pagevec != &p->page_array[0])) | 
 | 93 | 		kfree(p->pagevec); | 
 | 94 | 	mempool_free(p, nfs_wdata_mempool); | 
 | 95 | } | 
 | 96 |  | 
| Trond Myklebust | 383ba71 | 2008-02-19 20:04:20 -0500 | [diff] [blame] | 97 | void nfs_writedata_release(void *data) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | { | 
| Trond Myklebust | 383ba71 | 2008-02-19 20:04:20 -0500 | [diff] [blame] | 99 | 	struct nfs_write_data *wdata = data; | 
 | 100 |  | 
 | 101 | 	put_nfs_open_context(wdata->args.context); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | 	nfs_writedata_free(wdata); | 
 | 103 | } | 
 | 104 |  | 
| Trond Myklebust | 7b159fc | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 105 | static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error) | 
 | 106 | { | 
 | 107 | 	ctx->error = error; | 
 | 108 | 	smp_wmb(); | 
 | 109 | 	set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); | 
 | 110 | } | 
 | 111 |  | 
| Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 112 | static struct nfs_page *nfs_page_find_request_locked(struct page *page) | 
 | 113 | { | 
 | 114 | 	struct nfs_page *req = NULL; | 
 | 115 |  | 
 | 116 | 	if (PagePrivate(page)) { | 
 | 117 | 		req = (struct nfs_page *)page_private(page); | 
 | 118 | 		if (req != NULL) | 
| Trond Myklebust | c03b402 | 2007-06-17 13:26:38 -0400 | [diff] [blame] | 119 | 			kref_get(&req->wb_kref); | 
| Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 120 | 	} | 
 | 121 | 	return req; | 
 | 122 | } | 
 | 123 |  | 
 | 124 | static struct nfs_page *nfs_page_find_request(struct page *page) | 
 | 125 | { | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 126 | 	struct inode *inode = page->mapping->host; | 
| Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 127 | 	struct nfs_page *req = NULL; | 
| Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 128 |  | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 129 | 	spin_lock(&inode->i_lock); | 
| Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 130 | 	req = nfs_page_find_request_locked(page); | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 131 | 	spin_unlock(&inode->i_lock); | 
| Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 132 | 	return req; | 
 | 133 | } | 
 | 134 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | /* Adjust the file length if we're writing beyond the end */ | 
 | 136 | static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) | 
 | 137 | { | 
 | 138 | 	struct inode *inode = page->mapping->host; | 
 | 139 | 	loff_t end, i_size = i_size_read(inode); | 
| Trond Myklebust | ca52fec | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 140 | 	pgoff_t end_index = (i_size - 1) >> PAGE_CACHE_SHIFT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 |  | 
 | 142 | 	if (i_size > 0 && page->index < end_index) | 
 | 143 | 		return; | 
 | 144 | 	end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count); | 
 | 145 | 	if (i_size >= end) | 
 | 146 | 		return; | 
| Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 147 | 	nfs_inc_stats(inode, NFSIOS_EXTENDWRITE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | 	i_size_write(inode, end); | 
 | 149 | } | 
 | 150 |  | 
| Trond Myklebust | a301b77 | 2007-02-06 11:07:15 -0800 | [diff] [blame] | 151 | /* A writeback failed: mark the page as bad, and invalidate the page cache */ | 
 | 152 | static void nfs_set_pageerror(struct page *page) | 
 | 153 | { | 
 | 154 | 	SetPageError(page); | 
 | 155 | 	nfs_zap_mapping(page->mapping->host, page->mapping); | 
 | 156 | } | 
 | 157 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | /* We can set the PG_uptodate flag if we see that a write request | 
 | 159 |  * covers the full page. | 
 | 160 |  */ | 
 | 161 | static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count) | 
 | 162 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | 	if (PageUptodate(page)) | 
 | 164 | 		return; | 
 | 165 | 	if (base != 0) | 
 | 166 | 		return; | 
| Trond Myklebust | 49a70f2 | 2006-12-05 00:35:38 -0500 | [diff] [blame] | 167 | 	if (count != nfs_page_length(page)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | 		return; | 
| Trond Myklebust | 49a70f2 | 2006-12-05 00:35:38 -0500 | [diff] [blame] | 169 | 	SetPageUptodate(page); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } | 
 | 171 |  | 
| Trond Myklebust | e21195a | 2006-12-05 00:35:39 -0500 | [diff] [blame] | 172 | static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | 		unsigned int offset, unsigned int count) | 
 | 174 | { | 
 | 175 | 	struct nfs_page	*req; | 
| Trond Myklebust | e21195a | 2006-12-05 00:35:39 -0500 | [diff] [blame] | 176 | 	int ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 |  | 
| Trond Myklebust | e21195a | 2006-12-05 00:35:39 -0500 | [diff] [blame] | 178 | 	for (;;) { | 
 | 179 | 		req = nfs_update_request(ctx, page, offset, count); | 
 | 180 | 		if (!IS_ERR(req)) | 
 | 181 | 			break; | 
 | 182 | 		ret = PTR_ERR(req); | 
 | 183 | 		if (ret != -EBUSY) | 
 | 184 | 			return ret; | 
 | 185 | 		ret = nfs_wb_page(page->mapping->host, page); | 
 | 186 | 		if (ret != 0) | 
 | 187 | 			return ret; | 
 | 188 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | 	/* Update file length */ | 
 | 190 | 	nfs_grow_file(page, offset, count); | 
| Trond Myklebust | acee478 | 2008-01-22 17:13:07 -0500 | [diff] [blame] | 191 | 	nfs_clear_page_tag_locked(req); | 
| Trond Myklebust | abd3e64 | 2006-01-03 09:55:02 +0100 | [diff] [blame] | 192 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | } | 
 | 194 |  | 
 | 195 | static int wb_priority(struct writeback_control *wbc) | 
 | 196 | { | 
 | 197 | 	if (wbc->for_reclaim) | 
| Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 198 | 		return FLUSH_HIGHPRI | FLUSH_STABLE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | 	if (wbc->for_kupdate) | 
 | 200 | 		return FLUSH_LOWPRI; | 
 | 201 | 	return 0; | 
 | 202 | } | 
 | 203 |  | 
 | 204 | /* | 
| Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 205 |  * NFS congestion control | 
 | 206 |  */ | 
 | 207 |  | 
 | 208 | int nfs_congestion_kb; | 
 | 209 |  | 
 | 210 | #define NFS_CONGESTION_ON_THRESH 	(nfs_congestion_kb >> (PAGE_SHIFT-10)) | 
 | 211 | #define NFS_CONGESTION_OFF_THRESH	\ | 
 | 212 | 	(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2)) | 
 | 213 |  | 
| Trond Myklebust | 5a6d41b | 2007-04-14 19:10:12 -0400 | [diff] [blame] | 214 | static int nfs_set_page_writeback(struct page *page) | 
| Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 215 | { | 
| Trond Myklebust | 5a6d41b | 2007-04-14 19:10:12 -0400 | [diff] [blame] | 216 | 	int ret = test_set_page_writeback(page); | 
 | 217 |  | 
 | 218 | 	if (!ret) { | 
| Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 219 | 		struct inode *inode = page->mapping->host; | 
 | 220 | 		struct nfs_server *nfss = NFS_SERVER(inode); | 
 | 221 |  | 
| Peter Zijlstra | 277866a | 2007-05-08 00:35:12 -0700 | [diff] [blame] | 222 | 		if (atomic_long_inc_return(&nfss->writeback) > | 
| Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 223 | 				NFS_CONGESTION_ON_THRESH) | 
 | 224 | 			set_bdi_congested(&nfss->backing_dev_info, WRITE); | 
 | 225 | 	} | 
| Trond Myklebust | 5a6d41b | 2007-04-14 19:10:12 -0400 | [diff] [blame] | 226 | 	return ret; | 
| Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 227 | } | 
 | 228 |  | 
 | 229 | static void nfs_end_page_writeback(struct page *page) | 
 | 230 | { | 
 | 231 | 	struct inode *inode = page->mapping->host; | 
 | 232 | 	struct nfs_server *nfss = NFS_SERVER(inode); | 
 | 233 |  | 
 | 234 | 	end_page_writeback(page); | 
| Peter Zijlstra | c4dc4be | 2007-10-16 23:25:41 -0700 | [diff] [blame] | 235 | 	if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) | 
| Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 236 | 		clear_bdi_congested(&nfss->backing_dev_info, WRITE); | 
| Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 237 | } | 
 | 238 |  | 
 | 239 | /* | 
| Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 240 |  * Find an associated nfs write request, and prepare to flush it out | 
| Trond Myklebust | 9cccef9 | 2007-07-22 17:09:05 -0400 | [diff] [blame] | 241 |  * May return an error if the user signalled nfs_wait_on_request(). | 
| Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 242 |  */ | 
| Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 243 | static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio, | 
 | 244 | 				struct page *page) | 
| Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 245 | { | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 246 | 	struct inode *inode = page->mapping->host; | 
| Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 247 | 	struct nfs_page *req; | 
| Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 248 | 	int ret; | 
 | 249 |  | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 250 | 	spin_lock(&inode->i_lock); | 
| Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 251 | 	for(;;) { | 
 | 252 | 		req = nfs_page_find_request_locked(page); | 
 | 253 | 		if (req == NULL) { | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 254 | 			spin_unlock(&inode->i_lock); | 
| Trond Myklebust | 9cccef9 | 2007-07-22 17:09:05 -0400 | [diff] [blame] | 255 | 			return 0; | 
| Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 256 | 		} | 
| Trond Myklebust | acee478 | 2008-01-22 17:13:07 -0500 | [diff] [blame] | 257 | 		if (nfs_set_page_tag_locked(req)) | 
| Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 258 | 			break; | 
 | 259 | 		/* Note: If we hold the page lock, as is the case in nfs_writepage, | 
| Trond Myklebust | acee478 | 2008-01-22 17:13:07 -0500 | [diff] [blame] | 260 | 		 *	 then the call to nfs_set_page_tag_locked() will always | 
| Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 261 | 		 *	 succeed provided that someone hasn't already marked the | 
 | 262 | 		 *	 request as dirty (in which case we don't care). | 
 | 263 | 		 */ | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 264 | 		spin_unlock(&inode->i_lock); | 
| Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 265 | 		ret = nfs_wait_on_request(req); | 
 | 266 | 		nfs_release_request(req); | 
 | 267 | 		if (ret != 0) | 
 | 268 | 			return ret; | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 269 | 		spin_lock(&inode->i_lock); | 
| Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 270 | 	} | 
| Trond Myklebust | 612c938 | 2007-04-20 16:12:45 -0400 | [diff] [blame] | 271 | 	if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) { | 
 | 272 | 		/* This request is marked for commit */ | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 273 | 		spin_unlock(&inode->i_lock); | 
| Trond Myklebust | acee478 | 2008-01-22 17:13:07 -0500 | [diff] [blame] | 274 | 		nfs_clear_page_tag_locked(req); | 
| Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 275 | 		nfs_pageio_complete(pgio); | 
| Trond Myklebust | 9cccef9 | 2007-07-22 17:09:05 -0400 | [diff] [blame] | 276 | 		return 0; | 
| Trond Myklebust | 612c938 | 2007-04-20 16:12:45 -0400 | [diff] [blame] | 277 | 	} | 
| Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 278 | 	if (nfs_set_page_writeback(page) != 0) { | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 279 | 		spin_unlock(&inode->i_lock); | 
| Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 280 | 		BUG(); | 
 | 281 | 	} | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 282 | 	spin_unlock(&inode->i_lock); | 
| Fred Isaman | f8512ad | 2008-03-19 11:24:39 -0400 | [diff] [blame] | 283 | 	if (!nfs_pageio_add_request(pgio, req)) { | 
 | 284 | 		nfs_redirty_request(req); | 
| Fred Isaman | f8512ad | 2008-03-19 11:24:39 -0400 | [diff] [blame] | 285 | 		return pgio->pg_error; | 
 | 286 | 	} | 
| Trond Myklebust | 9cccef9 | 2007-07-22 17:09:05 -0400 | [diff] [blame] | 287 | 	return 0; | 
| Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 288 | } | 
 | 289 |  | 
| Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 290 | static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio) | 
 | 291 | { | 
 | 292 | 	struct inode *inode = page->mapping->host; | 
 | 293 |  | 
 | 294 | 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); | 
 | 295 | 	nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1); | 
 | 296 |  | 
 | 297 | 	nfs_pageio_cond_complete(pgio, page->index); | 
 | 298 | 	return nfs_page_async_flush(pgio, page); | 
 | 299 | } | 
 | 300 |  | 
| Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 301 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 |  * Write an mmapped page to the server. | 
 | 303 |  */ | 
| Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 304 | static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | { | 
| Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 306 | 	struct nfs_pageio_descriptor pgio; | 
| Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 307 | 	int err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 |  | 
| Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 309 | 	nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc)); | 
 | 310 | 	err = nfs_do_writepage(page, wbc, &pgio); | 
 | 311 | 	nfs_pageio_complete(&pgio); | 
 | 312 | 	if (err < 0) | 
 | 313 | 		return err; | 
 | 314 | 	if (pgio.pg_error < 0) | 
 | 315 | 		return pgio.pg_error; | 
 | 316 | 	return 0; | 
| Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 317 | } | 
 | 318 |  | 
 | 319 | int nfs_writepage(struct page *page, struct writeback_control *wbc) | 
 | 320 | { | 
| Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 321 | 	int ret; | 
| Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 322 |  | 
| Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 323 | 	ret = nfs_writepage_locked(page, wbc); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | 	unlock_page(page); | 
| Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 325 | 	return ret; | 
 | 326 | } | 
 | 327 |  | 
 | 328 | static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data) | 
 | 329 | { | 
 | 330 | 	int ret; | 
 | 331 |  | 
 | 332 | 	ret = nfs_do_writepage(page, wbc, data); | 
 | 333 | 	unlock_page(page); | 
 | 334 | 	return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | } | 
 | 336 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) | 
 | 338 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | 	struct inode *inode = mapping->host; | 
| Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 340 | 	struct nfs_pageio_descriptor pgio; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | 	int err; | 
 | 342 |  | 
| Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 343 | 	nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); | 
 | 344 |  | 
| Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 345 | 	nfs_pageio_init_write(&pgio, inode, wb_priority(wbc)); | 
| Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 346 | 	err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio); | 
| Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 347 | 	nfs_pageio_complete(&pgio); | 
| Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 348 | 	if (err < 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | 		return err; | 
| Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 350 | 	if (pgio.pg_error < 0) | 
| Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 351 | 		return pgio.pg_error; | 
 | 352 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } | 
 | 354 |  | 
 | 355 | /* | 
 | 356 |  * Insert a write request into an inode | 
 | 357 |  */ | 
| Nick Piggin | 2785259 | 2008-02-04 23:48:37 -0800 | [diff] [blame] | 358 | static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | { | 
 | 360 | 	struct nfs_inode *nfsi = NFS_I(inode); | 
 | 361 | 	int error; | 
 | 362 |  | 
 | 363 | 	error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req); | 
| Nick Piggin | 2785259 | 2008-02-04 23:48:37 -0800 | [diff] [blame] | 364 | 	BUG_ON(error); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | 	if (!nfsi->npages) { | 
 | 366 | 		igrab(inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | 		if (nfs_have_delegation(inode, FMODE_WRITE)) | 
 | 368 | 			nfsi->change_attr++; | 
 | 369 | 	} | 
| Trond Myklebust | deb7d63 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 370 | 	SetPagePrivate(req->wb_page); | 
| Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 371 | 	set_page_private(req->wb_page, (unsigned long)req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | 	nfsi->npages++; | 
| Trond Myklebust | c03b402 | 2007-06-17 13:26:38 -0400 | [diff] [blame] | 373 | 	kref_get(&req->wb_kref); | 
| Nick Piggin | 2785259 | 2008-02-04 23:48:37 -0800 | [diff] [blame] | 374 | 	radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, | 
 | 375 | 				NFS_PAGE_TAG_LOCKED); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } | 
 | 377 |  | 
 | 378 | /* | 
| Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 379 |  * Remove a write request from an inode | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 |  */ | 
 | 381 | static void nfs_inode_remove_request(struct nfs_page *req) | 
 | 382 | { | 
| Trond Myklebust | 88be9f9 | 2007-06-05 10:42:27 -0400 | [diff] [blame] | 383 | 	struct inode *inode = req->wb_context->path.dentry->d_inode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | 	struct nfs_inode *nfsi = NFS_I(inode); | 
 | 385 |  | 
 | 386 | 	BUG_ON (!NFS_WBACK_BUSY(req)); | 
 | 387 |  | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 388 | 	spin_lock(&inode->i_lock); | 
| Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 389 | 	set_page_private(req->wb_page, 0); | 
| Trond Myklebust | deb7d63 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 390 | 	ClearPagePrivate(req->wb_page); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | 	radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index); | 
 | 392 | 	nfsi->npages--; | 
 | 393 | 	if (!nfsi->npages) { | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 394 | 		spin_unlock(&inode->i_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | 		iput(inode); | 
 | 396 | 	} else | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 397 | 		spin_unlock(&inode->i_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | 	nfs_clear_request(req); | 
 | 399 | 	nfs_release_request(req); | 
 | 400 | } | 
 | 401 |  | 
| Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 402 | static void | 
| Fred | 6d884e8 | 2008-03-19 11:24:38 -0400 | [diff] [blame] | 403 | nfs_mark_request_dirty(struct nfs_page *req) | 
| Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 404 | { | 
| Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 405 | 	__set_page_dirty_nobuffers(req->wb_page); | 
 | 406 | } | 
 | 407 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | /* | 
 | 409 |  * Check if a request is dirty | 
 | 410 |  */ | 
 | 411 | static inline int | 
 | 412 | nfs_dirty_request(struct nfs_page *req) | 
 | 413 | { | 
| Trond Myklebust | 5a6d41b | 2007-04-14 19:10:12 -0400 | [diff] [blame] | 414 | 	struct page *page = req->wb_page; | 
 | 415 |  | 
| Trond Myklebust | 612c938 | 2007-04-20 16:12:45 -0400 | [diff] [blame] | 416 | 	if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags)) | 
| Trond Myklebust | 5a6d41b | 2007-04-14 19:10:12 -0400 | [diff] [blame] | 417 | 		return 0; | 
| Fred Isaman | 38def50 | 2008-05-01 20:03:22 +0300 | [diff] [blame] | 418 | 	return !PageWriteback(page); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | } | 
 | 420 |  | 
 | 421 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) | 
 | 422 | /* | 
 | 423 |  * Add a request to the inode's commit list. | 
 | 424 |  */ | 
 | 425 | static void | 
 | 426 | nfs_mark_request_commit(struct nfs_page *req) | 
 | 427 | { | 
| Trond Myklebust | 88be9f9 | 2007-06-05 10:42:27 -0400 | [diff] [blame] | 428 | 	struct inode *inode = req->wb_context->path.dentry->d_inode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | 	struct nfs_inode *nfsi = NFS_I(inode); | 
 | 430 |  | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 431 | 	spin_lock(&inode->i_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | 	nfsi->ncommit++; | 
| Trond Myklebust | 612c938 | 2007-04-20 16:12:45 -0400 | [diff] [blame] | 433 | 	set_bit(PG_NEED_COMMIT, &(req)->wb_flags); | 
| Trond Myklebust | 5c36968 | 2007-06-17 15:27:42 -0400 | [diff] [blame] | 434 | 	radix_tree_tag_set(&nfsi->nfs_page_tree, | 
 | 435 | 			req->wb_index, | 
 | 436 | 			NFS_PAGE_TAG_COMMIT); | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 437 | 	spin_unlock(&inode->i_lock); | 
| Christoph Lameter | fd39fc8 | 2006-06-30 01:55:40 -0700 | [diff] [blame] | 438 | 	inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); | 
| Peter Zijlstra | c9e51e4 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 439 | 	inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE); | 
| Trond Myklebust | a180304 | 2006-12-05 00:45:19 -0500 | [diff] [blame] | 440 | 	__mark_inode_dirty(inode, I_DIRTY_DATASYNC); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } | 
| Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 442 |  | 
 | 443 | static inline | 
 | 444 | int nfs_write_need_commit(struct nfs_write_data *data) | 
 | 445 | { | 
 | 446 | 	return data->verf.committed != NFS_FILE_SYNC; | 
 | 447 | } | 
 | 448 |  | 
 | 449 | static inline | 
 | 450 | int nfs_reschedule_unstable_write(struct nfs_page *req) | 
 | 451 | { | 
| Trond Myklebust | 612c938 | 2007-04-20 16:12:45 -0400 | [diff] [blame] | 452 | 	if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) { | 
| Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 453 | 		nfs_mark_request_commit(req); | 
 | 454 | 		return 1; | 
 | 455 | 	} | 
 | 456 | 	if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) { | 
| Fred | 6d884e8 | 2008-03-19 11:24:38 -0400 | [diff] [blame] | 457 | 		nfs_mark_request_dirty(req); | 
| Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 458 | 		return 1; | 
 | 459 | 	} | 
 | 460 | 	return 0; | 
 | 461 | } | 
 | 462 | #else | 
 | 463 | static inline void | 
 | 464 | nfs_mark_request_commit(struct nfs_page *req) | 
 | 465 | { | 
 | 466 | } | 
 | 467 |  | 
 | 468 | static inline | 
 | 469 | int nfs_write_need_commit(struct nfs_write_data *data) | 
 | 470 | { | 
 | 471 | 	return 0; | 
 | 472 | } | 
 | 473 |  | 
 | 474 | static inline | 
 | 475 | int nfs_reschedule_unstable_write(struct nfs_page *req) | 
 | 476 | { | 
 | 477 | 	return 0; | 
 | 478 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | #endif | 
 | 480 |  | 
 | 481 | /* | 
 | 482 |  * Wait for a request to complete. | 
 | 483 |  * | 
| Matthew Wilcox | 150030b | 2007-12-06 16:24:39 -0500 | [diff] [blame] | 484 |  * Interruptible by fatal signals only. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 |  */ | 
| Trond Myklebust | ca52fec | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 486 | static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | { | 
 | 488 | 	struct nfs_inode *nfsi = NFS_I(inode); | 
 | 489 | 	struct nfs_page *req; | 
| Trond Myklebust | ca52fec | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 490 | 	pgoff_t idx_end, next; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | 	unsigned int		res = 0; | 
 | 492 | 	int			error; | 
 | 493 |  | 
 | 494 | 	if (npages == 0) | 
 | 495 | 		idx_end = ~0; | 
 | 496 | 	else | 
 | 497 | 		idx_end = idx_start + npages - 1; | 
 | 498 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | 	next = idx_start; | 
| Trond Myklebust | 9fd367f | 2007-06-17 15:10:24 -0400 | [diff] [blame] | 500 | 	while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | 		if (req->wb_index > idx_end) | 
 | 502 | 			break; | 
 | 503 |  | 
 | 504 | 		next = req->wb_index + 1; | 
| Trond Myklebust | c6a556b | 2005-06-22 17:16:30 +0000 | [diff] [blame] | 505 | 		BUG_ON(!NFS_WBACK_BUSY(req)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 |  | 
| Trond Myklebust | c03b402 | 2007-06-17 13:26:38 -0400 | [diff] [blame] | 507 | 		kref_get(&req->wb_kref); | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 508 | 		spin_unlock(&inode->i_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | 		error = nfs_wait_on_request(req); | 
 | 510 | 		nfs_release_request(req); | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 511 | 		spin_lock(&inode->i_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | 		if (error < 0) | 
 | 513 | 			return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | 		res++; | 
 | 515 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | 	return res; | 
 | 517 | } | 
 | 518 |  | 
| Trond Myklebust | 83715ad | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 519 | static void nfs_cancel_commit_list(struct list_head *head) | 
 | 520 | { | 
 | 521 | 	struct nfs_page *req; | 
 | 522 |  | 
 | 523 | 	while(!list_empty(head)) { | 
 | 524 | 		req = nfs_list_entry(head->next); | 
| Trond Myklebust | b6dff26 | 2006-10-19 23:28:38 -0700 | [diff] [blame] | 525 | 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); | 
| Peter Zijlstra | c9e51e4 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 526 | 		dec_bdi_stat(req->wb_page->mapping->backing_dev_info, | 
 | 527 | 				BDI_RECLAIMABLE); | 
| Trond Myklebust | 83715ad | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 528 | 		nfs_list_remove_request(req); | 
| Trond Myklebust | 612c938 | 2007-04-20 16:12:45 -0400 | [diff] [blame] | 529 | 		clear_bit(PG_NEED_COMMIT, &(req)->wb_flags); | 
| Trond Myklebust | 83715ad | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 530 | 		nfs_inode_remove_request(req); | 
| Trond Myklebust | b6dff26 | 2006-10-19 23:28:38 -0700 | [diff] [blame] | 531 | 		nfs_unlock_request(req); | 
| Trond Myklebust | 83715ad | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 532 | 	} | 
 | 533 | } | 
 | 534 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) | 
 | 536 | /* | 
 | 537 |  * nfs_scan_commit - Scan an inode for commit requests | 
 | 538 |  * @inode: NFS inode to scan | 
 | 539 |  * @dst: destination list | 
 | 540 |  * @idx_start: lower bound of page->index to scan. | 
 | 541 |  * @npages: idx_start + npages sets the upper bound to scan. | 
 | 542 |  * | 
 | 543 |  * Moves requests from the inode's 'commit' request list. | 
 | 544 |  * The requests are *not* checked to ensure that they form a contiguous set. | 
 | 545 |  */ | 
 | 546 | static int | 
| Trond Myklebust | ca52fec | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 547 | nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | { | 
 | 549 | 	struct nfs_inode *nfsi = NFS_I(inode); | 
| Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 550 | 	int res = 0; | 
 | 551 |  | 
 | 552 | 	if (nfsi->ncommit != 0) { | 
| Trond Myklebust | 5c36968 | 2007-06-17 15:27:42 -0400 | [diff] [blame] | 553 | 		res = nfs_scan_list(nfsi, dst, idx_start, npages, | 
 | 554 | 				NFS_PAGE_TAG_COMMIT); | 
| Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 555 | 		nfsi->ncommit -= res; | 
| Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 556 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | 	return res; | 
 | 558 | } | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 559 | #else | 
| Trond Myklebust | ca52fec | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 560 | static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages) | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 561 | { | 
 | 562 | 	return 0; | 
 | 563 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | #endif | 
 | 565 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | /* | 
 | 567 |  * Try to update any existing write request, or create one if there is none. | 
 | 568 |  * In order to match, the request's credentials must match those of | 
 | 569 |  * the calling process. | 
 | 570 |  * | 
 | 571 |  * Note: Should always be called with the Page Lock held! | 
 | 572 |  */ | 
 | 573 | static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx, | 
| Trond Myklebust | e21195a | 2006-12-05 00:35:39 -0500 | [diff] [blame] | 574 | 		struct page *page, unsigned int offset, unsigned int bytes) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | { | 
| Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 576 | 	struct address_space *mapping = page->mapping; | 
 | 577 | 	struct inode *inode = mapping->host; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | 	struct nfs_page		*req, *new = NULL; | 
| Trond Myklebust | ca52fec | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 579 | 	pgoff_t		rqend, end; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 |  | 
 | 581 | 	end = offset + bytes; | 
 | 582 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | 	for (;;) { | 
 | 584 | 		/* Loop over all inode entries and see if we find | 
 | 585 | 		 * A request for the page we wish to update | 
 | 586 | 		 */ | 
| Nick Piggin | 2785259 | 2008-02-04 23:48:37 -0800 | [diff] [blame] | 587 | 		if (new) { | 
 | 588 | 			if (radix_tree_preload(GFP_NOFS)) { | 
 | 589 | 				nfs_release_request(new); | 
 | 590 | 				return ERR_PTR(-ENOMEM); | 
 | 591 | 			} | 
 | 592 | 		} | 
 | 593 |  | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 594 | 		spin_lock(&inode->i_lock); | 
| Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 595 | 		req = nfs_page_find_request_locked(page); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | 		if (req) { | 
| Trond Myklebust | acee478 | 2008-01-22 17:13:07 -0500 | [diff] [blame] | 597 | 			if (!nfs_set_page_tag_locked(req)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | 				int error; | 
| Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 599 |  | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 600 | 				spin_unlock(&inode->i_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | 				error = nfs_wait_on_request(req); | 
 | 602 | 				nfs_release_request(req); | 
| Neil Brown | 1dd594b | 2006-03-20 13:44:04 -0500 | [diff] [blame] | 603 | 				if (error < 0) { | 
| Nick Piggin | 2785259 | 2008-02-04 23:48:37 -0800 | [diff] [blame] | 604 | 					if (new) { | 
 | 605 | 						radix_tree_preload_end(); | 
| Neil Brown | 1dd594b | 2006-03-20 13:44:04 -0500 | [diff] [blame] | 606 | 						nfs_release_request(new); | 
| Nick Piggin | 2785259 | 2008-02-04 23:48:37 -0800 | [diff] [blame] | 607 | 					} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | 					return ERR_PTR(error); | 
| Neil Brown | 1dd594b | 2006-03-20 13:44:04 -0500 | [diff] [blame] | 609 | 				} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | 				continue; | 
 | 611 | 			} | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 612 | 			spin_unlock(&inode->i_lock); | 
| Nick Piggin | 2785259 | 2008-02-04 23:48:37 -0800 | [diff] [blame] | 613 | 			if (new) { | 
 | 614 | 				radix_tree_preload_end(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | 				nfs_release_request(new); | 
| Nick Piggin | 2785259 | 2008-02-04 23:48:37 -0800 | [diff] [blame] | 616 | 			} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | 			break; | 
 | 618 | 		} | 
 | 619 |  | 
 | 620 | 		if (new) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | 			nfs_lock_request_dontget(new); | 
| Nick Piggin | 2785259 | 2008-02-04 23:48:37 -0800 | [diff] [blame] | 622 | 			nfs_inode_add_request(inode, new); | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 623 | 			spin_unlock(&inode->i_lock); | 
| Nick Piggin | 2785259 | 2008-02-04 23:48:37 -0800 | [diff] [blame] | 624 | 			radix_tree_preload_end(); | 
| Trond Myklebust | 61e930a | 2007-10-18 17:08:05 -0400 | [diff] [blame] | 625 | 			req = new; | 
 | 626 | 			goto zero_page; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | 		} | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 628 | 		spin_unlock(&inode->i_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 |  | 
 | 630 | 		new = nfs_create_request(ctx, inode, page, offset, bytes); | 
 | 631 | 		if (IS_ERR(new)) | 
 | 632 | 			return new; | 
 | 633 | 	} | 
 | 634 |  | 
 | 635 | 	/* We have a request for our page. | 
 | 636 | 	 * If the creds don't match, or the | 
 | 637 | 	 * page addresses don't match, | 
 | 638 | 	 * tell the caller to wait on the conflicting | 
 | 639 | 	 * request. | 
 | 640 | 	 */ | 
 | 641 | 	rqend = req->wb_offset + req->wb_bytes; | 
 | 642 | 	if (req->wb_context != ctx | 
 | 643 | 	    || req->wb_page != page | 
 | 644 | 	    || !nfs_dirty_request(req) | 
 | 645 | 	    || offset > rqend || end < req->wb_offset) { | 
| Trond Myklebust | acee478 | 2008-01-22 17:13:07 -0500 | [diff] [blame] | 646 | 		nfs_clear_page_tag_locked(req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | 		return ERR_PTR(-EBUSY); | 
 | 648 | 	} | 
 | 649 |  | 
 | 650 | 	/* Okay, the request matches. Update the region */ | 
 | 651 | 	if (offset < req->wb_offset) { | 
 | 652 | 		req->wb_offset = offset; | 
 | 653 | 		req->wb_pgbase = offset; | 
| Trond Myklebust | 61e930a | 2007-10-18 17:08:05 -0400 | [diff] [blame] | 654 | 		req->wb_bytes = max(end, rqend) - req->wb_offset; | 
 | 655 | 		goto zero_page; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | 	} | 
 | 657 |  | 
 | 658 | 	if (end > rqend) | 
 | 659 | 		req->wb_bytes = end - req->wb_offset; | 
 | 660 |  | 
 | 661 | 	return req; | 
| Trond Myklebust | 61e930a | 2007-10-18 17:08:05 -0400 | [diff] [blame] | 662 | zero_page: | 
 | 663 | 	/* If this page might potentially be marked as up to date, | 
 | 664 | 	 * then we need to zero any uninitalised data. */ | 
 | 665 | 	if (req->wb_pgbase == 0 && req->wb_bytes != PAGE_CACHE_SIZE | 
 | 666 | 			&& !PageUptodate(req->wb_page)) | 
| Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 667 | 		zero_user_segment(req->wb_page, req->wb_bytes, PAGE_CACHE_SIZE); | 
| Trond Myklebust | 61e930a | 2007-10-18 17:08:05 -0400 | [diff] [blame] | 668 | 	return req; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | } | 
 | 670 |  | 
 | 671 | int nfs_flush_incompatible(struct file *file, struct page *page) | 
 | 672 | { | 
| Trond Myklebust | cd3758e | 2007-08-10 17:44:32 -0400 | [diff] [blame] | 673 | 	struct nfs_open_context *ctx = nfs_file_open_context(file); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | 	struct nfs_page	*req; | 
| Trond Myklebust | 1a54533 | 2006-12-05 00:35:40 -0500 | [diff] [blame] | 675 | 	int do_flush, status; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | 	/* | 
 | 677 | 	 * Look for a request corresponding to this page. If there | 
 | 678 | 	 * is one, and it belongs to another file, we flush it out | 
 | 679 | 	 * before we try to copy anything into the page. Do this | 
 | 680 | 	 * due to the lack of an ACCESS-type call in NFSv2. | 
 | 681 | 	 * Also do the same if we find a request from an existing | 
 | 682 | 	 * dropped page. | 
 | 683 | 	 */ | 
| Trond Myklebust | 1a54533 | 2006-12-05 00:35:40 -0500 | [diff] [blame] | 684 | 	do { | 
 | 685 | 		req = nfs_page_find_request(page); | 
 | 686 | 		if (req == NULL) | 
 | 687 | 			return 0; | 
 | 688 | 		do_flush = req->wb_page != page || req->wb_context != ctx | 
| Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 689 | 			|| !nfs_dirty_request(req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | 		nfs_release_request(req); | 
| Trond Myklebust | 1a54533 | 2006-12-05 00:35:40 -0500 | [diff] [blame] | 691 | 		if (!do_flush) | 
 | 692 | 			return 0; | 
 | 693 | 		status = nfs_wb_page(page->mapping->host, page); | 
 | 694 | 	} while (status == 0); | 
 | 695 | 	return status; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | } | 
 | 697 |  | 
 | 698 | /* | 
| Trond Myklebust | 5d47a35 | 2008-02-07 17:24:07 -0500 | [diff] [blame] | 699 |  * If the page cache is marked as unsafe or invalid, then we can't rely on | 
 | 700 |  * the PageUptodate() flag. In this case, we will need to turn off | 
 | 701 |  * write optimisations that depend on the page contents being correct. | 
 | 702 |  */ | 
 | 703 | static int nfs_write_pageuptodate(struct page *page, struct inode *inode) | 
 | 704 | { | 
 | 705 | 	return PageUptodate(page) && | 
 | 706 | 		!(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA)); | 
 | 707 | } | 
 | 708 |  | 
 | 709 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 |  * Update and possibly write a cached page of an NFS file. | 
 | 711 |  * | 
 | 712 |  * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad | 
 | 713 |  * things with a page scheduled for an RPC call (e.g. invalidate it). | 
 | 714 |  */ | 
 | 715 | int nfs_updatepage(struct file *file, struct page *page, | 
 | 716 | 		unsigned int offset, unsigned int count) | 
 | 717 | { | 
| Trond Myklebust | cd3758e | 2007-08-10 17:44:32 -0400 | [diff] [blame] | 718 | 	struct nfs_open_context *ctx = nfs_file_open_context(file); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | 	struct inode	*inode = page->mapping->host; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | 	int		status = 0; | 
 | 721 |  | 
| Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 722 | 	nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); | 
 | 723 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | 	dprintk("NFS:      nfs_updatepage(%s/%s %d@%Ld)\n", | 
| Josef "Jeff" Sipek | 01cce93 | 2006-12-08 02:36:40 -0800 | [diff] [blame] | 725 | 		file->f_path.dentry->d_parent->d_name.name, | 
 | 726 | 		file->f_path.dentry->d_name.name, count, | 
| Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 727 | 		(long long)(page_offset(page) +offset)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | 	/* If we're not using byte range locks, and we know the page | 
| Trond Myklebust | 5d47a35 | 2008-02-07 17:24:07 -0500 | [diff] [blame] | 730 | 	 * is up to date, it may be more efficient to extend the write | 
 | 731 | 	 * to cover the entire page in order to avoid fragmentation | 
 | 732 | 	 * inefficiencies. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | 	 */ | 
| Trond Myklebust | 5d47a35 | 2008-02-07 17:24:07 -0500 | [diff] [blame] | 734 | 	if (nfs_write_pageuptodate(page, inode) && | 
 | 735 | 			inode->i_flock == NULL && | 
| Trond Myklebust | 4b5621f | 2008-02-25 15:56:29 -0800 | [diff] [blame] | 736 | 			!(file->f_flags & O_SYNC)) { | 
| Trond Myklebust | 49a70f2 | 2006-12-05 00:35:38 -0500 | [diff] [blame] | 737 | 		count = max(count + offset, nfs_page_length(page)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | 		offset = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | 	} | 
 | 740 |  | 
| Trond Myklebust | e21195a | 2006-12-05 00:35:39 -0500 | [diff] [blame] | 741 | 	status = nfs_writepage_setup(ctx, page, offset, count); | 
| Trond Myklebust | 03fa9e8 | 2008-06-05 16:02:35 -0400 | [diff] [blame] | 742 | 	if (status < 0) | 
 | 743 | 		nfs_set_pageerror(page); | 
 | 744 | 	else | 
 | 745 | 		__set_page_dirty_nobuffers(page); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 |         dprintk("NFS:      nfs_updatepage returns %d (isize %Ld)\n", | 
 | 748 | 			status, (long long)i_size_read(inode)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | 	return status; | 
 | 750 | } | 
 | 751 |  | 
 | 752 | static void nfs_writepage_release(struct nfs_page *req) | 
 | 753 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 |  | 
| Trond Myklebust | 44dd151 | 2007-05-19 11:58:03 -0400 | [diff] [blame] | 755 | 	if (PageError(req->wb_page)) { | 
 | 756 | 		nfs_end_page_writeback(req->wb_page); | 
 | 757 | 		nfs_inode_remove_request(req); | 
 | 758 | 	} else if (!nfs_reschedule_unstable_write(req)) { | 
 | 759 | 		/* Set the PG_uptodate flag */ | 
 | 760 | 		nfs_mark_uptodate(req->wb_page, req->wb_pgbase, req->wb_bytes); | 
| Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 761 | 		nfs_end_page_writeback(req->wb_page); | 
 | 762 | 		nfs_inode_remove_request(req); | 
 | 763 | 	} else | 
 | 764 | 		nfs_end_page_writeback(req->wb_page); | 
| Trond Myklebust | 9fd367f | 2007-06-17 15:10:24 -0400 | [diff] [blame] | 765 | 	nfs_clear_page_tag_locked(req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | } | 
 | 767 |  | 
| Trond Myklebust | 3ff7576 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 768 | static int flush_task_priority(int how) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | { | 
 | 770 | 	switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { | 
 | 771 | 		case FLUSH_HIGHPRI: | 
 | 772 | 			return RPC_PRIORITY_HIGH; | 
 | 773 | 		case FLUSH_LOWPRI: | 
 | 774 | 			return RPC_PRIORITY_LOW; | 
 | 775 | 	} | 
 | 776 | 	return RPC_PRIORITY_NORMAL; | 
 | 777 | } | 
 | 778 |  | 
 | 779 | /* | 
 | 780 |  * Set up the argument/result storage required for the RPC call. | 
 | 781 |  */ | 
| Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 782 | static int nfs_write_rpcsetup(struct nfs_page *req, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | 		struct nfs_write_data *data, | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 784 | 		const struct rpc_call_ops *call_ops, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | 		unsigned int count, unsigned int offset, | 
 | 786 | 		int how) | 
 | 787 | { | 
| Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 788 | 	struct inode *inode = req->wb_context->path.dentry->d_inode; | 
 | 789 | 	int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC; | 
| Trond Myklebust | 3ff7576 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 790 | 	int priority = flush_task_priority(how); | 
| Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 791 | 	struct rpc_task *task; | 
| Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 792 | 	struct rpc_message msg = { | 
 | 793 | 		.rpc_argp = &data->args, | 
 | 794 | 		.rpc_resp = &data->res, | 
 | 795 | 		.rpc_cred = req->wb_context->cred, | 
 | 796 | 	}; | 
| Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 797 | 	struct rpc_task_setup task_setup_data = { | 
 | 798 | 		.rpc_client = NFS_CLIENT(inode), | 
| Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 799 | 		.task = &data->task, | 
| Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 800 | 		.rpc_message = &msg, | 
| Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 801 | 		.callback_ops = call_ops, | 
 | 802 | 		.callback_data = data, | 
| Trond Myklebust | 101070c | 2008-02-19 20:04:23 -0500 | [diff] [blame] | 803 | 		.workqueue = nfsiod_workqueue, | 
| Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 804 | 		.flags = flags, | 
| Trond Myklebust | 3ff7576 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 805 | 		.priority = priority, | 
| Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 806 | 	}; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 |  | 
 | 808 | 	/* Set up the RPC argument and reply structs | 
 | 809 | 	 * NB: take care not to mess about with data->commit et al. */ | 
 | 810 |  | 
 | 811 | 	data->req = req; | 
| Trond Myklebust | 88be9f9 | 2007-06-05 10:42:27 -0400 | [diff] [blame] | 812 | 	data->inode = inode = req->wb_context->path.dentry->d_inode; | 
| Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 813 | 	data->cred = msg.rpc_cred; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 |  | 
 | 815 | 	data->args.fh     = NFS_FH(inode); | 
 | 816 | 	data->args.offset = req_offset(req) + offset; | 
 | 817 | 	data->args.pgbase = req->wb_pgbase + offset; | 
 | 818 | 	data->args.pages  = data->pagevec; | 
 | 819 | 	data->args.count  = count; | 
| Trond Myklebust | 383ba71 | 2008-02-19 20:04:20 -0500 | [diff] [blame] | 820 | 	data->args.context = get_nfs_open_context(req->wb_context); | 
| Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 821 | 	data->args.stable  = NFS_UNSTABLE; | 
 | 822 | 	if (how & FLUSH_STABLE) { | 
 | 823 | 		data->args.stable = NFS_DATA_SYNC; | 
 | 824 | 		if (!NFS_I(inode)->ncommit) | 
 | 825 | 			data->args.stable = NFS_FILE_SYNC; | 
 | 826 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 |  | 
 | 828 | 	data->res.fattr   = &data->fattr; | 
 | 829 | 	data->res.count   = count; | 
 | 830 | 	data->res.verf    = &data->verf; | 
| Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 831 | 	nfs_fattr_init(&data->fattr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 |  | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 833 | 	/* Set up the initial task struct.  */ | 
| Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 834 | 	NFS_PROTO(inode)->write_setup(data, &msg); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 |  | 
| Chuck Lever | a3f565b | 2007-01-31 12:14:01 -0500 | [diff] [blame] | 836 | 	dprintk("NFS: %5u initiated write call " | 
 | 837 | 		"(req %s/%Ld, %u bytes @ offset %Lu)\n", | 
| Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 838 | 		data->task.tk_pid, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | 		inode->i_sb->s_id, | 
 | 840 | 		(long long)NFS_FILEID(inode), | 
 | 841 | 		count, | 
 | 842 | 		(unsigned long long)data->args.offset); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 |  | 
| Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 844 | 	task = rpc_run_task(&task_setup_data); | 
| Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 845 | 	if (IS_ERR(task)) | 
 | 846 | 		return PTR_ERR(task); | 
 | 847 | 	rpc_put_task(task); | 
 | 848 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | } | 
 | 850 |  | 
| Fred | 6d884e8 | 2008-03-19 11:24:38 -0400 | [diff] [blame] | 851 | /* If a nfs_flush_* function fails, it should remove reqs from @head and | 
 | 852 |  * call this on each, which will prepare them to be retried on next | 
 | 853 |  * writeback using standard nfs. | 
 | 854 |  */ | 
 | 855 | static void nfs_redirty_request(struct nfs_page *req) | 
 | 856 | { | 
 | 857 | 	nfs_mark_request_dirty(req); | 
 | 858 | 	nfs_end_page_writeback(req->wb_page); | 
 | 859 | 	nfs_clear_page_tag_locked(req); | 
 | 860 | } | 
 | 861 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | /* | 
 | 863 |  * Generate multiple small requests to write out a single | 
 | 864 |  * contiguous dirty area on one page. | 
 | 865 |  */ | 
| Trond Myklebust | 8d5658c | 2007-04-10 09:26:35 -0400 | [diff] [blame] | 866 | static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | { | 
 | 868 | 	struct nfs_page *req = nfs_list_entry(head->next); | 
 | 869 | 	struct page *page = req->wb_page; | 
 | 870 | 	struct nfs_write_data *data; | 
| Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 871 | 	size_t wsize = NFS_SERVER(inode)->wsize, nbytes; | 
 | 872 | 	unsigned int offset; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | 	int requests = 0; | 
| Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 874 | 	int ret = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | 	LIST_HEAD(list); | 
 | 876 |  | 
 | 877 | 	nfs_list_remove_request(req); | 
 | 878 |  | 
| Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 879 | 	nbytes = count; | 
| Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 880 | 	do { | 
 | 881 | 		size_t len = min(nbytes, wsize); | 
 | 882 |  | 
| Trond Myklebust | 8d5658c | 2007-04-10 09:26:35 -0400 | [diff] [blame] | 883 | 		data = nfs_writedata_alloc(1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | 		if (!data) | 
 | 885 | 			goto out_bad; | 
 | 886 | 		list_add(&data->pages, &list); | 
 | 887 | 		requests++; | 
| Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 888 | 		nbytes -= len; | 
 | 889 | 	} while (nbytes != 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | 	atomic_set(&req->wb_complete, requests); | 
 | 891 |  | 
 | 892 | 	ClearPageError(page); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | 	offset = 0; | 
| Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 894 | 	nbytes = count; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | 	do { | 
| Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 896 | 		int ret2; | 
 | 897 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | 		data = list_entry(list.next, struct nfs_write_data, pages); | 
 | 899 | 		list_del_init(&data->pages); | 
 | 900 |  | 
 | 901 | 		data->pagevec[0] = page; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 |  | 
| Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 903 | 		if (nbytes < wsize) | 
 | 904 | 			wsize = nbytes; | 
| Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 905 | 		ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops, | 
| Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 906 | 				   wsize, offset, how); | 
| Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 907 | 		if (ret == 0) | 
 | 908 | 			ret = ret2; | 
| Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 909 | 		offset += wsize; | 
 | 910 | 		nbytes -= wsize; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | 	} while (nbytes != 0); | 
 | 912 |  | 
| Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 913 | 	return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 |  | 
 | 915 | out_bad: | 
 | 916 | 	while (!list_empty(&list)) { | 
 | 917 | 		data = list_entry(list.next, struct nfs_write_data, pages); | 
 | 918 | 		list_del(&data->pages); | 
| Trond Myklebust | 8aca67f | 2006-11-13 16:23:44 -0500 | [diff] [blame] | 919 | 		nfs_writedata_release(data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | 	} | 
| Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 921 | 	nfs_redirty_request(req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | 	return -ENOMEM; | 
 | 923 | } | 
 | 924 |  | 
 | 925 | /* | 
 | 926 |  * Create an RPC task for the given write request and kick it. | 
 | 927 |  * The page must have been locked by the caller. | 
 | 928 |  * | 
 | 929 |  * It may happen that the page we're passed is not marked dirty. | 
 | 930 |  * This is the case if nfs_updatepage detects a conflicting request | 
 | 931 |  * that has been written but not committed. | 
 | 932 |  */ | 
| Trond Myklebust | 8d5658c | 2007-04-10 09:26:35 -0400 | [diff] [blame] | 933 | static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | { | 
 | 935 | 	struct nfs_page		*req; | 
 | 936 | 	struct page		**pages; | 
 | 937 | 	struct nfs_write_data	*data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 |  | 
| Trond Myklebust | 8d5658c | 2007-04-10 09:26:35 -0400 | [diff] [blame] | 939 | 	data = nfs_writedata_alloc(npages); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | 	if (!data) | 
 | 941 | 		goto out_bad; | 
 | 942 |  | 
 | 943 | 	pages = data->pagevec; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | 	while (!list_empty(head)) { | 
 | 945 | 		req = nfs_list_entry(head->next); | 
 | 946 | 		nfs_list_remove_request(req); | 
 | 947 | 		nfs_list_add_request(req, &data->pages); | 
 | 948 | 		ClearPageError(req->wb_page); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | 		*pages++ = req->wb_page; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | 	} | 
 | 951 | 	req = nfs_list_entry(data->pages.next); | 
 | 952 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | 	/* Set up the argument struct */ | 
| Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 954 | 	return nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 |  out_bad: | 
 | 956 | 	while (!list_empty(head)) { | 
| Trond Myklebust | 10afec9 | 2007-05-14 17:16:04 -0400 | [diff] [blame] | 957 | 		req = nfs_list_entry(head->next); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | 		nfs_list_remove_request(req); | 
| Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 959 | 		nfs_redirty_request(req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | 	} | 
 | 961 | 	return -ENOMEM; | 
 | 962 | } | 
 | 963 |  | 
| Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 964 | static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, | 
 | 965 | 				  struct inode *inode, int ioflags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | { | 
| Chuck Lever | bf4285e | 2007-12-20 14:54:57 -0500 | [diff] [blame] | 967 | 	size_t wsize = NFS_SERVER(inode)->wsize; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 |  | 
| Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 969 | 	if (wsize < PAGE_CACHE_SIZE) | 
| Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 970 | 		nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags); | 
| Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 971 | 	else | 
| Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 972 | 		nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | } | 
 | 974 |  | 
 | 975 | /* | 
 | 976 |  * Handle a write reply that flushed part of a page. | 
 | 977 |  */ | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 978 | static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | { | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 980 | 	struct nfs_write_data	*data = calldata; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | 	struct nfs_page		*req = data->req; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 |  | 
 | 983 | 	dprintk("NFS: write (%s/%Ld %d@%Ld)", | 
| Trond Myklebust | 88be9f9 | 2007-06-05 10:42:27 -0400 | [diff] [blame] | 984 | 		req->wb_context->path.dentry->d_inode->i_sb->s_id, | 
 | 985 | 		(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | 		req->wb_bytes, | 
 | 987 | 		(long long)req_offset(req)); | 
 | 988 |  | 
| Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 989 | 	nfs_writeback_done(task, data); | 
 | 990 | } | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 991 |  | 
| Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 992 | static void nfs_writeback_release_partial(void *calldata) | 
 | 993 | { | 
 | 994 | 	struct nfs_write_data	*data = calldata; | 
 | 995 | 	struct nfs_page		*req = data->req; | 
 | 996 | 	struct page		*page = req->wb_page; | 
 | 997 | 	int status = data->task.tk_status; | 
 | 998 |  | 
 | 999 | 	if (status < 0) { | 
| Trond Myklebust | a301b77 | 2007-02-06 11:07:15 -0800 | [diff] [blame] | 1000 | 		nfs_set_pageerror(page); | 
| Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1001 | 		nfs_context_set_write_error(req->wb_context, status); | 
 | 1002 | 		dprintk(", error = %d\n", status); | 
| Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1003 | 		goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | 	} | 
 | 1005 |  | 
| Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1006 | 	if (nfs_write_need_commit(data)) { | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1007 | 		struct inode *inode = page->mapping->host; | 
| Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1008 |  | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1009 | 		spin_lock(&inode->i_lock); | 
| Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1010 | 		if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) { | 
 | 1011 | 			/* Do nothing we need to resend the writes */ | 
 | 1012 | 		} else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) { | 
 | 1013 | 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); | 
 | 1014 | 			dprintk(" defer commit\n"); | 
 | 1015 | 		} else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) { | 
 | 1016 | 			set_bit(PG_NEED_RESCHED, &req->wb_flags); | 
 | 1017 | 			clear_bit(PG_NEED_COMMIT, &req->wb_flags); | 
 | 1018 | 			dprintk(" server reboot detected\n"); | 
 | 1019 | 		} | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1020 | 		spin_unlock(&inode->i_lock); | 
| Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1021 | 	} else | 
 | 1022 | 		dprintk(" OK\n"); | 
 | 1023 |  | 
 | 1024 | out: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | 	if (atomic_dec_and_test(&req->wb_complete)) | 
 | 1026 | 		nfs_writepage_release(req); | 
| Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1027 | 	nfs_writedata_release(calldata); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | } | 
 | 1029 |  | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1030 | static const struct rpc_call_ops nfs_write_partial_ops = { | 
 | 1031 | 	.rpc_call_done = nfs_writeback_done_partial, | 
| Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1032 | 	.rpc_release = nfs_writeback_release_partial, | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1033 | }; | 
 | 1034 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | /* | 
 | 1036 |  * Handle a write reply that flushes a whole page. | 
 | 1037 |  * | 
 | 1038 |  * FIXME: There is an inherent race with invalidate_inode_pages and | 
 | 1039 |  *	  writebacks since the page->count is kept > 1 for as long | 
 | 1040 |  *	  as the page has a write request pending. | 
 | 1041 |  */ | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1042 | static void nfs_writeback_done_full(struct rpc_task *task, void *calldata) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | { | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1044 | 	struct nfs_write_data	*data = calldata; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 |  | 
| Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1046 | 	nfs_writeback_done(task, data); | 
 | 1047 | } | 
 | 1048 |  | 
 | 1049 | static void nfs_writeback_release_full(void *calldata) | 
 | 1050 | { | 
 | 1051 | 	struct nfs_write_data	*data = calldata; | 
 | 1052 | 	int status = data->task.tk_status; | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1053 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | 	/* Update attributes as result of writeback. */ | 
 | 1055 | 	while (!list_empty(&data->pages)) { | 
| Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1056 | 		struct nfs_page *req = nfs_list_entry(data->pages.next); | 
 | 1057 | 		struct page *page = req->wb_page; | 
 | 1058 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | 		nfs_list_remove_request(req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 |  | 
 | 1061 | 		dprintk("NFS: write (%s/%Ld %d@%Ld)", | 
| Trond Myklebust | 88be9f9 | 2007-06-05 10:42:27 -0400 | [diff] [blame] | 1062 | 			req->wb_context->path.dentry->d_inode->i_sb->s_id, | 
 | 1063 | 			(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | 			req->wb_bytes, | 
 | 1065 | 			(long long)req_offset(req)); | 
 | 1066 |  | 
| Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1067 | 		if (status < 0) { | 
| Trond Myklebust | a301b77 | 2007-02-06 11:07:15 -0800 | [diff] [blame] | 1068 | 			nfs_set_pageerror(page); | 
| Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1069 | 			nfs_context_set_write_error(req->wb_context, status); | 
 | 1070 | 			dprintk(", error = %d\n", status); | 
| Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1071 | 			goto remove_request; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 |  | 
| Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1074 | 		if (nfs_write_need_commit(data)) { | 
 | 1075 | 			memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); | 
 | 1076 | 			nfs_mark_request_commit(req); | 
 | 1077 | 			nfs_end_page_writeback(page); | 
 | 1078 | 			dprintk(" marked for commit\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | 			goto next; | 
 | 1080 | 		} | 
| Trond Myklebust | 44dd151 | 2007-05-19 11:58:03 -0400 | [diff] [blame] | 1081 | 		/* Set the PG_uptodate flag? */ | 
 | 1082 | 		nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes); | 
| Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1083 | 		dprintk(" OK\n"); | 
 | 1084 | remove_request: | 
 | 1085 | 		nfs_end_page_writeback(page); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | 		nfs_inode_remove_request(req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | 	next: | 
| Trond Myklebust | 9fd367f | 2007-06-17 15:10:24 -0400 | [diff] [blame] | 1088 | 		nfs_clear_page_tag_locked(req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | 	} | 
| Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1090 | 	nfs_writedata_release(calldata); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | } | 
 | 1092 |  | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1093 | static const struct rpc_call_ops nfs_write_full_ops = { | 
 | 1094 | 	.rpc_call_done = nfs_writeback_done_full, | 
| Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1095 | 	.rpc_release = nfs_writeback_release_full, | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1096 | }; | 
 | 1097 |  | 
 | 1098 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | /* | 
 | 1100 |  * This function is called when the WRITE call is complete. | 
 | 1101 |  */ | 
| Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 1102 | int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | 	struct nfs_writeargs	*argp = &data->args; | 
 | 1105 | 	struct nfs_writeres	*resp = &data->res; | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1106 | 	int status; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 |  | 
| Chuck Lever | a3f565b | 2007-01-31 12:14:01 -0500 | [diff] [blame] | 1108 | 	dprintk("NFS: %5u nfs_writeback_done (status %d)\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | 		task->tk_pid, task->tk_status); | 
 | 1110 |  | 
| Chuck Lever | f551e44 | 2006-09-20 14:33:04 -0400 | [diff] [blame] | 1111 | 	/* | 
 | 1112 | 	 * ->write_done will attempt to use post-op attributes to detect | 
 | 1113 | 	 * conflicting writes by other clients.  A strict interpretation | 
 | 1114 | 	 * of close-to-open would allow us to continue caching even if | 
 | 1115 | 	 * another writer had changed the file, but some applications | 
 | 1116 | 	 * depend on tighter cache coherency when writing. | 
 | 1117 | 	 */ | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1118 | 	status = NFS_PROTO(data->inode)->write_done(task, data); | 
 | 1119 | 	if (status != 0) | 
 | 1120 | 		return status; | 
| Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 1121 | 	nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count); | 
 | 1122 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) | 
 | 1124 | 	if (resp->verf->committed < argp->stable && task->tk_status >= 0) { | 
 | 1125 | 		/* We tried a write call, but the server did not | 
 | 1126 | 		 * commit data to stable storage even though we | 
 | 1127 | 		 * requested it. | 
 | 1128 | 		 * Note: There is a known bug in Tru64 < 5.0 in which | 
 | 1129 | 		 *	 the server reports NFS_DATA_SYNC, but performs | 
 | 1130 | 		 *	 NFS_FILE_SYNC. We therefore implement this checking | 
 | 1131 | 		 *	 as a dprintk() in order to avoid filling syslog. | 
 | 1132 | 		 */ | 
 | 1133 | 		static unsigned long    complain; | 
 | 1134 |  | 
 | 1135 | 		if (time_before(complain, jiffies)) { | 
 | 1136 | 			dprintk("NFS: faulty NFS server %s:" | 
 | 1137 | 				" (committed = %d) != (stable = %d)\n", | 
| David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1138 | 				NFS_SERVER(data->inode)->nfs_client->cl_hostname, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | 				resp->verf->committed, argp->stable); | 
 | 1140 | 			complain = jiffies + 300 * HZ; | 
 | 1141 | 		} | 
 | 1142 | 	} | 
 | 1143 | #endif | 
 | 1144 | 	/* Is this a short write? */ | 
 | 1145 | 	if (task->tk_status >= 0 && resp->count < argp->count) { | 
 | 1146 | 		static unsigned long    complain; | 
 | 1147 |  | 
| Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 1148 | 		nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE); | 
 | 1149 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | 		/* Has the server at least made some progress? */ | 
 | 1151 | 		if (resp->count != 0) { | 
 | 1152 | 			/* Was this an NFSv2 write or an NFSv3 stable write? */ | 
 | 1153 | 			if (resp->verf->committed != NFS_UNSTABLE) { | 
 | 1154 | 				/* Resend from where the server left off */ | 
 | 1155 | 				argp->offset += resp->count; | 
 | 1156 | 				argp->pgbase += resp->count; | 
 | 1157 | 				argp->count -= resp->count; | 
 | 1158 | 			} else { | 
 | 1159 | 				/* Resend as a stable write in order to avoid | 
 | 1160 | 				 * headaches in the case of a server crash. | 
 | 1161 | 				 */ | 
 | 1162 | 				argp->stable = NFS_FILE_SYNC; | 
 | 1163 | 			} | 
 | 1164 | 			rpc_restart_call(task); | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1165 | 			return -EAGAIN; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | 		} | 
 | 1167 | 		if (time_before(complain, jiffies)) { | 
 | 1168 | 			printk(KERN_WARNING | 
 | 1169 | 			       "NFS: Server wrote zero bytes, expected %u.\n", | 
 | 1170 | 					argp->count); | 
 | 1171 | 			complain = jiffies + 300 * HZ; | 
 | 1172 | 		} | 
 | 1173 | 		/* Can't do anything about it except throw an error. */ | 
 | 1174 | 		task->tk_status = -EIO; | 
 | 1175 | 	} | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1176 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | } | 
 | 1178 |  | 
 | 1179 |  | 
 | 1180 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) | 
| Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1181 | void nfs_commitdata_release(void *data) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | { | 
| Trond Myklebust | 383ba71 | 2008-02-19 20:04:20 -0500 | [diff] [blame] | 1183 | 	struct nfs_write_data *wdata = data; | 
 | 1184 |  | 
 | 1185 | 	put_nfs_open_context(wdata->args.context); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | 	nfs_commit_free(wdata); | 
 | 1187 | } | 
 | 1188 |  | 
 | 1189 | /* | 
 | 1190 |  * Set up the argument/result storage required for the RPC call. | 
 | 1191 |  */ | 
| Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 1192 | static int nfs_commit_rpcsetup(struct list_head *head, | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1193 | 		struct nfs_write_data *data, | 
 | 1194 | 		int how) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | { | 
| Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1196 | 	struct nfs_page *first = nfs_list_entry(head->next); | 
 | 1197 | 	struct inode *inode = first->wb_context->path.dentry->d_inode; | 
 | 1198 | 	int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC; | 
| Trond Myklebust | 3ff7576 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1199 | 	int priority = flush_task_priority(how); | 
| Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 1200 | 	struct rpc_task *task; | 
| Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1201 | 	struct rpc_message msg = { | 
 | 1202 | 		.rpc_argp = &data->args, | 
 | 1203 | 		.rpc_resp = &data->res, | 
 | 1204 | 		.rpc_cred = first->wb_context->cred, | 
 | 1205 | 	}; | 
| Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1206 | 	struct rpc_task_setup task_setup_data = { | 
| Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 1207 | 		.task = &data->task, | 
| Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1208 | 		.rpc_client = NFS_CLIENT(inode), | 
| Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1209 | 		.rpc_message = &msg, | 
| Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1210 | 		.callback_ops = &nfs_commit_ops, | 
 | 1211 | 		.callback_data = data, | 
| Trond Myklebust | 101070c | 2008-02-19 20:04:23 -0500 | [diff] [blame] | 1212 | 		.workqueue = nfsiod_workqueue, | 
| Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1213 | 		.flags = flags, | 
| Trond Myklebust | 3ff7576 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1214 | 		.priority = priority, | 
| Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1215 | 	}; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 |  | 
 | 1217 | 	/* Set up the RPC argument and reply structs | 
 | 1218 | 	 * NB: take care not to mess about with data->commit et al. */ | 
 | 1219 |  | 
 | 1220 | 	list_splice_init(head, &data->pages); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | 	data->inode	  = inode; | 
| Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1223 | 	data->cred	  = msg.rpc_cred; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 |  | 
 | 1225 | 	data->args.fh     = NFS_FH(data->inode); | 
| Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 1226 | 	/* Note: we always request a commit of the entire inode */ | 
 | 1227 | 	data->args.offset = 0; | 
 | 1228 | 	data->args.count  = 0; | 
| Trond Myklebust | 383ba71 | 2008-02-19 20:04:20 -0500 | [diff] [blame] | 1229 | 	data->args.context = get_nfs_open_context(first->wb_context); | 
| Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 1230 | 	data->res.count   = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | 	data->res.fattr   = &data->fattr; | 
 | 1232 | 	data->res.verf    = &data->verf; | 
| Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 1233 | 	nfs_fattr_init(&data->fattr); | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1234 |  | 
 | 1235 | 	/* Set up the initial task struct.  */ | 
| Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1236 | 	NFS_PROTO(inode)->commit_setup(data, &msg); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 |  | 
| Chuck Lever | a3f565b | 2007-01-31 12:14:01 -0500 | [diff] [blame] | 1238 | 	dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); | 
| Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1239 |  | 
| Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 1240 | 	task = rpc_run_task(&task_setup_data); | 
| Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 1241 | 	if (IS_ERR(task)) | 
 | 1242 | 		return PTR_ERR(task); | 
 | 1243 | 	rpc_put_task(task); | 
 | 1244 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | } | 
 | 1246 |  | 
 | 1247 | /* | 
 | 1248 |  * Commit dirty pages | 
 | 1249 |  */ | 
 | 1250 | static int | 
| Chuck Lever | 40859d7 | 2005-11-30 18:09:02 -0500 | [diff] [blame] | 1251 | nfs_commit_list(struct inode *inode, struct list_head *head, int how) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | { | 
 | 1253 | 	struct nfs_write_data	*data; | 
 | 1254 | 	struct nfs_page         *req; | 
 | 1255 |  | 
| Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1256 | 	data = nfs_commitdata_alloc(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 |  | 
 | 1258 | 	if (!data) | 
 | 1259 | 		goto out_bad; | 
 | 1260 |  | 
 | 1261 | 	/* Set up the argument struct */ | 
| Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 1262 | 	return nfs_commit_rpcsetup(head, data, how); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 |  out_bad: | 
 | 1264 | 	while (!list_empty(head)) { | 
 | 1265 | 		req = nfs_list_entry(head->next); | 
 | 1266 | 		nfs_list_remove_request(req); | 
 | 1267 | 		nfs_mark_request_commit(req); | 
| Trond Myklebust | 83715ad | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 1268 | 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); | 
| Peter Zijlstra | c9e51e4 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 1269 | 		dec_bdi_stat(req->wb_page->mapping->backing_dev_info, | 
 | 1270 | 				BDI_RECLAIMABLE); | 
| Trond Myklebust | 9fd367f | 2007-06-17 15:10:24 -0400 | [diff] [blame] | 1271 | 		nfs_clear_page_tag_locked(req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | 	} | 
 | 1273 | 	return -ENOMEM; | 
 | 1274 | } | 
 | 1275 |  | 
 | 1276 | /* | 
 | 1277 |  * COMMIT call returned | 
 | 1278 |  */ | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1279 | static void nfs_commit_done(struct rpc_task *task, void *calldata) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | { | 
| Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 1281 | 	struct nfs_write_data	*data = calldata; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 |  | 
| Chuck Lever | a3f565b | 2007-01-31 12:14:01 -0500 | [diff] [blame] | 1283 |         dprintk("NFS: %5u nfs_commit_done (status %d)\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 |                                 task->tk_pid, task->tk_status); | 
 | 1285 |  | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1286 | 	/* Call the NFS version-specific code */ | 
 | 1287 | 	if (NFS_PROTO(data->inode)->commit_done(task, data) != 0) | 
 | 1288 | 		return; | 
| Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1289 | } | 
 | 1290 |  | 
 | 1291 | static void nfs_commit_release(void *calldata) | 
 | 1292 | { | 
 | 1293 | 	struct nfs_write_data	*data = calldata; | 
 | 1294 | 	struct nfs_page		*req; | 
 | 1295 | 	int status = data->task.tk_status; | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1296 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | 	while (!list_empty(&data->pages)) { | 
 | 1298 | 		req = nfs_list_entry(data->pages.next); | 
 | 1299 | 		nfs_list_remove_request(req); | 
| Trond Myklebust | 612c938 | 2007-04-20 16:12:45 -0400 | [diff] [blame] | 1300 | 		clear_bit(PG_NEED_COMMIT, &(req)->wb_flags); | 
| Christoph Lameter | fd39fc8 | 2006-06-30 01:55:40 -0700 | [diff] [blame] | 1301 | 		dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); | 
| Peter Zijlstra | c9e51e4 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 1302 | 		dec_bdi_stat(req->wb_page->mapping->backing_dev_info, | 
 | 1303 | 				BDI_RECLAIMABLE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 |  | 
 | 1305 | 		dprintk("NFS: commit (%s/%Ld %d@%Ld)", | 
| Trond Myklebust | 88be9f9 | 2007-06-05 10:42:27 -0400 | [diff] [blame] | 1306 | 			req->wb_context->path.dentry->d_inode->i_sb->s_id, | 
 | 1307 | 			(long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | 			req->wb_bytes, | 
 | 1309 | 			(long long)req_offset(req)); | 
| Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1310 | 		if (status < 0) { | 
 | 1311 | 			nfs_context_set_write_error(req->wb_context, status); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | 			nfs_inode_remove_request(req); | 
| Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1313 | 			dprintk(", error = %d\n", status); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | 			goto next; | 
 | 1315 | 		} | 
 | 1316 |  | 
 | 1317 | 		/* Okay, COMMIT succeeded, apparently. Check the verifier | 
 | 1318 | 		 * returned by the server against all stored verfs. */ | 
 | 1319 | 		if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) { | 
 | 1320 | 			/* We have a match */ | 
| Trond Myklebust | 44dd151 | 2007-05-19 11:58:03 -0400 | [diff] [blame] | 1321 | 			/* Set the PG_uptodate flag */ | 
 | 1322 | 			nfs_mark_uptodate(req->wb_page, req->wb_pgbase, | 
 | 1323 | 					req->wb_bytes); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | 			nfs_inode_remove_request(req); | 
 | 1325 | 			dprintk(" OK\n"); | 
 | 1326 | 			goto next; | 
 | 1327 | 		} | 
 | 1328 | 		/* We have a mismatch. Write the page again */ | 
 | 1329 | 		dprintk(" mismatch\n"); | 
| Fred | 6d884e8 | 2008-03-19 11:24:38 -0400 | [diff] [blame] | 1330 | 		nfs_mark_request_dirty(req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | 	next: | 
| Trond Myklebust | 9fd367f | 2007-06-17 15:10:24 -0400 | [diff] [blame] | 1332 | 		nfs_clear_page_tag_locked(req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | 	} | 
| Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1334 | 	nfs_commitdata_release(calldata); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | } | 
| Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1336 |  | 
 | 1337 | static const struct rpc_call_ops nfs_commit_ops = { | 
 | 1338 | 	.rpc_call_done = nfs_commit_done, | 
 | 1339 | 	.rpc_release = nfs_commit_release, | 
 | 1340 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 |  | 
| Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 1342 | int nfs_commit_inode(struct inode *inode, int how) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | 	LIST_HEAD(head); | 
| Trond Myklebust | 7d46a49 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 1345 | 	int res; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 |  | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1347 | 	spin_lock(&inode->i_lock); | 
| Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 1348 | 	res = nfs_scan_commit(inode, &head, 0, 0); | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1349 | 	spin_unlock(&inode->i_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | 	if (res) { | 
| Trond Myklebust | 7d46a49 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 1351 | 		int error = nfs_commit_list(inode, &head, how); | 
| Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 1352 | 		if (error < 0) | 
 | 1353 | 			return error; | 
 | 1354 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | 	return res; | 
 | 1356 | } | 
| Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 1357 | #else | 
 | 1358 | static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how) | 
 | 1359 | { | 
 | 1360 | 	return 0; | 
 | 1361 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | #endif | 
 | 1363 |  | 
| Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1364 | long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | { | 
| Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1366 | 	struct inode *inode = mapping->host; | 
| Trond Myklebust | ca52fec | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 1367 | 	pgoff_t idx_start, idx_end; | 
| Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1368 | 	unsigned int npages = 0; | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1369 | 	LIST_HEAD(head); | 
| Trond Myklebust | 70b9ecb | 2006-01-03 09:55:34 +0100 | [diff] [blame] | 1370 | 	int nocommit = how & FLUSH_NOCOMMIT; | 
| Trond Myklebust | 3f44254 | 2006-09-17 14:46:44 -0400 | [diff] [blame] | 1371 | 	long pages, ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 |  | 
| Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1373 | 	/* FIXME */ | 
 | 1374 | 	if (wbc->range_cyclic) | 
 | 1375 | 		idx_start = 0; | 
 | 1376 | 	else { | 
 | 1377 | 		idx_start = wbc->range_start >> PAGE_CACHE_SHIFT; | 
 | 1378 | 		idx_end = wbc->range_end >> PAGE_CACHE_SHIFT; | 
 | 1379 | 		if (idx_end > idx_start) { | 
| Trond Myklebust | ca52fec | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 1380 | 			pgoff_t l_npages = 1 + idx_end - idx_start; | 
| Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1381 | 			npages = l_npages; | 
 | 1382 | 			if (sizeof(npages) != sizeof(l_npages) && | 
| Trond Myklebust | ca52fec | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 1383 | 					(pgoff_t)npages != l_npages) | 
| Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1384 | 				npages = 0; | 
 | 1385 | 		} | 
 | 1386 | 	} | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1387 | 	how &= ~FLUSH_NOCOMMIT; | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1388 | 	spin_lock(&inode->i_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | 	do { | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1390 | 		ret = nfs_wait_on_requests_locked(inode, idx_start, npages); | 
 | 1391 | 		if (ret != 0) | 
| Trond Myklebust | 70b9ecb | 2006-01-03 09:55:34 +0100 | [diff] [blame] | 1392 | 			continue; | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1393 | 		if (nocommit) | 
 | 1394 | 			break; | 
| Trond Myklebust | d2ccddf | 2006-05-31 01:13:38 -0400 | [diff] [blame] | 1395 | 		pages = nfs_scan_commit(inode, &head, idx_start, npages); | 
| Trond Myklebust | 724c439 | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 1396 | 		if (pages == 0) | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1397 | 			break; | 
| Trond Myklebust | d2ccddf | 2006-05-31 01:13:38 -0400 | [diff] [blame] | 1398 | 		if (how & FLUSH_INVALIDATE) { | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1399 | 			spin_unlock(&inode->i_lock); | 
| Trond Myklebust | 83715ad | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 1400 | 			nfs_cancel_commit_list(&head); | 
| Trond Myklebust | e8e058e | 2006-11-15 17:31:56 -0500 | [diff] [blame] | 1401 | 			ret = pages; | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1402 | 			spin_lock(&inode->i_lock); | 
| Trond Myklebust | d2ccddf | 2006-05-31 01:13:38 -0400 | [diff] [blame] | 1403 | 			continue; | 
 | 1404 | 		} | 
 | 1405 | 		pages += nfs_scan_commit(inode, &head, 0, 0); | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1406 | 		spin_unlock(&inode->i_lock); | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1407 | 		ret = nfs_commit_list(inode, &head, how); | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1408 | 		spin_lock(&inode->i_lock); | 
 | 1409 |  | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1410 | 	} while (ret >= 0); | 
| Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1411 | 	spin_unlock(&inode->i_lock); | 
| Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1412 | 	return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | } | 
 | 1414 |  | 
| Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1415 | static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how) | 
| Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1416 | { | 
| Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1417 | 	int ret; | 
 | 1418 |  | 
| Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1419 | 	ret = nfs_writepages(mapping, wbc); | 
| Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 1420 | 	if (ret < 0) | 
 | 1421 | 		goto out; | 
| Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1422 | 	ret = nfs_sync_mapping_wait(mapping, wbc, how); | 
| Trond Myklebust | ed90ef5 | 2007-07-20 13:13:28 -0400 | [diff] [blame] | 1423 | 	if (ret < 0) | 
 | 1424 | 		goto out; | 
 | 1425 | 	return 0; | 
| Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 1426 | out: | 
| Trond Myklebust | e507d9e | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 1427 | 	__mark_inode_dirty(mapping->host, I_DIRTY_PAGES); | 
| Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1428 | 	return ret; | 
 | 1429 | } | 
 | 1430 |  | 
| Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1431 | /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */ | 
 | 1432 | static int nfs_write_mapping(struct address_space *mapping, int how) | 
 | 1433 | { | 
 | 1434 | 	struct writeback_control wbc = { | 
 | 1435 | 		.bdi = mapping->backing_dev_info, | 
 | 1436 | 		.sync_mode = WB_SYNC_NONE, | 
 | 1437 | 		.nr_to_write = LONG_MAX, | 
 | 1438 | 		.for_writepages = 1, | 
 | 1439 | 		.range_cyclic = 1, | 
 | 1440 | 	}; | 
 | 1441 | 	int ret; | 
 | 1442 |  | 
 | 1443 | 	ret = __nfs_write_mapping(mapping, &wbc, how); | 
 | 1444 | 	if (ret < 0) | 
 | 1445 | 		return ret; | 
 | 1446 | 	wbc.sync_mode = WB_SYNC_ALL; | 
 | 1447 | 	return __nfs_write_mapping(mapping, &wbc, how); | 
 | 1448 | } | 
 | 1449 |  | 
| Trond Myklebust | ed90ef5 | 2007-07-20 13:13:28 -0400 | [diff] [blame] | 1450 | /* | 
 | 1451 |  * flush the inode to disk. | 
 | 1452 |  */ | 
 | 1453 | int nfs_wb_all(struct inode *inode) | 
| Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1454 | { | 
| Trond Myklebust | ed90ef5 | 2007-07-20 13:13:28 -0400 | [diff] [blame] | 1455 | 	return nfs_write_mapping(inode->i_mapping, 0); | 
 | 1456 | } | 
| Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1457 |  | 
| Trond Myklebust | ed90ef5 | 2007-07-20 13:13:28 -0400 | [diff] [blame] | 1458 | int nfs_wb_nocommit(struct inode *inode) | 
 | 1459 | { | 
 | 1460 | 	return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT); | 
| Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1461 | } | 
 | 1462 |  | 
| Trond Myklebust | 1b3b4a1 | 2007-08-28 10:29:36 -0400 | [diff] [blame] | 1463 | int nfs_wb_page_cancel(struct inode *inode, struct page *page) | 
 | 1464 | { | 
 | 1465 | 	struct nfs_page *req; | 
 | 1466 | 	loff_t range_start = page_offset(page); | 
 | 1467 | 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); | 
 | 1468 | 	struct writeback_control wbc = { | 
 | 1469 | 		.bdi = page->mapping->backing_dev_info, | 
 | 1470 | 		.sync_mode = WB_SYNC_ALL, | 
 | 1471 | 		.nr_to_write = LONG_MAX, | 
 | 1472 | 		.range_start = range_start, | 
 | 1473 | 		.range_end = range_end, | 
 | 1474 | 	}; | 
 | 1475 | 	int ret = 0; | 
 | 1476 |  | 
 | 1477 | 	BUG_ON(!PageLocked(page)); | 
 | 1478 | 	for (;;) { | 
 | 1479 | 		req = nfs_page_find_request(page); | 
 | 1480 | 		if (req == NULL) | 
 | 1481 | 			goto out; | 
 | 1482 | 		if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) { | 
 | 1483 | 			nfs_release_request(req); | 
 | 1484 | 			break; | 
 | 1485 | 		} | 
 | 1486 | 		if (nfs_lock_request_dontget(req)) { | 
 | 1487 | 			nfs_inode_remove_request(req); | 
 | 1488 | 			/* | 
 | 1489 | 			 * In case nfs_inode_remove_request has marked the | 
 | 1490 | 			 * page as being dirty | 
 | 1491 | 			 */ | 
 | 1492 | 			cancel_dirty_page(page, PAGE_CACHE_SIZE); | 
 | 1493 | 			nfs_unlock_request(req); | 
 | 1494 | 			break; | 
 | 1495 | 		} | 
 | 1496 | 		ret = nfs_wait_on_request(req); | 
 | 1497 | 		if (ret < 0) | 
 | 1498 | 			goto out; | 
 | 1499 | 	} | 
 | 1500 | 	if (!PagePrivate(page)) | 
 | 1501 | 		return 0; | 
 | 1502 | 	ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE); | 
 | 1503 | out: | 
 | 1504 | 	return ret; | 
 | 1505 | } | 
 | 1506 |  | 
| Adrian Bunk | 5334eb1 | 2007-11-21 15:04:31 -0800 | [diff] [blame] | 1507 | static int nfs_wb_page_priority(struct inode *inode, struct page *page, | 
 | 1508 | 				int how) | 
| Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1509 | { | 
 | 1510 | 	loff_t range_start = page_offset(page); | 
 | 1511 | 	loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); | 
| Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 1512 | 	struct writeback_control wbc = { | 
 | 1513 | 		.bdi = page->mapping->backing_dev_info, | 
 | 1514 | 		.sync_mode = WB_SYNC_ALL, | 
 | 1515 | 		.nr_to_write = LONG_MAX, | 
 | 1516 | 		.range_start = range_start, | 
 | 1517 | 		.range_end = range_end, | 
 | 1518 | 	}; | 
 | 1519 | 	int ret; | 
| Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1520 |  | 
| Trond Myklebust | 73e3302 | 2008-04-11 16:03:54 -0400 | [diff] [blame] | 1521 | 	do { | 
 | 1522 | 		if (clear_page_dirty_for_io(page)) { | 
 | 1523 | 			ret = nfs_writepage_locked(page, &wbc); | 
 | 1524 | 			if (ret < 0) | 
 | 1525 | 				goto out_error; | 
 | 1526 | 		} else if (!PagePrivate(page)) | 
 | 1527 | 			break; | 
 | 1528 | 		ret = nfs_sync_mapping_wait(page->mapping, &wbc, how); | 
| Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 1529 | 		if (ret < 0) | 
| Trond Myklebust | 73e3302 | 2008-04-11 16:03:54 -0400 | [diff] [blame] | 1530 | 			goto out_error; | 
 | 1531 | 	} while (PagePrivate(page)); | 
 | 1532 | 	return 0; | 
 | 1533 | out_error: | 
| Trond Myklebust | e507d9e | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 1534 | 	__mark_inode_dirty(inode, I_DIRTY_PAGES); | 
| Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 1535 | 	return ret; | 
| Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1536 | } | 
 | 1537 |  | 
 | 1538 | /* | 
 | 1539 |  * Write back all requests on one page - we do this before reading it. | 
 | 1540 |  */ | 
 | 1541 | int nfs_wb_page(struct inode *inode, struct page* page) | 
 | 1542 | { | 
| Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 1543 | 	return nfs_wb_page_priority(inode, page, FLUSH_STABLE); | 
| Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1544 | } | 
 | 1545 |  | 
| David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 1546 | int __init nfs_init_writepagecache(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | { | 
 | 1548 | 	nfs_wdata_cachep = kmem_cache_create("nfs_write_data", | 
 | 1549 | 					     sizeof(struct nfs_write_data), | 
 | 1550 | 					     0, SLAB_HWCACHE_ALIGN, | 
| Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1551 | 					     NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | 	if (nfs_wdata_cachep == NULL) | 
 | 1553 | 		return -ENOMEM; | 
 | 1554 |  | 
| Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 1555 | 	nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, | 
 | 1556 | 						     nfs_wdata_cachep); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | 	if (nfs_wdata_mempool == NULL) | 
 | 1558 | 		return -ENOMEM; | 
 | 1559 |  | 
| Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 1560 | 	nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, | 
 | 1561 | 						      nfs_wdata_cachep); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | 	if (nfs_commit_mempool == NULL) | 
 | 1563 | 		return -ENOMEM; | 
 | 1564 |  | 
| Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 1565 | 	/* | 
 | 1566 | 	 * NFS congestion size, scale with available memory. | 
 | 1567 | 	 * | 
 | 1568 | 	 *  64MB:    8192k | 
 | 1569 | 	 * 128MB:   11585k | 
 | 1570 | 	 * 256MB:   16384k | 
 | 1571 | 	 * 512MB:   23170k | 
 | 1572 | 	 *   1GB:   32768k | 
 | 1573 | 	 *   2GB:   46340k | 
 | 1574 | 	 *   4GB:   65536k | 
 | 1575 | 	 *   8GB:   92681k | 
 | 1576 | 	 *  16GB:  131072k | 
 | 1577 | 	 * | 
 | 1578 | 	 * This allows larger machines to have larger/more transfers. | 
 | 1579 | 	 * Limit the default to 256M | 
 | 1580 | 	 */ | 
 | 1581 | 	nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); | 
 | 1582 | 	if (nfs_congestion_kb > 256*1024) | 
 | 1583 | 		nfs_congestion_kb = 256*1024; | 
 | 1584 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | 	return 0; | 
 | 1586 | } | 
 | 1587 |  | 
| David Brownell | 266bee8 | 2006-06-27 12:59:15 -0700 | [diff] [blame] | 1588 | void nfs_destroy_writepagecache(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | { | 
 | 1590 | 	mempool_destroy(nfs_commit_mempool); | 
 | 1591 | 	mempool_destroy(nfs_wdata_mempool); | 
| Alexey Dobriyan | 1a1d92c | 2006-09-27 01:49:40 -0700 | [diff] [blame] | 1592 | 	kmem_cache_destroy(nfs_wdata_cachep); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | } | 
 | 1594 |  |