Line data Source code
1 : /* SPDX-License-Identifier: GPL-2.0 */
2 :
3 : #ifndef BTRFS_SPACE_INFO_H
4 : #define BTRFS_SPACE_INFO_H
5 :
6 : #include "volumes.h"
7 :
8 : /*
9 : * Different levels for to flush space when doing space reservations.
10 : *
11 : * The higher the level, the more methods we try to reclaim space.
12 : */
13 : enum btrfs_reserve_flush_enum {
14 : /* If we are in the transaction, we can't flush anything.*/
15 : BTRFS_RESERVE_NO_FLUSH,
16 :
17 : /*
18 : * Flush space by:
19 : * - Running delayed inode items
20 : * - Allocating a new chunk
21 : */
22 : BTRFS_RESERVE_FLUSH_LIMIT,
23 :
24 : /*
25 : * Flush space by:
26 : * - Running delayed inode items
27 : * - Running delayed refs
28 : * - Running delalloc and waiting for ordered extents
29 : * - Allocating a new chunk
30 : * - Committing transaction
31 : */
32 : BTRFS_RESERVE_FLUSH_EVICT,
33 :
34 : /*
35 : * Flush space by above mentioned methods and by:
36 : * - Running delayed iputs
37 : * - Committing transaction
38 : *
39 : * Can be interrupted by a fatal signal.
40 : */
41 : BTRFS_RESERVE_FLUSH_DATA,
42 : BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE,
43 : BTRFS_RESERVE_FLUSH_ALL,
44 :
45 : /*
46 : * Pretty much the same as FLUSH_ALL, but can also steal space from
47 : * global rsv.
48 : *
49 : * Can be interrupted by a fatal signal.
50 : */
51 : BTRFS_RESERVE_FLUSH_ALL_STEAL,
52 :
53 : /*
54 : * This is for btrfs_use_block_rsv only. We have exhausted our block
55 : * rsv and our global block rsv. This can happen for things like
56 : * delalloc where we are overwriting a lot of extents with a single
57 : * extent and didn't reserve enough space. Alternatively it can happen
58 : * with delalloc where we reserve 1 extents worth for a large extent but
59 : * fragmentation leads to multiple extents being created. This will
60 : * give us the reservation in the case of
61 : *
62 : * if (num_bytes < (space_info->total_bytes -
63 : * btrfs_space_info_used(space_info, false))
64 : *
65 : * Which ignores bytes_may_use. This is potentially dangerous, but our
66 : * reservation system is generally pessimistic so is able to absorb this
67 : * style of mistake.
68 : */
69 : BTRFS_RESERVE_FLUSH_EMERGENCY,
70 : };
71 :
72 : enum btrfs_flush_state {
73 : FLUSH_DELAYED_ITEMS_NR = 1,
74 : FLUSH_DELAYED_ITEMS = 2,
75 : FLUSH_DELAYED_REFS_NR = 3,
76 : FLUSH_DELAYED_REFS = 4,
77 : FLUSH_DELALLOC = 5,
78 : FLUSH_DELALLOC_WAIT = 6,
79 : FLUSH_DELALLOC_FULL = 7,
80 : ALLOC_CHUNK = 8,
81 : ALLOC_CHUNK_FORCE = 9,
82 : RUN_DELAYED_IPUTS = 10,
83 : COMMIT_TRANS = 11,
84 : };
85 :
86 : struct btrfs_space_info {
87 : spinlock_t lock;
88 :
89 : u64 total_bytes; /* total bytes in the space,
90 : this doesn't take mirrors into account */
91 : u64 bytes_used; /* total bytes used,
92 : this doesn't take mirrors into account */
93 : u64 bytes_pinned; /* total bytes pinned, will be freed when the
94 : transaction finishes */
95 : u64 bytes_reserved; /* total bytes the allocator has reserved for
96 : current allocations */
97 : u64 bytes_may_use; /* number of bytes that may be used for
98 : delalloc/allocations */
99 : u64 bytes_readonly; /* total bytes that are read only */
100 : u64 bytes_zone_unusable; /* total bytes that are unusable until
101 : resetting the device zone */
102 :
103 : u64 max_extent_size; /* This will hold the maximum extent size of
104 : the space info if we had an ENOSPC in the
105 : allocator. */
106 : /* Chunk size in bytes */
107 : u64 chunk_size;
108 :
109 : /*
110 : * Once a block group drops below this threshold (percents) we'll
111 : * schedule it for reclaim.
112 : */
113 : int bg_reclaim_threshold;
114 :
115 : int clamp; /* Used to scale our threshold for preemptive
116 : flushing. The value is >> clamp, so turns
117 : out to be a 2^clamp divisor. */
118 :
119 : unsigned int full:1; /* indicates that we cannot allocate any more
120 : chunks for this space */
121 : unsigned int chunk_alloc:1; /* set if we are allocating a chunk */
122 :
123 : unsigned int flush:1; /* set if we are trying to make space */
124 :
125 : unsigned int force_alloc; /* set if we need to force a chunk
126 : alloc for this space */
127 :
128 : u64 disk_used; /* total bytes used on disk */
129 : u64 disk_total; /* total bytes on disk, takes mirrors into
130 : account */
131 :
132 : u64 flags;
133 :
134 : struct list_head list;
135 : /* Protected by the spinlock 'lock'. */
136 : struct list_head ro_bgs;
137 : struct list_head priority_tickets;
138 : struct list_head tickets;
139 :
140 : /*
141 : * Size of space that needs to be reclaimed in order to satisfy pending
142 : * tickets
143 : */
144 : u64 reclaim_size;
145 :
146 : /*
147 : * tickets_id just indicates the next ticket will be handled, so note
148 : * it's not stored per ticket.
149 : */
150 : u64 tickets_id;
151 :
152 : struct rw_semaphore groups_sem;
153 : /* for block groups in our same type */
154 : struct list_head block_groups[BTRFS_NR_RAID_TYPES];
155 :
156 : struct kobject kobj;
157 : struct kobject *block_group_kobjs[BTRFS_NR_RAID_TYPES];
158 : };
159 :
160 : struct reserve_ticket {
161 : u64 bytes;
162 : int error;
163 : bool steal;
164 : struct list_head list;
165 : wait_queue_head_t wait;
166 : };
167 :
168 : static inline bool btrfs_mixed_space_info(struct btrfs_space_info *space_info)
169 : {
170 17208670 : return ((space_info->flags & BTRFS_BLOCK_GROUP_METADATA) &&
171 : (space_info->flags & BTRFS_BLOCK_GROUP_DATA));
172 : }
173 :
174 : /*
175 : *
176 : * Declare a helper function to detect underflow of various space info members
177 : */
178 : #define DECLARE_SPACE_INFO_UPDATE(name, trace_name) \
179 : static inline void \
180 : btrfs_space_info_update_##name(struct btrfs_fs_info *fs_info, \
181 : struct btrfs_space_info *sinfo, \
182 : s64 bytes) \
183 : { \
184 : const u64 abs_bytes = (bytes < 0) ? -bytes : bytes; \
185 : lockdep_assert_held(&sinfo->lock); \
186 : trace_update_##name(fs_info, sinfo, sinfo->name, bytes); \
187 : trace_btrfs_space_reservation(fs_info, trace_name, \
188 : sinfo->flags, abs_bytes, \
189 : bytes > 0); \
190 : if (bytes < 0 && sinfo->name < -bytes) { \
191 : WARN_ON(1); \
192 : sinfo->name = 0; \
193 : return; \
194 : } \
195 : sinfo->name += bytes; \
196 : }
197 :
198 270775920 : DECLARE_SPACE_INFO_UPDATE(bytes_may_use, "space_info");
199 15794084 : DECLARE_SPACE_INFO_UPDATE(bytes_pinned, "pinned");
200 :
201 : int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
202 : void btrfs_add_bg_to_space_info(struct btrfs_fs_info *info,
203 : struct btrfs_block_group *block_group);
204 : void btrfs_update_space_info_chunk_size(struct btrfs_space_info *space_info,
205 : u64 chunk_size);
206 : struct btrfs_space_info *btrfs_find_space_info(struct btrfs_fs_info *info,
207 : u64 flags);
208 : u64 __pure btrfs_space_info_used(struct btrfs_space_info *s_info,
209 : bool may_use_included);
210 : void btrfs_clear_space_info_full(struct btrfs_fs_info *info);
211 : void btrfs_dump_space_info(struct btrfs_fs_info *fs_info,
212 : struct btrfs_space_info *info, u64 bytes,
213 : int dump_block_groups);
214 : int btrfs_reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
215 : struct btrfs_block_rsv *block_rsv,
216 : u64 orig_bytes,
217 : enum btrfs_reserve_flush_enum flush);
218 : void btrfs_try_granting_tickets(struct btrfs_fs_info *fs_info,
219 : struct btrfs_space_info *space_info);
220 : int btrfs_can_overcommit(struct btrfs_fs_info *fs_info,
221 : struct btrfs_space_info *space_info, u64 bytes,
222 : enum btrfs_reserve_flush_enum flush);
223 :
224 138669856 : static inline void btrfs_space_info_free_bytes_may_use(
225 : struct btrfs_fs_info *fs_info,
226 : struct btrfs_space_info *space_info,
227 : u64 num_bytes)
228 : {
229 138669856 : spin_lock(&space_info->lock);
230 138815283 : btrfs_space_info_update_bytes_may_use(fs_info, space_info, -num_bytes);
231 138815274 : btrfs_try_granting_tickets(fs_info, space_info);
232 138815270 : spin_unlock(&space_info->lock);
233 138814260 : }
234 : int btrfs_reserve_data_bytes(struct btrfs_fs_info *fs_info, u64 bytes,
235 : enum btrfs_reserve_flush_enum flush);
236 : void btrfs_dump_space_info_for_trans_abort(struct btrfs_fs_info *fs_info);
237 : void btrfs_init_async_reclaim_work(struct btrfs_fs_info *fs_info);
238 : u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo);
239 :
240 : #endif /* BTRFS_SPACE_INFO_H */
|