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 <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 
     37 enum {
     38 	NAT_BITMAP,
     39 	SIT_BITMAP
     40 };
     41 
     42 struct node_info {
     43 	nid_t nid;
     44 	nid_t ino;
     45 	u32 blk_addr;
     46 	unsigned char version;
     47 };
     48 
     49 struct 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 
     62 struct 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 
     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_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 
    160 static 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 
    165 static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
    166 {
    167 	return (struct f2fs_checkpoint *)(sbi->ckpt);
    168 }
    169 
    170 static inline struct f2fs_fsck *F2FS_FSCK(struct f2fs_sb_info *sbi)
    171 {
    172 	return (struct f2fs_fsck *)(sbi->fsck);
    173 }
    174 
    175 static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
    176 {
    177 	return (struct f2fs_nm_info *)(sbi->nm_info);
    178 }
    179 
    180 static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
    181 {
    182 	return (struct f2fs_sm_info *)(sbi->sm_info);
    183 }
    184 
    185 static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
    186 {
    187 	return (struct sit_info *)(SM_I(sbi)->sit_info);
    188 }
    189 
    190 static 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 
    203 static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
    204 {
    205 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
    206 	int offset;
    207 	if (le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload) > 0) {
    208 		if (flag == NAT_BITMAP)
    209 			return &ckpt->sit_nat_version_bitmap;
    210 		else
    211 			return ((char *)ckpt + F2FS_BLKSIZE);
    212 	} else {
    213 		offset = (flag == NAT_BITMAP) ?
    214 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
    215 		return &ckpt->sit_nat_version_bitmap + offset;
    216 	}
    217 }
    218 
    219 static inline bool is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
    220 {
    221 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
    222 	return ckpt_flags & f;
    223 }
    224 
    225 static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
    226 {
    227 	block_t start_addr;
    228 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
    229 	unsigned long long ckpt_version = le64_to_cpu(ckpt->checkpoint_ver);
    230 
    231 	start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
    232 
    233 	/*
    234 	 * odd numbered checkpoint should at cp segment 0
    235 	 * and even segent must be at cp segment 1
    236 	 */
    237 	if (!(ckpt_version & 1))
    238 		start_addr += sbi->blocks_per_seg;
    239 
    240 	return start_addr;
    241 }
    242 
    243 static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
    244 {
    245 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
    246 }
    247 
    248 #define GET_ZONENO_FROM_SEGNO(sbi, segno)                               \
    249 	((segno / sbi->segs_per_sec) / sbi->secs_per_zone)
    250 
    251 #define IS_DATASEG(t)                                                   \
    252 	((t == CURSEG_HOT_DATA) || (t == CURSEG_COLD_DATA) ||           \
    253 	 (t == CURSEG_WARM_DATA))
    254 
    255 #define IS_NODESEG(t)                                                   \
    256 	((t == CURSEG_HOT_NODE) || (t == CURSEG_COLD_NODE) ||           \
    257 	 (t == CURSEG_WARM_NODE))
    258 
    259 #define GET_SUM_BLKADDR(sbi, segno)					\
    260 	((sbi->sm_info->ssa_blkaddr) + segno)
    261 
    262 #define GET_SEGOFF_FROM_SEG0(sbi, blk_addr)				\
    263 	((blk_addr) - SM_I(sbi)->seg0_blkaddr)
    264 
    265 #define GET_SEGNO_FROM_SEG0(sbi, blk_addr)				\
    266 	(GET_SEGOFF_FROM_SEG0(sbi, blk_addr) >> sbi->log_blocks_per_seg)
    267 
    268 #define FREE_I_START_SEGNO(sbi)		GET_SEGNO_FROM_SEG0(sbi, SM_I(sbi)->main_blkaddr)
    269 #define GET_R2L_SEGNO(sbi, segno)	(segno + FREE_I_START_SEGNO(sbi))
    270 
    271 #define START_BLOCK(sbi, segno)	(SM_I(sbi)->main_blkaddr + (segno << sbi->log_blocks_per_seg))
    272 
    273 static inline struct curseg_info *CURSEG_I(struct f2fs_sb_info *sbi, int type)
    274 {
    275 	return (struct curseg_info *)(SM_I(sbi)->curseg_array + type);
    276 }
    277 
    278 static inline block_t start_sum_block(struct f2fs_sb_info *sbi)
    279 {
    280 	return __start_cp_addr(sbi) + le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
    281 }
    282 
    283 static inline block_t sum_blk_addr(struct f2fs_sb_info *sbi, int base, int type)
    284 {
    285 	return __start_cp_addr(sbi) + le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_total_block_count)
    286 		- (base + 1) + type;
    287 }
    288 
    289 
    290 #define nats_in_cursum(sum)             (le16_to_cpu(sum->n_nats))
    291 #define sits_in_cursum(sum)             (le16_to_cpu(sum->n_sits))
    292 
    293 #define nat_in_journal(sum, i)          (sum->nat_j.entries[i].ne)
    294 #define nid_in_journal(sum, i)          (sum->nat_j.entries[i].nid)
    295 #define sit_in_journal(sum, i)          (sum->sit_j.entries[i].se)
    296 #define segno_in_journal(sum, i)        (sum->sit_j.entries[i].segno)
    297 
    298 #define SIT_ENTRY_OFFSET(sit_i, segno)                                  \
    299 	(segno % sit_i->sents_per_block)
    300 #define SIT_BLOCK_OFFSET(sit_i, segno)                                  \
    301 	(segno / SIT_ENTRY_PER_BLOCK)
    302 #define TOTAL_SEGS(sbi) (SM_I(sbi)->main_segments)
    303 
    304 #define IS_VALID_NID(sbi, nid) 			\
    305 	do {						\
    306 		ASSERT(nid <= (NAT_ENTRY_PER_BLOCK *	\
    307 					F2FS_RAW_SUPER(sbi)->segment_count_nat	\
    308 					<< (sbi->log_blocks_per_seg - 1)));	\
    309 	} while (0);
    310 
    311 #define IS_VALID_BLK_ADDR(sbi, addr)				\
    312 	do {							\
    313 		if (addr >= F2FS_RAW_SUPER(sbi)->block_count ||	 	\
    314 				addr < SM_I(sbi)->main_blkaddr)	\
    315 		{						\
    316 			DBG(0, "block addr [0x%x]\n", addr);	\
    317 			ASSERT(addr <  F2FS_RAW_SUPER(sbi)->block_count);	\
    318 			ASSERT(addr >= SM_I(sbi)->main_blkaddr);	\
    319 		}						\
    320 	} while (0);
    321 
    322 static inline u64 BLKOFF_FROM_MAIN(struct f2fs_sb_info *sbi, u64 blk_addr)
    323 {
    324 	ASSERT(blk_addr >= SM_I(sbi)->main_blkaddr);
    325 	return blk_addr - SM_I(sbi)->main_blkaddr;
    326 }
    327 
    328 static inline u32 GET_SEGNO(struct f2fs_sb_info *sbi, u64 blk_addr)
    329 {
    330 	return (u32)(BLKOFF_FROM_MAIN(sbi, blk_addr)
    331 			>> sbi->log_blocks_per_seg);
    332 }
    333 
    334 static inline u32 OFFSET_IN_SEG(struct f2fs_sb_info *sbi, u64 blk_addr)
    335 {
    336 	return (u32)(BLKOFF_FROM_MAIN(sbi, blk_addr)
    337 			% (1 << sbi->log_blocks_per_seg));
    338 }
    339 
    340 static inline void node_info_from_raw_nat(struct node_info *ni,
    341 		struct f2fs_nat_entry *raw_nat)
    342 {
    343 	ni->ino = le32_to_cpu(raw_nat->ino);
    344 	ni->blk_addr = le32_to_cpu(raw_nat->block_addr);
    345 	ni->version = raw_nat->version;
    346 }
    347 
    348 extern int lookup_nat_in_journal(struct f2fs_sb_info *sbi, u32 nid, struct f2fs_nat_entry *ne);
    349 #define IS_SUM_NODE_SEG(footer)		(footer.entry_type == SUM_TYPE_NODE)
    350 
    351 #endif /* _F2FS_H_ */
    352