4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 * Copyright (c) 2019 Google Inc.
7 * http://www.google.com/
8 * Copyright (c) 2020 Google Inc.
9 * Robin Hsu <robinhsu@google.com>
10 * : add sload compression support
12 * Dual licensed under the GPL or LGPL version 2 licenses.
14 * The byteswap codes are copied from:
15 * samba_3_master/lib/ccan/endian/endian.h under LGPL 2.1
20 #ifndef __SANE_USERSPACE_TYPES__
21 #define __SANE_USERSPACE_TYPES__ /* For PPC64, to get LL64 types */
37 #endif /* HAVE_CONFIG_H */
40 #include <android_config.h>
50 #ifdef HAVE_LINUX_TYPES_H
51 #include <linux/types.h>
53 #include <sys/types.h>
55 #ifdef HAVE_KERNEL_UAPI_LINUX_BLKZONED_H
56 #include <kernel/uapi/linux/blkzoned.h>
57 #elif defined(HAVE_LINUX_BLKZONED_H)
58 #include <linux/blkzoned.h>
61 #ifdef HAVE_LIBSELINUX
62 #include <selinux/selinux.h>
63 #include <selinux/label.h>
67 #elif defined(__GNUC__)
68 # define UNUSED(x) UNUSED_ ## x __attribute__((unused))
69 #elif defined(__LCLINT__)
71 #elif defined(__cplusplus)
78 #define static_assert _Static_assert
81 #ifdef HAVE_SYS_MOUNT_H
82 #include <sys/mount.h>
87 #define fallthrough do {} while (0) /* fall through */
89 #define fallthrough __attribute__((__fallthrough__))
94 #undef HAVE_LINUX_TYPES_H
97 /* codes from kernel's f2fs.h, GPL-v2.0 */
98 #define MIN_COMPRESS_LOG_SIZE 2
99 #define MAX_COMPRESS_LOG_SIZE 8
101 typedef uint64_t u64;
102 typedef uint32_t u32;
103 typedef uint16_t u16;
110 typedef unsigned long pgoff_t;
111 typedef unsigned short umode_t;
113 #ifndef HAVE_LINUX_TYPES_H
127 * code borrowed from kernel f2fs dirver: f2fs.h, GPL-2.0
128 * : definitions of COMPRESS_DATA_RESERVED_SIZE,
129 * struct compress_data, COMPRESS_HEADER_SIZE,
130 * and struct compress_ctx
132 #define COMPRESS_DATA_RESERVED_SIZE 4
133 struct compress_data {
134 __le32 clen; /* compressed data size */
135 __le32 chksum; /* checksum of compressed data */
136 __le32 reserved[COMPRESS_DATA_RESERVED_SIZE]; /* reserved */
137 u8 cdata[]; /* compressed data */
139 #define COMPRESS_HEADER_SIZE (sizeof(struct compress_data))
140 /* compress context */
141 struct compress_ctx {
142 unsigned int cluster_size; /* page count in cluster */
143 unsigned int log_cluster_size; /* log of cluster size */
144 void *rbuf; /* compression input buffer */
145 struct compress_data *cbuf; /* comprsssion output header + data */
146 size_t rlen; /* valid data length in rbuf */
147 size_t clen; /* valid data length in cbuf */
148 void *private; /* work buf for compress algorithm */
152 #include <byteswap.h>
155 * bswap_16 - reverse bytes in a uint16_t value.
156 * @val: value whose bytes to swap.
159 * // Output contains "1024 is 4 as two bytes reversed"
160 * printf("1024 is %u as two bytes reversed\n", bswap_16(1024));
162 static inline uint16_t bswap_16(uint16_t val)
164 return ((val & (uint16_t)0x00ffU) << 8)
165 | ((val & (uint16_t)0xff00U) >> 8);
169 * bswap_32 - reverse bytes in a uint32_t value.
170 * @val: value whose bytes to swap.
173 * // Output contains "1024 is 262144 as four bytes reversed"
174 * printf("1024 is %u as four bytes reversed\n", bswap_32(1024));
176 static inline uint32_t bswap_32(uint32_t val)
178 return ((val & (uint32_t)0x000000ffUL) << 24)
179 | ((val & (uint32_t)0x0000ff00UL) << 8)
180 | ((val & (uint32_t)0x00ff0000UL) >> 8)
181 | ((val & (uint32_t)0xff000000UL) >> 24);
183 #endif /* !HAVE_BYTESWAP_H */
185 #if defined HAVE_DECL_BSWAP_64 && !HAVE_DECL_BSWAP_64
187 * bswap_64 - reverse bytes in a uint64_t value.
188 * @val: value whose bytes to swap.
191 * // Output contains "1024 is 1125899906842624 as eight bytes reversed"
192 * printf("1024 is %llu as eight bytes reversed\n",
193 * (unsigned long long)bswap_64(1024));
195 static inline uint64_t bswap_64(uint64_t val)
197 return ((val & (uint64_t)0x00000000000000ffULL) << 56)
198 | ((val & (uint64_t)0x000000000000ff00ULL) << 40)
199 | ((val & (uint64_t)0x0000000000ff0000ULL) << 24)
200 | ((val & (uint64_t)0x00000000ff000000ULL) << 8)
201 | ((val & (uint64_t)0x000000ff00000000ULL) >> 8)
202 | ((val & (uint64_t)0x0000ff0000000000ULL) >> 24)
203 | ((val & (uint64_t)0x00ff000000000000ULL) >> 40)
204 | ((val & (uint64_t)0xff00000000000000ULL) >> 56);
208 #if __BYTE_ORDER == __LITTLE_ENDIAN
209 #define le16_to_cpu(x) ((uint16_t)(x))
210 #define le32_to_cpu(x) ((uint32_t)(x))
211 #define le64_to_cpu(x) ((uint64_t)(x))
212 #define cpu_to_le16(x) ((uint16_t)(x))
213 #define cpu_to_le32(x) ((uint32_t)(x))
214 #define cpu_to_le64(x) ((uint64_t)(x))
215 #define be32_to_cpu(x) __builtin_bswap64(x)
216 #elif __BYTE_ORDER == __BIG_ENDIAN
217 #define le16_to_cpu(x) bswap_16(x)
218 #define le32_to_cpu(x) bswap_32(x)
219 #define le64_to_cpu(x) bswap_64(x)
220 #define cpu_to_le16(x) bswap_16(x)
221 #define cpu_to_le32(x) bswap_32(x)
222 #define cpu_to_le64(x) bswap_64(x)
223 #define be32_to_cpu(x) ((uint64_t)(x))
226 #define typecheck(type,x) \
228 typeof(x) __dummy2; \
229 (void)(&__dummy == &__dummy2); \
233 #define NULL_SEGNO ((unsigned int)~0)
236 * Debugging interfaces
238 #define FIX_MSG(fmt, ...) \
240 printf("[FIX] (%s:%4d) ", __func__, __LINE__); \
241 printf(" --> "fmt"\n", ##__VA_ARGS__); \
244 #define ASSERT_MSG(fmt, ...) \
246 printf("[ASSERT] (%s:%4d) ", __func__, __LINE__); \
247 printf(" --> "fmt"\n", ##__VA_ARGS__); \
251 #define ASSERT(exp) \
254 printf("[ASSERT] (%s:%4d) %s\n", \
255 __func__, __LINE__, #exp); \
260 #define ERR_MSG(fmt, ...) \
262 printf("[%s:%d] " fmt, __func__, __LINE__, ##__VA_ARGS__); \
265 #define MSG(n, fmt, ...) \
267 if (c.dbg_lv >= n && !c.layout && !c.show_file_map) { \
268 printf(fmt, ##__VA_ARGS__); \
272 #define DBG(n, fmt, ...) \
274 if (c.dbg_lv >= n && !c.layout && !c.show_file_map) { \
275 printf("[%s:%4d] " fmt, \
276 __func__, __LINE__, ##__VA_ARGS__); \
280 /* Display on console */
281 #define DISP(fmt, ptr, member) \
283 printf("%-30s" fmt, #member, ((ptr)->member)); \
286 #define DISP_raw_str(fmt, member) \
289 printf("%-30s " fmt "\n", #member":", member); \
291 printf("%-30s" "\t\t[" fmt "]\n", \
295 #define DISP_str(fmt, ptr, member) \
298 printf("%-30s " fmt "\n", \
299 #member":", ((ptr)->member)); \
301 printf("%-30s" "\t\t[" fmt "]\n", \
302 #member, ((ptr)->member)); \
305 #define DISP_u8(ptr, member) \
307 assert(sizeof((ptr)->member) == 1); \
309 printf("%-30s %u\n", \
310 #member":", ((ptr)->member)); \
312 printf("%-30s" "\t\t[0x%8x : %u]\n", \
313 #member, ((ptr)->member), ((ptr)->member)); \
316 #define DISP_u16(ptr, member) \
318 assert(sizeof((ptr)->member) == 2); \
320 printf("%-30s %u\n", \
321 #member":", le16_to_cpu(((ptr)->member))); \
323 printf("%-30s" "\t\t[0x%8x : %u]\n", \
324 #member, le16_to_cpu(((ptr)->member)), \
325 le16_to_cpu(((ptr)->member))); \
328 #define DISP_u32(ptr, member) \
330 assert(sizeof((ptr)->member) <= 4); \
332 printf("%-30s %u\n", \
333 #member":", le32_to_cpu(((ptr)->member))); \
335 printf("%-30s" "\t\t[0x%8x : %u]\n", \
336 #member, le32_to_cpu(((ptr)->member)), \
337 le32_to_cpu(((ptr)->member))); \
340 #define DISP_u64(ptr, member) \
342 assert(sizeof((ptr)->member) == 8); \
344 printf("%-30s %" PRIu64 "\n", \
345 #member":", le64_to_cpu(((ptr)->member))); \
347 printf("%-30s" "\t\t[0x%8" PRIx64 " : %" PRIu64 "]\n", \
348 #member, le64_to_cpu(((ptr)->member)), \
349 le64_to_cpu(((ptr)->member))); \
352 #define DISP_utf(ptr, member) \
355 printf("%-30s %s\n", #member":", \
358 printf("%-30s" "\t\t[%s]\n", #member, \
362 /* Display to buffer */
363 #define BUF_DISP_u32(buf, data, len, ptr, member) \
365 assert(sizeof((ptr)->member) <= 4); \
366 snprintf(buf, len, #member); \
367 snprintf(data, len, "0x%x : %u", ((ptr)->member), \
371 #define BUF_DISP_u64(buf, data, len, ptr, member) \
373 assert(sizeof((ptr)->member) == 8); \
374 snprintf(buf, len, #member); \
375 snprintf(data, len, "0x%llx : %llu", ((ptr)->member), \
379 #define BUF_DISP_utf(buf, data, len, ptr, member) \
380 snprintf(buf, len, #member)
382 /* these are defined in kernel */
383 #define BITS_PER_BYTE 8
385 #define SECTOR_SHIFT 9
387 #define F2FS_SUPER_MAGIC 0xF2F52010 /* F2FS Magic Number */
388 #define CP_CHKSUM_OFFSET 4092
389 #define SB_CHKSUM_OFFSET 3068
390 #define MAX_PATH_LEN 64
391 #define MAX_DEVICES 8
393 #define F2FS_BYTES_TO_BLK(bytes) ((bytes) >> F2FS_BLKSIZE_BITS)
394 #define F2FS_BLKSIZE_BITS 12
397 #define F2FS_NUMBER_OF_CHECKPOINT_PACK 2
398 #define DEFAULT_SECTOR_SIZE 512
399 #define DEFAULT_SECTORS_PER_BLOCK 8
400 #define DEFAULT_BLOCKS_PER_SEGMENT 512
401 #define DEFAULT_SEGMENTS_PER_SECTION 1
403 #define VERSION_LEN 256
404 #define VERSION_TIMESTAMP_LEN 4
405 #define VERSION_NAME_LEN (VERSION_LEN - VERSION_TIMESTAMP_LEN)
407 #define LPF "lost+found"
409 /* one for gc buffer, the other for node */
410 #define MIN_RSVD_SECS (NR_CURSEG_TYPE + 2U)
411 #define CONFIG_RSVD_DEFAULT_OP_RATIO 3.0
413 enum f2fs_config_func {
431 uint32_t sector_size;
432 uint64_t total_sectors; /* got by get_device_info */
433 uint64_t start_blkaddr;
434 uint64_t end_blkaddr;
435 uint32_t total_segments;
437 /* to handle zone block devices */
440 uint32_t nr_rnd_zones;
443 size_t *zone_cap_blocks;
447 /* Value 0 means no cache, minimum 1024 */
448 long num_cache_entry;
450 /* Value 0 means always overwrite (no collision allowed). maximum 16 */
451 unsigned max_hash_collision;
454 } dev_cache_config_t;
456 /* f2fs_configration for compression used for sload.f2fs */
458 void (*init)(struct compress_ctx *cc);
459 int (*compress)(struct compress_ctx *cc);
460 void (*reset)(struct compress_ctx *cc);
463 /* Should be aligned to supported_comp_names and support_comp_ops */
464 enum compress_algorithms {
471 COMPR_FILTER_UNASSIGNED = 0,
477 void (*add)(const char *);
478 void (*destroy)(void);
479 bool (*filter)(const char *);
483 bool enabled; /* disabled by default */
484 bool required; /* require to enable */
485 bool readonly; /* readonly to release blocks */
486 struct compress_ctx cc; /* work context */
487 enum compress_algorithms alg; /* algorithm to compress */
488 compress_ops *ops; /* ops per algorithm */
489 unsigned int min_blocks; /* save more blocks than this */
490 enum filter_policy filter; /* filter to try compression */
491 filter_ops *filter_ops; /* filter ops */
494 #define ALIGN_DOWN(addrs, size) (((addrs) / (size)) * (size))
495 #define ALIGN_UP(addrs, size) ALIGN_DOWN(((addrs) + (size) - 1), (size))
498 #define BITS_PER_LONG 64
500 #define BITS_PER_LONG 32
503 #define BIT_MASK(nr) (1 << (nr % BITS_PER_LONG))
504 #define BIT_WORD(nr) (nr / BITS_PER_LONG)
506 #define set_sb_le64(member, val) (sb->member = cpu_to_le64(val))
507 #define set_sb_le32(member, val) (sb->member = cpu_to_le32(val))
508 #define set_sb_le16(member, val) (sb->member = cpu_to_le16(val))
509 #define get_sb_le64(member) le64_to_cpu(sb->member)
510 #define get_sb_le32(member) le32_to_cpu(sb->member)
511 #define get_sb_le16(member) le16_to_cpu(sb->member)
512 #define get_newsb_le64(member) le64_to_cpu(new_sb->member)
513 #define get_newsb_le32(member) le32_to_cpu(new_sb->member)
514 #define get_newsb_le16(member) le16_to_cpu(new_sb->member)
516 #define set_sb(member, val) \
518 typeof(sb->member) t = (val); \
519 switch (sizeof(t)) { \
520 case 8: set_sb_le64(member, t); break; \
521 case 4: set_sb_le32(member, t); break; \
522 case 2: set_sb_le16(member, t); break; \
526 #define get_sb(member) \
528 typeof(sb->member) t; \
529 switch (sizeof(t)) { \
530 case 8: t = get_sb_le64(member); break; \
531 case 4: t = get_sb_le32(member); break; \
532 case 2: t = get_sb_le16(member); break; \
536 #define get_newsb(member) \
538 typeof(new_sb->member) t; \
539 switch (sizeof(t)) { \
540 case 8: t = get_newsb_le64(member); break; \
541 case 4: t = get_newsb_le32(member); break; \
542 case 2: t = get_newsb_le16(member); break; \
547 #define set_cp_le64(member, val) (cp->member = cpu_to_le64(val))
548 #define set_cp_le32(member, val) (cp->member = cpu_to_le32(val))
549 #define set_cp_le16(member, val) (cp->member = cpu_to_le16(val))
550 #define get_cp_le64(member) le64_to_cpu(cp->member)
551 #define get_cp_le32(member) le32_to_cpu(cp->member)
552 #define get_cp_le16(member) le16_to_cpu(cp->member)
554 #define set_cp(member, val) \
556 typeof(cp->member) t = (val); \
557 switch (sizeof(t)) { \
558 case 8: set_cp_le64(member, t); break; \
559 case 4: set_cp_le32(member, t); break; \
560 case 2: set_cp_le16(member, t); break; \
564 #define get_cp(member) \
566 typeof(cp->member) t; \
567 switch (sizeof(t)) { \
568 case 8: t = get_cp_le64(member); break; \
569 case 4: t = get_cp_le32(member); break; \
570 case 2: t = get_cp_le16(member); break; \
576 * Copied from include/linux/kernel.h
578 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
579 #define round_down(x, y) ((x) & ~__round_mask(x, y))
581 #define min(x, y) ({ \
582 typeof(x) _min1 = (x); \
583 typeof(y) _min2 = (y); \
584 (void) (&_min1 == &_min2); \
585 _min1 < _min2 ? _min1 : _min2; })
587 #define max(x, y) ({ \
588 typeof(x) _max1 = (x); \
589 typeof(y) _max2 = (y); \
590 (void) (&_max1 == &_max2); \
591 _max1 > _max2 ? _max1 : _max2; })
593 #define round_up(x, y) (((x) + (y) - 1) / (y))
595 * Copied from fs/f2fs/f2fs.h
597 #define NR_CURSEG_DATA_TYPE (3)
598 #define NR_CURSEG_NODE_TYPE (3)
599 #define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
602 CURSEG_HOT_DATA = 0, /* directory entry blocks */
603 CURSEG_WARM_DATA, /* data blocks */
604 CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
605 CURSEG_HOT_NODE, /* direct node blocks of directory files */
606 CURSEG_WARM_NODE, /* direct node blocks of normal files */
607 CURSEG_COLD_NODE, /* indirect node blocks */
617 #define F2FS_MIN_SEGMENTS 9 /* SB + 2 (CP + SIT + NAT) + SSA + MAIN */
620 * Copied from fs/f2fs/segment.h
622 #define GET_SUM_TYPE(footer) ((footer)->entry_type)
623 #define SET_SUM_TYPE(footer, type) ((footer)->entry_type = type)
626 * Copied from include/linux/f2fs_sb.h
628 #define F2FS_SUPER_OFFSET 1024 /* byte-size offset */
629 #define F2FS_MIN_LOG_SECTOR_SIZE 9 /* 9 bits for 512 bytes */
630 #define F2FS_MAX_LOG_SECTOR_SIZE 12 /* 12 bits for 4096 bytes */
631 #define F2FS_BLKSIZE 4096 /* support only 4KB block */
632 #define F2FS_MAX_EXTENSION 64 /* # of extension entries */
633 #define F2FS_EXTENSION_LEN 8 /* max size of extension */
634 #define F2FS_BLK_ALIGN(x) (((x) + F2FS_BLKSIZE - 1) / F2FS_BLKSIZE)
636 #define NULL_ADDR 0x0U
638 #define COMPRESS_ADDR -2U
640 #define F2FS_ROOT_INO(sbi) (sbi->root_ino_num)
641 #define F2FS_NODE_INO(sbi) (sbi->node_ino_num)
642 #define F2FS_META_INO(sbi) (sbi->meta_ino_num)
644 #define F2FS_MAX_QUOTAS 3
646 #define QUOTA_INO(sb,t) (le32_to_cpu((sb)->qf_ino[t]))
649 * On-disk inode flags (f2fs_inode::i_flags)
651 #define F2FS_IMMUTABLE_FL 0x00000010 /* Immutable file */
652 #define F2FS_NOATIME_FL 0x00000080 /* do not update atime */
655 #define F2FS_ENC_UTF8_12_1 1
656 #define F2FS_ENC_STRICT_MODE_FL (1 << 0)
658 /* This flag is used by node and meta inodes, and by recovery */
659 #define GFP_F2FS_ZERO (GFP_NOFS | __GFP_ZERO)
662 * For further optimization on multi-head logs, on-disk layout supports maximum
663 * 16 logs by default. The number, 16, is expected to cover all the cases
664 * enoughly. The implementaion currently uses no more than 6 logs.
665 * Half the logs are used for nodes, and the other half are used for data.
667 #define MAX_ACTIVE_LOGS 16
668 #define MAX_ACTIVE_NODE_LOGS 8
669 #define MAX_ACTIVE_DATA_LOGS 8
671 #define F2FS_FEATURE_ENCRYPT 0x0001
672 #define F2FS_FEATURE_BLKZONED 0x0002
673 #define F2FS_FEATURE_ATOMIC_WRITE 0x0004
674 #define F2FS_FEATURE_EXTRA_ATTR 0x0008
675 #define F2FS_FEATURE_PRJQUOTA 0x0010
676 #define F2FS_FEATURE_INODE_CHKSUM 0x0020
677 #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR 0x0040
678 #define F2FS_FEATURE_QUOTA_INO 0x0080
679 #define F2FS_FEATURE_INODE_CRTIME 0x0100
680 #define F2FS_FEATURE_LOST_FOUND 0x0200
681 #define F2FS_FEATURE_VERITY 0x0400 /* reserved */
682 #define F2FS_FEATURE_SB_CHKSUM 0x0800
683 #define F2FS_FEATURE_CASEFOLD 0x1000
684 #define F2FS_FEATURE_COMPRESSION 0x2000
685 #define F2FS_FEATURE_RO 0x4000
687 #define MAX_VOLUME_NAME 512
693 __u8 path[MAX_PATH_LEN];
694 __le32 total_segments;
697 static_assert(sizeof(struct f2fs_device) == 68, "");
699 /* reason of stop_checkpoint */
700 enum stop_cp_reason {
701 STOP_CP_REASON_SHUTDOWN,
702 STOP_CP_REASON_FAULT_INJECT,
703 STOP_CP_REASON_META_PAGE,
704 STOP_CP_REASON_WRITE_FAIL,
705 STOP_CP_REASON_CORRUPTED_SUMMARY,
706 STOP_CP_REASON_UPDATE_INODE,
707 STOP_CP_REASON_FLUSH_FAIL,
711 #define MAX_STOP_REASON 32
713 /* detail reason for EFSCORRUPTED */
715 ERROR_CORRUPTED_CLUSTER,
716 ERROR_FAIL_DECOMPRESSION,
717 ERROR_INVALID_BLKADDR,
718 ERROR_CORRUPTED_DIRENT,
719 ERROR_CORRUPTED_INODE,
720 ERROR_INCONSISTENT_SUMMARY,
721 ERROR_INCONSISTENT_FOOTER,
722 ERROR_INCONSISTENT_SUM_TYPE,
723 ERROR_CORRUPTED_JOURNAL,
724 ERROR_INCONSISTENT_NODE_COUNT,
725 ERROR_INCONSISTENT_BLOCK_COUNT,
726 ERROR_INVALID_CURSEG,
727 ERROR_INCONSISTENT_SIT,
728 ERROR_CORRUPTED_VERITY_XATTR,
729 ERROR_CORRUPTED_XATTR,
733 #define MAX_F2FS_ERRORS 16
735 struct f2fs_super_block {
736 __le32 magic; /* Magic Number */
737 __le16 major_ver; /* Major Version */
738 __le16 minor_ver; /* Minor Version */
739 __le32 log_sectorsize; /* log2 sector size in bytes */
740 __le32 log_sectors_per_block; /* log2 # of sectors per block */
741 __le32 log_blocksize; /* log2 block size in bytes */
742 __le32 log_blocks_per_seg; /* log2 # of blocks per segment */
743 __le32 segs_per_sec; /* # of segments per section */
744 __le32 secs_per_zone; /* # of sections per zone */
745 __le32 checksum_offset; /* checksum offset inside super block */
746 __le64 block_count __attribute__((packed));
747 /* total # of user blocks */
748 __le32 section_count; /* total # of sections */
749 __le32 segment_count; /* total # of segments */
750 __le32 segment_count_ckpt; /* # of segments for checkpoint */
751 __le32 segment_count_sit; /* # of segments for SIT */
752 __le32 segment_count_nat; /* # of segments for NAT */
753 __le32 segment_count_ssa; /* # of segments for SSA */
754 __le32 segment_count_main; /* # of segments for main area */
755 __le32 segment0_blkaddr; /* start block address of segment 0 */
756 __le32 cp_blkaddr; /* start block address of checkpoint */
757 __le32 sit_blkaddr; /* start block address of SIT */
758 __le32 nat_blkaddr; /* start block address of NAT */
759 __le32 ssa_blkaddr; /* start block address of SSA */
760 __le32 main_blkaddr; /* start block address of main area */
761 __le32 root_ino; /* root inode number */
762 __le32 node_ino; /* node inode number */
763 __le32 meta_ino; /* meta inode number */
764 __u8 uuid[16]; /* 128-bit uuid for volume */
765 __le16 volume_name[MAX_VOLUME_NAME]; /* volume name */
766 __le32 extension_count; /* # of extensions below */
767 __u8 extension_list[F2FS_MAX_EXTENSION][8]; /* extension array */
769 __u8 version[VERSION_LEN]; /* the kernel version */
770 __u8 init_version[VERSION_LEN]; /* the initial kernel version */
771 __le32 feature; /* defined features */
772 __u8 encryption_level; /* versioning level for encryption */
773 __u8 encrypt_pw_salt[16]; /* Salt used for string2key algorithm */
774 struct f2fs_device devs[MAX_DEVICES] __attribute__((packed)); /* device list */
775 __le32 qf_ino[F2FS_MAX_QUOTAS] __attribute__((packed)); /* quota inode numbers */
776 __u8 hot_ext_count; /* # of hot file extension */
777 __le16 s_encoding; /* Filename charset encoding */
778 __le16 s_encoding_flags; /* Filename charset encoding flags */
779 __u8 s_stop_reason[MAX_STOP_REASON]; /* stop checkpoint reason */
780 __u8 s_errors[MAX_F2FS_ERRORS]; /* reason of image corrupts */
781 __u8 reserved[258]; /* valid reserved region */
782 __le32 crc; /* checksum of superblock */
783 } __attribute__((packed));
785 static_assert(sizeof(struct f2fs_super_block) == 3072, "");
790 #define CP_RESIZEFS_FLAG 0x00004000
791 #define CP_DISABLED_FLAG 0x00001000
792 #define CP_QUOTA_NEED_FSCK_FLAG 0x00000800
793 #define CP_LARGE_NAT_BITMAP_FLAG 0x00000400
794 #define CP_NOCRC_RECOVERY_FLAG 0x00000200
795 #define CP_TRIMMED_FLAG 0x00000100
796 #define CP_NAT_BITS_FLAG 0x00000080
797 #define CP_CRC_RECOVERY_FLAG 0x00000040
798 #define CP_FASTBOOT_FLAG 0x00000020
799 #define CP_FSCK_FLAG 0x00000010
800 #define CP_ERROR_FLAG 0x00000008
801 #define CP_COMPACT_SUM_FLAG 0x00000004
802 #define CP_ORPHAN_PRESENT_FLAG 0x00000002
803 #define CP_UMOUNT_FLAG 0x00000001
805 #define F2FS_CP_PACKS 2 /* # of checkpoint packs */
807 struct f2fs_checkpoint {
808 __le64 checkpoint_ver; /* checkpoint block version number */
809 __le64 user_block_count; /* # of user blocks */
810 __le64 valid_block_count; /* # of valid blocks in main area */
811 __le32 rsvd_segment_count; /* # of reserved segments for gc */
812 __le32 overprov_segment_count; /* # of overprovision segments */
813 __le32 free_segment_count; /* # of free segments in main area */
815 /* information of current node segments */
816 __le32 cur_node_segno[MAX_ACTIVE_NODE_LOGS];
817 __le16 cur_node_blkoff[MAX_ACTIVE_NODE_LOGS];
818 /* information of current data segments */
819 __le32 cur_data_segno[MAX_ACTIVE_DATA_LOGS];
820 __le16 cur_data_blkoff[MAX_ACTIVE_DATA_LOGS];
821 __le32 ckpt_flags; /* Flags : umount and journal_present */
822 __le32 cp_pack_total_block_count; /* total # of one cp pack */
823 __le32 cp_pack_start_sum; /* start block number of data summary */
824 __le32 valid_node_count; /* Total number of valid nodes */
825 __le32 valid_inode_count; /* Total number of valid inodes */
826 __le32 next_free_nid; /* Next free node number */
827 __le32 sit_ver_bitmap_bytesize; /* Default value 64 */
828 __le32 nat_ver_bitmap_bytesize; /* Default value 256 */
829 __le32 checksum_offset; /* checksum offset inside cp block */
830 __le64 elapsed_time; /* mounted time */
831 /* allocation type of current segment */
832 unsigned char alloc_type[MAX_ACTIVE_LOGS];
834 /* SIT and NAT version bitmap */
835 unsigned char sit_nat_version_bitmap[];
838 static_assert(sizeof(struct f2fs_checkpoint) == 192, "");
840 #define CP_BITMAP_OFFSET \
841 (offsetof(struct f2fs_checkpoint, sit_nat_version_bitmap))
842 #define CP_MIN_CHKSUM_OFFSET CP_BITMAP_OFFSET
844 #define MIN_NAT_BITMAP_SIZE 64
845 #define MAX_SIT_BITMAP_SIZE_IN_CKPT \
846 (CP_CHKSUM_OFFSET - CP_BITMAP_OFFSET - MIN_NAT_BITMAP_SIZE)
847 #define MAX_BITMAP_SIZE_IN_CKPT \
848 (CP_CHKSUM_OFFSET - CP_BITMAP_OFFSET)
851 * For orphan inode management
853 #define F2FS_ORPHANS_PER_BLOCK 1020
855 struct f2fs_orphan_block {
856 __le32 ino[F2FS_ORPHANS_PER_BLOCK]; /* inode numbers */
857 __le32 reserved; /* reserved */
858 __le16 blk_addr; /* block index in current CP */
859 __le16 blk_count; /* Number of orphan inode blocks in CP */
860 __le32 entry_count; /* Total number of orphan nodes in current CP */
861 __le32 check_sum; /* CRC32 for orphan inode block */
864 static_assert(sizeof(struct f2fs_orphan_block) == 4096, "");
870 __le32 fofs; /* start file offset of the extent */
871 __le32 blk_addr; /* start block address of the extent */
872 __le32 len; /* lengh of the extent */
875 static_assert(sizeof(struct f2fs_extent) == 12, "");
877 #define F2FS_NAME_LEN 255
879 /* max output length of pretty_print_filename() including null terminator */
880 #define F2FS_PRINT_NAMELEN (4 * ((F2FS_NAME_LEN + 2) / 3) + 1)
882 /* 200 bytes for inline xattrs by default */
883 #define DEFAULT_INLINE_XATTR_ADDRS 50
884 #define DEF_ADDRS_PER_INODE 923 /* Address Pointers in an Inode */
885 #define CUR_ADDRS_PER_INODE(inode) (DEF_ADDRS_PER_INODE - \
886 __get_extra_isize(inode))
887 #define ADDRS_PER_INODE(i) addrs_per_inode(i)
888 #define DEF_ADDRS_PER_BLOCK 1018 /* Address Pointers in a Direct Block */
889 #define ADDRS_PER_BLOCK(i) addrs_per_block(i)
890 #define NIDS_PER_BLOCK 1018 /* Node IDs in an Indirect Block */
892 #define NODE_DIR1_BLOCK (DEF_ADDRS_PER_INODE + 1)
893 #define NODE_DIR2_BLOCK (DEF_ADDRS_PER_INODE + 2)
894 #define NODE_IND1_BLOCK (DEF_ADDRS_PER_INODE + 3)
895 #define NODE_IND2_BLOCK (DEF_ADDRS_PER_INODE + 4)
896 #define NODE_DIND_BLOCK (DEF_ADDRS_PER_INODE + 5)
898 #define F2FS_INLINE_XATTR 0x01 /* file inline xattr flag */
899 #define F2FS_INLINE_DATA 0x02 /* file inline data flag */
900 #define F2FS_INLINE_DENTRY 0x04 /* file inline dentry flag */
901 #define F2FS_DATA_EXIST 0x08 /* file inline data exist flag */
902 #define F2FS_INLINE_DOTS 0x10 /* file having implicit dot dentries */
903 #define F2FS_EXTRA_ATTR 0x20 /* file having extra attribute */
904 #define F2FS_PIN_FILE 0x40 /* file should not be gced */
905 #define F2FS_COMPRESS_RELEASED 0x80 /* file released compressed blocks */
907 #define F2FS_EXTRA_ISIZE_OFFSET \
908 offsetof(struct f2fs_inode, i_extra_isize)
909 #define F2FS_TOTAL_EXTRA_ATTR_SIZE \
910 (offsetof(struct f2fs_inode, i_extra_end) - F2FS_EXTRA_ISIZE_OFFSET)
912 #define F2FS_DEF_PROJID 0 /* default project ID */
914 #define MAX_INLINE_DATA(node) (sizeof(__le32) * \
915 (DEF_ADDRS_PER_INODE - \
916 get_inline_xattr_addrs(&node->i) - \
917 get_extra_isize(node) - \
918 DEF_INLINE_RESERVED_SIZE))
919 #define DEF_MAX_INLINE_DATA (sizeof(__le32) * \
920 (DEF_ADDRS_PER_INODE - \
921 DEFAULT_INLINE_XATTR_ADDRS - \
922 F2FS_TOTAL_EXTRA_ATTR_SIZE - \
923 DEF_INLINE_RESERVED_SIZE))
924 #define INLINE_DATA_OFFSET (F2FS_BLKSIZE - \
925 sizeof(struct node_footer) - \
926 sizeof(__le32) * (DEF_ADDRS_PER_INODE + \
927 5 - DEF_INLINE_RESERVED_SIZE))
929 #define DEF_DIR_LEVEL 0
932 * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
934 #define FADVISE_COLD_BIT 0x01
935 #define FADVISE_LOST_PINO_BIT 0x02
936 #define FADVISE_ENCRYPT_BIT 0x04
937 #define FADVISE_ENC_NAME_BIT 0x08
938 #define FADVISE_KEEP_SIZE_BIT 0x10
939 #define FADVISE_HOT_BIT 0x20
940 #define FADVISE_VERITY_BIT 0x40 /* reserved */
942 #define file_is_encrypt(fi) ((fi)->i_advise & FADVISE_ENCRYPT_BIT)
943 #define file_enc_name(fi) ((fi)->i_advise & FADVISE_ENC_NAME_BIT)
945 #define F2FS_CASEFOLD_FL 0x40000000 /* Casefolded file */
946 #define IS_CASEFOLDED(dir) ((dir)->i_flags & F2FS_CASEFOLD_FL)
949 * fsck i_compr_blocks counting helper
951 struct f2fs_compr_blk_cnt {
952 /* counting i_compr_blocks, init 0 */
956 * previous seen compression header (COMPR_ADDR) page offsets,
957 * use CHEADER_PGOFS_NONE for none
961 #define CHEADER_PGOFS_NONE ((u32)-(1 << MAX_COMPRESS_LOG_SIZE))
966 #define F2FS_COMPR_FL 0x00000004 /* Compress file */
968 __le16 i_mode; /* file mode */
969 __u8 i_advise; /* file hints */
970 __u8 i_inline; /* file inline flags */
971 __le32 i_uid; /* user ID */
972 __le32 i_gid; /* group ID */
973 __le32 i_links; /* links count */
974 __le64 i_size; /* file size in bytes */
975 __le64 i_blocks; /* file size in blocks */
976 __le64 i_atime; /* access time */
977 __le64 i_ctime; /* change time */
978 __le64 i_mtime; /* modification time */
979 __le32 i_atime_nsec; /* access time in nano scale */
980 __le32 i_ctime_nsec; /* change time in nano scale */
981 __le32 i_mtime_nsec; /* modification time in nano scale */
982 __le32 i_generation; /* file version (for NFS) */
984 __le32 i_current_depth; /* only for directory depth */
985 __le16 i_gc_failures; /*
986 * # of gc failures on pinned file.
987 * only for regular files.
990 __le32 i_xattr_nid; /* nid to save xattr */
991 __le32 i_flags; /* file attributes */
992 __le32 i_pino; /* parent inode number */
993 __le32 i_namelen; /* file name length */
994 __u8 i_name[F2FS_NAME_LEN]; /* file name for SPOR */
995 __u8 i_dir_level; /* dentry_level for large dir */
997 struct f2fs_extent i_ext __attribute__((packed)); /* caching a largest extent */
1001 __le16 i_extra_isize; /* extra inode attribute size */
1002 __le16 i_inline_xattr_size; /* inline xattr size, unit: 4 bytes */
1003 __le32 i_projid; /* project id */
1004 __le32 i_inode_checksum;/* inode meta checksum */
1005 __le64 i_crtime; /* creation time */
1006 __le32 i_crtime_nsec; /* creation time in nano scale */
1007 __le64 i_compr_blocks; /* # of compressed blocks */
1008 __u8 i_compress_algorithm; /* compress algorithm */
1009 __u8 i_log_cluster_size; /* log of cluster size */
1010 __le16 i_compress_flag; /* compress flag */
1011 /* 0 bit: chksum flag
1012 * [8,15] bits: compress level
1014 __le32 i_extra_end[0]; /* for attribute size calculation */
1015 } __attribute__((packed));
1016 __le32 i_addr[DEF_ADDRS_PER_INODE]; /* Pointers to data blocks */
1018 __le32 i_nid[5]; /* direct(2), indirect(2),
1019 double_indirect(1) node id */
1022 static_assert(offsetof(struct f2fs_inode, i_extra_end) -
1023 offsetof(struct f2fs_inode, i_extra_isize) == 36, "");
1024 static_assert(sizeof(struct f2fs_inode) == 4072, "");
1026 struct direct_node {
1027 __le32 addr[DEF_ADDRS_PER_BLOCK]; /* array of data block address */
1030 static_assert(sizeof(struct direct_node) == 4072, "");
1032 struct indirect_node {
1033 __le32 nid[NIDS_PER_BLOCK]; /* array of data block address */
1036 static_assert(sizeof(struct indirect_node) == 4072, "");
1045 #define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
1046 >> OFFSET_BIT_SHIFT)
1047 struct node_footer {
1048 __le32 nid; /* node id */
1049 __le32 ino; /* inode nunmber */
1050 __le32 flag; /* include cold/fsync/dentry marks and offset */
1051 __le64 cp_ver __attribute__((packed)); /* checkpoint version */
1052 __le32 next_blkaddr; /* next node page block address */
1055 static_assert(sizeof(struct node_footer) == 24, "");
1058 /* can be one of three types: inode, direct, and indirect types */
1060 struct f2fs_inode i;
1061 struct direct_node dn;
1062 struct indirect_node in;
1064 struct node_footer footer;
1067 static_assert(sizeof(struct f2fs_node) == 4096, "");
1072 #define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / sizeof(struct f2fs_nat_entry))
1073 #define NAT_BLOCK_OFFSET(start_nid) (start_nid / NAT_ENTRY_PER_BLOCK)
1075 #define DEFAULT_NAT_ENTRY_RATIO 20
1077 struct f2fs_nat_entry {
1078 __u8 version; /* latest version of cached nat entry */
1079 __le32 ino; /* inode number */
1080 __le32 block_addr; /* block address */
1081 } __attribute__((packed));
1083 static_assert(sizeof(struct f2fs_nat_entry) == 9, "");
1085 struct f2fs_nat_block {
1086 struct f2fs_nat_entry entries[NAT_ENTRY_PER_BLOCK];
1089 static_assert(sizeof(struct f2fs_nat_block) == 4095, "");
1094 * Each segment is 2MB in size by default so that a bitmap for validity of
1095 * there-in blocks should occupy 64 bytes, 512 bits.
1096 * Not allow to change this.
1098 #define SIT_VBLOCK_MAP_SIZE 64
1099 #define SIT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / sizeof(struct f2fs_sit_entry))
1102 * F2FS uses 4 bytes to represent block address. As a result, supported size of
1103 * disk is 16 TB and it equals to 16 * 1024 * 1024 / 2 segments.
1105 #define F2FS_MIN_SEGMENT 9 /* SB + 2 (CP + SIT + NAT) + SSA + MAIN */
1106 #define F2FS_MAX_SEGMENT ((16 * 1024 * 1024) / 2)
1107 #define MAX_SIT_BITMAP_SIZE (SEG_ALIGN(SIZE_ALIGN(F2FS_MAX_SEGMENT, \
1108 SIT_ENTRY_PER_BLOCK)) * \
1110 #define MAX_CP_PAYLOAD (SEG_ALIGN(SIZE_ALIGN(UINT32_MAX, NAT_ENTRY_PER_BLOCK)) * \
1111 DEFAULT_NAT_ENTRY_RATIO / 100 * \
1112 c.blks_per_seg / 8 + \
1113 MAX_SIT_BITMAP_SIZE - MAX_BITMAP_SIZE_IN_CKPT)
1116 * Note that f2fs_sit_entry->vblocks has the following bit-field information.
1117 * [15:10] : allocation type such as CURSEG_XXXX_TYPE
1118 * [9:0] : valid block count
1120 #define SIT_VBLOCKS_SHIFT 10
1121 #define SIT_VBLOCKS_MASK ((1 << SIT_VBLOCKS_SHIFT) - 1)
1122 #define GET_SIT_VBLOCKS(raw_sit) \
1123 (le16_to_cpu((raw_sit)->vblocks) & SIT_VBLOCKS_MASK)
1124 #define GET_SIT_TYPE(raw_sit) \
1125 ((le16_to_cpu((raw_sit)->vblocks) & ~SIT_VBLOCKS_MASK) \
1126 >> SIT_VBLOCKS_SHIFT)
1128 struct f2fs_sit_entry {
1129 __le16 vblocks; /* reference above */
1130 __u8 valid_map[SIT_VBLOCK_MAP_SIZE]; /* bitmap for valid blocks */
1131 __le64 mtime; /* segment age for cleaning */
1132 } __attribute__((packed));
1134 static_assert(sizeof(struct f2fs_sit_entry) == 74, "");
1136 struct f2fs_sit_block {
1137 struct f2fs_sit_entry entries[SIT_ENTRY_PER_BLOCK];
1140 static_assert(sizeof(struct f2fs_sit_block) == 4070, "");
1143 * For segment summary
1145 * One summary block contains exactly 512 summary entries, which represents
1146 * exactly 2MB segment by default. Not allow to change the basic units.
1148 * NOTE: For initializing fields, you must use set_summary
1150 * - If data page, nid represents dnode's nid
1151 * - If node page, nid represents the node page's nid.
1153 * The ofs_in_node is used by only data page. It represents offset
1154 * from node's page's beginning to get a data block address.
1155 * ex) data_blkaddr = (block_t)(nodepage_start_address + ofs_in_node)
1157 #define ENTRIES_IN_SUM 512
1158 #define SUMMARY_SIZE (7) /* sizeof(struct summary) */
1159 #define SUM_FOOTER_SIZE (5) /* sizeof(struct summary_footer) */
1160 #define SUM_ENTRIES_SIZE (SUMMARY_SIZE * ENTRIES_IN_SUM)
1162 /* a summary entry for a 4KB-sized block in a segment */
1163 struct f2fs_summary {
1164 __le32 nid; /* parent node id */
1168 __u8 version; /* node version number */
1169 __le16 ofs_in_node; /* block index in parent node */
1170 } __attribute__((packed));
1172 } __attribute__((packed));
1174 static_assert(sizeof(struct f2fs_summary) == 7, "");
1176 /* summary block type, node or data, is stored to the summary_footer */
1177 #define SUM_TYPE_NODE (1)
1178 #define SUM_TYPE_DATA (0)
1180 struct summary_footer {
1181 unsigned char entry_type; /* SUM_TYPE_XXX */
1182 __le32 check_sum __attribute__((packed)); /* summary checksum */
1185 static_assert(sizeof(struct summary_footer) == 5, "");
1187 #define SUM_JOURNAL_SIZE (F2FS_BLKSIZE - SUM_FOOTER_SIZE -\
1189 #define NAT_JOURNAL_ENTRIES ((SUM_JOURNAL_SIZE - 2) /\
1190 sizeof(struct nat_journal_entry))
1191 #define NAT_JOURNAL_RESERVED ((SUM_JOURNAL_SIZE - 2) %\
1192 sizeof(struct nat_journal_entry))
1193 #define SIT_JOURNAL_ENTRIES ((SUM_JOURNAL_SIZE - 2) /\
1194 sizeof(struct sit_journal_entry))
1195 #define SIT_JOURNAL_RESERVED ((SUM_JOURNAL_SIZE - 2) %\
1196 sizeof(struct sit_journal_entry))
1199 * Reserved area should make size of f2fs_extra_info equals to
1200 * that of nat_journal and sit_journal.
1202 #define EXTRA_INFO_RESERVED (SUM_JOURNAL_SIZE - 2 - 8)
1205 * frequently updated NAT/SIT entries can be stored in the spare area in
1213 struct nat_journal_entry {
1215 struct f2fs_nat_entry ne;
1216 } __attribute__((packed));
1218 static_assert(sizeof(struct nat_journal_entry) == 13, "");
1220 struct nat_journal {
1221 struct nat_journal_entry entries[NAT_JOURNAL_ENTRIES];
1222 __u8 reserved[NAT_JOURNAL_RESERVED];
1225 static_assert(sizeof(struct nat_journal) == 505, "");
1227 struct sit_journal_entry {
1229 struct f2fs_sit_entry se;
1230 } __attribute__((packed));
1232 static_assert(sizeof(struct sit_journal_entry) == 78, "");
1234 struct sit_journal {
1235 struct sit_journal_entry entries[SIT_JOURNAL_ENTRIES];
1236 __u8 reserved[SIT_JOURNAL_RESERVED];
1239 static_assert(sizeof(struct sit_journal) == 505, "");
1241 struct f2fs_extra_info {
1242 __le64 kbytes_written;
1243 __u8 reserved[EXTRA_INFO_RESERVED];
1244 } __attribute__((packed));
1246 static_assert(sizeof(struct f2fs_extra_info) == 505, "");
1248 struct f2fs_journal {
1253 /* spare area is used by NAT or SIT journals or extra info */
1255 struct nat_journal nat_j;
1256 struct sit_journal sit_j;
1257 struct f2fs_extra_info info;
1259 } __attribute__((packed));
1261 static_assert(sizeof(struct f2fs_journal) == 507, "");
1263 /* 4KB-sized summary block structure */
1264 struct f2fs_summary_block {
1265 struct f2fs_summary entries[ENTRIES_IN_SUM];
1266 struct f2fs_journal journal;
1267 struct summary_footer footer;
1270 static_assert(sizeof(struct f2fs_summary_block) == 4096, "");
1273 * For directory operations
1275 #define F2FS_DOT_HASH 0
1276 #define F2FS_DDOT_HASH F2FS_DOT_HASH
1277 #define F2FS_MAX_HASH (~((0x3ULL) << 62))
1278 #define F2FS_HASH_COL_BIT ((0x1ULL) << 63)
1280 typedef __le32 f2fs_hash_t;
1282 /* One directory entry slot covers 8bytes-long file name */
1283 #define F2FS_SLOT_LEN 8
1284 #define F2FS_SLOT_LEN_BITS 3
1286 #define GET_DENTRY_SLOTS(x) ((x + F2FS_SLOT_LEN - 1) >> F2FS_SLOT_LEN_BITS)
1288 /* the number of dentry in a block */
1289 #define NR_DENTRY_IN_BLOCK 214
1291 /* MAX level for dir lookup */
1292 #define MAX_DIR_HASH_DEPTH 63
1294 /* MAX buckets in one level of dir */
1295 #define MAX_DIR_BUCKETS (1 << ((MAX_DIR_HASH_DEPTH / 2) - 1))
1297 #define SIZE_OF_DIR_ENTRY 11 /* by byte */
1298 #define SIZE_OF_DENTRY_BITMAP ((NR_DENTRY_IN_BLOCK + BITS_PER_BYTE - 1) / \
1300 #define SIZE_OF_RESERVED (F2FS_BLKSIZE - ((SIZE_OF_DIR_ENTRY + \
1302 NR_DENTRY_IN_BLOCK + SIZE_OF_DENTRY_BITMAP))
1303 #define MIN_INLINE_DENTRY_SIZE 40 /* just include '.' and '..' entries */
1305 /* One directory entry slot representing F2FS_SLOT_LEN-sized file name */
1306 struct f2fs_dir_entry {
1307 __le32 hash_code; /* hash code of file name */
1308 __le32 ino; /* inode number */
1309 __le16 name_len; /* lengh of file name */
1310 __u8 file_type; /* file type */
1311 } __attribute__((packed));
1313 static_assert(sizeof(struct f2fs_dir_entry) == 11, "");
1315 /* 4KB-sized directory entry block */
1316 struct f2fs_dentry_block {
1317 /* validity bitmap for directory entries in each block */
1318 __u8 dentry_bitmap[SIZE_OF_DENTRY_BITMAP];
1319 __u8 reserved[SIZE_OF_RESERVED];
1320 struct f2fs_dir_entry dentry[NR_DENTRY_IN_BLOCK];
1321 __u8 filename[NR_DENTRY_IN_BLOCK][F2FS_SLOT_LEN];
1324 static_assert(sizeof(struct f2fs_dentry_block) == F2FS_BLKSIZE, "");
1326 /* for inline stuff */
1327 #define DEF_INLINE_RESERVED_SIZE 1
1329 /* for inline dir */
1330 #define NR_INLINE_DENTRY(node) (MAX_INLINE_DATA(node) * BITS_PER_BYTE / \
1331 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
1333 #define INLINE_DENTRY_BITMAP_SIZE(node) ((NR_INLINE_DENTRY(node) + \
1334 BITS_PER_BYTE - 1) / BITS_PER_BYTE)
1335 #define INLINE_RESERVED_SIZE(node) (MAX_INLINE_DATA(node) - \
1336 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
1337 NR_INLINE_DENTRY(node) + \
1338 INLINE_DENTRY_BITMAP_SIZE(node)))
1340 /* file types used in inode_info->flags */
1351 /* added for fsck */
1354 F2FS_FT_LAST_FILE_TYPE = F2FS_FT_XATTR,
1357 #define LINUX_S_IFMT 00170000
1358 #define LINUX_S_IFREG 0100000
1359 #define LINUX_S_ISREG(m) (((m) & LINUX_S_IFMT) == LINUX_S_IFREG)
1361 /* from f2fs/segment.h */
1367 #define MAX_CACHE_SUMS 8
1369 struct f2fs_configuration {
1370 uint32_t conf_reserved_sections;
1371 uint32_t reserved_segments;
1372 uint32_t new_reserved_segments;
1377 double overprovision;
1378 double new_overprovision;
1379 uint32_t cur_seg[NR_CURSEG_TYPE];
1380 uint32_t segs_per_sec;
1381 uint32_t secs_per_zone;
1382 uint32_t segs_per_zone;
1383 uint32_t start_sector;
1384 uint32_t total_segments;
1385 uint32_t sector_size;
1386 uint64_t device_size;
1387 uint64_t total_sectors;
1388 uint64_t wanted_total_sectors;
1389 uint64_t wanted_sector_size;
1390 uint64_t target_sectors;
1391 uint32_t sectors_per_blk;
1392 uint32_t blks_per_seg;
1393 __u8 init_version[VERSION_LEN + 1];
1394 __u8 sb_version[VERSION_LEN + 1];
1395 __u8 version[VERSION_LEN + 1];
1398 uint16_t s_encoding;
1399 uint16_t s_encoding_flags;
1403 struct device_info devices[MAX_DEVICES];
1405 char *extension_list[2];
1406 const char *rootdev_name;
1414 int no_kernel_check;
1428 u64 show_file_map_max_offset;
1432 int preserve_limits; /* preserve quota limits */
1433 int large_nat_bitmap;
1434 int fix_chksum; /* fix old cp.chksum position */
1435 __le32 feature; /* defined features */
1436 unsigned int quota_bits; /* quota bits */
1439 /* mkfs parameters */
1441 uint32_t next_free_nid;
1446 /* defragmentation parameters */
1448 uint64_t defrag_start;
1449 uint64_t defrag_len;
1450 uint64_t defrag_target;
1452 /* sload parameters */
1455 char *target_out_dir;
1456 char *fs_config_file;
1457 #ifdef HAVE_LIBSELINUX
1458 struct selinux_opt seopt_file[8];
1463 /* resize parameters */
1466 /* precomputed fs UUID checksum for seeding other checksums */
1467 uint32_t chksum_seed;
1469 /* cache parameters */
1470 dev_cache_config_t cache_config;
1472 /* compression support for sload.f2fs */
1473 compress_config_t compress;
1475 block_t curseg_offset[NR_CURSEG_TYPE];
1476 struct f2fs_journal sit_jnl;
1477 struct f2fs_journal nat_jnl;
1478 struct f2fs_summary sum[NR_CURSEG_TYPE][MAX_CACHE_SUMS];
1481 extern int utf8_to_utf16(char *, const char *, size_t, size_t);
1482 extern int utf16_to_utf8(char *, const char *, size_t, size_t);
1483 extern int log_base_2(uint32_t);
1484 extern unsigned int addrs_per_inode(struct f2fs_inode *);
1485 extern unsigned int addrs_per_block(struct f2fs_inode *);
1486 extern unsigned int f2fs_max_file_offset(struct f2fs_inode *);
1487 extern __u32 f2fs_inode_chksum(struct f2fs_node *);
1488 extern __u32 f2fs_checkpoint_chksum(struct f2fs_checkpoint *);
1489 extern int write_inode(struct f2fs_node *, u64);
1491 extern int get_bits_in_byte(unsigned char n);
1492 extern int test_and_set_bit_le(u32, u8 *);
1493 extern int test_and_clear_bit_le(u32, u8 *);
1494 extern int test_bit_le(u32, const u8 *);
1495 extern int f2fs_test_bit(unsigned int, const char *);
1496 extern int f2fs_set_bit(unsigned int, char *);
1497 extern int f2fs_clear_bit(unsigned int, char *);
1498 extern u64 find_next_bit_le(const u8 *, u64, u64);
1499 extern u64 find_next_zero_bit_le(const u8 *, u64, u64);
1501 extern uint32_t f2fs_cal_crc32(uint32_t, void *, int);
1502 extern int f2fs_crc_valid(uint32_t blk_crc, void *buf, int len);
1504 extern void f2fs_init_configuration(void);
1505 extern int f2fs_devs_are_umounted(void);
1506 extern int f2fs_dev_is_writable(void);
1507 extern int f2fs_dev_is_umounted(char *);
1508 extern int f2fs_get_device_info(void);
1509 extern int f2fs_get_f2fs_info(void);
1510 extern unsigned int calc_extra_isize(void);
1511 extern int get_device_info(int);
1512 extern int f2fs_init_sparse_file(void);
1513 extern void f2fs_release_sparse_resource(void);
1514 extern int f2fs_finalize_device(void);
1515 extern int f2fs_fsync_device(void);
1517 extern void dcache_init(void);
1518 extern void dcache_release(void);
1520 extern int dev_read(void *, __u64, size_t);
1521 #ifdef POSIX_FADV_WILLNEED
1522 extern int dev_readahead(__u64, size_t);
1524 extern int dev_readahead(__u64, size_t UNUSED(len));
1526 extern int dev_write(void *, __u64, size_t);
1527 extern int dev_write_block(void *, __u64);
1528 extern int dev_write_dump(void *, __u64, size_t);
1529 /* All bytes in the buffer must be 0 use dev_fill(). */
1530 extern int dev_fill(void *, __u64, size_t);
1531 extern int dev_fill_block(void *, __u64);
1533 extern int dev_read_block(void *, __u64);
1534 extern int dev_reada_block(__u64);
1536 extern int dev_read_version(void *, __u64, size_t);
1537 extern void get_kernel_version(__u8 *);
1538 extern void get_kernel_uname_version(__u8 *);
1539 f2fs_hash_t f2fs_dentry_hash(int, int, const unsigned char *, int);
1541 static inline bool f2fs_has_extra_isize(struct f2fs_inode *inode)
1543 return (inode->i_inline & F2FS_EXTRA_ATTR);
1546 static inline int __get_extra_isize(struct f2fs_inode *inode)
1548 if (f2fs_has_extra_isize(inode))
1549 return le16_to_cpu(inode->i_extra_isize) / sizeof(__le32);
1553 extern struct f2fs_configuration c;
1554 static inline int get_inline_xattr_addrs(struct f2fs_inode *inode)
1556 if (c.feature & cpu_to_le32(F2FS_FEATURE_FLEXIBLE_INLINE_XATTR))
1557 return le16_to_cpu(inode->i_inline_xattr_size);
1558 else if (inode->i_inline & F2FS_INLINE_XATTR ||
1559 inode->i_inline & F2FS_INLINE_DENTRY)
1560 return DEFAULT_INLINE_XATTR_ADDRS;
1565 #define get_extra_isize(node) __get_extra_isize(&node->i)
1567 #define F2FS_ZONED_NONE 0
1568 #define F2FS_ZONED_HA 1
1569 #define F2FS_ZONED_HM 2
1571 #ifdef HAVE_LINUX_BLKZONED_H
1573 /* Let's just use v2, since v1 should be compatible with v2 */
1574 #define BLK_ZONE_REP_CAPACITY (1 << 0)
1575 struct blk_zone_v2 {
1576 __u64 start; /* Zone start sector */
1577 __u64 len; /* Zone length in number of sectors */
1578 __u64 wp; /* Zone write pointer position */
1579 __u8 type; /* Zone type */
1580 __u8 cond; /* Zone condition */
1581 __u8 non_seq; /* Non-sequential write resources active */
1582 __u8 reset; /* Reset write pointer recommended */
1584 __u64 capacity; /* Zone capacity in number of sectors */
1587 #define blk_zone blk_zone_v2
1589 struct blk_zone_report_v2 {
1593 struct blk_zone zones[0];
1595 #define blk_zone_report blk_zone_report_v2
1597 #define blk_zone_type(z) (z)->type
1598 #define blk_zone_conv(z) ((z)->type == BLK_ZONE_TYPE_CONVENTIONAL)
1599 #define blk_zone_seq_req(z) ((z)->type == BLK_ZONE_TYPE_SEQWRITE_REQ)
1600 #define blk_zone_seq_pref(z) ((z)->type == BLK_ZONE_TYPE_SEQWRITE_PREF)
1601 #define blk_zone_seq(z) (blk_zone_seq_req(z) || blk_zone_seq_pref(z))
1603 static inline const char *
1604 blk_zone_type_str(struct blk_zone *blkz)
1606 switch (blk_zone_type(blkz)) {
1607 case BLK_ZONE_TYPE_CONVENTIONAL:
1608 return( "Conventional" );
1609 case BLK_ZONE_TYPE_SEQWRITE_REQ:
1610 return( "Sequential-write-required" );
1611 case BLK_ZONE_TYPE_SEQWRITE_PREF:
1612 return( "Sequential-write-preferred" );
1614 return( "Unknown-type" );
1617 #define blk_zone_cond(z) (z)->cond
1619 static inline const char *
1620 blk_zone_cond_str(struct blk_zone *blkz)
1622 switch (blk_zone_cond(blkz)) {
1623 case BLK_ZONE_COND_NOT_WP:
1624 return "Not-write-pointer";
1625 case BLK_ZONE_COND_EMPTY:
1627 case BLK_ZONE_COND_IMP_OPEN:
1628 return "Implicit-open";
1629 case BLK_ZONE_COND_EXP_OPEN:
1630 return "Explicit-open";
1631 case BLK_ZONE_COND_CLOSED:
1633 case BLK_ZONE_COND_READONLY:
1635 case BLK_ZONE_COND_FULL:
1637 case BLK_ZONE_COND_OFFLINE:
1640 return "Unknown-cond";
1644 * Handle kernel zone capacity support
1646 #define blk_zone_empty(z) (blk_zone_cond(z) == BLK_ZONE_COND_EMPTY)
1647 #define blk_zone_sector(z) (z)->start
1648 #define blk_zone_length(z) (z)->len
1649 #define blk_zone_wp_sector(z) (z)->wp
1650 #define blk_zone_need_reset(z) (int)(z)->reset
1651 #define blk_zone_non_seq(z) (int)(z)->non_seq
1652 #define blk_zone_capacity(z, f) ((f & BLK_ZONE_REP_CAPACITY) ? \
1653 (z)->capacity : (z)->len)
1659 extern int f2fs_get_zoned_model(int);
1660 extern int f2fs_get_zone_blocks(int);
1661 extern int f2fs_report_zone(int, uint64_t, struct blk_zone *);
1662 typedef int (report_zones_cb_t)(int i, void *, void *);
1663 extern int f2fs_report_zones(int, report_zones_cb_t *, void *);
1664 extern int f2fs_check_zones(int);
1665 int f2fs_reset_zone(int, void *);
1666 extern int f2fs_reset_zones(int);
1667 extern uint32_t f2fs_get_usable_segments(struct f2fs_super_block *sb);
1669 #define SIZE_ALIGN(val, size) (((val) + (size) - 1) / (size))
1670 #define SEG_ALIGN(blks) SIZE_ALIGN(blks, c.blks_per_seg)
1671 #define ZONE_ALIGN(blks) SIZE_ALIGN(blks, c.blks_per_seg * \
1674 static inline double get_reserved(struct f2fs_super_block *sb, double ovp)
1677 uint32_t usable_main_segs = f2fs_get_usable_segments(sb);
1678 uint32_t segs_per_sec = round_up(usable_main_segs, get_sb(section_count));
1680 if (c.conf_reserved_sections)
1681 reserved = c.conf_reserved_sections * segs_per_sec;
1683 reserved = (100 / ovp + 1 + NR_CURSEG_TYPE) * segs_per_sec;
1688 static inline double get_best_overprovision(struct f2fs_super_block *sb)
1690 double reserved, ovp, candidate, end, diff, space;
1691 double max_ovp = 0, max_space = 0;
1692 uint32_t usable_main_segs = f2fs_get_usable_segments(sb);
1694 if (get_sb(segment_count_main) < 256) {
1704 for (; candidate <= end; candidate += diff) {
1705 reserved = get_reserved(sb, candidate);
1706 ovp = (usable_main_segs - reserved) * candidate / 100;
1709 space = usable_main_segs - max(reserved, ovp) -
1710 2 * get_sb(segs_per_sec);
1711 if (max_space < space) {
1713 max_ovp = candidate;
1719 static inline __le64 get_cp_crc(struct f2fs_checkpoint *cp)
1721 uint64_t cp_ver = get_cp(checkpoint_ver);
1722 size_t crc_offset = get_cp(checksum_offset);
1723 uint32_t crc = le32_to_cpu(*(__le32 *)((unsigned char *)cp +
1726 cp_ver |= ((uint64_t)crc << 32);
1727 return cpu_to_le64(cp_ver);
1730 static inline int exist_qf_ino(struct f2fs_super_block *sb)
1734 for (i = 0; i < F2FS_MAX_QUOTAS; i++)
1740 static inline int is_qf_ino(struct f2fs_super_block *sb, nid_t ino)
1744 for (i = 0; i < F2FS_MAX_QUOTAS; i++)
1745 if (sb->qf_ino[i] == ino)
1750 static inline void show_version(const char *prog)
1752 #if defined(F2FS_TOOLS_VERSION) && defined(F2FS_TOOLS_DATE)
1753 MSG(0, "%s %s (%s)\n", prog, F2FS_TOOLS_VERSION, F2FS_TOOLS_DATE);
1755 MSG(0, "%s -- version not supported\n", prog);
1759 static inline void f2fs_init_inode(struct f2fs_super_block *sb,
1760 struct f2fs_node *raw_node, nid_t ino, time_t mtime, mode_t mode)
1762 raw_node->footer.nid = cpu_to_le32(ino);
1763 raw_node->footer.ino = cpu_to_le32(ino);
1764 raw_node->footer.cp_ver = cpu_to_le64(1);
1766 raw_node->i.i_uid = cpu_to_le32(c.root_uid);
1767 raw_node->i.i_gid = cpu_to_le32(c.root_gid);
1769 raw_node->i.i_atime = cpu_to_le32(mtime);
1770 raw_node->i.i_atime_nsec = 0;
1771 raw_node->i.i_ctime = cpu_to_le32(mtime);
1772 raw_node->i.i_ctime_nsec = 0;
1773 raw_node->i.i_mtime = cpu_to_le32(mtime);
1774 raw_node->i.i_mtime_nsec = 0;
1775 raw_node->i.i_generation = 0;
1776 raw_node->i.i_xattr_nid = 0;
1777 raw_node->i.i_flags = 0;
1778 raw_node->i.i_current_depth = cpu_to_le32(S_ISDIR(mode) ? 1 : 0);
1779 raw_node->i.i_dir_level = DEF_DIR_LEVEL;
1780 raw_node->i.i_mode = cpu_to_le16(mode);
1781 raw_node->i.i_links = cpu_to_le32(S_ISDIR(mode) ? 2 : 1);
1783 /* for dentry block in directory */
1784 raw_node->i.i_size = cpu_to_le64(1 << get_sb(log_blocksize));
1785 raw_node->i.i_blocks = cpu_to_le64(2);
1787 if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
1788 raw_node->i.i_inline = F2FS_EXTRA_ATTR;
1789 raw_node->i.i_extra_isize = cpu_to_le16(calc_extra_isize());
1792 if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA))
1793 raw_node->i.i_projid = cpu_to_le32(F2FS_DEF_PROJID);
1795 if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CRTIME)) {
1796 raw_node->i.i_crtime = cpu_to_le32(mtime);
1797 raw_node->i.i_crtime_nsec = 0;
1800 if (c.feature & cpu_to_le32(F2FS_FEATURE_COMPRESSION)) {
1801 raw_node->i.i_compr_blocks = 0;
1802 raw_node->i.i_compress_algorithm = 0;
1803 raw_node->i.i_log_cluster_size = 0;
1804 raw_node->i.i_compress_flag = 0;
1807 raw_node->i.i_ext.fofs = 0;
1808 raw_node->i.i_ext.blk_addr = 0;
1809 raw_node->i.i_ext.len = 0;
1817 #define INIT_FEATURE_TABLE \
1818 struct feature feature_table[] = { \
1819 { "encrypt", F2FS_FEATURE_ENCRYPT }, \
1820 { "extra_attr", F2FS_FEATURE_EXTRA_ATTR }, \
1821 { "project_quota", F2FS_FEATURE_PRJQUOTA }, \
1822 { "inode_checksum", F2FS_FEATURE_INODE_CHKSUM }, \
1823 { "flexible_inline_xattr", F2FS_FEATURE_FLEXIBLE_INLINE_XATTR },\
1824 { "quota", F2FS_FEATURE_QUOTA_INO }, \
1825 { "inode_crtime", F2FS_FEATURE_INODE_CRTIME }, \
1826 { "lost_found", F2FS_FEATURE_LOST_FOUND }, \
1827 { "verity", F2FS_FEATURE_VERITY }, /* reserved */ \
1828 { "sb_checksum", F2FS_FEATURE_SB_CHKSUM }, \
1829 { "casefold", F2FS_FEATURE_CASEFOLD }, \
1830 { "compression", F2FS_FEATURE_COMPRESSION }, \
1831 { "ro", F2FS_FEATURE_RO}, \
1835 static inline u32 feature_map(struct feature *table, char *feature)
1838 for (p = table; p->name && strcmp(p->name, feature); p++)
1843 static inline int set_feature_bits(struct feature *table, char *features)
1845 u32 mask = feature_map(table, features);
1847 c.feature |= cpu_to_le32(mask);
1849 MSG(0, "Error: Wrong features %s\n", features);
1855 static inline int parse_feature(struct feature *table, const char *features)
1857 char *buf, *sub, *next;
1859 buf = strdup(features);
1863 for (sub = buf; sub && *sub; sub = next ? next + 1 : NULL) {
1864 /* Skip the beginning blanks */
1865 while (*sub && *sub == ' ')
1868 /* Skip a feature word */
1869 while (*next && *next != ' ' && *next != ',')
1877 if (set_feature_bits(table, sub)) {
1886 static inline int parse_root_owner(char *ids,
1887 uint32_t *root_uid, uint32_t *root_gid)
1894 for (i = 0; i < strlen(ids) - 1; i++)
1895 if (*(ids + i) == ':')
1900 *root_uid = atoi(uid);
1901 *root_gid = atoi(gid);
1908 struct f2fs_nls_table {
1910 const struct f2fs_nls_ops *ops;
1913 struct f2fs_nls_ops {
1914 int (*casefold)(const struct f2fs_nls_table *charset,
1915 const unsigned char *str, size_t len,
1916 unsigned char *dest, size_t dlen);
1919 extern const struct f2fs_nls_table *f2fs_load_nls_table(int encoding);
1920 #define F2FS_ENC_UTF8_12_0 1
1922 extern int f2fs_str2encoding(const char *string);
1923 extern char *f2fs_encoding2str(const int encoding);
1924 extern int f2fs_get_encoding_flags(int encoding);
1925 extern int f2fs_str2encoding_flags(char **param, __u16 *flags);
1927 #endif /*__F2FS_FS_H */