Skip to content

Commit

Permalink
upgrade f2fs to 1.16
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas-Tsai committed Dec 12, 2024
1 parent 52cc058 commit 5477037
Show file tree
Hide file tree
Showing 26 changed files with 6,837 additions and 162 deletions.
3 changes: 0 additions & 3 deletions config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@
/* Define to 1 if you have the <ext2fs/ext2fs.h> header file. */
#undef HAVE_EXT2FS_EXT2FS_H

/* Define to 1 if you have the <f2fs-tools/f2fs_fs.h> header file. */
#undef HAVE_F2FS_TOOLS_F2FS_FS_H

/* Define to 1 if you have the <fcntl.h> header file. */
#undef HAVE_FCNTL_H

Expand Down
13 changes: 1 addition & 12 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -242,20 +242,9 @@ AC_ARG_ENABLE([f2fs],

AM_CONDITIONAL(ENABLE_F2FS, test "$enable_f2fs" = yes)

if test "$enable_f2fs" = "yes"; then
#check library of f2fs filesystems
dnl Check for F2FS
AS_MESSAGE([checking for F2FS Library and Header files ... ...])
AC_CHECK_HEADERS([f2fs-tools/f2fs_fs.h], ,
AC_MSG_ERROR([*** F2FS header files (f2fs-tools/f2fs_fs.h) not found])
)
AC_CHECK_LIB([f2fs], [f2fs_get_device_info], true,
AC_MSG_ERROR([*** f2fs library (libef2fs) not found]))

supported_fs=$supported_fs" f2fs"
f2fs_version="unknown, suggest 1.15.0"
f2fs_version="build-in 1.16"

fi
##end of check f2fs

##nilfs2##
Expand Down
6 changes: 3 additions & 3 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ XFS_SOURCE=xfs/libxfs/cache.c xfs/libxfs/crc32.c xfs/libxfs/defer_item.c xfs/lib

EXFATFS_SOURCE=exfat/cluster.c exfat/utf.c exfat/utils.c exfat/lookup.c exfat/io.c exfat/log.c exfat/node.c exfat/mount.c exfat/time.c

F2FS_SOURCE=f2fs/fsck.c f2fs/fsck.h f2fs/f2fs.h f2fs/mount.c f2fs/mkquota.c f2fs/quotaio.c f2fs/dump.c f2fs/dir.c f2fs/quotaio_v2.c f2fs/node.c f2fs/segment.c f2fs/xattr.c f2fs/dict.c f2fs/quotaio_tree.c
F2FS_SOURCE=f2fs/fsck.c f2fs/fsck.h f2fs/f2fs.h f2fs/mount.c f2fs/mkquota.c f2fs/quotaio.c f2fs/dump.c f2fs/dir.c f2fs/quotaio_v2.c f2fs/node.c f2fs/segment.c f2fs/xattr.c f2fs/dict.c f2fs/quotaio_tree.c f2fs/libf2fs.c f2fs/libf2fs_io.c f2fs/libf2fs_zoned.c f2fs/nls_utf8.c

if ENABLE_STATIC
AM_LDFLAGS=-static
Expand Down Expand Up @@ -110,8 +110,8 @@ endif
if ENABLE_F2FS
sbin_PROGRAMS += partclone.f2fs
partclone_f2fs_SOURCES=$(main_files) f2fsclone.c f2fsclone.h $(F2FS_SOURCE)
partclone_f2fs_CFLAGS=-DF2FS -If2fs/
partclone_f2fs_LDADD=-luuid -lcrypto -lf2fs ${LDADD_static}
partclone_f2fs_CFLAGS=-DF2FS -DHAVE_MNTENT_H=1 -DHAVE_SYS_MOUNT_H=1 -DHAVE_SYS_SYSMACROS_H=1 -DHAVE_SYS_UTSNAME_H=1 -DHAVE_SCSI_SG_H=1 -DHAVE_SYS_IOCTL_H=1 -DHAVE_LINUX_HDREG_H -DHAVE_LINUX_LIMITS_H=1 -DHAVE_LINUX_TYPES_H=1 -DHAVE_LSEEK64=1 -DHAVE_SETMNTENT=1 -If2fs/
partclone_f2fs_LDADD=-luuid -lcrypto ${LDADD_static}
endif

if ENABLE_NILFS2
Expand Down
1 change: 0 additions & 1 deletion src/f2fs/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#define DICT_NODEBUG

#include "config.h"
#include <stdlib.h>
#include <stddef.h>
#ifdef DICT_NODEBUG
Expand Down
33 changes: 33 additions & 0 deletions src/f2fs/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,39 @@ static bool is_sit_bitmap_set(struct f2fs_sb_info *sbi, u32 blk_addr)
(const char *)se->cur_valid_map) != 0;
}

