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