1
2
3
4
5
6
7
8
9
10
11#ifndef _LINUX_F2FS_FS_H
12#define _LINUX_F2FS_FS_H
13
14#include <linux/pagemap.h>
15#include <linux/types.h>
16
17#define F2FS_SUPER_OFFSET 1024
18#define F2FS_MIN_LOG_SECTOR_SIZE 9
19#define F2FS_MAX_LOG_SECTOR_SIZE 12
20#define F2FS_LOG_SECTORS_PER_BLOCK 3
21#define F2FS_BLKSIZE 4096
22#define F2FS_BLKSIZE_BITS 12
23#define F2FS_MAX_EXTENSION 64
24#define F2FS_EXTENSION_LEN 8
25#define F2FS_BLK_ALIGN(x) (((x) + F2FS_BLKSIZE - 1) >> F2FS_BLKSIZE_BITS)
26
27#define NULL_ADDR ((block_t)0)
28#define NEW_ADDR ((block_t)-1)
29
30#define F2FS_BYTES_TO_BLK(bytes) ((bytes) >> F2FS_BLKSIZE_BITS)
31#define F2FS_BLK_TO_BYTES(blk) ((blk) << F2FS_BLKSIZE_BITS)
32
33
34#define F2FS_RESERVED_NODE_NUM 3
35
36#define F2FS_ROOT_INO(sbi) ((sbi)->root_ino_num)
37#define F2FS_NODE_INO(sbi) ((sbi)->node_ino_num)
38#define F2FS_META_INO(sbi) ((sbi)->meta_ino_num)
39
40#define F2FS_MAX_QUOTAS 3
41
42#define F2FS_IO_SIZE(sbi) (1 << F2FS_OPTION(sbi).write_io_size_bits)
43#define F2FS_IO_SIZE_KB(sbi) (1 << (F2FS_OPTION(sbi).write_io_size_bits + 2))
44#define F2FS_IO_SIZE_BYTES(sbi) (1 << (F2FS_OPTION(sbi).write_io_size_bits + 12))
45#define F2FS_IO_SIZE_BITS(sbi) (F2FS_OPTION(sbi).write_io_size_bits)
46#define F2FS_IO_SIZE_MASK(sbi) (F2FS_IO_SIZE(sbi) - 1)
47
48
49#define GFP_F2FS_ZERO (GFP_NOFS | __GFP_ZERO)
50
51
52
53
54
55
56
57#define MAX_ACTIVE_LOGS 16
58#define MAX_ACTIVE_NODE_LOGS 8
59#define MAX_ACTIVE_DATA_LOGS 8
60
61#define VERSION_LEN 256
62#define MAX_VOLUME_NAME 512
63#define MAX_PATH_LEN 64
64#define MAX_DEVICES 8
65
66
67
68
69struct f2fs_device {
70 __u8 path[MAX_PATH_LEN];
71 __le32 total_segments;
72} __packed;
73
74struct f2fs_super_block {
75 __le32 magic;
76 __le16 major_ver;
77 __le16 minor_ver;
78 __le32 log_sectorsize;
79 __le32 log_sectors_per_block;
80 __le32 log_blocksize;
81 __le32 log_blocks_per_seg;
82 __le32 segs_per_sec;
83 __le32 secs_per_zone;
84 __le32 checksum_offset;
85 __le64 block_count;
86 __le32 section_count;
87 __le32 segment_count;
88 __le32 segment_count_ckpt;
89 __le32 segment_count_sit;
90 __le32 segment_count_nat;
91 __le32 segment_count_ssa;
92 __le32 segment_count_main;
93 __le32 segment0_blkaddr;
94 __le32 cp_blkaddr;
95 __le32 sit_blkaddr;
96 __le32 nat_blkaddr;
97 __le32 ssa_blkaddr;
98 __le32 main_blkaddr;
99 __le32 root_ino;
100 __le32 node_ino;
101 __le32 meta_ino;
102 __u8 uuid[16];
103 __le16 volume_name[MAX_VOLUME_NAME];
104 __le32 extension_count;
105 __u8 extension_list[F2FS_MAX_EXTENSION][F2FS_EXTENSION_LEN];
106 __le32 cp_payload;
107 __u8 version[VERSION_LEN];
108 __u8 init_version[VERSION_LEN];
109 __le32 feature;
110 __u8 encryption_level;
111 __u8 encrypt_pw_salt[16];
112 struct f2fs_device devs[MAX_DEVICES];
113 __le32 qf_ino[F2FS_MAX_QUOTAS];
114 __u8 hot_ext_count;
115 __u8 reserved[314];
116} __packed;
117
118
119
120
121#define CP_LARGE_NAT_BITMAP_FLAG 0x00000400
122#define CP_NOCRC_RECOVERY_FLAG 0x00000200
123#define CP_TRIMMED_FLAG 0x00000100
124#define CP_NAT_BITS_FLAG 0x00000080
125#define CP_CRC_RECOVERY_FLAG 0x00000040
126#define CP_FASTBOOT_FLAG 0x00000020
127#define CP_FSCK_FLAG 0x00000010
128#define CP_ERROR_FLAG 0x00000008
129#define CP_COMPACT_SUM_FLAG 0x00000004
130#define CP_ORPHAN_PRESENT_FLAG 0x00000002
131#define CP_UMOUNT_FLAG 0x00000001
132
133#define F2FS_CP_PACKS 2
134
135struct f2fs_checkpoint {
136 __le64 checkpoint_ver;
137 __le64 user_block_count;
138 __le64 valid_block_count;
139 __le32 rsvd_segment_count;
140 __le32 overprov_segment_count;
141 __le32 free_segment_count;
142
143
144 __le32 cur_node_segno[MAX_ACTIVE_NODE_LOGS];
145 __le16 cur_node_blkoff[MAX_ACTIVE_NODE_LOGS];
146
147 __le32 cur_data_segno[MAX_ACTIVE_DATA_LOGS];
148 __le16 cur_data_blkoff[MAX_ACTIVE_DATA_LOGS];
149 __le32 ckpt_flags;
150 __le32 cp_pack_total_block_count;
151 __le32 cp_pack_start_sum;
152 __le32 valid_node_count;
153 __le32 valid_inode_count;
154 __le32 next_free_nid;
155 __le32 sit_ver_bitmap_bytesize;
156 __le32 nat_ver_bitmap_bytesize;
157 __le32 checksum_offset;
158 __le64 elapsed_time;
159
160 unsigned char alloc_type[MAX_ACTIVE_LOGS];
161
162
163 unsigned char sit_nat_version_bitmap[1];
164} __packed;
165
166
167
168
169#define F2FS_ORPHANS_PER_BLOCK 1020
170
171#define GET_ORPHAN_BLOCKS(n) (((n) + F2FS_ORPHANS_PER_BLOCK - 1) / \
172 F2FS_ORPHANS_PER_BLOCK)
173
174struct f2fs_orphan_block {
175 __le32 ino[F2FS_ORPHANS_PER_BLOCK];
176 __le32 reserved;
177 __le16 blk_addr;
178 __le16 blk_count;
179 __le32 entry_count;
180 __le32 check_sum;
181} __packed;
182
183
184
185
186struct f2fs_extent {
187 __le32 fofs;
188 __le32 blk;
189 __le32 len;
190} __packed;
191
192#define F2FS_NAME_LEN 255
193
194#define DEFAULT_INLINE_XATTR_ADDRS 50
195#define DEF_ADDRS_PER_INODE 923
196#define CUR_ADDRS_PER_INODE(inode) (DEF_ADDRS_PER_INODE - \
197 get_extra_isize(inode))
198#define DEF_NIDS_PER_INODE 5
199#define ADDRS_PER_INODE(inode) addrs_per_inode(inode)
200#define ADDRS_PER_BLOCK 1018
201#define NIDS_PER_BLOCK 1018
202
203#define ADDRS_PER_PAGE(page, inode) \
204 (IS_INODE(page) ? ADDRS_PER_INODE(inode) : ADDRS_PER_BLOCK)
205
206#define NODE_DIR1_BLOCK (DEF_ADDRS_PER_INODE + 1)
207#define NODE_DIR2_BLOCK (DEF_ADDRS_PER_INODE + 2)
208#define NODE_IND1_BLOCK (DEF_ADDRS_PER_INODE + 3)
209#define NODE_IND2_BLOCK (DEF_ADDRS_PER_INODE + 4)
210#define NODE_DIND_BLOCK (DEF_ADDRS_PER_INODE + 5)
211
212#define F2FS_INLINE_XATTR 0x01
213#define F2FS_INLINE_DATA 0x02
214#define F2FS_INLINE_DENTRY 0x04
215#define F2FS_DATA_EXIST 0x08
216#define F2FS_INLINE_DOTS 0x10
217#define F2FS_EXTRA_ATTR 0x20
218#define F2FS_PIN_FILE 0x40
219
220struct f2fs_inode {
221 __le16 i_mode;
222 __u8 i_advise;
223 __u8 i_inline;
224 __le32 i_uid;
225 __le32 i_gid;
226 __le32 i_links;
227 __le64 i_size;
228 __le64 i_blocks;
229 __le64 i_atime;
230 __le64 i_ctime;
231 __le64 i_mtime;
232 __le32 i_atime_nsec;
233 __le32 i_ctime_nsec;
234 __le32 i_mtime_nsec;
235 __le32 i_generation;
236 union {
237 __le32 i_current_depth;
238 __le16 i_gc_failures;
239
240
241
242 };
243 __le32 i_xattr_nid;
244 __le32 i_flags;
245 __le32 i_pino;
246 __le32 i_namelen;
247 __u8 i_name[F2FS_NAME_LEN];
248 __u8 i_dir_level;
249
250 struct f2fs_extent i_ext;
251
252 union {
253 struct {
254 __le16 i_extra_isize;
255 __le16 i_inline_xattr_size;
256 __le32 i_projid;
257 __le32 i_inode_checksum;
258 __le64 i_crtime;
259 __le32 i_crtime_nsec;
260 __le32 i_extra_end[0];
261 } __packed;
262 __le32 i_addr[DEF_ADDRS_PER_INODE];
263 };
264 __le32 i_nid[DEF_NIDS_PER_INODE];
265
266} __packed;
267
268struct direct_node {
269 __le32 addr[ADDRS_PER_BLOCK];
270} __packed;
271
272struct indirect_node {
273 __le32 nid[NIDS_PER_BLOCK];
274} __packed;
275
276enum {
277 COLD_BIT_SHIFT = 0,
278 FSYNC_BIT_SHIFT,
279 DENT_BIT_SHIFT,
280 OFFSET_BIT_SHIFT
281};
282
283#define OFFSET_BIT_MASK (0x07)
284
285struct node_footer {
286 __le32 nid;
287 __le32 ino;
288 __le32 flag;
289 __le64 cp_ver;
290 __le32 next_blkaddr;
291} __packed;
292
293struct f2fs_node {
294
295 union {
296 struct f2fs_inode i;
297 struct direct_node dn;
298 struct indirect_node in;
299 };
300 struct node_footer footer;
301} __packed;
302
303
304
305
306#define NAT_ENTRY_PER_BLOCK (PAGE_SIZE / sizeof(struct f2fs_nat_entry))
307#define NAT_ENTRY_BITMAP_SIZE ((NAT_ENTRY_PER_BLOCK + 7) / 8)
308#define NAT_ENTRY_BITMAP_SIZE_ALIGNED \
309 ((NAT_ENTRY_BITMAP_SIZE + BITS_PER_LONG - 1) / \
310 BITS_PER_LONG * BITS_PER_LONG)
311
312
313struct f2fs_nat_entry {
314 __u8 version;
315 __le32 ino;
316 __le32 block_addr;
317} __packed;
318
319struct f2fs_nat_block {
320 struct f2fs_nat_entry entries[NAT_ENTRY_PER_BLOCK];
321} __packed;
322
323
324
325
326
327
328
329
330#define SIT_VBLOCK_MAP_SIZE 64
331#define SIT_ENTRY_PER_BLOCK (PAGE_SIZE / sizeof(struct f2fs_sit_entry))
332
333
334
335
336
337#define F2FS_MAX_SEGMENT ((16 * 1024 * 1024) / 2)
338
339
340
341
342
343
344#define SIT_VBLOCKS_SHIFT 10
345#define SIT_VBLOCKS_MASK ((1 << SIT_VBLOCKS_SHIFT) - 1)
346#define GET_SIT_VBLOCKS(raw_sit) \
347 (le16_to_cpu((raw_sit)->vblocks) & SIT_VBLOCKS_MASK)
348#define GET_SIT_TYPE(raw_sit) \
349 ((le16_to_cpu((raw_sit)->vblocks) & ~SIT_VBLOCKS_MASK) \
350 >> SIT_VBLOCKS_SHIFT)
351
352struct f2fs_sit_entry {
353 __le16 vblocks;
354 __u8 valid_map[SIT_VBLOCK_MAP_SIZE];
355 __le64 mtime;
356} __packed;
357
358struct f2fs_sit_block {
359 struct f2fs_sit_entry entries[SIT_ENTRY_PER_BLOCK];
360} __packed;
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377#define ENTRIES_IN_SUM 512
378#define SUMMARY_SIZE (7)
379#define SUM_FOOTER_SIZE (5)
380#define SUM_ENTRY_SIZE (SUMMARY_SIZE * ENTRIES_IN_SUM)
381
382
383struct f2fs_summary {
384 __le32 nid;
385 union {
386 __u8 reserved[3];
387 struct {
388 __u8 version;
389 __le16 ofs_in_node;
390 } __packed;
391 };
392} __packed;
393
394
395#define SUM_TYPE_NODE (1)
396#define SUM_TYPE_DATA (0)
397
398struct summary_footer {
399 unsigned char entry_type;
400 __le32 check_sum;
401} __packed;
402
403#define SUM_JOURNAL_SIZE (F2FS_BLKSIZE - SUM_FOOTER_SIZE -\
404 SUM_ENTRY_SIZE)
405#define NAT_JOURNAL_ENTRIES ((SUM_JOURNAL_SIZE - 2) /\
406 sizeof(struct nat_journal_entry))
407#define NAT_JOURNAL_RESERVED ((SUM_JOURNAL_SIZE - 2) %\
408 sizeof(struct nat_journal_entry))
409#define SIT_JOURNAL_ENTRIES ((SUM_JOURNAL_SIZE - 2) /\
410 sizeof(struct sit_journal_entry))
411#define SIT_JOURNAL_RESERVED ((SUM_JOURNAL_SIZE - 2) %\
412 sizeof(struct sit_journal_entry))
413
414
415
416
417#define EXTRA_INFO_RESERVED (SUM_JOURNAL_SIZE - 2 - 8)
418
419
420
421
422
423enum {
424 NAT_JOURNAL = 0,
425 SIT_JOURNAL
426};
427
428struct nat_journal_entry {
429 __le32 nid;
430 struct f2fs_nat_entry ne;
431} __packed;
432
433struct nat_journal {
434 struct nat_journal_entry entries[NAT_JOURNAL_ENTRIES];
435 __u8 reserved[NAT_JOURNAL_RESERVED];
436} __packed;
437
438struct sit_journal_entry {
439 __le32 segno;
440 struct f2fs_sit_entry se;
441} __packed;
442
443struct sit_journal {
444 struct sit_journal_entry entries[SIT_JOURNAL_ENTRIES];
445 __u8 reserved[SIT_JOURNAL_RESERVED];
446} __packed;
447
448struct f2fs_extra_info {
449 __le64 kbytes_written;
450 __u8 reserved[EXTRA_INFO_RESERVED];
451} __packed;
452
453struct f2fs_journal {
454 union {
455 __le16 n_nats;
456 __le16 n_sits;
457 };
458
459 union {
460 struct nat_journal nat_j;
461 struct sit_journal sit_j;
462 struct f2fs_extra_info info;
463 };
464} __packed;
465
466
467struct f2fs_summary_block {
468 struct f2fs_summary entries[ENTRIES_IN_SUM];
469 struct f2fs_journal journal;
470 struct summary_footer footer;
471} __packed;
472
473
474
475
476#define F2FS_DOT_HASH 0
477#define F2FS_DDOT_HASH F2FS_DOT_HASH
478#define F2FS_MAX_HASH (~((0x3ULL) << 62))
479#define F2FS_HASH_COL_BIT ((0x1ULL) << 63)
480
481typedef __le32 f2fs_hash_t;
482
483
484#define F2FS_SLOT_LEN 8
485#define F2FS_SLOT_LEN_BITS 3
486
487#define GET_DENTRY_SLOTS(x) (((x) + F2FS_SLOT_LEN - 1) >> F2FS_SLOT_LEN_BITS)
488
489
490#define MAX_DIR_HASH_DEPTH 63
491
492
493#define MAX_DIR_BUCKETS (1 << ((MAX_DIR_HASH_DEPTH / 2) - 1))
494
495
496
497
498
499
500
501
502
503
504
505
506
507#define NR_DENTRY_IN_BLOCK 214
508#define SIZE_OF_DIR_ENTRY 11
509#define SIZE_OF_DENTRY_BITMAP ((NR_DENTRY_IN_BLOCK + BITS_PER_BYTE - 1) / \
510 BITS_PER_BYTE)
511#define SIZE_OF_RESERVED (PAGE_SIZE - ((SIZE_OF_DIR_ENTRY + \
512 F2FS_SLOT_LEN) * \
513 NR_DENTRY_IN_BLOCK + SIZE_OF_DENTRY_BITMAP))
514
515
516struct f2fs_dir_entry {
517 __le32 hash_code;
518 __le32 ino;
519 __le16 name_len;
520 __u8 file_type;
521} __packed;
522
523
524struct f2fs_dentry_block {
525
526 __u8 dentry_bitmap[SIZE_OF_DENTRY_BITMAP];
527 __u8 reserved[SIZE_OF_RESERVED];
528 struct f2fs_dir_entry dentry[NR_DENTRY_IN_BLOCK];
529 __u8 filename[NR_DENTRY_IN_BLOCK][F2FS_SLOT_LEN];
530} __packed;
531
532
533enum {
534 F2FS_FT_UNKNOWN,
535 F2FS_FT_REG_FILE,
536 F2FS_FT_DIR,
537 F2FS_FT_CHRDEV,
538 F2FS_FT_BLKDEV,
539 F2FS_FT_FIFO,
540 F2FS_FT_SOCK,
541 F2FS_FT_SYMLINK,
542 F2FS_FT_MAX
543};
544
545#define S_SHIFT 12
546
547#define F2FS_DEF_PROJID 0
548
549#endif
550