1 /* 2 * bcache setup/teardown code, and some metadata io - read a superblock and 3 * figure out what to do with it. 4 * 5 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com> 6 * Copyright 2012 Google, Inc. 7 */ 8 9 #include "bcache.h" 10 #include "btree.h" 11 #include "debug.h" 12 #include "extents.h" 13 #include "request.h" 14 #include "writeback.h" 15 16 #include <linux/blkdev.h> 17 #include <linux/buffer_head.h> 18 #include <linux/debugfs.h> 19 #include <linux/genhd.h> 20 #include <linux/idr.h> 21 #include <linux/kthread.h> 22 #include <linux/module.h> 23 #include <linux/random.h> 24 #include <linux/reboot.h> 25 #include <linux/sysfs.h> 26 27 MODULE_LICENSE("GPL"); 28 MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>"); 29 30 static const char bcache_magic[] = { 31 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca, 32 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81 33 }; 34 35 static const char invalid_uuid[] = { 36 0xa0, 0x3e, 0xf8, 0xed, 0x3e, 0xe1, 0xb8, 0x78, 37 0xc8, 0x50, 0xfc, 0x5e, 0xcb, 0x16, 0xcd, 0x99 38 }; 39 40 /* Default is -1; we skip past it for struct cached_dev's cache mode */ 41 const char * const bch_cache_modes[] = { 42 "default", 43 "writethrough", 44 "writeback", 45 "writearound", 46 "none", 47 NULL 48 }; 49 50 static struct kobject *bcache_kobj; 51 struct mutex bch_register_lock; 52 LIST_HEAD(bch_cache_sets); 53 static LIST_HEAD(uncached_devices); 54 55 static int bcache_major; 56 static DEFINE_IDA(bcache_minor); 57 static wait_queue_head_t unregister_wait; 58 struct workqueue_struct *bcache_wq; 59 60 #define BTREE_MAX_PAGES (256 * 1024 / PAGE_SIZE) 61 62 static void bio_split_pool_free(struct bio_split_pool *p) 63 { 64 if (p->bio_split_hook) 65 mempool_destroy(p->bio_split_hook); 66 67 if (p->bio_split) 68 bioset_free(p->bio_split); 69 } 70 71 static int bio_split_pool_init(struct bio_split_pool *p) 72 { 73 p->bio_split = bioset_create(4, 0); 74 if (!p->bio_split) 75 return -ENOMEM; 76 77 p->bio_split_hook = mempool_create_kmalloc_pool(4, 78 sizeof(struct bio_split_hook)); 79 if (!p->bio_split_hook) 80 return -ENOMEM; 81 82 return 0; 83 } 84 85 /* Superblock */ 86 87 static const char *read_super(struct cache_sb *sb, struct block_device *bdev, 88 struct page **res) 89 { 90 const char *err; 91 struct cache_sb *s; 92 struct buffer_head *bh = __bread(bdev, 1, SB_SIZE); 93 unsigned i; 94 95 if (!bh) 96 return "IO error"; 97 98 s = (struct cache_sb *) bh->b_data; 99 100 sb->offset = le64_to_cpu(s->offset); 101 sb->version = le64_to_cpu(s->version); 102 103 memcpy(sb->magic, s->magic, 16); 104 memcpy(sb->uuid, s->uuid, 16); 105 memcpy(sb->set_uuid, s->set_uuid, 16); 106 memcpy(sb->label, s->label, SB_LABEL_SIZE); 107 108 sb->flags = le64_to_cpu(s->flags); 109 sb->seq = le64_to_cpu(s->seq); 110 sb->last_mount = le32_to_cpu(s->last_mount); 111 sb->first_bucket = le16_to_cpu(s->first_bucket); 112 sb->keys = le16_to_cpu(s->keys); 113 114 for (i = 0; i < SB_JOURNAL_BUCKETS; i++) 115 sb->d[i] = le64_to_cpu(s->d[i]); 116 117 pr_debug("read sb version %llu, flags %llu, seq %llu, journal size %u", 118 sb->version, sb->flags, sb->seq, sb->keys); 119 120 err = "Not a bcache superblock"; 121 if (sb->offset != SB_SECTOR) 122 goto err; 123 124 if (memcmp(sb->magic, bcache_magic, 16)) 125 goto err; 126 127 err = "Too many journal buckets"; 128 if (sb->keys > SB_JOURNAL_BUCKETS) 129 goto err; 130 131 err = "Bad checksum"; 132 if (s->csum != csum_set(s)) 133 goto err; 134 135 err = "Bad UUID"; 136 if (bch_is_zero(sb->uuid, 16)) 137 goto err; 138 139 sb->block_size = le16_to_cpu(s->block_size); 140 141 err = "Superblock block size smaller than device block size"; 142 if (sb->block_size << 9 < bdev_logical_block_size(bdev)) 143 goto err; 144 145 switch (sb->version) { 146 case BCACHE_SB_VERSION_BDEV: 147 sb->data_offset = BDEV_DATA_START_DEFAULT; 148 break; 149 case BCACHE_SB_VERSION_BDEV_WITH_OFFSET: 150 sb->data_offset = le64_to_cpu(s->data_offset); 151 152 err = "Bad data offset"; 153 if (sb->data_offset < BDEV_DATA_START_DEFAULT) 154 goto err; 155 156 break; 157 case BCACHE_SB_VERSION_CDEV: 158 case BCACHE_SB_VERSION_CDEV_WITH_UUID: 159 sb->nbuckets = le64_to_cpu(s->nbuckets); 160 sb->block_size = le16_to_cpu(s->block_size); 161 sb->bucket_size = le16_to_cpu(s->bucket_size); 162 163 sb->nr_in_set = le16_to_cpu(s->nr_in_set); 164 sb->nr_this_dev = le16_to_cpu(s->nr_this_dev); 165 166 err = "Too many buckets"; 167 if (sb->nbuckets > LONG_MAX) 168 goto err; 169 170 err = "Not enough buckets"; 171 if (sb->nbuckets < 1 << 7) 172 goto err; 173 174 err = "Bad block/bucket size"; 175 if (!is_power_of_2(sb->block_size) || 176 sb->block_size > PAGE_SECTORS || 177 !is_power_of_2(sb->bucket_size) || 178 sb->bucket_size < PAGE_SECTORS) 179 goto err; 180 181 err = "Invalid superblock: device too small"; 182 if (get_capacity(bdev->bd_disk) < sb->bucket_size * sb->nbuckets) 183 goto err; 184 185 err = "Bad UUID"; 186 if (bch_is_zero(sb->set_uuid, 16)) 187 goto err; 188 189 err = "Bad cache device number in set"; 190 if (!sb->nr_in_set || 191 sb->nr_in_set <= sb->nr_this_dev || 192 sb->nr_in_set > MAX_CACHES_PER_SET) 193 goto err; 194 195 err = "Journal buckets not sequential"; 196 for (i = 0; i < sb->keys; i++) 197 if (sb->d[i] != sb->first_bucket + i) 198 goto err; 199 200 err = "Too many journal buckets"; 201 if (sb->first_bucket + sb->keys > sb->nbuckets) 202 goto err; 203 204 err = "Invalid superblock: first bucket comes before end of super"; 205 if (sb->first_bucket * sb->bucket_size < 16) 206 goto err; 207 208 break; 209 default: 210 err = "Unsupported superblock version"; 211 goto err; 212 } 213 214 sb->last_mount = get_seconds(); 215 err = NULL; 216 217 get_page(bh->b_page); 218 *res = bh->b_page; 219 err: 220 put_bh(bh); 221 return err; 222 } 223 224 static void write_bdev_super_endio(struct bio *bio, int error) 225 { 226 struct cached_dev *dc = bio->bi_private; 227 /* XXX: error checking */ 228 229 closure_put(&dc->sb_write); 230 } 231 232 static void __write_super(struct cache_sb *sb, struct bio *bio) 233 { 234 struct cache_sb *out = page_address(bio->bi_io_vec[0].bv_page); 235 unsigned i; 236 237 bio->bi_iter.bi_sector = SB_SECTOR; 238 bio->bi_rw = REQ_SYNC|REQ_META; 239 bio->bi_iter.bi_size = SB_SIZE; 240 bch_bio_map(bio, NULL); 241 242 out->offset = cpu_to_le64(sb->offset); 243 out->version = cpu_to_le64(sb->version); 244 245 memcpy(out->uuid, sb->uuid, 16); 246 memcpy(out->set_uuid, sb->set_uuid, 16); 247 memcpy(out->label, sb->label, SB_LABEL_SIZE); 248 249 out->flags = cpu_to_le64(sb->flags); 250 out->seq = cpu_to_le64(sb->seq); 251 252 out->last_mount = cpu_to_le32(sb->last_mount); 253 out->first_bucket = cpu_to_le16(sb->first_bucket); 254 out->keys = cpu_to_le16(sb->keys); 255 256 for (i = 0; i < sb->keys; i++) 257 out->d[i] = cpu_to_le64(sb->d[i]); 258 259 out->csum = csum_set(out); 260 261 pr_debug("ver %llu, flags %llu, seq %llu", 262 sb->version, sb->flags, sb->seq); 263 264 submit_bio(REQ_WRITE, bio); 265 } 266 267 static void bch_write_bdev_super_unlock(struct closure *cl) 268 { 269 struct cached_dev *dc = container_of(cl, struct cached_dev, sb_write); 270 271 up(&dc->sb_write_mutex); 272 } 273 274 void bch_write_bdev_super(struct cached_dev *dc, struct closure *parent) 275 { 276 struct closure *cl = &dc->sb_write; 277 struct bio *bio = &dc->sb_bio; 278 279 down(&dc->sb_write_mutex); 280 closure_init(cl, parent); 281 282 bio_reset(bio); 283 bio->bi_bdev = dc->bdev; 284 bio->bi_end_io = write_bdev_super_endio; 285 bio->bi_private = dc; 286 287 closure_get(cl); 288 __write_super(&dc->sb, bio); 289 290 closure_return_with_destructor(cl, bch_write_bdev_super_unlock); 291 } 292 293 static void write_super_endio(struct bio *bio, int error) 294 { 295 struct cache *ca = bio->bi_private; 296 297 bch_count_io_errors(ca, error, "writing superblock"); 298 closure_put(&ca->set->sb_write); 299 } 300 301 static void bcache_write_super_unlock(struct closure *cl) 302 { 303 struct cache_set *c = container_of(cl, struct cache_set, sb_write); 304 305 up(&c->sb_write_mutex); 306 } 307 308 void bcache_write_super(struct cache_set *c) 309 { 310 struct closure *cl = &c->sb_write; 311 struct cache *ca; 312 unsigned i; 313 314 down(&c->sb_write_mutex); 315 closure_init(cl, &c->cl); 316 317 c->sb.seq++; 318 319 for_each_cache(ca, c, i) { 320 struct bio *bio = &ca->sb_bio; 321 322 ca->sb.version = BCACHE_SB_VERSION_CDEV_WITH_UUID; 323 ca->sb.seq = c->sb.seq; 324 ca->sb.last_mount = c->sb.last_mount; 325 326 SET_CACHE_SYNC(&ca->sb, CACHE_SYNC(&c->sb)); 327 328 bio_reset(bio); 329 bio->bi_bdev = ca->bdev; 330 bio->bi_end_io = write_super_endio; 331 bio->bi_private = ca; 332 333 closure_get(cl); 334 __write_super(&ca->sb, bio); 335 } 336 337 closure_return_with_destructor(cl, bcache_write_super_unlock); 338 } 339 340 /* UUID io */ 341 342 static void uuid_endio(struct bio *bio, int error) 343 { 344 struct closure *cl = bio->bi_private; 345 struct cache_set *c = container_of(cl, struct cache_set, uuid_write); 346 347 cache_set_err_on(error, c, "accessing uuids"); 348 bch_bbio_free(bio, c); 349 closure_put(cl); 350 } 351 352 static void uuid_io_unlock(struct closure *cl) 353 { 354 struct cache_set *c = container_of(cl, struct cache_set, uuid_write); 355 356 up(&c->uuid_write_mutex); 357 } 358 359 static void uuid_io(struct cache_set *c, unsigned long rw, 360 struct bkey *k, struct closure *parent) 361 { 362 struct closure *cl = &c->uuid_write; 363 struct uuid_entry *u; 364 unsigned i; 365 char buf[80]; 366 367 BUG_ON(!parent); 368 down(&c->uuid_write_mutex); 369 closure_init(cl, parent); 370 371 for (i = 0; i < KEY_PTRS(k); i++) { 372 struct bio *bio = bch_bbio_alloc(c); 373 374 bio->bi_rw = REQ_SYNC|REQ_META|rw; 375 bio->bi_iter.bi_size = KEY_SIZE(k) << 9; 376 377 bio->bi_end_io = uuid_endio; 378 bio->bi_private = cl; 379 bch_bio_map(bio, c->uuids); 380 381 bch_submit_bbio(bio, c, k, i); 382 383 if (!(rw & WRITE)) 384 break; 385 } 386 387 bch_extent_to_text(buf, sizeof(buf), k); 388 pr_debug("%s UUIDs at %s", rw & REQ_WRITE ? "wrote" : "read", buf); 389 390 for (u = c->uuids; u < c->uuids + c->nr_uuids; u++) 391 if (!bch_is_zero(u->uuid, 16)) 392 pr_debug("Slot %zi: %pU: %s: 1st: %u last: %u inv: %u", 393 u - c->uuids, u->uuid, u->label, 394 u->first_reg, u->last_reg, u->invalidated); 395 396 closure_return_with_destructor(cl, uuid_io_unlock); 397 } 398 399 static char *uuid_read(struct cache_set *c, struct jset *j, struct closure *cl) 400 { 401 struct bkey *k = &j->uuid_bucket; 402 403 if (__bch_btree_ptr_invalid(c, k)) 404 return "bad uuid pointer"; 405 406 bkey_copy(&c->uuid_bucket, k); 407 uuid_io(c, READ_SYNC, k, cl); 408 409 if (j->version < BCACHE_JSET_VERSION_UUIDv1) { 410 struct uuid_entry_v0 *u0 = (void *) c->uuids; 411 struct uuid_entry *u1 = (void *) c->uuids; 412 int i; 413 414 closure_sync(cl); 415 416 /* 417 * Since the new uuid entry is bigger than the old, we have to 418 * convert starting at the highest memory address and work down 419 * in order to do it in place 420 */ 421 422 for (i = c->nr_uuids - 1; 423 i >= 0; 424 --i) { 425 memcpy(u1[i].uuid, u0[i].uuid, 16); 426 memcpy(u1[i].label, u0[i].label, 32); 427 428 u1[i].first_reg = u0[i].first_reg; 429 u1[i].last_reg = u0[i].last_reg; 430 u1[i].invalidated = u0[i].invalidated; 431 432 u1[i].flags = 0; 433 u1[i].sectors = 0; 434 } 435 } 436 437 return NULL; 438 } 439 440 static int __uuid_write(struct cache_set *c) 441 { 442 BKEY_PADDED(key) k; 443 struct closure cl; 444 closure_init_stack(&cl); 445 446 lockdep_assert_held(&bch_register_lock); 447 448 if (bch_bucket_alloc_set(c, RESERVE_BTREE, &k.key, 1, true)) 449 return 1; 450 451 SET_KEY_SIZE(&k.key, c->sb.bucket_size); 452 uuid_io(c, REQ_WRITE, &k.key, &cl); 453 closure_sync(&cl); 454 455 bkey_copy(&c->uuid_bucket, &k.key); 456 bkey_put(c, &k.key); 457 return 0; 458 } 459 460 int bch_uuid_write(struct cache_set *c) 461 { 462 int ret = __uuid_write(c); 463 464 if (!ret) 465 bch_journal_meta(c, NULL); 466 467 return ret; 468 } 469 470 static struct uuid_entry *uuid_find(struct cache_set *c, const char *uuid) 471 { 472 struct uuid_entry *u; 473 474 for (u = c->uuids; 475 u < c->uuids + c->nr_uuids; u++) 476 if (!memcmp(u->uuid, uuid, 16)) 477 return u; 478 479 return NULL; 480 } 481 482 static struct uuid_entry *uuid_find_empty(struct cache_set *c) 483 { 484 static const char zero_uuid[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; 485 return uuid_find(c, zero_uuid); 486 } 487 488 /* 489 * Bucket priorities/gens: 490 * 491 * For each bucket, we store on disk its 492 * 8 bit gen 493 * 16 bit priority 494 * 495 * See alloc.c for an explanation of the gen. The priority is used to implement 496 * lru (and in the future other) cache replacement policies; for most purposes 497 * it's just an opaque integer. 498 * 499 * The gens and the priorities don't have a whole lot to do with each other, and 500 * it's actually the gens that must be written out at specific times - it's no 501 * big deal if the priorities don't get written, if we lose them we just reuse 502 * buckets in suboptimal order. 503 * 504 * On disk they're stored in a packed array, and in as many buckets are required 505 * to fit them all. The buckets we use to store them form a list; the journal 506 * header points to the first bucket, the first bucket points to the second 507 * bucket, et cetera. 508 * 509 * This code is used by the allocation code; periodically (whenever it runs out 510 * of buckets to allocate from) the allocation code will invalidate some 511 * buckets, but it can't use those buckets until their new gens are safely on 512 * disk. 513 */ 514 515 static void prio_endio(struct bio *bio, int error) 516 { 517 struct cache *ca = bio->bi_private; 518 519 cache_set_err_on(error, ca->set, "accessing priorities"); 520 bch_bbio_free(bio, ca->set); 521 closure_put(&ca->prio); 522 } 523 524 static void prio_io(struct cache *ca, uint64_t bucket, unsigned long rw) 525 { 526 struct closure *cl = &ca->prio; 527 struct bio *bio = bch_bbio_alloc(ca->set); 528 529 closure_init_stack(cl); 530 531 bio->bi_iter.bi_sector = bucket * ca->sb.bucket_size; 532 bio->bi_bdev = ca->bdev; 533 bio->bi_rw = REQ_SYNC|REQ_META|rw; 534 bio->bi_iter.bi_size = bucket_bytes(ca); 535 536 bio->bi_end_io = prio_endio; 537 bio->bi_private = ca; 538 bch_bio_map(bio, ca->disk_buckets); 539 540 closure_bio_submit(bio, &ca->prio, ca); 541 closure_sync(cl); 542 } 543 544 void bch_prio_write(struct cache *ca) 545 { 546 int i; 547 struct bucket *b; 548 struct closure cl; 549 550 closure_init_stack(&cl); 551 552 lockdep_assert_held(&ca->set->bucket_lock); 553 554 ca->disk_buckets->seq++; 555 556 atomic_long_add(ca->sb.bucket_size * prio_buckets(ca), 557 &ca->meta_sectors_written); 558 559 //pr_debug("free %zu, free_inc %zu, unused %zu", fifo_used(&ca->free), 560 // fifo_used(&ca->free_inc), fifo_used(&ca->unused)); 561 562 for (i = prio_buckets(ca) - 1; i >= 0; --i) { 563 long bucket; 564 struct prio_set *p = ca->disk_buckets; 565 struct bucket_disk *d = p->data; 566 struct bucket_disk *end = d + prios_per_bucket(ca); 567 568 for (b = ca->buckets + i * prios_per_bucket(ca); 569 b < ca->buckets + ca->sb.nbuckets && d < end; 570 b++, d++) { 571 d->prio = cpu_to_le16(b->prio); 572 d->gen = b->gen; 573 } 574 575 p->next_bucket = ca->prio_buckets[i + 1]; 576 p->magic = pset_magic(&ca->sb); 577 p->csum = bch_crc64(&p->magic, bucket_bytes(ca) - 8); 578 579 bucket = bch_bucket_alloc(ca, RESERVE_PRIO, true); 580 BUG_ON(bucket == -1); 581 582 mutex_unlock(&ca->set->bucket_lock); 583 prio_io(ca, bucket, REQ_WRITE); 584 mutex_lock(&ca->set->bucket_lock); 585 586 ca->prio_buckets[i] = bucket; 587 atomic_dec_bug(&ca->buckets[bucket].pin); 588 } 589 590 mutex_unlock(&ca->set->bucket_lock); 591 592 bch_journal_meta(ca->set, &cl); 593 closure_sync(&cl); 594 595 mutex_lock(&ca->set->bucket_lock); 596 597 /* 598 * Don't want the old priorities to get garbage collected until after we 599 * finish writing the new ones, and they're journalled 600 */ 601 for (i = 0; i < prio_buckets(ca); i++) { 602 if (ca->prio_last_buckets[i]) 603 __bch_bucket_free(ca, 604 &ca->buckets[ca->prio_last_buckets[i]]); 605 606 ca->prio_last_buckets[i] = ca->prio_buckets[i]; 607 } 608 } 609 610 static void prio_read(struct cache *ca, uint64_t bucket) 611 { 612 struct prio_set *p = ca->disk_buckets; 613 struct bucket_disk *d = p->data + prios_per_bucket(ca), *end = d; 614 struct bucket *b; 615 unsigned bucket_nr = 0; 616 617 for (b = ca->buckets; 618 b < ca->buckets + ca->sb.nbuckets; 619 b++, d++) { 620 if (d == end) { 621 ca->prio_buckets[bucket_nr] = bucket; 622 ca->prio_last_buckets[bucket_nr] = bucket; 623 bucket_nr++; 624 625 prio_io(ca, bucket, READ_SYNC); 626 627 if (p->csum != bch_crc64(&p->magic, bucket_bytes(ca) - 8)) 628 pr_warn("bad csum reading priorities"); 629 630 if (p->magic != pset_magic(&ca->sb)) 631 pr_warn("bad magic reading priorities"); 632 633 bucket = p->next_bucket; 634 d = p->data; 635 } 636 637 b->prio = le16_to_cpu(d->prio); 638 b->gen = b->last_gc = d->gen; 639 } 640 } 641 642 /* Bcache device */ 643 644 static int open_dev(struct block_device *b, fmode_t mode) 645 { 646 struct bcache_device *d = b->bd_disk->private_data; 647 if (test_bit(BCACHE_DEV_CLOSING, &d->flags)) 648 return -ENXIO; 649 650 closure_get(&d->cl); 651 return 0; 652 } 653 654 static void release_dev(struct gendisk *b, fmode_t mode) 655 { 656 struct bcache_device *d = b->private_data; 657 closure_put(&d->cl); 658 } 659 660 static int ioctl_dev(struct block_device *b, fmode_t mode, 661 unsigned int cmd, unsigned long arg) 662 { 663 struct bcache_device *d = b->bd_disk->private_data; 664 return d->ioctl(d, mode, cmd, arg); 665 } 666 667 static const struct block_device_operations bcache_ops = { 668 .open = open_dev, 669 .release = release_dev, 670 .ioctl = ioctl_dev, 671 .owner = THIS_MODULE, 672 }; 673 674 void bcache_device_stop(struct bcache_device *d) 675 { 676 if (!test_and_set_bit(BCACHE_DEV_CLOSING, &d->flags)) 677 closure_queue(&d->cl); 678 } 679 680 static void bcache_device_unlink(struct bcache_device *d) 681 { 682 lockdep_assert_held(&bch_register_lock); 683 684 if (d->c && !test_and_set_bit(BCACHE_DEV_UNLINK_DONE, &d->flags)) { 685 unsigned i; 686 struct cache *ca; 687 688 sysfs_remove_link(&d->c->kobj, d->name); 689 sysfs_remove_link(&d->kobj, "cache"); 690 691 for_each_cache(ca, d->c, i) 692 bd_unlink_disk_holder(ca->bdev, d->disk); 693 } 694 } 695 696 static void bcache_device_link(struct bcache_device *d, struct cache_set *c, 697 const char *name) 698 { 699 unsigned i; 700 struct cache *ca; 701 702 for_each_cache(ca, d->c, i) 703 bd_link_disk_holder(ca->bdev, d->disk); 704 705 snprintf(d->name, BCACHEDEVNAME_SIZE, 706 "%s%u", name, d->id); 707 708 WARN(sysfs_create_link(&d->kobj, &c->kobj, "cache") || 709 sysfs_create_link(&c->kobj, &d->kobj, d->name), 710 "Couldn't create device <-> cache set symlinks"); 711 } 712 713 static void bcache_device_detach(struct bcache_device *d) 714 { 715 lockdep_assert_held(&bch_register_lock); 716 717 if (test_bit(BCACHE_DEV_DETACHING, &d->flags)) { 718 struct uuid_entry *u = d->c->uuids + d->id; 719 720 SET_UUID_FLASH_ONLY(u, 0); 721 memcpy(u->uuid, invalid_uuid, 16); 722 u->invalidated = cpu_to_le32(get_seconds()); 723 bch_uuid_write(d->c); 724 } 725 726 bcache_device_unlink(d); 727 728 d->c->devices[d->id] = NULL; 729 closure_put(&d->c->caching); 730 d->c = NULL; 731 } 732 733 static void bcache_device_attach(struct bcache_device *d, struct cache_set *c, 734 unsigned id) 735 { 736 d->id = id; 737 d->c = c; 738 c->devices[id] = d; 739 740 closure_get(&c->caching); 741 } 742 743 static void bcache_device_free(struct bcache_device *d) 744 { 745 lockdep_assert_held(&bch_register_lock); 746 747 pr_info("%s stopped", d->disk->disk_name); 748 749 if (d->c) 750 bcache_device_detach(d); 751 if (d->disk && d->disk->flags & GENHD_FL_UP) 752 del_gendisk(d->disk); 753 if (d->disk && d->disk->queue) 754 blk_cleanup_queue(d->disk->queue); 755 if (d->disk) { 756 ida_simple_remove(&bcache_minor, d->disk->first_minor); 757 put_disk(d->disk); 758 } 759 760 bio_split_pool_free(&d->bio_split_hook); 761 if (d->bio_split) 762 bioset_free(d->bio_split); 763 if (is_vmalloc_addr(d->full_dirty_stripes)) 764 vfree(d->full_dirty_stripes); 765 else 766 kfree(d->full_dirty_stripes); 767 if (is_vmalloc_addr(d->stripe_sectors_dirty)) 768 vfree(d->stripe_sectors_dirty); 769 else 770 kfree(d->stripe_sectors_dirty); 771 772 closure_debug_destroy(&d->cl); 773 } 774 775 static int bcache_device_init(struct bcache_device *d, unsigned block_size, 776 sector_t sectors) 777 { 778 struct request_queue *q; 779 size_t n; 780 int minor; 781 782 if (!d->stripe_size) 783 d->stripe_size = 1 << 31; 784 785 d->nr_stripes = DIV_ROUND_UP_ULL(sectors, d->stripe_size); 786 787 if (!d->nr_stripes || 788 d->nr_stripes > INT_MAX || 789 d->nr_stripes > SIZE_MAX / sizeof(atomic_t)) { 790 pr_err("nr_stripes too large"); 791 return -ENOMEM; 792 } 793 794 n = d->nr_stripes * sizeof(atomic_t); 795 d->stripe_sectors_dirty = n < PAGE_SIZE << 6 796 ? kzalloc(n, GFP_KERNEL) 797 : vzalloc(n); 798 if (!d->stripe_sectors_dirty) 799 return -ENOMEM; 800 801 n = BITS_TO_LONGS(d->nr_stripes) * sizeof(unsigned long); 802 d->full_dirty_stripes = n < PAGE_SIZE << 6 803 ? kzalloc(n, GFP_KERNEL) 804 : vzalloc(n); 805 if (!d->full_dirty_stripes) 806 return -ENOMEM; 807 808 minor = ida_simple_get(&bcache_minor, 0, MINORMASK + 1, GFP_KERNEL); 809 if (minor < 0) 810 return minor; 811 812 if (!(d->bio_split = bioset_create(4, offsetof(struct bbio, bio))) || 813 bio_split_pool_init(&d->bio_split_hook) || 814 !(d->disk = alloc_disk(1))) { 815 ida_simple_remove(&bcache_minor, minor); 816 return -ENOMEM; 817 } 818 819 set_capacity(d->disk, sectors); 820 snprintf(d->disk->disk_name, DISK_NAME_LEN, "bcache%i", minor); 821 822 d->disk->major = bcache_major; 823 d->disk->first_minor = minor; 824 d->disk->fops = &bcache_ops; 825 d->disk->private_data = d; 826 827 q = blk_alloc_queue(GFP_KERNEL); 828 if (!q) 829 return -ENOMEM; 830 831 blk_queue_make_request(q, NULL); 832 d->disk->queue = q; 833 q->queuedata = d; 834 q->backing_dev_info.congested_data = d; 835 q->limits.max_hw_sectors = UINT_MAX; 836 q->limits.max_sectors = UINT_MAX; 837 q->limits.max_segment_size = UINT_MAX; 838 q->limits.max_segments = BIO_MAX_PAGES; 839 q->limits.max_discard_sectors = UINT_MAX; 840 q->limits.discard_granularity = 512; 841 q->limits.io_min = block_size; 842 q->limits.logical_block_size = block_size; 843 q->limits.physical_block_size = block_size; 844 set_bit(QUEUE_FLAG_NONROT, &d->disk->queue->queue_flags); 845 set_bit(QUEUE_FLAG_DISCARD, &d->disk->queue->queue_flags); 846 847 blk_queue_flush(q, REQ_FLUSH|REQ_FUA); 848 849 return 0; 850 } 851 852 /* Cached device */ 853 854 static void calc_cached_dev_sectors(struct cache_set *c) 855 { 856 uint64_t sectors = 0; 857 struct cached_dev *dc; 858 859 list_for_each_entry(dc, &c->cached_devs, list) 860 sectors += bdev_sectors(dc->bdev); 861 862 c->cached_dev_sectors = sectors; 863 } 864 865 void bch_cached_dev_run(struct cached_dev *dc) 866 { 867 struct bcache_device *d = &dc->disk; 868 char buf[SB_LABEL_SIZE + 1]; 869 char *env[] = { 870 "DRIVER=bcache", 871 kasprintf(GFP_KERNEL, "CACHED_UUID=%pU", dc->sb.uuid), 872 NULL, 873 NULL, 874 }; 875 876 memcpy(buf, dc->sb.label, SB_LABEL_SIZE); 877 buf[SB_LABEL_SIZE] = '\0'; 878 env[2] = kasprintf(GFP_KERNEL, "CACHED_LABEL=%s", buf); 879 880 if (atomic_xchg(&dc->running, 1)) 881 return; 882 883 if (!d->c && 884 BDEV_STATE(&dc->sb) != BDEV_STATE_NONE) { 885 struct closure cl; 886 closure_init_stack(&cl); 887 888 SET_BDEV_STATE(&dc->sb, BDEV_STATE_STALE); 889 bch_write_bdev_super(dc, &cl); 890 closure_sync(&cl); 891 } 892 893 add_disk(d->disk); 894 bd_link_disk_holder(dc->bdev, dc->disk.disk); 895 /* won't show up in the uevent file, use udevadm monitor -e instead 896 * only class / kset properties are persistent */ 897 kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env); 898 kfree(env[1]); 899 kfree(env[2]); 900 901 if (sysfs_create_link(&d->kobj, &disk_to_dev(d->disk)->kobj, "dev") || 902 sysfs_create_link(&disk_to_dev(d->disk)->kobj, &d->kobj, "bcache")) 903 pr_debug("error creating sysfs link"); 904 } 905 906 static void cached_dev_detach_finish(struct work_struct *w) 907 { 908 struct cached_dev *dc = container_of(w, struct cached_dev, detach); 909 char buf[BDEVNAME_SIZE]; 910 struct closure cl; 911 closure_init_stack(&cl); 912 913 BUG_ON(!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags)); 914 BUG_ON(atomic_read(&dc->count)); 915 916 mutex_lock(&bch_register_lock); 917 918 memset(&dc->sb.set_uuid, 0, 16); 919 SET_BDEV_STATE(&dc->sb, BDEV_STATE_NONE); 920 921 bch_write_bdev_super(dc, &cl); 922 closure_sync(&cl); 923 924 bcache_device_detach(&dc->disk); 925 list_move(&dc->list, &uncached_devices); 926 927 clear_bit(BCACHE_DEV_DETACHING, &dc->disk.flags); 928 clear_bit(BCACHE_DEV_UNLINK_DONE, &dc->disk.flags); 929 930 mutex_unlock(&bch_register_lock); 931 932 pr_info("Caching disabled for %s", bdevname(dc->bdev, buf)); 933 934 /* Drop ref we took in cached_dev_detach() */ 935 closure_put(&dc->disk.cl); 936 } 937 938 void bch_cached_dev_detach(struct cached_dev *dc) 939 { 940 lockdep_assert_held(&bch_register_lock); 941 942 if (test_bit(BCACHE_DEV_CLOSING, &dc->disk.flags)) 943 return; 944 945 if (test_and_set_bit(BCACHE_DEV_DETACHING, &dc->disk.flags)) 946 return; 947 948 /* 949 * Block the device from being closed and freed until we're finished 950 * detaching 951 */ 952 closure_get(&dc->disk.cl); 953 954 bch_writeback_queue(dc); 955 cached_dev_put(dc); 956 } 957 958 int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c) 959 { 960 uint32_t rtime = cpu_to_le32(get_seconds()); 961 struct uuid_entry *u; 962 char buf[BDEVNAME_SIZE]; 963 964 bdevname(dc->bdev, buf); 965 966 if (memcmp(dc->sb.set_uuid, c->sb.set_uuid, 16)) 967 return -ENOENT; 968 969 if (dc->disk.c) { 970 pr_err("Can't attach %s: already attached", buf); 971 return -EINVAL; 972 } 973 974 if (test_bit(CACHE_SET_STOPPING, &c->flags)) { 975 pr_err("Can't attach %s: shutting down", buf); 976 return -EINVAL; 977 } 978 979 if (dc->sb.block_size < c->sb.block_size) { 980 /* Will die */ 981 pr_err("Couldn't attach %s: block size less than set's block size", 982 buf); 983 return -EINVAL; 984 } 985 986 u = uuid_find(c, dc->sb.uuid); 987 988 if (u && 989 (BDEV_STATE(&dc->sb) == BDEV_STATE_STALE || 990 BDEV_STATE(&dc->sb) == BDEV_STATE_NONE)) { 991 memcpy(u->uuid, invalid_uuid, 16); 992 u->invalidated = cpu_to_le32(get_seconds()); 993 u = NULL; 994 } 995 996 if (!u) { 997 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) { 998 pr_err("Couldn't find uuid for %s in set", buf); 999 return -ENOENT; 1000 } 1001 1002 u = uuid_find_empty(c); 1003 if (!u) { 1004 pr_err("Not caching %s, no room for UUID", buf); 1005 return -EINVAL; 1006 } 1007 } 1008 1009 /* Deadlocks since we're called via sysfs... 1010 sysfs_remove_file(&dc->kobj, &sysfs_attach); 1011 */ 1012 1013 if (bch_is_zero(u->uuid, 16)) { 1014 struct closure cl; 1015 closure_init_stack(&cl); 1016 1017 memcpy(u->uuid, dc->sb.uuid, 16); 1018 memcpy(u->label, dc->sb.label, SB_LABEL_SIZE); 1019 u->first_reg = u->last_reg = rtime; 1020 bch_uuid_write(c); 1021 1022 memcpy(dc->sb.set_uuid, c->sb.set_uuid, 16); 1023 SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN); 1024 1025 bch_write_bdev_super(dc, &cl); 1026 closure_sync(&cl); 1027 } else { 1028 u->last_reg = rtime; 1029 bch_uuid_write(c); 1030 } 1031 1032 bcache_device_attach(&dc->disk, c, u - c->uuids); 1033 list_move(&dc->list, &c->cached_devs); 1034 calc_cached_dev_sectors(c); 1035 1036 smp_wmb(); 1037 /* 1038 * dc->c must be set before dc->count != 0 - paired with the mb in 1039 * cached_dev_get() 1040 */ 1041 atomic_set(&dc->count, 1); 1042 1043 if (bch_cached_dev_writeback_start(dc)) 1044 return -ENOMEM; 1045 1046 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) { 1047 bch_sectors_dirty_init(dc); 1048 atomic_set(&dc->has_dirty, 1); 1049 atomic_inc(&dc->count); 1050 bch_writeback_queue(dc); 1051 } 1052 1053 bch_cached_dev_run(dc); 1054 bcache_device_link(&dc->disk, c, "bdev"); 1055 1056 pr_info("Caching %s as %s on set %pU", 1057 bdevname(dc->bdev, buf), dc->disk.disk->disk_name, 1058 dc->disk.c->sb.set_uuid); 1059 return 0; 1060 } 1061 1062 void bch_cached_dev_release(struct kobject *kobj) 1063 { 1064 struct cached_dev *dc = container_of(kobj, struct cached_dev, 1065 disk.kobj); 1066 kfree(dc); 1067 module_put(THIS_MODULE); 1068 } 1069 1070 static void cached_dev_free(struct closure *cl) 1071 { 1072 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl); 1073 1074 cancel_delayed_work_sync(&dc->writeback_rate_update); 1075 if (!IS_ERR_OR_NULL(dc->writeback_thread)) 1076 kthread_stop(dc->writeback_thread); 1077 1078 mutex_lock(&bch_register_lock); 1079 1080 if (atomic_read(&dc->running)) 1081 bd_unlink_disk_holder(dc->bdev, dc->disk.disk); 1082 bcache_device_free(&dc->disk); 1083 list_del(&dc->list); 1084 1085 mutex_unlock(&bch_register_lock); 1086 1087 if (!IS_ERR_OR_NULL(dc->bdev)) { 1088 if (dc->bdev->bd_disk) 1089 blk_sync_queue(bdev_get_queue(dc->bdev)); 1090 1091 blkdev_put(dc->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); 1092 } 1093 1094 wake_up(&unregister_wait); 1095 1096 kobject_put(&dc->disk.kobj); 1097 } 1098 1099 static void cached_dev_flush(struct closure *cl) 1100 { 1101 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl); 1102 struct bcache_device *d = &dc->disk; 1103 1104 mutex_lock(&bch_register_lock); 1105 bcache_device_unlink(d); 1106 mutex_unlock(&bch_register_lock); 1107 1108 bch_cache_accounting_destroy(&dc->accounting); 1109 kobject_del(&d->kobj); 1110 1111 continue_at(cl, cached_dev_free, system_wq); 1112 } 1113 1114 static int cached_dev_init(struct cached_dev *dc, unsigned block_size) 1115 { 1116 int ret; 1117 struct io *io; 1118 struct request_queue *q = bdev_get_queue(dc->bdev); 1119 1120 __module_get(THIS_MODULE); 1121 INIT_LIST_HEAD(&dc->list); 1122 closure_init(&dc->disk.cl, NULL); 1123 set_closure_fn(&dc->disk.cl, cached_dev_flush, system_wq); 1124 kobject_init(&dc->disk.kobj, &bch_cached_dev_ktype); 1125 INIT_WORK(&dc->detach, cached_dev_detach_finish); 1126 sema_init(&dc->sb_write_mutex, 1); 1127 INIT_LIST_HEAD(&dc->io_lru); 1128 spin_lock_init(&dc->io_lock); 1129 bch_cache_accounting_init(&dc->accounting, &dc->disk.cl); 1130 1131 dc->sequential_cutoff = 4 << 20; 1132 1133 for (io = dc->io; io < dc->io + RECENT_IO; io++) { 1134 list_add(&io->lru, &dc->io_lru); 1135 hlist_add_head(&io->hash, dc->io_hash + RECENT_IO); 1136 } 1137 1138 dc->disk.stripe_size = q->limits.io_opt >> 9; 1139 1140 if (dc->disk.stripe_size) 1141 dc->partial_stripes_expensive = 1142 q->limits.raid_partial_stripes_expensive; 1143 1144 ret = bcache_device_init(&dc->disk, block_size, 1145 dc->bdev->bd_part->nr_sects - dc->sb.data_offset); 1146 if (ret) 1147 return ret; 1148 1149 set_capacity(dc->disk.disk, 1150 dc->bdev->bd_part->nr_sects - dc->sb.data_offset); 1151 1152 dc->disk.disk->queue->backing_dev_info.ra_pages = 1153 max(dc->disk.disk->queue->backing_dev_info.ra_pages, 1154 q->backing_dev_info.ra_pages); 1155 1156 bch_cached_dev_request_init(dc); 1157 bch_cached_dev_writeback_init(dc); 1158 return 0; 1159 } 1160 1161 /* Cached device - bcache superblock */ 1162 1163 static void register_bdev(struct cache_sb *sb, struct page *sb_page, 1164 struct block_device *bdev, 1165 struct cached_dev *dc) 1166 { 1167 char name[BDEVNAME_SIZE]; 1168 const char *err = "cannot allocate memory"; 1169 struct cache_set *c; 1170 1171 memcpy(&dc->sb, sb, sizeof(struct cache_sb)); 1172 dc->bdev = bdev; 1173 dc->bdev->bd_holder = dc; 1174 1175 bio_init(&dc->sb_bio); 1176 dc->sb_bio.bi_max_vecs = 1; 1177 dc->sb_bio.bi_io_vec = dc->sb_bio.bi_inline_vecs; 1178 dc->sb_bio.bi_io_vec[0].bv_page = sb_page; 1179 get_page(sb_page); 1180 1181 if (cached_dev_init(dc, sb->block_size << 9)) 1182 goto err; 1183 1184 err = "error creating kobject"; 1185 if (kobject_add(&dc->disk.kobj, &part_to_dev(bdev->bd_part)->kobj, 1186 "bcache")) 1187 goto err; 1188 if (bch_cache_accounting_add_kobjs(&dc->accounting, &dc->disk.kobj)) 1189 goto err; 1190 1191 pr_info("registered backing device %s", bdevname(bdev, name)); 1192 1193 list_add(&dc->list, &uncached_devices); 1194 list_for_each_entry(c, &bch_cache_sets, list) 1195 bch_cached_dev_attach(dc, c); 1196 1197 if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE || 1198 BDEV_STATE(&dc->sb) == BDEV_STATE_STALE) 1199 bch_cached_dev_run(dc); 1200 1201 return; 1202 err: 1203 pr_notice("error opening %s: %s", bdevname(bdev, name), err); 1204 bcache_device_stop(&dc->disk); 1205 } 1206 1207 /* Flash only volumes */ 1208 1209 void bch_flash_dev_release(struct kobject *kobj) 1210 { 1211 struct bcache_device *d = container_of(kobj, struct bcache_device, 1212 kobj); 1213 kfree(d); 1214 } 1215 1216 static void flash_dev_free(struct closure *cl) 1217 { 1218 struct bcache_device *d = container_of(cl, struct bcache_device, cl); 1219 mutex_lock(&bch_register_lock); 1220 bcache_device_free(d); 1221 mutex_unlock(&bch_register_lock); 1222 kobject_put(&d->kobj); 1223 } 1224 1225 static void flash_dev_flush(struct closure *cl) 1226 { 1227 struct bcache_device *d = container_of(cl, struct bcache_device, cl); 1228 1229 mutex_lock(&bch_register_lock); 1230 bcache_device_unlink(d); 1231 mutex_unlock(&bch_register_lock); 1232 kobject_del(&d->kobj); 1233 continue_at(cl, flash_dev_free, system_wq); 1234 } 1235 1236 static int flash_dev_run(struct cache_set *c, struct uuid_entry *u) 1237 { 1238 struct bcache_device *d = kzalloc(sizeof(struct bcache_device), 1239 GFP_KERNEL); 1240 if (!d) 1241 return -ENOMEM; 1242 1243 closure_init(&d->cl, NULL); 1244 set_closure_fn(&d->cl, flash_dev_flush, system_wq); 1245 1246 kobject_init(&d->kobj, &bch_flash_dev_ktype); 1247 1248 if (bcache_device_init(d, block_bytes(c), u->sectors)) 1249 goto err; 1250 1251 bcache_device_attach(d, c, u - c->uuids); 1252 bch_flash_dev_request_init(d); 1253 add_disk(d->disk); 1254 1255 if (kobject_add(&d->kobj, &disk_to_dev(d->disk)->kobj, "bcache")) 1256 goto err; 1257 1258 bcache_device_link(d, c, "volume"); 1259 1260 return 0; 1261 err: 1262 kobject_put(&d->kobj); 1263 return -ENOMEM; 1264 } 1265 1266 static int flash_devs_run(struct cache_set *c) 1267 { 1268 int ret = 0; 1269 struct uuid_entry *u; 1270 1271 for (u = c->uuids; 1272 u < c->uuids + c->nr_uuids && !ret; 1273 u++) 1274 if (UUID_FLASH_ONLY(u)) 1275 ret = flash_dev_run(c, u); 1276 1277 return ret; 1278 } 1279 1280 int bch_flash_dev_create(struct cache_set *c, uint64_t size) 1281 { 1282 struct uuid_entry *u; 1283 1284 if (test_bit(CACHE_SET_STOPPING, &c->flags)) 1285 return -EINTR; 1286 1287 if (!test_bit(CACHE_SET_RUNNING, &c->flags)) 1288 return -EPERM; 1289 1290 u = uuid_find_empty(c); 1291 if (!u) { 1292 pr_err("Can't create volume, no room for UUID"); 1293 return -EINVAL; 1294 } 1295 1296 get_random_bytes(u->uuid, 16); 1297 memset(u->label, 0, 32); 1298 u->first_reg = u->last_reg = cpu_to_le32(get_seconds()); 1299 1300 SET_UUID_FLASH_ONLY(u, 1); 1301 u->sectors = size >> 9; 1302 1303 bch_uuid_write(c); 1304 1305 return flash_dev_run(c, u); 1306 } 1307 1308 /* Cache set */ 1309 1310 __printf(2, 3) 1311 bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...) 1312 { 1313 va_list args; 1314 1315 if (c->on_error != ON_ERROR_PANIC && 1316 test_bit(CACHE_SET_STOPPING, &c->flags)) 1317 return false; 1318 1319 /* XXX: we can be called from atomic context 1320 acquire_console_sem(); 1321 */ 1322 1323 printk(KERN_ERR "bcache: error on %pU: ", c->sb.set_uuid); 1324 1325 va_start(args, fmt); 1326 vprintk(fmt, args); 1327 va_end(args); 1328 1329 printk(", disabling caching\n"); 1330 1331 if (c->on_error == ON_ERROR_PANIC) 1332 panic("panic forced after error\n"); 1333 1334 bch_cache_set_unregister(c); 1335 return true; 1336 } 1337 1338 void bch_cache_set_release(struct kobject *kobj) 1339 { 1340 struct cache_set *c = container_of(kobj, struct cache_set, kobj); 1341 kfree(c); 1342 module_put(THIS_MODULE); 1343 } 1344 1345 static void cache_set_free(struct closure *cl) 1346 { 1347 struct cache_set *c = container_of(cl, struct cache_set, cl); 1348 struct cache *ca; 1349 unsigned i; 1350 1351 if (!IS_ERR_OR_NULL(c->debug)) 1352 debugfs_remove(c->debug); 1353 1354 bch_open_buckets_free(c); 1355 bch_btree_cache_free(c); 1356 bch_journal_free(c); 1357 1358 for_each_cache(ca, c, i) 1359 if (ca) { 1360 ca->set = NULL; 1361 c->cache[ca->sb.nr_this_dev] = NULL; 1362 kobject_put(&ca->kobj); 1363 } 1364 1365 bch_bset_sort_state_free(&c->sort); 1366 free_pages((unsigned long) c->uuids, ilog2(bucket_pages(c))); 1367 1368 if (c->moving_gc_wq) 1369 destroy_workqueue(c->moving_gc_wq); 1370 if (c->bio_split) 1371 bioset_free(c->bio_split); 1372 if (c->fill_iter) 1373 mempool_destroy(c->fill_iter); 1374 if (c->bio_meta) 1375 mempool_destroy(c->bio_meta); 1376 if (c->search) 1377 mempool_destroy(c->search); 1378 kfree(c->devices); 1379 1380 mutex_lock(&bch_register_lock); 1381 list_del(&c->list); 1382 mutex_unlock(&bch_register_lock); 1383 1384 pr_info("Cache set %pU unregistered", c->sb.set_uuid); 1385 wake_up(&unregister_wait); 1386 1387 closure_debug_destroy(&c->cl); 1388 kobject_put(&c->kobj); 1389 } 1390 1391 static void cache_set_flush(struct closure *cl) 1392 { 1393 struct cache_set *c = container_of(cl, struct cache_set, caching); 1394 struct cache *ca; 1395 struct btree *b; 1396 unsigned i; 1397 1398 bch_cache_accounting_destroy(&c->accounting); 1399 1400 kobject_put(&c->internal); 1401 kobject_del(&c->kobj); 1402 1403 if (c->gc_thread) 1404 kthread_stop(c->gc_thread); 1405 1406 if (!IS_ERR_OR_NULL(c->root)) 1407 list_add(&c->root->list, &c->btree_cache); 1408 1409 /* Should skip this if we're unregistering because of an error */ 1410 list_for_each_entry(b, &c->btree_cache, list) { 1411 mutex_lock(&b->write_lock); 1412 if (btree_node_dirty(b)) 1413 __bch_btree_node_write(b, NULL); 1414 mutex_unlock(&b->write_lock); 1415 } 1416 1417 for_each_cache(ca, c, i) 1418 if (ca->alloc_thread) 1419 kthread_stop(ca->alloc_thread); 1420 1421 if (c->journal.cur) { 1422 cancel_delayed_work_sync(&c->journal.work); 1423 /* flush last journal entry if needed */ 1424 c->journal.work.work.func(&c->journal.work.work); 1425 } 1426 1427 closure_return(cl); 1428 } 1429 1430 static void __cache_set_unregister(struct closure *cl) 1431 { 1432 struct cache_set *c = container_of(cl, struct cache_set, caching); 1433 struct cached_dev *dc; 1434 size_t i; 1435 1436 mutex_lock(&bch_register_lock); 1437 1438 for (i = 0; i < c->nr_uuids; i++) 1439 if (c->devices[i]) { 1440 if (!UUID_FLASH_ONLY(&c->uuids[i]) && 1441 test_bit(CACHE_SET_UNREGISTERING, &c->flags)) { 1442 dc = container_of(c->devices[i], 1443 struct cached_dev, disk); 1444 bch_cached_dev_detach(dc); 1445 } else { 1446 bcache_device_stop(c->devices[i]); 1447 } 1448 } 1449 1450 mutex_unlock(&bch_register_lock); 1451 1452 continue_at(cl, cache_set_flush, system_wq); 1453 } 1454 1455 void bch_cache_set_stop(struct cache_set *c) 1456 { 1457 if (!test_and_set_bit(CACHE_SET_STOPPING, &c->flags)) 1458 closure_queue(&c->caching); 1459 } 1460 1461 void bch_cache_set_unregister(struct cache_set *c) 1462 { 1463 set_bit(CACHE_SET_UNREGISTERING, &c->flags); 1464 bch_cache_set_stop(c); 1465 } 1466 1467 #define alloc_bucket_pages(gfp, c) \ 1468 ((void *) __get_free_pages(__GFP_ZERO|gfp, ilog2(bucket_pages(c)))) 1469 1470 struct cache_set *bch_cache_set_alloc(struct cache_sb *sb) 1471 { 1472 int iter_size; 1473 struct cache_set *c = kzalloc(sizeof(struct cache_set), GFP_KERNEL); 1474 if (!c) 1475 return NULL; 1476 1477 __module_get(THIS_MODULE); 1478 closure_init(&c->cl, NULL); 1479 set_closure_fn(&c->cl, cache_set_free, system_wq); 1480 1481 closure_init(&c->caching, &c->cl); 1482 set_closure_fn(&c->caching, __cache_set_unregister, system_wq); 1483 1484 /* Maybe create continue_at_noreturn() and use it here? */ 1485 closure_set_stopped(&c->cl); 1486 closure_put(&c->cl); 1487 1488 kobject_init(&c->kobj, &bch_cache_set_ktype); 1489 kobject_init(&c->internal, &bch_cache_set_internal_ktype); 1490 1491 bch_cache_accounting_init(&c->accounting, &c->cl); 1492 1493 memcpy(c->sb.set_uuid, sb->set_uuid, 16); 1494 c->sb.block_size = sb->block_size; 1495 c->sb.bucket_size = sb->bucket_size; 1496 c->sb.nr_in_set = sb->nr_in_set; 1497 c->sb.last_mount = sb->last_mount; 1498 c->bucket_bits = ilog2(sb->bucket_size); 1499 c->block_bits = ilog2(sb->block_size); 1500 c->nr_uuids = bucket_bytes(c) / sizeof(struct uuid_entry); 1501 1502 c->btree_pages = bucket_pages(c); 1503 if (c->btree_pages > BTREE_MAX_PAGES) 1504 c->btree_pages = max_t(int, c->btree_pages / 4, 1505 BTREE_MAX_PAGES); 1506 1507 sema_init(&c->sb_write_mutex, 1); 1508 mutex_init(&c->bucket_lock); 1509 init_waitqueue_head(&c->btree_cache_wait); 1510 init_waitqueue_head(&c->bucket_wait); 1511 sema_init(&c->uuid_write_mutex, 1); 1512 1513 spin_lock_init(&c->btree_gc_time.lock); 1514 spin_lock_init(&c->btree_split_time.lock); 1515 spin_lock_init(&c->btree_read_time.lock); 1516 1517 bch_moving_init_cache_set(c); 1518 1519 INIT_LIST_HEAD(&c->list); 1520 INIT_LIST_HEAD(&c->cached_devs); 1521 INIT_LIST_HEAD(&c->btree_cache); 1522 INIT_LIST_HEAD(&c->btree_cache_freeable); 1523 INIT_LIST_HEAD(&c->btree_cache_freed); 1524 INIT_LIST_HEAD(&c->data_buckets); 1525 1526 c->search = mempool_create_slab_pool(32, bch_search_cache); 1527 if (!c->search) 1528 goto err; 1529 1530 iter_size = (sb->bucket_size / sb->block_size + 1) * 1531 sizeof(struct btree_iter_set); 1532 1533 if (!(c->devices = kzalloc(c->nr_uuids * sizeof(void *), GFP_KERNEL)) || 1534 !(c->bio_meta = mempool_create_kmalloc_pool(2, 1535 sizeof(struct bbio) + sizeof(struct bio_vec) * 1536 bucket_pages(c))) || 1537 !(c->fill_iter = mempool_create_kmalloc_pool(1, iter_size)) || 1538 !(c->bio_split = bioset_create(4, offsetof(struct bbio, bio))) || 1539 !(c->uuids = alloc_bucket_pages(GFP_KERNEL, c)) || 1540 !(c->moving_gc_wq = create_workqueue("bcache_gc")) || 1541 bch_journal_alloc(c) || 1542 bch_btree_cache_alloc(c) || 1543 bch_open_buckets_alloc(c) || 1544 bch_bset_sort_state_init(&c->sort, ilog2(c->btree_pages))) 1545 goto err; 1546 1547 c->congested_read_threshold_us = 2000; 1548 c->congested_write_threshold_us = 20000; 1549 c->error_limit = 8 << IO_ERROR_SHIFT; 1550 1551 return c; 1552 err: 1553 bch_cache_set_unregister(c); 1554 return NULL; 1555 } 1556 1557 static void run_cache_set(struct cache_set *c) 1558 { 1559 const char *err = "cannot allocate memory"; 1560 struct cached_dev *dc, *t; 1561 struct cache *ca; 1562 struct closure cl; 1563 unsigned i; 1564 1565 closure_init_stack(&cl); 1566 1567 for_each_cache(ca, c, i) 1568 c->nbuckets += ca->sb.nbuckets; 1569 1570 if (CACHE_SYNC(&c->sb)) { 1571 LIST_HEAD(journal); 1572 struct bkey *k; 1573 struct jset *j; 1574 1575 err = "cannot allocate memory for journal"; 1576 if (bch_journal_read(c, &journal)) 1577 goto err; 1578 1579 pr_debug("btree_journal_read() done"); 1580 1581 err = "no journal entries found"; 1582 if (list_empty(&journal)) 1583 goto err; 1584 1585 j = &list_entry(journal.prev, struct journal_replay, list)->j; 1586 1587 err = "IO error reading priorities"; 1588 for_each_cache(ca, c, i) 1589 prio_read(ca, j->prio_bucket[ca->sb.nr_this_dev]); 1590 1591 /* 1592 * If prio_read() fails it'll call cache_set_error and we'll 1593 * tear everything down right away, but if we perhaps checked 1594 * sooner we could avoid journal replay. 1595 */ 1596 1597 k = &j->btree_root; 1598 1599 err = "bad btree root"; 1600 if (__bch_btree_ptr_invalid(c, k)) 1601 goto err; 1602 1603 err = "error reading btree root"; 1604 c->root = bch_btree_node_get(c, NULL, k, j->btree_level, true, NULL); 1605 if (IS_ERR_OR_NULL(c->root)) 1606 goto err; 1607 1608 list_del_init(&c->root->list); 1609 rw_unlock(true, c->root); 1610 1611 err = uuid_read(c, j, &cl); 1612 if (err) 1613 goto err; 1614 1615 err = "error in recovery"; 1616 if (bch_btree_check(c)) 1617 goto err; 1618 1619 bch_journal_mark(c, &journal); 1620 bch_initial_gc_finish(c); 1621 pr_debug("btree_check() done"); 1622 1623 /* 1624 * bcache_journal_next() can't happen sooner, or 1625 * btree_gc_finish() will give spurious errors about last_gc > 1626 * gc_gen - this is a hack but oh well. 1627 */ 1628 bch_journal_next(&c->journal); 1629 1630 err = "error starting allocator thread"; 1631 for_each_cache(ca, c, i) 1632 if (bch_cache_allocator_start(ca)) 1633 goto err; 1634 1635 /* 1636 * First place it's safe to allocate: btree_check() and 1637 * btree_gc_finish() have to run before we have buckets to 1638 * allocate, and bch_bucket_alloc_set() might cause a journal 1639 * entry to be written so bcache_journal_next() has to be called 1640 * first. 1641 * 1642 * If the uuids were in the old format we have to rewrite them 1643 * before the next journal entry is written: 1644 */ 1645 if (j->version < BCACHE_JSET_VERSION_UUID) 1646 __uuid_write(c); 1647 1648 bch_journal_replay(c, &journal); 1649 } else { 1650 pr_notice("invalidating existing data"); 1651 1652 for_each_cache(ca, c, i) { 1653 unsigned j; 1654 1655 ca->sb.keys = clamp_t(int, ca->sb.nbuckets >> 7, 1656 2, SB_JOURNAL_BUCKETS); 1657 1658 for (j = 0; j < ca->sb.keys; j++) 1659 ca->sb.d[j] = ca->sb.first_bucket + j; 1660 } 1661 1662 bch_initial_gc_finish(c); 1663 1664 err = "error starting allocator thread"; 1665 for_each_cache(ca, c, i) 1666 if (bch_cache_allocator_start(ca)) 1667 goto err; 1668 1669 mutex_lock(&c->bucket_lock); 1670 for_each_cache(ca, c, i) 1671 bch_prio_write(ca); 1672 mutex_unlock(&c->bucket_lock); 1673 1674 err = "cannot allocate new UUID bucket"; 1675 if (__uuid_write(c)) 1676 goto err; 1677 1678 err = "cannot allocate new btree root"; 1679 c->root = __bch_btree_node_alloc(c, NULL, 0, true, NULL); 1680 if (IS_ERR_OR_NULL(c->root)) 1681 goto err; 1682 1683 mutex_lock(&c->root->write_lock); 1684 bkey_copy_key(&c->root->key, &MAX_KEY); 1685 bch_btree_node_write(c->root, &cl); 1686 mutex_unlock(&c->root->write_lock); 1687 1688 bch_btree_set_root(c->root); 1689 rw_unlock(true, c->root); 1690 1691 /* 1692 * We don't want to write the first journal entry until 1693 * everything is set up - fortunately journal entries won't be 1694 * written until the SET_CACHE_SYNC() here: 1695 */ 1696 SET_CACHE_SYNC(&c->sb, true); 1697 1698 bch_journal_next(&c->journal); 1699 bch_journal_meta(c, &cl); 1700 } 1701 1702 err = "error starting gc thread"; 1703 if (bch_gc_thread_start(c)) 1704 goto err; 1705 1706 closure_sync(&cl); 1707 c->sb.last_mount = get_seconds(); 1708 bcache_write_super(c); 1709 1710 list_for_each_entry_safe(dc, t, &uncached_devices, list) 1711 bch_cached_dev_attach(dc, c); 1712 1713 flash_devs_run(c); 1714 1715 set_bit(CACHE_SET_RUNNING, &c->flags); 1716 return; 1717 err: 1718 closure_sync(&cl); 1719 /* XXX: test this, it's broken */ 1720 bch_cache_set_error(c, "%s", err); 1721 } 1722 1723 static bool can_attach_cache(struct cache *ca, struct cache_set *c) 1724 { 1725 return ca->sb.block_size == c->sb.block_size && 1726 ca->sb.bucket_size == c->sb.bucket_size && 1727 ca->sb.nr_in_set == c->sb.nr_in_set; 1728 } 1729 1730 static const char *register_cache_set(struct cache *ca) 1731 { 1732 char buf[12]; 1733 const char *err = "cannot allocate memory"; 1734 struct cache_set *c; 1735 1736 list_for_each_entry(c, &bch_cache_sets, list) 1737 if (!memcmp(c->sb.set_uuid, ca->sb.set_uuid, 16)) { 1738 if (c->cache[ca->sb.nr_this_dev]) 1739 return "duplicate cache set member"; 1740 1741 if (!can_attach_cache(ca, c)) 1742 return "cache sb does not match set"; 1743 1744 if (!CACHE_SYNC(&ca->sb)) 1745 SET_CACHE_SYNC(&c->sb, false); 1746 1747 goto found; 1748 } 1749 1750 c = bch_cache_set_alloc(&ca->sb); 1751 if (!c) 1752 return err; 1753 1754 err = "error creating kobject"; 1755 if (kobject_add(&c->kobj, bcache_kobj, "%pU", c->sb.set_uuid) || 1756 kobject_add(&c->internal, &c->kobj, "internal")) 1757 goto err; 1758 1759 if (bch_cache_accounting_add_kobjs(&c->accounting, &c->kobj)) 1760 goto err; 1761 1762 bch_debug_init_cache_set(c); 1763 1764 list_add(&c->list, &bch_cache_sets); 1765 found: 1766 sprintf(buf, "cache%i", ca->sb.nr_this_dev); 1767 if (sysfs_create_link(&ca->kobj, &c->kobj, "set") || 1768 sysfs_create_link(&c->kobj, &ca->kobj, buf)) 1769 goto err; 1770 1771 if (ca->sb.seq > c->sb.seq) { 1772 c->sb.version = ca->sb.version; 1773 memcpy(c->sb.set_uuid, ca->sb.set_uuid, 16); 1774 c->sb.flags = ca->sb.flags; 1775 c->sb.seq = ca->sb.seq; 1776 pr_debug("set version = %llu", c->sb.version); 1777 } 1778 1779 kobject_get(&ca->kobj); 1780 ca->set = c; 1781 ca->set->cache[ca->sb.nr_this_dev] = ca; 1782 c->cache_by_alloc[c->caches_loaded++] = ca; 1783 1784 if (c->caches_loaded == c->sb.nr_in_set) 1785 run_cache_set(c); 1786 1787 return NULL; 1788 err: 1789 bch_cache_set_unregister(c); 1790 return err; 1791 } 1792 1793 /* Cache device */ 1794 1795 void bch_cache_release(struct kobject *kobj) 1796 { 1797 struct cache *ca = container_of(kobj, struct cache, kobj); 1798 unsigned i; 1799 1800 if (ca->set) { 1801 BUG_ON(ca->set->cache[ca->sb.nr_this_dev] != ca); 1802 ca->set->cache[ca->sb.nr_this_dev] = NULL; 1803 } 1804 1805 bio_split_pool_free(&ca->bio_split_hook); 1806 1807 free_pages((unsigned long) ca->disk_buckets, ilog2(bucket_pages(ca))); 1808 kfree(ca->prio_buckets); 1809 vfree(ca->buckets); 1810 1811 free_heap(&ca->heap); 1812 free_fifo(&ca->free_inc); 1813 1814 for (i = 0; i < RESERVE_NR; i++) 1815 free_fifo(&ca->free[i]); 1816 1817 if (ca->sb_bio.bi_inline_vecs[0].bv_page) 1818 put_page(ca->sb_bio.bi_io_vec[0].bv_page); 1819 1820 if (!IS_ERR_OR_NULL(ca->bdev)) { 1821 blk_sync_queue(bdev_get_queue(ca->bdev)); 1822 blkdev_put(ca->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); 1823 } 1824 1825 kfree(ca); 1826 module_put(THIS_MODULE); 1827 } 1828 1829 static int cache_alloc(struct cache_sb *sb, struct cache *ca) 1830 { 1831 size_t free; 1832 struct bucket *b; 1833 1834 __module_get(THIS_MODULE); 1835 kobject_init(&ca->kobj, &bch_cache_ktype); 1836 1837 bio_init(&ca->journal.bio); 1838 ca->journal.bio.bi_max_vecs = 8; 1839 ca->journal.bio.bi_io_vec = ca->journal.bio.bi_inline_vecs; 1840 1841 free = roundup_pow_of_two(ca->sb.nbuckets) >> 10; 1842 1843 if (!init_fifo(&ca->free[RESERVE_BTREE], 8, GFP_KERNEL) || 1844 !init_fifo(&ca->free[RESERVE_PRIO], prio_buckets(ca), GFP_KERNEL) || 1845 !init_fifo(&ca->free[RESERVE_MOVINGGC], free, GFP_KERNEL) || 1846 !init_fifo(&ca->free[RESERVE_NONE], free, GFP_KERNEL) || 1847 !init_fifo(&ca->free_inc, free << 2, GFP_KERNEL) || 1848 !init_heap(&ca->heap, free << 3, GFP_KERNEL) || 1849 !(ca->buckets = vzalloc(sizeof(struct bucket) * 1850 ca->sb.nbuckets)) || 1851 !(ca->prio_buckets = kzalloc(sizeof(uint64_t) * prio_buckets(ca) * 1852 2, GFP_KERNEL)) || 1853 !(ca->disk_buckets = alloc_bucket_pages(GFP_KERNEL, ca)) || 1854 bio_split_pool_init(&ca->bio_split_hook)) 1855 return -ENOMEM; 1856 1857 ca->prio_last_buckets = ca->prio_buckets + prio_buckets(ca); 1858 1859 for_each_bucket(b, ca) 1860 atomic_set(&b->pin, 0); 1861 1862 return 0; 1863 } 1864 1865 static void register_cache(struct cache_sb *sb, struct page *sb_page, 1866 struct block_device *bdev, struct cache *ca) 1867 { 1868 char name[BDEVNAME_SIZE]; 1869 const char *err = "cannot allocate memory"; 1870 1871 memcpy(&ca->sb, sb, sizeof(struct cache_sb)); 1872 ca->bdev = bdev; 1873 ca->bdev->bd_holder = ca; 1874 1875 bio_init(&ca->sb_bio); 1876 ca->sb_bio.bi_max_vecs = 1; 1877 ca->sb_bio.bi_io_vec = ca->sb_bio.bi_inline_vecs; 1878 ca->sb_bio.bi_io_vec[0].bv_page = sb_page; 1879 get_page(sb_page); 1880 1881 if (blk_queue_discard(bdev_get_queue(ca->bdev))) 1882 ca->discard = CACHE_DISCARD(&ca->sb); 1883 1884 if (cache_alloc(sb, ca) != 0) 1885 goto err; 1886 1887 err = "error creating kobject"; 1888 if (kobject_add(&ca->kobj, &part_to_dev(bdev->bd_part)->kobj, "bcache")) 1889 goto err; 1890 1891 mutex_lock(&bch_register_lock); 1892 err = register_cache_set(ca); 1893 mutex_unlock(&bch_register_lock); 1894 1895 if (err) 1896 goto err; 1897 1898 pr_info("registered cache device %s", bdevname(bdev, name)); 1899 out: 1900 kobject_put(&ca->kobj); 1901 return; 1902 err: 1903 pr_notice("error opening %s: %s", bdevname(bdev, name), err); 1904 goto out; 1905 } 1906 1907 /* Global interfaces/init */ 1908 1909 static ssize_t register_bcache(struct kobject *, struct kobj_attribute *, 1910 const char *, size_t); 1911 1912 kobj_attribute_write(register, register_bcache); 1913 kobj_attribute_write(register_quiet, register_bcache); 1914 1915 static bool bch_is_open_backing(struct block_device *bdev) { 1916 struct cache_set *c, *tc; 1917 struct cached_dev *dc, *t; 1918 1919 list_for_each_entry_safe(c, tc, &bch_cache_sets, list) 1920 list_for_each_entry_safe(dc, t, &c->cached_devs, list) 1921 if (dc->bdev == bdev) 1922 return true; 1923 list_for_each_entry_safe(dc, t, &uncached_devices, list) 1924 if (dc->bdev == bdev) 1925 return true; 1926 return false; 1927 } 1928 1929 static bool bch_is_open_cache(struct block_device *bdev) { 1930 struct cache_set *c, *tc; 1931 struct cache *ca; 1932 unsigned i; 1933 1934 list_for_each_entry_safe(c, tc, &bch_cache_sets, list) 1935 for_each_cache(ca, c, i) 1936 if (ca->bdev == bdev) 1937 return true; 1938 return false; 1939 } 1940 1941 static bool bch_is_open(struct block_device *bdev) { 1942 return bch_is_open_cache(bdev) || bch_is_open_backing(bdev); 1943 } 1944 1945 static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr, 1946 const char *buffer, size_t size) 1947 { 1948 ssize_t ret = size; 1949 const char *err = "cannot allocate memory"; 1950 char *path = NULL; 1951 struct cache_sb *sb = NULL; 1952 struct block_device *bdev = NULL; 1953 struct page *sb_page = NULL; 1954 1955 if (!try_module_get(THIS_MODULE)) 1956 return -EBUSY; 1957 1958 if (!(path = kstrndup(buffer, size, GFP_KERNEL)) || 1959 !(sb = kmalloc(sizeof(struct cache_sb), GFP_KERNEL))) 1960 goto err; 1961 1962 err = "failed to open device"; 1963 bdev = blkdev_get_by_path(strim(path), 1964 FMODE_READ|FMODE_WRITE|FMODE_EXCL, 1965 sb); 1966 if (IS_ERR(bdev)) { 1967 if (bdev == ERR_PTR(-EBUSY)) { 1968 bdev = lookup_bdev(strim(path)); 1969 mutex_lock(&bch_register_lock); 1970 if (!IS_ERR(bdev) && bch_is_open(bdev)) 1971 err = "device already registered"; 1972 else 1973 err = "device busy"; 1974 mutex_unlock(&bch_register_lock); 1975 } 1976 goto err; 1977 } 1978 1979 err = "failed to set blocksize"; 1980 if (set_blocksize(bdev, 4096)) 1981 goto err_close; 1982 1983 err = read_super(sb, bdev, &sb_page); 1984 if (err) 1985 goto err_close; 1986 1987 if (SB_IS_BDEV(sb)) { 1988 struct cached_dev *dc = kzalloc(sizeof(*dc), GFP_KERNEL); 1989 if (!dc) 1990 goto err_close; 1991 1992 mutex_lock(&bch_register_lock); 1993 register_bdev(sb, sb_page, bdev, dc); 1994 mutex_unlock(&bch_register_lock); 1995 } else { 1996 struct cache *ca = kzalloc(sizeof(*ca), GFP_KERNEL); 1997 if (!ca) 1998 goto err_close; 1999 2000 register_cache(sb, sb_page, bdev, ca); 2001 } 2002 out: 2003 if (sb_page) 2004 put_page(sb_page); 2005 kfree(sb); 2006 kfree(path); 2007 module_put(THIS_MODULE); 2008 return ret; 2009 2010 err_close: 2011 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); 2012 err: 2013 if (attr != &ksysfs_register_quiet) 2014 pr_info("error opening %s: %s", path, err); 2015 ret = -EINVAL; 2016 goto out; 2017 } 2018 2019 static int bcache_reboot(struct notifier_block *n, unsigned long code, void *x) 2020 { 2021 if (code == SYS_DOWN || 2022 code == SYS_HALT || 2023 code == SYS_POWER_OFF) { 2024 DEFINE_WAIT(wait); 2025 unsigned long start = jiffies; 2026 bool stopped = false; 2027 2028 struct cache_set *c, *tc; 2029 struct cached_dev *dc, *tdc; 2030 2031 mutex_lock(&bch_register_lock); 2032 2033 if (list_empty(&bch_cache_sets) && 2034 list_empty(&uncached_devices)) 2035 goto out; 2036 2037 pr_info("Stopping all devices:"); 2038 2039 list_for_each_entry_safe(c, tc, &bch_cache_sets, list) 2040 bch_cache_set_stop(c); 2041 2042 list_for_each_entry_safe(dc, tdc, &uncached_devices, list) 2043 bcache_device_stop(&dc->disk); 2044 2045 /* What's a condition variable? */ 2046 while (1) { 2047 long timeout = start + 2 * HZ - jiffies; 2048 2049 stopped = list_empty(&bch_cache_sets) && 2050 list_empty(&uncached_devices); 2051 2052 if (timeout < 0 || stopped) 2053 break; 2054 2055 prepare_to_wait(&unregister_wait, &wait, 2056 TASK_UNINTERRUPTIBLE); 2057 2058 mutex_unlock(&bch_register_lock); 2059 schedule_timeout(timeout); 2060 mutex_lock(&bch_register_lock); 2061 } 2062 2063 finish_wait(&unregister_wait, &wait); 2064 2065 if (stopped) 2066 pr_info("All devices stopped"); 2067 else 2068 pr_notice("Timeout waiting for devices to be closed"); 2069 out: 2070 mutex_unlock(&bch_register_lock); 2071 } 2072 2073 return NOTIFY_DONE; 2074 } 2075 2076 static struct notifier_block reboot = { 2077 .notifier_call = bcache_reboot, 2078 .priority = INT_MAX, /* before any real devices */ 2079 }; 2080 2081 static void bcache_exit(void) 2082 { 2083 bch_debug_exit(); 2084 bch_request_exit(); 2085 if (bcache_kobj) 2086 kobject_put(bcache_kobj); 2087 if (bcache_wq) 2088 destroy_workqueue(bcache_wq); 2089 if (bcache_major) 2090 unregister_blkdev(bcache_major, "bcache"); 2091 unregister_reboot_notifier(&reboot); 2092 } 2093 2094 static int __init bcache_init(void) 2095 { 2096 static const struct attribute *files[] = { 2097 &ksysfs_register.attr, 2098 &ksysfs_register_quiet.attr, 2099 NULL 2100 }; 2101 2102 mutex_init(&bch_register_lock); 2103 init_waitqueue_head(&unregister_wait); 2104 register_reboot_notifier(&reboot); 2105 closure_debug_init(); 2106 2107 bcache_major = register_blkdev(0, "bcache"); 2108 if (bcache_major < 0) 2109 return bcache_major; 2110 2111 if (!(bcache_wq = create_workqueue("bcache")) || 2112 !(bcache_kobj = kobject_create_and_add("bcache", fs_kobj)) || 2113 sysfs_create_files(bcache_kobj, files) || 2114 bch_request_init() || 2115 bch_debug_init(bcache_kobj)) 2116 goto err; 2117 2118 return 0; 2119 err: 2120 bcache_exit(); 2121 return -ENOMEM; 2122 } 2123 2124 module_exit(bcache_exit); 2125 module_init(bcache_init); 2126