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