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