Skip to content

Commit 7d824b6

Browse files
kdavemasoncl
authored andcommitted
btrfs: balance filter: add limit of processed chunks
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 <idryomov@gmail.com> CC: Hugo Mills <hugo@carfax.org.uk> Signed-off-by: David Sterba <dsterba@suse.cz> Signed-off-by: Chris Mason <clm@fb.com>
1 parent fc19c5e commit 7d824b6

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

fs/btrfs/ctree.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,10 @@ struct btrfs_disk_balance_args {
840840
/* BTRFS_BALANCE_ARGS_* */
841841
__le64 flags;
842842

843-
__le64 unused[8];
843+
/* BTRFS_BALANCE_ARGS_LIMIT value */
844+
__le64 limit;
845+
846+
__le64 unused[7];
844847
} __attribute__ ((__packed__));
845848

846849
/*
@@ -2897,6 +2900,7 @@ btrfs_disk_balance_args_to_cpu(struct btrfs_balance_args *cpu,
28972900
cpu->vend = le64_to_cpu(disk->vend);
28982901
cpu->target = le64_to_cpu(disk->target);
28992902
cpu->flags = le64_to_cpu(disk->flags);
2903+
cpu->limit = le64_to_cpu(disk->limit);
29002904
}
29012905

29022906
static inline void
@@ -2914,6 +2918,7 @@ btrfs_cpu_balance_args_to_disk(struct btrfs_disk_balance_args *disk,
29142918
disk->vend = cpu_to_le64(cpu->vend);
29152919
disk->target = cpu_to_le64(cpu->target);
29162920
disk->flags = cpu_to_le64(cpu->flags);
2921+
disk->limit = cpu_to_le64(cpu->limit);
29172922
}
29182923

29192924
/* struct btrfs_super_block */

fs/btrfs/volumes.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2922,6 +2922,16 @@ static int should_balance_chunk(struct btrfs_root *root,
29222922
return 0;
29232923
}
29242924

2925+
/*
2926+
* limited by count, must be the last filter
2927+
*/
2928+
if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
2929+
if (bargs->limit == 0)
2930+
return 0;
2931+
else
2932+
bargs->limit--;
2933+
}
2934+
29252935
return 1;
29262936
}
29272937

@@ -2944,6 +2954,9 @@ static int __btrfs_balance(struct btrfs_fs_info *fs_info)
29442954
int ret;
29452955
int enospc_errors = 0;
29462956
bool counting = true;
2957+
u64 limit_data = bctl->data.limit;
2958+
u64 limit_meta = bctl->meta.limit;
2959+
u64 limit_sys = bctl->sys.limit;
29472960

29482961
/* step one make some room on all the devices */
29492962
devices = &fs_info->fs_devices->devices;
@@ -2982,6 +2995,11 @@ static int __btrfs_balance(struct btrfs_fs_info *fs_info)
29822995
memset(&bctl->stat, 0, sizeof(bctl->stat));
29832996
spin_unlock(&fs_info->balance_lock);
29842997
again:
2998+
if (!counting) {
2999+
bctl->data.limit = limit_data;
3000+
bctl->meta.limit = limit_meta;
3001+
bctl->sys.limit = limit_sys;
3002+
}
29853003
key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
29863004
key.offset = (u64)-1;
29873005
key.type = BTRFS_CHUNK_ITEM_KEY;

fs/btrfs/volumes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ struct map_lookup {
255255
#define BTRFS_BALANCE_ARGS_DEVID (1ULL << 2)
256256
#define BTRFS_BALANCE_ARGS_DRANGE (1ULL << 3)
257257
#define BTRFS_BALANCE_ARGS_VRANGE (1ULL << 4)
258+
#define BTRFS_BALANCE_ARGS_LIMIT (1ULL << 5)
258259

259260
/*
260261
* Profile changing flags. When SOFT is set we won't relocate chunk if

include/uapi/linux/btrfs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ struct btrfs_balance_args {
211211

212212
__u64 flags;
213213

214-
__u64 unused[8];
214+
__u64 limit; /* limit number of processed chunks */
215+
__u64 unused[7];
215216
} __attribute__ ((__packed__));
216217

217218
/* report balance progress to userspace */

0 commit comments

Comments
 (0)