diff mbox series

[RFC,14/17] ubifs: repair: Clean up log and orphan area

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

Commit Message

Zhihao Cheng Dec. 28, 2023, 1:41 a.m. UTC
This is the 12/13 step of repairing. Clean up log and orphan area, all
nodes have been recovered, these two areas should be cleared, otherwise
old content in journal/orphan could be replayed in next mounting.

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

Patch

diff --git a/fs/ubifs/repair.c b/fs/ubifs/repair.c
index 8d40cff26f7a..42124cda5d7d 100644
--- a/fs/ubifs/repair.c
+++ b/fs/ubifs/repair.c
@@ -2367,6 +2367,43 @@  int build_lpt(struct ubifs_info *c)
 	return ubifs_create_lpt(c, c->repair->lpts, c->main_lebs, hash_lpt);
 }
 
+/**
+ * clean_log - clean up log area.
+ * @c: UBIFS file-system description object
+ *
+ * This function cleans up log area, since there is no need to do recovery
+ * in next mounting.
+ */
+static int clean_log(struct ubifs_info *c)
+{
+	int lnum, err;
+	struct ubifs_cs_node *cs;
+
+	for (lnum = UBIFS_LOG_LNUM; lnum <= c->log_last; lnum++) {
+		if (fatal_signal_pending(current))
+			return -EINTR;
+		cond_resched();
+
+		err = ubifs_leb_unmap(c, lnum);
+		if (err)
+			return err;
+	}
+
+	cs = kzalloc(ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size), GFP_KERNEL);
+	if (!cs)
+		return -ENOMEM;
+
+	cs->ch.node_type = UBIFS_CS_NODE;
+	cs->cmt_no = cpu_to_le64(0);
+
+	err = ubifs_write_node(c, cs, UBIFS_CS_NODE_SZ, UBIFS_LOG_LNUM, 0);
+	kfree(cs);
+	if (err)
+		return err;
+
+	return 0;
+}
+
 static int do_repair(struct ubifs_info *c)
 {
 	int err = 0;
@@ -2416,6 +2453,16 @@  static int do_repair(struct ubifs_info *c)
 	/* Step 11. Build LPT. */
 	ubifs_msg(c, "Step 11: Build LPT");
 	err = build_lpt(c);
+	if (err)
+		goto out;
+
+	/* Step 12. Clean up log & orphan. */
+	ubifs_msg(c, "Step 12: Clean up log & orphan");
+	err = clean_log(c);
+	if (err)
+		goto out;
+
+	err = ubifs_clear_orphans(c);
 
 out:
 	destroy_scanned_info(c, &si);