btrfs: balance: print args during start and resume
[sfrench/cifs-2.6.git] / fs / btrfs / volumes.c
index a89596c3de56b0ba5e68521b10d412b7a50c3fdb..cdba4f03d465396c20693c2e954a5d8869dbe3b0 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);
@@ -3925,6 +3979,162 @@ static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
                 (bctl_arg->target & ~allowed)));
 }
 
+/*
+ * Fill @buf with textual description of balance filter flags @bargs, up to
+ * @size_buf including the terminating null. The output may be trimmed if it
+ * does not fit into the provided buffer.
+ */
+static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf,
+                                u32 size_buf)
+{
+       int ret;
+       u32 size_bp = size_buf;
+       char *bp = buf;
+       u64 flags = bargs->flags;
+       char tmp_buf[128] = {'\0'};
+
+       if (!flags)
+               return;
+
+#define CHECK_APPEND_NOARG(a)                                          \
+       do {                                                            \
+               ret = snprintf(bp, size_bp, (a));                       \
+               if (ret < 0 || ret >= size_bp)                          \
+                       goto out_overflow;                              \
+               size_bp -= ret;                                         \
+               bp += ret;                                              \
+       } while (0)
+
+#define CHECK_APPEND_1ARG(a, v1)                                       \
+       do {                                                            \
+               ret = snprintf(bp, size_bp, (a), (v1));                 \
+               if (ret < 0 || ret >= size_bp)                          \
+                       goto out_overflow;                              \
+               size_bp -= ret;                                         \
+               bp += ret;                                              \
+       } while (0)
+
+#define CHECK_APPEND_2ARG(a, v1, v2)                                   \
+       do {                                                            \
+               ret = snprintf(bp, size_bp, (a), (v1), (v2));           \
+               if (ret < 0 || ret >= size_bp)                          \
+                       goto out_overflow;                              \
+               size_bp -= ret;                                         \
+               bp += ret;                                              \
+       } while (0)
+
+       if (flags & BTRFS_BALANCE_ARGS_CONVERT) {
+               int index = btrfs_bg_flags_to_raid_index(bargs->target);
+
+               CHECK_APPEND_1ARG("convert=%s,", get_raid_name(index));
+       }
+
+       if (flags & BTRFS_BALANCE_ARGS_SOFT)
+               CHECK_APPEND_NOARG("soft,");
+
+       if (flags & BTRFS_BALANCE_ARGS_PROFILES) {
+               btrfs_describe_block_groups(bargs->profiles, tmp_buf,
+                                           sizeof(tmp_buf));
+               CHECK_APPEND_1ARG("profiles=%s,", tmp_buf);
+       }
+
+       if (flags & BTRFS_BALANCE_ARGS_USAGE)
+               CHECK_APPEND_1ARG("usage=%llu,", bargs->usage);
+
+       if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE)
+               CHECK_APPEND_2ARG("usage=%u..%u,",
+                                 bargs->usage_min, bargs->usage_max);
+
+       if (flags & BTRFS_BALANCE_ARGS_DEVID)
+               CHECK_APPEND_1ARG("devid=%llu,", bargs->devid);
+
+       if (flags & BTRFS_BALANCE_ARGS_DRANGE)
+               CHECK_APPEND_2ARG("drange=%llu..%llu,",
+                                 bargs->pstart, bargs->pend);
+
+       if (flags & BTRFS_BALANCE_ARGS_VRANGE)
+               CHECK_APPEND_2ARG("vrange=%llu..%llu,",
+                                 bargs->vstart, bargs->vend);
+
+       if (flags & BTRFS_BALANCE_ARGS_LIMIT)
+               CHECK_APPEND_1ARG("limit=%llu,", bargs->limit);
+
+       if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)
+               CHECK_APPEND_2ARG("limit=%u..%u,",
+                               bargs->limit_min, bargs->limit_max);
+
+       if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE)
+               CHECK_APPEND_2ARG("stripes=%u..%u,",
+                                 bargs->stripes_min, bargs->stripes_max);
+
+#undef CHECK_APPEND_2ARG
+#undef CHECK_APPEND_1ARG
+#undef CHECK_APPEND_NOARG
+
+out_overflow:
+
+       if (size_bp < size_buf)
+               buf[size_buf - size_bp - 1] = '\0'; /* remove last , */
+       else
+               buf[0] = '\0';
+}
+
+static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info)
+{
+       u32 size_buf = 1024;
+       char tmp_buf[192] = {'\0'};
+       char *buf;
+       char *bp;
+       u32 size_bp = size_buf;
+       int ret;
+       struct btrfs_balance_control *bctl = fs_info->balance_ctl;
+
+       buf = kzalloc(size_buf, GFP_KERNEL);
+       if (!buf)
+               return;
+
+       bp = buf;
+
+#define CHECK_APPEND_1ARG(a, v1)                                       \
+       do {                                                            \
+               ret = snprintf(bp, size_bp, (a), (v1));                 \
+               if (ret < 0 || ret >= size_bp)                          \
+                       goto out_overflow;                              \
+               size_bp -= ret;                                         \
+               bp += ret;                                              \
+       } while (0)
+
+       if (bctl->flags & BTRFS_BALANCE_FORCE)
+               CHECK_APPEND_1ARG("%s", "-f ");
+
+       if (bctl->flags & BTRFS_BALANCE_DATA) {
+               describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf));
+               CHECK_APPEND_1ARG("-d%s ", tmp_buf);
+       }
+
+       if (bctl->flags & BTRFS_BALANCE_METADATA) {
+               describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf));
+               CHECK_APPEND_1ARG("-m%s ", tmp_buf);
+       }
+
+       if (bctl->flags & BTRFS_BALANCE_SYSTEM) {
+               describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf));
+               CHECK_APPEND_1ARG("-s%s ", tmp_buf);
+       }
+
+#undef CHECK_APPEND_1ARG
+
+out_overflow:
+
+       if (size_bp < size_buf)
+               buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */
+       btrfs_info(fs_info, "balance: %s %s",
+                  (bctl->flags & BTRFS_BALANCE_RESUME) ?
+                  "resume" : "start", buf);
+
+       kfree(buf);
+}
+
 /*
  * Should be called with balance mutexe held
  */
