segment.h 24 KB

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