Skip to content

Commit bc30946

Browse files
kdavemasoncl
authored andcommitted
btrfs: extend balance filter usage to take minimum and maximum
Similar to the 'limit' filter, we can enhance the 'usage' filter to accept a range. The change is backward compatible, the range is applied only in connection with the BTRFS_BALANCE_ARGS_USAGE_RANGE flag. We don't have a usecase yet, the current syntax has been sufficient. The enhancement should provide parity with other range-like filters. Signed-off-by: David Sterba <[email protected]> Signed-off-by: Chris Mason <[email protected]>
1 parent dee32d0 commit bc30946

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

fs/btrfs/ctree.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -823,8 +823,18 @@ struct btrfs_disk_balance_args {
823823
*/
824824
__le64 profiles;
825825

826-
/* usage filter */
827-
__le64 usage;
826+
/*
827+
* usage filter
828+
* BTRFS_BALANCE_ARGS_USAGE with a single value means '0..N'
829+
* BTRFS_BALANCE_ARGS_USAGE_RANGE - range syntax, min..max
830+
*/
831+
union {
832+
__le64 usage;
833+
struct {
834+
__le32 usage_min;
835+
__le32 usage_max;
836+
};
837+
};
828838

829839
/* devid filter */
830840
__le64 devid;

fs/btrfs/volumes.c

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3059,16 +3059,19 @@ static void update_balance_args(struct btrfs_balance_control *bctl)
30593059
* (albeit full) chunks.
30603060
*/
30613061
if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3062+
!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
30623063
!(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
30633064
bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
30643065
bctl->data.usage = 90;
30653066
}
30663067
if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3068+
!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
30673069
!(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
30683070
bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
30693071
bctl->sys.usage = 90;
30703072
}
30713073
if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3074+
!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
30723075
!(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
30733076
bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
30743077
bctl->meta.usage = 90;
@@ -3122,6 +3125,39 @@ static int chunk_profiles_filter(u64 chunk_type,
31223125

31233126
static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
31243127
struct btrfs_balance_args *bargs)
3128+
{
3129+
struct btrfs_block_group_cache *cache;
3130+
u64 chunk_used;
3131+
u64 user_thresh_min;
3132+
u64 user_thresh_max;
3133+
int ret = 1;
3134+
3135+
cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3136+
chunk_used = btrfs_block_group_used(&cache->item);
3137+
3138+
if (bargs->usage_min == 0)
3139+
user_thresh_min = 0;
3140+
else
3141+
user_thresh_min = div_factor_fine(cache->key.offset,
3142+
bargs->usage_min);
3143+
3144+
if (bargs->usage_max == 0)
3145+
user_thresh_max = 1;
3146+
else if (bargs->usage_max > 100)
3147+
user_thresh_max = cache->key.offset;
3148+
else
3149+
user_thresh_max = div_factor_fine(cache->key.offset,
3150+
bargs->usage_max);
3151+
3152+
if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3153+
ret = 0;
3154+
3155+
btrfs_put_block_group(cache);
3156+
return ret;
3157+
}
3158+
3159+
static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info,
3160+
u64 chunk_offset, struct btrfs_balance_args *bargs)
31253161
{
31263162
struct btrfs_block_group_cache *cache;
31273163
u64 chunk_used, user_thresh;
@@ -3130,7 +3166,7 @@ static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
31303166
cache = btrfs_lookup_block_group(fs_info, chunk_offset);
31313167
chunk_used = btrfs_block_group_used(&cache->item);
31323168

3133-
if (bargs->usage == 0)
3169+
if (bargs->usage_min == 0)
31343170
user_thresh = 1;
31353171
else if (bargs->usage > 100)
31363172
user_thresh = cache->key.offset;
@@ -3279,6 +3315,9 @@ static int should_balance_chunk(struct btrfs_root *root,
32793315
if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
32803316
chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
32813317
return 0;
3318+
} else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3319+
chunk_usage_range_filter(bctl->fs_info, chunk_offset, bargs)) {
3320+
return 0;
32823321
}
32833322

32843323
/* devid filter */

fs/btrfs/volumes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ struct map_lookup {
382382
#define BTRFS_BALANCE_ARGS_LIMIT (1ULL << 5)
383383
#define BTRFS_BALANCE_ARGS_LIMIT_RANGE (1ULL << 6)
384384
#define BTRFS_BALANCE_ARGS_STRIPES_RANGE (1ULL << 7)
385+
#define BTRFS_BALANCE_ARGS_USAGE_RANGE (1ULL << 8)
385386

386387
#define BTRFS_BALANCE_ARGS_MASK \
387388
(BTRFS_BALANCE_ARGS_PROFILES | \

include/uapi/linux/btrfs.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,13 @@ struct btrfs_ioctl_feature_flags {
206206
*/
207207
struct btrfs_balance_args {
208208
__u64 profiles;
209-
__u64 usage;
209+
union {
210+
__le64 usage;
211+
struct {
212+
__le32 usage_min;
213+
__le32 usage_max;
214+
};
215+
};
210216
__u64 devid;
211217
__u64 pstart;
212218
__u64 pend;

0 commit comments

Comments
 (0)