blob: 1e4c6e95ab558cd0b77dec55fb76bf32873afb8a [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"
Jan Schmidt2f232032013-04-25 16:04:51 +000034#include "extent_io.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070035#include "qgroup.h"
Arne Jansenbed92ea2012-06-28 18:03:02 +020036
37/* TODO XXX FIXME
38 * - subvol delete -> delete when ref goes to 0? delete limits also?
39 * - reorganize keys
40 * - compressed
41 * - sync
Arne Jansenbed92ea2012-06-28 18:03:02 +020042 * - copy also limits on subvol creation
43 * - limit
44 * - caches fuer ulists
45 * - performance benchmarks
46 * - check all ioctl parameters
47 */
48
49/*
50 * one struct for each qgroup, organized in fs_info->qgroup_tree.
51 */
52struct btrfs_qgroup {
53 u64 qgroupid;
54
55 /*
56 * state
57 */
58 u64 rfer; /* referenced */
59 u64 rfer_cmpr; /* referenced compressed */
60 u64 excl; /* exclusive */
61 u64 excl_cmpr; /* exclusive compressed */
62
63 /*
64 * limits
65 */
66 u64 lim_flags; /* which limits are set */
67 u64 max_rfer;
68 u64 max_excl;
69 u64 rsv_rfer;
70 u64 rsv_excl;
71
72 /*
73 * reservation tracking
74 */
75 u64 reserved;
76
77 /*
78 * lists
79 */
80 struct list_head groups; /* groups this group is member of */
81 struct list_head members; /* groups that are members of this group */
82 struct list_head dirty; /* dirty groups */
83 struct rb_node node; /* tree of qgroups */
84
85 /*
86 * temp variables for accounting operations
87 */
Josef Bacikfcebe452014-05-13 17:30:47 -070088 u64 old_refcnt;
89 u64 new_refcnt;
Arne Jansenbed92ea2012-06-28 18:03:02 +020090};
91
92/*
93 * glue structure to represent the relations between qgroups.
94 */
95struct btrfs_qgroup_list {
96 struct list_head next_group;
97 struct list_head next_member;
98 struct btrfs_qgroup *group;
99 struct btrfs_qgroup *member;
100};
101
Josef Bacikfcebe452014-05-13 17:30:47 -0700102#define ptr_to_u64(x) ((u64)(uintptr_t)x)
103#define u64_to_ptr(x) ((struct btrfs_qgroup *)(uintptr_t)x)
104
Jan Schmidtb382a322013-05-28 15:47:24 +0000105static int
106qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
107 int init_flags);
108static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +0000109
Wang Shilong58400fc2013-04-07 10:50:17 +0000110/* must be called with qgroup_ioctl_lock held */
Arne Jansenbed92ea2012-06-28 18:03:02 +0200111static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
112 u64 qgroupid)
113{
114 struct rb_node *n = fs_info->qgroup_tree.rb_node;
115 struct btrfs_qgroup *qgroup;
116
117 while (n) {
118 qgroup = rb_entry(n, struct btrfs_qgroup, node);
119 if (qgroup->qgroupid < qgroupid)
120 n = n->rb_left;
121 else if (qgroup->qgroupid > qgroupid)
122 n = n->rb_right;
123 else
124 return qgroup;
125 }
126 return NULL;
127}
128
129/* must be called with qgroup_lock held */
130static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
131 u64 qgroupid)
132{
133 struct rb_node **p = &fs_info->qgroup_tree.rb_node;
134 struct rb_node *parent = NULL;
135 struct btrfs_qgroup *qgroup;
136
137 while (*p) {
138 parent = *p;
139 qgroup = rb_entry(parent, struct btrfs_qgroup, node);
140
141 if (qgroup->qgroupid < qgroupid)
142 p = &(*p)->rb_left;
143 else if (qgroup->qgroupid > qgroupid)
144 p = &(*p)->rb_right;
145 else
146 return qgroup;
147 }
148
149 qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
150 if (!qgroup)
151 return ERR_PTR(-ENOMEM);
152
153 qgroup->qgroupid = qgroupid;
154 INIT_LIST_HEAD(&qgroup->groups);
155 INIT_LIST_HEAD(&qgroup->members);
156 INIT_LIST_HEAD(&qgroup->dirty);
157
158 rb_link_node(&qgroup->node, parent, p);
159 rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
160
161 return qgroup;
162}
163
Wang Shilong4082bd32013-08-14 09:13:36 +0800164static void __del_qgroup_rb(struct btrfs_qgroup *qgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200165{
Arne Jansenbed92ea2012-06-28 18:03:02 +0200166 struct btrfs_qgroup_list *list;
167
Arne Jansenbed92ea2012-06-28 18:03:02 +0200168 list_del(&qgroup->dirty);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200169 while (!list_empty(&qgroup->groups)) {
170 list = list_first_entry(&qgroup->groups,
171 struct btrfs_qgroup_list, next_group);
172 list_del(&list->next_group);
173 list_del(&list->next_member);
174 kfree(list);
175 }
176
177 while (!list_empty(&qgroup->members)) {
178 list = list_first_entry(&qgroup->members,
179 struct btrfs_qgroup_list, next_member);
180 list_del(&list->next_group);
181 list_del(&list->next_member);
182 kfree(list);
183 }
184 kfree(qgroup);
Wang Shilong4082bd32013-08-14 09:13:36 +0800185}
Arne Jansenbed92ea2012-06-28 18:03:02 +0200186
Wang Shilong4082bd32013-08-14 09:13:36 +0800187/* must be called with qgroup_lock held */
188static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
189{
190 struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
191
192 if (!qgroup)
193 return -ENOENT;
194
195 rb_erase(&qgroup->node, &fs_info->qgroup_tree);
196 __del_qgroup_rb(qgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200197 return 0;
198}
199
200/* must be called with qgroup_lock held */
201static int add_relation_rb(struct btrfs_fs_info *fs_info,
202 u64 memberid, u64 parentid)
203{
204 struct btrfs_qgroup *member;
205 struct btrfs_qgroup *parent;
206 struct btrfs_qgroup_list *list;
207
208 member = find_qgroup_rb(fs_info, memberid);
209 parent = find_qgroup_rb(fs_info, parentid);
210 if (!member || !parent)
211 return -ENOENT;
212
213 list = kzalloc(sizeof(*list), GFP_ATOMIC);
214 if (!list)
215 return -ENOMEM;
216
217 list->group = parent;
218 list->member = member;
219 list_add_tail(&list->next_group, &member->groups);
220 list_add_tail(&list->next_member, &parent->members);
221
222 return 0;
223}
224
225/* must be called with qgroup_lock held */
226static int del_relation_rb(struct btrfs_fs_info *fs_info,
227 u64 memberid, u64 parentid)
228{
229 struct btrfs_qgroup *member;
230 struct btrfs_qgroup *parent;
231 struct btrfs_qgroup_list *list;
232
233 member = find_qgroup_rb(fs_info, memberid);
234 parent = find_qgroup_rb(fs_info, parentid);
235 if (!member || !parent)
236 return -ENOENT;
237
238 list_for_each_entry(list, &member->groups, next_group) {
239 if (list->group == parent) {
240 list_del(&list->next_group);
241 list_del(&list->next_member);
242 kfree(list);
243 return 0;
244 }
245 }
246 return -ENOENT;
247}
248
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400249#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
250int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
251 u64 rfer, u64 excl)
252{
253 struct btrfs_qgroup *qgroup;
254
255 qgroup = find_qgroup_rb(fs_info, qgroupid);
256 if (!qgroup)
257 return -EINVAL;
258 if (qgroup->rfer != rfer || qgroup->excl != excl)
259 return -EINVAL;
260 return 0;
261}
262#endif
263
Arne Jansenbed92ea2012-06-28 18:03:02 +0200264/*
265 * The full config is read in one go, only called from open_ctree()
266 * It doesn't use any locking, as at this point we're still single-threaded
267 */
268int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
269{
270 struct btrfs_key key;
271 struct btrfs_key found_key;
272 struct btrfs_root *quota_root = fs_info->quota_root;
273 struct btrfs_path *path = NULL;
274 struct extent_buffer *l;
275 int slot;
276 int ret = 0;
277 u64 flags = 0;
Jan Schmidtb382a322013-05-28 15:47:24 +0000278 u64 rescan_progress = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200279
280 if (!fs_info->quota_enabled)
281 return 0;
282
Wang Shilong1e8f9152013-05-06 11:03:27 +0000283 fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
284 if (!fs_info->qgroup_ulist) {
285 ret = -ENOMEM;
286 goto out;
287 }
288
Arne Jansenbed92ea2012-06-28 18:03:02 +0200289 path = btrfs_alloc_path();
290 if (!path) {
291 ret = -ENOMEM;
292 goto out;
293 }
294
295 /* default this to quota off, in case no status key is found */
296 fs_info->qgroup_flags = 0;
297
298 /*
299 * pass 1: read status, all qgroup infos and limits
300 */
301 key.objectid = 0;
302 key.type = 0;
303 key.offset = 0;
304 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
305 if (ret)
306 goto out;
307
308 while (1) {
309 struct btrfs_qgroup *qgroup;
310
311 slot = path->slots[0];
312 l = path->nodes[0];
313 btrfs_item_key_to_cpu(l, &found_key, slot);
314
315 if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
316 struct btrfs_qgroup_status_item *ptr;
317
318 ptr = btrfs_item_ptr(l, slot,
319 struct btrfs_qgroup_status_item);
320
321 if (btrfs_qgroup_status_version(l, ptr) !=
322 BTRFS_QGROUP_STATUS_VERSION) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500323 btrfs_err(fs_info,
324 "old qgroup version, quota disabled");
Arne Jansenbed92ea2012-06-28 18:03:02 +0200325 goto out;
326 }
327 if (btrfs_qgroup_status_generation(l, ptr) !=
328 fs_info->generation) {
329 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Frank Holtonefe120a2013-12-20 11:37:06 -0500330 btrfs_err(fs_info,
331 "qgroup generation mismatch, "
332 "marked as inconsistent");
Arne Jansenbed92ea2012-06-28 18:03:02 +0200333 }
334 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
335 ptr);
Jan Schmidtb382a322013-05-28 15:47:24 +0000336 rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200337 goto next1;
338 }
339
340 if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
341 found_key.type != BTRFS_QGROUP_LIMIT_KEY)
342 goto next1;
343
344 qgroup = find_qgroup_rb(fs_info, found_key.offset);
345 if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
346 (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500347 btrfs_err(fs_info, "inconsitent qgroup config");
Arne Jansenbed92ea2012-06-28 18:03:02 +0200348 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
349 }
350 if (!qgroup) {
351 qgroup = add_qgroup_rb(fs_info, found_key.offset);
352 if (IS_ERR(qgroup)) {
353 ret = PTR_ERR(qgroup);
354 goto out;
355 }
356 }
357 switch (found_key.type) {
358 case BTRFS_QGROUP_INFO_KEY: {
359 struct btrfs_qgroup_info_item *ptr;
360
361 ptr = btrfs_item_ptr(l, slot,
362 struct btrfs_qgroup_info_item);
363 qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
364 qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
365 qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
366 qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
367 /* generation currently unused */
368 break;
369 }
370 case BTRFS_QGROUP_LIMIT_KEY: {
371 struct btrfs_qgroup_limit_item *ptr;
372
373 ptr = btrfs_item_ptr(l, slot,
374 struct btrfs_qgroup_limit_item);
375 qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
376 qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
377 qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
378 qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
379 qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
380 break;
381 }
382 }
383next1:
384 ret = btrfs_next_item(quota_root, path);
385 if (ret < 0)
386 goto out;
387 if (ret)
388 break;
389 }
390 btrfs_release_path(path);
391
392 /*
393 * pass 2: read all qgroup relations
394 */
395 key.objectid = 0;
396 key.type = BTRFS_QGROUP_RELATION_KEY;
397 key.offset = 0;
398 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
399 if (ret)
400 goto out;
401 while (1) {
402 slot = path->slots[0];
403 l = path->nodes[0];
404 btrfs_item_key_to_cpu(l, &found_key, slot);
405
406 if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
407 goto next2;
408
409 if (found_key.objectid > found_key.offset) {
410 /* parent <- member, not needed to build config */
411 /* FIXME should we omit the key completely? */
412 goto next2;
413 }
414
415 ret = add_relation_rb(fs_info, found_key.objectid,
416 found_key.offset);
Arne Jansenff248582013-01-17 01:22:08 -0700417 if (ret == -ENOENT) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500418 btrfs_warn(fs_info,
419 "orphan qgroup relation 0x%llx->0x%llx",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200420 found_key.objectid, found_key.offset);
Arne Jansenff248582013-01-17 01:22:08 -0700421 ret = 0; /* ignore the error */
422 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200423 if (ret)
424 goto out;
425next2:
426 ret = btrfs_next_item(quota_root, path);
427 if (ret < 0)
428 goto out;
429 if (ret)
430 break;
431 }
432out:
433 fs_info->qgroup_flags |= flags;
434 if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON)) {
435 fs_info->quota_enabled = 0;
436 fs_info->pending_quota_state = 0;
Jan Schmidtb382a322013-05-28 15:47:24 +0000437 } else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
438 ret >= 0) {
439 ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200440 }
441 btrfs_free_path(path);
442
Jan Schmidteb1716a2013-05-28 15:47:23 +0000443 if (ret < 0) {
Wang Shilong1e8f9152013-05-06 11:03:27 +0000444 ulist_free(fs_info->qgroup_ulist);
Jan Schmidteb1716a2013-05-28 15:47:23 +0000445 fs_info->qgroup_ulist = NULL;
Jan Schmidtb382a322013-05-28 15:47:24 +0000446 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
Jan Schmidteb1716a2013-05-28 15:47:23 +0000447 }
Wang Shilong1e8f9152013-05-06 11:03:27 +0000448
Arne Jansenbed92ea2012-06-28 18:03:02 +0200449 return ret < 0 ? ret : 0;
450}
451
452/*
Wang Shilonge685da12013-08-14 09:13:37 +0800453 * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
454 * first two are in single-threaded paths.And for the third one, we have set
455 * quota_root to be null with qgroup_lock held before, so it is safe to clean
456 * up the in-memory structures without qgroup_lock held.
Arne Jansenbed92ea2012-06-28 18:03:02 +0200457 */
458void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
459{
460 struct rb_node *n;
461 struct btrfs_qgroup *qgroup;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200462
463 while ((n = rb_first(&fs_info->qgroup_tree))) {
464 qgroup = rb_entry(n, struct btrfs_qgroup, node);
465 rb_erase(n, &fs_info->qgroup_tree);
Wang Shilong4082bd32013-08-14 09:13:36 +0800466 __del_qgroup_rb(qgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200467 }
Wang Shilong1e7bac12013-07-13 21:02:54 +0800468 /*
469 * we call btrfs_free_qgroup_config() when umounting
470 * filesystem and disabling quota, so we set qgroup_ulit
471 * to be null here to avoid double free.
472 */
Wang Shilong1e8f9152013-05-06 11:03:27 +0000473 ulist_free(fs_info->qgroup_ulist);
Wang Shilong1e7bac12013-07-13 21:02:54 +0800474 fs_info->qgroup_ulist = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200475}
476
477static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
478 struct btrfs_root *quota_root,
479 u64 src, u64 dst)
480{
481 int ret;
482 struct btrfs_path *path;
483 struct btrfs_key key;
484
485 path = btrfs_alloc_path();
486 if (!path)
487 return -ENOMEM;
488
489 key.objectid = src;
490 key.type = BTRFS_QGROUP_RELATION_KEY;
491 key.offset = dst;
492
493 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
494
495 btrfs_mark_buffer_dirty(path->nodes[0]);
496
497 btrfs_free_path(path);
498 return ret;
499}
500
501static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
502 struct btrfs_root *quota_root,
503 u64 src, u64 dst)
504{
505 int ret;
506 struct btrfs_path *path;
507 struct btrfs_key key;
508
509 path = btrfs_alloc_path();
510 if (!path)
511 return -ENOMEM;
512
513 key.objectid = src;
514 key.type = BTRFS_QGROUP_RELATION_KEY;
515 key.offset = dst;
516
517 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
518 if (ret < 0)
519 goto out;
520
521 if (ret > 0) {
522 ret = -ENOENT;
523 goto out;
524 }
525
526 ret = btrfs_del_item(trans, quota_root, path);
527out:
528 btrfs_free_path(path);
529 return ret;
530}
531
532static int add_qgroup_item(struct btrfs_trans_handle *trans,
533 struct btrfs_root *quota_root, u64 qgroupid)
534{
535 int ret;
536 struct btrfs_path *path;
537 struct btrfs_qgroup_info_item *qgroup_info;
538 struct btrfs_qgroup_limit_item *qgroup_limit;
539 struct extent_buffer *leaf;
540 struct btrfs_key key;
541
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400542#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
543 if (unlikely(test_bit(BTRFS_ROOT_DUMMY_ROOT, &quota_root->state)))
544 return 0;
545#endif
Arne Jansenbed92ea2012-06-28 18:03:02 +0200546 path = btrfs_alloc_path();
547 if (!path)
548 return -ENOMEM;
549
550 key.objectid = 0;
551 key.type = BTRFS_QGROUP_INFO_KEY;
552 key.offset = qgroupid;
553
554 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
555 sizeof(*qgroup_info));
556 if (ret)
557 goto out;
558
559 leaf = path->nodes[0];
560 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
561 struct btrfs_qgroup_info_item);
562 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
563 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
564 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
565 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
566 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
567
568 btrfs_mark_buffer_dirty(leaf);
569
570 btrfs_release_path(path);
571
572 key.type = BTRFS_QGROUP_LIMIT_KEY;
573 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
574 sizeof(*qgroup_limit));
575 if (ret)
576 goto out;
577
578 leaf = path->nodes[0];
579 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
580 struct btrfs_qgroup_limit_item);
581 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
582 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
583 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
584 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
585 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
586
587 btrfs_mark_buffer_dirty(leaf);
588
589 ret = 0;
590out:
591 btrfs_free_path(path);
592 return ret;
593}
594
595static int del_qgroup_item(struct btrfs_trans_handle *trans,
596 struct btrfs_root *quota_root, u64 qgroupid)
597{
598 int ret;
599 struct btrfs_path *path;
600 struct btrfs_key key;
601
602 path = btrfs_alloc_path();
603 if (!path)
604 return -ENOMEM;
605
606 key.objectid = 0;
607 key.type = BTRFS_QGROUP_INFO_KEY;
608 key.offset = qgroupid;
609 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
610 if (ret < 0)
611 goto out;
612
613 if (ret > 0) {
614 ret = -ENOENT;
615 goto out;
616 }
617
618 ret = btrfs_del_item(trans, quota_root, path);
619 if (ret)
620 goto out;
621
622 btrfs_release_path(path);
623
624 key.type = BTRFS_QGROUP_LIMIT_KEY;
625 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
626 if (ret < 0)
627 goto out;
628
629 if (ret > 0) {
630 ret = -ENOENT;
631 goto out;
632 }
633
634 ret = btrfs_del_item(trans, quota_root, path);
635
636out:
637 btrfs_free_path(path);
638 return ret;
639}
640
641static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
642 struct btrfs_root *root, u64 qgroupid,
643 u64 flags, u64 max_rfer, u64 max_excl,
644 u64 rsv_rfer, u64 rsv_excl)
645{
646 struct btrfs_path *path;
647 struct btrfs_key key;
648 struct extent_buffer *l;
649 struct btrfs_qgroup_limit_item *qgroup_limit;
650 int ret;
651 int slot;
652
653 key.objectid = 0;
654 key.type = BTRFS_QGROUP_LIMIT_KEY;
655 key.offset = qgroupid;
656
657 path = btrfs_alloc_path();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000658 if (!path)
659 return -ENOMEM;
660
Arne Jansenbed92ea2012-06-28 18:03:02 +0200661 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
662 if (ret > 0)
663 ret = -ENOENT;
664
665 if (ret)
666 goto out;
667
668 l = path->nodes[0];
669 slot = path->slots[0];
Valentina Giustia3df41e2013-11-04 22:34:29 +0100670 qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200671 btrfs_set_qgroup_limit_flags(l, qgroup_limit, flags);
672 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, max_rfer);
673 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, max_excl);
674 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, rsv_rfer);
675 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, rsv_excl);
676
677 btrfs_mark_buffer_dirty(l);
678
679out:
680 btrfs_free_path(path);
681 return ret;
682}
683
684static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
685 struct btrfs_root *root,
686 struct btrfs_qgroup *qgroup)
687{
688 struct btrfs_path *path;
689 struct btrfs_key key;
690 struct extent_buffer *l;
691 struct btrfs_qgroup_info_item *qgroup_info;
692 int ret;
693 int slot;
694
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400695#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
696 if (unlikely(test_bit(BTRFS_ROOT_DUMMY_ROOT, &root->state)))
697 return 0;
698#endif
Arne Jansenbed92ea2012-06-28 18:03:02 +0200699 key.objectid = 0;
700 key.type = BTRFS_QGROUP_INFO_KEY;
701 key.offset = qgroup->qgroupid;
702
703 path = btrfs_alloc_path();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000704 if (!path)
705 return -ENOMEM;
706
Arne Jansenbed92ea2012-06-28 18:03:02 +0200707 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
708 if (ret > 0)
709 ret = -ENOENT;
710
711 if (ret)
712 goto out;
713
714 l = path->nodes[0];
715 slot = path->slots[0];
Valentina Giustia3df41e2013-11-04 22:34:29 +0100716 qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200717 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
718 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
719 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
720 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
721 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
722
723 btrfs_mark_buffer_dirty(l);
724
725out:
726 btrfs_free_path(path);
727 return ret;
728}
729
730static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
731 struct btrfs_fs_info *fs_info,
732 struct btrfs_root *root)
733{
734 struct btrfs_path *path;
735 struct btrfs_key key;
736 struct extent_buffer *l;
737 struct btrfs_qgroup_status_item *ptr;
738 int ret;
739 int slot;
740
741 key.objectid = 0;
742 key.type = BTRFS_QGROUP_STATUS_KEY;
743 key.offset = 0;
744
745 path = btrfs_alloc_path();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000746 if (!path)
747 return -ENOMEM;
748
Arne Jansenbed92ea2012-06-28 18:03:02 +0200749 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
750 if (ret > 0)
751 ret = -ENOENT;
752
753 if (ret)
754 goto out;
755
756 l = path->nodes[0];
757 slot = path->slots[0];
758 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
759 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
760 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
Jan Schmidt2f232032013-04-25 16:04:51 +0000761 btrfs_set_qgroup_status_rescan(l, ptr,
762 fs_info->qgroup_rescan_progress.objectid);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200763
764 btrfs_mark_buffer_dirty(l);
765
766out:
767 btrfs_free_path(path);
768 return ret;
769}
770
771/*
772 * called with qgroup_lock held
773 */
774static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
775 struct btrfs_root *root)
776{
777 struct btrfs_path *path;
778 struct btrfs_key key;
Wang Shilong06b3a862013-02-27 11:16:57 +0000779 struct extent_buffer *leaf = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200780 int ret;
Wang Shilong06b3a862013-02-27 11:16:57 +0000781 int nr = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200782
Arne Jansenbed92ea2012-06-28 18:03:02 +0200783 path = btrfs_alloc_path();
784 if (!path)
785 return -ENOMEM;
786
Wang Shilong06b3a862013-02-27 11:16:57 +0000787 path->leave_spinning = 1;
788
789 key.objectid = 0;
790 key.offset = 0;
791 key.type = 0;
792
Arne Jansenbed92ea2012-06-28 18:03:02 +0200793 while (1) {
Arne Jansenbed92ea2012-06-28 18:03:02 +0200794 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Wang Shilong06b3a862013-02-27 11:16:57 +0000795 if (ret < 0)
796 goto out;
797 leaf = path->nodes[0];
798 nr = btrfs_header_nritems(leaf);
799 if (!nr)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200800 break;
Wang Shilong06b3a862013-02-27 11:16:57 +0000801 /*
802 * delete the leaf one by one
803 * since the whole tree is going
804 * to be deleted.
805 */
806 path->slots[0] = 0;
807 ret = btrfs_del_items(trans, root, path, 0, nr);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200808 if (ret)
809 goto out;
Wang Shilong06b3a862013-02-27 11:16:57 +0000810
Arne Jansenbed92ea2012-06-28 18:03:02 +0200811 btrfs_release_path(path);
812 }
813 ret = 0;
814out:
815 root->fs_info->pending_quota_state = 0;
816 btrfs_free_path(path);
817 return ret;
818}
819
820int btrfs_quota_enable(struct btrfs_trans_handle *trans,
821 struct btrfs_fs_info *fs_info)
822{
823 struct btrfs_root *quota_root;
Wang Shilong7708f022013-04-07 10:24:57 +0000824 struct btrfs_root *tree_root = fs_info->tree_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200825 struct btrfs_path *path = NULL;
826 struct btrfs_qgroup_status_item *ptr;
827 struct extent_buffer *leaf;
828 struct btrfs_key key;
Wang Shilong7708f022013-04-07 10:24:57 +0000829 struct btrfs_key found_key;
830 struct btrfs_qgroup *qgroup = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200831 int ret = 0;
Wang Shilong7708f022013-04-07 10:24:57 +0000832 int slot;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200833
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000834 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200835 if (fs_info->quota_root) {
836 fs_info->pending_quota_state = 1;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200837 goto out;
838 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200839
Wang Shilong1e8f9152013-05-06 11:03:27 +0000840 fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
841 if (!fs_info->qgroup_ulist) {
842 ret = -ENOMEM;
843 goto out;
844 }
845
Arne Jansenbed92ea2012-06-28 18:03:02 +0200846 /*
847 * initially create the quota tree
848 */
849 quota_root = btrfs_create_tree(trans, fs_info,
850 BTRFS_QUOTA_TREE_OBJECTID);
851 if (IS_ERR(quota_root)) {
852 ret = PTR_ERR(quota_root);
853 goto out;
854 }
855
856 path = btrfs_alloc_path();
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000857 if (!path) {
858 ret = -ENOMEM;
859 goto out_free_root;
860 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200861
862 key.objectid = 0;
863 key.type = BTRFS_QGROUP_STATUS_KEY;
864 key.offset = 0;
865
866 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
867 sizeof(*ptr));
868 if (ret)
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000869 goto out_free_path;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200870
871 leaf = path->nodes[0];
872 ptr = btrfs_item_ptr(leaf, path->slots[0],
873 struct btrfs_qgroup_status_item);
874 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
875 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
876 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
877 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
878 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
Jan Schmidt2f232032013-04-25 16:04:51 +0000879 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200880
881 btrfs_mark_buffer_dirty(leaf);
882
Wang Shilong7708f022013-04-07 10:24:57 +0000883 key.objectid = 0;
884 key.type = BTRFS_ROOT_REF_KEY;
885 key.offset = 0;
886
887 btrfs_release_path(path);
888 ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
889 if (ret > 0)
890 goto out_add_root;
891 if (ret < 0)
892 goto out_free_path;
893
894
895 while (1) {
896 slot = path->slots[0];
897 leaf = path->nodes[0];
898 btrfs_item_key_to_cpu(leaf, &found_key, slot);
899
900 if (found_key.type == BTRFS_ROOT_REF_KEY) {
901 ret = add_qgroup_item(trans, quota_root,
902 found_key.offset);
903 if (ret)
904 goto out_free_path;
905
Wang Shilong7708f022013-04-07 10:24:57 +0000906 qgroup = add_qgroup_rb(fs_info, found_key.offset);
907 if (IS_ERR(qgroup)) {
Wang Shilong7708f022013-04-07 10:24:57 +0000908 ret = PTR_ERR(qgroup);
909 goto out_free_path;
910 }
Wang Shilong7708f022013-04-07 10:24:57 +0000911 }
912 ret = btrfs_next_item(tree_root, path);
913 if (ret < 0)
914 goto out_free_path;
915 if (ret)
916 break;
917 }
918
919out_add_root:
920 btrfs_release_path(path);
921 ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
922 if (ret)
923 goto out_free_path;
924
Wang Shilong7708f022013-04-07 10:24:57 +0000925 qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
926 if (IS_ERR(qgroup)) {
Wang Shilong7708f022013-04-07 10:24:57 +0000927 ret = PTR_ERR(qgroup);
928 goto out_free_path;
929 }
Wang Shilong58400fc2013-04-07 10:50:17 +0000930 spin_lock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200931 fs_info->quota_root = quota_root;
932 fs_info->pending_quota_state = 1;
933 spin_unlock(&fs_info->qgroup_lock);
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000934out_free_path:
Arne Jansenbed92ea2012-06-28 18:03:02 +0200935 btrfs_free_path(path);
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000936out_free_root:
937 if (ret) {
938 free_extent_buffer(quota_root->node);
939 free_extent_buffer(quota_root->commit_root);
940 kfree(quota_root);
941 }
942out:
Jan Schmidteb1716a2013-05-28 15:47:23 +0000943 if (ret) {
Wang Shilong1e8f9152013-05-06 11:03:27 +0000944 ulist_free(fs_info->qgroup_ulist);
Jan Schmidteb1716a2013-05-28 15:47:23 +0000945 fs_info->qgroup_ulist = NULL;
946 }
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000947 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200948 return ret;
949}
950
951int btrfs_quota_disable(struct btrfs_trans_handle *trans,
952 struct btrfs_fs_info *fs_info)
953{
954 struct btrfs_root *tree_root = fs_info->tree_root;
955 struct btrfs_root *quota_root;
956 int ret = 0;
957
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000958 mutex_lock(&fs_info->qgroup_ioctl_lock);
Wang Shilong58400fc2013-04-07 10:50:17 +0000959 if (!fs_info->quota_root)
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000960 goto out;
Wang Shilong58400fc2013-04-07 10:50:17 +0000961 spin_lock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200962 fs_info->quota_enabled = 0;
963 fs_info->pending_quota_state = 0;
964 quota_root = fs_info->quota_root;
965 fs_info->quota_root = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200966 spin_unlock(&fs_info->qgroup_lock);
967
Wang Shilonge685da12013-08-14 09:13:37 +0800968 btrfs_free_qgroup_config(fs_info);
969
Arne Jansenbed92ea2012-06-28 18:03:02 +0200970 ret = btrfs_clean_quota_tree(trans, quota_root);
971 if (ret)
972 goto out;
973
974 ret = btrfs_del_root(trans, tree_root, &quota_root->root_key);
975 if (ret)
976 goto out;
977
978 list_del(&quota_root->dirty_list);
979
980 btrfs_tree_lock(quota_root->node);
981 clean_tree_block(trans, tree_root, quota_root->node);
982 btrfs_tree_unlock(quota_root->node);
983 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
984
985 free_extent_buffer(quota_root->node);
986 free_extent_buffer(quota_root->commit_root);
987 kfree(quota_root);
988out:
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000989 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200990 return ret;
991}
992
Jan Schmidt2f232032013-04-25 16:04:51 +0000993static void qgroup_dirty(struct btrfs_fs_info *fs_info,
994 struct btrfs_qgroup *qgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200995{
Jan Schmidt2f232032013-04-25 16:04:51 +0000996 if (list_empty(&qgroup->dirty))
997 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200998}
999
1000int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
1001 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1002{
1003 struct btrfs_root *quota_root;
Wang Shilongb7fef4f2013-04-07 10:50:18 +00001004 struct btrfs_qgroup *parent;
1005 struct btrfs_qgroup *member;
Wang Shilong534e6622013-04-17 14:49:51 +00001006 struct btrfs_qgroup_list *list;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001007 int ret = 0;
1008
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001009 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001010 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001011 if (!quota_root) {
1012 ret = -EINVAL;
1013 goto out;
1014 }
Wang Shilongb7fef4f2013-04-07 10:50:18 +00001015 member = find_qgroup_rb(fs_info, src);
1016 parent = find_qgroup_rb(fs_info, dst);
1017 if (!member || !parent) {
1018 ret = -EINVAL;
1019 goto out;
1020 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001021
Wang Shilong534e6622013-04-17 14:49:51 +00001022 /* check if such qgroup relation exist firstly */
1023 list_for_each_entry(list, &member->groups, next_group) {
1024 if (list->group == parent) {
1025 ret = -EEXIST;
1026 goto out;
1027 }
1028 }
1029
Arne Jansenbed92ea2012-06-28 18:03:02 +02001030 ret = add_qgroup_relation_item(trans, quota_root, src, dst);
1031 if (ret)
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001032 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001033
1034 ret = add_qgroup_relation_item(trans, quota_root, dst, src);
1035 if (ret) {
1036 del_qgroup_relation_item(trans, quota_root, src, dst);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001037 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001038 }
1039
1040 spin_lock(&fs_info->qgroup_lock);
1041 ret = add_relation_rb(quota_root->fs_info, src, dst);
1042 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001043out:
1044 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001045 return ret;
1046}
1047
1048int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
1049 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1050{
1051 struct btrfs_root *quota_root;
Wang Shilong534e6622013-04-17 14:49:51 +00001052 struct btrfs_qgroup *parent;
1053 struct btrfs_qgroup *member;
1054 struct btrfs_qgroup_list *list;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001055 int ret = 0;
1056 int err;
1057
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001058 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001059 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001060 if (!quota_root) {
1061 ret = -EINVAL;
1062 goto out;
1063 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001064
Wang Shilong534e6622013-04-17 14:49:51 +00001065 member = find_qgroup_rb(fs_info, src);
1066 parent = find_qgroup_rb(fs_info, dst);
1067 if (!member || !parent) {
1068 ret = -EINVAL;
1069 goto out;
1070 }
1071
1072 /* check if such qgroup relation exist firstly */
1073 list_for_each_entry(list, &member->groups, next_group) {
1074 if (list->group == parent)
1075 goto exist;
1076 }
1077 ret = -ENOENT;
1078 goto out;
1079exist:
Arne Jansenbed92ea2012-06-28 18:03:02 +02001080 ret = del_qgroup_relation_item(trans, quota_root, src, dst);
1081 err = del_qgroup_relation_item(trans, quota_root, dst, src);
1082 if (err && !ret)
1083 ret = err;
1084
1085 spin_lock(&fs_info->qgroup_lock);
1086 del_relation_rb(fs_info, src, dst);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001087 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001088out:
1089 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001090 return ret;
1091}
1092
1093int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
1094 struct btrfs_fs_info *fs_info, u64 qgroupid, char *name)
1095{
1096 struct btrfs_root *quota_root;
1097 struct btrfs_qgroup *qgroup;
1098 int ret = 0;
1099
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001100 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001101 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001102 if (!quota_root) {
1103 ret = -EINVAL;
1104 goto out;
1105 }
Wang Shilong534e6622013-04-17 14:49:51 +00001106 qgroup = find_qgroup_rb(fs_info, qgroupid);
1107 if (qgroup) {
1108 ret = -EEXIST;
1109 goto out;
1110 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001111
1112 ret = add_qgroup_item(trans, quota_root, qgroupid);
Wang Shilong534e6622013-04-17 14:49:51 +00001113 if (ret)
1114 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001115
1116 spin_lock(&fs_info->qgroup_lock);
1117 qgroup = add_qgroup_rb(fs_info, qgroupid);
1118 spin_unlock(&fs_info->qgroup_lock);
1119
1120 if (IS_ERR(qgroup))
1121 ret = PTR_ERR(qgroup);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001122out:
1123 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001124 return ret;
1125}
1126
1127int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
1128 struct btrfs_fs_info *fs_info, u64 qgroupid)
1129{
1130 struct btrfs_root *quota_root;
Arne Jansen2cf68702013-01-17 01:22:09 -07001131 struct btrfs_qgroup *qgroup;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001132 int ret = 0;
1133
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001134 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001135 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001136 if (!quota_root) {
1137 ret = -EINVAL;
1138 goto out;
1139 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001140
Arne Jansen2cf68702013-01-17 01:22:09 -07001141 qgroup = find_qgroup_rb(fs_info, qgroupid);
Wang Shilong534e6622013-04-17 14:49:51 +00001142 if (!qgroup) {
1143 ret = -ENOENT;
1144 goto out;
1145 } else {
1146 /* check if there are no relations to this qgroup */
1147 if (!list_empty(&qgroup->groups) ||
1148 !list_empty(&qgroup->members)) {
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001149 ret = -EBUSY;
1150 goto out;
Arne Jansen2cf68702013-01-17 01:22:09 -07001151 }
1152 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001153 ret = del_qgroup_item(trans, quota_root, qgroupid);
1154
1155 spin_lock(&fs_info->qgroup_lock);
1156 del_qgroup_rb(quota_root->fs_info, qgroupid);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001157 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001158out:
1159 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001160 return ret;
1161}
1162
1163int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
1164 struct btrfs_fs_info *fs_info, u64 qgroupid,
1165 struct btrfs_qgroup_limit *limit)
1166{
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001167 struct btrfs_root *quota_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001168 struct btrfs_qgroup *qgroup;
1169 int ret = 0;
1170
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001171 mutex_lock(&fs_info->qgroup_ioctl_lock);
1172 quota_root = fs_info->quota_root;
1173 if (!quota_root) {
1174 ret = -EINVAL;
1175 goto out;
1176 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001177
Wang Shilongddb47af2013-04-07 10:50:20 +00001178 qgroup = find_qgroup_rb(fs_info, qgroupid);
1179 if (!qgroup) {
1180 ret = -ENOENT;
1181 goto out;
1182 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001183 ret = update_qgroup_limit_item(trans, quota_root, qgroupid,
1184 limit->flags, limit->max_rfer,
1185 limit->max_excl, limit->rsv_rfer,
1186 limit->rsv_excl);
1187 if (ret) {
1188 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Frank Holtonefe120a2013-12-20 11:37:06 -05001189 btrfs_info(fs_info, "unable to update quota limit for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001190 qgroupid);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001191 }
1192
Wang Shilong58400fc2013-04-07 10:50:17 +00001193 spin_lock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001194 qgroup->lim_flags = limit->flags;
1195 qgroup->max_rfer = limit->max_rfer;
1196 qgroup->max_excl = limit->max_excl;
1197 qgroup->rsv_rfer = limit->rsv_rfer;
1198 qgroup->rsv_excl = limit->rsv_excl;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001199 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001200out:
1201 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001202 return ret;
1203}
Mark Fasheh11526512014-07-17 12:39:01 -07001204
1205static int comp_oper_exist(struct btrfs_qgroup_operation *oper1,
1206 struct btrfs_qgroup_operation *oper2)
1207{
1208 /*
1209 * Ignore seq and type here, we're looking for any operation
1210 * at all related to this extent on that root.
1211 */
1212 if (oper1->bytenr < oper2->bytenr)
1213 return -1;
1214 if (oper1->bytenr > oper2->bytenr)
1215 return 1;
1216 if (oper1->ref_root < oper2->ref_root)
1217 return -1;
1218 if (oper1->ref_root > oper2->ref_root)
1219 return 1;
1220 return 0;
1221}
1222
1223static int qgroup_oper_exists(struct btrfs_fs_info *fs_info,
1224 struct btrfs_qgroup_operation *oper)
1225{
1226 struct rb_node *n;
1227 struct btrfs_qgroup_operation *cur;
1228 int cmp;
1229
1230 spin_lock(&fs_info->qgroup_op_lock);
1231 n = fs_info->qgroup_op_tree.rb_node;
1232 while (n) {
1233 cur = rb_entry(n, struct btrfs_qgroup_operation, n);
1234 cmp = comp_oper_exist(cur, oper);
1235 if (cmp < 0) {
1236 n = n->rb_right;
1237 } else if (cmp) {
1238 n = n->rb_left;
1239 } else {
1240 spin_unlock(&fs_info->qgroup_op_lock);
1241 return -EEXIST;
1242 }
1243 }
1244 spin_unlock(&fs_info->qgroup_op_lock);
1245 return 0;
1246}
1247
Josef Bacikfcebe452014-05-13 17:30:47 -07001248static int comp_oper(struct btrfs_qgroup_operation *oper1,
1249 struct btrfs_qgroup_operation *oper2)
1250{
1251 if (oper1->bytenr < oper2->bytenr)
1252 return -1;
1253 if (oper1->bytenr > oper2->bytenr)
1254 return 1;
1255 if (oper1->seq < oper2->seq)
1256 return -1;
1257 if (oper1->seq > oper2->seq)
1258 return -1;
1259 if (oper1->ref_root < oper2->ref_root)
1260 return -1;
1261 if (oper1->ref_root > oper2->ref_root)
1262 return 1;
1263 if (oper1->type < oper2->type)
1264 return -1;
1265 if (oper1->type > oper2->type)
1266 return 1;
1267 return 0;
1268}
1269
1270static int insert_qgroup_oper(struct btrfs_fs_info *fs_info,
1271 struct btrfs_qgroup_operation *oper)
1272{
1273 struct rb_node **p;
1274 struct rb_node *parent = NULL;
1275 struct btrfs_qgroup_operation *cur;
1276 int cmp;
1277
1278 spin_lock(&fs_info->qgroup_op_lock);
1279 p = &fs_info->qgroup_op_tree.rb_node;
1280 while (*p) {
1281 parent = *p;
1282 cur = rb_entry(parent, struct btrfs_qgroup_operation, n);
1283 cmp = comp_oper(cur, oper);
1284 if (cmp < 0) {
1285 p = &(*p)->rb_right;
1286 } else if (cmp) {
1287 p = &(*p)->rb_left;
1288 } else {
1289 spin_unlock(&fs_info->qgroup_op_lock);
1290 return -EEXIST;
1291 }
1292 }
1293 rb_link_node(&oper->n, parent, p);
1294 rb_insert_color(&oper->n, &fs_info->qgroup_op_tree);
1295 spin_unlock(&fs_info->qgroup_op_lock);
1296 return 0;
1297}
Arne Jansenbed92ea2012-06-28 18:03:02 +02001298
Arne Jansenbed92ea2012-06-28 18:03:02 +02001299/*
Josef Bacikfcebe452014-05-13 17:30:47 -07001300 * Record a quota operation for processing later on.
1301 * @trans: the transaction we are adding the delayed op to.
1302 * @fs_info: the fs_info for this fs.
1303 * @ref_root: the root of the reference we are acting on,
1304 * @bytenr: the bytenr we are acting on.
1305 * @num_bytes: the number of bytes in the reference.
1306 * @type: the type of operation this is.
1307 * @mod_seq: do we need to get a sequence number for looking up roots.
1308 *
1309 * We just add it to our trans qgroup_ref_list and carry on and process these
1310 * operations in order at some later point. If the reference root isn't a fs
1311 * root then we don't bother with doing anything.
1312 *
1313 * MUST BE HOLDING THE REF LOCK.
Arne Jansenbed92ea2012-06-28 18:03:02 +02001314 */
1315int btrfs_qgroup_record_ref(struct btrfs_trans_handle *trans,
Josef Bacikfcebe452014-05-13 17:30:47 -07001316 struct btrfs_fs_info *fs_info, u64 ref_root,
1317 u64 bytenr, u64 num_bytes,
1318 enum btrfs_qgroup_operation_type type, int mod_seq)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001319{
Josef Bacikfcebe452014-05-13 17:30:47 -07001320 struct btrfs_qgroup_operation *oper;
1321 int ret;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001322
Josef Bacikfcebe452014-05-13 17:30:47 -07001323 if (!is_fstree(ref_root) || !fs_info->quota_enabled)
1324 return 0;
1325
1326 oper = kmalloc(sizeof(*oper), GFP_NOFS);
1327 if (!oper)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001328 return -ENOMEM;
1329
Josef Bacikfcebe452014-05-13 17:30:47 -07001330 oper->ref_root = ref_root;
1331 oper->bytenr = bytenr;
1332 oper->num_bytes = num_bytes;
1333 oper->type = type;
1334 oper->seq = atomic_inc_return(&fs_info->qgroup_op_seq);
1335 INIT_LIST_HEAD(&oper->elem.list);
1336 oper->elem.seq = 0;
Mark Fasheh11526512014-07-17 12:39:01 -07001337
Mark Fashehd3982102014-07-17 12:39:00 -07001338 trace_btrfs_qgroup_record_ref(oper);
1339
Mark Fasheh11526512014-07-17 12:39:01 -07001340 if (type == BTRFS_QGROUP_OPER_SUB_SUBTREE) {
1341 /*
1342 * If any operation for this bytenr/ref_root combo
1343 * exists, then we know it's not exclusively owned and
1344 * shouldn't be queued up.
1345 *
1346 * This also catches the case where we have a cloned
1347 * extent that gets queued up multiple times during
1348 * drop snapshot.
1349 */
1350 if (qgroup_oper_exists(fs_info, oper)) {
1351 kfree(oper);
1352 return 0;
1353 }
1354 }
1355
Josef Bacikfcebe452014-05-13 17:30:47 -07001356 ret = insert_qgroup_oper(fs_info, oper);
1357 if (ret) {
1358 /* Shouldn't happen so have an assert for developers */
1359 ASSERT(0);
1360 kfree(oper);
1361 return ret;
1362 }
1363 list_add_tail(&oper->list, &trans->qgroup_ref_list);
1364
1365 if (mod_seq)
1366 btrfs_get_tree_mod_seq(fs_info, &oper->elem);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001367
1368 return 0;
1369}
1370
Josef Bacikfcebe452014-05-13 17:30:47 -07001371/*
1372 * The easy accounting, if we are adding/removing the only ref for an extent
1373 * then this qgroup and all of the parent qgroups get their refrence and
1374 * exclusive counts adjusted.
1375 */
1376static int qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
1377 struct btrfs_qgroup_operation *oper)
1378{
1379 struct btrfs_qgroup *qgroup;
1380 struct ulist *tmp;
1381 struct btrfs_qgroup_list *glist;
1382 struct ulist_node *unode;
1383 struct ulist_iterator uiter;
1384 int sign = 0;
1385 int ret = 0;
1386
1387 tmp = ulist_alloc(GFP_NOFS);
1388 if (!tmp)
1389 return -ENOMEM;
1390
1391 spin_lock(&fs_info->qgroup_lock);
1392 if (!fs_info->quota_root)
1393 goto out;
1394 qgroup = find_qgroup_rb(fs_info, oper->ref_root);
1395 if (!qgroup)
1396 goto out;
1397 switch (oper->type) {
1398 case BTRFS_QGROUP_OPER_ADD_EXCL:
1399 sign = 1;
1400 break;
1401 case BTRFS_QGROUP_OPER_SUB_EXCL:
1402 sign = -1;
1403 break;
1404 default:
1405 ASSERT(0);
1406 }
1407 qgroup->rfer += sign * oper->num_bytes;
1408 qgroup->rfer_cmpr += sign * oper->num_bytes;
1409
1410 WARN_ON(sign < 0 && qgroup->excl < oper->num_bytes);
1411 qgroup->excl += sign * oper->num_bytes;
1412 qgroup->excl_cmpr += sign * oper->num_bytes;
1413
1414 qgroup_dirty(fs_info, qgroup);
1415
1416 /* Get all of the parent groups that contain this qgroup */
1417 list_for_each_entry(glist, &qgroup->groups, next_group) {
1418 ret = ulist_add(tmp, glist->group->qgroupid,
1419 ptr_to_u64(glist->group), GFP_ATOMIC);
1420 if (ret < 0)
1421 goto out;
1422 }
1423
1424 /* Iterate all of the parents and adjust their reference counts */
1425 ULIST_ITER_INIT(&uiter);
1426 while ((unode = ulist_next(tmp, &uiter))) {
1427 qgroup = u64_to_ptr(unode->aux);
1428 qgroup->rfer += sign * oper->num_bytes;
1429 qgroup->rfer_cmpr += sign * oper->num_bytes;
1430 qgroup->excl += sign * oper->num_bytes;
1431 if (sign < 0)
1432 WARN_ON(qgroup->excl < oper->num_bytes);
1433 qgroup->excl_cmpr += sign * oper->num_bytes;
1434 qgroup_dirty(fs_info, qgroup);
1435
1436 /* Add any parents of the parents */
1437 list_for_each_entry(glist, &qgroup->groups, next_group) {
1438 ret = ulist_add(tmp, glist->group->qgroupid,
1439 ptr_to_u64(glist->group), GFP_ATOMIC);
1440 if (ret < 0)
1441 goto out;
1442 }
1443 }
1444 ret = 0;
1445out:
1446 spin_unlock(&fs_info->qgroup_lock);
1447 ulist_free(tmp);
1448 return ret;
1449}
1450
1451/*
1452 * Walk all of the roots that pointed to our bytenr and adjust their refcnts as
1453 * properly.
1454 */
1455static int qgroup_calc_old_refcnt(struct btrfs_fs_info *fs_info,
1456 u64 root_to_skip, struct ulist *tmp,
1457 struct ulist *roots, struct ulist *qgroups,
1458 u64 seq, int *old_roots, int rescan)
Jan Schmidt46b665c2013-04-25 16:04:50 +00001459{
1460 struct ulist_node *unode;
1461 struct ulist_iterator uiter;
1462 struct ulist_node *tmp_unode;
1463 struct ulist_iterator tmp_uiter;
1464 struct btrfs_qgroup *qg;
1465 int ret;
1466
1467 ULIST_ITER_INIT(&uiter);
1468 while ((unode = ulist_next(roots, &uiter))) {
Josef Bacikfcebe452014-05-13 17:30:47 -07001469 /* We don't count our current root here */
1470 if (unode->val == root_to_skip)
1471 continue;
Jan Schmidt46b665c2013-04-25 16:04:50 +00001472 qg = find_qgroup_rb(fs_info, unode->val);
1473 if (!qg)
1474 continue;
Josef Bacikfcebe452014-05-13 17:30:47 -07001475 /*
1476 * We could have a pending removal of this same ref so we may
1477 * not have actually found our ref root when doing
1478 * btrfs_find_all_roots, so we need to keep track of how many
1479 * old roots we find in case we removed ours and added a
1480 * different one at the same time. I don't think this could
1481 * happen in practice but that sort of thinking leads to pain
1482 * and suffering and to the dark side.
1483 */
1484 (*old_roots)++;
Jan Schmidt46b665c2013-04-25 16:04:50 +00001485
1486 ulist_reinit(tmp);
Josef Bacikfcebe452014-05-13 17:30:47 -07001487 ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg),
1488 GFP_ATOMIC);
1489 if (ret < 0)
1490 return ret;
1491 ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg), GFP_ATOMIC);
Jan Schmidt46b665c2013-04-25 16:04:50 +00001492 if (ret < 0)
1493 return ret;
1494 ULIST_ITER_INIT(&tmp_uiter);
1495 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1496 struct btrfs_qgroup_list *glist;
1497
Josef Bacikfcebe452014-05-13 17:30:47 -07001498 qg = u64_to_ptr(tmp_unode->aux);
1499 /*
1500 * We use this sequence number to keep from having to
1501 * run the whole list and 0 out the refcnt every time.
1502 * We basically use sequnce as the known 0 count and
1503 * then add 1 everytime we see a qgroup. This is how we
1504 * get how many of the roots actually point up to the
1505 * upper level qgroups in order to determine exclusive
1506 * counts.
1507 *
1508 * For rescan we want to set old_refcnt to seq so our
1509 * exclusive calculations end up correct.
1510 */
1511 if (rescan)
1512 qg->old_refcnt = seq;
1513 else if (qg->old_refcnt < seq)
1514 qg->old_refcnt = seq + 1;
Jan Schmidt46b665c2013-04-25 16:04:50 +00001515 else
Josef Bacikfcebe452014-05-13 17:30:47 -07001516 qg->old_refcnt++;
Jan Schmidt46b665c2013-04-25 16:04:50 +00001517
Josef Bacikfcebe452014-05-13 17:30:47 -07001518 if (qg->new_refcnt < seq)
1519 qg->new_refcnt = seq + 1;
1520 else
1521 qg->new_refcnt++;
Jan Schmidt46b665c2013-04-25 16:04:50 +00001522 list_for_each_entry(glist, &qg->groups, next_group) {
Josef Bacikfcebe452014-05-13 17:30:47 -07001523 ret = ulist_add(qgroups, glist->group->qgroupid,
1524 ptr_to_u64(glist->group),
1525 GFP_ATOMIC);
1526 if (ret < 0)
1527 return ret;
Jan Schmidt46b665c2013-04-25 16:04:50 +00001528 ret = ulist_add(tmp, glist->group->qgroupid,
Josef Bacikfcebe452014-05-13 17:30:47 -07001529 ptr_to_u64(glist->group),
Jan Schmidt46b665c2013-04-25 16:04:50 +00001530 GFP_ATOMIC);
1531 if (ret < 0)
1532 return ret;
1533 }
1534 }
1535 }
Jan Schmidt46b665c2013-04-25 16:04:50 +00001536 return 0;
1537}
1538
Josef Bacikfcebe452014-05-13 17:30:47 -07001539/*
1540 * We need to walk forward in our operation tree and account for any roots that
1541 * were deleted after we made this operation.
1542 */
1543static int qgroup_account_deleted_refs(struct btrfs_fs_info *fs_info,
1544 struct btrfs_qgroup_operation *oper,
1545 struct ulist *tmp,
1546 struct ulist *qgroups, u64 seq,
1547 int *old_roots)
Jan Schmidt46b665c2013-04-25 16:04:50 +00001548{
1549 struct ulist_node *unode;
1550 struct ulist_iterator uiter;
1551 struct btrfs_qgroup *qg;
Josef Bacikfcebe452014-05-13 17:30:47 -07001552 struct btrfs_qgroup_operation *tmp_oper;
1553 struct rb_node *n;
Jan Schmidt46b665c2013-04-25 16:04:50 +00001554 int ret;
1555
1556 ulist_reinit(tmp);
Jan Schmidt46b665c2013-04-25 16:04:50 +00001557
Josef Bacikfcebe452014-05-13 17:30:47 -07001558 /*
1559 * We only walk forward in the tree since we're only interested in
1560 * removals that happened _after_ our operation.
1561 */
1562 spin_lock(&fs_info->qgroup_op_lock);
1563 n = rb_next(&oper->n);
1564 spin_unlock(&fs_info->qgroup_op_lock);
1565 if (!n)
1566 return 0;
1567 tmp_oper = rb_entry(n, struct btrfs_qgroup_operation, n);
1568 while (tmp_oper->bytenr == oper->bytenr) {
1569 /*
1570 * If it's not a removal we don't care, additions work out
1571 * properly with our refcnt tracking.
1572 */
1573 if (tmp_oper->type != BTRFS_QGROUP_OPER_SUB_SHARED &&
1574 tmp_oper->type != BTRFS_QGROUP_OPER_SUB_EXCL)
1575 goto next;
1576 qg = find_qgroup_rb(fs_info, tmp_oper->ref_root);
1577 if (!qg)
1578 goto next;
1579 ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg),
1580 GFP_ATOMIC);
1581 if (ret) {
1582 if (ret < 0)
1583 return ret;
1584 /*
1585 * We only want to increase old_roots if this qgroup is
1586 * not already in the list of qgroups. If it is already
1587 * there then that means it must have been re-added or
1588 * the delete will be discarded because we had an
1589 * existing ref that we haven't looked up yet. In this
1590 * case we don't want to increase old_roots. So if ret
1591 * == 1 then we know that this is the first time we've
1592 * seen this qgroup and we can bump the old_roots.
1593 */
1594 (*old_roots)++;
1595 ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg),
1596 GFP_ATOMIC);
1597 if (ret < 0)
1598 return ret;
1599 }
1600next:
1601 spin_lock(&fs_info->qgroup_op_lock);
1602 n = rb_next(&tmp_oper->n);
1603 spin_unlock(&fs_info->qgroup_op_lock);
1604 if (!n)
1605 break;
1606 tmp_oper = rb_entry(n, struct btrfs_qgroup_operation, n);
1607 }
1608
1609 /* Ok now process the qgroups we found */
Jan Schmidt46b665c2013-04-25 16:04:50 +00001610 ULIST_ITER_INIT(&uiter);
1611 while ((unode = ulist_next(tmp, &uiter))) {
Josef Bacikfcebe452014-05-13 17:30:47 -07001612 struct btrfs_qgroup_list *glist;
Jan Schmidt46b665c2013-04-25 16:04:50 +00001613
Josef Bacikfcebe452014-05-13 17:30:47 -07001614 qg = u64_to_ptr(unode->aux);
1615 if (qg->old_refcnt < seq)
1616 qg->old_refcnt = seq + 1;
1617 else
1618 qg->old_refcnt++;
1619 if (qg->new_refcnt < seq)
1620 qg->new_refcnt = seq + 1;
1621 else
1622 qg->new_refcnt++;
Jan Schmidt46b665c2013-04-25 16:04:50 +00001623 list_for_each_entry(glist, &qg->groups, next_group) {
Josef Bacikfcebe452014-05-13 17:30:47 -07001624 ret = ulist_add(qgroups, glist->group->qgroupid,
1625 ptr_to_u64(glist->group), GFP_ATOMIC);
1626 if (ret < 0)
1627 return ret;
Jan Schmidt46b665c2013-04-25 16:04:50 +00001628 ret = ulist_add(tmp, glist->group->qgroupid,
Josef Bacikfcebe452014-05-13 17:30:47 -07001629 ptr_to_u64(glist->group), GFP_ATOMIC);
Jan Schmidt46b665c2013-04-25 16:04:50 +00001630 if (ret < 0)
1631 return ret;
1632 }
1633 }
Jan Schmidt46b665c2013-04-25 16:04:50 +00001634 return 0;
1635}
1636
Josef Bacikfcebe452014-05-13 17:30:47 -07001637/* Add refcnt for the newly added reference. */
1638static int qgroup_calc_new_refcnt(struct btrfs_fs_info *fs_info,
1639 struct btrfs_qgroup_operation *oper,
1640 struct btrfs_qgroup *qgroup,
1641 struct ulist *tmp, struct ulist *qgroups,
1642 u64 seq)
Jan Schmidt46b665c2013-04-25 16:04:50 +00001643{
1644 struct ulist_node *unode;
1645 struct ulist_iterator uiter;
1646 struct btrfs_qgroup *qg;
Jan Schmidt46b665c2013-04-25 16:04:50 +00001647 int ret;
1648
Josef Bacikfcebe452014-05-13 17:30:47 -07001649 ulist_reinit(tmp);
1650 ret = ulist_add(qgroups, qgroup->qgroupid, ptr_to_u64(qgroup),
1651 GFP_ATOMIC);
1652 if (ret < 0)
1653 return ret;
1654 ret = ulist_add(tmp, qgroup->qgroupid, ptr_to_u64(qgroup),
1655 GFP_ATOMIC);
1656 if (ret < 0)
1657 return ret;
1658 ULIST_ITER_INIT(&uiter);
1659 while ((unode = ulist_next(tmp, &uiter))) {
1660 struct btrfs_qgroup_list *glist;
1661
1662 qg = u64_to_ptr(unode->aux);
1663 if (oper->type == BTRFS_QGROUP_OPER_ADD_SHARED) {
1664 if (qg->new_refcnt < seq)
1665 qg->new_refcnt = seq + 1;
1666 else
1667 qg->new_refcnt++;
1668 } else {
1669 if (qg->old_refcnt < seq)
1670 qg->old_refcnt = seq + 1;
1671 else
1672 qg->old_refcnt++;
1673 }
1674 list_for_each_entry(glist, &qg->groups, next_group) {
1675 ret = ulist_add(tmp, glist->group->qgroupid,
1676 ptr_to_u64(glist->group), GFP_ATOMIC);
1677 if (ret < 0)
1678 return ret;
1679 ret = ulist_add(qgroups, glist->group->qgroupid,
1680 ptr_to_u64(glist->group), GFP_ATOMIC);
1681 if (ret < 0)
1682 return ret;
1683 }
1684 }
1685 return 0;
1686}
1687
1688/*
1689 * This adjusts the counters for all referenced qgroups if need be.
1690 */
1691static int qgroup_adjust_counters(struct btrfs_fs_info *fs_info,
1692 u64 root_to_skip, u64 num_bytes,
1693 struct ulist *qgroups, u64 seq,
1694 int old_roots, int new_roots, int rescan)
1695{
1696 struct ulist_node *unode;
1697 struct ulist_iterator uiter;
1698 struct btrfs_qgroup *qg;
1699 u64 cur_new_count, cur_old_count;
1700
1701 ULIST_ITER_INIT(&uiter);
1702 while ((unode = ulist_next(qgroups, &uiter))) {
1703 bool dirty = false;
1704
1705 qg = u64_to_ptr(unode->aux);
1706 /*
1707 * Wasn't referenced before but is now, add to the reference
1708 * counters.
1709 */
1710 if (qg->old_refcnt <= seq && qg->new_refcnt > seq) {
1711 qg->rfer += num_bytes;
1712 qg->rfer_cmpr += num_bytes;
1713 dirty = true;
1714 }
1715
1716 /*
1717 * Was referenced before but isn't now, subtract from the
1718 * reference counters.
1719 */
1720 if (qg->old_refcnt > seq && qg->new_refcnt <= seq) {
1721 qg->rfer -= num_bytes;
1722 qg->rfer_cmpr -= num_bytes;
1723 dirty = true;
1724 }
1725
1726 if (qg->old_refcnt < seq)
1727 cur_old_count = 0;
1728 else
1729 cur_old_count = qg->old_refcnt - seq;
1730 if (qg->new_refcnt < seq)
1731 cur_new_count = 0;
1732 else
1733 cur_new_count = qg->new_refcnt - seq;
1734
1735 /*
1736 * If our refcount was the same as the roots previously but our
1737 * new count isn't the same as the number of roots now then we
1738 * went from having a exclusive reference on this range to not.
1739 */
1740 if (old_roots && cur_old_count == old_roots &&
1741 (cur_new_count != new_roots || new_roots == 0)) {
1742 WARN_ON(cur_new_count != new_roots && new_roots == 0);
1743 qg->excl -= num_bytes;
1744 qg->excl_cmpr -= num_bytes;
1745 dirty = true;
1746 }
1747
1748 /*
1749 * If we didn't reference all the roots before but now we do we
1750 * have an exclusive reference to this range.
1751 */
1752 if ((!old_roots || (old_roots && cur_old_count != old_roots))
1753 && cur_new_count == new_roots) {
1754 qg->excl += num_bytes;
1755 qg->excl_cmpr += num_bytes;
1756 dirty = true;
1757 }
1758
1759 if (dirty)
1760 qgroup_dirty(fs_info, qg);
1761 }
1762 return 0;
1763}
1764
1765/*
1766 * If we removed a data extent and there were other references for that bytenr
1767 * then we need to lookup all referenced roots to make sure we still don't
1768 * reference this bytenr. If we do then we can just discard this operation.
1769 */
1770static int check_existing_refs(struct btrfs_trans_handle *trans,
1771 struct btrfs_fs_info *fs_info,
1772 struct btrfs_qgroup_operation *oper)
1773{
1774 struct ulist *roots = NULL;
1775 struct ulist_node *unode;
1776 struct ulist_iterator uiter;
1777 int ret = 0;
1778
1779 ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr,
1780 oper->elem.seq, &roots);
1781 if (ret < 0)
1782 return ret;
1783 ret = 0;
1784
Jan Schmidt46b665c2013-04-25 16:04:50 +00001785 ULIST_ITER_INIT(&uiter);
1786 while ((unode = ulist_next(roots, &uiter))) {
Josef Bacikfcebe452014-05-13 17:30:47 -07001787 if (unode->val == oper->ref_root) {
1788 ret = 1;
1789 break;
Jan Schmidt46b665c2013-04-25 16:04:50 +00001790 }
1791 }
Josef Bacikfcebe452014-05-13 17:30:47 -07001792 ulist_free(roots);
1793 btrfs_put_tree_mod_seq(fs_info, &oper->elem);
Jan Schmidt46b665c2013-04-25 16:04:50 +00001794
Josef Bacikfcebe452014-05-13 17:30:47 -07001795 return ret;
1796}
1797
1798/*
1799 * If we share a reference across multiple roots then we may need to adjust
1800 * various qgroups referenced and exclusive counters. The basic premise is this
1801 *
1802 * 1) We have seq to represent a 0 count. Instead of looping through all of the
1803 * qgroups and resetting their refcount to 0 we just constantly bump this
1804 * sequence number to act as the base reference count. This means that if
1805 * anybody is equal to or below this sequence they were never referenced. We
1806 * jack this sequence up by the number of roots we found each time in order to
1807 * make sure we don't have any overlap.
1808 *
1809 * 2) We first search all the roots that reference the area _except_ the root
1810 * we're acting on currently. This makes up the old_refcnt of all the qgroups
1811 * before.
1812 *
1813 * 3) We walk all of the qgroups referenced by the root we are currently acting
1814 * on, and will either adjust old_refcnt in the case of a removal or the
1815 * new_refcnt in the case of an addition.
1816 *
1817 * 4) Finally we walk all the qgroups that are referenced by this range
1818 * including the root we are acting on currently. We will adjust the counters
1819 * based on the number of roots we had and will have after this operation.
1820 *
1821 * Take this example as an illustration
1822 *
1823 * [qgroup 1/0]
1824 * / | \
1825 * [qg 0/0] [qg 0/1] [qg 0/2]
1826 * \ | /
1827 * [ extent ]
1828 *
1829 * Say we are adding a reference that is covered by qg 0/0. The first step
1830 * would give a refcnt of 1 to qg 0/1 and 0/2 and a refcnt of 2 to qg 1/0 with
1831 * old_roots being 2. Because it is adding new_roots will be 1. We then go
1832 * through qg 0/0 which will get the new_refcnt set to 1 and add 1 to qg 1/0's
1833 * new_refcnt, bringing it to 3. We then walk through all of the qgroups, we
1834 * notice that the old refcnt for qg 0/0 < the new refcnt, so we added a
1835 * reference and thus must add the size to the referenced bytes. Everything
1836 * else is the same so nothing else changes.
1837 */
1838static int qgroup_shared_accounting(struct btrfs_trans_handle *trans,
1839 struct btrfs_fs_info *fs_info,
1840 struct btrfs_qgroup_operation *oper)
1841{
1842 struct ulist *roots = NULL;
1843 struct ulist *qgroups, *tmp;
1844 struct btrfs_qgroup *qgroup;
1845 struct seq_list elem = {};
1846 u64 seq;
1847 int old_roots = 0;
1848 int new_roots = 0;
1849 int ret = 0;
1850
1851 if (oper->elem.seq) {
1852 ret = check_existing_refs(trans, fs_info, oper);
1853 if (ret < 0)
1854 return ret;
1855 if (ret)
1856 return 0;
1857 }
1858
1859 qgroups = ulist_alloc(GFP_NOFS);
1860 if (!qgroups)
1861 return -ENOMEM;
1862
1863 tmp = ulist_alloc(GFP_NOFS);
Eric Sandeend7372782014-06-12 00:14:59 -05001864 if (!tmp) {
1865 ulist_free(qgroups);
Josef Bacikfcebe452014-05-13 17:30:47 -07001866 return -ENOMEM;
Eric Sandeend7372782014-06-12 00:14:59 -05001867 }
Josef Bacikfcebe452014-05-13 17:30:47 -07001868
1869 btrfs_get_tree_mod_seq(fs_info, &elem);
1870 ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr, elem.seq,
1871 &roots);
1872 btrfs_put_tree_mod_seq(fs_info, &elem);
1873 if (ret < 0) {
1874 ulist_free(qgroups);
1875 ulist_free(tmp);
1876 return ret;
1877 }
1878 spin_lock(&fs_info->qgroup_lock);
1879 qgroup = find_qgroup_rb(fs_info, oper->ref_root);
1880 if (!qgroup)
1881 goto out;
1882 seq = fs_info->qgroup_seq;
1883
1884 /*
1885 * So roots is the list of all the roots currently pointing at the
1886 * bytenr, including the ref we are adding if we are adding, or not if
1887 * we are removing a ref. So we pass in the ref_root to skip that root
1888 * in our calculations. We set old_refnct and new_refcnt cause who the
1889 * hell knows what everything looked like before, and it doesn't matter
1890 * except...
1891 */
1892 ret = qgroup_calc_old_refcnt(fs_info, oper->ref_root, tmp, roots, qgroups,
1893 seq, &old_roots, 0);
1894 if (ret < 0)
1895 goto out;
1896
1897 /*
1898 * Now adjust the refcounts of the qgroups that care about this
1899 * reference, either the old_count in the case of removal or new_count
1900 * in the case of an addition.
1901 */
1902 ret = qgroup_calc_new_refcnt(fs_info, oper, qgroup, tmp, qgroups,
1903 seq);
1904 if (ret < 0)
1905 goto out;
1906
1907 /*
1908 * ...in the case of removals. If we had a removal before we got around
1909 * to processing this operation then we need to find that guy and count
1910 * his references as if they really existed so we don't end up screwing
1911 * up the exclusive counts. Then whenever we go to process the delete
1912 * everything will be grand and we can account for whatever exclusive
1913 * changes need to be made there. We also have to pass in old_roots so
1914 * we have an accurate count of the roots as it pertains to this
1915 * operations view of the world.
1916 */
1917 ret = qgroup_account_deleted_refs(fs_info, oper, tmp, qgroups, seq,
1918 &old_roots);
1919 if (ret < 0)
1920 goto out;
1921
1922 /*
1923 * We are adding our root, need to adjust up the number of roots,
1924 * otherwise old_roots is the number of roots we want.
1925 */
1926 if (oper->type == BTRFS_QGROUP_OPER_ADD_SHARED) {
1927 new_roots = old_roots + 1;
1928 } else {
1929 new_roots = old_roots;
1930 old_roots++;
1931 }
1932 fs_info->qgroup_seq += old_roots + 1;
1933
1934
1935 /*
1936 * And now the magic happens, bless Arne for having a pretty elegant
1937 * solution for this.
1938 */
1939 qgroup_adjust_counters(fs_info, oper->ref_root, oper->num_bytes,
1940 qgroups, seq, old_roots, new_roots, 0);
1941out:
1942 spin_unlock(&fs_info->qgroup_lock);
1943 ulist_free(qgroups);
1944 ulist_free(roots);
1945 ulist_free(tmp);
1946 return ret;
Jan Schmidt46b665c2013-04-25 16:04:50 +00001947}
1948
Arne Jansenbed92ea2012-06-28 18:03:02 +02001949/*
Mark Fasheh11526512014-07-17 12:39:01 -07001950 * Process a reference to a shared subtree. This type of operation is
1951 * queued during snapshot removal when we encounter extents which are
1952 * shared between more than one root.
1953 */
1954static int qgroup_subtree_accounting(struct btrfs_trans_handle *trans,
1955 struct btrfs_fs_info *fs_info,
1956 struct btrfs_qgroup_operation *oper)
1957{
1958 struct ulist *roots = NULL;
1959 struct ulist_node *unode;
1960 struct ulist_iterator uiter;
1961 struct btrfs_qgroup_list *glist;
1962 struct ulist *parents;
1963 int ret = 0;
Mark Fashehf90e5792014-07-17 12:39:04 -07001964 int err;
Mark Fasheh11526512014-07-17 12:39:01 -07001965 struct btrfs_qgroup *qg;
1966 u64 root_obj = 0;
1967 struct seq_list elem = {};
1968
1969 parents = ulist_alloc(GFP_NOFS);
1970 if (!parents)
1971 return -ENOMEM;
1972
1973 btrfs_get_tree_mod_seq(fs_info, &elem);
1974 ret = btrfs_find_all_roots(trans, fs_info, oper->bytenr,
1975 elem.seq, &roots);
1976 btrfs_put_tree_mod_seq(fs_info, &elem);
1977 if (ret < 0)
Eric Sandeena3c10892014-08-17 15:09:21 -05001978 goto out;
Mark Fasheh11526512014-07-17 12:39:01 -07001979
1980 if (roots->nnodes != 1)
1981 goto out;
1982
1983 ULIST_ITER_INIT(&uiter);
1984 unode = ulist_next(roots, &uiter); /* Only want 1 so no need to loop */
1985 /*
1986 * If we find our ref root then that means all refs
1987 * this extent has to the root have not yet been
1988 * deleted. In that case, we do nothing and let the
1989 * last ref for this bytenr drive our update.
1990 *
1991 * This can happen for example if an extent is
1992 * referenced multiple times in a snapshot (clone,
1993 * etc). If we are in the middle of snapshot removal,
1994 * queued updates for such an extent will find the
1995 * root if we have not yet finished removing the
1996 * snapshot.
1997 */
1998 if (unode->val == oper->ref_root)
1999 goto out;
2000
2001 root_obj = unode->val;
2002 BUG_ON(!root_obj);
2003
2004 spin_lock(&fs_info->qgroup_lock);
2005 qg = find_qgroup_rb(fs_info, root_obj);
2006 if (!qg)
2007 goto out_unlock;
2008
2009 qg->excl += oper->num_bytes;
2010 qg->excl_cmpr += oper->num_bytes;
2011 qgroup_dirty(fs_info, qg);
2012
2013 /*
2014 * Adjust counts for parent groups. First we find all
2015 * parents, then in the 2nd loop we do the adjustment
2016 * while adding parents of the parents to our ulist.
2017 */
2018 list_for_each_entry(glist, &qg->groups, next_group) {
Mark Fashehf90e5792014-07-17 12:39:04 -07002019 err = ulist_add(parents, glist->group->qgroupid,
Mark Fasheh11526512014-07-17 12:39:01 -07002020 ptr_to_u64(glist->group), GFP_ATOMIC);
Mark Fashehf90e5792014-07-17 12:39:04 -07002021 if (err < 0) {
2022 ret = err;
Mark Fasheh11526512014-07-17 12:39:01 -07002023 goto out_unlock;
Mark Fashehf90e5792014-07-17 12:39:04 -07002024 }
Mark Fasheh11526512014-07-17 12:39:01 -07002025 }
2026
2027 ULIST_ITER_INIT(&uiter);
2028 while ((unode = ulist_next(parents, &uiter))) {
2029 qg = u64_to_ptr(unode->aux);
2030 qg->excl += oper->num_bytes;
2031 qg->excl_cmpr += oper->num_bytes;
2032 qgroup_dirty(fs_info, qg);
2033
2034 /* Add any parents of the parents */
2035 list_for_each_entry(glist, &qg->groups, next_group) {
Mark Fashehf90e5792014-07-17 12:39:04 -07002036 err = ulist_add(parents, glist->group->qgroupid,
Mark Fasheh11526512014-07-17 12:39:01 -07002037 ptr_to_u64(glist->group), GFP_ATOMIC);
Mark Fashehf90e5792014-07-17 12:39:04 -07002038 if (err < 0) {
2039 ret = err;
Mark Fasheh11526512014-07-17 12:39:01 -07002040 goto out_unlock;
Mark Fashehf90e5792014-07-17 12:39:04 -07002041 }
Mark Fasheh11526512014-07-17 12:39:01 -07002042 }
2043 }
2044
2045out_unlock:
2046 spin_unlock(&fs_info->qgroup_lock);
2047
2048out:
2049 ulist_free(roots);
2050 ulist_free(parents);
2051 return ret;
2052}
2053
2054/*
Arne Jansenbed92ea2012-06-28 18:03:02 +02002055 * btrfs_qgroup_account_ref is called for every ref that is added to or deleted
2056 * from the fs. First, all roots referencing the extent are searched, and
2057 * then the space is accounted accordingly to the different roots. The
2058 * accounting algorithm works in 3 steps documented inline.
2059 */
Josef Bacikfcebe452014-05-13 17:30:47 -07002060static int btrfs_qgroup_account(struct btrfs_trans_handle *trans,
2061 struct btrfs_fs_info *fs_info,
2062 struct btrfs_qgroup_operation *oper)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002063{
Arne Jansenbed92ea2012-06-28 18:03:02 +02002064 int ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002065
2066 if (!fs_info->quota_enabled)
2067 return 0;
2068
2069 BUG_ON(!fs_info->quota_root);
2070
Jan Schmidt2f232032013-04-25 16:04:51 +00002071 mutex_lock(&fs_info->qgroup_rescan_lock);
2072 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Josef Bacikfcebe452014-05-13 17:30:47 -07002073 if (fs_info->qgroup_rescan_progress.objectid <= oper->bytenr) {
Jan Schmidt2f232032013-04-25 16:04:51 +00002074 mutex_unlock(&fs_info->qgroup_rescan_lock);
2075 return 0;
2076 }
2077 }
2078 mutex_unlock(&fs_info->qgroup_rescan_lock);
2079
Josef Bacikfcebe452014-05-13 17:30:47 -07002080 ASSERT(is_fstree(oper->ref_root));
Arne Jansenbed92ea2012-06-28 18:03:02 +02002081
Mark Fashehd3982102014-07-17 12:39:00 -07002082 trace_btrfs_qgroup_account(oper);
2083
Josef Bacikfcebe452014-05-13 17:30:47 -07002084 switch (oper->type) {
2085 case BTRFS_QGROUP_OPER_ADD_EXCL:
2086 case BTRFS_QGROUP_OPER_SUB_EXCL:
2087 ret = qgroup_excl_accounting(fs_info, oper);
2088 break;
2089 case BTRFS_QGROUP_OPER_ADD_SHARED:
2090 case BTRFS_QGROUP_OPER_SUB_SHARED:
2091 ret = qgroup_shared_accounting(trans, fs_info, oper);
2092 break;
Mark Fasheh11526512014-07-17 12:39:01 -07002093 case BTRFS_QGROUP_OPER_SUB_SUBTREE:
2094 ret = qgroup_subtree_accounting(trans, fs_info, oper);
2095 break;
Josef Bacikfcebe452014-05-13 17:30:47 -07002096 default:
2097 ASSERT(0);
2098 }
2099 return ret;
2100}
Jan Schmidt2f232032013-04-25 16:04:51 +00002101
Josef Bacikfcebe452014-05-13 17:30:47 -07002102/*
2103 * Needs to be called everytime we run delayed refs, even if there is an error
2104 * in order to cleanup outstanding operations.
2105 */
2106int btrfs_delayed_qgroup_accounting(struct btrfs_trans_handle *trans,
2107 struct btrfs_fs_info *fs_info)
2108{
2109 struct btrfs_qgroup_operation *oper;
2110 int ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002111
Josef Bacikfcebe452014-05-13 17:30:47 -07002112 while (!list_empty(&trans->qgroup_ref_list)) {
2113 oper = list_first_entry(&trans->qgroup_ref_list,
2114 struct btrfs_qgroup_operation, list);
2115 list_del_init(&oper->list);
2116 if (!ret || !trans->aborted)
2117 ret = btrfs_qgroup_account(trans, fs_info, oper);
2118 spin_lock(&fs_info->qgroup_op_lock);
2119 rb_erase(&oper->n, &fs_info->qgroup_op_tree);
2120 spin_unlock(&fs_info->qgroup_op_lock);
2121 btrfs_put_tree_mod_seq(fs_info, &oper->elem);
2122 kfree(oper);
2123 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002124 return ret;
2125}
2126
2127/*
2128 * called from commit_transaction. Writes all changed qgroups to disk.
2129 */
2130int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
2131 struct btrfs_fs_info *fs_info)
2132{
2133 struct btrfs_root *quota_root = fs_info->quota_root;
2134 int ret = 0;
Jan Schmidt3d7b5a22013-04-25 16:04:52 +00002135 int start_rescan_worker = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002136
2137 if (!quota_root)
2138 goto out;
2139
Jan Schmidt3d7b5a22013-04-25 16:04:52 +00002140 if (!fs_info->quota_enabled && fs_info->pending_quota_state)
2141 start_rescan_worker = 1;
2142
Arne Jansenbed92ea2012-06-28 18:03:02 +02002143 fs_info->quota_enabled = fs_info->pending_quota_state;
2144
2145 spin_lock(&fs_info->qgroup_lock);
2146 while (!list_empty(&fs_info->dirty_qgroups)) {
2147 struct btrfs_qgroup *qgroup;
2148 qgroup = list_first_entry(&fs_info->dirty_qgroups,
2149 struct btrfs_qgroup, dirty);
2150 list_del_init(&qgroup->dirty);
2151 spin_unlock(&fs_info->qgroup_lock);
2152 ret = update_qgroup_info_item(trans, quota_root, qgroup);
2153 if (ret)
2154 fs_info->qgroup_flags |=
2155 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2156 spin_lock(&fs_info->qgroup_lock);
2157 }
2158 if (fs_info->quota_enabled)
2159 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
2160 else
2161 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
2162 spin_unlock(&fs_info->qgroup_lock);
2163
2164 ret = update_qgroup_status_item(trans, fs_info, quota_root);
2165 if (ret)
2166 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2167
Jan Schmidt3d7b5a22013-04-25 16:04:52 +00002168 if (!ret && start_rescan_worker) {
Jan Schmidtb382a322013-05-28 15:47:24 +00002169 ret = qgroup_rescan_init(fs_info, 0, 1);
2170 if (!ret) {
2171 qgroup_rescan_zero_tracking(fs_info);
Qu Wenruofc97fab2014-02-28 10:46:16 +08002172 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2173 &fs_info->qgroup_rescan_work);
Jan Schmidtb382a322013-05-28 15:47:24 +00002174 }
Jan Schmidt3d7b5a22013-04-25 16:04:52 +00002175 ret = 0;
2176 }
2177
Arne Jansenbed92ea2012-06-28 18:03:02 +02002178out:
2179
2180 return ret;
2181}
2182
2183/*
2184 * copy the acounting information between qgroups. This is necessary when a
2185 * snapshot or a subvolume is created
2186 */
2187int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
2188 struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
2189 struct btrfs_qgroup_inherit *inherit)
2190{
2191 int ret = 0;
2192 int i;
2193 u64 *i_qgroups;
2194 struct btrfs_root *quota_root = fs_info->quota_root;
2195 struct btrfs_qgroup *srcgroup;
2196 struct btrfs_qgroup *dstgroup;
2197 u32 level_size = 0;
Wang Shilong3f5e2d32013-04-07 10:50:19 +00002198 u64 nums;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002199
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002200 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002201 if (!fs_info->quota_enabled)
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002202 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002203
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002204 if (!quota_root) {
2205 ret = -EINVAL;
2206 goto out;
2207 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002208
Wang Shilong3f5e2d32013-04-07 10:50:19 +00002209 if (inherit) {
2210 i_qgroups = (u64 *)(inherit + 1);
2211 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
2212 2 * inherit->num_excl_copies;
2213 for (i = 0; i < nums; ++i) {
2214 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
2215 if (!srcgroup) {
2216 ret = -EINVAL;
2217 goto out;
2218 }
2219 ++i_qgroups;
2220 }
2221 }
2222
Arne Jansenbed92ea2012-06-28 18:03:02 +02002223 /*
2224 * create a tracking group for the subvol itself
2225 */
2226 ret = add_qgroup_item(trans, quota_root, objectid);
2227 if (ret)
2228 goto out;
2229
2230 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
2231 ret = update_qgroup_limit_item(trans, quota_root, objectid,
2232 inherit->lim.flags,
2233 inherit->lim.max_rfer,
2234 inherit->lim.max_excl,
2235 inherit->lim.rsv_rfer,
2236 inherit->lim.rsv_excl);
2237 if (ret)
2238 goto out;
2239 }
2240
2241 if (srcid) {
2242 struct btrfs_root *srcroot;
2243 struct btrfs_key srckey;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002244
2245 srckey.objectid = srcid;
2246 srckey.type = BTRFS_ROOT_ITEM_KEY;
2247 srckey.offset = (u64)-1;
2248 srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
2249 if (IS_ERR(srcroot)) {
2250 ret = PTR_ERR(srcroot);
2251 goto out;
2252 }
2253
2254 rcu_read_lock();
David Sterba707e8a02014-06-04 19:22:26 +02002255 level_size = srcroot->nodesize;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002256 rcu_read_unlock();
2257 }
2258
2259 /*
2260 * add qgroup to all inherited groups
2261 */
2262 if (inherit) {
2263 i_qgroups = (u64 *)(inherit + 1);
2264 for (i = 0; i < inherit->num_qgroups; ++i) {
2265 ret = add_qgroup_relation_item(trans, quota_root,
2266 objectid, *i_qgroups);
2267 if (ret)
2268 goto out;
2269 ret = add_qgroup_relation_item(trans, quota_root,
2270 *i_qgroups, objectid);
2271 if (ret)
2272 goto out;
2273 ++i_qgroups;
2274 }
2275 }
2276
2277
2278 spin_lock(&fs_info->qgroup_lock);
2279
2280 dstgroup = add_qgroup_rb(fs_info, objectid);
Dan Carpenter57a5a882012-07-30 02:15:43 -06002281 if (IS_ERR(dstgroup)) {
2282 ret = PTR_ERR(dstgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002283 goto unlock;
Dan Carpenter57a5a882012-07-30 02:15:43 -06002284 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002285
2286 if (srcid) {
2287 srcgroup = find_qgroup_rb(fs_info, srcid);
Chris Masonf3a87f12012-09-14 20:06:30 -04002288 if (!srcgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002289 goto unlock;
Josef Bacikfcebe452014-05-13 17:30:47 -07002290
2291 /*
2292 * We call inherit after we clone the root in order to make sure
2293 * our counts don't go crazy, so at this point the only
2294 * difference between the two roots should be the root node.
2295 */
2296 dstgroup->rfer = srcgroup->rfer;
2297 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
2298 dstgroup->excl = level_size;
2299 dstgroup->excl_cmpr = level_size;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002300 srcgroup->excl = level_size;
2301 srcgroup->excl_cmpr = level_size;
2302 qgroup_dirty(fs_info, dstgroup);
2303 qgroup_dirty(fs_info, srcgroup);
2304 }
2305
Chris Masonf3a87f12012-09-14 20:06:30 -04002306 if (!inherit)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002307 goto unlock;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002308
2309 i_qgroups = (u64 *)(inherit + 1);
2310 for (i = 0; i < inherit->num_qgroups; ++i) {
2311 ret = add_relation_rb(quota_root->fs_info, objectid,
2312 *i_qgroups);
2313 if (ret)
2314 goto unlock;
2315 ++i_qgroups;
2316 }
2317
2318 for (i = 0; i < inherit->num_ref_copies; ++i) {
2319 struct btrfs_qgroup *src;
2320 struct btrfs_qgroup *dst;
2321
2322 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2323 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2324
2325 if (!src || !dst) {
2326 ret = -EINVAL;
2327 goto unlock;
2328 }
2329
2330 dst->rfer = src->rfer - level_size;
2331 dst->rfer_cmpr = src->rfer_cmpr - level_size;
2332 i_qgroups += 2;
2333 }
2334 for (i = 0; i < inherit->num_excl_copies; ++i) {
2335 struct btrfs_qgroup *src;
2336 struct btrfs_qgroup *dst;
2337
2338 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2339 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2340
2341 if (!src || !dst) {
2342 ret = -EINVAL;
2343 goto unlock;
2344 }
2345
2346 dst->excl = src->excl + level_size;
2347 dst->excl_cmpr = src->excl_cmpr + level_size;
2348 i_qgroups += 2;
2349 }
2350
2351unlock:
2352 spin_unlock(&fs_info->qgroup_lock);
2353out:
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002354 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002355 return ret;
2356}
2357
2358/*
2359 * reserve some space for a qgroup and all its parents. The reservation takes
2360 * place with start_transaction or dealloc_reserve, similar to ENOSPC
2361 * accounting. If not enough space is available, EDQUOT is returned.
2362 * We assume that the requested space is new for all qgroups.
2363 */
2364int btrfs_qgroup_reserve(struct btrfs_root *root, u64 num_bytes)
2365{
2366 struct btrfs_root *quota_root;
2367 struct btrfs_qgroup *qgroup;
2368 struct btrfs_fs_info *fs_info = root->fs_info;
2369 u64 ref_root = root->root_key.objectid;
2370 int ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002371 struct ulist_node *unode;
2372 struct ulist_iterator uiter;
2373
2374 if (!is_fstree(ref_root))
2375 return 0;
2376
2377 if (num_bytes == 0)
2378 return 0;
2379
2380 spin_lock(&fs_info->qgroup_lock);
2381 quota_root = fs_info->quota_root;
2382 if (!quota_root)
2383 goto out;
2384
2385 qgroup = find_qgroup_rb(fs_info, ref_root);
2386 if (!qgroup)
2387 goto out;
2388
2389 /*
2390 * in a first step, we check all affected qgroups if any limits would
2391 * be exceeded
2392 */
Wang Shilong1e8f9152013-05-06 11:03:27 +00002393 ulist_reinit(fs_info->qgroup_ulist);
2394 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
Wang Shilong3c971852013-04-17 14:00:36 +00002395 (uintptr_t)qgroup, GFP_ATOMIC);
2396 if (ret < 0)
2397 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002398 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00002399 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002400 struct btrfs_qgroup *qg;
2401 struct btrfs_qgroup_list *glist;
2402
Josef Bacikfcebe452014-05-13 17:30:47 -07002403 qg = u64_to_ptr(unode->aux);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002404
2405 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
Wang Shilongb4fcd6b2013-04-15 12:56:49 +00002406 qg->reserved + (s64)qg->rfer + num_bytes >
Wang Shilong720f1e22013-03-06 11:51:47 +00002407 qg->max_rfer) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002408 ret = -EDQUOT;
Wang Shilong720f1e22013-03-06 11:51:47 +00002409 goto out;
2410 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002411
2412 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
Wang Shilongb4fcd6b2013-04-15 12:56:49 +00002413 qg->reserved + (s64)qg->excl + num_bytes >
Wang Shilong720f1e22013-03-06 11:51:47 +00002414 qg->max_excl) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002415 ret = -EDQUOT;
Wang Shilong720f1e22013-03-06 11:51:47 +00002416 goto out;
2417 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002418
2419 list_for_each_entry(glist, &qg->groups, next_group) {
Wang Shilong1e8f9152013-05-06 11:03:27 +00002420 ret = ulist_add(fs_info->qgroup_ulist,
2421 glist->group->qgroupid,
Wang Shilong3c971852013-04-17 14:00:36 +00002422 (uintptr_t)glist->group, GFP_ATOMIC);
2423 if (ret < 0)
2424 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002425 }
2426 }
Wang Shilong3c971852013-04-17 14:00:36 +00002427 ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002428 /*
2429 * no limits exceeded, now record the reservation into all qgroups
2430 */
2431 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00002432 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002433 struct btrfs_qgroup *qg;
2434
Josef Bacikfcebe452014-05-13 17:30:47 -07002435 qg = u64_to_ptr(unode->aux);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002436
2437 qg->reserved += num_bytes;
2438 }
2439
2440out:
2441 spin_unlock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002442 return ret;
2443}
2444
2445void btrfs_qgroup_free(struct btrfs_root *root, u64 num_bytes)
2446{
2447 struct btrfs_root *quota_root;
2448 struct btrfs_qgroup *qgroup;
2449 struct btrfs_fs_info *fs_info = root->fs_info;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002450 struct ulist_node *unode;
2451 struct ulist_iterator uiter;
2452 u64 ref_root = root->root_key.objectid;
Wang Shilong3c971852013-04-17 14:00:36 +00002453 int ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002454
2455 if (!is_fstree(ref_root))
2456 return;
2457
2458 if (num_bytes == 0)
2459 return;
2460
2461 spin_lock(&fs_info->qgroup_lock);
2462
2463 quota_root = fs_info->quota_root;
2464 if (!quota_root)
2465 goto out;
2466
2467 qgroup = find_qgroup_rb(fs_info, ref_root);
2468 if (!qgroup)
2469 goto out;
2470
Wang Shilong1e8f9152013-05-06 11:03:27 +00002471 ulist_reinit(fs_info->qgroup_ulist);
2472 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
Wang Shilong3c971852013-04-17 14:00:36 +00002473 (uintptr_t)qgroup, GFP_ATOMIC);
2474 if (ret < 0)
2475 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002476 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00002477 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002478 struct btrfs_qgroup *qg;
2479 struct btrfs_qgroup_list *glist;
2480
Josef Bacikfcebe452014-05-13 17:30:47 -07002481 qg = u64_to_ptr(unode->aux);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002482
2483 qg->reserved -= num_bytes;
2484
2485 list_for_each_entry(glist, &qg->groups, next_group) {
Wang Shilong1e8f9152013-05-06 11:03:27 +00002486 ret = ulist_add(fs_info->qgroup_ulist,
2487 glist->group->qgroupid,
Wang Shilong3c971852013-04-17 14:00:36 +00002488 (uintptr_t)glist->group, GFP_ATOMIC);
2489 if (ret < 0)
2490 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002491 }
2492 }
2493
2494out:
2495 spin_unlock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002496}
2497
2498void assert_qgroups_uptodate(struct btrfs_trans_handle *trans)
2499{
2500 if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq)
2501 return;
Frank Holtonefe120a2013-12-20 11:37:06 -05002502 btrfs_err(trans->root->fs_info,
2503 "qgroups not uptodate in trans handle %p: list is%s empty, "
2504 "seq is %#x.%x",
Arne Jansenbed92ea2012-06-28 18:03:02 +02002505 trans, list_empty(&trans->qgroup_ref_list) ? "" : " not",
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +00002506 (u32)(trans->delayed_ref_elem.seq >> 32),
2507 (u32)trans->delayed_ref_elem.seq);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002508 BUG();
2509}
Jan Schmidt2f232032013-04-25 16:04:51 +00002510
2511/*
2512 * returns < 0 on error, 0 when more leafs are to be scanned.
2513 * returns 1 when done, 2 when done and FLAG_INCONSISTENT was cleared.
2514 */
2515static int
Jan Schmidtb382a322013-05-28 15:47:24 +00002516qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
Josef Bacikfcebe452014-05-13 17:30:47 -07002517 struct btrfs_trans_handle *trans, struct ulist *qgroups,
2518 struct ulist *tmp, struct extent_buffer *scratch_leaf)
Jan Schmidt2f232032013-04-25 16:04:51 +00002519{
2520 struct btrfs_key found;
Jan Schmidt2f232032013-04-25 16:04:51 +00002521 struct ulist *roots = NULL;
Jan Schmidt2f232032013-04-25 16:04:51 +00002522 struct seq_list tree_mod_seq_elem = {};
Josef Bacikfcebe452014-05-13 17:30:47 -07002523 u64 num_bytes;
Jan Schmidt2f232032013-04-25 16:04:51 +00002524 u64 seq;
Josef Bacikfcebe452014-05-13 17:30:47 -07002525 int new_roots;
Jan Schmidt2f232032013-04-25 16:04:51 +00002526 int slot;
2527 int ret;
2528
2529 path->leave_spinning = 1;
2530 mutex_lock(&fs_info->qgroup_rescan_lock);
2531 ret = btrfs_search_slot_for_read(fs_info->extent_root,
2532 &fs_info->qgroup_rescan_progress,
2533 path, 1, 0);
2534
2535 pr_debug("current progress key (%llu %u %llu), search_slot ret %d\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002536 fs_info->qgroup_rescan_progress.objectid,
Jan Schmidt2f232032013-04-25 16:04:51 +00002537 fs_info->qgroup_rescan_progress.type,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002538 fs_info->qgroup_rescan_progress.offset, ret);
Jan Schmidt2f232032013-04-25 16:04:51 +00002539
2540 if (ret) {
2541 /*
2542 * The rescan is about to end, we will not be scanning any
2543 * further blocks. We cannot unset the RESCAN flag here, because
2544 * we want to commit the transaction if everything went well.
2545 * To make the live accounting work in this phase, we set our
2546 * scan progress pointer such that every real extent objectid
2547 * will be smaller.
2548 */
2549 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
2550 btrfs_release_path(path);
2551 mutex_unlock(&fs_info->qgroup_rescan_lock);
2552 return ret;
2553 }
2554
2555 btrfs_item_key_to_cpu(path->nodes[0], &found,
2556 btrfs_header_nritems(path->nodes[0]) - 1);
2557 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
2558
2559 btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
2560 memcpy(scratch_leaf, path->nodes[0], sizeof(*scratch_leaf));
2561 slot = path->slots[0];
2562 btrfs_release_path(path);
2563 mutex_unlock(&fs_info->qgroup_rescan_lock);
2564
2565 for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
2566 btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
Josef Bacik3a6d75e2014-01-23 16:45:10 -05002567 if (found.type != BTRFS_EXTENT_ITEM_KEY &&
2568 found.type != BTRFS_METADATA_ITEM_KEY)
Jan Schmidt2f232032013-04-25 16:04:51 +00002569 continue;
Josef Bacik3a6d75e2014-01-23 16:45:10 -05002570 if (found.type == BTRFS_METADATA_ITEM_KEY)
David Sterba707e8a02014-06-04 19:22:26 +02002571 num_bytes = fs_info->extent_root->nodesize;
Josef Bacik3a6d75e2014-01-23 16:45:10 -05002572 else
2573 num_bytes = found.offset;
2574
Josef Bacikfcebe452014-05-13 17:30:47 -07002575 ulist_reinit(qgroups);
2576 ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
2577 &roots);
Jan Schmidt2f232032013-04-25 16:04:51 +00002578 if (ret < 0)
2579 goto out;
2580 spin_lock(&fs_info->qgroup_lock);
2581 seq = fs_info->qgroup_seq;
2582 fs_info->qgroup_seq += roots->nnodes + 1; /* max refcnt */
2583
Josef Bacikfcebe452014-05-13 17:30:47 -07002584 new_roots = 0;
2585 ret = qgroup_calc_old_refcnt(fs_info, 0, tmp, roots, qgroups,
2586 seq, &new_roots, 1);
2587 if (ret < 0) {
Jan Schmidt2f232032013-04-25 16:04:51 +00002588 spin_unlock(&fs_info->qgroup_lock);
2589 ulist_free(roots);
2590 goto out;
2591 }
2592
Josef Bacikfcebe452014-05-13 17:30:47 -07002593 ret = qgroup_adjust_counters(fs_info, 0, num_bytes, qgroups,
2594 seq, 0, new_roots, 1);
2595 if (ret < 0) {
2596 spin_unlock(&fs_info->qgroup_lock);
2597 ulist_free(roots);
2598 goto out;
Jan Schmidt2f232032013-04-25 16:04:51 +00002599 }
Jan Schmidt2f232032013-04-25 16:04:51 +00002600 spin_unlock(&fs_info->qgroup_lock);
2601 ulist_free(roots);
Jan Schmidt2f232032013-04-25 16:04:51 +00002602 }
Jan Schmidt2f232032013-04-25 16:04:51 +00002603out:
2604 btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
2605
2606 return ret;
2607}
2608
Qu Wenruod458b052014-02-28 10:46:19 +08002609static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
Jan Schmidt2f232032013-04-25 16:04:51 +00002610{
Jan Schmidtb382a322013-05-28 15:47:24 +00002611 struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
2612 qgroup_rescan_work);
Jan Schmidt2f232032013-04-25 16:04:51 +00002613 struct btrfs_path *path;
2614 struct btrfs_trans_handle *trans = NULL;
Josef Bacikfcebe452014-05-13 17:30:47 -07002615 struct ulist *tmp = NULL, *qgroups = NULL;
Jan Schmidt2f232032013-04-25 16:04:51 +00002616 struct extent_buffer *scratch_leaf = NULL;
2617 int err = -ENOMEM;
2618
2619 path = btrfs_alloc_path();
2620 if (!path)
2621 goto out;
Josef Bacikfcebe452014-05-13 17:30:47 -07002622 qgroups = ulist_alloc(GFP_NOFS);
2623 if (!qgroups)
2624 goto out;
Jan Schmidt2f232032013-04-25 16:04:51 +00002625 tmp = ulist_alloc(GFP_NOFS);
2626 if (!tmp)
2627 goto out;
2628 scratch_leaf = kmalloc(sizeof(*scratch_leaf), GFP_NOFS);
2629 if (!scratch_leaf)
2630 goto out;
2631
2632 err = 0;
2633 while (!err) {
2634 trans = btrfs_start_transaction(fs_info->fs_root, 0);
2635 if (IS_ERR(trans)) {
2636 err = PTR_ERR(trans);
2637 break;
2638 }
2639 if (!fs_info->quota_enabled) {
2640 err = -EINTR;
2641 } else {
Jan Schmidtb382a322013-05-28 15:47:24 +00002642 err = qgroup_rescan_leaf(fs_info, path, trans,
Josef Bacikfcebe452014-05-13 17:30:47 -07002643 qgroups, tmp, scratch_leaf);
Jan Schmidt2f232032013-04-25 16:04:51 +00002644 }
2645 if (err > 0)
2646 btrfs_commit_transaction(trans, fs_info->fs_root);
2647 else
2648 btrfs_end_transaction(trans, fs_info->fs_root);
2649 }
2650
2651out:
2652 kfree(scratch_leaf);
Josef Bacikfcebe452014-05-13 17:30:47 -07002653 ulist_free(qgroups);
Josef Bacik2a108402014-05-20 09:23:31 -04002654 ulist_free(tmp);
Jan Schmidt2f232032013-04-25 16:04:51 +00002655 btrfs_free_path(path);
Jan Schmidt2f232032013-04-25 16:04:51 +00002656
2657 mutex_lock(&fs_info->qgroup_rescan_lock);
2658 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2659
2660 if (err == 2 &&
2661 fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
2662 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2663 } else if (err < 0) {
2664 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2665 }
2666 mutex_unlock(&fs_info->qgroup_rescan_lock);
2667
2668 if (err >= 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05002669 btrfs_info(fs_info, "qgroup scan completed%s",
Jan Schmidt2f232032013-04-25 16:04:51 +00002670 err == 2 ? " (inconsistency flag cleared)" : "");
2671 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05002672 btrfs_err(fs_info, "qgroup scan failed with %d", err);
Jan Schmidt2f232032013-04-25 16:04:51 +00002673 }
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002674
2675 complete_all(&fs_info->qgroup_rescan_completion);
Jan Schmidt2f232032013-04-25 16:04:51 +00002676}
2677
Jan Schmidtb382a322013-05-28 15:47:24 +00002678/*
2679 * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
2680 * memory required for the rescan context.
2681 */
2682static int
2683qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
2684 int init_flags)
Jan Schmidt2f232032013-04-25 16:04:51 +00002685{
2686 int ret = 0;
Jan Schmidt2f232032013-04-25 16:04:51 +00002687
Jan Schmidtb382a322013-05-28 15:47:24 +00002688 if (!init_flags &&
2689 (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
2690 !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
2691 ret = -EINVAL;
2692 goto err;
2693 }
Jan Schmidt2f232032013-04-25 16:04:51 +00002694
2695 mutex_lock(&fs_info->qgroup_rescan_lock);
2696 spin_lock(&fs_info->qgroup_lock);
Jan Schmidtb382a322013-05-28 15:47:24 +00002697
2698 if (init_flags) {
2699 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
2700 ret = -EINPROGRESS;
2701 else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
2702 ret = -EINVAL;
2703
2704 if (ret) {
2705 spin_unlock(&fs_info->qgroup_lock);
2706 mutex_unlock(&fs_info->qgroup_rescan_lock);
2707 goto err;
2708 }
2709
2710 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2711 }
2712
2713 memset(&fs_info->qgroup_rescan_progress, 0,
2714 sizeof(fs_info->qgroup_rescan_progress));
2715 fs_info->qgroup_rescan_progress.objectid = progress_objectid;
2716
2717 spin_unlock(&fs_info->qgroup_lock);
2718 mutex_unlock(&fs_info->qgroup_rescan_lock);
2719
2720 init_completion(&fs_info->qgroup_rescan_completion);
2721
2722 memset(&fs_info->qgroup_rescan_work, 0,
2723 sizeof(fs_info->qgroup_rescan_work));
Qu Wenruofc97fab2014-02-28 10:46:16 +08002724 btrfs_init_work(&fs_info->qgroup_rescan_work,
Liu Bo9e0af232014-08-15 23:36:53 +08002725 btrfs_qgroup_rescan_helper,
Qu Wenruofc97fab2014-02-28 10:46:16 +08002726 btrfs_qgroup_rescan_worker, NULL, NULL);
Jan Schmidtb382a322013-05-28 15:47:24 +00002727
Jan Schmidt2f232032013-04-25 16:04:51 +00002728 if (ret) {
Jan Schmidtb382a322013-05-28 15:47:24 +00002729err:
Frank Holtonefe120a2013-12-20 11:37:06 -05002730 btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret);
Jan Schmidt2f232032013-04-25 16:04:51 +00002731 return ret;
2732 }
2733
Jan Schmidtb382a322013-05-28 15:47:24 +00002734 return 0;
2735}
Jan Schmidt2f232032013-04-25 16:04:51 +00002736
Jan Schmidtb382a322013-05-28 15:47:24 +00002737static void
2738qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
2739{
2740 struct rb_node *n;
2741 struct btrfs_qgroup *qgroup;
2742
2743 spin_lock(&fs_info->qgroup_lock);
Jan Schmidt2f232032013-04-25 16:04:51 +00002744 /* clear all current qgroup tracking information */
2745 for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
2746 qgroup = rb_entry(n, struct btrfs_qgroup, node);
2747 qgroup->rfer = 0;
2748 qgroup->rfer_cmpr = 0;
2749 qgroup->excl = 0;
2750 qgroup->excl_cmpr = 0;
2751 }
2752 spin_unlock(&fs_info->qgroup_lock);
Jan Schmidtb382a322013-05-28 15:47:24 +00002753}
Jan Schmidt2f232032013-04-25 16:04:51 +00002754
Jan Schmidtb382a322013-05-28 15:47:24 +00002755int
2756btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
2757{
2758 int ret = 0;
2759 struct btrfs_trans_handle *trans;
2760
2761 ret = qgroup_rescan_init(fs_info, 0, 1);
2762 if (ret)
2763 return ret;
2764
2765 /*
2766 * We have set the rescan_progress to 0, which means no more
2767 * delayed refs will be accounted by btrfs_qgroup_account_ref.
2768 * However, btrfs_qgroup_account_ref may be right after its call
2769 * to btrfs_find_all_roots, in which case it would still do the
2770 * accounting.
2771 * To solve this, we're committing the transaction, which will
2772 * ensure we run all delayed refs and only after that, we are
2773 * going to clear all tracking information for a clean start.
2774 */
2775
2776 trans = btrfs_join_transaction(fs_info->fs_root);
2777 if (IS_ERR(trans)) {
2778 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2779 return PTR_ERR(trans);
2780 }
2781 ret = btrfs_commit_transaction(trans, fs_info->fs_root);
2782 if (ret) {
2783 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2784 return ret;
2785 }
2786
2787 qgroup_rescan_zero_tracking(fs_info);
2788
Qu Wenruofc97fab2014-02-28 10:46:16 +08002789 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2790 &fs_info->qgroup_rescan_work);
Jan Schmidt2f232032013-04-25 16:04:51 +00002791
2792 return 0;
2793}
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002794
2795int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info)
2796{
2797 int running;
2798 int ret = 0;
2799
2800 mutex_lock(&fs_info->qgroup_rescan_lock);
2801 spin_lock(&fs_info->qgroup_lock);
2802 running = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2803 spin_unlock(&fs_info->qgroup_lock);
2804 mutex_unlock(&fs_info->qgroup_rescan_lock);
2805
2806 if (running)
2807 ret = wait_for_completion_interruptible(
2808 &fs_info->qgroup_rescan_completion);
2809
2810 return ret;
2811}
Jan Schmidtb382a322013-05-28 15:47:24 +00002812
2813/*
2814 * this is only called from open_ctree where we're still single threaded, thus
2815 * locking is omitted here.
2816 */
2817void
2818btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
2819{
2820 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
Qu Wenruofc97fab2014-02-28 10:46:16 +08002821 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2822 &fs_info->qgroup_rescan_work);
Jan Schmidtb382a322013-05-28 15:47:24 +00002823}