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