segment.h 23 KB

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