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