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