segment.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /*
  2. * fs/f2fs/segment.h
  3. *
  4. * Copyright (c) 2012 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. /* constant macro */
  12. #define NULL_SEGNO ((unsigned int)(~0))
  13. /* V: Logical segment # in volume, R: Relative segment # in main area */
  14. #define GET_L2R_SEGNO(free_i, segno) (segno - free_i->start_segno)
  15. #define GET_R2L_SEGNO(free_i, segno) (segno + free_i->start_segno)
  16. #define IS_DATASEG(t) \
  17. ((t == CURSEG_HOT_DATA) || (t == CURSEG_COLD_DATA) || \
  18. (t == CURSEG_WARM_DATA))
  19. #define IS_NODESEG(t) \
  20. ((t == CURSEG_HOT_NODE) || (t == CURSEG_COLD_NODE) || \
  21. (t == CURSEG_WARM_NODE))
  22. #define IS_CURSEG(sbi, segno) \
  23. ((segno == CURSEG_I(sbi, CURSEG_HOT_DATA)->segno) || \
  24. (segno == CURSEG_I(sbi, CURSEG_WARM_DATA)->segno) || \
  25. (segno == CURSEG_I(sbi, CURSEG_COLD_DATA)->segno) || \
  26. (segno == CURSEG_I(sbi, CURSEG_HOT_NODE)->segno) || \
  27. (segno == CURSEG_I(sbi, CURSEG_WARM_NODE)->segno) || \
  28. (segno == CURSEG_I(sbi, CURSEG_COLD_NODE)->segno))
  29. #define IS_CURSEC(sbi, secno) \
  30. ((secno == CURSEG_I(sbi, CURSEG_HOT_DATA)->segno / \
  31. sbi->segs_per_sec) || \
  32. (secno == CURSEG_I(sbi, CURSEG_WARM_DATA)->segno / \
  33. sbi->segs_per_sec) || \
  34. (secno == CURSEG_I(sbi, CURSEG_COLD_DATA)->segno / \
  35. sbi->segs_per_sec) || \
  36. (secno == CURSEG_I(sbi, CURSEG_HOT_NODE)->segno / \
  37. sbi->segs_per_sec) || \
  38. (secno == CURSEG_I(sbi, CURSEG_WARM_NODE)->segno / \
  39. sbi->segs_per_sec) || \
  40. (secno == CURSEG_I(sbi, CURSEG_COLD_NODE)->segno / \
  41. sbi->segs_per_sec)) \
  42. #define START_BLOCK(sbi, segno) \
  43. (SM_I(sbi)->seg0_blkaddr + \
  44. (GET_R2L_SEGNO(FREE_I(sbi), segno) << sbi->log_blocks_per_seg))
  45. #define NEXT_FREE_BLKADDR(sbi, curseg) \
  46. (START_BLOCK(sbi, curseg->segno) + curseg->next_blkoff)
  47. #define MAIN_BASE_BLOCK(sbi) (SM_I(sbi)->main_blkaddr)
  48. #define GET_SEGOFF_FROM_SEG0(sbi, blk_addr) \
  49. ((blk_addr) - SM_I(sbi)->seg0_blkaddr)
  50. #define GET_SEGNO_FROM_SEG0(sbi, blk_addr) \
  51. (GET_SEGOFF_FROM_SEG0(sbi, blk_addr) >> sbi->log_blocks_per_seg)
  52. #define GET_SEGNO(sbi, blk_addr) \
  53. (((blk_addr == NULL_ADDR) || (blk_addr == NEW_ADDR)) ? \
  54. NULL_SEGNO : GET_L2R_SEGNO(FREE_I(sbi), \
  55. GET_SEGNO_FROM_SEG0(sbi, blk_addr)))
  56. #define GET_SECNO(sbi, segno) \
  57. ((segno) / sbi->segs_per_sec)
  58. #define GET_ZONENO_FROM_SEGNO(sbi, segno) \
  59. ((segno / sbi->segs_per_sec) / sbi->secs_per_zone)
  60. #define GET_SUM_BLOCK(sbi, segno) \
  61. ((sbi->sm_info->ssa_blkaddr) + segno)
  62. #define GET_SUM_TYPE(footer) ((footer)->entry_type)
  63. #define SET_SUM_TYPE(footer, type) ((footer)->entry_type = type)
  64. #define SIT_ENTRY_OFFSET(sit_i, segno) \
  65. (segno % sit_i->sents_per_block)
  66. #define SIT_BLOCK_OFFSET(sit_i, segno) \
  67. (segno / SIT_ENTRY_PER_BLOCK)
  68. #define START_SEGNO(sit_i, segno) \
  69. (SIT_BLOCK_OFFSET(sit_i, segno) * SIT_ENTRY_PER_BLOCK)
  70. #define f2fs_bitmap_size(nr) \
  71. (BITS_TO_LONGS(nr) * sizeof(unsigned long))
  72. #define TOTAL_SEGS(sbi) (SM_I(sbi)->main_segments)
  73. #define SECTOR_FROM_BLOCK(sbi, blk_addr) \
  74. (blk_addr << ((sbi)->log_blocksize - F2FS_LOG_SECTOR_SIZE))
  75. /* during checkpoint, bio_private is used to synchronize the last bio */
  76. struct bio_private {
  77. struct f2fs_sb_info *sbi;
  78. bool is_sync;
  79. void *wait;
  80. };
  81. /*
  82. * indicate a block allocation direction: RIGHT and LEFT.
  83. * RIGHT means allocating new sections towards the end of volume.
  84. * LEFT means the opposite direction.
  85. */
  86. enum {
  87. ALLOC_RIGHT = 0,
  88. ALLOC_LEFT
  89. };
  90. /*
  91. * In the victim_sel_policy->alloc_mode, there are two block allocation modes.
  92. * LFS writes data sequentially with cleaning operations.
  93. * SSR (Slack Space Recycle) reuses obsolete space without cleaning operations.
  94. */
  95. enum {
  96. LFS = 0,
  97. SSR
  98. };
  99. /*
  100. * In the victim_sel_policy->gc_mode, there are two gc, aka cleaning, modes.
  101. * GC_CB is based on cost-benefit algorithm.
  102. * GC_GREEDY is based on greedy algorithm.
  103. */
  104. enum {
  105. GC_CB = 0,
  106. GC_GREEDY
  107. };
  108. /*
  109. * BG_GC means the background cleaning job.
  110. * FG_GC means the on-demand cleaning job.
  111. */
  112. enum {
  113. BG_GC = 0,
  114. FG_GC
  115. };
  116. /* for a function parameter to select a victim segment */
  117. struct victim_sel_policy {
  118. int alloc_mode; /* LFS or SSR */
  119. int gc_mode; /* GC_CB or GC_GREEDY */
  120. unsigned long *dirty_segmap; /* dirty segment bitmap */
  121. unsigned int offset; /* last scanned bitmap offset */
  122. unsigned int ofs_unit; /* bitmap search unit */
  123. unsigned int min_cost; /* minimum cost */
  124. unsigned int min_segno; /* segment # having min. cost */
  125. };
  126. struct seg_entry {
  127. unsigned short valid_blocks; /* # of valid blocks */
  128. unsigned char *cur_valid_map; /* validity bitmap of blocks */
  129. /*
  130. * # of valid blocks and the validity bitmap stored in the the last
  131. * checkpoint pack. This information is used by the SSR mode.
  132. */
  133. unsigned short ckpt_valid_blocks;
  134. unsigned char *ckpt_valid_map;
  135. unsigned char type; /* segment type like CURSEG_XXX_TYPE */
  136. unsigned long long mtime; /* modification time of the segment */
  137. };
  138. struct sec_entry {
  139. unsigned int valid_blocks; /* # of valid blocks in a section */
  140. };
  141. struct segment_allocation {
  142. void (*allocate_segment)(struct f2fs_sb_info *, int, bool);
  143. };
  144. struct sit_info {
  145. const struct segment_allocation *s_ops;
  146. block_t sit_base_addr; /* start block address of SIT area */
  147. block_t sit_blocks; /* # of blocks used by SIT area */
  148. block_t written_valid_blocks; /* # of valid blocks in main area */
  149. char *sit_bitmap; /* SIT bitmap pointer */
  150. unsigned int bitmap_size; /* SIT bitmap size */
  151. unsigned long *dirty_sentries_bitmap; /* bitmap for dirty sentries */
  152. unsigned int dirty_sentries; /* # of dirty sentries */
  153. unsigned int sents_per_block; /* # of SIT entries per block */
  154. struct mutex sentry_lock; /* to protect SIT cache */
  155. struct seg_entry *sentries; /* SIT segment-level cache */
  156. struct sec_entry *sec_entries; /* SIT section-level cache */
  157. /* for cost-benefit algorithm in cleaning procedure */
  158. unsigned long long elapsed_time; /* elapsed time after mount */
  159. unsigned long long mounted_time; /* mount time */
  160. unsigned long long min_mtime; /* min. modification time */
  161. unsigned long long max_mtime; /* max. modification time */
  162. };
  163. struct free_segmap_info {
  164. unsigned int start_segno; /* start segment number logically */
  165. unsigned int free_segments; /* # of free segments */
  166. unsigned int free_sections; /* # of free sections */
  167. rwlock_t segmap_lock; /* free segmap lock */
  168. unsigned long *free_segmap; /* free segment bitmap */
  169. unsigned long *free_secmap; /* free section bitmap */
  170. };
  171. /* Notice: The order of dirty type is same with CURSEG_XXX in f2fs.h */
  172. enum dirty_type {
  173. DIRTY_HOT_DATA, /* dirty segments assigned as hot data logs */
  174. DIRTY_WARM_DATA, /* dirty segments assigned as warm data logs */
  175. DIRTY_COLD_DATA, /* dirty segments assigned as cold data logs */
  176. DIRTY_HOT_NODE, /* dirty segments assigned as hot node logs */
  177. DIRTY_WARM_NODE, /* dirty segments assigned as warm node logs */
  178. DIRTY_COLD_NODE, /* dirty segments assigned as cold node logs */
  179. DIRTY, /* to count # of dirty segments */
  180. PRE, /* to count # of entirely obsolete segments */
  181. NR_DIRTY_TYPE
  182. };
  183. struct dirty_seglist_info {
  184. const struct victim_selection *v_ops; /* victim selction operation */
  185. unsigned long *dirty_segmap[NR_DIRTY_TYPE];
  186. struct mutex seglist_lock; /* lock for segment bitmaps */
  187. int nr_dirty[NR_DIRTY_TYPE]; /* # of dirty segments */
  188. unsigned long *victim_segmap[2]; /* BG_GC, FG_GC */
  189. };
  190. /* victim selection function for cleaning and SSR */
  191. struct victim_selection {
  192. int (*get_victim)(struct f2fs_sb_info *, unsigned int *,
  193. int, int, char);
  194. };
  195. /* for active log information */
  196. struct curseg_info {
  197. struct mutex curseg_mutex; /* lock for consistency */
  198. struct f2fs_summary_block *sum_blk; /* cached summary block */
  199. unsigned char alloc_type; /* current allocation type */
  200. unsigned int segno; /* current segment number */
  201. unsigned short next_blkoff; /* next block offset to write */
  202. unsigned int zone; /* current zone number */
  203. unsigned int next_segno; /* preallocated segment */
  204. };
  205. /*
  206. * inline functions
  207. */
  208. static inline struct curseg_info *CURSEG_I(struct f2fs_sb_info *sbi, int type)
  209. {
  210. return (struct curseg_info *)(SM_I(sbi)->curseg_array + type);
  211. }
  212. static inline struct seg_entry *get_seg_entry(struct f2fs_sb_info *sbi,
  213. unsigned int segno)
  214. {
  215. struct sit_info *sit_i = SIT_I(sbi);
  216. return &sit_i->sentries[segno];
  217. }
  218. static inline struct sec_entry *get_sec_entry(struct f2fs_sb_info *sbi,
  219. unsigned int segno)
  220. {
  221. struct sit_info *sit_i = SIT_I(sbi);
  222. return &sit_i->sec_entries[GET_SECNO(sbi, segno)];
  223. }
  224. static inline unsigned int get_valid_blocks(struct f2fs_sb_info *sbi,
  225. unsigned int segno, int section)
  226. {
  227. /*
  228. * In order to get # of valid blocks in a section instantly from many
  229. * segments, f2fs manages two counting structures separately.
  230. */
  231. if (section > 1)
  232. return get_sec_entry(sbi, segno)->valid_blocks;
  233. else
  234. return get_seg_entry(sbi, segno)->valid_blocks;
  235. }
  236. static inline void seg_info_from_raw_sit(struct seg_entry *se,
  237. struct f2fs_sit_entry *rs)
  238. {
  239. se->valid_blocks = GET_SIT_VBLOCKS(rs);
  240. se->ckpt_valid_blocks = GET_SIT_VBLOCKS(rs);
  241. memcpy(se->cur_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
  242. memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
  243. se->type = GET_SIT_TYPE(rs);
  244. se->mtime = le64_to_cpu(rs->mtime);
  245. }
  246. static inline void seg_info_to_raw_sit(struct seg_entry *se,
  247. struct f2fs_sit_entry *rs)
  248. {
  249. unsigned short raw_vblocks = (se->type << SIT_VBLOCKS_SHIFT) |
  250. se->valid_blocks;
  251. rs->vblocks = cpu_to_le16(raw_vblocks);
  252. memcpy(rs->valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
  253. memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
  254. se->ckpt_valid_blocks = se->valid_blocks;
  255. rs->mtime = cpu_to_le64(se->mtime);
  256. }
  257. static inline unsigned int find_next_inuse(struct free_segmap_info *free_i,
  258. unsigned int max, unsigned int segno)
  259. {
  260. unsigned int ret;
  261. read_lock(&free_i->segmap_lock);
  262. ret = find_next_bit(free_i->free_segmap, max, segno);
  263. read_unlock(&free_i->segmap_lock);
  264. return ret;
  265. }
  266. static inline void __set_free(struct f2fs_sb_info *sbi, unsigned int segno)
  267. {
  268. struct free_segmap_info *free_i = FREE_I(sbi);
  269. unsigned int secno = segno / sbi->segs_per_sec;
  270. unsigned int start_segno = secno * sbi->segs_per_sec;
  271. unsigned int next;
  272. write_lock(&free_i->segmap_lock);
  273. clear_bit(segno, free_i->free_segmap);
  274. free_i->free_segments++;
  275. next = find_next_bit(free_i->free_segmap, TOTAL_SEGS(sbi), start_segno);
  276. if (next >= start_segno + sbi->segs_per_sec) {
  277. clear_bit(secno, free_i->free_secmap);
  278. free_i->free_sections++;
  279. }
  280. write_unlock(&free_i->segmap_lock);
  281. }
  282. static inline void __set_inuse(struct f2fs_sb_info *sbi,
  283. unsigned int segno)
  284. {
  285. struct free_segmap_info *free_i = FREE_I(sbi);
  286. unsigned int secno = segno / sbi->segs_per_sec;
  287. set_bit(segno, free_i->free_segmap);
  288. free_i->free_segments--;
  289. if (!test_and_set_bit(secno, free_i->free_secmap))
  290. free_i->free_sections--;
  291. }
  292. static inline void __set_test_and_free(struct f2fs_sb_info *sbi,
  293. unsigned int segno)
  294. {
  295. struct free_segmap_info *free_i = FREE_I(sbi);
  296. unsigned int secno = segno / sbi->segs_per_sec;
  297. unsigned int start_segno = secno * sbi->segs_per_sec;
  298. unsigned int next;
  299. write_lock(&free_i->segmap_lock);
  300. if (test_and_clear_bit(segno, free_i->free_segmap)) {
  301. free_i->free_segments++;
  302. next = find_next_bit(free_i->free_segmap, TOTAL_SEGS(sbi),
  303. start_segno);
  304. if (next >= start_segno + sbi->segs_per_sec) {
  305. if (test_and_clear_bit(secno, free_i->free_secmap))
  306. free_i->free_sections++;
  307. }
  308. }
  309. write_unlock(&free_i->segmap_lock);
  310. }
  311. static inline void __set_test_and_inuse(struct f2fs_sb_info *sbi,
  312. unsigned int segno)
  313. {
  314. struct free_segmap_info *free_i = FREE_I(sbi);
  315. unsigned int secno = segno / sbi->segs_per_sec;
  316. write_lock(&free_i->segmap_lock);
  317. if (!test_and_set_bit(segno, free_i->free_segmap)) {
  318. free_i->free_segments--;
  319. if (!test_and_set_bit(secno, free_i->free_secmap))
  320. free_i->free_sections--;
  321. }
  322. write_unlock(&free_i->segmap_lock);
  323. }
  324. static inline void get_sit_bitmap(struct f2fs_sb_info *sbi,
  325. void *dst_addr)
  326. {
  327. struct sit_info *sit_i = SIT_I(sbi);
  328. memcpy(dst_addr, sit_i->sit_bitmap, sit_i->bitmap_size);
  329. }
  330. static inline block_t written_block_count(struct f2fs_sb_info *sbi)
  331. {
  332. struct sit_info *sit_i = SIT_I(sbi);
  333. block_t vblocks;
  334. mutex_lock(&sit_i->sentry_lock);
  335. vblocks = sit_i->written_valid_blocks;
  336. mutex_unlock(&sit_i->sentry_lock);
  337. return vblocks;
  338. }
  339. static inline unsigned int free_segments(struct f2fs_sb_info *sbi)
  340. {
  341. struct free_segmap_info *free_i = FREE_I(sbi);
  342. unsigned int free_segs;
  343. read_lock(&free_i->segmap_lock);
  344. free_segs = free_i->free_segments;
  345. read_unlock(&free_i->segmap_lock);
  346. return free_segs;
  347. }
  348. static inline int reserved_segments(struct f2fs_sb_info *sbi)
  349. {
  350. return SM_I(sbi)->reserved_segments;
  351. }
  352. static inline unsigned int free_sections(struct f2fs_sb_info *sbi)
  353. {
  354. struct free_segmap_info *free_i = FREE_I(sbi);
  355. unsigned int free_secs;
  356. read_lock(&free_i->segmap_lock);
  357. free_secs = free_i->free_sections;
  358. read_unlock(&free_i->segmap_lock);
  359. return free_secs;
  360. }
  361. static inline unsigned int prefree_segments(struct f2fs_sb_info *sbi)
  362. {
  363. return DIRTY_I(sbi)->nr_dirty[PRE];
  364. }
  365. static inline unsigned int dirty_segments(struct f2fs_sb_info *sbi)
  366. {
  367. return DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_DATA] +
  368. DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_DATA] +
  369. DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_DATA] +
  370. DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_NODE] +
  371. DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_NODE] +
  372. DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_NODE];
  373. }
  374. static inline int overprovision_segments(struct f2fs_sb_info *sbi)
  375. {
  376. return SM_I(sbi)->ovp_segments;
  377. }
  378. static inline int overprovision_sections(struct f2fs_sb_info *sbi)
  379. {
  380. return ((unsigned int) overprovision_segments(sbi)) / sbi->segs_per_sec;
  381. }
  382. static inline int reserved_sections(struct f2fs_sb_info *sbi)
  383. {
  384. return ((unsigned int) reserved_segments(sbi)) / sbi->segs_per_sec;
  385. }
  386. static inline bool need_SSR(struct f2fs_sb_info *sbi)
  387. {
  388. return (free_sections(sbi) < overprovision_sections(sbi));
  389. }
  390. static inline bool has_not_enough_free_secs(struct f2fs_sb_info *sbi, int freed)
  391. {
  392. int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
  393. int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
  394. if (sbi->por_doing)
  395. return false;
  396. return ((free_sections(sbi) + freed) <= (node_secs + 2 * dent_secs +
  397. reserved_sections(sbi)));
  398. }
  399. static inline int utilization(struct f2fs_sb_info *sbi)
  400. {
  401. return (long int)valid_user_blocks(sbi) * 100 /
  402. (long int)sbi->user_block_count;
  403. }
  404. /*
  405. * Sometimes f2fs may be better to drop out-of-place update policy.
  406. * So, if fs utilization is over MIN_IPU_UTIL, then f2fs tries to write
  407. * data in the original place likewise other traditional file systems.
  408. * But, currently set 100 in percentage, which means it is disabled.
  409. * See below need_inplace_update().
  410. */
  411. #define MIN_IPU_UTIL 100
  412. static inline bool need_inplace_update(struct inode *inode)
  413. {
  414. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  415. if (S_ISDIR(inode->i_mode))
  416. return false;
  417. if (need_SSR(sbi) && utilization(sbi) > MIN_IPU_UTIL)
  418. return true;
  419. return false;
  420. }
  421. static inline unsigned int curseg_segno(struct f2fs_sb_info *sbi,
  422. int type)
  423. {
  424. struct curseg_info *curseg = CURSEG_I(sbi, type);
  425. return curseg->segno;
  426. }
  427. static inline unsigned char curseg_alloc_type(struct f2fs_sb_info *sbi,
  428. int type)
  429. {
  430. struct curseg_info *curseg = CURSEG_I(sbi, type);
  431. return curseg->alloc_type;
  432. }
  433. static inline unsigned short curseg_blkoff(struct f2fs_sb_info *sbi, int type)
  434. {
  435. struct curseg_info *curseg = CURSEG_I(sbi, type);
  436. return curseg->next_blkoff;
  437. }
  438. static inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno)
  439. {
  440. unsigned int end_segno = SM_I(sbi)->segment_count - 1;
  441. BUG_ON(segno > end_segno);
  442. }
  443. /*
  444. * This function is used for only debugging.
  445. * NOTE: In future, we have to remove this function.
  446. */
  447. static inline void verify_block_addr(struct f2fs_sb_info *sbi, block_t blk_addr)
  448. {
  449. struct f2fs_sm_info *sm_info = SM_I(sbi);
  450. block_t total_blks = sm_info->segment_count << sbi->log_blocks_per_seg;
  451. block_t start_addr = sm_info->seg0_blkaddr;
  452. block_t end_addr = start_addr + total_blks - 1;
  453. BUG_ON(blk_addr < start_addr);
  454. BUG_ON(blk_addr > end_addr);
  455. }
  456. /*
  457. * Summary block is always treated as invalid block
  458. */
  459. static inline void check_block_count(struct f2fs_sb_info *sbi,
  460. int segno, struct f2fs_sit_entry *raw_sit)
  461. {
  462. struct f2fs_sm_info *sm_info = SM_I(sbi);
  463. unsigned int end_segno = sm_info->segment_count - 1;
  464. int valid_blocks = 0;
  465. int i;
  466. /* check segment usage */
  467. BUG_ON(GET_SIT_VBLOCKS(raw_sit) > sbi->blocks_per_seg);
  468. /* check boundary of a given segment number */
  469. BUG_ON(segno > end_segno);
  470. /* check bitmap with valid block count */
  471. for (i = 0; i < sbi->blocks_per_seg; i++)
  472. if (f2fs_test_bit(i, raw_sit->valid_map))
  473. valid_blocks++;
  474. BUG_ON(GET_SIT_VBLOCKS(raw_sit) != valid_blocks);
  475. }
  476. static inline pgoff_t current_sit_addr(struct f2fs_sb_info *sbi,
  477. unsigned int start)
  478. {
  479. struct sit_info *sit_i = SIT_I(sbi);
  480. unsigned int offset = SIT_BLOCK_OFFSET(sit_i, start);
  481. block_t blk_addr = sit_i->sit_base_addr + offset;
  482. check_seg_range(sbi, start);
  483. /* calculate sit block address */
  484. if (f2fs_test_bit(offset, sit_i->sit_bitmap))
  485. blk_addr += sit_i->sit_blocks;
  486. return blk_addr;
  487. }
  488. static inline pgoff_t next_sit_addr(struct f2fs_sb_info *sbi,
  489. pgoff_t block_addr)
  490. {
  491. struct sit_info *sit_i = SIT_I(sbi);
  492. block_addr -= sit_i->sit_base_addr;
  493. if (block_addr < sit_i->sit_blocks)
  494. block_addr += sit_i->sit_blocks;
  495. else
  496. block_addr -= sit_i->sit_blocks;
  497. return block_addr + sit_i->sit_base_addr;
  498. }
  499. static inline void set_to_next_sit(struct sit_info *sit_i, unsigned int start)
  500. {
  501. unsigned int block_off = SIT_BLOCK_OFFSET(sit_i, start);
  502. if (f2fs_test_bit(block_off, sit_i->sit_bitmap))
  503. f2fs_clear_bit(block_off, sit_i->sit_bitmap);
  504. else
  505. f2fs_set_bit(block_off, sit_i->sit_bitmap);
  506. }
  507. static inline unsigned long long get_mtime(struct f2fs_sb_info *sbi)
  508. {
  509. struct sit_info *sit_i = SIT_I(sbi);
  510. return sit_i->elapsed_time + CURRENT_TIME_SEC.tv_sec -
  511. sit_i->mounted_time;
  512. }
  513. static inline void set_summary(struct f2fs_summary *sum, nid_t nid,
  514. unsigned int ofs_in_node, unsigned char version)
  515. {
  516. sum->nid = cpu_to_le32(nid);
  517. sum->ofs_in_node = cpu_to_le16(ofs_in_node);
  518. sum->version = version;
  519. }
  520. static inline block_t start_sum_block(struct f2fs_sb_info *sbi)
  521. {
  522. return __start_cp_addr(sbi) +
  523. le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
  524. }
  525. static inline block_t sum_blk_addr(struct f2fs_sb_info *sbi, int base, int type)
  526. {
  527. return __start_cp_addr(sbi) +
  528. le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_total_block_count)
  529. - (base + 1) + type;
  530. }