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