Skip to contentSkip to conversation

erofs-utils: fix battach on full buffer block

PATCH Superseded Hu Weiwen 3 participants 17 messages Jan 18, 2021 – Feb 22, 2021 Library code

lore
Share & tools
lore

Hu Weiwen

author
#0 lore raw

PATCH 1/1 erofs-utils: fix battach on full buffer block

When __erofs_battach() is called on an buffer block of which (bb->buffers.off % EROFS_BLKSIZ == 0), `tail_blkaddr' will not be updated correctly. This bug can be reproduced by:

mkdir bug-repo
head -c 4032 /dev/urandom > bug-repo/1
head -c 4095 /dev/urandom > bug-repo/2
head -c 12345 /dev/urandom > bug-repo/3 # arbitrary size mkfs.erofs -Eforce-inode-compact bug-repo.erofs.img bug-repo

Then mount this image and see that file 3' in the image is different from bug-repo/3'.

This patch fix this by:

* Don't inline tail-end data in this case, since the tail-end data will be in a different block from inode.
* Correctly handle `battach' in this case.

---
 lib/cache.c | 4 ++--
 lib/inode.c | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
lib/cache.c +2 -2
index 9e65429..464a43c 100644
@@ -102,7 +102,7 @@ static int __erofs_battach(struct erofs_buffer_block *bb,
 			   bool dryrun)
 {
 	const erofs_off_t alignedoffset = roundup(bb->buffers.off, alignsize);
-	const int oob = cmpsgn(roundup(bb->buffers.off % EROFS_BLKSIZ,
+	const int oob = cmpsgn(roundup((bb->buffers.off - 1) % EROFS_BLKSIZ + 1,
 				       alignsize) + incr + extrasize,
 			       EROFS_BLKSIZ);
 	bool tailupdate = false;
@@ -134,7 +134,7 @@ static int __erofs_battach(struct erofs_buffer_block *bb,
 			tail_blkaddr = blkaddr + BLK_ROUND_UP(bb->buffers.off);
 		erofs_bupdate_mapped(bb);
 	}
-	return (alignedoffset + incr) % EROFS_BLKSIZ;
+	return (alignedoffset + incr - 1) % EROFS_BLKSIZ + 1;
 }
 
 int erofs_bh_balloon(struct erofs_buffer_head *bh, erofs_off_t incr)
lib/inode.c +1 -1
index 4ed6aed..d6a64cc 100644
@@ -531,7 +531,7 @@ int erofs_prepare_tail_block(struct erofs_inode *inode)
 	}
 	/* expend a block as the tail block (should be successful) */
 	ret = erofs_bh_balloon(bh, EROFS_BLKSIZ);
-	DBG_BUGON(ret);
+	DBG_BUGON(ret < 0);
 	return 0;
 }
 
Signature · 1 line
2.30.0
#1 lore raw

Hi Weiwen,

Quoted 10 lines · Hu Weiwen

When __erofs_battach() is called on an buffer block of which (bb->buffers.off % EROFS_BLKSIZ == 0), `tail_blkaddr' will not be updated correctly. This bug can be reproduced by:

mkdir bug-repo
head -c 4032 /dev/urandom > bug-repo/1
head -c 4095 /dev/urandom > bug-repo/2
head -c 12345 /dev/urandom > bug-repo/3 # arbitrary size mkfs.erofs -Eforce-inode-compact bug-repo.erofs.img bug-repo

Quoted 9 lines

Then mount this image and see that file 3' in the image is different from bug-repo/3'.

This patch fix this by:

* Don't inline tail-end data in this case, since the tail-end data will be in a different block from inode.
* Correctly handle `battach' in this case.

I will evaluate this condition later, yet if you have some interest and extra time, could you also help on writing a regression testcase for this, so we can look after such regression in case of the future code changes?

This is also an ongoing work for the next erofs-utils release, see:
https://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs-utils.git/

Thanks,
Gao Xiang

author
#2 lore raw

Hi Xiang,

I would like to. If I understand it correctly, tests should based on experimental-tests branch, and be submitted to this mail-list, too?

I wonder if we already have some CI service set up to run these tests automatically? I saw a mail from Travis CI in the mail-list archive, but it seems I don't have access to that organization.

Quoted 33 lines · Gao Xiang

