Lines Matching full:ca

172 		for_each_member_device_rcu(c, ca, NULL)  in bch2_dev_to_fs()
173 if (ca->disk_sb.bdev && ca->disk_sb.bdev->bd_dev == dev) { in bch2_dev_to_fs()
219 for_each_member_device_rcu(c, ca, NULL) in bch2_dev_usage_journal_reserve()
282 for_each_member_device(c, ca) in __bch2_fs_read_only()
283 bch2_dev_allocator_remove(c, ca); in __bch2_fs_read_only()
453 for_each_rw_member(c, ca) in __bch2_fs_read_write()
454 bch2_dev_allocator_add(c, ca); in __bch2_fs_read_write()
602 for_each_member_device(c, ca) in __bch2_fs_stop()
603 if (ca->kobj.state_in_sysfs && in __bch2_fs_stop()
604 ca->disk_sb.bdev) in __bch2_fs_stop()
605 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); in __bch2_fs_stop()
624 for_each_member_device(c, ca) in __bch2_fs_stop()
625 cancel_work_sync(&ca->io_error_work); in __bch2_fs_stop()
642 struct bch_dev *ca = rcu_dereference_protected(c->devs[i], true); in bch2_fs_free() local
644 if (ca) { in bch2_fs_free()
645 bch2_free_super(&ca->disk_sb); in bch2_fs_free()
646 bch2_dev_free(ca); in bch2_fs_free()
695 for_each_member_device(c, ca) { in bch2_fs_online()
696 ret = bch2_dev_sysfs_online(c, ca); in bch2_fs_online()
699 percpu_ref_put(&ca->ref); in bch2_fs_online()
1002 for_each_online_member(c, ca) in bch2_fs_start()
1003 bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx)->last_mount = cpu_to_le64(now); in bch2_fs_start()
1007 for_each_rw_member(c, ca) in bch2_fs_start()
1008 bch2_dev_allocator_add(c, ca); in bch2_fs_start()
1147 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj); in bch2_dev_release() local
1149 kfree(ca); in bch2_dev_release()
1152 static void bch2_dev_free(struct bch_dev *ca) in bch2_dev_free() argument
1154 cancel_work_sync(&ca->io_error_work); in bch2_dev_free()
1156 if (ca->kobj.state_in_sysfs && in bch2_dev_free()
1157 ca->disk_sb.bdev) in bch2_dev_free()
1158 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); in bch2_dev_free()
1160 if (ca->kobj.state_in_sysfs) in bch2_dev_free()
1161 kobject_del(&ca->kobj); in bch2_dev_free()
1163 bch2_free_super(&ca->disk_sb); in bch2_dev_free()
1164 bch2_dev_journal_exit(ca); in bch2_dev_free()
1166 free_percpu(ca->io_done); in bch2_dev_free()
1167 bioset_exit(&ca->replica_set); in bch2_dev_free()
1168 bch2_dev_buckets_free(ca); in bch2_dev_free()
1169 free_page((unsigned long) ca->sb_read_scratch); in bch2_dev_free()
1171 bch2_time_stats_exit(&ca->io_latency[WRITE]); in bch2_dev_free()
1172 bch2_time_stats_exit(&ca->io_latency[READ]); in bch2_dev_free()
1174 percpu_ref_exit(&ca->io_ref); in bch2_dev_free()
1175 percpu_ref_exit(&ca->ref); in bch2_dev_free()
1176 kobject_put(&ca->kobj); in bch2_dev_free()
1179 static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca) in __bch2_dev_offline() argument
1184 if (percpu_ref_is_zero(&ca->io_ref)) in __bch2_dev_offline()
1187 __bch2_dev_read_only(c, ca); in __bch2_dev_offline()
1189 reinit_completion(&ca->io_ref_completion); in __bch2_dev_offline()
1190 percpu_ref_kill(&ca->io_ref); in __bch2_dev_offline()
1191 wait_for_completion(&ca->io_ref_completion); in __bch2_dev_offline()
1193 if (ca->kobj.state_in_sysfs) { in __bch2_dev_offline()
1194 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); in __bch2_dev_offline()
1195 sysfs_remove_link(&ca->kobj, "block"); in __bch2_dev_offline()
1198 bch2_free_super(&ca->disk_sb); in __bch2_dev_offline()
1199 bch2_dev_journal_exit(ca); in __bch2_dev_offline()
1204 struct bch_dev *ca = container_of(ref, struct bch_dev, ref); in bch2_dev_ref_complete() local
1206 complete(&ca->ref_completion); in bch2_dev_ref_complete()
1211 struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref); in bch2_dev_io_ref_complete() local
1213 complete(&ca->io_ref_completion); in bch2_dev_io_ref_complete()
1216 static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca) in bch2_dev_sysfs_online() argument
1223 if (!ca->kobj.state_in_sysfs) { in bch2_dev_sysfs_online()
1224 ret = kobject_add(&ca->kobj, &c->kobj, in bch2_dev_sysfs_online()
1225 "dev-%u", ca->dev_idx); in bch2_dev_sysfs_online()
1230 if (ca->disk_sb.bdev) { in bch2_dev_sysfs_online()
1231 struct kobject *block = bdev_kobj(ca->disk_sb.bdev); in bch2_dev_sysfs_online()
1233 ret = sysfs_create_link(block, &ca->kobj, "bcachefs"); in bch2_dev_sysfs_online()
1237 ret = sysfs_create_link(&ca->kobj, block, "block"); in bch2_dev_sysfs_online()
1248 struct bch_dev *ca; in __bch2_dev_alloc() local
1251 ca = kzalloc(sizeof(*ca), GFP_KERNEL); in __bch2_dev_alloc()
1252 if (!ca) in __bch2_dev_alloc()
1255 kobject_init(&ca->kobj, &bch2_dev_ktype); in __bch2_dev_alloc()
1256 init_completion(&ca->ref_completion); in __bch2_dev_alloc()
1257 init_completion(&ca->io_ref_completion); in __bch2_dev_alloc()
1259 init_rwsem(&ca->bucket_lock); in __bch2_dev_alloc()
1261 INIT_WORK(&ca->io_error_work, bch2_io_error_work); in __bch2_dev_alloc()
1263 bch2_time_stats_init(&ca->io_latency[READ]); in __bch2_dev_alloc()
1264 bch2_time_stats_init(&ca->io_latency[WRITE]); in __bch2_dev_alloc()
1266 ca->mi = bch2_mi_to_cpu(member); in __bch2_dev_alloc()
1269 atomic64_set(&ca->errors[i], le64_to_cpu(member->errors[i])); in __bch2_dev_alloc()
1271 ca->uuid = member->uuid; in __bch2_dev_alloc()
1273 ca->nr_btree_reserve = DIV_ROUND_UP(BTREE_NODE_RESERVE, in __bch2_dev_alloc()
1274 ca->mi.bucket_size / btree_sectors(c)); in __bch2_dev_alloc()
1276 if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete, in __bch2_dev_alloc()
1278 percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete, in __bch2_dev_alloc()
1280 !(ca->sb_read_scratch = (void *) __get_free_page(GFP_KERNEL)) || in __bch2_dev_alloc()
1281 bch2_dev_buckets_alloc(c, ca) || in __bch2_dev_alloc()
1282 bioset_init(&ca->replica_set, 4, in __bch2_dev_alloc()
1284 !(ca->io_done = alloc_percpu(*ca->io_done))) in __bch2_dev_alloc()
1287 return ca; in __bch2_dev_alloc()
1289 bch2_dev_free(ca); in __bch2_dev_alloc()
1293 static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca, in bch2_dev_attach() argument
1296 ca->dev_idx = dev_idx; in bch2_dev_attach()
1297 __set_bit(ca->dev_idx, ca->self.d); in bch2_dev_attach()
1298 scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx); in bch2_dev_attach()
1300 ca->fs = c; in bch2_dev_attach()
1301 rcu_assign_pointer(c->devs[ca->dev_idx], ca); in bch2_dev_attach()
1303 if (bch2_dev_sysfs_online(c, ca)) in bch2_dev_attach()
1310 struct bch_dev *ca = NULL; in bch2_dev_alloc() local
1316 ca = __bch2_dev_alloc(c, &member); in bch2_dev_alloc()
1317 if (!ca) in bch2_dev_alloc()
1320 ca->fs = c; in bch2_dev_alloc()
1322 bch2_dev_attach(c, ca, dev_idx); in bch2_dev_alloc()
1325 if (ca) in bch2_dev_alloc()
1326 bch2_dev_free(ca); in bch2_dev_alloc()
1330 static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb) in __bch2_dev_attach_bdev() argument
1334 if (bch2_dev_is_online(ca)) { in __bch2_dev_attach_bdev()
1335 bch_err(ca, "already have device online in slot %u", in __bch2_dev_attach_bdev()
1341 ca->mi.bucket_size * ca->mi.nbuckets) { in __bch2_dev_attach_bdev()
1342 bch_err(ca, "cannot online: device too small"); in __bch2_dev_attach_bdev()
1346 BUG_ON(!percpu_ref_is_zero(&ca->io_ref)); in __bch2_dev_attach_bdev()
1348 ret = bch2_dev_journal_init(ca, sb->sb); in __bch2_dev_attach_bdev()
1353 ca->disk_sb = *sb; in __bch2_dev_attach_bdev()
1356 ca->dev = ca->disk_sb.bdev->bd_dev; in __bch2_dev_attach_bdev()
1358 percpu_ref_reinit(&ca->io_ref); in __bch2_dev_attach_bdev()
1365 struct bch_dev *ca; in bch2_dev_attach_bdev() local
1377 ca = bch_dev_locked(c, sb->sb->dev_idx); in bch2_dev_attach_bdev()
1379 ret = __bch2_dev_attach_bdev(ca, sb); in bch2_dev_attach_bdev()
1383 bch2_dev_sysfs_online(c, ca); in bch2_dev_attach_bdev()
1386 prt_bdevname(&name, ca->disk_sb.bdev); in bch2_dev_attach_bdev()
1390 strscpy(ca->name, name.buf, sizeof(ca->name)); in bch2_dev_attach_bdev()
1409 bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca, in bch2_dev_state_allowed() argument
1421 if (ca->mi.state != BCH_MEMBER_STATE_rw) in bch2_dev_state_allowed()
1426 if (ca2 != ca) in bch2_dev_state_allowed()
1439 if (ca->mi.state != BCH_MEMBER_STATE_rw && in bch2_dev_state_allowed()
1440 ca->mi.state != BCH_MEMBER_STATE_ro) in bch2_dev_state_allowed()
1445 __clear_bit(ca->dev_idx, new_online_devs.d); in bch2_dev_state_allowed()
1455 struct bch_dev *ca; in bch2_fs_may_start() local
1472 ca = bch_dev_locked(c, i); in bch2_fs_may_start()
1474 if (!bch2_dev_is_online(ca) && in bch2_fs_may_start()
1475 (ca->mi.state == BCH_MEMBER_STATE_rw || in bch2_fs_may_start()
1476 ca->mi.state == BCH_MEMBER_STATE_ro)) { in bch2_fs_may_start()
1487 static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca) in __bch2_dev_read_only() argument
1492 bch2_dev_allocator_remove(c, ca); in __bch2_dev_read_only()
1493 bch2_dev_journal_stop(&c->journal, ca); in __bch2_dev_read_only()
1496 static void __bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca) in __bch2_dev_read_write() argument
1500 BUG_ON(ca->mi.state != BCH_MEMBER_STATE_rw); in __bch2_dev_read_write()
1502 bch2_dev_allocator_add(c, ca); in __bch2_dev_read_write()
1506 int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, in __bch2_dev_set_state() argument
1512 if (ca->mi.state == new_state) in __bch2_dev_set_state()
1515 if (!bch2_dev_state_allowed(c, ca, new_state, flags)) in __bch2_dev_set_state()
1519 __bch2_dev_read_only(c, ca); in __bch2_dev_set_state()
1521 bch_notice(ca, "%s", bch2_member_states[new_state]); in __bch2_dev_set_state()
1524 m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); in __bch2_dev_set_state()
1530 __bch2_dev_read_write(c, ca); in __bch2_dev_set_state()
1537 int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, in bch2_dev_set_state() argument
1543 ret = __bch2_dev_set_state(c, ca, new_state, flags); in bch2_dev_set_state()
1551 static int bch2_dev_remove_alloc(struct bch_fs *c, struct bch_dev *ca) in bch2_dev_remove_alloc() argument
1553 struct bpos start = POS(ca->dev_idx, 0); in bch2_dev_remove_alloc()
1554 struct bpos end = POS(ca->dev_idx, U64_MAX); in bch2_dev_remove_alloc()
1577 int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags) in bch2_dev_remove() argument
1580 unsigned dev_idx = ca->dev_idx, data; in bch2_dev_remove()
1586 * We consume a reference to ca->ref, regardless of whether we succeed in bch2_dev_remove()
1589 percpu_ref_put(&ca->ref); in bch2_dev_remove()
1591 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { in bch2_dev_remove()
1592 bch_err(ca, "Cannot remove without losing data"); in bch2_dev_remove()
1597 __bch2_dev_read_only(c, ca); in bch2_dev_remove()
1599 ret = bch2_dev_data_drop(c, ca->dev_idx, flags); in bch2_dev_remove()
1600 bch_err_msg(ca, ret, "dropping data"); in bch2_dev_remove()
1604 ret = bch2_dev_remove_alloc(c, ca); in bch2_dev_remove()
1605 bch_err_msg(ca, ret, "deleting alloc info"); in bch2_dev_remove()
1609 ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx); in bch2_dev_remove()
1610 bch_err_msg(ca, ret, "flushing journal"); in bch2_dev_remove()
1615 bch_err(ca, "journal error"); in bch2_dev_remove()
1620 bch_err_msg(ca, ret, "in replicas_gc2()"); in bch2_dev_remove()
1624 data = bch2_dev_has_data(c, ca); in bch2_dev_remove()
1629 bch_err(ca, "Remove failed, still has data (%s)", data_has.buf); in bch2_dev_remove()
1635 __bch2_dev_offline(c, ca); in bch2_dev_remove()
1638 rcu_assign_pointer(c->devs[ca->dev_idx], NULL); in bch2_dev_remove()
1641 percpu_ref_kill(&ca->ref); in bch2_dev_remove()
1642 wait_for_completion(&ca->ref_completion); in bch2_dev_remove()
1644 bch2_dev_free(ca); in bch2_dev_remove()
1673 if (ca->mi.state == BCH_MEMBER_STATE_rw && in bch2_dev_remove()
1674 !percpu_ref_is_zero(&ca->io_ref)) in bch2_dev_remove()
1675 __bch2_dev_read_write(c, ca); in bch2_dev_remove()
1685 struct bch_dev *ca = NULL; in bch2_dev_add() local
1712 ca = __bch2_dev_alloc(c, &dev_mi); in bch2_dev_add()
1713 if (!ca) { in bch2_dev_add()
1718 bch2_dev_usage_init(ca); in bch2_dev_add()
1720 ret = __bch2_dev_attach_bdev(ca, &sb); in bch2_dev_add()
1724 ret = bch2_dev_journal_alloc(ca); in bch2_dev_add()
1732 ret = bch2_sb_from_fs(c, ca); in bch2_dev_add()
1769 ca->disk_sb.sb->dev_idx = dev_idx; in bch2_dev_add()
1770 bch2_dev_attach(c, ca, dev_idx); in bch2_dev_add()
1773 ret = __bch2_dev_group_set(c, ca, label.buf); in bch2_dev_add()
1784 ret = bch2_trans_mark_dev_sb(c, ca); in bch2_dev_add()
1785 bch_err_msg(ca, ret, "marking new superblock"); in bch2_dev_add()
1790 bch_err_msg(ca, ret, "initializing free space"); in bch2_dev_add()
1794 ca->new_fs_bucket_idx = 0; in bch2_dev_add()
1796 if (ca->mi.state == BCH_MEMBER_STATE_rw) in bch2_dev_add()
1797 __bch2_dev_read_write(c, ca); in bch2_dev_add()
1806 if (ca) in bch2_dev_add()
1807 bch2_dev_free(ca); in bch2_dev_add()
1815 ca = NULL; in bch2_dev_add()
1824 struct bch_dev *ca; in bch2_dev_online() local
1847 ca = bch_dev_locked(c, dev_idx); in bch2_dev_online()
1849 ret = bch2_trans_mark_dev_sb(c, ca); in bch2_dev_online()
1854 if (ca->mi.state == BCH_MEMBER_STATE_rw) in bch2_dev_online()
1855 __bch2_dev_read_write(c, ca); in bch2_dev_online()
1857 if (!ca->mi.freespace_initialized) { in bch2_dev_online()
1858 ret = bch2_dev_freespace_init(c, ca, 0, ca->mi.nbuckets); in bch2_dev_online()
1859 bch_err_msg(ca, ret, "initializing free space"); in bch2_dev_online()
1864 if (!ca->journal.nr) { in bch2_dev_online()
1865 ret = bch2_dev_journal_alloc(ca); in bch2_dev_online()
1866 bch_err_msg(ca, ret, "allocating journal"); in bch2_dev_online()
1872 bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx)->last_mount = in bch2_dev_online()
1885 int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags) in bch2_dev_offline() argument
1889 if (!bch2_dev_is_online(ca)) { in bch2_dev_offline()
1890 bch_err(ca, "Already offline"); in bch2_dev_offline()
1895 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { in bch2_dev_offline()
1896 bch_err(ca, "Cannot offline required disk"); in bch2_dev_offline()
1901 __bch2_dev_offline(c, ca); in bch2_dev_offline()
1907 int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets) in bch2_dev_resize() argument
1914 old_nbuckets = ca->mi.nbuckets; in bch2_dev_resize()
1916 if (nbuckets < ca->mi.nbuckets) { in bch2_dev_resize()
1917 bch_err(ca, "Cannot shrink yet"); in bch2_dev_resize()
1922 if (bch2_dev_is_online(ca) && in bch2_dev_resize()
1923 get_capacity(ca->disk_sb.bdev->bd_disk) < in bch2_dev_resize()
1924 ca->mi.bucket_size * nbuckets) { in bch2_dev_resize()
1925 bch_err(ca, "New size larger than device"); in bch2_dev_resize()
1930 ret = bch2_dev_buckets_resize(c, ca, nbuckets); in bch2_dev_resize()
1931 bch_err_msg(ca, ret, "resizing buckets"); in bch2_dev_resize()
1935 ret = bch2_trans_mark_dev_sb(c, ca); in bch2_dev_resize()
1940 m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); in bch2_dev_resize()
1946 if (ca->mi.freespace_initialized) { in bch2_dev_resize()
1947 ret = bch2_dev_freespace_init(c, ca, old_nbuckets, nbuckets); in bch2_dev_resize()
1955 ca->usage_base->d[BCH_DATA_free].buckets += nbuckets - old_nbuckets; in bch2_dev_resize()
1964 /* return with ref on ca->ref: */
1968 for_each_member_device_rcu(c, ca, NULL) in bch2_dev_lookup()
1969 if (!strcmp(name, ca->name)) { in bch2_dev_lookup()
1971 return ca; in bch2_dev_lookup()