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_u16(ptr, member) \
287 assert(sizeof((ptr)->member) == 2); \
289 printf("%-30s %u\n", \
290 #member":", le16_to_cpu(((ptr)->member))); \
292 printf("%-30s" "\t\t[0x%8x : %u]\n", \
293 #member, le16_to_cpu(((ptr)->member)), \
294 le16_to_cpu(((ptr)->member))); \
297 #define DISP_u32(ptr, member) \
299 assert(sizeof((ptr)->member) <= 4); \
301 printf("%-30s %u\n", \
302 #member":", le32_to_cpu(((ptr)->member))); \
304 printf("%-30s" "\t\t[0x%8x : %u]\n", \
305 #member, le32_to_cpu(((ptr)->member)), \
306 le32_to_cpu(((ptr)->member))); \
309 #define DISP_u64(ptr, member) \
311 assert(sizeof((ptr)->member) == 8); \
313 printf("%-30s %" PRIu64 "\n", \
314 #member":", le64_to_cpu(((ptr)->member))); \
316 printf("%-30s" "\t\t[0x%8" PRIx64 " : %" PRIu64 "]\n", \
317 #member, le64_to_cpu(((ptr)->member)), \
318 le64_to_cpu(((ptr)->member))); \
321 #define DISP_utf(ptr, member) \
324 printf("%-30s %s\n", #member":", \
327 printf("%-30s" "\t\t[%s]\n", #member, \
331 /* Display to buffer */
332 #define BUF_DISP_u32(buf, data, len, ptr, member) \
334 assert(sizeof((ptr)->member) <= 4); \
335 snprintf(buf, len, #member); \
336 snprintf(data, len, "0x%x : %u", ((ptr)->member), \
340 #define BUF_DISP_u64(buf, data, len, ptr, member) \
342 assert(sizeof((ptr)->member) == 8); \
343 snprintf(buf, len, #member); \
344 snprintf(data, len, "0x%llx : %llu", ((ptr)->member), \
348 #define BUF_DISP_utf(buf, data, len, ptr, member) \
349 snprintf(buf, len, #member)
351 /* these are defined in kernel */
352 #define BITS_PER_BYTE 8
354 #define SECTOR_SHIFT 9
356 #define F2FS_SUPER_MAGIC 0xF2F52010 /* F2FS Magic Number */
357 #define CP_CHKSUM_OFFSET 4092
358 #define SB_CHKSUM_OFFSET 3068
359 #define MAX_PATH_LEN 64
360 #define MAX_DEVICES 8
362 #define F2FS_BYTES_TO_BLK(bytes) ((bytes) >> F2FS_BLKSIZE_BITS)
363 #define F2FS_BLKSIZE_BITS 12
366 #define F2FS_NUMBER_OF_CHECKPOINT_PACK 2
367 #define DEFAULT_SECTOR_SIZE 512
368 #define DEFAULT_SECTORS_PER_BLOCK 8
369 #define DEFAULT_BLOCKS_PER_SEGMENT 512
370 #define DEFAULT_SEGMENTS_PER_SECTION 1
372 #define VERSION_LEN 256
373 #define VERSION_TIMESTAMP_LEN 4
374 #define VERSION_NAME_LEN (VERSION_LEN - VERSION_TIMESTAMP_LEN)
376 #define LPF "lost+found"
378 /* one for gc buffer, the other for node */
379 #define MIN_RSVD_SECS (NR_CURSEG_TYPE + 2U)
380 #define CONFIG_RSVD_DEFAULT_OP_RATIO 3.0
382 enum f2fs_config_func {
400 uint32_t sector_size;
401 uint64_t total_sectors; /* got by get_device_info */
402 uint64_t start_blkaddr;
403 uint64_t end_blkaddr;
404 uint32_t total_segments;
406 /* to handle zone block devices */
409 uint32_t nr_rnd_zones;
412 size_t *zone_cap_blocks;
416 /* Value 0 means no cache, minimum 1024 */
417 long num_cache_entry;
419 /* Value 0 means always overwrite (no collision allowed). maximum 16 */
420 unsigned max_hash_collision;
423 } dev_cache_config_t;
425 /* f2fs_configration for compression used for sload.f2fs */
427 void (*init)(struct compress_ctx *cc);
428 int (*compress)(struct compress_ctx *cc);
429 void (*reset)(struct compress_ctx *cc);
432 /* Should be aligned to supported_comp_names and support_comp_ops */
433 enum compress_algorithms {
440 COMPR_FILTER_UNASSIGNED = 0,
446 void (*add)(const char *);
447 void (*destroy)(void);
448 bool (*filter)(const char *);
452 bool enabled; /* disabled by default */
453 bool required; /* require to enable */
454 bool readonly; /* readonly to release blocks */
455 struct compress_ctx cc; /* work context */
456 enum compress_algorithms alg; /* algorithm to compress */
457 compress_ops *ops; /* ops per algorithm */
458 unsigned int min_blocks; /* save more blocks than this */
459 enum filter_policy filter; /* filter to try compression */
460 filter_ops *filter_ops; /* filter ops */
463 #define ALIGN_DOWN(addrs, size) (((addrs) / (size)) * (size))
464 #define ALIGN_UP(addrs, size) ALIGN_DOWN(((addrs) + (size) - 1), (size))
466 struct f2fs_configuration {
467 uint32_t conf_reserved_sections;
468 uint32_t reserved_segments;
469 uint32_t new_reserved_segments;
474 double overprovision;
475 double new_overprovision;
477 uint32_t segs_per_sec;
478 uint32_t secs_per_zone;
479 uint32_t segs_per_zone;
480 uint32_t start_sector;
481 uint32_t total_segments;
482 uint32_t sector_size;
483 uint64_t device_size;
484 uint64_t total_sectors;
485 uint64_t wanted_total_sectors;
486 uint64_t wanted_sector_size;
487 uint64_t target_sectors;
488 uint32_t sectors_per_blk;
489 uint32_t blks_per_seg;
490 __u8 init_version[VERSION_LEN + 1];
491 __u8 sb_version[VERSION_LEN + 1];
492 __u8 version[VERSION_LEN + 1];
496 uint16_t s_encoding_flags;
500 struct device_info devices[MAX_DEVICES];
502 char *extension_list[2];
503 const char *rootdev_name;
525 u64 show_file_map_max_offset;
529 int preserve_limits; /* preserve quota limits */
530 int large_nat_bitmap;
531 int fix_chksum; /* fix old cp.chksum position */
532 __le32 feature; /* defined features */
533 unsigned int quota_bits; /* quota bits */
536 /* mkfs parameters */
538 uint32_t next_free_nid;
547 /* defragmentation parameters */
549 uint64_t defrag_start;
551 uint64_t defrag_target;
553 /* sload parameters */
556 char *target_out_dir;
557 char *fs_config_file;
558 #ifdef HAVE_LIBSELINUX
559 struct selinux_opt seopt_file[8];
564 /* resize parameters */
567 /* precomputed fs UUID checksum for seeding other checksums */
568 uint32_t chksum_seed;
570 /* cache parameters */
571 dev_cache_config_t cache_config;
573 /* compression support for sload.f2fs */
574 compress_config_t compress;
578 #define BITS_PER_LONG 64
580 #define BITS_PER_LONG 32
583 #define BIT_MASK(nr) (1 << (nr % BITS_PER_LONG))
584 #define BIT_WORD(nr) (nr / BITS_PER_LONG)
586 #define set_sb_le64(member, val) (sb->member = cpu_to_le64(val))
587 #define set_sb_le32(member, val) (sb->member = cpu_to_le32(val))
588 #define set_sb_le16(member, val) (sb->member = cpu_to_le16(val))
589 #define get_sb_le64(member) le64_to_cpu(sb->member)
590 #define get_sb_le32(member) le32_to_cpu(sb->member)
591 #define get_sb_le16(member) le16_to_cpu(sb->member)
592 #define get_newsb_le64(member) le64_to_cpu(new_sb->member)
593 #define get_newsb_le32(member) le32_to_cpu(new_sb->member)
594 #define get_newsb_le16(member) le16_to_cpu(new_sb->member)
596 #define set_sb(member, val) \
598 typeof(sb->member) t = (val); \
599 switch (sizeof(t)) { \
600 case 8: set_sb_le64(member, t); break; \
601 case 4: set_sb_le32(member, t); break; \
602 case 2: set_sb_le16(member, t); break; \
606 #define get_sb(member) \
608 typeof(sb->member) t; \
609 switch (sizeof(t)) { \
610 case 8: t = get_sb_le64(member); break; \
611 case 4: t = get_sb_le32(member); break; \
612 case 2: t = get_sb_le16(member); break; \
616 #define get_newsb(member) \
618 typeof(new_sb->member) t; \
619 switch (sizeof(t)) { \
620 case 8: t = get_newsb_le64(member); break; \
621 case 4: t = get_newsb_le32(member); break; \
622 case 2: t = get_newsb_le16(member); break; \
627 #define set_cp_le64(member, val) (cp->member = cpu_to_le64(val))
628 #define set_cp_le32(member, val) (cp->member = cpu_to_le32(val))
629 #define set_cp_le16(member, val) (cp->member = cpu_to_le16(val))
630 #define get_cp_le64(member) le64_to_cpu(cp->member)
631 #define get_cp_le32(member) le32_to_cpu(cp->member)
632 #define get_cp_le16(member) le16_to_cpu(cp->member)
634 #define set_cp(member, val) \
636 typeof(cp->member) t = (val); \
637 switch (sizeof(t)) { \
638 case 8: set_cp_le64(member, t); break; \
639 case 4: set_cp_le32(member, t); break; \
640 case 2: set_cp_le16(member, t); break; \
644 #define get_cp(member) \
646 typeof(cp->member) t; \
647 switch (sizeof(t)) { \
648 case 8: t = get_cp_le64(member); break; \
649 case 4: t = get_cp_le32(member); break; \
650 case 2: t = get_cp_le16(member); break; \
656 * Copied from include/linux/kernel.h
658 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
659 #define round_down(x, y) ((x) & ~__round_mask(x, y))
661 #define min(x, y) ({ \
662 typeof(x) _min1 = (x); \
663 typeof(y) _min2 = (y); \
664 (void) (&_min1 == &_min2); \
665 _min1 < _min2 ? _min1 : _min2; })
667 #define max(x, y) ({ \
668 typeof(x) _max1 = (x); \
669 typeof(y) _max2 = (y); \
670 (void) (&_max1 == &_max2); \
671 _max1 > _max2 ? _max1 : _max2; })
673 #define round_up(x, y) (((x) + (y) - 1) / (y))
675 * Copied from fs/f2fs/f2fs.h
677 #define NR_CURSEG_DATA_TYPE (3)
678 #define NR_CURSEG_NODE_TYPE (3)
679 #define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
682 CURSEG_HOT_DATA = 0, /* directory entry blocks */
683 CURSEG_WARM_DATA, /* data blocks */
684 CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
685 CURSEG_HOT_NODE, /* direct node blocks of directory files */
686 CURSEG_WARM_NODE, /* direct node blocks of normal files */
687 CURSEG_COLD_NODE, /* indirect node blocks */
691 #define F2FS_MIN_SEGMENTS 9 /* SB + 2 (CP + SIT + NAT) + SSA + MAIN */
694 * Copied from fs/f2fs/segment.h
696 #define GET_SUM_TYPE(footer) ((footer)->entry_type)
697 #define SET_SUM_TYPE(footer, type) ((footer)->entry_type = type)
700 * Copied from include/linux/f2fs_sb.h
702 #define F2FS_SUPER_OFFSET 1024 /* byte-size offset */
703 #define F2FS_MIN_LOG_SECTOR_SIZE 9 /* 9 bits for 512 bytes */
704 #define F2FS_MAX_LOG_SECTOR_SIZE 12 /* 12 bits for 4096 bytes */
705 #define F2FS_BLKSIZE 4096 /* support only 4KB block */
706 #define F2FS_MAX_EXTENSION 64 /* # of extension entries */
707 #define F2FS_EXTENSION_LEN 8 /* max size of extension */
708 #define F2FS_BLK_ALIGN(x) (((x) + F2FS_BLKSIZE - 1) / F2FS_BLKSIZE)
710 #define NULL_ADDR 0x0U
712 #define COMPRESS_ADDR -2U
714 #define F2FS_ROOT_INO(sbi) (sbi->root_ino_num)
715 #define F2FS_NODE_INO(sbi) (sbi->node_ino_num)
716 #define F2FS_META_INO(sbi) (sbi->meta_ino_num)
718 #define F2FS_MAX_QUOTAS 3
719 #define QUOTA_DATA(i) (2)
720 #define QUOTA_INO(sb,t) (le32_to_cpu((sb)->qf_ino[t]))
722 #define FS_IMMUTABLE_FL 0x00000010 /* Immutable file */
724 #define F2FS_ENC_UTF8_12_1 1
725 #define F2FS_ENC_STRICT_MODE_FL (1 << 0)
727 /* This flag is used by node and meta inodes, and by recovery */
728 #define GFP_F2FS_ZERO (GFP_NOFS | __GFP_ZERO)
731 * For further optimization on multi-head logs, on-disk layout supports maximum
732 * 16 logs by default. The number, 16, is expected to cover all the cases
733 * enoughly. The implementaion currently uses no more than 6 logs.
734 * Half the logs are used for nodes, and the other half are used for data.
736 #define MAX_ACTIVE_LOGS 16
737 #define MAX_ACTIVE_NODE_LOGS 8
738 #define MAX_ACTIVE_DATA_LOGS 8
740 #define F2FS_FEATURE_ENCRYPT 0x0001
741 #define F2FS_FEATURE_BLKZONED 0x0002
742 #define F2FS_FEATURE_ATOMIC_WRITE 0x0004
743 #define F2FS_FEATURE_EXTRA_ATTR 0x0008
744 #define F2FS_FEATURE_PRJQUOTA 0x0010
745 #define F2FS_FEATURE_INODE_CHKSUM 0x0020
746 #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR 0x0040
747 #define F2FS_FEATURE_QUOTA_INO 0x0080
748 #define F2FS_FEATURE_INODE_CRTIME 0x0100
749 #define F2FS_FEATURE_LOST_FOUND 0x0200
750 #define F2FS_FEATURE_VERITY 0x0400 /* reserved */
751 #define F2FS_FEATURE_SB_CHKSUM 0x0800
752 #define F2FS_FEATURE_CASEFOLD 0x1000
753 #define F2FS_FEATURE_COMPRESSION 0x2000
754 #define F2FS_FEATURE_RO 0x4000
756 #define MAX_VOLUME_NAME 512
762 __u8 path[MAX_PATH_LEN];
763 __le32 total_segments;
766 static_assert(sizeof(struct f2fs_device) == 68, "");
768 /* reason of stop_checkpoint */
769 enum stop_cp_reason {
770 STOP_CP_REASON_SHUTDOWN,
771 STOP_CP_REASON_FAULT_INJECT,
772 STOP_CP_REASON_META_PAGE,
773 STOP_CP_REASON_WRITE_FAIL,
774 STOP_CP_REASON_CORRUPTED_SUMMARY,
775 STOP_CP_REASON_UPDATE_INODE,
776 STOP_CP_REASON_FLUSH_FAIL,
780 #define MAX_STOP_REASON 32
782 /* detail reason for EFSCORRUPTED */
784 ERROR_CORRUPTED_CLUSTER,
785 ERROR_FAIL_DECOMPRESSION,
786 ERROR_INVALID_BLKADDR,
787 ERROR_CORRUPTED_DIRENT,
788 ERROR_CORRUPTED_INODE,
789 ERROR_INCONSISTENT_SUMMARY,
790 ERROR_INCONSISTENT_FOOTER,
791 ERROR_INCONSISTENT_SUM_TYPE,
792 ERROR_CORRUPTED_JOURNAL,
793 ERROR_INCONSISTENT_NODE_COUNT,
794 ERROR_INCONSISTENT_BLOCK_COUNT,
795 ERROR_INVALID_CURSEG,
796 ERROR_INCONSISTENT_SIT,
797 ERROR_CORRUPTED_VERITY_XATTR,
798 ERROR_CORRUPTED_XATTR,
802 #define MAX_F2FS_ERRORS 16
804 struct f2fs_super_block {
805 __le32 magic; /* Magic Number */
806 __le16 major_ver; /* Major Version */
807 __le16 minor_ver; /* Minor Version */
808 __le32 log_sectorsize; /* log2 sector size in bytes */
809 __le32 log_sectors_per_block; /* log2 # of sectors per block */
810 __le32 log_blocksize; /* log2 block size in bytes */
811 __le32 log_blocks_per_seg; /* log2 # of blocks per segment */
812 __le32 segs_per_sec; /* # of segments per section */
813 __le32 secs_per_zone; /* # of sections per zone */
814 __le32 checksum_offset; /* checksum offset inside super block */
815 __le64 block_count __attribute__((packed));
816 /* total # of user blocks */
817 __le32 section_count; /* total # of sections */
818 __le32 segment_count; /* total # of segments */
819 __le32 segment_count_ckpt; /* # of segments for checkpoint */
820 __le32 segment_count_sit; /* # of segments for SIT */
821 __le32 segment_count_nat; /* # of segments for NAT */
822 __le32 segment_count_ssa; /* # of segments for SSA */
823 __le32 segment_count_main; /* # of segments for main area */
824 __le32 segment0_blkaddr; /* start block address of segment 0 */
825 __le32 cp_blkaddr; /* start block address of checkpoint */
826 __le32 sit_blkaddr; /* start block address of SIT */
827 __le32 nat_blkaddr; /* start block address of NAT */
828 __le32 ssa_blkaddr; /* start block address of SSA */
829 __le32 main_blkaddr; /* start block address of main area */
830 __le32 root_ino; /* root inode number */
831 __le32 node_ino; /* node inode number */
832 __le32 meta_ino; /* meta inode number */
833 __u8 uuid[16]; /* 128-bit uuid for volume */
834 __le16 volume_name[MAX_VOLUME_NAME]; /* volume name */
835 __le32 extension_count; /* # of extensions below */
836 __u8 extension_list[F2FS_MAX_EXTENSION][8]; /* extension array */
838 __u8 version[VERSION_LEN]; /* the kernel version */
839 __u8 init_version[VERSION_LEN]; /* the initial kernel version */
840 __le32 feature; /* defined features */
841 __u8 encryption_level; /* versioning level for encryption */
842 __u8 encrypt_pw_salt[16]; /* Salt used for string2key algorithm */
843 struct f2fs_device devs[MAX_DEVICES] __attribute__((packed)); /* device list */
844 __le32 qf_ino[F2FS_MAX_QUOTAS] __attribute__((packed)); /* quota inode numbers */
845 __u8 hot_ext_count; /* # of hot file extension */
846 __le16 s_encoding; /* Filename charset encoding */
847 __le16 s_encoding_flags; /* Filename charset encoding flags */
848 __u8 s_stop_reason[MAX_STOP_REASON]; /* stop checkpoint reason */
849 __u8 s_errors[MAX_F2FS_ERRORS]; /* reason of image corrupts */
850 __u8 reserved[258]; /* valid reserved region */
851 __le32 crc; /* checksum of superblock */
852 } __attribute__((packed));
854 static_assert(sizeof(struct f2fs_super_block) == 3072, "");
859 #define CP_RESIZEFS_FLAG 0x00004000
860 #define CP_DISABLED_FLAG 0x00001000
861 #define CP_QUOTA_NEED_FSCK_FLAG 0x00000800
862 #define CP_LARGE_NAT_BITMAP_FLAG 0x00000400
863 #define CP_NOCRC_RECOVERY_FLAG 0x00000200
864 #define CP_TRIMMED_FLAG 0x00000100
865 #define CP_NAT_BITS_FLAG 0x00000080
866 #define CP_CRC_RECOVERY_FLAG 0x00000040
867 #define CP_FASTBOOT_FLAG 0x00000020
868 #define CP_FSCK_FLAG 0x00000010
869 #define CP_ERROR_FLAG 0x00000008
870 #define CP_COMPACT_SUM_FLAG 0x00000004
871 #define CP_ORPHAN_PRESENT_FLAG 0x00000002
872 #define CP_UMOUNT_FLAG 0x00000001
874 #define F2FS_CP_PACKS 2 /* # of checkpoint packs */
876 struct f2fs_checkpoint {
877 __le64 checkpoint_ver; /* checkpoint block version number */
878 __le64 user_block_count; /* # of user blocks */
879 __le64 valid_block_count; /* # of valid blocks in main area */
880 __le32 rsvd_segment_count; /* # of reserved segments for gc */
881 __le32 overprov_segment_count; /* # of overprovision segments */
882 __le32 free_segment_count; /* # of free segments in main area */
884 /* information of current node segments */
885 __le32 cur_node_segno[MAX_ACTIVE_NODE_LOGS];
886 __le16 cur_node_blkoff[MAX_ACTIVE_NODE_LOGS];
887 /* information of current data segments */
888 __le32 cur_data_segno[MAX_ACTIVE_DATA_LOGS];
889 __le16 cur_data_blkoff[MAX_ACTIVE_DATA_LOGS];
890 __le32 ckpt_flags; /* Flags : umount and journal_present */
891 __le32 cp_pack_total_block_count; /* total # of one cp pack */
892 __le32 cp_pack_start_sum; /* start block number of data summary */
893 __le32 valid_node_count; /* Total number of valid nodes */
894 __le32 valid_inode_count; /* Total number of valid inodes */
895 __le32 next_free_nid; /* Next free node number */
896 __le32 sit_ver_bitmap_bytesize; /* Default value 64 */
897 __le32 nat_ver_bitmap_bytesize; /* Default value 256 */
898 __le32 checksum_offset; /* checksum offset inside cp block */
899 __le64 elapsed_time; /* mounted time */
900 /* allocation type of current segment */
901 unsigned char alloc_type[MAX_ACTIVE_LOGS];
903 /* SIT and NAT version bitmap */
904 unsigned char sit_nat_version_bitmap[];
907 static_assert(sizeof(struct f2fs_checkpoint) == 192, "");
909 #define CP_BITMAP_OFFSET \
910 (offsetof(struct f2fs_checkpoint, sit_nat_version_bitmap))
911 #define CP_MIN_CHKSUM_OFFSET CP_BITMAP_OFFSET
913 #define MIN_NAT_BITMAP_SIZE 64
914 #define MAX_SIT_BITMAP_SIZE_IN_CKPT \
915 (CP_CHKSUM_OFFSET - CP_BITMAP_OFFSET - MIN_NAT_BITMAP_SIZE)
916 #define MAX_BITMAP_SIZE_IN_CKPT \
917 (CP_CHKSUM_OFFSET - CP_BITMAP_OFFSET)
920 * For orphan inode management
922 #define F2FS_ORPHANS_PER_BLOCK 1020
924 struct f2fs_orphan_block {
925 __le32 ino[F2FS_ORPHANS_PER_BLOCK]; /* inode numbers */
926 __le32 reserved; /* reserved */
927 __le16 blk_addr; /* block index in current CP */
928 __le16 blk_count; /* Number of orphan inode blocks in CP */
929 __le32 entry_count; /* Total number of orphan nodes in current CP */
930 __le32 check_sum; /* CRC32 for orphan inode block */
933 static_assert(sizeof(struct f2fs_orphan_block) == 4096, "");
939 __le32 fofs; /* start file offset of the extent */
940 __le32 blk_addr; /* start block address of the extent */
941 __le32 len; /* lengh of the extent */
944 static_assert(sizeof(struct f2fs_extent) == 12, "");
946 #define F2FS_NAME_LEN 255
948 /* max output length of pretty_print_filename() including null terminator */
949 #define F2FS_PRINT_NAMELEN (4 * ((F2FS_NAME_LEN + 2) / 3) + 1)
951 /* 200 bytes for inline xattrs by default */
952 #define DEFAULT_INLINE_XATTR_ADDRS 50
953 #define DEF_ADDRS_PER_INODE 923 /* Address Pointers in an Inode */
954 #define CUR_ADDRS_PER_INODE(inode) (DEF_ADDRS_PER_INODE - \
955 __get_extra_isize(inode))
956 #define ADDRS_PER_INODE(i) addrs_per_inode(i)
957 #define DEF_ADDRS_PER_BLOCK 1018 /* Address Pointers in a Direct Block */
958 #define ADDRS_PER_BLOCK(i) addrs_per_block(i)
959 #define NIDS_PER_BLOCK 1018 /* Node IDs in an Indirect Block */
961 #define NODE_DIR1_BLOCK (DEF_ADDRS_PER_INODE + 1)
962 #define NODE_DIR2_BLOCK (DEF_ADDRS_PER_INODE + 2)
963 #define NODE_IND1_BLOCK (DEF_ADDRS_PER_INODE + 3)
964 #define NODE_IND2_BLOCK (DEF_ADDRS_PER_INODE + 4)
965 #define NODE_DIND_BLOCK (DEF_ADDRS_PER_INODE + 5)
967 #define F2FS_INLINE_XATTR 0x01 /* file inline xattr flag */
968 #define F2FS_INLINE_DATA 0x02 /* file inline data flag */
969 #define F2FS_INLINE_DENTRY 0x04 /* file inline dentry flag */
970 #define F2FS_DATA_EXIST 0x08 /* file inline data exist flag */
971 #define F2FS_INLINE_DOTS 0x10 /* file having implicit dot dentries */
972 #define F2FS_EXTRA_ATTR 0x20 /* file having extra attribute */
973 #define F2FS_PIN_FILE 0x40 /* file should not be gced */
974 #define F2FS_COMPRESS_RELEASED 0x80 /* file released compressed blocks */
976 #define F2FS_EXTRA_ISIZE_OFFSET \
977 offsetof(struct f2fs_inode, i_extra_isize)
978 #define F2FS_TOTAL_EXTRA_ATTR_SIZE \
979 (offsetof(struct f2fs_inode, i_extra_end) - F2FS_EXTRA_ISIZE_OFFSET)
981 #define F2FS_DEF_PROJID 0 /* default project ID */
983 #define MAX_INLINE_DATA(node) (sizeof(__le32) * \
984 (DEF_ADDRS_PER_INODE - \
985 get_inline_xattr_addrs(&node->i) - \
986 get_extra_isize(node) - \
987 DEF_INLINE_RESERVED_SIZE))
988 #define DEF_MAX_INLINE_DATA (sizeof(__le32) * \
989 (DEF_ADDRS_PER_INODE - \
990 DEFAULT_INLINE_XATTR_ADDRS - \
991 F2FS_TOTAL_EXTRA_ATTR_SIZE - \
992 DEF_INLINE_RESERVED_SIZE))
993 #define INLINE_DATA_OFFSET (F2FS_BLKSIZE - \
994 sizeof(struct node_footer) - \
995 sizeof(__le32) * (DEF_ADDRS_PER_INODE + \
996 5 - DEF_INLINE_RESERVED_SIZE))
998 #define DEF_DIR_LEVEL 0
1001 * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
1003 #define FADVISE_COLD_BIT 0x01
1004 #define FADVISE_LOST_PINO_BIT 0x02
1005 #define FADVISE_ENCRYPT_BIT 0x04
1006 #define FADVISE_ENC_NAME_BIT 0x08
1007 #define FADVISE_KEEP_SIZE_BIT 0x10
1008 #define FADVISE_HOT_BIT 0x20
1009 #define FADVISE_VERITY_BIT 0x40 /* reserved */
1011 #define file_is_encrypt(fi) ((fi)->i_advise & FADVISE_ENCRYPT_BIT)
1012 #define file_enc_name(fi) ((fi)->i_advise & FADVISE_ENC_NAME_BIT)
1014 #define F2FS_CASEFOLD_FL 0x40000000 /* Casefolded file */
1015 #define IS_CASEFOLDED(dir) ((dir)->i_flags & F2FS_CASEFOLD_FL)
1018 * fsck i_compr_blocks counting helper
1020 struct f2fs_compr_blk_cnt {
1021 /* counting i_compr_blocks, init 0 */
1025 * previous seen compression header (COMPR_ADDR) page offsets,
1026 * use CHEADER_PGOFS_NONE for none
1030 #define CHEADER_PGOFS_NONE ((u32)-(1 << MAX_COMPRESS_LOG_SIZE))
1035 #define F2FS_COMPR_FL 0x00000004 /* Compress file */
1037 __le16 i_mode; /* file mode */
1038 __u8 i_advise; /* file hints */
1039 __u8 i_inline; /* file inline flags */
1040 __le32 i_uid; /* user ID */
1041 __le32 i_gid; /* group ID */
1042 __le32 i_links; /* links count */
1043 __le64 i_size; /* file size in bytes */
1044 __le64 i_blocks; /* file size in blocks */
1045 __le64 i_atime; /* access time */
1046 __le64 i_ctime; /* change time */
1047 __le64 i_mtime; /* modification time */
1048 __le32 i_atime_nsec; /* access time in nano scale */
1049 __le32 i_ctime_nsec; /* change time in nano scale */
1050 __le32 i_mtime_nsec; /* modification time in nano scale */
1051 __le32 i_generation; /* file version (for NFS) */
1053 __le32 i_current_depth; /* only for directory depth */
1054 __le16 i_gc_failures; /*
1055 * # of gc failures on pinned file.
1056 * only for regular files.
1059 __le32 i_xattr_nid; /* nid to save xattr */
1060 __le32 i_flags; /* file attributes */
1061 __le32 i_pino; /* parent inode number */
1062 __le32 i_namelen; /* file name length */
1063 __u8 i_name[F2FS_NAME_LEN]; /* file name for SPOR */
1064 __u8 i_dir_level; /* dentry_level for large dir */
1066 struct f2fs_extent i_ext __attribute__((packed)); /* caching a largest extent */
1070 __le16 i_extra_isize; /* extra inode attribute size */
1071 __le16 i_inline_xattr_size; /* inline xattr size, unit: 4 bytes */
1072 __le32 i_projid; /* project id */
1073 __le32 i_inode_checksum;/* inode meta checksum */
1074 __le64 i_crtime; /* creation time */
1075 __le32 i_crtime_nsec; /* creation time in nano scale */
1076 __le64 i_compr_blocks; /* # of compressed blocks */
1077 __u8 i_compress_algorithm; /* compress algorithm */
1078 __u8 i_log_cluster_size; /* log of cluster size */
1079 __le16 i_compress_flag; /* compress flag */
1080 /* 0 bit: chksum flag
1081 * [8,15] bits: compress level
1083 __le32 i_extra_end[0]; /* for attribute size calculation */
1084 } __attribute__((packed));
1085 __le32 i_addr[DEF_ADDRS_PER_INODE]; /* Pointers to data blocks */
1087 __le32 i_nid[5]; /* direct(2), indirect(2),
1088 double_indirect(1) node id */
1091 static_assert(offsetof(struct f2fs_inode, i_extra_end) -
1092 offsetof(struct f2fs_inode, i_extra_isize) == 36, "");
1093 static_assert(sizeof(struct f2fs_inode) == 4072, "");
1095 struct direct_node {
1096 __le32 addr[DEF_ADDRS_PER_BLOCK]; /* array of data block address */
1099 static_assert(sizeof(struct direct_node) == 4072, "");
1101 struct indirect_node {
1102 __le32 nid[NIDS_PER_BLOCK]; /* array of data block address */
1105 static_assert(sizeof(struct indirect_node) == 4072, "");
1114 #define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
1115 >> OFFSET_BIT_SHIFT)
1116 struct node_footer {
1117 __le32 nid; /* node id */
1118 __le32 ino; /* inode nunmber */
1119 __le32 flag; /* include cold/fsync/dentry marks and offset */
1120 __le64 cp_ver __attribute__((packed)); /* checkpoint version */
1121 __le32 next_blkaddr; /* next node page block address */
1124 static_assert(sizeof(struct node_footer) == 24, "");
1127 /* can be one of three types: inode, direct, and indirect types */
1129 struct f2fs_inode i;
1130 struct direct_node dn;
1131 struct indirect_node in;
1133 struct node_footer footer;
1136 static_assert(sizeof(struct f2fs_node) == 4096, "");
1141 #define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / sizeof(struct f2fs_nat_entry))
1142 #define NAT_BLOCK_OFFSET(start_nid) (start_nid / NAT_ENTRY_PER_BLOCK)
1144 #define DEFAULT_NAT_ENTRY_RATIO 20
1146 struct f2fs_nat_entry {
1147 __u8 version; /* latest version of cached nat entry */
1148 __le32 ino; /* inode number */
1149 __le32 block_addr; /* block address */
1150 } __attribute__((packed));
1152 static_assert(sizeof(struct f2fs_nat_entry) == 9, "");
1154 struct f2fs_nat_block {
1155 struct f2fs_nat_entry entries[NAT_ENTRY_PER_BLOCK];
1158 static_assert(sizeof(struct f2fs_nat_block) == 4095, "");
1163 * Each segment is 2MB in size by default so that a bitmap for validity of
1164 * there-in blocks should occupy 64 bytes, 512 bits.
1165 * Not allow to change this.
1167 #define SIT_VBLOCK_MAP_SIZE 64
1168 #define SIT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / sizeof(struct f2fs_sit_entry))
1171 * F2FS uses 4 bytes to represent block address. As a result, supported size of
1172 * disk is 16 TB and it equals to 16 * 1024 * 1024 / 2 segments.
1174 #define F2FS_MIN_SEGMENT 9 /* SB + 2 (CP + SIT + NAT) + SSA + MAIN */
1175 #define F2FS_MAX_SEGMENT ((16 * 1024 * 1024) / 2)
1176 #define MAX_SIT_BITMAP_SIZE (SEG_ALIGN(SIZE_ALIGN(F2FS_MAX_SEGMENT, \
1177 SIT_ENTRY_PER_BLOCK)) * \
1179 #define MAX_CP_PAYLOAD (SEG_ALIGN(SIZE_ALIGN(UINT32_MAX, NAT_ENTRY_PER_BLOCK)) * \
1180 DEFAULT_NAT_ENTRY_RATIO / 100 * \
1181 c.blks_per_seg / 8 + \
1182 MAX_SIT_BITMAP_SIZE - MAX_BITMAP_SIZE_IN_CKPT)
1185 * Note that f2fs_sit_entry->vblocks has the following bit-field information.
1186 * [15:10] : allocation type such as CURSEG_XXXX_TYPE
1187 * [9:0] : valid block count
1189 #define SIT_VBLOCKS_SHIFT 10
1190 #define SIT_VBLOCKS_MASK ((1 << SIT_VBLOCKS_SHIFT) - 1)
1191 #define GET_SIT_VBLOCKS(raw_sit) \
1192 (le16_to_cpu((raw_sit)->vblocks) & SIT_VBLOCKS_MASK)
1193 #define GET_SIT_TYPE(raw_sit) \
1194 ((le16_to_cpu((raw_sit)->vblocks) & ~SIT_VBLOCKS_MASK) \
1195 >> SIT_VBLOCKS_SHIFT)
1197 struct f2fs_sit_entry {
1198 __le16 vblocks; /* reference above */
1199 __u8 valid_map[SIT_VBLOCK_MAP_SIZE]; /* bitmap for valid blocks */
1200 __le64 mtime; /* segment age for cleaning */
1201 } __attribute__((packed));
1203 static_assert(sizeof(struct f2fs_sit_entry) == 74, "");
1205 struct f2fs_sit_block {
1206 struct f2fs_sit_entry entries[SIT_ENTRY_PER_BLOCK];
1209 static_assert(sizeof(struct f2fs_sit_block) == 4070, "");
1212 * For segment summary
1214 * One summary block contains exactly 512 summary entries, which represents
1215 * exactly 2MB segment by default. Not allow to change the basic units.
1217 * NOTE: For initializing fields, you must use set_summary
1219 * - If data page, nid represents dnode's nid
1220 * - If node page, nid represents the node page's nid.
1222 * The ofs_in_node is used by only data page. It represents offset
1223 * from node's page's beginning to get a data block address.
1224 * ex) data_blkaddr = (block_t)(nodepage_start_address + ofs_in_node)
1226 #define ENTRIES_IN_SUM 512
1227 #define SUMMARY_SIZE (7) /* sizeof(struct summary) */
1228 #define SUM_FOOTER_SIZE (5) /* sizeof(struct summary_footer) */
1229 #define SUM_ENTRIES_SIZE (SUMMARY_SIZE * ENTRIES_IN_SUM)
1231 /* a summary entry for a 4KB-sized block in a segment */
1232 struct f2fs_summary {
1233 __le32 nid; /* parent node id */
1237 __u8 version; /* node version number */
1238 __le16 ofs_in_node; /* block index in parent node */
1239 } __attribute__((packed));
1241 } __attribute__((packed));
1243 static_assert(sizeof(struct f2fs_summary) == 7, "");
1245 /* summary block type, node or data, is stored to the summary_footer */
1246 #define SUM_TYPE_NODE (1)
1247 #define SUM_TYPE_DATA (0)
1249 struct summary_footer {
1250 unsigned char entry_type; /* SUM_TYPE_XXX */
1251 __le32 check_sum __attribute__((packed)); /* summary checksum */
1254 static_assert(sizeof(struct summary_footer) == 5, "");
1256 #define SUM_JOURNAL_SIZE (F2FS_BLKSIZE - SUM_FOOTER_SIZE -\
1258 #define NAT_JOURNAL_ENTRIES ((SUM_JOURNAL_SIZE - 2) /\
1259 sizeof(struct nat_journal_entry))
1260 #define NAT_JOURNAL_RESERVED ((SUM_JOURNAL_SIZE - 2) %\
1261 sizeof(struct nat_journal_entry))
1262 #define SIT_JOURNAL_ENTRIES ((SUM_JOURNAL_SIZE - 2) /\
1263 sizeof(struct sit_journal_entry))
1264 #define SIT_JOURNAL_RESERVED ((SUM_JOURNAL_SIZE - 2) %\
1265 sizeof(struct sit_journal_entry))
1268 * Reserved area should make size of f2fs_extra_info equals to
1269 * that of nat_journal and sit_journal.
1271 #define EXTRA_INFO_RESERVED (SUM_JOURNAL_SIZE - 2 - 8)
1274 * frequently updated NAT/SIT entries can be stored in the spare area in
1282 struct nat_journal_entry {
1284 struct f2fs_nat_entry ne;
1285 } __attribute__((packed));
1287 static_assert(sizeof(struct nat_journal_entry) == 13, "");
1289 struct nat_journal {
1290 struct nat_journal_entry entries[NAT_JOURNAL_ENTRIES];
1291 __u8 reserved[NAT_JOURNAL_RESERVED];
1294 static_assert(sizeof(struct nat_journal) == 505, "");
1296 struct sit_journal_entry {
1298 struct f2fs_sit_entry se;
1299 } __attribute__((packed));
1301 static_assert(sizeof(struct sit_journal_entry) == 78, "");
1303 struct sit_journal {
1304 struct sit_journal_entry entries[SIT_JOURNAL_ENTRIES];
1305 __u8 reserved[SIT_JOURNAL_RESERVED];
1308 static_assert(sizeof(struct sit_journal) == 505, "");
1310 struct f2fs_extra_info {
1311 __le64 kbytes_written;
1312 __u8 reserved[EXTRA_INFO_RESERVED];
1313 } __attribute__((packed));
1315 static_assert(sizeof(struct f2fs_extra_info) == 505, "");
1317 struct f2fs_journal {
1322 /* spare area is used by NAT or SIT journals or extra info */
1324 struct nat_journal nat_j;
1325 struct sit_journal sit_j;
1326 struct f2fs_extra_info info;
1328 } __attribute__((packed));
1330 static_assert(sizeof(struct f2fs_journal) == 507, "");
1332 /* 4KB-sized summary block structure */
1333 struct f2fs_summary_block {
1334 struct f2fs_summary entries[ENTRIES_IN_SUM];
1335 struct f2fs_journal journal;
1336 struct summary_footer footer;
1339 static_assert(sizeof(struct f2fs_summary_block) == 4096, "");
1342 * For directory operations
1344 #define F2FS_DOT_HASH 0
1345 #define F2FS_DDOT_HASH F2FS_DOT_HASH
1346 #define F2FS_MAX_HASH (~((0x3ULL) << 62))
1347 #define F2FS_HASH_COL_BIT ((0x1ULL) << 63)
1349 typedef __le32 f2fs_hash_t;
1351 /* One directory entry slot covers 8bytes-long file name */
1352 #define F2FS_SLOT_LEN 8
1353 #define F2FS_SLOT_LEN_BITS 3
1355 #define GET_DENTRY_SLOTS(x) ((x + F2FS_SLOT_LEN - 1) >> F2FS_SLOT_LEN_BITS)
1357 /* the number of dentry in a block */
1358 #define NR_DENTRY_IN_BLOCK 214
1360 /* MAX level for dir lookup */
1361 #define MAX_DIR_HASH_DEPTH 63
1363 /* MAX buckets in one level of dir */
1364 #define MAX_DIR_BUCKETS (1 << ((MAX_DIR_HASH_DEPTH / 2) - 1))
1366 #define SIZE_OF_DIR_ENTRY 11 /* by byte */
1367 #define SIZE_OF_DENTRY_BITMAP ((NR_DENTRY_IN_BLOCK + BITS_PER_BYTE - 1) / \
1369 #define SIZE_OF_RESERVED (F2FS_BLKSIZE - ((SIZE_OF_DIR_ENTRY + \
1371 NR_DENTRY_IN_BLOCK + SIZE_OF_DENTRY_BITMAP))
1372 #define MIN_INLINE_DENTRY_SIZE 40 /* just include '.' and '..' entries */
1374 /* One directory entry slot representing F2FS_SLOT_LEN-sized file name */
1375 struct f2fs_dir_entry {
1376 __le32 hash_code; /* hash code of file name */
1377 __le32 ino; /* inode number */
1378 __le16 name_len; /* lengh of file name */
1379 __u8 file_type; /* file type */
1380 } __attribute__((packed));
1382 static_assert(sizeof(struct f2fs_dir_entry) == 11, "");
1384 /* 4KB-sized directory entry block */
1385 struct f2fs_dentry_block {
1386 /* validity bitmap for directory entries in each block */
1387 __u8 dentry_bitmap[SIZE_OF_DENTRY_BITMAP];
1388 __u8 reserved[SIZE_OF_RESERVED];
1389 struct f2fs_dir_entry dentry[NR_DENTRY_IN_BLOCK];
1390 __u8 filename[NR_DENTRY_IN_BLOCK][F2FS_SLOT_LEN];
1393 static_assert(sizeof(struct f2fs_dentry_block) == F2FS_BLKSIZE, "");
1395 /* for inline stuff */
1396 #define DEF_INLINE_RESERVED_SIZE 1
1398 /* for inline dir */
1399 #define NR_INLINE_DENTRY(node) (MAX_INLINE_DATA(node) * BITS_PER_BYTE / \
1400 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
1402 #define INLINE_DENTRY_BITMAP_SIZE(node) ((NR_INLINE_DENTRY(node) + \
1403 BITS_PER_BYTE - 1) / BITS_PER_BYTE)
1404 #define INLINE_RESERVED_SIZE(node) (MAX_INLINE_DATA(node) - \
1405 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
1406 NR_INLINE_DENTRY(node) + \
1407 INLINE_DENTRY_BITMAP_SIZE(node)))
1409 /* file types used in inode_info->flags */
1420 /* added for fsck */
1423 F2FS_FT_LAST_FILE_TYPE = F2FS_FT_XATTR,
1426 #define LINUX_S_IFMT 00170000
1427 #define LINUX_S_IFREG 0100000
1428 #define LINUX_S_ISREG(m) (((m) & LINUX_S_IFMT) == LINUX_S_IFREG)
1430 /* from f2fs/segment.h */
1436 extern int utf8_to_utf16(char *, const char *, size_t, size_t);
1437 extern int utf16_to_utf8(char *, const char *, size_t, size_t);
1438 extern int log_base_2(uint32_t);
1439 extern unsigned int addrs_per_inode(struct f2fs_inode *);
1440 extern unsigned int addrs_per_block(struct f2fs_inode *);
1441 extern unsigned int f2fs_max_file_offset(struct f2fs_inode *);
1442 extern __u32 f2fs_inode_chksum(struct f2fs_node *);
1443 extern __u32 f2fs_checkpoint_chksum(struct f2fs_checkpoint *);
1444 extern int write_inode(struct f2fs_node *, u64);
1446 extern int get_bits_in_byte(unsigned char n);
1447 extern int test_and_set_bit_le(u32, u8 *);
1448 extern int test_and_clear_bit_le(u32, u8 *);
1449 extern int test_bit_le(u32, const u8 *);
1450 extern int f2fs_test_bit(unsigned int, const char *);
1451 extern int f2fs_set_bit(unsigned int, char *);
1452 extern int f2fs_clear_bit(unsigned int, char *);
1453 extern u64 find_next_bit_le(const u8 *, u64, u64);
1454 extern u64 find_next_zero_bit_le(const u8 *, u64, u64);
1456 extern uint32_t f2fs_cal_crc32(uint32_t, void *, int);
1457 extern int f2fs_crc_valid(uint32_t blk_crc, void *buf, int len);
1459 extern void f2fs_init_configuration(void);
1460 extern int f2fs_devs_are_umounted(void);
1461 extern int f2fs_dev_is_writable(void);
1462 extern int f2fs_dev_is_umounted(char *);
1463 extern int f2fs_get_device_info(void);
1464 extern int f2fs_get_f2fs_info(void);
1465 extern unsigned int calc_extra_isize(void);
1466 extern int get_device_info(int);
1467 extern int f2fs_init_sparse_file(void);
1468 extern void f2fs_release_sparse_resource(void);
1469 extern int f2fs_finalize_device(void);
1470 extern int f2fs_fsync_device(void);
1472 extern void dcache_init(void);
1473 extern void dcache_release(void);
1475 extern int dev_read(void *, __u64, size_t);
1476 #ifdef POSIX_FADV_WILLNEED
1477 extern int dev_readahead(__u64, size_t);
1479 extern int dev_readahead(__u64, size_t UNUSED(len));
1481 extern int dev_write(void *, __u64, size_t);
1482 extern int dev_write_block(void *, __u64);
1483 extern int dev_write_dump(void *, __u64, size_t);
1484 /* All bytes in the buffer must be 0 use dev_fill(). */
1485 extern int dev_fill(void *, __u64, size_t);
1486 extern int dev_fill_block(void *, __u64);
1488 extern int dev_read_block(void *, __u64);
1489 extern int dev_reada_block(__u64);
1491 extern int dev_read_version(void *, __u64, size_t);
1492 extern void get_kernel_version(__u8 *);
1493 extern void get_kernel_uname_version(__u8 *);
1494 f2fs_hash_t f2fs_dentry_hash(int, int, const unsigned char *, int);
1496 static inline bool f2fs_has_extra_isize(struct f2fs_inode *inode)
1498 return (inode->i_inline & F2FS_EXTRA_ATTR);
1501 static inline int __get_extra_isize(struct f2fs_inode *inode)
1503 if (f2fs_has_extra_isize(inode))
1504 return le16_to_cpu(inode->i_extra_isize) / sizeof(__le32);
1508 extern struct f2fs_configuration c;
1509 static inline int get_inline_xattr_addrs(struct f2fs_inode *inode)
1511 if (c.feature & cpu_to_le32(F2FS_FEATURE_FLEXIBLE_INLINE_XATTR))
1512 return le16_to_cpu(inode->i_inline_xattr_size);
1513 else if (inode->i_inline & F2FS_INLINE_XATTR ||
1514 inode->i_inline & F2FS_INLINE_DENTRY)
1515 return DEFAULT_INLINE_XATTR_ADDRS;
1520 #define get_extra_isize(node) __get_extra_isize(&node->i)
1522 #define F2FS_ZONED_NONE 0
1523 #define F2FS_ZONED_HA 1
1524 #define F2FS_ZONED_HM 2
1526 #ifdef HAVE_LINUX_BLKZONED_H
1528 /* Let's just use v2, since v1 should be compatible with v2 */
1529 #define BLK_ZONE_REP_CAPACITY (1 << 0)
1530 struct blk_zone_v2 {
1531 __u64 start; /* Zone start sector */
1532 __u64 len; /* Zone length in number of sectors */
1533 __u64 wp; /* Zone write pointer position */
1534 __u8 type; /* Zone type */
1535 __u8 cond; /* Zone condition */
1536 __u8 non_seq; /* Non-sequential write resources active */
1537 __u8 reset; /* Reset write pointer recommended */
1539 __u64 capacity; /* Zone capacity in number of sectors */
1542 #define blk_zone blk_zone_v2
1544 struct blk_zone_report_v2 {
1548 struct blk_zone zones[0];
1550 #define blk_zone_report blk_zone_report_v2
1552 #define blk_zone_type(z) (z)->type
1553 #define blk_zone_conv(z) ((z)->type == BLK_ZONE_TYPE_CONVENTIONAL)
1554 #define blk_zone_seq_req(z) ((z)->type == BLK_ZONE_TYPE_SEQWRITE_REQ)
1555 #define blk_zone_seq_pref(z) ((z)->type == BLK_ZONE_TYPE_SEQWRITE_PREF)
1556 #define blk_zone_seq(z) (blk_zone_seq_req(z) || blk_zone_seq_pref(z))
1558 static inline const char *
1559 blk_zone_type_str(struct blk_zone *blkz)
1561 switch (blk_zone_type(blkz)) {
1562 case BLK_ZONE_TYPE_CONVENTIONAL:
1563 return( "Conventional" );
1564 case BLK_ZONE_TYPE_SEQWRITE_REQ:
1565 return( "Sequential-write-required" );
1566 case BLK_ZONE_TYPE_SEQWRITE_PREF:
1567 return( "Sequential-write-preferred" );
1569 return( "Unknown-type" );
1572 #define blk_zone_cond(z) (z)->cond
1574 static inline const char *
1575 blk_zone_cond_str(struct blk_zone *blkz)
1577 switch (blk_zone_cond(blkz)) {
1578 case BLK_ZONE_COND_NOT_WP:
1579 return "Not-write-pointer";
1580 case BLK_ZONE_COND_EMPTY:
1582 case BLK_ZONE_COND_IMP_OPEN:
1583 return "Implicit-open";
1584 case BLK_ZONE_COND_EXP_OPEN:
1585 return "Explicit-open";
1586 case BLK_ZONE_COND_CLOSED:
1588 case BLK_ZONE_COND_READONLY:
1590 case BLK_ZONE_COND_FULL:
1592 case BLK_ZONE_COND_OFFLINE:
1595 return "Unknown-cond";
1599 * Handle kernel zone capacity support
1601 #define blk_zone_empty(z) (blk_zone_cond(z) == BLK_ZONE_COND_EMPTY)
1602 #define blk_zone_sector(z) (z)->start
1603 #define blk_zone_length(z) (z)->len
1604 #define blk_zone_wp_sector(z) (z)->wp
1605 #define blk_zone_need_reset(z) (int)(z)->reset
1606 #define blk_zone_non_seq(z) (int)(z)->non_seq
1607 #define blk_zone_capacity(z, f) ((f & BLK_ZONE_REP_CAPACITY) ? \
1608 (z)->capacity : (z)->len)
1614 extern int f2fs_get_zoned_model(int);
1615 extern int f2fs_get_zone_blocks(int);
1616 extern int f2fs_report_zone(int, uint64_t, struct blk_zone *);
1617 typedef int (report_zones_cb_t)(int i, void *, void *);
1618 extern int f2fs_report_zones(int, report_zones_cb_t *, void *);
1619 extern int f2fs_check_zones(int);
1620 int f2fs_reset_zone(int, void *);
1621 extern int f2fs_reset_zones(int);
1622 extern uint32_t f2fs_get_usable_segments(struct f2fs_super_block *sb);
1624 #define SIZE_ALIGN(val, size) (((val) + (size) - 1) / (size))
1625 #define SEG_ALIGN(blks) SIZE_ALIGN(blks, c.blks_per_seg)
1626 #define ZONE_ALIGN(blks) SIZE_ALIGN(blks, c.blks_per_seg * \
1629 static inline double get_reserved(struct f2fs_super_block *sb, double ovp)
1632 uint32_t usable_main_segs = f2fs_get_usable_segments(sb);
1633 uint32_t segs_per_sec = round_up(usable_main_segs, get_sb(section_count));
1635 if (c.conf_reserved_sections)
1636 reserved = c.conf_reserved_sections * segs_per_sec;
1638 reserved = (100 / ovp + 1 + NR_CURSEG_TYPE) * segs_per_sec;
1643 static inline double get_best_overprovision(struct f2fs_super_block *sb)
1645 double reserved, ovp, candidate, end, diff, space;
1646 double max_ovp = 0, max_space = 0;
1647 uint32_t usable_main_segs = f2fs_get_usable_segments(sb);
1649 if (get_sb(segment_count_main) < 256) {
1659 for (; candidate <= end; candidate += diff) {
1660 reserved = get_reserved(sb, candidate);
1661 ovp = (usable_main_segs - reserved) * candidate / 100;
1664 space = usable_main_segs - max(reserved, ovp) -
1665 2 * get_sb(segs_per_sec);
1666 if (max_space < space) {
1668 max_ovp = candidate;
1674 static inline __le64 get_cp_crc(struct f2fs_checkpoint *cp)
1676 uint64_t cp_ver = get_cp(checkpoint_ver);
1677 size_t crc_offset = get_cp(checksum_offset);
1678 uint32_t crc = le32_to_cpu(*(__le32 *)((unsigned char *)cp +
1681 cp_ver |= ((uint64_t)crc << 32);
1682 return cpu_to_le64(cp_ver);
1685 static inline int exist_qf_ino(struct f2fs_super_block *sb)
1689 for (i = 0; i < F2FS_MAX_QUOTAS; i++)
1695 static inline int is_qf_ino(struct f2fs_super_block *sb, nid_t ino)
1699 for (i = 0; i < F2FS_MAX_QUOTAS; i++)
1700 if (sb->qf_ino[i] == ino)
1705 static inline void show_version(const char *prog)
1707 #if defined(F2FS_TOOLS_VERSION) && defined(F2FS_TOOLS_DATE)
1708 MSG(0, "%s %s (%s)\n", prog, F2FS_TOOLS_VERSION, F2FS_TOOLS_DATE);
1710 MSG(0, "%s -- version not supported\n", prog);
1714 static inline void f2fs_init_qf_inode(struct f2fs_super_block *sb,
1715 struct f2fs_node *raw_node, int qtype, time_t mtime)
1717 raw_node->footer.nid = sb->qf_ino[qtype];
1718 raw_node->footer.ino = sb->qf_ino[qtype];
1719 raw_node->footer.cp_ver = cpu_to_le64(1);
1720 raw_node->i.i_mode = cpu_to_le16(0x8180);
1721 raw_node->i.i_links = cpu_to_le32(1);
1722 raw_node->i.i_uid = cpu_to_le32(c.root_uid);
1723 raw_node->i.i_gid = cpu_to_le32(c.root_gid);
1725 raw_node->i.i_size = cpu_to_le64(1024 * 6); /* Hard coded */
1726 raw_node->i.i_blocks = cpu_to_le64(1);
1728 raw_node->i.i_atime = cpu_to_le32(mtime);
1729 raw_node->i.i_atime_nsec = 0;
1730 raw_node->i.i_ctime = cpu_to_le32(mtime);
1731 raw_node->i.i_ctime_nsec = 0;
1732 raw_node->i.i_mtime = cpu_to_le32(mtime);
1733 raw_node->i.i_mtime_nsec = 0;
1734 raw_node->i.i_generation = 0;
1735 raw_node->i.i_xattr_nid = 0;
1736 raw_node->i.i_flags = FS_IMMUTABLE_FL;
1737 raw_node->i.i_current_depth = cpu_to_le32(0);
1738 raw_node->i.i_dir_level = DEF_DIR_LEVEL;
1740 if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
1741 raw_node->i.i_inline = F2FS_EXTRA_ATTR;
1742 raw_node->i.i_extra_isize = cpu_to_le16(calc_extra_isize());
1745 if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA))
1746 raw_node->i.i_projid = cpu_to_le32(F2FS_DEF_PROJID);
1748 raw_node->i.i_ext.fofs = 0;
1749 raw_node->i.i_ext.blk_addr = 0;
1750 raw_node->i.i_ext.len = 0;
1758 #define INIT_FEATURE_TABLE \
1759 struct feature feature_table[] = { \
1760 { "encrypt", F2FS_FEATURE_ENCRYPT }, \
1761 { "extra_attr", F2FS_FEATURE_EXTRA_ATTR }, \
1762 { "project_quota", F2FS_FEATURE_PRJQUOTA }, \
1763 { "inode_checksum", F2FS_FEATURE_INODE_CHKSUM }, \
1764 { "flexible_inline_xattr", F2FS_FEATURE_FLEXIBLE_INLINE_XATTR },\
1765 { "quota", F2FS_FEATURE_QUOTA_INO }, \
1766 { "inode_crtime", F2FS_FEATURE_INODE_CRTIME }, \
1767 { "lost_found", F2FS_FEATURE_LOST_FOUND }, \
1768 { "verity", F2FS_FEATURE_VERITY }, /* reserved */ \
1769 { "sb_checksum", F2FS_FEATURE_SB_CHKSUM }, \
1770 { "casefold", F2FS_FEATURE_CASEFOLD }, \
1771 { "compression", F2FS_FEATURE_COMPRESSION }, \
1772 { "ro", F2FS_FEATURE_RO}, \
1776 static inline u32 feature_map(struct feature *table, char *feature)
1779 for (p = table; p->name && strcmp(p->name, feature); p++)
1784 static inline int set_feature_bits(struct feature *table, char *features)
1786 u32 mask = feature_map(table, features);
1788 c.feature |= cpu_to_le32(mask);
1790 MSG(0, "Error: Wrong features %s\n", features);
1796 static inline int parse_feature(struct feature *table, const char *features)
1798 char *buf, *sub, *next;
1800 buf = strdup(features);
1804 for (sub = buf; sub && *sub; sub = next ? next + 1 : NULL) {
1805 /* Skip the beginning blanks */
1806 while (*sub && *sub == ' ')
1809 /* Skip a feature word */
1810 while (*next && *next != ' ' && *next != ',')
1818 if (set_feature_bits(table, sub)) {
1827 static inline int parse_root_owner(char *ids,
1828 uint32_t *root_uid, uint32_t *root_gid)
1835 for (i = 0; i < strlen(ids) - 1; i++)
1836 if (*(ids + i) == ':')
1841 *root_uid = atoi(uid);
1842 *root_gid = atoi(gid);
1849 struct f2fs_nls_table {
1851 const struct f2fs_nls_ops *ops;
1854 struct f2fs_nls_ops {
1855 int (*casefold)(const struct f2fs_nls_table *charset,
1856 const unsigned char *str, size_t len,
1857 unsigned char *dest, size_t dlen);
1860 extern const struct f2fs_nls_table *f2fs_load_nls_table(int encoding);
1861 #define F2FS_ENC_UTF8_12_0 1
1863 extern int f2fs_str2encoding(const char *string);
1864 extern char *f2fs_encoding2str(const int encoding);
1865 extern int f2fs_get_encoding_flags(int encoding);
1866 extern int f2fs_str2encoding_flags(char **param, __u16 *flags);
1868 #endif /*__F2FS_FS_H */