• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * f2fs_fs.h
3  *
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
11  *
12  * Dual licensed under the GPL or LGPL version 2 licenses.
13  *
14  * The byteswap codes are copied from:
15  *   samba_3_master/lib/ccan/endian/endian.h under LGPL 2.1
16  */
17 #ifndef __F2FS_FS_H__
18 #define __F2FS_FS_H__
19 
20 #ifndef __SANE_USERSPACE_TYPES__
21 #define __SANE_USERSPACE_TYPES__       /* For PPC64, to get LL64 types */
22 #endif
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stddef.h>
27 #include <string.h>
28 #include <time.h>
29 
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #else
33 #ifdef __ANDROID__
34 #define WITH_ANDROID
35 #endif
36 #endif /* HAVE_CONFIG_H */
37 
38 #ifdef WITH_ANDROID
39 #include <android_config.h>
40 #else
41 #define WITH_DUMP
42 #define WITH_DEFRAG
43 #define WITH_RESIZE
44 #define WITH_SLOAD
45 #define WITH_LABEL
46 #endif
47 
48 #include <inttypes.h>
49 #ifdef HAVE_LINUX_TYPES_H
50 #include <linux/types.h>
51 #endif
52 #include <sys/types.h>
53 
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>
58 #endif
59 
60 #ifdef HAVE_LIBSELINUX
61 #include <selinux/selinux.h>
62 #include <selinux/label.h>
63 #endif
64 
65 #ifdef UNUSED
66 #elif defined(__GNUC__)
67 # define UNUSED(x) UNUSED_ ## x __attribute__((unused))
68 #elif defined(__LCLINT__)
69 # define UNUSED(x) x
70 #elif defined(__cplusplus)
71 # define UNUSED(x)
72 #else
73 # define UNUSED(x) x
74 #endif
75 
76 #ifndef static_assert
77 #define static_assert _Static_assert
78 #endif
79 
80 #ifdef HAVE_SYS_MOUNT_H
81 #include <sys/mount.h>
82 #endif
83 
84 #ifndef fallthrough
85 #ifdef __clang__
86 #define fallthrough do {} while (0) /* fall through */
87 #else
88 #define fallthrough __attribute__((__fallthrough__))
89 #endif
90 #endif
91 
92 #ifdef _WIN32
93 #undef HAVE_LINUX_TYPES_H
94 #endif
95 
96 /* codes from kernel's f2fs.h, GPL-v2.0 */
97 #define MIN_COMPRESS_LOG_SIZE	2
98 #define MAX_COMPRESS_LOG_SIZE	8
99 
100 typedef uint64_t	u64;
101 typedef uint32_t	u32;
102 typedef uint16_t	u16;
103 typedef uint8_t		u8;
104 typedef u32		block_t;
105 typedef u32		nid_t;
106 #ifndef bool
107 typedef u8		bool;
108 #endif
109 typedef unsigned long	pgoff_t;
110 typedef unsigned short	umode_t;
111 
112 #ifndef HAVE_LINUX_TYPES_H
113 typedef u8	__u8;
114 typedef u16	__u16;
115 typedef u32	__u32;
116 typedef u64	__u64;
117 typedef u16	__le16;
118 typedef u32	__le32;
119 typedef u64	__le64;
120 typedef u16	__be16;
121 typedef u32	__be32;
122 typedef u64	__be64;
123 #endif
124 
125 /*
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
130  */
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 */
137 };
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 */
148 };
149 
150 #if HAVE_BYTESWAP_H
151 #include <byteswap.h>
152 #else
153 /**
154  * bswap_16 - reverse bytes in a uint16_t value.
155  * @val: value whose bytes to swap.
156  *
157  * Example:
158  *	// Output contains "1024 is 4 as two bytes reversed"
159  *	printf("1024 is %u as two bytes reversed\n", bswap_16(1024));
160  */
bswap_16(uint16_t val)161 static inline uint16_t bswap_16(uint16_t val)
162 {
163 	return ((val & (uint16_t)0x00ffU) << 8)
164 		| ((val & (uint16_t)0xff00U) >> 8);
165 }
166 
167 /**
168  * bswap_32 - reverse bytes in a uint32_t value.
169  * @val: value whose bytes to swap.
170  *
171  * Example:
172  *	// Output contains "1024 is 262144 as four bytes reversed"
173  *	printf("1024 is %u as four bytes reversed\n", bswap_32(1024));
174  */
bswap_32(uint32_t val)175 static inline uint32_t bswap_32(uint32_t val)
176 {
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);
181 }
182 #endif /* !HAVE_BYTESWAP_H */
183 
184 #if defined HAVE_DECL_BSWAP_64 && !HAVE_DECL_BSWAP_64
185 /**
186  * bswap_64 - reverse bytes in a uint64_t value.
187  * @val: value whose bytes to swap.
188  *
189  * Example:
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));
193  */
bswap_64(uint64_t val)194 static inline uint64_t bswap_64(uint64_t val)
195 {
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);
204 }
205 #endif
206 
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))
223 #endif
224 
225 #define typecheck(type,x) \
226 	({	type __dummy; \
227 		typeof(x) __dummy2; \
228 		(void)(&__dummy == &__dummy2); \
229 		1; \
230 	 })
231 
232 #define NULL_SEGNO	((unsigned int)~0)
233 
234 /*
235  * Debugging interfaces
236  */
237 #define FIX_MSG(fmt, ...)						\
238 	do {								\
239 		printf("[FIX] (%s:%4d) ", __func__, __LINE__);		\
240 		printf(" --> "fmt"\n", ##__VA_ARGS__);			\
241 	} while (0)
242 
243 #define ASSERT_MSG(fmt, ...)						\
244 	do {								\
245 		printf("[ASSERT] (%s:%4d) ", __func__, __LINE__);	\
246 		printf(" --> "fmt"\n", ##__VA_ARGS__);			\
247 		c.bug_on = 1;						\
248 	} while (0)
249 
250 #define ASSERT(exp)							\
251 	do {								\
252 		if (!(exp)) {						\
253 			printf("[ASSERT] (%s:%4d) %s\n",		\
254 					__func__, __LINE__, #exp);	\
255 			exit(-1);					\
256 		}							\
257 	} while (0)
258 
259 #define ERR_MSG(fmt, ...)						\
260 	do {								\
261 		printf("[%s:%d] " fmt, __func__, __LINE__, ##__VA_ARGS__); \
262 	} while (0)
263 
264 #define MSG(n, fmt, ...)						\
265 	do {								\
266 		if (c.dbg_lv >= n && !c.layout && !c.show_file_map) {	\
267 			printf(fmt, ##__VA_ARGS__);			\
268 		}							\
269 	} while (0)
270 
271 #define DBG(n, fmt, ...)						\
272 	do {								\
273 		if (c.dbg_lv >= n && !c.layout && !c.show_file_map) {	\
274 			printf("[%s:%4d] " fmt,				\
275 				__func__, __LINE__, ##__VA_ARGS__);	\
276 		}							\
277 	} while (0)
278 
279 /* Display on console */
280 #define DISP(fmt, ptr, member)				\
281 	do {						\
282 		printf("%-30s" fmt, #member, ((ptr)->member));	\
283 	} while (0)
284 
285 #define DISP_u16(ptr, member)						\
286 	do {								\
287 		assert(sizeof((ptr)->member) == 2);			\
288 		if (c.layout)						\
289 			printf("%-30s %u\n",				\
290 			#member":", le16_to_cpu(((ptr)->member)));	\
291 		else							\
292 			printf("%-30s" "\t\t[0x%8x : %u]\n",		\
293 			#member, le16_to_cpu(((ptr)->member)),		\
294 			le16_to_cpu(((ptr)->member)));			\
295 	} while (0)
296 
297 #define DISP_u32(ptr, member)						\
298 	do {								\
299 		assert(sizeof((ptr)->member) <= 4);			\
300 		if (c.layout)						\
301 			printf("%-30s %u\n",				\
302 			#member":", le32_to_cpu(((ptr)->member)));	\
303 		else							\
304 			printf("%-30s" "\t\t[0x%8x : %u]\n",		\
305 			#member, le32_to_cpu(((ptr)->member)),		\
306 			le32_to_cpu(((ptr)->member)));			\
307 	} while (0)
308 
309 #define DISP_u64(ptr, member)						\
310 	do {								\
311 		assert(sizeof((ptr)->member) == 8);			\
312 		if (c.layout)						\
313 			printf("%-30s %" PRIu64 "\n",			\
314 			#member":", le64_to_cpu(((ptr)->member)));	\
315 		else							\
316 			printf("%-30s" "\t\t[0x%8" PRIx64 " : %" PRIu64 "]\n",	\
317 			#member, le64_to_cpu(((ptr)->member)),		\
318 			le64_to_cpu(((ptr)->member)));			\
319 	} while (0)
320 
321 #define DISP_utf(ptr, member)						\
322 	do {								\
323 		if (c.layout)						\
324 			printf("%-30s %s\n", #member":",		\
325 					((ptr)->member)); 		\
326 		else							\
327 			printf("%-30s" "\t\t[%s]\n", #member,		\
328 					((ptr)->member));		\
329 	} while (0)
330 
331 /* Display to buffer */
332 #define BUF_DISP_u32(buf, data, len, ptr, member)			\
333 	do {								\
334 		assert(sizeof((ptr)->member) <= 4);			\
335 		snprintf(buf, len, #member);				\
336 		snprintf(data, len, "0x%x : %u", ((ptr)->member),	\
337 						((ptr)->member));	\
338 	} while (0)
339 
340 #define BUF_DISP_u64(buf, data, len, ptr, member)			\
341 	do {								\
342 		assert(sizeof((ptr)->member) == 8);			\
343 		snprintf(buf, len, #member);				\
344 		snprintf(data, len, "0x%llx : %llu", ((ptr)->member),	\
345 						((ptr)->member));	\
346 	} while (0)
347 
348 #define BUF_DISP_utf(buf, data, len, ptr, member)			\
349 		snprintf(buf, len, #member)
350 
351 /* these are defined in kernel */
352 #define BITS_PER_BYTE		8
353 #ifndef SECTOR_SHIFT
354 #define SECTOR_SHIFT		9
355 #endif
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
361 
362 #define F2FS_BYTES_TO_BLK(bytes)    ((bytes) >> F2FS_BLKSIZE_BITS)
363 #define F2FS_BLKSIZE_BITS 12
364 
365 /* for mkfs */
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
371 
372 #define VERSION_LEN		256
373 #define VERSION_TIMESTAMP_LEN	4
374 #define VERSION_NAME_LEN	(VERSION_LEN - VERSION_TIMESTAMP_LEN)
375 
376 #define LPF "lost+found"
377 
378 enum f2fs_config_func {
379 	MKFS,
380 	FSCK,
381 	DUMP,
382 	DEFRAG,
383 	RESIZE,
384 	SLOAD,
385 	LABEL,
386 };
387 
388 enum default_set {
389 	CONF_NONE = 0,
390 	CONF_ANDROID,
391 };
392 
393 struct device_info {
394 	char *path;
395 	int32_t fd;
396 	uint32_t sector_size;
397 	uint64_t total_sectors;	/* got by get_device_info */
398 	uint64_t start_blkaddr;
399 	uint64_t end_blkaddr;
400 	uint32_t total_segments;
401 
402 	/* to handle zone block devices */
403 	int zoned_model;
404 	uint32_t nr_zones;
405 	uint32_t nr_rnd_zones;
406 	size_t zone_blocks;
407 	uint64_t zone_size;
408 	size_t *zone_cap_blocks;
409 };
410 
411 typedef struct {
412 	/* Value 0 means no cache, minimum 1024 */
413 	long num_cache_entry;
414 
415 	/* Value 0 means always overwrite (no collision allowed). maximum 16 */
416 	unsigned max_hash_collision;
417 
418 	bool dbg_en;
419 } dev_cache_config_t;
420 
421 /* f2fs_configration for compression used for sload.f2fs */
422 typedef struct  {
423 	void (*init)(struct compress_ctx *cc);
424 	int (*compress)(struct compress_ctx *cc);
425 	void (*reset)(struct compress_ctx *cc);
426 } compress_ops;
427 
428 /* Should be aligned to supported_comp_names and support_comp_ops */
429 enum compress_algorithms {
430 	COMPR_LZO,
431 	COMPR_LZ4,
432 	MAX_COMPRESS_ALGS,
433 };
434 
435 enum filter_policy {
436 	COMPR_FILTER_UNASSIGNED = 0,
437 	COMPR_FILTER_ALLOW,
438 	COMPR_FILTER_DENY,
439 };
440 
441 typedef struct {
442 	void (*add)(const char *);
443 	void (*destroy)(void);
444 	bool (*filter)(const char *);
445 } filter_ops;
446 
447 typedef struct {
448 	bool enabled;			/* disabled by default */
449 	bool required;			/* require to enable */
450 	bool readonly;			/* readonly to release blocks */
451 	struct compress_ctx cc;		/* work context */
452 	enum compress_algorithms alg;	/* algorithm to compress */
453 	compress_ops *ops;		/* ops per algorithm */
454 	unsigned int min_blocks;	/* save more blocks than this */
455 	enum filter_policy filter;	/* filter to try compression */
456 	filter_ops *filter_ops;		/* filter ops */
457 } compress_config_t;
458 
459 #define ALIGN_DOWN(addrs, size)	(((addrs) / (size)) * (size))
460 #define ALIGN_UP(addrs, size)	ALIGN_DOWN(((addrs) + (size) - 1), (size))
461 
462 struct f2fs_configuration {
463 	uint32_t reserved_segments;
464 	uint32_t new_reserved_segments;
465 	int sparse_mode;
466 	int zoned_mode;
467 	int zoned_model;
468 	size_t zone_blocks;
469 	double overprovision;
470 	double new_overprovision;
471 	uint32_t cur_seg[6];
472 	uint32_t segs_per_sec;
473 	uint32_t secs_per_zone;
474 	uint32_t segs_per_zone;
475 	uint32_t start_sector;
476 	uint32_t total_segments;
477 	uint32_t sector_size;
478 	uint64_t device_size;
479 	uint64_t total_sectors;
480 	uint64_t wanted_total_sectors;
481 	uint64_t wanted_sector_size;
482 	uint64_t target_sectors;
483 	uint32_t sectors_per_blk;
484 	uint32_t blks_per_seg;
485 	__u8 init_version[VERSION_LEN + 1];
486 	__u8 sb_version[VERSION_LEN + 1];
487 	__u8 version[VERSION_LEN + 1];
488 	char *vol_label;
489 	char *vol_uuid;
490 	uint16_t s_encoding;
491 	uint16_t s_encoding_flags;
492 	int heap;
493 	int32_t kd;
494 	int32_t dump_fd;
495 	struct device_info devices[MAX_DEVICES];
496 	int ndevs;
497 	char *extension_list[2];
498 	const char *rootdev_name;
499 	int dbg_lv;
500 	int show_dentry;
501 	int trim;
502 	int trimmed;
503 	int func;
504 	void *private;
505 	int dry_run;
506 	int no_kernel_check;
507 	int fix_on;
508 	int force;
509 	int defset;
510 	int bug_on;
511 	int force_stop;
512 	int abnormal_stop;
513 	int fs_errors;
514 	int bug_nat_bits;
515 	bool quota_fixed;
516 	int alloc_failed;
517 	int auto_fix;
518 	int layout;
519 	int show_file_map;
520 	u64 show_file_map_max_offset;
521 	int quota_fix;
522 	int preen_mode;
523 	int ro;
524 	int preserve_limits;		/* preserve quota limits */
525 	int large_nat_bitmap;
526 	int fix_chksum;			/* fix old cp.chksum position */
527 	__le32 feature;			/* defined features */
528 	unsigned int quota_bits;	/* quota bits */
529 	time_t fixed_time;
530 
531 	/* mkfs parameters */
532 	int fake_seed;
533 	uint32_t next_free_nid;
534 	uint32_t quota_inum;
535 	uint32_t quota_dnum;
536 	uint32_t lpf_inum;
537 	uint32_t lpf_dnum;
538 	uint32_t lpf_ino;
539 	uint32_t root_uid;
540 	uint32_t root_gid;
541 
542 	/* defragmentation parameters */
543 	int defrag_shrink;
544 	uint64_t defrag_start;
545 	uint64_t defrag_len;
546 	uint64_t defrag_target;
547 
548 	/* sload parameters */
549 	char *from_dir;
550 	char *mount_point;
551 	char *target_out_dir;
552 	char *fs_config_file;
553 #ifdef HAVE_LIBSELINUX
554 	struct selinux_opt seopt_file[8];
555 	int nr_opt;
556 #endif
557 	int preserve_perms;
558 
559 	/* resize parameters */
560 	int safe_resize;
561 
562 	/* precomputed fs UUID checksum for seeding other checksums */
563 	uint32_t chksum_seed;
564 
565 	/* cache parameters */
566 	dev_cache_config_t cache_config;
567 
568 	/* compression support for sload.f2fs */
569 	compress_config_t compress;
570 };
571 
572 #ifdef CONFIG_64BIT
573 #define BITS_PER_LONG	64
574 #else
575 #define BITS_PER_LONG	32
576 #endif
577 
578 #define BIT_MASK(nr)	(1 << (nr % BITS_PER_LONG))
579 #define BIT_WORD(nr)	(nr / BITS_PER_LONG)
580 
581 #define set_sb_le64(member, val)		(sb->member = cpu_to_le64(val))
582 #define set_sb_le32(member, val)		(sb->member = cpu_to_le32(val))
583 #define set_sb_le16(member, val)		(sb->member = cpu_to_le16(val))
584 #define get_sb_le64(member)			le64_to_cpu(sb->member)
585 #define get_sb_le32(member)			le32_to_cpu(sb->member)
586 #define get_sb_le16(member)			le16_to_cpu(sb->member)
587 #define get_newsb_le64(member)			le64_to_cpu(new_sb->member)
588 #define get_newsb_le32(member)			le32_to_cpu(new_sb->member)
589 #define get_newsb_le16(member)			le16_to_cpu(new_sb->member)
590 
591 #define set_sb(member, val)						\
592 			do {						\
593 				typeof(sb->member) t = (val);		\
594 				switch (sizeof(t)) {			\
595 				case 8: set_sb_le64(member, t); break;	\
596 				case 4: set_sb_le32(member, t); break;	\
597 				case 2: set_sb_le16(member, t); break;	\
598 				}					\
599 			} while(0)
600 
601 #define get_sb(member)		\
602 			({						\
603 				typeof(sb->member) t;			\
604 				switch (sizeof(t)) {			\
605 				case 8: t = get_sb_le64(member); break; \
606 				case 4: t = get_sb_le32(member); break; \
607 				case 2: t = get_sb_le16(member); break; \
608 				} 					\
609 				t; \
610 			})
611 #define get_newsb(member)		\
612 			({						\
613 				typeof(new_sb->member) t;		\
614 				switch (sizeof(t)) {			\
615 				case 8: t = get_newsb_le64(member); break; \
616 				case 4: t = get_newsb_le32(member); break; \
617 				case 2: t = get_newsb_le16(member); break; \
618 				} 					\
619 				t; \
620 			})
621 
622 #define set_cp_le64(member, val)		(cp->member = cpu_to_le64(val))
623 #define set_cp_le32(member, val)		(cp->member = cpu_to_le32(val))
624 #define set_cp_le16(member, val)		(cp->member = cpu_to_le16(val))
625 #define get_cp_le64(member)			le64_to_cpu(cp->member)
626 #define get_cp_le32(member)			le32_to_cpu(cp->member)
627 #define get_cp_le16(member)			le16_to_cpu(cp->member)
628 
629 #define set_cp(member, val)						\
630 			do {						\
631 				typeof(cp->member) t = (val);		\
632 				switch (sizeof(t)) {			\
633 				case 8: set_cp_le64(member, t); break;	\
634 				case 4: set_cp_le32(member, t); break;	\
635 				case 2: set_cp_le16(member, t); break;	\
636 				}					\
637 			} while(0)
638 
639 #define get_cp(member)		\
640 			({						\
641 				typeof(cp->member) t;			\
642 				switch (sizeof(t)) {			\
643 				case 8: t = get_cp_le64(member); break; \
644 				case 4: t = get_cp_le32(member); break; \
645 				case 2: t = get_cp_le16(member); break; \
646 				} 					\
647 				t; \
648 			})
649 
650 /*
651  * Copied from include/linux/kernel.h
652  */
653 #define __round_mask(x, y)	((__typeof__(x))((y)-1))
654 #define round_down(x, y)	((x) & ~__round_mask(x, y))
655 
656 #define min(x, y) ({				\
657 	typeof(x) _min1 = (x);			\
658 	typeof(y) _min2 = (y);			\
659 	(void) (&_min1 == &_min2);		\
660 	_min1 < _min2 ? _min1 : _min2; })
661 
662 #define max(x, y) ({				\
663 	typeof(x) _max1 = (x);			\
664 	typeof(y) _max2 = (y);			\
665 	(void) (&_max1 == &_max2);		\
666 	_max1 > _max2 ? _max1 : _max2; })
667 
668 #define round_up(x, y)		(((x) + (y) - 1) / (y))
669 /*
670  * Copied from fs/f2fs/f2fs.h
671  */
672 #define	NR_CURSEG_DATA_TYPE	(3)
673 #define NR_CURSEG_NODE_TYPE	(3)
674 #define NR_CURSEG_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
675 
676 enum {
677 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
678 	CURSEG_WARM_DATA,	/* data blocks */
679 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
680 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
681 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
682 	CURSEG_COLD_NODE,	/* indirect node blocks */
683 	NO_CHECK_TYPE
684 };
685 
686 #define F2FS_MIN_SEGMENTS	9 /* SB + 2 (CP + SIT + NAT) + SSA + MAIN */
687 
688 /*
689  * Copied from fs/f2fs/segment.h
690  */
691 #define GET_SUM_TYPE(footer) ((footer)->entry_type)
692 #define SET_SUM_TYPE(footer, type) ((footer)->entry_type = type)
693 
694 /*
695  * Copied from include/linux/f2fs_sb.h
696  */
697 #define F2FS_SUPER_OFFSET		1024	/* byte-size offset */
698 #define F2FS_MIN_LOG_SECTOR_SIZE	9	/* 9 bits for 512 bytes */
699 #define F2FS_MAX_LOG_SECTOR_SIZE	12	/* 12 bits for 4096 bytes */
700 #define F2FS_BLKSIZE			4096	/* support only 4KB block */
701 #define F2FS_MAX_EXTENSION		64	/* # of extension entries */
702 #define F2FS_EXTENSION_LEN		8	/* max size of extension */
703 #define F2FS_BLK_ALIGN(x)	(((x) + F2FS_BLKSIZE - 1) / F2FS_BLKSIZE)
704 
705 #define NULL_ADDR		0x0U
706 #define NEW_ADDR		-1U
707 #define COMPRESS_ADDR		-2U
708 
709 #define F2FS_ROOT_INO(sbi)	(sbi->root_ino_num)
710 #define F2FS_NODE_INO(sbi)	(sbi->node_ino_num)
711 #define F2FS_META_INO(sbi)	(sbi->meta_ino_num)
712 
713 #define F2FS_MAX_QUOTAS		3
714 #define QUOTA_DATA(i)		(2)
715 #define QUOTA_INO(sb,t)	(le32_to_cpu((sb)->qf_ino[t]))
716 
717 #define FS_IMMUTABLE_FL		0x00000010 /* Immutable file */
718 
719 #define F2FS_ENC_UTF8_12_1	1
720 #define F2FS_ENC_STRICT_MODE_FL	(1 << 0)
721 
722 /* This flag is used by node and meta inodes, and by recovery */
723 #define GFP_F2FS_ZERO	(GFP_NOFS | __GFP_ZERO)
724 
725 /*
726  * For further optimization on multi-head logs, on-disk layout supports maximum
727  * 16 logs by default. The number, 16, is expected to cover all the cases
728  * enoughly. The implementaion currently uses no more than 6 logs.
729  * Half the logs are used for nodes, and the other half are used for data.
730  */
731 #define MAX_ACTIVE_LOGS	16
732 #define MAX_ACTIVE_NODE_LOGS	8
733 #define MAX_ACTIVE_DATA_LOGS	8
734 
735 #define F2FS_FEATURE_ENCRYPT		0x0001
736 #define F2FS_FEATURE_BLKZONED		0x0002
737 #define F2FS_FEATURE_ATOMIC_WRITE	0x0004
738 #define F2FS_FEATURE_EXTRA_ATTR		0x0008
739 #define F2FS_FEATURE_PRJQUOTA		0x0010
740 #define F2FS_FEATURE_INODE_CHKSUM	0x0020
741 #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x0040
742 #define F2FS_FEATURE_QUOTA_INO		0x0080
743 #define F2FS_FEATURE_INODE_CRTIME	0x0100
744 #define F2FS_FEATURE_LOST_FOUND		0x0200
745 #define F2FS_FEATURE_VERITY		0x0400	/* reserved */
746 #define F2FS_FEATURE_SB_CHKSUM		0x0800
747 #define F2FS_FEATURE_CASEFOLD		0x1000
748 #define F2FS_FEATURE_COMPRESSION	0x2000
749 #define F2FS_FEATURE_RO			0x4000
750 
751 #define MAX_VOLUME_NAME		512
752 
753 /*
754  * For superblock
755  */
756 struct f2fs_device {
757 	__u8 path[MAX_PATH_LEN];
758 	__le32 total_segments;
759 };
760 
761 static_assert(sizeof(struct f2fs_device) == 68, "");
762 
763 /* reason of stop_checkpoint */
764 enum stop_cp_reason {
765 	STOP_CP_REASON_SHUTDOWN,
766 	STOP_CP_REASON_FAULT_INJECT,
767 	STOP_CP_REASON_META_PAGE,
768 	STOP_CP_REASON_WRITE_FAIL,
769 	STOP_CP_REASON_CORRUPTED_SUMMARY,
770 	STOP_CP_REASON_UPDATE_INODE,
771 	STOP_CP_REASON_FLUSH_FAIL,
772 	STOP_CP_REASON_MAX,
773 };
774 
775 #define	MAX_STOP_REASON			32
776 
777 /* detail reason for EFSCORRUPTED */
778 enum f2fs_error {
779 	ERROR_CORRUPTED_CLUSTER,
780 	ERROR_FAIL_DECOMPRESSION,
781 	ERROR_INVALID_BLKADDR,
782 	ERROR_CORRUPTED_DIRENT,
783 	ERROR_CORRUPTED_INODE,
784 	ERROR_INCONSISTENT_SUMMARY,
785 	ERROR_INCONSISTENT_FOOTER,
786 	ERROR_INCONSISTENT_SUM_TYPE,
787 	ERROR_CORRUPTED_JOURNAL,
788 	ERROR_INCONSISTENT_NODE_COUNT,
789 	ERROR_INCONSISTENT_BLOCK_COUNT,
790 	ERROR_INVALID_CURSEG,
791 	ERROR_INCONSISTENT_SIT,
792 	ERROR_CORRUPTED_VERITY_XATTR,
793 	ERROR_CORRUPTED_XATTR,
794 	ERROR_MAX,
795 };
796 
797 #define MAX_F2FS_ERRORS			16
798 
799 struct f2fs_super_block {
800 	__le32 magic;			/* Magic Number */
801 	__le16 major_ver;		/* Major Version */
802 	__le16 minor_ver;		/* Minor Version */
803 	__le32 log_sectorsize;		/* log2 sector size in bytes */
804 	__le32 log_sectors_per_block;	/* log2 # of sectors per block */
805 	__le32 log_blocksize;		/* log2 block size in bytes */
806 	__le32 log_blocks_per_seg;	/* log2 # of blocks per segment */
807 	__le32 segs_per_sec;		/* # of segments per section */
808 	__le32 secs_per_zone;		/* # of sections per zone */
809 	__le32 checksum_offset;		/* checksum offset inside super block */
810 	__le64 block_count __attribute__((packed));
811 					/* total # of user blocks */
812 	__le32 section_count;		/* total # of sections */
813 	__le32 segment_count;		/* total # of segments */
814 	__le32 segment_count_ckpt;	/* # of segments for checkpoint */
815 	__le32 segment_count_sit;	/* # of segments for SIT */
816 	__le32 segment_count_nat;	/* # of segments for NAT */
817 	__le32 segment_count_ssa;	/* # of segments for SSA */
818 	__le32 segment_count_main;	/* # of segments for main area */
819 	__le32 segment0_blkaddr;	/* start block address of segment 0 */
820 	__le32 cp_blkaddr;		/* start block address of checkpoint */
821 	__le32 sit_blkaddr;		/* start block address of SIT */
822 	__le32 nat_blkaddr;		/* start block address of NAT */
823 	__le32 ssa_blkaddr;		/* start block address of SSA */
824 	__le32 main_blkaddr;		/* start block address of main area */
825 	__le32 root_ino;		/* root inode number */
826 	__le32 node_ino;		/* node inode number */
827 	__le32 meta_ino;		/* meta inode number */
828 	__u8 uuid[16];			/* 128-bit uuid for volume */
829 	__le16 volume_name[MAX_VOLUME_NAME];	/* volume name */
830 	__le32 extension_count;		/* # of extensions below */
831 	__u8 extension_list[F2FS_MAX_EXTENSION][8];	/* extension array */
832 	__le32 cp_payload;
833 	__u8 version[VERSION_LEN];	/* the kernel version */
834 	__u8 init_version[VERSION_LEN];	/* the initial kernel version */
835 	__le32 feature;			/* defined features */
836 	__u8 encryption_level;		/* versioning level for encryption */
837 	__u8 encrypt_pw_salt[16];	/* Salt used for string2key algorithm */
838 	struct f2fs_device devs[MAX_DEVICES] __attribute__((packed));	/* device list */
839 	__le32 qf_ino[F2FS_MAX_QUOTAS] __attribute__((packed));	/* quota inode numbers */
840 	__u8 hot_ext_count;		/* # of hot file extension */
841 	__le16  s_encoding;		/* Filename charset encoding */
842 	__le16  s_encoding_flags;	/* Filename charset encoding flags */
843 	__u8 s_stop_reason[MAX_STOP_REASON];	/* stop checkpoint reason */
844 	__u8 s_errors[MAX_F2FS_ERRORS];		/* reason of image corrupts */
845 	__u8 reserved[258];		/* valid reserved region */
846 	__le32 crc;			/* checksum of superblock */
847 };
848 
849 static_assert(sizeof(struct f2fs_super_block) == 3072, "");
850 
851 /*
852  * For checkpoint
853  */
854 #define CP_RESIZEFS_FLAG                0x00004000
855 #define CP_DISABLED_FLAG		0x00001000
856 #define CP_QUOTA_NEED_FSCK_FLAG		0x00000800
857 #define CP_LARGE_NAT_BITMAP_FLAG	0x00000400
858 #define CP_NOCRC_RECOVERY_FLAG	0x00000200
859 #define CP_TRIMMED_FLAG		0x00000100
860 #define CP_NAT_BITS_FLAG	0x00000080
861 #define CP_CRC_RECOVERY_FLAG	0x00000040
862 #define CP_FASTBOOT_FLAG	0x00000020
863 #define CP_FSCK_FLAG		0x00000010
864 #define CP_ERROR_FLAG		0x00000008
865 #define CP_COMPACT_SUM_FLAG	0x00000004
866 #define CP_ORPHAN_PRESENT_FLAG	0x00000002
867 #define CP_UMOUNT_FLAG		0x00000001
868 
869 #define F2FS_CP_PACKS		2	/* # of checkpoint packs */
870 
871 struct f2fs_checkpoint {
872 	__le64 checkpoint_ver;		/* checkpoint block version number */
873 	__le64 user_block_count;	/* # of user blocks */
874 	__le64 valid_block_count;	/* # of valid blocks in main area */
875 	__le32 rsvd_segment_count;	/* # of reserved segments for gc */
876 	__le32 overprov_segment_count;	/* # of overprovision segments */
877 	__le32 free_segment_count;	/* # of free segments in main area */
878 
879 	/* information of current node segments */
880 	__le32 cur_node_segno[MAX_ACTIVE_NODE_LOGS];
881 	__le16 cur_node_blkoff[MAX_ACTIVE_NODE_LOGS];
882 	/* information of current data segments */
883 	__le32 cur_data_segno[MAX_ACTIVE_DATA_LOGS];
884 	__le16 cur_data_blkoff[MAX_ACTIVE_DATA_LOGS];
885 	__le32 ckpt_flags;		/* Flags : umount and journal_present */
886 	__le32 cp_pack_total_block_count;	/* total # of one cp pack */
887 	__le32 cp_pack_start_sum;	/* start block number of data summary */
888 	__le32 valid_node_count;	/* Total number of valid nodes */
889 	__le32 valid_inode_count;	/* Total number of valid inodes */
890 	__le32 next_free_nid;		/* Next free node number */
891 	__le32 sit_ver_bitmap_bytesize;	/* Default value 64 */
892 	__le32 nat_ver_bitmap_bytesize; /* Default value 256 */
893 	__le32 checksum_offset;		/* checksum offset inside cp block */
894 	__le64 elapsed_time;		/* mounted time */
895 	/* allocation type of current segment */
896 	unsigned char alloc_type[MAX_ACTIVE_LOGS];
897 
898 	/* SIT and NAT version bitmap */
899 	unsigned char sit_nat_version_bitmap[];
900 };
901 
902 static_assert(sizeof(struct f2fs_checkpoint) == 192, "");
903 
904 #define CP_BITMAP_OFFSET	\
905 	(offsetof(struct f2fs_checkpoint, sit_nat_version_bitmap))
906 #define CP_MIN_CHKSUM_OFFSET	CP_BITMAP_OFFSET
907 
908 #define MIN_NAT_BITMAP_SIZE	64
909 #define MAX_SIT_BITMAP_SIZE_IN_CKPT    \
910 	(CP_CHKSUM_OFFSET - CP_BITMAP_OFFSET - MIN_NAT_BITMAP_SIZE)
911 #define MAX_BITMAP_SIZE_IN_CKPT	\
912 	(CP_CHKSUM_OFFSET - CP_BITMAP_OFFSET)
913 
914 /*
915  * For orphan inode management
916  */
917 #define F2FS_ORPHANS_PER_BLOCK	1020
918 
919 struct f2fs_orphan_block {
920 	__le32 ino[F2FS_ORPHANS_PER_BLOCK];	/* inode numbers */
921 	__le32 reserved;	/* reserved */
922 	__le16 blk_addr;	/* block index in current CP */
923 	__le16 blk_count;	/* Number of orphan inode blocks in CP */
924 	__le32 entry_count;	/* Total number of orphan nodes in current CP */
925 	__le32 check_sum;	/* CRC32 for orphan inode block */
926 };
927 
928 static_assert(sizeof(struct f2fs_orphan_block) == 4096, "");
929 
930 /*
931  * For NODE structure
932  */
933 struct f2fs_extent {
934 	__le32 fofs;		/* start file offset of the extent */
935 	__le32 blk_addr;	/* start block address of the extent */
936 	__le32 len;		/* lengh of the extent */
937 };
938 
939 static_assert(sizeof(struct f2fs_extent) == 12, "");
940 
941 #define F2FS_NAME_LEN		255
942 
943 /* max output length of pretty_print_filename() including null terminator */
944 #define F2FS_PRINT_NAMELEN	(4 * ((F2FS_NAME_LEN + 2) / 3) + 1)
945 
946 /* 200 bytes for inline xattrs by default */
947 #define DEFAULT_INLINE_XATTR_ADDRS	50
948 #define DEF_ADDRS_PER_INODE	923	/* Address Pointers in an Inode */
949 #define CUR_ADDRS_PER_INODE(inode)	(DEF_ADDRS_PER_INODE - \
950 					__get_extra_isize(inode))
951 #define ADDRS_PER_INODE(i)	addrs_per_inode(i)
952 #define DEF_ADDRS_PER_BLOCK	1018	/* Address Pointers in a Direct Block */
953 #define ADDRS_PER_BLOCK(i)	addrs_per_block(i)
954 #define NIDS_PER_BLOCK          1018	/* Node IDs in an Indirect Block */
955 
956 #define	NODE_DIR1_BLOCK		(DEF_ADDRS_PER_INODE + 1)
957 #define	NODE_DIR2_BLOCK		(DEF_ADDRS_PER_INODE + 2)
958 #define	NODE_IND1_BLOCK		(DEF_ADDRS_PER_INODE + 3)
959 #define	NODE_IND2_BLOCK		(DEF_ADDRS_PER_INODE + 4)
960 #define	NODE_DIND_BLOCK		(DEF_ADDRS_PER_INODE + 5)
961 
962 #define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
963 #define F2FS_INLINE_DATA	0x02	/* file inline data flag */
964 #define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
965 #define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
966 #define F2FS_INLINE_DOTS	0x10	/* file having implicit dot dentries */
967 #define F2FS_EXTRA_ATTR		0x20	/* file having extra attribute */
968 #define F2FS_PIN_FILE		0x40	/* file should not be gced */
969 #define F2FS_COMPRESS_RELEASED	0x80	/* file released compressed blocks */
970 
971 #define F2FS_EXTRA_ISIZE_OFFSET				\
972 	offsetof(struct f2fs_inode, i_extra_isize)
973 #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
974 	(offsetof(struct f2fs_inode, i_extra_end) - F2FS_EXTRA_ISIZE_OFFSET)
975 
976 #define	F2FS_DEF_PROJID		0	/* default project ID */
977 
978 #define MAX_INLINE_DATA(node) (sizeof(__le32) *				\
979 				(DEF_ADDRS_PER_INODE -			\
980 				get_inline_xattr_addrs(&node->i) -	\
981 				get_extra_isize(node) -			\
982 				DEF_INLINE_RESERVED_SIZE))
983 #define DEF_MAX_INLINE_DATA	(sizeof(__le32) *			\
984 				(DEF_ADDRS_PER_INODE -			\
985 				DEFAULT_INLINE_XATTR_ADDRS -		\
986 				F2FS_TOTAL_EXTRA_ATTR_SIZE -		\
987 				DEF_INLINE_RESERVED_SIZE))
988 #define INLINE_DATA_OFFSET	(F2FS_BLKSIZE -				\
989 				sizeof(struct node_footer) -		\
990 				sizeof(__le32) * (DEF_ADDRS_PER_INODE +	\
991 				5 - DEF_INLINE_RESERVED_SIZE))
992 
993 #define DEF_DIR_LEVEL		0
994 
995 /*
996  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
997  */
998 #define FADVISE_COLD_BIT	0x01
999 #define FADVISE_LOST_PINO_BIT	0x02
1000 #define FADVISE_ENCRYPT_BIT	0x04
1001 #define FADVISE_ENC_NAME_BIT	0x08
1002 #define FADVISE_KEEP_SIZE_BIT	0x10
1003 #define FADVISE_HOT_BIT		0x20
1004 #define FADVISE_VERITY_BIT	0x40	/* reserved */
1005 
1006 #define file_is_encrypt(fi)      ((fi)->i_advise & FADVISE_ENCRYPT_BIT)
1007 #define file_enc_name(fi)        ((fi)->i_advise & FADVISE_ENC_NAME_BIT)
1008 
1009 #define F2FS_CASEFOLD_FL	0x40000000 /* Casefolded file */
1010 #define IS_CASEFOLDED(dir)     ((dir)->i_flags & F2FS_CASEFOLD_FL)
1011 
1012 /*
1013  * fsck i_compr_blocks counting helper
1014  */
1015 struct f2fs_compr_blk_cnt {
1016 	/* counting i_compr_blocks, init 0 */
1017 	u32 cnt;
1018 
1019 	/*
1020 	 * previous seen compression header (COMPR_ADDR) page offsets,
1021 	 * use CHEADER_PGOFS_NONE for none
1022 	 */
1023 	u32 cheader_pgofs;
1024 };
1025 #define CHEADER_PGOFS_NONE ((u32)-(1 << MAX_COMPRESS_LOG_SIZE))
1026 
1027 /*
1028  * inode flags
1029  */
1030 #define F2FS_COMPR_FL		0x00000004 /* Compress file */
1031 struct f2fs_inode {
1032 	__le16 i_mode;			/* file mode */
1033 	__u8 i_advise;			/* file hints */
1034 	__u8 i_inline;			/* file inline flags */
1035 	__le32 i_uid;			/* user ID */
1036 	__le32 i_gid;			/* group ID */
1037 	__le32 i_links;			/* links count */
1038 	__le64 i_size;			/* file size in bytes */
1039 	__le64 i_blocks;		/* file size in blocks */
1040 	__le64 i_atime;			/* access time */
1041 	__le64 i_ctime;			/* change time */
1042 	__le64 i_mtime;			/* modification time */
1043 	__le32 i_atime_nsec;		/* access time in nano scale */
1044 	__le32 i_ctime_nsec;		/* change time in nano scale */
1045 	__le32 i_mtime_nsec;		/* modification time in nano scale */
1046 	__le32 i_generation;		/* file version (for NFS) */
1047 	union {
1048 		__le32 i_current_depth;	/* only for directory depth */
1049 		__le16 i_gc_failures;	/*
1050 					 * # of gc failures on pinned file.
1051 					 * only for regular files.
1052 					 */
1053 	};
1054 	__le32 i_xattr_nid;		/* nid to save xattr */
1055 	__le32 i_flags;			/* file attributes */
1056 	__le32 i_pino;			/* parent inode number */
1057 	__le32 i_namelen;		/* file name length */
1058 	__u8 i_name[F2FS_NAME_LEN];	/* file name for SPOR */
1059 	__u8 i_dir_level;		/* dentry_level for large dir */
1060 
1061 	struct f2fs_extent i_ext __attribute__((packed));	/* caching a largest extent */
1062 
1063 	union {
1064 		struct {
1065 			__le16 i_extra_isize;	/* extra inode attribute size */
1066 			__le16 i_inline_xattr_size;	/* inline xattr size, unit: 4 bytes */
1067 			__le32 i_projid;	/* project id */
1068 			__le32 i_inode_checksum;/* inode meta checksum */
1069 			__le64 i_crtime;	/* creation time */
1070 			__le32 i_crtime_nsec;	/* creation time in nano scale */
1071 			__le64 i_compr_blocks;	/* # of compressed blocks */
1072 			__u8 i_compress_algrithm;	/* compress algrithm */
1073 			__u8 i_log_cluster_size;	/* log of cluster size */
1074 			__le16 i_padding;		/* padding */
1075 			__le32 i_extra_end[0];	/* for attribute size calculation */
1076 		} __attribute__((packed));
1077 		__le32 i_addr[DEF_ADDRS_PER_INODE];	/* Pointers to data blocks */
1078 	};
1079 	__le32 i_nid[5];		/* direct(2), indirect(2),
1080 						double_indirect(1) node id */
1081 };
1082 
1083 static_assert(offsetof(struct f2fs_inode, i_extra_end) -
1084 	      offsetof(struct f2fs_inode, i_extra_isize) == 36, "");
1085 static_assert(sizeof(struct f2fs_inode) == 4072, "");
1086 
1087 struct direct_node {
1088 	__le32 addr[DEF_ADDRS_PER_BLOCK];	/* array of data block address */
1089 };
1090 
1091 static_assert(sizeof(struct direct_node) == 4072, "");
1092 
1093 struct indirect_node {
1094 	__le32 nid[NIDS_PER_BLOCK];	/* array of data block address */
1095 };
1096 
1097 static_assert(sizeof(struct indirect_node) == 4072, "");
1098 
1099 enum {
1100 	COLD_BIT_SHIFT = 0,
1101 	FSYNC_BIT_SHIFT,
1102 	DENT_BIT_SHIFT,
1103 	OFFSET_BIT_SHIFT
1104 };
1105 
1106 #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
1107 				>> OFFSET_BIT_SHIFT)
1108 struct node_footer {
1109 	__le32 nid;		/* node id */
1110 	__le32 ino;		/* inode nunmber */
1111 	__le32 flag;		/* include cold/fsync/dentry marks and offset */
1112 	__le64 cp_ver __attribute__((packed));		/* checkpoint version */
1113 	__le32 next_blkaddr;	/* next node page block address */
1114 };
1115 
1116 static_assert(sizeof(struct node_footer) == 24, "");
1117 
1118 struct f2fs_node {
1119 	/* can be one of three types: inode, direct, and indirect types */
1120 	union {
1121 		struct f2fs_inode i;
1122 		struct direct_node dn;
1123 		struct indirect_node in;
1124 	};
1125 	struct node_footer footer;
1126 };
1127 
1128 static_assert(sizeof(struct f2fs_node) == 4096, "");
1129 
1130 /*
1131  * For NAT entries
1132  */
1133 #define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / sizeof(struct f2fs_nat_entry))
1134 #define NAT_BLOCK_OFFSET(start_nid) (start_nid / NAT_ENTRY_PER_BLOCK)
1135 
1136 #define DEFAULT_NAT_ENTRY_RATIO		20
1137 
1138 struct f2fs_nat_entry {
1139 	__u8 version;		/* latest version of cached nat entry */
1140 	__le32 ino;		/* inode number */
1141 	__le32 block_addr;	/* block address */
1142 } __attribute__((packed));
1143 
1144 static_assert(sizeof(struct f2fs_nat_entry) == 9, "");
1145 
1146 struct f2fs_nat_block {
1147 	struct f2fs_nat_entry entries[NAT_ENTRY_PER_BLOCK];
1148 };
1149 
1150 static_assert(sizeof(struct f2fs_nat_block) == 4095, "");
1151 
1152 /*
1153  * For SIT entries
1154  *
1155  * Each segment is 2MB in size by default so that a bitmap for validity of
1156  * there-in blocks should occupy 64 bytes, 512 bits.
1157  * Not allow to change this.
1158  */
1159 #define SIT_VBLOCK_MAP_SIZE 64
1160 #define SIT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / sizeof(struct f2fs_sit_entry))
1161 
1162 /*
1163  * F2FS uses 4 bytes to represent block address. As a result, supported size of
1164  * disk is 16 TB and it equals to 16 * 1024 * 1024 / 2 segments.
1165  */
1166 #define F2FS_MIN_SEGMENT      9 /* SB + 2 (CP + SIT + NAT) + SSA + MAIN */
1167 #define F2FS_MAX_SEGMENT       ((16 * 1024 * 1024) / 2)
1168 #define MAX_SIT_BITMAP_SIZE    (SEG_ALIGN(SIZE_ALIGN(F2FS_MAX_SEGMENT, \
1169 						SIT_ENTRY_PER_BLOCK)) * \
1170 						c.blks_per_seg / 8)
1171 #define MAX_CP_PAYLOAD         (SEG_ALIGN(SIZE_ALIGN(UINT32_MAX, NAT_ENTRY_PER_BLOCK)) * \
1172 						DEFAULT_NAT_ENTRY_RATIO / 100 * \
1173 						c.blks_per_seg / 8 + \
1174 						MAX_SIT_BITMAP_SIZE - MAX_BITMAP_SIZE_IN_CKPT)
1175 
1176 /*
1177  * Note that f2fs_sit_entry->vblocks has the following bit-field information.
1178  * [15:10] : allocation type such as CURSEG_XXXX_TYPE
1179  * [9:0] : valid block count
1180  */
1181 #define SIT_VBLOCKS_SHIFT	10
1182 #define SIT_VBLOCKS_MASK	((1 << SIT_VBLOCKS_SHIFT) - 1)
1183 #define GET_SIT_VBLOCKS(raw_sit)				\
1184 	(le16_to_cpu((raw_sit)->vblocks) & SIT_VBLOCKS_MASK)
1185 #define GET_SIT_TYPE(raw_sit)					\
1186 	((le16_to_cpu((raw_sit)->vblocks) & ~SIT_VBLOCKS_MASK)	\
1187 	 >> SIT_VBLOCKS_SHIFT)
1188 
1189 struct f2fs_sit_entry {
1190 	__le16 vblocks;				/* reference above */
1191 	__u8 valid_map[SIT_VBLOCK_MAP_SIZE];	/* bitmap for valid blocks */
1192 	__le64 mtime;				/* segment age for cleaning */
1193 } __attribute__((packed));
1194 
1195 static_assert(sizeof(struct f2fs_sit_entry) == 74, "");
1196 
1197 struct f2fs_sit_block {
1198 	struct f2fs_sit_entry entries[SIT_ENTRY_PER_BLOCK];
1199 };
1200 
1201 static_assert(sizeof(struct f2fs_sit_block) == 4070, "");
1202 
1203 /*
1204  * For segment summary
1205  *
1206  * One summary block contains exactly 512 summary entries, which represents
1207  * exactly 2MB segment by default. Not allow to change the basic units.
1208  *
1209  * NOTE: For initializing fields, you must use set_summary
1210  *
1211  * - If data page, nid represents dnode's nid
1212  * - If node page, nid represents the node page's nid.
1213  *
1214  * The ofs_in_node is used by only data page. It represents offset
1215  * from node's page's beginning to get a data block address.
1216  * ex) data_blkaddr = (block_t)(nodepage_start_address + ofs_in_node)
1217  */
1218 #define ENTRIES_IN_SUM		512
1219 #define	SUMMARY_SIZE		(7)	/* sizeof(struct summary) */
1220 #define	SUM_FOOTER_SIZE		(5)	/* sizeof(struct summary_footer) */
1221 #define SUM_ENTRIES_SIZE	(SUMMARY_SIZE * ENTRIES_IN_SUM)
1222 
1223 /* a summary entry for a 4KB-sized block in a segment */
1224 struct f2fs_summary {
1225 	__le32 nid;		/* parent node id */
1226 	union {
1227 		__u8 reserved[3];
1228 		struct {
1229 			__u8 version;		/* node version number */
1230 			__le16 ofs_in_node;	/* block index in parent node */
1231 		} __attribute__((packed));
1232 	};
1233 } __attribute__((packed));
1234 
1235 static_assert(sizeof(struct f2fs_summary) == 7, "");
1236 
1237 /* summary block type, node or data, is stored to the summary_footer */
1238 #define SUM_TYPE_NODE		(1)
1239 #define SUM_TYPE_DATA		(0)
1240 
1241 struct summary_footer {
1242 	unsigned char entry_type;	/* SUM_TYPE_XXX */
1243 	__le32 check_sum __attribute__((packed)); /* summary checksum */
1244 };
1245 
1246 static_assert(sizeof(struct summary_footer) == 5, "");
1247 
1248 #define SUM_JOURNAL_SIZE	(F2FS_BLKSIZE - SUM_FOOTER_SIZE -\
1249 				SUM_ENTRIES_SIZE)
1250 #define NAT_JOURNAL_ENTRIES	((SUM_JOURNAL_SIZE - 2) /\
1251 				sizeof(struct nat_journal_entry))
1252 #define NAT_JOURNAL_RESERVED	((SUM_JOURNAL_SIZE - 2) %\
1253 				sizeof(struct nat_journal_entry))
1254 #define SIT_JOURNAL_ENTRIES	((SUM_JOURNAL_SIZE - 2) /\
1255 				sizeof(struct sit_journal_entry))
1256 #define SIT_JOURNAL_RESERVED	((SUM_JOURNAL_SIZE - 2) %\
1257 				sizeof(struct sit_journal_entry))
1258 
1259 /*
1260  * Reserved area should make size of f2fs_extra_info equals to
1261  * that of nat_journal and sit_journal.
1262  */
1263 #define EXTRA_INFO_RESERVED	(SUM_JOURNAL_SIZE - 2 - 8)
1264 
1265 /*
1266  * frequently updated NAT/SIT entries can be stored in the spare area in
1267  * summary blocks
1268  */
1269 enum {
1270 	NAT_JOURNAL = 0,
1271 	SIT_JOURNAL
1272 };
1273 
1274 struct nat_journal_entry {
1275 	__le32 nid;
1276 	struct f2fs_nat_entry ne;
1277 } __attribute__((packed));
1278 
1279 static_assert(sizeof(struct nat_journal_entry) == 13, "");
1280 
1281 struct nat_journal {
1282 	struct nat_journal_entry entries[NAT_JOURNAL_ENTRIES];
1283 	__u8 reserved[NAT_JOURNAL_RESERVED];
1284 };
1285 
1286 static_assert(sizeof(struct nat_journal) == 505, "");
1287 
1288 struct sit_journal_entry {
1289 	__le32 segno;
1290 	struct f2fs_sit_entry se;
1291 } __attribute__((packed));
1292 
1293 static_assert(sizeof(struct sit_journal_entry) == 78, "");
1294 
1295 struct sit_journal {
1296 	struct sit_journal_entry entries[SIT_JOURNAL_ENTRIES];
1297 	__u8 reserved[SIT_JOURNAL_RESERVED];
1298 };
1299 
1300 static_assert(sizeof(struct sit_journal) == 505, "");
1301 
1302 struct f2fs_extra_info {
1303 	__le64 kbytes_written;
1304 	__u8 reserved[EXTRA_INFO_RESERVED];
1305 } __attribute__((packed));
1306 
1307 static_assert(sizeof(struct f2fs_extra_info) == 505, "");
1308 
1309 struct f2fs_journal {
1310 	union {
1311 		__le16 n_nats;
1312 		__le16 n_sits;
1313 	};
1314 	/* spare area is used by NAT or SIT journals or extra info */
1315 	union {
1316 		struct nat_journal nat_j;
1317 		struct sit_journal sit_j;
1318 		struct f2fs_extra_info info;
1319 	};
1320 } __attribute__((packed));
1321 
1322 static_assert(sizeof(struct f2fs_journal) == 507, "");
1323 
1324 /* 4KB-sized summary block structure */
1325 struct f2fs_summary_block {
1326 	struct f2fs_summary entries[ENTRIES_IN_SUM];
1327 	struct f2fs_journal journal;
1328 	struct summary_footer footer;
1329 };
1330 
1331 static_assert(sizeof(struct f2fs_summary_block) == 4096, "");
1332 
1333 /*
1334  * For directory operations
1335  */
1336 #define F2FS_DOT_HASH		0
1337 #define F2FS_DDOT_HASH		F2FS_DOT_HASH
1338 #define F2FS_MAX_HASH		(~((0x3ULL) << 62))
1339 #define F2FS_HASH_COL_BIT	((0x1ULL) << 63)
1340 
1341 typedef __le32	f2fs_hash_t;
1342 
1343 /* One directory entry slot covers 8bytes-long file name */
1344 #define F2FS_SLOT_LEN		8
1345 #define F2FS_SLOT_LEN_BITS	3
1346 
1347 #define GET_DENTRY_SLOTS(x)	((x + F2FS_SLOT_LEN - 1) >> F2FS_SLOT_LEN_BITS)
1348 
1349 /* the number of dentry in a block */
1350 #define NR_DENTRY_IN_BLOCK	214
1351 
1352 /* MAX level for dir lookup */
1353 #define MAX_DIR_HASH_DEPTH	63
1354 
1355 /* MAX buckets in one level of dir */
1356 #define MAX_DIR_BUCKETS		(1 << ((MAX_DIR_HASH_DEPTH / 2) - 1))
1357 
1358 #define SIZE_OF_DIR_ENTRY	11	/* by byte */
1359 #define SIZE_OF_DENTRY_BITMAP	((NR_DENTRY_IN_BLOCK + BITS_PER_BYTE - 1) / \
1360 					BITS_PER_BYTE)
1361 #define SIZE_OF_RESERVED	(F2FS_BLKSIZE - ((SIZE_OF_DIR_ENTRY + \
1362 				F2FS_SLOT_LEN) * \
1363 				NR_DENTRY_IN_BLOCK + SIZE_OF_DENTRY_BITMAP))
1364 #define MIN_INLINE_DENTRY_SIZE		40	/* just include '.' and '..' entries */
1365 
1366 /* One directory entry slot representing F2FS_SLOT_LEN-sized file name */
1367 struct f2fs_dir_entry {
1368 	__le32 hash_code;	/* hash code of file name */
1369 	__le32 ino;		/* inode number */
1370 	__le16 name_len;	/* lengh of file name */
1371 	__u8 file_type;		/* file type */
1372 } __attribute__((packed));
1373 
1374 static_assert(sizeof(struct f2fs_dir_entry) == 11, "");
1375 
1376 /* 4KB-sized directory entry block */
1377 struct f2fs_dentry_block {
1378 	/* validity bitmap for directory entries in each block */
1379 	__u8 dentry_bitmap[SIZE_OF_DENTRY_BITMAP];
1380 	__u8 reserved[SIZE_OF_RESERVED];
1381 	struct f2fs_dir_entry dentry[NR_DENTRY_IN_BLOCK];
1382 	__u8 filename[NR_DENTRY_IN_BLOCK][F2FS_SLOT_LEN];
1383 };
1384 
1385 static_assert(sizeof(struct f2fs_dentry_block) == F2FS_BLKSIZE, "");
1386 
1387 /* for inline stuff */
1388 #define DEF_INLINE_RESERVED_SIZE	1
1389 
1390 /* for inline dir */
1391 #define NR_INLINE_DENTRY(node)	(MAX_INLINE_DATA(node) * BITS_PER_BYTE / \
1392 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
1393 				BITS_PER_BYTE + 1))
1394 #define INLINE_DENTRY_BITMAP_SIZE(node)	((NR_INLINE_DENTRY(node) + \
1395 					BITS_PER_BYTE - 1) / BITS_PER_BYTE)
1396 #define INLINE_RESERVED_SIZE(node)	(MAX_INLINE_DATA(node) - \
1397 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
1398 				NR_INLINE_DENTRY(node) + \
1399 				INLINE_DENTRY_BITMAP_SIZE(node)))
1400 
1401 /* file types used in inode_info->flags */
1402 enum FILE_TYPE {
1403 	F2FS_FT_UNKNOWN,
1404 	F2FS_FT_REG_FILE,
1405 	F2FS_FT_DIR,
1406 	F2FS_FT_CHRDEV,
1407 	F2FS_FT_BLKDEV,
1408 	F2FS_FT_FIFO,
1409 	F2FS_FT_SOCK,
1410 	F2FS_FT_SYMLINK,
1411 	F2FS_FT_MAX,
1412 	/* added for fsck */
1413 	F2FS_FT_ORPHAN,
1414 	F2FS_FT_XATTR,
1415 	F2FS_FT_LAST_FILE_TYPE = F2FS_FT_XATTR,
1416 };
1417 
1418 #define LINUX_S_IFMT  00170000
1419 #define LINUX_S_IFREG  0100000
1420 #define LINUX_S_ISREG(m)	(((m) & LINUX_S_IFMT) == LINUX_S_IFREG)
1421 
1422 /* from f2fs/segment.h */
1423 enum {
1424 	LFS = 0,
1425 	SSR
1426 };
1427 
1428 extern int utf8_to_utf16(uint16_t *, const char *, size_t, size_t);
1429 extern int utf16_to_utf8(char *, const uint16_t *, size_t, size_t);
1430 extern int log_base_2(uint32_t);
1431 extern unsigned int addrs_per_inode(struct f2fs_inode *);
1432 extern unsigned int addrs_per_block(struct f2fs_inode *);
1433 extern unsigned int f2fs_max_file_offset(struct f2fs_inode *);
1434 extern __u32 f2fs_inode_chksum(struct f2fs_node *);
1435 extern __u32 f2fs_checkpoint_chksum(struct f2fs_checkpoint *);
1436 extern int write_inode(struct f2fs_node *, u64);
1437 
1438 extern int get_bits_in_byte(unsigned char n);
1439 extern int test_and_set_bit_le(u32, u8 *);
1440 extern int test_and_clear_bit_le(u32, u8 *);
1441 extern int test_bit_le(u32, const u8 *);
1442 extern int f2fs_test_bit(unsigned int, const char *);
1443 extern int f2fs_set_bit(unsigned int, char *);
1444 extern int f2fs_clear_bit(unsigned int, char *);
1445 extern u64 find_next_bit_le(const u8 *, u64, u64);
1446 extern u64 find_next_zero_bit_le(const u8 *, u64, u64);
1447 
1448 extern uint32_t f2fs_cal_crc32(uint32_t, void *, int);
1449 extern int f2fs_crc_valid(uint32_t blk_crc, void *buf, int len);
1450 
1451 extern void f2fs_init_configuration(void);
1452 extern int f2fs_devs_are_umounted(void);
1453 extern int f2fs_dev_is_writable(void);
1454 extern int f2fs_dev_is_umounted(char *);
1455 extern int f2fs_get_device_info(void);
1456 extern int f2fs_get_f2fs_info(void);
1457 extern unsigned int calc_extra_isize(void);
1458 extern int get_device_info(int);
1459 extern int f2fs_init_sparse_file(void);
1460 extern void f2fs_release_sparse_resource(void);
1461 extern int f2fs_finalize_device(void);
1462 extern int f2fs_fsync_device(void);
1463 
1464 extern void dcache_init(void);
1465 extern void dcache_release(void);
1466 
1467 extern int dev_read(void *, __u64, size_t);
1468 #ifdef POSIX_FADV_WILLNEED
1469 extern int dev_readahead(__u64, size_t);
1470 #else
1471 extern int dev_readahead(__u64, size_t UNUSED(len));
1472 #endif
1473 extern int dev_write(void *, __u64, size_t);
1474 extern int dev_write_block(void *, __u64);
1475 extern int dev_write_dump(void *, __u64, size_t);
1476 /* All bytes in the buffer must be 0 use dev_fill(). */
1477 extern int dev_fill(void *, __u64, size_t);
1478 extern int dev_fill_block(void *, __u64);
1479 
1480 extern int dev_read_block(void *, __u64);
1481 extern int dev_reada_block(__u64);
1482 
1483 extern int dev_read_version(void *, __u64, size_t);
1484 extern void get_kernel_version(__u8 *);
1485 extern void get_kernel_uname_version(__u8 *);
1486 f2fs_hash_t f2fs_dentry_hash(int, int, const unsigned char *, int);
1487 
f2fs_has_extra_isize(struct f2fs_inode * inode)1488 static inline bool f2fs_has_extra_isize(struct f2fs_inode *inode)
1489 {
1490 	return (inode->i_inline & F2FS_EXTRA_ATTR);
1491 }
1492 
__get_extra_isize(struct f2fs_inode * inode)1493 static inline int __get_extra_isize(struct f2fs_inode *inode)
1494 {
1495 	if (f2fs_has_extra_isize(inode))
1496 		return le16_to_cpu(inode->i_extra_isize) / sizeof(__le32);
1497 	return 0;
1498 }
1499 
1500 extern struct f2fs_configuration c;
get_inline_xattr_addrs(struct f2fs_inode * inode)1501 static inline int get_inline_xattr_addrs(struct f2fs_inode *inode)
1502 {
1503 	if (c.feature & cpu_to_le32(F2FS_FEATURE_FLEXIBLE_INLINE_XATTR))
1504 		return le16_to_cpu(inode->i_inline_xattr_size);
1505 	else if (inode->i_inline & F2FS_INLINE_XATTR ||
1506 			inode->i_inline & F2FS_INLINE_DENTRY)
1507 		return DEFAULT_INLINE_XATTR_ADDRS;
1508 	else
1509 		return 0;
1510 }
1511 
1512 #define get_extra_isize(node)	__get_extra_isize(&node->i)
1513 
1514 #define F2FS_ZONED_NONE		0
1515 #define F2FS_ZONED_HA		1
1516 #define F2FS_ZONED_HM		2
1517 
1518 #ifdef HAVE_LINUX_BLKZONED_H
1519 
1520 /* Let's just use v2, since v1 should be compatible with v2 */
1521 #define BLK_ZONE_REP_CAPACITY   (1 << 0)
1522 struct blk_zone_v2 {
1523 	__u64   start;          /* Zone start sector */
1524 	__u64   len;            /* Zone length in number of sectors */
1525 	__u64   wp;             /* Zone write pointer position */
1526 	__u8    type;           /* Zone type */
1527 	__u8    cond;           /* Zone condition */
1528 	__u8    non_seq;        /* Non-sequential write resources active */
1529 	__u8    reset;          /* Reset write pointer recommended */
1530 	__u8    resv[4];
1531 	__u64   capacity;       /* Zone capacity in number of sectors */
1532 	__u8    reserved[24];
1533 };
1534 #define blk_zone blk_zone_v2
1535 
1536 struct blk_zone_report_v2 {
1537 	__u64   sector;
1538 	__u32   nr_zones;
1539 	__u32   flags;
1540 	struct blk_zone zones[0];
1541 };
1542 #define blk_zone_report blk_zone_report_v2
1543 
1544 #define blk_zone_type(z)        (z)->type
1545 #define blk_zone_conv(z)	((z)->type == BLK_ZONE_TYPE_CONVENTIONAL)
1546 #define blk_zone_seq_req(z)	((z)->type == BLK_ZONE_TYPE_SEQWRITE_REQ)
1547 #define blk_zone_seq_pref(z)	((z)->type == BLK_ZONE_TYPE_SEQWRITE_PREF)
1548 #define blk_zone_seq(z)		(blk_zone_seq_req(z) || blk_zone_seq_pref(z))
1549 
1550 static inline const char *
blk_zone_type_str(struct blk_zone * blkz)1551 blk_zone_type_str(struct blk_zone *blkz)
1552 {
1553 	switch (blk_zone_type(blkz)) {
1554 	case BLK_ZONE_TYPE_CONVENTIONAL:
1555 		return( "Conventional" );
1556 	case BLK_ZONE_TYPE_SEQWRITE_REQ:
1557 		return( "Sequential-write-required" );
1558 	case BLK_ZONE_TYPE_SEQWRITE_PREF:
1559 		return( "Sequential-write-preferred" );
1560 	}
1561 	return( "Unknown-type" );
1562 }
1563 
1564 #define blk_zone_cond(z)	(z)->cond
1565 
1566 static inline const char *
blk_zone_cond_str(struct blk_zone * blkz)1567 blk_zone_cond_str(struct blk_zone *blkz)
1568 {
1569 	switch (blk_zone_cond(blkz)) {
1570 	case BLK_ZONE_COND_NOT_WP:
1571 		return "Not-write-pointer";
1572 	case BLK_ZONE_COND_EMPTY:
1573 		return "Empty";
1574 	case BLK_ZONE_COND_IMP_OPEN:
1575 		return "Implicit-open";
1576 	case BLK_ZONE_COND_EXP_OPEN:
1577 		return "Explicit-open";
1578 	case BLK_ZONE_COND_CLOSED:
1579 		return "Closed";
1580 	case BLK_ZONE_COND_READONLY:
1581 		return "Read-only";
1582 	case BLK_ZONE_COND_FULL:
1583 		return "Full";
1584 	case BLK_ZONE_COND_OFFLINE:
1585 		return "Offline";
1586 	}
1587 	return "Unknown-cond";
1588 }
1589 
1590 /*
1591  * Handle kernel zone capacity support
1592  */
1593 #define blk_zone_empty(z)	(blk_zone_cond(z) == BLK_ZONE_COND_EMPTY)
1594 #define blk_zone_sector(z)	(z)->start
1595 #define blk_zone_length(z)	(z)->len
1596 #define blk_zone_wp_sector(z)	(z)->wp
1597 #define blk_zone_need_reset(z)	(int)(z)->reset
1598 #define blk_zone_non_seq(z)	(int)(z)->non_seq
1599 #define blk_zone_capacity(z, f) ((f & BLK_ZONE_REP_CAPACITY) ? \
1600 					(z)->capacity : (z)->len)
1601 
1602 #endif
1603 
1604 struct blk_zone;
1605 
1606 extern int f2fs_get_zoned_model(int);
1607 extern int f2fs_get_zone_blocks(int);
1608 extern int f2fs_report_zone(int, uint64_t, struct blk_zone *);
1609 typedef int (report_zones_cb_t)(int i, void *, void *);
1610 extern int f2fs_report_zones(int, report_zones_cb_t *, void *);
1611 extern int f2fs_check_zones(int);
1612 int f2fs_reset_zone(int, void *);
1613 extern int f2fs_reset_zones(int);
1614 extern uint32_t f2fs_get_usable_segments(struct f2fs_super_block *sb);
1615 
1616 #define SIZE_ALIGN(val, size)	(((val) + (size) - 1) / (size))
1617 #define SEG_ALIGN(blks)		SIZE_ALIGN(blks, c.blks_per_seg)
1618 #define ZONE_ALIGN(blks)	SIZE_ALIGN(blks, c.blks_per_seg * \
1619 					c.segs_per_zone)
1620 
get_best_overprovision(struct f2fs_super_block * sb)1621 static inline double get_best_overprovision(struct f2fs_super_block *sb)
1622 {
1623 	double reserved, ovp, candidate, end, diff, space;
1624 	double max_ovp = 0, max_space = 0;
1625 	uint32_t usable_main_segs = f2fs_get_usable_segments(sb);
1626 
1627 	if (get_sb(segment_count_main) < 256) {
1628 		candidate = 10;
1629 		end = 95;
1630 		diff = 5;
1631 	} else {
1632 		candidate = 0.01;
1633 		end = 10;
1634 		diff = 0.01;
1635 	}
1636 
1637 	for (; candidate <= end; candidate += diff) {
1638 		reserved = (100 / candidate + 1 + NR_CURSEG_TYPE) *
1639 				round_up(usable_main_segs, get_sb(section_count));
1640 		ovp = (usable_main_segs - reserved) * candidate / 100;
1641 		if (ovp < 0)
1642 			continue;
1643 		space = usable_main_segs - max(reserved, ovp) -
1644 					2 * get_sb(segs_per_sec);
1645 		if (max_space < space) {
1646 			max_space = space;
1647 			max_ovp = candidate;
1648 		}
1649 	}
1650 	return max_ovp;
1651 }
1652 
get_cp_crc(struct f2fs_checkpoint * cp)1653 static inline __le64 get_cp_crc(struct f2fs_checkpoint *cp)
1654 {
1655 	uint64_t cp_ver = get_cp(checkpoint_ver);
1656 	size_t crc_offset = get_cp(checksum_offset);
1657 	uint32_t crc = le32_to_cpu(*(__le32 *)((unsigned char *)cp +
1658 							crc_offset));
1659 
1660 	cp_ver |= ((uint64_t)crc << 32);
1661 	return cpu_to_le64(cp_ver);
1662 }
1663 
exist_qf_ino(struct f2fs_super_block * sb)1664 static inline int exist_qf_ino(struct f2fs_super_block *sb)
1665 {
1666 	int i;
1667 
1668 	for (i = 0; i < F2FS_MAX_QUOTAS; i++)
1669 		if (sb->qf_ino[i])
1670 			return 1;
1671 	return 0;
1672 }
1673 
is_qf_ino(struct f2fs_super_block * sb,nid_t ino)1674 static inline int is_qf_ino(struct f2fs_super_block *sb, nid_t ino)
1675 {
1676 	int i;
1677 
1678 	for (i = 0; i < F2FS_MAX_QUOTAS; i++)
1679 		if (sb->qf_ino[i] == ino)
1680 			return 1;
1681 	return 0;
1682 }
1683 
show_version(const char * prog)1684 static inline void show_version(const char *prog)
1685 {
1686 #if defined(F2FS_TOOLS_VERSION) && defined(F2FS_TOOLS_DATE)
1687 	MSG(0, "%s %s (%s)\n", prog, F2FS_TOOLS_VERSION, F2FS_TOOLS_DATE);
1688 #else
1689 	MSG(0, "%s -- version not supported\n", prog);
1690 #endif
1691 }
1692 
f2fs_init_qf_inode(struct f2fs_super_block * sb,struct f2fs_node * raw_node,int qtype,time_t mtime)1693 static inline void f2fs_init_qf_inode(struct f2fs_super_block *sb,
1694 		struct f2fs_node *raw_node, int qtype, time_t mtime)
1695 {
1696 	raw_node->footer.nid = sb->qf_ino[qtype];
1697 	raw_node->footer.ino = sb->qf_ino[qtype];
1698 	raw_node->footer.cp_ver = cpu_to_le64(1);
1699 	raw_node->i.i_mode = cpu_to_le16(0x8180);
1700 	raw_node->i.i_links = cpu_to_le32(1);
1701 	raw_node->i.i_uid = cpu_to_le32(c.root_uid);
1702 	raw_node->i.i_gid = cpu_to_le32(c.root_gid);
1703 
1704 	raw_node->i.i_size = cpu_to_le64(1024 * 6); /* Hard coded */
1705 	raw_node->i.i_blocks = cpu_to_le64(1);
1706 
1707 	raw_node->i.i_atime = cpu_to_le32(mtime);
1708 	raw_node->i.i_atime_nsec = 0;
1709 	raw_node->i.i_ctime = cpu_to_le32(mtime);
1710 	raw_node->i.i_ctime_nsec = 0;
1711 	raw_node->i.i_mtime = cpu_to_le32(mtime);
1712 	raw_node->i.i_mtime_nsec = 0;
1713 	raw_node->i.i_generation = 0;
1714 	raw_node->i.i_xattr_nid = 0;
1715 	raw_node->i.i_flags = FS_IMMUTABLE_FL;
1716 	raw_node->i.i_current_depth = cpu_to_le32(0);
1717 	raw_node->i.i_dir_level = DEF_DIR_LEVEL;
1718 
1719 	if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
1720 		raw_node->i.i_inline = F2FS_EXTRA_ATTR;
1721 		raw_node->i.i_extra_isize = cpu_to_le16(calc_extra_isize());
1722 	}
1723 
1724 	if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA))
1725 		raw_node->i.i_projid = cpu_to_le32(F2FS_DEF_PROJID);
1726 
1727 	raw_node->i.i_ext.fofs = 0;
1728 	raw_node->i.i_ext.blk_addr = 0;
1729 	raw_node->i.i_ext.len = 0;
1730 }
1731 
1732 struct feature {
1733 	char *name;
1734 	u32  mask;
1735 };
1736 
1737 #define INIT_FEATURE_TABLE						\
1738 struct feature feature_table[] = {					\
1739 	{ "encrypt",			F2FS_FEATURE_ENCRYPT },		\
1740 	{ "extra_attr",			F2FS_FEATURE_EXTRA_ATTR },	\
1741 	{ "project_quota",		F2FS_FEATURE_PRJQUOTA },	\
1742 	{ "inode_checksum",		F2FS_FEATURE_INODE_CHKSUM },	\
1743 	{ "flexible_inline_xattr",	F2FS_FEATURE_FLEXIBLE_INLINE_XATTR },\
1744 	{ "quota",			F2FS_FEATURE_QUOTA_INO },	\
1745 	{ "inode_crtime",		F2FS_FEATURE_INODE_CRTIME },	\
1746 	{ "lost_found",			F2FS_FEATURE_LOST_FOUND },	\
1747 	{ "verity",			F2FS_FEATURE_VERITY },	/* reserved */ \
1748 	{ "sb_checksum",		F2FS_FEATURE_SB_CHKSUM },	\
1749 	{ "casefold",			F2FS_FEATURE_CASEFOLD },	\
1750 	{ "compression",		F2FS_FEATURE_COMPRESSION },	\
1751 	{ "ro",				F2FS_FEATURE_RO},		\
1752 	{ NULL,				0x0},				\
1753 };
1754 
feature_map(struct feature * table,char * feature)1755 static inline u32 feature_map(struct feature *table, char *feature)
1756 {
1757 	struct feature *p;
1758 	for (p = table; p->name && strcmp(p->name, feature); p++)
1759 		;
1760 	return p->mask;
1761 }
1762 
set_feature_bits(struct feature * table,char * features)1763 static inline int set_feature_bits(struct feature *table, char *features)
1764 {
1765 	u32 mask = feature_map(table, features);
1766 	if (mask) {
1767 		c.feature |= cpu_to_le32(mask);
1768 	} else {
1769 		MSG(0, "Error: Wrong features %s\n", features);
1770 		return -1;
1771 	}
1772 	return 0;
1773 }
1774 
parse_feature(struct feature * table,const char * features)1775 static inline int parse_feature(struct feature *table, const char *features)
1776 {
1777 	char *buf, *sub, *next;
1778 
1779 	buf = strdup(features);
1780 	if (!buf)
1781 		return -1;
1782 
1783 	for (sub = buf; sub && *sub; sub = next ? next + 1 : NULL) {
1784 		/* Skip the beginning blanks */
1785 		while (*sub && *sub == ' ')
1786 			sub++;
1787 		next = sub;
1788 		/* Skip a feature word */
1789 		while (*next && *next != ' ' && *next != ',')
1790 			next++;
1791 
1792 		if (*next == 0)
1793 			next = NULL;
1794 		else
1795 			*next = 0;
1796 
1797 		if (set_feature_bits(table, sub)) {
1798 			free(buf);
1799 			return -1;
1800 		}
1801 	}
1802 	free(buf);
1803 	return 0;
1804 }
1805 
parse_root_owner(char * ids,uint32_t * root_uid,uint32_t * root_gid)1806 static inline int parse_root_owner(char *ids,
1807 			uint32_t *root_uid, uint32_t *root_gid)
1808 {
1809 	char *uid = ids;
1810 	char *gid = NULL;
1811 	int i;
1812 
1813 	/* uid:gid */
1814 	for (i = 0; i < strlen(ids) - 1; i++)
1815 		if (*(ids + i) == ':')
1816 			gid = ids + i + 1;
1817 	if (!gid)
1818 		return -1;
1819 
1820 	*root_uid = atoi(uid);
1821 	*root_gid = atoi(gid);
1822 	return 0;
1823 }
1824 
1825 /*
1826  * NLS definitions
1827  */
1828 struct f2fs_nls_table {
1829 	int version;
1830 	const struct f2fs_nls_ops *ops;
1831 };
1832 
1833 struct f2fs_nls_ops {
1834 	int (*casefold)(const struct f2fs_nls_table *charset,
1835 			const unsigned char *str, size_t len,
1836 			unsigned char *dest, size_t dlen);
1837 };
1838 
1839 extern const struct f2fs_nls_table *f2fs_load_nls_table(int encoding);
1840 #define F2FS_ENC_UTF8_12_0	1
1841 
1842 extern int f2fs_str2encoding(const char *string);
1843 extern char *f2fs_encoding2str(const int encoding);
1844 extern int f2fs_get_encoding_flags(int encoding);
1845 extern int f2fs_str2encoding_flags(char **param, __u16 *flags);
1846 
1847 #endif	/*__F2FS_FS_H */
1848