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