f2fs_fs.h revision 7b74ed3d7cc84c3bb7e85b825cf17540baa3bbfa
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
219enum f2fs_config_func {
220	FSCK,
221	DUMP,
222};
223
224struct 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
266enum {
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 */
315struct 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
359struct 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
395struct 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 */
407struct 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 MAX_INLINE_DATA		(sizeof(__le32) * (DEF_ADDRS_PER_INODE - \
429						F2FS_INLINE_XATTR_ADDRS - 1))
430
431#define INLINE_DATA_OFFSET	(PAGE_CACHE_SIZE - sizeof(struct node_footer) \
432				- sizeof(__le32)*(DEF_ADDRS_PER_INODE + 5 - 1))
433
434#define DEF_DIR_LEVEL		0
435
436struct f2fs_inode {
437	__le16 i_mode;			/* file mode */
438	__u8 i_advise;			/* file hints */
439	__u8 i_inline;			/* file inline flags */
440	__le32 i_uid;			/* user ID */
441	__le32 i_gid;			/* group ID */
442	__le32 i_links;			/* links count */
443	__le64 i_size;			/* file size in bytes */
444	__le64 i_blocks;		/* file size in blocks */
445	__le64 i_atime;			/* access time */
446	__le64 i_ctime;			/* change time */
447	__le64 i_mtime;			/* modification time */
448	__le32 i_atime_nsec;		/* access time in nano scale */
449	__le32 i_ctime_nsec;		/* change time in nano scale */
450	__le32 i_mtime_nsec;		/* modification time in nano scale */
451	__le32 i_generation;		/* file version (for NFS) */
452	__le32 i_current_depth;		/* only for directory depth */
453	__le32 i_xattr_nid;		/* nid to save xattr */
454	__le32 i_flags;			/* file attributes */
455	__le32 i_pino;			/* parent inode number */
456	__le32 i_namelen;		/* file name length */
457	__u8 i_name[F2FS_NAME_LEN];	/* file name for SPOR */
458	__u8 i_dir_level;		/* dentry_level for large dir */
459
460	struct f2fs_extent i_ext;	/* caching a largest extent */
461
462	__le32 i_addr[DEF_ADDRS_PER_INODE];	/* Pointers to data blocks */
463
464	__le32 i_nid[5];		/* direct(2), indirect(2),
465						double_indirect(1) node id */
466} __attribute__((packed));
467
468struct direct_node {
469	__le32 addr[ADDRS_PER_BLOCK];	/* array of data block address */
470} __attribute__((packed));
471
472struct indirect_node {
473	__le32 nid[NIDS_PER_BLOCK];	/* array of data block address */
474} __attribute__((packed));
475
476enum {
477	COLD_BIT_SHIFT = 0,
478	FSYNC_BIT_SHIFT,
479	DENT_BIT_SHIFT,
480	OFFSET_BIT_SHIFT
481};
482
483#define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
484				>> OFFSET_BIT_SHIFT)
485
486struct node_footer {
487	__le32 nid;		/* node id */
488	__le32 ino;		/* inode nunmber */
489	__le32 flag;		/* include cold/fsync/dentry marks and offset */
490	__le64 cp_ver;		/* checkpoint version */
491	__le32 next_blkaddr;	/* next node page block address */
492} __attribute__((packed));
493
494struct f2fs_node {
495	/* can be one of three types: inode, direct, and indirect types */
496	union {
497		struct f2fs_inode i;
498		struct direct_node dn;
499		struct indirect_node in;
500	};
501	struct node_footer footer;
502} __attribute__((packed));
503
504/*
505 * For NAT entries
506 */
507#define NAT_ENTRY_PER_BLOCK (PAGE_CACHE_SIZE / sizeof(struct f2fs_nat_entry))
508
509struct f2fs_nat_entry {
510	__u8 version;		/* latest version of cached nat entry */
511	__le32 ino;		/* inode number */
512	__le32 block_addr;	/* block address */
513} __attribute__((packed));
514
515struct f2fs_nat_block {
516	struct f2fs_nat_entry entries[NAT_ENTRY_PER_BLOCK];
517} __attribute__((packed));
518
519/*
520 * For SIT entries
521 *
522 * Each segment is 2MB in size by default so that a bitmap for validity of
523 * there-in blocks should occupy 64 bytes, 512 bits.
524 * Not allow to change this.
525 */
526#define SIT_VBLOCK_MAP_SIZE 64
527#define SIT_ENTRY_PER_BLOCK (PAGE_CACHE_SIZE / sizeof(struct f2fs_sit_entry))
528
529/*
530 * F2FS uses 4 bytes to represent block address. As a result, supported size of
531 * disk is 16 TB and it equals to 16 * 1024 * 1024 / 2 segments.
532 */
533#define F2FS_MAX_SEGMENT       ((16 * 1024 * 1024) / 2)
534#define MAX_SIT_BITMAP_SIZE    ((F2FS_MAX_SEGMENT / SIT_ENTRY_PER_BLOCK) / 8)
535
536/*
537 * Note that f2fs_sit_entry->vblocks has the following bit-field information.
538 * [15:10] : allocation type such as CURSEG_XXXX_TYPE
539 * [9:0] : valid block count
540 */
541#define SIT_VBLOCKS_SHIFT	10
542#define SIT_VBLOCKS_MASK	((1 << SIT_VBLOCKS_SHIFT) - 1)
543#define GET_SIT_VBLOCKS(raw_sit)				\
544	(le16_to_cpu((raw_sit)->vblocks) & SIT_VBLOCKS_MASK)
545#define GET_SIT_TYPE(raw_sit)					\
546	((le16_to_cpu((raw_sit)->vblocks) & ~SIT_VBLOCKS_MASK)	\
547	 >> SIT_VBLOCKS_SHIFT)
548
549struct f2fs_sit_entry {
550	__le16 vblocks;				/* reference above */
551	__u8 valid_map[SIT_VBLOCK_MAP_SIZE];	/* bitmap for valid blocks */
552	__le64 mtime;				/* segment age for cleaning */
553} __attribute__((packed));
554
555struct f2fs_sit_block {
556	struct f2fs_sit_entry entries[SIT_ENTRY_PER_BLOCK];
557} __attribute__((packed));
558
559/*
560 * For segment summary
561 *
562 * One summary block contains exactly 512 summary entries, which represents
563 * exactly 2MB segment by default. Not allow to change the basic units.
564 *
565 * NOTE: For initializing fields, you must use set_summary
566 *
567 * - If data page, nid represents dnode's nid
568 * - If node page, nid represents the node page's nid.
569 *
570 * The ofs_in_node is used by only data page. It represents offset
571 * from node's page's beginning to get a data block address.
572 * ex) data_blkaddr = (block_t)(nodepage_start_address + ofs_in_node)
573 */
574#define ENTRIES_IN_SUM		512
575#define	SUMMARY_SIZE		(7)	/* sizeof(struct summary) */
576#define	SUM_FOOTER_SIZE		(5)	/* sizeof(struct summary_footer) */
577#define SUM_ENTRIES_SIZE	(SUMMARY_SIZE * ENTRIES_IN_SUM)
578
579/* a summary entry for a 4KB-sized block in a segment */
580struct f2fs_summary {
581	__le32 nid;		/* parent node id */
582	union {
583		__u8 reserved[3];
584		struct {
585			__u8 version;		/* node version number */
586			__le16 ofs_in_node;	/* block index in parent node */
587		} __attribute__((packed));
588	};
589} __attribute__((packed));
590
591/* summary block type, node or data, is stored to the summary_footer */
592#define SUM_TYPE_NODE		(1)
593#define SUM_TYPE_DATA		(0)
594
595struct summary_footer {
596	unsigned char entry_type;	/* SUM_TYPE_XXX */
597	__u32 check_sum;		/* summary checksum */
598} __attribute__((packed));
599
600#define SUM_JOURNAL_SIZE	(F2FS_BLKSIZE - SUM_FOOTER_SIZE -\
601				SUM_ENTRIES_SIZE)
602#define NAT_JOURNAL_ENTRIES	((SUM_JOURNAL_SIZE - 2) /\
603				sizeof(struct nat_journal_entry))
604#define NAT_JOURNAL_RESERVED	((SUM_JOURNAL_SIZE - 2) %\
605				sizeof(struct nat_journal_entry))
606#define SIT_JOURNAL_ENTRIES	((SUM_JOURNAL_SIZE - 2) /\
607				sizeof(struct sit_journal_entry))
608#define SIT_JOURNAL_RESERVED	((SUM_JOURNAL_SIZE - 2) %\
609				sizeof(struct sit_journal_entry))
610/*
611 * frequently updated NAT/SIT entries can be stored in the spare area in
612 * summary blocks
613 */
614enum {
615	NAT_JOURNAL = 0,
616	SIT_JOURNAL
617};
618
619struct nat_journal_entry {
620	__le32 nid;
621	struct f2fs_nat_entry ne;
622} __attribute__((packed));
623
624struct nat_journal {
625	struct nat_journal_entry entries[NAT_JOURNAL_ENTRIES];
626	__u8 reserved[NAT_JOURNAL_RESERVED];
627} __attribute__((packed));
628
629struct sit_journal_entry {
630	__le32 segno;
631	struct f2fs_sit_entry se;
632} __attribute__((packed));
633
634struct sit_journal {
635	struct sit_journal_entry entries[SIT_JOURNAL_ENTRIES];
636	__u8 reserved[SIT_JOURNAL_RESERVED];
637} __attribute__((packed));
638
639/* 4KB-sized summary block structure */
640struct f2fs_summary_block {
641	struct f2fs_summary entries[ENTRIES_IN_SUM];
642	union {
643		__le16 n_nats;
644		__le16 n_sits;
645	};
646	/* spare area is used by NAT or SIT journals */
647	union {
648		struct nat_journal nat_j;
649		struct sit_journal sit_j;
650	};
651	struct summary_footer footer;
652} __attribute__((packed));
653
654/*
655 * For directory operations
656 */
657#define F2FS_DOT_HASH		0
658#define F2FS_DDOT_HASH		F2FS_DOT_HASH
659#define F2FS_MAX_HASH		(~((0x3ULL) << 62))
660#define F2FS_HASH_COL_BIT	((0x1ULL) << 63)
661
662typedef __le32	f2fs_hash_t;
663
664/* One directory entry slot covers 8bytes-long file name */
665#define F2FS_SLOT_LEN		8
666#define F2FS_SLOT_LEN_BITS	3
667
668#define GET_DENTRY_SLOTS(x)	((x + F2FS_SLOT_LEN - 1) >> F2FS_SLOT_LEN_BITS)
669
670/* the number of dentry in a block */
671#define NR_DENTRY_IN_BLOCK	214
672
673/* MAX level for dir lookup */
674#define MAX_DIR_HASH_DEPTH	63
675
676#define SIZE_OF_DIR_ENTRY	11	/* by byte */
677#define SIZE_OF_DENTRY_BITMAP	((NR_DENTRY_IN_BLOCK + BITS_PER_BYTE - 1) / \
678					BITS_PER_BYTE)
679#define SIZE_OF_RESERVED	(PAGE_SIZE - ((SIZE_OF_DIR_ENTRY + \
680				F2FS_SLOT_LEN) * \
681				NR_DENTRY_IN_BLOCK + SIZE_OF_DENTRY_BITMAP))
682
683/* One directory entry slot representing F2FS_SLOT_LEN-sized file name */
684struct f2fs_dir_entry {
685	__le32 hash_code;	/* hash code of file name */
686	__le32 ino;		/* inode number */
687	__le16 name_len;	/* lengh of file name */
688	__u8 file_type;		/* file type */
689} __attribute__((packed));
690
691/* 4KB-sized directory entry block */
692struct f2fs_dentry_block {
693	/* validity bitmap for directory entries in each block */
694	__u8 dentry_bitmap[SIZE_OF_DENTRY_BITMAP];
695	__u8 reserved[SIZE_OF_RESERVED];
696	struct f2fs_dir_entry dentry[NR_DENTRY_IN_BLOCK];
697	__u8 filename[NR_DENTRY_IN_BLOCK][F2FS_SLOT_LEN];
698} __attribute__((packed));
699
700/* file types used in inode_info->flags */
701enum FILE_TYPE {
702	F2FS_FT_UNKNOWN,
703	F2FS_FT_REG_FILE,
704	F2FS_FT_DIR,
705	F2FS_FT_CHRDEV,
706	F2FS_FT_BLKDEV,
707	F2FS_FT_FIFO,
708	F2FS_FT_SOCK,
709	F2FS_FT_SYMLINK,
710	F2FS_FT_MAX,
711	/* added for fsck */
712	F2FS_FT_ORPHAN,
713	F2FS_FT_XATTR,
714};
715
716/* from f2fs/segment.h */
717enum {
718	LFS = 0,
719	SSR
720};
721
722extern void ASCIIToUNICODE(u_int16_t *, u_int8_t *);
723extern int log_base_2(u_int32_t);
724extern unsigned int addrs_per_inode(struct f2fs_inode *);
725
726extern int get_bits_in_byte(unsigned char n);
727extern int set_bit(unsigned int nr,void * addr);
728extern int clear_bit(unsigned int nr, void * addr);
729extern int test_bit(unsigned int nr, const void * addr);
730extern int f2fs_test_bit(unsigned int, const char *);
731extern int f2fs_set_bit(unsigned int, char *);
732extern int f2fs_clear_bit(unsigned int, char *);
733extern unsigned long find_next_bit(const unsigned long *,
734				unsigned long, unsigned long);
735
736extern u_int32_t f2fs_cal_crc32(u_int32_t, void *, int);
737extern int f2fs_crc_valid(u_int32_t blk_crc, void *buf, int len);
738
739extern void f2fs_init_configuration(struct f2fs_configuration *);
740extern int f2fs_dev_is_umounted(struct f2fs_configuration *);
741extern int f2fs_get_device_info(struct f2fs_configuration *);
742extern void f2fs_finalize_device(struct f2fs_configuration *);
743
744extern int dev_read(void *, __u64, size_t);
745extern int dev_write(void *, __u64, size_t);
746extern int dev_write_block(void *, __u64);
747extern int dev_write_dump(void *, __u64, size_t);
748/* All bytes in the buffer must be 0 use dev_fill(). */
749extern int dev_fill(void *, __u64, size_t);
750
751extern int dev_read_block(void *, __u64);
752extern int dev_read_blocks(void *, __u64, __u32 );
753
754f2fs_hash_t f2fs_dentry_hash(const unsigned char *, int);
755
756extern struct f2fs_configuration config;
757
758#endif	/*__F2FS_FS_H */
759