free-space-cache.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2009 Oracle. All rights reserved.
  4. */
  5. #ifndef BTRFS_FREE_SPACE_CACHE_H
  6. #define BTRFS_FREE_SPACE_CACHE_H
  7. #include <linux/rbtree.h>
  8. #include <linux/list.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/mutex.h>
  11. #include <linux/freezer.h>
  12. #include "fs.h"
  13. struct inode;
  14. struct page;
  15. struct btrfs_fs_info;
  16. struct btrfs_path;
  17. struct btrfs_trans_handle;
  18. struct btrfs_trim_block_group;
  19. /*
  20. * This is the trim state of an extent or bitmap.
  21. *
  22. * BTRFS_TRIM_STATE_TRIMMING is special and used to maintain the state of a
  23. * bitmap as we may need several trims to fully trim a single bitmap entry.
  24. * This is reset should any free space other than trimmed space be added to the
  25. * bitmap.
  26. */
  27. enum btrfs_trim_state {
  28. BTRFS_TRIM_STATE_UNTRIMMED,
  29. BTRFS_TRIM_STATE_TRIMMED,
  30. BTRFS_TRIM_STATE_TRIMMING,
  31. };
  32. struct btrfs_free_space {
  33. struct rb_node offset_index;
  34. struct rb_node bytes_index;
  35. u64 offset;
  36. u64 bytes;
  37. u64 max_extent_size;
  38. unsigned long *bitmap;
  39. struct list_head list;
  40. enum btrfs_trim_state trim_state;
  41. s32 bitmap_extents;
  42. };
  43. static inline bool btrfs_free_space_trimmed(struct btrfs_free_space *info)
  44. {
  45. return (info->trim_state == BTRFS_TRIM_STATE_TRIMMED);
  46. }
  47. static inline bool btrfs_free_space_trimming_bitmap(
  48. struct btrfs_free_space *info)
  49. {
  50. return (info->trim_state == BTRFS_TRIM_STATE_TRIMMING);
  51. }
  52. static inline bool btrfs_trim_interrupted(void)
  53. {
  54. return fatal_signal_pending(current) || freezing(current);
  55. }
  56. /*
  57. * Deltas are an effective way to populate global statistics. Give macro names
  58. * to make it clear what we're doing. An example is discard_extents in
  59. * btrfs_free_space_ctl.
  60. */
  61. enum {
  62. BTRFS_STAT_CURR,
  63. BTRFS_STAT_PREV,
  64. BTRFS_STAT_NR_ENTRIES,
  65. };
  66. struct btrfs_free_space_ctl {
  67. spinlock_t tree_lock;
  68. struct rb_root free_space_offset;
  69. struct rb_root_cached free_space_bytes;
  70. u64 free_space;
  71. int extents_thresh;
  72. int free_extents;
  73. int total_bitmaps;
  74. int unit;
  75. u64 start;
  76. s32 discardable_extents[BTRFS_STAT_NR_ENTRIES];
  77. s64 discardable_bytes[BTRFS_STAT_NR_ENTRIES];
  78. const struct btrfs_free_space_op *op;
  79. struct btrfs_block_group *block_group;
  80. struct mutex cache_writeout_mutex;
  81. struct list_head trimming_ranges;
  82. };
  83. struct btrfs_free_space_op {
  84. bool (*use_bitmap)(struct btrfs_free_space_ctl *ctl,
  85. struct btrfs_free_space *info);
  86. };
  87. struct btrfs_io_ctl {
  88. void *cur, *orig;
  89. struct page *page;
  90. struct page **pages;
  91. struct btrfs_fs_info *fs_info;
  92. struct inode *inode;
  93. unsigned long size;
  94. int index;
  95. int num_pages;
  96. int entries;
  97. int bitmaps;
  98. };
  99. int __init btrfs_free_space_init(void);
  100. void __cold btrfs_free_space_exit(void);
  101. struct inode *lookup_free_space_inode(struct btrfs_block_group *block_group,
  102. struct btrfs_path *path);
  103. int create_free_space_inode(struct btrfs_trans_handle *trans,
  104. struct btrfs_block_group *block_group,
  105. struct btrfs_path *path);
  106. int btrfs_remove_free_space_inode(struct btrfs_trans_handle *trans,
  107. struct inode *inode,
  108. struct btrfs_block_group *block_group);
  109. int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
  110. struct btrfs_block_group *block_group,
  111. struct inode *inode);
  112. int load_free_space_cache(struct btrfs_block_group *block_group);
  113. int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
  114. struct btrfs_block_group *block_group,
  115. struct btrfs_path *path);
  116. int btrfs_write_out_cache(struct btrfs_trans_handle *trans,
  117. struct btrfs_block_group *block_group,
  118. struct btrfs_path *path);
  119. void btrfs_init_free_space_ctl(struct btrfs_block_group *block_group,
  120. struct btrfs_free_space_ctl *ctl);
  121. int btrfs_add_free_space(struct btrfs_block_group *block_group,
  122. u64 bytenr, u64 size);
  123. int btrfs_add_free_space_unused(struct btrfs_block_group *block_group,
  124. u64 bytenr, u64 size);
  125. int btrfs_add_free_space_async_trimmed(struct btrfs_block_group *block_group,
  126. u64 bytenr, u64 size);
  127. int btrfs_remove_free_space(struct btrfs_block_group *block_group,
  128. u64 bytenr, u64 size);
  129. void btrfs_remove_free_space_cache(struct btrfs_block_group *block_group);
  130. bool btrfs_is_free_space_trimmed(struct btrfs_block_group *block_group);
  131. u64 btrfs_find_space_for_alloc(struct btrfs_block_group *block_group,
  132. u64 offset, u64 bytes, u64 empty_size,
  133. u64 *max_extent_size);
  134. void btrfs_dump_free_space(struct btrfs_block_group *block_group,
  135. u64 bytes);
  136. int btrfs_find_space_cluster(struct btrfs_block_group *block_group,
  137. struct btrfs_free_cluster *cluster,
  138. u64 offset, u64 bytes, u64 empty_size);
  139. void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster);
  140. u64 btrfs_alloc_from_cluster(struct btrfs_block_group *block_group,
  141. struct btrfs_free_cluster *cluster, u64 bytes,
  142. u64 min_start, u64 *max_extent_size);
  143. void btrfs_return_cluster_to_free_space(
  144. struct btrfs_block_group *block_group,
  145. struct btrfs_free_cluster *cluster);
  146. int btrfs_trim_block_group(struct btrfs_block_group *block_group,
  147. u64 *trimmed, u64 start, u64 end, u64 minlen);
  148. int btrfs_trim_block_group_extents(struct btrfs_block_group *block_group,
  149. u64 *trimmed, u64 start, u64 end, u64 minlen,
  150. bool async);
  151. int btrfs_trim_block_group_bitmaps(struct btrfs_block_group *block_group,
  152. u64 *trimmed, u64 start, u64 end, u64 minlen,
  153. u64 maxlen, bool async);
  154. bool btrfs_free_space_cache_v1_active(struct btrfs_fs_info *fs_info);
  155. int btrfs_set_free_space_cache_v1_active(struct btrfs_fs_info *fs_info, bool active);
  156. /* Support functions for running our sanity tests */
  157. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  158. int test_add_free_space_entry(struct btrfs_block_group *cache,
  159. u64 offset, u64 bytes, bool bitmap);
  160. int test_check_exists(struct btrfs_block_group *cache, u64 offset, u64 bytes);
  161. #endif
  162. #endif