]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - fs/btrfs/qgroup.c
69fa487e29ce4368d779c90faefcca8fd3cef07b
[mirror_ubuntu-focal-kernel.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
35 /* TODO XXX FIXME
36 * - subvol delete -> delete when ref goes to 0? delete limits also?
37 * - reorganize keys
38 * - compressed
39 * - sync
40 * - rescan
41 * - copy also limits on subvol creation
42 * - limit
43 * - caches fuer ulists
44 * - performance benchmarks
45 * - check all ioctl parameters
46 */
47
48 /*
49 * one struct for each qgroup, organized in fs_info->qgroup_tree.
50 */
51 struct btrfs_qgroup {
52 u64 qgroupid;
53
54 /*
55 * state
56 */
57 u64 rfer; /* referenced */
58 u64 rfer_cmpr; /* referenced compressed */
59 u64 excl; /* exclusive */
60 u64 excl_cmpr; /* exclusive compressed */
61
62 /*
63 * limits
64 */
65 u64 lim_flags; /* which limits are set */
66 u64 max_rfer;
67 u64 max_excl;
68 u64 rsv_rfer;
69 u64 rsv_excl;
70
71 /*
72 * reservation tracking
73 */
74 u64 reserved;
75
76 /*
77 * lists
78 */
79 struct list_head groups; /* groups this group is member of */
80 struct list_head members; /* groups that are members of this group */
81 struct list_head dirty; /* dirty groups */
82 struct rb_node node; /* tree of qgroups */
83
84 /*
85 * temp variables for accounting operations
86 */
87 u64 tag;
88 u64 refcnt;
89 };
90
91 /*
92 * glue structure to represent the relations between qgroups.
93 */
94 struct btrfs_qgroup_list {
95 struct list_head next_group;
96 struct list_head next_member;
97 struct btrfs_qgroup *group;
98 struct btrfs_qgroup *member;
99 };
100
101 /* must be called with qgroup_lock held */
102 static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
103 u64 qgroupid)
104 {
105 struct rb_node *n = fs_info->qgroup_tree.rb_node;
106 struct btrfs_qgroup *qgroup;
107
108 while (n) {
109 qgroup = rb_entry(n, struct btrfs_qgroup, node);
110 if (qgroup->qgroupid < qgroupid)
111 n = n->rb_left;
112 else if (qgroup->qgroupid > qgroupid)
113 n = n->rb_right;
114 else
115 return qgroup;
116 }
117 return NULL;
118 }
119
120 /* must be called with qgroup_lock held */
121 static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
122 u64 qgroupid)
123 {
124 struct rb_node **p = &fs_info->qgroup_tree.rb_node;
125 struct rb_node *parent = NULL;
126 struct btrfs_qgroup *qgroup;
127
128 while (*p) {
129 parent = *p;
130 qgroup = rb_entry(parent, struct btrfs_qgroup, node);
131
132 if (qgroup->qgroupid < qgroupid)
133 p = &(*p)->rb_left;
134 else if (qgroup->qgroupid > qgroupid)
135 p = &(*p)->rb_right;
136 else
137 return qgroup;
138 }
139
140 qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
141 if (!qgroup)
142 return ERR_PTR(-ENOMEM);
143
144 qgroup->qgroupid = qgroupid;
145 INIT_LIST_HEAD(&qgroup->groups);
146 INIT_LIST_HEAD(&qgroup->members);
147 INIT_LIST_HEAD(&qgroup->dirty);
148
149 rb_link_node(&qgroup->node, parent, p);
150 rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
151
152 return qgroup;
153 }
154
155 /* must be called with qgroup_lock held */
156 static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
157 {
158 struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
159 struct btrfs_qgroup_list *list;
160
161 if (!qgroup)
162 return -ENOENT;
163
164 rb_erase(&qgroup->node, &fs_info->qgroup_tree);
165 list_del(&qgroup->dirty);
166
167 while (!list_empty(&qgroup->groups)) {
168 list = list_first_entry(&qgroup->groups,
169 struct btrfs_qgroup_list, next_group);
170 list_del(&list->next_group);
171 list_del(&list->next_member);
172 kfree(list);
173 }
174
175 while (!list_empty(&qgroup->members)) {
176 list = list_first_entry(&qgroup->members,
177 struct btrfs_qgroup_list, next_member);
178 list_del(&list->next_group);
179 list_del(&list->next_member);
180 kfree(list);
181 }
182 kfree(qgroup);
183
184 return 0;
185 }
186
187 /* must be called with qgroup_lock held */
188 static int add_relation_rb(struct btrfs_fs_info *fs_info,
189 u64 memberid, u64 parentid)
190 {
191 struct btrfs_qgroup *member;
192 struct btrfs_qgroup *parent;
193 struct btrfs_qgroup_list *list;
194
195 member = find_qgroup_rb(fs_info, memberid);
196 parent = find_qgroup_rb(fs_info, parentid);
197 if (!member || !parent)
198 return -ENOENT;
199
200 list = kzalloc(sizeof(*list), GFP_ATOMIC);
201 if (!list)
202 return -ENOMEM;
203
204 list->group = parent;
205 list->member = member;
206 list_add_tail(&list->next_group, &member->groups);
207 list_add_tail(&list->next_member, &parent->members);
208
209 return 0;
210 }
211
212 /* must be called with qgroup_lock held */
213 static int del_relation_rb(struct btrfs_fs_info *fs_info,
214 u64 memberid, u64 parentid)
215 {
216 struct btrfs_qgroup *member;
217 struct btrfs_qgroup *parent;
218 struct btrfs_qgroup_list *list;
219
220 member = find_qgroup_rb(fs_info, memberid);
221 parent = find_qgroup_rb(fs_info, parentid);
222 if (!member || !parent)
223 return -ENOENT;
224
225 list_for_each_entry(list, &member->groups, next_group) {
226 if (list->group == parent) {
227 list_del(&list->next_group);
228 list_del(&list->next_member);
229 kfree(list);
230 return 0;
231 }
232 }
233 return -ENOENT;
234 }
235
236 /*
237 * The full config is read in one go, only called from open_ctree()
238 * It doesn't use any locking, as at this point we're still single-threaded
239 */
240 int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
241 {
242 struct btrfs_key key;
243 struct btrfs_key found_key;
244 struct btrfs_root *quota_root = fs_info->quota_root;
245 struct btrfs_path *path = NULL;
246 struct extent_buffer *l;
247 int slot;
248 int ret = 0;
249 u64 flags = 0;
250
251 if (!fs_info->quota_enabled)
252 return 0;
253
254 path = btrfs_alloc_path();
255 if (!path) {
256 ret = -ENOMEM;
257 goto out;
258 }
259
260 /* default this to quota off, in case no status key is found */
261 fs_info->qgroup_flags = 0;
262
263 /*
264 * pass 1: read status, all qgroup infos and limits
265 */
266 key.objectid = 0;
267 key.type = 0;
268 key.offset = 0;
269 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
270 if (ret)
271 goto out;
272
273 while (1) {
274 struct btrfs_qgroup *qgroup;
275
276 slot = path->slots[0];
277 l = path->nodes[0];
278 btrfs_item_key_to_cpu(l, &found_key, slot);
279
280 if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
281 struct btrfs_qgroup_status_item *ptr;
282
283 ptr = btrfs_item_ptr(l, slot,
284 struct btrfs_qgroup_status_item);
285
286 if (btrfs_qgroup_status_version(l, ptr) !=
287 BTRFS_QGROUP_STATUS_VERSION) {
288 printk(KERN_ERR
289 "btrfs: old qgroup version, quota disabled\n");
290 goto out;
291 }
292 if (btrfs_qgroup_status_generation(l, ptr) !=
293 fs_info->generation) {
294 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
295 printk(KERN_ERR
296 "btrfs: qgroup generation mismatch, "
297 "marked as inconsistent\n");
298 }
299 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
300 ptr);
301 /* FIXME read scan element */
302 goto next1;
303 }
304
305 if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
306 found_key.type != BTRFS_QGROUP_LIMIT_KEY)
307 goto next1;
308
309 qgroup = find_qgroup_rb(fs_info, found_key.offset);
310 if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
311 (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
312 printk(KERN_ERR "btrfs: inconsitent qgroup config\n");
313 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
314 }
315 if (!qgroup) {
316 qgroup = add_qgroup_rb(fs_info, found_key.offset);
317 if (IS_ERR(qgroup)) {
318 ret = PTR_ERR(qgroup);
319 goto out;
320 }
321 }
322 switch (found_key.type) {
323 case BTRFS_QGROUP_INFO_KEY: {
324 struct btrfs_qgroup_info_item *ptr;
325
326 ptr = btrfs_item_ptr(l, slot,
327 struct btrfs_qgroup_info_item);
328 qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
329 qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
330 qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
331 qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
332 /* generation currently unused */
333 break;
334 }
335 case BTRFS_QGROUP_LIMIT_KEY: {
336 struct btrfs_qgroup_limit_item *ptr;
337
338 ptr = btrfs_item_ptr(l, slot,
339 struct btrfs_qgroup_limit_item);
340 qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
341 qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
342 qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
343 qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
344 qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
345 break;
346 }
347 }
348 next1:
349 ret = btrfs_next_item(quota_root, path);
350 if (ret < 0)
351 goto out;
352 if (ret)
353 break;
354 }
355 btrfs_release_path(path);
356
357 /*
358 * pass 2: read all qgroup relations
359 */
360 key.objectid = 0;
361 key.type = BTRFS_QGROUP_RELATION_KEY;
362 key.offset = 0;
363 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
364 if (ret)
365 goto out;
366 while (1) {
367 slot = path->slots[0];
368 l = path->nodes[0];
369 btrfs_item_key_to_cpu(l, &found_key, slot);
370
371 if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
372 goto next2;
373
374 if (found_key.objectid > found_key.offset) {
375 /* parent <- member, not needed to build config */
376 /* FIXME should we omit the key completely? */
377 goto next2;
378 }
379
380 ret = add_relation_rb(fs_info, found_key.objectid,
381 found_key.offset);
382 if (ret == -ENOENT) {
383 printk(KERN_WARNING
384 "btrfs: orphan qgroup relation 0x%llx->0x%llx\n",
385 (unsigned long long)found_key.objectid,
386 (unsigned long long)found_key.offset);
387 ret = 0; /* ignore the error */
388 }
389 if (ret)
390 goto out;
391 next2:
392 ret = btrfs_next_item(quota_root, path);
393 if (ret < 0)
394 goto out;
395 if (ret)
396 break;
397 }
398 out:
399 fs_info->qgroup_flags |= flags;
400 if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)) {
401 fs_info->quota_enabled = 0;
402 fs_info->pending_quota_state = 0;
403 }
404 btrfs_free_path(path);
405
406 return ret < 0 ? ret : 0;
407 }
408
409 /*
410 * This is only called from close_ctree() or open_ctree(), both in single-
411 * treaded paths. Clean up the in-memory structures. No locking needed.
412 */
413 void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
414 {
415 struct rb_node *n;
416 struct btrfs_qgroup *qgroup;
417 struct btrfs_qgroup_list *list;
418
419 while ((n = rb_first(&fs_info->qgroup_tree))) {
420 qgroup = rb_entry(n, struct btrfs_qgroup, node);
421 rb_erase(n, &fs_info->qgroup_tree);
422
423 WARN_ON(!list_empty(&qgroup->dirty));
424
425 while (!list_empty(&qgroup->groups)) {
426 list = list_first_entry(&qgroup->groups,
427 struct btrfs_qgroup_list,
428 next_group);
429 list_del(&list->next_group);
430 list_del(&list->next_member);
431 kfree(list);
432 }
433
434 while (!list_empty(&qgroup->members)) {
435 list = list_first_entry(&qgroup->members,
436 struct btrfs_qgroup_list,
437 next_member);
438 list_del(&list->next_group);
439 list_del(&list->next_member);
440 kfree(list);
441 }
442 kfree(qgroup);
443 }
444 }
445
446 static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
447 struct btrfs_root *quota_root,
448 u64 src, u64 dst)
449 {
450 int ret;
451 struct btrfs_path *path;
452 struct btrfs_key key;
453
454 path = btrfs_alloc_path();
455 if (!path)
456 return -ENOMEM;
457
458 key.objectid = src;
459 key.type = BTRFS_QGROUP_RELATION_KEY;
460 key.offset = dst;
461
462 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
463
464 btrfs_mark_buffer_dirty(path->nodes[0]);
465
466 btrfs_free_path(path);
467 return ret;
468 }
469
470 static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
471 struct btrfs_root *quota_root,
472 u64 src, u64 dst)
473 {
474 int ret;
475 struct btrfs_path *path;
476 struct btrfs_key key;
477
478 path = btrfs_alloc_path();
479 if (!path)
480 return -ENOMEM;
481
482 key.objectid = src;
483 key.type = BTRFS_QGROUP_RELATION_KEY;
484 key.offset = dst;
485
486 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
487 if (ret < 0)
488 goto out;
489
490 if (ret > 0) {
491 ret = -ENOENT;
492 goto out;
493 }
494
495 ret = btrfs_del_item(trans, quota_root, path);
496 out:
497 btrfs_free_path(path);
498 return ret;
499 }
500
501 static int add_qgroup_item(struct btrfs_trans_handle *trans,
502 struct btrfs_root *quota_root, u64 qgroupid)
503 {
504 int ret;
505 struct btrfs_path *path;
506 struct btrfs_qgroup_info_item *qgroup_info;
507 struct btrfs_qgroup_limit_item *qgroup_limit;
508 struct extent_buffer *leaf;
509 struct btrfs_key key;
510
511 path = btrfs_alloc_path();
512 if (!path)
513 return -ENOMEM;
514
515 key.objectid = 0;
516 key.type = BTRFS_QGROUP_INFO_KEY;
517 key.offset = qgroupid;
518
519 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
520 sizeof(*qgroup_info));
521 if (ret)
522 goto out;
523
524 leaf = path->nodes[0];
525 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
526 struct btrfs_qgroup_info_item);
527 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
528 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
529 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
530 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
531 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
532
533 btrfs_mark_buffer_dirty(leaf);
534
535 btrfs_release_path(path);
536
537 key.type = BTRFS_QGROUP_LIMIT_KEY;
538 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
539 sizeof(*qgroup_limit));
540 if (ret)
541 goto out;
542
543 leaf = path->nodes[0];
544 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
545 struct btrfs_qgroup_limit_item);
546 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
547 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
548 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
549 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
550 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
551
552 btrfs_mark_buffer_dirty(leaf);
553
554 ret = 0;
555 out:
556 btrfs_free_path(path);
557 return ret;
558 }
559
560 static int del_qgroup_item(struct btrfs_trans_handle *trans,
561 struct btrfs_root *quota_root, u64 qgroupid)
562 {
563 int ret;
564 struct btrfs_path *path;
565 struct btrfs_key key;
566
567 path = btrfs_alloc_path();
568 if (!path)
569 return -ENOMEM;
570
571 key.objectid = 0;
572 key.type = BTRFS_QGROUP_INFO_KEY;
573 key.offset = qgroupid;
574 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
575 if (ret < 0)
576 goto out;
577
578 if (ret > 0) {
579 ret = -ENOENT;
580 goto out;
581 }
582
583 ret = btrfs_del_item(trans, quota_root, path);
584 if (ret)
585 goto out;
586
587 btrfs_release_path(path);
588
589 key.type = BTRFS_QGROUP_LIMIT_KEY;
590 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
591 if (ret < 0)
592 goto out;
593
594 if (ret > 0) {
595 ret = -ENOENT;
596 goto out;
597 }
598
599 ret = btrfs_del_item(trans, quota_root, path);
600
601 out:
602 btrfs_free_path(path);
603 return ret;
604 }
605
606 static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
607 struct btrfs_root *root, u64 qgroupid,
608 u64 flags, u64 max_rfer, u64 max_excl,
609 u64 rsv_rfer, u64 rsv_excl)
610 {
611 struct btrfs_path *path;
612 struct btrfs_key key;
613 struct extent_buffer *l;
614 struct btrfs_qgroup_limit_item *qgroup_limit;
615 int ret;
616 int slot;
617
618 key.objectid = 0;
619 key.type = BTRFS_QGROUP_LIMIT_KEY;
620 key.offset = qgroupid;
621
622 path = btrfs_alloc_path();
623 if (!path)
624 return -ENOMEM;
625
626 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
627 if (ret > 0)
628 ret = -ENOENT;
629
630 if (ret)
631 goto out;
632
633 l = path->nodes[0];
634 slot = path->slots[0];
635 qgroup_limit = btrfs_item_ptr(l, path->slots[0],
636 struct btrfs_qgroup_limit_item);
637 btrfs_set_qgroup_limit_flags(l, qgroup_limit, flags);
638 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, max_rfer);
639 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, max_excl);
640 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, rsv_rfer);
641 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, rsv_excl);
642
643 btrfs_mark_buffer_dirty(l);
644
645 out:
646 btrfs_free_path(path);
647 return ret;
648 }
649
650 static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
651 struct btrfs_root *root,
652 struct btrfs_qgroup *qgroup)
653 {
654 struct btrfs_path *path;
655 struct btrfs_key key;
656 struct extent_buffer *l;
657 struct btrfs_qgroup_info_item *qgroup_info;
658 int ret;
659 int slot;
660
661 key.objectid = 0;
662 key.type = BTRFS_QGROUP_INFO_KEY;
663 key.offset = qgroup->qgroupid;
664
665 path = btrfs_alloc_path();
666 if (!path)
667 return -ENOMEM;
668
669 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
670 if (ret > 0)
671 ret = -ENOENT;
672
673 if (ret)
674 goto out;
675
676 l = path->nodes[0];
677 slot = path->slots[0];
678 qgroup_info = btrfs_item_ptr(l, path->slots[0],
679 struct btrfs_qgroup_info_item);
680 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
681 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
682 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
683 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
684 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
685
686 btrfs_mark_buffer_dirty(l);
687
688 out:
689 btrfs_free_path(path);
690 return ret;
691 }
692
693 static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
694 struct btrfs_fs_info *fs_info,
695 struct btrfs_root *root)
696 {
697 struct btrfs_path *path;
698 struct btrfs_key key;
699 struct extent_buffer *l;
700 struct btrfs_qgroup_status_item *ptr;
701 int ret;
702 int slot;
703
704 key.objectid = 0;
705 key.type = BTRFS_QGROUP_STATUS_KEY;
706 key.offset = 0;
707
708 path = btrfs_alloc_path();
709 if (!path)
710 return -ENOMEM;
711
712 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
713 if (ret > 0)
714 ret = -ENOENT;
715
716 if (ret)
717 goto out;
718
719 l = path->nodes[0];
720 slot = path->slots[0];
721 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
722 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
723 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
724 /* XXX scan */
725
726 btrfs_mark_buffer_dirty(l);
727
728 out:
729 btrfs_free_path(path);
730 return ret;
731 }
732
733 /*
734 * called with qgroup_lock held
735 */
736 static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
737 struct btrfs_root *root)
738 {
739 struct btrfs_path *path;
740 struct btrfs_key key;
741 struct extent_buffer *leaf = NULL;
742 int ret;
743 int nr = 0;
744
745 if (!root)
746 return -EINVAL;
747
748 path = btrfs_alloc_path();
749 if (!path)
750 return -ENOMEM;
751
752 path->leave_spinning = 1;
753
754 key.objectid = 0;
755 key.offset = 0;
756 key.type = 0;
757
758 while (1) {
759 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
760 if (ret < 0)
761 goto out;
762 leaf = path->nodes[0];
763 nr = btrfs_header_nritems(leaf);
764 if (!nr)
765 break;
766 /*
767 * delete the leaf one by one
768 * since the whole tree is going
769 * to be deleted.
770 */
771 path->slots[0] = 0;
772 ret = btrfs_del_items(trans, root, path, 0, nr);
773 if (ret)
774 goto out;
775
776 btrfs_release_path(path);
777 }
778 ret = 0;
779 out:
780 root->fs_info->pending_quota_state = 0;
781 btrfs_free_path(path);
782 return ret;
783 }
784
785 int btrfs_quota_enable(struct btrfs_trans_handle *trans,
786 struct btrfs_fs_info *fs_info)
787 {
788 struct btrfs_root *quota_root;
789 struct btrfs_path *path = NULL;
790 struct btrfs_qgroup_status_item *ptr;
791 struct extent_buffer *leaf;
792 struct btrfs_key key;
793 int ret = 0;
794
795 spin_lock(&fs_info->qgroup_lock);
796 if (fs_info->quota_root) {
797 fs_info->pending_quota_state = 1;
798 spin_unlock(&fs_info->qgroup_lock);
799 goto out;
800 }
801 spin_unlock(&fs_info->qgroup_lock);
802
803 /*
804 * initially create the quota tree
805 */
806 quota_root = btrfs_create_tree(trans, fs_info,
807 BTRFS_QUOTA_TREE_OBJECTID);
808 if (IS_ERR(quota_root)) {
809 ret = PTR_ERR(quota_root);
810 goto out;
811 }
812
813 path = btrfs_alloc_path();
814 if (!path) {
815 ret = -ENOMEM;
816 goto out_free_root;
817 }
818
819 key.objectid = 0;
820 key.type = BTRFS_QGROUP_STATUS_KEY;
821 key.offset = 0;
822
823 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
824 sizeof(*ptr));
825 if (ret)
826 goto out_free_path;
827
828 leaf = path->nodes[0];
829 ptr = btrfs_item_ptr(leaf, path->slots[0],
830 struct btrfs_qgroup_status_item);
831 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
832 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
833 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
834 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
835 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
836 btrfs_set_qgroup_status_scan(leaf, ptr, 0);
837
838 btrfs_mark_buffer_dirty(leaf);
839
840 spin_lock(&fs_info->qgroup_lock);
841 fs_info->quota_root = quota_root;
842 fs_info->pending_quota_state = 1;
843 spin_unlock(&fs_info->qgroup_lock);
844 out_free_path:
845 btrfs_free_path(path);
846 out_free_root:
847 if (ret) {
848 free_extent_buffer(quota_root->node);
849 free_extent_buffer(quota_root->commit_root);
850 kfree(quota_root);
851 }
852 out:
853 return ret;
854 }
855
856 int btrfs_quota_disable(struct btrfs_trans_handle *trans,
857 struct btrfs_fs_info *fs_info)
858 {
859 struct btrfs_root *tree_root = fs_info->tree_root;
860 struct btrfs_root *quota_root;
861 int ret = 0;
862
863 spin_lock(&fs_info->qgroup_lock);
864 if (!fs_info->quota_root) {
865 spin_unlock(&fs_info->qgroup_lock);
866 return 0;
867 }
868 fs_info->quota_enabled = 0;
869 fs_info->pending_quota_state = 0;
870 quota_root = fs_info->quota_root;
871 fs_info->quota_root = NULL;
872 btrfs_free_qgroup_config(fs_info);
873 spin_unlock(&fs_info->qgroup_lock);
874
875 if (!quota_root)
876 return -EINVAL;
877
878 ret = btrfs_clean_quota_tree(trans, quota_root);
879 if (ret)
880 goto out;
881
882 ret = btrfs_del_root(trans, tree_root, &quota_root->root_key);
883 if (ret)
884 goto out;
885
886 list_del(&quota_root->dirty_list);
887
888 btrfs_tree_lock(quota_root->node);
889 clean_tree_block(trans, tree_root, quota_root->node);
890 btrfs_tree_unlock(quota_root->node);
891 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
892
893 free_extent_buffer(quota_root->node);
894 free_extent_buffer(quota_root->commit_root);
895 kfree(quota_root);
896 out:
897 return ret;
898 }
899
900 int btrfs_quota_rescan(struct btrfs_fs_info *fs_info)
901 {
902 /* FIXME */
903 return 0;
904 }
905
906 int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
907 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
908 {
909 struct btrfs_root *quota_root;
910 int ret = 0;
911
912 quota_root = fs_info->quota_root;
913 if (!quota_root)
914 return -EINVAL;
915
916 ret = add_qgroup_relation_item(trans, quota_root, src, dst);
917 if (ret)
918 return ret;
919
920 ret = add_qgroup_relation_item(trans, quota_root, dst, src);
921 if (ret) {
922 del_qgroup_relation_item(trans, quota_root, src, dst);
923 return ret;
924 }
925
926 spin_lock(&fs_info->qgroup_lock);
927 ret = add_relation_rb(quota_root->fs_info, src, dst);
928 spin_unlock(&fs_info->qgroup_lock);
929
930 return ret;
931 }
932
933 int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
934 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
935 {
936 struct btrfs_root *quota_root;
937 int ret = 0;
938 int err;
939
940 quota_root = fs_info->quota_root;
941 if (!quota_root)
942 return -EINVAL;
943
944 ret = del_qgroup_relation_item(trans, quota_root, src, dst);
945 err = del_qgroup_relation_item(trans, quota_root, dst, src);
946 if (err && !ret)
947 ret = err;
948
949 spin_lock(&fs_info->qgroup_lock);
950 del_relation_rb(fs_info, src, dst);
951
952 spin_unlock(&fs_info->qgroup_lock);
953
954 return ret;
955 }
956
957 int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
958 struct btrfs_fs_info *fs_info, u64 qgroupid, char *name)
959 {
960 struct btrfs_root *quota_root;
961 struct btrfs_qgroup *qgroup;
962 int ret = 0;
963
964 quota_root = fs_info->quota_root;
965 if (!quota_root)
966 return -EINVAL;
967
968 ret = add_qgroup_item(trans, quota_root, qgroupid);
969
970 spin_lock(&fs_info->qgroup_lock);
971 qgroup = add_qgroup_rb(fs_info, qgroupid);
972 spin_unlock(&fs_info->qgroup_lock);
973
974 if (IS_ERR(qgroup))
975 ret = PTR_ERR(qgroup);
976
977 return ret;
978 }
979
980 int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
981 struct btrfs_fs_info *fs_info, u64 qgroupid)
982 {
983 struct btrfs_root *quota_root;
984 struct btrfs_qgroup *qgroup;
985 int ret = 0;
986
987 quota_root = fs_info->quota_root;
988 if (!quota_root)
989 return -EINVAL;
990
991 /* check if there are no relations to this qgroup */
992 spin_lock(&fs_info->qgroup_lock);
993 qgroup = find_qgroup_rb(fs_info, qgroupid);
994 if (qgroup) {
995 if (!list_empty(&qgroup->groups) || !list_empty(&qgroup->members)) {
996 spin_unlock(&fs_info->qgroup_lock);
997 return -EBUSY;
998 }
999 }
1000 spin_unlock(&fs_info->qgroup_lock);
1001
1002 ret = del_qgroup_item(trans, quota_root, qgroupid);
1003
1004 spin_lock(&fs_info->qgroup_lock);
1005 del_qgroup_rb(quota_root->fs_info, qgroupid);
1006 spin_unlock(&fs_info->qgroup_lock);
1007
1008 return ret;
1009 }
1010
1011 int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
1012 struct btrfs_fs_info *fs_info, u64 qgroupid,
1013 struct btrfs_qgroup_limit *limit)
1014 {
1015 struct btrfs_root *quota_root = fs_info->quota_root;
1016 struct btrfs_qgroup *qgroup;
1017 int ret = 0;
1018
1019 if (!quota_root)
1020 return -EINVAL;
1021
1022 ret = update_qgroup_limit_item(trans, quota_root, qgroupid,
1023 limit->flags, limit->max_rfer,
1024 limit->max_excl, limit->rsv_rfer,
1025 limit->rsv_excl);
1026 if (ret) {
1027 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1028 printk(KERN_INFO "unable to update quota limit for %llu\n",
1029 (unsigned long long)qgroupid);
1030 }
1031
1032 spin_lock(&fs_info->qgroup_lock);
1033
1034 qgroup = find_qgroup_rb(fs_info, qgroupid);
1035 if (!qgroup) {
1036 ret = -ENOENT;
1037 goto unlock;
1038 }
1039 qgroup->lim_flags = limit->flags;
1040 qgroup->max_rfer = limit->max_rfer;
1041 qgroup->max_excl = limit->max_excl;
1042 qgroup->rsv_rfer = limit->rsv_rfer;
1043 qgroup->rsv_excl = limit->rsv_excl;
1044
1045 unlock:
1046 spin_unlock(&fs_info->qgroup_lock);
1047
1048 return ret;
1049 }
1050
1051 static void qgroup_dirty(struct btrfs_fs_info *fs_info,
1052 struct btrfs_qgroup *qgroup)
1053 {
1054 if (list_empty(&qgroup->dirty))
1055 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
1056 }
1057
1058 /*
1059 * btrfs_qgroup_record_ref is called when the ref is added or deleted. it puts
1060 * the modification into a list that's later used by btrfs_end_transaction to
1061 * pass the recorded modifications on to btrfs_qgroup_account_ref.
1062 */
1063 int btrfs_qgroup_record_ref(struct btrfs_trans_handle *trans,
1064 struct btrfs_delayed_ref_node *node,
1065 struct btrfs_delayed_extent_op *extent_op)
1066 {
1067 struct qgroup_update *u;
1068
1069 BUG_ON(!trans->delayed_ref_elem.seq);
1070 u = kmalloc(sizeof(*u), GFP_NOFS);
1071 if (!u)
1072 return -ENOMEM;
1073
1074 u->node = node;
1075 u->extent_op = extent_op;
1076 list_add_tail(&u->list, &trans->qgroup_ref_list);
1077
1078 return 0;
1079 }
1080
1081 /*
1082 * btrfs_qgroup_account_ref is called for every ref that is added to or deleted
1083 * from the fs. First, all roots referencing the extent are searched, and
1084 * then the space is accounted accordingly to the different roots. The
1085 * accounting algorithm works in 3 steps documented inline.
1086 */
1087 int btrfs_qgroup_account_ref(struct btrfs_trans_handle *trans,
1088 struct btrfs_fs_info *fs_info,
1089 struct btrfs_delayed_ref_node *node,
1090 struct btrfs_delayed_extent_op *extent_op)
1091 {
1092 struct btrfs_key ins;
1093 struct btrfs_root *quota_root;
1094 u64 ref_root;
1095 struct btrfs_qgroup *qgroup;
1096 struct ulist_node *unode;
1097 struct ulist *roots = NULL;
1098 struct ulist *tmp = NULL;
1099 struct ulist_iterator uiter;
1100 u64 seq;
1101 int ret = 0;
1102 int sgn;
1103
1104 if (!fs_info->quota_enabled)
1105 return 0;
1106
1107 BUG_ON(!fs_info->quota_root);
1108
1109 ins.objectid = node->bytenr;
1110 ins.offset = node->num_bytes;
1111 ins.type = BTRFS_EXTENT_ITEM_KEY;
1112
1113 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1114 node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
1115 struct btrfs_delayed_tree_ref *ref;
1116 ref = btrfs_delayed_node_to_tree_ref(node);
1117 ref_root = ref->root;
1118 } else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1119 node->type == BTRFS_SHARED_DATA_REF_KEY) {
1120 struct btrfs_delayed_data_ref *ref;
1121 ref = btrfs_delayed_node_to_data_ref(node);
1122 ref_root = ref->root;
1123 } else {
1124 BUG();
1125 }
1126
1127 if (!is_fstree(ref_root)) {
1128 /*
1129 * non-fs-trees are not being accounted
1130 */
1131 return 0;
1132 }
1133
1134 switch (node->action) {
1135 case BTRFS_ADD_DELAYED_REF:
1136 case BTRFS_ADD_DELAYED_EXTENT:
1137 sgn = 1;
1138 break;
1139 case BTRFS_DROP_DELAYED_REF:
1140 sgn = -1;
1141 break;
1142 case BTRFS_UPDATE_DELAYED_HEAD:
1143 return 0;
1144 default:
1145 BUG();
1146 }
1147
1148 /*
1149 * the delayed ref sequence number we pass depends on the direction of
1150 * the operation. for add operations, we pass (node->seq - 1) to skip
1151 * the delayed ref's current sequence number, because we need the state
1152 * of the tree before the add operation. for delete operations, we pass
1153 * (node->seq) to include the delayed ref's current sequence number,
1154 * because we need the state of the tree after the delete operation.
1155 */
1156 ret = btrfs_find_all_roots(trans, fs_info, node->bytenr,
1157 sgn > 0 ? node->seq - 1 : node->seq, &roots);
1158 if (ret < 0)
1159 goto out;
1160
1161 spin_lock(&fs_info->qgroup_lock);
1162 quota_root = fs_info->quota_root;
1163 if (!quota_root)
1164 goto unlock;
1165
1166 qgroup = find_qgroup_rb(fs_info, ref_root);
1167 if (!qgroup)
1168 goto unlock;
1169
1170 /*
1171 * step 1: for each old ref, visit all nodes once and inc refcnt
1172 */
1173 tmp = ulist_alloc(GFP_ATOMIC);
1174 if (!tmp) {
1175 ret = -ENOMEM;
1176 goto unlock;
1177 }
1178 seq = fs_info->qgroup_seq;
1179 fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */
1180
1181 ULIST_ITER_INIT(&uiter);
1182 while ((unode = ulist_next(roots, &uiter))) {
1183 struct ulist_node *tmp_unode;
1184 struct ulist_iterator tmp_uiter;
1185 struct btrfs_qgroup *qg;
1186
1187 qg = find_qgroup_rb(fs_info, unode->val);
1188 if (!qg)
1189 continue;
1190
1191 ulist_reinit(tmp);
1192 /* XXX id not needed */
1193 ulist_add(tmp, qg->qgroupid, (u64)(uintptr_t)qg, GFP_ATOMIC);
1194 ULIST_ITER_INIT(&tmp_uiter);
1195 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1196 struct btrfs_qgroup_list *glist;
1197
1198 qg = (struct btrfs_qgroup *)(uintptr_t)tmp_unode->aux;
1199 if (qg->refcnt < seq)
1200 qg->refcnt = seq + 1;
1201 else
1202 ++qg->refcnt;
1203
1204 list_for_each_entry(glist, &qg->groups, next_group) {
1205 ulist_add(tmp, glist->group->qgroupid,
1206 (u64)(uintptr_t)glist->group,
1207 GFP_ATOMIC);
1208 }
1209 }
1210 }
1211
1212 /*
1213 * step 2: walk from the new root
1214 */
1215 ulist_reinit(tmp);
1216 ulist_add(tmp, qgroup->qgroupid, (uintptr_t)qgroup, GFP_ATOMIC);
1217 ULIST_ITER_INIT(&uiter);
1218 while ((unode = ulist_next(tmp, &uiter))) {
1219 struct btrfs_qgroup *qg;
1220 struct btrfs_qgroup_list *glist;
1221
1222 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
1223 if (qg->refcnt < seq) {
1224 /* not visited by step 1 */
1225 qg->rfer += sgn * node->num_bytes;
1226 qg->rfer_cmpr += sgn * node->num_bytes;
1227 if (roots->nnodes == 0) {
1228 qg->excl += sgn * node->num_bytes;
1229 qg->excl_cmpr += sgn * node->num_bytes;
1230 }
1231 qgroup_dirty(fs_info, qg);
1232 }
1233 WARN_ON(qg->tag >= seq);
1234 qg->tag = seq;
1235
1236 list_for_each_entry(glist, &qg->groups, next_group) {
1237 ulist_add(tmp, glist->group->qgroupid,
1238 (uintptr_t)glist->group, GFP_ATOMIC);
1239 }
1240 }
1241
1242 /*
1243 * step 3: walk again from old refs
1244 */
1245 ULIST_ITER_INIT(&uiter);
1246 while ((unode = ulist_next(roots, &uiter))) {
1247 struct btrfs_qgroup *qg;
1248 struct ulist_node *tmp_unode;
1249 struct ulist_iterator tmp_uiter;
1250
1251 qg = find_qgroup_rb(fs_info, unode->val);
1252 if (!qg)
1253 continue;
1254
1255 ulist_reinit(tmp);
1256 ulist_add(tmp, qg->qgroupid, (uintptr_t)qg, GFP_ATOMIC);
1257 ULIST_ITER_INIT(&tmp_uiter);
1258 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1259 struct btrfs_qgroup_list *glist;
1260
1261 qg = (struct btrfs_qgroup *)(uintptr_t)tmp_unode->aux;
1262 if (qg->tag == seq)
1263 continue;
1264
1265 if (qg->refcnt - seq == roots->nnodes) {
1266 qg->excl -= sgn * node->num_bytes;
1267 qg->excl_cmpr -= sgn * node->num_bytes;
1268 qgroup_dirty(fs_info, qg);
1269 }
1270
1271 list_for_each_entry(glist, &qg->groups, next_group) {
1272 ulist_add(tmp, glist->group->qgroupid,
1273 (uintptr_t)glist->group,
1274 GFP_ATOMIC);
1275 }
1276 }
1277 }
1278 ret = 0;
1279 unlock:
1280 spin_unlock(&fs_info->qgroup_lock);
1281 out:
1282 ulist_free(roots);
1283 ulist_free(tmp);
1284
1285 return ret;
1286 }
1287
1288 /*
1289 * called from commit_transaction. Writes all changed qgroups to disk.
1290 */
1291 int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
1292 struct btrfs_fs_info *fs_info)
1293 {
1294 struct btrfs_root *quota_root = fs_info->quota_root;
1295 int ret = 0;
1296
1297 if (!quota_root)
1298 goto out;
1299
1300 fs_info->quota_enabled = fs_info->pending_quota_state;
1301
1302 spin_lock(&fs_info->qgroup_lock);
1303 while (!list_empty(&fs_info->dirty_qgroups)) {
1304 struct btrfs_qgroup *qgroup;
1305 qgroup = list_first_entry(&fs_info->dirty_qgroups,
1306 struct btrfs_qgroup, dirty);
1307 list_del_init(&qgroup->dirty);
1308 spin_unlock(&fs_info->qgroup_lock);
1309 ret = update_qgroup_info_item(trans, quota_root, qgroup);
1310 if (ret)
1311 fs_info->qgroup_flags |=
1312 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1313 spin_lock(&fs_info->qgroup_lock);
1314 }
1315 if (fs_info->quota_enabled)
1316 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
1317 else
1318 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
1319 spin_unlock(&fs_info->qgroup_lock);
1320
1321 ret = update_qgroup_status_item(trans, fs_info, quota_root);
1322 if (ret)
1323 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1324
1325 out:
1326
1327 return ret;
1328 }
1329
1330 /*
1331 * copy the acounting information between qgroups. This is necessary when a
1332 * snapshot or a subvolume is created
1333 */
1334 int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
1335 struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
1336 struct btrfs_qgroup_inherit *inherit)
1337 {
1338 int ret = 0;
1339 int i;
1340 u64 *i_qgroups;
1341 struct btrfs_root *quota_root = fs_info->quota_root;
1342 struct btrfs_qgroup *srcgroup;
1343 struct btrfs_qgroup *dstgroup;
1344 u32 level_size = 0;
1345
1346 if (!fs_info->quota_enabled)
1347 return 0;
1348
1349 if (!quota_root)
1350 return -EINVAL;
1351
1352 /*
1353 * create a tracking group for the subvol itself
1354 */
1355 ret = add_qgroup_item(trans, quota_root, objectid);
1356 if (ret)
1357 goto out;
1358
1359 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
1360 ret = update_qgroup_limit_item(trans, quota_root, objectid,
1361 inherit->lim.flags,
1362 inherit->lim.max_rfer,
1363 inherit->lim.max_excl,
1364 inherit->lim.rsv_rfer,
1365 inherit->lim.rsv_excl);
1366 if (ret)
1367 goto out;
1368 }
1369
1370 if (srcid) {
1371 struct btrfs_root *srcroot;
1372 struct btrfs_key srckey;
1373 int srcroot_level;
1374
1375 srckey.objectid = srcid;
1376 srckey.type = BTRFS_ROOT_ITEM_KEY;
1377 srckey.offset = (u64)-1;
1378 srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
1379 if (IS_ERR(srcroot)) {
1380 ret = PTR_ERR(srcroot);
1381 goto out;
1382 }
1383
1384 rcu_read_lock();
1385 srcroot_level = btrfs_header_level(srcroot->node);
1386 level_size = btrfs_level_size(srcroot, srcroot_level);
1387 rcu_read_unlock();
1388 }
1389
1390 /*
1391 * add qgroup to all inherited groups
1392 */
1393 if (inherit) {
1394 i_qgroups = (u64 *)(inherit + 1);
1395 for (i = 0; i < inherit->num_qgroups; ++i) {
1396 ret = add_qgroup_relation_item(trans, quota_root,
1397 objectid, *i_qgroups);
1398 if (ret)
1399 goto out;
1400 ret = add_qgroup_relation_item(trans, quota_root,
1401 *i_qgroups, objectid);
1402 if (ret)
1403 goto out;
1404 ++i_qgroups;
1405 }
1406 }
1407
1408
1409 spin_lock(&fs_info->qgroup_lock);
1410
1411 dstgroup = add_qgroup_rb(fs_info, objectid);
1412 if (IS_ERR(dstgroup)) {
1413 ret = PTR_ERR(dstgroup);
1414 goto unlock;
1415 }
1416
1417 if (srcid) {
1418 srcgroup = find_qgroup_rb(fs_info, srcid);
1419 if (!srcgroup)
1420 goto unlock;
1421 dstgroup->rfer = srcgroup->rfer - level_size;
1422 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr - level_size;
1423 srcgroup->excl = level_size;
1424 srcgroup->excl_cmpr = level_size;
1425 qgroup_dirty(fs_info, dstgroup);
1426 qgroup_dirty(fs_info, srcgroup);
1427 }
1428
1429 if (!inherit)
1430 goto unlock;
1431
1432 i_qgroups = (u64 *)(inherit + 1);
1433 for (i = 0; i < inherit->num_qgroups; ++i) {
1434 ret = add_relation_rb(quota_root->fs_info, objectid,
1435 *i_qgroups);
1436 if (ret)
1437 goto unlock;
1438 ++i_qgroups;
1439 }
1440
1441 for (i = 0; i < inherit->num_ref_copies; ++i) {
1442 struct btrfs_qgroup *src;
1443 struct btrfs_qgroup *dst;
1444
1445 src = find_qgroup_rb(fs_info, i_qgroups[0]);
1446 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
1447
1448 if (!src || !dst) {
1449 ret = -EINVAL;
1450 goto unlock;
1451 }
1452
1453 dst->rfer = src->rfer - level_size;
1454 dst->rfer_cmpr = src->rfer_cmpr - level_size;
1455 i_qgroups += 2;
1456 }
1457 for (i = 0; i < inherit->num_excl_copies; ++i) {
1458 struct btrfs_qgroup *src;
1459 struct btrfs_qgroup *dst;
1460
1461 src = find_qgroup_rb(fs_info, i_qgroups[0]);
1462 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
1463
1464 if (!src || !dst) {
1465 ret = -EINVAL;
1466 goto unlock;
1467 }
1468
1469 dst->excl = src->excl + level_size;
1470 dst->excl_cmpr = src->excl_cmpr + level_size;
1471 i_qgroups += 2;
1472 }
1473
1474 unlock:
1475 spin_unlock(&fs_info->qgroup_lock);
1476 out:
1477 return ret;
1478 }
1479
1480 /*
1481 * reserve some space for a qgroup and all its parents. The reservation takes
1482 * place with start_transaction or dealloc_reserve, similar to ENOSPC
1483 * accounting. If not enough space is available, EDQUOT is returned.
1484 * We assume that the requested space is new for all qgroups.
1485 */
1486 int btrfs_qgroup_reserve(struct btrfs_root *root, u64 num_bytes)
1487 {
1488 struct btrfs_root *quota_root;
1489 struct btrfs_qgroup *qgroup;
1490 struct btrfs_fs_info *fs_info = root->fs_info;
1491 u64 ref_root = root->root_key.objectid;
1492 int ret = 0;
1493 struct ulist *ulist = NULL;
1494 struct ulist_node *unode;
1495 struct ulist_iterator uiter;
1496
1497 if (!is_fstree(ref_root))
1498 return 0;
1499
1500 if (num_bytes == 0)
1501 return 0;
1502
1503 spin_lock(&fs_info->qgroup_lock);
1504 quota_root = fs_info->quota_root;
1505 if (!quota_root)
1506 goto out;
1507
1508 qgroup = find_qgroup_rb(fs_info, ref_root);
1509 if (!qgroup)
1510 goto out;
1511
1512 /*
1513 * in a first step, we check all affected qgroups if any limits would
1514 * be exceeded
1515 */
1516 ulist = ulist_alloc(GFP_ATOMIC);
1517 if (!ulist) {
1518 ret = -ENOMEM;
1519 goto out;
1520 }
1521 ulist_add(ulist, qgroup->qgroupid, (uintptr_t)qgroup, GFP_ATOMIC);
1522 ULIST_ITER_INIT(&uiter);
1523 while ((unode = ulist_next(ulist, &uiter))) {
1524 struct btrfs_qgroup *qg;
1525 struct btrfs_qgroup_list *glist;
1526
1527 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
1528
1529 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
1530 qg->reserved + qg->rfer + num_bytes >
1531 qg->max_rfer)
1532 ret = -EDQUOT;
1533
1534 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
1535 qg->reserved + qg->excl + num_bytes >
1536 qg->max_excl)
1537 ret = -EDQUOT;
1538
1539 list_for_each_entry(glist, &qg->groups, next_group) {
1540 ulist_add(ulist, glist->group->qgroupid,
1541 (uintptr_t)glist->group, GFP_ATOMIC);
1542 }
1543 }
1544 if (ret)
1545 goto out;
1546
1547 /*
1548 * no limits exceeded, now record the reservation into all qgroups
1549 */
1550 ULIST_ITER_INIT(&uiter);
1551 while ((unode = ulist_next(ulist, &uiter))) {
1552 struct btrfs_qgroup *qg;
1553
1554 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
1555
1556 qg->reserved += num_bytes;
1557 }
1558
1559 out:
1560 spin_unlock(&fs_info->qgroup_lock);
1561 ulist_free(ulist);
1562
1563 return ret;
1564 }
1565
1566 void btrfs_qgroup_free(struct btrfs_root *root, u64 num_bytes)
1567 {
1568 struct btrfs_root *quota_root;
1569 struct btrfs_qgroup *qgroup;
1570 struct btrfs_fs_info *fs_info = root->fs_info;
1571 struct ulist *ulist = NULL;
1572 struct ulist_node *unode;
1573 struct ulist_iterator uiter;
1574 u64 ref_root = root->root_key.objectid;
1575
1576 if (!is_fstree(ref_root))
1577 return;
1578
1579 if (num_bytes == 0)
1580 return;
1581
1582 spin_lock(&fs_info->qgroup_lock);
1583
1584 quota_root = fs_info->quota_root;
1585 if (!quota_root)
1586 goto out;
1587
1588 qgroup = find_qgroup_rb(fs_info, ref_root);
1589 if (!qgroup)
1590 goto out;
1591
1592 ulist = ulist_alloc(GFP_ATOMIC);
1593 if (!ulist) {
1594 btrfs_std_error(fs_info, -ENOMEM);
1595 goto out;
1596 }
1597 ulist_add(ulist, qgroup->qgroupid, (uintptr_t)qgroup, GFP_ATOMIC);
1598 ULIST_ITER_INIT(&uiter);
1599 while ((unode = ulist_next(ulist, &uiter))) {
1600 struct btrfs_qgroup *qg;
1601 struct btrfs_qgroup_list *glist;
1602
1603 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
1604
1605 qg->reserved -= num_bytes;
1606
1607 list_for_each_entry(glist, &qg->groups, next_group) {
1608 ulist_add(ulist, glist->group->qgroupid,
1609 (uintptr_t)glist->group, GFP_ATOMIC);
1610 }
1611 }
1612
1613 out:
1614 spin_unlock(&fs_info->qgroup_lock);
1615 ulist_free(ulist);
1616 }
1617
1618 void assert_qgroups_uptodate(struct btrfs_trans_handle *trans)
1619 {
1620 if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq)
1621 return;
1622 printk(KERN_ERR "btrfs: qgroups not uptodate in trans handle %p: list is%s empty, seq is %llu\n",
1623 trans, list_empty(&trans->qgroup_ref_list) ? "" : " not",
1624 trans->delayed_ref_elem.seq);
1625 BUG();
1626 }