Skip to contentSkip to conversation

mkfs: Fix input offset counting in headerball mode

PATCH Superseded Mike Baynton 2 participants 8 messages Oct 24, 2024 – Nov 12, 2024 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mkfs: Fix input offset counting in headerball mode

When using --tar=headerball, most files included in the headerball are not included in the EROFS image. mkfs.erofs typically exits prematurely, having processed non-USTAR blocks as USTAR and believing they are end-of-archive markers. (Other failure modes are probably also possible if the input stream doesn't look like end-of-archive markers at the locations that are being read.)

This is because we lost correct count of bytes that are read from the input stream when in headerball (or ddtaridx) modes. We were assuming that in these modes no data would be read following the ustar block, but in case of things like PAX headers, lots more data may be read without incrementing tar->offset.

This corrects by always incrementing the offset counter, and then decrementing it again in the one case where headerballs differ - regular file data blocks are not present.

---

I suspect headerball mode may be unfamiliar, since it exists due to my prior contribution and my site is probably the only active user. As a recap, the idea of a "headerball" is to offer a relatively easy to produce, optimally efficient input format for constructing EROFS filesystems of only inode metadata. A headerball is a PAX tarball, except that all 512-byte blocks containing regular file data are not present.

For convenience, a small example "headerball" can be downloaded from
https://gist.github.com/mbaynton/bae60bf1044d83985956c6dc5b199cc3

This produces a 67-inode EROFS image with this patch, and as I recall about 3 inodes without it.

This also changes the behavior of ddtaridx format, which sounds like a similar use case but the user is Alibaba. It _looks_ like this format would also be affected, but without sample input I can't confirm.

 lib/tar.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/tar.c +3 -2
index b32abd4..726e565 100644
@@ -808,8 +808,7 @@ out_eot:
 	}
 
 	dataoff = tar->offset;
