btrfs: qgroup: update limit info in function btrfs_run_qgroups().
[linux-2.6-block.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
AJ
36
37/* TODO XXX FIXME
38 * - subvol delete -> delete when ref goes to 0? delete limits also?
39 * - reorganize keys
40 * - compressed
41 * - sync
bed92eae
AJ
42 * - copy also limits on subvol creation
43 * - limit
44 * - caches fuer ulists
45 * - performance benchmarks
46 * - check all ioctl parameters
47 */
48
49/*
50 * one struct for each qgroup, organized in fs_info->qgroup_tree.
51 */
52struct btrfs_qgroup {
53 u64 qgroupid;
54
55 /*
56 * state
57 */
58 u64 rfer; /* referenced */
59 u64 rfer_cmpr; /* referenced compressed */
60 u64 excl; /* exclusive */
61 u64 excl_cmpr; /* exclusive compressed */
62
63 /*
64 * limits
65 */
66 u64 lim_flags; /* which limits are set */
67 u64 max_rfer;
68 u64 max_excl;
69 u64 rsv_rfer;
70 u64 rsv_excl;
71
72 /*
73 * reservation tracking
74 */
75 u64 reserved;
76
77 /*
78 * lists
79 */
80 struct list_head groups; /* groups this group is member of */
81 struct list_head members; /* groups that are members of this group */
82 struct list_head dirty; /* dirty groups */
83 struct rb_node node; /* tree of qgroups */
84
85 /*
86 * temp variables for accounting operations
87 */
fcebe456
JB
88 u64 old_refcnt;
89 u64 new_refcnt;
bed92eae
AJ
90};
91
92/*
93 * glue structure to represent the relations between qgroups.
94 */
95struct btrfs_qgroup_list {
96 struct list_head next_group;
97 struct list_head next_member;
98 struct btrfs_qgroup *group;
99 struct btrfs_qgroup *member;
100};
101
fcebe456
JB
102#define ptr_to_u64(x) ((u64)(uintptr_t)x)
103#define u64_to_ptr(x) ((struct btrfs_qgroup *)(uintptr_t)x)
104
b382a324
JS
105static int
106qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
107 int init_flags);
108static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
2f232036 109
58400fce 110/* must be called with qgroup_ioctl_lock held */
bed92eae
AJ
111static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
112 u64 qgroupid)
113{
114 struct rb_node *n = fs_info->qgroup_tree.rb_node;
115 struct btrfs_qgroup *qgroup;
116
117 while (n) {
118 qgroup = rb_entry(n, struct btrfs_qgroup, node);
119 if (qgroup->qgroupid < qgroupid)
120 n = n->rb_left;
121 else if (qgroup->qgroupid > qgroupid)
122 n = n->rb_right;
123 else
124 return qgroup;
125 }
126 return NULL;
127}
128
129/* must be called with qgroup_lock held */
130static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
131 u64 qgroupid)
132{
133 struct rb_node **p = &fs_info->qgroup_tree.rb_node;
134 struct rb_node *parent = NULL;
135 struct btrfs_qgroup *qgroup;
136
137 while (*p) {
138 parent = *p;
139 qgroup = rb_entry(parent, struct btrfs_qgroup, node);
140
141 if (qgroup->qgroupid < qgroupid)
142 p = &(*p)->rb_left;
143 else if (qgroup->qgroupid > qgroupid)
144 p = &(*p)->rb_right;
145 else
146 return qgroup;
147 }
148
149 qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
150 if (!qgroup)
151 return ERR_PTR(-ENOMEM);
152
153 qgroup->qgroupid = qgroupid;
154 INIT_LIST_HEAD(&qgroup->groups);
155 INIT_LIST_HEAD(&qgroup->members);
156 INIT_LIST_HEAD(&qgroup->dirty);
157
158 rb_link_node(&qgroup->node, parent, p);
159 rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
160
161 return qgroup;
162}
163
4082bd3d 164static void __del_qgroup_rb(struct btrfs_qgroup *qgroup)
bed92eae 165{
bed92eae
AJ
166 struct btrfs_qgroup_list *list;
167
bed92eae 168 list_del(&qgroup->dirty);
bed92eae
AJ
169 while (!list_empty(&qgroup->groups)) {
170 list = list_first_entry(&qgroup->groups,
171 struct btrfs_qgroup_list, next_group);
172 list_del(&list->next_group);
173 list_del(&list->next_member);
174 kfree(list);
175 }
176
177 while (!list_empty(&qgroup->members)) {
178 list = list_first_entry(&qgroup->members,
179 struct btrfs_qgroup_list, next_member);
180 list_del(&list->next_group);
181 list_del(&list->next_member);
182 kfree(list);
183 }
184 kfree(qgroup);
4082bd3d 185}
bed92eae 186
4082bd3d
WS
187/* must be called with qgroup_lock held */
188static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
189{
190 struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
191
192 if (!qgroup)
193 return -ENOENT;
194
195 rb_erase(&qgroup->node, &fs_info->qgroup_tree);
196 __del_qgroup_rb(qgroup);
bed92eae
AJ
197 return 0;
198}
199
200/* must be called with qgroup_lock held */
201static int add_relation_rb(struct btrfs_fs_info *fs_info,
202 u64 memberid, u64 parentid)
203{
204 struct btrfs_qgroup *member;
205 struct btrfs_qgroup *parent;
206 struct btrfs_qgroup_list *list;
207
208 member = find_qgroup_rb(fs_info, memberid);
209 parent = find_qgroup_rb(fs_info, parentid);
210 if (!member || !parent)
211 return -ENOENT;
212
213 list = kzalloc(sizeof(*list), GFP_ATOMIC);
214 if (!list)
215 return -ENOMEM;
216
217 list->group = parent;
218 list->member = member;
219 list_add_tail(&list->next_group, &member->groups);
220 list_add_tail(&list->next_member, &parent->members);
221
222 return 0;
223}
224
225/* must be called with qgroup_lock held */
226static int del_relation_rb(struct btrfs_fs_info *fs_info,
227 u64 memberid, u64 parentid)
228{
229 struct btrfs_qgroup *member;
230 struct btrfs_qgroup *parent;
231 struct btrfs_qgroup_list *list;
232
233 member = find_qgroup_rb(fs_info, memberid);
234 parent = find_qgroup_rb(fs_info, parentid);
235 if (!member || !parent)
236 return -ENOENT;
237
238 list_for_each_entry(list, &member->groups, next_group) {
239 if (list->group == parent) {
240 list_del(&list->next_group);
241 list_del(&list->next_member);
242 kfree(list);
243 return 0;
244 }
245 }
246 return -ENOENT;
247}
248
faa2dbf0
JB
249#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
250int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
251 u64 rfer, u64 excl)
252{
253 struct btrfs_qgroup *qgroup;
254
255 qgroup = find_qgroup_rb(fs_info, qgroupid);
256 if (!qgroup)
257 return -EINVAL;
258 if (qgroup->rfer != rfer || qgroup->excl != excl)
259 return -EINVAL;
260 return 0;
261}
262#endif
263
bed92eae
AJ
264/*
265 * The full config is read in one go, only called from open_ctree()
266 * It doesn't use any locking, as at this point we're still single-threaded
267 */
268int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
269{
270 struct btrfs_key key;
271 struct btrfs_key found_key;
272 struct btrfs_root *quota_root = fs_info->quota_root;
273 struct btrfs_path *path = NULL;
274 struct extent_buffer *l;
275 int slot;
276 int ret = 0;
277 u64 flags = 0;
b382a324 278 u64 rescan_progress = 0;
bed92eae
AJ
279
280 if (!fs_info->quota_enabled)
281 return 0;
282
1e8f9158
WS
283 fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
284 if (!fs_info->qgroup_ulist) {
285 ret = -ENOMEM;
286 goto out;
287 }
288
bed92eae
AJ
289 path = btrfs_alloc_path();
290 if (!path) {
291 ret = -ENOMEM;
292 goto out;
293 }
294
295 /* default this to quota off, in case no status key is found */
296 fs_info->qgroup_flags = 0;
297
298 /*
299 * pass 1: read status, all qgroup infos and limits
300 */
301 key.objectid = 0;
302 key.type = 0;
303 key.offset = 0;
304 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
305 if (ret)
306 goto out;
307
308 while (1) {
309 struct btrfs_qgroup *qgroup;
310
311 slot = path->slots[0];
312 l = path->nodes[0];
313 btrfs_item_key_to_cpu(l, &found_key, slot);
314
315 if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
316 struct btrfs_qgroup_status_item *ptr;
317
318 ptr = btrfs_item_ptr(l, slot,
319 struct btrfs_qgroup_status_item);
320
321 if (btrfs_qgroup_status_version(l, ptr) !=
322 BTRFS_QGROUP_STATUS_VERSION) {
efe120a0
FH
323 btrfs_err(fs_info,
324 "old qgroup version, quota disabled");
bed92eae
AJ
325 goto out;
326 }
327 if (btrfs_qgroup_status_generation(l, ptr) !=
328 fs_info->generation) {
329 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
efe120a0
FH
330 btrfs_err(fs_info,
331 "qgroup generation mismatch, "
332 "marked as inconsistent");
bed92eae
AJ
333 }
334 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
335 ptr);
b382a324 336 rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
bed92eae
AJ
337 goto next1;
338 }
339
340 if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
341 found_key.type != BTRFS_QGROUP_LIMIT_KEY)
342 goto next1;
343
344 qgroup = find_qgroup_rb(fs_info, found_key.offset);
345 if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
346 (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
efe120a0 347 btrfs_err(fs_info, "inconsitent qgroup config");
bed92eae
AJ
348 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
349 }
350 if (!qgroup) {
351 qgroup = add_qgroup_rb(fs_info, found_key.offset);
352 if (IS_ERR(qgroup)) {
353 ret = PTR_ERR(qgroup);
354 goto out;
355 }
356 }
357 switch (found_key.type) {
358 case BTRFS_QGROUP_INFO_KEY: {
359 struct btrfs_qgroup_info_item *ptr;
360
361 ptr = btrfs_item_ptr(l, slot,
362 struct btrfs_qgroup_info_item);
363 qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
364 qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
365 qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
366 qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
367 /* generation currently unused */
368 break;
369 }
370 case BTRFS_QGROUP_LIMIT_KEY: {
371 struct btrfs_qgroup_limit_item *ptr;
372
373 ptr = btrfs_item_ptr(l, slot,
374 struct btrfs_qgroup_limit_item);
375 qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
376 qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
377 qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
378 qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
379 qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
380 break;
381 }
382 }
383next1:
384 ret = btrfs_next_item(quota_root, path);
385 if (ret < 0)
386 goto out;
387 if (ret)
388 break;
389 }
390 btrfs_release_path(path);
391
392 /*
393 * pass 2: read all qgroup relations
394 */
395 key.objectid = 0;
396 key.type = BTRFS_QGROUP_RELATION_KEY;
397 key.offset = 0;
398 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
399 if (ret)
400 goto out;
401 while (1) {
402 slot = path->slots[0];
403 l = path->nodes[0];
404 btrfs_item_key_to_cpu(l, &found_key, slot);
405
406 if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
407 goto next2;
408
409 if (found_key.objectid > found_key.offset) {
410 /* parent <- member, not needed to build config */
411 /* FIXME should we omit the key completely? */
412 goto next2;
413 }
414
415 ret = add_relation_rb(fs_info, found_key.objectid,
416 found_key.offset);
ff24858c 417 if (ret == -ENOENT) {
efe120a0
FH
418 btrfs_warn(fs_info,
419 "orphan qgroup relation 0x%llx->0x%llx",
c1c9ff7c 420 found_key.objectid, found_key.offset);
ff24858c
AJ
421 ret = 0; /* ignore the error */
422 }
bed92eae
AJ
423 if (ret)
424 goto out;
425next2:
426 ret = btrfs_next_item(quota_root, path);
427 if (ret < 0)
428 goto out;
429 if (ret)
430 break;
431 }
432out:
433 fs_info->qgroup_flags |= flags;
434 if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)) {
435 fs_info->quota_enabled = 0;
436 fs_info->pending_quota_state = 0;
b382a324
JS
437 } else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
438 ret >= 0) {
439 ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
bed92eae
AJ
440 }
441 btrfs_free_path(path);
442
eb1716af 443 if (ret < 0) {
1e8f9158 444 ulist_free(fs_info->qgroup_ulist);
eb1716af 445 fs_info->qgroup_ulist = NULL;
b382a324 446 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
eb1716af 447 }
1e8f9158 448
bed92eae
AJ
449 return ret < 0 ? ret : 0;
450}
451
452/*
e685da14
WS
453 * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
454 * first two are in single-threaded paths.And for the third one, we have set
455 * quota_root to be null with qgroup_lock held before, so it is safe to clean
456 * up the in-memory structures without qgroup_lock held.
bed92eae
AJ
457 */
458void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
459{
460 struct rb_node *n;
461 struct btrfs_qgroup *qgroup;
bed92eae
AJ
462
463 while ((n = rb_first(&fs_info->qgroup_tree))) {
464 qgroup = rb_entry(n, struct btrfs_qgroup, node);
465 rb_erase(n, &fs_info->qgroup_tree);
4082bd3d 466 __del_qgroup_rb(qgroup);
bed92eae 467 }
1e7bac1e
WS
468 /*
469 * we call btrfs_free_qgroup_config() when umounting
470 * filesystem and disabling quota, so we set qgroup_ulit
471 * to be null here to avoid double free.
472 */
1e8f9158 473 ulist_free(fs_info->qgroup_ulist);
1e7bac1e 474 fs_info->qgroup_ulist = NULL;
bed92eae
AJ
475}
476
477static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
478 struct btrfs_root *quota_root,
479 u64 src, u64 dst)
480{
481 int ret;
482 struct btrfs_path *path;
483 struct btrfs_key key;
484
485 path = btrfs_alloc_path();
486 if (!path)
487 return -ENOMEM;
488
489 key.objectid = src;
490 key.type = BTRFS_QGROUP_RELATION_KEY;
491 key.offset = dst;
492
493 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
494
495 btrfs_mark_buffer_dirty(path->nodes[0]);
496
497 btrfs_free_path(path);
498 return ret;
499}
500
501static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
502 struct btrfs_root *quota_root,
503 u64 src, u64 dst)
504{
505 int ret;
506 struct btrfs_path *path;
507 struct btrfs_key key;
508
509 path = btrfs_alloc_path();
510 if (!path)
511 return -ENOMEM;
512
513 key.objectid = src;
514 key.type = BTRFS_QGROUP_RELATION_KEY;
515 key.offset = dst;
516
517 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
518 if (ret < 0)
519 goto out;
520
521 if (ret > 0) {
522 ret = -ENOENT;
523 goto out;
524 }
525
526 ret = btrfs_del_item(trans, quota_root, path);
527out:
528 btrfs_free_path(path);
529 return ret;
530}
531
532static int add_qgroup_item(struct btrfs_trans_handle *trans,
533 struct btrfs_root *quota_root, u64 qgroupid)
534{
535 int ret;
536 struct btrfs_path *path;
537 struct btrfs_qgroup_info_item *qgroup_info;
538 struct btrfs_qgroup_limit_item *qgroup_limit;
539 struct extent_buffer *leaf;
540 struct btrfs_key key;
541
fccb84c9 542 if (btrfs_test_is_dummy_root(quota_root))
faa2dbf0 543 return 0;
fccb84c9 544
bed92eae
AJ
545 path = btrfs_alloc_path();
546 if (!path)
547 return -ENOMEM;
548
549 key.objectid = 0;
550 key.type = BTRFS_QGROUP_INFO_KEY;
551 key.offset = qgroupid;
552
0b4699dc
MF
553 /*
554 * Avoid a transaction abort by catching -EEXIST here. In that
555 * case, we proceed by re-initializing the existing structure
556 * on disk.
557 */
558
bed92eae
AJ
559 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
560 sizeof(*qgroup_info));
0b4699dc 561 if (ret && ret != -EEXIST)
bed92eae
AJ
562 goto out;
563
564 leaf = path->nodes[0];
565 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
566 struct btrfs_qgroup_info_item);
567 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
568 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
569 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
570 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
571 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
572
573 btrfs_mark_buffer_dirty(leaf);
574
575 btrfs_release_path(path);
576
577 key.type = BTRFS_QGROUP_LIMIT_KEY;
578 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
579 sizeof(*qgroup_limit));
0b4699dc 580 if (ret && ret != -EEXIST)
bed92eae
AJ
581 goto out;
582
583 leaf = path->nodes[0];
584 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
585 struct btrfs_qgroup_limit_item);
586 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
587 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
588 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
589 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
590 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
591
592 btrfs_mark_buffer_dirty(leaf);
593
594 ret = 0;
595out:
596 btrfs_free_path(path);
597 return ret;
598}
599
600static int del_qgroup_item(struct btrfs_trans_handle *trans,
601 struct btrfs_root *quota_root, u64 qgroupid)
602{
603 int ret;
604 struct btrfs_path *path;
605 struct btrfs_key key;
606
607 path = btrfs_alloc_path();
608 if (!path)
609 return -ENOMEM;
610
611 key.objectid = 0;
612 key.type = BTRFS_QGROUP_INFO_KEY;
613 key.offset = qgroupid;
614 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
615 if (ret < 0)
616 goto out;
617
618 if (ret > 0) {
619 ret = -ENOENT;
620 goto out;
621 }
622
623 ret = btrfs_del_item(trans, quota_root, path);
624 if (ret)
625 goto out;
626
627 btrfs_release_path(path);
628
629 key.type = BTRFS_QGROUP_LIMIT_KEY;
630 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
631 if (ret < 0)
632 goto out;
633
634 if (ret > 0) {
635 ret = -ENOENT;
636 goto out;
637 }
638
639 ret = btrfs_del_item(trans, quota_root, path);
640
641out:
642 btrfs_free_path(path);
643 return ret;
644}
645
646static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
1510e71c
DY
647 struct btrfs_root *root,
648 struct btrfs_qgroup *qgroup)
bed92eae
AJ
649{
650 struct btrfs_path *path;
651 struct btrfs_key key;
652 struct extent_buffer *l;
653 struct btrfs_qgroup_limit_item *qgroup_limit;
654 int ret;
655 int slot;
656
657 key.objectid = 0;
658 key.type = BTRFS_QGROUP_LIMIT_KEY;
1510e71c 659 key.offset = qgroup->qgroupid;
bed92eae
AJ
660
661 path = btrfs_alloc_path();
84cbe2f7
WS
662 if (!path)
663 return -ENOMEM;
664
bed92eae
AJ
665 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
666 if (ret > 0)
667 ret = -ENOENT;
668
669 if (ret)
670 goto out;
671
672 l = path->nodes[0];
673 slot = path->slots[0];
a3df41ee 674 qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
1510e71c
DY
675 btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
676 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
677 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
678 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
679 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
bed92eae
AJ
680
681 btrfs_mark_buffer_dirty(l);
682
683out:
684 btrfs_free_path(path);
685 return ret;
686}
687
688static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
689 struct btrfs_root *root,
690 struct btrfs_qgroup *qgroup)
691{
692 struct btrfs_path *path;
693 struct btrfs_key key;
694 struct extent_buffer *l;
695 struct btrfs_qgroup_info_item *qgroup_info;
696 int ret;
697 int slot;
698
fccb84c9 699 if (btrfs_test_is_dummy_root(root))
faa2dbf0 700 return 0;
fccb84c9 701
bed92eae
AJ
702 key.objectid = 0;
703 key.type = BTRFS_QGROUP_INFO_KEY;
704 key.offset = qgroup->qgroupid;
705
706 path = btrfs_alloc_path();
84cbe2f7
WS
707 if (!path)
708 return -ENOMEM;
709
bed92eae
AJ
710 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
711 if (ret > 0)
712 ret = -ENOENT;
713
714 if (ret)
715 goto out;
716
717 l = path->nodes[0];
718 slot = path->slots[0];
a3df41ee 719 qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
bed92eae
AJ
720 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
721 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
722 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
723 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
724 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
725
726 btrfs_mark_buffer_dirty(l);
727
728out:
729 btrfs_free_path(path);
730 return ret;
731}
732
733static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
734 struct btrfs_fs_info *fs_info,
735 struct btrfs_root *root)
736{
737 struct btrfs_path *path;
738 struct btrfs_key key;
739 struct extent_buffer *l;
740 struct btrfs_qgroup_status_item *ptr;
741 int ret;
742 int slot;
743
744 key.objectid = 0;
745 key.type = BTRFS_QGROUP_STATUS_KEY;
746 key.offset = 0;
747
748 path = btrfs_alloc_path();
84cbe2f7
WS
749 if (!path)
750 return -ENOMEM;
751
bed92eae
AJ
752 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
753 if (ret > 0)
754 ret = -ENOENT;
755
756 if (ret)
757 goto out;
758
759 l = path->nodes[0];
760 slot = path->slots[0];
761 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
762 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
763 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
2f232036
JS
764 btrfs_set_qgroup_status_rescan(l, ptr,
765 fs_info->qgroup_rescan_progress.objectid);
bed92eae
AJ
766
767 btrfs_mark_buffer_dirty(l);
768
769out:
770 btrfs_free_path(path);
771 return ret;
772}
773
774/*
775 * called with qgroup_lock held
776 */
777static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
778 struct btrfs_root *root)
779{
780 struct btrfs_path *path;
781 struct btrfs_key key;
06b3a860 782 struct extent_buffer *leaf = NULL;
bed92eae 783 int ret;
06b3a860 784 int nr = 0;
bed92eae 785
bed92eae
AJ
786 path = btrfs_alloc_path();
787 if (!path)
788 return -ENOMEM;
789
06b3a860
WS
790 path->leave_spinning = 1;
791
792 key.objectid = 0;
793 key.offset = 0;
794 key.type = 0;
bed92eae 795
06b3a860 796 while (1) {
bed92eae 797 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
06b3a860
WS
798 if (ret < 0)
799 goto out;
800 leaf = path->nodes[0];
801 nr = btrfs_header_nritems(leaf);
802 if (!nr)
bed92eae 803 break;
06b3a860
WS
804 /*
805 * delete the leaf one by one
806 * since the whole tree is going
807 * to be deleted.
808 */
809 path->slots[0] = 0;
810 ret = btrfs_del_items(trans, root, path, 0, nr);
bed92eae
AJ
811 if (ret)
812 goto out;
06b3a860 813
bed92eae
AJ
814 btrfs_release_path(path);
815 }
816 ret = 0;
817out:
818 root->fs_info->pending_quota_state = 0;
819 btrfs_free_path(path);
820 return ret;
821}
822
823int btrfs_quota_enable(struct btrfs_trans_handle *trans,
824 struct btrfs_fs_info *fs_info)
825{
826 struct btrfs_root *quota_root;
7708f029 827 struct btrfs_root *tree_root = fs_info->tree_root;
bed92eae
AJ
828 struct btrfs_path *path = NULL;
829 struct btrfs_qgroup_status_item *ptr;
830 struct extent_buffer *leaf;
831 struct btrfs_key key;
7708f029
WS
832 struct btrfs_key found_key;
833 struct btrfs_qgroup *qgroup = NULL;
bed92eae 834 int ret = 0;
7708f029 835 int slot;
bed92eae 836
f2f6ed3d 837 mutex_lock(&fs_info->qgroup_ioctl_lock);
bed92eae
AJ
838 if (fs_info->quota_root) {
839 fs_info->pending_quota_state = 1;
bed92eae
AJ
840 goto out;
841 }
bed92eae 842
1e8f9158
WS
843 fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
844 if (!fs_info->qgroup_ulist) {
845 ret = -ENOMEM;
846 goto out;
847 }
848
bed92eae
AJ
849 /*
850 * initially create the quota tree
851 */
852 quota_root = btrfs_create_tree(trans, fs_info,
853 BTRFS_QUOTA_TREE_OBJECTID);
854 if (IS_ERR(quota_root)) {
855 ret = PTR_ERR(quota_root);
856 goto out;
857 }
858
859 path = btrfs_alloc_path();
5b7ff5b3
TI
860 if (!path) {
861 ret = -ENOMEM;
862 goto out_free_root;
863 }
bed92eae
AJ
864
865 key.objectid = 0;
866 key.type = BTRFS_QGROUP_STATUS_KEY;
867 key.offset = 0;
868
869 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
870 sizeof(*ptr));
871 if (ret)
5b7ff5b3 872 goto out_free_path;
bed92eae
AJ
873
874 leaf = path->nodes[0];
875 ptr = btrfs_item_ptr(leaf, path->slots[0],
876 struct btrfs_qgroup_status_item);
877 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
878 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
879 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
880 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
881 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
2f232036 882 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
bed92eae
AJ
883
884 btrfs_mark_buffer_dirty(leaf);
885
7708f029
WS
886 key.objectid = 0;
887 key.type = BTRFS_ROOT_REF_KEY;
888 key.offset = 0;
889
890 btrfs_release_path(path);
891 ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
892 if (ret > 0)
893 goto out_add_root;
894 if (ret < 0)
895 goto out_free_path;
896
897
898 while (1) {
899 slot = path->slots[0];
900 leaf = path->nodes[0];
901 btrfs_item_key_to_cpu(leaf, &found_key, slot);
902
903 if (found_key.type == BTRFS_ROOT_REF_KEY) {
904 ret = add_qgroup_item(trans, quota_root,
905 found_key.offset);
906 if (ret)
907 goto out_free_path;
908
7708f029
WS
909 qgroup = add_qgroup_rb(fs_info, found_key.offset);
910 if (IS_ERR(qgroup)) {
7708f029
WS
911 ret = PTR_ERR(qgroup);
912 goto out_free_path;
913 }
7708f029
WS
914 }
915 ret = btrfs_next_item(tree_root, path);
916 if (ret < 0)
917 goto out_free_path;
918 if (ret)
919 break;
920 }
921
922out_add_root:
923 btrfs_release_path(path);
924 ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
925 if (ret)
926 goto out_free_path;
927
7708f029
WS
928 qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
929 if (IS_ERR(qgroup)) {
7708f029
WS
930 ret = PTR_ERR(qgroup);
931 goto out_free_path;
932 }
58400fce 933 spin_lock(&fs_info->qgroup_lock);
bed92eae
AJ
934 fs_info->quota_root = quota_root;
935 fs_info->pending_quota_state = 1;
936 spin_unlock(&fs_info->qgroup_lock);
5b7ff5b3 937out_free_path:
bed92eae 938 btrfs_free_path(path);
5b7ff5b3
TI
939out_free_root:
940 if (ret) {
941 free_extent_buffer(quota_root->node);
942 free_extent_buffer(quota_root->commit_root);
943 kfree(quota_root);
944 }
945out:
eb1716af 946 if (ret) {
1e8f9158 947 ulist_free(fs_info->qgroup_ulist);
eb1716af
JS
948 fs_info->qgroup_ulist = NULL;
949 }
f2f6ed3d 950 mutex_unlock(&fs_info->qgroup_ioctl_lock);
bed92eae
AJ
951 return ret;
952}
953
954int btrfs_quota_disable(struct btrfs_trans_handle *trans,
955 struct btrfs_fs_info *fs_info)
956{
957 struct btrfs_root *tree_root = fs_info->tree_root;
958 struct btrfs_root *quota_root;
959 int ret = 0;
960
f2f6ed3d 961 mutex_lock(&fs_info->qgroup_ioctl_lock);
58400fce 962 if (!fs_info->quota_root)
f2f6ed3d 963 goto out;
58400fce 964 spin_lock(&fs_info->qgroup_lock);
bed92eae
AJ
965 fs_info->quota_enabled = 0;
966 fs_info->pending_quota_state = 0;
967 quota_root = fs_info->quota_root;
968 fs_info->quota_root = NULL;
bed92eae
AJ
969 spin_unlock(&fs_info->qgroup_lock);
970
e685da14
WS
971 btrfs_free_qgroup_config(fs_info);
972
bed92eae
AJ
973 ret = btrfs_clean_quota_tree(trans, quota_root);
974 if (ret)
975 goto out;
976
977 ret = btrfs_del_root(trans, tree_root, &quota_root->root_key);
978 if (ret)
979 goto out;
980
981 list_del(&quota_root->dirty_list);
982
983 btrfs_tree_lock(quota_root->node);
01d58472 984 clean_tree_block(trans, tree_root->fs_info, quota_root->node);
bed92eae
AJ
985 btrfs_tree_unlock(quota_root->node);
986 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
987
988 free_extent_buffer(quota_root->node);
989 free_extent_buffer(quota_root->commit_root);
990 kfree(quota_root);
991out:
f2f6ed3d 992 mutex_unlock(&fs_info->qgroup_ioctl_lock);
bed92eae
AJ
993 return ret;
994}
995
2f232036
JS
996static void qgroup_dirty(struct btrfs_fs_info *fs_info,
997 struct btrfs_qgroup *qgroup)
bed92eae 998{
2f232036
JS
999 if (list_empty(&qgroup->dirty))
1000 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
bed92eae
AJ
1001}
1002
1003int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
1004 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1005{
1006 struct btrfs_root *quota_root;
b7fef4f5
WS
1007 struct btrfs_qgroup *parent;
1008 struct btrfs_qgroup *member;
534e6623 1009 struct btrfs_qgroup_list *list;
bed92eae
AJ
1010 int ret = 0;
1011
f2f6ed3d 1012 mutex_lock(&fs_info->qgroup_ioctl_lock);
bed92eae 1013 quota_root = fs_info->quota_root;
f2f6ed3d
WS
1014 if (!quota_root) {
1015 ret = -EINVAL;
1016 goto out;
1017 }
b7fef4f5
WS
1018 member = find_qgroup_rb(fs_info, src);
1019 parent = find_qgroup_rb(fs_info, dst);
1020 if (!member || !parent) {
1021 ret = -EINVAL;
1022 goto out;
1023 }
bed92eae 1024
534e6623
WS
1025 /* check if such qgroup relation exist firstly */
1026 list_for_each_entry(list, &member->groups, next_group) {
1027 if (list->group == parent) {
1028 ret = -EEXIST;
1029 goto out;
1030 }
1031 }
1032
bed92eae
AJ
1033 ret = add_qgroup_relation_item(trans, quota_root, src, dst);
1034 if (ret)
f2f6ed3d 1035 goto out;
bed92eae
AJ
1036
1037 ret = add_qgroup_relation_item(trans, quota_root, dst, src);
1038 if (ret) {
1039 del_qgroup_relation_item(trans, quota_root, src, dst);
f2f6ed3d 1040 goto out;
bed92eae
AJ
1041 }
1042
1043 spin_lock(&fs_info->qgroup_lock);
1044 ret = add_relation_rb(quota_root->fs_info, src, dst);
1045 spin_unlock(&fs_info->qgroup_lock);
f2f6ed3d
WS
1046out:
1047 mutex_unlock(&fs_info->qgroup_ioctl_lock);
bed92eae
AJ
1048 return ret;
1049}
1050
1051int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
1052 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1053{
1054 struct btrfs_root *quota_root;
534e6623
WS
1055 struct btrfs_qgroup *parent;
1056 struct btrfs_qgroup *member;
1057 struct btrfs_qgroup_list *list;
bed92eae
AJ
1058 int ret = 0;
1059 int err;
1060
f2f6ed3d 1061 mutex_lock(&fs_info->qgroup_ioctl_lock);
bed92eae 1062 quota_root = fs_info->quota_root;
f2f6ed3d
WS
1063 if (!quota_root) {
1064 ret = -EINVAL;
1065 goto out;
1066 }
bed92eae 1067
534e6623
WS
1068 member = find_qgroup_rb(fs_info, src);
1069 parent = find_qgroup_rb(fs_info, dst);
1070 if (!member || !parent) {
1071 ret = -EINVAL;
1072 goto out;
1073 }
1074
1075 /* check if such qgroup relation exist firstly */
1076 list_for_each_entry(list, &member->groups, next_group) {
1077 if (list->group == parent)
1078 goto exist;
1079 }
1080 ret = -ENOENT;
1081 goto out;
1082exist:
bed92eae
AJ
1083 ret = del_qgroup_relation_item(trans, quota_root, src, dst);
1084 err = del_qgroup_relation_item(trans, quota_root, dst, src);
1085 if (err && !ret)
1086 ret = err;
1087
1088 spin_lock(&fs_info->qgroup_lock);
1089 del_relation_rb(fs_info, src, dst);
bed92eae 1090 spin_unlock(&fs_info->qgroup_lock);
f2f6ed3d
WS
1091out:
1092 mutex_unlock(&fs_info->qgroup_ioctl_lock);
bed92eae
AJ
1093 return ret;
1094}
1095
1096int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
1097 struct btrfs_fs_info *fs_info, u64 qgroupid, char *name)
1098{
1099 struct btrfs_root *quota_root;
1100 struct btrfs_qgroup *qgroup;
1101 int ret = 0;
1102
f2f6ed3d 1103 mutex_lock(&fs_info->qgroup_ioctl_lock);
bed92eae 1104 quota_root = fs_info->quota_root;
f2f6ed3d
WS
1105 if (!quota_root) {
1106 ret = -EINVAL;
1107 goto out;
1108 }
534e6623
WS
1109 qgroup = find_qgroup_rb(fs_info, qgroupid);
1110 if (qgroup) {
1111 ret = -EEXIST;
1112 goto out;
1113 }
bed92eae
AJ
1114
1115 ret = add_qgroup_item(trans, quota_root, qgroupid);
534e6623
WS
1116 if (ret)
1117 goto out;
bed92eae
AJ
1118
1119 spin_lock(&fs_info->qgroup_lock);
1120 qgroup = add_qgroup_rb(fs_info, qgroupid);
1121 spin_unlock(&fs_info->qgroup_lock);
1122
1123 if (IS_ERR(qgroup))
1124 ret = PTR_ERR(qgroup);
f2f6ed3d
WS
1125out:
1126 mutex_unlock(&fs_info->qgroup_ioctl_lock);
bed92eae
AJ
1127 return ret;
1128}
1129
1130int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
1131 struct btrfs_fs_info *fs_info, u64 qgroupid)
1132{
1133 struct btrfs_root *quota_root;
2cf68703 1134 struct btrfs_qgroup *qgroup;
bed92eae
AJ
1135 int ret = 0;
1136
f2f6ed3d 1137 mutex_lock(&fs_info->qgroup_ioctl_lock);
bed92eae 1138 quota_root = fs_info->quota_root;
f2f6ed3d
WS
1139 if (!quota_root) {
1140 ret = -EINVAL;
1141 goto out;
1142 }
bed92eae 1143
2cf68703 1144 qgroup = find_qgroup_rb(fs_info, qgroupid);
534e6623
WS
1145 if (!qgroup) {
1146 ret = -ENOENT;
1147 goto out;
1148 } else {
1149 /* check if there are no relations to this qgroup */
1150 if (!list_empty(&qgroup->groups) ||
1151 !list_empty(&qgroup->members)) {
f2f6ed3d
WS
1152 ret = -EBUSY;
1153 goto out;
2cf68703
AJ
1154 }
1155 }
bed92eae
AJ
1156 ret = del_qgroup_item(trans, quota_root, qgroupid);
1157
1158 spin_lock(&fs_info->qgroup_lock);
1159 del_qgroup_rb(quota_root->fs_info, qgroupid);
bed92eae 1160 spin_unlock(&fs_info->qgroup_lock);
f2f6ed3d
WS
1161out:
1162 mutex_unlock(&fs_info->qgroup_ioctl_lock);
bed92eae
AJ
1163 return ret;
1164}
1165
1166int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
1167 struct btrfs_fs_info *fs_info, u64 qgroupid,
1168 struct btrfs_qgroup_limit *limit)
1169{
f2f6ed3d 1170 struct btrfs_root *quota_root;
bed92eae
AJ
1171 struct btrfs_qgroup *qgroup;
1172 int ret = 0;
1173
f2f6ed3d
WS
1174 mutex_lock(&fs_info->qgroup_ioctl_lock);
1175 quota_root = fs_info->quota_root;
1176 if (!quota_root) {
1177 ret = -EINVAL;
1178 goto out;
1179 }
bed92eae 1180
ddb47afa
WS
1181 qgroup = find_qgroup_rb(fs_info, qgroupid);
1182 if (!qgroup) {
1183 ret = -ENOENT;
1184 goto out;
1185 }
bed92eae 1186
58400fce 1187 spin_lock(&fs_info->qgroup_lock);
bed92eae
AJ
1188 qgroup->lim_flags = limit->flags;
1189 qgroup->max_rfer = limit->max_rfer;
1190 qgroup->max_excl = limit->max_excl;
1191 qgroup->rsv_rfer = limit->rsv_rfer;
1192 qgroup->rsv_excl = limit->rsv_excl;
bed92eae 1193 spin_unlock(&fs_info->qgroup_lock);
1510e71c
DY
1194
1195 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
1196 if (ret) {
1197 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1198 btrfs_info(fs_info, "unable to update quota limit for %llu",
1199 qgroupid);
1200 }
1201
f2f6ed3d
WS
1202out:
1203 mutex_unlock(&fs_info->qgroup_ioctl_lock);
bed92eae
AJ
1204 return ret;
1205}
1152651a
MF
1206
1207static int comp_oper_exist(struct btrfs_qgroup_operation *oper1,
1208 struct btrfs_qgroup_operation *oper2)
1209{
1210 /*
1211 * Ignore seq and type here, we're looking for any operation
1212 * at all related to this extent on that root.
1213 */
1214 if (oper1->bytenr < oper2->bytenr)
1215 return -1;
1216 if (oper1->bytenr > oper2->bytenr)
1217 return 1;
1218 if (oper1->ref_root < oper2->ref_root)
1219 return -1;
1220 if (oper1->ref_root > oper2->ref_root)
1221 return 1;
1222 return 0;
1223}
1224
1225static int qgroup_oper_exists(struct btrfs_fs_info *fs_info,
1226 struct btrfs_qgroup_operation *oper)
1227{
1228 struct rb_node *n;
1229 struct btrfs_qgroup_operation *cur;
1230 int cmp;
1231
1232 spin_lock(&fs_info->qgroup_op_lock);
1233 n = fs_info->qgroup_op_tree.rb_node;
1234 while (n) {
1235 cur = rb_entry(n, struct btrfs_qgroup_operation, n);
1236 cmp = comp_oper_exist(cur, oper);
1237 if (cmp < 0) {
1238 n = n->rb_right;
1239 } else if (cmp) {
1240 n = n->rb_left;
1241 } else {
1242 spin_unlock(&fs_info->qgroup_op_lock);
1243 return -EEXIST;
1244 }
1245 }
1246 spin_unlock(&fs_info->qgroup_op_lock);
1247 return 0;
1248}
1249
fcebe456
JB
1250static int comp_oper(struct btrfs_qgroup_operation *oper1,
1251 struct btrfs_qgroup_operation *oper2)
1252{
1253 if (oper1->bytenr < oper2->bytenr)
1254 return -1;
1255 if (oper1->bytenr > oper2->bytenr)
1256 return 1;
fcebe456
JB
1257 if (oper1->ref_root < oper2->ref_root)
1258 return -1;
1259 if (oper1->ref_root > oper2->ref_root)
1260 return 1;
bf691960
FM
1261 if (oper1->seq < oper2->seq)
1262 return -1;
1263 if (oper1->seq > oper2->seq)
1264 return 1;
fcebe456
JB
1265 if (oper1->type < oper2->type)
1266 return -1;
1267 if (oper1->type > oper2->type)
1268 return 1;
1269 return 0;
1270}
1271
1272static int insert_qgroup_oper(struct btrfs_fs_info *fs_info,
1273 struct btrfs_qgroup_operation *oper)
1274{
1275 struct rb_node **p;
1276 struct rb_node *parent = NULL;
1277 struct btrfs_qgroup_operation *cur;
1278 int cmp;
1279
1280 spin_lock(&fs_info->qgroup_op_lock);
1281 p = &fs_info->qgroup_op_tree.rb_node;
1282 while (*p) {
1283 parent = *p;
1284 cur = rb_entry(parent, struct btrfs_qgroup_operation, n);
1285 cmp = comp_oper(cur, oper);
1286 if (cmp < 0) {
1287 p = &(*p)->rb_right;
1288 } else if (cmp) {
1289 p = &(*p)->rb_left;
1290 } else {
1291 spin_unlock(&fs_info->qgroup_op_lock);
1292 return -EEXIST;
1293 }
1294 }
1295 rb_link_node(&oper->n, parent, p);
1296 rb_insert_color(&oper->n, &fs_info->qgroup_op_tree);
1297 spin_unlock(&fs_info->qgroup_op_lock);
1298 return 0;
1299}
bed92eae 1300
bed92eae 1301/*
fcebe456
JB
1302 * Record a quota operation for processing later on.
1303 * @trans: the transaction we are adding the delayed op to.
1304 * @fs_info: the fs_info for this fs.
1305 * @ref_root: the root of the reference we are acting on,
1306 * @bytenr: the bytenr we are acting on.
1307 * @num_bytes: the number of bytes in the reference.
1308 * @type: the type of operation this is.
1309 * @mod_seq: do we need to get a sequence number for looking up roots.
1310 *
1311 * We just add it to our trans qgroup_ref_list and carry on and process these
1312 * operations in order at some later point. If the reference root isn't a fs
1313 * root then we don't bother with doing anything.
1314 *
1315 * MUST BE HOLDING THE REF LOCK.
bed92eae
AJ
1316 */
1317int btrfs_qgroup_record_ref(struct btrfs_trans_handle *trans,
fcebe456
JB
1318 struct btrfs_fs_info *fs_info, u64 ref_root,
1319 u64 bytenr, u64 num_bytes,
1320 enum btrfs_qgroup_operation_type type, int mod_seq)
bed92eae 1321{
fcebe456
JB
1322 struct btrfs_qgroup_operation *oper;
1323 int ret;
bed92eae 1324
fcebe456
JB
1325 if (!is_fstree(ref_root) || !fs_info->quota_enabled)
1326 return 0;
1327
1328 oper = kmalloc(sizeof(*oper), GFP_NOFS);
1329 if (!oper)
bed92eae
AJ
1330 return -ENOMEM;
1331
fcebe456
JB
1332 oper->ref_root = ref_root;
1333 oper->bytenr = bytenr;
1334 oper->num_bytes = num_bytes;
1335 oper->type = type;
1336 oper->seq = atomic_inc_return(&fs_info->qgroup_op_seq);
1337 INIT_LIST_HEAD(&oper->elem.list);
1338 oper->elem.seq = 0;
1152651a 1339
d3982100
MF
1340 trace_btrfs_qgroup_record_ref(oper);
1341
1152651a
MF
1342 if (type == BTRFS_QGROUP_OPER_SUB_SUBTREE) {
1343 /*
1344 * If any operation for this bytenr/ref_root combo
1345 * exists, then we know it's not exclusively owned and
1346 * shouldn't be queued up.
1347 *
1348 * This also catches the case where we have a cloned
1349 * extent that gets queued up multiple times during
1350 * drop snapshot.
1351 */
1352 if (qgroup_oper_exists(fs_info, oper)) {
1353 kfree(oper);
1354 return 0;
1355 }
1356 }
1357
fcebe456
JB
1358 ret = insert_qgroup_oper(fs_info, oper);
1359 if (ret) {
1360 /* Shouldn't happen so have an assert for developers */
1361 ASSERT(0);
1362 kfree(oper);
1363 return ret;
1364 }
1365 list_add_tail(&oper->list, &trans->qgroup_ref_list);
1366
1367 if (mod_seq)
1368 btrfs_get_tree_mod_seq(fs_info, &oper->elem);
bed92eae
AJ
1369
1370 return 0;
1371}
1372
fcebe456
JB
1373/*
1374 * The easy accounting, if we are adding/removing the only ref for an extent
1375 * then this qgroup and all of the parent qgroups get their refrence and
1376 * exclusive counts adjusted.
1377 */
1378static int qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
1379 struct btrfs_qgroup_operation *oper)
1380{
1381 struct btrfs_qgroup *qgroup;
1382 struct ulist *tmp;
1383 struct btrfs_qgroup_list *glist;
1384 struct ulist_node *unode;
1385 struct ulist_iterator uiter;
1386 int sign = 0;
1387 int ret = 0;
1388
1389 tmp = ulist_alloc(GFP_NOFS);
1390 if (!tmp)
1391 return -ENOMEM;
1392
1393 spin_lock(&fs_info->qgroup_lock);
1394 if (!fs_info->quota_root)
1395 goto out;
1396 qgroup = find_qgroup_rb(fs_info, oper->ref_root);
1397 if (!qgroup)
1398 goto out;
1399 switch (oper->type) {
1400 case BTRFS_QGROUP_OPER_ADD_EXCL:
1401 sign = 1;
1402 break;
1403 case BTRFS_QGROUP_OPER_SUB_EXCL:
1404 sign = -1;
1405 break;
1406 default:
1407 ASSERT(0);
1408 }
1409 qgroup->rfer += sign * oper->num_bytes;
1410 qgroup->rfer_cmpr += sign * oper->num_bytes;
1411
1412 WARN_ON(sign < 0 && qgroup->excl < oper->num_bytes);
1413 qgroup->excl += sign * oper->num_bytes;
1414 qgroup->excl_cmpr += sign * oper->num_bytes;
1415
1416 qgroup_dirty(fs_info, qgroup);
1417
1418 /* Get all of the parent groups that contain this qgroup */
1419 list_for_each_entry(glist, &qgroup->groups, next_group) {
1420 ret = ulist_add(tmp, glist->group->qgroupid,
1421 ptr_to_u64(glist->group), GFP_ATOMIC);
1422 if (ret < 0)
1423 goto out;
1424 }
1425
1426 /* Iterate all of the parents and adjust their reference counts */
1427 ULIST_ITER_INIT(&uiter);
1428 while ((unode = ulist_next(tmp, &uiter))) {
1429 qgroup = u64_to_ptr(unode->aux);
1430 qgroup->rfer += sign * oper->num_bytes;
1431 qgroup->rfer_cmpr += sign * oper->num_bytes;
0ee13fe2 1432 WARN_ON(sign < 0 && qgroup->excl < oper->num_bytes);
fcebe456 1433 qgroup->excl += sign * oper->num_bytes;
fcebe456
JB
1434 qgroup->excl_cmpr += sign * oper->num_bytes;
1435 qgroup_dirty(fs_info, qgroup);
1436
1437 /* Add any parents of the parents */
1438 list_for_each_entry(glist, &qgroup->groups, next_group) {
1439 ret = ulist_add(tmp, glist->group->qgroupid,
1440 ptr_to_u64(glist->group), GFP_ATOMIC);
1441 if (ret < 0)
1442 goto out;
1443 }
1444 }
1445 ret = 0;
1446out:
1447 spin_unlock(&fs_info->qgroup_lock);
1448 ulist_free(tmp);
1449 return ret;
1450}
1451
1452/*
1453 * Walk all of the roots that pointed to our bytenr and adjust their refcnts as
1454 * properly.
1455 */
1456static int qgroup_calc_old_refcnt(struct btrfs_fs_info *fs_info,
1457 u64 root_to_skip, struct ulist *tmp,
1458 struct ulist *roots, struct ulist *qgroups,
1459 u64 seq, int *old_roots, int rescan)
46b665ce
JS
1460{
1461 struct ulist_node *unode;
1462 struct ulist_iterator uiter;
1463 struct ulist_node *tmp_unode;
1464 struct ulist_iterator tmp_uiter;
1465 struct btrfs_qgroup *qg;
1466 int ret;
1467
1468 ULIST_ITER_INIT(&uiter);
1469 while ((unode = ulist_next(roots, &uiter))) {
fcebe456
JB
1470 /* We don't count our current root here */
1471 if (unode->val == root_to_skip)
1472 continue;
46b665ce
JS
1473 qg = find_qgroup_rb(fs_info, unode->val);
1474 if (!qg)
1475 continue;
fcebe456
JB
1476 /*
1477 * We could have a pending removal of this same ref so we may
1478 * not have actually found our ref root when doing
1479 * btrfs_find_all_roots, so we need to keep track of how many
1480 * old roots we find in case we removed ours and added a
1481 * different one at the same time. I don't think this could
1482 * happen in practice but that sort of thinking leads to pain
1483 * and suffering and to the dark side.
1484 */
1485 (*old_roots)++;
46b665ce
JS
1486
1487 ulist_reinit(tmp);
fcebe456
JB
1488 ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg),
1489 GFP_ATOMIC);
1490 if (ret < 0)
1491 return ret;
1492 ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg), GFP_ATOMIC);
46b665ce
JS
1493 if (ret < 0)
1494 return ret;
1495 ULIST_ITER_INIT(&tmp_uiter);
1496 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1497 struct btrfs_qgroup_list *glist;
1498
fcebe456
JB
1499 qg = u64_to_ptr(tmp_unode->aux);
1500 /*
1501 * We use this sequence number to keep from having to
1502 * run the whole list and 0 out the refcnt every time.
1503 * We basically use sequnce as the known 0 count and
1504 * then add 1 everytime we see a qgroup. This is how we
1505 * get how many of the roots actually point up to the
1506 * upper level qgroups in order to determine exclusive
1507 * counts.
1508 *
1509 * For rescan we want to set old_refcnt to seq so our
1510 * exclusive calculations end up correct.
1511 */
1512 if (rescan)
1513 qg->old_refcnt = seq;
1514 else if (qg->old_refcnt < seq)
1515 qg->old_refcnt = seq + 1;
46b665ce 1516 else
fcebe456 1517 qg->old_refcnt++;
46b665ce 1518
fcebe456
JB
1519 if (qg->new_refcnt < seq)
1520 qg->new_refcnt = seq + 1;
1521 else
1522 qg->new_refcnt++;
46b665ce 1523 list_for_each_entry(glist, &qg->groups, next_group) {
fcebe456
JB
1524 ret = ulist_add(qgroups, glist->group->qgroupid,
1525 ptr_to_u64(glist->group),
1526 GFP_ATOMIC);
1527 if (ret < 0)
1528 return ret;
46b665ce 1529 ret = ulist_add(tmp, glist->group->qgroupid,
fcebe456 1530 ptr_to_u64(glist->group),
46b665ce
JS
1531 GFP_ATOMIC);
1532 if (ret < 0)
1533 return ret;
1534 }
1535 }
1536 }
fcebe456
JB
1537 return 0;
1538}
46b665ce 1539
fcebe456
JB
1540/*
1541 * We need to walk forward in our operation tree and account for any roots that
1542 * were deleted after we made this operation.
1543 */
1544static int qgroup_account_deleted_refs(struct btrfs_fs_info *fs_info,
1545 struct btrfs_qgroup_operation *oper,
1546 struct ulist *tmp,
1547 struct ulist *qgroups, u64 seq,
1548 int *old_roots)
1549{
1550 struct ulist_node *unode;
1551 struct ulist_iterator uiter;
1552 struct btrfs_qgroup *qg;
1553 struct btrfs_qgroup_operation *tmp_oper;
1554 struct rb_node *n;
1555 int ret;
1556
1557 ulist_reinit(tmp);
1558
1559 /*
1560 * We only walk forward in the tree since we're only interested in
1561 * removals that happened _after_ our operation.
1562 */
1563 spin_lock(&fs_info->qgroup_op_lock);
1564 n = rb_next(&oper->n);
1565 spin_unlock(&fs_info->qgroup_op_lock);
1566 if (!n)
1567 return 0;
1568 tmp_oper = rb_entry(n, struct btrfs_qgroup_operation, n);
1569 while (tmp_oper->bytenr == oper->bytenr) {
1570 /*
1571 * If it's not a removal we don't care, additions work out
1572 * properly with our refcnt tracking.
1573 */
1574 if (tmp_oper->type != BTRFS_QGROUP_OPER_SUB_SHARED &&
1575 tmp_oper->type != BTRFS_QGROUP_OPER_SUB_EXCL)
1576 goto next;
1577 qg = find_qgroup_rb(fs_info, tmp_oper->ref_root);
1578 if (!qg)
1579 goto next;
1580 ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg),
1581 GFP_ATOMIC);
1582 if (ret) {
1583 if (ret < 0)
1584 return ret;
1585 /*
1586 * We only want to increase old_roots if this qgroup is
1587 * not already in the list of qgroups. If it is already
1588 * there then that means it must have been re-added or
1589 * the delete will be discarded because we had an
1590 * existing ref that we haven't looked up yet. In this
1591 * case we don't want to increase old_roots. So if ret
1592 * == 1 then we know that this is the first time we've
1593 * seen this qgroup and we can bump the old_roots.
1594 */
1595 (*old_roots)++;
1596 ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg),
1597 GFP_ATOMIC);
1598 if (ret < 0)
1599 return ret;
1600 }
1601next:
1602 spin_lock(&fs_info->qgroup_op_lock);
1603 n = rb_next(&tmp_oper->n);
1604 spin_unlock(&fs_info->qgroup_op_lock);
1605 if (!n)
1606 break;
1607 tmp_oper = rb_entry(n, struct btrfs_qgroup_operation, n);
1608 }
1609
1610 /* Ok now process the qgroups we found */
1611 ULIST_ITER_INIT(&uiter);
1612 while ((unode = ulist_next(tmp, &uiter))) {
1613 struct btrfs_qgroup_list *glist;
1614
1615 qg = u64_to_ptr(unode->aux);
1616 if (qg->old_refcnt < seq)
1617 qg->old_refcnt = seq + 1;
1618 else
1619 qg->old_refcnt++;
1620 if (qg->new_refcnt < seq)
1621 qg->new_refcnt = seq + 1;
1622 else
1623 qg->new_refcnt++;
1624 list_for_each_entry(glist, &qg->groups, next_group) {
1625 ret = ulist_add(qgroups, glist->group->qgroupid,
1626 ptr_to_u64(glist->group), GFP_ATOMIC);
1627 if (ret < 0)
1628 return ret;
1629 ret = ulist_add(tmp, glist->group->qgroupid,
1630 ptr_to_u64(glist->group), GFP_ATOMIC);
1631 if (ret < 0)
1632 return ret;
1633 }
1634 }
46b665ce
JS
1635 return 0;
1636}
1637
fcebe456
JB
1638/* Add refcnt for the newly added reference. */
1639static int qgroup_calc_new_refcnt(struct btrfs_fs_info *fs_info,
1640 struct btrfs_qgroup_operation *oper,
1641 struct btrfs_qgroup *qgroup,
1642 struct ulist *tmp, struct ulist *qgroups,
1643 u64 seq)
46b665ce
JS
1644{
1645 struct ulist_node *unode;
1646 struct ulist_iterator uiter;
1647 struct btrfs_qgroup *qg;
46b665ce
JS
1648 int ret;
1649
1650 ulist_reinit(tmp);
fcebe456
JB
1651 ret = ulist_add(qgroups, qgroup->qgroupid, ptr_to_u64(qgroup),
1652 GFP_ATOMIC);
1653 if (ret < 0)
1654 return ret;
1655 ret = ulist_add(tmp, qgroup->qgroupid, ptr_to_u64(qgroup),
1656 GFP_ATOMIC);
46b665ce
JS
1657 if (ret < 0)
1658 return ret;
46b665ce
JS
1659 ULIST_ITER_INIT(&uiter);
1660 while ((unode = ulist_next(tmp, &uiter))) {
fcebe456 1661 struct btrfs_qgroup_list *glist;
46b665ce 1662
fcebe456
JB
1663 qg = u64_to_ptr(unode->aux);
1664 if (oper->type == BTRFS_QGROUP_OPER_ADD_SHARED) {
1665 if (qg->new_refcnt < seq)
1666 qg->new_refcnt = seq + 1;
1667 else
1668 qg->new_refcnt++;
1669 } else {
1670 if (qg->old_refcnt < seq)
1671 qg->old_refcnt = seq + 1;
1672 else
1673 qg->old_refcnt++;
1674 }
46b665ce
JS
1675 list_for_each_entry(glist, &qg->groups, next_group) {
1676 ret = ulist_add(tmp, glist->group->qgroupid,
fcebe456
JB
1677 ptr_to_u64(glist->group), GFP_ATOMIC);
1678 if (ret < 0)
1679 return ret;
1680 ret = ulist_add(qgroups, glist->group->qgroupid,
1681 ptr_to_u64(glist->group), GFP_ATOMIC);
46b665ce
JS
1682 if (ret < 0)
1683 return ret;
1684 }
1685 }
46b665ce
JS
1686 return 0;
1687}
1688
fcebe456
JB
1689/*
1690 * This adjusts the counters for all referenced qgroups if need be.
1691 */
1692static int qgroup_adjust_counters(struct btrfs_fs_info *fs_info,
1693 u64 root_to_skip, u64 num_bytes,
1694 struct ulist *qgroups, u64 seq,
1695 int old_roots, int new_roots, int rescan)
46b665ce
JS
1696{
1697 struct ulist_node *unode;
1698 struct ulist_iterator uiter;
1699 struct btrfs_qgroup *qg;
fcebe456 1700 u64 cur_new_count, cur_old_count;
46b665ce
JS
1701
1702 ULIST_ITER_INIT(&uiter);
fcebe456
JB
1703 while ((unode = ulist_next(qgroups, &uiter))) {
1704 bool dirty = false;
46b665ce 1705
fcebe456
JB
1706 qg = u64_to_ptr(unode->aux);
1707 /*
1708 * Wasn't referenced before but is now, add to the reference
1709 * counters.
1710 */
1711 if (qg->old_refcnt <= seq && qg->new_refcnt > seq) {
1712 qg->rfer += num_bytes;
1713 qg->rfer_cmpr += num_bytes;
1714 dirty = true;
1715 }
46b665ce 1716
fcebe456
JB
1717 /*
1718 * Was referenced before but isn't now, subtract from the
1719 * reference counters.
1720 */
1721 if (qg->old_refcnt > seq && qg->new_refcnt <= seq) {
1722 qg->rfer -= num_bytes;
1723 qg->rfer_cmpr -= num_bytes;
1724 dirty = true;
1725 }
46b665ce 1726
fcebe456
JB
1727 if (qg->old_refcnt < seq)
1728 cur_old_count = 0;
1729 else
1730 cur_old_count = qg->old_refcnt - seq;
1731 if (qg->new_refcnt < seq)
1732 cur_new_count = 0;
1733 else
1734 cur_new_count = qg->new_refcnt - seq;
46b665ce 1735
fcebe456
JB
1736 /*
1737 * If our refcount was the same as the roots previously but our
1738 * new count isn't the same as the number of roots now then we
1739 * went from having a exclusive reference on this range to not.
1740 */
1741 if (old_roots && cur_old_count == old_roots &&
1742 (cur_new_count != new_roots || new_roots == 0)) {
1743 WARN_ON(cur_new_count != new_roots && new_roots == 0);
1744 qg->excl -= num_bytes;
1745 qg->excl_cmpr -= num_bytes;
1746 dirty = true;
1747 }
46b665ce 1748
fcebe456
JB
1749 /*
1750 * If we didn't reference all the roots before but now we do we
1751 * have an exclusive reference to this range.
1752 */
1753 if ((!old_roots || (old_roots && cur_old_count != old_roots))
1754 && cur_new_count == new_roots) {
1755 qg->excl += num_bytes;
1756 qg->excl_cmpr += num_bytes;
1757 dirty = true;
46b665ce 1758 }
46b665ce 1759
fcebe456
JB
1760 if (dirty)
1761 qgroup_dirty(fs_info, qg);
1762 }
46b665ce
JS
1763 return 0;
1764}
1765
bed92eae 1766/*
fcebe456
JB
1767 * If we removed a data extent and there were other references for that bytenr
1768 * then we need to lookup all referenced roots to make sure we still don't
1769 * reference this bytenr. If we do then we can just discard this operation.
bed92eae 1770 */
fcebe456
JB
1771static int check_existing_refs(struct btrfs_trans_handle *trans,
1772 struct btrfs_fs_info *fs_info,
1773 struct btrfs_qgroup_operation *oper)
bed92eae 1774{
bed92eae 1775 struct ulist *roots = NULL;
fcebe456
JB
1776 struct ulist_node *unode;
1777 struct ulist_iterator uiter;
bed92eae 1778 int ret = 0;
bed92eae 1779
fcebe456
JB
1780 ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr,
1781 oper->elem.seq, &roots);
1782 if (ret < 0)
1783 return ret;
1784 ret = 0;
bed92eae 1785
fcebe456
JB
1786 ULIST_ITER_INIT(&uiter);
1787 while ((unode = ulist_next(roots, &uiter))) {
1788 if (unode->val == oper->ref_root) {
1789 ret = 1;
1790 break;
1791 }
bed92eae 1792 }
fcebe456
JB
1793 ulist_free(roots);
1794 btrfs_put_tree_mod_seq(fs_info, &oper->elem);
bed92eae 1795
fcebe456
JB
1796 return ret;
1797}
bed92eae 1798
fcebe456
JB
1799/*
1800 * If we share a reference across multiple roots then we may need to adjust
1801 * various qgroups referenced and exclusive counters. The basic premise is this
1802 *
1803 * 1) We have seq to represent a 0 count. Instead of looping through all of the
1804 * qgroups and resetting their refcount to 0 we just constantly bump this
1805 * sequence number to act as the base reference count. This means that if
1806 * anybody is equal to or below this sequence they were never referenced. We
1807 * jack this sequence up by the number of roots we found each time in order to
1808 * make sure we don't have any overlap.
1809 *
1810 * 2) We first search all the roots that reference the area _except_ the root
1811 * we're acting on currently. This makes up the old_refcnt of all the qgroups
1812 * before.
1813 *
1814 * 3) We walk all of the qgroups referenced by the root we are currently acting
1815 * on, and will either adjust old_refcnt in the case of a removal or the
1816 * new_refcnt in the case of an addition.
1817 *
1818 * 4) Finally we walk all the qgroups that are referenced by this range
1819 * including the root we are acting on currently. We will adjust the counters
1820 * based on the number of roots we had and will have after this operation.
1821 *
1822 * Take this example as an illustration
1823 *
1824 * [qgroup 1/0]
1825 * / | \
1826 * [qg 0/0] [qg 0/1] [qg 0/2]
1827 * \ | /
1828 * [ extent ]
1829 *
1830 * Say we are adding a reference that is covered by qg 0/0. The first step
1831 * would give a refcnt of 1 to qg 0/1 and 0/2 and a refcnt of 2 to qg 1/0 with
1832 * old_roots being 2. Because it is adding new_roots will be 1. We then go
1833 * through qg 0/0 which will get the new_refcnt set to 1 and add 1 to qg 1/0's
1834 * new_refcnt, bringing it to 3. We then walk through all of the qgroups, we
1835 * notice that the old refcnt for qg 0/0 < the new refcnt, so we added a
1836 * reference and thus must add the size to the referenced bytes. Everything
1837 * else is the same so nothing else changes.
1838 */
1839static int qgroup_shared_accounting(struct btrfs_trans_handle *trans,
1840 struct btrfs_fs_info *fs_info,
1841 struct btrfs_qgroup_operation *oper)
1842{
1843 struct ulist *roots = NULL;
1844 struct ulist *qgroups, *tmp;
1845 struct btrfs_qgroup *qgroup;
3284da7b 1846 struct seq_list elem = SEQ_LIST_INIT(elem);
fcebe456
JB
1847 u64 seq;
1848 int old_roots = 0;
1849 int new_roots = 0;
1850 int ret = 0;
bed92eae 1851
fcebe456
JB
1852 if (oper->elem.seq) {
1853 ret = check_existing_refs(trans, fs_info, oper);
1854 if (ret < 0)
1855 return ret;
1856 if (ret)
2f232036 1857 return 0;
2f232036 1858 }
2f232036 1859
fcebe456
JB
1860 qgroups = ulist_alloc(GFP_NOFS);
1861 if (!qgroups)
1862 return -ENOMEM;
2f232036 1863
fcebe456 1864 tmp = ulist_alloc(GFP_NOFS);
d7372780
ES
1865 if (!tmp) {
1866 ulist_free(qgroups);
fcebe456 1867 return -ENOMEM;
d7372780 1868 }
bed92eae 1869
fcebe456
JB
1870 btrfs_get_tree_mod_seq(fs_info, &elem);
1871 ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr, elem.seq,
1872 &roots);
1873 btrfs_put_tree_mod_seq(fs_info, &elem);
1874 if (ret < 0) {
1875 ulist_free(qgroups);
1876 ulist_free(tmp);
1877 return ret;
1878 }
1879 spin_lock(&fs_info->qgroup_lock);
1880 qgroup = find_qgroup_rb(fs_info, oper->ref_root);
bed92eae 1881 if (!qgroup)
fcebe456
JB
1882 goto out;
1883 seq = fs_info->qgroup_seq;
bed92eae
AJ
1884
1885 /*
fcebe456
JB
1886 * So roots is the list of all the roots currently pointing at the
1887 * bytenr, including the ref we are adding if we are adding, or not if
1888 * we are removing a ref. So we pass in the ref_root to skip that root
1889 * in our calculations. We set old_refnct and new_refcnt cause who the
1890 * hell knows what everything looked like before, and it doesn't matter
1891 * except...
bed92eae 1892 */
fcebe456
JB
1893 ret = qgroup_calc_old_refcnt(fs_info, oper->ref_root, tmp, roots, qgroups,
1894 seq, &old_roots, 0);
1895 if (ret < 0)
1896 goto out;
bed92eae 1897
fcebe456
JB
1898 /*
1899 * Now adjust the refcounts of the qgroups that care about this
1900 * reference, either the old_count in the case of removal or new_count
1901 * in the case of an addition.
1902 */
1903 ret = qgroup_calc_new_refcnt(fs_info, oper, qgroup, tmp, qgroups,
1904 seq);
1905 if (ret < 0)
1906 goto out;
bed92eae
AJ
1907
1908 /*
fcebe456
JB
1909 * ...in the case of removals. If we had a removal before we got around
1910 * to processing this operation then we need to find that guy and count
1911 * his references as if they really existed so we don't end up screwing
1912 * up the exclusive counts. Then whenever we go to process the delete
1913 * everything will be grand and we can account for whatever exclusive
1914 * changes need to be made there. We also have to pass in old_roots so
1915 * we have an accurate count of the roots as it pertains to this
1916 * operations view of the world.
bed92eae 1917 */
fcebe456
JB
1918 ret = qgroup_account_deleted_refs(fs_info, oper, tmp, qgroups, seq,
1919 &old_roots);
1920 if (ret < 0)
1921 goto out;
bed92eae
AJ
1922
1923 /*
fcebe456
JB
1924 * We are adding our root, need to adjust up the number of roots,
1925 * otherwise old_roots is the number of roots we want.
bed92eae 1926 */
fcebe456
JB
1927 if (oper->type == BTRFS_QGROUP_OPER_ADD_SHARED) {
1928 new_roots = old_roots + 1;
1929 } else {
1930 new_roots = old_roots;
1931 old_roots++;
1932 }
1933 fs_info->qgroup_seq += old_roots + 1;
bed92eae 1934
fcebe456
JB
1935
1936 /*
1937 * And now the magic happens, bless Arne for having a pretty elegant
1938 * solution for this.
1939 */
1940 qgroup_adjust_counters(fs_info, oper->ref_root, oper->num_bytes,
1941 qgroups, seq, old_roots, new_roots, 0);
1942out:
bed92eae 1943 spin_unlock(&fs_info->qgroup_lock);
fcebe456 1944 ulist_free(qgroups);
bed92eae 1945 ulist_free(roots);
fcebe456
JB
1946 ulist_free(tmp);
1947 return ret;
1948}
1949
1152651a
MF
1950/*
1951 * Process a reference to a shared subtree. This type of operation is
1952 * queued during snapshot removal when we encounter extents which are
1953 * shared between more than one root.
1954 */
1955static int qgroup_subtree_accounting(struct btrfs_trans_handle *trans,
1956 struct btrfs_fs_info *fs_info,
1957 struct btrfs_qgroup_operation *oper)
1958{
1959 struct ulist *roots = NULL;
1960 struct ulist_node *unode;
1961 struct ulist_iterator uiter;
1962 struct btrfs_qgroup_list *glist;
1963 struct ulist *parents;
1964 int ret = 0;
f90e579c 1965 int err;
1152651a
MF
1966 struct btrfs_qgroup *qg;
1967 u64 root_obj = 0;
3284da7b 1968 struct seq_list elem = SEQ_LIST_INIT(elem);
1152651a
MF
1969
1970 parents = ulist_alloc(GFP_NOFS);
1971 if (!parents)
1972 return -ENOMEM;
1973
1974 btrfs_get_tree_mod_seq(fs_info, &elem);
1975 ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr,
1976 elem.seq, &roots);
1977 btrfs_put_tree_mod_seq(fs_info, &elem);
1978 if (ret < 0)
a3c10895 1979 goto out;
1152651a
MF
1980
1981 if (roots->nnodes != 1)
1982 goto out;
1983
1984 ULIST_ITER_INIT(&uiter);
1985 unode = ulist_next(roots, &uiter); /* Only want 1 so no need to loop */
1986 /*
1987 * If we find our ref root then that means all refs
1988 * this extent has to the root have not yet been
1989 * deleted. In that case, we do nothing and let the
1990 * last ref for this bytenr drive our update.
1991 *
1992 * This can happen for example if an extent is
1993 * referenced multiple times in a snapshot (clone,
1994 * etc). If we are in the middle of snapshot removal,
1995 * queued updates for such an extent will find the
1996 * root if we have not yet finished removing the
1997 * snapshot.
1998 */
1999 if (unode->val == oper->ref_root)
2000 goto out;
2001
2002 root_obj = unode->val;
2003 BUG_ON(!root_obj);
2004
2005 spin_lock(&fs_info->qgroup_lock);
2006 qg = find_qgroup_rb(fs_info, root_obj);
2007 if (!qg)
2008 goto out_unlock;
2009
2010 qg->excl += oper->num_bytes;
2011 qg->excl_cmpr += oper->num_bytes;
2012 qgroup_dirty(fs_info, qg);
2013
2014 /*
2015 * Adjust counts for parent groups. First we find all
2016 * parents, then in the 2nd loop we do the adjustment
2017 * while adding parents of the parents to our ulist.
2018 */
2019 list_for_each_entry(glist, &qg->groups, next_group) {
f90e579c 2020 err = ulist_add(parents, glist->group->qgroupid,
1152651a 2021 ptr_to_u64(glist->group), GFP_ATOMIC);
f90e579c
MF
2022 if (err < 0) {
2023 ret = err;
1152651a 2024 goto out_unlock;
f90e579c 2025 }
1152651a
MF
2026 }
2027
2028 ULIST_ITER_INIT(&uiter);
2029 while ((unode = ulist_next(parents, &uiter))) {
2030 qg = u64_to_ptr(unode->aux);
2031 qg->excl += oper->num_bytes;
2032 qg->excl_cmpr += oper->num_bytes;
2033 qgroup_dirty(fs_info, qg);
2034
2035 /* Add any parents of the parents */
2036 list_for_each_entry(glist, &qg->groups, next_group) {
f90e579c 2037 err = ulist_add(parents, glist->group->qgroupid,
1152651a 2038 ptr_to_u64(glist->group), GFP_ATOMIC);
f90e579c
MF
2039 if (err < 0) {
2040 ret = err;
1152651a 2041 goto out_unlock;
f90e579c 2042 }
1152651a
MF
2043 }
2044 }
2045
2046out_unlock:
2047 spin_unlock(&fs_info->qgroup_lock);
2048
2049out:
2050 ulist_free(roots);
2051 ulist_free(parents);
2052 return ret;
2053}
2054
fcebe456
JB
2055/*
2056 * btrfs_qgroup_account_ref is called for every ref that is added to or deleted
2057 * from the fs. First, all roots referencing the extent are searched, and
2058 * then the space is accounted accordingly to the different roots. The
2059 * accounting algorithm works in 3 steps documented inline.
2060 */
2061static int btrfs_qgroup_account(struct btrfs_trans_handle *trans,
2062 struct btrfs_fs_info *fs_info,
2063 struct btrfs_qgroup_operation *oper)
2064{
2065 int ret = 0;
2066
2067 if (!fs_info->quota_enabled)
2068 return 0;
2069
2070 BUG_ON(!fs_info->quota_root);
2071
2072 mutex_lock(&fs_info->qgroup_rescan_lock);
2073 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
2074 if (fs_info->qgroup_rescan_progress.objectid <= oper->bytenr) {
2075 mutex_unlock(&fs_info->qgroup_rescan_lock);
2076 return 0;
2077 }
2078 }
2079 mutex_unlock(&fs_info->qgroup_rescan_lock);
2080
2081 ASSERT(is_fstree(oper->ref_root));
2082
d3982100
MF
2083 trace_btrfs_qgroup_account(oper);
2084
fcebe456
JB
2085 switch (oper->type) {
2086 case BTRFS_QGROUP_OPER_ADD_EXCL:
2087 case BTRFS_QGROUP_OPER_SUB_EXCL:
2088 ret = qgroup_excl_accounting(fs_info, oper);
2089 break;
2090 case BTRFS_QGROUP_OPER_ADD_SHARED:
2091 case BTRFS_QGROUP_OPER_SUB_SHARED:
2092 ret = qgroup_shared_accounting(trans, fs_info, oper);
2093 break;
1152651a
MF
2094 case BTRFS_QGROUP_OPER_SUB_SUBTREE:
2095 ret = qgroup_subtree_accounting(trans, fs_info, oper);
2096 break;
fcebe456
JB
2097 default:
2098 ASSERT(0);
2099 }
2100 return ret;
2101}
bed92eae 2102
fcebe456
JB
2103/*
2104 * Needs to be called everytime we run delayed refs, even if there is an error
2105 * in order to cleanup outstanding operations.
2106 */
2107int btrfs_delayed_qgroup_accounting(struct btrfs_trans_handle *trans,
2108 struct btrfs_fs_info *fs_info)
2109{
2110 struct btrfs_qgroup_operation *oper;
2111 int ret = 0;
2112
2113 while (!list_empty(&trans->qgroup_ref_list)) {
2114 oper = list_first_entry(&trans->qgroup_ref_list,
2115 struct btrfs_qgroup_operation, list);
2116 list_del_init(&oper->list);
2117 if (!ret || !trans->aborted)
2118 ret = btrfs_qgroup_account(trans, fs_info, oper);
2119 spin_lock(&fs_info->qgroup_op_lock);
2120 rb_erase(&oper->n, &fs_info->qgroup_op_tree);
2121 spin_unlock(&fs_info->qgroup_op_lock);
2122 btrfs_put_tree_mod_seq(fs_info, &oper->elem);
2123 kfree(oper);
2124 }
bed92eae
AJ
2125 return ret;
2126}
2127
2128/*
2129 * called from commit_transaction. Writes all changed qgroups to disk.
2130 */
2131int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
2132 struct btrfs_fs_info *fs_info)
2133{
2134 struct btrfs_root *quota_root = fs_info->quota_root;
2135 int ret = 0;
3d7b5a28 2136 int start_rescan_worker = 0;
bed92eae
AJ
2137
2138 if (!quota_root)
2139 goto out;
2140
3d7b5a28
JS
2141 if (!fs_info->quota_enabled && fs_info->pending_quota_state)
2142 start_rescan_worker = 1;
2143
bed92eae
AJ
2144 fs_info->quota_enabled = fs_info->pending_quota_state;
2145
2146 spin_lock(&fs_info->qgroup_lock);
2147 while (!list_empty(&fs_info->dirty_qgroups)) {
2148 struct btrfs_qgroup *qgroup;
2149 qgroup = list_first_entry(&fs_info->dirty_qgroups,
2150 struct btrfs_qgroup, dirty);
2151 list_del_init(&qgroup->dirty);
2152 spin_unlock(&fs_info->qgroup_lock);
2153 ret = update_qgroup_info_item(trans, quota_root, qgroup);
d3001ed3
DY
2154 if (ret)
2155 fs_info->qgroup_flags |=
2156 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2157 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
bed92eae
AJ
2158 if (ret)
2159 fs_info->qgroup_flags |=
2160 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2161 spin_lock(&fs_info->qgroup_lock);
2162 }
2163 if (fs_info->quota_enabled)
2164 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
2165 else
2166 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
2167 spin_unlock(&fs_info->qgroup_lock);
2168
2169 ret = update_qgroup_status_item(trans, fs_info, quota_root);
2170 if (ret)
2171 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2172
3d7b5a28 2173 if (!ret && start_rescan_worker) {
b382a324
JS
2174 ret = qgroup_rescan_init(fs_info, 0, 1);
2175 if (!ret) {
2176 qgroup_rescan_zero_tracking(fs_info);
fc97fab0
QW
2177 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2178 &fs_info->qgroup_rescan_work);
b382a324 2179 }
3d7b5a28
JS
2180 ret = 0;
2181 }
2182
bed92eae
AJ
2183out:
2184
2185 return ret;
2186}
2187
2188/*
2189 * copy the acounting information between qgroups. This is necessary when a
2190 * snapshot or a subvolume is created
2191 */
2192int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
2193 struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
2194 struct btrfs_qgroup_inherit *inherit)
2195{
2196 int ret = 0;
2197 int i;
2198 u64 *i_qgroups;
2199 struct btrfs_root *quota_root = fs_info->quota_root;
2200 struct btrfs_qgroup *srcgroup;
2201 struct btrfs_qgroup *dstgroup;
2202 u32 level_size = 0;
3f5e2d3b 2203 u64 nums;
bed92eae 2204
f2f6ed3d 2205 mutex_lock(&fs_info->qgroup_ioctl_lock);
bed92eae 2206 if (!fs_info->quota_enabled)
f2f6ed3d 2207 goto out;
bed92eae 2208
f2f6ed3d
WS
2209 if (!quota_root) {
2210 ret = -EINVAL;
2211 goto out;
2212 }
bed92eae 2213
3f5e2d3b
WS
2214 if (inherit) {
2215 i_qgroups = (u64 *)(inherit + 1);
2216 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
2217 2 * inherit->num_excl_copies;
2218 for (i = 0; i < nums; ++i) {
2219 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
2220 if (!srcgroup) {
2221 ret = -EINVAL;
2222 goto out;
2223 }
2224 ++i_qgroups;
2225 }
2226 }
2227
bed92eae
AJ
2228 /*
2229 * create a tracking group for the subvol itself
2230 */
2231 ret = add_qgroup_item(trans, quota_root, objectid);
2232 if (ret)
2233 goto out;
2234
bed92eae
AJ
2235 if (srcid) {
2236 struct btrfs_root *srcroot;
2237 struct btrfs_key srckey;
bed92eae
AJ
2238
2239 srckey.objectid = srcid;
2240 srckey.type = BTRFS_ROOT_ITEM_KEY;
2241 srckey.offset = (u64)-1;
2242 srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
2243 if (IS_ERR(srcroot)) {
2244 ret = PTR_ERR(srcroot);
2245 goto out;
2246 }
2247
2248 rcu_read_lock();
707e8a07 2249 level_size = srcroot->nodesize;
bed92eae
AJ
2250 rcu_read_unlock();
2251 }
2252
2253 /*
2254 * add qgroup to all inherited groups
2255 */
2256 if (inherit) {
2257 i_qgroups = (u64 *)(inherit + 1);
2258 for (i = 0; i < inherit->num_qgroups; ++i) {
2259 ret = add_qgroup_relation_item(trans, quota_root,
2260 objectid, *i_qgroups);
2261 if (ret)
2262 goto out;
2263 ret = add_qgroup_relation_item(trans, quota_root,
2264 *i_qgroups, objectid);
2265 if (ret)
2266 goto out;
2267 ++i_qgroups;
2268 }
2269 }
2270
2271
2272 spin_lock(&fs_info->qgroup_lock);
2273
2274 dstgroup = add_qgroup_rb(fs_info, objectid);
57a5a882
DC
2275 if (IS_ERR(dstgroup)) {
2276 ret = PTR_ERR(dstgroup);
bed92eae 2277 goto unlock;
57a5a882 2278 }
bed92eae 2279
e8c8541a 2280 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
e8c8541a
DY
2281 dstgroup->lim_flags = inherit->lim.flags;
2282 dstgroup->max_rfer = inherit->lim.max_rfer;
2283 dstgroup->max_excl = inherit->lim.max_excl;
2284 dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
2285 dstgroup->rsv_excl = inherit->lim.rsv_excl;
1510e71c
DY
2286
2287 ret = update_qgroup_limit_item(trans, quota_root, dstgroup);
2288 if (ret) {
2289 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2290 btrfs_info(fs_info, "unable to update quota limit for %llu",
2291 dstgroup->qgroupid);
2292 goto unlock;
2293 }
e8c8541a
DY
2294 }
2295
bed92eae
AJ
2296 if (srcid) {
2297 srcgroup = find_qgroup_rb(fs_info, srcid);
f3a87f1b 2298 if (!srcgroup)
bed92eae 2299 goto unlock;
fcebe456
JB
2300
2301 /*
2302 * We call inherit after we clone the root in order to make sure
2303 * our counts don't go crazy, so at this point the only
2304 * difference between the two roots should be the root node.
2305 */
2306 dstgroup->rfer = srcgroup->rfer;
2307 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
2308 dstgroup->excl = level_size;
2309 dstgroup->excl_cmpr = level_size;
bed92eae
AJ
2310 srcgroup->excl = level_size;
2311 srcgroup->excl_cmpr = level_size;
3eeb4d59
DY
2312
2313 /* inherit the limit info */
2314 dstgroup->lim_flags = srcgroup->lim_flags;
2315 dstgroup->max_rfer = srcgroup->max_rfer;
2316 dstgroup->max_excl = srcgroup->max_excl;
2317 dstgroup->rsv_rfer = srcgroup->rsv_rfer;
2318 dstgroup->rsv_excl = srcgroup->rsv_excl;
2319
bed92eae
AJ
2320 qgroup_dirty(fs_info, dstgroup);
2321 qgroup_dirty(fs_info, srcgroup);
2322 }
2323
f3a87f1b 2324 if (!inherit)
bed92eae
AJ
2325 goto unlock;
2326
2327 i_qgroups = (u64 *)(inherit + 1);
2328 for (i = 0; i < inherit->num_qgroups; ++i) {
2329 ret = add_relation_rb(quota_root->fs_info, objectid,
2330 *i_qgroups);
2331 if (ret)
2332 goto unlock;
2333 ++i_qgroups;
2334 }
2335
2336 for (i = 0; i < inherit->num_ref_copies; ++i) {
2337 struct btrfs_qgroup *src;
2338 struct btrfs_qgroup *dst;
2339
2340 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2341 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2342
2343 if (!src || !dst) {
2344 ret = -EINVAL;
2345 goto unlock;
2346 }
2347
2348 dst->rfer = src->rfer - level_size;
2349 dst->rfer_cmpr = src->rfer_cmpr - level_size;
2350 i_qgroups += 2;
2351 }
2352 for (i = 0; i < inherit->num_excl_copies; ++i) {
2353 struct btrfs_qgroup *src;
2354 struct btrfs_qgroup *dst;
2355
2356 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2357 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2358
2359 if (!src || !dst) {
2360 ret = -EINVAL;
2361 goto unlock;
2362 }
2363
2364 dst->excl = src->excl + level_size;
2365 dst->excl_cmpr = src->excl_cmpr + level_size;
2366 i_qgroups += 2;
2367 }
2368
2369unlock:
2370 spin_unlock(&fs_info->qgroup_lock);
2371out:
f2f6ed3d 2372 mutex_unlock(&fs_info->qgroup_ioctl_lock);
bed92eae
AJ
2373 return ret;
2374}
2375
2376/*
2377 * reserve some space for a qgroup and all its parents. The reservation takes
2378 * place with start_transaction or dealloc_reserve, similar to ENOSPC
2379 * accounting. If not enough space is available, EDQUOT is returned.
2380 * We assume that the requested space is new for all qgroups.
2381 */
2382int btrfs_qgroup_reserve(struct btrfs_root *root, u64 num_bytes)
2383{
2384 struct btrfs_root *quota_root;
2385 struct btrfs_qgroup *qgroup;
2386 struct btrfs_fs_info *fs_info = root->fs_info;
2387 u64 ref_root = root->root_key.objectid;
2388 int ret = 0;
bed92eae
AJ
2389 struct ulist_node *unode;
2390 struct ulist_iterator uiter;
2391
2392 if (!is_fstree(ref_root))
2393 return 0;
2394
2395 if (num_bytes == 0)
2396 return 0;
2397
2398 spin_lock(&fs_info->qgroup_lock);
2399 quota_root = fs_info->quota_root;
2400 if (!quota_root)
2401 goto out;
2402
2403 qgroup = find_qgroup_rb(fs_info, ref_root);
2404 if (!qgroup)
2405 goto out;
2406
2407 /*
2408 * in a first step, we check all affected qgroups if any limits would
2409 * be exceeded
2410 */
1e8f9158
WS
2411 ulist_reinit(fs_info->qgroup_ulist);
2412 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
3c97185c
WS
2413 (uintptr_t)qgroup, GFP_ATOMIC);
2414 if (ret < 0)
2415 goto out;
bed92eae 2416 ULIST_ITER_INIT(&uiter);
1e8f9158 2417 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
bed92eae
AJ
2418 struct btrfs_qgroup *qg;
2419 struct btrfs_qgroup_list *glist;
2420
fcebe456 2421 qg = u64_to_ptr(unode->aux);
bed92eae
AJ
2422
2423 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
b4fcd6be 2424 qg->reserved + (s64)qg->rfer + num_bytes >
720f1e20 2425 qg->max_rfer) {
bed92eae 2426 ret = -EDQUOT;
720f1e20
WS
2427 goto out;
2428 }
bed92eae
AJ
2429
2430 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
b4fcd6be 2431 qg->reserved + (s64)qg->excl + num_bytes >
720f1e20 2432 qg->max_excl) {
bed92eae 2433 ret = -EDQUOT;
720f1e20
WS
2434 goto out;
2435 }
bed92eae
AJ
2436
2437 list_for_each_entry(glist, &qg->groups, next_group) {
1e8f9158
WS
2438 ret = ulist_add(fs_info->qgroup_ulist,
2439 glist->group->qgroupid,
3c97185c
WS
2440 (uintptr_t)glist->group, GFP_ATOMIC);
2441 if (ret < 0)
2442 goto out;
bed92eae
AJ
2443 }
2444 }
3c97185c 2445 ret = 0;
bed92eae
AJ
2446 /*
2447 * no limits exceeded, now record the reservation into all qgroups
2448 */
2449 ULIST_ITER_INIT(&uiter);
1e8f9158 2450 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
bed92eae
AJ
2451 struct btrfs_qgroup *qg;
2452
fcebe456 2453 qg = u64_to_ptr(unode->aux);
bed92eae
AJ
2454
2455 qg->reserved += num_bytes;
2456 }
2457
2458out:
2459 spin_unlock(&fs_info->qgroup_lock);
bed92eae
AJ
2460 return ret;
2461}
2462
2463void btrfs_qgroup_free(struct btrfs_root *root, u64 num_bytes)
2464{
2465 struct btrfs_root *quota_root;
2466 struct btrfs_qgroup *qgroup;
2467 struct btrfs_fs_info *fs_info = root->fs_info;
bed92eae
AJ
2468 struct ulist_node *unode;
2469 struct ulist_iterator uiter;
2470 u64 ref_root = root->root_key.objectid;
3c97185c 2471 int ret = 0;
bed92eae
AJ
2472
2473 if (!is_fstree(ref_root))
2474 return;
2475
2476 if (num_bytes == 0)
2477 return;
2478
2479 spin_lock(&fs_info->qgroup_lock);
2480
2481 quota_root = fs_info->quota_root;
2482 if (!quota_root)
2483 goto out;
2484
2485 qgroup = find_qgroup_rb(fs_info, ref_root);
2486 if (!qgroup)
2487 goto out;
2488
1e8f9158
WS
2489 ulist_reinit(fs_info->qgroup_ulist);
2490 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
3c97185c
WS
2491 (uintptr_t)qgroup, GFP_ATOMIC);
2492 if (ret < 0)
2493 goto out;
bed92eae 2494 ULIST_ITER_INIT(&uiter);
1e8f9158 2495 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
bed92eae
AJ
2496 struct btrfs_qgroup *qg;
2497 struct btrfs_qgroup_list *glist;
2498
fcebe456 2499 qg = u64_to_ptr(unode->aux);
bed92eae
AJ
2500
2501 qg->reserved -= num_bytes;
2502
2503 list_for_each_entry(glist, &qg->groups, next_group) {
1e8f9158
WS
2504 ret = ulist_add(fs_info->qgroup_ulist,
2505 glist->group->qgroupid,
3c97185c
WS
2506 (uintptr_t)glist->group, GFP_ATOMIC);
2507 if (ret < 0)
2508 goto out;
bed92eae
AJ
2509 }
2510 }
2511
2512out:
2513 spin_unlock(&fs_info->qgroup_lock);
bed92eae
AJ
2514}
2515
2516void assert_qgroups_uptodate(struct btrfs_trans_handle *trans)
2517{
2518 if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq)
2519 return;
efe120a0
FH
2520 btrfs_err(trans->root->fs_info,
2521 "qgroups not uptodate in trans handle %p: list is%s empty, "
2522 "seq is %#x.%x",
bed92eae 2523 trans, list_empty(&trans->qgroup_ref_list) ? "" : " not",
fc36ed7e
JS
2524 (u32)(trans->delayed_ref_elem.seq >> 32),
2525 (u32)trans->delayed_ref_elem.seq);
bed92eae
AJ
2526 BUG();
2527}
2f232036
JS
2528
2529/*
2530 * returns < 0 on error, 0 when more leafs are to be scanned.
2531 * returns 1 when done, 2 when done and FLAG_INCONSISTENT was cleared.
2532 */
2533static int
b382a324 2534qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
fcebe456
JB
2535 struct btrfs_trans_handle *trans, struct ulist *qgroups,
2536 struct ulist *tmp, struct extent_buffer *scratch_leaf)
2f232036
JS
2537{
2538 struct btrfs_key found;
2f232036 2539 struct ulist *roots = NULL;
3284da7b 2540 struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
fcebe456 2541 u64 num_bytes;
2f232036 2542 u64 seq;
fcebe456 2543 int new_roots;
2f232036
JS
2544 int slot;
2545 int ret;
2546
2547 path->leave_spinning = 1;
2548 mutex_lock(&fs_info->qgroup_rescan_lock);
2549 ret = btrfs_search_slot_for_read(fs_info->extent_root,
2550 &fs_info->qgroup_rescan_progress,
2551 path, 1, 0);
2552
2553 pr_debug("current progress key (%llu %u %llu), search_slot ret %d\n",
c1c9ff7c 2554 fs_info->qgroup_rescan_progress.objectid,
2f232036 2555 fs_info->qgroup_rescan_progress.type,
c1c9ff7c 2556 fs_info->qgroup_rescan_progress.offset, ret);
2f232036
JS
2557
2558 if (ret) {
2559 /*
2560 * The rescan is about to end, we will not be scanning any
2561 * further blocks. We cannot unset the RESCAN flag here, because
2562 * we want to commit the transaction if everything went well.
2563 * To make the live accounting work in this phase, we set our
2564 * scan progress pointer such that every real extent objectid
2565 * will be smaller.
2566 */
2567 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
2568 btrfs_release_path(path);
2569 mutex_unlock(&fs_info->qgroup_rescan_lock);
2570 return ret;
2571 }
2572
2573 btrfs_item_key_to_cpu(path->nodes[0], &found,
2574 btrfs_header_nritems(path->nodes[0]) - 1);
2575 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
2576
2577 btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
2578 memcpy(scratch_leaf, path->nodes[0], sizeof(*scratch_leaf));
2579 slot = path->slots[0];
2580 btrfs_release_path(path);
2581 mutex_unlock(&fs_info->qgroup_rescan_lock);
2582
2583 for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
2584 btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
3a6d75e8
JB
2585 if (found.type != BTRFS_EXTENT_ITEM_KEY &&
2586 found.type != BTRFS_METADATA_ITEM_KEY)
2f232036 2587 continue;
3a6d75e8 2588 if (found.type == BTRFS_METADATA_ITEM_KEY)
707e8a07 2589 num_bytes = fs_info->extent_root->nodesize;
3a6d75e8
JB
2590 else
2591 num_bytes = found.offset;
2592
fcebe456
JB
2593 ulist_reinit(qgroups);
2594 ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
2595 &roots);
2f232036
JS
2596 if (ret < 0)
2597 goto out;
2598 spin_lock(&fs_info->qgroup_lock);
2599 seq = fs_info->qgroup_seq;
2600 fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */
2601
fcebe456
JB
2602 new_roots = 0;
2603 ret = qgroup_calc_old_refcnt(fs_info, 0, tmp, roots, qgroups,
2604 seq, &new_roots, 1);
2605 if (ret < 0) {
2f232036
JS
2606 spin_unlock(&fs_info->qgroup_lock);
2607 ulist_free(roots);
2608 goto out;
2609 }
2610
fcebe456
JB
2611 ret = qgroup_adjust_counters(fs_info, 0, num_bytes, qgroups,
2612 seq, 0, new_roots, 1);
2613 if (ret < 0) {
2614 spin_unlock(&fs_info->qgroup_lock);
2615 ulist_free(roots);
2616 goto out;
2f232036 2617 }
2f232036
JS
2618 spin_unlock(&fs_info->qgroup_lock);
2619 ulist_free(roots);
2f232036 2620 }
2f232036
JS
2621out:
2622 btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
2623
2624 return ret;
2625}
2626
d458b054 2627static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
2f232036 2628{
b382a324
JS
2629 struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
2630 qgroup_rescan_work);
2f232036
JS
2631 struct btrfs_path *path;
2632 struct btrfs_trans_handle *trans = NULL;
fcebe456 2633 struct ulist *tmp = NULL, *qgroups = NULL;
2f232036
JS
2634 struct extent_buffer *scratch_leaf = NULL;
2635 int err = -ENOMEM;
2636
2637 path = btrfs_alloc_path();
2638 if (!path)
2639 goto out;
fcebe456
JB
2640 qgroups = ulist_alloc(GFP_NOFS);
2641 if (!qgroups)
2642 goto out;
2f232036
JS
2643 tmp = ulist_alloc(GFP_NOFS);
2644 if (!tmp)
2645 goto out;
2646 scratch_leaf = kmalloc(sizeof(*scratch_leaf), GFP_NOFS);
2647 if (!scratch_leaf)
2648 goto out;
2649
2650 err = 0;
2651 while (!err) {
2652 trans = btrfs_start_transaction(fs_info->fs_root, 0);
2653 if (IS_ERR(trans)) {
2654 err = PTR_ERR(trans);
2655 break;
2656 }
2657 if (!fs_info->quota_enabled) {
2658 err = -EINTR;
2659 } else {
b382a324 2660 err = qgroup_rescan_leaf(fs_info, path, trans,
fcebe456 2661 qgroups, tmp, scratch_leaf);
2f232036
JS
2662 }
2663 if (err > 0)
2664 btrfs_commit_transaction(trans, fs_info->fs_root);
2665 else
2666 btrfs_end_transaction(trans, fs_info->fs_root);
2667 }
2668
2669out:
2670 kfree(scratch_leaf);
fcebe456 2671 ulist_free(qgroups);
2a108409 2672 ulist_free(tmp);
2f232036 2673 btrfs_free_path(path);
2f232036
JS
2674
2675 mutex_lock(&fs_info->qgroup_rescan_lock);
2676 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2677
2678 if (err == 2 &&
2679 fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
2680 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2681 } else if (err < 0) {
2682 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2683 }
2684 mutex_unlock(&fs_info->qgroup_rescan_lock);
2685
2686 if (err >= 0) {
efe120a0 2687 btrfs_info(fs_info, "qgroup scan completed%s",
2f232036
JS
2688 err == 2 ? " (inconsistency flag cleared)" : "");
2689 } else {
efe120a0 2690 btrfs_err(fs_info, "qgroup scan failed with %d", err);
2f232036 2691 }
57254b6e
JS
2692
2693 complete_all(&fs_info->qgroup_rescan_completion);
2f232036
JS
2694}
2695
b382a324
JS
2696/*
2697 * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
2698 * memory required for the rescan context.
2699 */
2700static int
2701qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
2702 int init_flags)
2f232036
JS
2703{
2704 int ret = 0;
2f232036 2705
b382a324
JS
2706 if (!init_flags &&
2707 (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
2708 !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
2709 ret = -EINVAL;
2710 goto err;
2711 }
2f232036
JS
2712
2713 mutex_lock(&fs_info->qgroup_rescan_lock);
2714 spin_lock(&fs_info->qgroup_lock);
b382a324
JS
2715
2716 if (init_flags) {
2717 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
2718 ret = -EINPROGRESS;
2719 else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
2720 ret = -EINVAL;
2721
2722 if (ret) {
2723 spin_unlock(&fs_info->qgroup_lock);
2724 mutex_unlock(&fs_info->qgroup_rescan_lock);
2725 goto err;
2726 }
2727
2728 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2f232036
JS
2729 }
2730
2f232036
JS
2731 memset(&fs_info->qgroup_rescan_progress, 0,
2732 sizeof(fs_info->qgroup_rescan_progress));
b382a324
JS
2733 fs_info->qgroup_rescan_progress.objectid = progress_objectid;
2734
2735 spin_unlock(&fs_info->qgroup_lock);
2736 mutex_unlock(&fs_info->qgroup_rescan_lock);
2737
57254b6e 2738 init_completion(&fs_info->qgroup_rescan_completion);
2f232036 2739
b382a324
JS
2740 memset(&fs_info->qgroup_rescan_work, 0,
2741 sizeof(fs_info->qgroup_rescan_work));
fc97fab0 2742 btrfs_init_work(&fs_info->qgroup_rescan_work,
9e0af237 2743 btrfs_qgroup_rescan_helper,
fc97fab0 2744 btrfs_qgroup_rescan_worker, NULL, NULL);
b382a324
JS
2745
2746 if (ret) {
2747err:
efe120a0 2748 btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret);
b382a324
JS
2749 return ret;
2750 }
2751
2752 return 0;
2753}
2754
2755static void
2756qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
2757{
2758 struct rb_node *n;
2759 struct btrfs_qgroup *qgroup;
2760
2761 spin_lock(&fs_info->qgroup_lock);
2f232036
JS
2762 /* clear all current qgroup tracking information */
2763 for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
2764 qgroup = rb_entry(n, struct btrfs_qgroup, node);
2765 qgroup->rfer = 0;
2766 qgroup->rfer_cmpr = 0;
2767 qgroup->excl = 0;
2768 qgroup->excl_cmpr = 0;
2769 }
2770 spin_unlock(&fs_info->qgroup_lock);
b382a324 2771}
2f232036 2772
b382a324
JS
2773int
2774btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
2775{
2776 int ret = 0;
2777 struct btrfs_trans_handle *trans;
2778
2779 ret = qgroup_rescan_init(fs_info, 0, 1);
2780 if (ret)
2781 return ret;
2782
2783 /*
2784 * We have set the rescan_progress to 0, which means no more
2785 * delayed refs will be accounted by btrfs_qgroup_account_ref.
2786 * However, btrfs_qgroup_account_ref may be right after its call
2787 * to btrfs_find_all_roots, in which case it would still do the
2788 * accounting.
2789 * To solve this, we're committing the transaction, which will
2790 * ensure we run all delayed refs and only after that, we are
2791 * going to clear all tracking information for a clean start.
2792 */
2793
2794 trans = btrfs_join_transaction(fs_info->fs_root);
2795 if (IS_ERR(trans)) {
2796 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2797 return PTR_ERR(trans);
2798 }
2799 ret = btrfs_commit_transaction(trans, fs_info->fs_root);
2800 if (ret) {
2801 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2802 return ret;
2803 }
2804
2805 qgroup_rescan_zero_tracking(fs_info);
2806
fc97fab0
QW
2807 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2808 &fs_info->qgroup_rescan_work);
2f232036
JS
2809
2810 return 0;
2811}
57254b6e
JS
2812
2813int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info)
2814{
2815 int running;
2816 int ret = 0;
2817
2818 mutex_lock(&fs_info->qgroup_rescan_lock);
2819 spin_lock(&fs_info->qgroup_lock);
2820 running = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2821 spin_unlock(&fs_info->qgroup_lock);
2822 mutex_unlock(&fs_info->qgroup_rescan_lock);
2823
2824 if (running)
2825 ret = wait_for_completion_interruptible(
2826 &fs_info->qgroup_rescan_completion);
2827
2828 return ret;
2829}
b382a324
JS
2830
2831/*
2832 * this is only called from open_ctree where we're still single threaded, thus
2833 * locking is omitted here.
2834 */
2835void
2836btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
2837{
2838 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
fc97fab0
QW
2839 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2840 &fs_info->qgroup_rescan_work);
b382a324 2841}