space-info.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. /*
  73. * Please be aware that the order of enum values will be the order of the reclaim
  74. * process in btrfs_async_reclaim_metadata_space().
  75. */
  76. enum btrfs_flush_state {
  77. FLUSH_DELAYED_ITEMS_NR = 1,
  78. FLUSH_DELAYED_ITEMS = 2,
  79. FLUSH_DELAYED_REFS_NR = 3,
  80. FLUSH_DELAYED_REFS = 4,
  81. FLUSH_DELALLOC = 5,
  82. FLUSH_DELALLOC_WAIT = 6,
  83. FLUSH_DELALLOC_FULL = 7,
  84. ALLOC_CHUNK = 8,
  85. ALLOC_CHUNK_FORCE = 9,
  86. RUN_DELAYED_IPUTS = 10,
  87. COMMIT_TRANS = 11,
  88. RESET_ZONES = 12,
  89. };
  90. enum btrfs_space_info_sub_group {
  91. BTRFS_SUB_GROUP_PRIMARY,
  92. BTRFS_SUB_GROUP_DATA_RELOC,
  93. BTRFS_SUB_GROUP_TREELOG,
  94. };
  95. #define BTRFS_SPACE_INFO_SUB_GROUP_MAX 1
  96. struct btrfs_space_info {
  97. struct btrfs_fs_info *fs_info;
  98. struct btrfs_space_info *parent;
  99. struct btrfs_space_info *sub_group[BTRFS_SPACE_INFO_SUB_GROUP_MAX];
  100. int subgroup_id;
  101. spinlock_t lock;
  102. u64 total_bytes; /* total bytes in the space,
  103. this doesn't take mirrors into account */
  104. u64 bytes_used; /* total bytes used,
  105. this doesn't take mirrors into account */
  106. u64 bytes_pinned; /* total bytes pinned, will be freed when the
  107. transaction finishes */
  108. u64 bytes_reserved; /* total bytes the allocator has reserved for
  109. current allocations */
  110. u64 bytes_may_use; /* number of bytes that may be used for
  111. delalloc/allocations */
  112. u64 bytes_readonly; /* total bytes that are read only */
  113. u64 bytes_zone_unusable; /* total bytes that are unusable until
  114. resetting the device zone */
  115. u64 max_extent_size; /* This will hold the maximum extent size of
  116. the space info if we had an ENOSPC in the
  117. allocator. */
  118. /* Chunk size in bytes */
  119. u64 chunk_size;
  120. /*
  121. * Once a block group drops below this threshold (percents) we'll
  122. * schedule it for reclaim.
  123. */
  124. int bg_reclaim_threshold;
  125. int clamp; /* Used to scale our threshold for preemptive
  126. flushing. The value is >> clamp, so turns
  127. out to be a 2^clamp divisor. */
  128. bool full; /* indicates that we cannot allocate any more
  129. chunks for this space */
  130. bool chunk_alloc; /* set if we are allocating a chunk */
  131. bool flush; /* set if we are trying to make space */
  132. unsigned int force_alloc; /* set if we need to force a chunk
  133. alloc for this space */
  134. u64 disk_used; /* total bytes used on disk */
  135. u64 disk_total; /* total bytes on disk, takes mirrors into
  136. account */
  137. u64 flags;
  138. struct list_head list;
  139. /* Protected by the spinlock 'lock'. */
  140. struct list_head ro_bgs;
  141. struct list_head priority_tickets;
  142. struct list_head tickets;
  143. /*
  144. * Size of space that needs to be reclaimed in order to satisfy pending
  145. * tickets
  146. */
  147. u64 reclaim_size;
  148. /*
  149. * tickets_id just indicates the next ticket will be handled, so note
  150. * it's not stored per ticket.
  151. */
  152. u64 tickets_id;
  153. struct rw_semaphore groups_sem;
  154. /* for block groups in our same type */
  155. struct list_head block_groups[BTRFS_NR_RAID_TYPES];
  156. struct kobject kobj;
  157. struct kobject *block_group_kobjs[BTRFS_NR_RAID_TYPES];
  158. /*
  159. * Monotonically increasing counter of block group reclaim attempts
  160. * Exposed in /sys/fs/<uuid>/allocation/<type>/reclaim_count
  161. */
  162. u64 reclaim_count;
  163. /*
  164. * Monotonically increasing counter of reclaimed bytes
  165. * Exposed in /sys/fs/<uuid>/allocation/<type>/reclaim_bytes
  166. */
  167. u64 reclaim_bytes;
  168. /*
  169. * Monotonically increasing counter of reclaim errors
  170. * Exposed in /sys/fs/<uuid>/allocation/<type>/reclaim_errors
  171. */
  172. u64 reclaim_errors;
  173. /*
  174. * If true, use the dynamic relocation threshold, instead of the
  175. * fixed bg_reclaim_threshold.
  176. */
  177. bool dynamic_reclaim;
  178. /*
  179. * Periodically check all block groups against the reclaim
  180. * threshold in the cleaner thread.
  181. */
  182. bool periodic_reclaim;
  183. /*
  184. * Periodic reclaim should be a no-op if a space_info hasn't
  185. * freed any space since the last time we tried.
  186. */
  187. bool periodic_reclaim_ready;
  188. /*
  189. * Net bytes freed or allocated since the last reclaim pass.
  190. */
  191. s64 reclaimable_bytes;
  192. };
  193. static inline bool btrfs_mixed_space_info(const struct btrfs_space_info *space_info)
  194. {
  195. return ((space_info->flags & BTRFS_BLOCK_GROUP_METADATA) &&
  196. (space_info->flags & BTRFS_BLOCK_GROUP_DATA));
  197. }
  198. /*
  199. *
  200. * Declare a helper function to detect underflow of various space info members
  201. */
  202. #define DECLARE_SPACE_INFO_UPDATE(name, trace_name) \
  203. static inline void \
  204. btrfs_space_info_update_##name(struct btrfs_space_info *sinfo, \
  205. s64 bytes) \
  206. { \
  207. struct btrfs_fs_info *fs_info = sinfo->fs_info; \
  208. const u64 abs_bytes = (bytes < 0) ? -bytes : bytes; \
  209. lockdep_assert_held(&sinfo->lock); \
  210. trace_update_##name(fs_info, sinfo, sinfo->name, bytes); \
  211. trace_btrfs_space_reservation(fs_info, trace_name, \
  212. sinfo->flags, abs_bytes, \
  213. bytes > 0); \
  214. if (bytes < 0 && sinfo->name < -bytes) { \
  215. WARN_ON(1); \
  216. sinfo->name = 0; \
  217. return; \
  218. } \
  219. sinfo->name += bytes; \
  220. }
  221. DECLARE_SPACE_INFO_UPDATE(bytes_may_use, "space_info");
  222. DECLARE_SPACE_INFO_UPDATE(bytes_pinned, "pinned");
  223. DECLARE_SPACE_INFO_UPDATE(bytes_zone_unusable, "zone_unusable");
  224. static inline u64 btrfs_space_info_used(const struct btrfs_space_info *s_info,
  225. bool may_use_included)
  226. {
  227. lockdep_assert_held(&s_info->lock);
  228. return s_info->bytes_used + s_info->bytes_reserved +
  229. s_info->bytes_pinned + s_info->bytes_readonly +
  230. s_info->bytes_zone_unusable +
  231. (may_use_included ? s_info->bytes_may_use : 0);
  232. }
  233. int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
  234. void btrfs_add_bg_to_space_info(struct btrfs_fs_info *info,
  235. struct btrfs_block_group *block_group);
  236. void btrfs_update_space_info_chunk_size(struct btrfs_space_info *space_info,
  237. u64 chunk_size);
  238. struct btrfs_space_info *btrfs_find_space_info(struct btrfs_fs_info *info,
  239. u64 flags);
  240. void btrfs_clear_space_info_full(struct btrfs_fs_info *info);
  241. void btrfs_dump_space_info(struct btrfs_space_info *info, u64 bytes,
  242. bool dump_block_groups);
  243. int btrfs_reserve_metadata_bytes(struct btrfs_space_info *space_info,
  244. u64 orig_bytes,
  245. enum btrfs_reserve_flush_enum flush);
  246. void btrfs_try_granting_tickets(struct btrfs_space_info *space_info);
  247. bool btrfs_can_overcommit(const struct btrfs_space_info *space_info, u64 bytes,
  248. enum btrfs_reserve_flush_enum flush);
  249. static inline void btrfs_space_info_free_bytes_may_use(
  250. struct btrfs_space_info *space_info,
  251. u64 num_bytes)
  252. {
  253. spin_lock(&space_info->lock);
  254. btrfs_space_info_update_bytes_may_use(space_info, -num_bytes);
  255. btrfs_try_granting_tickets(space_info);
  256. spin_unlock(&space_info->lock);
  257. }
  258. int btrfs_reserve_data_bytes(struct btrfs_space_info *space_info, u64 bytes,
  259. enum btrfs_reserve_flush_enum flush);
  260. void btrfs_dump_space_info_for_trans_abort(struct btrfs_fs_info *fs_info);
  261. void btrfs_init_async_reclaim_work(struct btrfs_fs_info *fs_info);
  262. u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo);
  263. void btrfs_space_info_update_reclaimable(struct btrfs_space_info *space_info, s64 bytes);
  264. void btrfs_set_periodic_reclaim_ready(struct btrfs_space_info *space_info, bool ready);
  265. int btrfs_calc_reclaim_threshold(const struct btrfs_space_info *space_info);
  266. void btrfs_reclaim_sweep(const struct btrfs_fs_info *fs_info);
  267. void btrfs_return_free_space(struct btrfs_space_info *space_info, u64 len);
  268. static inline const char *btrfs_space_info_type_str(const struct btrfs_space_info *space_info)
  269. {
  270. switch (space_info->flags) {
  271. case BTRFS_BLOCK_GROUP_SYSTEM:
  272. return "SYSTEM";
  273. case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
  274. return "DATA+METADATA";
  275. case BTRFS_BLOCK_GROUP_DATA:
  276. return "DATA";
  277. case BTRFS_BLOCK_GROUP_METADATA:
  278. return "METADATA";
  279. default:
  280. return "UNKNOWN";
  281. }
  282. }
  283. #endif /* BTRFS_SPACE_INFO_H */