blob: 88ab785bbd73181e64843d97838799619e1c140e [file] [log] [blame]
Arne Jansenbed92ea2012-06-28 18:03:02 +02001/*
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>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000026#include <linux/btrfs.h>
Arne Jansenbed92ea2012-06-28 18:03:02 +020027
28#include "ctree.h"
29#include "transaction.h"
30#include "disk-io.h"
31#include "locking.h"
32#include "ulist.h"
Arne Jansenbed92ea2012-06-28 18:03:02 +020033#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 */
51struct 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 */
94struct 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 */
102static 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 */
121static 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 */
156static 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 */
188static 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 */
213static 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 */
240int 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 }
348next1:
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);
Arne Jansenff248582013-01-17 01:22:08 -0700382 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 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200389 if (ret)
390 goto out;
391next2:
392 ret = btrfs_next_item(quota_root, path);
393 if (ret < 0)
394 goto out;
395 if (ret)
396 break;
397 }
398out:
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 */
413void 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
446static 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
470static 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);
496out:
497 btrfs_free_path(path);
498 return ret;
499}
500
501static 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;
555out:
556 btrfs_free_path(path);
557 return ret;
558}
559
560static 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
601out:
602 btrfs_free_path(path);
603 return ret;
604}
605
606static 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 BUG_ON(!path);
624 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
625 if (ret > 0)
626 ret = -ENOENT;
627
628 if (ret)
629 goto out;
630
631 l = path->nodes[0];
632 slot = path->slots[0];
633 qgroup_limit = btrfs_item_ptr(l, path->slots[0],
634 struct btrfs_qgroup_limit_item);
635 btrfs_set_qgroup_limit_flags(l, qgroup_limit, flags);
636 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, max_rfer);
637 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, max_excl);
638 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, rsv_rfer);
639 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, rsv_excl);
640
641 btrfs_mark_buffer_dirty(l);
642
643out:
644 btrfs_free_path(path);
645 return ret;
646}
647
648static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
649 struct btrfs_root *root,
650 struct btrfs_qgroup *qgroup)
651{
652 struct btrfs_path *path;
653 struct btrfs_key key;
654 struct extent_buffer *l;
655 struct btrfs_qgroup_info_item *qgroup_info;
656 int ret;
657 int slot;
658
659 key.objectid = 0;
660 key.type = BTRFS_QGROUP_INFO_KEY;
661 key.offset = qgroup->qgroupid;
662
663 path = btrfs_alloc_path();
664 BUG_ON(!path);
665 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
666 if (ret > 0)
667 ret = -ENOENT;
668
669 if (ret)
670 goto out;
671
672 l = path->nodes[0];
673 slot = path->slots[0];
674 qgroup_info = btrfs_item_ptr(l, path->slots[0],
675 struct btrfs_qgroup_info_item);
676 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
677 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
678 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
679 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
680 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
681
682 btrfs_mark_buffer_dirty(l);
683
684out:
685 btrfs_free_path(path);
686 return ret;
687}
688
689static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
690 struct btrfs_fs_info *fs_info,
691 struct btrfs_root *root)
692{
693 struct btrfs_path *path;
694 struct btrfs_key key;
695 struct extent_buffer *l;
696 struct btrfs_qgroup_status_item *ptr;
697 int ret;
698 int slot;
699
700 key.objectid = 0;
701 key.type = BTRFS_QGROUP_STATUS_KEY;
702 key.offset = 0;
703
704 path = btrfs_alloc_path();
705 BUG_ON(!path);
706 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
707 if (ret > 0)
708 ret = -ENOENT;
709
710 if (ret)
711 goto out;
712
713 l = path->nodes[0];
714 slot = path->slots[0];
715 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
716 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
717 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
718 /* XXX scan */
719
720 btrfs_mark_buffer_dirty(l);
721
722out:
723 btrfs_free_path(path);
724 return ret;
725}
726
727/*
728 * called with qgroup_lock held
729 */
730static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
731 struct btrfs_root *root)
732{
733 struct btrfs_path *path;
734 struct btrfs_key key;
735 int ret;
736
737 if (!root)
738 return -EINVAL;
739
740 path = btrfs_alloc_path();
741 if (!path)
742 return -ENOMEM;
743
744 while (1) {
745 key.objectid = 0;
746 key.offset = 0;
747 key.type = 0;
748
749 path->leave_spinning = 1;
750 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
751 if (ret > 0) {
752 if (path->slots[0] == 0)
753 break;
754 path->slots[0]--;
755 } else if (ret < 0) {
756 break;
757 }
758
759 ret = btrfs_del_item(trans, root, path);
760 if (ret)
761 goto out;
762 btrfs_release_path(path);
763 }
764 ret = 0;
765out:
766 root->fs_info->pending_quota_state = 0;
767 btrfs_free_path(path);
768 return ret;
769}
770
771int btrfs_quota_enable(struct btrfs_trans_handle *trans,
772 struct btrfs_fs_info *fs_info)
773{
774 struct btrfs_root *quota_root;
775 struct btrfs_path *path = NULL;
776 struct btrfs_qgroup_status_item *ptr;
777 struct extent_buffer *leaf;
778 struct btrfs_key key;
779 int ret = 0;
780
781 spin_lock(&fs_info->qgroup_lock);
782 if (fs_info->quota_root) {
783 fs_info->pending_quota_state = 1;
784 spin_unlock(&fs_info->qgroup_lock);
785 goto out;
786 }
787 spin_unlock(&fs_info->qgroup_lock);
788
789 /*
790 * initially create the quota tree
791 */
792 quota_root = btrfs_create_tree(trans, fs_info,
793 BTRFS_QUOTA_TREE_OBJECTID);
794 if (IS_ERR(quota_root)) {
795 ret = PTR_ERR(quota_root);
796 goto out;
797 }
798
799 path = btrfs_alloc_path();
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000800 if (!path) {
801 ret = -ENOMEM;
802 goto out_free_root;
803 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200804
805 key.objectid = 0;
806 key.type = BTRFS_QGROUP_STATUS_KEY;
807 key.offset = 0;
808
809 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
810 sizeof(*ptr));
811 if (ret)
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000812 goto out_free_path;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200813
814 leaf = path->nodes[0];
815 ptr = btrfs_item_ptr(leaf, path->slots[0],
816 struct btrfs_qgroup_status_item);
817 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
818 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
819 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
820 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
821 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
822 btrfs_set_qgroup_status_scan(leaf, ptr, 0);
823
824 btrfs_mark_buffer_dirty(leaf);
825
826 spin_lock(&fs_info->qgroup_lock);
827 fs_info->quota_root = quota_root;
828 fs_info->pending_quota_state = 1;
829 spin_unlock(&fs_info->qgroup_lock);
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000830out_free_path:
Arne Jansenbed92ea2012-06-28 18:03:02 +0200831 btrfs_free_path(path);
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000832out_free_root:
833 if (ret) {
834 free_extent_buffer(quota_root->node);
835 free_extent_buffer(quota_root->commit_root);
836 kfree(quota_root);
837 }
838out:
Arne Jansenbed92ea2012-06-28 18:03:02 +0200839 return ret;
840}
841
842int btrfs_quota_disable(struct btrfs_trans_handle *trans,
843 struct btrfs_fs_info *fs_info)
844{
845 struct btrfs_root *tree_root = fs_info->tree_root;
846 struct btrfs_root *quota_root;
847 int ret = 0;
848
849 spin_lock(&fs_info->qgroup_lock);
Wang Shilong683cebd2013-02-20 14:16:14 +0000850 if (!fs_info->quota_root) {
851 spin_unlock(&fs_info->qgroup_lock);
852 return 0;
853 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200854 fs_info->quota_enabled = 0;
855 fs_info->pending_quota_state = 0;
856 quota_root = fs_info->quota_root;
857 fs_info->quota_root = NULL;
858 btrfs_free_qgroup_config(fs_info);
859 spin_unlock(&fs_info->qgroup_lock);
860
861 if (!quota_root)
862 return -EINVAL;
863
864 ret = btrfs_clean_quota_tree(trans, quota_root);
865 if (ret)
866 goto out;
867
868 ret = btrfs_del_root(trans, tree_root, &quota_root->root_key);
869 if (ret)
870 goto out;
871
872 list_del(&quota_root->dirty_list);
873
874 btrfs_tree_lock(quota_root->node);
875 clean_tree_block(trans, tree_root, quota_root->node);
876 btrfs_tree_unlock(quota_root->node);
877 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
878
879 free_extent_buffer(quota_root->node);
880 free_extent_buffer(quota_root->commit_root);
881 kfree(quota_root);
882out:
883 return ret;
884}
885
886int btrfs_quota_rescan(struct btrfs_fs_info *fs_info)
887{
888 /* FIXME */
889 return 0;
890}
891
892int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
893 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
894{
895 struct btrfs_root *quota_root;
896 int ret = 0;
897
898 quota_root = fs_info->quota_root;
899 if (!quota_root)
900 return -EINVAL;
901
902 ret = add_qgroup_relation_item(trans, quota_root, src, dst);
903 if (ret)
904 return ret;
905
906 ret = add_qgroup_relation_item(trans, quota_root, dst, src);
907 if (ret) {
908 del_qgroup_relation_item(trans, quota_root, src, dst);
909 return ret;
910 }
911
912 spin_lock(&fs_info->qgroup_lock);
913 ret = add_relation_rb(quota_root->fs_info, src, dst);
914 spin_unlock(&fs_info->qgroup_lock);
915
916 return ret;
917}
918
919int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
920 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
921{
922 struct btrfs_root *quota_root;
923 int ret = 0;
924 int err;
925
926 quota_root = fs_info->quota_root;
927 if (!quota_root)
928 return -EINVAL;
929
930 ret = del_qgroup_relation_item(trans, quota_root, src, dst);
931 err = del_qgroup_relation_item(trans, quota_root, dst, src);
932 if (err && !ret)
933 ret = err;
934
935 spin_lock(&fs_info->qgroup_lock);
936 del_relation_rb(fs_info, src, dst);
937
938 spin_unlock(&fs_info->qgroup_lock);
939
940 return ret;
941}
942
943int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
944 struct btrfs_fs_info *fs_info, u64 qgroupid, char *name)
945{
946 struct btrfs_root *quota_root;
947 struct btrfs_qgroup *qgroup;
948 int ret = 0;
949
950 quota_root = fs_info->quota_root;
951 if (!quota_root)
952 return -EINVAL;
953
954 ret = add_qgroup_item(trans, quota_root, qgroupid);
955
956 spin_lock(&fs_info->qgroup_lock);
957 qgroup = add_qgroup_rb(fs_info, qgroupid);
958 spin_unlock(&fs_info->qgroup_lock);
959
960 if (IS_ERR(qgroup))
961 ret = PTR_ERR(qgroup);
962
963 return ret;
964}
965
966int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
967 struct btrfs_fs_info *fs_info, u64 qgroupid)
968{
969 struct btrfs_root *quota_root;
Arne Jansen2cf68702013-01-17 01:22:09 -0700970 struct btrfs_qgroup *qgroup;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200971 int ret = 0;
972
973 quota_root = fs_info->quota_root;
974 if (!quota_root)
975 return -EINVAL;
976
Arne Jansen2cf68702013-01-17 01:22:09 -0700977 /* check if there are no relations to this qgroup */
978 spin_lock(&fs_info->qgroup_lock);
979 qgroup = find_qgroup_rb(fs_info, qgroupid);
980 if (qgroup) {
981 if (!list_empty(&qgroup->groups) || !list_empty(&qgroup->members)) {
982 spin_unlock(&fs_info->qgroup_lock);
983 return -EBUSY;
984 }
985 }
986 spin_unlock(&fs_info->qgroup_lock);
987
Arne Jansenbed92ea2012-06-28 18:03:02 +0200988 ret = del_qgroup_item(trans, quota_root, qgroupid);
989
990 spin_lock(&fs_info->qgroup_lock);
991 del_qgroup_rb(quota_root->fs_info, qgroupid);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200992 spin_unlock(&fs_info->qgroup_lock);
993
994 return ret;
995}
996
997int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
998 struct btrfs_fs_info *fs_info, u64 qgroupid,
999 struct btrfs_qgroup_limit *limit)
1000{
1001 struct btrfs_root *quota_root = fs_info->quota_root;
1002 struct btrfs_qgroup *qgroup;
1003 int ret = 0;
1004
1005 if (!quota_root)
1006 return -EINVAL;
1007
1008 ret = update_qgroup_limit_item(trans, quota_root, qgroupid,
1009 limit->flags, limit->max_rfer,
1010 limit->max_excl, limit->rsv_rfer,
1011 limit->rsv_excl);
1012 if (ret) {
1013 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1014 printk(KERN_INFO "unable to update quota limit for %llu\n",
1015 (unsigned long long)qgroupid);
1016 }
1017
1018 spin_lock(&fs_info->qgroup_lock);
1019
1020 qgroup = find_qgroup_rb(fs_info, qgroupid);
1021 if (!qgroup) {
1022 ret = -ENOENT;
1023 goto unlock;
1024 }
1025 qgroup->lim_flags = limit->flags;
1026 qgroup->max_rfer = limit->max_rfer;
1027 qgroup->max_excl = limit->max_excl;
1028 qgroup->rsv_rfer = limit->rsv_rfer;
1029 qgroup->rsv_excl = limit->rsv_excl;
1030
1031unlock:
1032 spin_unlock(&fs_info->qgroup_lock);
1033
1034 return ret;
1035}
1036
1037static void qgroup_dirty(struct btrfs_fs_info *fs_info,
1038 struct btrfs_qgroup *qgroup)
1039{
1040 if (list_empty(&qgroup->dirty))
1041 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
1042}
1043
1044/*
1045 * btrfs_qgroup_record_ref is called when the ref is added or deleted. it puts
1046 * the modification into a list that's later used by btrfs_end_transaction to
1047 * pass the recorded modifications on to btrfs_qgroup_account_ref.
1048 */
1049int btrfs_qgroup_record_ref(struct btrfs_trans_handle *trans,
1050 struct btrfs_delayed_ref_node *node,
1051 struct btrfs_delayed_extent_op *extent_op)
1052{
1053 struct qgroup_update *u;
1054
1055 BUG_ON(!trans->delayed_ref_elem.seq);
1056 u = kmalloc(sizeof(*u), GFP_NOFS);
1057 if (!u)
1058 return -ENOMEM;
1059
1060 u->node = node;
1061 u->extent_op = extent_op;
1062 list_add_tail(&u->list, &trans->qgroup_ref_list);
1063
1064 return 0;
1065}
1066
1067/*
1068 * btrfs_qgroup_account_ref is called for every ref that is added to or deleted
1069 * from the fs. First, all roots referencing the extent are searched, and
1070 * then the space is accounted accordingly to the different roots. The
1071 * accounting algorithm works in 3 steps documented inline.
1072 */
1073int btrfs_qgroup_account_ref(struct btrfs_trans_handle *trans,
1074 struct btrfs_fs_info *fs_info,
1075 struct btrfs_delayed_ref_node *node,
1076 struct btrfs_delayed_extent_op *extent_op)
1077{
1078 struct btrfs_key ins;
1079 struct btrfs_root *quota_root;
1080 u64 ref_root;
1081 struct btrfs_qgroup *qgroup;
1082 struct ulist_node *unode;
1083 struct ulist *roots = NULL;
1084 struct ulist *tmp = NULL;
1085 struct ulist_iterator uiter;
1086 u64 seq;
1087 int ret = 0;
1088 int sgn;
1089
1090 if (!fs_info->quota_enabled)
1091 return 0;
1092
1093 BUG_ON(!fs_info->quota_root);
1094
1095 ins.objectid = node->bytenr;
1096 ins.offset = node->num_bytes;
1097 ins.type = BTRFS_EXTENT_ITEM_KEY;
1098
1099 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1100 node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
1101 struct btrfs_delayed_tree_ref *ref;
1102 ref = btrfs_delayed_node_to_tree_ref(node);
1103 ref_root = ref->root;
1104 } else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1105 node->type == BTRFS_SHARED_DATA_REF_KEY) {
1106 struct btrfs_delayed_data_ref *ref;
1107 ref = btrfs_delayed_node_to_data_ref(node);
1108 ref_root = ref->root;
1109 } else {
1110 BUG();
1111 }
1112
1113 if (!is_fstree(ref_root)) {
1114 /*
1115 * non-fs-trees are not being accounted
1116 */
1117 return 0;
1118 }
1119
1120 switch (node->action) {
1121 case BTRFS_ADD_DELAYED_REF:
1122 case BTRFS_ADD_DELAYED_EXTENT:
1123 sgn = 1;
1124 break;
1125 case BTRFS_DROP_DELAYED_REF:
1126 sgn = -1;
1127 break;
1128 case BTRFS_UPDATE_DELAYED_HEAD:
1129 return 0;
1130 default:
1131 BUG();
1132 }
1133
1134 /*
1135 * the delayed ref sequence number we pass depends on the direction of
1136 * the operation. for add operations, we pass (node->seq - 1) to skip
1137 * the delayed ref's current sequence number, because we need the state
1138 * of the tree before the add operation. for delete operations, we pass
1139 * (node->seq) to include the delayed ref's current sequence number,
1140 * because we need the state of the tree after the delete operation.
1141 */
1142 ret = btrfs_find_all_roots(trans, fs_info, node->bytenr,
1143 sgn > 0 ? node->seq - 1 : node->seq, &roots);
1144 if (ret < 0)
1145 goto out;
1146
1147 spin_lock(&fs_info->qgroup_lock);
1148 quota_root = fs_info->quota_root;
1149 if (!quota_root)
1150 goto unlock;
1151
1152 qgroup = find_qgroup_rb(fs_info, ref_root);
1153 if (!qgroup)
1154 goto unlock;
1155
1156 /*
1157 * step 1: for each old ref, visit all nodes once and inc refcnt
1158 */
1159 tmp = ulist_alloc(GFP_ATOMIC);
1160 if (!tmp) {
1161 ret = -ENOMEM;
1162 goto unlock;
1163 }
1164 seq = fs_info->qgroup_seq;
1165 fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */
1166
1167 ULIST_ITER_INIT(&uiter);
1168 while ((unode = ulist_next(roots, &uiter))) {
1169 struct ulist_node *tmp_unode;
1170 struct ulist_iterator tmp_uiter;
1171 struct btrfs_qgroup *qg;
1172
1173 qg = find_qgroup_rb(fs_info, unode->val);
1174 if (!qg)
1175 continue;
1176
1177 ulist_reinit(tmp);
1178 /* XXX id not needed */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001179 ulist_add(tmp, qg->qgroupid, (u64)(uintptr_t)qg, GFP_ATOMIC);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001180 ULIST_ITER_INIT(&tmp_uiter);
1181 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1182 struct btrfs_qgroup_list *glist;
1183
Jan Schmidt995e01b2012-08-13 02:52:38 -06001184 qg = (struct btrfs_qgroup *)(uintptr_t)tmp_unode->aux;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001185 if (qg->refcnt < seq)
1186 qg->refcnt = seq + 1;
1187 else
1188 ++qg->refcnt;
1189
1190 list_for_each_entry(glist, &qg->groups, next_group) {
1191 ulist_add(tmp, glist->group->qgroupid,
Jan Schmidt995e01b2012-08-13 02:52:38 -06001192 (u64)(uintptr_t)glist->group,
Arne Jansenbed92ea2012-06-28 18:03:02 +02001193 GFP_ATOMIC);
1194 }
1195 }
1196 }
1197
1198 /*
1199 * step 2: walk from the new root
1200 */
1201 ulist_reinit(tmp);
Jan Schmidt995e01b2012-08-13 02:52:38 -06001202 ulist_add(tmp, qgroup->qgroupid, (uintptr_t)qgroup, GFP_ATOMIC);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001203 ULIST_ITER_INIT(&uiter);
1204 while ((unode = ulist_next(tmp, &uiter))) {
1205 struct btrfs_qgroup *qg;
1206 struct btrfs_qgroup_list *glist;
1207
Jan Schmidt995e01b2012-08-13 02:52:38 -06001208 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001209 if (qg->refcnt < seq) {
1210 /* not visited by step 1 */
1211 qg->rfer += sgn * node->num_bytes;
1212 qg->rfer_cmpr += sgn * node->num_bytes;
1213 if (roots->nnodes == 0) {
1214 qg->excl += sgn * node->num_bytes;
1215 qg->excl_cmpr += sgn * node->num_bytes;
1216 }
1217 qgroup_dirty(fs_info, qg);
1218 }
1219 WARN_ON(qg->tag >= seq);
1220 qg->tag = seq;
1221
1222 list_for_each_entry(glist, &qg->groups, next_group) {
1223 ulist_add(tmp, glist->group->qgroupid,
Jan Schmidt995e01b2012-08-13 02:52:38 -06001224 (uintptr_t)glist->group, GFP_ATOMIC);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001225 }
1226 }
1227
1228 /*
1229 * step 3: walk again from old refs
1230 */
1231 ULIST_ITER_INIT(&uiter);
1232 while ((unode = ulist_next(roots, &uiter))) {
1233 struct btrfs_qgroup *qg;
1234 struct ulist_node *tmp_unode;
1235 struct ulist_iterator tmp_uiter;
1236
1237 qg = find_qgroup_rb(fs_info, unode->val);
1238 if (!qg)
1239 continue;
1240
1241 ulist_reinit(tmp);
Jan Schmidt995e01b2012-08-13 02:52:38 -06001242 ulist_add(tmp, qg->qgroupid, (uintptr_t)qg, GFP_ATOMIC);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001243 ULIST_ITER_INIT(&tmp_uiter);
1244 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1245 struct btrfs_qgroup_list *glist;
1246
Jan Schmidt995e01b2012-08-13 02:52:38 -06001247 qg = (struct btrfs_qgroup *)(uintptr_t)tmp_unode->aux;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001248 if (qg->tag == seq)
1249 continue;
1250
1251 if (qg->refcnt - seq == roots->nnodes) {
1252 qg->excl -= sgn * node->num_bytes;
1253 qg->excl_cmpr -= sgn * node->num_bytes;
1254 qgroup_dirty(fs_info, qg);
1255 }
1256
1257 list_for_each_entry(glist, &qg->groups, next_group) {
1258 ulist_add(tmp, glist->group->qgroupid,
Jan Schmidt995e01b2012-08-13 02:52:38 -06001259 (uintptr_t)glist->group,
Arne Jansenbed92ea2012-06-28 18:03:02 +02001260 GFP_ATOMIC);
1261 }
1262 }
1263 }
1264 ret = 0;
1265unlock:
1266 spin_unlock(&fs_info->qgroup_lock);
1267out:
1268 ulist_free(roots);
1269 ulist_free(tmp);
1270
1271 return ret;
1272}
1273
1274/*
1275 * called from commit_transaction. Writes all changed qgroups to disk.
1276 */
1277int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
1278 struct btrfs_fs_info *fs_info)
1279{
1280 struct btrfs_root *quota_root = fs_info->quota_root;
1281 int ret = 0;
1282
1283 if (!quota_root)
1284 goto out;
1285
1286 fs_info->quota_enabled = fs_info->pending_quota_state;
1287
1288 spin_lock(&fs_info->qgroup_lock);
1289 while (!list_empty(&fs_info->dirty_qgroups)) {
1290 struct btrfs_qgroup *qgroup;
1291 qgroup = list_first_entry(&fs_info->dirty_qgroups,
1292 struct btrfs_qgroup, dirty);
1293 list_del_init(&qgroup->dirty);
1294 spin_unlock(&fs_info->qgroup_lock);
1295 ret = update_qgroup_info_item(trans, quota_root, qgroup);
1296 if (ret)
1297 fs_info->qgroup_flags |=
1298 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1299 spin_lock(&fs_info->qgroup_lock);
1300 }
1301 if (fs_info->quota_enabled)
1302 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
1303 else
1304 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
1305 spin_unlock(&fs_info->qgroup_lock);
1306
1307 ret = update_qgroup_status_item(trans, fs_info, quota_root);
1308 if (ret)
1309 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1310
1311out:
1312
1313 return ret;
1314}
1315
1316/*
1317 * copy the acounting information between qgroups. This is necessary when a
1318 * snapshot or a subvolume is created
1319 */
1320int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
1321 struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
1322 struct btrfs_qgroup_inherit *inherit)
1323{
1324 int ret = 0;
1325 int i;
1326 u64 *i_qgroups;
1327 struct btrfs_root *quota_root = fs_info->quota_root;
1328 struct btrfs_qgroup *srcgroup;
1329 struct btrfs_qgroup *dstgroup;
1330 u32 level_size = 0;
1331
1332 if (!fs_info->quota_enabled)
1333 return 0;
1334
1335 if (!quota_root)
1336 return -EINVAL;
1337
1338 /*
1339 * create a tracking group for the subvol itself
1340 */
1341 ret = add_qgroup_item(trans, quota_root, objectid);
1342 if (ret)
1343 goto out;
1344
1345 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
1346 ret = update_qgroup_limit_item(trans, quota_root, objectid,
1347 inherit->lim.flags,
1348 inherit->lim.max_rfer,
1349 inherit->lim.max_excl,
1350 inherit->lim.rsv_rfer,
1351 inherit->lim.rsv_excl);
1352 if (ret)
1353 goto out;
1354 }
1355
1356 if (srcid) {
1357 struct btrfs_root *srcroot;
1358 struct btrfs_key srckey;
1359 int srcroot_level;
1360
1361 srckey.objectid = srcid;
1362 srckey.type = BTRFS_ROOT_ITEM_KEY;
1363 srckey.offset = (u64)-1;
1364 srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
1365 if (IS_ERR(srcroot)) {
1366 ret = PTR_ERR(srcroot);
1367 goto out;
1368 }
1369
1370 rcu_read_lock();
1371 srcroot_level = btrfs_header_level(srcroot->node);
1372 level_size = btrfs_level_size(srcroot, srcroot_level);
1373 rcu_read_unlock();
1374 }
1375
1376 /*
1377 * add qgroup to all inherited groups
1378 */
1379 if (inherit) {
1380 i_qgroups = (u64 *)(inherit + 1);
1381 for (i = 0; i < inherit->num_qgroups; ++i) {
1382 ret = add_qgroup_relation_item(trans, quota_root,
1383 objectid, *i_qgroups);
1384 if (ret)
1385 goto out;
1386 ret = add_qgroup_relation_item(trans, quota_root,
1387 *i_qgroups, objectid);
1388 if (ret)
1389 goto out;
1390 ++i_qgroups;
1391 }
1392 }
1393
1394
1395 spin_lock(&fs_info->qgroup_lock);
1396
1397 dstgroup = add_qgroup_rb(fs_info, objectid);
Dan Carpenter57a5a882012-07-30 02:15:43 -06001398 if (IS_ERR(dstgroup)) {
1399 ret = PTR_ERR(dstgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001400 goto unlock;
Dan Carpenter57a5a882012-07-30 02:15:43 -06001401 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001402
1403 if (srcid) {
1404 srcgroup = find_qgroup_rb(fs_info, srcid);
Chris Masonf3a87f12012-09-14 20:06:30 -04001405 if (!srcgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001406 goto unlock;
1407 dstgroup->rfer = srcgroup->rfer - level_size;
1408 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr - level_size;
1409 srcgroup->excl = level_size;
1410 srcgroup->excl_cmpr = level_size;
1411 qgroup_dirty(fs_info, dstgroup);
1412 qgroup_dirty(fs_info, srcgroup);
1413 }
1414
Chris Masonf3a87f12012-09-14 20:06:30 -04001415 if (!inherit)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001416 goto unlock;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001417
1418 i_qgroups = (u64 *)(inherit + 1);
1419 for (i = 0; i < inherit->num_qgroups; ++i) {
1420 ret = add_relation_rb(quota_root->fs_info, objectid,
1421 *i_qgroups);
1422 if (ret)
1423 goto unlock;
1424 ++i_qgroups;
1425 }
1426
1427 for (i = 0; i < inherit->num_ref_copies; ++i) {
1428 struct btrfs_qgroup *src;
1429 struct btrfs_qgroup *dst;
1430
1431 src = find_qgroup_rb(fs_info, i_qgroups[0]);
1432 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
1433
1434 if (!src || !dst) {
1435 ret = -EINVAL;
1436 goto unlock;
1437 }
1438
1439 dst->rfer = src->rfer - level_size;
1440 dst->rfer_cmpr = src->rfer_cmpr - level_size;
1441 i_qgroups += 2;
1442 }
1443 for (i = 0; i < inherit->num_excl_copies; ++i) {
1444 struct btrfs_qgroup *src;
1445 struct btrfs_qgroup *dst;
1446
1447 src = find_qgroup_rb(fs_info, i_qgroups[0]);
1448 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
1449
1450 if (!src || !dst) {
1451 ret = -EINVAL;
1452 goto unlock;
1453 }
1454
1455 dst->excl = src->excl + level_size;
1456 dst->excl_cmpr = src->excl_cmpr + level_size;
1457 i_qgroups += 2;
1458 }
1459
1460unlock:
1461 spin_unlock(&fs_info->qgroup_lock);
1462out:
1463 return ret;
1464}
1465
1466/*
1467 * reserve some space for a qgroup and all its parents. The reservation takes
1468 * place with start_transaction or dealloc_reserve, similar to ENOSPC
1469 * accounting. If not enough space is available, EDQUOT is returned.
1470 * We assume that the requested space is new for all qgroups.
1471 */
1472int btrfs_qgroup_reserve(struct btrfs_root *root, u64 num_bytes)
1473{
1474 struct btrfs_root *quota_root;
1475 struct btrfs_qgroup *qgroup;
1476 struct btrfs_fs_info *fs_info = root->fs_info;
1477 u64 ref_root = root->root_key.objectid;
1478 int ret = 0;
1479 struct ulist *ulist = NULL;
1480 struct ulist_node *unode;
1481 struct ulist_iterator uiter;
1482
1483 if (!is_fstree(ref_root))
1484 return 0;
1485
1486 if (num_bytes == 0)
1487 return 0;
1488
1489 spin_lock(&fs_info->qgroup_lock);
1490 quota_root = fs_info->quota_root;
1491 if (!quota_root)
1492 goto out;
1493
1494 qgroup = find_qgroup_rb(fs_info, ref_root);
1495 if (!qgroup)
1496 goto out;
1497
1498 /*
1499 * in a first step, we check all affected qgroups if any limits would
1500 * be exceeded
1501 */
1502 ulist = ulist_alloc(GFP_ATOMIC);
Tsutomu Itoh3d6b5c32012-09-06 00:18:10 -06001503 if (!ulist) {
1504 ret = -ENOMEM;
1505 goto out;
1506 }
Jan Schmidt995e01b2012-08-13 02:52:38 -06001507 ulist_add(ulist, qgroup->qgroupid, (uintptr_t)qgroup, GFP_ATOMIC);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001508 ULIST_ITER_INIT(&uiter);
1509 while ((unode = ulist_next(ulist, &uiter))) {
1510 struct btrfs_qgroup *qg;
1511 struct btrfs_qgroup_list *glist;
1512
Jan Schmidt995e01b2012-08-13 02:52:38 -06001513 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001514
1515 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
1516 qg->reserved + qg->rfer + num_bytes >
1517 qg->max_rfer)
1518 ret = -EDQUOT;
1519
1520 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
1521 qg->reserved + qg->excl + num_bytes >
1522 qg->max_excl)
1523 ret = -EDQUOT;
1524
1525 list_for_each_entry(glist, &qg->groups, next_group) {
1526 ulist_add(ulist, glist->group->qgroupid,
Jan Schmidt995e01b2012-08-13 02:52:38 -06001527 (uintptr_t)glist->group, GFP_ATOMIC);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001528 }
1529 }
1530 if (ret)
1531 goto out;
1532
1533 /*
1534 * no limits exceeded, now record the reservation into all qgroups
1535 */
1536 ULIST_ITER_INIT(&uiter);
1537 while ((unode = ulist_next(ulist, &uiter))) {
1538 struct btrfs_qgroup *qg;
1539
Jan Schmidt995e01b2012-08-13 02:52:38 -06001540 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001541
1542 qg->reserved += num_bytes;
1543 }
1544
1545out:
1546 spin_unlock(&fs_info->qgroup_lock);
1547 ulist_free(ulist);
1548
1549 return ret;
1550}
1551
1552void btrfs_qgroup_free(struct btrfs_root *root, u64 num_bytes)
1553{
1554 struct btrfs_root *quota_root;
1555 struct btrfs_qgroup *qgroup;
1556 struct btrfs_fs_info *fs_info = root->fs_info;
1557 struct ulist *ulist = NULL;
1558 struct ulist_node *unode;
1559 struct ulist_iterator uiter;
1560 u64 ref_root = root->root_key.objectid;
1561
1562 if (!is_fstree(ref_root))
1563 return;
1564
1565 if (num_bytes == 0)
1566 return;
1567
1568 spin_lock(&fs_info->qgroup_lock);
1569
1570 quota_root = fs_info->quota_root;
1571 if (!quota_root)
1572 goto out;
1573
1574 qgroup = find_qgroup_rb(fs_info, ref_root);
1575 if (!qgroup)
1576 goto out;
1577
1578 ulist = ulist_alloc(GFP_ATOMIC);
Tsutomu Itoh3d6b5c32012-09-06 00:18:10 -06001579 if (!ulist) {
1580 btrfs_std_error(fs_info, -ENOMEM);
1581 goto out;
1582 }
Jan Schmidt995e01b2012-08-13 02:52:38 -06001583 ulist_add(ulist, qgroup->qgroupid, (uintptr_t)qgroup, GFP_ATOMIC);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001584 ULIST_ITER_INIT(&uiter);
1585 while ((unode = ulist_next(ulist, &uiter))) {
1586 struct btrfs_qgroup *qg;
1587 struct btrfs_qgroup_list *glist;
1588
Jan Schmidt995e01b2012-08-13 02:52:38 -06001589 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001590
1591 qg->reserved -= num_bytes;
1592
1593 list_for_each_entry(glist, &qg->groups, next_group) {
1594 ulist_add(ulist, glist->group->qgroupid,
Jan Schmidt995e01b2012-08-13 02:52:38 -06001595 (uintptr_t)glist->group, GFP_ATOMIC);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001596 }
1597 }
1598
1599out:
1600 spin_unlock(&fs_info->qgroup_lock);
1601 ulist_free(ulist);
1602}
1603
1604void assert_qgroups_uptodate(struct btrfs_trans_handle *trans)
1605{
1606 if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq)
1607 return;
1608 printk(KERN_ERR "btrfs: qgroups not uptodate in trans handle %p: list is%s empty, seq is %llu\n",
1609 trans, list_empty(&trans->qgroup_ref_list) ? "" : " not",
1610 trans->delayed_ref_elem.seq);
1611 BUG();
1612}