segment.h 22 KB

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