Lines Matching full:ca

195 		for_each_member_device_rcu(c, ca, NULL)  in bch2_dev_to_fs()
196 if (ca->disk_sb.bdev && ca->disk_sb.bdev->bd_dev == dev) { in bch2_dev_to_fs()
294 for_each_member_device(c, ca) { in __bch2_fs_read_only()
295 bch2_dev_io_ref_stop(ca, WRITE); in __bch2_fs_read_only()
296 bch2_dev_allocator_remove(c, ca); in __bch2_fs_read_only()
448 __for_each_online_member(c, ca, BIT(BCH_MEMBER_STATE_rw), READ) { in __bch2_fs_read_write()
449 bch2_dev_allocator_add(c, ca); in __bch2_fs_read_write()
450 percpu_ref_reinit(&ca->io_ref[WRITE]); in __bch2_fs_read_write()
631 for_each_member_device(c, ca) in __bch2_fs_stop()
632 bch2_dev_unlink(ca); in __bch2_fs_stop()
651 for_each_member_device(c, ca) in __bch2_fs_stop()
652 cancel_work_sync(&ca->io_error_work); in __bch2_fs_stop()
669 struct bch_dev *ca = rcu_dereference_protected(c->devs[i], true); in bch2_fs_free() local
671 if (ca) { in bch2_fs_free()
672 EBUG_ON(atomic_long_read(&ca->ref) != 1); in bch2_fs_free()
673 bch2_dev_io_ref_stop(ca, READ); in bch2_fs_free()
674 bch2_free_super(&ca->disk_sb); in bch2_fs_free()
675 bch2_dev_free(ca); in bch2_fs_free()
724 for_each_member_device(c, ca) { in bch2_fs_online()
725 ret = bch2_dev_sysfs_online(c, ca); in bch2_fs_online()
728 bch2_dev_put(ca); in bch2_fs_online()
1022 struct bch_dev *ca; in bch2_fs_may_start() local
1039 ca = bch2_dev_locked(c, i); in bch2_fs_may_start()
1041 if (!bch2_dev_is_online(ca) && in bch2_fs_may_start()
1042 (ca->mi.state == BCH_MEMBER_STATE_rw || in bch2_fs_may_start()
1043 ca->mi.state == BCH_MEMBER_STATE_ro)) { in bch2_fs_may_start()
1084 for_each_online_member(c, ca) in bch2_fs_start()
1085 bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx)->last_mount = cpu_to_le64(now); in bch2_fs_start()
1089 for_each_rw_member(c, ca) in bch2_fs_start()
1090 bch2_dev_allocator_add(c, ca); in bch2_fs_start()
1235 static void bch2_dev_io_ref_stop(struct bch_dev *ca, int rw) in bch2_dev_io_ref_stop() argument
1237 if (!percpu_ref_is_zero(&ca->io_ref[rw])) { in bch2_dev_io_ref_stop()
1238 reinit_completion(&ca->io_ref_completion[rw]); in bch2_dev_io_ref_stop()
1239 percpu_ref_kill(&ca->io_ref[rw]); in bch2_dev_io_ref_stop()
1240 wait_for_completion(&ca->io_ref_completion[rw]); in bch2_dev_io_ref_stop()
1246 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj); in bch2_dev_release() local
1248 kfree(ca); in bch2_dev_release()
1251 static void bch2_dev_free(struct bch_dev *ca) in bch2_dev_free() argument
1253 WARN_ON(!percpu_ref_is_zero(&ca->io_ref[WRITE])); in bch2_dev_free()
1254 WARN_ON(!percpu_ref_is_zero(&ca->io_ref[READ])); in bch2_dev_free()
1256 cancel_work_sync(&ca->io_error_work); in bch2_dev_free()
1258 bch2_dev_unlink(ca); in bch2_dev_free()
1260 if (ca->kobj.state_in_sysfs) in bch2_dev_free()
1261 kobject_del(&ca->kobj); in bch2_dev_free()
1263 bch2_free_super(&ca->disk_sb); in bch2_dev_free()
1264 bch2_dev_allocator_background_exit(ca); in bch2_dev_free()
1265 bch2_dev_journal_exit(ca); in bch2_dev_free()
1267 free_percpu(ca->io_done); in bch2_dev_free()
1268 bch2_dev_buckets_free(ca); in bch2_dev_free()
1269 kfree(ca->sb_read_scratch); in bch2_dev_free()
1271 bch2_time_stats_quantiles_exit(&ca->io_latency[WRITE]); in bch2_dev_free()
1272 bch2_time_stats_quantiles_exit(&ca->io_latency[READ]); in bch2_dev_free()
1274 percpu_ref_exit(&ca->io_ref[WRITE]); in bch2_dev_free()
1275 percpu_ref_exit(&ca->io_ref[READ]); in bch2_dev_free()
1277 percpu_ref_exit(&ca->ref); in bch2_dev_free()
1279 kobject_put(&ca->kobj); in bch2_dev_free()
1282 static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca) in __bch2_dev_offline() argument
1287 if (percpu_ref_is_zero(&ca->io_ref[READ])) in __bch2_dev_offline()
1290 __bch2_dev_read_only(c, ca); in __bch2_dev_offline()
1292 bch2_dev_io_ref_stop(ca, READ); in __bch2_dev_offline()
1294 bch2_dev_unlink(ca); in __bch2_dev_offline()
1296 bch2_free_super(&ca->disk_sb); in __bch2_dev_offline()
1297 bch2_dev_journal_exit(ca); in __bch2_dev_offline()
1303 struct bch_dev *ca = container_of(ref, struct bch_dev, ref); in bch2_dev_ref_complete() local
1305 complete(&ca->ref_completion); in bch2_dev_ref_complete()
1311 struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref[READ]); in bch2_dev_io_ref_read_complete() local
1313 complete(&ca->io_ref_completion[READ]); in bch2_dev_io_ref_read_complete()
1318 struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref[WRITE]); in bch2_dev_io_ref_write_complete() local
1320 complete(&ca->io_ref_completion[WRITE]); in bch2_dev_io_ref_write_complete()
1323 static void bch2_dev_unlink(struct bch_dev *ca) in bch2_dev_unlink() argument
1335 if (ca->kobj.state_in_sysfs && in bch2_dev_unlink()
1336 ca->disk_sb.bdev && in bch2_dev_unlink()
1337 (b = bdev_kobj(ca->disk_sb.bdev))->state_in_sysfs) { in bch2_dev_unlink()
1339 sysfs_remove_link(&ca->kobj, "block"); in bch2_dev_unlink()
1343 static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca) in bch2_dev_sysfs_online() argument
1350 if (!ca->kobj.state_in_sysfs) { in bch2_dev_sysfs_online()
1351 ret = kobject_add(&ca->kobj, &c->kobj, "dev-%u", ca->dev_idx) ?: in bch2_dev_sysfs_online()
1352 bch2_opts_create_sysfs_files(&ca->kobj, OPT_DEVICE); in bch2_dev_sysfs_online()
1357 if (ca->disk_sb.bdev) { in bch2_dev_sysfs_online()
1358 struct kobject *block = bdev_kobj(ca->disk_sb.bdev); in bch2_dev_sysfs_online()
1360 ret = sysfs_create_link(block, &ca->kobj, "bcachefs"); in bch2_dev_sysfs_online()
1364 ret = sysfs_create_link(&ca->kobj, block, "block"); in bch2_dev_sysfs_online()
1375 struct bch_dev *ca; in __bch2_dev_alloc() local
1378 ca = kzalloc(sizeof(*ca), GFP_KERNEL); in __bch2_dev_alloc()
1379 if (!ca) in __bch2_dev_alloc()
1382 kobject_init(&ca->kobj, &bch2_dev_ktype); in __bch2_dev_alloc()
1383 init_completion(&ca->ref_completion); in __bch2_dev_alloc()
1384 init_completion(&ca->io_ref_completion[READ]); in __bch2_dev_alloc()
1385 init_completion(&ca->io_ref_completion[WRITE]); in __bch2_dev_alloc()
1387 INIT_WORK(&ca->io_error_work, bch2_io_error_work); in __bch2_dev_alloc()
1389 bch2_time_stats_quantiles_init(&ca->io_latency[READ]); in __bch2_dev_alloc()
1390 bch2_time_stats_quantiles_init(&ca->io_latency[WRITE]); in __bch2_dev_alloc()
1392 ca->mi = bch2_mi_to_cpu(member); in __bch2_dev_alloc()
1395 atomic64_set(&ca->errors[i], le64_to_cpu(member->errors[i])); in __bch2_dev_alloc()
1397 ca->uuid = member->uuid; in __bch2_dev_alloc()
1399 ca->nr_btree_reserve = DIV_ROUND_UP(BTREE_NODE_RESERVE, in __bch2_dev_alloc()
1400 ca->mi.bucket_size / btree_sectors(c)); in __bch2_dev_alloc()
1403 if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete, 0, GFP_KERNEL)) in __bch2_dev_alloc()
1406 atomic_long_set(&ca->ref, 1); in __bch2_dev_alloc()
1409 bch2_dev_allocator_background_init(ca); in __bch2_dev_alloc()
1411 if (percpu_ref_init(&ca->io_ref[READ], bch2_dev_io_ref_read_complete, in __bch2_dev_alloc()
1413 percpu_ref_init(&ca->io_ref[WRITE], bch2_dev_io_ref_write_complete, in __bch2_dev_alloc()
1415 !(ca->sb_read_scratch = kmalloc(BCH_SB_READ_SCRATCH_BUF_SIZE, GFP_KERNEL)) || in __bch2_dev_alloc()
1416 bch2_dev_buckets_alloc(c, ca) || in __bch2_dev_alloc()
1417 !(ca->io_done = alloc_percpu(*ca->io_done))) in __bch2_dev_alloc()
1420 return ca; in __bch2_dev_alloc()
1422 bch2_dev_free(ca); in __bch2_dev_alloc()
1426 static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca, in bch2_dev_attach() argument
1429 ca->dev_idx = dev_idx; in bch2_dev_attach()
1430 __set_bit(ca->dev_idx, ca->self.d); in bch2_dev_attach()
1431 scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx); in bch2_dev_attach()
1433 ca->fs = c; in bch2_dev_attach()
1434 rcu_assign_pointer(c->devs[ca->dev_idx], ca); in bch2_dev_attach()
1436 if (bch2_dev_sysfs_online(c, ca)) in bch2_dev_attach()
1443 struct bch_dev *ca = NULL; in bch2_dev_alloc() local
1448 ca = __bch2_dev_alloc(c, &member); in bch2_dev_alloc()
1449 if (!ca) in bch2_dev_alloc()
1452 ca->fs = c; in bch2_dev_alloc()
1454 bch2_dev_attach(c, ca, dev_idx); in bch2_dev_alloc()
1460 static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb) in __bch2_dev_attach_bdev() argument
1464 if (bch2_dev_is_online(ca)) { in __bch2_dev_attach_bdev()
1465 bch_err(ca, "already have device online in slot %u", in __bch2_dev_attach_bdev()
1471 ca->mi.bucket_size * ca->mi.nbuckets) { in __bch2_dev_attach_bdev()
1472 bch_err(ca, "cannot online: device too small"); in __bch2_dev_attach_bdev()
1476 BUG_ON(!percpu_ref_is_zero(&ca->io_ref[READ])); in __bch2_dev_attach_bdev()
1477 BUG_ON(!percpu_ref_is_zero(&ca->io_ref[WRITE])); in __bch2_dev_attach_bdev()
1479 ret = bch2_dev_journal_init(ca, sb->sb); in __bch2_dev_attach_bdev()
1484 ca->disk_sb = *sb; in __bch2_dev_attach_bdev()
1492 ca->disk_sb.holder->c = ca->fs; in __bch2_dev_attach_bdev()
1494 ca->dev = ca->disk_sb.bdev->bd_dev; in __bch2_dev_attach_bdev()
1496 percpu_ref_reinit(&ca->io_ref[READ]); in __bch2_dev_attach_bdev()
1503 struct bch_dev *ca; in bch2_dev_attach_bdev() local
1514 ca = bch2_dev_locked(c, sb->sb->dev_idx); in bch2_dev_attach_bdev()
1516 ret = __bch2_dev_attach_bdev(ca, sb); in bch2_dev_attach_bdev()
1520 bch2_dev_sysfs_online(c, ca); in bch2_dev_attach_bdev()
1523 prt_bdevname(&name, ca->disk_sb.bdev); in bch2_dev_attach_bdev()
1527 strscpy(ca->name, name.buf, sizeof(ca->name)); in bch2_dev_attach_bdev()
1546 bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca, in bch2_dev_state_allowed() argument
1558 if (ca->mi.state != BCH_MEMBER_STATE_rw) in bch2_dev_state_allowed()
1563 if (ca2 != ca) in bch2_dev_state_allowed()
1576 if (ca->mi.state != BCH_MEMBER_STATE_rw && in bch2_dev_state_allowed()
1577 ca->mi.state != BCH_MEMBER_STATE_ro) in bch2_dev_state_allowed()
1582 __clear_bit(ca->dev_idx, new_online_devs.d); in bch2_dev_state_allowed()
1590 static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca) in __bch2_dev_read_only() argument
1592 bch2_dev_io_ref_stop(ca, WRITE); in __bch2_dev_read_only()
1597 bch2_dev_allocator_remove(c, ca); in __bch2_dev_read_only()
1599 bch2_dev_journal_stop(&c->journal, ca); in __bch2_dev_read_only()
1602 static void __bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca) in __bch2_dev_read_write() argument
1606 BUG_ON(ca->mi.state != BCH_MEMBER_STATE_rw); in __bch2_dev_read_write()
1608 bch2_dev_allocator_add(c, ca); in __bch2_dev_read_write()
1611 if (percpu_ref_is_zero(&ca->io_ref[WRITE])) in __bch2_dev_read_write()
1612 percpu_ref_reinit(&ca->io_ref[WRITE]); in __bch2_dev_read_write()
1614 bch2_dev_do_discards(ca); in __bch2_dev_read_write()
1617 int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, in __bch2_dev_set_state() argument
1623 if (ca->mi.state == new_state) in __bch2_dev_set_state()
1626 if (!bch2_dev_state_allowed(c, ca, new_state, flags)) in __bch2_dev_set_state()
1630 __bch2_dev_read_only(c, ca); in __bch2_dev_set_state()
1632 bch_notice(ca, "%s", bch2_member_states[new_state]); in __bch2_dev_set_state()
1635 m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); in __bch2_dev_set_state()
1641 __bch2_dev_read_write(c, ca); in __bch2_dev_set_state()
1648 int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, in bch2_dev_set_state() argument
1654 ret = __bch2_dev_set_state(c, ca, new_state, flags); in bch2_dev_set_state()
1662 int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags) in bch2_dev_remove() argument
1665 unsigned dev_idx = ca->dev_idx, data; in bch2_dev_remove()
1671 * We consume a reference to ca->ref, regardless of whether we succeed in bch2_dev_remove()
1674 bch2_dev_put(ca); in bch2_dev_remove()
1676 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { in bch2_dev_remove()
1677 bch_err(ca, "Cannot remove without losing data"); in bch2_dev_remove()
1682 __bch2_dev_read_only(c, ca); in bch2_dev_remove()
1684 ret = bch2_dev_data_drop(c, ca->dev_idx, flags); in bch2_dev_remove()
1685 bch_err_msg(ca, ret, "bch2_dev_data_drop()"); in bch2_dev_remove()
1689 ret = bch2_dev_remove_alloc(c, ca); in bch2_dev_remove()
1690 bch_err_msg(ca, ret, "bch2_dev_remove_alloc()"); in bch2_dev_remove()
1704 ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx); in bch2_dev_remove()
1705 bch_err_msg(ca, ret, "bch2_journal_flush_device_pins()"); in bch2_dev_remove()
1710 bch_err_msg(ca, ret, "bch2_journal_flush()"); in bch2_dev_remove()
1715 bch_err_msg(ca, ret, "bch2_replicas_gc2()"); in bch2_dev_remove()
1719 data = bch2_dev_has_data(c, ca); in bch2_dev_remove()
1724 bch_err(ca, "Remove failed, still has data (%s)", data_has.buf); in bch2_dev_remove()
1730 __bch2_dev_offline(c, ca); in bch2_dev_remove()
1733 rcu_assign_pointer(c->devs[ca->dev_idx], NULL); in bch2_dev_remove()
1737 percpu_ref_kill(&ca->ref); in bch2_dev_remove()
1739 ca->dying = true; in bch2_dev_remove()
1740 bch2_dev_put(ca); in bch2_dev_remove()
1742 wait_for_completion(&ca->ref_completion); in bch2_dev_remove()
1744 bch2_dev_free(ca); in bch2_dev_remove()
1761 ca->mi.state == BCH_MEMBER_STATE_rw && in bch2_dev_remove()
1762 !percpu_ref_is_zero(&ca->io_ref[READ])) in bch2_dev_remove()
1763 __bch2_dev_read_write(c, ca); in bch2_dev_remove()
1773 struct bch_dev *ca = NULL; in bch2_dev_add() local
1797 ca = __bch2_dev_alloc(c, &dev_mi); in bch2_dev_add()
1798 if (!ca) { in bch2_dev_add()
1803 ret = __bch2_dev_attach_bdev(ca, &sb); in bch2_dev_add()
1810 ret = bch2_sb_from_fs(c, ca); in bch2_dev_add()
1830 ca->disk_sb.sb->dev_idx = dev_idx; in bch2_dev_add()
1831 bch2_dev_attach(c, ca, dev_idx); in bch2_dev_add()
1834 ret = __bch2_dev_group_set(c, ca, label.buf); in bch2_dev_add()
1843 ret = bch2_dev_usage_init(ca, false); in bch2_dev_add()
1847 ret = bch2_trans_mark_dev_sb(c, ca, BTREE_TRIGGER_transactional); in bch2_dev_add()
1848 bch_err_msg(ca, ret, "marking new superblock"); in bch2_dev_add()
1853 bch_err_msg(ca, ret, "initializing free space"); in bch2_dev_add()
1857 if (ca->mi.state == BCH_MEMBER_STATE_rw) in bch2_dev_add()
1858 __bch2_dev_read_write(c, ca); in bch2_dev_add()
1860 ret = bch2_dev_journal_alloc(ca, false); in bch2_dev_add()
1876 if (ca) in bch2_dev_add()
1877 bch2_dev_free(ca); in bch2_dev_add()
1882 ca = NULL; in bch2_dev_add()
1891 struct bch_dev *ca; in bch2_dev_online() local
1914 ca = bch2_dev_locked(c, dev_idx); in bch2_dev_online()
1916 ret = bch2_trans_mark_dev_sb(c, ca, BTREE_TRIGGER_transactional); in bch2_dev_online()
1921 if (ca->mi.state == BCH_MEMBER_STATE_rw) in bch2_dev_online()
1922 __bch2_dev_read_write(c, ca); in bch2_dev_online()
1924 if (!ca->mi.freespace_initialized) { in bch2_dev_online()
1925 ret = bch2_dev_freespace_init(c, ca, 0, ca->mi.nbuckets); in bch2_dev_online()
1926 bch_err_msg(ca, ret, "initializing free space"); in bch2_dev_online()
1931 if (!ca->journal.nr) { in bch2_dev_online()
1932 ret = bch2_dev_journal_alloc(ca, false); in bch2_dev_online()
1933 bch_err_msg(ca, ret, "allocating journal"); in bch2_dev_online()
1939 bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx)->last_mount = in bch2_dev_online()
1952 int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags) in bch2_dev_offline() argument
1956 if (!bch2_dev_is_online(ca)) { in bch2_dev_offline()
1957 bch_err(ca, "Already offline"); in bch2_dev_offline()
1962 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { in bch2_dev_offline()
1963 bch_err(ca, "Cannot offline required disk"); in bch2_dev_offline()
1968 __bch2_dev_offline(c, ca); in bch2_dev_offline()
1974 int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets) in bch2_dev_resize() argument
1981 old_nbuckets = ca->mi.nbuckets; in bch2_dev_resize()
1983 if (nbuckets < ca->mi.nbuckets) { in bch2_dev_resize()
1984 bch_err(ca, "Cannot shrink yet"); in bch2_dev_resize()
1990 bch_err(ca, "New device size too big (%llu greater than max %u)", in bch2_dev_resize()
1996 if (bch2_dev_is_online(ca) && in bch2_dev_resize()
1997 get_capacity(ca->disk_sb.bdev->bd_disk) < in bch2_dev_resize()
1998 ca->mi.bucket_size * nbuckets) { in bch2_dev_resize()
1999 bch_err(ca, "New size larger than device"); in bch2_dev_resize()
2004 ret = bch2_dev_buckets_resize(c, ca, nbuckets); in bch2_dev_resize()
2005 bch_err_msg(ca, ret, "resizing buckets"); in bch2_dev_resize()
2009 ret = bch2_trans_mark_dev_sb(c, ca, BTREE_TRIGGER_transactional); in bch2_dev_resize()
2014 m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); in bch2_dev_resize()
2020 if (ca->mi.freespace_initialized) { in bch2_dev_resize()
2023 ret = bch2_trans_commit_do(ca->fs, NULL, NULL, 0, in bch2_dev_resize()
2025 .dev = ca->dev_idx, in bch2_dev_resize()
2027 bch2_dev_freespace_init(c, ca, old_nbuckets, nbuckets); in bch2_dev_resize()
2038 /* return with ref on ca->ref: */
2044 for_each_member_device(c, ca) in bch2_dev_lookup()
2045 if (!strcmp(name, ca->name)) in bch2_dev_lookup()
2046 return ca; in bch2_dev_lookup()
2068 /* returns with ref on ca->ref */
2071 for_each_member_device(c, ca) in bdev_to_bch_dev()
2072 if (ca->disk_sb.bdev == bdev) in bdev_to_bch_dev()
2073 return ca; in bdev_to_bch_dev()
2094 struct bch_dev *ca = bdev_to_bch_dev(c, bdev); in bch2_fs_bdev_mark_dead() local
2095 if (!ca) in bch2_fs_bdev_mark_dead()
2098 if (bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, BCH_FORCE_IF_DEGRADED)) { in bch2_fs_bdev_mark_dead()
2099 __bch2_dev_offline(c, ca); in bch2_fs_bdev_mark_dead()
2112 bch2_dev_put(ca); in bch2_fs_bdev_mark_dead()