space-info.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef BTRFS_SPACE_INFO_H
  3. #define BTRFS_SPACE_INFO_H
  4. struct btrfs_space_info {
  5. spinlock_t lock;
  6. u64 total_bytes; /* total bytes in the space,
  7. this doesn't take mirrors into account */
  8. u64 bytes_used; /* total bytes used,
  9. this doesn't take mirrors into account */
  10. u64 bytes_pinned; /* total bytes pinned, will be freed when the
  11. transaction finishes */
  12. u64 bytes_reserved; /* total bytes the allocator has reserved for
  13. current allocations */
  14. u64 bytes_may_use; /* number of bytes that may be used for
  15. delalloc/allocations */
  16. u64 bytes_readonly; /* total bytes that are read only */
  17. u64 max_extent_size; /* This will hold the maximum extent size of
  18. the space info if we had an ENOSPC in the
  19. allocator. */
  20. unsigned int full:1; /* indicates that we cannot allocate any more
  21. chunks for this space */
  22. unsigned int chunk_alloc:1; /* set if we are allocating a chunk */
  23. unsigned int flush:1; /* set if we are trying to make space */
  24. unsigned int force_alloc; /* set if we need to force a chunk
  25. alloc for this space */
  26. u64 disk_used; /* total bytes used on disk */
  27. u64 disk_total; /* total bytes on disk, takes mirrors into
  28. account */
  29. u64 flags;
  30. /*
  31. * bytes_pinned is kept in line with what is actually pinned, as in
  32. * we've called update_block_group and dropped the bytes_used counter
  33. * and increased the bytes_pinned counter. However this means that
  34. * bytes_pinned does not reflect the bytes that will be pinned once the
  35. * delayed refs are flushed, so this counter is inc'ed every time we
  36. * call btrfs_free_extent so it is a realtime count of what will be
  37. * freed once the transaction is committed. It will be zeroed every
  38. * time the transaction commits.
  39. */
  40. struct percpu_counter total_bytes_pinned;
  41. struct list_head list;
  42. /* Protected by the spinlock 'lock'. */
  43. struct list_head ro_bgs;
  44. struct list_head priority_tickets;
  45. struct list_head tickets;
  46. /*
  47. * Size of space that needs to be reclaimed in order to satisfy pending
  48. * tickets
  49. */
  50. u64 reclaim_size;
  51. /*
  52. * tickets_id just indicates the next ticket will be handled, so note
  53. * it's not stored per ticket.
  54. */
  55. u64 tickets_id;
  56. struct rw_semaphore groups_sem;
  57. /* for block groups in our same type */
  58. struct list_head block_groups[BTRFS_NR_RAID_TYPES];
  59. struct kobject kobj;
  60. struct kobject *block_group_kobjs[BTRFS_NR_RAID_TYPES];
  61. };
  62. struct reserve_ticket {
  63. u64 bytes;
  64. int error;
  65. bool steal;
  66. struct list_head list;
  67. wait_queue_head_t wait;
  68. };
  69. static inline bool btrfs_mixed_space_info(struct btrfs_space_info *space_info)
  70. {
  71. return ((space_info->flags & BTRFS_BLOCK_GROUP_METADATA) &&
  72. (space_info->flags & BTRFS_BLOCK_GROUP_DATA));
  73. }
  74. /*
  75. *
  76. * Declare a helper function to detect underflow of various space info members
  77. */
  78. #define DECLARE_SPACE_INFO_UPDATE(name, trace_name) \
  79. static inline void \
  80. btrfs_space_info_update_##name(struct btrfs_fs_info *fs_info, \
  81. struct btrfs_space_info *sinfo, \
  82. s64 bytes) \
  83. { \
  84. const u64 abs_bytes = (bytes < 0) ? -bytes : bytes; \
  85. lockdep_assert_held(&sinfo->lock); \
  86. trace_update_##name(fs_info, sinfo, sinfo->name, bytes); \
  87. trace_btrfs_space_reservation(fs_info, trace_name, \
  88. sinfo->flags, abs_bytes, \
  89. bytes > 0); \
  90. if (bytes < 0 && sinfo->name < -bytes) { \
  91. WARN_ON(1); \
  92. sinfo->name = 0; \
  93. return; \
  94. } \
  95. sinfo->name += bytes; \
  96. }
  97. DECLARE_SPACE_INFO_UPDATE(bytes_may_use, "space_info");
  98. DECLARE_SPACE_INFO_UPDATE(bytes_pinned, "pinned");
  99. int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
  100. void btrfs_update_space_info(struct btrfs_fs_info *info, u64 flags,
  101. u64 total_bytes, u64 bytes_used,
  102. u64 bytes_readonly,
  103. struct btrfs_space_info **space_info);
  104. struct btrfs_space_info *btrfs_find_space_info(struct btrfs_fs_info *info,
  105. u64 flags);
  106. u64 __pure btrfs_space_info_used(struct btrfs_space_info *s_info,
  107. bool may_use_included);
  108. void btrfs_clear_space_info_full(struct btrfs_fs_info *info);
  109. void btrfs_dump_space_info(struct btrfs_fs_info *fs_info,
  110. struct btrfs_space_info *info, u64 bytes,
  111. int dump_block_groups);
  112. int btrfs_reserve_metadata_bytes(struct btrfs_root *root,
  113. struct btrfs_block_rsv *block_rsv,
  114. u64 orig_bytes,
  115. enum btrfs_reserve_flush_enum flush);
  116. void btrfs_try_granting_tickets(struct btrfs_fs_info *fs_info,
  117. struct btrfs_space_info *space_info);
  118. int btrfs_can_overcommit(struct btrfs_fs_info *fs_info,
  119. struct btrfs_space_info *space_info, u64 bytes,
  120. enum btrfs_reserve_flush_enum flush);
  121. static inline void btrfs_space_info_free_bytes_may_use(
  122. struct btrfs_fs_info *fs_info,
  123. struct btrfs_space_info *space_info,
  124. u64 num_bytes)
  125. {
  126. spin_lock(&space_info->lock);
  127. btrfs_space_info_update_bytes_may_use(fs_info, space_info, -num_bytes);
  128. btrfs_try_granting_tickets(fs_info, space_info);
  129. spin_unlock(&space_info->lock);
  130. }
  131. int btrfs_reserve_data_bytes(struct btrfs_fs_info *fs_info, u64 bytes,
  132. enum btrfs_reserve_flush_enum flush);
  133. static inline void __btrfs_mod_total_bytes_pinned(
  134. struct btrfs_space_info *space_info,
  135. s64 mod)
  136. {
  137. percpu_counter_add_batch(&space_info->total_bytes_pinned, mod,
  138. BTRFS_TOTAL_BYTES_PINNED_BATCH);
  139. }
  140. static inline void btrfs_mod_total_bytes_pinned(struct btrfs_fs_info *fs_info,
  141. u64 flags, s64 mod)
  142. {
  143. struct btrfs_space_info *space_info = btrfs_find_space_info(fs_info, flags);
  144. ASSERT(space_info);
  145. __btrfs_mod_total_bytes_pinned(space_info, mod);
  146. }
  147. #endif /* BTRFS_SPACE_INFO_H */