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 <dro...@google.com> --- fsck/fsck.c | 4 ++-- fsck/main.c | 1 + include/f2fs_fs.h | 23 ++++++++++++++++++++--- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/fsck/fsck.c b/fsck/fsck.c index 78ffdb6..e0dce16 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 3690c74..15e2e4e 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 6975143..c5b1e26 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> @@ -855,8 +856,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 */ @@ -864,8 +876,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 */ @@ -1956,4 +1966,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 */ -- 2.42.0.rc2.253.gd59a3bf2b4-goog _______________________________________________ Linux-f2fs-devel mailing list Linux-f2fs-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel