blob: e089fc108483f3a65685df1c52aa2d3f1a7a51aa [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
Wang Shilong58400fc2013-04-07 10:50:17 +0000101/* must be called with qgroup_ioctl_lock held */
Arne Jansenbed92ea2012-06-28 18:03:02 +0200102static 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
Arne Jansenbed92ea2012-06-28 18:03:02 +0200423 while (!list_empty(&qgroup->groups)) {
424 list = list_first_entry(&qgroup->groups,
425 struct btrfs_qgroup_list,
426 next_group);
427 list_del(&list->next_group);
428 list_del(&list->next_member);
429 kfree(list);
430 }
431
432 while (!list_empty(&qgroup->members)) {
433 list = list_first_entry(&qgroup->members,
434 struct btrfs_qgroup_list,
435 next_member);
436 list_del(&list->next_group);
437 list_del(&list->next_member);
438 kfree(list);
439 }
440 kfree(qgroup);
441 }
442}
443
444static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
445 struct btrfs_root *quota_root,
446 u64 src, u64 dst)
447{
448 int ret;
449 struct btrfs_path *path;
450 struct btrfs_key key;
451
452 path = btrfs_alloc_path();
453 if (!path)
454 return -ENOMEM;
455
456 key.objectid = src;
457 key.type = BTRFS_QGROUP_RELATION_KEY;
458 key.offset = dst;
459
460 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
461
462 btrfs_mark_buffer_dirty(path->nodes[0]);
463
464 btrfs_free_path(path);
465 return ret;
466}
467
468static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
469 struct btrfs_root *quota_root,
470 u64 src, u64 dst)
471{
472 int ret;
473 struct btrfs_path *path;
474 struct btrfs_key key;
475
476 path = btrfs_alloc_path();
477 if (!path)
478 return -ENOMEM;
479
480 key.objectid = src;
481 key.type = BTRFS_QGROUP_RELATION_KEY;
482 key.offset = dst;
483
484 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
485 if (ret < 0)
486 goto out;
487
488 if (ret > 0) {
489 ret = -ENOENT;
490 goto out;
491 }
492
493 ret = btrfs_del_item(trans, quota_root, path);
494out:
495 btrfs_free_path(path);
496 return ret;
497}
498
499static int add_qgroup_item(struct btrfs_trans_handle *trans,
500 struct btrfs_root *quota_root, u64 qgroupid)
501{
502 int ret;
503 struct btrfs_path *path;
504 struct btrfs_qgroup_info_item *qgroup_info;
505 struct btrfs_qgroup_limit_item *qgroup_limit;
506 struct extent_buffer *leaf;
507 struct btrfs_key key;
508
509 path = btrfs_alloc_path();
510 if (!path)
511 return -ENOMEM;
512
513 key.objectid = 0;
514 key.type = BTRFS_QGROUP_INFO_KEY;
515 key.offset = qgroupid;
516
517 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
518 sizeof(*qgroup_info));
519 if (ret)
520 goto out;
521
522 leaf = path->nodes[0];
523 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
524 struct btrfs_qgroup_info_item);
525 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
526 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
527 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
528 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
529 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
530
531 btrfs_mark_buffer_dirty(leaf);
532
533 btrfs_release_path(path);
534
535 key.type = BTRFS_QGROUP_LIMIT_KEY;
536 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
537 sizeof(*qgroup_limit));
538 if (ret)
539 goto out;
540
541 leaf = path->nodes[0];
542 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
543 struct btrfs_qgroup_limit_item);
544 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
545 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
546 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
547 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
548 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
549
550 btrfs_mark_buffer_dirty(leaf);
551
552 ret = 0;
553out:
554 btrfs_free_path(path);
555 return ret;
556}
557
558static int del_qgroup_item(struct btrfs_trans_handle *trans,
559 struct btrfs_root *quota_root, u64 qgroupid)
560{
561 int ret;
562 struct btrfs_path *path;
563 struct btrfs_key key;
564
565 path = btrfs_alloc_path();
566 if (!path)
567 return -ENOMEM;
568
569 key.objectid = 0;
570 key.type = BTRFS_QGROUP_INFO_KEY;
571 key.offset = qgroupid;
572 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
573 if (ret < 0)
574 goto out;
575
576 if (ret > 0) {
577 ret = -ENOENT;
578 goto out;
579 }
580
581 ret = btrfs_del_item(trans, quota_root, path);
582 if (ret)
583 goto out;
584
585 btrfs_release_path(path);
586
587 key.type = BTRFS_QGROUP_LIMIT_KEY;
588 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
589 if (ret < 0)
590 goto out;
591
592 if (ret > 0) {
593 ret = -ENOENT;
594 goto out;
595 }
596
597 ret = btrfs_del_item(trans, quota_root, path);
598
599out:
600 btrfs_free_path(path);
601 return ret;
602}
603
604static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
605 struct btrfs_root *root, u64 qgroupid,
606 u64 flags, u64 max_rfer, u64 max_excl,
607 u64 rsv_rfer, u64 rsv_excl)
608{
609 struct btrfs_path *path;
610 struct btrfs_key key;
611 struct extent_buffer *l;
612 struct btrfs_qgroup_limit_item *qgroup_limit;
613 int ret;
614 int slot;
615
616 key.objectid = 0;
617 key.type = BTRFS_QGROUP_LIMIT_KEY;
618 key.offset = qgroupid;
619
620 path = btrfs_alloc_path();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000621 if (!path)
622 return -ENOMEM;
623
Arne Jansenbed92ea2012-06-28 18:03:02 +0200624 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();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000664 if (!path)
665 return -ENOMEM;
666
Arne Jansenbed92ea2012-06-28 18:03:02 +0200667 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
668 if (ret > 0)
669 ret = -ENOENT;
670
671 if (ret)
672 goto out;
673
674 l = path->nodes[0];
675 slot = path->slots[0];
676 qgroup_info = btrfs_item_ptr(l, path->slots[0],
677 struct btrfs_qgroup_info_item);
678 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
679 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
680 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
681 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
682 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
683
684 btrfs_mark_buffer_dirty(l);
685
686out:
687 btrfs_free_path(path);
688 return ret;
689}
690
691static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
692 struct btrfs_fs_info *fs_info,
693 struct btrfs_root *root)
694{
695 struct btrfs_path *path;
696 struct btrfs_key key;
697 struct extent_buffer *l;
698 struct btrfs_qgroup_status_item *ptr;
699 int ret;
700 int slot;
701
702 key.objectid = 0;
703 key.type = BTRFS_QGROUP_STATUS_KEY;
704 key.offset = 0;
705
706 path = btrfs_alloc_path();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000707 if (!path)
708 return -ENOMEM;
709
Arne Jansenbed92ea2012-06-28 18:03:02 +0200710 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
711 if (ret > 0)
712 ret = -ENOENT;
713
714 if (ret)
715 goto out;
716
717 l = path->nodes[0];
718 slot = path->slots[0];
719 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
720 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
721 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
722 /* XXX scan */
723
724 btrfs_mark_buffer_dirty(l);
725
726out:
727 btrfs_free_path(path);
728 return ret;
729}
730
731/*
732 * called with qgroup_lock held
733 */
734static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
735 struct btrfs_root *root)
736{
737 struct btrfs_path *path;
738 struct btrfs_key key;
Wang Shilong06b3a862013-02-27 11:16:57 +0000739 struct extent_buffer *leaf = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200740 int ret;
Wang Shilong06b3a862013-02-27 11:16:57 +0000741 int nr = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200742
Arne Jansenbed92ea2012-06-28 18:03:02 +0200743 path = btrfs_alloc_path();
744 if (!path)
745 return -ENOMEM;
746
Wang Shilong06b3a862013-02-27 11:16:57 +0000747 path->leave_spinning = 1;
748
749 key.objectid = 0;
750 key.offset = 0;
751 key.type = 0;
752
Arne Jansenbed92ea2012-06-28 18:03:02 +0200753 while (1) {
Arne Jansenbed92ea2012-06-28 18:03:02 +0200754 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Wang Shilong06b3a862013-02-27 11:16:57 +0000755 if (ret < 0)
756 goto out;
757 leaf = path->nodes[0];
758 nr = btrfs_header_nritems(leaf);
759 if (!nr)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200760 break;
Wang Shilong06b3a862013-02-27 11:16:57 +0000761 /*
762 * delete the leaf one by one
763 * since the whole tree is going
764 * to be deleted.
765 */
766 path->slots[0] = 0;
767 ret = btrfs_del_items(trans, root, path, 0, nr);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200768 if (ret)
769 goto out;
Wang Shilong06b3a862013-02-27 11:16:57 +0000770
Arne Jansenbed92ea2012-06-28 18:03:02 +0200771 btrfs_release_path(path);
772 }
773 ret = 0;
774out:
775 root->fs_info->pending_quota_state = 0;
776 btrfs_free_path(path);
777 return ret;
778}
779
780int btrfs_quota_enable(struct btrfs_trans_handle *trans,
781 struct btrfs_fs_info *fs_info)
782{
783 struct btrfs_root *quota_root;
Wang Shilong7708f022013-04-07 10:24:57 +0000784 struct btrfs_root *tree_root = fs_info->tree_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200785 struct btrfs_path *path = NULL;
786 struct btrfs_qgroup_status_item *ptr;
787 struct extent_buffer *leaf;
788 struct btrfs_key key;
Wang Shilong7708f022013-04-07 10:24:57 +0000789 struct btrfs_key found_key;
790 struct btrfs_qgroup *qgroup = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200791 int ret = 0;
Wang Shilong7708f022013-04-07 10:24:57 +0000792 int slot;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200793
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000794 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200795 if (fs_info->quota_root) {
796 fs_info->pending_quota_state = 1;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200797 goto out;
798 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200799
800 /*
801 * initially create the quota tree
802 */
803 quota_root = btrfs_create_tree(trans, fs_info,
804 BTRFS_QUOTA_TREE_OBJECTID);
805 if (IS_ERR(quota_root)) {
806 ret = PTR_ERR(quota_root);
807 goto out;
808 }
809
810 path = btrfs_alloc_path();
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000811 if (!path) {
812 ret = -ENOMEM;
813 goto out_free_root;
814 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200815
816 key.objectid = 0;
817 key.type = BTRFS_QGROUP_STATUS_KEY;
818 key.offset = 0;
819
820 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
821 sizeof(*ptr));
822 if (ret)
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000823 goto out_free_path;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200824
825 leaf = path->nodes[0];
826 ptr = btrfs_item_ptr(leaf, path->slots[0],
827 struct btrfs_qgroup_status_item);
828 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
829 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
830 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
831 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
832 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
833 btrfs_set_qgroup_status_scan(leaf, ptr, 0);
834
835 btrfs_mark_buffer_dirty(leaf);
836
Wang Shilong7708f022013-04-07 10:24:57 +0000837 key.objectid = 0;
838 key.type = BTRFS_ROOT_REF_KEY;
839 key.offset = 0;
840
841 btrfs_release_path(path);
842 ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
843 if (ret > 0)
844 goto out_add_root;
845 if (ret < 0)
846 goto out_free_path;
847
848
849 while (1) {
850 slot = path->slots[0];
851 leaf = path->nodes[0];
852 btrfs_item_key_to_cpu(leaf, &found_key, slot);
853
854 if (found_key.type == BTRFS_ROOT_REF_KEY) {
855 ret = add_qgroup_item(trans, quota_root,
856 found_key.offset);
857 if (ret)
858 goto out_free_path;
859
Wang Shilong7708f022013-04-07 10:24:57 +0000860 qgroup = add_qgroup_rb(fs_info, found_key.offset);
861 if (IS_ERR(qgroup)) {
Wang Shilong7708f022013-04-07 10:24:57 +0000862 ret = PTR_ERR(qgroup);
863 goto out_free_path;
864 }
Wang Shilong7708f022013-04-07 10:24:57 +0000865 }
866 ret = btrfs_next_item(tree_root, path);
867 if (ret < 0)
868 goto out_free_path;
869 if (ret)
870 break;
871 }
872
873out_add_root:
874 btrfs_release_path(path);
875 ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
876 if (ret)
877 goto out_free_path;
878
Wang Shilong7708f022013-04-07 10:24:57 +0000879 qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
880 if (IS_ERR(qgroup)) {
Wang Shilong7708f022013-04-07 10:24:57 +0000881 ret = PTR_ERR(qgroup);
882 goto out_free_path;
883 }
Wang Shilong58400fc2013-04-07 10:50:17 +0000884 spin_lock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200885 fs_info->quota_root = quota_root;
886 fs_info->pending_quota_state = 1;
887 spin_unlock(&fs_info->qgroup_lock);
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000888out_free_path:
Arne Jansenbed92ea2012-06-28 18:03:02 +0200889 btrfs_free_path(path);
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000890out_free_root:
891 if (ret) {
892 free_extent_buffer(quota_root->node);
893 free_extent_buffer(quota_root->commit_root);
894 kfree(quota_root);
895 }
896out:
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000897 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200898 return ret;
899}
900
901int btrfs_quota_disable(struct btrfs_trans_handle *trans,
902 struct btrfs_fs_info *fs_info)
903{
904 struct btrfs_root *tree_root = fs_info->tree_root;
905 struct btrfs_root *quota_root;
906 int ret = 0;
907
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000908 mutex_lock(&fs_info->qgroup_ioctl_lock);
Wang Shilong58400fc2013-04-07 10:50:17 +0000909 if (!fs_info->quota_root)
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000910 goto out;
Wang Shilong58400fc2013-04-07 10:50:17 +0000911 spin_lock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200912 fs_info->quota_enabled = 0;
913 fs_info->pending_quota_state = 0;
914 quota_root = fs_info->quota_root;
915 fs_info->quota_root = NULL;
916 btrfs_free_qgroup_config(fs_info);
917 spin_unlock(&fs_info->qgroup_lock);
918
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000919 if (!quota_root) {
920 ret = -EINVAL;
921 goto out;
922 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200923
924 ret = btrfs_clean_quota_tree(trans, quota_root);
925 if (ret)
926 goto out;
927
928 ret = btrfs_del_root(trans, tree_root, &quota_root->root_key);
929 if (ret)
930 goto out;
931
932 list_del(&quota_root->dirty_list);
933
934 btrfs_tree_lock(quota_root->node);
935 clean_tree_block(trans, tree_root, quota_root->node);
936 btrfs_tree_unlock(quota_root->node);
937 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
938
939 free_extent_buffer(quota_root->node);
940 free_extent_buffer(quota_root->commit_root);
941 kfree(quota_root);
942out:
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000943 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200944 return ret;
945}
946
947int btrfs_quota_rescan(struct btrfs_fs_info *fs_info)
948{
949 /* FIXME */
950 return 0;
951}
952
953int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
954 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
955{
956 struct btrfs_root *quota_root;
Wang Shilongb7fef4f2013-04-07 10:50:18 +0000957 struct btrfs_qgroup *parent;
958 struct btrfs_qgroup *member;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200959 int ret = 0;
960
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000961 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200962 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000963 if (!quota_root) {
964 ret = -EINVAL;
965 goto out;
966 }
Wang Shilongb7fef4f2013-04-07 10:50:18 +0000967 member = find_qgroup_rb(fs_info, src);
968 parent = find_qgroup_rb(fs_info, dst);
969 if (!member || !parent) {
970 ret = -EINVAL;
971 goto out;
972 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200973
974 ret = add_qgroup_relation_item(trans, quota_root, src, dst);
975 if (ret)
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000976 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200977
978 ret = add_qgroup_relation_item(trans, quota_root, dst, src);
979 if (ret) {
980 del_qgroup_relation_item(trans, quota_root, src, dst);
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000981 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200982 }
983
984 spin_lock(&fs_info->qgroup_lock);
985 ret = add_relation_rb(quota_root->fs_info, src, dst);
986 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000987out:
988 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200989 return ret;
990}
991
992int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
993 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
994{
995 struct btrfs_root *quota_root;
996 int ret = 0;
997 int err;
998
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000999 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001000 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001001 if (!quota_root) {
1002 ret = -EINVAL;
1003 goto out;
1004 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001005
1006 ret = del_qgroup_relation_item(trans, quota_root, src, dst);
1007 err = del_qgroup_relation_item(trans, quota_root, dst, src);
1008 if (err && !ret)
1009 ret = err;
1010
1011 spin_lock(&fs_info->qgroup_lock);
1012 del_relation_rb(fs_info, src, dst);
1013
1014 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001015out:
1016 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001017 return ret;
1018}
1019
1020int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
1021 struct btrfs_fs_info *fs_info, u64 qgroupid, char *name)
1022{
1023 struct btrfs_root *quota_root;
1024 struct btrfs_qgroup *qgroup;
1025 int ret = 0;
1026
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001027 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001028 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001029 if (!quota_root) {
1030 ret = -EINVAL;
1031 goto out;
1032 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001033
1034 ret = add_qgroup_item(trans, quota_root, qgroupid);
1035
1036 spin_lock(&fs_info->qgroup_lock);
1037 qgroup = add_qgroup_rb(fs_info, qgroupid);
1038 spin_unlock(&fs_info->qgroup_lock);
1039
1040 if (IS_ERR(qgroup))
1041 ret = PTR_ERR(qgroup);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001042out:
1043 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001044 return ret;
1045}
1046
1047int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
1048 struct btrfs_fs_info *fs_info, u64 qgroupid)
1049{
1050 struct btrfs_root *quota_root;
Arne Jansen2cf68702013-01-17 01:22:09 -07001051 struct btrfs_qgroup *qgroup;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001052 int ret = 0;
1053
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001054 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001055 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001056 if (!quota_root) {
1057 ret = -EINVAL;
1058 goto out;
1059 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001060
Arne Jansen2cf68702013-01-17 01:22:09 -07001061 /* check if there are no relations to this qgroup */
Arne Jansen2cf68702013-01-17 01:22:09 -07001062 qgroup = find_qgroup_rb(fs_info, qgroupid);
1063 if (qgroup) {
1064 if (!list_empty(&qgroup->groups) || !list_empty(&qgroup->members)) {
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001065 ret = -EBUSY;
1066 goto out;
Arne Jansen2cf68702013-01-17 01:22:09 -07001067 }
1068 }
Arne Jansen2cf68702013-01-17 01:22:09 -07001069
Arne Jansenbed92ea2012-06-28 18:03:02 +02001070 ret = del_qgroup_item(trans, quota_root, qgroupid);
1071
1072 spin_lock(&fs_info->qgroup_lock);
1073 del_qgroup_rb(quota_root->fs_info, qgroupid);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001074 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001075out:
1076 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001077 return ret;
1078}
1079
1080int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
1081 struct btrfs_fs_info *fs_info, u64 qgroupid,
1082 struct btrfs_qgroup_limit *limit)
1083{
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001084 struct btrfs_root *quota_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001085 struct btrfs_qgroup *qgroup;
1086 int ret = 0;
1087
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001088 mutex_lock(&fs_info->qgroup_ioctl_lock);
1089 quota_root = fs_info->quota_root;
1090 if (!quota_root) {
1091 ret = -EINVAL;
1092 goto out;
1093 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001094
Wang Shilongddb47af2013-04-07 10:50:20 +00001095 qgroup = find_qgroup_rb(fs_info, qgroupid);
1096 if (!qgroup) {
1097 ret = -ENOENT;
1098 goto out;
1099 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001100 ret = update_qgroup_limit_item(trans, quota_root, qgroupid,
1101 limit->flags, limit->max_rfer,
1102 limit->max_excl, limit->rsv_rfer,
1103 limit->rsv_excl);
1104 if (ret) {
1105 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1106 printk(KERN_INFO "unable to update quota limit for %llu\n",
1107 (unsigned long long)qgroupid);
1108 }
1109
Wang Shilong58400fc2013-04-07 10:50:17 +00001110 spin_lock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001111 qgroup->lim_flags = limit->flags;
1112 qgroup->max_rfer = limit->max_rfer;
1113 qgroup->max_excl = limit->max_excl;
1114 qgroup->rsv_rfer = limit->rsv_rfer;
1115 qgroup->rsv_excl = limit->rsv_excl;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001116 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001117out:
1118 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001119 return ret;
1120}
1121
1122static void qgroup_dirty(struct btrfs_fs_info *fs_info,
1123 struct btrfs_qgroup *qgroup)
1124{
1125 if (list_empty(&qgroup->dirty))
1126 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
1127}
1128
1129/*
1130 * btrfs_qgroup_record_ref is called when the ref is added or deleted. it puts
1131 * the modification into a list that's later used by btrfs_end_transaction to
1132 * pass the recorded modifications on to btrfs_qgroup_account_ref.
1133 */
1134int btrfs_qgroup_record_ref(struct btrfs_trans_handle *trans,
1135 struct btrfs_delayed_ref_node *node,
1136 struct btrfs_delayed_extent_op *extent_op)
1137{
1138 struct qgroup_update *u;
1139
1140 BUG_ON(!trans->delayed_ref_elem.seq);
1141 u = kmalloc(sizeof(*u), GFP_NOFS);
1142 if (!u)
1143 return -ENOMEM;
1144
1145 u->node = node;
1146 u->extent_op = extent_op;
1147 list_add_tail(&u->list, &trans->qgroup_ref_list);
1148
1149 return 0;
1150}
1151
1152/*
1153 * btrfs_qgroup_account_ref is called for every ref that is added to or deleted
1154 * from the fs. First, all roots referencing the extent are searched, and
1155 * then the space is accounted accordingly to the different roots. The
1156 * accounting algorithm works in 3 steps documented inline.
1157 */
1158int btrfs_qgroup_account_ref(struct btrfs_trans_handle *trans,
1159 struct btrfs_fs_info *fs_info,
1160 struct btrfs_delayed_ref_node *node,
1161 struct btrfs_delayed_extent_op *extent_op)
1162{
1163 struct btrfs_key ins;
1164 struct btrfs_root *quota_root;
1165 u64 ref_root;
1166 struct btrfs_qgroup *qgroup;
1167 struct ulist_node *unode;
1168 struct ulist *roots = NULL;
1169 struct ulist *tmp = NULL;
1170 struct ulist_iterator uiter;
1171 u64 seq;
1172 int ret = 0;
1173 int sgn;
1174
1175 if (!fs_info->quota_enabled)
1176 return 0;
1177
1178 BUG_ON(!fs_info->quota_root);
1179
1180 ins.objectid = node->bytenr;
1181 ins.offset = node->num_bytes;
1182 ins.type = BTRFS_EXTENT_ITEM_KEY;
1183
1184 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1185 node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
1186 struct btrfs_delayed_tree_ref *ref;
1187 ref = btrfs_delayed_node_to_tree_ref(node);
1188 ref_root = ref->root;
1189 } else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1190 node->type == BTRFS_SHARED_DATA_REF_KEY) {
1191 struct btrfs_delayed_data_ref *ref;
1192 ref = btrfs_delayed_node_to_data_ref(node);
1193 ref_root = ref->root;
1194 } else {
1195 BUG();
1196 }
1197
1198 if (!is_fstree(ref_root)) {
1199 /*
1200 * non-fs-trees are not being accounted
1201 */
1202 return 0;
1203 }
1204
1205 switch (node->action) {
1206 case BTRFS_ADD_DELAYED_REF:
1207 case BTRFS_ADD_DELAYED_EXTENT:
1208 sgn = 1;
1209 break;
1210 case BTRFS_DROP_DELAYED_REF:
1211 sgn = -1;
1212 break;
1213 case BTRFS_UPDATE_DELAYED_HEAD:
1214 return 0;
1215 default:
1216 BUG();
1217 }
1218
1219 /*
1220 * the delayed ref sequence number we pass depends on the direction of
1221 * the operation. for add operations, we pass (node->seq - 1) to skip
1222 * the delayed ref's current sequence number, because we need the state
1223 * of the tree before the add operation. for delete operations, we pass
1224 * (node->seq) to include the delayed ref's current sequence number,
1225 * because we need the state of the tree after the delete operation.
1226 */
1227 ret = btrfs_find_all_roots(trans, fs_info, node->bytenr,
1228 sgn > 0 ? node->seq - 1 : node->seq, &roots);
1229 if (ret < 0)
Wang Shilonga7975022013-03-25 11:08:23 +00001230 return ret;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001231
1232 spin_lock(&fs_info->qgroup_lock);
1233 quota_root = fs_info->quota_root;
1234 if (!quota_root)
1235 goto unlock;
1236
1237 qgroup = find_qgroup_rb(fs_info, ref_root);
1238 if (!qgroup)
1239 goto unlock;
1240
1241 /*
1242 * step 1: for each old ref, visit all nodes once and inc refcnt
1243 */
1244 tmp = ulist_alloc(GFP_ATOMIC);
1245 if (!tmp) {
1246 ret = -ENOMEM;
1247 goto unlock;
1248 }
1249 seq = fs_info->qgroup_seq;
1250 fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */
1251
1252 ULIST_ITER_INIT(&uiter);
1253 while ((unode = ulist_next(roots, &uiter))) {
1254 struct ulist_node *tmp_unode;
1255 struct ulist_iterator tmp_uiter;
1256 struct btrfs_qgroup *qg;
1257
1258 qg = find_qgroup_rb(fs_info, unode->val);
1259 if (!qg)
1260 continue;
1261
1262 ulist_reinit(tmp);
1263 /* XXX id not needed */
Jan Schmidt995e01b2012-08-13 02:52:38 -06001264 ulist_add(tmp, qg->qgroupid, (u64)(uintptr_t)qg, GFP_ATOMIC);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001265 ULIST_ITER_INIT(&tmp_uiter);
1266 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1267 struct btrfs_qgroup_list *glist;
1268
Jan Schmidt995e01b2012-08-13 02:52:38 -06001269 qg = (struct btrfs_qgroup *)(uintptr_t)tmp_unode->aux;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001270 if (qg->refcnt < seq)
1271 qg->refcnt = seq + 1;
1272 else
1273 ++qg->refcnt;
1274
1275 list_for_each_entry(glist, &qg->groups, next_group) {
1276 ulist_add(tmp, glist->group->qgroupid,
Jan Schmidt995e01b2012-08-13 02:52:38 -06001277 (u64)(uintptr_t)glist->group,
Arne Jansenbed92ea2012-06-28 18:03:02 +02001278 GFP_ATOMIC);
1279 }
1280 }
1281 }
1282
1283 /*
1284 * step 2: walk from the new root
1285 */
1286 ulist_reinit(tmp);
Jan Schmidt995e01b2012-08-13 02:52:38 -06001287 ulist_add(tmp, qgroup->qgroupid, (uintptr_t)qgroup, GFP_ATOMIC);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001288 ULIST_ITER_INIT(&uiter);
1289 while ((unode = ulist_next(tmp, &uiter))) {
1290 struct btrfs_qgroup *qg;
1291 struct btrfs_qgroup_list *glist;
1292
Jan Schmidt995e01b2012-08-13 02:52:38 -06001293 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001294 if (qg->refcnt < seq) {
1295 /* not visited by step 1 */
1296 qg->rfer += sgn * node->num_bytes;
1297 qg->rfer_cmpr += sgn * node->num_bytes;
1298 if (roots->nnodes == 0) {
1299 qg->excl += sgn * node->num_bytes;
1300 qg->excl_cmpr += sgn * node->num_bytes;
1301 }
1302 qgroup_dirty(fs_info, qg);
1303 }
1304 WARN_ON(qg->tag >= seq);
1305 qg->tag = seq;
1306
1307 list_for_each_entry(glist, &qg->groups, next_group) {
1308 ulist_add(tmp, glist->group->qgroupid,
Jan Schmidt995e01b2012-08-13 02:52:38 -06001309 (uintptr_t)glist->group, GFP_ATOMIC);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001310 }
1311 }
1312
1313 /*
1314 * step 3: walk again from old refs
1315 */
1316 ULIST_ITER_INIT(&uiter);
1317 while ((unode = ulist_next(roots, &uiter))) {
1318 struct btrfs_qgroup *qg;
1319 struct ulist_node *tmp_unode;
1320 struct ulist_iterator tmp_uiter;
1321
1322 qg = find_qgroup_rb(fs_info, unode->val);
1323 if (!qg)
1324 continue;
1325
1326 ulist_reinit(tmp);
Jan Schmidt995e01b2012-08-13 02:52:38 -06001327 ulist_add(tmp, qg->qgroupid, (uintptr_t)qg, GFP_ATOMIC);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001328 ULIST_ITER_INIT(&tmp_uiter);
1329 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1330 struct btrfs_qgroup_list *glist;
1331
Jan Schmidt995e01b2012-08-13 02:52:38 -06001332 qg = (struct btrfs_qgroup *)(uintptr_t)tmp_unode->aux;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001333 if (qg->tag == seq)
1334 continue;
1335
1336 if (qg->refcnt - seq == roots->nnodes) {
1337 qg->excl -= sgn * node->num_bytes;
1338 qg->excl_cmpr -= sgn * node->num_bytes;
1339 qgroup_dirty(fs_info, qg);
1340 }
1341
1342 list_for_each_entry(glist, &qg->groups, next_group) {
1343 ulist_add(tmp, glist->group->qgroupid,
Jan Schmidt995e01b2012-08-13 02:52:38 -06001344 (uintptr_t)glist->group,
Arne Jansenbed92ea2012-06-28 18:03:02 +02001345 GFP_ATOMIC);
1346 }
1347 }
1348 }
1349 ret = 0;
1350unlock:
1351 spin_unlock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001352 ulist_free(roots);
1353 ulist_free(tmp);
1354
1355 return ret;
1356}
1357
1358/*
1359 * called from commit_transaction. Writes all changed qgroups to disk.
1360 */
1361int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
1362 struct btrfs_fs_info *fs_info)
1363{
1364 struct btrfs_root *quota_root = fs_info->quota_root;
1365 int ret = 0;
1366
1367 if (!quota_root)
1368 goto out;
1369
1370 fs_info->quota_enabled = fs_info->pending_quota_state;
1371
1372 spin_lock(&fs_info->qgroup_lock);
1373 while (!list_empty(&fs_info->dirty_qgroups)) {
1374 struct btrfs_qgroup *qgroup;
1375 qgroup = list_first_entry(&fs_info->dirty_qgroups,
1376 struct btrfs_qgroup, dirty);
1377 list_del_init(&qgroup->dirty);
1378 spin_unlock(&fs_info->qgroup_lock);
1379 ret = update_qgroup_info_item(trans, quota_root, qgroup);
1380 if (ret)
1381 fs_info->qgroup_flags |=
1382 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1383 spin_lock(&fs_info->qgroup_lock);
1384 }
1385 if (fs_info->quota_enabled)
1386 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
1387 else
1388 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
1389 spin_unlock(&fs_info->qgroup_lock);
1390
1391 ret = update_qgroup_status_item(trans, fs_info, quota_root);
1392 if (ret)
1393 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1394
1395out:
1396
1397 return ret;
1398}
1399
1400/*
1401 * copy the acounting information between qgroups. This is necessary when a
1402 * snapshot or a subvolume is created
1403 */
1404int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
1405 struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
1406 struct btrfs_qgroup_inherit *inherit)
1407{
1408 int ret = 0;
1409 int i;
1410 u64 *i_qgroups;
1411 struct btrfs_root *quota_root = fs_info->quota_root;
1412 struct btrfs_qgroup *srcgroup;
1413 struct btrfs_qgroup *dstgroup;
1414 u32 level_size = 0;
Wang Shilong3f5e2d32013-04-07 10:50:19 +00001415 u64 nums;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001416
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001417 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001418 if (!fs_info->quota_enabled)
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001419 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001420
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001421 if (!quota_root) {
1422 ret = -EINVAL;
1423 goto out;
1424 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001425
Wang Shilong3f5e2d32013-04-07 10:50:19 +00001426 if (inherit) {
1427 i_qgroups = (u64 *)(inherit + 1);
1428 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
1429 2 * inherit->num_excl_copies;
1430 for (i = 0; i < nums; ++i) {
1431 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
1432 if (!srcgroup) {
1433 ret = -EINVAL;
1434 goto out;
1435 }
1436 ++i_qgroups;
1437 }
1438 }
1439
Arne Jansenbed92ea2012-06-28 18:03:02 +02001440 /*
1441 * create a tracking group for the subvol itself
1442 */
1443 ret = add_qgroup_item(trans, quota_root, objectid);
1444 if (ret)
1445 goto out;
1446
1447 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
1448 ret = update_qgroup_limit_item(trans, quota_root, objectid,
1449 inherit->lim.flags,
1450 inherit->lim.max_rfer,
1451 inherit->lim.max_excl,
1452 inherit->lim.rsv_rfer,
1453 inherit->lim.rsv_excl);
1454 if (ret)
1455 goto out;
1456 }
1457
1458 if (srcid) {
1459 struct btrfs_root *srcroot;
1460 struct btrfs_key srckey;
1461 int srcroot_level;
1462
1463 srckey.objectid = srcid;
1464 srckey.type = BTRFS_ROOT_ITEM_KEY;
1465 srckey.offset = (u64)-1;
1466 srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
1467 if (IS_ERR(srcroot)) {
1468 ret = PTR_ERR(srcroot);
1469 goto out;
1470 }
1471
1472 rcu_read_lock();
1473 srcroot_level = btrfs_header_level(srcroot->node);
1474 level_size = btrfs_level_size(srcroot, srcroot_level);
1475 rcu_read_unlock();
1476 }
1477
1478 /*
1479 * add qgroup to all inherited groups
1480 */
1481 if (inherit) {
1482 i_qgroups = (u64 *)(inherit + 1);
1483 for (i = 0; i < inherit->num_qgroups; ++i) {
1484 ret = add_qgroup_relation_item(trans, quota_root,
1485 objectid, *i_qgroups);
1486 if (ret)
1487 goto out;
1488 ret = add_qgroup_relation_item(trans, quota_root,
1489 *i_qgroups, objectid);
1490 if (ret)
1491 goto out;
1492 ++i_qgroups;
1493 }
1494 }
1495
1496
1497 spin_lock(&fs_info->qgroup_lock);
1498
1499 dstgroup = add_qgroup_rb(fs_info, objectid);
Dan Carpenter57a5a882012-07-30 02:15:43 -06001500 if (IS_ERR(dstgroup)) {
1501 ret = PTR_ERR(dstgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001502 goto unlock;
Dan Carpenter57a5a882012-07-30 02:15:43 -06001503 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001504
1505 if (srcid) {
1506 srcgroup = find_qgroup_rb(fs_info, srcid);
Chris Masonf3a87f12012-09-14 20:06:30 -04001507 if (!srcgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001508 goto unlock;
1509 dstgroup->rfer = srcgroup->rfer - level_size;
1510 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr - level_size;
1511 srcgroup->excl = level_size;
1512 srcgroup->excl_cmpr = level_size;
1513 qgroup_dirty(fs_info, dstgroup);
1514 qgroup_dirty(fs_info, srcgroup);
1515 }
1516
Chris Masonf3a87f12012-09-14 20:06:30 -04001517 if (!inherit)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001518 goto unlock;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001519
1520 i_qgroups = (u64 *)(inherit + 1);
1521 for (i = 0; i < inherit->num_qgroups; ++i) {
1522 ret = add_relation_rb(quota_root->fs_info, objectid,
1523 *i_qgroups);
1524 if (ret)
1525 goto unlock;
1526 ++i_qgroups;
1527 }
1528
1529 for (i = 0; i < inherit->num_ref_copies; ++i) {
1530 struct btrfs_qgroup *src;
1531 struct btrfs_qgroup *dst;
1532
1533 src = find_qgroup_rb(fs_info, i_qgroups[0]);
1534 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
1535
1536 if (!src || !dst) {
1537 ret = -EINVAL;
1538 goto unlock;
1539 }
1540
1541 dst->rfer = src->rfer - level_size;
1542 dst->rfer_cmpr = src->rfer_cmpr - level_size;
1543 i_qgroups += 2;
1544 }
1545 for (i = 0; i < inherit->num_excl_copies; ++i) {
1546 struct btrfs_qgroup *src;
1547 struct btrfs_qgroup *dst;
1548
1549 src = find_qgroup_rb(fs_info, i_qgroups[0]);
1550 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
1551
1552 if (!src || !dst) {
1553 ret = -EINVAL;
1554 goto unlock;
1555 }
1556
1557 dst->excl = src->excl + level_size;
1558 dst->excl_cmpr = src->excl_cmpr + level_size;
1559 i_qgroups += 2;
1560 }
1561
1562unlock:
1563 spin_unlock(&fs_info->qgroup_lock);
1564out:
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001565 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001566 return ret;
1567}
1568
1569/*
1570 * reserve some space for a qgroup and all its parents. The reservation takes
1571 * place with start_transaction or dealloc_reserve, similar to ENOSPC
1572 * accounting. If not enough space is available, EDQUOT is returned.
1573 * We assume that the requested space is new for all qgroups.
1574 */
1575int btrfs_qgroup_reserve(struct btrfs_root *root, u64 num_bytes)
1576{
1577 struct btrfs_root *quota_root;
1578 struct btrfs_qgroup *qgroup;
1579 struct btrfs_fs_info *fs_info = root->fs_info;
1580 u64 ref_root = root->root_key.objectid;
1581 int ret = 0;
1582 struct ulist *ulist = NULL;
1583 struct ulist_node *unode;
1584 struct ulist_iterator uiter;
1585
1586 if (!is_fstree(ref_root))
1587 return 0;
1588
1589 if (num_bytes == 0)
1590 return 0;
1591
1592 spin_lock(&fs_info->qgroup_lock);
1593 quota_root = fs_info->quota_root;
1594 if (!quota_root)
1595 goto out;
1596
1597 qgroup = find_qgroup_rb(fs_info, ref_root);
1598 if (!qgroup)
1599 goto out;
1600
1601 /*
1602 * in a first step, we check all affected qgroups if any limits would
1603 * be exceeded
1604 */
1605 ulist = ulist_alloc(GFP_ATOMIC);
Tsutomu Itoh3d6b5c32012-09-06 00:18:10 -06001606 if (!ulist) {
1607 ret = -ENOMEM;
1608 goto out;
1609 }
Jan Schmidt995e01b2012-08-13 02:52:38 -06001610 ulist_add(ulist, qgroup->qgroupid, (uintptr_t)qgroup, GFP_ATOMIC);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001611 ULIST_ITER_INIT(&uiter);
1612 while ((unode = ulist_next(ulist, &uiter))) {
1613 struct btrfs_qgroup *qg;
1614 struct btrfs_qgroup_list *glist;
1615
Jan Schmidt995e01b2012-08-13 02:52:38 -06001616 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001617
1618 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
1619 qg->reserved + qg->rfer + num_bytes >
Wang Shilong720f1e22013-03-06 11:51:47 +00001620 qg->max_rfer) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02001621 ret = -EDQUOT;
Wang Shilong720f1e22013-03-06 11:51:47 +00001622 goto out;
1623 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001624
1625 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
1626 qg->reserved + qg->excl + num_bytes >
Wang Shilong720f1e22013-03-06 11:51:47 +00001627 qg->max_excl) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02001628 ret = -EDQUOT;
Wang Shilong720f1e22013-03-06 11:51:47 +00001629 goto out;
1630 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001631
1632 list_for_each_entry(glist, &qg->groups, next_group) {
1633 ulist_add(ulist, glist->group->qgroupid,
Jan Schmidt995e01b2012-08-13 02:52:38 -06001634 (uintptr_t)glist->group, GFP_ATOMIC);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001635 }
1636 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001637
1638 /*
1639 * no limits exceeded, now record the reservation into all qgroups
1640 */
1641 ULIST_ITER_INIT(&uiter);
1642 while ((unode = ulist_next(ulist, &uiter))) {
1643 struct btrfs_qgroup *qg;
1644
Jan Schmidt995e01b2012-08-13 02:52:38 -06001645 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001646
1647 qg->reserved += num_bytes;
1648 }
1649
1650out:
1651 spin_unlock(&fs_info->qgroup_lock);
1652 ulist_free(ulist);
1653
1654 return ret;
1655}
1656
1657void btrfs_qgroup_free(struct btrfs_root *root, u64 num_bytes)
1658{
1659 struct btrfs_root *quota_root;
1660 struct btrfs_qgroup *qgroup;
1661 struct btrfs_fs_info *fs_info = root->fs_info;
1662 struct ulist *ulist = NULL;
1663 struct ulist_node *unode;
1664 struct ulist_iterator uiter;
1665 u64 ref_root = root->root_key.objectid;
1666
1667 if (!is_fstree(ref_root))
1668 return;
1669
1670 if (num_bytes == 0)
1671 return;
1672
1673 spin_lock(&fs_info->qgroup_lock);
1674
1675 quota_root = fs_info->quota_root;
1676 if (!quota_root)
1677 goto out;
1678
1679 qgroup = find_qgroup_rb(fs_info, ref_root);
1680 if (!qgroup)
1681 goto out;
1682
1683 ulist = ulist_alloc(GFP_ATOMIC);
Tsutomu Itoh3d6b5c32012-09-06 00:18:10 -06001684 if (!ulist) {
1685 btrfs_std_error(fs_info, -ENOMEM);
1686 goto out;
1687 }
Jan Schmidt995e01b2012-08-13 02:52:38 -06001688 ulist_add(ulist, qgroup->qgroupid, (uintptr_t)qgroup, GFP_ATOMIC);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001689 ULIST_ITER_INIT(&uiter);
1690 while ((unode = ulist_next(ulist, &uiter))) {
1691 struct btrfs_qgroup *qg;
1692 struct btrfs_qgroup_list *glist;
1693
Jan Schmidt995e01b2012-08-13 02:52:38 -06001694 qg = (struct btrfs_qgroup *)(uintptr_t)unode->aux;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001695
1696 qg->reserved -= num_bytes;
1697
1698 list_for_each_entry(glist, &qg->groups, next_group) {
1699 ulist_add(ulist, glist->group->qgroupid,
Jan Schmidt995e01b2012-08-13 02:52:38 -06001700 (uintptr_t)glist->group, GFP_ATOMIC);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001701 }
1702 }
1703
1704out:
1705 spin_unlock(&fs_info->qgroup_lock);
1706 ulist_free(ulist);
1707}
1708
1709void assert_qgroups_uptodate(struct btrfs_trans_handle *trans)
1710{
1711 if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq)
1712 return;
1713 printk(KERN_ERR "btrfs: qgroups not uptodate in trans handle %p: list is%s empty, seq is %llu\n",
1714 trans, list_empty(&trans->qgroup_ref_list) ? "" : " not",
1715 trans->delayed_ref_elem.seq);
1716 BUG();
1717}