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
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)10 static u64 block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
11 struct btrfs_block_rsv *block_rsv,
12 struct btrfs_block_rsv *dest, u64 num_bytes,
13 u64 *qgroup_to_release_ret)
14 {
15 struct btrfs_space_info *space_info = block_rsv->space_info;
16 u64 qgroup_to_release = 0;
17 u64 ret;
18
19 spin_lock(&block_rsv->lock);
20 if (num_bytes == (u64)-1) {
21 num_bytes = block_rsv->size;
22 qgroup_to_release = block_rsv->qgroup_rsv_size;
23 }
24 block_rsv->size -= num_bytes;
25 if (block_rsv->reserved >= block_rsv->size) {
26 num_bytes = block_rsv->reserved - block_rsv->size;
27 block_rsv->reserved = block_rsv->size;
28 block_rsv->full = 1;
29 } else {
30 num_bytes = 0;
31 }
32 if (qgroup_to_release_ret &&
33 block_rsv->qgroup_rsv_reserved >= block_rsv->qgroup_rsv_size) {
34 qgroup_to_release = block_rsv->qgroup_rsv_reserved -
35 block_rsv->qgroup_rsv_size;
36 block_rsv->qgroup_rsv_reserved = block_rsv->qgroup_rsv_size;
37 } else {
38 qgroup_to_release = 0;
39 }
40 spin_unlock(&block_rsv->lock);
41
42 ret = num_bytes;
43 if (num_bytes > 0) {
44 if (dest) {
45 spin_lock(&dest->lock);
46 if (!dest->full) {
47 u64 bytes_to_add;
48
49 bytes_to_add = dest->size - dest->reserved;
50 bytes_to_add = min(num_bytes, bytes_to_add);
51 dest->reserved += bytes_to_add;
52 if (dest->reserved >= dest->size)
53 dest->full = 1;
54 num_bytes -= bytes_to_add;
55 }
56 spin_unlock(&dest->lock);
57 }
58 if (num_bytes)
59 btrfs_space_info_free_bytes_may_use(fs_info,
60 space_info,
61 num_bytes);
62 }
63 if (qgroup_to_release_ret)
64 *qgroup_to_release_ret = qgroup_to_release;
65 return ret;
66 }
67
btrfs_block_rsv_migrate(struct btrfs_block_rsv * src,struct btrfs_block_rsv * dst,u64 num_bytes,bool update_size)68 int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src,
69 struct btrfs_block_rsv *dst, u64 num_bytes,
70 bool update_size)
71 {
72 int ret;
73
74 ret = btrfs_block_rsv_use_bytes(src, num_bytes);
75 if (ret)
76 return ret;
77
78 btrfs_block_rsv_add_bytes(dst, num_bytes, update_size);
79 return 0;
80 }
81
btrfs_init_block_rsv(struct btrfs_block_rsv * rsv,unsigned short type)82 void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
83 {
84 memset(rsv, 0, sizeof(*rsv));
85 spin_lock_init(&rsv->lock);
86 rsv->type = type;
87 }
88
btrfs_init_metadata_block_rsv(struct btrfs_fs_info * fs_info,struct btrfs_block_rsv * rsv,unsigned short type)89 void btrfs_init_metadata_block_rsv(struct btrfs_fs_info *fs_info,
90 struct btrfs_block_rsv *rsv,
91 unsigned short type)
92 {
93 btrfs_init_block_rsv(rsv, type);
94 rsv->space_info = btrfs_find_space_info(fs_info,
95 BTRFS_BLOCK_GROUP_METADATA);
96 }
97
btrfs_alloc_block_rsv(struct btrfs_fs_info * fs_info,unsigned short type)98 struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_fs_info *fs_info,
99 unsigned short type)
100 {
101 struct btrfs_block_rsv *block_rsv;
102
103 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
104 if (!block_rsv)
105 return NULL;
106
107 btrfs_init_metadata_block_rsv(fs_info, block_rsv, type);
108 return block_rsv;
109 }
110
btrfs_free_block_rsv(struct btrfs_fs_info * fs_info,struct btrfs_block_rsv * rsv)111 void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info,
112 struct btrfs_block_rsv *rsv)
113 {
114 if (!rsv)
115 return;
116 btrfs_block_rsv_release(fs_info, rsv, (u64)-1);
117 kfree(rsv);
118 }
119
btrfs_block_rsv_add(struct btrfs_root * root,struct btrfs_block_rsv * block_rsv,u64 num_bytes,enum btrfs_reserve_flush_enum flush)120 int btrfs_block_rsv_add(struct btrfs_root *root,
121 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
122 enum btrfs_reserve_flush_enum flush)
123 {
124 int ret;
125
126 if (num_bytes == 0)
127 return 0;
128
129 ret = btrfs_reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
130 if (!ret)
131 btrfs_block_rsv_add_bytes(block_rsv, num_bytes, true);
132
133 return ret;
134 }
135
btrfs_block_rsv_check(struct btrfs_block_rsv * block_rsv,int min_factor)136 int btrfs_block_rsv_check(struct btrfs_block_rsv *block_rsv, int min_factor)
137 {
138 u64 num_bytes = 0;
139 int ret = -ENOSPC;
140
141 if (!block_rsv)
142 return 0;
143
144 spin_lock(&block_rsv->lock);
145 num_bytes = div_factor(block_rsv->size, min_factor);
146 if (block_rsv->reserved >= num_bytes)
147 ret = 0;
148 spin_unlock(&block_rsv->lock);
149
150 return ret;
151 }
152
btrfs_block_rsv_refill(struct btrfs_root * root,struct btrfs_block_rsv * block_rsv,u64 min_reserved,enum btrfs_reserve_flush_enum flush)153 int btrfs_block_rsv_refill(struct btrfs_root *root,
154 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
155 enum btrfs_reserve_flush_enum flush)
156 {
157 u64 num_bytes = 0;
158 int ret = -ENOSPC;
159
160 if (!block_rsv)
161 return 0;
162
163 spin_lock(&block_rsv->lock);
164 num_bytes = min_reserved;
165 if (block_rsv->reserved >= num_bytes)
166 ret = 0;
167 else
168 num_bytes -= block_rsv->reserved;
169 spin_unlock(&block_rsv->lock);
170
171 if (!ret)
172 return 0;
173
174 ret = btrfs_reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
175 if (!ret) {
176 btrfs_block_rsv_add_bytes(block_rsv, num_bytes, false);
177 return 0;
178 }
179
180 return ret;
181 }
182
__btrfs_block_rsv_release(struct btrfs_fs_info * fs_info,struct btrfs_block_rsv * block_rsv,u64 num_bytes,u64 * qgroup_to_release)183 u64 __btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
184 struct btrfs_block_rsv *block_rsv,
185 u64 num_bytes, u64 *qgroup_to_release)
186 {
187 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
188 struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_refs_rsv;
189 struct btrfs_block_rsv *target = NULL;
190
191 /*
192 * If we are the delayed_rsv then push to the global rsv, otherwise dump
193 * into the delayed rsv if it is not full.
194 */
195 if (block_rsv == delayed_rsv)
196 target = global_rsv;
197 else if (block_rsv != global_rsv && !delayed_rsv->full)
198 target = delayed_rsv;
199
200 if (target && block_rsv->space_info != target->space_info)
201 target = NULL;
202
203 return block_rsv_release_bytes(fs_info, block_rsv, target, num_bytes,
204 qgroup_to_release);
205 }
206
btrfs_block_rsv_use_bytes(struct btrfs_block_rsv * block_rsv,u64 num_bytes)207 int btrfs_block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, u64 num_bytes)
208 {
209 int ret = -ENOSPC;
210
211 spin_lock(&block_rsv->lock);
212 if (block_rsv->reserved >= num_bytes) {
213 block_rsv->reserved -= num_bytes;
214 if (block_rsv->reserved < block_rsv->size)
215 block_rsv->full = 0;
216 ret = 0;
217 }
218 spin_unlock(&block_rsv->lock);
219 return ret;
220 }
221
btrfs_block_rsv_add_bytes(struct btrfs_block_rsv * block_rsv,u64 num_bytes,bool update_size)222 void btrfs_block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
223 u64 num_bytes, bool update_size)
224 {
225 spin_lock(&block_rsv->lock);
226 block_rsv->reserved += num_bytes;
227 if (update_size)
228 block_rsv->size += num_bytes;
229 else if (block_rsv->reserved >= block_rsv->size)
230 block_rsv->full = 1;
231 spin_unlock(&block_rsv->lock);
232 }
233
btrfs_cond_migrate_bytes(struct btrfs_fs_info * fs_info,struct btrfs_block_rsv * dest,u64 num_bytes,int min_factor)234 int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
235 struct btrfs_block_rsv *dest, u64 num_bytes,
236 int min_factor)
237 {
238 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
239 u64 min_bytes;
240
241 if (global_rsv->space_info != dest->space_info)
242 return -ENOSPC;
243
244 spin_lock(&global_rsv->lock);
245 min_bytes = div_factor(global_rsv->size, min_factor);
246 if (global_rsv->reserved < min_bytes + num_bytes) {
247 spin_unlock(&global_rsv->lock);
248 return -ENOSPC;
249 }
250 global_rsv->reserved -= num_bytes;
251 if (global_rsv->reserved < global_rsv->size)
252 global_rsv->full = 0;
253 spin_unlock(&global_rsv->lock);
254
255 btrfs_block_rsv_add_bytes(dest, num_bytes, true);
256 return 0;
257 }
258
btrfs_update_global_block_rsv(struct btrfs_fs_info * fs_info)259 void btrfs_update_global_block_rsv(struct btrfs_fs_info *fs_info)
260 {
261 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
262 struct btrfs_space_info *sinfo = block_rsv->space_info;
263 u64 num_bytes;
264 unsigned min_items;
265
266 /*
267 * The global block rsv is based on the size of the extent tree, the
268 * checksum tree and the root tree. If the fs is empty we want to set
269 * it to a minimal amount for safety.
270 */
271 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item) +
272 btrfs_root_used(&fs_info->csum_root->root_item) +
273 btrfs_root_used(&fs_info->tree_root->root_item);
274
275 /*
276 * We at a minimum are going to modify the csum root, the tree root, and
277 * the extent root.
278 */
279 min_items = 3;
280
281 /*
282 * But we also want to reserve enough space so we can do the fallback
283 * global reserve for an unlink, which is an additional 5 items (see the
284 * comment in __unlink_start_trans for what we're modifying.)
285 *
286 * But we also need space for the delayed ref updates from the unlink,
287 * so its 10, 5 for the actual operation, and 5 for the delayed ref
288 * updates.
289 */
290 min_items += 10;
291
292 num_bytes = max_t(u64, num_bytes,
293 btrfs_calc_insert_metadata_size(fs_info, min_items));
294
295 spin_lock(&sinfo->lock);
296 spin_lock(&block_rsv->lock);
297
298 block_rsv->size = min_t(u64, num_bytes, SZ_512M);
299
300 if (block_rsv->reserved < block_rsv->size) {
301 num_bytes = block_rsv->size - block_rsv->reserved;
302 block_rsv->reserved += num_bytes;
303 btrfs_space_info_update_bytes_may_use(fs_info, sinfo,
304 num_bytes);
305 } else if (block_rsv->reserved > block_rsv->size) {
306 num_bytes = block_rsv->reserved - block_rsv->size;
307 btrfs_space_info_update_bytes_may_use(fs_info, sinfo,
308 -num_bytes);
309 block_rsv->reserved = block_rsv->size;
310 btrfs_try_granting_tickets(fs_info, sinfo);
311 }
312
313 if (block_rsv->reserved == block_rsv->size)
314 block_rsv->full = 1;
315 else
316 block_rsv->full = 0;
317
318 if (block_rsv->size >= sinfo->total_bytes)
319 sinfo->force_alloc = CHUNK_ALLOC_FORCE;
320 spin_unlock(&block_rsv->lock);
321 spin_unlock(&sinfo->lock);
322 }
323
btrfs_init_global_block_rsv(struct btrfs_fs_info * fs_info)324 void btrfs_init_global_block_rsv(struct btrfs_fs_info *fs_info)
325 {
326 struct btrfs_space_info *space_info;
327
328 space_info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
329 fs_info->chunk_block_rsv.space_info = space_info;
330
331 space_info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
332 fs_info->global_block_rsv.space_info = space_info;
333 fs_info->trans_block_rsv.space_info = space_info;
334 fs_info->empty_block_rsv.space_info = space_info;
335 fs_info->delayed_block_rsv.space_info = space_info;
336 fs_info->delayed_refs_rsv.space_info = space_info;
337
338 fs_info->extent_root->block_rsv = &fs_info->delayed_refs_rsv;
339 fs_info->csum_root->block_rsv = &fs_info->delayed_refs_rsv;
340 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
341 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
342 if (fs_info->quota_root)
343 fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
344 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
345
346 btrfs_update_global_block_rsv(fs_info);
347 }
348
btrfs_release_global_block_rsv(struct btrfs_fs_info * fs_info)349 void btrfs_release_global_block_rsv(struct btrfs_fs_info *fs_info)
350 {
351 btrfs_block_rsv_release(fs_info, &fs_info->global_block_rsv, (u64)-1);
352 WARN_ON(fs_info->trans_block_rsv.size > 0);
353 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
354 WARN_ON(fs_info->chunk_block_rsv.size > 0);
355 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
356 WARN_ON(fs_info->delayed_block_rsv.size > 0);
357 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
358 WARN_ON(fs_info->delayed_refs_rsv.reserved > 0);
359 WARN_ON(fs_info->delayed_refs_rsv.size > 0);
360 }
361
get_block_rsv(const struct btrfs_trans_handle * trans,const struct btrfs_root * root)362 static struct btrfs_block_rsv *get_block_rsv(
363 const struct btrfs_trans_handle *trans,
364 const struct btrfs_root *root)
365 {
366 struct btrfs_fs_info *fs_info = root->fs_info;
367 struct btrfs_block_rsv *block_rsv = NULL;
368
369 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
370 (root == fs_info->csum_root && trans->adding_csums) ||
371 (root == fs_info->uuid_root))
372 block_rsv = trans->block_rsv;
373
374 if (!block_rsv)
375 block_rsv = root->block_rsv;
376
377 if (!block_rsv)
378 block_rsv = &fs_info->empty_block_rsv;
379
380 return block_rsv;
381 }
382
btrfs_use_block_rsv(struct btrfs_trans_handle * trans,struct btrfs_root * root,u32 blocksize)383 struct btrfs_block_rsv *btrfs_use_block_rsv(struct btrfs_trans_handle *trans,
384 struct btrfs_root *root,
385 u32 blocksize)
386 {
387 struct btrfs_fs_info *fs_info = root->fs_info;
388 struct btrfs_block_rsv *block_rsv;
389 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
390 int ret;
391 bool global_updated = false;
392
393 block_rsv = get_block_rsv(trans, root);
394
395 if (unlikely(btrfs_block_rsv_size(block_rsv) == 0))
396 goto try_reserve;
397 again:
398 ret = btrfs_block_rsv_use_bytes(block_rsv, blocksize);
399 if (!ret)
400 return block_rsv;
401
402 if (block_rsv->failfast)
403 return ERR_PTR(ret);
404
405 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
406 global_updated = true;
407 btrfs_update_global_block_rsv(fs_info);
408 goto again;
409 }
410
411 /*
412 * The global reserve still exists to save us from ourselves, so don't
413 * warn_on if we are short on our delayed refs reserve.
414 */
415 if (block_rsv->type != BTRFS_BLOCK_RSV_DELREFS &&
416 btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
417 static DEFINE_RATELIMIT_STATE(_rs,
418 DEFAULT_RATELIMIT_INTERVAL * 10,
419 /*DEFAULT_RATELIMIT_BURST*/ 1);
420 if (__ratelimit(&_rs))
421 WARN(1, KERN_DEBUG
422 "BTRFS: block rsv returned %d\n", ret);
423 }
424 try_reserve:
425 ret = btrfs_reserve_metadata_bytes(root, block_rsv, blocksize,
426 BTRFS_RESERVE_NO_FLUSH);
427 if (!ret)
428 return block_rsv;
429 /*
430 * If we couldn't reserve metadata bytes try and use some from
431 * the global reserve if its space type is the same as the global
432 * reservation.
433 */
434 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
435 block_rsv->space_info == global_rsv->space_info) {
436 ret = btrfs_block_rsv_use_bytes(global_rsv, blocksize);
437 if (!ret)
438 return global_rsv;
439 }
440 return ERR_PTR(ret);
441 }
442