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