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