• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 typedef u_int64_t	u64;
24 typedef u_int32_t	u32;
25 typedef u_int16_t	u16;
26 typedef u_int8_t	u8;
27 typedef u32		block_t;
28 typedef u32		nid_t;
29 typedef u8		bool;
30 typedef unsigned long	pgoff_t;
31 
32 #if HAVE_BYTESWAP_H
33 #include <byteswap.h>
34 #else
35 /**
36  * bswap_16 - reverse bytes in a uint16_t value.
37  * @val: value whose bytes to swap.
38  *
39  * Example:
40  *	// Output contains "1024 is 4 as two bytes reversed"
41  *	printf("1024 is %u as two bytes reversed\n", bswap_16(1024));
42  */
bswap_16(uint16_t val)43 static inline uint16_t bswap_16(uint16_t val)
44 {
45 	return ((val & (uint16_t)0x00ffU) << 8)
46 		| ((val & (uint16_t)0xff00U) >> 8);
47 }
48 
49 /**
50  * bswap_32 - reverse bytes in a uint32_t value.
51  * @val: value whose bytes to swap.
52  *
53  * Example:
54  *	// Output contains "1024 is 262144 as four bytes reversed"
55  *	printf("1024 is %u as four bytes reversed\n", bswap_32(1024));
56  */
bswap_32(uint32_t val)57 static inline uint32_t bswap_32(uint32_t val)
58 {
59 	return ((val & (uint32_t)0x000000ffUL) << 24)
60 		| ((val & (uint32_t)0x0000ff00UL) <<  8)
61 		| ((val & (uint32_t)0x00ff0000UL) >>  8)
62 		| ((val & (uint32_t)0xff000000UL) >> 24);
63 }
64 #endif /* !HAVE_BYTESWAP_H */
65 
66 #if defined HAVE_DECL_BSWAP_64 && !HAVE_DECL_BSWAP_64
67 /**
68  * bswap_64 - reverse bytes in a uint64_t value.
69  * @val: value whose bytes to swap.
70  *
71  * Example:
72  *	// Output contains "1024 is 1125899906842624 as eight bytes reversed"
73  *	printf("1024 is %llu as eight bytes reversed\n",
74  *		(unsigned long long)bswap_64(1024));
75  */
bswap_64(uint64_t val)76 static inline uint64_t bswap_64(uint64_t val)
77 {
78 	return ((val & (uint64_t)0x00000000000000ffULL) << 56)
79 		| ((val & (uint64_t)0x000000000000ff00ULL) << 40)
80 		| ((val & (uint64_t)0x0000000000ff0000ULL) << 24)
81 		| ((val & (uint64_t)0x00000000ff000000ULL) <<  8)
82 		| ((val & (uint64_t)0x000000ff00000000ULL) >>  8)
83 		| ((val & (uint64_t)0x0000ff0000000000ULL) >> 24)
84 		| ((val & (uint64_t)0x00ff000000000000ULL) >> 40)
85 		| ((val & (uint64_t)0xff00000000000000ULL) >> 56);
86 }
87 #endif
88 
89 #if __BYTE_ORDER == __LITTLE_ENDIAN
90 #define le16_to_cpu(x)	((__u16)(x))
91 #define le32_to_cpu(x)	((__u32)(x))
92 #define le64_to_cpu(x)	((__u64)(x))
93 #define cpu_to_le16(x)	((__u16)(x))
94 #define cpu_to_le32(x)	((__u32)(x))
95 #define cpu_to_le64(x)	((__u64)(x))
96 #elif __BYTE_ORDER == __BIG_ENDIAN
97 #define le16_to_cpu(x)	bswap_16(x)
98 #define le32_to_cpu(x)	bswap_32(x)
99 #define le64_to_cpu(x)	bswap_64(x)
100 #define cpu_to_le16(x)	bswap_16(x)
101 #define cpu_to_le32(x)	bswap_32(x)
102 #define cpu_to_le64(x)	bswap_64(x)
103 #endif
104 
105 #define typecheck(type,x) \
106 	({	type __dummy; \
107 		typeof(x) __dummy2; \
108 		(void)(&__dummy == &__dummy2); \
109 		1; \
110 	 })
111 
112 #define NULL_SEGNO	((unsigned int)~0)
113 
114 /*
115  * Debugging interfaces
116  */
117 #define FIX_MSG(fmt, ...)						\
118 	do {								\
119 		printf("[FIX] (%s:%4d) ", __func__, __LINE__);		\
120 		printf(" --> "fmt"\n", ##__VA_ARGS__);			\
121 	} while (0)
122 
123 #define ASSERT_MSG(fmt, ...)						\
124 	do {								\
125 		printf("[ASSERT] (%s:%4d) ", __func__, __LINE__);	\
126 		printf(" --> "fmt"\n", ##__VA_ARGS__);			\
127 		config.bug_on = 1;					\
128 	} while (0)
129 
130 #define ASSERT(exp)							\
131 	do {								\
132 		if (!(exp)) {						\
133 			printf("[ASSERT] (%s:%4d) " #exp"\n",		\
134 					__func__, __LINE__);		\
135 			exit(-1);					\
136 		}							\
137 	} while (0)
138 
139 #define ERR_MSG(fmt, ...)						\
140 	do {								\
141 		printf("[%s:%d] " fmt, __func__, __LINE__, ##__VA_ARGS__); \
142 	} while (0)
143 
144 #define MSG(n, fmt, ...)						\
145 	do {								\
146 		if (config.dbg_lv >= n) {				\
147 			printf(fmt, ##__VA_ARGS__);			\
148 		}							\
149 	} while (0)
150 
151 #define DBG(n, fmt, ...)						\
152 	do {								\
153 		if (config.dbg_lv >= n) {				\
154 			printf("[%s:%4d] " fmt,				\
155 				__func__, __LINE__, ##__VA_ARGS__);	\
156 		}							\
157 	} while (0)
158 
159 /* Display on console */
160 #define DISP(fmt, ptr, member)				\
161 	do {						\
162 		printf("%-30s" fmt, #member, ((ptr)->member));	\
163 	} while (0)
164 
165 #define DISP_u32(ptr, member)						\
166 	do {								\
167 		assert(sizeof((ptr)->member) <= 4);			\
168 		printf("%-30s" "\t\t[0x%8x : %u]\n",		\
169 			#member, ((ptr)->member), ((ptr)->member));	\
170 	} while (0)
171 
172 #define DISP_u64(ptr, member)						\
173 	do {								\
174 		assert(sizeof((ptr)->member) == 8);			\
175 		printf("%-30s" "\t\t[0x%8llx : %llu]\n",		\
176 			#member, ((ptr)->member), ((ptr)->member));	\
177 	} while (0)
178 
179 #define DISP_utf(ptr, member)						\
180 	do {								\
181 		printf("%-30s" "\t\t[%s]\n", #member, ((ptr)->member)); \
182 	} while (0)
183 
184 /* Display to buffer */
185 #define BUF_DISP_u32(buf, data, len, ptr, member)			\
186 	do {								\
187 		assert(sizeof((ptr)->member) <= 4);			\
188 		snprintf(buf, len, #member);				\
189 		snprintf(data, len, "0x%x : %u", ((ptr)->member),	\
190 						((ptr)->member));	\
191 	} while (0)
192 
193 #define BUF_DISP_u64(buf, data, len, ptr, member)			\
194 	do {								\
195 		assert(sizeof((ptr)->member) == 8);			\
196 		snprintf(buf, len, #member);				\
197 		snprintf(data, len, "0x%llx : %llu", ((ptr)->member),	\
198 						((ptr)->member));	\
199 	} while (0)
200 
201 #define BUF_DISP_utf(buf, data, len, ptr, member)			\
202 		snprintf(buf, len, #member)
203 
204 /* these are defined in kernel */
205 #define PAGE_SIZE		4096
206 #define PAGE_CACHE_SIZE		4096
207 #define BITS_PER_BYTE		8
208 #define F2FS_SUPER_MAGIC	0xF2F52010	/* F2FS Magic Number */
209 #define CHECKSUM_OFFSET		4092
210 
211 /* for mkfs */
212 #define F2FS_MIN_VOLUME_SIZE	104857600
213 #define	F2FS_NUMBER_OF_CHECKPOINT_PACK	2
214 #define	DEFAULT_SECTOR_SIZE		512
215 #define	DEFAULT_SECTORS_PER_BLOCK	8
216 #define	DEFAULT_BLOCKS_PER_SEGMENT	512
217 #define DEFAULT_SEGMENTS_PER_SECTION	1
218 
219 #define VERSION_LEN	256
220 
221 enum f2fs_config_func {
222 	FSCK,
223 	DUMP,
224 };
225 
226 struct f2fs_configuration {
227 	u_int32_t sector_size;
228 	u_int32_t reserved_segments;
229 	u_int32_t overprovision;
230 	u_int32_t cur_seg[6];
231 	u_int32_t segs_per_sec;
232 	u_int32_t secs_per_zone;
233 	u_int32_t segs_per_zone;
234 	u_int32_t start_sector;
235 	u_int64_t total_sectors;
236 	u_int32_t sectors_per_blk;
237 	u_int32_t blks_per_seg;
238 	__u8 init_version[VERSION_LEN + 1];
239 	__u8 sb_version[VERSION_LEN + 1];
240 	__u8 version[VERSION_LEN + 1];
241 	char *vol_label;
242 	int heap;
243 	int32_t fd, kd;
244 	int32_t dump_fd;
245 	char *device_name;
246 	char *extension_list;
247 	int dbg_lv;
248 	int trim;
249 	int func;
250 	void *private;
251 	int fix_on;
252 	int bug_on;
253 	int auto_fix;
254 } __attribute__((packed));
255 
256 #ifdef CONFIG_64BIT
257 #define BITS_PER_LONG	64
258 #else
259 #define BITS_PER_LONG	32
260 #endif
261 
262 #define BIT_MASK(nr)	(1 << (nr % BITS_PER_LONG))
263 #define BIT_WORD(nr)	(nr / BITS_PER_LONG)
264 
265 /*
266  * Copied from fs/f2fs/f2fs.h
267  */
268 #define	NR_CURSEG_DATA_TYPE	(3)
269 #define NR_CURSEG_NODE_TYPE	(3)
270 #define NR_CURSEG_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
271 
272 enum {
273 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
274 	CURSEG_WARM_DATA,	/* data blocks */
275 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
276 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
277 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
278 	CURSEG_COLD_NODE,	/* indirect node blocks */
279 	NO_CHECK_TYPE
280 };
281 
282 /*
283  * Copied from fs/f2fs/segment.h
284  */
285 #define GET_SUM_TYPE(footer) ((footer)->entry_type)
286 #define SET_SUM_TYPE(footer, type) ((footer)->entry_type = type)
287 
288 /*
289  * Copied from include/linux/f2fs_sb.h
290  */
291 #define F2FS_SUPER_OFFSET		1024	/* byte-size offset */
292 #define F2FS_MIN_LOG_SECTOR_SIZE	9	/* 9 bits for 512 bytes */
293 #define F2FS_MAX_LOG_SECTOR_SIZE	12	/* 12 bits for 4096 bytes */
294 #define F2FS_BLKSIZE			4096	/* support only 4KB block */
295 #define F2FS_MAX_EXTENSION		64	/* # of extension entries */
296 #define F2FS_BLK_ALIGN(x)	(((x) + F2FS_BLKSIZE - 1) / F2FS_BLKSIZE)
297 
298 #define NULL_ADDR		0x0U
299 #define NEW_ADDR		-1U
300 
301 #define F2FS_ROOT_INO(sbi)	(sbi->root_ino_num)
302 #define F2FS_NODE_INO(sbi)	(sbi->node_ino_num)
303 #define F2FS_META_INO(sbi)	(sbi->meta_ino_num)
304 
305 /* This flag is used by node and meta inodes, and by recovery */
306 #define GFP_F2FS_ZERO	(GFP_NOFS | __GFP_ZERO)
307 
308 /*
309  * For further optimization on multi-head logs, on-disk layout supports maximum
310  * 16 logs by default. The number, 16, is expected to cover all the cases
311  * enoughly. The implementaion currently uses no more than 6 logs.
312  * Half the logs are used for nodes, and the other half are used for data.
313  */
314 #define MAX_ACTIVE_LOGS	16
315 #define MAX_ACTIVE_NODE_LOGS	8
316 #define MAX_ACTIVE_DATA_LOGS	8
317 
318 /*
319  * For superblock
320  */
321 struct f2fs_super_block {
322 	__le32 magic;			/* Magic Number */
323 	__le16 major_ver;		/* Major Version */
324 	__le16 minor_ver;		/* Minor Version */
325 	__le32 log_sectorsize;		/* log2 sector size in bytes */
326 	__le32 log_sectors_per_block;	/* log2 # of sectors per block */
327 	__le32 log_blocksize;		/* log2 block size in bytes */
328 	__le32 log_blocks_per_seg;	/* log2 # of blocks per segment */
329 	__le32 segs_per_sec;		/* # of segments per section */
330 	__le32 secs_per_zone;		/* # of sections per zone */
331 	__le32 checksum_offset;		/* checksum offset inside super block */
332 	__le64 block_count;		/* total # of user blocks */
333 	__le32 section_count;		/* total # of sections */
334 	__le32 segment_count;		/* total # of segments */
335 	__le32 segment_count_ckpt;	/* # of segments for checkpoint */
336 	__le32 segment_count_sit;	/* # of segments for SIT */
337 	__le32 segment_count_nat;	/* # of segments for NAT */
338 	__le32 segment_count_ssa;	/* # of segments for SSA */
339 	__le32 segment_count_main;	/* # of segments for main area */
340 	__le32 segment0_blkaddr;	/* start block address of segment 0 */
341 	__le32 cp_blkaddr;		/* start block address of checkpoint */
342 	__le32 sit_blkaddr;		/* start block address of SIT */
343 	__le32 nat_blkaddr;		/* start block address of NAT */
344 	__le32 ssa_blkaddr;		/* start block address of SSA */
345 	__le32 main_blkaddr;		/* start block address of main area */
346 	__le32 root_ino;		/* root inode number */
347 	__le32 node_ino;		/* node inode number */
348 	__le32 meta_ino;		/* meta inode number */
349 	__u8 uuid[16];			/* 128-bit uuid for volume */
350 	__le16 volume_name[512];	/* volume name */
351 	__le32 extension_count;		/* # of extensions below */
352 	__u8 extension_list[F2FS_MAX_EXTENSION][8];	/* extension array */
353 	__le32 cp_payload;
354 	__u8 version[VERSION_LEN];	/* the kernel version */
355 	__u8 init_version[VERSION_LEN];	/* the initial kernel version */
356 } __attribute__((packed));
357 
358 /*
359  * For checkpoint
360  */
361 #define CP_FASTBOOT_FLAG	0x00000020
362 #define CP_FSCK_FLAG		0x00000010
363 #define CP_ERROR_FLAG		0x00000008
364 #define CP_COMPACT_SUM_FLAG	0x00000004
365 #define CP_ORPHAN_PRESENT_FLAG	0x00000002
366 #define CP_UMOUNT_FLAG		0x00000001
367 
368 struct f2fs_checkpoint {
369 	__le64 checkpoint_ver;		/* checkpoint block version number */
370 	__le64 user_block_count;	/* # of user blocks */
371 	__le64 valid_block_count;	/* # of valid blocks in main area */
372 	__le32 rsvd_segment_count;	/* # of reserved segments for gc */
373 	__le32 overprov_segment_count;	/* # of overprovision segments */
374 	__le32 free_segment_count;	/* # of free segments in main area */
375 
376 	/* information of current node segments */
377 	__le32 cur_node_segno[MAX_ACTIVE_NODE_LOGS];
378 	__le16 cur_node_blkoff[MAX_ACTIVE_NODE_LOGS];
379 	/* information of current data segments */
380 	__le32 cur_data_segno[MAX_ACTIVE_DATA_LOGS];
381 	__le16 cur_data_blkoff[MAX_ACTIVE_DATA_LOGS];
382 	__le32 ckpt_flags;		/* Flags : umount and journal_present */
383 	__le32 cp_pack_total_block_count;	/* total # of one cp pack */
384 	__le32 cp_pack_start_sum;	/* start block number of data summary */
385 	__le32 valid_node_count;	/* Total number of valid nodes */
386 	__le32 valid_inode_count;	/* Total number of valid inodes */
387 	__le32 next_free_nid;		/* Next free node number */
388 	__le32 sit_ver_bitmap_bytesize;	/* Default value 64 */
389 	__le32 nat_ver_bitmap_bytesize; /* Default value 256 */
390 	__le32 checksum_offset;		/* checksum offset inside cp block */
391 	__le64 elapsed_time;		/* mounted time */
392 	/* allocation type of current segment */
393 	unsigned char alloc_type[MAX_ACTIVE_LOGS];
394 
395 	/* SIT and NAT version bitmap */
396 	unsigned char sit_nat_version_bitmap[1];
397 } __attribute__((packed));
398 
399 /*
400  * For orphan inode management
401  */
402 #define F2FS_ORPHANS_PER_BLOCK	1020
403 
404 struct f2fs_orphan_block {
405 	__le32 ino[F2FS_ORPHANS_PER_BLOCK];	/* inode numbers */
406 	__le32 reserved;	/* reserved */
407 	__le16 blk_addr;	/* block index in current CP */
408 	__le16 blk_count;	/* Number of orphan inode blocks in CP */
409 	__le32 entry_count;	/* Total number of orphan nodes in current CP */
410 	__le32 check_sum;	/* CRC32 for orphan inode block */
411 } __attribute__((packed));
412 
413 /*
414  * For NODE structure
415  */
416 struct f2fs_extent {
417 	__le32 fofs;		/* start file offset of the extent */
418 	__le32 blk_addr;	/* start block address of the extent */
419 	__le32 len;		/* lengh of the extent */
420 } __attribute__((packed));
421 
422 #define F2FS_NAME_LEN		255
423 #define F2FS_INLINE_XATTR_ADDRS	50	/* 200 bytes for inline xattrs */
424 #define DEF_ADDRS_PER_INODE	923	/* Address Pointers in an Inode */
425 #define ADDRS_PER_INODE(fi)	addrs_per_inode(fi)
426 #define ADDRS_PER_BLOCK         1018	/* Address Pointers in a Direct Block */
427 #define NIDS_PER_BLOCK          1018	/* Node IDs in an Indirect Block */
428 
429 #define	NODE_DIR1_BLOCK		(DEF_ADDRS_PER_INODE + 1)
430 #define	NODE_DIR2_BLOCK		(DEF_ADDRS_PER_INODE + 2)
431 #define	NODE_IND1_BLOCK		(DEF_ADDRS_PER_INODE + 3)
432 #define	NODE_IND2_BLOCK		(DEF_ADDRS_PER_INODE + 4)
433 #define	NODE_DIND_BLOCK		(DEF_ADDRS_PER_INODE + 5)
434 
435 #define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
436 #define F2FS_INLINE_DATA	0x02	/* file inline data flag */
437 #define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
438 #define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
439 
440 #define MAX_INLINE_DATA		(sizeof(__le32) * (DEF_ADDRS_PER_INODE - \
441 						F2FS_INLINE_XATTR_ADDRS - 1))
442 
443 #define INLINE_DATA_OFFSET	(PAGE_CACHE_SIZE - sizeof(struct node_footer) \
444 				- sizeof(__le32)*(DEF_ADDRS_PER_INODE + 5 - 1))
445 
446 #define DEF_DIR_LEVEL		0
447 
448 struct f2fs_inode {
449 	__le16 i_mode;			/* file mode */
450 	__u8 i_advise;			/* file hints */
451 	__u8 i_inline;			/* file inline flags */
452 	__le32 i_uid;			/* user ID */
453 	__le32 i_gid;			/* group ID */
454 	__le32 i_links;			/* links count */
455 	__le64 i_size;			/* file size in bytes */
456 	__le64 i_blocks;		/* file size in blocks */
457 	__le64 i_atime;			/* access time */
458 	__le64 i_ctime;			/* change time */
459 	__le64 i_mtime;			/* modification time */
460 	__le32 i_atime_nsec;		/* access time in nano scale */
461 	__le32 i_ctime_nsec;		/* change time in nano scale */
462 	__le32 i_mtime_nsec;		/* modification time in nano scale */
463 	__le32 i_generation;		/* file version (for NFS) */
464 	__le32 i_current_depth;		/* only for directory depth */
465 	__le32 i_xattr_nid;		/* nid to save xattr */
466 	__le32 i_flags;			/* file attributes */
467 	__le32 i_pino;			/* parent inode number */
468 	__le32 i_namelen;		/* file name length */
469 	__u8 i_name[F2FS_NAME_LEN];	/* file name for SPOR */
470 	__u8 i_dir_level;		/* dentry_level for large dir */
471 
472 	struct f2fs_extent i_ext;	/* caching a largest extent */
473 
474 	__le32 i_addr[DEF_ADDRS_PER_INODE];	/* Pointers to data blocks */
475 
476 	__le32 i_nid[5];		/* direct(2), indirect(2),
477 						double_indirect(1) node id */
478 } __attribute__((packed));
479 
480 struct direct_node {
481 	__le32 addr[ADDRS_PER_BLOCK];	/* array of data block address */
482 } __attribute__((packed));
483 
484 struct indirect_node {
485 	__le32 nid[NIDS_PER_BLOCK];	/* array of data block address */
486 } __attribute__((packed));
487 
488 enum {
489 	COLD_BIT_SHIFT = 0,
490 	FSYNC_BIT_SHIFT,
491 	DENT_BIT_SHIFT,
492 	OFFSET_BIT_SHIFT
493 };
494 
495 #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
496 				>> OFFSET_BIT_SHIFT)
497 
498 struct node_footer {
499 	__le32 nid;		/* node id */
500 	__le32 ino;		/* inode nunmber */
501 	__le32 flag;		/* include cold/fsync/dentry marks and offset */
502 	__le64 cp_ver;		/* checkpoint version */
503 	__le32 next_blkaddr;	/* next node page block address */
504 } __attribute__((packed));
505 
506 struct f2fs_node {
507 	/* can be one of three types: inode, direct, and indirect types */
508 	union {
509 		struct f2fs_inode i;
510 		struct direct_node dn;
511 		struct indirect_node in;
512 	};
513 	struct node_footer footer;
514 } __attribute__((packed));
515 
516 /*
517  * For NAT entries
518  */
519 #define NAT_ENTRY_PER_BLOCK (PAGE_CACHE_SIZE / sizeof(struct f2fs_nat_entry))
520 
521 struct f2fs_nat_entry {
522 	__u8 version;		/* latest version of cached nat entry */
523 	__le32 ino;		/* inode number */
524 	__le32 block_addr;	/* block address */
525 } __attribute__((packed));
526 
527 struct f2fs_nat_block {
528 	struct f2fs_nat_entry entries[NAT_ENTRY_PER_BLOCK];
529 } __attribute__((packed));
530 
531 /*
532  * For SIT entries
533  *
534  * Each segment is 2MB in size by default so that a bitmap for validity of
535  * there-in blocks should occupy 64 bytes, 512 bits.
536  * Not allow to change this.
537  */
538 #define SIT_VBLOCK_MAP_SIZE 64
539 #define SIT_ENTRY_PER_BLOCK (PAGE_CACHE_SIZE / sizeof(struct f2fs_sit_entry))
540 
541 /*
542  * F2FS uses 4 bytes to represent block address. As a result, supported size of
543  * disk is 16 TB and it equals to 16 * 1024 * 1024 / 2 segments.
544  */
545 #define F2FS_MAX_SEGMENT       ((16 * 1024 * 1024) / 2)
546 #define MAX_SIT_BITMAP_SIZE    ((F2FS_MAX_SEGMENT / SIT_ENTRY_PER_BLOCK) / 8)
547 
548 /*
549  * Note that f2fs_sit_entry->vblocks has the following bit-field information.
550  * [15:10] : allocation type such as CURSEG_XXXX_TYPE
551  * [9:0] : valid block count
552  */
553 #define SIT_VBLOCKS_SHIFT	10
554 #define SIT_VBLOCKS_MASK	((1 << SIT_VBLOCKS_SHIFT) - 1)
555 #define GET_SIT_VBLOCKS(raw_sit)				\
556 	(le16_to_cpu((raw_sit)->vblocks) & SIT_VBLOCKS_MASK)
557 #define GET_SIT_TYPE(raw_sit)					\
558 	((le16_to_cpu((raw_sit)->vblocks) & ~SIT_VBLOCKS_MASK)	\
559 	 >> SIT_VBLOCKS_SHIFT)
560 
561 struct f2fs_sit_entry {
562 	__le16 vblocks;				/* reference above */
563 	__u8 valid_map[SIT_VBLOCK_MAP_SIZE];	/* bitmap for valid blocks */
564 	__le64 mtime;				/* segment age for cleaning */
565 } __attribute__((packed));
566 
567 struct f2fs_sit_block {
568 	struct f2fs_sit_entry entries[SIT_ENTRY_PER_BLOCK];
569 } __attribute__((packed));
570 
571 /*
572  * For segment summary
573  *
574  * One summary block contains exactly 512 summary entries, which represents
575  * exactly 2MB segment by default. Not allow to change the basic units.
576  *
577  * NOTE: For initializing fields, you must use set_summary
578  *
579  * - If data page, nid represents dnode's nid
580  * - If node page, nid represents the node page's nid.
581  *
582  * The ofs_in_node is used by only data page. It represents offset
583  * from node's page's beginning to get a data block address.
584  * ex) data_blkaddr = (block_t)(nodepage_start_address + ofs_in_node)
585  */
586 #define ENTRIES_IN_SUM		512
587 #define	SUMMARY_SIZE		(7)	/* sizeof(struct summary) */
588 #define	SUM_FOOTER_SIZE		(5)	/* sizeof(struct summary_footer) */
589 #define SUM_ENTRIES_SIZE	(SUMMARY_SIZE * ENTRIES_IN_SUM)
590 
591 /* a summary entry for a 4KB-sized block in a segment */
592 struct f2fs_summary {
593 	__le32 nid;		/* parent node id */
594 	union {
595 		__u8 reserved[3];
596 		struct {
597 			__u8 version;		/* node version number */
598 			__le16 ofs_in_node;	/* block index in parent node */
599 		} __attribute__((packed));
600 	};
601 } __attribute__((packed));
602 
603 /* summary block type, node or data, is stored to the summary_footer */
604 #define SUM_TYPE_NODE		(1)
605 #define SUM_TYPE_DATA		(0)
606 
607 struct summary_footer {
608 	unsigned char entry_type;	/* SUM_TYPE_XXX */
609 	__u32 check_sum;		/* summary checksum */
610 } __attribute__((packed));
611 
612 #define SUM_JOURNAL_SIZE	(F2FS_BLKSIZE - SUM_FOOTER_SIZE -\
613 				SUM_ENTRIES_SIZE)
614 #define NAT_JOURNAL_ENTRIES	((SUM_JOURNAL_SIZE - 2) /\
615 				sizeof(struct nat_journal_entry))
616 #define NAT_JOURNAL_RESERVED	((SUM_JOURNAL_SIZE - 2) %\
617 				sizeof(struct nat_journal_entry))
618 #define SIT_JOURNAL_ENTRIES	((SUM_JOURNAL_SIZE - 2) /\
619 				sizeof(struct sit_journal_entry))
620 #define SIT_JOURNAL_RESERVED	((SUM_JOURNAL_SIZE - 2) %\
621 				sizeof(struct sit_journal_entry))
622 /*
623  * frequently updated NAT/SIT entries can be stored in the spare area in
624  * summary blocks
625  */
626 enum {
627 	NAT_JOURNAL = 0,
628 	SIT_JOURNAL
629 };
630 
631 struct nat_journal_entry {
632 	__le32 nid;
633 	struct f2fs_nat_entry ne;
634 } __attribute__((packed));
635 
636 struct nat_journal {
637 	struct nat_journal_entry entries[NAT_JOURNAL_ENTRIES];
638 	__u8 reserved[NAT_JOURNAL_RESERVED];
639 } __attribute__((packed));
640 
641 struct sit_journal_entry {
642 	__le32 segno;
643 	struct f2fs_sit_entry se;
644 } __attribute__((packed));
645 
646 struct sit_journal {
647 	struct sit_journal_entry entries[SIT_JOURNAL_ENTRIES];
648 	__u8 reserved[SIT_JOURNAL_RESERVED];
649 } __attribute__((packed));
650 
651 /* 4KB-sized summary block structure */
652 struct f2fs_summary_block {
653 	struct f2fs_summary entries[ENTRIES_IN_SUM];
654 	union {
655 		__le16 n_nats;
656 		__le16 n_sits;
657 	};
658 	/* spare area is used by NAT or SIT journals */
659 	union {
660 		struct nat_journal nat_j;
661 		struct sit_journal sit_j;
662 	};
663 	struct summary_footer footer;
664 } __attribute__((packed));
665 
666 /*
667  * For directory operations
668  */
669 #define F2FS_DOT_HASH		0
670 #define F2FS_DDOT_HASH		F2FS_DOT_HASH
671 #define F2FS_MAX_HASH		(~((0x3ULL) << 62))
672 #define F2FS_HASH_COL_BIT	((0x1ULL) << 63)
673 
674 typedef __le32	f2fs_hash_t;
675 
676 /* One directory entry slot covers 8bytes-long file name */
677 #define F2FS_SLOT_LEN		8
678 #define F2FS_SLOT_LEN_BITS	3
679 
680 #define GET_DENTRY_SLOTS(x)	((x + F2FS_SLOT_LEN - 1) >> F2FS_SLOT_LEN_BITS)
681 
682 /* the number of dentry in a block */
683 #define NR_DENTRY_IN_BLOCK	214
684 
685 /* MAX level for dir lookup */
686 #define MAX_DIR_HASH_DEPTH	63
687 
688 #define SIZE_OF_DIR_ENTRY	11	/* by byte */
689 #define SIZE_OF_DENTRY_BITMAP	((NR_DENTRY_IN_BLOCK + BITS_PER_BYTE - 1) / \
690 					BITS_PER_BYTE)
691 #define SIZE_OF_RESERVED	(PAGE_SIZE - ((SIZE_OF_DIR_ENTRY + \
692 				F2FS_SLOT_LEN) * \
693 				NR_DENTRY_IN_BLOCK + SIZE_OF_DENTRY_BITMAP))
694 
695 /* One directory entry slot representing F2FS_SLOT_LEN-sized file name */
696 struct f2fs_dir_entry {
697 	__le32 hash_code;	/* hash code of file name */
698 	__le32 ino;		/* inode number */
699 	__le16 name_len;	/* lengh of file name */
700 	__u8 file_type;		/* file type */
701 } __attribute__((packed));
702 
703 /* 4KB-sized directory entry block */
704 struct f2fs_dentry_block {
705 	/* validity bitmap for directory entries in each block */
706 	__u8 dentry_bitmap[SIZE_OF_DENTRY_BITMAP];
707 	__u8 reserved[SIZE_OF_RESERVED];
708 	struct f2fs_dir_entry dentry[NR_DENTRY_IN_BLOCK];
709 	__u8 filename[NR_DENTRY_IN_BLOCK][F2FS_SLOT_LEN];
710 } __attribute__((packed));
711 
712 /* for inline dir */
713 #define NR_INLINE_DENTRY	(MAX_INLINE_DATA * BITS_PER_BYTE / \
714 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
715 				BITS_PER_BYTE + 1))
716 #define INLINE_DENTRY_BITMAP_SIZE	((NR_INLINE_DENTRY + \
717 					BITS_PER_BYTE - 1) / BITS_PER_BYTE)
718 #define INLINE_RESERVED_SIZE	(MAX_INLINE_DATA - \
719 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
720 				NR_INLINE_DENTRY + INLINE_DENTRY_BITMAP_SIZE))
721 
722 /* inline directory entry structure */
723 struct f2fs_inline_dentry {
724 	__u8 dentry_bitmap[INLINE_DENTRY_BITMAP_SIZE];
725 	__u8 reserved[INLINE_RESERVED_SIZE];
726 	struct f2fs_dir_entry dentry[NR_INLINE_DENTRY];
727 	__u8 filename[NR_INLINE_DENTRY][F2FS_SLOT_LEN];
728 } __packed;
729 
730 /* file types used in inode_info->flags */
731 enum FILE_TYPE {
732 	F2FS_FT_UNKNOWN,
733 	F2FS_FT_REG_FILE,
734 	F2FS_FT_DIR,
735 	F2FS_FT_CHRDEV,
736 	F2FS_FT_BLKDEV,
737 	F2FS_FT_FIFO,
738 	F2FS_FT_SOCK,
739 	F2FS_FT_SYMLINK,
740 	F2FS_FT_MAX,
741 	/* added for fsck */
742 	F2FS_FT_ORPHAN,
743 	F2FS_FT_XATTR,
744 	F2FS_FT_LAST_FILE_TYPE = F2FS_FT_XATTR,
745 };
746 
747 /* from f2fs/segment.h */
748 enum {
749 	LFS = 0,
750 	SSR
751 };
752 
753 extern void ASCIIToUNICODE(u_int16_t *, u_int8_t *);
754 extern int log_base_2(u_int32_t);
755 extern unsigned int addrs_per_inode(struct f2fs_inode *);
756 
757 extern int get_bits_in_byte(unsigned char n);
758 extern int set_bit(unsigned int nr,void * addr);
759 extern int clear_bit(unsigned int nr, void * addr);
760 extern int test_bit(unsigned int nr, const void * addr);
761 extern int f2fs_test_bit(unsigned int, const char *);
762 extern int f2fs_set_bit(unsigned int, char *);
763 extern int f2fs_clear_bit(unsigned int, char *);
764 extern unsigned long find_next_bit(const unsigned long *,
765 				unsigned long, unsigned long);
766 
767 extern u_int32_t f2fs_cal_crc32(u_int32_t, void *, int);
768 extern int f2fs_crc_valid(u_int32_t blk_crc, void *buf, int len);
769 
770 extern void f2fs_init_configuration(struct f2fs_configuration *);
771 extern int f2fs_dev_is_umounted(struct f2fs_configuration *);
772 extern int f2fs_get_device_info(struct f2fs_configuration *);
773 extern void f2fs_finalize_device(struct f2fs_configuration *);
774 
775 extern int dev_read(void *, __u64, size_t);
776 extern int dev_write(void *, __u64, size_t);
777 extern int dev_write_block(void *, __u64);
778 extern int dev_write_dump(void *, __u64, size_t);
779 /* All bytes in the buffer must be 0 use dev_fill(). */
780 extern int dev_fill(void *, __u64, size_t);
781 
782 extern int dev_read_block(void *, __u64);
783 extern int dev_read_blocks(void *, __u64, __u32 );
784 extern int dev_reada_block(__u64);
785 
786 extern int dev_read_version(void *, __u64, size_t);
787 extern void get_kernel_version(__u8 *);
788 f2fs_hash_t f2fs_dentry_hash(const unsigned char *, int);
789 
790 extern struct f2fs_configuration config;
791 
792 #define ALIGN(val, size)	((val) + (size) - 1) / (size)
793 #define SEG_ALIGN(blks)		ALIGN(blks, config.blks_per_seg)
794 #define ZONE_ALIGN(blks)	ALIGN(blks, config.blks_per_seg * \
795 					config.segs_per_zone)
796 
797 #endif	/*__F2FS_FS_H */
798