• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <f2fs_fs.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <stdio.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <string.h>
21 #include <errno.h>
22 #ifdef HAVE_MNTENT_H
23 #include <mntent.h>
24 #endif
25 #include <sys/stat.h>
26 #include <sys/ioctl.h>
27 #include <sys/mount.h>
28 #include <assert.h>
29 
30 #define EXIT_ERR_CODE		(-1)
31 #define ver_after(a, b) (typecheck(unsigned long long, a) &&            \
32 		typecheck(unsigned long long, b) &&                     \
33 		((long long)((a) - (b)) > 0))
34 
35 struct list_head {
36 	struct list_head *next, *prev;
37 };
38 
39 enum {
40 	NAT_BITMAP,
41 	SIT_BITMAP
42 };
43 
44 struct node_info {
45 	nid_t nid;
46 	nid_t ino;
47 	u32 blk_addr;
48 	unsigned char version;
49 };
50 
51 struct f2fs_nm_info {
52 	block_t nat_blkaddr;
53 	block_t nat_blocks;
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 	char *nid_bitmap;
64 };
65 
66 struct seg_entry {
67 	unsigned short valid_blocks;    /* # of valid blocks */
68 	unsigned char *cur_valid_map;   /* validity bitmap of blocks */
69 	unsigned char type;             /* segment type like CURSEG_XXX_TYPE */
70 	unsigned char orig_type;        /* segment type like CURSEG_XXX_TYPE */
71 	unsigned long long mtime;       /* modification time of the segment */
72 	int dirty;
73 };
74 
75 struct sec_entry {
76 	unsigned int valid_blocks;      /* # of valid blocks in a section */
77 };
78 
79 struct 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 
99 struct 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 
108 struct 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 
122 struct f2fs_dentry_ptr {
123 	struct inode *inode;
124 	u8 *bitmap;
125 	struct f2fs_dir_entry *dentry;
126 	__u8 (*filename)[F2FS_SLOT_LEN];
127 	int max;
128 	int nr_bitmap;
129 };
130 
131 struct dentry {
132 	char *path;
133 	char *full_path;
134 	const u8 *name;
135 	int len;
136 	char *link;
137 	unsigned long size;
138 	u8 file_type;
139 	u16 mode;
140 	u16 uid;
141 	u16 gid;
142 	u32 *inode;
143 	u32 mtime;
144 	char *secon;
145 	uint64_t capabilities;
146 	nid_t ino;
147 	nid_t pino;
148 };
149 
150 /* different from dnode_of_data in kernel */
151 struct dnode_of_data {
152 	struct f2fs_node *inode_blk;	/* inode page */
153 	struct f2fs_node *node_blk;	/* cached direct node page */
154 	nid_t nid;
155 	unsigned int ofs_in_node;
156 	block_t data_blkaddr;
157 	block_t node_blkaddr;
158 	int idirty, ndirty;
159 };
160 
161 struct f2fs_sb_info {
162 	struct f2fs_fsck *fsck;
163 
164 	struct f2fs_super_block *raw_super;
165 	struct f2fs_nm_info *nm_info;
166 	struct f2fs_sm_info *sm_info;
167 	struct f2fs_checkpoint *ckpt;
168 	int cur_cp;
169 
170 	struct list_head orphan_inode_list;
171 	unsigned int n_orphans;
172 
173 	/* basic file system units */
174 	unsigned int log_sectors_per_block;     /* log2 sectors per block */
175 	unsigned int log_blocksize;             /* log2 block size */
176 	unsigned int blocksize;                 /* block size */
177 	unsigned int root_ino_num;              /* root inode number*/
178 	unsigned int node_ino_num;              /* node inode number*/
179 	unsigned int meta_ino_num;              /* meta inode number*/
180 	unsigned int log_blocks_per_seg;        /* log2 blocks per segment */
181 	unsigned int blocks_per_seg;            /* blocks per segment */
182 	unsigned int segs_per_sec;              /* segments per section */
183 	unsigned int secs_per_zone;             /* sections per zone */
184 	unsigned int total_sections;            /* total section count */
185 	unsigned int total_node_count;          /* total node block count */
186 	unsigned int total_valid_node_count;    /* valid node block count */
187 	unsigned int total_valid_inode_count;   /* valid inode count */
188 	int active_logs;                        /* # of active logs */
189 
190 	block_t user_block_count;               /* # of user blocks */
191 	block_t total_valid_block_count;        /* # of valid blocks */
192 	block_t alloc_valid_block_count;        /* # of allocated blocks */
193 	block_t last_valid_block_count;         /* for recovery */
194 	u32 s_next_generation;                  /* for NFS support */
195 
196 	unsigned int cur_victim_sec;            /* current victim section num */
197 	u32 free_segments;
198 };
199 
F2FS_RAW_SUPER(struct f2fs_sb_info * sbi)200 static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
201 {
202 	return (struct f2fs_super_block *)(sbi->raw_super);
203 }
204 
F2FS_CKPT(struct f2fs_sb_info * sbi)205 static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
206 {
207 	return (struct f2fs_checkpoint *)(sbi->ckpt);
208 }
209 
F2FS_FSCK(struct f2fs_sb_info * sbi)210 static inline struct f2fs_fsck *F2FS_FSCK(struct f2fs_sb_info *sbi)
211 {
212 	return (struct f2fs_fsck *)(sbi->fsck);
213 }
214 
NM_I(struct f2fs_sb_info * sbi)215 static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
216 {
217 	return (struct f2fs_nm_info *)(sbi->nm_info);
218 }
219 
SM_I(struct f2fs_sb_info * sbi)220 static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
221 {
222 	return (struct f2fs_sm_info *)(sbi->sm_info);
223 }
224 
SIT_I(struct f2fs_sb_info * sbi)225 static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
226 {
227 	return (struct sit_info *)(SM_I(sbi)->sit_info);
228 }
229 
inline_data_addr(struct f2fs_node * node_blk)230 static inline void *inline_data_addr(struct f2fs_node *node_blk)
231 {
232 	int ofs = get_extra_isize(node_blk) + DEF_INLINE_RESERVED_SIZE;
233 
234 	return (void *)&(node_blk->i.i_addr[ofs]);
235 }
236 
ofs_of_node(struct f2fs_node * node_blk)237 static inline unsigned int ofs_of_node(struct f2fs_node *node_blk)
238 {
239 	unsigned flag = le32_to_cpu(node_blk->footer.flag);
240 	return flag >> OFFSET_BIT_SHIFT;
241 }
242 
__bitmap_size(struct f2fs_sb_info * sbi,int flag)243 static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
244 {
245 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
246 
247 	/* return NAT or SIT bitmap */
248 	if (flag == NAT_BITMAP)
249 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
250 	else if (flag == SIT_BITMAP)
251 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
252 
253 	return 0;
254 }
255 
__bitmap_ptr(struct f2fs_sb_info * sbi,int flag)256 static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
257 {
258 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
259 	int offset;
260 	if (le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload) > 0) {
261 		if (flag == NAT_BITMAP)
262 			return &ckpt->sit_nat_version_bitmap;
263 		else
264 			return ((char *)ckpt + F2FS_BLKSIZE);
265 	} else {
266 		offset = (flag == NAT_BITMAP) ?
267 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
268 		return &ckpt->sit_nat_version_bitmap + offset;
269 	}
270 }
271 
is_set_ckpt_flags(struct f2fs_checkpoint * cp,unsigned int f)272 static inline bool is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
273 {
274 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
275 	return ckpt_flags & f ? 1 : 0;
276 }
277 
__start_cp_addr(struct f2fs_sb_info * sbi)278 static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
279 {
280 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
281 
282 	if (sbi->cur_cp == 2)
283 		start_addr += sbi->blocks_per_seg;
284 	return start_addr;
285 }
286 
__start_sum_addr(struct f2fs_sb_info * sbi)287 static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
288 {
289 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
290 }
291 
__end_block_addr(struct f2fs_sb_info * sbi)292 static inline block_t __end_block_addr(struct f2fs_sb_info *sbi)
293 {
294 	block_t end = SM_I(sbi)->main_blkaddr;
295 	return end + le64_to_cpu(F2FS_RAW_SUPER(sbi)->block_count);
296 }
297 
298 #define GET_ZONENO_FROM_SEGNO(sbi, segno)                               \
299 	((segno / sbi->segs_per_sec) / sbi->secs_per_zone)
300 
301 #define IS_DATASEG(t)                                                   \
302 	((t == CURSEG_HOT_DATA) || (t == CURSEG_COLD_DATA) ||           \
303 	 (t == CURSEG_WARM_DATA))
304 
305 #define IS_NODESEG(t)                                                   \
306 	((t == CURSEG_HOT_NODE) || (t == CURSEG_COLD_NODE) ||           \
307 	 (t == CURSEG_WARM_NODE))
308 
309 #define GET_SUM_BLKADDR(sbi, segno)					\
310 	((sbi->sm_info->ssa_blkaddr) + segno)
311 
312 #define GET_SEGOFF_FROM_SEG0(sbi, blk_addr)				\
313 	((blk_addr) - SM_I(sbi)->seg0_blkaddr)
314 
315 #define GET_SEGNO_FROM_SEG0(sbi, blk_addr)				\
316 	(GET_SEGOFF_FROM_SEG0(sbi, blk_addr) >> sbi->log_blocks_per_seg)
317 
318 #define GET_BLKOFF_FROM_SEG0(sbi, blk_addr)				\
319 	(GET_SEGOFF_FROM_SEG0(sbi, blk_addr) & (sbi->blocks_per_seg - 1))
320 
321 #define FREE_I_START_SEGNO(sbi)						\
322 	GET_SEGNO_FROM_SEG0(sbi, SM_I(sbi)->main_blkaddr)
323 #define GET_R2L_SEGNO(sbi, segno)	(segno + FREE_I_START_SEGNO(sbi))
324 
325 #define START_BLOCK(sbi, segno)	(SM_I(sbi)->main_blkaddr +		\
326 	((segno) << sbi->log_blocks_per_seg))
327 
CURSEG_I(struct f2fs_sb_info * sbi,int type)328 static inline struct curseg_info *CURSEG_I(struct f2fs_sb_info *sbi, int type)
329 {
330 	return (struct curseg_info *)(SM_I(sbi)->curseg_array + type);
331 }
332 
start_sum_block(struct f2fs_sb_info * sbi)333 static inline block_t start_sum_block(struct f2fs_sb_info *sbi)
334 {
335 	return __start_cp_addr(sbi) + le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
336 }
337 
sum_blk_addr(struct f2fs_sb_info * sbi,int base,int type)338 static inline block_t sum_blk_addr(struct f2fs_sb_info *sbi, int base, int type)
339 {
340 	return __start_cp_addr(sbi) + le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_total_block_count)
341 		- (base + 1) + type;
342 }
343 
344 #define nats_in_cursum(jnl)             (le16_to_cpu(jnl->n_nats))
345 #define sits_in_cursum(jnl)             (le16_to_cpu(jnl->n_sits))
346 
347 #define nat_in_journal(jnl, i)          (jnl->nat_j.entries[i].ne)
348 #define nid_in_journal(jnl, i)          (jnl->nat_j.entries[i].nid)
349 #define sit_in_journal(jnl, i)          (jnl->sit_j.entries[i].se)
350 #define segno_in_journal(jnl, i)        (jnl->sit_j.entries[i].segno)
351 
352 #define SIT_ENTRY_OFFSET(sit_i, segno)                                  \
353 	((segno) % sit_i->sents_per_block)
354 #define SIT_BLOCK_OFFSET(sit_i, segno)                                  \
355 	((segno) / SIT_ENTRY_PER_BLOCK)
356 #define TOTAL_SEGS(sbi) (SM_I(sbi)->main_segments)
357 
IS_VALID_NID(struct f2fs_sb_info * sbi,u32 nid)358 static inline bool IS_VALID_NID(struct f2fs_sb_info *sbi, u32 nid)
359 {
360 	return (nid <= (NAT_ENTRY_PER_BLOCK *
361 			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_nat)
362 			<< (sbi->log_blocks_per_seg - 1)));
363 }
364 
IS_VALID_BLK_ADDR(struct f2fs_sb_info * sbi,u32 addr)365 static inline bool IS_VALID_BLK_ADDR(struct f2fs_sb_info *sbi, u32 addr)
366 {
367 	if (addr >= le64_to_cpu(F2FS_RAW_SUPER(sbi)->block_count) ||
368 				addr < SM_I(sbi)->main_blkaddr) {
369 		DBG(1, "block addr [0x%x]\n", addr);
370 		return 0;
371 	}
372 	/* next block offset will be checked at the end of fsck. */
373 	return 1;
374 }
375 
IS_CUR_SEGNO(struct f2fs_sb_info * sbi,u32 segno,int type)376 static inline int IS_CUR_SEGNO(struct f2fs_sb_info *sbi, u32 segno, int type)
377 {
378 	int i;
379 
380 	for (i = 0; i < NO_CHECK_TYPE; i++) {
381 		struct curseg_info *curseg = CURSEG_I(sbi, i);
382 
383 		if (type == i)
384 			continue;
385 
386 		if (segno == curseg->segno)
387 			return 1;
388 	}
389 	return 0;
390 }
391 
BLKOFF_FROM_MAIN(struct f2fs_sb_info * sbi,u64 blk_addr)392 static inline u64 BLKOFF_FROM_MAIN(struct f2fs_sb_info *sbi, u64 blk_addr)
393 {
394 	ASSERT(blk_addr >= SM_I(sbi)->main_blkaddr);
395 	return blk_addr - SM_I(sbi)->main_blkaddr;
396 }
397 
GET_SEGNO(struct f2fs_sb_info * sbi,u64 blk_addr)398 static inline u32 GET_SEGNO(struct f2fs_sb_info *sbi, u64 blk_addr)
399 {
400 	return (u32)(BLKOFF_FROM_MAIN(sbi, blk_addr)
401 			>> sbi->log_blocks_per_seg);
402 }
403 
OFFSET_IN_SEG(struct f2fs_sb_info * sbi,u64 blk_addr)404 static inline u32 OFFSET_IN_SEG(struct f2fs_sb_info *sbi, u64 blk_addr)
405 {
406 	return (u32)(BLKOFF_FROM_MAIN(sbi, blk_addr)
407 			% (1 << sbi->log_blocks_per_seg));
408 }
409 
node_info_from_raw_nat(struct node_info * ni,struct f2fs_nat_entry * raw_nat)410 static inline void node_info_from_raw_nat(struct node_info *ni,
411 		struct f2fs_nat_entry *raw_nat)
412 {
413 	ni->ino = le32_to_cpu(raw_nat->ino);
414 	ni->blk_addr = le32_to_cpu(raw_nat->block_addr);
415 	ni->version = raw_nat->version;
416 }
417 
set_summary(struct f2fs_summary * sum,nid_t nid,unsigned int ofs_in_node,unsigned char version)418 static inline void set_summary(struct f2fs_summary *sum, nid_t nid,
419 			unsigned int ofs_in_node, unsigned char version)
420 {
421 	sum->nid = cpu_to_le32(nid);
422 	sum->ofs_in_node = cpu_to_le16(ofs_in_node);
423 	sum->version = version;
424 }
425 
426 #define S_SHIFT 12
427 static unsigned char f2fs_type_by_mode[S_IFMT >> S_SHIFT] = {
428 	[S_IFREG >> S_SHIFT]    = F2FS_FT_REG_FILE,
429 	[S_IFDIR >> S_SHIFT]    = F2FS_FT_DIR,
430 	[S_IFCHR >> S_SHIFT]    = F2FS_FT_CHRDEV,
431 	[S_IFBLK >> S_SHIFT]    = F2FS_FT_BLKDEV,
432 	[S_IFIFO >> S_SHIFT]    = F2FS_FT_FIFO,
433 	[S_IFSOCK >> S_SHIFT]   = F2FS_FT_SOCK,
434 	[S_IFLNK >> S_SHIFT]    = F2FS_FT_SYMLINK,
435 };
436 
map_de_type(umode_t mode)437 static inline int map_de_type(umode_t mode)
438 {
439        return f2fs_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
440 }
441 
inline_xattr_addr(struct f2fs_inode * inode)442 static inline void *inline_xattr_addr(struct f2fs_inode *inode)
443 {
444 	return (void *)&(inode->i_addr[DEF_ADDRS_PER_INODE -
445 				get_inline_xattr_addrs(inode)]);
446 }
447 
inline_xattr_size(struct f2fs_inode * inode)448 static inline int inline_xattr_size(struct f2fs_inode *inode)
449 {
450 	return get_inline_xattr_addrs(inode) * sizeof(__le32);
451 }
452 
453 extern int lookup_nat_in_journal(struct f2fs_sb_info *sbi, u32 nid, struct f2fs_nat_entry *ne);
454 #define IS_SUM_NODE_SEG(footer)		(footer.entry_type == SUM_TYPE_NODE)
455 #define IS_SUM_DATA_SEG(footer)		(footer.entry_type == SUM_TYPE_DATA)
456 
dir_buckets(unsigned int level,int dir_level)457 static inline unsigned int dir_buckets(unsigned int level, int dir_level)
458 {
459 	if (level + dir_level < MAX_DIR_HASH_DEPTH / 2)
460 		return 1 << (level + dir_level);
461 	else
462 		return MAX_DIR_BUCKETS;
463 }
464 
bucket_blocks(unsigned int level)465 static inline unsigned int bucket_blocks(unsigned int level)
466 {
467 	if (level < MAX_DIR_HASH_DEPTH / 2)
468 		return 2;
469 	else
470 		return 4;
471 }
472 
dir_block_index(unsigned int level,int dir_level,unsigned int idx)473 static inline unsigned long dir_block_index(unsigned int level,
474 				int dir_level, unsigned int idx)
475 {
476 	unsigned long i;
477 	unsigned long bidx = 0;
478 
479 	for (i = 0; i < level; i++)
480 		bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
481 	bidx += idx * bucket_blocks(level);
482 	return bidx;
483 }
484 
is_dot_dotdot(const unsigned char * name,const int len)485 static inline int is_dot_dotdot(const unsigned char *name, const int len)
486 {
487 	if (len == 1 && name[0] == '.')
488 		return 1;
489 	if (len == 2 && name[0] == '.' && name[1] == '.')
490 		return 1;
491 	return 0;
492 }
493 
494 #endif /* _F2FS_H_ */
495