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