1 /* handling of writes to regular files and writing back to the server 2 * 3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. 4 * Written by David Howells (dhowells@redhat.com) 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 9 * 2 of the License, or (at your option) any later version. 10 */ 11 12 #include <linux/backing-dev.h> 13 #include <linux/slab.h> 14 #include <linux/fs.h> 15 #include <linux/pagemap.h> 16 #include <linux/writeback.h> 17 #include <linux/pagevec.h> 18 #include "internal.h" 19 20 /* 21 * mark a page as having been made dirty and thus needing writeback 22 */ 23 int afs_set_page_dirty(struct page *page) 24 { 25 _enter(""); 26 return __set_page_dirty_nobuffers(page); 27 } 28 29 /* 30 * partly or wholly fill a page that's under preparation for writing 31 */ 32 static int afs_fill_page(struct afs_vnode *vnode, struct key *key, 33 loff_t pos, unsigned int len, struct page *page) 34 { 35 struct afs_read *req; 36 size_t p; 37 void *data; 38 int ret; 39 40 _enter(",,%llu", (unsigned long long)pos); 41 42 if (pos >= vnode->vfs_inode.i_size) { 43 p = pos & ~PAGE_MASK; 44 ASSERTCMP(p + len, <=, PAGE_SIZE); 45 data = kmap(page); 46 memset(data + p, 0, len); 47 kunmap(page); 48 return 0; 49 } 50 51 req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *), 52 GFP_KERNEL); 53 if (!req) 54 return -ENOMEM; 55 56 refcount_set(&req->usage, 1); 57 req->pos = pos; 58 req->len = len; 59 req->nr_pages = 1; 60 req->pages = req->array; 61 req->pages[0] = page; 62 get_page(page); 63 64 ret = afs_fetch_data(vnode, key, req); 65 afs_put_read(req); 66 if (ret < 0) { 67 if (ret == -ENOENT) { 68 _debug("got NOENT from server" 69 " - marking file deleted and stale"); 70 set_bit(AFS_VNODE_DELETED, &vnode->flags); 71 ret = -ESTALE; 72 } 73 } 74 75 _leave(" = %d", ret); 76 return ret; 77 } 78 79 /* 80 * prepare to perform part of a write to a page 81 */ 82 int afs_write_begin(struct file *file, struct address_space *mapping, 83 loff_t pos, unsigned len, unsigned flags, 84 struct page **pagep, void **fsdata) 85 { 86 struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 87 struct page *page; 88 struct key *key = afs_file_key(file); 89 unsigned long priv; 90 unsigned f, from = pos & (PAGE_SIZE - 1); 91 unsigned t, to = from + len; 92 pgoff_t index = pos >> PAGE_SHIFT; 93 int ret; 94 95 _enter("{%llx:%llu},{%lx},%u,%u", 96 vnode->fid.vid, vnode->fid.vnode, index, from, to); 97 98 /* We want to store information about how much of a page is altered in 99 * page->private. 100 */ 101 BUILD_BUG_ON(PAGE_SIZE > 32768 && sizeof(page->private) < 8); 102 103 page = grab_cache_page_write_begin(mapping, index, flags); 104 if (!page) 105 return -ENOMEM; 106 107 if (!PageUptodate(page) && len != PAGE_SIZE) { 108 ret = afs_fill_page(vnode, key, pos & PAGE_MASK, PAGE_SIZE, page); 109 if (ret < 0) { 110 unlock_page(page); 111 put_page(page); 112 _leave(" = %d [prep]", ret); 113 return ret; 114 } 115 SetPageUptodate(page); 116 } 117 118 /* page won't leak in error case: it eventually gets cleaned off LRU */ 119 *pagep = page; 120 121 try_again: 122 /* See if this page is already partially written in a way that we can 123 * merge the new write with. 124 */ 125 t = f = 0; 126 if (PagePrivate(page)) { 127 priv = page_private(page); 128 f = priv & AFS_PRIV_MAX; 129 t = priv >> AFS_PRIV_SHIFT; 130 ASSERTCMP(f, <=, t); 131 } 132 133 if (f != t) { 134 if (PageWriteback(page)) { 135 trace_afs_page_dirty(vnode, tracepoint_string("alrdy"), 136 page->index, priv); 137 goto flush_conflicting_write; 138 } 139 /* If the file is being filled locally, allow inter-write 140 * spaces to be merged into writes. If it's not, only write 141 * back what the user gives us. 142 */ 143 if (!test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags) && 144 (to < f || from > t)) 145 goto flush_conflicting_write; 146 if (from < f) 147 f = from; 148 if (to > t) 149 t = to; 150 } else { 151 f = from; 152 t = to; 153 } 154 155 priv = (unsigned long)t << AFS_PRIV_SHIFT; 156 priv |= f; 157 trace_afs_page_dirty(vnode, tracepoint_string("begin"), 158 page->index, priv); 159 SetPagePrivate(page); 160 set_page_private(page, priv); 161 _leave(" = 0"); 162 return 0; 163 164 /* The previous write and this write aren't adjacent or overlapping, so 165 * flush the page out. 166 */ 167 flush_conflicting_write: 168 _debug("flush conflict"); 169 ret = write_one_page(page); 170 if (ret < 0) { 171 _leave(" = %d", ret); 172 return ret; 173 } 174 175 ret = lock_page_killable(page); 176 if (ret < 0) { 177 _leave(" = %d", ret); 178 return ret; 179 } 180 goto try_again; 181 } 182 183 /* 184 * finalise part of a write to a page 185 */ 186 int afs_write_end(struct file *file, struct address_space *mapping, 187 loff_t pos, unsigned len, unsigned copied, 188 struct page *page, void *fsdata) 189 { 190 struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); 191 struct key *key = afs_file_key(file); 192 loff_t i_size, maybe_i_size; 193 int ret; 194 195 _enter("{%llx:%llu},{%lx}", 196 vnode->fid.vid, vnode->fid.vnode, page->index); 197 198 maybe_i_size = pos + copied; 199 200 i_size = i_size_read(&vnode->vfs_inode); 201 if (maybe_i_size > i_size) { 202 spin_lock(&vnode->wb_lock); 203 i_size = i_size_read(&vnode->vfs_inode); 204 if (maybe_i_size > i_size) 205 i_size_write(&vnode->vfs_inode, maybe_i_size); 206 spin_unlock(&vnode->wb_lock); 207 } 208 209 if (!PageUptodate(page)) { 210 if (copied < len) { 211 /* Try and load any missing data from the server. The 212 * unmarshalling routine will take care of clearing any 213 * bits that are beyond the EOF. 214 */ 215 ret = afs_fill_page(vnode, key, pos + copied, 216 len - copied, page); 217 if (ret < 0) 218 goto out; 219 } 220 SetPageUptodate(page); 221 } 222 223 set_page_dirty(page); 224 if (PageDirty(page)) 225 _debug("dirtied"); 226 ret = copied; 227 228 out: 229 unlock_page(page); 230 put_page(page); 231 return ret; 232 } 233 234 /* 235 * kill all the pages in the given range 236 */ 237 static void afs_kill_pages(struct address_space *mapping, 238 pgoff_t first, pgoff_t last) 239 { 240 struct afs_vnode *vnode = AFS_FS_I(mapping->host); 241 struct pagevec pv; 242 unsigned count, loop; 243 244 _enter("{%llx:%llu},%lx-%lx", 245 vnode->fid.vid, vnode->fid.vnode, first, last); 246 247 pagevec_init(&pv); 248 249 do { 250 _debug("kill %lx-%lx", first, last); 251 252 count = last - first + 1; 253 if (count > PAGEVEC_SIZE) 254 count = PAGEVEC_SIZE; 255 pv.nr = find_get_pages_contig(mapping, first, count, pv.pages); 256 ASSERTCMP(pv.nr, ==, count); 257 258 for (loop = 0; loop < count; loop++) { 259 struct page *page = pv.pages[loop]; 260 ClearPageUptodate(page); 261 SetPageError(page); 262 end_page_writeback(page); 263 if (page->index >= first) 264 first = page->index + 1; 265 lock_page(page); 266 generic_error_remove_page(mapping, page); 267 unlock_page(page); 268 } 269 270 __pagevec_release(&pv); 271 } while (first <= last); 272 273 _leave(""); 274 } 275 276 /* 277 * Redirty all the pages in a given range. 278 */ 279 static void afs_redirty_pages(struct writeback_control *wbc, 280 struct address_space *mapping, 281 pgoff_t first, pgoff_t last) 282 { 283 struct afs_vnode *vnode = AFS_FS_I(mapping->host); 284 struct pagevec pv; 285 unsigned count, loop; 286 287 _enter("{%llx:%llu},%lx-%lx", 288 vnode->fid.vid, vnode->fid.vnode, first, last); 289 290 pagevec_init(&pv); 291 292 do { 293 _debug("redirty %lx-%lx", first, last); 294 295 count = last - first + 1; 296 if (count > PAGEVEC_SIZE) 297 count = PAGEVEC_SIZE; 298 pv.nr = find_get_pages_contig(mapping, first, count, pv.pages); 299 ASSERTCMP(pv.nr, ==, count); 300 301 for (loop = 0; loop < count; loop++) { 302 struct page *page = pv.pages[loop]; 303 304 redirty_page_for_writepage(wbc, page); 305 end_page_writeback(page); 306 if (page->index >= first) 307 first = page->index + 1; 308 } 309 310 __pagevec_release(&pv); 311 } while (first <= last); 312 313 _leave(""); 314 } 315 316 /* 317 * write to a file 318 */ 319 static int afs_store_data(struct address_space *mapping, 320 pgoff_t first, pgoff_t last, 321 unsigned offset, unsigned to) 322 { 323 struct afs_vnode *vnode = AFS_FS_I(mapping->host); 324 struct afs_fs_cursor fc; 325 struct afs_wb_key *wbk = NULL; 326 struct list_head *p; 327 int ret = -ENOKEY, ret2; 328 329 _enter("%s{%llx:%llu.%u},%lx,%lx,%x,%x", 330 vnode->volume->name, 331 vnode->fid.vid, 332 vnode->fid.vnode, 333 vnode->fid.unique, 334 first, last, offset, to); 335 336 spin_lock(&vnode->wb_lock); 337 p = vnode->wb_keys.next; 338 339 /* Iterate through the list looking for a valid key to use. */ 340 try_next_key: 341 while (p != &vnode->wb_keys) { 342 wbk = list_entry(p, struct afs_wb_key, vnode_link); 343 _debug("wbk %u", key_serial(wbk->key)); 344 ret2 = key_validate(wbk->key); 345 if (ret2 == 0) 346 goto found_key; 347 if (ret == -ENOKEY) 348 ret = ret2; 349 p = p->next; 350 } 351 352 spin_unlock(&vnode->wb_lock); 353 afs_put_wb_key(wbk); 354 _leave(" = %d [no keys]", ret); 355 return ret; 356 357 found_key: 358 refcount_inc(&wbk->usage); 359 spin_unlock(&vnode->wb_lock); 360 361 _debug("USE WB KEY %u", key_serial(wbk->key)); 362 363 ret = -ERESTARTSYS; 364 if (afs_begin_vnode_operation(&fc, vnode, wbk->key)) { 365 while (afs_select_fileserver(&fc)) { 366 fc.cb_break = afs_calc_vnode_cb_break(vnode); 367 afs_fs_store_data(&fc, mapping, first, last, offset, to); 368 } 369 370 afs_check_for_remote_deletion(&fc, fc.vnode); 371 afs_vnode_commit_status(&fc, vnode, fc.cb_break); 372 ret = afs_end_vnode_operation(&fc); 373 } 374 375 switch (ret) { 376 case 0: 377 afs_stat_v(vnode, n_stores); 378 atomic_long_add((last * PAGE_SIZE + to) - 379 (first * PAGE_SIZE + offset), 380 &afs_v2net(vnode)->n_store_bytes); 381 break; 382 case -EACCES: 383 case -EPERM: 384 case -ENOKEY: 385 case -EKEYEXPIRED: 386 case -EKEYREJECTED: 387 case -EKEYREVOKED: 388 _debug("next"); 389 spin_lock(&vnode->wb_lock); 390 p = wbk->vnode_link.next; 391 afs_put_wb_key(wbk); 392 goto try_next_key; 393 } 394 395 afs_put_wb_key(wbk); 396 _leave(" = %d", ret); 397 return ret; 398 } 399 400 /* 401 * Synchronously write back the locked page and any subsequent non-locked dirty 402 * pages. 403 */ 404 static int afs_write_back_from_locked_page(struct address_space *mapping, 405 struct writeback_control *wbc, 406 struct page *primary_page, 407 pgoff_t final_page) 408 { 409 struct afs_vnode *vnode = AFS_FS_I(mapping->host); 410 struct page *pages[8], *page; 411 unsigned long count, priv; 412 unsigned n, offset, to, f, t; 413 pgoff_t start, first, last; 414 int loop, ret; 415 416 _enter(",%lx", primary_page->index); 417 418 count = 1; 419 if (test_set_page_writeback(primary_page)) 420 BUG(); 421 422 /* Find all consecutive lockable dirty pages that have contiguous 423 * written regions, stopping when we find a page that is not 424 * immediately lockable, is not dirty or is missing, or we reach the 425 * end of the range. 426 */ 427 start = primary_page->index; 428 priv = page_private(primary_page); 429 offset = priv & AFS_PRIV_MAX; 430 to = priv >> AFS_PRIV_SHIFT; 431 trace_afs_page_dirty(vnode, tracepoint_string("store"), 432 primary_page->index, priv); 433 434 WARN_ON(offset == to); 435 if (offset == to) 436 trace_afs_page_dirty(vnode, tracepoint_string("WARN"), 437 primary_page->index, priv); 438 439 if (start >= final_page || 440 (to < PAGE_SIZE && !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags))) 441 goto no_more; 442 443 start++; 444 do { 445 _debug("more %lx [%lx]", start, count); 446 n = final_page - start + 1; 447 if (n > ARRAY_SIZE(pages)) 448 n = ARRAY_SIZE(pages); 449 n = find_get_pages_contig(mapping, start, ARRAY_SIZE(pages), pages); 450 _debug("fgpc %u", n); 451 if (n == 0) 452 goto no_more; 453 if (pages[0]->index != start) { 454 do { 455 put_page(pages[--n]); 456 } while (n > 0); 457 goto no_more; 458 } 459 460 for (loop = 0; loop < n; loop++) { 461 page = pages[loop]; 462 if (to != PAGE_SIZE && 463 !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags)) 464 break; 465 if (page->index > final_page) 466 break; 467 if (!trylock_page(page)) 468 break; 469 if (!PageDirty(page) || PageWriteback(page)) { 470 unlock_page(page); 471 break; 472 } 473 474 priv = page_private(page); 475 f = priv & AFS_PRIV_MAX; 476 t = priv >> AFS_PRIV_SHIFT; 477 if (f != 0 && 478 !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags)) { 479 unlock_page(page); 480 break; 481 } 482 to = t; 483 484 trace_afs_page_dirty(vnode, tracepoint_string("store+"), 485 page->index, priv); 486 487 if (!clear_page_dirty_for_io(page)) 488 BUG(); 489 if (test_set_page_writeback(page)) 490 BUG(); 491 unlock_page(page); 492 put_page(page); 493 } 494 count += loop; 495 if (loop < n) { 496 for (; loop < n; loop++) 497 put_page(pages[loop]); 498 goto no_more; 499 } 500 501 start += loop; 502 } while (start <= final_page && count < 65536); 503 504 no_more: 505 /* We now have a contiguous set of dirty pages, each with writeback 506 * set; the first page is still locked at this point, but all the rest 507 * have been unlocked. 508 */ 509 unlock_page(primary_page); 510 511 first = primary_page->index; 512 last = first + count - 1; 513 514 _debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to); 515 516 ret = afs_store_data(mapping, first, last, offset, to); 517 switch (ret) { 518 case 0: 519 ret = count; 520 break; 521 522 default: 523 pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret); 524 /* Fall through */ 525 case -EACCES: 526 case -EPERM: 527 case -ENOKEY: 528 case -EKEYEXPIRED: 529 case -EKEYREJECTED: 530 case -EKEYREVOKED: 531 afs_redirty_pages(wbc, mapping, first, last); 532 mapping_set_error(mapping, ret); 533 break; 534 535 case -EDQUOT: 536 case -ENOSPC: 537 afs_redirty_pages(wbc, mapping, first, last); 538 mapping_set_error(mapping, -ENOSPC); 539 break; 540 541 case -EROFS: 542 case -EIO: 543 case -EREMOTEIO: 544 case -EFBIG: 545 case -ENOENT: 546 case -ENOMEDIUM: 547 case -ENXIO: 548 trace_afs_file_error(vnode, ret, afs_file_error_writeback_fail); 549 afs_kill_pages(mapping, first, last); 550 mapping_set_error(mapping, ret); 551 break; 552 } 553 554 _leave(" = %d", ret); 555 return ret; 556 } 557 558 /* 559 * write a page back to the server 560 * - the caller locked the page for us 561 */ 562 int afs_writepage(struct page *page, struct writeback_control *wbc) 563 { 564 int ret; 565 566 _enter("{%lx},", page->index); 567 568 ret = afs_write_back_from_locked_page(page->mapping, wbc, page, 569 wbc->range_end >> PAGE_SHIFT); 570 if (ret < 0) { 571 _leave(" = %d", ret); 572 return 0; 573 } 574 575 wbc->nr_to_write -= ret; 576 577 _leave(" = 0"); 578 return 0; 579 } 580 581 /* 582 * write a region of pages back to the server 583 */ 584 static int afs_writepages_region(struct address_space *mapping, 585 struct writeback_control *wbc, 586 pgoff_t index, pgoff_t end, pgoff_t *_next) 587 { 588 struct page *page; 589 int ret, n; 590 591 _enter(",,%lx,%lx,", index, end); 592 593 do { 594 n = find_get_pages_range_tag(mapping, &index, end, 595 PAGECACHE_TAG_DIRTY, 1, &page); 596 if (!n) 597 break; 598 599 _debug("wback %lx", page->index); 600 601 /* 602 * at this point we hold neither the i_pages lock nor the 603 * page lock: the page may be truncated or invalidated 604 * (changing page->mapping to NULL), or even swizzled 605 * back from swapper_space to tmpfs file mapping 606 */ 607 ret = lock_page_killable(page); 608 if (ret < 0) { 609 put_page(page); 610 _leave(" = %d", ret); 611 return ret; 612 } 613 614 if (page->mapping != mapping || !PageDirty(page)) { 615 unlock_page(page); 616 put_page(page); 617 continue; 618 } 619 620 if (PageWriteback(page)) { 621 unlock_page(page); 622 if (wbc->sync_mode != WB_SYNC_NONE) 623 wait_on_page_writeback(page); 624 put_page(page); 625 continue; 626 } 627 628 if (!clear_page_dirty_for_io(page)) 629 BUG(); 630 ret = afs_write_back_from_locked_page(mapping, wbc, page, end); 631 put_page(page); 632 if (ret < 0) { 633 _leave(" = %d", ret); 634 return ret; 635 } 636 637 wbc->nr_to_write -= ret; 638 639 cond_resched(); 640 } while (index < end && wbc->nr_to_write > 0); 641 642 *_next = index; 643 _leave(" = 0 [%lx]", *_next); 644 return 0; 645 } 646 647 /* 648 * write some of the pending data back to the server 649 */ 650 int afs_writepages(struct address_space *mapping, 651 struct writeback_control *wbc) 652 { 653 pgoff_t start, end, next; 654 int ret; 655 656 _enter(""); 657 658 if (wbc->range_cyclic) { 659 start = mapping->writeback_index; 660 end = -1; 661 ret = afs_writepages_region(mapping, wbc, start, end, &next); 662 if (start > 0 && wbc->nr_to_write > 0 && ret == 0) 663 ret = afs_writepages_region(mapping, wbc, 0, start, 664 &next); 665 mapping->writeback_index = next; 666 } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) { 667 end = (pgoff_t)(LLONG_MAX >> PAGE_SHIFT); 668 ret = afs_writepages_region(mapping, wbc, 0, end, &next); 669 if (wbc->nr_to_write > 0) 670 mapping->writeback_index = next; 671 } else { 672 start = wbc->range_start >> PAGE_SHIFT; 673 end = wbc->range_end >> PAGE_SHIFT; 674 ret = afs_writepages_region(mapping, wbc, start, end, &next); 675 } 676 677 _leave(" = %d", ret); 678 return ret; 679 } 680 681 /* 682 * completion of write to server 683 */ 684 void afs_pages_written_back(struct afs_vnode *vnode, struct afs_call *call) 685 { 686 struct pagevec pv; 687 unsigned long priv; 688 unsigned count, loop; 689 pgoff_t first = call->first, last = call->last; 690 691 _enter("{%llx:%llu},{%lx-%lx}", 692 vnode->fid.vid, vnode->fid.vnode, first, last); 693 694 pagevec_init(&pv); 695 696 do { 697 _debug("done %lx-%lx", first, last); 698 699 count = last - first + 1; 700 if (count > PAGEVEC_SIZE) 701 count = PAGEVEC_SIZE; 702 pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping, 703 first, count, pv.pages); 704 ASSERTCMP(pv.nr, ==, count); 705 706 for (loop = 0; loop < count; loop++) { 707 priv = page_private(pv.pages[loop]); 708 trace_afs_page_dirty(vnode, tracepoint_string("clear"), 709 pv.pages[loop]->index, priv); 710 set_page_private(pv.pages[loop], 0); 711 end_page_writeback(pv.pages[loop]); 712 } 713 first += count; 714 __pagevec_release(&pv); 715 } while (first <= last); 716 717 afs_prune_wb_keys(vnode); 718 _leave(""); 719 } 720 721 /* 722 * write to an AFS file 723 */ 724 ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from) 725 { 726 struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp)); 727 ssize_t result; 728 size_t count = iov_iter_count(from); 729 730 _enter("{%llx:%llu},{%zu},", 731 vnode->fid.vid, vnode->fid.vnode, count); 732 733 if (IS_SWAPFILE(&vnode->vfs_inode)) { 734 printk(KERN_INFO 735 "AFS: Attempt to write to active swap file!\n"); 736 return -EBUSY; 737 } 738 739 if (!count) 740 return 0; 741 742 result = generic_file_write_iter(iocb, from); 743 744 _leave(" = %zd", result); 745 return result; 746 } 747 748 /* 749 * flush any dirty pages for this process, and check for write errors. 750 * - the return status from this call provides a reliable indication of 751 * whether any write errors occurred for this process. 752 */ 753 int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync) 754 { 755 struct inode *inode = file_inode(file); 756 struct afs_vnode *vnode = AFS_FS_I(inode); 757 758 _enter("{%llx:%llu},{n=%pD},%d", 759 vnode->fid.vid, vnode->fid.vnode, file, 760 datasync); 761 762 return file_write_and_wait_range(file, start, end); 763 } 764 765 /* 766 * notification that a previously read-only page is about to become writable 767 * - if it returns an error, the caller will deliver a bus error signal 768 */ 769 vm_fault_t afs_page_mkwrite(struct vm_fault *vmf) 770 { 771 struct file *file = vmf->vma->vm_file; 772 struct inode *inode = file_inode(file); 773 struct afs_vnode *vnode = AFS_FS_I(inode); 774 unsigned long priv; 775 776 _enter("{{%llx:%llu}},{%lx}", 777 vnode->fid.vid, vnode->fid.vnode, vmf->page->index); 778 779 sb_start_pagefault(inode->i_sb); 780 781 /* Wait for the page to be written to the cache before we allow it to 782 * be modified. We then assume the entire page will need writing back. 783 */ 784 #ifdef CONFIG_AFS_FSCACHE 785 fscache_wait_on_page_write(vnode->cache, vmf->page); 786 #endif 787 788 if (PageWriteback(vmf->page) && 789 wait_on_page_bit_killable(vmf->page, PG_writeback) < 0) 790 return VM_FAULT_RETRY; 791 792 if (lock_page_killable(vmf->page) < 0) 793 return VM_FAULT_RETRY; 794 795 /* We mustn't change page->private until writeback is complete as that 796 * details the portion of the page we need to write back and we might 797 * need to redirty the page if there's a problem. 798 */ 799 wait_on_page_writeback(vmf->page); 800 801 priv = (unsigned long)PAGE_SIZE << AFS_PRIV_SHIFT; /* To */ 802 priv |= 0; /* From */ 803 trace_afs_page_dirty(vnode, tracepoint_string("mkwrite"), 804 vmf->page->index, priv); 805 SetPagePrivate(vmf->page); 806 set_page_private(vmf->page, priv); 807 808 sb_end_pagefault(inode->i_sb); 809 return VM_FAULT_LOCKED; 810 } 811 812 /* 813 * Prune the keys cached for writeback. The caller must hold vnode->wb_lock. 814 */ 815 void afs_prune_wb_keys(struct afs_vnode *vnode) 816 { 817 LIST_HEAD(graveyard); 818 struct afs_wb_key *wbk, *tmp; 819 820 /* Discard unused keys */ 821 spin_lock(&vnode->wb_lock); 822 823 if (!mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_WRITEBACK) && 824 !mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_DIRTY)) { 825 list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) { 826 if (refcount_read(&wbk->usage) == 1) 827 list_move(&wbk->vnode_link, &graveyard); 828 } 829 } 830 831 spin_unlock(&vnode->wb_lock); 832 833 while (!list_empty(&graveyard)) { 834 wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link); 835 list_del(&wbk->vnode_link); 836 afs_put_wb_key(wbk); 837 } 838 } 839 840 /* 841 * Clean up a page during invalidation. 842 */ 843 int afs_launder_page(struct page *page) 844 { 845 struct address_space *mapping = page->mapping; 846 struct afs_vnode *vnode = AFS_FS_I(mapping->host); 847 unsigned long priv; 848 unsigned int f, t; 849 int ret = 0; 850 851 _enter("{%lx}", page->index); 852 853 priv = page_private(page); 854 if (clear_page_dirty_for_io(page)) { 855 f = 0; 856 t = PAGE_SIZE; 857 if (PagePrivate(page)) { 858 f = priv & AFS_PRIV_MAX; 859 t = priv >> AFS_PRIV_SHIFT; 860 } 861 862 trace_afs_page_dirty(vnode, tracepoint_string("launder"), 863 page->index, priv); 864 ret = afs_store_data(mapping, page->index, page->index, t, f); 865 } 866 867 trace_afs_page_dirty(vnode, tracepoint_string("laundered"), 868 page->index, priv); 869 set_page_private(page, 0); 870 ClearPagePrivate(page); 871 872 #ifdef CONFIG_AFS_FSCACHE 873 if (PageFsCache(page)) { 874 fscache_wait_on_page_write(vnode->cache, page); 875 fscache_uncache_page(vnode->cache, page); 876 } 877 #endif 878 return ret; 879 } 880