Skip to contentSkip to conversation

fsck.f2fs: fix to detect double '.' or '..'

PATCH New Chao Yu via Linux-f2fs-devel 1 participant 2 messages Sep 4, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 fsck.f2fs: fix to detect double '.' or '..'

If there are double '.' or '..' dirents in directory, fsck.f2fs won't detect and repaire the issue correctly, fix it.

---
 fsck/fsck.c       | 55 +++++++++++++++++++++++++++++++----------------
 fsck/fsck.h       |  3 ++-
 include/f2fs_fs.h |  6 ++++++
 3 files changed, 45 insertions(+), 19 deletions(-)
fsck/fsck.c +37 -18
index a18bee9..54ddc11 100644
@@ -1307,10 +1307,10 @@ skip_blkcnt_fix:
 						nid, i_links, child.links);
 			}
 		}
-		if (child.dots < 2 &&
+		if ((child.dot == 0 || child.dotdot == 0) &&
 				!(node_blk->i.i_inline & F2FS_INLINE_DOTS)) {
-			ASSERT_MSG("ino: 0x%x dots: %u",
-					nid, child.dots);
+			ASSERT_MSG("ino: 0x%x dot: %u, dotdot: %u",
+					nid, child.dot, child.dotdot);
 			if (c.fix_on) {
 				node_blk->i.i_inline |= F2FS_INLINE_DOTS;
 				need_fix = 1;
@@ -1862,26 +1862,45 @@ static int __chk_dentries(struct f2fs_sb_info *sbi, int casefolded,
 
 		/* Becareful. 'dentry.file_type' is not imode. */
 		if (ftype == F2FS_FT_DIR) {
-			if ((name[0] == '.' && name_len == 1) ||
-				(name[0] == '.' && name[1] == '.' &&
-							name_len == 2)) {
-				ret = __chk_dots_dentries(sbi, casefolded, &dentry[i],
-					child, name, name_len, &filenames[i],
-					enc_name);
-				switch (ret) {
-				case 1:
+			enum dot_type dot_type = NON_DOT;
+
+			if (name[0] == '.' && name_len == 1)
+				dot_type = TYPE_DOT;
+			else if (name[0] == '.' && name[1] == '.' &&
+						name_len == 2)
+				dot_type = TYPE_DOTDOT;
+
+			if (dot_type != NON_DOT) {
+				bool need_del = false;
+
+				DBG(3, "i:%u, dot_type:%u, ino:%u, p:%u, pp:%u\n",
+					i, dot_type, dentry[i].ino,
+					child->p_ino, child->pp_ino);
+
+				ret = __chk_dots_dentries(sbi, casefolded,
+					&dentry[i], child, name, name_len,
+					&filenames[i], enc_name);
+				if (ret)
 					fixed = 1;
-					fallthrough;
-				case 0:
-					child->dots++;
-					break;
+
+				if (dot_type == TYPE_DOT) {
+					if (child->dot == 0)
+						child->dot++;
+					else
+						need_del = true;
+				} else if (dot_type == TYPE_DOTDOT) {
+					if (child->dotdot == 0)
+						child->dotdot++;
+					else
+						need_del = true;
 				}
 
-				if (child->dots > 2) {
-					ASSERT_MSG("More than one '.' or '..', should delete the extra one\n");
+				if (need_del) {
+					ASSERT_MSG("More than one '%s', should delete the extra one, i: %u, ino:%u",
+						dot_type == TYPE_DOT ? "." : "..",
+						i, dentry[i].ino);
 					nullify_dentry(&dentry[i], i,
 						       &filenames[i], &bitmap);
-					child->dots--;
 					fixed = 1;
 				}
 
fsck/fsck.h +2 -1
index a8f187e..efccfbc 100644
@@ -70,7 +70,8 @@ struct child_info {
 	u32 links;
 	u32 files;
 	u32 pgofs;
-	u8 dots;
+	u8 dot;
+	u8 dotdot;
 	u8 dir_level;
 	u32 p_ino;		/* parent ino */
 	char p_name[F2FS_NAME_LEN + 1]; /* parent name */
include/f2fs_fs.h +6 -0
index 15a1c82..0b6777a 100644
@@ -1429,6 +1429,12 @@ enum FILE_TYPE {
 	F2FS_FT_LAST_FILE_TYPE = F2FS_FT_XATTR,
 };
 
+enum dot_type {
+	NON_DOT,
+	TYPE_DOT,
+	TYPE_DOTDOT
+};
+
 #define LINUX_S_IFMT  00170000
 #define LINUX_S_IFREG  0100000
 #define LINUX_S_IFDIR  0040000
Signature · 8 lines
2.40.1



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#1 lore raw

PATCH 2/2 fsck.f2fs: support to add missing '.' or '..' dirent

Previously, once fsck.f2fs detects that directory missed '.' or '..' dirent, it tags inode w/ F2FS_INLINE_DOTS flag, and expects f2fs kernel module can repaire it online during it runs to lookup() on target directory.

This patch abandons previous implementation, and do repaires directly.

---
 fsck/fsck.c | 45 ++++++++++++++++++++++++++++++++++++++++-----
 fsck/main.c |  4 +++-
 2 files changed, 43 insertions(+), 6 deletions(-)
fsck/fsck.c +40 -5
index 54ddc11..f8ea7f0 100644
@@ -1307,17 +1307,52 @@ skip_blkcnt_fix:
 						nid, i_links, child.links);
 			}
 		}
-		if ((child.dot == 0 || child.dotdot == 0) &&
-				!(node_blk->i.i_inline & F2FS_INLINE_DOTS)) {
-			ASSERT_MSG("ino: 0x%x dot: %u, dotdot: %u",
+		if (child.dot == 0 || child.dotdot == 0) {
+			ASSERT_MSG("ino: 0x%x has no '.' and/or '..' dirents, dot: %u, dotdot: %u",
 					nid, child.dot, child.dotdot);
 			if (c.fix_on) {
-				node_blk->i.i_inline |= F2FS_INLINE_DOTS;
+				umode_t mode = le16_to_cpu(node_blk->i.i_mode);
+				block_t blkaddr;
+
+				ret = convert_inline_dentry(sbi, node_blk,
+								&ni->blk_addr);
+				FIX_MSG("convert inline dentry ino: %u, pino: %u, ret: %d",
+						nid, child_d->p_ino, ret);
+				if (ret)
+					goto skip_dot_fix;
+
+				if (child.dot == 0) {
+					char *name = ".";
+
+					ret = f2fs_add_link(sbi, node_blk,
+						(const unsigned char *)name,
+						1, nid, map_de_type(mode),
+						&blkaddr, 0);
+					FIX_MSG("add missing '%s' dirent in ino: %u, pino: %u, ret:%d",
+						name, nid, child_d->p_ino, ret);
+					if (ret)
+						goto skip_dot_fix;
+				}
+
+				if (child.dotdot == 0) {
+					char *name = "..";
+
+					ret = f2fs_add_link(sbi, node_blk,
+						(const unsigned char *)name,
+						2, child_d->p_ino,
+						map_de_type(mode),
+						&blkaddr, 0);
+					FIX_MSG("add missing '%s' dirent in ino: %u, pino: %u, ret:%d",
+						name, nid, child_d->p_ino, ret);
+					if (ret)
+						goto skip_dot_fix;
+				}
+
 				need_fix = 1;
-				FIX_MSG("Dir: 0x%x set inline_dots", nid);
 			}
 		}
 	}
+skip_dot_fix:
 
 	i_gc_failures = le16_to_cpu(node_blk->i.i_gc_failures);
 
fsck/main.c +3 -1
index 8881936..4e0254d 100644
@@ -892,6 +892,7 @@ static int do_fsck(struct f2fs_sb_info *sbi)
 	u32 flag = le32_to_cpu(ckpt->ckpt_flags);
 	u32 blk_cnt;
 	struct f2fs_compr_blk_cnt cbc;
+	struct child_info child = { 0 };
 	errcode_t ret;
 
 	fsck_init(sbi);
@@ -957,8 +958,9 @@ static int do_fsck(struct f2fs_sb_info *sbi)
 	if (fsck_sanity_check_nat(sbi, sbi->root_ino_num))
 		fsck_chk_root_inode(sbi);
 
+	child.p_ino = sbi->root_ino_num;
 	fsck_chk_node_blk(sbi, NULL, sbi->root_ino_num,
-			F2FS_FT_DIR, TYPE_INODE, &blk_cnt, &cbc, NULL);
+			F2FS_FT_DIR, TYPE_INODE, &blk_cnt, &cbc, &child);
 	fsck_chk_quota_files(sbi);
 
 	ret = fsck_verify(sbi);
Signature · 8 lines
2.40.1



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel