segment.h 19 KB

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