This started as debugging helper, to watch the effects of converting
between raid levels on multiple devices, but could be useful standalone.

In my case the usage filter was not finegrained enough and led to
converting too many chunks at once. Another example use is in connection
with drange+devid or vrange filters that allow to work with a specific
chunk or even with a chunk on a given device.

The limit filter applies last, the value of 0 means no limiting.

CC: Ilya Dryomov <idryo...@gmail.com>
CC: Hugo Mills <h...@carfax.org.uk>
Signed-off-by: David Sterba <dste...@suse.cz>
---

The name 'limit' should resebmle the meaning from SQL SELECT.

Though it may not be that useful on it's own, we can use it as a building block
for more complex filters.

 fs/btrfs/ctree.h           |    7 ++++++-
 fs/btrfs/volumes.c         |   18 ++++++++++++++++++
 fs/btrfs/volumes.h         |    1 +
 include/uapi/linux/btrfs.h |    3 ++-
 4 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index ba6b88528dc7..e6f899dc5e47 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -840,7 +840,10 @@ struct btrfs_disk_balance_args {
        /* BTRFS_BALANCE_ARGS_* */
        __le64 flags;
 
-       __le64 unused[8];
+       /* BTRFS_BALANCE_ARGS_LIMIT value */
+       __le64 limit;
+
+       __le64 unused[7];
 } __attribute__ ((__packed__));
 
 /*
@@ -2897,6 +2900,7 @@ btrfs_disk_balance_args_to_cpu(struct btrfs_balance_args 
*cpu,
        cpu->vend = le64_to_cpu(disk->vend);
        cpu->target = le64_to_cpu(disk->target);
        cpu->flags = le64_to_cpu(disk->flags);
+       cpu->limit = le64_to_cpu(disk->limit);
 }
 
 static inline void
@@ -2914,6 +2918,7 @@ btrfs_cpu_balance_args_to_disk(struct 
btrfs_disk_balance_args *disk,
        disk->vend = cpu_to_le64(cpu->vend);
        disk->target = cpu_to_le64(cpu->target);
        disk->flags = cpu_to_le64(cpu->flags);
+       disk->limit = cpu_to_le64(cpu->limit);
 }
 
 /* struct btrfs_super_block */
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 49d7fab73360..3b761a456acd 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -2922,6 +2922,16 @@ static int should_balance_chunk(struct btrfs_root *root,
                return 0;
        }
 
+       /*
+        * limited by count, must be the last filter
+        */
+       if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
+               if (bargs->limit == 0)
+                       return 0;
+               else
+                       bargs->limit--;
+       }
+
        return 1;
 }
 
@@ -2944,6 +2954,9 @@ static int __btrfs_balance(struct btrfs_fs_info *fs_info)
        int ret;
        int enospc_errors = 0;
        bool counting = true;
+       u64 limit_data = bctl->data.limit;
+       u64 limit_meta = bctl->meta.limit;
+       u64 limit_sys = bctl->sys.limit;
 
        /* step one make some room on all the devices */
        devices = &fs_info->fs_devices->devices;
@@ -2982,6 +2995,11 @@ static int __btrfs_balance(struct btrfs_fs_info *fs_info)
        memset(&bctl->stat, 0, sizeof(bctl->stat));
        spin_unlock(&fs_info->balance_lock);
 again:
+       if (!counting) {
+               bctl->data.limit = limit_data;
+               bctl->meta.limit = limit_meta;
+               bctl->sys.limit = limit_sys;
+       }
        key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
        key.offset = (u64)-1;
        key.type = BTRFS_CHUNK_ITEM_KEY;
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index 80754f9dd3df..1a15bbeb65e2 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -255,6 +255,7 @@ struct map_lookup {
 #define BTRFS_BALANCE_ARGS_DEVID       (1ULL << 2)
 #define BTRFS_BALANCE_ARGS_DRANGE      (1ULL << 3)
 #define BTRFS_BALANCE_ARGS_VRANGE      (1ULL << 4)
+#define BTRFS_BALANCE_ARGS_LIMIT       (1ULL << 5)
 
 /*
  * Profile changing flags.  When SOFT is set we won't relocate chunk if
diff --git a/include/uapi/linux/btrfs.h b/include/uapi/linux/btrfs.h
index b4d69092fbdb..901a3c563f60 100644
--- a/include/uapi/linux/btrfs.h
+++ b/include/uapi/linux/btrfs.h
@@ -211,7 +211,8 @@ struct btrfs_balance_args {
 
        __u64 flags;
 
-       __u64 unused[8];
+       __u64 limit;            /* limit number of processed chunks */
+       __u64 unused[7];
 } __attribute__ ((__packed__));
 
 /* report balance progress to userspace */
-- 
1.7.9

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to