[PATCH v2 24/27] ubifs: ubifs_dump: dump index area

Dongsheng Yang yangds.fnst at cn.fujitsu.com
Wed Oct 14 21:04:47 PDT 2015


Scan the whole index tree and dump each index node.

Signed-off-by: Dongsheng Yang <yangds.fnst at cn.fujitsu.com>
---
 ubifs-utils/ubifs_dump/ubifs_dump.c | 95 +++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)

diff --git a/ubifs-utils/ubifs_dump/ubifs_dump.c b/ubifs-utils/ubifs_dump/ubifs_dump.c
index 4b255e3..e72b8dd 100644
--- a/ubifs-utils/ubifs_dump/ubifs_dump.c
+++ b/ubifs-utils/ubifs_dump/ubifs_dump.c
@@ -883,6 +883,97 @@ static int dump_lpt(void)
 	return 0;
 }
 
+/**
+ * ubifs_read_node - read node.
+ * @c: UBIFS file-system description object
+ * @buf: buffer to read to
+ * @type: node type
+ * @len: node length (not aligned)
+ * @lnum: logical eraseblock number
+ * @offs: offset within the logical eraseblock
+ *
+ * This function reads a node of known type and and length, checks it and
+ * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
+ * and a negative error code in case of failure.
+ */
+int ubifs_read_node(const struct ubifs_info *c, void *buf, int len,
+		    int lnum, loff_t offs)
+{
+	int err;
+
+	ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
+	ubifs_assert(len >= UBIFS_CH_SZ && offs + len <= c->leb_size);
+	ubifs_assert(!(offs & 7) && offs < c->leb_size);
+	ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
+
+	err = ubifs_read((loff_t)lnum * c->leb_size + offs, len, buf);
+	if (err && err != -EBADMSG)
+		return err;
+
+	return 0;
+}
+
+static int dump_zbr(struct ubifs_info *c, struct ubifs_zbranch *zbr)
+{
+	int i = 0;
+	int err = 0;
+	struct ubifs_idx_node *idx;
+	void *buf;
+
+	buf = malloc(zbr->len);
+	if (!buf)
+		return -ENOMEM;
+
+	err = ubifs_read_node(c, buf, zbr->len, zbr->lnum, zbr->offs);
+	if (err)
+		goto out;
+
+	dump_node(c, buf);
+
+	if (((struct ubifs_ch *)buf)->node_type != UBIFS_IDX_NODE)
+		goto out;
+
+	idx = ((struct ubifs_idx_node *)buf);
+	for (i = 0; i < le16_to_cpu(idx->child_cnt); i++) {
+		struct ubifs_branch *br;
+		struct ubifs_zbranch child;
+
+		br = ubifs_idx_branch(c, idx, i);
+		child.lnum = le32_to_cpu(br->lnum);
+		child.offs = le32_to_cpu(br->offs);
+		child.len = le32_to_cpu(br->len);
+		err = dump_zbr(c, &child);
+		if (err)
+			goto out;
+	}
+
+out:
+	free(buf);
+	return err;
+}
+
+static int dump_index()
+{
+	int err = 0;
+
+	printf("\tINDEX AREA: \n");
+	err = dump_zbr(c, &c->zroot);
+	if (err)
+		return err;
+	return 0;
+}
+
+static int dump_main()
+{
+	int err = 0;
+	printf("MAIN AREA: \n");
+	err = dump_index();
+	if (err)
+		return err;
+
+	return 0;
+}
+
 static int dump()
 {
 	int err = 0;
@@ -908,6 +999,10 @@ static int dump()
 	err = dump_lpt();
 	if (err)
 		goto free;
+
+	err = dump_main();
+	if (err)
+		goto free;
 free:
 	free(leb_buf);
 out:
-- 
1.8.4.2




More information about the linux-mtd mailing list