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