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