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