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.
14 #include <linux/buffer_head.h>
15 #include <linux/debugfs.h>
16 #include <linux/genhd.h>
17 #include <linux/module.h>
18 #include <linux/random.h>
19 #include <linux/reboot.h>
20 #include <linux/sysfs.h>
22 MODULE_LICENSE("GPL");
23 MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
25 static const char bcache_magic[] = {
26 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca,
27 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81
30 static const char invalid_uuid[] = {
31 0xa0, 0x3e, 0xf8, 0xed, 0x3e, 0xe1, 0xb8, 0x78,
32 0xc8, 0x50, 0xfc, 0x5e, 0xcb, 0x16, 0xcd, 0x99
35 /* Default is -1; we skip past it for struct cached_dev's cache mode */
36 const char * const bch_cache_modes[] = {
45 struct uuid_entry_v0 {
54 static struct kobject *bcache_kobj;
55 struct mutex bch_register_lock;
56 LIST_HEAD(bch_cache_sets);
57 static LIST_HEAD(uncached_devices);
59 static int bcache_major, bcache_minor;
60 static wait_queue_head_t unregister_wait;
61 struct workqueue_struct *bcache_wq;
63 #define BTREE_MAX_PAGES (256 * 1024 / PAGE_SIZE)
65 static void bio_split_pool_free(struct bio_split_pool *p)
67 if (p->bio_split_hook)
68 mempool_destroy(p->bio_split_hook);
71 bioset_free(p->bio_split);
74 static int bio_split_pool_init(struct bio_split_pool *p)
76 p->bio_split = bioset_create(4, 0);
80 p->bio_split_hook = mempool_create_kmalloc_pool(4,
81 sizeof(struct bio_split_hook));
82 if (!p->bio_split_hook)
90 static const char *read_super(struct cache_sb *sb, struct block_device *bdev,
95 struct buffer_head *bh = __bread(bdev, 1, SB_SIZE);
101 s = (struct cache_sb *) bh->b_data;
103 sb->offset = le64_to_cpu(s->offset);
104 sb->version = le64_to_cpu(s->version);
106 memcpy(sb->magic, s->magic, 16);
107 memcpy(sb->uuid, s->uuid, 16);
108 memcpy(sb->set_uuid, s->set_uuid, 16);
109 memcpy(sb->label, s->label, SB_LABEL_SIZE);
111 sb->flags = le64_to_cpu(s->flags);
112 sb->seq = le64_to_cpu(s->seq);
113 sb->last_mount = le32_to_cpu(s->last_mount);
114 sb->first_bucket = le16_to_cpu(s->first_bucket);
115 sb->keys = le16_to_cpu(s->keys);
117 for (i = 0; i < SB_JOURNAL_BUCKETS; i++)
118 sb->d[i] = le64_to_cpu(s->d[i]);
120 pr_debug("read sb version %llu, flags %llu, seq %llu, journal size %u",
121 sb->version, sb->flags, sb->seq, sb->keys);
123 err = "Not a bcache superblock";
124 if (sb->offset != SB_SECTOR)
127 if (memcmp(sb->magic, bcache_magic, 16))
130 err = "Too many journal buckets";
131 if (sb->keys > SB_JOURNAL_BUCKETS)
134 err = "Bad checksum";
135 if (s->csum != csum_set(s))
139 if (bch_is_zero(sb->uuid, 16))
142 sb->block_size = le16_to_cpu(s->block_size);
144 err = "Superblock block size smaller than device block size";
145 if (sb->block_size << 9 < bdev_logical_block_size(bdev))
148 switch (sb->version) {
149 case BCACHE_SB_VERSION_BDEV:
150 sb->data_offset = BDEV_DATA_START_DEFAULT;
152 case BCACHE_SB_VERSION_BDEV_WITH_OFFSET:
153 sb->data_offset = le64_to_cpu(s->data_offset);
155 err = "Bad data offset";
156 if (sb->data_offset < BDEV_DATA_START_DEFAULT)
160 case BCACHE_SB_VERSION_CDEV:
161 case BCACHE_SB_VERSION_CDEV_WITH_UUID:
162 sb->nbuckets = le64_to_cpu(s->nbuckets);
163 sb->block_size = le16_to_cpu(s->block_size);
164 sb->bucket_size = le16_to_cpu(s->bucket_size);
166 sb->nr_in_set = le16_to_cpu(s->nr_in_set);
167 sb->nr_this_dev = le16_to_cpu(s->nr_this_dev);
169 err = "Too many buckets";
170 if (sb->nbuckets > LONG_MAX)
173 err = "Not enough buckets";
174 if (sb->nbuckets < 1 << 7)
177 err = "Bad block/bucket size";
178 if (!is_power_of_2(sb->block_size) ||
179 sb->block_size > PAGE_SECTORS ||
180 !is_power_of_2(sb->bucket_size) ||
181 sb->bucket_size < PAGE_SECTORS)
184 err = "Invalid superblock: device too small";
185 if (get_capacity(bdev->bd_disk) < sb->bucket_size * sb->nbuckets)
189 if (bch_is_zero(sb->set_uuid, 16))
192 err = "Bad cache device number in set";
193 if (!sb->nr_in_set ||
194 sb->nr_in_set <= sb->nr_this_dev ||
195 sb->nr_in_set > MAX_CACHES_PER_SET)
198 err = "Journal buckets not sequential";
199 for (i = 0; i < sb->keys; i++)
200 if (sb->d[i] != sb->first_bucket + i)
203 err = "Too many journal buckets";
204 if (sb->first_bucket + sb->keys > sb->nbuckets)
207 err = "Invalid superblock: first bucket comes before end of super";
208 if (sb->first_bucket * sb->bucket_size < 16)
213 err = "Unsupported superblock version";
217 sb->last_mount = get_seconds();
220 get_page(bh->b_page);
227 static void write_bdev_super_endio(struct bio *bio, int error)
229 struct cached_dev *dc = bio->bi_private;
230 /* XXX: error checking */
232 closure_put(&dc->sb_write.cl);
235 static void __write_super(struct cache_sb *sb, struct bio *bio)
237 struct cache_sb *out = page_address(bio->bi_io_vec[0].bv_page);
240 bio->bi_sector = SB_SECTOR;
241 bio->bi_rw = REQ_SYNC|REQ_META;
242 bio->bi_size = SB_SIZE;
243 bch_bio_map(bio, NULL);
245 out->offset = cpu_to_le64(sb->offset);
246 out->version = cpu_to_le64(sb->version);
248 memcpy(out->uuid, sb->uuid, 16);
249 memcpy(out->set_uuid, sb->set_uuid, 16);
250 memcpy(out->label, sb->label, SB_LABEL_SIZE);
252 out->flags = cpu_to_le64(sb->flags);
253 out->seq = cpu_to_le64(sb->seq);
255 out->last_mount = cpu_to_le32(sb->last_mount);
256 out->first_bucket = cpu_to_le16(sb->first_bucket);
257 out->keys = cpu_to_le16(sb->keys);
259 for (i = 0; i < sb->keys; i++)
260 out->d[i] = cpu_to_le64(sb->d[i]);
262 out->csum = csum_set(out);
264 pr_debug("ver %llu, flags %llu, seq %llu",
265 sb->version, sb->flags, sb->seq);
267 submit_bio(REQ_WRITE, bio);
270 void bch_write_bdev_super(struct cached_dev *dc, struct closure *parent)
272 struct closure *cl = &dc->sb_write.cl;
273 struct bio *bio = &dc->sb_bio;
275 closure_lock(&dc->sb_write, parent);
278 bio->bi_bdev = dc->bdev;
279 bio->bi_end_io = write_bdev_super_endio;
280 bio->bi_private = dc;
283 __write_super(&dc->sb, bio);
288 static void write_super_endio(struct bio *bio, int error)
290 struct cache *ca = bio->bi_private;
292 bch_count_io_errors(ca, error, "writing superblock");
293 closure_put(&ca->set->sb_write.cl);
296 void bcache_write_super(struct cache_set *c)
298 struct closure *cl = &c->sb_write.cl;
302 closure_lock(&c->sb_write, &c->cl);
306 for_each_cache(ca, c, i) {
307 struct bio *bio = &ca->sb_bio;
309 ca->sb.version = BCACHE_SB_VERSION_CDEV_WITH_UUID;
310 ca->sb.seq = c->sb.seq;
311 ca->sb.last_mount = c->sb.last_mount;
313 SET_CACHE_SYNC(&ca->sb, CACHE_SYNC(&c->sb));
316 bio->bi_bdev = ca->bdev;
317 bio->bi_end_io = write_super_endio;
318 bio->bi_private = ca;
321 __write_super(&ca->sb, bio);
329 static void uuid_endio(struct bio *bio, int error)
331 struct closure *cl = bio->bi_private;
332 struct cache_set *c = container_of(cl, struct cache_set, uuid_write.cl);
334 cache_set_err_on(error, c, "accessing uuids");
335 bch_bbio_free(bio, c);
339 static void uuid_io(struct cache_set *c, unsigned long rw,
340 struct bkey *k, struct closure *parent)
342 struct closure *cl = &c->uuid_write.cl;
343 struct uuid_entry *u;
347 closure_lock(&c->uuid_write, parent);
349 for (i = 0; i < KEY_PTRS(k); i++) {
350 struct bio *bio = bch_bbio_alloc(c);
352 bio->bi_rw = REQ_SYNC|REQ_META|rw;
353 bio->bi_size = KEY_SIZE(k) << 9;
355 bio->bi_end_io = uuid_endio;
356 bio->bi_private = cl;
357 bch_bio_map(bio, c->uuids);
359 bch_submit_bbio(bio, c, k, i);
365 pr_debug("%s UUIDs at %s", rw & REQ_WRITE ? "wrote" : "read",
366 pkey(&c->uuid_bucket));
368 for (u = c->uuids; u < c->uuids + c->nr_uuids; u++)
369 if (!bch_is_zero(u->uuid, 16))
370 pr_debug("Slot %zi: %pU: %s: 1st: %u last: %u inv: %u",
371 u - c->uuids, u->uuid, u->label,
372 u->first_reg, u->last_reg, u->invalidated);
377 static char *uuid_read(struct cache_set *c, struct jset *j, struct closure *cl)
379 struct bkey *k = &j->uuid_bucket;
381 if (__bch_ptr_invalid(c, 1, k))
382 return "bad uuid pointer";
384 bkey_copy(&c->uuid_bucket, k);
385 uuid_io(c, READ_SYNC, k, cl);
387 if (j->version < BCACHE_JSET_VERSION_UUIDv1) {
388 struct uuid_entry_v0 *u0 = (void *) c->uuids;
389 struct uuid_entry *u1 = (void *) c->uuids;
395 * Since the new uuid entry is bigger than the old, we have to
396 * convert starting at the highest memory address and work down
397 * in order to do it in place
400 for (i = c->nr_uuids - 1;
403 memcpy(u1[i].uuid, u0[i].uuid, 16);
404 memcpy(u1[i].label, u0[i].label, 32);
406 u1[i].first_reg = u0[i].first_reg;
407 u1[i].last_reg = u0[i].last_reg;
408 u1[i].invalidated = u0[i].invalidated;
418 static int __uuid_write(struct cache_set *c)
422 closure_init_stack(&cl);
424 lockdep_assert_held(&bch_register_lock);
426 if (bch_bucket_alloc_set(c, WATERMARK_METADATA, &k.key, 1, &cl))
429 SET_KEY_SIZE(&k.key, c->sb.bucket_size);
430 uuid_io(c, REQ_WRITE, &k.key, &cl);
433 bkey_copy(&c->uuid_bucket, &k.key);
434 __bkey_put(c, &k.key);
438 int bch_uuid_write(struct cache_set *c)
440 int ret = __uuid_write(c);
443 bch_journal_meta(c, NULL);
448 static struct uuid_entry *uuid_find(struct cache_set *c, const char *uuid)
450 struct uuid_entry *u;
453 u < c->uuids + c->nr_uuids; u++)
454 if (!memcmp(u->uuid, uuid, 16))
460 static struct uuid_entry *uuid_find_empty(struct cache_set *c)
462 static const char zero_uuid[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
463 return uuid_find(c, zero_uuid);
467 * Bucket priorities/gens:
469 * For each bucket, we store on disk its
473 * See alloc.c for an explanation of the gen. The priority is used to implement
474 * lru (and in the future other) cache replacement policies; for most purposes
475 * it's just an opaque integer.
477 * The gens and the priorities don't have a whole lot to do with each other, and
478 * it's actually the gens that must be written out at specific times - it's no
479 * big deal if the priorities don't get written, if we lose them we just reuse
480 * buckets in suboptimal order.
482 * On disk they're stored in a packed array, and in as many buckets are required
483 * to fit them all. The buckets we use to store them form a list; the journal
484 * header points to the first bucket, the first bucket points to the second
487 * This code is used by the allocation code; periodically (whenever it runs out
488 * of buckets to allocate from) the allocation code will invalidate some
489 * buckets, but it can't use those buckets until their new gens are safely on
493 static void prio_endio(struct bio *bio, int error)
495 struct cache *ca = bio->bi_private;
497 cache_set_err_on(error, ca->set, "accessing priorities");
498 bch_bbio_free(bio, ca->set);
499 closure_put(&ca->prio);
502 static void prio_io(struct cache *ca, uint64_t bucket, unsigned long rw)
504 struct closure *cl = &ca->prio;
505 struct bio *bio = bch_bbio_alloc(ca->set);
507 closure_init_stack(cl);
509 bio->bi_sector = bucket * ca->sb.bucket_size;
510 bio->bi_bdev = ca->bdev;
511 bio->bi_rw = REQ_SYNC|REQ_META|rw;
512 bio->bi_size = bucket_bytes(ca);
514 bio->bi_end_io = prio_endio;
515 bio->bi_private = ca;
516 bch_bio_map(bio, ca->disk_buckets);
518 closure_bio_submit(bio, &ca->prio, ca);
522 #define buckets_free(c) "free %zu, free_inc %zu, unused %zu", \
523 fifo_used(&c->free), fifo_used(&c->free_inc), fifo_used(&c->unused)
525 void bch_prio_write(struct cache *ca)
531 closure_init_stack(&cl);
533 lockdep_assert_held(&ca->set->bucket_lock);
535 for (b = ca->buckets;
536 b < ca->buckets + ca->sb.nbuckets; b++)
537 b->disk_gen = b->gen;
539 ca->disk_buckets->seq++;
541 atomic_long_add(ca->sb.bucket_size * prio_buckets(ca),
542 &ca->meta_sectors_written);
544 pr_debug("free %zu, free_inc %zu, unused %zu", fifo_used(&ca->free),
545 fifo_used(&ca->free_inc), fifo_used(&ca->unused));
546 blktrace_msg(ca, "Starting priorities: " buckets_free(ca));
548 for (i = prio_buckets(ca) - 1; i >= 0; --i) {
550 struct prio_set *p = ca->disk_buckets;
551 struct bucket_disk *d = p->data;
552 struct bucket_disk *end = d + prios_per_bucket(ca);
554 for (b = ca->buckets + i * prios_per_bucket(ca);
555 b < ca->buckets + ca->sb.nbuckets && d < end;
557 d->prio = cpu_to_le16(b->prio);
561 p->next_bucket = ca->prio_buckets[i + 1];
562 p->magic = pset_magic(ca);
563 p->csum = bch_crc64(&p->magic, bucket_bytes(ca) - 8);
565 bucket = bch_bucket_alloc(ca, WATERMARK_PRIO, &cl);
566 BUG_ON(bucket == -1);
568 mutex_unlock(&ca->set->bucket_lock);
569 prio_io(ca, bucket, REQ_WRITE);
570 mutex_lock(&ca->set->bucket_lock);
572 ca->prio_buckets[i] = bucket;
573 atomic_dec_bug(&ca->buckets[bucket].pin);
576 mutex_unlock(&ca->set->bucket_lock);
578 bch_journal_meta(ca->set, &cl);
581 mutex_lock(&ca->set->bucket_lock);
583 ca->need_save_prio = 0;
586 * Don't want the old priorities to get garbage collected until after we
587 * finish writing the new ones, and they're journalled
589 for (i = 0; i < prio_buckets(ca); i++)
590 ca->prio_last_buckets[i] = ca->prio_buckets[i];
593 static void prio_read(struct cache *ca, uint64_t bucket)
595 struct prio_set *p = ca->disk_buckets;
596 struct bucket_disk *d = p->data + prios_per_bucket(ca), *end = d;
598 unsigned bucket_nr = 0;
600 for (b = ca->buckets;
601 b < ca->buckets + ca->sb.nbuckets;
604 ca->prio_buckets[bucket_nr] = bucket;
605 ca->prio_last_buckets[bucket_nr] = bucket;
608 prio_io(ca, bucket, READ_SYNC);
610 if (p->csum != bch_crc64(&p->magic, bucket_bytes(ca) - 8))
611 pr_warn("bad csum reading priorities");
613 if (p->magic != pset_magic(ca))
614 pr_warn("bad magic reading priorities");
616 bucket = p->next_bucket;
620 b->prio = le16_to_cpu(d->prio);
621 b->gen = b->disk_gen = b->last_gc = b->gc_gen = d->gen;
627 static int open_dev(struct block_device *b, fmode_t mode)
629 struct bcache_device *d = b->bd_disk->private_data;
630 if (atomic_read(&d->closing))
637 static void release_dev(struct gendisk *b, fmode_t mode)
639 struct bcache_device *d = b->private_data;
643 static int ioctl_dev(struct block_device *b, fmode_t mode,
644 unsigned int cmd, unsigned long arg)
646 struct bcache_device *d = b->bd_disk->private_data;
647 return d->ioctl(d, mode, cmd, arg);
650 static const struct block_device_operations bcache_ops = {
652 .release = release_dev,
654 .owner = THIS_MODULE,
657 void bcache_device_stop(struct bcache_device *d)
659 if (!atomic_xchg(&d->closing, 1))
660 closure_queue(&d->cl);
663 static void bcache_device_unlink(struct bcache_device *d)
668 sysfs_remove_link(&d->c->kobj, d->name);
669 sysfs_remove_link(&d->kobj, "cache");
671 for_each_cache(ca, d->c, i)
672 bd_unlink_disk_holder(ca->bdev, d->disk);
675 static void bcache_device_link(struct bcache_device *d, struct cache_set *c,
681 for_each_cache(ca, d->c, i)
682 bd_link_disk_holder(ca->bdev, d->disk);
684 snprintf(d->name, BCACHEDEVNAME_SIZE,
685 "%s%u", name, d->id);
687 WARN(sysfs_create_link(&d->kobj, &c->kobj, "cache") ||
688 sysfs_create_link(&c->kobj, &d->kobj, d->name),
689 "Couldn't create device <-> cache set symlinks");
692 static void bcache_device_detach(struct bcache_device *d)
694 lockdep_assert_held(&bch_register_lock);
696 if (atomic_read(&d->detaching)) {
697 struct uuid_entry *u = d->c->uuids + d->id;
699 SET_UUID_FLASH_ONLY(u, 0);
700 memcpy(u->uuid, invalid_uuid, 16);
701 u->invalidated = cpu_to_le32(get_seconds());
702 bch_uuid_write(d->c);
704 atomic_set(&d->detaching, 0);
708 bcache_device_unlink(d);
710 d->c->devices[d->id] = NULL;
711 closure_put(&d->c->caching);
715 static void bcache_device_attach(struct bcache_device *d, struct cache_set *c,
718 BUG_ON(test_bit(CACHE_SET_STOPPING, &c->flags));
724 closure_get(&c->caching);
727 static void bcache_device_free(struct bcache_device *d)
729 lockdep_assert_held(&bch_register_lock);
731 pr_info("%s stopped", d->disk->disk_name);
734 bcache_device_detach(d);
735 if (d->disk && d->disk->flags & GENHD_FL_UP)
736 del_gendisk(d->disk);
737 if (d->disk && d->disk->queue)
738 blk_cleanup_queue(d->disk->queue);
742 bio_split_pool_free(&d->bio_split_hook);
743 if (d->unaligned_bvec)
744 mempool_destroy(d->unaligned_bvec);
746 bioset_free(d->bio_split);
748 closure_debug_destroy(&d->cl);
751 static int bcache_device_init(struct bcache_device *d, unsigned block_size)
753 struct request_queue *q;
755 if (!(d->bio_split = bioset_create(4, offsetof(struct bbio, bio))) ||
756 !(d->unaligned_bvec = mempool_create_kmalloc_pool(1,
757 sizeof(struct bio_vec) * BIO_MAX_PAGES)) ||
758 bio_split_pool_init(&d->bio_split_hook) ||
759 !(d->disk = alloc_disk(1)) ||
760 !(q = blk_alloc_queue(GFP_KERNEL)))
763 snprintf(d->disk->disk_name, DISK_NAME_LEN, "bcache%i", bcache_minor);
765 d->disk->major = bcache_major;
766 d->disk->first_minor = bcache_minor++;
767 d->disk->fops = &bcache_ops;
768 d->disk->private_data = d;
770 blk_queue_make_request(q, NULL);
773 q->backing_dev_info.congested_data = d;
774 q->limits.max_hw_sectors = UINT_MAX;
775 q->limits.max_sectors = UINT_MAX;
776 q->limits.max_segment_size = UINT_MAX;
777 q->limits.max_segments = BIO_MAX_PAGES;
778 q->limits.max_discard_sectors = UINT_MAX;
779 q->limits.io_min = block_size;
780 q->limits.logical_block_size = block_size;
781 q->limits.physical_block_size = block_size;
782 set_bit(QUEUE_FLAG_NONROT, &d->disk->queue->queue_flags);
783 set_bit(QUEUE_FLAG_DISCARD, &d->disk->queue->queue_flags);
785 blk_queue_flush(q, REQ_FLUSH|REQ_FUA);
792 static void calc_cached_dev_sectors(struct cache_set *c)
794 uint64_t sectors = 0;
795 struct cached_dev *dc;
797 list_for_each_entry(dc, &c->cached_devs, list)
798 sectors += bdev_sectors(dc->bdev);
800 c->cached_dev_sectors = sectors;
803 void bch_cached_dev_run(struct cached_dev *dc)
805 struct bcache_device *d = &dc->disk;
807 if (atomic_xchg(&dc->running, 1))
811 BDEV_STATE(&dc->sb) != BDEV_STATE_NONE) {
813 closure_init_stack(&cl);
815 SET_BDEV_STATE(&dc->sb, BDEV_STATE_STALE);
816 bch_write_bdev_super(dc, &cl);
821 bd_link_disk_holder(dc->bdev, dc->disk.disk);
823 char *env[] = { "SYMLINK=label" , NULL };
824 kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env);
826 if (sysfs_create_link(&d->kobj, &disk_to_dev(d->disk)->kobj, "dev") ||
827 sysfs_create_link(&disk_to_dev(d->disk)->kobj, &d->kobj, "bcache"))
828 pr_debug("error creating sysfs link");
831 static void cached_dev_detach_finish(struct work_struct *w)
833 struct cached_dev *dc = container_of(w, struct cached_dev, detach);
834 char buf[BDEVNAME_SIZE];
836 closure_init_stack(&cl);
838 BUG_ON(!atomic_read(&dc->disk.detaching));
839 BUG_ON(atomic_read(&dc->count));
841 mutex_lock(&bch_register_lock);
843 memset(&dc->sb.set_uuid, 0, 16);
844 SET_BDEV_STATE(&dc->sb, BDEV_STATE_NONE);
846 bch_write_bdev_super(dc, &cl);
849 bcache_device_detach(&dc->disk);
850 list_move(&dc->list, &uncached_devices);
852 mutex_unlock(&bch_register_lock);
854 pr_info("Caching disabled for %s", bdevname(dc->bdev, buf));
856 /* Drop ref we took in cached_dev_detach() */
857 closure_put(&dc->disk.cl);
860 void bch_cached_dev_detach(struct cached_dev *dc)
862 lockdep_assert_held(&bch_register_lock);
864 if (atomic_read(&dc->disk.closing))
867 if (atomic_xchg(&dc->disk.detaching, 1))
871 * Block the device from being closed and freed until we're finished
874 closure_get(&dc->disk.cl);
876 bch_writeback_queue(dc);
880 int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c)
882 uint32_t rtime = cpu_to_le32(get_seconds());
883 struct uuid_entry *u;
884 char buf[BDEVNAME_SIZE];
886 bdevname(dc->bdev, buf);
888 if (memcmp(dc->sb.set_uuid, c->sb.set_uuid, 16))
892 pr_err("Can't attach %s: already attached", buf);
896 if (test_bit(CACHE_SET_STOPPING, &c->flags)) {
897 pr_err("Can't attach %s: shutting down", buf);
901 if (dc->sb.block_size < c->sb.block_size) {
903 pr_err("Couldn't attach %s: block size less than set's block size",
908 u = uuid_find(c, dc->sb.uuid);
911 (BDEV_STATE(&dc->sb) == BDEV_STATE_STALE ||
912 BDEV_STATE(&dc->sb) == BDEV_STATE_NONE)) {
913 memcpy(u->uuid, invalid_uuid, 16);
914 u->invalidated = cpu_to_le32(get_seconds());
919 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
920 pr_err("Couldn't find uuid for %s in set", buf);
924 u = uuid_find_empty(c);
926 pr_err("Not caching %s, no room for UUID", buf);
931 /* Deadlocks since we're called via sysfs...
932 sysfs_remove_file(&dc->kobj, &sysfs_attach);
935 if (bch_is_zero(u->uuid, 16)) {
937 closure_init_stack(&cl);
939 memcpy(u->uuid, dc->sb.uuid, 16);
940 memcpy(u->label, dc->sb.label, SB_LABEL_SIZE);
941 u->first_reg = u->last_reg = rtime;
944 memcpy(dc->sb.set_uuid, c->sb.set_uuid, 16);
945 SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN);
947 bch_write_bdev_super(dc, &cl);
954 bcache_device_attach(&dc->disk, c, u - c->uuids);
955 list_move(&dc->list, &c->cached_devs);
956 calc_cached_dev_sectors(c);
960 * dc->c must be set before dc->count != 0 - paired with the mb in
963 atomic_set(&dc->count, 1);
965 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
966 atomic_set(&dc->has_dirty, 1);
967 atomic_inc(&dc->count);
968 bch_writeback_queue(dc);
971 bch_cached_dev_run(dc);
972 bcache_device_link(&dc->disk, c, "bdev");
974 pr_info("Caching %s as %s on set %pU",
975 bdevname(dc->bdev, buf), dc->disk.disk->disk_name,
976 dc->disk.c->sb.set_uuid);
980 void bch_cached_dev_release(struct kobject *kobj)
982 struct cached_dev *dc = container_of(kobj, struct cached_dev,
985 module_put(THIS_MODULE);
988 static void cached_dev_free(struct closure *cl)
990 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
992 cancel_delayed_work_sync(&dc->writeback_rate_update);
994 mutex_lock(&bch_register_lock);
996 if (atomic_read(&dc->running))
997 bd_unlink_disk_holder(dc->bdev, dc->disk.disk);
998 bcache_device_free(&dc->disk);
1001 mutex_unlock(&bch_register_lock);
1003 if (!IS_ERR_OR_NULL(dc->bdev)) {
1004 if (dc->bdev->bd_disk)
1005 blk_sync_queue(bdev_get_queue(dc->bdev));
1007 blkdev_put(dc->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1010 wake_up(&unregister_wait);
1012 kobject_put(&dc->disk.kobj);
1015 static void cached_dev_flush(struct closure *cl)
1017 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
1018 struct bcache_device *d = &dc->disk;
1020 mutex_lock(&bch_register_lock);
1024 bcache_device_unlink(d);
1026 mutex_unlock(&bch_register_lock);
1028 bch_cache_accounting_destroy(&dc->accounting);
1029 kobject_del(&d->kobj);
1031 continue_at(cl, cached_dev_free, system_wq);
1034 static int cached_dev_init(struct cached_dev *dc, unsigned block_size)
1038 struct request_queue *q = bdev_get_queue(dc->bdev);
1040 __module_get(THIS_MODULE);
1041 INIT_LIST_HEAD(&dc->list);
1042 closure_init(&dc->disk.cl, NULL);
1043 set_closure_fn(&dc->disk.cl, cached_dev_flush, system_wq);
1044 kobject_init(&dc->disk.kobj, &bch_cached_dev_ktype);
1045 INIT_WORK(&dc->detach, cached_dev_detach_finish);
1046 closure_init_unlocked(&dc->sb_write);
1047 INIT_LIST_HEAD(&dc->io_lru);
1048 spin_lock_init(&dc->io_lock);
1049 bch_cache_accounting_init(&dc->accounting, &dc->disk.cl);
1051 dc->sequential_merge = true;
1052 dc->sequential_cutoff = 4 << 20;
1054 for (io = dc->io; io < dc->io + RECENT_IO; io++) {
1055 list_add(&io->lru, &dc->io_lru);
1056 hlist_add_head(&io->hash, dc->io_hash + RECENT_IO);
1059 ret = bcache_device_init(&dc->disk, block_size);
1063 set_capacity(dc->disk.disk,
1064 dc->bdev->bd_part->nr_sects - dc->sb.data_offset);
1066 dc->disk.disk->queue->backing_dev_info.ra_pages =
1067 max(dc->disk.disk->queue->backing_dev_info.ra_pages,
1068 q->backing_dev_info.ra_pages);
1070 bch_cached_dev_request_init(dc);
1071 bch_cached_dev_writeback_init(dc);
1075 /* Cached device - bcache superblock */
1077 static void register_bdev(struct cache_sb *sb, struct page *sb_page,
1078 struct block_device *bdev,
1079 struct cached_dev *dc)
1081 char name[BDEVNAME_SIZE];
1082 const char *err = "cannot allocate memory";
1083 struct cache_set *c;
1085 memcpy(&dc->sb, sb, sizeof(struct cache_sb));
1087 dc->bdev->bd_holder = dc;
1089 bio_init(&dc->sb_bio);
1090 dc->sb_bio.bi_max_vecs = 1;
1091 dc->sb_bio.bi_io_vec = dc->sb_bio.bi_inline_vecs;
1092 dc->sb_bio.bi_io_vec[0].bv_page = sb_page;
1095 if (cached_dev_init(dc, sb->block_size << 9))
1098 err = "error creating kobject";
1099 if (kobject_add(&dc->disk.kobj, &part_to_dev(bdev->bd_part)->kobj,
1102 if (bch_cache_accounting_add_kobjs(&dc->accounting, &dc->disk.kobj))
1105 pr_info("registered backing device %s", bdevname(bdev, name));
1107 list_add(&dc->list, &uncached_devices);
1108 list_for_each_entry(c, &bch_cache_sets, list)
1109 bch_cached_dev_attach(dc, c);
1111 if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE ||
1112 BDEV_STATE(&dc->sb) == BDEV_STATE_STALE)
1113 bch_cached_dev_run(dc);
1117 pr_notice("error opening %s: %s", bdevname(bdev, name), err);
1118 bcache_device_stop(&dc->disk);
1121 /* Flash only volumes */
1123 void bch_flash_dev_release(struct kobject *kobj)
1125 struct bcache_device *d = container_of(kobj, struct bcache_device,
1130 static void flash_dev_free(struct closure *cl)
1132 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1133 bcache_device_free(d);
1134 kobject_put(&d->kobj);
1137 static void flash_dev_flush(struct closure *cl)
1139 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1141 bcache_device_unlink(d);
1142 kobject_del(&d->kobj);
1143 continue_at(cl, flash_dev_free, system_wq);
1146 static int flash_dev_run(struct cache_set *c, struct uuid_entry *u)
1148 struct bcache_device *d = kzalloc(sizeof(struct bcache_device),
1153 closure_init(&d->cl, NULL);
1154 set_closure_fn(&d->cl, flash_dev_flush, system_wq);
1156 kobject_init(&d->kobj, &bch_flash_dev_ktype);
1158 if (bcache_device_init(d, block_bytes(c)))
1161 bcache_device_attach(d, c, u - c->uuids);
1162 set_capacity(d->disk, u->sectors);
1163 bch_flash_dev_request_init(d);
1166 if (kobject_add(&d->kobj, &disk_to_dev(d->disk)->kobj, "bcache"))
1169 bcache_device_link(d, c, "volume");
1173 kobject_put(&d->kobj);
1177 static int flash_devs_run(struct cache_set *c)
1180 struct uuid_entry *u;
1183 u < c->uuids + c->nr_uuids && !ret;
1185 if (UUID_FLASH_ONLY(u))
1186 ret = flash_dev_run(c, u);
1191 int bch_flash_dev_create(struct cache_set *c, uint64_t size)
1193 struct uuid_entry *u;
1195 if (test_bit(CACHE_SET_STOPPING, &c->flags))
1198 u = uuid_find_empty(c);
1200 pr_err("Can't create volume, no room for UUID");
1204 get_random_bytes(u->uuid, 16);
1205 memset(u->label, 0, 32);
1206 u->first_reg = u->last_reg = cpu_to_le32(get_seconds());
1208 SET_UUID_FLASH_ONLY(u, 1);
1209 u->sectors = size >> 9;
1213 return flash_dev_run(c, u);
1219 bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...)
1223 if (test_bit(CACHE_SET_STOPPING, &c->flags))
1226 /* XXX: we can be called from atomic context
1227 acquire_console_sem();
1230 printk(KERN_ERR "bcache: error on %pU: ", c->sb.set_uuid);
1232 va_start(args, fmt);
1236 printk(", disabling caching\n");
1238 bch_cache_set_unregister(c);
1242 void bch_cache_set_release(struct kobject *kobj)
1244 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
1246 module_put(THIS_MODULE);
1249 static void cache_set_free(struct closure *cl)
1251 struct cache_set *c = container_of(cl, struct cache_set, cl);
1255 if (!IS_ERR_OR_NULL(c->debug))
1256 debugfs_remove(c->debug);
1258 bch_open_buckets_free(c);
1259 bch_btree_cache_free(c);
1260 bch_journal_free(c);
1262 for_each_cache(ca, c, i)
1264 kobject_put(&ca->kobj);
1266 free_pages((unsigned long) c->uuids, ilog2(bucket_pages(c)));
1267 free_pages((unsigned long) c->sort, ilog2(bucket_pages(c)));
1269 kfree(c->fill_iter);
1271 bioset_free(c->bio_split);
1273 mempool_destroy(c->bio_meta);
1275 mempool_destroy(c->search);
1278 mutex_lock(&bch_register_lock);
1280 mutex_unlock(&bch_register_lock);
1282 pr_info("Cache set %pU unregistered", c->sb.set_uuid);
1283 wake_up(&unregister_wait);
1285 closure_debug_destroy(&c->cl);
1286 kobject_put(&c->kobj);
1289 static void cache_set_flush(struct closure *cl)
1291 struct cache_set *c = container_of(cl, struct cache_set, caching);
1294 /* Shut down allocator threads */
1295 set_bit(CACHE_SET_STOPPING_2, &c->flags);
1296 wake_up(&c->alloc_wait);
1298 bch_cache_accounting_destroy(&c->accounting);
1300 kobject_put(&c->internal);
1301 kobject_del(&c->kobj);
1303 if (!IS_ERR_OR_NULL(c->root))
1304 list_add(&c->root->list, &c->btree_cache);
1306 /* Should skip this if we're unregistering because of an error */
1307 list_for_each_entry(b, &c->btree_cache, list)
1308 if (btree_node_dirty(b))
1309 bch_btree_write(b, true, NULL);
1314 static void __cache_set_unregister(struct closure *cl)
1316 struct cache_set *c = container_of(cl, struct cache_set, caching);
1317 struct cached_dev *dc;
1320 mutex_lock(&bch_register_lock);
1322 for (i = 0; i < c->nr_uuids; i++)
1323 if (c->devices[i]) {
1324 if (!UUID_FLASH_ONLY(&c->uuids[i]) &&
1325 test_bit(CACHE_SET_UNREGISTERING, &c->flags)) {
1326 dc = container_of(c->devices[i],
1327 struct cached_dev, disk);
1328 bch_cached_dev_detach(dc);
1330 bcache_device_stop(c->devices[i]);
1334 mutex_unlock(&bch_register_lock);
1336 continue_at(cl, cache_set_flush, system_wq);
1339 void bch_cache_set_stop(struct cache_set *c)
1341 if (!test_and_set_bit(CACHE_SET_STOPPING, &c->flags))
1342 closure_queue(&c->caching);
1345 void bch_cache_set_unregister(struct cache_set *c)
1347 set_bit(CACHE_SET_UNREGISTERING, &c->flags);
1348 bch_cache_set_stop(c);
1351 #define alloc_bucket_pages(gfp, c) \
1352 ((void *) __get_free_pages(__GFP_ZERO|gfp, ilog2(bucket_pages(c))))
1354 struct cache_set *bch_cache_set_alloc(struct cache_sb *sb)
1357 struct cache_set *c = kzalloc(sizeof(struct cache_set), GFP_KERNEL);
1361 __module_get(THIS_MODULE);
1362 closure_init(&c->cl, NULL);
1363 set_closure_fn(&c->cl, cache_set_free, system_wq);
1365 closure_init(&c->caching, &c->cl);
1366 set_closure_fn(&c->caching, __cache_set_unregister, system_wq);
1368 /* Maybe create continue_at_noreturn() and use it here? */
1369 closure_set_stopped(&c->cl);
1370 closure_put(&c->cl);
1372 kobject_init(&c->kobj, &bch_cache_set_ktype);
1373 kobject_init(&c->internal, &bch_cache_set_internal_ktype);
1375 bch_cache_accounting_init(&c->accounting, &c->cl);
1377 memcpy(c->sb.set_uuid, sb->set_uuid, 16);
1378 c->sb.block_size = sb->block_size;
1379 c->sb.bucket_size = sb->bucket_size;
1380 c->sb.nr_in_set = sb->nr_in_set;
1381 c->sb.last_mount = sb->last_mount;
1382 c->bucket_bits = ilog2(sb->bucket_size);
1383 c->block_bits = ilog2(sb->block_size);
1384 c->nr_uuids = bucket_bytes(c) / sizeof(struct uuid_entry);
1386 c->btree_pages = c->sb.bucket_size / PAGE_SECTORS;
1387 if (c->btree_pages > BTREE_MAX_PAGES)
1388 c->btree_pages = max_t(int, c->btree_pages / 4,
1391 init_waitqueue_head(&c->alloc_wait);
1392 mutex_init(&c->bucket_lock);
1393 mutex_init(&c->fill_lock);
1394 mutex_init(&c->sort_lock);
1395 spin_lock_init(&c->sort_time_lock);
1396 closure_init_unlocked(&c->sb_write);
1397 closure_init_unlocked(&c->uuid_write);
1398 spin_lock_init(&c->btree_read_time_lock);
1399 bch_moving_init_cache_set(c);
1401 INIT_LIST_HEAD(&c->list);
1402 INIT_LIST_HEAD(&c->cached_devs);
1403 INIT_LIST_HEAD(&c->btree_cache);
1404 INIT_LIST_HEAD(&c->btree_cache_freeable);
1405 INIT_LIST_HEAD(&c->btree_cache_freed);
1406 INIT_LIST_HEAD(&c->data_buckets);
1408 c->search = mempool_create_slab_pool(32, bch_search_cache);
1412 iter_size = (sb->bucket_size / sb->block_size + 1) *
1413 sizeof(struct btree_iter_set);
1415 if (!(c->devices = kzalloc(c->nr_uuids * sizeof(void *), GFP_KERNEL)) ||
1416 !(c->bio_meta = mempool_create_kmalloc_pool(2,
1417 sizeof(struct bbio) + sizeof(struct bio_vec) *
1418 bucket_pages(c))) ||
1419 !(c->bio_split = bioset_create(4, offsetof(struct bbio, bio))) ||
1420 !(c->fill_iter = kmalloc(iter_size, GFP_KERNEL)) ||
1421 !(c->sort = alloc_bucket_pages(GFP_KERNEL, c)) ||
1422 !(c->uuids = alloc_bucket_pages(GFP_KERNEL, c)) ||
1423 bch_journal_alloc(c) ||
1424 bch_btree_cache_alloc(c) ||
1425 bch_open_buckets_alloc(c))
1428 c->fill_iter->size = sb->bucket_size / sb->block_size;
1430 c->congested_read_threshold_us = 2000;
1431 c->congested_write_threshold_us = 20000;
1432 c->error_limit = 8 << IO_ERROR_SHIFT;
1436 bch_cache_set_unregister(c);
1440 static void run_cache_set(struct cache_set *c)
1442 const char *err = "cannot allocate memory";
1443 struct cached_dev *dc, *t;
1448 bch_btree_op_init_stack(&op);
1451 for_each_cache(ca, c, i)
1452 c->nbuckets += ca->sb.nbuckets;
1454 if (CACHE_SYNC(&c->sb)) {
1459 err = "cannot allocate memory for journal";
1460 if (bch_journal_read(c, &journal, &op))
1463 pr_debug("btree_journal_read() done");
1465 err = "no journal entries found";
1466 if (list_empty(&journal))
1469 j = &list_entry(journal.prev, struct journal_replay, list)->j;
1471 err = "IO error reading priorities";
1472 for_each_cache(ca, c, i)
1473 prio_read(ca, j->prio_bucket[ca->sb.nr_this_dev]);
1476 * If prio_read() fails it'll call cache_set_error and we'll
1477 * tear everything down right away, but if we perhaps checked
1478 * sooner we could avoid journal replay.
1483 err = "bad btree root";
1484 if (__bch_ptr_invalid(c, j->btree_level + 1, k))
1487 err = "error reading btree root";
1488 c->root = bch_btree_node_get(c, k, j->btree_level, &op);
1489 if (IS_ERR_OR_NULL(c->root))
1492 list_del_init(&c->root->list);
1493 rw_unlock(true, c->root);
1495 err = uuid_read(c, j, &op.cl);
1499 err = "error in recovery";
1500 if (bch_btree_check(c, &op))
1503 bch_journal_mark(c, &journal);
1504 bch_btree_gc_finish(c);
1505 pr_debug("btree_check() done");
1508 * bcache_journal_next() can't happen sooner, or
1509 * btree_gc_finish() will give spurious errors about last_gc >
1510 * gc_gen - this is a hack but oh well.
1512 bch_journal_next(&c->journal);
1514 for_each_cache(ca, c, i)
1515 closure_call(&ca->alloc, bch_allocator_thread,
1519 * First place it's safe to allocate: btree_check() and
1520 * btree_gc_finish() have to run before we have buckets to
1521 * allocate, and bch_bucket_alloc_set() might cause a journal
1522 * entry to be written so bcache_journal_next() has to be called
1525 * If the uuids were in the old format we have to rewrite them
1526 * before the next journal entry is written:
1528 if (j->version < BCACHE_JSET_VERSION_UUID)
1531 bch_journal_replay(c, &journal, &op);
1533 pr_notice("invalidating existing data");
1534 /* Don't want invalidate_buckets() to queue a gc yet */
1535 closure_lock(&c->gc, NULL);
1537 for_each_cache(ca, c, i) {
1540 ca->sb.keys = clamp_t(int, ca->sb.nbuckets >> 7,
1541 2, SB_JOURNAL_BUCKETS);
1543 for (j = 0; j < ca->sb.keys; j++)
1544 ca->sb.d[j] = ca->sb.first_bucket + j;
1547 bch_btree_gc_finish(c);
1549 for_each_cache(ca, c, i)
1550 closure_call(&ca->alloc, bch_allocator_thread,
1551 ca->alloc_workqueue, &c->cl);
1553 mutex_lock(&c->bucket_lock);
1554 for_each_cache(ca, c, i)
1556 mutex_unlock(&c->bucket_lock);
1558 wake_up(&c->alloc_wait);
1560 err = "cannot allocate new UUID bucket";
1561 if (__uuid_write(c))
1564 err = "cannot allocate new btree root";
1565 c->root = bch_btree_node_alloc(c, 0, &op.cl);
1566 if (IS_ERR_OR_NULL(c->root))
1569 bkey_copy_key(&c->root->key, &MAX_KEY);
1570 bch_btree_write(c->root, true, &op);
1572 bch_btree_set_root(c->root);
1573 rw_unlock(true, c->root);
1576 * We don't want to write the first journal entry until
1577 * everything is set up - fortunately journal entries won't be
1578 * written until the SET_CACHE_SYNC() here:
1580 SET_CACHE_SYNC(&c->sb, true);
1582 bch_journal_next(&c->journal);
1583 bch_journal_meta(c, &op.cl);
1586 closure_set_stopped(&c->gc.cl);
1587 closure_put(&c->gc.cl);
1590 closure_sync(&op.cl);
1591 c->sb.last_mount = get_seconds();
1592 bcache_write_super(c);
1594 list_for_each_entry_safe(dc, t, &uncached_devices, list)
1595 bch_cached_dev_attach(dc, c);
1601 closure_set_stopped(&c->gc.cl);
1602 closure_put(&c->gc.cl);
1604 closure_sync(&op.cl);
1605 /* XXX: test this, it's broken */
1606 bch_cache_set_error(c, err);
1609 static bool can_attach_cache(struct cache *ca, struct cache_set *c)
1611 return ca->sb.block_size == c->sb.block_size &&
1612 ca->sb.bucket_size == c->sb.block_size &&
1613 ca->sb.nr_in_set == c->sb.nr_in_set;
1616 static const char *register_cache_set(struct cache *ca)
1619 const char *err = "cannot allocate memory";
1620 struct cache_set *c;
1622 list_for_each_entry(c, &bch_cache_sets, list)
1623 if (!memcmp(c->sb.set_uuid, ca->sb.set_uuid, 16)) {
1624 if (c->cache[ca->sb.nr_this_dev])
1625 return "duplicate cache set member";
1627 if (!can_attach_cache(ca, c))
1628 return "cache sb does not match set";
1630 if (!CACHE_SYNC(&ca->sb))
1631 SET_CACHE_SYNC(&c->sb, false);
1636 c = bch_cache_set_alloc(&ca->sb);
1640 err = "error creating kobject";
1641 if (kobject_add(&c->kobj, bcache_kobj, "%pU", c->sb.set_uuid) ||
1642 kobject_add(&c->internal, &c->kobj, "internal"))
1645 if (bch_cache_accounting_add_kobjs(&c->accounting, &c->kobj))
1648 bch_debug_init_cache_set(c);
1650 list_add(&c->list, &bch_cache_sets);
1652 sprintf(buf, "cache%i", ca->sb.nr_this_dev);
1653 if (sysfs_create_link(&ca->kobj, &c->kobj, "set") ||
1654 sysfs_create_link(&c->kobj, &ca->kobj, buf))
1657 if (ca->sb.seq > c->sb.seq) {
1658 c->sb.version = ca->sb.version;
1659 memcpy(c->sb.set_uuid, ca->sb.set_uuid, 16);
1660 c->sb.flags = ca->sb.flags;
1661 c->sb.seq = ca->sb.seq;
1662 pr_debug("set version = %llu", c->sb.version);
1666 ca->set->cache[ca->sb.nr_this_dev] = ca;
1667 c->cache_by_alloc[c->caches_loaded++] = ca;
1669 if (c->caches_loaded == c->sb.nr_in_set)
1674 bch_cache_set_unregister(c);
1680 void bch_cache_release(struct kobject *kobj)
1682 struct cache *ca = container_of(kobj, struct cache, kobj);
1685 ca->set->cache[ca->sb.nr_this_dev] = NULL;
1687 bch_cache_allocator_exit(ca);
1689 bio_split_pool_free(&ca->bio_split_hook);
1691 if (ca->alloc_workqueue)
1692 destroy_workqueue(ca->alloc_workqueue);
1694 free_pages((unsigned long) ca->disk_buckets, ilog2(bucket_pages(ca)));
1695 kfree(ca->prio_buckets);
1698 free_heap(&ca->heap);
1699 free_fifo(&ca->unused);
1700 free_fifo(&ca->free_inc);
1701 free_fifo(&ca->free);
1703 if (ca->sb_bio.bi_inline_vecs[0].bv_page)
1704 put_page(ca->sb_bio.bi_io_vec[0].bv_page);
1706 if (!IS_ERR_OR_NULL(ca->bdev)) {
1707 blk_sync_queue(bdev_get_queue(ca->bdev));
1708 blkdev_put(ca->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1712 module_put(THIS_MODULE);
1715 static int cache_alloc(struct cache_sb *sb, struct cache *ca)
1720 __module_get(THIS_MODULE);
1721 kobject_init(&ca->kobj, &bch_cache_ktype);
1723 INIT_LIST_HEAD(&ca->discards);
1725 bio_init(&ca->journal.bio);
1726 ca->journal.bio.bi_max_vecs = 8;
1727 ca->journal.bio.bi_io_vec = ca->journal.bio.bi_inline_vecs;
1729 free = roundup_pow_of_two(ca->sb.nbuckets) >> 9;
1730 free = max_t(size_t, free, (prio_buckets(ca) + 8) * 2);
1732 if (!init_fifo(&ca->free, free, GFP_KERNEL) ||
1733 !init_fifo(&ca->free_inc, free << 2, GFP_KERNEL) ||
1734 !init_fifo(&ca->unused, free << 2, GFP_KERNEL) ||
1735 !init_heap(&ca->heap, free << 3, GFP_KERNEL) ||
1736 !(ca->buckets = vzalloc(sizeof(struct bucket) *
1737 ca->sb.nbuckets)) ||
1738 !(ca->prio_buckets = kzalloc(sizeof(uint64_t) * prio_buckets(ca) *
1740 !(ca->disk_buckets = alloc_bucket_pages(GFP_KERNEL, ca)) ||
1741 !(ca->alloc_workqueue = alloc_workqueue("bch_allocator", 0, 1)) ||
1742 bio_split_pool_init(&ca->bio_split_hook))
1745 ca->prio_last_buckets = ca->prio_buckets + prio_buckets(ca);
1747 for_each_bucket(b, ca)
1748 atomic_set(&b->pin, 0);
1750 if (bch_cache_allocator_init(ca))
1755 kobject_put(&ca->kobj);
1759 static void register_cache(struct cache_sb *sb, struct page *sb_page,
1760 struct block_device *bdev, struct cache *ca)
1762 char name[BDEVNAME_SIZE];
1763 const char *err = "cannot allocate memory";
1765 memcpy(&ca->sb, sb, sizeof(struct cache_sb));
1767 ca->bdev->bd_holder = ca;
1769 bio_init(&ca->sb_bio);
1770 ca->sb_bio.bi_max_vecs = 1;
1771 ca->sb_bio.bi_io_vec = ca->sb_bio.bi_inline_vecs;
1772 ca->sb_bio.bi_io_vec[0].bv_page = sb_page;
1775 if (blk_queue_discard(bdev_get_queue(ca->bdev)))
1776 ca->discard = CACHE_DISCARD(&ca->sb);
1778 if (cache_alloc(sb, ca) != 0)
1781 err = "error creating kobject";
1782 if (kobject_add(&ca->kobj, &part_to_dev(bdev->bd_part)->kobj, "bcache"))
1785 err = register_cache_set(ca);
1789 pr_info("registered cache device %s", bdevname(bdev, name));
1792 pr_notice("error opening %s: %s", bdevname(bdev, name), err);
1793 kobject_put(&ca->kobj);
1796 /* Global interfaces/init */
1798 static ssize_t register_bcache(struct kobject *, struct kobj_attribute *,
1799 const char *, size_t);
1801 kobj_attribute_write(register, register_bcache);
1802 kobj_attribute_write(register_quiet, register_bcache);
1804 static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
1805 const char *buffer, size_t size)
1808 const char *err = "cannot allocate memory";
1810 struct cache_sb *sb = NULL;
1811 struct block_device *bdev = NULL;
1812 struct page *sb_page = NULL;
1814 if (!try_module_get(THIS_MODULE))
1817 mutex_lock(&bch_register_lock);
1819 if (!(path = kstrndup(buffer, size, GFP_KERNEL)) ||
1820 !(sb = kmalloc(sizeof(struct cache_sb), GFP_KERNEL)))
1823 err = "failed to open device";
1824 bdev = blkdev_get_by_path(strim(path),
1825 FMODE_READ|FMODE_WRITE|FMODE_EXCL,
1828 if (bdev == ERR_PTR(-EBUSY))
1829 err = "device busy";
1833 err = "failed to set blocksize";
1834 if (set_blocksize(bdev, 4096))
1837 err = read_super(sb, bdev, &sb_page);
1841 if (SB_IS_BDEV(sb)) {
1842 struct cached_dev *dc = kzalloc(sizeof(*dc), GFP_KERNEL);
1846 register_bdev(sb, sb_page, bdev, dc);
1848 struct cache *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
1852 register_cache(sb, sb_page, bdev, ca);
1859 mutex_unlock(&bch_register_lock);
1860 module_put(THIS_MODULE);
1864 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1866 if (attr != &ksysfs_register_quiet)
1867 pr_info("error opening %s: %s", path, err);
1872 static int bcache_reboot(struct notifier_block *n, unsigned long code, void *x)
1874 if (code == SYS_DOWN ||
1876 code == SYS_POWER_OFF) {
1878 unsigned long start = jiffies;
1879 bool stopped = false;
1881 struct cache_set *c, *tc;
1882 struct cached_dev *dc, *tdc;
1884 mutex_lock(&bch_register_lock);
1886 if (list_empty(&bch_cache_sets) &&
1887 list_empty(&uncached_devices))
1890 pr_info("Stopping all devices:");
1892 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
1893 bch_cache_set_stop(c);
1895 list_for_each_entry_safe(dc, tdc, &uncached_devices, list)
1896 bcache_device_stop(&dc->disk);
1898 /* What's a condition variable? */
1900 long timeout = start + 2 * HZ - jiffies;
1902 stopped = list_empty(&bch_cache_sets) &&
1903 list_empty(&uncached_devices);
1905 if (timeout < 0 || stopped)
1908 prepare_to_wait(&unregister_wait, &wait,
1909 TASK_UNINTERRUPTIBLE);
1911 mutex_unlock(&bch_register_lock);
1912 schedule_timeout(timeout);
1913 mutex_lock(&bch_register_lock);
1916 finish_wait(&unregister_wait, &wait);
1919 pr_info("All devices stopped");
1921 pr_notice("Timeout waiting for devices to be closed");
1923 mutex_unlock(&bch_register_lock);
1929 static struct notifier_block reboot = {
1930 .notifier_call = bcache_reboot,
1931 .priority = INT_MAX, /* before any real devices */
1934 static void bcache_exit(void)
1937 bch_writeback_exit();
1941 kobject_put(bcache_kobj);
1943 destroy_workqueue(bcache_wq);
1944 unregister_blkdev(bcache_major, "bcache");
1945 unregister_reboot_notifier(&reboot);
1948 static int __init bcache_init(void)
1950 static const struct attribute *files[] = {
1951 &ksysfs_register.attr,
1952 &ksysfs_register_quiet.attr,
1956 mutex_init(&bch_register_lock);
1957 init_waitqueue_head(&unregister_wait);
1958 register_reboot_notifier(&reboot);
1959 closure_debug_init();
1961 bcache_major = register_blkdev(0, "bcache");
1962 if (bcache_major < 0)
1963 return bcache_major;
1965 if (!(bcache_wq = create_workqueue("bcache")) ||
1966 !(bcache_kobj = kobject_create_and_add("bcache", fs_kobj)) ||
1967 sysfs_create_files(bcache_kobj, files) ||
1969 bch_request_init() ||
1970 bch_writeback_init() ||
1971 bch_debug_init(bcache_kobj))
1980 module_exit(bcache_exit);
1981 module_init(bcache_init);