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