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; | |
ab9e1400 | 828 | char buf[SB_LABEL_SIZE + 1]; |
a25c32be GP |
829 | char *env[] = { |
830 | "DRIVER=bcache", | |
831 | kasprintf(GFP_KERNEL, "CACHED_UUID=%pU", dc->sb.uuid), | |
ab9e1400 GP |
832 | NULL, |
833 | NULL, | |
a25c32be | 834 | }; |
cafe5635 | 835 | |
ab9e1400 GP |
836 | memcpy(buf, dc->sb.label, SB_LABEL_SIZE); |
837 | buf[SB_LABEL_SIZE] = '\0'; | |
838 | env[2] = kasprintf(GFP_KERNEL, "CACHED_LABEL=%s", buf); | |
839 | ||
cafe5635 KO |
840 | if (atomic_xchg(&dc->running, 1)) |
841 | return; | |
842 | ||
843 | if (!d->c && | |
844 | BDEV_STATE(&dc->sb) != BDEV_STATE_NONE) { | |
845 | struct closure cl; | |
846 | closure_init_stack(&cl); | |
847 | ||
848 | SET_BDEV_STATE(&dc->sb, BDEV_STATE_STALE); | |
849 | bch_write_bdev_super(dc, &cl); | |
850 | closure_sync(&cl); | |
851 | } | |
852 | ||
853 | add_disk(d->disk); | |
ee668506 | 854 | bd_link_disk_holder(dc->bdev, dc->disk.disk); |
a25c32be GP |
855 | /* won't show up in the uevent file, use udevadm monitor -e instead |
856 | * only class / kset properties are persistent */ | |
cafe5635 | 857 | kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env); |
a25c32be | 858 | kfree(env[1]); |
ab9e1400 | 859 | kfree(env[2]); |
a25c32be | 860 | |
cafe5635 KO |
861 | if (sysfs_create_link(&d->kobj, &disk_to_dev(d->disk)->kobj, "dev") || |
862 | sysfs_create_link(&disk_to_dev(d->disk)->kobj, &d->kobj, "bcache")) | |
863 | pr_debug("error creating sysfs link"); | |
864 | } | |
865 | ||
866 | static void cached_dev_detach_finish(struct work_struct *w) | |
867 | { | |
868 | struct cached_dev *dc = container_of(w, struct cached_dev, detach); | |
869 | char buf[BDEVNAME_SIZE]; | |
870 | struct closure cl; | |
871 | closure_init_stack(&cl); | |
872 | ||
873 | BUG_ON(!atomic_read(&dc->disk.detaching)); | |
874 | BUG_ON(atomic_read(&dc->count)); | |
875 | ||
cafe5635 KO |
876 | mutex_lock(&bch_register_lock); |
877 | ||
878 | memset(&dc->sb.set_uuid, 0, 16); | |
879 | SET_BDEV_STATE(&dc->sb, BDEV_STATE_NONE); | |
880 | ||
881 | bch_write_bdev_super(dc, &cl); | |
882 | closure_sync(&cl); | |
883 | ||
884 | bcache_device_detach(&dc->disk); | |
885 | list_move(&dc->list, &uncached_devices); | |
886 | ||
887 | mutex_unlock(&bch_register_lock); | |
888 | ||
889 | pr_info("Caching disabled for %s", bdevname(dc->bdev, buf)); | |
890 | ||
891 | /* Drop ref we took in cached_dev_detach() */ | |
892 | closure_put(&dc->disk.cl); | |
893 | } | |
894 | ||
895 | void bch_cached_dev_detach(struct cached_dev *dc) | |
896 | { | |
897 | lockdep_assert_held(&bch_register_lock); | |
898 | ||
899 | if (atomic_read(&dc->disk.closing)) | |
900 | return; | |
901 | ||
902 | if (atomic_xchg(&dc->disk.detaching, 1)) | |
903 | return; | |
904 | ||
905 | /* | |
906 | * Block the device from being closed and freed until we're finished | |
907 | * detaching | |
908 | */ | |
909 | closure_get(&dc->disk.cl); | |
910 | ||
911 | bch_writeback_queue(dc); | |
912 | cached_dev_put(dc); | |
913 | } | |
914 | ||
915 | int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c) | |
916 | { | |
917 | uint32_t rtime = cpu_to_le32(get_seconds()); | |
918 | struct uuid_entry *u; | |
919 | char buf[BDEVNAME_SIZE]; | |
920 | ||
921 | bdevname(dc->bdev, buf); | |
922 | ||
923 | if (memcmp(dc->sb.set_uuid, c->sb.set_uuid, 16)) | |
924 | return -ENOENT; | |
925 | ||
926 | if (dc->disk.c) { | |
927 | pr_err("Can't attach %s: already attached", buf); | |
928 | return -EINVAL; | |
929 | } | |
930 | ||
931 | if (test_bit(CACHE_SET_STOPPING, &c->flags)) { | |
932 | pr_err("Can't attach %s: shutting down", buf); | |
933 | return -EINVAL; | |
934 | } | |
935 | ||
936 | if (dc->sb.block_size < c->sb.block_size) { | |
937 | /* Will die */ | |
b1a67b0f KO |
938 | pr_err("Couldn't attach %s: block size less than set's block size", |
939 | buf); | |
cafe5635 KO |
940 | return -EINVAL; |
941 | } | |
942 | ||
943 | u = uuid_find(c, dc->sb.uuid); | |
944 | ||
945 | if (u && | |
946 | (BDEV_STATE(&dc->sb) == BDEV_STATE_STALE || | |
947 | BDEV_STATE(&dc->sb) == BDEV_STATE_NONE)) { | |
948 | memcpy(u->uuid, invalid_uuid, 16); | |
949 | u->invalidated = cpu_to_le32(get_seconds()); | |
950 | u = NULL; | |
951 | } | |
952 | ||
953 | if (!u) { | |
954 | if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) { | |
955 | pr_err("Couldn't find uuid for %s in set", buf); | |
956 | return -ENOENT; | |
957 | } | |
958 | ||
959 | u = uuid_find_empty(c); | |
960 | if (!u) { | |
961 | pr_err("Not caching %s, no room for UUID", buf); | |
962 | return -EINVAL; | |
963 | } | |
964 | } | |
965 | ||
966 | /* Deadlocks since we're called via sysfs... | |
967 | sysfs_remove_file(&dc->kobj, &sysfs_attach); | |
968 | */ | |
969 | ||
169ef1cf | 970 | if (bch_is_zero(u->uuid, 16)) { |
cafe5635 KO |
971 | struct closure cl; |
972 | closure_init_stack(&cl); | |
973 | ||
974 | memcpy(u->uuid, dc->sb.uuid, 16); | |
975 | memcpy(u->label, dc->sb.label, SB_LABEL_SIZE); | |
976 | u->first_reg = u->last_reg = rtime; | |
977 | bch_uuid_write(c); | |
978 | ||
979 | memcpy(dc->sb.set_uuid, c->sb.set_uuid, 16); | |
980 | SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN); | |
981 | ||
982 | bch_write_bdev_super(dc, &cl); | |
983 | closure_sync(&cl); | |
984 | } else { | |
985 | u->last_reg = rtime; | |
986 | bch_uuid_write(c); | |
987 | } | |
988 | ||
989 | bcache_device_attach(&dc->disk, c, u - c->uuids); | |
cafe5635 KO |
990 | list_move(&dc->list, &c->cached_devs); |
991 | calc_cached_dev_sectors(c); | |
992 | ||
993 | smp_wmb(); | |
994 | /* | |
995 | * dc->c must be set before dc->count != 0 - paired with the mb in | |
996 | * cached_dev_get() | |
997 | */ | |
998 | atomic_set(&dc->count, 1); | |
999 | ||
1000 | if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) { | |
444fc0b6 | 1001 | bch_sectors_dirty_init(dc); |
cafe5635 KO |
1002 | atomic_set(&dc->has_dirty, 1); |
1003 | atomic_inc(&dc->count); | |
1004 | bch_writeback_queue(dc); | |
1005 | } | |
1006 | ||
1007 | bch_cached_dev_run(dc); | |
ee668506 | 1008 | bcache_device_link(&dc->disk, c, "bdev"); |
cafe5635 KO |
1009 | |
1010 | pr_info("Caching %s as %s on set %pU", | |
1011 | bdevname(dc->bdev, buf), dc->disk.disk->disk_name, | |
1012 | dc->disk.c->sb.set_uuid); | |
1013 | return 0; | |
1014 | } | |
1015 | ||
1016 | void bch_cached_dev_release(struct kobject *kobj) | |
1017 | { | |
1018 | struct cached_dev *dc = container_of(kobj, struct cached_dev, | |
1019 | disk.kobj); | |
1020 | kfree(dc); | |
1021 | module_put(THIS_MODULE); | |
1022 | } | |
1023 | ||
1024 | static void cached_dev_free(struct closure *cl) | |
1025 | { | |
1026 | struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl); | |
1027 | ||
1028 | cancel_delayed_work_sync(&dc->writeback_rate_update); | |
1029 | ||
1030 | mutex_lock(&bch_register_lock); | |
1031 | ||
f59fce84 KO |
1032 | if (atomic_read(&dc->running)) |
1033 | bd_unlink_disk_holder(dc->bdev, dc->disk.disk); | |
cafe5635 KO |
1034 | bcache_device_free(&dc->disk); |
1035 | list_del(&dc->list); | |
1036 | ||
1037 | mutex_unlock(&bch_register_lock); | |
1038 | ||
1039 | if (!IS_ERR_OR_NULL(dc->bdev)) { | |
f59fce84 KO |
1040 | if (dc->bdev->bd_disk) |
1041 | blk_sync_queue(bdev_get_queue(dc->bdev)); | |
1042 | ||
cafe5635 KO |
1043 | blkdev_put(dc->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
1044 | } | |
1045 | ||
1046 | wake_up(&unregister_wait); | |
1047 | ||
1048 | kobject_put(&dc->disk.kobj); | |
1049 | } | |
1050 | ||
1051 | static void cached_dev_flush(struct closure *cl) | |
1052 | { | |
1053 | struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl); | |
1054 | struct bcache_device *d = &dc->disk; | |
1055 | ||
1056 | bch_cache_accounting_destroy(&dc->accounting); | |
1057 | kobject_del(&d->kobj); | |
1058 | ||
1059 | continue_at(cl, cached_dev_free, system_wq); | |
1060 | } | |
1061 | ||
1062 | static int cached_dev_init(struct cached_dev *dc, unsigned block_size) | |
1063 | { | |
f59fce84 | 1064 | int ret; |
cafe5635 | 1065 | struct io *io; |
f59fce84 | 1066 | struct request_queue *q = bdev_get_queue(dc->bdev); |
cafe5635 KO |
1067 | |
1068 | __module_get(THIS_MODULE); | |
1069 | INIT_LIST_HEAD(&dc->list); | |
f59fce84 KO |
1070 | closure_init(&dc->disk.cl, NULL); |
1071 | set_closure_fn(&dc->disk.cl, cached_dev_flush, system_wq); | |
cafe5635 | 1072 | kobject_init(&dc->disk.kobj, &bch_cached_dev_ktype); |
cafe5635 | 1073 | INIT_WORK(&dc->detach, cached_dev_detach_finish); |
f59fce84 KO |
1074 | closure_init_unlocked(&dc->sb_write); |
1075 | INIT_LIST_HEAD(&dc->io_lru); | |
1076 | spin_lock_init(&dc->io_lock); | |
1077 | bch_cache_accounting_init(&dc->accounting, &dc->disk.cl); | |
cafe5635 KO |
1078 | |
1079 | dc->sequential_merge = true; | |
1080 | dc->sequential_cutoff = 4 << 20; | |
1081 | ||
cafe5635 KO |
1082 | for (io = dc->io; io < dc->io + RECENT_IO; io++) { |
1083 | list_add(&io->lru, &dc->io_lru); | |
1084 | hlist_add_head(&io->hash, dc->io_hash + RECENT_IO); | |
1085 | } | |
1086 | ||
279afbad KO |
1087 | ret = bcache_device_init(&dc->disk, block_size, |
1088 | dc->bdev->bd_part->nr_sects - dc->sb.data_offset); | |
f59fce84 KO |
1089 | if (ret) |
1090 | return ret; | |
1091 | ||
1092 | set_capacity(dc->disk.disk, | |
1093 | dc->bdev->bd_part->nr_sects - dc->sb.data_offset); | |
1094 | ||
1095 | dc->disk.disk->queue->backing_dev_info.ra_pages = | |
1096 | max(dc->disk.disk->queue->backing_dev_info.ra_pages, | |
1097 | q->backing_dev_info.ra_pages); | |
1098 | ||
1099 | bch_cached_dev_request_init(dc); | |
1100 | bch_cached_dev_writeback_init(dc); | |
cafe5635 | 1101 | return 0; |
cafe5635 KO |
1102 | } |
1103 | ||
1104 | /* Cached device - bcache superblock */ | |
1105 | ||
f59fce84 | 1106 | static void register_bdev(struct cache_sb *sb, struct page *sb_page, |
cafe5635 KO |
1107 | struct block_device *bdev, |
1108 | struct cached_dev *dc) | |
1109 | { | |
1110 | char name[BDEVNAME_SIZE]; | |
1111 | const char *err = "cannot allocate memory"; | |
cafe5635 KO |
1112 | struct cache_set *c; |
1113 | ||
cafe5635 | 1114 | memcpy(&dc->sb, sb, sizeof(struct cache_sb)); |
cafe5635 KO |
1115 | dc->bdev = bdev; |
1116 | dc->bdev->bd_holder = dc; | |
1117 | ||
f59fce84 KO |
1118 | bio_init(&dc->sb_bio); |
1119 | dc->sb_bio.bi_max_vecs = 1; | |
1120 | dc->sb_bio.bi_io_vec = dc->sb_bio.bi_inline_vecs; | |
1121 | dc->sb_bio.bi_io_vec[0].bv_page = sb_page; | |
1122 | get_page(sb_page); | |
4f0fd955 | 1123 | |
f59fce84 KO |
1124 | if (cached_dev_init(dc, sb->block_size << 9)) |
1125 | goto err; | |
cafe5635 KO |
1126 | |
1127 | err = "error creating kobject"; | |
1128 | if (kobject_add(&dc->disk.kobj, &part_to_dev(bdev->bd_part)->kobj, | |
1129 | "bcache")) | |
1130 | goto err; | |
1131 | if (bch_cache_accounting_add_kobjs(&dc->accounting, &dc->disk.kobj)) | |
1132 | goto err; | |
1133 | ||
f59fce84 KO |
1134 | pr_info("registered backing device %s", bdevname(bdev, name)); |
1135 | ||
cafe5635 KO |
1136 | list_add(&dc->list, &uncached_devices); |
1137 | list_for_each_entry(c, &bch_cache_sets, list) | |
1138 | bch_cached_dev_attach(dc, c); | |
1139 | ||
1140 | if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE || | |
1141 | BDEV_STATE(&dc->sb) == BDEV_STATE_STALE) | |
1142 | bch_cached_dev_run(dc); | |
1143 | ||
f59fce84 | 1144 | return; |
cafe5635 | 1145 | err: |
cafe5635 | 1146 | pr_notice("error opening %s: %s", bdevname(bdev, name), err); |
f59fce84 | 1147 | bcache_device_stop(&dc->disk); |
cafe5635 KO |
1148 | } |
1149 | ||
1150 | /* Flash only volumes */ | |
1151 | ||
1152 | void bch_flash_dev_release(struct kobject *kobj) | |
1153 | { | |
1154 | struct bcache_device *d = container_of(kobj, struct bcache_device, | |
1155 | kobj); | |
1156 | kfree(d); | |
1157 | } | |
1158 | ||
1159 | static void flash_dev_free(struct closure *cl) | |
1160 | { | |
1161 | struct bcache_device *d = container_of(cl, struct bcache_device, cl); | |
1162 | bcache_device_free(d); | |
1163 | kobject_put(&d->kobj); | |
1164 | } | |
1165 | ||
1166 | static void flash_dev_flush(struct closure *cl) | |
1167 | { | |
1168 | struct bcache_device *d = container_of(cl, struct bcache_device, cl); | |
1169 | ||
ee668506 | 1170 | bcache_device_unlink(d); |
cafe5635 KO |
1171 | kobject_del(&d->kobj); |
1172 | continue_at(cl, flash_dev_free, system_wq); | |
1173 | } | |
1174 | ||
1175 | static int flash_dev_run(struct cache_set *c, struct uuid_entry *u) | |
1176 | { | |
1177 | struct bcache_device *d = kzalloc(sizeof(struct bcache_device), | |
1178 | GFP_KERNEL); | |
1179 | if (!d) | |
1180 | return -ENOMEM; | |
1181 | ||
1182 | closure_init(&d->cl, NULL); | |
1183 | set_closure_fn(&d->cl, flash_dev_flush, system_wq); | |
1184 | ||
1185 | kobject_init(&d->kobj, &bch_flash_dev_ktype); | |
1186 | ||
279afbad | 1187 | if (bcache_device_init(d, block_bytes(c), u->sectors)) |
cafe5635 KO |
1188 | goto err; |
1189 | ||
1190 | bcache_device_attach(d, c, u - c->uuids); | |
cafe5635 KO |
1191 | bch_flash_dev_request_init(d); |
1192 | add_disk(d->disk); | |
1193 | ||
1194 | if (kobject_add(&d->kobj, &disk_to_dev(d->disk)->kobj, "bcache")) | |
1195 | goto err; | |
1196 | ||
1197 | bcache_device_link(d, c, "volume"); | |
1198 | ||
1199 | return 0; | |
1200 | err: | |
1201 | kobject_put(&d->kobj); | |
1202 | return -ENOMEM; | |
1203 | } | |
1204 | ||
1205 | static int flash_devs_run(struct cache_set *c) | |
1206 | { | |
1207 | int ret = 0; | |
1208 | struct uuid_entry *u; | |
1209 | ||
1210 | for (u = c->uuids; | |
1211 | u < c->uuids + c->nr_uuids && !ret; | |
1212 | u++) | |
1213 | if (UUID_FLASH_ONLY(u)) | |
1214 | ret = flash_dev_run(c, u); | |
1215 | ||
1216 | return ret; | |
1217 | } | |
1218 | ||
1219 | int bch_flash_dev_create(struct cache_set *c, uint64_t size) | |
1220 | { | |
1221 | struct uuid_entry *u; | |
1222 | ||
1223 | if (test_bit(CACHE_SET_STOPPING, &c->flags)) | |
1224 | return -EINTR; | |
1225 | ||
1226 | u = uuid_find_empty(c); | |
1227 | if (!u) { | |
1228 | pr_err("Can't create volume, no room for UUID"); | |
1229 | return -EINVAL; | |
1230 | } | |
1231 | ||
1232 | get_random_bytes(u->uuid, 16); | |
1233 | memset(u->label, 0, 32); | |
1234 | u->first_reg = u->last_reg = cpu_to_le32(get_seconds()); | |
1235 | ||
1236 | SET_UUID_FLASH_ONLY(u, 1); | |
1237 | u->sectors = size >> 9; | |
1238 | ||
1239 | bch_uuid_write(c); | |
1240 | ||
1241 | return flash_dev_run(c, u); | |
1242 | } | |
1243 | ||
1244 | /* Cache set */ | |
1245 | ||
1246 | __printf(2, 3) | |
1247 | bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...) | |
1248 | { | |
1249 | va_list args; | |
1250 | ||
1251 | if (test_bit(CACHE_SET_STOPPING, &c->flags)) | |
1252 | return false; | |
1253 | ||
1254 | /* XXX: we can be called from atomic context | |
1255 | acquire_console_sem(); | |
1256 | */ | |
1257 | ||
1258 | printk(KERN_ERR "bcache: error on %pU: ", c->sb.set_uuid); | |
1259 | ||
1260 | va_start(args, fmt); | |
1261 | vprintk(fmt, args); | |
1262 | va_end(args); | |
1263 | ||
1264 | printk(", disabling caching\n"); | |
1265 | ||
1266 | bch_cache_set_unregister(c); | |
1267 | return true; | |
1268 | } | |
1269 | ||
1270 | void bch_cache_set_release(struct kobject *kobj) | |
1271 | { | |
1272 | struct cache_set *c = container_of(kobj, struct cache_set, kobj); | |
1273 | kfree(c); | |
1274 | module_put(THIS_MODULE); | |
1275 | } | |
1276 | ||
1277 | static void cache_set_free(struct closure *cl) | |
1278 | { | |
1279 | struct cache_set *c = container_of(cl, struct cache_set, cl); | |
1280 | struct cache *ca; | |
1281 | unsigned i; | |
1282 | ||
1283 | if (!IS_ERR_OR_NULL(c->debug)) | |
1284 | debugfs_remove(c->debug); | |
1285 | ||
1286 | bch_open_buckets_free(c); | |
1287 | bch_btree_cache_free(c); | |
1288 | bch_journal_free(c); | |
1289 | ||
1290 | for_each_cache(ca, c, i) | |
1291 | if (ca) | |
1292 | kobject_put(&ca->kobj); | |
1293 | ||
1294 | free_pages((unsigned long) c->uuids, ilog2(bucket_pages(c))); | |
1295 | free_pages((unsigned long) c->sort, ilog2(bucket_pages(c))); | |
1296 | ||
cafe5635 KO |
1297 | if (c->bio_split) |
1298 | bioset_free(c->bio_split); | |
57943511 KO |
1299 | if (c->fill_iter) |
1300 | mempool_destroy(c->fill_iter); | |
cafe5635 KO |
1301 | if (c->bio_meta) |
1302 | mempool_destroy(c->bio_meta); | |
1303 | if (c->search) | |
1304 | mempool_destroy(c->search); | |
1305 | kfree(c->devices); | |
1306 | ||
1307 | mutex_lock(&bch_register_lock); | |
1308 | list_del(&c->list); | |
1309 | mutex_unlock(&bch_register_lock); | |
1310 | ||
1311 | pr_info("Cache set %pU unregistered", c->sb.set_uuid); | |
1312 | wake_up(&unregister_wait); | |
1313 | ||
1314 | closure_debug_destroy(&c->cl); | |
1315 | kobject_put(&c->kobj); | |
1316 | } | |
1317 | ||
1318 | static void cache_set_flush(struct closure *cl) | |
1319 | { | |
1320 | struct cache_set *c = container_of(cl, struct cache_set, caching); | |
1321 | struct btree *b; | |
1322 | ||
1323 | /* Shut down allocator threads */ | |
1324 | set_bit(CACHE_SET_STOPPING_2, &c->flags); | |
119ba0f8 | 1325 | wake_up_allocators(c); |
cafe5635 KO |
1326 | |
1327 | bch_cache_accounting_destroy(&c->accounting); | |
1328 | ||
1329 | kobject_put(&c->internal); | |
1330 | kobject_del(&c->kobj); | |
1331 | ||
1332 | if (!IS_ERR_OR_NULL(c->root)) | |
1333 | list_add(&c->root->list, &c->btree_cache); | |
1334 | ||
1335 | /* Should skip this if we're unregistering because of an error */ | |
1336 | list_for_each_entry(b, &c->btree_cache, list) | |
1337 | if (btree_node_dirty(b)) | |
57943511 | 1338 | bch_btree_node_write(b, NULL); |
cafe5635 KO |
1339 | |
1340 | closure_return(cl); | |
1341 | } | |
1342 | ||
1343 | static void __cache_set_unregister(struct closure *cl) | |
1344 | { | |
1345 | struct cache_set *c = container_of(cl, struct cache_set, caching); | |
1346 | struct cached_dev *dc, *t; | |
1347 | size_t i; | |
1348 | ||
1349 | mutex_lock(&bch_register_lock); | |
1350 | ||
1351 | if (test_bit(CACHE_SET_UNREGISTERING, &c->flags)) | |
1352 | list_for_each_entry_safe(dc, t, &c->cached_devs, list) | |
1353 | bch_cached_dev_detach(dc); | |
1354 | ||
1355 | for (i = 0; i < c->nr_uuids; i++) | |
1356 | if (c->devices[i] && UUID_FLASH_ONLY(&c->uuids[i])) | |
1357 | bcache_device_stop(c->devices[i]); | |
1358 | ||
1359 | mutex_unlock(&bch_register_lock); | |
1360 | ||
1361 | continue_at(cl, cache_set_flush, system_wq); | |
1362 | } | |
1363 | ||
1364 | void bch_cache_set_stop(struct cache_set *c) | |
1365 | { | |
1366 | if (!test_and_set_bit(CACHE_SET_STOPPING, &c->flags)) | |
1367 | closure_queue(&c->caching); | |
1368 | } | |
1369 | ||
1370 | void bch_cache_set_unregister(struct cache_set *c) | |
1371 | { | |
1372 | set_bit(CACHE_SET_UNREGISTERING, &c->flags); | |
1373 | bch_cache_set_stop(c); | |
1374 | } | |
1375 | ||
1376 | #define alloc_bucket_pages(gfp, c) \ | |
1377 | ((void *) __get_free_pages(__GFP_ZERO|gfp, ilog2(bucket_pages(c)))) | |
1378 | ||
1379 | struct cache_set *bch_cache_set_alloc(struct cache_sb *sb) | |
1380 | { | |
1381 | int iter_size; | |
1382 | struct cache_set *c = kzalloc(sizeof(struct cache_set), GFP_KERNEL); | |
1383 | if (!c) | |
1384 | return NULL; | |
1385 | ||
1386 | __module_get(THIS_MODULE); | |
1387 | closure_init(&c->cl, NULL); | |
1388 | set_closure_fn(&c->cl, cache_set_free, system_wq); | |
1389 | ||
1390 | closure_init(&c->caching, &c->cl); | |
1391 | set_closure_fn(&c->caching, __cache_set_unregister, system_wq); | |
1392 | ||
1393 | /* Maybe create continue_at_noreturn() and use it here? */ | |
1394 | closure_set_stopped(&c->cl); | |
1395 | closure_put(&c->cl); | |
1396 | ||
1397 | kobject_init(&c->kobj, &bch_cache_set_ktype); | |
1398 | kobject_init(&c->internal, &bch_cache_set_internal_ktype); | |
1399 | ||
1400 | bch_cache_accounting_init(&c->accounting, &c->cl); | |
1401 | ||
1402 | memcpy(c->sb.set_uuid, sb->set_uuid, 16); | |
1403 | c->sb.block_size = sb->block_size; | |
1404 | c->sb.bucket_size = sb->bucket_size; | |
1405 | c->sb.nr_in_set = sb->nr_in_set; | |
1406 | c->sb.last_mount = sb->last_mount; | |
1407 | c->bucket_bits = ilog2(sb->bucket_size); | |
1408 | c->block_bits = ilog2(sb->block_size); | |
1409 | c->nr_uuids = bucket_bytes(c) / sizeof(struct uuid_entry); | |
1410 | ||
1411 | c->btree_pages = c->sb.bucket_size / PAGE_SECTORS; | |
1412 | if (c->btree_pages > BTREE_MAX_PAGES) | |
1413 | c->btree_pages = max_t(int, c->btree_pages / 4, | |
1414 | BTREE_MAX_PAGES); | |
1415 | ||
6ded34d1 KO |
1416 | c->sort_crit_factor = int_sqrt(c->btree_pages); |
1417 | ||
cafe5635 | 1418 | mutex_init(&c->bucket_lock); |
cafe5635 KO |
1419 | mutex_init(&c->sort_lock); |
1420 | spin_lock_init(&c->sort_time_lock); | |
1421 | closure_init_unlocked(&c->sb_write); | |
1422 | closure_init_unlocked(&c->uuid_write); | |
1423 | spin_lock_init(&c->btree_read_time_lock); | |
1424 | bch_moving_init_cache_set(c); | |
1425 | ||
1426 | INIT_LIST_HEAD(&c->list); | |
1427 | INIT_LIST_HEAD(&c->cached_devs); | |
1428 | INIT_LIST_HEAD(&c->btree_cache); | |
1429 | INIT_LIST_HEAD(&c->btree_cache_freeable); | |
1430 | INIT_LIST_HEAD(&c->btree_cache_freed); | |
1431 | INIT_LIST_HEAD(&c->data_buckets); | |
1432 | ||
1433 | c->search = mempool_create_slab_pool(32, bch_search_cache); | |
1434 | if (!c->search) | |
1435 | goto err; | |
1436 | ||
1437 | iter_size = (sb->bucket_size / sb->block_size + 1) * | |
1438 | sizeof(struct btree_iter_set); | |
1439 | ||
1440 | if (!(c->devices = kzalloc(c->nr_uuids * sizeof(void *), GFP_KERNEL)) || | |
1441 | !(c->bio_meta = mempool_create_kmalloc_pool(2, | |
1442 | sizeof(struct bbio) + sizeof(struct bio_vec) * | |
1443 | bucket_pages(c))) || | |
57943511 | 1444 | !(c->fill_iter = mempool_create_kmalloc_pool(1, iter_size)) || |
cafe5635 | 1445 | !(c->bio_split = bioset_create(4, offsetof(struct bbio, bio))) || |
cafe5635 KO |
1446 | !(c->sort = alloc_bucket_pages(GFP_KERNEL, c)) || |
1447 | !(c->uuids = alloc_bucket_pages(GFP_KERNEL, c)) || | |
1448 | bch_journal_alloc(c) || | |
1449 | bch_btree_cache_alloc(c) || | |
1450 | bch_open_buckets_alloc(c)) | |
1451 | goto err; | |
1452 | ||
cafe5635 KO |
1453 | c->congested_read_threshold_us = 2000; |
1454 | c->congested_write_threshold_us = 20000; | |
1455 | c->error_limit = 8 << IO_ERROR_SHIFT; | |
1456 | ||
1457 | return c; | |
1458 | err: | |
1459 | bch_cache_set_unregister(c); | |
1460 | return NULL; | |
1461 | } | |
1462 | ||
1463 | static void run_cache_set(struct cache_set *c) | |
1464 | { | |
1465 | const char *err = "cannot allocate memory"; | |
1466 | struct cached_dev *dc, *t; | |
1467 | struct cache *ca; | |
1468 | unsigned i; | |
1469 | ||
1470 | struct btree_op op; | |
1471 | bch_btree_op_init_stack(&op); | |
1472 | op.lock = SHRT_MAX; | |
1473 | ||
1474 | for_each_cache(ca, c, i) | |
1475 | c->nbuckets += ca->sb.nbuckets; | |
1476 | ||
1477 | if (CACHE_SYNC(&c->sb)) { | |
1478 | LIST_HEAD(journal); | |
1479 | struct bkey *k; | |
1480 | struct jset *j; | |
1481 | ||
1482 | err = "cannot allocate memory for journal"; | |
1483 | if (bch_journal_read(c, &journal, &op)) | |
1484 | goto err; | |
1485 | ||
1486 | pr_debug("btree_journal_read() done"); | |
1487 | ||
1488 | err = "no journal entries found"; | |
1489 | if (list_empty(&journal)) | |
1490 | goto err; | |
1491 | ||
1492 | j = &list_entry(journal.prev, struct journal_replay, list)->j; | |
1493 | ||
1494 | err = "IO error reading priorities"; | |
1495 | for_each_cache(ca, c, i) | |
1496 | prio_read(ca, j->prio_bucket[ca->sb.nr_this_dev]); | |
1497 | ||
1498 | /* | |
1499 | * If prio_read() fails it'll call cache_set_error and we'll | |
1500 | * tear everything down right away, but if we perhaps checked | |
1501 | * sooner we could avoid journal replay. | |
1502 | */ | |
1503 | ||
1504 | k = &j->btree_root; | |
1505 | ||
1506 | err = "bad btree root"; | |
1507 | if (__bch_ptr_invalid(c, j->btree_level + 1, k)) | |
1508 | goto err; | |
1509 | ||
1510 | err = "error reading btree root"; | |
1511 | c->root = bch_btree_node_get(c, k, j->btree_level, &op); | |
1512 | if (IS_ERR_OR_NULL(c->root)) | |
1513 | goto err; | |
1514 | ||
1515 | list_del_init(&c->root->list); | |
1516 | rw_unlock(true, c->root); | |
1517 | ||
1518 | err = uuid_read(c, j, &op.cl); | |
1519 | if (err) | |
1520 | goto err; | |
1521 | ||
1522 | err = "error in recovery"; | |
1523 | if (bch_btree_check(c, &op)) | |
1524 | goto err; | |
1525 | ||
1526 | bch_journal_mark(c, &journal); | |
1527 | bch_btree_gc_finish(c); | |
1528 | pr_debug("btree_check() done"); | |
1529 | ||
1530 | /* | |
1531 | * bcache_journal_next() can't happen sooner, or | |
1532 | * btree_gc_finish() will give spurious errors about last_gc > | |
1533 | * gc_gen - this is a hack but oh well. | |
1534 | */ | |
1535 | bch_journal_next(&c->journal); | |
1536 | ||
119ba0f8 | 1537 | err = "error starting allocator thread"; |
cafe5635 | 1538 | for_each_cache(ca, c, i) |
119ba0f8 KO |
1539 | if (bch_cache_allocator_start(ca)) |
1540 | goto err; | |
cafe5635 KO |
1541 | |
1542 | /* | |
1543 | * First place it's safe to allocate: btree_check() and | |
1544 | * btree_gc_finish() have to run before we have buckets to | |
1545 | * allocate, and bch_bucket_alloc_set() might cause a journal | |
1546 | * entry to be written so bcache_journal_next() has to be called | |
1547 | * first. | |
1548 | * | |
1549 | * If the uuids were in the old format we have to rewrite them | |
1550 | * before the next journal entry is written: | |
1551 | */ | |
1552 | if (j->version < BCACHE_JSET_VERSION_UUID) | |
1553 | __uuid_write(c); | |
1554 | ||
1555 | bch_journal_replay(c, &journal, &op); | |
1556 | } else { | |
1557 | pr_notice("invalidating existing data"); | |
1558 | /* Don't want invalidate_buckets() to queue a gc yet */ | |
1559 | closure_lock(&c->gc, NULL); | |
1560 | ||
1561 | for_each_cache(ca, c, i) { | |
1562 | unsigned j; | |
1563 | ||
1564 | ca->sb.keys = clamp_t(int, ca->sb.nbuckets >> 7, | |
1565 | 2, SB_JOURNAL_BUCKETS); | |
1566 | ||
1567 | for (j = 0; j < ca->sb.keys; j++) | |
1568 | ca->sb.d[j] = ca->sb.first_bucket + j; | |
1569 | } | |
1570 | ||
1571 | bch_btree_gc_finish(c); | |
1572 | ||
119ba0f8 | 1573 | err = "error starting allocator thread"; |
cafe5635 | 1574 | for_each_cache(ca, c, i) |
119ba0f8 KO |
1575 | if (bch_cache_allocator_start(ca)) |
1576 | goto err; | |
cafe5635 KO |
1577 | |
1578 | mutex_lock(&c->bucket_lock); | |
1579 | for_each_cache(ca, c, i) | |
1580 | bch_prio_write(ca); | |
1581 | mutex_unlock(&c->bucket_lock); | |
1582 | ||
cafe5635 KO |
1583 | err = "cannot allocate new UUID bucket"; |
1584 | if (__uuid_write(c)) | |
1585 | goto err_unlock_gc; | |
1586 | ||
1587 | err = "cannot allocate new btree root"; | |
1588 | c->root = bch_btree_node_alloc(c, 0, &op.cl); | |
1589 | if (IS_ERR_OR_NULL(c->root)) | |
1590 | goto err_unlock_gc; | |
1591 | ||
1592 | bkey_copy_key(&c->root->key, &MAX_KEY); | |
57943511 | 1593 | bch_btree_node_write(c->root, &op.cl); |
cafe5635 KO |
1594 | |
1595 | bch_btree_set_root(c->root); | |
1596 | rw_unlock(true, c->root); | |
1597 | ||
1598 | /* | |
1599 | * We don't want to write the first journal entry until | |
1600 | * everything is set up - fortunately journal entries won't be | |
1601 | * written until the SET_CACHE_SYNC() here: | |
1602 | */ | |
1603 | SET_CACHE_SYNC(&c->sb, true); | |
1604 | ||
1605 | bch_journal_next(&c->journal); | |
1606 | bch_journal_meta(c, &op.cl); | |
1607 | ||
1608 | /* Unlock */ | |
1609 | closure_set_stopped(&c->gc.cl); | |
1610 | closure_put(&c->gc.cl); | |
1611 | } | |
1612 | ||
1613 | closure_sync(&op.cl); | |
1614 | c->sb.last_mount = get_seconds(); | |
1615 | bcache_write_super(c); | |
1616 | ||
1617 | list_for_each_entry_safe(dc, t, &uncached_devices, list) | |
1618 | bch_cached_dev_attach(dc, c); | |
1619 | ||
1620 | flash_devs_run(c); | |
1621 | ||
1622 | return; | |
1623 | err_unlock_gc: | |
1624 | closure_set_stopped(&c->gc.cl); | |
1625 | closure_put(&c->gc.cl); | |
1626 | err: | |
1627 | closure_sync(&op.cl); | |
1628 | /* XXX: test this, it's broken */ | |
1629 | bch_cache_set_error(c, err); | |
1630 | } | |
1631 | ||
1632 | static bool can_attach_cache(struct cache *ca, struct cache_set *c) | |
1633 | { | |
1634 | return ca->sb.block_size == c->sb.block_size && | |
1635 | ca->sb.bucket_size == c->sb.block_size && | |
1636 | ca->sb.nr_in_set == c->sb.nr_in_set; | |
1637 | } | |
1638 | ||
1639 | static const char *register_cache_set(struct cache *ca) | |
1640 | { | |
1641 | char buf[12]; | |
1642 | const char *err = "cannot allocate memory"; | |
1643 | struct cache_set *c; | |
1644 | ||
1645 | list_for_each_entry(c, &bch_cache_sets, list) | |
1646 | if (!memcmp(c->sb.set_uuid, ca->sb.set_uuid, 16)) { | |
1647 | if (c->cache[ca->sb.nr_this_dev]) | |
1648 | return "duplicate cache set member"; | |
1649 | ||
1650 | if (!can_attach_cache(ca, c)) | |
1651 | return "cache sb does not match set"; | |
1652 | ||
1653 | if (!CACHE_SYNC(&ca->sb)) | |
1654 | SET_CACHE_SYNC(&c->sb, false); | |
1655 | ||
1656 | goto found; | |
1657 | } | |
1658 | ||
1659 | c = bch_cache_set_alloc(&ca->sb); | |
1660 | if (!c) | |
1661 | return err; | |
1662 | ||
1663 | err = "error creating kobject"; | |
1664 | if (kobject_add(&c->kobj, bcache_kobj, "%pU", c->sb.set_uuid) || | |
1665 | kobject_add(&c->internal, &c->kobj, "internal")) | |
1666 | goto err; | |
1667 | ||
1668 | if (bch_cache_accounting_add_kobjs(&c->accounting, &c->kobj)) | |
1669 | goto err; | |
1670 | ||
1671 | bch_debug_init_cache_set(c); | |
1672 | ||
1673 | list_add(&c->list, &bch_cache_sets); | |
1674 | found: | |
1675 | sprintf(buf, "cache%i", ca->sb.nr_this_dev); | |
1676 | if (sysfs_create_link(&ca->kobj, &c->kobj, "set") || | |
1677 | sysfs_create_link(&c->kobj, &ca->kobj, buf)) | |
1678 | goto err; | |
1679 | ||
1680 | if (ca->sb.seq > c->sb.seq) { | |
1681 | c->sb.version = ca->sb.version; | |
1682 | memcpy(c->sb.set_uuid, ca->sb.set_uuid, 16); | |
1683 | c->sb.flags = ca->sb.flags; | |
1684 | c->sb.seq = ca->sb.seq; | |
1685 | pr_debug("set version = %llu", c->sb.version); | |
1686 | } | |
1687 | ||
1688 | ca->set = c; | |
1689 | ca->set->cache[ca->sb.nr_this_dev] = ca; | |
1690 | c->cache_by_alloc[c->caches_loaded++] = ca; | |
1691 | ||
1692 | if (c->caches_loaded == c->sb.nr_in_set) | |
1693 | run_cache_set(c); | |
1694 | ||
1695 | return NULL; | |
1696 | err: | |
1697 | bch_cache_set_unregister(c); | |
1698 | return err; | |
1699 | } | |
1700 | ||
1701 | /* Cache device */ | |
1702 | ||
1703 | void bch_cache_release(struct kobject *kobj) | |
1704 | { | |
1705 | struct cache *ca = container_of(kobj, struct cache, kobj); | |
1706 | ||
1707 | if (ca->set) | |
1708 | ca->set->cache[ca->sb.nr_this_dev] = NULL; | |
1709 | ||
1710 | bch_cache_allocator_exit(ca); | |
1711 | ||
1712 | bio_split_pool_free(&ca->bio_split_hook); | |
1713 | ||
cafe5635 KO |
1714 | free_pages((unsigned long) ca->disk_buckets, ilog2(bucket_pages(ca))); |
1715 | kfree(ca->prio_buckets); | |
1716 | vfree(ca->buckets); | |
1717 | ||
1718 | free_heap(&ca->heap); | |
1719 | free_fifo(&ca->unused); | |
1720 | free_fifo(&ca->free_inc); | |
1721 | free_fifo(&ca->free); | |
1722 | ||
1723 | if (ca->sb_bio.bi_inline_vecs[0].bv_page) | |
1724 | put_page(ca->sb_bio.bi_io_vec[0].bv_page); | |
1725 | ||
1726 | if (!IS_ERR_OR_NULL(ca->bdev)) { | |
1727 | blk_sync_queue(bdev_get_queue(ca->bdev)); | |
1728 | blkdev_put(ca->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); | |
1729 | } | |
1730 | ||
1731 | kfree(ca); | |
1732 | module_put(THIS_MODULE); | |
1733 | } | |
1734 | ||
1735 | static int cache_alloc(struct cache_sb *sb, struct cache *ca) | |
1736 | { | |
1737 | size_t free; | |
1738 | struct bucket *b; | |
1739 | ||
cafe5635 KO |
1740 | __module_get(THIS_MODULE); |
1741 | kobject_init(&ca->kobj, &bch_cache_ktype); | |
1742 | ||
cafe5635 KO |
1743 | INIT_LIST_HEAD(&ca->discards); |
1744 | ||
cafe5635 KO |
1745 | bio_init(&ca->journal.bio); |
1746 | ca->journal.bio.bi_max_vecs = 8; | |
1747 | ca->journal.bio.bi_io_vec = ca->journal.bio.bi_inline_vecs; | |
1748 | ||
1749 | free = roundup_pow_of_two(ca->sb.nbuckets) >> 9; | |
1750 | free = max_t(size_t, free, (prio_buckets(ca) + 8) * 2); | |
1751 | ||
1752 | if (!init_fifo(&ca->free, free, GFP_KERNEL) || | |
1753 | !init_fifo(&ca->free_inc, free << 2, GFP_KERNEL) || | |
1754 | !init_fifo(&ca->unused, free << 2, GFP_KERNEL) || | |
1755 | !init_heap(&ca->heap, free << 3, GFP_KERNEL) || | |
f59fce84 | 1756 | !(ca->buckets = vzalloc(sizeof(struct bucket) * |
cafe5635 KO |
1757 | ca->sb.nbuckets)) || |
1758 | !(ca->prio_buckets = kzalloc(sizeof(uint64_t) * prio_buckets(ca) * | |
1759 | 2, GFP_KERNEL)) || | |
1760 | !(ca->disk_buckets = alloc_bucket_pages(GFP_KERNEL, ca)) || | |
cafe5635 | 1761 | bio_split_pool_init(&ca->bio_split_hook)) |
f59fce84 | 1762 | return -ENOMEM; |
cafe5635 KO |
1763 | |
1764 | ca->prio_last_buckets = ca->prio_buckets + prio_buckets(ca); | |
1765 | ||
cafe5635 KO |
1766 | for_each_bucket(b, ca) |
1767 | atomic_set(&b->pin, 0); | |
1768 | ||
1769 | if (bch_cache_allocator_init(ca)) | |
1770 | goto err; | |
1771 | ||
1772 | return 0; | |
1773 | err: | |
1774 | kobject_put(&ca->kobj); | |
1775 | return -ENOMEM; | |
1776 | } | |
1777 | ||
f59fce84 | 1778 | static void register_cache(struct cache_sb *sb, struct page *sb_page, |
cafe5635 KO |
1779 | struct block_device *bdev, struct cache *ca) |
1780 | { | |
1781 | char name[BDEVNAME_SIZE]; | |
1782 | const char *err = "cannot allocate memory"; | |
1783 | ||
f59fce84 | 1784 | memcpy(&ca->sb, sb, sizeof(struct cache_sb)); |
cafe5635 KO |
1785 | ca->bdev = bdev; |
1786 | ca->bdev->bd_holder = ca; | |
1787 | ||
f59fce84 KO |
1788 | bio_init(&ca->sb_bio); |
1789 | ca->sb_bio.bi_max_vecs = 1; | |
1790 | ca->sb_bio.bi_io_vec = ca->sb_bio.bi_inline_vecs; | |
1791 | ca->sb_bio.bi_io_vec[0].bv_page = sb_page; | |
1792 | get_page(sb_page); | |
1793 | ||
cafe5635 KO |
1794 | if (blk_queue_discard(bdev_get_queue(ca->bdev))) |
1795 | ca->discard = CACHE_DISCARD(&ca->sb); | |
1796 | ||
f59fce84 KO |
1797 | if (cache_alloc(sb, ca) != 0) |
1798 | goto err; | |
1799 | ||
cafe5635 KO |
1800 | err = "error creating kobject"; |
1801 | if (kobject_add(&ca->kobj, &part_to_dev(bdev->bd_part)->kobj, "bcache")) | |
1802 | goto err; | |
1803 | ||
1804 | err = register_cache_set(ca); | |
1805 | if (err) | |
1806 | goto err; | |
1807 | ||
1808 | pr_info("registered cache device %s", bdevname(bdev, name)); | |
f59fce84 | 1809 | return; |
cafe5635 | 1810 | err: |
f59fce84 | 1811 | pr_notice("error opening %s: %s", bdevname(bdev, name), err); |
cafe5635 | 1812 | kobject_put(&ca->kobj); |
cafe5635 KO |
1813 | } |
1814 | ||
1815 | /* Global interfaces/init */ | |
1816 | ||
1817 | static ssize_t register_bcache(struct kobject *, struct kobj_attribute *, | |
1818 | const char *, size_t); | |
1819 | ||
1820 | kobj_attribute_write(register, register_bcache); | |
1821 | kobj_attribute_write(register_quiet, register_bcache); | |
1822 | ||
a9dd53ad GP |
1823 | static bool bch_is_open_backing(struct block_device *bdev) { |
1824 | struct cache_set *c, *tc; | |
1825 | struct cached_dev *dc, *t; | |
1826 | ||
1827 | list_for_each_entry_safe(c, tc, &bch_cache_sets, list) | |
1828 | list_for_each_entry_safe(dc, t, &c->cached_devs, list) | |
1829 | if (dc->bdev == bdev) | |
1830 | return true; | |
1831 | list_for_each_entry_safe(dc, t, &uncached_devices, list) | |
1832 | if (dc->bdev == bdev) | |
1833 | return true; | |
1834 | return false; | |
1835 | } | |
1836 | ||
1837 | static bool bch_is_open_cache(struct block_device *bdev) { | |
1838 | struct cache_set *c, *tc; | |
1839 | struct cache *ca; | |
1840 | unsigned i; | |
1841 | ||
1842 | list_for_each_entry_safe(c, tc, &bch_cache_sets, list) | |
1843 | for_each_cache(ca, c, i) | |
1844 | if (ca->bdev == bdev) | |
1845 | return true; | |
1846 | return false; | |
1847 | } | |
1848 | ||
1849 | static bool bch_is_open(struct block_device *bdev) { | |
1850 | return bch_is_open_cache(bdev) || bch_is_open_backing(bdev); | |
1851 | } | |
1852 | ||
cafe5635 KO |
1853 | static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr, |
1854 | const char *buffer, size_t size) | |
1855 | { | |
1856 | ssize_t ret = size; | |
1857 | const char *err = "cannot allocate memory"; | |
1858 | char *path = NULL; | |
1859 | struct cache_sb *sb = NULL; | |
1860 | struct block_device *bdev = NULL; | |
1861 | struct page *sb_page = NULL; | |
1862 | ||
1863 | if (!try_module_get(THIS_MODULE)) | |
1864 | return -EBUSY; | |
1865 | ||
1866 | mutex_lock(&bch_register_lock); | |
1867 | ||
1868 | if (!(path = kstrndup(buffer, size, GFP_KERNEL)) || | |
1869 | !(sb = kmalloc(sizeof(struct cache_sb), GFP_KERNEL))) | |
1870 | goto err; | |
1871 | ||
1872 | err = "failed to open device"; | |
1873 | bdev = blkdev_get_by_path(strim(path), | |
1874 | FMODE_READ|FMODE_WRITE|FMODE_EXCL, | |
1875 | sb); | |
f59fce84 | 1876 | if (IS_ERR(bdev)) { |
a9dd53ad GP |
1877 | if (bdev == ERR_PTR(-EBUSY)) { |
1878 | bdev = lookup_bdev(strim(path)); | |
1879 | if (!IS_ERR(bdev) && bch_is_open(bdev)) | |
1880 | err = "device already registered"; | |
1881 | else | |
1882 | err = "device busy"; | |
1883 | } | |
cafe5635 | 1884 | goto err; |
f59fce84 KO |
1885 | } |
1886 | ||
1887 | err = "failed to set blocksize"; | |
1888 | if (set_blocksize(bdev, 4096)) | |
1889 | goto err_close; | |
cafe5635 KO |
1890 | |
1891 | err = read_super(sb, bdev, &sb_page); | |
1892 | if (err) | |
1893 | goto err_close; | |
1894 | ||
2903381f | 1895 | if (SB_IS_BDEV(sb)) { |
cafe5635 | 1896 | struct cached_dev *dc = kzalloc(sizeof(*dc), GFP_KERNEL); |
f59fce84 KO |
1897 | if (!dc) |
1898 | goto err_close; | |
cafe5635 | 1899 | |
f59fce84 | 1900 | register_bdev(sb, sb_page, bdev, dc); |
cafe5635 KO |
1901 | } else { |
1902 | struct cache *ca = kzalloc(sizeof(*ca), GFP_KERNEL); | |
f59fce84 KO |
1903 | if (!ca) |
1904 | goto err_close; | |
cafe5635 | 1905 | |
f59fce84 | 1906 | register_cache(sb, sb_page, bdev, ca); |
cafe5635 | 1907 | } |
f59fce84 KO |
1908 | out: |
1909 | if (sb_page) | |
cafe5635 | 1910 | put_page(sb_page); |
cafe5635 KO |
1911 | kfree(sb); |
1912 | kfree(path); | |
1913 | mutex_unlock(&bch_register_lock); | |
1914 | module_put(THIS_MODULE); | |
1915 | return ret; | |
f59fce84 KO |
1916 | |
1917 | err_close: | |
1918 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); | |
1919 | err: | |
1920 | if (attr != &ksysfs_register_quiet) | |
1921 | pr_info("error opening %s: %s", path, err); | |
1922 | ret = -EINVAL; | |
1923 | goto out; | |
cafe5635 KO |
1924 | } |
1925 | ||
1926 | static int bcache_reboot(struct notifier_block *n, unsigned long code, void *x) | |
1927 | { | |
1928 | if (code == SYS_DOWN || | |
1929 | code == SYS_HALT || | |
1930 | code == SYS_POWER_OFF) { | |
1931 | DEFINE_WAIT(wait); | |
1932 | unsigned long start = jiffies; | |
1933 | bool stopped = false; | |
1934 | ||
1935 | struct cache_set *c, *tc; | |
1936 | struct cached_dev *dc, *tdc; | |
1937 | ||
1938 | mutex_lock(&bch_register_lock); | |
1939 | ||
1940 | if (list_empty(&bch_cache_sets) && | |
1941 | list_empty(&uncached_devices)) | |
1942 | goto out; | |
1943 | ||
1944 | pr_info("Stopping all devices:"); | |
1945 | ||
1946 | list_for_each_entry_safe(c, tc, &bch_cache_sets, list) | |
1947 | bch_cache_set_stop(c); | |
1948 | ||
1949 | list_for_each_entry_safe(dc, tdc, &uncached_devices, list) | |
1950 | bcache_device_stop(&dc->disk); | |
1951 | ||
1952 | /* What's a condition variable? */ | |
1953 | while (1) { | |
1954 | long timeout = start + 2 * HZ - jiffies; | |
1955 | ||
1956 | stopped = list_empty(&bch_cache_sets) && | |
1957 | list_empty(&uncached_devices); | |
1958 | ||
1959 | if (timeout < 0 || stopped) | |
1960 | break; | |
1961 | ||
1962 | prepare_to_wait(&unregister_wait, &wait, | |
1963 | TASK_UNINTERRUPTIBLE); | |
1964 | ||
1965 | mutex_unlock(&bch_register_lock); | |
1966 | schedule_timeout(timeout); | |
1967 | mutex_lock(&bch_register_lock); | |
1968 | } | |
1969 | ||
1970 | finish_wait(&unregister_wait, &wait); | |
1971 | ||
1972 | if (stopped) | |
1973 | pr_info("All devices stopped"); | |
1974 | else | |
1975 | pr_notice("Timeout waiting for devices to be closed"); | |
1976 | out: | |
1977 | mutex_unlock(&bch_register_lock); | |
1978 | } | |
1979 | ||
1980 | return NOTIFY_DONE; | |
1981 | } | |
1982 | ||
1983 | static struct notifier_block reboot = { | |
1984 | .notifier_call = bcache_reboot, | |
1985 | .priority = INT_MAX, /* before any real devices */ | |
1986 | }; | |
1987 | ||
1988 | static void bcache_exit(void) | |
1989 | { | |
1990 | bch_debug_exit(); | |
1991 | bch_writeback_exit(); | |
1992 | bch_request_exit(); | |
1993 | bch_btree_exit(); | |
1994 | if (bcache_kobj) | |
1995 | kobject_put(bcache_kobj); | |
1996 | if (bcache_wq) | |
1997 | destroy_workqueue(bcache_wq); | |
1998 | unregister_blkdev(bcache_major, "bcache"); | |
1999 | unregister_reboot_notifier(&reboot); | |
2000 | } | |
2001 | ||
2002 | static int __init bcache_init(void) | |
2003 | { | |
2004 | static const struct attribute *files[] = { | |
2005 | &ksysfs_register.attr, | |
2006 | &ksysfs_register_quiet.attr, | |
2007 | NULL | |
2008 | }; | |
2009 | ||
2010 | mutex_init(&bch_register_lock); | |
2011 | init_waitqueue_head(&unregister_wait); | |
2012 | register_reboot_notifier(&reboot); | |
07e86ccb | 2013 | closure_debug_init(); |
cafe5635 KO |
2014 | |
2015 | bcache_major = register_blkdev(0, "bcache"); | |
2016 | if (bcache_major < 0) | |
2017 | return bcache_major; | |
2018 | ||
2019 | if (!(bcache_wq = create_workqueue("bcache")) || | |
2020 | !(bcache_kobj = kobject_create_and_add("bcache", fs_kobj)) || | |
2021 | sysfs_create_files(bcache_kobj, files) || | |
2022 | bch_btree_init() || | |
2023 | bch_request_init() || | |
2024 | bch_writeback_init() || | |
2025 | bch_debug_init(bcache_kobj)) | |
2026 | goto err; | |
2027 | ||
2028 | return 0; | |
2029 | err: | |
2030 | bcache_exit(); | |
2031 | return -ENOMEM; | |
2032 | } | |
2033 | ||
2034 | module_exit(bcache_exit); | |
2035 | module_init(bcache_init); |