Skip to contentSkip to conversation

erofs-utils: erofs debug utility.

PATCH In review Pratik Shinde 3 participants 5 messages Aug 21, 2019 – Aug 22, 2019 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs-utils: erofs debug utility.

Hello Maintainers,

After going through the recent mail thread between linux's filesystem folks on erofs channel, I felt erofs needs an interactive debug utility (like xfs_db) which can be used to examine erofs images & can also be used to inject errors OR fuzzing for testing purpose & dumping different erofs meta data structures for debugging.
In order to demonstrate above I wrote an experimental patch that simply dumps the superblock of an image after mkfs completes.the full fletch utility will run independently and be able to seek / print / modify any byte of an erofs image, dump structures/lists/directory content of an image.

NOTE:This is an experimental patch just to demonstrate the purpose. The patch lacks a lot of things like coding standard, and new code runs in the context of mkfs itself.kindly ignore it.

kindly provide your feedback on this.

---
 include/erofs/io.h |  8 ++++++++
 lib/io.c           | 27 +++++++++++++++++++++++++++
 mkfs/main.c        | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 71 insertions(+)
include/erofs/io.h +8 -0
index 4b574bd..e91d6ee 100644
@@ -18,6 +18,7 @@
 
 int dev_open(const char *devname);
 void dev_close(void);
+int dev_read(void *buf, u64 offset, size_t len);
 int dev_write(const void *buf, u64 offset, size_t len);
 int dev_fillzero(u64 offset, size_t len, bool padding);
 int dev_fsync(void);
@@ -30,5 +31,12 @@ static inline int blk_write(const void *buf, erofs_blk_t blkaddr,
 			 blknr_to_addr(nblocks));
 }
 
+static inline int blk_read(void *buf, erofs_blk_t blkaddr,
+			   u32 nblocks)
+{
+	return dev_read(buf, blknr_to_addr(blkaddr),
+			blknr_to_addr(nblocks));
+}
+
 #endif
 
lib/io.c +27 -0
index 15c5a35..87d7d6c 100644
@@ -109,6 +109,33 @@ u64 dev_length(void)
 	return erofs_devsz;
 }
 
