1 // SPDX-License-Identifier: GPL-2.0
6 #include "space-info.h"
7 #include "transaction.h"
9 static u64
block_rsv_release_bytes(struct btrfs_fs_info
*fs_info
,
10 struct btrfs_block_rsv
*block_rsv
,
11 struct btrfs_block_rsv
*dest
, u64 num_bytes
,
12 u64
*qgroup_to_release_ret
)
14 struct btrfs_space_info
*space_info
= block_rsv
->space_info
;
15 u64 qgroup_to_release
= 0;
18 spin_lock(&block_rsv
->lock
);
19 if (num_bytes
== (u64
)-1) {
20 num_bytes
= block_rsv
->size
;
21 qgroup_to_release
= block_rsv
->qgroup_rsv_size
;
23 block_rsv
->size
-= num_bytes
;
24 if (block_rsv
->reserved
>= block_rsv
->size
) {
25 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
26 block_rsv
->reserved
= block_rsv
->size
;
31 if (block_rsv
->qgroup_rsv_reserved
>= block_rsv
->qgroup_rsv_size
) {
32 qgroup_to_release
= block_rsv
->qgroup_rsv_reserved
-
33 block_rsv
->qgroup_rsv_size
;
34 block_rsv
->qgroup_rsv_reserved
= block_rsv
->qgroup_rsv_size
;
36 qgroup_to_release
= 0;
38 spin_unlock(&block_rsv
->lock
);
43 spin_lock(&dest
->lock
);
47 bytes_to_add
= dest
->size
- dest
->reserved
;
48 bytes_to_add
= min(num_bytes
, bytes_to_add
);
49 dest
->reserved
+= bytes_to_add
;
50 if (dest
->reserved
>= dest
->size
)
52 num_bytes
-= bytes_to_add
;
54 spin_unlock(&dest
->lock
);
57 btrfs_space_info_free_bytes_may_use(fs_info
,
61 if (qgroup_to_release_ret
)
62 *qgroup_to_release_ret
= qgroup_to_release
;
66 int btrfs_block_rsv_migrate(struct btrfs_block_rsv
*src
,
67 struct btrfs_block_rsv
*dst
, u64 num_bytes
,
72 ret
= btrfs_block_rsv_use_bytes(src
, num_bytes
);
76 btrfs_block_rsv_add_bytes(dst
, num_bytes
, update_size
);
80 void btrfs_init_block_rsv(struct btrfs_block_rsv
*rsv
, unsigned short type
)
82 memset(rsv
, 0, sizeof(*rsv
));
83 spin_lock_init(&rsv
->lock
);
87 void btrfs_init_metadata_block_rsv(struct btrfs_fs_info
*fs_info
,
88 struct btrfs_block_rsv
*rsv
,
91 btrfs_init_block_rsv(rsv
, type
);
92 rsv
->space_info
= btrfs_find_space_info(fs_info
,
93 BTRFS_BLOCK_GROUP_METADATA
);
96 struct btrfs_block_rsv
*btrfs_alloc_block_rsv(struct btrfs_fs_info
*fs_info
,
99 struct btrfs_block_rsv
*block_rsv
;
101 block_rsv
= kmalloc(sizeof(*block_rsv
), GFP_NOFS
);
105 btrfs_init_metadata_block_rsv(fs_info
, block_rsv
, type
);
109 void btrfs_free_block_rsv(struct btrfs_fs_info
*fs_info
,
110 struct btrfs_block_rsv
*rsv
)
114 btrfs_block_rsv_release(fs_info
, rsv
, (u64
)-1);
118 int btrfs_block_rsv_add(struct btrfs_root
*root
,
119 struct btrfs_block_rsv
*block_rsv
, u64 num_bytes
,
120 enum btrfs_reserve_flush_enum flush
)
127 ret
= btrfs_reserve_metadata_bytes(root
, block_rsv
, num_bytes
, flush
);
129 btrfs_block_rsv_add_bytes(block_rsv
, num_bytes
, true);
134 int btrfs_block_rsv_check(struct btrfs_block_rsv
*block_rsv
, int min_factor
)
142 spin_lock(&block_rsv
->lock
);
143 num_bytes
= div_factor(block_rsv
->size
, min_factor
);
144 if (block_rsv
->reserved
>= num_bytes
)
146 spin_unlock(&block_rsv
->lock
);
151 int btrfs_block_rsv_refill(struct btrfs_root
*root
,
152 struct btrfs_block_rsv
*block_rsv
, u64 min_reserved
,
153 enum btrfs_reserve_flush_enum flush
)
161 spin_lock(&block_rsv
->lock
);
162 num_bytes
= min_reserved
;
163 if (block_rsv
->reserved
>= num_bytes
)
166 num_bytes
-= block_rsv
->reserved
;
167 spin_unlock(&block_rsv
->lock
);
172 ret
= btrfs_reserve_metadata_bytes(root
, block_rsv
, num_bytes
, flush
);
174 btrfs_block_rsv_add_bytes(block_rsv
, num_bytes
, false);
181 u64
__btrfs_block_rsv_release(struct btrfs_fs_info
*fs_info
,
182 struct btrfs_block_rsv
*block_rsv
,
183 u64 num_bytes
, u64
*qgroup_to_release
)
185 struct btrfs_block_rsv
*global_rsv
= &fs_info
->global_block_rsv
;
186 struct btrfs_block_rsv
*delayed_rsv
= &fs_info
->delayed_refs_rsv
;
187 struct btrfs_block_rsv
*target
= NULL
;
190 * If we are the delayed_rsv then push to the global rsv, otherwise dump
191 * into the delayed rsv if it is not full.
193 if (block_rsv
== delayed_rsv
)
195 else if (block_rsv
!= global_rsv
&& !delayed_rsv
->full
)
196 target
= delayed_rsv
;
198 if (target
&& block_rsv
->space_info
!= target
->space_info
)
201 return block_rsv_release_bytes(fs_info
, block_rsv
, target
, num_bytes
,
205 int btrfs_block_rsv_use_bytes(struct btrfs_block_rsv
*block_rsv
, u64 num_bytes
)
209 spin_lock(&block_rsv
->lock
);
210 if (block_rsv
->reserved
>= num_bytes
) {
211 block_rsv
->reserved
-= num_bytes
;
212 if (block_rsv
->reserved
< block_rsv
->size
)
216 spin_unlock(&block_rsv
->lock
);
220 void btrfs_block_rsv_add_bytes(struct btrfs_block_rsv
*block_rsv
,
221 u64 num_bytes
, bool update_size
)
223 spin_lock(&block_rsv
->lock
);
224 block_rsv
->reserved
+= num_bytes
;
226 block_rsv
->size
+= num_bytes
;
227 else if (block_rsv
->reserved
>= block_rsv
->size
)
229 spin_unlock(&block_rsv
->lock
);
232 int btrfs_cond_migrate_bytes(struct btrfs_fs_info
*fs_info
,
233 struct btrfs_block_rsv
*dest
, u64 num_bytes
,
236 struct btrfs_block_rsv
*global_rsv
= &fs_info
->global_block_rsv
;
239 if (global_rsv
->space_info
!= dest
->space_info
)
242 spin_lock(&global_rsv
->lock
);
243 min_bytes
= div_factor(global_rsv
->size
, min_factor
);
244 if (global_rsv
->reserved
< min_bytes
+ num_bytes
) {
245 spin_unlock(&global_rsv
->lock
);
248 global_rsv
->reserved
-= num_bytes
;
249 if (global_rsv
->reserved
< global_rsv
->size
)
250 global_rsv
->full
= 0;
251 spin_unlock(&global_rsv
->lock
);
253 btrfs_block_rsv_add_bytes(dest
, num_bytes
, true);
257 void btrfs_update_global_block_rsv(struct btrfs_fs_info
*fs_info
)
259 struct btrfs_block_rsv
*block_rsv
= &fs_info
->global_block_rsv
;
260 struct btrfs_space_info
*sinfo
= block_rsv
->space_info
;
265 * The global block rsv is based on the size of the extent tree, the
266 * checksum tree and the root tree. If the fs is empty we want to set
267 * it to a minimal amount for safety.
269 num_bytes
= btrfs_root_used(&fs_info
->extent_root
->root_item
) +
270 btrfs_root_used(&fs_info
->csum_root
->root_item
) +
271 btrfs_root_used(&fs_info
->tree_root
->root_item
);
274 * We at a minimum are going to modify the csum root, the tree root, and
280 * But we also want to reserve enough space so we can do the fallback
281 * global reserve for an unlink, which is an additional 5 items (see the
282 * comment in __unlink_start_trans for what we're modifying.)
284 * But we also need space for the delayed ref updates from the unlink,
285 * so its 10, 5 for the actual operation, and 5 for the delayed ref
290 num_bytes
= max_t(u64
, num_bytes
,
291 btrfs_calc_insert_metadata_size(fs_info
, min_items
));
293 spin_lock(&sinfo
->lock
);
294 spin_lock(&block_rsv
->lock
);
296 block_rsv
->size
= min_t(u64
, num_bytes
, SZ_512M
);
298 if (block_rsv
->reserved
< block_rsv
->size
) {
299 num_bytes
= block_rsv
->size
- block_rsv
->reserved
;
300 block_rsv
->reserved
+= num_bytes
;
301 btrfs_space_info_update_bytes_may_use(fs_info
, sinfo
,
303 } else if (block_rsv
->reserved
> block_rsv
->size
) {
304 num_bytes
= block_rsv
->reserved
- block_rsv
->size
;
305 btrfs_space_info_update_bytes_may_use(fs_info
, sinfo
,
307 block_rsv
->reserved
= block_rsv
->size
;
308 btrfs_try_granting_tickets(fs_info
, sinfo
);
311 if (block_rsv
->reserved
== block_rsv
->size
)
316 spin_unlock(&block_rsv
->lock
);
317 spin_unlock(&sinfo
->lock
);
320 void btrfs_init_global_block_rsv(struct btrfs_fs_info
*fs_info
)
322 struct btrfs_space_info
*space_info
;
324 space_info
= btrfs_find_space_info(fs_info
, BTRFS_BLOCK_GROUP_SYSTEM
);
325 fs_info
->chunk_block_rsv
.space_info
= space_info
;
327 space_info
= btrfs_find_space_info(fs_info
, BTRFS_BLOCK_GROUP_METADATA
);
328 fs_info
->global_block_rsv
.space_info
= space_info
;
329 fs_info
->trans_block_rsv
.space_info
= space_info
;
330 fs_info
->empty_block_rsv
.space_info
= space_info
;
331 fs_info
->delayed_block_rsv
.space_info
= space_info
;
332 fs_info
->delayed_refs_rsv
.space_info
= space_info
;
334 fs_info
->extent_root
->block_rsv
= &fs_info
->delayed_refs_rsv
;
335 fs_info
->csum_root
->block_rsv
= &fs_info
->delayed_refs_rsv
;
336 fs_info
->dev_root
->block_rsv
= &fs_info
->global_block_rsv
;
337 fs_info
->tree_root
->block_rsv
= &fs_info
->global_block_rsv
;
338 if (fs_info
->quota_root
)
339 fs_info
->quota_root
->block_rsv
= &fs_info
->global_block_rsv
;
340 fs_info
->chunk_root
->block_rsv
= &fs_info
->chunk_block_rsv
;
342 btrfs_update_global_block_rsv(fs_info
);
345 void btrfs_release_global_block_rsv(struct btrfs_fs_info
*fs_info
)
347 btrfs_block_rsv_release(fs_info
, &fs_info
->global_block_rsv
, (u64
)-1);
348 WARN_ON(fs_info
->trans_block_rsv
.size
> 0);
349 WARN_ON(fs_info
->trans_block_rsv
.reserved
> 0);
350 WARN_ON(fs_info
->chunk_block_rsv
.size
> 0);
351 WARN_ON(fs_info
->chunk_block_rsv
.reserved
> 0);
352 WARN_ON(fs_info
->delayed_block_rsv
.size
> 0);
353 WARN_ON(fs_info
->delayed_block_rsv
.reserved
> 0);
354 WARN_ON(fs_info
->delayed_refs_rsv
.reserved
> 0);
355 WARN_ON(fs_info
->delayed_refs_rsv
.size
> 0);
358 static struct btrfs_block_rsv
*get_block_rsv(
359 const struct btrfs_trans_handle
*trans
,
360 const struct btrfs_root
*root
)
362 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
363 struct btrfs_block_rsv
*block_rsv
= NULL
;
365 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) ||
366 (root
== fs_info
->csum_root
&& trans
->adding_csums
) ||
367 (root
== fs_info
->uuid_root
))
368 block_rsv
= trans
->block_rsv
;
371 block_rsv
= root
->block_rsv
;
374 block_rsv
= &fs_info
->empty_block_rsv
;
379 struct btrfs_block_rsv
*btrfs_use_block_rsv(struct btrfs_trans_handle
*trans
,
380 struct btrfs_root
*root
,
383 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
384 struct btrfs_block_rsv
*block_rsv
;
385 struct btrfs_block_rsv
*global_rsv
= &fs_info
->global_block_rsv
;
387 bool global_updated
= false;
389 block_rsv
= get_block_rsv(trans
, root
);
391 if (unlikely(block_rsv
->size
== 0))
394 ret
= btrfs_block_rsv_use_bytes(block_rsv
, blocksize
);
398 if (block_rsv
->failfast
)
401 if (block_rsv
->type
== BTRFS_BLOCK_RSV_GLOBAL
&& !global_updated
) {
402 global_updated
= true;
403 btrfs_update_global_block_rsv(fs_info
);
408 * The global reserve still exists to save us from ourselves, so don't
409 * warn_on if we are short on our delayed refs reserve.
411 if (block_rsv
->type
!= BTRFS_BLOCK_RSV_DELREFS
&&
412 btrfs_test_opt(fs_info
, ENOSPC_DEBUG
)) {
413 static DEFINE_RATELIMIT_STATE(_rs
,
414 DEFAULT_RATELIMIT_INTERVAL
* 10,
415 /*DEFAULT_RATELIMIT_BURST*/ 1);
416 if (__ratelimit(&_rs
))
418 "BTRFS: block rsv returned %d\n", ret
);
421 ret
= btrfs_reserve_metadata_bytes(root
, block_rsv
, blocksize
,
422 BTRFS_RESERVE_NO_FLUSH
);
426 * If we couldn't reserve metadata bytes try and use some from
427 * the global reserve if its space type is the same as the global
430 if (block_rsv
->type
!= BTRFS_BLOCK_RSV_GLOBAL
&&
431 block_rsv
->space_info
== global_rsv
->space_info
) {
432 ret
= btrfs_block_rsv_use_bytes(global_rsv
, blocksize
);