1 #include "ceph_debug.h" 2 3 #include <linux/backing-dev.h> 4 #include <linux/fs.h> 5 #include <linux/mm.h> 6 #include <linux/pagemap.h> 7 #include <linux/writeback.h> /* generic_writepages */ 8 #include <linux/slab.h> 9 #include <linux/pagevec.h> 10 #include <linux/task_io_accounting_ops.h> 11 12 #include "super.h" 13 #include "osd_client.h" 14 15 /* 16 * Ceph address space ops. 17 * 18 * There are a few funny things going on here. 19 * 20 * The page->private field is used to reference a struct 21 * ceph_snap_context for _every_ dirty page. This indicates which 22 * snapshot the page was logically dirtied in, and thus which snap 23 * context needs to be associated with the osd write during writeback. 24 * 25 * Similarly, struct ceph_inode_info maintains a set of counters to 26 * count dirty pages on the inode. In the absense of snapshots, 27 * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count. 28 * 29 * When a snapshot is taken (that is, when the client receives 30 * notification that a snapshot was taken), each inode with caps and 31 * with dirty pages (dirty pages implies there is a cap) gets a new 32 * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending 33 * order, new snaps go to the tail). The i_wrbuffer_ref_head count is 34 * moved to capsnap->dirty. (Unless a sync write is currently in 35 * progress. In that case, the capsnap is said to be "pending", new 36 * writes cannot start, and the capsnap isn't "finalized" until the 37 * write completes (or fails) and a final size/mtime for the inode for 38 * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0. 39 * 40 * On writeback, we must submit writes to the osd IN SNAP ORDER. So, 41 * we look for the first capsnap in i_cap_snaps and write out pages in 42 * that snap context _only_. Then we move on to the next capsnap, 43 * eventually reaching the "live" or "head" context (i.e., pages that 44 * are not yet snapped) and are writing the most recently dirtied 45 * pages. 46 * 47 * Invalidate and so forth must take care to ensure the dirty page 48 * accounting is preserved. 49 */ 50 51 #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10)) 52 #define CONGESTION_OFF_THRESH(congestion_kb) \ 53 (CONGESTION_ON_THRESH(congestion_kb) - \ 54 (CONGESTION_ON_THRESH(congestion_kb) >> 2)) 55 56 57 58 /* 59 * Dirty a page. Optimistically adjust accounting, on the assumption 60 * that we won't race with invalidate. If we do, readjust. 61 */ 62 static int ceph_set_page_dirty(struct page *page) 63 { 64 struct address_space *mapping = page->mapping; 65 struct inode *inode; 66 struct ceph_inode_info *ci; 67 int undo = 0; 68 struct ceph_snap_context *snapc; 69 70 if (unlikely(!mapping)) 71 return !TestSetPageDirty(page); 72 73 if (TestSetPageDirty(page)) { 74 dout("%p set_page_dirty %p idx %lu -- already dirty\n", 75 mapping->host, page, page->index); 76 return 0; 77 } 78 79 inode = mapping->host; 80 ci = ceph_inode(inode); 81 82 /* 83 * Note that we're grabbing a snapc ref here without holding 84 * any locks! 85 */ 86 snapc = ceph_get_snap_context(ci->i_snap_realm->cached_context); 87 88 /* dirty the head */ 89 spin_lock(&inode->i_lock); 90 if (ci->i_wrbuffer_ref_head == 0) 91 ci->i_head_snapc = ceph_get_snap_context(snapc); 92 ++ci->i_wrbuffer_ref_head; 93 if (ci->i_wrbuffer_ref == 0) 94 igrab(inode); 95 ++ci->i_wrbuffer_ref; 96 dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d " 97 "snapc %p seq %lld (%d snaps)\n", 98 mapping->host, page, page->index, 99 ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1, 100 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head, 101 snapc, snapc->seq, snapc->num_snaps); 102 spin_unlock(&inode->i_lock); 103 104 /* now adjust page */ 105 spin_lock_irq(&mapping->tree_lock); 106 if (page->mapping) { /* Race with truncate? */ 107 WARN_ON_ONCE(!PageUptodate(page)); 108 109 if (mapping_cap_account_dirty(mapping)) { 110 __inc_zone_page_state(page, NR_FILE_DIRTY); 111 __inc_bdi_stat(mapping->backing_dev_info, 112 BDI_RECLAIMABLE); 113 task_io_account_write(PAGE_CACHE_SIZE); 114 } 115 radix_tree_tag_set(&mapping->page_tree, 116 page_index(page), PAGECACHE_TAG_DIRTY); 117 118 /* 119 * Reference snap context in page->private. Also set 120 * PagePrivate so that we get invalidatepage callback. 121 */ 122 page->private = (unsigned long)snapc; 123 SetPagePrivate(page); 124 } else { 125 dout("ANON set_page_dirty %p (raced truncate?)\n", page); 126 undo = 1; 127 } 128 129 spin_unlock_irq(&mapping->tree_lock); 130 131 if (undo) 132 /* whoops, we failed to dirty the page */ 133 ceph_put_wrbuffer_cap_refs(ci, 1, snapc); 134 135 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); 136 137 BUG_ON(!PageDirty(page)); 138 return 1; 139 } 140 141 /* 142 * If we are truncating the full page (i.e. offset == 0), adjust the 143 * dirty page counters appropriately. Only called if there is private 144 * data on the page. 145 */ 146 static void ceph_invalidatepage(struct page *page, unsigned long offset) 147 { 148 struct inode *inode; 149 struct ceph_inode_info *ci; 150 struct ceph_snap_context *snapc = (void *)page->private; 151 152 BUG_ON(!PageLocked(page)); 153 BUG_ON(!page->private); 154 BUG_ON(!PagePrivate(page)); 155 BUG_ON(!page->mapping); 156 157 inode = page->mapping->host; 158 159 /* 160 * We can get non-dirty pages here due to races between 161 * set_page_dirty and truncate_complete_page; just spit out a 162 * warning, in case we end up with accounting problems later. 163 */ 164 if (!PageDirty(page)) 165 pr_err("%p invalidatepage %p page not dirty\n", inode, page); 166 167 if (offset == 0) 168 ClearPageChecked(page); 169 170 ci = ceph_inode(inode); 171 if (offset == 0) { 172 dout("%p invalidatepage %p idx %lu full dirty page %lu\n", 173 inode, page, page->index, offset); 174 ceph_put_wrbuffer_cap_refs(ci, 1, snapc); 175 ceph_put_snap_context(snapc); 176 page->private = 0; 177 ClearPagePrivate(page); 178 } else { 179 dout("%p invalidatepage %p idx %lu partial dirty page\n", 180 inode, page, page->index); 181 } 182 } 183 184 /* just a sanity check */ 185 static int ceph_releasepage(struct page *page, gfp_t g) 186 { 187 struct inode *inode = page->mapping ? page->mapping->host : NULL; 188 dout("%p releasepage %p idx %lu\n", inode, page, page->index); 189 WARN_ON(PageDirty(page)); 190 WARN_ON(page->private); 191 WARN_ON(PagePrivate(page)); 192 return 0; 193 } 194 195 /* 196 * read a single page, without unlocking it. 197 */ 198 static int readpage_nounlock(struct file *filp, struct page *page) 199 { 200 struct inode *inode = filp->f_dentry->d_inode; 201 struct ceph_inode_info *ci = ceph_inode(inode); 202 struct ceph_osd_client *osdc = &ceph_inode_to_client(inode)->osdc; 203 int err = 0; 204 u64 len = PAGE_CACHE_SIZE; 205 206 dout("readpage inode %p file %p page %p index %lu\n", 207 inode, filp, page, page->index); 208 err = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout, 209 page->index << PAGE_CACHE_SHIFT, &len, 210 ci->i_truncate_seq, ci->i_truncate_size, 211 &page, 1); 212 if (err == -ENOENT) 213 err = 0; 214 if (err < 0) { 215 SetPageError(page); 216 goto out; 217 } else if (err < PAGE_CACHE_SIZE) { 218 /* zero fill remainder of page */ 219 zero_user_segment(page, err, PAGE_CACHE_SIZE); 220 } 221 SetPageUptodate(page); 222 223 out: 224 return err < 0 ? err : 0; 225 } 226 227 static int ceph_readpage(struct file *filp, struct page *page) 228 { 229 int r = readpage_nounlock(filp, page); 230 unlock_page(page); 231 return r; 232 } 233 234 /* 235 * Build a vector of contiguous pages from the provided page list. 236 */ 237 static struct page **page_vector_from_list(struct list_head *page_list, 238 unsigned *nr_pages) 239 { 240 struct page **pages; 241 struct page *page; 242 int next_index, contig_pages = 0; 243 244 /* build page vector */ 245 pages = kmalloc(sizeof(*pages) * *nr_pages, GFP_NOFS); 246 if (!pages) 247 return ERR_PTR(-ENOMEM); 248 249 BUG_ON(list_empty(page_list)); 250 next_index = list_entry(page_list->prev, struct page, lru)->index; 251 list_for_each_entry_reverse(page, page_list, lru) { 252 if (page->index == next_index) { 253 dout("readpages page %d %p\n", contig_pages, page); 254 pages[contig_pages] = page; 255 contig_pages++; 256 next_index++; 257 } else { 258 break; 259 } 260 } 261 *nr_pages = contig_pages; 262 return pages; 263 } 264 265 /* 266 * Read multiple pages. Leave pages we don't read + unlock in page_list; 267 * the caller (VM) cleans them up. 268 */ 269 static int ceph_readpages(struct file *file, struct address_space *mapping, 270 struct list_head *page_list, unsigned nr_pages) 271 { 272 struct inode *inode = file->f_dentry->d_inode; 273 struct ceph_inode_info *ci = ceph_inode(inode); 274 struct ceph_osd_client *osdc = &ceph_inode_to_client(inode)->osdc; 275 int rc = 0; 276 struct page **pages; 277 struct pagevec pvec; 278 loff_t offset; 279 u64 len; 280 281 dout("readpages %p file %p nr_pages %d\n", 282 inode, file, nr_pages); 283 284 pages = page_vector_from_list(page_list, &nr_pages); 285 if (IS_ERR(pages)) 286 return PTR_ERR(pages); 287 288 /* guess read extent */ 289 offset = pages[0]->index << PAGE_CACHE_SHIFT; 290 len = nr_pages << PAGE_CACHE_SHIFT; 291 rc = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout, 292 offset, &len, 293 ci->i_truncate_seq, ci->i_truncate_size, 294 pages, nr_pages); 295 if (rc == -ENOENT) 296 rc = 0; 297 if (rc < 0) 298 goto out; 299 300 /* set uptodate and add to lru in pagevec-sized chunks */ 301 pagevec_init(&pvec, 0); 302 for (; !list_empty(page_list) && len > 0; 303 rc -= PAGE_CACHE_SIZE, len -= PAGE_CACHE_SIZE) { 304 struct page *page = 305 list_entry(page_list->prev, struct page, lru); 306 307 list_del(&page->lru); 308 309 if (rc < (int)PAGE_CACHE_SIZE) { 310 /* zero (remainder of) page */ 311 int s = rc < 0 ? 0 : rc; 312 zero_user_segment(page, s, PAGE_CACHE_SIZE); 313 } 314 315 if (add_to_page_cache(page, mapping, page->index, GFP_NOFS)) { 316 page_cache_release(page); 317 dout("readpages %p add_to_page_cache failed %p\n", 318 inode, page); 319 continue; 320 } 321 dout("readpages %p adding %p idx %lu\n", inode, page, 322 page->index); 323 flush_dcache_page(page); 324 SetPageUptodate(page); 325 unlock_page(page); 326 if (pagevec_add(&pvec, page) == 0) 327 pagevec_lru_add_file(&pvec); /* add to lru */ 328 } 329 pagevec_lru_add_file(&pvec); 330 rc = 0; 331 332 out: 333 kfree(pages); 334 return rc; 335 } 336 337 /* 338 * Get ref for the oldest snapc for an inode with dirty data... that is, the 339 * only snap context we are allowed to write back. 340 */ 341 static struct ceph_snap_context *get_oldest_context(struct inode *inode, 342 u64 *snap_size) 343 { 344 struct ceph_inode_info *ci = ceph_inode(inode); 345 struct ceph_snap_context *snapc = NULL; 346 struct ceph_cap_snap *capsnap = NULL; 347 348 spin_lock(&inode->i_lock); 349 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { 350 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap, 351 capsnap->context, capsnap->dirty_pages); 352 if (capsnap->dirty_pages) { 353 snapc = ceph_get_snap_context(capsnap->context); 354 if (snap_size) 355 *snap_size = capsnap->size; 356 break; 357 } 358 } 359 if (!snapc && ci->i_head_snapc) { 360 snapc = ceph_get_snap_context(ci->i_head_snapc); 361 dout(" head snapc %p has %d dirty pages\n", 362 snapc, ci->i_wrbuffer_ref_head); 363 } 364 spin_unlock(&inode->i_lock); 365 return snapc; 366 } 367 368 /* 369 * Write a single page, but leave the page locked. 370 * 371 * If we get a write error, set the page error bit, but still adjust the 372 * dirty page accounting (i.e., page is no longer dirty). 373 */ 374 static int writepage_nounlock(struct page *page, struct writeback_control *wbc) 375 { 376 struct inode *inode; 377 struct ceph_inode_info *ci; 378 struct ceph_client *client; 379 struct ceph_osd_client *osdc; 380 loff_t page_off = page->index << PAGE_CACHE_SHIFT; 381 int len = PAGE_CACHE_SIZE; 382 loff_t i_size; 383 int err = 0; 384 struct ceph_snap_context *snapc, *oldest; 385 u64 snap_size = 0; 386 long writeback_stat; 387 388 dout("writepage %p idx %lu\n", page, page->index); 389 390 if (!page->mapping || !page->mapping->host) { 391 dout("writepage %p - no mapping\n", page); 392 return -EFAULT; 393 } 394 inode = page->mapping->host; 395 ci = ceph_inode(inode); 396 client = ceph_inode_to_client(inode); 397 osdc = &client->osdc; 398 399 /* verify this is a writeable snap context */ 400 snapc = (void *)page->private; 401 if (snapc == NULL) { 402 dout("writepage %p page %p not dirty?\n", inode, page); 403 goto out; 404 } 405 oldest = get_oldest_context(inode, &snap_size); 406 if (snapc->seq > oldest->seq) { 407 dout("writepage %p page %p snapc %p not writeable - noop\n", 408 inode, page, (void *)page->private); 409 /* we should only noop if called by kswapd */ 410 WARN_ON((current->flags & PF_MEMALLOC) == 0); 411 ceph_put_snap_context(oldest); 412 goto out; 413 } 414 ceph_put_snap_context(oldest); 415 416 /* is this a partial page at end of file? */ 417 if (snap_size) 418 i_size = snap_size; 419 else 420 i_size = i_size_read(inode); 421 if (i_size < page_off + len) 422 len = i_size - page_off; 423 424 dout("writepage %p page %p index %lu on %llu~%u\n", 425 inode, page, page->index, page_off, len); 426 427 writeback_stat = atomic_long_inc_return(&client->writeback_count); 428 if (writeback_stat > 429 CONGESTION_ON_THRESH(client->mount_args->congestion_kb)) 430 set_bdi_congested(&client->backing_dev_info, BLK_RW_ASYNC); 431 432 set_page_writeback(page); 433 err = ceph_osdc_writepages(osdc, ceph_vino(inode), 434 &ci->i_layout, snapc, 435 page_off, len, 436 ci->i_truncate_seq, ci->i_truncate_size, 437 &inode->i_mtime, 438 &page, 1, 0, 0, true); 439 if (err < 0) { 440 dout("writepage setting page/mapping error %d %p\n", err, page); 441 SetPageError(page); 442 mapping_set_error(&inode->i_data, err); 443 if (wbc) 444 wbc->pages_skipped++; 445 } else { 446 dout("writepage cleaned page %p\n", page); 447 err = 0; /* vfs expects us to return 0 */ 448 } 449 page->private = 0; 450 ClearPagePrivate(page); 451 end_page_writeback(page); 452 ceph_put_wrbuffer_cap_refs(ci, 1, snapc); 453 ceph_put_snap_context(snapc); /* page's reference */ 454 out: 455 return err; 456 } 457 458 static int ceph_writepage(struct page *page, struct writeback_control *wbc) 459 { 460 int err; 461 struct inode *inode = page->mapping->host; 462 BUG_ON(!inode); 463 igrab(inode); 464 err = writepage_nounlock(page, wbc); 465 unlock_page(page); 466 iput(inode); 467 return err; 468 } 469 470 471 /* 472 * lame release_pages helper. release_pages() isn't exported to 473 * modules. 474 */ 475 static void ceph_release_pages(struct page **pages, int num) 476 { 477 struct pagevec pvec; 478 int i; 479 480 pagevec_init(&pvec, 0); 481 for (i = 0; i < num; i++) { 482 if (pagevec_add(&pvec, pages[i]) == 0) 483 pagevec_release(&pvec); 484 } 485 pagevec_release(&pvec); 486 } 487 488 489 /* 490 * async writeback completion handler. 491 * 492 * If we get an error, set the mapping error bit, but not the individual 493 * page error bits. 494 */ 495 static void writepages_finish(struct ceph_osd_request *req, 496 struct ceph_msg *msg) 497 { 498 struct inode *inode = req->r_inode; 499 struct ceph_osd_reply_head *replyhead; 500 struct ceph_osd_op *op; 501 struct ceph_inode_info *ci = ceph_inode(inode); 502 unsigned wrote; 503 struct page *page; 504 int i; 505 struct ceph_snap_context *snapc = req->r_snapc; 506 struct address_space *mapping = inode->i_mapping; 507 struct writeback_control *wbc = req->r_wbc; 508 __s32 rc = -EIO; 509 u64 bytes = 0; 510 struct ceph_client *client = ceph_inode_to_client(inode); 511 long writeback_stat; 512 unsigned issued = __ceph_caps_issued(ci, NULL); 513 514 /* parse reply */ 515 replyhead = msg->front.iov_base; 516 WARN_ON(le32_to_cpu(replyhead->num_ops) == 0); 517 op = (void *)(replyhead + 1); 518 rc = le32_to_cpu(replyhead->result); 519 bytes = le64_to_cpu(op->extent.length); 520 521 if (rc >= 0) { 522 /* 523 * Assume we wrote the pages we originally sent. The 524 * osd might reply with fewer pages if our writeback 525 * raced with a truncation and was adjusted at the osd, 526 * so don't believe the reply. 527 */ 528 wrote = req->r_num_pages; 529 } else { 530 wrote = 0; 531 mapping_set_error(mapping, rc); 532 } 533 dout("writepages_finish %p rc %d bytes %llu wrote %d (pages)\n", 534 inode, rc, bytes, wrote); 535 536 /* clean all pages */ 537 for (i = 0; i < req->r_num_pages; i++) { 538 page = req->r_pages[i]; 539 BUG_ON(!page); 540 WARN_ON(!PageUptodate(page)); 541 542 writeback_stat = 543 atomic_long_dec_return(&client->writeback_count); 544 if (writeback_stat < 545 CONGESTION_OFF_THRESH(client->mount_args->congestion_kb)) 546 clear_bdi_congested(&client->backing_dev_info, 547 BLK_RW_ASYNC); 548 549 if (i >= wrote) { 550 dout("inode %p skipping page %p\n", inode, page); 551 wbc->pages_skipped++; 552 } 553 ceph_put_snap_context((void *)page->private); 554 page->private = 0; 555 ClearPagePrivate(page); 556 dout("unlocking %d %p\n", i, page); 557 end_page_writeback(page); 558 559 /* 560 * We lost the cache cap, need to truncate the page before 561 * it is unlocked, otherwise we'd truncate it later in the 562 * page truncation thread, possibly losing some data that 563 * raced its way in 564 */ 565 if ((issued & CEPH_CAP_FILE_CACHE) == 0) 566 generic_error_remove_page(inode->i_mapping, page); 567 568 unlock_page(page); 569 } 570 dout("%p wrote+cleaned %d pages\n", inode, wrote); 571 ceph_put_wrbuffer_cap_refs(ci, req->r_num_pages, snapc); 572 573 ceph_release_pages(req->r_pages, req->r_num_pages); 574 if (req->r_pages_from_pool) 575 mempool_free(req->r_pages, 576 ceph_client(inode->i_sb)->wb_pagevec_pool); 577 else 578 kfree(req->r_pages); 579 ceph_osdc_put_request(req); 580 } 581 582 /* 583 * allocate a page vec, either directly, or if necessary, via a the 584 * mempool. we avoid the mempool if we can because req->r_num_pages 585 * may be less than the maximum write size. 586 */ 587 static void alloc_page_vec(struct ceph_client *client, 588 struct ceph_osd_request *req) 589 { 590 req->r_pages = kmalloc(sizeof(struct page *) * req->r_num_pages, 591 GFP_NOFS); 592 if (!req->r_pages) { 593 req->r_pages = mempool_alloc(client->wb_pagevec_pool, GFP_NOFS); 594 req->r_pages_from_pool = 1; 595 WARN_ON(!req->r_pages); 596 } 597 } 598 599 /* 600 * initiate async writeback 601 */ 602 static int ceph_writepages_start(struct address_space *mapping, 603 struct writeback_control *wbc) 604 { 605 struct inode *inode = mapping->host; 606 struct backing_dev_info *bdi = mapping->backing_dev_info; 607 struct ceph_inode_info *ci = ceph_inode(inode); 608 struct ceph_client *client; 609 pgoff_t index, start, end; 610 int range_whole = 0; 611 int should_loop = 1; 612 pgoff_t max_pages = 0, max_pages_ever = 0; 613 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc; 614 struct pagevec pvec; 615 int done = 0; 616 int rc = 0; 617 unsigned wsize = 1 << inode->i_blkbits; 618 struct ceph_osd_request *req = NULL; 619 int do_sync; 620 u64 snap_size = 0; 621 622 /* 623 * Include a 'sync' in the OSD request if this is a data 624 * integrity write (e.g., O_SYNC write or fsync()), or if our 625 * cap is being revoked. 626 */ 627 do_sync = wbc->sync_mode == WB_SYNC_ALL; 628 if (ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER)) 629 do_sync = 1; 630 dout("writepages_start %p dosync=%d (mode=%s)\n", 631 inode, do_sync, 632 wbc->sync_mode == WB_SYNC_NONE ? "NONE" : 633 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD")); 634 635 client = ceph_inode_to_client(inode); 636 if (client->mount_state == CEPH_MOUNT_SHUTDOWN) { 637 pr_warning("writepage_start %p on forced umount\n", inode); 638 return -EIO; /* we're in a forced umount, don't write! */ 639 } 640 if (client->mount_args->wsize && client->mount_args->wsize < wsize) 641 wsize = client->mount_args->wsize; 642 if (wsize < PAGE_CACHE_SIZE) 643 wsize = PAGE_CACHE_SIZE; 644 max_pages_ever = wsize >> PAGE_CACHE_SHIFT; 645 646 pagevec_init(&pvec, 0); 647 648 /* ?? */ 649 if (wbc->nonblocking && bdi_write_congested(bdi)) { 650 dout(" writepages congested\n"); 651 wbc->encountered_congestion = 1; 652 goto out_final; 653 } 654 655 /* where to start/end? */ 656 if (wbc->range_cyclic) { 657 start = mapping->writeback_index; /* Start from prev offset */ 658 end = -1; 659 dout(" cyclic, start at %lu\n", start); 660 } else { 661 start = wbc->range_start >> PAGE_CACHE_SHIFT; 662 end = wbc->range_end >> PAGE_CACHE_SHIFT; 663 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 664 range_whole = 1; 665 should_loop = 0; 666 dout(" not cyclic, %lu to %lu\n", start, end); 667 } 668 index = start; 669 670 retry: 671 /* find oldest snap context with dirty data */ 672 ceph_put_snap_context(snapc); 673 snapc = get_oldest_context(inode, &snap_size); 674 if (!snapc) { 675 /* hmm, why does writepages get called when there 676 is no dirty data? */ 677 dout(" no snap context with dirty data?\n"); 678 goto out; 679 } 680 dout(" oldest snapc is %p seq %lld (%d snaps)\n", 681 snapc, snapc->seq, snapc->num_snaps); 682 if (last_snapc && snapc != last_snapc) { 683 /* if we switched to a newer snapc, restart our scan at the 684 * start of the original file range. */ 685 dout(" snapc differs from last pass, restarting at %lu\n", 686 index); 687 index = start; 688 } 689 last_snapc = snapc; 690 691 while (!done && index <= end) { 692 unsigned i; 693 int first; 694 pgoff_t next; 695 int pvec_pages, locked_pages; 696 struct page *page; 697 int want; 698 u64 offset, len; 699 struct ceph_osd_request_head *reqhead; 700 struct ceph_osd_op *op; 701 long writeback_stat; 702 703 next = 0; 704 locked_pages = 0; 705 max_pages = max_pages_ever; 706 707 get_more_pages: 708 first = -1; 709 want = min(end - index, 710 min((pgoff_t)PAGEVEC_SIZE, 711 max_pages - (pgoff_t)locked_pages) - 1) 712 + 1; 713 pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index, 714 PAGECACHE_TAG_DIRTY, 715 want); 716 dout("pagevec_lookup_tag got %d\n", pvec_pages); 717 if (!pvec_pages && !locked_pages) 718 break; 719 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) { 720 page = pvec.pages[i]; 721 dout("? %p idx %lu\n", page, page->index); 722 if (locked_pages == 0) 723 lock_page(page); /* first page */ 724 else if (!trylock_page(page)) 725 break; 726 727 /* only dirty pages, or our accounting breaks */ 728 if (unlikely(!PageDirty(page)) || 729 unlikely(page->mapping != mapping)) { 730 dout("!dirty or !mapping %p\n", page); 731 unlock_page(page); 732 break; 733 } 734 if (!wbc->range_cyclic && page->index > end) { 735 dout("end of range %p\n", page); 736 done = 1; 737 unlock_page(page); 738 break; 739 } 740 if (next && (page->index != next)) { 741 dout("not consecutive %p\n", page); 742 unlock_page(page); 743 break; 744 } 745 if (wbc->sync_mode != WB_SYNC_NONE) { 746 dout("waiting on writeback %p\n", page); 747 wait_on_page_writeback(page); 748 } 749 if ((snap_size && page_offset(page) > snap_size) || 750 (!snap_size && 751 page_offset(page) > i_size_read(inode))) { 752 dout("%p page eof %llu\n", page, snap_size ? 753 snap_size : i_size_read(inode)); 754 done = 1; 755 unlock_page(page); 756 break; 757 } 758 if (PageWriteback(page)) { 759 dout("%p under writeback\n", page); 760 unlock_page(page); 761 break; 762 } 763 764 /* only if matching snap context */ 765 pgsnapc = (void *)page->private; 766 if (pgsnapc->seq > snapc->seq) { 767 dout("page snapc %p %lld > oldest %p %lld\n", 768 pgsnapc, pgsnapc->seq, snapc, snapc->seq); 769 unlock_page(page); 770 if (!locked_pages) 771 continue; /* keep looking for snap */ 772 break; 773 } 774 775 if (!clear_page_dirty_for_io(page)) { 776 dout("%p !clear_page_dirty_for_io\n", page); 777 unlock_page(page); 778 break; 779 } 780 781 /* ok */ 782 if (locked_pages == 0) { 783 /* prepare async write request */ 784 offset = page->index << PAGE_CACHE_SHIFT; 785 len = wsize; 786 req = ceph_osdc_new_request(&client->osdc, 787 &ci->i_layout, 788 ceph_vino(inode), 789 offset, &len, 790 CEPH_OSD_OP_WRITE, 791 CEPH_OSD_FLAG_WRITE | 792 CEPH_OSD_FLAG_ONDISK, 793 snapc, do_sync, 794 ci->i_truncate_seq, 795 ci->i_truncate_size, 796 &inode->i_mtime, true, 1); 797 max_pages = req->r_num_pages; 798 799 alloc_page_vec(client, req); 800 req->r_callback = writepages_finish; 801 req->r_inode = inode; 802 req->r_wbc = wbc; 803 } 804 805 /* note position of first page in pvec */ 806 if (first < 0) 807 first = i; 808 dout("%p will write page %p idx %lu\n", 809 inode, page, page->index); 810 811 writeback_stat = atomic_long_inc_return(&client->writeback_count); 812 if (writeback_stat > CONGESTION_ON_THRESH(client->mount_args->congestion_kb)) { 813 set_bdi_congested(&client->backing_dev_info, BLK_RW_ASYNC); 814 } 815 816 set_page_writeback(page); 817 req->r_pages[locked_pages] = page; 818 locked_pages++; 819 next = page->index + 1; 820 } 821 822 /* did we get anything? */ 823 if (!locked_pages) 824 goto release_pvec_pages; 825 if (i) { 826 int j; 827 BUG_ON(!locked_pages || first < 0); 828 829 if (pvec_pages && i == pvec_pages && 830 locked_pages < max_pages) { 831 dout("reached end pvec, trying for more\n"); 832 pagevec_reinit(&pvec); 833 goto get_more_pages; 834 } 835 836 /* shift unused pages over in the pvec... we 837 * will need to release them below. */ 838 for (j = i; j < pvec_pages; j++) { 839 dout(" pvec leftover page %p\n", 840 pvec.pages[j]); 841 pvec.pages[j-i+first] = pvec.pages[j]; 842 } 843 pvec.nr -= i-first; 844 } 845 846 /* submit the write */ 847 offset = req->r_pages[0]->index << PAGE_CACHE_SHIFT; 848 len = min((snap_size ? snap_size : i_size_read(inode)) - offset, 849 (u64)locked_pages << PAGE_CACHE_SHIFT); 850 dout("writepages got %d pages at %llu~%llu\n", 851 locked_pages, offset, len); 852 853 /* revise final length, page count */ 854 req->r_num_pages = locked_pages; 855 reqhead = req->r_request->front.iov_base; 856 op = (void *)(reqhead + 1); 857 op->extent.length = cpu_to_le64(len); 858 op->payload_len = cpu_to_le32(len); 859 req->r_request->hdr.data_len = cpu_to_le32(len); 860 861 ceph_osdc_start_request(&client->osdc, req, true); 862 req = NULL; 863 864 /* continue? */ 865 index = next; 866 wbc->nr_to_write -= locked_pages; 867 if (wbc->nr_to_write <= 0) 868 done = 1; 869 870 release_pvec_pages: 871 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr, 872 pvec.nr ? pvec.pages[0] : NULL); 873 pagevec_release(&pvec); 874 875 if (locked_pages && !done) 876 goto retry; 877 } 878 879 if (should_loop && !done) { 880 /* more to do; loop back to beginning of file */ 881 dout("writepages looping back to beginning of file\n"); 882 should_loop = 0; 883 index = 0; 884 goto retry; 885 } 886 887 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 888 mapping->writeback_index = index; 889 890 out: 891 if (req) 892 ceph_osdc_put_request(req); 893 if (rc > 0) 894 rc = 0; /* vfs expects us to return 0 */ 895 ceph_put_snap_context(snapc); 896 dout("writepages done, rc = %d\n", rc); 897 out_final: 898 return rc; 899 } 900 901 902 903 /* 904 * See if a given @snapc is either writeable, or already written. 905 */ 906 static int context_is_writeable_or_written(struct inode *inode, 907 struct ceph_snap_context *snapc) 908 { 909 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL); 910 int ret = !oldest || snapc->seq <= oldest->seq; 911 912 ceph_put_snap_context(oldest); 913 return ret; 914 } 915 916 /* 917 * We are only allowed to write into/dirty the page if the page is 918 * clean, or already dirty within the same snap context. 919 * 920 * called with page locked. 921 * return success with page locked, 922 * or any failure (incl -EAGAIN) with page unlocked. 923 */ 924 static int ceph_update_writeable_page(struct file *file, 925 loff_t pos, unsigned len, 926 struct page *page) 927 { 928 struct inode *inode = file->f_dentry->d_inode; 929 struct ceph_inode_info *ci = ceph_inode(inode); 930 struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc; 931 loff_t page_off = pos & PAGE_CACHE_MASK; 932 int pos_in_page = pos & ~PAGE_CACHE_MASK; 933 int end_in_page = pos_in_page + len; 934 loff_t i_size; 935 int r; 936 struct ceph_snap_context *snapc, *oldest; 937 938 retry_locked: 939 /* writepages currently holds page lock, but if we change that later, */ 940 wait_on_page_writeback(page); 941 942 /* check snap context */ 943 BUG_ON(!ci->i_snap_realm); 944 down_read(&mdsc->snap_rwsem); 945 BUG_ON(!ci->i_snap_realm->cached_context); 946 snapc = (void *)page->private; 947 if (snapc && snapc != ci->i_head_snapc) { 948 /* 949 * this page is already dirty in another (older) snap 950 * context! is it writeable now? 951 */ 952 oldest = get_oldest_context(inode, NULL); 953 up_read(&mdsc->snap_rwsem); 954 955 if (snapc->seq > oldest->seq) { 956 ceph_put_snap_context(oldest); 957 dout(" page %p snapc %p not current or oldest\n", 958 page, snapc); 959 /* 960 * queue for writeback, and wait for snapc to 961 * be writeable or written 962 */ 963 snapc = ceph_get_snap_context(snapc); 964 unlock_page(page); 965 ceph_queue_writeback(inode); 966 r = wait_event_interruptible(ci->i_cap_wq, 967 context_is_writeable_or_written(inode, snapc)); 968 ceph_put_snap_context(snapc); 969 if (r == -ERESTARTSYS) 970 return r; 971 return -EAGAIN; 972 } 973 ceph_put_snap_context(oldest); 974 975 /* yay, writeable, do it now (without dropping page lock) */ 976 dout(" page %p snapc %p not current, but oldest\n", 977 page, snapc); 978 if (!clear_page_dirty_for_io(page)) 979 goto retry_locked; 980 r = writepage_nounlock(page, NULL); 981 if (r < 0) 982 goto fail_nosnap; 983 goto retry_locked; 984 } 985 986 if (PageUptodate(page)) { 987 dout(" page %p already uptodate\n", page); 988 return 0; 989 } 990 991 /* full page? */ 992 if (pos_in_page == 0 && len == PAGE_CACHE_SIZE) 993 return 0; 994 995 /* past end of file? */ 996 i_size = inode->i_size; /* caller holds i_mutex */ 997 998 if (i_size + len > inode->i_sb->s_maxbytes) { 999 /* file is too big */ 1000 r = -EINVAL; 1001 goto fail; 1002 } 1003 1004 if (page_off >= i_size || 1005 (pos_in_page == 0 && (pos+len) >= i_size && 1006 end_in_page - pos_in_page != PAGE_CACHE_SIZE)) { 1007 dout(" zeroing %p 0 - %d and %d - %d\n", 1008 page, pos_in_page, end_in_page, (int)PAGE_CACHE_SIZE); 1009 zero_user_segments(page, 1010 0, pos_in_page, 1011 end_in_page, PAGE_CACHE_SIZE); 1012 return 0; 1013 } 1014 1015 /* we need to read it. */ 1016 up_read(&mdsc->snap_rwsem); 1017 r = readpage_nounlock(file, page); 1018 if (r < 0) 1019 goto fail_nosnap; 1020 goto retry_locked; 1021 1022 fail: 1023 up_read(&mdsc->snap_rwsem); 1024 fail_nosnap: 1025 unlock_page(page); 1026 return r; 1027 } 1028 1029 /* 1030 * We are only allowed to write into/dirty the page if the page is 1031 * clean, or already dirty within the same snap context. 1032 */ 1033 static int ceph_write_begin(struct file *file, struct address_space *mapping, 1034 loff_t pos, unsigned len, unsigned flags, 1035 struct page **pagep, void **fsdata) 1036 { 1037 struct inode *inode = file->f_dentry->d_inode; 1038 struct page *page; 1039 pgoff_t index = pos >> PAGE_CACHE_SHIFT; 1040 int r; 1041 1042 do { 1043 /* get a page */ 1044 page = grab_cache_page_write_begin(mapping, index, 0); 1045 if (!page) 1046 return -ENOMEM; 1047 *pagep = page; 1048 1049 dout("write_begin file %p inode %p page %p %d~%d\n", file, 1050 inode, page, (int)pos, (int)len); 1051 1052 r = ceph_update_writeable_page(file, pos, len, page); 1053 } while (r == -EAGAIN); 1054 1055 return r; 1056 } 1057 1058 /* 1059 * we don't do anything in here that simple_write_end doesn't do 1060 * except adjust dirty page accounting and drop read lock on 1061 * mdsc->snap_rwsem. 1062 */ 1063 static int ceph_write_end(struct file *file, struct address_space *mapping, 1064 loff_t pos, unsigned len, unsigned copied, 1065 struct page *page, void *fsdata) 1066 { 1067 struct inode *inode = file->f_dentry->d_inode; 1068 struct ceph_client *client = ceph_inode_to_client(inode); 1069 struct ceph_mds_client *mdsc = &client->mdsc; 1070 unsigned from = pos & (PAGE_CACHE_SIZE - 1); 1071 int check_cap = 0; 1072 1073 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file, 1074 inode, page, (int)pos, (int)copied, (int)len); 1075 1076 /* zero the stale part of the page if we did a short copy */ 1077 if (copied < len) 1078 zero_user_segment(page, from+copied, len); 1079 1080 /* did file size increase? */ 1081 /* (no need for i_size_read(); we caller holds i_mutex */ 1082 if (pos+copied > inode->i_size) 1083 check_cap = ceph_inode_set_size(inode, pos+copied); 1084 1085 if (!PageUptodate(page)) 1086 SetPageUptodate(page); 1087 1088 set_page_dirty(page); 1089 1090 unlock_page(page); 1091 up_read(&mdsc->snap_rwsem); 1092 page_cache_release(page); 1093 1094 if (check_cap) 1095 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL); 1096 1097 return copied; 1098 } 1099 1100 /* 1101 * we set .direct_IO to indicate direct io is supported, but since we 1102 * intercept O_DIRECT reads and writes early, this function should 1103 * never get called. 1104 */ 1105 static ssize_t ceph_direct_io(int rw, struct kiocb *iocb, 1106 const struct iovec *iov, 1107 loff_t pos, unsigned long nr_segs) 1108 { 1109 WARN_ON(1); 1110 return -EINVAL; 1111 } 1112 1113 const struct address_space_operations ceph_aops = { 1114 .readpage = ceph_readpage, 1115 .readpages = ceph_readpages, 1116 .writepage = ceph_writepage, 1117 .writepages = ceph_writepages_start, 1118 .write_begin = ceph_write_begin, 1119 .write_end = ceph_write_end, 1120 .set_page_dirty = ceph_set_page_dirty, 1121 .invalidatepage = ceph_invalidatepage, 1122 .releasepage = ceph_releasepage, 1123 .direct_IO = ceph_direct_io, 1124 }; 1125 1126 1127 /* 1128 * vm ops 1129 */ 1130 1131 /* 1132 * Reuse write_begin here for simplicity. 1133 */ 1134 static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 1135 { 1136 struct inode *inode = vma->vm_file->f_dentry->d_inode; 1137 struct page *page = vmf->page; 1138 struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc; 1139 loff_t off = page->index << PAGE_CACHE_SHIFT; 1140 loff_t size, len; 1141 int ret; 1142 1143 size = i_size_read(inode); 1144 if (off + PAGE_CACHE_SIZE <= size) 1145 len = PAGE_CACHE_SIZE; 1146 else 1147 len = size & ~PAGE_CACHE_MASK; 1148 1149 dout("page_mkwrite %p %llu~%llu page %p idx %lu\n", inode, 1150 off, len, page, page->index); 1151 1152 lock_page(page); 1153 1154 ret = VM_FAULT_NOPAGE; 1155 if ((off > size) || 1156 (page->mapping != inode->i_mapping)) 1157 goto out; 1158 1159 ret = ceph_update_writeable_page(vma->vm_file, off, len, page); 1160 if (ret == 0) { 1161 /* success. we'll keep the page locked. */ 1162 set_page_dirty(page); 1163 up_read(&mdsc->snap_rwsem); 1164 ret = VM_FAULT_LOCKED; 1165 } else { 1166 if (ret == -ENOMEM) 1167 ret = VM_FAULT_OOM; 1168 else 1169 ret = VM_FAULT_SIGBUS; 1170 } 1171 out: 1172 dout("page_mkwrite %p %llu~%llu = %d\n", inode, off, len, ret); 1173 if (ret != VM_FAULT_LOCKED) 1174 unlock_page(page); 1175 return ret; 1176 } 1177 1178 static struct vm_operations_struct ceph_vmops = { 1179 .fault = filemap_fault, 1180 .page_mkwrite = ceph_page_mkwrite, 1181 }; 1182 1183 int ceph_mmap(struct file *file, struct vm_area_struct *vma) 1184 { 1185 struct address_space *mapping = file->f_mapping; 1186 1187 if (!mapping->a_ops->readpage) 1188 return -ENOEXEC; 1189 file_accessed(file); 1190 vma->vm_ops = &ceph_vmops; 1191 vma->vm_flags |= VM_CAN_NONLINEAR; 1192 return 0; 1193 } 1194