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