ext4: fix reserved gdt blocks handling in fsmap
authorOjaswin Mujoo <ojaswin@linux.ibm.com>
Tue, 5 Aug 2025 08:30:31 +0000 (14:00 +0530)
committerTheodore Ts'o <tytso@mit.edu>
Wed, 13 Aug 2025 03:15:05 +0000 (23:15 -0400)
In some cases like small FSes with no meta_bg and where the resize
doesn't need extra gdt blocks as it can fit in the current one,
s_reserved_gdt_blocks is set as 0, which causes fsmap to emit a 0
length entry, which is incorrect.

  $ mkfs.ext4 -b 65536 -O bigalloc /dev/sda 5G
  $ mount /dev/sda /mnt/scratch
  $ xfs_io -c "fsmap -d" /mnt/scartch

        0: 253:48 [0..127]: static fs metadata 128
        1: 253:48 [128..255]: special 102:1 128
        2: 253:48 [256..255]: special 102:2 0     <---- 0 len entry
        3: 253:48 [256..383]: special 102:3 128

Fix this by adding a check for this case.

Cc: stable@kernel.org
Fixes: 0c9ec4beecac ("ext4: support GETFSMAP ioctls")
Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Link: https://patch.msgid.link/08781b796453a5770112aa96ad14c864fbf31935.1754377641.git.ojaswin@linux.ibm.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
fs/ext4/fsmap.c

index 9d63c39f6077b7a7eb851933bcf6a3fcdd6370ac..91185c40f755a57ced16fb18da9b2788b9ae8936 100644 (file)
@@ -393,6 +393,14 @@ static unsigned int ext4_getfsmap_find_sb(struct super_block *sb,
        /* Reserved GDT blocks */
        if (!ext4_has_feature_meta_bg(sb) || metagroup < first_meta_bg) {
                len = le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks);
+
+               /*
+                * mkfs.ext4 can set s_reserved_gdt_blocks as 0 in some cases,
+                * check for that.
+                */
+               if (!len)
+                       return 0;
+
                error = ext4_getfsmap_fill(meta_list, fsb, len,
                                           EXT4_FMR_OWN_RESV_GDT);
                if (error)