Commit | Line | Data |
---|---|---|
1c6fdbd8 KO |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * bcachefs 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 "bcachefs.h" | |
7b3f84ea KO |
11 | #include "alloc_background.h" |
12 | #include "alloc_foreground.h" | |
5b8a9227 | 13 | #include "bkey_sort.h" |
1c6fdbd8 KO |
14 | #include "btree_cache.h" |
15 | #include "btree_gc.h" | |
2ca88e5a | 16 | #include "btree_key_cache.h" |
1c6fdbd8 KO |
17 | #include "btree_update_interior.h" |
18 | #include "btree_io.h" | |
19 | #include "chardev.h" | |
20 | #include "checksum.h" | |
21 | #include "clock.h" | |
22 | #include "compress.h" | |
23 | #include "debug.h" | |
24 | #include "disk_groups.h" | |
cd575ddf | 25 | #include "ec.h" |
1c6fdbd8 KO |
26 | #include "error.h" |
27 | #include "fs.h" | |
28 | #include "fs-io.h" | |
29 | #include "fsck.h" | |
30 | #include "inode.h" | |
31 | #include "io.h" | |
32 | #include "journal.h" | |
33 | #include "journal_reclaim.h" | |
1dd7f9d9 | 34 | #include "journal_seq_blacklist.h" |
1c6fdbd8 KO |
35 | #include "move.h" |
36 | #include "migrate.h" | |
37 | #include "movinggc.h" | |
38 | #include "quota.h" | |
39 | #include "rebalance.h" | |
40 | #include "recovery.h" | |
41 | #include "replicas.h" | |
14b393ee | 42 | #include "subvolume.h" |
1c6fdbd8 KO |
43 | #include "super.h" |
44 | #include "super-io.h" | |
45 | #include "sysfs.h" | |
46 | #include "trace.h" | |
47 | ||
48 | #include <linux/backing-dev.h> | |
49 | #include <linux/blkdev.h> | |
50 | #include <linux/debugfs.h> | |
51 | #include <linux/device.h> | |
52 | #include <linux/idr.h> | |
1c6fdbd8 KO |
53 | #include <linux/module.h> |
54 | #include <linux/percpu.h> | |
55 | #include <linux/random.h> | |
56 | #include <linux/sysfs.h> | |
57 | #include <crypto/hash.h> | |
58 | ||
59 | MODULE_LICENSE("GPL"); | |
60 | MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>"); | |
61 | ||
62 | #define KTYPE(type) \ | |
63 | static const struct attribute_group type ## _group = { \ | |
64 | .attrs = type ## _files \ | |
65 | }; \ | |
66 | \ | |
67 | static const struct attribute_group *type ## _groups[] = { \ | |
68 | &type ## _group, \ | |
69 | NULL \ | |
70 | }; \ | |
71 | \ | |
72 | static const struct kobj_type type ## _ktype = { \ | |
73 | .release = type ## _release, \ | |
74 | .sysfs_ops = &type ## _sysfs_ops, \ | |
75 | .default_groups = type ## _groups \ | |
76 | } | |
77 | ||
78 | static void bch2_fs_release(struct kobject *); | |
79 | static void bch2_dev_release(struct kobject *); | |
80 | ||
81 | static void bch2_fs_internal_release(struct kobject *k) | |
82 | { | |
83 | } | |
84 | ||
85 | static void bch2_fs_opts_dir_release(struct kobject *k) | |
86 | { | |
87 | } | |
88 | ||
89 | static void bch2_fs_time_stats_release(struct kobject *k) | |
90 | { | |
91 | } | |
92 | ||
93 | KTYPE(bch2_fs); | |
94 | KTYPE(bch2_fs_internal); | |
95 | KTYPE(bch2_fs_opts_dir); | |
96 | KTYPE(bch2_fs_time_stats); | |
97 | KTYPE(bch2_dev); | |
98 | ||
99 | static struct kset *bcachefs_kset; | |
100 | static LIST_HEAD(bch_fs_list); | |
101 | static DEFINE_MUTEX(bch_fs_list_lock); | |
102 | ||
103 | static DECLARE_WAIT_QUEUE_HEAD(bch_read_only_wait); | |
104 | ||
105 | static void bch2_dev_free(struct bch_dev *); | |
106 | static int bch2_dev_alloc(struct bch_fs *, unsigned); | |
107 | static int bch2_dev_sysfs_online(struct bch_fs *, struct bch_dev *); | |
108 | static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *); | |
109 | ||
110 | struct bch_fs *bch2_dev_to_fs(dev_t dev) | |
111 | { | |
112 | struct bch_fs *c; | |
113 | struct bch_dev *ca; | |
114 | unsigned i; | |
115 | ||
116 | mutex_lock(&bch_fs_list_lock); | |
117 | rcu_read_lock(); | |
118 | ||
119 | list_for_each_entry(c, &bch_fs_list, list) | |
120 | for_each_member_device_rcu(ca, c, i, NULL) | |
ec4ab9d2 | 121 | if (ca->disk_sb.bdev && ca->disk_sb.bdev->bd_dev == dev) { |
1c6fdbd8 KO |
122 | closure_get(&c->cl); |
123 | goto found; | |
124 | } | |
125 | c = NULL; | |
126 | found: | |
127 | rcu_read_unlock(); | |
128 | mutex_unlock(&bch_fs_list_lock); | |
129 | ||
130 | return c; | |
131 | } | |
132 | ||
133 | static struct bch_fs *__bch2_uuid_to_fs(__uuid_t uuid) | |
134 | { | |
135 | struct bch_fs *c; | |
136 | ||
137 | lockdep_assert_held(&bch_fs_list_lock); | |
138 | ||
139 | list_for_each_entry(c, &bch_fs_list, list) | |
140 | if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid))) | |
141 | return c; | |
142 | ||
143 | return NULL; | |
144 | } | |
145 | ||
146 | struct bch_fs *bch2_uuid_to_fs(__uuid_t uuid) | |
147 | { | |
148 | struct bch_fs *c; | |
149 | ||
150 | mutex_lock(&bch_fs_list_lock); | |
151 | c = __bch2_uuid_to_fs(uuid); | |
152 | if (c) | |
153 | closure_get(&c->cl); | |
154 | mutex_unlock(&bch_fs_list_lock); | |
155 | ||
156 | return c; | |
157 | } | |
158 | ||
180fb49d KO |
159 | static void bch2_dev_usage_journal_reserve(struct bch_fs *c) |
160 | { | |
161 | struct bch_dev *ca; | |
162 | unsigned i, nr = 0, u64s = | |
4b8f89af KO |
163 | ((sizeof(struct jset_entry_dev_usage) + |
164 | sizeof(struct jset_entry_dev_usage_type) * BCH_DATA_NR)) / | |
165 | sizeof(u64); | |
180fb49d KO |
166 | |
167 | rcu_read_lock(); | |
168 | for_each_member_device_rcu(ca, c, i, NULL) | |
169 | nr++; | |
170 | rcu_read_unlock(); | |
171 | ||
172 | bch2_journal_entry_res_resize(&c->journal, | |
173 | &c->dev_usage_journal_res, u64s * nr); | |
174 | } | |
175 | ||
1c6fdbd8 KO |
176 | /* Filesystem RO/RW: */ |
177 | ||
178 | /* | |
179 | * For startup/shutdown of RW stuff, the dependencies are: | |
180 | * | |
181 | * - foreground writes depend on copygc and rebalance (to free up space) | |
182 | * | |
183 | * - copygc and rebalance depend on mark and sweep gc (they actually probably | |
184 | * don't because they either reserve ahead of time or don't block if | |
185 | * allocations fail, but allocations can require mark and sweep gc to run | |
186 | * because of generation number wraparound) | |
187 | * | |
188 | * - all of the above depends on the allocator threads | |
189 | * | |
190 | * - allocator depends on the journal (when it rewrites prios and gens) | |
191 | */ | |
192 | ||
193 | static void __bch2_fs_read_only(struct bch_fs *c) | |
194 | { | |
195 | struct bch_dev *ca; | |
d5f70c1f | 196 | unsigned i, clean_passes = 0; |
1c6fdbd8 KO |
197 | |
198 | bch2_rebalance_stop(c); | |
e6d11615 | 199 | bch2_copygc_stop(c); |
1c6fdbd8 KO |
200 | bch2_gc_thread_stop(c); |
201 | ||
202 | /* | |
203 | * Flush journal before stopping allocators, because flushing journal | |
204 | * blacklist entries involves allocating new btree nodes: | |
205 | */ | |
206 | bch2_journal_flush_all_pins(&c->journal); | |
207 | ||
2340fd9d KO |
208 | /* |
209 | * If the allocator threads didn't all start up, the btree updates to | |
210 | * write out alloc info aren't going to work: | |
211 | */ | |
b935a8a6 | 212 | if (!test_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags)) |
2340fd9d | 213 | goto nowrote_alloc; |
b935a8a6 | 214 | |
039fc4c5 | 215 | bch_verbose(c, "flushing journal and stopping allocators"); |
460651ee | 216 | |
039fc4c5 KO |
217 | bch2_journal_flush_all_pins(&c->journal); |
218 | set_bit(BCH_FS_ALLOCATOR_STOPPING, &c->flags); | |
61c8d7c8 | 219 | |
039fc4c5 KO |
220 | do { |
221 | clean_passes++; | |
430735cd | 222 | |
039fc4c5 KO |
223 | if (bch2_journal_flush_all_pins(&c->journal)) |
224 | clean_passes = 0; | |
430735cd KO |
225 | |
226 | /* | |
039fc4c5 KO |
227 | * In flight interior btree updates will generate more journal |
228 | * updates and btree updates (alloc btree): | |
430735cd | 229 | */ |
039fc4c5 KO |
230 | if (bch2_btree_interior_updates_nr_pending(c)) { |
231 | closure_wait_event(&c->btree_interior_update_wait, | |
232 | !bch2_btree_interior_updates_nr_pending(c)); | |
233 | clean_passes = 0; | |
234 | } | |
00b8ccf7 | 235 | flush_work(&c->btree_interior_update_work); |
d5f70c1f | 236 | |
039fc4c5 KO |
237 | if (bch2_journal_flush_all_pins(&c->journal)) |
238 | clean_passes = 0; | |
d5f70c1f | 239 | } while (clean_passes < 2); |
039fc4c5 | 240 | bch_verbose(c, "flushing journal and stopping allocators complete"); |
2340fd9d KO |
241 | |
242 | set_bit(BCH_FS_ALLOC_CLEAN, &c->flags); | |
243 | nowrote_alloc: | |
00b8ccf7 KO |
244 | closure_wait_event(&c->btree_interior_update_wait, |
245 | !bch2_btree_interior_updates_nr_pending(c)); | |
246 | flush_work(&c->btree_interior_update_work); | |
247 | ||
1c6fdbd8 KO |
248 | for_each_member_device(ca, c, i) |
249 | bch2_dev_allocator_stop(ca); | |
250 | ||
b935a8a6 | 251 | clear_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags); |
039fc4c5 | 252 | clear_bit(BCH_FS_ALLOCATOR_STOPPING, &c->flags); |
b935a8a6 | 253 | |
1c6fdbd8 KO |
254 | bch2_fs_journal_stop(&c->journal); |
255 | ||
256 | /* | |
257 | * the journal kicks off btree writes via reclaim - wait for in flight | |
258 | * writes after stopping journal: | |
259 | */ | |
a5cd80ea | 260 | bch2_btree_flush_all_writes(c); |
1c6fdbd8 KO |
261 | |
262 | /* | |
263 | * After stopping journal: | |
264 | */ | |
265 | for_each_member_device(ca, c, i) | |
266 | bch2_dev_allocator_remove(c, ca); | |
267 | } | |
268 | ||
269 | static void bch2_writes_disabled(struct percpu_ref *writes) | |
270 | { | |
271 | struct bch_fs *c = container_of(writes, struct bch_fs, writes); | |
272 | ||
273 | set_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags); | |
274 | wake_up(&bch_read_only_wait); | |
275 | } | |
276 | ||
277 | void bch2_fs_read_only(struct bch_fs *c) | |
278 | { | |
134915f3 | 279 | if (!test_bit(BCH_FS_RW, &c->flags)) { |
9ae28f82 | 280 | bch2_journal_reclaim_stop(&c->journal); |
1c6fdbd8 | 281 | return; |
134915f3 | 282 | } |
1c6fdbd8 KO |
283 | |
284 | BUG_ON(test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags)); | |
285 | ||
286 | /* | |
287 | * Block new foreground-end write operations from starting - any new | |
288 | * writes will return -EROFS: | |
289 | * | |
290 | * (This is really blocking new _allocations_, writes to previously | |
291 | * allocated space can still happen until stopping the allocator in | |
292 | * bch2_dev_allocator_stop()). | |
293 | */ | |
294 | percpu_ref_kill(&c->writes); | |
295 | ||
f516c872 | 296 | cancel_work_sync(&c->ec_stripe_delete_work); |
1c6fdbd8 KO |
297 | |
298 | /* | |
299 | * If we're not doing an emergency shutdown, we want to wait on | |
300 | * outstanding writes to complete so they don't see spurious errors due | |
301 | * to shutting down the allocator: | |
302 | * | |
303 | * If we are doing an emergency shutdown outstanding writes may | |
304 | * hang until we shutdown the allocator so we don't want to wait | |
305 | * on outstanding writes before shutting everything down - but | |
306 | * we do need to wait on them before returning and signalling | |
307 | * that going RO is complete: | |
308 | */ | |
309 | wait_event(bch_read_only_wait, | |
310 | test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags) || | |
311 | test_bit(BCH_FS_EMERGENCY_RO, &c->flags)); | |
312 | ||
313 | __bch2_fs_read_only(c); | |
314 | ||
315 | wait_event(bch_read_only_wait, | |
316 | test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags)); | |
317 | ||
318 | clear_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags); | |
319 | ||
320 | if (!bch2_journal_error(&c->journal) && | |
321 | !test_bit(BCH_FS_ERROR, &c->flags) && | |
3aea4342 | 322 | !test_bit(BCH_FS_EMERGENCY_RO, &c->flags) && |
a0e0bda1 | 323 | test_bit(BCH_FS_STARTED, &c->flags) && |
2340fd9d | 324 | test_bit(BCH_FS_ALLOC_CLEAN, &c->flags) && |
b2930396 KO |
325 | !c->opts.norecovery) { |
326 | bch_verbose(c, "marking filesystem clean"); | |
134915f3 | 327 | bch2_fs_mark_clean(c); |
b2930396 | 328 | } |
1c6fdbd8 | 329 | |
134915f3 | 330 | clear_bit(BCH_FS_RW, &c->flags); |
1c6fdbd8 KO |
331 | } |
332 | ||
333 | static void bch2_fs_read_only_work(struct work_struct *work) | |
334 | { | |
335 | struct bch_fs *c = | |
336 | container_of(work, struct bch_fs, read_only_work); | |
337 | ||
1ada1606 | 338 | down_write(&c->state_lock); |
1c6fdbd8 | 339 | bch2_fs_read_only(c); |
1ada1606 | 340 | up_write(&c->state_lock); |
1c6fdbd8 KO |
341 | } |
342 | ||
343 | static void bch2_fs_read_only_async(struct bch_fs *c) | |
344 | { | |
345 | queue_work(system_long_wq, &c->read_only_work); | |
346 | } | |
347 | ||
348 | bool bch2_fs_emergency_read_only(struct bch_fs *c) | |
349 | { | |
350 | bool ret = !test_and_set_bit(BCH_FS_EMERGENCY_RO, &c->flags); | |
351 | ||
1c6fdbd8 | 352 | bch2_journal_halt(&c->journal); |
9f115ce9 | 353 | bch2_fs_read_only_async(c); |
1c6fdbd8 KO |
354 | |
355 | wake_up(&bch_read_only_wait); | |
356 | return ret; | |
357 | } | |
358 | ||
134915f3 | 359 | static int bch2_fs_read_write_late(struct bch_fs *c) |
1c6fdbd8 | 360 | { |
134915f3 | 361 | int ret; |
1c6fdbd8 | 362 | |
134915f3 KO |
363 | ret = bch2_gc_thread_start(c); |
364 | if (ret) { | |
365 | bch_err(c, "error starting gc thread"); | |
366 | return ret; | |
367 | } | |
368 | ||
e6d11615 KO |
369 | ret = bch2_copygc_start(c); |
370 | if (ret) { | |
371 | bch_err(c, "error starting copygc thread"); | |
372 | return ret; | |
134915f3 KO |
373 | } |
374 | ||
375 | ret = bch2_rebalance_start(c); | |
376 | if (ret) { | |
377 | bch_err(c, "error starting rebalance thread"); | |
378 | return ret; | |
379 | } | |
380 | ||
97fd13ad KO |
381 | schedule_work(&c->ec_stripe_delete_work); |
382 | ||
134915f3 KO |
383 | return 0; |
384 | } | |
1c6fdbd8 | 385 | |
e731d466 | 386 | static int __bch2_fs_read_write(struct bch_fs *c, bool early) |
134915f3 KO |
387 | { |
388 | struct bch_dev *ca; | |
389 | unsigned i; | |
390 | int ret; | |
391 | ||
aae15aaf KO |
392 | if (test_bit(BCH_FS_INITIAL_GC_UNFIXED, &c->flags)) { |
393 | bch_err(c, "cannot go rw, unfixed btree errors"); | |
394 | return -EROFS; | |
395 | } | |
396 | ||
134915f3 KO |
397 | if (test_bit(BCH_FS_RW, &c->flags)) |
398 | return 0; | |
399 | ||
619f5bee KO |
400 | /* |
401 | * nochanges is used for fsck -n mode - we have to allow going rw | |
402 | * during recovery for that to work: | |
403 | */ | |
404 | if (c->opts.norecovery || | |
405 | (c->opts.nochanges && | |
406 | (!early || c->opts.read_only))) | |
407 | return -EROFS; | |
330581f1 | 408 | |
2c944fa1 KO |
409 | bch_info(c, "going read-write"); |
410 | ||
134915f3 KO |
411 | ret = bch2_fs_mark_dirty(c); |
412 | if (ret) | |
413 | goto err; | |
1c6fdbd8 | 414 | |
2340fd9d KO |
415 | clear_bit(BCH_FS_ALLOC_CLEAN, &c->flags); |
416 | ||
1c6fdbd8 KO |
417 | for_each_rw_member(ca, c, i) |
418 | bch2_dev_allocator_add(c, ca); | |
419 | bch2_recalc_capacity(c); | |
420 | ||
134915f3 KO |
421 | for_each_rw_member(ca, c, i) { |
422 | ret = bch2_dev_allocator_start(ca); | |
423 | if (ret) { | |
424 | bch_err(c, "error starting allocator threads"); | |
1c6fdbd8 KO |
425 | percpu_ref_put(&ca->io_ref); |
426 | goto err; | |
427 | } | |
134915f3 | 428 | } |
1c6fdbd8 | 429 | |
134915f3 | 430 | set_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags); |
1c6fdbd8 | 431 | |
59a74051 KO |
432 | for_each_rw_member(ca, c, i) |
433 | bch2_wake_allocator(ca); | |
434 | ||
134915f3 KO |
435 | if (!early) { |
436 | ret = bch2_fs_read_write_late(c); | |
437 | if (ret) | |
438 | goto err; | |
439 | } | |
1c6fdbd8 | 440 | |
134915f3 KO |
441 | percpu_ref_reinit(&c->writes); |
442 | set_bit(BCH_FS_RW, &c->flags); | |
4932e07e | 443 | set_bit(BCH_FS_WAS_RW, &c->flags); |
134915f3 | 444 | return 0; |
1c6fdbd8 KO |
445 | err: |
446 | __bch2_fs_read_only(c); | |
134915f3 KO |
447 | return ret; |
448 | } | |
449 | ||
450 | int bch2_fs_read_write(struct bch_fs *c) | |
451 | { | |
452 | return __bch2_fs_read_write(c, false); | |
453 | } | |
454 | ||
455 | int bch2_fs_read_write_early(struct bch_fs *c) | |
456 | { | |
457 | lockdep_assert_held(&c->state_lock); | |
458 | ||
134915f3 | 459 | return __bch2_fs_read_write(c, true); |
1c6fdbd8 KO |
460 | } |
461 | ||
462 | /* Filesystem startup/shutdown: */ | |
463 | ||
d5e4dcc2 | 464 | static void __bch2_fs_free(struct bch_fs *c) |
1c6fdbd8 KO |
465 | { |
466 | unsigned i; | |
1a21bf98 | 467 | int cpu; |
1c6fdbd8 KO |
468 | |
469 | for (i = 0; i < BCH_TIME_STAT_NR; i++) | |
470 | bch2_time_stats_exit(&c->times[i]); | |
471 | ||
14b393ee | 472 | bch2_fs_snapshots_exit(c); |
1c6fdbd8 KO |
473 | bch2_fs_quota_exit(c); |
474 | bch2_fs_fsio_exit(c); | |
cd575ddf | 475 | bch2_fs_ec_exit(c); |
1c6fdbd8 KO |
476 | bch2_fs_encryption_exit(c); |
477 | bch2_fs_io_exit(c); | |
c823c339 | 478 | bch2_fs_btree_interior_update_exit(c); |
36e9d698 | 479 | bch2_fs_btree_iter_exit(c); |
2ca88e5a | 480 | bch2_fs_btree_key_cache_exit(&c->btree_key_cache); |
1c6fdbd8 | 481 | bch2_fs_btree_cache_exit(c); |
9620c3ec | 482 | bch2_fs_replicas_exit(c); |
1c6fdbd8 KO |
483 | bch2_fs_journal_exit(&c->journal); |
484 | bch2_io_clock_exit(&c->io_clock[WRITE]); | |
485 | bch2_io_clock_exit(&c->io_clock[READ]); | |
486 | bch2_fs_compress_exit(c); | |
f1d786a0 KO |
487 | bch2_journal_keys_free(&c->journal_keys); |
488 | bch2_journal_entries_free(&c->journal_entries); | |
9166b41d | 489 | percpu_free_rwsem(&c->mark_lock); |
5e82a9a1 | 490 | free_percpu(c->online_reserved); |
1a21bf98 | 491 | |
67e0dd8f | 492 | if (c->btree_paths_bufs) |
1a21bf98 | 493 | for_each_possible_cpu(cpu) |
67e0dd8f | 494 | kfree(per_cpu_ptr(c->btree_paths_bufs, cpu)->path); |
1a21bf98 | 495 | |
67e0dd8f | 496 | free_percpu(c->btree_paths_bufs); |
5663a415 | 497 | free_percpu(c->pcpu); |
35189e09 | 498 | mempool_exit(&c->large_bkey_pool); |
1c6fdbd8 KO |
499 | mempool_exit(&c->btree_bounce_pool); |
500 | bioset_exit(&c->btree_bio); | |
1c6fdbd8 KO |
501 | mempool_exit(&c->fill_iter); |
502 | percpu_ref_exit(&c->writes); | |
1c6fdbd8 | 503 | kfree(rcu_dereference_protected(c->disk_groups, 1)); |
1dd7f9d9 | 504 | kfree(c->journal_seq_blacklist_table); |
b5e8a699 | 505 | kfree(c->unused_inode_hints); |
e6d11615 | 506 | free_heap(&c->copygc_heap); |
1c6fdbd8 | 507 | |
731bdd2e KO |
508 | if (c->io_complete_wq ) |
509 | destroy_workqueue(c->io_complete_wq ); | |
1c6fdbd8 KO |
510 | if (c->copygc_wq) |
511 | destroy_workqueue(c->copygc_wq); | |
9f1833ca KO |
512 | if (c->btree_io_complete_wq) |
513 | destroy_workqueue(c->btree_io_complete_wq); | |
731bdd2e KO |
514 | if (c->btree_update_wq) |
515 | destroy_workqueue(c->btree_update_wq); | |
1c6fdbd8 | 516 | |
9d8022db | 517 | bch2_free_super(&c->disk_sb); |
1c6fdbd8 KO |
518 | kvpfree(c, sizeof(*c)); |
519 | module_put(THIS_MODULE); | |
520 | } | |
521 | ||
522 | static void bch2_fs_release(struct kobject *kobj) | |
523 | { | |
524 | struct bch_fs *c = container_of(kobj, struct bch_fs, kobj); | |
525 | ||
d5e4dcc2 | 526 | __bch2_fs_free(c); |
1c6fdbd8 KO |
527 | } |
528 | ||
d5e4dcc2 | 529 | void __bch2_fs_stop(struct bch_fs *c) |
1c6fdbd8 KO |
530 | { |
531 | struct bch_dev *ca; | |
532 | unsigned i; | |
533 | ||
af1c6871 KO |
534 | bch_verbose(c, "shutting down"); |
535 | ||
1dd7f9d9 KO |
536 | set_bit(BCH_FS_STOPPING, &c->flags); |
537 | ||
538 | cancel_work_sync(&c->journal_seq_blacklist_gc_work); | |
539 | ||
1ada1606 | 540 | down_write(&c->state_lock); |
883f1a7c | 541 | bch2_fs_read_only(c); |
1ada1606 | 542 | up_write(&c->state_lock); |
883f1a7c | 543 | |
1c6fdbd8 KO |
544 | for_each_member_device(ca, c, i) |
545 | if (ca->kobj.state_in_sysfs && | |
546 | ca->disk_sb.bdev) | |
547 | sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); | |
548 | ||
549 | if (c->kobj.state_in_sysfs) | |
550 | kobject_del(&c->kobj); | |
551 | ||
552 | bch2_fs_debug_exit(c); | |
553 | bch2_fs_chardev_exit(c); | |
554 | ||
555 | kobject_put(&c->time_stats); | |
556 | kobject_put(&c->opts_dir); | |
557 | kobject_put(&c->internal); | |
558 | ||
1c6fdbd8 KO |
559 | /* btree prefetch might have kicked off reads in the background: */ |
560 | bch2_btree_flush_all_reads(c); | |
561 | ||
562 | for_each_member_device(ca, c, i) | |
563 | cancel_work_sync(&ca->io_error_work); | |
564 | ||
1c6fdbd8 | 565 | cancel_work_sync(&c->read_only_work); |
d5e4dcc2 KO |
566 | } |
567 | ||
568 | void bch2_fs_free(struct bch_fs *c) | |
569 | { | |
570 | unsigned i; | |
571 | ||
572 | mutex_lock(&bch_fs_list_lock); | |
573 | list_del(&c->list); | |
574 | mutex_unlock(&bch_fs_list_lock); | |
575 | ||
576 | closure_sync(&c->cl); | |
577 | closure_debug_destroy(&c->cl); | |
578 | ||
579 | for (i = 0; i < c->sb.nr_devices; i++) { | |
580 | struct bch_dev *ca = rcu_dereference_protected(c->devs[i], true); | |
1c6fdbd8 | 581 | |
d5e4dcc2 KO |
582 | if (ca) { |
583 | bch2_free_super(&ca->disk_sb); | |
584 | bch2_dev_free(ca); | |
585 | } | |
586 | } | |
1c6fdbd8 | 587 | |
af1c6871 KO |
588 | bch_verbose(c, "shutdown complete"); |
589 | ||
1c6fdbd8 KO |
590 | kobject_put(&c->kobj); |
591 | } | |
592 | ||
d5e4dcc2 KO |
593 | void bch2_fs_stop(struct bch_fs *c) |
594 | { | |
595 | __bch2_fs_stop(c); | |
596 | bch2_fs_free(c); | |
597 | } | |
598 | ||
1c6fdbd8 KO |
599 | static const char *bch2_fs_online(struct bch_fs *c) |
600 | { | |
601 | struct bch_dev *ca; | |
602 | const char *err = NULL; | |
603 | unsigned i; | |
604 | int ret; | |
605 | ||
606 | lockdep_assert_held(&bch_fs_list_lock); | |
607 | ||
608 | if (!list_empty(&c->list)) | |
609 | return NULL; | |
610 | ||
611 | if (__bch2_uuid_to_fs(c->sb.uuid)) | |
612 | return "filesystem UUID already open"; | |
613 | ||
614 | ret = bch2_fs_chardev_init(c); | |
615 | if (ret) | |
616 | return "error creating character device"; | |
617 | ||
618 | bch2_fs_debug_init(c); | |
619 | ||
620 | if (kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) || | |
621 | kobject_add(&c->internal, &c->kobj, "internal") || | |
622 | kobject_add(&c->opts_dir, &c->kobj, "options") || | |
623 | kobject_add(&c->time_stats, &c->kobj, "time_stats") || | |
624 | bch2_opts_create_sysfs_files(&c->opts_dir)) | |
625 | return "error creating sysfs objects"; | |
626 | ||
1ada1606 | 627 | down_write(&c->state_lock); |
1c6fdbd8 KO |
628 | |
629 | err = "error creating sysfs objects"; | |
3a402c8d KO |
630 | for_each_member_device(ca, c, i) |
631 | if (bch2_dev_sysfs_online(c, ca)) { | |
632 | percpu_ref_put(&ca->ref); | |
1c6fdbd8 | 633 | goto err; |
3a402c8d | 634 | } |
1c6fdbd8 KO |
635 | |
636 | list_add(&c->list, &bch_fs_list); | |
637 | err = NULL; | |
638 | err: | |
1ada1606 | 639 | up_write(&c->state_lock); |
1c6fdbd8 KO |
640 | return err; |
641 | } | |
642 | ||
643 | static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts) | |
644 | { | |
645 | struct bch_sb_field_members *mi; | |
646 | struct bch_fs *c; | |
ecf37a4a | 647 | unsigned i, iter_size; |
1c6fdbd8 KO |
648 | const char *err; |
649 | ||
650 | pr_verbose_init(opts, ""); | |
651 | ||
652 | c = kvpmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO); | |
653 | if (!c) | |
654 | goto out; | |
655 | ||
656 | __module_get(THIS_MODULE); | |
657 | ||
505b7a4c KO |
658 | closure_init(&c->cl, NULL); |
659 | ||
660 | c->kobj.kset = bcachefs_kset; | |
661 | kobject_init(&c->kobj, &bch2_fs_ktype); | |
662 | kobject_init(&c->internal, &bch2_fs_internal_ktype); | |
663 | kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype); | |
664 | kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype); | |
665 | ||
1c6fdbd8 KO |
666 | c->minor = -1; |
667 | c->disk_sb.fs_sb = true; | |
668 | ||
1ada1606 | 669 | init_rwsem(&c->state_lock); |
1c6fdbd8 KO |
670 | mutex_init(&c->sb_lock); |
671 | mutex_init(&c->replicas_gc_lock); | |
672 | mutex_init(&c->btree_root_lock); | |
673 | INIT_WORK(&c->read_only_work, bch2_fs_read_only_work); | |
674 | ||
675 | init_rwsem(&c->gc_lock); | |
676 | ||
677 | for (i = 0; i < BCH_TIME_STAT_NR; i++) | |
678 | bch2_time_stats_init(&c->times[i]); | |
679 | ||
e6d11615 | 680 | bch2_fs_copygc_init(c); |
2ca88e5a | 681 | bch2_fs_btree_key_cache_init_early(&c->btree_key_cache); |
b092dadd KO |
682 | bch2_fs_allocator_background_init(c); |
683 | bch2_fs_allocator_foreground_init(c); | |
1c6fdbd8 KO |
684 | bch2_fs_rebalance_init(c); |
685 | bch2_fs_quota_init(c); | |
686 | ||
687 | INIT_LIST_HEAD(&c->list); | |
688 | ||
4d8100da KO |
689 | mutex_init(&c->usage_scratch_lock); |
690 | ||
1c6fdbd8 | 691 | mutex_init(&c->bio_bounce_pages_lock); |
14b393ee | 692 | mutex_init(&c->snapshot_table_lock); |
1c6fdbd8 | 693 | |
1c6fdbd8 | 694 | spin_lock_init(&c->btree_write_error_lock); |
1c6fdbd8 | 695 | |
1dd7f9d9 KO |
696 | INIT_WORK(&c->journal_seq_blacklist_gc_work, |
697 | bch2_blacklist_entries_gc); | |
698 | ||
f1d786a0 | 699 | INIT_LIST_HEAD(&c->journal_entries); |
5b593ee1 | 700 | INIT_LIST_HEAD(&c->journal_iters); |
f1d786a0 | 701 | |
1c6fdbd8 KO |
702 | INIT_LIST_HEAD(&c->fsck_errors); |
703 | mutex_init(&c->fsck_error_lock); | |
704 | ||
703e2a43 KO |
705 | INIT_LIST_HEAD(&c->ec_stripe_head_list); |
706 | mutex_init(&c->ec_stripe_head_lock); | |
707 | ||
708 | INIT_LIST_HEAD(&c->ec_stripe_new_list); | |
709 | mutex_init(&c->ec_stripe_new_lock); | |
710 | ||
8dd6ed94 BH |
711 | INIT_LIST_HEAD(&c->data_progress_list); |
712 | mutex_init(&c->data_progress_lock); | |
713 | ||
cd575ddf KO |
714 | spin_lock_init(&c->ec_stripes_heap_lock); |
715 | ||
1c6fdbd8 KO |
716 | seqcount_init(&c->gc_pos_lock); |
717 | ||
5e82a9a1 KO |
718 | seqcount_init(&c->usage_lock); |
719 | ||
ef1b2092 KO |
720 | sema_init(&c->io_in_flight, 128); |
721 | ||
1c6fdbd8 KO |
722 | c->copy_gc_enabled = 1; |
723 | c->rebalance.enabled = 1; | |
724 | c->promote_whole_extents = true; | |
725 | ||
726 | c->journal.write_time = &c->times[BCH_TIME_journal_write]; | |
727 | c->journal.delay_time = &c->times[BCH_TIME_journal_delay]; | |
49a67206 | 728 | c->journal.blocked_time = &c->times[BCH_TIME_blocked_journal]; |
1c6fdbd8 KO |
729 | c->journal.flush_seq_time = &c->times[BCH_TIME_journal_flush_seq]; |
730 | ||
731 | bch2_fs_btree_cache_init_early(&c->btree_cache); | |
732 | ||
fca1223c KO |
733 | mutex_init(&c->sectors_available_lock); |
734 | ||
73e6ab95 KO |
735 | if (percpu_init_rwsem(&c->mark_lock)) |
736 | goto err; | |
737 | ||
1c6fdbd8 KO |
738 | mutex_lock(&c->sb_lock); |
739 | ||
740 | if (bch2_sb_to_fs(c, sb)) { | |
741 | mutex_unlock(&c->sb_lock); | |
742 | goto err; | |
743 | } | |
744 | ||
745 | mutex_unlock(&c->sb_lock); | |
746 | ||
747 | scnprintf(c->name, sizeof(c->name), "%pU", &c->sb.user_uuid); | |
748 | ||
749 | c->opts = bch2_opts_default; | |
750 | bch2_opts_apply(&c->opts, bch2_opts_from_sb(sb)); | |
751 | bch2_opts_apply(&c->opts, opts); | |
752 | ||
753 | c->block_bits = ilog2(c->opts.block_size); | |
754 | c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c); | |
755 | ||
1c6fdbd8 KO |
756 | if (bch2_fs_init_fault("fs_alloc")) |
757 | goto err; | |
758 | ||
ae2f17d5 | 759 | iter_size = sizeof(struct sort_iter) + |
1c6fdbd8 | 760 | (btree_blocks(c) + 1) * 2 * |
ae2f17d5 | 761 | sizeof(struct sort_iter_set); |
1c6fdbd8 | 762 | |
b5e8a699 KO |
763 | c->inode_shard_bits = ilog2(roundup_pow_of_two(num_possible_cpus())); |
764 | ||
731bdd2e | 765 | if (!(c->btree_update_wq = alloc_workqueue("bcachefs", |
2f33ece9 | 766 | WQ_FREEZABLE|WQ_MEM_RECLAIM, 1)) || |
9f1833ca | 767 | !(c->btree_io_complete_wq = alloc_workqueue("bcachefs_btree_io", |
9f2772c4 | 768 | WQ_FREEZABLE|WQ_MEM_RECLAIM, 1)) || |
2f33ece9 KO |
769 | !(c->copygc_wq = alloc_workqueue("bcachefs_copygc", |
770 | WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) || | |
731bdd2e KO |
771 | !(c->io_complete_wq = alloc_workqueue("bcachefs_io", |
772 | WQ_FREEZABLE|WQ_HIGHPRI|WQ_MEM_RECLAIM, 1)) || | |
134915f3 KO |
773 | percpu_ref_init(&c->writes, bch2_writes_disabled, |
774 | PERCPU_REF_INIT_DEAD, GFP_KERNEL) || | |
1c6fdbd8 KO |
775 | mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) || |
776 | bioset_init(&c->btree_bio, 1, | |
777 | max(offsetof(struct btree_read_bio, bio), | |
778 | offsetof(struct btree_write_bio, wbio.bio)), | |
779 | BIOSET_NEED_BVECS) || | |
5663a415 | 780 | !(c->pcpu = alloc_percpu(struct bch_fs_pcpu)) || |
5e82a9a1 | 781 | !(c->online_reserved = alloc_percpu(u64)) || |
67e0dd8f | 782 | !(c->btree_paths_bufs = alloc_percpu(struct btree_path_buf)) || |
1c6fdbd8 KO |
783 | mempool_init_kvpmalloc_pool(&c->btree_bounce_pool, 1, |
784 | btree_bytes(c)) || | |
35189e09 | 785 | mempool_init_kmalloc_pool(&c->large_bkey_pool, 1, 2048) || |
b5e8a699 KO |
786 | !(c->unused_inode_hints = kcalloc(1U << c->inode_shard_bits, |
787 | sizeof(u64), GFP_KERNEL)) || | |
1c6fdbd8 KO |
788 | bch2_io_clock_init(&c->io_clock[READ]) || |
789 | bch2_io_clock_init(&c->io_clock[WRITE]) || | |
790 | bch2_fs_journal_init(&c->journal) || | |
2c5af169 | 791 | bch2_fs_replicas_init(c) || |
1c6fdbd8 | 792 | bch2_fs_btree_cache_init(c) || |
2ca88e5a | 793 | bch2_fs_btree_key_cache_init(&c->btree_key_cache) || |
36e9d698 | 794 | bch2_fs_btree_iter_init(c) || |
c823c339 | 795 | bch2_fs_btree_interior_update_init(c) || |
14b393ee | 796 | bch2_fs_subvolumes_init(c) || |
1c6fdbd8 KO |
797 | bch2_fs_io_init(c) || |
798 | bch2_fs_encryption_init(c) || | |
799 | bch2_fs_compress_init(c) || | |
cd575ddf | 800 | bch2_fs_ec_init(c) || |
1c6fdbd8 KO |
801 | bch2_fs_fsio_init(c)) |
802 | goto err; | |
803 | ||
fae1157d KO |
804 | if (c->opts.nochanges) |
805 | set_bit(JOURNAL_NOCHANGES, &c->journal.flags); | |
806 | ||
1c6fdbd8 KO |
807 | mi = bch2_sb_get_members(c->disk_sb.sb); |
808 | for (i = 0; i < c->sb.nr_devices; i++) | |
809 | if (bch2_dev_exists(c->disk_sb.sb, mi, i) && | |
810 | bch2_dev_alloc(c, i)) | |
811 | goto err; | |
812 | ||
4b8f89af KO |
813 | bch2_journal_entry_res_resize(&c->journal, |
814 | &c->btree_root_journal_res, | |
815 | BTREE_ID_NR * (JSET_KEYS_U64s + BKEY_BTREE_PTR_U64s_MAX)); | |
816 | bch2_dev_usage_journal_reserve(c); | |
817 | bch2_journal_entry_res_resize(&c->journal, | |
818 | &c->clock_journal_res, | |
819 | (sizeof(struct jset_entry_clock) / sizeof(u64)) * 2); | |
820 | ||
1c6fdbd8 KO |
821 | mutex_lock(&bch_fs_list_lock); |
822 | err = bch2_fs_online(c); | |
823 | mutex_unlock(&bch_fs_list_lock); | |
824 | if (err) { | |
825 | bch_err(c, "bch2_fs_online() error: %s", err); | |
826 | goto err; | |
827 | } | |
828 | out: | |
829 | pr_verbose_init(opts, "ret %i", c ? 0 : -ENOMEM); | |
830 | return c; | |
831 | err: | |
832 | bch2_fs_free(c); | |
833 | c = NULL; | |
834 | goto out; | |
835 | } | |
836 | ||
619f5bee KO |
837 | noinline_for_stack |
838 | static void print_mount_opts(struct bch_fs *c) | |
839 | { | |
840 | enum bch_opt_id i; | |
841 | char buf[512]; | |
842 | struct printbuf p = PBUF(buf); | |
843 | bool first = true; | |
844 | ||
845 | strcpy(buf, "(null)"); | |
846 | ||
847 | if (c->opts.read_only) { | |
848 | pr_buf(&p, "ro"); | |
849 | first = false; | |
850 | } | |
851 | ||
852 | for (i = 0; i < bch2_opts_nr; i++) { | |
853 | const struct bch_option *opt = &bch2_opt_table[i]; | |
854 | u64 v = bch2_opt_get_by_id(&c->opts, i); | |
855 | ||
856 | if (!(opt->mode & OPT_MOUNT)) | |
857 | continue; | |
858 | ||
859 | if (v == bch2_opt_get_by_id(&bch2_opts_default, i)) | |
860 | continue; | |
861 | ||
862 | if (!first) | |
863 | pr_buf(&p, ","); | |
864 | first = false; | |
865 | bch2_opt_to_text(&p, c, opt, v, OPT_SHOW_MOUNT_STYLE); | |
866 | } | |
867 | ||
868 | bch_info(c, "mounted with opts: %s", buf); | |
869 | } | |
870 | ||
871 | int bch2_fs_start(struct bch_fs *c) | |
1c6fdbd8 KO |
872 | { |
873 | const char *err = "cannot allocate memory"; | |
874 | struct bch_sb_field_members *mi; | |
875 | struct bch_dev *ca; | |
a420eea6 | 876 | time64_t now = ktime_get_real_seconds(); |
1c6fdbd8 KO |
877 | unsigned i; |
878 | int ret = -EINVAL; | |
879 | ||
1ada1606 | 880 | down_write(&c->state_lock); |
1c6fdbd8 | 881 | |
134915f3 | 882 | BUG_ON(test_bit(BCH_FS_STARTED, &c->flags)); |
1c6fdbd8 KO |
883 | |
884 | mutex_lock(&c->sb_lock); | |
885 | ||
886 | for_each_online_member(ca, c, i) | |
887 | bch2_sb_from_fs(c, ca); | |
888 | ||
889 | mi = bch2_sb_get_members(c->disk_sb.sb); | |
890 | for_each_online_member(ca, c, i) | |
891 | mi->members[ca->dev_idx].last_mount = cpu_to_le64(now); | |
892 | ||
893 | mutex_unlock(&c->sb_lock); | |
894 | ||
895 | for_each_rw_member(ca, c, i) | |
896 | bch2_dev_allocator_add(c, ca); | |
897 | bch2_recalc_capacity(c); | |
898 | ||
899 | ret = BCH_SB_INITIALIZED(c->disk_sb.sb) | |
900 | ? bch2_fs_recovery(c) | |
901 | : bch2_fs_initialize(c); | |
902 | if (ret) | |
903 | goto err; | |
cd575ddf KO |
904 | |
905 | ret = bch2_opts_check_may_set(c); | |
906 | if (ret) | |
907 | goto err; | |
1c6fdbd8 KO |
908 | |
909 | err = "dynamic fault"; | |
619f5bee | 910 | ret = -EINVAL; |
1c6fdbd8 KO |
911 | if (bch2_fs_init_fault("fs_start")) |
912 | goto err; | |
913 | ||
a9310ab0 KO |
914 | set_bit(BCH_FS_STARTED, &c->flags); |
915 | ||
9f20ed15 KO |
916 | /* |
917 | * Allocator threads don't start filling copygc reserve until after we | |
918 | * set BCH_FS_STARTED - wake them now: | |
2ee47eec KO |
919 | * |
920 | * XXX ugly hack: | |
921 | * Need to set ca->allocator_state here instead of relying on the | |
922 | * allocator threads to do it to avoid racing with the copygc threads | |
923 | * checking it and thinking they have no alloc reserve: | |
9f20ed15 | 924 | */ |
2ee47eec KO |
925 | for_each_online_member(ca, c, i) { |
926 | ca->allocator_state = ALLOCATOR_running; | |
9f20ed15 | 927 | bch2_wake_allocator(ca); |
2ee47eec | 928 | } |
9f20ed15 | 929 | |
619f5bee | 930 | if (c->opts.read_only || c->opts.nochanges) { |
1c6fdbd8 KO |
931 | bch2_fs_read_only(c); |
932 | } else { | |
619f5bee KO |
933 | err = "error going read write"; |
934 | ret = !test_bit(BCH_FS_RW, &c->flags) | |
935 | ? bch2_fs_read_write(c) | |
936 | : bch2_fs_read_write_late(c); | |
937 | if (ret) | |
1c6fdbd8 KO |
938 | goto err; |
939 | } | |
940 | ||
619f5bee KO |
941 | print_mount_opts(c); |
942 | ret = 0; | |
1c6fdbd8 | 943 | out: |
1ada1606 | 944 | up_write(&c->state_lock); |
619f5bee | 945 | return ret; |
1c6fdbd8 KO |
946 | err: |
947 | switch (ret) { | |
948 | case BCH_FSCK_ERRORS_NOT_FIXED: | |
949 | bch_err(c, "filesystem contains errors: please report this to the developers"); | |
950 | pr_cont("mount with -o fix_errors to repair\n"); | |
951 | err = "fsck error"; | |
952 | break; | |
953 | case BCH_FSCK_REPAIR_UNIMPLEMENTED: | |
954 | bch_err(c, "filesystem contains errors: please report this to the developers"); | |
955 | pr_cont("repair unimplemented: inform the developers so that it can be added\n"); | |
956 | err = "fsck error"; | |
957 | break; | |
958 | case BCH_FSCK_REPAIR_IMPOSSIBLE: | |
959 | bch_err(c, "filesystem contains errors, but repair impossible"); | |
960 | err = "fsck error"; | |
961 | break; | |
962 | case BCH_FSCK_UNKNOWN_VERSION: | |
963 | err = "unknown metadata version";; | |
964 | break; | |
965 | case -ENOMEM: | |
966 | err = "cannot allocate memory"; | |
967 | break; | |
968 | case -EIO: | |
969 | err = "IO error"; | |
970 | break; | |
971 | } | |
972 | ||
9516950c KO |
973 | if (ret >= 0) |
974 | ret = -EIO; | |
1c6fdbd8 KO |
975 | goto out; |
976 | } | |
977 | ||
978 | static const char *bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c) | |
979 | { | |
980 | struct bch_sb_field_members *sb_mi; | |
981 | ||
982 | sb_mi = bch2_sb_get_members(sb); | |
983 | if (!sb_mi) | |
984 | return "Invalid superblock: member info area missing"; | |
985 | ||
986 | if (le16_to_cpu(sb->block_size) != c->opts.block_size) | |
987 | return "mismatched block size"; | |
988 | ||
989 | if (le16_to_cpu(sb_mi->members[sb->dev_idx].bucket_size) < | |
990 | BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb)) | |
991 | return "new cache bucket size is too small"; | |
992 | ||
993 | return NULL; | |
994 | } | |
995 | ||
996 | static const char *bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb) | |
997 | { | |
998 | struct bch_sb *newest = | |
999 | le64_to_cpu(fs->seq) > le64_to_cpu(sb->seq) ? fs : sb; | |
1000 | struct bch_sb_field_members *mi = bch2_sb_get_members(newest); | |
1001 | ||
1002 | if (!uuid_equal(&fs->uuid, &sb->uuid)) | |
1003 | return "device not a member of filesystem"; | |
1004 | ||
1005 | if (!bch2_dev_exists(newest, mi, sb->dev_idx)) | |
1006 | return "device has been removed"; | |
1007 | ||
1008 | if (fs->block_size != sb->block_size) | |
1009 | return "mismatched block size"; | |
1010 | ||
1011 | return NULL; | |
1012 | } | |
1013 | ||
1014 | /* Device startup/shutdown: */ | |
1015 | ||
1016 | static void bch2_dev_release(struct kobject *kobj) | |
1017 | { | |
1018 | struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj); | |
1019 | ||
1020 | kfree(ca); | |
1021 | } | |
1022 | ||
1023 | static void bch2_dev_free(struct bch_dev *ca) | |
1024 | { | |
59a74051 KO |
1025 | bch2_dev_allocator_stop(ca); |
1026 | ||
1c6fdbd8 KO |
1027 | cancel_work_sync(&ca->io_error_work); |
1028 | ||
1029 | if (ca->kobj.state_in_sysfs && | |
1030 | ca->disk_sb.bdev) | |
1031 | sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); | |
1032 | ||
1033 | if (ca->kobj.state_in_sysfs) | |
1034 | kobject_del(&ca->kobj); | |
1035 | ||
1036 | bch2_free_super(&ca->disk_sb); | |
1037 | bch2_dev_journal_exit(ca); | |
1038 | ||
1039 | free_percpu(ca->io_done); | |
1040 | bioset_exit(&ca->replica_set); | |
1041 | bch2_dev_buckets_free(ca); | |
d1170ce5 | 1042 | free_page((unsigned long) ca->sb_read_scratch); |
1c6fdbd8 KO |
1043 | |
1044 | bch2_time_stats_exit(&ca->io_latency[WRITE]); | |
1045 | bch2_time_stats_exit(&ca->io_latency[READ]); | |
1046 | ||
1047 | percpu_ref_exit(&ca->io_ref); | |
1048 | percpu_ref_exit(&ca->ref); | |
1049 | kobject_put(&ca->kobj); | |
1050 | } | |
1051 | ||
1052 | static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca) | |
1053 | { | |
1054 | ||
1055 | lockdep_assert_held(&c->state_lock); | |
1056 | ||
1057 | if (percpu_ref_is_zero(&ca->io_ref)) | |
1058 | return; | |
1059 | ||
1060 | __bch2_dev_read_only(c, ca); | |
1061 | ||
1062 | reinit_completion(&ca->io_ref_completion); | |
1063 | percpu_ref_kill(&ca->io_ref); | |
1064 | wait_for_completion(&ca->io_ref_completion); | |
1065 | ||
1066 | if (ca->kobj.state_in_sysfs) { | |
1067 | sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); | |
1068 | sysfs_remove_link(&ca->kobj, "block"); | |
1069 | } | |
1070 | ||
1071 | bch2_free_super(&ca->disk_sb); | |
1072 | bch2_dev_journal_exit(ca); | |
1073 | } | |
1074 | ||
1075 | static void bch2_dev_ref_complete(struct percpu_ref *ref) | |
1076 | { | |
1077 | struct bch_dev *ca = container_of(ref, struct bch_dev, ref); | |
1078 | ||
1079 | complete(&ca->ref_completion); | |
1080 | } | |
1081 | ||
1082 | static void bch2_dev_io_ref_complete(struct percpu_ref *ref) | |
1083 | { | |
1084 | struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref); | |
1085 | ||
1086 | complete(&ca->io_ref_completion); | |
1087 | } | |
1088 | ||
1089 | static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca) | |
1090 | { | |
1091 | int ret; | |
1092 | ||
1093 | if (!c->kobj.state_in_sysfs) | |
1094 | return 0; | |
1095 | ||
1096 | if (!ca->kobj.state_in_sysfs) { | |
1097 | ret = kobject_add(&ca->kobj, &c->kobj, | |
1098 | "dev-%u", ca->dev_idx); | |
1099 | if (ret) | |
1100 | return ret; | |
1101 | } | |
1102 | ||
1103 | if (ca->disk_sb.bdev) { | |
1104 | struct kobject *block = bdev_kobj(ca->disk_sb.bdev); | |
1105 | ||
1106 | ret = sysfs_create_link(block, &ca->kobj, "bcachefs"); | |
1107 | if (ret) | |
1108 | return ret; | |
1109 | ||
1110 | ret = sysfs_create_link(&ca->kobj, block, "block"); | |
1111 | if (ret) | |
1112 | return ret; | |
1113 | } | |
1114 | ||
1115 | return 0; | |
1116 | } | |
1117 | ||
1118 | static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c, | |
1119 | struct bch_member *member) | |
1120 | { | |
1121 | struct bch_dev *ca; | |
1122 | ||
1123 | ca = kzalloc(sizeof(*ca), GFP_KERNEL); | |
1124 | if (!ca) | |
1125 | return NULL; | |
1126 | ||
1127 | kobject_init(&ca->kobj, &bch2_dev_ktype); | |
1128 | init_completion(&ca->ref_completion); | |
1129 | init_completion(&ca->io_ref_completion); | |
1130 | ||
1131 | init_rwsem(&ca->bucket_lock); | |
1132 | ||
1c6fdbd8 KO |
1133 | INIT_WORK(&ca->io_error_work, bch2_io_error_work); |
1134 | ||
1135 | bch2_time_stats_init(&ca->io_latency[READ]); | |
1136 | bch2_time_stats_init(&ca->io_latency[WRITE]); | |
1137 | ||
1138 | ca->mi = bch2_mi_to_cpu(member); | |
1139 | ca->uuid = member->uuid; | |
1140 | ||
1141 | if (opt_defined(c->opts, discard)) | |
1142 | ca->mi.discard = opt_get(c->opts, discard); | |
1143 | ||
1144 | if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete, | |
1145 | 0, GFP_KERNEL) || | |
1146 | percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete, | |
1147 | PERCPU_REF_INIT_DEAD, GFP_KERNEL) || | |
d1170ce5 | 1148 | !(ca->sb_read_scratch = (void *) __get_free_page(GFP_KERNEL)) || |
1c6fdbd8 KO |
1149 | bch2_dev_buckets_alloc(c, ca) || |
1150 | bioset_init(&ca->replica_set, 4, | |
1151 | offsetof(struct bch_write_bio, bio), 0) || | |
1152 | !(ca->io_done = alloc_percpu(*ca->io_done))) | |
1153 | goto err; | |
1154 | ||
1155 | return ca; | |
1156 | err: | |
1157 | bch2_dev_free(ca); | |
1158 | return NULL; | |
1159 | } | |
1160 | ||
1161 | static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca, | |
1162 | unsigned dev_idx) | |
1163 | { | |
1164 | ca->dev_idx = dev_idx; | |
1165 | __set_bit(ca->dev_idx, ca->self.d); | |
1166 | scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx); | |
1167 | ||
1168 | ca->fs = c; | |
1169 | rcu_assign_pointer(c->devs[ca->dev_idx], ca); | |
1170 | ||
1171 | if (bch2_dev_sysfs_online(c, ca)) | |
1172 | pr_warn("error creating sysfs objects"); | |
1173 | } | |
1174 | ||
1175 | static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx) | |
1176 | { | |
1177 | struct bch_member *member = | |
1178 | bch2_sb_get_members(c->disk_sb.sb)->members + dev_idx; | |
1179 | struct bch_dev *ca = NULL; | |
1180 | int ret = 0; | |
1181 | ||
1182 | pr_verbose_init(c->opts, ""); | |
1183 | ||
1184 | if (bch2_fs_init_fault("dev_alloc")) | |
1185 | goto err; | |
1186 | ||
1187 | ca = __bch2_dev_alloc(c, member); | |
1188 | if (!ca) | |
1189 | goto err; | |
1190 | ||
220d2062 KO |
1191 | ca->fs = c; |
1192 | ||
2436cb9f | 1193 | if (ca->mi.state == BCH_MEMBER_STATE_rw && |
59a74051 KO |
1194 | bch2_dev_allocator_start(ca)) { |
1195 | bch2_dev_free(ca); | |
1196 | goto err; | |
1197 | } | |
1198 | ||
1c6fdbd8 KO |
1199 | bch2_dev_attach(c, ca, dev_idx); |
1200 | out: | |
1201 | pr_verbose_init(c->opts, "ret %i", ret); | |
1202 | return ret; | |
1203 | err: | |
1204 | if (ca) | |
1205 | bch2_dev_free(ca); | |
1206 | ret = -ENOMEM; | |
1207 | goto out; | |
1208 | } | |
1209 | ||
1210 | static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb) | |
1211 | { | |
1212 | unsigned ret; | |
1213 | ||
1214 | if (bch2_dev_is_online(ca)) { | |
1215 | bch_err(ca, "already have device online in slot %u", | |
1216 | sb->sb->dev_idx); | |
1217 | return -EINVAL; | |
1218 | } | |
1219 | ||
1220 | if (get_capacity(sb->bdev->bd_disk) < | |
1221 | ca->mi.bucket_size * ca->mi.nbuckets) { | |
1222 | bch_err(ca, "cannot online: device too small"); | |
1223 | return -EINVAL; | |
1224 | } | |
1225 | ||
1226 | BUG_ON(!percpu_ref_is_zero(&ca->io_ref)); | |
1227 | ||
1228 | if (get_capacity(sb->bdev->bd_disk) < | |
1229 | ca->mi.bucket_size * ca->mi.nbuckets) { | |
1230 | bch_err(ca, "device too small"); | |
1231 | return -EINVAL; | |
1232 | } | |
1233 | ||
1234 | ret = bch2_dev_journal_init(ca, sb->sb); | |
1235 | if (ret) | |
1236 | return ret; | |
1237 | ||
1238 | /* Commit: */ | |
1239 | ca->disk_sb = *sb; | |
1240 | memset(sb, 0, sizeof(*sb)); | |
1241 | ||
1c6fdbd8 KO |
1242 | percpu_ref_reinit(&ca->io_ref); |
1243 | ||
1244 | return 0; | |
1245 | } | |
1246 | ||
1247 | static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb) | |
1248 | { | |
1249 | struct bch_dev *ca; | |
1250 | int ret; | |
1251 | ||
1252 | lockdep_assert_held(&c->state_lock); | |
1253 | ||
1254 | if (le64_to_cpu(sb->sb->seq) > | |
1255 | le64_to_cpu(c->disk_sb.sb->seq)) | |
1256 | bch2_sb_to_fs(c, sb->sb); | |
1257 | ||
1258 | BUG_ON(sb->sb->dev_idx >= c->sb.nr_devices || | |
1259 | !c->devs[sb->sb->dev_idx]); | |
1260 | ||
1261 | ca = bch_dev_locked(c, sb->sb->dev_idx); | |
1262 | ||
1263 | ret = __bch2_dev_attach_bdev(ca, sb); | |
1264 | if (ret) | |
1265 | return ret; | |
1266 | ||
1267 | bch2_dev_sysfs_online(c, ca); | |
1268 | ||
1269 | if (c->sb.nr_devices == 1) | |
1270 | snprintf(c->name, sizeof(c->name), "%pg", ca->disk_sb.bdev); | |
1271 | snprintf(ca->name, sizeof(ca->name), "%pg", ca->disk_sb.bdev); | |
1272 | ||
1273 | rebalance_wakeup(c); | |
1274 | return 0; | |
1275 | } | |
1276 | ||
1277 | /* Device management: */ | |
1278 | ||
1279 | /* | |
1280 | * Note: this function is also used by the error paths - when a particular | |
1281 | * device sees an error, we call it to determine whether we can just set the | |
1282 | * device RO, or - if this function returns false - we'll set the whole | |
1283 | * filesystem RO: | |
1284 | * | |
1285 | * XXX: maybe we should be more explicit about whether we're changing state | |
1286 | * because we got an error or what have you? | |
1287 | */ | |
1288 | bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca, | |
1289 | enum bch_member_state new_state, int flags) | |
1290 | { | |
1291 | struct bch_devs_mask new_online_devs; | |
1c6fdbd8 KO |
1292 | struct bch_dev *ca2; |
1293 | int i, nr_rw = 0, required; | |
1294 | ||
1295 | lockdep_assert_held(&c->state_lock); | |
1296 | ||
1297 | switch (new_state) { | |
2436cb9f | 1298 | case BCH_MEMBER_STATE_rw: |
1c6fdbd8 | 1299 | return true; |
2436cb9f KO |
1300 | case BCH_MEMBER_STATE_ro: |
1301 | if (ca->mi.state != BCH_MEMBER_STATE_rw) | |
1c6fdbd8 KO |
1302 | return true; |
1303 | ||
1304 | /* do we have enough devices to write to? */ | |
1305 | for_each_member_device(ca2, c, i) | |
1306 | if (ca2 != ca) | |
2436cb9f | 1307 | nr_rw += ca2->mi.state == BCH_MEMBER_STATE_rw; |
1c6fdbd8 KO |
1308 | |
1309 | required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED) | |
1310 | ? c->opts.metadata_replicas | |
1311 | : c->opts.metadata_replicas_required, | |
1312 | !(flags & BCH_FORCE_IF_DATA_DEGRADED) | |
1313 | ? c->opts.data_replicas | |
1314 | : c->opts.data_replicas_required); | |
1315 | ||
1316 | return nr_rw >= required; | |
2436cb9f KO |
1317 | case BCH_MEMBER_STATE_failed: |
1318 | case BCH_MEMBER_STATE_spare: | |
1319 | if (ca->mi.state != BCH_MEMBER_STATE_rw && | |
1320 | ca->mi.state != BCH_MEMBER_STATE_ro) | |
1c6fdbd8 KO |
1321 | return true; |
1322 | ||
1323 | /* do we have enough devices to read from? */ | |
1324 | new_online_devs = bch2_online_devs(c); | |
1325 | __clear_bit(ca->dev_idx, new_online_devs.d); | |
1326 | ||
fcb3431b | 1327 | return bch2_have_enough_devs(c, new_online_devs, flags, false); |
1c6fdbd8 KO |
1328 | default: |
1329 | BUG(); | |
1330 | } | |
1331 | } | |
1332 | ||
1333 | static bool bch2_fs_may_start(struct bch_fs *c) | |
1334 | { | |
1c6fdbd8 KO |
1335 | struct bch_sb_field_members *mi; |
1336 | struct bch_dev *ca; | |
fcb3431b KO |
1337 | unsigned i, flags = 0; |
1338 | ||
1339 | if (c->opts.very_degraded) | |
1340 | flags |= BCH_FORCE_IF_DEGRADED|BCH_FORCE_IF_LOST; | |
1c6fdbd8 | 1341 | |
fcb3431b KO |
1342 | if (c->opts.degraded) |
1343 | flags |= BCH_FORCE_IF_DEGRADED; | |
1344 | ||
1345 | if (!c->opts.degraded && | |
1346 | !c->opts.very_degraded) { | |
1c6fdbd8 KO |
1347 | mutex_lock(&c->sb_lock); |
1348 | mi = bch2_sb_get_members(c->disk_sb.sb); | |
1349 | ||
1350 | for (i = 0; i < c->disk_sb.sb->nr_devices; i++) { | |
1351 | if (!bch2_dev_exists(c->disk_sb.sb, mi, i)) | |
1352 | continue; | |
1353 | ||
1354 | ca = bch_dev_locked(c, i); | |
1355 | ||
1356 | if (!bch2_dev_is_online(ca) && | |
2436cb9f KO |
1357 | (ca->mi.state == BCH_MEMBER_STATE_rw || |
1358 | ca->mi.state == BCH_MEMBER_STATE_ro)) { | |
1c6fdbd8 KO |
1359 | mutex_unlock(&c->sb_lock); |
1360 | return false; | |
1361 | } | |
1362 | } | |
1363 | mutex_unlock(&c->sb_lock); | |
1364 | } | |
1365 | ||
fcb3431b | 1366 | return bch2_have_enough_devs(c, bch2_online_devs(c), flags, true); |
1c6fdbd8 KO |
1367 | } |
1368 | ||
1369 | static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca) | |
1370 | { | |
74ed7e56 KO |
1371 | /* |
1372 | * Device going read only means the copygc reserve get smaller, so we | |
1373 | * don't want that happening while copygc is in progress: | |
1374 | */ | |
1375 | bch2_copygc_stop(c); | |
1376 | ||
1c6fdbd8 KO |
1377 | /* |
1378 | * The allocator thread itself allocates btree nodes, so stop it first: | |
1379 | */ | |
1380 | bch2_dev_allocator_stop(ca); | |
1381 | bch2_dev_allocator_remove(c, ca); | |
1382 | bch2_dev_journal_stop(&c->journal, ca); | |
74ed7e56 KO |
1383 | |
1384 | bch2_copygc_start(c); | |
1c6fdbd8 KO |
1385 | } |
1386 | ||
1387 | static const char *__bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca) | |
1388 | { | |
1389 | lockdep_assert_held(&c->state_lock); | |
1390 | ||
2436cb9f | 1391 | BUG_ON(ca->mi.state != BCH_MEMBER_STATE_rw); |
1c6fdbd8 KO |
1392 | |
1393 | bch2_dev_allocator_add(c, ca); | |
1394 | bch2_recalc_capacity(c); | |
1395 | ||
1396 | if (bch2_dev_allocator_start(ca)) | |
1397 | return "error starting allocator thread"; | |
1398 | ||
1c6fdbd8 KO |
1399 | return NULL; |
1400 | } | |
1401 | ||
1402 | int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, | |
1403 | enum bch_member_state new_state, int flags) | |
1404 | { | |
1405 | struct bch_sb_field_members *mi; | |
1406 | int ret = 0; | |
1407 | ||
1408 | if (ca->mi.state == new_state) | |
1409 | return 0; | |
1410 | ||
1411 | if (!bch2_dev_state_allowed(c, ca, new_state, flags)) | |
1412 | return -EINVAL; | |
1413 | ||
2436cb9f | 1414 | if (new_state != BCH_MEMBER_STATE_rw) |
1c6fdbd8 KO |
1415 | __bch2_dev_read_only(c, ca); |
1416 | ||
2436cb9f | 1417 | bch_notice(ca, "%s", bch2_member_states[new_state]); |
1c6fdbd8 KO |
1418 | |
1419 | mutex_lock(&c->sb_lock); | |
1420 | mi = bch2_sb_get_members(c->disk_sb.sb); | |
1421 | SET_BCH_MEMBER_STATE(&mi->members[ca->dev_idx], new_state); | |
1422 | bch2_write_super(c); | |
1423 | mutex_unlock(&c->sb_lock); | |
1424 | ||
2436cb9f | 1425 | if (new_state == BCH_MEMBER_STATE_rw && |
1c6fdbd8 KO |
1426 | __bch2_dev_read_write(c, ca)) |
1427 | ret = -ENOMEM; | |
1428 | ||
1429 | rebalance_wakeup(c); | |
1430 | ||
1431 | return ret; | |
1432 | } | |
1433 | ||
1434 | int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, | |
1435 | enum bch_member_state new_state, int flags) | |
1436 | { | |
1437 | int ret; | |
1438 | ||
1ada1606 | 1439 | down_write(&c->state_lock); |
1c6fdbd8 | 1440 | ret = __bch2_dev_set_state(c, ca, new_state, flags); |
1ada1606 | 1441 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1442 | |
1443 | return ret; | |
1444 | } | |
1445 | ||
1446 | /* Device add/removal: */ | |
1447 | ||
c0ebe3e4 | 1448 | static int bch2_dev_remove_alloc(struct bch_fs *c, struct bch_dev *ca) |
5d20ba48 KO |
1449 | { |
1450 | struct btree_trans trans; | |
1451 | size_t i; | |
1452 | int ret; | |
1453 | ||
1454 | bch2_trans_init(&trans, c, 0, 0); | |
1455 | ||
1456 | for (i = 0; i < ca->mi.nbuckets; i++) { | |
a9cb0a67 KO |
1457 | ret = lockrestart_do(&trans, |
1458 | bch2_btree_key_cache_flush(&trans, | |
1459 | BTREE_ID_alloc, POS(ca->dev_idx, i))); | |
5d20ba48 KO |
1460 | if (ret) |
1461 | break; | |
1462 | } | |
1463 | bch2_trans_exit(&trans); | |
1464 | ||
a9cb0a67 KO |
1465 | if (ret) { |
1466 | bch_err(c, "error %i removing dev alloc info", ret); | |
5d20ba48 | 1467 | return ret; |
a9cb0a67 | 1468 | } |
5d20ba48 | 1469 | |
41f8b09e | 1470 | return bch2_btree_delete_range(c, BTREE_ID_alloc, |
5d20ba48 KO |
1471 | POS(ca->dev_idx, 0), |
1472 | POS(ca->dev_idx + 1, 0), | |
1473 | NULL); | |
1474 | } | |
1475 | ||
1c6fdbd8 KO |
1476 | int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags) |
1477 | { | |
1478 | struct bch_sb_field_members *mi; | |
1479 | unsigned dev_idx = ca->dev_idx, data; | |
1480 | int ret = -EINVAL; | |
1481 | ||
1ada1606 | 1482 | down_write(&c->state_lock); |
1c6fdbd8 | 1483 | |
31ba2cd3 KO |
1484 | /* |
1485 | * We consume a reference to ca->ref, regardless of whether we succeed | |
1486 | * or fail: | |
1487 | */ | |
1488 | percpu_ref_put(&ca->ref); | |
1c6fdbd8 | 1489 | |
2436cb9f | 1490 | if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { |
1c6fdbd8 KO |
1491 | bch_err(ca, "Cannot remove without losing data"); |
1492 | goto err; | |
1493 | } | |
1494 | ||
1495 | __bch2_dev_read_only(c, ca); | |
1496 | ||
1c6fdbd8 KO |
1497 | ret = bch2_dev_data_drop(c, ca->dev_idx, flags); |
1498 | if (ret) { | |
1499 | bch_err(ca, "Remove failed: error %i dropping data", ret); | |
1500 | goto err; | |
1501 | } | |
1502 | ||
1503 | ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx); | |
1504 | if (ret) { | |
1505 | bch_err(ca, "Remove failed: error %i flushing journal", ret); | |
1506 | goto err; | |
1507 | } | |
1508 | ||
5d20ba48 | 1509 | ret = bch2_dev_remove_alloc(c, ca); |
1c6fdbd8 KO |
1510 | if (ret) { |
1511 | bch_err(ca, "Remove failed, error deleting alloc info"); | |
1512 | goto err; | |
1513 | } | |
1514 | ||
1515 | /* | |
1516 | * must flush all existing journal entries, they might have | |
1517 | * (overwritten) keys that point to the device we're removing: | |
1518 | */ | |
1519 | bch2_journal_flush_all_pins(&c->journal); | |
31ba2cd3 KO |
1520 | /* |
1521 | * hack to ensure bch2_replicas_gc2() clears out entries to this device | |
1522 | */ | |
1523 | bch2_journal_meta(&c->journal); | |
1c6fdbd8 KO |
1524 | ret = bch2_journal_error(&c->journal); |
1525 | if (ret) { | |
1526 | bch_err(ca, "Remove failed, journal error"); | |
1527 | goto err; | |
1528 | } | |
1529 | ||
31ba2cd3 KO |
1530 | ret = bch2_replicas_gc2(c); |
1531 | if (ret) { | |
1532 | bch_err(ca, "Remove failed: error %i from replicas gc", ret); | |
1533 | goto err; | |
1534 | } | |
1535 | ||
1536 | data = bch2_dev_has_data(c, ca); | |
1537 | if (data) { | |
1538 | char data_has_str[100]; | |
1539 | ||
1540 | bch2_flags_to_text(&PBUF(data_has_str), | |
1541 | bch2_data_types, data); | |
1542 | bch_err(ca, "Remove failed, still has data (%s)", data_has_str); | |
1543 | ret = -EBUSY; | |
1544 | goto err; | |
1545 | } | |
1546 | ||
1c6fdbd8 KO |
1547 | __bch2_dev_offline(c, ca); |
1548 | ||
1549 | mutex_lock(&c->sb_lock); | |
1550 | rcu_assign_pointer(c->devs[ca->dev_idx], NULL); | |
1551 | mutex_unlock(&c->sb_lock); | |
1552 | ||
1553 | percpu_ref_kill(&ca->ref); | |
1554 | wait_for_completion(&ca->ref_completion); | |
1555 | ||
1556 | bch2_dev_free(ca); | |
1557 | ||
1558 | /* | |
1559 | * Free this device's slot in the bch_member array - all pointers to | |
1560 | * this device must be gone: | |
1561 | */ | |
1562 | mutex_lock(&c->sb_lock); | |
1563 | mi = bch2_sb_get_members(c->disk_sb.sb); | |
1564 | memset(&mi->members[dev_idx].uuid, 0, sizeof(mi->members[dev_idx].uuid)); | |
1565 | ||
1566 | bch2_write_super(c); | |
1567 | ||
1568 | mutex_unlock(&c->sb_lock); | |
1ada1606 | 1569 | up_write(&c->state_lock); |
180fb49d KO |
1570 | |
1571 | bch2_dev_usage_journal_reserve(c); | |
1c6fdbd8 KO |
1572 | return 0; |
1573 | err: | |
2436cb9f | 1574 | if (ca->mi.state == BCH_MEMBER_STATE_rw && |
d3bb629d | 1575 | !percpu_ref_is_zero(&ca->io_ref)) |
1c6fdbd8 | 1576 | __bch2_dev_read_write(c, ca); |
1ada1606 | 1577 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1578 | return ret; |
1579 | } | |
1580 | ||
1581 | /* Add new device to running filesystem: */ | |
1582 | int bch2_dev_add(struct bch_fs *c, const char *path) | |
1583 | { | |
1584 | struct bch_opts opts = bch2_opts_empty(); | |
1585 | struct bch_sb_handle sb; | |
1586 | const char *err; | |
1587 | struct bch_dev *ca = NULL; | |
1588 | struct bch_sb_field_members *mi; | |
1589 | struct bch_member dev_mi; | |
114eea75 KO |
1590 | struct bucket_array *buckets; |
1591 | struct bucket *g; | |
1c6fdbd8 KO |
1592 | unsigned dev_idx, nr_devices, u64s; |
1593 | int ret; | |
1594 | ||
1595 | ret = bch2_read_super(path, &opts, &sb); | |
1596 | if (ret) | |
1597 | return ret; | |
1598 | ||
1599 | err = bch2_sb_validate(&sb); | |
1600 | if (err) | |
1601 | return -EINVAL; | |
1602 | ||
1603 | dev_mi = bch2_sb_get_members(sb.sb)->members[sb.sb->dev_idx]; | |
1604 | ||
1605 | err = bch2_dev_may_add(sb.sb, c); | |
1606 | if (err) | |
1607 | return -EINVAL; | |
1608 | ||
1609 | ca = __bch2_dev_alloc(c, &dev_mi); | |
1610 | if (!ca) { | |
1611 | bch2_free_super(&sb); | |
1612 | return -ENOMEM; | |
1613 | } | |
1614 | ||
1615 | ret = __bch2_dev_attach_bdev(ca, &sb); | |
1616 | if (ret) { | |
1617 | bch2_dev_free(ca); | |
1618 | return ret; | |
1619 | } | |
1620 | ||
6eac2c2e KO |
1621 | /* |
1622 | * We want to allocate journal on the new device before adding the new | |
1623 | * device to the filesystem because allocating after we attach requires | |
1624 | * spinning up the allocator thread, and the allocator thread requires | |
1625 | * doing btree writes, which if the existing devices are RO isn't going | |
1626 | * to work | |
1627 | * | |
1628 | * So we have to mark where the superblocks are, but marking allocated | |
1629 | * data normally updates the filesystem usage too, so we have to mark, | |
1630 | * allocate the journal, reset all the marks, then remark after we | |
1631 | * attach... | |
1632 | */ | |
bfcf840d | 1633 | bch2_mark_dev_superblock(NULL, ca, 0); |
6eac2c2e | 1634 | |
1c6fdbd8 KO |
1635 | err = "journal alloc failed"; |
1636 | ret = bch2_dev_journal_alloc(ca); | |
1637 | if (ret) | |
1638 | goto err; | |
1639 | ||
1ada1606 | 1640 | down_write(&c->state_lock); |
1c6fdbd8 KO |
1641 | mutex_lock(&c->sb_lock); |
1642 | ||
1643 | err = "insufficient space in new superblock"; | |
1644 | ret = bch2_sb_from_fs(c, ca); | |
1645 | if (ret) | |
1646 | goto err_unlock; | |
1647 | ||
1648 | mi = bch2_sb_get_members(ca->disk_sb.sb); | |
1649 | ||
1650 | if (!bch2_sb_resize_members(&ca->disk_sb, | |
1651 | le32_to_cpu(mi->field.u64s) + | |
1652 | sizeof(dev_mi) / sizeof(u64))) { | |
1653 | ret = -ENOSPC; | |
1654 | goto err_unlock; | |
1655 | } | |
1656 | ||
1657 | if (dynamic_fault("bcachefs:add:no_slot")) | |
1658 | goto no_slot; | |
1659 | ||
1660 | mi = bch2_sb_get_members(c->disk_sb.sb); | |
1661 | for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++) | |
1662 | if (!bch2_dev_exists(c->disk_sb.sb, mi, dev_idx)) | |
1663 | goto have_slot; | |
1664 | no_slot: | |
1665 | err = "no slots available in superblock"; | |
1666 | ret = -ENOSPC; | |
1667 | goto err_unlock; | |
1668 | ||
1669 | have_slot: | |
1670 | nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices); | |
1671 | u64s = (sizeof(struct bch_sb_field_members) + | |
1672 | sizeof(struct bch_member) * nr_devices) / sizeof(u64); | |
1673 | ||
1674 | err = "no space in superblock for member info"; | |
1675 | ret = -ENOSPC; | |
1676 | ||
1677 | mi = bch2_sb_resize_members(&c->disk_sb, u64s); | |
1678 | if (!mi) | |
1679 | goto err_unlock; | |
1680 | ||
1681 | /* success: */ | |
1682 | ||
1683 | mi->members[dev_idx] = dev_mi; | |
a420eea6 | 1684 | mi->members[dev_idx].last_mount = cpu_to_le64(ktime_get_real_seconds()); |
1c6fdbd8 KO |
1685 | c->disk_sb.sb->nr_devices = nr_devices; |
1686 | ||
1687 | ca->disk_sb.sb->dev_idx = dev_idx; | |
1688 | bch2_dev_attach(c, ca, dev_idx); | |
1689 | ||
1690 | bch2_write_super(c); | |
1691 | mutex_unlock(&c->sb_lock); | |
1692 | ||
180fb49d KO |
1693 | bch2_dev_usage_journal_reserve(c); |
1694 | ||
114eea75 KO |
1695 | /* |
1696 | * Clear marks before marking transactionally in the btree, so that | |
1697 | * per-device accounting gets done correctly: | |
1698 | */ | |
1699 | down_read(&ca->bucket_lock); | |
1700 | buckets = bucket_array(ca); | |
1701 | for_each_bucket(g, buckets) | |
1702 | atomic64_set(&g->_mark.v, 0); | |
1703 | up_read(&ca->bucket_lock); | |
1704 | ||
bfcf840d | 1705 | err = "error marking superblock"; |
d62ab355 | 1706 | ret = bch2_trans_mark_dev_sb(c, ca); |
8d6b6222 | 1707 | if (ret) |
bfcf840d | 1708 | goto err_late; |
8d6b6222 | 1709 | |
2436cb9f | 1710 | if (ca->mi.state == BCH_MEMBER_STATE_rw) { |
1c6fdbd8 KO |
1711 | err = __bch2_dev_read_write(c, ca); |
1712 | if (err) | |
1713 | goto err_late; | |
1714 | } | |
1715 | ||
1ada1606 | 1716 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1717 | return 0; |
1718 | ||
1719 | err_unlock: | |
1720 | mutex_unlock(&c->sb_lock); | |
1ada1606 | 1721 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1722 | err: |
1723 | if (ca) | |
1724 | bch2_dev_free(ca); | |
1725 | bch2_free_super(&sb); | |
1726 | bch_err(c, "Unable to add device: %s", err); | |
1727 | return ret; | |
1728 | err_late: | |
bfcf840d | 1729 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1730 | bch_err(c, "Error going rw after adding device: %s", err); |
1731 | return -EINVAL; | |
1732 | } | |
1733 | ||
1734 | /* Hot add existing device to running filesystem: */ | |
1735 | int bch2_dev_online(struct bch_fs *c, const char *path) | |
1736 | { | |
1737 | struct bch_opts opts = bch2_opts_empty(); | |
1738 | struct bch_sb_handle sb = { NULL }; | |
1739 | struct bch_sb_field_members *mi; | |
1740 | struct bch_dev *ca; | |
1741 | unsigned dev_idx; | |
1742 | const char *err; | |
1743 | int ret; | |
1744 | ||
1ada1606 | 1745 | down_write(&c->state_lock); |
1c6fdbd8 KO |
1746 | |
1747 | ret = bch2_read_super(path, &opts, &sb); | |
1748 | if (ret) { | |
1ada1606 | 1749 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1750 | return ret; |
1751 | } | |
1752 | ||
1753 | dev_idx = sb.sb->dev_idx; | |
1754 | ||
1755 | err = bch2_dev_in_fs(c->disk_sb.sb, sb.sb); | |
1756 | if (err) | |
1757 | goto err; | |
1758 | ||
1759 | if (bch2_dev_attach_bdev(c, &sb)) { | |
1760 | err = "bch2_dev_attach_bdev() error"; | |
1761 | goto err; | |
1762 | } | |
1763 | ||
1764 | ca = bch_dev_locked(c, dev_idx); | |
bfcf840d | 1765 | |
d62ab355 | 1766 | if (bch2_trans_mark_dev_sb(c, ca)) { |
bfcf840d KO |
1767 | err = "bch2_trans_mark_dev_sb() error"; |
1768 | goto err; | |
1769 | } | |
1770 | ||
2436cb9f | 1771 | if (ca->mi.state == BCH_MEMBER_STATE_rw) { |
1c6fdbd8 KO |
1772 | err = __bch2_dev_read_write(c, ca); |
1773 | if (err) | |
1774 | goto err; | |
1775 | } | |
1776 | ||
1777 | mutex_lock(&c->sb_lock); | |
1778 | mi = bch2_sb_get_members(c->disk_sb.sb); | |
1779 | ||
1780 | mi->members[ca->dev_idx].last_mount = | |
a420eea6 | 1781 | cpu_to_le64(ktime_get_real_seconds()); |
1c6fdbd8 KO |
1782 | |
1783 | bch2_write_super(c); | |
1784 | mutex_unlock(&c->sb_lock); | |
1785 | ||
1ada1606 | 1786 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1787 | return 0; |
1788 | err: | |
1ada1606 | 1789 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1790 | bch2_free_super(&sb); |
1791 | bch_err(c, "error bringing %s online: %s", path, err); | |
1792 | return -EINVAL; | |
1793 | } | |
1794 | ||
1795 | int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags) | |
1796 | { | |
1ada1606 | 1797 | down_write(&c->state_lock); |
1c6fdbd8 KO |
1798 | |
1799 | if (!bch2_dev_is_online(ca)) { | |
1800 | bch_err(ca, "Already offline"); | |
1ada1606 | 1801 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1802 | return 0; |
1803 | } | |
1804 | ||
2436cb9f | 1805 | if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { |
1c6fdbd8 | 1806 | bch_err(ca, "Cannot offline required disk"); |
1ada1606 | 1807 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1808 | return -EINVAL; |
1809 | } | |
1810 | ||
1811 | __bch2_dev_offline(c, ca); | |
1812 | ||
1ada1606 | 1813 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1814 | return 0; |
1815 | } | |
1816 | ||
1817 | int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets) | |
1818 | { | |
1819 | struct bch_member *mi; | |
1820 | int ret = 0; | |
1821 | ||
1ada1606 | 1822 | down_write(&c->state_lock); |
1c6fdbd8 KO |
1823 | |
1824 | if (nbuckets < ca->mi.nbuckets) { | |
1825 | bch_err(ca, "Cannot shrink yet"); | |
1826 | ret = -EINVAL; | |
1827 | goto err; | |
1828 | } | |
1829 | ||
1830 | if (bch2_dev_is_online(ca) && | |
1831 | get_capacity(ca->disk_sb.bdev->bd_disk) < | |
1832 | ca->mi.bucket_size * nbuckets) { | |
1833 | bch_err(ca, "New size larger than device"); | |
1834 | ret = -EINVAL; | |
1835 | goto err; | |
1836 | } | |
1837 | ||
1838 | ret = bch2_dev_buckets_resize(c, ca, nbuckets); | |
1839 | if (ret) { | |
1840 | bch_err(ca, "Resize error: %i", ret); | |
1841 | goto err; | |
1842 | } | |
1843 | ||
224ec3e6 KO |
1844 | ret = bch2_trans_mark_dev_sb(c, ca); |
1845 | if (ret) { | |
1846 | goto err; | |
1847 | } | |
1848 | ||
1c6fdbd8 KO |
1849 | mutex_lock(&c->sb_lock); |
1850 | mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx]; | |
1851 | mi->nbuckets = cpu_to_le64(nbuckets); | |
1852 | ||
1853 | bch2_write_super(c); | |
1854 | mutex_unlock(&c->sb_lock); | |
1855 | ||
1856 | bch2_recalc_capacity(c); | |
1857 | err: | |
1ada1606 | 1858 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1859 | return ret; |
1860 | } | |
1861 | ||
1862 | /* return with ref on ca->ref: */ | |
1863 | struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *path) | |
1864 | { | |
1c6fdbd8 KO |
1865 | struct bch_dev *ca; |
1866 | dev_t dev; | |
1867 | unsigned i; | |
1868 | int ret; | |
1869 | ||
1870 | ret = lookup_bdev(path, &dev); | |
1871 | if (ret) | |
1872 | return ERR_PTR(ret); | |
1873 | ||
3a402c8d KO |
1874 | rcu_read_lock(); |
1875 | for_each_member_device_rcu(ca, c, i, NULL) | |
1c6fdbd8 KO |
1876 | if (ca->disk_sb.bdev->bd_dev == dev) |
1877 | goto found; | |
1c6fdbd8 KO |
1878 | ca = ERR_PTR(-ENOENT); |
1879 | found: | |
3a402c8d KO |
1880 | rcu_read_unlock(); |
1881 | ||
1c6fdbd8 KO |
1882 | return ca; |
1883 | } | |
1884 | ||
1885 | /* Filesystem open: */ | |
1886 | ||
1887 | struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices, | |
1888 | struct bch_opts opts) | |
1889 | { | |
1890 | struct bch_sb_handle *sb = NULL; | |
1891 | struct bch_fs *c = NULL; | |
625104ea | 1892 | struct bch_sb_field_members *mi; |
1c6fdbd8 KO |
1893 | unsigned i, best_sb = 0; |
1894 | const char *err; | |
1895 | int ret = -ENOMEM; | |
1896 | ||
1897 | pr_verbose_init(opts, ""); | |
1898 | ||
1899 | if (!nr_devices) { | |
1900 | c = ERR_PTR(-EINVAL); | |
1901 | goto out2; | |
1902 | } | |
1903 | ||
1904 | if (!try_module_get(THIS_MODULE)) { | |
1905 | c = ERR_PTR(-ENODEV); | |
1906 | goto out2; | |
1907 | } | |
1908 | ||
1909 | sb = kcalloc(nr_devices, sizeof(*sb), GFP_KERNEL); | |
1910 | if (!sb) | |
1911 | goto err; | |
1912 | ||
1913 | for (i = 0; i < nr_devices; i++) { | |
1914 | ret = bch2_read_super(devices[i], &opts, &sb[i]); | |
1915 | if (ret) | |
1916 | goto err; | |
1917 | ||
1918 | err = bch2_sb_validate(&sb[i]); | |
1919 | if (err) | |
1920 | goto err_print; | |
1921 | } | |
1922 | ||
1923 | for (i = 1; i < nr_devices; i++) | |
1924 | if (le64_to_cpu(sb[i].sb->seq) > | |
1925 | le64_to_cpu(sb[best_sb].sb->seq)) | |
1926 | best_sb = i; | |
1927 | ||
625104ea KO |
1928 | mi = bch2_sb_get_members(sb[best_sb].sb); |
1929 | ||
1930 | i = 0; | |
1931 | while (i < nr_devices) { | |
1932 | if (i != best_sb && | |
1933 | !bch2_dev_exists(sb[best_sb].sb, mi, sb[i].sb->dev_idx)) { | |
1934 | pr_info("%pg has been removed, skipping", sb[i].bdev); | |
1935 | bch2_free_super(&sb[i]); | |
1936 | array_remove_item(sb, nr_devices, i); | |
1937 | continue; | |
1938 | } | |
1939 | ||
1c6fdbd8 KO |
1940 | err = bch2_dev_in_fs(sb[best_sb].sb, sb[i].sb); |
1941 | if (err) | |
1942 | goto err_print; | |
625104ea | 1943 | i++; |
1c6fdbd8 KO |
1944 | } |
1945 | ||
1946 | ret = -ENOMEM; | |
1947 | c = bch2_fs_alloc(sb[best_sb].sb, opts); | |
1948 | if (!c) | |
1949 | goto err; | |
1950 | ||
1951 | err = "bch2_dev_online() error"; | |
1ada1606 | 1952 | down_write(&c->state_lock); |
1c6fdbd8 KO |
1953 | for (i = 0; i < nr_devices; i++) |
1954 | if (bch2_dev_attach_bdev(c, &sb[i])) { | |
1ada1606 | 1955 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1956 | goto err_print; |
1957 | } | |
1ada1606 | 1958 | up_write(&c->state_lock); |
1c6fdbd8 KO |
1959 | |
1960 | err = "insufficient devices"; | |
1961 | if (!bch2_fs_may_start(c)) | |
1962 | goto err_print; | |
1963 | ||
1964 | if (!c->opts.nostart) { | |
619f5bee KO |
1965 | ret = bch2_fs_start(c); |
1966 | if (ret) | |
1967 | goto err; | |
1c6fdbd8 KO |
1968 | } |
1969 | out: | |
1970 | kfree(sb); | |
1971 | module_put(THIS_MODULE); | |
1972 | out2: | |
1973 | pr_verbose_init(opts, "ret %i", PTR_ERR_OR_ZERO(c)); | |
1974 | return c; | |
1975 | err_print: | |
1976 | pr_err("bch_fs_open err opening %s: %s", | |
1977 | devices[0], err); | |
1978 | ret = -EINVAL; | |
1979 | err: | |
1980 | if (c) | |
1981 | bch2_fs_stop(c); | |
1982 | for (i = 0; i < nr_devices; i++) | |
1983 | bch2_free_super(&sb[i]); | |
1984 | c = ERR_PTR(ret); | |
1985 | goto out; | |
1986 | } | |
1987 | ||
1988 | static const char *__bch2_fs_open_incremental(struct bch_sb_handle *sb, | |
1989 | struct bch_opts opts) | |
1990 | { | |
1991 | const char *err; | |
1992 | struct bch_fs *c; | |
1993 | bool allocated_fs = false; | |
619f5bee | 1994 | int ret; |
1c6fdbd8 KO |
1995 | |
1996 | err = bch2_sb_validate(sb); | |
1997 | if (err) | |
1998 | return err; | |
1999 | ||
2000 | mutex_lock(&bch_fs_list_lock); | |
2001 | c = __bch2_uuid_to_fs(sb->sb->uuid); | |
2002 | if (c) { | |
2003 | closure_get(&c->cl); | |
2004 | ||
2005 | err = bch2_dev_in_fs(c->disk_sb.sb, sb->sb); | |
2006 | if (err) | |
2007 | goto err; | |
2008 | } else { | |
2009 | c = bch2_fs_alloc(sb->sb, opts); | |
2010 | err = "cannot allocate memory"; | |
2011 | if (!c) | |
2012 | goto err; | |
2013 | ||
2014 | allocated_fs = true; | |
2015 | } | |
2016 | ||
2017 | err = "bch2_dev_online() error"; | |
2018 | ||
2019 | mutex_lock(&c->sb_lock); | |
2020 | if (bch2_dev_attach_bdev(c, sb)) { | |
2021 | mutex_unlock(&c->sb_lock); | |
2022 | goto err; | |
2023 | } | |
2024 | mutex_unlock(&c->sb_lock); | |
2025 | ||
2026 | if (!c->opts.nostart && bch2_fs_may_start(c)) { | |
619f5bee KO |
2027 | err = "error starting filesystem"; |
2028 | ret = bch2_fs_start(c); | |
2029 | if (ret) | |
1c6fdbd8 KO |
2030 | goto err; |
2031 | } | |
2032 | ||
2033 | closure_put(&c->cl); | |
2034 | mutex_unlock(&bch_fs_list_lock); | |
2035 | ||
2036 | return NULL; | |
2037 | err: | |
2038 | mutex_unlock(&bch_fs_list_lock); | |
2039 | ||
2040 | if (allocated_fs) | |
2041 | bch2_fs_stop(c); | |
2042 | else if (c) | |
2043 | closure_put(&c->cl); | |
2044 | ||
2045 | return err; | |
2046 | } | |
2047 | ||
2048 | const char *bch2_fs_open_incremental(const char *path) | |
2049 | { | |
2050 | struct bch_sb_handle sb; | |
2051 | struct bch_opts opts = bch2_opts_empty(); | |
2052 | const char *err; | |
2053 | ||
2054 | if (bch2_read_super(path, &opts, &sb)) | |
2055 | return "error reading superblock"; | |
2056 | ||
2057 | err = __bch2_fs_open_incremental(&sb, opts); | |
2058 | bch2_free_super(&sb); | |
2059 | ||
2060 | return err; | |
2061 | } | |
2062 | ||
2063 | /* Global interfaces/init */ | |
2064 | ||
2065 | static void bcachefs_exit(void) | |
2066 | { | |
2067 | bch2_debug_exit(); | |
2068 | bch2_vfs_exit(); | |
2069 | bch2_chardev_exit(); | |
14ba3706 | 2070 | bch2_btree_key_cache_exit(); |
1c6fdbd8 KO |
2071 | if (bcachefs_kset) |
2072 | kset_unregister(bcachefs_kset); | |
2073 | } | |
2074 | ||
2075 | static int __init bcachefs_init(void) | |
2076 | { | |
2077 | bch2_bkey_pack_test(); | |
1c6fdbd8 KO |
2078 | |
2079 | if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) || | |
14ba3706 | 2080 | bch2_btree_key_cache_init() || |
1c6fdbd8 KO |
2081 | bch2_chardev_init() || |
2082 | bch2_vfs_init() || | |
2083 | bch2_debug_init()) | |
2084 | goto err; | |
2085 | ||
2086 | return 0; | |
2087 | err: | |
2088 | bcachefs_exit(); | |
2089 | return -ENOMEM; | |
2090 | } | |
2091 | ||
2092 | #define BCH_DEBUG_PARAM(name, description) \ | |
2093 | bool bch2_##name; \ | |
2094 | module_param_named(name, bch2_##name, bool, 0644); \ | |
2095 | MODULE_PARM_DESC(name, description); | |
2096 | BCH_DEBUG_PARAMS() | |
2097 | #undef BCH_DEBUG_PARAM | |
2098 | ||
26609b61 | 2099 | unsigned bch2_metadata_version = bcachefs_metadata_version_current; |
1c6fdbd8 KO |
2100 | module_param_named(version, bch2_metadata_version, uint, 0400); |
2101 | ||
2102 | module_exit(bcachefs_exit); | |
2103 | module_init(bcachefs_init); |