segment.h 22 KB

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