blob: bb034e19a2a8a74c64cd6e0f037b1f8a663923ca [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2011 STRATO. All rights reserved.
4 */
5
6#include <linux/sched.h>
7#include <linux/pagemap.h>
8#include <linux/writeback.h>
9#include <linux/blkdev.h>
10#include <linux/rbtree.h>
11#include <linux/slab.h>
12#include <linux/workqueue.h>
13#include <linux/btrfs.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014
15#include "ctree.h"
16#include "transaction.h"
17#include "disk-io.h"
18#include "locking.h"
19#include "ulist.h"
20#include "backref.h"
21#include "extent_io.h"
22#include "qgroup.h"
David Brazdil0f672f62019-12-10 10:32:29 +000023#include "block-group.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000024
25/* TODO XXX FIXME
26 * - subvol delete -> delete when ref goes to 0? delete limits also?
27 * - reorganize keys
28 * - compressed
29 * - sync
30 * - copy also limits on subvol creation
31 * - limit
David Brazdil0f672f62019-12-10 10:32:29 +000032 * - caches for ulists
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000033 * - performance benchmarks
34 * - check all ioctl parameters
35 */
36
37/*
38 * Helpers to access qgroup reservation
39 *
40 * Callers should ensure the lock context and type are valid
41 */
42
43static u64 qgroup_rsv_total(const struct btrfs_qgroup *qgroup)
44{
45 u64 ret = 0;
46 int i;
47
48 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
49 ret += qgroup->rsv.values[i];
50
51 return ret;
52}
53
54#ifdef CONFIG_BTRFS_DEBUG
55static const char *qgroup_rsv_type_str(enum btrfs_qgroup_rsv_type type)
56{
57 if (type == BTRFS_QGROUP_RSV_DATA)
58 return "data";
59 if (type == BTRFS_QGROUP_RSV_META_PERTRANS)
60 return "meta_pertrans";
61 if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
62 return "meta_prealloc";
63 return NULL;
64}
65#endif
66
67static void qgroup_rsv_add(struct btrfs_fs_info *fs_info,
68 struct btrfs_qgroup *qgroup, u64 num_bytes,
69 enum btrfs_qgroup_rsv_type type)
70{
71 trace_qgroup_update_reserve(fs_info, qgroup, num_bytes, type);
72 qgroup->rsv.values[type] += num_bytes;
73}
74
75static void qgroup_rsv_release(struct btrfs_fs_info *fs_info,
76 struct btrfs_qgroup *qgroup, u64 num_bytes,
77 enum btrfs_qgroup_rsv_type type)
78{
79 trace_qgroup_update_reserve(fs_info, qgroup, -(s64)num_bytes, type);
80 if (qgroup->rsv.values[type] >= num_bytes) {
81 qgroup->rsv.values[type] -= num_bytes;
82 return;
83 }
84#ifdef CONFIG_BTRFS_DEBUG
85 WARN_RATELIMIT(1,
86 "qgroup %llu %s reserved space underflow, have %llu to free %llu",
87 qgroup->qgroupid, qgroup_rsv_type_str(type),
88 qgroup->rsv.values[type], num_bytes);
89#endif
90 qgroup->rsv.values[type] = 0;
91}
92
93static void qgroup_rsv_add_by_qgroup(struct btrfs_fs_info *fs_info,
94 struct btrfs_qgroup *dest,
95 struct btrfs_qgroup *src)
96{
97 int i;
98
99 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
100 qgroup_rsv_add(fs_info, dest, src->rsv.values[i], i);
101}
102
103static void qgroup_rsv_release_by_qgroup(struct btrfs_fs_info *fs_info,
104 struct btrfs_qgroup *dest,
105 struct btrfs_qgroup *src)
106{
107 int i;
108
109 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
110 qgroup_rsv_release(fs_info, dest, src->rsv.values[i], i);
111}
112
113static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup *qg, u64 seq,
114 int mod)
115{
116 if (qg->old_refcnt < seq)
117 qg->old_refcnt = seq;
118 qg->old_refcnt += mod;
119}
120
121static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup *qg, u64 seq,
122 int mod)
123{
124 if (qg->new_refcnt < seq)
125 qg->new_refcnt = seq;
126 qg->new_refcnt += mod;
127}
128
129static inline u64 btrfs_qgroup_get_old_refcnt(struct btrfs_qgroup *qg, u64 seq)
130{
131 if (qg->old_refcnt < seq)
132 return 0;
133 return qg->old_refcnt - seq;
134}
135
136static inline u64 btrfs_qgroup_get_new_refcnt(struct btrfs_qgroup *qg, u64 seq)
137{
138 if (qg->new_refcnt < seq)
139 return 0;
140 return qg->new_refcnt - seq;
141}
142
143/*
144 * glue structure to represent the relations between qgroups.
145 */
146struct btrfs_qgroup_list {
147 struct list_head next_group;
148 struct list_head next_member;
149 struct btrfs_qgroup *group;
150 struct btrfs_qgroup *member;
151};
152
153static inline u64 qgroup_to_aux(struct btrfs_qgroup *qg)
154{
155 return (u64)(uintptr_t)qg;
156}
157
158static inline struct btrfs_qgroup* unode_aux_to_qgroup(struct ulist_node *n)
159{
160 return (struct btrfs_qgroup *)(uintptr_t)n->aux;
161}
162
163static int
164qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
165 int init_flags);
166static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
167
168/* must be called with qgroup_ioctl_lock held */
169static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
170 u64 qgroupid)
171{
172 struct rb_node *n = fs_info->qgroup_tree.rb_node;
173 struct btrfs_qgroup *qgroup;
174
175 while (n) {
176 qgroup = rb_entry(n, struct btrfs_qgroup, node);
177 if (qgroup->qgroupid < qgroupid)
178 n = n->rb_left;
179 else if (qgroup->qgroupid > qgroupid)
180 n = n->rb_right;
181 else
182 return qgroup;
183 }
184 return NULL;
185}
186
187/* must be called with qgroup_lock held */
188static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
189 u64 qgroupid)
190{
191 struct rb_node **p = &fs_info->qgroup_tree.rb_node;
192 struct rb_node *parent = NULL;
193 struct btrfs_qgroup *qgroup;
194
195 while (*p) {
196 parent = *p;
197 qgroup = rb_entry(parent, struct btrfs_qgroup, node);
198
199 if (qgroup->qgroupid < qgroupid)
200 p = &(*p)->rb_left;
201 else if (qgroup->qgroupid > qgroupid)
202 p = &(*p)->rb_right;
203 else
204 return qgroup;
205 }
206
207 qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
208 if (!qgroup)
209 return ERR_PTR(-ENOMEM);
210
211 qgroup->qgroupid = qgroupid;
212 INIT_LIST_HEAD(&qgroup->groups);
213 INIT_LIST_HEAD(&qgroup->members);
214 INIT_LIST_HEAD(&qgroup->dirty);
215
216 rb_link_node(&qgroup->node, parent, p);
217 rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
218
219 return qgroup;
220}
221
222static void __del_qgroup_rb(struct btrfs_qgroup *qgroup)
223{
224 struct btrfs_qgroup_list *list;
225
226 list_del(&qgroup->dirty);
227 while (!list_empty(&qgroup->groups)) {
228 list = list_first_entry(&qgroup->groups,
229 struct btrfs_qgroup_list, next_group);
230 list_del(&list->next_group);
231 list_del(&list->next_member);
232 kfree(list);
233 }
234
235 while (!list_empty(&qgroup->members)) {
236 list = list_first_entry(&qgroup->members,
237 struct btrfs_qgroup_list, next_member);
238 list_del(&list->next_group);
239 list_del(&list->next_member);
240 kfree(list);
241 }
242 kfree(qgroup);
243}
244
245/* must be called with qgroup_lock held */
246static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
247{
248 struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
249
250 if (!qgroup)
251 return -ENOENT;
252
253 rb_erase(&qgroup->node, &fs_info->qgroup_tree);
254 __del_qgroup_rb(qgroup);
255 return 0;
256}
257
258/* must be called with qgroup_lock held */
259static int add_relation_rb(struct btrfs_fs_info *fs_info,
260 u64 memberid, u64 parentid)
261{
262 struct btrfs_qgroup *member;
263 struct btrfs_qgroup *parent;
264 struct btrfs_qgroup_list *list;
265
266 member = find_qgroup_rb(fs_info, memberid);
267 parent = find_qgroup_rb(fs_info, parentid);
268 if (!member || !parent)
269 return -ENOENT;
270
271 list = kzalloc(sizeof(*list), GFP_ATOMIC);
272 if (!list)
273 return -ENOMEM;
274
275 list->group = parent;
276 list->member = member;
277 list_add_tail(&list->next_group, &member->groups);
278 list_add_tail(&list->next_member, &parent->members);
279
280 return 0;
281}
282
283/* must be called with qgroup_lock held */
284static int del_relation_rb(struct btrfs_fs_info *fs_info,
285 u64 memberid, u64 parentid)
286{
287 struct btrfs_qgroup *member;
288 struct btrfs_qgroup *parent;
289 struct btrfs_qgroup_list *list;
290
291 member = find_qgroup_rb(fs_info, memberid);
292 parent = find_qgroup_rb(fs_info, parentid);
293 if (!member || !parent)
294 return -ENOENT;
295
296 list_for_each_entry(list, &member->groups, next_group) {
297 if (list->group == parent) {
298 list_del(&list->next_group);
299 list_del(&list->next_member);
300 kfree(list);
301 return 0;
302 }
303 }
304 return -ENOENT;
305}
306
307#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
308int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
309 u64 rfer, u64 excl)
310{
311 struct btrfs_qgroup *qgroup;
312
313 qgroup = find_qgroup_rb(fs_info, qgroupid);
314 if (!qgroup)
315 return -EINVAL;
316 if (qgroup->rfer != rfer || qgroup->excl != excl)
317 return -EINVAL;
318 return 0;
319}
320#endif
321
322/*
323 * The full config is read in one go, only called from open_ctree()
324 * It doesn't use any locking, as at this point we're still single-threaded
325 */
326int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
327{
328 struct btrfs_key key;
329 struct btrfs_key found_key;
330 struct btrfs_root *quota_root = fs_info->quota_root;
331 struct btrfs_path *path = NULL;
332 struct extent_buffer *l;
333 int slot;
334 int ret = 0;
335 u64 flags = 0;
336 u64 rescan_progress = 0;
337
338 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
339 return 0;
340
341 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
342 if (!fs_info->qgroup_ulist) {
343 ret = -ENOMEM;
344 goto out;
345 }
346
347 path = btrfs_alloc_path();
348 if (!path) {
349 ret = -ENOMEM;
350 goto out;
351 }
352
353 /* default this to quota off, in case no status key is found */
354 fs_info->qgroup_flags = 0;
355
356 /*
357 * pass 1: read status, all qgroup infos and limits
358 */
359 key.objectid = 0;
360 key.type = 0;
361 key.offset = 0;
362 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
363 if (ret)
364 goto out;
365
366 while (1) {
367 struct btrfs_qgroup *qgroup;
368
369 slot = path->slots[0];
370 l = path->nodes[0];
371 btrfs_item_key_to_cpu(l, &found_key, slot);
372
373 if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
374 struct btrfs_qgroup_status_item *ptr;
375
376 ptr = btrfs_item_ptr(l, slot,
377 struct btrfs_qgroup_status_item);
378
379 if (btrfs_qgroup_status_version(l, ptr) !=
380 BTRFS_QGROUP_STATUS_VERSION) {
381 btrfs_err(fs_info,
382 "old qgroup version, quota disabled");
383 goto out;
384 }
385 if (btrfs_qgroup_status_generation(l, ptr) !=
386 fs_info->generation) {
387 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
388 btrfs_err(fs_info,
389 "qgroup generation mismatch, marked as inconsistent");
390 }
391 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
392 ptr);
393 rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
394 goto next1;
395 }
396
397 if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
398 found_key.type != BTRFS_QGROUP_LIMIT_KEY)
399 goto next1;
400
401 qgroup = find_qgroup_rb(fs_info, found_key.offset);
402 if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
403 (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
404 btrfs_err(fs_info, "inconsistent qgroup config");
405 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
406 }
407 if (!qgroup) {
408 qgroup = add_qgroup_rb(fs_info, found_key.offset);
409 if (IS_ERR(qgroup)) {
410 ret = PTR_ERR(qgroup);
411 goto out;
412 }
413 }
414 switch (found_key.type) {
415 case BTRFS_QGROUP_INFO_KEY: {
416 struct btrfs_qgroup_info_item *ptr;
417
418 ptr = btrfs_item_ptr(l, slot,
419 struct btrfs_qgroup_info_item);
420 qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
421 qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
422 qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
423 qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
424 /* generation currently unused */
425 break;
426 }
427 case BTRFS_QGROUP_LIMIT_KEY: {
428 struct btrfs_qgroup_limit_item *ptr;
429
430 ptr = btrfs_item_ptr(l, slot,
431 struct btrfs_qgroup_limit_item);
432 qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
433 qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
434 qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
435 qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
436 qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
437 break;
438 }
439 }
440next1:
441 ret = btrfs_next_item(quota_root, path);
442 if (ret < 0)
443 goto out;
444 if (ret)
445 break;
446 }
447 btrfs_release_path(path);
448
449 /*
450 * pass 2: read all qgroup relations
451 */
452 key.objectid = 0;
453 key.type = BTRFS_QGROUP_RELATION_KEY;
454 key.offset = 0;
455 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
456 if (ret)
457 goto out;
458 while (1) {
459 slot = path->slots[0];
460 l = path->nodes[0];
461 btrfs_item_key_to_cpu(l, &found_key, slot);
462
463 if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
464 goto next2;
465
466 if (found_key.objectid > found_key.offset) {
467 /* parent <- member, not needed to build config */
468 /* FIXME should we omit the key completely? */
469 goto next2;
470 }
471
472 ret = add_relation_rb(fs_info, found_key.objectid,
473 found_key.offset);
474 if (ret == -ENOENT) {
475 btrfs_warn(fs_info,
476 "orphan qgroup relation 0x%llx->0x%llx",
477 found_key.objectid, found_key.offset);
478 ret = 0; /* ignore the error */
479 }
480 if (ret)
481 goto out;
482next2:
483 ret = btrfs_next_item(quota_root, path);
484 if (ret < 0)
485 goto out;
486 if (ret)
487 break;
488 }
489out:
Olivier Deprez0e641232021-09-23 10:07:05 +0200490 btrfs_free_path(path);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000491 fs_info->qgroup_flags |= flags;
492 if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
493 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
494 else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
495 ret >= 0)
496 ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000497
498 if (ret < 0) {
499 ulist_free(fs_info->qgroup_ulist);
500 fs_info->qgroup_ulist = NULL;
501 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
502 }
503
504 return ret < 0 ? ret : 0;
505}
506
507/*
508 * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
509 * first two are in single-threaded paths.And for the third one, we have set
510 * quota_root to be null with qgroup_lock held before, so it is safe to clean
511 * up the in-memory structures without qgroup_lock held.
512 */
513void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
514{
515 struct rb_node *n;
516 struct btrfs_qgroup *qgroup;
517
518 while ((n = rb_first(&fs_info->qgroup_tree))) {
519 qgroup = rb_entry(n, struct btrfs_qgroup, node);
520 rb_erase(n, &fs_info->qgroup_tree);
521 __del_qgroup_rb(qgroup);
522 }
523 /*
David Brazdil0f672f62019-12-10 10:32:29 +0000524 * We call btrfs_free_qgroup_config() when unmounting
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000525 * filesystem and disabling quota, so we set qgroup_ulist
526 * to be null here to avoid double free.
527 */
528 ulist_free(fs_info->qgroup_ulist);
529 fs_info->qgroup_ulist = NULL;
530}
531
532static int add_qgroup_relation_item(struct btrfs_trans_handle *trans, u64 src,
533 u64 dst)
534{
535 int ret;
536 struct btrfs_root *quota_root = trans->fs_info->quota_root;
537 struct btrfs_path *path;
538 struct btrfs_key key;
539
540 path = btrfs_alloc_path();
541 if (!path)
542 return -ENOMEM;
543
544 key.objectid = src;
545 key.type = BTRFS_QGROUP_RELATION_KEY;
546 key.offset = dst;
547
548 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
549
550 btrfs_mark_buffer_dirty(path->nodes[0]);
551
552 btrfs_free_path(path);
553 return ret;
554}
555
556static int del_qgroup_relation_item(struct btrfs_trans_handle *trans, u64 src,
557 u64 dst)
558{
559 int ret;
560 struct btrfs_root *quota_root = trans->fs_info->quota_root;
561 struct btrfs_path *path;
562 struct btrfs_key key;
563
564 path = btrfs_alloc_path();
565 if (!path)
566 return -ENOMEM;
567
568 key.objectid = src;
569 key.type = BTRFS_QGROUP_RELATION_KEY;
570 key.offset = dst;
571
572 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
573 if (ret < 0)
574 goto out;
575
576 if (ret > 0) {
577 ret = -ENOENT;
578 goto out;
579 }
580
581 ret = btrfs_del_item(trans, quota_root, path);
582out:
583 btrfs_free_path(path);
584 return ret;
585}
586
587static int add_qgroup_item(struct btrfs_trans_handle *trans,
588 struct btrfs_root *quota_root, u64 qgroupid)
589{
590 int ret;
591 struct btrfs_path *path;
592 struct btrfs_qgroup_info_item *qgroup_info;
593 struct btrfs_qgroup_limit_item *qgroup_limit;
594 struct extent_buffer *leaf;
595 struct btrfs_key key;
596
597 if (btrfs_is_testing(quota_root->fs_info))
598 return 0;
599
600 path = btrfs_alloc_path();
601 if (!path)
602 return -ENOMEM;
603
604 key.objectid = 0;
605 key.type = BTRFS_QGROUP_INFO_KEY;
606 key.offset = qgroupid;
607
608 /*
609 * Avoid a transaction abort by catching -EEXIST here. In that
610 * case, we proceed by re-initializing the existing structure
611 * on disk.
612 */
613
614 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
615 sizeof(*qgroup_info));
616 if (ret && ret != -EEXIST)
617 goto out;
618
619 leaf = path->nodes[0];
620 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
621 struct btrfs_qgroup_info_item);
622 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
623 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
624 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
625 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
626 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
627
628 btrfs_mark_buffer_dirty(leaf);
629
630 btrfs_release_path(path);
631
632 key.type = BTRFS_QGROUP_LIMIT_KEY;
633 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
634 sizeof(*qgroup_limit));
635 if (ret && ret != -EEXIST)
636 goto out;
637
638 leaf = path->nodes[0];
639 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
640 struct btrfs_qgroup_limit_item);
641 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
642 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
643 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
644 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
645 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
646
647 btrfs_mark_buffer_dirty(leaf);
648
649 ret = 0;
650out:
651 btrfs_free_path(path);
652 return ret;
653}
654
655static int del_qgroup_item(struct btrfs_trans_handle *trans, u64 qgroupid)
656{
657 int ret;
658 struct btrfs_root *quota_root = trans->fs_info->quota_root;
659 struct btrfs_path *path;
660 struct btrfs_key key;
661
662 path = btrfs_alloc_path();
663 if (!path)
664 return -ENOMEM;
665
666 key.objectid = 0;
667 key.type = BTRFS_QGROUP_INFO_KEY;
668 key.offset = qgroupid;
669 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
670 if (ret < 0)
671 goto out;
672
673 if (ret > 0) {
674 ret = -ENOENT;
675 goto out;
676 }
677
678 ret = btrfs_del_item(trans, quota_root, path);
679 if (ret)
680 goto out;
681
682 btrfs_release_path(path);
683
684 key.type = BTRFS_QGROUP_LIMIT_KEY;
685 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
686 if (ret < 0)
687 goto out;
688
689 if (ret > 0) {
690 ret = -ENOENT;
691 goto out;
692 }
693
694 ret = btrfs_del_item(trans, quota_root, path);
695
696out:
697 btrfs_free_path(path);
698 return ret;
699}
700
701static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
702 struct btrfs_qgroup *qgroup)
703{
704 struct btrfs_root *quota_root = trans->fs_info->quota_root;
705 struct btrfs_path *path;
706 struct btrfs_key key;
707 struct extent_buffer *l;
708 struct btrfs_qgroup_limit_item *qgroup_limit;
709 int ret;
710 int slot;
711
712 key.objectid = 0;
713 key.type = BTRFS_QGROUP_LIMIT_KEY;
714 key.offset = qgroup->qgroupid;
715
716 path = btrfs_alloc_path();
717 if (!path)
718 return -ENOMEM;
719
720 ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
721 if (ret > 0)
722 ret = -ENOENT;
723
724 if (ret)
725 goto out;
726
727 l = path->nodes[0];
728 slot = path->slots[0];
729 qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
730 btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
731 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
732 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
733 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
734 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
735
736 btrfs_mark_buffer_dirty(l);
737
738out:
739 btrfs_free_path(path);
740 return ret;
741}
742
743static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
744 struct btrfs_qgroup *qgroup)
745{
746 struct btrfs_fs_info *fs_info = trans->fs_info;
747 struct btrfs_root *quota_root = fs_info->quota_root;
748 struct btrfs_path *path;
749 struct btrfs_key key;
750 struct extent_buffer *l;
751 struct btrfs_qgroup_info_item *qgroup_info;
752 int ret;
753 int slot;
754
755 if (btrfs_is_testing(fs_info))
756 return 0;
757
758 key.objectid = 0;
759 key.type = BTRFS_QGROUP_INFO_KEY;
760 key.offset = qgroup->qgroupid;
761
762 path = btrfs_alloc_path();
763 if (!path)
764 return -ENOMEM;
765
766 ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
767 if (ret > 0)
768 ret = -ENOENT;
769
770 if (ret)
771 goto out;
772
773 l = path->nodes[0];
774 slot = path->slots[0];
775 qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
776 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
777 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
778 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
779 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
780 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
781
782 btrfs_mark_buffer_dirty(l);
783
784out:
785 btrfs_free_path(path);
786 return ret;
787}
788
789static int update_qgroup_status_item(struct btrfs_trans_handle *trans)
790{
791 struct btrfs_fs_info *fs_info = trans->fs_info;
792 struct btrfs_root *quota_root = fs_info->quota_root;
793 struct btrfs_path *path;
794 struct btrfs_key key;
795 struct extent_buffer *l;
796 struct btrfs_qgroup_status_item *ptr;
797 int ret;
798 int slot;
799
800 key.objectid = 0;
801 key.type = BTRFS_QGROUP_STATUS_KEY;
802 key.offset = 0;
803
804 path = btrfs_alloc_path();
805 if (!path)
806 return -ENOMEM;
807
808 ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
809 if (ret > 0)
810 ret = -ENOENT;
811
812 if (ret)
813 goto out;
814
815 l = path->nodes[0];
816 slot = path->slots[0];
817 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
818 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
819 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
820 btrfs_set_qgroup_status_rescan(l, ptr,
821 fs_info->qgroup_rescan_progress.objectid);
822
823 btrfs_mark_buffer_dirty(l);
824
825out:
826 btrfs_free_path(path);
827 return ret;
828}
829
830/*
831 * called with qgroup_lock held
832 */
833static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
834 struct btrfs_root *root)
835{
836 struct btrfs_path *path;
837 struct btrfs_key key;
838 struct extent_buffer *leaf = NULL;
839 int ret;
840 int nr = 0;
841
842 path = btrfs_alloc_path();
843 if (!path)
844 return -ENOMEM;
845
846 path->leave_spinning = 1;
847
848 key.objectid = 0;
849 key.offset = 0;
850 key.type = 0;
851
852 while (1) {
853 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
854 if (ret < 0)
855 goto out;
856 leaf = path->nodes[0];
857 nr = btrfs_header_nritems(leaf);
858 if (!nr)
859 break;
860 /*
861 * delete the leaf one by one
862 * since the whole tree is going
863 * to be deleted.
864 */
865 path->slots[0] = 0;
866 ret = btrfs_del_items(trans, root, path, 0, nr);
867 if (ret)
868 goto out;
869
870 btrfs_release_path(path);
871 }
872 ret = 0;
873out:
874 btrfs_free_path(path);
875 return ret;
876}
877
878int btrfs_quota_enable(struct btrfs_fs_info *fs_info)
879{
880 struct btrfs_root *quota_root;
881 struct btrfs_root *tree_root = fs_info->tree_root;
882 struct btrfs_path *path = NULL;
883 struct btrfs_qgroup_status_item *ptr;
884 struct extent_buffer *leaf;
885 struct btrfs_key key;
886 struct btrfs_key found_key;
887 struct btrfs_qgroup *qgroup = NULL;
888 struct btrfs_trans_handle *trans = NULL;
Olivier Deprez0e641232021-09-23 10:07:05 +0200889 struct ulist *ulist = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000890 int ret = 0;
891 int slot;
892
893 mutex_lock(&fs_info->qgroup_ioctl_lock);
894 if (fs_info->quota_root)
895 goto out;
896
Olivier Deprez0e641232021-09-23 10:07:05 +0200897 ulist = ulist_alloc(GFP_KERNEL);
898 if (!ulist) {
David Brazdil0f672f62019-12-10 10:32:29 +0000899 ret = -ENOMEM;
900 goto out;
901 }
902
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000903 /*
Olivier Deprez0e641232021-09-23 10:07:05 +0200904 * Unlock qgroup_ioctl_lock before starting the transaction. This is to
905 * avoid lock acquisition inversion problems (reported by lockdep) between
906 * qgroup_ioctl_lock and the vfs freeze semaphores, acquired when we
907 * start a transaction.
908 * After we started the transaction lock qgroup_ioctl_lock again and
909 * check if someone else created the quota root in the meanwhile. If so,
910 * just return success and release the transaction handle.
911 *
912 * Also we don't need to worry about someone else calling
913 * btrfs_sysfs_add_qgroups() after we unlock and getting an error because
914 * that function returns 0 (success) when the sysfs entries already exist.
915 */
916 mutex_unlock(&fs_info->qgroup_ioctl_lock);
917
918 /*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000919 * 1 for quota root item
920 * 1 for BTRFS_QGROUP_STATUS item
921 *
922 * Yet we also need 2*n items for a QGROUP_INFO/QGROUP_LIMIT items
923 * per subvolume. However those are not currently reserved since it
924 * would be a lot of overkill.
925 */
926 trans = btrfs_start_transaction(tree_root, 2);
Olivier Deprez0e641232021-09-23 10:07:05 +0200927
928 mutex_lock(&fs_info->qgroup_ioctl_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000929 if (IS_ERR(trans)) {
930 ret = PTR_ERR(trans);
931 trans = NULL;
932 goto out;
933 }
934
Olivier Deprez0e641232021-09-23 10:07:05 +0200935 if (fs_info->quota_root)
936 goto out;
937
938 fs_info->qgroup_ulist = ulist;
939 ulist = NULL;
940
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000941 /*
942 * initially create the quota tree
943 */
David Brazdil0f672f62019-12-10 10:32:29 +0000944 quota_root = btrfs_create_tree(trans, BTRFS_QUOTA_TREE_OBJECTID);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000945 if (IS_ERR(quota_root)) {
946 ret = PTR_ERR(quota_root);
947 btrfs_abort_transaction(trans, ret);
948 goto out;
949 }
950
951 path = btrfs_alloc_path();
952 if (!path) {
953 ret = -ENOMEM;
954 btrfs_abort_transaction(trans, ret);
955 goto out_free_root;
956 }
957
958 key.objectid = 0;
959 key.type = BTRFS_QGROUP_STATUS_KEY;
960 key.offset = 0;
961
962 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
963 sizeof(*ptr));
964 if (ret) {
965 btrfs_abort_transaction(trans, ret);
966 goto out_free_path;
967 }
968
969 leaf = path->nodes[0];
970 ptr = btrfs_item_ptr(leaf, path->slots[0],
971 struct btrfs_qgroup_status_item);
972 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
973 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
974 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
975 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
976 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
977 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
978
979 btrfs_mark_buffer_dirty(leaf);
980
981 key.objectid = 0;
982 key.type = BTRFS_ROOT_REF_KEY;
983 key.offset = 0;
984
985 btrfs_release_path(path);
986 ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
987 if (ret > 0)
988 goto out_add_root;
989 if (ret < 0) {
990 btrfs_abort_transaction(trans, ret);
991 goto out_free_path;
992 }
993
994 while (1) {
995 slot = path->slots[0];
996 leaf = path->nodes[0];
997 btrfs_item_key_to_cpu(leaf, &found_key, slot);
998
999 if (found_key.type == BTRFS_ROOT_REF_KEY) {
1000 ret = add_qgroup_item(trans, quota_root,
1001 found_key.offset);
1002 if (ret) {
1003 btrfs_abort_transaction(trans, ret);
1004 goto out_free_path;
1005 }
1006
1007 qgroup = add_qgroup_rb(fs_info, found_key.offset);
1008 if (IS_ERR(qgroup)) {
1009 ret = PTR_ERR(qgroup);
1010 btrfs_abort_transaction(trans, ret);
1011 goto out_free_path;
1012 }
1013 }
1014 ret = btrfs_next_item(tree_root, path);
1015 if (ret < 0) {
1016 btrfs_abort_transaction(trans, ret);
1017 goto out_free_path;
1018 }
1019 if (ret)
1020 break;
1021 }
1022
1023out_add_root:
1024 btrfs_release_path(path);
1025 ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
1026 if (ret) {
1027 btrfs_abort_transaction(trans, ret);
1028 goto out_free_path;
1029 }
1030
1031 qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
1032 if (IS_ERR(qgroup)) {
1033 ret = PTR_ERR(qgroup);
1034 btrfs_abort_transaction(trans, ret);
1035 goto out_free_path;
1036 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001037
1038 ret = btrfs_commit_transaction(trans);
1039 trans = NULL;
1040 if (ret)
1041 goto out_free_path;
1042
David Brazdil0f672f62019-12-10 10:32:29 +00001043 /*
1044 * Set quota enabled flag after committing the transaction, to avoid
1045 * deadlocks on fs_info->qgroup_ioctl_lock with concurrent snapshot
1046 * creation.
1047 */
1048 spin_lock(&fs_info->qgroup_lock);
1049 fs_info->quota_root = quota_root;
1050 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
1051 spin_unlock(&fs_info->qgroup_lock);
1052
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001053 ret = qgroup_rescan_init(fs_info, 0, 1);
1054 if (!ret) {
1055 qgroup_rescan_zero_tracking(fs_info);
Olivier Deprez0e641232021-09-23 10:07:05 +02001056 fs_info->qgroup_rescan_running = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001057 btrfs_queue_work(fs_info->qgroup_rescan_workers,
1058 &fs_info->qgroup_rescan_work);
1059 }
1060
1061out_free_path:
1062 btrfs_free_path(path);
1063out_free_root:
1064 if (ret) {
1065 free_extent_buffer(quota_root->node);
1066 free_extent_buffer(quota_root->commit_root);
1067 kfree(quota_root);
1068 }
1069out:
1070 if (ret) {
1071 ulist_free(fs_info->qgroup_ulist);
1072 fs_info->qgroup_ulist = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001073 }
1074 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Olivier Deprez0e641232021-09-23 10:07:05 +02001075 if (ret && trans)
1076 btrfs_end_transaction(trans);
1077 else if (trans)
1078 ret = btrfs_end_transaction(trans);
1079 ulist_free(ulist);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001080 return ret;
1081}
1082
1083int btrfs_quota_disable(struct btrfs_fs_info *fs_info)
1084{
1085 struct btrfs_root *quota_root;
1086 struct btrfs_trans_handle *trans = NULL;
1087 int ret = 0;
1088
1089 mutex_lock(&fs_info->qgroup_ioctl_lock);
1090 if (!fs_info->quota_root)
1091 goto out;
Olivier Deprez0e641232021-09-23 10:07:05 +02001092 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001093
1094 /*
1095 * 1 For the root item
1096 *
1097 * We should also reserve enough items for the quota tree deletion in
1098 * btrfs_clean_quota_tree but this is not done.
Olivier Deprez0e641232021-09-23 10:07:05 +02001099 *
1100 * Also, we must always start a transaction without holding the mutex
1101 * qgroup_ioctl_lock, see btrfs_quota_enable().
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001102 */
1103 trans = btrfs_start_transaction(fs_info->tree_root, 1);
Olivier Deprez0e641232021-09-23 10:07:05 +02001104
1105 mutex_lock(&fs_info->qgroup_ioctl_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001106 if (IS_ERR(trans)) {
1107 ret = PTR_ERR(trans);
Olivier Deprez0e641232021-09-23 10:07:05 +02001108 trans = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001109 goto out;
1110 }
1111
Olivier Deprez0e641232021-09-23 10:07:05 +02001112 if (!fs_info->quota_root)
1113 goto out;
1114
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001115 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
1116 btrfs_qgroup_wait_for_completion(fs_info, false);
1117 spin_lock(&fs_info->qgroup_lock);
1118 quota_root = fs_info->quota_root;
1119 fs_info->quota_root = NULL;
1120 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
1121 spin_unlock(&fs_info->qgroup_lock);
1122
1123 btrfs_free_qgroup_config(fs_info);
1124
1125 ret = btrfs_clean_quota_tree(trans, quota_root);
1126 if (ret) {
1127 btrfs_abort_transaction(trans, ret);
Olivier Deprez0e641232021-09-23 10:07:05 +02001128 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001129 }
1130
1131 ret = btrfs_del_root(trans, &quota_root->root_key);
1132 if (ret) {
1133 btrfs_abort_transaction(trans, ret);
Olivier Deprez0e641232021-09-23 10:07:05 +02001134 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001135 }
1136
1137 list_del(&quota_root->dirty_list);
1138
1139 btrfs_tree_lock(quota_root->node);
David Brazdil0f672f62019-12-10 10:32:29 +00001140 btrfs_clean_tree_block(quota_root->node);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001141 btrfs_tree_unlock(quota_root->node);
1142 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
1143
1144 free_extent_buffer(quota_root->node);
1145 free_extent_buffer(quota_root->commit_root);
1146 kfree(quota_root);
1147
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001148out:
1149 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Olivier Deprez0e641232021-09-23 10:07:05 +02001150 if (ret && trans)
1151 btrfs_end_transaction(trans);
1152 else if (trans)
1153 ret = btrfs_end_transaction(trans);
1154
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001155 return ret;
1156}
1157
1158static void qgroup_dirty(struct btrfs_fs_info *fs_info,
1159 struct btrfs_qgroup *qgroup)
1160{
1161 if (list_empty(&qgroup->dirty))
1162 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
1163}
1164
1165/*
1166 * The easy accounting, we're updating qgroup relationship whose child qgroup
1167 * only has exclusive extents.
1168 *
David Brazdil0f672f62019-12-10 10:32:29 +00001169 * In this case, all exclusive extents will also be exclusive for parent, so
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001170 * excl/rfer just get added/removed.
1171 *
1172 * So is qgroup reservation space, which should also be added/removed to
1173 * parent.
1174 * Or when child tries to release reservation space, parent will underflow its
1175 * reservation (for relationship adding case).
1176 *
1177 * Caller should hold fs_info->qgroup_lock.
1178 */
1179static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
1180 struct ulist *tmp, u64 ref_root,
1181 struct btrfs_qgroup *src, int sign)
1182{
1183 struct btrfs_qgroup *qgroup;
1184 struct btrfs_qgroup_list *glist;
1185 struct ulist_node *unode;
1186 struct ulist_iterator uiter;
1187 u64 num_bytes = src->excl;
1188 int ret = 0;
1189
1190 qgroup = find_qgroup_rb(fs_info, ref_root);
1191 if (!qgroup)
1192 goto out;
1193
1194 qgroup->rfer += sign * num_bytes;
1195 qgroup->rfer_cmpr += sign * num_bytes;
1196
1197 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1198 qgroup->excl += sign * num_bytes;
1199 qgroup->excl_cmpr += sign * num_bytes;
1200
1201 if (sign > 0)
1202 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
1203 else
1204 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
1205
1206 qgroup_dirty(fs_info, qgroup);
1207
1208 /* Get all of the parent groups that contain this qgroup */
1209 list_for_each_entry(glist, &qgroup->groups, next_group) {
1210 ret = ulist_add(tmp, glist->group->qgroupid,
1211 qgroup_to_aux(glist->group), GFP_ATOMIC);
1212 if (ret < 0)
1213 goto out;
1214 }
1215
1216 /* Iterate all of the parents and adjust their reference counts */
1217 ULIST_ITER_INIT(&uiter);
1218 while ((unode = ulist_next(tmp, &uiter))) {
1219 qgroup = unode_aux_to_qgroup(unode);
1220 qgroup->rfer += sign * num_bytes;
1221 qgroup->rfer_cmpr += sign * num_bytes;
1222 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1223 qgroup->excl += sign * num_bytes;
1224 if (sign > 0)
1225 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
1226 else
1227 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
1228 qgroup->excl_cmpr += sign * num_bytes;
1229 qgroup_dirty(fs_info, qgroup);
1230
1231 /* Add any parents of the parents */
1232 list_for_each_entry(glist, &qgroup->groups, next_group) {
1233 ret = ulist_add(tmp, glist->group->qgroupid,
1234 qgroup_to_aux(glist->group), GFP_ATOMIC);
1235 if (ret < 0)
1236 goto out;
1237 }
1238 }
1239 ret = 0;
1240out:
1241 return ret;
1242}
1243
1244
1245/*
1246 * Quick path for updating qgroup with only excl refs.
1247 *
1248 * In that case, just update all parent will be enough.
1249 * Or we needs to do a full rescan.
1250 * Caller should also hold fs_info->qgroup_lock.
1251 *
1252 * Return 0 for quick update, return >0 for need to full rescan
1253 * and mark INCONSISTENT flag.
1254 * Return < 0 for other error.
1255 */
1256static int quick_update_accounting(struct btrfs_fs_info *fs_info,
1257 struct ulist *tmp, u64 src, u64 dst,
1258 int sign)
1259{
1260 struct btrfs_qgroup *qgroup;
1261 int ret = 1;
1262 int err = 0;
1263
1264 qgroup = find_qgroup_rb(fs_info, src);
1265 if (!qgroup)
1266 goto out;
1267 if (qgroup->excl == qgroup->rfer) {
1268 ret = 0;
1269 err = __qgroup_excl_accounting(fs_info, tmp, dst,
1270 qgroup, sign);
1271 if (err < 0) {
1272 ret = err;
1273 goto out;
1274 }
1275 }
1276out:
1277 if (ret)
1278 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1279 return ret;
1280}
1281
1282int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
1283 u64 dst)
1284{
1285 struct btrfs_fs_info *fs_info = trans->fs_info;
1286 struct btrfs_root *quota_root;
1287 struct btrfs_qgroup *parent;
1288 struct btrfs_qgroup *member;
1289 struct btrfs_qgroup_list *list;
1290 struct ulist *tmp;
1291 int ret = 0;
1292
1293 /* Check the level of src and dst first */
1294 if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
1295 return -EINVAL;
1296
1297 tmp = ulist_alloc(GFP_KERNEL);
1298 if (!tmp)
1299 return -ENOMEM;
1300
1301 mutex_lock(&fs_info->qgroup_ioctl_lock);
1302 quota_root = fs_info->quota_root;
1303 if (!quota_root) {
1304 ret = -EINVAL;
1305 goto out;
1306 }
1307 member = find_qgroup_rb(fs_info, src);
1308 parent = find_qgroup_rb(fs_info, dst);
1309 if (!member || !parent) {
1310 ret = -EINVAL;
1311 goto out;
1312 }
1313
1314 /* check if such qgroup relation exist firstly */
1315 list_for_each_entry(list, &member->groups, next_group) {
1316 if (list->group == parent) {
1317 ret = -EEXIST;
1318 goto out;
1319 }
1320 }
1321
1322 ret = add_qgroup_relation_item(trans, src, dst);
1323 if (ret)
1324 goto out;
1325
1326 ret = add_qgroup_relation_item(trans, dst, src);
1327 if (ret) {
1328 del_qgroup_relation_item(trans, src, dst);
1329 goto out;
1330 }
1331
1332 spin_lock(&fs_info->qgroup_lock);
1333 ret = add_relation_rb(fs_info, src, dst);
1334 if (ret < 0) {
1335 spin_unlock(&fs_info->qgroup_lock);
1336 goto out;
1337 }
1338 ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
1339 spin_unlock(&fs_info->qgroup_lock);
1340out:
1341 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1342 ulist_free(tmp);
1343 return ret;
1344}
1345
1346static int __del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
1347 u64 dst)
1348{
1349 struct btrfs_fs_info *fs_info = trans->fs_info;
1350 struct btrfs_root *quota_root;
1351 struct btrfs_qgroup *parent;
1352 struct btrfs_qgroup *member;
1353 struct btrfs_qgroup_list *list;
1354 struct ulist *tmp;
David Brazdil0f672f62019-12-10 10:32:29 +00001355 bool found = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001356 int ret = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001357 int ret2;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001358
1359 tmp = ulist_alloc(GFP_KERNEL);
1360 if (!tmp)
1361 return -ENOMEM;
1362
1363 quota_root = fs_info->quota_root;
1364 if (!quota_root) {
1365 ret = -EINVAL;
1366 goto out;
1367 }
1368
1369 member = find_qgroup_rb(fs_info, src);
1370 parent = find_qgroup_rb(fs_info, dst);
David Brazdil0f672f62019-12-10 10:32:29 +00001371 /*
1372 * The parent/member pair doesn't exist, then try to delete the dead
1373 * relation items only.
1374 */
1375 if (!member || !parent)
1376 goto delete_item;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001377
1378 /* check if such qgroup relation exist firstly */
1379 list_for_each_entry(list, &member->groups, next_group) {
David Brazdil0f672f62019-12-10 10:32:29 +00001380 if (list->group == parent) {
1381 found = true;
1382 break;
1383 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001384 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001385
David Brazdil0f672f62019-12-10 10:32:29 +00001386delete_item:
1387 ret = del_qgroup_relation_item(trans, src, dst);
1388 if (ret < 0 && ret != -ENOENT)
1389 goto out;
1390 ret2 = del_qgroup_relation_item(trans, dst, src);
1391 if (ret2 < 0 && ret2 != -ENOENT)
1392 goto out;
1393
1394 /* At least one deletion succeeded, return 0 */
1395 if (!ret || !ret2)
1396 ret = 0;
1397
1398 if (found) {
1399 spin_lock(&fs_info->qgroup_lock);
1400 del_relation_rb(fs_info, src, dst);
1401 ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
1402 spin_unlock(&fs_info->qgroup_lock);
1403 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001404out:
1405 ulist_free(tmp);
1406 return ret;
1407}
1408
1409int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
1410 u64 dst)
1411{
1412 struct btrfs_fs_info *fs_info = trans->fs_info;
1413 int ret = 0;
1414
1415 mutex_lock(&fs_info->qgroup_ioctl_lock);
1416 ret = __del_qgroup_relation(trans, src, dst);
1417 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1418
1419 return ret;
1420}
1421
1422int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
1423{
1424 struct btrfs_fs_info *fs_info = trans->fs_info;
1425 struct btrfs_root *quota_root;
1426 struct btrfs_qgroup *qgroup;
1427 int ret = 0;
1428
1429 mutex_lock(&fs_info->qgroup_ioctl_lock);
1430 quota_root = fs_info->quota_root;
1431 if (!quota_root) {
1432 ret = -EINVAL;
1433 goto out;
1434 }
1435 qgroup = find_qgroup_rb(fs_info, qgroupid);
1436 if (qgroup) {
1437 ret = -EEXIST;
1438 goto out;
1439 }
1440
1441 ret = add_qgroup_item(trans, quota_root, qgroupid);
1442 if (ret)
1443 goto out;
1444
1445 spin_lock(&fs_info->qgroup_lock);
1446 qgroup = add_qgroup_rb(fs_info, qgroupid);
1447 spin_unlock(&fs_info->qgroup_lock);
1448
1449 if (IS_ERR(qgroup))
1450 ret = PTR_ERR(qgroup);
1451out:
1452 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1453 return ret;
1454}
1455
1456int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
1457{
1458 struct btrfs_fs_info *fs_info = trans->fs_info;
1459 struct btrfs_root *quota_root;
1460 struct btrfs_qgroup *qgroup;
1461 struct btrfs_qgroup_list *list;
1462 int ret = 0;
1463
1464 mutex_lock(&fs_info->qgroup_ioctl_lock);
1465 quota_root = fs_info->quota_root;
1466 if (!quota_root) {
1467 ret = -EINVAL;
1468 goto out;
1469 }
1470
1471 qgroup = find_qgroup_rb(fs_info, qgroupid);
1472 if (!qgroup) {
1473 ret = -ENOENT;
1474 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001475 }
David Brazdil0f672f62019-12-10 10:32:29 +00001476
1477 /* Check if there are no children of this qgroup */
1478 if (!list_empty(&qgroup->members)) {
1479 ret = -EBUSY;
1480 goto out;
1481 }
1482
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001483 ret = del_qgroup_item(trans, qgroupid);
1484 if (ret && ret != -ENOENT)
1485 goto out;
1486
1487 while (!list_empty(&qgroup->groups)) {
1488 list = list_first_entry(&qgroup->groups,
1489 struct btrfs_qgroup_list, next_group);
1490 ret = __del_qgroup_relation(trans, qgroupid,
1491 list->group->qgroupid);
1492 if (ret)
1493 goto out;
1494 }
1495
1496 spin_lock(&fs_info->qgroup_lock);
1497 del_qgroup_rb(fs_info, qgroupid);
1498 spin_unlock(&fs_info->qgroup_lock);
1499out:
1500 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1501 return ret;
1502}
1503
1504int btrfs_limit_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid,
1505 struct btrfs_qgroup_limit *limit)
1506{
1507 struct btrfs_fs_info *fs_info = trans->fs_info;
1508 struct btrfs_root *quota_root;
1509 struct btrfs_qgroup *qgroup;
1510 int ret = 0;
1511 /* Sometimes we would want to clear the limit on this qgroup.
1512 * To meet this requirement, we treat the -1 as a special value
1513 * which tell kernel to clear the limit on this qgroup.
1514 */
1515 const u64 CLEAR_VALUE = -1;
1516
1517 mutex_lock(&fs_info->qgroup_ioctl_lock);
1518 quota_root = fs_info->quota_root;
1519 if (!quota_root) {
1520 ret = -EINVAL;
1521 goto out;
1522 }
1523
1524 qgroup = find_qgroup_rb(fs_info, qgroupid);
1525 if (!qgroup) {
1526 ret = -ENOENT;
1527 goto out;
1528 }
1529
1530 spin_lock(&fs_info->qgroup_lock);
1531 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
1532 if (limit->max_rfer == CLEAR_VALUE) {
1533 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1534 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1535 qgroup->max_rfer = 0;
1536 } else {
1537 qgroup->max_rfer = limit->max_rfer;
1538 }
1539 }
1540 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
1541 if (limit->max_excl == CLEAR_VALUE) {
1542 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1543 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1544 qgroup->max_excl = 0;
1545 } else {
1546 qgroup->max_excl = limit->max_excl;
1547 }
1548 }
1549 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
1550 if (limit->rsv_rfer == CLEAR_VALUE) {
1551 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1552 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1553 qgroup->rsv_rfer = 0;
1554 } else {
1555 qgroup->rsv_rfer = limit->rsv_rfer;
1556 }
1557 }
1558 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
1559 if (limit->rsv_excl == CLEAR_VALUE) {
1560 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1561 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1562 qgroup->rsv_excl = 0;
1563 } else {
1564 qgroup->rsv_excl = limit->rsv_excl;
1565 }
1566 }
1567 qgroup->lim_flags |= limit->flags;
1568
1569 spin_unlock(&fs_info->qgroup_lock);
1570
1571 ret = update_qgroup_limit_item(trans, qgroup);
1572 if (ret) {
1573 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1574 btrfs_info(fs_info, "unable to update quota limit for %llu",
1575 qgroupid);
1576 }
1577
1578out:
1579 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1580 return ret;
1581}
1582
1583int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
1584 struct btrfs_delayed_ref_root *delayed_refs,
1585 struct btrfs_qgroup_extent_record *record)
1586{
1587 struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
1588 struct rb_node *parent_node = NULL;
1589 struct btrfs_qgroup_extent_record *entry;
1590 u64 bytenr = record->bytenr;
1591
1592 lockdep_assert_held(&delayed_refs->lock);
1593 trace_btrfs_qgroup_trace_extent(fs_info, record);
1594
1595 while (*p) {
1596 parent_node = *p;
1597 entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
1598 node);
David Brazdil0f672f62019-12-10 10:32:29 +00001599 if (bytenr < entry->bytenr) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001600 p = &(*p)->rb_left;
David Brazdil0f672f62019-12-10 10:32:29 +00001601 } else if (bytenr > entry->bytenr) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001602 p = &(*p)->rb_right;
David Brazdil0f672f62019-12-10 10:32:29 +00001603 } else {
1604 if (record->data_rsv && !entry->data_rsv) {
1605 entry->data_rsv = record->data_rsv;
1606 entry->data_rsv_refroot =
1607 record->data_rsv_refroot;
1608 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001609 return 1;
David Brazdil0f672f62019-12-10 10:32:29 +00001610 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001611 }
1612
1613 rb_link_node(&record->node, parent_node, p);
1614 rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
1615 return 0;
1616}
1617
1618int btrfs_qgroup_trace_extent_post(struct btrfs_fs_info *fs_info,
1619 struct btrfs_qgroup_extent_record *qrecord)
1620{
1621 struct ulist *old_root;
1622 u64 bytenr = qrecord->bytenr;
1623 int ret;
1624
1625 ret = btrfs_find_all_roots(NULL, fs_info, bytenr, 0, &old_root, false);
1626 if (ret < 0) {
1627 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1628 btrfs_warn(fs_info,
1629"error accounting new delayed refs extent (err code: %d), quota inconsistent",
1630 ret);
1631 return 0;
1632 }
1633
1634 /*
1635 * Here we don't need to get the lock of
1636 * trans->transaction->delayed_refs, since inserted qrecord won't
1637 * be deleted, only qrecord->node may be modified (new qrecord insert)
1638 *
1639 * So modifying qrecord->old_roots is safe here
1640 */
1641 qrecord->old_roots = old_root;
1642 return 0;
1643}
1644
1645int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans, u64 bytenr,
1646 u64 num_bytes, gfp_t gfp_flag)
1647{
1648 struct btrfs_fs_info *fs_info = trans->fs_info;
1649 struct btrfs_qgroup_extent_record *record;
1650 struct btrfs_delayed_ref_root *delayed_refs;
1651 int ret;
1652
1653 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)
1654 || bytenr == 0 || num_bytes == 0)
1655 return 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001656 record = kzalloc(sizeof(*record), gfp_flag);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001657 if (!record)
1658 return -ENOMEM;
1659
1660 delayed_refs = &trans->transaction->delayed_refs;
1661 record->bytenr = bytenr;
1662 record->num_bytes = num_bytes;
1663 record->old_roots = NULL;
1664
1665 spin_lock(&delayed_refs->lock);
1666 ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record);
1667 spin_unlock(&delayed_refs->lock);
1668 if (ret > 0) {
1669 kfree(record);
1670 return 0;
1671 }
1672 return btrfs_qgroup_trace_extent_post(fs_info, record);
1673}
1674
1675int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
1676 struct extent_buffer *eb)
1677{
1678 struct btrfs_fs_info *fs_info = trans->fs_info;
1679 int nr = btrfs_header_nritems(eb);
1680 int i, extent_type, ret;
1681 struct btrfs_key key;
1682 struct btrfs_file_extent_item *fi;
1683 u64 bytenr, num_bytes;
1684
1685 /* We can be called directly from walk_up_proc() */
1686 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
1687 return 0;
1688
1689 for (i = 0; i < nr; i++) {
1690 btrfs_item_key_to_cpu(eb, &key, i);
1691
1692 if (key.type != BTRFS_EXTENT_DATA_KEY)
1693 continue;
1694
1695 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
1696 /* filter out non qgroup-accountable extents */
1697 extent_type = btrfs_file_extent_type(eb, fi);
1698
1699 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
1700 continue;
1701
1702 bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1703 if (!bytenr)
1704 continue;
1705
1706 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1707
1708 ret = btrfs_qgroup_trace_extent(trans, bytenr, num_bytes,
1709 GFP_NOFS);
1710 if (ret)
1711 return ret;
1712 }
1713 cond_resched();
1714 return 0;
1715}
1716
1717/*
1718 * Walk up the tree from the bottom, freeing leaves and any interior
1719 * nodes which have had all slots visited. If a node (leaf or
1720 * interior) is freed, the node above it will have it's slot
1721 * incremented. The root node will never be freed.
1722 *
1723 * At the end of this function, we should have a path which has all
1724 * slots incremented to the next position for a search. If we need to
1725 * read a new node it will be NULL and the node above it will have the
1726 * correct slot selected for a later read.
1727 *
1728 * If we increment the root nodes slot counter past the number of
1729 * elements, 1 is returned to signal completion of the search.
1730 */
1731static int adjust_slots_upwards(struct btrfs_path *path, int root_level)
1732{
1733 int level = 0;
1734 int nr, slot;
1735 struct extent_buffer *eb;
1736
1737 if (root_level == 0)
1738 return 1;
1739
1740 while (level <= root_level) {
1741 eb = path->nodes[level];
1742 nr = btrfs_header_nritems(eb);
1743 path->slots[level]++;
1744 slot = path->slots[level];
1745 if (slot >= nr || level == 0) {
1746 /*
1747 * Don't free the root - we will detect this
1748 * condition after our loop and return a
1749 * positive value for caller to stop walking the tree.
1750 */
1751 if (level != root_level) {
1752 btrfs_tree_unlock_rw(eb, path->locks[level]);
1753 path->locks[level] = 0;
1754
1755 free_extent_buffer(eb);
1756 path->nodes[level] = NULL;
1757 path->slots[level] = 0;
1758 }
1759 } else {
1760 /*
1761 * We have a valid slot to walk back down
1762 * from. Stop here so caller can process these
1763 * new nodes.
1764 */
1765 break;
1766 }
1767
1768 level++;
1769 }
1770
1771 eb = path->nodes[root_level];
1772 if (path->slots[root_level] >= btrfs_header_nritems(eb))
1773 return 1;
1774
1775 return 0;
1776}
1777
David Brazdil0f672f62019-12-10 10:32:29 +00001778/*
1779 * Helper function to trace a subtree tree block swap.
1780 *
1781 * The swap will happen in highest tree block, but there may be a lot of
1782 * tree blocks involved.
1783 *
1784 * For example:
1785 * OO = Old tree blocks
1786 * NN = New tree blocks allocated during balance
1787 *
1788 * File tree (257) Reloc tree for 257
1789 * L2 OO NN
1790 * / \ / \
1791 * L1 OO OO (a) OO NN (a)
1792 * / \ / \ / \ / \
1793 * L0 OO OO OO OO OO OO NN NN
1794 * (b) (c) (b) (c)
1795 *
1796 * When calling qgroup_trace_extent_swap(), we will pass:
1797 * @src_eb = OO(a)
1798 * @dst_path = [ nodes[1] = NN(a), nodes[0] = NN(c) ]
1799 * @dst_level = 0
1800 * @root_level = 1
1801 *
1802 * In that case, qgroup_trace_extent_swap() will search from OO(a) to
1803 * reach OO(c), then mark both OO(c) and NN(c) as qgroup dirty.
1804 *
1805 * The main work of qgroup_trace_extent_swap() can be split into 3 parts:
1806 *
1807 * 1) Tree search from @src_eb
1808 * It should acts as a simplified btrfs_search_slot().
1809 * The key for search can be extracted from @dst_path->nodes[dst_level]
1810 * (first key).
1811 *
1812 * 2) Mark the final tree blocks in @src_path and @dst_path qgroup dirty
1813 * NOTE: In above case, OO(a) and NN(a) won't be marked qgroup dirty.
1814 * They should be marked during previous (@dst_level = 1) iteration.
1815 *
1816 * 3) Mark file extents in leaves dirty
1817 * We don't have good way to pick out new file extents only.
1818 * So we still follow the old method by scanning all file extents in
1819 * the leave.
1820 *
1821 * This function can free us from keeping two paths, thus later we only need
1822 * to care about how to iterate all new tree blocks in reloc tree.
1823 */
1824static int qgroup_trace_extent_swap(struct btrfs_trans_handle* trans,
1825 struct extent_buffer *src_eb,
1826 struct btrfs_path *dst_path,
1827 int dst_level, int root_level,
1828 bool trace_leaf)
1829{
1830 struct btrfs_key key;
1831 struct btrfs_path *src_path;
1832 struct btrfs_fs_info *fs_info = trans->fs_info;
1833 u32 nodesize = fs_info->nodesize;
1834 int cur_level = root_level;
1835 int ret;
1836
1837 BUG_ON(dst_level > root_level);
1838 /* Level mismatch */
1839 if (btrfs_header_level(src_eb) != root_level)
1840 return -EINVAL;
1841
1842 src_path = btrfs_alloc_path();
1843 if (!src_path) {
1844 ret = -ENOMEM;
1845 goto out;
1846 }
1847
1848 if (dst_level)
1849 btrfs_node_key_to_cpu(dst_path->nodes[dst_level], &key, 0);
1850 else
1851 btrfs_item_key_to_cpu(dst_path->nodes[dst_level], &key, 0);
1852
1853 /* For src_path */
1854 extent_buffer_get(src_eb);
1855 src_path->nodes[root_level] = src_eb;
1856 src_path->slots[root_level] = dst_path->slots[root_level];
1857 src_path->locks[root_level] = 0;
1858
1859 /* A simplified version of btrfs_search_slot() */
1860 while (cur_level >= dst_level) {
1861 struct btrfs_key src_key;
1862 struct btrfs_key dst_key;
1863
1864 if (src_path->nodes[cur_level] == NULL) {
1865 struct btrfs_key first_key;
1866 struct extent_buffer *eb;
1867 int parent_slot;
1868 u64 child_gen;
1869 u64 child_bytenr;
1870
1871 eb = src_path->nodes[cur_level + 1];
1872 parent_slot = src_path->slots[cur_level + 1];
1873 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
1874 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
1875 btrfs_node_key_to_cpu(eb, &first_key, parent_slot);
1876
1877 eb = read_tree_block(fs_info, child_bytenr, child_gen,
1878 cur_level, &first_key);
1879 if (IS_ERR(eb)) {
1880 ret = PTR_ERR(eb);
1881 goto out;
1882 } else if (!extent_buffer_uptodate(eb)) {
1883 free_extent_buffer(eb);
1884 ret = -EIO;
1885 goto out;
1886 }
1887
1888 src_path->nodes[cur_level] = eb;
1889
1890 btrfs_tree_read_lock(eb);
1891 btrfs_set_lock_blocking_read(eb);
1892 src_path->locks[cur_level] = BTRFS_READ_LOCK_BLOCKING;
1893 }
1894
1895 src_path->slots[cur_level] = dst_path->slots[cur_level];
1896 if (cur_level) {
1897 btrfs_node_key_to_cpu(dst_path->nodes[cur_level],
1898 &dst_key, dst_path->slots[cur_level]);
1899 btrfs_node_key_to_cpu(src_path->nodes[cur_level],
1900 &src_key, src_path->slots[cur_level]);
1901 } else {
1902 btrfs_item_key_to_cpu(dst_path->nodes[cur_level],
1903 &dst_key, dst_path->slots[cur_level]);
1904 btrfs_item_key_to_cpu(src_path->nodes[cur_level],
1905 &src_key, src_path->slots[cur_level]);
1906 }
1907 /* Content mismatch, something went wrong */
1908 if (btrfs_comp_cpu_keys(&dst_key, &src_key)) {
1909 ret = -ENOENT;
1910 goto out;
1911 }
1912 cur_level--;
1913 }
1914
1915 /*
1916 * Now both @dst_path and @src_path have been populated, record the tree
1917 * blocks for qgroup accounting.
1918 */
1919 ret = btrfs_qgroup_trace_extent(trans, src_path->nodes[dst_level]->start,
1920 nodesize, GFP_NOFS);
1921 if (ret < 0)
1922 goto out;
1923 ret = btrfs_qgroup_trace_extent(trans,
1924 dst_path->nodes[dst_level]->start,
1925 nodesize, GFP_NOFS);
1926 if (ret < 0)
1927 goto out;
1928
1929 /* Record leaf file extents */
1930 if (dst_level == 0 && trace_leaf) {
1931 ret = btrfs_qgroup_trace_leaf_items(trans, src_path->nodes[0]);
1932 if (ret < 0)
1933 goto out;
1934 ret = btrfs_qgroup_trace_leaf_items(trans, dst_path->nodes[0]);
1935 }
1936out:
1937 btrfs_free_path(src_path);
1938 return ret;
1939}
1940
1941/*
1942 * Helper function to do recursive generation-aware depth-first search, to
1943 * locate all new tree blocks in a subtree of reloc tree.
1944 *
1945 * E.g. (OO = Old tree blocks, NN = New tree blocks, whose gen == last_snapshot)
1946 * reloc tree
1947 * L2 NN (a)
1948 * / \
1949 * L1 OO NN (b)
1950 * / \ / \
1951 * L0 OO OO OO NN
1952 * (c) (d)
1953 * If we pass:
1954 * @dst_path = [ nodes[1] = NN(b), nodes[0] = NULL ],
1955 * @cur_level = 1
1956 * @root_level = 1
1957 *
1958 * We will iterate through tree blocks NN(b), NN(d) and info qgroup to trace
1959 * above tree blocks along with their counter parts in file tree.
1960 * While during search, old tree blocks OO(c) will be skipped as tree block swap
1961 * won't affect OO(c).
1962 */
1963static int qgroup_trace_new_subtree_blocks(struct btrfs_trans_handle* trans,
1964 struct extent_buffer *src_eb,
1965 struct btrfs_path *dst_path,
1966 int cur_level, int root_level,
1967 u64 last_snapshot, bool trace_leaf)
1968{
1969 struct btrfs_fs_info *fs_info = trans->fs_info;
1970 struct extent_buffer *eb;
1971 bool need_cleanup = false;
1972 int ret = 0;
1973 int i;
1974
1975 /* Level sanity check */
1976 if (cur_level < 0 || cur_level >= BTRFS_MAX_LEVEL - 1 ||
1977 root_level < 0 || root_level >= BTRFS_MAX_LEVEL - 1 ||
1978 root_level < cur_level) {
1979 btrfs_err_rl(fs_info,
1980 "%s: bad levels, cur_level=%d root_level=%d",
1981 __func__, cur_level, root_level);
1982 return -EUCLEAN;
1983 }
1984
1985 /* Read the tree block if needed */
1986 if (dst_path->nodes[cur_level] == NULL) {
1987 struct btrfs_key first_key;
1988 int parent_slot;
1989 u64 child_gen;
1990 u64 child_bytenr;
1991
1992 /*
1993 * dst_path->nodes[root_level] must be initialized before
1994 * calling this function.
1995 */
1996 if (cur_level == root_level) {
1997 btrfs_err_rl(fs_info,
1998 "%s: dst_path->nodes[%d] not initialized, root_level=%d cur_level=%d",
1999 __func__, root_level, root_level, cur_level);
2000 return -EUCLEAN;
2001 }
2002
2003 /*
2004 * We need to get child blockptr/gen from parent before we can
2005 * read it.
2006 */
2007 eb = dst_path->nodes[cur_level + 1];
2008 parent_slot = dst_path->slots[cur_level + 1];
2009 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
2010 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
2011 btrfs_node_key_to_cpu(eb, &first_key, parent_slot);
2012
2013 /* This node is old, no need to trace */
2014 if (child_gen < last_snapshot)
2015 goto out;
2016
2017 eb = read_tree_block(fs_info, child_bytenr, child_gen,
2018 cur_level, &first_key);
2019 if (IS_ERR(eb)) {
2020 ret = PTR_ERR(eb);
2021 goto out;
2022 } else if (!extent_buffer_uptodate(eb)) {
2023 free_extent_buffer(eb);
2024 ret = -EIO;
2025 goto out;
2026 }
2027
2028 dst_path->nodes[cur_level] = eb;
2029 dst_path->slots[cur_level] = 0;
2030
2031 btrfs_tree_read_lock(eb);
2032 btrfs_set_lock_blocking_read(eb);
2033 dst_path->locks[cur_level] = BTRFS_READ_LOCK_BLOCKING;
2034 need_cleanup = true;
2035 }
2036
2037 /* Now record this tree block and its counter part for qgroups */
2038 ret = qgroup_trace_extent_swap(trans, src_eb, dst_path, cur_level,
2039 root_level, trace_leaf);
2040 if (ret < 0)
2041 goto cleanup;
2042
2043 eb = dst_path->nodes[cur_level];
2044
2045 if (cur_level > 0) {
2046 /* Iterate all child tree blocks */
2047 for (i = 0; i < btrfs_header_nritems(eb); i++) {
2048 /* Skip old tree blocks as they won't be swapped */
2049 if (btrfs_node_ptr_generation(eb, i) < last_snapshot)
2050 continue;
2051 dst_path->slots[cur_level] = i;
2052
2053 /* Recursive call (at most 7 times) */
2054 ret = qgroup_trace_new_subtree_blocks(trans, src_eb,
2055 dst_path, cur_level - 1, root_level,
2056 last_snapshot, trace_leaf);
2057 if (ret < 0)
2058 goto cleanup;
2059 }
2060 }
2061
2062cleanup:
2063 if (need_cleanup) {
2064 /* Clean up */
2065 btrfs_tree_unlock_rw(dst_path->nodes[cur_level],
2066 dst_path->locks[cur_level]);
2067 free_extent_buffer(dst_path->nodes[cur_level]);
2068 dst_path->nodes[cur_level] = NULL;
2069 dst_path->slots[cur_level] = 0;
2070 dst_path->locks[cur_level] = 0;
2071 }
2072out:
2073 return ret;
2074}
2075
2076static int qgroup_trace_subtree_swap(struct btrfs_trans_handle *trans,
2077 struct extent_buffer *src_eb,
2078 struct extent_buffer *dst_eb,
2079 u64 last_snapshot, bool trace_leaf)
2080{
2081 struct btrfs_fs_info *fs_info = trans->fs_info;
2082 struct btrfs_path *dst_path = NULL;
2083 int level;
2084 int ret;
2085
2086 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2087 return 0;
2088
2089 /* Wrong parameter order */
2090 if (btrfs_header_generation(src_eb) > btrfs_header_generation(dst_eb)) {
2091 btrfs_err_rl(fs_info,
2092 "%s: bad parameter order, src_gen=%llu dst_gen=%llu", __func__,
2093 btrfs_header_generation(src_eb),
2094 btrfs_header_generation(dst_eb));
2095 return -EUCLEAN;
2096 }
2097
2098 if (!extent_buffer_uptodate(src_eb) || !extent_buffer_uptodate(dst_eb)) {
2099 ret = -EIO;
2100 goto out;
2101 }
2102
2103 level = btrfs_header_level(dst_eb);
2104 dst_path = btrfs_alloc_path();
2105 if (!dst_path) {
2106 ret = -ENOMEM;
2107 goto out;
2108 }
2109 /* For dst_path */
2110 extent_buffer_get(dst_eb);
2111 dst_path->nodes[level] = dst_eb;
2112 dst_path->slots[level] = 0;
2113 dst_path->locks[level] = 0;
2114
2115 /* Do the generation aware breadth-first search */
2116 ret = qgroup_trace_new_subtree_blocks(trans, src_eb, dst_path, level,
2117 level, last_snapshot, trace_leaf);
2118 if (ret < 0)
2119 goto out;
2120 ret = 0;
2121
2122out:
2123 btrfs_free_path(dst_path);
2124 if (ret < 0)
2125 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2126 return ret;
2127}
2128
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002129int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
2130 struct extent_buffer *root_eb,
2131 u64 root_gen, int root_level)
2132{
2133 struct btrfs_fs_info *fs_info = trans->fs_info;
2134 int ret = 0;
2135 int level;
2136 struct extent_buffer *eb = root_eb;
2137 struct btrfs_path *path = NULL;
2138
2139 BUG_ON(root_level < 0 || root_level >= BTRFS_MAX_LEVEL);
2140 BUG_ON(root_eb == NULL);
2141
2142 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2143 return 0;
2144
2145 if (!extent_buffer_uptodate(root_eb)) {
2146 ret = btrfs_read_buffer(root_eb, root_gen, root_level, NULL);
2147 if (ret)
2148 goto out;
2149 }
2150
2151 if (root_level == 0) {
2152 ret = btrfs_qgroup_trace_leaf_items(trans, root_eb);
2153 goto out;
2154 }
2155
2156 path = btrfs_alloc_path();
2157 if (!path)
2158 return -ENOMEM;
2159
2160 /*
2161 * Walk down the tree. Missing extent blocks are filled in as
2162 * we go. Metadata is accounted every time we read a new
2163 * extent block.
2164 *
2165 * When we reach a leaf, we account for file extent items in it,
2166 * walk back up the tree (adjusting slot pointers as we go)
2167 * and restart the search process.
2168 */
2169 extent_buffer_get(root_eb); /* For path */
2170 path->nodes[root_level] = root_eb;
2171 path->slots[root_level] = 0;
2172 path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
2173walk_down:
2174 level = root_level;
2175 while (level >= 0) {
2176 if (path->nodes[level] == NULL) {
2177 struct btrfs_key first_key;
2178 int parent_slot;
2179 u64 child_gen;
2180 u64 child_bytenr;
2181
2182 /*
2183 * We need to get child blockptr/gen from parent before
2184 * we can read it.
2185 */
2186 eb = path->nodes[level + 1];
2187 parent_slot = path->slots[level + 1];
2188 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
2189 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
2190 btrfs_node_key_to_cpu(eb, &first_key, parent_slot);
2191
2192 eb = read_tree_block(fs_info, child_bytenr, child_gen,
2193 level, &first_key);
2194 if (IS_ERR(eb)) {
2195 ret = PTR_ERR(eb);
2196 goto out;
2197 } else if (!extent_buffer_uptodate(eb)) {
2198 free_extent_buffer(eb);
2199 ret = -EIO;
2200 goto out;
2201 }
2202
2203 path->nodes[level] = eb;
2204 path->slots[level] = 0;
2205
2206 btrfs_tree_read_lock(eb);
David Brazdil0f672f62019-12-10 10:32:29 +00002207 btrfs_set_lock_blocking_read(eb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002208 path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
2209
2210 ret = btrfs_qgroup_trace_extent(trans, child_bytenr,
2211 fs_info->nodesize,
2212 GFP_NOFS);
2213 if (ret)
2214 goto out;
2215 }
2216
2217 if (level == 0) {
2218 ret = btrfs_qgroup_trace_leaf_items(trans,
2219 path->nodes[level]);
2220 if (ret)
2221 goto out;
2222
2223 /* Nonzero return here means we completed our search */
2224 ret = adjust_slots_upwards(path, root_level);
2225 if (ret)
2226 break;
2227
2228 /* Restart search with new slots */
2229 goto walk_down;
2230 }
2231
2232 level--;
2233 }
2234
2235 ret = 0;
2236out:
2237 btrfs_free_path(path);
2238
2239 return ret;
2240}
2241
2242#define UPDATE_NEW 0
2243#define UPDATE_OLD 1
2244/*
2245 * Walk all of the roots that points to the bytenr and adjust their refcnts.
2246 */
2247static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
2248 struct ulist *roots, struct ulist *tmp,
2249 struct ulist *qgroups, u64 seq, int update_old)
2250{
2251 struct ulist_node *unode;
2252 struct ulist_iterator uiter;
2253 struct ulist_node *tmp_unode;
2254 struct ulist_iterator tmp_uiter;
2255 struct btrfs_qgroup *qg;
2256 int ret = 0;
2257
2258 if (!roots)
2259 return 0;
2260 ULIST_ITER_INIT(&uiter);
2261 while ((unode = ulist_next(roots, &uiter))) {
2262 qg = find_qgroup_rb(fs_info, unode->val);
2263 if (!qg)
2264 continue;
2265
2266 ulist_reinit(tmp);
2267 ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg),
2268 GFP_ATOMIC);
2269 if (ret < 0)
2270 return ret;
2271 ret = ulist_add(tmp, qg->qgroupid, qgroup_to_aux(qg), GFP_ATOMIC);
2272 if (ret < 0)
2273 return ret;
2274 ULIST_ITER_INIT(&tmp_uiter);
2275 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
2276 struct btrfs_qgroup_list *glist;
2277
2278 qg = unode_aux_to_qgroup(tmp_unode);
2279 if (update_old)
2280 btrfs_qgroup_update_old_refcnt(qg, seq, 1);
2281 else
2282 btrfs_qgroup_update_new_refcnt(qg, seq, 1);
2283 list_for_each_entry(glist, &qg->groups, next_group) {
2284 ret = ulist_add(qgroups, glist->group->qgroupid,
2285 qgroup_to_aux(glist->group),
2286 GFP_ATOMIC);
2287 if (ret < 0)
2288 return ret;
2289 ret = ulist_add(tmp, glist->group->qgroupid,
2290 qgroup_to_aux(glist->group),
2291 GFP_ATOMIC);
2292 if (ret < 0)
2293 return ret;
2294 }
2295 }
2296 }
2297 return 0;
2298}
2299
2300/*
2301 * Update qgroup rfer/excl counters.
2302 * Rfer update is easy, codes can explain themselves.
2303 *
Olivier Deprez0e641232021-09-23 10:07:05 +02002304 * Excl update is tricky, the update is split into 2 parts.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002305 * Part 1: Possible exclusive <-> sharing detect:
2306 * | A | !A |
2307 * -------------------------------------
2308 * B | * | - |
2309 * -------------------------------------
2310 * !B | + | ** |
2311 * -------------------------------------
2312 *
2313 * Conditions:
2314 * A: cur_old_roots < nr_old_roots (not exclusive before)
2315 * !A: cur_old_roots == nr_old_roots (possible exclusive before)
2316 * B: cur_new_roots < nr_new_roots (not exclusive now)
2317 * !B: cur_new_roots == nr_new_roots (possible exclusive now)
2318 *
2319 * Results:
2320 * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
2321 * *: Definitely not changed. **: Possible unchanged.
2322 *
2323 * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
2324 *
2325 * To make the logic clear, we first use condition A and B to split
2326 * combination into 4 results.
2327 *
2328 * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
2329 * only on variant maybe 0.
2330 *
2331 * Lastly, check result **, since there are 2 variants maybe 0, split them
2332 * again(2x2).
2333 * But this time we don't need to consider other things, the codes and logic
2334 * is easy to understand now.
2335 */
2336static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
2337 struct ulist *qgroups,
2338 u64 nr_old_roots,
2339 u64 nr_new_roots,
2340 u64 num_bytes, u64 seq)
2341{
2342 struct ulist_node *unode;
2343 struct ulist_iterator uiter;
2344 struct btrfs_qgroup *qg;
2345 u64 cur_new_count, cur_old_count;
2346
2347 ULIST_ITER_INIT(&uiter);
2348 while ((unode = ulist_next(qgroups, &uiter))) {
2349 bool dirty = false;
2350
2351 qg = unode_aux_to_qgroup(unode);
2352 cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
2353 cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
2354
2355 trace_qgroup_update_counters(fs_info, qg, cur_old_count,
2356 cur_new_count);
2357
2358 /* Rfer update part */
2359 if (cur_old_count == 0 && cur_new_count > 0) {
2360 qg->rfer += num_bytes;
2361 qg->rfer_cmpr += num_bytes;
2362 dirty = true;
2363 }
2364 if (cur_old_count > 0 && cur_new_count == 0) {
2365 qg->rfer -= num_bytes;
2366 qg->rfer_cmpr -= num_bytes;
2367 dirty = true;
2368 }
2369
2370 /* Excl update part */
2371 /* Exclusive/none -> shared case */
2372 if (cur_old_count == nr_old_roots &&
2373 cur_new_count < nr_new_roots) {
2374 /* Exclusive -> shared */
2375 if (cur_old_count != 0) {
2376 qg->excl -= num_bytes;
2377 qg->excl_cmpr -= num_bytes;
2378 dirty = true;
2379 }
2380 }
2381
2382 /* Shared -> exclusive/none case */
2383 if (cur_old_count < nr_old_roots &&
2384 cur_new_count == nr_new_roots) {
2385 /* Shared->exclusive */
2386 if (cur_new_count != 0) {
2387 qg->excl += num_bytes;
2388 qg->excl_cmpr += num_bytes;
2389 dirty = true;
2390 }
2391 }
2392
2393 /* Exclusive/none -> exclusive/none case */
2394 if (cur_old_count == nr_old_roots &&
2395 cur_new_count == nr_new_roots) {
2396 if (cur_old_count == 0) {
2397 /* None -> exclusive/none */
2398
2399 if (cur_new_count != 0) {
2400 /* None -> exclusive */
2401 qg->excl += num_bytes;
2402 qg->excl_cmpr += num_bytes;
2403 dirty = true;
2404 }
2405 /* None -> none, nothing changed */
2406 } else {
2407 /* Exclusive -> exclusive/none */
2408
2409 if (cur_new_count == 0) {
2410 /* Exclusive -> none */
2411 qg->excl -= num_bytes;
2412 qg->excl_cmpr -= num_bytes;
2413 dirty = true;
2414 }
2415 /* Exclusive -> exclusive, nothing changed */
2416 }
2417 }
2418
2419 if (dirty)
2420 qgroup_dirty(fs_info, qg);
2421 }
2422 return 0;
2423}
2424
2425/*
2426 * Check if the @roots potentially is a list of fs tree roots
2427 *
2428 * Return 0 for definitely not a fs/subvol tree roots ulist
2429 * Return 1 for possible fs/subvol tree roots in the list (considering an empty
2430 * one as well)
2431 */
2432static int maybe_fs_roots(struct ulist *roots)
2433{
2434 struct ulist_node *unode;
2435 struct ulist_iterator uiter;
2436
2437 /* Empty one, still possible for fs roots */
2438 if (!roots || roots->nnodes == 0)
2439 return 1;
2440
2441 ULIST_ITER_INIT(&uiter);
2442 unode = ulist_next(roots, &uiter);
2443 if (!unode)
2444 return 1;
2445
2446 /*
2447 * If it contains fs tree roots, then it must belong to fs/subvol
2448 * trees.
2449 * If it contains a non-fs tree, it won't be shared with fs/subvol trees.
2450 */
2451 return is_fstree(unode->val);
2452}
2453
2454int btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans, u64 bytenr,
2455 u64 num_bytes, struct ulist *old_roots,
2456 struct ulist *new_roots)
2457{
2458 struct btrfs_fs_info *fs_info = trans->fs_info;
2459 struct ulist *qgroups = NULL;
2460 struct ulist *tmp = NULL;
2461 u64 seq;
2462 u64 nr_new_roots = 0;
2463 u64 nr_old_roots = 0;
2464 int ret = 0;
2465
Olivier Deprez0e641232021-09-23 10:07:05 +02002466 /*
2467 * If quotas get disabled meanwhile, the resouces need to be freed and
2468 * we can't just exit here.
2469 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002470 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Olivier Deprez0e641232021-09-23 10:07:05 +02002471 goto out_free;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002472
2473 if (new_roots) {
2474 if (!maybe_fs_roots(new_roots))
2475 goto out_free;
2476 nr_new_roots = new_roots->nnodes;
2477 }
2478 if (old_roots) {
2479 if (!maybe_fs_roots(old_roots))
2480 goto out_free;
2481 nr_old_roots = old_roots->nnodes;
2482 }
2483
2484 /* Quick exit, either not fs tree roots, or won't affect any qgroup */
2485 if (nr_old_roots == 0 && nr_new_roots == 0)
2486 goto out_free;
2487
2488 BUG_ON(!fs_info->quota_root);
2489
2490 trace_btrfs_qgroup_account_extent(fs_info, trans->transid, bytenr,
2491 num_bytes, nr_old_roots, nr_new_roots);
2492
2493 qgroups = ulist_alloc(GFP_NOFS);
2494 if (!qgroups) {
2495 ret = -ENOMEM;
2496 goto out_free;
2497 }
2498 tmp = ulist_alloc(GFP_NOFS);
2499 if (!tmp) {
2500 ret = -ENOMEM;
2501 goto out_free;
2502 }
2503
2504 mutex_lock(&fs_info->qgroup_rescan_lock);
2505 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
2506 if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
2507 mutex_unlock(&fs_info->qgroup_rescan_lock);
2508 ret = 0;
2509 goto out_free;
2510 }
2511 }
2512 mutex_unlock(&fs_info->qgroup_rescan_lock);
2513
2514 spin_lock(&fs_info->qgroup_lock);
2515 seq = fs_info->qgroup_seq;
2516
2517 /* Update old refcnts using old_roots */
2518 ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
2519 UPDATE_OLD);
2520 if (ret < 0)
2521 goto out;
2522
2523 /* Update new refcnts using new_roots */
2524 ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
2525 UPDATE_NEW);
2526 if (ret < 0)
2527 goto out;
2528
2529 qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
2530 num_bytes, seq);
2531
2532 /*
2533 * Bump qgroup_seq to avoid seq overlap
2534 */
2535 fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
2536out:
2537 spin_unlock(&fs_info->qgroup_lock);
2538out_free:
2539 ulist_free(tmp);
2540 ulist_free(qgroups);
2541 ulist_free(old_roots);
2542 ulist_free(new_roots);
2543 return ret;
2544}
2545
2546int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans)
2547{
2548 struct btrfs_fs_info *fs_info = trans->fs_info;
2549 struct btrfs_qgroup_extent_record *record;
2550 struct btrfs_delayed_ref_root *delayed_refs;
2551 struct ulist *new_roots = NULL;
2552 struct rb_node *node;
David Brazdil0f672f62019-12-10 10:32:29 +00002553 u64 num_dirty_extents = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002554 u64 qgroup_to_skip;
2555 int ret = 0;
2556
2557 delayed_refs = &trans->transaction->delayed_refs;
2558 qgroup_to_skip = delayed_refs->qgroup_to_skip;
2559 while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
2560 record = rb_entry(node, struct btrfs_qgroup_extent_record,
2561 node);
2562
David Brazdil0f672f62019-12-10 10:32:29 +00002563 num_dirty_extents++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002564 trace_btrfs_qgroup_account_extents(fs_info, record);
2565
2566 if (!ret) {
2567 /*
2568 * Old roots should be searched when inserting qgroup
2569 * extent record
2570 */
2571 if (WARN_ON(!record->old_roots)) {
2572 /* Search commit root to find old_roots */
2573 ret = btrfs_find_all_roots(NULL, fs_info,
2574 record->bytenr, 0,
2575 &record->old_roots, false);
2576 if (ret < 0)
2577 goto cleanup;
2578 }
2579
David Brazdil0f672f62019-12-10 10:32:29 +00002580 /* Free the reserved data space */
2581 btrfs_qgroup_free_refroot(fs_info,
2582 record->data_rsv_refroot,
2583 record->data_rsv,
2584 BTRFS_QGROUP_RSV_DATA);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002585 /*
2586 * Use SEQ_LAST as time_seq to do special search, which
2587 * doesn't lock tree or delayed_refs and search current
2588 * root. It's safe inside commit_transaction().
2589 */
2590 ret = btrfs_find_all_roots(trans, fs_info,
2591 record->bytenr, SEQ_LAST, &new_roots, false);
2592 if (ret < 0)
2593 goto cleanup;
2594 if (qgroup_to_skip) {
2595 ulist_del(new_roots, qgroup_to_skip, 0);
2596 ulist_del(record->old_roots, qgroup_to_skip,
2597 0);
2598 }
2599 ret = btrfs_qgroup_account_extent(trans, record->bytenr,
2600 record->num_bytes,
2601 record->old_roots,
2602 new_roots);
2603 record->old_roots = NULL;
2604 new_roots = NULL;
2605 }
2606cleanup:
2607 ulist_free(record->old_roots);
2608 ulist_free(new_roots);
2609 new_roots = NULL;
2610 rb_erase(node, &delayed_refs->dirty_extent_root);
2611 kfree(record);
2612
2613 }
David Brazdil0f672f62019-12-10 10:32:29 +00002614 trace_qgroup_num_dirty_extents(fs_info, trans->transid,
2615 num_dirty_extents);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002616 return ret;
2617}
2618
2619/*
2620 * called from commit_transaction. Writes all changed qgroups to disk.
2621 */
2622int btrfs_run_qgroups(struct btrfs_trans_handle *trans)
2623{
2624 struct btrfs_fs_info *fs_info = trans->fs_info;
2625 struct btrfs_root *quota_root = fs_info->quota_root;
2626 int ret = 0;
2627
2628 if (!quota_root)
2629 return ret;
2630
2631 spin_lock(&fs_info->qgroup_lock);
2632 while (!list_empty(&fs_info->dirty_qgroups)) {
2633 struct btrfs_qgroup *qgroup;
2634 qgroup = list_first_entry(&fs_info->dirty_qgroups,
2635 struct btrfs_qgroup, dirty);
2636 list_del_init(&qgroup->dirty);
2637 spin_unlock(&fs_info->qgroup_lock);
2638 ret = update_qgroup_info_item(trans, qgroup);
2639 if (ret)
2640 fs_info->qgroup_flags |=
2641 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2642 ret = update_qgroup_limit_item(trans, qgroup);
2643 if (ret)
2644 fs_info->qgroup_flags |=
2645 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2646 spin_lock(&fs_info->qgroup_lock);
2647 }
2648 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2649 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
2650 else
2651 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
2652 spin_unlock(&fs_info->qgroup_lock);
2653
2654 ret = update_qgroup_status_item(trans);
2655 if (ret)
2656 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2657
2658 return ret;
2659}
2660
2661/*
2662 * Copy the accounting information between qgroups. This is necessary
2663 * when a snapshot or a subvolume is created. Throwing an error will
2664 * cause a transaction abort so we take extra care here to only error
2665 * when a readonly fs is a reasonable outcome.
2666 */
2667int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
2668 u64 objectid, struct btrfs_qgroup_inherit *inherit)
2669{
2670 int ret = 0;
2671 int i;
2672 u64 *i_qgroups;
David Brazdil0f672f62019-12-10 10:32:29 +00002673 bool committing = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002674 struct btrfs_fs_info *fs_info = trans->fs_info;
2675 struct btrfs_root *quota_root;
2676 struct btrfs_qgroup *srcgroup;
2677 struct btrfs_qgroup *dstgroup;
Olivier Deprez0e641232021-09-23 10:07:05 +02002678 bool need_rescan = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002679 u32 level_size = 0;
2680 u64 nums;
2681
David Brazdil0f672f62019-12-10 10:32:29 +00002682 /*
2683 * There are only two callers of this function.
2684 *
2685 * One in create_subvol() in the ioctl context, which needs to hold
2686 * the qgroup_ioctl_lock.
2687 *
2688 * The other one in create_pending_snapshot() where no other qgroup
2689 * code can modify the fs as they all need to either start a new trans
2690 * or hold a trans handler, thus we don't need to hold
2691 * qgroup_ioctl_lock.
2692 * This would avoid long and complex lock chain and make lockdep happy.
2693 */
2694 spin_lock(&fs_info->trans_lock);
2695 if (trans->transaction->state == TRANS_STATE_COMMIT_DOING)
2696 committing = true;
2697 spin_unlock(&fs_info->trans_lock);
2698
2699 if (!committing)
2700 mutex_lock(&fs_info->qgroup_ioctl_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002701 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2702 goto out;
2703
2704 quota_root = fs_info->quota_root;
2705 if (!quota_root) {
2706 ret = -EINVAL;
2707 goto out;
2708 }
2709
2710 if (inherit) {
2711 i_qgroups = (u64 *)(inherit + 1);
2712 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
2713 2 * inherit->num_excl_copies;
2714 for (i = 0; i < nums; ++i) {
2715 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
2716
2717 /*
2718 * Zero out invalid groups so we can ignore
2719 * them later.
2720 */
2721 if (!srcgroup ||
2722 ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
2723 *i_qgroups = 0ULL;
2724
2725 ++i_qgroups;
2726 }
2727 }
2728
2729 /*
2730 * create a tracking group for the subvol itself
2731 */
2732 ret = add_qgroup_item(trans, quota_root, objectid);
2733 if (ret)
2734 goto out;
2735
2736 /*
2737 * add qgroup to all inherited groups
2738 */
2739 if (inherit) {
2740 i_qgroups = (u64 *)(inherit + 1);
2741 for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) {
2742 if (*i_qgroups == 0)
2743 continue;
2744 ret = add_qgroup_relation_item(trans, objectid,
2745 *i_qgroups);
2746 if (ret && ret != -EEXIST)
2747 goto out;
2748 ret = add_qgroup_relation_item(trans, *i_qgroups,
2749 objectid);
2750 if (ret && ret != -EEXIST)
2751 goto out;
2752 }
2753 ret = 0;
2754 }
2755
2756
2757 spin_lock(&fs_info->qgroup_lock);
2758
2759 dstgroup = add_qgroup_rb(fs_info, objectid);
2760 if (IS_ERR(dstgroup)) {
2761 ret = PTR_ERR(dstgroup);
2762 goto unlock;
2763 }
2764
2765 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
2766 dstgroup->lim_flags = inherit->lim.flags;
2767 dstgroup->max_rfer = inherit->lim.max_rfer;
2768 dstgroup->max_excl = inherit->lim.max_excl;
2769 dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
2770 dstgroup->rsv_excl = inherit->lim.rsv_excl;
2771
2772 ret = update_qgroup_limit_item(trans, dstgroup);
2773 if (ret) {
2774 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2775 btrfs_info(fs_info,
2776 "unable to update quota limit for %llu",
2777 dstgroup->qgroupid);
2778 goto unlock;
2779 }
2780 }
2781
2782 if (srcid) {
2783 srcgroup = find_qgroup_rb(fs_info, srcid);
2784 if (!srcgroup)
2785 goto unlock;
2786
2787 /*
2788 * We call inherit after we clone the root in order to make sure
2789 * our counts don't go crazy, so at this point the only
2790 * difference between the two roots should be the root node.
2791 */
2792 level_size = fs_info->nodesize;
2793 dstgroup->rfer = srcgroup->rfer;
2794 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
2795 dstgroup->excl = level_size;
2796 dstgroup->excl_cmpr = level_size;
2797 srcgroup->excl = level_size;
2798 srcgroup->excl_cmpr = level_size;
2799
2800 /* inherit the limit info */
2801 dstgroup->lim_flags = srcgroup->lim_flags;
2802 dstgroup->max_rfer = srcgroup->max_rfer;
2803 dstgroup->max_excl = srcgroup->max_excl;
2804 dstgroup->rsv_rfer = srcgroup->rsv_rfer;
2805 dstgroup->rsv_excl = srcgroup->rsv_excl;
2806
2807 qgroup_dirty(fs_info, dstgroup);
2808 qgroup_dirty(fs_info, srcgroup);
2809 }
2810
2811 if (!inherit)
2812 goto unlock;
2813
2814 i_qgroups = (u64 *)(inherit + 1);
2815 for (i = 0; i < inherit->num_qgroups; ++i) {
2816 if (*i_qgroups) {
2817 ret = add_relation_rb(fs_info, objectid, *i_qgroups);
2818 if (ret)
2819 goto unlock;
2820 }
2821 ++i_qgroups;
Olivier Deprez0e641232021-09-23 10:07:05 +02002822
2823 /*
2824 * If we're doing a snapshot, and adding the snapshot to a new
2825 * qgroup, the numbers are guaranteed to be incorrect.
2826 */
2827 if (srcid)
2828 need_rescan = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002829 }
2830
2831 for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {
2832 struct btrfs_qgroup *src;
2833 struct btrfs_qgroup *dst;
2834
2835 if (!i_qgroups[0] || !i_qgroups[1])
2836 continue;
2837
2838 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2839 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2840
2841 if (!src || !dst) {
2842 ret = -EINVAL;
2843 goto unlock;
2844 }
2845
2846 dst->rfer = src->rfer - level_size;
2847 dst->rfer_cmpr = src->rfer_cmpr - level_size;
Olivier Deprez0e641232021-09-23 10:07:05 +02002848
2849 /* Manually tweaking numbers certainly needs a rescan */
2850 need_rescan = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002851 }
2852 for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {
2853 struct btrfs_qgroup *src;
2854 struct btrfs_qgroup *dst;
2855
2856 if (!i_qgroups[0] || !i_qgroups[1])
2857 continue;
2858
2859 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2860 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2861
2862 if (!src || !dst) {
2863 ret = -EINVAL;
2864 goto unlock;
2865 }
2866
2867 dst->excl = src->excl + level_size;
2868 dst->excl_cmpr = src->excl_cmpr + level_size;
Olivier Deprez0e641232021-09-23 10:07:05 +02002869 need_rescan = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002870 }
2871
2872unlock:
2873 spin_unlock(&fs_info->qgroup_lock);
2874out:
David Brazdil0f672f62019-12-10 10:32:29 +00002875 if (!committing)
2876 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Olivier Deprez0e641232021-09-23 10:07:05 +02002877 if (need_rescan)
2878 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002879 return ret;
2880}
2881
Olivier Deprez0e641232021-09-23 10:07:05 +02002882static bool qgroup_check_limits(const struct btrfs_qgroup *qg, u64 num_bytes)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002883{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002884 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
2885 qgroup_rsv_total(qg) + (s64)qg->rfer + num_bytes > qg->max_rfer)
2886 return false;
2887
2888 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
2889 qgroup_rsv_total(qg) + (s64)qg->excl + num_bytes > qg->max_excl)
2890 return false;
2891
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002892 return true;
2893}
2894
2895static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce,
2896 enum btrfs_qgroup_rsv_type type)
2897{
2898 struct btrfs_root *quota_root;
2899 struct btrfs_qgroup *qgroup;
2900 struct btrfs_fs_info *fs_info = root->fs_info;
2901 u64 ref_root = root->root_key.objectid;
2902 int ret = 0;
2903 struct ulist_node *unode;
2904 struct ulist_iterator uiter;
2905
2906 if (!is_fstree(ref_root))
2907 return 0;
2908
2909 if (num_bytes == 0)
2910 return 0;
2911
2912 if (test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags) &&
2913 capable(CAP_SYS_RESOURCE))
2914 enforce = false;
2915
2916 spin_lock(&fs_info->qgroup_lock);
2917 quota_root = fs_info->quota_root;
2918 if (!quota_root)
2919 goto out;
2920
2921 qgroup = find_qgroup_rb(fs_info, ref_root);
2922 if (!qgroup)
2923 goto out;
2924
2925 /*
2926 * in a first step, we check all affected qgroups if any limits would
2927 * be exceeded
2928 */
2929 ulist_reinit(fs_info->qgroup_ulist);
2930 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
2931 qgroup_to_aux(qgroup), GFP_ATOMIC);
2932 if (ret < 0)
2933 goto out;
2934 ULIST_ITER_INIT(&uiter);
2935 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
2936 struct btrfs_qgroup *qg;
2937 struct btrfs_qgroup_list *glist;
2938
2939 qg = unode_aux_to_qgroup(unode);
2940
Olivier Deprez0e641232021-09-23 10:07:05 +02002941 if (enforce && !qgroup_check_limits(qg, num_bytes)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002942 ret = -EDQUOT;
2943 goto out;
2944 }
2945
2946 list_for_each_entry(glist, &qg->groups, next_group) {
2947 ret = ulist_add(fs_info->qgroup_ulist,
2948 glist->group->qgroupid,
2949 qgroup_to_aux(glist->group), GFP_ATOMIC);
2950 if (ret < 0)
2951 goto out;
2952 }
2953 }
2954 ret = 0;
2955 /*
2956 * no limits exceeded, now record the reservation into all qgroups
2957 */
2958 ULIST_ITER_INIT(&uiter);
2959 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
2960 struct btrfs_qgroup *qg;
2961
2962 qg = unode_aux_to_qgroup(unode);
2963
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002964 qgroup_rsv_add(fs_info, qg, num_bytes, type);
2965 }
2966
2967out:
2968 spin_unlock(&fs_info->qgroup_lock);
2969 return ret;
2970}
2971
2972/*
2973 * Free @num_bytes of reserved space with @type for qgroup. (Normally level 0
2974 * qgroup).
2975 *
2976 * Will handle all higher level qgroup too.
2977 *
2978 * NOTE: If @num_bytes is (u64)-1, this means to free all bytes of this qgroup.
2979 * This special case is only used for META_PERTRANS type.
2980 */
2981void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
2982 u64 ref_root, u64 num_bytes,
2983 enum btrfs_qgroup_rsv_type type)
2984{
2985 struct btrfs_root *quota_root;
2986 struct btrfs_qgroup *qgroup;
2987 struct ulist_node *unode;
2988 struct ulist_iterator uiter;
2989 int ret = 0;
2990
2991 if (!is_fstree(ref_root))
2992 return;
2993
2994 if (num_bytes == 0)
2995 return;
2996
2997 if (num_bytes == (u64)-1 && type != BTRFS_QGROUP_RSV_META_PERTRANS) {
2998 WARN(1, "%s: Invalid type to free", __func__);
2999 return;
3000 }
3001 spin_lock(&fs_info->qgroup_lock);
3002
3003 quota_root = fs_info->quota_root;
3004 if (!quota_root)
3005 goto out;
3006
3007 qgroup = find_qgroup_rb(fs_info, ref_root);
3008 if (!qgroup)
3009 goto out;
3010
3011 if (num_bytes == (u64)-1)
3012 /*
3013 * We're freeing all pertrans rsv, get reserved value from
3014 * level 0 qgroup as real num_bytes to free.
3015 */
3016 num_bytes = qgroup->rsv.values[type];
3017
3018 ulist_reinit(fs_info->qgroup_ulist);
3019 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
3020 qgroup_to_aux(qgroup), GFP_ATOMIC);
3021 if (ret < 0)
3022 goto out;
3023 ULIST_ITER_INIT(&uiter);
3024 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
3025 struct btrfs_qgroup *qg;
3026 struct btrfs_qgroup_list *glist;
3027
3028 qg = unode_aux_to_qgroup(unode);
3029
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003030 qgroup_rsv_release(fs_info, qg, num_bytes, type);
3031
3032 list_for_each_entry(glist, &qg->groups, next_group) {
3033 ret = ulist_add(fs_info->qgroup_ulist,
3034 glist->group->qgroupid,
3035 qgroup_to_aux(glist->group), GFP_ATOMIC);
3036 if (ret < 0)
3037 goto out;
3038 }
3039 }
3040
3041out:
3042 spin_unlock(&fs_info->qgroup_lock);
3043}
3044
3045/*
3046 * Check if the leaf is the last leaf. Which means all node pointers
3047 * are at their last position.
3048 */
3049static bool is_last_leaf(struct btrfs_path *path)
3050{
3051 int i;
3052
3053 for (i = 1; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
3054 if (path->slots[i] != btrfs_header_nritems(path->nodes[i]) - 1)
3055 return false;
3056 }
3057 return true;
3058}
3059
3060/*
3061 * returns < 0 on error, 0 when more leafs are to be scanned.
3062 * returns 1 when done.
3063 */
3064static int qgroup_rescan_leaf(struct btrfs_trans_handle *trans,
3065 struct btrfs_path *path)
3066{
3067 struct btrfs_fs_info *fs_info = trans->fs_info;
3068 struct btrfs_key found;
3069 struct extent_buffer *scratch_leaf = NULL;
3070 struct ulist *roots = NULL;
3071 u64 num_bytes;
3072 bool done;
3073 int slot;
3074 int ret;
3075
3076 mutex_lock(&fs_info->qgroup_rescan_lock);
3077 ret = btrfs_search_slot_for_read(fs_info->extent_root,
3078 &fs_info->qgroup_rescan_progress,
3079 path, 1, 0);
3080
3081 btrfs_debug(fs_info,
3082 "current progress key (%llu %u %llu), search_slot ret %d",
3083 fs_info->qgroup_rescan_progress.objectid,
3084 fs_info->qgroup_rescan_progress.type,
3085 fs_info->qgroup_rescan_progress.offset, ret);
3086
3087 if (ret) {
3088 /*
3089 * The rescan is about to end, we will not be scanning any
3090 * further blocks. We cannot unset the RESCAN flag here, because
3091 * we want to commit the transaction if everything went well.
3092 * To make the live accounting work in this phase, we set our
3093 * scan progress pointer such that every real extent objectid
3094 * will be smaller.
3095 */
3096 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
3097 btrfs_release_path(path);
3098 mutex_unlock(&fs_info->qgroup_rescan_lock);
3099 return ret;
3100 }
3101 done = is_last_leaf(path);
3102
3103 btrfs_item_key_to_cpu(path->nodes[0], &found,
3104 btrfs_header_nritems(path->nodes[0]) - 1);
3105 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
3106
3107 scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
3108 if (!scratch_leaf) {
3109 ret = -ENOMEM;
3110 mutex_unlock(&fs_info->qgroup_rescan_lock);
3111 goto out;
3112 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003113 slot = path->slots[0];
3114 btrfs_release_path(path);
3115 mutex_unlock(&fs_info->qgroup_rescan_lock);
3116
3117 for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
3118 btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
3119 if (found.type != BTRFS_EXTENT_ITEM_KEY &&
3120 found.type != BTRFS_METADATA_ITEM_KEY)
3121 continue;
3122 if (found.type == BTRFS_METADATA_ITEM_KEY)
3123 num_bytes = fs_info->nodesize;
3124 else
3125 num_bytes = found.offset;
3126
3127 ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
3128 &roots, false);
3129 if (ret < 0)
3130 goto out;
3131 /* For rescan, just pass old_roots as NULL */
3132 ret = btrfs_qgroup_account_extent(trans, found.objectid,
3133 num_bytes, NULL, roots);
3134 if (ret < 0)
3135 goto out;
3136 }
3137out:
David Brazdil0f672f62019-12-10 10:32:29 +00003138 if (scratch_leaf)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003139 free_extent_buffer(scratch_leaf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003140
3141 if (done && !ret) {
3142 ret = 1;
3143 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
3144 }
3145 return ret;
3146}
3147
Olivier Deprez0e641232021-09-23 10:07:05 +02003148static bool rescan_should_stop(struct btrfs_fs_info *fs_info)
3149{
3150 return btrfs_fs_closing(fs_info) ||
3151 test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
3152}
3153
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003154static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
3155{
3156 struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
3157 qgroup_rescan_work);
3158 struct btrfs_path *path;
3159 struct btrfs_trans_handle *trans = NULL;
3160 int err = -ENOMEM;
3161 int ret = 0;
Olivier Deprez0e641232021-09-23 10:07:05 +02003162 bool stopped = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003163
3164 path = btrfs_alloc_path();
3165 if (!path)
3166 goto out;
3167 /*
3168 * Rescan should only search for commit root, and any later difference
3169 * should be recorded by qgroup
3170 */
3171 path->search_commit_root = 1;
3172 path->skip_locking = 1;
3173
3174 err = 0;
Olivier Deprez0e641232021-09-23 10:07:05 +02003175 while (!err && !(stopped = rescan_should_stop(fs_info))) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003176 trans = btrfs_start_transaction(fs_info->fs_root, 0);
3177 if (IS_ERR(trans)) {
3178 err = PTR_ERR(trans);
3179 break;
3180 }
3181 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
3182 err = -EINTR;
3183 } else {
3184 err = qgroup_rescan_leaf(trans, path);
3185 }
3186 if (err > 0)
3187 btrfs_commit_transaction(trans);
3188 else
3189 btrfs_end_transaction(trans);
3190 }
3191
3192out:
3193 btrfs_free_path(path);
3194
3195 mutex_lock(&fs_info->qgroup_rescan_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003196 if (err > 0 &&
3197 fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
3198 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
3199 } else if (err < 0) {
3200 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
3201 }
3202 mutex_unlock(&fs_info->qgroup_rescan_lock);
3203
3204 /*
3205 * only update status, since the previous part has already updated the
3206 * qgroup info.
3207 */
3208 trans = btrfs_start_transaction(fs_info->quota_root, 1);
3209 if (IS_ERR(trans)) {
3210 err = PTR_ERR(trans);
David Brazdil0f672f62019-12-10 10:32:29 +00003211 trans = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003212 btrfs_err(fs_info,
3213 "fail to start transaction for status update: %d",
3214 err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003215 }
David Brazdil0f672f62019-12-10 10:32:29 +00003216
3217 mutex_lock(&fs_info->qgroup_rescan_lock);
Olivier Deprez0e641232021-09-23 10:07:05 +02003218 if (!stopped)
David Brazdil0f672f62019-12-10 10:32:29 +00003219 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3220 if (trans) {
3221 ret = update_qgroup_status_item(trans);
3222 if (ret < 0) {
3223 err = ret;
3224 btrfs_err(fs_info, "fail to update qgroup status: %d",
3225 err);
3226 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003227 }
David Brazdil0f672f62019-12-10 10:32:29 +00003228 fs_info->qgroup_rescan_running = false;
3229 complete_all(&fs_info->qgroup_rescan_completion);
3230 mutex_unlock(&fs_info->qgroup_rescan_lock);
3231
3232 if (!trans)
3233 return;
3234
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003235 btrfs_end_transaction(trans);
3236
Olivier Deprez0e641232021-09-23 10:07:05 +02003237 if (stopped) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003238 btrfs_info(fs_info, "qgroup scan paused");
3239 } else if (err >= 0) {
3240 btrfs_info(fs_info, "qgroup scan completed%s",
3241 err > 0 ? " (inconsistency flag cleared)" : "");
3242 } else {
3243 btrfs_err(fs_info, "qgroup scan failed with %d", err);
3244 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003245}
3246
3247/*
3248 * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
3249 * memory required for the rescan context.
3250 */
3251static int
3252qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
3253 int init_flags)
3254{
3255 int ret = 0;
3256
3257 if (!init_flags) {
3258 /* we're resuming qgroup rescan at mount time */
3259 if (!(fs_info->qgroup_flags &
3260 BTRFS_QGROUP_STATUS_FLAG_RESCAN)) {
3261 btrfs_warn(fs_info,
Olivier Deprez0e641232021-09-23 10:07:05 +02003262 "qgroup rescan init failed, qgroup rescan is not queued");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003263 ret = -EINVAL;
3264 } else if (!(fs_info->qgroup_flags &
3265 BTRFS_QGROUP_STATUS_FLAG_ON)) {
3266 btrfs_warn(fs_info,
Olivier Deprez0e641232021-09-23 10:07:05 +02003267 "qgroup rescan init failed, qgroup is not enabled");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003268 ret = -EINVAL;
3269 }
3270
3271 if (ret)
3272 return ret;
3273 }
3274
3275 mutex_lock(&fs_info->qgroup_rescan_lock);
3276 spin_lock(&fs_info->qgroup_lock);
3277
3278 if (init_flags) {
3279 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
3280 btrfs_warn(fs_info,
3281 "qgroup rescan is already in progress");
3282 ret = -EINPROGRESS;
3283 } else if (!(fs_info->qgroup_flags &
3284 BTRFS_QGROUP_STATUS_FLAG_ON)) {
3285 btrfs_warn(fs_info,
3286 "qgroup rescan init failed, qgroup is not enabled");
3287 ret = -EINVAL;
3288 }
3289
3290 if (ret) {
3291 spin_unlock(&fs_info->qgroup_lock);
3292 mutex_unlock(&fs_info->qgroup_rescan_lock);
3293 return ret;
3294 }
3295 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3296 }
3297
3298 memset(&fs_info->qgroup_rescan_progress, 0,
3299 sizeof(fs_info->qgroup_rescan_progress));
3300 fs_info->qgroup_rescan_progress.objectid = progress_objectid;
3301 init_completion(&fs_info->qgroup_rescan_completion);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003302
3303 spin_unlock(&fs_info->qgroup_lock);
3304 mutex_unlock(&fs_info->qgroup_rescan_lock);
3305
3306 memset(&fs_info->qgroup_rescan_work, 0,
3307 sizeof(fs_info->qgroup_rescan_work));
3308 btrfs_init_work(&fs_info->qgroup_rescan_work,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003309 btrfs_qgroup_rescan_worker, NULL, NULL);
3310 return 0;
3311}
3312
3313static void
3314qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
3315{
3316 struct rb_node *n;
3317 struct btrfs_qgroup *qgroup;
3318
3319 spin_lock(&fs_info->qgroup_lock);
3320 /* clear all current qgroup tracking information */
3321 for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
3322 qgroup = rb_entry(n, struct btrfs_qgroup, node);
3323 qgroup->rfer = 0;
3324 qgroup->rfer_cmpr = 0;
3325 qgroup->excl = 0;
3326 qgroup->excl_cmpr = 0;
3327 qgroup_dirty(fs_info, qgroup);
3328 }
3329 spin_unlock(&fs_info->qgroup_lock);
3330}
3331
3332int
3333btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
3334{
3335 int ret = 0;
3336 struct btrfs_trans_handle *trans;
3337
3338 ret = qgroup_rescan_init(fs_info, 0, 1);
3339 if (ret)
3340 return ret;
3341
3342 /*
3343 * We have set the rescan_progress to 0, which means no more
3344 * delayed refs will be accounted by btrfs_qgroup_account_ref.
3345 * However, btrfs_qgroup_account_ref may be right after its call
3346 * to btrfs_find_all_roots, in which case it would still do the
3347 * accounting.
3348 * To solve this, we're committing the transaction, which will
3349 * ensure we run all delayed refs and only after that, we are
3350 * going to clear all tracking information for a clean start.
3351 */
3352
3353 trans = btrfs_join_transaction(fs_info->fs_root);
3354 if (IS_ERR(trans)) {
3355 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3356 return PTR_ERR(trans);
3357 }
3358 ret = btrfs_commit_transaction(trans);
3359 if (ret) {
3360 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3361 return ret;
3362 }
3363
3364 qgroup_rescan_zero_tracking(fs_info);
3365
Olivier Deprez0e641232021-09-23 10:07:05 +02003366 mutex_lock(&fs_info->qgroup_rescan_lock);
3367 fs_info->qgroup_rescan_running = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003368 btrfs_queue_work(fs_info->qgroup_rescan_workers,
3369 &fs_info->qgroup_rescan_work);
Olivier Deprez0e641232021-09-23 10:07:05 +02003370 mutex_unlock(&fs_info->qgroup_rescan_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003371
3372 return 0;
3373}
3374
3375int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
3376 bool interruptible)
3377{
3378 int running;
3379 int ret = 0;
3380
3381 mutex_lock(&fs_info->qgroup_rescan_lock);
3382 spin_lock(&fs_info->qgroup_lock);
3383 running = fs_info->qgroup_rescan_running;
3384 spin_unlock(&fs_info->qgroup_lock);
3385 mutex_unlock(&fs_info->qgroup_rescan_lock);
3386
3387 if (!running)
3388 return 0;
3389
3390 if (interruptible)
3391 ret = wait_for_completion_interruptible(
3392 &fs_info->qgroup_rescan_completion);
3393 else
3394 wait_for_completion(&fs_info->qgroup_rescan_completion);
3395
3396 return ret;
3397}
3398
3399/*
3400 * this is only called from open_ctree where we're still single threaded, thus
3401 * locking is omitted here.
3402 */
3403void
3404btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
3405{
Olivier Deprez0e641232021-09-23 10:07:05 +02003406 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
3407 mutex_lock(&fs_info->qgroup_rescan_lock);
3408 fs_info->qgroup_rescan_running = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003409 btrfs_queue_work(fs_info->qgroup_rescan_workers,
3410 &fs_info->qgroup_rescan_work);
Olivier Deprez0e641232021-09-23 10:07:05 +02003411 mutex_unlock(&fs_info->qgroup_rescan_lock);
3412 }
3413}
3414
3415#define rbtree_iterate_from_safe(node, next, start) \
3416 for (node = start; node && ({ next = rb_next(node); 1;}); node = next)
3417
3418static int qgroup_unreserve_range(struct btrfs_inode *inode,
3419 struct extent_changeset *reserved, u64 start,
3420 u64 len)
3421{
3422 struct rb_node *node;
3423 struct rb_node *next;
3424 struct ulist_node *entry = NULL;
3425 int ret = 0;
3426
3427 node = reserved->range_changed.root.rb_node;
3428 while (node) {
3429 entry = rb_entry(node, struct ulist_node, rb_node);
3430 if (entry->val < start)
3431 node = node->rb_right;
3432 else if (entry)
3433 node = node->rb_left;
3434 else
3435 break;
3436 }
3437
3438 /* Empty changeset */
3439 if (!entry)
3440 return 0;
3441
3442 if (entry->val > start && rb_prev(&entry->rb_node))
3443 entry = rb_entry(rb_prev(&entry->rb_node), struct ulist_node,
3444 rb_node);
3445
3446 rbtree_iterate_from_safe(node, next, &entry->rb_node) {
3447 u64 entry_start;
3448 u64 entry_end;
3449 u64 entry_len;
3450 int clear_ret;
3451
3452 entry = rb_entry(node, struct ulist_node, rb_node);
3453 entry_start = entry->val;
3454 entry_end = entry->aux;
3455 entry_len = entry_end - entry_start + 1;
3456
3457 if (entry_start >= start + len)
3458 break;
3459 if (entry_start + entry_len <= start)
3460 continue;
3461 /*
3462 * Now the entry is in [start, start + len), revert the
3463 * EXTENT_QGROUP_RESERVED bit.
3464 */
3465 clear_ret = clear_extent_bits(&inode->io_tree, entry_start,
3466 entry_end, EXTENT_QGROUP_RESERVED);
3467 if (!ret && clear_ret < 0)
3468 ret = clear_ret;
3469
3470 ulist_del(&reserved->range_changed, entry->val, entry->aux);
3471 if (likely(reserved->bytes_changed >= entry_len)) {
3472 reserved->bytes_changed -= entry_len;
3473 } else {
3474 WARN_ON(1);
3475 reserved->bytes_changed = 0;
3476 }
3477 }
3478
3479 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003480}
3481
3482/*
Olivier Deprez0e641232021-09-23 10:07:05 +02003483 * Try to free some space for qgroup.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003484 *
Olivier Deprez0e641232021-09-23 10:07:05 +02003485 * For qgroup, there are only 3 ways to free qgroup space:
3486 * - Flush nodatacow write
3487 * Any nodatacow write will free its reserved data space at run_delalloc_range().
3488 * In theory, we should only flush nodatacow inodes, but it's not yet
3489 * possible, so we need to flush the whole root.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003490 *
Olivier Deprez0e641232021-09-23 10:07:05 +02003491 * - Wait for ordered extents
3492 * When ordered extents are finished, their reserved metadata is finally
3493 * converted to per_trans status, which can be freed by later commit
3494 * transaction.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003495 *
Olivier Deprez0e641232021-09-23 10:07:05 +02003496 * - Commit transaction
3497 * This would free the meta_per_trans space.
3498 * In theory this shouldn't provide much space, but any more qgroup space
3499 * is needed.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003500 */
Olivier Deprez0e641232021-09-23 10:07:05 +02003501static int try_flush_qgroup(struct btrfs_root *root)
3502{
3503 struct btrfs_trans_handle *trans;
3504 int ret;
3505 bool can_commit = true;
3506
3507 /*
3508 * We don't want to run flush again and again, so if there is a running
3509 * one, we won't try to start a new flush, but exit directly.
3510 */
3511 if (test_and_set_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state)) {
3512 wait_event(root->qgroup_flush_wait,
3513 !test_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state));
3514 return 0;
3515 }
3516
3517 /*
3518 * If current process holds a transaction, we shouldn't flush, as we
3519 * assume all space reservation happens before a transaction handle is
3520 * held.
3521 *
3522 * But there are cases like btrfs_delayed_item_reserve_metadata() where
3523 * we try to reserve space with one transction handle already held.
3524 * In that case we can't commit transaction, but at least try to end it
3525 * and hope the started data writes can free some space.
3526 */
3527 if (current->journal_info &&
3528 current->journal_info != BTRFS_SEND_TRANS_STUB)
3529 can_commit = false;
3530
3531 ret = btrfs_start_delalloc_snapshot(root);
3532 if (ret < 0)
3533 goto out;
3534 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
3535
3536 trans = btrfs_join_transaction(root);
3537 if (IS_ERR(trans)) {
3538 ret = PTR_ERR(trans);
3539 goto out;
3540 }
3541
3542 if (can_commit)
3543 ret = btrfs_commit_transaction(trans);
3544 else
3545 ret = btrfs_end_transaction(trans);
3546out:
3547 clear_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state);
3548 wake_up(&root->qgroup_flush_wait);
3549 return ret;
3550}
3551
3552static int qgroup_reserve_data(struct btrfs_inode *inode,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003553 struct extent_changeset **reserved_ret, u64 start,
3554 u64 len)
3555{
Olivier Deprez0e641232021-09-23 10:07:05 +02003556 struct btrfs_root *root = inode->root;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003557 struct extent_changeset *reserved;
Olivier Deprez0e641232021-09-23 10:07:05 +02003558 bool new_reserved = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003559 u64 orig_reserved;
3560 u64 to_reserve;
3561 int ret;
3562
3563 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
David Brazdil0f672f62019-12-10 10:32:29 +00003564 !is_fstree(root->root_key.objectid) || len == 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003565 return 0;
3566
3567 /* @reserved parameter is mandatory for qgroup */
3568 if (WARN_ON(!reserved_ret))
3569 return -EINVAL;
3570 if (!*reserved_ret) {
Olivier Deprez0e641232021-09-23 10:07:05 +02003571 new_reserved = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003572 *reserved_ret = extent_changeset_alloc();
3573 if (!*reserved_ret)
3574 return -ENOMEM;
3575 }
3576 reserved = *reserved_ret;
3577 /* Record already reserved space */
3578 orig_reserved = reserved->bytes_changed;
Olivier Deprez0e641232021-09-23 10:07:05 +02003579 ret = set_record_extent_bits(&inode->io_tree, start,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003580 start + len -1, EXTENT_QGROUP_RESERVED, reserved);
3581
3582 /* Newly reserved space */
3583 to_reserve = reserved->bytes_changed - orig_reserved;
Olivier Deprez0e641232021-09-23 10:07:05 +02003584 trace_btrfs_qgroup_reserve_data(&inode->vfs_inode, start, len,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003585 to_reserve, QGROUP_RESERVE);
3586 if (ret < 0)
Olivier Deprez0e641232021-09-23 10:07:05 +02003587 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003588 ret = qgroup_reserve(root, to_reserve, true, BTRFS_QGROUP_RSV_DATA);
3589 if (ret < 0)
3590 goto cleanup;
3591
3592 return ret;
3593
3594cleanup:
Olivier Deprez0e641232021-09-23 10:07:05 +02003595 qgroup_unreserve_range(inode, reserved, start, len);
3596out:
3597 if (new_reserved) {
3598 extent_changeset_release(reserved);
3599 kfree(reserved);
3600 *reserved_ret = NULL;
3601 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003602 return ret;
3603}
3604
Olivier Deprez0e641232021-09-23 10:07:05 +02003605/*
3606 * Reserve qgroup space for range [start, start + len).
3607 *
3608 * This function will either reserve space from related qgroups or do nothing
3609 * if the range is already reserved.
3610 *
3611 * Return 0 for successful reservation
3612 * Return <0 for error (including -EQUOT)
3613 *
3614 * NOTE: This function may sleep for memory allocation, dirty page flushing and
3615 * commit transaction. So caller should not hold any dirty page locked.
3616 */
3617int btrfs_qgroup_reserve_data(struct btrfs_inode *inode,
3618 struct extent_changeset **reserved_ret, u64 start,
3619 u64 len)
3620{
3621 int ret;
3622
3623 ret = qgroup_reserve_data(inode, reserved_ret, start, len);
3624 if (ret <= 0 && ret != -EDQUOT)
3625 return ret;
3626
3627 ret = try_flush_qgroup(inode->root);
3628 if (ret < 0)
3629 return ret;
3630 return qgroup_reserve_data(inode, reserved_ret, start, len);
3631}
3632
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003633/* Free ranges specified by @reserved, normally in error path */
Olivier Deprez0e641232021-09-23 10:07:05 +02003634static int qgroup_free_reserved_data(struct btrfs_inode *inode,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003635 struct extent_changeset *reserved, u64 start, u64 len)
3636{
Olivier Deprez0e641232021-09-23 10:07:05 +02003637 struct btrfs_root *root = inode->root;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003638 struct ulist_node *unode;
3639 struct ulist_iterator uiter;
3640 struct extent_changeset changeset;
3641 int freed = 0;
3642 int ret;
3643
3644 extent_changeset_init(&changeset);
3645 len = round_up(start + len, root->fs_info->sectorsize);
3646 start = round_down(start, root->fs_info->sectorsize);
3647
3648 ULIST_ITER_INIT(&uiter);
3649 while ((unode = ulist_next(&reserved->range_changed, &uiter))) {
3650 u64 range_start = unode->val;
3651 /* unode->aux is the inclusive end */
3652 u64 range_len = unode->aux - range_start + 1;
3653 u64 free_start;
3654 u64 free_len;
3655
3656 extent_changeset_release(&changeset);
3657
3658 /* Only free range in range [start, start + len) */
3659 if (range_start >= start + len ||
3660 range_start + range_len <= start)
3661 continue;
3662 free_start = max(range_start, start);
3663 free_len = min(start + len, range_start + range_len) -
3664 free_start;
3665 /*
3666 * TODO: To also modify reserved->ranges_reserved to reflect
3667 * the modification.
3668 *
3669 * However as long as we free qgroup reserved according to
3670 * EXTENT_QGROUP_RESERVED, we won't double free.
3671 * So not need to rush.
3672 */
Olivier Deprez0e641232021-09-23 10:07:05 +02003673 ret = clear_record_extent_bits(&inode->io_tree, free_start,
3674 free_start + free_len - 1,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003675 EXTENT_QGROUP_RESERVED, &changeset);
3676 if (ret < 0)
3677 goto out;
3678 freed += changeset.bytes_changed;
3679 }
David Brazdil0f672f62019-12-10 10:32:29 +00003680 btrfs_qgroup_free_refroot(root->fs_info, root->root_key.objectid, freed,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003681 BTRFS_QGROUP_RSV_DATA);
3682 ret = freed;
3683out:
3684 extent_changeset_release(&changeset);
3685 return ret;
3686}
3687
3688static int __btrfs_qgroup_release_data(struct inode *inode,
3689 struct extent_changeset *reserved, u64 start, u64 len,
3690 int free)
3691{
3692 struct extent_changeset changeset;
3693 int trace_op = QGROUP_RELEASE;
3694 int ret;
3695
3696 if (!test_bit(BTRFS_FS_QUOTA_ENABLED,
3697 &BTRFS_I(inode)->root->fs_info->flags))
3698 return 0;
3699
3700 /* In release case, we shouldn't have @reserved */
3701 WARN_ON(!free && reserved);
3702 if (free && reserved)
Olivier Deprez0e641232021-09-23 10:07:05 +02003703 return qgroup_free_reserved_data(BTRFS_I(inode), reserved,
3704 start, len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003705 extent_changeset_init(&changeset);
3706 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
3707 start + len -1, EXTENT_QGROUP_RESERVED, &changeset);
3708 if (ret < 0)
3709 goto out;
3710
3711 if (free)
3712 trace_op = QGROUP_FREE;
3713 trace_btrfs_qgroup_release_data(inode, start, len,
3714 changeset.bytes_changed, trace_op);
3715 if (free)
3716 btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
David Brazdil0f672f62019-12-10 10:32:29 +00003717 BTRFS_I(inode)->root->root_key.objectid,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003718 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
3719 ret = changeset.bytes_changed;
3720out:
3721 extent_changeset_release(&changeset);
3722 return ret;
3723}
3724
3725/*
3726 * Free a reserved space range from io_tree and related qgroups
3727 *
3728 * Should be called when a range of pages get invalidated before reaching disk.
3729 * Or for error cleanup case.
3730 * if @reserved is given, only reserved range in [@start, @start + @len) will
3731 * be freed.
3732 *
3733 * For data written to disk, use btrfs_qgroup_release_data().
3734 *
3735 * NOTE: This function may sleep for memory allocation.
3736 */
3737int btrfs_qgroup_free_data(struct inode *inode,
3738 struct extent_changeset *reserved, u64 start, u64 len)
3739{
3740 return __btrfs_qgroup_release_data(inode, reserved, start, len, 1);
3741}
3742
3743/*
3744 * Release a reserved space range from io_tree only.
3745 *
3746 * Should be called when a range of pages get written to disk and corresponding
3747 * FILE_EXTENT is inserted into corresponding root.
3748 *
3749 * Since new qgroup accounting framework will only update qgroup numbers at
3750 * commit_transaction() time, its reserved space shouldn't be freed from
3751 * related qgroups.
3752 *
3753 * But we should release the range from io_tree, to allow further write to be
3754 * COWed.
3755 *
3756 * NOTE: This function may sleep for memory allocation.
3757 */
3758int btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len)
3759{
3760 return __btrfs_qgroup_release_data(inode, NULL, start, len, 0);
3761}
3762
3763static void add_root_meta_rsv(struct btrfs_root *root, int num_bytes,
3764 enum btrfs_qgroup_rsv_type type)
3765{
3766 if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
3767 type != BTRFS_QGROUP_RSV_META_PERTRANS)
3768 return;
3769 if (num_bytes == 0)
3770 return;
3771
3772 spin_lock(&root->qgroup_meta_rsv_lock);
3773 if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
3774 root->qgroup_meta_rsv_prealloc += num_bytes;
3775 else
3776 root->qgroup_meta_rsv_pertrans += num_bytes;
3777 spin_unlock(&root->qgroup_meta_rsv_lock);
3778}
3779
3780static int sub_root_meta_rsv(struct btrfs_root *root, int num_bytes,
3781 enum btrfs_qgroup_rsv_type type)
3782{
3783 if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
3784 type != BTRFS_QGROUP_RSV_META_PERTRANS)
3785 return 0;
3786 if (num_bytes == 0)
3787 return 0;
3788
3789 spin_lock(&root->qgroup_meta_rsv_lock);
3790 if (type == BTRFS_QGROUP_RSV_META_PREALLOC) {
3791 num_bytes = min_t(u64, root->qgroup_meta_rsv_prealloc,
3792 num_bytes);
3793 root->qgroup_meta_rsv_prealloc -= num_bytes;
3794 } else {
3795 num_bytes = min_t(u64, root->qgroup_meta_rsv_pertrans,
3796 num_bytes);
3797 root->qgroup_meta_rsv_pertrans -= num_bytes;
3798 }
3799 spin_unlock(&root->qgroup_meta_rsv_lock);
3800 return num_bytes;
3801}
3802
Olivier Deprez0e641232021-09-23 10:07:05 +02003803int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
3804 enum btrfs_qgroup_rsv_type type, bool enforce)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003805{
3806 struct btrfs_fs_info *fs_info = root->fs_info;
3807 int ret;
3808
3809 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
David Brazdil0f672f62019-12-10 10:32:29 +00003810 !is_fstree(root->root_key.objectid) || num_bytes == 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003811 return 0;
3812
3813 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
David Brazdil0f672f62019-12-10 10:32:29 +00003814 trace_qgroup_meta_reserve(root, (s64)num_bytes, type);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003815 ret = qgroup_reserve(root, num_bytes, enforce, type);
3816 if (ret < 0)
3817 return ret;
3818 /*
3819 * Record what we have reserved into root.
3820 *
3821 * To avoid quota disabled->enabled underflow.
3822 * In that case, we may try to free space we haven't reserved
3823 * (since quota was disabled), so record what we reserved into root.
3824 * And ensure later release won't underflow this number.
3825 */
3826 add_root_meta_rsv(root, num_bytes, type);
3827 return ret;
3828}
3829
Olivier Deprez0e641232021-09-23 10:07:05 +02003830int __btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
3831 enum btrfs_qgroup_rsv_type type, bool enforce)
3832{
3833 int ret;
3834
3835 ret = btrfs_qgroup_reserve_meta(root, num_bytes, type, enforce);
3836 if (ret <= 0 && ret != -EDQUOT)
3837 return ret;
3838
3839 ret = try_flush_qgroup(root);
3840 if (ret < 0)
3841 return ret;
3842 return btrfs_qgroup_reserve_meta(root, num_bytes, type, enforce);
3843}
3844
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003845void btrfs_qgroup_free_meta_all_pertrans(struct btrfs_root *root)
3846{
3847 struct btrfs_fs_info *fs_info = root->fs_info;
3848
3849 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
David Brazdil0f672f62019-12-10 10:32:29 +00003850 !is_fstree(root->root_key.objectid))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003851 return;
3852
3853 /* TODO: Update trace point to handle such free */
3854 trace_qgroup_meta_free_all_pertrans(root);
3855 /* Special value -1 means to free all reserved space */
David Brazdil0f672f62019-12-10 10:32:29 +00003856 btrfs_qgroup_free_refroot(fs_info, root->root_key.objectid, (u64)-1,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003857 BTRFS_QGROUP_RSV_META_PERTRANS);
3858}
3859
3860void __btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes,
3861 enum btrfs_qgroup_rsv_type type)
3862{
3863 struct btrfs_fs_info *fs_info = root->fs_info;
3864
3865 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
David Brazdil0f672f62019-12-10 10:32:29 +00003866 !is_fstree(root->root_key.objectid))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003867 return;
3868
3869 /*
3870 * reservation for META_PREALLOC can happen before quota is enabled,
3871 * which can lead to underflow.
3872 * Here ensure we will only free what we really have reserved.
3873 */
3874 num_bytes = sub_root_meta_rsv(root, num_bytes, type);
3875 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
David Brazdil0f672f62019-12-10 10:32:29 +00003876 trace_qgroup_meta_reserve(root, -(s64)num_bytes, type);
3877 btrfs_qgroup_free_refroot(fs_info, root->root_key.objectid,
3878 num_bytes, type);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003879}
3880
3881static void qgroup_convert_meta(struct btrfs_fs_info *fs_info, u64 ref_root,
3882 int num_bytes)
3883{
3884 struct btrfs_root *quota_root = fs_info->quota_root;
3885 struct btrfs_qgroup *qgroup;
3886 struct ulist_node *unode;
3887 struct ulist_iterator uiter;
3888 int ret = 0;
3889
3890 if (num_bytes == 0)
3891 return;
3892 if (!quota_root)
3893 return;
3894
3895 spin_lock(&fs_info->qgroup_lock);
3896 qgroup = find_qgroup_rb(fs_info, ref_root);
3897 if (!qgroup)
3898 goto out;
3899 ulist_reinit(fs_info->qgroup_ulist);
3900 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
3901 qgroup_to_aux(qgroup), GFP_ATOMIC);
3902 if (ret < 0)
3903 goto out;
3904 ULIST_ITER_INIT(&uiter);
3905 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
3906 struct btrfs_qgroup *qg;
3907 struct btrfs_qgroup_list *glist;
3908
3909 qg = unode_aux_to_qgroup(unode);
3910
3911 qgroup_rsv_release(fs_info, qg, num_bytes,
3912 BTRFS_QGROUP_RSV_META_PREALLOC);
3913 qgroup_rsv_add(fs_info, qg, num_bytes,
3914 BTRFS_QGROUP_RSV_META_PERTRANS);
3915 list_for_each_entry(glist, &qg->groups, next_group) {
3916 ret = ulist_add(fs_info->qgroup_ulist,
3917 glist->group->qgroupid,
3918 qgroup_to_aux(glist->group), GFP_ATOMIC);
3919 if (ret < 0)
3920 goto out;
3921 }
3922 }
3923out:
3924 spin_unlock(&fs_info->qgroup_lock);
3925}
3926
3927void btrfs_qgroup_convert_reserved_meta(struct btrfs_root *root, int num_bytes)
3928{
3929 struct btrfs_fs_info *fs_info = root->fs_info;
3930
3931 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
David Brazdil0f672f62019-12-10 10:32:29 +00003932 !is_fstree(root->root_key.objectid))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003933 return;
3934 /* Same as btrfs_qgroup_free_meta_prealloc() */
3935 num_bytes = sub_root_meta_rsv(root, num_bytes,
3936 BTRFS_QGROUP_RSV_META_PREALLOC);
3937 trace_qgroup_meta_convert(root, num_bytes);
David Brazdil0f672f62019-12-10 10:32:29 +00003938 qgroup_convert_meta(fs_info, root->root_key.objectid, num_bytes);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003939}
3940
3941/*
3942 * Check qgroup reserved space leaking, normally at destroy inode
3943 * time
3944 */
Olivier Deprez0e641232021-09-23 10:07:05 +02003945void btrfs_qgroup_check_reserved_leak(struct btrfs_inode *inode)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003946{
3947 struct extent_changeset changeset;
3948 struct ulist_node *unode;
3949 struct ulist_iterator iter;
3950 int ret;
3951
3952 extent_changeset_init(&changeset);
Olivier Deprez0e641232021-09-23 10:07:05 +02003953 ret = clear_record_extent_bits(&inode->io_tree, 0, (u64)-1,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003954 EXTENT_QGROUP_RESERVED, &changeset);
3955
3956 WARN_ON(ret < 0);
3957 if (WARN_ON(changeset.bytes_changed)) {
3958 ULIST_ITER_INIT(&iter);
3959 while ((unode = ulist_next(&changeset.range_changed, &iter))) {
Olivier Deprez0e641232021-09-23 10:07:05 +02003960 btrfs_warn(inode->root->fs_info,
3961 "leaking qgroup reserved space, ino: %llu, start: %llu, end: %llu",
3962 btrfs_ino(inode), unode->val, unode->aux);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003963 }
Olivier Deprez0e641232021-09-23 10:07:05 +02003964 btrfs_qgroup_free_refroot(inode->root->fs_info,
3965 inode->root->root_key.objectid,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003966 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
3967
3968 }
3969 extent_changeset_release(&changeset);
3970}
David Brazdil0f672f62019-12-10 10:32:29 +00003971
3972void btrfs_qgroup_init_swapped_blocks(
3973 struct btrfs_qgroup_swapped_blocks *swapped_blocks)
3974{
3975 int i;
3976
3977 spin_lock_init(&swapped_blocks->lock);
3978 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
3979 swapped_blocks->blocks[i] = RB_ROOT;
3980 swapped_blocks->swapped = false;
3981}
3982
3983/*
3984 * Delete all swapped blocks record of @root.
3985 * Every record here means we skipped a full subtree scan for qgroup.
3986 *
3987 * Gets called when committing one transaction.
3988 */
3989void btrfs_qgroup_clean_swapped_blocks(struct btrfs_root *root)
3990{
3991 struct btrfs_qgroup_swapped_blocks *swapped_blocks;
3992 int i;
3993
3994 swapped_blocks = &root->swapped_blocks;
3995
3996 spin_lock(&swapped_blocks->lock);
3997 if (!swapped_blocks->swapped)
3998 goto out;
3999 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
4000 struct rb_root *cur_root = &swapped_blocks->blocks[i];
4001 struct btrfs_qgroup_swapped_block *entry;
4002 struct btrfs_qgroup_swapped_block *next;
4003
4004 rbtree_postorder_for_each_entry_safe(entry, next, cur_root,
4005 node)
4006 kfree(entry);
4007 swapped_blocks->blocks[i] = RB_ROOT;
4008 }
4009 swapped_blocks->swapped = false;
4010out:
4011 spin_unlock(&swapped_blocks->lock);
4012}
4013
4014/*
4015 * Add subtree roots record into @subvol_root.
4016 *
4017 * @subvol_root: tree root of the subvolume tree get swapped
4018 * @bg: block group under balance
4019 * @subvol_parent/slot: pointer to the subtree root in subvolume tree
4020 * @reloc_parent/slot: pointer to the subtree root in reloc tree
4021 * BOTH POINTERS ARE BEFORE TREE SWAP
4022 * @last_snapshot: last snapshot generation of the subvolume tree
4023 */
4024int btrfs_qgroup_add_swapped_blocks(struct btrfs_trans_handle *trans,
4025 struct btrfs_root *subvol_root,
4026 struct btrfs_block_group_cache *bg,
4027 struct extent_buffer *subvol_parent, int subvol_slot,
4028 struct extent_buffer *reloc_parent, int reloc_slot,
4029 u64 last_snapshot)
4030{
4031 struct btrfs_fs_info *fs_info = subvol_root->fs_info;
4032 struct btrfs_qgroup_swapped_blocks *blocks = &subvol_root->swapped_blocks;
4033 struct btrfs_qgroup_swapped_block *block;
4034 struct rb_node **cur;
4035 struct rb_node *parent = NULL;
4036 int level = btrfs_header_level(subvol_parent) - 1;
4037 int ret = 0;
4038
4039 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
4040 return 0;
4041
4042 if (btrfs_node_ptr_generation(subvol_parent, subvol_slot) >
4043 btrfs_node_ptr_generation(reloc_parent, reloc_slot)) {
4044 btrfs_err_rl(fs_info,
4045 "%s: bad parameter order, subvol_gen=%llu reloc_gen=%llu",
4046 __func__,
4047 btrfs_node_ptr_generation(subvol_parent, subvol_slot),
4048 btrfs_node_ptr_generation(reloc_parent, reloc_slot));
4049 return -EUCLEAN;
4050 }
4051
4052 block = kmalloc(sizeof(*block), GFP_NOFS);
4053 if (!block) {
4054 ret = -ENOMEM;
4055 goto out;
4056 }
4057
4058 /*
4059 * @reloc_parent/slot is still before swap, while @block is going to
4060 * record the bytenr after swap, so we do the swap here.
4061 */
4062 block->subvol_bytenr = btrfs_node_blockptr(reloc_parent, reloc_slot);
4063 block->subvol_generation = btrfs_node_ptr_generation(reloc_parent,
4064 reloc_slot);
4065 block->reloc_bytenr = btrfs_node_blockptr(subvol_parent, subvol_slot);
4066 block->reloc_generation = btrfs_node_ptr_generation(subvol_parent,
4067 subvol_slot);
4068 block->last_snapshot = last_snapshot;
4069 block->level = level;
4070
4071 /*
4072 * If we have bg == NULL, we're called from btrfs_recover_relocation(),
4073 * no one else can modify tree blocks thus we qgroup will not change
4074 * no matter the value of trace_leaf.
4075 */
4076 if (bg && bg->flags & BTRFS_BLOCK_GROUP_DATA)
4077 block->trace_leaf = true;
4078 else
4079 block->trace_leaf = false;
4080 btrfs_node_key_to_cpu(reloc_parent, &block->first_key, reloc_slot);
4081
4082 /* Insert @block into @blocks */
4083 spin_lock(&blocks->lock);
4084 cur = &blocks->blocks[level].rb_node;
4085 while (*cur) {
4086 struct btrfs_qgroup_swapped_block *entry;
4087
4088 parent = *cur;
4089 entry = rb_entry(parent, struct btrfs_qgroup_swapped_block,
4090 node);
4091
4092 if (entry->subvol_bytenr < block->subvol_bytenr) {
4093 cur = &(*cur)->rb_left;
4094 } else if (entry->subvol_bytenr > block->subvol_bytenr) {
4095 cur = &(*cur)->rb_right;
4096 } else {
4097 if (entry->subvol_generation !=
4098 block->subvol_generation ||
4099 entry->reloc_bytenr != block->reloc_bytenr ||
4100 entry->reloc_generation !=
4101 block->reloc_generation) {
4102 /*
4103 * Duplicated but mismatch entry found.
4104 * Shouldn't happen.
4105 *
4106 * Marking qgroup inconsistent should be enough
4107 * for end users.
4108 */
4109 WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
4110 ret = -EEXIST;
4111 }
4112 kfree(block);
4113 goto out_unlock;
4114 }
4115 }
4116 rb_link_node(&block->node, parent, cur);
4117 rb_insert_color(&block->node, &blocks->blocks[level]);
4118 blocks->swapped = true;
4119out_unlock:
4120 spin_unlock(&blocks->lock);
4121out:
4122 if (ret < 0)
4123 fs_info->qgroup_flags |=
4124 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
4125 return ret;
4126}
4127
4128/*
4129 * Check if the tree block is a subtree root, and if so do the needed
4130 * delayed subtree trace for qgroup.
4131 *
4132 * This is called during btrfs_cow_block().
4133 */
4134int btrfs_qgroup_trace_subtree_after_cow(struct btrfs_trans_handle *trans,
4135 struct btrfs_root *root,
4136 struct extent_buffer *subvol_eb)
4137{
4138 struct btrfs_fs_info *fs_info = root->fs_info;
4139 struct btrfs_qgroup_swapped_blocks *blocks = &root->swapped_blocks;
4140 struct btrfs_qgroup_swapped_block *block;
4141 struct extent_buffer *reloc_eb = NULL;
4142 struct rb_node *node;
4143 bool found = false;
4144 bool swapped = false;
4145 int level = btrfs_header_level(subvol_eb);
4146 int ret = 0;
4147 int i;
4148
4149 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
4150 return 0;
4151 if (!is_fstree(root->root_key.objectid) || !root->reloc_root)
4152 return 0;
4153
4154 spin_lock(&blocks->lock);
4155 if (!blocks->swapped) {
4156 spin_unlock(&blocks->lock);
4157 return 0;
4158 }
4159 node = blocks->blocks[level].rb_node;
4160
4161 while (node) {
4162 block = rb_entry(node, struct btrfs_qgroup_swapped_block, node);
4163 if (block->subvol_bytenr < subvol_eb->start) {
4164 node = node->rb_left;
4165 } else if (block->subvol_bytenr > subvol_eb->start) {
4166 node = node->rb_right;
4167 } else {
4168 found = true;
4169 break;
4170 }
4171 }
4172 if (!found) {
4173 spin_unlock(&blocks->lock);
4174 goto out;
4175 }
4176 /* Found one, remove it from @blocks first and update blocks->swapped */
4177 rb_erase(&block->node, &blocks->blocks[level]);
4178 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
4179 if (RB_EMPTY_ROOT(&blocks->blocks[i])) {
4180 swapped = true;
4181 break;
4182 }
4183 }
4184 blocks->swapped = swapped;
4185 spin_unlock(&blocks->lock);
4186
4187 /* Read out reloc subtree root */
4188 reloc_eb = read_tree_block(fs_info, block->reloc_bytenr,
4189 block->reloc_generation, block->level,
4190 &block->first_key);
4191 if (IS_ERR(reloc_eb)) {
4192 ret = PTR_ERR(reloc_eb);
4193 reloc_eb = NULL;
4194 goto free_out;
4195 }
4196 if (!extent_buffer_uptodate(reloc_eb)) {
4197 ret = -EIO;
4198 goto free_out;
4199 }
4200
4201 ret = qgroup_trace_subtree_swap(trans, reloc_eb, subvol_eb,
4202 block->last_snapshot, block->trace_leaf);
4203free_out:
4204 kfree(block);
4205 free_extent_buffer(reloc_eb);
4206out:
4207 if (ret < 0) {
4208 btrfs_err_rl(fs_info,
4209 "failed to account subtree at bytenr %llu: %d",
4210 subvol_eb->start, ret);
4211 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
4212 }
4213 return ret;
4214}
Olivier Deprez0e641232021-09-23 10:07:05 +02004215
4216void btrfs_qgroup_destroy_extent_records(struct btrfs_transaction *trans)
4217{
4218 struct btrfs_qgroup_extent_record *entry;
4219 struct btrfs_qgroup_extent_record *next;
4220 struct rb_root *root;
4221
4222 root = &trans->delayed_refs.dirty_extent_root;
4223 rbtree_postorder_for_each_entry_safe(entry, next, root, node) {
4224 ulist_free(entry->old_roots);
4225 kfree(entry);
4226 }
4227}