segment.h 26 KB

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