-	if (!(tar->headeronly_mode || tar->ddtaridx_mode))
-		tar->offset += st.st_size;
+	tar->offset += st.st_size;
 	switch(th->typeflag) {
 	case '0':
 	case '7':
@@ -1022,8 +1021,10 @@ new_inode:
 			memcpy(inode->i_link, eh.link, inode->i_size + 1);
 		} else if (inode->i_size) {
 			if (tar->headeronly_mode) {
+				tar->offset -= st.st_size; // assumed wrong earlier
 				ret = erofs_write_zero_inode(inode);
 			} else if (tar->ddtaridx_mode) {
+				tar->offset -= st.st_size; // assumed wrong earlier
 				dataoff = le64_to_cpu(*(__le64 *)(th->devmajor));
 				if (tar->rvsp_mode) {
 					inode->datasource = EROFS_INODE_DATA_SOURCE_RESVSP;
Signature · 1 line
2.34.1
#1 lore raw

Hi Mike,

Quoted 13 lines · Mike Baynton

When using --tar=headerball, most files included in the headerball are not included in the EROFS image. mkfs.erofs typically exits prematurely, having processed non-USTAR blocks as USTAR and believing they are end-of-archive markers. (Other failure modes are probably also possible if the input stream doesn't look like end-of-archive markers at the locations that are being read.)

This is because we lost correct count of bytes that are read from the input stream when in headerball (or ddtaridx) modes. We were assuming that in these modes no data would be read following the ustar block, but in case of things like PAX headers, lots more data may be read without incrementing tar->offset.

Quoted 5 lines

This corrects by always incrementing the offset counter, and then decrementing it again in the one case where headerballs differ - regular file data blocks are not present.

Sorry for late reply, I'm busy in personal stuffs now. I will look into these cases and reply again later.

Thanks,
Gao Xiang

#2 lore raw

Thanks for the update, Gao! Appreciate your review.

Mike

Quoted 28 lines · Gao Xiang

Hi Mike,

Quoted 13 lines · Mike Baynton

When using --tar=headerball, most files included in the headerball are not included in the EROFS image. mkfs.erofs typically exits prematurely, having processed non-USTAR blocks as USTAR and believing they are end-of-archive markers. (Other failure modes are probably also possible if the input stream doesn't look like end-of-archive markers at the locations that are being read.)

This is because we lost correct count of bytes that are read from the input stream when in headerball (or ddtaridx) modes. We were assuming that in these modes no data would be read following the ustar block, but in case of things like PAX headers, lots more data may be read without incrementing tar->offset.

Quoted 5 lines

This corrects by always incrementing the offset counter, and then decrementing it again in the one case where headerballs differ - regular file data blocks are not present.

Sorry for late reply, I'm busy in personal stuffs now. I will look into these cases and reply again later.

Thanks,
Gao Xiang

#3 lore raw

Hi Mike,

Quoted 3 lines · Mike Baynton

Thanks for the update, Gao! Appreciate your review.

Mike

I'm just back from vacation.

Quoted 19 lines · Gao Xiang
Quoted 17 lines

Hi Mike,

Quoted 13 lines · Mike Baynton

When using --tar=headerball, most files included in the headerball are not included in the EROFS image. mkfs.erofs typically exits prematurely, having processed non-USTAR blocks as USTAR and believing they are end-of-archive markers. (Other failure modes are probably also possible if the input stream doesn't look like end-of-archive markers at the locations that are being read.)

This is because we lost correct count of bytes that are read from the input stream when in headerball (or ddtaridx) modes. We were assuming that in these modes no data would be read following the ustar block, but in case of things like PAX headers, lots more data may be read without incrementing tar->offset.

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

This corrects by always incrementing the offset counter, and then decrementing it again in the one case where headerballs differ - regular file data blocks are not present.

Sorry for late reply, I'm busy in personal stuffs now. I will look into these cases and reply again later.

Yeah, thanks for the nice catch! Yet I'm not sure if decreasing tar->offset so late is safe (because there are potential early exits here).

So how about the following diff? If it looks good to you too, could you submit another version for this?

lib/tar.c +3 -2
index b32abd4..990c6cb 100644
@@ -808,13 +808,14 @@ out_eot:
 	}
 
 	dataoff = tar->offset;
-	if (!(tar->headeronly_mode || tar->ddtaridx_mode))
-		tar->offset += st.st_size;
+	tar->offset += st.st_size;
 	switch(th->typeflag) {
 	case '0':
 	case '7':
 	case '1':
 		st.st_mode |= S_IFREG;
+		if (tar->headeronly_mode || tar->ddtaridx_mode)
+			tar->offset -= st.st_size;
 		break;
 	case '2':
 		st.st_mode |= S_IFLNK;

Thanks,
Gao Xiang

#4 lore raw

PATCH v2 1/1 mkfs: Fix input offset counting in headerball mode

When using --tar=headerball, most files included in the headerball are not included in the EROFS image. mkfs.erofs typically exits prematurely, having processed non-USTAR blocks as USTAR and believing they are end-of-archive markers. (Other failure modes are probably also possible if the input stream doesn't look like end-of-archive markers at the locations that are being read.)

This is because we lost correct count of bytes that are read from the input stream when in headerball (or ddtaridx) modes. We were assuming that in these modes no data would be read following the ustar block, but in case of things like PAX headers, lots more data may be read without incrementing tar->offset.

This corrects by always incrementing the offset counter, and then decrementing it again in the one case where headerballs differ - regular file data blocks are not present.

---

Thanks Gao for the suggestion, looks good to me and tests ok on my sample headerball inputs. Let me know if you want me to resubmit this with Co-developed-by / Signed-off-by you.

 lib/tar.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/tar.c +3 -2
index b32abd4..990c6cb 100644
@@ -808,13 +808,14 @@ out_eot:
 	}
 
 	dataoff = tar->offset;
-	if (!(tar->headeronly_mode || tar->ddtaridx_mode))
-		tar->offset += st.st_size;
+	tar->offset += st.st_size;
 	switch(th->typeflag) {
 	case '0':
 	case '7':
 	case '1':
 		st.st_mode |= S_IFREG;
+		if (tar->headeronly_mode || tar->ddtaridx_mode)
+			tar->offset -= st.st_size;
 		break;
 	case '2':
 		st.st_mode |= S_IFLNK;
Signature · 1 line
2.34.1
#5 lore raw

Hi Mike,

Quoted 17 lines · Mike Baynton

When using --tar=headerball, most files included in the headerball are not included in the EROFS image. mkfs.erofs typically exits prematurely, having processed non-USTAR blocks as USTAR and believing they are end-of-archive markers. (Other failure modes are probably also possible if the input stream doesn't look like end-of-archive markers at the locations that are being read.)

This is because we lost correct count of bytes that are read from the input stream when in headerball (or ddtaridx) modes. We were assuming that in these modes no data would be read following the ustar block, but in case of things like PAX headers, lots more data may be read without incrementing tar->offset.

This corrects by always incrementing the offset counter, and then decrementing it again in the one case where headerballs differ - regular file data blocks are not present.

Quoted 6 lines

---

Thanks Gao for the suggestion, looks good to me and tests ok on my sample headerball inputs. Let me know if you want me to resubmit this with Co-developed-by / Signed-off-by you.

I will add "erofs-utils:" prefix to the patch subject but no need to add "Co-developed-by" tag.

Btw, if some converter for headerball files from tarballs is available in public? It'd be better to get some tests for this feature. ddtaridx is designed by some other team in Alibaba so I don't have a valid simple generator/converter too...

Thanks,
Gao Xiang

Quoted 25 lines
  lib/tar.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
lib/tar.c +3 -2
index b32abd4..990c6cb 100644
@@ -808,13 +808,14 @@ out_eot:
  	}
 
  	dataoff = tar->offset;
-	if (!(tar->headeronly_mode || tar->ddtaridx_mode))
-		tar->offset += st.st_size;
+	tar->offset += st.st_size;
  	switch(th->typeflag) {
  	case '0':
  	case '7':
  	case '1':
  		st.st_mode |= S_IFREG;
+		if (tar->headeronly_mode || tar->ddtaridx_mode)
+			tar->offset -= st.st_size;
  		break;
  	case '2':
  		st.st_mode |= S_IFLNK;
#6 lore raw
Quoted 10 lines · Gao Xiang

Hi Mike,

I will add "erofs-utils:" prefix to the patch subject but no need to add "Co-developed-by" tag.

Btw, if some converter for headerball files from tarballs is available in public? It'd be better to get some tests for this feature. ddtaridx is designed by some other team in Alibaba so I don't have a valid simple generator/converter too...

The converter we've made is in Go, could that be incorporated into your tests?

Quoted 30 lines

Thanks,
Gao Xiang

Quoted 25 lines
  lib/tar.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
lib/tar.c +3 -2
index b32abd4..990c6cb 100644
@@ -808,13 +808,14 @@ out_eot:
      }
 
      dataoff = tar->offset;
-     if (!(tar->headeronly_mode || tar->ddtaridx_mode))
-             tar->offset += st.st_size;
+     tar->offset += st.st_size;
      switch(th->typeflag) {
      case '0':
      case '7':
      case '1':
              st.st_mode |= S_IFREG;
+             if (tar->headeronly_mode || tar->ddtaridx_mode)
+                     tar->offset -= st.st_size;
              break;
      case '2':
              st.st_mode |= S_IFLNK;
#7 lore raw
Quoted 7 lines · Mike Baynton
Quoted 6 lines · Gao Xiang

Hi Mike,

I will add "erofs-utils:" prefix to the patch subject but no need to add "Co-developed-by" tag.

Quoted 6 lines
Quoted 4 lines

Btw, if some converter for headerball files from tarballs is available in public? It'd be better to get some tests for this feature. ddtaridx is designed by some other team in Alibaba so I don't have a valid simple generator/converter too...

The converter we've made is in Go, could that be incorporated into your tests?

I guess not... Currently erofs-utils has a testset:
https://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs-utils.git/log/?h=experimental-tests

I guess it'd be better to have a simple C converter anyway to avoid extra language dependency for now...

Thanks,
Gao Xiang