blob: a02e38fb696c17f62d81a9ef0358bf376d411c48 [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>
Olivier Deprez157378f2022-04-04 15:47:50 +020014#include <linux/sched/mm.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000015
16#include "ctree.h"
17#include "transaction.h"
18#include "disk-io.h"
19#include "locking.h"
20#include "ulist.h"
21#include "backref.h"
22#include "extent_io.h"
23#include "qgroup.h"
David Brazdil0f672f62019-12-10 10:32:29 +000024#include "block-group.h"
Olivier Deprez157378f2022-04-04 15:47:50 +020025#include "sysfs.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000026
27/* TODO XXX FIXME
28 * - subvol delete -> delete when ref goes to 0? delete limits also?
29 * - reorganize keys
30 * - compressed
31 * - sync
32 * - copy also limits on subvol creation
33 * - limit
David Brazdil0f672f62019-12-10 10:32:29 +000034 * - caches for ulists
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000035 * - performance benchmarks
36 * - check all ioctl parameters
37 */
38
39/*
40 * Helpers to access qgroup reservation
41 *
42 * Callers should ensure the lock context and type are valid
43 */
44
45static u64 qgroup_rsv_total(const struct btrfs_qgroup *qgroup)
46{
47 u64 ret = 0;
48 int i;
49
50 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
51 ret += qgroup->rsv.values[i];
52
53 return ret;
54}
55
56#ifdef CONFIG_BTRFS_DEBUG
57static const char *qgroup_rsv_type_str(enum btrfs_qgroup_rsv_type type)
58{
59 if (type == BTRFS_QGROUP_RSV_DATA)
60 return "data";
61 if (type == BTRFS_QGROUP_RSV_META_PERTRANS)
62 return "meta_pertrans";
63 if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
64 return "meta_prealloc";
65 return NULL;
66}
67#endif
68
69static void qgroup_rsv_add(struct btrfs_fs_info *fs_info,
70 struct btrfs_qgroup *qgroup, u64 num_bytes,
71 enum btrfs_qgroup_rsv_type type)
72{
73 trace_qgroup_update_reserve(fs_info, qgroup, num_bytes, type);
74 qgroup->rsv.values[type] += num_bytes;
75}
76
77static void qgroup_rsv_release(struct btrfs_fs_info *fs_info,
78 struct btrfs_qgroup *qgroup, u64 num_bytes,
79 enum btrfs_qgroup_rsv_type type)
80{
81 trace_qgroup_update_reserve(fs_info, qgroup, -(s64)num_bytes, type);
82 if (qgroup->rsv.values[type] >= num_bytes) {
83 qgroup->rsv.values[type] -= num_bytes;
84 return;
85 }
86#ifdef CONFIG_BTRFS_DEBUG
87 WARN_RATELIMIT(1,
88 "qgroup %llu %s reserved space underflow, have %llu to free %llu",
89 qgroup->qgroupid, qgroup_rsv_type_str(type),
90 qgroup->rsv.values[type], num_bytes);
91#endif
92 qgroup->rsv.values[type] = 0;
93}
94
95static void qgroup_rsv_add_by_qgroup(struct btrfs_fs_info *fs_info,
96 struct btrfs_qgroup *dest,
97 struct btrfs_qgroup *src)
98{
99 int i;
100
101 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
102 qgroup_rsv_add(fs_info, dest, src->rsv.values[i], i);
103}
104
105static void qgroup_rsv_release_by_qgroup(struct btrfs_fs_info *fs_info,
106 struct btrfs_qgroup *dest,
107 struct btrfs_qgroup *src)
108{
109 int i;
110
111 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
112 qgroup_rsv_release(fs_info, dest, src->rsv.values[i], i);
113}
114
115static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup *qg, u64 seq,
116 int mod)
117{
118 if (qg->old_refcnt < seq)
119 qg->old_refcnt = seq;
120 qg->old_refcnt += mod;
121}
122
123static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup *qg, u64 seq,
124 int mod)
125{
126 if (qg->new_refcnt < seq)
127 qg->new_refcnt = seq;
128 qg->new_refcnt += mod;
129}
130
131static inline u64 btrfs_qgroup_get_old_refcnt(struct btrfs_qgroup *qg, u64 seq)
132{
133 if (qg->old_refcnt < seq)
134 return 0;
135 return qg->old_refcnt - seq;
136}
137
138static inline u64 btrfs_qgroup_get_new_refcnt(struct btrfs_qgroup *qg, u64 seq)
139{
140 if (qg->new_refcnt < seq)
141 return 0;
142 return qg->new_refcnt - seq;
143}
144
145/*
146 * glue structure to represent the relations between qgroups.
147 */
148struct btrfs_qgroup_list {
149 struct list_head next_group;
150 struct list_head next_member;
151 struct btrfs_qgroup *group;
152 struct btrfs_qgroup *member;
153};
154
155static inline u64 qgroup_to_aux(struct btrfs_qgroup *qg)
156{
157 return (u64)(uintptr_t)qg;
158}
159
160static inline struct btrfs_qgroup* unode_aux_to_qgroup(struct ulist_node *n)
161{
162 return (struct btrfs_qgroup *)(uintptr_t)n->aux;
163}
164
165static int
166qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
167 int init_flags);
168static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
169
170/* must be called with qgroup_ioctl_lock held */
171static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
172 u64 qgroupid)
173{
174 struct rb_node *n = fs_info->qgroup_tree.rb_node;
175 struct btrfs_qgroup *qgroup;
176
177 while (n) {
178 qgroup = rb_entry(n, struct btrfs_qgroup, node);
179 if (qgroup->qgroupid < qgroupid)
180 n = n->rb_left;
181 else if (qgroup->qgroupid > qgroupid)
182 n = n->rb_right;
183 else
184 return qgroup;
185 }
186 return NULL;
187}
188
189/* must be called with qgroup_lock held */
190static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
191 u64 qgroupid)
192{
193 struct rb_node **p = &fs_info->qgroup_tree.rb_node;
194 struct rb_node *parent = NULL;
195 struct btrfs_qgroup *qgroup;
196
197 while (*p) {
198 parent = *p;
199 qgroup = rb_entry(parent, struct btrfs_qgroup, node);
200
201 if (qgroup->qgroupid < qgroupid)
202 p = &(*p)->rb_left;
203 else if (qgroup->qgroupid > qgroupid)
204 p = &(*p)->rb_right;
205 else
206 return qgroup;
207 }
208
209 qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
210 if (!qgroup)
211 return ERR_PTR(-ENOMEM);
212
213 qgroup->qgroupid = qgroupid;
214 INIT_LIST_HEAD(&qgroup->groups);
215 INIT_LIST_HEAD(&qgroup->members);
216 INIT_LIST_HEAD(&qgroup->dirty);
217
218 rb_link_node(&qgroup->node, parent, p);
219 rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
220
221 return qgroup;
222}
223
Olivier Deprez157378f2022-04-04 15:47:50 +0200224static void __del_qgroup_rb(struct btrfs_fs_info *fs_info,
225 struct btrfs_qgroup *qgroup)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000226{
227 struct btrfs_qgroup_list *list;
228
229 list_del(&qgroup->dirty);
230 while (!list_empty(&qgroup->groups)) {
231 list = list_first_entry(&qgroup->groups,
232 struct btrfs_qgroup_list, next_group);
233 list_del(&list->next_group);
234 list_del(&list->next_member);
235 kfree(list);
236 }
237
238 while (!list_empty(&qgroup->members)) {
239 list = list_first_entry(&qgroup->members,
240 struct btrfs_qgroup_list, next_member);
241 list_del(&list->next_group);
242 list_del(&list->next_member);
243 kfree(list);
244 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000245}
246
247/* must be called with qgroup_lock held */
248static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
249{
250 struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
251
252 if (!qgroup)
253 return -ENOENT;
254
255 rb_erase(&qgroup->node, &fs_info->qgroup_tree);
Olivier Deprez157378f2022-04-04 15:47:50 +0200256 __del_qgroup_rb(fs_info, qgroup);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000257 return 0;
258}
259
260/* must be called with qgroup_lock held */
261static int add_relation_rb(struct btrfs_fs_info *fs_info,
262 u64 memberid, u64 parentid)
263{
264 struct btrfs_qgroup *member;
265 struct btrfs_qgroup *parent;
266 struct btrfs_qgroup_list *list;
267
268 member = find_qgroup_rb(fs_info, memberid);
269 parent = find_qgroup_rb(fs_info, parentid);
270 if (!member || !parent)
271 return -ENOENT;
272
273 list = kzalloc(sizeof(*list), GFP_ATOMIC);
274 if (!list)
275 return -ENOMEM;
276
277 list->group = parent;
278 list->member = member;
279 list_add_tail(&list->next_group, &member->groups);
280 list_add_tail(&list->next_member, &parent->members);
281
282 return 0;
283}
284
285/* must be called with qgroup_lock held */
286static int del_relation_rb(struct btrfs_fs_info *fs_info,
287 u64 memberid, u64 parentid)
288{
289 struct btrfs_qgroup *member;
290 struct btrfs_qgroup *parent;
291 struct btrfs_qgroup_list *list;
292
293 member = find_qgroup_rb(fs_info, memberid);
294 parent = find_qgroup_rb(fs_info, parentid);
295 if (!member || !parent)
296 return -ENOENT;
297
298 list_for_each_entry(list, &member->groups, next_group) {
299 if (list->group == parent) {
300 list_del(&list->next_group);
301 list_del(&list->next_member);
302 kfree(list);
303 return 0;
304 }
305 }
306 return -ENOENT;
307}
308
309#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
310int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
311 u64 rfer, u64 excl)
312{
313 struct btrfs_qgroup *qgroup;
314
315 qgroup = find_qgroup_rb(fs_info, qgroupid);
316 if (!qgroup)
317 return -EINVAL;
318 if (qgroup->rfer != rfer || qgroup->excl != excl)
319 return -EINVAL;
320 return 0;
321}
322#endif
323
324/*
325 * The full config is read in one go, only called from open_ctree()
326 * It doesn't use any locking, as at this point we're still single-threaded
327 */
328int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
329{
330 struct btrfs_key key;
331 struct btrfs_key found_key;
332 struct btrfs_root *quota_root = fs_info->quota_root;
333 struct btrfs_path *path = NULL;
334 struct extent_buffer *l;
335 int slot;
336 int ret = 0;
337 u64 flags = 0;
338 u64 rescan_progress = 0;
339
340 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
341 return 0;
342
343 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
344 if (!fs_info->qgroup_ulist) {
345 ret = -ENOMEM;
346 goto out;
347 }
348
349 path = btrfs_alloc_path();
350 if (!path) {
351 ret = -ENOMEM;
352 goto out;
353 }
354
Olivier Deprez157378f2022-04-04 15:47:50 +0200355 ret = btrfs_sysfs_add_qgroups(fs_info);
356 if (ret < 0)
357 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000358 /* default this to quota off, in case no status key is found */
359 fs_info->qgroup_flags = 0;
360
361 /*
362 * pass 1: read status, all qgroup infos and limits
363 */
364 key.objectid = 0;
365 key.type = 0;
366 key.offset = 0;
367 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
368 if (ret)
369 goto out;
370
371 while (1) {
372 struct btrfs_qgroup *qgroup;
373
374 slot = path->slots[0];
375 l = path->nodes[0];
376 btrfs_item_key_to_cpu(l, &found_key, slot);
377
378 if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
379 struct btrfs_qgroup_status_item *ptr;
380
381 ptr = btrfs_item_ptr(l, slot,
382 struct btrfs_qgroup_status_item);
383
384 if (btrfs_qgroup_status_version(l, ptr) !=
385 BTRFS_QGROUP_STATUS_VERSION) {
386 btrfs_err(fs_info,
387 "old qgroup version, quota disabled");
388 goto out;
389 }
390 if (btrfs_qgroup_status_generation(l, ptr) !=
391 fs_info->generation) {
392 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
393 btrfs_err(fs_info,
394 "qgroup generation mismatch, marked as inconsistent");
395 }
396 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
397 ptr);
398 rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
399 goto next1;
400 }
401
402 if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
403 found_key.type != BTRFS_QGROUP_LIMIT_KEY)
404 goto next1;
405
406 qgroup = find_qgroup_rb(fs_info, found_key.offset);
407 if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
408 (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
409 btrfs_err(fs_info, "inconsistent qgroup config");
410 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
411 }
412 if (!qgroup) {
413 qgroup = add_qgroup_rb(fs_info, found_key.offset);
414 if (IS_ERR(qgroup)) {
415 ret = PTR_ERR(qgroup);
416 goto out;
417 }
418 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200419 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
420 if (ret < 0)
421 goto out;
422
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000423 switch (found_key.type) {
424 case BTRFS_QGROUP_INFO_KEY: {
425 struct btrfs_qgroup_info_item *ptr;
426
427 ptr = btrfs_item_ptr(l, slot,
428 struct btrfs_qgroup_info_item);
429 qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
430 qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
431 qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
432 qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
433 /* generation currently unused */
434 break;
435 }
436 case BTRFS_QGROUP_LIMIT_KEY: {
437 struct btrfs_qgroup_limit_item *ptr;
438
439 ptr = btrfs_item_ptr(l, slot,
440 struct btrfs_qgroup_limit_item);
441 qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
442 qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
443 qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
444 qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
445 qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
446 break;
447 }
448 }
449next1:
450 ret = btrfs_next_item(quota_root, path);
451 if (ret < 0)
452 goto out;
453 if (ret)
454 break;
455 }
456 btrfs_release_path(path);
457
458 /*
459 * pass 2: read all qgroup relations
460 */
461 key.objectid = 0;
462 key.type = BTRFS_QGROUP_RELATION_KEY;
463 key.offset = 0;
464 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
465 if (ret)
466 goto out;
467 while (1) {
468 slot = path->slots[0];
469 l = path->nodes[0];
470 btrfs_item_key_to_cpu(l, &found_key, slot);
471
472 if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
473 goto next2;
474
475 if (found_key.objectid > found_key.offset) {
476 /* parent <- member, not needed to build config */
477 /* FIXME should we omit the key completely? */
478 goto next2;
479 }
480
481 ret = add_relation_rb(fs_info, found_key.objectid,
482 found_key.offset);
483 if (ret == -ENOENT) {
484 btrfs_warn(fs_info,
485 "orphan qgroup relation 0x%llx->0x%llx",
486 found_key.objectid, found_key.offset);
487 ret = 0; /* ignore the error */
488 }
489 if (ret)
490 goto out;
491next2:
492 ret = btrfs_next_item(quota_root, path);
493 if (ret < 0)
494 goto out;
495 if (ret)
496 break;
497 }
498out:
Olivier Deprez0e641232021-09-23 10:07:05 +0200499 btrfs_free_path(path);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000500 fs_info->qgroup_flags |= flags;
501 if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
502 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
503 else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
504 ret >= 0)
505 ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000506
507 if (ret < 0) {
508 ulist_free(fs_info->qgroup_ulist);
509 fs_info->qgroup_ulist = NULL;
510 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
Olivier Deprez157378f2022-04-04 15:47:50 +0200511 btrfs_sysfs_del_qgroups(fs_info);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000512 }
513
514 return ret < 0 ? ret : 0;
515}
516
517/*
Olivier Deprez157378f2022-04-04 15:47:50 +0200518 * Called in close_ctree() when quota is still enabled. This verifies we don't
519 * leak some reserved space.
520 *
521 * Return false if no reserved space is left.
522 * Return true if some reserved space is leaked.
523 */
524bool btrfs_check_quota_leak(struct btrfs_fs_info *fs_info)
525{
526 struct rb_node *node;
527 bool ret = false;
528
529 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
530 return ret;
531 /*
532 * Since we're unmounting, there is no race and no need to grab qgroup
533 * lock. And here we don't go post-order to provide a more user
534 * friendly sorted result.
535 */
536 for (node = rb_first(&fs_info->qgroup_tree); node; node = rb_next(node)) {
537 struct btrfs_qgroup *qgroup;
538 int i;
539
540 qgroup = rb_entry(node, struct btrfs_qgroup, node);
541 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++) {
542 if (qgroup->rsv.values[i]) {
543 ret = true;
544 btrfs_warn(fs_info,
545 "qgroup %hu/%llu has unreleased space, type %d rsv %llu",
546 btrfs_qgroup_level(qgroup->qgroupid),
547 btrfs_qgroup_subvolid(qgroup->qgroupid),
548 i, qgroup->rsv.values[i]);
549 }
550 }
551 }
552 return ret;
553}
554
555/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000556 * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
557 * first two are in single-threaded paths.And for the third one, we have set
558 * quota_root to be null with qgroup_lock held before, so it is safe to clean
559 * up the in-memory structures without qgroup_lock held.
560 */
561void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
562{
563 struct rb_node *n;
564 struct btrfs_qgroup *qgroup;
565
566 while ((n = rb_first(&fs_info->qgroup_tree))) {
567 qgroup = rb_entry(n, struct btrfs_qgroup, node);
568 rb_erase(n, &fs_info->qgroup_tree);
Olivier Deprez157378f2022-04-04 15:47:50 +0200569 __del_qgroup_rb(fs_info, qgroup);
570 btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
571 kfree(qgroup);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000572 }
573 /*
David Brazdil0f672f62019-12-10 10:32:29 +0000574 * We call btrfs_free_qgroup_config() when unmounting
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000575 * filesystem and disabling quota, so we set qgroup_ulist
576 * to be null here to avoid double free.
577 */
578 ulist_free(fs_info->qgroup_ulist);
579 fs_info->qgroup_ulist = NULL;
Olivier Deprez157378f2022-04-04 15:47:50 +0200580 btrfs_sysfs_del_qgroups(fs_info);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000581}
582
583static int add_qgroup_relation_item(struct btrfs_trans_handle *trans, u64 src,
584 u64 dst)
585{
586 int ret;
587 struct btrfs_root *quota_root = trans->fs_info->quota_root;
588 struct btrfs_path *path;
589 struct btrfs_key key;
590
591 path = btrfs_alloc_path();
592 if (!path)
593 return -ENOMEM;
594
595 key.objectid = src;
596 key.type = BTRFS_QGROUP_RELATION_KEY;
597 key.offset = dst;
598
599 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
600
601 btrfs_mark_buffer_dirty(path->nodes[0]);
602
603 btrfs_free_path(path);
604 return ret;
605}
606
607static int del_qgroup_relation_item(struct btrfs_trans_handle *trans, u64 src,
608 u64 dst)
609{
610 int ret;
611 struct btrfs_root *quota_root = trans->fs_info->quota_root;
612 struct btrfs_path *path;
613 struct btrfs_key key;
614
615 path = btrfs_alloc_path();
616 if (!path)
617 return -ENOMEM;
618
619 key.objectid = src;
620 key.type = BTRFS_QGROUP_RELATION_KEY;
621 key.offset = dst;
622
623 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
624 if (ret < 0)
625 goto out;
626
627 if (ret > 0) {
628 ret = -ENOENT;
629 goto out;
630 }
631
632 ret = btrfs_del_item(trans, quota_root, path);
633out:
634 btrfs_free_path(path);
635 return ret;
636}
637
638static int add_qgroup_item(struct btrfs_trans_handle *trans,
639 struct btrfs_root *quota_root, u64 qgroupid)
640{
641 int ret;
642 struct btrfs_path *path;
643 struct btrfs_qgroup_info_item *qgroup_info;
644 struct btrfs_qgroup_limit_item *qgroup_limit;
645 struct extent_buffer *leaf;
646 struct btrfs_key key;
647
648 if (btrfs_is_testing(quota_root->fs_info))
649 return 0;
650
651 path = btrfs_alloc_path();
652 if (!path)
653 return -ENOMEM;
654
655 key.objectid = 0;
656 key.type = BTRFS_QGROUP_INFO_KEY;
657 key.offset = qgroupid;
658
659 /*
660 * Avoid a transaction abort by catching -EEXIST here. In that
661 * case, we proceed by re-initializing the existing structure
662 * on disk.
663 */
664
665 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
666 sizeof(*qgroup_info));
667 if (ret && ret != -EEXIST)
668 goto out;
669
670 leaf = path->nodes[0];
671 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
672 struct btrfs_qgroup_info_item);
673 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
674 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
675 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
676 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
677 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
678
679 btrfs_mark_buffer_dirty(leaf);
680
681 btrfs_release_path(path);
682
683 key.type = BTRFS_QGROUP_LIMIT_KEY;
684 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
685 sizeof(*qgroup_limit));
686 if (ret && ret != -EEXIST)
687 goto out;
688
689 leaf = path->nodes[0];
690 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
691 struct btrfs_qgroup_limit_item);
692 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
693 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
694 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
695 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
696 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
697
698 btrfs_mark_buffer_dirty(leaf);
699
700 ret = 0;
701out:
702 btrfs_free_path(path);
703 return ret;
704}
705
706static int del_qgroup_item(struct btrfs_trans_handle *trans, u64 qgroupid)
707{
708 int ret;
709 struct btrfs_root *quota_root = trans->fs_info->quota_root;
710 struct btrfs_path *path;
711 struct btrfs_key key;
712
713 path = btrfs_alloc_path();
714 if (!path)
715 return -ENOMEM;
716
717 key.objectid = 0;
718 key.type = BTRFS_QGROUP_INFO_KEY;
719 key.offset = qgroupid;
720 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
721 if (ret < 0)
722 goto out;
723
724 if (ret > 0) {
725 ret = -ENOENT;
726 goto out;
727 }
728
729 ret = btrfs_del_item(trans, quota_root, path);
730 if (ret)
731 goto out;
732
733 btrfs_release_path(path);
734
735 key.type = BTRFS_QGROUP_LIMIT_KEY;
736 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
737 if (ret < 0)
738 goto out;
739
740 if (ret > 0) {
741 ret = -ENOENT;
742 goto out;
743 }
744
745 ret = btrfs_del_item(trans, quota_root, path);
746
747out:
748 btrfs_free_path(path);
749 return ret;
750}
751
752static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
753 struct btrfs_qgroup *qgroup)
754{
755 struct btrfs_root *quota_root = trans->fs_info->quota_root;
756 struct btrfs_path *path;
757 struct btrfs_key key;
758 struct extent_buffer *l;
759 struct btrfs_qgroup_limit_item *qgroup_limit;
760 int ret;
761 int slot;
762
763 key.objectid = 0;
764 key.type = BTRFS_QGROUP_LIMIT_KEY;
765 key.offset = qgroup->qgroupid;
766
767 path = btrfs_alloc_path();
768 if (!path)
769 return -ENOMEM;
770
771 ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
772 if (ret > 0)
773 ret = -ENOENT;
774
775 if (ret)
776 goto out;
777
778 l = path->nodes[0];
779 slot = path->slots[0];
780 qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
781 btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
782 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
783 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
784 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
785 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
786
787 btrfs_mark_buffer_dirty(l);
788
789out:
790 btrfs_free_path(path);
791 return ret;
792}
793
794static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
795 struct btrfs_qgroup *qgroup)
796{
797 struct btrfs_fs_info *fs_info = trans->fs_info;
798 struct btrfs_root *quota_root = fs_info->quota_root;
799 struct btrfs_path *path;
800 struct btrfs_key key;
801 struct extent_buffer *l;
802 struct btrfs_qgroup_info_item *qgroup_info;
803 int ret;
804 int slot;
805
806 if (btrfs_is_testing(fs_info))
807 return 0;
808
809 key.objectid = 0;
810 key.type = BTRFS_QGROUP_INFO_KEY;
811 key.offset = qgroup->qgroupid;
812
813 path = btrfs_alloc_path();
814 if (!path)
815 return -ENOMEM;
816
817 ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
818 if (ret > 0)
819 ret = -ENOENT;
820
821 if (ret)
822 goto out;
823
824 l = path->nodes[0];
825 slot = path->slots[0];
826 qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
827 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
828 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
829 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
830 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
831 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
832
833 btrfs_mark_buffer_dirty(l);
834
835out:
836 btrfs_free_path(path);
837 return ret;
838}
839
840static int update_qgroup_status_item(struct btrfs_trans_handle *trans)
841{
842 struct btrfs_fs_info *fs_info = trans->fs_info;
843 struct btrfs_root *quota_root = fs_info->quota_root;
844 struct btrfs_path *path;
845 struct btrfs_key key;
846 struct extent_buffer *l;
847 struct btrfs_qgroup_status_item *ptr;
848 int ret;
849 int slot;
850
851 key.objectid = 0;
852 key.type = BTRFS_QGROUP_STATUS_KEY;
853 key.offset = 0;
854
855 path = btrfs_alloc_path();
856 if (!path)
857 return -ENOMEM;
858
859 ret = btrfs_search_slot(trans, quota_root, &key, path, 0, 1);
860 if (ret > 0)
861 ret = -ENOENT;
862
863 if (ret)
864 goto out;
865
866 l = path->nodes[0];
867 slot = path->slots[0];
868 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
869 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
870 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
871 btrfs_set_qgroup_status_rescan(l, ptr,
872 fs_info->qgroup_rescan_progress.objectid);
873
874 btrfs_mark_buffer_dirty(l);
875
876out:
877 btrfs_free_path(path);
878 return ret;
879}
880
881/*
882 * called with qgroup_lock held
883 */
884static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
885 struct btrfs_root *root)
886{
887 struct btrfs_path *path;
888 struct btrfs_key key;
889 struct extent_buffer *leaf = NULL;
890 int ret;
891 int nr = 0;
892
893 path = btrfs_alloc_path();
894 if (!path)
895 return -ENOMEM;
896
897 path->leave_spinning = 1;
898
899 key.objectid = 0;
900 key.offset = 0;
901 key.type = 0;
902
903 while (1) {
904 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
905 if (ret < 0)
906 goto out;
907 leaf = path->nodes[0];
908 nr = btrfs_header_nritems(leaf);
909 if (!nr)
910 break;
911 /*
912 * delete the leaf one by one
913 * since the whole tree is going
914 * to be deleted.
915 */
916 path->slots[0] = 0;
917 ret = btrfs_del_items(trans, root, path, 0, nr);
918 if (ret)
919 goto out;
920
921 btrfs_release_path(path);
922 }
923 ret = 0;
924out:
925 btrfs_free_path(path);
926 return ret;
927}
928
929int btrfs_quota_enable(struct btrfs_fs_info *fs_info)
930{
931 struct btrfs_root *quota_root;
932 struct btrfs_root *tree_root = fs_info->tree_root;
933 struct btrfs_path *path = NULL;
934 struct btrfs_qgroup_status_item *ptr;
935 struct extent_buffer *leaf;
936 struct btrfs_key key;
937 struct btrfs_key found_key;
938 struct btrfs_qgroup *qgroup = NULL;
939 struct btrfs_trans_handle *trans = NULL;
Olivier Deprez0e641232021-09-23 10:07:05 +0200940 struct ulist *ulist = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000941 int ret = 0;
942 int slot;
943
Olivier Deprez157378f2022-04-04 15:47:50 +0200944 /*
945 * We need to have subvol_sem write locked, to prevent races between
946 * concurrent tasks trying to enable quotas, because we will unlock
947 * and relock qgroup_ioctl_lock before setting fs_info->quota_root
948 * and before setting BTRFS_FS_QUOTA_ENABLED.
949 */
950 lockdep_assert_held_write(&fs_info->subvol_sem);
951
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000952 mutex_lock(&fs_info->qgroup_ioctl_lock);
953 if (fs_info->quota_root)
954 goto out;
955
Olivier Deprez0e641232021-09-23 10:07:05 +0200956 ulist = ulist_alloc(GFP_KERNEL);
957 if (!ulist) {
David Brazdil0f672f62019-12-10 10:32:29 +0000958 ret = -ENOMEM;
959 goto out;
960 }
961
Olivier Deprez157378f2022-04-04 15:47:50 +0200962 ret = btrfs_sysfs_add_qgroups(fs_info);
963 if (ret < 0)
964 goto out;
965
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000966 /*
Olivier Deprez0e641232021-09-23 10:07:05 +0200967 * Unlock qgroup_ioctl_lock before starting the transaction. This is to
968 * avoid lock acquisition inversion problems (reported by lockdep) between
969 * qgroup_ioctl_lock and the vfs freeze semaphores, acquired when we
970 * start a transaction.
971 * After we started the transaction lock qgroup_ioctl_lock again and
972 * check if someone else created the quota root in the meanwhile. If so,
973 * just return success and release the transaction handle.
974 *
975 * Also we don't need to worry about someone else calling
976 * btrfs_sysfs_add_qgroups() after we unlock and getting an error because
977 * that function returns 0 (success) when the sysfs entries already exist.
978 */
979 mutex_unlock(&fs_info->qgroup_ioctl_lock);
980
981 /*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000982 * 1 for quota root item
983 * 1 for BTRFS_QGROUP_STATUS item
984 *
985 * Yet we also need 2*n items for a QGROUP_INFO/QGROUP_LIMIT items
986 * per subvolume. However those are not currently reserved since it
987 * would be a lot of overkill.
988 */
989 trans = btrfs_start_transaction(tree_root, 2);
Olivier Deprez0e641232021-09-23 10:07:05 +0200990
991 mutex_lock(&fs_info->qgroup_ioctl_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000992 if (IS_ERR(trans)) {
993 ret = PTR_ERR(trans);
994 trans = NULL;
995 goto out;
996 }
997
Olivier Deprez0e641232021-09-23 10:07:05 +0200998 if (fs_info->quota_root)
999 goto out;
1000
1001 fs_info->qgroup_ulist = ulist;
1002 ulist = NULL;
1003
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001004 /*
1005 * initially create the quota tree
1006 */
David Brazdil0f672f62019-12-10 10:32:29 +00001007 quota_root = btrfs_create_tree(trans, BTRFS_QUOTA_TREE_OBJECTID);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001008 if (IS_ERR(quota_root)) {
1009 ret = PTR_ERR(quota_root);
1010 btrfs_abort_transaction(trans, ret);
1011 goto out;
1012 }
1013
1014 path = btrfs_alloc_path();
1015 if (!path) {
1016 ret = -ENOMEM;
1017 btrfs_abort_transaction(trans, ret);
1018 goto out_free_root;
1019 }
1020
1021 key.objectid = 0;
1022 key.type = BTRFS_QGROUP_STATUS_KEY;
1023 key.offset = 0;
1024
1025 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
1026 sizeof(*ptr));
1027 if (ret) {
1028 btrfs_abort_transaction(trans, ret);
1029 goto out_free_path;
1030 }
1031
1032 leaf = path->nodes[0];
1033 ptr = btrfs_item_ptr(leaf, path->slots[0],
1034 struct btrfs_qgroup_status_item);
1035 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
1036 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
1037 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
1038 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1039 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
1040 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
1041
1042 btrfs_mark_buffer_dirty(leaf);
1043
1044 key.objectid = 0;
1045 key.type = BTRFS_ROOT_REF_KEY;
1046 key.offset = 0;
1047
1048 btrfs_release_path(path);
1049 ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
1050 if (ret > 0)
1051 goto out_add_root;
1052 if (ret < 0) {
1053 btrfs_abort_transaction(trans, ret);
1054 goto out_free_path;
1055 }
1056
1057 while (1) {
1058 slot = path->slots[0];
1059 leaf = path->nodes[0];
1060 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1061
1062 if (found_key.type == BTRFS_ROOT_REF_KEY) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001063
1064 /* Release locks on tree_root before we access quota_root */
1065 btrfs_release_path(path);
1066
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001067 ret = add_qgroup_item(trans, quota_root,
1068 found_key.offset);
1069 if (ret) {
1070 btrfs_abort_transaction(trans, ret);
1071 goto out_free_path;
1072 }
1073
1074 qgroup = add_qgroup_rb(fs_info, found_key.offset);
1075 if (IS_ERR(qgroup)) {
1076 ret = PTR_ERR(qgroup);
1077 btrfs_abort_transaction(trans, ret);
1078 goto out_free_path;
1079 }
Olivier Deprez157378f2022-04-04 15:47:50 +02001080 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
1081 if (ret < 0) {
1082 btrfs_abort_transaction(trans, ret);
1083 goto out_free_path;
1084 }
1085 ret = btrfs_search_slot_for_read(tree_root, &found_key,
1086 path, 1, 0);
1087 if (ret < 0) {
1088 btrfs_abort_transaction(trans, ret);
1089 goto out_free_path;
1090 }
1091 if (ret > 0) {
1092 /*
1093 * Shouldn't happen, but in case it does we
1094 * don't need to do the btrfs_next_item, just
1095 * continue.
1096 */
1097 continue;
1098 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001099 }
1100 ret = btrfs_next_item(tree_root, path);
1101 if (ret < 0) {
1102 btrfs_abort_transaction(trans, ret);
1103 goto out_free_path;
1104 }
1105 if (ret)
1106 break;
1107 }
1108
1109out_add_root:
1110 btrfs_release_path(path);
1111 ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
1112 if (ret) {
1113 btrfs_abort_transaction(trans, ret);
1114 goto out_free_path;
1115 }
1116
1117 qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
1118 if (IS_ERR(qgroup)) {
1119 ret = PTR_ERR(qgroup);
1120 btrfs_abort_transaction(trans, ret);
1121 goto out_free_path;
1122 }
Olivier Deprez157378f2022-04-04 15:47:50 +02001123 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
1124 if (ret < 0) {
1125 btrfs_abort_transaction(trans, ret);
1126 goto out_free_path;
1127 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001128
Olivier Deprez157378f2022-04-04 15:47:50 +02001129 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1130 /*
1131 * Commit the transaction while not holding qgroup_ioctl_lock, to avoid
1132 * a deadlock with tasks concurrently doing other qgroup operations, such
1133 * adding/removing qgroups or adding/deleting qgroup relations for example,
1134 * because all qgroup operations first start or join a transaction and then
1135 * lock the qgroup_ioctl_lock mutex.
1136 * We are safe from a concurrent task trying to enable quotas, by calling
1137 * this function, since we are serialized by fs_info->subvol_sem.
1138 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001139 ret = btrfs_commit_transaction(trans);
1140 trans = NULL;
Olivier Deprez157378f2022-04-04 15:47:50 +02001141 mutex_lock(&fs_info->qgroup_ioctl_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001142 if (ret)
1143 goto out_free_path;
1144
David Brazdil0f672f62019-12-10 10:32:29 +00001145 /*
1146 * Set quota enabled flag after committing the transaction, to avoid
1147 * deadlocks on fs_info->qgroup_ioctl_lock with concurrent snapshot
1148 * creation.
1149 */
1150 spin_lock(&fs_info->qgroup_lock);
1151 fs_info->quota_root = quota_root;
1152 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
1153 spin_unlock(&fs_info->qgroup_lock);
1154
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001155 ret = qgroup_rescan_init(fs_info, 0, 1);
1156 if (!ret) {
1157 qgroup_rescan_zero_tracking(fs_info);
Olivier Deprez0e641232021-09-23 10:07:05 +02001158 fs_info->qgroup_rescan_running = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001159 btrfs_queue_work(fs_info->qgroup_rescan_workers,
1160 &fs_info->qgroup_rescan_work);
1161 }
1162
1163out_free_path:
1164 btrfs_free_path(path);
1165out_free_root:
Olivier Deprez157378f2022-04-04 15:47:50 +02001166 if (ret)
1167 btrfs_put_root(quota_root);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001168out:
1169 if (ret) {
1170 ulist_free(fs_info->qgroup_ulist);
1171 fs_info->qgroup_ulist = NULL;
Olivier Deprez157378f2022-04-04 15:47:50 +02001172 btrfs_sysfs_del_qgroups(fs_info);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001173 }
1174 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Olivier Deprez0e641232021-09-23 10:07:05 +02001175 if (ret && trans)
1176 btrfs_end_transaction(trans);
1177 else if (trans)
1178 ret = btrfs_end_transaction(trans);
1179 ulist_free(ulist);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001180 return ret;
1181}
1182
1183int btrfs_quota_disable(struct btrfs_fs_info *fs_info)
1184{
1185 struct btrfs_root *quota_root;
1186 struct btrfs_trans_handle *trans = NULL;
1187 int ret = 0;
1188
Olivier Deprez157378f2022-04-04 15:47:50 +02001189 /*
1190 * We need to have subvol_sem write locked, to prevent races between
1191 * concurrent tasks trying to disable quotas, because we will unlock
1192 * and relock qgroup_ioctl_lock across BTRFS_FS_QUOTA_ENABLED changes.
1193 */
1194 lockdep_assert_held_write(&fs_info->subvol_sem);
1195
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001196 mutex_lock(&fs_info->qgroup_ioctl_lock);
1197 if (!fs_info->quota_root)
1198 goto out;
Olivier Deprez157378f2022-04-04 15:47:50 +02001199
1200 /*
1201 * Unlock the qgroup_ioctl_lock mutex before waiting for the rescan worker to
1202 * complete. Otherwise we can deadlock because btrfs_remove_qgroup() needs
1203 * to lock that mutex while holding a transaction handle and the rescan
1204 * worker needs to commit a transaction.
1205 */
Olivier Deprez0e641232021-09-23 10:07:05 +02001206 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001207
1208 /*
Olivier Deprez157378f2022-04-04 15:47:50 +02001209 * Request qgroup rescan worker to complete and wait for it. This wait
1210 * must be done before transaction start for quota disable since it may
1211 * deadlock with transaction by the qgroup rescan worker.
1212 */
1213 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
1214 btrfs_qgroup_wait_for_completion(fs_info, false);
1215
1216 /*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001217 * 1 For the root item
1218 *
1219 * We should also reserve enough items for the quota tree deletion in
1220 * btrfs_clean_quota_tree but this is not done.
Olivier Deprez0e641232021-09-23 10:07:05 +02001221 *
1222 * Also, we must always start a transaction without holding the mutex
1223 * qgroup_ioctl_lock, see btrfs_quota_enable().
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001224 */
1225 trans = btrfs_start_transaction(fs_info->tree_root, 1);
Olivier Deprez0e641232021-09-23 10:07:05 +02001226
1227 mutex_lock(&fs_info->qgroup_ioctl_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001228 if (IS_ERR(trans)) {
1229 ret = PTR_ERR(trans);
Olivier Deprez0e641232021-09-23 10:07:05 +02001230 trans = NULL;
Olivier Deprez157378f2022-04-04 15:47:50 +02001231 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001232 goto out;
1233 }
1234
Olivier Deprez0e641232021-09-23 10:07:05 +02001235 if (!fs_info->quota_root)
1236 goto out;
1237
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001238 spin_lock(&fs_info->qgroup_lock);
1239 quota_root = fs_info->quota_root;
1240 fs_info->quota_root = NULL;
1241 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
1242 spin_unlock(&fs_info->qgroup_lock);
1243
1244 btrfs_free_qgroup_config(fs_info);
1245
1246 ret = btrfs_clean_quota_tree(trans, quota_root);
1247 if (ret) {
1248 btrfs_abort_transaction(trans, ret);
Olivier Deprez0e641232021-09-23 10:07:05 +02001249 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001250 }
1251
1252 ret = btrfs_del_root(trans, &quota_root->root_key);
1253 if (ret) {
1254 btrfs_abort_transaction(trans, ret);
Olivier Deprez0e641232021-09-23 10:07:05 +02001255 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001256 }
1257
1258 list_del(&quota_root->dirty_list);
1259
1260 btrfs_tree_lock(quota_root->node);
David Brazdil0f672f62019-12-10 10:32:29 +00001261 btrfs_clean_tree_block(quota_root->node);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001262 btrfs_tree_unlock(quota_root->node);
1263 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
1264
Olivier Deprez157378f2022-04-04 15:47:50 +02001265 btrfs_put_root(quota_root);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001266
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001267out:
1268 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Olivier Deprez0e641232021-09-23 10:07:05 +02001269 if (ret && trans)
1270 btrfs_end_transaction(trans);
1271 else if (trans)
1272 ret = btrfs_end_transaction(trans);
1273
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001274 return ret;
1275}
1276
1277static void qgroup_dirty(struct btrfs_fs_info *fs_info,
1278 struct btrfs_qgroup *qgroup)
1279{
1280 if (list_empty(&qgroup->dirty))
1281 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
1282}
1283
1284/*
1285 * The easy accounting, we're updating qgroup relationship whose child qgroup
1286 * only has exclusive extents.
1287 *
David Brazdil0f672f62019-12-10 10:32:29 +00001288 * In this case, all exclusive extents will also be exclusive for parent, so
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001289 * excl/rfer just get added/removed.
1290 *
1291 * So is qgroup reservation space, which should also be added/removed to
1292 * parent.
1293 * Or when child tries to release reservation space, parent will underflow its
1294 * reservation (for relationship adding case).
1295 *
1296 * Caller should hold fs_info->qgroup_lock.
1297 */
1298static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
1299 struct ulist *tmp, u64 ref_root,
1300 struct btrfs_qgroup *src, int sign)
1301{
1302 struct btrfs_qgroup *qgroup;
1303 struct btrfs_qgroup_list *glist;
1304 struct ulist_node *unode;
1305 struct ulist_iterator uiter;
1306 u64 num_bytes = src->excl;
1307 int ret = 0;
1308
1309 qgroup = find_qgroup_rb(fs_info, ref_root);
1310 if (!qgroup)
1311 goto out;
1312
1313 qgroup->rfer += sign * num_bytes;
1314 qgroup->rfer_cmpr += sign * num_bytes;
1315
1316 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1317 qgroup->excl += sign * num_bytes;
1318 qgroup->excl_cmpr += sign * num_bytes;
1319
1320 if (sign > 0)
1321 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
1322 else
1323 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
1324
1325 qgroup_dirty(fs_info, qgroup);
1326
1327 /* Get all of the parent groups that contain this qgroup */
1328 list_for_each_entry(glist, &qgroup->groups, next_group) {
1329 ret = ulist_add(tmp, glist->group->qgroupid,
1330 qgroup_to_aux(glist->group), GFP_ATOMIC);
1331 if (ret < 0)
1332 goto out;
1333 }
1334
1335 /* Iterate all of the parents and adjust their reference counts */
1336 ULIST_ITER_INIT(&uiter);
1337 while ((unode = ulist_next(tmp, &uiter))) {
1338 qgroup = unode_aux_to_qgroup(unode);
1339 qgroup->rfer += sign * num_bytes;
1340 qgroup->rfer_cmpr += sign * num_bytes;
1341 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1342 qgroup->excl += sign * num_bytes;
1343 if (sign > 0)
1344 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
1345 else
1346 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
1347 qgroup->excl_cmpr += sign * num_bytes;
1348 qgroup_dirty(fs_info, qgroup);
1349
1350 /* Add any parents of the parents */
1351 list_for_each_entry(glist, &qgroup->groups, next_group) {
1352 ret = ulist_add(tmp, glist->group->qgroupid,
1353 qgroup_to_aux(glist->group), GFP_ATOMIC);
1354 if (ret < 0)
1355 goto out;
1356 }
1357 }
1358 ret = 0;
1359out:
1360 return ret;
1361}
1362
1363
1364/*
1365 * Quick path for updating qgroup with only excl refs.
1366 *
1367 * In that case, just update all parent will be enough.
1368 * Or we needs to do a full rescan.
1369 * Caller should also hold fs_info->qgroup_lock.
1370 *
1371 * Return 0 for quick update, return >0 for need to full rescan
1372 * and mark INCONSISTENT flag.
1373 * Return < 0 for other error.
1374 */
1375static int quick_update_accounting(struct btrfs_fs_info *fs_info,
1376 struct ulist *tmp, u64 src, u64 dst,
1377 int sign)
1378{
1379 struct btrfs_qgroup *qgroup;
1380 int ret = 1;
1381 int err = 0;
1382
1383 qgroup = find_qgroup_rb(fs_info, src);
1384 if (!qgroup)
1385 goto out;
1386 if (qgroup->excl == qgroup->rfer) {
1387 ret = 0;
1388 err = __qgroup_excl_accounting(fs_info, tmp, dst,
1389 qgroup, sign);
1390 if (err < 0) {
1391 ret = err;
1392 goto out;
1393 }
1394 }
1395out:
1396 if (ret)
1397 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1398 return ret;
1399}
1400
1401int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
1402 u64 dst)
1403{
1404 struct btrfs_fs_info *fs_info = trans->fs_info;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001405 struct btrfs_qgroup *parent;
1406 struct btrfs_qgroup *member;
1407 struct btrfs_qgroup_list *list;
1408 struct ulist *tmp;
Olivier Deprez157378f2022-04-04 15:47:50 +02001409 unsigned int nofs_flag;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001410 int ret = 0;
1411
1412 /* Check the level of src and dst first */
1413 if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
1414 return -EINVAL;
1415
Olivier Deprez157378f2022-04-04 15:47:50 +02001416 /* We hold a transaction handle open, must do a NOFS allocation. */
1417 nofs_flag = memalloc_nofs_save();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001418 tmp = ulist_alloc(GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +02001419 memalloc_nofs_restore(nofs_flag);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001420 if (!tmp)
1421 return -ENOMEM;
1422
1423 mutex_lock(&fs_info->qgroup_ioctl_lock);
Olivier Deprez157378f2022-04-04 15:47:50 +02001424 if (!fs_info->quota_root) {
1425 ret = -ENOTCONN;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001426 goto out;
1427 }
1428 member = find_qgroup_rb(fs_info, src);
1429 parent = find_qgroup_rb(fs_info, dst);
1430 if (!member || !parent) {
1431 ret = -EINVAL;
1432 goto out;
1433 }
1434
1435 /* check if such qgroup relation exist firstly */
1436 list_for_each_entry(list, &member->groups, next_group) {
1437 if (list->group == parent) {
1438 ret = -EEXIST;
1439 goto out;
1440 }
1441 }
1442
1443 ret = add_qgroup_relation_item(trans, src, dst);
1444 if (ret)
1445 goto out;
1446
1447 ret = add_qgroup_relation_item(trans, dst, src);
1448 if (ret) {
1449 del_qgroup_relation_item(trans, src, dst);
1450 goto out;
1451 }
1452
1453 spin_lock(&fs_info->qgroup_lock);
1454 ret = add_relation_rb(fs_info, src, dst);
1455 if (ret < 0) {
1456 spin_unlock(&fs_info->qgroup_lock);
1457 goto out;
1458 }
1459 ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
1460 spin_unlock(&fs_info->qgroup_lock);
1461out:
1462 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1463 ulist_free(tmp);
1464 return ret;
1465}
1466
1467static int __del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
1468 u64 dst)
1469{
1470 struct btrfs_fs_info *fs_info = trans->fs_info;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001471 struct btrfs_qgroup *parent;
1472 struct btrfs_qgroup *member;
1473 struct btrfs_qgroup_list *list;
1474 struct ulist *tmp;
David Brazdil0f672f62019-12-10 10:32:29 +00001475 bool found = false;
Olivier Deprez157378f2022-04-04 15:47:50 +02001476 unsigned int nofs_flag;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001477 int ret = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001478 int ret2;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001479
Olivier Deprez157378f2022-04-04 15:47:50 +02001480 /* We hold a transaction handle open, must do a NOFS allocation. */
1481 nofs_flag = memalloc_nofs_save();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001482 tmp = ulist_alloc(GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +02001483 memalloc_nofs_restore(nofs_flag);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001484 if (!tmp)
1485 return -ENOMEM;
1486
Olivier Deprez157378f2022-04-04 15:47:50 +02001487 if (!fs_info->quota_root) {
1488 ret = -ENOTCONN;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001489 goto out;
1490 }
1491
1492 member = find_qgroup_rb(fs_info, src);
1493 parent = find_qgroup_rb(fs_info, dst);
David Brazdil0f672f62019-12-10 10:32:29 +00001494 /*
1495 * The parent/member pair doesn't exist, then try to delete the dead
1496 * relation items only.
1497 */
1498 if (!member || !parent)
1499 goto delete_item;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001500
1501 /* check if such qgroup relation exist firstly */
1502 list_for_each_entry(list, &member->groups, next_group) {
David Brazdil0f672f62019-12-10 10:32:29 +00001503 if (list->group == parent) {
1504 found = true;
1505 break;
1506 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001507 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001508
David Brazdil0f672f62019-12-10 10:32:29 +00001509delete_item:
1510 ret = del_qgroup_relation_item(trans, src, dst);
1511 if (ret < 0 && ret != -ENOENT)
1512 goto out;
1513 ret2 = del_qgroup_relation_item(trans, dst, src);
1514 if (ret2 < 0 && ret2 != -ENOENT)
1515 goto out;
1516
1517 /* At least one deletion succeeded, return 0 */
1518 if (!ret || !ret2)
1519 ret = 0;
1520
1521 if (found) {
1522 spin_lock(&fs_info->qgroup_lock);
1523 del_relation_rb(fs_info, src, dst);
1524 ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
1525 spin_unlock(&fs_info->qgroup_lock);
1526 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001527out:
1528 ulist_free(tmp);
1529 return ret;
1530}
1531
1532int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
1533 u64 dst)
1534{
1535 struct btrfs_fs_info *fs_info = trans->fs_info;
1536 int ret = 0;
1537
1538 mutex_lock(&fs_info->qgroup_ioctl_lock);
1539 ret = __del_qgroup_relation(trans, src, dst);
1540 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1541
1542 return ret;
1543}
1544
1545int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
1546{
1547 struct btrfs_fs_info *fs_info = trans->fs_info;
1548 struct btrfs_root *quota_root;
1549 struct btrfs_qgroup *qgroup;
1550 int ret = 0;
1551
1552 mutex_lock(&fs_info->qgroup_ioctl_lock);
Olivier Deprez157378f2022-04-04 15:47:50 +02001553 if (!fs_info->quota_root) {
1554 ret = -ENOTCONN;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001555 goto out;
1556 }
Olivier Deprez157378f2022-04-04 15:47:50 +02001557 quota_root = fs_info->quota_root;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001558 qgroup = find_qgroup_rb(fs_info, qgroupid);
1559 if (qgroup) {
1560 ret = -EEXIST;
1561 goto out;
1562 }
1563
1564 ret = add_qgroup_item(trans, quota_root, qgroupid);
1565 if (ret)
1566 goto out;
1567
1568 spin_lock(&fs_info->qgroup_lock);
1569 qgroup = add_qgroup_rb(fs_info, qgroupid);
1570 spin_unlock(&fs_info->qgroup_lock);
1571
Olivier Deprez157378f2022-04-04 15:47:50 +02001572 if (IS_ERR(qgroup)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001573 ret = PTR_ERR(qgroup);
Olivier Deprez157378f2022-04-04 15:47:50 +02001574 goto out;
1575 }
1576 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001577out:
1578 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1579 return ret;
1580}
1581
1582int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
1583{
1584 struct btrfs_fs_info *fs_info = trans->fs_info;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001585 struct btrfs_qgroup *qgroup;
1586 struct btrfs_qgroup_list *list;
1587 int ret = 0;
1588
1589 mutex_lock(&fs_info->qgroup_ioctl_lock);
Olivier Deprez157378f2022-04-04 15:47:50 +02001590 if (!fs_info->quota_root) {
1591 ret = -ENOTCONN;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001592 goto out;
1593 }
1594
1595 qgroup = find_qgroup_rb(fs_info, qgroupid);
1596 if (!qgroup) {
1597 ret = -ENOENT;
1598 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001599 }
David Brazdil0f672f62019-12-10 10:32:29 +00001600
1601 /* Check if there are no children of this qgroup */
1602 if (!list_empty(&qgroup->members)) {
1603 ret = -EBUSY;
1604 goto out;
1605 }
1606
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001607 ret = del_qgroup_item(trans, qgroupid);
1608 if (ret && ret != -ENOENT)
1609 goto out;
1610
1611 while (!list_empty(&qgroup->groups)) {
1612 list = list_first_entry(&qgroup->groups,
1613 struct btrfs_qgroup_list, next_group);
1614 ret = __del_qgroup_relation(trans, qgroupid,
1615 list->group->qgroupid);
1616 if (ret)
1617 goto out;
1618 }
1619
1620 spin_lock(&fs_info->qgroup_lock);
1621 del_qgroup_rb(fs_info, qgroupid);
1622 spin_unlock(&fs_info->qgroup_lock);
Olivier Deprez157378f2022-04-04 15:47:50 +02001623
1624 /*
1625 * Remove the qgroup from sysfs now without holding the qgroup_lock
1626 * spinlock, since the sysfs_remove_group() function needs to take
1627 * the mutex kernfs_mutex through kernfs_remove_by_name_ns().
1628 */
1629 btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
1630 kfree(qgroup);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001631out:
1632 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1633 return ret;
1634}
1635
1636int btrfs_limit_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid,
1637 struct btrfs_qgroup_limit *limit)
1638{
1639 struct btrfs_fs_info *fs_info = trans->fs_info;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001640 struct btrfs_qgroup *qgroup;
1641 int ret = 0;
1642 /* Sometimes we would want to clear the limit on this qgroup.
1643 * To meet this requirement, we treat the -1 as a special value
1644 * which tell kernel to clear the limit on this qgroup.
1645 */
1646 const u64 CLEAR_VALUE = -1;
1647
1648 mutex_lock(&fs_info->qgroup_ioctl_lock);
Olivier Deprez157378f2022-04-04 15:47:50 +02001649 if (!fs_info->quota_root) {
1650 ret = -ENOTCONN;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001651 goto out;
1652 }
1653
1654 qgroup = find_qgroup_rb(fs_info, qgroupid);
1655 if (!qgroup) {
1656 ret = -ENOENT;
1657 goto out;
1658 }
1659
1660 spin_lock(&fs_info->qgroup_lock);
1661 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
1662 if (limit->max_rfer == CLEAR_VALUE) {
1663 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1664 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1665 qgroup->max_rfer = 0;
1666 } else {
1667 qgroup->max_rfer = limit->max_rfer;
1668 }
1669 }
1670 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
1671 if (limit->max_excl == CLEAR_VALUE) {
1672 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1673 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1674 qgroup->max_excl = 0;
1675 } else {
1676 qgroup->max_excl = limit->max_excl;
1677 }
1678 }
1679 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
1680 if (limit->rsv_rfer == CLEAR_VALUE) {
1681 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1682 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1683 qgroup->rsv_rfer = 0;
1684 } else {
1685 qgroup->rsv_rfer = limit->rsv_rfer;
1686 }
1687 }
1688 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
1689 if (limit->rsv_excl == CLEAR_VALUE) {
1690 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1691 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1692 qgroup->rsv_excl = 0;
1693 } else {
1694 qgroup->rsv_excl = limit->rsv_excl;
1695 }
1696 }
1697 qgroup->lim_flags |= limit->flags;
1698
1699 spin_unlock(&fs_info->qgroup_lock);
1700
1701 ret = update_qgroup_limit_item(trans, qgroup);
1702 if (ret) {
1703 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1704 btrfs_info(fs_info, "unable to update quota limit for %llu",
1705 qgroupid);
1706 }
1707
1708out:
1709 mutex_unlock(&fs_info->qgroup_ioctl_lock);
1710 return ret;
1711}
1712
1713int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
1714 struct btrfs_delayed_ref_root *delayed_refs,
1715 struct btrfs_qgroup_extent_record *record)
1716{
1717 struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
1718 struct rb_node *parent_node = NULL;
1719 struct btrfs_qgroup_extent_record *entry;
1720 u64 bytenr = record->bytenr;
1721
1722 lockdep_assert_held(&delayed_refs->lock);
1723 trace_btrfs_qgroup_trace_extent(fs_info, record);
1724
1725 while (*p) {
1726 parent_node = *p;
1727 entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
1728 node);
David Brazdil0f672f62019-12-10 10:32:29 +00001729 if (bytenr < entry->bytenr) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001730 p = &(*p)->rb_left;
David Brazdil0f672f62019-12-10 10:32:29 +00001731 } else if (bytenr > entry->bytenr) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001732 p = &(*p)->rb_right;
David Brazdil0f672f62019-12-10 10:32:29 +00001733 } else {
1734 if (record->data_rsv && !entry->data_rsv) {
1735 entry->data_rsv = record->data_rsv;
1736 entry->data_rsv_refroot =
1737 record->data_rsv_refroot;
1738 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001739 return 1;
David Brazdil0f672f62019-12-10 10:32:29 +00001740 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001741 }
1742
1743 rb_link_node(&record->node, parent_node, p);
1744 rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
1745 return 0;
1746}
1747
1748int btrfs_qgroup_trace_extent_post(struct btrfs_fs_info *fs_info,
1749 struct btrfs_qgroup_extent_record *qrecord)
1750{
1751 struct ulist *old_root;
1752 u64 bytenr = qrecord->bytenr;
1753 int ret;
1754
1755 ret = btrfs_find_all_roots(NULL, fs_info, bytenr, 0, &old_root, false);
1756 if (ret < 0) {
1757 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1758 btrfs_warn(fs_info,
1759"error accounting new delayed refs extent (err code: %d), quota inconsistent",
1760 ret);
1761 return 0;
1762 }
1763
1764 /*
1765 * Here we don't need to get the lock of
1766 * trans->transaction->delayed_refs, since inserted qrecord won't
1767 * be deleted, only qrecord->node may be modified (new qrecord insert)
1768 *
1769 * So modifying qrecord->old_roots is safe here
1770 */
1771 qrecord->old_roots = old_root;
1772 return 0;
1773}
1774
1775int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans, u64 bytenr,
1776 u64 num_bytes, gfp_t gfp_flag)
1777{
1778 struct btrfs_fs_info *fs_info = trans->fs_info;
1779 struct btrfs_qgroup_extent_record *record;
1780 struct btrfs_delayed_ref_root *delayed_refs;
1781 int ret;
1782
1783 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)
1784 || bytenr == 0 || num_bytes == 0)
1785 return 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001786 record = kzalloc(sizeof(*record), gfp_flag);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001787 if (!record)
1788 return -ENOMEM;
1789
1790 delayed_refs = &trans->transaction->delayed_refs;
1791 record->bytenr = bytenr;
1792 record->num_bytes = num_bytes;
1793 record->old_roots = NULL;
1794
1795 spin_lock(&delayed_refs->lock);
1796 ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record);
1797 spin_unlock(&delayed_refs->lock);
1798 if (ret > 0) {
1799 kfree(record);
1800 return 0;
1801 }
1802 return btrfs_qgroup_trace_extent_post(fs_info, record);
1803}
1804
1805int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
1806 struct extent_buffer *eb)
1807{
1808 struct btrfs_fs_info *fs_info = trans->fs_info;
1809 int nr = btrfs_header_nritems(eb);
1810 int i, extent_type, ret;
1811 struct btrfs_key key;
1812 struct btrfs_file_extent_item *fi;
1813 u64 bytenr, num_bytes;
1814
1815 /* We can be called directly from walk_up_proc() */
1816 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
1817 return 0;
1818
1819 for (i = 0; i < nr; i++) {
1820 btrfs_item_key_to_cpu(eb, &key, i);
1821
1822 if (key.type != BTRFS_EXTENT_DATA_KEY)
1823 continue;
1824
1825 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
1826 /* filter out non qgroup-accountable extents */
1827 extent_type = btrfs_file_extent_type(eb, fi);
1828
1829 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
1830 continue;
1831
1832 bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1833 if (!bytenr)
1834 continue;
1835
1836 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1837
1838 ret = btrfs_qgroup_trace_extent(trans, bytenr, num_bytes,
1839 GFP_NOFS);
1840 if (ret)
1841 return ret;
1842 }
1843 cond_resched();
1844 return 0;
1845}
1846
1847/*
1848 * Walk up the tree from the bottom, freeing leaves and any interior
1849 * nodes which have had all slots visited. If a node (leaf or
1850 * interior) is freed, the node above it will have it's slot
1851 * incremented. The root node will never be freed.
1852 *
1853 * At the end of this function, we should have a path which has all
1854 * slots incremented to the next position for a search. If we need to
1855 * read a new node it will be NULL and the node above it will have the
1856 * correct slot selected for a later read.
1857 *
1858 * If we increment the root nodes slot counter past the number of
1859 * elements, 1 is returned to signal completion of the search.
1860 */
1861static int adjust_slots_upwards(struct btrfs_path *path, int root_level)
1862{
1863 int level = 0;
1864 int nr, slot;
1865 struct extent_buffer *eb;
1866
1867 if (root_level == 0)
1868 return 1;
1869
1870 while (level <= root_level) {
1871 eb = path->nodes[level];
1872 nr = btrfs_header_nritems(eb);
1873 path->slots[level]++;
1874 slot = path->slots[level];
1875 if (slot >= nr || level == 0) {
1876 /*
1877 * Don't free the root - we will detect this
1878 * condition after our loop and return a
1879 * positive value for caller to stop walking the tree.
1880 */
1881 if (level != root_level) {
1882 btrfs_tree_unlock_rw(eb, path->locks[level]);
1883 path->locks[level] = 0;
1884
1885 free_extent_buffer(eb);
1886 path->nodes[level] = NULL;
1887 path->slots[level] = 0;
1888 }
1889 } else {
1890 /*
1891 * We have a valid slot to walk back down
1892 * from. Stop here so caller can process these
1893 * new nodes.
1894 */
1895 break;
1896 }
1897
1898 level++;
1899 }
1900
1901 eb = path->nodes[root_level];
1902 if (path->slots[root_level] >= btrfs_header_nritems(eb))
1903 return 1;
1904
1905 return 0;
1906}
1907
David Brazdil0f672f62019-12-10 10:32:29 +00001908/*
1909 * Helper function to trace a subtree tree block swap.
1910 *
1911 * The swap will happen in highest tree block, but there may be a lot of
1912 * tree blocks involved.
1913 *
1914 * For example:
1915 * OO = Old tree blocks
1916 * NN = New tree blocks allocated during balance
1917 *
1918 * File tree (257) Reloc tree for 257
1919 * L2 OO NN
1920 * / \ / \
1921 * L1 OO OO (a) OO NN (a)
1922 * / \ / \ / \ / \
1923 * L0 OO OO OO OO OO OO NN NN
1924 * (b) (c) (b) (c)
1925 *
1926 * When calling qgroup_trace_extent_swap(), we will pass:
1927 * @src_eb = OO(a)
1928 * @dst_path = [ nodes[1] = NN(a), nodes[0] = NN(c) ]
1929 * @dst_level = 0
1930 * @root_level = 1
1931 *
1932 * In that case, qgroup_trace_extent_swap() will search from OO(a) to
1933 * reach OO(c), then mark both OO(c) and NN(c) as qgroup dirty.
1934 *
1935 * The main work of qgroup_trace_extent_swap() can be split into 3 parts:
1936 *
1937 * 1) Tree search from @src_eb
1938 * It should acts as a simplified btrfs_search_slot().
1939 * The key for search can be extracted from @dst_path->nodes[dst_level]
1940 * (first key).
1941 *
1942 * 2) Mark the final tree blocks in @src_path and @dst_path qgroup dirty
1943 * NOTE: In above case, OO(a) and NN(a) won't be marked qgroup dirty.
1944 * They should be marked during previous (@dst_level = 1) iteration.
1945 *
1946 * 3) Mark file extents in leaves dirty
1947 * We don't have good way to pick out new file extents only.
1948 * So we still follow the old method by scanning all file extents in
1949 * the leave.
1950 *
1951 * This function can free us from keeping two paths, thus later we only need
1952 * to care about how to iterate all new tree blocks in reloc tree.
1953 */
1954static int qgroup_trace_extent_swap(struct btrfs_trans_handle* trans,
1955 struct extent_buffer *src_eb,
1956 struct btrfs_path *dst_path,
1957 int dst_level, int root_level,
1958 bool trace_leaf)
1959{
1960 struct btrfs_key key;
1961 struct btrfs_path *src_path;
1962 struct btrfs_fs_info *fs_info = trans->fs_info;
1963 u32 nodesize = fs_info->nodesize;
1964 int cur_level = root_level;
1965 int ret;
1966
1967 BUG_ON(dst_level > root_level);
1968 /* Level mismatch */
1969 if (btrfs_header_level(src_eb) != root_level)
1970 return -EINVAL;
1971
1972 src_path = btrfs_alloc_path();
1973 if (!src_path) {
1974 ret = -ENOMEM;
1975 goto out;
1976 }
1977
1978 if (dst_level)
1979 btrfs_node_key_to_cpu(dst_path->nodes[dst_level], &key, 0);
1980 else
1981 btrfs_item_key_to_cpu(dst_path->nodes[dst_level], &key, 0);
1982
1983 /* For src_path */
Olivier Deprez157378f2022-04-04 15:47:50 +02001984 atomic_inc(&src_eb->refs);
David Brazdil0f672f62019-12-10 10:32:29 +00001985 src_path->nodes[root_level] = src_eb;
1986 src_path->slots[root_level] = dst_path->slots[root_level];
1987 src_path->locks[root_level] = 0;
1988
1989 /* A simplified version of btrfs_search_slot() */
1990 while (cur_level >= dst_level) {
1991 struct btrfs_key src_key;
1992 struct btrfs_key dst_key;
1993
1994 if (src_path->nodes[cur_level] == NULL) {
1995 struct btrfs_key first_key;
1996 struct extent_buffer *eb;
1997 int parent_slot;
1998 u64 child_gen;
1999 u64 child_bytenr;
2000
2001 eb = src_path->nodes[cur_level + 1];
2002 parent_slot = src_path->slots[cur_level + 1];
2003 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
2004 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
2005 btrfs_node_key_to_cpu(eb, &first_key, parent_slot);
2006
2007 eb = read_tree_block(fs_info, child_bytenr, child_gen,
2008 cur_level, &first_key);
2009 if (IS_ERR(eb)) {
2010 ret = PTR_ERR(eb);
2011 goto out;
2012 } else if (!extent_buffer_uptodate(eb)) {
2013 free_extent_buffer(eb);
2014 ret = -EIO;
2015 goto out;
2016 }
2017
2018 src_path->nodes[cur_level] = eb;
2019
2020 btrfs_tree_read_lock(eb);
2021 btrfs_set_lock_blocking_read(eb);
2022 src_path->locks[cur_level] = BTRFS_READ_LOCK_BLOCKING;
2023 }
2024
2025 src_path->slots[cur_level] = dst_path->slots[cur_level];
2026 if (cur_level) {
2027 btrfs_node_key_to_cpu(dst_path->nodes[cur_level],
2028 &dst_key, dst_path->slots[cur_level]);
2029 btrfs_node_key_to_cpu(src_path->nodes[cur_level],
2030 &src_key, src_path->slots[cur_level]);
2031 } else {
2032 btrfs_item_key_to_cpu(dst_path->nodes[cur_level],
2033 &dst_key, dst_path->slots[cur_level]);
2034 btrfs_item_key_to_cpu(src_path->nodes[cur_level],
2035 &src_key, src_path->slots[cur_level]);
2036 }
2037 /* Content mismatch, something went wrong */
2038 if (btrfs_comp_cpu_keys(&dst_key, &src_key)) {
2039 ret = -ENOENT;
2040 goto out;
2041 }
2042 cur_level--;
2043 }
2044
2045 /*
2046 * Now both @dst_path and @src_path have been populated, record the tree
2047 * blocks for qgroup accounting.
2048 */
2049 ret = btrfs_qgroup_trace_extent(trans, src_path->nodes[dst_level]->start,
2050 nodesize, GFP_NOFS);
2051 if (ret < 0)
2052 goto out;
2053 ret = btrfs_qgroup_trace_extent(trans,
2054 dst_path->nodes[dst_level]->start,
2055 nodesize, GFP_NOFS);
2056 if (ret < 0)
2057 goto out;
2058
2059 /* Record leaf file extents */
2060 if (dst_level == 0 && trace_leaf) {
2061 ret = btrfs_qgroup_trace_leaf_items(trans, src_path->nodes[0]);
2062 if (ret < 0)
2063 goto out;
2064 ret = btrfs_qgroup_trace_leaf_items(trans, dst_path->nodes[0]);
2065 }
2066out:
2067 btrfs_free_path(src_path);
2068 return ret;
2069}
2070
2071/*
2072 * Helper function to do recursive generation-aware depth-first search, to
2073 * locate all new tree blocks in a subtree of reloc tree.
2074 *
2075 * E.g. (OO = Old tree blocks, NN = New tree blocks, whose gen == last_snapshot)
2076 * reloc tree
2077 * L2 NN (a)
2078 * / \
2079 * L1 OO NN (b)
2080 * / \ / \
2081 * L0 OO OO OO NN
2082 * (c) (d)
2083 * If we pass:
2084 * @dst_path = [ nodes[1] = NN(b), nodes[0] = NULL ],
2085 * @cur_level = 1
2086 * @root_level = 1
2087 *
2088 * We will iterate through tree blocks NN(b), NN(d) and info qgroup to trace
2089 * above tree blocks along with their counter parts in file tree.
2090 * While during search, old tree blocks OO(c) will be skipped as tree block swap
2091 * won't affect OO(c).
2092 */
2093static int qgroup_trace_new_subtree_blocks(struct btrfs_trans_handle* trans,
2094 struct extent_buffer *src_eb,
2095 struct btrfs_path *dst_path,
2096 int cur_level, int root_level,
2097 u64 last_snapshot, bool trace_leaf)
2098{
2099 struct btrfs_fs_info *fs_info = trans->fs_info;
2100 struct extent_buffer *eb;
2101 bool need_cleanup = false;
2102 int ret = 0;
2103 int i;
2104
2105 /* Level sanity check */
2106 if (cur_level < 0 || cur_level >= BTRFS_MAX_LEVEL - 1 ||
2107 root_level < 0 || root_level >= BTRFS_MAX_LEVEL - 1 ||
2108 root_level < cur_level) {
2109 btrfs_err_rl(fs_info,
2110 "%s: bad levels, cur_level=%d root_level=%d",
2111 __func__, cur_level, root_level);
2112 return -EUCLEAN;
2113 }
2114
2115 /* Read the tree block if needed */
2116 if (dst_path->nodes[cur_level] == NULL) {
2117 struct btrfs_key first_key;
2118 int parent_slot;
2119 u64 child_gen;
2120 u64 child_bytenr;
2121
2122 /*
2123 * dst_path->nodes[root_level] must be initialized before
2124 * calling this function.
2125 */
2126 if (cur_level == root_level) {
2127 btrfs_err_rl(fs_info,
2128 "%s: dst_path->nodes[%d] not initialized, root_level=%d cur_level=%d",
2129 __func__, root_level, root_level, cur_level);
2130 return -EUCLEAN;
2131 }
2132
2133 /*
2134 * We need to get child blockptr/gen from parent before we can
2135 * read it.
2136 */
2137 eb = dst_path->nodes[cur_level + 1];
2138 parent_slot = dst_path->slots[cur_level + 1];
2139 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
2140 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
2141 btrfs_node_key_to_cpu(eb, &first_key, parent_slot);
2142
2143 /* This node is old, no need to trace */
2144 if (child_gen < last_snapshot)
2145 goto out;
2146
2147 eb = read_tree_block(fs_info, child_bytenr, child_gen,
2148 cur_level, &first_key);
2149 if (IS_ERR(eb)) {
2150 ret = PTR_ERR(eb);
2151 goto out;
2152 } else if (!extent_buffer_uptodate(eb)) {
2153 free_extent_buffer(eb);
2154 ret = -EIO;
2155 goto out;
2156 }
2157
2158 dst_path->nodes[cur_level] = eb;
2159 dst_path->slots[cur_level] = 0;
2160
2161 btrfs_tree_read_lock(eb);
2162 btrfs_set_lock_blocking_read(eb);
2163 dst_path->locks[cur_level] = BTRFS_READ_LOCK_BLOCKING;
2164 need_cleanup = true;
2165 }
2166
2167 /* Now record this tree block and its counter part for qgroups */
2168 ret = qgroup_trace_extent_swap(trans, src_eb, dst_path, cur_level,
2169 root_level, trace_leaf);
2170 if (ret < 0)
2171 goto cleanup;
2172
2173 eb = dst_path->nodes[cur_level];
2174
2175 if (cur_level > 0) {
2176 /* Iterate all child tree blocks */
2177 for (i = 0; i < btrfs_header_nritems(eb); i++) {
2178 /* Skip old tree blocks as they won't be swapped */
2179 if (btrfs_node_ptr_generation(eb, i) < last_snapshot)
2180 continue;
2181 dst_path->slots[cur_level] = i;
2182
2183 /* Recursive call (at most 7 times) */
2184 ret = qgroup_trace_new_subtree_blocks(trans, src_eb,
2185 dst_path, cur_level - 1, root_level,
2186 last_snapshot, trace_leaf);
2187 if (ret < 0)
2188 goto cleanup;
2189 }
2190 }
2191
2192cleanup:
2193 if (need_cleanup) {
2194 /* Clean up */
2195 btrfs_tree_unlock_rw(dst_path->nodes[cur_level],
2196 dst_path->locks[cur_level]);
2197 free_extent_buffer(dst_path->nodes[cur_level]);
2198 dst_path->nodes[cur_level] = NULL;
2199 dst_path->slots[cur_level] = 0;
2200 dst_path->locks[cur_level] = 0;
2201 }
2202out:
2203 return ret;
2204}
2205
2206static int qgroup_trace_subtree_swap(struct btrfs_trans_handle *trans,
2207 struct extent_buffer *src_eb,
2208 struct extent_buffer *dst_eb,
2209 u64 last_snapshot, bool trace_leaf)
2210{
2211 struct btrfs_fs_info *fs_info = trans->fs_info;
2212 struct btrfs_path *dst_path = NULL;
2213 int level;
2214 int ret;
2215
2216 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2217 return 0;
2218
2219 /* Wrong parameter order */
2220 if (btrfs_header_generation(src_eb) > btrfs_header_generation(dst_eb)) {
2221 btrfs_err_rl(fs_info,
2222 "%s: bad parameter order, src_gen=%llu dst_gen=%llu", __func__,
2223 btrfs_header_generation(src_eb),
2224 btrfs_header_generation(dst_eb));
2225 return -EUCLEAN;
2226 }
2227
2228 if (!extent_buffer_uptodate(src_eb) || !extent_buffer_uptodate(dst_eb)) {
2229 ret = -EIO;
2230 goto out;
2231 }
2232
2233 level = btrfs_header_level(dst_eb);
2234 dst_path = btrfs_alloc_path();
2235 if (!dst_path) {
2236 ret = -ENOMEM;
2237 goto out;
2238 }
2239 /* For dst_path */
Olivier Deprez157378f2022-04-04 15:47:50 +02002240 atomic_inc(&dst_eb->refs);
David Brazdil0f672f62019-12-10 10:32:29 +00002241 dst_path->nodes[level] = dst_eb;
2242 dst_path->slots[level] = 0;
2243 dst_path->locks[level] = 0;
2244
2245 /* Do the generation aware breadth-first search */
2246 ret = qgroup_trace_new_subtree_blocks(trans, src_eb, dst_path, level,
2247 level, last_snapshot, trace_leaf);
2248 if (ret < 0)
2249 goto out;
2250 ret = 0;
2251
2252out:
2253 btrfs_free_path(dst_path);
2254 if (ret < 0)
2255 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2256 return ret;
2257}
2258
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002259int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
2260 struct extent_buffer *root_eb,
2261 u64 root_gen, int root_level)
2262{
2263 struct btrfs_fs_info *fs_info = trans->fs_info;
2264 int ret = 0;
2265 int level;
2266 struct extent_buffer *eb = root_eb;
2267 struct btrfs_path *path = NULL;
2268
2269 BUG_ON(root_level < 0 || root_level >= BTRFS_MAX_LEVEL);
2270 BUG_ON(root_eb == NULL);
2271
2272 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2273 return 0;
2274
2275 if (!extent_buffer_uptodate(root_eb)) {
2276 ret = btrfs_read_buffer(root_eb, root_gen, root_level, NULL);
2277 if (ret)
2278 goto out;
2279 }
2280
2281 if (root_level == 0) {
2282 ret = btrfs_qgroup_trace_leaf_items(trans, root_eb);
2283 goto out;
2284 }
2285
2286 path = btrfs_alloc_path();
2287 if (!path)
2288 return -ENOMEM;
2289
2290 /*
2291 * Walk down the tree. Missing extent blocks are filled in as
2292 * we go. Metadata is accounted every time we read a new
2293 * extent block.
2294 *
2295 * When we reach a leaf, we account for file extent items in it,
2296 * walk back up the tree (adjusting slot pointers as we go)
2297 * and restart the search process.
2298 */
Olivier Deprez157378f2022-04-04 15:47:50 +02002299 atomic_inc(&root_eb->refs); /* For path */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002300 path->nodes[root_level] = root_eb;
2301 path->slots[root_level] = 0;
2302 path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
2303walk_down:
2304 level = root_level;
2305 while (level >= 0) {
2306 if (path->nodes[level] == NULL) {
2307 struct btrfs_key first_key;
2308 int parent_slot;
2309 u64 child_gen;
2310 u64 child_bytenr;
2311
2312 /*
2313 * We need to get child blockptr/gen from parent before
2314 * we can read it.
2315 */
2316 eb = path->nodes[level + 1];
2317 parent_slot = path->slots[level + 1];
2318 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
2319 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
2320 btrfs_node_key_to_cpu(eb, &first_key, parent_slot);
2321
2322 eb = read_tree_block(fs_info, child_bytenr, child_gen,
2323 level, &first_key);
2324 if (IS_ERR(eb)) {
2325 ret = PTR_ERR(eb);
2326 goto out;
2327 } else if (!extent_buffer_uptodate(eb)) {
2328 free_extent_buffer(eb);
2329 ret = -EIO;
2330 goto out;
2331 }
2332
2333 path->nodes[level] = eb;
2334 path->slots[level] = 0;
2335
2336 btrfs_tree_read_lock(eb);
David Brazdil0f672f62019-12-10 10:32:29 +00002337 btrfs_set_lock_blocking_read(eb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002338 path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
2339
2340 ret = btrfs_qgroup_trace_extent(trans, child_bytenr,
2341 fs_info->nodesize,
2342 GFP_NOFS);
2343 if (ret)
2344 goto out;
2345 }
2346
2347 if (level == 0) {
2348 ret = btrfs_qgroup_trace_leaf_items(trans,
2349 path->nodes[level]);
2350 if (ret)
2351 goto out;
2352
2353 /* Nonzero return here means we completed our search */
2354 ret = adjust_slots_upwards(path, root_level);
2355 if (ret)
2356 break;
2357
2358 /* Restart search with new slots */
2359 goto walk_down;
2360 }
2361
2362 level--;
2363 }
2364
2365 ret = 0;
2366out:
2367 btrfs_free_path(path);
2368
2369 return ret;
2370}
2371
2372#define UPDATE_NEW 0
2373#define UPDATE_OLD 1
2374/*
2375 * Walk all of the roots that points to the bytenr and adjust their refcnts.
2376 */
2377static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
2378 struct ulist *roots, struct ulist *tmp,
2379 struct ulist *qgroups, u64 seq, int update_old)
2380{
2381 struct ulist_node *unode;
2382 struct ulist_iterator uiter;
2383 struct ulist_node *tmp_unode;
2384 struct ulist_iterator tmp_uiter;
2385 struct btrfs_qgroup *qg;
2386 int ret = 0;
2387
2388 if (!roots)
2389 return 0;
2390 ULIST_ITER_INIT(&uiter);
2391 while ((unode = ulist_next(roots, &uiter))) {
2392 qg = find_qgroup_rb(fs_info, unode->val);
2393 if (!qg)
2394 continue;
2395
2396 ulist_reinit(tmp);
2397 ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg),
2398 GFP_ATOMIC);
2399 if (ret < 0)
2400 return ret;
2401 ret = ulist_add(tmp, qg->qgroupid, qgroup_to_aux(qg), GFP_ATOMIC);
2402 if (ret < 0)
2403 return ret;
2404 ULIST_ITER_INIT(&tmp_uiter);
2405 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
2406 struct btrfs_qgroup_list *glist;
2407
2408 qg = unode_aux_to_qgroup(tmp_unode);
2409 if (update_old)
2410 btrfs_qgroup_update_old_refcnt(qg, seq, 1);
2411 else
2412 btrfs_qgroup_update_new_refcnt(qg, seq, 1);
2413 list_for_each_entry(glist, &qg->groups, next_group) {
2414 ret = ulist_add(qgroups, glist->group->qgroupid,
2415 qgroup_to_aux(glist->group),
2416 GFP_ATOMIC);
2417 if (ret < 0)
2418 return ret;
2419 ret = ulist_add(tmp, glist->group->qgroupid,
2420 qgroup_to_aux(glist->group),
2421 GFP_ATOMIC);
2422 if (ret < 0)
2423 return ret;
2424 }
2425 }
2426 }
2427 return 0;
2428}
2429
2430/*
2431 * Update qgroup rfer/excl counters.
2432 * Rfer update is easy, codes can explain themselves.
2433 *
Olivier Deprez0e641232021-09-23 10:07:05 +02002434 * Excl update is tricky, the update is split into 2 parts.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002435 * Part 1: Possible exclusive <-> sharing detect:
2436 * | A | !A |
2437 * -------------------------------------
2438 * B | * | - |
2439 * -------------------------------------
2440 * !B | + | ** |
2441 * -------------------------------------
2442 *
2443 * Conditions:
2444 * A: cur_old_roots < nr_old_roots (not exclusive before)
2445 * !A: cur_old_roots == nr_old_roots (possible exclusive before)
2446 * B: cur_new_roots < nr_new_roots (not exclusive now)
2447 * !B: cur_new_roots == nr_new_roots (possible exclusive now)
2448 *
2449 * Results:
2450 * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
2451 * *: Definitely not changed. **: Possible unchanged.
2452 *
2453 * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
2454 *
2455 * To make the logic clear, we first use condition A and B to split
2456 * combination into 4 results.
2457 *
2458 * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
2459 * only on variant maybe 0.
2460 *
2461 * Lastly, check result **, since there are 2 variants maybe 0, split them
2462 * again(2x2).
2463 * But this time we don't need to consider other things, the codes and logic
2464 * is easy to understand now.
2465 */
2466static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
2467 struct ulist *qgroups,
2468 u64 nr_old_roots,
2469 u64 nr_new_roots,
2470 u64 num_bytes, u64 seq)
2471{
2472 struct ulist_node *unode;
2473 struct ulist_iterator uiter;
2474 struct btrfs_qgroup *qg;
2475 u64 cur_new_count, cur_old_count;
2476
2477 ULIST_ITER_INIT(&uiter);
2478 while ((unode = ulist_next(qgroups, &uiter))) {
2479 bool dirty = false;
2480
2481 qg = unode_aux_to_qgroup(unode);
2482 cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
2483 cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
2484
2485 trace_qgroup_update_counters(fs_info, qg, cur_old_count,
2486 cur_new_count);
2487
2488 /* Rfer update part */
2489 if (cur_old_count == 0 && cur_new_count > 0) {
2490 qg->rfer += num_bytes;
2491 qg->rfer_cmpr += num_bytes;
2492 dirty = true;
2493 }
2494 if (cur_old_count > 0 && cur_new_count == 0) {
2495 qg->rfer -= num_bytes;
2496 qg->rfer_cmpr -= num_bytes;
2497 dirty = true;
2498 }
2499
2500 /* Excl update part */
2501 /* Exclusive/none -> shared case */
2502 if (cur_old_count == nr_old_roots &&
2503 cur_new_count < nr_new_roots) {
2504 /* Exclusive -> shared */
2505 if (cur_old_count != 0) {
2506 qg->excl -= num_bytes;
2507 qg->excl_cmpr -= num_bytes;
2508 dirty = true;
2509 }
2510 }
2511
2512 /* Shared -> exclusive/none case */
2513 if (cur_old_count < nr_old_roots &&
2514 cur_new_count == nr_new_roots) {
2515 /* Shared->exclusive */
2516 if (cur_new_count != 0) {
2517 qg->excl += num_bytes;
2518 qg->excl_cmpr += num_bytes;
2519 dirty = true;
2520 }
2521 }
2522
2523 /* Exclusive/none -> exclusive/none case */
2524 if (cur_old_count == nr_old_roots &&
2525 cur_new_count == nr_new_roots) {
2526 if (cur_old_count == 0) {
2527 /* None -> exclusive/none */
2528
2529 if (cur_new_count != 0) {
2530 /* None -> exclusive */
2531 qg->excl += num_bytes;
2532 qg->excl_cmpr += num_bytes;
2533 dirty = true;
2534 }
2535 /* None -> none, nothing changed */
2536 } else {
2537 /* Exclusive -> exclusive/none */
2538
2539 if (cur_new_count == 0) {
2540 /* Exclusive -> none */
2541 qg->excl -= num_bytes;
2542 qg->excl_cmpr -= num_bytes;
2543 dirty = true;
2544 }
2545 /* Exclusive -> exclusive, nothing changed */
2546 }
2547 }
2548
2549 if (dirty)
2550 qgroup_dirty(fs_info, qg);
2551 }
2552 return 0;
2553}
2554
2555/*
2556 * Check if the @roots potentially is a list of fs tree roots
2557 *
2558 * Return 0 for definitely not a fs/subvol tree roots ulist
2559 * Return 1 for possible fs/subvol tree roots in the list (considering an empty
2560 * one as well)
2561 */
2562static int maybe_fs_roots(struct ulist *roots)
2563{
2564 struct ulist_node *unode;
2565 struct ulist_iterator uiter;
2566
2567 /* Empty one, still possible for fs roots */
2568 if (!roots || roots->nnodes == 0)
2569 return 1;
2570
2571 ULIST_ITER_INIT(&uiter);
2572 unode = ulist_next(roots, &uiter);
2573 if (!unode)
2574 return 1;
2575
2576 /*
2577 * If it contains fs tree roots, then it must belong to fs/subvol
2578 * trees.
2579 * If it contains a non-fs tree, it won't be shared with fs/subvol trees.
2580 */
2581 return is_fstree(unode->val);
2582}
2583
2584int btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans, u64 bytenr,
2585 u64 num_bytes, struct ulist *old_roots,
2586 struct ulist *new_roots)
2587{
2588 struct btrfs_fs_info *fs_info = trans->fs_info;
2589 struct ulist *qgroups = NULL;
2590 struct ulist *tmp = NULL;
2591 u64 seq;
2592 u64 nr_new_roots = 0;
2593 u64 nr_old_roots = 0;
2594 int ret = 0;
2595
Olivier Deprez0e641232021-09-23 10:07:05 +02002596 /*
2597 * If quotas get disabled meanwhile, the resouces need to be freed and
2598 * we can't just exit here.
2599 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002600 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Olivier Deprez0e641232021-09-23 10:07:05 +02002601 goto out_free;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002602
2603 if (new_roots) {
2604 if (!maybe_fs_roots(new_roots))
2605 goto out_free;
2606 nr_new_roots = new_roots->nnodes;
2607 }
2608 if (old_roots) {
2609 if (!maybe_fs_roots(old_roots))
2610 goto out_free;
2611 nr_old_roots = old_roots->nnodes;
2612 }
2613
2614 /* Quick exit, either not fs tree roots, or won't affect any qgroup */
2615 if (nr_old_roots == 0 && nr_new_roots == 0)
2616 goto out_free;
2617
2618 BUG_ON(!fs_info->quota_root);
2619
2620 trace_btrfs_qgroup_account_extent(fs_info, trans->transid, bytenr,
2621 num_bytes, nr_old_roots, nr_new_roots);
2622
2623 qgroups = ulist_alloc(GFP_NOFS);
2624 if (!qgroups) {
2625 ret = -ENOMEM;
2626 goto out_free;
2627 }
2628 tmp = ulist_alloc(GFP_NOFS);
2629 if (!tmp) {
2630 ret = -ENOMEM;
2631 goto out_free;
2632 }
2633
2634 mutex_lock(&fs_info->qgroup_rescan_lock);
2635 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
2636 if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
2637 mutex_unlock(&fs_info->qgroup_rescan_lock);
2638 ret = 0;
2639 goto out_free;
2640 }
2641 }
2642 mutex_unlock(&fs_info->qgroup_rescan_lock);
2643
2644 spin_lock(&fs_info->qgroup_lock);
2645 seq = fs_info->qgroup_seq;
2646
2647 /* Update old refcnts using old_roots */
2648 ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
2649 UPDATE_OLD);
2650 if (ret < 0)
2651 goto out;
2652
2653 /* Update new refcnts using new_roots */
2654 ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
2655 UPDATE_NEW);
2656 if (ret < 0)
2657 goto out;
2658
2659 qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
2660 num_bytes, seq);
2661
2662 /*
2663 * Bump qgroup_seq to avoid seq overlap
2664 */
2665 fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
2666out:
2667 spin_unlock(&fs_info->qgroup_lock);
2668out_free:
2669 ulist_free(tmp);
2670 ulist_free(qgroups);
2671 ulist_free(old_roots);
2672 ulist_free(new_roots);
2673 return ret;
2674}
2675
2676int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans)
2677{
2678 struct btrfs_fs_info *fs_info = trans->fs_info;
2679 struct btrfs_qgroup_extent_record *record;
2680 struct btrfs_delayed_ref_root *delayed_refs;
2681 struct ulist *new_roots = NULL;
2682 struct rb_node *node;
David Brazdil0f672f62019-12-10 10:32:29 +00002683 u64 num_dirty_extents = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002684 u64 qgroup_to_skip;
2685 int ret = 0;
2686
2687 delayed_refs = &trans->transaction->delayed_refs;
2688 qgroup_to_skip = delayed_refs->qgroup_to_skip;
2689 while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
2690 record = rb_entry(node, struct btrfs_qgroup_extent_record,
2691 node);
2692
David Brazdil0f672f62019-12-10 10:32:29 +00002693 num_dirty_extents++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002694 trace_btrfs_qgroup_account_extents(fs_info, record);
2695
2696 if (!ret) {
2697 /*
2698 * Old roots should be searched when inserting qgroup
2699 * extent record
2700 */
2701 if (WARN_ON(!record->old_roots)) {
2702 /* Search commit root to find old_roots */
2703 ret = btrfs_find_all_roots(NULL, fs_info,
2704 record->bytenr, 0,
2705 &record->old_roots, false);
2706 if (ret < 0)
2707 goto cleanup;
2708 }
2709
David Brazdil0f672f62019-12-10 10:32:29 +00002710 /* Free the reserved data space */
2711 btrfs_qgroup_free_refroot(fs_info,
2712 record->data_rsv_refroot,
2713 record->data_rsv,
2714 BTRFS_QGROUP_RSV_DATA);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002715 /*
2716 * Use SEQ_LAST as time_seq to do special search, which
2717 * doesn't lock tree or delayed_refs and search current
2718 * root. It's safe inside commit_transaction().
2719 */
2720 ret = btrfs_find_all_roots(trans, fs_info,
2721 record->bytenr, SEQ_LAST, &new_roots, false);
2722 if (ret < 0)
2723 goto cleanup;
2724 if (qgroup_to_skip) {
2725 ulist_del(new_roots, qgroup_to_skip, 0);
2726 ulist_del(record->old_roots, qgroup_to_skip,
2727 0);
2728 }
2729 ret = btrfs_qgroup_account_extent(trans, record->bytenr,
2730 record->num_bytes,
2731 record->old_roots,
2732 new_roots);
2733 record->old_roots = NULL;
2734 new_roots = NULL;
2735 }
2736cleanup:
2737 ulist_free(record->old_roots);
2738 ulist_free(new_roots);
2739 new_roots = NULL;
2740 rb_erase(node, &delayed_refs->dirty_extent_root);
2741 kfree(record);
2742
2743 }
David Brazdil0f672f62019-12-10 10:32:29 +00002744 trace_qgroup_num_dirty_extents(fs_info, trans->transid,
2745 num_dirty_extents);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002746 return ret;
2747}
2748
2749/*
2750 * called from commit_transaction. Writes all changed qgroups to disk.
2751 */
2752int btrfs_run_qgroups(struct btrfs_trans_handle *trans)
2753{
2754 struct btrfs_fs_info *fs_info = trans->fs_info;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002755 int ret = 0;
2756
Olivier Deprez157378f2022-04-04 15:47:50 +02002757 if (!fs_info->quota_root)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002758 return ret;
2759
2760 spin_lock(&fs_info->qgroup_lock);
2761 while (!list_empty(&fs_info->dirty_qgroups)) {
2762 struct btrfs_qgroup *qgroup;
2763 qgroup = list_first_entry(&fs_info->dirty_qgroups,
2764 struct btrfs_qgroup, dirty);
2765 list_del_init(&qgroup->dirty);
2766 spin_unlock(&fs_info->qgroup_lock);
2767 ret = update_qgroup_info_item(trans, qgroup);
2768 if (ret)
2769 fs_info->qgroup_flags |=
2770 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2771 ret = update_qgroup_limit_item(trans, qgroup);
2772 if (ret)
2773 fs_info->qgroup_flags |=
2774 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2775 spin_lock(&fs_info->qgroup_lock);
2776 }
2777 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2778 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
2779 else
2780 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
2781 spin_unlock(&fs_info->qgroup_lock);
2782
2783 ret = update_qgroup_status_item(trans);
2784 if (ret)
2785 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2786
2787 return ret;
2788}
2789
2790/*
2791 * Copy the accounting information between qgroups. This is necessary
2792 * when a snapshot or a subvolume is created. Throwing an error will
2793 * cause a transaction abort so we take extra care here to only error
2794 * when a readonly fs is a reasonable outcome.
2795 */
2796int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
2797 u64 objectid, struct btrfs_qgroup_inherit *inherit)
2798{
2799 int ret = 0;
2800 int i;
2801 u64 *i_qgroups;
David Brazdil0f672f62019-12-10 10:32:29 +00002802 bool committing = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002803 struct btrfs_fs_info *fs_info = trans->fs_info;
2804 struct btrfs_root *quota_root;
2805 struct btrfs_qgroup *srcgroup;
2806 struct btrfs_qgroup *dstgroup;
Olivier Deprez0e641232021-09-23 10:07:05 +02002807 bool need_rescan = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002808 u32 level_size = 0;
2809 u64 nums;
2810
David Brazdil0f672f62019-12-10 10:32:29 +00002811 /*
2812 * There are only two callers of this function.
2813 *
2814 * One in create_subvol() in the ioctl context, which needs to hold
2815 * the qgroup_ioctl_lock.
2816 *
2817 * The other one in create_pending_snapshot() where no other qgroup
2818 * code can modify the fs as they all need to either start a new trans
2819 * or hold a trans handler, thus we don't need to hold
2820 * qgroup_ioctl_lock.
2821 * This would avoid long and complex lock chain and make lockdep happy.
2822 */
2823 spin_lock(&fs_info->trans_lock);
2824 if (trans->transaction->state == TRANS_STATE_COMMIT_DOING)
2825 committing = true;
2826 spin_unlock(&fs_info->trans_lock);
2827
2828 if (!committing)
2829 mutex_lock(&fs_info->qgroup_ioctl_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002830 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2831 goto out;
2832
2833 quota_root = fs_info->quota_root;
2834 if (!quota_root) {
2835 ret = -EINVAL;
2836 goto out;
2837 }
2838
2839 if (inherit) {
2840 i_qgroups = (u64 *)(inherit + 1);
2841 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
2842 2 * inherit->num_excl_copies;
2843 for (i = 0; i < nums; ++i) {
2844 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
2845
2846 /*
2847 * Zero out invalid groups so we can ignore
2848 * them later.
2849 */
2850 if (!srcgroup ||
2851 ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
2852 *i_qgroups = 0ULL;
2853
2854 ++i_qgroups;
2855 }
2856 }
2857
2858 /*
2859 * create a tracking group for the subvol itself
2860 */
2861 ret = add_qgroup_item(trans, quota_root, objectid);
2862 if (ret)
2863 goto out;
2864
2865 /*
2866 * add qgroup to all inherited groups
2867 */
2868 if (inherit) {
2869 i_qgroups = (u64 *)(inherit + 1);
2870 for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) {
2871 if (*i_qgroups == 0)
2872 continue;
2873 ret = add_qgroup_relation_item(trans, objectid,
2874 *i_qgroups);
2875 if (ret && ret != -EEXIST)
2876 goto out;
2877 ret = add_qgroup_relation_item(trans, *i_qgroups,
2878 objectid);
2879 if (ret && ret != -EEXIST)
2880 goto out;
2881 }
2882 ret = 0;
2883 }
2884
2885
2886 spin_lock(&fs_info->qgroup_lock);
2887
2888 dstgroup = add_qgroup_rb(fs_info, objectid);
2889 if (IS_ERR(dstgroup)) {
2890 ret = PTR_ERR(dstgroup);
2891 goto unlock;
2892 }
2893
2894 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
2895 dstgroup->lim_flags = inherit->lim.flags;
2896 dstgroup->max_rfer = inherit->lim.max_rfer;
2897 dstgroup->max_excl = inherit->lim.max_excl;
2898 dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
2899 dstgroup->rsv_excl = inherit->lim.rsv_excl;
2900
2901 ret = update_qgroup_limit_item(trans, dstgroup);
2902 if (ret) {
2903 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2904 btrfs_info(fs_info,
2905 "unable to update quota limit for %llu",
2906 dstgroup->qgroupid);
2907 goto unlock;
2908 }
2909 }
2910
2911 if (srcid) {
2912 srcgroup = find_qgroup_rb(fs_info, srcid);
2913 if (!srcgroup)
2914 goto unlock;
2915
2916 /*
2917 * We call inherit after we clone the root in order to make sure
2918 * our counts don't go crazy, so at this point the only
2919 * difference between the two roots should be the root node.
2920 */
2921 level_size = fs_info->nodesize;
2922 dstgroup->rfer = srcgroup->rfer;
2923 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
2924 dstgroup->excl = level_size;
2925 dstgroup->excl_cmpr = level_size;
2926 srcgroup->excl = level_size;
2927 srcgroup->excl_cmpr = level_size;
2928
2929 /* inherit the limit info */
2930 dstgroup->lim_flags = srcgroup->lim_flags;
2931 dstgroup->max_rfer = srcgroup->max_rfer;
2932 dstgroup->max_excl = srcgroup->max_excl;
2933 dstgroup->rsv_rfer = srcgroup->rsv_rfer;
2934 dstgroup->rsv_excl = srcgroup->rsv_excl;
2935
2936 qgroup_dirty(fs_info, dstgroup);
2937 qgroup_dirty(fs_info, srcgroup);
2938 }
2939
2940 if (!inherit)
2941 goto unlock;
2942
2943 i_qgroups = (u64 *)(inherit + 1);
2944 for (i = 0; i < inherit->num_qgroups; ++i) {
2945 if (*i_qgroups) {
2946 ret = add_relation_rb(fs_info, objectid, *i_qgroups);
2947 if (ret)
2948 goto unlock;
2949 }
2950 ++i_qgroups;
Olivier Deprez0e641232021-09-23 10:07:05 +02002951
2952 /*
2953 * If we're doing a snapshot, and adding the snapshot to a new
2954 * qgroup, the numbers are guaranteed to be incorrect.
2955 */
2956 if (srcid)
2957 need_rescan = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002958 }
2959
2960 for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {
2961 struct btrfs_qgroup *src;
2962 struct btrfs_qgroup *dst;
2963
2964 if (!i_qgroups[0] || !i_qgroups[1])
2965 continue;
2966
2967 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2968 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2969
2970 if (!src || !dst) {
2971 ret = -EINVAL;
2972 goto unlock;
2973 }
2974
2975 dst->rfer = src->rfer - level_size;
2976 dst->rfer_cmpr = src->rfer_cmpr - level_size;
Olivier Deprez0e641232021-09-23 10:07:05 +02002977
2978 /* Manually tweaking numbers certainly needs a rescan */
2979 need_rescan = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002980 }
2981 for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {
2982 struct btrfs_qgroup *src;
2983 struct btrfs_qgroup *dst;
2984
2985 if (!i_qgroups[0] || !i_qgroups[1])
2986 continue;
2987
2988 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2989 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2990
2991 if (!src || !dst) {
2992 ret = -EINVAL;
2993 goto unlock;
2994 }
2995
2996 dst->excl = src->excl + level_size;
2997 dst->excl_cmpr = src->excl_cmpr + level_size;
Olivier Deprez0e641232021-09-23 10:07:05 +02002998 need_rescan = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002999 }
3000
3001unlock:
3002 spin_unlock(&fs_info->qgroup_lock);
Olivier Deprez157378f2022-04-04 15:47:50 +02003003 if (!ret)
3004 ret = btrfs_sysfs_add_one_qgroup(fs_info, dstgroup);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003005out:
David Brazdil0f672f62019-12-10 10:32:29 +00003006 if (!committing)
3007 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Olivier Deprez0e641232021-09-23 10:07:05 +02003008 if (need_rescan)
3009 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003010 return ret;
3011}
3012
Olivier Deprez0e641232021-09-23 10:07:05 +02003013static bool qgroup_check_limits(const struct btrfs_qgroup *qg, u64 num_bytes)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003014{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003015 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
3016 qgroup_rsv_total(qg) + (s64)qg->rfer + num_bytes > qg->max_rfer)
3017 return false;
3018
3019 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
3020 qgroup_rsv_total(qg) + (s64)qg->excl + num_bytes > qg->max_excl)
3021 return false;
3022
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003023 return true;
3024}
3025
3026static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce,
3027 enum btrfs_qgroup_rsv_type type)
3028{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003029 struct btrfs_qgroup *qgroup;
3030 struct btrfs_fs_info *fs_info = root->fs_info;
3031 u64 ref_root = root->root_key.objectid;
3032 int ret = 0;
3033 struct ulist_node *unode;
3034 struct ulist_iterator uiter;
3035
3036 if (!is_fstree(ref_root))
3037 return 0;
3038
3039 if (num_bytes == 0)
3040 return 0;
3041
3042 if (test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags) &&
3043 capable(CAP_SYS_RESOURCE))
3044 enforce = false;
3045
3046 spin_lock(&fs_info->qgroup_lock);
Olivier Deprez157378f2022-04-04 15:47:50 +02003047 if (!fs_info->quota_root)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003048 goto out;
3049
3050 qgroup = find_qgroup_rb(fs_info, ref_root);
3051 if (!qgroup)
3052 goto out;
3053
3054 /*
3055 * in a first step, we check all affected qgroups if any limits would
3056 * be exceeded
3057 */
3058 ulist_reinit(fs_info->qgroup_ulist);
3059 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
3060 qgroup_to_aux(qgroup), GFP_ATOMIC);
3061 if (ret < 0)
3062 goto out;
3063 ULIST_ITER_INIT(&uiter);
3064 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
3065 struct btrfs_qgroup *qg;
3066 struct btrfs_qgroup_list *glist;
3067
3068 qg = unode_aux_to_qgroup(unode);
3069
Olivier Deprez0e641232021-09-23 10:07:05 +02003070 if (enforce && !qgroup_check_limits(qg, num_bytes)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003071 ret = -EDQUOT;
3072 goto out;
3073 }
3074
3075 list_for_each_entry(glist, &qg->groups, next_group) {
3076 ret = ulist_add(fs_info->qgroup_ulist,
3077 glist->group->qgroupid,
3078 qgroup_to_aux(glist->group), GFP_ATOMIC);
3079 if (ret < 0)
3080 goto out;
3081 }
3082 }
3083 ret = 0;
3084 /*
3085 * no limits exceeded, now record the reservation into all qgroups
3086 */
3087 ULIST_ITER_INIT(&uiter);
3088 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
3089 struct btrfs_qgroup *qg;
3090
3091 qg = unode_aux_to_qgroup(unode);
3092
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003093 qgroup_rsv_add(fs_info, qg, num_bytes, type);
3094 }
3095
3096out:
3097 spin_unlock(&fs_info->qgroup_lock);
3098 return ret;
3099}
3100
3101/*
3102 * Free @num_bytes of reserved space with @type for qgroup. (Normally level 0
3103 * qgroup).
3104 *
3105 * Will handle all higher level qgroup too.
3106 *
3107 * NOTE: If @num_bytes is (u64)-1, this means to free all bytes of this qgroup.
3108 * This special case is only used for META_PERTRANS type.
3109 */
3110void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
3111 u64 ref_root, u64 num_bytes,
3112 enum btrfs_qgroup_rsv_type type)
3113{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003114 struct btrfs_qgroup *qgroup;
3115 struct ulist_node *unode;
3116 struct ulist_iterator uiter;
3117 int ret = 0;
3118
3119 if (!is_fstree(ref_root))
3120 return;
3121
3122 if (num_bytes == 0)
3123 return;
3124
3125 if (num_bytes == (u64)-1 && type != BTRFS_QGROUP_RSV_META_PERTRANS) {
3126 WARN(1, "%s: Invalid type to free", __func__);
3127 return;
3128 }
3129 spin_lock(&fs_info->qgroup_lock);
3130
Olivier Deprez157378f2022-04-04 15:47:50 +02003131 if (!fs_info->quota_root)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003132 goto out;
3133
3134 qgroup = find_qgroup_rb(fs_info, ref_root);
3135 if (!qgroup)
3136 goto out;
3137
3138 if (num_bytes == (u64)-1)
3139 /*
3140 * We're freeing all pertrans rsv, get reserved value from
3141 * level 0 qgroup as real num_bytes to free.
3142 */
3143 num_bytes = qgroup->rsv.values[type];
3144
3145 ulist_reinit(fs_info->qgroup_ulist);
3146 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
3147 qgroup_to_aux(qgroup), GFP_ATOMIC);
3148 if (ret < 0)
3149 goto out;
3150 ULIST_ITER_INIT(&uiter);
3151 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
3152 struct btrfs_qgroup *qg;
3153 struct btrfs_qgroup_list *glist;
3154
3155 qg = unode_aux_to_qgroup(unode);
3156
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003157 qgroup_rsv_release(fs_info, qg, num_bytes, type);
3158
3159 list_for_each_entry(glist, &qg->groups, next_group) {
3160 ret = ulist_add(fs_info->qgroup_ulist,
3161 glist->group->qgroupid,
3162 qgroup_to_aux(glist->group), GFP_ATOMIC);
3163 if (ret < 0)
3164 goto out;
3165 }
3166 }
3167
3168out:
3169 spin_unlock(&fs_info->qgroup_lock);
3170}
3171
3172/*
3173 * Check if the leaf is the last leaf. Which means all node pointers
3174 * are at their last position.
3175 */
3176static bool is_last_leaf(struct btrfs_path *path)
3177{
3178 int i;
3179
3180 for (i = 1; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
3181 if (path->slots[i] != btrfs_header_nritems(path->nodes[i]) - 1)
3182 return false;
3183 }
3184 return true;
3185}
3186
3187/*
3188 * returns < 0 on error, 0 when more leafs are to be scanned.
3189 * returns 1 when done.
3190 */
3191static int qgroup_rescan_leaf(struct btrfs_trans_handle *trans,
3192 struct btrfs_path *path)
3193{
3194 struct btrfs_fs_info *fs_info = trans->fs_info;
3195 struct btrfs_key found;
3196 struct extent_buffer *scratch_leaf = NULL;
3197 struct ulist *roots = NULL;
3198 u64 num_bytes;
3199 bool done;
3200 int slot;
3201 int ret;
3202
3203 mutex_lock(&fs_info->qgroup_rescan_lock);
3204 ret = btrfs_search_slot_for_read(fs_info->extent_root,
3205 &fs_info->qgroup_rescan_progress,
3206 path, 1, 0);
3207
3208 btrfs_debug(fs_info,
3209 "current progress key (%llu %u %llu), search_slot ret %d",
3210 fs_info->qgroup_rescan_progress.objectid,
3211 fs_info->qgroup_rescan_progress.type,
3212 fs_info->qgroup_rescan_progress.offset, ret);
3213
3214 if (ret) {
3215 /*
3216 * The rescan is about to end, we will not be scanning any
3217 * further blocks. We cannot unset the RESCAN flag here, because
3218 * we want to commit the transaction if everything went well.
3219 * To make the live accounting work in this phase, we set our
3220 * scan progress pointer such that every real extent objectid
3221 * will be smaller.
3222 */
3223 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
3224 btrfs_release_path(path);
3225 mutex_unlock(&fs_info->qgroup_rescan_lock);
3226 return ret;
3227 }
3228 done = is_last_leaf(path);
3229
3230 btrfs_item_key_to_cpu(path->nodes[0], &found,
3231 btrfs_header_nritems(path->nodes[0]) - 1);
3232 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
3233
3234 scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
3235 if (!scratch_leaf) {
3236 ret = -ENOMEM;
3237 mutex_unlock(&fs_info->qgroup_rescan_lock);
3238 goto out;
3239 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003240 slot = path->slots[0];
3241 btrfs_release_path(path);
3242 mutex_unlock(&fs_info->qgroup_rescan_lock);
3243
3244 for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
3245 btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
3246 if (found.type != BTRFS_EXTENT_ITEM_KEY &&
3247 found.type != BTRFS_METADATA_ITEM_KEY)
3248 continue;
3249 if (found.type == BTRFS_METADATA_ITEM_KEY)
3250 num_bytes = fs_info->nodesize;
3251 else
3252 num_bytes = found.offset;
3253
3254 ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
3255 &roots, false);
3256 if (ret < 0)
3257 goto out;
3258 /* For rescan, just pass old_roots as NULL */
3259 ret = btrfs_qgroup_account_extent(trans, found.objectid,
3260 num_bytes, NULL, roots);
3261 if (ret < 0)
3262 goto out;
3263 }
3264out:
David Brazdil0f672f62019-12-10 10:32:29 +00003265 if (scratch_leaf)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003266 free_extent_buffer(scratch_leaf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003267
3268 if (done && !ret) {
3269 ret = 1;
3270 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
3271 }
3272 return ret;
3273}
3274
Olivier Deprez0e641232021-09-23 10:07:05 +02003275static bool rescan_should_stop(struct btrfs_fs_info *fs_info)
3276{
3277 return btrfs_fs_closing(fs_info) ||
3278 test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
3279}
3280
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003281static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
3282{
3283 struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
3284 qgroup_rescan_work);
3285 struct btrfs_path *path;
3286 struct btrfs_trans_handle *trans = NULL;
3287 int err = -ENOMEM;
3288 int ret = 0;
Olivier Deprez0e641232021-09-23 10:07:05 +02003289 bool stopped = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003290
3291 path = btrfs_alloc_path();
3292 if (!path)
3293 goto out;
3294 /*
3295 * Rescan should only search for commit root, and any later difference
3296 * should be recorded by qgroup
3297 */
3298 path->search_commit_root = 1;
3299 path->skip_locking = 1;
3300
3301 err = 0;
Olivier Deprez0e641232021-09-23 10:07:05 +02003302 while (!err && !(stopped = rescan_should_stop(fs_info))) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003303 trans = btrfs_start_transaction(fs_info->fs_root, 0);
3304 if (IS_ERR(trans)) {
3305 err = PTR_ERR(trans);
3306 break;
3307 }
3308 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
3309 err = -EINTR;
3310 } else {
3311 err = qgroup_rescan_leaf(trans, path);
3312 }
3313 if (err > 0)
3314 btrfs_commit_transaction(trans);
3315 else
3316 btrfs_end_transaction(trans);
3317 }
3318
3319out:
3320 btrfs_free_path(path);
3321
3322 mutex_lock(&fs_info->qgroup_rescan_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003323 if (err > 0 &&
3324 fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
3325 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
3326 } else if (err < 0) {
3327 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
3328 }
3329 mutex_unlock(&fs_info->qgroup_rescan_lock);
3330
3331 /*
3332 * only update status, since the previous part has already updated the
3333 * qgroup info.
3334 */
3335 trans = btrfs_start_transaction(fs_info->quota_root, 1);
3336 if (IS_ERR(trans)) {
3337 err = PTR_ERR(trans);
David Brazdil0f672f62019-12-10 10:32:29 +00003338 trans = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003339 btrfs_err(fs_info,
3340 "fail to start transaction for status update: %d",
3341 err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003342 }
David Brazdil0f672f62019-12-10 10:32:29 +00003343
3344 mutex_lock(&fs_info->qgroup_rescan_lock);
Olivier Deprez0e641232021-09-23 10:07:05 +02003345 if (!stopped)
David Brazdil0f672f62019-12-10 10:32:29 +00003346 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3347 if (trans) {
3348 ret = update_qgroup_status_item(trans);
3349 if (ret < 0) {
3350 err = ret;
3351 btrfs_err(fs_info, "fail to update qgroup status: %d",
3352 err);
3353 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003354 }
David Brazdil0f672f62019-12-10 10:32:29 +00003355 fs_info->qgroup_rescan_running = false;
3356 complete_all(&fs_info->qgroup_rescan_completion);
3357 mutex_unlock(&fs_info->qgroup_rescan_lock);
3358
3359 if (!trans)
3360 return;
3361
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003362 btrfs_end_transaction(trans);
3363
Olivier Deprez0e641232021-09-23 10:07:05 +02003364 if (stopped) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003365 btrfs_info(fs_info, "qgroup scan paused");
3366 } else if (err >= 0) {
3367 btrfs_info(fs_info, "qgroup scan completed%s",
3368 err > 0 ? " (inconsistency flag cleared)" : "");
3369 } else {
3370 btrfs_err(fs_info, "qgroup scan failed with %d", err);
3371 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003372}
3373
3374/*
3375 * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
3376 * memory required for the rescan context.
3377 */
3378static int
3379qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
3380 int init_flags)
3381{
3382 int ret = 0;
3383
3384 if (!init_flags) {
3385 /* we're resuming qgroup rescan at mount time */
3386 if (!(fs_info->qgroup_flags &
3387 BTRFS_QGROUP_STATUS_FLAG_RESCAN)) {
3388 btrfs_warn(fs_info,
Olivier Deprez0e641232021-09-23 10:07:05 +02003389 "qgroup rescan init failed, qgroup rescan is not queued");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003390 ret = -EINVAL;
3391 } else if (!(fs_info->qgroup_flags &
3392 BTRFS_QGROUP_STATUS_FLAG_ON)) {
3393 btrfs_warn(fs_info,
Olivier Deprez0e641232021-09-23 10:07:05 +02003394 "qgroup rescan init failed, qgroup is not enabled");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003395 ret = -EINVAL;
3396 }
3397
3398 if (ret)
3399 return ret;
3400 }
3401
3402 mutex_lock(&fs_info->qgroup_rescan_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003403
3404 if (init_flags) {
3405 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
3406 btrfs_warn(fs_info,
3407 "qgroup rescan is already in progress");
3408 ret = -EINPROGRESS;
3409 } else if (!(fs_info->qgroup_flags &
3410 BTRFS_QGROUP_STATUS_FLAG_ON)) {
3411 btrfs_warn(fs_info,
3412 "qgroup rescan init failed, qgroup is not enabled");
3413 ret = -EINVAL;
Olivier Deprez157378f2022-04-04 15:47:50 +02003414 } else if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
3415 /* Quota disable is in progress */
3416 ret = -EBUSY;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003417 }
3418
3419 if (ret) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003420 mutex_unlock(&fs_info->qgroup_rescan_lock);
3421 return ret;
3422 }
3423 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3424 }
3425
3426 memset(&fs_info->qgroup_rescan_progress, 0,
3427 sizeof(fs_info->qgroup_rescan_progress));
3428 fs_info->qgroup_rescan_progress.objectid = progress_objectid;
3429 init_completion(&fs_info->qgroup_rescan_completion);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003430 mutex_unlock(&fs_info->qgroup_rescan_lock);
3431
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003432 btrfs_init_work(&fs_info->qgroup_rescan_work,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003433 btrfs_qgroup_rescan_worker, NULL, NULL);
3434 return 0;
3435}
3436
3437static void
3438qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
3439{
3440 struct rb_node *n;
3441 struct btrfs_qgroup *qgroup;
3442
3443 spin_lock(&fs_info->qgroup_lock);
3444 /* clear all current qgroup tracking information */
3445 for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
3446 qgroup = rb_entry(n, struct btrfs_qgroup, node);
3447 qgroup->rfer = 0;
3448 qgroup->rfer_cmpr = 0;
3449 qgroup->excl = 0;
3450 qgroup->excl_cmpr = 0;
3451 qgroup_dirty(fs_info, qgroup);
3452 }
3453 spin_unlock(&fs_info->qgroup_lock);
3454}
3455
3456int
3457btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
3458{
3459 int ret = 0;
3460 struct btrfs_trans_handle *trans;
3461
3462 ret = qgroup_rescan_init(fs_info, 0, 1);
3463 if (ret)
3464 return ret;
3465
3466 /*
3467 * We have set the rescan_progress to 0, which means no more
3468 * delayed refs will be accounted by btrfs_qgroup_account_ref.
3469 * However, btrfs_qgroup_account_ref may be right after its call
3470 * to btrfs_find_all_roots, in which case it would still do the
3471 * accounting.
3472 * To solve this, we're committing the transaction, which will
3473 * ensure we run all delayed refs and only after that, we are
3474 * going to clear all tracking information for a clean start.
3475 */
3476
3477 trans = btrfs_join_transaction(fs_info->fs_root);
3478 if (IS_ERR(trans)) {
3479 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3480 return PTR_ERR(trans);
3481 }
3482 ret = btrfs_commit_transaction(trans);
3483 if (ret) {
3484 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
3485 return ret;
3486 }
3487
3488 qgroup_rescan_zero_tracking(fs_info);
3489
Olivier Deprez0e641232021-09-23 10:07:05 +02003490 mutex_lock(&fs_info->qgroup_rescan_lock);
3491 fs_info->qgroup_rescan_running = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003492 btrfs_queue_work(fs_info->qgroup_rescan_workers,
3493 &fs_info->qgroup_rescan_work);
Olivier Deprez0e641232021-09-23 10:07:05 +02003494 mutex_unlock(&fs_info->qgroup_rescan_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003495
3496 return 0;
3497}
3498
3499int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
3500 bool interruptible)
3501{
3502 int running;
3503 int ret = 0;
3504
3505 mutex_lock(&fs_info->qgroup_rescan_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003506 running = fs_info->qgroup_rescan_running;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003507 mutex_unlock(&fs_info->qgroup_rescan_lock);
3508
3509 if (!running)
3510 return 0;
3511
3512 if (interruptible)
3513 ret = wait_for_completion_interruptible(
3514 &fs_info->qgroup_rescan_completion);
3515 else
3516 wait_for_completion(&fs_info->qgroup_rescan_completion);
3517
3518 return ret;
3519}
3520
3521/*
3522 * this is only called from open_ctree where we're still single threaded, thus
3523 * locking is omitted here.
3524 */
3525void
3526btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
3527{
Olivier Deprez0e641232021-09-23 10:07:05 +02003528 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
3529 mutex_lock(&fs_info->qgroup_rescan_lock);
3530 fs_info->qgroup_rescan_running = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003531 btrfs_queue_work(fs_info->qgroup_rescan_workers,
3532 &fs_info->qgroup_rescan_work);
Olivier Deprez0e641232021-09-23 10:07:05 +02003533 mutex_unlock(&fs_info->qgroup_rescan_lock);
3534 }
3535}
3536
3537#define rbtree_iterate_from_safe(node, next, start) \
3538 for (node = start; node && ({ next = rb_next(node); 1;}); node = next)
3539
3540static int qgroup_unreserve_range(struct btrfs_inode *inode,
3541 struct extent_changeset *reserved, u64 start,
3542 u64 len)
3543{
3544 struct rb_node *node;
3545 struct rb_node *next;
Olivier Deprez157378f2022-04-04 15:47:50 +02003546 struct ulist_node *entry;
Olivier Deprez0e641232021-09-23 10:07:05 +02003547 int ret = 0;
3548
3549 node = reserved->range_changed.root.rb_node;
Olivier Deprez157378f2022-04-04 15:47:50 +02003550 if (!node)
3551 return 0;
Olivier Deprez0e641232021-09-23 10:07:05 +02003552 while (node) {
3553 entry = rb_entry(node, struct ulist_node, rb_node);
3554 if (entry->val < start)
3555 node = node->rb_right;
Olivier Deprez0e641232021-09-23 10:07:05 +02003556 else
Olivier Deprez157378f2022-04-04 15:47:50 +02003557 node = node->rb_left;
Olivier Deprez0e641232021-09-23 10:07:05 +02003558 }
3559
Olivier Deprez0e641232021-09-23 10:07:05 +02003560 if (entry->val > start && rb_prev(&entry->rb_node))
3561 entry = rb_entry(rb_prev(&entry->rb_node), struct ulist_node,
3562 rb_node);
3563
3564 rbtree_iterate_from_safe(node, next, &entry->rb_node) {
3565 u64 entry_start;
3566 u64 entry_end;
3567 u64 entry_len;
3568 int clear_ret;
3569
3570 entry = rb_entry(node, struct ulist_node, rb_node);
3571 entry_start = entry->val;
3572 entry_end = entry->aux;
3573 entry_len = entry_end - entry_start + 1;
3574
3575 if (entry_start >= start + len)
3576 break;
3577 if (entry_start + entry_len <= start)
3578 continue;
3579 /*
3580 * Now the entry is in [start, start + len), revert the
3581 * EXTENT_QGROUP_RESERVED bit.
3582 */
3583 clear_ret = clear_extent_bits(&inode->io_tree, entry_start,
3584 entry_end, EXTENT_QGROUP_RESERVED);
3585 if (!ret && clear_ret < 0)
3586 ret = clear_ret;
3587
3588 ulist_del(&reserved->range_changed, entry->val, entry->aux);
3589 if (likely(reserved->bytes_changed >= entry_len)) {
3590 reserved->bytes_changed -= entry_len;
3591 } else {
3592 WARN_ON(1);
3593 reserved->bytes_changed = 0;
3594 }
3595 }
3596
3597 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003598}
3599
3600/*
Olivier Deprez0e641232021-09-23 10:07:05 +02003601 * Try to free some space for qgroup.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003602 *
Olivier Deprez0e641232021-09-23 10:07:05 +02003603 * For qgroup, there are only 3 ways to free qgroup space:
3604 * - Flush nodatacow write
3605 * Any nodatacow write will free its reserved data space at run_delalloc_range().
3606 * In theory, we should only flush nodatacow inodes, but it's not yet
3607 * possible, so we need to flush the whole root.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003608 *
Olivier Deprez0e641232021-09-23 10:07:05 +02003609 * - Wait for ordered extents
3610 * When ordered extents are finished, their reserved metadata is finally
3611 * converted to per_trans status, which can be freed by later commit
3612 * transaction.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003613 *
Olivier Deprez0e641232021-09-23 10:07:05 +02003614 * - Commit transaction
3615 * This would free the meta_per_trans space.
3616 * In theory this shouldn't provide much space, but any more qgroup space
3617 * is needed.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003618 */
Olivier Deprez0e641232021-09-23 10:07:05 +02003619static int try_flush_qgroup(struct btrfs_root *root)
3620{
3621 struct btrfs_trans_handle *trans;
3622 int ret;
3623 bool can_commit = true;
3624
3625 /*
Olivier Deprez0e641232021-09-23 10:07:05 +02003626 * If current process holds a transaction, we shouldn't flush, as we
3627 * assume all space reservation happens before a transaction handle is
3628 * held.
3629 *
3630 * But there are cases like btrfs_delayed_item_reserve_metadata() where
3631 * we try to reserve space with one transction handle already held.
3632 * In that case we can't commit transaction, but at least try to end it
3633 * and hope the started data writes can free some space.
3634 */
3635 if (current->journal_info &&
3636 current->journal_info != BTRFS_SEND_TRANS_STUB)
3637 can_commit = false;
3638
Olivier Deprez157378f2022-04-04 15:47:50 +02003639 /*
3640 * We don't want to run flush again and again, so if there is a running
3641 * one, we won't try to start a new flush, but exit directly.
3642 */
3643 if (test_and_set_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state)) {
3644 /*
3645 * We are already holding a transaction, thus we can block other
3646 * threads from flushing. So exit right now. This increases
3647 * the chance of EDQUOT for heavy load and near limit cases.
3648 * But we can argue that if we're already near limit, EDQUOT is
3649 * unavoidable anyway.
3650 */
3651 if (!can_commit)
3652 return 0;
3653
3654 wait_event(root->qgroup_flush_wait,
3655 !test_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state));
3656 return 0;
3657 }
3658
Olivier Deprez0e641232021-09-23 10:07:05 +02003659 ret = btrfs_start_delalloc_snapshot(root);
3660 if (ret < 0)
3661 goto out;
3662 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
3663
3664 trans = btrfs_join_transaction(root);
3665 if (IS_ERR(trans)) {
3666 ret = PTR_ERR(trans);
3667 goto out;
3668 }
3669
3670 if (can_commit)
3671 ret = btrfs_commit_transaction(trans);
3672 else
3673 ret = btrfs_end_transaction(trans);
3674out:
3675 clear_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state);
3676 wake_up(&root->qgroup_flush_wait);
3677 return ret;
3678}
3679
3680static int qgroup_reserve_data(struct btrfs_inode *inode,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003681 struct extent_changeset **reserved_ret, u64 start,
3682 u64 len)
3683{
Olivier Deprez0e641232021-09-23 10:07:05 +02003684 struct btrfs_root *root = inode->root;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003685 struct extent_changeset *reserved;
Olivier Deprez0e641232021-09-23 10:07:05 +02003686 bool new_reserved = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003687 u64 orig_reserved;
3688 u64 to_reserve;
3689 int ret;
3690
3691 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
David Brazdil0f672f62019-12-10 10:32:29 +00003692 !is_fstree(root->root_key.objectid) || len == 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003693 return 0;
3694
3695 /* @reserved parameter is mandatory for qgroup */
3696 if (WARN_ON(!reserved_ret))
3697 return -EINVAL;
3698 if (!*reserved_ret) {
Olivier Deprez0e641232021-09-23 10:07:05 +02003699 new_reserved = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003700 *reserved_ret = extent_changeset_alloc();
3701 if (!*reserved_ret)
3702 return -ENOMEM;
3703 }
3704 reserved = *reserved_ret;
3705 /* Record already reserved space */
3706 orig_reserved = reserved->bytes_changed;
Olivier Deprez0e641232021-09-23 10:07:05 +02003707 ret = set_record_extent_bits(&inode->io_tree, start,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003708 start + len -1, EXTENT_QGROUP_RESERVED, reserved);
3709
3710 /* Newly reserved space */
3711 to_reserve = reserved->bytes_changed - orig_reserved;
Olivier Deprez0e641232021-09-23 10:07:05 +02003712 trace_btrfs_qgroup_reserve_data(&inode->vfs_inode, start, len,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003713 to_reserve, QGROUP_RESERVE);
3714 if (ret < 0)
Olivier Deprez0e641232021-09-23 10:07:05 +02003715 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003716 ret = qgroup_reserve(root, to_reserve, true, BTRFS_QGROUP_RSV_DATA);
3717 if (ret < 0)
3718 goto cleanup;
3719
3720 return ret;
3721
3722cleanup:
Olivier Deprez0e641232021-09-23 10:07:05 +02003723 qgroup_unreserve_range(inode, reserved, start, len);
3724out:
3725 if (new_reserved) {
3726 extent_changeset_release(reserved);
3727 kfree(reserved);
3728 *reserved_ret = NULL;
3729 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003730 return ret;
3731}
3732
Olivier Deprez0e641232021-09-23 10:07:05 +02003733/*
3734 * Reserve qgroup space for range [start, start + len).
3735 *
3736 * This function will either reserve space from related qgroups or do nothing
3737 * if the range is already reserved.
3738 *
3739 * Return 0 for successful reservation
3740 * Return <0 for error (including -EQUOT)
3741 *
3742 * NOTE: This function may sleep for memory allocation, dirty page flushing and
3743 * commit transaction. So caller should not hold any dirty page locked.
3744 */
3745int btrfs_qgroup_reserve_data(struct btrfs_inode *inode,
3746 struct extent_changeset **reserved_ret, u64 start,
3747 u64 len)
3748{
3749 int ret;
3750
3751 ret = qgroup_reserve_data(inode, reserved_ret, start, len);
3752 if (ret <= 0 && ret != -EDQUOT)
3753 return ret;
3754
3755 ret = try_flush_qgroup(inode->root);
3756 if (ret < 0)
3757 return ret;
3758 return qgroup_reserve_data(inode, reserved_ret, start, len);
3759}
3760
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003761/* Free ranges specified by @reserved, normally in error path */
Olivier Deprez0e641232021-09-23 10:07:05 +02003762static int qgroup_free_reserved_data(struct btrfs_inode *inode,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003763 struct extent_changeset *reserved, u64 start, u64 len)
3764{
Olivier Deprez0e641232021-09-23 10:07:05 +02003765 struct btrfs_root *root = inode->root;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003766 struct ulist_node *unode;
3767 struct ulist_iterator uiter;
3768 struct extent_changeset changeset;
3769 int freed = 0;
3770 int ret;
3771
3772 extent_changeset_init(&changeset);
3773 len = round_up(start + len, root->fs_info->sectorsize);
3774 start = round_down(start, root->fs_info->sectorsize);
3775
3776 ULIST_ITER_INIT(&uiter);
3777 while ((unode = ulist_next(&reserved->range_changed, &uiter))) {
3778 u64 range_start = unode->val;
3779 /* unode->aux is the inclusive end */
3780 u64 range_len = unode->aux - range_start + 1;
3781 u64 free_start;
3782 u64 free_len;
3783
3784 extent_changeset_release(&changeset);
3785
3786 /* Only free range in range [start, start + len) */
3787 if (range_start >= start + len ||
3788 range_start + range_len <= start)
3789 continue;
3790 free_start = max(range_start, start);
3791 free_len = min(start + len, range_start + range_len) -
3792 free_start;
3793 /*
3794 * TODO: To also modify reserved->ranges_reserved to reflect
3795 * the modification.
3796 *
3797 * However as long as we free qgroup reserved according to
3798 * EXTENT_QGROUP_RESERVED, we won't double free.
3799 * So not need to rush.
3800 */
Olivier Deprez0e641232021-09-23 10:07:05 +02003801 ret = clear_record_extent_bits(&inode->io_tree, free_start,
3802 free_start + free_len - 1,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003803 EXTENT_QGROUP_RESERVED, &changeset);
3804 if (ret < 0)
3805 goto out;
3806 freed += changeset.bytes_changed;
3807 }
David Brazdil0f672f62019-12-10 10:32:29 +00003808 btrfs_qgroup_free_refroot(root->fs_info, root->root_key.objectid, freed,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003809 BTRFS_QGROUP_RSV_DATA);
3810 ret = freed;
3811out:
3812 extent_changeset_release(&changeset);
3813 return ret;
3814}
3815
Olivier Deprez157378f2022-04-04 15:47:50 +02003816static int __btrfs_qgroup_release_data(struct btrfs_inode *inode,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003817 struct extent_changeset *reserved, u64 start, u64 len,
3818 int free)
3819{
3820 struct extent_changeset changeset;
3821 int trace_op = QGROUP_RELEASE;
3822 int ret;
3823
Olivier Deprez157378f2022-04-04 15:47:50 +02003824 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &inode->root->fs_info->flags))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003825 return 0;
3826
3827 /* In release case, we shouldn't have @reserved */
3828 WARN_ON(!free && reserved);
3829 if (free && reserved)
Olivier Deprez157378f2022-04-04 15:47:50 +02003830 return qgroup_free_reserved_data(inode, reserved, start, len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003831 extent_changeset_init(&changeset);
Olivier Deprez157378f2022-04-04 15:47:50 +02003832 ret = clear_record_extent_bits(&inode->io_tree, start, start + len -1,
3833 EXTENT_QGROUP_RESERVED, &changeset);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003834 if (ret < 0)
3835 goto out;
3836
3837 if (free)
3838 trace_op = QGROUP_FREE;
Olivier Deprez157378f2022-04-04 15:47:50 +02003839 trace_btrfs_qgroup_release_data(&inode->vfs_inode, start, len,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003840 changeset.bytes_changed, trace_op);
3841 if (free)
Olivier Deprez157378f2022-04-04 15:47:50 +02003842 btrfs_qgroup_free_refroot(inode->root->fs_info,
3843 inode->root->root_key.objectid,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003844 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
3845 ret = changeset.bytes_changed;
3846out:
3847 extent_changeset_release(&changeset);
3848 return ret;
3849}
3850
3851/*
3852 * Free a reserved space range from io_tree and related qgroups
3853 *
3854 * Should be called when a range of pages get invalidated before reaching disk.
3855 * Or for error cleanup case.
3856 * if @reserved is given, only reserved range in [@start, @start + @len) will
3857 * be freed.
3858 *
3859 * For data written to disk, use btrfs_qgroup_release_data().
3860 *
3861 * NOTE: This function may sleep for memory allocation.
3862 */
Olivier Deprez157378f2022-04-04 15:47:50 +02003863int btrfs_qgroup_free_data(struct btrfs_inode *inode,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003864 struct extent_changeset *reserved, u64 start, u64 len)
3865{
3866 return __btrfs_qgroup_release_data(inode, reserved, start, len, 1);
3867}
3868
3869/*
3870 * Release a reserved space range from io_tree only.
3871 *
3872 * Should be called when a range of pages get written to disk and corresponding
3873 * FILE_EXTENT is inserted into corresponding root.
3874 *
3875 * Since new qgroup accounting framework will only update qgroup numbers at
3876 * commit_transaction() time, its reserved space shouldn't be freed from
3877 * related qgroups.
3878 *
3879 * But we should release the range from io_tree, to allow further write to be
3880 * COWed.
3881 *
3882 * NOTE: This function may sleep for memory allocation.
3883 */
Olivier Deprez157378f2022-04-04 15:47:50 +02003884int btrfs_qgroup_release_data(struct btrfs_inode *inode, u64 start, u64 len)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003885{
3886 return __btrfs_qgroup_release_data(inode, NULL, start, len, 0);
3887}
3888
3889static void add_root_meta_rsv(struct btrfs_root *root, int num_bytes,
3890 enum btrfs_qgroup_rsv_type type)
3891{
3892 if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
3893 type != BTRFS_QGROUP_RSV_META_PERTRANS)
3894 return;
3895 if (num_bytes == 0)
3896 return;
3897
3898 spin_lock(&root->qgroup_meta_rsv_lock);
3899 if (type == BTRFS_QGROUP_RSV_META_PREALLOC)
3900 root->qgroup_meta_rsv_prealloc += num_bytes;
3901 else
3902 root->qgroup_meta_rsv_pertrans += num_bytes;
3903 spin_unlock(&root->qgroup_meta_rsv_lock);
3904}
3905
3906static int sub_root_meta_rsv(struct btrfs_root *root, int num_bytes,
3907 enum btrfs_qgroup_rsv_type type)
3908{
3909 if (type != BTRFS_QGROUP_RSV_META_PREALLOC &&
3910 type != BTRFS_QGROUP_RSV_META_PERTRANS)
3911 return 0;
3912 if (num_bytes == 0)
3913 return 0;
3914
3915 spin_lock(&root->qgroup_meta_rsv_lock);
3916 if (type == BTRFS_QGROUP_RSV_META_PREALLOC) {
3917 num_bytes = min_t(u64, root->qgroup_meta_rsv_prealloc,
3918 num_bytes);
3919 root->qgroup_meta_rsv_prealloc -= num_bytes;
3920 } else {
3921 num_bytes = min_t(u64, root->qgroup_meta_rsv_pertrans,
3922 num_bytes);
3923 root->qgroup_meta_rsv_pertrans -= num_bytes;
3924 }
3925 spin_unlock(&root->qgroup_meta_rsv_lock);
3926 return num_bytes;
3927}
3928
Olivier Deprez0e641232021-09-23 10:07:05 +02003929int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
3930 enum btrfs_qgroup_rsv_type type, bool enforce)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003931{
3932 struct btrfs_fs_info *fs_info = root->fs_info;
3933 int ret;
3934
3935 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
David Brazdil0f672f62019-12-10 10:32:29 +00003936 !is_fstree(root->root_key.objectid) || num_bytes == 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003937 return 0;
3938
3939 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
David Brazdil0f672f62019-12-10 10:32:29 +00003940 trace_qgroup_meta_reserve(root, (s64)num_bytes, type);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003941 ret = qgroup_reserve(root, num_bytes, enforce, type);
3942 if (ret < 0)
3943 return ret;
3944 /*
3945 * Record what we have reserved into root.
3946 *
3947 * To avoid quota disabled->enabled underflow.
3948 * In that case, we may try to free space we haven't reserved
3949 * (since quota was disabled), so record what we reserved into root.
3950 * And ensure later release won't underflow this number.
3951 */
3952 add_root_meta_rsv(root, num_bytes, type);
3953 return ret;
3954}
3955
Olivier Deprez0e641232021-09-23 10:07:05 +02003956int __btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
3957 enum btrfs_qgroup_rsv_type type, bool enforce)
3958{
3959 int ret;
3960
3961 ret = btrfs_qgroup_reserve_meta(root, num_bytes, type, enforce);
3962 if (ret <= 0 && ret != -EDQUOT)
3963 return ret;
3964
3965 ret = try_flush_qgroup(root);
3966 if (ret < 0)
3967 return ret;
3968 return btrfs_qgroup_reserve_meta(root, num_bytes, type, enforce);
3969}
3970
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003971void btrfs_qgroup_free_meta_all_pertrans(struct btrfs_root *root)
3972{
3973 struct btrfs_fs_info *fs_info = root->fs_info;
3974
3975 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
David Brazdil0f672f62019-12-10 10:32:29 +00003976 !is_fstree(root->root_key.objectid))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003977 return;
3978
3979 /* TODO: Update trace point to handle such free */
3980 trace_qgroup_meta_free_all_pertrans(root);
3981 /* Special value -1 means to free all reserved space */
David Brazdil0f672f62019-12-10 10:32:29 +00003982 btrfs_qgroup_free_refroot(fs_info, root->root_key.objectid, (u64)-1,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003983 BTRFS_QGROUP_RSV_META_PERTRANS);
3984}
3985
3986void __btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes,
3987 enum btrfs_qgroup_rsv_type type)
3988{
3989 struct btrfs_fs_info *fs_info = root->fs_info;
3990
3991 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
David Brazdil0f672f62019-12-10 10:32:29 +00003992 !is_fstree(root->root_key.objectid))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003993 return;
3994
3995 /*
3996 * reservation for META_PREALLOC can happen before quota is enabled,
3997 * which can lead to underflow.
3998 * Here ensure we will only free what we really have reserved.
3999 */
4000 num_bytes = sub_root_meta_rsv(root, num_bytes, type);
4001 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
David Brazdil0f672f62019-12-10 10:32:29 +00004002 trace_qgroup_meta_reserve(root, -(s64)num_bytes, type);
4003 btrfs_qgroup_free_refroot(fs_info, root->root_key.objectid,
4004 num_bytes, type);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004005}
4006
4007static void qgroup_convert_meta(struct btrfs_fs_info *fs_info, u64 ref_root,
4008 int num_bytes)
4009{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004010 struct btrfs_qgroup *qgroup;
4011 struct ulist_node *unode;
4012 struct ulist_iterator uiter;
4013 int ret = 0;
4014
4015 if (num_bytes == 0)
4016 return;
Olivier Deprez157378f2022-04-04 15:47:50 +02004017 if (!fs_info->quota_root)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004018 return;
4019
4020 spin_lock(&fs_info->qgroup_lock);
4021 qgroup = find_qgroup_rb(fs_info, ref_root);
4022 if (!qgroup)
4023 goto out;
4024 ulist_reinit(fs_info->qgroup_ulist);
4025 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
4026 qgroup_to_aux(qgroup), GFP_ATOMIC);
4027 if (ret < 0)
4028 goto out;
4029 ULIST_ITER_INIT(&uiter);
4030 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
4031 struct btrfs_qgroup *qg;
4032 struct btrfs_qgroup_list *glist;
4033
4034 qg = unode_aux_to_qgroup(unode);
4035
4036 qgroup_rsv_release(fs_info, qg, num_bytes,
4037 BTRFS_QGROUP_RSV_META_PREALLOC);
4038 qgroup_rsv_add(fs_info, qg, num_bytes,
4039 BTRFS_QGROUP_RSV_META_PERTRANS);
4040 list_for_each_entry(glist, &qg->groups, next_group) {
4041 ret = ulist_add(fs_info->qgroup_ulist,
4042 glist->group->qgroupid,
4043 qgroup_to_aux(glist->group), GFP_ATOMIC);
4044 if (ret < 0)
4045 goto out;
4046 }
4047 }
4048out:
4049 spin_unlock(&fs_info->qgroup_lock);
4050}
4051
4052void btrfs_qgroup_convert_reserved_meta(struct btrfs_root *root, int num_bytes)
4053{
4054 struct btrfs_fs_info *fs_info = root->fs_info;
4055
4056 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
David Brazdil0f672f62019-12-10 10:32:29 +00004057 !is_fstree(root->root_key.objectid))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004058 return;
4059 /* Same as btrfs_qgroup_free_meta_prealloc() */
4060 num_bytes = sub_root_meta_rsv(root, num_bytes,
4061 BTRFS_QGROUP_RSV_META_PREALLOC);
4062 trace_qgroup_meta_convert(root, num_bytes);
David Brazdil0f672f62019-12-10 10:32:29 +00004063 qgroup_convert_meta(fs_info, root->root_key.objectid, num_bytes);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004064}
4065
4066/*
4067 * Check qgroup reserved space leaking, normally at destroy inode
4068 * time
4069 */
Olivier Deprez0e641232021-09-23 10:07:05 +02004070void btrfs_qgroup_check_reserved_leak(struct btrfs_inode *inode)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004071{
4072 struct extent_changeset changeset;
4073 struct ulist_node *unode;
4074 struct ulist_iterator iter;
4075 int ret;
4076
4077 extent_changeset_init(&changeset);
Olivier Deprez0e641232021-09-23 10:07:05 +02004078 ret = clear_record_extent_bits(&inode->io_tree, 0, (u64)-1,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004079 EXTENT_QGROUP_RESERVED, &changeset);
4080
4081 WARN_ON(ret < 0);
4082 if (WARN_ON(changeset.bytes_changed)) {
4083 ULIST_ITER_INIT(&iter);
4084 while ((unode = ulist_next(&changeset.range_changed, &iter))) {
Olivier Deprez0e641232021-09-23 10:07:05 +02004085 btrfs_warn(inode->root->fs_info,
4086 "leaking qgroup reserved space, ino: %llu, start: %llu, end: %llu",
4087 btrfs_ino(inode), unode->val, unode->aux);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004088 }
Olivier Deprez0e641232021-09-23 10:07:05 +02004089 btrfs_qgroup_free_refroot(inode->root->fs_info,
4090 inode->root->root_key.objectid,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004091 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
4092
4093 }
4094 extent_changeset_release(&changeset);
4095}
David Brazdil0f672f62019-12-10 10:32:29 +00004096
4097void btrfs_qgroup_init_swapped_blocks(
4098 struct btrfs_qgroup_swapped_blocks *swapped_blocks)
4099{
4100 int i;
4101
4102 spin_lock_init(&swapped_blocks->lock);
4103 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
4104 swapped_blocks->blocks[i] = RB_ROOT;
4105 swapped_blocks->swapped = false;
4106}
4107
4108/*
4109 * Delete all swapped blocks record of @root.
4110 * Every record here means we skipped a full subtree scan for qgroup.
4111 *
4112 * Gets called when committing one transaction.
4113 */
4114void btrfs_qgroup_clean_swapped_blocks(struct btrfs_root *root)
4115{
4116 struct btrfs_qgroup_swapped_blocks *swapped_blocks;
4117 int i;
4118
4119 swapped_blocks = &root->swapped_blocks;
4120
4121 spin_lock(&swapped_blocks->lock);
4122 if (!swapped_blocks->swapped)
4123 goto out;
4124 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
4125 struct rb_root *cur_root = &swapped_blocks->blocks[i];
4126 struct btrfs_qgroup_swapped_block *entry;
4127 struct btrfs_qgroup_swapped_block *next;
4128
4129 rbtree_postorder_for_each_entry_safe(entry, next, cur_root,
4130 node)
4131 kfree(entry);
4132 swapped_blocks->blocks[i] = RB_ROOT;
4133 }
4134 swapped_blocks->swapped = false;
4135out:
4136 spin_unlock(&swapped_blocks->lock);
4137}
4138
4139/*
4140 * Add subtree roots record into @subvol_root.
4141 *
4142 * @subvol_root: tree root of the subvolume tree get swapped
4143 * @bg: block group under balance
4144 * @subvol_parent/slot: pointer to the subtree root in subvolume tree
4145 * @reloc_parent/slot: pointer to the subtree root in reloc tree
4146 * BOTH POINTERS ARE BEFORE TREE SWAP
4147 * @last_snapshot: last snapshot generation of the subvolume tree
4148 */
4149int btrfs_qgroup_add_swapped_blocks(struct btrfs_trans_handle *trans,
4150 struct btrfs_root *subvol_root,
Olivier Deprez157378f2022-04-04 15:47:50 +02004151 struct btrfs_block_group *bg,
David Brazdil0f672f62019-12-10 10:32:29 +00004152 struct extent_buffer *subvol_parent, int subvol_slot,
4153 struct extent_buffer *reloc_parent, int reloc_slot,
4154 u64 last_snapshot)
4155{
4156 struct btrfs_fs_info *fs_info = subvol_root->fs_info;
4157 struct btrfs_qgroup_swapped_blocks *blocks = &subvol_root->swapped_blocks;
4158 struct btrfs_qgroup_swapped_block *block;
4159 struct rb_node **cur;
4160 struct rb_node *parent = NULL;
4161 int level = btrfs_header_level(subvol_parent) - 1;
4162 int ret = 0;
4163
4164 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
4165 return 0;
4166
4167 if (btrfs_node_ptr_generation(subvol_parent, subvol_slot) >
4168 btrfs_node_ptr_generation(reloc_parent, reloc_slot)) {
4169 btrfs_err_rl(fs_info,
4170 "%s: bad parameter order, subvol_gen=%llu reloc_gen=%llu",
4171 __func__,
4172 btrfs_node_ptr_generation(subvol_parent, subvol_slot),
4173 btrfs_node_ptr_generation(reloc_parent, reloc_slot));
4174 return -EUCLEAN;
4175 }
4176
4177 block = kmalloc(sizeof(*block), GFP_NOFS);
4178 if (!block) {
4179 ret = -ENOMEM;
4180 goto out;
4181 }
4182
4183 /*
4184 * @reloc_parent/slot is still before swap, while @block is going to
4185 * record the bytenr after swap, so we do the swap here.
4186 */
4187 block->subvol_bytenr = btrfs_node_blockptr(reloc_parent, reloc_slot);
4188 block->subvol_generation = btrfs_node_ptr_generation(reloc_parent,
4189 reloc_slot);
4190 block->reloc_bytenr = btrfs_node_blockptr(subvol_parent, subvol_slot);
4191 block->reloc_generation = btrfs_node_ptr_generation(subvol_parent,
4192 subvol_slot);
4193 block->last_snapshot = last_snapshot;
4194 block->level = level;
4195
4196 /*
4197 * If we have bg == NULL, we're called from btrfs_recover_relocation(),
4198 * no one else can modify tree blocks thus we qgroup will not change
4199 * no matter the value of trace_leaf.
4200 */
4201 if (bg && bg->flags & BTRFS_BLOCK_GROUP_DATA)
4202 block->trace_leaf = true;
4203 else
4204 block->trace_leaf = false;
4205 btrfs_node_key_to_cpu(reloc_parent, &block->first_key, reloc_slot);
4206
4207 /* Insert @block into @blocks */
4208 spin_lock(&blocks->lock);
4209 cur = &blocks->blocks[level].rb_node;
4210 while (*cur) {
4211 struct btrfs_qgroup_swapped_block *entry;
4212
4213 parent = *cur;
4214 entry = rb_entry(parent, struct btrfs_qgroup_swapped_block,
4215 node);
4216
4217 if (entry->subvol_bytenr < block->subvol_bytenr) {
4218 cur = &(*cur)->rb_left;
4219 } else if (entry->subvol_bytenr > block->subvol_bytenr) {
4220 cur = &(*cur)->rb_right;
4221 } else {
4222 if (entry->subvol_generation !=
4223 block->subvol_generation ||
4224 entry->reloc_bytenr != block->reloc_bytenr ||
4225 entry->reloc_generation !=
4226 block->reloc_generation) {
4227 /*
4228 * Duplicated but mismatch entry found.
4229 * Shouldn't happen.
4230 *
4231 * Marking qgroup inconsistent should be enough
4232 * for end users.
4233 */
4234 WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
4235 ret = -EEXIST;
4236 }
4237 kfree(block);
4238 goto out_unlock;
4239 }
4240 }
4241 rb_link_node(&block->node, parent, cur);
4242 rb_insert_color(&block->node, &blocks->blocks[level]);
4243 blocks->swapped = true;
4244out_unlock:
4245 spin_unlock(&blocks->lock);
4246out:
4247 if (ret < 0)
4248 fs_info->qgroup_flags |=
4249 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
4250 return ret;
4251}
4252
4253/*
4254 * Check if the tree block is a subtree root, and if so do the needed
4255 * delayed subtree trace for qgroup.
4256 *
4257 * This is called during btrfs_cow_block().
4258 */
4259int btrfs_qgroup_trace_subtree_after_cow(struct btrfs_trans_handle *trans,
4260 struct btrfs_root *root,
4261 struct extent_buffer *subvol_eb)
4262{
4263 struct btrfs_fs_info *fs_info = root->fs_info;
4264 struct btrfs_qgroup_swapped_blocks *blocks = &root->swapped_blocks;
4265 struct btrfs_qgroup_swapped_block *block;
4266 struct extent_buffer *reloc_eb = NULL;
4267 struct rb_node *node;
4268 bool found = false;
4269 bool swapped = false;
4270 int level = btrfs_header_level(subvol_eb);
4271 int ret = 0;
4272 int i;
4273
4274 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
4275 return 0;
4276 if (!is_fstree(root->root_key.objectid) || !root->reloc_root)
4277 return 0;
4278
4279 spin_lock(&blocks->lock);
4280 if (!blocks->swapped) {
4281 spin_unlock(&blocks->lock);
4282 return 0;
4283 }
4284 node = blocks->blocks[level].rb_node;
4285
4286 while (node) {
4287 block = rb_entry(node, struct btrfs_qgroup_swapped_block, node);
4288 if (block->subvol_bytenr < subvol_eb->start) {
4289 node = node->rb_left;
4290 } else if (block->subvol_bytenr > subvol_eb->start) {
4291 node = node->rb_right;
4292 } else {
4293 found = true;
4294 break;
4295 }
4296 }
4297 if (!found) {
4298 spin_unlock(&blocks->lock);
4299 goto out;
4300 }
4301 /* Found one, remove it from @blocks first and update blocks->swapped */
4302 rb_erase(&block->node, &blocks->blocks[level]);
4303 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
4304 if (RB_EMPTY_ROOT(&blocks->blocks[i])) {
4305 swapped = true;
4306 break;
4307 }
4308 }
4309 blocks->swapped = swapped;
4310 spin_unlock(&blocks->lock);
4311
4312 /* Read out reloc subtree root */
4313 reloc_eb = read_tree_block(fs_info, block->reloc_bytenr,
4314 block->reloc_generation, block->level,
4315 &block->first_key);
4316 if (IS_ERR(reloc_eb)) {
4317 ret = PTR_ERR(reloc_eb);
4318 reloc_eb = NULL;
4319 goto free_out;
4320 }
4321 if (!extent_buffer_uptodate(reloc_eb)) {
4322 ret = -EIO;
4323 goto free_out;
4324 }
4325
4326 ret = qgroup_trace_subtree_swap(trans, reloc_eb, subvol_eb,
4327 block->last_snapshot, block->trace_leaf);
4328free_out:
4329 kfree(block);
4330 free_extent_buffer(reloc_eb);
4331out:
4332 if (ret < 0) {
4333 btrfs_err_rl(fs_info,
4334 "failed to account subtree at bytenr %llu: %d",
4335 subvol_eb->start, ret);
4336 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
4337 }
4338 return ret;
4339}
Olivier Deprez0e641232021-09-23 10:07:05 +02004340
4341void btrfs_qgroup_destroy_extent_records(struct btrfs_transaction *trans)
4342{
4343 struct btrfs_qgroup_extent_record *entry;
4344 struct btrfs_qgroup_extent_record *next;
4345 struct rb_root *root;
4346
4347 root = &trans->delayed_refs.dirty_extent_root;
4348 rbtree_postorder_for_each_entry_safe(entry, next, root, node) {
4349 ulist_free(entry->old_roots);
4350 kfree(entry);
4351 }
4352}