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