block-group.h 14 KB

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