f2fs.h revision 7f35b548d4b0e3c8577ad7a09433e589a0ab3f2a
1/**
2 * f2fs.h
3 *
4 * Copyright (c) 2013 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_H_
12#define _F2FS_H_
13
14#include <stdlib.h>
15#include <unistd.h>
16#include <stdio.h>
17#include <errno.h>
18#include <fcntl.h>
19#include <string.h>
20#include <errno.h>
21#include <mntent.h>
22#include <linux/types.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <sys/ioctl.h>
26#include <sys/mount.h>
27#include <assert.h>
28
29#include <list.h>
30#include <f2fs_fs.h>
31
32#define EXIT_ERR_CODE		(-1)
33#define ver_after(a, b) (typecheck(unsigned long long, a) &&            \
34		typecheck(unsigned long long, b) &&                     \
35		((long long)((a) - (b)) > 0))
36
37enum {
38	NAT_BITMAP,
39	SIT_BITMAP
40};
41
42struct node_info {
43	nid_t nid;
44	nid_t ino;
45	u32 blk_addr;
46	unsigned char version;
47};
48
49struct f2fs_nm_info {
50	block_t nat_blkaddr;
51	nid_t max_nid;
52	nid_t init_scan_nid;
53	nid_t next_scan_nid;
54
55	unsigned int nat_cnt;
56	unsigned int fcnt;
57
58	char *nat_bitmap;
59	int bitmap_size;
60};
61
62struct seg_entry {
63	unsigned short valid_blocks;    /* # of valid blocks */
64	unsigned char *cur_valid_map;   /* validity bitmap of blocks */
65	/*
66	 * # of valid blocks and the validity bitmap stored in the the last
67	 * checkpoint pack. This information is used by the SSR mode.
68	 */
69	unsigned short ckpt_valid_blocks;
70	unsigned char *ckpt_valid_map;
71	unsigned char type;             /* segment type like CURSEG_XXX_TYPE */
72	unsigned long long mtime;       /* modification time of the segment */
73};
74
75struct sec_entry {
76	unsigned int valid_blocks;      /* # of valid blocks in a section */
77};
78
79struct sit_info {
80
81	block_t sit_base_addr;          /* start block address of SIT area */
82	block_t sit_blocks;             /* # of blocks used by SIT area */
83	block_t written_valid_blocks;   /* # of valid blocks in main area */
84	char *sit_bitmap;               /* SIT bitmap pointer */
85	unsigned int bitmap_size;       /* SIT bitmap size */
86
87	unsigned long *dirty_sentries_bitmap;   /* bitmap for dirty sentries */
88	unsigned int dirty_sentries;            /* # of dirty sentries */
89	unsigned int sents_per_block;           /* # of SIT entries per block */
90	struct seg_entry *sentries;             /* SIT segment-level cache */
91	struct sec_entry *sec_entries;          /* SIT section-level cache */
92
93	unsigned long long elapsed_time;        /* elapsed time after mount */
94	unsigned long long mounted_time;        /* mount time */
95	unsigned long long min_mtime;           /* min. modification time */
96	unsigned long long max_mtime;           /* max. modification time */
97};
98
99struct curseg_info {
100	struct f2fs_summary_block *sum_blk;     /* cached summary block */
101	unsigned char alloc_type;               /* current allocation type */
102	unsigned int segno;                     /* current segment number */
103	unsigned short next_blkoff;             /* next block offset to write */
104	unsigned int zone;                      /* current zone number */
105	unsigned int next_segno;                /* preallocated segment */
106};
107
108struct f2fs_sm_info {
109	struct sit_info *sit_info;
110	struct curseg_info *curseg_array;
111
112	block_t seg0_blkaddr;
113	block_t main_blkaddr;
114	block_t ssa_blkaddr;
115
116	unsigned int segment_count;
117	unsigned int main_segments;
118	unsigned int reserved_segments;
119	unsigned int ovp_segments;
120};
121
122struct f2fs_sb_info {
123	struct f2fs_fsck *fsck;
124
125	struct f2fs_super_block *raw_super;
126	struct f2fs_nm_info *nm_info;
127	struct f2fs_sm_info *sm_info;
128	struct f2fs_checkpoint *ckpt;
129
130	struct list_head orphan_inode_list;
131	unsigned int n_orphans;
132
133	/* basic file system units */
134	unsigned int log_sectors_per_block;     /* log2 sectors per block */
135	unsigned int log_blocksize;             /* log2 block size */
136	unsigned int blocksize;                 /* block size */
137	unsigned int root_ino_num;              /* root inode number*/
138	unsigned int node_ino_num;              /* node inode number*/
139	unsigned int meta_ino_num;              /* meta inode number*/
140	unsigned int log_blocks_per_seg;        /* log2 blocks per segment */
141	unsigned int blocks_per_seg;            /* blocks per segment */
142	unsigned int segs_per_sec;              /* segments per section */
143	unsigned int secs_per_zone;             /* sections per zone */
144	unsigned int total_sections;            /* total section count */
145	unsigned int total_node_count;          /* total node block count */
146	unsigned int total_valid_node_count;    /* valid node block count */
147	unsigned int total_valid_inode_count;   /* valid inode count */
148	int active_logs;                        /* # of active logs */
149
150	block_t user_block_count;               /* # of user blocks */
151	block_t total_valid_block_count;        /* # of valid blocks */
152	block_t alloc_valid_block_count;        /* # of allocated blocks */
153	block_t last_valid_block_count;         /* for recovery */
154	u32 s_next_generation;                  /* for NFS support */
155
156	unsigned int cur_victim_sec;            /* current victim section num */
157
158};
159
160static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
161{
162	return (struct f2fs_super_block *)(sbi->raw_super);
163}
164
165static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
166{
167	return (struct f2fs_checkpoint *)(sbi->ckpt);
168}
169
170static inline struct f2fs_fsck *F2FS_FSCK(struct f2fs_sb_info *sbi)
171{
172	return (struct f2fs_fsck *)(sbi->fsck);
173}
174
175static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
176{
177	return (struct f2fs_nm_info *)(sbi->nm_info);
178}
179
180static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
181{
182	return (struct f2fs_sm_info *)(sbi->sm_info);
183}
184
185static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
186{
187	return (struct sit_info *)(SM_I(sbi)->sit_info);
188}
189
190static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
191{
192	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
193
194	/* return NAT or SIT bitmap */
195	if (flag == NAT_BITMAP)
196		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
197	else if (flag == SIT_BITMAP)
198		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
199
200	return 0;
201}
202
203static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
204{
205	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
206	int offset = (flag == NAT_BITMAP) ?
207		le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
208	return &ckpt->sit_nat_version_bitmap + offset;
209}
210
211static inline bool is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
212{
213	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
214	return ckpt_flags & f;
215}
216
217static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
218{
219	block_t start_addr;
220	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
221	unsigned long long ckpt_version = le64_to_cpu(ckpt->checkpoint_ver);
222
223	start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
224
225	/*
226	 * odd numbered checkpoint should at cp segment 0
227	 * and even segent must be at cp segment 1
228	 */
229	if (!(ckpt_version & 1))
230		start_addr += sbi->blocks_per_seg;
231
232	return start_addr;
233}
234
235static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
236{
237	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
238}
239
240#define GET_ZONENO_FROM_SEGNO(sbi, segno)                               \
241	((segno / sbi->segs_per_sec) / sbi->secs_per_zone)
242
243#define IS_DATASEG(t)                                                   \
244	((t == CURSEG_HOT_DATA) || (t == CURSEG_COLD_DATA) ||           \
245	 (t == CURSEG_WARM_DATA))
246
247#define IS_NODESEG(t)                                                   \
248	((t == CURSEG_HOT_NODE) || (t == CURSEG_COLD_NODE) ||           \
249	 (t == CURSEG_WARM_NODE))
250
251#define GET_SUM_BLKADDR(sbi, segno)					\
252	((sbi->sm_info->ssa_blkaddr) + segno)
253
254#define GET_SEGOFF_FROM_SEG0(sbi, blk_addr)				\
255	((blk_addr) - SM_I(sbi)->seg0_blkaddr)
256
257#define GET_SEGNO_FROM_SEG0(sbi, blk_addr)				\
258	(GET_SEGOFF_FROM_SEG0(sbi, blk_addr) >> sbi->log_blocks_per_seg)
259
260#define FREE_I_START_SEGNO(sbi)		GET_SEGNO_FROM_SEG0(sbi, SM_I(sbi)->main_blkaddr)
261#define GET_R2L_SEGNO(sbi, segno)	(segno + FREE_I_START_SEGNO(sbi))
262
263#define START_BLOCK(sbi, segno)	(SM_I(sbi)->main_blkaddr + (segno << sbi->log_blocks_per_seg))
264
265static inline struct curseg_info *CURSEG_I(struct f2fs_sb_info *sbi, int type)
266{
267	return (struct curseg_info *)(SM_I(sbi)->curseg_array + type);
268}
269
270static inline block_t start_sum_block(struct f2fs_sb_info *sbi)
271{
272	return __start_cp_addr(sbi) + le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
273}
274
275static inline block_t sum_blk_addr(struct f2fs_sb_info *sbi, int base, int type)
276{
277	return __start_cp_addr(sbi) + le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_total_block_count)
278		- (base + 1) + type;
279}
280
281
282#define nats_in_cursum(sum)             (le16_to_cpu(sum->n_nats))
283#define sits_in_cursum(sum)             (le16_to_cpu(sum->n_sits))
284
285#define nat_in_journal(sum, i)          (sum->nat_j.entries[i].ne)
286#define nid_in_journal(sum, i)          (sum->nat_j.entries[i].nid)
287#define sit_in_journal(sum, i)          (sum->sit_j.entries[i].se)
288#define segno_in_journal(sum, i)        (sum->sit_j.entries[i].segno)
289
290#define SIT_ENTRY_OFFSET(sit_i, segno)                                  \
291	(segno % sit_i->sents_per_block)
292#define SIT_BLOCK_OFFSET(sit_i, segno)                                  \
293	(segno / SIT_ENTRY_PER_BLOCK)
294#define TOTAL_SEGS(sbi) (SM_I(sbi)->main_segments)
295
296#define IS_VALID_NID(sbi, nid) 			\
297	do {						\
298		ASSERT(nid <= (NAT_ENTRY_PER_BLOCK *	\
299					F2FS_RAW_SUPER(sbi)->segment_count_nat	\
300					<< (sbi->log_blocks_per_seg - 1)));	\
301	} while (0);
302
303#define IS_VALID_BLK_ADDR(sbi, addr)				\
304	do {							\
305		if (addr >= F2FS_RAW_SUPER(sbi)->block_count ||	 	\
306				addr < SM_I(sbi)->main_blkaddr)	\
307		{						\
308			DBG(0, "block addr [0x%x]\n", addr);	\
309			ASSERT(addr <  F2FS_RAW_SUPER(sbi)->block_count);	\
310			ASSERT(addr >= SM_I(sbi)->main_blkaddr);	\
311		}						\
312	} while (0);
313
314static inline u64 BLKOFF_FROM_MAIN(struct f2fs_sb_info *sbi, u64 blk_addr)
315{
316	ASSERT(blk_addr >= SM_I(sbi)->main_blkaddr);
317	return blk_addr - SM_I(sbi)->main_blkaddr;
318}
319
320static inline u32 GET_SEGNO(struct f2fs_sb_info *sbi, u64 blk_addr)
321{
322	return (u32)(BLKOFF_FROM_MAIN(sbi, blk_addr)
323			>> sbi->log_blocks_per_seg);
324}
325
326static inline u32 OFFSET_IN_SEG(struct f2fs_sb_info *sbi, u64 blk_addr)
327{
328	return (u32)(BLKOFF_FROM_MAIN(sbi, blk_addr)
329			% (1 << sbi->log_blocks_per_seg));
330}
331
332static inline void node_info_from_raw_nat(struct node_info *ni,
333		struct f2fs_nat_entry *raw_nat)
334{
335	ni->ino = le32_to_cpu(raw_nat->ino);
336	ni->blk_addr = le32_to_cpu(raw_nat->block_addr);
337	ni->version = raw_nat->version;
338}
339
340extern int lookup_nat_in_journal(struct f2fs_sb_info *sbi, u32 nid, struct f2fs_nat_entry *ne);
341#define IS_SUM_NODE_SEG(footer)		(footer.entry_type == SUM_TYPE_NODE)
342
343#endif /* _F2FS_H_ */
344