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