tytso / e2fsprogs

Ext2/3/4 file system utilities
http://ext4.wiki.kernel.org
373 stars 219 forks source link

fsck breaks preallocated files #145

Open antmat opened 1 year ago

antmat commented 1 year ago

Hi! First of all I'm in doubt if this is a ext4 driver issue or fsck issue, but as far as without fsck all works just fine I thought you may be interested in it. In situation when we have a file with rather small size, which was preallocated via fallocate with FALLOC_FL_KEEP_SIZE flag to rather big value fsck treats its extents as invalid and lost data.

Some details:

I created a minimal reproducible example with minimal comments, you can see it here: https://gist.github.com/antmat/569408754a007f7519a0ce5122e4ffac

Thanks in advance!

antmat commented 1 year ago

It looks like the problem is here https://github.com/tytso/e2fsprogs/blob/25ad8a431331b4d1d444a70b6079456cc612ac40/lib/ext2fs/extent.c#L287 We assume, that end_blk is located at the size bound of file, but that is not true, as we preallocated more than size blocks. The check does not fail on extent tree of deth 2, as on the second level there are leaf nodes that are in UNINIT state, but starting from depth 3 we have index nodes, which doesn't have that flag

antmat commented 1 year ago

https://gist.github.com/antmat/9c3ce004a080c50d341e8c3afb22416a I've created a simple patch, that fixes the problem, but it has a problem itself: if we have a file with holes and preallocated space end_blk should be calculated smarter. Perhaps we could traverse child nodes to determine real bounds or we can leave end_blk zeroed.