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