2 * bcache setup/teardown code, and some metadata io - read a superblock and
3 * figure out what to do with it.
5 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6 * Copyright 2012 Google, Inc.
13 #include "writeback.h"
15 #include <linux/blkdev.h>
16 #include <linux/buffer_head.h>
17 #include <linux/debugfs.h>
18 #include <linux/genhd.h>
19 #include <linux/idr.h>
20 #include <linux/kthread.h>
21 #include <linux/module.h>
22 #include <linux/random.h>
23 #include <linux/reboot.h>
24 #include <linux/sysfs.h>
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
29 static const char bcache_magic[] = {
30 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca,
31 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81
34 static const char invalid_uuid[] = {
35 0xa0, 0x3e, 0xf8, 0xed, 0x3e, 0xe1, 0xb8, 0x78,
36 0xc8, 0x50, 0xfc, 0x5e, 0xcb, 0x16, 0xcd, 0x99
39 /* Default is -1; we skip past it for struct cached_dev's cache mode */
40 const char * const bch_cache_modes[] = {
49 static struct kobject *bcache_kobj;
50 struct mutex bch_register_lock;
51 LIST_HEAD(bch_cache_sets);
52 static LIST_HEAD(uncached_devices);
54 static int bcache_major;
55 static DEFINE_IDA(bcache_minor);
56 static wait_queue_head_t unregister_wait;
57 struct workqueue_struct *bcache_wq;
59 #define BTREE_MAX_PAGES (256 * 1024 / PAGE_SIZE)
61 static void bio_split_pool_free(struct bio_split_pool *p)
63 if (p->bio_split_hook)
64 mempool_destroy(p->bio_split_hook);
67 bioset_free(p->bio_split);
70 static int bio_split_pool_init(struct bio_split_pool *p)
72 p->bio_split = bioset_create(4, 0);
76 p->bio_split_hook = mempool_create_kmalloc_pool(4,
77 sizeof(struct bio_split_hook));
78 if (!p->bio_split_hook)
86 static const char *read_super(struct cache_sb *sb, struct block_device *bdev,
91 struct buffer_head *bh = __bread(bdev, 1, SB_SIZE);
97 s = (struct cache_sb *) bh->b_data;
99 sb->offset = le64_to_cpu(s->offset);
100 sb->version = le64_to_cpu(s->version);
102 memcpy(sb->magic, s->magic, 16);
103 memcpy(sb->uuid, s->uuid, 16);
104 memcpy(sb->set_uuid, s->set_uuid, 16);
105 memcpy(sb->label, s->label, SB_LABEL_SIZE);
107 sb->flags = le64_to_cpu(s->flags);
108 sb->seq = le64_to_cpu(s->seq);
109 sb->last_mount = le32_to_cpu(s->last_mount);
110 sb->first_bucket = le16_to_cpu(s->first_bucket);
111 sb->keys = le16_to_cpu(s->keys);
113 for (i = 0; i < SB_JOURNAL_BUCKETS; i++)
114 sb->d[i] = le64_to_cpu(s->d[i]);
116 pr_debug("read sb version %llu, flags %llu, seq %llu, journal size %u",
117 sb->version, sb->flags, sb->seq, sb->keys);
119 err = "Not a bcache superblock";
120 if (sb->offset != SB_SECTOR)
123 if (memcmp(sb->magic, bcache_magic, 16))
126 err = "Too many journal buckets";
127 if (sb->keys > SB_JOURNAL_BUCKETS)
130 err = "Bad checksum";
131 if (s->csum != csum_set(s))
135 if (bch_is_zero(sb->uuid, 16))
138 sb->block_size = le16_to_cpu(s->block_size);
140 err = "Superblock block size smaller than device block size";
141 if (sb->block_size << 9 < bdev_logical_block_size(bdev))
144 switch (sb->version) {
145 case BCACHE_SB_VERSION_BDEV:
146 sb->data_offset = BDEV_DATA_START_DEFAULT;
148 case BCACHE_SB_VERSION_BDEV_WITH_OFFSET:
149 sb->data_offset = le64_to_cpu(s->data_offset);
151 err = "Bad data offset";
152 if (sb->data_offset < BDEV_DATA_START_DEFAULT)
156 case BCACHE_SB_VERSION_CDEV:
157 case BCACHE_SB_VERSION_CDEV_WITH_UUID:
158 sb->nbuckets = le64_to_cpu(s->nbuckets);
159 sb->block_size = le16_to_cpu(s->block_size);
160 sb->bucket_size = le16_to_cpu(s->bucket_size);
162 sb->nr_in_set = le16_to_cpu(s->nr_in_set);
163 sb->nr_this_dev = le16_to_cpu(s->nr_this_dev);
165 err = "Too many buckets";
166 if (sb->nbuckets > LONG_MAX)
169 err = "Not enough buckets";
170 if (sb->nbuckets < 1 << 7)
173 err = "Bad block/bucket size";
174 if (!is_power_of_2(sb->block_size) ||
175 sb->block_size > PAGE_SECTORS ||
176 !is_power_of_2(sb->bucket_size) ||
177 sb->bucket_size < PAGE_SECTORS)
180 err = "Invalid superblock: device too small";
181 if (get_capacity(bdev->bd_disk) < sb->bucket_size * sb->nbuckets)
185 if (bch_is_zero(sb->set_uuid, 16))
188 err = "Bad cache device number in set";
189 if (!sb->nr_in_set ||
190 sb->nr_in_set <= sb->nr_this_dev ||
191 sb->nr_in_set > MAX_CACHES_PER_SET)
194 err = "Journal buckets not sequential";
195 for (i = 0; i < sb->keys; i++)
196 if (sb->d[i] != sb->first_bucket + i)
199 err = "Too many journal buckets";
200 if (sb->first_bucket + sb->keys > sb->nbuckets)
203 err = "Invalid superblock: first bucket comes before end of super";
204 if (sb->first_bucket * sb->bucket_size < 16)
209 err = "Unsupported superblock version";
213 sb->last_mount = get_seconds();
216 get_page(bh->b_page);
223 static void write_bdev_super_endio(struct bio *bio, int error)
225 struct cached_dev *dc = bio->bi_private;
226 /* XXX: error checking */
228 closure_put(&dc->sb_write);
231 static void __write_super(struct cache_sb *sb, struct bio *bio)
233 struct cache_sb *out = page_address(bio->bi_io_vec[0].bv_page);
236 bio->bi_iter.bi_sector = SB_SECTOR;
237 bio->bi_rw = REQ_SYNC|REQ_META;
238 bio->bi_iter.bi_size = SB_SIZE;
239 bch_bio_map(bio, NULL);
241 out->offset = cpu_to_le64(sb->offset);
242 out->version = cpu_to_le64(sb->version);
244 memcpy(out->uuid, sb->uuid, 16);
245 memcpy(out->set_uuid, sb->set_uuid, 16);
246 memcpy(out->label, sb->label, SB_LABEL_SIZE);
248 out->flags = cpu_to_le64(sb->flags);
249 out->seq = cpu_to_le64(sb->seq);
251 out->last_mount = cpu_to_le32(sb->last_mount);
252 out->first_bucket = cpu_to_le16(sb->first_bucket);
253 out->keys = cpu_to_le16(sb->keys);
255 for (i = 0; i < sb->keys; i++)
256 out->d[i] = cpu_to_le64(sb->d[i]);
258 out->csum = csum_set(out);
260 pr_debug("ver %llu, flags %llu, seq %llu",
261 sb->version, sb->flags, sb->seq);
263 submit_bio(REQ_WRITE, bio);
266 static void bch_write_bdev_super_unlock(struct closure *cl)
268 struct cached_dev *dc = container_of(cl, struct cached_dev, sb_write);
270 up(&dc->sb_write_mutex);
273 void bch_write_bdev_super(struct cached_dev *dc, struct closure *parent)
275 struct closure *cl = &dc->sb_write;
276 struct bio *bio = &dc->sb_bio;
278 down(&dc->sb_write_mutex);
279 closure_init(cl, parent);
282 bio->bi_bdev = dc->bdev;
283 bio->bi_end_io = write_bdev_super_endio;
284 bio->bi_private = dc;
287 __write_super(&dc->sb, bio);
289 closure_return_with_destructor(cl, bch_write_bdev_super_unlock);
292 static void write_super_endio(struct bio *bio, int error)
294 struct cache *ca = bio->bi_private;
296 bch_count_io_errors(ca, error, "writing superblock");
297 closure_put(&ca->set->sb_write);
300 static void bcache_write_super_unlock(struct closure *cl)
302 struct cache_set *c = container_of(cl, struct cache_set, sb_write);
304 up(&c->sb_write_mutex);
307 void bcache_write_super(struct cache_set *c)
309 struct closure *cl = &c->sb_write;
313 down(&c->sb_write_mutex);
314 closure_init(cl, &c->cl);
318 for_each_cache(ca, c, i) {
319 struct bio *bio = &ca->sb_bio;
321 ca->sb.version = BCACHE_SB_VERSION_CDEV_WITH_UUID;
322 ca->sb.seq = c->sb.seq;
323 ca->sb.last_mount = c->sb.last_mount;
325 SET_CACHE_SYNC(&ca->sb, CACHE_SYNC(&c->sb));
328 bio->bi_bdev = ca->bdev;
329 bio->bi_end_io = write_super_endio;
330 bio->bi_private = ca;
333 __write_super(&ca->sb, bio);
336 closure_return_with_destructor(cl, bcache_write_super_unlock);
341 static void uuid_endio(struct bio *bio, int error)
343 struct closure *cl = bio->bi_private;
344 struct cache_set *c = container_of(cl, struct cache_set, uuid_write);
346 cache_set_err_on(error, c, "accessing uuids");
347 bch_bbio_free(bio, c);
351 static void uuid_io_unlock(struct closure *cl)
353 struct cache_set *c = container_of(cl, struct cache_set, uuid_write);
355 up(&c->uuid_write_mutex);
358 static void uuid_io(struct cache_set *c, unsigned long rw,
359 struct bkey *k, struct closure *parent)
361 struct closure *cl = &c->uuid_write;
362 struct uuid_entry *u;
367 down(&c->uuid_write_mutex);
368 closure_init(cl, parent);
370 for (i = 0; i < KEY_PTRS(k); i++) {
371 struct bio *bio = bch_bbio_alloc(c);
373 bio->bi_rw = REQ_SYNC|REQ_META|rw;
374 bio->bi_iter.bi_size = KEY_SIZE(k) << 9;
376 bio->bi_end_io = uuid_endio;
377 bio->bi_private = cl;
378 bch_bio_map(bio, c->uuids);
380 bch_submit_bbio(bio, c, k, i);
386 bch_bkey_to_text(buf, sizeof(buf), k);
387 pr_debug("%s UUIDs at %s", rw & REQ_WRITE ? "wrote" : "read", buf);
389 for (u = c->uuids; u < c->uuids + c->nr_uuids; u++)
390 if (!bch_is_zero(u->uuid, 16))
391 pr_debug("Slot %zi: %pU: %s: 1st: %u last: %u inv: %u",
392 u - c->uuids, u->uuid, u->label,
393 u->first_reg, u->last_reg, u->invalidated);
395 closure_return_with_destructor(cl, uuid_io_unlock);
398 static char *uuid_read(struct cache_set *c, struct jset *j, struct closure *cl)
400 struct bkey *k = &j->uuid_bucket;
402 if (bch_btree_ptr_invalid(c, k))
403 return "bad uuid pointer";
405 bkey_copy(&c->uuid_bucket, k);
406 uuid_io(c, READ_SYNC, k, cl);
408 if (j->version < BCACHE_JSET_VERSION_UUIDv1) {
409 struct uuid_entry_v0 *u0 = (void *) c->uuids;
410 struct uuid_entry *u1 = (void *) c->uuids;
416 * Since the new uuid entry is bigger than the old, we have to
417 * convert starting at the highest memory address and work down
418 * in order to do it in place
421 for (i = c->nr_uuids - 1;
424 memcpy(u1[i].uuid, u0[i].uuid, 16);
425 memcpy(u1[i].label, u0[i].label, 32);
427 u1[i].first_reg = u0[i].first_reg;
428 u1[i].last_reg = u0[i].last_reg;
429 u1[i].invalidated = u0[i].invalidated;
439 static int __uuid_write(struct cache_set *c)
443 closure_init_stack(&cl);
445 lockdep_assert_held(&bch_register_lock);
447 if (bch_bucket_alloc_set(c, RESERVE_BTREE, &k.key, 1, true))
450 SET_KEY_SIZE(&k.key, c->sb.bucket_size);
451 uuid_io(c, REQ_WRITE, &k.key, &cl);
454 bkey_copy(&c->uuid_bucket, &k.key);
459 int bch_uuid_write(struct cache_set *c)
461 int ret = __uuid_write(c);
464 bch_journal_meta(c, NULL);
469 static struct uuid_entry *uuid_find(struct cache_set *c, const char *uuid)
471 struct uuid_entry *u;
474 u < c->uuids + c->nr_uuids; u++)
475 if (!memcmp(u->uuid, uuid, 16))
481 static struct uuid_entry *uuid_find_empty(struct cache_set *c)
483 static const char zero_uuid[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
484 return uuid_find(c, zero_uuid);
488 * Bucket priorities/gens:
490 * For each bucket, we store on disk its
494 * See alloc.c for an explanation of the gen. The priority is used to implement
495 * lru (and in the future other) cache replacement policies; for most purposes
496 * it's just an opaque integer.
498 * The gens and the priorities don't have a whole lot to do with each other, and
499 * it's actually the gens that must be written out at specific times - it's no
500 * big deal if the priorities don't get written, if we lose them we just reuse
501 * buckets in suboptimal order.
503 * On disk they're stored in a packed array, and in as many buckets are required
504 * to fit them all. The buckets we use to store them form a list; the journal
505 * header points to the first bucket, the first bucket points to the second
508 * This code is used by the allocation code; periodically (whenever it runs out
509 * of buckets to allocate from) the allocation code will invalidate some
510 * buckets, but it can't use those buckets until their new gens are safely on
514 static void prio_endio(struct bio *bio, int error)
516 struct cache *ca = bio->bi_private;
518 cache_set_err_on(error, ca->set, "accessing priorities");
519 bch_bbio_free(bio, ca->set);
520 closure_put(&ca->prio);
523 static void prio_io(struct cache *ca, uint64_t bucket, unsigned long rw)
525 struct closure *cl = &ca->prio;
526 struct bio *bio = bch_bbio_alloc(ca->set);
528 closure_init_stack(cl);
530 bio->bi_iter.bi_sector = bucket * ca->sb.bucket_size;
531 bio->bi_bdev = ca->bdev;
532 bio->bi_rw = REQ_SYNC|REQ_META|rw;
533 bio->bi_iter.bi_size = bucket_bytes(ca);
535 bio->bi_end_io = prio_endio;
536 bio->bi_private = ca;
537 bch_bio_map(bio, ca->disk_buckets);
539 closure_bio_submit(bio, &ca->prio, ca);
543 #define buckets_free(c) "free %zu, free_inc %zu, unused %zu", \
544 fifo_used(&c->free), fifo_used(&c->free_inc), fifo_used(&c->unused)
546 void bch_prio_write(struct cache *ca)
552 closure_init_stack(&cl);
554 lockdep_assert_held(&ca->set->bucket_lock);
556 for (b = ca->buckets;
557 b < ca->buckets + ca->sb.nbuckets; b++)
558 b->disk_gen = b->gen;
560 ca->disk_buckets->seq++;
562 atomic_long_add(ca->sb.bucket_size * prio_buckets(ca),
563 &ca->meta_sectors_written);
565 //pr_debug("free %zu, free_inc %zu, unused %zu", fifo_used(&ca->free),
566 // fifo_used(&ca->free_inc), fifo_used(&ca->unused));
568 for (i = prio_buckets(ca) - 1; i >= 0; --i) {
570 struct prio_set *p = ca->disk_buckets;
571 struct bucket_disk *d = p->data;
572 struct bucket_disk *end = d + prios_per_bucket(ca);
574 for (b = ca->buckets + i * prios_per_bucket(ca);
575 b < ca->buckets + ca->sb.nbuckets && d < end;
577 d->prio = cpu_to_le16(b->prio);
581 p->next_bucket = ca->prio_buckets[i + 1];
582 p->magic = pset_magic(&ca->sb);
583 p->csum = bch_crc64(&p->magic, bucket_bytes(ca) - 8);
585 bucket = bch_bucket_alloc(ca, RESERVE_PRIO, true);
586 BUG_ON(bucket == -1);
588 mutex_unlock(&ca->set->bucket_lock);
589 prio_io(ca, bucket, REQ_WRITE);
590 mutex_lock(&ca->set->bucket_lock);
592 ca->prio_buckets[i] = bucket;
593 atomic_dec_bug(&ca->buckets[bucket].pin);
596 mutex_unlock(&ca->set->bucket_lock);
598 bch_journal_meta(ca->set, &cl);
601 mutex_lock(&ca->set->bucket_lock);
603 ca->need_save_prio = 0;
606 * Don't want the old priorities to get garbage collected until after we
607 * finish writing the new ones, and they're journalled
609 for (i = 0; i < prio_buckets(ca); i++)
610 ca->prio_last_buckets[i] = ca->prio_buckets[i];
613 static void prio_read(struct cache *ca, uint64_t bucket)
615 struct prio_set *p = ca->disk_buckets;
616 struct bucket_disk *d = p->data + prios_per_bucket(ca), *end = d;
618 unsigned bucket_nr = 0;
620 for (b = ca->buckets;
621 b < ca->buckets + ca->sb.nbuckets;
624 ca->prio_buckets[bucket_nr] = bucket;
625 ca->prio_last_buckets[bucket_nr] = bucket;
628 prio_io(ca, bucket, READ_SYNC);
630 if (p->csum != bch_crc64(&p->magic, bucket_bytes(ca) - 8))
631 pr_warn("bad csum reading priorities");
633 if (p->magic != pset_magic(&ca->sb))
634 pr_warn("bad magic reading priorities");
636 bucket = p->next_bucket;
640 b->prio = le16_to_cpu(d->prio);
641 b->gen = b->disk_gen = b->last_gc = b->gc_gen = d->gen;
647 static int open_dev(struct block_device *b, fmode_t mode)
649 struct bcache_device *d = b->bd_disk->private_data;
650 if (test_bit(BCACHE_DEV_CLOSING, &d->flags))
657 static void release_dev(struct gendisk *b, fmode_t mode)
659 struct bcache_device *d = b->private_data;
663 static int ioctl_dev(struct block_device *b, fmode_t mode,
664 unsigned int cmd, unsigned long arg)
666 struct bcache_device *d = b->bd_disk->private_data;
667 return d->ioctl(d, mode, cmd, arg);
670 static const struct block_device_operations bcache_ops = {
672 .release = release_dev,
674 .owner = THIS_MODULE,
677 void bcache_device_stop(struct bcache_device *d)
679 if (!test_and_set_bit(BCACHE_DEV_CLOSING, &d->flags))
680 closure_queue(&d->cl);
683 static void bcache_device_unlink(struct bcache_device *d)
685 lockdep_assert_held(&bch_register_lock);
687 if (d->c && !test_and_set_bit(BCACHE_DEV_UNLINK_DONE, &d->flags)) {
691 sysfs_remove_link(&d->c->kobj, d->name);
692 sysfs_remove_link(&d->kobj, "cache");
694 for_each_cache(ca, d->c, i)
695 bd_unlink_disk_holder(ca->bdev, d->disk);
699 static void bcache_device_link(struct bcache_device *d, struct cache_set *c,
705 for_each_cache(ca, d->c, i)
706 bd_link_disk_holder(ca->bdev, d->disk);
708 snprintf(d->name, BCACHEDEVNAME_SIZE,
709 "%s%u", name, d->id);
711 WARN(sysfs_create_link(&d->kobj, &c->kobj, "cache") ||
712 sysfs_create_link(&c->kobj, &d->kobj, d->name),
713 "Couldn't create device <-> cache set symlinks");
716 static void bcache_device_detach(struct bcache_device *d)
718 lockdep_assert_held(&bch_register_lock);
720 if (test_bit(BCACHE_DEV_DETACHING, &d->flags)) {
721 struct uuid_entry *u = d->c->uuids + d->id;
723 SET_UUID_FLASH_ONLY(u, 0);
724 memcpy(u->uuid, invalid_uuid, 16);
725 u->invalidated = cpu_to_le32(get_seconds());
726 bch_uuid_write(d->c);
729 bcache_device_unlink(d);
731 d->c->devices[d->id] = NULL;
732 closure_put(&d->c->caching);
736 static void bcache_device_attach(struct bcache_device *d, struct cache_set *c,
739 BUG_ON(test_bit(CACHE_SET_STOPPING, &c->flags));
745 closure_get(&c->caching);
748 static void bcache_device_free(struct bcache_device *d)
750 lockdep_assert_held(&bch_register_lock);
752 pr_info("%s stopped", d->disk->disk_name);
755 bcache_device_detach(d);
756 if (d->disk && d->disk->flags & GENHD_FL_UP)
757 del_gendisk(d->disk);
758 if (d->disk && d->disk->queue)
759 blk_cleanup_queue(d->disk->queue);
761 ida_simple_remove(&bcache_minor, d->disk->first_minor);
765 bio_split_pool_free(&d->bio_split_hook);
767 bioset_free(d->bio_split);
768 if (is_vmalloc_addr(d->full_dirty_stripes))
769 vfree(d->full_dirty_stripes);
771 kfree(d->full_dirty_stripes);
772 if (is_vmalloc_addr(d->stripe_sectors_dirty))
773 vfree(d->stripe_sectors_dirty);
775 kfree(d->stripe_sectors_dirty);
777 closure_debug_destroy(&d->cl);
780 static int bcache_device_init(struct bcache_device *d, unsigned block_size,
783 struct request_queue *q;
788 d->stripe_size = 1 << 31;
790 d->nr_stripes = DIV_ROUND_UP_ULL(sectors, d->stripe_size);
792 if (!d->nr_stripes ||
793 d->nr_stripes > INT_MAX ||
794 d->nr_stripes > SIZE_MAX / sizeof(atomic_t)) {
795 pr_err("nr_stripes too large");
799 n = d->nr_stripes * sizeof(atomic_t);
800 d->stripe_sectors_dirty = n < PAGE_SIZE << 6
801 ? kzalloc(n, GFP_KERNEL)
803 if (!d->stripe_sectors_dirty)
806 n = BITS_TO_LONGS(d->nr_stripes) * sizeof(unsigned long);
807 d->full_dirty_stripes = n < PAGE_SIZE << 6
808 ? kzalloc(n, GFP_KERNEL)
810 if (!d->full_dirty_stripes)
813 minor = ida_simple_get(&bcache_minor, 0, MINORMASK + 1, GFP_KERNEL);
817 if (!(d->bio_split = bioset_create(4, offsetof(struct bbio, bio))) ||
818 bio_split_pool_init(&d->bio_split_hook) ||
819 !(d->disk = alloc_disk(1))) {
820 ida_simple_remove(&bcache_minor, minor);
824 set_capacity(d->disk, sectors);
825 snprintf(d->disk->disk_name, DISK_NAME_LEN, "bcache%i", minor);
827 d->disk->major = bcache_major;
828 d->disk->first_minor = minor;
829 d->disk->fops = &bcache_ops;
830 d->disk->private_data = d;
832 q = blk_alloc_queue(GFP_KERNEL);
836 blk_queue_make_request(q, NULL);
839 q->backing_dev_info.congested_data = d;
840 q->limits.max_hw_sectors = UINT_MAX;
841 q->limits.max_sectors = UINT_MAX;
842 q->limits.max_segment_size = UINT_MAX;
843 q->limits.max_segments = BIO_MAX_PAGES;
844 q->limits.max_discard_sectors = UINT_MAX;
845 q->limits.io_min = block_size;
846 q->limits.logical_block_size = block_size;
847 q->limits.physical_block_size = block_size;
848 set_bit(QUEUE_FLAG_NONROT, &d->disk->queue->queue_flags);
849 set_bit(QUEUE_FLAG_DISCARD, &d->disk->queue->queue_flags);
851 blk_queue_flush(q, REQ_FLUSH|REQ_FUA);
858 static void calc_cached_dev_sectors(struct cache_set *c)
860 uint64_t sectors = 0;
861 struct cached_dev *dc;
863 list_for_each_entry(dc, &c->cached_devs, list)
864 sectors += bdev_sectors(dc->bdev);
866 c->cached_dev_sectors = sectors;
869 void bch_cached_dev_run(struct cached_dev *dc)
871 struct bcache_device *d = &dc->disk;
872 char buf[SB_LABEL_SIZE + 1];
875 kasprintf(GFP_KERNEL, "CACHED_UUID=%pU", dc->sb.uuid),
880 memcpy(buf, dc->sb.label, SB_LABEL_SIZE);
881 buf[SB_LABEL_SIZE] = '\0';
882 env[2] = kasprintf(GFP_KERNEL, "CACHED_LABEL=%s", buf);
884 if (atomic_xchg(&dc->running, 1))
888 BDEV_STATE(&dc->sb) != BDEV_STATE_NONE) {
890 closure_init_stack(&cl);
892 SET_BDEV_STATE(&dc->sb, BDEV_STATE_STALE);
893 bch_write_bdev_super(dc, &cl);
898 bd_link_disk_holder(dc->bdev, dc->disk.disk);
899 /* won't show up in the uevent file, use udevadm monitor -e instead
900 * only class / kset properties are persistent */
901 kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env);
905 if (sysfs_create_link(&d->kobj, &disk_to_dev(d->disk)->kobj, "dev") ||
906 sysfs_create_link(&disk_to_dev(d->disk)->kobj, &d->kobj, "bcache"))
907 pr_debug("error creating sysfs link");
910 static void cached_dev_detach_finish(struct work_struct *w)
912 struct cached_dev *dc = container_of(w, struct cached_dev, detach);
913 char buf[BDEVNAME_SIZE];
915 closure_init_stack(&cl);
917 BUG_ON(!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags));
918 BUG_ON(atomic_read(&dc->count));
920 mutex_lock(&bch_register_lock);
922 memset(&dc->sb.set_uuid, 0, 16);
923 SET_BDEV_STATE(&dc->sb, BDEV_STATE_NONE);
925 bch_write_bdev_super(dc, &cl);
928 bcache_device_detach(&dc->disk);
929 list_move(&dc->list, &uncached_devices);
931 clear_bit(BCACHE_DEV_DETACHING, &dc->disk.flags);
933 mutex_unlock(&bch_register_lock);
935 pr_info("Caching disabled for %s", bdevname(dc->bdev, buf));
937 /* Drop ref we took in cached_dev_detach() */
938 closure_put(&dc->disk.cl);
941 void bch_cached_dev_detach(struct cached_dev *dc)
943 lockdep_assert_held(&bch_register_lock);
945 if (test_bit(BCACHE_DEV_CLOSING, &dc->disk.flags))
948 if (test_and_set_bit(BCACHE_DEV_DETACHING, &dc->disk.flags))
952 * Block the device from being closed and freed until we're finished
955 closure_get(&dc->disk.cl);
957 bch_writeback_queue(dc);
961 int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c)
963 uint32_t rtime = cpu_to_le32(get_seconds());
964 struct uuid_entry *u;
965 char buf[BDEVNAME_SIZE];
967 bdevname(dc->bdev, buf);
969 if (memcmp(dc->sb.set_uuid, c->sb.set_uuid, 16))
973 pr_err("Can't attach %s: already attached", buf);
977 if (test_bit(CACHE_SET_STOPPING, &c->flags)) {
978 pr_err("Can't attach %s: shutting down", buf);
982 if (dc->sb.block_size < c->sb.block_size) {
984 pr_err("Couldn't attach %s: block size less than set's block size",
989 u = uuid_find(c, dc->sb.uuid);
992 (BDEV_STATE(&dc->sb) == BDEV_STATE_STALE ||
993 BDEV_STATE(&dc->sb) == BDEV_STATE_NONE)) {
994 memcpy(u->uuid, invalid_uuid, 16);
995 u->invalidated = cpu_to_le32(get_seconds());
1000 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
1001 pr_err("Couldn't find uuid for %s in set", buf);
1005 u = uuid_find_empty(c);
1007 pr_err("Not caching %s, no room for UUID", buf);
1012 /* Deadlocks since we're called via sysfs...
1013 sysfs_remove_file(&dc->kobj, &sysfs_attach);
1016 if (bch_is_zero(u->uuid, 16)) {
1018 closure_init_stack(&cl);
1020 memcpy(u->uuid, dc->sb.uuid, 16);
1021 memcpy(u->label, dc->sb.label, SB_LABEL_SIZE);
1022 u->first_reg = u->last_reg = rtime;
1025 memcpy(dc->sb.set_uuid, c->sb.set_uuid, 16);
1026 SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN);
1028 bch_write_bdev_super(dc, &cl);
1031 u->last_reg = rtime;
1035 bcache_device_attach(&dc->disk, c, u - c->uuids);
1036 list_move(&dc->list, &c->cached_devs);
1037 calc_cached_dev_sectors(c);
1041 * dc->c must be set before dc->count != 0 - paired with the mb in
1044 atomic_set(&dc->count, 1);
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);
1053 bch_cached_dev_run(dc);
1054 bcache_device_link(&dc->disk, c, "bdev");
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);
1062 void bch_cached_dev_release(struct kobject *kobj)
1064 struct cached_dev *dc = container_of(kobj, struct cached_dev,
1067 module_put(THIS_MODULE);
1070 static void cached_dev_free(struct closure *cl)
1072 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
1074 cancel_delayed_work_sync(&dc->writeback_rate_update);
1075 kthread_stop(dc->writeback_thread);
1077 mutex_lock(&bch_register_lock);
1079 if (atomic_read(&dc->running))
1080 bd_unlink_disk_holder(dc->bdev, dc->disk.disk);
1081 bcache_device_free(&dc->disk);
1082 list_del(&dc->list);
1084 mutex_unlock(&bch_register_lock);
1086 if (!IS_ERR_OR_NULL(dc->bdev)) {
1087 if (dc->bdev->bd_disk)
1088 blk_sync_queue(bdev_get_queue(dc->bdev));
1090 blkdev_put(dc->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1093 wake_up(&unregister_wait);
1095 kobject_put(&dc->disk.kobj);
1098 static void cached_dev_flush(struct closure *cl)
1100 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
1101 struct bcache_device *d = &dc->disk;
1103 mutex_lock(&bch_register_lock);
1104 bcache_device_unlink(d);
1105 mutex_unlock(&bch_register_lock);
1107 bch_cache_accounting_destroy(&dc->accounting);
1108 kobject_del(&d->kobj);
1110 continue_at(cl, cached_dev_free, system_wq);
1113 static int cached_dev_init(struct cached_dev *dc, unsigned block_size)
1117 struct request_queue *q = bdev_get_queue(dc->bdev);
1119 __module_get(THIS_MODULE);
1120 INIT_LIST_HEAD(&dc->list);
1121 closure_init(&dc->disk.cl, NULL);
1122 set_closure_fn(&dc->disk.cl, cached_dev_flush, system_wq);
1123 kobject_init(&dc->disk.kobj, &bch_cached_dev_ktype);
1124 INIT_WORK(&dc->detach, cached_dev_detach_finish);
1125 sema_init(&dc->sb_write_mutex, 1);
1126 INIT_LIST_HEAD(&dc->io_lru);
1127 spin_lock_init(&dc->io_lock);
1128 bch_cache_accounting_init(&dc->accounting, &dc->disk.cl);
1130 dc->sequential_cutoff = 4 << 20;
1132 for (io = dc->io; io < dc->io + RECENT_IO; io++) {
1133 list_add(&io->lru, &dc->io_lru);
1134 hlist_add_head(&io->hash, dc->io_hash + RECENT_IO);
1137 ret = bcache_device_init(&dc->disk, block_size,
1138 dc->bdev->bd_part->nr_sects - dc->sb.data_offset);
1142 set_capacity(dc->disk.disk,
1143 dc->bdev->bd_part->nr_sects - dc->sb.data_offset);
1145 dc->disk.disk->queue->backing_dev_info.ra_pages =
1146 max(dc->disk.disk->queue->backing_dev_info.ra_pages,
1147 q->backing_dev_info.ra_pages);
1149 bch_cached_dev_request_init(dc);
1150 bch_cached_dev_writeback_init(dc);
1154 /* Cached device - bcache superblock */
1156 static void register_bdev(struct cache_sb *sb, struct page *sb_page,
1157 struct block_device *bdev,
1158 struct cached_dev *dc)
1160 char name[BDEVNAME_SIZE];
1161 const char *err = "cannot allocate memory";
1162 struct cache_set *c;
1164 memcpy(&dc->sb, sb, sizeof(struct cache_sb));
1166 dc->bdev->bd_holder = dc;
1168 bio_init(&dc->sb_bio);
1169 dc->sb_bio.bi_max_vecs = 1;
1170 dc->sb_bio.bi_io_vec = dc->sb_bio.bi_inline_vecs;
1171 dc->sb_bio.bi_io_vec[0].bv_page = sb_page;
1174 if (cached_dev_init(dc, sb->block_size << 9))
1177 err = "error creating kobject";
1178 if (kobject_add(&dc->disk.kobj, &part_to_dev(bdev->bd_part)->kobj,
1181 if (bch_cache_accounting_add_kobjs(&dc->accounting, &dc->disk.kobj))
1184 pr_info("registered backing device %s", bdevname(bdev, name));
1186 list_add(&dc->list, &uncached_devices);
1187 list_for_each_entry(c, &bch_cache_sets, list)
1188 bch_cached_dev_attach(dc, c);
1190 if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE ||
1191 BDEV_STATE(&dc->sb) == BDEV_STATE_STALE)
1192 bch_cached_dev_run(dc);
1196 pr_notice("error opening %s: %s", bdevname(bdev, name), err);
1197 bcache_device_stop(&dc->disk);
1200 /* Flash only volumes */
1202 void bch_flash_dev_release(struct kobject *kobj)
1204 struct bcache_device *d = container_of(kobj, struct bcache_device,
1209 static void flash_dev_free(struct closure *cl)
1211 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1212 bcache_device_free(d);
1213 kobject_put(&d->kobj);
1216 static void flash_dev_flush(struct closure *cl)
1218 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1220 bcache_device_unlink(d);
1221 kobject_del(&d->kobj);
1222 continue_at(cl, flash_dev_free, system_wq);
1225 static int flash_dev_run(struct cache_set *c, struct uuid_entry *u)
1227 struct bcache_device *d = kzalloc(sizeof(struct bcache_device),
1232 closure_init(&d->cl, NULL);
1233 set_closure_fn(&d->cl, flash_dev_flush, system_wq);
1235 kobject_init(&d->kobj, &bch_flash_dev_ktype);
1237 if (bcache_device_init(d, block_bytes(c), u->sectors))
1240 bcache_device_attach(d, c, u - c->uuids);
1241 bch_flash_dev_request_init(d);
1244 if (kobject_add(&d->kobj, &disk_to_dev(d->disk)->kobj, "bcache"))
1247 bcache_device_link(d, c, "volume");
1251 kobject_put(&d->kobj);
1255 static int flash_devs_run(struct cache_set *c)
1258 struct uuid_entry *u;
1261 u < c->uuids + c->nr_uuids && !ret;
1263 if (UUID_FLASH_ONLY(u))
1264 ret = flash_dev_run(c, u);
1269 int bch_flash_dev_create(struct cache_set *c, uint64_t size)
1271 struct uuid_entry *u;
1273 if (test_bit(CACHE_SET_STOPPING, &c->flags))
1276 u = uuid_find_empty(c);
1278 pr_err("Can't create volume, no room for UUID");
1282 get_random_bytes(u->uuid, 16);
1283 memset(u->label, 0, 32);
1284 u->first_reg = u->last_reg = cpu_to_le32(get_seconds());
1286 SET_UUID_FLASH_ONLY(u, 1);
1287 u->sectors = size >> 9;
1291 return flash_dev_run(c, u);
1297 bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...)
1301 if (c->on_error != ON_ERROR_PANIC &&
1302 test_bit(CACHE_SET_STOPPING, &c->flags))
1305 /* XXX: we can be called from atomic context
1306 acquire_console_sem();
1309 printk(KERN_ERR "bcache: error on %pU: ", c->sb.set_uuid);
1311 va_start(args, fmt);
1315 printk(", disabling caching\n");
1317 if (c->on_error == ON_ERROR_PANIC)
1318 panic("panic forced after error\n");
1320 bch_cache_set_unregister(c);
1324 void bch_cache_set_release(struct kobject *kobj)
1326 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
1328 module_put(THIS_MODULE);
1331 static void cache_set_free(struct closure *cl)
1333 struct cache_set *c = container_of(cl, struct cache_set, cl);
1337 if (!IS_ERR_OR_NULL(c->debug))
1338 debugfs_remove(c->debug);
1340 bch_open_buckets_free(c);
1341 bch_btree_cache_free(c);
1342 bch_journal_free(c);
1344 for_each_cache(ca, c, i)
1346 kobject_put(&ca->kobj);
1348 free_pages((unsigned long) c->uuids, ilog2(bucket_pages(c)));
1349 free_pages((unsigned long) c->sort, ilog2(bucket_pages(c)));
1352 bioset_free(c->bio_split);
1354 mempool_destroy(c->fill_iter);
1356 mempool_destroy(c->bio_meta);
1358 mempool_destroy(c->search);
1361 mutex_lock(&bch_register_lock);
1363 mutex_unlock(&bch_register_lock);
1365 pr_info("Cache set %pU unregistered", c->sb.set_uuid);
1366 wake_up(&unregister_wait);
1368 closure_debug_destroy(&c->cl);
1369 kobject_put(&c->kobj);
1372 static void cache_set_flush(struct closure *cl)
1374 struct cache_set *c = container_of(cl, struct cache_set, caching);
1379 bch_cache_accounting_destroy(&c->accounting);
1381 kobject_put(&c->internal);
1382 kobject_del(&c->kobj);
1385 kthread_stop(c->gc_thread);
1387 if (!IS_ERR_OR_NULL(c->root))
1388 list_add(&c->root->list, &c->btree_cache);
1390 /* Should skip this if we're unregistering because of an error */
1391 list_for_each_entry(b, &c->btree_cache, list)
1392 if (btree_node_dirty(b))
1393 bch_btree_node_write(b, NULL);
1395 for_each_cache(ca, c, i)
1396 if (ca->alloc_thread)
1397 kthread_stop(ca->alloc_thread);
1402 static void __cache_set_unregister(struct closure *cl)
1404 struct cache_set *c = container_of(cl, struct cache_set, caching);
1405 struct cached_dev *dc;
1408 mutex_lock(&bch_register_lock);
1410 for (i = 0; i < c->nr_uuids; i++)
1411 if (c->devices[i]) {
1412 if (!UUID_FLASH_ONLY(&c->uuids[i]) &&
1413 test_bit(CACHE_SET_UNREGISTERING, &c->flags)) {
1414 dc = container_of(c->devices[i],
1415 struct cached_dev, disk);
1416 bch_cached_dev_detach(dc);
1418 bcache_device_stop(c->devices[i]);
1422 mutex_unlock(&bch_register_lock);
1424 continue_at(cl, cache_set_flush, system_wq);
1427 void bch_cache_set_stop(struct cache_set *c)
1429 if (!test_and_set_bit(CACHE_SET_STOPPING, &c->flags))
1430 closure_queue(&c->caching);
1433 void bch_cache_set_unregister(struct cache_set *c)
1435 set_bit(CACHE_SET_UNREGISTERING, &c->flags);
1436 bch_cache_set_stop(c);
1439 #define alloc_bucket_pages(gfp, c) \
1440 ((void *) __get_free_pages(__GFP_ZERO|gfp, ilog2(bucket_pages(c))))
1442 struct cache_set *bch_cache_set_alloc(struct cache_sb *sb)
1445 struct cache_set *c = kzalloc(sizeof(struct cache_set), GFP_KERNEL);
1449 __module_get(THIS_MODULE);
1450 closure_init(&c->cl, NULL);
1451 set_closure_fn(&c->cl, cache_set_free, system_wq);
1453 closure_init(&c->caching, &c->cl);
1454 set_closure_fn(&c->caching, __cache_set_unregister, system_wq);
1456 /* Maybe create continue_at_noreturn() and use it here? */
1457 closure_set_stopped(&c->cl);
1458 closure_put(&c->cl);
1460 kobject_init(&c->kobj, &bch_cache_set_ktype);
1461 kobject_init(&c->internal, &bch_cache_set_internal_ktype);
1463 bch_cache_accounting_init(&c->accounting, &c->cl);
1465 memcpy(c->sb.set_uuid, sb->set_uuid, 16);
1466 c->sb.block_size = sb->block_size;
1467 c->sb.bucket_size = sb->bucket_size;
1468 c->sb.nr_in_set = sb->nr_in_set;
1469 c->sb.last_mount = sb->last_mount;
1470 c->bucket_bits = ilog2(sb->bucket_size);
1471 c->block_bits = ilog2(sb->block_size);
1472 c->nr_uuids = bucket_bytes(c) / sizeof(struct uuid_entry);
1474 c->btree_pages = c->sb.bucket_size / PAGE_SECTORS;
1475 if (c->btree_pages > BTREE_MAX_PAGES)
1476 c->btree_pages = max_t(int, c->btree_pages / 4,
1479 c->sort_crit_factor = int_sqrt(c->btree_pages);
1481 sema_init(&c->sb_write_mutex, 1);
1482 mutex_init(&c->bucket_lock);
1483 init_waitqueue_head(&c->try_wait);
1484 init_waitqueue_head(&c->bucket_wait);
1485 sema_init(&c->uuid_write_mutex, 1);
1486 mutex_init(&c->sort_lock);
1488 spin_lock_init(&c->sort_time.lock);
1489 spin_lock_init(&c->btree_gc_time.lock);
1490 spin_lock_init(&c->btree_split_time.lock);
1491 spin_lock_init(&c->btree_read_time.lock);
1492 spin_lock_init(&c->try_harder_time.lock);
1494 bch_moving_init_cache_set(c);
1496 INIT_LIST_HEAD(&c->list);
1497 INIT_LIST_HEAD(&c->cached_devs);
1498 INIT_LIST_HEAD(&c->btree_cache);
1499 INIT_LIST_HEAD(&c->btree_cache_freeable);
1500 INIT_LIST_HEAD(&c->btree_cache_freed);
1501 INIT_LIST_HEAD(&c->data_buckets);
1503 c->search = mempool_create_slab_pool(32, bch_search_cache);
1507 iter_size = (sb->bucket_size / sb->block_size + 1) *
1508 sizeof(struct btree_iter_set);
1510 if (!(c->devices = kzalloc(c->nr_uuids * sizeof(void *), GFP_KERNEL)) ||
1511 !(c->bio_meta = mempool_create_kmalloc_pool(2,
1512 sizeof(struct bbio) + sizeof(struct bio_vec) *
1513 bucket_pages(c))) ||
1514 !(c->fill_iter = mempool_create_kmalloc_pool(1, iter_size)) ||
1515 !(c->bio_split = bioset_create(4, offsetof(struct bbio, bio))) ||
1516 !(c->sort = alloc_bucket_pages(GFP_KERNEL, c)) ||
1517 !(c->uuids = alloc_bucket_pages(GFP_KERNEL, c)) ||
1518 bch_journal_alloc(c) ||
1519 bch_btree_cache_alloc(c) ||
1520 bch_open_buckets_alloc(c))
1523 c->congested_read_threshold_us = 2000;
1524 c->congested_write_threshold_us = 20000;
1525 c->error_limit = 8 << IO_ERROR_SHIFT;
1529 bch_cache_set_unregister(c);
1533 static void run_cache_set(struct cache_set *c)
1535 const char *err = "cannot allocate memory";
1536 struct cached_dev *dc, *t;
1541 closure_init_stack(&cl);
1543 for_each_cache(ca, c, i)
1544 c->nbuckets += ca->sb.nbuckets;
1546 if (CACHE_SYNC(&c->sb)) {
1551 err = "cannot allocate memory for journal";
1552 if (bch_journal_read(c, &journal))
1555 pr_debug("btree_journal_read() done");
1557 err = "no journal entries found";
1558 if (list_empty(&journal))
1561 j = &list_entry(journal.prev, struct journal_replay, list)->j;
1563 err = "IO error reading priorities";
1564 for_each_cache(ca, c, i)
1565 prio_read(ca, j->prio_bucket[ca->sb.nr_this_dev]);
1568 * If prio_read() fails it'll call cache_set_error and we'll
1569 * tear everything down right away, but if we perhaps checked
1570 * sooner we could avoid journal replay.
1575 err = "bad btree root";
1576 if (bch_btree_ptr_invalid(c, k))
1579 err = "error reading btree root";
1580 c->root = bch_btree_node_get(c, k, j->btree_level, true);
1581 if (IS_ERR_OR_NULL(c->root))
1584 list_del_init(&c->root->list);
1585 rw_unlock(true, c->root);
1587 err = uuid_read(c, j, &cl);
1591 err = "error in recovery";
1592 if (bch_btree_check(c))
1595 bch_journal_mark(c, &journal);
1596 bch_btree_gc_finish(c);
1597 pr_debug("btree_check() done");
1600 * bcache_journal_next() can't happen sooner, or
1601 * btree_gc_finish() will give spurious errors about last_gc >
1602 * gc_gen - this is a hack but oh well.
1604 bch_journal_next(&c->journal);
1606 err = "error starting allocator thread";
1607 for_each_cache(ca, c, i)
1608 if (bch_cache_allocator_start(ca))
1612 * First place it's safe to allocate: btree_check() and
1613 * btree_gc_finish() have to run before we have buckets to
1614 * allocate, and bch_bucket_alloc_set() might cause a journal
1615 * entry to be written so bcache_journal_next() has to be called
1618 * If the uuids were in the old format we have to rewrite them
1619 * before the next journal entry is written:
1621 if (j->version < BCACHE_JSET_VERSION_UUID)
1624 bch_journal_replay(c, &journal);
1626 pr_notice("invalidating existing data");
1628 for_each_cache(ca, c, i) {
1631 ca->sb.keys = clamp_t(int, ca->sb.nbuckets >> 7,
1632 2, SB_JOURNAL_BUCKETS);
1634 for (j = 0; j < ca->sb.keys; j++)
1635 ca->sb.d[j] = ca->sb.first_bucket + j;
1638 bch_btree_gc_finish(c);
1640 err = "error starting allocator thread";
1641 for_each_cache(ca, c, i)
1642 if (bch_cache_allocator_start(ca))
1645 mutex_lock(&c->bucket_lock);
1646 for_each_cache(ca, c, i)
1648 mutex_unlock(&c->bucket_lock);
1650 err = "cannot allocate new UUID bucket";
1651 if (__uuid_write(c))
1654 err = "cannot allocate new btree root";
1655 c->root = bch_btree_node_alloc(c, 0, true);
1656 if (IS_ERR_OR_NULL(c->root))
1659 bkey_copy_key(&c->root->key, &MAX_KEY);
1660 bch_btree_node_write(c->root, &cl);
1662 bch_btree_set_root(c->root);
1663 rw_unlock(true, c->root);
1666 * We don't want to write the first journal entry until
1667 * everything is set up - fortunately journal entries won't be
1668 * written until the SET_CACHE_SYNC() here:
1670 SET_CACHE_SYNC(&c->sb, true);
1672 bch_journal_next(&c->journal);
1673 bch_journal_meta(c, &cl);
1676 err = "error starting gc thread";
1677 if (bch_gc_thread_start(c))
1681 c->sb.last_mount = get_seconds();
1682 bcache_write_super(c);
1684 list_for_each_entry_safe(dc, t, &uncached_devices, list)
1685 bch_cached_dev_attach(dc, c);
1692 /* XXX: test this, it's broken */
1693 bch_cache_set_error(c, "%s", err);
1696 static bool can_attach_cache(struct cache *ca, struct cache_set *c)
1698 return ca->sb.block_size == c->sb.block_size &&
1699 ca->sb.bucket_size == c->sb.bucket_size &&
1700 ca->sb.nr_in_set == c->sb.nr_in_set;
1703 static const char *register_cache_set(struct cache *ca)
1706 const char *err = "cannot allocate memory";
1707 struct cache_set *c;
1709 list_for_each_entry(c, &bch_cache_sets, list)
1710 if (!memcmp(c->sb.set_uuid, ca->sb.set_uuid, 16)) {
1711 if (c->cache[ca->sb.nr_this_dev])
1712 return "duplicate cache set member";
1714 if (!can_attach_cache(ca, c))
1715 return "cache sb does not match set";
1717 if (!CACHE_SYNC(&ca->sb))
1718 SET_CACHE_SYNC(&c->sb, false);
1723 c = bch_cache_set_alloc(&ca->sb);
1727 err = "error creating kobject";
1728 if (kobject_add(&c->kobj, bcache_kobj, "%pU", c->sb.set_uuid) ||
1729 kobject_add(&c->internal, &c->kobj, "internal"))
1732 if (bch_cache_accounting_add_kobjs(&c->accounting, &c->kobj))
1735 bch_debug_init_cache_set(c);
1737 list_add(&c->list, &bch_cache_sets);
1739 sprintf(buf, "cache%i", ca->sb.nr_this_dev);
1740 if (sysfs_create_link(&ca->kobj, &c->kobj, "set") ||
1741 sysfs_create_link(&c->kobj, &ca->kobj, buf))
1744 if (ca->sb.seq > c->sb.seq) {
1745 c->sb.version = ca->sb.version;
1746 memcpy(c->sb.set_uuid, ca->sb.set_uuid, 16);
1747 c->sb.flags = ca->sb.flags;
1748 c->sb.seq = ca->sb.seq;
1749 pr_debug("set version = %llu", c->sb.version);
1753 ca->set->cache[ca->sb.nr_this_dev] = ca;
1754 c->cache_by_alloc[c->caches_loaded++] = ca;
1756 if (c->caches_loaded == c->sb.nr_in_set)
1761 bch_cache_set_unregister(c);
1767 void bch_cache_release(struct kobject *kobj)
1769 struct cache *ca = container_of(kobj, struct cache, kobj);
1773 ca->set->cache[ca->sb.nr_this_dev] = NULL;
1775 bio_split_pool_free(&ca->bio_split_hook);
1777 free_pages((unsigned long) ca->disk_buckets, ilog2(bucket_pages(ca)));
1778 kfree(ca->prio_buckets);
1781 free_heap(&ca->heap);
1782 free_fifo(&ca->unused);
1783 free_fifo(&ca->free_inc);
1785 for (i = 0; i < RESERVE_NR; i++)
1786 free_fifo(&ca->free[i]);
1788 if (ca->sb_bio.bi_inline_vecs[0].bv_page)
1789 put_page(ca->sb_bio.bi_io_vec[0].bv_page);
1791 if (!IS_ERR_OR_NULL(ca->bdev)) {
1792 blk_sync_queue(bdev_get_queue(ca->bdev));
1793 blkdev_put(ca->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1797 module_put(THIS_MODULE);
1800 static int cache_alloc(struct cache_sb *sb, struct cache *ca)
1805 __module_get(THIS_MODULE);
1806 kobject_init(&ca->kobj, &bch_cache_ktype);
1808 bio_init(&ca->journal.bio);
1809 ca->journal.bio.bi_max_vecs = 8;
1810 ca->journal.bio.bi_io_vec = ca->journal.bio.bi_inline_vecs;
1812 free = roundup_pow_of_two(ca->sb.nbuckets) >> 10;
1814 if (!init_fifo(&ca->free[RESERVE_BTREE], 8, GFP_KERNEL) ||
1815 !init_fifo(&ca->free[RESERVE_PRIO], prio_buckets(ca), GFP_KERNEL) ||
1816 !init_fifo(&ca->free[RESERVE_MOVINGGC], free, GFP_KERNEL) ||
1817 !init_fifo(&ca->free[RESERVE_NONE], free, GFP_KERNEL) ||
1818 !init_fifo(&ca->free_inc, free << 2, GFP_KERNEL) ||
1819 !init_fifo(&ca->unused, free << 2, GFP_KERNEL) ||
1820 !init_heap(&ca->heap, free << 3, GFP_KERNEL) ||
1821 !(ca->buckets = vzalloc(sizeof(struct bucket) *
1822 ca->sb.nbuckets)) ||
1823 !(ca->prio_buckets = kzalloc(sizeof(uint64_t) * prio_buckets(ca) *
1825 !(ca->disk_buckets = alloc_bucket_pages(GFP_KERNEL, ca)) ||
1826 bio_split_pool_init(&ca->bio_split_hook))
1829 ca->prio_last_buckets = ca->prio_buckets + prio_buckets(ca);
1831 for_each_bucket(b, ca)
1832 atomic_set(&b->pin, 0);
1834 if (bch_cache_allocator_init(ca))
1839 kobject_put(&ca->kobj);
1843 static void register_cache(struct cache_sb *sb, struct page *sb_page,
1844 struct block_device *bdev, struct cache *ca)
1846 char name[BDEVNAME_SIZE];
1847 const char *err = "cannot allocate memory";
1849 memcpy(&ca->sb, sb, sizeof(struct cache_sb));
1851 ca->bdev->bd_holder = ca;
1853 bio_init(&ca->sb_bio);
1854 ca->sb_bio.bi_max_vecs = 1;
1855 ca->sb_bio.bi_io_vec = ca->sb_bio.bi_inline_vecs;
1856 ca->sb_bio.bi_io_vec[0].bv_page = sb_page;
1859 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
1860 ca->discard = CACHE_DISCARD(&ca->sb);
1862 if (cache_alloc(sb, ca) != 0)
1865 err = "error creating kobject";
1866 if (kobject_add(&ca->kobj, &part_to_dev(bdev->bd_part)->kobj, "bcache"))
1869 err = register_cache_set(ca);
1873 pr_info("registered cache device %s", bdevname(bdev, name));
1876 pr_notice("error opening %s: %s", bdevname(bdev, name), err);
1877 kobject_put(&ca->kobj);
1880 /* Global interfaces/init */
1882 static ssize_t register_bcache(struct kobject *, struct kobj_attribute *,
1883 const char *, size_t);
1885 kobj_attribute_write(register, register_bcache);
1886 kobj_attribute_write(register_quiet, register_bcache);
1888 static bool bch_is_open_backing(struct block_device *bdev) {
1889 struct cache_set *c, *tc;
1890 struct cached_dev *dc, *t;
1892 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1893 list_for_each_entry_safe(dc, t, &c->cached_devs, list)
1894 if (dc->bdev == bdev)
1896 list_for_each_entry_safe(dc, t, &uncached_devices, list)
1897 if (dc->bdev == bdev)
1902 static bool bch_is_open_cache(struct block_device *bdev) {
1903 struct cache_set *c, *tc;
1907 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1908 for_each_cache(ca, c, i)
1909 if (ca->bdev == bdev)
1914 static bool bch_is_open(struct block_device *bdev) {
1915 return bch_is_open_cache(bdev) || bch_is_open_backing(bdev);
1918 static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
1919 const char *buffer, size_t size)
1922 const char *err = "cannot allocate memory";
1924 struct cache_sb *sb = NULL;
1925 struct block_device *bdev = NULL;
1926 struct page *sb_page = NULL;
1928 if (!try_module_get(THIS_MODULE))
1931 mutex_lock(&bch_register_lock);
1933 if (!(path = kstrndup(buffer, size, GFP_KERNEL)) ||
1934 !(sb = kmalloc(sizeof(struct cache_sb), GFP_KERNEL)))
1937 err = "failed to open device";
1938 bdev = blkdev_get_by_path(strim(path),
1939 FMODE_READ|FMODE_WRITE|FMODE_EXCL,
1942 if (bdev == ERR_PTR(-EBUSY)) {
1943 bdev = lookup_bdev(strim(path));
1944 if (!IS_ERR(bdev) && bch_is_open(bdev))
1945 err = "device already registered";
1947 err = "device busy";
1952 err = "failed to set blocksize";
1953 if (set_blocksize(bdev, 4096))
1956 err = read_super(sb, bdev, &sb_page);
1960 if (SB_IS_BDEV(sb)) {
1961 struct cached_dev *dc = kzalloc(sizeof(*dc), GFP_KERNEL);
1965 register_bdev(sb, sb_page, bdev, dc);
1967 struct cache *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
1971 register_cache(sb, sb_page, bdev, ca);
1978 mutex_unlock(&bch_register_lock);
1979 module_put(THIS_MODULE);
1983 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1985 if (attr != &ksysfs_register_quiet)
1986 pr_info("error opening %s: %s", path, err);
1991 static int bcache_reboot(struct notifier_block *n, unsigned long code, void *x)
1993 if (code == SYS_DOWN ||
1995 code == SYS_POWER_OFF) {
1997 unsigned long start = jiffies;
1998 bool stopped = false;
2000 struct cache_set *c, *tc;
2001 struct cached_dev *dc, *tdc;
2003 mutex_lock(&bch_register_lock);
2005 if (list_empty(&bch_cache_sets) &&
2006 list_empty(&uncached_devices))
2009 pr_info("Stopping all devices:");
2011 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
2012 bch_cache_set_stop(c);
2014 list_for_each_entry_safe(dc, tdc, &uncached_devices, list)
2015 bcache_device_stop(&dc->disk);
2017 /* What's a condition variable? */
2019 long timeout = start + 2 * HZ - jiffies;
2021 stopped = list_empty(&bch_cache_sets) &&
2022 list_empty(&uncached_devices);
2024 if (timeout < 0 || stopped)
2027 prepare_to_wait(&unregister_wait, &wait,
2028 TASK_UNINTERRUPTIBLE);
2030 mutex_unlock(&bch_register_lock);
2031 schedule_timeout(timeout);
2032 mutex_lock(&bch_register_lock);
2035 finish_wait(&unregister_wait, &wait);
2038 pr_info("All devices stopped");
2040 pr_notice("Timeout waiting for devices to be closed");
2042 mutex_unlock(&bch_register_lock);
2048 static struct notifier_block reboot = {
2049 .notifier_call = bcache_reboot,
2050 .priority = INT_MAX, /* before any real devices */
2053 static void bcache_exit(void)
2059 kobject_put(bcache_kobj);
2061 destroy_workqueue(bcache_wq);
2062 unregister_blkdev(bcache_major, "bcache");
2063 unregister_reboot_notifier(&reboot);
2066 static int __init bcache_init(void)
2068 static const struct attribute *files[] = {
2069 &ksysfs_register.attr,
2070 &ksysfs_register_quiet.attr,
2074 mutex_init(&bch_register_lock);
2075 init_waitqueue_head(&unregister_wait);
2076 register_reboot_notifier(&reboot);
2077 closure_debug_init();
2079 bcache_major = register_blkdev(0, "bcache");
2080 if (bcache_major < 0)
2081 return bcache_major;
2083 if (!(bcache_wq = create_workqueue("bcache")) ||
2084 !(bcache_kobj = kobject_create_and_add("bcache", fs_kobj)) ||
2085 sysfs_create_files(bcache_kobj, files) ||
2087 bch_request_init() ||
2088 bch_debug_init(bcache_kobj))
2097 module_exit(bcache_exit);
2098 module_init(bcache_init);