segment.h 20 KB

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