lseek in sparse files broken on ext3 mounted as ext4
Hi!
Mounting ext3 with '-o delalloc' is explicitly rejected by the kernel ("EXT4-fs: Mount option(s) incompatible with ext3" in dmesg).
At the same time, mounting ext3 with '-t ext4' is accepted, and enables delayed allocation. In this case, lseek with SEEK_DATA/SEEK_HOLE requests does not work correctly and breaks userspace programs such as install(1) from coreutils.
To reproduce, it is sufficient to prepare an ext3 image as usual and mount it as ext4:
truncate -s 1G img-ext3
mkfs.ext3 img-ext3
mkdir mnt-ext3
mount -t ext4 img-ext3 mnt-ext3
and run the following repro script:
#!/bin/sh
echo | dd of=src bs=1 count=1 seek=64K
strace -v -o install.strace install src dst
cmp src dst
the output should be
src dst differ: char 65537, line 1
with
lseek(3, 0, SEEK_DATA) = -1 ENXIO
in install.strace on the first lseek call, meaning that it reports "no more data until EOF" (and hence install does not copy anything).
Either mounting with '-o nodelalloc' or fdatasync'ing the file before install (conv=fdatasync in dd or 'sync -d src' after dd) avoids the problem.
The old ext4 wiki [1], the KernelNewbies wiki [2], and the Arch wiki [3] all claim that mounting ext3 as ext4 is expected to work correctly.
[1] https://archive.kernel.org/oldwiki/ext4.wiki.kernel.org/index.php/UpgradeToExt4.html
[2] https://kernelnewbies.org/Ext4
[3] https://wiki.archlinux.org/title/Ext4
Thanks.
Alexander
Quoted 39 lines · Alexander Monakov
Hi!
Mounting ext3 with '-o delalloc' is explicitly rejected by the kernel ("EXT4-fs: Mount option(s) incompatible with ext3" in dmesg).
At the same time, mounting ext3 with '-t ext4' is accepted, and enables delayed allocation. In this case, lseek with SEEK_DATA/SEEK_HOLE requests does not work correctly and breaks userspace programs such as install(1) from coreutils.
To reproduce, it is sufficient to prepare an ext3 image as usual and mount it as ext4:
truncate -s 1G img-ext3
mkfs.ext3 img-ext3
mkdir mnt-ext3
mount -t ext4 img-ext3 mnt-ext3and run the following repro script:
#!/bin/sh
echo | dd of=src bs=1 count=1 seek=64K
strace -v -o install.strace install src dst
cmp src dstthe output should be
src dst differ: char 65537, line 1
with
lseek(3, 0, SEEK_DATA)= -1 ENXIOin install.strace on the first lseek call, meaning that it reports "no more data until EOF" (and hence install does not copy anything).
Either mounting with '-o nodelalloc' or fdatasync'ing the file before install (conv=fdatasync in dd or 'sync -d src' after dd) avoids the problem.
Quoted 6 lines
The old ext4 wiki [1], the KernelNewbies wiki [2], and the Arch wiki [3] all claim that mounting ext3 as ext4 is expected to work correctly.
[1] https://archive.kernel.org/oldwiki/ext4.wiki.kernel.org/index.php/UpgradeToExt4.html
[2] https://kernelnewbies.org/Ext4
[3] https://wiki.archlinux.org/title/Ext4
I would like to ping this, and take this chance to cc Zhang Yi: it seems you have experience in this area, do you have some ideas what is happening here? How does delayed allocation interact with lseek like that?
I think it would be nice to solve this problem, because it is just silent data corruption (no sign in dmesg of anything going wrong) in a setup that was claimed to be safe by ext4 wiki (and propagated elsewhere).
Downstream this was discovered in Gentoo bug 970253 (I'm not the affected user, I assisted with root-causing the bug): https://bugs.gentoo.org/970253
Thanks.
Alexander