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 long long mtime; /* modification time of the segment */ 76 }; 77 78 struct sec_entry { 79 unsigned int valid_blocks; /* # of valid blocks in a section */ 80 }; 81 82 struct sit_info { 83 84 block_t sit_base_addr; /* start block address of SIT area */ 85 block_t sit_blocks; /* # of blocks used by SIT area */ 86 block_t written_valid_blocks; /* # of valid blocks in main area */ 87 char *sit_bitmap; /* SIT bitmap pointer */ 88 unsigned int bitmap_size; /* SIT bitmap size */ 89 90 unsigned long *dirty_sentries_bitmap; /* bitmap for dirty sentries */ 91 unsigned int dirty_sentries; /* # of dirty sentries */ 92 unsigned int sents_per_block; /* # of SIT entries per block */ 93 struct seg_entry *sentries; /* SIT segment-level cache */ 94 struct sec_entry *sec_entries; /* SIT section-level cache */ 95 96 unsigned long long elapsed_time; /* elapsed time after mount */ 97 unsigned long long mounted_time; /* mount time */ 98 unsigned long long min_mtime; /* min. modification time */ 99 unsigned long long max_mtime; /* max. modification time */ 100 }; 101 102 struct curseg_info { 103 struct f2fs_summary_block *sum_blk; /* cached summary block */ 104 unsigned char alloc_type; /* current allocation type */ 105 unsigned int segno; /* current segment number */ 106 unsigned short next_blkoff; /* next block offset to write */ 107 unsigned int zone; /* current zone number */ 108 unsigned int next_segno; /* preallocated segment */ 109 }; 110 111 struct f2fs_sm_info { 112 struct sit_info *sit_info; 113 struct curseg_info *curseg_array; 114 115 block_t seg0_blkaddr; 116 block_t main_blkaddr; 117 block_t ssa_blkaddr; 118 119 unsigned int segment_count; 120 unsigned int main_segments; 121 unsigned int reserved_segments; 122 unsigned int ovp_segments; 123 }; 124 125 struct f2fs_sb_info { 126 struct f2fs_fsck *fsck; 127 128 struct f2fs_super_block *raw_super; 129 struct f2fs_nm_info *nm_info; 130 struct f2fs_sm_info *sm_info; 131 struct f2fs_checkpoint *ckpt; 132 int cur_cp; 133 134 struct list_head orphan_inode_list; 135 unsigned int n_orphans; 136 137 /* basic file system units */ 138 unsigned int log_sectors_per_block; /* log2 sectors per block */ 139 unsigned int log_blocksize; /* log2 block size */ 140 unsigned int blocksize; /* block size */ 141 unsigned int root_ino_num; /* root inode number*/ 142 unsigned int node_ino_num; /* node inode number*/ 143 unsigned int meta_ino_num; /* meta inode number*/ 144 unsigned int log_blocks_per_seg; /* log2 blocks per segment */ 145 unsigned int blocks_per_seg; /* blocks per segment */ 146 unsigned int segs_per_sec; /* segments per section */ 147 unsigned int secs_per_zone; /* sections per zone */ 148 unsigned int total_sections; /* total section count */ 149 unsigned int total_node_count; /* total node block count */ 150 unsigned int total_valid_node_count; /* valid node block count */ 151 unsigned int total_valid_inode_count; /* valid inode count */ 152 int active_logs; /* # of active logs */ 153 154 block_t user_block_count; /* # of user blocks */ 155 block_t total_valid_block_count; /* # of valid blocks */ 156 block_t alloc_valid_block_count; /* # of allocated blocks */ 157 block_t last_valid_block_count; /* for recovery */ 158 u32 s_next_generation; /* for NFS support */ 159 160 unsigned int cur_victim_sec; /* current victim section num */ 161 162 }; 163 164 static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi) 165 { 166 return (struct f2fs_super_block *)(sbi->raw_super); 167 } 168 169 static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi) 170 { 171 return (struct f2fs_checkpoint *)(sbi->ckpt); 172 } 173 174 static inline struct f2fs_fsck *F2FS_FSCK(struct f2fs_sb_info *sbi) 175 { 176 return (struct f2fs_fsck *)(sbi->fsck); 177 } 178 179 static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi) 180 { 181 return (struct f2fs_nm_info *)(sbi->nm_info); 182 } 183 184 static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi) 185 { 186 return (struct f2fs_sm_info *)(sbi->sm_info); 187 } 188 189 static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi) 190 { 191 return (struct sit_info *)(SM_I(sbi)->sit_info); 192 } 193 194 static inline void *inline_data_addr(struct f2fs_node *node_blk) 195 { 196 return (void *)&(node_blk->i.i_addr[1]); 197 } 198 199 static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag) 200 { 201 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 202 203 /* return NAT or SIT bitmap */ 204 if (flag == NAT_BITMAP) 205 return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize); 206 else if (flag == SIT_BITMAP) 207 return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize); 208 209 return 0; 210 } 211 212 static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag) 213 { 214 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 215 int offset; 216 if (le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload) > 0) { 217 if (flag == NAT_BITMAP) 218 return &ckpt->sit_nat_version_bitmap; 219 else 220 return ((char *)ckpt + F2FS_BLKSIZE); 221 } else { 222 offset = (flag == NAT_BITMAP) ? 223 le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0; 224 return &ckpt->sit_nat_version_bitmap + offset; 225 } 226 } 227 228 static inline bool is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f) 229 { 230 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags); 231 return ckpt_flags & f; 232 } 233 234 static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi) 235 { 236 block_t start_addr; 237 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 238 unsigned long long ckpt_version = le64_to_cpu(ckpt->checkpoint_ver); 239 240 start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr); 241 242 /* 243 * odd numbered checkpoint should at cp segment 0 244 * and even segent must be at cp segment 1 245 */ 246 if (!(ckpt_version & 1)) 247 start_addr += sbi->blocks_per_seg; 248 249 return start_addr; 250 } 251 252 static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi) 253 { 254 return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum); 255 } 256 257 #define GET_ZONENO_FROM_SEGNO(sbi, segno) \ 258 ((segno / sbi->segs_per_sec) / sbi->secs_per_zone) 259 260 #define IS_DATASEG(t) \ 261 ((t == CURSEG_HOT_DATA) || (t == CURSEG_COLD_DATA) || \ 262 (t == CURSEG_WARM_DATA)) 263 264 #define IS_NODESEG(t) \ 265 ((t == CURSEG_HOT_NODE) || (t == CURSEG_COLD_NODE) || \ 266 (t == CURSEG_WARM_NODE)) 267 268 #define GET_SUM_BLKADDR(sbi, segno) \ 269 ((sbi->sm_info->ssa_blkaddr) + segno) 270 271 #define GET_SEGOFF_FROM_SEG0(sbi, blk_addr) \ 272 ((blk_addr) - SM_I(sbi)->seg0_blkaddr) 273 274 #define GET_SEGNO_FROM_SEG0(sbi, blk_addr) \ 275 (GET_SEGOFF_FROM_SEG0(sbi, blk_addr) >> sbi->log_blocks_per_seg) 276 277 #define GET_BLKOFF_FROM_SEG0(sbi, blk_addr) \ 278 (GET_SEGOFF_FROM_SEG0(sbi, blk_addr) & (sbi->blocks_per_seg - 1)) 279 280 #define FREE_I_START_SEGNO(sbi) \ 281 GET_SEGNO_FROM_SEG0(sbi, SM_I(sbi)->main_blkaddr) 282 #define GET_R2L_SEGNO(sbi, segno) (segno + FREE_I_START_SEGNO(sbi)) 283 284 #define START_BLOCK(sbi, segno) (SM_I(sbi)->main_blkaddr + \ 285 (segno << sbi->log_blocks_per_seg)) 286 287 static inline struct curseg_info *CURSEG_I(struct f2fs_sb_info *sbi, int type) 288 { 289 return (struct curseg_info *)(SM_I(sbi)->curseg_array + type); 290 } 291 292 static inline block_t start_sum_block(struct f2fs_sb_info *sbi) 293 { 294 return __start_cp_addr(sbi) + le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum); 295 } 296 297 static inline block_t sum_blk_addr(struct f2fs_sb_info *sbi, int base, int type) 298 { 299 return __start_cp_addr(sbi) + le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_total_block_count) 300 - (base + 1) + type; 301 } 302 303 304 #define nats_in_cursum(sum) (le16_to_cpu(sum->n_nats)) 305 #define sits_in_cursum(sum) (le16_to_cpu(sum->n_sits)) 306 307 #define nat_in_journal(sum, i) (sum->nat_j.entries[i].ne) 308 #define nid_in_journal(sum, i) (sum->nat_j.entries[i].nid) 309 #define sit_in_journal(sum, i) (sum->sit_j.entries[i].se) 310 #define segno_in_journal(sum, i) (sum->sit_j.entries[i].segno) 311 312 #define SIT_ENTRY_OFFSET(sit_i, segno) \ 313 (segno % sit_i->sents_per_block) 314 #define SIT_BLOCK_OFFSET(sit_i, segno) \ 315 (segno / SIT_ENTRY_PER_BLOCK) 316 #define TOTAL_SEGS(sbi) (SM_I(sbi)->main_segments) 317 318 static inline bool IS_VALID_NID(struct f2fs_sb_info *sbi, u32 nid) 319 { 320 return (nid <= (NAT_ENTRY_PER_BLOCK * 321 F2FS_RAW_SUPER(sbi)->segment_count_nat 322 << (sbi->log_blocks_per_seg - 1))); 323 } 324 325 static inline bool IS_VALID_BLK_ADDR(struct f2fs_sb_info *sbi, u32 addr) 326 { 327 int i; 328 329 if (addr >= F2FS_RAW_SUPER(sbi)->block_count || 330 addr < SM_I(sbi)->main_blkaddr) { 331 ASSERT_MSG("block addr [0x%x]\n", addr); 332 return 0; 333 } 334 335 for (i = 0; i < NO_CHECK_TYPE; i++) { 336 struct curseg_info *curseg = CURSEG_I(sbi, i); 337 338 if (START_BLOCK(sbi, curseg->segno) + 339 curseg->next_blkoff == addr) 340 return 0; 341 } 342 return 1; 343 } 344 345 static inline u64 BLKOFF_FROM_MAIN(struct f2fs_sb_info *sbi, u64 blk_addr) 346 { 347 ASSERT(blk_addr >= SM_I(sbi)->main_blkaddr); 348 return blk_addr - SM_I(sbi)->main_blkaddr; 349 } 350 351 static inline u32 GET_SEGNO(struct f2fs_sb_info *sbi, u64 blk_addr) 352 { 353 return (u32)(BLKOFF_FROM_MAIN(sbi, blk_addr) 354 >> sbi->log_blocks_per_seg); 355 } 356 357 static inline u32 OFFSET_IN_SEG(struct f2fs_sb_info *sbi, u64 blk_addr) 358 { 359 return (u32)(BLKOFF_FROM_MAIN(sbi, blk_addr) 360 % (1 << sbi->log_blocks_per_seg)); 361 } 362 363 static inline void node_info_from_raw_nat(struct node_info *ni, 364 struct f2fs_nat_entry *raw_nat) 365 { 366 ni->ino = le32_to_cpu(raw_nat->ino); 367 ni->blk_addr = le32_to_cpu(raw_nat->block_addr); 368 ni->version = raw_nat->version; 369 } 370 371 extern int lookup_nat_in_journal(struct f2fs_sb_info *sbi, u32 nid, struct f2fs_nat_entry *ne); 372 #define IS_SUM_NODE_SEG(footer) (footer.entry_type == SUM_TYPE_NODE) 373 374 #endif /* _F2FS_H_ */ 375