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 */
36 #endif /* HAVE_CONFIG_H */
39 #include <android_config.h>
49 #ifdef HAVE_LINUX_TYPES_H
50 #include <linux/types.h>
52 #include <sys/types.h>
54 #ifdef HAVE_KERNEL_UAPI_LINUX_BLKZONED_H
55 #include <kernel/uapi/linux/blkzoned.h>
56 #elif defined(HAVE_LINUX_BLKZONED_H)
57 #include <linux/blkzoned.h>
60 #ifdef HAVE_LIBSELINUX
61 #include <selinux/selinux.h>
62 #include <selinux/label.h>
66 #elif defined(__GNUC__)
67 # define UNUSED(x) UNUSED_ ## x __attribute__((unused))
68 #elif defined(__LCLINT__)
70 #elif defined(__cplusplus)
77 #define static_assert _Static_assert
80 #ifdef HAVE_SYS_MOUNT_H
81 #include <sys/mount.h>
86 #define fallthrough do {} while (0) /* fall through */
88 #define fallthrough __attribute__((__fallthrough__))
93 #undef HAVE_LINUX_TYPES_H
96 /* codes from kernel's f2fs.h, GPL-v2.0 */
97 #define MIN_COMPRESS_LOG_SIZE 2
98 #define MAX_COMPRESS_LOG_SIZE 8
100 typedef uint64_t u64;
101 typedef uint32_t u32;
102 typedef uint16_t u16;
109 typedef unsigned long pgoff_t;
110 typedef unsigned short umode_t;
112 #ifndef HAVE_LINUX_TYPES_H
126 * code borrowed from kernel f2fs dirver: f2fs.h, GPL-2.0
127 * : definitions of COMPRESS_DATA_RESERVED_SIZE,
128 * struct compress_data, COMPRESS_HEADER_SIZE,
129 * and struct compress_ctx
131 #define COMPRESS_DATA_RESERVED_SIZE 4
132 struct compress_data {
133 __le32 clen; /* compressed data size */
134 __le32 chksum; /* checksum of compressed data */
135 __le32 reserved[COMPRESS_DATA_RESERVED_SIZE]; /* reserved */
136 u8 cdata[]; /* compressed data */
138 #define COMPRESS_HEADER_SIZE (sizeof(struct compress_data))
139 /* compress context */
140 struct compress_ctx {
141 unsigned int cluster_size; /* page count in cluster */
142 unsigned int log_cluster_size; /* log of cluster size */
143 void *rbuf; /* compression input buffer */
144 struct compress_data *cbuf; /* comprsssion output header + data */
145 size_t rlen; /* valid data length in rbuf */
146 size_t clen; /* valid data length in cbuf */
147 void *private; /* work buf for compress algorithm */
151 #include <byteswap.h>
154 * bswap_16 - reverse bytes in a uint16_t value.
155 * @val: value whose bytes to swap.
158 * // Output contains "1024 is 4 as two bytes reversed"
159 * printf("1024 is %u as two bytes reversed\n", bswap_16(1024));
161 static inline uint16_t bswap_16(uint16_t val)
163 return ((val & (uint16_t)0x00ffU) << 8)
164 | ((val & (uint16_t)0xff00U) >> 8);
168 * bswap_32 - reverse bytes in a uint32_t value.
169 * @val: value whose bytes to swap.
172 * // Output contains "1024 is 262144 as four bytes reversed"
173 * printf("1024 is %u as four bytes reversed\n", bswap_32(1024));
175 static inline uint32_t bswap_32(uint32_t val)
177 return ((val & (uint32_t)0x000000ffUL) << 24)
178 | ((val & (uint32_t)0x0000ff00UL) << 8)
179 | ((val & (uint32_t)0x00ff0000UL) >> 8)
180 | ((val & (uint32_t)0xff000000UL) >> 24);
182 #endif /* !HAVE_BYTESWAP_H */
184 #if defined HAVE_DECL_BSWAP_64 && !HAVE_DECL_BSWAP_64
186 * bswap_64 - reverse bytes in a uint64_t value.
187 * @val: value whose bytes to swap.
190 * // Output contains "1024 is 1125899906842624 as eight bytes reversed"
191 * printf("1024 is %llu as eight bytes reversed\n",
192 * (unsigned long long)bswap_64(1024));
194 static inline uint64_t bswap_64(uint64_t val)
196 return ((val & (uint64_t)0x00000000000000ffULL) << 56)
197 | ((val & (uint64_t)0x000000000000ff00ULL) << 40)
198 | ((val & (uint64_t)0x0000000000ff0000ULL) << 24)
199 | ((val & (uint64_t)0x00000000ff000000ULL) << 8)
200 | ((val & (uint64_t)0x000000ff00000000ULL) >> 8)
201 | ((val & (uint64_t)0x0000ff0000000000ULL) >> 24)
202 | ((val & (uint64_t)0x00ff000000000000ULL) >> 40)
203 | ((val & (uint64_t)0xff00000000000000ULL) >> 56);
207 #if __BYTE_ORDER == __LITTLE_ENDIAN
208 #define le16_to_cpu(x) ((uint16_t)(x))
209 #define le32_to_cpu(x) ((uint32_t)(x))
210 #define le64_to_cpu(x) ((uint64_t)(x))
211 #define cpu_to_le16(x) ((uint16_t)(x))
212 #define cpu_to_le32(x) ((uint32_t)(x))
213 #define cpu_to_le64(x) ((uint64_t)(x))
214 #define be32_to_cpu(x) __builtin_bswap64(x)
215 #elif __BYTE_ORDER == __BIG_ENDIAN
216 #define le16_to_cpu(x) bswap_16(x)
217 #define le32_to_cpu(x) bswap_32(x)
218 #define le64_to_cpu(x) bswap_64(x)
219 #define cpu_to_le16(x) bswap_16(x)
220 #define cpu_to_le32(x) bswap_32(x)
221 #define cpu_to_le64(x) bswap_64(x)
222 #define be32_to_cpu(x) ((uint64_t)(x))
225 #define typecheck(type,x) \
227 typeof(x) __dummy2; \
228 (void)(&__dummy == &__dummy2); \
232 #define NULL_SEGNO ((unsigned int)~0)
235 * Debugging interfaces
237 #define FIX_MSG(fmt, ...) \
239 printf("[FIX] (%s:%4d) ", __func__, __LINE__); \
240 printf(" --> "fmt"\n", ##__VA_ARGS__); \
243 #define ASSERT_MSG(fmt, ...) \
245 printf("[ASSERT] (%s:%4d) ", __func__, __LINE__); \
246 printf(" --> "fmt"\n", ##__VA_ARGS__); \
250 #define ASSERT(exp) \
253 printf("[ASSERT] (%s:%4d) %s\n", \
254 __func__, __LINE__, #exp); \
259 #define ERR_MSG(fmt, ...) \
261 printf("[%s:%d] " fmt, __func__, __LINE__, ##__VA_ARGS__); \
264 #define MSG(n, fmt, ...) \
266 if (c.dbg_lv >= n && !c.layout && !c.show_file_map) { \
267 printf(fmt, ##__VA_ARGS__); \
271 #define DBG(n, fmt, ...) \
273 if (c.dbg_lv >= n && !c.layout && !c.show_file_map) { \
274 printf("[%s:%4d] " fmt, \
275 __func__, __LINE__, ##__VA_ARGS__); \
279 /* Display on console */
280 #define DISP(fmt, ptr, member) \
282 printf("%-30s" fmt, #member, ((ptr)->member)); \
285 #define DISP_raw_str(fmt, member) \
288 printf("%-30s " fmt "\n", #member":", member); \
290 printf("%-30s" "\t\t[" fmt "]\n", \
294 #define DISP_str(fmt, ptr, member) \
297 printf("%-30s " fmt "\n", \
298 #member":", ((ptr)->member)); \
300 printf("%-30s" "\t\t[" fmt "]\n", \
301 #member, ((ptr)->member)); \
304 #define DISP_u8(ptr, member) \
306 assert(sizeof((ptr)->member) == 1); \
308 printf("%-30s %u\n", \
309 #member":", ((ptr)->member)); \
311 printf("%-30s" "\t\t[0x%8x : %u]\n", \
312 #member, ((ptr)->member), ((ptr)->member)); \
315 #define DISP_u16(ptr, member) \
317 assert(sizeof((ptr)->member) == 2); \
319 printf("%-30s %u\n", \
320 #member":", le16_to_cpu(((ptr)->member))); \
322 printf("%-30s" "\t\t[0x%8x : %u]\n", \
323 #member, le16_to_cpu(((ptr)->member)), \
324 le16_to_cpu(((ptr)->member))); \
327 #define DISP_u32(ptr, member) \
329 assert(sizeof((ptr)->member) <= 4); \
331 printf("%-30s %u\n", \
332 #member":", le32_to_cpu(((ptr)->member))); \
334 printf("%-30s" "\t\t[0x%8x : %u]\n", \
335 #member, le32_to_cpu(((ptr)->member)), \
336 le32_to_cpu(((ptr)->member))); \
339 #define DISP_u64(ptr, member) \
341 assert(sizeof((ptr)->member) == 8); \
343 printf("%-30s %" PRIu64 "\n", \
344 #member":", le64_to_cpu(((ptr)->member))); \
346 printf("%-30s" "\t\t[0x%8" PRIx64 " : %" PRIu64 "]\n", \
347 #member, le64_to_cpu(((ptr)->member)), \
348 le64_to_cpu(((ptr)->member))); \
351 #define DISP_utf(ptr, member) \
354 printf("%-30s %s\n", #member":", \
357 printf("%-30s" "\t\t[%s]\n", #member, \
361 /* Display to buffer */
362 #define BUF_DISP_u32(buf, data, len, ptr, member) \
364 assert(sizeof((ptr)->member) <= 4); \
365 snprintf(buf, len, #member); \
366 snprintf(data, len, "0x%x : %u", ((ptr)->member), \
370 #define BUF_DISP_u64(buf, data, len, ptr, member) \
372 assert(sizeof((ptr)->member) == 8); \
373 snprintf(buf, len, #member); \
374 snprintf(data, len, "0x%llx : %llu", ((ptr)->member), \
378 #define BUF_DISP_utf(buf, data, len, ptr, member) \
379 snprintf(buf, len, #member)
381 /* these are defined in kernel */
382 #define BITS_PER_BYTE 8
384 #define SECTOR_SHIFT 9
386 #define F2FS_SUPER_MAGIC 0xF2F52010 /* F2FS Magic Number */
387 #define CP_CHKSUM_OFFSET 4092
388 #define SB_CHKSUM_OFFSET 3068
389 #define MAX_PATH_LEN 64
390 #define MAX_DEVICES 8
392 #define F2FS_BYTES_TO_BLK(bytes) ((bytes) >> F2FS_BLKSIZE_BITS)
393 #define F2FS_BLKSIZE_BITS 12
396 #define F2FS_NUMBER_OF_CHECKPOINT_PACK 2
397 #define DEFAULT_SECTOR_SIZE 512
398 #define DEFAULT_SECTORS_PER_BLOCK 8
399 #define DEFAULT_BLOCKS_PER_SEGMENT 512
400 #define DEFAULT_SEGMENTS_PER_SECTION 1
402 #define VERSION_LEN 256
403 #define VERSION_TIMESTAMP_LEN 4
404 #define VERSION_NAME_LEN (VERSION_LEN - VERSION_TIMESTAMP_LEN)
406 #define LPF "lost+found"
408 /* one for gc buffer, the other for node */
409 #define MIN_RSVD_SECS (NR_CURSEG_TYPE + 2U)
410 #define CONFIG_RSVD_DEFAULT_OP_RATIO 3.0
412 enum f2fs_config_func {
430 uint32_t sector_size;
431 uint64_t total_sectors; /* got by get_device_info */
432 uint64_t start_blkaddr;
433 uint64_t end_blkaddr;
434 uint32_t total_segments;
436 /* to handle zone block devices */
439 uint32_t nr_rnd_zones;
442 size_t *zone_cap_blocks;
446 /* Value 0 means no cache, minimum 1024 */
447 long num_cache_entry;
449 /* Value 0 means always overwrite (no collision allowed). maximum 16 */
450 unsigned max_hash_collision;
453 } dev_cache_config_t;
455 /* f2fs_configration for compression used for sload.f2fs */
457 void (*init)(struct compress_ctx *cc);
458 int (*compress)(struct compress_ctx *cc);
459 void (*reset)(struct compress_ctx *cc);
462 /* Should be aligned to supported_comp_names and support_comp_ops */
463 enum compress_algorithms {
470 COMPR_FILTER_UNASSIGNED = 0,
476 void (*add)(const char *);
477 void (*destroy)(void);
478 bool (*filter)(const char *);
482 bool enabled; /* disabled by default */
483 bool required; /* require to enable */
484 bool readonly; /* readonly to release blocks */
485 struct compress_ctx cc; /* work context */
486 enum compress_algorithms alg; /* algorithm to compress */
487 compress_ops *ops; /* ops per algorithm */
488 unsigned int min_blocks; /* save more blocks than this */
489 enum filter_policy filter; /* filter to try compression */
490 filter_ops *filter_ops; /* filter ops */
493 #define ALIGN_DOWN(addrs, size) (((addrs) / (size)) * (size))
494 #define ALIGN_UP(addrs, size) ALIGN_DOWN(((addrs) + (size) - 1), (size))
496 struct f2fs_configuration {
497 uint32_t conf_reserved_sections;
498 uint32_t reserved_segments;
499 uint32_t new_reserved_segments;
504 double overprovision;
505 double new_overprovision;
507 uint32_t segs_per_sec;
508 uint32_t secs_per_zone;
509 uint32_t segs_per_zone;
510 uint32_t start_sector;
511 uint32_t total_segments;
512 uint32_t sector_size;
513 uint64_t device_size;
514 uint64_t total_sectors;
515 uint64_t wanted_total_sectors;
516 uint64_t wanted_sector_size;
517 uint64_t target_sectors;
518 uint32_t sectors_per_blk;
519 uint32_t blks_per_seg;
520 __u8 init_version[VERSION_LEN + 1];
521 __u8 sb_version[VERSION_LEN + 1];
522 __u8 version[VERSION_LEN + 1];
526 uint16_t s_encoding_flags;
530 struct device_info devices[MAX_DEVICES];
532 char *extension_list[2];
533 const char *rootdev_name;
555 u64 show_file_map_max_offset;
559 int preserve_limits; /* preserve quota limits */
560 int large_nat_bitmap;
561 int fix_chksum; /* fix old cp.chksum position */
562 __le32 feature; /* defined features */
563 unsigned int quota_bits; /* quota bits */
566 /* mkfs parameters */
568 uint32_t next_free_nid;
577 /* defragmentation parameters */
579 uint64_t defrag_start;
581 uint64_t defrag_target;
583 /* sload parameters */
586 char *target_out_dir;
587 char *fs_config_file;
588 #ifdef HAVE_LIBSELINUX
589 struct selinux_opt seopt_file[8];
594 /* resize parameters */
597 /* precomputed fs UUID checksum for seeding other checksums */
598 uint32_t chksum_seed;
600 /* cache parameters */
601 dev_cache_config_t cache_config;
603 /* compression support for sload.f2fs */
604 compress_config_t compress;
608 #define BITS_PER_LONG 64
610 #define BITS_PER_LONG 32
613 #define BIT_MASK(nr) (1 << (nr % BITS_PER_LONG))
614 #define BIT_WORD(nr) (nr / BITS_PER_LONG)
616 #define set_sb_le64(member, val) (sb->member = cpu_to_le64(val))
617 #define set_sb_le32(member, val) (sb->member = cpu_to_le32(val))
618 #define set_sb_le16(member, val) (sb->member = cpu_to_le16(val))
619 #define get_sb_le64(member) le64_to_cpu(sb->member)
620 #define get_sb_le32(member) le32_to_cpu(sb->member)
621 #define get_sb_le16(member) le16_to_cpu(sb->member)
622 #define get_newsb_le64(member) le64_to_cpu(new_sb->member)
623 #define get_newsb_le32(member) le32_to_cpu(new_sb->member)
624 #define get_newsb_le16(member) le16_to_cpu(new_sb->member)
626 #define set_sb(member, val) \
628 typeof(sb->member) t = (val); \
629 switch (sizeof(t)) { \
630 case 8: set_sb_le64(member, t); break; \
631 case 4: set_sb_le32(member, t); break; \
632 case 2: set_sb_le16(member, t); break; \
636 #define get_sb(member) \
638 typeof(sb->member) t; \
639 switch (sizeof(t)) { \
640 case 8: t = get_sb_le64(member); break; \
641 case 4: t = get_sb_le32(member); break; \
642 case 2: t = get_sb_le16(member); break; \
646 #define get_newsb(member) \
648 typeof(new_sb->member) t; \
649 switch (sizeof(t)) { \
650 case 8: t = get_newsb_le64(member); break; \
651 case 4: t = get_newsb_le32(member); break; \
652 case 2: t = get_newsb_le16(member); break; \
657 #define set_cp_le64(member, val) (cp->member = cpu_to_le64(val))
658 #define set_cp_le32(member, val) (cp->member = cpu_to_le32(val))
659 #define set_cp_le16(member, val) (cp->member = cpu_to_le16(val))
660 #define get_cp_le64(member) le64_to_cpu(cp->member)
661 #define get_cp_le32(member) le32_to_cpu(cp->member)
662 #define get_cp_le16(member) le16_to_cpu(cp->member)
664 #define set_cp(member, val) \
666 typeof(cp->member) t = (val); \
667 switch (sizeof(t)) { \
668 case 8: set_cp_le64(member, t); break; \
669 case 4: set_cp_le32(member, t); break; \
670 case 2: set_cp_le16(member, t); break; \
674 #define get_cp(member) \
676 typeof(cp->member) t; \
677 switch (sizeof(t)) { \
678 case 8: t = get_cp_le64(member); break; \
679 case 4: t = get_cp_le32(member); break; \
680 case 2: t = get_cp_le16(member); break; \
686 * Copied from include/linux/kernel.h
688 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
689 #define round_down(x, y) ((x) & ~__round_mask(x, y))
691 #define min(x, y) ({ \
692 typeof(x) _min1 = (x); \
693 typeof(y) _min2 = (y); \
694 (void) (&_min1 == &_min2); \
695 _min1 < _min2 ? _min1 : _min2; })
697 #define max(x, y) ({ \
698 typeof(x) _max1 = (x); \
699 typeof(y) _max2 = (y); \
700 (void) (&_max1 == &_max2); \
701 _max1 > _max2 ? _max1 : _max2; })
703 #define round_up(x, y) (((x) + (y) - 1) / (y))
705 * Copied from fs/f2fs/f2fs.h
707 #define NR_CURSEG_DATA_TYPE (3)
708 #define NR_CURSEG_NODE_TYPE (3)
709 #define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
712 CURSEG_HOT_DATA = 0, /* directory entry blocks */
713 CURSEG_WARM_DATA, /* data blocks */
714 CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
715 CURSEG_HOT_NODE, /* direct node blocks of directory files */
716 CURSEG_WARM_NODE, /* direct node blocks of normal files */
717 CURSEG_COLD_NODE, /* indirect node blocks */
721 #define F2FS_MIN_SEGMENTS 9 /* SB + 2 (CP + SIT + NAT) + SSA + MAIN */
724 * Copied from fs/f2fs/segment.h
726 #define GET_SUM_TYPE(footer) ((footer)->entry_type)
727 #define SET_SUM_TYPE(footer, type) ((footer)->entry_type = type)
730 * Copied from include/linux/f2fs_sb.h
732 #define F2FS_SUPER_OFFSET 1024 /* byte-size offset */
733 #define F2FS_MIN_LOG_SECTOR_SIZE 9 /* 9 bits for 512 bytes */
734 #define F2FS_MAX_LOG_SECTOR_SIZE 12 /* 12 bits for 4096 bytes */
735 #define F2FS_BLKSIZE 4096 /* support only 4KB block */
736 #define F2FS_MAX_EXTENSION 64 /* # of extension entries */
737 #define F2FS_EXTENSION_LEN 8 /* max size of extension */
738 #define F2FS_BLK_ALIGN(x) (((x) + F2FS_BLKSIZE - 1) / F2FS_BLKSIZE)
740 #define NULL_ADDR 0x0U
742 #define COMPRESS_ADDR -2U
744 #define F2FS_ROOT_INO(sbi) (sbi->root_ino_num)
745 #define F2FS_NODE_INO(sbi) (sbi->node_ino_num)
746 #define F2FS_META_INO(sbi) (sbi->meta_ino_num)
748 #define F2FS_MAX_QUOTAS 3
749 #define QUOTA_DATA(i) (2)
750 #define QUOTA_INO(sb,t) (le32_to_cpu((sb)->qf_ino[t]))
752 #define FS_IMMUTABLE_FL 0x00000010 /* Immutable file */
754 #define F2FS_ENC_UTF8_12_1 1
755 #define F2FS_ENC_STRICT_MODE_FL (1 << 0)
757 /* This flag is used by node and meta inodes, and by recovery */
758 #define GFP_F2FS_ZERO (GFP_NOFS | __GFP_ZERO)
761 * For further optimization on multi-head logs, on-disk layout supports maximum
762 * 16 logs by default. The number, 16, is expected to cover all the cases
763 * enoughly. The implementaion currently uses no more than 6 logs.
764 * Half the logs are used for nodes, and the other half are used for data.
766 #define MAX_ACTIVE_LOGS 16
767 #define MAX_ACTIVE_NODE_LOGS 8
768 #define MAX_ACTIVE_DATA_LOGS 8
770 #define F2FS_FEATURE_ENCRYPT 0x0001
771 #define F2FS_FEATURE_BLKZONED 0x0002
772 #define F2FS_FEATURE_ATOMIC_WRITE 0x0004
773 #define F2FS_FEATURE_EXTRA_ATTR 0x0008
774 #define F2FS_FEATURE_PRJQUOTA 0x0010
775 #define F2FS_FEATURE_INODE_CHKSUM 0x0020
776 #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR 0x0040
777 #define F2FS_FEATURE_QUOTA_INO 0x0080
778 #define F2FS_FEATURE_INODE_CRTIME 0x0100
779 #define F2FS_FEATURE_LOST_FOUND 0x0200
780 #define F2FS_FEATURE_VERITY 0x0400 /* reserved */
781 #define F2FS_FEATURE_SB_CHKSUM 0x0800
782 #define F2FS_FEATURE_CASEFOLD 0x1000
783 #define F2FS_FEATURE_COMPRESSION 0x2000
784 #define F2FS_FEATURE_RO 0x4000
786 #define MAX_VOLUME_NAME 512
792 __u8 path[MAX_PATH_LEN];
793 __le32 total_segments;
796 static_assert(sizeof(struct f2fs_device) == 68, "");
798 /* reason of stop_checkpoint */
799 enum stop_cp_reason {
800 STOP_CP_REASON_SHUTDOWN,
801 STOP_CP_REASON_FAULT_INJECT,
802 STOP_CP_REASON_META_PAGE,
803 STOP_CP_REASON_WRITE_FAIL,
804 STOP_CP_REASON_CORRUPTED_SUMMARY,
805 STOP_CP_REASON_UPDATE_INODE,
806 STOP_CP_REASON_FLUSH_FAIL,
810 #define MAX_STOP_REASON 32
812 /* detail reason for EFSCORRUPTED */
814 ERROR_CORRUPTED_CLUSTER,
815 ERROR_FAIL_DECOMPRESSION,
816 ERROR_INVALID_BLKADDR,
817 ERROR_CORRUPTED_DIRENT,
818 ERROR_CORRUPTED_INODE,
819 ERROR_INCONSISTENT_SUMMARY,
820 ERROR_INCONSISTENT_FOOTER,
821 ERROR_INCONSISTENT_SUM_TYPE,
822 ERROR_CORRUPTED_JOURNAL,
823 ERROR_INCONSISTENT_NODE_COUNT,
824 ERROR_INCONSISTENT_BLOCK_COUNT,
825 ERROR_INVALID_CURSEG,
826 ERROR_INCONSISTENT_SIT,
827 ERROR_CORRUPTED_VERITY_XATTR,
828 ERROR_CORRUPTED_XATTR,
832 #define MAX_F2FS_ERRORS 16
834 struct f2fs_super_block {
835 __le32 magic; /* Magic Number */
836 __le16 major_ver; /* Major Version */
837 __le16 minor_ver; /* Minor Version */
838 __le32 log_sectorsize; /* log2 sector size in bytes */
839 __le32 log_sectors_per_block; /* log2 # of sectors per block */
840 __le32 log_blocksize; /* log2 block size in bytes */
841 __le32 log_blocks_per_seg; /* log2 # of blocks per segment */
842 __le32 segs_per_sec; /* # of segments per section */
843 __le32 secs_per_zone; /* # of sections per zone */
844 __le32 checksum_offset; /* checksum offset inside super block */
845 __le64 block_count __attribute__((packed));
846 /* total # of user blocks */
847 __le32 section_count; /* total # of sections */
848 __le32 segment_count; /* total # of segments */
849 __le32 segment_count_ckpt; /* # of segments for checkpoint */
850 __le32 segment_count_sit; /* # of segments for SIT */
851 __le32 segment_count_nat; /* # of segments for NAT */
852 __le32 segment_count_ssa; /* # of segments for SSA */
853 __le32 segment_count_main; /* # of segments for main area */
854 __le32 segment0_blkaddr; /* start block address of segment 0 */
855 __le32 cp_blkaddr; /* start block address of checkpoint */
856 __le32 sit_blkaddr; /* start block address of SIT */
857 __le32 nat_blkaddr; /* start block address of NAT */
858 __le32 ssa_blkaddr; /* start block address of SSA */
859 __le32 main_blkaddr; /* start block address of main area */
860 __le32 root_ino; /* root inode number */
861 __le32 node_ino; /* node inode number */
862 __le32 meta_ino; /* meta inode number */
863 __u8 uuid[16]; /* 128-bit uuid for volume */
864 __le16 volume_name[MAX_VOLUME_NAME]; /* volume name */
865 __le32 extension_count; /* # of extensions below */
866 __u8 extension_list[F2FS_MAX_EXTENSION][8]; /* extension array */
868 __u8 version[VERSION_LEN]; /* the kernel version */
869 __u8 init_version[VERSION_LEN]; /* the initial kernel version */
870 __le32 feature; /* defined features */
871 __u8 encryption_level; /* versioning level for encryption */
872 __u8 encrypt_pw_salt[16]; /* Salt used for string2key algorithm */
873 struct f2fs_device devs[MAX_DEVICES] __attribute__((packed)); /* device list */
874 __le32 qf_ino[F2FS_MAX_QUOTAS] __attribute__((packed)); /* quota inode numbers */
875 __u8 hot_ext_count; /* # of hot file extension */
876 __le16 s_encoding; /* Filename charset encoding */
877 __le16 s_encoding_flags; /* Filename charset encoding flags */
878 __u8 s_stop_reason[MAX_STOP_REASON]; /* stop checkpoint reason */
879 __u8 s_errors[MAX_F2FS_ERRORS]; /* reason of image corrupts */
880 __u8 reserved[258]; /* valid reserved region */
881 __le32 crc; /* checksum of superblock */
882 } __attribute__((packed));
884 static_assert(sizeof(struct f2fs_super_block) == 3072, "");
889 #define CP_RESIZEFS_FLAG 0x00004000
890 #define CP_DISABLED_FLAG 0x00001000
891 #define CP_QUOTA_NEED_FSCK_FLAG 0x00000800
892 #define CP_LARGE_NAT_BITMAP_FLAG 0x00000400
893 #define CP_NOCRC_RECOVERY_FLAG 0x00000200
894 #define CP_TRIMMED_FLAG 0x00000100
895 #define CP_NAT_BITS_FLAG 0x00000080
896 #define CP_CRC_RECOVERY_FLAG 0x00000040
897 #define CP_FASTBOOT_FLAG 0x00000020
898 #define CP_FSCK_FLAG 0x00000010
899 #define CP_ERROR_FLAG 0x00000008
900 #define CP_COMPACT_SUM_FLAG 0x00000004
901 #define CP_ORPHAN_PRESENT_FLAG 0x00000002
902 #define CP_UMOUNT_FLAG 0x00000001
904 #define F2FS_CP_PACKS 2 /* # of checkpoint packs */
906 struct f2fs_checkpoint {
907 __le64 checkpoint_ver; /* checkpoint block version number */
908 __le64 user_block_count; /* # of user blocks */
909 __le64 valid_block_count; /* # of valid blocks in main area */
910 __le32 rsvd_segment_count; /* # of reserved segments for gc */
911 __le32 overprov_segment_count; /* # of overprovision segments */
912 __le32 free_segment_count; /* # of free segments in main area */
914 /* information of current node segments */
915 __le32 cur_node_segno[MAX_ACTIVE_NODE_LOGS];
916 __le16 cur_node_blkoff[MAX_ACTIVE_NODE_LOGS];
917 /* information of current data segments */
918 __le32 cur_data_segno[MAX_ACTIVE_DATA_LOGS];
919 __le16 cur_data_blkoff[MAX_ACTIVE_DATA_LOGS];
920 __le32 ckpt_flags; /* Flags : umount and journal_present */
921 __le32 cp_pack_total_block_count; /* total # of one cp pack */
922 __le32 cp_pack_start_sum; /* start block number of data summary */
923 __le32 valid_node_count; /* Total number of valid nodes */
924 __le32 valid_inode_count; /* Total number of valid inodes */
925 __le32 next_free_nid; /* Next free node number */
926 __le32 sit_ver_bitmap_bytesize; /* Default value 64 */
927 __le32 nat_ver_bitmap_bytesize; /* Default value 256 */
928 __le32 checksum_offset; /* checksum offset inside cp block */
929 __le64 elapsed_time; /* mounted time */
930 /* allocation type of current segment */
931 unsigned char alloc_type[MAX_ACTIVE_LOGS];
933 /* SIT and NAT version bitmap */
934 unsigned char sit_nat_version_bitmap[];
937 static_assert(sizeof(struct f2fs_checkpoint) == 192, "");
939 #define CP_BITMAP_OFFSET \
940 (offsetof(struct f2fs_checkpoint, sit_nat_version_bitmap))
941 #define CP_MIN_CHKSUM_OFFSET CP_BITMAP_OFFSET
943 #define MIN_NAT_BITMAP_SIZE 64
944 #define MAX_SIT_BITMAP_SIZE_IN_CKPT \
945 (CP_CHKSUM_OFFSET - CP_BITMAP_OFFSET - MIN_NAT_BITMAP_SIZE)
946 #define MAX_BITMAP_SIZE_IN_CKPT \
947 (CP_CHKSUM_OFFSET - CP_BITMAP_OFFSET)
950 * For orphan inode management
952 #define F2FS_ORPHANS_PER_BLOCK 1020
954 struct f2fs_orphan_block {
955 __le32 ino[F2FS_ORPHANS_PER_BLOCK]; /* inode numbers */
956 __le32 reserved; /* reserved */
957 __le16 blk_addr; /* block index in current CP */
958 __le16 blk_count; /* Number of orphan inode blocks in CP */
959 __le32 entry_count; /* Total number of orphan nodes in current CP */
960 __le32 check_sum; /* CRC32 for orphan inode block */
963 static_assert(sizeof(struct f2fs_orphan_block) == 4096, "");
969 __le32 fofs; /* start file offset of the extent */
970 __le32 blk_addr; /* start block address of the extent */
971 __le32 len; /* lengh of the extent */
974 static_assert(sizeof(struct f2fs_extent) == 12, "");
976 #define F2FS_NAME_LEN 255
978 /* max output length of pretty_print_filename() including null terminator */
979 #define F2FS_PRINT_NAMELEN (4 * ((F2FS_NAME_LEN + 2) / 3) + 1)
981 /* 200 bytes for inline xattrs by default */
982 #define DEFAULT_INLINE_XATTR_ADDRS 50
983 #define DEF_ADDRS_PER_INODE 923 /* Address Pointers in an Inode */
984 #define CUR_ADDRS_PER_INODE(inode) (DEF_ADDRS_PER_INODE - \
985 __get_extra_isize(inode))
986 #define ADDRS_PER_INODE(i) addrs_per_inode(i)
987 #define DEF_ADDRS_PER_BLOCK 1018 /* Address Pointers in a Direct Block */
988 #define ADDRS_PER_BLOCK(i) addrs_per_block(i)
989 #define NIDS_PER_BLOCK 1018 /* Node IDs in an Indirect Block */
991 #define NODE_DIR1_BLOCK (DEF_ADDRS_PER_INODE + 1)
992 #define NODE_DIR2_BLOCK (DEF_ADDRS_PER_INODE + 2)
993 #define NODE_IND1_BLOCK (DEF_ADDRS_PER_INODE + 3)
994 #define NODE_IND2_BLOCK (DEF_ADDRS_PER_INODE + 4)
995 #define NODE_DIND_BLOCK (DEF_ADDRS_PER_INODE + 5)
997 #define F2FS_INLINE_XATTR 0x01 /* file inline xattr flag */
998 #define F2FS_INLINE_DATA 0x02 /* file inline data flag */
999 #define F2FS_INLINE_DENTRY 0x04 /* file inline dentry flag */
1000 #define F2FS_DATA_EXIST 0x08 /* file inline data exist flag */
1001 #define F2FS_INLINE_DOTS 0x10 /* file having implicit dot dentries */
1002 #define F2FS_EXTRA_ATTR 0x20 /* file having extra attribute */
1003 #define F2FS_PIN_FILE 0x40 /* file should not be gced */
1004 #define F2FS_COMPRESS_RELEASED 0x80 /* file released compressed blocks */
1006 #define F2FS_EXTRA_ISIZE_OFFSET \
1007 offsetof(struct f2fs_inode, i_extra_isize)
1008 #define F2FS_TOTAL_EXTRA_ATTR_SIZE \
1009 (offsetof(struct f2fs_inode, i_extra_end) - F2FS_EXTRA_ISIZE_OFFSET)
1011 #define F2FS_DEF_PROJID 0 /* default project ID */
1013 #define MAX_INLINE_DATA(node) (sizeof(__le32) * \
1014 (DEF_ADDRS_PER_INODE - \
1015 get_inline_xattr_addrs(&node->i) - \
1016 get_extra_isize(node) - \
1017 DEF_INLINE_RESERVED_SIZE))
1018 #define DEF_MAX_INLINE_DATA (sizeof(__le32) * \
1019 (DEF_ADDRS_PER_INODE - \
1020 DEFAULT_INLINE_XATTR_ADDRS - \
1021 F2FS_TOTAL_EXTRA_ATTR_SIZE - \
1022 DEF_INLINE_RESERVED_SIZE))
1023 #define INLINE_DATA_OFFSET (F2FS_BLKSIZE - \
1024 sizeof(struct node_footer) - \
1025 sizeof(__le32) * (DEF_ADDRS_PER_INODE + \
1026 5 - DEF_INLINE_RESERVED_SIZE))
1028 #define DEF_DIR_LEVEL 0
1031 * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
1033 #define FADVISE_COLD_BIT 0x01
1034 #define FADVISE_LOST_PINO_BIT 0x02
1035 #define FADVISE_ENCRYPT_BIT 0x04
1036 #define FADVISE_ENC_NAME_BIT 0x08
1037 #define FADVISE_KEEP_SIZE_BIT 0x10
1038 #define FADVISE_HOT_BIT 0x20
1039 #define FADVISE_VERITY_BIT 0x40 /* reserved */
1041 #define file_is_encrypt(fi) ((fi)->i_advise & FADVISE_ENCRYPT_BIT)
1042 #define file_enc_name(fi) ((fi)->i_advise & FADVISE_ENC_NAME_BIT)
1044 #define F2FS_CASEFOLD_FL 0x40000000 /* Casefolded file */
1045 #define IS_CASEFOLDED(dir) ((dir)->i_flags & F2FS_CASEFOLD_FL)
1048 * fsck i_compr_blocks counting helper
1050 struct f2fs_compr_blk_cnt {
1051 /* counting i_compr_blocks, init 0 */
1055 * previous seen compression header (COMPR_ADDR) page offsets,
1056 * use CHEADER_PGOFS_NONE for none
1060 #define CHEADER_PGOFS_NONE ((u32)-(1 << MAX_COMPRESS_LOG_SIZE))
1065 #define F2FS_COMPR_FL 0x00000004 /* Compress file */
1067 __le16 i_mode; /* file mode */
1068 __u8 i_advise; /* file hints */
1069 __u8 i_inline; /* file inline flags */
1070 __le32 i_uid; /* user ID */
1071 __le32 i_gid; /* group ID */
1072 __le32 i_links; /* links count */
1073 __le64 i_size; /* file size in bytes */
1074 __le64 i_blocks; /* file size in blocks */
1075 __le64 i_atime; /* access time */
1076 __le64 i_ctime; /* change time */
1077 __le64 i_mtime; /* modification time */
1078 __le32 i_atime_nsec; /* access time in nano scale */
1079 __le32 i_ctime_nsec; /* change time in nano scale */
1080 __le32 i_mtime_nsec; /* modification time in nano scale */
1081 __le32 i_generation; /* file version (for NFS) */
1083 __le32 i_current_depth; /* only for directory depth */
1084 __le16 i_gc_failures; /*
1085 * # of gc failures on pinned file.
1086 * only for regular files.
1089 __le32 i_xattr_nid; /* nid to save xattr */
1090 __le32 i_flags; /* file attributes */
1091 __le32 i_pino; /* parent inode number */
1092 __le32 i_namelen; /* file name length */
1093 __u8 i_name[F2FS_NAME_LEN]; /* file name for SPOR */
1094 __u8 i_dir_level; /* dentry_level for large dir */
1096 struct f2fs_extent i_ext __attribute__((packed)); /* caching a largest extent */
1100 __le16 i_extra_isize; /* extra inode attribute size */
1101 __le16 i_inline_xattr_size; /* inline xattr size, unit: 4 bytes */
1102 __le32 i_projid; /* project id */
1103 __le32 i_inode_checksum;/* inode meta checksum */
1104 __le64 i_crtime; /* creation time */
1105 __le32 i_crtime_nsec; /* creation time in nano scale */
1106 __le64 i_compr_blocks; /* # of compressed blocks */
1107 __u8 i_compress_algorithm; /* compress algorithm */
1108 __u8 i_log_cluster_size; /* log of cluster size */
1109 __le16 i_compress_flag; /* compress flag */
1110 /* 0 bit: chksum flag
1111 * [8,15] bits: compress level
1113 __le32 i_extra_end[0]; /* for attribute size calculation */
1114 } __attribute__((packed));
1115 __le32 i_addr[DEF_ADDRS_PER_INODE]; /* Pointers to data blocks */
1117 __le32 i_nid[5]; /* direct(2), indirect(2),
1118 double_indirect(1) node id */
1121 static_assert(offsetof(struct f2fs_inode, i_extra_end) -
1122 offsetof(struct f2fs_inode, i_extra_isize) == 36, "");
1123 static_assert(sizeof(struct f2fs_inode) == 4072, "");
1125 struct direct_node {
1126 __le32 addr[DEF_ADDRS_PER_BLOCK]; /* array of data block address */
1129 static_assert(sizeof(struct direct_node) == 4072, "");
1131 struct indirect_node {
1132 __le32 nid[NIDS_PER_BLOCK]; /* array of data block address */
1135 static_assert(sizeof(struct indirect_node) == 4072, "");
1144 #define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
1145 >> OFFSET_BIT_SHIFT)
1146 struct node_footer {
1147 __le32 nid; /* node id */
1148 __le32 ino; /* inode nunmber */
1149 __le32 flag; /* include cold/fsync/dentry marks and offset */
1150 __le64 cp_ver __attribute__((packed)); /* checkpoint version */
1151 __le32 next_blkaddr; /* next node page block address */
1154 static_assert(sizeof(struct node_footer) == 24, "");
1157 /* can be one of three types: inode, direct, and indirect types */
1159 struct f2fs_inode i;
1160 struct direct_node dn;
1161 struct indirect_node in;
1163 struct node_footer footer;
1166 static_assert(sizeof(struct f2fs_node) == 4096, "");
1171 #define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / sizeof(struct f2fs_nat_entry))
1172 #define NAT_BLOCK_OFFSET(start_nid) (start_nid / NAT_ENTRY_PER_BLOCK)
1174 #define DEFAULT_NAT_ENTRY_RATIO 20
1176 struct f2fs_nat_entry {
1177 __u8 version; /* latest version of cached nat entry */
1178 __le32 ino; /* inode number */
1179 __le32 block_addr; /* block address */
1180 } __attribute__((packed));
1182 static_assert(sizeof(struct f2fs_nat_entry) == 9, "");
1184 struct f2fs_nat_block {
1185 struct f2fs_nat_entry entries[NAT_ENTRY_PER_BLOCK];
1188 static_assert(sizeof(struct f2fs_nat_block) == 4095, "");
1193 * Each segment is 2MB in size by default so that a bitmap for validity of
1194 * there-in blocks should occupy 64 bytes, 512 bits.
1195 * Not allow to change this.
1197 #define SIT_VBLOCK_MAP_SIZE 64
1198 #define SIT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / sizeof(struct f2fs_sit_entry))
1201 * F2FS uses 4 bytes to represent block address. As a result, supported size of
1202 * disk is 16 TB and it equals to 16 * 1024 * 1024 / 2 segments.
1204 #define F2FS_MIN_SEGMENT 9 /* SB + 2 (CP + SIT + NAT) + SSA + MAIN */
1205 #define F2FS_MAX_SEGMENT ((16 * 1024 * 1024) / 2)
1206 #define MAX_SIT_BITMAP_SIZE (SEG_ALIGN(SIZE_ALIGN(F2FS_MAX_SEGMENT, \
1207 SIT_ENTRY_PER_BLOCK)) * \
1209 #define MAX_CP_PAYLOAD (SEG_ALIGN(SIZE_ALIGN(UINT32_MAX, NAT_ENTRY_PER_BLOCK)) * \
1210 DEFAULT_NAT_ENTRY_RATIO / 100 * \
1211 c.blks_per_seg / 8 + \
1212 MAX_SIT_BITMAP_SIZE - MAX_BITMAP_SIZE_IN_CKPT)
1215 * Note that f2fs_sit_entry->vblocks has the following bit-field information.
1216 * [15:10] : allocation type such as CURSEG_XXXX_TYPE
1217 * [9:0] : valid block count
1219 #define SIT_VBLOCKS_SHIFT 10
1220 #define SIT_VBLOCKS_MASK ((1 << SIT_VBLOCKS_SHIFT) - 1)
1221 #define GET_SIT_VBLOCKS(raw_sit) \
1222 (le16_to_cpu((raw_sit)->vblocks) & SIT_VBLOCKS_MASK)
1223 #define GET_SIT_TYPE(raw_sit) \
1224 ((le16_to_cpu((raw_sit)->vblocks) & ~SIT_VBLOCKS_MASK) \
1225 >> SIT_VBLOCKS_SHIFT)
1227 struct f2fs_sit_entry {
1228 __le16 vblocks; /* reference above */
1229 __u8 valid_map[SIT_VBLOCK_MAP_SIZE]; /* bitmap for valid blocks */
1230 __le64 mtime; /* segment age for cleaning */
1231 } __attribute__((packed));
1233 static_assert(sizeof(struct f2fs_sit_entry) == 74, "");
1235 struct f2fs_sit_block {
1236 struct f2fs_sit_entry entries[SIT_ENTRY_PER_BLOCK];
1239 static_assert(sizeof(struct f2fs_sit_block) == 4070, "");
1242 * For segment summary
1244 * One summary block contains exactly 512 summary entries, which represents
1245 * exactly 2MB segment by default. Not allow to change the basic units.
1247 * NOTE: For initializing fields, you must use set_summary
1249 * - If data page, nid represents dnode's nid
1250 * - If node page, nid represents the node page's nid.
1252 * The ofs_in_node is used by only data page. It represents offset
1253 * from node's page's beginning to get a data block address.
1254 * ex) data_blkaddr = (block_t)(nodepage_start_address + ofs_in_node)
1256 #define ENTRIES_IN_SUM 512
1257 #define SUMMARY_SIZE (7) /* sizeof(struct summary) */
1258 #define SUM_FOOTER_SIZE (5) /* sizeof(struct summary_footer) */
1259 #define SUM_ENTRIES_SIZE (SUMMARY_SIZE * ENTRIES_IN_SUM)
1261 /* a summary entry for a 4KB-sized block in a segment */
1262 struct f2fs_summary {
1263 __le32 nid; /* parent node id */
1267 __u8 version; /* node version number */
1268 __le16 ofs_in_node; /* block index in parent node */
1269 } __attribute__((packed));
1271 } __attribute__((packed));
1273 static_assert(sizeof(struct f2fs_summary) == 7, "");
1275 /* summary block type, node or data, is stored to the summary_footer */
1276 #define SUM_TYPE_NODE (1)
1277 #define SUM_TYPE_DATA (0)
1279 struct summary_footer {
1280 unsigned char entry_type; /* SUM_TYPE_XXX */
1281 __le32 check_sum __attribute__((packed)); /* summary checksum */
1284 static_assert(sizeof(struct summary_footer) == 5, "");
1286 #define SUM_JOURNAL_SIZE (F2FS_BLKSIZE - SUM_FOOTER_SIZE -\
1288 #define NAT_JOURNAL_ENTRIES ((SUM_JOURNAL_SIZE - 2) /\
1289 sizeof(struct nat_journal_entry))
1290 #define NAT_JOURNAL_RESERVED ((SUM_JOURNAL_SIZE - 2) %\
1291 sizeof(struct nat_journal_entry))
1292 #define SIT_JOURNAL_ENTRIES ((SUM_JOURNAL_SIZE - 2) /\
1293 sizeof(struct sit_journal_entry))
1294 #define SIT_JOURNAL_RESERVED ((SUM_JOURNAL_SIZE - 2) %\
1295 sizeof(struct sit_journal_entry))
1298 * Reserved area should make size of f2fs_extra_info equals to
1299 * that of nat_journal and sit_journal.
1301 #define EXTRA_INFO_RESERVED (SUM_JOURNAL_SIZE - 2 - 8)
1304 * frequently updated NAT/SIT entries can be stored in the spare area in
1312 struct nat_journal_entry {
1314 struct f2fs_nat_entry ne;
1315 } __attribute__((packed));
1317 static_assert(sizeof(struct nat_journal_entry) == 13, "");
1319 struct nat_journal {
1320 struct nat_journal_entry entries[NAT_JOURNAL_ENTRIES];
1321 __u8 reserved[NAT_JOURNAL_RESERVED];
1324 static_assert(sizeof(struct nat_journal) == 505, "");
1326 struct sit_journal_entry {
1328 struct f2fs_sit_entry se;
1329 } __attribute__((packed));
1331 static_assert(sizeof(struct sit_journal_entry) == 78, "");
1333 struct sit_journal {
1334 struct sit_journal_entry entries[SIT_JOURNAL_ENTRIES];
1335 __u8 reserved[SIT_JOURNAL_RESERVED];
1338 static_assert(sizeof(struct sit_journal) == 505, "");
1340 struct f2fs_extra_info {
1341 __le64 kbytes_written;
1342 __u8 reserved[EXTRA_INFO_RESERVED];
1343 } __attribute__((packed));
1345 static_assert(sizeof(struct f2fs_extra_info) == 505, "");
1347 struct f2fs_journal {
1352 /* spare area is used by NAT or SIT journals or extra info */
1354 struct nat_journal nat_j;
1355 struct sit_journal sit_j;
1356 struct f2fs_extra_info info;
1358 } __attribute__((packed));
1360 static_assert(sizeof(struct f2fs_journal) == 507, "");
1362 /* 4KB-sized summary block structure */
1363 struct f2fs_summary_block {
1364 struct f2fs_summary entries[ENTRIES_IN_SUM];
1365 struct f2fs_journal journal;
1366 struct summary_footer footer;
1369 static_assert(sizeof(struct f2fs_summary_block) == 4096, "");
1372 * For directory operations
1374 #define F2FS_DOT_HASH 0
1375 #define F2FS_DDOT_HASH F2FS_DOT_HASH
1376 #define F2FS_MAX_HASH (~((0x3ULL) << 62))
1377 #define F2FS_HASH_COL_BIT ((0x1ULL) << 63)
1379 typedef __le32 f2fs_hash_t;
1381 /* One directory entry slot covers 8bytes-long file name */
1382 #define F2FS_SLOT_LEN 8
1383 #define F2FS_SLOT_LEN_BITS 3
1385 #define GET_DENTRY_SLOTS(x) ((x + F2FS_SLOT_LEN - 1) >> F2FS_SLOT_LEN_BITS)
1387 /* the number of dentry in a block */
1388 #define NR_DENTRY_IN_BLOCK 214
1390 /* MAX level for dir lookup */
1391 #define MAX_DIR_HASH_DEPTH 63
1393 /* MAX buckets in one level of dir */
1394 #define MAX_DIR_BUCKETS (1 << ((MAX_DIR_HASH_DEPTH / 2) - 1))
1396 #define SIZE_OF_DIR_ENTRY 11 /* by byte */
1397 #define SIZE_OF_DENTRY_BITMAP ((NR_DENTRY_IN_BLOCK + BITS_PER_BYTE - 1) / \
1399 #define SIZE_OF_RESERVED (F2FS_BLKSIZE - ((SIZE_OF_DIR_ENTRY + \
1401 NR_DENTRY_IN_BLOCK + SIZE_OF_DENTRY_BITMAP))
1402 #define MIN_INLINE_DENTRY_SIZE 40 /* just include '.' and '..' entries */
1404 /* One directory entry slot representing F2FS_SLOT_LEN-sized file name */
1405 struct f2fs_dir_entry {
1406 __le32 hash_code; /* hash code of file name */
1407 __le32 ino; /* inode number */
1408 __le16 name_len; /* lengh of file name */
1409 __u8 file_type; /* file type */
1410 } __attribute__((packed));
1412 static_assert(sizeof(struct f2fs_dir_entry) == 11, "");
1414 /* 4KB-sized directory entry block */
1415 struct f2fs_dentry_block {
1416 /* validity bitmap for directory entries in each block */
1417 __u8 dentry_bitmap[SIZE_OF_DENTRY_BITMAP];
1418 __u8 reserved[SIZE_OF_RESERVED];
1419 struct f2fs_dir_entry dentry[NR_DENTRY_IN_BLOCK];
1420 __u8 filename[NR_DENTRY_IN_BLOCK][F2FS_SLOT_LEN];
1423 static_assert(sizeof(struct f2fs_dentry_block) == F2FS_BLKSIZE, "");
1425 /* for inline stuff */
1426 #define DEF_INLINE_RESERVED_SIZE 1
1428 /* for inline dir */
1429 #define NR_INLINE_DENTRY(node) (MAX_INLINE_DATA(node) * BITS_PER_BYTE / \
1430 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
1432 #define INLINE_DENTRY_BITMAP_SIZE(node) ((NR_INLINE_DENTRY(node) + \
1433 BITS_PER_BYTE - 1) / BITS_PER_BYTE)
1434 #define INLINE_RESERVED_SIZE(node) (MAX_INLINE_DATA(node) - \
1435 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
1436 NR_INLINE_DENTRY(node) + \
1437 INLINE_DENTRY_BITMAP_SIZE(node)))
1439 /* file types used in inode_info->flags */
1450 /* added for fsck */
1453 F2FS_FT_LAST_FILE_TYPE = F2FS_FT_XATTR,
1456 #define LINUX_S_IFMT 00170000
1457 #define LINUX_S_IFREG 0100000
1458 #define LINUX_S_ISREG(m) (((m) & LINUX_S_IFMT) == LINUX_S_IFREG)
1460 /* from f2fs/segment.h */
1466 extern int utf8_to_utf16(char *, const char *, size_t, size_t);
1467 extern int utf16_to_utf8(char *, const char *, size_t, size_t);
1468 extern int log_base_2(uint32_t);
1469 extern unsigned int addrs_per_inode(struct f2fs_inode *);
1470 extern unsigned int addrs_per_block(struct f2fs_inode *);
1471 extern unsigned int f2fs_max_file_offset(struct f2fs_inode *);
1472 extern __u32 f2fs_inode_chksum(struct f2fs_node *);
1473 extern __u32 f2fs_checkpoint_chksum(struct f2fs_checkpoint *);
1474 extern int write_inode(struct f2fs_node *, u64);
1476 extern int get_bits_in_byte(unsigned char n);
1477 extern int test_and_set_bit_le(u32, u8 *);
1478 extern int test_and_clear_bit_le(u32, u8 *);
1479 extern int test_bit_le(u32, const u8 *);
1480 extern int f2fs_test_bit(unsigned int, const char *);
1481 extern int f2fs_set_bit(unsigned int, char *);
1482 extern int f2fs_clear_bit(unsigned int, char *);
1483 extern u64 find_next_bit_le(const u8 *, u64, u64);
1484 extern u64 find_next_zero_bit_le(const u8 *, u64, u64);
1486 extern uint32_t f2fs_cal_crc32(uint32_t, void *, int);
1487 extern int f2fs_crc_valid(uint32_t blk_crc, void *buf, int len);
1489 extern void f2fs_init_configuration(void);
1490 extern int f2fs_devs_are_umounted(void);
1491 extern int f2fs_dev_is_writable(void);
1492 extern int f2fs_dev_is_umounted(char *);
1493 extern int f2fs_get_device_info(void);
1494 extern int f2fs_get_f2fs_info(void);
1495 extern unsigned int calc_extra_isize(void);
1496 extern int get_device_info(int);
1497 extern int f2fs_init_sparse_file(void);
1498 extern void f2fs_release_sparse_resource(void);
1499 extern int f2fs_finalize_device(void);
1500 extern int f2fs_fsync_device(void);
1502 extern void dcache_init(void);
1503 extern void dcache_release(void);
1505 extern int dev_read(void *, __u64, size_t);
1506 #ifdef POSIX_FADV_WILLNEED
1507 extern int dev_readahead(__u64, size_t);
1509 extern int dev_readahead(__u64, size_t UNUSED(len));
1511 extern int dev_write(void *, __u64, size_t);
1512 extern int dev_write_block(void *, __u64);
1513 extern int dev_write_dump(void *, __u64, size_t);
1514 /* All bytes in the buffer must be 0 use dev_fill(). */
1515 extern int dev_fill(void *, __u64, size_t);
1516 extern int dev_fill_block(void *, __u64);
1518 extern int dev_read_block(void *, __u64);
1519 extern int dev_reada_block(__u64);
1521 extern int dev_read_version(void *, __u64, size_t);
1522 extern void get_kernel_version(__u8 *);
1523 extern void get_kernel_uname_version(__u8 *);
1524 f2fs_hash_t f2fs_dentry_hash(int, int, const unsigned char *, int);
1526 static inline bool f2fs_has_extra_isize(struct f2fs_inode *inode)
1528 return (inode->i_inline & F2FS_EXTRA_ATTR);
1531 static inline int __get_extra_isize(struct f2fs_inode *inode)
1533 if (f2fs_has_extra_isize(inode))
1534 return le16_to_cpu(inode->i_extra_isize) / sizeof(__le32);
1538 extern struct f2fs_configuration c;
1539 static inline int get_inline_xattr_addrs(struct f2fs_inode *inode)
1541 if (c.feature & cpu_to_le32(F2FS_FEATURE_FLEXIBLE_INLINE_XATTR))
1542 return le16_to_cpu(inode->i_inline_xattr_size);
1543 else if (inode->i_inline & F2FS_INLINE_XATTR ||
1544 inode->i_inline & F2FS_INLINE_DENTRY)
1545 return DEFAULT_INLINE_XATTR_ADDRS;
1550 #define get_extra_isize(node) __get_extra_isize(&node->i)
1552 #define F2FS_ZONED_NONE 0
1553 #define F2FS_ZONED_HA 1
1554 #define F2FS_ZONED_HM 2
1556 #ifdef HAVE_LINUX_BLKZONED_H
1558 /* Let's just use v2, since v1 should be compatible with v2 */
1559 #define BLK_ZONE_REP_CAPACITY (1 << 0)
1560 struct blk_zone_v2 {
1561 __u64 start; /* Zone start sector */
1562 __u64 len; /* Zone length in number of sectors */
1563 __u64 wp; /* Zone write pointer position */
1564 __u8 type; /* Zone type */
1565 __u8 cond; /* Zone condition */
1566 __u8 non_seq; /* Non-sequential write resources active */
1567 __u8 reset; /* Reset write pointer recommended */
1569 __u64 capacity; /* Zone capacity in number of sectors */
1572 #define blk_zone blk_zone_v2
1574 struct blk_zone_report_v2 {
1578 struct blk_zone zones[0];
1580 #define blk_zone_report blk_zone_report_v2
1582 #define blk_zone_type(z) (z)->type
1583 #define blk_zone_conv(z) ((z)->type == BLK_ZONE_TYPE_CONVENTIONAL)
1584 #define blk_zone_seq_req(z) ((z)->type == BLK_ZONE_TYPE_SEQWRITE_REQ)
1585 #define blk_zone_seq_pref(z) ((z)->type == BLK_ZONE_TYPE_SEQWRITE_PREF)
1586 #define blk_zone_seq(z) (blk_zone_seq_req(z) || blk_zone_seq_pref(z))
1588 static inline const char *
1589 blk_zone_type_str(struct blk_zone *blkz)
1591 switch (blk_zone_type(blkz)) {
1592 case BLK_ZONE_TYPE_CONVENTIONAL:
1593 return( "Conventional" );
1594 case BLK_ZONE_TYPE_SEQWRITE_REQ:
1595 return( "Sequential-write-required" );
1596 case BLK_ZONE_TYPE_SEQWRITE_PREF:
1597 return( "Sequential-write-preferred" );
1599 return( "Unknown-type" );
1602 #define blk_zone_cond(z) (z)->cond
1604 static inline const char *
1605 blk_zone_cond_str(struct blk_zone *blkz)
1607 switch (blk_zone_cond(blkz)) {
1608 case BLK_ZONE_COND_NOT_WP:
1609 return "Not-write-pointer";
1610 case BLK_ZONE_COND_EMPTY:
1612 case BLK_ZONE_COND_IMP_OPEN:
1613 return "Implicit-open";
1614 case BLK_ZONE_COND_EXP_OPEN:
1615 return "Explicit-open";
1616 case BLK_ZONE_COND_CLOSED:
1618 case BLK_ZONE_COND_READONLY:
1620 case BLK_ZONE_COND_FULL:
1622 case BLK_ZONE_COND_OFFLINE:
1625 return "Unknown-cond";
1629 * Handle kernel zone capacity support
1631 #define blk_zone_empty(z) (blk_zone_cond(z) == BLK_ZONE_COND_EMPTY)
1632 #define blk_zone_sector(z) (z)->start
1633 #define blk_zone_length(z) (z)->len
1634 #define blk_zone_wp_sector(z) (z)->wp
1635 #define blk_zone_need_reset(z) (int)(z)->reset
1636 #define blk_zone_non_seq(z) (int)(z)->non_seq
1637 #define blk_zone_capacity(z, f) ((f & BLK_ZONE_REP_CAPACITY) ? \
1638 (z)->capacity : (z)->len)
1644 extern int f2fs_get_zoned_model(int);
1645 extern int f2fs_get_zone_blocks(int);
1646 extern int f2fs_report_zone(int, uint64_t, struct blk_zone *);
1647 typedef int (report_zones_cb_t)(int i, void *, void *);
1648 extern int f2fs_report_zones(int, report_zones_cb_t *, void *);
1649 extern int f2fs_check_zones(int);
1650 int f2fs_reset_zone(int, void *);
1651 extern int f2fs_reset_zones(int);
1652 extern uint32_t f2fs_get_usable_segments(struct f2fs_super_block *sb);
1654 #define SIZE_ALIGN(val, size) (((val) + (size) - 1) / (size))
1655 #define SEG_ALIGN(blks) SIZE_ALIGN(blks, c.blks_per_seg)
1656 #define ZONE_ALIGN(blks) SIZE_ALIGN(blks, c.blks_per_seg * \
1659 static inline double get_reserved(struct f2fs_super_block *sb, double ovp)
1662 uint32_t usable_main_segs = f2fs_get_usable_segments(sb);
1663 uint32_t segs_per_sec = round_up(usable_main_segs, get_sb(section_count));
1665 if (c.conf_reserved_sections)
1666 reserved = c.conf_reserved_sections * segs_per_sec;
1668 reserved = (100 / ovp + 1 + NR_CURSEG_TYPE) * segs_per_sec;
1673 static inline double get_best_overprovision(struct f2fs_super_block *sb)
1675 double reserved, ovp, candidate, end, diff, space;
1676 double max_ovp = 0, max_space = 0;
1677 uint32_t usable_main_segs = f2fs_get_usable_segments(sb);
1679 if (get_sb(segment_count_main) < 256) {
1689 for (; candidate <= end; candidate += diff) {
1690 reserved = get_reserved(sb, candidate);
1691 ovp = (usable_main_segs - reserved) * candidate / 100;
1694 space = usable_main_segs - max(reserved, ovp) -
1695 2 * get_sb(segs_per_sec);
1696 if (max_space < space) {
1698 max_ovp = candidate;
1704 static inline __le64 get_cp_crc(struct f2fs_checkpoint *cp)
1706 uint64_t cp_ver = get_cp(checkpoint_ver);
1707 size_t crc_offset = get_cp(checksum_offset);
1708 uint32_t crc = le32_to_cpu(*(__le32 *)((unsigned char *)cp +
1711 cp_ver |= ((uint64_t)crc << 32);
1712 return cpu_to_le64(cp_ver);
1715 static inline int exist_qf_ino(struct f2fs_super_block *sb)
1719 for (i = 0; i < F2FS_MAX_QUOTAS; i++)
1725 static inline int is_qf_ino(struct f2fs_super_block *sb, nid_t ino)
1729 for (i = 0; i < F2FS_MAX_QUOTAS; i++)
1730 if (sb->qf_ino[i] == ino)
1735 static inline void show_version(const char *prog)
1737 #if defined(F2FS_TOOLS_VERSION) && defined(F2FS_TOOLS_DATE)
1738 MSG(0, "%s %s (%s)\n", prog, F2FS_TOOLS_VERSION, F2FS_TOOLS_DATE);
1740 MSG(0, "%s -- version not supported\n", prog);
1744 static inline void f2fs_init_qf_inode(struct f2fs_super_block *sb,
1745 struct f2fs_node *raw_node, int qtype, time_t mtime)
1747 raw_node->footer.nid = sb->qf_ino[qtype];
1748 raw_node->footer.ino = sb->qf_ino[qtype];
1749 raw_node->footer.cp_ver = cpu_to_le64(1);
1750 raw_node->i.i_mode = cpu_to_le16(0x8180);
1751 raw_node->i.i_links = cpu_to_le32(1);
1752 raw_node->i.i_uid = cpu_to_le32(c.root_uid);
1753 raw_node->i.i_gid = cpu_to_le32(c.root_gid);
1755 raw_node->i.i_size = cpu_to_le64(1024 * 6); /* Hard coded */
1756 raw_node->i.i_blocks = cpu_to_le64(1);
1758 raw_node->i.i_atime = cpu_to_le32(mtime);
1759 raw_node->i.i_atime_nsec = 0;
1760 raw_node->i.i_ctime = cpu_to_le32(mtime);
1761 raw_node->i.i_ctime_nsec = 0;
1762 raw_node->i.i_mtime = cpu_to_le32(mtime);
1763 raw_node->i.i_mtime_nsec = 0;
1764 raw_node->i.i_generation = 0;
1765 raw_node->i.i_xattr_nid = 0;
1766 raw_node->i.i_flags = FS_IMMUTABLE_FL;
1767 raw_node->i.i_current_depth = cpu_to_le32(0);
1768 raw_node->i.i_dir_level = DEF_DIR_LEVEL;
1770 if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
1771 raw_node->i.i_inline = F2FS_EXTRA_ATTR;
1772 raw_node->i.i_extra_isize = cpu_to_le16(calc_extra_isize());
1775 if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA))
1776 raw_node->i.i_projid = cpu_to_le32(F2FS_DEF_PROJID);
1778 raw_node->i.i_ext.fofs = 0;
1779 raw_node->i.i_ext.blk_addr = 0;
1780 raw_node->i.i_ext.len = 0;
1788 #define INIT_FEATURE_TABLE \
1789 struct feature feature_table[] = { \
1790 { "encrypt", F2FS_FEATURE_ENCRYPT }, \
1791 { "extra_attr", F2FS_FEATURE_EXTRA_ATTR }, \
1792 { "project_quota", F2FS_FEATURE_PRJQUOTA }, \
1793 { "inode_checksum", F2FS_FEATURE_INODE_CHKSUM }, \
1794 { "flexible_inline_xattr", F2FS_FEATURE_FLEXIBLE_INLINE_XATTR },\
1795 { "quota", F2FS_FEATURE_QUOTA_INO }, \
1796 { "inode_crtime", F2FS_FEATURE_INODE_CRTIME }, \
1797 { "lost_found", F2FS_FEATURE_LOST_FOUND }, \
1798 { "verity", F2FS_FEATURE_VERITY }, /* reserved */ \
1799 { "sb_checksum", F2FS_FEATURE_SB_CHKSUM }, \
1800 { "casefold", F2FS_FEATURE_CASEFOLD }, \
1801 { "compression", F2FS_FEATURE_COMPRESSION }, \
1802 { "ro", F2FS_FEATURE_RO}, \
1806 static inline u32 feature_map(struct feature *table, char *feature)
1809 for (p = table; p->name && strcmp(p->name, feature); p++)
1814 static inline int set_feature_bits(struct feature *table, char *features)
1816 u32 mask = feature_map(table, features);
1818 c.feature |= cpu_to_le32(mask);
1820 MSG(0, "Error: Wrong features %s\n", features);
1826 static inline int parse_feature(struct feature *table, const char *features)
1828 char *buf, *sub, *next;
1830 buf = strdup(features);
1834 for (sub = buf; sub && *sub; sub = next ? next + 1 : NULL) {
1835 /* Skip the beginning blanks */
1836 while (*sub && *sub == ' ')
1839 /* Skip a feature word */
1840 while (*next && *next != ' ' && *next != ',')
1848 if (set_feature_bits(table, sub)) {
1857 static inline int parse_root_owner(char *ids,
1858 uint32_t *root_uid, uint32_t *root_gid)
1865 for (i = 0; i < strlen(ids) - 1; i++)
1866 if (*(ids + i) == ':')
1871 *root_uid = atoi(uid);
1872 *root_gid = atoi(gid);
1879 struct f2fs_nls_table {
1881 const struct f2fs_nls_ops *ops;
1884 struct f2fs_nls_ops {
1885 int (*casefold)(const struct f2fs_nls_table *charset,
1886 const unsigned char *str, size_t len,
1887 unsigned char *dest, size_t dlen);
1890 extern const struct f2fs_nls_table *f2fs_load_nls_table(int encoding);
1891 #define F2FS_ENC_UTF8_12_0 1
1893 extern int f2fs_str2encoding(const char *string);
1894 extern char *f2fs_encoding2str(const int encoding);
1895 extern int f2fs_get_encoding_flags(int encoding);
1896 extern int f2fs_str2encoding_flags(char **param, __u16 *flags);
1898 #endif /*__F2FS_FS_H */