erofs-utils: lib: fix compressed packed inodes
authorDanny Lin <danny@orbstack.dev>
Mon, 23 Sep 2024 05:17:29 +0000 (22:17 -0700)
committerGao Xiang <hsiangkao@linux.alibaba.com>
Mon, 23 Sep 2024 15:17:04 +0000 (23:17 +0800)
Commit 2fdbd28 fixed uncompressed packed inodes by not always writing
compressed data, but it broke compressed packed inodes because now
uncompressed file data is always written after the compressed data.

The new error handling always rewinds with lseek and falls through to
write_uncompressed_file_from_fd, regardless of whether the compressed
data was written successfully (ret = 0) or not (ret = -ENOSPC). This
can result in corrupted files.

Fix it by simplifying the error handling to better match the old code.

Fixes: 2fdbd28 ("erofs-utils: lib: fix uncompressed packed inode")
Co-authored-by: Gao Xiang <hsiangkao@linux.alibaba.com>
Signed-off-by: Danny Lin <danny@orbstack.dev>
Reviewed-by: Gao Xiang <hsiangkao@linux.alibaba.com>
Link: https://lore.kernel.org/r/20240923051759.7563-2-danny@orbstack.dev
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
lib/inode.c

index f08f3950d7f8dbcd21c9616483846ab16722f9d5..7958d432ee2878293f54d3bc84555213e0eb3831 100644 (file)
@@ -1926,7 +1926,9 @@ struct erofs_inode *erofs_mkfs_build_special_from_fd(struct erofs_sb_info *sbi,
 
                DBG_BUGON(!ictx);
                ret = erofs_write_compressed_file(ictx);
-               if (ret && ret != -ENOSPC)
+               if (!ret)
+                       goto out;
+               if (ret != -ENOSPC)
                         return ERR_PTR(ret);
 
                ret = lseek(fd, 0, SEEK_SET);
@@ -1936,6 +1938,7 @@ struct erofs_inode *erofs_mkfs_build_special_from_fd(struct erofs_sb_info *sbi,
        ret = write_uncompressed_file_from_fd(inode, fd);
        if (ret)
                return ERR_PTR(ret);
+out:
        erofs_prepare_inode_buffer(inode);
        erofs_write_tail_end(inode);
        return inode;