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