| 1 |
/* SPDX-License-Identifier: GPL-2.0 */ |
| 2 |
|
| 3 |
#ifndef BTRFS_SPACE_INFO_H |
| 4 |
#define BTRFS_SPACE_INFO_H |
| 5 |
|
| 6 |
#include <trace/events/btrfs.h> |
| 7 |
#include <linux/spinlock.h> |
| 8 |
#include <linux/list.h> |
| 9 |
#include <linux/kobject.h> |
| 10 |
#include <linux/lockdep.h> |
| 11 |
#include <linux/wait.h> |
| 12 |
#include <linux/rwsem.h> |
| 13 |
#include "volumes.h" |
| 14 |
|
| 15 |
struct btrfs_fs_info; |
| 16 |
struct btrfs_block_group; |
| 17 |
|
| 18 |
/* |
| 19 |
* Different levels for to flush space when doing space reservations. |
| 20 |
* |
| 21 |
* The higher the level, the more methods we try to reclaim space. |
| 22 |
*/ |
| 23 |
enum btrfs_reserve_flush_enum { |
| 24 |
/* If we are in the transaction, we can't flush anything.*/ |
| 25 |
BTRFS_RESERVE_NO_FLUSH, |
| 26 |
|
| 27 |
/* |
| 28 |
* Flush space by: |
| 29 |
* - Running delayed inode items |
| 30 |
* - Allocating a new chunk |
| 31 |
*/ |
| 32 |
BTRFS_RESERVE_FLUSH_LIMIT, |
| 33 |
|
| 34 |
/* |
| 35 |
* Flush space by: |
| 36 |
* - Running delayed inode items |
| 37 |
* - Running delayed refs |
| 38 |
* - Running delalloc and waiting for ordered extents |
| 39 |
* - Allocating a new chunk |
| 40 |
* - Committing transaction |
| 41 |
*/ |
| 42 |
BTRFS_RESERVE_FLUSH_EVICT, |
| 43 |
|
| 44 |
/* |
| 45 |
* Flush space by above mentioned methods and by: |
| 46 |
* - Running delayed iputs |
| 47 |
* - Committing transaction |
| 48 |
* |
| 49 |
* Can be interrupted by a fatal signal. |
| 50 |
*/ |
| 51 |
BTRFS_RESERVE_FLUSH_DATA, |
| 52 |
BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE, |
| 53 |
BTRFS_RESERVE_FLUSH_ALL, |
| 54 |
|
| 55 |
/* |
| 56 |
* Pretty much the same as FLUSH_ALL, but can also steal space from |
| 57 |
* global rsv. |
| 58 |
* |
| 59 |
* Can be interrupted by a fatal signal. |
| 60 |
*/ |
| 61 |
BTRFS_RESERVE_FLUSH_ALL_STEAL, |
| 62 |
|
| 63 |
/* |
| 64 |
* This is for btrfs_use_block_rsv only. We have exhausted our block |
| 65 |
* rsv and our global block rsv. This can happen for things like |
| 66 |
* delalloc where we are overwriting a lot of extents with a single |
| 67 |
* extent and didn't reserve enough space. Alternatively it can happen |
| 68 |
* with delalloc where we reserve 1 extents worth for a large extent but |
| 69 |
* fragmentation leads to multiple extents being created. This will |
| 70 |
* give us the reservation in the case of |
| 71 |
* |
| 72 |
* if (num_bytes < (space_info->total_bytes - |
| 73 |
* btrfs_space_info_used(space_info, false)) |
| 74 |
* |
| 75 |
* Which ignores bytes_may_use. This is potentially dangerous, but our |
| 76 |
* reservation system is generally pessimistic so is able to absorb this |
| 77 |
* style of mistake. |
| 78 |
*/ |
| 79 |
BTRFS_RESERVE_FLUSH_EMERGENCY, |
| 80 |
}; |
| 81 |
|
| 82 |
/* |
| 83 |
* Please be aware that the order of enum values will be the order of the reclaim |
| 84 |
* process in btrfs_async_reclaim_metadata_space(). |
| 85 |
*/ |
| 86 |
enum btrfs_flush_state { |
| 87 |
FLUSH_DELAYED_ITEMS_NR = 1, |
| 88 |
FLUSH_DELAYED_ITEMS = 2, |
| 89 |
FLUSH_DELAYED_REFS_NR = 3, |
| 90 |
FLUSH_DELAYED_REFS = 4, |
| 91 |
FLUSH_DELALLOC = 5, |
| 92 |
FLUSH_DELALLOC_WAIT = 6, |
| 93 |
FLUSH_DELALLOC_FULL = 7, |
| 94 |
ALLOC_CHUNK = 8, |
| 95 |
ALLOC_CHUNK_FORCE = 9, |
| 96 |
RUN_DELAYED_IPUTS = 10, |
| 97 |
COMMIT_TRANS = 11, |
| 98 |
RESET_ZONES = 12, |
| 99 |
}; |
| 100 |
|
| 101 |
enum btrfs_space_info_sub_group { |
| 102 |
BTRFS_SUB_GROUP_PRIMARY, |
| 103 |
BTRFS_SUB_GROUP_DATA_RELOC, |
| 104 |
BTRFS_SUB_GROUP_TREELOG, |
| 105 |
}; |
| 106 |
|
| 107 |
#define BTRFS_SPACE_INFO_SUB_GROUP_MAX 1 |
| 108 |
struct btrfs_space_info { |
| 109 |
struct btrfs_fs_info *fs_info; |
| 110 |
struct btrfs_space_info *parent; |
| 111 |
struct btrfs_space_info *sub_group[BTRFS_SPACE_INFO_SUB_GROUP_MAX]; |
| 112 |
int subgroup_id; |
| 113 |
spinlock_t lock; |
| 114 |
|
| 115 |
u64 total_bytes; /* total bytes in the space, |
| 116 |
this doesn't take mirrors into account */ |
| 117 |
u64 bytes_used; /* total bytes used, |
| 118 |
this doesn't take mirrors into account */ |
| 119 |
u64 bytes_pinned; /* total bytes pinned, will be freed when the |
| 120 |
transaction finishes */ |
| 121 |
u64 bytes_reserved; /* total bytes the allocator has reserved for |
| 122 |
current allocations */ |
| 123 |
u64 bytes_may_use; /* number of bytes that may be used for |
| 124 |
delalloc/allocations */ |
| 125 |
u64 bytes_readonly; /* total bytes that are read only */ |
| 126 |
u64 bytes_zone_unusable; /* total bytes that are unusable until |
| 127 |
resetting the device zone */ |
| 128 |
|
| 129 |
u64 max_extent_size; /* This will hold the maximum extent size of |
| 130 |
the space info if we had an ENOSPC in the |
| 131 |
allocator. */ |
| 132 |
/* Chunk size in bytes */ |
| 133 |
u64 chunk_size; |
| 134 |
|
| 135 |
/* |
| 136 |
* Once a block group drops below this threshold (percents) we'll |
| 137 |
* schedule it for reclaim. |
| 138 |
*/ |
| 139 |
int bg_reclaim_threshold; |
| 140 |
|
| 141 |
int clamp; /* Used to scale our threshold for preemptive |
| 142 |
flushing. The value is >> clamp, so turns |
| 143 |
out to be a 2^clamp divisor. */ |
| 144 |
|
| 145 |
bool full; /* indicates that we cannot allocate any more |
| 146 |
chunks for this space */ |
| 147 |
bool chunk_alloc; /* set if we are allocating a chunk */ |
| 148 |
|
| 149 |
bool flush; /* set if we are trying to make space */ |
| 150 |
|
| 151 |
unsigned int force_alloc; /* set if we need to force a chunk |
| 152 |
alloc for this space */ |
| 153 |
|
| 154 |
u64 disk_used; /* total bytes used on disk */ |
| 155 |
u64 disk_total; /* total bytes on disk, takes mirrors into |
| 156 |
account */ |
| 157 |
|
| 158 |
u64 flags; |
| 159 |
|
| 160 |
struct list_head list; |
| 161 |
/* Protected by the spinlock 'lock'. */ |
| 162 |
struct list_head ro_bgs; |
| 163 |
struct list_head priority_tickets; |
| 164 |
struct list_head tickets; |
| 165 |
|
| 166 |
/* |
| 167 |
* Size of space that needs to be reclaimed in order to satisfy pending |
| 168 |
* tickets |
| 169 |
*/ |
| 170 |
u64 reclaim_size; |
| 171 |
|
| 172 |
/* |
| 173 |
* tickets_id just indicates the next ticket will be handled, so note |
| 174 |
* it's not stored per ticket. |
| 175 |
*/ |
| 176 |
u64 tickets_id; |
| 177 |
|
| 178 |
struct rw_semaphore groups_sem; |
| 179 |
/* for block groups in our same type */ |
| 180 |
struct list_head block_groups[BTRFS_NR_RAID_TYPES]; |
| 181 |
|
| 182 |
struct kobject kobj; |
| 183 |
struct kobject *block_group_kobjs[BTRFS_NR_RAID_TYPES]; |
| 184 |
|
| 185 |
/* |
| 186 |
* Monotonically increasing counter of block group reclaim attempts |
| 187 |
* Exposed in /sys/fs/<uuid>/allocation/<type>/reclaim_count |
| 188 |
*/ |
| 189 |
u64 reclaim_count; |
| 190 |
|
| 191 |
/* |
| 192 |
* Monotonically increasing counter of reclaimed bytes |
| 193 |
* Exposed in /sys/fs/<uuid>/allocation/<type>/reclaim_bytes |
| 194 |
*/ |
| 195 |
u64 reclaim_bytes; |
| 196 |
|
| 197 |
/* |
| 198 |
* Monotonically increasing counter of reclaim errors |
| 199 |
* Exposed in /sys/fs/<uuid>/allocation/<type>/reclaim_errors |
| 200 |
*/ |
| 201 |
u64 reclaim_errors; |
| 202 |
|
| 203 |
/* |
| 204 |
* If true, use the dynamic relocation threshold, instead of the |
| 205 |
* fixed bg_reclaim_threshold. |
| 206 |
*/ |
| 207 |
bool dynamic_reclaim; |
| 208 |
|
| 209 |
/* |
| 210 |
* Periodically check all block groups against the reclaim |
| 211 |
* threshold in the cleaner thread. |
| 212 |
*/ |
| 213 |
bool periodic_reclaim; |
| 214 |
|
| 215 |
/* |
| 216 |
* Periodic reclaim should be a no-op if a space_info hasn't |
| 217 |
* freed any space since the last time we tried. |
| 218 |
*/ |
| 219 |
bool periodic_reclaim_ready; |
| 220 |
|
| 221 |
/* |
| 222 |
* Net bytes freed or allocated since the last reclaim pass. |
| 223 |
*/ |
| 224 |
s64 reclaimable_bytes; |
| 225 |
}; |
| 226 |
|
| 227 |
static inline bool btrfs_mixed_space_info(const struct btrfs_space_info *space_info) |
| 228 |
{ |
| 229 |
return ((space_info->flags & BTRFS_BLOCK_GROUP_METADATA) && |
| 230 |
(space_info->flags & BTRFS_BLOCK_GROUP_DATA)); |
| 231 |
} |
| 232 |
|
| 233 |
/* |
| 234 |
* |
| 235 |
* Declare a helper function to detect underflow of various space info members |
| 236 |
*/ |
| 237 |
#define DECLARE_SPACE_INFO_UPDATE(name, trace_name) \ |
| 238 |
static inline void \ |
| 239 |
btrfs_space_info_update_##name(struct btrfs_space_info *sinfo, \ |
| 240 |
s64 bytes) \ |
| 241 |
{ \ |
| 242 |
struct btrfs_fs_info *fs_info = sinfo->fs_info; \ |
| 243 |
const u64 abs_bytes = (bytes < 0) ? -bytes : bytes; \ |
| 244 |
lockdep_assert_held(&sinfo->lock); \ |
| 245 |
trace_update_##name(fs_info, sinfo, sinfo->name, bytes); \ |
| 246 |
trace_btrfs_space_reservation(fs_info, trace_name, \ |
| 247 |
sinfo->flags, abs_bytes, \ |
| 248 |
bytes > 0); \ |
| 249 |
if (bytes < 0 && sinfo->name < -bytes) { \ |
| 250 |
WARN_ON(1); \ |
| 251 |
sinfo->name = 0; \ |
| 252 |
return; \ |
| 253 |
} \ |
| 254 |
sinfo->name += bytes; \ |
| 255 |
} |
| 256 |
|
| 257 |
DECLARE_SPACE_INFO_UPDATE(bytes_may_use, "space_info"); |
| 258 |
DECLARE_SPACE_INFO_UPDATE(bytes_pinned, "pinned"); |
| 259 |
DECLARE_SPACE_INFO_UPDATE(bytes_zone_unusable, "zone_unusable"); |
| 260 |
|
| 261 |
static inline u64 btrfs_space_info_used(const struct btrfs_space_info *s_info, |
| 262 |
bool may_use_included) |
| 263 |
{ |
| 264 |
lockdep_assert_held(&s_info->lock); |
| 265 |
|
| 266 |
return s_info->bytes_used + s_info->bytes_reserved + |
| 267 |
s_info->bytes_pinned + s_info->bytes_readonly + |
| 268 |
s_info->bytes_zone_unusable + |
| 269 |
(may_use_included ? s_info->bytes_may_use : 0); |
| 270 |
} |
| 271 |
|
| 272 |
int btrfs_init_space_info(struct btrfs_fs_info *fs_info); |
| 273 |
void btrfs_add_bg_to_space_info(struct btrfs_fs_info *info, |
| 274 |
struct btrfs_block_group *block_group); |
| 275 |
void btrfs_update_space_info_chunk_size(struct btrfs_space_info *space_info, |
| 276 |
u64 chunk_size); |
| 277 |
struct btrfs_space_info *btrfs_find_space_info(struct btrfs_fs_info *info, |
| 278 |
u64 flags); |
| 279 |
void btrfs_clear_space_info_full(struct btrfs_fs_info *info); |
| 280 |
void btrfs_dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 281 |
bool dump_block_groups); |
| 282 |
int btrfs_reserve_metadata_bytes(struct btrfs_space_info *space_info, |
| 283 |
u64 orig_bytes, |
| 284 |
enum btrfs_reserve_flush_enum flush); |
| 285 |
void btrfs_try_granting_tickets(struct btrfs_space_info *space_info); |
| 286 |
bool btrfs_can_overcommit(const struct btrfs_space_info *space_info, u64 bytes, |
| 287 |
enum btrfs_reserve_flush_enum flush); |
| 288 |
|
| 289 |
static inline void btrfs_space_info_free_bytes_may_use( |
| 290 |
struct btrfs_space_info *space_info, |
| 291 |
u64 num_bytes) |
| 292 |
{ |
| 293 |
spin_lock(&space_info->lock); |
| 294 |
btrfs_space_info_update_bytes_may_use(space_info, -num_bytes); |
| 295 |
btrfs_try_granting_tickets(space_info); |
| 296 |
spin_unlock(&space_info->lock); |
| 297 |
} |
| 298 |
int btrfs_reserve_data_bytes(struct btrfs_space_info *space_info, u64 bytes, |
| 299 |
enum btrfs_reserve_flush_enum flush); |
| 300 |
void btrfs_dump_space_info_for_trans_abort(struct btrfs_fs_info *fs_info); |
| 301 |
void btrfs_init_async_reclaim_work(struct btrfs_fs_info *fs_info); |
| 302 |
u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo); |
| 303 |
|
| 304 |
void btrfs_space_info_update_reclaimable(struct btrfs_space_info *space_info, s64 bytes); |
| 305 |
void btrfs_set_periodic_reclaim_ready(struct btrfs_space_info *space_info, bool ready); |
| 306 |
int btrfs_calc_reclaim_threshold(const struct btrfs_space_info *space_info); |
| 307 |
void btrfs_reclaim_sweep(const struct btrfs_fs_info *fs_info); |
| 308 |
void btrfs_return_free_space(struct btrfs_space_info *space_info, u64 len); |
| 309 |
|
| 310 |
static inline const char *btrfs_space_info_type_str(const struct btrfs_space_info *space_info) |
| 311 |
{ |
| 312 |
switch (space_info->flags) { |
| 313 |
case BTRFS_BLOCK_GROUP_SYSTEM: |
| 314 |
return "SYSTEM"; |
| 315 |
case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA: |
| 316 |
return "DATA+METADATA"; |
| 317 |
case BTRFS_BLOCK_GROUP_DATA: |
| 318 |
return "DATA"; |
| 319 |
case BTRFS_BLOCK_GROUP_METADATA: |
| 320 |
return "METADATA"; |
| 321 |
default: |
| 322 |
return "UNKNOWN"; |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
#endif /* BTRFS_SPACE_INFO_H */ |