Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bcache: Break up struct search

With all the recent refactoring around struct btree op struct search has
gotten rather large.

But we can now easily break it up in a different way - we break out
struct btree_insert_op which is for inserting data into the cache, and
that's now what the copying gc code uses - struct search is now specific
to request.c

Signed-off-by: Kent Overstreet <kmo@daterainc.com>

+448 -465
+9 -29
drivers/md/bcache/debug.c
··· 8 8 #include "bcache.h" 9 9 #include "btree.h" 10 10 #include "debug.h" 11 - #include "request.h" 12 11 13 12 #include <linux/console.h> 14 13 #include <linux/debugfs.h> ··· 175 176 mutex_unlock(&b->c->verify_lock); 176 177 } 177 178 178 - static void data_verify_endio(struct bio *bio, int error) 179 - { 180 - struct closure *cl = bio->bi_private; 181 - closure_put(cl); 182 - } 183 - 184 - void bch_data_verify(struct search *s) 179 + void bch_data_verify(struct cached_dev *dc, struct bio *bio) 185 180 { 186 181 char name[BDEVNAME_SIZE]; 187 - struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); 188 - struct closure *cl = &s->cl; 189 182 struct bio *check; 190 183 struct bio_vec *bv; 191 184 int i; 192 185 193 - if (!s->unaligned_bvec) 194 - bio_for_each_segment(bv, s->orig_bio, i) 195 - bv->bv_offset = 0, bv->bv_len = PAGE_SIZE; 196 - 197 - check = bio_clone(s->orig_bio, GFP_NOIO); 186 + check = bio_clone(bio, GFP_NOIO); 198 187 if (!check) 199 188 return; 200 189 201 190 if (bio_alloc_pages(check, GFP_NOIO)) 202 191 goto out_put; 203 192 204 - check->bi_rw = READ_SYNC; 205 - check->bi_private = cl; 206 - check->bi_end_io = data_verify_endio; 193 + submit_bio_wait(READ_SYNC, check); 207 194 208 - closure_bio_submit(check, cl, &dc->disk); 209 - closure_sync(cl); 210 - 211 - bio_for_each_segment(bv, s->orig_bio, i) { 212 - void *p1 = kmap(bv->bv_page); 213 - void *p2 = kmap(check->bi_io_vec[i].bv_page); 195 + bio_for_each_segment(bv, bio, i) { 196 + void *p1 = kmap_atomic(bv->bv_page); 197 + void *p2 = page_address(check->bi_io_vec[i].bv_page); 214 198 215 199 if (memcmp(p1 + bv->bv_offset, 216 200 p2 + bv->bv_offset, ··· 201 219 printk(KERN_ERR 202 220 "bcache (%s): verify failed at sector %llu\n", 203 221 bdevname(dc->bdev, name), 204 - (uint64_t) s->orig_bio->bi_sector); 205 - 206 - kunmap(bv->bv_page); 207 - kunmap(check->bi_io_vec[i].bv_page); 222 + (uint64_t) bio->bi_sector); 223 + kunmap_atomic(p1); 208 224 } 209 225 210 - __bio_for_each_segment(bv, check, i, 0) 226 + bio_for_each_segment_all(bv, check, i) 211 227 __free_page(bv->bv_page); 212 228 out_put: 213 229 bio_put(check);
+2 -2
drivers/md/bcache/debug.h
··· 29 29 #ifdef CONFIG_BCACHE_DEBUG 30 30 31 31 void bch_btree_verify(struct btree *, struct bset *); 32 - void bch_data_verify(struct search *); 32 + void bch_data_verify(struct cached_dev *, struct bio *); 33 33 34 34 #else /* DEBUG */ 35 35 36 36 static inline void bch_btree_verify(struct btree *b, struct bset *i) {} 37 - static inline void bch_data_verify(struct search *s) {}; 37 + static inline void bch_data_verify(struct cached_dev *dc, struct bio *bio) {}; 38 38 39 39 #endif 40 40
+27 -29
drivers/md/bcache/movinggc.c
··· 12 12 #include <trace/events/bcache.h> 13 13 14 14 struct moving_io { 15 + struct closure cl; 15 16 struct keybuf_key *w; 16 - struct search s; 17 + struct data_insert_op op; 17 18 struct bbio bio; 18 19 }; 19 20 ··· 39 38 40 39 static void moving_io_destructor(struct closure *cl) 41 40 { 42 - struct moving_io *io = container_of(cl, struct moving_io, s.cl); 41 + struct moving_io *io = container_of(cl, struct moving_io, cl); 43 42 kfree(io); 44 43 } 45 44 46 45 static void write_moving_finish(struct closure *cl) 47 46 { 48 - struct moving_io *io = container_of(cl, struct moving_io, s.cl); 47 + struct moving_io *io = container_of(cl, struct moving_io, cl); 49 48 struct bio *bio = &io->bio.bio; 50 49 struct bio_vec *bv; 51 50 int i; ··· 53 52 bio_for_each_segment_all(bv, bio, i) 54 53 __free_page(bv->bv_page); 55 54 56 - if (io->s.insert_collision) 55 + if (io->op.replace_collision) 57 56 trace_bcache_gc_copy_collision(&io->w->key); 58 57 59 - bch_keybuf_del(&io->s.c->moving_gc_keys, io->w); 58 + bch_keybuf_del(&io->op.c->moving_gc_keys, io->w); 60 59 61 - up(&io->s.c->moving_in_flight); 60 + up(&io->op.c->moving_in_flight); 62 61 63 62 closure_return_with_destructor(cl, moving_io_destructor); 64 63 } ··· 66 65 static void read_moving_endio(struct bio *bio, int error) 67 66 { 68 67 struct moving_io *io = container_of(bio->bi_private, 69 - struct moving_io, s.cl); 68 + struct moving_io, cl); 70 69 71 70 if (error) 72 - io->s.error = error; 71 + io->op.error = error; 73 72 74 - bch_bbio_endio(io->s.c, bio, error, "reading data to move"); 73 + bch_bbio_endio(io->op.c, bio, error, "reading data to move"); 75 74 } 76 75 77 76 static void moving_init(struct moving_io *io) ··· 85 84 bio->bi_size = KEY_SIZE(&io->w->key) << 9; 86 85 bio->bi_max_vecs = DIV_ROUND_UP(KEY_SIZE(&io->w->key), 87 86 PAGE_SECTORS); 88 - bio->bi_private = &io->s.cl; 87 + bio->bi_private = &io->cl; 89 88 bio->bi_io_vec = bio->bi_inline_vecs; 90 89 bch_bio_map(bio, NULL); 91 90 } 92 91 93 92 static void write_moving(struct closure *cl) 94 93 { 95 - struct search *s = container_of(cl, struct search, cl); 96 - struct moving_io *io = container_of(s, struct moving_io, s); 94 + struct moving_io *io = container_of(cl, struct moving_io, cl); 95 + struct data_insert_op *op = &io->op; 97 96 98 - if (!s->error) { 97 + if (!op->error) { 99 98 moving_init(io); 100 99 101 - io->bio.bio.bi_sector = KEY_START(&io->w->key); 102 - s->op.lock = -1; 103 - s->write_prio = 1; 104 - s->cache_bio = &io->bio.bio; 100 + io->bio.bio.bi_sector = KEY_START(&io->w->key); 101 + op->write_prio = 1; 102 + op->bio = &io->bio.bio; 105 103 106 - s->writeback = KEY_DIRTY(&io->w->key); 107 - s->csum = KEY_CSUM(&io->w->key); 104 + op->writeback = KEY_DIRTY(&io->w->key); 105 + op->csum = KEY_CSUM(&io->w->key); 108 106 109 - bkey_copy(&s->replace_key, &io->w->key); 110 - s->replace = true; 107 + bkey_copy(&op->replace_key, &io->w->key); 108 + op->replace = true; 111 109 112 - closure_init(&s->btree, cl); 113 - bch_data_insert(&s->btree); 110 + closure_call(&op->cl, bch_data_insert, NULL, cl); 114 111 } 115 112 116 113 continue_at(cl, write_moving_finish, system_wq); ··· 116 117 117 118 static void read_moving_submit(struct closure *cl) 118 119 { 119 - struct search *s = container_of(cl, struct search, cl); 120 - struct moving_io *io = container_of(s, struct moving_io, s); 120 + struct moving_io *io = container_of(cl, struct moving_io, cl); 121 121 struct bio *bio = &io->bio.bio; 122 122 123 - bch_submit_bbio(bio, s->c, &io->w->key, 0); 123 + bch_submit_bbio(bio, io->op.c, &io->w->key, 0); 124 124 125 125 continue_at(cl, write_moving, system_wq); 126 126 } ··· 149 151 150 152 w->private = io; 151 153 io->w = w; 152 - io->s.inode = KEY_INODE(&w->key); 153 - io->s.c = c; 154 + io->op.inode = KEY_INODE(&w->key); 155 + io->op.c = c; 154 156 155 157 moving_init(io); 156 158 bio = &io->bio.bio; ··· 164 166 trace_bcache_gc_copy(&w->key); 165 167 166 168 down(&c->moving_in_flight); 167 - closure_call(&io->s.cl, read_moving_submit, NULL, &cl); 169 + closure_call(&io->cl, read_moving_submit, NULL, &cl); 168 170 } 169 171 170 172 if (0) {
+372 -346
drivers/md/bcache/request.c
··· 215 215 216 216 static void bch_data_insert_keys(struct closure *cl) 217 217 { 218 - struct search *s = container_of(cl, struct search, btree); 218 + struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); 219 219 atomic_t *journal_ref = NULL; 220 - struct bkey *replace_key = s->replace ? &s->replace_key : NULL; 220 + struct bkey *replace_key = op->replace ? &op->replace_key : NULL; 221 221 int ret; 222 222 223 223 /* ··· 232 232 closure_sync(&s->cl); 233 233 #endif 234 234 235 - if (s->write) 236 - journal_ref = bch_journal(s->c, &s->insert_keys, 237 - s->flush_journal 238 - ? &s->cl : NULL); 235 + if (!op->replace) 236 + journal_ref = bch_journal(op->c, &op->insert_keys, 237 + op->flush_journal ? cl : NULL); 239 238 240 - ret = bch_btree_insert(s->c, &s->insert_keys, 239 + ret = bch_btree_insert(op->c, &op->insert_keys, 241 240 journal_ref, replace_key); 242 241 if (ret == -ESRCH) { 243 - s->insert_collision = true; 242 + op->replace_collision = true; 244 243 } else if (ret) { 245 - s->error = -ENOMEM; 246 - s->insert_data_done = true; 244 + op->error = -ENOMEM; 245 + op->insert_data_done = true; 247 246 } 248 247 249 248 if (journal_ref) 250 249 atomic_dec_bug(journal_ref); 251 250 252 - if (!s->insert_data_done) 251 + if (!op->insert_data_done) 253 252 continue_at(cl, bch_data_insert_start, bcache_wq); 254 253 255 - bch_keylist_free(&s->insert_keys); 254 + bch_keylist_free(&op->insert_keys); 256 255 closure_return(cl); 257 256 } 258 257 ··· 348 349 * 349 350 * If s->writeback is true, will not fail. 350 351 */ 351 - static bool bch_alloc_sectors(struct bkey *k, unsigned sectors, 352 - struct search *s) 352 + static bool bch_alloc_sectors(struct data_insert_op *op, 353 + struct bkey *k, unsigned sectors) 353 354 { 354 - struct cache_set *c = s->c; 355 + struct cache_set *c = op->c; 355 356 struct open_bucket *b; 356 357 BKEY_PADDED(key) alloc; 357 358 unsigned i; ··· 366 367 bkey_init(&alloc.key); 367 368 spin_lock(&c->data_bucket_lock); 368 369 369 - while (!(b = pick_data_bucket(c, k, s->task, &alloc.key))) { 370 - unsigned watermark = s->write_prio 370 + while (!(b = pick_data_bucket(c, k, op->task, &alloc.key))) { 371 + unsigned watermark = op->write_prio 371 372 ? WATERMARK_MOVINGGC 372 373 : WATERMARK_NONE; 373 374 374 375 spin_unlock(&c->data_bucket_lock); 375 376 376 377 if (bch_bucket_alloc_set(c, watermark, &alloc.key, 377 - 1, s->writeback)) 378 + 1, op->writeback)) 378 379 return false; 379 380 380 381 spin_lock(&c->data_bucket_lock); ··· 408 409 */ 409 410 list_move_tail(&b->list, &c->data_buckets); 410 411 bkey_copy_key(&b->key, k); 411 - b->last = s->task; 412 + b->last = op->task; 412 413 413 414 b->sectors_free -= sectors; 414 415 ··· 437 438 438 439 static void bch_data_invalidate(struct closure *cl) 439 440 { 440 - struct search *s = container_of(cl, struct search, btree); 441 - struct bio *bio = s->cache_bio; 441 + struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); 442 + struct bio *bio = op->bio; 442 443 443 444 pr_debug("invalidating %i sectors from %llu", 444 445 bio_sectors(bio), (uint64_t) bio->bi_sector); ··· 446 447 while (bio_sectors(bio)) { 447 448 unsigned len = min(bio_sectors(bio), 1U << 14); 448 449 449 - if (bch_keylist_realloc(&s->insert_keys, 0, s->c)) 450 + if (bch_keylist_realloc(&op->insert_keys, 0, op->c)) 450 451 goto out; 451 452 452 453 bio->bi_sector += len; 453 454 bio->bi_size -= len << 9; 454 455 455 - bch_keylist_add(&s->insert_keys, 456 - &KEY(s->inode, bio->bi_sector, len)); 456 + bch_keylist_add(&op->insert_keys, 457 + &KEY(op->inode, bio->bi_sector, len)); 457 458 } 458 459 459 - s->insert_data_done = true; 460 + op->insert_data_done = true; 460 461 bio_put(bio); 461 462 out: 462 463 continue_at(cl, bch_data_insert_keys, bcache_wq); ··· 464 465 465 466 static void bch_data_insert_error(struct closure *cl) 466 467 { 467 - struct search *s = container_of(cl, struct search, btree); 468 + struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); 468 469 469 470 /* 470 471 * Our data write just errored, which means we've got a bunch of keys to ··· 475 476 * from the keys we'll accomplish just that. 476 477 */ 477 478 478 - struct bkey *src = s->insert_keys.keys, *dst = s->insert_keys.keys; 479 + struct bkey *src = op->insert_keys.keys, *dst = op->insert_keys.keys; 479 480 480 - while (src != s->insert_keys.top) { 481 + while (src != op->insert_keys.top) { 481 482 struct bkey *n = bkey_next(src); 482 483 483 484 SET_KEY_PTRS(src, 0); ··· 487 488 src = n; 488 489 } 489 490 490 - s->insert_keys.top = dst; 491 + op->insert_keys.top = dst; 491 492 492 493 bch_data_insert_keys(cl); 493 494 } ··· 495 496 static void bch_data_insert_endio(struct bio *bio, int error) 496 497 { 497 498 struct closure *cl = bio->bi_private; 498 - struct search *s = container_of(cl, struct search, btree); 499 + struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); 499 500 500 501 if (error) { 501 502 /* TODO: We could try to recover from this. */ 502 - if (s->writeback) 503 - s->error = error; 504 - else if (s->write) 503 + if (op->writeback) 504 + op->error = error; 505 + else if (!op->replace) 505 506 set_closure_fn(cl, bch_data_insert_error, bcache_wq); 506 507 else 507 508 set_closure_fn(cl, NULL, NULL); 508 509 } 509 510 510 - bch_bbio_endio(s->c, bio, error, "writing data to cache"); 511 + bch_bbio_endio(op->c, bio, error, "writing data to cache"); 511 512 } 512 513 513 514 static void bch_data_insert_start(struct closure *cl) 514 515 { 515 - struct search *s = container_of(cl, struct search, btree); 516 - struct bio *bio = s->cache_bio, *n; 516 + struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); 517 + struct bio *bio = op->bio, *n; 517 518 518 - if (s->bypass) 519 + if (op->bypass) 519 520 return bch_data_invalidate(cl); 520 521 521 - if (atomic_sub_return(bio_sectors(bio), &s->c->sectors_to_gc) < 0) { 522 - set_gc_sectors(s->c); 523 - wake_up_gc(s->c); 522 + if (atomic_sub_return(bio_sectors(bio), &op->c->sectors_to_gc) < 0) { 523 + set_gc_sectors(op->c); 524 + wake_up_gc(op->c); 524 525 } 525 526 526 527 /* ··· 532 533 do { 533 534 unsigned i; 534 535 struct bkey *k; 535 - struct bio_set *split = s->d 536 - ? s->d->bio_split : s->c->bio_split; 536 + struct bio_set *split = op->c->bio_split; 537 537 538 538 /* 1 for the device pointer and 1 for the chksum */ 539 - if (bch_keylist_realloc(&s->insert_keys, 540 - 1 + (s->csum ? 1 : 0), 541 - s->c)) 539 + if (bch_keylist_realloc(&op->insert_keys, 540 + 1 + (op->csum ? 1 : 0), 541 + op->c)) 542 542 continue_at(cl, bch_data_insert_keys, bcache_wq); 543 543 544 - k = s->insert_keys.top; 544 + k = op->insert_keys.top; 545 545 bkey_init(k); 546 - SET_KEY_INODE(k, s->inode); 546 + SET_KEY_INODE(k, op->inode); 547 547 SET_KEY_OFFSET(k, bio->bi_sector); 548 548 549 - if (!bch_alloc_sectors(k, bio_sectors(bio), s)) 549 + if (!bch_alloc_sectors(op, k, bio_sectors(bio))) 550 550 goto err; 551 551 552 552 n = bch_bio_split(bio, KEY_SIZE(k), GFP_NOIO, split); ··· 553 555 n->bi_end_io = bch_data_insert_endio; 554 556 n->bi_private = cl; 555 557 556 - if (s->writeback) { 558 + if (op->writeback) { 557 559 SET_KEY_DIRTY(k, true); 558 560 559 561 for (i = 0; i < KEY_PTRS(k); i++) 560 - SET_GC_MARK(PTR_BUCKET(s->c, k, i), 562 + SET_GC_MARK(PTR_BUCKET(op->c, k, i), 561 563 GC_MARK_DIRTY); 562 564 } 563 565 564 - SET_KEY_CSUM(k, s->csum); 566 + SET_KEY_CSUM(k, op->csum); 565 567 if (KEY_CSUM(k)) 566 568 bio_csum(n, k); 567 569 568 570 trace_bcache_cache_insert(k); 569 - bch_keylist_push(&s->insert_keys); 571 + bch_keylist_push(&op->insert_keys); 570 572 571 573 n->bi_rw |= REQ_WRITE; 572 - bch_submit_bbio(n, s->c, k, 0); 574 + bch_submit_bbio(n, op->c, k, 0); 573 575 } while (n != bio); 574 576 575 - s->insert_data_done = true; 577 + op->insert_data_done = true; 576 578 continue_at(cl, bch_data_insert_keys, bcache_wq); 577 579 err: 578 580 /* bch_alloc_sectors() blocks if s->writeback = true */ 579 - BUG_ON(s->writeback); 581 + BUG_ON(op->writeback); 580 582 581 583 /* 582 584 * But if it's not a writeback write we'd rather just bail out if ··· 584 586 * we might be starving btree writes for gc or something. 585 587 */ 586 588 587 - if (s->write) { 589 + if (!op->replace) { 588 590 /* 589 591 * Writethrough write: We can't complete the write until we've 590 592 * updated the index. But we don't want to delay the write while 591 593 * we wait for buckets to be freed up, so just invalidate the 592 594 * rest of the write. 593 595 */ 594 - s->bypass = true; 596 + op->bypass = true; 595 597 return bch_data_invalidate(cl); 596 598 } else { 597 599 /* 598 600 * From a cache miss, we can just insert the keys for the data 599 601 * we have written or bail out if we didn't do anything. 600 602 */ 601 - s->insert_data_done = true; 603 + op->insert_data_done = true; 602 604 bio_put(bio); 603 605 604 - if (!bch_keylist_empty(&s->insert_keys)) 606 + if (!bch_keylist_empty(&op->insert_keys)) 605 607 continue_at(cl, bch_data_insert_keys, bcache_wq); 606 608 else 607 609 closure_return(cl); ··· 629 631 */ 630 632 void bch_data_insert(struct closure *cl) 631 633 { 632 - struct search *s = container_of(cl, struct search, btree); 634 + struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); 633 635 634 - bch_keylist_init(&s->insert_keys); 635 - bio_get(s->cache_bio); 636 + trace_bcache_write(op->bio, op->writeback, op->bypass); 637 + 638 + bch_keylist_init(&op->insert_keys); 639 + bio_get(op->bio); 636 640 bch_data_insert_start(cl); 637 641 } 638 642 639 - /* Cache lookup */ 640 - 641 - static void bch_cache_read_endio(struct bio *bio, int error) 642 - { 643 - struct bbio *b = container_of(bio, struct bbio, bio); 644 - struct closure *cl = bio->bi_private; 645 - struct search *s = container_of(cl, struct search, cl); 646 - 647 - /* 648 - * If the bucket was reused while our bio was in flight, we might have 649 - * read the wrong data. Set s->error but not error so it doesn't get 650 - * counted against the cache device, but we'll still reread the data 651 - * from the backing device. 652 - */ 653 - 654 - if (error) 655 - s->error = error; 656 - else if (ptr_stale(s->c, &b->key, 0)) { 657 - atomic_long_inc(&s->c->cache_read_races); 658 - s->error = -EINTR; 659 - } 660 - 661 - bch_bbio_endio(s->c, bio, error, "reading from cache"); 662 - } 663 - 664 - /* 665 - * Read from a single key, handling the initial cache miss if the key starts in 666 - * the middle of the bio 667 - */ 668 - static int cache_lookup_fn(struct btree_op *op, struct btree *b, struct bkey *k) 669 - { 670 - struct search *s = container_of(op, struct search, op); 671 - struct bio *n, *bio = &s->bio.bio; 672 - struct bkey *bio_key; 673 - unsigned ptr; 674 - 675 - if (bkey_cmp(k, &KEY(s->inode, bio->bi_sector, 0)) <= 0) 676 - return MAP_CONTINUE; 677 - 678 - if (KEY_INODE(k) != s->inode || 679 - KEY_START(k) > bio->bi_sector) { 680 - unsigned bio_sectors = bio_sectors(bio); 681 - unsigned sectors = KEY_INODE(k) == s->inode 682 - ? min_t(uint64_t, INT_MAX, 683 - KEY_START(k) - bio->bi_sector) 684 - : INT_MAX; 685 - 686 - int ret = s->d->cache_miss(b, s, bio, sectors); 687 - if (ret != MAP_CONTINUE) 688 - return ret; 689 - 690 - /* if this was a complete miss we shouldn't get here */ 691 - BUG_ON(bio_sectors <= sectors); 692 - } 693 - 694 - if (!KEY_SIZE(k)) 695 - return MAP_CONTINUE; 696 - 697 - /* XXX: figure out best pointer - for multiple cache devices */ 698 - ptr = 0; 699 - 700 - PTR_BUCKET(b->c, k, ptr)->prio = INITIAL_PRIO; 701 - 702 - n = bch_bio_split(bio, min_t(uint64_t, INT_MAX, 703 - KEY_OFFSET(k) - bio->bi_sector), 704 - GFP_NOIO, s->d->bio_split); 705 - 706 - bio_key = &container_of(n, struct bbio, bio)->key; 707 - bch_bkey_copy_single_ptr(bio_key, k, ptr); 708 - 709 - bch_cut_front(&KEY(s->inode, n->bi_sector, 0), bio_key); 710 - bch_cut_back(&KEY(s->inode, bio_end_sector(n), 0), bio_key); 711 - 712 - n->bi_end_io = bch_cache_read_endio; 713 - n->bi_private = &s->cl; 714 - 715 - /* 716 - * The bucket we're reading from might be reused while our bio 717 - * is in flight, and we could then end up reading the wrong 718 - * data. 719 - * 720 - * We guard against this by checking (in cache_read_endio()) if 721 - * the pointer is stale again; if so, we treat it as an error 722 - * and reread from the backing device (but we don't pass that 723 - * error up anywhere). 724 - */ 725 - 726 - __bch_submit_bbio(n, b->c); 727 - return n == bio ? MAP_DONE : MAP_CONTINUE; 728 - } 729 - 730 - static void cache_lookup(struct closure *cl) 731 - { 732 - struct search *s = container_of(cl, struct search, btree); 733 - struct bio *bio = &s->bio.bio; 734 - 735 - int ret = bch_btree_map_keys(&s->op, s->c, 736 - &KEY(s->inode, bio->bi_sector, 0), 737 - cache_lookup_fn, MAP_END_KEY); 738 - if (ret == -EAGAIN) 739 - continue_at(cl, cache_lookup, bcache_wq); 740 - 741 - closure_return(cl); 742 - } 743 - 744 - /* Common code for the make_request functions */ 745 - 746 - static void request_endio(struct bio *bio, int error) 747 - { 748 - struct closure *cl = bio->bi_private; 749 - 750 - if (error) { 751 - struct search *s = container_of(cl, struct search, cl); 752 - s->error = error; 753 - /* Only cache read errors are recoverable */ 754 - s->recoverable = false; 755 - } 756 - 757 - bio_put(bio); 758 - closure_put(cl); 759 - } 760 - 761 - static void bio_complete(struct search *s) 762 - { 763 - if (s->orig_bio) { 764 - int cpu, rw = bio_data_dir(s->orig_bio); 765 - unsigned long duration = jiffies - s->start_time; 766 - 767 - cpu = part_stat_lock(); 768 - part_round_stats(cpu, &s->d->disk->part0); 769 - part_stat_add(cpu, &s->d->disk->part0, ticks[rw], duration); 770 - part_stat_unlock(); 771 - 772 - trace_bcache_request_end(s, s->orig_bio); 773 - bio_endio(s->orig_bio, s->error); 774 - s->orig_bio = NULL; 775 - } 776 - } 777 - 778 - static void do_bio_hook(struct search *s) 779 - { 780 - struct bio *bio = &s->bio.bio; 781 - memcpy(bio, s->orig_bio, sizeof(struct bio)); 782 - 783 - bio->bi_end_io = request_endio; 784 - bio->bi_private = &s->cl; 785 - atomic_set(&bio->bi_cnt, 3); 786 - } 787 - 788 - static void search_free(struct closure *cl) 789 - { 790 - struct search *s = container_of(cl, struct search, cl); 791 - bio_complete(s); 792 - 793 - if (s->cache_bio) 794 - bio_put(s->cache_bio); 795 - 796 - if (s->unaligned_bvec) 797 - mempool_free(s->bio.bio.bi_io_vec, s->d->unaligned_bvec); 798 - 799 - closure_debug_destroy(cl); 800 - mempool_free(s, s->d->c->search); 801 - } 802 - 803 - static struct search *search_alloc(struct bio *bio, struct bcache_device *d) 804 - { 805 - struct search *s; 806 - struct bio_vec *bv; 807 - 808 - s = mempool_alloc(d->c->search, GFP_NOIO); 809 - memset(s, 0, offsetof(struct search, insert_keys)); 810 - 811 - __closure_init(&s->cl, NULL); 812 - 813 - s->inode = d->id; 814 - s->c = d->c; 815 - s->d = d; 816 - s->op.lock = -1; 817 - s->task = current; 818 - s->orig_bio = bio; 819 - s->write = (bio->bi_rw & REQ_WRITE) != 0; 820 - s->flush_journal = (bio->bi_rw & (REQ_FLUSH|REQ_FUA)) != 0; 821 - s->recoverable = 1; 822 - s->start_time = jiffies; 823 - do_bio_hook(s); 824 - 825 - if (bio->bi_size != bio_segments(bio) * PAGE_SIZE) { 826 - bv = mempool_alloc(d->unaligned_bvec, GFP_NOIO); 827 - memcpy(bv, bio_iovec(bio), 828 - sizeof(struct bio_vec) * bio_segments(bio)); 829 - 830 - s->bio.bio.bi_io_vec = bv; 831 - s->unaligned_bvec = 1; 832 - } 833 - 834 - return s; 835 - } 836 - 837 - /* Cached devices */ 838 - 839 - static void cached_dev_bio_complete(struct closure *cl) 840 - { 841 - struct search *s = container_of(cl, struct search, cl); 842 - struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); 843 - 844 - search_free(cl); 845 - cached_dev_put(dc); 846 - } 643 + /* Congested? */ 847 644 848 645 unsigned bch_get_congested(struct cache_set *c) 849 646 { ··· 681 888 return &dc->io_hash[hash_64(k, RECENT_IO_BITS)]; 682 889 } 683 890 684 - static bool check_should_bypass(struct cached_dev *dc, struct search *s) 891 + static bool check_should_bypass(struct cached_dev *dc, struct bio *bio) 685 892 { 686 - struct cache_set *c = s->c; 687 - struct bio *bio = &s->bio.bio; 893 + struct cache_set *c = dc->disk.c; 688 894 unsigned mode = cache_mode(dc, bio); 689 895 unsigned sectors, congested = bch_get_congested(c); 896 + struct task_struct *task = current; 690 897 691 898 if (atomic_read(&dc->disk.detaching) || 692 899 c->gc_stats.in_use > CUTOFF_CACHE_ADD || ··· 725 932 726 933 i = list_first_entry(&dc->io_lru, struct io, lru); 727 934 728 - add_sequential(s->task); 935 + add_sequential(task); 729 936 i->sequential = 0; 730 937 found: 731 938 if (i->sequential + bio->bi_size > i->sequential) ··· 733 940 734 941 i->last = bio_end_sector(bio); 735 942 i->jiffies = jiffies + msecs_to_jiffies(5000); 736 - s->task->sequential_io = i->sequential; 943 + task->sequential_io = i->sequential; 737 944 738 945 hlist_del(&i->hash); 739 946 hlist_add_head(&i->hash, iohash(dc, i->last)); ··· 741 948 742 949 spin_unlock(&dc->io_lock); 743 950 } else { 744 - s->task->sequential_io = bio->bi_size; 951 + task->sequential_io = bio->bi_size; 745 952 746 - add_sequential(s->task); 953 + add_sequential(task); 747 954 } 748 955 749 - sectors = max(s->task->sequential_io, 750 - s->task->sequential_io_avg) >> 9; 956 + sectors = max(task->sequential_io, 957 + task->sequential_io_avg) >> 9; 751 958 752 959 if (dc->sequential_cutoff && 753 960 sectors >= dc->sequential_cutoff >> 9) { 754 - trace_bcache_bypass_sequential(s->orig_bio); 961 + trace_bcache_bypass_sequential(bio); 755 962 goto skip; 756 963 } 757 964 758 965 if (congested && sectors >= congested) { 759 - trace_bcache_bypass_congested(s->orig_bio); 966 + trace_bcache_bypass_congested(bio); 760 967 goto skip; 761 968 } 762 969 ··· 764 971 bch_rescale_priorities(c, bio_sectors(bio)); 765 972 return false; 766 973 skip: 767 - bch_mark_sectors_bypassed(s, bio_sectors(bio)); 974 + bch_mark_sectors_bypassed(c, dc, bio_sectors(bio)); 768 975 return true; 976 + } 977 + 978 + /* Cache lookup */ 979 + 980 + struct search { 981 + /* Stack frame for bio_complete */ 982 + struct closure cl; 983 + 984 + struct bcache_device *d; 985 + 986 + struct bbio bio; 987 + struct bio *orig_bio; 988 + struct bio *cache_miss; 989 + 990 + unsigned insert_bio_sectors; 991 + 992 + unsigned recoverable:1; 993 + unsigned unaligned_bvec:1; 994 + unsigned write:1; 995 + 996 + unsigned long start_time; 997 + 998 + struct btree_op op; 999 + struct data_insert_op iop; 1000 + }; 1001 + 1002 + static void bch_cache_read_endio(struct bio *bio, int error) 1003 + { 1004 + struct bbio *b = container_of(bio, struct bbio, bio); 1005 + struct closure *cl = bio->bi_private; 1006 + struct search *s = container_of(cl, struct search, cl); 1007 + 1008 + /* 1009 + * If the bucket was reused while our bio was in flight, we might have 1010 + * read the wrong data. Set s->error but not error so it doesn't get 1011 + * counted against the cache device, but we'll still reread the data 1012 + * from the backing device. 1013 + */ 1014 + 1015 + if (error) 1016 + s->iop.error = error; 1017 + else if (ptr_stale(s->iop.c, &b->key, 0)) { 1018 + atomic_long_inc(&s->iop.c->cache_read_races); 1019 + s->iop.error = -EINTR; 1020 + } 1021 + 1022 + bch_bbio_endio(s->iop.c, bio, error, "reading from cache"); 1023 + } 1024 + 1025 + /* 1026 + * Read from a single key, handling the initial cache miss if the key starts in 1027 + * the middle of the bio 1028 + */ 1029 + static int cache_lookup_fn(struct btree_op *op, struct btree *b, struct bkey *k) 1030 + { 1031 + struct search *s = container_of(op, struct search, op); 1032 + struct bio *n, *bio = &s->bio.bio; 1033 + struct bkey *bio_key; 1034 + unsigned ptr; 1035 + 1036 + if (bkey_cmp(k, &KEY(s->iop.inode, bio->bi_sector, 0)) <= 0) 1037 + return MAP_CONTINUE; 1038 + 1039 + if (KEY_INODE(k) != s->iop.inode || 1040 + KEY_START(k) > bio->bi_sector) { 1041 + unsigned bio_sectors = bio_sectors(bio); 1042 + unsigned sectors = KEY_INODE(k) == s->iop.inode 1043 + ? min_t(uint64_t, INT_MAX, 1044 + KEY_START(k) - bio->bi_sector) 1045 + : INT_MAX; 1046 + 1047 + int ret = s->d->cache_miss(b, s, bio, sectors); 1048 + if (ret != MAP_CONTINUE) 1049 + return ret; 1050 + 1051 + /* if this was a complete miss we shouldn't get here */ 1052 + BUG_ON(bio_sectors <= sectors); 1053 + } 1054 + 1055 + if (!KEY_SIZE(k)) 1056 + return MAP_CONTINUE; 1057 + 1058 + /* XXX: figure out best pointer - for multiple cache devices */ 1059 + ptr = 0; 1060 + 1061 + PTR_BUCKET(b->c, k, ptr)->prio = INITIAL_PRIO; 1062 + 1063 + n = bch_bio_split(bio, min_t(uint64_t, INT_MAX, 1064 + KEY_OFFSET(k) - bio->bi_sector), 1065 + GFP_NOIO, s->d->bio_split); 1066 + 1067 + bio_key = &container_of(n, struct bbio, bio)->key; 1068 + bch_bkey_copy_single_ptr(bio_key, k, ptr); 1069 + 1070 + bch_cut_front(&KEY(s->iop.inode, n->bi_sector, 0), bio_key); 1071 + bch_cut_back(&KEY(s->iop.inode, bio_end_sector(n), 0), bio_key); 1072 + 1073 + n->bi_end_io = bch_cache_read_endio; 1074 + n->bi_private = &s->cl; 1075 + 1076 + /* 1077 + * The bucket we're reading from might be reused while our bio 1078 + * is in flight, and we could then end up reading the wrong 1079 + * data. 1080 + * 1081 + * We guard against this by checking (in cache_read_endio()) if 1082 + * the pointer is stale again; if so, we treat it as an error 1083 + * and reread from the backing device (but we don't pass that 1084 + * error up anywhere). 1085 + */ 1086 + 1087 + __bch_submit_bbio(n, b->c); 1088 + return n == bio ? MAP_DONE : MAP_CONTINUE; 1089 + } 1090 + 1091 + static void cache_lookup(struct closure *cl) 1092 + { 1093 + struct search *s = container_of(cl, struct search, iop.cl); 1094 + struct bio *bio = &s->bio.bio; 1095 + 1096 + int ret = bch_btree_map_keys(&s->op, s->iop.c, 1097 + &KEY(s->iop.inode, bio->bi_sector, 0), 1098 + cache_lookup_fn, MAP_END_KEY); 1099 + if (ret == -EAGAIN) 1100 + continue_at(cl, cache_lookup, bcache_wq); 1101 + 1102 + closure_return(cl); 1103 + } 1104 + 1105 + /* Common code for the make_request functions */ 1106 + 1107 + static void request_endio(struct bio *bio, int error) 1108 + { 1109 + struct closure *cl = bio->bi_private; 1110 + 1111 + if (error) { 1112 + struct search *s = container_of(cl, struct search, cl); 1113 + s->iop.error = error; 1114 + /* Only cache read errors are recoverable */ 1115 + s->recoverable = false; 1116 + } 1117 + 1118 + bio_put(bio); 1119 + closure_put(cl); 1120 + } 1121 + 1122 + static void bio_complete(struct search *s) 1123 + { 1124 + if (s->orig_bio) { 1125 + int cpu, rw = bio_data_dir(s->orig_bio); 1126 + unsigned long duration = jiffies - s->start_time; 1127 + 1128 + cpu = part_stat_lock(); 1129 + part_round_stats(cpu, &s->d->disk->part0); 1130 + part_stat_add(cpu, &s->d->disk->part0, ticks[rw], duration); 1131 + part_stat_unlock(); 1132 + 1133 + trace_bcache_request_end(s->d, s->orig_bio); 1134 + bio_endio(s->orig_bio, s->iop.error); 1135 + s->orig_bio = NULL; 1136 + } 1137 + } 1138 + 1139 + static void do_bio_hook(struct search *s) 1140 + { 1141 + struct bio *bio = &s->bio.bio; 1142 + memcpy(bio, s->orig_bio, sizeof(struct bio)); 1143 + 1144 + bio->bi_end_io = request_endio; 1145 + bio->bi_private = &s->cl; 1146 + atomic_set(&bio->bi_cnt, 3); 1147 + } 1148 + 1149 + static void search_free(struct closure *cl) 1150 + { 1151 + struct search *s = container_of(cl, struct search, cl); 1152 + bio_complete(s); 1153 + 1154 + if (s->iop.bio) 1155 + bio_put(s->iop.bio); 1156 + 1157 + if (s->unaligned_bvec) 1158 + mempool_free(s->bio.bio.bi_io_vec, s->d->unaligned_bvec); 1159 + 1160 + closure_debug_destroy(cl); 1161 + mempool_free(s, s->d->c->search); 1162 + } 1163 + 1164 + static struct search *search_alloc(struct bio *bio, struct bcache_device *d) 1165 + { 1166 + struct search *s; 1167 + struct bio_vec *bv; 1168 + 1169 + s = mempool_alloc(d->c->search, GFP_NOIO); 1170 + memset(s, 0, offsetof(struct search, iop.insert_keys)); 1171 + 1172 + __closure_init(&s->cl, NULL); 1173 + 1174 + s->iop.inode = d->id; 1175 + s->iop.c = d->c; 1176 + s->d = d; 1177 + s->op.lock = -1; 1178 + s->iop.task = current; 1179 + s->orig_bio = bio; 1180 + s->write = (bio->bi_rw & REQ_WRITE) != 0; 1181 + s->iop.flush_journal = (bio->bi_rw & (REQ_FLUSH|REQ_FUA)) != 0; 1182 + s->recoverable = 1; 1183 + s->start_time = jiffies; 1184 + do_bio_hook(s); 1185 + 1186 + if (bio->bi_size != bio_segments(bio) * PAGE_SIZE) { 1187 + bv = mempool_alloc(d->unaligned_bvec, GFP_NOIO); 1188 + memcpy(bv, bio_iovec(bio), 1189 + sizeof(struct bio_vec) * bio_segments(bio)); 1190 + 1191 + s->bio.bio.bi_io_vec = bv; 1192 + s->unaligned_bvec = 1; 1193 + } 1194 + 1195 + return s; 1196 + } 1197 + 1198 + /* Cached devices */ 1199 + 1200 + static void cached_dev_bio_complete(struct closure *cl) 1201 + { 1202 + struct search *s = container_of(cl, struct search, cl); 1203 + struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); 1204 + 1205 + search_free(cl); 1206 + cached_dev_put(dc); 769 1207 } 770 1208 771 1209 /* Process reads */ ··· 1005 981 { 1006 982 struct search *s = container_of(cl, struct search, cl); 1007 983 1008 - if (s->insert_collision) 1009 - bch_mark_cache_miss_collision(s); 984 + if (s->iop.replace_collision) 985 + bch_mark_cache_miss_collision(s->iop.c, s->d); 1010 986 1011 - if (s->cache_bio) { 987 + if (s->iop.bio) { 1012 988 int i; 1013 989 struct bio_vec *bv; 1014 990 1015 - bio_for_each_segment_all(bv, s->cache_bio, i) 991 + bio_for_each_segment_all(bv, s->iop.bio, i) 1016 992 __free_page(bv->bv_page); 1017 993 } 1018 994 ··· 1030 1006 /* Retry from the backing device: */ 1031 1007 trace_bcache_read_retry(s->orig_bio); 1032 1008 1033 - s->error = 0; 1009 + s->iop.error = 0; 1034 1010 bv = s->bio.bio.bi_io_vec; 1035 1011 do_bio_hook(s); 1036 1012 s->bio.bio.bi_io_vec = bv; ··· 1065 1041 * to the buffers the original bio pointed to: 1066 1042 */ 1067 1043 1068 - if (s->cache_bio) { 1069 - bio_reset(s->cache_bio); 1070 - s->cache_bio->bi_sector = 1071 - s->cache_miss->bi_sector; 1072 - s->cache_bio->bi_bdev = s->cache_miss->bi_bdev; 1073 - s->cache_bio->bi_size = s->cache_bio_sectors << 9; 1074 - bch_bio_map(s->cache_bio, NULL); 1044 + if (s->iop.bio) { 1045 + bio_reset(s->iop.bio); 1046 + s->iop.bio->bi_sector = s->cache_miss->bi_sector; 1047 + s->iop.bio->bi_bdev = s->cache_miss->bi_bdev; 1048 + s->iop.bio->bi_size = s->insert_bio_sectors << 9; 1049 + bch_bio_map(s->iop.bio, NULL); 1075 1050 1076 - bio_copy_data(s->cache_miss, s->cache_bio); 1051 + bio_copy_data(s->cache_miss, s->iop.bio); 1077 1052 1078 1053 bio_put(s->cache_miss); 1079 1054 s->cache_miss = NULL; 1080 1055 } 1081 1056 1082 - if (verify(dc, &s->bio.bio) && s->recoverable) 1083 - bch_data_verify(s); 1057 + if (verify(dc, &s->bio.bio) && s->recoverable && !s->unaligned_bvec) 1058 + bch_data_verify(dc, s->orig_bio); 1084 1059 1085 1060 bio_complete(s); 1086 1061 1087 - if (s->cache_bio && 1088 - !test_bit(CACHE_SET_STOPPING, &s->c->flags)) { 1089 - BUG_ON(!s->replace); 1090 - closure_call(&s->btree, bch_data_insert, NULL, cl); 1062 + if (s->iop.bio && 1063 + !test_bit(CACHE_SET_STOPPING, &s->iop.c->flags)) { 1064 + BUG_ON(!s->iop.replace); 1065 + closure_call(&s->iop.cl, bch_data_insert, NULL, cl); 1091 1066 } 1092 1067 1093 1068 continue_at(cl, cached_dev_cache_miss_done, NULL); ··· 1097 1074 struct search *s = container_of(cl, struct search, cl); 1098 1075 struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); 1099 1076 1100 - bch_mark_cache_accounting(s, !s->cache_miss, s->bypass); 1101 - trace_bcache_read(s->orig_bio, !s->cache_miss, s->bypass); 1077 + bch_mark_cache_accounting(s->iop.c, s->d, 1078 + !s->cache_miss, s->iop.bypass); 1079 + trace_bcache_read(s->orig_bio, !s->cache_miss, s->iop.bypass); 1102 1080 1103 - if (s->error) 1081 + if (s->iop.error) 1104 1082 continue_at_nobarrier(cl, cached_dev_read_error, bcache_wq); 1105 - else if (s->cache_bio || verify(dc, &s->bio.bio)) 1083 + else if (s->iop.bio || verify(dc, &s->bio.bio)) 1106 1084 continue_at_nobarrier(cl, cached_dev_read_done, bcache_wq); 1107 1085 else 1108 1086 continue_at_nobarrier(cl, cached_dev_bio_complete, NULL); ··· 1117 1093 struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); 1118 1094 struct bio *miss, *cache_bio; 1119 1095 1120 - if (s->cache_miss || s->bypass) { 1096 + if (s->cache_miss || s->iop.bypass) { 1121 1097 miss = bch_bio_split(bio, sectors, GFP_NOIO, s->d->bio_split); 1122 1098 ret = miss == bio ? MAP_DONE : MAP_CONTINUE; 1123 1099 goto out_submit; ··· 1125 1101 1126 1102 if (!(bio->bi_rw & REQ_RAHEAD) && 1127 1103 !(bio->bi_rw & REQ_META) && 1128 - s->c->gc_stats.in_use < CUTOFF_CACHE_READA) 1104 + s->iop.c->gc_stats.in_use < CUTOFF_CACHE_READA) 1129 1105 reada = min_t(sector_t, dc->readahead >> 9, 1130 1106 bdev_sectors(bio->bi_bdev) - bio_end_sector(bio)); 1131 1107 1132 - s->cache_bio_sectors = min(sectors, bio_sectors(bio) + reada); 1108 + s->insert_bio_sectors = min(sectors, bio_sectors(bio) + reada); 1133 1109 1134 - s->replace_key = KEY(s->inode, bio->bi_sector + 1135 - s->cache_bio_sectors, s->cache_bio_sectors); 1110 + s->iop.replace_key = KEY(s->iop.inode, 1111 + bio->bi_sector + s->insert_bio_sectors, 1112 + s->insert_bio_sectors); 1136 1113 1137 - ret = bch_btree_insert_check_key(b, &s->op, &s->replace_key); 1114 + ret = bch_btree_insert_check_key(b, &s->op, &s->iop.replace_key); 1138 1115 if (ret) 1139 1116 return ret; 1140 1117 1141 - s->replace = true; 1118 + s->iop.replace = true; 1142 1119 1143 1120 miss = bch_bio_split(bio, sectors, GFP_NOIO, s->d->bio_split); 1144 1121 ··· 1147 1122 ret = miss == bio ? MAP_DONE : -EINTR; 1148 1123 1149 1124 cache_bio = bio_alloc_bioset(GFP_NOWAIT, 1150 - DIV_ROUND_UP(s->cache_bio_sectors, PAGE_SECTORS), 1125 + DIV_ROUND_UP(s->insert_bio_sectors, PAGE_SECTORS), 1151 1126 dc->disk.bio_split); 1152 1127 if (!cache_bio) 1153 1128 goto out_submit; 1154 1129 1155 1130 cache_bio->bi_sector = miss->bi_sector; 1156 1131 cache_bio->bi_bdev = miss->bi_bdev; 1157 - cache_bio->bi_size = s->cache_bio_sectors << 9; 1132 + cache_bio->bi_size = s->insert_bio_sectors << 9; 1158 1133 1159 1134 cache_bio->bi_end_io = request_endio; 1160 1135 cache_bio->bi_private = &s->cl; ··· 1163 1138 if (bio_alloc_pages(cache_bio, __GFP_NOWARN|GFP_NOIO)) 1164 1139 goto out_put; 1165 1140 1141 + if (reada) 1142 + bch_mark_cache_readahead(s->iop.c, s->d); 1143 + 1166 1144 s->cache_miss = miss; 1167 - s->cache_bio = cache_bio; 1145 + s->iop.bio = cache_bio; 1168 1146 bio_get(cache_bio); 1169 1147 closure_bio_submit(cache_bio, &s->cl, s->d); 1170 1148 ··· 1185 1157 { 1186 1158 struct closure *cl = &s->cl; 1187 1159 1188 - closure_call(&s->btree, cache_lookup, NULL, cl); 1160 + closure_call(&s->iop.cl, cache_lookup, NULL, cl); 1189 1161 continue_at(cl, cached_dev_read_done_bh, NULL); 1190 1162 } 1191 1163 ··· 1207 1179 struct bkey start = KEY(dc->disk.id, bio->bi_sector, 0); 1208 1180 struct bkey end = KEY(dc->disk.id, bio_end_sector(bio), 0); 1209 1181 1210 - bch_keybuf_check_overlapping(&s->c->moving_gc_keys, &start, &end); 1182 + bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys, &start, &end); 1211 1183 1212 1184 down_read_non_owner(&dc->writeback_lock); 1213 1185 if (bch_keybuf_check_overlapping(&dc->writeback_keys, &start, &end)) { ··· 1215 1187 * We overlap with some dirty data undergoing background 1216 1188 * writeback, force this write to writeback 1217 1189 */ 1218 - s->bypass = false; 1219 - s->writeback = true; 1190 + s->iop.bypass = false; 1191 + s->iop.writeback = true; 1220 1192 } 1221 1193 1222 1194 /* ··· 1227 1199 * so we still want to call it. 1228 1200 */ 1229 1201 if (bio->bi_rw & REQ_DISCARD) 1230 - s->bypass = true; 1202 + s->iop.bypass = true; 1231 1203 1232 1204 if (should_writeback(dc, s->orig_bio, 1233 1205 cache_mode(dc, bio), 1234 - s->bypass)) { 1235 - s->bypass = false; 1236 - s->writeback = true; 1206 + s->iop.bypass)) { 1207 + s->iop.bypass = false; 1208 + s->iop.writeback = true; 1237 1209 } 1238 1210 1239 - trace_bcache_write(s->orig_bio, s->writeback, s->bypass); 1240 - 1241 - if (s->bypass) { 1242 - s->cache_bio = s->orig_bio; 1243 - bio_get(s->cache_bio); 1211 + if (s->iop.bypass) { 1212 + s->iop.bio = s->orig_bio; 1213 + bio_get(s->iop.bio); 1244 1214 1245 1215 if (!(bio->bi_rw & REQ_DISCARD) || 1246 1216 blk_queue_discard(bdev_get_queue(dc->bdev))) 1247 1217 closure_bio_submit(bio, cl, s->d); 1248 - } else if (s->writeback) { 1218 + } else if (s->iop.writeback) { 1249 1219 bch_writeback_add(dc); 1250 - s->cache_bio = bio; 1220 + s->iop.bio = bio; 1251 1221 1252 1222 if (bio->bi_rw & REQ_FLUSH) { 1253 1223 /* Also need to send a flush to the backing device */ ··· 1260 1234 closure_bio_submit(flush, cl, s->d); 1261 1235 } 1262 1236 } else { 1263 - s->cache_bio = bio_clone_bioset(bio, GFP_NOIO, 1264 - dc->disk.bio_split); 1237 + s->iop.bio = bio_clone_bioset(bio, GFP_NOIO, 1238 + dc->disk.bio_split); 1265 1239 1266 1240 closure_bio_submit(bio, cl, s->d); 1267 1241 } 1268 1242 1269 - closure_call(&s->btree, bch_data_insert, NULL, cl); 1243 + closure_call(&s->iop.cl, bch_data_insert, NULL, cl); 1270 1244 continue_at(cl, cached_dev_write_complete, NULL); 1271 1245 } 1272 1246 ··· 1275 1249 struct search *s = container_of(cl, struct search, cl); 1276 1250 struct bio *bio = &s->bio.bio; 1277 1251 1278 - if (s->flush_journal) 1279 - bch_journal_meta(s->c, cl); 1252 + if (s->iop.flush_journal) 1253 + bch_journal_meta(s->iop.c, cl); 1280 1254 1281 1255 /* If it's a flush, we send the flush to the backing device too */ 1282 1256 closure_bio_submit(bio, cl, s->d); ··· 1303 1277 1304 1278 if (cached_dev_get(dc)) { 1305 1279 s = search_alloc(bio, d); 1306 - trace_bcache_request_start(s, bio); 1280 + trace_bcache_request_start(s->d, bio); 1307 1281 1308 1282 if (!bio->bi_size) { 1309 1283 /* ··· 1314 1288 cached_dev_nodata, 1315 1289 bcache_wq); 1316 1290 } else { 1317 - s->bypass = check_should_bypass(dc, s); 1291 + s->iop.bypass = check_should_bypass(dc, bio); 1318 1292 1319 1293 if (rw) 1320 1294 cached_dev_write(dc, s); ··· 1404 1378 { 1405 1379 struct search *s = container_of(cl, struct search, cl); 1406 1380 1407 - if (s->flush_journal) 1408 - bch_journal_meta(s->c, cl); 1381 + if (s->iop.flush_journal) 1382 + bch_journal_meta(s->iop.c, cl); 1409 1383 1410 1384 continue_at(cl, search_free, NULL); 1411 1385 } ··· 1426 1400 cl = &s->cl; 1427 1401 bio = &s->bio.bio; 1428 1402 1429 - trace_bcache_request_start(s, bio); 1403 + trace_bcache_request_start(s->d, bio); 1430 1404 1431 1405 if (!bio->bi_size) { 1432 1406 /* ··· 1437 1411 flash_dev_nodata, 1438 1412 bcache_wq); 1439 1413 } else if (rw) { 1440 - bch_keybuf_check_overlapping(&s->c->moving_gc_keys, 1414 + bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys, 1441 1415 &KEY(d->id, bio->bi_sector, 0), 1442 1416 &KEY(d->id, bio_end_sector(bio), 0)); 1443 1417 1444 - s->bypass = (bio->bi_rw & REQ_DISCARD) != 0; 1445 - s->writeback = true; 1446 - s->cache_bio = bio; 1418 + s->iop.bypass = (bio->bi_rw & REQ_DISCARD) != 0; 1419 + s->iop.writeback = true; 1420 + s->iop.bio = bio; 1447 1421 1448 - closure_call(&s->btree, bch_data_insert, NULL, cl); 1422 + closure_call(&s->iop.cl, bch_data_insert, NULL, cl); 1449 1423 } else { 1450 - closure_call(&s->btree, cache_lookup, NULL, cl); 1424 + closure_call(&s->iop.cl, cache_lookup, NULL, cl); 1451 1425 } 1452 1426 1453 1427 continue_at(cl, search_free, NULL);
+9 -30
drivers/md/bcache/request.h
··· 3 3 4 4 #include <linux/cgroup.h> 5 5 6 - struct search { 7 - /* Stack frame for bio_complete */ 6 + struct data_insert_op { 8 7 struct closure cl; 9 - struct closure btree; 10 - 11 - struct bcache_device *d; 12 8 struct cache_set *c; 13 9 struct task_struct *task; 14 - 15 - struct bbio bio; 16 - struct bio *orig_bio; 17 - struct bio *cache_miss; 18 - 19 - /* Bio to be inserted into the cache */ 20 - struct bio *cache_bio; 21 - unsigned cache_bio_sectors; 10 + struct bio *bio; 22 11 23 12 unsigned inode; 13 + uint16_t write_prio; 14 + short error; 24 15 25 - unsigned recoverable:1; 26 - unsigned unaligned_bvec:1; 27 - 28 - unsigned write:1; 29 - unsigned writeback:1; 30 - 31 - unsigned csum:1; 32 16 unsigned bypass:1; 17 + unsigned writeback:1; 33 18 unsigned flush_journal:1; 19 + unsigned csum:1; 20 + 21 + unsigned replace:1; 22 + unsigned replace_collision:1; 34 23 35 24 unsigned insert_data_done:1; 36 - unsigned replace:1; 37 - unsigned insert_collision:1; 38 - 39 - uint16_t write_prio; 40 - 41 - /* IO error returned to s->bio */ 42 - short error; 43 - unsigned long start_time; 44 - 45 - struct btree_op op; 46 25 47 26 /* Anything past this point won't get zeroed in search_alloc() */ 48 27 struct keylist insert_keys;
+13 -13
drivers/md/bcache/stats.c
··· 7 7 #include "bcache.h" 8 8 #include "stats.h" 9 9 #include "btree.h" 10 - #include "request.h" 11 10 #include "sysfs.h" 12 11 13 12 /* ··· 195 196 atomic_inc(&stats->cache_bypass_misses); 196 197 } 197 198 198 - void bch_mark_cache_accounting(struct search *s, bool hit, bool bypass) 199 + void bch_mark_cache_accounting(struct cache_set *c, struct bcache_device *d, 200 + bool hit, bool bypass) 199 201 { 200 - struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); 202 + struct cached_dev *dc = container_of(d, struct cached_dev, disk); 201 203 mark_cache_stats(&dc->accounting.collector, hit, bypass); 202 - mark_cache_stats(&s->c->accounting.collector, hit, bypass); 204 + mark_cache_stats(&c->accounting.collector, hit, bypass); 203 205 #ifdef CONFIG_CGROUP_BCACHE 204 206 mark_cache_stats(&(bch_bio_to_cgroup(s->orig_bio)->stats), hit, bypass); 205 207 #endif 206 208 } 207 209 208 - void bch_mark_cache_readahead(struct search *s) 210 + void bch_mark_cache_readahead(struct cache_set *c, struct bcache_device *d) 209 211 { 210 - struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); 212 + struct cached_dev *dc = container_of(d, struct cached_dev, disk); 211 213 atomic_inc(&dc->accounting.collector.cache_readaheads); 212 - atomic_inc(&s->c->accounting.collector.cache_readaheads); 214 + atomic_inc(&c->accounting.collector.cache_readaheads); 213 215 } 214 216 215 - void bch_mark_cache_miss_collision(struct search *s) 217 + void bch_mark_cache_miss_collision(struct cache_set *c, struct bcache_device *d) 216 218 { 217 - struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); 219 + struct cached_dev *dc = container_of(d, struct cached_dev, disk); 218 220 atomic_inc(&dc->accounting.collector.cache_miss_collisions); 219 - atomic_inc(&s->c->accounting.collector.cache_miss_collisions); 221 + atomic_inc(&c->accounting.collector.cache_miss_collisions); 220 222 } 221 223 222 - void bch_mark_sectors_bypassed(struct search *s, int sectors) 224 + void bch_mark_sectors_bypassed(struct cache_set *c, struct cached_dev *dc, 225 + int sectors) 223 226 { 224 - struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); 225 227 atomic_add(sectors, &dc->accounting.collector.sectors_bypassed); 226 - atomic_add(sectors, &s->c->accounting.collector.sectors_bypassed); 228 + atomic_add(sectors, &c->accounting.collector.sectors_bypassed); 227 229 } 228 230 229 231 void bch_cache_accounting_init(struct cache_accounting *acc,
+8 -5
drivers/md/bcache/stats.h
··· 38 38 struct cache_stats day; 39 39 }; 40 40 41 - struct search; 41 + struct cache_set; 42 + struct cached_dev; 43 + struct bcache_device; 42 44 43 45 void bch_cache_accounting_init(struct cache_accounting *acc, 44 46 struct closure *parent); ··· 52 50 53 51 void bch_cache_accounting_destroy(struct cache_accounting *acc); 54 52 55 - void bch_mark_cache_accounting(struct search *s, bool hit, bool bypass); 56 - void bch_mark_cache_readahead(struct search *s); 57 - void bch_mark_cache_miss_collision(struct search *s); 58 - void bch_mark_sectors_bypassed(struct search *s, int sectors); 53 + void bch_mark_cache_accounting(struct cache_set *, struct bcache_device *, 54 + bool, bool); 55 + void bch_mark_cache_readahead(struct cache_set *, struct bcache_device *); 56 + void bch_mark_cache_miss_collision(struct cache_set *, struct bcache_device *); 57 + void bch_mark_sectors_bypassed(struct cache_set *, struct cached_dev *, int); 59 58 60 59 #endif /* _BCACHE_STATS_H_ */
-1
drivers/md/bcache/trace.c
··· 1 1 #include "bcache.h" 2 2 #include "btree.h" 3 - #include "request.h" 4 3 5 4 #include <linux/blktrace_api.h> 6 5 #include <linux/module.h>
+8 -10
include/trace/events/bcache.h
··· 6 6 7 7 #include <linux/tracepoint.h> 8 8 9 - struct search; 10 - 11 9 DECLARE_EVENT_CLASS(bcache_request, 12 - TP_PROTO(struct search *s, struct bio *bio), 13 - TP_ARGS(s, bio), 10 + TP_PROTO(struct bcache_device *d, struct bio *bio), 11 + TP_ARGS(d, bio), 14 12 15 13 TP_STRUCT__entry( 16 14 __field(dev_t, dev ) ··· 22 24 23 25 TP_fast_assign( 24 26 __entry->dev = bio->bi_bdev->bd_dev; 25 - __entry->orig_major = s->d->disk->major; 26 - __entry->orig_minor = s->d->disk->first_minor; 27 + __entry->orig_major = d->disk->major; 28 + __entry->orig_minor = d->disk->first_minor; 27 29 __entry->sector = bio->bi_sector; 28 30 __entry->orig_sector = bio->bi_sector - 16; 29 31 __entry->nr_sector = bio->bi_size >> 9; ··· 77 79 /* request.c */ 78 80 79 81 DEFINE_EVENT(bcache_request, bcache_request_start, 80 - TP_PROTO(struct search *s, struct bio *bio), 81 - TP_ARGS(s, bio) 82 + TP_PROTO(struct bcache_device *d, struct bio *bio), 83 + TP_ARGS(d, bio) 82 84 ); 83 85 84 86 DEFINE_EVENT(bcache_request, bcache_request_end, 85 - TP_PROTO(struct search *s, struct bio *bio), 86 - TP_ARGS(s, bio) 87 + TP_PROTO(struct bcache_device *d, struct bio *bio), 88 + TP_ARGS(d, bio) 87 89 ); 88 90 89 91 DECLARE_EVENT_CLASS(bcache_bio,