Skip to contentSkip to conversation

erofs-utils: lib: fix compressed packed inodes

PATCH Superseded Danny Lin via Linux-erofs 2 participants 9 messages Sep 16, 2024 – Sep 23, 2024 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs-utils: lib: fix compressed packed inodes

Commit b9b6493 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 b9b6493 ("erofs-utils: lib: fix uncompressed packed inode")
---
 lib/inode.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
lib/inode.c +8 -6
index bc3cb76..797c622 100644
@@ -1927,14 +1927,16 @@ 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)
-			 return ERR_PTR(ret);
+		if (ret == -ENOSPC) {
+			ret = lseek(fd, 0, SEEK_SET);
+			if (ret < 0)
+				return ERR_PTR(-errno);
 
-		ret = lseek(fd, 0, SEEK_SET);
-		if (ret < 0)
-			return ERR_PTR(-errno);
+			ret = write_uncompressed_file_from_fd(inode, fd);
+		}
+	} else {
+		ret = write_uncompressed_file_from_fd(inode, fd);
 	}
-	ret = write_uncompressed_file_from_fd(inode, fd);
 	if (ret)
 		return ERR_PTR(ret);
 	erofs_prepare_inode_buffer(inode);
Signature · 1 line
2.46.0
#1 lore raw

Gentle bump — let me know if anything needs to be changed!

Thanks,
Danny

Quoted 48 lines · Danny Lin

Commit b9b6493 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 b9b6493 ("erofs-utils: lib: fix uncompressed packed inode")
---
 lib/inode.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
lib/inode.c +8 -7
index bc3cb76..797c622 100644
@@ -1927,14 +1927,16 @@ 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)
-                        return ERR_PTR(ret);
+               if (ret == -ENOSPC) {
+                       ret = lseek(fd, 0, SEEK_SET);
+                       if (ret < 0)
+                               return ERR_PTR(-errno);
 
-               ret = lseek(fd, 0, SEEK_SET);
-               if (ret < 0)
-                       return ERR_PTR(-errno);
+                       ret = write_uncompressed_file_from_fd(inode, fd);
+               }
+       } else {
+               ret = write_uncompressed_file_from_fd(inode, fd);
        }
-       ret = write_uncompressed_file_from_fd(inode, fd);
        if (ret)
                return ERR_PTR(ret);
        erofs_prepare_inode_buffer(inode);
--

2.46.0

#2 lore raw

Hi Danny,

Thanks for the patch!
Sorry I somewhat missed the previous email..

Quoted 1 line · Danny Lin

Gentle bump — let me know if anything needs to be changed!

Does the following change resolve the issue too?

Also I think it

Fixes 2fdbd28ad4a3 ("erofs-utils: lib: fix uncompressed packed inode")

@@ -1927,7 +1926,7 @@ 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 != -ENOSPC)
                         return ERR_PTR(ret);

                ret = lseek(fd, 0, SEEK_SET);

Thanks,
Gao Xiang

Quoted 3 lines

Thanks,
Danny

#3 lore raw

Hi Danny,

Quoted 14 lines · Gao Xiang

Hi Danny,

Thanks for the patch!
Sorry I somewhat missed the previous email..

Quoted 1 line · Danny Lin

Gentle bump — let me know if anything needs to be changed!

Does the following change resolve the issue too?

Also I think it

Fixes 2fdbd28ad4a3 ("erofs-utils: lib: fix uncompressed packed inode")

lib/inode.c struct erofs_inode *erofs_mkfs_build_special_from_fd(struct erofs_sb_info *sbi, · line 1932

Quoted 8 lines
lib/inode.c +1 -1
@@ -1927,7 +1926,7 @@ 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 != -ENOSPC)
                          return ERR_PTR(ret);
 
                 ret = lseek(fd, 0, SEEK_SET);

Add some more words, I'm on releasing erofs-utils 1.8.2 this week.

So if the diff above also fixes the issue, could you submit a patch for this so I could merge in time?

Thanks,
Gsao Xiang

Quoted 7 lines

Thanks,
Gao Xiang

Quoted 3 lines

Thanks,
Danny

#4 lore raw
Quoted 13 lines · Gao Xiang

Hi Danny,

Quoted 9 lines · Gao Xiang

Hi Danny,

Thanks for the patch!
Sorry I somewhat missed the previous email..

Quoted 1 line · Danny Lin

Gentle bump — let me know if anything needs to be changed!

Does the following change resolve the issue too?

Thanks for the suggestion. I tried your patch and it segfaults instead.

From a quick glance at the surrounding code, it doesn't seem correct because the calls to erofs_prepare_inode_buffer and erofs_write_tail_end are skipped if ret == 0.

Quoted 3 lines
Quoted 3 lines

Also I think it

Fixes 2fdbd28ad4a3 ("erofs-utils: lib: fix uncompressed packed inode")

