Skip to content

Commit dc5b1d8

Browse files
adam900710kdave
authored andcommitted
btrfs: simplify support block size check
Currently we manually check the block size against 3 different values: - 4K - PAGE_SIZE - MIN_BLOCKSIZE Those 3 values can match or differ from each other. This makes it pretty complex to output the supported block sizes. Considering we're going to add block size > page size support soon, this can make the support block size sysfs attribute much harder to implement. To make it easier, factor out a helper, btrfs_supported_blocksize() to do a simple check for the block size. Then utilize it in the two locations: - btrfs_validate_super() This is very straightforward - supported_sectorsizes_show() Iterate through all valid block sizes, and only output supported ones. This is to make future full range block sizes support much easier. Reviewed-by: Anand Jain <[email protected]> Signed-off-by: Qu Wenruo <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 46e5a72 commit dc5b1d8

File tree

4 files changed

+41
-21
lines changed

4 files changed

+41
-21
lines changed

fs/btrfs/disk-io.c

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,21 +2438,7 @@ int btrfs_validate_super(const struct btrfs_fs_info *fs_info,
24382438
ret = -EINVAL;
24392439
}
24402440

2441-
/*
2442-
* We only support at most 3 sectorsizes: 4K, PAGE_SIZE, MIN_BLOCKSIZE.
2443-
*
2444-
* For 4K page sized systems with non-debug builds, all 3 matches (4K).
2445-
* For 4K page sized systems with debug builds, there are two block sizes
2446-
* supported. (4K and 2K)
2447-
*
2448-
* We can support 16K sectorsize with 64K page size without problem,
2449-
* but such sectorsize/pagesize combination doesn't make much sense.
2450-
* 4K will be our future standard, PAGE_SIZE is supported from the very
2451-
* beginning.
2452-
*/
2453-
if (sectorsize > PAGE_SIZE || (sectorsize != SZ_4K &&
2454-
sectorsize != PAGE_SIZE &&
2455-
sectorsize != BTRFS_MIN_BLOCKSIZE)) {
2441+
if (!btrfs_supported_blocksize(sectorsize)) {
24562442
btrfs_err(fs_info,
24572443
"sectorsize %llu not yet supported for page size %lu",
24582444
sectorsize, PAGE_SIZE);

fs/btrfs/fs.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,33 @@ size_t __attribute_const__ btrfs_get_num_csums(void)
5454
return ARRAY_SIZE(btrfs_csums);
5555
}
5656

57+
/*
58+
* We support the following block sizes for all systems:
59+
*
60+
* - 4K
61+
* This is the most common block size. For PAGE SIZE > 4K cases the subage
62+
* mode is used.
63+
*
64+
* - PAGE_SIZE
65+
* The straightforward block size to support.
66+
*
67+
* And extra support for the following block sizes based on the kernel config:
68+
*
69+
* - MIN_BLOCKSIZE
70+
* This is either 4K (regular builds) or 2K (debug builds)
71+
* This allows testing subpage routines on x86_64.
72+
*/
73+
bool __attribute_const__ btrfs_supported_blocksize(u32 blocksize)
74+
{
75+
/* @blocksize should be validated first. */
76+
ASSERT(is_power_of_2(blocksize) && blocksize >= BTRFS_MIN_BLOCKSIZE &&
77+
blocksize <= BTRFS_MAX_BLOCKSIZE);
78+
79+
if (blocksize == PAGE_SIZE || blocksize == SZ_4K || blocksize == BTRFS_MIN_BLOCKSIZE)
80+
return true;
81+
return false;
82+
}
83+
5784
/*
5885
* Start exclusive operation @type, return true on success.
5986
*/

fs/btrfs/fs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ struct btrfs_space_info;
5959
#define BTRFS_MIN_BLOCKSIZE (SZ_4K)
6060
#endif
6161

62+
#define BTRFS_MAX_BLOCKSIZE (SZ_64K)
63+
6264
#define BTRFS_MAX_EXTENT_SIZE SZ_128M
6365

6466
#define BTRFS_OLDEST_GENERATION 0ULL
@@ -997,6 +999,7 @@ static inline unsigned int btrfs_blocks_per_folio(const struct btrfs_fs_info *fs
997999
return folio_size(folio) >> fs_info->sectorsize_bits;
9981000
}
9991001

1002+
bool __attribute_const__ btrfs_supported_blocksize(u32 blocksize);
10001003
bool btrfs_exclop_start(struct btrfs_fs_info *fs_info,
10011004
enum btrfs_exclusive_operation type);
10021005
bool btrfs_exclop_start_try_lock(struct btrfs_fs_info *fs_info,

fs/btrfs/sysfs.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,13 +409,17 @@ static ssize_t supported_sectorsizes_show(struct kobject *kobj,
409409
char *buf)
410410
{
411411
ssize_t ret = 0;
412+
bool has_output = false;
412413

413-
if (BTRFS_MIN_BLOCKSIZE != SZ_4K && BTRFS_MIN_BLOCKSIZE != PAGE_SIZE)
414-
ret += sysfs_emit_at(buf, ret, "%u ", BTRFS_MIN_BLOCKSIZE);
415-
if (PAGE_SIZE > SZ_4K)
416-
ret += sysfs_emit_at(buf, ret, "%u ", SZ_4K);
417-
ret += sysfs_emit_at(buf, ret, "%lu\n", PAGE_SIZE);
418-
414+
for (u32 cur = BTRFS_MIN_BLOCKSIZE; cur <= BTRFS_MAX_BLOCKSIZE; cur *= 2) {
415+
if (!btrfs_supported_blocksize(cur))
416+
continue;
417+
if (has_output)
418+
ret += sysfs_emit_at(buf, ret, " ");
419+
ret += sysfs_emit_at(buf, ret, "%u", cur);
420+
has_output = true;
421+
}
422+
ret += sysfs_emit_at(buf, ret, "\n");
419423
return ret;
420424
}
421425
BTRFS_ATTR(static_feature, supported_sectorsizes,

0 commit comments

Comments
 (0)