free-space-cache.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. struct btrfs_free_space {
  8. struct rb_node offset_index;
  9. u64 offset;
  10. u64 bytes;
  11. u64 max_extent_size;
  12. unsigned long *bitmap;
  13. struct list_head list;
  14. };
  15. struct btrfs_free_space_ctl {
  16. spinlock_t tree_lock;
  17. struct rb_root free_space_offset;
  18. u64 free_space;
  19. int extents_thresh;
  20. int free_extents;
  21. int total_bitmaps;
  22. int unit;
  23. u64 start;
  24. const struct btrfs_free_space_op *op;
  25. void *private;
  26. struct mutex cache_writeout_mutex;
  27. struct list_head trimming_ranges;
  28. };
  29. struct btrfs_free_space_op {
  30. void (*recalc_thresholds)(struct btrfs_free_space_ctl *ctl);
  31. bool (*use_bitmap)(struct btrfs_free_space_ctl *ctl,
  32. struct btrfs_free_space *info);
  33. };
  34. struct btrfs_io_ctl;
  35. struct inode *lookup_free_space_inode(struct btrfs_fs_info *fs_info,
  36. struct btrfs_block_group_cache
  37. *block_group, struct btrfs_path *path);
  38. int create_free_space_inode(struct btrfs_fs_info *fs_info,
  39. struct btrfs_trans_handle *trans,
  40. struct btrfs_block_group_cache *block_group,
  41. struct btrfs_path *path);
  42. int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info,
  43. struct btrfs_block_rsv *rsv);
  44. int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
  45. struct btrfs_block_group_cache *block_group,
  46. struct inode *inode);
  47. int load_free_space_cache(struct btrfs_fs_info *fs_info,
  48. struct btrfs_block_group_cache *block_group);
  49. int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
  50. struct btrfs_block_group_cache *block_group,
  51. struct btrfs_path *path);
  52. int btrfs_write_out_cache(struct btrfs_fs_info *fs_info,
  53. struct btrfs_trans_handle *trans,
  54. struct btrfs_block_group_cache *block_group,
  55. struct btrfs_path *path);
  56. struct inode *lookup_free_ino_inode(struct btrfs_root *root,
  57. struct btrfs_path *path);
  58. int create_free_ino_inode(struct btrfs_root *root,
  59. struct btrfs_trans_handle *trans,
  60. struct btrfs_path *path);
  61. int load_free_ino_cache(struct btrfs_fs_info *fs_info,
  62. struct btrfs_root *root);
  63. int btrfs_write_out_ino_cache(struct btrfs_root *root,
  64. struct btrfs_trans_handle *trans,
  65. struct btrfs_path *path,
  66. struct inode *inode);
  67. void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group);
  68. int __btrfs_add_free_space(struct btrfs_fs_info *fs_info,
  69. struct btrfs_free_space_ctl *ctl,
  70. u64 bytenr, u64 size);
  71. static inline int
  72. btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
  73. u64 bytenr, u64 size)
  74. {
  75. return __btrfs_add_free_space(block_group->fs_info,
  76. block_group->free_space_ctl,
  77. bytenr, size);
  78. }
  79. int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
  80. u64 bytenr, u64 size);
  81. void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl);
  82. void btrfs_remove_free_space_cache(struct btrfs_block_group_cache
  83. *block_group);
  84. u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
  85. u64 offset, u64 bytes, u64 empty_size,
  86. u64 *max_extent_size);
  87. u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root);
  88. void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
  89. u64 bytes);
  90. int btrfs_find_space_cluster(struct btrfs_fs_info *fs_info,
  91. struct btrfs_block_group_cache *block_group,
  92. struct btrfs_free_cluster *cluster,
  93. u64 offset, u64 bytes, u64 empty_size);
  94. void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster);
  95. u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
  96. struct btrfs_free_cluster *cluster, u64 bytes,
  97. u64 min_start, u64 *max_extent_size);
  98. int btrfs_return_cluster_to_free_space(
  99. struct btrfs_block_group_cache *block_group,
  100. struct btrfs_free_cluster *cluster);
  101. int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
  102. u64 *trimmed, u64 start, u64 end, u64 minlen);
  103. /* Support functions for running our sanity tests */
  104. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  105. int test_add_free_space_entry(struct btrfs_block_group_cache *cache,
  106. u64 offset, u64 bytes, bool bitmap);
  107. int test_check_exists(struct btrfs_block_group_cache *cache,
  108. u64 offset, u64 bytes);
  109. #endif
  110. #endif