segment.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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. rs->mtime = cpu_to_le64(se->mtime);
  311. }
  312. static inline void seg_info_to_sit_page(struct f2fs_sb_info *sbi,
  313. struct page *page, unsigned int start)
  314. {
  315. struct f2fs_sit_block *raw_sit;
  316. struct seg_entry *se;
  317. struct f2fs_sit_entry *rs;
  318. unsigned int end = min(start + SIT_ENTRY_PER_BLOCK,
  319. (unsigned long)MAIN_SEGS(sbi));
  320. int i;
  321. raw_sit = (struct f2fs_sit_block *)page_address(page);
  322. for (i = 0; i < end - start; i++) {
  323. rs = &raw_sit->entries[i];
  324. se = get_seg_entry(sbi, start + i);
  325. __seg_info_to_raw_sit(se, rs);
  326. }
  327. }
  328. static inline void seg_info_to_raw_sit(struct seg_entry *se,
  329. struct f2fs_sit_entry *rs)
  330. {
  331. __seg_info_to_raw_sit(se, rs);
  332. memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
  333. se->ckpt_valid_blocks = se->valid_blocks;
  334. }
  335. static inline unsigned int find_next_inuse(struct free_segmap_info *free_i,
  336. unsigned int max, unsigned int segno)
  337. {
  338. unsigned int ret;
  339. spin_lock(&free_i->segmap_lock);
  340. ret = find_next_bit(free_i->free_segmap, max, segno);
  341. spin_unlock(&free_i->segmap_lock);
  342. return ret;
  343. }
  344. static inline void __set_free(struct f2fs_sb_info *sbi, unsigned int segno)
  345. {
  346. struct free_segmap_info *free_i = FREE_I(sbi);
  347. unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
  348. unsigned int start_segno = GET_SEG_FROM_SEC(sbi, secno);
  349. unsigned int next;
  350. spin_lock(&free_i->segmap_lock);
  351. clear_bit(segno, free_i->free_segmap);
  352. free_i->free_segments++;
  353. next = find_next_bit(free_i->free_segmap,
  354. start_segno + sbi->segs_per_sec, start_segno);
  355. if (next >= start_segno + sbi->segs_per_sec) {
  356. clear_bit(secno, free_i->free_secmap);
  357. free_i->free_sections++;
  358. }
  359. spin_unlock(&free_i->segmap_lock);
  360. }
  361. static inline void __set_inuse(struct f2fs_sb_info *sbi,
  362. unsigned int segno)
  363. {
  364. struct free_segmap_info *free_i = FREE_I(sbi);
  365. unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
  366. set_bit(segno, free_i->free_segmap);
  367. free_i->free_segments--;
  368. if (!test_and_set_bit(secno, free_i->free_secmap))
  369. free_i->free_sections--;
  370. }
  371. static inline void __set_test_and_free(struct f2fs_sb_info *sbi,
  372. unsigned int segno)
  373. {
  374. struct free_segmap_info *free_i = FREE_I(sbi);
  375. unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
  376. unsigned int start_segno = GET_SEG_FROM_SEC(sbi, secno);
  377. unsigned int next;
  378. spin_lock(&free_i->segmap_lock);
  379. if (test_and_clear_bit(segno, free_i->free_segmap)) {
  380. free_i->free_segments++;
  381. next = find_next_bit(free_i->free_segmap,
  382. start_segno + sbi->segs_per_sec, start_segno);
  383. if (next >= start_segno + sbi->segs_per_sec) {
  384. if (test_and_clear_bit(secno, free_i->free_secmap))
  385. free_i->free_sections++;
  386. }
  387. }
  388. spin_unlock(&free_i->segmap_lock);
  389. }
  390. static inline void __set_test_and_inuse(struct f2fs_sb_info *sbi,
  391. unsigned int segno)
  392. {
  393. struct free_segmap_info *free_i = FREE_I(sbi);
  394. unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
  395. spin_lock(&free_i->segmap_lock);
  396. if (!test_and_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. spin_unlock(&free_i->segmap_lock);
  402. }
  403. static inline void get_sit_bitmap(struct f2fs_sb_info *sbi,
  404. void *dst_addr)
  405. {
  406. struct sit_info *sit_i = SIT_I(sbi);
  407. #ifdef CONFIG_F2FS_CHECK_FS
  408. if (memcmp(sit_i->sit_bitmap, sit_i->sit_bitmap_mir,
  409. sit_i->bitmap_size))
  410. f2fs_bug_on(sbi, 1);
  411. #endif
  412. memcpy(dst_addr, sit_i->sit_bitmap, sit_i->bitmap_size);
  413. }
  414. static inline block_t written_block_count(struct f2fs_sb_info *sbi)
  415. {
  416. return SIT_I(sbi)->written_valid_blocks;
  417. }
  418. static inline unsigned int free_segments(struct f2fs_sb_info *sbi)
  419. {
  420. return FREE_I(sbi)->free_segments;
  421. }
  422. static inline int reserved_segments(struct f2fs_sb_info *sbi)
  423. {
  424. return SM_I(sbi)->reserved_segments;
  425. }
  426. static inline unsigned int free_sections(struct f2fs_sb_info *sbi)
  427. {
  428. return FREE_I(sbi)->free_sections;
  429. }
  430. static inline unsigned int prefree_segments(struct f2fs_sb_info *sbi)
  431. {
  432. return DIRTY_I(sbi)->nr_dirty[PRE];
  433. }
  434. static inline unsigned int dirty_segments(struct f2fs_sb_info *sbi)
  435. {
  436. return DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_DATA] +
  437. DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_DATA] +
  438. DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_DATA] +
  439. DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_NODE] +
  440. DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_NODE] +
  441. DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_NODE];
  442. }
  443. static inline int overprovision_segments(struct f2fs_sb_info *sbi)
  444. {
  445. return SM_I(sbi)->ovp_segments;
  446. }
  447. static inline int reserved_sections(struct f2fs_sb_info *sbi)
  448. {
  449. return GET_SEC_FROM_SEG(sbi, (unsigned int)reserved_segments(sbi));
  450. }
  451. static inline bool has_curseg_enough_space(struct f2fs_sb_info *sbi)
  452. {
  453. unsigned int node_blocks = get_pages(sbi, F2FS_DIRTY_NODES) +
  454. get_pages(sbi, F2FS_DIRTY_DENTS);
  455. unsigned int dent_blocks = get_pages(sbi, F2FS_DIRTY_DENTS);
  456. unsigned int segno, left_blocks;
  457. int i;
  458. /* check current node segment */
  459. for (i = CURSEG_HOT_NODE; i <= CURSEG_COLD_NODE; i++) {
  460. segno = CURSEG_I(sbi, i)->segno;
  461. left_blocks = sbi->blocks_per_seg -
  462. get_seg_entry(sbi, segno)->ckpt_valid_blocks;
  463. if (node_blocks > left_blocks)
  464. return false;
  465. }
  466. /* check current data segment */
  467. segno = CURSEG_I(sbi, CURSEG_HOT_DATA)->segno;
  468. left_blocks = sbi->blocks_per_seg -
  469. get_seg_entry(sbi, segno)->ckpt_valid_blocks;
  470. if (dent_blocks > left_blocks)
  471. return false;
  472. return true;
  473. }
  474. static inline bool has_not_enough_free_secs(struct f2fs_sb_info *sbi,
  475. int freed, int needed)
  476. {
  477. int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
  478. int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
  479. int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA);
  480. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  481. return false;
  482. if (free_sections(sbi) + freed == reserved_sections(sbi) + needed &&
  483. has_curseg_enough_space(sbi))
  484. return false;
  485. return (free_sections(sbi) + freed) <=
  486. (node_secs + 2 * dent_secs + imeta_secs +
  487. reserved_sections(sbi) + needed);
  488. }
  489. static inline bool excess_prefree_segs(struct f2fs_sb_info *sbi)
  490. {
  491. return prefree_segments(sbi) > SM_I(sbi)->rec_prefree_segments;
  492. }
  493. static inline int utilization(struct f2fs_sb_info *sbi)
  494. {
  495. return div_u64((u64)valid_user_blocks(sbi) * 100,
  496. sbi->user_block_count);
  497. }
  498. /*
  499. * Sometimes f2fs may be better to drop out-of-place update policy.
  500. * And, users can control the policy through sysfs entries.
  501. * There are five policies with triggering conditions as follows.
  502. * F2FS_IPU_FORCE - all the time,
  503. * F2FS_IPU_SSR - if SSR mode is activated,
  504. * F2FS_IPU_UTIL - if FS utilization is over threashold,
  505. * F2FS_IPU_SSR_UTIL - if SSR mode is activated and FS utilization is over
  506. * threashold,
  507. * F2FS_IPU_FSYNC - activated in fsync path only for high performance flash
  508. * storages. IPU will be triggered only if the # of dirty
  509. * pages over min_fsync_blocks.
  510. * F2FS_IPUT_DISABLE - disable IPU. (=default option)
  511. */
  512. #define DEF_MIN_IPU_UTIL 70
  513. #define DEF_MIN_FSYNC_BLOCKS 8
  514. #define DEF_MIN_HOT_BLOCKS 16
  515. enum {
  516. F2FS_IPU_FORCE,
  517. F2FS_IPU_SSR,
  518. F2FS_IPU_UTIL,
  519. F2FS_IPU_SSR_UTIL,
  520. F2FS_IPU_FSYNC,
  521. F2FS_IPU_ASYNC,
  522. };
  523. static inline unsigned int curseg_segno(struct f2fs_sb_info *sbi,
  524. int type)
  525. {
  526. struct curseg_info *curseg = CURSEG_I(sbi, type);
  527. return curseg->segno;
  528. }
  529. static inline unsigned char curseg_alloc_type(struct f2fs_sb_info *sbi,
  530. int type)
  531. {
  532. struct curseg_info *curseg = CURSEG_I(sbi, type);
  533. return curseg->alloc_type;
  534. }
  535. static inline unsigned short curseg_blkoff(struct f2fs_sb_info *sbi, int type)
  536. {
  537. struct curseg_info *curseg = CURSEG_I(sbi, type);
  538. return curseg->next_blkoff;
  539. }
  540. static inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno)
  541. {
  542. f2fs_bug_on(sbi, segno > TOTAL_SEGS(sbi) - 1);
  543. }
  544. static inline void verify_block_addr(struct f2fs_sb_info *sbi, block_t blk_addr)
  545. {
  546. BUG_ON(blk_addr < SEG0_BLKADDR(sbi)
  547. || blk_addr >= MAX_BLKADDR(sbi));
  548. }
  549. /*
  550. * Summary block is always treated as an invalid block
  551. */
  552. static inline int check_block_count(struct f2fs_sb_info *sbi,
  553. int segno, struct f2fs_sit_entry *raw_sit)
  554. {
  555. #ifdef CONFIG_F2FS_CHECK_FS
  556. bool is_valid = test_bit_le(0, raw_sit->valid_map) ? true : false;
  557. int valid_blocks = 0;
  558. int cur_pos = 0, next_pos;
  559. /* check bitmap with valid block count */
  560. do {
  561. if (is_valid) {
  562. next_pos = find_next_zero_bit_le(&raw_sit->valid_map,
  563. sbi->blocks_per_seg,
  564. cur_pos);
  565. valid_blocks += next_pos - cur_pos;
  566. } else
  567. next_pos = find_next_bit_le(&raw_sit->valid_map,
  568. sbi->blocks_per_seg,
  569. cur_pos);
  570. cur_pos = next_pos;
  571. is_valid = !is_valid;
  572. } while (cur_pos < sbi->blocks_per_seg);
  573. if (unlikely(GET_SIT_VBLOCKS(raw_sit) != valid_blocks)) {
  574. f2fs_msg(sbi->sb, KERN_ERR,
  575. "Mismatch valid blocks %d vs. %d",
  576. GET_SIT_VBLOCKS(raw_sit), valid_blocks);
  577. set_sbi_flag(sbi, SBI_NEED_FSCK);
  578. return -EINVAL;
  579. }
  580. #endif
  581. /* check segment usage, and check boundary of a given segment number */
  582. if (unlikely(GET_SIT_VBLOCKS(raw_sit) > sbi->blocks_per_seg
  583. || segno > TOTAL_SEGS(sbi) - 1)) {
  584. f2fs_msg(sbi->sb, KERN_ERR,
  585. "Wrong valid blocks %d or segno %u",
  586. GET_SIT_VBLOCKS(raw_sit), segno);
  587. set_sbi_flag(sbi, SBI_NEED_FSCK);
  588. return -EINVAL;
  589. }
  590. return 0;
  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. }