]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/disk_groups.c
Update bcachefs sources to 0d63ed13ea3d closures: Fix race in closure_sync()
[bcachefs-tools-debian] / libbcachefs / disk_groups.c
index de14ca3a9895ddc2d756776252fde5dd0480fd03..d613695abf9f67c2e9f2ab4ce91d863bdfd743c7 100644 (file)
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 #include "bcachefs.h"
 #include "disk_groups.h"
+#include "sb-members.h"
 #include "super-io.h"
 
 #include <linux/sort.h>
@@ -24,28 +25,27 @@ static int bch2_sb_disk_groups_validate(struct bch_sb *sb,
        struct bch_sb_field_disk_groups *groups =
                field_to_type(f, disk_groups);
        struct bch_disk_group *g, *sorted = NULL;
-       struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
        unsigned nr_groups = disk_groups_nr(groups);
        unsigned i, len;
        int ret = 0;
 
        for (i = 0; i < sb->nr_devices; i++) {
-               struct bch_member *m = mi->members + i;
-               unsigned g;
+               struct bch_member m = bch2_sb_member_get(sb, i);
+               unsigned group_id;
 
-               if (!BCH_MEMBER_GROUP(m))
+               if (!BCH_MEMBER_GROUP(&m))
                        continue;
 
-               g = BCH_MEMBER_GROUP(m) - 1;
+               group_id = BCH_MEMBER_GROUP(&m) - 1;
 
-               if (g >= nr_groups) {
+               if (group_id >= nr_groups) {
                        prt_printf(err, "disk %u has invalid label %u (have %u)",
-                              i, g, nr_groups);
+                                  i, group_id, nr_groups);
                        return -BCH_ERR_invalid_sb_disk_groups;
                }
 
-               if (BCH_GROUP_DELETED(&groups->entries[g])) {
-                       prt_printf(err, "disk %u has deleted label %u", i, g);
+               if (BCH_GROUP_DELETED(&groups->entries[group_id])) {
+                       prt_printf(err, "disk %u has deleted label %u", i, group_id);
                        return -BCH_ERR_invalid_sb_disk_groups;
                }
        }
@@ -151,22 +151,19 @@ const struct bch_sb_field_ops bch_sb_field_ops_disk_groups = {
 
 int bch2_sb_disk_groups_to_cpu(struct bch_fs *c)
 {
-       struct bch_sb_field_members *mi;
        struct bch_sb_field_disk_groups *groups;
        struct bch_disk_groups_cpu *cpu_g, *old_g;
        unsigned i, g, nr_groups;
 
        lockdep_assert_held(&c->sb_lock);
 
-       mi              = bch2_sb_get_members(c->disk_sb.sb);
-       groups          = bch2_sb_get_disk_groups(c->disk_sb.sb);
+       groups          = bch2_sb_field_get(c->disk_sb.sb, disk_groups);
        nr_groups       = disk_groups_nr(groups);
 
        if (!groups)
                return 0;
 
-       cpu_g = kzalloc(sizeof(*cpu_g) +
-                       sizeof(cpu_g->entries[0]) * nr_groups, GFP_KERNEL);
+       cpu_g = kzalloc(struct_size(cpu_g, entries, nr_groups), GFP_KERNEL);
        if (!cpu_g)
                return -BCH_ERR_ENOMEM_disk_groups_to_cpu;
 
@@ -178,17 +175,17 @@ int bch2_sb_disk_groups_to_cpu(struct bch_fs *c)
 
                dst->deleted    = BCH_GROUP_DELETED(src);
                dst->parent     = BCH_GROUP_PARENT(src);
+               memcpy(dst->label, src->label, sizeof(dst->label));
        }
 
        for (i = 0; i < c->disk_sb.sb->nr_devices; i++) {
-               struct bch_member *m = mi->members + i;
-               struct bch_disk_group_cpu *dst =
-                       &cpu_g->entries[BCH_MEMBER_GROUP(m)];
+               struct bch_member m = bch2_sb_member_get(c->disk_sb.sb, i);
+               struct bch_disk_group_cpu *dst;
 
-               if (!bch2_member_exists(m))
+               if (!bch2_member_exists(&m))
                        continue;
 
-               g = BCH_MEMBER_GROUP(m);
+               g = BCH_MEMBER_GROUP(&m);
                while (g) {
                        dst = &cpu_g->entries[g - 1];
                        __set_bit(i, dst->devs.d);
@@ -299,7 +296,7 @@ static int __bch2_disk_group_add(struct bch_sb_handle *sb, unsigned parent,
                                 const char *name, unsigned namelen)
 {
        struct bch_sb_field_disk_groups *groups =
-               bch2_sb_get_disk_groups(sb->sb);
+               bch2_sb_field_get(sb->sb, disk_groups);
        unsigned i, nr_groups = disk_groups_nr(groups);
        struct bch_disk_group *g;
 
@@ -317,7 +314,7 @@ static int __bch2_disk_group_add(struct bch_sb_handle *sb, unsigned parent,
                         sizeof(struct bch_disk_group) * (nr_groups + 1)) /
                        sizeof(u64);
 
-               groups = bch2_sb_resize_disk_groups(sb, u64s);
+               groups = bch2_sb_field_resize(sb, disk_groups, u64s);
                if (!groups)
                        return -BCH_ERR_ENOSPC_disk_label_add;
 
@@ -341,7 +338,7 @@ static int __bch2_disk_group_add(struct bch_sb_handle *sb, unsigned parent,
 int bch2_disk_path_find(struct bch_sb_handle *sb, const char *name)
 {
        struct bch_sb_field_disk_groups *groups =
-               bch2_sb_get_disk_groups(sb->sb);
+               bch2_sb_field_get(sb->sb, disk_groups);
        int v = -1;
 
        do {
@@ -371,7 +368,7 @@ int bch2_disk_path_find_or_create(struct bch_sb_handle *sb, const char *name)
                if (*next == '.')
                        next++;
 
-               groups = bch2_sb_get_disk_groups(sb->sb);
+               groups = bch2_sb_field_get(sb->sb, disk_groups);
 
                v = __bch2_disk_group_find(groups, parent, name, len);
                if (v < 0)
@@ -386,10 +383,60 @@ int bch2_disk_path_find_or_create(struct bch_sb_handle *sb, const char *name)
        return v;
 }
 
-void bch2_disk_path_to_text(struct printbuf *out, struct bch_sb *sb, unsigned v)
+void bch2_disk_path_to_text(struct printbuf *out, struct bch_fs *c, unsigned v)
+{
+       struct bch_disk_groups_cpu *groups;
+       struct bch_disk_group_cpu *g;
+       unsigned nr = 0;
+       u16 path[32];
+
+       out->atomic++;
+       rcu_read_lock();
+       groups = rcu_dereference(c->disk_groups);
+       if (!groups)
+               goto invalid;
+
+       while (1) {
+               if (nr == ARRAY_SIZE(path))
+                       goto invalid;
+
+               if (v >= groups->nr)
+                       goto invalid;
+
+               g = groups->entries + v;
+
+               if (g->deleted)
+                       goto invalid;
+
+               path[nr++] = v;
+
+               if (!g->parent)
+                       break;
+
+               v = g->parent - 1;
+       }
+
+       while (nr) {
+               v = path[--nr];
+               g = groups->entries + v;
+
+               prt_printf(out, "%.*s", (int) sizeof(g->label), g->label);
+               if (nr)
+                       prt_printf(out, ".");
+       }
+out:
+       rcu_read_unlock();
+       out->atomic--;
+       return;
+invalid:
+       prt_printf(out, "invalid label %u", v);
+       goto out;
+}
+
+void bch2_disk_path_to_text_sb(struct printbuf *out, struct bch_sb *sb, unsigned v)
 {
        struct bch_sb_field_disk_groups *groups =
-               bch2_sb_get_disk_groups(sb);
+               bch2_sb_field_get(sb, disk_groups);
        struct bch_disk_group *g;
        unsigned nr = 0;
        u16 path[32];
@@ -443,7 +490,7 @@ int __bch2_dev_group_set(struct bch_fs *c, struct bch_dev *ca, const char *name)
        if (ret)
                return ret;
 
-       mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
+       mi = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx);
        SET_BCH_MEMBER_GROUP(mi, v + 1);
        return 0;
 }
@@ -497,10 +544,7 @@ int bch2_opt_target_parse(struct bch_fs *c, const char *val, u64 *res,
        return -EINVAL;
 }
 
-void bch2_opt_target_to_text(struct printbuf *out,
-                            struct bch_fs *c,
-                            struct bch_sb *sb,
-                            u64 v)
+void bch2_target_to_text(struct printbuf *out, struct bch_fs *c, unsigned v)
 {
        struct target t = target_decode(v);
 
@@ -508,48 +552,69 @@ void bch2_opt_target_to_text(struct printbuf *out,
        case TARGET_NULL:
                prt_printf(out, "none");
                break;
-       case TARGET_DEV:
-               if (c) {
-                       struct bch_dev *ca;
-
-                       rcu_read_lock();
-                       ca = t.dev < c->sb.nr_devices
-                               ? rcu_dereference(c->devs[t.dev])
-                               : NULL;
-
-                       if (ca && percpu_ref_tryget(&ca->io_ref)) {
-                               prt_printf(out, "/dev/%pg", ca->disk_sb.bdev);
-                               percpu_ref_put(&ca->io_ref);
-                       } else if (ca) {
-                               prt_printf(out, "offline device %u", t.dev);
-                       } else {
-                               prt_printf(out, "invalid device %u", t.dev);
-                       }
-
-                       rcu_read_unlock();
+       case TARGET_DEV: {
+               struct bch_dev *ca;
+
+               rcu_read_lock();
+               ca = t.dev < c->sb.nr_devices
+                       ? rcu_dereference(c->devs[t.dev])
+                       : NULL;
+
+               if (ca && percpu_ref_tryget(&ca->io_ref)) {
+                       prt_printf(out, "/dev/%pg", ca->disk_sb.bdev);
+                       percpu_ref_put(&ca->io_ref);
+               } else if (ca) {
+                       prt_printf(out, "offline device %u", t.dev);
                } else {
-                       struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
-                       struct bch_member *m = mi->members + t.dev;
-
-                       if (bch2_dev_exists(sb, mi, t.dev)) {
-                               prt_printf(out, "Device ");
-                               pr_uuid(out, m->uuid.b);
-                               prt_printf(out, " (%u)", t.dev);
-                       } else {
-                               prt_printf(out, "Bad device %u", t.dev);
-                       }
+                       prt_printf(out, "invalid device %u", t.dev);
                }
+
+               rcu_read_unlock();
                break;
+       }
        case TARGET_GROUP:
-               if (c) {
-                       mutex_lock(&c->sb_lock);
-                       bch2_disk_path_to_text(out, c->disk_sb.sb, t.group);
-                       mutex_unlock(&c->sb_lock);
+               bch2_disk_path_to_text(out, c, t.group);
+               break;
+       default:
+               BUG();
+       }
+}
+
+void bch2_target_to_text_sb(struct printbuf *out, struct bch_sb *sb, unsigned v)
+{
+       struct target t = target_decode(v);
+
+       switch (t.type) {
+       case TARGET_NULL:
+               prt_printf(out, "none");
+               break;
+       case TARGET_DEV: {
+               struct bch_member m = bch2_sb_member_get(sb, t.dev);
+
+               if (bch2_dev_exists(sb, t.dev)) {
+                       prt_printf(out, "Device ");
+                       pr_uuid(out, m.uuid.b);
+                       prt_printf(out, " (%u)", t.dev);
                } else {
-                       bch2_disk_path_to_text(out, sb, t.group);
+                       prt_printf(out, "Bad device %u", t.dev);
                }
                break;
+       }
+       case TARGET_GROUP:
+               bch2_disk_path_to_text_sb(out, sb, t.group);
+               break;
        default:
                BUG();
        }
 }
+
+void bch2_opt_target_to_text(struct printbuf *out,
+                            struct bch_fs *c,
+                            struct bch_sb *sb,
+                            u64 v)
+{
+       if (c)
+               bch2_target_to_text(out, c, v);
+       else
+               bch2_target_to_text_sb(out, sb, v);
+}