blob: 6279d200cf83228746dba1bc4186f1d46d6a5ea5 [file] [log] [blame]
Josef Bacik550fa222019-06-19 13:47:22 -04001// SPDX-License-Identifier: GPL-2.0
2
David Sterba784352f2019-08-21 18:54:28 +02003#include "misc.h"
Josef Bacik550fa222019-06-19 13:47:22 -04004#include "ctree.h"
5#include "block-rsv.h"
6#include "space-info.h"
Josef Bacik67f9c222019-06-19 13:47:23 -04007#include "transaction.h"
Josef Bacik9c343782020-03-13 15:28:48 -04008#include "block-group.h"
Josef Bacik29cbcf42021-11-05 16:45:45 -04009#include "disk-io.h"
Josef Bacikfc97a412022-10-19 10:50:53 -040010#include "fs.h"
Josef Bacik07e81dc2022-10-19 10:51:00 -040011#include "accessors.h"
Josef Bacik550fa222019-06-19 13:47:22 -040012
Josef Bacik734d8c12020-02-04 13:18:54 -050013/*
14 * HOW DO BLOCK RESERVES WORK
15 *
16 * Think of block_rsv's as buckets for logically grouped metadata
17 * reservations. Each block_rsv has a ->size and a ->reserved. ->size is
18 * how large we want our block rsv to be, ->reserved is how much space is
19 * currently reserved for this block reserve.
20 *
21 * ->failfast exists for the truncate case, and is described below.
22 *
23 * NORMAL OPERATION
24 *
25 * -> Reserve
26 * Entrance: btrfs_block_rsv_add, btrfs_block_rsv_refill
27 *
28 * We call into btrfs_reserve_metadata_bytes() with our bytes, which is
29 * accounted for in space_info->bytes_may_use, and then add the bytes to
30 * ->reserved, and ->size in the case of btrfs_block_rsv_add.
31 *
32 * ->size is an over-estimation of how much we may use for a particular
33 * operation.
34 *
35 * -> Use
36 * Entrance: btrfs_use_block_rsv
37 *
38 * When we do a btrfs_alloc_tree_block() we call into btrfs_use_block_rsv()
39 * to determine the appropriate block_rsv to use, and then verify that
40 * ->reserved has enough space for our tree block allocation. Once
41 * successful we subtract fs_info->nodesize from ->reserved.
42 *
43 * -> Finish
44 * Entrance: btrfs_block_rsv_release
45 *
46 * We are finished with our operation, subtract our individual reservation
47 * from ->size, and then subtract ->size from ->reserved and free up the
48 * excess if there is any.
49 *
50 * There is some logic here to refill the delayed refs rsv or the global rsv
51 * as needed, otherwise the excess is subtracted from
52 * space_info->bytes_may_use.
53 *
54 * TYPES OF BLOCK RESERVES
55 *
56 * BLOCK_RSV_TRANS, BLOCK_RSV_DELOPS, BLOCK_RSV_CHUNK
57 * These behave normally, as described above, just within the confines of the
58 * lifetime of their particular operation (transaction for the whole trans
59 * handle lifetime, for example).
60 *
61 * BLOCK_RSV_GLOBAL
62 * It is impossible to properly account for all the space that may be required
63 * to make our extent tree updates. This block reserve acts as an overflow
64 * buffer in case our delayed refs reserve does not reserve enough space to
65 * update the extent tree.
66 *
67 * We can steal from this in some cases as well, notably on evict() or
68 * truncate() in order to help users recover from ENOSPC conditions.
69 *
70 * BLOCK_RSV_DELALLOC
71 * The individual item sizes are determined by the per-inode size
72 * calculations, which are described with the delalloc code. This is pretty
73 * straightforward, it's just the calculation of ->size encodes a lot of
74 * different items, and thus it gets used when updating inodes, inserting file
75 * extents, and inserting checksums.
76 *
77 * BLOCK_RSV_DELREFS
78 * We keep a running tally of how many delayed refs we have on the system.
79 * We assume each one of these delayed refs are going to use a full
80 * reservation. We use the transaction items and pre-reserve space for every
81 * operation, and use this reservation to refill any gap between ->size and
82 * ->reserved that may exist.
83 *
84 * From there it's straightforward, removing a delayed ref means we remove its
85 * count from ->size and free up reservations as necessary. Since this is
86 * the most dynamic block reserve in the system, we will try to refill this
87 * block reserve first with any excess returned by any other block reserve.
88 *
89 * BLOCK_RSV_EMPTY
90 * This is the fallback block reserve to make us try to reserve space if we
91 * don't have a specific bucket for this allocation. It is mostly used for
92 * updating the device tree and such, since that is a separate pool we're
93 * content to just reserve space from the space_info on demand.
94 *
95 * BLOCK_RSV_TEMP
96 * This is used by things like truncate and iput. We will temporarily
97 * allocate a block reserve, set it to some size, and then truncate bytes
98 * until we have no space left. With ->failfast set we'll simply return
99 * ENOSPC from btrfs_use_block_rsv() to signal that we need to unwind and try
100 * to make a new reservation. This is because these operations are
101 * unbounded, so we want to do as much work as we can, and then back off and
102 * re-reserve.
103 */
104
Josef Bacik550fa222019-06-19 13:47:22 -0400105static u64 block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
106 struct btrfs_block_rsv *block_rsv,
107 struct btrfs_block_rsv *dest, u64 num_bytes,
108 u64 *qgroup_to_release_ret)
109{
110 struct btrfs_space_info *space_info = block_rsv->space_info;
111 u64 qgroup_to_release = 0;
112 u64 ret;
113
114 spin_lock(&block_rsv->lock);
115 if (num_bytes == (u64)-1) {
116 num_bytes = block_rsv->size;
117 qgroup_to_release = block_rsv->qgroup_rsv_size;
118 }
119 block_rsv->size -= num_bytes;
120 if (block_rsv->reserved >= block_rsv->size) {
121 num_bytes = block_rsv->reserved - block_rsv->size;
122 block_rsv->reserved = block_rsv->size;
David Sterbac70c2c52022-06-23 17:08:14 +0200123 block_rsv->full = true;
Josef Bacik550fa222019-06-19 13:47:22 -0400124 } else {
125 num_bytes = 0;
126 }
Josef Bacikd2463312023-05-02 16:00:06 -0400127 if (qgroup_to_release_ret &&
128 block_rsv->qgroup_rsv_reserved >= block_rsv->qgroup_rsv_size) {
Josef Bacik550fa222019-06-19 13:47:22 -0400129 qgroup_to_release = block_rsv->qgroup_rsv_reserved -
130 block_rsv->qgroup_rsv_size;
131 block_rsv->qgroup_rsv_reserved = block_rsv->qgroup_rsv_size;
132 } else {
133 qgroup_to_release = 0;
134 }
135 spin_unlock(&block_rsv->lock);
136
137 ret = num_bytes;
138 if (num_bytes > 0) {
139 if (dest) {
140 spin_lock(&dest->lock);
141 if (!dest->full) {
142 u64 bytes_to_add;
143
144 bytes_to_add = dest->size - dest->reserved;
145 bytes_to_add = min(num_bytes, bytes_to_add);
146 dest->reserved += bytes_to_add;
147 if (dest->reserved >= dest->size)
David Sterbac70c2c52022-06-23 17:08:14 +0200148 dest->full = true;
Josef Bacik550fa222019-06-19 13:47:22 -0400149 num_bytes -= bytes_to_add;
150 }
151 spin_unlock(&dest->lock);
152 }
153 if (num_bytes)
Josef Bacikd05e4642019-08-22 15:11:02 -0400154 btrfs_space_info_free_bytes_may_use(fs_info,
155 space_info,
156 num_bytes);
Josef Bacik550fa222019-06-19 13:47:22 -0400157 }
158 if (qgroup_to_release_ret)
159 *qgroup_to_release_ret = qgroup_to_release;
160 return ret;
161}
162
163int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src,
164 struct btrfs_block_rsv *dst, u64 num_bytes,
165 bool update_size)
166{
167 int ret;
168
169 ret = btrfs_block_rsv_use_bytes(src, num_bytes);
170 if (ret)
171 return ret;
172
173 btrfs_block_rsv_add_bytes(dst, num_bytes, update_size);
174 return 0;
175}
176
David Sterba8bfc9b22022-06-23 17:15:37 +0200177void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, enum btrfs_rsv_type type)
Josef Bacik550fa222019-06-19 13:47:22 -0400178{
179 memset(rsv, 0, sizeof(*rsv));
180 spin_lock_init(&rsv->lock);
181 rsv->type = type;
182}
183
184void btrfs_init_metadata_block_rsv(struct btrfs_fs_info *fs_info,
185 struct btrfs_block_rsv *rsv,
David Sterba8bfc9b22022-06-23 17:15:37 +0200186 enum btrfs_rsv_type type)
Josef Bacik550fa222019-06-19 13:47:22 -0400187{
188 btrfs_init_block_rsv(rsv, type);
189 rsv->space_info = btrfs_find_space_info(fs_info,
190 BTRFS_BLOCK_GROUP_METADATA);
191}
192
193struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_fs_info *fs_info,
David Sterba8bfc9b22022-06-23 17:15:37 +0200194 enum btrfs_rsv_type type)
Josef Bacik550fa222019-06-19 13:47:22 -0400195{
196 struct btrfs_block_rsv *block_rsv;
197
198 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
199 if (!block_rsv)
200 return NULL;
201
202 btrfs_init_metadata_block_rsv(fs_info, block_rsv, type);
203 return block_rsv;
204}
205
206void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info,
207 struct btrfs_block_rsv *rsv)
208{
209 if (!rsv)
210 return;
Nikolay Borisov63f018b2020-03-10 10:59:31 +0200211 btrfs_block_rsv_release(fs_info, rsv, (u64)-1, NULL);
Josef Bacik550fa222019-06-19 13:47:22 -0400212 kfree(rsv);
213}
214
Josef Bacik92705012021-11-09 10:12:07 -0500215int btrfs_block_rsv_add(struct btrfs_fs_info *fs_info,
Josef Bacik550fa222019-06-19 13:47:22 -0400216 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
217 enum btrfs_reserve_flush_enum flush)
218{
219 int ret;
220
221 if (num_bytes == 0)
222 return 0;
223
Josef Bacik92705012021-11-09 10:12:07 -0500224 ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, num_bytes, flush);
Josef Bacik550fa222019-06-19 13:47:22 -0400225 if (!ret)
226 btrfs_block_rsv_add_bytes(block_rsv, num_bytes, true);
227
228 return ret;
229}
230
David Sterba428c8e02022-10-26 23:25:14 +0200231int btrfs_block_rsv_check(struct btrfs_block_rsv *block_rsv, int min_percent)
Josef Bacik550fa222019-06-19 13:47:22 -0400232{
233 u64 num_bytes = 0;
234 int ret = -ENOSPC;
235
Josef Bacik550fa222019-06-19 13:47:22 -0400236 spin_lock(&block_rsv->lock);
David Sterba428c8e02022-10-26 23:25:14 +0200237 num_bytes = mult_perc(block_rsv->size, min_percent);
Josef Bacik550fa222019-06-19 13:47:22 -0400238 if (block_rsv->reserved >= num_bytes)
239 ret = 0;
240 spin_unlock(&block_rsv->lock);
241
242 return ret;
243}
244
Josef Bacik92705012021-11-09 10:12:07 -0500245int btrfs_block_rsv_refill(struct btrfs_fs_info *fs_info,
Filipe Manana4e8313e2023-03-21 11:13:48 +0000246 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
Josef Bacik550fa222019-06-19 13:47:22 -0400247 enum btrfs_reserve_flush_enum flush)
248{
Josef Bacik550fa222019-06-19 13:47:22 -0400249 int ret = -ENOSPC;
250
251 if (!block_rsv)
252 return 0;
253
254 spin_lock(&block_rsv->lock);
Josef Bacik550fa222019-06-19 13:47:22 -0400255 if (block_rsv->reserved >= num_bytes)
256 ret = 0;
257 else
258 num_bytes -= block_rsv->reserved;
259 spin_unlock(&block_rsv->lock);
260
261 if (!ret)
262 return 0;
263
Josef Bacik92705012021-11-09 10:12:07 -0500264 ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, num_bytes, flush);
Josef Bacik550fa222019-06-19 13:47:22 -0400265 if (!ret) {
266 btrfs_block_rsv_add_bytes(block_rsv, num_bytes, false);
267 return 0;
268 }
269
270 return ret;
271}
272
Nikolay Borisov63f018b2020-03-10 10:59:31 +0200273u64 btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
274 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
275 u64 *qgroup_to_release)
Josef Bacik550fa222019-06-19 13:47:22 -0400276{
277 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
278 struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_refs_rsv;
279 struct btrfs_block_rsv *target = NULL;
280
281 /*
282 * If we are the delayed_rsv then push to the global rsv, otherwise dump
283 * into the delayed rsv if it is not full.
284 */
285 if (block_rsv == delayed_rsv)
286 target = global_rsv;
David Sterba748f5532022-09-05 18:32:23 +0200287 else if (block_rsv != global_rsv && !btrfs_block_rsv_full(delayed_rsv))
Josef Bacik550fa222019-06-19 13:47:22 -0400288 target = delayed_rsv;
289
290 if (target && block_rsv->space_info != target->space_info)
291 target = NULL;
292
293 return block_rsv_release_bytes(fs_info, block_rsv, target, num_bytes,
294 qgroup_to_release);
295}
296
297int btrfs_block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, u64 num_bytes)
298{
299 int ret = -ENOSPC;
300
301 spin_lock(&block_rsv->lock);
302 if (block_rsv->reserved >= num_bytes) {
303 block_rsv->reserved -= num_bytes;
304 if (block_rsv->reserved < block_rsv->size)
David Sterbac70c2c52022-06-23 17:08:14 +0200305 block_rsv->full = false;
Josef Bacik550fa222019-06-19 13:47:22 -0400306 ret = 0;
307 }
308 spin_unlock(&block_rsv->lock);
309 return ret;
310}
311
312void btrfs_block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
313 u64 num_bytes, bool update_size)
314{
315 spin_lock(&block_rsv->lock);
316 block_rsv->reserved += num_bytes;
317 if (update_size)
318 block_rsv->size += num_bytes;
319 else if (block_rsv->reserved >= block_rsv->size)
David Sterbac70c2c52022-06-23 17:08:14 +0200320 block_rsv->full = true;
Josef Bacik550fa222019-06-19 13:47:22 -0400321 spin_unlock(&block_rsv->lock);
322}
323
Josef Bacik67f9c222019-06-19 13:47:23 -0400324void btrfs_update_global_block_rsv(struct btrfs_fs_info *fs_info)
325{
326 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
327 struct btrfs_space_info *sinfo = block_rsv->space_info;
Josef Bacik9506f952021-12-02 15:34:31 -0500328 struct btrfs_root *root, *tmp;
329 u64 num_bytes = btrfs_root_used(&fs_info->tree_root->root_item);
330 unsigned int min_items = 1;
Josef Bacik67f9c222019-06-19 13:47:23 -0400331
332 /*
333 * The global block rsv is based on the size of the extent tree, the
334 * checksum tree and the root tree. If the fs is empty we want to set
335 * it to a minimal amount for safety.
Josef Bacik9506f952021-12-02 15:34:31 -0500336 *
337 * We also are going to need to modify the minimum of the tree root and
338 * any global roots we could touch.
Josef Bacik67f9c222019-06-19 13:47:23 -0400339 */
Josef Bacik9506f952021-12-02 15:34:31 -0500340 read_lock(&fs_info->global_root_lock);
341 rbtree_postorder_for_each_entry_safe(root, tmp, &fs_info->global_root_tree,
342 rb_node) {
343 if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID ||
344 root->root_key.objectid == BTRFS_CSUM_TREE_OBJECTID ||
345 root->root_key.objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) {
346 num_bytes += btrfs_root_used(&root->root_item);
347 min_items++;
348 }
349 }
350 read_unlock(&fs_info->global_root_lock);
Josef Bacik3593ce32019-08-22 15:19:00 -0400351
352 /*
353 * But we also want to reserve enough space so we can do the fallback
Filipe Manana5630e2b2023-03-21 11:13:58 +0000354 * global reserve for an unlink, which is an additional
355 * BTRFS_UNLINK_METADATA_UNITS items.
Josef Bacik3593ce32019-08-22 15:19:00 -0400356 *
357 * But we also need space for the delayed ref updates from the unlink,
Filipe Mananaf8f210d2023-03-21 11:13:59 +0000358 * so add BTRFS_UNLINK_METADATA_UNITS units for delayed refs, one for
359 * each unlink metadata item.
Josef Bacik3593ce32019-08-22 15:19:00 -0400360 */
Filipe Mananaf8f210d2023-03-21 11:13:59 +0000361 min_items += BTRFS_UNLINK_METADATA_UNITS;
Josef Bacik3593ce32019-08-22 15:19:00 -0400362
363 num_bytes = max_t(u64, num_bytes,
Filipe Mananaf8f210d2023-03-21 11:13:59 +0000364 btrfs_calc_insert_metadata_size(fs_info, min_items) +
365 btrfs_calc_delayed_ref_bytes(fs_info,
366 BTRFS_UNLINK_METADATA_UNITS));
Josef Bacik67f9c222019-06-19 13:47:23 -0400367
368 spin_lock(&sinfo->lock);
369 spin_lock(&block_rsv->lock);
370
371 block_rsv->size = min_t(u64, num_bytes, SZ_512M);
372
373 if (block_rsv->reserved < block_rsv->size) {
Josef Bacikd792b0f2019-08-22 15:19:01 -0400374 num_bytes = block_rsv->size - block_rsv->reserved;
Josef Bacikd792b0f2019-08-22 15:19:01 -0400375 btrfs_space_info_update_bytes_may_use(fs_info, sinfo,
376 num_bytes);
Anand Jainb82582d2020-02-04 19:05:58 +0800377 block_rsv->reserved = block_rsv->size;
Josef Bacik67f9c222019-06-19 13:47:23 -0400378 } else if (block_rsv->reserved > block_rsv->size) {
379 num_bytes = block_rsv->reserved - block_rsv->size;
380 btrfs_space_info_update_bytes_may_use(fs_info, sinfo,
381 -num_bytes);
Josef Bacik67f9c222019-06-19 13:47:23 -0400382 block_rsv->reserved = block_rsv->size;
Josef Bacik426551f2019-08-22 15:19:02 -0400383 btrfs_try_granting_tickets(fs_info, sinfo);
Josef Bacik67f9c222019-06-19 13:47:23 -0400384 }
385
David Sterbac70c2c52022-06-23 17:08:14 +0200386 block_rsv->full = (block_rsv->reserved == block_rsv->size);
Josef Bacik67f9c222019-06-19 13:47:23 -0400387
Josef Bacik9c343782020-03-13 15:28:48 -0400388 if (block_rsv->size >= sinfo->total_bytes)
389 sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik67f9c222019-06-19 13:47:23 -0400390 spin_unlock(&block_rsv->lock);
391 spin_unlock(&sinfo->lock);
392}
393
Josef Bacik2e608bd2021-11-05 16:45:44 -0400394void btrfs_init_root_block_rsv(struct btrfs_root *root)
395{
396 struct btrfs_fs_info *fs_info = root->fs_info;
397
398 switch (root->root_key.objectid) {
399 case BTRFS_CSUM_TREE_OBJECTID:
400 case BTRFS_EXTENT_TREE_OBJECTID:
Josef Bacikc18e3232021-12-02 15:34:32 -0500401 case BTRFS_FREE_SPACE_TREE_OBJECTID:
Qu Wenruo14033b02022-08-09 13:02:17 +0800402 case BTRFS_BLOCK_GROUP_TREE_OBJECTID:
Josef Bacik2e608bd2021-11-05 16:45:44 -0400403 root->block_rsv = &fs_info->delayed_refs_rsv;
404 break;
405 case BTRFS_ROOT_TREE_OBJECTID:
406 case BTRFS_DEV_TREE_OBJECTID:
407 case BTRFS_QUOTA_TREE_OBJECTID:
408 root->block_rsv = &fs_info->global_block_rsv;
409 break;
410 case BTRFS_CHUNK_TREE_OBJECTID:
411 root->block_rsv = &fs_info->chunk_block_rsv;
412 break;
413 default:
414 root->block_rsv = NULL;
415 break;
416 }
417}
418
Josef Bacik67f9c222019-06-19 13:47:23 -0400419void btrfs_init_global_block_rsv(struct btrfs_fs_info *fs_info)
420{
421 struct btrfs_space_info *space_info;
422
423 space_info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
424 fs_info->chunk_block_rsv.space_info = space_info;
425
426 space_info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
427 fs_info->global_block_rsv.space_info = space_info;
428 fs_info->trans_block_rsv.space_info = space_info;
429 fs_info->empty_block_rsv.space_info = space_info;
430 fs_info->delayed_block_rsv.space_info = space_info;
431 fs_info->delayed_refs_rsv.space_info = space_info;
432
Josef Bacik67f9c222019-06-19 13:47:23 -0400433 btrfs_update_global_block_rsv(fs_info);
434}
435
436void btrfs_release_global_block_rsv(struct btrfs_fs_info *fs_info)
437{
Nikolay Borisov63f018b2020-03-10 10:59:31 +0200438 btrfs_block_rsv_release(fs_info, &fs_info->global_block_rsv, (u64)-1,
439 NULL);
Josef Bacik67f9c222019-06-19 13:47:23 -0400440 WARN_ON(fs_info->trans_block_rsv.size > 0);
441 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
442 WARN_ON(fs_info->chunk_block_rsv.size > 0);
443 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
444 WARN_ON(fs_info->delayed_block_rsv.size > 0);
445 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
446 WARN_ON(fs_info->delayed_refs_rsv.reserved > 0);
447 WARN_ON(fs_info->delayed_refs_rsv.size > 0);
448}
449
450static struct btrfs_block_rsv *get_block_rsv(
451 const struct btrfs_trans_handle *trans,
452 const struct btrfs_root *root)
453{
454 struct btrfs_fs_info *fs_info = root->fs_info;
455 struct btrfs_block_rsv *block_rsv = NULL;
456
Qu Wenruo92a7cc42020-05-15 14:01:40 +0800457 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) ||
Josef Bacikfc28b252021-11-05 16:45:48 -0400458 (root == fs_info->uuid_root) ||
459 (trans->adding_csums &&
460 root->root_key.objectid == BTRFS_CSUM_TREE_OBJECTID))
Josef Bacik67f9c222019-06-19 13:47:23 -0400461 block_rsv = trans->block_rsv;
462
463 if (!block_rsv)
464 block_rsv = root->block_rsv;
465
466 if (!block_rsv)
467 block_rsv = &fs_info->empty_block_rsv;
468
469 return block_rsv;
470}
471
472struct btrfs_block_rsv *btrfs_use_block_rsv(struct btrfs_trans_handle *trans,
473 struct btrfs_root *root,
474 u32 blocksize)
475{
476 struct btrfs_fs_info *fs_info = root->fs_info;
477 struct btrfs_block_rsv *block_rsv;
478 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
479 int ret;
480 bool global_updated = false;
481
482 block_rsv = get_block_rsv(trans, root);
483
484 if (unlikely(block_rsv->size == 0))
485 goto try_reserve;
486again:
487 ret = btrfs_block_rsv_use_bytes(block_rsv, blocksize);
488 if (!ret)
489 return block_rsv;
490
491 if (block_rsv->failfast)
492 return ERR_PTR(ret);
493
494 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
495 global_updated = true;
496 btrfs_update_global_block_rsv(fs_info);
497 goto again;
498 }
499
500 /*
501 * The global reserve still exists to save us from ourselves, so don't
502 * warn_on if we are short on our delayed refs reserve.
503 */
504 if (block_rsv->type != BTRFS_BLOCK_RSV_DELREFS &&
505 btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
506 static DEFINE_RATELIMIT_STATE(_rs,
507 DEFAULT_RATELIMIT_INTERVAL * 10,
508 /*DEFAULT_RATELIMIT_BURST*/ 1);
509 if (__ratelimit(&_rs))
510 WARN(1, KERN_DEBUG
Josef Bacike38fdb72020-10-26 16:57:26 -0400511 "BTRFS: block rsv %d returned %d\n",
512 block_rsv->type, ret);
Josef Bacik67f9c222019-06-19 13:47:23 -0400513 }
514try_reserve:
Josef Bacik92705012021-11-09 10:12:07 -0500515 ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, blocksize,
Josef Bacik67f9c222019-06-19 13:47:23 -0400516 BTRFS_RESERVE_NO_FLUSH);
517 if (!ret)
518 return block_rsv;
519 /*
520 * If we couldn't reserve metadata bytes try and use some from
521 * the global reserve if its space type is the same as the global
522 * reservation.
523 */
524 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
525 block_rsv->space_info == global_rsv->space_info) {
526 ret = btrfs_block_rsv_use_bytes(global_rsv, blocksize);
527 if (!ret)
528 return global_rsv;
529 }
Josef Bacik765c3fe92022-09-09 09:35:01 -0400530
531 /*
532 * All hope is lost, but of course our reservations are overly
533 * pessimistic, so instead of possibly having an ENOSPC abort here, try
534 * one last time to force a reservation if there's enough actual space
535 * on disk to make the reservation.
536 */
537 ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, blocksize,
538 BTRFS_RESERVE_FLUSH_EMERGENCY);
539 if (!ret)
540 return block_rsv;
541
Josef Bacik67f9c222019-06-19 13:47:23 -0400542 return ERR_PTR(ret);
543}
Josef Bacik54d687c2023-04-29 16:07:10 -0400544
545int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info,
546 struct btrfs_block_rsv *rsv)
547{
548 u64 needed_bytes;
549 int ret;
550
551 /* 1 for slack space, 1 for updating the inode */
552 needed_bytes = btrfs_calc_insert_metadata_size(fs_info, 1) +
553 btrfs_calc_metadata_size(fs_info, 1);
554
555 spin_lock(&rsv->lock);
556 if (rsv->reserved < needed_bytes)
557 ret = -ENOSPC;
558 else
559 ret = 0;
560 spin_unlock(&rsv->lock);
561 return ret;
562}