segment.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/f2fs/segment.h
  4. *
  5. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  6. * http://www.samsung.com/
  7. */
  8. #include <linux/blkdev.h>
  9. #include <linux/backing-dev.h>
  10. /* constant macro */
  11. #define NULL_SEGNO ((unsigned int)(~0))
  12. #define NULL_SECNO ((unsigned int)(~0))
  13. #define DEF_RECLAIM_PREFREE_SEGMENTS 5 /* 5% over total segments */
  14. #define DEF_MAX_RECLAIM_PREFREE_SEGMENTS 4096 /* 8GB in maximum */
  15. #define F2FS_MIN_SEGMENTS 9 /* SB + 2 (CP + SIT + NAT) + SSA + MAIN */
  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) ((t) <= CURSEG_COLD_DATA)
  20. #define IS_NODESEG(t) ((t) >= CURSEG_HOT_NODE)
  21. #define IS_HOT(t) ((t) == CURSEG_HOT_NODE || (t) == CURSEG_HOT_DATA)
  22. #define IS_WARM(t) ((t) == CURSEG_WARM_NODE || (t) == CURSEG_WARM_DATA)
  23. #define IS_COLD(t) ((t) == CURSEG_COLD_NODE || (t) == CURSEG_COLD_DATA)
  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 MAIN_BLKADDR(sbi) \
  45. (SM_I(sbi) ? SM_I(sbi)->main_blkaddr : \
  46. le32_to_cpu(F2FS_RAW_SUPER(sbi)->main_blkaddr))
  47. #define SEG0_BLKADDR(sbi) \
  48. (SM_I(sbi) ? SM_I(sbi)->seg0_blkaddr : \
  49. le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment0_blkaddr))
  50. #define MAIN_SEGS(sbi) (SM_I(sbi)->main_segments)
  51. #define MAIN_SECS(sbi) ((sbi)->total_sections)
  52. #define TOTAL_SEGS(sbi) \
  53. (SM_I(sbi) ? SM_I(sbi)->segment_count : \
  54. le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count))
  55. #define TOTAL_BLKS(sbi) (TOTAL_SEGS(sbi) << (sbi)->log_blocks_per_seg)
  56. #define MAX_BLKADDR(sbi) (SEG0_BLKADDR(sbi) + TOTAL_BLKS(sbi))
  57. #define SEGMENT_SIZE(sbi) (1ULL << ((sbi)->log_blocksize + \
  58. (sbi)->log_blocks_per_seg))
  59. #define START_BLOCK(sbi, segno) (SEG0_BLKADDR(sbi) + \
  60. (GET_R2L_SEGNO(FREE_I(sbi), segno) << (sbi)->log_blocks_per_seg))
  61. #define NEXT_FREE_BLKADDR(sbi, curseg) \
  62. (START_BLOCK(sbi, (curseg)->segno) + (curseg)->next_blkoff)
  63. #define GET_SEGOFF_FROM_SEG0(sbi, blk_addr) ((blk_addr) - SEG0_BLKADDR(sbi))
  64. #define GET_SEGNO_FROM_SEG0(sbi, blk_addr) \
  65. (GET_SEGOFF_FROM_SEG0(sbi, blk_addr) >> (sbi)->log_blocks_per_seg)
  66. #define GET_BLKOFF_FROM_SEG0(sbi, blk_addr) \
  67. (GET_SEGOFF_FROM_SEG0(sbi, blk_addr) & ((sbi)->blocks_per_seg - 1))
  68. #define GET_SEGNO(sbi, blk_addr) \
  69. ((!is_valid_data_blkaddr(sbi, blk_addr)) ? \
  70. NULL_SEGNO : GET_L2R_SEGNO(FREE_I(sbi), \
  71. GET_SEGNO_FROM_SEG0(sbi, blk_addr)))
  72. #define BLKS_PER_SEC(sbi) \
  73. ((sbi)->segs_per_sec * (sbi)->blocks_per_seg)
  74. #define GET_SEC_FROM_SEG(sbi, segno) \
  75. ((segno) / (sbi)->segs_per_sec)
  76. #define GET_SEG_FROM_SEC(sbi, secno) \
  77. ((secno) * (sbi)->segs_per_sec)
  78. #define GET_ZONE_FROM_SEC(sbi, secno) \
  79. ((secno) / (sbi)->secs_per_zone)
  80. #define GET_ZONE_FROM_SEG(sbi, segno) \
  81. GET_ZONE_FROM_SEC(sbi, GET_SEC_FROM_SEG(sbi, segno))
  82. #define GET_SUM_BLOCK(sbi, segno) \
  83. ((sbi)->sm_info->ssa_blkaddr + (segno))
  84. #define GET_SUM_TYPE(footer) ((footer)->entry_type)
  85. #define SET_SUM_TYPE(footer, type) ((footer)->entry_type = (type))
  86. #define SIT_ENTRY_OFFSET(sit_i, segno) \
  87. ((segno) % (sit_i)->sents_per_block)
  88. #define SIT_BLOCK_OFFSET(segno) \
  89. ((segno) / SIT_ENTRY_PER_BLOCK)
  90. #define START_SEGNO(segno) \
  91. (SIT_BLOCK_OFFSET(segno) * SIT_ENTRY_PER_BLOCK)
  92. #define SIT_BLK_CNT(sbi) \
  93. ((MAIN_SEGS(sbi) + SIT_ENTRY_PER_BLOCK - 1) / SIT_ENTRY_PER_BLOCK)
  94. #define f2fs_bitmap_size(nr) \
  95. (BITS_TO_LONGS(nr) * sizeof(unsigned long))
  96. #define SECTOR_FROM_BLOCK(blk_addr) \
  97. (((sector_t)blk_addr) << F2FS_LOG_SECTORS_PER_BLOCK)
  98. #define SECTOR_TO_BLOCK(sectors) \
  99. ((sectors) >> F2FS_LOG_SECTORS_PER_BLOCK)
  100. /*
  101. * indicate a block allocation direction: RIGHT and LEFT.
  102. * RIGHT means allocating new sections towards the end of volume.
  103. * LEFT means the opposite direction.
  104. */
  105. enum {
  106. ALLOC_RIGHT = 0,
  107. ALLOC_LEFT
  108. };
  109. /*
  110. * In the victim_sel_policy->alloc_mode, there are two block allocation modes.
  111. * LFS writes data sequentially with cleaning operations.
  112. * SSR (Slack Space Recycle) reuses obsolete space without cleaning operations.
  113. */
  114. enum {
  115. LFS = 0,
  116. SSR
  117. };
  118. /*
  119. * In the victim_sel_policy->gc_mode, there are two gc, aka cleaning, modes.
  120. * GC_CB is based on cost-benefit algorithm.
  121. * GC_GREEDY is based on greedy algorithm.
  122. */
  123. enum {
  124. GC_CB = 0,
  125. GC_GREEDY,
  126. ALLOC_NEXT,
  127. FLUSH_DEVICE,
  128. MAX_GC_POLICY,
  129. };
  130. /*
  131. * BG_GC means the background cleaning job.
  132. * FG_GC means the on-demand cleaning job.
  133. * FORCE_FG_GC means on-demand cleaning job in background.
  134. */
  135. enum {
  136. BG_GC = 0,
  137. FG_GC,
  138. FORCE_FG_GC,
  139. };
  140. /* for a function parameter to select a victim segment */
  141. struct victim_sel_policy {
  142. int alloc_mode; /* LFS or SSR */
  143. int gc_mode; /* GC_CB or GC_GREEDY */
  144. unsigned long *dirty_segmap; /* dirty segment bitmap */
  145. unsigned int max_search; /* maximum # of segments to search */
  146. unsigned int offset; /* last scanned bitmap offset */
  147. unsigned int ofs_unit; /* bitmap search unit */
  148. unsigned int min_cost; /* minimum cost */
  149. unsigned int min_segno; /* segment # having min. cost */
  150. };
  151. struct seg_entry {
  152. unsigned int type:6; /* segment type like CURSEG_XXX_TYPE */
  153. unsigned int valid_blocks:10; /* # of valid blocks */
  154. unsigned int ckpt_valid_blocks:10; /* # of valid blocks last cp */
  155. unsigned int padding:6; /* padding */
  156. unsigned char *cur_valid_map; /* validity bitmap of blocks */
  157. #ifdef CONFIG_F2FS_CHECK_FS
  158. unsigned char *cur_valid_map_mir; /* mirror of current valid bitmap */
  159. #endif
  160. /*
  161. * # of valid blocks and the validity bitmap stored in the the last
  162. * checkpoint pack. This information is used by the SSR mode.
  163. */
  164. unsigned char *ckpt_valid_map; /* validity bitmap of blocks last cp */
  165. unsigned char *discard_map;
  166. unsigned long long mtime; /* modification time of the segment */
  167. };
  168. struct sec_entry {
  169. unsigned int valid_blocks; /* # of valid blocks in a section */
  170. };
  171. struct segment_allocation {
  172. void (*allocate_segment)(struct f2fs_sb_info *, int, bool);
  173. };
  174. /*
  175. * this value is set in page as a private data which indicate that
  176. * the page is atomically written, and it is in inmem_pages list.
  177. */
  178. #define ATOMIC_WRITTEN_PAGE ((unsigned long)-1)
  179. #define DUMMY_WRITTEN_PAGE ((unsigned long)-2)
  180. #define IS_ATOMIC_WRITTEN_PAGE(page) \
  181. (page_private(page) == (unsigned long)ATOMIC_WRITTEN_PAGE)
  182. #define IS_DUMMY_WRITTEN_PAGE(page) \
  183. (page_private(page) == (unsigned long)DUMMY_WRITTEN_PAGE)
  184. #define MAX_SKIP_GC_COUNT 16
  185. struct inmem_pages {
  186. struct list_head list;
  187. struct page *page;
  188. block_t old_addr; /* for revoking when fail to commit */
  189. };
  190. struct sit_info {
  191. const struct segment_allocation *s_ops;
  192. block_t sit_base_addr; /* start block address of SIT area */
  193. block_t sit_blocks; /* # of blocks used by SIT area */
  194. block_t written_valid_blocks; /* # of valid blocks in main area */
  195. char *sit_bitmap; /* SIT bitmap pointer */
  196. #ifdef CONFIG_F2FS_CHECK_FS
  197. char *sit_bitmap_mir; /* SIT bitmap mirror */
  198. #endif
  199. unsigned int bitmap_size; /* SIT bitmap size */
  200. unsigned long *tmp_map; /* bitmap for temporal use */
  201. unsigned long *dirty_sentries_bitmap; /* bitmap for dirty sentries */
  202. unsigned int dirty_sentries; /* # of dirty sentries */
  203. unsigned int sents_per_block; /* # of SIT entries per block */
  204. struct rw_semaphore sentry_lock; /* to protect SIT cache */
  205. struct seg_entry *sentries; /* SIT segment-level cache */
  206. struct sec_entry *sec_entries; /* SIT section-level cache */
  207. /* for cost-benefit algorithm in cleaning procedure */
  208. unsigned long long elapsed_time; /* elapsed time after mount */
  209. unsigned long long mounted_time; /* mount time */
  210. unsigned long long min_mtime; /* min. modification time */
  211. unsigned long long max_mtime; /* max. modification time */
  212. unsigned int last_victim[MAX_GC_POLICY]; /* last victim segment # */
  213. };
  214. struct free_segmap_info {
  215. unsigned int start_segno; /* start segment number logically */
  216. unsigned int free_segments; /* # of free segments */
  217. unsigned int free_sections; /* # of free sections */
  218. spinlock_t segmap_lock; /* free segmap lock */
  219. unsigned long *free_segmap; /* free segment bitmap */
  220. unsigned long *free_secmap; /* free section bitmap */
  221. };
  222. /* Notice: The order of dirty type is same with CURSEG_XXX in f2fs.h */
  223. enum dirty_type {
  224. DIRTY_HOT_DATA, /* dirty segments assigned as hot data logs */
  225. DIRTY_WARM_DATA, /* dirty segments assigned as warm data logs */
  226. DIRTY_COLD_DATA, /* dirty segments assigned as cold data logs */
  227. DIRTY_HOT_NODE, /* dirty segments assigned as hot node logs */
  228. DIRTY_WARM_NODE, /* dirty segments assigned as warm node logs */
  229. DIRTY_COLD_NODE, /* dirty segments assigned as cold node logs */
  230. DIRTY, /* to count # of dirty segments */
  231. PRE, /* to count # of entirely obsolete segments */
  232. NR_DIRTY_TYPE
  233. };
  234. struct dirty_seglist_info {
  235. const struct victim_selection *v_ops; /* victim selction operation */
  236. unsigned long *dirty_segmap[NR_DIRTY_TYPE];
  237. struct mutex seglist_lock; /* lock for segment bitmaps */
  238. int nr_dirty[NR_DIRTY_TYPE]; /* # of dirty segments */
  239. unsigned long *victim_secmap; /* background GC victims */
  240. };
  241. /* victim selection function for cleaning and SSR */
  242. struct victim_selection {
  243. int (*get_victim)(struct f2fs_sb_info *, unsigned int *,
  244. int, int, char);
  245. };
  246. /* for active log information */
  247. struct curseg_info {
  248. struct mutex curseg_mutex; /* lock for consistency */
  249. struct f2fs_summary_block *sum_blk; /* cached summary block */
  250. struct rw_semaphore journal_rwsem; /* protect journal area */
  251. struct f2fs_journal *journal; /* cached journal info */
  252. unsigned char alloc_type; /* current allocation type */
  253. unsigned int segno; /* current segment number */
  254. unsigned short next_blkoff; /* next block offset to write */
  255. unsigned int zone; /* current zone number */
  256. unsigned int next_segno; /* preallocated segment */
  257. };
  258. struct sit_entry_set {
  259. struct list_head set_list; /* link with all sit sets */
  260. unsigned int start_segno; /* start segno of sits in set */
  261. unsigned int entry_cnt; /* the # of sit entries in set */
  262. };
  263. /*
  264. * inline functions
  265. */
  266. static inline struct curseg_info *CURSEG_I(struct f2fs_sb_info *sbi, int type)
  267. {
  268. return (struct curseg_info *)(SM_I(sbi)->curseg_array + type);
  269. }
  270. static inline struct seg_entry *get_seg_entry(struct f2fs_sb_info *sbi,
  271. unsigned int segno)
  272. {
  273. struct sit_info *sit_i = SIT_I(sbi);
  274. return &sit_i->sentries[segno];
  275. }
  276. static inline struct sec_entry *get_sec_entry(struct f2fs_sb_info *sbi,
  277. unsigned int segno)
  278. {
  279. struct sit_info *sit_i = SIT_I(sbi);
  280. return &sit_i->sec_entries[GET_SEC_FROM_SEG(sbi, segno)];
  281. }
  282. static inline unsigned int get_valid_blocks(struct f2fs_sb_info *sbi,
  283. unsigned int segno, bool use_section)
  284. {
  285. /*
  286. * In order to get # of valid blocks in a section instantly from many
  287. * segments, f2fs manages two counting structures separately.
  288. */
  289. if (use_section && sbi->segs_per_sec > 1)
  290. return get_sec_entry(sbi, segno)->valid_blocks;
  291. else
  292. return get_seg_entry(sbi, segno)->valid_blocks;
  293. }
  294. static inline unsigned int get_ckpt_valid_blocks(struct f2fs_sb_info *sbi,
  295. unsigned int segno)
  296. {
  297. return get_seg_entry(sbi, segno)->ckpt_valid_blocks;
  298. }
  299. static inline void seg_info_from_raw_sit(struct seg_entry *se,
  300. struct f2fs_sit_entry *rs)
  301. {
  302. se->valid_blocks = GET_SIT_VBLOCKS(rs);
  303. se->ckpt_valid_blocks = GET_SIT_VBLOCKS(rs);
  304. memcpy(se->cur_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
  305. memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
  306. #ifdef CONFIG_F2FS_CHECK_FS
  307. memcpy(se->cur_valid_map_mir, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
  308. #endif
  309. se->type = GET_SIT_TYPE(rs);
  310. se->mtime = le64_to_cpu(rs->mtime);
  311. }
  312. static inline void __seg_info_to_raw_sit(struct seg_entry *se,
  313. struct f2fs_sit_entry *rs)
  314. {
  315. unsigned short raw_vblocks = (se->type << SIT_VBLOCKS_SHIFT) |
  316. se->valid_blocks;
  317. rs->vblocks = cpu_to_le16(raw_vblocks);
  318. memcpy(rs->valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
  319. rs->mtime = cpu_to_le64(se->mtime);
  320. }
  321. static inline void seg_info_to_sit_page(struct f2fs_sb_info *sbi,
  322. struct page *page, unsigned int start)
  323. {
  324. struct f2fs_sit_block *raw_sit;
  325. struct seg_entry *se;
  326. struct f2fs_sit_entry *rs;
  327. unsigned int end = min(start + SIT_ENTRY_PER_BLOCK,
  328. (unsigned long)MAIN_SEGS(sbi));
  329. int i;
  330. raw_sit = (struct f2fs_sit_block *)page_address(page);
  331. memset(raw_sit, 0, PAGE_SIZE);
  332. for (i = 0; i < end - start; i++) {
  333. rs = &raw_sit->entries[i];
  334. se = get_seg_entry(sbi, start + i);
  335. __seg_info_to_raw_sit(se, rs);
  336. }
  337. }
  338. static inline void seg_info_to_raw_sit(struct seg_entry *se,
  339. struct f2fs_sit_entry *rs)
  340. {
  341. __seg_info_to_raw_sit(se, rs);
  342. memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
  343. se->ckpt_valid_blocks = se->valid_blocks;
  344. }
  345. static inline unsigned int find_next_inuse(struct free_segmap_info *free_i,
  346. unsigned int max, unsigned int segno)
  347. {
  348. unsigned int ret;
  349. spin_lock(&free_i->segmap_lock);
  350. ret = find_next_bit(free_i->free_segmap, max, segno);
  351. spin_unlock(&free_i->segmap_lock);
  352. return ret;
  353. }
  354. static inline void __set_free(struct f2fs_sb_info *sbi, unsigned int segno)
  355. {
  356. struct free_segmap_info *free_i = FREE_I(sbi);
  357. unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
  358. unsigned int start_segno = GET_SEG_FROM_SEC(sbi, secno);
  359. unsigned int next;
  360. spin_lock(&free_i->segmap_lock);
  361. clear_bit(segno, free_i->free_segmap);
  362. free_i->free_segments++;
  363. next = find_next_bit(free_i->free_segmap,
  364. start_segno + sbi->segs_per_sec, start_segno);
  365. if (next >= start_segno + sbi->segs_per_sec) {
  366. clear_bit(secno, free_i->free_secmap);
  367. free_i->free_sections++;
  368. }
  369. spin_unlock(&free_i->segmap_lock);
  370. }
  371. static inline void __set_inuse(struct f2fs_sb_info *sbi,
  372. unsigned int segno)
  373. {
  374. struct free_segmap_info *free_i = FREE_I(sbi);
  375. unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
  376. set_bit(segno, free_i->free_segmap);
  377. free_i->free_segments--;
  378. if (!test_and_set_bit(secno, free_i->free_secmap))
  379. free_i->free_sections--;
  380. }
  381. static inline void __set_test_and_free(struct f2fs_sb_info *sbi,
  382. unsigned int segno)
  383. {
  384. struct free_segmap_info *free_i = FREE_I(sbi);
  385. unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
  386. unsigned int start_segno = GET_SEG_FROM_SEC(sbi, secno);
  387. unsigned int next;
  388. spin_lock(&free_i->segmap_lock);
  389. if (test_and_clear_bit(segno, free_i->free_segmap)) {
  390. free_i->free_segments++;
  391. if (IS_CURSEC(sbi, secno))
  392. goto skip_free;
  393. next = find_next_bit(free_i->free_segmap,
  394. start_segno + sbi->segs_per_sec, start_segno);
  395. if (next >= start_segno + sbi->segs_per_sec) {
  396. if (test_and_clear_bit(secno, free_i->free_secmap))
  397. free_i->free_sections++;
  398. }
  399. }
  400. skip_free:
  401. spin_unlock(&free_i->segmap_lock);
  402. }
  403. static inline void __set_test_and_inuse(struct f2fs_sb_info *sbi,
  404. unsigned int segno)
  405. {
  406. struct free_segmap_info *free_i = FREE_I(sbi);
  407. unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
  408. spin_lock(&free_i->segmap_lock);
  409. if (!test_and_set_bit(segno, free_i->free_segmap)) {
  410. free_i->free_segments--;
  411. if (!test_and_set_bit(secno, free_i->free_secmap))
  412. free_i->free_sections--;
  413. }
  414. spin_unlock(&free_i->segmap_lock);
  415. }
  416. static inline void get_sit_bitmap(struct f2fs_sb_info *sbi,
  417. void *dst_addr)
  418. {
  419. struct sit_info *sit_i = SIT_I(sbi);
  420. #ifdef CONFIG_F2FS_CHECK_FS
  421. if (memcmp(sit_i->sit_bitmap, sit_i->sit_bitmap_mir,
  422. sit_i->bitmap_size))
  423. f2fs_bug_on(sbi, 1);
  424. #endif
  425. memcpy(dst_addr, sit_i->sit_bitmap, sit_i->bitmap_size);
  426. }
  427. static inline block_t written_block_count(struct f2fs_sb_info *sbi)
  428. {
  429. return SIT_I(sbi)->written_valid_blocks;
  430. }
  431. static inline unsigned int free_segments(struct f2fs_sb_info *sbi)
  432. {
  433. return FREE_I(sbi)->free_segments;
  434. }
  435. static inline int reserved_segments(struct f2fs_sb_info *sbi)
  436. {
  437. return SM_I(sbi)->reserved_segments;
  438. }
  439. static inline unsigned int free_sections(struct f2fs_sb_info *sbi)
  440. {
  441. return FREE_I(sbi)->free_sections;
  442. }
  443. static inline unsigned int prefree_segments(struct f2fs_sb_info *sbi)
  444. {
  445. return DIRTY_I(sbi)->nr_dirty[PRE];
  446. }
  447. static inline unsigned int dirty_segments(struct f2fs_sb_info *sbi)
  448. {
  449. return DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_DATA] +
  450. DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_DATA] +
  451. DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_DATA] +
  452. DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_NODE] +
  453. DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_NODE] +
  454. DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_NODE];
  455. }
  456. static inline int overprovision_segments(struct f2fs_sb_info *sbi)
  457. {
  458. return SM_I(sbi)->ovp_segments;
  459. }
  460. static inline int reserved_sections(struct f2fs_sb_info *sbi)
  461. {
  462. return GET_SEC_FROM_SEG(sbi, (unsigned int)reserved_segments(sbi));
  463. }
  464. static inline bool has_curseg_enough_space(struct f2fs_sb_info *sbi)
  465. {
  466. unsigned int node_blocks = get_pages(sbi, F2FS_DIRTY_NODES) +
  467. get_pages(sbi, F2FS_DIRTY_DENTS);
  468. unsigned int dent_blocks = get_pages(sbi, F2FS_DIRTY_DENTS);
  469. unsigned int segno, left_blocks;
  470. int i;
  471. /* check current node segment */
  472. for (i = CURSEG_HOT_NODE; i <= CURSEG_COLD_NODE; i++) {
  473. segno = CURSEG_I(sbi, i)->segno;
  474. left_blocks = sbi->blocks_per_seg -
  475. get_seg_entry(sbi, segno)->ckpt_valid_blocks;
  476. if (node_blocks > left_blocks)
  477. return false;
  478. }
  479. /* check current data segment */
  480. segno = CURSEG_I(sbi, CURSEG_HOT_DATA)->segno;
  481. left_blocks = sbi->blocks_per_seg -
  482. get_seg_entry(sbi, segno)->ckpt_valid_blocks;
  483. if (dent_blocks > left_blocks)
  484. return false;
  485. return true;
  486. }
  487. static inline bool has_not_enough_free_secs(struct f2fs_sb_info *sbi,
  488. int freed, int needed)
  489. {
  490. int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
  491. int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
  492. int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA);
  493. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  494. return false;
  495. if (free_sections(sbi) + freed == reserved_sections(sbi) + needed &&
  496. has_curseg_enough_space(sbi))
  497. return false;
  498. return (free_sections(sbi) + freed) <=
  499. (node_secs + 2 * dent_secs + imeta_secs +
  500. reserved_sections(sbi) + needed);
  501. }
  502. static inline int f2fs_is_checkpoint_ready(struct f2fs_sb_info *sbi)
  503. {
  504. if (likely(!is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
  505. return 0;
  506. if (likely(!has_not_enough_free_secs(sbi, 0, 0)))
  507. return 0;
  508. return -ENOSPC;
  509. }
  510. static inline bool excess_prefree_segs(struct f2fs_sb_info *sbi)
  511. {
  512. return prefree_segments(sbi) > SM_I(sbi)->rec_prefree_segments;
  513. }
  514. static inline int utilization(struct f2fs_sb_info *sbi)
  515. {
  516. return div_u64((u64)valid_user_blocks(sbi) * 100,
  517. sbi->user_block_count);
  518. }
  519. /*
  520. * Sometimes f2fs may be better to drop out-of-place update policy.
  521. * And, users can control the policy through sysfs entries.
  522. * There are five policies with triggering conditions as follows.
  523. * F2FS_IPU_FORCE - all the time,
  524. * F2FS_IPU_SSR - if SSR mode is activated,
  525. * F2FS_IPU_UTIL - if FS utilization is over threashold,
  526. * F2FS_IPU_SSR_UTIL - if SSR mode is activated and FS utilization is over
  527. * threashold,
  528. * F2FS_IPU_FSYNC - activated in fsync path only for high performance flash
  529. * storages. IPU will be triggered only if the # of dirty
  530. * pages over min_fsync_blocks.
  531. * F2FS_IPUT_DISABLE - disable IPU. (=default option)
  532. */
  533. #define DEF_MIN_IPU_UTIL 70
  534. #define DEF_MIN_FSYNC_BLOCKS 8
  535. #define DEF_MIN_HOT_BLOCKS 16
  536. #define SMALL_VOLUME_SEGMENTS (16 * 512) /* 16GB */
  537. enum {
  538. F2FS_IPU_FORCE,
  539. F2FS_IPU_SSR,
  540. F2FS_IPU_UTIL,
  541. F2FS_IPU_SSR_UTIL,
  542. F2FS_IPU_FSYNC,
  543. F2FS_IPU_ASYNC,
  544. };
  545. static inline unsigned int curseg_segno(struct f2fs_sb_info *sbi,
  546. int type)
  547. {
  548. struct curseg_info *curseg = CURSEG_I(sbi, type);
  549. return curseg->segno;
  550. }
  551. static inline unsigned char curseg_alloc_type(struct f2fs_sb_info *sbi,
  552. int type)
  553. {
  554. struct curseg_info *curseg = CURSEG_I(sbi, type);
  555. return curseg->alloc_type;
  556. }
  557. static inline unsigned short curseg_blkoff(struct f2fs_sb_info *sbi, int type)
  558. {
  559. struct curseg_info *curseg = CURSEG_I(sbi, type);
  560. return curseg->next_blkoff;
  561. }
  562. static inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno)
  563. {
  564. f2fs_bug_on(sbi, segno > TOTAL_SEGS(sbi) - 1);
  565. }
  566. static inline void verify_block_addr(struct f2fs_io_info *fio, block_t blk_addr)
  567. {
  568. struct f2fs_sb_info *sbi = fio->sbi;
  569. if (__is_meta_io(fio))
  570. verify_blkaddr(sbi, blk_addr, META_GENERIC);
  571. else
  572. verify_blkaddr(sbi, blk_addr, DATA_GENERIC);
  573. }
  574. /*
  575. * Summary block is always treated as an invalid block
  576. */
  577. static inline int check_block_count(struct f2fs_sb_info *sbi,
  578. int segno, struct f2fs_sit_entry *raw_sit)
  579. {
  580. #ifdef CONFIG_F2FS_CHECK_FS
  581. bool is_valid = test_bit_le(0, raw_sit->valid_map) ? true : false;
  582. int valid_blocks = 0;
  583. int cur_pos = 0, next_pos;
  584. /* check bitmap with valid block count */
  585. do {
  586. if (is_valid) {
  587. next_pos = find_next_zero_bit_le(&raw_sit->valid_map,
  588. sbi->blocks_per_seg,
  589. cur_pos);
  590. valid_blocks += next_pos - cur_pos;
  591. } else
  592. next_pos = find_next_bit_le(&raw_sit->valid_map,
  593. sbi->blocks_per_seg,
  594. cur_pos);
  595. cur_pos = next_pos;
  596. is_valid = !is_valid;
  597. } while (cur_pos < sbi->blocks_per_seg);
  598. if (unlikely(GET_SIT_VBLOCKS(raw_sit) != valid_blocks)) {
  599. f2fs_msg(sbi->sb, KERN_ERR,
  600. "Mismatch valid blocks %d vs. %d",
  601. GET_SIT_VBLOCKS(raw_sit), valid_blocks);
  602. set_sbi_flag(sbi, SBI_NEED_FSCK);
  603. return -EINVAL;
  604. }
  605. #endif
  606. /* check segment usage, and check boundary of a given segment number */
  607. if (unlikely(GET_SIT_VBLOCKS(raw_sit) > sbi->blocks_per_seg
  608. || segno > TOTAL_SEGS(sbi) - 1)) {
  609. f2fs_msg(sbi->sb, KERN_ERR,
  610. "Wrong valid blocks %d or segno %u",
  611. GET_SIT_VBLOCKS(raw_sit), segno);
  612. set_sbi_flag(sbi, SBI_NEED_FSCK);
  613. return -EINVAL;
  614. }
  615. return 0;
  616. }
  617. static inline pgoff_t current_sit_addr(struct f2fs_sb_info *sbi,
  618. unsigned int start)
  619. {
  620. struct sit_info *sit_i = SIT_I(sbi);
  621. unsigned int offset = SIT_BLOCK_OFFSET(start);
  622. block_t blk_addr = sit_i->sit_base_addr + offset;
  623. check_seg_range(sbi, start);
  624. #ifdef CONFIG_F2FS_CHECK_FS
  625. if (f2fs_test_bit(offset, sit_i->sit_bitmap) !=
  626. f2fs_test_bit(offset, sit_i->sit_bitmap_mir))
  627. f2fs_bug_on(sbi, 1);
  628. #endif
  629. /* calculate sit block address */
  630. if (f2fs_test_bit(offset, sit_i->sit_bitmap))
  631. blk_addr += sit_i->sit_blocks;
  632. return blk_addr;
  633. }
  634. static inline pgoff_t next_sit_addr(struct f2fs_sb_info *sbi,
  635. pgoff_t block_addr)
  636. {
  637. struct sit_info *sit_i = SIT_I(sbi);
  638. block_addr -= sit_i->sit_base_addr;
  639. if (block_addr < sit_i->sit_blocks)
  640. block_addr += sit_i->sit_blocks;
  641. else
  642. block_addr -= sit_i->sit_blocks;
  643. return block_addr + sit_i->sit_base_addr;
  644. }
  645. static inline void set_to_next_sit(struct sit_info *sit_i, unsigned int start)
  646. {
  647. unsigned int block_off = SIT_BLOCK_OFFSET(start);
  648. f2fs_change_bit(block_off, sit_i->sit_bitmap);
  649. #ifdef CONFIG_F2FS_CHECK_FS
  650. f2fs_change_bit(block_off, sit_i->sit_bitmap_mir);
  651. #endif
  652. }
  653. static inline unsigned long long get_mtime(struct f2fs_sb_info *sbi,
  654. bool base_time)
  655. {
  656. struct sit_info *sit_i = SIT_I(sbi);
  657. time64_t diff, now = ktime_get_real_seconds();
  658. if (now >= sit_i->mounted_time)
  659. return sit_i->elapsed_time + now - sit_i->mounted_time;
  660. /* system time is set to the past */
  661. if (!base_time) {
  662. diff = sit_i->mounted_time - now;
  663. if (sit_i->elapsed_time >= diff)
  664. return sit_i->elapsed_time - diff;
  665. return 0;
  666. }
  667. return sit_i->elapsed_time;
  668. }
  669. static inline void set_summary(struct f2fs_summary *sum, nid_t nid,
  670. unsigned int ofs_in_node, unsigned char version)
  671. {
  672. sum->nid = cpu_to_le32(nid);
  673. sum->ofs_in_node = cpu_to_le16(ofs_in_node);
  674. sum->version = version;
  675. }
  676. static inline block_t start_sum_block(struct f2fs_sb_info *sbi)
  677. {
  678. return __start_cp_addr(sbi) +
  679. le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
  680. }
  681. static inline block_t sum_blk_addr(struct f2fs_sb_info *sbi, int base, int type)
  682. {
  683. return __start_cp_addr(sbi) +
  684. le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_total_block_count)
  685. - (base + 1) + type;
  686. }
  687. static inline bool sec_usage_check(struct f2fs_sb_info *sbi, unsigned int secno)
  688. {
  689. if (IS_CURSEC(sbi, secno) || (sbi->cur_victim_sec == secno))
  690. return true;
  691. return false;
  692. }
  693. /*
  694. * It is very important to gather dirty pages and write at once, so that we can
  695. * submit a big bio without interfering other data writes.
  696. * By default, 512 pages for directory data,
  697. * 512 pages (2MB) * 8 for nodes, and
  698. * 256 pages * 8 for meta are set.
  699. */
  700. static inline int nr_pages_to_skip(struct f2fs_sb_info *sbi, int type)
  701. {
  702. if (sbi->sb->s_bdi->wb.dirty_exceeded)
  703. return 0;
  704. if (type == DATA)
  705. return sbi->blocks_per_seg;
  706. else if (type == NODE)
  707. return 8 * sbi->blocks_per_seg;
  708. else if (type == META)
  709. return 8 * BIO_MAX_PAGES;
  710. else
  711. return 0;
  712. }
  713. /*
  714. * When writing pages, it'd better align nr_to_write for segment size.
  715. */
  716. static inline long nr_pages_to_write(struct f2fs_sb_info *sbi, int type,
  717. struct writeback_control *wbc)
  718. {
  719. long nr_to_write, desired;
  720. if (wbc->sync_mode != WB_SYNC_NONE)
  721. return 0;
  722. nr_to_write = wbc->nr_to_write;
  723. desired = BIO_MAX_PAGES;
  724. if (type == NODE)
  725. desired <<= 1;
  726. wbc->nr_to_write = desired;
  727. return desired - nr_to_write;
  728. }
  729. static inline void wake_up_discard_thread(struct f2fs_sb_info *sbi, bool force)
  730. {
  731. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  732. bool wakeup = false;
  733. int i;
  734. if (force)
  735. goto wake_up;
  736. mutex_lock(&dcc->cmd_lock);
  737. for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
  738. if (i + 1 < dcc->discard_granularity)
  739. break;
  740. if (!list_empty(&dcc->pend_list[i])) {
  741. wakeup = true;
  742. break;
  743. }
  744. }
  745. mutex_unlock(&dcc->cmd_lock);
  746. if (!wakeup)
  747. return;
  748. wake_up:
  749. dcc->discard_wake = 1;
  750. wake_up_interruptible_all(&dcc->discard_wait_queue);
  751. }