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