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