aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Rosenberg <drosen@google.com>2023-08-28 18:05:31 -0700
committerJaegeuk Kim <jaegeuk@kernel.org>2023-09-07 14:17:00 -0700
commitb1aeb99ec014d38af45a88bc0058d4312d0f9af4 (patch)
tree8cf669bd649436302dbc6ff61b7c1457ded88f81
parent30825b3813510ed8669e6be6ca791174785f47d7 (diff)
downloadf2fs-tools-b1aeb99ec014d38af45a88bc0058d4312d0f9af4.tar.gz
f2fs-tools: Refactor Orphan Block struct
This splits off access to the orphan block's footer into a macro call as its location is dependent on block size. Because of this, you should use F2FS_BLKSIZE instead of sizeof(struct f2fs_orphan_block) Signed-off-by: Daniel Rosenberg <drosen@google.com> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
-rw-r--r--fsck/fsck.c4
-rw-r--r--fsck/main.c1
-rw-r--r--include/f2fs_fs.h23
-rw-r--r--mkfs/f2fs_format_main.c1
4 files changed, 24 insertions, 5 deletions
diff --git a/fsck/fsck.c b/fsck/fsck.c
index a30719e..5cdf92d 100644
--- a/fsck/fsck.c
+++ b/fsck/fsck.c
@@ -2058,7 +2058,7 @@ int fsck_chk_orphan_node(struct f2fs_sb_info *sbi)
u32 new_entry_count = 0;
ASSERT(ret >= 0);
- entry_count = le32_to_cpu(orphan_blk->entry_count);
+ entry_count = le32_to_cpu(F2FS_ORPHAN_BLOCK_FOOTER(orphan_blk)->entry_count);
for (j = 0; j < entry_count; j++) {
nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
@@ -2094,7 +2094,7 @@ int fsck_chk_orphan_node(struct f2fs_sb_info *sbi)
}
if (f2fs_dev_is_writable() && c.fix_on &&
entry_count != new_entry_count) {
- new_blk->entry_count = cpu_to_le32(new_entry_count);
+ F2FS_ORPHAN_BLOCK_FOOTER(new_blk)->entry_count = cpu_to_le32(new_entry_count);
ret = dev_write_block(new_blk, start_blk + i);
ASSERT(ret >= 0);
}
diff --git a/fsck/main.c b/fsck/main.c
index dd8643c..6d48d40 100644
--- a/fsck/main.c
+++ b/fsck/main.c
@@ -803,6 +803,7 @@ void f2fs_parse_options(int argc, char *argv[])
return;
}
+ check_block_struct_sizes();
/* print out error */
switch (err) {
case EWRONG_OPT:
diff --git a/include/f2fs_fs.h b/include/f2fs_fs.h
index 639eccf..5f12363 100644
--- a/include/f2fs_fs.h
+++ b/include/f2fs_fs.h
@@ -21,6 +21,7 @@
#define __SANE_USERSPACE_TYPES__ /* For PPC64, to get LL64 types */
#endif
+#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
@@ -862,8 +863,19 @@ static_assert(sizeof(struct f2fs_checkpoint) == 192, "");
*/
#define F2FS_ORPHANS_PER_BLOCK ((F2FS_BLKSIZE - 4 * sizeof(__le32)) / sizeof(__le32))
+/*
+ * On disk layout is:
+ * __le32 ino[F2FS_ORPHANS_PER_BLOCK];
+ * struct f2fs_ophan_block_footer
+ *
+ * Do NOT use sizeof, use F2FS_BLKSIZE instead
+ */
struct f2fs_orphan_block {
- __le32 ino[F2FS_ORPHANS_PER_BLOCK]; /* inode numbers */
+ __le32 ino[0]; /* F2FS_ORPHANS_PER_BLOCK inode numbers */
+};
+#define F2FS_ORPHAN_BLOCK_FOOTER(blk) ((struct orphan_block_footer *)&(blk)->ino[F2FS_ORPHANS_PER_BLOCK])
+
+struct orphan_block_footer {
__le32 reserved; /* reserved */
__le16 blk_addr; /* block index in current CP */
__le16 blk_count; /* Number of orphan inode blocks in CP */
@@ -871,8 +883,6 @@ struct f2fs_orphan_block {
__le32 check_sum; /* CRC32 for orphan inode block */
};
-static_assert(sizeof(struct f2fs_orphan_block) == F2FS_BLKSIZE, "");
-
/*
* For NODE structure
*/
@@ -1963,4 +1973,11 @@ extern char *f2fs_encoding2str(const int encoding);
extern int f2fs_get_encoding_flags(int encoding);
extern int f2fs_str2encoding_flags(char **param, __u16 *flags);
+static inline void check_block_struct_sizes(void)
+{
+ /* Check Orphan Block Size */
+ assert(F2FS_ORPHANS_PER_BLOCK * sizeof(__le32)
+ + sizeof(struct orphan_block_footer) == F2FS_BLKSIZE);
+}
+
#endif /*__F2FS_FS_H */
diff --git a/mkfs/f2fs_format_main.c b/mkfs/f2fs_format_main.c
index b2b84dd..3a4b18c 100644
--- a/mkfs/f2fs_format_main.c
+++ b/mkfs/f2fs_format_main.c
@@ -355,6 +355,7 @@ static void f2fs_parse_options(int argc, char *argv[])
if (c.zoned_mode)
c.feature |= F2FS_FEATURE_BLKZONED;
+ check_block_struct_sizes();
}
#ifdef HAVE_LIBBLKID