Ah, nice catch. Do you want me to resubmit or will you fix it when applying the patch?

Quoted 14 lines

lib/inode.c struct erofs_inode *erofs_mkfs_build_special_from_fd(struct erofs_sb_info *sbi, · line 1932

Quoted 10 lines
lib/inode.c +1 -1
@@ -1927,7 +1926,7 @@ 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 != -ENOSPC)
                          return ERR_PTR(ret);
 
                 ret = lseek(fd, 0, SEEK_SET);

Add some more words, I'm on releasing erofs-utils 1.8.2 this week.

Quoted 14 lines

So if the diff above also fixes the issue, could you submit a patch for this so I could merge in time?

Thanks,
Gsao Xiang

Quoted 7 lines

Thanks,
Gao Xiang

Quoted 3 lines

Thanks,
Danny

Thanks,
Danny

#5 lore raw
Quoted 11 lines · Danny Lin
Quoted 9 lines · Gao Xiang

Hi Danny,

Quoted 5 lines · Gao Xiang

Hi Danny,

Thanks for the patch!
Sorry I somewhat missed the previous email..

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines · Danny Lin
Quoted 1 line

Gentle bump — let me know if anything needs to be changed!

Does the following change resolve the issue too?

Thanks for the suggestion. I tried your patch and it segfaults instead.

I think the segfault is because it returns ERR_PTR(0) instead of inode on success. That should be an easy fix but we'd still be skipping erofs_prepare_inode_buffer and erofs_write_tail_end.

Quoted 37 lines

From a quick glance at the surrounding code, it doesn't seem correct because the calls to erofs_prepare_inode_buffer and erofs_write_tail_end are skipped if ret == 0.

Quoted 3 lines
Quoted 3 lines

Also I think it

Fixes 2fdbd28ad4a3 ("erofs-utils: lib: fix uncompressed packed inode")

Ah, nice catch. Do you want me to resubmit or will you fix it when applying the patch?

Quoted 25 lines

lib/inode.c struct erofs_inode *erofs_mkfs_build_special_from_fd(struct erofs_sb_info *sbi, · line 1932

Quoted 10 lines
lib/inode.c +1 -1
@@ -1927,7 +1926,7 @@ 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 != -ENOSPC)
                          return ERR_PTR(ret);
 
                 ret = lseek(fd, 0, SEEK_SET);

Add some more words, I'm on releasing erofs-utils 1.8.2 this week.

So if the diff above also fixes the issue, could you submit a patch for this so I could merge in time?

Thanks,
Gsao Xiang

Quoted 5 lines

Thanks,
Gao Xiang

Quoted 1 line
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines
Quoted 2 lines

Thanks,
Danny

Thanks,
Danny

Thanks,
Danny

#6 lore raw

Hi Danny,

Quoted 15 lines · Danny Lin
Quoted 14 lines · Danny Lin
Quoted 12 lines · Gao Xiang

Hi Danny,

Quoted 8 lines · Gao Xiang

Hi Danny,

Thanks for the patch!
Sorry I somewhat missed the previous email..

Quoted 1 line · Danny Lin

Gentle bump — let me know if anything needs to be changed!

Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

Does the following change resolve the issue too?

Thanks for the suggestion. I tried your patch and it segfaults instead.

I think the segfault is because it returns ERR_PTR(0) instead of inode on success. That should be an easy fix but we'd still be skipping erofs_prepare_inode_buffer and erofs_write_tail_end.

Yeah, correct, thanks for catching! so I think just?

@@ -1927,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);
@@ -1937,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;
Quoted 7 lines
Quoted 7 lines

From a quick glance at the surrounding code, it doesn't seem correct
because the calls to erofs_prepare_inode_buffer and erofs_write_tail_end are skipped if ret == 0.

Quoted 1 line
Quoted 1 line
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

Also I think it

Fixes 2fdbd28ad4a3 ("erofs-utils: lib: fix uncompressed packed inode")

Ah, nice catch. Do you want me to resubmit or will you fix it when applying the patch?

Could you please resubmit since you're credited on this?

Thanks,
Gao Xiang

#7 lore raw

PATCH v2 1/1 erofs-utils: lib: fix compressed packed inodes

In reply to Danny Lin via Linux-erofs

From: Danny Lin <[email protected]>

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")
---
 lib/inode.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
lib/inode.c +4 -1
index bc3cb76..4c37a0d 100644
@@ -1927,7 +1927,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);
@@ -1937,6 +1939,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;
Signature · 1 line
2.46.1
Reviewed-by on v2 1/1
#8 lore raw
Quoted 11 lines · On 2024/9/23 13:17, [email protected] wrote:

From: Danny Lin <[email protected]>

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.

Quoted 5 lines

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

Fixes 2fdbd28 ("erofs-utils: lib: fix uncompressed packed inode")

Thanks,
Gao Xiang