block-group.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef BTRFS_BLOCK_GROUP_H
  3. #define BTRFS_BLOCK_GROUP_H
  4. #include <linux/atomic.h>
  5. #include <linux/mutex.h>
  6. #include <linux/list.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/refcount.h>
  9. #include <linux/wait.h>
  10. #include <linux/sizes.h>
  11. #include <linux/rwsem.h>
  12. #include <linux/rbtree.h>
  13. #include <uapi/linux/btrfs_tree.h>
  14. #include "free-space-cache.h"
  15. struct btrfs_chunk_map;
  16. struct btrfs_fs_info;
  17. struct btrfs_inode;
  18. struct btrfs_trans_handle;
  19. enum btrfs_disk_cache_state {
  20. BTRFS_DC_WRITTEN,
  21. BTRFS_DC_ERROR,
  22. BTRFS_DC_CLEAR,
  23. BTRFS_DC_SETUP,
  24. };
  25. enum btrfs_block_group_size_class {
  26. /* Unset */
  27. BTRFS_BG_SZ_NONE,
  28. /* 0 < size <= 128K */
  29. BTRFS_BG_SZ_SMALL,
  30. /* 128K < size <= 8M */
  31. BTRFS_BG_SZ_MEDIUM,
  32. /* 8M < size < BG_LENGTH */
  33. BTRFS_BG_SZ_LARGE,
  34. };
  35. /*
  36. * This describes the state of the block_group for async discard. This is due
  37. * to the two pass nature of it where extent discarding is prioritized over
  38. * bitmap discarding. BTRFS_DISCARD_RESET_CURSOR is set when we are resetting
  39. * between lists to prevent contention for discard state variables
  40. * (eg. discard_cursor).
  41. */
  42. enum btrfs_discard_state {
  43. BTRFS_DISCARD_EXTENTS,
  44. BTRFS_DISCARD_BITMAPS,
  45. BTRFS_DISCARD_RESET_CURSOR,
  46. };
  47. /*
  48. * Control flags for do_chunk_alloc's force field CHUNK_ALLOC_NO_FORCE means to
  49. * only allocate a chunk if we really need one.
  50. *
  51. * CHUNK_ALLOC_LIMITED means to only try and allocate one if we have very few
  52. * chunks already allocated. This is used as part of the clustering code to
  53. * help make sure we have a good pool of storage to cluster in, without filling
  54. * the FS with empty chunks
  55. *
  56. * CHUNK_ALLOC_FORCE means it must try to allocate one
  57. *
  58. * CHUNK_ALLOC_FORCE_FOR_EXTENT like CHUNK_ALLOC_FORCE but called from
  59. * find_free_extent() that also activaes the zone
  60. */
  61. enum btrfs_chunk_alloc_enum {
  62. CHUNK_ALLOC_NO_FORCE,
  63. CHUNK_ALLOC_LIMITED,
  64. CHUNK_ALLOC_FORCE,
  65. CHUNK_ALLOC_FORCE_FOR_EXTENT,
  66. };
  67. /* Block group flags set at runtime */
  68. enum btrfs_block_group_flags {
  69. BLOCK_GROUP_FLAG_IREF,
  70. BLOCK_GROUP_FLAG_REMOVED,
  71. BLOCK_GROUP_FLAG_TO_COPY,
  72. BLOCK_GROUP_FLAG_RELOCATING_REPAIR,
  73. BLOCK_GROUP_FLAG_CHUNK_ITEM_INSERTED,
  74. BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
  75. BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
  76. /* Does the block group need to be added to the free space tree? */
  77. BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE,
  78. /* Indicate that the block group is placed on a sequential zone */
  79. BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE,
  80. /*
  81. * Indicate that block group is in the list of new block groups of a
  82. * transaction.
  83. */
  84. BLOCK_GROUP_FLAG_NEW,
  85. };
  86. enum btrfs_caching_type {
  87. BTRFS_CACHE_NO,
  88. BTRFS_CACHE_STARTED,
  89. BTRFS_CACHE_FINISHED,
  90. BTRFS_CACHE_ERROR,
  91. };
  92. struct btrfs_caching_control {
  93. struct list_head list;
  94. struct mutex mutex;
  95. wait_queue_head_t wait;
  96. struct btrfs_work work;
  97. struct btrfs_block_group *block_group;
  98. /* Track progress of caching during allocation. */
  99. atomic_t progress;
  100. refcount_t count;
  101. };
  102. /* Once caching_thread() finds this much free space, it will wake up waiters. */
  103. #define CACHING_CTL_WAKE_UP SZ_2M
  104. struct btrfs_block_group {
  105. struct btrfs_fs_info *fs_info;
  106. struct btrfs_inode *inode;
  107. spinlock_t lock;
  108. u64 start;
  109. u64 length;
  110. u64 pinned;
  111. u64 reserved;
  112. u64 used;
  113. u64 delalloc_bytes;
  114. u64 bytes_super;
  115. u64 flags;
  116. u64 cache_generation;
  117. u64 global_root_id;
  118. /*
  119. * The last committed used bytes of this block group, if the above @used
  120. * is still the same as @commit_used, we don't need to update block
  121. * group item of this block group.
  122. */
  123. u64 commit_used;
  124. /*
  125. * If the free space extent count exceeds this number, convert the block
  126. * group to bitmaps.
  127. */
  128. u32 bitmap_high_thresh;
  129. /*
  130. * If the free space extent count drops below this number, convert the
  131. * block group back to extents.
  132. */
  133. u32 bitmap_low_thresh;
  134. /*
  135. * It is just used for the delayed data space allocation because
  136. * only the data space allocation and the relative metadata update
  137. * can be done cross the transaction.
  138. */
  139. struct rw_semaphore data_rwsem;
  140. /* For raid56, this is a full stripe, without parity */
  141. unsigned long full_stripe_len;
  142. unsigned long runtime_flags;
  143. unsigned int ro;
  144. int disk_cache_state;
  145. /* Cache tracking stuff */
  146. int cached;
  147. struct btrfs_caching_control *caching_ctl;
  148. struct btrfs_space_info *space_info;
  149. /* Free space cache stuff */
  150. struct btrfs_free_space_ctl *free_space_ctl;
  151. /* Block group cache stuff */
  152. struct rb_node cache_node;
  153. /* For block groups in the same raid type */
  154. struct list_head list;
  155. refcount_t refs;
  156. /*
  157. * List of struct btrfs_free_clusters for this block group.
  158. * Today it will only have one thing on it, but that may change
  159. */
  160. struct list_head cluster_list;
  161. /*
  162. * Used for several lists:
  163. *
  164. * 1) struct btrfs_fs_info::unused_bgs
  165. * 2) struct btrfs_fs_info::reclaim_bgs
  166. * 3) struct btrfs_transaction::deleted_bgs
  167. * 4) struct btrfs_trans_handle::new_bgs
  168. */
  169. struct list_head bg_list;
  170. /* For read-only block groups */
  171. struct list_head ro_list;
  172. /*
  173. * When non-zero it means the block group's logical address and its
  174. * device extents can not be reused for future block group allocations
  175. * until the counter goes down to 0. This is to prevent them from being
  176. * reused while some task is still using the block group after it was
  177. * deleted - we want to make sure they can only be reused for new block
  178. * groups after that task is done with the deleted block group.
  179. */
  180. atomic_t frozen;
  181. /* For discard operations */
  182. struct list_head discard_list;
  183. int discard_index;
  184. u64 discard_eligible_time;
  185. u64 discard_cursor;
  186. enum btrfs_discard_state discard_state;
  187. /* For dirty block groups */
  188. struct list_head dirty_list;
  189. struct list_head io_list;
  190. struct btrfs_io_ctl io_ctl;
  191. /*
  192. * Incremented when doing extent allocations and holding a read lock
  193. * on the space_info's groups_sem semaphore.
  194. * Decremented when an ordered extent that represents an IO against this
  195. * block group's range is created (after it's added to its inode's
  196. * root's list of ordered extents) or immediately after the allocation
  197. * if it's a metadata extent or fallocate extent (for these cases we
  198. * don't create ordered extents).
  199. */
  200. atomic_t reservations;
  201. /*
  202. * Incremented while holding the spinlock *lock* by a task checking if
  203. * it can perform a nocow write (incremented if the value for the *ro*
  204. * field is 0). Decremented by such tasks once they create an ordered
  205. * extent or before that if some error happens before reaching that step.
  206. * This is to prevent races between block group relocation and nocow
  207. * writes through direct IO.
  208. */
  209. atomic_t nocow_writers;
  210. /* Lock for free space tree operations. */
  211. struct mutex free_space_lock;
  212. /*
  213. * Number of extents in this block group used for swap files.
  214. * All accesses protected by the spinlock 'lock'.
  215. */
  216. int swap_extents;
  217. /*
  218. * Allocation offset for the block group to implement sequential
  219. * allocation. This is used only on a zoned filesystem.
  220. */
  221. u64 alloc_offset;
  222. u64 zone_unusable;
  223. u64 zone_capacity;
  224. u64 meta_write_pointer;
  225. struct btrfs_chunk_map *physical_map;
  226. struct list_head active_bg_list;
  227. struct work_struct zone_finish_work;
  228. struct extent_buffer *last_eb;
  229. enum btrfs_block_group_size_class size_class;
  230. u64 reclaim_mark;
  231. };
  232. static inline u64 btrfs_block_group_end(const struct btrfs_block_group *block_group)
  233. {
  234. return (block_group->start + block_group->length);
  235. }
  236. static inline bool btrfs_is_block_group_used(const struct btrfs_block_group *bg)
  237. {
  238. lockdep_assert_held(&bg->lock);
  239. return (bg->used > 0 || bg->reserved > 0 || bg->pinned > 0);
  240. }
  241. static inline bool btrfs_is_block_group_data_only(const struct btrfs_block_group *block_group)
  242. {
  243. /*
  244. * In mixed mode the fragmentation is expected to be high, lowering the
  245. * efficiency, so only proper data block groups are considered.
  246. */
  247. return (block_group->flags & BTRFS_BLOCK_GROUP_DATA) &&
  248. !(block_group->flags & BTRFS_BLOCK_GROUP_METADATA);
  249. }
  250. #ifdef CONFIG_BTRFS_DEBUG
  251. int btrfs_should_fragment_free_space(const struct btrfs_block_group *block_group);
  252. #endif
  253. struct btrfs_block_group *btrfs_lookup_first_block_group(
  254. struct btrfs_fs_info *info, u64 bytenr);
  255. struct btrfs_block_group *btrfs_lookup_block_group(
  256. struct btrfs_fs_info *info, u64 bytenr);
  257. struct btrfs_block_group *btrfs_next_block_group(
  258. struct btrfs_block_group *cache);
  259. void btrfs_get_block_group(struct btrfs_block_group *cache);
  260. void btrfs_put_block_group(struct btrfs_block_group *cache);
  261. void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
  262. const u64 start);
  263. void btrfs_wait_block_group_reservations(struct btrfs_block_group *bg);
  264. struct btrfs_block_group *btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info,
  265. u64 bytenr);
  266. void btrfs_dec_nocow_writers(struct btrfs_block_group *bg);
  267. void btrfs_wait_nocow_writers(struct btrfs_block_group *bg);
  268. void btrfs_wait_block_group_cache_progress(struct btrfs_block_group *cache,
  269. u64 num_bytes);
  270. int btrfs_cache_block_group(struct btrfs_block_group *cache, bool wait);
  271. struct btrfs_caching_control *btrfs_get_caching_control(
  272. struct btrfs_block_group *cache);
  273. int btrfs_add_new_free_space(struct btrfs_block_group *block_group,
  274. u64 start, u64 end, u64 *total_added_ret);
  275. struct btrfs_trans_handle *btrfs_start_trans_remove_block_group(
  276. struct btrfs_fs_info *fs_info,
  277. const u64 chunk_offset);
  278. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  279. struct btrfs_chunk_map *map);
  280. void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info);
  281. void btrfs_mark_bg_unused(struct btrfs_block_group *bg);
  282. void btrfs_reclaim_bgs_work(struct work_struct *work);
  283. void btrfs_reclaim_bgs(struct btrfs_fs_info *fs_info);
  284. void btrfs_mark_bg_to_reclaim(struct btrfs_block_group *bg);
  285. int btrfs_read_block_groups(struct btrfs_fs_info *info);
  286. struct btrfs_block_group *btrfs_make_block_group(struct btrfs_trans_handle *trans,
  287. u64 type,
  288. u64 chunk_offset, u64 size);
  289. void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans);
  290. int btrfs_inc_block_group_ro(struct btrfs_block_group *cache,
  291. bool do_chunk_alloc);
  292. void btrfs_dec_block_group_ro(struct btrfs_block_group *cache);
  293. int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans);
  294. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans);
  295. int btrfs_setup_space_cache(struct btrfs_trans_handle *trans);
  296. int btrfs_update_block_group(struct btrfs_trans_handle *trans,
  297. u64 bytenr, u64 num_bytes, bool alloc);
  298. int btrfs_add_reserved_bytes(struct btrfs_block_group *cache,
  299. u64 ram_bytes, u64 num_bytes, int delalloc,
  300. bool force_wrong_size_class);
  301. void btrfs_free_reserved_bytes(struct btrfs_block_group *cache,
  302. u64 num_bytes, int delalloc);
  303. int btrfs_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags,
  304. enum btrfs_chunk_alloc_enum force);
  305. int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type);
  306. void check_system_chunk(struct btrfs_trans_handle *trans, const u64 type);
  307. void btrfs_reserve_chunk_metadata(struct btrfs_trans_handle *trans,
  308. bool is_item_insertion);
  309. u64 btrfs_get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags);
  310. void btrfs_put_block_group_cache(struct btrfs_fs_info *info);
  311. int btrfs_free_block_groups(struct btrfs_fs_info *info);
  312. int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
  313. u64 physical, u64 **logical, int *naddrs, int *stripe_len);
  314. static inline u64 btrfs_data_alloc_profile(struct btrfs_fs_info *fs_info)
  315. {
  316. return btrfs_get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_DATA);
  317. }
  318. static inline u64 btrfs_metadata_alloc_profile(struct btrfs_fs_info *fs_info)
  319. {
  320. return btrfs_get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_METADATA);
  321. }
  322. static inline u64 btrfs_system_alloc_profile(struct btrfs_fs_info *fs_info)
  323. {
  324. return btrfs_get_alloc_profile(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
  325. }
  326. static inline int btrfs_block_group_done(const struct btrfs_block_group *cache)
  327. {
  328. smp_mb();
  329. return cache->cached == BTRFS_CACHE_FINISHED ||
  330. cache->cached == BTRFS_CACHE_ERROR;
  331. }
  332. void btrfs_freeze_block_group(struct btrfs_block_group *cache);
  333. void btrfs_unfreeze_block_group(struct btrfs_block_group *cache);
  334. bool btrfs_inc_block_group_swap_extents(struct btrfs_block_group *bg);
  335. void btrfs_dec_block_group_swap_extents(struct btrfs_block_group *bg, int amount);
  336. enum btrfs_block_group_size_class btrfs_calc_block_group_size_class(u64 size);
  337. int btrfs_use_block_group_size_class(struct btrfs_block_group *bg,
  338. enum btrfs_block_group_size_class size_class,
  339. bool force_wrong_size_class);
  340. bool btrfs_block_group_should_use_size_class(const struct btrfs_block_group *bg);
  341. #endif /* BTRFS_BLOCK_GROUP_H */