segment.h 22 KB

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