+int dev_read(void *buf, u64 offset, size_t len)
+{
+	int ret;
+
+	if (cfg.c_dry_run)
+		return 0;
+
+	if (!buf) {
+		erofs_err("buf is NULL");
+		return -EINVAL;
+	}
+	if (offset >= erofs_devsz || len > erofs_devsz ||
+	    offset > erofs_devsz - len) {
+		erofs_err("read posion[%" PRIu64 ", %zd] is too large beyond the end of device(%" PRIu64 ").",
+			  offset, len, erofs_devsz);
+		return -EINVAL;
+	}
+
+	ret = pread64(erofs_devfd, buf, len, (off64_t)offset);
+	if (ret != (int)len) {
+		erofs_err("Failed to read data from device - %s:[%" PRIu64 ", %zd].",
+			  erofs_devname, offset, len);
+		return -errno;
+	}
+	return 0;
+}
+
 int dev_write(const void *buf, u64 offset, size_t len)
 {
 	int ret;
mkfs/main.c +36 -0
index f127fe1..109486e 100644
@@ -182,6 +182,41 @@ int erofs_mkfs_update_super_block(struct erofs_buffer_head *bh,
 	return 0;
 }
 
+void erofs_dump_super(char *img_path)
+{
+	struct erofs_super_block *sb;
+	char buf[EROFS_BLKSIZ];
+	unsigned int blksz;
+	int ret = 0;
+
+	if (img_path == NULL) {
+		erofs_err("image path cannot be null");
+		return;
+	}
+	ret = blk_read(buf, 0, 1);
+	if (ret) {
+		erofs_err("error reading super-block structure");
+		return;
+	}
+
+	sb = (struct erofs_super_block *)((u8 *)buf + EROFS_SUPER_OFFSET);
+	if (le32_to_cpu(sb->magic) != EROFS_SUPER_MAGIC_V1) {
+		erofs_err("not a erofs image");
+		return;
+	}
+
+	erofs_dump("magic: 0x%x\n", le32_to_cpu(sb->magic));
+	blksz = 1 << sb->blkszbits;
+	erofs_dump("block size: %d\n", blksz);
+	erofs_dump("root inode: %d\n", le32_to_cpu(sb->root_nid));
+	erofs_dump("inodes: %llu\n", le64_to_cpu(sb->inos));
+	erofs_dump("build time: %u\n", le32_to_cpu(sb->build_time));
+	erofs_dump("blocks: %u\n", le32_to_cpu(sb->blocks));
+	erofs_dump("meta block: %u\n", le32_to_cpu(sb->meta_blkaddr));
+	erofs_dump("xattr block: %u\n", le32_to_cpu(sb->xattr_blkaddr));
+	erofs_dump("requirements: 0x%x\n", le32_to_cpu(sb->requirements));
+}
+
 int main(int argc, char **argv)
 {
 	int err = 0;
@@ -268,6 +303,7 @@ int main(int argc, char **argv)
 		err = -EIO;
 exit:
 	z_erofs_compress_exit();
+	erofs_dump_super("dummy");
 	dev_close();
 	erofs_exit_configure();
 
Signature · 1 line
2.9.3
#1 lore raw

Hi Pratik,

Quoted 11 lines · Pratik Shinde

Hello Maintainers,

After going through the recent mail thread between linux's filesystem folks on erofs channel, I felt erofs needs an interactive debug utility (like xfs_db) which can be used to examine erofs images & can also be used to inject errors OR fuzzing for testing purpose & dumping different erofs meta data structures for debugging.
In order to demonstrate above I wrote an experimental patch that simply dumps the superblock of an image after mkfs completes.the full fletch utility will run independently and be able to seek / print / modify any byte of an erofs image, dump structures/lists/directory content of an image.

Yes, I think we really need that interactive tools, actually I'm stuggling in modifing Guifu's erofs-fuse now, we need to add the parsing ability to "lib/" first.

I mean, first, I will add a "fuse" field to "cfg". If it is false, it will generate a image, or it will parse a image...
And then we need to add parsing logic into "lib/" as well, and use "if (cfg.fuse)" to differnate whether it should read or write data.

That is my prelimitary thought.. I will work on this framework in this weekend. and then we can work together on it. :)

p.s. Pratik, if you have some time, could you take some extra time adding the super checksum calulation to EROFS? I mean we can add EROFS_FEATURE_SB_CHKSUM to the compat superblock field ("features"), and do crc32_le on kernel and mkfs... If you dont have time, I will do it later instead... (since we are using EROFS on the top of dm-verity, but completing the superblock chksum is also a good idea.)

And then we can add block-based verification layer to EROFS, it can be seen as a hash tree like dm-verity or just simply CRC32 arrays for user to choise.

Thanks,
Gao Xiang

Quoted 134 lines

NOTE:This is an experimental patch just to demonstrate the purpose. The patch lacks a lot of things like coding standard, and new code runs in the context of mkfs itself.kindly ignore it.

kindly provide your feedback on this.

---
 include/erofs/io.h |  8 ++++++++
 lib/io.c           | 27 +++++++++++++++++++++++++++
 mkfs/main.c        | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 71 insertions(+)
include/erofs/io.h +8 -0
index 4b574bd..e91d6ee 100644
@@ -18,6 +18,7 @@
 
 int dev_open(const char *devname);
 void dev_close(void);
+int dev_read(void *buf, u64 offset, size_t len);
 int dev_write(const void *buf, u64 offset, size_t len);
 int dev_fillzero(u64 offset, size_t len, bool padding);
 int dev_fsync(void);
@@ -30,5 +31,12 @@ static inline int blk_write(const void *buf, erofs_blk_t blkaddr,
 			 blknr_to_addr(nblocks));
 }
 
+static inline int blk_read(void *buf, erofs_blk_t blkaddr,
+			   u32 nblocks)
+{
+	return dev_read(buf, blknr_to_addr(blkaddr),
+			blknr_to_addr(nblocks));
+}
+
 #endif
 
lib/io.c +27 -0
index 15c5a35..87d7d6c 100644
@@ -109,6 +109,33 @@ u64 dev_length(void)
 	return erofs_devsz;
 }
 
