2 * bcache setup/teardown code, and some metadata io - read a superblock and
3 * figure out what to do with it.
5 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6 * Copyright 2012 Google, Inc.
12 #include "btree_cache.h"
14 #include "btree_update.h"
36 #include "writeback.h"
38 #include <linux/backing-dev.h>
39 #include <linux/blkdev.h>
40 #include <linux/debugfs.h>
41 #include <linux/device.h>
42 #include <linux/genhd.h>
43 #include <linux/idr.h>
44 #include <linux/kthread.h>
45 #include <linux/module.h>
46 #include <linux/percpu.h>
47 #include <linux/random.h>
48 #include <linux/reboot.h>
49 #include <linux/sysfs.h>
50 #include <crypto/hash.h>
52 #include <trace/events/bcache.h>
54 MODULE_LICENSE("GPL");
55 MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
57 static const uuid_le invalid_uuid = {
59 0xa0, 0x3e, 0xf8, 0xed, 0x3e, 0xe1, 0xb8, 0x78,
60 0xc8, 0x50, 0xfc, 0x5e, 0xcb, 0x16, 0xcd, 0x99
64 static struct kset *bcache_kset;
65 static LIST_HEAD(bch_fs_list);
66 static DEFINE_MUTEX(bch_fs_list_lock);
68 static DECLARE_WAIT_QUEUE_HEAD(bch_read_only_wait);
69 struct workqueue_struct *bcache_io_wq;
70 struct crypto_shash *bch_sha256;
72 static void bch_dev_free(struct bch_dev *);
73 static int bch_dev_alloc(struct bch_fs *, unsigned);
74 static int bch_dev_sysfs_online(struct bch_dev *);
75 static void __bch_dev_read_only(struct bch_fs *, struct bch_dev *);
77 struct bch_fs *bch_bdev_to_fs(struct block_device *bdev)
83 mutex_lock(&bch_fs_list_lock);
86 list_for_each_entry(c, &bch_fs_list, list)
87 for_each_member_device_rcu(ca, c, i)
88 if (ca->disk_sb.bdev == bdev) {
95 mutex_unlock(&bch_fs_list_lock);
100 static struct bch_fs *__bch_uuid_to_fs(uuid_le uuid)
104 lockdep_assert_held(&bch_fs_list_lock);
106 list_for_each_entry(c, &bch_fs_list, list)
107 if (!memcmp(&c->disk_sb->uuid, &uuid, sizeof(uuid_le)))
113 struct bch_fs *bch_uuid_to_fs(uuid_le uuid)
117 mutex_lock(&bch_fs_list_lock);
118 c = __bch_uuid_to_fs(uuid);
121 mutex_unlock(&bch_fs_list_lock);
126 int bch_congested(struct bch_fs *c, int bdi_bits)
128 struct backing_dev_info *bdi;
133 if (bdi_bits & (1 << WB_sync_congested)) {
134 /* Reads - check all devices: */
135 for_each_readable_member(ca, c, i) {
136 bdi = blk_get_backing_dev_info(ca->disk_sb.bdev);
138 if (bdi_congested(bdi, bdi_bits)) {
144 /* Writes prefer fastest tier: */
145 struct bch_tier *tier = READ_ONCE(c->fastest_tier);
146 struct dev_group *grp = tier ? &tier->devs : &c->all_devs;
149 group_for_each_dev(ca, grp, i) {
150 bdi = blk_get_backing_dev_info(ca->disk_sb.bdev);
152 if (bdi_congested(bdi, bdi_bits)) {
163 static int bch_congested_fn(void *data, int bdi_bits)
165 struct bch_fs *c = data;
167 return bch_congested(c, bdi_bits);
170 /* Filesystem RO/RW: */
173 * For startup/shutdown of RW stuff, the dependencies are:
175 * - foreground writes depend on copygc and tiering (to free up space)
177 * - copygc and tiering depend on mark and sweep gc (they actually probably
178 * don't because they either reserve ahead of time or don't block if
179 * allocations fail, but allocations can require mark and sweep gc to run
180 * because of generation number wraparound)
182 * - all of the above depends on the allocator threads
184 * - allocator depends on the journal (when it rewrites prios and gens)
187 static void __bch_fs_read_only(struct bch_fs *c)
194 for_each_member_device(ca, c, i)
195 bch_moving_gc_stop(ca);
197 bch_gc_thread_stop(c);
201 for_each_member_device(ca, c, i)
202 bch_dev_allocator_stop(ca);
204 bch_fs_journal_stop(&c->journal);
207 static void bch_writes_disabled(struct percpu_ref *writes)
209 struct bch_fs *c = container_of(writes, struct bch_fs, writes);
211 set_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
212 wake_up(&bch_read_only_wait);
215 void bch_fs_read_only(struct bch_fs *c)
217 mutex_lock(&c->state_lock);
218 if (c->state != BCH_FS_STARTING &&
219 c->state != BCH_FS_RW)
222 if (test_bit(BCH_FS_ERROR, &c->flags))
225 trace_fs_read_only(c);
228 * Block new foreground-end write operations from starting - any new
229 * writes will return -EROFS:
231 * (This is really blocking new _allocations_, writes to previously
232 * allocated space can still happen until stopping the allocator in
233 * bch_dev_allocator_stop()).
235 percpu_ref_kill(&c->writes);
237 del_timer(&c->foreground_write_wakeup);
238 cancel_delayed_work(&c->pd_controllers_update);
240 c->foreground_write_pd.rate.rate = UINT_MAX;
241 bch_wake_delayed_writes((unsigned long) c);
244 * If we're not doing an emergency shutdown, we want to wait on
245 * outstanding writes to complete so they don't see spurious errors due
246 * to shutting down the allocator:
248 * If we are doing an emergency shutdown outstanding writes may
249 * hang until we shutdown the allocator so we don't want to wait
250 * on outstanding writes before shutting everything down - but
251 * we do need to wait on them before returning and signalling
252 * that going RO is complete:
254 wait_event(bch_read_only_wait,
255 test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags) ||
256 test_bit(BCH_FS_EMERGENCY_RO, &c->flags));
258 __bch_fs_read_only(c);
260 wait_event(bch_read_only_wait,
261 test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
263 clear_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
265 if (!bch_journal_error(&c->journal) &&
266 !test_bit(BCH_FS_ERROR, &c->flags)) {
267 mutex_lock(&c->sb_lock);
268 SET_BCH_SB_CLEAN(c->disk_sb, true);
270 mutex_unlock(&c->sb_lock);
273 c->state = BCH_FS_RO;
274 bch_notify_fs_read_only(c);
275 trace_fs_read_only_done(c);
277 mutex_unlock(&c->state_lock);
280 static void bch_fs_read_only_work(struct work_struct *work)
283 container_of(work, struct bch_fs, read_only_work);
288 static void bch_fs_read_only_async(struct bch_fs *c)
290 queue_work(system_long_wq, &c->read_only_work);
293 bool bch_fs_emergency_read_only(struct bch_fs *c)
295 bool ret = !test_and_set_bit(BCH_FS_EMERGENCY_RO, &c->flags);
297 bch_fs_read_only_async(c);
298 bch_journal_halt(&c->journal);
300 wake_up(&bch_read_only_wait);
304 const char *bch_fs_read_write(struct bch_fs *c)
307 const char *err = NULL;
310 mutex_lock(&c->state_lock);
311 if (c->state != BCH_FS_STARTING &&
312 c->state != BCH_FS_RO)
315 err = "error starting allocator thread";
316 for_each_rw_member(ca, c, i)
317 if (bch_dev_allocator_start(ca)) {
318 percpu_ref_put(&ca->io_ref);
322 err = "error starting btree GC thread";
323 if (bch_gc_thread_start(c))
326 err = "error starting moving GC thread";
327 for_each_rw_member(ca, c, i)
328 if (bch_moving_gc_start(ca)) {
329 percpu_ref_put(&ca->io_ref);
333 err = "error starting tiering thread";
334 if (bch_tiering_start(c))
337 schedule_delayed_work(&c->pd_controllers_update, 5 * HZ);
339 if (c->state != BCH_FS_STARTING)
340 percpu_ref_reinit(&c->writes);
342 c->state = BCH_FS_RW;
345 mutex_unlock(&c->state_lock);
348 __bch_fs_read_only(c);
352 /* Filesystem startup/shutdown: */
354 static void bch_fs_free(struct bch_fs *c)
356 bch_fs_encryption_exit(c);
357 bch_fs_btree_exit(c);
358 bch_fs_journal_exit(&c->journal);
359 bch_io_clock_exit(&c->io_clock[WRITE]);
360 bch_io_clock_exit(&c->io_clock[READ]);
361 bch_fs_compress_exit(c);
362 bch_fs_blockdev_exit(c);
363 bdi_destroy(&c->bdi);
364 lg_lock_free(&c->usage_lock);
365 free_percpu(c->usage_percpu);
366 mempool_exit(&c->btree_bounce_pool);
367 mempool_exit(&c->bio_bounce_pages);
368 bioset_exit(&c->bio_write);
369 bioset_exit(&c->bio_read_split);
370 bioset_exit(&c->bio_read);
371 bioset_exit(&c->btree_read_bio);
372 mempool_exit(&c->btree_interior_update_pool);
373 mempool_exit(&c->btree_reserve_pool);
374 mempool_exit(&c->fill_iter);
375 percpu_ref_exit(&c->writes);
378 destroy_workqueue(c->copygc_wq);
380 destroy_workqueue(c->wq);
382 free_pages((unsigned long) c->disk_sb, c->disk_sb_order);
384 module_put(THIS_MODULE);
387 static void bch_fs_exit(struct bch_fs *c)
391 del_timer_sync(&c->foreground_write_wakeup);
392 cancel_delayed_work_sync(&c->pd_controllers_update);
393 cancel_work_sync(&c->read_only_work);
394 cancel_work_sync(&c->bio_submit_work);
395 cancel_work_sync(&c->read_retry_work);
397 for (i = 0; i < c->sb.nr_devices; i++)
399 bch_dev_free(c->devs[i]);
401 closure_debug_destroy(&c->cl);
402 kobject_put(&c->kobj);
405 static void bch_fs_offline(struct bch_fs *c)
410 mutex_lock(&bch_fs_list_lock);
412 mutex_unlock(&bch_fs_list_lock);
414 for_each_member_device(ca, c, i)
415 if (ca->kobj.state_in_sysfs &&
417 sysfs_remove_link(&part_to_dev(ca->disk_sb.bdev->bd_part)->kobj,
420 if (c->kobj.state_in_sysfs)
421 kobject_del(&c->kobj);
423 bch_fs_debug_exit(c);
424 bch_fs_chardev_exit(c);
426 bch_cache_accounting_destroy(&c->accounting);
428 kobject_put(&c->time_stats);
429 kobject_put(&c->opts_dir);
430 kobject_put(&c->internal);
432 __bch_fs_read_only(c);
436 * should be __bch_fs_stop4 - block devices are closed, now we can finally
439 void bch_fs_release(struct kobject *kobj)
441 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
443 bch_notify_fs_stopped(c);
448 * All activity on the filesystem should have stopped now - close devices:
450 static void __bch_fs_stop3(struct closure *cl)
452 struct bch_fs *c = container_of(cl, struct bch_fs, cl);
458 * Openers (i.e. block devices) should have exited, shutdown all userspace
459 * interfaces and wait for &c->cl to hit 0
461 static void __bch_fs_stop2(struct closure *cl)
463 struct bch_fs *c = container_of(cl, struct bch_fs, caching);
471 * First phase of the shutdown process that's kicked off by bch_fs_stop_async();
472 * we haven't waited for anything to stop yet, we're just punting to process
473 * context to shut down block devices:
475 static void __bch_fs_stop1(struct closure *cl)
477 struct bch_fs *c = container_of(cl, struct bch_fs, caching);
479 bch_blockdevs_stop(c);
481 continue_at(cl, __bch_fs_stop2, system_wq);
484 void bch_fs_stop_async(struct bch_fs *c)
486 mutex_lock(&c->state_lock);
487 if (c->state != BCH_FS_STOPPING) {
488 c->state = BCH_FS_STOPPING;
489 closure_queue(&c->caching);
491 mutex_unlock(&c->state_lock);
494 void bch_fs_stop(struct bch_fs *c)
496 mutex_lock(&c->state_lock);
497 BUG_ON(c->state == BCH_FS_STOPPING);
498 c->state = BCH_FS_STOPPING;
499 mutex_unlock(&c->state_lock);
501 bch_blockdevs_stop(c);
503 closure_sync(&c->caching);
504 closure_debug_destroy(&c->caching);
509 closure_sync(&c->cl);
514 /* Stop, detaching from backing devices: */
515 void bch_fs_detach(struct bch_fs *c)
517 if (!test_and_set_bit(BCH_FS_DETACHING, &c->flags))
518 bch_fs_stop_async(c);
521 #define alloc_bucket_pages(gfp, ca) \
522 ((void *) __get_free_pages(__GFP_ZERO|gfp, ilog2(bucket_pages(ca))))
524 static struct bch_fs *bch_fs_alloc(struct bch_sb *sb, struct bch_opts opts)
526 struct bch_sb_field_members *mi;
528 unsigned i, iter_size, journal_entry_bytes;
530 c = kzalloc(sizeof(struct bch_fs), GFP_KERNEL);
534 __module_get(THIS_MODULE);
538 mutex_init(&c->state_lock);
539 mutex_init(&c->sb_lock);
540 INIT_RADIX_TREE(&c->devices, GFP_KERNEL);
541 mutex_init(&c->btree_cache_lock);
542 mutex_init(&c->bucket_lock);
543 mutex_init(&c->btree_root_lock);
544 INIT_WORK(&c->read_only_work, bch_fs_read_only_work);
546 init_rwsem(&c->gc_lock);
548 #define BCH_TIME_STAT(name, frequency_units, duration_units) \
549 spin_lock_init(&c->name##_time.lock);
553 bch_fs_allocator_init(c);
554 bch_fs_tiering_init(c);
556 INIT_LIST_HEAD(&c->list);
557 INIT_LIST_HEAD(&c->cached_devs);
558 INIT_LIST_HEAD(&c->btree_cache);
559 INIT_LIST_HEAD(&c->btree_cache_freeable);
560 INIT_LIST_HEAD(&c->btree_cache_freed);
562 INIT_LIST_HEAD(&c->btree_interior_update_list);
563 mutex_init(&c->btree_reserve_cache_lock);
564 mutex_init(&c->btree_interior_update_lock);
566 mutex_init(&c->bio_bounce_pages_lock);
567 INIT_WORK(&c->bio_submit_work, bch_bio_submit_work);
568 spin_lock_init(&c->bio_submit_lock);
569 bio_list_init(&c->read_retry_list);
570 spin_lock_init(&c->read_retry_lock);
571 INIT_WORK(&c->read_retry_work, bch_read_retry_work);
572 mutex_init(&c->zlib_workspace_lock);
574 seqcount_init(&c->gc_pos_lock);
576 c->prio_clock[READ].hand = 1;
577 c->prio_clock[READ].min_prio = 0;
578 c->prio_clock[WRITE].hand = 1;
579 c->prio_clock[WRITE].min_prio = 0;
581 c->congested_read_threshold_us = 2000;
582 c->congested_write_threshold_us = 20000;
583 c->error_limit = 16 << IO_ERROR_SHIFT;
584 init_waitqueue_head(&c->writeback_wait);
586 c->writeback_pages_max = (256 << 10) / PAGE_SIZE;
588 c->copy_gc_enabled = 1;
589 c->tiering_enabled = 1;
590 c->tiering_percent = 10;
592 c->foreground_target_percent = 20;
594 c->journal.write_time = &c->journal_write_time;
595 c->journal.delay_time = &c->journal_delay_time;
596 c->journal.blocked_time = &c->journal_blocked_time;
597 c->journal.flush_seq_time = &c->journal_flush_seq_time;
599 mutex_init(&c->uevent_lock);
601 mutex_lock(&c->sb_lock);
603 if (bch_sb_to_fs(c, sb)) {
604 mutex_unlock(&c->sb_lock);
608 mutex_unlock(&c->sb_lock);
610 scnprintf(c->name, sizeof(c->name), "%pU", &c->sb.user_uuid);
612 bch_opts_apply(&c->opts, bch_sb_opts(sb));
613 bch_opts_apply(&c->opts, opts);
615 c->opts.nochanges |= c->opts.noreplay;
616 c->opts.read_only |= c->opts.nochanges;
618 c->block_bits = ilog2(c->sb.block_size);
620 if (bch_fs_init_fault("fs_alloc"))
623 iter_size = (btree_blocks(c) + 1) * 2 *
624 sizeof(struct btree_node_iter_set);
626 journal_entry_bytes = 512U << BCH_SB_JOURNAL_ENTRY_SIZE(sb);
628 if (!(c->wq = alloc_workqueue("bcache",
629 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) ||
630 !(c->copygc_wq = alloc_workqueue("bcache_copygc",
631 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) ||
632 percpu_ref_init(&c->writes, bch_writes_disabled, 0, GFP_KERNEL) ||
633 mempool_init_kmalloc_pool(&c->btree_reserve_pool, 1,
634 sizeof(struct btree_reserve)) ||
635 mempool_init_kmalloc_pool(&c->btree_interior_update_pool, 1,
636 sizeof(struct btree_interior_update)) ||
637 mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) ||
638 bioset_init(&c->btree_read_bio, 1, 0) ||
639 bioset_init(&c->bio_read, 1, offsetof(struct bch_read_bio, bio)) ||
640 bioset_init(&c->bio_read_split, 1, offsetof(struct bch_read_bio, bio)) ||
641 bioset_init(&c->bio_write, 1, offsetof(struct bch_write_bio, bio)) ||
642 mempool_init_page_pool(&c->bio_bounce_pages,
644 c->sb.btree_node_size,
645 BCH_ENCODED_EXTENT_MAX) /
647 !(c->usage_percpu = alloc_percpu(struct bch_fs_usage)) ||
648 lg_lock_init(&c->usage_lock) ||
649 mempool_init_page_pool(&c->btree_bounce_pool, 1,
650 ilog2(btree_pages(c))) ||
651 bdi_setup_and_register(&c->bdi, "bcache") ||
652 bch_fs_blockdev_init(c) ||
653 bch_io_clock_init(&c->io_clock[READ]) ||
654 bch_io_clock_init(&c->io_clock[WRITE]) ||
655 bch_fs_journal_init(&c->journal, journal_entry_bytes) ||
656 bch_fs_btree_init(c) ||
657 bch_fs_encryption_init(c) ||
658 bch_fs_compress_init(c) ||
659 bch_check_set_has_compressed_data(c, c->opts.compression))
662 c->bdi.ra_pages = VM_MAX_READAHEAD * 1024 / PAGE_SIZE;
663 c->bdi.congested_fn = bch_congested_fn;
664 c->bdi.congested_data = c;
666 mi = bch_sb_get_members(c->disk_sb);
667 for (i = 0; i < c->sb.nr_devices; i++)
668 if (!bch_is_zero(mi->members[i].uuid.b, sizeof(uuid_le)) &&
673 * Now that all allocations have succeeded, init various refcounty
674 * things that let us shutdown:
676 closure_init(&c->cl, NULL);
678 c->kobj.kset = bcache_kset;
679 kobject_init(&c->kobj, &bch_fs_ktype);
680 kobject_init(&c->internal, &bch_fs_internal_ktype);
681 kobject_init(&c->opts_dir, &bch_fs_opts_dir_ktype);
682 kobject_init(&c->time_stats, &bch_fs_time_stats_ktype);
684 bch_cache_accounting_init(&c->accounting, &c->cl);
686 closure_init(&c->caching, &c->cl);
687 set_closure_fn(&c->caching, __bch_fs_stop1, system_wq);
690 continue_at_noreturn(&c->cl, __bch_fs_stop3, system_wq);
697 static const char *__bch_fs_online(struct bch_fs *c)
700 const char *err = NULL;
704 lockdep_assert_held(&bch_fs_list_lock);
706 if (!list_empty(&c->list))
709 if (__bch_uuid_to_fs(c->sb.uuid))
710 return "filesystem UUID already open";
712 ret = bch_fs_chardev_init(c);
714 return "error creating character device";
716 bch_fs_debug_init(c);
718 if (kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ||
719 kobject_add(&c->internal, &c->kobj, "internal") ||
720 kobject_add(&c->opts_dir, &c->kobj, "options") ||
721 kobject_add(&c->time_stats, &c->kobj, "time_stats") ||
722 bch_cache_accounting_add_kobjs(&c->accounting, &c->kobj))
723 return "error creating sysfs objects";
725 mutex_lock(&c->state_lock);
727 err = "error creating sysfs objects";
728 __for_each_member_device(ca, c, i)
729 if (bch_dev_sysfs_online(ca))
732 err = "can't bring up blockdev volumes";
733 if (bch_blockdev_volumes_start(c))
736 bch_attach_backing_devs(c);
738 list_add(&c->list, &bch_fs_list);
741 mutex_unlock(&c->state_lock);
745 static const char *bch_fs_online(struct bch_fs *c)
749 mutex_lock(&bch_fs_list_lock);
750 err = __bch_fs_online(c);
751 mutex_unlock(&bch_fs_list_lock);
756 static const char *__bch_fs_start(struct bch_fs *c)
758 const char *err = "cannot allocate memory";
759 struct bch_sb_field_members *mi;
767 BUG_ON(c->state != BCH_FS_STARTING);
769 mutex_lock(&c->sb_lock);
770 for_each_online_member(ca, c, i)
771 bch_sb_from_fs(c, ca);
772 mutex_unlock(&c->sb_lock);
774 if (BCH_SB_INITIALIZED(c->disk_sb)) {
775 ret = bch_journal_read(c, &journal);
779 j = &list_entry(journal.prev, struct journal_replay, list)->j;
781 c->prio_clock[READ].hand = le16_to_cpu(j->read_clock);
782 c->prio_clock[WRITE].hand = le16_to_cpu(j->write_clock);
784 err = "error reading priorities";
785 for_each_readable_member(ca, c, i) {
786 ret = bch_prio_read(ca);
788 percpu_ref_put(&ca->io_ref);
793 for (id = 0; id < BTREE_ID_NR; id++) {
797 err = "bad btree root";
798 k = bch_journal_find_btree_root(c, j, id, &level);
799 if (!k && id == BTREE_ID_EXTENTS)
802 pr_debug("missing btree root: %d", id);
806 err = "error reading btree root";
807 if (bch_btree_root_read(c, id, k, level))
811 bch_verbose(c, "starting mark and sweep:");
813 err = "error in recovery";
814 if (bch_initial_gc(c, &journal))
817 if (c->opts.noreplay)
820 bch_verbose(c, "mark and sweep done");
823 * bch_journal_start() can't happen sooner, or btree_gc_finish()
824 * will give spurious errors about oldest_gen > bucket_gen -
825 * this is a hack but oh well.
827 bch_journal_start(c);
829 err = "error starting allocator thread";
830 for_each_rw_member(ca, c, i)
831 if (bch_dev_allocator_start(ca)) {
832 percpu_ref_put(&ca->io_ref);
836 bch_verbose(c, "starting journal replay:");
838 err = "journal replay failed";
839 ret = bch_journal_replay(c, &journal);
843 bch_verbose(c, "journal replay done");
845 if (c->opts.norecovery)
848 bch_verbose(c, "starting fsck:");
849 err = "error in fsck";
850 ret = bch_fsck(c, !c->opts.nofsck);
854 bch_verbose(c, "fsck done");
856 struct bch_inode_unpacked inode;
857 struct bkey_inode_buf packed_inode;
860 closure_init_stack(&cl);
862 bch_notice(c, "initializing new filesystem");
864 bch_initial_gc(c, NULL);
866 err = "unable to allocate journal buckets";
867 for_each_rw_member(ca, c, i)
868 if (bch_dev_journal_alloc(ca)) {
869 percpu_ref_put(&ca->io_ref);
874 * journal_res_get() will crash if called before this has
875 * set up the journal.pin FIFO and journal.cur pointer:
877 bch_journal_start(c);
878 bch_journal_set_replay_done(&c->journal);
880 err = "error starting allocator thread";
881 for_each_rw_member(ca, c, i)
882 if (bch_dev_allocator_start(ca)) {
883 percpu_ref_put(&ca->io_ref);
887 err = "cannot allocate new btree root";
888 for (id = 0; id < BTREE_ID_NR; id++)
889 if (bch_btree_root_alloc(c, id, &cl)) {
894 /* Wait for new btree roots to be written: */
897 bch_inode_init(c, &inode, 0, 0,
898 S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO, 0);
899 inode.inum = BCACHE_ROOT_INO;
901 bch_inode_pack(&packed_inode, &inode);
903 err = "error creating root directory";
904 if (bch_btree_insert(c, BTREE_ID_INODES,
905 &packed_inode.inode.k_i,
906 NULL, NULL, NULL, 0))
909 err = "error writing first journal entry";
910 if (bch_journal_meta(&c->journal))
914 err = "dynamic fault";
915 if (bch_fs_init_fault("fs_start"))
918 if (c->opts.read_only) {
921 err = bch_fs_read_write(c);
926 mutex_lock(&c->sb_lock);
927 mi = bch_sb_get_members(c->disk_sb);
928 now = ktime_get_seconds();
930 for_each_member_device(ca, c, i)
931 mi->members[ca->dev_idx].last_mount = cpu_to_le64(now);
933 SET_BCH_SB_INITIALIZED(c->disk_sb, true);
934 SET_BCH_SB_CLEAN(c->disk_sb, false);
935 c->disk_sb->version = BCACHE_SB_VERSION_CDEV;
938 mutex_unlock(&c->sb_lock);
942 bch_journal_entries_free(&journal);
946 case BCH_FSCK_ERRORS_NOT_FIXED:
947 bch_err(c, "filesystem contains errors: please report this to the developers");
948 pr_cont("mount with -o fix_errors to repair");
951 case BCH_FSCK_REPAIR_UNIMPLEMENTED:
952 bch_err(c, "filesystem contains errors: please report this to the developers");
953 pr_cont("repair unimplemented: inform the developers so that it can be added");
956 case BCH_FSCK_REPAIR_IMPOSSIBLE:
957 bch_err(c, "filesystem contains errors, but repair impossible");
960 case BCH_FSCK_UNKNOWN_VERSION:
961 err = "unknown metadata version";;
964 err = "cannot allocate memory";
972 set_bit(BCH_FS_ERROR, &c->flags);
976 const char *bch_fs_start(struct bch_fs *c)
978 return __bch_fs_start(c) ?: bch_fs_online(c);
981 static const char *bch_dev_may_add(struct bch_sb *sb, struct bch_fs *c)
983 struct bch_sb_field_members *sb_mi;
985 sb_mi = bch_sb_get_members(sb);
987 return "Invalid superblock: member info area missing";
989 if (le16_to_cpu(sb->block_size) != c->sb.block_size)
990 return "mismatched block size";
992 if (le16_to_cpu(sb_mi->members[sb->dev_idx].bucket_size) <
993 BCH_SB_BTREE_NODE_SIZE(c->disk_sb))
994 return "new cache bucket size is too small";
999 static const char *bch_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb)
1001 struct bch_sb *newest =
1002 le64_to_cpu(fs->seq) > le64_to_cpu(sb->seq) ? fs : sb;
1003 struct bch_sb_field_members *mi = bch_sb_get_members(newest);
1005 if (uuid_le_cmp(fs->uuid, sb->uuid))
1006 return "device not a member of filesystem";
1008 if (sb->dev_idx >= newest->nr_devices)
1009 return "device has invalid dev_idx";
1011 if (bch_is_zero(mi->members[sb->dev_idx].uuid.b, sizeof(uuid_le)))
1012 return "device has been removed";
1014 if (fs->block_size != sb->block_size)
1015 return "mismatched block size";
1020 /* Device startup/shutdown: */
1022 void bch_dev_release(struct kobject *kobj)
1024 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
1029 static void bch_dev_free(struct bch_dev *ca)
1033 cancel_work_sync(&ca->io_error_work);
1035 if (ca->kobj.state_in_sysfs &&
1037 sysfs_remove_link(&part_to_dev(ca->disk_sb.bdev->bd_part)->kobj,
1040 if (ca->kobj.state_in_sysfs)
1041 kobject_del(&ca->kobj);
1043 bch_free_super(&ca->disk_sb);
1044 bch_dev_journal_exit(ca);
1046 free_percpu(ca->sectors_written);
1047 bioset_exit(&ca->replica_set);
1048 free_percpu(ca->usage_percpu);
1049 free_pages((unsigned long) ca->disk_buckets, ilog2(bucket_pages(ca)));
1050 kfree(ca->prio_buckets);
1051 kfree(ca->bio_prio);
1053 vfree(ca->oldest_gens);
1054 free_heap(&ca->heap);
1055 free_fifo(&ca->free_inc);
1057 for (i = 0; i < RESERVE_NR; i++)
1058 free_fifo(&ca->free[i]);
1060 percpu_ref_exit(&ca->io_ref);
1061 percpu_ref_exit(&ca->ref);
1062 kobject_put(&ca->kobj);
1065 static void bch_dev_io_ref_release(struct percpu_ref *ref)
1067 struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref);
1069 complete(&ca->offline_complete);
1072 static void __bch_dev_offline(struct bch_dev *ca)
1074 struct bch_fs *c = ca->fs;
1076 lockdep_assert_held(&c->state_lock);
1078 __bch_dev_read_only(ca->fs, ca);
1080 reinit_completion(&ca->offline_complete);
1081 percpu_ref_kill(&ca->io_ref);
1082 wait_for_completion(&ca->offline_complete);
1084 if (ca->kobj.state_in_sysfs) {
1085 struct kobject *block =
1086 &part_to_dev(ca->disk_sb.bdev->bd_part)->kobj;
1088 sysfs_remove_link(block, "bcache");
1089 sysfs_remove_link(&ca->kobj, "block");
1092 bch_free_super(&ca->disk_sb);
1093 bch_dev_journal_exit(ca);
1096 static void bch_dev_ref_release(struct percpu_ref *ref)
1098 struct bch_dev *ca = container_of(ref, struct bch_dev, ref);
1100 complete(&ca->stop_complete);
1103 static void bch_dev_stop(struct bch_dev *ca)
1105 struct bch_fs *c = ca->fs;
1107 lockdep_assert_held(&c->state_lock);
1109 BUG_ON(rcu_access_pointer(c->devs[ca->dev_idx]) != ca);
1110 rcu_assign_pointer(c->devs[ca->dev_idx], NULL);
1114 reinit_completion(&ca->stop_complete);
1115 percpu_ref_kill(&ca->ref);
1116 wait_for_completion(&ca->stop_complete);
1119 static int bch_dev_sysfs_online(struct bch_dev *ca)
1121 struct bch_fs *c = ca->fs;
1124 if (!c->kobj.state_in_sysfs)
1127 if (!ca->kobj.state_in_sysfs) {
1128 ret = kobject_add(&ca->kobj, &ca->fs->kobj,
1129 "dev-%u", ca->dev_idx);
1134 if (ca->disk_sb.bdev) {
1135 struct kobject *block =
1136 &part_to_dev(ca->disk_sb.bdev->bd_part)->kobj;
1138 ret = sysfs_create_link(block, &ca->kobj, "bcache");
1141 ret = sysfs_create_link(&ca->kobj, block, "block");
1149 static int bch_dev_alloc(struct bch_fs *c, unsigned dev_idx)
1151 struct bch_member *member;
1152 size_t reserve_none, movinggc_reserve, free_inc_reserve, total_reserve;
1157 if (bch_fs_init_fault("dev_alloc"))
1160 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
1164 kobject_init(&ca->kobj, &bch_dev_ktype);
1165 init_completion(&ca->stop_complete);
1166 init_completion(&ca->offline_complete);
1168 spin_lock_init(&ca->self.lock);
1170 rcu_assign_pointer(ca->self.d[0].dev, ca);
1171 ca->dev_idx = dev_idx;
1173 spin_lock_init(&ca->freelist_lock);
1174 spin_lock_init(&ca->prio_buckets_lock);
1175 mutex_init(&ca->heap_lock);
1176 bch_dev_moving_gc_init(ca);
1178 INIT_WORK(&ca->io_error_work, bch_nonfatal_io_error_work);
1180 if (bch_fs_init_fault("dev_alloc"))
1183 member = bch_sb_get_members(c->disk_sb)->members + dev_idx;
1185 ca->mi = bch_mi_to_cpu(member);
1186 ca->uuid = member->uuid;
1187 ca->bucket_bits = ilog2(ca->mi.bucket_size);
1188 scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx);
1190 /* XXX: tune these */
1191 movinggc_reserve = max_t(size_t, 16, ca->mi.nbuckets >> 7);
1192 reserve_none = max_t(size_t, 4, ca->mi.nbuckets >> 9);
1194 * free_inc must be smaller than the copygc reserve: if it was bigger,
1195 * one copygc iteration might not make enough buckets available to fill
1196 * up free_inc and allow the allocator to make forward progress
1198 free_inc_reserve = movinggc_reserve / 2;
1199 heap_size = movinggc_reserve * 8;
1201 if (percpu_ref_init(&ca->ref, bch_dev_ref_release,
1203 percpu_ref_init(&ca->io_ref, bch_dev_io_ref_release,
1204 PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
1205 !init_fifo(&ca->free[RESERVE_PRIO], prio_buckets(ca), GFP_KERNEL) ||
1206 !init_fifo(&ca->free[RESERVE_BTREE], BTREE_NODE_RESERVE, GFP_KERNEL) ||
1207 !init_fifo(&ca->free[RESERVE_MOVINGGC],
1208 movinggc_reserve, GFP_KERNEL) ||
1209 !init_fifo(&ca->free[RESERVE_NONE], reserve_none, GFP_KERNEL) ||
1210 !init_fifo(&ca->free_inc, free_inc_reserve, GFP_KERNEL) ||
1211 !init_heap(&ca->heap, heap_size, GFP_KERNEL) ||
1212 !(ca->oldest_gens = vzalloc(sizeof(u8) *
1213 ca->mi.nbuckets)) ||
1214 !(ca->buckets = vzalloc(sizeof(struct bucket) *
1215 ca->mi.nbuckets)) ||
1216 !(ca->prio_buckets = kzalloc(sizeof(u64) * prio_buckets(ca) *
1218 !(ca->disk_buckets = alloc_bucket_pages(GFP_KERNEL, ca)) ||
1219 !(ca->usage_percpu = alloc_percpu(struct bch_dev_usage)) ||
1220 !(ca->bio_prio = bio_kmalloc(GFP_NOIO, bucket_pages(ca))) ||
1221 bioset_init(&ca->replica_set, 4,
1222 offsetof(struct bch_write_bio, bio)) ||
1223 !(ca->sectors_written = alloc_percpu(*ca->sectors_written)))
1226 ca->prio_last_buckets = ca->prio_buckets + prio_buckets(ca);
1228 total_reserve = ca->free_inc.size;
1229 for (i = 0; i < RESERVE_NR; i++)
1230 total_reserve += ca->free[i].size;
1232 ca->copygc_write_point.group = &ca->self;
1233 ca->tiering_write_point.group = &ca->self;
1236 rcu_assign_pointer(c->devs[ca->dev_idx], ca);
1238 if (bch_dev_sysfs_online(ca))
1239 pr_warn("error creating sysfs objects");
1247 static int __bch_dev_online(struct bch_fs *c, struct bcache_superblock *sb)
1252 lockdep_assert_held(&c->sb_lock);
1254 if (le64_to_cpu(sb->sb->seq) >
1255 le64_to_cpu(c->disk_sb->seq))
1256 bch_sb_to_fs(c, sb->sb);
1258 BUG_ON(sb->sb->dev_idx >= c->sb.nr_devices ||
1259 !c->devs[sb->sb->dev_idx]);
1261 ca = c->devs[sb->sb->dev_idx];
1262 if (ca->disk_sb.bdev) {
1263 bch_err(c, "already have device online in slot %u",
1268 ret = bch_dev_journal_init(ca, sb->sb);
1273 * Increase journal write timeout if flushes to this device are
1276 if (!blk_queue_nonrot(bdev_get_queue(sb->bdev)) &&
1277 journal_flushes_device(ca))
1278 c->journal.write_delay_ms =
1279 max(c->journal.write_delay_ms, 1000U);
1283 if (sb->mode & FMODE_EXCL)
1284 ca->disk_sb.bdev->bd_holder = ca;
1285 memset(sb, 0, sizeof(*sb));
1287 if (c->sb.nr_devices == 1)
1288 bdevname(ca->disk_sb.bdev, c->name);
1289 bdevname(ca->disk_sb.bdev, ca->name);
1291 if (bch_dev_sysfs_online(ca))
1292 pr_warn("error creating sysfs objects");
1294 lg_local_lock(&c->usage_lock);
1295 if (!gc_will_visit(c, gc_phase(GC_PHASE_SB_METADATA)))
1296 bch_mark_dev_metadata(ca->fs, ca);
1297 lg_local_unlock(&c->usage_lock);
1299 percpu_ref_reinit(&ca->io_ref);
1303 /* Device management: */
1305 bool bch_fs_may_start(struct bch_fs *c, int flags)
1307 struct bch_sb_field_members *mi;
1308 unsigned meta_missing = 0;
1309 unsigned data_missing = 0;
1310 bool degraded = false;
1313 mutex_lock(&c->sb_lock);
1314 mi = bch_sb_get_members(c->disk_sb);
1316 for (i = 0; i < c->disk_sb->nr_devices; i++)
1318 !bch_is_zero(mi->members[i].uuid.b, sizeof(uuid_le))) {
1320 if (BCH_MEMBER_HAS_METADATA(&mi->members[i]))
1322 if (BCH_MEMBER_HAS_DATA(&mi->members[i]))
1325 mutex_unlock(&c->sb_lock);
1328 !(flags & BCH_FORCE_IF_DEGRADED))
1332 !(flags & BCH_FORCE_IF_METADATA_DEGRADED))
1335 if (meta_missing >= BCH_SB_META_REPLICAS_HAVE(c->disk_sb) &&
1336 !(flags & BCH_FORCE_IF_METADATA_LOST))
1339 if (data_missing && !(flags & BCH_FORCE_IF_DATA_DEGRADED))
1342 if (data_missing >= BCH_SB_DATA_REPLICAS_HAVE(c->disk_sb) &&
1343 !(flags & BCH_FORCE_IF_DATA_LOST))
1349 bool bch_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca,
1350 enum bch_member_state new_state, int flags)
1352 lockdep_assert_held(&c->state_lock);
1354 if (new_state == BCH_MEMBER_STATE_RW)
1357 if (ca->mi.has_data &&
1358 !(flags & BCH_FORCE_IF_DATA_DEGRADED))
1361 if (ca->mi.has_data &&
1362 c->sb.data_replicas_have <= 1 &&
1363 !(flags & BCH_FORCE_IF_DATA_LOST))
1366 if (ca->mi.has_metadata &&
1367 !(flags & BCH_FORCE_IF_METADATA_DEGRADED))
1370 if (ca->mi.has_metadata &&
1371 c->sb.meta_replicas_have <= 1 &&
1372 !(flags & BCH_FORCE_IF_METADATA_LOST))
1378 static void __bch_dev_read_only(struct bch_fs *c, struct bch_dev *ca)
1380 bch_moving_gc_stop(ca);
1383 * This stops new data writes (e.g. to existing open data
1384 * buckets) and then waits for all existing writes to
1387 bch_dev_allocator_stop(ca);
1389 bch_dev_group_remove(&c->journal.devs, ca);
1392 static const char *__bch_dev_read_write(struct bch_fs *c, struct bch_dev *ca)
1394 lockdep_assert_held(&c->state_lock);
1396 BUG_ON(ca->mi.state != BCH_MEMBER_STATE_RW);
1398 trace_bcache_cache_read_write(ca);
1400 if (bch_dev_allocator_start(ca))
1401 return "error starting allocator thread";
1403 if (bch_moving_gc_start(ca))
1404 return "error starting moving GC thread";
1406 if (bch_tiering_start(c))
1407 return "error starting tiering thread";
1409 bch_notify_dev_read_write(ca);
1410 trace_bcache_cache_read_write_done(ca);
1415 int __bch_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
1416 enum bch_member_state new_state, int flags)
1418 struct bch_sb_field_members *mi;
1420 if (ca->mi.state == new_state)
1423 if (!bch_dev_state_allowed(c, ca, new_state, flags))
1426 if (new_state == BCH_MEMBER_STATE_RW) {
1427 if (__bch_dev_read_write(c, ca))
1430 __bch_dev_read_only(c, ca);
1433 bch_notice(ca, "%s", bch_dev_state[new_state]);
1435 mutex_lock(&c->sb_lock);
1436 mi = bch_sb_get_members(c->disk_sb);
1437 SET_BCH_MEMBER_STATE(&mi->members[ca->dev_idx], new_state);
1439 mutex_unlock(&c->sb_lock);
1444 int bch_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
1445 enum bch_member_state new_state, int flags)
1449 mutex_lock(&c->state_lock);
1450 ret = __bch_dev_set_state(c, ca, new_state, flags);
1451 mutex_unlock(&c->state_lock);
1457 int bch_dev_migrate_from(struct bch_fs *c, struct bch_dev *ca)
1459 /* First, go RO before we try to migrate data off: */
1460 ret = bch_dev_set_state(c, ca, BCH_MEMBER_STATE_RO, flags);
1464 bch_notify_dev_removing(ca);
1466 /* Migrate data, metadata off device: */
1468 ret = bch_move_data_off_device(ca);
1469 if (ret && !(flags & BCH_FORCE_IF_DATA_LOST)) {
1470 bch_err(c, "Remove of %s failed, unable to migrate data off",
1476 ret = bch_flag_data_bad(ca);
1478 bch_err(c, "Remove of %s failed, unable to migrate data off",
1483 ret = bch_move_metadata_off_device(ca);
1489 /* Device add/removal: */
1491 static int __bch_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags)
1493 struct bch_sb_field_members *mi;
1494 unsigned dev_idx = ca->dev_idx;
1497 if (ca->mi.state == BCH_MEMBER_STATE_RW) {
1498 bch_err(ca, "Cannot remove RW device");
1499 bch_notify_dev_remove_failed(ca);
1503 if (!bch_dev_state_allowed(c, ca, BCH_MEMBER_STATE_FAILED, flags)) {
1504 bch_err(ca, "Cannot remove without losing data");
1505 bch_notify_dev_remove_failed(ca);
1510 * XXX: verify that dev_idx is really not in use anymore, anywhere
1512 * flag_data_bad() does not check btree pointers
1514 ret = bch_flag_data_bad(ca);
1516 bch_err(ca, "Remove failed");
1520 if (ca->mi.has_data || ca->mi.has_metadata) {
1521 bch_err(ca, "Can't remove, still has data");
1526 * Ok, really doing the remove:
1527 * Drop device's prio pointer before removing it from superblock:
1529 bch_notify_dev_removed(ca);
1531 spin_lock(&c->journal.lock);
1532 c->journal.prio_buckets[dev_idx] = 0;
1533 spin_unlock(&c->journal.lock);
1535 bch_journal_meta(&c->journal);
1537 __bch_dev_offline(ca);
1542 * Free this device's slot in the bch_member array - all pointers to
1543 * this device must be gone:
1545 mutex_lock(&c->sb_lock);
1546 mi = bch_sb_get_members(c->disk_sb);
1547 memset(&mi->members[dev_idx].uuid, 0, sizeof(mi->members[dev_idx].uuid));
1551 mutex_unlock(&c->sb_lock);
1556 int bch_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags)
1560 mutex_lock(&c->state_lock);
1561 percpu_ref_put(&ca->ref);
1562 ret = __bch_dev_remove(c, ca, flags);
1563 mutex_unlock(&c->state_lock);
1568 int bch_dev_add(struct bch_fs *c, const char *path)
1570 struct bcache_superblock sb;
1572 struct bch_dev *ca = NULL;
1573 struct bch_sb_field_members *mi, *dev_mi;
1574 struct bch_member saved_mi;
1575 unsigned dev_idx, nr_devices, u64s;
1578 err = bch_read_super(&sb, bch_opts_empty(), path);
1582 err = bch_validate_cache_super(&sb);
1586 err = bch_dev_may_add(sb.sb, c);
1590 mutex_lock(&c->state_lock);
1591 mutex_lock(&c->sb_lock);
1594 * Preserve the old cache member information (esp. tier)
1595 * before we start bashing the disk stuff.
1597 dev_mi = bch_sb_get_members(sb.sb);
1598 saved_mi = dev_mi->members[sb.sb->dev_idx];
1599 saved_mi.last_mount = cpu_to_le64(ktime_get_seconds());
1601 if (dynamic_fault("bcache:add:no_slot"))
1604 mi = bch_sb_get_members(c->disk_sb);
1605 for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++)
1606 if (dev_idx >= c->sb.nr_devices ||
1607 bch_is_zero(mi->members[dev_idx].uuid.b,
1611 err = "no slots available in superblock";
1616 nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices);
1617 u64s = (sizeof(struct bch_sb_field_members) +
1618 sizeof(struct bch_member) * nr_devices) / sizeof(u64);
1619 err = "no space in superblock for member info";
1621 mi = bch_fs_sb_resize_members(c, u64s);
1625 dev_mi = bch_sb_resize_members(&sb, u64s);
1629 memcpy(dev_mi, mi, u64s * sizeof(u64));
1630 dev_mi->members[dev_idx] = saved_mi;
1632 sb.sb->uuid = c->disk_sb->uuid;
1633 sb.sb->dev_idx = dev_idx;
1634 sb.sb->nr_devices = nr_devices;
1636 /* commit new member info */
1637 memcpy(mi, dev_mi, u64s * sizeof(u64));
1638 c->disk_sb->nr_devices = nr_devices;
1639 c->sb.nr_devices = nr_devices;
1641 if (bch_dev_alloc(c, dev_idx)) {
1642 err = "cannot allocate memory";
1647 if (__bch_dev_online(c, &sb)) {
1648 err = "bch_dev_online() error";
1654 mutex_unlock(&c->sb_lock);
1656 ca = c->devs[dev_idx];
1657 if (ca->mi.state == BCH_MEMBER_STATE_RW) {
1658 err = "journal alloc failed";
1659 if (bch_dev_journal_alloc(ca))
1662 err = __bch_dev_read_write(c, ca);
1667 bch_notify_dev_added(ca);
1668 mutex_unlock(&c->state_lock);
1671 mutex_unlock(&c->sb_lock);
1673 mutex_unlock(&c->state_lock);
1674 bch_free_super(&sb);
1676 bch_err(c, "Unable to add device: %s", err);
1677 return ret ?: -EINVAL;
1680 int bch_dev_online(struct bch_fs *c, const char *path)
1682 struct bcache_superblock sb = { 0 };
1685 mutex_lock(&c->state_lock);
1687 err = bch_read_super(&sb, bch_opts_empty(), path);
1691 err = bch_dev_in_fs(c->disk_sb, sb.sb);
1695 mutex_lock(&c->sb_lock);
1696 if (__bch_dev_online(c, &sb)) {
1697 mutex_unlock(&c->sb_lock);
1700 mutex_unlock(&c->sb_lock);
1702 mutex_unlock(&c->state_lock);
1705 mutex_unlock(&c->state_lock);
1706 bch_free_super(&sb);
1707 bch_err(c, "error bringing %s online: %s", path, err);
1711 int bch_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags)
1713 mutex_lock(&c->state_lock);
1715 if (!bch_dev_state_allowed(c, ca, BCH_MEMBER_STATE_FAILED, flags)) {
1716 bch_err(ca, "Cannot offline required disk");
1717 mutex_unlock(&c->state_lock);
1721 __bch_dev_read_only(c, ca);
1722 __bch_dev_offline(ca);
1724 mutex_unlock(&c->state_lock);
1728 int bch_dev_migrate(struct bch_fs *c, struct bch_dev *ca)
1732 mutex_lock(&c->state_lock);
1734 if (ca->mi.state == BCH_MEMBER_STATE_RW) {
1735 bch_err(ca, "Cannot migrate data off RW device");
1736 mutex_unlock(&c->state_lock);
1740 mutex_unlock(&c->state_lock);
1742 ret = bch_move_data_off_device(ca);
1744 bch_err(ca, "Error migrating data: %i", ret);
1748 ret = bch_move_metadata_off_device(ca);
1750 bch_err(ca, "Error migrating metadata: %i", ret);
1754 if (ca->mi.has_data || ca->mi.has_metadata) {
1755 bch_err(ca, "Migrate error: data still present");
1762 /* Filesystem open: */
1764 const char *bch_fs_open(char * const *devices, unsigned nr_devices,
1765 struct bch_opts opts, struct bch_fs **ret)
1768 struct bch_fs *c = NULL;
1769 struct bcache_superblock *sb;
1770 unsigned i, best_sb = 0;
1773 return "need at least one device";
1775 if (!try_module_get(THIS_MODULE))
1776 return "module unloading";
1778 err = "cannot allocate memory";
1779 sb = kcalloc(nr_devices, sizeof(*sb), GFP_KERNEL);
1783 for (i = 0; i < nr_devices; i++) {
1784 err = bch_read_super(&sb[i], opts, devices[i]);
1788 err = "attempting to register backing device";
1789 if (__SB_IS_BDEV(le64_to_cpu(sb[i].sb->version)))
1792 err = bch_validate_cache_super(&sb[i]);
1797 for (i = 1; i < nr_devices; i++)
1798 if (le64_to_cpu(sb[i].sb->seq) >
1799 le64_to_cpu(sb[best_sb].sb->seq))
1802 for (i = 0; i < nr_devices; i++) {
1803 err = bch_dev_in_fs(sb[best_sb].sb, sb[i].sb);
1808 err = "cannot allocate memory";
1809 c = bch_fs_alloc(sb[best_sb].sb, opts);
1813 err = "bch_dev_online() error";
1814 mutex_lock(&c->sb_lock);
1815 for (i = 0; i < nr_devices; i++)
1816 if (__bch_dev_online(c, &sb[i])) {
1817 mutex_unlock(&c->sb_lock);
1820 mutex_unlock(&c->sb_lock);
1822 err = "insufficient devices";
1823 if (!bch_fs_may_start(c, 0))
1826 if (!c->opts.nostart) {
1827 err = __bch_fs_start(c);
1832 err = bch_fs_online(c);
1839 closure_put(&c->cl);
1844 module_put(THIS_MODULE);
1852 for (i = 0; i < nr_devices; i++)
1853 bch_free_super(&sb[i]);
1857 static const char *__bch_fs_open_incremental(struct bcache_superblock *sb,
1858 struct bch_opts opts)
1862 bool allocated_fs = false;
1864 err = bch_validate_cache_super(sb);
1868 mutex_lock(&bch_fs_list_lock);
1869 c = __bch_uuid_to_fs(sb->sb->uuid);
1871 closure_get(&c->cl);
1873 err = bch_dev_in_fs(c->disk_sb, sb->sb);
1877 c = bch_fs_alloc(sb->sb, opts);
1878 err = "cannot allocate memory";
1882 allocated_fs = true;
1885 err = "bch_dev_online() error";
1887 mutex_lock(&c->sb_lock);
1888 if (__bch_dev_online(c, sb)) {
1889 mutex_unlock(&c->sb_lock);
1892 mutex_unlock(&c->sb_lock);
1894 if (!c->opts.nostart && bch_fs_may_start(c, 0)) {
1895 err = __bch_fs_start(c);
1900 err = __bch_fs_online(c);
1904 closure_put(&c->cl);
1905 mutex_unlock(&bch_fs_list_lock);
1909 mutex_unlock(&bch_fs_list_lock);
1914 closure_put(&c->cl);
1919 const char *bch_fs_open_incremental(const char *path)
1921 struct bcache_superblock sb;
1922 struct bch_opts opts = bch_opts_empty();
1925 err = bch_read_super(&sb, opts, path);
1929 if (__SB_IS_BDEV(le64_to_cpu(sb.sb->version))) {
1930 mutex_lock(&bch_fs_list_lock);
1931 err = bch_backing_dev_register(&sb);
1932 mutex_unlock(&bch_fs_list_lock);
1934 err = __bch_fs_open_incremental(&sb, opts);
1937 bch_free_super(&sb);
1942 /* Global interfaces/init */
1944 #define kobj_attribute_write(n, fn) \
1945 static struct kobj_attribute ksysfs_##n = __ATTR(n, S_IWUSR, NULL, fn)
1947 #define kobj_attribute_rw(n, show, store) \
1948 static struct kobj_attribute ksysfs_##n = \
1949 __ATTR(n, S_IWUSR|S_IRUSR, show, store)
1951 static ssize_t register_bcache(struct kobject *, struct kobj_attribute *,
1952 const char *, size_t);
1954 kobj_attribute_write(register, register_bcache);
1955 kobj_attribute_write(register_quiet, register_bcache);
1957 static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
1958 const char *buffer, size_t size)
1960 ssize_t ret = -EINVAL;
1961 const char *err = "cannot allocate memory";
1964 if (!try_module_get(THIS_MODULE))
1967 if (!(path = kstrndup(skip_spaces(buffer), size, GFP_KERNEL)))
1970 err = bch_fs_open_incremental(strim(path));
1977 module_put(THIS_MODULE);
1980 pr_err("error opening %s: %s", path, err);
1984 static int bcache_reboot(struct notifier_block *n, unsigned long code, void *x)
1986 if (code == SYS_DOWN ||
1988 code == SYS_POWER_OFF) {
1991 mutex_lock(&bch_fs_list_lock);
1993 if (!list_empty(&bch_fs_list))
1994 pr_info("Setting all devices read only:");
1996 list_for_each_entry(c, &bch_fs_list, list)
1997 bch_fs_read_only_async(c);
1999 list_for_each_entry(c, &bch_fs_list, list)
2000 bch_fs_read_only(c);
2002 mutex_unlock(&bch_fs_list_lock);
2008 static struct notifier_block reboot = {
2009 .notifier_call = bcache_reboot,
2010 .priority = INT_MAX, /* before any real devices */
2013 static ssize_t reboot_test(struct kobject *k, struct kobj_attribute *attr,
2014 const char *buffer, size_t size)
2016 bcache_reboot(NULL, SYS_DOWN, NULL);
2020 kobj_attribute_write(reboot, reboot_test);
2022 static void bcache_exit(void)
2026 bch_blockdev_exit();
2029 kset_unregister(bcache_kset);
2031 destroy_workqueue(bcache_io_wq);
2032 if (!IS_ERR_OR_NULL(bch_sha256))
2033 crypto_free_shash(bch_sha256);
2034 unregister_reboot_notifier(&reboot);
2037 static int __init bcache_init(void)
2039 static const struct attribute *files[] = {
2040 &ksysfs_register.attr,
2041 &ksysfs_register_quiet.attr,
2042 &ksysfs_reboot.attr,
2046 register_reboot_notifier(&reboot);
2047 closure_debug_init();
2050 bch_sha256 = crypto_alloc_shash("sha256", 0, 0);
2051 if (IS_ERR(bch_sha256))
2054 if (!(bcache_io_wq = create_freezable_workqueue("bcache_io")) ||
2055 !(bcache_kset = kset_create_and_add("bcache", NULL, fs_kobj)) ||
2056 sysfs_create_files(&bcache_kset->kobj, files) ||
2057 bch_chardev_init() ||
2058 bch_blockdev_init() ||
2069 #define BCH_DEBUG_PARAM(name, description) \
2071 module_param_named(name, bch_##name, bool, 0644); \
2072 MODULE_PARM_DESC(name, description);
2074 #undef BCH_DEBUG_PARAM
2076 module_exit(bcache_exit);
2077 module_init(bcache_init);