Commit | Line | Data |
---|---|---|
87418ef9 | 1 | // SPDX-License-Identifier: GPL-2.0 |
cafe5635 KO |
2 | /* |
3 | * bcache setup/teardown code, and some metadata io - read a superblock and | |
4 | * figure out what to do with it. | |
5 | * | |
6 | * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com> | |
7 | * Copyright 2012 Google, Inc. | |
8 | */ | |
9 | ||
10 | #include "bcache.h" | |
11 | #include "btree.h" | |
12 | #include "debug.h" | |
65d45231 | 13 | #include "extents.h" |
cafe5635 | 14 | #include "request.h" |
279afbad | 15 | #include "writeback.h" |
d721a43f | 16 | #include "features.h" |
cafe5635 | 17 | |
c37511b8 | 18 | #include <linux/blkdev.h> |
4ee60ec1 | 19 | #include <linux/pagemap.h> |
cafe5635 KO |
20 | #include <linux/debugfs.h> |
21 | #include <linux/genhd.h> | |
28935ab5 | 22 | #include <linux/idr.h> |
79826c35 | 23 | #include <linux/kthread.h> |
ee4a36f4 | 24 | #include <linux/workqueue.h> |
cafe5635 KO |
25 | #include <linux/module.h> |
26 | #include <linux/random.h> | |
27 | #include <linux/reboot.h> | |
28 | #include <linux/sysfs.h> | |
29 | ||
9aaf5165 CL |
30 | unsigned int bch_cutoff_writeback; |
31 | unsigned int bch_cutoff_writeback_sync; | |
32 | ||
cafe5635 KO |
33 | static const char bcache_magic[] = { |
34 | 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca, | |
35 | 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81 | |
36 | }; | |
37 | ||
38 | static const char invalid_uuid[] = { | |
39 | 0xa0, 0x3e, 0xf8, 0xed, 0x3e, 0xe1, 0xb8, 0x78, | |
40 | 0xc8, 0x50, 0xfc, 0x5e, 0xcb, 0x16, 0xcd, 0x99 | |
41 | }; | |
42 | ||
cafe5635 KO |
43 | static struct kobject *bcache_kobj; |
44 | struct mutex bch_register_lock; | |
a59ff6cc | 45 | bool bcache_is_reboot; |
cafe5635 KO |
46 | LIST_HEAD(bch_cache_sets); |
47 | static LIST_HEAD(uncached_devices); | |
48 | ||
28935ab5 | 49 | static int bcache_major; |
1dbe32ad | 50 | static DEFINE_IDA(bcache_device_idx); |
cafe5635 KO |
51 | static wait_queue_head_t unregister_wait; |
52 | struct workqueue_struct *bcache_wq; | |
afe78ab4 | 53 | struct workqueue_struct *bch_flush_wq; |
0f843e65 | 54 | struct workqueue_struct *bch_journal_wq; |
cafe5635 | 55 | |
a59ff6cc | 56 | |
cafe5635 | 57 | #define BTREE_MAX_PAGES (256 * 1024 / PAGE_SIZE) |
1dbe32ad CL |
58 | /* limitation of partitions number on single bcache device */ |
59 | #define BCACHE_MINORS 128 | |
60 | /* limitation of bcache devices number on single system */ | |
61 | #define BCACHE_DEVICE_IDX_MAX ((1U << MINORBITS)/BCACHE_MINORS) | |
cafe5635 | 62 | |
cafe5635 KO |
63 | /* Superblock */ |
64 | ||
ffa47032 CL |
65 | static unsigned int get_bucket_size(struct cache_sb *sb, struct cache_sb_disk *s) |
66 | { | |
67 | unsigned int bucket_size = le16_to_cpu(s->bucket_size); | |
68 | ||
b16671e8 CL |
69 | if (sb->version >= BCACHE_SB_VERSION_CDEV_WITH_FEATURES) { |
70 | if (bch_has_feature_large_bucket(sb)) { | |
71 | unsigned int max, order; | |
72 | ||
73 | max = sizeof(unsigned int) * BITS_PER_BYTE - 1; | |
74 | order = le16_to_cpu(s->bucket_size); | |
75 | /* | |
76 | * bcache tool will make sure the overflow won't | |
77 | * happen, an error message here is enough. | |
78 | */ | |
79 | if (order > max) | |
80 | pr_err("Bucket size (1 << %u) overflows\n", | |
81 | order); | |
82 | bucket_size = 1 << order; | |
83 | } else if (bch_has_feature_obso_large_bucket(sb)) { | |
84 | bucket_size += | |
85 | le16_to_cpu(s->obso_bucket_size_hi) << 16; | |
86 | } | |
87 | } | |
ffa47032 CL |
88 | |
89 | return bucket_size; | |
90 | } | |
91 | ||
5b21403c CL |
92 | static const char *read_super_common(struct cache_sb *sb, struct block_device *bdev, |
93 | struct cache_sb_disk *s) | |
94 | { | |
95 | const char *err; | |
96 | unsigned int i; | |
97 | ||
198efa35 | 98 | sb->first_bucket= le16_to_cpu(s->first_bucket); |
5b21403c | 99 | sb->nbuckets = le64_to_cpu(s->nbuckets); |
ffa47032 | 100 | sb->bucket_size = get_bucket_size(sb, s); |
5b21403c CL |
101 | |
102 | sb->nr_in_set = le16_to_cpu(s->nr_in_set); | |
103 | sb->nr_this_dev = le16_to_cpu(s->nr_this_dev); | |
104 | ||
198efa35 CL |
105 | err = "Too many journal buckets"; |
106 | if (sb->keys > SB_JOURNAL_BUCKETS) | |
107 | goto err; | |
108 | ||
5b21403c CL |
109 | err = "Too many buckets"; |
110 | if (sb->nbuckets > LONG_MAX) | |
111 | goto err; | |
112 | ||
113 | err = "Not enough buckets"; | |
114 | if (sb->nbuckets < 1 << 7) | |
115 | goto err; | |
116 | ||
c557a5f7 CL |
117 | err = "Bad block size (not power of 2)"; |
118 | if (!is_power_of_2(sb->block_size)) | |
119 | goto err; | |
120 | ||
121 | err = "Bad block size (larger than page size)"; | |
122 | if (sb->block_size > PAGE_SECTORS) | |
123 | goto err; | |
124 | ||
125 | err = "Bad bucket size (not power of 2)"; | |
126 | if (!is_power_of_2(sb->bucket_size)) | |
127 | goto err; | |
128 | ||
129 | err = "Bad bucket size (smaller than page size)"; | |
130 | if (sb->bucket_size < PAGE_SECTORS) | |
5b21403c CL |
131 | goto err; |
132 | ||
133 | err = "Invalid superblock: device too small"; | |
134 | if (get_capacity(bdev->bd_disk) < | |
135 | sb->bucket_size * sb->nbuckets) | |
136 | goto err; | |
137 | ||
138 | err = "Bad UUID"; | |
139 | if (bch_is_zero(sb->set_uuid, 16)) | |
140 | goto err; | |
141 | ||
142 | err = "Bad cache device number in set"; | |
143 | if (!sb->nr_in_set || | |
144 | sb->nr_in_set <= sb->nr_this_dev || | |
145 | sb->nr_in_set > MAX_CACHES_PER_SET) | |
146 | goto err; | |
147 | ||
148 | err = "Journal buckets not sequential"; | |
149 | for (i = 0; i < sb->keys; i++) | |
150 | if (sb->d[i] != sb->first_bucket + i) | |
151 | goto err; | |
152 | ||
153 | err = "Too many journal buckets"; | |
154 | if (sb->first_bucket + sb->keys > sb->nbuckets) | |
155 | goto err; | |
156 | ||
157 | err = "Invalid superblock: first bucket comes before end of super"; | |
158 | if (sb->first_bucket * sb->bucket_size < 16) | |
159 | goto err; | |
160 | ||
161 | err = NULL; | |
162 | err: | |
163 | return err; | |
164 | } | |
165 | ||
166 | ||
cafe5635 | 167 | static const char *read_super(struct cache_sb *sb, struct block_device *bdev, |
cfa0c56d | 168 | struct cache_sb_disk **res) |
cafe5635 KO |
169 | { |
170 | const char *err; | |
a702a692 | 171 | struct cache_sb_disk *s; |
6321bef0 | 172 | struct page *page; |
6f10f7d1 | 173 | unsigned int i; |
cafe5635 | 174 | |
6321bef0 CH |
175 | page = read_cache_page_gfp(bdev->bd_inode->i_mapping, |
176 | SB_OFFSET >> PAGE_SHIFT, GFP_KERNEL); | |
177 | if (IS_ERR(page)) | |
cafe5635 | 178 | return "IO error"; |
6321bef0 | 179 | s = page_address(page) + offset_in_page(SB_OFFSET); |
cafe5635 KO |
180 | |
181 | sb->offset = le64_to_cpu(s->offset); | |
182 | sb->version = le64_to_cpu(s->version); | |
183 | ||
184 | memcpy(sb->magic, s->magic, 16); | |
185 | memcpy(sb->uuid, s->uuid, 16); | |
186 | memcpy(sb->set_uuid, s->set_uuid, 16); | |
187 | memcpy(sb->label, s->label, SB_LABEL_SIZE); | |
188 | ||
189 | sb->flags = le64_to_cpu(s->flags); | |
190 | sb->seq = le64_to_cpu(s->seq); | |
cafe5635 | 191 | sb->last_mount = le32_to_cpu(s->last_mount); |
cafe5635 KO |
192 | sb->keys = le16_to_cpu(s->keys); |
193 | ||
194 | for (i = 0; i < SB_JOURNAL_BUCKETS; i++) | |
195 | sb->d[i] = le64_to_cpu(s->d[i]); | |
196 | ||
46f5aa88 | 197 | pr_debug("read sb version %llu, flags %llu, seq %llu, journal size %u\n", |
cafe5635 KO |
198 | sb->version, sb->flags, sb->seq, sb->keys); |
199 | ||
aaf8dbea | 200 | err = "Not a bcache superblock (bad offset)"; |
cafe5635 KO |
201 | if (sb->offset != SB_SECTOR) |
202 | goto err; | |
203 | ||
aaf8dbea | 204 | err = "Not a bcache superblock (bad magic)"; |
cafe5635 KO |
205 | if (memcmp(sb->magic, bcache_magic, 16)) |
206 | goto err; | |
207 | ||
cafe5635 KO |
208 | err = "Bad checksum"; |
209 | if (s->csum != csum_set(s)) | |
210 | goto err; | |
211 | ||
212 | err = "Bad UUID"; | |
169ef1cf | 213 | if (bch_is_zero(sb->uuid, 16)) |
cafe5635 KO |
214 | goto err; |
215 | ||
8abb2a5d KO |
216 | sb->block_size = le16_to_cpu(s->block_size); |
217 | ||
218 | err = "Superblock block size smaller than device block size"; | |
219 | if (sb->block_size << 9 < bdev_logical_block_size(bdev)) | |
220 | goto err; | |
221 | ||
2903381f KO |
222 | switch (sb->version) { |
223 | case BCACHE_SB_VERSION_BDEV: | |
2903381f KO |
224 | sb->data_offset = BDEV_DATA_START_DEFAULT; |
225 | break; | |
226 | case BCACHE_SB_VERSION_BDEV_WITH_OFFSET: | |
d721a43f | 227 | case BCACHE_SB_VERSION_BDEV_WITH_FEATURES: |
2903381f KO |
228 | sb->data_offset = le64_to_cpu(s->data_offset); |
229 | ||
230 | err = "Bad data offset"; | |
231 | if (sb->data_offset < BDEV_DATA_START_DEFAULT) | |
232 | goto err; | |
cafe5635 | 233 | |
2903381f KO |
234 | break; |
235 | case BCACHE_SB_VERSION_CDEV: | |
236 | case BCACHE_SB_VERSION_CDEV_WITH_UUID: | |
5b21403c CL |
237 | err = read_super_common(sb, bdev, s); |
238 | if (err) | |
2903381f | 239 | goto err; |
2903381f | 240 | break; |
d721a43f | 241 | case BCACHE_SB_VERSION_CDEV_WITH_FEATURES: |
ffa47032 CL |
242 | /* |
243 | * Feature bits are needed in read_super_common(), | |
244 | * convert them firstly. | |
245 | */ | |
d721a43f CL |
246 | sb->feature_compat = le64_to_cpu(s->feature_compat); |
247 | sb->feature_incompat = le64_to_cpu(s->feature_incompat); | |
248 | sb->feature_ro_compat = le64_to_cpu(s->feature_ro_compat); | |
1dfc0686 CL |
249 | |
250 | /* Check incompatible features */ | |
251 | err = "Unsupported compatible feature found"; | |
252 | if (bch_has_unknown_compat_features(sb)) | |
253 | goto err; | |
254 | ||
255 | err = "Unsupported read-only compatible feature found"; | |
256 | if (bch_has_unknown_ro_compat_features(sb)) | |
257 | goto err; | |
258 | ||
259 | err = "Unsupported incompatible feature found"; | |
260 | if (bch_has_unknown_incompat_features(sb)) | |
261 | goto err; | |
262 | ||
ffa47032 CL |
263 | err = read_super_common(sb, bdev, s); |
264 | if (err) | |
2903381f | 265 | goto err; |
2903381f KO |
266 | break; |
267 | default: | |
268 | err = "Unsupported superblock version"; | |
cafe5635 | 269 | goto err; |
2903381f KO |
270 | } |
271 | ||
75cbb3f1 | 272 | sb->last_mount = (u32)ktime_get_real_seconds(); |
cfa0c56d | 273 | *res = s; |
6321bef0 | 274 | return NULL; |
cafe5635 | 275 | err: |
6321bef0 | 276 | put_page(page); |
cafe5635 KO |
277 | return err; |
278 | } | |
279 | ||
4246a0b6 | 280 | static void write_bdev_super_endio(struct bio *bio) |
cafe5635 KO |
281 | { |
282 | struct cached_dev *dc = bio->bi_private; | |
08ec1e62 CL |
283 | |
284 | if (bio->bi_status) | |
285 | bch_count_backing_io_errors(dc, bio); | |
cafe5635 | 286 | |
cb7a583e | 287 | closure_put(&dc->sb_write); |
cafe5635 KO |
288 | } |
289 | ||
475389ae CH |
290 | static void __write_super(struct cache_sb *sb, struct cache_sb_disk *out, |
291 | struct bio *bio) | |
cafe5635 | 292 | { |
6f10f7d1 | 293 | unsigned int i; |
cafe5635 | 294 | |
475389ae | 295 | bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_META; |
4f024f37 | 296 | bio->bi_iter.bi_sector = SB_SECTOR; |
475389ae CH |
297 | __bio_add_page(bio, virt_to_page(out), SB_SIZE, |
298 | offset_in_page(out)); | |
cafe5635 KO |
299 | |
300 | out->offset = cpu_to_le64(sb->offset); | |
cafe5635 KO |
301 | |
302 | memcpy(out->uuid, sb->uuid, 16); | |
303 | memcpy(out->set_uuid, sb->set_uuid, 16); | |
304 | memcpy(out->label, sb->label, SB_LABEL_SIZE); | |
305 | ||
306 | out->flags = cpu_to_le64(sb->flags); | |
307 | out->seq = cpu_to_le64(sb->seq); | |
308 | ||
309 | out->last_mount = cpu_to_le32(sb->last_mount); | |
310 | out->first_bucket = cpu_to_le16(sb->first_bucket); | |
311 | out->keys = cpu_to_le16(sb->keys); | |
312 | ||
313 | for (i = 0; i < sb->keys; i++) | |
314 | out->d[i] = cpu_to_le64(sb->d[i]); | |
315 | ||
d721a43f CL |
316 | if (sb->version >= BCACHE_SB_VERSION_CDEV_WITH_FEATURES) { |
317 | out->feature_compat = cpu_to_le64(sb->feature_compat); | |
318 | out->feature_incompat = cpu_to_le64(sb->feature_incompat); | |
319 | out->feature_ro_compat = cpu_to_le64(sb->feature_ro_compat); | |
320 | } | |
321 | ||
322 | out->version = cpu_to_le64(sb->version); | |
cafe5635 KO |
323 | out->csum = csum_set(out); |
324 | ||
46f5aa88 | 325 | pr_debug("ver %llu, flags %llu, seq %llu\n", |
cafe5635 KO |
326 | sb->version, sb->flags, sb->seq); |
327 | ||
4e49ea4a | 328 | submit_bio(bio); |
cafe5635 KO |
329 | } |
330 | ||
cb7a583e KO |
331 | static void bch_write_bdev_super_unlock(struct closure *cl) |
332 | { | |
333 | struct cached_dev *dc = container_of(cl, struct cached_dev, sb_write); | |
334 | ||
335 | up(&dc->sb_write_mutex); | |
336 | } | |
337 | ||
cafe5635 KO |
338 | void bch_write_bdev_super(struct cached_dev *dc, struct closure *parent) |
339 | { | |
cb7a583e | 340 | struct closure *cl = &dc->sb_write; |
cafe5635 KO |
341 | struct bio *bio = &dc->sb_bio; |
342 | ||
cb7a583e KO |
343 | down(&dc->sb_write_mutex); |
344 | closure_init(cl, parent); | |
cafe5635 | 345 | |
475389ae | 346 | bio_init(bio, dc->sb_bv, 1); |
74d46992 | 347 | bio_set_dev(bio, dc->bdev); |
cafe5635 KO |
348 | bio->bi_end_io = write_bdev_super_endio; |
349 | bio->bi_private = dc; | |
350 | ||
351 | closure_get(cl); | |
27a40ab9 | 352 | /* I/O request sent to backing device */ |
475389ae | 353 | __write_super(&dc->sb, dc->sb_disk, bio); |
cafe5635 | 354 | |
cb7a583e | 355 | closure_return_with_destructor(cl, bch_write_bdev_super_unlock); |
cafe5635 KO |
356 | } |
357 | ||
4246a0b6 | 358 | static void write_super_endio(struct bio *bio) |
cafe5635 KO |
359 | { |
360 | struct cache *ca = bio->bi_private; | |
361 | ||
5138ac67 CL |
362 | /* is_read = 0 */ |
363 | bch_count_io_errors(ca, bio->bi_status, 0, | |
364 | "writing superblock"); | |
cb7a583e KO |
365 | closure_put(&ca->set->sb_write); |
366 | } | |
367 | ||
368 | static void bcache_write_super_unlock(struct closure *cl) | |
369 | { | |
370 | struct cache_set *c = container_of(cl, struct cache_set, sb_write); | |
371 | ||
372 | up(&c->sb_write_mutex); | |
cafe5635 KO |
373 | } |
374 | ||
375 | void bcache_write_super(struct cache_set *c) | |
376 | { | |
cb7a583e | 377 | struct closure *cl = &c->sb_write; |
08fdb2cd CL |
378 | struct cache *ca = c->cache; |
379 | struct bio *bio = &ca->sb_bio; | |
380 | unsigned int version = BCACHE_SB_VERSION_CDEV_WITH_UUID; | |
cafe5635 | 381 | |
cb7a583e KO |
382 | down(&c->sb_write_mutex); |
383 | closure_init(cl, &c->cl); | |
cafe5635 | 384 | |
4a784266 | 385 | ca->sb.seq++; |
cafe5635 | 386 | |
4a784266 CL |
387 | if (ca->sb.version < version) |
388 | ca->sb.version = version; | |
cafe5635 | 389 | |
08fdb2cd CL |
390 | bio_init(bio, ca->sb_bv, 1); |
391 | bio_set_dev(bio, ca->bdev); | |
392 | bio->bi_end_io = write_super_endio; | |
393 | bio->bi_private = ca; | |
cafe5635 | 394 | |
08fdb2cd CL |
395 | closure_get(cl); |
396 | __write_super(&ca->sb, ca->sb_disk, bio); | |
cafe5635 | 397 | |
cb7a583e | 398 | closure_return_with_destructor(cl, bcache_write_super_unlock); |
cafe5635 KO |
399 | } |
400 | ||
401 | /* UUID io */ | |
402 | ||
4246a0b6 | 403 | static void uuid_endio(struct bio *bio) |
cafe5635 KO |
404 | { |
405 | struct closure *cl = bio->bi_private; | |
cb7a583e | 406 | struct cache_set *c = container_of(cl, struct cache_set, uuid_write); |
cafe5635 | 407 | |
4e4cbee9 | 408 | cache_set_err_on(bio->bi_status, c, "accessing uuids"); |
cafe5635 KO |
409 | bch_bbio_free(bio, c); |
410 | closure_put(cl); | |
411 | } | |
412 | ||
cb7a583e KO |
413 | static void uuid_io_unlock(struct closure *cl) |
414 | { | |
415 | struct cache_set *c = container_of(cl, struct cache_set, uuid_write); | |
416 | ||
417 | up(&c->uuid_write_mutex); | |
418 | } | |
419 | ||
ad0d9e76 | 420 | static void uuid_io(struct cache_set *c, int op, unsigned long op_flags, |
cafe5635 KO |
421 | struct bkey *k, struct closure *parent) |
422 | { | |
cb7a583e | 423 | struct closure *cl = &c->uuid_write; |
cafe5635 | 424 | struct uuid_entry *u; |
6f10f7d1 | 425 | unsigned int i; |
85b1492e | 426 | char buf[80]; |
cafe5635 KO |
427 | |
428 | BUG_ON(!parent); | |
cb7a583e KO |
429 | down(&c->uuid_write_mutex); |
430 | closure_init(cl, parent); | |
cafe5635 KO |
431 | |
432 | for (i = 0; i < KEY_PTRS(k); i++) { | |
433 | struct bio *bio = bch_bbio_alloc(c); | |
434 | ||
1eff9d32 | 435 | bio->bi_opf = REQ_SYNC | REQ_META | op_flags; |
4f024f37 | 436 | bio->bi_iter.bi_size = KEY_SIZE(k) << 9; |
cafe5635 KO |
437 | |
438 | bio->bi_end_io = uuid_endio; | |
439 | bio->bi_private = cl; | |
ad0d9e76 | 440 | bio_set_op_attrs(bio, op, REQ_SYNC|REQ_META|op_flags); |
169ef1cf | 441 | bch_bio_map(bio, c->uuids); |
cafe5635 KO |
442 | |
443 | bch_submit_bbio(bio, c, k, i); | |
444 | ||
ad0d9e76 | 445 | if (op != REQ_OP_WRITE) |
cafe5635 KO |
446 | break; |
447 | } | |
448 | ||
dc9d98d6 | 449 | bch_extent_to_text(buf, sizeof(buf), k); |
46f5aa88 | 450 | pr_debug("%s UUIDs at %s\n", op == REQ_OP_WRITE ? "wrote" : "read", buf); |
cafe5635 KO |
451 | |
452 | for (u = c->uuids; u < c->uuids + c->nr_uuids; u++) | |
169ef1cf | 453 | if (!bch_is_zero(u->uuid, 16)) |
46f5aa88 | 454 | pr_debug("Slot %zi: %pU: %s: 1st: %u last: %u inv: %u\n", |
cafe5635 KO |
455 | u - c->uuids, u->uuid, u->label, |
456 | u->first_reg, u->last_reg, u->invalidated); | |
457 | ||
cb7a583e | 458 | closure_return_with_destructor(cl, uuid_io_unlock); |
cafe5635 KO |
459 | } |
460 | ||
461 | static char *uuid_read(struct cache_set *c, struct jset *j, struct closure *cl) | |
462 | { | |
463 | struct bkey *k = &j->uuid_bucket; | |
464 | ||
65d45231 | 465 | if (__bch_btree_ptr_invalid(c, k)) |
cafe5635 KO |
466 | return "bad uuid pointer"; |
467 | ||
468 | bkey_copy(&c->uuid_bucket, k); | |
70fd7614 | 469 | uuid_io(c, REQ_OP_READ, 0, k, cl); |
cafe5635 KO |
470 | |
471 | if (j->version < BCACHE_JSET_VERSION_UUIDv1) { | |
472 | struct uuid_entry_v0 *u0 = (void *) c->uuids; | |
473 | struct uuid_entry *u1 = (void *) c->uuids; | |
474 | int i; | |
475 | ||
476 | closure_sync(cl); | |
477 | ||
478 | /* | |
479 | * Since the new uuid entry is bigger than the old, we have to | |
480 | * convert starting at the highest memory address and work down | |
481 | * in order to do it in place | |
482 | */ | |
483 | ||
484 | for (i = c->nr_uuids - 1; | |
485 | i >= 0; | |
486 | --i) { | |
487 | memcpy(u1[i].uuid, u0[i].uuid, 16); | |
488 | memcpy(u1[i].label, u0[i].label, 32); | |
489 | ||
490 | u1[i].first_reg = u0[i].first_reg; | |
491 | u1[i].last_reg = u0[i].last_reg; | |
492 | u1[i].invalidated = u0[i].invalidated; | |
493 | ||
494 | u1[i].flags = 0; | |
495 | u1[i].sectors = 0; | |
496 | } | |
497 | } | |
498 | ||
499 | return NULL; | |
500 | } | |
501 | ||
502 | static int __uuid_write(struct cache_set *c) | |
503 | { | |
504 | BKEY_PADDED(key) k; | |
505 | struct closure cl; | |
4a784266 | 506 | struct cache *ca = c->cache; |
21e478dd | 507 | unsigned int size; |
cafe5635 | 508 | |
1fae7cf0 | 509 | closure_init_stack(&cl); |
cafe5635 KO |
510 | lockdep_assert_held(&bch_register_lock); |
511 | ||
17e4aed8 | 512 | if (bch_bucket_alloc_set(c, RESERVE_BTREE, &k.key, true)) |
cafe5635 KO |
513 | return 1; |
514 | ||
4a784266 | 515 | size = meta_bucket_pages(&ca->sb) * PAGE_SECTORS; |
21e478dd | 516 | SET_KEY_SIZE(&k.key, size); |
ad0d9e76 | 517 | uuid_io(c, REQ_OP_WRITE, 0, &k.key, &cl); |
cafe5635 KO |
518 | closure_sync(&cl); |
519 | ||
7a55948d | 520 | /* Only one bucket used for uuid write */ |
7a55948d SW |
521 | atomic_long_add(ca->sb.bucket_size, &ca->meta_sectors_written); |
522 | ||
cafe5635 | 523 | bkey_copy(&c->uuid_bucket, &k.key); |
3a3b6a4e | 524 | bkey_put(c, &k.key); |
cafe5635 KO |
525 | return 0; |
526 | } | |
527 | ||
528 | int bch_uuid_write(struct cache_set *c) | |
529 | { | |
530 | int ret = __uuid_write(c); | |
531 | ||
532 | if (!ret) | |
533 | bch_journal_meta(c, NULL); | |
534 | ||
535 | return ret; | |
536 | } | |
537 | ||
538 | static struct uuid_entry *uuid_find(struct cache_set *c, const char *uuid) | |
539 | { | |
540 | struct uuid_entry *u; | |
541 | ||
542 | for (u = c->uuids; | |
543 | u < c->uuids + c->nr_uuids; u++) | |
544 | if (!memcmp(u->uuid, uuid, 16)) | |
545 | return u; | |
546 | ||
547 | return NULL; | |
548 | } | |
549 | ||
550 | static struct uuid_entry *uuid_find_empty(struct cache_set *c) | |
551 | { | |
552 | static const char zero_uuid[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; | |
1fae7cf0 | 553 | |
cafe5635 KO |
554 | return uuid_find(c, zero_uuid); |
555 | } | |
556 | ||
557 | /* | |
558 | * Bucket priorities/gens: | |
559 | * | |
560 | * For each bucket, we store on disk its | |
3be11dba CL |
561 | * 8 bit gen |
562 | * 16 bit priority | |
cafe5635 KO |
563 | * |
564 | * See alloc.c for an explanation of the gen. The priority is used to implement | |
565 | * lru (and in the future other) cache replacement policies; for most purposes | |
566 | * it's just an opaque integer. | |
567 | * | |
568 | * The gens and the priorities don't have a whole lot to do with each other, and | |
569 | * it's actually the gens that must be written out at specific times - it's no | |
570 | * big deal if the priorities don't get written, if we lose them we just reuse | |
571 | * buckets in suboptimal order. | |
572 | * | |
573 | * On disk they're stored in a packed array, and in as many buckets are required | |
574 | * to fit them all. The buckets we use to store them form a list; the journal | |
575 | * header points to the first bucket, the first bucket points to the second | |
576 | * bucket, et cetera. | |
577 | * | |
578 | * This code is used by the allocation code; periodically (whenever it runs out | |
579 | * of buckets to allocate from) the allocation code will invalidate some | |
580 | * buckets, but it can't use those buckets until their new gens are safely on | |
581 | * disk. | |
582 | */ | |
583 | ||
4246a0b6 | 584 | static void prio_endio(struct bio *bio) |
cafe5635 KO |
585 | { |
586 | struct cache *ca = bio->bi_private; | |
587 | ||
4e4cbee9 | 588 | cache_set_err_on(bio->bi_status, ca->set, "accessing priorities"); |
cafe5635 KO |
589 | bch_bbio_free(bio, ca->set); |
590 | closure_put(&ca->prio); | |
591 | } | |
592 | ||
ad0d9e76 MC |
593 | static void prio_io(struct cache *ca, uint64_t bucket, int op, |
594 | unsigned long op_flags) | |
cafe5635 KO |
595 | { |
596 | struct closure *cl = &ca->prio; | |
597 | struct bio *bio = bch_bbio_alloc(ca->set); | |
598 | ||
599 | closure_init_stack(cl); | |
600 | ||
4f024f37 | 601 | bio->bi_iter.bi_sector = bucket * ca->sb.bucket_size; |
74d46992 | 602 | bio_set_dev(bio, ca->bdev); |
c954ac8d | 603 | bio->bi_iter.bi_size = meta_bucket_bytes(&ca->sb); |
cafe5635 KO |
604 | |
605 | bio->bi_end_io = prio_endio; | |
606 | bio->bi_private = ca; | |
ad0d9e76 | 607 | bio_set_op_attrs(bio, op, REQ_SYNC|REQ_META|op_flags); |
169ef1cf | 608 | bch_bio_map(bio, ca->disk_buckets); |
cafe5635 | 609 | |
771f393e | 610 | closure_bio_submit(ca->set, bio, &ca->prio); |
cafe5635 KO |
611 | closure_sync(cl); |
612 | } | |
613 | ||
84c529ae | 614 | int bch_prio_write(struct cache *ca, bool wait) |
cafe5635 KO |
615 | { |
616 | int i; | |
617 | struct bucket *b; | |
618 | struct closure cl; | |
619 | ||
46f5aa88 | 620 | pr_debug("free_prio=%zu, free_none=%zu, free_inc=%zu\n", |
84c529ae AR |
621 | fifo_used(&ca->free[RESERVE_PRIO]), |
622 | fifo_used(&ca->free[RESERVE_NONE]), | |
623 | fifo_used(&ca->free_inc)); | |
624 | ||
625 | /* | |
626 | * Pre-check if there are enough free buckets. In the non-blocking | |
627 | * scenario it's better to fail early rather than starting to allocate | |
628 | * buckets and do a cleanup later in case of failure. | |
629 | */ | |
630 | if (!wait) { | |
631 | size_t avail = fifo_used(&ca->free[RESERVE_PRIO]) + | |
632 | fifo_used(&ca->free[RESERVE_NONE]); | |
633 | if (prio_buckets(ca) > avail) | |
634 | return -ENOMEM; | |
635 | } | |
636 | ||
cafe5635 KO |
637 | closure_init_stack(&cl); |
638 | ||
639 | lockdep_assert_held(&ca->set->bucket_lock); | |
640 | ||
cafe5635 KO |
641 | ca->disk_buckets->seq++; |
642 | ||
643 | atomic_long_add(ca->sb.bucket_size * prio_buckets(ca), | |
644 | &ca->meta_sectors_written); | |
645 | ||
cafe5635 KO |
646 | for (i = prio_buckets(ca) - 1; i >= 0; --i) { |
647 | long bucket; | |
648 | struct prio_set *p = ca->disk_buckets; | |
b1a67b0f KO |
649 | struct bucket_disk *d = p->data; |
650 | struct bucket_disk *end = d + prios_per_bucket(ca); | |
cafe5635 KO |
651 | |
652 | for (b = ca->buckets + i * prios_per_bucket(ca); | |
653 | b < ca->buckets + ca->sb.nbuckets && d < end; | |
654 | b++, d++) { | |
655 | d->prio = cpu_to_le16(b->prio); | |
656 | d->gen = b->gen; | |
657 | } | |
658 | ||
659 | p->next_bucket = ca->prio_buckets[i + 1]; | |
81ab4190 | 660 | p->magic = pset_magic(&ca->sb); |
c954ac8d | 661 | p->csum = bch_crc64(&p->magic, meta_bucket_bytes(&ca->sb) - 8); |
cafe5635 | 662 | |
84c529ae | 663 | bucket = bch_bucket_alloc(ca, RESERVE_PRIO, wait); |
cafe5635 KO |
664 | BUG_ON(bucket == -1); |
665 | ||
666 | mutex_unlock(&ca->set->bucket_lock); | |
ad0d9e76 | 667 | prio_io(ca, bucket, REQ_OP_WRITE, 0); |
cafe5635 KO |
668 | mutex_lock(&ca->set->bucket_lock); |
669 | ||
670 | ca->prio_buckets[i] = bucket; | |
671 | atomic_dec_bug(&ca->buckets[bucket].pin); | |
672 | } | |
673 | ||
674 | mutex_unlock(&ca->set->bucket_lock); | |
675 | ||
676 | bch_journal_meta(ca->set, &cl); | |
677 | closure_sync(&cl); | |
678 | ||
679 | mutex_lock(&ca->set->bucket_lock); | |
680 | ||
cafe5635 KO |
681 | /* |
682 | * Don't want the old priorities to get garbage collected until after we | |
683 | * finish writing the new ones, and they're journalled | |
684 | */ | |
2531d9ee KO |
685 | for (i = 0; i < prio_buckets(ca); i++) { |
686 | if (ca->prio_last_buckets[i]) | |
687 | __bch_bucket_free(ca, | |
688 | &ca->buckets[ca->prio_last_buckets[i]]); | |
689 | ||
cafe5635 | 690 | ca->prio_last_buckets[i] = ca->prio_buckets[i]; |
2531d9ee | 691 | } |
84c529ae | 692 | return 0; |
cafe5635 KO |
693 | } |
694 | ||
49d08d59 | 695 | static int prio_read(struct cache *ca, uint64_t bucket) |
cafe5635 KO |
696 | { |
697 | struct prio_set *p = ca->disk_buckets; | |
698 | struct bucket_disk *d = p->data + prios_per_bucket(ca), *end = d; | |
699 | struct bucket *b; | |
6f10f7d1 | 700 | unsigned int bucket_nr = 0; |
49d08d59 | 701 | int ret = -EIO; |
cafe5635 KO |
702 | |
703 | for (b = ca->buckets; | |
704 | b < ca->buckets + ca->sb.nbuckets; | |
705 | b++, d++) { | |
706 | if (d == end) { | |
707 | ca->prio_buckets[bucket_nr] = bucket; | |
708 | ca->prio_last_buckets[bucket_nr] = bucket; | |
709 | bucket_nr++; | |
710 | ||
70fd7614 | 711 | prio_io(ca, bucket, REQ_OP_READ, 0); |
cafe5635 | 712 | |
b0d30981 | 713 | if (p->csum != |
c954ac8d | 714 | bch_crc64(&p->magic, meta_bucket_bytes(&ca->sb) - 8)) { |
46f5aa88 | 715 | pr_warn("bad csum reading priorities\n"); |
49d08d59 CL |
716 | goto out; |
717 | } | |
cafe5635 | 718 | |
49d08d59 | 719 | if (p->magic != pset_magic(&ca->sb)) { |
46f5aa88 | 720 | pr_warn("bad magic reading priorities\n"); |
49d08d59 CL |
721 | goto out; |
722 | } | |
cafe5635 KO |
723 | |
724 | bucket = p->next_bucket; | |
725 | d = p->data; | |
726 | } | |
727 | ||
728 | b->prio = le16_to_cpu(d->prio); | |
3a2fd9d5 | 729 | b->gen = b->last_gc = d->gen; |
cafe5635 | 730 | } |
49d08d59 CL |
731 | |
732 | ret = 0; | |
733 | out: | |
734 | return ret; | |
cafe5635 KO |
735 | } |
736 | ||
737 | /* Bcache device */ | |
738 | ||
739 | static int open_dev(struct block_device *b, fmode_t mode) | |
740 | { | |
741 | struct bcache_device *d = b->bd_disk->private_data; | |
1fae7cf0 | 742 | |
c4d951dd | 743 | if (test_bit(BCACHE_DEV_CLOSING, &d->flags)) |
cafe5635 KO |
744 | return -ENXIO; |
745 | ||
746 | closure_get(&d->cl); | |
747 | return 0; | |
748 | } | |
749 | ||
867e1162 | 750 | static void release_dev(struct gendisk *b, fmode_t mode) |
cafe5635 KO |
751 | { |
752 | struct bcache_device *d = b->private_data; | |
1fae7cf0 | 753 | |
cafe5635 | 754 | closure_put(&d->cl); |
cafe5635 KO |
755 | } |
756 | ||
757 | static int ioctl_dev(struct block_device *b, fmode_t mode, | |
758 | unsigned int cmd, unsigned long arg) | |
759 | { | |
760 | struct bcache_device *d = b->bd_disk->private_data; | |
0f0709e6 | 761 | |
cafe5635 KO |
762 | return d->ioctl(d, mode, cmd, arg); |
763 | } | |
764 | ||
c62b37d9 CH |
765 | static const struct block_device_operations bcache_cached_ops = { |
766 | .submit_bio = cached_dev_submit_bio, | |
767 | .open = open_dev, | |
768 | .release = release_dev, | |
769 | .ioctl = ioctl_dev, | |
770 | .owner = THIS_MODULE, | |
771 | }; | |
772 | ||
773 | static const struct block_device_operations bcache_flash_ops = { | |
774 | .submit_bio = flash_dev_submit_bio, | |
cafe5635 KO |
775 | .open = open_dev, |
776 | .release = release_dev, | |
777 | .ioctl = ioctl_dev, | |
778 | .owner = THIS_MODULE, | |
779 | }; | |
780 | ||
781 | void bcache_device_stop(struct bcache_device *d) | |
782 | { | |
c4d951dd | 783 | if (!test_and_set_bit(BCACHE_DEV_CLOSING, &d->flags)) |
63d63b51 CL |
784 | /* |
785 | * closure_fn set to | |
786 | * - cached device: cached_dev_flush() | |
787 | * - flash dev: flash_dev_flush() | |
788 | */ | |
cafe5635 KO |
789 | closure_queue(&d->cl); |
790 | } | |
791 | ||
ee668506 KO |
792 | static void bcache_device_unlink(struct bcache_device *d) |
793 | { | |
c4d951dd | 794 | lockdep_assert_held(&bch_register_lock); |
ee668506 | 795 | |
c4d951dd | 796 | if (d->c && !test_and_set_bit(BCACHE_DEV_UNLINK_DONE, &d->flags)) { |
08fdb2cd | 797 | struct cache *ca = d->c->cache; |
ee668506 | 798 | |
c4d951dd KO |
799 | sysfs_remove_link(&d->c->kobj, d->name); |
800 | sysfs_remove_link(&d->kobj, "cache"); | |
801 | ||
08fdb2cd | 802 | bd_unlink_disk_holder(ca->bdev, d->disk); |
c4d951dd | 803 | } |
ee668506 KO |
804 | } |
805 | ||
806 | static void bcache_device_link(struct bcache_device *d, struct cache_set *c, | |
807 | const char *name) | |
808 | { | |
08fdb2cd | 809 | struct cache *ca = c->cache; |
4b6efb4b | 810 | int ret; |
ee668506 | 811 | |
08fdb2cd | 812 | bd_link_disk_holder(ca->bdev, d->disk); |
ee668506 KO |
813 | |
814 | snprintf(d->name, BCACHEDEVNAME_SIZE, | |
815 | "%s%u", name, d->id); | |
816 | ||
4b6efb4b CL |
817 | ret = sysfs_create_link(&d->kobj, &c->kobj, "cache"); |
818 | if (ret < 0) | |
46f5aa88 | 819 | pr_err("Couldn't create device -> cache set symlink\n"); |
4b6efb4b CL |
820 | |
821 | ret = sysfs_create_link(&c->kobj, &d->kobj, d->name); | |
822 | if (ret < 0) | |
46f5aa88 | 823 | pr_err("Couldn't create cache set -> device symlink\n"); |
fecaee6f ZL |
824 | |
825 | clear_bit(BCACHE_DEV_UNLINK_DONE, &d->flags); | |
ee668506 KO |
826 | } |
827 | ||
cafe5635 KO |
828 | static void bcache_device_detach(struct bcache_device *d) |
829 | { | |
830 | lockdep_assert_held(&bch_register_lock); | |
831 | ||
ea8c5356 CL |
832 | atomic_dec(&d->c->attached_dev_nr); |
833 | ||
c4d951dd | 834 | if (test_bit(BCACHE_DEV_DETACHING, &d->flags)) { |
cafe5635 KO |
835 | struct uuid_entry *u = d->c->uuids + d->id; |
836 | ||
837 | SET_UUID_FLASH_ONLY(u, 0); | |
838 | memcpy(u->uuid, invalid_uuid, 16); | |
75cbb3f1 | 839 | u->invalidated = cpu_to_le32((u32)ktime_get_real_seconds()); |
cafe5635 | 840 | bch_uuid_write(d->c); |
cafe5635 KO |
841 | } |
842 | ||
c4d951dd | 843 | bcache_device_unlink(d); |
ee668506 | 844 | |
cafe5635 KO |
845 | d->c->devices[d->id] = NULL; |
846 | closure_put(&d->c->caching); | |
847 | d->c = NULL; | |
848 | } | |
849 | ||
850 | static void bcache_device_attach(struct bcache_device *d, struct cache_set *c, | |
6f10f7d1 | 851 | unsigned int id) |
cafe5635 | 852 | { |
cafe5635 KO |
853 | d->id = id; |
854 | d->c = c; | |
855 | c->devices[id] = d; | |
856 | ||
2831231d CL |
857 | if (id >= c->devices_max_used) |
858 | c->devices_max_used = id + 1; | |
859 | ||
cafe5635 KO |
860 | closure_get(&c->caching); |
861 | } | |
862 | ||
1dbe32ad CL |
863 | static inline int first_minor_to_idx(int first_minor) |
864 | { | |
865 | return (first_minor/BCACHE_MINORS); | |
866 | } | |
867 | ||
868 | static inline int idx_to_first_minor(int idx) | |
869 | { | |
870 | return (idx * BCACHE_MINORS); | |
871 | } | |
872 | ||
cafe5635 KO |
873 | static void bcache_device_free(struct bcache_device *d) |
874 | { | |
2d886951 CL |
875 | struct gendisk *disk = d->disk; |
876 | ||
cafe5635 KO |
877 | lockdep_assert_held(&bch_register_lock); |
878 | ||
2d886951 | 879 | if (disk) |
46f5aa88 | 880 | pr_info("%s stopped\n", disk->disk_name); |
2d886951 | 881 | else |
46f5aa88 | 882 | pr_err("bcache device (NULL gendisk) stopped\n"); |
cafe5635 KO |
883 | |
884 | if (d->c) | |
885 | bcache_device_detach(d); | |
2d886951 CL |
886 | |
887 | if (disk) { | |
1dbe32ad | 888 | ida_simple_remove(&bcache_device_idx, |
2d886951 | 889 | first_minor_to_idx(disk->first_minor)); |
8468f450 | 890 | blk_cleanup_disk(disk); |
28935ab5 | 891 | } |
cafe5635 | 892 | |
d19936a2 | 893 | bioset_exit(&d->bio_split); |
958b4338 PE |
894 | kvfree(d->full_dirty_stripes); |
895 | kvfree(d->stripe_sectors_dirty); | |
cafe5635 KO |
896 | |
897 | closure_debug_destroy(&d->cl); | |
898 | } | |
899 | ||
6f10f7d1 | 900 | static int bcache_device_init(struct bcache_device *d, unsigned int block_size, |
c62b37d9 CH |
901 | sector_t sectors, struct block_device *cached_bdev, |
902 | const struct block_device_operations *ops) | |
cafe5635 KO |
903 | { |
904 | struct request_queue *q; | |
5f2b18ec BVA |
905 | const size_t max_stripes = min_t(size_t, INT_MAX, |
906 | SIZE_MAX / sizeof(atomic_t)); | |
65f0f017 | 907 | uint64_t n; |
1dbe32ad | 908 | int idx; |
279afbad | 909 | |
2d679fc7 KO |
910 | if (!d->stripe_size) |
911 | d->stripe_size = 1 << 31; | |
279afbad | 912 | |
65f0f017 CL |
913 | n = DIV_ROUND_UP_ULL(sectors, d->stripe_size); |
914 | if (!n || n > max_stripes) { | |
915 | pr_err("nr_stripes too large or invalid: %llu (start sector beyond end of disk?)\n", | |
916 | n); | |
279afbad | 917 | return -ENOMEM; |
48a915a8 | 918 | } |
65f0f017 | 919 | d->nr_stripes = n; |
279afbad KO |
920 | |
921 | n = d->nr_stripes * sizeof(atomic_t); | |
bc4e54f6 | 922 | d->stripe_sectors_dirty = kvzalloc(n, GFP_KERNEL); |
279afbad KO |
923 | if (!d->stripe_sectors_dirty) |
924 | return -ENOMEM; | |
cafe5635 | 925 | |
48a915a8 | 926 | n = BITS_TO_LONGS(d->nr_stripes) * sizeof(unsigned long); |
bc4e54f6 | 927 | d->full_dirty_stripes = kvzalloc(n, GFP_KERNEL); |
48a915a8 | 928 | if (!d->full_dirty_stripes) |
224b0683 | 929 | goto out_free_stripe_sectors_dirty; |
48a915a8 | 930 | |
1dbe32ad CL |
931 | idx = ida_simple_get(&bcache_device_idx, 0, |
932 | BCACHE_DEVICE_IDX_MAX, GFP_KERNEL); | |
933 | if (idx < 0) | |
224b0683 | 934 | goto out_free_full_dirty_stripes; |
b8c0d911 | 935 | |
d19936a2 | 936 | if (bioset_init(&d->bio_split, 4, offsetof(struct bbio, bio), |
9b4e9f5a | 937 | BIOSET_NEED_BVECS|BIOSET_NEED_RESCUER)) |
224b0683 | 938 | goto out_ida_remove; |
9b4e9f5a | 939 | |
bc70852f | 940 | d->disk = blk_alloc_disk(NUMA_NO_NODE); |
9b4e9f5a | 941 | if (!d->disk) |
224b0683 | 942 | goto out_bioset_exit; |
cafe5635 | 943 | |
279afbad | 944 | set_capacity(d->disk, sectors); |
1dbe32ad | 945 | snprintf(d->disk->disk_name, DISK_NAME_LEN, "bcache%i", idx); |
cafe5635 KO |
946 | |
947 | d->disk->major = bcache_major; | |
1dbe32ad | 948 | d->disk->first_minor = idx_to_first_minor(idx); |
bc70852f | 949 | d->disk->minors = BCACHE_MINORS; |
c62b37d9 | 950 | d->disk->fops = ops; |
cafe5635 KO |
951 | d->disk->private_data = d; |
952 | ||
bc70852f | 953 | q = d->disk->queue; |
cafe5635 KO |
954 | q->limits.max_hw_sectors = UINT_MAX; |
955 | q->limits.max_sectors = UINT_MAX; | |
956 | q->limits.max_segment_size = UINT_MAX; | |
a8affc03 | 957 | q->limits.max_segments = BIO_MAX_VECS; |
2bb4cd5c | 958 | blk_queue_max_discard_sectors(q, UINT_MAX); |
90db6919 | 959 | q->limits.discard_granularity = 512; |
cafe5635 KO |
960 | q->limits.io_min = block_size; |
961 | q->limits.logical_block_size = block_size; | |
962 | q->limits.physical_block_size = block_size; | |
dcacbc12 MFO |
963 | |
964 | if (q->limits.logical_block_size > PAGE_SIZE && cached_bdev) { | |
965 | /* | |
966 | * This should only happen with BCACHE_SB_VERSION_BDEV. | |
967 | * Block/page size is checked for BCACHE_SB_VERSION_CDEV. | |
968 | */ | |
4b25bbf5 | 969 | pr_info("%s: sb/logical block size (%u) greater than page size (%lu) falling back to device logical block size (%u)\n", |
dcacbc12 MFO |
970 | d->disk->disk_name, q->limits.logical_block_size, |
971 | PAGE_SIZE, bdev_logical_block_size(cached_bdev)); | |
972 | ||
973 | /* This also adjusts physical block size/min io size if needed */ | |
974 | blk_queue_logical_block_size(q, bdev_logical_block_size(cached_bdev)); | |
975 | } | |
976 | ||
44e1ebe2 BVA |
977 | blk_queue_flag_set(QUEUE_FLAG_NONROT, d->disk->queue); |
978 | blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, d->disk->queue); | |
979 | blk_queue_flag_set(QUEUE_FLAG_DISCARD, d->disk->queue); | |
cafe5635 | 980 | |
84b4ff9e | 981 | blk_queue_write_cache(q, true, true); |
54d12f2b | 982 | |
cafe5635 | 983 | return 0; |
9b4e9f5a | 984 | |
224b0683 CH |
985 | out_bioset_exit: |
986 | bioset_exit(&d->bio_split); | |
987 | out_ida_remove: | |
9b4e9f5a | 988 | ida_simple_remove(&bcache_device_idx, idx); |
224b0683 CH |
989 | out_free_full_dirty_stripes: |
990 | kvfree(d->full_dirty_stripes); | |
991 | out_free_stripe_sectors_dirty: | |
992 | kvfree(d->stripe_sectors_dirty); | |
9b4e9f5a FS |
993 | return -ENOMEM; |
994 | ||
cafe5635 KO |
995 | } |
996 | ||
997 | /* Cached device */ | |
998 | ||
999 | static void calc_cached_dev_sectors(struct cache_set *c) | |
1000 | { | |
1001 | uint64_t sectors = 0; | |
1002 | struct cached_dev *dc; | |
1003 | ||
1004 | list_for_each_entry(dc, &c->cached_devs, list) | |
cda25b82 | 1005 | sectors += bdev_nr_sectors(dc->bdev); |
cafe5635 KO |
1006 | |
1007 | c->cached_dev_sectors = sectors; | |
1008 | } | |
1009 | ||
0f0709e6 CL |
1010 | #define BACKING_DEV_OFFLINE_TIMEOUT 5 |
1011 | static int cached_dev_status_update(void *arg) | |
1012 | { | |
1013 | struct cached_dev *dc = arg; | |
1014 | struct request_queue *q; | |
1015 | ||
1016 | /* | |
1017 | * If this delayed worker is stopping outside, directly quit here. | |
1018 | * dc->io_disable might be set via sysfs interface, so check it | |
1019 | * here too. | |
1020 | */ | |
1021 | while (!kthread_should_stop() && !dc->io_disable) { | |
1022 | q = bdev_get_queue(dc->bdev); | |
1023 | if (blk_queue_dying(q)) | |
1024 | dc->offline_seconds++; | |
1025 | else | |
1026 | dc->offline_seconds = 0; | |
1027 | ||
1028 | if (dc->offline_seconds >= BACKING_DEV_OFFLINE_TIMEOUT) { | |
0f5cd781 CH |
1029 | pr_err("%pg: device offline for %d seconds\n", |
1030 | dc->bdev, | |
0f0709e6 | 1031 | BACKING_DEV_OFFLINE_TIMEOUT); |
46f5aa88 JP |
1032 | pr_err("%s: disable I/O request due to backing device offline\n", |
1033 | dc->disk.name); | |
0f0709e6 CL |
1034 | dc->io_disable = true; |
1035 | /* let others know earlier that io_disable is true */ | |
1036 | smp_mb(); | |
1037 | bcache_device_stop(&dc->disk); | |
1038 | break; | |
1039 | } | |
1040 | schedule_timeout_interruptible(HZ); | |
1041 | } | |
1042 | ||
1043 | wait_for_kthread_stop(); | |
1044 | return 0; | |
1045 | } | |
1046 | ||
1047 | ||
0b13efec | 1048 | int bch_cached_dev_run(struct cached_dev *dc) |
cafe5635 | 1049 | { |
13e1db65 | 1050 | int ret = 0; |
cafe5635 | 1051 | struct bcache_device *d = &dc->disk; |
792732d9 | 1052 | char *buf = kmemdup_nul(dc->sb.label, SB_LABEL_SIZE, GFP_KERNEL); |
a25c32be GP |
1053 | char *env[] = { |
1054 | "DRIVER=bcache", | |
1055 | kasprintf(GFP_KERNEL, "CACHED_UUID=%pU", dc->sb.uuid), | |
792732d9 | 1056 | kasprintf(GFP_KERNEL, "CACHED_LABEL=%s", buf ? : ""), |
ab9e1400 | 1057 | NULL, |
a25c32be | 1058 | }; |
cafe5635 | 1059 | |
e0faa3d7 | 1060 | if (dc->io_disable) { |
0f5cd781 | 1061 | pr_err("I/O disabled on cached dev %pg\n", dc->bdev); |
13e1db65 ZL |
1062 | ret = -EIO; |
1063 | goto out; | |
e0faa3d7 | 1064 | } |
0b13efec | 1065 | |
4d4d8573 | 1066 | if (atomic_xchg(&dc->running, 1)) { |
0f5cd781 | 1067 | pr_info("cached dev %pg is running already\n", dc->bdev); |
13e1db65 ZL |
1068 | ret = -EBUSY; |
1069 | goto out; | |
4d4d8573 | 1070 | } |
cafe5635 KO |
1071 | |
1072 | if (!d->c && | |
1073 | BDEV_STATE(&dc->sb) != BDEV_STATE_NONE) { | |
1074 | struct closure cl; | |
1fae7cf0 | 1075 | |
cafe5635 KO |
1076 | closure_init_stack(&cl); |
1077 | ||
1078 | SET_BDEV_STATE(&dc->sb, BDEV_STATE_STALE); | |
1079 | bch_write_bdev_super(dc, &cl); | |
1080 | closure_sync(&cl); | |
1081 | } | |
1082 | ||
2961c3bb LC |
1083 | ret = add_disk(d->disk); |
1084 | if (ret) | |
1085 | goto out; | |
ee668506 | 1086 | bd_link_disk_holder(dc->bdev, dc->disk.disk); |
3be11dba CL |
1087 | /* |
1088 | * won't show up in the uevent file, use udevadm monitor -e instead | |
1089 | * only class / kset properties are persistent | |
1090 | */ | |
cafe5635 | 1091 | kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env); |
a25c32be | 1092 | |
cafe5635 | 1093 | if (sysfs_create_link(&d->kobj, &disk_to_dev(d->disk)->kobj, "dev") || |
0b13efec CL |
1094 | sysfs_create_link(&disk_to_dev(d->disk)->kobj, |
1095 | &d->kobj, "bcache")) { | |
46f5aa88 | 1096 | pr_err("Couldn't create bcache dev <-> disk sysfs symlinks\n"); |
13e1db65 ZL |
1097 | ret = -ENOMEM; |
1098 | goto out; | |
0b13efec | 1099 | } |
0f0709e6 CL |
1100 | |
1101 | dc->status_update_thread = kthread_run(cached_dev_status_update, | |
1102 | dc, "bcache_status_update"); | |
1103 | if (IS_ERR(dc->status_update_thread)) { | |
46f5aa88 | 1104 | pr_warn("failed to create bcache_status_update kthread, continue to run without monitoring backing device status\n"); |
0f0709e6 | 1105 | } |
0b13efec | 1106 | |
13e1db65 ZL |
1107 | out: |
1108 | kfree(env[1]); | |
1109 | kfree(env[2]); | |
1110 | kfree(buf); | |
1111 | return ret; | |
cafe5635 KO |
1112 | } |
1113 | ||
3fd47bfe CL |
1114 | /* |
1115 | * If BCACHE_DEV_RATE_DW_RUNNING is set, it means routine of the delayed | |
1116 | * work dc->writeback_rate_update is running. Wait until the routine | |
1117 | * quits (BCACHE_DEV_RATE_DW_RUNNING is clear), then continue to | |
1118 | * cancel it. If BCACHE_DEV_RATE_DW_RUNNING is not clear after time_out | |
1119 | * seconds, give up waiting here and continue to cancel it too. | |
1120 | */ | |
1121 | static void cancel_writeback_rate_update_dwork(struct cached_dev *dc) | |
1122 | { | |
1123 | int time_out = WRITEBACK_RATE_UPDATE_SECS_MAX * HZ; | |
1124 | ||
1125 | do { | |
1126 | if (!test_bit(BCACHE_DEV_RATE_DW_RUNNING, | |
1127 | &dc->disk.flags)) | |
1128 | break; | |
1129 | time_out--; | |
1130 | schedule_timeout_interruptible(1); | |
1131 | } while (time_out > 0); | |
1132 | ||
1133 | if (time_out == 0) | |
46f5aa88 | 1134 | pr_warn("give up waiting for dc->writeback_write_update to quit\n"); |
3fd47bfe CL |
1135 | |
1136 | cancel_delayed_work_sync(&dc->writeback_rate_update); | |
1137 | } | |
1138 | ||
cafe5635 KO |
1139 | static void cached_dev_detach_finish(struct work_struct *w) |
1140 | { | |
1141 | struct cached_dev *dc = container_of(w, struct cached_dev, detach); | |
cafe5635 | 1142 | |
c4d951dd | 1143 | BUG_ON(!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags)); |
3b304d24 | 1144 | BUG_ON(refcount_read(&dc->count)); |
cafe5635 | 1145 | |
cafe5635 | 1146 | |
3fd47bfe CL |
1147 | if (test_and_clear_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags)) |
1148 | cancel_writeback_rate_update_dwork(dc); | |
1149 | ||
8d29c442 TJ |
1150 | if (!IS_ERR_OR_NULL(dc->writeback_thread)) { |
1151 | kthread_stop(dc->writeback_thread); | |
1152 | dc->writeback_thread = NULL; | |
1153 | } | |
1154 | ||
97ba3b81 CL |
1155 | mutex_lock(&bch_register_lock); |
1156 | ||
cafe5635 KO |
1157 | bcache_device_detach(&dc->disk); |
1158 | list_move(&dc->list, &uncached_devices); | |
0259d449 | 1159 | calc_cached_dev_sectors(dc->disk.c); |
cafe5635 | 1160 | |
c4d951dd | 1161 | clear_bit(BCACHE_DEV_DETACHING, &dc->disk.flags); |
5b1016e6 | 1162 | clear_bit(BCACHE_DEV_UNLINK_DONE, &dc->disk.flags); |
c4d951dd | 1163 | |
cafe5635 KO |
1164 | mutex_unlock(&bch_register_lock); |
1165 | ||
0f5cd781 | 1166 | pr_info("Caching disabled for %pg\n", dc->bdev); |
cafe5635 KO |
1167 | |
1168 | /* Drop ref we took in cached_dev_detach() */ | |
1169 | closure_put(&dc->disk.cl); | |
1170 | } | |
1171 | ||
1172 | void bch_cached_dev_detach(struct cached_dev *dc) | |
1173 | { | |
1174 | lockdep_assert_held(&bch_register_lock); | |
1175 | ||
c4d951dd | 1176 | if (test_bit(BCACHE_DEV_CLOSING, &dc->disk.flags)) |
cafe5635 KO |
1177 | return; |
1178 | ||
c4d951dd | 1179 | if (test_and_set_bit(BCACHE_DEV_DETACHING, &dc->disk.flags)) |
cafe5635 KO |
1180 | return; |
1181 | ||
1182 | /* | |
1183 | * Block the device from being closed and freed until we're finished | |
1184 | * detaching | |
1185 | */ | |
1186 | closure_get(&dc->disk.cl); | |
1187 | ||
1188 | bch_writeback_queue(dc); | |
3fd47bfe | 1189 | |
cafe5635 KO |
1190 | cached_dev_put(dc); |
1191 | } | |
1192 | ||
73ac105b TJ |
1193 | int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c, |
1194 | uint8_t *set_uuid) | |
cafe5635 | 1195 | { |
75cbb3f1 | 1196 | uint32_t rtime = cpu_to_le32((u32)ktime_get_real_seconds()); |
cafe5635 | 1197 | struct uuid_entry *u; |
86755b7a | 1198 | struct cached_dev *exist_dc, *t; |
0b13efec | 1199 | int ret = 0; |
cafe5635 | 1200 | |
1132e56e CL |
1201 | if ((set_uuid && memcmp(set_uuid, c->set_uuid, 16)) || |
1202 | (!set_uuid && memcmp(dc->sb.set_uuid, c->set_uuid, 16))) | |
cafe5635 KO |
1203 | return -ENOENT; |
1204 | ||
1205 | if (dc->disk.c) { | |
0f5cd781 | 1206 | pr_err("Can't attach %pg: already attached\n", dc->bdev); |
cafe5635 KO |
1207 | return -EINVAL; |
1208 | } | |
1209 | ||
1210 | if (test_bit(CACHE_SET_STOPPING, &c->flags)) { | |
0f5cd781 | 1211 | pr_err("Can't attach %pg: shutting down\n", dc->bdev); |
cafe5635 KO |
1212 | return -EINVAL; |
1213 | } | |
1214 | ||
4a784266 | 1215 | if (dc->sb.block_size < c->cache->sb.block_size) { |
cafe5635 | 1216 | /* Will die */ |
0f5cd781 CH |
1217 | pr_err("Couldn't attach %pg: block size less than set's block size\n", |
1218 | dc->bdev); | |
cafe5635 KO |
1219 | return -EINVAL; |
1220 | } | |
1221 | ||
86755b7a ML |
1222 | /* Check whether already attached */ |
1223 | list_for_each_entry_safe(exist_dc, t, &c->cached_devs, list) { | |
1224 | if (!memcmp(dc->sb.uuid, exist_dc->sb.uuid, 16)) { | |
0f5cd781 CH |
1225 | pr_err("Tried to attach %pg but duplicate UUID already attached\n", |
1226 | dc->bdev); | |
86755b7a ML |
1227 | |
1228 | return -EINVAL; | |
1229 | } | |
1230 | } | |
1231 | ||
cafe5635 KO |
1232 | u = uuid_find(c, dc->sb.uuid); |
1233 | ||
1234 | if (u && | |
1235 | (BDEV_STATE(&dc->sb) == BDEV_STATE_STALE || | |
1236 | BDEV_STATE(&dc->sb) == BDEV_STATE_NONE)) { | |
1237 | memcpy(u->uuid, invalid_uuid, 16); | |
75cbb3f1 | 1238 | u->invalidated = cpu_to_le32((u32)ktime_get_real_seconds()); |
cafe5635 KO |
1239 | u = NULL; |
1240 | } | |
1241 | ||
1242 | if (!u) { | |
1243 | if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) { | |
0f5cd781 | 1244 | pr_err("Couldn't find uuid for %pg in set\n", dc->bdev); |
cafe5635 KO |
1245 | return -ENOENT; |
1246 | } | |
1247 | ||
1248 | u = uuid_find_empty(c); | |
1249 | if (!u) { | |
0f5cd781 | 1250 | pr_err("Not caching %pg, no room for UUID\n", dc->bdev); |
cafe5635 KO |
1251 | return -EINVAL; |
1252 | } | |
1253 | } | |
1254 | ||
3be11dba CL |
1255 | /* |
1256 | * Deadlocks since we're called via sysfs... | |
1257 | * sysfs_remove_file(&dc->kobj, &sysfs_attach); | |
cafe5635 KO |
1258 | */ |
1259 | ||
169ef1cf | 1260 | if (bch_is_zero(u->uuid, 16)) { |
cafe5635 | 1261 | struct closure cl; |
1fae7cf0 | 1262 | |
cafe5635 KO |
1263 | closure_init_stack(&cl); |
1264 | ||
1265 | memcpy(u->uuid, dc->sb.uuid, 16); | |
1266 | memcpy(u->label, dc->sb.label, SB_LABEL_SIZE); | |
1267 | u->first_reg = u->last_reg = rtime; | |
1268 | bch_uuid_write(c); | |
1269 | ||
1132e56e | 1270 | memcpy(dc->sb.set_uuid, c->set_uuid, 16); |
cafe5635 KO |
1271 | SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN); |
1272 | ||
1273 | bch_write_bdev_super(dc, &cl); | |
1274 | closure_sync(&cl); | |
1275 | } else { | |
1276 | u->last_reg = rtime; | |
1277 | bch_uuid_write(c); | |
1278 | } | |
1279 | ||
1280 | bcache_device_attach(&dc->disk, c, u - c->uuids); | |
cafe5635 KO |
1281 | list_move(&dc->list, &c->cached_devs); |
1282 | calc_cached_dev_sectors(c); | |
1283 | ||
cafe5635 KO |
1284 | /* |
1285 | * dc->c must be set before dc->count != 0 - paired with the mb in | |
1286 | * cached_dev_get() | |
1287 | */ | |
eb2b3d03 | 1288 | smp_wmb(); |
3b304d24 | 1289 | refcount_set(&dc->count, 1); |
cafe5635 | 1290 | |
07cc6ef8 EW |
1291 | /* Block writeback thread, but spawn it */ |
1292 | down_write(&dc->writeback_lock); | |
1293 | if (bch_cached_dev_writeback_start(dc)) { | |
1294 | up_write(&dc->writeback_lock); | |
46f5aa88 | 1295 | pr_err("Couldn't start writeback facilities for %s\n", |
633bb2ce | 1296 | dc->disk.disk->disk_name); |
9e5c3535 | 1297 | return -ENOMEM; |
07cc6ef8 | 1298 | } |
9e5c3535 | 1299 | |
cafe5635 KO |
1300 | if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) { |
1301 | atomic_set(&dc->has_dirty, 1); | |
cafe5635 KO |
1302 | bch_writeback_queue(dc); |
1303 | } | |
1304 | ||
2e17a262 TJ |
1305 | bch_sectors_dirty_init(&dc->disk); |
1306 | ||
0b13efec CL |
1307 | ret = bch_cached_dev_run(dc); |
1308 | if (ret && (ret != -EBUSY)) { | |
1309 | up_write(&dc->writeback_lock); | |
5c2a634c CL |
1310 | /* |
1311 | * bch_register_lock is held, bcache_device_stop() is not | |
1312 | * able to be directly called. The kthread and kworker | |
1313 | * created previously in bch_cached_dev_writeback_start() | |
1314 | * have to be stopped manually here. | |
1315 | */ | |
1316 | kthread_stop(dc->writeback_thread); | |
1317 | cancel_writeback_rate_update_dwork(dc); | |
0f5cd781 | 1318 | pr_err("Couldn't run cached device %pg\n", dc->bdev); |
0b13efec CL |
1319 | return ret; |
1320 | } | |
1321 | ||
ee668506 | 1322 | bcache_device_link(&dc->disk, c, "bdev"); |
ea8c5356 | 1323 | atomic_inc(&c->attached_dev_nr); |
cafe5635 | 1324 | |
5342fd42 CL |
1325 | if (bch_has_feature_obso_large_bucket(&(c->cache->sb))) { |
1326 | pr_err("The obsoleted large bucket layout is unsupported, set the bcache device into read-only\n"); | |
1327 | pr_err("Please update to the latest bcache-tools to create the cache device\n"); | |
1328 | set_disk_ro(dc->disk.disk, 1); | |
1329 | } | |
1330 | ||
07cc6ef8 EW |
1331 | /* Allow the writeback thread to proceed */ |
1332 | up_write(&dc->writeback_lock); | |
1333 | ||
0f5cd781 CH |
1334 | pr_info("Caching %pg as %s on set %pU\n", |
1335 | dc->bdev, | |
6e916a7e | 1336 | dc->disk.disk->disk_name, |
1132e56e | 1337 | dc->disk.c->set_uuid); |
cafe5635 KO |
1338 | return 0; |
1339 | } | |
1340 | ||
2d17456e | 1341 | /* when dc->disk.kobj released */ |
cafe5635 KO |
1342 | void bch_cached_dev_release(struct kobject *kobj) |
1343 | { | |
1344 | struct cached_dev *dc = container_of(kobj, struct cached_dev, | |
1345 | disk.kobj); | |
1346 | kfree(dc); | |
1347 | module_put(THIS_MODULE); | |
1348 | } | |
1349 | ||
1350 | static void cached_dev_free(struct closure *cl) | |
1351 | { | |
1352 | struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl); | |
1353 | ||
3fd47bfe CL |
1354 | if (test_and_clear_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags)) |
1355 | cancel_writeback_rate_update_dwork(dc); | |
1356 | ||
a664d0f0 SP |
1357 | if (!IS_ERR_OR_NULL(dc->writeback_thread)) |
1358 | kthread_stop(dc->writeback_thread); | |
0f0709e6 CL |
1359 | if (!IS_ERR_OR_NULL(dc->status_update_thread)) |
1360 | kthread_stop(dc->status_update_thread); | |
cafe5635 | 1361 | |
80265d8d CL |
1362 | mutex_lock(&bch_register_lock); |
1363 | ||
b75f4aed | 1364 | if (atomic_read(&dc->running)) { |
f59fce84 | 1365 | bd_unlink_disk_holder(dc->bdev, dc->disk.disk); |
b75f4aed CH |
1366 | del_gendisk(dc->disk.disk); |
1367 | } | |
cafe5635 KO |
1368 | bcache_device_free(&dc->disk); |
1369 | list_del(&dc->list); | |
1370 | ||
1371 | mutex_unlock(&bch_register_lock); | |
1372 | ||
475389ae CH |
1373 | if (dc->sb_disk) |
1374 | put_page(virt_to_page(dc->sb_disk)); | |
e8547d42 | 1375 | |
0781c874 | 1376 | if (!IS_ERR_OR_NULL(dc->bdev)) |
cafe5635 | 1377 | blkdev_put(dc->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
cafe5635 KO |
1378 | |
1379 | wake_up(&unregister_wait); | |
1380 | ||
1381 | kobject_put(&dc->disk.kobj); | |
1382 | } | |
1383 | ||
1384 | static void cached_dev_flush(struct closure *cl) | |
1385 | { | |
1386 | struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl); | |
1387 | struct bcache_device *d = &dc->disk; | |
1388 | ||
c9502ea4 | 1389 | mutex_lock(&bch_register_lock); |
c4d951dd | 1390 | bcache_device_unlink(d); |
c9502ea4 KO |
1391 | mutex_unlock(&bch_register_lock); |
1392 | ||
cafe5635 KO |
1393 | bch_cache_accounting_destroy(&dc->accounting); |
1394 | kobject_del(&d->kobj); | |
1395 | ||
1396 | continue_at(cl, cached_dev_free, system_wq); | |
1397 | } | |
1398 | ||
6f10f7d1 | 1399 | static int cached_dev_init(struct cached_dev *dc, unsigned int block_size) |
cafe5635 | 1400 | { |
f59fce84 | 1401 | int ret; |
cafe5635 | 1402 | struct io *io; |
f59fce84 | 1403 | struct request_queue *q = bdev_get_queue(dc->bdev); |
cafe5635 KO |
1404 | |
1405 | __module_get(THIS_MODULE); | |
1406 | INIT_LIST_HEAD(&dc->list); | |
f59fce84 KO |
1407 | closure_init(&dc->disk.cl, NULL); |
1408 | set_closure_fn(&dc->disk.cl, cached_dev_flush, system_wq); | |
cafe5635 | 1409 | kobject_init(&dc->disk.kobj, &bch_cached_dev_ktype); |
cafe5635 | 1410 | INIT_WORK(&dc->detach, cached_dev_detach_finish); |
cb7a583e | 1411 | sema_init(&dc->sb_write_mutex, 1); |
f59fce84 KO |
1412 | INIT_LIST_HEAD(&dc->io_lru); |
1413 | spin_lock_init(&dc->io_lock); | |
1414 | bch_cache_accounting_init(&dc->accounting, &dc->disk.cl); | |
cafe5635 | 1415 | |
cafe5635 KO |
1416 | dc->sequential_cutoff = 4 << 20; |
1417 | ||
cafe5635 KO |
1418 | for (io = dc->io; io < dc->io + RECENT_IO; io++) { |
1419 | list_add(&io->lru, &dc->io_lru); | |
1420 | hlist_add_head(&io->hash, dc->io_hash + RECENT_IO); | |
1421 | } | |
1422 | ||
c78afc62 KO |
1423 | dc->disk.stripe_size = q->limits.io_opt >> 9; |
1424 | ||
1425 | if (dc->disk.stripe_size) | |
1426 | dc->partial_stripes_expensive = | |
1427 | q->limits.raid_partial_stripes_expensive; | |
1428 | ||
279afbad | 1429 | ret = bcache_device_init(&dc->disk, block_size, |
a782483c | 1430 | bdev_nr_sectors(dc->bdev) - dc->sb.data_offset, |
c62b37d9 | 1431 | dc->bdev, &bcache_cached_ops); |
f59fce84 KO |
1432 | if (ret) |
1433 | return ret; | |
1434 | ||
5d4ce78b CH |
1435 | blk_queue_io_opt(dc->disk.disk->queue, |
1436 | max(queue_io_opt(dc->disk.disk->queue), queue_io_opt(q))); | |
f59fce84 | 1437 | |
c7b7bd07 CL |
1438 | atomic_set(&dc->io_errors, 0); |
1439 | dc->io_disable = false; | |
1440 | dc->error_limit = DEFAULT_CACHED_DEV_ERROR_LIMIT; | |
7e027ca4 CL |
1441 | /* default to auto */ |
1442 | dc->stop_when_cache_set_failed = BCH_CACHED_DEV_STOP_AUTO; | |
1443 | ||
f59fce84 KO |
1444 | bch_cached_dev_request_init(dc); |
1445 | bch_cached_dev_writeback_init(dc); | |
cafe5635 | 1446 | return 0; |
cafe5635 KO |
1447 | } |
1448 | ||
1449 | /* Cached device - bcache superblock */ | |
1450 | ||
cfa0c56d | 1451 | static int register_bdev(struct cache_sb *sb, struct cache_sb_disk *sb_disk, |
cafe5635 KO |
1452 | struct block_device *bdev, |
1453 | struct cached_dev *dc) | |
1454 | { | |
cafe5635 | 1455 | const char *err = "cannot allocate memory"; |
cafe5635 | 1456 | struct cache_set *c; |
0b13efec | 1457 | int ret = -ENOMEM; |
cafe5635 | 1458 | |
cafe5635 | 1459 | memcpy(&dc->sb, sb, sizeof(struct cache_sb)); |
cafe5635 KO |
1460 | dc->bdev = bdev; |
1461 | dc->bdev->bd_holder = dc; | |
475389ae | 1462 | dc->sb_disk = sb_disk; |
6e916a7e | 1463 | |
f59fce84 KO |
1464 | if (cached_dev_init(dc, sb->block_size << 9)) |
1465 | goto err; | |
cafe5635 KO |
1466 | |
1467 | err = "error creating kobject"; | |
8d65269f | 1468 | if (kobject_add(&dc->disk.kobj, bdev_kobj(bdev), "bcache")) |
cafe5635 KO |
1469 | goto err; |
1470 | if (bch_cache_accounting_add_kobjs(&dc->accounting, &dc->disk.kobj)) | |
1471 | goto err; | |
1472 | ||
0f5cd781 | 1473 | pr_info("registered backing device %pg\n", dc->bdev); |
f59fce84 | 1474 | |
cafe5635 | 1475 | list_add(&dc->list, &uncached_devices); |
e57fd746 | 1476 | /* attach to a matched cache set if it exists */ |
cafe5635 | 1477 | list_for_each_entry(c, &bch_cache_sets, list) |
73ac105b | 1478 | bch_cached_dev_attach(dc, c, NULL); |
cafe5635 KO |
1479 | |
1480 | if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE || | |
0b13efec CL |
1481 | BDEV_STATE(&dc->sb) == BDEV_STATE_STALE) { |
1482 | err = "failed to run cached device"; | |
1483 | ret = bch_cached_dev_run(dc); | |
1484 | if (ret) | |
1485 | goto err; | |
1486 | } | |
cafe5635 | 1487 | |
88c12d42 | 1488 | return 0; |
cafe5635 | 1489 | err: |
0f5cd781 | 1490 | pr_notice("error %pg: %s\n", dc->bdev, err); |
f59fce84 | 1491 | bcache_device_stop(&dc->disk); |
0b13efec | 1492 | return ret; |
cafe5635 KO |
1493 | } |
1494 | ||
1495 | /* Flash only volumes */ | |
1496 | ||
2d17456e | 1497 | /* When d->kobj released */ |
cafe5635 KO |
1498 | void bch_flash_dev_release(struct kobject *kobj) |
1499 | { | |
1500 | struct bcache_device *d = container_of(kobj, struct bcache_device, | |
1501 | kobj); | |
1502 | kfree(d); | |
1503 | } | |
1504 | ||
1505 | static void flash_dev_free(struct closure *cl) | |
1506 | { | |
1507 | struct bcache_device *d = container_of(cl, struct bcache_device, cl); | |
1fae7cf0 | 1508 | |
e5112201 | 1509 | mutex_lock(&bch_register_lock); |
99a27d59 TJ |
1510 | atomic_long_sub(bcache_dev_sectors_dirty(d), |
1511 | &d->c->flash_dev_dirty_sectors); | |
b75f4aed | 1512 | del_gendisk(d->disk); |
cafe5635 | 1513 | bcache_device_free(d); |
e5112201 | 1514 | mutex_unlock(&bch_register_lock); |
cafe5635 KO |
1515 | kobject_put(&d->kobj); |
1516 | } | |
1517 | ||
1518 | static void flash_dev_flush(struct closure *cl) | |
1519 | { | |
1520 | struct bcache_device *d = container_of(cl, struct bcache_device, cl); | |
1521 | ||
e5112201 | 1522 | mutex_lock(&bch_register_lock); |
ee668506 | 1523 | bcache_device_unlink(d); |
e5112201 | 1524 | mutex_unlock(&bch_register_lock); |
cafe5635 KO |
1525 | kobject_del(&d->kobj); |
1526 | continue_at(cl, flash_dev_free, system_wq); | |
1527 | } | |
1528 | ||
1529 | static int flash_dev_run(struct cache_set *c, struct uuid_entry *u) | |
1530 | { | |
2961c3bb | 1531 | int err = -ENOMEM; |
cafe5635 KO |
1532 | struct bcache_device *d = kzalloc(sizeof(struct bcache_device), |
1533 | GFP_KERNEL); | |
1534 | if (!d) | |
2961c3bb | 1535 | goto err_ret; |
cafe5635 KO |
1536 | |
1537 | closure_init(&d->cl, NULL); | |
1538 | set_closure_fn(&d->cl, flash_dev_flush, system_wq); | |
1539 | ||
1540 | kobject_init(&d->kobj, &bch_flash_dev_ktype); | |
1541 | ||
4e1ebae3 | 1542 | if (bcache_device_init(d, block_bytes(c->cache), u->sectors, |
c62b37d9 | 1543 | NULL, &bcache_flash_ops)) |
cafe5635 KO |
1544 | goto err; |
1545 | ||
1546 | bcache_device_attach(d, c, u - c->uuids); | |
175206cf | 1547 | bch_sectors_dirty_init(d); |
cafe5635 | 1548 | bch_flash_dev_request_init(d); |
2961c3bb LC |
1549 | err = add_disk(d->disk); |
1550 | if (err) | |
1551 | goto err; | |
cafe5635 | 1552 | |
2961c3bb LC |
1553 | err = kobject_add(&d->kobj, &disk_to_dev(d->disk)->kobj, "bcache"); |
1554 | if (err) | |
cafe5635 KO |
1555 | goto err; |
1556 | ||
1557 | bcache_device_link(d, c, "volume"); | |
1558 | ||
5342fd42 CL |
1559 | if (bch_has_feature_obso_large_bucket(&c->cache->sb)) { |
1560 | pr_err("The obsoleted large bucket layout is unsupported, set the bcache device into read-only\n"); | |
1561 | pr_err("Please update to the latest bcache-tools to create the cache device\n"); | |
1562 | set_disk_ro(d->disk, 1); | |
1563 | } | |
1564 | ||
cafe5635 KO |
1565 | return 0; |
1566 | err: | |
1567 | kobject_put(&d->kobj); | |
2961c3bb LC |
1568 | err_ret: |
1569 | return err; | |
cafe5635 KO |
1570 | } |
1571 | ||
1572 | static int flash_devs_run(struct cache_set *c) | |
1573 | { | |
1574 | int ret = 0; | |
1575 | struct uuid_entry *u; | |
1576 | ||
1577 | for (u = c->uuids; | |
02aa8a8b | 1578 | u < c->uuids + c->nr_uuids && !ret; |
cafe5635 KO |
1579 | u++) |
1580 | if (UUID_FLASH_ONLY(u)) | |
1581 | ret = flash_dev_run(c, u); | |
1582 | ||
1583 | return ret; | |
1584 | } | |
1585 | ||
1586 | int bch_flash_dev_create(struct cache_set *c, uint64_t size) | |
1587 | { | |
1588 | struct uuid_entry *u; | |
1589 | ||
1590 | if (test_bit(CACHE_SET_STOPPING, &c->flags)) | |
1591 | return -EINTR; | |
1592 | ||
bf0c55c9 SP |
1593 | if (!test_bit(CACHE_SET_RUNNING, &c->flags)) |
1594 | return -EPERM; | |
1595 | ||
cafe5635 KO |
1596 | u = uuid_find_empty(c); |
1597 | if (!u) { | |
46f5aa88 | 1598 | pr_err("Can't create volume, no room for UUID\n"); |
cafe5635 KO |
1599 | return -EINVAL; |
1600 | } | |
1601 | ||
1602 | get_random_bytes(u->uuid, 16); | |
1603 | memset(u->label, 0, 32); | |
75cbb3f1 | 1604 | u->first_reg = u->last_reg = cpu_to_le32((u32)ktime_get_real_seconds()); |
cafe5635 KO |
1605 | |
1606 | SET_UUID_FLASH_ONLY(u, 1); | |
1607 | u->sectors = size >> 9; | |
1608 | ||
1609 | bch_uuid_write(c); | |
1610 | ||
1611 | return flash_dev_run(c, u); | |
1612 | } | |
1613 | ||
c7b7bd07 CL |
1614 | bool bch_cached_dev_error(struct cached_dev *dc) |
1615 | { | |
c7b7bd07 CL |
1616 | if (!dc || test_bit(BCACHE_DEV_CLOSING, &dc->disk.flags)) |
1617 | return false; | |
1618 | ||
1619 | dc->io_disable = true; | |
1620 | /* make others know io_disable is true earlier */ | |
1621 | smp_mb(); | |
1622 | ||
0f5cd781 CH |
1623 | pr_err("stop %s: too many IO errors on backing device %pg\n", |
1624 | dc->disk.disk->disk_name, dc->bdev); | |
c7b7bd07 CL |
1625 | |
1626 | bcache_device_stop(&dc->disk); | |
1627 | return true; | |
1628 | } | |
1629 | ||
cafe5635 KO |
1630 | /* Cache set */ |
1631 | ||
1632 | __printf(2, 3) | |
1633 | bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...) | |
1634 | { | |
46f5aa88 | 1635 | struct va_format vaf; |
cafe5635 KO |
1636 | va_list args; |
1637 | ||
77c320eb KO |
1638 | if (c->on_error != ON_ERROR_PANIC && |
1639 | test_bit(CACHE_SET_STOPPING, &c->flags)) | |
cafe5635 KO |
1640 | return false; |
1641 | ||
771f393e | 1642 | if (test_and_set_bit(CACHE_SET_IO_DISABLE, &c->flags)) |
46f5aa88 | 1643 | pr_info("CACHE_SET_IO_DISABLE already set\n"); |
771f393e | 1644 | |
3be11dba CL |
1645 | /* |
1646 | * XXX: we can be called from atomic context | |
1647 | * acquire_console_sem(); | |
1648 | */ | |
cafe5635 | 1649 | |
cafe5635 | 1650 | va_start(args, fmt); |
cafe5635 | 1651 | |
46f5aa88 JP |
1652 | vaf.fmt = fmt; |
1653 | vaf.va = &args; | |
1654 | ||
1655 | pr_err("error on %pU: %pV, disabling caching\n", | |
1132e56e | 1656 | c->set_uuid, &vaf); |
46f5aa88 JP |
1657 | |
1658 | va_end(args); | |
cafe5635 | 1659 | |
77c320eb KO |
1660 | if (c->on_error == ON_ERROR_PANIC) |
1661 | panic("panic forced after error\n"); | |
1662 | ||
cafe5635 KO |
1663 | bch_cache_set_unregister(c); |
1664 | return true; | |
1665 | } | |
1666 | ||
2d17456e | 1667 | /* When c->kobj released */ |
cafe5635 KO |
1668 | void bch_cache_set_release(struct kobject *kobj) |
1669 | { | |
1670 | struct cache_set *c = container_of(kobj, struct cache_set, kobj); | |
1fae7cf0 | 1671 | |
cafe5635 KO |
1672 | kfree(c); |
1673 | module_put(THIS_MODULE); | |
1674 | } | |
1675 | ||
1676 | static void cache_set_free(struct closure *cl) | |
1677 | { | |
1678 | struct cache_set *c = container_of(cl, struct cache_set, cl); | |
1679 | struct cache *ca; | |
cafe5635 | 1680 | |
ae171023 | 1681 | debugfs_remove(c->debug); |
cafe5635 KO |
1682 | |
1683 | bch_open_buckets_free(c); | |
1684 | bch_btree_cache_free(c); | |
1685 | bch_journal_free(c); | |
1686 | ||
a4b732a2 | 1687 | mutex_lock(&bch_register_lock); |
4a784266 CL |
1688 | bch_bset_sort_state_free(&c->sort); |
1689 | free_pages((unsigned long) c->uuids, ilog2(meta_bucket_pages(&c->cache->sb))); | |
1690 | ||
08fdb2cd CL |
1691 | ca = c->cache; |
1692 | if (ca) { | |
1693 | ca->set = NULL; | |
1694 | c->cache = NULL; | |
1695 | kobject_put(&ca->kobj); | |
1696 | } | |
cafe5635 | 1697 | |
cafe5635 | 1698 | |
da415a09 NS |
1699 | if (c->moving_gc_wq) |
1700 | destroy_workqueue(c->moving_gc_wq); | |
d19936a2 KO |
1701 | bioset_exit(&c->bio_split); |
1702 | mempool_exit(&c->fill_iter); | |
1703 | mempool_exit(&c->bio_meta); | |
1704 | mempool_exit(&c->search); | |
cafe5635 KO |
1705 | kfree(c->devices); |
1706 | ||
cafe5635 KO |
1707 | list_del(&c->list); |
1708 | mutex_unlock(&bch_register_lock); | |
1709 | ||
1132e56e | 1710 | pr_info("Cache set %pU unregistered\n", c->set_uuid); |
cafe5635 KO |
1711 | wake_up(&unregister_wait); |
1712 | ||
1713 | closure_debug_destroy(&c->cl); | |
1714 | kobject_put(&c->kobj); | |
1715 | } | |
1716 | ||
1717 | static void cache_set_flush(struct closure *cl) | |
1718 | { | |
1719 | struct cache_set *c = container_of(cl, struct cache_set, caching); | |
08fdb2cd | 1720 | struct cache *ca = c->cache; |
cafe5635 | 1721 | struct btree *b; |
cafe5635 KO |
1722 | |
1723 | bch_cache_accounting_destroy(&c->accounting); | |
1724 | ||
1725 | kobject_put(&c->internal); | |
1726 | kobject_del(&c->kobj); | |
1727 | ||
b387e9b5 | 1728 | if (!IS_ERR_OR_NULL(c->gc_thread)) |
72a44517 KO |
1729 | kthread_stop(c->gc_thread); |
1730 | ||
cafe5635 KO |
1731 | if (!IS_ERR_OR_NULL(c->root)) |
1732 | list_add(&c->root->list, &c->btree_cache); | |
1733 | ||
e6dcbd3e CL |
1734 | /* |
1735 | * Avoid flushing cached nodes if cache set is retiring | |
1736 | * due to too many I/O errors detected. | |
1737 | */ | |
1738 | if (!test_bit(CACHE_SET_IO_DISABLE, &c->flags)) | |
1739 | list_for_each_entry(b, &c->btree_cache, list) { | |
1740 | mutex_lock(&b->write_lock); | |
1741 | if (btree_node_dirty(b)) | |
1742 | __bch_btree_node_write(b, NULL); | |
1743 | mutex_unlock(&b->write_lock); | |
1744 | } | |
cafe5635 | 1745 | |
08fdb2cd CL |
1746 | if (ca->alloc_thread) |
1747 | kthread_stop(ca->alloc_thread); | |
79826c35 | 1748 | |
5b1016e6 KO |
1749 | if (c->journal.cur) { |
1750 | cancel_delayed_work_sync(&c->journal.work); | |
1751 | /* flush last journal entry if needed */ | |
1752 | c->journal.work.work.func(&c->journal.work.work); | |
1753 | } | |
dabb4433 | 1754 | |
cafe5635 KO |
1755 | closure_return(cl); |
1756 | } | |
1757 | ||
7e027ca4 CL |
1758 | /* |
1759 | * This function is only called when CACHE_SET_IO_DISABLE is set, which means | |
1760 | * cache set is unregistering due to too many I/O errors. In this condition, | |
1761 | * the bcache device might be stopped, it depends on stop_when_cache_set_failed | |
1762 | * value and whether the broken cache has dirty data: | |
1763 | * | |
1764 | * dc->stop_when_cache_set_failed dc->has_dirty stop bcache device | |
1765 | * BCH_CACHED_STOP_AUTO 0 NO | |
1766 | * BCH_CACHED_STOP_AUTO 1 YES | |
1767 | * BCH_CACHED_DEV_STOP_ALWAYS 0 YES | |
1768 | * BCH_CACHED_DEV_STOP_ALWAYS 1 YES | |
1769 | * | |
1770 | * The expected behavior is, if stop_when_cache_set_failed is configured to | |
1771 | * "auto" via sysfs interface, the bcache device will not be stopped if the | |
1772 | * backing device is clean on the broken cache device. | |
1773 | */ | |
1774 | static void conditional_stop_bcache_device(struct cache_set *c, | |
1775 | struct bcache_device *d, | |
1776 | struct cached_dev *dc) | |
1777 | { | |
1778 | if (dc->stop_when_cache_set_failed == BCH_CACHED_DEV_STOP_ALWAYS) { | |
46f5aa88 | 1779 | pr_warn("stop_when_cache_set_failed of %s is \"always\", stop it for failed cache set %pU.\n", |
1132e56e | 1780 | d->disk->disk_name, c->set_uuid); |
7e027ca4 CL |
1781 | bcache_device_stop(d); |
1782 | } else if (atomic_read(&dc->has_dirty)) { | |
1783 | /* | |
1784 | * dc->stop_when_cache_set_failed == BCH_CACHED_STOP_AUTO | |
1785 | * and dc->has_dirty == 1 | |
1786 | */ | |
46f5aa88 | 1787 | pr_warn("stop_when_cache_set_failed of %s is \"auto\" and cache is dirty, stop it to avoid potential data corruption.\n", |
7e027ca4 | 1788 | d->disk->disk_name); |
e8cf978d CIK |
1789 | /* |
1790 | * There might be a small time gap that cache set is | |
1791 | * released but bcache device is not. Inside this time | |
1792 | * gap, regular I/O requests will directly go into | |
1793 | * backing device as no cache set attached to. This | |
1794 | * behavior may also introduce potential inconsistence | |
1795 | * data in writeback mode while cache is dirty. | |
1796 | * Therefore before calling bcache_device_stop() due | |
1797 | * to a broken cache device, dc->io_disable should be | |
1798 | * explicitly set to true. | |
1799 | */ | |
1800 | dc->io_disable = true; | |
1801 | /* make others know io_disable is true earlier */ | |
1802 | smp_mb(); | |
1803 | bcache_device_stop(d); | |
7e027ca4 CL |
1804 | } else { |
1805 | /* | |
1806 | * dc->stop_when_cache_set_failed == BCH_CACHED_STOP_AUTO | |
1807 | * and dc->has_dirty == 0 | |
1808 | */ | |
46f5aa88 | 1809 | pr_warn("stop_when_cache_set_failed of %s is \"auto\" and cache is clean, keep it alive.\n", |
7e027ca4 CL |
1810 | d->disk->disk_name); |
1811 | } | |
1812 | } | |
1813 | ||
cafe5635 KO |
1814 | static void __cache_set_unregister(struct closure *cl) |
1815 | { | |
1816 | struct cache_set *c = container_of(cl, struct cache_set, caching); | |
5caa52af | 1817 | struct cached_dev *dc; |
7e027ca4 | 1818 | struct bcache_device *d; |
cafe5635 KO |
1819 | size_t i; |
1820 | ||
1821 | mutex_lock(&bch_register_lock); | |
1822 | ||
7e027ca4 CL |
1823 | for (i = 0; i < c->devices_max_used; i++) { |
1824 | d = c->devices[i]; | |
1825 | if (!d) | |
1826 | continue; | |
1827 | ||
1828 | if (!UUID_FLASH_ONLY(&c->uuids[i]) && | |
1829 | test_bit(CACHE_SET_UNREGISTERING, &c->flags)) { | |
1830 | dc = container_of(d, struct cached_dev, disk); | |
1831 | bch_cached_dev_detach(dc); | |
1832 | if (test_bit(CACHE_SET_IO_DISABLE, &c->flags)) | |
1833 | conditional_stop_bcache_device(c, d, dc); | |
1834 | } else { | |
1835 | bcache_device_stop(d); | |
5caa52af | 1836 | } |
7e027ca4 | 1837 | } |
cafe5635 KO |
1838 | |
1839 | mutex_unlock(&bch_register_lock); | |
1840 | ||
1841 | continue_at(cl, cache_set_flush, system_wq); | |
1842 | } | |
1843 | ||
1844 | void bch_cache_set_stop(struct cache_set *c) | |
1845 | { | |
1846 | if (!test_and_set_bit(CACHE_SET_STOPPING, &c->flags)) | |
63d63b51 | 1847 | /* closure_fn set to __cache_set_unregister() */ |
cafe5635 KO |
1848 | closure_queue(&c->caching); |
1849 | } | |
1850 | ||
1851 | void bch_cache_set_unregister(struct cache_set *c) | |
1852 | { | |
1853 | set_bit(CACHE_SET_UNREGISTERING, &c->flags); | |
1854 | bch_cache_set_stop(c); | |
1855 | } | |
1856 | ||
de1fafab CL |
1857 | #define alloc_meta_bucket_pages(gfp, sb) \ |
1858 | ((void *) __get_free_pages(__GFP_ZERO|__GFP_COMP|gfp, ilog2(meta_bucket_pages(sb)))) | |
cafe5635 KO |
1859 | |
1860 | struct cache_set *bch_cache_set_alloc(struct cache_sb *sb) | |
1861 | { | |
1862 | int iter_size; | |
4a784266 | 1863 | struct cache *ca = container_of(sb, struct cache, sb); |
cafe5635 | 1864 | struct cache_set *c = kzalloc(sizeof(struct cache_set), GFP_KERNEL); |
1fae7cf0 | 1865 | |
cafe5635 KO |
1866 | if (!c) |
1867 | return NULL; | |
1868 | ||
1869 | __module_get(THIS_MODULE); | |
1870 | closure_init(&c->cl, NULL); | |
1871 | set_closure_fn(&c->cl, cache_set_free, system_wq); | |
1872 | ||
1873 | closure_init(&c->caching, &c->cl); | |
1874 | set_closure_fn(&c->caching, __cache_set_unregister, system_wq); | |
1875 | ||
1876 | /* Maybe create continue_at_noreturn() and use it here? */ | |
1877 | closure_set_stopped(&c->cl); | |
1878 | closure_put(&c->cl); | |
1879 | ||
1880 | kobject_init(&c->kobj, &bch_cache_set_ktype); | |
1881 | kobject_init(&c->internal, &bch_cache_set_internal_ktype); | |
1882 | ||
1883 | bch_cache_accounting_init(&c->accounting, &c->cl); | |
1884 | ||
1132e56e | 1885 | memcpy(c->set_uuid, sb->set_uuid, 16); |
d721a43f | 1886 | |
4a784266 CL |
1887 | c->cache = ca; |
1888 | c->cache->set = c; | |
cafe5635 KO |
1889 | c->bucket_bits = ilog2(sb->bucket_size); |
1890 | c->block_bits = ilog2(sb->block_size); | |
4a784266 | 1891 | c->nr_uuids = meta_bucket_bytes(sb) / sizeof(struct uuid_entry); |
2831231d | 1892 | c->devices_max_used = 0; |
ea8c5356 | 1893 | atomic_set(&c->attached_dev_nr, 0); |
4a784266 | 1894 | c->btree_pages = meta_bucket_pages(sb); |
cafe5635 KO |
1895 | if (c->btree_pages > BTREE_MAX_PAGES) |
1896 | c->btree_pages = max_t(int, c->btree_pages / 4, | |
1897 | BTREE_MAX_PAGES); | |
1898 | ||
cb7a583e | 1899 | sema_init(&c->sb_write_mutex, 1); |
e8e1d468 | 1900 | mutex_init(&c->bucket_lock); |
0a63b66d | 1901 | init_waitqueue_head(&c->btree_cache_wait); |
34cf78bf | 1902 | spin_lock_init(&c->btree_cannibalize_lock); |
35fcd848 | 1903 | init_waitqueue_head(&c->bucket_wait); |
be628be0 | 1904 | init_waitqueue_head(&c->gc_wait); |
cb7a583e | 1905 | sema_init(&c->uuid_write_mutex, 1); |
65d22e91 | 1906 | |
65d22e91 KO |
1907 | spin_lock_init(&c->btree_gc_time.lock); |
1908 | spin_lock_init(&c->btree_split_time.lock); | |
1909 | spin_lock_init(&c->btree_read_time.lock); | |
e8e1d468 | 1910 | |
cafe5635 KO |
1911 | bch_moving_init_cache_set(c); |
1912 | ||
1913 | INIT_LIST_HEAD(&c->list); | |
1914 | INIT_LIST_HEAD(&c->cached_devs); | |
1915 | INIT_LIST_HEAD(&c->btree_cache); | |
1916 | INIT_LIST_HEAD(&c->btree_cache_freeable); | |
1917 | INIT_LIST_HEAD(&c->btree_cache_freed); | |
1918 | INIT_LIST_HEAD(&c->data_buckets); | |
1919 | ||
6907dc49 | 1920 | iter_size = ((meta_bucket_pages(sb) * PAGE_SECTORS) / sb->block_size + 1) * |
cafe5635 KO |
1921 | sizeof(struct btree_iter_set); |
1922 | ||
a42d3c64 CL |
1923 | c->devices = kcalloc(c->nr_uuids, sizeof(void *), GFP_KERNEL); |
1924 | if (!c->devices) | |
1925 | goto err; | |
1926 | ||
1927 | if (mempool_init_slab_pool(&c->search, 32, bch_search_cache)) | |
1928 | goto err; | |
1929 | ||
1930 | if (mempool_init_kmalloc_pool(&c->bio_meta, 2, | |
1931 | sizeof(struct bbio) + | |
4a784266 | 1932 | sizeof(struct bio_vec) * meta_bucket_pages(sb))) |
a42d3c64 CL |
1933 | goto err; |
1934 | ||
1935 | if (mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size)) | |
1936 | goto err; | |
1937 | ||
1938 | if (bioset_init(&c->bio_split, 4, offsetof(struct bbio, bio), | |
faa8e2c4 | 1939 | BIOSET_NEED_RESCUER)) |
a42d3c64 CL |
1940 | goto err; |
1941 | ||
4a784266 | 1942 | c->uuids = alloc_meta_bucket_pages(GFP_KERNEL, sb); |
a42d3c64 CL |
1943 | if (!c->uuids) |
1944 | goto err; | |
1945 | ||
1946 | c->moving_gc_wq = alloc_workqueue("bcache_gc", WQ_MEM_RECLAIM, 0); | |
1947 | if (!c->moving_gc_wq) | |
1948 | goto err; | |
1949 | ||
1950 | if (bch_journal_alloc(c)) | |
1951 | goto err; | |
1952 | ||
1953 | if (bch_btree_cache_alloc(c)) | |
1954 | goto err; | |
1955 | ||
1956 | if (bch_open_buckets_alloc(c)) | |
1957 | goto err; | |
1958 | ||
1959 | if (bch_bset_sort_state_init(&c->sort, ilog2(c->btree_pages))) | |
cafe5635 KO |
1960 | goto err; |
1961 | ||
cafe5635 KO |
1962 | c->congested_read_threshold_us = 2000; |
1963 | c->congested_write_threshold_us = 20000; | |
7ba0d830 | 1964 | c->error_limit = DEFAULT_IO_ERROR_LIMIT; |
c5fcdedc | 1965 | c->idle_max_writeback_rate_enabled = 1; |
771f393e | 1966 | WARN_ON(test_and_clear_bit(CACHE_SET_IO_DISABLE, &c->flags)); |
cafe5635 KO |
1967 | |
1968 | return c; | |
1969 | err: | |
1970 | bch_cache_set_unregister(c); | |
1971 | return NULL; | |
1972 | } | |
1973 | ||
ce3e4cfb | 1974 | static int run_cache_set(struct cache_set *c) |
cafe5635 KO |
1975 | { |
1976 | const char *err = "cannot allocate memory"; | |
1977 | struct cached_dev *dc, *t; | |
08fdb2cd | 1978 | struct cache *ca = c->cache; |
c18536a7 | 1979 | struct closure cl; |
95f18c9d SW |
1980 | LIST_HEAD(journal); |
1981 | struct journal_replay *l; | |
cafe5635 | 1982 | |
c18536a7 | 1983 | closure_init_stack(&cl); |
cafe5635 | 1984 | |
08fdb2cd | 1985 | c->nbuckets = ca->sb.nbuckets; |
be628be0 | 1986 | set_gc_sectors(c); |
cafe5635 | 1987 | |
6f9414e0 | 1988 | if (CACHE_SYNC(&c->cache->sb)) { |
cafe5635 KO |
1989 | struct bkey *k; |
1990 | struct jset *j; | |
1991 | ||
1992 | err = "cannot allocate memory for journal"; | |
c18536a7 | 1993 | if (bch_journal_read(c, &journal)) |
cafe5635 KO |
1994 | goto err; |
1995 | ||
46f5aa88 | 1996 | pr_debug("btree_journal_read() done\n"); |
cafe5635 KO |
1997 | |
1998 | err = "no journal entries found"; | |
1999 | if (list_empty(&journal)) | |
2000 | goto err; | |
2001 | ||
2002 | j = &list_entry(journal.prev, struct journal_replay, list)->j; | |
2003 | ||
2004 | err = "IO error reading priorities"; | |
08fdb2cd CL |
2005 | if (prio_read(ca, j->prio_bucket[ca->sb.nr_this_dev])) |
2006 | goto err; | |
cafe5635 KO |
2007 | |
2008 | /* | |
2009 | * If prio_read() fails it'll call cache_set_error and we'll | |
2010 | * tear everything down right away, but if we perhaps checked | |
2011 | * sooner we could avoid journal replay. | |
2012 | */ | |
2013 | ||
2014 | k = &j->btree_root; | |
2015 | ||
2016 | err = "bad btree root"; | |
65d45231 | 2017 | if (__bch_btree_ptr_invalid(c, k)) |
cafe5635 KO |
2018 | goto err; |
2019 | ||
2020 | err = "error reading btree root"; | |
b0d30981 CL |
2021 | c->root = bch_btree_node_get(c, NULL, k, |
2022 | j->btree_level, | |
2023 | true, NULL); | |
cafe5635 KO |
2024 | if (IS_ERR_OR_NULL(c->root)) |
2025 | goto err; | |
2026 | ||
2027 | list_del_init(&c->root->list); | |
2028 | rw_unlock(true, c->root); | |
2029 | ||
c18536a7 | 2030 | err = uuid_read(c, j, &cl); |
cafe5635 KO |
2031 | if (err) |
2032 | goto err; | |
2033 | ||
2034 | err = "error in recovery"; | |
c18536a7 | 2035 | if (bch_btree_check(c)) |
cafe5635 KO |
2036 | goto err; |
2037 | ||
2038 | bch_journal_mark(c, &journal); | |
2531d9ee | 2039 | bch_initial_gc_finish(c); |
46f5aa88 | 2040 | pr_debug("btree_check() done\n"); |
cafe5635 KO |
2041 | |
2042 | /* | |
2043 | * bcache_journal_next() can't happen sooner, or | |
2044 | * btree_gc_finish() will give spurious errors about last_gc > | |
2045 | * gc_gen - this is a hack but oh well. | |
2046 | */ | |
2047 | bch_journal_next(&c->journal); | |
2048 | ||
119ba0f8 | 2049 | err = "error starting allocator thread"; |
08fdb2cd CL |
2050 | if (bch_cache_allocator_start(ca)) |
2051 | goto err; | |
cafe5635 KO |
2052 | |
2053 | /* | |
2054 | * First place it's safe to allocate: btree_check() and | |
2055 | * btree_gc_finish() have to run before we have buckets to | |
2056 | * allocate, and bch_bucket_alloc_set() might cause a journal | |
2057 | * entry to be written so bcache_journal_next() has to be called | |
2058 | * first. | |
2059 | * | |
2060 | * If the uuids were in the old format we have to rewrite them | |
2061 | * before the next journal entry is written: | |
2062 | */ | |
2063 | if (j->version < BCACHE_JSET_VERSION_UUID) | |
2064 | __uuid_write(c); | |
2065 | ||
ce3e4cfb CL |
2066 | err = "bcache: replay journal failed"; |
2067 | if (bch_journal_replay(c, &journal)) | |
2068 | goto err; | |
cafe5635 | 2069 | } else { |
08fdb2cd | 2070 | unsigned int j; |
cafe5635 | 2071 | |
08fdb2cd CL |
2072 | pr_notice("invalidating existing data\n"); |
2073 | ca->sb.keys = clamp_t(int, ca->sb.nbuckets >> 7, | |
2074 | 2, SB_JOURNAL_BUCKETS); | |
cafe5635 | 2075 | |
08fdb2cd CL |
2076 | for (j = 0; j < ca->sb.keys; j++) |
2077 | ca->sb.d[j] = ca->sb.first_bucket + j; | |
cafe5635 | 2078 | |
2531d9ee | 2079 | bch_initial_gc_finish(c); |
cafe5635 | 2080 | |
119ba0f8 | 2081 | err = "error starting allocator thread"; |
08fdb2cd CL |
2082 | if (bch_cache_allocator_start(ca)) |
2083 | goto err; | |
cafe5635 KO |
2084 | |
2085 | mutex_lock(&c->bucket_lock); | |
08fdb2cd | 2086 | bch_prio_write(ca, true); |
cafe5635 KO |
2087 | mutex_unlock(&c->bucket_lock); |
2088 | ||
cafe5635 KO |
2089 | err = "cannot allocate new UUID bucket"; |
2090 | if (__uuid_write(c)) | |
72a44517 | 2091 | goto err; |
cafe5635 KO |
2092 | |
2093 | err = "cannot allocate new btree root"; | |
2452cc89 | 2094 | c->root = __bch_btree_node_alloc(c, NULL, 0, true, NULL); |
cafe5635 | 2095 | if (IS_ERR_OR_NULL(c->root)) |
72a44517 | 2096 | goto err; |
cafe5635 | 2097 | |
2a285686 | 2098 | mutex_lock(&c->root->write_lock); |
cafe5635 | 2099 | bkey_copy_key(&c->root->key, &MAX_KEY); |
c18536a7 | 2100 | bch_btree_node_write(c->root, &cl); |
2a285686 | 2101 | mutex_unlock(&c->root->write_lock); |
cafe5635 KO |
2102 | |
2103 | bch_btree_set_root(c->root); | |
2104 | rw_unlock(true, c->root); | |
2105 | ||
2106 | /* | |
2107 | * We don't want to write the first journal entry until | |
2108 | * everything is set up - fortunately journal entries won't be | |
2109 | * written until the SET_CACHE_SYNC() here: | |
2110 | */ | |
6f9414e0 | 2111 | SET_CACHE_SYNC(&c->cache->sb, true); |
cafe5635 KO |
2112 | |
2113 | bch_journal_next(&c->journal); | |
c18536a7 | 2114 | bch_journal_meta(c, &cl); |
cafe5635 KO |
2115 | } |
2116 | ||
72a44517 KO |
2117 | err = "error starting gc thread"; |
2118 | if (bch_gc_thread_start(c)) | |
2119 | goto err; | |
2120 | ||
c18536a7 | 2121 | closure_sync(&cl); |
4a784266 | 2122 | c->cache->sb.last_mount = (u32)ktime_get_real_seconds(); |
cafe5635 KO |
2123 | bcache_write_super(c); |
2124 | ||
5342fd42 CL |
2125 | if (bch_has_feature_obso_large_bucket(&c->cache->sb)) |
2126 | pr_err("Detect obsoleted large bucket layout, all attached bcache device will be read-only\n"); | |
2127 | ||
cafe5635 | 2128 | list_for_each_entry_safe(dc, t, &uncached_devices, list) |
73ac105b | 2129 | bch_cached_dev_attach(dc, c, NULL); |
cafe5635 KO |
2130 | |
2131 | flash_devs_run(c); | |
2132 | ||
bf0c55c9 | 2133 | set_bit(CACHE_SET_RUNNING, &c->flags); |
ce3e4cfb | 2134 | return 0; |
cafe5635 | 2135 | err: |
95f18c9d SW |
2136 | while (!list_empty(&journal)) { |
2137 | l = list_first_entry(&journal, struct journal_replay, list); | |
2138 | list_del(&l->list); | |
2139 | kfree(l); | |
2140 | } | |
2141 | ||
c18536a7 | 2142 | closure_sync(&cl); |
68a53c95 | 2143 | |
c8694948 | 2144 | bch_cache_set_error(c, "%s", err); |
ce3e4cfb CL |
2145 | |
2146 | return -EIO; | |
cafe5635 KO |
2147 | } |
2148 | ||
cafe5635 KO |
2149 | static const char *register_cache_set(struct cache *ca) |
2150 | { | |
2151 | char buf[12]; | |
2152 | const char *err = "cannot allocate memory"; | |
2153 | struct cache_set *c; | |
2154 | ||
2155 | list_for_each_entry(c, &bch_cache_sets, list) | |
1132e56e | 2156 | if (!memcmp(c->set_uuid, ca->sb.set_uuid, 16)) { |
697e2349 | 2157 | if (c->cache) |
cafe5635 KO |
2158 | return "duplicate cache set member"; |
2159 | ||
cafe5635 KO |
2160 | goto found; |
2161 | } | |
2162 | ||
2163 | c = bch_cache_set_alloc(&ca->sb); | |
2164 | if (!c) | |
2165 | return err; | |
2166 | ||
2167 | err = "error creating kobject"; | |
1132e56e | 2168 | if (kobject_add(&c->kobj, bcache_kobj, "%pU", c->set_uuid) || |
cafe5635 KO |
2169 | kobject_add(&c->internal, &c->kobj, "internal")) |
2170 | goto err; | |
2171 | ||
2172 | if (bch_cache_accounting_add_kobjs(&c->accounting, &c->kobj)) | |
2173 | goto err; | |
2174 | ||
2175 | bch_debug_init_cache_set(c); | |
2176 | ||
2177 | list_add(&c->list, &bch_cache_sets); | |
2178 | found: | |
2179 | sprintf(buf, "cache%i", ca->sb.nr_this_dev); | |
2180 | if (sysfs_create_link(&ca->kobj, &c->kobj, "set") || | |
2181 | sysfs_create_link(&c->kobj, &ca->kobj, buf)) | |
2182 | goto err; | |
2183 | ||
d83353b3 | 2184 | kobject_get(&ca->kobj); |
cafe5635 | 2185 | ca->set = c; |
697e2349 | 2186 | ca->set->cache = ca; |
cafe5635 | 2187 | |
697e2349 CL |
2188 | err = "failed to run cache set"; |
2189 | if (run_cache_set(c) < 0) | |
2190 | goto err; | |
cafe5635 KO |
2191 | |
2192 | return NULL; | |
2193 | err: | |
2194 | bch_cache_set_unregister(c); | |
2195 | return err; | |
2196 | } | |
2197 | ||
2198 | /* Cache device */ | |
2199 | ||
2d17456e | 2200 | /* When ca->kobj released */ |
cafe5635 KO |
2201 | void bch_cache_release(struct kobject *kobj) |
2202 | { | |
2203 | struct cache *ca = container_of(kobj, struct cache, kobj); | |
6f10f7d1 | 2204 | unsigned int i; |
cafe5635 | 2205 | |
c9a78332 | 2206 | if (ca->set) { |
697e2349 CL |
2207 | BUG_ON(ca->set->cache != ca); |
2208 | ca->set->cache = NULL; | |
c9a78332 | 2209 | } |
cafe5635 | 2210 | |
c954ac8d | 2211 | free_pages((unsigned long) ca->disk_buckets, ilog2(meta_bucket_pages(&ca->sb))); |
cafe5635 KO |
2212 | kfree(ca->prio_buckets); |
2213 | vfree(ca->buckets); | |
2214 | ||
2215 | free_heap(&ca->heap); | |
cafe5635 | 2216 | free_fifo(&ca->free_inc); |
78365411 KO |
2217 | |
2218 | for (i = 0; i < RESERVE_NR; i++) | |
2219 | free_fifo(&ca->free[i]); | |
cafe5635 | 2220 | |
475389ae CH |
2221 | if (ca->sb_disk) |
2222 | put_page(virt_to_page(ca->sb_disk)); | |
cafe5635 | 2223 | |
0781c874 | 2224 | if (!IS_ERR_OR_NULL(ca->bdev)) |
cafe5635 | 2225 | blkdev_put(ca->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
cafe5635 KO |
2226 | |
2227 | kfree(ca); | |
2228 | module_put(THIS_MODULE); | |
2229 | } | |
2230 | ||
c50d4d5d | 2231 | static int cache_alloc(struct cache *ca) |
cafe5635 KO |
2232 | { |
2233 | size_t free; | |
682811b3 | 2234 | size_t btree_buckets; |
cafe5635 | 2235 | struct bucket *b; |
f6027bca DC |
2236 | int ret = -ENOMEM; |
2237 | const char *err = NULL; | |
cafe5635 | 2238 | |
cafe5635 KO |
2239 | __module_get(THIS_MODULE); |
2240 | kobject_init(&ca->kobj, &bch_cache_ktype); | |
2241 | ||
3a83f467 | 2242 | bio_init(&ca->journal.bio, ca->journal.bio.bi_inline_vecs, 8); |
cafe5635 | 2243 | |
682811b3 TJ |
2244 | /* |
2245 | * when ca->sb.njournal_buckets is not zero, journal exists, | |
2246 | * and in bch_journal_replay(), tree node may split, | |
2247 | * so bucket of RESERVE_BTREE type is needed, | |
2248 | * the worst situation is all journal buckets are valid journal, | |
2249 | * and all the keys need to replay, | |
2250 | * so the number of RESERVE_BTREE type buckets should be as much | |
2251 | * as journal buckets | |
2252 | */ | |
2253 | btree_buckets = ca->sb.njournal_buckets ?: 8; | |
78365411 | 2254 | free = roundup_pow_of_two(ca->sb.nbuckets) >> 10; |
3a646fd7 DC |
2255 | if (!free) { |
2256 | ret = -EPERM; | |
2257 | err = "ca->sb.nbuckets is too small"; | |
2258 | goto err_free; | |
2259 | } | |
cafe5635 | 2260 | |
f6027bca DC |
2261 | if (!init_fifo(&ca->free[RESERVE_BTREE], btree_buckets, |
2262 | GFP_KERNEL)) { | |
2263 | err = "ca->free[RESERVE_BTREE] alloc failed"; | |
2264 | goto err_btree_alloc; | |
2265 | } | |
2266 | ||
2267 | if (!init_fifo_exact(&ca->free[RESERVE_PRIO], prio_buckets(ca), | |
2268 | GFP_KERNEL)) { | |
2269 | err = "ca->free[RESERVE_PRIO] alloc failed"; | |
2270 | goto err_prio_alloc; | |
2271 | } | |
2272 | ||
2273 | if (!init_fifo(&ca->free[RESERVE_MOVINGGC], free, GFP_KERNEL)) { | |
2274 | err = "ca->free[RESERVE_MOVINGGC] alloc failed"; | |
2275 | goto err_movinggc_alloc; | |
2276 | } | |
2277 | ||
2278 | if (!init_fifo(&ca->free[RESERVE_NONE], free, GFP_KERNEL)) { | |
2279 | err = "ca->free[RESERVE_NONE] alloc failed"; | |
2280 | goto err_none_alloc; | |
2281 | } | |
2282 | ||
2283 | if (!init_fifo(&ca->free_inc, free << 2, GFP_KERNEL)) { | |
2284 | err = "ca->free_inc alloc failed"; | |
2285 | goto err_free_inc_alloc; | |
2286 | } | |
2287 | ||
2288 | if (!init_heap(&ca->heap, free << 3, GFP_KERNEL)) { | |
2289 | err = "ca->heap alloc failed"; | |
2290 | goto err_heap_alloc; | |
2291 | } | |
2292 | ||
2293 | ca->buckets = vzalloc(array_size(sizeof(struct bucket), | |
2294 | ca->sb.nbuckets)); | |
2295 | if (!ca->buckets) { | |
2296 | err = "ca->buckets alloc failed"; | |
2297 | goto err_buckets_alloc; | |
2298 | } | |
2299 | ||
2300 | ca->prio_buckets = kzalloc(array3_size(sizeof(uint64_t), | |
2301 | prio_buckets(ca), 2), | |
2302 | GFP_KERNEL); | |
2303 | if (!ca->prio_buckets) { | |
2304 | err = "ca->prio_buckets alloc failed"; | |
2305 | goto err_prio_buckets_alloc; | |
2306 | } | |
2307 | ||
c954ac8d | 2308 | ca->disk_buckets = alloc_meta_bucket_pages(GFP_KERNEL, &ca->sb); |
f6027bca DC |
2309 | if (!ca->disk_buckets) { |
2310 | err = "ca->disk_buckets alloc failed"; | |
2311 | goto err_disk_buckets_alloc; | |
2312 | } | |
cafe5635 KO |
2313 | |
2314 | ca->prio_last_buckets = ca->prio_buckets + prio_buckets(ca); | |
2315 | ||
cafe5635 KO |
2316 | for_each_bucket(b, ca) |
2317 | atomic_set(&b->pin, 0); | |
cafe5635 | 2318 | return 0; |
f6027bca DC |
2319 | |
2320 | err_disk_buckets_alloc: | |
2321 | kfree(ca->prio_buckets); | |
2322 | err_prio_buckets_alloc: | |
2323 | vfree(ca->buckets); | |
2324 | err_buckets_alloc: | |
2325 | free_heap(&ca->heap); | |
2326 | err_heap_alloc: | |
2327 | free_fifo(&ca->free_inc); | |
2328 | err_free_inc_alloc: | |
2329 | free_fifo(&ca->free[RESERVE_NONE]); | |
2330 | err_none_alloc: | |
2331 | free_fifo(&ca->free[RESERVE_MOVINGGC]); | |
2332 | err_movinggc_alloc: | |
2333 | free_fifo(&ca->free[RESERVE_PRIO]); | |
2334 | err_prio_alloc: | |
2335 | free_fifo(&ca->free[RESERVE_BTREE]); | |
2336 | err_btree_alloc: | |
3a646fd7 | 2337 | err_free: |
f6027bca DC |
2338 | module_put(THIS_MODULE); |
2339 | if (err) | |
7e84c215 | 2340 | pr_notice("error %pg: %s\n", ca->bdev, err); |
f6027bca | 2341 | return ret; |
cafe5635 KO |
2342 | } |
2343 | ||
cfa0c56d | 2344 | static int register_cache(struct cache_sb *sb, struct cache_sb_disk *sb_disk, |
c9a78332 | 2345 | struct block_device *bdev, struct cache *ca) |
cafe5635 | 2346 | { |
d9dc1702 | 2347 | const char *err = NULL; /* must be set for any error case */ |
9b299728 | 2348 | int ret = 0; |
cafe5635 | 2349 | |
f59fce84 | 2350 | memcpy(&ca->sb, sb, sizeof(struct cache_sb)); |
cafe5635 KO |
2351 | ca->bdev = bdev; |
2352 | ca->bdev->bd_holder = ca; | |
475389ae | 2353 | ca->sb_disk = sb_disk; |
f59fce84 | 2354 | |
cc40daf9 | 2355 | if (blk_queue_discard(bdev_get_queue(bdev))) |
cafe5635 KO |
2356 | ca->discard = CACHE_DISCARD(&ca->sb); |
2357 | ||
c50d4d5d | 2358 | ret = cache_alloc(ca); |
d9dc1702 | 2359 | if (ret != 0) { |
bb6d355c CL |
2360 | /* |
2361 | * If we failed here, it means ca->kobj is not initialized yet, | |
2362 | * kobject_put() won't be called and there is no chance to | |
2363 | * call blkdev_put() to bdev in bch_cache_release(). So we | |
2364 | * explicitly call blkdev_put() here. | |
2365 | */ | |
cc40daf9 | 2366 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
d9dc1702 EW |
2367 | if (ret == -ENOMEM) |
2368 | err = "cache_alloc(): -ENOMEM"; | |
3a646fd7 DC |
2369 | else if (ret == -EPERM) |
2370 | err = "cache_alloc(): cache device is too small"; | |
d9dc1702 EW |
2371 | else |
2372 | err = "cache_alloc(): unknown error"; | |
f59fce84 | 2373 | goto err; |
d9dc1702 | 2374 | } |
f59fce84 | 2375 | |
8d65269f | 2376 | if (kobject_add(&ca->kobj, bdev_kobj(bdev), "bcache")) { |
9b299728 EW |
2377 | err = "error calling kobject_add"; |
2378 | ret = -ENOMEM; | |
2379 | goto out; | |
2380 | } | |
cafe5635 | 2381 | |
4fa03402 | 2382 | mutex_lock(&bch_register_lock); |
cafe5635 | 2383 | err = register_cache_set(ca); |
4fa03402 KO |
2384 | mutex_unlock(&bch_register_lock); |
2385 | ||
9b299728 EW |
2386 | if (err) { |
2387 | ret = -ENODEV; | |
2388 | goto out; | |
2389 | } | |
cafe5635 | 2390 | |
7e84c215 | 2391 | pr_info("registered cache device %pg\n", ca->bdev); |
9b299728 | 2392 | |
d83353b3 KO |
2393 | out: |
2394 | kobject_put(&ca->kobj); | |
9b299728 | 2395 | |
cafe5635 | 2396 | err: |
9b299728 | 2397 | if (err) |
7e84c215 | 2398 | pr_notice("error %pg: %s\n", ca->bdev, err); |
9b299728 EW |
2399 | |
2400 | return ret; | |
cafe5635 KO |
2401 | } |
2402 | ||
2403 | /* Global interfaces/init */ | |
2404 | ||
fc2d5988 CL |
2405 | static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr, |
2406 | const char *buffer, size_t size); | |
0c277e21 CL |
2407 | static ssize_t bch_pending_bdevs_cleanup(struct kobject *k, |
2408 | struct kobj_attribute *attr, | |
2409 | const char *buffer, size_t size); | |
cafe5635 KO |
2410 | |
2411 | kobj_attribute_write(register, register_bcache); | |
2412 | kobj_attribute_write(register_quiet, register_bcache); | |
0c277e21 | 2413 | kobj_attribute_write(pendings_cleanup, bch_pending_bdevs_cleanup); |
cafe5635 | 2414 | |
4e7b5671 | 2415 | static bool bch_is_open_backing(dev_t dev) |
b3cf37bf | 2416 | { |
a9dd53ad GP |
2417 | struct cache_set *c, *tc; |
2418 | struct cached_dev *dc, *t; | |
2419 | ||
2420 | list_for_each_entry_safe(c, tc, &bch_cache_sets, list) | |
2421 | list_for_each_entry_safe(dc, t, &c->cached_devs, list) | |
4e7b5671 | 2422 | if (dc->bdev->bd_dev == dev) |
a9dd53ad GP |
2423 | return true; |
2424 | list_for_each_entry_safe(dc, t, &uncached_devices, list) | |
4e7b5671 | 2425 | if (dc->bdev->bd_dev == dev) |
a9dd53ad GP |
2426 | return true; |
2427 | return false; | |
2428 | } | |
2429 | ||
4e7b5671 | 2430 | static bool bch_is_open_cache(dev_t dev) |
b3cf37bf | 2431 | { |
a9dd53ad | 2432 | struct cache_set *c, *tc; |
a9dd53ad | 2433 | |
08fdb2cd CL |
2434 | list_for_each_entry_safe(c, tc, &bch_cache_sets, list) { |
2435 | struct cache *ca = c->cache; | |
2436 | ||
4e7b5671 | 2437 | if (ca->bdev->bd_dev == dev) |
08fdb2cd CL |
2438 | return true; |
2439 | } | |
2440 | ||
a9dd53ad GP |
2441 | return false; |
2442 | } | |
2443 | ||
4e7b5671 | 2444 | static bool bch_is_open(dev_t dev) |
b3cf37bf | 2445 | { |
4e7b5671 | 2446 | return bch_is_open_cache(dev) || bch_is_open_backing(dev); |
a9dd53ad GP |
2447 | } |
2448 | ||
9e23ccf8 | 2449 | struct async_reg_args { |
ee4a36f4 | 2450 | struct delayed_work reg_work; |
9e23ccf8 CL |
2451 | char *path; |
2452 | struct cache_sb *sb; | |
2453 | struct cache_sb_disk *sb_disk; | |
2454 | struct block_device *bdev; | |
2455 | }; | |
2456 | ||
2457 | static void register_bdev_worker(struct work_struct *work) | |
2458 | { | |
2459 | int fail = false; | |
2460 | struct async_reg_args *args = | |
ee4a36f4 | 2461 | container_of(work, struct async_reg_args, reg_work.work); |
9e23ccf8 CL |
2462 | struct cached_dev *dc; |
2463 | ||
2464 | dc = kzalloc(sizeof(*dc), GFP_KERNEL); | |
2465 | if (!dc) { | |
2466 | fail = true; | |
2467 | put_page(virt_to_page(args->sb_disk)); | |
2468 | blkdev_put(args->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); | |
2469 | goto out; | |
2470 | } | |
2471 | ||
2472 | mutex_lock(&bch_register_lock); | |
2473 | if (register_bdev(args->sb, args->sb_disk, args->bdev, dc) < 0) | |
2474 | fail = true; | |
2475 | mutex_unlock(&bch_register_lock); | |
2476 | ||
2477 | out: | |
2478 | if (fail) | |
2479 | pr_info("error %s: fail to register backing device\n", | |
2480 | args->path); | |
2481 | kfree(args->sb); | |
2482 | kfree(args->path); | |
2483 | kfree(args); | |
2484 | module_put(THIS_MODULE); | |
2485 | } | |
2486 | ||
2487 | static void register_cache_worker(struct work_struct *work) | |
2488 | { | |
2489 | int fail = false; | |
2490 | struct async_reg_args *args = | |
ee4a36f4 | 2491 | container_of(work, struct async_reg_args, reg_work.work); |
9e23ccf8 CL |
2492 | struct cache *ca; |
2493 | ||
2494 | ca = kzalloc(sizeof(*ca), GFP_KERNEL); | |
2495 | if (!ca) { | |
2496 | fail = true; | |
2497 | put_page(virt_to_page(args->sb_disk)); | |
2498 | blkdev_put(args->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); | |
2499 | goto out; | |
2500 | } | |
2501 | ||
2502 | /* blkdev_put() will be called in bch_cache_release() */ | |
2503 | if (register_cache(args->sb, args->sb_disk, args->bdev, ca) != 0) | |
2504 | fail = true; | |
2505 | ||
2506 | out: | |
2507 | if (fail) | |
2508 | pr_info("error %s: fail to register cache device\n", | |
2509 | args->path); | |
2510 | kfree(args->sb); | |
2511 | kfree(args->path); | |
2512 | kfree(args); | |
2513 | module_put(THIS_MODULE); | |
2514 | } | |
2515 | ||
d7fae7b4 | 2516 | static void register_device_async(struct async_reg_args *args) |
9e23ccf8 CL |
2517 | { |
2518 | if (SB_IS_BDEV(args->sb)) | |
ee4a36f4 | 2519 | INIT_DELAYED_WORK(&args->reg_work, register_bdev_worker); |
9e23ccf8 | 2520 | else |
ee4a36f4 | 2521 | INIT_DELAYED_WORK(&args->reg_work, register_cache_worker); |
9e23ccf8 | 2522 | |
ee4a36f4 CL |
2523 | /* 10 jiffies is enough for a delay */ |
2524 | queue_delayed_work(system_wq, &args->reg_work, 10); | |
9e23ccf8 CL |
2525 | } |
2526 | ||
cafe5635 KO |
2527 | static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr, |
2528 | const char *buffer, size_t size) | |
2529 | { | |
50246693 | 2530 | const char *err; |
29cda393 | 2531 | char *path = NULL; |
50246693 | 2532 | struct cache_sb *sb; |
cfa0c56d | 2533 | struct cache_sb_disk *sb_disk; |
fc8f19cc | 2534 | struct block_device *bdev; |
50246693 | 2535 | ssize_t ret; |
a58e88bf CL |
2536 | bool async_registration = false; |
2537 | ||
2538 | #ifdef CONFIG_BCACHE_ASYNC_REGISTRATION | |
2539 | async_registration = true; | |
2540 | #endif | |
cafe5635 | 2541 | |
50246693 | 2542 | ret = -EBUSY; |
29cda393 | 2543 | err = "failed to reference bcache module"; |
cafe5635 | 2544 | if (!try_module_get(THIS_MODULE)) |
50246693 | 2545 | goto out; |
cafe5635 | 2546 | |
a59ff6cc CL |
2547 | /* For latest state of bcache_is_reboot */ |
2548 | smp_mb(); | |
29cda393 | 2549 | err = "bcache is in reboot"; |
a59ff6cc | 2550 | if (bcache_is_reboot) |
50246693 | 2551 | goto out_module_put; |
a59ff6cc | 2552 | |
50246693 CH |
2553 | ret = -ENOMEM; |
2554 | err = "cannot allocate memory"; | |
a56489d4 FS |
2555 | path = kstrndup(buffer, size, GFP_KERNEL); |
2556 | if (!path) | |
50246693 | 2557 | goto out_module_put; |
a56489d4 FS |
2558 | |
2559 | sb = kmalloc(sizeof(struct cache_sb), GFP_KERNEL); | |
2560 | if (!sb) | |
50246693 | 2561 | goto out_free_path; |
cafe5635 | 2562 | |
50246693 | 2563 | ret = -EINVAL; |
cafe5635 KO |
2564 | err = "failed to open device"; |
2565 | bdev = blkdev_get_by_path(strim(path), | |
2566 | FMODE_READ|FMODE_WRITE|FMODE_EXCL, | |
2567 | sb); | |
f59fce84 | 2568 | if (IS_ERR(bdev)) { |
a9dd53ad | 2569 | if (bdev == ERR_PTR(-EBUSY)) { |
4e7b5671 CH |
2570 | dev_t dev; |
2571 | ||
789d21db | 2572 | mutex_lock(&bch_register_lock); |
4e7b5671 CH |
2573 | if (lookup_bdev(strim(path), &dev) == 0 && |
2574 | bch_is_open(dev)) | |
a9dd53ad GP |
2575 | err = "device already registered"; |
2576 | else | |
2577 | err = "device busy"; | |
789d21db | 2578 | mutex_unlock(&bch_register_lock); |
d7076f21 | 2579 | if (attr == &ksysfs_register_quiet) |
50246693 | 2580 | goto done; |
a9dd53ad | 2581 | } |
50246693 | 2582 | goto out_free_sb; |
f59fce84 KO |
2583 | } |
2584 | ||
2585 | err = "failed to set blocksize"; | |
2586 | if (set_blocksize(bdev, 4096)) | |
50246693 | 2587 | goto out_blkdev_put; |
cafe5635 | 2588 | |
cfa0c56d | 2589 | err = read_super(sb, bdev, &sb_disk); |
cafe5635 | 2590 | if (err) |
50246693 | 2591 | goto out_blkdev_put; |
cafe5635 | 2592 | |
cc40daf9 | 2593 | err = "failed to register device"; |
a58e88bf CL |
2594 | |
2595 | if (async_registration) { | |
9e23ccf8 CL |
2596 | /* register in asynchronous way */ |
2597 | struct async_reg_args *args = | |
2598 | kzalloc(sizeof(struct async_reg_args), GFP_KERNEL); | |
2599 | ||
2600 | if (!args) { | |
2601 | ret = -ENOMEM; | |
2602 | err = "cannot allocate memory"; | |
2603 | goto out_put_sb_page; | |
2604 | } | |
2605 | ||
2606 | args->path = path; | |
2607 | args->sb = sb; | |
2608 | args->sb_disk = sb_disk; | |
2609 | args->bdev = bdev; | |
d7fae7b4 | 2610 | register_device_async(args); |
9e23ccf8 CL |
2611 | /* No wait and returns to user space */ |
2612 | goto async_done; | |
2613 | } | |
2614 | ||
2903381f | 2615 | if (SB_IS_BDEV(sb)) { |
cafe5635 | 2616 | struct cached_dev *dc = kzalloc(sizeof(*dc), GFP_KERNEL); |
1fae7cf0 | 2617 | |
d55f7cb2 CY |
2618 | if (!dc) { |
2619 | ret = -ENOMEM; | |
2620 | err = "cannot allocate memory"; | |
50246693 | 2621 | goto out_put_sb_page; |
d55f7cb2 | 2622 | } |
cafe5635 | 2623 | |
4fa03402 | 2624 | mutex_lock(&bch_register_lock); |
cfa0c56d | 2625 | ret = register_bdev(sb, sb_disk, bdev, dc); |
4fa03402 | 2626 | mutex_unlock(&bch_register_lock); |
bb6d355c | 2627 | /* blkdev_put() will be called in cached_dev_free() */ |
fc8f19cc CH |
2628 | if (ret < 0) |
2629 | goto out_free_sb; | |
cafe5635 KO |
2630 | } else { |
2631 | struct cache *ca = kzalloc(sizeof(*ca), GFP_KERNEL); | |
1fae7cf0 | 2632 | |
d55f7cb2 CY |
2633 | if (!ca) { |
2634 | ret = -ENOMEM; | |
2635 | err = "cannot allocate memory"; | |
50246693 | 2636 | goto out_put_sb_page; |
d55f7cb2 | 2637 | } |
cafe5635 | 2638 | |
bb6d355c | 2639 | /* blkdev_put() will be called in bch_cache_release() */ |
d55f7cb2 CY |
2640 | ret = register_cache(sb, sb_disk, bdev, ca); |
2641 | if (ret) | |
fc8f19cc | 2642 | goto out_free_sb; |
cafe5635 | 2643 | } |
50246693 | 2644 | |
50246693 | 2645 | done: |
cafe5635 KO |
2646 | kfree(sb); |
2647 | kfree(path); | |
cafe5635 | 2648 | module_put(THIS_MODULE); |
9e23ccf8 | 2649 | async_done: |
50246693 CH |
2650 | return size; |
2651 | ||
2652 | out_put_sb_page: | |
cfa0c56d | 2653 | put_page(virt_to_page(sb_disk)); |
50246693 | 2654 | out_blkdev_put: |
fc8f19cc | 2655 | blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); |
50246693 CH |
2656 | out_free_sb: |
2657 | kfree(sb); | |
2658 | out_free_path: | |
2659 | kfree(path); | |
ae3cd299 | 2660 | path = NULL; |
50246693 CH |
2661 | out_module_put: |
2662 | module_put(THIS_MODULE); | |
2663 | out: | |
46f5aa88 | 2664 | pr_info("error %s: %s\n", path?path:"", err); |
50246693 | 2665 | return ret; |
cafe5635 KO |
2666 | } |
2667 | ||
0c277e21 CL |
2668 | |
2669 | struct pdev { | |
2670 | struct list_head list; | |
2671 | struct cached_dev *dc; | |
2672 | }; | |
2673 | ||
2674 | static ssize_t bch_pending_bdevs_cleanup(struct kobject *k, | |
2675 | struct kobj_attribute *attr, | |
2676 | const char *buffer, | |
2677 | size_t size) | |
2678 | { | |
2679 | LIST_HEAD(pending_devs); | |
2680 | ssize_t ret = size; | |
2681 | struct cached_dev *dc, *tdc; | |
2682 | struct pdev *pdev, *tpdev; | |
2683 | struct cache_set *c, *tc; | |
2684 | ||
2685 | mutex_lock(&bch_register_lock); | |
2686 | list_for_each_entry_safe(dc, tdc, &uncached_devices, list) { | |
2687 | pdev = kmalloc(sizeof(struct pdev), GFP_KERNEL); | |
2688 | if (!pdev) | |
2689 | break; | |
2690 | pdev->dc = dc; | |
2691 | list_add(&pdev->list, &pending_devs); | |
2692 | } | |
2693 | ||
2694 | list_for_each_entry_safe(pdev, tpdev, &pending_devs, list) { | |
e8092707 | 2695 | char *pdev_set_uuid = pdev->dc->sb.set_uuid; |
0c277e21 | 2696 | list_for_each_entry_safe(c, tc, &bch_cache_sets, list) { |
1132e56e | 2697 | char *set_uuid = c->set_uuid; |
0c277e21 CL |
2698 | |
2699 | if (!memcmp(pdev_set_uuid, set_uuid, 16)) { | |
2700 | list_del(&pdev->list); | |
2701 | kfree(pdev); | |
2702 | break; | |
2703 | } | |
2704 | } | |
2705 | } | |
2706 | mutex_unlock(&bch_register_lock); | |
2707 | ||
2708 | list_for_each_entry_safe(pdev, tpdev, &pending_devs, list) { | |
46f5aa88 | 2709 | pr_info("delete pdev %p\n", pdev); |
0c277e21 CL |
2710 | list_del(&pdev->list); |
2711 | bcache_device_stop(&pdev->dc->disk); | |
2712 | kfree(pdev); | |
2713 | } | |
2714 | ||
2715 | return ret; | |
2716 | } | |
2717 | ||
cafe5635 KO |
2718 | static int bcache_reboot(struct notifier_block *n, unsigned long code, void *x) |
2719 | { | |
a59ff6cc CL |
2720 | if (bcache_is_reboot) |
2721 | return NOTIFY_DONE; | |
2722 | ||
cafe5635 KO |
2723 | if (code == SYS_DOWN || |
2724 | code == SYS_HALT || | |
2725 | code == SYS_POWER_OFF) { | |
2726 | DEFINE_WAIT(wait); | |
2727 | unsigned long start = jiffies; | |
2728 | bool stopped = false; | |
2729 | ||
2730 | struct cache_set *c, *tc; | |
2731 | struct cached_dev *dc, *tdc; | |
2732 | ||
2733 | mutex_lock(&bch_register_lock); | |
2734 | ||
a59ff6cc CL |
2735 | if (bcache_is_reboot) |
2736 | goto out; | |
2737 | ||
2738 | /* New registration is rejected since now */ | |
2739 | bcache_is_reboot = true; | |
2740 | /* | |
2741 | * Make registering caller (if there is) on other CPU | |
2742 | * core know bcache_is_reboot set to true earlier | |
2743 | */ | |
2744 | smp_mb(); | |
2745 | ||
cafe5635 KO |
2746 | if (list_empty(&bch_cache_sets) && |
2747 | list_empty(&uncached_devices)) | |
2748 | goto out; | |
2749 | ||
a59ff6cc CL |
2750 | mutex_unlock(&bch_register_lock); |
2751 | ||
46f5aa88 | 2752 | pr_info("Stopping all devices:\n"); |
cafe5635 | 2753 | |
a59ff6cc CL |
2754 | /* |
2755 | * The reason bch_register_lock is not held to call | |
2756 | * bch_cache_set_stop() and bcache_device_stop() is to | |
2757 | * avoid potential deadlock during reboot, because cache | |
a307e2ab | 2758 | * set or bcache device stopping process will acquire |
a59ff6cc CL |
2759 | * bch_register_lock too. |
2760 | * | |
2761 | * We are safe here because bcache_is_reboot sets to | |
2762 | * true already, register_bcache() will reject new | |
2763 | * registration now. bcache_is_reboot also makes sure | |
2764 | * bcache_reboot() won't be re-entered on by other thread, | |
2765 | * so there is no race in following list iteration by | |
2766 | * list_for_each_entry_safe(). | |
2767 | */ | |
cafe5635 KO |
2768 | list_for_each_entry_safe(c, tc, &bch_cache_sets, list) |
2769 | bch_cache_set_stop(c); | |
2770 | ||
2771 | list_for_each_entry_safe(dc, tdc, &uncached_devices, list) | |
2772 | bcache_device_stop(&dc->disk); | |
2773 | ||
eb8cbb6d CL |
2774 | |
2775 | /* | |
2776 | * Give an early chance for other kthreads and | |
2777 | * kworkers to stop themselves | |
2778 | */ | |
2779 | schedule(); | |
2780 | ||
cafe5635 KO |
2781 | /* What's a condition variable? */ |
2782 | while (1) { | |
eb8cbb6d | 2783 | long timeout = start + 10 * HZ - jiffies; |
cafe5635 | 2784 | |
eb8cbb6d | 2785 | mutex_lock(&bch_register_lock); |
cafe5635 KO |
2786 | stopped = list_empty(&bch_cache_sets) && |
2787 | list_empty(&uncached_devices); | |
2788 | ||
2789 | if (timeout < 0 || stopped) | |
2790 | break; | |
2791 | ||
2792 | prepare_to_wait(&unregister_wait, &wait, | |
2793 | TASK_UNINTERRUPTIBLE); | |
2794 | ||
2795 | mutex_unlock(&bch_register_lock); | |
2796 | schedule_timeout(timeout); | |
cafe5635 KO |
2797 | } |
2798 | ||
2799 | finish_wait(&unregister_wait, &wait); | |
2800 | ||
2801 | if (stopped) | |
46f5aa88 | 2802 | pr_info("All devices stopped\n"); |
cafe5635 | 2803 | else |
46f5aa88 | 2804 | pr_notice("Timeout waiting for devices to be closed\n"); |
cafe5635 KO |
2805 | out: |
2806 | mutex_unlock(&bch_register_lock); | |
2807 | } | |
2808 | ||
2809 | return NOTIFY_DONE; | |
2810 | } | |
2811 | ||
2812 | static struct notifier_block reboot = { | |
2813 | .notifier_call = bcache_reboot, | |
2814 | .priority = INT_MAX, /* before any real devices */ | |
2815 | }; | |
2816 | ||
2817 | static void bcache_exit(void) | |
2818 | { | |
2819 | bch_debug_exit(); | |
cafe5635 | 2820 | bch_request_exit(); |
cafe5635 KO |
2821 | if (bcache_kobj) |
2822 | kobject_put(bcache_kobj); | |
2823 | if (bcache_wq) | |
2824 | destroy_workqueue(bcache_wq); | |
0f843e65 GF |
2825 | if (bch_journal_wq) |
2826 | destroy_workqueue(bch_journal_wq); | |
afe78ab4 KK |
2827 | if (bch_flush_wq) |
2828 | destroy_workqueue(bch_flush_wq); | |
9f233ffe | 2829 | bch_btree_exit(); |
0f843e65 | 2830 | |
5c41c8a7 KO |
2831 | if (bcache_major) |
2832 | unregister_blkdev(bcache_major, "bcache"); | |
cafe5635 | 2833 | unregister_reboot_notifier(&reboot); |
330a4db8 | 2834 | mutex_destroy(&bch_register_lock); |
cafe5635 KO |
2835 | } |
2836 | ||
9aaf5165 CL |
2837 | /* Check and fixup module parameters */ |
2838 | static void check_module_parameters(void) | |
2839 | { | |
2840 | if (bch_cutoff_writeback_sync == 0) | |
2841 | bch_cutoff_writeback_sync = CUTOFF_WRITEBACK_SYNC; | |
2842 | else if (bch_cutoff_writeback_sync > CUTOFF_WRITEBACK_SYNC_MAX) { | |
46f5aa88 | 2843 | pr_warn("set bch_cutoff_writeback_sync (%u) to max value %u\n", |
9aaf5165 CL |
2844 | bch_cutoff_writeback_sync, CUTOFF_WRITEBACK_SYNC_MAX); |
2845 | bch_cutoff_writeback_sync = CUTOFF_WRITEBACK_SYNC_MAX; | |
2846 | } | |
2847 | ||
2848 | if (bch_cutoff_writeback == 0) | |
2849 | bch_cutoff_writeback = CUTOFF_WRITEBACK; | |
2850 | else if (bch_cutoff_writeback > CUTOFF_WRITEBACK_MAX) { | |
46f5aa88 | 2851 | pr_warn("set bch_cutoff_writeback (%u) to max value %u\n", |
9aaf5165 CL |
2852 | bch_cutoff_writeback, CUTOFF_WRITEBACK_MAX); |
2853 | bch_cutoff_writeback = CUTOFF_WRITEBACK_MAX; | |
2854 | } | |
2855 | ||
2856 | if (bch_cutoff_writeback > bch_cutoff_writeback_sync) { | |
46f5aa88 | 2857 | pr_warn("set bch_cutoff_writeback (%u) to %u\n", |
9aaf5165 CL |
2858 | bch_cutoff_writeback, bch_cutoff_writeback_sync); |
2859 | bch_cutoff_writeback = bch_cutoff_writeback_sync; | |
2860 | } | |
2861 | } | |
2862 | ||
cafe5635 KO |
2863 | static int __init bcache_init(void) |
2864 | { | |
2865 | static const struct attribute *files[] = { | |
2866 | &ksysfs_register.attr, | |
2867 | &ksysfs_register_quiet.attr, | |
0c277e21 | 2868 | &ksysfs_pendings_cleanup.attr, |
cafe5635 KO |
2869 | NULL |
2870 | }; | |
2871 | ||
9aaf5165 CL |
2872 | check_module_parameters(); |
2873 | ||
cafe5635 KO |
2874 | mutex_init(&bch_register_lock); |
2875 | init_waitqueue_head(&unregister_wait); | |
2876 | register_reboot_notifier(&reboot); | |
2877 | ||
2878 | bcache_major = register_blkdev(0, "bcache"); | |
2ecf0cdb ZL |
2879 | if (bcache_major < 0) { |
2880 | unregister_reboot_notifier(&reboot); | |
330a4db8 | 2881 | mutex_destroy(&bch_register_lock); |
cafe5635 | 2882 | return bcache_major; |
2ecf0cdb | 2883 | } |
cafe5635 | 2884 | |
9f233ffe KK |
2885 | if (bch_btree_init()) |
2886 | goto err; | |
2887 | ||
16c1fdf4 FS |
2888 | bcache_wq = alloc_workqueue("bcache", WQ_MEM_RECLAIM, 0); |
2889 | if (!bcache_wq) | |
2890 | goto err; | |
2891 | ||
afe78ab4 KK |
2892 | /* |
2893 | * Let's not make this `WQ_MEM_RECLAIM` for the following reasons: | |
2894 | * | |
2895 | * 1. It used `system_wq` before which also does no memory reclaim. | |
2896 | * 2. With `WQ_MEM_RECLAIM` desktop stalls, increased boot times, and | |
2897 | * reduced throughput can be observed. | |
2898 | * | |
2899 | * We still want to user our own queue to not congest the `system_wq`. | |
2900 | */ | |
2901 | bch_flush_wq = alloc_workqueue("bch_flush", 0, 0); | |
2902 | if (!bch_flush_wq) | |
2903 | goto err; | |
2904 | ||
0f843e65 GF |
2905 | bch_journal_wq = alloc_workqueue("bch_journal", WQ_MEM_RECLAIM, 0); |
2906 | if (!bch_journal_wq) | |
2907 | goto err; | |
2908 | ||
16c1fdf4 FS |
2909 | bcache_kobj = kobject_create_and_add("bcache", fs_kobj); |
2910 | if (!bcache_kobj) | |
2911 | goto err; | |
2912 | ||
2913 | if (bch_request_init() || | |
330a4db8 | 2914 | sysfs_create_files(bcache_kobj, files)) |
cafe5635 KO |
2915 | goto err; |
2916 | ||
91bafdf0 | 2917 | bch_debug_init(); |
78ac2107 CL |
2918 | closure_debug_init(); |
2919 | ||
a59ff6cc CL |
2920 | bcache_is_reboot = false; |
2921 | ||
cafe5635 KO |
2922 | return 0; |
2923 | err: | |
2924 | bcache_exit(); | |
2925 | return -ENOMEM; | |
2926 | } | |
2927 | ||
9aaf5165 CL |
2928 | /* |
2929 | * Module hooks | |
2930 | */ | |
cafe5635 KO |
2931 | module_exit(bcache_exit); |
2932 | module_init(bcache_init); | |
009673d0 | 2933 | |
9aaf5165 CL |
2934 | module_param(bch_cutoff_writeback, uint, 0); |
2935 | MODULE_PARM_DESC(bch_cutoff_writeback, "threshold to cutoff writeback"); | |
2936 | ||
2937 | module_param(bch_cutoff_writeback_sync, uint, 0); | |
2938 | MODULE_PARM_DESC(bch_cutoff_writeback_sync, "hard threshold to cutoff writeback"); | |
2939 | ||
009673d0 CL |
2940 | MODULE_DESCRIPTION("Bcache: a Linux block layer cache"); |
2941 | MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>"); | |
2942 | MODULE_LICENSE("GPL"); |