+int dev_read(void *buf, u64 offset, size_t len)
+{
+	int ret;
+
+	if (cfg.c_dry_run)
+		return 0;
+
+	if (!buf) {
+		erofs_err("buf is NULL");
+		return -EINVAL;
+	}
+	if (offset >= erofs_devsz || len > erofs_devsz ||
+	    offset > erofs_devsz - len) {
+		erofs_err("read posion[%" PRIu64 ", %zd] is too large beyond the end of device(%" PRIu64 ").",
+			  offset, len, erofs_devsz);
+		return -EINVAL;
+	}
+
+	ret = pread64(erofs_devfd, buf, len, (off64_t)offset);
+	if (ret != (int)len) {
+		erofs_err("Failed to read data from device - %s:[%" PRIu64 ", %zd].",
+			  erofs_devname, offset, len);
+		return -errno;
+	}
+	return 0;
+}
+
 int dev_write(const void *buf, u64 offset, size_t len)
 {
 	int ret;
mkfs/main.c +36 -0
index f127fe1..109486e 100644
@@ -182,6 +182,41 @@ int erofs_mkfs_update_super_block(struct erofs_buffer_head *bh,
 	return 0;
 }
 
+void erofs_dump_super(char *img_path)
+{
+	struct erofs_super_block *sb;
+	char buf[EROFS_BLKSIZ];
+	unsigned int blksz;
+	int ret = 0;
+
+	if (img_path == NULL) {
+		erofs_err("image path cannot be null");
+		return;
+	}
+	ret = blk_read(buf, 0, 1);
+	if (ret) {
+		erofs_err("error reading super-block structure");
+		return;
+	}
+
+	sb = (struct erofs_super_block *)((u8 *)buf + EROFS_SUPER_OFFSET);
+	if (le32_to_cpu(sb->magic) != EROFS_SUPER_MAGIC_V1) {
+		erofs_err("not a erofs image");
+		return;
+	}
+
+	erofs_dump("magic: 0x%x\n", le32_to_cpu(sb->magic));
+	blksz = 1 << sb->blkszbits;
+	erofs_dump("block size: %d\n", blksz);
+	erofs_dump("root inode: %d\n", le32_to_cpu(sb->root_nid));
+	erofs_dump("inodes: %llu\n", le64_to_cpu(sb->inos));
+	erofs_dump("build time: %u\n", le32_to_cpu(sb->build_time));
+	erofs_dump("blocks: %u\n", le32_to_cpu(sb->blocks));
+	erofs_dump("meta block: %u\n", le32_to_cpu(sb->meta_blkaddr));
+	erofs_dump("xattr block: %u\n", le32_to_cpu(sb->xattr_blkaddr));
+	erofs_dump("requirements: 0x%x\n", le32_to_cpu(sb->requirements));
+}
+
 int main(int argc, char **argv)
 {
 	int err = 0;
@@ -268,6 +303,7 @@ int main(int argc, char **argv)
 		err = -EIO;
 exit:
 	z_erofs_compress_exit();
+	erofs_dump_super("dummy");
 	dev_close();
 	erofs_exit_configure();
 
Signature · 1 line
2.9.3
#2 lore raw

Thanks Gao,

Let me know when we can work on debug utility OR atleast list out things we want to achieve through the utility.
Regarding the superblock checksum calculations, Yes I will dedicate sometime this week for it. will start exploring it.

--Pratik.

Quoted 196 lines · Gao Xiang

Hi Pratik,

Quoted 3 lines · Pratik Shinde

Hello Maintainers,

After going through the recent mail thread between linux's filesystem

folks

Quoted 1 line

on erofs channel, I felt erofs needs an interactive debug utility (like

xfs_db)

Quoted 1 line

which can be used to examine erofs images & can also be used to inject

errors OR

Quoted 1 line

fuzzing for testing purpose & dumping different erofs meta data

structures

Quoted 2 lines

for debugging.
In order to demonstrate above I wrote an experimental patch that simply

dumps

Quoted 1 line

the superblock of an image after mkfs completes.the full fletch utility

will run

Quoted 1 line

independently and be able to seek / print / modify any byte of an erofs

image,

Quoted 1 line

dump structures/lists/directory content of an image.

Yes, I think we really need that interactive tools, actually I'm stuggling in
modifing Guifu's erofs-fuse now, we need to add the parsing ability to "lib/"
first.

I mean, first, I will add a "fuse" field to "cfg". If it is false, it will generate a image, or it will parse a image...
And then we need to add parsing logic into "lib/" as well, and use "if (cfg.fuse)" to differnate whether it should read or write data.

That is my prelimitary thought.. I will work on this framework in this weekend.
and then we can work together on it. :)

p.s. Pratik, if you have some time, could you take some extra time adding the
super checksum calulation to EROFS? I mean we can add EROFS_FEATURE_SB_CHKSUM
to the compat superblock field ("features"), and do crc32_le on kernel and mkfs...
If you dont have time, I will do it later instead... (since we are using EROFS
on the top of dm-verity, but completing the superblock chksum is also a good idea.)

And then we can add block-based verification layer to EROFS, it can be seen as a hash tree like dm-verity or just simply CRC32 arrays for user to choise.

Thanks,
Gao Xiang

Quoted 2 lines

NOTE:This is an experimental patch just to demonstrate the purpose. The

patch

Quoted 1 line

lacks a lot of things like coding standard, and new code runs in the

context

include/erofs/io.h static inline int blk_write(const void *buf,

Quoted 24 lines

of mkfs itself.kindly ignore it.

kindly provide your feedback on this.

---
 include/erofs/io.h |  8 ++++++++
 lib/io.c           | 27 +++++++++++++++++++++++++++
 mkfs/main.c        | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 71 insertions(+)
include/erofs/io.h +1 -0
index 4b574bd..e91d6ee 100644
@@ -18,6 +18,7 @@
 
 int dev_open(const char *devname);
 void dev_close(void);
+int dev_read(void *buf, u64 offset, size_t len);
 int dev_write(const void *buf, u64 offset, size_t len);
 int dev_fillzero(u64 offset, size_t len, bool padding);
 int dev_fsync(void);
@@ -30,5 +31,12 @@ static inline int blk_write(const void *buf,

erofs_blk_t blkaddr,

Quoted 24 lines
include/erofs/io.h +7 -0
                       blknr_to_addr(nblocks));
 }
 
+static inline int blk_read(void *buf, erofs_blk_t blkaddr,
+                        u32 nblocks)
+{
+     return dev_read(buf, blknr_to_addr(blkaddr),
+                     blknr_to_addr(nblocks));
+}
+
 #endif
 
lib/io.c +4 -0
index 15c5a35..87d7d6c 100644
@@ -109,6 +109,33 @@ u64 dev_length(void)
      return erofs_devsz;
 }
 
