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