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