linux-f2fs-devel.lists.sourceforge.net archive mirror
 help / color / mirror / Atom feed
From: Chao Yu <chao@kernel.org>
To: jaegeuk@kernel.org
Cc: linux-f2fs-devel@lists.sourceforge.net
Subject: [f2fs-dev] [PATCH 2/4] fsck.f2fs: use f2fs_is_valid_blkaddr()
Date: Fri, 12 May 2023 18:03:52 +0800	[thread overview]
Message-ID: <20230512100354.4072489-2-chao@kernel.org> (raw)
In-Reply-To: <20230512100354.4072489-1-chao@kernel.org>

use f2fs_is_valid_blkaddr() instead of IS_VALID_BLK_ADDR() to
check validity of data/node's block address.

use is_valid_data_blkaddr() in sanity_check_nid() to check NULL_ADDR
as NEW_ADDR, and print the value in error path explicitly.

Signed-off-by: Chao Yu <chao@kernel.org>
---
 fsck/dump.c  |  5 +++--
 fsck/f2fs.h  | 15 +--------------
 fsck/fsck.c  | 23 +++++++++++++----------
 fsck/fsck.h  |  2 ++
 fsck/mount.c |  4 +++-
 5 files changed, 22 insertions(+), 27 deletions(-)

diff --git a/fsck/dump.c b/fsck/dump.c
index b8f6144..dd1f0ab 100644
--- a/fsck/dump.c
+++ b/fsck/dump.c
@@ -278,7 +278,8 @@ static void dump_data_blk(struct f2fs_sb_info *sbi, __u64 offset, u32 blkaddr)
 		return;
 
 	/* get data */
-	if (blkaddr == NEW_ADDR || !IS_VALID_BLK_ADDR(sbi, blkaddr)) {
+	if (blkaddr == NEW_ADDR ||
+			!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
 		memset(buf, 0, F2FS_BLKSIZE);
 	} else {
 		int ret;
@@ -588,7 +589,7 @@ int dump_node(struct f2fs_sb_info *sbi, nid_t nid, int force)
 	DBG(1, "nat_entry.version     [0x%x]\n", ni.version);
 	DBG(1, "nat_entry.ino         [0x%x]\n", ni.ino);
 
-	if (!IS_VALID_BLK_ADDR(sbi, ni.blk_addr)) {
+	if (!f2fs_is_valid_blkaddr(sbi, ni.blk_addr, DATA_GENERIC)) {
 		MSG(force, "Invalid node blkaddr: %u\n\n", ni.blk_addr);
 		goto out;
 	}
diff --git a/fsck/f2fs.h b/fsck/f2fs.h
index e65644e..9791071 100644
--- a/fsck/f2fs.h
+++ b/fsck/f2fs.h
@@ -108,6 +108,7 @@ enum {
 	META_SSA,
 	META_MAX,
 	META_POR,
+	DATA_GENERIC,
 };
 
 #define MAX_RA_BLOCKS	64
@@ -520,20 +521,6 @@ static inline bool IS_VALID_NID(struct f2fs_sb_info *sbi, u32 nid)
 			<< (sbi->log_blocks_per_seg - 1)));
 }
 
