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