gc.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. /* choose candidates from sections which has age of more than 7 days */
  18. #define DEF_GC_THREAD_AGE_THRESHOLD (60 * 60 * 24 * 7)
  19. #define DEF_GC_THREAD_CANDIDATE_RATIO 20 /* select 20% oldest sections as candidates */
  20. #define DEF_GC_THREAD_MAX_CANDIDATE_COUNT 10 /* select at most 10 sections as candidates */
  21. #define DEF_GC_THREAD_AGE_WEIGHT 60 /* age weight */
  22. #define DEFAULT_ACCURACY_CLASS 10000 /* accuracy class */
  23. #define LIMIT_INVALID_BLOCK 40 /* percentage over total user space */
  24. #define LIMIT_FREE_BLOCK 40 /* percentage over invalid + free space */
  25. #define DEF_GC_FAILED_PINNED_FILES 2048
  26. /* Search max. number of dirty segments to select a victim segment */
  27. #define DEF_MAX_VICTIM_SEARCH 4096 /* covers 8GB */
  28. struct f2fs_gc_kthread {
  29. struct task_struct *f2fs_gc_task;
  30. wait_queue_head_t gc_wait_queue_head;
  31. /* for gc sleep time */
  32. unsigned int urgent_sleep_time;
  33. unsigned int min_sleep_time;
  34. unsigned int max_sleep_time;
  35. unsigned int no_gc_sleep_time;
  36. /* for changing gc mode */
  37. unsigned int gc_wake;
  38. /* for GC_MERGE mount option */
  39. wait_queue_head_t fggc_wq; /*
  40. * caller of f2fs_balance_fs()
  41. * will wait on this wait queue.
  42. */
  43. };
  44. struct gc_inode_list {
  45. struct list_head ilist;
  46. struct radix_tree_root iroot;
  47. };
  48. struct victim_info {
  49. unsigned long long mtime; /* mtime of section */
  50. unsigned int segno; /* section No. */
  51. };
  52. struct victim_entry {
  53. struct rb_node rb_node; /* rb node located in rb-tree */
  54. union {
  55. struct {
  56. unsigned long long mtime; /* mtime of section */
  57. unsigned int segno; /* segment No. */
  58. };
  59. struct victim_info vi; /* victim info */
  60. };
  61. struct list_head list;
  62. };
  63. /*
  64. * inline functions
  65. */
  66. /*
  67. * On a Zoned device zone-capacity can be less than zone-size and if
  68. * zone-capacity is not aligned to f2fs segment size(2MB), then the segment
  69. * starting just before zone-capacity has some blocks spanning across the
  70. * zone-capacity, these blocks are not usable.
  71. * Such spanning segments can be in free list so calculate the sum of usable
  72. * blocks in currently free segments including normal and spanning segments.
  73. */
  74. static inline block_t free_segs_blk_count_zoned(struct f2fs_sb_info *sbi)
  75. {
  76. block_t free_seg_blks = 0;
  77. struct free_segmap_info *free_i = FREE_I(sbi);
  78. int j;
  79. spin_lock(&free_i->segmap_lock);
  80. for (j = 0; j < MAIN_SEGS(sbi); j++)
  81. if (!test_bit(j, free_i->free_segmap))
  82. free_seg_blks += f2fs_usable_blks_in_seg(sbi, j);
  83. spin_unlock(&free_i->segmap_lock);
  84. return free_seg_blks;
  85. }
  86. static inline block_t free_segs_blk_count(struct f2fs_sb_info *sbi)
  87. {
  88. if (f2fs_sb_has_blkzoned(sbi))
  89. return free_segs_blk_count_zoned(sbi);
  90. return free_segments(sbi) << sbi->log_blocks_per_seg;
  91. }
  92. static inline block_t free_user_blocks(struct f2fs_sb_info *sbi)
  93. {
  94. block_t free_blks, ovp_blks;
  95. free_blks = free_segs_blk_count(sbi);
  96. ovp_blks = overprovision_segments(sbi) << sbi->log_blocks_per_seg;
  97. if (free_blks < ovp_blks)
  98. return 0;
  99. return free_blks - ovp_blks;
  100. }
  101. static inline block_t limit_invalid_user_blocks(struct f2fs_sb_info *sbi)
  102. {
  103. return (long)(sbi->user_block_count * LIMIT_INVALID_BLOCK) / 100;
  104. }
  105. static inline block_t limit_free_user_blocks(struct f2fs_sb_info *sbi)
  106. {
  107. block_t reclaimable_user_blocks = sbi->user_block_count -
  108. written_block_count(sbi);
  109. return (long)(reclaimable_user_blocks * LIMIT_FREE_BLOCK) / 100;
  110. }
  111. static inline void increase_sleep_time(struct f2fs_gc_kthread *gc_th,
  112. unsigned int *wait)
  113. {
  114. unsigned int min_time = gc_th->min_sleep_time;
  115. unsigned int max_time = gc_th->max_sleep_time;
  116. if (*wait == gc_th->no_gc_sleep_time)
  117. return;
  118. if ((long long)*wait + (long long)min_time > (long long)max_time)
  119. *wait = max_time;
  120. else
  121. *wait += min_time;
  122. }
  123. static inline void decrease_sleep_time(struct f2fs_gc_kthread *gc_th,
  124. unsigned int *wait)
  125. {
  126. unsigned int min_time = gc_th->min_sleep_time;
  127. if (*wait == gc_th->no_gc_sleep_time)
  128. *wait = gc_th->max_sleep_time;
  129. if ((long long)*wait - (long long)min_time < (long long)min_time)
  130. *wait = min_time;
  131. else
  132. *wait -= min_time;
  133. }
  134. static inline bool has_enough_invalid_blocks(struct f2fs_sb_info *sbi)
  135. {
  136. block_t invalid_user_blocks = sbi->user_block_count -
  137. written_block_count(sbi);
  138. /*
  139. * Background GC is triggered with the following conditions.
  140. * 1. There are a number of invalid blocks.
  141. * 2. There is not enough free space.
  142. */
  143. if (invalid_user_blocks > limit_invalid_user_blocks(sbi) &&
  144. free_user_blocks(sbi) < limit_free_user_blocks(sbi))
  145. return true;
  146. return false;
  147. }