Home | History | Annotate | Download | only in fsck
      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 
    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 
    205 static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
    206 {
    207 	return (struct f2fs_checkpoint *)(sbi->ckpt);
    208 }
    209 
    210 static inline struct f2fs_fsck *F2FS_FSCK(struct f2fs_sb_info *sbi)
    211 {
    212 	return (struct f2fs_fsck *)(sbi->fsck);
    213 }
    214 
    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 
    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 
    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 
    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 
    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 
    243 static inline bool is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
    244 {
    245 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
    246 	return ckpt_flags & f ? 1 : 0;
    247 }
    248 
    249 static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
    250 {
    251 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
    252 
    253 	/* return NAT or SIT bitmap */
    254 	if (flag == NAT_BITMAP)
    255 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
    256 	else if (flag == SIT_BITMAP)
    257 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
    258 
    259 	return 0;
    260 }
    261 
    262 static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
    263 {
    264 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
    265 }
    266 
    267 static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
    268 {
    269 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
    270 	int offset;
    271 
    272 	if (is_set_ckpt_flags(ckpt, CP_LARGE_NAT_BITMAP_FLAG)) {
    273 		offset = (flag == SIT_BITMAP) ?
    274 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
    275 		return &ckpt->sit_nat_version_bitmap + offset;
    276 	}
    277 
    278 	if (le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload) > 0) {
    279 		if (flag == NAT_BITMAP)
    280 			return &ckpt->sit_nat_version_bitmap;
    281 		else
    282 			return ((char *)ckpt + F2FS_BLKSIZE);
    283 	} else {
    284 		offset = (flag == NAT_BITMAP) ?
    285 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
    286 		return &ckpt->sit_nat_version_bitmap + offset;
    287 	}
    288 }
    289 
    290 static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
    291 {
    292 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
    293 
    294 	if (sbi->cur_cp == 2)
    295 		start_addr += sbi->blocks_per_seg;
    296 	return start_addr;
    297 }
    298 
    299 static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
    300 {
    301 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
    302 }
    303 
    304 static inline block_t __end_block_addr(struct f2fs_sb_info *sbi)
    305 {
    306 	block_t end = SM_I(sbi)->main_blkaddr;
    307 	return end + le64_to_cpu(F2FS_RAW_SUPER(sbi)->block_count);
    308 }
    309 
    310 #define GET_ZONENO_FROM_SEGNO(sbi, segno)                               \
    311 	((segno / sbi->segs_per_sec) / sbi->secs_per_zone)
    312 
    313 #define IS_DATASEG(t)                                                   \
    314 	((t == CURSEG_HOT_DATA) || (t == CURSEG_COLD_DATA) ||           \
    315 	 (t == CURSEG_WARM_DATA))
    316 
    317 #define IS_NODESEG(t)                                                   \
    318 	((t == CURSEG_HOT_NODE) || (t == CURSEG_COLD_NODE) ||           \
    319 	 (t == CURSEG_WARM_NODE))
    320 
    321 #define GET_SUM_BLKADDR(sbi, segno)					\
    322 	((sbi->sm_info->ssa_blkaddr) + segno)
    323 
    324 #define GET_SEGOFF_FROM_SEG0(sbi, blk_addr)				\
    325 	((blk_addr) - SM_I(sbi)->seg0_blkaddr)
    326 
    327 #define GET_SEGNO_FROM_SEG0(sbi, blk_addr)				\
    328 	(GET_SEGOFF_FROM_SEG0(sbi, blk_addr) >> sbi->log_blocks_per_seg)
    329 
    330 #define GET_BLKOFF_FROM_SEG0(sbi, blk_addr)				\
    331 	(GET_SEGOFF_FROM_SEG0(sbi, blk_addr) & (sbi->blocks_per_seg - 1))
    332 
    333 #define FREE_I_START_SEGNO(sbi)						\
    334 	GET_SEGNO_FROM_SEG0(sbi, SM_I(sbi)->main_blkaddr)
    335 #define GET_R2L_SEGNO(sbi, segno)	(segno + FREE_I_START_SEGNO(sbi))
    336 
    337 #define START_BLOCK(sbi, segno)	(SM_I(sbi)->main_blkaddr +		\
    338 	((segno) << sbi->log_blocks_per_seg))
    339 
    340 static inline struct curseg_info *CURSEG_I(struct f2fs_sb_info *sbi, int type)
    341 {
    342 	return (struct curseg_info *)(SM_I(sbi)->curseg_array + type);
    343 }
    344 
    345 static inline block_t start_sum_block(struct f2fs_sb_info *sbi)
    346 {
    347 	return __start_cp_addr(sbi) + le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
    348 }
    349 
    350 static inline block_t sum_blk_addr(struct f2fs_sb_info *sbi, int base, int type)
    351 {
    352 	return __start_cp_addr(sbi) + le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_total_block_count)
    353 		- (base + 1) + type;
    354 }
    355 
    356 #define nats_in_cursum(jnl)             (le16_to_cpu(jnl->n_nats))
    357 #define sits_in_cursum(jnl)             (le16_to_cpu(jnl->n_sits))
    358 
    359 #define nat_in_journal(jnl, i)          (jnl->nat_j.entries[i].ne)
    360 #define nid_in_journal(jnl, i)          (jnl->nat_j.entries[i].nid)
    361 #define sit_in_journal(jnl, i)          (jnl->sit_j.entries[i].se)
    362 #define segno_in_journal(jnl, i)        (jnl->sit_j.entries[i].segno)
    363 
    364 #define SIT_ENTRY_OFFSET(sit_i, segno)                                  \
    365 	((segno) % sit_i->sents_per_block)
    366 #define SIT_BLOCK_OFFSET(sit_i, segno)                                  \
    367 	((segno) / SIT_ENTRY_PER_BLOCK)
    368 #define TOTAL_SEGS(sbi) (SM_I(sbi)->main_segments)
    369 
    370 static inline bool IS_VALID_NID(struct f2fs_sb_info *sbi, u32 nid)
    371 {
    372 	return (nid < (NAT_ENTRY_PER_BLOCK *
    373 			le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_nat)
    374 			<< (sbi->log_blocks_per_seg - 1)));
    375 }
    376 
    377 static inline bool IS_VALID_BLK_ADDR(struct f2fs_sb_info *sbi, u32 addr)
    378 {
    379 	if (addr == NULL_ADDR || addr == NEW_ADDR)
    380 		return 1;
    381 
    382 	if (addr >= le64_to_cpu(F2FS_RAW_SUPER(sbi)->block_count) ||
    383 				addr < SM_I(sbi)->main_blkaddr) {
    384 		DBG(1, "block addr [0x%x]\n", addr);
    385 		return 0;
    386 	}
    387 	/* next block offset will be checked at the end of fsck. */
    388 	return 1;
    389 }
    390 
    391 static inline int IS_CUR_SEGNO(struct f2fs_sb_info *sbi, u32 segno)
    392 {
    393 	int i;
    394 
    395 	for (i = 0; i < NO_CHECK_TYPE; i++) {
    396 		struct curseg_info *curseg = CURSEG_I(sbi, i);
    397 
    398 		if (segno == curseg->segno)
    399 			return 1;
    400 	}
    401 	return 0;
    402 }
    403 
    404 static inline u64 BLKOFF_FROM_MAIN(struct f2fs_sb_info *sbi, u64 blk_addr)
    405 {
    406 	ASSERT(blk_addr >= SM_I(sbi)->main_blkaddr);
    407 	return blk_addr - SM_I(sbi)->main_blkaddr;
    408 }
    409 
    410 static inline u32 GET_SEGNO(struct f2fs_sb_info *sbi, u64 blk_addr)
    411 {
    412 	return (u32)(BLKOFF_FROM_MAIN(sbi, blk_addr)
    413 			>> sbi->log_blocks_per_seg);
    414 }
    415 
    416 static inline u32 OFFSET_IN_SEG(struct f2fs_sb_info *sbi, u64 blk_addr)
    417 {
    418 	return (u32)(BLKOFF_FROM_MAIN(sbi, blk_addr)
    419 			% (1 << sbi->log_blocks_per_seg));
    420 }
    421 
    422 static inline void node_info_from_raw_nat(struct node_info *ni,
    423 		struct f2fs_nat_entry *raw_nat)
    424 {
    425 	ni->ino = le32_to_cpu(raw_nat->ino);
    426 	ni->blk_addr = le32_to_cpu(raw_nat->block_addr);
    427 	ni->version = raw_nat->version;
    428 }
    429 
    430 static inline void set_summary(struct f2fs_summary *sum, nid_t nid,
    431 			unsigned int ofs_in_node, unsigned char version)
    432 {
    433 	sum->nid = cpu_to_le32(nid);
    434 	sum->ofs_in_node = cpu_to_le16(ofs_in_node);
    435 	sum->version = version;
    436 }
    437 
    438 #define S_SHIFT 12
    439 static unsigned char f2fs_type_by_mode[S_IFMT >> S_SHIFT] = {
    440 	[S_IFREG >> S_SHIFT]    = F2FS_FT_REG_FILE,
    441 	[S_IFDIR >> S_SHIFT]    = F2FS_FT_DIR,
    442 	[S_IFCHR >> S_SHIFT]    = F2FS_FT_CHRDEV,
    443 	[S_IFBLK >> S_SHIFT]    = F2FS_FT_BLKDEV,
    444 	[S_IFIFO >> S_SHIFT]    = F2FS_FT_FIFO,
    445 	[S_IFSOCK >> S_SHIFT]   = F2FS_FT_SOCK,
    446 	[S_IFLNK >> S_SHIFT]    = F2FS_FT_SYMLINK,
    447 };
    448 
    449 static inline int map_de_type(umode_t mode)
    450 {
    451        return f2fs_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
    452 }
    453 
    454 static inline void *inline_xattr_addr(struct f2fs_inode *inode)
    455 {
    456 	return (void *)&(inode->i_addr[DEF_ADDRS_PER_INODE -
    457 				get_inline_xattr_addrs(inode)]);
    458 }
    459 
    460 static inline int inline_xattr_size(struct f2fs_inode *inode)
    461 {
    462 	return get_inline_xattr_addrs(inode) * sizeof(__le32);
    463 }
    464 
    465 extern int lookup_nat_in_journal(struct f2fs_sb_info *sbi, u32 nid, struct f2fs_nat_entry *ne);
    466 #define IS_SUM_NODE_SEG(footer)		(footer.entry_type == SUM_TYPE_NODE)
    467 #define IS_SUM_DATA_SEG(footer)		(footer.entry_type == SUM_TYPE_DATA)
    468 
    469 static inline unsigned int dir_buckets(unsigned int level, int dir_level)
    470 {
    471 	if (level + dir_level < MAX_DIR_HASH_DEPTH / 2)
    472 		return 1 << (level + dir_level);
    473 	else
    474 		return MAX_DIR_BUCKETS;
    475 }
    476 
    477 static inline unsigned int bucket_blocks(unsigned int level)
    478 {
    479 	if (level < MAX_DIR_HASH_DEPTH / 2)
    480 		return 2;
    481 	else
    482 		return 4;
    483 }
    484 
    485 static inline unsigned long dir_block_index(unsigned int level,
    486 				int dir_level, unsigned int idx)
    487 {
    488 	unsigned long i;
    489 	unsigned long bidx = 0;
    490 
    491 	for (i = 0; i < level; i++)
    492 		bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
    493 	bidx += idx * bucket_blocks(level);
    494 	return bidx;
    495 }
    496 
    497 static inline int is_dot_dotdot(const unsigned char *name, const int len)
    498 {
    499 	if (len == 1 && name[0] == '.')
    500 		return 1;
    501 	if (len == 2 && name[0] == '.' && name[1] == '.')
    502 		return 1;
    503 	return 0;
    504 }
    505 
    506 #endif /* _F2FS_H_ */
    507