block-group.h 12 KB

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