btrfs: add helper to describe block group flags
authorAnand Jain <anand.jain@oracle.com>
Tue, 20 Nov 2018 08:12:55 +0000 (16:12 +0800)
committerDavid Sterba <dsterba@suse.com>
Mon, 17 Dec 2018 13:51:39 +0000 (14:51 +0100)
Factor out helper that describes block group flags from
describe_relocation. The result will not be longer than the given size.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Reviewed-by: David Sterba <dsterba@suse.com>
[ add comments ]
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/relocation.c
fs/btrfs/volumes.c
fs/btrfs/volumes.h

index 5471eea1c0fd7ebf97b43f7f692f76ef12756553..35bfe014712ab197815272d4533efbdd1100803d 100644 (file)
@@ -4185,37 +4185,13 @@ static struct reloc_control *alloc_reloc_control(void)
 static void describe_relocation(struct btrfs_fs_info *fs_info,
                                struct btrfs_block_group_cache *block_group)
 {
-       char buf[128];          /* prefixed by a '|' that'll be dropped */
-       u64 flags = block_group->flags;
+       char buf[128] = {'\0'};
 
-       /* Shouldn't happen */
-       if (!flags) {
-               strcpy(buf, "|NONE");
-       } else {
-               char *bp = buf;
-
-#define DESCRIBE_FLAG(f, d) \
-               if (flags & BTRFS_BLOCK_GROUP_##f) { \
-                       bp += snprintf(bp, buf - bp + sizeof(buf), "|%s", d); \
-                       flags &= ~BTRFS_BLOCK_GROUP_##f; \
-               }
-               DESCRIBE_FLAG(DATA,     "data");
-               DESCRIBE_FLAG(SYSTEM,   "system");
-               DESCRIBE_FLAG(METADATA, "metadata");
-               DESCRIBE_FLAG(RAID0,    "raid0");
-               DESCRIBE_FLAG(RAID1,    "raid1");
-               DESCRIBE_FLAG(DUP,      "dup");
-               DESCRIBE_FLAG(RAID10,   "raid10");
-               DESCRIBE_FLAG(RAID5,    "raid5");
-               DESCRIBE_FLAG(RAID6,    "raid6");
-               if (flags)
-                       snprintf(bp, buf - bp + sizeof(buf), "|0x%llx", flags);
-#undef DESCRIBE_FLAG
-       }
+       btrfs_describe_block_groups(block_group->flags, buf, sizeof(buf));
 
        btrfs_info(fs_info,
                   "relocating block group %llu flags %s",
-                  block_group->key.objectid, buf + 1);
+                  block_group->key.objectid, buf);
 }
 
 /*
index 2ebc827bed88ffc5c9eb08d2243654dded09fe43..bb38b86b837441a33a70edd286271c7723d3febe 100644 (file)
@@ -130,6 +130,60 @@ const char *get_raid_name(enum btrfs_raid_types type)
        return btrfs_raid_array[type].raid_name;
 }
 
+/*
+ * Fill @buf with textual description of @bg_flags, no more than @size_buf
+ * bytes including terminating null byte.
+ */
+void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf)
+{
+       int i;
+       int ret;
+       char *bp = buf;
+       u64 flags = bg_flags;
+       u32 size_bp = size_buf;
+
+       if (!flags) {
+               strcpy(bp, "NONE");
+               return;
+       }
+
+#define DESCRIBE_FLAG(flag, desc)                                              \
+       do {                                                            \
+               if (flags & (flag)) {                                   \
+                       ret = snprintf(bp, size_bp, "%s|", (desc));     \
+                       if (ret < 0 || ret >= size_bp)                  \
+                               goto out_overflow;                      \
+                       size_bp -= ret;                                 \
+                       bp += ret;                                      \
+                       flags &= ~(flag);                               \
+               }                                                       \
+       } while (0)
+
+       DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA, "data");
+       DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM, "system");
+       DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA, "metadata");
+
+       DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE, "single");
+       for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
+               DESCRIBE_FLAG(btrfs_raid_array[i].bg_flag,
+                             btrfs_raid_array[i].raid_name);
+#undef DESCRIBE_FLAG
+
+       if (flags) {
+               ret = snprintf(bp, size_bp, "0x%llx|", flags);
+               size_bp -= ret;
+       }
+
+       if (size_bp < size_buf)
+               buf[size_buf - size_bp - 1] = '\0'; /* remove last | */
+
+       /*
+        * The text is trimmed, it's up to the caller to provide sufficiently
+        * large buffer
+        */
+out_overflow:;
+}
+
 static int init_first_rw_device(struct btrfs_trans_handle *trans,
                                struct btrfs_fs_info *fs_info);
 static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
index 1d936ce282c3537d8ac33f45dd7cdda234b0476c..8b092bb1e2ee9efda7c720c346456efa24bdc77d 100644 (file)
@@ -438,6 +438,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *path);
 int btrfs_balance(struct btrfs_fs_info *fs_info,
                  struct btrfs_balance_control *bctl,
                  struct btrfs_ioctl_balance_args *bargs);
+void btrfs_describe_block_groups(u64 flags, char *buf, u32 size_buf);
 int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info);
 int btrfs_recover_balance(struct btrfs_fs_info *fs_info);
 int btrfs_pause_balance(struct btrfs_fs_info *fs_info);