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