Home | History | Annotate | Download | only in include
      1 /**
      2  * f2fs_fs.h
      3  *
      4  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
      5  *             http://www.samsung.com/
      6  *
      7  * Dual licensed under the GPL or LGPL version 2 licenses.
      8  *
      9  * The byteswap codes are copied from:
     10  *   samba_3_master/lib/ccan/endian/endian.h under LGPL 2.1
     11  */
     12 #ifndef __F2FS_FS_H__
     13 #define __F2FS_FS_H__
     14 
     15 #ifdef HAVE_CONFIG_H
     16 #include <config.h>
     17 #endif
     18 
     19 #ifdef __ANDROID__
     20 #define WITH_ANDROID
     21 #endif
     22 
     23 #ifdef WITH_ANDROID
     24 #include <android_config.h>
     25 #else
     26 #define WITH_DUMP
     27 #define WITH_DEFRAG
     28 #define WITH_RESIZE
     29 #define WITH_SLOAD
     30 #endif
     31 
     32 #include <inttypes.h>
     33 #ifdef HAVE_LINUX_TYPES_H
     34 #include <linux/types.h>
     35 #endif
     36 #include <sys/types.h>
     37 
     38 #ifdef HAVE_LINUX_BLKZONED_H
     39 #include <linux/blkzoned.h>
     40 #endif
     41 
     42 #ifdef HAVE_LIBSELINUX
     43 #include <selinux/selinux.h>
     44 #include <selinux/label.h>
     45 #endif
     46 
     47 #ifdef UNUSED
     48 #elif defined(__GNUC__)
     49 # define UNUSED(x) UNUSED_ ## x __attribute__((unused))
     50 #elif defined(__LCLINT__)
     51 # define UNUSED(x) x
     52 #else
     53 # define UNUSED(x) x
     54 #endif
     55 
     56 #ifdef ANDROID_WINDOWS_HOST
     57 #undef HAVE_LINUX_TYPES_H
     58 typedef uint64_t u_int64_t;
     59 typedef uint32_t u_int32_t;
     60 typedef uint16_t u_int16_t;
     61 typedef uint8_t u_int8_t;
     62 #endif
     63 
     64 typedef u_int64_t	u64;
     65 typedef u_int32_t	u32;
     66 typedef u_int16_t	u16;
     67 typedef u_int8_t	u8;
     68 typedef u32		block_t;
     69 typedef u32		nid_t;
     70 #ifndef bool
     71 typedef u8		bool;
     72 #endif
     73 typedef unsigned long	pgoff_t;
     74 typedef unsigned short	umode_t;
     75 
     76 #ifndef HAVE_LINUX_TYPES_H
     77 typedef u8	__u8;
     78 typedef u16	__u16;
     79 typedef u32	__u32;
     80 typedef u64	__u64;
     81 typedef u16	__le16;
     82 typedef u32	__le32;
     83 typedef u64	__le64;
     84 typedef u16	__be16;
     85 typedef u32	__be32;
     86 typedef u64	__be64;
     87 #endif
     88 
     89 #if HAVE_BYTESWAP_H
     90 #include <byteswap.h>
     91 #else
     92 /**
     93  * bswap_16 - reverse bytes in a uint16_t value.
     94  * @val: value whose bytes to swap.
     95  *
     96  * Example:
     97  *	// Output contains "1024 is 4 as two bytes reversed"
     98  *	printf("1024 is %u as two bytes reversed\n", bswap_16(1024));
     99  */
    100 static inline uint16_t bswap_16(uint16_t val)
    101 {
    102 	return ((val & (uint16_t)0x00ffU) << 8)
    103 		| ((val & (uint16_t)0xff00U) >> 8);
    104 }
    105 
    106 /**
    107  * bswap_32 - reverse bytes in a uint32_t value.
    108  * @val: value whose bytes to swap.
    109  *
    110  * Example:
    111  *	// Output contains "1024 is 262144 as four bytes reversed"
    112  *	printf("1024 is %u as four bytes reversed\n", bswap_32(1024));
    113  */
    114 static inline uint32_t bswap_32(uint32_t val)
    115 {
    116 	return ((val & (uint32_t)0x000000ffUL) << 24)
    117 		| ((val & (uint32_t)0x0000ff00UL) <<  8)
    118 		| ((val & (uint32_t)0x00ff0000UL) >>  8)
    119 		| ((val & (uint32_t)0xff000000UL) >> 24);
    120 }
    121 #endif /* !HAVE_BYTESWAP_H */
    122 
    123 #if defined HAVE_DECL_BSWAP_64 && !HAVE_DECL_BSWAP_64
    124 /**
    125  * bswap_64 - reverse bytes in a uint64_t value.
    126  * @val: value whose bytes to swap.
    127  *
    128  * Example:
    129  *	// Output contains "1024 is 1125899906842624 as eight bytes reversed"
    130  *	printf("1024 is %llu as eight bytes reversed\n",
    131  *		(unsigned long long)bswap_64(1024));
    132  */
    133 static inline uint64_t bswap_64(uint64_t val)
    134 {
    135 	return ((val & (uint64_t)0x00000000000000ffULL) << 56)
    136 		| ((val & (uint64_t)0x000000000000ff00ULL) << 40)
    137 		| ((val & (uint64_t)0x0000000000ff0000ULL) << 24)
    138 		| ((val & (uint64_t)0x00000000ff000000ULL) <<  8)
    139 		| ((val & (uint64_t)0x000000ff00000000ULL) >>  8)
    140 		| ((val & (uint64_t)0x0000ff0000000000ULL) >> 24)
    141 		| ((val & (uint64_t)0x00ff000000000000ULL) >> 40)
    142 		| ((val & (uint64_t)0xff00000000000000ULL) >> 56);
    143 }
    144 #endif
    145 
    146 #if __BYTE_ORDER == __LITTLE_ENDIAN
    147 #define le16_to_cpu(x)	((__u16)(x))
    148 #define le32_to_cpu(x)	((__u32)(x))
    149 #define le64_to_cpu(x)	((__u64)(x))
    150 #define cpu_to_le16(x)	((__u16)(x))
    151 #define cpu_to_le32(x)	((__u32)(x))
    152 #define cpu_to_le64(x)	((__u64)(x))
    153 #elif __BYTE_ORDER == __BIG_ENDIAN
    154 #define le16_to_cpu(x)	bswap_16(x)
    155 #define le32_to_cpu(x)	bswap_32(x)
    156 #define le64_to_cpu(x)	bswap_64(x)
    157 #define cpu_to_le16(x)	bswap_16(x)
    158 #define cpu_to_le32(x)	bswap_32(x)
    159 #define cpu_to_le64(x)	bswap_64(x)
    160 #endif
    161 
    162 #define typecheck(type,x) \
    163 	({	type __dummy; \
    164 		typeof(x) __dummy2; \
    165 		(void)(&__dummy == &__dummy2); \
    166 		1; \
    167 	 })
    168 
    169 #define NULL_SEGNO	((unsigned int)~0)
    170 
    171 /*
    172  * Debugging interfaces
    173  */
    174 #define FIX_MSG(fmt, ...)						\
    175 	do {								\
    176 		printf("[FIX] (%s:%4d) ", __func__, __LINE__);		\
    177 		printf(" --> "fmt"\n", ##__VA_ARGS__);			\
    178 	} while (0)
    179 
    180 #define ASSERT_MSG(fmt, ...)						\
    181 	do {								\
    182 		printf("[ASSERT] (%s:%4d) ", __func__, __LINE__);	\
    183 		printf(" --> "fmt"\n", ##__VA_ARGS__);			\
    184 		c.bug_on = 1;						\
    185 	} while (0)
    186 
    187 #define ASSERT(exp)							\
    188 	do {								\
    189 		if (!(exp)) {						\
    190 			printf("[ASSERT] (%s:%4d) " #exp"\n",		\
    191 					__func__, __LINE__);		\
    192 			exit(-1);					\
    193 		}							\
    194 	} while (0)
    195 
    196 #define ERR_MSG(fmt, ...)						\
    197 	do {								\
    198 		printf("[%s:%d] " fmt, __func__, __LINE__, ##__VA_ARGS__); \
    199 	} while (0)
    200 
    201 #define MSG(n, fmt, ...)						\
    202 	do {								\
    203 		if (c.dbg_lv >= n) {					\
    204 			printf(fmt, ##__VA_ARGS__);			\
    205 		}							\
    206 	} while (0)
    207 
    208 #define DBG(n, fmt, ...)						\
    209 	do {								\
    210 		if (c.dbg_lv >= n) {					\
    211 			printf("[%s:%4d] " fmt,				\
    212 				__func__, __LINE__, ##__VA_ARGS__);	\
    213 		}							\
    214 	} while (0)
    215 
    216 /* Display on console */
    217 #define DISP(fmt, ptr, member)				\
    218 	do {						\
    219 		printf("%-30s" fmt, #member, ((ptr)->member));	\
    220 	} while (0)
    221 
    222 #define DISP_u16(ptr, member)						\
    223 	do {								\
    224 		assert(sizeof((ptr)->member) == 2);			\
    225 		printf("%-30s" "\t\t[0x%8x : %u]\n",			\
    226 			#member, le16_to_cpu(((ptr)->member)),		\
    227 			le16_to_cpu(((ptr)->member)));			\
    228 	} while (0)
    229 
    230 #define DISP_u32(ptr, member)						\
    231 	do {								\
    232 		assert(sizeof((ptr)->member) <= 4);			\
    233 		printf("%-30s" "\t\t[0x%8x : %u]\n",			\
    234 			#member, le32_to_cpu(((ptr)->member)),		\
    235 			le32_to_cpu(((ptr)->member)));			\
    236 	} while (0)
    237 
    238 #define DISP_u64(ptr, member)						\
    239 	do {								\
    240 		assert(sizeof((ptr)->member) == 8);			\
    241 		printf("%-30s" "\t\t[0x%8llx : %llu]\n",		\
    242 			#member, le64_to_cpu(((ptr)->member)),		\
    243 			le64_to_cpu(((ptr)->member)));			\
    244 	} while (0)
    245 
    246 #define DISP_utf(ptr, member)						\
    247 	do {								\
    248 		printf("%-30s" "\t\t[%s]\n", #member, ((ptr)->member)); \
    249 	} while (0)
    250 
    251 /* Display to buffer */
    252 #define BUF_DISP_u32(buf, data, len, ptr, member)			\
    253 	do {								\
    254 		assert(sizeof((ptr)->member) <= 4);			\
    255 		snprintf(buf, len, #member);				\
    256 		snprintf(data, len, "0x%x : %u", ((ptr)->member),	\
    257 						((ptr)->member));	\
    258 	} while (0)
    259 
    260 #define BUF_DISP_u64(buf, data, len, ptr, member)			\
    261 	do {								\
    262 		assert(sizeof((ptr)->member) == 8);			\
    263 		snprintf(buf, len, #member);				\
    264 		snprintf(data, len, "0x%llx : %llu", ((ptr)->member),	\
    265 						((ptr)->member));	\
    266 	} while (0)
    267 
    268 #define BUF_DISP_utf(buf, data, len, ptr, member)			\
    269 		snprintf(buf, len, #member)
    270 
    271 /* these are defined in kernel */
    272 #ifndef PAGE_SIZE
    273 #define PAGE_SIZE		4096
    274 #endif
    275 #define PAGE_CACHE_SIZE		4096
    276 #define BITS_PER_BYTE		8
    277 #define F2FS_SUPER_MAGIC	0xF2F52010	/* F2FS Magic Number */
    278 #define CHECKSUM_OFFSET		4092
    279 #define MAX_PATH_LEN		64
    280 #define MAX_DEVICES		8
    281 
    282 #define F2FS_BYTES_TO_BLK(bytes)    ((bytes) >> F2FS_BLKSIZE_BITS)
    283 #define F2FS_BLKSIZE_BITS 12
    284 
    285 /* for mkfs */
    286 #define	F2FS_NUMBER_OF_CHECKPOINT_PACK	2
    287 #define	DEFAULT_SECTOR_SIZE		512
    288 #define	DEFAULT_SECTORS_PER_BLOCK	8
    289 #define	DEFAULT_BLOCKS_PER_SEGMENT	512
    290 #define DEFAULT_SEGMENTS_PER_SECTION	1
    291 
    292 #define VERSION_LEN	256
    293 
    294 enum f2fs_config_func {
    295 	MKFS,
    296 	FSCK,
    297 	DUMP,
    298 	DEFRAG,
    299 	RESIZE,
    300 	SLOAD,
    301 };
    302 
    303 struct device_info {
    304 	char *path;
    305 	int32_t fd;
    306 	u_int32_t sector_size;
    307 	u_int64_t total_sectors;	/* got by get_device_info */
    308 	u_int64_t start_blkaddr;
    309 	u_int64_t end_blkaddr;
    310 	u_int32_t total_segments;
    311 
    312 	/* to handle zone block devices */
    313 	int zoned_model;
    314 	u_int32_t nr_zones;
    315 	u_int32_t nr_rnd_zones;
    316 	size_t zone_blocks;
    317 };
    318 
    319 struct f2fs_configuration {
    320 	u_int32_t reserved_segments;
    321 	u_int32_t new_reserved_segments;
    322 	int sparse_mode;
    323 	int zoned_mode;
    324 	int zoned_model;
    325 	size_t zone_blocks;
    326 	double overprovision;
    327 	double new_overprovision;
    328 	u_int32_t cur_seg[6];
    329 	u_int32_t segs_per_sec;
    330 	u_int32_t secs_per_zone;
    331 	u_int32_t segs_per_zone;
    332 	u_int32_t start_sector;
    333 	u_int32_t total_segments;
    334 	u_int32_t sector_size;
    335 	u_int64_t device_size;
    336 	u_int64_t total_sectors;
    337 	u_int64_t wanted_total_sectors;
    338 	u_int64_t wanted_sector_size;
    339 	u_int64_t target_sectors;
    340 	u_int32_t sectors_per_blk;
    341 	u_int32_t blks_per_seg;
    342 	__u8 init_version[VERSION_LEN + 1];
    343 	__u8 sb_version[VERSION_LEN + 1];
    344 	__u8 version[VERSION_LEN + 1];
    345 	char *vol_label;
    346 	int heap;
    347 	int32_t kd;
    348 	int32_t dump_fd;
    349 	struct device_info devices[MAX_DEVICES];
    350 	int ndevs;
    351 	char *extension_list;
    352 	const char *rootdev_name;
    353 	int dbg_lv;
    354 	int show_dentry;
    355 	int trim;
    356 	int trimmed;
    357 	int func;
    358 	void *private;
    359 	int dry_run;
    360 	int fix_on;
    361 	int bug_on;
    362 	int auto_fix;
    363 	int preen_mode;
    364 	int ro;
    365 	int preserve_limits;		/* preserve quota limits */
    366 	__le32 feature;			/* defined features */
    367 
    368 	/* defragmentation parameters */
    369 	int defrag_shrink;
    370 	u_int64_t defrag_start;
    371 	u_int64_t defrag_len;
    372 	u_int64_t defrag_target;
    373 
    374 	/* sload parameters */
    375 	char *from_dir;
    376 	char *mount_point;
    377 	char *target_out_dir;
    378 	char *fs_config_file;
    379 	time_t fixed_time;
    380 #ifdef HAVE_LIBSELINUX
    381 	struct selinux_opt seopt_file[8];
    382 	int nr_opt;
    383 #endif
    384 
    385 	/* precomputed fs UUID checksum for seeding other checksums */
    386 	u_int32_t chksum_seed;
    387 };
    388 
    389 #ifdef CONFIG_64BIT
    390 #define BITS_PER_LONG	64
    391 #else
    392 #define BITS_PER_LONG	32
    393 #endif
    394 
    395 #define BIT_MASK(nr)	(1 << (nr % BITS_PER_LONG))
    396 #define BIT_WORD(nr)	(nr / BITS_PER_LONG)
    397 
    398 #define set_sb_le64(member, val)		(sb->member = cpu_to_le64(val))
    399 #define set_sb_le32(member, val)		(sb->member = cpu_to_le32(val))
    400 #define set_sb_le16(member, val)		(sb->member = cpu_to_le16(val))
    401 #define get_sb_le64(member)			le64_to_cpu(sb->member)
    402 #define get_sb_le32(member)			le32_to_cpu(sb->member)
    403 #define get_sb_le16(member)			le16_to_cpu(sb->member)
    404 #define get_newsb_le64(member)			le64_to_cpu(new_sb->member)
    405 #define get_newsb_le32(member)			le32_to_cpu(new_sb->member)
    406 #define get_newsb_le16(member)			le16_to_cpu(new_sb->member)
    407 
    408 #define set_sb(member, val)	\
    409 			do {						\
    410 				typeof(sb->member) t;			\
    411 				switch (sizeof(t)) {			\
    412 				case 8: set_sb_le64(member, val); break; \
    413 				case 4: set_sb_le32(member, val); break; \
    414 				case 2: set_sb_le16(member, val); break; \
    415 				} \
    416 			} while(0)
    417 
    418 #define get_sb(member)		\
    419 			({						\
    420 				typeof(sb->member) t;			\
    421 				switch (sizeof(t)) {			\
    422 				case 8: t = get_sb_le64(member); break; \
    423 				case 4: t = get_sb_le32(member); break; \
    424 				case 2: t = get_sb_le16(member); break; \
    425 				} 					\
    426 				t; \
    427 			})
    428 #define get_newsb(member)		\
    429 			({						\
    430 				typeof(new_sb->member) t;		\
    431 				switch (sizeof(t)) {			\
    432 				case 8: t = get_newsb_le64(member); break; \
    433 				case 4: t = get_newsb_le32(member); break; \
    434 				case 2: t = get_newsb_le16(member); break; \
    435 				} 					\
    436 				t; \
    437 			})
    438 
    439 #define set_cp_le64(member, val)		(cp->member = cpu_to_le64(val))
    440 #define set_cp_le32(member, val)		(cp->member = cpu_to_le32(val))
    441 #define set_cp_le16(member, val)		(cp->member = cpu_to_le16(val))
    442 #define get_cp_le64(member)			le64_to_cpu(cp->member)
    443 #define get_cp_le32(member)			le32_to_cpu(cp->member)
    444 #define get_cp_le16(member)			le16_to_cpu(cp->member)
    445 
    446 #define set_cp(member, val)	\
    447 			do {						\
    448 				typeof(cp->member) t;			\
    449 				switch (sizeof(t)) {			\
    450 				case 8: set_cp_le64(member, val); break; \
    451 				case 4: set_cp_le32(member, val); break; \
    452 				case 2: set_cp_le16(member, val); break; \
    453 				} \
    454 			} while(0)
    455 
    456 #define get_cp(member)		\
    457 			({						\
    458 				typeof(cp->member) t;			\
    459 				switch (sizeof(t)) {			\
    460 				case 8: t = get_cp_le64(member); break; \
    461 				case 4: t = get_cp_le32(member); break; \
    462 				case 2: t = get_cp_le16(member); break; \
    463 				} 					\
    464 				t; \
    465 			})
    466 
    467 /*
    468  * Copied from include/linux/kernel.h
    469  */
    470 #define __round_mask(x, y)	((__typeof__(x))((y)-1))
    471 #define round_down(x, y)	((x) & ~__round_mask(x, y))
    472 
    473 #define min(x, y) ({				\
    474 	typeof(x) _min1 = (x);			\
    475 	typeof(y) _min2 = (y);			\
    476 	(void) (&_min1 == &_min2);		\
    477 	_min1 < _min2 ? _min1 : _min2; })
    478 
    479 #define max(x, y) ({				\
    480 	typeof(x) _max1 = (x);			\
    481 	typeof(y) _max2 = (y);			\
    482 	(void) (&_max1 == &_max2);		\
    483 	_max1 > _max2 ? _max1 : _max2; })
    484 
    485 /*
    486  * Copied from fs/f2fs/f2fs.h
    487  */
    488 #define	NR_CURSEG_DATA_TYPE	(3)
    489 #define NR_CURSEG_NODE_TYPE	(3)
    490 #define NR_CURSEG_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
    491 
    492 enum {
    493 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
    494 	CURSEG_WARM_DATA,	/* data blocks */
    495 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
    496 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
    497 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
    498 	CURSEG_COLD_NODE,	/* indirect node blocks */
    499 	NO_CHECK_TYPE
    500 };
    501 
    502 #define F2FS_MIN_SEGMENTS	9 /* SB + 2 (CP + SIT + NAT) + SSA + MAIN */
    503 
    504 /*
    505  * Copied from fs/f2fs/segment.h
    506  */
    507 #define GET_SUM_TYPE(footer) ((footer)->entry_type)
    508 #define SET_SUM_TYPE(footer, type) ((footer)->entry_type = type)
    509 
    510 /*
    511  * Copied from include/linux/f2fs_sb.h
    512  */
    513 #define F2FS_SUPER_OFFSET		1024	/* byte-size offset */
    514 #define F2FS_MIN_LOG_SECTOR_SIZE	9	/* 9 bits for 512 bytes */
    515 #define F2FS_MAX_LOG_SECTOR_SIZE	12	/* 12 bits for 4096 bytes */
    516 #define F2FS_BLKSIZE			4096	/* support only 4KB block */
    517 #define F2FS_MAX_EXTENSION		64	/* # of extension entries */
    518 #define F2FS_BLK_ALIGN(x)	(((x) + F2FS_BLKSIZE - 1) / F2FS_BLKSIZE)
    519 
    520 #define NULL_ADDR		0x0U
    521 #define NEW_ADDR		-1U
    522 
    523 #define F2FS_ROOT_INO(sbi)	(sbi->root_ino_num)
    524 #define F2FS_NODE_INO(sbi)	(sbi->node_ino_num)
    525 #define F2FS_META_INO(sbi)	(sbi->meta_ino_num)
    526 
    527 #define F2FS_MAX_QUOTAS		3
    528 #define QUOTA_DATA(i)		(2)
    529 #define QUOTA_INO(sb,t)	(le32_to_cpu((sb)->qf_ino[t]))
    530 
    531 #define FS_IMMUTABLE_FL		0x00000010 /* Immutable file */
    532 
    533 /* This flag is used by node and meta inodes, and by recovery */
    534 #define GFP_F2FS_ZERO	(GFP_NOFS | __GFP_ZERO)
    535 
    536 /*
    537  * For further optimization on multi-head logs, on-disk layout supports maximum
    538  * 16 logs by default. The number, 16, is expected to cover all the cases
    539  * enoughly. The implementaion currently uses no more than 6 logs.
    540  * Half the logs are used for nodes, and the other half are used for data.
    541  */
    542 #define MAX_ACTIVE_LOGS	16
    543 #define MAX_ACTIVE_NODE_LOGS	8
    544 #define MAX_ACTIVE_DATA_LOGS	8
    545 
    546 #define F2FS_FEATURE_ENCRYPT		0x0001
    547 #define F2FS_FEATURE_BLKZONED		0x0002
    548 #define F2FS_FEATURE_ATOMIC_WRITE	0x0004
    549 #define F2FS_FEATURE_EXTRA_ATTR		0x0008
    550 #define F2FS_FEATURE_PRJQUOTA		0x0010
    551 #define F2FS_FEATURE_INODE_CHKSUM	0x0020
    552 #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x0040
    553 #define F2FS_FEATURE_QUOTA_INO		0x0080
    554 #define F2FS_FEATURE_INODE_CRTIME	0x0100
    555 #define F2FS_FEATURE_VERITY		0x0400	/* reserved */
    556 
    557 #define MAX_VOLUME_NAME		512
    558 
    559 /*
    560  * For superblock
    561  */
    562 #pragma pack(push, 1)
    563 struct f2fs_device {
    564 	__u8 path[MAX_PATH_LEN];
    565 	__le32 total_segments;
    566 } __attribute__((packed));
    567 
    568 struct f2fs_super_block {
    569 	__le32 magic;			/* Magic Number */
    570 	__le16 major_ver;		/* Major Version */
    571 	__le16 minor_ver;		/* Minor Version */
    572 	__le32 log_sectorsize;		/* log2 sector size in bytes */
    573 	__le32 log_sectors_per_block;	/* log2 # of sectors per block */
    574 	__le32 log_blocksize;		/* log2 block size in bytes */
    575 	__le32 log_blocks_per_seg;	/* log2 # of blocks per segment */
    576 	__le32 segs_per_sec;		/* # of segments per section */
    577 	__le32 secs_per_zone;		/* # of sections per zone */
    578 	__le32 checksum_offset;		/* checksum offset inside super block */
    579 	__le64 block_count;		/* total # of user blocks */
    580 	__le32 section_count;		/* total # of sections */
    581 	__le32 segment_count;		/* total # of segments */
    582 	__le32 segment_count_ckpt;	/* # of segments for checkpoint */
    583 	__le32 segment_count_sit;	/* # of segments for SIT */
    584 	__le32 segment_count_nat;	/* # of segments for NAT */
    585 	__le32 segment_count_ssa;	/* # of segments for SSA */
    586 	__le32 segment_count_main;	/* # of segments for main area */
    587 	__le32 segment0_blkaddr;	/* start block address of segment 0 */
    588 	__le32 cp_blkaddr;		/* start block address of checkpoint */
    589 	__le32 sit_blkaddr;		/* start block address of SIT */
    590 	__le32 nat_blkaddr;		/* start block address of NAT */
    591 	__le32 ssa_blkaddr;		/* start block address of SSA */
    592 	__le32 main_blkaddr;		/* start block address of main area */
    593 	__le32 root_ino;		/* root inode number */
    594 	__le32 node_ino;		/* node inode number */
    595 	__le32 meta_ino;		/* meta inode number */
    596 	__u8 uuid[16];			/* 128-bit uuid for volume */
    597 	__le16 volume_name[MAX_VOLUME_NAME];	/* volume name */
    598 	__le32 extension_count;		/* # of extensions below */
    599 	__u8 extension_list[F2FS_MAX_EXTENSION][8];	/* extension array */
    600 	__le32 cp_payload;
    601 	__u8 version[VERSION_LEN];	/* the kernel version */
    602 	__u8 init_version[VERSION_LEN];	/* the initial kernel version */
    603 	__le32 feature;			/* defined features */
    604 	__u8 encryption_level;		/* versioning level for encryption */
    605 	__u8 encrypt_pw_salt[16];	/* Salt used for string2key algorithm */
    606 	struct f2fs_device devs[MAX_DEVICES];	/* device list */
    607 	__le32 qf_ino[F2FS_MAX_QUOTAS];	/* quota inode numbers */
    608 	__u8 reserved[315];		/* valid reserved region */
    609 } __attribute__((packed));
    610 
    611 /*
    612  * For checkpoint
    613  */
    614 #define CP_NOCRC_RECOVERY_FLAG	0x00000200
    615 #define CP_TRIMMED_FLAG		0x00000100
    616 #define CP_NAT_BITS_FLAG	0x00000080
    617 #define CP_CRC_RECOVERY_FLAG	0x00000040
    618 #define CP_FASTBOOT_FLAG	0x00000020
    619 #define CP_FSCK_FLAG		0x00000010
    620 #define CP_ERROR_FLAG		0x00000008
    621 #define CP_COMPACT_SUM_FLAG	0x00000004
    622 #define CP_ORPHAN_PRESENT_FLAG	0x00000002
    623 #define CP_UMOUNT_FLAG		0x00000001
    624 
    625 struct f2fs_checkpoint {
    626 	__le64 checkpoint_ver;		/* checkpoint block version number */
    627 	__le64 user_block_count;	/* # of user blocks */
    628 	__le64 valid_block_count;	/* # of valid blocks in main area */
    629 	__le32 rsvd_segment_count;	/* # of reserved segments for gc */
    630 	__le32 overprov_segment_count;	/* # of overprovision segments */
    631 	__le32 free_segment_count;	/* # of free segments in main area */
    632 
    633 	/* information of current node segments */
    634 	__le32 cur_node_segno[MAX_ACTIVE_NODE_LOGS];
    635 	__le16 cur_node_blkoff[MAX_ACTIVE_NODE_LOGS];
    636 	/* information of current data segments */
    637 	__le32 cur_data_segno[MAX_ACTIVE_DATA_LOGS];
    638 	__le16 cur_data_blkoff[MAX_ACTIVE_DATA_LOGS];
    639 	__le32 ckpt_flags;		/* Flags : umount and journal_present */
    640 	__le32 cp_pack_total_block_count;	/* total # of one cp pack */
    641 	__le32 cp_pack_start_sum;	/* start block number of data summary */
    642 	__le32 valid_node_count;	/* Total number of valid nodes */
    643 	__le32 valid_inode_count;	/* Total number of valid inodes */
    644 	__le32 next_free_nid;		/* Next free node number */
    645 	__le32 sit_ver_bitmap_bytesize;	/* Default value 64 */
    646 	__le32 nat_ver_bitmap_bytesize; /* Default value 256 */
    647 	__le32 checksum_offset;		/* checksum offset inside cp block */
    648 	__le64 elapsed_time;		/* mounted time */
    649 	/* allocation type of current segment */
    650 	unsigned char alloc_type[MAX_ACTIVE_LOGS];
    651 
    652 	/* SIT and NAT version bitmap */
    653 	unsigned char sit_nat_version_bitmap[1];
    654 } __attribute__((packed));
    655 
    656 #define MAX_SIT_BITMAP_SIZE_IN_CKPT	\
    657 	(CHECKSUM_OFFSET - sizeof(struct f2fs_checkpoint) + 1 - 64)
    658 
    659 /*
    660  * For orphan inode management
    661  */
    662 #define F2FS_ORPHANS_PER_BLOCK	1020
    663 
    664 struct f2fs_orphan_block {
    665 	__le32 ino[F2FS_ORPHANS_PER_BLOCK];	/* inode numbers */
    666 	__le32 reserved;	/* reserved */
    667 	__le16 blk_addr;	/* block index in current CP */
    668 	__le16 blk_count;	/* Number of orphan inode blocks in CP */
    669 	__le32 entry_count;	/* Total number of orphan nodes in current CP */
    670 	__le32 check_sum;	/* CRC32 for orphan inode block */
    671 } __attribute__((packed));
    672 
    673 /*
    674  * For NODE structure
    675  */
    676 struct f2fs_extent {
    677 	__le32 fofs;		/* start file offset of the extent */
    678 	__le32 blk_addr;	/* start block address of the extent */
    679 	__le32 len;		/* lengh of the extent */
    680 } __attribute__((packed));
    681 
    682 #define F2FS_NAME_LEN		255
    683 /* 200 bytes for inline xattrs by default */
    684 #define DEFAULT_INLINE_XATTR_ADDRS	50
    685 #define DEF_ADDRS_PER_INODE	923	/* Address Pointers in an Inode */
    686 #define CUR_ADDRS_PER_INODE(inode)	(DEF_ADDRS_PER_INODE - \
    687 					__get_extra_isize(inode))
    688 #define ADDRS_PER_INODE(i)	addrs_per_inode(i)
    689 #define ADDRS_PER_BLOCK         1018	/* Address Pointers in a Direct Block */
    690 #define NIDS_PER_BLOCK          1018	/* Node IDs in an Indirect Block */
    691 
    692 #define	NODE_DIR1_BLOCK		(DEF_ADDRS_PER_INODE + 1)
    693 #define	NODE_DIR2_BLOCK		(DEF_ADDRS_PER_INODE + 2)
    694 #define	NODE_IND1_BLOCK		(DEF_ADDRS_PER_INODE + 3)
    695 #define	NODE_IND2_BLOCK		(DEF_ADDRS_PER_INODE + 4)
    696 #define	NODE_DIND_BLOCK		(DEF_ADDRS_PER_INODE + 5)
    697 
    698 #define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
    699 #define F2FS_INLINE_DATA	0x02	/* file inline data flag */
    700 #define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
    701 #define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
    702 #define F2FS_INLINE_DOTS	0x10	/* file having implicit dot dentries */
    703 #define F2FS_EXTRA_ATTR		0x20	/* file having extra attribute */
    704 
    705 #if !defined(offsetof)
    706 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
    707 #endif
    708 
    709 #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
    710 	(offsetof(struct f2fs_inode, i_extra_end) -	\
    711 	offsetof(struct f2fs_inode, i_extra_isize))	\
    712 
    713 #define	F2FS_DEF_PROJID		0	/* default project ID */
    714 
    715 #define MAX_INLINE_DATA(node) (sizeof(__le32) *				\
    716 				(DEF_ADDRS_PER_INODE -			\
    717 				get_inline_xattr_addrs(&node->i) -	\
    718 				get_extra_isize(node) -			\
    719 				DEF_INLINE_RESERVED_SIZE))
    720 #define DEF_MAX_INLINE_DATA	(sizeof(__le32) *			\
    721 				(DEF_ADDRS_PER_INODE -			\
    722 				DEFAULT_INLINE_XATTR_ADDRS -		\
    723 				F2FS_TOTAL_EXTRA_ATTR_SIZE -		\
    724 				DEF_INLINE_RESERVED_SIZE))
    725 #define INLINE_DATA_OFFSET	(PAGE_CACHE_SIZE - sizeof(struct node_footer) \
    726 				- sizeof(__le32)*(DEF_ADDRS_PER_INODE + 5 - \
    727 				DEF_INLINE_RESERVED_SIZE))
    728 
    729 #define DEF_DIR_LEVEL		0
    730 
    731 /*
    732  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
    733  */
    734 #define FADVISE_COLD_BIT	0x01
    735 #define FADVISE_LOST_PINO_BIT	0x02
    736 #define FADVISE_ENCRYPT_BIT	0x04
    737 #define FADVISE_ENC_NAME_BIT	0x08
    738 #define FADVISE_KEEP_SIZE_BIT	0x10
    739 #define FADVISE_HOT_BIT		0x20
    740 #define FADVISE_VERITY_BIT	0x40	/* reserved */
    741 
    742 #define file_is_encrypt(fi)      ((fi)->i_advise & FADVISE_ENCRYPT_BIT)
    743 #define file_enc_name(fi)        ((fi)->i_advise & FADVISE_ENC_NAME_BIT)
    744 
    745 struct f2fs_inode {
    746 	__le16 i_mode;			/* file mode */
    747 	__u8 i_advise;			/* file hints */
    748 	__u8 i_inline;			/* file inline flags */
    749 	__le32 i_uid;			/* user ID */
    750 	__le32 i_gid;			/* group ID */
    751 	__le32 i_links;			/* links count */
    752 	__le64 i_size;			/* file size in bytes */
    753 	__le64 i_blocks;		/* file size in blocks */
    754 	__le64 i_atime;			/* access time */
    755 	__le64 i_ctime;			/* change time */
    756 	__le64 i_mtime;			/* modification time */
    757 	__le32 i_atime_nsec;		/* access time in nano scale */
    758 	__le32 i_ctime_nsec;		/* change time in nano scale */
    759 	__le32 i_mtime_nsec;		/* modification time in nano scale */
    760 	__le32 i_generation;		/* file version (for NFS) */
    761 	__le32 i_current_depth;		/* only for directory depth */
    762 	__le32 i_xattr_nid;		/* nid to save xattr */
    763 	__le32 i_flags;			/* file attributes */
    764 	__le32 i_pino;			/* parent inode number */
    765 	__le32 i_namelen;		/* file name length */
    766 	__u8 i_name[F2FS_NAME_LEN];	/* file name for SPOR */
    767 	__u8 i_dir_level;		/* dentry_level for large dir */
    768 
    769 	struct f2fs_extent i_ext;	/* caching a largest extent */
    770 
    771 	union {
    772 		struct {
    773 			__le16 i_extra_isize;	/* extra inode attribute size */
    774 			__le16 i_inline_xattr_size;	/* inline xattr size, unit: 4 bytes */
    775 			__le32 i_projid;	/* project id */
    776 			__le32 i_inode_checksum;/* inode meta checksum */
    777 			__le64 i_crtime;	/* creation time */
    778 			__le32 i_crtime_nsec;	/* creation time in nano scale */
    779 			__le32 i_extra_end[0];	/* for attribute size calculation */
    780 		} __attribute__((packed));
    781 		__le32 i_addr[DEF_ADDRS_PER_INODE];	/* Pointers to data blocks */
    782 	};
    783 	__le32 i_nid[5];		/* direct(2), indirect(2),
    784 						double_indirect(1) node id */
    785 } __attribute__((packed));
    786 
    787 
    788 struct direct_node {
    789 	__le32 addr[ADDRS_PER_BLOCK];	/* array of data block address */
    790 } __attribute__((packed));
    791 
    792 struct indirect_node {
    793 	__le32 nid[NIDS_PER_BLOCK];	/* array of data block address */
    794 } __attribute__((packed));
    795 
    796 enum {
    797 	COLD_BIT_SHIFT = 0,
    798 	FSYNC_BIT_SHIFT,
    799 	DENT_BIT_SHIFT,
    800 	OFFSET_BIT_SHIFT
    801 };
    802 
    803 #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
    804 				>> OFFSET_BIT_SHIFT)
    805 struct node_footer {
    806 	__le32 nid;		/* node id */
    807 	__le32 ino;		/* inode nunmber */
    808 	__le32 flag;		/* include cold/fsync/dentry marks and offset */
    809 	__le64 cp_ver;		/* checkpoint version */
    810 	__le32 next_blkaddr;	/* next node page block address */
    811 } __attribute__((packed));
    812 
    813 struct f2fs_node {
    814 	/* can be one of three types: inode, direct, and indirect types */
    815 	union {
    816 		struct f2fs_inode i;
    817 		struct direct_node dn;
    818 		struct indirect_node in;
    819 	};
    820 	struct node_footer footer;
    821 } __attribute__((packed));
    822 
    823 /*
    824  * For NAT entries
    825  */
    826 #define NAT_ENTRY_PER_BLOCK (PAGE_CACHE_SIZE / sizeof(struct f2fs_nat_entry))
    827 #define NAT_BLOCK_OFFSET(start_nid) (start_nid / NAT_ENTRY_PER_BLOCK)
    828 
    829 struct f2fs_nat_entry {
    830 	__u8 version;		/* latest version of cached nat entry */
    831 	__le32 ino;		/* inode number */
    832 	__le32 block_addr;	/* block address */
    833 } __attribute__((packed));
    834 
    835 struct f2fs_nat_block {
    836 	struct f2fs_nat_entry entries[NAT_ENTRY_PER_BLOCK];
    837 } __attribute__((packed));
    838 
    839 /*
    840  * For SIT entries
    841  *
    842  * Each segment is 2MB in size by default so that a bitmap for validity of
    843  * there-in blocks should occupy 64 bytes, 512 bits.
    844  * Not allow to change this.
    845  */
    846 #define SIT_VBLOCK_MAP_SIZE 64
    847 #define SIT_ENTRY_PER_BLOCK (PAGE_CACHE_SIZE / sizeof(struct f2fs_sit_entry))
    848 
    849 /*
    850  * F2FS uses 4 bytes to represent block address. As a result, supported size of
    851  * disk is 16 TB and it equals to 16 * 1024 * 1024 / 2 segments.
    852  */
    853 #define F2FS_MAX_SEGMENT       ((16 * 1024 * 1024) / 2)
    854 #define MAX_SIT_BITMAP_SIZE    (SEG_ALIGN(SIZE_ALIGN(F2FS_MAX_SEGMENT, \
    855 						SIT_ENTRY_PER_BLOCK)) * \
    856 						c.blks_per_seg / 8)
    857 
    858 /*
    859  * Note that f2fs_sit_entry->vblocks has the following bit-field information.
    860  * [15:10] : allocation type such as CURSEG_XXXX_TYPE
    861  * [9:0] : valid block count
    862  */
    863 #define SIT_VBLOCKS_SHIFT	10
    864 #define SIT_VBLOCKS_MASK	((1 << SIT_VBLOCKS_SHIFT) - 1)
    865 #define GET_SIT_VBLOCKS(raw_sit)				\
    866 	(le16_to_cpu((raw_sit)->vblocks) & SIT_VBLOCKS_MASK)
    867 #define GET_SIT_TYPE(raw_sit)					\
    868 	((le16_to_cpu((raw_sit)->vblocks) & ~SIT_VBLOCKS_MASK)	\
    869 	 >> SIT_VBLOCKS_SHIFT)
    870 
    871 struct f2fs_sit_entry {
    872 	__le16 vblocks;				/* reference above */
    873 	__u8 valid_map[SIT_VBLOCK_MAP_SIZE];	/* bitmap for valid blocks */
    874 	__le64 mtime;				/* segment age for cleaning */
    875 } __attribute__((packed));
    876 
    877 struct f2fs_sit_block {
    878 	struct f2fs_sit_entry entries[SIT_ENTRY_PER_BLOCK];
    879 } __attribute__((packed));
    880 
    881 /*
    882  * For segment summary
    883  *
    884  * One summary block contains exactly 512 summary entries, which represents
    885  * exactly 2MB segment by default. Not allow to change the basic units.
    886  *
    887  * NOTE: For initializing fields, you must use set_summary
    888  *
    889  * - If data page, nid represents dnode's nid
    890  * - If node page, nid represents the node page's nid.
    891  *
    892  * The ofs_in_node is used by only data page. It represents offset
    893  * from node's page's beginning to get a data block address.
    894  * ex) data_blkaddr = (block_t)(nodepage_start_address + ofs_in_node)
    895  */
    896 #define ENTRIES_IN_SUM		512
    897 #define	SUMMARY_SIZE		(7)	/* sizeof(struct summary) */
    898 #define	SUM_FOOTER_SIZE		(5)	/* sizeof(struct summary_footer) */
    899 #define SUM_ENTRIES_SIZE	(SUMMARY_SIZE * ENTRIES_IN_SUM)
    900 
    901 /* a summary entry for a 4KB-sized block in a segment */
    902 struct f2fs_summary {
    903 	__le32 nid;		/* parent node id */
    904 	union {
    905 		__u8 reserved[3];
    906 		struct {
    907 			__u8 version;		/* node version number */
    908 			__le16 ofs_in_node;	/* block index in parent node */
    909 		} __attribute__((packed));
    910 	};
    911 } __attribute__((packed));
    912 
    913 /* summary block type, node or data, is stored to the summary_footer */
    914 #define SUM_TYPE_NODE		(1)
    915 #define SUM_TYPE_DATA		(0)
    916 
    917 struct summary_footer {
    918 	unsigned char entry_type;	/* SUM_TYPE_XXX */
    919 	__le32 check_sum;		/* summary checksum */
    920 } __attribute__((packed));
    921 
    922 #define SUM_JOURNAL_SIZE	(F2FS_BLKSIZE - SUM_FOOTER_SIZE -\
    923 				SUM_ENTRIES_SIZE)
    924 #define NAT_JOURNAL_ENTRIES	((SUM_JOURNAL_SIZE - 2) /\
    925 				sizeof(struct nat_journal_entry))
    926 #define NAT_JOURNAL_RESERVED	((SUM_JOURNAL_SIZE - 2) %\
    927 				sizeof(struct nat_journal_entry))
    928 #define SIT_JOURNAL_ENTRIES	((SUM_JOURNAL_SIZE - 2) /\
    929 				sizeof(struct sit_journal_entry))
    930 #define SIT_JOURNAL_RESERVED	((SUM_JOURNAL_SIZE - 2) %\
    931 				sizeof(struct sit_journal_entry))
    932 
    933 /*
    934  * Reserved area should make size of f2fs_extra_info equals to
    935  * that of nat_journal and sit_journal.
    936  */
    937 #define EXTRA_INFO_RESERVED	(SUM_JOURNAL_SIZE - 2 - 8)
    938 
    939 /*
    940  * frequently updated NAT/SIT entries can be stored in the spare area in
    941  * summary blocks
    942  */
    943 enum {
    944 	NAT_JOURNAL = 0,
    945 	SIT_JOURNAL
    946 };
    947 
    948 struct nat_journal_entry {
    949 	__le32 nid;
    950 	struct f2fs_nat_entry ne;
    951 } __attribute__((packed));
    952 
    953 struct nat_journal {
    954 	struct nat_journal_entry entries[NAT_JOURNAL_ENTRIES];
    955 	__u8 reserved[NAT_JOURNAL_RESERVED];
    956 } __attribute__((packed));
    957 
    958 struct sit_journal_entry {
    959 	__le32 segno;
    960 	struct f2fs_sit_entry se;
    961 } __attribute__((packed));
    962 
    963 struct sit_journal {
    964 	struct sit_journal_entry entries[SIT_JOURNAL_ENTRIES];
    965 	__u8 reserved[SIT_JOURNAL_RESERVED];
    966 } __attribute__((packed));
    967 
    968 struct f2fs_extra_info {
    969 	__le64 kbytes_written;
    970 	__u8 reserved[EXTRA_INFO_RESERVED];
    971 } __attribute__((packed));
    972 
    973 struct f2fs_journal {
    974 	union {
    975 		__le16 n_nats;
    976 		__le16 n_sits;
    977 	};
    978 	/* spare area is used by NAT or SIT journals or extra info */
    979 	union {
    980 		struct nat_journal nat_j;
    981 		struct sit_journal sit_j;
    982 		struct f2fs_extra_info info;
    983 	};
    984 } __attribute__((packed));
    985 
    986 /* 4KB-sized summary block structure */
    987 struct f2fs_summary_block {
    988 	struct f2fs_summary entries[ENTRIES_IN_SUM];
    989 	struct f2fs_journal journal;
    990 	struct summary_footer footer;
    991 } __attribute__((packed));
    992 
    993 /*
    994  * For directory operations
    995  */
    996 #define F2FS_DOT_HASH		0
    997 #define F2FS_DDOT_HASH		F2FS_DOT_HASH
    998 #define F2FS_MAX_HASH		(~((0x3ULL) << 62))
    999 #define F2FS_HASH_COL_BIT	((0x1ULL) << 63)
   1000 
   1001 typedef __le32	f2fs_hash_t;
   1002 
   1003 /* One directory entry slot covers 8bytes-long file name */
   1004 #define F2FS_SLOT_LEN		8
   1005 #define F2FS_SLOT_LEN_BITS	3
   1006 
   1007 #define GET_DENTRY_SLOTS(x)	((x + F2FS_SLOT_LEN - 1) >> F2FS_SLOT_LEN_BITS)
   1008 
   1009 /* the number of dentry in a block */
   1010 #define NR_DENTRY_IN_BLOCK	214
   1011 
   1012 /* MAX level for dir lookup */
   1013 #define MAX_DIR_HASH_DEPTH	63
   1014 
   1015 /* MAX buckets in one level of dir */
   1016 #define MAX_DIR_BUCKETS		(1 << ((MAX_DIR_HASH_DEPTH / 2) - 1))
   1017 
   1018 #define SIZE_OF_DIR_ENTRY	11	/* by byte */
   1019 #define SIZE_OF_DENTRY_BITMAP	((NR_DENTRY_IN_BLOCK + BITS_PER_BYTE - 1) / \
   1020 					BITS_PER_BYTE)
   1021 #define SIZE_OF_RESERVED	(PAGE_SIZE - ((SIZE_OF_DIR_ENTRY + \
   1022 				F2FS_SLOT_LEN) * \
   1023 				NR_DENTRY_IN_BLOCK + SIZE_OF_DENTRY_BITMAP))
   1024 
   1025 /* One directory entry slot representing F2FS_SLOT_LEN-sized file name */
   1026 struct f2fs_dir_entry {
   1027 	__le32 hash_code;	/* hash code of file name */
   1028 	__le32 ino;		/* inode number */
   1029 	__le16 name_len;	/* lengh of file name */
   1030 	__u8 file_type;		/* file type */
   1031 } __attribute__((packed));
   1032 
   1033 /* 4KB-sized directory entry block */
   1034 struct f2fs_dentry_block {
   1035 	/* validity bitmap for directory entries in each block */
   1036 	__u8 dentry_bitmap[SIZE_OF_DENTRY_BITMAP];
   1037 	__u8 reserved[SIZE_OF_RESERVED];
   1038 	struct f2fs_dir_entry dentry[NR_DENTRY_IN_BLOCK];
   1039 	__u8 filename[NR_DENTRY_IN_BLOCK][F2FS_SLOT_LEN];
   1040 } __attribute__((packed));
   1041 #pragma pack(pop)
   1042 
   1043 /* for inline stuff */
   1044 #define DEF_INLINE_RESERVED_SIZE	1
   1045 
   1046 /* for inline dir */
   1047 #define NR_INLINE_DENTRY(node)	(MAX_INLINE_DATA(node) * BITS_PER_BYTE / \
   1048 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
   1049 				BITS_PER_BYTE + 1))
   1050 #define INLINE_DENTRY_BITMAP_SIZE(node)	((NR_INLINE_DENTRY(node) + \
   1051 					BITS_PER_BYTE - 1) / BITS_PER_BYTE)
   1052 #define INLINE_RESERVED_SIZE(node)	(MAX_INLINE_DATA(node) - \
   1053 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
   1054 				NR_INLINE_DENTRY(node) + \
   1055 				INLINE_DENTRY_BITMAP_SIZE(node)))
   1056 
   1057 /* file types used in inode_info->flags */
   1058 enum FILE_TYPE {
   1059 	F2FS_FT_UNKNOWN,
   1060 	F2FS_FT_REG_FILE,
   1061 	F2FS_FT_DIR,
   1062 	F2FS_FT_CHRDEV,
   1063 	F2FS_FT_BLKDEV,
   1064 	F2FS_FT_FIFO,
   1065 	F2FS_FT_SOCK,
   1066 	F2FS_FT_SYMLINK,
   1067 	F2FS_FT_MAX,
   1068 	/* added for fsck */
   1069 	F2FS_FT_ORPHAN,
   1070 	F2FS_FT_XATTR,
   1071 	F2FS_FT_LAST_FILE_TYPE = F2FS_FT_XATTR,
   1072 };
   1073 
   1074 /* from f2fs/segment.h */
   1075 enum {
   1076 	LFS = 0,
   1077 	SSR
   1078 };
   1079 
   1080 extern int utf8_to_utf16(u_int16_t *, const char *, size_t, size_t);
   1081 extern int utf16_to_utf8(char *, const u_int16_t *, size_t, size_t);
   1082 extern int log_base_2(u_int32_t);
   1083 extern unsigned int addrs_per_inode(struct f2fs_inode *);
   1084 extern __u32 f2fs_inode_chksum(struct f2fs_node *);
   1085 
   1086 extern int get_bits_in_byte(unsigned char n);
   1087 extern int test_and_set_bit_le(u32, u8 *);
   1088 extern int test_and_clear_bit_le(u32, u8 *);
   1089 extern int test_bit_le(u32, const u8 *);
   1090 extern int f2fs_test_bit(unsigned int, const char *);
   1091 extern int f2fs_set_bit(unsigned int, char *);
   1092 extern int f2fs_clear_bit(unsigned int, char *);
   1093 extern u64 find_next_bit_le(const u8 *, u64, u64);
   1094 extern u64 find_next_zero_bit_le(const u8 *, u64, u64);
   1095 
   1096 extern u_int32_t f2fs_cal_crc32(u_int32_t, void *, int);
   1097 extern int f2fs_crc_valid(u_int32_t blk_crc, void *buf, int len);
   1098 
   1099 extern void f2fs_init_configuration(void);
   1100 extern int f2fs_devs_are_umounted(void);
   1101 extern int f2fs_dev_is_umounted(char *);
   1102 extern int f2fs_get_device_info(void);
   1103 extern int get_device_info(int);
   1104 extern int f2fs_init_sparse_file(void);
   1105 extern int f2fs_finalize_device(void);
   1106 extern int f2fs_fsync_device(void);
   1107 
   1108 extern int dev_read(void *, __u64, size_t);
   1109 extern int dev_write(void *, __u64, size_t);
   1110 extern int dev_write_block(void *, __u64);
   1111 extern int dev_write_dump(void *, __u64, size_t);
   1112 /* All bytes in the buffer must be 0 use dev_fill(). */
   1113 extern int dev_fill(void *, __u64, size_t);
   1114 extern int dev_fill_block(void *, __u64);
   1115 
   1116 extern int dev_read_block(void *, __u64);
   1117 extern int dev_reada_block(__u64);
   1118 
   1119 extern int dev_read_version(void *, __u64, size_t);
   1120 extern void get_kernel_version(__u8 *);
   1121 extern void get_kernel_uname_version(__u8 *);
   1122 f2fs_hash_t f2fs_dentry_hash(const unsigned char *, int);
   1123 
   1124 static inline bool f2fs_has_extra_isize(struct f2fs_inode *inode)
   1125 {
   1126 	return (inode->i_inline & F2FS_EXTRA_ATTR);
   1127 }
   1128 
   1129 static inline int __get_extra_isize(struct f2fs_inode *inode)
   1130 {
   1131 	if (f2fs_has_extra_isize(inode))
   1132 		return le16_to_cpu(inode->i_extra_isize) / sizeof(__le32);
   1133 	return 0;
   1134 }
   1135 
   1136 extern struct f2fs_configuration c;
   1137 static inline int get_inline_xattr_addrs(struct f2fs_inode *inode)
   1138 {
   1139 	if (c.feature & cpu_to_le32(F2FS_FEATURE_FLEXIBLE_INLINE_XATTR))
   1140 		return le16_to_cpu(inode->i_inline_xattr_size);
   1141 	else if (inode->i_inline & F2FS_INLINE_XATTR ||
   1142 			inode->i_inline & F2FS_INLINE_DENTRY)
   1143 		return DEFAULT_INLINE_XATTR_ADDRS;
   1144 	else
   1145 		return 0;
   1146 }
   1147 
   1148 #define get_extra_isize(node)	__get_extra_isize(&node->i)
   1149 
   1150 #define F2FS_ZONED_NONE		0
   1151 #define F2FS_ZONED_HA		1
   1152 #define F2FS_ZONED_HM		2
   1153 
   1154 #ifdef HAVE_LINUX_BLKZONED_H
   1155 
   1156 #define blk_zone_type(z)        (z)->type
   1157 #define blk_zone_conv(z)	((z)->type == BLK_ZONE_TYPE_CONVENTIONAL)
   1158 #define blk_zone_seq_req(z)	((z)->type == BLK_ZONE_TYPE_SEQWRITE_REQ)
   1159 #define blk_zone_seq_pref(z)	((z)->type == BLK_ZONE_TYPE_SEQWRITE_PREF)
   1160 #define blk_zone_seq(z)		(blk_zone_seq_req(z) || blk_zone_seq_pref(z))
   1161 
   1162 static inline const char *
   1163 blk_zone_type_str(struct blk_zone *blkz)
   1164 {
   1165 	switch (blk_zone_type(blkz)) {
   1166 	case BLK_ZONE_TYPE_CONVENTIONAL:
   1167 		return( "Conventional" );
   1168 	case BLK_ZONE_TYPE_SEQWRITE_REQ:
   1169 		return( "Sequential-write-required" );
   1170 	case BLK_ZONE_TYPE_SEQWRITE_PREF:
   1171 		return( "Sequential-write-preferred" );
   1172 	}
   1173 	return( "Unknown-type" );
   1174 }
   1175 
   1176 #define blk_zone_cond(z)	(z)->cond
   1177 
   1178 static inline const char *
   1179 blk_zone_cond_str(struct blk_zone *blkz)
   1180 {
   1181 	switch (blk_zone_cond(blkz)) {
   1182 	case BLK_ZONE_COND_NOT_WP:
   1183 		return "Not-write-pointer";
   1184 	case BLK_ZONE_COND_EMPTY:
   1185 		return "Empty";
   1186 	case BLK_ZONE_COND_IMP_OPEN:
   1187 		return "Implicit-open";
   1188 	case BLK_ZONE_COND_EXP_OPEN:
   1189 		return "Explicit-open";
   1190 	case BLK_ZONE_COND_CLOSED:
   1191 		return "Closed";
   1192 	case BLK_ZONE_COND_READONLY:
   1193 		return "Read-only";
   1194 	case BLK_ZONE_COND_FULL:
   1195 		return "Full";
   1196 	case BLK_ZONE_COND_OFFLINE:
   1197 		return "Offline";
   1198 	}
   1199 	return "Unknown-cond";
   1200 }
   1201 
   1202 #define blk_zone_empty(z)	(blk_zone_cond(z) == BLK_ZONE_COND_EMPTY)
   1203 
   1204 #define blk_zone_sector(z)	(z)->start
   1205 #define blk_zone_length(z)	(z)->len
   1206 #define blk_zone_wp_sector(z)	(z)->wp
   1207 #define blk_zone_need_reset(z)	(int)(z)->reset
   1208 #define blk_zone_non_seq(z)	(int)(z)->non_seq
   1209 
   1210 #endif
   1211 
   1212 extern void f2fs_get_zoned_model(int);
   1213 extern int f2fs_get_zone_blocks(int);
   1214 extern int f2fs_check_zones(int);
   1215 extern int f2fs_reset_zones(int);
   1216 
   1217 extern struct f2fs_configuration c;
   1218 
   1219 #define SIZE_ALIGN(val, size)	((val) + (size) - 1) / (size)
   1220 #define SEG_ALIGN(blks)		SIZE_ALIGN(blks, c.blks_per_seg)
   1221 #define ZONE_ALIGN(blks)	SIZE_ALIGN(blks, c.blks_per_seg * \
   1222 					c.segs_per_zone)
   1223 
   1224 static inline double get_best_overprovision(struct f2fs_super_block *sb)
   1225 {
   1226 	double reserved, ovp, candidate, end, diff, space;
   1227 	double max_ovp = 0, max_space = 0;
   1228 
   1229 	if (get_sb(segment_count_main) < 256) {
   1230 		candidate = 10;
   1231 		end = 95;
   1232 		diff = 5;
   1233 	} else {
   1234 		candidate = 0.01;
   1235 		end = 10;
   1236 		diff = 0.01;
   1237 	}
   1238 
   1239 	for (; candidate <= end; candidate += diff) {
   1240 		reserved = (2 * (100 / candidate + 1) + 6) *
   1241 						get_sb(segs_per_sec);
   1242 		ovp = (get_sb(segment_count_main) - reserved) * candidate / 100;
   1243 		space = get_sb(segment_count_main) - reserved - ovp;
   1244 		if (max_space < space) {
   1245 			max_space = space;
   1246 			max_ovp = candidate;
   1247 		}
   1248 	}
   1249 	return max_ovp;
   1250 }
   1251 
   1252 static inline __le64 get_cp_crc(struct f2fs_checkpoint *cp)
   1253 {
   1254 	u_int64_t cp_ver = get_cp(checkpoint_ver);
   1255 	size_t crc_offset = get_cp(checksum_offset);
   1256 	u_int32_t crc = le32_to_cpu(*(__le32 *)((unsigned char *)cp +
   1257 							crc_offset));
   1258 
   1259 	cp_ver |= ((u_int64_t)crc << 32);
   1260 	return cpu_to_le64(cp_ver);
   1261 }
   1262 
   1263 static inline int exist_qf_ino(struct f2fs_super_block *sb)
   1264 {
   1265 	int i;
   1266 
   1267 	for (i = 0; i < F2FS_MAX_QUOTAS; i++)
   1268 		if (sb->qf_ino[i])
   1269 			return 1;
   1270 	return 0;
   1271 }
   1272 
   1273 static inline int is_qf_ino(struct f2fs_super_block *sb, nid_t ino)
   1274 {
   1275 	int i;
   1276 
   1277 	for (i = 0; i < F2FS_MAX_QUOTAS; i++)
   1278 		if (sb->qf_ino[i] == ino)
   1279 			return 1;
   1280 	return 0;
   1281 }
   1282 
   1283 #endif	/*__F2FS_FS_H */
   1284