space-info.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef BTRFS_SPACE_INFO_H
  3. #define BTRFS_SPACE_INFO_H
  4. #include <trace/events/btrfs.h>
  5. #include <linux/spinlock.h>
  6. #include <linux/list.h>
  7. #include <linux/kobject.h>
  8. #include <linux/lockdep.h>
  9. #include <linux/wait.h>
  10. #include <linux/rwsem.h>
  11. #include "volumes.h"
  12. struct btrfs_fs_info;
  13. struct btrfs_block_group;
  14. /*
  15. * Different levels for to flush space when doing space reservations.
  16. *
  17. * The higher the level, the more methods we try to reclaim space.
  18. */
  19. enum btrfs_reserve_flush_enum {
  20. /* If we are in the transaction, we can't flush anything.*/
  21. BTRFS_RESERVE_NO_FLUSH,
  22. /*
  23. * Flush space by:
  24. * - Running delayed inode items
  25. * - Allocating a new chunk
  26. */
  27. BTRFS_RESERVE_FLUSH_LIMIT,
  28. /*
  29. * Flush space by:
  30. * - Running delayed inode items
  31. * - Running delayed refs
  32. * - Running delalloc and waiting for ordered extents
  33. * - Allocating a new chunk
  34. * - Committing transaction
  35. */
  36. BTRFS_RESERVE_FLUSH_EVICT,
  37. /*
  38. * Flush space by above mentioned methods and by:
  39. * - Running delayed iputs
  40. * - Committing transaction
  41. *
  42. * Can be interrupted by a fatal signal.
  43. */
  44. BTRFS_RESERVE_FLUSH_DATA,
  45. BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE,
  46. BTRFS_RESERVE_FLUSH_ALL,
  47. /*
  48. * Pretty much the same as FLUSH_ALL, but can also steal space from
  49. * global rsv.
  50. *
  51. * Can be interrupted by a fatal signal.
  52. */
  53. BTRFS_RESERVE_FLUSH_ALL_STEAL,
  54. /*
  55. * This is for btrfs_use_block_rsv only. We have exhausted our block
  56. * rsv and our global block rsv. This can happen for things like
  57. * delalloc where we are overwriting a lot of extents with a single
  58. * extent and didn't reserve enough space. Alternatively it can happen
  59. * with delalloc where we reserve 1 extents worth for a large extent but
  60. * fragmentation leads to multiple extents being created. This will
  61. * give us the reservation in the case of
  62. *
  63. * if (num_bytes < (space_info->total_bytes -
  64. * btrfs_space_info_used(space_info, false))
  65. *
  66. * Which ignores bytes_may_use. This is potentially dangerous, but our
  67. * reservation system is generally pessimistic so is able to absorb this
  68. * style of mistake.
  69. */
  70. BTRFS_RESERVE_FLUSH_EMERGENCY,
  71. };
  72. enum btrfs_flush_state {
  73. FLUSH_DELAYED_ITEMS_NR = 1,
  74. FLUSH_DELAYED_ITEMS = 2,
  75. FLUSH_DELAYED_REFS_NR = 3,
  76. FLUSH_DELAYED_REFS = 4,
  77. FLUSH_DELALLOC = 5,
  78. FLUSH_DELALLOC_WAIT = 6,
  79. FLUSH_DELALLOC_FULL = 7,
  80. ALLOC_CHUNK = 8,
  81. ALLOC_CHUNK_FORCE = 9,
  82. RUN_DELAYED_IPUTS = 10,
  83. COMMIT_TRANS = 11,
  84. };
  85. struct btrfs_space_info {
  86. struct btrfs_fs_info *fs_info;
  87. spinlock_t lock;
  88. u64 total_bytes; /* total bytes in the space,
  89. this doesn't take mirrors into account */
  90. u64 bytes_used; /* total bytes used,
  91. this doesn't take mirrors into account */
  92. u64 bytes_pinned; /* total bytes pinned, will be freed when the
  93. transaction finishes */
  94. u64 bytes_reserved; /* total bytes the allocator has reserved for
  95. current allocations */
  96. u64 bytes_may_use; /* number of bytes that may be used for
  97. delalloc/allocations */
  98. u64 bytes_readonly; /* total bytes that are read only */
  99. u64 bytes_zone_unusable; /* total bytes that are unusable until
  100. resetting the device zone */
  101. u64 max_extent_size; /* This will hold the maximum extent size of
  102. the space info if we had an ENOSPC in the
  103. allocator. */
  104. /* Chunk size in bytes */
  105. u64 chunk_size;
  106. /*
  107. * Once a block group drops below this threshold (percents) we'll
  108. * schedule it for reclaim.
  109. */
  110. int bg_reclaim_threshold;
  111. int clamp; /* Used to scale our threshold for preemptive
  112. flushing. The value is >> clamp, so turns
  113. out to be a 2^clamp divisor. */
  114. unsigned int full:1; /* indicates that we cannot allocate any more
  115. chunks for this space */
  116. unsigned int chunk_alloc:1; /* set if we are allocating a chunk */
  117. unsigned int flush:1; /* set if we are trying to make space */
  118. unsigned int force_alloc; /* set if we need to force a chunk
  119. alloc for this space */
  120. u64 disk_used; /* total bytes used on disk */
  121. u64 disk_total; /* total bytes on disk, takes mirrors into
  122. account */
  123. u64 flags;
  124. struct list_head list;
  125. /* Protected by the spinlock 'lock'. */
  126. struct list_head ro_bgs;
  127. struct list_head priority_tickets;
  128. struct list_head tickets;
  129. /*
  130. * Size of space that needs to be reclaimed in order to satisfy pending
  131. * tickets
  132. */
  133. u64 reclaim_size;
  134. /*
  135. * tickets_id just indicates the next ticket will be handled, so note
  136. * it's not stored per ticket.
  137. */
  138. u64 tickets_id;
  139. struct rw_semaphore groups_sem;
  140. /* for block groups in our same type */
  141. struct list_head block_groups[BTRFS_NR_RAID_TYPES];
  142. struct kobject kobj;
  143. struct kobject *block_group_kobjs[BTRFS_NR_RAID_TYPES];
  144. /*
  145. * Monotonically increasing counter of block group reclaim attempts
  146. * Exposed in /sys/fs/<uuid>/allocation/<type>/reclaim_count
  147. */
  148. u64 reclaim_count;
  149. /*
  150. * Monotonically increasing counter of reclaimed bytes
  151. * Exposed in /sys/fs/<uuid>/allocation/<type>/reclaim_bytes
  152. */
  153. u64 reclaim_bytes;
  154. /*
  155. * Monotonically increasing counter of reclaim errors
  156. * Exposed in /sys/fs/<uuid>/allocation/<type>/reclaim_errors
  157. */
  158. u64 reclaim_errors;
  159. /*
  160. * If true, use the dynamic relocation threshold, instead of the
  161. * fixed bg_reclaim_threshold.
  162. */
  163. bool dynamic_reclaim;
  164. /*
  165. * Periodically check all block groups against the reclaim
  166. * threshold in the cleaner thread.
  167. */
  168. bool periodic_reclaim;
  169. /*
  170. * Periodic reclaim should be a no-op if a space_info hasn't
  171. * freed any space since the last time we tried.
  172. */
  173. bool periodic_reclaim_ready;
  174. /*
  175. * Net bytes freed or allocated since the last reclaim pass.
  176. */
  177. s64 reclaimable_bytes;
  178. };
  179. struct reserve_ticket {
  180. u64 bytes;
  181. int error;
  182. bool steal;
  183. struct list_head list;
  184. wait_queue_head_t wait;
  185. };
  186. static inline bool btrfs_mixed_space_info(const struct btrfs_space_info *space_info)
  187. {
  188. return ((space_info->flags & BTRFS_BLOCK_GROUP_METADATA) &&
  189. (space_info->flags & BTRFS_BLOCK_GROUP_DATA));
  190. }
  191. /*
  192. *
  193. * Declare a helper function to detect underflow of various space info members
  194. */
  195. #define DECLARE_SPACE_INFO_UPDATE(name, trace_name) \
  196. static inline void \
  197. btrfs_space_info_update_##name(struct btrfs_fs_info *fs_info, \
  198. struct btrfs_space_info *sinfo, \
  199. s64 bytes) \
  200. { \
  201. const u64 abs_bytes = (bytes < 0) ? -bytes : bytes; \
  202. lockdep_assert_held(&sinfo->lock); \
  203. trace_update_##name(fs_info, sinfo, sinfo->name, bytes); \
  204. trace_btrfs_space_reservation(fs_info, trace_name, \
  205. sinfo->flags, abs_bytes, \
  206. bytes > 0); \
  207. if (bytes < 0 && sinfo->name < -bytes) { \
  208. WARN_ON(1); \
  209. sinfo->name = 0; \
  210. return; \
  211. } \
  212. sinfo->name += bytes; \
  213. }
  214. DECLARE_SPACE_INFO_UPDATE(bytes_may_use, "space_info");
  215. DECLARE_SPACE_INFO_UPDATE(bytes_pinned, "pinned");
  216. DECLARE_SPACE_INFO_UPDATE(bytes_zone_unusable, "zone_unusable");
  217. int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
  218. void btrfs_add_bg_to_space_info(struct btrfs_fs_info *info,
  219. struct btrfs_block_group *block_group);
  220. void btrfs_update_space_info_chunk_size(struct btrfs_space_info *space_info,
  221. u64 chunk_size);
  222. struct btrfs_space_info *btrfs_find_space_info(struct btrfs_fs_info *info,
  223. u64 flags);
  224. u64 __pure btrfs_space_info_used(const struct btrfs_space_info *s_info,
  225. bool may_use_included);
  226. void btrfs_clear_space_info_full(struct btrfs_fs_info *info);
  227. void btrfs_dump_space_info(struct btrfs_fs_info *fs_info,
  228. struct btrfs_space_info *info, u64 bytes,
  229. int dump_block_groups);
  230. int btrfs_reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
  231. struct btrfs_space_info *space_info,
  232. u64 orig_bytes,
  233. enum btrfs_reserve_flush_enum flush);
  234. void btrfs_try_granting_tickets(struct btrfs_fs_info *fs_info,
  235. struct btrfs_space_info *space_info);
  236. int btrfs_can_overcommit(struct btrfs_fs_info *fs_info,
  237. const struct btrfs_space_info *space_info, u64 bytes,
  238. enum btrfs_reserve_flush_enum flush);
  239. static inline void btrfs_space_info_free_bytes_may_use(
  240. struct btrfs_fs_info *fs_info,
  241. struct btrfs_space_info *space_info,
  242. u64 num_bytes)
  243. {
  244. spin_lock(&space_info->lock);
  245. btrfs_space_info_update_bytes_may_use(fs_info, space_info, -num_bytes);
  246. btrfs_try_granting_tickets(fs_info, space_info);
  247. spin_unlock(&space_info->lock);
  248. }
  249. int btrfs_reserve_data_bytes(struct btrfs_fs_info *fs_info, u64 bytes,
  250. enum btrfs_reserve_flush_enum flush);
  251. void btrfs_dump_space_info_for_trans_abort(struct btrfs_fs_info *fs_info);
  252. void btrfs_init_async_reclaim_work(struct btrfs_fs_info *fs_info);
  253. u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo);
  254. void btrfs_space_info_update_reclaimable(struct btrfs_space_info *space_info, s64 bytes);
  255. void btrfs_set_periodic_reclaim_ready(struct btrfs_space_info *space_info, bool ready);
  256. bool btrfs_should_periodic_reclaim(struct btrfs_space_info *space_info);
  257. int btrfs_calc_reclaim_threshold(const struct btrfs_space_info *space_info);
  258. void btrfs_reclaim_sweep(const struct btrfs_fs_info *fs_info);
  259. #endif /* BTRFS_SPACE_INFO_H */