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