1 /* SPDX-License-Identifier: GPL-2.0 */
3 #ifndef BTRFS_SPACE_INFO_H
4 #define BTRFS_SPACE_INFO_H
6 #include <trace/events/btrfs.h>
10 * Different levels for to flush space when doing space reservations.
12 * The higher the level, the more methods we try to reclaim space.
14 enum btrfs_reserve_flush_enum {
15 /* If we are in the transaction, we can't flush anything.*/
16 BTRFS_RESERVE_NO_FLUSH,
20 * - Running delayed inode items
21 * - Allocating a new chunk
23 BTRFS_RESERVE_FLUSH_LIMIT,
27 * - Running delayed inode items
28 * - Running delayed refs
29 * - Running delalloc and waiting for ordered extents
30 * - Allocating a new chunk
31 * - Committing transaction
33 BTRFS_RESERVE_FLUSH_EVICT,
36 * Flush space by above mentioned methods and by:
37 * - Running delayed iputs
38 * - Committing transaction
40 * Can be interrupted by a fatal signal.
42 BTRFS_RESERVE_FLUSH_DATA,
43 BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE,
44 BTRFS_RESERVE_FLUSH_ALL,
47 * Pretty much the same as FLUSH_ALL, but can also steal space from
50 * Can be interrupted by a fatal signal.
52 BTRFS_RESERVE_FLUSH_ALL_STEAL,
55 * This is for btrfs_use_block_rsv only. We have exhausted our block
56 * rsv and our global block rsv. This can happen for things like
57 * delalloc where we are overwriting a lot of extents with a single
58 * extent and didn't reserve enough space. Alternatively it can happen
59 * with delalloc where we reserve 1 extents worth for a large extent but
60 * fragmentation leads to multiple extents being created. This will
61 * give us the reservation in the case of
63 * if (num_bytes < (space_info->total_bytes -
64 * btrfs_space_info_used(space_info, false))
66 * Which ignores bytes_may_use. This is potentially dangerous, but our
67 * reservation system is generally pessimistic so is able to absorb this
70 BTRFS_RESERVE_FLUSH_EMERGENCY,
73 enum btrfs_flush_state {
74 FLUSH_DELAYED_ITEMS_NR = 1,
75 FLUSH_DELAYED_ITEMS = 2,
76 FLUSH_DELAYED_REFS_NR = 3,
77 FLUSH_DELAYED_REFS = 4,
79 FLUSH_DELALLOC_WAIT = 6,
80 FLUSH_DELALLOC_FULL = 7,
82 ALLOC_CHUNK_FORCE = 9,
83 RUN_DELAYED_IPUTS = 10,
87 struct btrfs_space_info {
90 u64 total_bytes; /* total bytes in the space,
91 this doesn't take mirrors into account */
92 u64 bytes_used; /* total bytes used,
93 this doesn't take mirrors into account */
94 u64 bytes_pinned; /* total bytes pinned, will be freed when the
95 transaction finishes */
96 u64 bytes_reserved; /* total bytes the allocator has reserved for
97 current allocations */
98 u64 bytes_may_use; /* number of bytes that may be used for
99 delalloc/allocations */
100 u64 bytes_readonly; /* total bytes that are read only */
101 u64 bytes_zone_unusable; /* total bytes that are unusable until
102 resetting the device zone */
104 u64 max_extent_size; /* This will hold the maximum extent size of
105 the space info if we had an ENOSPC in the
107 /* Chunk size in bytes */
111 * Once a block group drops below this threshold (percents) we'll
112 * schedule it for reclaim.
114 int bg_reclaim_threshold;
116 int clamp; /* Used to scale our threshold for preemptive
117 flushing. The value is >> clamp, so turns
118 out to be a 2^clamp divisor. */
120 unsigned int full:1; /* indicates that we cannot allocate any more
121 chunks for this space */
122 unsigned int chunk_alloc:1; /* set if we are allocating a chunk */
124 unsigned int flush:1; /* set if we are trying to make space */
126 unsigned int force_alloc; /* set if we need to force a chunk
127 alloc for this space */
129 u64 disk_used; /* total bytes used on disk */
130 u64 disk_total; /* total bytes on disk, takes mirrors into
135 struct list_head list;
136 /* Protected by the spinlock 'lock'. */
137 struct list_head ro_bgs;
138 struct list_head priority_tickets;
139 struct list_head tickets;
142 * Size of space that needs to be reclaimed in order to satisfy pending
148 * tickets_id just indicates the next ticket will be handled, so note
149 * it's not stored per ticket.
153 struct rw_semaphore groups_sem;
154 /* for block groups in our same type */
155 struct list_head block_groups[BTRFS_NR_RAID_TYPES];
158 struct kobject *block_group_kobjs[BTRFS_NR_RAID_TYPES];
161 struct reserve_ticket {
165 struct list_head list;
166 wait_queue_head_t wait;
169 static inline bool btrfs_mixed_space_info(struct btrfs_space_info *space_info)
171 return ((space_info->flags & BTRFS_BLOCK_GROUP_METADATA) &&
172 (space_info->flags & BTRFS_BLOCK_GROUP_DATA));
177 * Declare a helper function to detect underflow of various space info members
179 #define DECLARE_SPACE_INFO_UPDATE(name, trace_name) \
181 btrfs_space_info_update_##name(struct btrfs_fs_info *fs_info, \
182 struct btrfs_space_info *sinfo, \
185 const u64 abs_bytes = (bytes < 0) ? -bytes : bytes; \
186 lockdep_assert_held(&sinfo->lock); \
187 trace_update_##name(fs_info, sinfo, sinfo->name, bytes); \
188 trace_btrfs_space_reservation(fs_info, trace_name, \
189 sinfo->flags, abs_bytes, \
191 if (bytes < 0 && sinfo->name < -bytes) { \
196 sinfo->name += bytes; \
199 DECLARE_SPACE_INFO_UPDATE(bytes_may_use, "space_info");
200 DECLARE_SPACE_INFO_UPDATE(bytes_pinned, "pinned");
202 int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
203 void btrfs_add_bg_to_space_info(struct btrfs_fs_info *info,
204 struct btrfs_block_group *block_group);
205 void btrfs_update_space_info_chunk_size(struct btrfs_space_info *space_info,
207 struct btrfs_space_info *btrfs_find_space_info(struct btrfs_fs_info *info,
209 u64 __pure btrfs_space_info_used(struct btrfs_space_info *s_info,
210 bool may_use_included);
211 void btrfs_clear_space_info_full(struct btrfs_fs_info *info);
212 void btrfs_dump_space_info(struct btrfs_fs_info *fs_info,
213 struct btrfs_space_info *info, u64 bytes,
214 int dump_block_groups);
215 int btrfs_reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
216 struct btrfs_space_info *space_info,
218 enum btrfs_reserve_flush_enum flush);
219 void btrfs_try_granting_tickets(struct btrfs_fs_info *fs_info,
220 struct btrfs_space_info *space_info);
221 int btrfs_can_overcommit(struct btrfs_fs_info *fs_info,
222 struct btrfs_space_info *space_info, u64 bytes,
223 enum btrfs_reserve_flush_enum flush);
225 static inline void btrfs_space_info_free_bytes_may_use(
226 struct btrfs_fs_info *fs_info,
227 struct btrfs_space_info *space_info,
230 spin_lock(&space_info->lock);
231 btrfs_space_info_update_bytes_may_use(fs_info, space_info, -num_bytes);
232 btrfs_try_granting_tickets(fs_info, space_info);
233 spin_unlock(&space_info->lock);
235 int btrfs_reserve_data_bytes(struct btrfs_fs_info *fs_info, u64 bytes,
236 enum btrfs_reserve_flush_enum flush);
237 void btrfs_dump_space_info_for_trans_abort(struct btrfs_fs_info *fs_info);
238 void btrfs_init_async_reclaim_work(struct btrfs_fs_info *fs_info);
239 u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo);
241 #endif /* BTRFS_SPACE_INFO_H */