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