Hi Weiwen,

Quoted 10 lines · Hu Weiwen

When __erofs_battach() is called on an buffer block of which (bb->buffers.off % EROFS_BLKSIZ == 0), `tail_blkaddr' will not be updated correctly. This bug can be reproduced by:

mkdir bug-repo
head -c 4032 /dev/urandom > bug-repo/1
head -c 4095 /dev/urandom > bug-repo/2
head -c 12345 /dev/urandom > bug-repo/3 # arbitrary size mkfs.erofs -Eforce-inode-compact bug-repo.erofs.img bug-repo

Quoted 9 lines

Then mount this image and see that file 3' in the image is different from bug-repo/3'.

This patch fix this by:

* Don't inline tail-end data in this case, since the tail-end data will be in a different block from inode.
* Correctly handle `battach' in this case.

I will evaluate this condition later, yet if you have some interest and extra time, could you also help on writing a regression testcase for this, so we can look after such regression in case of the future code changes?

This is also an ongoing work for the next erofs-utils release, see:
https://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs-utils.git/

Thanks,
Gao Xiang

#3 lore raw
Quoted 8 lines · 胡玮文

Hi Xiang,

I would like to. If I understand it correctly, tests should based on experimental-tests branch, and be submitted to this mail-list, too?

I wonder if we already have some CI service set up to run these tests automatically? I saw a mail from Travis CI in the mail-list archive, but it seems I don't have access to that organization.

A travis CI was once used for avoiding erofs-utils build regression... I haven't set up a CI to run such regression tests (since it's still in experimental branch...). Also, it seems that travis CI.org will be obsoleted in the near future, and I'm not sure if I like travisCI.com... Therefore, github action would be better (recently I tried to autobuild erofs-utils deb by using github action and it seems fine... will investigate to automaticly run regression tests when I have extra free slots...)

Thanks,
Gao Xiang

author
#4 lore raw
In reply to Gao Xiang

Hi Xiang,

After further investgate, this bug will not reveal in any released version of mkfs.erofs. Previous patch v5 [1] will map all allocated bb when erofs_mapbh() is called on an already mapped bb, which triggers this bug. before that patch, under the same condition, __erofs_battach() will only be called on bb which is not mapped, thus no need to update `tail_blkaddr'.

[1]: https://lore.kernel.org/linux-erofs/[email protected]/

Hu Weiwen

Quoted 33 lines · Gao Xiang

Hi Weiwen,

Quoted 10 lines · Hu Weiwen

When __erofs_battach() is called on an buffer block of which (bb->buffers.off % EROFS_BLKSIZ == 0), `tail_blkaddr' will not be updated correctly. This bug can be reproduced by:

mkdir bug-repo
head -c 4032 /dev/urandom > bug-repo/1
head -c 4095 /dev/urandom > bug-repo/2
head -c 12345 /dev/urandom > bug-repo/3 # arbitrary size mkfs.erofs -Eforce-inode-compact bug-repo.erofs.img bug-repo

Quoted 9 lines

Then mount this image and see that file 3' in the image is different from bug-repo/3'.

This patch fix this by:

* Don't inline tail-end data in this case, since the tail-end data will be in a different block from inode.
* Correctly handle `battach' in this case.

I will evaluate this condition later, yet if you have some interest and extra time, could you also help on writing a regression testcase for this, so we can look after such regression in case of the future code changes?

This is also an ongoing work for the next erofs-utils release, see:
https://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs-utils.git/

Thanks,
Gao Xiang

#5 lore raw

Hi Weiwen,

Quoted 7 lines · 胡玮文

Hi Xiang,

After further investgate, this bug will not reveal in any released version of mkfs.erofs. Previous patch v5 [1] will map all allocated bb when erofs_mapbh() is called on an already mapped bb, which triggers this bug. before that patch, under the same condition, __erofs_battach() will only be called on bb which is not mapped, thus no need to update `tail_blkaddr'.

Good to know this, thanks! I haven't looked into that (I will test it this weekend.) IMO, although this is not a regression, yet it seems it's potential harmful if we didn't notice this... So I think a proper testcase is still useful to look after this... If you have extra time, could you help on it?

Also, without the detail of this, I think the fix might be folded into the original patchset (could you resend it?). In addition, I think after last_mapped_block is introduced, we might not need tail_blkaddr anymore, not sure. But I'm very cautious about this in case of introducing any new regression...

