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