segment.h 21 KB

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