[PATCH 3/3] erofs-utils: dump: add -e option to dump the inode extent info

Guo Xuenan guoxuenan at huawei.com
Wed Oct 27 23:03:51 AEDT 2021


From: Wang Qi <mpiglet at outlook.com>

Signed-off-by: Guo Xuenan <guoxuenan at huawei.com>
Signed-off-by: Wang Qi <mpiglet at outlook.com>
---
 dump/main.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 85 insertions(+), 1 deletion(-)

diff --git a/dump/main.c b/dump/main.c
index 8a31345..e4018a7 100644
--- a/dump/main.c
+++ b/dump/main.c
@@ -18,9 +18,11 @@
 struct erofsdump_cfg {
 	unsigned int totalshow;
 	bool show_inode;
+	bool show_inode_phy;
 	bool show_superblock;
 	bool show_statistics;
 	u64 ino;
+	u64 ino_phy;
 };
 static struct erofsdump_cfg dumpcfg;
 
@@ -94,6 +96,7 @@ static void usage(void)
 	      "Dump erofs layout from IMAGE, and [options] are:\n"
 	      " -S      show statistic information of the image\n"
 	      " -V      print the version number of dump.erofs and exit.\n"
+	      " -e #    show the inode extent info of nid #\n"
 	      " -i #    show the target inode info of nid #\n"
 	      " -s      show information about superblock\n"
 	      " --help  display this help and exit.\n",
@@ -110,9 +113,14 @@ static int erofsdump_parse_options_cfg(int argc, char **argv)
 	int opt;
 	u64 i;
 
-	while ((opt = getopt_long(argc, argv, "SVi:s",
+	while ((opt = getopt_long(argc, argv, "SVi:e:s",
 				  long_options, NULL)) != -1) {
 		switch (opt) {
+		case 'e':
+			dumpcfg.show_inode_phy = true;
+			dumpcfg.ino_phy = atoll(optarg);
+			++dumpcfg.totalshow;
+			break;
 		case 'i':
 			i = atoll(optarg);
 			dumpcfg.show_inode = true;
@@ -408,6 +416,79 @@ static int erofs_get_pathname(erofs_nid_t nid, erofs_nid_t parent_nid,
 	return -1;
 }
 
+static void erofsdump_show_inode_phy(void)
+{
+	int err;
+	erofs_nid_t nid = dumpcfg.ino_phy;
+	struct erofs_inode inode = {.nid = nid};
+	char path[PATH_MAX + 1] = {0};
+
+	err = erofs_read_inode_from_disk(&inode);
+	if (err < 0) {
+		erofs_err("read inode %lu from disk failed", nid);
+		return;
+	}
+
+	const erofs_off_t ibase = iloc(inode.nid);
+	const erofs_off_t pos = Z_EROFS_VLE_LEGACY_INDEX_ALIGN(
+			ibase + inode.inode_isize + inode.xattr_isize);
+	erofs_blk_t blocks = inode.u.i_blocks;
+	erofs_blk_t start = 0;
+	erofs_blk_t end = 0;
+	unsigned int extent_count;
+	struct erofs_map_blocks map = {
+		.index = UINT_MAX,
+		.m_la = 0,
+	};
+
+	fprintf(stdout, "Inode: %lu\n", nid);
+	err = erofs_get_pathname(sbi.root_nid, sbi.root_nid, nid, path, 0);
+	if (err) {
+		erofs_err("Path not found\n");
+		return;
+	}
+	fprintf(stdout, "File path: %s\n", path);
+	fprintf(stdout, "File size: %lu\n", inode.i_size);
+
+	switch (inode.datalayout) {
+	case EROFS_INODE_FLAT_INLINE:
+	case EROFS_INODE_FLAT_PLAIN:
+		if (inode.u.i_blkaddr == NULL_ADDR)
+			start = end = erofs_blknr(pos);
+		else {
+			start = inode.u.i_blkaddr;
+			end = start + BLK_ROUND_UP(inode.i_size) - 1;
+		}
+		fprintf(stdout, "Plain blknr: %u - %u\n", start, end);
+		break;
+
+	case EROFS_INODE_FLAT_COMPRESSION_LEGACY:
+	case EROFS_INODE_FLAT_COMPRESSION:
+		err = z_erofs_map_blocks_iter(&inode, &map, 0);
+		if (err)
+			erofs_err("get file blocks range failed");
+
+		start = erofs_blknr(map.m_pa);
+		end = start - 1 + blocks;
+		fprintf(stdout,
+				"Compressed blknr: %u - %u\n", start, end);
+		extent_count = 0;
+		map.m_la = 0;
+		fprintf(stdout, "Ext:  logical_offset/length:  physical_offset/length\n");
+		while (map.m_la < inode.i_size) {
+			err = z_erofs_map_blocks_iter(&inode, &map,
+					EROFS_GET_BLOCKS_FIEMAP);
+			fprintf(stdout, "%u: ", extent_count++);
+			fprintf(stdout, "%6lu..%-6lu / %-lu: ",
+					map.m_la, map.m_la + map.m_llen, map.m_llen);
+			fprintf(stdout, "%lu..%lu / %-lu\n",
+					map.m_pa, map.m_pa + map.m_plen, map.m_plen);
+			map.m_la += map.m_llen;
+		}
+		break;
+	}
+}
+
 static void dumpfs_print_inode(void)
 {
 	int err;
@@ -645,6 +726,9 @@ int main(int argc, char **argv)
 	if (dumpcfg.show_inode)
 		dumpfs_print_inode();
 
+	if (dumpcfg.show_inode_phy)
+		erofsdump_show_inode_phy();
+
 exit:
 	erofs_exit_configure();
 	return err;
-- 
2.31.1



More information about the Linux-erofs mailing list