1 /**
2 * f2fs_fs.h
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * Dual licensed under the GPL or LGPL version 2 licenses.
8 *
9 * The byteswap codes are copied from:
10 * samba_3_master/lib/ccan/endian/endian.h under LGPL 2.1
11 */
12 #ifndef __F2FS_FS_H__
13 #define __F2FS_FS_H__
14
15 #include <inttypes.h>
16 #include <linux/types.h>
17 #include <sys/types.h>
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #ifdef HAVE_LINUX_BLKZONED_H
24 #include <linux/blkzoned.h>
25 #endif
26
27 typedef u_int64_t u64;
28 typedef u_int32_t u32;
29 typedef u_int16_t u16;
30 typedef u_int8_t u8;
31 typedef u32 block_t;
32 typedef u32 nid_t;
33 typedef u8 bool;
34 typedef unsigned long pgoff_t;
35 typedef unsigned short umode_t;
36
37 #if HAVE_BYTESWAP_H
38 #include <byteswap.h>
39 #else
40 /**
41 * bswap_16 - reverse bytes in a uint16_t value.
42 * @val: value whose bytes to swap.
43 *
44 * Example:
45 * // Output contains "1024 is 4 as two bytes reversed"
46 * printf("1024 is %u as two bytes reversed\n", bswap_16(1024));
47 */
bswap_16(uint16_t val)48 static inline uint16_t bswap_16(uint16_t val)
49 {
50 return ((val & (uint16_t)0x00ffU) << 8)
51 | ((val & (uint16_t)0xff00U) >> 8);
52 }
53
54 /**
55 * bswap_32 - reverse bytes in a uint32_t value.
56 * @val: value whose bytes to swap.
57 *
58 * Example:
59 * // Output contains "1024 is 262144 as four bytes reversed"
60 * printf("1024 is %u as four bytes reversed\n", bswap_32(1024));
61 */
bswap_32(uint32_t val)62 static inline uint32_t bswap_32(uint32_t val)
63 {
64 return ((val & (uint32_t)0x000000ffUL) << 24)
65 | ((val & (uint32_t)0x0000ff00UL) << 8)
66 | ((val & (uint32_t)0x00ff0000UL) >> 8)
67 | ((val & (uint32_t)0xff000000UL) >> 24);
68 }
69 #endif /* !HAVE_BYTESWAP_H */
70
71 #if defined HAVE_DECL_BSWAP_64 && !HAVE_DECL_BSWAP_64
72 /**
73 * bswap_64 - reverse bytes in a uint64_t value.
74 * @val: value whose bytes to swap.
75 *
76 * Example:
77 * // Output contains "1024 is 1125899906842624 as eight bytes reversed"
78 * printf("1024 is %llu as eight bytes reversed\n",
79 * (unsigned long long)bswap_64(1024));
80 */
bswap_64(uint64_t val)81 static inline uint64_t bswap_64(uint64_t val)
82 {
83 return ((val & (uint64_t)0x00000000000000ffULL) << 56)
84 | ((val & (uint64_t)0x000000000000ff00ULL) << 40)
85 | ((val & (uint64_t)0x0000000000ff0000ULL) << 24)
86 | ((val & (uint64_t)0x00000000ff000000ULL) << 8)
87 | ((val & (uint64_t)0x000000ff00000000ULL) >> 8)
88 | ((val & (uint64_t)0x0000ff0000000000ULL) >> 24)
89 | ((val & (uint64_t)0x00ff000000000000ULL) >> 40)
90 | ((val & (uint64_t)0xff00000000000000ULL) >> 56);
91 }
92 #endif
93
94 #if __BYTE_ORDER == __LITTLE_ENDIAN
95 #define le16_to_cpu(x) ((__u16)(x))
96 #define le32_to_cpu(x) ((__u32)(x))
97 #define le64_to_cpu(x) ((__u64)(x))
98 #define cpu_to_le16(x) ((__u16)(x))
99 #define cpu_to_le32(x) ((__u32)(x))
100 #define cpu_to_le64(x) ((__u64)(x))
101 #elif __BYTE_ORDER == __BIG_ENDIAN
102 #define le16_to_cpu(x) bswap_16(x)
103 #define le32_to_cpu(x) bswap_32(x)
104 #define le64_to_cpu(x) bswap_64(x)
105 #define cpu_to_le16(x) bswap_16(x)
106 #define cpu_to_le32(x) bswap_32(x)
107 #define cpu_to_le64(x) bswap_64(x)
108 #endif
109
110 #define typecheck(type,x) \
111 ({ type __dummy; \
112 typeof(x) __dummy2; \
113 (void)(&__dummy == &__dummy2); \
114 1; \
115 })
116
117 #define NULL_SEGNO ((unsigned int)~0)
118
119 /*
120 * Debugging interfaces
121 */
122 #define FIX_MSG(fmt, ...) \
123 do { \
124 printf("[FIX] (%s:%4d) ", __func__, __LINE__); \
125 printf(" --> "fmt"\n", ##__VA_ARGS__); \
126 } while (0)
127
128 #define ASSERT_MSG(fmt, ...) \
129 do { \
130 printf("[ASSERT] (%s:%4d) ", __func__, __LINE__); \
131 printf(" --> "fmt"\n", ##__VA_ARGS__); \
132 c.bug_on = 1; \
133 } while (0)
134
135 #define ASSERT(exp) \
136 do { \
137 if (!(exp)) { \
138 printf("[ASSERT] (%s:%4d) " #exp"\n", \
139 __func__, __LINE__); \
140 exit(-1); \
141 } \
142 } while (0)
143
144 #define ERR_MSG(fmt, ...) \
145 do { \
146 printf("[%s:%d] " fmt, __func__, __LINE__, ##__VA_ARGS__); \
147 } while (0)
148
149 #define MSG(n, fmt, ...) \
150 do { \
151 if (c.dbg_lv >= n) { \
152 printf(fmt, ##__VA_ARGS__); \
153 } \
154 } while (0)
155
156 #define DBG(n, fmt, ...) \
157 do { \
158 if (c.dbg_lv >= n) { \
159 printf("[%s:%4d] " fmt, \
160 __func__, __LINE__, ##__VA_ARGS__); \
161 } \
162 } while (0)
163
164 /* Display on console */
165 #define DISP(fmt, ptr, member) \
166 do { \
167 printf("%-30s" fmt, #member, ((ptr)->member)); \
168 } while (0)
169
170 #define DISP_u32(ptr, member) \
171 do { \
172 assert(sizeof((ptr)->member) <= 4); \
173 printf("%-30s" "\t\t[0x%8x : %u]\n", \
174 #member, le32_to_cpu(((ptr)->member)), \
175 le32_to_cpu(((ptr)->member))); \
176 } while (0)
177
178 #define DISP_u64(ptr, member) \
179 do { \
180 assert(sizeof((ptr)->member) == 8); \
181 printf("%-30s" "\t\t[0x%8llx : %llu]\n", \
182 #member, le64_to_cpu(((ptr)->member)), \
183 le64_to_cpu(((ptr)->member))); \
184 } while (0)
185
186 #define DISP_utf(ptr, member) \
187 do { \
188 printf("%-30s" "\t\t[%s]\n", #member, ((ptr)->member)); \
189 } while (0)
190
191 /* Display to buffer */
192 #define BUF_DISP_u32(buf, data, len, ptr, member) \
193 do { \
194 assert(sizeof((ptr)->member) <= 4); \
195 snprintf(buf, len, #member); \
196 snprintf(data, len, "0x%x : %u", ((ptr)->member), \
197 ((ptr)->member)); \
198 } while (0)
199
200 #define BUF_DISP_u64(buf, data, len, ptr, member) \
201 do { \
202 assert(sizeof((ptr)->member) == 8); \
203 snprintf(buf, len, #member); \
204 snprintf(data, len, "0x%llx : %llu", ((ptr)->member), \
205 ((ptr)->member)); \
206 } while (0)
207
208 #define BUF_DISP_utf(buf, data, len, ptr, member) \
209 snprintf(buf, len, #member)
210
211 /* these are defined in kernel */
212 #define PAGE_SIZE 4096
213 #define PAGE_CACHE_SIZE 4096
214 #define BITS_PER_BYTE 8
215 #define F2FS_SUPER_MAGIC 0xF2F52010 /* F2FS Magic Number */
216 #define CHECKSUM_OFFSET 4092
217 #define MAX_PATH_LEN 64
218 #define MAX_DEVICES 8
219
220 #define F2FS_BYTES_TO_BLK(bytes) ((bytes) >> F2FS_BLKSIZE_BITS)
221 #define F2FS_BLKSIZE_BITS 12
222
223 /* for mkfs */
224 #define F2FS_NUMBER_OF_CHECKPOINT_PACK 2
225 #define DEFAULT_SECTOR_SIZE 512
226 #define DEFAULT_SECTORS_PER_BLOCK 8
227 #define DEFAULT_BLOCKS_PER_SEGMENT 512
228 #define DEFAULT_SEGMENTS_PER_SECTION 1
229
230 #define VERSION_LEN 256
231
232 enum f2fs_config_func {
233 FSCK,
234 DUMP,
235 DEFRAG,
236 RESIZE,
237 SLOAD,
238 };
239
240 struct device_info {
241 char *path;
242 int32_t fd;
243 u_int32_t sector_size;
244 u_int64_t total_sectors; /* got by get_device_info */
245 u_int64_t start_blkaddr;
246 u_int64_t end_blkaddr;
247 u_int32_t total_segments;
248
249 /* to handle zone block devices */
250 int zoned_model;
251 u_int32_t nr_zones;
252 u_int32_t nr_rnd_zones;
253 size_t zone_blocks;
254 };
255
256 struct f2fs_configuration {
257 u_int32_t reserved_segments;
258 u_int32_t new_reserved_segments;
259 int sparse_mode;
260 int zoned_mode;
261 int zoned_model;
262 size_t zone_blocks;
263 double overprovision;
264 double new_overprovision;
265 u_int32_t cur_seg[6];
266 u_int32_t segs_per_sec;
267 u_int32_t secs_per_zone;
268 u_int32_t segs_per_zone;
269 u_int32_t start_sector;
270 u_int32_t total_segments;
271 u_int32_t sector_size;
272 u_int64_t device_size;
273 u_int64_t total_sectors;
274 u_int64_t wanted_total_sectors;
275 u_int64_t target_sectors;
276 u_int32_t sectors_per_blk;
277 u_int32_t blks_per_seg;
278 __u8 init_version[VERSION_LEN + 1];
279 __u8 sb_version[VERSION_LEN + 1];
280 __u8 version[VERSION_LEN + 1];
281 char *vol_label;
282 int heap;
283 int32_t kd;
284 int32_t dump_fd;
285 struct device_info devices[MAX_DEVICES];
286 int ndevs;
287 char *extension_list;
288 const char *rootdev_name;
289 int dbg_lv;
290 int show_dentry;
291 int trim;
292 int trimmed;
293 int func;
294 void *private;
295 int fix_on;
296 int bug_on;
297 int auto_fix;
298 int preen_mode;
299 int ro;
300 __le32 feature; /* defined features */
301
302 /* defragmentation parameters */
303 int defrag_shrink;
304 u_int64_t defrag_start;
305 u_int64_t defrag_len;
306 u_int64_t defrag_target;
307
308 /* sload parameters */
309 char *from_dir;
310 char *mount_point;
311 };
312
313 #ifdef CONFIG_64BIT
314 #define BITS_PER_LONG 64
315 #else
316 #define BITS_PER_LONG 32
317 #endif
318
319 #define BIT_MASK(nr) (1 << (nr % BITS_PER_LONG))
320 #define BIT_WORD(nr) (nr / BITS_PER_LONG)
321
322 #define set_sb_le64(member, val) (sb->member = cpu_to_le64(val))
323 #define set_sb_le32(member, val) (sb->member = cpu_to_le32(val))
324 #define set_sb_le16(member, val) (sb->member = cpu_to_le16(val))
325 #define get_sb_le64(member) le64_to_cpu(sb->member)
326 #define get_sb_le32(member) le32_to_cpu(sb->member)
327 #define get_sb_le16(member) le16_to_cpu(sb->member)
328 #define get_newsb_le64(member) le64_to_cpu(new_sb->member)
329 #define get_newsb_le32(member) le32_to_cpu(new_sb->member)
330 #define get_newsb_le16(member) le16_to_cpu(new_sb->member)
331
332 #define set_sb(member, val) \
333 do { \
334 typeof(sb->member) t; \
335 switch (sizeof(t)) { \
336 case 8: set_sb_le64(member, val); break; \
337 case 4: set_sb_le32(member, val); break; \
338 case 2: set_sb_le16(member, val); break; \
339 } \
340 } while(0)
341
342 #define get_sb(member) \
343 ({ \
344 typeof(sb->member) t; \
345 switch (sizeof(t)) { \
346 case 8: t = get_sb_le64(member); break; \
347 case 4: t = get_sb_le32(member); break; \
348 case 2: t = get_sb_le16(member); break; \
349 } \
350 t; \
351 })
352 #define get_newsb(member) \
353 ({ \
354 typeof(new_sb->member) t; \
355 switch (sizeof(t)) { \
356 case 8: t = get_newsb_le64(member); break; \
357 case 4: t = get_newsb_le32(member); break; \
358 case 2: t = get_newsb_le16(member); break; \
359 } \
360 t; \
361 })
362
363 #define set_cp_le64(member, val) (cp->member = cpu_to_le64(val))
364 #define set_cp_le32(member, val) (cp->member = cpu_to_le32(val))
365 #define set_cp_le16(member, val) (cp->member = cpu_to_le16(val))
366 #define get_cp_le64(member) le64_to_cpu(cp->member)
367 #define get_cp_le32(member) le32_to_cpu(cp->member)
368 #define get_cp_le16(member) le16_to_cpu(cp->member)
369
370 #define set_cp(member, val) \
371 do { \
372 typeof(cp->member) t; \
373 switch (sizeof(t)) { \
374 case 8: set_cp_le64(member, val); break; \
375 case 4: set_cp_le32(member, val); break; \
376 case 2: set_cp_le16(member, val); break; \
377 } \
378 } while(0)
379
380 #define get_cp(member) \
381 ({ \
382 typeof(cp->member) t; \
383 switch (sizeof(t)) { \
384 case 8: t = get_cp_le64(member); break; \
385 case 4: t = get_cp_le32(member); break; \
386 case 2: t = get_cp_le16(member); break; \
387 } \
388 t; \
389 })
390
391 /*
392 * Copied from include/linux/kernel.h
393 */
394 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
395 #define round_down(x, y) ((x) & ~__round_mask(x, y))
396 #define min(x, y) ({ \
397 typeof(x) _min1 = (x); \
398 typeof(y) _min2 = (y); \
399 (void) (&_min1 == &_min2); \
400 _min1 < _min2 ? _min1 : _min2; })
401
402 #define max(x, y) ({ \
403 typeof(x) _max1 = (x); \
404 typeof(y) _max2 = (y); \
405 (void) (&_max1 == &_max2); \
406 _max1 > _max2 ? _max1 : _max2; })
407
408 /*
409 * Copied from fs/f2fs/f2fs.h
410 */
411 #define NR_CURSEG_DATA_TYPE (3)
412 #define NR_CURSEG_NODE_TYPE (3)
413 #define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
414
415 enum {
416 CURSEG_HOT_DATA = 0, /* directory entry blocks */
417 CURSEG_WARM_DATA, /* data blocks */
418 CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
419 CURSEG_HOT_NODE, /* direct node blocks of directory files */
420 CURSEG_WARM_NODE, /* direct node blocks of normal files */
421 CURSEG_COLD_NODE, /* indirect node blocks */
422 NO_CHECK_TYPE
423 };
424
425 #define F2FS_MIN_SEGMENTS 9 /* SB + 2 (CP + SIT + NAT) + SSA + MAIN */
426
427 /*
428 * Copied from fs/f2fs/segment.h
429 */
430 #define GET_SUM_TYPE(footer) ((footer)->entry_type)
431 #define SET_SUM_TYPE(footer, type) ((footer)->entry_type = type)
432
433 /*
434 * Copied from include/linux/f2fs_sb.h
435 */
436 #define F2FS_SUPER_OFFSET 1024 /* byte-size offset */
437 #define F2FS_MIN_LOG_SECTOR_SIZE 9 /* 9 bits for 512 bytes */
438 #define F2FS_MAX_LOG_SECTOR_SIZE 12 /* 12 bits for 4096 bytes */
439 #define F2FS_BLKSIZE 4096 /* support only 4KB block */
440 #define F2FS_MAX_EXTENSION 64 /* # of extension entries */
441 #define F2FS_BLK_ALIGN(x) (((x) + F2FS_BLKSIZE - 1) / F2FS_BLKSIZE)
442
443 #define NULL_ADDR 0x0U
444 #define NEW_ADDR -1U
445
446 #define F2FS_ROOT_INO(sbi) (sbi->root_ino_num)
447 #define F2FS_NODE_INO(sbi) (sbi->node_ino_num)
448 #define F2FS_META_INO(sbi) (sbi->meta_ino_num)
449
450 /* This flag is used by node and meta inodes, and by recovery */
451 #define GFP_F2FS_ZERO (GFP_NOFS | __GFP_ZERO)
452
453 /*
454 * For further optimization on multi-head logs, on-disk layout supports maximum
455 * 16 logs by default. The number, 16, is expected to cover all the cases
456 * enoughly. The implementaion currently uses no more than 6 logs.
457 * Half the logs are used for nodes, and the other half are used for data.
458 */
459 #define MAX_ACTIVE_LOGS 16
460 #define MAX_ACTIVE_NODE_LOGS 8
461 #define MAX_ACTIVE_DATA_LOGS 8
462
463 #define F2FS_FEATURE_ENCRYPT 0x0001
464 #define F2FS_FEATURE_BLKZONED 0x0002
465
466 #define MAX_VOLUME_NAME 512
467
468 /*
469 * For superblock
470 */
471 struct f2fs_device {
472 __u8 path[MAX_PATH_LEN];
473 __le32 total_segments;
474 } __attribute__((packed));
475
476 struct f2fs_super_block {
477 __le32 magic; /* Magic Number */
478 __le16 major_ver; /* Major Version */
479 __le16 minor_ver; /* Minor Version */
480 __le32 log_sectorsize; /* log2 sector size in bytes */
481 __le32 log_sectors_per_block; /* log2 # of sectors per block */
482 __le32 log_blocksize; /* log2 block size in bytes */
483 __le32 log_blocks_per_seg; /* log2 # of blocks per segment */
484 __le32 segs_per_sec; /* # of segments per section */
485 __le32 secs_per_zone; /* # of sections per zone */
486 __le32 checksum_offset; /* checksum offset inside super block */
487 __le64 block_count; /* total # of user blocks */
488 __le32 section_count; /* total # of sections */
489 __le32 segment_count; /* total # of segments */
490 __le32 segment_count_ckpt; /* # of segments for checkpoint */
491 __le32 segment_count_sit; /* # of segments for SIT */
492 __le32 segment_count_nat; /* # of segments for NAT */
493 __le32 segment_count_ssa; /* # of segments for SSA */
494 __le32 segment_count_main; /* # of segments for main area */
495 __le32 segment0_blkaddr; /* start block address of segment 0 */
496 __le32 cp_blkaddr; /* start block address of checkpoint */
497 __le32 sit_blkaddr; /* start block address of SIT */
498 __le32 nat_blkaddr; /* start block address of NAT */
499 __le32 ssa_blkaddr; /* start block address of SSA */
500 __le32 main_blkaddr; /* start block address of main area */
501 __le32 root_ino; /* root inode number */
502 __le32 node_ino; /* node inode number */
503 __le32 meta_ino; /* meta inode number */
504 __u8 uuid[16]; /* 128-bit uuid for volume */
505 __le16 volume_name[MAX_VOLUME_NAME]; /* volume name */
506 __le32 extension_count; /* # of extensions below */
507 __u8 extension_list[F2FS_MAX_EXTENSION][8]; /* extension array */
508 __le32 cp_payload;
509 __u8 version[VERSION_LEN]; /* the kernel version */
510 __u8 init_version[VERSION_LEN]; /* the initial kernel version */
511 __le32 feature; /* defined features */
512 __u8 encryption_level; /* versioning level for encryption */
513 __u8 encrypt_pw_salt[16]; /* Salt used for string2key algorithm */
514 struct f2fs_device devs[MAX_DEVICES]; /* device list */
515 __u8 reserved[327]; /* valid reserved region */
516 } __attribute__((packed));
517
518 /*
519 * For checkpoint
520 */
521 #define CP_TRIMMED_FLAG 0x00000100
522 #define CP_NAT_BITS_FLAG 0x00000080
523 #define CP_CRC_RECOVERY_FLAG 0x00000040
524 #define CP_FASTBOOT_FLAG 0x00000020
525 #define CP_FSCK_FLAG 0x00000010
526 #define CP_ERROR_FLAG 0x00000008
527 #define CP_COMPACT_SUM_FLAG 0x00000004
528 #define CP_ORPHAN_PRESENT_FLAG 0x00000002
529 #define CP_UMOUNT_FLAG 0x00000001
530
531 struct f2fs_checkpoint {
532 __le64 checkpoint_ver; /* checkpoint block version number */
533 __le64 user_block_count; /* # of user blocks */
534 __le64 valid_block_count; /* # of valid blocks in main area */
535 __le32 rsvd_segment_count; /* # of reserved segments for gc */
536 __le32 overprov_segment_count; /* # of overprovision segments */
537 __le32 free_segment_count; /* # of free segments in main area */
538
539 /* information of current node segments */
540 __le32 cur_node_segno[MAX_ACTIVE_NODE_LOGS];
541 __le16 cur_node_blkoff[MAX_ACTIVE_NODE_LOGS];
542 /* information of current data segments */
543 __le32 cur_data_segno[MAX_ACTIVE_DATA_LOGS];
544 __le16 cur_data_blkoff[MAX_ACTIVE_DATA_LOGS];
545 __le32 ckpt_flags; /* Flags : umount and journal_present */
546 __le32 cp_pack_total_block_count; /* total # of one cp pack */
547 __le32 cp_pack_start_sum; /* start block number of data summary */
548 __le32 valid_node_count; /* Total number of valid nodes */
549 __le32 valid_inode_count; /* Total number of valid inodes */
550 __le32 next_free_nid; /* Next free node number */
551 __le32 sit_ver_bitmap_bytesize; /* Default value 64 */
552 __le32 nat_ver_bitmap_bytesize; /* Default value 256 */
553 __le32 checksum_offset; /* checksum offset inside cp block */
554 __le64 elapsed_time; /* mounted time */
555 /* allocation type of current segment */
556 unsigned char alloc_type[MAX_ACTIVE_LOGS];
557
558 /* SIT and NAT version bitmap */
559 unsigned char sit_nat_version_bitmap[1];
560 } __attribute__((packed));
561
562 /*
563 * For orphan inode management
564 */
565 #define F2FS_ORPHANS_PER_BLOCK 1020
566
567 struct f2fs_orphan_block {
568 __le32 ino[F2FS_ORPHANS_PER_BLOCK]; /* inode numbers */
569 __le32 reserved; /* reserved */
570 __le16 blk_addr; /* block index in current CP */
571 __le16 blk_count; /* Number of orphan inode blocks in CP */
572 __le32 entry_count; /* Total number of orphan nodes in current CP */
573 __le32 check_sum; /* CRC32 for orphan inode block */
574 } __attribute__((packed));
575
576 /*
577 * For NODE structure
578 */
579 struct f2fs_extent {
580 __le32 fofs; /* start file offset of the extent */
581 __le32 blk_addr; /* start block address of the extent */
582 __le32 len; /* lengh of the extent */
583 } __attribute__((packed));
584
585 #define F2FS_NAME_LEN 255
586 #define F2FS_INLINE_XATTR_ADDRS 50 /* 200 bytes for inline xattrs */
587 #define DEF_ADDRS_PER_INODE 923 /* Address Pointers in an Inode */
588 #define ADDRS_PER_INODE(i) addrs_per_inode(i)
589 #define DEF_ADDRS_PER_INODE_INLINE_XATTR \
590 (DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS)
591 #define ADDRS_PER_BLOCK 1018 /* Address Pointers in a Direct Block */
592 #define NIDS_PER_BLOCK 1018 /* Node IDs in an Indirect Block */
593
594 #define NODE_DIR1_BLOCK (DEF_ADDRS_PER_INODE + 1)
595 #define NODE_DIR2_BLOCK (DEF_ADDRS_PER_INODE + 2)
596 #define NODE_IND1_BLOCK (DEF_ADDRS_PER_INODE + 3)
597 #define NODE_IND2_BLOCK (DEF_ADDRS_PER_INODE + 4)
598 #define NODE_DIND_BLOCK (DEF_ADDRS_PER_INODE + 5)
599
600 #define F2FS_INLINE_XATTR 0x01 /* file inline xattr flag */
601 #define F2FS_INLINE_DATA 0x02 /* file inline data flag */
602 #define F2FS_INLINE_DENTRY 0x04 /* file inline dentry flag */
603 #define F2FS_DATA_EXIST 0x08 /* file inline data exist flag */
604 #define F2FS_INLINE_DOTS 0x10 /* file having implicit dot dentries */
605
606 #define MAX_INLINE_DATA (sizeof(__le32) * \
607 (DEF_ADDRS_PER_INODE_INLINE_XATTR - 1))
608
609 #define INLINE_DATA_OFFSET (PAGE_CACHE_SIZE - sizeof(struct node_footer) \
610 - sizeof(__le32)*(DEF_ADDRS_PER_INODE + 5 - 1))
611
612 #define DEF_DIR_LEVEL 0
613
614 /*
615 * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
616 */
617 #define FADVISE_COLD_BIT 0x01
618 #define FADVISE_LOST_PINO_BIT 0x02
619 #define FADVISE_ENCRYPT_BIT 0x04
620 #define FADVISE_ENC_NAME_BIT 0x08
621
622 #define file_is_encrypt(fi) ((fi)->i_advise & FADVISE_ENCRYPT_BIT)
623 #define file_enc_name(fi) ((fi)->i_advise & FADVISE_ENC_NAME_BIT)
624
625 struct f2fs_inode {
626 __le16 i_mode; /* file mode */
627 __u8 i_advise; /* file hints */
628 __u8 i_inline; /* file inline flags */
629 __le32 i_uid; /* user ID */
630 __le32 i_gid; /* group ID */
631 __le32 i_links; /* links count */
632 __le64 i_size; /* file size in bytes */
633 __le64 i_blocks; /* file size in blocks */
634 __le64 i_atime; /* access time */
635 __le64 i_ctime; /* change time */
636 __le64 i_mtime; /* modification time */
637 __le32 i_atime_nsec; /* access time in nano scale */
638 __le32 i_ctime_nsec; /* change time in nano scale */
639 __le32 i_mtime_nsec; /* modification time in nano scale */
640 __le32 i_generation; /* file version (for NFS) */
641 __le32 i_current_depth; /* only for directory depth */
642 __le32 i_xattr_nid; /* nid to save xattr */
643 __le32 i_flags; /* file attributes */
644 __le32 i_pino; /* parent inode number */
645 __le32 i_namelen; /* file name length */
646 __u8 i_name[F2FS_NAME_LEN]; /* file name for SPOR */
647 __u8 i_dir_level; /* dentry_level for large dir */
648
649 struct f2fs_extent i_ext; /* caching a largest extent */
650
651 __le32 i_addr[DEF_ADDRS_PER_INODE]; /* Pointers to data blocks */
652
653 __le32 i_nid[5]; /* direct(2), indirect(2),
654 double_indirect(1) node id */
655 } __attribute__((packed));
656
657 struct direct_node {
658 __le32 addr[ADDRS_PER_BLOCK]; /* array of data block address */
659 } __attribute__((packed));
660
661 struct indirect_node {
662 __le32 nid[NIDS_PER_BLOCK]; /* array of data block address */
663 } __attribute__((packed));
664
665 enum {
666 COLD_BIT_SHIFT = 0,
667 FSYNC_BIT_SHIFT,
668 DENT_BIT_SHIFT,
669 OFFSET_BIT_SHIFT
670 };
671
672 #define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
673 >> OFFSET_BIT_SHIFT)
674
675 struct node_footer {
676 __le32 nid; /* node id */
677 __le32 ino; /* inode nunmber */
678 __le32 flag; /* include cold/fsync/dentry marks and offset */
679 __le64 cp_ver; /* checkpoint version */
680 __le32 next_blkaddr; /* next node page block address */
681 } __attribute__((packed));
682
683 struct f2fs_node {
684 /* can be one of three types: inode, direct, and indirect types */
685 union {
686 struct f2fs_inode i;
687 struct direct_node dn;
688 struct indirect_node in;
689 };
690 struct node_footer footer;
691 } __attribute__((packed));
692
693 /*
694 * For NAT entries
695 */
696 #define NAT_ENTRY_PER_BLOCK (PAGE_CACHE_SIZE / sizeof(struct f2fs_nat_entry))
697 #define NAT_BLOCK_OFFSET(start_nid) (start_nid / NAT_ENTRY_PER_BLOCK)
698
699 struct f2fs_nat_entry {
700 __u8 version; /* latest version of cached nat entry */
701 __le32 ino; /* inode number */
702 __le32 block_addr; /* block address */
703 } __attribute__((packed));
704
705 struct f2fs_nat_block {
706 struct f2fs_nat_entry entries[NAT_ENTRY_PER_BLOCK];
707 } __attribute__((packed));
708
709 /*
710 * For SIT entries
711 *
712 * Each segment is 2MB in size by default so that a bitmap for validity of
713 * there-in blocks should occupy 64 bytes, 512 bits.
714 * Not allow to change this.
715 */
716 #define SIT_VBLOCK_MAP_SIZE 64
717 #define SIT_ENTRY_PER_BLOCK (PAGE_CACHE_SIZE / sizeof(struct f2fs_sit_entry))
718
719 /*
720 * F2FS uses 4 bytes to represent block address. As a result, supported size of
721 * disk is 16 TB and it equals to 16 * 1024 * 1024 / 2 segments.
722 */
723 #define F2FS_MAX_SEGMENT ((16 * 1024 * 1024) / 2)
724 #define MAX_SIT_BITMAP_SIZE (SEG_ALIGN(ALIGN(F2FS_MAX_SEGMENT, \
725 SIT_ENTRY_PER_BLOCK)) * \
726 c.blks_per_seg / 8)
727
728 /*
729 * Note that f2fs_sit_entry->vblocks has the following bit-field information.
730 * [15:10] : allocation type such as CURSEG_XXXX_TYPE
731 * [9:0] : valid block count
732 */
733 #define SIT_VBLOCKS_SHIFT 10
734 #define SIT_VBLOCKS_MASK ((1 << SIT_VBLOCKS_SHIFT) - 1)
735 #define GET_SIT_VBLOCKS(raw_sit) \
736 (le16_to_cpu((raw_sit)->vblocks) & SIT_VBLOCKS_MASK)
737 #define GET_SIT_TYPE(raw_sit) \
738 ((le16_to_cpu((raw_sit)->vblocks) & ~SIT_VBLOCKS_MASK) \
739 >> SIT_VBLOCKS_SHIFT)
740
741 struct f2fs_sit_entry {
742 __le16 vblocks; /* reference above */
743 __u8 valid_map[SIT_VBLOCK_MAP_SIZE]; /* bitmap for valid blocks */
744 __le64 mtime; /* segment age for cleaning */
745 } __attribute__((packed));
746
747 struct f2fs_sit_block {
748 struct f2fs_sit_entry entries[SIT_ENTRY_PER_BLOCK];
749 } __attribute__((packed));
750
751 /*
752 * For segment summary
753 *
754 * One summary block contains exactly 512 summary entries, which represents
755 * exactly 2MB segment by default. Not allow to change the basic units.
756 *
757 * NOTE: For initializing fields, you must use set_summary
758 *
759 * - If data page, nid represents dnode's nid
760 * - If node page, nid represents the node page's nid.
761 *
762 * The ofs_in_node is used by only data page. It represents offset
763 * from node's page's beginning to get a data block address.
764 * ex) data_blkaddr = (block_t)(nodepage_start_address + ofs_in_node)
765 */
766 #define ENTRIES_IN_SUM 512
767 #define SUMMARY_SIZE (7) /* sizeof(struct summary) */
768 #define SUM_FOOTER_SIZE (5) /* sizeof(struct summary_footer) */
769 #define SUM_ENTRIES_SIZE (SUMMARY_SIZE * ENTRIES_IN_SUM)
770
771 /* a summary entry for a 4KB-sized block in a segment */
772 struct f2fs_summary {
773 __le32 nid; /* parent node id */
774 union {
775 __u8 reserved[3];
776 struct {
777 __u8 version; /* node version number */
778 __le16 ofs_in_node; /* block index in parent node */
779 } __attribute__((packed));
780 };
781 } __attribute__((packed));
782
783 /* summary block type, node or data, is stored to the summary_footer */
784 #define SUM_TYPE_NODE (1)
785 #define SUM_TYPE_DATA (0)
786
787 struct summary_footer {
788 unsigned char entry_type; /* SUM_TYPE_XXX */
789 __le32 check_sum; /* summary checksum */
790 } __attribute__((packed));
791
792 #define SUM_JOURNAL_SIZE (F2FS_BLKSIZE - SUM_FOOTER_SIZE -\
793 SUM_ENTRIES_SIZE)
794 #define NAT_JOURNAL_ENTRIES ((SUM_JOURNAL_SIZE - 2) /\
795 sizeof(struct nat_journal_entry))
796 #define NAT_JOURNAL_RESERVED ((SUM_JOURNAL_SIZE - 2) %\
797 sizeof(struct nat_journal_entry))
798 #define SIT_JOURNAL_ENTRIES ((SUM_JOURNAL_SIZE - 2) /\
799 sizeof(struct sit_journal_entry))
800 #define SIT_JOURNAL_RESERVED ((SUM_JOURNAL_SIZE - 2) %\
801 sizeof(struct sit_journal_entry))
802
803 /*
804 * Reserved area should make size of f2fs_extra_info equals to
805 * that of nat_journal and sit_journal.
806 */
807 #define EXTRA_INFO_RESERVED (SUM_JOURNAL_SIZE - 2 - 8)
808
809 /*
810 * frequently updated NAT/SIT entries can be stored in the spare area in
811 * summary blocks
812 */
813 enum {
814 NAT_JOURNAL = 0,
815 SIT_JOURNAL
816 };
817
818 struct nat_journal_entry {
819 __le32 nid;
820 struct f2fs_nat_entry ne;
821 } __attribute__((packed));
822
823 struct nat_journal {
824 struct nat_journal_entry entries[NAT_JOURNAL_ENTRIES];
825 __u8 reserved[NAT_JOURNAL_RESERVED];
826 } __attribute__((packed));
827
828 struct sit_journal_entry {
829 __le32 segno;
830 struct f2fs_sit_entry se;
831 } __attribute__((packed));
832
833 struct sit_journal {
834 struct sit_journal_entry entries[SIT_JOURNAL_ENTRIES];
835 __u8 reserved[SIT_JOURNAL_RESERVED];
836 } __attribute__((packed));
837
838 struct f2fs_extra_info {
839 __le64 kbytes_written;
840 __u8 reserved[EXTRA_INFO_RESERVED];
841 } __attribute__((packed));
842
843 struct f2fs_journal {
844 union {
845 __le16 n_nats;
846 __le16 n_sits;
847 };
848 /* spare area is used by NAT or SIT journals or extra info */
849 union {
850 struct nat_journal nat_j;
851 struct sit_journal sit_j;
852 struct f2fs_extra_info info;
853 };
854 } __attribute__((packed));
855
856 /* 4KB-sized summary block structure */
857 struct f2fs_summary_block {
858 struct f2fs_summary entries[ENTRIES_IN_SUM];
859 struct f2fs_journal journal;
860 struct summary_footer footer;
861 } __attribute__((packed));
862
863 /*
864 * For directory operations
865 */
866 #define F2FS_DOT_HASH 0
867 #define F2FS_DDOT_HASH F2FS_DOT_HASH
868 #define F2FS_MAX_HASH (~((0x3ULL) << 62))
869 #define F2FS_HASH_COL_BIT ((0x1ULL) << 63)
870
871 typedef __le32 f2fs_hash_t;
872
873 /* One directory entry slot covers 8bytes-long file name */
874 #define F2FS_SLOT_LEN 8
875 #define F2FS_SLOT_LEN_BITS 3
876
877 #define GET_DENTRY_SLOTS(x) ((x + F2FS_SLOT_LEN - 1) >> F2FS_SLOT_LEN_BITS)
878
879 /* the number of dentry in a block */
880 #define NR_DENTRY_IN_BLOCK 214
881
882 /* MAX level for dir lookup */
883 #define MAX_DIR_HASH_DEPTH 63
884
885 /* MAX buckets in one level of dir */
886 #define MAX_DIR_BUCKETS (1 << ((MAX_DIR_HASH_DEPTH / 2) - 1))
887
888 #define SIZE_OF_DIR_ENTRY 11 /* by byte */
889 #define SIZE_OF_DENTRY_BITMAP ((NR_DENTRY_IN_BLOCK + BITS_PER_BYTE - 1) / \
890 BITS_PER_BYTE)
891 #define SIZE_OF_RESERVED (PAGE_SIZE - ((SIZE_OF_DIR_ENTRY + \
892 F2FS_SLOT_LEN) * \
893 NR_DENTRY_IN_BLOCK + SIZE_OF_DENTRY_BITMAP))
894
895 /* One directory entry slot representing F2FS_SLOT_LEN-sized file name */
896 struct f2fs_dir_entry {
897 __le32 hash_code; /* hash code of file name */
898 __le32 ino; /* inode number */
899 __le16 name_len; /* lengh of file name */
900 __u8 file_type; /* file type */
901 } __attribute__((packed));
902
903 /* 4KB-sized directory entry block */
904 struct f2fs_dentry_block {
905 /* validity bitmap for directory entries in each block */
906 __u8 dentry_bitmap[SIZE_OF_DENTRY_BITMAP];
907 __u8 reserved[SIZE_OF_RESERVED];
908 struct f2fs_dir_entry dentry[NR_DENTRY_IN_BLOCK];
909 __u8 filename[NR_DENTRY_IN_BLOCK][F2FS_SLOT_LEN];
910 } __attribute__((packed));
911
912 /* for inline dir */
913 #define NR_INLINE_DENTRY (MAX_INLINE_DATA * BITS_PER_BYTE / \
914 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
915 BITS_PER_BYTE + 1))
916 #define INLINE_DENTRY_BITMAP_SIZE ((NR_INLINE_DENTRY + \
917 BITS_PER_BYTE - 1) / BITS_PER_BYTE)
918 #define INLINE_RESERVED_SIZE (MAX_INLINE_DATA - \
919 ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
920 NR_INLINE_DENTRY + INLINE_DENTRY_BITMAP_SIZE))
921
922 /* inline directory entry structure */
923 struct f2fs_inline_dentry {
924 __u8 dentry_bitmap[INLINE_DENTRY_BITMAP_SIZE];
925 __u8 reserved[INLINE_RESERVED_SIZE];
926 struct f2fs_dir_entry dentry[NR_INLINE_DENTRY];
927 __u8 filename[NR_INLINE_DENTRY][F2FS_SLOT_LEN];
928 } __attribute__((packed));
929
930 /* file types used in inode_info->flags */
931 enum FILE_TYPE {
932 F2FS_FT_UNKNOWN,
933 F2FS_FT_REG_FILE,
934 F2FS_FT_DIR,
935 F2FS_FT_CHRDEV,
936 F2FS_FT_BLKDEV,
937 F2FS_FT_FIFO,
938 F2FS_FT_SOCK,
939 F2FS_FT_SYMLINK,
940 F2FS_FT_MAX,
941 /* added for fsck */
942 F2FS_FT_ORPHAN,
943 F2FS_FT_XATTR,
944 F2FS_FT_LAST_FILE_TYPE = F2FS_FT_XATTR,
945 };
946
947 /* from f2fs/segment.h */
948 enum {
949 LFS = 0,
950 SSR
951 };
952
953 extern int utf8_to_utf16(u_int16_t *, const char *, size_t, size_t);
954 extern int utf16_to_utf8(char *, const u_int16_t *, size_t, size_t);
955 extern int log_base_2(u_int32_t);
956 extern unsigned int addrs_per_inode(struct f2fs_inode *);
957
958 extern int get_bits_in_byte(unsigned char n);
959 extern int test_and_set_bit_le(u32, u8 *);
960 extern int test_and_clear_bit_le(u32, u8 *);
961 extern int test_bit_le(u32, const u8 *);
962 extern int f2fs_test_bit(unsigned int, const char *);
963 extern int f2fs_set_bit(unsigned int, char *);
964 extern int f2fs_clear_bit(unsigned int, char *);
965 extern u64 find_next_bit_le(const u8 *, u64, u64);
966 extern u64 find_next_zero_bit_le(const u8 *, u64, u64);
967
968 extern u_int32_t f2fs_cal_crc32(u_int32_t, void *, int);
969 extern int f2fs_crc_valid(u_int32_t blk_crc, void *buf, int len);
970
971 extern void f2fs_init_configuration(void);
972 extern int f2fs_devs_are_umounted(void);
973 extern int f2fs_dev_is_umounted(char *);
974 extern int f2fs_get_device_info(void);
975 extern int get_device_info(int);
976 extern void f2fs_finalize_device(void);
977
978 extern int dev_read(void *, __u64, size_t);
979 extern int dev_write(void *, __u64, size_t);
980 extern int dev_write_block(void *, __u64);
981 extern int dev_write_dump(void *, __u64, size_t);
982 /* All bytes in the buffer must be 0 use dev_fill(). */
983 extern int dev_fill(void *, __u64, size_t);
984 extern int dev_fill_block(void *, __u64);
985
986 extern int dev_read_block(void *, __u64);
987 extern int dev_reada_block(__u64);
988
989 extern int dev_read_version(void *, __u64, size_t);
990 extern void get_kernel_version(__u8 *);
991 f2fs_hash_t f2fs_dentry_hash(const unsigned char *, int);
992
993 #define F2FS_ZONED_NONE 0
994 #define F2FS_ZONED_HA 1
995 #define F2FS_ZONED_HM 2
996
997 #ifdef HAVE_LINUX_BLKZONED_H
998
999 #define blk_zone_type(z) (z)->type
1000 #define blk_zone_conv(z) ((z)->type == BLK_ZONE_TYPE_CONVENTIONAL)
1001 #define blk_zone_seq_req(z) ((z)->type == BLK_ZONE_TYPE_SEQWRITE_REQ)
1002 #define blk_zone_seq_pref(z) ((z)->type == BLK_ZONE_TYPE_SEQWRITE_PREF)
1003 #define blk_zone_seq(z) (blk_zone_seq_req(z) || blk_zone_seq_pref(z))
1004
1005 static inline const char *
blk_zone_type_str(struct blk_zone * blkz)1006 blk_zone_type_str(struct blk_zone *blkz)
1007 {
1008 switch (blk_zone_type(blkz)) {
1009 case BLK_ZONE_TYPE_CONVENTIONAL:
1010 return( "Conventional" );
1011 case BLK_ZONE_TYPE_SEQWRITE_REQ:
1012 return( "Sequential-write-required" );
1013 case BLK_ZONE_TYPE_SEQWRITE_PREF:
1014 return( "Sequential-write-preferred" );
1015 }
1016 return( "Unknown-type" );
1017 }
1018
1019 #define blk_zone_cond(z) (z)->cond
1020
1021 static inline const char *
blk_zone_cond_str(struct blk_zone * blkz)1022 blk_zone_cond_str(struct blk_zone *blkz)
1023 {
1024 switch (blk_zone_cond(blkz)) {
1025 case BLK_ZONE_COND_NOT_WP:
1026 return "Not-write-pointer";
1027 case BLK_ZONE_COND_EMPTY:
1028 return "Empty";
1029 case BLK_ZONE_COND_IMP_OPEN:
1030 return "Implicit-open";
1031 case BLK_ZONE_COND_EXP_OPEN:
1032 return "Explicit-open";
1033 case BLK_ZONE_COND_CLOSED:
1034 return "Closed";
1035 case BLK_ZONE_COND_READONLY:
1036 return "Read-only";
1037 case BLK_ZONE_COND_FULL:
1038 return "Full";
1039 case BLK_ZONE_COND_OFFLINE:
1040 return "Offline";
1041 }
1042 return "Unknown-cond";
1043 }
1044
1045 #define blk_zone_empty(z) (blk_zone_cond(z) == BLK_ZONE_COND_EMPTY)
1046
1047 #define blk_zone_sector(z) (z)->start
1048 #define blk_zone_length(z) (z)->len
1049 #define blk_zone_wp_sector(z) (z)->wp
1050 #define blk_zone_need_reset(z) (int)(z)->reset
1051 #define blk_zone_non_seq(z) (int)(z)->non_seq
1052
1053 #endif
1054
1055 extern void f2fs_get_zoned_model(int);
1056 extern int f2fs_get_zone_blocks(int);
1057 extern int f2fs_check_zones(int);
1058 extern int f2fs_reset_zones(int);
1059
1060 extern struct f2fs_configuration c;
1061
1062 #define ALIGN(val, size) ((val) + (size) - 1) / (size)
1063 #define SEG_ALIGN(blks) ALIGN(blks, c.blks_per_seg)
1064 #define ZONE_ALIGN(blks) ALIGN(blks, c.blks_per_seg * \
1065 c.segs_per_zone)
1066
get_best_overprovision(struct f2fs_super_block * sb)1067 static inline double get_best_overprovision(struct f2fs_super_block *sb)
1068 {
1069 double reserved, ovp, candidate, end, diff, space;
1070 double max_ovp = 0, max_space = 0;
1071
1072 if (get_sb(segment_count_main) < 256) {
1073 candidate = 10;
1074 end = 95;
1075 diff = 5;
1076 } else {
1077 candidate = 0.01;
1078 end = 10;
1079 diff = 0.01;
1080 }
1081
1082 for (; candidate <= end; candidate += diff) {
1083 reserved = (2 * (100 / candidate + 1) + 6) *
1084 get_sb(segs_per_sec);
1085 ovp = (get_sb(segment_count_main) - reserved) * candidate / 100;
1086 space = get_sb(segment_count_main) - reserved - ovp;
1087 if (max_space < space) {
1088 max_space = space;
1089 max_ovp = candidate;
1090 }
1091 }
1092 return max_ovp;
1093 }
1094
get_cp_crc(struct f2fs_checkpoint * cp)1095 static inline __le64 get_cp_crc(struct f2fs_checkpoint *cp)
1096 {
1097 u_int64_t cp_ver = get_cp(checkpoint_ver);
1098 size_t crc_offset = get_cp(checksum_offset);
1099 u_int32_t crc = le32_to_cpu(*(__le32 *)((unsigned char *)cp +
1100 crc_offset));
1101
1102 cp_ver |= ((u_int64_t)crc << 32);
1103 return cpu_to_le64(cp_ver);
1104 }
1105
1106 #endif /*__F2FS_FS_H */
1107