@@ -3938,6 +4148,7 @@ int btrfs_balance(struct btrfs_fs_info *fs_info,
        int ret;
        u64 num_devices;
        unsigned seq;
+       bool reducing_integrity;
 
        if (btrfs_fs_closing(fs_info) ||
            atomic_read(&fs_info->balance_pause_req) ||
@@ -4017,24 +4228,30 @@ int btrfs_balance(struct btrfs_fs_info *fs_info,
                     !(bctl->sys.target & allowed)) ||
                    ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
                     (fs_info->avail_metadata_alloc_bits & allowed) &&
-                    !(bctl->meta.target & allowed))) {
-                       if (bctl->flags & BTRFS_BALANCE_FORCE) {
-                               btrfs_info(fs_info,
-                               "balance: force reducing metadata integrity");
-                       } else {
-                               btrfs_err(fs_info,
-       "balance: reduces metadata integrity, use --force if you want this");
-                               ret = -EINVAL;
-                               goto out;
-                       }
-               }
+                    !(bctl->meta.target & allowed)))
+                       reducing_integrity = true;
+               else
+                       reducing_integrity = false;
+
+               /* if we're not converting, the target field is uninitialized */
+               meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
+                       bctl->meta.target : fs_info->avail_metadata_alloc_bits;
+               data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
+                       bctl->data.target : fs_info->avail_data_alloc_bits;
        } while (read_seqretry(&fs_info->profiles_lock, seq));
 
-       /* if we're not converting, the target field is uninitialized */
-       meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
-               bctl->meta.target : fs_info->avail_metadata_alloc_bits;
-       data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
-               bctl->data.target : fs_info->avail_data_alloc_bits;
+       if (reducing_integrity) {
+               if (bctl->flags & BTRFS_BALANCE_FORCE) {
+                       btrfs_info(fs_info,
+                                  "balance: force reducing metadata integrity");
+               } else {
+                       btrfs_err(fs_info,
+         "balance: reduces metadata integrity, use --force if you want this");
+                       ret = -EINVAL;
+                       goto out;
+               }
+       }
+
        if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
                btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
                int meta_index = btrfs_bg_flags_to_raid_index(meta_target);
@@ -4064,6 +4281,7 @@ int btrfs_balance(struct btrfs_fs_info *fs_info,
 
        ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
        set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
+       describe_balance_start_or_resume(fs_info);
        mutex_unlock(&fs_info->balance_mutex);
 
        ret = __btrfs_balance(fs_info);
@@ -4101,10 +4319,8 @@ static int balance_kthread(void *data)
        int ret = 0;
 
        mutex_lock(&fs_info->balance_mutex);
-       if (fs_info->balance_ctl) {
-               btrfs_info(fs_info, "balance: resuming");
+       if (fs_info->balance_ctl)
                ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
-       }
        mutex_unlock(&fs_info->balance_mutex);
 
        return ret;