1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "misc.h"
4 #include "ctree.h"
5 #include "block-rsv.h"
6 #include "space-info.h"
7 #include "transaction.h"
8 #include "block-group.h"
9
10 /*
11 * HOW DO BLOCK RESERVES WORK
12 *
13 * Think of block_rsv's as buckets for logically grouped metadata
14 * reservations. Each block_rsv has a ->size and a ->reserved. ->size is
15 * how large we want our block rsv to be, ->reserved is how much space is
16 * currently reserved for this block reserve.
17 *
18 * ->failfast exists for the truncate case, and is described below.
19 *
20 * NORMAL OPERATION
21 *
22 * -> Reserve
23 * Entrance: btrfs_block_rsv_add, btrfs_block_rsv_refill
24 *
25 * We call into btrfs_reserve_metadata_bytes() with our bytes, which is
26 * accounted for in space_info->bytes_may_use, and then add the bytes to
27 * ->reserved, and ->size in the case of btrfs_block_rsv_add.
28 *
29 * ->size is an over-estimation of how much we may use for a particular
30 * operation.
31 *
32 * -> Use
33 * Entrance: btrfs_use_block_rsv
34 *
35 * When we do a btrfs_alloc_tree_block() we call into btrfs_use_block_rsv()
36 * to determine the appropriate block_rsv to use, and then verify that
37 * ->reserved has enough space for our tree block allocation. Once
38 * successful we subtract fs_info->nodesize from ->reserved.
39 *
40 * -> Finish
41 * Entrance: btrfs_block_rsv_release
42 *
43 * We are finished with our operation, subtract our individual reservation
44 * from ->size, and then subtract ->size from ->reserved and free up the
45 * excess if there is any.
46 *
47 * There is some logic here to refill the delayed refs rsv or the global rsv
48 * as needed, otherwise the excess is subtracted from
49 * space_info->bytes_may_use.
50 *
51 * TYPES OF BLOCK RESERVES
52 *
53 * BLOCK_RSV_TRANS, BLOCK_RSV_DELOPS, BLOCK_RSV_CHUNK
54 * These behave normally, as described above, just within the confines of the
55 * lifetime of their particular operation (transaction for the whole trans
56 * handle lifetime, for example).
57 *
58 * BLOCK_RSV_GLOBAL
59 * It is impossible to properly account for all the space that may be required
60 * to make our extent tree updates. This block reserve acts as an overflow
61 * buffer in case our delayed refs reserve does not reserve enough space to
62 * update the extent tree.
63 *
64 * We can steal from this in some cases as well, notably on evict() or
65 * truncate() in order to help users recover from ENOSPC conditions.
66 *
67 * BLOCK_RSV_DELALLOC
68 * The individual item sizes are determined by the per-inode size
69 * calculations, which are described with the delalloc code. This is pretty
70 * straightforward, it's just the calculation of ->size encodes a lot of
71 * different items, and thus it gets used when updating inodes, inserting file
72 * extents, and inserting checksums.
73 *
74 * BLOCK_RSV_DELREFS
75 * We keep a running tally of how many delayed refs we have on the system.
76 * We assume each one of these delayed refs are going to use a full
77 * reservation. We use the transaction items and pre-reserve space for every
78 * operation, and use this reservation to refill any gap between ->size and
79 * ->reserved that may exist.
80 *
81 * From there it's straightforward, removing a delayed ref means we remove its
82 * count from ->size and free up reservations as necessary. Since this is
83 * the most dynamic block reserve in the system, we will try to refill this
84 * block reserve first with any excess returned by any other block reserve.
85 *
86 * BLOCK_RSV_EMPTY
87 * This is the fallback block reserve to make us try to reserve space if we
88 * don't have a specific bucket for this allocation. It is mostly used for
89 * updating the device tree and such, since that is a separate pool we're
90 * content to just reserve space from the space_info on demand.
91 *
92 * BLOCK_RSV_TEMP
93 * This is used by things like truncate and iput. We will temporarily
94 * allocate a block reserve, set it to some size, and then truncate bytes
95 * until we have no space left. With ->failfast set we'll simply return
96 * ENOSPC from btrfs_use_block_rsv() to signal that we need to unwind and try
97 * to make a new reservation. This is because these operations are
98 * unbounded, so we want to do as much work as we can, and then back off and
99 * re-reserve.
100 */
101
block_rsv_release_bytes(struct btrfs_fs_info * fs_info,struct btrfs_block_rsv * block_rsv,struct btrfs_block_rsv * dest,u64 num_bytes,u64 * qgroup_to_release_ret)102 static u64 block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
103 struct btrfs_block_rsv *block_rsv,
104 struct btrfs_block_rsv *dest, u64 num_bytes,
105 u64 *qgroup_to_release_ret)
106 {
107 struct btrfs_space_info *space_info = block_rsv->space_info;
108 u64 qgroup_to_release = 0;
109 u64 ret;
110
111 spin_lock(&block_rsv->lock);
112 if (num_bytes == (u64)-1) {
113 num_bytes = block_rsv->size;
114 qgroup_to_release = block_rsv->qgroup_rsv_size;
115 }
116 block_rsv->size -= num_bytes;
117 if (block_rsv->reserved >= block_rsv->size) {
118 num_bytes = block_rsv->reserved - block_rsv->size;
119 block_rsv->reserved = block_rsv->size;
120 block_rsv->full = 1;
121 } else {
122 num_bytes = 0;
123 }
124 if (qgroup_to_release_ret &&
125 block_rsv->qgroup_rsv_reserved >= block_rsv->qgroup_rsv_size) {
126 qgroup_to_release = block_rsv->qgroup_rsv_reserved -
127 block_rsv->qgroup_rsv_size;
128 block_rsv->qgroup_rsv_reserved = block_rsv->qgroup_rsv_size;
129 } else {
130 qgroup_to_release = 0;
131 }
132 spin_unlock(&block_rsv->lock);
133
134 ret = num_bytes;
135 if (num_bytes > 0) {
136 if (dest) {
137 spin_lock(&dest->lock);
138 if (!dest->full) {
139 u64 bytes_to_add;
140
141 bytes_to_add = dest->size - dest->reserved;
142 bytes_to_add = min(num_bytes, bytes_to_add);
143 dest->reserved += bytes_to_add;
144 if (dest->reserved >= dest->size)
145 dest->full = 1;
146 num_bytes -= bytes_to_add;
147 }
148 spin_unlock(&dest->lock);
149 }
150 if (num_bytes)
151 btrfs_space_info_free_bytes_may_use(fs_info,
152 space_info,
153 num_bytes);
154 }
155 if (qgroup_to_release_ret)
156 *qgroup_to_release_ret = qgroup_to_release;
157 return ret;
158 }
159
btrfs_block_rsv_migrate(struct btrfs_block_rsv * src,struct btrfs_block_rsv * dst,u64 num_bytes,bool update_size)160 int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src,
161 struct btrfs_block_rsv *dst, u64 num_bytes,
162 bool update_size)
163 {
164 int ret;
165
166 ret = btrfs_block_rsv_use_bytes(src, num_bytes);
167 if (ret)
168 return ret;
169
170 btrfs_block_rsv_add_bytes(dst, num_bytes, update_size);
171 return 0;
172 }
173
btrfs_init_block_rsv(struct btrfs_block_rsv * rsv,unsigned short type)174 void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
175 {
176 memset(rsv, 0, sizeof(*rsv));
177 spin_lock_init(&rsv->lock);
178 rsv->type = type;
179 }
180
btrfs_init_metadata_block_rsv(struct btrfs_fs_info * fs_info,struct btrfs_block_rsv * rsv,unsigned short type)181 void btrfs_init_metadata_block_rsv(struct btrfs_fs_info *fs_info,
182 struct btrfs_block_rsv *rsv,
183 unsigned short type)
184 {
185 btrfs_init_block_rsv(rsv, type);
186 rsv->space_info = btrfs_find_space_info(fs_info,
187 BTRFS_BLOCK_GROUP_METADATA);
188 }
189
btrfs_alloc_block_rsv(struct btrfs_fs_info * fs_info,unsigned short type)190 struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_fs_info *fs_info,
191 unsigned short type)
192 {
193 struct btrfs_block_rsv *block_rsv;
194
195 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
196 if (!block_rsv)
197 return NULL;
198
199 btrfs_init_metadata_block_rsv(fs_info, block_rsv, type);
200 return block_rsv;
201 }
202
btrfs_free_block_rsv(struct btrfs_fs_info * fs_info,struct btrfs_block_rsv * rsv)203 void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info,
204 struct btrfs_block_rsv *rsv)
205 {
206 if (!rsv)
207 return;
208 btrfs_block_rsv_release(fs_info, rsv, (u64)-1, NULL);
209 kfree(rsv);
210 }
211
btrfs_block_rsv_add(struct btrfs_root * root,struct btrfs_block_rsv * block_rsv,u64 num_bytes,enum btrfs_reserve_flush_enum flush)212 int btrfs_block_rsv_add(struct btrfs_root *root,
213 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
214 enum btrfs_reserve_flush_enum flush)
215 {
216 int ret;
217
218 if (num_bytes == 0)
219 return 0;
220
221 ret = btrfs_reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
222 if (!ret)
223 btrfs_block_rsv_add_bytes(block_rsv, num_bytes, true);
224
225 return ret;
226 }
227
btrfs_block_rsv_check(struct btrfs_block_rsv * block_rsv,int min_factor)228 int btrfs_block_rsv_check(struct btrfs_block_rsv *block_rsv, int min_factor)
229 {
230 u64 num_bytes = 0;
231 int ret = -ENOSPC;
232
233 if (!block_rsv)
234 return 0;
235
236 spin_lock(&block_rsv->lock);
237 num_bytes = div_factor(block_rsv->size, min_factor);
238 if (block_rsv->reserved >= num_bytes)
239 ret = 0;
240 spin_unlock(&block_rsv->lock);
241
242 return ret;
243 }
244
btrfs_block_rsv_refill(struct btrfs_root * root,struct btrfs_block_rsv * block_rsv,u64 min_reserved,enum btrfs_reserve_flush_enum flush)245 int btrfs_block_rsv_refill(struct btrfs_root *root,
246 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
247 enum btrfs_reserve_flush_enum flush)
248 {
249 u64 num_bytes = 0;
250 int ret = -ENOSPC;
251
252 if (!block_rsv)
253 return 0;
254
255 spin_lock(&block_rsv->lock);
256 num_bytes = min_reserved;
257 if (block_rsv->reserved >= num_bytes)
258 ret = 0;
259 else
260 num_bytes -= block_rsv->reserved;
261 spin_unlock(&block_rsv->lock);
262
263 if (!ret)
264 return 0;
265
266 ret = btrfs_reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
267 if (!ret) {
268 btrfs_block_rsv_add_bytes(block_rsv, num_bytes, false);
269 return 0;
270 }
271
272 return ret;
273 }
274
btrfs_block_rsv_release(struct btrfs_fs_info * fs_info,struct btrfs_block_rsv * block_rsv,u64 num_bytes,u64 * qgroup_to_release)275 u64 btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
276 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
277 u64 *qgroup_to_release)
278 {
279 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
280 struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_refs_rsv;
281 struct btrfs_block_rsv *target = NULL;
282
283 /*
284 * If we are the delayed_rsv then push to the global rsv, otherwise dump
285 * into the delayed rsv if it is not full.
286 */
287 if (block_rsv == delayed_rsv)
288 target = global_rsv;
289 else if (block_rsv != global_rsv && !delayed_rsv->full)
290 target = delayed_rsv;
291
292 if (target && block_rsv->space_info != target->space_info)
293 target = NULL;
294
295 return block_rsv_release_bytes(fs_info, block_rsv, target, num_bytes,
296 qgroup_to_release);
297 }
298
btrfs_block_rsv_use_bytes(struct btrfs_block_rsv * block_rsv,u64 num_bytes)299 int btrfs_block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, u64 num_bytes)
300 {
301 int ret = -ENOSPC;
302
303 spin_lock(&block_rsv->lock);
304 if (block_rsv->reserved >= num_bytes) {
305 block_rsv->reserved -= num_bytes;
306 if (block_rsv->reserved < block_rsv->size)
307 block_rsv->full = 0;
308 ret = 0;
309 }
310 spin_unlock(&block_rsv->lock);
311 return ret;
312 }
313
btrfs_block_rsv_add_bytes(struct btrfs_block_rsv * block_rsv,u64 num_bytes,bool update_size)314 void btrfs_block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
315 u64 num_bytes, bool update_size)
316 {
317 spin_lock(&block_rsv->lock);
318 block_rsv->reserved += num_bytes;
319 if (update_size)
320 block_rsv->size += num_bytes;
321 else if (block_rsv->reserved >= block_rsv->size)
322 block_rsv->full = 1;
323 spin_unlock(&block_rsv->lock);
324 }
325
btrfs_cond_migrate_bytes(struct btrfs_fs_info * fs_info,struct btrfs_block_rsv * dest,u64 num_bytes,int min_factor)326 int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
327 struct btrfs_block_rsv *dest, u64 num_bytes,
328 int min_factor)
329 {
330 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
331 u64 min_bytes;
332
333 if (global_rsv->space_info != dest->space_info)
334 return -ENOSPC;
335
336 spin_lock(&global_rsv->lock);
337 min_bytes = div_factor(global_rsv->size, min_factor);
338 if (global_rsv->reserved < min_bytes + num_bytes) {
339 spin_unlock(&global_rsv->lock);
340 return -ENOSPC;
341 }
342 global_rsv->reserved -= num_bytes;
343 if (global_rsv->reserved < global_rsv->size)
344 global_rsv->full = 0;
345 spin_unlock(&global_rsv->lock);
346
347 btrfs_block_rsv_add_bytes(dest, num_bytes, true);
348 return 0;
349 }
350
btrfs_update_global_block_rsv(struct btrfs_fs_info * fs_info)351 void btrfs_update_global_block_rsv(struct btrfs_fs_info *fs_info)
352 {
353 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
354 struct btrfs_space_info *sinfo = block_rsv->space_info;
355 u64 num_bytes;
356 unsigned min_items;
357
358 /*
359 * The global block rsv is based on the size of the extent tree, the
360 * checksum tree and the root tree. If the fs is empty we want to set
361 * it to a minimal amount for safety.
362 */
363 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item) +
364 btrfs_root_used(&fs_info->csum_root->root_item) +
365 btrfs_root_used(&fs_info->tree_root->root_item);
366
367 /*
368 * We at a minimum are going to modify the csum root, the tree root, and
369 * the extent root.
370 */
371 min_items = 3;
372
373 /*
374 * But we also want to reserve enough space so we can do the fallback
375 * global reserve for an unlink, which is an additional 5 items (see the
376 * comment in __unlink_start_trans for what we're modifying.)
377 *
378 * But we also need space for the delayed ref updates from the unlink,
379 * so its 10, 5 for the actual operation, and 5 for the delayed ref
380 * updates.
381 */
382 min_items += 10;
383
384 num_bytes = max_t(u64, num_bytes,
385 btrfs_calc_insert_metadata_size(fs_info, min_items));
386
387 spin_lock(&sinfo->lock);
388 spin_lock(&block_rsv->lock);
389
390 block_rsv->size = min_t(u64, num_bytes, SZ_512M);
391
392 if (block_rsv->reserved < block_rsv->size) {
393 num_bytes = block_rsv->size - block_rsv->reserved;
394 btrfs_space_info_update_bytes_may_use(fs_info, sinfo,
395 num_bytes);
396 block_rsv->reserved = block_rsv->size;
397 } else if (block_rsv->reserved > block_rsv->size) {
398 num_bytes = block_rsv->reserved - block_rsv->size;
399 btrfs_space_info_update_bytes_may_use(fs_info, sinfo,
400 -num_bytes);
401 block_rsv->reserved = block_rsv->size;
402 btrfs_try_granting_tickets(fs_info, sinfo);
403 }
404
405 if (block_rsv->reserved == block_rsv->size)
406 block_rsv->full = 1;
407 else
408 block_rsv->full = 0;
409
410 if (block_rsv->size >= sinfo->total_bytes)
411 sinfo->force_alloc = CHUNK_ALLOC_FORCE;
412 spin_unlock(&block_rsv->lock);
413 spin_unlock(&sinfo->lock);
414 }
415
btrfs_init_global_block_rsv(struct btrfs_fs_info * fs_info)416 void btrfs_init_global_block_rsv(struct btrfs_fs_info *fs_info)
417 {
418 struct btrfs_space_info *space_info;
419
420 space_info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
421 fs_info->chunk_block_rsv.space_info = space_info;
422
423 space_info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
424 fs_info->global_block_rsv.space_info = space_info;
425 fs_info->trans_block_rsv.space_info = space_info;
426 fs_info->empty_block_rsv.space_info = space_info;
427 fs_info->delayed_block_rsv.space_info = space_info;
428 fs_info->delayed_refs_rsv.space_info = space_info;
429
430 fs_info->extent_root->block_rsv = &fs_info->delayed_refs_rsv;
431 fs_info->csum_root->block_rsv = &fs_info->delayed_refs_rsv;
432 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
433 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
434 if (fs_info->quota_root)
435 fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
436 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
437
438 btrfs_update_global_block_rsv(fs_info);
439 }
440
btrfs_release_global_block_rsv(struct btrfs_fs_info * fs_info)441 void btrfs_release_global_block_rsv(struct btrfs_fs_info *fs_info)
442 {
443 btrfs_block_rsv_release(fs_info, &fs_info->global_block_rsv, (u64)-1,
444 NULL);
445 WARN_ON(fs_info->trans_block_rsv.size > 0);
446 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
447 WARN_ON(fs_info->chunk_block_rsv.size > 0);
448 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
449 WARN_ON(fs_info->delayed_block_rsv.size > 0);
450 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
451 WARN_ON(fs_info->delayed_refs_rsv.reserved > 0);
452 WARN_ON(fs_info->delayed_refs_rsv.size > 0);
453 }
454
get_block_rsv(const struct btrfs_trans_handle * trans,const struct btrfs_root * root)455 static struct btrfs_block_rsv *get_block_rsv(
456 const struct btrfs_trans_handle *trans,
457 const struct btrfs_root *root)
458 {
459 struct btrfs_fs_info *fs_info = root->fs_info;
460 struct btrfs_block_rsv *block_rsv = NULL;
461
462 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) ||
463 (root == fs_info->csum_root && trans->adding_csums) ||
464 (root == fs_info->uuid_root))
465 block_rsv = trans->block_rsv;
466
467 if (!block_rsv)
468 block_rsv = root->block_rsv;
469
470 if (!block_rsv)
471 block_rsv = &fs_info->empty_block_rsv;
472
473 return block_rsv;
474 }
475
btrfs_use_block_rsv(struct btrfs_trans_handle * trans,struct btrfs_root * root,u32 blocksize)476 struct btrfs_block_rsv *btrfs_use_block_rsv(struct btrfs_trans_handle *trans,
477 struct btrfs_root *root,
478 u32 blocksize)
479 {
480 struct btrfs_fs_info *fs_info = root->fs_info;
481 struct btrfs_block_rsv *block_rsv;
482 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
483 int ret;
484 bool global_updated = false;
485
486 block_rsv = get_block_rsv(trans, root);
487
488 if (unlikely(block_rsv->size == 0))
489 goto try_reserve;
490 again:
491 ret = btrfs_block_rsv_use_bytes(block_rsv, blocksize);
492 if (!ret)
493 return block_rsv;
494
495 if (block_rsv->failfast)
496 return ERR_PTR(ret);
497
498 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
499 global_updated = true;
500 btrfs_update_global_block_rsv(fs_info);
501 goto again;
502 }
503
504 /*
505 * The global reserve still exists to save us from ourselves, so don't
506 * warn_on if we are short on our delayed refs reserve.
507 */
508 if (block_rsv->type != BTRFS_BLOCK_RSV_DELREFS &&
509 btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
510 static DEFINE_RATELIMIT_STATE(_rs,
511 DEFAULT_RATELIMIT_INTERVAL * 10,
512 /*DEFAULT_RATELIMIT_BURST*/ 1);
513 if (__ratelimit(&_rs))
514 WARN(1, KERN_DEBUG
515 "BTRFS: block rsv %d returned %d\n",
516 block_rsv->type, ret);
517 }
518 try_reserve:
519 ret = btrfs_reserve_metadata_bytes(root, block_rsv, blocksize,
520 BTRFS_RESERVE_NO_FLUSH);
521 if (!ret)
522 return block_rsv;
523 /*
524 * If we couldn't reserve metadata bytes try and use some from
525 * the global reserve if its space type is the same as the global
526 * reservation.
527 */
528 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
529 block_rsv->space_info == global_rsv->space_info) {
530 ret = btrfs_block_rsv_use_bytes(global_rsv, blocksize);
531 if (!ret)
532 return global_rsv;
533 }
534 return ERR_PTR(ret);
535 }
536