1 // SPDX-License-Identifier: GPL-2.0
3 * bcache setup/teardown code, and some metadata io - read a superblock and
4 * figure out what to do with it.
6 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
7 * Copyright 2012 Google, Inc.
15 #include "writeback.h"
17 #include <linux/blkdev.h>
18 #include <linux/buffer_head.h>
19 #include <linux/debugfs.h>
20 #include <linux/genhd.h>
21 #include <linux/idr.h>
22 #include <linux/kthread.h>
23 #include <linux/module.h>
24 #include <linux/random.h>
25 #include <linux/reboot.h>
26 #include <linux/sysfs.h>
28 unsigned int bch_cutoff_writeback;
29 unsigned int bch_cutoff_writeback_sync;
31 static const char bcache_magic[] = {
32 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca,
33 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81
36 static const char invalid_uuid[] = {
37 0xa0, 0x3e, 0xf8, 0xed, 0x3e, 0xe1, 0xb8, 0x78,
38 0xc8, 0x50, 0xfc, 0x5e, 0xcb, 0x16, 0xcd, 0x99
41 static struct kobject *bcache_kobj;
42 struct mutex bch_register_lock;
43 LIST_HEAD(bch_cache_sets);
44 static LIST_HEAD(uncached_devices);
46 static int bcache_major;
47 static DEFINE_IDA(bcache_device_idx);
48 static wait_queue_head_t unregister_wait;
49 struct workqueue_struct *bcache_wq;
50 struct workqueue_struct *bch_journal_wq;
52 #define BTREE_MAX_PAGES (256 * 1024 / PAGE_SIZE)
53 /* limitation of partitions number on single bcache device */
54 #define BCACHE_MINORS 128
55 /* limitation of bcache devices number on single system */
56 #define BCACHE_DEVICE_IDX_MAX ((1U << MINORBITS)/BCACHE_MINORS)
60 static const char *read_super(struct cache_sb *sb, struct block_device *bdev,
65 struct buffer_head *bh = __bread(bdev, 1, SB_SIZE);
71 s = (struct cache_sb *) bh->b_data;
73 sb->offset = le64_to_cpu(s->offset);
74 sb->version = le64_to_cpu(s->version);
76 memcpy(sb->magic, s->magic, 16);
77 memcpy(sb->uuid, s->uuid, 16);
78 memcpy(sb->set_uuid, s->set_uuid, 16);
79 memcpy(sb->label, s->label, SB_LABEL_SIZE);
81 sb->flags = le64_to_cpu(s->flags);
82 sb->seq = le64_to_cpu(s->seq);
83 sb->last_mount = le32_to_cpu(s->last_mount);
84 sb->first_bucket = le16_to_cpu(s->first_bucket);
85 sb->keys = le16_to_cpu(s->keys);
87 for (i = 0; i < SB_JOURNAL_BUCKETS; i++)
88 sb->d[i] = le64_to_cpu(s->d[i]);
90 pr_debug("read sb version %llu, flags %llu, seq %llu, journal size %u",
91 sb->version, sb->flags, sb->seq, sb->keys);
93 err = "Not a bcache superblock";
94 if (sb->offset != SB_SECTOR)
97 if (memcmp(sb->magic, bcache_magic, 16))
100 err = "Too many journal buckets";
101 if (sb->keys > SB_JOURNAL_BUCKETS)
104 err = "Bad checksum";
105 if (s->csum != csum_set(s))
109 if (bch_is_zero(sb->uuid, 16))
112 sb->block_size = le16_to_cpu(s->block_size);
114 err = "Superblock block size smaller than device block size";
115 if (sb->block_size << 9 < bdev_logical_block_size(bdev))
118 switch (sb->version) {
119 case BCACHE_SB_VERSION_BDEV:
120 sb->data_offset = BDEV_DATA_START_DEFAULT;
122 case BCACHE_SB_VERSION_BDEV_WITH_OFFSET:
123 sb->data_offset = le64_to_cpu(s->data_offset);
125 err = "Bad data offset";
126 if (sb->data_offset < BDEV_DATA_START_DEFAULT)
130 case BCACHE_SB_VERSION_CDEV:
131 case BCACHE_SB_VERSION_CDEV_WITH_UUID:
132 sb->nbuckets = le64_to_cpu(s->nbuckets);
133 sb->bucket_size = le16_to_cpu(s->bucket_size);
135 sb->nr_in_set = le16_to_cpu(s->nr_in_set);
136 sb->nr_this_dev = le16_to_cpu(s->nr_this_dev);
138 err = "Too many buckets";
139 if (sb->nbuckets > LONG_MAX)
142 err = "Not enough buckets";
143 if (sb->nbuckets < 1 << 7)
146 err = "Bad block/bucket size";
147 if (!is_power_of_2(sb->block_size) ||
148 sb->block_size > PAGE_SECTORS ||
149 !is_power_of_2(sb->bucket_size) ||
150 sb->bucket_size < PAGE_SECTORS)
153 err = "Invalid superblock: device too small";
154 if (get_capacity(bdev->bd_disk) <
155 sb->bucket_size * sb->nbuckets)
159 if (bch_is_zero(sb->set_uuid, 16))
162 err = "Bad cache device number in set";
163 if (!sb->nr_in_set ||
164 sb->nr_in_set <= sb->nr_this_dev ||
165 sb->nr_in_set > MAX_CACHES_PER_SET)
168 err = "Journal buckets not sequential";
169 for (i = 0; i < sb->keys; i++)
170 if (sb->d[i] != sb->first_bucket + i)
173 err = "Too many journal buckets";
174 if (sb->first_bucket + sb->keys > sb->nbuckets)
177 err = "Invalid superblock: first bucket comes before end of super";
178 if (sb->first_bucket * sb->bucket_size < 16)
183 err = "Unsupported superblock version";
187 sb->last_mount = (u32)ktime_get_real_seconds();
190 get_page(bh->b_page);
197 static void write_bdev_super_endio(struct bio *bio)
199 struct cached_dev *dc = bio->bi_private;
202 bch_count_backing_io_errors(dc, bio);
204 closure_put(&dc->sb_write);
207 static void __write_super(struct cache_sb *sb, struct bio *bio)
209 struct cache_sb *out = page_address(bio_first_page_all(bio));
212 bio->bi_iter.bi_sector = SB_SECTOR;
213 bio->bi_iter.bi_size = SB_SIZE;
214 bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_SYNC|REQ_META);
215 bch_bio_map(bio, NULL);
217 out->offset = cpu_to_le64(sb->offset);
218 out->version = cpu_to_le64(sb->version);
220 memcpy(out->uuid, sb->uuid, 16);
221 memcpy(out->set_uuid, sb->set_uuid, 16);
222 memcpy(out->label, sb->label, SB_LABEL_SIZE);
224 out->flags = cpu_to_le64(sb->flags);
225 out->seq = cpu_to_le64(sb->seq);
227 out->last_mount = cpu_to_le32(sb->last_mount);
228 out->first_bucket = cpu_to_le16(sb->first_bucket);
229 out->keys = cpu_to_le16(sb->keys);
231 for (i = 0; i < sb->keys; i++)
232 out->d[i] = cpu_to_le64(sb->d[i]);
234 out->csum = csum_set(out);
236 pr_debug("ver %llu, flags %llu, seq %llu",
237 sb->version, sb->flags, sb->seq);
242 static void bch_write_bdev_super_unlock(struct closure *cl)
244 struct cached_dev *dc = container_of(cl, struct cached_dev, sb_write);
246 up(&dc->sb_write_mutex);
249 void bch_write_bdev_super(struct cached_dev *dc, struct closure *parent)
251 struct closure *cl = &dc->sb_write;
252 struct bio *bio = &dc->sb_bio;
254 down(&dc->sb_write_mutex);
255 closure_init(cl, parent);
258 bio_set_dev(bio, dc->bdev);
259 bio->bi_end_io = write_bdev_super_endio;
260 bio->bi_private = dc;
263 /* I/O request sent to backing device */
264 __write_super(&dc->sb, bio);
266 closure_return_with_destructor(cl, bch_write_bdev_super_unlock);
269 static void write_super_endio(struct bio *bio)
271 struct cache *ca = bio->bi_private;
274 bch_count_io_errors(ca, bio->bi_status, 0,
275 "writing superblock");
276 closure_put(&ca->set->sb_write);
279 static void bcache_write_super_unlock(struct closure *cl)
281 struct cache_set *c = container_of(cl, struct cache_set, sb_write);
283 up(&c->sb_write_mutex);
286 void bcache_write_super(struct cache_set *c)
288 struct closure *cl = &c->sb_write;
292 down(&c->sb_write_mutex);
293 closure_init(cl, &c->cl);
297 for_each_cache(ca, c, i) {
298 struct bio *bio = &ca->sb_bio;
300 ca->sb.version = BCACHE_SB_VERSION_CDEV_WITH_UUID;
301 ca->sb.seq = c->sb.seq;
302 ca->sb.last_mount = c->sb.last_mount;
304 SET_CACHE_SYNC(&ca->sb, CACHE_SYNC(&c->sb));
307 bio_set_dev(bio, ca->bdev);
308 bio->bi_end_io = write_super_endio;
309 bio->bi_private = ca;
312 __write_super(&ca->sb, bio);
315 closure_return_with_destructor(cl, bcache_write_super_unlock);
320 static void uuid_endio(struct bio *bio)
322 struct closure *cl = bio->bi_private;
323 struct cache_set *c = container_of(cl, struct cache_set, uuid_write);
325 cache_set_err_on(bio->bi_status, c, "accessing uuids");
326 bch_bbio_free(bio, c);
330 static void uuid_io_unlock(struct closure *cl)
332 struct cache_set *c = container_of(cl, struct cache_set, uuid_write);
334 up(&c->uuid_write_mutex);
337 static void uuid_io(struct cache_set *c, int op, unsigned long op_flags,
338 struct bkey *k, struct closure *parent)
340 struct closure *cl = &c->uuid_write;
341 struct uuid_entry *u;
346 down(&c->uuid_write_mutex);
347 closure_init(cl, parent);
349 for (i = 0; i < KEY_PTRS(k); i++) {
350 struct bio *bio = bch_bbio_alloc(c);
352 bio->bi_opf = REQ_SYNC | REQ_META | op_flags;
353 bio->bi_iter.bi_size = KEY_SIZE(k) << 9;
355 bio->bi_end_io = uuid_endio;
356 bio->bi_private = cl;
357 bio_set_op_attrs(bio, op, REQ_SYNC|REQ_META|op_flags);
358 bch_bio_map(bio, c->uuids);
360 bch_submit_bbio(bio, c, k, i);
362 if (op != REQ_OP_WRITE)
366 bch_extent_to_text(buf, sizeof(buf), k);
367 pr_debug("%s UUIDs at %s", op == REQ_OP_WRITE ? "wrote" : "read", buf);
369 for (u = c->uuids; u < c->uuids + c->nr_uuids; u++)
370 if (!bch_is_zero(u->uuid, 16))
371 pr_debug("Slot %zi: %pU: %s: 1st: %u last: %u inv: %u",
372 u - c->uuids, u->uuid, u->label,
373 u->first_reg, u->last_reg, u->invalidated);
375 closure_return_with_destructor(cl, uuid_io_unlock);
378 static char *uuid_read(struct cache_set *c, struct jset *j, struct closure *cl)
380 struct bkey *k = &j->uuid_bucket;
382 if (__bch_btree_ptr_invalid(c, k))
383 return "bad uuid pointer";
385 bkey_copy(&c->uuid_bucket, k);
386 uuid_io(c, REQ_OP_READ, 0, k, cl);
388 if (j->version < BCACHE_JSET_VERSION_UUIDv1) {
389 struct uuid_entry_v0 *u0 = (void *) c->uuids;
390 struct uuid_entry *u1 = (void *) c->uuids;
396 * Since the new uuid entry is bigger than the old, we have to
397 * convert starting at the highest memory address and work down
398 * in order to do it in place
401 for (i = c->nr_uuids - 1;
404 memcpy(u1[i].uuid, u0[i].uuid, 16);
405 memcpy(u1[i].label, u0[i].label, 32);
407 u1[i].first_reg = u0[i].first_reg;
408 u1[i].last_reg = u0[i].last_reg;
409 u1[i].invalidated = u0[i].invalidated;
419 static int __uuid_write(struct cache_set *c)
425 closure_init_stack(&cl);
426 lockdep_assert_held(&bch_register_lock);
428 if (bch_bucket_alloc_set(c, RESERVE_BTREE, &k.key, 1, true))
431 SET_KEY_SIZE(&k.key, c->sb.bucket_size);
432 uuid_io(c, REQ_OP_WRITE, 0, &k.key, &cl);
435 /* Only one bucket used for uuid write */
436 ca = PTR_CACHE(c, &k.key, 0);
437 atomic_long_add(ca->sb.bucket_size, &ca->meta_sectors_written);
439 bkey_copy(&c->uuid_bucket, &k.key);
444 int bch_uuid_write(struct cache_set *c)
446 int ret = __uuid_write(c);
449 bch_journal_meta(c, NULL);
454 static struct uuid_entry *uuid_find(struct cache_set *c, const char *uuid)
456 struct uuid_entry *u;
459 u < c->uuids + c->nr_uuids; u++)
460 if (!memcmp(u->uuid, uuid, 16))
466 static struct uuid_entry *uuid_find_empty(struct cache_set *c)
468 static const char zero_uuid[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
470 return uuid_find(c, zero_uuid);
474 * Bucket priorities/gens:
476 * For each bucket, we store on disk its
480 * See alloc.c for an explanation of the gen. The priority is used to implement
481 * lru (and in the future other) cache replacement policies; for most purposes
482 * it's just an opaque integer.
484 * The gens and the priorities don't have a whole lot to do with each other, and
485 * it's actually the gens that must be written out at specific times - it's no
486 * big deal if the priorities don't get written, if we lose them we just reuse
487 * buckets in suboptimal order.
489 * On disk they're stored in a packed array, and in as many buckets are required
490 * to fit them all. The buckets we use to store them form a list; the journal
491 * header points to the first bucket, the first bucket points to the second
494 * This code is used by the allocation code; periodically (whenever it runs out
495 * of buckets to allocate from) the allocation code will invalidate some
496 * buckets, but it can't use those buckets until their new gens are safely on
500 static void prio_endio(struct bio *bio)
502 struct cache *ca = bio->bi_private;
504 cache_set_err_on(bio->bi_status, ca->set, "accessing priorities");
505 bch_bbio_free(bio, ca->set);
506 closure_put(&ca->prio);
509 static void prio_io(struct cache *ca, uint64_t bucket, int op,
510 unsigned long op_flags)
512 struct closure *cl = &ca->prio;
513 struct bio *bio = bch_bbio_alloc(ca->set);
515 closure_init_stack(cl);
517 bio->bi_iter.bi_sector = bucket * ca->sb.bucket_size;
518 bio_set_dev(bio, ca->bdev);
519 bio->bi_iter.bi_size = bucket_bytes(ca);
521 bio->bi_end_io = prio_endio;
522 bio->bi_private = ca;
523 bio_set_op_attrs(bio, op, REQ_SYNC|REQ_META|op_flags);
524 bch_bio_map(bio, ca->disk_buckets);
526 closure_bio_submit(ca->set, bio, &ca->prio);
530 void bch_prio_write(struct cache *ca)
536 closure_init_stack(&cl);
538 lockdep_assert_held(&ca->set->bucket_lock);
540 ca->disk_buckets->seq++;
542 atomic_long_add(ca->sb.bucket_size * prio_buckets(ca),
543 &ca->meta_sectors_written);
545 //pr_debug("free %zu, free_inc %zu, unused %zu", fifo_used(&ca->free),
546 // fifo_used(&ca->free_inc), fifo_used(&ca->unused));
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->sb);
563 p->csum = bch_crc64(&p->magic, bucket_bytes(ca) - 8);
565 bucket = bch_bucket_alloc(ca, RESERVE_PRIO, true);
566 BUG_ON(bucket == -1);
568 mutex_unlock(&ca->set->bucket_lock);
569 prio_io(ca, bucket, REQ_OP_WRITE, 0);
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);
584 * Don't want the old priorities to get garbage collected until after we
585 * finish writing the new ones, and they're journalled
587 for (i = 0; i < prio_buckets(ca); i++) {
588 if (ca->prio_last_buckets[i])
589 __bch_bucket_free(ca,
590 &ca->buckets[ca->prio_last_buckets[i]]);
592 ca->prio_last_buckets[i] = ca->prio_buckets[i];
596 static void prio_read(struct cache *ca, uint64_t bucket)
598 struct prio_set *p = ca->disk_buckets;
599 struct bucket_disk *d = p->data + prios_per_bucket(ca), *end = d;
601 unsigned int bucket_nr = 0;
603 for (b = ca->buckets;
604 b < ca->buckets + ca->sb.nbuckets;
607 ca->prio_buckets[bucket_nr] = bucket;
608 ca->prio_last_buckets[bucket_nr] = bucket;
611 prio_io(ca, bucket, REQ_OP_READ, 0);
614 bch_crc64(&p->magic, bucket_bytes(ca) - 8))
615 pr_warn("bad csum reading priorities");
617 if (p->magic != pset_magic(&ca->sb))
618 pr_warn("bad magic reading priorities");
620 bucket = p->next_bucket;
624 b->prio = le16_to_cpu(d->prio);
625 b->gen = b->last_gc = d->gen;
631 static int open_dev(struct block_device *b, fmode_t mode)
633 struct bcache_device *d = b->bd_disk->private_data;
635 if (test_bit(BCACHE_DEV_CLOSING, &d->flags))
642 static void release_dev(struct gendisk *b, fmode_t mode)
644 struct bcache_device *d = b->private_data;
649 static int ioctl_dev(struct block_device *b, fmode_t mode,
650 unsigned int cmd, unsigned long arg)
652 struct bcache_device *d = b->bd_disk->private_data;
654 return d->ioctl(d, mode, cmd, arg);
657 static const struct block_device_operations bcache_ops = {
659 .release = release_dev,
661 .owner = THIS_MODULE,
664 void bcache_device_stop(struct bcache_device *d)
666 if (!test_and_set_bit(BCACHE_DEV_CLOSING, &d->flags))
669 * - cached device: cached_dev_flush()
670 * - flash dev: flash_dev_flush()
672 closure_queue(&d->cl);
675 static void bcache_device_unlink(struct bcache_device *d)
677 lockdep_assert_held(&bch_register_lock);
679 if (d->c && !test_and_set_bit(BCACHE_DEV_UNLINK_DONE, &d->flags)) {
683 sysfs_remove_link(&d->c->kobj, d->name);
684 sysfs_remove_link(&d->kobj, "cache");
686 for_each_cache(ca, d->c, i)
687 bd_unlink_disk_holder(ca->bdev, d->disk);
691 static void bcache_device_link(struct bcache_device *d, struct cache_set *c,
697 for_each_cache(ca, d->c, i)
698 bd_link_disk_holder(ca->bdev, d->disk);
700 snprintf(d->name, BCACHEDEVNAME_SIZE,
701 "%s%u", name, d->id);
703 WARN(sysfs_create_link(&d->kobj, &c->kobj, "cache") ||
704 sysfs_create_link(&c->kobj, &d->kobj, d->name),
705 "Couldn't create device <-> cache set symlinks");
707 clear_bit(BCACHE_DEV_UNLINK_DONE, &d->flags);
710 static void bcache_device_detach(struct bcache_device *d)
712 lockdep_assert_held(&bch_register_lock);
714 atomic_dec(&d->c->attached_dev_nr);
716 if (test_bit(BCACHE_DEV_DETACHING, &d->flags)) {
717 struct uuid_entry *u = d->c->uuids + d->id;
719 SET_UUID_FLASH_ONLY(u, 0);
720 memcpy(u->uuid, invalid_uuid, 16);
721 u->invalidated = cpu_to_le32((u32)ktime_get_real_seconds());
722 bch_uuid_write(d->c);
725 bcache_device_unlink(d);
727 d->c->devices[d->id] = NULL;
728 closure_put(&d->c->caching);
732 static void bcache_device_attach(struct bcache_device *d, struct cache_set *c,
739 if (id >= c->devices_max_used)
740 c->devices_max_used = id + 1;
742 closure_get(&c->caching);
745 static inline int first_minor_to_idx(int first_minor)
747 return (first_minor/BCACHE_MINORS);
750 static inline int idx_to_first_minor(int idx)
752 return (idx * BCACHE_MINORS);
755 static void bcache_device_free(struct bcache_device *d)
757 lockdep_assert_held(&bch_register_lock);
759 pr_info("%s stopped", d->disk->disk_name);
762 bcache_device_detach(d);
763 if (d->disk && d->disk->flags & GENHD_FL_UP)
764 del_gendisk(d->disk);
765 if (d->disk && d->disk->queue)
766 blk_cleanup_queue(d->disk->queue);
768 ida_simple_remove(&bcache_device_idx,
769 first_minor_to_idx(d->disk->first_minor));
773 bioset_exit(&d->bio_split);
774 kvfree(d->full_dirty_stripes);
775 kvfree(d->stripe_sectors_dirty);
777 closure_debug_destroy(&d->cl);
780 static int bcache_device_init(struct bcache_device *d, unsigned int block_size,
783 struct request_queue *q;
784 const size_t max_stripes = min_t(size_t, INT_MAX,
785 SIZE_MAX / sizeof(atomic_t));
790 d->stripe_size = 1 << 31;
792 d->nr_stripes = DIV_ROUND_UP_ULL(sectors, d->stripe_size);
794 if (!d->nr_stripes || d->nr_stripes > max_stripes) {
795 pr_err("nr_stripes too large or invalid: %u (start sector beyond end of disk?)",
796 (unsigned int)d->nr_stripes);
800 n = d->nr_stripes * sizeof(atomic_t);
801 d->stripe_sectors_dirty = kvzalloc(n, GFP_KERNEL);
802 if (!d->stripe_sectors_dirty)
805 n = BITS_TO_LONGS(d->nr_stripes) * sizeof(unsigned long);
806 d->full_dirty_stripes = kvzalloc(n, GFP_KERNEL);
807 if (!d->full_dirty_stripes)
810 idx = ida_simple_get(&bcache_device_idx, 0,
811 BCACHE_DEVICE_IDX_MAX, GFP_KERNEL);
815 if (bioset_init(&d->bio_split, 4, offsetof(struct bbio, bio),
816 BIOSET_NEED_BVECS|BIOSET_NEED_RESCUER))
819 d->disk = alloc_disk(BCACHE_MINORS);
823 set_capacity(d->disk, sectors);
824 snprintf(d->disk->disk_name, DISK_NAME_LEN, "bcache%i", idx);
826 d->disk->major = bcache_major;
827 d->disk->first_minor = idx_to_first_minor(idx);
828 d->disk->fops = &bcache_ops;
829 d->disk->private_data = d;
831 q = blk_alloc_queue(GFP_KERNEL);
835 blk_queue_make_request(q, NULL);
838 q->backing_dev_info->congested_data = d;
839 q->limits.max_hw_sectors = UINT_MAX;
840 q->limits.max_sectors = UINT_MAX;
841 q->limits.max_segment_size = UINT_MAX;
842 q->limits.max_segments = BIO_MAX_PAGES;
843 blk_queue_max_discard_sectors(q, UINT_MAX);
844 q->limits.discard_granularity = 512;
845 q->limits.io_min = block_size;
846 q->limits.logical_block_size = block_size;
847 q->limits.physical_block_size = block_size;
848 blk_queue_flag_set(QUEUE_FLAG_NONROT, d->disk->queue);
849 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, d->disk->queue);
850 blk_queue_flag_set(QUEUE_FLAG_DISCARD, d->disk->queue);
852 blk_queue_write_cache(q, true, true);
857 ida_simple_remove(&bcache_device_idx, idx);
864 static void calc_cached_dev_sectors(struct cache_set *c)
866 uint64_t sectors = 0;
867 struct cached_dev *dc;
869 list_for_each_entry(dc, &c->cached_devs, list)
870 sectors += bdev_sectors(dc->bdev);
872 c->cached_dev_sectors = sectors;
875 #define BACKING_DEV_OFFLINE_TIMEOUT 5
876 static int cached_dev_status_update(void *arg)
878 struct cached_dev *dc = arg;
879 struct request_queue *q;
882 * If this delayed worker is stopping outside, directly quit here.
883 * dc->io_disable might be set via sysfs interface, so check it
886 while (!kthread_should_stop() && !dc->io_disable) {
887 q = bdev_get_queue(dc->bdev);
888 if (blk_queue_dying(q))
889 dc->offline_seconds++;
891 dc->offline_seconds = 0;
893 if (dc->offline_seconds >= BACKING_DEV_OFFLINE_TIMEOUT) {
894 pr_err("%s: device offline for %d seconds",
895 dc->backing_dev_name,
896 BACKING_DEV_OFFLINE_TIMEOUT);
897 pr_err("%s: disable I/O request due to backing "
898 "device offline", dc->disk.name);
899 dc->io_disable = true;
900 /* let others know earlier that io_disable is true */
902 bcache_device_stop(&dc->disk);
905 schedule_timeout_interruptible(HZ);
908 wait_for_kthread_stop();
913 void bch_cached_dev_run(struct cached_dev *dc)
915 struct bcache_device *d = &dc->disk;
916 char *buf = kmemdup_nul(dc->sb.label, SB_LABEL_SIZE, GFP_KERNEL);
919 kasprintf(GFP_KERNEL, "CACHED_UUID=%pU", dc->sb.uuid),
920 kasprintf(GFP_KERNEL, "CACHED_LABEL=%s", buf ? : ""),
924 if (atomic_xchg(&dc->running, 1)) {
932 BDEV_STATE(&dc->sb) != BDEV_STATE_NONE) {
935 closure_init_stack(&cl);
937 SET_BDEV_STATE(&dc->sb, BDEV_STATE_STALE);
938 bch_write_bdev_super(dc, &cl);
943 bd_link_disk_holder(dc->bdev, dc->disk.disk);
945 * won't show up in the uevent file, use udevadm monitor -e instead
946 * only class / kset properties are persistent
948 kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env);
953 if (sysfs_create_link(&d->kobj, &disk_to_dev(d->disk)->kobj, "dev") ||
954 sysfs_create_link(&disk_to_dev(d->disk)->kobj, &d->kobj, "bcache"))
955 pr_debug("error creating sysfs link");
957 dc->status_update_thread = kthread_run(cached_dev_status_update,
958 dc, "bcache_status_update");
959 if (IS_ERR(dc->status_update_thread)) {
960 pr_warn("failed to create bcache_status_update kthread, "
961 "continue to run without monitoring backing "
967 * If BCACHE_DEV_RATE_DW_RUNNING is set, it means routine of the delayed
968 * work dc->writeback_rate_update is running. Wait until the routine
969 * quits (BCACHE_DEV_RATE_DW_RUNNING is clear), then continue to
970 * cancel it. If BCACHE_DEV_RATE_DW_RUNNING is not clear after time_out
971 * seconds, give up waiting here and continue to cancel it too.
973 static void cancel_writeback_rate_update_dwork(struct cached_dev *dc)
975 int time_out = WRITEBACK_RATE_UPDATE_SECS_MAX * HZ;
978 if (!test_bit(BCACHE_DEV_RATE_DW_RUNNING,
982 schedule_timeout_interruptible(1);
983 } while (time_out > 0);
986 pr_warn("give up waiting for dc->writeback_write_update to quit");
988 cancel_delayed_work_sync(&dc->writeback_rate_update);
991 static void cached_dev_detach_finish(struct work_struct *w)
993 struct cached_dev *dc = container_of(w, struct cached_dev, detach);
996 closure_init_stack(&cl);
998 BUG_ON(!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags));
999 BUG_ON(refcount_read(&dc->count));
1001 mutex_lock(&bch_register_lock);
1003 if (test_and_clear_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags))
1004 cancel_writeback_rate_update_dwork(dc);
1006 if (!IS_ERR_OR_NULL(dc->writeback_thread)) {
1007 kthread_stop(dc->writeback_thread);
1008 dc->writeback_thread = NULL;
1011 memset(&dc->sb.set_uuid, 0, 16);
1012 SET_BDEV_STATE(&dc->sb, BDEV_STATE_NONE);
1014 bch_write_bdev_super(dc, &cl);
1017 calc_cached_dev_sectors(dc->disk.c);
1018 bcache_device_detach(&dc->disk);
1019 list_move(&dc->list, &uncached_devices);
1021 clear_bit(BCACHE_DEV_DETACHING, &dc->disk.flags);
1022 clear_bit(BCACHE_DEV_UNLINK_DONE, &dc->disk.flags);
1024 mutex_unlock(&bch_register_lock);
1026 pr_info("Caching disabled for %s", dc->backing_dev_name);
1028 /* Drop ref we took in cached_dev_detach() */
1029 closure_put(&dc->disk.cl);
1032 void bch_cached_dev_detach(struct cached_dev *dc)
1034 lockdep_assert_held(&bch_register_lock);
1036 if (test_bit(BCACHE_DEV_CLOSING, &dc->disk.flags))
1039 if (test_and_set_bit(BCACHE_DEV_DETACHING, &dc->disk.flags))
1043 * Block the device from being closed and freed until we're finished
1046 closure_get(&dc->disk.cl);
1048 bch_writeback_queue(dc);
1053 int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c,
1056 uint32_t rtime = cpu_to_le32((u32)ktime_get_real_seconds());
1057 struct uuid_entry *u;
1058 struct cached_dev *exist_dc, *t;
1060 if ((set_uuid && memcmp(set_uuid, c->sb.set_uuid, 16)) ||
1061 (!set_uuid && memcmp(dc->sb.set_uuid, c->sb.set_uuid, 16)))
1065 pr_err("Can't attach %s: already attached",
1066 dc->backing_dev_name);
1070 if (test_bit(CACHE_SET_STOPPING, &c->flags)) {
1071 pr_err("Can't attach %s: shutting down",
1072 dc->backing_dev_name);
1076 if (dc->sb.block_size < c->sb.block_size) {
1078 pr_err("Couldn't attach %s: block size less than set's block size",
1079 dc->backing_dev_name);
1083 /* Check whether already attached */
1084 list_for_each_entry_safe(exist_dc, t, &c->cached_devs, list) {
1085 if (!memcmp(dc->sb.uuid, exist_dc->sb.uuid, 16)) {
1086 pr_err("Tried to attach %s but duplicate UUID already attached",
1087 dc->backing_dev_name);
1093 u = uuid_find(c, dc->sb.uuid);
1096 (BDEV_STATE(&dc->sb) == BDEV_STATE_STALE ||
1097 BDEV_STATE(&dc->sb) == BDEV_STATE_NONE)) {
1098 memcpy(u->uuid, invalid_uuid, 16);
1099 u->invalidated = cpu_to_le32((u32)ktime_get_real_seconds());
1104 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
1105 pr_err("Couldn't find uuid for %s in set",
1106 dc->backing_dev_name);
1110 u = uuid_find_empty(c);
1112 pr_err("Not caching %s, no room for UUID",
1113 dc->backing_dev_name);
1119 * Deadlocks since we're called via sysfs...
1120 * sysfs_remove_file(&dc->kobj, &sysfs_attach);
1123 if (bch_is_zero(u->uuid, 16)) {
1126 closure_init_stack(&cl);
1128 memcpy(u->uuid, dc->sb.uuid, 16);
1129 memcpy(u->label, dc->sb.label, SB_LABEL_SIZE);
1130 u->first_reg = u->last_reg = rtime;
1133 memcpy(dc->sb.set_uuid, c->sb.set_uuid, 16);
1134 SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN);
1136 bch_write_bdev_super(dc, &cl);
1139 u->last_reg = rtime;
1143 bcache_device_attach(&dc->disk, c, u - c->uuids);
1144 list_move(&dc->list, &c->cached_devs);
1145 calc_cached_dev_sectors(c);
1148 * dc->c must be set before dc->count != 0 - paired with the mb in
1152 refcount_set(&dc->count, 1);
1154 /* Block writeback thread, but spawn it */
1155 down_write(&dc->writeback_lock);
1156 if (bch_cached_dev_writeback_start(dc)) {
1157 up_write(&dc->writeback_lock);
1161 if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) {
1162 atomic_set(&dc->has_dirty, 1);
1163 bch_writeback_queue(dc);
1166 bch_sectors_dirty_init(&dc->disk);
1168 bch_cached_dev_run(dc);
1169 bcache_device_link(&dc->disk, c, "bdev");
1170 atomic_inc(&c->attached_dev_nr);
1172 /* Allow the writeback thread to proceed */
1173 up_write(&dc->writeback_lock);
1175 pr_info("Caching %s as %s on set %pU",
1176 dc->backing_dev_name,
1177 dc->disk.disk->disk_name,
1178 dc->disk.c->sb.set_uuid);
1182 /* when dc->disk.kobj released */
1183 void bch_cached_dev_release(struct kobject *kobj)
1185 struct cached_dev *dc = container_of(kobj, struct cached_dev,
1188 module_put(THIS_MODULE);
1191 static void cached_dev_free(struct closure *cl)
1193 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
1195 mutex_lock(&bch_register_lock);
1197 if (test_and_clear_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags))
1198 cancel_writeback_rate_update_dwork(dc);
1200 if (!IS_ERR_OR_NULL(dc->writeback_thread))
1201 kthread_stop(dc->writeback_thread);
1202 if (dc->writeback_write_wq)
1203 destroy_workqueue(dc->writeback_write_wq);
1204 if (!IS_ERR_OR_NULL(dc->status_update_thread))
1205 kthread_stop(dc->status_update_thread);
1207 if (atomic_read(&dc->running))
1208 bd_unlink_disk_holder(dc->bdev, dc->disk.disk);
1209 bcache_device_free(&dc->disk);
1210 list_del(&dc->list);
1212 mutex_unlock(&bch_register_lock);
1214 if (!IS_ERR_OR_NULL(dc->bdev))
1215 blkdev_put(dc->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1217 wake_up(&unregister_wait);
1219 kobject_put(&dc->disk.kobj);
1222 static void cached_dev_flush(struct closure *cl)
1224 struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl);
1225 struct bcache_device *d = &dc->disk;
1227 mutex_lock(&bch_register_lock);
1228 bcache_device_unlink(d);
1229 mutex_unlock(&bch_register_lock);
1231 bch_cache_accounting_destroy(&dc->accounting);
1232 kobject_del(&d->kobj);
1234 continue_at(cl, cached_dev_free, system_wq);
1237 static int cached_dev_init(struct cached_dev *dc, unsigned int block_size)
1241 struct request_queue *q = bdev_get_queue(dc->bdev);
1243 __module_get(THIS_MODULE);
1244 INIT_LIST_HEAD(&dc->list);
1245 closure_init(&dc->disk.cl, NULL);
1246 set_closure_fn(&dc->disk.cl, cached_dev_flush, system_wq);
1247 kobject_init(&dc->disk.kobj, &bch_cached_dev_ktype);
1248 INIT_WORK(&dc->detach, cached_dev_detach_finish);
1249 sema_init(&dc->sb_write_mutex, 1);
1250 INIT_LIST_HEAD(&dc->io_lru);
1251 spin_lock_init(&dc->io_lock);
1252 bch_cache_accounting_init(&dc->accounting, &dc->disk.cl);
1254 dc->sequential_cutoff = 4 << 20;
1256 for (io = dc->io; io < dc->io + RECENT_IO; io++) {
1257 list_add(&io->lru, &dc->io_lru);
1258 hlist_add_head(&io->hash, dc->io_hash + RECENT_IO);
1261 dc->disk.stripe_size = q->limits.io_opt >> 9;
1263 if (dc->disk.stripe_size)
1264 dc->partial_stripes_expensive =
1265 q->limits.raid_partial_stripes_expensive;
1267 ret = bcache_device_init(&dc->disk, block_size,
1268 dc->bdev->bd_part->nr_sects - dc->sb.data_offset);
1272 dc->disk.disk->queue->backing_dev_info->ra_pages =
1273 max(dc->disk.disk->queue->backing_dev_info->ra_pages,
1274 q->backing_dev_info->ra_pages);
1276 atomic_set(&dc->io_errors, 0);
1277 dc->io_disable = false;
1278 dc->error_limit = DEFAULT_CACHED_DEV_ERROR_LIMIT;
1279 /* default to auto */
1280 dc->stop_when_cache_set_failed = BCH_CACHED_DEV_STOP_AUTO;
1282 bch_cached_dev_request_init(dc);
1283 bch_cached_dev_writeback_init(dc);
1287 /* Cached device - bcache superblock */
1289 static int register_bdev(struct cache_sb *sb, struct page *sb_page,
1290 struct block_device *bdev,
1291 struct cached_dev *dc)
1293 const char *err = "cannot allocate memory";
1294 struct cache_set *c;
1296 bdevname(bdev, dc->backing_dev_name);
1297 memcpy(&dc->sb, sb, sizeof(struct cache_sb));
1299 dc->bdev->bd_holder = dc;
1301 bio_init(&dc->sb_bio, dc->sb_bio.bi_inline_vecs, 1);
1302 bio_first_bvec_all(&dc->sb_bio)->bv_page = sb_page;
1306 if (cached_dev_init(dc, sb->block_size << 9))
1309 err = "error creating kobject";
1310 if (kobject_add(&dc->disk.kobj, &part_to_dev(bdev->bd_part)->kobj,
1313 if (bch_cache_accounting_add_kobjs(&dc->accounting, &dc->disk.kobj))
1316 pr_info("registered backing device %s", dc->backing_dev_name);
1318 list_add(&dc->list, &uncached_devices);
1319 /* attach to a matched cache set if it exists */
1320 list_for_each_entry(c, &bch_cache_sets, list)
1321 bch_cached_dev_attach(dc, c, NULL);
1323 if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE ||
1324 BDEV_STATE(&dc->sb) == BDEV_STATE_STALE)
1325 bch_cached_dev_run(dc);
1329 pr_notice("error %s: %s", dc->backing_dev_name, err);
1330 bcache_device_stop(&dc->disk);
1334 /* Flash only volumes */
1336 /* When d->kobj released */
1337 void bch_flash_dev_release(struct kobject *kobj)
1339 struct bcache_device *d = container_of(kobj, struct bcache_device,
1344 static void flash_dev_free(struct closure *cl)
1346 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1348 mutex_lock(&bch_register_lock);
1349 atomic_long_sub(bcache_dev_sectors_dirty(d),
1350 &d->c->flash_dev_dirty_sectors);
1351 bcache_device_free(d);
1352 mutex_unlock(&bch_register_lock);
1353 kobject_put(&d->kobj);
1356 static void flash_dev_flush(struct closure *cl)
1358 struct bcache_device *d = container_of(cl, struct bcache_device, cl);
1360 mutex_lock(&bch_register_lock);
1361 bcache_device_unlink(d);
1362 mutex_unlock(&bch_register_lock);
1363 kobject_del(&d->kobj);
1364 continue_at(cl, flash_dev_free, system_wq);
1367 static int flash_dev_run(struct cache_set *c, struct uuid_entry *u)
1369 struct bcache_device *d = kzalloc(sizeof(struct bcache_device),
1374 closure_init(&d->cl, NULL);
1375 set_closure_fn(&d->cl, flash_dev_flush, system_wq);
1377 kobject_init(&d->kobj, &bch_flash_dev_ktype);
1379 if (bcache_device_init(d, block_bytes(c), u->sectors))
1382 bcache_device_attach(d, c, u - c->uuids);
1383 bch_sectors_dirty_init(d);
1384 bch_flash_dev_request_init(d);
1387 if (kobject_add(&d->kobj, &disk_to_dev(d->disk)->kobj, "bcache"))
1390 bcache_device_link(d, c, "volume");
1394 kobject_put(&d->kobj);
1398 static int flash_devs_run(struct cache_set *c)
1401 struct uuid_entry *u;
1404 u < c->uuids + c->nr_uuids && !ret;
1406 if (UUID_FLASH_ONLY(u))
1407 ret = flash_dev_run(c, u);
1412 int bch_flash_dev_create(struct cache_set *c, uint64_t size)
1414 struct uuid_entry *u;
1416 if (test_bit(CACHE_SET_STOPPING, &c->flags))
1419 if (!test_bit(CACHE_SET_RUNNING, &c->flags))
1422 u = uuid_find_empty(c);
1424 pr_err("Can't create volume, no room for UUID");
1428 get_random_bytes(u->uuid, 16);
1429 memset(u->label, 0, 32);
1430 u->first_reg = u->last_reg = cpu_to_le32((u32)ktime_get_real_seconds());
1432 SET_UUID_FLASH_ONLY(u, 1);
1433 u->sectors = size >> 9;
1437 return flash_dev_run(c, u);
1440 bool bch_cached_dev_error(struct cached_dev *dc)
1442 if (!dc || test_bit(BCACHE_DEV_CLOSING, &dc->disk.flags))
1445 dc->io_disable = true;
1446 /* make others know io_disable is true earlier */
1449 pr_err("stop %s: too many IO errors on backing device %s\n",
1450 dc->disk.disk->disk_name, dc->backing_dev_name);
1452 bcache_device_stop(&dc->disk);
1459 bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...)
1463 if (c->on_error != ON_ERROR_PANIC &&
1464 test_bit(CACHE_SET_STOPPING, &c->flags))
1467 if (test_and_set_bit(CACHE_SET_IO_DISABLE, &c->flags))
1468 pr_info("CACHE_SET_IO_DISABLE already set");
1471 * XXX: we can be called from atomic context
1472 * acquire_console_sem();
1475 pr_err("bcache: error on %pU: ", c->sb.set_uuid);
1477 va_start(args, fmt);
1481 pr_err(", disabling caching\n");
1483 if (c->on_error == ON_ERROR_PANIC)
1484 panic("panic forced after error\n");
1486 bch_cache_set_unregister(c);
1490 /* When c->kobj released */
1491 void bch_cache_set_release(struct kobject *kobj)
1493 struct cache_set *c = container_of(kobj, struct cache_set, kobj);
1496 module_put(THIS_MODULE);
1499 static void cache_set_free(struct closure *cl)
1501 struct cache_set *c = container_of(cl, struct cache_set, cl);
1505 debugfs_remove(c->debug);
1507 bch_open_buckets_free(c);
1508 bch_btree_cache_free(c);
1509 bch_journal_free(c);
1511 mutex_lock(&bch_register_lock);
1512 for_each_cache(ca, c, i)
1515 c->cache[ca->sb.nr_this_dev] = NULL;
1516 kobject_put(&ca->kobj);
1519 bch_bset_sort_state_free(&c->sort);
1520 free_pages((unsigned long) c->uuids, ilog2(bucket_pages(c)));
1522 if (c->moving_gc_wq)
1523 destroy_workqueue(c->moving_gc_wq);
1524 bioset_exit(&c->bio_split);
1525 mempool_exit(&c->fill_iter);
1526 mempool_exit(&c->bio_meta);
1527 mempool_exit(&c->search);
1531 mutex_unlock(&bch_register_lock);
1533 pr_info("Cache set %pU unregistered", c->sb.set_uuid);
1534 wake_up(&unregister_wait);
1536 closure_debug_destroy(&c->cl);
1537 kobject_put(&c->kobj);
1540 static void cache_set_flush(struct closure *cl)
1542 struct cache_set *c = container_of(cl, struct cache_set, caching);
1547 bch_cache_accounting_destroy(&c->accounting);
1549 kobject_put(&c->internal);
1550 kobject_del(&c->kobj);
1552 if (!IS_ERR_OR_NULL(c->gc_thread))
1553 kthread_stop(c->gc_thread);
1555 if (!IS_ERR_OR_NULL(c->root))
1556 list_add(&c->root->list, &c->btree_cache);
1559 * Avoid flushing cached nodes if cache set is retiring
1560 * due to too many I/O errors detected.
1562 if (!test_bit(CACHE_SET_IO_DISABLE, &c->flags))
1563 list_for_each_entry(b, &c->btree_cache, list) {
1564 mutex_lock(&b->write_lock);
1565 if (btree_node_dirty(b))
1566 __bch_btree_node_write(b, NULL);
1567 mutex_unlock(&b->write_lock);
1570 for_each_cache(ca, c, i)
1571 if (ca->alloc_thread)
1572 kthread_stop(ca->alloc_thread);
1574 if (c->journal.cur) {
1575 cancel_delayed_work_sync(&c->journal.work);
1576 /* flush last journal entry if needed */
1577 c->journal.work.work.func(&c->journal.work.work);
1584 * This function is only called when CACHE_SET_IO_DISABLE is set, which means
1585 * cache set is unregistering due to too many I/O errors. In this condition,
1586 * the bcache device might be stopped, it depends on stop_when_cache_set_failed
1587 * value and whether the broken cache has dirty data:
1589 * dc->stop_when_cache_set_failed dc->has_dirty stop bcache device
1590 * BCH_CACHED_STOP_AUTO 0 NO
1591 * BCH_CACHED_STOP_AUTO 1 YES
1592 * BCH_CACHED_DEV_STOP_ALWAYS 0 YES
1593 * BCH_CACHED_DEV_STOP_ALWAYS 1 YES
1595 * The expected behavior is, if stop_when_cache_set_failed is configured to
1596 * "auto" via sysfs interface, the bcache device will not be stopped if the
1597 * backing device is clean on the broken cache device.
1599 static void conditional_stop_bcache_device(struct cache_set *c,
1600 struct bcache_device *d,
1601 struct cached_dev *dc)
1603 if (dc->stop_when_cache_set_failed == BCH_CACHED_DEV_STOP_ALWAYS) {
1604 pr_warn("stop_when_cache_set_failed of %s is \"always\", stop it for failed cache set %pU.",
1605 d->disk->disk_name, c->sb.set_uuid);
1606 bcache_device_stop(d);
1607 } else if (atomic_read(&dc->has_dirty)) {
1609 * dc->stop_when_cache_set_failed == BCH_CACHED_STOP_AUTO
1610 * and dc->has_dirty == 1
1612 pr_warn("stop_when_cache_set_failed of %s is \"auto\" and cache is dirty, stop it to avoid potential data corruption.",
1613 d->disk->disk_name);
1615 * There might be a small time gap that cache set is
1616 * released but bcache device is not. Inside this time
1617 * gap, regular I/O requests will directly go into
1618 * backing device as no cache set attached to. This
1619 * behavior may also introduce potential inconsistence
1620 * data in writeback mode while cache is dirty.
1621 * Therefore before calling bcache_device_stop() due
1622 * to a broken cache device, dc->io_disable should be
1623 * explicitly set to true.
1625 dc->io_disable = true;
1626 /* make others know io_disable is true earlier */
1628 bcache_device_stop(d);
1631 * dc->stop_when_cache_set_failed == BCH_CACHED_STOP_AUTO
1632 * and dc->has_dirty == 0
1634 pr_warn("stop_when_cache_set_failed of %s is \"auto\" and cache is clean, keep it alive.",
1635 d->disk->disk_name);
1639 static void __cache_set_unregister(struct closure *cl)
1641 struct cache_set *c = container_of(cl, struct cache_set, caching);
1642 struct cached_dev *dc;
1643 struct bcache_device *d;
1646 mutex_lock(&bch_register_lock);
1648 for (i = 0; i < c->devices_max_used; i++) {
1653 if (!UUID_FLASH_ONLY(&c->uuids[i]) &&
1654 test_bit(CACHE_SET_UNREGISTERING, &c->flags)) {
1655 dc = container_of(d, struct cached_dev, disk);
1656 bch_cached_dev_detach(dc);
1657 if (test_bit(CACHE_SET_IO_DISABLE, &c->flags))
1658 conditional_stop_bcache_device(c, d, dc);
1660 bcache_device_stop(d);
1664 mutex_unlock(&bch_register_lock);
1666 continue_at(cl, cache_set_flush, system_wq);
1669 void bch_cache_set_stop(struct cache_set *c)
1671 if (!test_and_set_bit(CACHE_SET_STOPPING, &c->flags))
1672 /* closure_fn set to __cache_set_unregister() */
1673 closure_queue(&c->caching);
1676 void bch_cache_set_unregister(struct cache_set *c)
1678 set_bit(CACHE_SET_UNREGISTERING, &c->flags);
1679 bch_cache_set_stop(c);
1682 #define alloc_bucket_pages(gfp, c) \
1683 ((void *) __get_free_pages(__GFP_ZERO|gfp, ilog2(bucket_pages(c))))
1685 struct cache_set *bch_cache_set_alloc(struct cache_sb *sb)
1688 struct cache_set *c = kzalloc(sizeof(struct cache_set), GFP_KERNEL);
1693 __module_get(THIS_MODULE);
1694 closure_init(&c->cl, NULL);
1695 set_closure_fn(&c->cl, cache_set_free, system_wq);
1697 closure_init(&c->caching, &c->cl);
1698 set_closure_fn(&c->caching, __cache_set_unregister, system_wq);
1700 /* Maybe create continue_at_noreturn() and use it here? */
1701 closure_set_stopped(&c->cl);
1702 closure_put(&c->cl);
1704 kobject_init(&c->kobj, &bch_cache_set_ktype);
1705 kobject_init(&c->internal, &bch_cache_set_internal_ktype);
1707 bch_cache_accounting_init(&c->accounting, &c->cl);
1709 memcpy(c->sb.set_uuid, sb->set_uuid, 16);
1710 c->sb.block_size = sb->block_size;
1711 c->sb.bucket_size = sb->bucket_size;
1712 c->sb.nr_in_set = sb->nr_in_set;
1713 c->sb.last_mount = sb->last_mount;
1714 c->bucket_bits = ilog2(sb->bucket_size);
1715 c->block_bits = ilog2(sb->block_size);
1716 c->nr_uuids = bucket_bytes(c) / sizeof(struct uuid_entry);
1717 c->devices_max_used = 0;
1718 atomic_set(&c->attached_dev_nr, 0);
1719 c->btree_pages = bucket_pages(c);
1720 if (c->btree_pages > BTREE_MAX_PAGES)
1721 c->btree_pages = max_t(int, c->btree_pages / 4,
1724 sema_init(&c->sb_write_mutex, 1);
1725 mutex_init(&c->bucket_lock);
1726 init_waitqueue_head(&c->btree_cache_wait);
1727 init_waitqueue_head(&c->bucket_wait);
1728 init_waitqueue_head(&c->gc_wait);
1729 sema_init(&c->uuid_write_mutex, 1);
1731 spin_lock_init(&c->btree_gc_time.lock);
1732 spin_lock_init(&c->btree_split_time.lock);
1733 spin_lock_init(&c->btree_read_time.lock);
1735 bch_moving_init_cache_set(c);
1737 INIT_LIST_HEAD(&c->list);
1738 INIT_LIST_HEAD(&c->cached_devs);
1739 INIT_LIST_HEAD(&c->btree_cache);
1740 INIT_LIST_HEAD(&c->btree_cache_freeable);
1741 INIT_LIST_HEAD(&c->btree_cache_freed);
1742 INIT_LIST_HEAD(&c->data_buckets);
1744 iter_size = (sb->bucket_size / sb->block_size + 1) *
1745 sizeof(struct btree_iter_set);
1747 if (!(c->devices = kcalloc(c->nr_uuids, sizeof(void *), GFP_KERNEL)) ||
1748 mempool_init_slab_pool(&c->search, 32, bch_search_cache) ||
1749 mempool_init_kmalloc_pool(&c->bio_meta, 2,
1750 sizeof(struct bbio) + sizeof(struct bio_vec) *
1752 mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) ||
1753 bioset_init(&c->bio_split, 4, offsetof(struct bbio, bio),
1754 BIOSET_NEED_BVECS|BIOSET_NEED_RESCUER) ||
1755 !(c->uuids = alloc_bucket_pages(GFP_KERNEL, c)) ||
1756 !(c->moving_gc_wq = alloc_workqueue("bcache_gc",
1757 WQ_MEM_RECLAIM, 0)) ||
1758 bch_journal_alloc(c) ||
1759 bch_btree_cache_alloc(c) ||
1760 bch_open_buckets_alloc(c) ||
1761 bch_bset_sort_state_init(&c->sort, ilog2(c->btree_pages)))
1764 c->congested_read_threshold_us = 2000;
1765 c->congested_write_threshold_us = 20000;
1766 c->error_limit = DEFAULT_IO_ERROR_LIMIT;
1767 WARN_ON(test_and_clear_bit(CACHE_SET_IO_DISABLE, &c->flags));
1771 bch_cache_set_unregister(c);
1775 static int run_cache_set(struct cache_set *c)
1777 const char *err = "cannot allocate memory";
1778 struct cached_dev *dc, *t;
1783 struct journal_replay *l;
1785 closure_init_stack(&cl);
1787 for_each_cache(ca, c, i)
1788 c->nbuckets += ca->sb.nbuckets;
1791 if (CACHE_SYNC(&c->sb)) {
1795 err = "cannot allocate memory for journal";
1796 if (bch_journal_read(c, &journal))
1799 pr_debug("btree_journal_read() done");
1801 err = "no journal entries found";
1802 if (list_empty(&journal))
1805 j = &list_entry(journal.prev, struct journal_replay, list)->j;
1807 err = "IO error reading priorities";
1808 for_each_cache(ca, c, i)
1809 prio_read(ca, j->prio_bucket[ca->sb.nr_this_dev]);
1812 * If prio_read() fails it'll call cache_set_error and we'll
1813 * tear everything down right away, but if we perhaps checked
1814 * sooner we could avoid journal replay.
1819 err = "bad btree root";
1820 if (__bch_btree_ptr_invalid(c, k))
1823 err = "error reading btree root";
1824 c->root = bch_btree_node_get(c, NULL, k,
1827 if (IS_ERR_OR_NULL(c->root))
1830 list_del_init(&c->root->list);
1831 rw_unlock(true, c->root);
1833 err = uuid_read(c, j, &cl);
1837 err = "error in recovery";
1838 if (bch_btree_check(c))
1841 bch_journal_mark(c, &journal);
1842 bch_initial_gc_finish(c);
1843 pr_debug("btree_check() done");
1846 * bcache_journal_next() can't happen sooner, or
1847 * btree_gc_finish() will give spurious errors about last_gc >
1848 * gc_gen - this is a hack but oh well.
1850 bch_journal_next(&c->journal);
1852 err = "error starting allocator thread";
1853 for_each_cache(ca, c, i)
1854 if (bch_cache_allocator_start(ca))
1858 * First place it's safe to allocate: btree_check() and
1859 * btree_gc_finish() have to run before we have buckets to
1860 * allocate, and bch_bucket_alloc_set() might cause a journal
1861 * entry to be written so bcache_journal_next() has to be called
1864 * If the uuids were in the old format we have to rewrite them
1865 * before the next journal entry is written:
1867 if (j->version < BCACHE_JSET_VERSION_UUID)
1870 err = "bcache: replay journal failed";
1871 if (bch_journal_replay(c, &journal))
1874 pr_notice("invalidating existing data");
1876 for_each_cache(ca, c, i) {
1879 ca->sb.keys = clamp_t(int, ca->sb.nbuckets >> 7,
1880 2, SB_JOURNAL_BUCKETS);
1882 for (j = 0; j < ca->sb.keys; j++)
1883 ca->sb.d[j] = ca->sb.first_bucket + j;
1886 bch_initial_gc_finish(c);
1888 err = "error starting allocator thread";
1889 for_each_cache(ca, c, i)
1890 if (bch_cache_allocator_start(ca))
1893 mutex_lock(&c->bucket_lock);
1894 for_each_cache(ca, c, i)
1896 mutex_unlock(&c->bucket_lock);
1898 err = "cannot allocate new UUID bucket";
1899 if (__uuid_write(c))
1902 err = "cannot allocate new btree root";
1903 c->root = __bch_btree_node_alloc(c, NULL, 0, true, NULL);
1904 if (IS_ERR_OR_NULL(c->root))
1907 mutex_lock(&c->root->write_lock);
1908 bkey_copy_key(&c->root->key, &MAX_KEY);
1909 bch_btree_node_write(c->root, &cl);
1910 mutex_unlock(&c->root->write_lock);
1912 bch_btree_set_root(c->root);
1913 rw_unlock(true, c->root);
1916 * We don't want to write the first journal entry until
1917 * everything is set up - fortunately journal entries won't be
1918 * written until the SET_CACHE_SYNC() here:
1920 SET_CACHE_SYNC(&c->sb, true);
1922 bch_journal_next(&c->journal);
1923 bch_journal_meta(c, &cl);
1926 err = "error starting gc thread";
1927 if (bch_gc_thread_start(c))
1931 c->sb.last_mount = (u32)ktime_get_real_seconds();
1932 bcache_write_super(c);
1934 list_for_each_entry_safe(dc, t, &uncached_devices, list)
1935 bch_cached_dev_attach(dc, c, NULL);
1939 set_bit(CACHE_SET_RUNNING, &c->flags);
1942 while (!list_empty(&journal)) {
1943 l = list_first_entry(&journal, struct journal_replay, list);
1949 /* XXX: test this, it's broken */
1950 bch_cache_set_error(c, "%s", err);
1955 static bool can_attach_cache(struct cache *ca, struct cache_set *c)
1957 return ca->sb.block_size == c->sb.block_size &&
1958 ca->sb.bucket_size == c->sb.bucket_size &&
1959 ca->sb.nr_in_set == c->sb.nr_in_set;
1962 static const char *register_cache_set(struct cache *ca)
1965 const char *err = "cannot allocate memory";
1966 struct cache_set *c;
1968 list_for_each_entry(c, &bch_cache_sets, list)
1969 if (!memcmp(c->sb.set_uuid, ca->sb.set_uuid, 16)) {
1970 if (c->cache[ca->sb.nr_this_dev])
1971 return "duplicate cache set member";
1973 if (!can_attach_cache(ca, c))
1974 return "cache sb does not match set";
1976 if (!CACHE_SYNC(&ca->sb))
1977 SET_CACHE_SYNC(&c->sb, false);
1982 c = bch_cache_set_alloc(&ca->sb);
1986 err = "error creating kobject";
1987 if (kobject_add(&c->kobj, bcache_kobj, "%pU", c->sb.set_uuid) ||
1988 kobject_add(&c->internal, &c->kobj, "internal"))
1991 if (bch_cache_accounting_add_kobjs(&c->accounting, &c->kobj))
1994 bch_debug_init_cache_set(c);
1996 list_add(&c->list, &bch_cache_sets);
1998 sprintf(buf, "cache%i", ca->sb.nr_this_dev);
1999 if (sysfs_create_link(&ca->kobj, &c->kobj, "set") ||
2000 sysfs_create_link(&c->kobj, &ca->kobj, buf))
2003 if (ca->sb.seq > c->sb.seq) {
2004 c->sb.version = ca->sb.version;
2005 memcpy(c->sb.set_uuid, ca->sb.set_uuid, 16);
2006 c->sb.flags = ca->sb.flags;
2007 c->sb.seq = ca->sb.seq;
2008 pr_debug("set version = %llu", c->sb.version);
2011 kobject_get(&ca->kobj);
2013 ca->set->cache[ca->sb.nr_this_dev] = ca;
2014 c->cache_by_alloc[c->caches_loaded++] = ca;
2016 if (c->caches_loaded == c->sb.nr_in_set) {
2017 err = "failed to run cache set";
2018 if (run_cache_set(c) < 0)
2024 bch_cache_set_unregister(c);
2030 /* When ca->kobj released */
2031 void bch_cache_release(struct kobject *kobj)
2033 struct cache *ca = container_of(kobj, struct cache, kobj);
2037 BUG_ON(ca->set->cache[ca->sb.nr_this_dev] != ca);
2038 ca->set->cache[ca->sb.nr_this_dev] = NULL;
2041 free_pages((unsigned long) ca->disk_buckets, ilog2(bucket_pages(ca)));
2042 kfree(ca->prio_buckets);
2045 free_heap(&ca->heap);
2046 free_fifo(&ca->free_inc);
2048 for (i = 0; i < RESERVE_NR; i++)
2049 free_fifo(&ca->free[i]);
2051 if (ca->sb_bio.bi_inline_vecs[0].bv_page)
2052 put_page(bio_first_page_all(&ca->sb_bio));
2054 if (!IS_ERR_OR_NULL(ca->bdev))
2055 blkdev_put(ca->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2058 module_put(THIS_MODULE);
2061 static int cache_alloc(struct cache *ca)
2064 size_t btree_buckets;
2067 const char *err = NULL;
2069 __module_get(THIS_MODULE);
2070 kobject_init(&ca->kobj, &bch_cache_ktype);
2072 bio_init(&ca->journal.bio, ca->journal.bio.bi_inline_vecs, 8);
2075 * when ca->sb.njournal_buckets is not zero, journal exists,
2076 * and in bch_journal_replay(), tree node may split,
2077 * so bucket of RESERVE_BTREE type is needed,
2078 * the worst situation is all journal buckets are valid journal,
2079 * and all the keys need to replay,
2080 * so the number of RESERVE_BTREE type buckets should be as much
2081 * as journal buckets
2083 btree_buckets = ca->sb.njournal_buckets ?: 8;
2084 free = roundup_pow_of_two(ca->sb.nbuckets) >> 10;
2087 err = "ca->sb.nbuckets is too small";
2091 if (!init_fifo(&ca->free[RESERVE_BTREE], btree_buckets,
2093 err = "ca->free[RESERVE_BTREE] alloc failed";
2094 goto err_btree_alloc;
2097 if (!init_fifo_exact(&ca->free[RESERVE_PRIO], prio_buckets(ca),
2099 err = "ca->free[RESERVE_PRIO] alloc failed";
2100 goto err_prio_alloc;
2103 if (!init_fifo(&ca->free[RESERVE_MOVINGGC], free, GFP_KERNEL)) {
2104 err = "ca->free[RESERVE_MOVINGGC] alloc failed";
2105 goto err_movinggc_alloc;
2108 if (!init_fifo(&ca->free[RESERVE_NONE], free, GFP_KERNEL)) {
2109 err = "ca->free[RESERVE_NONE] alloc failed";
2110 goto err_none_alloc;
2113 if (!init_fifo(&ca->free_inc, free << 2, GFP_KERNEL)) {
2114 err = "ca->free_inc alloc failed";
2115 goto err_free_inc_alloc;
2118 if (!init_heap(&ca->heap, free << 3, GFP_KERNEL)) {
2119 err = "ca->heap alloc failed";
2120 goto err_heap_alloc;
2123 ca->buckets = vzalloc(array_size(sizeof(struct bucket),
2126 err = "ca->buckets alloc failed";
2127 goto err_buckets_alloc;
2130 ca->prio_buckets = kzalloc(array3_size(sizeof(uint64_t),
2131 prio_buckets(ca), 2),
2133 if (!ca->prio_buckets) {
2134 err = "ca->prio_buckets alloc failed";
2135 goto err_prio_buckets_alloc;
2138 ca->disk_buckets = alloc_bucket_pages(GFP_KERNEL, ca);
2139 if (!ca->disk_buckets) {
2140 err = "ca->disk_buckets alloc failed";
2141 goto err_disk_buckets_alloc;
2144 ca->prio_last_buckets = ca->prio_buckets + prio_buckets(ca);
2146 for_each_bucket(b, ca)
2147 atomic_set(&b->pin, 0);
2150 err_disk_buckets_alloc:
2151 kfree(ca->prio_buckets);
2152 err_prio_buckets_alloc:
2155 free_heap(&ca->heap);
2157 free_fifo(&ca->free_inc);
2159 free_fifo(&ca->free[RESERVE_NONE]);
2161 free_fifo(&ca->free[RESERVE_MOVINGGC]);
2163 free_fifo(&ca->free[RESERVE_PRIO]);
2165 free_fifo(&ca->free[RESERVE_BTREE]);
2168 module_put(THIS_MODULE);
2170 pr_notice("error %s: %s", ca->cache_dev_name, err);
2174 static int register_cache(struct cache_sb *sb, struct page *sb_page,
2175 struct block_device *bdev, struct cache *ca)
2177 const char *err = NULL; /* must be set for any error case */
2180 bdevname(bdev, ca->cache_dev_name);
2181 memcpy(&ca->sb, sb, sizeof(struct cache_sb));
2183 ca->bdev->bd_holder = ca;
2185 bio_init(&ca->sb_bio, ca->sb_bio.bi_inline_vecs, 1);
2186 bio_first_bvec_all(&ca->sb_bio)->bv_page = sb_page;
2189 if (blk_queue_discard(bdev_get_queue(bdev)))
2190 ca->discard = CACHE_DISCARD(&ca->sb);
2192 ret = cache_alloc(ca);
2195 * If we failed here, it means ca->kobj is not initialized yet,
2196 * kobject_put() won't be called and there is no chance to
2197 * call blkdev_put() to bdev in bch_cache_release(). So we
2198 * explicitly call blkdev_put() here.
2200 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2202 err = "cache_alloc(): -ENOMEM";
2203 else if (ret == -EPERM)
2204 err = "cache_alloc(): cache device is too small";
2206 err = "cache_alloc(): unknown error";
2210 if (kobject_add(&ca->kobj,
2211 &part_to_dev(bdev->bd_part)->kobj,
2213 err = "error calling kobject_add";
2218 mutex_lock(&bch_register_lock);
2219 err = register_cache_set(ca);
2220 mutex_unlock(&bch_register_lock);
2227 pr_info("registered cache device %s", ca->cache_dev_name);
2230 kobject_put(&ca->kobj);
2234 pr_notice("error %s: %s", ca->cache_dev_name, err);
2239 /* Global interfaces/init */
2241 static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
2242 const char *buffer, size_t size);
2244 kobj_attribute_write(register, register_bcache);
2245 kobj_attribute_write(register_quiet, register_bcache);
2247 static bool bch_is_open_backing(struct block_device *bdev)
2249 struct cache_set *c, *tc;
2250 struct cached_dev *dc, *t;
2252 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
2253 list_for_each_entry_safe(dc, t, &c->cached_devs, list)
2254 if (dc->bdev == bdev)
2256 list_for_each_entry_safe(dc, t, &uncached_devices, list)
2257 if (dc->bdev == bdev)
2262 static bool bch_is_open_cache(struct block_device *bdev)
2264 struct cache_set *c, *tc;
2268 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
2269 for_each_cache(ca, c, i)
2270 if (ca->bdev == bdev)
2275 static bool bch_is_open(struct block_device *bdev)
2277 return bch_is_open_cache(bdev) || bch_is_open_backing(bdev);
2280 static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
2281 const char *buffer, size_t size)
2283 ssize_t ret = -EINVAL;
2284 const char *err = "cannot allocate memory";
2286 struct cache_sb *sb = NULL;
2287 struct block_device *bdev = NULL;
2288 struct page *sb_page = NULL;
2290 if (!try_module_get(THIS_MODULE))
2293 path = kstrndup(buffer, size, GFP_KERNEL);
2297 sb = kmalloc(sizeof(struct cache_sb), GFP_KERNEL);
2301 err = "failed to open device";
2302 bdev = blkdev_get_by_path(strim(path),
2303 FMODE_READ|FMODE_WRITE|FMODE_EXCL,
2306 if (bdev == ERR_PTR(-EBUSY)) {
2307 bdev = lookup_bdev(strim(path));
2308 mutex_lock(&bch_register_lock);
2309 if (!IS_ERR(bdev) && bch_is_open(bdev))
2310 err = "device already registered";
2312 err = "device busy";
2313 mutex_unlock(&bch_register_lock);
2316 if (attr == &ksysfs_register_quiet)
2322 err = "failed to set blocksize";
2323 if (set_blocksize(bdev, 4096))
2326 err = read_super(sb, bdev, &sb_page);
2330 err = "failed to register device";
2331 if (SB_IS_BDEV(sb)) {
2332 struct cached_dev *dc = kzalloc(sizeof(*dc), GFP_KERNEL);
2337 mutex_lock(&bch_register_lock);
2338 ret = register_bdev(sb, sb_page, bdev, dc);
2339 mutex_unlock(&bch_register_lock);
2340 /* blkdev_put() will be called in cached_dev_free() */
2344 struct cache *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
2349 /* blkdev_put() will be called in bch_cache_release() */
2350 if (register_cache(sb, sb_page, bdev, ca) != 0)
2360 module_put(THIS_MODULE);
2364 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2366 pr_info("error %s: %s", path, err);
2370 static int bcache_reboot(struct notifier_block *n, unsigned long code, void *x)
2372 if (code == SYS_DOWN ||
2374 code == SYS_POWER_OFF) {
2376 unsigned long start = jiffies;
2377 bool stopped = false;
2379 struct cache_set *c, *tc;
2380 struct cached_dev *dc, *tdc;
2382 mutex_lock(&bch_register_lock);
2384 if (list_empty(&bch_cache_sets) &&
2385 list_empty(&uncached_devices))
2388 pr_info("Stopping all devices:");
2390 list_for_each_entry_safe(c, tc, &bch_cache_sets, list)
2391 bch_cache_set_stop(c);
2393 list_for_each_entry_safe(dc, tdc, &uncached_devices, list)
2394 bcache_device_stop(&dc->disk);
2396 mutex_unlock(&bch_register_lock);
2399 * Give an early chance for other kthreads and
2400 * kworkers to stop themselves
2404 /* What's a condition variable? */
2406 long timeout = start + 10 * HZ - jiffies;
2408 mutex_lock(&bch_register_lock);
2409 stopped = list_empty(&bch_cache_sets) &&
2410 list_empty(&uncached_devices);
2412 if (timeout < 0 || stopped)
2415 prepare_to_wait(&unregister_wait, &wait,
2416 TASK_UNINTERRUPTIBLE);
2418 mutex_unlock(&bch_register_lock);
2419 schedule_timeout(timeout);
2422 finish_wait(&unregister_wait, &wait);
2425 pr_info("All devices stopped");
2427 pr_notice("Timeout waiting for devices to be closed");
2429 mutex_unlock(&bch_register_lock);
2435 static struct notifier_block reboot = {
2436 .notifier_call = bcache_reboot,
2437 .priority = INT_MAX, /* before any real devices */
2440 static void bcache_exit(void)
2445 kobject_put(bcache_kobj);
2447 destroy_workqueue(bcache_wq);
2449 destroy_workqueue(bch_journal_wq);
2452 unregister_blkdev(bcache_major, "bcache");
2453 unregister_reboot_notifier(&reboot);
2454 mutex_destroy(&bch_register_lock);
2457 /* Check and fixup module parameters */
2458 static void check_module_parameters(void)
2460 if (bch_cutoff_writeback_sync == 0)
2461 bch_cutoff_writeback_sync = CUTOFF_WRITEBACK_SYNC;
2462 else if (bch_cutoff_writeback_sync > CUTOFF_WRITEBACK_SYNC_MAX) {
2463 pr_warn("set bch_cutoff_writeback_sync (%u) to max value %u",
2464 bch_cutoff_writeback_sync, CUTOFF_WRITEBACK_SYNC_MAX);
2465 bch_cutoff_writeback_sync = CUTOFF_WRITEBACK_SYNC_MAX;
2468 if (bch_cutoff_writeback == 0)
2469 bch_cutoff_writeback = CUTOFF_WRITEBACK;
2470 else if (bch_cutoff_writeback > CUTOFF_WRITEBACK_MAX) {
2471 pr_warn("set bch_cutoff_writeback (%u) to max value %u",
2472 bch_cutoff_writeback, CUTOFF_WRITEBACK_MAX);
2473 bch_cutoff_writeback = CUTOFF_WRITEBACK_MAX;
2476 if (bch_cutoff_writeback > bch_cutoff_writeback_sync) {
2477 pr_warn("set bch_cutoff_writeback (%u) to %u",
2478 bch_cutoff_writeback, bch_cutoff_writeback_sync);
2479 bch_cutoff_writeback = bch_cutoff_writeback_sync;
2483 static int __init bcache_init(void)
2485 static const struct attribute *files[] = {
2486 &ksysfs_register.attr,
2487 &ksysfs_register_quiet.attr,
2491 check_module_parameters();
2493 mutex_init(&bch_register_lock);
2494 init_waitqueue_head(&unregister_wait);
2495 register_reboot_notifier(&reboot);
2497 bcache_major = register_blkdev(0, "bcache");
2498 if (bcache_major < 0) {
2499 unregister_reboot_notifier(&reboot);
2500 mutex_destroy(&bch_register_lock);
2501 return bcache_major;
2504 bcache_wq = alloc_workqueue("bcache", WQ_MEM_RECLAIM, 0);
2508 bch_journal_wq = alloc_workqueue("bch_journal", WQ_MEM_RECLAIM, 0);
2509 if (!bch_journal_wq)
2512 bcache_kobj = kobject_create_and_add("bcache", fs_kobj);
2516 if (bch_request_init() ||
2517 sysfs_create_files(bcache_kobj, files))
2521 closure_debug_init();
2532 module_exit(bcache_exit);
2533 module_init(bcache_init);
2535 module_param(bch_cutoff_writeback, uint, 0);
2536 MODULE_PARM_DESC(bch_cutoff_writeback, "threshold to cutoff writeback");
2538 module_param(bch_cutoff_writeback_sync, uint, 0);
2539 MODULE_PARM_DESC(bch_cutoff_writeback_sync, "hard threshold to cutoff writeback");
2541 MODULE_DESCRIPTION("Bcache: a Linux block layer cache");
2542 MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
2543 MODULE_LICENSE("GPL");