diff mbox series

[RFC,04/17] ubifs: repair: Add valid nodes into file

Message ID 20231228014112.2836317-5-chengzhihao1@huawei.com
State Changes Requested
Headers show
Series ubifs: Add filesystem repair support | expand

Commit Message

Zhihao Cheng Dec. 28, 2023, 1:40 a.m. UTC
This is the 4/13 step of repairing. Generate file according to left valid
inode nodes and dentry nodes. Based on results from step 3, it is easy to
understand:

Step 3 has done:
 valid_inos - del_inos = left_inos
 valid_dents - del_dents = left_dents
Step 4 should do:
 Traverse left_inos and left_dents, insert inode/dentry nodes into
 corresponding file.

Now, all files are generated by scanning, the next thing to do is
dropping invalid files(eg. nonconsistent file type between inode node and
dentry nodes, file has no dentry nodes(excepts '/'), encrypted file has
no xattr information, etc.).

Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com>
---
 fs/ubifs/repair.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
diff mbox series

Patch

diff --git a/fs/ubifs/repair.c b/fs/ubifs/repair.c
index d932c16ec893..7a1732ef903f 100644
--- a/fs/ubifs/repair.c
+++ b/fs/ubifs/repair.c
@@ -1043,6 +1043,62 @@  static void remove_del_nodes(struct ubifs_info *c, struct scanned_info *si)
 	}
 }
 
+/**
+ * add_valid_nodes_into_file - add valid nodes into file.
+ * @c: UBIFS file-system description object
+ * @si: records nodes and files information during scanning
+ *
+ * This function adds valid nodes into corresponding file, all valid ino/dent
+ * nodes will be removed from @si->valid_inos/@si->valid_dents if the function
+ * is executed successfully.
+ */
+static int add_valid_nodes_into_file(struct ubifs_info *c,
+				     struct scanned_info *si)
+{
+	int err, type;
+	ino_t inum;
+	struct scanned_node *sn;
+	struct scanned_ino_node *ino_node;
+	struct scanned_dent_node *dent_node;
+	struct rb_node *this;
+
+	this = rb_first(&si->valid_inos);
+	while (this) {
+		cond_resched();
+		ino_node = rb_entry(this, struct scanned_ino_node, rb);
+		this = rb_next(this);
+
+		sn = (struct scanned_node *)ino_node;
+		type = key_type(c, &ino_node->key);
+		inum = key_inum(c, &ino_node->key);
+		err = insert_or_update_file(c, sn, type, inum);
+		if (err)
+			return err;
+
+		rb_erase(&ino_node->rb, &si->valid_inos);
+		kfree(ino_node);
+	}
+
+	this = rb_first(&si->valid_dents);
+	while (this) {
+		cond_resched();
+		dent_node = rb_entry(this, struct scanned_dent_node, rb);
+		this = rb_next(this);
+
+		sn = (struct scanned_node *)dent_node;
+		inum = dent_node->inum;
+		type = key_type(c, &dent_node->key);
+		err = insert_or_update_file(c, sn, type, inum);
+		if (err)
+			return err;
+
+		rb_erase(&dent_node->rb, &si->valid_dents);
+		kfree(dent_node);
+	}
+
+	return 0;
+}
+
 static int do_repair(struct ubifs_info *c)
 {
 	int err = 0;
@@ -1060,6 +1116,10 @@  static int do_repair(struct ubifs_info *c)
 	ubifs_msg(c, "Step 3: Remove deleted nodes");
 	remove_del_nodes(c, &si);
 
+	/* Step 4: Add valid nodes into file. */
+	ubifs_msg(c, "Step 4: Add valid nodes into file");
+	err = add_valid_nodes_into_file(c, &si);
+
 out:
 	destroy_scanned_info(c, &si);
 	return err;