void dump_node_scan_disk(struct f2fs_sb_info *sbi, nid_t nid)
{
struct f2fs_node *node_blk;
pgoff_t blkaddr;
int ret;
pgoff_t start_blkaddr = SM_I(sbi)->main_blkaddr;
pgoff_t end_blkaddr = start_blkaddr +
(SM_I(sbi)->main_segments << sbi->log_blocks_per_seg);

node_blk = calloc(BLOCK_SZ, 1);
ASSERT(node_blk);
MSG(0, "Info: scan all nid: %u from block_addr [%lu: %lu]\n",
nid, start_blkaddr, end_blkaddr);

for (blkaddr = start_blkaddr; blkaddr < end_blkaddr; blkaddr++) {
struct seg_entry *se = get_seg_entry(sbi, GET_SEGNO(sbi, blkaddr));
if (se->type < CURSEG_HOT_NODE)
continue;

ret = dev_read_block(node_blk, blkaddr);
ASSERT(ret >= 0);
if (le32_to_cpu(node_blk->footer.ino) != nid ||
le32_to_cpu(node_blk->footer.nid) != nid)
continue;
MSG(0, "Info: nid: %u, blkaddr: %lu\n", nid, blkaddr);
MSG(0, "node_blk.footer.flag [0x%x]\n", le32_to_cpu(node_blk->footer.flag));
MSG(0, "node_blk.footer.cp_ver [%x]\n", (u32)(cpver_of_node(node_blk)));
print_inode_info(sbi, node_blk, 0);
}

free(node_blk);
}

int dump_node(struct f2fs_sb_info *sbi, nid_t nid, int force)
{
struct node_info ni;
Expand Down
13 changes: 8 additions & 5 deletions src/f2fs/f2fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <f2fs_fs.h>

#ifdef HAVE_MNTENT_H
#include <mntent.h>
#endif
Expand All @@ -35,8 +37,6 @@
#endif
#include <assert.h>

#include "f2fs_fs.h"

#define EXIT_ERR_CODE (-1)
#define ver_after(a, b) (typecheck(unsigned long long, a) && \
typecheck(unsigned long long, b) && \
Expand Down Expand Up @@ -416,10 +416,13 @@ static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)

static inline block_t __end_block_addr(struct f2fs_sb_info *sbi)
{
block_t end = SM_I(sbi)->main_blkaddr;
return end + le64_to_cpu(F2FS_RAW_SUPER(sbi)->block_count);
return SM_I(sbi)->main_blkaddr +
(le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_main) <<
sbi->log_blocks_per_seg);
}

#define BLKS_PER_SEC(sbi) \
((sbi)->segs_per_sec * (sbi)->blocks_per_seg)
#define GET_ZONENO_FROM_SEGNO(sbi, segno) \
((segno / sbi->segs_per_sec) / sbi->secs_per_zone)

Expand Down Expand Up @@ -460,6 +463,7 @@ static inline block_t __end_block_addr(struct f2fs_sb_info *sbi)
#define GET_R2L_SEGNO(sbi, segno) (segno + FREE_I_START_SEGNO(sbi))

#define MAIN_SEGS(sbi) (SM_I(sbi)->main_segments)
#define TOTAL_SEGS(sbi) (SM_I(sbi)->segment_count)
#define TOTAL_BLKS(sbi) (TOTAL_SEGS(sbi) << (sbi)->log_blocks_per_seg)
#define MAX_BLKADDR(sbi) (SEG0_BLKADDR(sbi) + TOTAL_BLKS(sbi))