Thanks,
Gao Xiang

Quoted 5 lines

[1]: https://lore.kernel.org/linux-erofs/[email protected]/

Hu Weiwen

author
#6 lore raw
Quoted 14 lines · 在 2021年1月19日,23:43,Gao Xiang <[email protected]> 写道:

Hi Weiwen,

Quoted 8 lines · 胡玮文

Hi Xiang,

After further investgate, this bug will not reveal in any released version of mkfs.erofs. Previous patch v5 [1] will map all allocated bb when erofs_mapbh() is called on an already mapped bb, which triggers this bug. before that patch, under the same condition, __erofs_battach() will only be called on bb which is not mapped, thus no need to update `tail_blkaddr'.

Quoted 4 lines

Good to know this, thanks! I haven't looked into that (I will test it this weekend.) IMO, although this is not a regression, yet it seems it's potential harmful if we didn't notice this... So I think a proper testcase is still useful to look after this... If you have extra time, could you help on it?

Hi Xiang,

I’m working on this. I have written a test case for this. And I’m also working on setting up GitHub actions to run tests automatically. So far, I’ve got uncompressed tests works, but when lz4 is enable, all test (except 001) fail. I have not found out why. You may see my progress at https://github.com/huww98/erofs-utils/tree/experimental-tests. I will send patches once everything is sorted out.

Quoted 2 lines

Also, without the detail of this, I think the fix might be folded into the original patchset (could you resend it?). In addition, I think after

You mean add a new commit [PATCH v6 3/3], or merge it into [PATCH v7 2/2]? I send it as a separate patch set because it may be merged independent of the cache.c optimization.

Quoted 3 lines

last_mapped_block is introduced, we might not need tail_blkaddr anymore, not sure. But I'm very cautious about this in case of introducing any new regression...

I think we still need it, because already mapped bb may be dropped, last_map_block does not always reflect tail_blkaddr.

Hu Weiwen

Quoted 8 lines

Thanks,
Gao Xiang

Quoted 5 lines

[1]: https://lore.kernel.org/linux-erofs/[email protected]/

Hu Weiwen

#7 lore raw

Hi Weiwen,

Quoted 14 lines · 胡玮文
Quoted 14 lines · 在 2021年1月19日,23:43,Gao Xiang <[email protected]> 写道:

Hi Weiwen,

Quoted 8 lines · 胡玮文

Hi Xiang,

After further investgate, this bug will not reveal in any released version of mkfs.erofs. Previous patch v5 [1] will map all allocated bb when erofs_mapbh() is called on an already mapped bb, which triggers this bug. before that patch, under the same condition, __erofs_battach() will only be called on bb which is not mapped, thus no need to update `tail_blkaddr'.

Quoted 8 lines
Quoted 4 lines

Good to know this, thanks! I haven't looked into that (I will test it this weekend.) IMO, although this is not a regression, yet it seems it's potential harmful if we didn't notice this... So I think a proper testcase is still useful to look after this... If you have extra time, could you help on it?

Hi Xiang,

I’m working on this. I have written a test case for this. And I’m also working on setting up GitHub actions to run tests automatically. So far, I’ve got uncompressed tests works, but when lz4 is enable, all test (except 001) fail. I have not found out why. You may see my progress at https://github.com/huww98/erofs-utils/tree/experimental-tests. I will send patches once everything is sorted out.

It would be better to know which kernel version github action is used (at least it seems no good if version is < 5.4)? also could you confirm the lz4 version as well (lz4-1.9.3)? if erofsfuse is used, specify "FSTYP=erofsfuse make check" to test it.

The temporary results are in "tests/results/", could you also check and debug it? (please kindly confirm the testcases work well on your local computer, since such testcase is still WIP, I'm not sure if it has some running issues as well)

Quoted 5 lines
Quoted 3 lines

Also, without the detail of this, I think the fix might be folded into the original patchset (could you resend it?). In addition, I think after

You mean add a new commit [PATCH v6 3/3], or merge it into [PATCH v7 2/2]? I send it as a separate patch set because it may be merged independent of the cache.c optimization.

Resend v7 and fold it into [v7 2/2] would be better...

Quoted 6 lines
Quoted 4 lines

last_mapped_block is introduced, we might not need tail_blkaddr anymore, not sure. But I'm very cautious about this in case of introducing any new regression...

I think we still need it, because already mapped bb may be dropped, last_map_block does not always reflect tail_blkaddr.

Okay, that makes sense...

Thanks,
Gao Xiang

Quoted 11 lines

Hu Weiwen

Quoted 8 lines

Thanks,
Gao Xiang

Quoted 5 lines

[1]: https://lore.kernel.org/linux-erofs/[email protected]/

Hu Weiwen

author
#8 lore raw

Hi Xiang,

Quoted 22 lines · Gao Xiang

Hi Weiwen,

Quoted 19 lines · 胡玮文
Quoted 18 lines · 在 2021年1月19日,23:43,Gao Xiang <[email protected]> 写道:

Hi Weiwen,

Quoted 8 lines · 胡玮文

Hi Xiang,

After further investgate, this bug will not reveal in any released version of mkfs.erofs. Previous patch v5 [1] will map all allocated bb when erofs_mapbh() is called on an already mapped bb, which triggers this bug. before that patch, under the same condition, __erofs_battach() will only be called on bb which is not mapped, thus no need to update `tail_blkaddr'.

Good to know this, thanks! I haven't looked into that (I will test it this weekend.) IMO, although this is not a regression, yet it seems it's potential harmful if we didn't notice this... So I think a proper testcase is still useful to look after this... If you have extra time, could you help on it?

Quoted 8 lines
Quoted 3 lines

Hi Xiang,

I’m working on this. I have written a test case for this. And I’m also working on setting up GitHub actions to run tests automatically. So far, I’ve got uncompressed tests works, but when lz4 is enable, all test (except 001) fail. I have not found out why. You may see my progress at https://github.com/huww98/erofs-utils/tree/experimental-tests. I will send patches once everything is sorted out.

It would be better to know which kernel version github action is used (at least it seems no good if version is < 5.4)? also could you confirm the lz4 version as well (lz4-1.9.3)? if erofsfuse is used, specify "FSTYP=erofsfuse make check" to test it.

I've verified kernel version is 5.4.0-1032-azure for ubuntu-20.04, and erofs mount succeeded. for lz4, I'm installing v1.9.3 manually from source. I haven't tried fuse, will give it a try later.

Quoted 4 lines

The temporary results are in "tests/results/", could you also check and debug it? (please kindly confirm the testcases work well on your local computer, since such testcase is still WIP, I'm not sure if it has some running issues as well)

I've downloaded "tests/results/" and it's test 007 (check for bad lz4 versions) that fails with output "test LZ4_compress_HC_destSize(1048576) error (4098 < 4116)". And it's the same error on my PC. Investigating.

BTW, why not use a more meaningful name for each test rather than a sequence number?

Hu Weiwen

Quoted 31 lines
Quoted 5 lines
Quoted 3 lines

Also, without the detail of this, I think the fix might be folded into the original patchset (could you resend it?). In addition, I think after

You mean add a new commit [PATCH v6 3/3], or merge it into [PATCH v7 2/2]? I send it as a separate patch set because it may be merged independent of the cache.c optimization.

Resend v7 and fold it into [v7 2/2] would be better...

Quoted 6 lines
Quoted 4 lines

last_mapped_block is introduced, we might not need tail_blkaddr anymore, not sure. But I'm very cautious about this in case of introducing any new regression...

I think we still need it, because already mapped bb may be dropped, last_map_block does not always reflect tail_blkaddr.

Okay, that makes sense...

Thanks,
Gao Xiang

Quoted 11 lines

Hu Weiwen

Quoted 8 lines

Thanks,
Gao Xiang

Quoted 5 lines

[1]: https://lore.kernel.org/linux-erofs/[email protected]/

Hu Weiwen

author
#9 lore raw
Quoted 38 lines · 胡玮文

Hi Xiang,

Quoted 22 lines · Gao Xiang

Hi Weiwen,

Quoted 19 lines · 胡玮文
Quoted 18 lines · 在 2021年1月19日,23:43,Gao Xiang <[email protected]> 写道:

Hi Weiwen,

Quoted 8 lines · 胡玮文

Hi Xiang,

After further investgate, this bug will not reveal in any released version of mkfs.erofs. Previous patch v5 [1] will map all allocated bb when erofs_mapbh() is called on an already mapped bb, which triggers this bug. before that patch, under the same condition, __erofs_battach() will only be called on bb which is not mapped, thus no need to update `tail_blkaddr'.

Good to know this, thanks! I haven't looked into that (I will test it this weekend.) IMO, although this is not a regression, yet it seems it's potential harmful if we didn't notice this... So I think a proper testcase is still useful to look after this... If you have extra time, could you help on it?

Quoted 8 lines
Quoted 3 lines

Hi Xiang,

I’m working on this. I have written a test case for this. And I’m also working on setting up GitHub actions to run tests automatically. So far, I’ve got uncompressed tests works, but when lz4 is enable, all test (except 001) fail. I have not found out why. You may see my progress at https://github.com/huww98/erofs-utils/tree/experimental-tests. I will send patches once everything is sorted out.

It would be better to know which kernel version github action is used (at least it seems no good if version is < 5.4)? also could you confirm the lz4 version as well (lz4-1.9.3)? if erofsfuse is used, specify "FSTYP=erofsfuse make check" to test it.

I've verified kernel version is 5.4.0-1032-azure for ubuntu-20.04, and erofs mount succeeded. for lz4, I'm installing v1.9.3 manually from source. I haven't tried fuse, will give it a try later.

Quoted 8 lines
Quoted 4 lines

The temporary results are in "tests/results/", could you also check and debug it? (please kindly confirm the testcases work well on your local computer, since such testcase is still WIP, I'm not sure if it has some running issues as well)

I've downloaded "tests/results/" and it's test 007 (check for bad lz4 versions) that fails with output "test LZ4_compress_HC_destSize(1048576) error (4098 < 4116)". And it's the same error on my PC. Investigating.

OK, I realized I just need to run ldconfig after installing new version of liblz4.so

Quoted 36 lines

BTW, why not use a more meaningful name for each test rather than a sequence number?

Hu Weiwen

Quoted 31 lines
Quoted 5 lines
Quoted 3 lines

Also, without the detail of this, I think the fix might be folded into the original patchset (could you resend it?). In addition, I think after

You mean add a new commit [PATCH v6 3/3], or merge it into [PATCH v7 2/2]? I send it as a separate patch set because it may be merged independent of the cache.c optimization.

Resend v7 and fold it into [v7 2/2] would be better...

Quoted 6 lines
Quoted 4 lines

last_mapped_block is introduced, we might not need tail_blkaddr anymore, not sure. But I'm very cautious about this in case of introducing any new regression...

I think we still need it, because already mapped bb may be dropped, last_map_block does not always reflect tail_blkaddr.

Okay, that makes sense...

Thanks,
Gao Xiang

Quoted 11 lines

Hu Weiwen

Quoted 8 lines

Thanks,
Gao Xiang

Quoted 5 lines

[1]: https://lore.kernel.org/linux-erofs/[email protected]/

Hu Weiwen

#10 lore raw
In reply to 胡玮文

On Thu, Jan 21, 2021 at 02:07:38PM +0800, 胡玮文 wrote:

...

Quoted 7 lines

I've downloaded "tests/results/" and it's test 007 (check for bad lz4 versions) that fails with output "test LZ4_compress_HC_destSize(1048576) error (4098 < 4116)". And it's the same error on my PC. Investigating.

BTW, why not use a more meaningful name for each test rather than a sequence number?

I'm not good at English naming (but such cases need stable names), also see xfstests:
https://git.kernel.org/pub/scm/fs/xfs/xfstests-dev.git/tree/tests/xfs
the meaningful description can be written down at tests/Makefile.am...

Yet they have no interest in integrating such testcases (also generic/ cases are not useful for EROFS either), and EROFS has many specific optimized paths so I decided to make a light-weight regression testcases in erofs-utils to look after it and kernel EROFS...

Thanks,
Gao Xiang

Quoted 2 lines

Hu Weiwen

author
#11 lore raw

PATCH v2 1/1 erofs-utils: fix battach on full buffer block

In reply to Gao Xiang

When __erofs_battach() is called on an buffer block of which (bb->buffers.off % EROFS_BLKSIZ == 0), `tail_blkaddr' will not be updated correctly. This bug can be reproduced by:

mkdir bug-repo
head -c 4032 /dev/urandom > bug-repo/1
head -c 4095 /dev/urandom > bug-repo/2
head -c 12345 /dev/urandom > bug-repo/3 # arbitrary size mkfs.erofs -Eforce-inode-compact bug-repo.erofs.img bug-repo

Then mount this image and see that file 3' in the image is different from bug-repo/3'.

This patch fix this by:

* Don't inline tail-end data in this case, since the tail-end data will be in a different block from inode.
* Correctly handle `battach' in this case.

---
Hi Xiang,

I still think send this as a seperate patch would be better. In previous v6 patch, I have fixed the erofs_mapbh() behaviour so that there should be no user-visible bug introduced in that patch. And this patch is almost unrelated to that optimization.

Compared with v1, this version fixes an error when compression is enabled.

Thanks,
Hu Weiwen

 lib/cache.c    | 4 ++--
 lib/compress.c | 2 +-
 lib/inode.c    | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
lib/cache.c +2 -2
index c9a8c50..a7de72d 100644
@@ -102,7 +102,7 @@ static int __erofs_battach(struct erofs_buffer_block *bb,
 			   bool dryrun)
 {
 	const erofs_off_t alignedoffset = roundup(bb->buffers.off, alignsize);
-	const int oob = cmpsgn(roundup(bb->buffers.off % EROFS_BLKSIZ,
+	const int oob = cmpsgn(roundup((bb->buffers.off - 1) % EROFS_BLKSIZ + 1,
 				       alignsize) + incr + extrasize,
 			       EROFS_BLKSIZ);
 	bool tailupdate = false;
@@ -134,7 +134,7 @@ static int __erofs_battach(struct erofs_buffer_block *bb,
 			tail_blkaddr = blkaddr + BLK_ROUND_UP(bb->buffers.off);
 		erofs_bupdate_mapped(bb);
 	}
-	return (alignedoffset + incr) % EROFS_BLKSIZ;
+	return (alignedoffset + incr - 1) % EROFS_BLKSIZ + 1;
 }
 
 int erofs_bh_balloon(struct erofs_buffer_head *bh, erofs_off_t incr)
lib/compress.c +1 -1
index 2b1f93c..670ac72 100644
@@ -457,7 +457,7 @@ int erofs_write_compressed_file(struct erofs_inode *inode)
 
 	close(fd);
 	ret = erofs_bh_balloon(bh, blknr_to_addr(compressed_blocks));
-	DBG_BUGON(ret);
+	DBG_BUGON(ret < 0);
 
 	erofs_info("compressed %s (%llu bytes) into %u blocks",
 		   inode->i_srcpath, (unsigned long long)inode->i_size,
lib/inode.c +1 -1
index 4ed6aed..d6a64cc 100644
@@ -531,7 +531,7 @@ int erofs_prepare_tail_block(struct erofs_inode *inode)
 	}
 	/* expend a block as the tail block (should be successful) */
 	ret = erofs_bh_balloon(bh, EROFS_BLKSIZ);
-	DBG_BUGON(ret);
+	DBG_BUGON(ret < 0);
 	return 0;
 }
 

--
2.30.0

#12 lore raw

Hi Weiwen,

Quoted 23 lines · Hu Weiwen

When __erofs_battach() is called on an buffer block of which (bb->buffers.off % EROFS_BLKSIZ == 0), `tail_blkaddr' will not be updated correctly. This bug can be reproduced by:

mkdir bug-repo
head -c 4032 /dev/urandom > bug-repo/1
head -c 4095 /dev/urandom > bug-repo/2
head -c 12345 /dev/urandom > bug-repo/3 # arbitrary size mkfs.erofs -Eforce-inode-compact bug-repo.erofs.img bug-repo

Then mount this image and see that file 3' in the image is different from bug-repo/3'.

This patch fix this by:

* Don't inline tail-end data in this case, since the tail-end data will be in a different block from inode.
* Correctly handle `battach' in this case.

---
Hi Xiang,

Quoted 6 lines

I still think send this as a seperate patch would be better. In previous v6 patch, I have fixed the erofs_mapbh() behaviour so that there should be no user-visible bug introduced in that patch. And this patch is almost unrelated to that optimization.

Compared with v1, this version fixes an error when compression is enabled.

I cannot tell too much of it for now (since it's not an obvious update, I need to investagate). I will leave it this weekend.

Thanks,
Gao Xiang

Quoted 3 lines

Thanks,
Hu Weiwen

#13 lore raw
In reply to Hu Weiwen

Hi Weiwen,

Quoted 23 lines · Hu Weiwen

When __erofs_battach() is called on an buffer block of which (bb->buffers.off % EROFS_BLKSIZ == 0), `tail_blkaddr' will not be updated correctly. This bug can be reproduced by:

mkdir bug-repo
head -c 4032 /dev/urandom > bug-repo/1
head -c 4095 /dev/urandom > bug-repo/2
head -c 12345 /dev/urandom > bug-repo/3 # arbitrary size mkfs.erofs -Eforce-inode-compact bug-repo.erofs.img bug-repo

Then mount this image and see that file 3' in the image is different from bug-repo/3'.

This patch fix this by:

* Don't inline tail-end data in this case, since the tail-end data will be in a different block from inode.
* Correctly handle `battach' in this case.

---
Hi Xiang,

Quoted 7 lines

I still think send this as a seperate patch would be better. In previous v6 patch, I have fixed the erofs_mapbh() behaviour so that there should be no user-visible bug introduced in that patch. And this patch is almost unrelated to that optimization.

Compared with v1, this version fixes an error when compression is enabled.

I have to say I still don't get the point of the whole description above and the patch itself honestly. even if (bb->buffers.off % EROFS_BLKSIZ == 0), the whole block can be used for tail-packing inline + inode. Assume that you testcase above is the case you addressed, could you elaborate them in detail?

If the original behavior is no user-visiable, I'm not sure what issue you'd like to resolve...

Thanks,
Gao Xiang

Quoted 2 lines

Thanks,
Hu Weiwen

Reviewed-by Gao Xiang on v2 1/1
#14 lore raw
In reply to Hu Weiwen

Hi Weiwen,

Quoted 13 lines · Hu Weiwen

When __erofs_battach() is called on an buffer block of which (bb->buffers.off % EROFS_BLKSIZ == 0), `tail_blkaddr' will not be updated correctly. This bug can be reproduced by:

mkdir bug-repo
head -c 4032 /dev/urandom > bug-repo/1
head -c 4095 /dev/urandom > bug-repo/2
head -c 12345 /dev/urandom > bug-repo/3 # arbitrary size mkfs.erofs -Eforce-inode-compact bug-repo.erofs.img bug-repo

Then mount this image and see that file 3' in the image is different from bug-repo/3'.

Quoted 8 lines

This patch fix this by:

* Don't inline tail-end data in this case, since the tail-end data will be in a different block from inode.
* Correctly handle `battach' in this case.

---

Now I get what you mentioned to, and I think this is a valid case and might influence old versions (even I have no idea how to reproduce it effectively.) So I tend to apply this patch right now, and thanks for your patch!

With updated commit message below:

When the subsequent erofs_battach() is called on an buffer block of which (bb->buffers.off % EROFS_BLKSIZ == 0), `tail_blkaddr' won't be updated correctly. This bug can be reproduced by:

mkdir bug-repo
head -c 4032 /dev/urandom > bug-repo/1
head -c 4095 /dev/urandom > bug-repo/2
head -c 12345 /dev/urandom > bug-repo/3 # arbitrary size mkfs.erofs -Eforce-inode-compact bug-repo.erofs.img bug-repo Then mount this image and see that file 3' in the image is different from bug-repo/3'.

This patch fix this by:
* Handle oob' and tail_blkaddr' for the case above properly;
* Don't inline tail-packing data for such case, since the tail-packing data is actually in a different block from inode itself even kernel can handle such cases properly.

Thanks,
Gao Xiang

#15 lore raw

PATCH v3 1/1 erofs-utils: fix battach on full buffer blocks

In reply to Hu Weiwen

From: Hu Weiwen <[email protected]>

When the subsequent erofs_battach() is called on an buffer block of which (bb->buffers.off % EROFS_BLKSIZ == 0), `tail_blkaddr' won't be updated correctly. This bug can be reproduced by:

mkdir bug-repo
head -c 4032 /dev/urandom > bug-repo/1
head -c 4095 /dev/urandom > bug-repo/2
head -c 12345 /dev/urandom > bug-repo/3 # arbitrary size mkfs.erofs -Eforce-inode-compact bug-repo.erofs.img bug-repo Then mount this image and see that file 3' in the image is different from bug-repo/3'.

This patch fix this by:
* Handle oob' and tail_blkaddr' for the case above properly;
* Don't inline tail-packing data for such case, since the tail-packing data is actually in a different block from inode itself even kernel can handle such cases properly.

---
changes since v2:
- update commit message;
- refine 2 asserts from < 0 to != EROFS_BLKSIZ.

 lib/cache.c    | 4 ++--
 lib/compress.c | 3 ++-
 lib/inode.c    | 2 +-
 3 files changed, 5 insertions(+), 4 deletions(-)
lib/cache.c +2 -2
index 40d3b1f3f4d5..e3327c3f1586 100644
@@ -102,7 +102,7 @@ static int __erofs_battach(struct erofs_buffer_block *bb,
 			   bool dryrun)
 {
 	const erofs_off_t alignedoffset = roundup(bb->buffers.off, alignsize);
-	const int oob = cmpsgn(roundup(bb->buffers.off % EROFS_BLKSIZ,
+	const int oob = cmpsgn(roundup((bb->buffers.off - 1) % EROFS_BLKSIZ + 1,
 				       alignsize) + incr + extrasize,
 			       EROFS_BLKSIZ);
 	bool tailupdate = false;
@@ -134,7 +134,7 @@ static int __erofs_battach(struct erofs_buffer_block *bb,
 			tail_blkaddr = blkaddr + BLK_ROUND_UP(bb->buffers.off);
 		erofs_bupdate_mapped(bb);
 	}
-	return (alignedoffset + incr) % EROFS_BLKSIZ;
+	return (alignedoffset + incr - 1) % EROFS_BLKSIZ + 1;
 }
 
 int erofs_bh_balloon(struct erofs_buffer_head *bh, erofs_off_t incr)
lib/compress.c +2 -1
index 2b1f93c389ff..4b685cd27080 100644
@@ -456,8 +456,9 @@ int erofs_write_compressed_file(struct erofs_inode *inode)
 	vle_write_indexes_final(&ctx);
 
 	close(fd);
+	DBG_BUGON(!compressed_blocks);
 	ret = erofs_bh_balloon(bh, blknr_to_addr(compressed_blocks));
-	DBG_BUGON(ret);
+	DBG_BUGON(ret != EROFS_BLKSIZ);
 
 	erofs_info("compressed %s (%llu bytes) into %u blocks",
 		   inode->i_srcpath, (unsigned long long)inode->i_size,
lib/inode.c +1 -1
index 6371aa563673..40189fed37dd 100644
@@ -531,7 +531,7 @@ int erofs_prepare_tail_block(struct erofs_inode *inode)
 	}
 	/* expend a block as the tail block (should be successful) */
 	ret = erofs_bh_balloon(bh, EROFS_BLKSIZ);
-	DBG_BUGON(ret);
+	DBG_BUGON(ret != EROFS_BLKSIZ);
 	return 0;
 }
 
Signature · 1 line
2.24.0
Reviewed-by Li Guifu on v3 1/1
#16 lore raw

Re: [PATCH v3] erofs-utils: fix battach on full buffer blocks

Quoted 20 lines · Gao Xiang via Linux-erofs

From: Hu Weiwen <[email protected]>

When the subsequent erofs_battach() is called on an buffer block of which (bb->buffers.off % EROFS_BLKSIZ == 0), `tail_blkaddr' won't be updated correctly. This bug can be reproduced by:

mkdir bug-repo
head -c 4032 /dev/urandom > bug-repo/1
head -c 4095 /dev/urandom > bug-repo/2
head -c 12345 /dev/urandom > bug-repo/3 # arbitrary size mkfs.erofs -Eforce-inode-compact bug-repo.erofs.img bug-repo Then mount this image and see that file 3' in the image is different from bug-repo/3'.

This patch fix this by:
* Handle oob' and tail_blkaddr' for the case above properly;
* Don't inline tail-packing data for such case, since the tail-packing data is actually in a different block from inode itself even kernel can handle such cases properly.

Quoted 4 lines

---

It looks good

Thanks,