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