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