• 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 !HAVE_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 enum f2fs_config_func {
220 	FSCK,
221 	DUMP,
222 };
223 
224 struct f2fs_configuration {
225 	u_int32_t sector_size;
226 	u_int32_t reserved_segments;
227 	u_int32_t overprovision;
228 	u_int32_t cur_seg[6];
229 	u_int32_t segs_per_sec;
230 	u_int32_t secs_per_zone;
231 	u_int32_t start_sector;
232 	u_int64_t total_sectors;
233 	u_int32_t sectors_per_blk;
234 	u_int32_t blks_per_seg;
235 	char *vol_label;
236 	int heap;
237 	int32_t fd;
238 	int32_t dump_fd;
239 	char *device_name;
240 	char *extension_list;
241 	int dbg_lv;
242 	int trim;
243 	int func;
244 	void *private;
245 	int fix_on;
246 	int bug_on;
247 	int auto_fix;
248 } __attribute__((packed));
249 
250 #ifdef CONFIG_64BIT
251 #define BITS_PER_LONG	64
252 #else
253 #define BITS_PER_LONG	32
254 #endif
255 
256 #define BIT_MASK(nr)	(1 << (nr % BITS_PER_LONG))
257 #define BIT_WORD(nr)	(nr / BITS_PER_LONG)
258 
259 /*
260  * Copied from fs/f2fs/f2fs.h
261  */
262 #define	NR_CURSEG_DATA_TYPE	(3)
263 #define NR_CURSEG_NODE_TYPE	(3)
264 #define NR_CURSEG_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
265 
266 enum {
267 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
268 	CURSEG_WARM_DATA,	/* data blocks */
269 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
270 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
271 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
272 	CURSEG_COLD_NODE,	/* indirect node blocks */
273 	NO_CHECK_TYPE
274 };
275 
276 /*
277  * Copied from fs/f2fs/segment.h
278  */
279 #define GET_SUM_TYPE(footer) ((footer)->entry_type)
280 #define SET_SUM_TYPE(footer, type) ((footer)->entry_type = type)
281 
282 /*
283  * Copied from include/linux/f2fs_sb.h
284  */
285 #define F2FS_SUPER_OFFSET		1024	/* byte-size offset */
286 #define F2FS_LOG_SECTOR_SIZE		9	/* 9 bits for 512 byte */
287 #define F2FS_LOG_SECTORS_PER_BLOCK	3	/* 4KB: F2FS_BLKSIZE */
288 #define F2FS_BLKSIZE			4096	/* support only 4KB block */
289 #define F2FS_MAX_EXTENSION		64	/* # of extension entries */
290 #define F2FS_BLK_ALIGN(x)	(((x) + F2FS_BLKSIZE - 1) / F2FS_BLKSIZE)
291 
292 #define NULL_ADDR		0x0U
293 #define NEW_ADDR		-1U
294 
295 #define F2FS_ROOT_INO(sbi)	(sbi->root_ino_num)
296 #define F2FS_NODE_INO(sbi)	(sbi->node_ino_num)
297 #define F2FS_META_INO(sbi)	(sbi->meta_ino_num)
298 
299 /* This flag is used by node and meta inodes, and by recovery */
300 #define GFP_F2FS_ZERO	(GFP_NOFS | __GFP_ZERO)
301 
302 /*
303  * For further optimization on multi-head logs, on-disk layout supports maximum
304  * 16 logs by default. The number, 16, is expected to cover all the cases
305  * enoughly. The implementaion currently uses no more than 6 logs.
306  * Half the logs are used for nodes, and the other half are used for data.
307  */
308 #define MAX_ACTIVE_LOGS	16
309 #define MAX_ACTIVE_NODE_LOGS	8
310 #define MAX_ACTIVE_DATA_LOGS	8
311 
312 /*
313  * For superblock
314  */
315 struct f2fs_super_block {
316 	__le32 magic;			/* Magic Number */
317 	__le16 major_ver;		/* Major Version */
318 	__le16 minor_ver;		/* Minor Version */
319 	__le32 log_sectorsize;		/* log2 sector size in bytes */
320 	__le32 log_sectors_per_block;	/* log2 # of sectors per block */
321 	__le32 log_blocksize;		/* log2 block size in bytes */
322 	__le32 log_blocks_per_seg;	/* log2 # of blocks per segment */
323 	__le32 segs_per_sec;		/* # of segments per section */
324 	__le32 secs_per_zone;		/* # of sections per zone */
325 	__le32 checksum_offset;		/* checksum offset inside super block */
326 	__le64 block_count;		/* total # of user blocks */
327 	__le32 section_count;		/* total # of sections */
328 	__le32 segment_count;		/* total # of segments */
329 	__le32 segment_count_ckpt;	/* # of segments for checkpoint */
330 	__le32 segment_count_sit;	/* # of segments for SIT */
331 	__le32 segment_count_nat;	/* # of segments for NAT */
332 	__le32 segment_count_ssa;	/* # of segments for SSA */
333 	__le32 segment_count_main;	/* # of segments for main area */
334 	__le32 segment0_blkaddr;	/* start block address of segment 0 */
335 	__le32 cp_blkaddr;		/* start block address of checkpoint */
336 	__le32 sit_blkaddr;		/* start block address of SIT */
337 	__le32 nat_blkaddr;		/* start block address of NAT */
338 	__le32 ssa_blkaddr;		/* start block address of SSA */
339 	__le32 main_blkaddr;		/* start block address of main area */
340 	__le32 root_ino;		/* root inode number */
341 	__le32 node_ino;		/* node inode number */
342 	__le32 meta_ino;		/* meta inode number */
343 	__u8 uuid[16];			/* 128-bit uuid for volume */
344 	__le16 volume_name[512];	/* volume name */
345 	__le32 extension_count;		/* # of extensions below */
346 	__u8 extension_list[F2FS_MAX_EXTENSION][8];	/* extension array */
347 	__le32 cp_payload;
348 } __attribute__((packed));
349 
350 /*
351  * For checkpoint
352  */
353 #define CP_FSCK_FLAG		0x00000010
354 #define CP_ERROR_FLAG		0x00000008
355 #define CP_COMPACT_SUM_FLAG	0x00000004
356 #define CP_ORPHAN_PRESENT_FLAG	0x00000002
357 #define CP_UMOUNT_FLAG		0x00000001
358 
359 struct f2fs_checkpoint {
360 	__le64 checkpoint_ver;		/* checkpoint block version number */
361 	__le64 user_block_count;	/* # of user blocks */
362 	__le64 valid_block_count;	/* # of valid blocks in main area */
363 	__le32 rsvd_segment_count;	/* # of reserved segments for gc */
364 	__le32 overprov_segment_count;	/* # of overprovision segments */
365 	__le32 free_segment_count;	/* # of free segments in main area */
366 
367 	/* information of current node segments */
368 	__le32 cur_node_segno[MAX_ACTIVE_NODE_LOGS];
369 	__le16 cur_node_blkoff[MAX_ACTIVE_NODE_LOGS];
370 	/* information of current data segments */
371 	__le32 cur_data_segno[MAX_ACTIVE_DATA_LOGS];
372 	__le16 cur_data_blkoff[MAX_ACTIVE_DATA_LOGS];
373 	__le32 ckpt_flags;		/* Flags : umount and journal_present */
374 	__le32 cp_pack_total_block_count;	/* total # of one cp pack */
375 	__le32 cp_pack_start_sum;	/* start block number of data summary */
376 	__le32 valid_node_count;	/* Total number of valid nodes */
377 	__le32 valid_inode_count;	/* Total number of valid inodes */
378 	__le32 next_free_nid;		/* Next free node number */
379 	__le32 sit_ver_bitmap_bytesize;	/* Default value 64 */
380 	__le32 nat_ver_bitmap_bytesize; /* Default value 256 */
381 	__le32 checksum_offset;		/* checksum offset inside cp block */
382 	__le64 elapsed_time;		/* mounted time */
383 	/* allocation type of current segment */
384 	unsigned char alloc_type[MAX_ACTIVE_LOGS];
385 
386 	/* SIT and NAT version bitmap */
387 	unsigned char sit_nat_version_bitmap[1];
388 } __attribute__((packed));
389 
390 /*
391  * For orphan inode management
392  */
393 #define F2FS_ORPHANS_PER_BLOCK	1020
394 
395 struct f2fs_orphan_block {
396 	__le32 ino[F2FS_ORPHANS_PER_BLOCK];	/* inode numbers */
397 	__le32 reserved;	/* reserved */
398 	__le16 blk_addr;	/* block index in current CP */
399 	__le16 blk_count;	/* Number of orphan inode blocks in CP */
400 	__le32 entry_count;	/* Total number of orphan nodes in current CP */
401 	__le32 check_sum;	/* CRC32 for orphan inode block */
402 } __attribute__((packed));
403 
404 /*
405  * For NODE structure
406  */
407 struct f2fs_extent {
408 	__le32 fofs;		/* start file offset of the extent */
409 	__le32 blk_addr;	/* start block address of the extent */
410 	__le32 len;		/* lengh of the extent */
411 } __attribute__((packed));
412 
413 #define F2FS_NAME_LEN		255
414 #define F2FS_INLINE_XATTR_ADDRS	50	/* 200 bytes for inline xattrs */
415 #define DEF_ADDRS_PER_INODE	923	/* Address Pointers in an Inode */
416 #define ADDRS_PER_INODE(fi)	addrs_per_inode(fi)
417 #define ADDRS_PER_BLOCK         1018	/* Address Pointers in a Direct Block */
418 #define NIDS_PER_BLOCK          1018	/* Node IDs in an Indirect Block */
419 
420 #define	NODE_DIR1_BLOCK		(DEF_ADDRS_PER_INODE + 1)
421 #define	NODE_DIR2_BLOCK		(DEF_ADDRS_PER_INODE + 2)
422 #define	NODE_IND1_BLOCK		(DEF_ADDRS_PER_INODE + 3)
423 #define	NODE_IND2_BLOCK		(DEF_ADDRS_PER_INODE + 4)
424 #define	NODE_DIND_BLOCK		(DEF_ADDRS_PER_INODE + 5)
425 
426 #define F2FS_INLINE_XATTR	0x01	/* file inline xattr flag */
427 #define F2FS_INLINE_DATA	0x02	/* file inline data flag */
428 #define F2FS_INLINE_DENTRY	0x04	/* file inline dentry flag */
429 #define F2FS_DATA_EXIST		0x08	/* file inline data exist flag */
430 
431 #define MAX_INLINE_DATA		(sizeof(__le32) * (DEF_ADDRS_PER_INODE - \
432 						F2FS_INLINE_XATTR_ADDRS - 1))
433 
434 #define INLINE_DATA_OFFSET	(PAGE_CACHE_SIZE - sizeof(struct node_footer) \
435 				- sizeof(__le32)*(DEF_ADDRS_PER_INODE + 5 - 1))
436 
437 #define DEF_DIR_LEVEL		0
438 
439 struct f2fs_inode {
440 	__le16 i_mode;			/* file mode */
441 	__u8 i_advise;			/* file hints */
442 	__u8 i_inline;			/* file inline flags */
443 	__le32 i_uid;			/* user ID */
444 	__le32 i_gid;			/* group ID */
445 	__le32 i_links;			/* links count */
446 	__le64 i_size;			/* file size in bytes */
447 	__le64 i_blocks;		/* file size in blocks */
448 	__le64 i_atime;			/* access time */
449 	__le64 i_ctime;			/* change time */
450 	__le64 i_mtime;			/* modification time */
451 	__le32 i_atime_nsec;		/* access time in nano scale */
452 	__le32 i_ctime_nsec;		/* change time in nano scale */
453 	__le32 i_mtime_nsec;		/* modification time in nano scale */
454 	__le32 i_generation;		/* file version (for NFS) */
455 	__le32 i_current_depth;		/* only for directory depth */
456 	__le32 i_xattr_nid;		/* nid to save xattr */
457 	__le32 i_flags;			/* file attributes */
458 	__le32 i_pino;			/* parent inode number */
459 	__le32 i_namelen;		/* file name length */
460 	__u8 i_name[F2FS_NAME_LEN];	/* file name for SPOR */
461 	__u8 i_dir_level;		/* dentry_level for large dir */
462 
463 	struct f2fs_extent i_ext;	/* caching a largest extent */
464 
465 	__le32 i_addr[DEF_ADDRS_PER_INODE];	/* Pointers to data blocks */
466 
467 	__le32 i_nid[5];		/* direct(2), indirect(2),
468 						double_indirect(1) node id */
469 } __attribute__((packed));
470 
471 struct direct_node {
472 	__le32 addr[ADDRS_PER_BLOCK];	/* array of data block address */
473 } __attribute__((packed));
474 
475 struct indirect_node {
476 	__le32 nid[NIDS_PER_BLOCK];	/* array of data block address */
477 } __attribute__((packed));
478 
479 enum {
480 	COLD_BIT_SHIFT = 0,
481 	FSYNC_BIT_SHIFT,
482 	DENT_BIT_SHIFT,
483 	OFFSET_BIT_SHIFT
484 };
485 
486 #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
487 				>> OFFSET_BIT_SHIFT)
488 
489 struct node_footer {
490 	__le32 nid;		/* node id */
491 	__le32 ino;		/* inode nunmber */
492 	__le32 flag;		/* include cold/fsync/dentry marks and offset */
493 	__le64 cp_ver;		/* checkpoint version */
494 	__le32 next_blkaddr;	/* next node page block address */
495 } __attribute__((packed));
496 
497 struct f2fs_node {
498 	/* can be one of three types: inode, direct, and indirect types */
499 	union {
500 		struct f2fs_inode i;
501 		struct direct_node dn;
502 		struct indirect_node in;
503 	};
504 	struct node_footer footer;
505 } __attribute__((packed));
506 
507 /*
508  * For NAT entries
509  */
510 #define NAT_ENTRY_PER_BLOCK (PAGE_CACHE_SIZE / sizeof(struct f2fs_nat_entry))
511 
512 struct f2fs_nat_entry {
513 	__u8 version;		/* latest version of cached nat entry */
514 	__le32 ino;		/* inode number */
515 	__le32 block_addr;	/* block address */
516 } __attribute__((packed));
517 
518 struct f2fs_nat_block {
519 	struct f2fs_nat_entry entries[NAT_ENTRY_PER_BLOCK];
520 } __attribute__((packed));
521 
522 /*
523  * For SIT entries
524  *
525  * Each segment is 2MB in size by default so that a bitmap for validity of
526  * there-in blocks should occupy 64 bytes, 512 bits.
527  * Not allow to change this.
528  */
529 #define SIT_VBLOCK_MAP_SIZE 64
530 #define SIT_ENTRY_PER_BLOCK (PAGE_CACHE_SIZE / sizeof(struct f2fs_sit_entry))
531 
532 /*
533  * F2FS uses 4 bytes to represent block address. As a result, supported size of
534  * disk is 16 TB and it equals to 16 * 1024 * 1024 / 2 segments.
535  */
536 #define F2FS_MAX_SEGMENT       ((16 * 1024 * 1024) / 2)
537 #define MAX_SIT_BITMAP_SIZE    ((F2FS_MAX_SEGMENT / SIT_ENTRY_PER_BLOCK) / 8)
538 
539 /*
540  * Note that f2fs_sit_entry->vblocks has the following bit-field information.
541  * [15:10] : allocation type such as CURSEG_XXXX_TYPE
542  * [9:0] : valid block count
543  */
544 #define SIT_VBLOCKS_SHIFT	10
545 #define SIT_VBLOCKS_MASK	((1 << SIT_VBLOCKS_SHIFT) - 1)
546 #define GET_SIT_VBLOCKS(raw_sit)				\
547 	(le16_to_cpu((raw_sit)->vblocks) & SIT_VBLOCKS_MASK)
548 #define GET_SIT_TYPE(raw_sit)					\
549 	((le16_to_cpu((raw_sit)->vblocks) & ~SIT_VBLOCKS_MASK)	\
550 	 >> SIT_VBLOCKS_SHIFT)
551 
552 struct f2fs_sit_entry {
553 	__le16 vblocks;				/* reference above */
554 	__u8 valid_map[SIT_VBLOCK_MAP_SIZE];	/* bitmap for valid blocks */
555 	__le64 mtime;				/* segment age for cleaning */
556 } __attribute__((packed));
557 
558 struct f2fs_sit_block {
559 	struct f2fs_sit_entry entries[SIT_ENTRY_PER_BLOCK];
560 } __attribute__((packed));
561 
562 /*
563  * For segment summary
564  *
565  * One summary block contains exactly 512 summary entries, which represents
566  * exactly 2MB segment by default. Not allow to change the basic units.
567  *
568  * NOTE: For initializing fields, you must use set_summary
569  *
570  * - If data page, nid represents dnode's nid
571  * - If node page, nid represents the node page's nid.
572  *
573  * The ofs_in_node is used by only data page. It represents offset
574  * from node's page's beginning to get a data block address.
575  * ex) data_blkaddr = (block_t)(nodepage_start_address + ofs_in_node)
576  */
577 #define ENTRIES_IN_SUM		512
578 #define	SUMMARY_SIZE		(7)	/* sizeof(struct summary) */
579 #define	SUM_FOOTER_SIZE		(5)	/* sizeof(struct summary_footer) */
580 #define SUM_ENTRIES_SIZE	(SUMMARY_SIZE * ENTRIES_IN_SUM)
581 
582 /* a summary entry for a 4KB-sized block in a segment */
583 struct f2fs_summary {
584 	__le32 nid;		/* parent node id */
585 	union {
586 		__u8 reserved[3];
587 		struct {
588 			__u8 version;		/* node version number */
589 			__le16 ofs_in_node;	/* block index in parent node */
590 		} __attribute__((packed));
591 	};
592 } __attribute__((packed));
593 
594 /* summary block type, node or data, is stored to the summary_footer */
595 #define SUM_TYPE_NODE		(1)
596 #define SUM_TYPE_DATA		(0)
597 
598 struct summary_footer {
599 	unsigned char entry_type;	/* SUM_TYPE_XXX */
600 	__u32 check_sum;		/* summary checksum */
601 } __attribute__((packed));
602 
603 #define SUM_JOURNAL_SIZE	(F2FS_BLKSIZE - SUM_FOOTER_SIZE -\
604 				SUM_ENTRIES_SIZE)
605 #define NAT_JOURNAL_ENTRIES	((SUM_JOURNAL_SIZE - 2) /\
606 				sizeof(struct nat_journal_entry))
607 #define NAT_JOURNAL_RESERVED	((SUM_JOURNAL_SIZE - 2) %\
608 				sizeof(struct nat_journal_entry))
609 #define SIT_JOURNAL_ENTRIES	((SUM_JOURNAL_SIZE - 2) /\
610 				sizeof(struct sit_journal_entry))
611 #define SIT_JOURNAL_RESERVED	((SUM_JOURNAL_SIZE - 2) %\
612 				sizeof(struct sit_journal_entry))
613 /*
614  * frequently updated NAT/SIT entries can be stored in the spare area in
615  * summary blocks
616  */
617 enum {
618 	NAT_JOURNAL = 0,
619 	SIT_JOURNAL
620 };
621 
622 struct nat_journal_entry {
623 	__le32 nid;
624 	struct f2fs_nat_entry ne;
625 } __attribute__((packed));
626 
627 struct nat_journal {
628 	struct nat_journal_entry entries[NAT_JOURNAL_ENTRIES];
629 	__u8 reserved[NAT_JOURNAL_RESERVED];
630 } __attribute__((packed));
631 
632 struct sit_journal_entry {
633 	__le32 segno;
634 	struct f2fs_sit_entry se;
635 } __attribute__((packed));
636 
637 struct sit_journal {
638 	struct sit_journal_entry entries[SIT_JOURNAL_ENTRIES];
639 	__u8 reserved[SIT_JOURNAL_RESERVED];
640 } __attribute__((packed));
641 
642 /* 4KB-sized summary block structure */
643 struct f2fs_summary_block {
644 	struct f2fs_summary entries[ENTRIES_IN_SUM];
645 	union {
646 		__le16 n_nats;
647 		__le16 n_sits;
648 	};
649 	/* spare area is used by NAT or SIT journals */
650 	union {
651 		struct nat_journal nat_j;
652 		struct sit_journal sit_j;
653 	};
654 	struct summary_footer footer;
655 } __attribute__((packed));
656 
657 /*
658  * For directory operations
659  */
660 #define F2FS_DOT_HASH		0
661 #define F2FS_DDOT_HASH		F2FS_DOT_HASH
662 #define F2FS_MAX_HASH		(~((0x3ULL) << 62))
663 #define F2FS_HASH_COL_BIT	((0x1ULL) << 63)
664 
665 typedef __le32	f2fs_hash_t;
666 
667 /* One directory entry slot covers 8bytes-long file name */
668 #define F2FS_SLOT_LEN		8
669 #define F2FS_SLOT_LEN_BITS	3
670 
671 #define GET_DENTRY_SLOTS(x)	((x + F2FS_SLOT_LEN - 1) >> F2FS_SLOT_LEN_BITS)
672 
673 /* the number of dentry in a block */
674 #define NR_DENTRY_IN_BLOCK	214
675 
676 /* MAX level for dir lookup */
677 #define MAX_DIR_HASH_DEPTH	63
678 
679 #define SIZE_OF_DIR_ENTRY	11	/* by byte */
680 #define SIZE_OF_DENTRY_BITMAP	((NR_DENTRY_IN_BLOCK + BITS_PER_BYTE - 1) / \
681 					BITS_PER_BYTE)
682 #define SIZE_OF_RESERVED	(PAGE_SIZE - ((SIZE_OF_DIR_ENTRY + \
683 				F2FS_SLOT_LEN) * \
684 				NR_DENTRY_IN_BLOCK + SIZE_OF_DENTRY_BITMAP))
685 
686 /* One directory entry slot representing F2FS_SLOT_LEN-sized file name */
687 struct f2fs_dir_entry {
688 	__le32 hash_code;	/* hash code of file name */
689 	__le32 ino;		/* inode number */
690 	__le16 name_len;	/* lengh of file name */
691 	__u8 file_type;		/* file type */
692 } __attribute__((packed));
693 
694 /* 4KB-sized directory entry block */
695 struct f2fs_dentry_block {
696 	/* validity bitmap for directory entries in each block */
697 	__u8 dentry_bitmap[SIZE_OF_DENTRY_BITMAP];
698 	__u8 reserved[SIZE_OF_RESERVED];
699 	struct f2fs_dir_entry dentry[NR_DENTRY_IN_BLOCK];
700 	__u8 filename[NR_DENTRY_IN_BLOCK][F2FS_SLOT_LEN];
701 } __attribute__((packed));
702 
703 /* for inline dir */
704 #define NR_INLINE_DENTRY	(MAX_INLINE_DATA * BITS_PER_BYTE / \
705 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
706 				BITS_PER_BYTE + 1))
707 #define INLINE_DENTRY_BITMAP_SIZE	((NR_INLINE_DENTRY + \
708 					BITS_PER_BYTE - 1) / BITS_PER_BYTE)
709 #define INLINE_RESERVED_SIZE	(MAX_INLINE_DATA - \
710 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
711 				NR_INLINE_DENTRY + INLINE_DENTRY_BITMAP_SIZE))
712 
713 /* inline directory entry structure */
714 struct f2fs_inline_dentry {
715 	__u8 dentry_bitmap[INLINE_DENTRY_BITMAP_SIZE];
716 	__u8 reserved[INLINE_RESERVED_SIZE];
717 	struct f2fs_dir_entry dentry[NR_INLINE_DENTRY];
718 	__u8 filename[NR_INLINE_DENTRY][F2FS_SLOT_LEN];
719 } __packed;
720 
721 /* file types used in inode_info->flags */
722 enum FILE_TYPE {
723 	F2FS_FT_UNKNOWN,
724 	F2FS_FT_REG_FILE,
725 	F2FS_FT_DIR,
726 	F2FS_FT_CHRDEV,
727 	F2FS_FT_BLKDEV,
728 	F2FS_FT_FIFO,
729 	F2FS_FT_SOCK,
730 	F2FS_FT_SYMLINK,
731 	F2FS_FT_MAX,
732 	/* added for fsck */
733 	F2FS_FT_ORPHAN,
734 	F2FS_FT_XATTR,
735 	F2FS_FT_LAST_FILE_TYPE = F2FS_FT_XATTR,
736 };
737 
738 /* from f2fs/segment.h */
739 enum {
740 	LFS = 0,
741 	SSR
742 };
743 
744 extern void ASCIIToUNICODE(u_int16_t *, u_int8_t *);
745 extern int log_base_2(u_int32_t);
746 extern unsigned int addrs_per_inode(struct f2fs_inode *);
747 
748 extern int get_bits_in_byte(unsigned char n);
749 extern int set_bit(unsigned int nr,void * addr);
750 extern int clear_bit(unsigned int nr, void * addr);
751 extern int test_bit(unsigned int nr, const void * addr);
752 extern int f2fs_test_bit(unsigned int, const char *);
753 extern int f2fs_set_bit(unsigned int, char *);
754 extern int f2fs_clear_bit(unsigned int, char *);
755 extern unsigned long find_next_bit(const unsigned long *,
756 				unsigned long, unsigned long);
757 
758 extern u_int32_t f2fs_cal_crc32(u_int32_t, void *, int);
759 extern int f2fs_crc_valid(u_int32_t blk_crc, void *buf, int len);
760 
761 extern void f2fs_init_configuration(struct f2fs_configuration *);
762 extern int f2fs_dev_is_umounted(struct f2fs_configuration *);
763 extern int f2fs_get_device_info(struct f2fs_configuration *);
764 extern void f2fs_finalize_device(struct f2fs_configuration *);
765 
766 extern int dev_read(void *, __u64, size_t);
767 extern int dev_write(void *, __u64, size_t);
768 extern int dev_write_block(void *, __u64);
769 extern int dev_write_dump(void *, __u64, size_t);
770 /* All bytes in the buffer must be 0 use dev_fill(). */
771 extern int dev_fill(void *, __u64, size_t);
772 
773 extern int dev_read_block(void *, __u64);
774 extern int dev_read_blocks(void *, __u64, __u32 );
775 
776 f2fs_hash_t f2fs_dentry_hash(const unsigned char *, int);
777 
778 extern struct f2fs_configuration config;
779 
780 #endif	/*__F2FS_FS_H */
781