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