-static inline bool IS_VALID_BLK_ADDR(struct f2fs_sb_info *sbi, u32 addr)
-{
-	if (addr == NULL_ADDR || addr == NEW_ADDR)
-		return 1;
-
-	if (addr >= le64_to_cpu(F2FS_RAW_SUPER(sbi)->block_count) ||
-				addr < SM_I(sbi)->main_blkaddr) {
-		DBG(1, "block addr [0x%x]\n", addr);
-		return 0;
-	}
-	/* next block offset will be checked at the end of fsck. */
-	return 1;
-}
-
 static inline bool is_valid_data_blkaddr(block_t blkaddr)
 {
 	if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR ||
diff --git a/fsck/fsck.c b/fsck/fsck.c
index 64db1e5..9180deb 100644
--- a/fsck/fsck.c
+++ b/fsck/fsck.c
@@ -236,7 +236,7 @@ static int is_valid_summary(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
 
 	get_node_info(sbi, nid, &ni);
 
-	if (!IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
+	if (!f2fs_is_valid_blkaddr(sbi, ni.blk_addr, DATA_GENERIC))
 		goto out;
 
 	/* read node_block */
@@ -405,12 +405,12 @@ static int sanity_check_nid(struct f2fs_sb_info *sbi, u32 nid,
 		return -EINVAL;
 	}
 
-	if (ni->blk_addr == NEW_ADDR) {
-		ASSERT_MSG("nid is NEW_ADDR. [0x%x]", nid);
+	if (!is_valid_data_blkaddr(ni->blk_addr)) {
+		ASSERT_MSG("nid->blk_addr is 0x%x. [0x%x]", ni->blk_addr, nid);
 		return -EINVAL;
 	}
 
-	if (!IS_VALID_BLK_ADDR(sbi, ni->blk_addr)) {
+	if (!f2fs_is_valid_blkaddr(sbi, ni->blk_addr, DATA_GENERIC)) {
 		ASSERT_MSG("blkaddress is not valid. [0x%x]", ni->blk_addr);
 		return -EINVAL;
 	}
@@ -676,7 +676,7 @@ void fsck_reada_node_block(struct f2fs_sb_info *sbi, u32 nid)
 
 	if (nid != 0 && IS_VALID_NID(sbi, nid)) {
 		get_node_info(sbi, nid, &ni);
-		if (IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
+		if (f2fs_is_valid_blkaddr(sbi, ni.blk_addr, DATA_GENERIC))
 			dev_reada_block(ni.blk_addr);
 	}
 }
@@ -1612,7 +1612,8 @@ static int __chk_dentries(struct f2fs_sb_info *sbi, int casefolded,
 			struct node_info ni;
 
 			get_node_info(sbi, ino, &ni);
-			if (IS_VALID_BLK_ADDR(sbi, ni.blk_addr)) {
+			if (f2fs_is_valid_blkaddr(sbi, ni.blk_addr,
+							DATA_GENERIC)) {
 				dev_reada_block(ni.blk_addr);
 				name_len = le16_to_cpu(dentry[i].name_len);
 				if (name_len > 0)
@@ -1867,7 +1868,7 @@ int fsck_chk_data_blk(struct f2fs_sb_info *sbi, int casefolded,
 		return 0;
 	}
 
-	if (!IS_VALID_BLK_ADDR(sbi, blk_addr)) {
+	if (!f2fs_is_valid_blkaddr(sbi, blk_addr, DATA_GENERIC)) {
 		ASSERT_MSG("blkaddress is not valid. [0x%x]", blk_addr);
 		return -EINVAL;
 	}
@@ -1939,7 +1940,8 @@ int fsck_chk_orphan_node(struct f2fs_sb_info *sbi)
 			if (c.preen_mode == PREEN_MODE_1 && !c.fix_on) {
 				get_node_info(sbi, ino, &ni);
 				if (!IS_VALID_NID(sbi, ino) ||
-				    !IS_VALID_BLK_ADDR(sbi, ni.blk_addr)) {
+					!f2fs_is_valid_blkaddr(sbi, ni.blk_addr,
+								DATA_GENERIC)) {
 					free(orphan_blk);
 					free(new_blk);
 					return -EINVAL;
@@ -1997,7 +1999,8 @@ int fsck_chk_quota_node(struct f2fs_sb_info *sbi)
 		if (c.preen_mode == PREEN_MODE_1 && !c.fix_on) {
 			get_node_info(sbi, ino, &ni);
 			if (!IS_VALID_NID(sbi, ino) ||
-					!IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
+				!f2fs_is_valid_blkaddr(sbi, ni.blk_addr,
+							DATA_GENERIC))
 				return -EINVAL;
 			continue;
 		}
@@ -2136,7 +2139,7 @@ int fsck_chk_meta(struct f2fs_sb_info *sbi)
 			 */
 			continue;
 
-		if (!IS_VALID_BLK_ADDR(sbi, blk)) {
+		if (!f2fs_is_valid_blkaddr(sbi, blk, DATA_GENERIC)) {
 			MSG(0, "\tError: nat entry[ino %u block_addr 0x%x]"
 				" is in valid\n",
 				ino, blk);
diff --git a/fsck/fsck.h b/fsck/fsck.h
index 02dcff8..89b1c6e 100644
--- a/fsck/fsck.h
+++ b/fsck/fsck.h
@@ -216,6 +216,8 @@ extern int f2fs_set_sit_bitmap(struct f2fs_sb_info *, u32);
 extern void fsck_init(struct f2fs_sb_info *);
 extern int fsck_verify(struct f2fs_sb_info *);
 extern void fsck_free(struct f2fs_sb_info *);
+extern bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
+					block_t blkaddr, int type);
 extern int f2fs_ra_meta_pages(struct f2fs_sb_info *, block_t, int, int);
 extern int f2fs_do_mount(struct f2fs_sb_info *);
 extern void f2fs_do_umount(struct f2fs_sb_info *);
diff --git a/fsck/mount.c b/fsck/mount.c
index 16c98cc..f0b0072 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -767,6 +767,7 @@ bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
 			return 0;
 		break;
 	case META_POR:
+	case DATA_GENERIC:
 		if (blkaddr >= MAX_BLKADDR(sbi) ||
 			blkaddr < MAIN_BLKADDR(sbi))
 			return 0;
@@ -1596,7 +1597,8 @@ static int f2fs_early_init_nid_bitmap(struct f2fs_sb_info *sbi)
 		block_t addr;
 
 		addr = le32_to_cpu(nat_in_journal(journal, i).block_addr);
-		if (!IS_VALID_BLK_ADDR(sbi, addr)) {
+		if (addr != NULL_ADDR &&
+			!f2fs_is_valid_blkaddr(sbi, addr, DATA_GENERIC)) {
 			MSG(0, "\tError: f2fs_init_nid_bitmap: addr(%u) is invalid!!!\n", addr);
 			journal->n_nats = cpu_to_le16(i);
 			c.fix_on = 1;
-- 
2.25.1



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

  reply	other threads:[~2023-05-12 10:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-12 10:03 [f2fs-dev] [PATCH 1/4] fsck.f2fs: wrap openned codes into fsck_sanity_check_nid() Chao Yu
2023-05-12 10:03 ` Chao Yu [this message]
2023-05-12 10:03 ` [f2fs-dev] [PATCH 3/4] fsck.f2fs: add more debug info in fsck_verify() Chao Yu
2023-05-12 10:03 ` [f2fs-dev] [PATCH 4/4] fsck.f2fs: lookup and relink root inode Chao Yu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230512100354.4072489-2-chao@kernel.org \
    --to=chao@kernel.org \
    --cc=jaegeuk@kernel.org \
    --cc=linux-f2fs-devel@lists.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).