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