xref: /openbmc/linux/fs/btrfs/block-rsv.c (revision 2d2f5f1e)
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 #include "disk-io.h"
10 #include "fs.h"
11 #include "accessors.h"
12 
13 /*
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 
105 static 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;
123 		block_rsv->full = true;
124 	} else {
125 		num_bytes = 0;
126 	}
127 	if (block_rsv->qgroup_rsv_reserved >= block_rsv->qgroup_rsv_size) {
128 		qgroup_to_release = block_rsv->qgroup_rsv_reserved -
129 				    block_rsv->qgroup_rsv_size;
130 		block_rsv->qgroup_rsv_reserved = block_rsv->qgroup_rsv_size;
131 	} else {
132 		qgroup_to_release = 0;
133 	}
134 	spin_unlock(&block_rsv->lock);
135 
136 	ret = num_bytes;
137 	if (num_bytes > 0) {
138 		if (dest) {
139 			spin_lock(&dest->lock);
140 			if (!dest->full) {
141 				u64 bytes_to_add;
142 
143 				bytes_to_add = dest->size - dest->reserved;
144 				bytes_to_add = min(num_bytes, bytes_to_add);
145 				dest->reserved += bytes_to_add;
146 				if (dest->reserved >= dest->size)
147 					dest->full = true;
148 				num_bytes -= bytes_to_add;
149 			}
150 			spin_unlock(&dest->lock);
151 		}
152 		if (num_bytes)
153 			btrfs_space_info_free_bytes_may_use(fs_info,
154 							    space_info,
155 							    num_bytes);
156 	}
157 	if (qgroup_to_release_ret)
158 		*qgroup_to_release_ret = qgroup_to_release;
159 	return ret;
160 }
161 
162 int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src,
163 			    struct btrfs_block_rsv *dst, u64 num_bytes,
164 			    bool update_size)
165 {
166 	int ret;
167 
168 	ret = btrfs_block_rsv_use_bytes(src, num_bytes);
169 	if (ret)
170 		return ret;
171 
172 	btrfs_block_rsv_add_bytes(dst, num_bytes, update_size);
173 	return 0;
174 }
175 
176 void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, enum btrfs_rsv_type type)
177 {
178 	memset(rsv, 0, sizeof(*rsv));
179 	spin_lock_init(&rsv->lock);
180 	rsv->type = type;
181 }
182 
183 void btrfs_init_metadata_block_rsv(struct btrfs_fs_info *fs_info,
184 				   struct btrfs_block_rsv *rsv,
185 				   enum btrfs_rsv_type type)
186 {
187 	btrfs_init_block_rsv(rsv, type);
188 	rsv->space_info = btrfs_find_space_info(fs_info,
189 					    BTRFS_BLOCK_GROUP_METADATA);
190 }
191 
192 struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_fs_info *fs_info,
193 					      enum btrfs_rsv_type type)
194 {
195 	struct btrfs_block_rsv *block_rsv;
196 
197 	block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
198 	if (!block_rsv)
199 		return NULL;
200 
201 	btrfs_init_metadata_block_rsv(fs_info, block_rsv, type);
202 	return block_rsv;
203 }
204 
205 void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info,
206 			  struct btrfs_block_rsv *rsv)
207 {
208 	if (!rsv)
209 		return;
210 	btrfs_block_rsv_release(fs_info, rsv, (u64)-1, NULL);
211 	kfree(rsv);
212 }
213 
214 int btrfs_block_rsv_add(struct btrfs_fs_info *fs_info,
215 			struct btrfs_block_rsv *block_rsv, u64 num_bytes,
216 			enum btrfs_reserve_flush_enum flush)
217 {
218 	int ret;
219 
220 	if (num_bytes == 0)
221 		return 0;
222 
223 	ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, num_bytes, flush);
224 	if (!ret)
225 		btrfs_block_rsv_add_bytes(block_rsv, num_bytes, true);
226 
227 	return ret;
228 }
229 
230 int btrfs_block_rsv_check(struct btrfs_block_rsv *block_rsv, int min_percent)
231 {
232 	u64 num_bytes = 0;
233 	int ret = -ENOSPC;
234 
235 	spin_lock(&block_rsv->lock);
236 	num_bytes = mult_perc(block_rsv->size, min_percent);
237 	if (block_rsv->reserved >= num_bytes)
238 		ret = 0;
239 	spin_unlock(&block_rsv->lock);
240 
241 	return ret;
242 }
243 
244 int btrfs_block_rsv_refill(struct btrfs_fs_info *fs_info,
245 			   struct btrfs_block_rsv *block_rsv, u64 num_bytes,
246 			   enum btrfs_reserve_flush_enum flush)
247 {
248 	int ret = -ENOSPC;
249 
250 	if (!block_rsv)
251 		return 0;
252 
253 	spin_lock(&block_rsv->lock);
254 	if (block_rsv->reserved >= num_bytes)
255 		ret = 0;
256 	else
257 		num_bytes -= block_rsv->reserved;
258 	spin_unlock(&block_rsv->lock);
259 
260 	if (!ret)
261 		return 0;
262 
263 	ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, num_bytes, flush);
264 	if (!ret) {
265 		btrfs_block_rsv_add_bytes(block_rsv, num_bytes, false);
266 		return 0;
267 	}
268 
269 	return ret;
270 }
271 
272 u64 btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
273 			    struct btrfs_block_rsv *block_rsv, u64 num_bytes,
274 			    u64 *qgroup_to_release)
275 {
276 	struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
277 	struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_refs_rsv;
278 	struct btrfs_block_rsv *target = NULL;
279 
280 	/*
281 	 * If we are the delayed_rsv then push to the global rsv, otherwise dump
282 	 * into the delayed rsv if it is not full.
283 	 */
284 	if (block_rsv == delayed_rsv)
285 		target = global_rsv;
286 	else if (block_rsv != global_rsv && !btrfs_block_rsv_full(delayed_rsv))
287 		target = delayed_rsv;
288 
289 	if (target && block_rsv->space_info != target->space_info)
290 		target = NULL;
291 
292 	return block_rsv_release_bytes(fs_info, block_rsv, target, num_bytes,
293 				       qgroup_to_release);
294 }
295 
296 int btrfs_block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, u64 num_bytes)
297 {
298 	int ret = -ENOSPC;
299 
300 	spin_lock(&block_rsv->lock);
301 	if (block_rsv->reserved >= num_bytes) {
302 		block_rsv->reserved -= num_bytes;
303 		if (block_rsv->reserved < block_rsv->size)
304 			block_rsv->full = false;
305 		ret = 0;
306 	}
307 	spin_unlock(&block_rsv->lock);
308 	return ret;
309 }
310 
311 void btrfs_block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
312 			       u64 num_bytes, bool update_size)
313 {
314 	spin_lock(&block_rsv->lock);
315 	block_rsv->reserved += num_bytes;
316 	if (update_size)
317 		block_rsv->size += num_bytes;
318 	else if (block_rsv->reserved >= block_rsv->size)
319 		block_rsv->full = true;
320 	spin_unlock(&block_rsv->lock);
321 }
322 
323 void btrfs_update_global_block_rsv(struct btrfs_fs_info *fs_info)
324 {
325 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
326 	struct btrfs_space_info *sinfo = block_rsv->space_info;
327 	struct btrfs_root *root, *tmp;
328 	u64 num_bytes = btrfs_root_used(&fs_info->tree_root->root_item);
329 	unsigned int min_items = 1;
330 
331 	/*
332 	 * The global block rsv is based on the size of the extent tree, the
333 	 * checksum tree and the root tree.  If the fs is empty we want to set
334 	 * it to a minimal amount for safety.
335 	 *
336 	 * We also are going to need to modify the minimum of the tree root and
337 	 * any global roots we could touch.
338 	 */
339 	read_lock(&fs_info->global_root_lock);
340 	rbtree_postorder_for_each_entry_safe(root, tmp, &fs_info->global_root_tree,
341 					     rb_node) {
342 		if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID ||
343 		    root->root_key.objectid == BTRFS_CSUM_TREE_OBJECTID ||
344 		    root->root_key.objectid == BTRFS_FREE_SPACE_TREE_OBJECTID) {
345 			num_bytes += btrfs_root_used(&root->root_item);
346 			min_items++;
347 		}
348 	}
349 	read_unlock(&fs_info->global_root_lock);
350 
351 	/*
352 	 * But we also want to reserve enough space so we can do the fallback
353 	 * global reserve for an unlink, which is an additional
354 	 * BTRFS_UNLINK_METADATA_UNITS items.
355 	 *
356 	 * But we also need space for the delayed ref updates from the unlink,
357 	 * so add BTRFS_UNLINK_METADATA_UNITS units for delayed refs, one for
358 	 * each unlink metadata item.
359 	 */
360 	min_items += BTRFS_UNLINK_METADATA_UNITS;
361 
362 	num_bytes = max_t(u64, num_bytes,
363 			  btrfs_calc_insert_metadata_size(fs_info, min_items) +
364 			  btrfs_calc_delayed_ref_bytes(fs_info,
365 					       BTRFS_UNLINK_METADATA_UNITS));
366 
367 	spin_lock(&sinfo->lock);
368 	spin_lock(&block_rsv->lock);
369 
370 	block_rsv->size = min_t(u64, num_bytes, SZ_512M);
371 
372 	if (block_rsv->reserved < block_rsv->size) {
373 		num_bytes = block_rsv->size - block_rsv->reserved;
374 		btrfs_space_info_update_bytes_may_use(fs_info, sinfo,
375 						      num_bytes);
376 		block_rsv->reserved = block_rsv->size;
377 	} else if (block_rsv->reserved > block_rsv->size) {
378 		num_bytes = block_rsv->reserved - block_rsv->size;
379 		btrfs_space_info_update_bytes_may_use(fs_info, sinfo,
380 						      -num_bytes);
381 		block_rsv->reserved = block_rsv->size;
382 		btrfs_try_granting_tickets(fs_info, sinfo);
383 	}
384 
385 	block_rsv->full = (block_rsv->reserved == block_rsv->size);
386 
387 	if (block_rsv->size >= sinfo->total_bytes)
388 		sinfo->force_alloc = CHUNK_ALLOC_FORCE;
389 	spin_unlock(&block_rsv->lock);
390 	spin_unlock(&sinfo->lock);
391 }
392 
393 void btrfs_init_root_block_rsv(struct btrfs_root *root)
394 {
395 	struct btrfs_fs_info *fs_info = root->fs_info;
396 
397 	switch (root->root_key.objectid) {
398 	case BTRFS_CSUM_TREE_OBJECTID:
399 	case BTRFS_EXTENT_TREE_OBJECTID:
400 	case BTRFS_FREE_SPACE_TREE_OBJECTID:
401 	case BTRFS_BLOCK_GROUP_TREE_OBJECTID:
402 		root->block_rsv = &fs_info->delayed_refs_rsv;
403 		break;
404 	case BTRFS_ROOT_TREE_OBJECTID:
405 	case BTRFS_DEV_TREE_OBJECTID:
406 	case BTRFS_QUOTA_TREE_OBJECTID:
407 		root->block_rsv = &fs_info->global_block_rsv;
408 		break;
409 	case BTRFS_CHUNK_TREE_OBJECTID:
410 		root->block_rsv = &fs_info->chunk_block_rsv;
411 		break;
412 	default:
413 		root->block_rsv = NULL;
414 		break;
415 	}
416 }
417 
418 void btrfs_init_global_block_rsv(struct btrfs_fs_info *fs_info)
419 {
420 	struct btrfs_space_info *space_info;
421 
422 	space_info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
423 	fs_info->chunk_block_rsv.space_info = space_info;
424 
425 	space_info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
426 	fs_info->global_block_rsv.space_info = space_info;
427 	fs_info->trans_block_rsv.space_info = space_info;
428 	fs_info->empty_block_rsv.space_info = space_info;
429 	fs_info->delayed_block_rsv.space_info = space_info;
430 	fs_info->delayed_refs_rsv.space_info = space_info;
431 
432 	btrfs_update_global_block_rsv(fs_info);
433 }
434 
435 void btrfs_release_global_block_rsv(struct btrfs_fs_info *fs_info)
436 {
437 	btrfs_block_rsv_release(fs_info, &fs_info->global_block_rsv, (u64)-1,
438 				NULL);
439 	WARN_ON(fs_info->trans_block_rsv.size > 0);
440 	WARN_ON(fs_info->trans_block_rsv.reserved > 0);
441 	WARN_ON(fs_info->chunk_block_rsv.size > 0);
442 	WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
443 	WARN_ON(fs_info->delayed_block_rsv.size > 0);
444 	WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
445 	WARN_ON(fs_info->delayed_refs_rsv.reserved > 0);
446 	WARN_ON(fs_info->delayed_refs_rsv.size > 0);
447 }
448 
449 static struct btrfs_block_rsv *get_block_rsv(
450 					const struct btrfs_trans_handle *trans,
451 					const struct btrfs_root *root)
452 {
453 	struct btrfs_fs_info *fs_info = root->fs_info;
454 	struct btrfs_block_rsv *block_rsv = NULL;
455 
456 	if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) ||
457 	    (root == fs_info->uuid_root) ||
458 	    (trans->adding_csums &&
459 	     root->root_key.objectid == BTRFS_CSUM_TREE_OBJECTID))
460 		block_rsv = trans->block_rsv;
461 
462 	if (!block_rsv)
463 		block_rsv = root->block_rsv;
464 
465 	if (!block_rsv)
466 		block_rsv = &fs_info->empty_block_rsv;
467 
468 	return block_rsv;
469 }
470 
471 struct btrfs_block_rsv *btrfs_use_block_rsv(struct btrfs_trans_handle *trans,
472 					    struct btrfs_root *root,
473 					    u32 blocksize)
474 {
475 	struct btrfs_fs_info *fs_info = root->fs_info;
476 	struct btrfs_block_rsv *block_rsv;
477 	struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
478 	int ret;
479 	bool global_updated = false;
480 
481 	block_rsv = get_block_rsv(trans, root);
482 
483 	if (unlikely(block_rsv->size == 0))
484 		goto try_reserve;
485 again:
486 	ret = btrfs_block_rsv_use_bytes(block_rsv, blocksize);
487 	if (!ret)
488 		return block_rsv;
489 
490 	if (block_rsv->failfast)
491 		return ERR_PTR(ret);
492 
493 	if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
494 		global_updated = true;
495 		btrfs_update_global_block_rsv(fs_info);
496 		goto again;
497 	}
498 
499 	/*
500 	 * The global reserve still exists to save us from ourselves, so don't
501 	 * warn_on if we are short on our delayed refs reserve.
502 	 */
503 	if (block_rsv->type != BTRFS_BLOCK_RSV_DELREFS &&
504 	    btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
505 		static DEFINE_RATELIMIT_STATE(_rs,
506 				DEFAULT_RATELIMIT_INTERVAL * 10,
507 				/*DEFAULT_RATELIMIT_BURST*/ 1);
508 		if (__ratelimit(&_rs))
509 			WARN(1, KERN_DEBUG
510 				"BTRFS: block rsv %d returned %d\n",
511 				block_rsv->type, ret);
512 	}
513 try_reserve:
514 	ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, blocksize,
515 					   BTRFS_RESERVE_NO_FLUSH);
516 	if (!ret)
517 		return block_rsv;
518 	/*
519 	 * If we couldn't reserve metadata bytes try and use some from
520 	 * the global reserve if its space type is the same as the global
521 	 * reservation.
522 	 */
523 	if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
524 	    block_rsv->space_info == global_rsv->space_info) {
525 		ret = btrfs_block_rsv_use_bytes(global_rsv, blocksize);
526 		if (!ret)
527 			return global_rsv;
528 	}
529 
530 	/*
531 	 * All hope is lost, but of course our reservations are overly
532 	 * pessimistic, so instead of possibly having an ENOSPC abort here, try
533 	 * one last time to force a reservation if there's enough actual space
534 	 * on disk to make the reservation.
535 	 */
536 	ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, blocksize,
537 					   BTRFS_RESERVE_FLUSH_EMERGENCY);
538 	if (!ret)
539 		return block_rsv;
540 
541 	return ERR_PTR(ret);
542 }
543