f2fs_fs.h revision 2784bd55dd9730f3a5d2126c82094990fad053c1
1/**
2 * f2fs_fs.h
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 *             http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#ifndef __F2FS_FS_H__
12#define __F2FS_FS_H__
13
14#include <linux/types.h>
15#include <endian.h>
16#include <byteswap.h>
17
18#ifdef HAVE_CONFIG_H
19#include <config.h>
20#endif
21
22#if __BYTE_ORDER == __LITTLE_ENDIAN
23#define le16_to_cpu(x)	((__u16)(x))
24#define le32_to_cpu(x)	((__u32)(x))
25#define le64_to_cpu(x)	((__u64)(x))
26#define cpu_to_le16(x)	((__u16)(x))
27#define cpu_to_le32(x)	((__u32)(x))
28#define cpu_to_le64(x)	((__u64)(x))
29#elif __BYTE_ORDER == __BIG_ENDIAN
30#define le16_to_cpu(x)	bswap_16(x)
31#define le32_to_cpu(x)	bswap_32(x)
32#define le64_to_cpu(x)	bswap_64(x)
33#define cpu_to_le16(x)	bswap_16(x)
34#define cpu_to_le32(x)	bswap_32(x)
35#define cpu_to_le64(x)	bswap_64(x)
36#endif
37
38/* these are defined in kernel */
39#define PAGE_SIZE		4096
40#define PAGE_CACHE_SIZE		4096
41#define BITS_PER_BYTE		8
42#define F2FS_SUPER_MAGIC	0xF2F52010	/* F2FS Magic Number */
43
44/* for mkfs */
45#define F2FS_MIN_VOLUME_SIZE	104857600
46#define	F2FS_NUMBER_OF_CHECKPOINT_PACK	2
47#define	DEFAULT_SECTOR_SIZE		512
48#define	DEFAULT_SECTORS_PER_BLOCK	8
49#define	DEFAULT_BLOCKS_PER_SEGMENT	512
50#define DEFAULT_SEGMENTS_PER_SECTION	1
51#define F2FS_CP_BLOCK_SIZE		(DEFAULT_SECTOR_SIZE * \
52					DEFAULT_SECTORS_PER_BLOCK)
53
54struct f2fs_global_parameters {
55	u_int32_t sector_size;
56	u_int32_t reserved_segments;
57	u_int32_t overprovision;
58	u_int32_t cur_seg[6];
59	u_int32_t segs_per_sec;
60	u_int32_t secs_per_zone;
61	u_int32_t start_sector;
62	u_int64_t total_sectors;
63	u_int32_t sectors_per_blk;
64	u_int32_t blks_per_seg;
65	u_int8_t vol_label[16];
66	int heap;
67	int32_t fd;
68	char *device_name;
69	char *extension_list;
70} __attribute__((packed));
71
72#ifdef CONFIG_64BIT
73#define BITS_PER_LONG	64
74#else
75#define BITS_PER_LONG	32
76#endif
77
78#define BIT_MASK(nr)	(1 << (nr % BITS_PER_LONG))
79#define BIT_WORD(nr)	(nr / BITS_PER_LONG)
80
81/*
82 * Copied from fs/f2fs/f2fs.h
83 */
84#define	NR_CURSEG_DATA_TYPE	(3)
85#define NR_CURSEG_NODE_TYPE	(3)
86#define NR_CURSEG_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
87
88enum {
89	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
90	CURSEG_WARM_DATA,	/* data blocks */
91	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
92	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
93	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
94	CURSEG_COLD_NODE,	/* indirect node blocks */
95	NO_CHECK_TYPE
96};
97
98/*
99 * Copied from fs/f2fs/segment.h
100 */
101#define GET_SUM_TYPE(footer) ((footer)->entry_type)
102#define SET_SUM_TYPE(footer, type) ((footer)->entry_type = type)
103
104/*
105 * Copied from include/linux/f2fs_sb.h
106 */
107#define F2FS_SUPER_OFFSET		1024	/* byte-size offset */
108#define F2FS_LOG_SECTOR_SIZE		9	/* 9 bits for 512 byte */
109#define F2FS_LOG_SECTORS_PER_BLOCK	3	/* 4KB: F2FS_BLKSIZE */
110#define F2FS_BLKSIZE			4096	/* support only 4KB block */
111#define F2FS_MAX_EXTENSION		64	/* # of extension entries */
112
113#define NULL_ADDR		0x0U
114#define NEW_ADDR		-1U
115
116#define F2FS_ROOT_INO(sbi)	(sbi->root_ino_num)
117#define F2FS_NODE_INO(sbi)	(sbi->node_ino_num)
118#define F2FS_META_INO(sbi)	(sbi->meta_ino_num)
119
120/* This flag is used by node and meta inodes, and by recovery */
121#define GFP_F2FS_ZERO	(GFP_NOFS | __GFP_ZERO)
122
123/*
124 * For further optimization on multi-head logs, on-disk layout supports maximum
125 * 16 logs by default. The number, 16, is expected to cover all the cases
126 * enoughly. The implementaion currently uses no more than 6 logs.
127 * Half the logs are used for nodes, and the other half are used for data.
128 */
129#define MAX_ACTIVE_LOGS	16
130#define MAX_ACTIVE_NODE_LOGS	8
131#define MAX_ACTIVE_DATA_LOGS	8
132
133/*
134 * For superblock
135 */
136struct f2fs_super_block {
137	__le32 magic;			/* Magic Number */
138	__le16 major_ver;		/* Major Version */
139	__le16 minor_ver;		/* Minor Version */
140	__le32 log_sectorsize;		/* log2 sector size in bytes */
141	__le32 log_sectors_per_block;	/* log2 # of sectors per block */
142	__le32 log_blocksize;		/* log2 block size in bytes */
143	__le32 log_blocks_per_seg;	/* log2 # of blocks per segment */
144	__le32 segs_per_sec;		/* # of segments per section */
145	__le32 secs_per_zone;		/* # of sections per zone */
146	__le32 checksum_offset;		/* checksum offset inside super block */
147	__le64 block_count;		/* total # of user blocks */
148	__le32 section_count;		/* total # of sections */
149	__le32 segment_count;		/* total # of segments */
150	__le32 segment_count_ckpt;	/* # of segments for checkpoint */
151	__le32 segment_count_sit;	/* # of segments for SIT */
152	__le32 segment_count_nat;	/* # of segments for NAT */
153	__le32 segment_count_ssa;	/* # of segments for SSA */
154	__le32 segment_count_main;	/* # of segments for main area */
155	__le32 segment0_blkaddr;	/* start block address of segment 0 */
156	__le32 cp_blkaddr;		/* start block address of checkpoint */
157	__le32 sit_blkaddr;		/* start block address of SIT */
158	__le32 nat_blkaddr;		/* start block address of NAT */
159	__le32 ssa_blkaddr;		/* start block address of SSA */
160	__le32 main_blkaddr;		/* start block address of main area */
161	__le32 root_ino;		/* root inode number */
162	__le32 node_ino;		/* node inode number */
163	__le32 meta_ino;		/* meta inode number */
164	__u8 uuid[16];			/* 128-bit uuid for volume */
165	__le16 volume_name[512];	/* volume name */
166	__le32 extension_count;		/* # of extensions below */
167	__u8 extension_list[F2FS_MAX_EXTENSION][8];	/* extension array */
168} __attribute__((packed));
169
170/*
171 * For checkpoint
172 */
173#define CP_ERROR_FLAG		0x00000008
174#define CP_COMPACT_SUM_FLAG	0x00000004
175#define CP_ORPHAN_PRESENT_FLAG	0x00000002
176#define CP_UMOUNT_FLAG		0x00000001
177
178struct f2fs_checkpoint {
179	__le64 checkpoint_ver;		/* checkpoint block version number */
180	__le64 user_block_count;	/* # of user blocks */
181	__le64 valid_block_count;	/* # of valid blocks in main area */
182	__le32 rsvd_segment_count;	/* # of reserved segments for gc */
183	__le32 overprov_segment_count;	/* # of overprovision segments */
184	__le32 free_segment_count;	/* # of free segments in main area */
185
186	/* information of current node segments */
187	__le32 cur_node_segno[MAX_ACTIVE_NODE_LOGS];
188	__le16 cur_node_blkoff[MAX_ACTIVE_NODE_LOGS];
189	/* information of current data segments */
190	__le32 cur_data_segno[MAX_ACTIVE_DATA_LOGS];
191	__le16 cur_data_blkoff[MAX_ACTIVE_DATA_LOGS];
192	__le32 ckpt_flags;		/* Flags : umount and journal_present */
193	__le32 cp_pack_total_block_count;	/* total # of one cp pack */
194	__le32 cp_pack_start_sum;	/* start block number of data summary */
195	__le32 valid_node_count;	/* Total number of valid nodes */
196	__le32 valid_inode_count;	/* Total number of valid inodes */
197	__le32 next_free_nid;		/* Next free node number */
198	__le32 sit_ver_bitmap_bytesize;	/* Default value 64 */
199	__le32 nat_ver_bitmap_bytesize; /* Default value 256 */
200	__le32 checksum_offset;		/* checksum offset inside cp block */
201	__le64 elapsed_time;		/* mounted time */
202	/* allocation type of current segment */
203	unsigned char alloc_type[MAX_ACTIVE_LOGS];
204
205	/* SIT and NAT version bitmap */
206	unsigned char sit_nat_version_bitmap[1];
207} __attribute__((packed));
208
209/*
210 * For orphan inode management
211 */
212#define F2FS_ORPHANS_PER_BLOCK	1020
213
214struct f2fs_orphan_block {
215	__le32 ino[F2FS_ORPHANS_PER_BLOCK];	/* inode numbers */
216	__le32 reserved;	/* reserved */
217	__le16 blk_addr;	/* block index in current CP */
218	__le16 blk_count;	/* Number of orphan inode blocks in CP */
219	__le32 entry_count;	/* Total number of orphan nodes in current CP */
220	__le32 check_sum;	/* CRC32 for orphan inode block */
221} __attribute__((packed));
222
223/*
224 * For NODE structure
225 */
226struct f2fs_extent {
227	__le32 fofs;		/* start file offset of the extent */
228	__le32 blk_addr;	/* start block address of the extent */
229	__le32 len;		/* lengh of the extent */
230} __attribute__((packed));
231
232#define F2FS_MAX_NAME_LEN	256
233#define ADDRS_PER_INODE         923	/* Address Pointers in an Inode */
234#define ADDRS_PER_BLOCK         1018	/* Address Pointers in a Direct Block */
235#define NIDS_PER_BLOCK          1018	/* Node IDs in an Indirect Block */
236
237struct f2fs_inode {
238	__le16 i_mode;			/* file mode */
239	__u8 i_advise;			/* file hints */
240	__u8 i_reserved;		/* reserved */
241	__le32 i_uid;			/* user ID */
242	__le32 i_gid;			/* group ID */
243	__le32 i_links;			/* links count */
244	__le64 i_size;			/* file size in bytes */
245	__le64 i_blocks;		/* file size in blocks */
246	__le64 i_atime;			/* access time */
247	__le64 i_ctime;			/* change time */
248	__le64 i_mtime;			/* modification time */
249	__le32 i_atime_nsec;		/* access time in nano scale */
250	__le32 i_ctime_nsec;		/* change time in nano scale */
251	__le32 i_mtime_nsec;		/* modification time in nano scale */
252	__le32 i_generation;		/* file version (for NFS) */
253	__le32 i_current_depth;		/* only for directory depth */
254	__le32 i_xattr_nid;		/* nid to save xattr */
255	__le32 i_flags;			/* file attributes */
256	__le32 i_pino;			/* parent inode number */
257	__le32 i_namelen;		/* file name length */
258	__u8 i_name[F2FS_MAX_NAME_LEN];	/* file name for SPOR */
259
260	struct f2fs_extent i_ext;	/* caching a largest extent */
261
262	__le32 i_addr[ADDRS_PER_INODE];	/* Pointers to data blocks */
263
264	__le32 i_nid[5];		/* direct(2), indirect(2),
265						double_indirect(1) node id */
266} __attribute__((packed));
267
268struct direct_node {
269	__le32 addr[ADDRS_PER_BLOCK];	/* array of data block address */
270} __attribute__((packed));
271
272struct indirect_node {
273	__le32 nid[NIDS_PER_BLOCK];	/* array of data block address */
274} __attribute__((packed));
275
276enum {
277	COLD_BIT_SHIFT = 0,
278	FSYNC_BIT_SHIFT,
279	DENT_BIT_SHIFT,
280	OFFSET_BIT_SHIFT
281};
282
283struct node_footer {
284	__le32 nid;		/* node id */
285	__le32 ino;		/* inode nunmber */
286	__le32 flag;		/* include cold/fsync/dentry marks and offset */
287	__le64 cp_ver;		/* checkpoint version */
288	__le32 next_blkaddr;	/* next node page block address */
289} __attribute__((packed));
290
291struct f2fs_node {
292	/* can be one of three types: inode, direct, and indirect types */
293	union {
294		struct f2fs_inode i;
295		struct direct_node dn;
296		struct indirect_node in;
297	};
298	struct node_footer footer;
299} __attribute__((packed));
300
301/*
302 * For NAT entries
303 */
304#define NAT_ENTRY_PER_BLOCK (PAGE_CACHE_SIZE / sizeof(struct f2fs_nat_entry))
305
306struct f2fs_nat_entry {
307	__u8 version;		/* latest version of cached nat entry */
308	__le32 ino;		/* inode number */
309	__le32 block_addr;	/* block address */
310} __attribute__((packed));
311
312struct f2fs_nat_block {
313	struct f2fs_nat_entry entries[NAT_ENTRY_PER_BLOCK];
314} __attribute__((packed));
315
316/*
317 * For SIT entries
318 *
319 * Each segment is 2MB in size by default so that a bitmap for validity of
320 * there-in blocks should occupy 64 bytes, 512 bits.
321 * Not allow to change this.
322 */
323#define SIT_VBLOCK_MAP_SIZE 64
324#define SIT_ENTRY_PER_BLOCK (PAGE_CACHE_SIZE / sizeof(struct f2fs_sit_entry))
325
326/*
327 * Note that f2fs_sit_entry->vblocks has the following bit-field information.
328 * [15:10] : allocation type such as CURSEG_XXXX_TYPE
329 * [9:0] : valid block count
330 */
331#define SIT_VBLOCKS_SHIFT	10
332#define SIT_VBLOCKS_MASK	((1 << SIT_VBLOCKS_SHIFT) - 1)
333#define GET_SIT_VBLOCKS(raw_sit)				\
334	(le16_to_cpu((raw_sit)->vblocks) & SIT_VBLOCKS_MASK)
335#define GET_SIT_TYPE(raw_sit)					\
336	((le16_to_cpu((raw_sit)->vblocks) & ~SIT_VBLOCKS_MASK)	\
337	 >> SIT_VBLOCKS_SHIFT)
338
339struct f2fs_sit_entry {
340	__le16 vblocks;				/* reference above */
341	__u8 valid_map[SIT_VBLOCK_MAP_SIZE];	/* bitmap for valid blocks */
342	__le64 mtime;				/* segment age for cleaning */
343} __attribute__((packed));
344
345struct f2fs_sit_block {
346	struct f2fs_sit_entry entries[SIT_ENTRY_PER_BLOCK];
347} __attribute__((packed));
348
349/*
350 * For segment summary
351 *
352 * One summary block contains exactly 512 summary entries, which represents
353 * exactly 2MB segment by default. Not allow to change the basic units.
354 *
355 * NOTE: For initializing fields, you must use set_summary
356 *
357 * - If data page, nid represents dnode's nid
358 * - If node page, nid represents the node page's nid.
359 *
360 * The ofs_in_node is used by only data page. It represents offset
361 * from node's page's beginning to get a data block address.
362 * ex) data_blkaddr = (block_t)(nodepage_start_address + ofs_in_node)
363 */
364#define ENTRIES_IN_SUM		512
365#define	SUMMARY_SIZE		(7)	/* sizeof(struct summary) */
366#define	SUM_FOOTER_SIZE		(5)	/* sizeof(struct summary_footer) */
367#define SUM_ENTRY_SIZE		(SUMMARY_SIZE * ENTRIES_IN_SUM)
368
369/* a summary entry for a 4KB-sized block in a segment */
370struct f2fs_summary {
371	__le32 nid;		/* parent node id */
372	union {
373		__u8 reserved[3];
374		struct {
375			__u8 version;		/* node version number */
376			__le16 ofs_in_node;	/* block index in parent node */
377		} __attribute__((packed));
378	};
379} __attribute__((packed));
380
381/* summary block type, node or data, is stored to the summary_footer */
382#define SUM_TYPE_NODE		(1)
383#define SUM_TYPE_DATA		(0)
384
385struct summary_footer {
386	unsigned char entry_type;	/* SUM_TYPE_XXX */
387	__u32 check_sum;		/* summary checksum */
388} __attribute__((packed));
389
390#define SUM_JOURNAL_SIZE	(F2FS_BLKSIZE - SUM_FOOTER_SIZE -\
391				SUM_ENTRY_SIZE)
392#define NAT_JOURNAL_ENTRIES	((SUM_JOURNAL_SIZE - 2) /\
393				sizeof(struct nat_journal_entry))
394#define NAT_JOURNAL_RESERVED	((SUM_JOURNAL_SIZE - 2) %\
395				sizeof(struct nat_journal_entry))
396#define SIT_JOURNAL_ENTRIES	((SUM_JOURNAL_SIZE - 2) /\
397				sizeof(struct sit_journal_entry))
398#define SIT_JOURNAL_RESERVED	((SUM_JOURNAL_SIZE - 2) %\
399				sizeof(struct sit_journal_entry))
400/*
401 * frequently updated NAT/SIT entries can be stored in the spare area in
402 * summary blocks
403 */
404enum {
405	NAT_JOURNAL = 0,
406	SIT_JOURNAL
407};
408
409struct nat_journal_entry {
410	__le32 nid;
411	struct f2fs_nat_entry ne;
412} __attribute__((packed));
413
414struct nat_journal {
415	struct nat_journal_entry entries[NAT_JOURNAL_ENTRIES];
416	__u8 reserved[NAT_JOURNAL_RESERVED];
417} __attribute__((packed));
418
419struct sit_journal_entry {
420	__le32 segno;
421	struct f2fs_sit_entry se;
422} __attribute__((packed));
423
424struct sit_journal {
425	struct sit_journal_entry entries[SIT_JOURNAL_ENTRIES];
426	__u8 reserved[SIT_JOURNAL_RESERVED];
427} __attribute__((packed));
428
429/* 4KB-sized summary block structure */
430struct f2fs_summary_block {
431	struct f2fs_summary entries[ENTRIES_IN_SUM];
432	union {
433		__le16 n_nats;
434		__le16 n_sits;
435	};
436	/* spare area is used by NAT or SIT journals */
437	union {
438		struct nat_journal nat_j;
439		struct sit_journal sit_j;
440	};
441	struct summary_footer footer;
442} __attribute__((packed));
443
444/*
445 * For directory operations
446 */
447#define F2FS_DOT_HASH		0
448#define F2FS_DDOT_HASH		F2FS_DOT_HASH
449#define F2FS_MAX_HASH		(~((0x3ULL) << 62))
450#define F2FS_HASH_COL_BIT	((0x1ULL) << 63)
451
452typedef __le32	f2fs_hash_t;
453
454/* One directory entry slot covers 8bytes-long file name */
455#define F2FS_NAME_LEN		8
456#define F2FS_NAME_LEN_BITS	3
457
458#define GET_DENTRY_SLOTS(x)	((x + F2FS_NAME_LEN - 1) >> F2FS_NAME_LEN_BITS)
459
460/* the number of dentry in a block */
461#define NR_DENTRY_IN_BLOCK	214
462
463/* MAX level for dir lookup */
464#define MAX_DIR_HASH_DEPTH	63
465
466#define SIZE_OF_DIR_ENTRY	11	/* by byte */
467#define SIZE_OF_DENTRY_BITMAP	((NR_DENTRY_IN_BLOCK + BITS_PER_BYTE - 1) / \
468					BITS_PER_BYTE)
469#define SIZE_OF_RESERVED	(PAGE_SIZE - ((SIZE_OF_DIR_ENTRY + \
470				F2FS_NAME_LEN) * \
471				NR_DENTRY_IN_BLOCK + SIZE_OF_DENTRY_BITMAP))
472
473/* One directory entry slot representing F2FS_NAME_LEN-sized file name */
474struct f2fs_dir_entry {
475	__le32 hash_code;	/* hash code of file name */
476	__le32 ino;		/* inode number */
477	__le16 name_len;	/* lengh of file name */
478	__u8 file_type;		/* file type */
479} __attribute__((packed));
480
481/* 4KB-sized directory entry block */
482struct f2fs_dentry_block {
483	/* validity bitmap for directory entries in each block */
484	__u8 dentry_bitmap[SIZE_OF_DENTRY_BITMAP];
485	__u8 reserved[SIZE_OF_RESERVED];
486	struct f2fs_dir_entry dentry[NR_DENTRY_IN_BLOCK];
487	__u8 filename[NR_DENTRY_IN_BLOCK][F2FS_NAME_LEN];
488} __attribute__((packed));
489
490/* file types used in inode_info->flags */
491enum {
492	F2FS_FT_UNKNOWN,
493	F2FS_FT_REG_FILE,
494	F2FS_FT_DIR,
495	F2FS_FT_CHRDEV,
496	F2FS_FT_BLKDEV,
497	F2FS_FT_FIFO,
498	F2FS_FT_SOCK,
499	F2FS_FT_SYMLINK,
500	F2FS_FT_MAX
501};
502
503#endif	//__F2FS_FS_H__
504