segment.h 30 KB

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