Expand Down Expand Up @@ -508,7 +512,6 @@ struct fsync_inode_entry {
((segno) % sit_i->sents_per_block)
#define SIT_BLOCK_OFFSET(sit_i, segno) \
((segno) / SIT_ENTRY_PER_BLOCK)
#define TOTAL_SEGS(sbi) (SM_I(sbi)->main_segments)

static inline bool IS_VALID_NID(struct f2fs_sb_info *sbi, u32 nid)
{
Expand Down
86 changes: 69 additions & 17 deletions src/f2fs/f2fs_fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,17 @@
#define static_assert _Static_assert
#endif

#ifdef HAVE_SYS_MOUNT_H
#include <sys/mount.h>
#endif

#ifndef fallthrough
#ifdef __clang__
#define fallthrough do {} while (0) /* fall through */
#else
#define fallthrough __attribute__((__fallthrough__))
#endif
#endif

#ifdef _WIN32
#undef HAVE_LINUX_TYPES_H
Expand Down Expand Up @@ -257,7 +263,7 @@ static inline uint64_t bswap_64(uint64_t val)

#define MSG(n, fmt, ...) \
do { \
if (c.dbg_lv > n && !c.layout && !c.show_file_map) { \
if (c.dbg_lv >= n && !c.layout && !c.show_file_map) { \
printf(fmt, ##__VA_ARGS__); \
} \
} while (0)
Expand Down Expand Up @@ -343,10 +349,6 @@ static inline uint64_t bswap_64(uint64_t val)
snprintf(buf, len, #member)

/* these are defined in kernel */
#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif
#define PAGE_CACHE_SIZE 4096
#define BITS_PER_BYTE 8
#ifndef SECTOR_SHIFT
#define SECTOR_SHIFT 9
Expand Down Expand Up @@ -478,7 +480,6 @@ struct f2fs_configuration {
uint64_t wanted_total_sectors;
uint64_t wanted_sector_size;
uint64_t target_sectors;
uint64_t max_size;
uint32_t sectors_per_blk;
uint32_t blks_per_seg;
__u8 init_version[VERSION_LEN + 1];
Expand Down Expand Up @@ -507,6 +508,9 @@ struct f2fs_configuration {
int force;
int defset;
int bug_on;
int force_stop;
int abnormal_stop;
int fs_errors;
int bug_nat_bits;
bool quota_fixed;
int alloc_failed;
Expand Down Expand Up @@ -756,6 +760,42 @@ struct f2fs_device {

static_assert(sizeof(struct f2fs_device) == 68, "");

/* reason of stop_checkpoint */
enum stop_cp_reason {
STOP_CP_REASON_SHUTDOWN,
STOP_CP_REASON_FAULT_INJECT,
STOP_CP_REASON_META_PAGE,
STOP_CP_REASON_WRITE_FAIL,
STOP_CP_REASON_CORRUPTED_SUMMARY,
STOP_CP_REASON_UPDATE_INODE,
STOP_CP_REASON_FLUSH_FAIL,
STOP_CP_REASON_MAX,
};

#define MAX_STOP_REASON 32

/* detail reason for EFSCORRUPTED */
enum f2fs_error {
ERROR_CORRUPTED_CLUSTER,
ERROR_FAIL_DECOMPRESSION,
ERROR_INVALID_BLKADDR,
ERROR_CORRUPTED_DIRENT,
ERROR_CORRUPTED_INODE,
ERROR_INCONSISTENT_SUMMARY,
ERROR_INCONSISTENT_FOOTER,
ERROR_INCONSISTENT_SUM_TYPE,
ERROR_CORRUPTED_JOURNAL,
ERROR_INCONSISTENT_NODE_COUNT,
ERROR_INCONSISTENT_BLOCK_COUNT,
ERROR_INVALID_CURSEG,
ERROR_INCONSISTENT_SIT,
ERROR_CORRUPTED_VERITY_XATTR,
ERROR_CORRUPTED_XATTR,
ERROR_MAX,
};

#define MAX_F2FS_ERRORS 16

struct f2fs_super_block {
__le32 magic; /* Magic Number */
__le16 major_ver; /* Major Version */
Expand Down Expand Up @@ -800,7 +840,9 @@ struct f2fs_super_block {
__u8 hot_ext_count; /* # of hot file extension */
__le16 s_encoding; /* Filename charset encoding */
__le16 s_encoding_flags; /* Filename charset encoding flags */
__u8 reserved[306]; /* valid reserved region */
__u8 s_stop_reason[MAX_STOP_REASON]; /* stop checkpoint reason */
__u8 s_errors[MAX_F2FS_ERRORS]; /* reason of image corrupts */
__u8 reserved[258]; /* valid reserved region */
__le32 crc; /* checksum of superblock */
};

Expand Down Expand Up @@ -943,9 +985,10 @@ static_assert(sizeof(struct f2fs_extent) == 12, "");
DEFAULT_INLINE_XATTR_ADDRS - \
F2FS_TOTAL_EXTRA_ATTR_SIZE - \
DEF_INLINE_RESERVED_SIZE))
#define INLINE_DATA_OFFSET (PAGE_CACHE_SIZE - sizeof(struct node_footer) \
- sizeof(__le32)*(DEF_ADDRS_PER_INODE + 5 - \
DEF_INLINE_RESERVED_SIZE))
#define INLINE_DATA_OFFSET (F2FS_BLKSIZE - \
sizeof(struct node_footer) - \
sizeof(__le32) * (DEF_ADDRS_PER_INODE + \
5 - DEF_INLINE_RESERVED_SIZE))

#define DEF_DIR_LEVEL 0

Expand Down Expand Up @@ -1087,7 +1130,7 @@ static_assert(sizeof(struct f2fs_node) == 4096, "");
/*
* For NAT entries
*/
#define NAT_ENTRY_PER_BLOCK (PAGE_CACHE_SIZE / sizeof(struct f2fs_nat_entry))
#define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / sizeof(struct f2fs_nat_entry))
#define NAT_BLOCK_OFFSET(start_nid) (start_nid / NAT_ENTRY_PER_BLOCK)

#define DEFAULT_NAT_ENTRY_RATIO 20
Expand All @@ -1114,7 +1157,7 @@ static_assert(sizeof(struct f2fs_nat_block) == 4095, "");
* Not allow to change this.
*/
#define SIT_VBLOCK_MAP_SIZE 64
#define SIT_ENTRY_PER_BLOCK (PAGE_CACHE_SIZE / sizeof(struct f2fs_sit_entry))
#define SIT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / sizeof(struct f2fs_sit_entry))

/*
* F2FS uses 4 bytes to represent block address. As a result, supported size of
Expand All @@ -1125,6 +1168,10 @@ static_assert(sizeof(struct f2fs_nat_block) == 4095, "");
#define MAX_SIT_BITMAP_SIZE (SEG_ALIGN(SIZE_ALIGN(F2FS_MAX_SEGMENT, \
SIT_ENTRY_PER_BLOCK)) * \
c.blks_per_seg / 8)
#define MAX_CP_PAYLOAD (SEG_ALIGN(SIZE_ALIGN(UINT32_MAX, NAT_ENTRY_PER_BLOCK)) * \
DEFAULT_NAT_ENTRY_RATIO / 100 * \
c.blks_per_seg / 8 + \
MAX_SIT_BITMAP_SIZE - MAX_BITMAP_SIZE_IN_CKPT)

/*
* Note that f2fs_sit_entry->vblocks has the following bit-field information.
Expand Down Expand Up @@ -1311,7 +1358,7 @@ typedef __le32 f2fs_hash_t;
#define SIZE_OF_DIR_ENTRY 11 /* by byte */
#define SIZE_OF_DENTRY_BITMAP ((NR_DENTRY_IN_BLOCK + BITS_PER_BYTE - 1) / \
BITS_PER_BYTE)
#define SIZE_OF_RESERVED (PAGE_SIZE - ((SIZE_OF_DIR_ENTRY + \
#define SIZE_OF_RESERVED (F2FS_BLKSIZE - ((SIZE_OF_DIR_ENTRY + \
F2FS_SLOT_LEN) * \
NR_DENTRY_IN_BLOCK + SIZE_OF_DENTRY_BITMAP))
#define MIN_INLINE_DENTRY_SIZE 40 /* just include '.' and '..' entries */
Expand All @@ -1335,7 +1382,7 @@ struct f2fs_dentry_block {
__u8 filename[NR_DENTRY_IN_BLOCK][F2FS_SLOT_LEN];
};

static_assert(sizeof(struct f2fs_dentry_block) == 4096, "");
static_assert(sizeof(struct f2fs_dentry_block) == F2FS_BLKSIZE, "");

/* for inline stuff */
#define DEF_INLINE_RESERVED_SIZE 1
Expand Down Expand Up @@ -1554,9 +1601,11 @@ blk_zone_cond_str(struct blk_zone *blkz)

#endif

struct blk_zone;

extern int f2fs_get_zoned_model(int);
extern int f2fs_get_zone_blocks(int);
extern int f2fs_report_zone(int, uint64_t, void *);
extern int f2fs_report_zone(int, uint64_t, struct blk_zone *);
typedef int (report_zones_cb_t)(int i, void *, void *);
extern int f2fs_report_zones(int, report_zones_cb_t *, void *);
extern int f2fs_check_zones(int);
Expand Down Expand Up @@ -1586,10 +1635,13 @@ static inline double get_best_overprovision(struct f2fs_super_block *sb)
}

for (; candidate <= end; candidate += diff) {
reserved = (2 * (100 / candidate + 1) + 6) *
reserved = (100 / candidate + 1 + NR_CURSEG_TYPE) *
round_up(usable_main_segs, get_sb(section_count));
ovp = (usable_main_segs - reserved) * candidate / 100;
space = usable_main_segs - reserved - ovp;
if (ovp < 0)
continue;
space = usable_main_segs - max(reserved, ovp) -
2 * get_sb(segs_per_sec);
if (max_space < space) {
max_space = space;
max_ovp = candidate;
Expand Down
Loading

0 comments on commit 5477037

Please sign in to comment.