segment.h 25 KB

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