segment.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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. #define F2FS_MIN_META_SEGMENTS 8 /* SB + 2 (CP + SIT + NAT) + SSA */
  17. /* L: Logical segment # in volume, R: Relative segment # in main area */
  18. #define GET_L2R_SEGNO(free_i, segno) ((segno) - (free_i)->start_segno)
  19. #define GET_R2L_SEGNO(free_i, segno) ((segno) + (free_i)->start_segno)
  20. #define IS_DATASEG(t) ((t) <= CURSEG_COLD_DATA)
  21. #define IS_NODESEG(t) ((t) >= CURSEG_HOT_NODE && (t) <= CURSEG_COLD_NODE)
  22. #define SE_PAGETYPE(se) ((IS_NODESEG((se)->type) ? NODE : DATA))
  23. static inline void sanity_check_seg_type(struct f2fs_sb_info *sbi,
  24. unsigned short seg_type)
  25. {
  26. f2fs_bug_on(sbi, seg_type >= NR_PERSISTENT_LOG);
  27. }
  28. #define IS_HOT(t) ((t) == CURSEG_HOT_NODE || (t) == CURSEG_HOT_DATA)
  29. #define IS_WARM(t) ((t) == CURSEG_WARM_NODE || (t) == CURSEG_WARM_DATA)
  30. #define IS_COLD(t) ((t) == CURSEG_COLD_NODE || (t) == CURSEG_COLD_DATA)
  31. #define IS_CURSEG(sbi, seg) \
  32. (((seg) == CURSEG_I(sbi, CURSEG_HOT_DATA)->segno) || \
  33. ((seg) == CURSEG_I(sbi, CURSEG_WARM_DATA)->segno) || \
  34. ((seg) == CURSEG_I(sbi, CURSEG_COLD_DATA)->segno) || \
  35. ((seg) == CURSEG_I(sbi, CURSEG_HOT_NODE)->segno) || \
  36. ((seg) == CURSEG_I(sbi, CURSEG_WARM_NODE)->segno) || \
  37. ((seg) == CURSEG_I(sbi, CURSEG_COLD_NODE)->segno) || \
  38. ((seg) == CURSEG_I(sbi, CURSEG_COLD_DATA_PINNED)->segno) || \
  39. ((seg) == CURSEG_I(sbi, CURSEG_ALL_DATA_ATGC)->segno))
  40. #define IS_CURSEC(sbi, secno) \
  41. (((secno) == CURSEG_I(sbi, CURSEG_HOT_DATA)->segno / \
  42. SEGS_PER_SEC(sbi)) || \
  43. ((secno) == CURSEG_I(sbi, CURSEG_WARM_DATA)->segno / \
  44. SEGS_PER_SEC(sbi)) || \
  45. ((secno) == CURSEG_I(sbi, CURSEG_COLD_DATA)->segno / \
  46. SEGS_PER_SEC(sbi)) || \
  47. ((secno) == CURSEG_I(sbi, CURSEG_HOT_NODE)->segno / \
  48. SEGS_PER_SEC(sbi)) || \
  49. ((secno) == CURSEG_I(sbi, CURSEG_WARM_NODE)->segno / \
  50. SEGS_PER_SEC(sbi)) || \
  51. ((secno) == CURSEG_I(sbi, CURSEG_COLD_NODE)->segno / \
  52. SEGS_PER_SEC(sbi)) || \
  53. ((secno) == CURSEG_I(sbi, CURSEG_COLD_DATA_PINNED)->segno / \
  54. SEGS_PER_SEC(sbi)) || \
  55. ((secno) == CURSEG_I(sbi, CURSEG_ALL_DATA_ATGC)->segno / \
  56. SEGS_PER_SEC(sbi)))
  57. #define MAIN_BLKADDR(sbi) \
  58. (SM_I(sbi) ? SM_I(sbi)->main_blkaddr : \
  59. le32_to_cpu(F2FS_RAW_SUPER(sbi)->main_blkaddr))
  60. #define SEG0_BLKADDR(sbi) \
  61. (SM_I(sbi) ? SM_I(sbi)->seg0_blkaddr : \
  62. le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment0_blkaddr))
  63. #define MAIN_SEGS(sbi) (SM_I(sbi)->main_segments)
  64. #define MAIN_SECS(sbi) ((sbi)->total_sections)
  65. #define TOTAL_SEGS(sbi) \
  66. (SM_I(sbi) ? SM_I(sbi)->segment_count : \
  67. le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count))
  68. #define TOTAL_BLKS(sbi) (SEGS_TO_BLKS(sbi, TOTAL_SEGS(sbi)))
  69. #define MAX_BLKADDR(sbi) (SEG0_BLKADDR(sbi) + TOTAL_BLKS(sbi))
  70. #define SEGMENT_SIZE(sbi) (1ULL << ((sbi)->log_blocksize + \
  71. (sbi)->log_blocks_per_seg))
  72. #define START_BLOCK(sbi, segno) (SEG0_BLKADDR(sbi) + \
  73. (SEGS_TO_BLKS(sbi, GET_R2L_SEGNO(FREE_I(sbi), segno))))
  74. #define NEXT_FREE_BLKADDR(sbi, curseg) \
  75. (START_BLOCK(sbi, (curseg)->segno) + (curseg)->next_blkoff)
  76. #define GET_SEGOFF_FROM_SEG0(sbi, blk_addr) ((blk_addr) - SEG0_BLKADDR(sbi))
  77. #define GET_SEGNO_FROM_SEG0(sbi, blk_addr) \
  78. (BLKS_TO_SEGS(sbi, GET_SEGOFF_FROM_SEG0(sbi, blk_addr)))
  79. #define GET_BLKOFF_FROM_SEG0(sbi, blk_addr) \
  80. (GET_SEGOFF_FROM_SEG0(sbi, blk_addr) & (BLKS_PER_SEG(sbi) - 1))
  81. #define GET_SEGNO(sbi, blk_addr) \
  82. ((!__is_valid_data_blkaddr(blk_addr)) ? \
  83. NULL_SEGNO : GET_L2R_SEGNO(FREE_I(sbi), \
  84. GET_SEGNO_FROM_SEG0(sbi, blk_addr)))
  85. #define CAP_BLKS_PER_SEC(sbi) \
  86. (BLKS_PER_SEC(sbi) - (sbi)->unusable_blocks_per_sec)
  87. #define CAP_SEGS_PER_SEC(sbi) \
  88. (SEGS_PER_SEC(sbi) - \
  89. BLKS_TO_SEGS(sbi, (sbi)->unusable_blocks_per_sec))
  90. #define GET_SEC_FROM_SEG(sbi, segno) \
  91. (((segno) == -1) ? -1 : (segno) / SEGS_PER_SEC(sbi))
  92. #define GET_SEG_FROM_SEC(sbi, secno) \
  93. ((secno) * SEGS_PER_SEC(sbi))
  94. #define GET_ZONE_FROM_SEC(sbi, secno) \
  95. (((secno) == -1) ? -1 : (secno) / (sbi)->secs_per_zone)
  96. #define GET_ZONE_FROM_SEG(sbi, segno) \
  97. GET_ZONE_FROM_SEC(sbi, GET_SEC_FROM_SEG(sbi, segno))
  98. #define GET_SUM_BLOCK(sbi, segno) \
  99. ((sbi)->sm_info->ssa_blkaddr + (segno))
  100. #define GET_SUM_TYPE(footer) ((footer)->entry_type)
  101. #define SET_SUM_TYPE(footer, type) ((footer)->entry_type = (type))
  102. #define SIT_ENTRY_OFFSET(sit_i, segno) \
  103. ((segno) % (sit_i)->sents_per_block)
  104. #define SIT_BLOCK_OFFSET(segno) \
  105. ((segno) / SIT_ENTRY_PER_BLOCK)
  106. #define START_SEGNO(segno) \
  107. (SIT_BLOCK_OFFSET(segno) * SIT_ENTRY_PER_BLOCK)
  108. #define SIT_BLK_CNT(sbi) \
  109. DIV_ROUND_UP(MAIN_SEGS(sbi), SIT_ENTRY_PER_BLOCK)
  110. #define f2fs_bitmap_size(nr) \
  111. (BITS_TO_LONGS(nr) * sizeof(unsigned long))
  112. #define SECTOR_FROM_BLOCK(blk_addr) \
  113. (((sector_t)blk_addr) << F2FS_LOG_SECTORS_PER_BLOCK)
  114. #define SECTOR_TO_BLOCK(sectors) \
  115. ((sectors) >> F2FS_LOG_SECTORS_PER_BLOCK)
  116. /*
  117. * In the victim_sel_policy->alloc_mode, there are three block allocation modes.
  118. * LFS writes data sequentially with cleaning operations.
  119. * SSR (Slack Space Recycle) reuses obsolete space without cleaning operations.
  120. * AT_SSR (Age Threshold based Slack Space Recycle) merges fragments into
  121. * fragmented segment which has similar aging degree.
  122. */
  123. enum {
  124. LFS = 0,
  125. SSR,
  126. AT_SSR,
  127. };
  128. /*
  129. * In the victim_sel_policy->gc_mode, there are three gc, aka cleaning, modes.
  130. * GC_CB is based on cost-benefit algorithm.
  131. * GC_GREEDY is based on greedy algorithm.
  132. * GC_AT is based on age-threshold algorithm.
  133. */
  134. enum {
  135. GC_CB = 0,
  136. GC_GREEDY,
  137. GC_AT,
  138. ALLOC_NEXT,
  139. FLUSH_DEVICE,
  140. MAX_GC_POLICY,
  141. };
  142. /*
  143. * BG_GC means the background cleaning job.
  144. * FG_GC means the on-demand cleaning job.
  145. */
  146. enum {
  147. BG_GC = 0,
  148. FG_GC,
  149. };
  150. /* for a function parameter to select a victim segment */
  151. struct victim_sel_policy {
  152. int alloc_mode; /* LFS or SSR */
  153. int gc_mode; /* GC_CB or GC_GREEDY */
  154. unsigned long *dirty_bitmap; /* dirty segment/section bitmap */
  155. unsigned int max_search; /*
  156. * maximum # of segments/sections
  157. * to search
  158. */
  159. unsigned int offset; /* last scanned bitmap offset */
  160. unsigned int ofs_unit; /* bitmap search unit */
  161. unsigned int min_cost; /* minimum cost */
  162. unsigned long long oldest_age; /* oldest age of segments having the same min cost */
  163. unsigned int min_segno; /* segment # having min. cost */
  164. unsigned long long age; /* mtime of GCed section*/
  165. unsigned long long age_threshold;/* age threshold */
  166. bool one_time_gc; /* one time GC */
  167. };
  168. struct seg_entry {
  169. unsigned int type:6; /* segment type like CURSEG_XXX_TYPE */
  170. unsigned int valid_blocks:10; /* # of valid blocks */
  171. unsigned int ckpt_valid_blocks:10; /* # of valid blocks last cp */
  172. unsigned int padding:6; /* padding */
  173. unsigned char *cur_valid_map; /* validity bitmap of blocks */
  174. #ifdef CONFIG_F2FS_CHECK_FS
  175. unsigned char *cur_valid_map_mir; /* mirror of current valid bitmap */
  176. #endif
  177. /*
  178. * # of valid blocks and the validity bitmap stored in the last
  179. * checkpoint pack. This information is used by the SSR mode.
  180. */
  181. unsigned char *ckpt_valid_map; /* validity bitmap of blocks last cp */
  182. unsigned char *discard_map;
  183. unsigned long long mtime; /* modification time of the segment */
  184. };
  185. struct sec_entry {
  186. unsigned int valid_blocks; /* # of valid blocks in a section */
  187. };
  188. #define MAX_SKIP_GC_COUNT 16
  189. struct revoke_entry {
  190. struct list_head list;
  191. block_t old_addr; /* for revoking when fail to commit */
  192. pgoff_t index;
  193. };
  194. struct sit_info {
  195. block_t sit_base_addr; /* start block address of SIT area */
  196. block_t sit_blocks; /* # of blocks used by SIT area */
  197. block_t written_valid_blocks; /* # of valid blocks in main area */
  198. char *bitmap; /* all bitmaps pointer */
  199. char *sit_bitmap; /* SIT bitmap pointer */
  200. #ifdef CONFIG_F2FS_CHECK_FS
  201. char *sit_bitmap_mir; /* SIT bitmap mirror */
  202. /* bitmap of segments to be ignored by GC in case of errors */
  203. unsigned long *invalid_segmap;
  204. #endif
  205. unsigned int bitmap_size; /* SIT bitmap size */
  206. unsigned long *tmp_map; /* bitmap for temporal use */
  207. unsigned long *dirty_sentries_bitmap; /* bitmap for dirty sentries */
  208. unsigned int dirty_sentries; /* # of dirty sentries */
  209. unsigned int sents_per_block; /* # of SIT entries per block */
  210. struct rw_semaphore sentry_lock; /* to protect SIT cache */
  211. struct seg_entry *sentries; /* SIT segment-level cache */
  212. struct sec_entry *sec_entries; /* SIT section-level cache */
  213. /* for cost-benefit algorithm in cleaning procedure */
  214. unsigned long long elapsed_time; /* elapsed time after mount */
  215. unsigned long long mounted_time; /* mount time */
  216. unsigned long long min_mtime; /* min. modification time */
  217. unsigned long long max_mtime; /* max. modification time */
  218. unsigned long long dirty_min_mtime; /* rerange candidates in GC_AT */
  219. unsigned long long dirty_max_mtime; /* rerange candidates in GC_AT */
  220. unsigned int last_victim[MAX_GC_POLICY]; /* last victim segment # */
  221. };
  222. struct free_segmap_info {
  223. unsigned int start_segno; /* start segment number logically */
  224. unsigned int free_segments; /* # of free segments */
  225. unsigned int free_sections; /* # of free sections */
  226. spinlock_t segmap_lock; /* free segmap lock */
  227. unsigned long *free_segmap; /* free segment bitmap */
  228. unsigned long *free_secmap; /* free section bitmap */
  229. };
  230. /* Notice: The order of dirty type is same with CURSEG_XXX in f2fs.h */
  231. enum dirty_type {
  232. DIRTY_HOT_DATA, /* dirty segments assigned as hot data logs */
  233. DIRTY_WARM_DATA, /* dirty segments assigned as warm data logs */
  234. DIRTY_COLD_DATA, /* dirty segments assigned as cold data logs */
  235. DIRTY_HOT_NODE, /* dirty segments assigned as hot node logs */
  236. DIRTY_WARM_NODE, /* dirty segments assigned as warm node logs */
  237. DIRTY_COLD_NODE, /* dirty segments assigned as cold node logs */
  238. DIRTY, /* to count # of dirty segments */
  239. PRE, /* to count # of entirely obsolete segments */
  240. NR_DIRTY_TYPE
  241. };
  242. struct dirty_seglist_info {
  243. unsigned long *dirty_segmap[NR_DIRTY_TYPE];
  244. unsigned long *dirty_secmap;
  245. struct mutex seglist_lock; /* lock for segment bitmaps */
  246. int nr_dirty[NR_DIRTY_TYPE]; /* # of dirty segments */
  247. unsigned long *victim_secmap; /* background GC victims */
  248. unsigned long *pinned_secmap; /* pinned victims from foreground GC */
  249. unsigned int pinned_secmap_cnt; /* count of victims which has pinned data */
  250. bool enable_pin_section; /* enable pinning section */
  251. };
  252. /* for active log information */
  253. struct curseg_info {
  254. struct mutex curseg_mutex; /* lock for consistency */
  255. struct f2fs_summary_block *sum_blk; /* cached summary block */
  256. struct rw_semaphore journal_rwsem; /* protect journal area */
  257. struct f2fs_journal *journal; /* cached journal info */
  258. unsigned char alloc_type; /* current allocation type */
  259. unsigned short seg_type; /* segment type like CURSEG_XXX_TYPE */
  260. unsigned int segno; /* current segment number */
  261. unsigned short next_blkoff; /* next block offset to write */
  262. unsigned int zone; /* current zone number */
  263. unsigned int next_segno; /* preallocated segment */
  264. int fragment_remained_chunk; /* remained block size in a chunk for block fragmentation mode */
  265. bool inited; /* indicate inmem log is inited */
  266. };
  267. struct sit_entry_set {
  268. struct list_head set_list; /* link with all sit sets */
  269. unsigned int start_segno; /* start segno of sits in set */
  270. unsigned int entry_cnt; /* the # of sit entries in set */
  271. };
  272. /*
  273. * inline functions
  274. */
  275. static inline struct curseg_info *CURSEG_I(struct f2fs_sb_info *sbi, int type)
  276. {
  277. return (struct curseg_info *)(SM_I(sbi)->curseg_array + type);
  278. }
  279. static inline struct seg_entry *get_seg_entry(struct f2fs_sb_info *sbi,
  280. unsigned int segno)
  281. {
  282. struct sit_info *sit_i = SIT_I(sbi);
  283. return &sit_i->sentries[segno];
  284. }
  285. static inline struct sec_entry *get_sec_entry(struct f2fs_sb_info *sbi,
  286. unsigned int segno)
  287. {
  288. struct sit_info *sit_i = SIT_I(sbi);
  289. return &sit_i->sec_entries[GET_SEC_FROM_SEG(sbi, segno)];
  290. }
  291. static inline unsigned int get_valid_blocks(struct f2fs_sb_info *sbi,
  292. unsigned int segno, bool use_section)
  293. {
  294. /*
  295. * In order to get # of valid blocks in a section instantly from many
  296. * segments, f2fs manages two counting structures separately.
  297. */
  298. if (use_section && __is_large_section(sbi))
  299. return get_sec_entry(sbi, segno)->valid_blocks;
  300. else
  301. return get_seg_entry(sbi, segno)->valid_blocks;
  302. }
  303. static inline unsigned int get_ckpt_valid_blocks(struct f2fs_sb_info *sbi,
  304. unsigned int segno, bool use_section)
  305. {
  306. if (use_section && __is_large_section(sbi)) {
  307. unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
  308. unsigned int start_segno = GET_SEG_FROM_SEC(sbi, secno);
  309. unsigned int blocks = 0;
  310. int i;
  311. for (i = 0; i < SEGS_PER_SEC(sbi); i++, start_segno++) {
  312. struct seg_entry *se = get_seg_entry(sbi, start_segno);
  313. blocks += se->ckpt_valid_blocks;
  314. }
  315. return blocks;
  316. }
  317. return get_seg_entry(sbi, segno)->ckpt_valid_blocks;
  318. }
  319. static inline void seg_info_from_raw_sit(struct seg_entry *se,
  320. struct f2fs_sit_entry *rs)
  321. {
  322. se->valid_blocks = GET_SIT_VBLOCKS(rs);
  323. se->ckpt_valid_blocks = GET_SIT_VBLOCKS(rs);
  324. memcpy(se->cur_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
  325. memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
  326. #ifdef CONFIG_F2FS_CHECK_FS
  327. memcpy(se->cur_valid_map_mir, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
  328. #endif
  329. se->type = GET_SIT_TYPE(rs);
  330. se->mtime = le64_to_cpu(rs->mtime);
  331. }
  332. static inline void __seg_info_to_raw_sit(struct seg_entry *se,
  333. struct f2fs_sit_entry *rs)
  334. {
  335. unsigned short raw_vblocks = (se->type << SIT_VBLOCKS_SHIFT) |
  336. se->valid_blocks;
  337. rs->vblocks = cpu_to_le16(raw_vblocks);
  338. memcpy(rs->valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
  339. rs->mtime = cpu_to_le64(se->mtime);
  340. }
  341. static inline void seg_info_to_sit_page(struct f2fs_sb_info *sbi,
  342. struct page *page, unsigned int start)
  343. {
  344. struct f2fs_sit_block *raw_sit;
  345. struct seg_entry *se;
  346. struct f2fs_sit_entry *rs;
  347. unsigned int end = min(start + SIT_ENTRY_PER_BLOCK,
  348. (unsigned long)MAIN_SEGS(sbi));
  349. int i;
  350. raw_sit = (struct f2fs_sit_block *)page_address(page);
  351. memset(raw_sit, 0, PAGE_SIZE);
  352. for (i = 0; i < end - start; i++) {
  353. rs = &raw_sit->entries[i];
  354. se = get_seg_entry(sbi, start + i);
  355. __seg_info_to_raw_sit(se, rs);
  356. }
  357. }
  358. static inline void seg_info_to_raw_sit(struct seg_entry *se,
  359. struct f2fs_sit_entry *rs)
  360. {
  361. __seg_info_to_raw_sit(se, rs);
  362. memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
  363. se->ckpt_valid_blocks = se->valid_blocks;
  364. }
  365. static inline unsigned int find_next_inuse(struct free_segmap_info *free_i,
  366. unsigned int max, unsigned int segno)
  367. {
  368. unsigned int ret;
  369. spin_lock(&free_i->segmap_lock);
  370. ret = find_next_bit(free_i->free_segmap, max, segno);
  371. spin_unlock(&free_i->segmap_lock);
  372. return ret;
  373. }
  374. static inline void __set_free(struct f2fs_sb_info *sbi, unsigned int segno)
  375. {
  376. struct free_segmap_info *free_i = FREE_I(sbi);
  377. unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
  378. unsigned int start_segno = GET_SEG_FROM_SEC(sbi, secno);
  379. unsigned int next;
  380. spin_lock(&free_i->segmap_lock);
  381. clear_bit(segno, free_i->free_segmap);
  382. free_i->free_segments++;
  383. next = find_next_bit(free_i->free_segmap,
  384. start_segno + SEGS_PER_SEC(sbi), start_segno);
  385. if (next >= start_segno + f2fs_usable_segs_in_sec(sbi)) {
  386. clear_bit(secno, free_i->free_secmap);
  387. free_i->free_sections++;
  388. }
  389. spin_unlock(&free_i->segmap_lock);
  390. }
  391. static inline void __set_inuse(struct f2fs_sb_info *sbi,
  392. unsigned int segno)
  393. {
  394. struct free_segmap_info *free_i = FREE_I(sbi);
  395. unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
  396. set_bit(segno, free_i->free_segmap);
  397. free_i->free_segments--;
  398. if (!test_and_set_bit(secno, free_i->free_secmap))
  399. free_i->free_sections--;
  400. }
  401. static inline void __set_test_and_free(struct f2fs_sb_info *sbi,
  402. unsigned int segno, bool inmem)
  403. {
  404. struct free_segmap_info *free_i = FREE_I(sbi);
  405. unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
  406. unsigned int start_segno = GET_SEG_FROM_SEC(sbi, secno);
  407. unsigned int next;
  408. bool ret;
  409. spin_lock(&free_i->segmap_lock);
  410. ret = test_and_clear_bit(segno, free_i->free_segmap);
  411. if (!ret)
  412. goto unlock_out;
  413. free_i->free_segments++;
  414. if (!inmem && IS_CURSEC(sbi, secno))
  415. goto unlock_out;
  416. /* check large section */
  417. next = find_next_bit(free_i->free_segmap,
  418. start_segno + SEGS_PER_SEC(sbi), start_segno);
  419. if (next < start_segno + f2fs_usable_segs_in_sec(sbi))
  420. goto unlock_out;
  421. ret = test_and_clear_bit(secno, free_i->free_secmap);
  422. if (!ret)
  423. goto unlock_out;
  424. free_i->free_sections++;
  425. if (GET_SEC_FROM_SEG(sbi, sbi->next_victim_seg[BG_GC]) == secno)
  426. sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
  427. if (GET_SEC_FROM_SEG(sbi, sbi->next_victim_seg[FG_GC]) == secno)
  428. sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
  429. unlock_out:
  430. spin_unlock(&free_i->segmap_lock);
  431. }
  432. static inline void __set_test_and_inuse(struct f2fs_sb_info *sbi,
  433. unsigned int segno)
  434. {
  435. struct free_segmap_info *free_i = FREE_I(sbi);
  436. unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
  437. spin_lock(&free_i->segmap_lock);
  438. if (!test_and_set_bit(segno, free_i->free_segmap)) {
  439. free_i->free_segments--;
  440. if (!test_and_set_bit(secno, free_i->free_secmap))
  441. free_i->free_sections--;
  442. }
  443. spin_unlock(&free_i->segmap_lock);
  444. }
  445. static inline void get_sit_bitmap(struct f2fs_sb_info *sbi,
  446. void *dst_addr)
  447. {
  448. struct sit_info *sit_i = SIT_I(sbi);
  449. #ifdef CONFIG_F2FS_CHECK_FS
  450. if (memcmp(sit_i->sit_bitmap, sit_i->sit_bitmap_mir,
  451. sit_i->bitmap_size))
  452. f2fs_bug_on(sbi, 1);
  453. #endif
  454. memcpy(dst_addr, sit_i->sit_bitmap, sit_i->bitmap_size);
  455. }
  456. static inline block_t written_block_count(struct f2fs_sb_info *sbi)
  457. {
  458. return SIT_I(sbi)->written_valid_blocks;
  459. }
  460. static inline unsigned int free_segments(struct f2fs_sb_info *sbi)
  461. {
  462. return FREE_I(sbi)->free_segments;
  463. }
  464. static inline unsigned int reserved_segments(struct f2fs_sb_info *sbi)
  465. {
  466. return SM_I(sbi)->reserved_segments +
  467. SM_I(sbi)->additional_reserved_segments;
  468. }
  469. static inline unsigned int free_sections(struct f2fs_sb_info *sbi)
  470. {
  471. return FREE_I(sbi)->free_sections;
  472. }
  473. static inline unsigned int prefree_segments(struct f2fs_sb_info *sbi)
  474. {
  475. return DIRTY_I(sbi)->nr_dirty[PRE];
  476. }
  477. static inline unsigned int dirty_segments(struct f2fs_sb_info *sbi)
  478. {
  479. return DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_DATA] +
  480. DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_DATA] +
  481. DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_DATA] +
  482. DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_NODE] +
  483. DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_NODE] +
  484. DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_NODE];
  485. }
  486. static inline int overprovision_segments(struct f2fs_sb_info *sbi)
  487. {
  488. return SM_I(sbi)->ovp_segments;
  489. }
  490. static inline int reserved_sections(struct f2fs_sb_info *sbi)
  491. {
  492. return GET_SEC_FROM_SEG(sbi, reserved_segments(sbi));
  493. }
  494. static inline bool has_curseg_enough_space(struct f2fs_sb_info *sbi,
  495. unsigned int node_blocks, unsigned int data_blocks,
  496. unsigned int dent_blocks)
  497. {
  498. unsigned int segno, left_blocks, blocks;
  499. int i;
  500. /* check current data/node sections in the worst case. */
  501. for (i = CURSEG_HOT_DATA; i < NR_PERSISTENT_LOG; i++) {
  502. segno = CURSEG_I(sbi, i)->segno;
  503. if (unlikely(segno == NULL_SEGNO))
  504. return false;
  505. left_blocks = CAP_BLKS_PER_SEC(sbi) -
  506. get_ckpt_valid_blocks(sbi, segno, true);
  507. blocks = i <= CURSEG_COLD_DATA ? data_blocks : node_blocks;
  508. if (blocks > left_blocks)
  509. return false;
  510. }
  511. /* check current data section for dentry blocks. */
  512. segno = CURSEG_I(sbi, CURSEG_HOT_DATA)->segno;
  513. if (unlikely(segno == NULL_SEGNO))
  514. return false;
  515. left_blocks = CAP_BLKS_PER_SEC(sbi) -
  516. get_ckpt_valid_blocks(sbi, segno, true);
  517. if (dent_blocks > left_blocks)
  518. return false;
  519. return true;
  520. }
  521. /*
  522. * calculate needed sections for dirty node/dentry and call
  523. * has_curseg_enough_space, please note that, it needs to account
  524. * dirty data as well in lfs mode when checkpoint is disabled.
  525. */
  526. static inline void __get_secs_required(struct f2fs_sb_info *sbi,
  527. unsigned int *lower_p, unsigned int *upper_p, bool *curseg_p)
  528. {
  529. unsigned int total_node_blocks = get_pages(sbi, F2FS_DIRTY_NODES) +
  530. get_pages(sbi, F2FS_DIRTY_DENTS) +
  531. get_pages(sbi, F2FS_DIRTY_IMETA);
  532. unsigned int total_dent_blocks = get_pages(sbi, F2FS_DIRTY_DENTS);
  533. unsigned int total_data_blocks = 0;
  534. unsigned int node_secs = total_node_blocks / CAP_BLKS_PER_SEC(sbi);
  535. unsigned int dent_secs = total_dent_blocks / CAP_BLKS_PER_SEC(sbi);
  536. unsigned int data_secs = 0;
  537. unsigned int node_blocks = total_node_blocks % CAP_BLKS_PER_SEC(sbi);
  538. unsigned int dent_blocks = total_dent_blocks % CAP_BLKS_PER_SEC(sbi);
  539. unsigned int data_blocks = 0;
  540. if (f2fs_lfs_mode(sbi)) {
  541. total_data_blocks = get_pages(sbi, F2FS_DIRTY_DATA);
  542. data_secs = total_data_blocks / CAP_BLKS_PER_SEC(sbi);
  543. data_blocks = total_data_blocks % CAP_BLKS_PER_SEC(sbi);
  544. }
  545. if (lower_p)
  546. *lower_p = node_secs + dent_secs + data_secs;
  547. if (upper_p)
  548. *upper_p = node_secs + dent_secs + data_secs +
  549. (node_blocks ? 1 : 0) + (dent_blocks ? 1 : 0) +
  550. (data_blocks ? 1 : 0);
  551. if (curseg_p)
  552. *curseg_p = has_curseg_enough_space(sbi,
  553. node_blocks, data_blocks, dent_blocks);
  554. }
  555. static inline bool has_not_enough_free_secs(struct f2fs_sb_info *sbi,
  556. int freed, int needed)
  557. {
  558. unsigned int free_secs, lower_secs, upper_secs;
  559. bool curseg_space;
  560. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  561. return false;
  562. __get_secs_required(sbi, &lower_secs, &upper_secs, &curseg_space);
  563. free_secs = free_sections(sbi) + freed;
  564. lower_secs += needed + reserved_sections(sbi);
  565. upper_secs += needed + reserved_sections(sbi);
  566. if (free_secs > upper_secs)
  567. return false;
  568. if (free_secs <= lower_secs)
  569. return true;
  570. return !curseg_space;
  571. }
  572. static inline bool has_enough_free_secs(struct f2fs_sb_info *sbi,
  573. int freed, int needed)
  574. {
  575. return !has_not_enough_free_secs(sbi, freed, needed);
  576. }
  577. static inline bool f2fs_is_checkpoint_ready(struct f2fs_sb_info *sbi)
  578. {
  579. if (likely(!is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
  580. return true;
  581. if (likely(has_enough_free_secs(sbi, 0, 0)))
  582. return true;
  583. return false;
  584. }
  585. static inline bool excess_prefree_segs(struct f2fs_sb_info *sbi)
  586. {
  587. return prefree_segments(sbi) > SM_I(sbi)->rec_prefree_segments;
  588. }
  589. static inline int utilization(struct f2fs_sb_info *sbi)
  590. {
  591. return div_u64((u64)valid_user_blocks(sbi) * 100,
  592. sbi->user_block_count);
  593. }
  594. /*
  595. * Sometimes f2fs may be better to drop out-of-place update policy.
  596. * And, users can control the policy through sysfs entries.
  597. * There are five policies with triggering conditions as follows.
  598. * F2FS_IPU_FORCE - all the time,
  599. * F2FS_IPU_SSR - if SSR mode is activated,
  600. * F2FS_IPU_UTIL - if FS utilization is over threashold,
  601. * F2FS_IPU_SSR_UTIL - if SSR mode is activated and FS utilization is over
  602. * threashold,
  603. * F2FS_IPU_FSYNC - activated in fsync path only for high performance flash
  604. * storages. IPU will be triggered only if the # of dirty
  605. * pages over min_fsync_blocks. (=default option)
  606. * F2FS_IPU_ASYNC - do IPU given by asynchronous write requests.
  607. * F2FS_IPU_NOCACHE - disable IPU bio cache.
  608. * F2FS_IPU_HONOR_OPU_WRITE - use OPU write prior to IPU write if inode has
  609. * FI_OPU_WRITE flag.
  610. * F2FS_IPU_DISABLE - disable IPU. (=default option in LFS mode)
  611. */
  612. #define DEF_MIN_IPU_UTIL 70
  613. #define DEF_MIN_FSYNC_BLOCKS 8
  614. #define DEF_MIN_HOT_BLOCKS 16
  615. #define SMALL_VOLUME_SEGMENTS (16 * 512) /* 16GB */
  616. #define F2FS_IPU_DISABLE 0
  617. /* Modification on enum should be synchronized with ipu_mode_names array */
  618. enum {
  619. F2FS_IPU_FORCE,
  620. F2FS_IPU_SSR,
  621. F2FS_IPU_UTIL,
  622. F2FS_IPU_SSR_UTIL,
  623. F2FS_IPU_FSYNC,
  624. F2FS_IPU_ASYNC,
  625. F2FS_IPU_NOCACHE,
  626. F2FS_IPU_HONOR_OPU_WRITE,
  627. F2FS_IPU_MAX,
  628. };
  629. static inline bool IS_F2FS_IPU_DISABLE(struct f2fs_sb_info *sbi)
  630. {
  631. return SM_I(sbi)->ipu_policy == F2FS_IPU_DISABLE;
  632. }
  633. #define F2FS_IPU_POLICY(name) \
  634. static inline bool IS_##name(struct f2fs_sb_info *sbi) \
  635. { \
  636. return SM_I(sbi)->ipu_policy & BIT(name); \
  637. }
  638. F2FS_IPU_POLICY(F2FS_IPU_FORCE);
  639. F2FS_IPU_POLICY(F2FS_IPU_SSR);
  640. F2FS_IPU_POLICY(F2FS_IPU_UTIL);
  641. F2FS_IPU_POLICY(F2FS_IPU_SSR_UTIL);
  642. F2FS_IPU_POLICY(F2FS_IPU_FSYNC);
  643. F2FS_IPU_POLICY(F2FS_IPU_ASYNC);
  644. F2FS_IPU_POLICY(F2FS_IPU_NOCACHE);
  645. F2FS_IPU_POLICY(F2FS_IPU_HONOR_OPU_WRITE);
  646. static inline unsigned int curseg_segno(struct f2fs_sb_info *sbi,
  647. int type)
  648. {
  649. struct curseg_info *curseg = CURSEG_I(sbi, type);
  650. return curseg->segno;
  651. }
  652. static inline unsigned char curseg_alloc_type(struct f2fs_sb_info *sbi,
  653. int type)
  654. {
  655. struct curseg_info *curseg = CURSEG_I(sbi, type);
  656. return curseg->alloc_type;
  657. }
  658. static inline bool valid_main_segno(struct f2fs_sb_info *sbi,
  659. unsigned int segno)
  660. {
  661. return segno <= (MAIN_SEGS(sbi) - 1);
  662. }
  663. static inline void verify_fio_blkaddr(struct f2fs_io_info *fio)
  664. {
  665. struct f2fs_sb_info *sbi = fio->sbi;
  666. if (__is_valid_data_blkaddr(fio->old_blkaddr))
  667. verify_blkaddr(sbi, fio->old_blkaddr, __is_meta_io(fio) ?
  668. META_GENERIC : DATA_GENERIC);
  669. verify_blkaddr(sbi, fio->new_blkaddr, __is_meta_io(fio) ?
  670. META_GENERIC : DATA_GENERIC_ENHANCE);
  671. }
  672. /*
  673. * Summary block is always treated as an invalid block
  674. */
  675. static inline int check_block_count(struct f2fs_sb_info *sbi,
  676. int segno, struct f2fs_sit_entry *raw_sit)
  677. {
  678. bool is_valid = test_bit_le(0, raw_sit->valid_map) ? true : false;
  679. int valid_blocks = 0;
  680. int cur_pos = 0, next_pos;
  681. unsigned int usable_blks_per_seg = f2fs_usable_blks_in_seg(sbi, segno);
  682. /* check bitmap with valid block count */
  683. do {
  684. if (is_valid) {
  685. next_pos = find_next_zero_bit_le(&raw_sit->valid_map,
  686. usable_blks_per_seg,
  687. cur_pos);
  688. valid_blocks += next_pos - cur_pos;
  689. } else
  690. next_pos = find_next_bit_le(&raw_sit->valid_map,
  691. usable_blks_per_seg,
  692. cur_pos);
  693. cur_pos = next_pos;
  694. is_valid = !is_valid;
  695. } while (cur_pos < usable_blks_per_seg);
  696. if (unlikely(GET_SIT_VBLOCKS(raw_sit) != valid_blocks)) {
  697. f2fs_err(sbi, "Mismatch valid blocks %d vs. %d",
  698. GET_SIT_VBLOCKS(raw_sit), valid_blocks);
  699. set_sbi_flag(sbi, SBI_NEED_FSCK);
  700. f2fs_handle_error(sbi, ERROR_INCONSISTENT_SIT);
  701. return -EFSCORRUPTED;
  702. }
  703. if (usable_blks_per_seg < BLKS_PER_SEG(sbi))
  704. f2fs_bug_on(sbi, find_next_bit_le(&raw_sit->valid_map,
  705. BLKS_PER_SEG(sbi),
  706. usable_blks_per_seg) != BLKS_PER_SEG(sbi));
  707. /* check segment usage, and check boundary of a given segment number */
  708. if (unlikely(GET_SIT_VBLOCKS(raw_sit) > usable_blks_per_seg
  709. || !valid_main_segno(sbi, segno))) {
  710. f2fs_err(sbi, "Wrong valid blocks %d or segno %u",
  711. GET_SIT_VBLOCKS(raw_sit), segno);
  712. set_sbi_flag(sbi, SBI_NEED_FSCK);
  713. f2fs_handle_error(sbi, ERROR_INCONSISTENT_SIT);
  714. return -EFSCORRUPTED;
  715. }
  716. return 0;
  717. }
  718. static inline pgoff_t current_sit_addr(struct f2fs_sb_info *sbi,
  719. unsigned int start)
  720. {
  721. struct sit_info *sit_i = SIT_I(sbi);
  722. unsigned int offset = SIT_BLOCK_OFFSET(start);
  723. block_t blk_addr = sit_i->sit_base_addr + offset;
  724. f2fs_bug_on(sbi, !valid_main_segno(sbi, start));
  725. #ifdef CONFIG_F2FS_CHECK_FS
  726. if (f2fs_test_bit(offset, sit_i->sit_bitmap) !=
  727. f2fs_test_bit(offset, sit_i->sit_bitmap_mir))
  728. f2fs_bug_on(sbi, 1);
  729. #endif
  730. /* calculate sit block address */
  731. if (f2fs_test_bit(offset, sit_i->sit_bitmap))
  732. blk_addr += sit_i->sit_blocks;
  733. return blk_addr;
  734. }
  735. static inline pgoff_t next_sit_addr(struct f2fs_sb_info *sbi,
  736. pgoff_t block_addr)
  737. {
  738. struct sit_info *sit_i = SIT_I(sbi);
  739. block_addr -= sit_i->sit_base_addr;
  740. if (block_addr < sit_i->sit_blocks)
  741. block_addr += sit_i->sit_blocks;
  742. else
  743. block_addr -= sit_i->sit_blocks;
  744. return block_addr + sit_i->sit_base_addr;
  745. }
  746. static inline void set_to_next_sit(struct sit_info *sit_i, unsigned int start)
  747. {
  748. unsigned int block_off = SIT_BLOCK_OFFSET(start);
  749. f2fs_change_bit(block_off, sit_i->sit_bitmap);
  750. #ifdef CONFIG_F2FS_CHECK_FS
  751. f2fs_change_bit(block_off, sit_i->sit_bitmap_mir);
  752. #endif
  753. }
  754. static inline unsigned long long get_mtime(struct f2fs_sb_info *sbi,
  755. bool base_time)
  756. {
  757. struct sit_info *sit_i = SIT_I(sbi);
  758. time64_t diff, now = ktime_get_boottime_seconds();
  759. if (now >= sit_i->mounted_time)
  760. return sit_i->elapsed_time + now - sit_i->mounted_time;
  761. /* system time is set to the past */
  762. if (!base_time) {
  763. diff = sit_i->mounted_time - now;
  764. if (sit_i->elapsed_time >= diff)
  765. return sit_i->elapsed_time - diff;
  766. return 0;
  767. }
  768. return sit_i->elapsed_time;
  769. }
  770. static inline void set_summary(struct f2fs_summary *sum, nid_t nid,
  771. unsigned int ofs_in_node, unsigned char version)
  772. {
  773. sum->nid = cpu_to_le32(nid);
  774. sum->ofs_in_node = cpu_to_le16(ofs_in_node);
  775. sum->version = version;
  776. }
  777. static inline block_t start_sum_block(struct f2fs_sb_info *sbi)
  778. {
  779. return __start_cp_addr(sbi) +
  780. le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
  781. }
  782. static inline block_t sum_blk_addr(struct f2fs_sb_info *sbi, int base, int type)
  783. {
  784. return __start_cp_addr(sbi) +
  785. le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_total_block_count)
  786. - (base + 1) + type;
  787. }
  788. static inline bool sec_usage_check(struct f2fs_sb_info *sbi, unsigned int secno)
  789. {
  790. if (IS_CURSEC(sbi, secno) || (sbi->cur_victim_sec == secno))
  791. return true;
  792. return false;
  793. }
  794. /*
  795. * It is very important to gather dirty pages and write at once, so that we can
  796. * submit a big bio without interfering other data writes.
  797. * By default, 512 pages for directory data,
  798. * 512 pages (2MB) * 8 for nodes, and
  799. * 256 pages * 8 for meta are set.
  800. */
  801. static inline int nr_pages_to_skip(struct f2fs_sb_info *sbi, int type)
  802. {
  803. if (sbi->sb->s_bdi->wb.dirty_exceeded)
  804. return 0;
  805. if (type == DATA)
  806. return BLKS_PER_SEG(sbi);
  807. else if (type == NODE)
  808. return SEGS_TO_BLKS(sbi, 8);
  809. else if (type == META)
  810. return 8 * BIO_MAX_VECS;
  811. else
  812. return 0;
  813. }
  814. /*
  815. * When writing pages, it'd better align nr_to_write for segment size.
  816. */
  817. static inline long nr_pages_to_write(struct f2fs_sb_info *sbi, int type,
  818. struct writeback_control *wbc)
  819. {
  820. long nr_to_write, desired;
  821. if (wbc->sync_mode != WB_SYNC_NONE)
  822. return 0;
  823. nr_to_write = wbc->nr_to_write;
  824. desired = BIO_MAX_VECS;
  825. if (type == NODE)
  826. desired <<= 1;
  827. wbc->nr_to_write = desired;
  828. return desired - nr_to_write;
  829. }
  830. static inline void wake_up_discard_thread(struct f2fs_sb_info *sbi, bool force)
  831. {
  832. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  833. bool wakeup = false;
  834. int i;
  835. if (force)
  836. goto wake_up;
  837. mutex_lock(&dcc->cmd_lock);
  838. for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
  839. if (i + 1 < dcc->discard_granularity)
  840. break;
  841. if (!list_empty(&dcc->pend_list[i])) {
  842. wakeup = true;
  843. break;
  844. }
  845. }
  846. mutex_unlock(&dcc->cmd_lock);
  847. if (!wakeup || !is_idle(sbi, DISCARD_TIME))
  848. return;
  849. wake_up:
  850. dcc->discard_wake = true;
  851. wake_up_interruptible_all(&dcc->discard_wait_queue);
  852. }