Skip to content

Commit abae64c

Browse files
OjaswinMaloktiwa
authored andcommitted
ext4: correctly handle queries for metadata mappings
commit 46c22a8bb4cb03211da1100d7ee4a2005bf77c70 upstream. Currently, our handling of metadata is _ambiguous_ in some scenarios, that is, we end up returning unknown if the range only covers the mapping partially. For example, in the following case: $ xfs_io -c fsmap -d 0: 254:16 [0..7]: static fs metadata 8 1: 254:16 [8..15]: special 102:1 8 2: 254:16 [16..5127]: special 102:2 5112 3: 254:16 [5128..5255]: special 102:3 128 4: 254:16 [5256..5383]: special 102:4 128 5: 254:16 [5384..70919]: inodes 65536 6: 254:16 [70920..70967]: unknown 48 ... $ xfs_io -c fsmap -d 24 33 0: 254:16 [24..39]: unknown 16 <--- incomplete reporting $ xfs_io -c fsmap -d 24 33 (With patch) 0: 254:16 [16..5127]: special 102:2 5112 This is because earlier in ext4_getfsmap_meta_helper, we end up ignoring any extent that starts before our queried range, but overlaps it. While the man page [1] is a bit ambiguous on this, this fix makes the output make more sense since we are anyways returning an "unknown" extent. This is also consistent to how XFS does it: $ xfs_io -c fsmap -d ... 6: 254:16 [104..127]: free space 24 7: 254:16 [128..191]: inodes 64 ... $ xfs_io -c fsmap -d 137 150 0: 254:16 [128..191]: inodes 64 <-- full extent returned [1] https://man7.org/linux/man-pages/man2/ioctl_getfsmap.2.html Reported-by: Ritesh Harjani (IBM) <[email protected]> Cc: [email protected] Signed-off-by: Ojaswin Mujoo <[email protected]> Message-ID: <023f37e35ee280cd9baac0296cbadcbe10995cab.1757058211.git.ojaswin@linux.ibm.com> Signed-off-by: Theodore Ts'o <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> (cherry picked from commit 7a1286e5a8c07d6468fcfe4743b0ba43fd0d4e20) Signed-off-by: Alok Tiwari <[email protected]>
1 parent 173e1e5 commit abae64c

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

fs/ext4/fsmap.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ static int ext4_getfsmap_dev_compare(const void *p1, const void *p2)
7474
static bool ext4_getfsmap_rec_before_low_key(struct ext4_getfsmap_info *info,
7575
struct ext4_fsmap *rec)
7676
{
77-
return rec->fmr_physical < info->gfi_low.fmr_physical;
77+
return rec->fmr_physical + rec->fmr_length <=
78+
info->gfi_low.fmr_physical;
7879
}
7980

8081
/*
@@ -200,15 +201,18 @@ static int ext4_getfsmap_meta_helper(struct super_block *sb,
200201
ext4_group_first_block_no(sb, agno));
201202
fs_end = fs_start + EXT4_C2B(sbi, len);
202203

203-
/* Return relevant extents from the meta_list */
204+
/*
205+
* Return relevant extents from the meta_list. We emit all extents that
206+
* partially/fully overlap with the query range
207+
*/
204208
list_for_each_entry_safe(p, tmp, &info->gfi_meta_list, fmr_list) {
205-
if (p->fmr_physical < info->gfi_next_fsblk) {
209+
if (p->fmr_physical + p->fmr_length <= info->gfi_next_fsblk) {
206210
list_del(&p->fmr_list);
207211
kfree(p);
208212
continue;
209213
}
210-
if (p->fmr_physical <= fs_start ||
211-
p->fmr_physical + p->fmr_length <= fs_end) {
214+
if (p->fmr_physical <= fs_end &&
215+
p->fmr_physical + p->fmr_length > fs_start) {
212216
/* Emit the retained free extent record if present */
213217
if (info->gfi_lastfree.fmr_owner) {
214218
error = ext4_getfsmap_helper(sb, info,

0 commit comments

Comments
 (0)