+int dev_read(void *buf, u64 offset, size_t len)
+{
+     int ret;
+

lib/io.c u64 dev_length(void) · line 125

Quoted 10 lines
lib/io.c +10 -0
+     if (cfg.c_dry_run)
+             return 0;
+
+     if (!buf) {
+             erofs_err("buf is NULL");
+             return -EINVAL;
+     }
+     if (offset >= erofs_devsz || len > erofs_devsz ||
+         offset > erofs_devsz - len) {
+             erofs_err("read posion[%" PRIu64 ", %zd] is too large

beyond the end of device(%" PRIu64 ").",

lib/io.c u64 dev_length(void)

Quoted 7 lines
lib/io.c +7 -0
+                       offset, len, erofs_devsz);
+             return -EINVAL;
+     }
+
+     ret = pread64(erofs_devfd, buf, len, (off64_t)offset);
+     if (ret != (int)len) {
+             erofs_err("Failed to read data from device - %s:[%" PRIu64

", %zd].",

mkfs/main.c int erofs_mkfs_update_super_block(struct

Quoted 14 lines
lib/io.c +6 -0
+                       erofs_devname, offset, len);
+             return -errno;
+     }
+     return 0;
+}
+
 int dev_write(const void *buf, u64 offset, size_t len)
 {
      int ret;
mkfs/main.c +0 -0
index f127fe1..109486e 100644
@@ -182,6 +182,41 @@ int erofs_mkfs_update_super_block(struct

erofs_buffer_head *bh,

Quoted 52 lines
mkfs/main.c +36 -1
      return 0;
 }
 
+void erofs_dump_super(char *img_path)
+{
+     struct erofs_super_block *sb;
+     char buf[EROFS_BLKSIZ];
+     unsigned int blksz;
+     int ret = 0;
+
+     if (img_path == NULL) {
+             erofs_err("image path cannot be null");
+             return;
+     }
+     ret = blk_read(buf, 0, 1);
+     if (ret) {
+             erofs_err("error reading super-block structure");
+             return;
+     }
+
+     sb = (struct erofs_super_block *)((u8 *)buf + EROFS_SUPER_OFFSET);
+     if (le32_to_cpu(sb->magic) != EROFS_SUPER_MAGIC_V1) {
+             erofs_err("not a erofs image");
+             return;
+     }
+
+     erofs_dump("magic: 0x%x\n", le32_to_cpu(sb->magic));
+     blksz = 1 << sb->blkszbits;
+     erofs_dump("block size: %d\n", blksz);
+     erofs_dump("root inode: %d\n", le32_to_cpu(sb->root_nid));
+     erofs_dump("inodes: %llu\n", le64_to_cpu(sb->inos));
+     erofs_dump("build time: %u\n", le32_to_cpu(sb->build_time));
+     erofs_dump("blocks: %u\n", le32_to_cpu(sb->blocks));
+     erofs_dump("meta block: %u\n", le32_to_cpu(sb->meta_blkaddr));
+     erofs_dump("xattr block: %u\n", le32_to_cpu(sb->xattr_blkaddr));
+     erofs_dump("requirements: 0x%x\n", le32_to_cpu(sb->requirements));
+}
+
 int main(int argc, char **argv)
 {
      int err = 0;
@@ -268,6 +303,7 @@ int main(int argc, char **argv)
              err = -EIO;
 exit:
      z_erofs_compress_exit();
+     erofs_dump_super("dummy");
      dev_close();
      erofs_exit_configure();
 
--

2.9.3

#3 lore raw

Hi Pratik,

Quoted 4 lines · Pratik Shinde

Thanks Gao,

Let me know when we can work on debug utility OR atleast list out things we want to achieve through the utility.

I think we need to implement
- dump super block
- dump a specfic file
- dump a physical block or a physical compressed cluster from nid and logical offset
- dump a specfic metadata (I mean dump the inode meta of a file)
- ...

Thanks,
Gao Xiang

Quoted 203 lines

Regarding the superblock checksum calculations, Yes I will dedicate sometime this week for it. will start exploring it.

--Pratik.

Quoted 196 lines · Gao Xiang

Hi Pratik,

Quoted 3 lines · Pratik Shinde

Hello Maintainers,

After going through the recent mail thread between linux's filesystem

folks

Quoted 1 line

on erofs channel, I felt erofs needs an interactive debug utility (like

xfs_db)

Quoted 1 line

which can be used to examine erofs images & can also be used to inject

errors OR

Quoted 1 line

fuzzing for testing purpose & dumping different erofs meta data

structures

Quoted 2 lines

for debugging.
In order to demonstrate above I wrote an experimental patch that simply

dumps

Quoted 1 line

the superblock of an image after mkfs completes.the full fletch utility

will run

Quoted 1 line

independently and be able to seek / print / modify any byte of an erofs

image,

Quoted 1 line

dump structures/lists/directory content of an image.

Yes, I think we really need that interactive tools, actually I'm stuggling in
modifing Guifu's erofs-fuse now, we need to add the parsing ability to "lib/"
first.

I mean, first, I will add a "fuse" field to "cfg". If it is false, it will generate a image, or it will parse a image...
And then we need to add parsing logic into "lib/" as well, and use "if (cfg.fuse)" to differnate whether it should read or write data.

That is my prelimitary thought.. I will work on this framework in this weekend.
and then we can work together on it. :)

p.s. Pratik, if you have some time, could you take some extra time adding the
super checksum calulation to EROFS? I mean we can add EROFS_FEATURE_SB_CHKSUM
to the compat superblock field ("features"), and do crc32_le on kernel and mkfs...
If you dont have time, I will do it later instead... (since we are using EROFS
on the top of dm-verity, but completing the superblock chksum is also a good idea.)

And then we can add block-based verification layer to EROFS, it can be seen as a hash tree like dm-verity or just simply CRC32 arrays for user to choise.

Thanks,
Gao Xiang

Quoted 2 lines

NOTE:This is an experimental patch just to demonstrate the purpose. The

patch

Quoted 1 line

lacks a lot of things like coding standard, and new code runs in the

context

include/erofs/io.h static inline int blk_write(const void *buf,

Quoted 24 lines

of mkfs itself.kindly ignore it.

kindly provide your feedback on this.

---
 include/erofs/io.h |  8 ++++++++
 lib/io.c           | 27 +++++++++++++++++++++++++++
 mkfs/main.c        | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 71 insertions(+)
include/erofs/io.h +1 -0
index 4b574bd..e91d6ee 100644
@@ -18,6 +18,7 @@
 
 int dev_open(const char *devname);
 void dev_close(void);
+int dev_read(void *buf, u64 offset, size_t len);
 int dev_write(const void *buf, u64 offset, size_t len);
 int dev_fillzero(u64 offset, size_t len, bool padding);
 int dev_fsync(void);
@@ -30,5 +31,12 @@ static inline int blk_write(const void *buf,

erofs_blk_t blkaddr,

Quoted 24 lines
include/erofs/io.h +7 -0
                       blknr_to_addr(nblocks));
 }
 
+static inline int blk_read(void *buf, erofs_blk_t blkaddr,
+                        u32 nblocks)
+{
+     return dev_read(buf, blknr_to_addr(blkaddr),
+                     blknr_to_addr(nblocks));
+}
+
 #endif
 
lib/io.c +4 -0
index 15c5a35..87d7d6c 100644
@@ -109,6 +109,33 @@ u64 dev_length(void)
      return erofs_devsz;
 }
 
+int dev_read(void *buf, u64 offset, size_t len)
+{
+     int ret;
+

lib/io.c u64 dev_length(void) · line 125

Quoted 10 lines
lib/io.c +10 -0
+     if (cfg.c_dry_run)
+             return 0;
+
+     if (!buf) {
+             erofs_err("buf is NULL");
+             return -EINVAL;
+     }
+     if (offset >= erofs_devsz || len > erofs_devsz ||
+         offset > erofs_devsz - len) {
+             erofs_err("read posion[%" PRIu64 ", %zd] is too large

beyond the end of device(%" PRIu64 ").",

lib/io.c u64 dev_length(void)

Quoted 7 lines
lib/io.c +7 -0
+                       offset, len, erofs_devsz);
+             return -EINVAL;
+     }
+
+     ret = pread64(erofs_devfd, buf, len, (off64_t)offset);
+     if (ret != (int)len) {
+             erofs_err("Failed to read data from device - %s:[%" PRIu64

", %zd].",

mkfs/main.c int erofs_mkfs_update_super_block(struct

Quoted 14 lines
lib/io.c +6 -0
+                       erofs_devname, offset, len);
+             return -errno;
+     }
+     return 0;
+}
+
 int dev_write(const void *buf, u64 offset, size_t len)
 {
      int ret;
mkfs/main.c +0 -0
index f127fe1..109486e 100644
@@ -182,6 +182,41 @@ int erofs_mkfs_update_super_block(struct

erofs_buffer_head *bh,

Quoted 52 lines
mkfs/main.c +36 -1
      return 0;
 }
 
+void erofs_dump_super(char *img_path)
+{
+     struct erofs_super_block *sb;
+     char buf[EROFS_BLKSIZ];
+     unsigned int blksz;
+     int ret = 0;
+
+     if (img_path == NULL) {
+             erofs_err("image path cannot be null");
+             return;
+     }
+     ret = blk_read(buf, 0, 1);
+     if (ret) {
+             erofs_err("error reading super-block structure");
+             return;
+     }
+
+     sb = (struct erofs_super_block *)((u8 *)buf + EROFS_SUPER_OFFSET);
+     if (le32_to_cpu(sb->magic) != EROFS_SUPER_MAGIC_V1) {
+             erofs_err("not a erofs image");
+             return;
+     }
+
+     erofs_dump("magic: 0x%x\n", le32_to_cpu(sb->magic));
+     blksz = 1 << sb->blkszbits;
+     erofs_dump("block size: %d\n", blksz);
+     erofs_dump("root inode: %d\n", le32_to_cpu(sb->root_nid));
+     erofs_dump("inodes: %llu\n", le64_to_cpu(sb->inos));
+     erofs_dump("build time: %u\n", le32_to_cpu(sb->build_time));
+     erofs_dump("blocks: %u\n", le32_to_cpu(sb->blocks));
+     erofs_dump("meta block: %u\n", le32_to_cpu(sb->meta_blkaddr));
+     erofs_dump("xattr block: %u\n", le32_to_cpu(sb->xattr_blkaddr));
+     erofs_dump("requirements: 0x%x\n", le32_to_cpu(sb->requirements));
+}
+
 int main(int argc, char **argv)
 {
      int err = 0;
@@ -268,6 +303,7 @@ int main(int argc, char **argv)
              err = -EIO;
 exit:
      z_erofs_compress_exit();
+     erofs_dump_super("dummy");
      dev_close();
      erofs_exit_configure();
 
--

2.9.3

#4 lore raw
Quoted 7 lines · Gao Xiang via Linux-erofs

Hi Pratik,

Quoted 4 lines · Pratik Shinde

Thanks Gao,

Let me know when we can work on debug utility OR atleast list out things we want to achieve through the utility.

Sorry, it seems I misunderstand that, I think I will do a prelimitary parsing version this weekend, but you can develop it now :) and then we can merge our code later, that's also fine of course.

Thanks,
Gao Xiang

Quoted 214 lines

I think we need to implement
- dump super block
- dump a specfic file
- dump a physical block or a physical compressed cluster from nid and logical offset
- dump a specfic metadata (I mean dump the inode meta of a file)
- ...

Thanks,
Gao Xiang

Quoted 203 lines

Regarding the superblock checksum calculations, Yes I will dedicate sometime this week for it. will start exploring it.

--Pratik.

Quoted 196 lines · Gao Xiang

Hi Pratik,

Quoted 3 lines · Pratik Shinde

Hello Maintainers,

After going through the recent mail thread between linux's filesystem

folks

Quoted 1 line

on erofs channel, I felt erofs needs an interactive debug utility (like

xfs_db)

Quoted 1 line

which can be used to examine erofs images & can also be used to inject

errors OR

Quoted 1 line

fuzzing for testing purpose & dumping different erofs meta data

structures

Quoted 2 lines

for debugging.
In order to demonstrate above I wrote an experimental patch that simply

dumps

Quoted 1 line

the superblock of an image after mkfs completes.the full fletch utility

will run

Quoted 1 line

independently and be able to seek / print / modify any byte of an erofs

image,

Quoted 1 line

dump structures/lists/directory content of an image.

Yes, I think we really need that interactive tools, actually I'm stuggling in
modifing Guifu's erofs-fuse now, we need to add the parsing ability to "lib/"
first.

I mean, first, I will add a "fuse" field to "cfg". If it is false, it will generate a image, or it will parse a image...
And then we need to add parsing logic into "lib/" as well, and use "if (cfg.fuse)" to differnate whether it should read or write data.

That is my prelimitary thought.. I will work on this framework in this weekend.
and then we can work together on it. :)

p.s. Pratik, if you have some time, could you take some extra time adding the
super checksum calulation to EROFS? I mean we can add EROFS_FEATURE_SB_CHKSUM
to the compat superblock field ("features"), and do crc32_le on kernel and mkfs...
If you dont have time, I will do it later instead... (since we are using EROFS
on the top of dm-verity, but completing the superblock chksum is also a good idea.)

And then we can add block-based verification layer to EROFS, it can be seen as a hash tree like dm-verity or just simply CRC32 arrays for user to choise.

Thanks,
Gao Xiang

Quoted 2 lines

NOTE:This is an experimental patch just to demonstrate the purpose. The

patch

Quoted 1 line

lacks a lot of things like coding standard, and new code runs in the

context

include/erofs/io.h static inline int blk_write(const void *buf,

Quoted 24 lines

of mkfs itself.kindly ignore it.

kindly provide your feedback on this.

---
 include/erofs/io.h |  8 ++++++++
 lib/io.c           | 27 +++++++++++++++++++++++++++
 mkfs/main.c        | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 71 insertions(+)
include/erofs/io.h +1 -0
index 4b574bd..e91d6ee 100644
@@ -18,6 +18,7 @@
 
 int dev_open(const char *devname);
 void dev_close(void);
+int dev_read(void *buf, u64 offset, size_t len);
 int dev_write(const void *buf, u64 offset, size_t len);
 int dev_fillzero(u64 offset, size_t len, bool padding);
 int dev_fsync(void);
@@ -30,5 +31,12 @@ static inline int blk_write(const void *buf,

erofs_blk_t blkaddr,

Quoted 24 lines
include/erofs/io.h +7 -0
                       blknr_to_addr(nblocks));
 }
 
+static inline int blk_read(void *buf, erofs_blk_t blkaddr,
+                        u32 nblocks)
+{
+     return dev_read(buf, blknr_to_addr(blkaddr),
+                     blknr_to_addr(nblocks));
+}
+
 #endif
 
lib/io.c +4 -0
index 15c5a35..87d7d6c 100644
@@ -109,6 +109,33 @@ u64 dev_length(void)
      return erofs_devsz;
 }
 
+int dev_read(void *buf, u64 offset, size_t len)
+{
+     int ret;
+

lib/io.c u64 dev_length(void) · line 125

Quoted 10 lines
lib/io.c +10 -0
+     if (cfg.c_dry_run)
+             return 0;
+
+     if (!buf) {
+             erofs_err("buf is NULL");
+             return -EINVAL;
+     }
+     if (offset >= erofs_devsz || len > erofs_devsz ||
+         offset > erofs_devsz - len) {
+             erofs_err("read posion[%" PRIu64 ", %zd] is too large

beyond the end of device(%" PRIu64 ").",

lib/io.c u64 dev_length(void)

Quoted 7 lines
lib/io.c +7 -0
+                       offset, len, erofs_devsz);
+             return -EINVAL;
+     }
+
+     ret = pread64(erofs_devfd, buf, len, (off64_t)offset);
+     if (ret != (int)len) {
+             erofs_err("Failed to read data from device - %s:[%" PRIu64

", %zd].",

mkfs/main.c int erofs_mkfs_update_super_block(struct

Quoted 14 lines
lib/io.c +6 -0
+                       erofs_devname, offset, len);
+             return -errno;
+     }
+     return 0;
+}
+
 int dev_write(const void *buf, u64 offset, size_t len)
 {
      int ret;
mkfs/main.c +0 -0
index f127fe1..109486e 100644
@@ -182,6 +182,41 @@ int erofs_mkfs_update_super_block(struct

erofs_buffer_head *bh,

Quoted 52 lines
mkfs/main.c +36 -1
      return 0;
 }
 
+void erofs_dump_super(char *img_path)
+{
+     struct erofs_super_block *sb;
+     char buf[EROFS_BLKSIZ];
+     unsigned int blksz;
+     int ret = 0;
+
+     if (img_path == NULL) {
+             erofs_err("image path cannot be null");
+             return;
+     }
+     ret = blk_read(buf, 0, 1);
+     if (ret) {
+             erofs_err("error reading super-block structure");
+             return;
+     }
+
+     sb = (struct erofs_super_block *)((u8 *)buf + EROFS_SUPER_OFFSET);
+     if (le32_to_cpu(sb->magic) != EROFS_SUPER_MAGIC_V1) {
+             erofs_err("not a erofs image");
+             return;
+     }
+
+     erofs_dump("magic: 0x%x\n", le32_to_cpu(sb->magic));
+     blksz = 1 << sb->blkszbits;
+     erofs_dump("block size: %d\n", blksz);
+     erofs_dump("root inode: %d\n", le32_to_cpu(sb->root_nid));
+     erofs_dump("inodes: %llu\n", le64_to_cpu(sb->inos));
+     erofs_dump("build time: %u\n", le32_to_cpu(sb->build_time));
+     erofs_dump("blocks: %u\n", le32_to_cpu(sb->blocks));
+     erofs_dump("meta block: %u\n", le32_to_cpu(sb->meta_blkaddr));
+     erofs_dump("xattr block: %u\n", le32_to_cpu(sb->xattr_blkaddr));
+     erofs_dump("requirements: 0x%x\n", le32_to_cpu(sb->requirements));
+}
+
 int main(int argc, char **argv)
 {
      int err = 0;
@@ -268,6 +303,7 @@ int main(int argc, char **argv)
              err = -EIO;
 exit:
      z_erofs_compress_exit();
+     erofs_dump_super("dummy");
      dev_close();
      erofs_exit_configure();
 
--

2.9.3