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