gc.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * fs/f2fs/gc.h
  4. *
  5. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  6. * http://www.samsung.com/
  7. */
  8. #define GC_THREAD_MIN_WB_PAGES 1 /*
  9. * a threshold to determine
  10. * whether IO subsystem is idle
  11. * or not
  12. */
  13. #define DEF_GC_THREAD_URGENT_SLEEP_TIME 500 /* 500 ms */
  14. #define DEF_GC_THREAD_MIN_SLEEP_TIME 30000 /* milliseconds */
  15. #define DEF_GC_THREAD_MAX_SLEEP_TIME 60000
  16. #define DEF_GC_THREAD_NOGC_SLEEP_TIME 300000 /* wait 5 min */
  17. /* GC sleep parameters for zoned deivces */
  18. #define DEF_GC_THREAD_MIN_SLEEP_TIME_ZONED 10
  19. #define DEF_GC_THREAD_MAX_SLEEP_TIME_ZONED 20
  20. #define DEF_GC_THREAD_NOGC_SLEEP_TIME_ZONED 60000
  21. /* choose candidates from sections which has age of more than 7 days */
  22. #define DEF_GC_THREAD_AGE_THRESHOLD (60 * 60 * 24 * 7)
  23. #define DEF_GC_THREAD_CANDIDATE_RATIO 20 /* select 20% oldest sections as candidates */
  24. #define DEF_GC_THREAD_MAX_CANDIDATE_COUNT 10 /* select at most 10 sections as candidates */
  25. #define DEF_GC_THREAD_AGE_WEIGHT 60 /* age weight */
  26. #define DEF_GC_THREAD_VALID_THRESH_RATIO 95 /* do not GC over 95% valid block ratio for one time GC */
  27. #define DEFAULT_ACCURACY_CLASS 10000 /* accuracy class */
  28. #define LIMIT_INVALID_BLOCK 40 /* percentage over total user space */
  29. #define LIMIT_FREE_BLOCK 40 /* percentage over invalid + free space */
  30. #define LIMIT_NO_ZONED_GC 60 /* percentage over total user space of no gc for zoned devices */
  31. #define LIMIT_BOOST_ZONED_GC 25 /* percentage over total user space of boosted gc for zoned devices */
  32. #define DEF_MIGRATION_WINDOW_GRANULARITY_ZONED 3
  33. #define BOOST_GC_MULTIPLE 5
  34. #define ZONED_PIN_SEC_REQUIRED_COUNT 1
  35. #define DEF_GC_FAILED_PINNED_FILES 2048
  36. #define MAX_GC_FAILED_PINNED_FILES USHRT_MAX
  37. /* Search max. number of dirty segments to select a victim segment */
  38. #define DEF_MAX_VICTIM_SEARCH 4096 /* covers 8GB */
  39. #define NR_GC_CHECKPOINT_SECS (3) /* data/node/dentry sections */
  40. struct f2fs_gc_kthread {
  41. struct task_struct *f2fs_gc_task;
  42. wait_queue_head_t gc_wait_queue_head;
  43. /* for gc sleep time */
  44. unsigned int urgent_sleep_time;
  45. unsigned int min_sleep_time;
  46. unsigned int max_sleep_time;
  47. unsigned int no_gc_sleep_time;
  48. /* for changing gc mode */
  49. bool gc_wake;
  50. /* for GC_MERGE mount option */
  51. wait_queue_head_t fggc_wq; /*
  52. * caller of f2fs_balance_fs()
  53. * will wait on this wait queue.
  54. */
  55. /* for gc control for zoned devices */
  56. unsigned int no_zoned_gc_percent;
  57. unsigned int boost_zoned_gc_percent;
  58. unsigned int valid_thresh_ratio;
  59. };
  60. struct gc_inode_list {
  61. struct list_head ilist;
  62. struct radix_tree_root iroot;
  63. };
  64. struct victim_entry {
  65. struct rb_node rb_node; /* rb node located in rb-tree */
  66. unsigned long long mtime; /* mtime of section */
  67. unsigned int segno; /* segment No. */
  68. struct list_head list;
  69. };
  70. /*
  71. * inline functions
  72. */
  73. /*
  74. * On a Zoned device zone-capacity can be less than zone-size and if
  75. * zone-capacity is not aligned to f2fs segment size(2MB), then the segment
  76. * starting just before zone-capacity has some blocks spanning across the
  77. * zone-capacity, these blocks are not usable.
  78. * Such spanning segments can be in free list so calculate the sum of usable
  79. * blocks in currently free segments including normal and spanning segments.
  80. */
  81. static inline block_t free_segs_blk_count_zoned(struct f2fs_sb_info *sbi)
  82. {
  83. block_t free_seg_blks = 0;
  84. struct free_segmap_info *free_i = FREE_I(sbi);
  85. int j;
  86. spin_lock(&free_i->segmap_lock);
  87. for (j = 0; j < MAIN_SEGS(sbi); j++)
  88. if (!test_bit(j, free_i->free_segmap))
  89. free_seg_blks += f2fs_usable_blks_in_seg(sbi, j);
  90. spin_unlock(&free_i->segmap_lock);
  91. return free_seg_blks;
  92. }
  93. static inline block_t free_segs_blk_count(struct f2fs_sb_info *sbi)
  94. {
  95. if (f2fs_sb_has_blkzoned(sbi))
  96. return free_segs_blk_count_zoned(sbi);
  97. return SEGS_TO_BLKS(sbi, free_segments(sbi));
  98. }
  99. static inline block_t free_user_blocks(struct f2fs_sb_info *sbi)
  100. {
  101. block_t free_blks, ovp_blks;
  102. free_blks = free_segs_blk_count(sbi);
  103. ovp_blks = SEGS_TO_BLKS(sbi, overprovision_segments(sbi));
  104. if (free_blks < ovp_blks)
  105. return 0;
  106. return free_blks - ovp_blks;
  107. }
  108. static inline block_t limit_invalid_user_blocks(block_t user_block_count)
  109. {
  110. return (long)(user_block_count * LIMIT_INVALID_BLOCK) / 100;
  111. }
  112. static inline block_t limit_free_user_blocks(block_t reclaimable_user_blocks)
  113. {
  114. return (long)(reclaimable_user_blocks * LIMIT_FREE_BLOCK) / 100;
  115. }
  116. static inline void increase_sleep_time(struct f2fs_gc_kthread *gc_th,
  117. unsigned int *wait)
  118. {
  119. unsigned int min_time = gc_th->min_sleep_time;
  120. unsigned int max_time = gc_th->max_sleep_time;
  121. if (*wait == gc_th->no_gc_sleep_time)
  122. return;
  123. if ((long long)*wait + (long long)min_time > (long long)max_time)
  124. *wait = max_time;
  125. else
  126. *wait += min_time;
  127. }
  128. static inline void decrease_sleep_time(struct f2fs_gc_kthread *gc_th,
  129. unsigned int *wait)
  130. {
  131. unsigned int min_time = gc_th->min_sleep_time;
  132. if (*wait == gc_th->no_gc_sleep_time)
  133. *wait = gc_th->max_sleep_time;
  134. if ((long long)*wait - (long long)min_time < (long long)min_time)
  135. *wait = min_time;
  136. else
  137. *wait -= min_time;
  138. }
  139. static inline bool has_enough_free_blocks(struct f2fs_sb_info *sbi,
  140. unsigned int limit_perc)
  141. {
  142. return free_sections(sbi) > ((sbi->total_sections * limit_perc) / 100);
  143. }
  144. static inline bool has_enough_invalid_blocks(struct f2fs_sb_info *sbi)
  145. {
  146. block_t user_block_count = sbi->user_block_count;
  147. block_t invalid_user_blocks = user_block_count -
  148. written_block_count(sbi);
  149. /*
  150. * Background GC is triggered with the following conditions.
  151. * 1. There are a number of invalid blocks.
  152. * 2. There is not enough free space.
  153. */
  154. return (invalid_user_blocks >
  155. limit_invalid_user_blocks(user_block_count) &&
  156. free_user_blocks(sbi) <
  157. limit_free_user_blocks(invalid_user_blocks));
  158. }
  159. static inline bool need_to_boost_gc(struct f2fs_sb_info *sbi)
  160. {
  161. if (f2fs_sb_has_blkzoned(sbi))
  162. return !has_enough_free_blocks(sbi, LIMIT_BOOST_ZONED_GC);
  163. return has_enough_invalid_blocks(sbi);
  164. }