]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/btrfs/qgroup.c
btrfs: qgroup: Add trace point for qgroup reserved space
[mirror_ubuntu-bionic-kernel.git] / fs / btrfs / qgroup.c
CommitLineData
bed92eae
AJ
1/*
2 * Copyright (C) 2011 STRATO. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/sched.h>
20#include <linux/pagemap.h>
21#include <linux/writeback.h>
22#include <linux/blkdev.h>
23#include <linux/rbtree.h>
24#include <linux/slab.h>
25#include <linux/workqueue.h>
55e301fd 26#include <linux/btrfs.h>
bed92eae
AJ
27
28#include "ctree.h"
29#include "transaction.h"
30#include "disk-io.h"
31#include "locking.h"
32#include "ulist.h"
bed92eae 33#include "backref.h"
2f232036 34#include "extent_io.h"
fcebe456 35#include "qgroup.h"
bed92eae 36
e69bcee3 37
bed92eae
AJ
38/* TODO XXX FIXME
39 * - subvol delete -> delete when ref goes to 0? delete limits also?
40 * - reorganize keys
41 * - compressed
42 * - sync
bed92eae
AJ
43 * - copy also limits on subvol creation
44 * - limit
45 * - caches fuer ulists
46 * - performance benchmarks
47 * - check all ioctl parameters
48 */
49
9c542136
QW
50static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup *qg, u64 seq,
51 int mod)
52{
53 if (qg->old_refcnt < seq)
54 qg->old_refcnt = seq;
55 qg->old_refcnt += mod;
56}
57
58static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup *qg, u64 seq,
59 int mod)
60{
61 if (qg->new_refcnt < seq)
62 qg->new_refcnt = seq;
63 qg->new_refcnt += mod;
64}
65
66static inline u64 btrfs_qgroup_get_old_refcnt(struct btrfs_qgroup *qg, u64 seq)
67{
68 if (qg->old_refcnt < seq)
69 return 0;
70 return qg->old_refcnt - seq;
71}
72
73static inline u64 btrfs_qgroup_get_new_refcnt(struct btrfs_qgroup *qg, u64 seq)
74{
75 if (qg->new_refcnt < seq)
76 return 0;
77 return qg->new_refcnt - seq;
78}
79
bed92eae
AJ
80/*
81 * glue structure to represent the relations between qgroups.
82 */
83struct btrfs_qgroup_list {
84 struct list_head next_group;
85 struct list_head next_member;
86 struct btrfs_qgroup *group;
87 struct btrfs_qgroup *member;
88};
89
ef2fff64
DS
90static inline u64 qgroup_to_aux(struct btrfs_qgroup *qg)
91{
92 return (u64)(uintptr_t)qg;
93}
94
95static inline struct btrfs_qgroup* unode_aux_to_qgroup(struct ulist_node *n)
96{
97 return (struct btrfs_qgroup *)(uintptr_t)n->aux;
98}
fcebe456 99
b382a324
JS
100static int
101qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
102 int init_flags);
103static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
2f232036 104
58400fce 105/* must be called with qgroup_ioctl_lock held */
bed92eae
AJ
106static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
107 u64 qgroupid)
108{
109 struct rb_node *n = fs_info->qgroup_tree.rb_node;
110 struct btrfs_qgroup *qgroup;
111
112 while (n) {
113 qgroup = rb_entry(n, struct btrfs_qgroup, node);
114 if (qgroup->qgroupid < qgroupid)
115 n = n->rb_left;
116 else if (qgroup->qgroupid > qgroupid)
117 n = n->rb_right;
118 else
119 return qgroup;
120 }
121 return NULL;
122}
123
124/* must be called with qgroup_lock held */
125static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
126 u64 qgroupid)
127{
128 struct rb_node **p = &fs_info->qgroup_tree.rb_node;
129 struct rb_node *parent = NULL;
130 struct btrfs_qgroup *qgroup;
131
132 while (*p) {
133 parent = *p;
134 qgroup = rb_entry(parent, struct btrfs_qgroup, node);
135
136 if (qgroup->qgroupid < qgroupid)
137 p = &(*p)->rb_left;
138 else if (qgroup->qgroupid > qgroupid)
139 p = &(*p)->rb_right;
140 else
141 return qgroup;
142 }
143
144 qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
145 if (!qgroup)
146 return ERR_PTR(-ENOMEM);
147
148 qgroup->qgroupid = qgroupid;
149 INIT_LIST_HEAD(&qgroup->groups);
150 INIT_LIST_HEAD(&qgroup->members);
151 INIT_LIST_HEAD(&qgroup->dirty);
152
153 rb_link_node(&qgroup->node, parent, p);
154 rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
155
156 return qgroup;
157}
158
4082bd3d 159static void __del_qgroup_rb(struct btrfs_qgroup *qgroup)
bed92eae 160{
bed92eae
AJ
161 struct btrfs_qgroup_list *list;
162
bed92eae 163 list_del(&qgroup->dirty);
bed92eae
AJ
164 while (!list_empty(&qgroup->groups)) {
165 list = list_first_entry(&qgroup->groups,
166 struct btrfs_qgroup_list, next_group);
167 list_del(&list->next_group);
168 list_del(&list->next_member);
169 kfree(list);
170 }
171
172 while (!list_empty(&qgroup->members)) {
173 list = list_first_entry(&qgroup->members,
174 struct btrfs_qgroup_list, next_member);
175 list_del(&list->next_group);
176 list_del(&list->next_member);
177 kfree(list);
178 }
179 kfree(qgroup);
4082bd3d 180}
bed92eae 181
4082bd3d
WS
182/* must be called with qgroup_lock held */
183static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
184{
185 struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
186
187 if (!qgroup)
188 return -ENOENT;
189
190 rb_erase(&qgroup->node, &fs_info->qgroup_tree);
191 __del_qgroup_rb(qgroup);
bed92eae
AJ
192 return 0;
193}
194
195/* must be called with qgroup_lock held */
196static int add_relation_rb(struct btrfs_fs_info *fs_info,
197 u64 memberid, u64 parentid)
198{
199 struct btrfs_qgroup *member;
200 struct btrfs_qgroup *parent;
201 struct btrfs_qgroup_list *list;
202
203 member = find_qgroup_rb(fs_info, memberid);
204 parent = find_qgroup_rb(fs_info, parentid);
205 if (!member || !parent)
206 return -ENOENT;
207
208 list = kzalloc(sizeof(*list), GFP_ATOMIC);
209 if (!list)
210 return -ENOMEM;
211
212 list->group = parent;
213 list->member = member;
214 list_add_tail(&list->next_group, &member->groups);
215 list_add_tail(&list->next_member, &parent->members);
216
217 return 0;
218}
219
220/* must be called with qgroup_lock held */
221static int del_relation_rb(struct btrfs_fs_info *fs_info,
222 u64 memberid, u64 parentid)
223{
224 struct btrfs_qgroup *member;
225 struct btrfs_qgroup *parent;
226 struct btrfs_qgroup_list *list;
227
228 member = find_qgroup_rb(fs_info, memberid);
229 parent = find_qgroup_rb(fs_info, parentid);
230 if (!member || !parent)
231 return -ENOENT;
232
233 list_for_each_entry(list, &member->groups, next_group) {
234 if (list->group == parent) {
235 list_del(&list->next_group);
236 list_del(&list->next_member);
237 kfree(list);
238 return 0;
239 }
240 }
241 return -ENOENT;
242}
243
faa2dbf0
JB
244#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
245int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
246 u64 rfer, u64 excl)
247{
248 struct btrfs_qgroup *qgroup;
249
250 qgroup = find_qgroup_rb(fs_info, qgroupid);
251 if (!qgroup)
252 return -EINVAL;
253 if (qgroup->rfer != rfer || qgroup->excl != excl)
254 return -EINVAL;
255 return 0;
256}
257#endif
258
bed92eae
AJ
259/*
260 * The full config is read in one go, only called from open_ctree()
261 * It doesn't use any locking, as at this point we're still single-threaded
262 */
263int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
264{
265 struct btrfs_key key;
266 struct btrfs_key found_key;
267 struct btrfs_root *quota_root = fs_info->quota_root;
268 struct btrfs_path *path = NULL;
269 struct extent_buffer *l;
270 int slot;
271 int ret = 0;
272 u64 flags = 0;
b382a324 273 u64 rescan_progress = 0;
bed92eae 274
afcdd129 275 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
bed92eae
AJ
276 return 0;
277
323b88f4 278 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
1e8f9158
WS
279 if (!fs_info->qgroup_ulist) {
280 ret = -ENOMEM;
281 goto out;
282 }
283
bed92eae
AJ
284 path = btrfs_alloc_path();
285 if (!path) {
286 ret = -ENOMEM;
287 goto out;
288 }
289
290 /* default this to quota off, in case no status key is found */
291 fs_info->qgroup_flags = 0;
292
293 /*
294 * pass 1: read status, all qgroup infos and limits
295 */
296 key.objectid = 0;
297 key.type = 0;
298 key.offset = 0;
299 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
300 if (ret)
301 goto out;
302
303 while (1) {
304 struct btrfs_qgroup *qgroup;
305
306 slot = path->slots[0];
307 l = path->nodes[0];
308 btrfs_item_key_to_cpu(l, &found_key, slot);
309
310 if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
311 struct btrfs_qgroup_status_item *ptr;
312
313 ptr = btrfs_item_ptr(l, slot,
314 struct btrfs_qgroup_status_item);
315
316 if (btrfs_qgroup_status_version(l, ptr) !=
317 BTRFS_QGROUP_STATUS_VERSION) {
efe120a0
FH
318 btrfs_err(fs_info,
319 "old qgroup version, quota disabled");
bed92eae
AJ
320 goto out;
321 }
322 if (btrfs_qgroup_status_generation(l, ptr) !=
323 fs_info->generation) {
324 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
efe120a0 325 btrfs_err(fs_info,
5d163e0e 326 "qgroup generation mismatch, marked as inconsistent");
bed92eae
AJ
327 }
328 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
329 ptr);
b382a324 330 rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
bed92eae
AJ
331 goto next1;
332 }
333
334 if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
335 found_key.type != BTRFS_QGROUP_LIMIT_KEY)
336 goto next1;
337
338 qgroup = find_qgroup_rb(fs_info, found_key.offset);
339 if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
340 (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
d41e36a0 341 btrfs_err(fs_info, "inconsistent qgroup config");
bed92eae
AJ
342 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
343 }
344 if (!qgroup) {
345 qgroup = add_qgroup_rb(fs_info, found_key.offset);
346 if (IS_ERR(qgroup)) {
347 ret = PTR_ERR(qgroup);
348 goto out;
349 }
350 }
351 switch (found_key.type) {
352 case BTRFS_QGROUP_INFO_KEY: {
353 struct btrfs_qgroup_info_item *ptr;
354
355 ptr = btrfs_item_ptr(l, slot,
356 struct btrfs_qgroup_info_item);
357 qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
358 qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
359 qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
360 qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
361 /* generation currently unused */
362 break;
363 }
364 case BTRFS_QGROUP_LIMIT_KEY: {
365 struct btrfs_qgroup_limit_item *ptr;
366
367 ptr = btrfs_item_ptr(l, slot,
368 struct btrfs_qgroup_limit_item);
369 qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
370 qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
371 qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
372 qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
373 qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
374 break;
375 }
376 }
377next1:
378 ret = btrfs_next_item(quota_root, path);
379 if (ret < 0)
380 goto out;
381 if (ret)
382 break;
383 }
384 btrfs_release_path(path);
385
386 /*
387 * pass 2: read all qgroup relations
388 */
389 key.objectid = 0;
390 key.type = BTRFS_QGROUP_RELATION_KEY;
391 key.offset = 0;
392 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
393 if (ret)
394 goto out;
395 while (1) {
396 slot = path->slots[0];
397 l = path->nodes[0];
398 btrfs_item_key_to_cpu(l, &found_key, slot);
399
400 if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
401 goto next2;
402
403 if (found_key.objectid > found_key.offset) {
404 /* parent <- member, not needed to build config */
405 /* FIXME should we omit the key completely? */
406 goto next2;
407 }
408
409 ret = add_relation_rb(fs_info, found_key.objectid,
410 found_key.offset);
ff24858c 411 if (ret == -ENOENT) {
efe120a0
FH
412 btrfs_warn(fs_info,
413 "orphan qgroup relation 0x%llx->0x%llx",
c1c9ff7c 414 found_key.objectid, found_key.offset);
ff24858c
AJ
415 ret = 0; /* ignore the error */
416 }
bed92eae
AJ
417 if (ret)
418 goto out;
419next2:
420 ret = btrfs_next_item(quota_root, path);
421 if (ret < 0)
422 goto out;
423 if (ret)
424 break;
425 }
426out:
427 fs_info->qgroup_flags |= flags;
afcdd129
JB
428 if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
429 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
430 else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
431 ret >= 0)
b382a324 432 ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
bed92eae
AJ
433 btrfs_free_path(path);
434
eb1716af 435 if (ret < 0) {
1e8f9158 436 ulist_free(fs_info->qgroup_ulist);
eb1716af 437 fs_info->qgroup_ulist = NULL;
b382a324 438 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
eb1716af 439 }
1e8f9158 440
bed92eae
AJ
441 return ret < 0 ? ret : 0;
442}
443
444/*
e685da14
WS
445 * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
446 * first two are in single-threaded paths.And for the third one, we have set
447 * quota_root to be null with qgroup_lock held before, so it is safe to clean
448 * up the in-memory structures without qgroup_lock held.
bed92eae
AJ
449 */
450void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
451{
452 struct rb_node *n;
453 struct btrfs_qgroup *qgroup;
bed92eae
AJ
454
455 while ((n = rb_first(&fs_info->qgroup_tree))) {
456 qgroup = rb_entry(n, struct btrfs_qgroup, node);
457 rb_erase(n, &fs_info->qgroup_tree);
4082bd3d 458 __del_qgroup_rb(qgroup);
bed92eae 459 }
1e7bac1e
WS
460 /*
461 * we call btrfs_free_qgroup_config() when umounting
01327610 462 * filesystem and disabling quota, so we set qgroup_ulist
1e7bac1e
WS
463 * to be null here to avoid double free.
464 */
1e8f9158 465 ulist_free(fs_info->qgroup_ulist);
1e7bac1e 466 fs_info->qgroup_ulist = NULL;
bed92eae
AJ
467}
468
469static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
470 struct btrfs_root *quota_root,
471 u64 src, u64 dst)
472{
473 int ret;
474 struct btrfs_path *path;
475 struct btrfs_key key;
476
477 path = btrfs_alloc_path();
478 if (!path)
479 return -ENOMEM;
480
481 key.objectid = src;
482 key.type = BTRFS_QGROUP_RELATION_KEY;
483 key.offset = dst;
484
485 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
486
487 btrfs_mark_buffer_dirty(path->nodes[0]);
488
489 btrfs_free_path(path);
490 return ret;
491}
492
493static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
494 struct btrfs_root *quota_root,
495 u64 src, u64 dst)
496{
497 int ret;
498 struct btrfs_path *path;
499 struct btrfs_key key;
500
501 path = btrfs_alloc_path();
502 if (!path)
503 return -ENOMEM;
504
505 key.objectid = src;
506 key.type = BTRFS_QGROUP_RELATION_KEY;
507 key.offset = dst;
508
509 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
510 if (ret < 0)
511 goto out;
512
513 if (ret > 0) {
514 ret = -ENOENT;
515 goto out;
516 }
517
518 ret = btrfs_del_item(trans, quota_root, path);
519out:
520 btrfs_free_path(path);
521 return ret;
522}
523
524static int add_qgroup_item(struct btrfs_trans_handle *trans,
525 struct btrfs_root *quota_root, u64 qgroupid)
526{
527 int ret;
528 struct btrfs_path *path;
529 struct btrfs_qgroup_info_item *qgroup_info;
530 struct btrfs_qgroup_limit_item *qgroup_limit;
531 struct extent_buffer *leaf;
532 struct btrfs_key key;
533
f5ee5c9a 534 if (btrfs_is_testing(quota_root->fs_info))
faa2dbf0 535 return 0;
fccb84c9 536
bed92eae
AJ
537 path = btrfs_alloc_path();
538 if (!path)
539 return -ENOMEM;
540
541 key.objectid = 0;
542 key.type = BTRFS_QGROUP_INFO_KEY;
543 key.offset = qgroupid;
544
0b4699dc
MF
545 /*
546 * Avoid a transaction abort by catching -EEXIST here. In that
547 * case, we proceed by re-initializing the existing structure
548 * on disk.
549 */
550
bed92eae
AJ
551 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
552 sizeof(*qgroup_info));
0b4699dc 553 if (ret && ret != -EEXIST)
bed92eae
AJ
554 goto out;
555
556 leaf = path->nodes[0];
557 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
558 struct btrfs_qgroup_info_item);
559 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
560 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
561 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
562 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
563 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
564
565 btrfs_mark_buffer_dirty(leaf);
566
567 btrfs_release_path(path);
568
569 key.type = BTRFS_QGROUP_LIMIT_KEY;
570 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
571 sizeof(*qgroup_limit));
0b4699dc 572 if (ret && ret != -EEXIST)
bed92eae
AJ
573 goto out;
574
575 leaf = path->nodes[0];
576 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
577 struct btrfs_qgroup_limit_item);
578 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
579 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
580 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
581 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
582 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
583
584 btrfs_mark_buffer_dirty(leaf);
585
586 ret = 0;
587out:
588 btrfs_free_path(path);
589 return ret;
590}
591
592static int del_qgroup_item(struct btrfs_trans_handle *trans,
593 struct btrfs_root *quota_root, u64 qgroupid)
594{
595 int ret;
596 struct btrfs_path *path;
597 struct btrfs_key key;
598
599 path = btrfs_alloc_path();
600 if (!path)
601 return -ENOMEM;
602
603 key.objectid = 0;
604 key.type = BTRFS_QGROUP_INFO_KEY;
605 key.offset = qgroupid;
606 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
607 if (ret < 0)
608 goto out;
609
610 if (ret > 0) {
611 ret = -ENOENT;
612 goto out;
613 }
614
615 ret = btrfs_del_item(trans, quota_root, path);
616 if (ret)
617 goto out;
618
619 btrfs_release_path(path);
620
621 key.type = BTRFS_QGROUP_LIMIT_KEY;
622 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
623 if (ret < 0)
624 goto out;
625
626 if (ret > 0) {
627 ret = -ENOENT;
628 goto out;
629 }
630
631 ret = btrfs_del_item(trans, quota_root, path);
632
633out:
634 btrfs_free_path(path);
635 return ret;
636}
637
638static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
1510e71c
DY
639 struct btrfs_root *root,
640 struct btrfs_qgroup *qgroup)
bed92eae
AJ
641{
642 struct btrfs_path *path;
643 struct btrfs_key key;
644 struct extent_buffer *l;
645 struct btrfs_qgroup_limit_item *qgroup_limit;
646 int ret;
647 int slot;
648
649 key.objectid = 0;
650 key.type = BTRFS_QGROUP_LIMIT_KEY;
1510e71c 651 key.offset = qgroup->qgroupid;
bed92eae
AJ
652
653 path = btrfs_alloc_path();
84cbe2f7
WS
654 if (!path)
655 return -ENOMEM;
656
bed92eae
AJ
657 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
658 if (ret > 0)
659 ret = -ENOENT;
660
661 if (ret)
662 goto out;
663
664 l = path->nodes[0];
665 slot = path->slots[0];
a3df41ee 666 qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
1510e71c
DY
667 btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
668 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
669 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
670 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
671 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
bed92eae
AJ
672
673 btrfs_mark_buffer_dirty(l);
674
675out:
676 btrfs_free_path(path);
677 return ret;
678}
679
680static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
681 struct btrfs_root *root,
682 struct btrfs_qgroup *qgroup)
683{
684 struct btrfs_path *path;
685 struct btrfs_key key;
686 struct extent_buffer *l;
687 struct btrfs_qgroup_info_item *qgroup_info;
688 int ret;
689 int slot;
690
f5ee5c9a 691 if (btrfs_is_testing(root->fs_info))
faa2dbf0 692 return 0;
fccb84c9 693
bed92eae
AJ
694 key.objectid = 0;
695 key.type = BTRFS_QGROUP_INFO_KEY;
696 key.offset = qgroup->qgroupid;
697
698 path = btrfs_alloc_path();
84cbe2f7
WS
699 if (!path)
700 return -ENOMEM;
701
bed92eae
AJ
702 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
703 if (ret > 0)
704 ret = -ENOENT;
705
706 if (ret)
707 goto out;
708
709 l = path->nodes[0];
710 slot = path->slots[0];
a3df41ee 711 qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
bed92eae
AJ
712 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
713 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
714 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
715 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
716 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
717
718 btrfs_mark_buffer_dirty(l);
719
720out:
721 btrfs_free_path(path);
722 return ret;
723}
724
725static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
726 struct btrfs_fs_info *fs_info,
727 struct btrfs_root *root)
728{
729 struct btrfs_path *path;
730 struct btrfs_key key;
731 struct extent_buffer *l;
732 struct btrfs_qgroup_status_item *ptr;
733 int ret;
734 int slot;
735
736 key.objectid = 0;
737 key.type = BTRFS_QGROUP_STATUS_KEY;
738 key.offset = 0;
739
740 path = btrfs_alloc_path();
84cbe2f7
WS
741 if (!path)
742 return -ENOMEM;
743
bed92eae
AJ
744 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
745 if (ret > 0)
746 ret = -ENOENT;
747
748 if (ret)
749 goto out;
750
751 l = path->nodes[0];
752 slot = path->slots[0];
753 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
754 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
755 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
2f232036
JS
756 btrfs_set_qgroup_status_rescan(l, ptr,
757 fs_info->qgroup_rescan_progress.objectid);
bed92eae
AJ
758
759 btrfs_mark_buffer_dirty(l);
760
761out:
762 btrfs_free_path(path);
763 return ret;
764}
765
766/*
767 * called with qgroup_lock held
768 */
769static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
770 struct btrfs_root *root)
771{
772 struct btrfs_path *path;
773 struct btrfs_key key;
06b3a860 774 struct extent_buffer *leaf = NULL;
bed92eae 775 int ret;
06b3a860 776 int nr = 0;
bed92eae 777
bed92eae
AJ
778 path = btrfs_alloc_path();
779 if (!path)
780 return -ENOMEM;
781
06b3a860
WS
782 path->leave_spinning = 1;
783
784 key.objectid = 0;
785 key.offset = 0;
786 key.type = 0;
bed92eae 787
06b3a860 788 while (1) {
bed92eae 789 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
06b3a860
WS
790 if (ret < 0)
791 goto out;
792 leaf = path->nodes[0];
793 nr = btrfs_header_nritems(leaf);
794 if (!nr)
bed92eae 795 break;
06b3a860
WS
796 /*
797 * delete the leaf one by one
798 * since the whole tree is going
799 * to be deleted.
800 */
801 path->slots[0] = 0;
802 ret = btrfs_del_items(trans, root, path, 0, nr);
bed92eae
AJ
803 if (ret)
804 goto out;
06b3a860 805
bed92eae
AJ
806 btrfs_release_path(path);
807 }
808 ret = 0;
809out:
afcdd129 810 set_bit(BTRFS_FS_QUOTA_DISABLING, &root->fs_info->flags);
bed92eae
AJ
811 btrfs_free_path(path);
812 return ret;
813}
814
815int btrfs_quota_enable(struct btrfs_trans_handle *trans,
816 struct btrfs_fs_info *fs_info)
817{
818 struct btrfs_root *quota_root;
7708f029 819 struct btrfs_root *tree_root = fs_info->tree_root;
bed92eae
AJ
820 struct btrfs_path *path = NULL;
821 struct btrfs_qgroup_status_item *ptr;
822 struct extent_buffer *leaf;
823 struct btrfs_key key;
7708f029
WS
824 struct btrfs_key found_key;
825 struct btrfs_qgroup *qgroup = NULL;
bed92eae 826 int ret = 0;
7708f029 827 int slot;
bed92eae 828
f2f6ed3d 829 mutex_lock(&fs_info->qgroup_ioctl_lock);
bed92eae 830 if (fs_info->quota_root) {
afcdd129 831 set_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags);
bed92eae
AJ
832 goto out;
833 }
bed92eae 834
52bf8e7a 835 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
1e8f9158
WS
836 if (!fs_info->qgroup_ulist) {
837 ret = -ENOMEM;
838 goto out;
839 }
840
bed92eae
AJ
841 /*
842 * initially create the quota tree
843 */
844 quota_root = btrfs_create_tree(trans, fs_info,
845 BTRFS_QUOTA_TREE_OBJECTID);
846 if (IS_ERR(quota_root)) {
847 ret = PTR_ERR(quota_root);
848 goto out;
849 }
850
851 path = btrfs_alloc_path();
5b7ff5b3
TI
852 if (!path) {
853 ret = -ENOMEM;
854 goto out_free_root;
855 }
bed92eae
AJ
856
857 key.objectid = 0;
858 key.type = BTRFS_QGROUP_STATUS_KEY;
859 key.offset = 0;
860
861 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
862 sizeof(*ptr));
863 if (ret)
5b7ff5b3 864 goto out_free_path;
bed92eae
AJ
865
866 leaf = path->nodes[0];
867 ptr = btrfs_item_ptr(leaf, path->slots[0],
868 struct btrfs_qgroup_status_item);
869 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
870 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
871 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
872 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
873 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
2f232036 874 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
bed92eae
AJ
875
876 btrfs_mark_buffer_dirty(leaf);
877
7708f029
WS
878 key.objectid = 0;
879 key.type = BTRFS_ROOT_REF_KEY;
880 key.offset = 0;
881
882 btrfs_release_path(path);
883 ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
884 if (ret > 0)
885 goto out_add_root;
886 if (ret < 0)
887 goto out_free_path;
888
889
890 while (1) {
891 slot = path->slots[0];
892 leaf = path->nodes[0];
893 btrfs_item_key_to_cpu(leaf, &found_key, slot);
894
895 if (found_key.type == BTRFS_ROOT_REF_KEY) {
896 ret = add_qgroup_item(trans, quota_root,
897 found_key.offset);
898 if (ret)
899 goto out_free_path;
900
7708f029
WS
901 qgroup = add_qgroup_rb(fs_info, found_key.offset);
902 if (IS_ERR(qgroup)) {
7708f029
WS
903 ret = PTR_ERR(qgroup);
904 goto out_free_path;
905 }
7708f029
WS
906 }
907 ret = btrfs_next_item(tree_root, path);
908 if (ret < 0)
909 goto out_free_path;
910 if (ret)
911 break;
912 }
913
914out_add_root:
915 btrfs_release_path(path);
916 ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
917 if (ret)
918 goto out_free_path;
919
7708f029
WS
920 qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
921 if (IS_ERR(qgroup)) {
7708f029
WS
922 ret = PTR_ERR(qgroup);
923 goto out_free_path;
924 }
58400fce 925 spin_lock(&fs_info->qgroup_lock);
bed92eae 926 fs_info->quota_root = quota_root;
afcdd129 927 set_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags);
bed92eae 928 spin_unlock(&fs_info->qgroup_lock);
5b7ff5b3 929out_free_path:
bed92eae 930 btrfs_free_path(path);
5b7ff5b3
TI
931out_free_root:
932 if (ret) {
933 free_extent_buffer(quota_root->node);
934 free_extent_buffer(quota_root->commit_root);
935 kfree(quota_root);
936 }
937out:
eb1716af 938 if (ret) {
1e8f9158 939 ulist_free(fs_info->qgroup_ulist);
eb1716af
JS
940 fs_info->qgroup_ulist = NULL;
941 }
f2f6ed3d 942 mutex_unlock(&fs_info->qgroup_ioctl_lock);
bed92eae
AJ
943 return ret;
944}
945
946int btrfs_quota_disable(struct btrfs_trans_handle *trans,
947 struct btrfs_fs_info *fs_info)
948{
949 struct btrfs_root *tree_root = fs_info->tree_root;
950 struct btrfs_root *quota_root;
951 int ret = 0;
952
f2f6ed3d 953 mutex_lock(&fs_info->qgroup_ioctl_lock);
58400fce 954 if (!fs_info->quota_root)
f2f6ed3d 955 goto out;
afcdd129
JB
956 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
957 set_bit(BTRFS_FS_QUOTA_DISABLING, &fs_info->flags);
d06f23d6 958 btrfs_qgroup_wait_for_completion(fs_info, false);
967ef513 959 spin_lock(&fs_info->qgroup_lock);
bed92eae
AJ
960 quota_root = fs_info->quota_root;
961 fs_info->quota_root = NULL;
8ea0ec9e 962 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
bed92eae
AJ
963 spin_unlock(&fs_info->qgroup_lock);
964
e685da14
WS
965 btrfs_free_qgroup_config(fs_info);
966
bed92eae
AJ
967 ret = btrfs_clean_quota_tree(trans, quota_root);
968 if (ret)
969 goto out;
970
971 ret = btrfs_del_root(trans, tree_root, &quota_root->root_key);
972 if (ret)
973 goto out;
974
975 list_del(&quota_root->dirty_list);
976
977 btrfs_tree_lock(quota_root->node);
7c302b49 978 clean_tree_block(fs_info, quota_root->node);
bed92eae
AJ
979 btrfs_tree_unlock(quota_root->node);
980 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
981
982 free_extent_buffer(quota_root->node);
983 free_extent_buffer(quota_root->commit_root);
984 kfree(quota_root);
985out:
f2f6ed3d 986 mutex_unlock(&fs_info->qgroup_ioctl_lock);
bed92eae
AJ
987 return ret;
988}
989
2f232036
JS
990static void qgroup_dirty(struct btrfs_fs_info *fs_info,
991 struct btrfs_qgroup *qgroup)
bed92eae 992{
2f232036
JS
993 if (list_empty(&qgroup->dirty))
994 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
bed92eae
AJ
995}
996
18dc22c1
QW
997static void report_reserved_underflow(struct btrfs_fs_info *fs_info,
998 struct btrfs_qgroup *qgroup,
999 u64 num_bytes)
1000{
1001 btrfs_warn(fs_info,
1002 "qgroup %llu reserved space underflow, have: %llu, to free: %llu",
1003 qgroup->qgroupid, qgroup->reserved, num_bytes);
1004 qgroup->reserved = 0;
1005}
9c8b35b1
QW
1006/*
1007 * The easy accounting, if we are adding/removing the only ref for an extent
01327610 1008 * then this qgroup and all of the parent qgroups get their reference and
9c8b35b1
QW
1009 * exclusive counts adjusted.
1010 *
1011 * Caller should hold fs_info->qgroup_lock.
1012 */
1013static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
1014 struct ulist *tmp, u64 ref_root,
1015 u64 num_bytes, int sign)
1016{
1017 struct btrfs_qgroup *qgroup;
1018 struct btrfs_qgroup_list *glist;
1019 struct ulist_node *unode;
1020 struct ulist_iterator uiter;
1021 int ret = 0;
1022
1023 qgroup = find_qgroup_rb(fs_info, ref_root);
1024 if (!qgroup)
1025 goto out;
1026
1027 qgroup->rfer += sign * num_bytes;
1028 qgroup->rfer_cmpr += sign * num_bytes;
1029
1030 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1031 qgroup->excl += sign * num_bytes;
1032 qgroup->excl_cmpr += sign * num_bytes;
18dc22c1 1033 if (sign > 0) {
3159fe7b 1034 trace_qgroup_update_reserve(fs_info, qgroup, -(s64)num_bytes);
18dc22c1
QW
1035 if (WARN_ON(qgroup->reserved < num_bytes))
1036 report_reserved_underflow(fs_info, qgroup, num_bytes);
1037 else
1038 qgroup->reserved -= num_bytes;
1039 }
9c8b35b1
QW
1040
1041 qgroup_dirty(fs_info, qgroup);
1042
1043 /* Get all of the parent groups that contain this qgroup */
1044 list_for_each_entry(glist, &qgroup->groups, next_group) {
1045 ret = ulist_add(tmp, glist->group->qgroupid,
ef2fff64 1046 qgroup_to_aux(glist->group), GFP_ATOMIC);
9c8b35b1
QW
1047 if (ret < 0)
1048 goto out;
1049 }
1050
1051 /* Iterate all of the parents and adjust their reference counts */
1052 ULIST_ITER_INIT(&uiter);
1053 while ((unode = ulist_next(tmp, &uiter))) {
ef2fff64 1054 qgroup = unode_aux_to_qgroup(unode);
9c8b35b1
QW
1055 qgroup->rfer += sign * num_bytes;
1056 qgroup->rfer_cmpr += sign * num_bytes;
1057 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1058 qgroup->excl += sign * num_bytes;
18dc22c1 1059 if (sign > 0) {
3159fe7b
QW
1060 trace_qgroup_update_reserve(fs_info, qgroup,
1061 -(s64)num_bytes);
18dc22c1
QW
1062 if (WARN_ON(qgroup->reserved < num_bytes))
1063 report_reserved_underflow(fs_info, qgroup,
1064 num_bytes);
1065 else
1066 qgroup->reserved -= num_bytes;
1067 }
9c8b35b1
QW
1068 qgroup->excl_cmpr += sign * num_bytes;
1069 qgroup_dirty(fs_info, qgroup);
1070
1071 /* Add any parents of the parents */
1072 list_for_each_entry(glist, &qgroup->groups, next_group) {
1073 ret = ulist_add(tmp, glist->group->qgroupid,
ef2fff64 1074 qgroup_to_aux(glist->group), GFP_ATOMIC);
9c8b35b1
QW
1075 if (ret < 0)
1076 goto out;
1077 }
1078 }
1079 ret = 0;
1080out:
1081 return ret;
1082}
1083
1084
1085/*
1086 * Quick path for updating qgroup with only excl refs.
1087 *
1088 * In that case, just update all parent will be enough.
1089 * Or we needs to do a full rescan.
1090 * Caller should also hold fs_info->qgroup_lock.
1091 *
1092 * Return 0 for quick update, return >0 for need to full rescan
1093 * and mark INCONSISTENT flag.
1094 * Return < 0 for other error.
1095 */
1096static int quick_update_accounting(struct btrfs_fs_info *fs_info,
1097 struct ulist *tmp, u64 src, u64 dst,
1098 int sign)
1099{
1100 struct btrfs_qgroup *qgroup;
1101 int ret = 1;
1102 int err = 0;
1103
1104 qgroup = find_qgroup_rb(fs_info, src);
1105 if (!qgroup)
1106 goto out;
1107 if (qgroup->excl == qgroup->rfer) {
1108 ret = 0;
1109 err = __qgroup_excl_accounting(fs_info, tmp, dst,
1110 qgroup->excl, sign);
1111 if (err < 0) {
1112 ret = err;
1113 goto out;
1114 }
1115 }
1116out:
1117 if (ret)
1118 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1119 return ret;
1120}
1121
bed92eae
AJ
1122int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
1123 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1124{
1125 struct btrfs_root *quota_root;
b7fef4f5
WS
1126 struct btrfs_qgroup *parent;
1127 struct btrfs_qgroup *member;
534e6623 1128 struct btrfs_qgroup_list *list;
9c8b35b1 1129 struct ulist *tmp;
bed92eae
AJ
1130 int ret = 0;
1131
8465ecec
QW
1132 /* Check the level of src and dst first */
1133 if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
1134 return -EINVAL;
1135
6602caf1 1136 tmp = ulist_alloc(GFP_KERNEL);
ab3680dd
CE
1137 if (!tmp)
1138 return -ENOMEM;
1139
f2f6ed3d 1140 mutex_lock(&fs_info->qgroup_ioctl_lock);
bed92eae 1141 quota_root = fs_info->quota_root;
f2f6ed3d
WS
1142 if (!quota_root) {
1143 ret = -EINVAL;
1144 goto out;
1145 }
b7fef4f5
WS
1146 member = find_qgroup_rb(fs_info, src);
1147 parent = find_qgroup_rb(fs_info, dst);
1148 if (!member || !parent) {
1149 ret = -EINVAL;
1150 goto out;
1151 }
bed92eae 1152
534e6623
WS
1153 /* check if such qgroup relation exist firstly */
1154 list_for_each_entry(list, &member->groups, next_group) {
1155 if (list->group == parent) {
1156 ret = -EEXIST;
1157 goto out;
1158 }
1159 }
1160
bed92eae
AJ
1161 ret = add_qgroup_relation_item(trans, quota_root, src, dst);
1162 if (ret)
f2f6ed3d 1163 goto out;
bed92eae
AJ
1164
1165 ret = add_qgroup_relation_item(trans, quota_root, dst, src);
1166 if (ret) {
1167 del_qgroup_relation_item(trans, quota_root, src, dst);
f2f6ed3d 1168 goto out;
bed92eae
AJ
1169 }
1170
1171 spin_lock(&fs_info->qgroup_lock);
0b246afa 1172 ret = add_relation_rb(fs_info, src, dst);
9c8b35b1
QW
1173 if (ret < 0) {
1174 spin_unlock(&fs_info->qgroup_lock);
1175 goto out;
1176 }
1177 ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
bed92eae 1178 spin_unlock(&fs_info->qgroup_lock);
f2f6ed3d
WS
1179out:
1180 mutex_unlock(&fs_info->qgroup_ioctl_lock);
9c8b35b1 1181 ulist_free(tmp);
bed92eae
AJ
1182 return ret;
1183}
1184
025db916 1185static int __del_qgroup_relation(struct btrfs_trans_handle *trans,
bed92eae
AJ
1186 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1187{
1188 struct btrfs_root *quota_root;
534e6623
WS
1189 struct btrfs_qgroup *parent;
1190 struct btrfs_qgroup *member;
1191 struct btrfs_qgroup_list *list;
9c8b35b1 1192 struct ulist *tmp;
bed92eae
AJ
1193 int ret = 0;
1194 int err;
1195
6602caf1 1196 tmp = ulist_alloc(GFP_KERNEL);
9c8b35b1
QW
1197 if (!tmp)
1198 return -ENOMEM;
1199
bed92eae 1200 quota_root = fs_info->quota_root;
f2f6ed3d
WS
1201 if (!quota_root) {
1202 ret = -EINVAL;
1203 goto out;
1204 }
bed92eae 1205
534e6623
WS
1206 member = find_qgroup_rb(fs_info, src);
1207 parent = find_qgroup_rb(fs_info, dst);
1208 if (!member || !parent) {
1209 ret = -EINVAL;
1210 goto out;
1211 }
1212
1213 /* check if such qgroup relation exist firstly */
1214 list_for_each_entry(list, &member->groups, next_group) {
1215 if (list->group == parent)
1216 goto exist;
1217 }
1218 ret = -ENOENT;
1219 goto out;
1220exist:
bed92eae
AJ
1221 ret = del_qgroup_relation_item(trans, quota_root, src, dst);
1222 err = del_qgroup_relation_item(trans, quota_root, dst, src);
1223 if (err && !ret)
1224 ret = err;
1225
1226 spin_lock(&fs_info->qgroup_lock);
1227 del_relation_rb(fs_info, src, dst);
9c8b35b1 1228 ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
bed92eae 1229 spin_unlock(&fs_info->qgroup_lock);
f2f6ed3d 1230out:
9c8b35b1 1231 ulist_free(tmp);
f5a6b1c5
DY
1232 return ret;
1233}
1234
1235int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
1236 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1237{
1238 int ret = 0;
1239
1240 mutex_lock(&fs_info->qgroup_ioctl_lock);
1241 ret = __del_qgroup_relation(trans, fs_info, src, dst);
f2f6ed3d 1242 mutex_unlock(&fs_info->qgroup_ioctl_lock);
f5a6b1c5 1243
bed92eae
AJ
1244 return ret;
1245}
1246
1247int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
4087cf24 1248 struct btrfs_fs_info *fs_info, u64 qgroupid)
bed92eae
AJ
1249{
1250 struct btrfs_root *quota_root;
1251 struct btrfs_qgroup *qgroup;
1252 int ret = 0;
1253
f2f6ed3d 1254 mutex_lock(&fs_info->qgroup_ioctl_lock);
bed92eae 1255 quota_root = fs_info->quota_root;
f2f6ed3d
WS
1256 if (!quota_root) {
1257 ret = -EINVAL;
1258 goto out;
1259 }
534e6623
WS
1260 qgroup = find_qgroup_rb(fs_info, qgroupid);
1261 if (qgroup) {
1262 ret = -EEXIST;
1263 goto out;
1264 }
bed92eae
AJ
1265
1266 ret = add_qgroup_item(trans, quota_root, qgroupid);
534e6623
WS
1267 if (ret)
1268 goto out;
bed92eae
AJ
1269
1270 spin_lock(&fs_info->qgroup_lock);
1271 qgroup = add_qgroup_rb(fs_info, qgroupid);
1272 spin_unlock(&fs_info->qgroup_lock);
1273
1274 if (IS_ERR(qgroup))
1275 ret = PTR_ERR(qgroup);
f2f6ed3d
WS
1276out:
1277 mutex_unlock(&fs_info->qgroup_ioctl_lock);
bed92eae
AJ
1278 return ret;
1279}
1280
1281int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
1282 struct btrfs_fs_info *fs_info, u64 qgroupid)
1283{
1284 struct btrfs_root *quota_root;
2cf68703 1285 struct btrfs_qgroup *qgroup;
f5a6b1c5 1286 struct btrfs_qgroup_list *list;
bed92eae
AJ
1287 int ret = 0;
1288
f2f6ed3d 1289 mutex_lock(&fs_info->qgroup_ioctl_lock);
bed92eae 1290 quota_root = fs_info->quota_root;
f2f6ed3d
WS
1291 if (!quota_root) {
1292 ret = -EINVAL;
1293 goto out;
1294 }
bed92eae 1295
2cf68703 1296 qgroup = find_qgroup_rb(fs_info, qgroupid);
534e6623
WS
1297 if (!qgroup) {
1298 ret = -ENOENT;
1299 goto out;
1300 } else {
f5a6b1c5
DY
1301 /* check if there are no children of this qgroup */
1302 if (!list_empty(&qgroup->members)) {
f2f6ed3d
WS
1303 ret = -EBUSY;
1304 goto out;
2cf68703
AJ
1305 }
1306 }
bed92eae
AJ
1307 ret = del_qgroup_item(trans, quota_root, qgroupid);
1308
f5a6b1c5
DY
1309 while (!list_empty(&qgroup->groups)) {
1310 list = list_first_entry(&qgroup->groups,
1311 struct btrfs_qgroup_list, next_group);
1312 ret = __del_qgroup_relation(trans, fs_info,
1313 qgroupid,
1314 list->group->qgroupid);
1315 if (ret)
1316 goto out;
1317 }
1318
bed92eae 1319 spin_lock(&fs_info->qgroup_lock);
0b246afa 1320 del_qgroup_rb(fs_info, qgroupid);
bed92eae 1321 spin_unlock(&fs_info->qgroup_lock);
f2f6ed3d
WS
1322out:
1323 mutex_unlock(&fs_info->qgroup_ioctl_lock);
bed92eae
AJ
1324 return ret;
1325}
1326
1327int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
1328 struct btrfs_fs_info *fs_info, u64 qgroupid,
1329 struct btrfs_qgroup_limit *limit)
1330{
f2f6ed3d 1331 struct btrfs_root *quota_root;
bed92eae
AJ
1332 struct btrfs_qgroup *qgroup;
1333 int ret = 0;
fe759907
YD
1334 /* Sometimes we would want to clear the limit on this qgroup.
1335 * To meet this requirement, we treat the -1 as a special value
1336 * which tell kernel to clear the limit on this qgroup.
1337 */
1338 const u64 CLEAR_VALUE = -1;
bed92eae 1339
f2f6ed3d
WS
1340 mutex_lock(&fs_info->qgroup_ioctl_lock);
1341 quota_root = fs_info->quota_root;
1342 if (!quota_root) {
1343 ret = -EINVAL;
1344 goto out;
1345 }
bed92eae 1346
ddb47afa
WS
1347 qgroup = find_qgroup_rb(fs_info, qgroupid);
1348 if (!qgroup) {
1349 ret = -ENOENT;
1350 goto out;
1351 }
bed92eae 1352
58400fce 1353 spin_lock(&fs_info->qgroup_lock);
fe759907
YD
1354 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
1355 if (limit->max_rfer == CLEAR_VALUE) {
1356 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1357 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1358 qgroup->max_rfer = 0;
1359 } else {
1360 qgroup->max_rfer = limit->max_rfer;
1361 }
1362 }
1363 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
1364 if (limit->max_excl == CLEAR_VALUE) {
1365 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1366 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1367 qgroup->max_excl = 0;
1368 } else {
1369 qgroup->max_excl = limit->max_excl;
1370 }
1371 }
1372 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
1373 if (limit->rsv_rfer == CLEAR_VALUE) {
1374 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1375 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1376 qgroup->rsv_rfer = 0;
1377 } else {
1378 qgroup->rsv_rfer = limit->rsv_rfer;
1379 }
1380 }
1381 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
1382 if (limit->rsv_excl == CLEAR_VALUE) {
1383 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1384 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1385 qgroup->rsv_excl = 0;
1386 } else {
1387 qgroup->rsv_excl = limit->rsv_excl;
1388 }
1389 }
03477d94
DY
1390 qgroup->lim_flags |= limit->flags;
1391
bed92eae 1392 spin_unlock(&fs_info->qgroup_lock);
1510e71c
DY
1393
1394 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
1395 if (ret) {
1396 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1397 btrfs_info(fs_info, "unable to update quota limit for %llu",
1398 qgroupid);
1399 }
1400
f2f6ed3d
WS
1401out:
1402 mutex_unlock(&fs_info->qgroup_ioctl_lock);
bed92eae
AJ
1403 return ret;
1404}
1152651a 1405
3b7d00f9
QW
1406int btrfs_qgroup_prepare_account_extents(struct btrfs_trans_handle *trans,
1407 struct btrfs_fs_info *fs_info)
1408{
1409 struct btrfs_qgroup_extent_record *record;
1410 struct btrfs_delayed_ref_root *delayed_refs;
1411 struct rb_node *node;
9086db86 1412 u64 qgroup_to_skip;
3b7d00f9
QW
1413 int ret = 0;
1414
1415 delayed_refs = &trans->transaction->delayed_refs;
9086db86 1416 qgroup_to_skip = delayed_refs->qgroup_to_skip;
3b7d00f9
QW
1417
1418 /*
1419 * No need to do lock, since this function will only be called in
01327610 1420 * btrfs_commit_transaction().
3b7d00f9
QW
1421 */
1422 node = rb_first(&delayed_refs->dirty_extent_root);
1423 while (node) {
1424 record = rb_entry(node, struct btrfs_qgroup_extent_record,
1425 node);
fb235dc0
QW
1426 if (WARN_ON(!record->old_roots))
1427 ret = btrfs_find_all_roots(NULL, fs_info,
1428 record->bytenr, 0, &record->old_roots);
3b7d00f9
QW
1429 if (ret < 0)
1430 break;
9086db86
QW
1431 if (qgroup_to_skip)
1432 ulist_del(record->old_roots, qgroup_to_skip, 0);
3b7d00f9
QW
1433 node = rb_next(node);
1434 }
1435 return ret;
1436}
1437
50b3e040 1438int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
cb93b52c
QW
1439 struct btrfs_delayed_ref_root *delayed_refs,
1440 struct btrfs_qgroup_extent_record *record)
3368d001
QW
1441{
1442 struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
1443 struct rb_node *parent_node = NULL;
1444 struct btrfs_qgroup_extent_record *entry;
1445 u64 bytenr = record->bytenr;
1446
82bd101b 1447 assert_spin_locked(&delayed_refs->lock);
50b3e040 1448 trace_btrfs_qgroup_trace_extent(fs_info, record);
82bd101b 1449
3368d001
QW
1450 while (*p) {
1451 parent_node = *p;
1452 entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
1453 node);
1454 if (bytenr < entry->bytenr)
1455 p = &(*p)->rb_left;
1456 else if (bytenr > entry->bytenr)
1457 p = &(*p)->rb_right;
1458 else
cb93b52c 1459 return 1;
3368d001
QW
1460 }
1461
1462 rb_link_node(&record->node, parent_node, p);
1463 rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
cb93b52c
QW
1464 return 0;
1465}
1466
fb235dc0
QW
1467int btrfs_qgroup_trace_extent_post(struct btrfs_fs_info *fs_info,
1468 struct btrfs_qgroup_extent_record *qrecord)
1469{
1470 struct ulist *old_root;
1471 u64 bytenr = qrecord->bytenr;
1472 int ret;
1473
1474 ret = btrfs_find_all_roots(NULL, fs_info, bytenr, 0, &old_root);
1475 if (ret < 0)
1476 return ret;
1477
1478 /*
1479 * Here we don't need to get the lock of
1480 * trans->transaction->delayed_refs, since inserted qrecord won't
1481 * be deleted, only qrecord->node may be modified (new qrecord insert)
1482 *
1483 * So modifying qrecord->old_roots is safe here
1484 */
1485 qrecord->old_roots = old_root;
1486 return 0;
1487}
1488
50b3e040 1489int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans,
cb93b52c
QW
1490 struct btrfs_fs_info *fs_info, u64 bytenr, u64 num_bytes,
1491 gfp_t gfp_flag)
1492{
1493 struct btrfs_qgroup_extent_record *record;
1494 struct btrfs_delayed_ref_root *delayed_refs;
1495 int ret;
1496
afcdd129
JB
1497 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)
1498 || bytenr == 0 || num_bytes == 0)
cb93b52c
QW
1499 return 0;
1500 if (WARN_ON(trans == NULL))
1501 return -EINVAL;
1502 record = kmalloc(sizeof(*record), gfp_flag);
1503 if (!record)
1504 return -ENOMEM;
1505
1506 delayed_refs = &trans->transaction->delayed_refs;
1507 record->bytenr = bytenr;
1508 record->num_bytes = num_bytes;
1509 record->old_roots = NULL;
1510
1511 spin_lock(&delayed_refs->lock);
2ff7e61e 1512 ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record);
cb93b52c 1513 spin_unlock(&delayed_refs->lock);
fb235dc0 1514 if (ret > 0) {
cb93b52c 1515 kfree(record);
fb235dc0
QW
1516 return 0;
1517 }
1518 return btrfs_qgroup_trace_extent_post(fs_info, record);
3368d001
QW
1519}
1520
33d1f05c 1521int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
2ff7e61e 1522 struct btrfs_fs_info *fs_info,
33d1f05c
QW
1523 struct extent_buffer *eb)
1524{
1525 int nr = btrfs_header_nritems(eb);
1526 int i, extent_type, ret;
1527 struct btrfs_key key;
1528 struct btrfs_file_extent_item *fi;
1529 u64 bytenr, num_bytes;
1530
1531 /* We can be called directly from walk_up_proc() */
0b246afa 1532 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
33d1f05c
QW
1533 return 0;
1534
1535 for (i = 0; i < nr; i++) {
1536 btrfs_item_key_to_cpu(eb, &key, i);
1537
1538 if (key.type != BTRFS_EXTENT_DATA_KEY)
1539 continue;
1540
1541 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
1542 /* filter out non qgroup-accountable extents */
1543 extent_type = btrfs_file_extent_type(eb, fi);
1544
1545 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
1546 continue;
1547
1548 bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1549 if (!bytenr)
1550 continue;
1551
1552 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1553
0b246afa
JM
1554 ret = btrfs_qgroup_trace_extent(trans, fs_info, bytenr,
1555 num_bytes, GFP_NOFS);
33d1f05c
QW
1556 if (ret)
1557 return ret;
1558 }
1559 return 0;
1560}
1561
1562/*
1563 * Walk up the tree from the bottom, freeing leaves and any interior
1564 * nodes which have had all slots visited. If a node (leaf or
1565 * interior) is freed, the node above it will have it's slot
1566 * incremented. The root node will never be freed.
1567 *
1568 * At the end of this function, we should have a path which has all
1569 * slots incremented to the next position for a search. If we need to
1570 * read a new node it will be NULL and the node above it will have the
1571 * correct slot selected for a later read.
1572 *
1573 * If we increment the root nodes slot counter past the number of
1574 * elements, 1 is returned to signal completion of the search.
1575 */
15b34517 1576static int adjust_slots_upwards(struct btrfs_path *path, int root_level)
33d1f05c
QW
1577{
1578 int level = 0;
1579 int nr, slot;
1580 struct extent_buffer *eb;
1581
1582 if (root_level == 0)
1583 return 1;
1584
1585 while (level <= root_level) {
1586 eb = path->nodes[level];
1587 nr = btrfs_header_nritems(eb);
1588 path->slots[level]++;
1589 slot = path->slots[level];
1590 if (slot >= nr || level == 0) {
1591 /*
1592 * Don't free the root - we will detect this
1593 * condition after our loop and return a
1594 * positive value for caller to stop walking the tree.
1595 */
1596 if (level != root_level) {
1597 btrfs_tree_unlock_rw(eb, path->locks[level]);
1598 path->locks[level] = 0;
1599
1600 free_extent_buffer(eb);
1601 path->nodes[level] = NULL;
1602 path->slots[level] = 0;
1603 }
1604 } else {
1605 /*
1606 * We have a valid slot to walk back down
1607 * from. Stop here so caller can process these
1608 * new nodes.
1609 */
1610 break;
1611 }
1612
1613 level++;
1614 }
1615
1616 eb = path->nodes[root_level];
1617 if (path->slots[root_level] >= btrfs_header_nritems(eb))
1618 return 1;
1619
1620 return 0;
1621}
1622
1623int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
1624 struct btrfs_root *root,
1625 struct extent_buffer *root_eb,
1626 u64 root_gen, int root_level)
1627{
0b246afa 1628 struct btrfs_fs_info *fs_info = root->fs_info;
33d1f05c
QW
1629 int ret = 0;
1630 int level;
1631 struct extent_buffer *eb = root_eb;
1632 struct btrfs_path *path = NULL;
1633
1634 BUG_ON(root_level < 0 || root_level > BTRFS_MAX_LEVEL);
1635 BUG_ON(root_eb == NULL);
1636
0b246afa 1637 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
33d1f05c
QW
1638 return 0;
1639
1640 if (!extent_buffer_uptodate(root_eb)) {
1641 ret = btrfs_read_buffer(root_eb, root_gen);
1642 if (ret)
1643 goto out;
1644 }
1645
1646 if (root_level == 0) {
2ff7e61e 1647 ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, root_eb);
33d1f05c
QW
1648 goto out;
1649 }
1650
1651 path = btrfs_alloc_path();
1652 if (!path)
1653 return -ENOMEM;
1654
1655 /*
1656 * Walk down the tree. Missing extent blocks are filled in as
1657 * we go. Metadata is accounted every time we read a new
1658 * extent block.
1659 *
1660 * When we reach a leaf, we account for file extent items in it,
1661 * walk back up the tree (adjusting slot pointers as we go)
1662 * and restart the search process.
1663 */
1664 extent_buffer_get(root_eb); /* For path */
1665 path->nodes[root_level] = root_eb;
1666 path->slots[root_level] = 0;
1667 path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
1668walk_down:
1669 level = root_level;
1670 while (level >= 0) {
1671 if (path->nodes[level] == NULL) {
1672 int parent_slot;
1673 u64 child_gen;
1674 u64 child_bytenr;
1675
1676 /*
1677 * We need to get child blockptr/gen from parent before
1678 * we can read it.
1679 */
1680 eb = path->nodes[level + 1];
1681 parent_slot = path->slots[level + 1];
1682 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
1683 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
1684
2ff7e61e 1685 eb = read_tree_block(fs_info, child_bytenr, child_gen);
33d1f05c
QW
1686 if (IS_ERR(eb)) {
1687 ret = PTR_ERR(eb);
1688 goto out;
1689 } else if (!extent_buffer_uptodate(eb)) {
1690 free_extent_buffer(eb);
1691 ret = -EIO;
1692 goto out;
1693 }
1694
1695 path->nodes[level] = eb;
1696 path->slots[level] = 0;
1697
1698 btrfs_tree_read_lock(eb);
1699 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1700 path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
1701
0b246afa
JM
1702 ret = btrfs_qgroup_trace_extent(trans, fs_info,
1703 child_bytenr,
1704 fs_info->nodesize,
1705 GFP_NOFS);
33d1f05c
QW
1706 if (ret)
1707 goto out;
1708 }
1709
1710 if (level == 0) {
2ff7e61e
JM
1711 ret = btrfs_qgroup_trace_leaf_items(trans,fs_info,
1712 path->nodes[level]);
33d1f05c
QW
1713 if (ret)
1714 goto out;
1715
1716 /* Nonzero return here means we completed our search */
15b34517 1717 ret = adjust_slots_upwards(path, root_level);
33d1f05c
QW
1718 if (ret)
1719 break;
1720
1721 /* Restart search with new slots */
1722 goto walk_down;
1723 }
1724
1725 level--;
1726 }
1727
1728 ret = 0;
1729out:
1730 btrfs_free_path(path);
1731
1732 return ret;
1733}
1734
d810ef2b
QW
1735#define UPDATE_NEW 0
1736#define UPDATE_OLD 1
1737/*
1738 * Walk all of the roots that points to the bytenr and adjust their refcnts.
1739 */
1740static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
1741 struct ulist *roots, struct ulist *tmp,
1742 struct ulist *qgroups, u64 seq, int update_old)
1743{
1744 struct ulist_node *unode;
1745 struct ulist_iterator uiter;
1746 struct ulist_node *tmp_unode;
1747 struct ulist_iterator tmp_uiter;
1748 struct btrfs_qgroup *qg;
1749 int ret = 0;
1750
1751 if (!roots)
1752 return 0;
1753 ULIST_ITER_INIT(&uiter);
1754 while ((unode = ulist_next(roots, &uiter))) {
1755 qg = find_qgroup_rb(fs_info, unode->val);
1756 if (!qg)
1757 continue;
1758
1759 ulist_reinit(tmp);
ef2fff64 1760 ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg),
d810ef2b
QW
1761 GFP_ATOMIC);
1762 if (ret < 0)
1763 return ret;
ef2fff64 1764 ret = ulist_add(tmp, qg->qgroupid, qgroup_to_aux(qg), GFP_ATOMIC);
d810ef2b
QW
1765 if (ret < 0)
1766 return ret;
1767 ULIST_ITER_INIT(&tmp_uiter);
1768 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1769 struct btrfs_qgroup_list *glist;
1770
ef2fff64 1771 qg = unode_aux_to_qgroup(tmp_unode);
d810ef2b
QW
1772 if (update_old)
1773 btrfs_qgroup_update_old_refcnt(qg, seq, 1);
1774 else
1775 btrfs_qgroup_update_new_refcnt(qg, seq, 1);
1776 list_for_each_entry(glist, &qg->groups, next_group) {
1777 ret = ulist_add(qgroups, glist->group->qgroupid,
ef2fff64 1778 qgroup_to_aux(glist->group),
d810ef2b
QW
1779 GFP_ATOMIC);
1780 if (ret < 0)
1781 return ret;
1782 ret = ulist_add(tmp, glist->group->qgroupid,
ef2fff64 1783 qgroup_to_aux(glist->group),
d810ef2b
QW
1784 GFP_ATOMIC);
1785 if (ret < 0)
1786 return ret;
1787 }
1788 }
1789 }
1790 return 0;
1791}
1792
823ae5b8
QW
1793/*
1794 * Update qgroup rfer/excl counters.
1795 * Rfer update is easy, codes can explain themselves.
e69bcee3 1796 *
823ae5b8
QW
1797 * Excl update is tricky, the update is split into 2 part.
1798 * Part 1: Possible exclusive <-> sharing detect:
1799 * | A | !A |
1800 * -------------------------------------
1801 * B | * | - |
1802 * -------------------------------------
1803 * !B | + | ** |
1804 * -------------------------------------
1805 *
1806 * Conditions:
1807 * A: cur_old_roots < nr_old_roots (not exclusive before)
1808 * !A: cur_old_roots == nr_old_roots (possible exclusive before)
1809 * B: cur_new_roots < nr_new_roots (not exclusive now)
01327610 1810 * !B: cur_new_roots == nr_new_roots (possible exclusive now)
823ae5b8
QW
1811 *
1812 * Results:
1813 * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
1814 * *: Definitely not changed. **: Possible unchanged.
1815 *
1816 * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
1817 *
1818 * To make the logic clear, we first use condition A and B to split
1819 * combination into 4 results.
1820 *
1821 * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
1822 * only on variant maybe 0.
1823 *
1824 * Lastly, check result **, since there are 2 variants maybe 0, split them
1825 * again(2x2).
1826 * But this time we don't need to consider other things, the codes and logic
1827 * is easy to understand now.
1828 */
1829static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
1830 struct ulist *qgroups,
1831 u64 nr_old_roots,
1832 u64 nr_new_roots,
1833 u64 num_bytes, u64 seq)
1834{
1835 struct ulist_node *unode;
1836 struct ulist_iterator uiter;
1837 struct btrfs_qgroup *qg;
1838 u64 cur_new_count, cur_old_count;
1839
1840 ULIST_ITER_INIT(&uiter);
1841 while ((unode = ulist_next(qgroups, &uiter))) {
1842 bool dirty = false;
1843
ef2fff64 1844 qg = unode_aux_to_qgroup(unode);
823ae5b8
QW
1845 cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
1846 cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
1847
bc074524
JM
1848 trace_qgroup_update_counters(fs_info, qg->qgroupid,
1849 cur_old_count, cur_new_count);
0f5dcf8d 1850
823ae5b8
QW
1851 /* Rfer update part */
1852 if (cur_old_count == 0 && cur_new_count > 0) {
1853 qg->rfer += num_bytes;
1854 qg->rfer_cmpr += num_bytes;
1855 dirty = true;
1856 }
1857 if (cur_old_count > 0 && cur_new_count == 0) {
1858 qg->rfer -= num_bytes;
1859 qg->rfer_cmpr -= num_bytes;
1860 dirty = true;
1861 }
1862
1863 /* Excl update part */
1864 /* Exclusive/none -> shared case */
1865 if (cur_old_count == nr_old_roots &&
1866 cur_new_count < nr_new_roots) {
1867 /* Exclusive -> shared */
1868 if (cur_old_count != 0) {
1869 qg->excl -= num_bytes;
1870 qg->excl_cmpr -= num_bytes;
1871 dirty = true;
1872 }
1873 }
1874
1875 /* Shared -> exclusive/none case */
1876 if (cur_old_count < nr_old_roots &&
1877 cur_new_count == nr_new_roots) {
1878 /* Shared->exclusive */
1879 if (cur_new_count != 0) {
1880 qg->excl += num_bytes;
1881 qg->excl_cmpr += num_bytes;
1882 dirty = true;
1883 }
1884 }
1885
1886 /* Exclusive/none -> exclusive/none case */
1887 if (cur_old_count == nr_old_roots &&
1888 cur_new_count == nr_new_roots) {
1889 if (cur_old_count == 0) {
1890 /* None -> exclusive/none */
1891
1892 if (cur_new_count != 0) {
1893 /* None -> exclusive */
1894 qg->excl += num_bytes;
1895 qg->excl_cmpr += num_bytes;
1896 dirty = true;
1897 }
1898 /* None -> none, nothing changed */
1899 } else {
1900 /* Exclusive -> exclusive/none */
1901
1902 if (cur_new_count == 0) {
1903 /* Exclusive -> none */
1904 qg->excl -= num_bytes;
1905 qg->excl_cmpr -= num_bytes;
1906 dirty = true;
1907 }
1908 /* Exclusive -> exclusive, nothing changed */
1909 }
1910 }
c05f9429 1911
823ae5b8
QW
1912 if (dirty)
1913 qgroup_dirty(fs_info, qg);
1914 }
1915 return 0;
1916}
1917
442244c9 1918int
550d7a2e
QW
1919btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans,
1920 struct btrfs_fs_info *fs_info,
1921 u64 bytenr, u64 num_bytes,
1922 struct ulist *old_roots, struct ulist *new_roots)
1923{
1924 struct ulist *qgroups = NULL;
1925 struct ulist *tmp = NULL;
1926 u64 seq;
1927 u64 nr_new_roots = 0;
1928 u64 nr_old_roots = 0;
1929 int ret = 0;
1930
81353d50
DS
1931 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
1932 return 0;
1933
550d7a2e
QW
1934 if (new_roots)
1935 nr_new_roots = new_roots->nnodes;
1936 if (old_roots)
1937 nr_old_roots = old_roots->nnodes;
1938
550d7a2e
QW
1939 BUG_ON(!fs_info->quota_root);
1940
bc074524
JM
1941 trace_btrfs_qgroup_account_extent(fs_info, bytenr, num_bytes,
1942 nr_old_roots, nr_new_roots);
0f5dcf8d 1943
550d7a2e
QW
1944 qgroups = ulist_alloc(GFP_NOFS);
1945 if (!qgroups) {
1946 ret = -ENOMEM;
1947 goto out_free;
1948 }
1949 tmp = ulist_alloc(GFP_NOFS);
1950 if (!tmp) {
1951 ret = -ENOMEM;
1952 goto out_free;
1953 }
1954
1955 mutex_lock(&fs_info->qgroup_rescan_lock);
1956 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
1957 if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
1958 mutex_unlock(&fs_info->qgroup_rescan_lock);
1959 ret = 0;
1960 goto out_free;
1961 }
1962 }
1963 mutex_unlock(&fs_info->qgroup_rescan_lock);
1964
1965 spin_lock(&fs_info->qgroup_lock);
1966 seq = fs_info->qgroup_seq;
1967
1968 /* Update old refcnts using old_roots */
1969 ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
1970 UPDATE_OLD);
1971 if (ret < 0)
1972 goto out;
1973
1974 /* Update new refcnts using new_roots */
1975 ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
1976 UPDATE_NEW);
1977 if (ret < 0)
1978 goto out;
1979
1980 qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
1981 num_bytes, seq);
1982
1983 /*
1984 * Bump qgroup_seq to avoid seq overlap
1985 */
1986 fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
1987out:
1988 spin_unlock(&fs_info->qgroup_lock);
1989out_free:
1990 ulist_free(tmp);
1991 ulist_free(qgroups);
1992 ulist_free(old_roots);
1993 ulist_free(new_roots);
1994 return ret;
1995}
1996
1997int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans,
1998 struct btrfs_fs_info *fs_info)
1999{
2000 struct btrfs_qgroup_extent_record *record;
2001 struct btrfs_delayed_ref_root *delayed_refs;
2002 struct ulist *new_roots = NULL;
2003 struct rb_node *node;
9086db86 2004 u64 qgroup_to_skip;
550d7a2e
QW
2005 int ret = 0;
2006
2007 delayed_refs = &trans->transaction->delayed_refs;
9086db86 2008 qgroup_to_skip = delayed_refs->qgroup_to_skip;
550d7a2e
QW
2009 while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
2010 record = rb_entry(node, struct btrfs_qgroup_extent_record,
2011 node);
2012
bc074524 2013 trace_btrfs_qgroup_account_extents(fs_info, record);
0f5dcf8d 2014
550d7a2e
QW
2015 if (!ret) {
2016 /*
de47c9d3 2017 * Use SEQ_LAST as time_seq to do special search, which
550d7a2e
QW
2018 * doesn't lock tree or delayed_refs and search current
2019 * root. It's safe inside commit_transaction().
2020 */
2021 ret = btrfs_find_all_roots(trans, fs_info,
de47c9d3 2022 record->bytenr, SEQ_LAST, &new_roots);
550d7a2e
QW
2023 if (ret < 0)
2024 goto cleanup;
9086db86
QW
2025 if (qgroup_to_skip)
2026 ulist_del(new_roots, qgroup_to_skip, 0);
550d7a2e
QW
2027 ret = btrfs_qgroup_account_extent(trans, fs_info,
2028 record->bytenr, record->num_bytes,
2029 record->old_roots, new_roots);
2030 record->old_roots = NULL;
2031 new_roots = NULL;
2032 }
2033cleanup:
2034 ulist_free(record->old_roots);
2035 ulist_free(new_roots);
2036 new_roots = NULL;
2037 rb_erase(node, &delayed_refs->dirty_extent_root);
2038 kfree(record);
2039
2040 }
2041 return ret;
2042}
2043
bed92eae
AJ
2044/*
2045 * called from commit_transaction. Writes all changed qgroups to disk.
2046 */
2047int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
2048 struct btrfs_fs_info *fs_info)
2049{
2050 struct btrfs_root *quota_root = fs_info->quota_root;
2051 int ret = 0;
3d7b5a28 2052 int start_rescan_worker = 0;
bed92eae
AJ
2053
2054 if (!quota_root)
2055 goto out;
2056
afcdd129
JB
2057 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
2058 test_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags))
3d7b5a28
JS
2059 start_rescan_worker = 1;
2060
afcdd129
JB
2061 if (test_and_clear_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags))
2062 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
2063 if (test_and_clear_bit(BTRFS_FS_QUOTA_DISABLING, &fs_info->flags))
2064 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
bed92eae
AJ
2065
2066 spin_lock(&fs_info->qgroup_lock);
2067 while (!list_empty(&fs_info->dirty_qgroups)) {
2068 struct btrfs_qgroup *qgroup;
2069 qgroup = list_first_entry(&fs_info->dirty_qgroups,
2070 struct btrfs_qgroup, dirty);
2071 list_del_init(&qgroup->dirty);
2072 spin_unlock(&fs_info->qgroup_lock);
2073 ret = update_qgroup_info_item(trans, quota_root, qgroup);
d3001ed3
DY
2074 if (ret)
2075 fs_info->qgroup_flags |=
2076 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2077 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
bed92eae
AJ
2078 if (ret)
2079 fs_info->qgroup_flags |=
2080 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2081 spin_lock(&fs_info->qgroup_lock);
2082 }
afcdd129 2083 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
bed92eae
AJ
2084 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
2085 else
2086 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
2087 spin_unlock(&fs_info->qgroup_lock);
2088
2089 ret = update_qgroup_status_item(trans, fs_info, quota_root);
2090 if (ret)
2091 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2092
3d7b5a28 2093 if (!ret && start_rescan_worker) {
b382a324
JS
2094 ret = qgroup_rescan_init(fs_info, 0, 1);
2095 if (!ret) {
2096 qgroup_rescan_zero_tracking(fs_info);
fc97fab0
QW
2097 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2098 &fs_info->qgroup_rescan_work);
b382a324 2099 }
3d7b5a28
JS
2100 ret = 0;
2101 }
2102
bed92eae
AJ
2103out:
2104
2105 return ret;
2106}
2107
2108/*
01327610 2109 * Copy the accounting information between qgroups. This is necessary
918c2ee1
MF
2110 * when a snapshot or a subvolume is created. Throwing an error will
2111 * cause a transaction abort so we take extra care here to only error
2112 * when a readonly fs is a reasonable outcome.
bed92eae
AJ
2113 */
2114int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
2115 struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
2116 struct btrfs_qgroup_inherit *inherit)
2117{
2118 int ret = 0;
2119 int i;
2120 u64 *i_qgroups;
2121 struct btrfs_root *quota_root = fs_info->quota_root;
2122 struct btrfs_qgroup *srcgroup;
2123 struct btrfs_qgroup *dstgroup;
2124 u32 level_size = 0;
3f5e2d3b 2125 u64 nums;
bed92eae 2126
f2f6ed3d 2127 mutex_lock(&fs_info->qgroup_ioctl_lock);
afcdd129 2128 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
f2f6ed3d 2129 goto out;
bed92eae 2130
f2f6ed3d
WS
2131 if (!quota_root) {
2132 ret = -EINVAL;
2133 goto out;
2134 }
bed92eae 2135
3f5e2d3b
WS
2136 if (inherit) {
2137 i_qgroups = (u64 *)(inherit + 1);
2138 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
2139 2 * inherit->num_excl_copies;
2140 for (i = 0; i < nums; ++i) {
2141 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
09870d27 2142
918c2ee1
MF
2143 /*
2144 * Zero out invalid groups so we can ignore
2145 * them later.
2146 */
2147 if (!srcgroup ||
2148 ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
2149 *i_qgroups = 0ULL;
2150
3f5e2d3b
WS
2151 ++i_qgroups;
2152 }
2153 }
2154
bed92eae
AJ
2155 /*
2156 * create a tracking group for the subvol itself
2157 */
2158 ret = add_qgroup_item(trans, quota_root, objectid);
2159 if (ret)
2160 goto out;
2161
bed92eae
AJ
2162 if (srcid) {
2163 struct btrfs_root *srcroot;
2164 struct btrfs_key srckey;
bed92eae
AJ
2165
2166 srckey.objectid = srcid;
2167 srckey.type = BTRFS_ROOT_ITEM_KEY;
2168 srckey.offset = (u64)-1;
2169 srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
2170 if (IS_ERR(srcroot)) {
2171 ret = PTR_ERR(srcroot);
2172 goto out;
2173 }
2174
0b246afa 2175 level_size = fs_info->nodesize;
bed92eae
AJ
2176 }
2177
2178 /*
2179 * add qgroup to all inherited groups
2180 */
2181 if (inherit) {
2182 i_qgroups = (u64 *)(inherit + 1);
918c2ee1
MF
2183 for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) {
2184 if (*i_qgroups == 0)
2185 continue;
bed92eae
AJ
2186 ret = add_qgroup_relation_item(trans, quota_root,
2187 objectid, *i_qgroups);
918c2ee1 2188 if (ret && ret != -EEXIST)
bed92eae
AJ
2189 goto out;
2190 ret = add_qgroup_relation_item(trans, quota_root,
2191 *i_qgroups, objectid);
918c2ee1 2192 if (ret && ret != -EEXIST)
bed92eae 2193 goto out;
bed92eae 2194 }
918c2ee1 2195 ret = 0;
bed92eae
AJ
2196 }
2197
2198
2199 spin_lock(&fs_info->qgroup_lock);
2200
2201 dstgroup = add_qgroup_rb(fs_info, objectid);
57a5a882
DC
2202 if (IS_ERR(dstgroup)) {
2203 ret = PTR_ERR(dstgroup);
bed92eae 2204 goto unlock;
57a5a882 2205 }
bed92eae 2206
e8c8541a 2207 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
e8c8541a
DY
2208 dstgroup->lim_flags = inherit->lim.flags;
2209 dstgroup->max_rfer = inherit->lim.max_rfer;
2210 dstgroup->max_excl = inherit->lim.max_excl;
2211 dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
2212 dstgroup->rsv_excl = inherit->lim.rsv_excl;
1510e71c
DY
2213
2214 ret = update_qgroup_limit_item(trans, quota_root, dstgroup);
2215 if (ret) {
2216 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
5d163e0e
JM
2217 btrfs_info(fs_info,
2218 "unable to update quota limit for %llu",
2219 dstgroup->qgroupid);
1510e71c
DY
2220 goto unlock;
2221 }
e8c8541a
DY
2222 }
2223
bed92eae
AJ
2224 if (srcid) {
2225 srcgroup = find_qgroup_rb(fs_info, srcid);
f3a87f1b 2226 if (!srcgroup)
bed92eae 2227 goto unlock;
fcebe456
JB
2228
2229 /*
2230 * We call inherit after we clone the root in order to make sure
2231 * our counts don't go crazy, so at this point the only
2232 * difference between the two roots should be the root node.
2233 */
2234 dstgroup->rfer = srcgroup->rfer;
2235 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
2236 dstgroup->excl = level_size;
2237 dstgroup->excl_cmpr = level_size;
bed92eae
AJ
2238 srcgroup->excl = level_size;
2239 srcgroup->excl_cmpr = level_size;
3eeb4d59
DY
2240
2241 /* inherit the limit info */
2242 dstgroup->lim_flags = srcgroup->lim_flags;
2243 dstgroup->max_rfer = srcgroup->max_rfer;
2244 dstgroup->max_excl = srcgroup->max_excl;
2245 dstgroup->rsv_rfer = srcgroup->rsv_rfer;
2246 dstgroup->rsv_excl = srcgroup->rsv_excl;
2247
bed92eae
AJ
2248 qgroup_dirty(fs_info, dstgroup);
2249 qgroup_dirty(fs_info, srcgroup);
2250 }
2251
f3a87f1b 2252 if (!inherit)
bed92eae
AJ
2253 goto unlock;
2254
2255 i_qgroups = (u64 *)(inherit + 1);
2256 for (i = 0; i < inherit->num_qgroups; ++i) {
918c2ee1 2257 if (*i_qgroups) {
0b246afa 2258 ret = add_relation_rb(fs_info, objectid, *i_qgroups);
918c2ee1
MF
2259 if (ret)
2260 goto unlock;
2261 }
bed92eae
AJ
2262 ++i_qgroups;
2263 }
2264
918c2ee1 2265 for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {
bed92eae
AJ
2266 struct btrfs_qgroup *src;
2267 struct btrfs_qgroup *dst;
2268
918c2ee1
MF
2269 if (!i_qgroups[0] || !i_qgroups[1])
2270 continue;
2271
bed92eae
AJ
2272 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2273 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2274
2275 if (!src || !dst) {
2276 ret = -EINVAL;
2277 goto unlock;
2278 }
2279
2280 dst->rfer = src->rfer - level_size;
2281 dst->rfer_cmpr = src->rfer_cmpr - level_size;
bed92eae 2282 }
918c2ee1 2283 for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {
bed92eae
AJ
2284 struct btrfs_qgroup *src;
2285 struct btrfs_qgroup *dst;
2286
918c2ee1
MF
2287 if (!i_qgroups[0] || !i_qgroups[1])
2288 continue;
2289
bed92eae
AJ
2290 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2291 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2292
2293 if (!src || !dst) {
2294 ret = -EINVAL;
2295 goto unlock;
2296 }
2297
2298 dst->excl = src->excl + level_size;
2299 dst->excl_cmpr = src->excl_cmpr + level_size;
bed92eae
AJ
2300 }
2301
2302unlock:
2303 spin_unlock(&fs_info->qgroup_lock);
2304out:
f2f6ed3d 2305 mutex_unlock(&fs_info->qgroup_ioctl_lock);
bed92eae
AJ
2306 return ret;
2307}
2308
003d7c59
JM
2309static bool qgroup_check_limits(const struct btrfs_qgroup *qg, u64 num_bytes)
2310{
2311 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
2312 qg->reserved + (s64)qg->rfer + num_bytes > qg->max_rfer)
2313 return false;
2314
2315 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
2316 qg->reserved + (s64)qg->excl + num_bytes > qg->max_excl)
2317 return false;
2318
2319 return true;
2320}
2321
2322static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce)
bed92eae
AJ
2323{
2324 struct btrfs_root *quota_root;
2325 struct btrfs_qgroup *qgroup;
2326 struct btrfs_fs_info *fs_info = root->fs_info;
2327 u64 ref_root = root->root_key.objectid;
2328 int ret = 0;
48a89bc4 2329 int retried = 0;
bed92eae
AJ
2330 struct ulist_node *unode;
2331 struct ulist_iterator uiter;
2332
2333 if (!is_fstree(ref_root))
2334 return 0;
2335
2336 if (num_bytes == 0)
2337 return 0;
48a89bc4 2338retry:
bed92eae
AJ
2339 spin_lock(&fs_info->qgroup_lock);
2340 quota_root = fs_info->quota_root;
2341 if (!quota_root)
2342 goto out;
2343
2344 qgroup = find_qgroup_rb(fs_info, ref_root);
2345 if (!qgroup)
2346 goto out;
2347
2348 /*
2349 * in a first step, we check all affected qgroups if any limits would
2350 * be exceeded
2351 */
1e8f9158
WS
2352 ulist_reinit(fs_info->qgroup_ulist);
2353 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
3c97185c
WS
2354 (uintptr_t)qgroup, GFP_ATOMIC);
2355 if (ret < 0)
2356 goto out;
bed92eae 2357 ULIST_ITER_INIT(&uiter);
1e8f9158 2358 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
bed92eae
AJ
2359 struct btrfs_qgroup *qg;
2360 struct btrfs_qgroup_list *glist;
2361
ef2fff64 2362 qg = unode_aux_to_qgroup(unode);
bed92eae 2363
003d7c59 2364 if (enforce && !qgroup_check_limits(qg, num_bytes)) {
48a89bc4
GR
2365 /*
2366 * Commit the tree and retry, since we may have
2367 * deletions which would free up space.
2368 */
2369 if (!retried && qg->reserved > 0) {
2370 struct btrfs_trans_handle *trans;
2371
2372 spin_unlock(&fs_info->qgroup_lock);
2373 ret = btrfs_start_delalloc_inodes(root, 0);
2374 if (ret)
2375 return ret;
2376 btrfs_wait_ordered_extents(root, -1, 0, (u64)-1);
2377 trans = btrfs_join_transaction(root);
2378 if (IS_ERR(trans))
2379 return PTR_ERR(trans);
2380 ret = btrfs_commit_transaction(trans);
2381 if (ret)
2382 return ret;
2383 retried++;
2384 goto retry;
2385 }
bed92eae 2386 ret = -EDQUOT;
720f1e20
WS
2387 goto out;
2388 }
bed92eae
AJ
2389
2390 list_for_each_entry(glist, &qg->groups, next_group) {
1e8f9158
WS
2391 ret = ulist_add(fs_info->qgroup_ulist,
2392 glist->group->qgroupid,
3c97185c
WS
2393 (uintptr_t)glist->group, GFP_ATOMIC);
2394 if (ret < 0)
2395 goto out;
bed92eae
AJ
2396 }
2397 }
3c97185c 2398 ret = 0;
bed92eae
AJ
2399 /*
2400 * no limits exceeded, now record the reservation into all qgroups
2401 */
2402 ULIST_ITER_INIT(&uiter);
1e8f9158 2403 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
bed92eae
AJ
2404 struct btrfs_qgroup *qg;
2405
ef2fff64 2406 qg = unode_aux_to_qgroup(unode);
bed92eae 2407
3159fe7b 2408 trace_qgroup_update_reserve(fs_info, qg, num_bytes);
e2d1f923 2409 qg->reserved += num_bytes;
bed92eae
AJ
2410 }
2411
2412out:
2413 spin_unlock(&fs_info->qgroup_lock);
bed92eae
AJ
2414 return ret;
2415}
2416
297d750b
QW
2417void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
2418 u64 ref_root, u64 num_bytes)
bed92eae
AJ
2419{
2420 struct btrfs_root *quota_root;
2421 struct btrfs_qgroup *qgroup;
bed92eae
AJ
2422 struct ulist_node *unode;
2423 struct ulist_iterator uiter;
3c97185c 2424 int ret = 0;
bed92eae
AJ
2425
2426 if (!is_fstree(ref_root))
2427 return;
2428
2429 if (num_bytes == 0)
2430 return;
2431
2432 spin_lock(&fs_info->qgroup_lock);
2433
2434 quota_root = fs_info->quota_root;
2435 if (!quota_root)
2436 goto out;
2437
2438 qgroup = find_qgroup_rb(fs_info, ref_root);
2439 if (!qgroup)
2440 goto out;
2441
1e8f9158
WS
2442 ulist_reinit(fs_info->qgroup_ulist);
2443 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
3c97185c
WS
2444 (uintptr_t)qgroup, GFP_ATOMIC);
2445 if (ret < 0)
2446 goto out;
bed92eae 2447 ULIST_ITER_INIT(&uiter);
1e8f9158 2448 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
bed92eae
AJ
2449 struct btrfs_qgroup *qg;
2450 struct btrfs_qgroup_list *glist;
2451
ef2fff64 2452 qg = unode_aux_to_qgroup(unode);
bed92eae 2453
3159fe7b 2454 trace_qgroup_update_reserve(fs_info, qg, -(s64)num_bytes);
18dc22c1
QW
2455 if (WARN_ON(qg->reserved < num_bytes))
2456 report_reserved_underflow(fs_info, qg, num_bytes);
2457 else
2458 qg->reserved -= num_bytes;
bed92eae
AJ
2459
2460 list_for_each_entry(glist, &qg->groups, next_group) {
1e8f9158
WS
2461 ret = ulist_add(fs_info->qgroup_ulist,
2462 glist->group->qgroupid,
3c97185c
WS
2463 (uintptr_t)glist->group, GFP_ATOMIC);
2464 if (ret < 0)
2465 goto out;
bed92eae
AJ
2466 }
2467 }
2468
2469out:
2470 spin_unlock(&fs_info->qgroup_lock);
bed92eae
AJ
2471}
2472
2f232036
JS
2473/*
2474 * returns < 0 on error, 0 when more leafs are to be scanned.
3393168d 2475 * returns 1 when done.
2f232036
JS
2476 */
2477static int
b382a324 2478qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
0a0e8b89 2479 struct btrfs_trans_handle *trans)
2f232036
JS
2480{
2481 struct btrfs_key found;
0a0e8b89 2482 struct extent_buffer *scratch_leaf = NULL;
2f232036 2483 struct ulist *roots = NULL;
3284da7b 2484 struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
fcebe456 2485 u64 num_bytes;
2f232036
JS
2486 int slot;
2487 int ret;
2488
2f232036
JS
2489 mutex_lock(&fs_info->qgroup_rescan_lock);
2490 ret = btrfs_search_slot_for_read(fs_info->extent_root,
2491 &fs_info->qgroup_rescan_progress,
2492 path, 1, 0);
2493
ab8d0fc4
JM
2494 btrfs_debug(fs_info,
2495 "current progress key (%llu %u %llu), search_slot ret %d",
2496 fs_info->qgroup_rescan_progress.objectid,
2497 fs_info->qgroup_rescan_progress.type,
2498 fs_info->qgroup_rescan_progress.offset, ret);
2f232036
JS
2499
2500 if (ret) {
2501 /*
2502 * The rescan is about to end, we will not be scanning any
2503 * further blocks. We cannot unset the RESCAN flag here, because
2504 * we want to commit the transaction if everything went well.
2505 * To make the live accounting work in this phase, we set our
2506 * scan progress pointer such that every real extent objectid
2507 * will be smaller.
2508 */
2509 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
2510 btrfs_release_path(path);
2511 mutex_unlock(&fs_info->qgroup_rescan_lock);
2512 return ret;
2513 }
2514
2515 btrfs_item_key_to_cpu(path->nodes[0], &found,
2516 btrfs_header_nritems(path->nodes[0]) - 1);
2517 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
2518
2519 btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
0a0e8b89
QW
2520 scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
2521 if (!scratch_leaf) {
2522 ret = -ENOMEM;
2523 mutex_unlock(&fs_info->qgroup_rescan_lock);
2524 goto out;
2525 }
2526 extent_buffer_get(scratch_leaf);
2527 btrfs_tree_read_lock(scratch_leaf);
2528 btrfs_set_lock_blocking_rw(scratch_leaf, BTRFS_READ_LOCK);
2f232036
JS
2529 slot = path->slots[0];
2530 btrfs_release_path(path);
2531 mutex_unlock(&fs_info->qgroup_rescan_lock);
2532
2533 for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
2534 btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
3a6d75e8
JB
2535 if (found.type != BTRFS_EXTENT_ITEM_KEY &&
2536 found.type != BTRFS_METADATA_ITEM_KEY)
2f232036 2537 continue;
3a6d75e8 2538 if (found.type == BTRFS_METADATA_ITEM_KEY)
da17066c 2539 num_bytes = fs_info->nodesize;
3a6d75e8
JB
2540 else
2541 num_bytes = found.offset;
2542
fcebe456
JB
2543 ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
2544 &roots);
2f232036
JS
2545 if (ret < 0)
2546 goto out;
9d220c95
QW
2547 /* For rescan, just pass old_roots as NULL */
2548 ret = btrfs_qgroup_account_extent(trans, fs_info,
2549 found.objectid, num_bytes, NULL, roots);
2550 if (ret < 0)
fcebe456 2551 goto out;
2f232036 2552 }
2f232036 2553out:
0a0e8b89
QW
2554 if (scratch_leaf) {
2555 btrfs_tree_read_unlock_blocking(scratch_leaf);
2556 free_extent_buffer(scratch_leaf);
2557 }
2f232036
JS
2558 btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
2559
2560 return ret;
2561}
2562
d458b054 2563static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
2f232036 2564{
b382a324
JS
2565 struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
2566 qgroup_rescan_work);
2f232036
JS
2567 struct btrfs_path *path;
2568 struct btrfs_trans_handle *trans = NULL;
2f232036 2569 int err = -ENOMEM;
53b7cde9 2570 int ret = 0;
2f232036
JS
2571
2572 path = btrfs_alloc_path();
2573 if (!path)
2574 goto out;
2f232036
JS
2575
2576 err = 0;
7343dd61 2577 while (!err && !btrfs_fs_closing(fs_info)) {
2f232036
JS
2578 trans = btrfs_start_transaction(fs_info->fs_root, 0);
2579 if (IS_ERR(trans)) {
2580 err = PTR_ERR(trans);
2581 break;
2582 }
afcdd129 2583 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
2f232036
JS
2584 err = -EINTR;
2585 } else {
0a0e8b89 2586 err = qgroup_rescan_leaf(fs_info, path, trans);
2f232036
JS
2587 }
2588 if (err > 0)
3a45bb20 2589 btrfs_commit_transaction(trans);
2f232036 2590 else
3a45bb20 2591 btrfs_end_transaction(trans);
2f232036
JS
2592 }
2593
2594out:
2f232036 2595 btrfs_free_path(path);
2f232036
JS
2596
2597 mutex_lock(&fs_info->qgroup_rescan_lock);
7343dd61
JM
2598 if (!btrfs_fs_closing(fs_info))
2599 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2f232036 2600
3393168d 2601 if (err > 0 &&
2f232036
JS
2602 fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
2603 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2604 } else if (err < 0) {
2605 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2606 }
2607 mutex_unlock(&fs_info->qgroup_rescan_lock);
2608
53b7cde9 2609 /*
01327610 2610 * only update status, since the previous part has already updated the
53b7cde9
QW
2611 * qgroup info.
2612 */
2613 trans = btrfs_start_transaction(fs_info->quota_root, 1);
2614 if (IS_ERR(trans)) {
2615 err = PTR_ERR(trans);
2616 btrfs_err(fs_info,
2617 "fail to start transaction for status update: %d\n",
2618 err);
2619 goto done;
2620 }
2621 ret = update_qgroup_status_item(trans, fs_info, fs_info->quota_root);
2622 if (ret < 0) {
2623 err = ret;
ab8d0fc4 2624 btrfs_err(fs_info, "fail to update qgroup status: %d", err);
53b7cde9 2625 }
3a45bb20 2626 btrfs_end_transaction(trans);
53b7cde9 2627
7343dd61
JM
2628 if (btrfs_fs_closing(fs_info)) {
2629 btrfs_info(fs_info, "qgroup scan paused");
2630 } else if (err >= 0) {
efe120a0 2631 btrfs_info(fs_info, "qgroup scan completed%s",
3393168d 2632 err > 0 ? " (inconsistency flag cleared)" : "");
2f232036 2633 } else {
efe120a0 2634 btrfs_err(fs_info, "qgroup scan failed with %d", err);
2f232036 2635 }
57254b6e 2636
53b7cde9 2637done:
d2c609b8
JM
2638 mutex_lock(&fs_info->qgroup_rescan_lock);
2639 fs_info->qgroup_rescan_running = false;
2640 mutex_unlock(&fs_info->qgroup_rescan_lock);
57254b6e 2641 complete_all(&fs_info->qgroup_rescan_completion);
2f232036
JS
2642}
2643
b382a324
JS
2644/*
2645 * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
2646 * memory required for the rescan context.
2647 */
2648static int
2649qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
2650 int init_flags)
2f232036
JS
2651{
2652 int ret = 0;
2f232036 2653
b382a324
JS
2654 if (!init_flags &&
2655 (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
2656 !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
2657 ret = -EINVAL;
2658 goto err;
2659 }
2f232036
JS
2660
2661 mutex_lock(&fs_info->qgroup_rescan_lock);
2662 spin_lock(&fs_info->qgroup_lock);
b382a324
JS
2663
2664 if (init_flags) {
2665 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
2666 ret = -EINPROGRESS;
2667 else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
2668 ret = -EINVAL;
2669
2670 if (ret) {
2671 spin_unlock(&fs_info->qgroup_lock);
2672 mutex_unlock(&fs_info->qgroup_rescan_lock);
2673 goto err;
2674 }
b382a324 2675 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2f232036
JS
2676 }
2677
2f232036
JS
2678 memset(&fs_info->qgroup_rescan_progress, 0,
2679 sizeof(fs_info->qgroup_rescan_progress));
b382a324 2680 fs_info->qgroup_rescan_progress.objectid = progress_objectid;
190631f1 2681 init_completion(&fs_info->qgroup_rescan_completion);
8d9eddad 2682 fs_info->qgroup_rescan_running = true;
b382a324
JS
2683
2684 spin_unlock(&fs_info->qgroup_lock);
2685 mutex_unlock(&fs_info->qgroup_rescan_lock);
2686
b382a324
JS
2687 memset(&fs_info->qgroup_rescan_work, 0,
2688 sizeof(fs_info->qgroup_rescan_work));
fc97fab0 2689 btrfs_init_work(&fs_info->qgroup_rescan_work,
9e0af237 2690 btrfs_qgroup_rescan_helper,
fc97fab0 2691 btrfs_qgroup_rescan_worker, NULL, NULL);
b382a324
JS
2692
2693 if (ret) {
2694err:
efe120a0 2695 btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret);
b382a324
JS
2696 return ret;
2697 }
2698
2699 return 0;
2700}
2701
2702static void
2703qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
2704{
2705 struct rb_node *n;
2706 struct btrfs_qgroup *qgroup;
2707
2708 spin_lock(&fs_info->qgroup_lock);
2f232036
JS
2709 /* clear all current qgroup tracking information */
2710 for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
2711 qgroup = rb_entry(n, struct btrfs_qgroup, node);
2712 qgroup->rfer = 0;
2713 qgroup->rfer_cmpr = 0;
2714 qgroup->excl = 0;
2715 qgroup->excl_cmpr = 0;
2716 }
2717 spin_unlock(&fs_info->qgroup_lock);
b382a324 2718}
2f232036 2719
b382a324
JS
2720int
2721btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
2722{
2723 int ret = 0;
2724 struct btrfs_trans_handle *trans;
2725
2726 ret = qgroup_rescan_init(fs_info, 0, 1);
2727 if (ret)
2728 return ret;
2729
2730 /*
2731 * We have set the rescan_progress to 0, which means no more
2732 * delayed refs will be accounted by btrfs_qgroup_account_ref.
2733 * However, btrfs_qgroup_account_ref may be right after its call
2734 * to btrfs_find_all_roots, in which case it would still do the
2735 * accounting.
2736 * To solve this, we're committing the transaction, which will
2737 * ensure we run all delayed refs and only after that, we are
2738 * going to clear all tracking information for a clean start.
2739 */
2740
2741 trans = btrfs_join_transaction(fs_info->fs_root);
2742 if (IS_ERR(trans)) {
2743 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2744 return PTR_ERR(trans);
2745 }
3a45bb20 2746 ret = btrfs_commit_transaction(trans);
b382a324
JS
2747 if (ret) {
2748 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2749 return ret;
2750 }
2751
2752 qgroup_rescan_zero_tracking(fs_info);
2753
fc97fab0
QW
2754 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2755 &fs_info->qgroup_rescan_work);
2f232036
JS
2756
2757 return 0;
2758}
57254b6e 2759
d06f23d6
JM
2760int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
2761 bool interruptible)
57254b6e
JS
2762{
2763 int running;
2764 int ret = 0;
2765
2766 mutex_lock(&fs_info->qgroup_rescan_lock);
2767 spin_lock(&fs_info->qgroup_lock);
d2c609b8 2768 running = fs_info->qgroup_rescan_running;
57254b6e
JS
2769 spin_unlock(&fs_info->qgroup_lock);
2770 mutex_unlock(&fs_info->qgroup_rescan_lock);
2771
d06f23d6
JM
2772 if (!running)
2773 return 0;
2774
2775 if (interruptible)
57254b6e
JS
2776 ret = wait_for_completion_interruptible(
2777 &fs_info->qgroup_rescan_completion);
d06f23d6
JM
2778 else
2779 wait_for_completion(&fs_info->qgroup_rescan_completion);
57254b6e
JS
2780
2781 return ret;
2782}
b382a324
JS
2783
2784/*
2785 * this is only called from open_ctree where we're still single threaded, thus
2786 * locking is omitted here.
2787 */
2788void
2789btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
2790{
2791 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
fc97fab0
QW
2792 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2793 &fs_info->qgroup_rescan_work);
b382a324 2794}
52472553
QW
2795
2796/*
2797 * Reserve qgroup space for range [start, start + len).
2798 *
2799 * This function will either reserve space from related qgroups or doing
2800 * nothing if the range is already reserved.
2801 *
2802 * Return 0 for successful reserve
2803 * Return <0 for error (including -EQUOT)
2804 *
2805 * NOTE: this function may sleep for memory allocation.
2806 */
2807int btrfs_qgroup_reserve_data(struct inode *inode, u64 start, u64 len)
2808{
2809 struct btrfs_root *root = BTRFS_I(inode)->root;
2810 struct extent_changeset changeset;
2811 struct ulist_node *unode;
2812 struct ulist_iterator uiter;
2813 int ret;
2814
afcdd129
JB
2815 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
2816 !is_fstree(root->objectid) || len == 0)
52472553
QW
2817 return 0;
2818
2819 changeset.bytes_changed = 0;
53d32359 2820 ulist_init(&changeset.range_changed);
52472553 2821 ret = set_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
2c53b912 2822 start + len -1, EXTENT_QGROUP_RESERVED, &changeset);
81fb6f77
QW
2823 trace_btrfs_qgroup_reserve_data(inode, start, len,
2824 changeset.bytes_changed,
2825 QGROUP_RESERVE);
52472553
QW
2826 if (ret < 0)
2827 goto cleanup;
003d7c59 2828 ret = qgroup_reserve(root, changeset.bytes_changed, true);
52472553
QW
2829 if (ret < 0)
2830 goto cleanup;
2831
6655bc3d 2832 ulist_release(&changeset.range_changed);
52472553
QW
2833 return ret;
2834
2835cleanup:
2836 /* cleanup already reserved ranges */
2837 ULIST_ITER_INIT(&uiter);
53d32359 2838 while ((unode = ulist_next(&changeset.range_changed, &uiter)))
52472553
QW
2839 clear_extent_bit(&BTRFS_I(inode)->io_tree, unode->val,
2840 unode->aux, EXTENT_QGROUP_RESERVED, 0, 0, NULL,
2841 GFP_NOFS);
6655bc3d 2842 ulist_release(&changeset.range_changed);
52472553
QW
2843 return ret;
2844}
f695fdce
QW
2845
2846static int __btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len,
2847 int free)
2848{
2849 struct extent_changeset changeset;
81fb6f77 2850 int trace_op = QGROUP_RELEASE;
f695fdce
QW
2851 int ret;
2852
2853 changeset.bytes_changed = 0;
53d32359 2854 ulist_init(&changeset.range_changed);
f695fdce 2855 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
f734c44a 2856 start + len -1, EXTENT_QGROUP_RESERVED, &changeset);
f695fdce
QW
2857 if (ret < 0)
2858 goto out;
2859
81fb6f77 2860 if (free) {
0b08e1f4
DS
2861 btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
2862 BTRFS_I(inode)->root->objectid,
2863 changeset.bytes_changed);
81fb6f77
QW
2864 trace_op = QGROUP_FREE;
2865 }
2866 trace_btrfs_qgroup_release_data(inode, start, len,
2867 changeset.bytes_changed, trace_op);
f695fdce 2868out:
6655bc3d 2869 ulist_release(&changeset.range_changed);
f695fdce
QW
2870 return ret;
2871}
2872
2873/*
2874 * Free a reserved space range from io_tree and related qgroups
2875 *
2876 * Should be called when a range of pages get invalidated before reaching disk.
2877 * Or for error cleanup case.
2878 *
2879 * For data written to disk, use btrfs_qgroup_release_data().
2880 *
2881 * NOTE: This function may sleep for memory allocation.
2882 */
2883int btrfs_qgroup_free_data(struct inode *inode, u64 start, u64 len)
2884{
2885 return __btrfs_qgroup_release_data(inode, start, len, 1);
2886}
2887
2888/*
2889 * Release a reserved space range from io_tree only.
2890 *
2891 * Should be called when a range of pages get written to disk and corresponding
2892 * FILE_EXTENT is inserted into corresponding root.
2893 *
2894 * Since new qgroup accounting framework will only update qgroup numbers at
2895 * commit_transaction() time, its reserved space shouldn't be freed from
2896 * related qgroups.
2897 *
2898 * But we should release the range from io_tree, to allow further write to be
2899 * COWed.
2900 *
2901 * NOTE: This function may sleep for memory allocation.
2902 */
2903int btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len)
2904{
2905 return __btrfs_qgroup_release_data(inode, start, len, 0);
2906}
55eeaf05 2907
003d7c59
JM
2908int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
2909 bool enforce)
55eeaf05 2910{
0b246afa 2911 struct btrfs_fs_info *fs_info = root->fs_info;
55eeaf05
QW
2912 int ret;
2913
0b246afa 2914 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
afcdd129 2915 !is_fstree(root->objectid) || num_bytes == 0)
55eeaf05
QW
2916 return 0;
2917
0b246afa 2918 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
3159fe7b 2919 trace_qgroup_meta_reserve(root, (s64)num_bytes);
003d7c59 2920 ret = qgroup_reserve(root, num_bytes, enforce);
55eeaf05
QW
2921 if (ret < 0)
2922 return ret;
ce0dcee6 2923 atomic64_add(num_bytes, &root->qgroup_meta_rsv);
55eeaf05
QW
2924 return ret;
2925}
2926
2927void btrfs_qgroup_free_meta_all(struct btrfs_root *root)
2928{
0b246afa 2929 struct btrfs_fs_info *fs_info = root->fs_info;
ce0dcee6 2930 u64 reserved;
55eeaf05 2931
0b246afa 2932 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
afcdd129 2933 !is_fstree(root->objectid))
55eeaf05
QW
2934 return;
2935
ce0dcee6 2936 reserved = atomic64_xchg(&root->qgroup_meta_rsv, 0);
55eeaf05
QW
2937 if (reserved == 0)
2938 return;
3159fe7b 2939 trace_qgroup_meta_reserve(root, -(s64)reserved);
0b08e1f4 2940 btrfs_qgroup_free_refroot(fs_info, root->objectid, reserved);
55eeaf05
QW
2941}
2942
2943void btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes)
2944{
0b246afa
JM
2945 struct btrfs_fs_info *fs_info = root->fs_info;
2946
2947 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
afcdd129 2948 !is_fstree(root->objectid))
55eeaf05
QW
2949 return;
2950
0b246afa 2951 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
ce0dcee6
GR
2952 WARN_ON(atomic64_read(&root->qgroup_meta_rsv) < num_bytes);
2953 atomic64_sub(num_bytes, &root->qgroup_meta_rsv);
3159fe7b 2954 trace_qgroup_meta_reserve(root, -(s64)num_bytes);
0b08e1f4 2955 btrfs_qgroup_free_refroot(fs_info, root->objectid, num_bytes);
55eeaf05 2956}
56fa9d07
QW
2957
2958/*
01327610 2959 * Check qgroup reserved space leaking, normally at destroy inode
56fa9d07
QW
2960 * time
2961 */
2962void btrfs_qgroup_check_reserved_leak(struct inode *inode)
2963{
2964 struct extent_changeset changeset;
2965 struct ulist_node *unode;
2966 struct ulist_iterator iter;
2967 int ret;
2968
2969 changeset.bytes_changed = 0;
53d32359 2970 ulist_init(&changeset.range_changed);
56fa9d07 2971 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
f734c44a 2972 EXTENT_QGROUP_RESERVED, &changeset);
56fa9d07
QW
2973
2974 WARN_ON(ret < 0);
2975 if (WARN_ON(changeset.bytes_changed)) {
2976 ULIST_ITER_INIT(&iter);
53d32359 2977 while ((unode = ulist_next(&changeset.range_changed, &iter))) {
56fa9d07
QW
2978 btrfs_warn(BTRFS_I(inode)->root->fs_info,
2979 "leaking qgroup reserved space, ino: %lu, start: %llu, end: %llu",
2980 inode->i_ino, unode->val, unode->aux);
2981 }
0b08e1f4
DS
2982 btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
2983 BTRFS_I(inode)->root->objectid,
2984 changeset.bytes_changed);
2985
56fa9d07 2986 }
6655bc3d 2987 ulist_release(&changeset.range_changed);
56fa9d07 2988}