bcachefs: Split apart bch2_target_to_text(), bch2_target_to_text_sb()
authorKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 14:54:24 +0000 (10:54 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Tue, 31 Oct 2023 16:18:37 +0000 (12:18 -0400)
Previously we just had bch2_opt_target_to_text() which could be passed
either a filesystem object or just a superblock - depending on if we
have a running filesystem or not.

Split these into two functions for clarity.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/disk_groups.c
fs/bcachefs/disk_groups.h

index e00133b6ea517be6a6544973532e0c299e7371b5..67a04fbbbbee484e33eafc067ef6902f2f5a89b5 100644 (file)
@@ -493,10 +493,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);
 
@@ -504,47 +501,71 @@ 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_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 {
-                               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);
+               mutex_lock(&c->sb_lock);
+               bch2_disk_path_to_text(out, c->disk_sb.sb, t.group);
+               mutex_unlock(&c->sb_lock);
+               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(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);
+}
index bd7711767fd4f95537fb2ed38d615fdf6aeec250..ab17dbaaf84274ba29a453e9a0b8a7223086de2b 100644 (file)
@@ -84,6 +84,7 @@ int bch2_disk_path_find(struct bch_sb_handle *, const char *);
 int bch2_disk_path_find_or_create(struct bch_sb_handle *, const char *);
 
 void bch2_disk_path_to_text(struct printbuf *, struct bch_sb *, unsigned);
+void bch2_target_to_text(struct printbuf *out, struct bch_fs *, unsigned);
 
 int bch2_opt_target_parse(struct bch_fs *, const char *, u64 *, struct printbuf *);
 void bch2_opt_target_to_text(struct printbuf *, struct bch_fs *, struct bch_sb *, u64);