blob: dfd99867ff4d7ed2e96c3f10751f1b7f9f884c9d [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
Qu Wenruoe69bcee2015-04-17 10:23:16 +080037
Arne Jansenbed92ea2012-06-28 18:03:02 +020038/* TODO XXX FIXME
39 * - subvol delete -> delete when ref goes to 0? delete limits also?
40 * - reorganize keys
41 * - compressed
42 * - sync
Arne Jansenbed92ea2012-06-28 18:03:02 +020043 * - copy also limits on subvol creation
44 * - limit
45 * - caches fuer ulists
46 * - performance benchmarks
47 * - check all ioctl parameters
48 */
49
50/*
51 * one struct for each qgroup, organized in fs_info->qgroup_tree.
52 */
53struct btrfs_qgroup {
54 u64 qgroupid;
55
56 /*
57 * state
58 */
59 u64 rfer; /* referenced */
60 u64 rfer_cmpr; /* referenced compressed */
61 u64 excl; /* exclusive */
62 u64 excl_cmpr; /* exclusive compressed */
63
64 /*
65 * limits
66 */
67 u64 lim_flags; /* which limits are set */
68 u64 max_rfer;
69 u64 max_excl;
70 u64 rsv_rfer;
71 u64 rsv_excl;
72
73 /*
74 * reservation tracking
75 */
76 u64 reserved;
77
78 /*
79 * lists
80 */
81 struct list_head groups; /* groups this group is member of */
82 struct list_head members; /* groups that are members of this group */
83 struct list_head dirty; /* dirty groups */
84 struct rb_node node; /* tree of qgroups */
85
86 /*
87 * temp variables for accounting operations
Nicholas D Steeves01327612016-05-19 21:18:45 -040088 * Refer to qgroup_shared_accounting() for details.
Arne Jansenbed92ea2012-06-28 18:03:02 +020089 */
Josef Bacikfcebe452014-05-13 17:30:47 -070090 u64 old_refcnt;
91 u64 new_refcnt;
Arne Jansenbed92ea2012-06-28 18:03:02 +020092};
93
Qu Wenruo9c542132015-03-12 16:10:13 +080094static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup *qg, u64 seq,
95 int mod)
96{
97 if (qg->old_refcnt < seq)
98 qg->old_refcnt = seq;
99 qg->old_refcnt += mod;
100}
101
102static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup *qg, u64 seq,
103 int mod)
104{
105 if (qg->new_refcnt < seq)
106 qg->new_refcnt = seq;
107 qg->new_refcnt += mod;
108}
109
110static inline u64 btrfs_qgroup_get_old_refcnt(struct btrfs_qgroup *qg, u64 seq)
111{
112 if (qg->old_refcnt < seq)
113 return 0;
114 return qg->old_refcnt - seq;
115}
116
117static inline u64 btrfs_qgroup_get_new_refcnt(struct btrfs_qgroup *qg, u64 seq)
118{
119 if (qg->new_refcnt < seq)
120 return 0;
121 return qg->new_refcnt - seq;
122}
123
Arne Jansenbed92ea2012-06-28 18:03:02 +0200124/*
125 * glue structure to represent the relations between qgroups.
126 */
127struct btrfs_qgroup_list {
128 struct list_head next_group;
129 struct list_head next_member;
130 struct btrfs_qgroup *group;
131 struct btrfs_qgroup *member;
132};
133
Josef Bacikfcebe452014-05-13 17:30:47 -0700134#define ptr_to_u64(x) ((u64)(uintptr_t)x)
135#define u64_to_ptr(x) ((struct btrfs_qgroup *)(uintptr_t)x)
136
Jan Schmidtb382a322013-05-28 15:47:24 +0000137static int
138qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
139 int init_flags);
140static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +0000141
Wang Shilong58400fc2013-04-07 10:50:17 +0000142/* must be called with qgroup_ioctl_lock held */
Arne Jansenbed92ea2012-06-28 18:03:02 +0200143static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
144 u64 qgroupid)
145{
146 struct rb_node *n = fs_info->qgroup_tree.rb_node;
147 struct btrfs_qgroup *qgroup;
148
149 while (n) {
150 qgroup = rb_entry(n, struct btrfs_qgroup, node);
151 if (qgroup->qgroupid < qgroupid)
152 n = n->rb_left;
153 else if (qgroup->qgroupid > qgroupid)
154 n = n->rb_right;
155 else
156 return qgroup;
157 }
158 return NULL;
159}
160
161/* must be called with qgroup_lock held */
162static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
163 u64 qgroupid)
164{
165 struct rb_node **p = &fs_info->qgroup_tree.rb_node;
166 struct rb_node *parent = NULL;
167 struct btrfs_qgroup *qgroup;
168
169 while (*p) {
170 parent = *p;
171 qgroup = rb_entry(parent, struct btrfs_qgroup, node);
172
173 if (qgroup->qgroupid < qgroupid)
174 p = &(*p)->rb_left;
175 else if (qgroup->qgroupid > qgroupid)
176 p = &(*p)->rb_right;
177 else
178 return qgroup;
179 }
180
181 qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
182 if (!qgroup)
183 return ERR_PTR(-ENOMEM);
184
185 qgroup->qgroupid = qgroupid;
186 INIT_LIST_HEAD(&qgroup->groups);
187 INIT_LIST_HEAD(&qgroup->members);
188 INIT_LIST_HEAD(&qgroup->dirty);
189
190 rb_link_node(&qgroup->node, parent, p);
191 rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
192
193 return qgroup;
194}
195
Wang Shilong4082bd32013-08-14 09:13:36 +0800196static void __del_qgroup_rb(struct btrfs_qgroup *qgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200197{
Arne Jansenbed92ea2012-06-28 18:03:02 +0200198 struct btrfs_qgroup_list *list;
199
Arne Jansenbed92ea2012-06-28 18:03:02 +0200200 list_del(&qgroup->dirty);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200201 while (!list_empty(&qgroup->groups)) {
202 list = list_first_entry(&qgroup->groups,
203 struct btrfs_qgroup_list, next_group);
204 list_del(&list->next_group);
205 list_del(&list->next_member);
206 kfree(list);
207 }
208
209 while (!list_empty(&qgroup->members)) {
210 list = list_first_entry(&qgroup->members,
211 struct btrfs_qgroup_list, next_member);
212 list_del(&list->next_group);
213 list_del(&list->next_member);
214 kfree(list);
215 }
216 kfree(qgroup);
Wang Shilong4082bd32013-08-14 09:13:36 +0800217}
Arne Jansenbed92ea2012-06-28 18:03:02 +0200218
Wang Shilong4082bd32013-08-14 09:13:36 +0800219/* must be called with qgroup_lock held */
220static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
221{
222 struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
223
224 if (!qgroup)
225 return -ENOENT;
226
227 rb_erase(&qgroup->node, &fs_info->qgroup_tree);
228 __del_qgroup_rb(qgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200229 return 0;
230}
231
232/* must be called with qgroup_lock held */
233static int add_relation_rb(struct btrfs_fs_info *fs_info,
234 u64 memberid, u64 parentid)
235{
236 struct btrfs_qgroup *member;
237 struct btrfs_qgroup *parent;
238 struct btrfs_qgroup_list *list;
239
240 member = find_qgroup_rb(fs_info, memberid);
241 parent = find_qgroup_rb(fs_info, parentid);
242 if (!member || !parent)
243 return -ENOENT;
244
245 list = kzalloc(sizeof(*list), GFP_ATOMIC);
246 if (!list)
247 return -ENOMEM;
248
249 list->group = parent;
250 list->member = member;
251 list_add_tail(&list->next_group, &member->groups);
252 list_add_tail(&list->next_member, &parent->members);
253
254 return 0;
255}
256
257/* must be called with qgroup_lock held */
258static int del_relation_rb(struct btrfs_fs_info *fs_info,
259 u64 memberid, u64 parentid)
260{
261 struct btrfs_qgroup *member;
262 struct btrfs_qgroup *parent;
263 struct btrfs_qgroup_list *list;
264
265 member = find_qgroup_rb(fs_info, memberid);
266 parent = find_qgroup_rb(fs_info, parentid);
267 if (!member || !parent)
268 return -ENOENT;
269
270 list_for_each_entry(list, &member->groups, next_group) {
271 if (list->group == parent) {
272 list_del(&list->next_group);
273 list_del(&list->next_member);
274 kfree(list);
275 return 0;
276 }
277 }
278 return -ENOENT;
279}
280
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400281#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
282int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
283 u64 rfer, u64 excl)
284{
285 struct btrfs_qgroup *qgroup;
286
287 qgroup = find_qgroup_rb(fs_info, qgroupid);
288 if (!qgroup)
289 return -EINVAL;
290 if (qgroup->rfer != rfer || qgroup->excl != excl)
291 return -EINVAL;
292 return 0;
293}
294#endif
295
Arne Jansenbed92ea2012-06-28 18:03:02 +0200296/*
297 * The full config is read in one go, only called from open_ctree()
298 * It doesn't use any locking, as at this point we're still single-threaded
299 */
300int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
301{
302 struct btrfs_key key;
303 struct btrfs_key found_key;
304 struct btrfs_root *quota_root = fs_info->quota_root;
305 struct btrfs_path *path = NULL;
306 struct extent_buffer *l;
307 int slot;
308 int ret = 0;
309 u64 flags = 0;
Jan Schmidtb382a322013-05-28 15:47:24 +0000310 u64 rescan_progress = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200311
Josef Bacikafcdd122016-09-02 15:40:02 -0400312 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Arne Jansenbed92ea2012-06-28 18:03:02 +0200313 return 0;
314
Wang Shilong1e8f9152013-05-06 11:03:27 +0000315 fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
316 if (!fs_info->qgroup_ulist) {
317 ret = -ENOMEM;
318 goto out;
319 }
320
Arne Jansenbed92ea2012-06-28 18:03:02 +0200321 path = btrfs_alloc_path();
322 if (!path) {
323 ret = -ENOMEM;
324 goto out;
325 }
326
327 /* default this to quota off, in case no status key is found */
328 fs_info->qgroup_flags = 0;
329
330 /*
331 * pass 1: read status, all qgroup infos and limits
332 */
333 key.objectid = 0;
334 key.type = 0;
335 key.offset = 0;
336 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
337 if (ret)
338 goto out;
339
340 while (1) {
341 struct btrfs_qgroup *qgroup;
342
343 slot = path->slots[0];
344 l = path->nodes[0];
345 btrfs_item_key_to_cpu(l, &found_key, slot);
346
347 if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
348 struct btrfs_qgroup_status_item *ptr;
349
350 ptr = btrfs_item_ptr(l, slot,
351 struct btrfs_qgroup_status_item);
352
353 if (btrfs_qgroup_status_version(l, ptr) !=
354 BTRFS_QGROUP_STATUS_VERSION) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500355 btrfs_err(fs_info,
356 "old qgroup version, quota disabled");
Arne Jansenbed92ea2012-06-28 18:03:02 +0200357 goto out;
358 }
359 if (btrfs_qgroup_status_generation(l, ptr) !=
360 fs_info->generation) {
361 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Frank Holtonefe120a2013-12-20 11:37:06 -0500362 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400363 "qgroup generation mismatch, marked as inconsistent");
Arne Jansenbed92ea2012-06-28 18:03:02 +0200364 }
365 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
366 ptr);
Jan Schmidtb382a322013-05-28 15:47:24 +0000367 rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200368 goto next1;
369 }
370
371 if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
372 found_key.type != BTRFS_QGROUP_LIMIT_KEY)
373 goto next1;
374
375 qgroup = find_qgroup_rb(fs_info, found_key.offset);
376 if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
377 (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
Geert Uytterhoevend41e36a2015-07-06 15:38:11 +0200378 btrfs_err(fs_info, "inconsistent qgroup config");
Arne Jansenbed92ea2012-06-28 18:03:02 +0200379 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
380 }
381 if (!qgroup) {
382 qgroup = add_qgroup_rb(fs_info, found_key.offset);
383 if (IS_ERR(qgroup)) {
384 ret = PTR_ERR(qgroup);
385 goto out;
386 }
387 }
388 switch (found_key.type) {
389 case BTRFS_QGROUP_INFO_KEY: {
390 struct btrfs_qgroup_info_item *ptr;
391
392 ptr = btrfs_item_ptr(l, slot,
393 struct btrfs_qgroup_info_item);
394 qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
395 qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
396 qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
397 qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
398 /* generation currently unused */
399 break;
400 }
401 case BTRFS_QGROUP_LIMIT_KEY: {
402 struct btrfs_qgroup_limit_item *ptr;
403
404 ptr = btrfs_item_ptr(l, slot,
405 struct btrfs_qgroup_limit_item);
406 qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
407 qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
408 qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
409 qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
410 qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
411 break;
412 }
413 }
414next1:
415 ret = btrfs_next_item(quota_root, path);
416 if (ret < 0)
417 goto out;
418 if (ret)
419 break;
420 }
421 btrfs_release_path(path);
422
423 /*
424 * pass 2: read all qgroup relations
425 */
426 key.objectid = 0;
427 key.type = BTRFS_QGROUP_RELATION_KEY;
428 key.offset = 0;
429 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
430 if (ret)
431 goto out;
432 while (1) {
433 slot = path->slots[0];
434 l = path->nodes[0];
435 btrfs_item_key_to_cpu(l, &found_key, slot);
436
437 if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
438 goto next2;
439
440 if (found_key.objectid > found_key.offset) {
441 /* parent <- member, not needed to build config */
442 /* FIXME should we omit the key completely? */
443 goto next2;
444 }
445
446 ret = add_relation_rb(fs_info, found_key.objectid,
447 found_key.offset);
Arne Jansenff248582013-01-17 01:22:08 -0700448 if (ret == -ENOENT) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500449 btrfs_warn(fs_info,
450 "orphan qgroup relation 0x%llx->0x%llx",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200451 found_key.objectid, found_key.offset);
Arne Jansenff248582013-01-17 01:22:08 -0700452 ret = 0; /* ignore the error */
453 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200454 if (ret)
455 goto out;
456next2:
457 ret = btrfs_next_item(quota_root, path);
458 if (ret < 0)
459 goto out;
460 if (ret)
461 break;
462 }
463out:
464 fs_info->qgroup_flags |= flags;
Josef Bacikafcdd122016-09-02 15:40:02 -0400465 if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
466 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
467 else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
468 ret >= 0)
Jan Schmidtb382a322013-05-28 15:47:24 +0000469 ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200470 btrfs_free_path(path);
471
Jan Schmidteb1716a2013-05-28 15:47:23 +0000472 if (ret < 0) {
Wang Shilong1e8f9152013-05-06 11:03:27 +0000473 ulist_free(fs_info->qgroup_ulist);
Jan Schmidteb1716a2013-05-28 15:47:23 +0000474 fs_info->qgroup_ulist = NULL;
Jan Schmidtb382a322013-05-28 15:47:24 +0000475 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
Jan Schmidteb1716a2013-05-28 15:47:23 +0000476 }
Wang Shilong1e8f9152013-05-06 11:03:27 +0000477
Arne Jansenbed92ea2012-06-28 18:03:02 +0200478 return ret < 0 ? ret : 0;
479}
480
481/*
Wang Shilonge685da12013-08-14 09:13:37 +0800482 * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
483 * first two are in single-threaded paths.And for the third one, we have set
484 * quota_root to be null with qgroup_lock held before, so it is safe to clean
485 * up the in-memory structures without qgroup_lock held.
Arne Jansenbed92ea2012-06-28 18:03:02 +0200486 */
487void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
488{
489 struct rb_node *n;
490 struct btrfs_qgroup *qgroup;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200491
492 while ((n = rb_first(&fs_info->qgroup_tree))) {
493 qgroup = rb_entry(n, struct btrfs_qgroup, node);
494 rb_erase(n, &fs_info->qgroup_tree);
Wang Shilong4082bd32013-08-14 09:13:36 +0800495 __del_qgroup_rb(qgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200496 }
Wang Shilong1e7bac12013-07-13 21:02:54 +0800497 /*
498 * we call btrfs_free_qgroup_config() when umounting
Nicholas D Steeves01327612016-05-19 21:18:45 -0400499 * filesystem and disabling quota, so we set qgroup_ulist
Wang Shilong1e7bac12013-07-13 21:02:54 +0800500 * to be null here to avoid double free.
501 */
Wang Shilong1e8f9152013-05-06 11:03:27 +0000502 ulist_free(fs_info->qgroup_ulist);
Wang Shilong1e7bac12013-07-13 21:02:54 +0800503 fs_info->qgroup_ulist = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200504}
505
506static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
507 struct btrfs_root *quota_root,
508 u64 src, u64 dst)
509{
510 int ret;
511 struct btrfs_path *path;
512 struct btrfs_key key;
513
514 path = btrfs_alloc_path();
515 if (!path)
516 return -ENOMEM;
517
518 key.objectid = src;
519 key.type = BTRFS_QGROUP_RELATION_KEY;
520 key.offset = dst;
521
522 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
523
524 btrfs_mark_buffer_dirty(path->nodes[0]);
525
526 btrfs_free_path(path);
527 return ret;
528}
529
530static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
531 struct btrfs_root *quota_root,
532 u64 src, u64 dst)
533{
534 int ret;
535 struct btrfs_path *path;
536 struct btrfs_key key;
537
538 path = btrfs_alloc_path();
539 if (!path)
540 return -ENOMEM;
541
542 key.objectid = src;
543 key.type = BTRFS_QGROUP_RELATION_KEY;
544 key.offset = dst;
545
546 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
547 if (ret < 0)
548 goto out;
549
550 if (ret > 0) {
551 ret = -ENOENT;
552 goto out;
553 }
554
555 ret = btrfs_del_item(trans, quota_root, path);
556out:
557 btrfs_free_path(path);
558 return ret;
559}
560
561static int add_qgroup_item(struct btrfs_trans_handle *trans,
562 struct btrfs_root *quota_root, u64 qgroupid)
563{
564 int ret;
565 struct btrfs_path *path;
566 struct btrfs_qgroup_info_item *qgroup_info;
567 struct btrfs_qgroup_limit_item *qgroup_limit;
568 struct extent_buffer *leaf;
569 struct btrfs_key key;
570
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -0400571 if (btrfs_is_testing(quota_root->fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400572 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +0200573
Arne Jansenbed92ea2012-06-28 18:03:02 +0200574 path = btrfs_alloc_path();
575 if (!path)
576 return -ENOMEM;
577
578 key.objectid = 0;
579 key.type = BTRFS_QGROUP_INFO_KEY;
580 key.offset = qgroupid;
581
Mark Fasheh0b4699d2014-08-18 14:01:17 -0700582 /*
583 * Avoid a transaction abort by catching -EEXIST here. In that
584 * case, we proceed by re-initializing the existing structure
585 * on disk.
586 */
587
Arne Jansenbed92ea2012-06-28 18:03:02 +0200588 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
589 sizeof(*qgroup_info));
Mark Fasheh0b4699d2014-08-18 14:01:17 -0700590 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200591 goto out;
592
593 leaf = path->nodes[0];
594 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
595 struct btrfs_qgroup_info_item);
596 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
597 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
598 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
599 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
600 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
601
602 btrfs_mark_buffer_dirty(leaf);
603
604 btrfs_release_path(path);
605
606 key.type = BTRFS_QGROUP_LIMIT_KEY;
607 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
608 sizeof(*qgroup_limit));
Mark Fasheh0b4699d2014-08-18 14:01:17 -0700609 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200610 goto out;
611
612 leaf = path->nodes[0];
613 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
614 struct btrfs_qgroup_limit_item);
615 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
616 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
617 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
618 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
619 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
620
621 btrfs_mark_buffer_dirty(leaf);
622
623 ret = 0;
624out:
625 btrfs_free_path(path);
626 return ret;
627}
628
629static int del_qgroup_item(struct btrfs_trans_handle *trans,
630 struct btrfs_root *quota_root, u64 qgroupid)
631{
632 int ret;
633 struct btrfs_path *path;
634 struct btrfs_key key;
635
636 path = btrfs_alloc_path();
637 if (!path)
638 return -ENOMEM;
639
640 key.objectid = 0;
641 key.type = BTRFS_QGROUP_INFO_KEY;
642 key.offset = qgroupid;
643 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
644 if (ret < 0)
645 goto out;
646
647 if (ret > 0) {
648 ret = -ENOENT;
649 goto out;
650 }
651
652 ret = btrfs_del_item(trans, quota_root, path);
653 if (ret)
654 goto out;
655
656 btrfs_release_path(path);
657
658 key.type = BTRFS_QGROUP_LIMIT_KEY;
659 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
660 if (ret < 0)
661 goto out;
662
663 if (ret > 0) {
664 ret = -ENOENT;
665 goto out;
666 }
667
668 ret = btrfs_del_item(trans, quota_root, path);
669
670out:
671 btrfs_free_path(path);
672 return ret;
673}
674
675static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
Dongsheng Yang1510e712014-11-20 21:01:41 -0500676 struct btrfs_root *root,
677 struct btrfs_qgroup *qgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200678{
679 struct btrfs_path *path;
680 struct btrfs_key key;
681 struct extent_buffer *l;
682 struct btrfs_qgroup_limit_item *qgroup_limit;
683 int ret;
684 int slot;
685
686 key.objectid = 0;
687 key.type = BTRFS_QGROUP_LIMIT_KEY;
Dongsheng Yang1510e712014-11-20 21:01:41 -0500688 key.offset = qgroup->qgroupid;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200689
690 path = btrfs_alloc_path();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000691 if (!path)
692 return -ENOMEM;
693
Arne Jansenbed92ea2012-06-28 18:03:02 +0200694 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
695 if (ret > 0)
696 ret = -ENOENT;
697
698 if (ret)
699 goto out;
700
701 l = path->nodes[0];
702 slot = path->slots[0];
Valentina Giustia3df41e2013-11-04 22:34:29 +0100703 qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
Dongsheng Yang1510e712014-11-20 21:01:41 -0500704 btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
705 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
706 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
707 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
708 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200709
710 btrfs_mark_buffer_dirty(l);
711
712out:
713 btrfs_free_path(path);
714 return ret;
715}
716
717static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
718 struct btrfs_root *root,
719 struct btrfs_qgroup *qgroup)
720{
721 struct btrfs_path *path;
722 struct btrfs_key key;
723 struct extent_buffer *l;
724 struct btrfs_qgroup_info_item *qgroup_info;
725 int ret;
726 int slot;
727
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -0400728 if (btrfs_is_testing(root->fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400729 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +0200730
Arne Jansenbed92ea2012-06-28 18:03:02 +0200731 key.objectid = 0;
732 key.type = BTRFS_QGROUP_INFO_KEY;
733 key.offset = qgroup->qgroupid;
734
735 path = btrfs_alloc_path();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000736 if (!path)
737 return -ENOMEM;
738
Arne Jansenbed92ea2012-06-28 18:03:02 +0200739 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
740 if (ret > 0)
741 ret = -ENOENT;
742
743 if (ret)
744 goto out;
745
746 l = path->nodes[0];
747 slot = path->slots[0];
Valentina Giustia3df41e2013-11-04 22:34:29 +0100748 qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200749 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
750 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
751 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
752 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
753 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
754
755 btrfs_mark_buffer_dirty(l);
756
757out:
758 btrfs_free_path(path);
759 return ret;
760}
761
762static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
763 struct btrfs_fs_info *fs_info,
764 struct btrfs_root *root)
765{
766 struct btrfs_path *path;
767 struct btrfs_key key;
768 struct extent_buffer *l;
769 struct btrfs_qgroup_status_item *ptr;
770 int ret;
771 int slot;
772
773 key.objectid = 0;
774 key.type = BTRFS_QGROUP_STATUS_KEY;
775 key.offset = 0;
776
777 path = btrfs_alloc_path();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000778 if (!path)
779 return -ENOMEM;
780
Arne Jansenbed92ea2012-06-28 18:03:02 +0200781 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
782 if (ret > 0)
783 ret = -ENOENT;
784
785 if (ret)
786 goto out;
787
788 l = path->nodes[0];
789 slot = path->slots[0];
790 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
791 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
792 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
Jan Schmidt2f232032013-04-25 16:04:51 +0000793 btrfs_set_qgroup_status_rescan(l, ptr,
794 fs_info->qgroup_rescan_progress.objectid);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200795
796 btrfs_mark_buffer_dirty(l);
797
798out:
799 btrfs_free_path(path);
800 return ret;
801}
802
803/*
804 * called with qgroup_lock held
805 */
806static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
807 struct btrfs_root *root)
808{
809 struct btrfs_path *path;
810 struct btrfs_key key;
Wang Shilong06b3a862013-02-27 11:16:57 +0000811 struct extent_buffer *leaf = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200812 int ret;
Wang Shilong06b3a862013-02-27 11:16:57 +0000813 int nr = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200814
Arne Jansenbed92ea2012-06-28 18:03:02 +0200815 path = btrfs_alloc_path();
816 if (!path)
817 return -ENOMEM;
818
Wang Shilong06b3a862013-02-27 11:16:57 +0000819 path->leave_spinning = 1;
820
821 key.objectid = 0;
822 key.offset = 0;
823 key.type = 0;
824
Arne Jansenbed92ea2012-06-28 18:03:02 +0200825 while (1) {
Arne Jansenbed92ea2012-06-28 18:03:02 +0200826 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Wang Shilong06b3a862013-02-27 11:16:57 +0000827 if (ret < 0)
828 goto out;
829 leaf = path->nodes[0];
830 nr = btrfs_header_nritems(leaf);
831 if (!nr)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200832 break;
Wang Shilong06b3a862013-02-27 11:16:57 +0000833 /*
834 * delete the leaf one by one
835 * since the whole tree is going
836 * to be deleted.
837 */
838 path->slots[0] = 0;
839 ret = btrfs_del_items(trans, root, path, 0, nr);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200840 if (ret)
841 goto out;
Wang Shilong06b3a862013-02-27 11:16:57 +0000842
Arne Jansenbed92ea2012-06-28 18:03:02 +0200843 btrfs_release_path(path);
844 }
845 ret = 0;
846out:
Josef Bacikafcdd122016-09-02 15:40:02 -0400847 set_bit(BTRFS_FS_QUOTA_DISABLING, &root->fs_info->flags);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200848 btrfs_free_path(path);
849 return ret;
850}
851
852int btrfs_quota_enable(struct btrfs_trans_handle *trans,
853 struct btrfs_fs_info *fs_info)
854{
855 struct btrfs_root *quota_root;
Wang Shilong7708f022013-04-07 10:24:57 +0000856 struct btrfs_root *tree_root = fs_info->tree_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200857 struct btrfs_path *path = NULL;
858 struct btrfs_qgroup_status_item *ptr;
859 struct extent_buffer *leaf;
860 struct btrfs_key key;
Wang Shilong7708f022013-04-07 10:24:57 +0000861 struct btrfs_key found_key;
862 struct btrfs_qgroup *qgroup = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200863 int ret = 0;
Wang Shilong7708f022013-04-07 10:24:57 +0000864 int slot;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200865
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000866 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200867 if (fs_info->quota_root) {
Josef Bacikafcdd122016-09-02 15:40:02 -0400868 set_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200869 goto out;
870 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200871
Wang Shilong1e8f9152013-05-06 11:03:27 +0000872 fs_info->qgroup_ulist = ulist_alloc(GFP_NOFS);
873 if (!fs_info->qgroup_ulist) {
874 ret = -ENOMEM;
875 goto out;
876 }
877
Arne Jansenbed92ea2012-06-28 18:03:02 +0200878 /*
879 * initially create the quota tree
880 */
881 quota_root = btrfs_create_tree(trans, fs_info,
882 BTRFS_QUOTA_TREE_OBJECTID);
883 if (IS_ERR(quota_root)) {
884 ret = PTR_ERR(quota_root);
885 goto out;
886 }
887
888 path = btrfs_alloc_path();
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000889 if (!path) {
890 ret = -ENOMEM;
891 goto out_free_root;
892 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200893
894 key.objectid = 0;
895 key.type = BTRFS_QGROUP_STATUS_KEY;
896 key.offset = 0;
897
898 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
899 sizeof(*ptr));
900 if (ret)
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000901 goto out_free_path;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200902
903 leaf = path->nodes[0];
904 ptr = btrfs_item_ptr(leaf, path->slots[0],
905 struct btrfs_qgroup_status_item);
906 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
907 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
908 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
909 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
910 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
Jan Schmidt2f232032013-04-25 16:04:51 +0000911 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200912
913 btrfs_mark_buffer_dirty(leaf);
914
Wang Shilong7708f022013-04-07 10:24:57 +0000915 key.objectid = 0;
916 key.type = BTRFS_ROOT_REF_KEY;
917 key.offset = 0;
918
919 btrfs_release_path(path);
920 ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
921 if (ret > 0)
922 goto out_add_root;
923 if (ret < 0)
924 goto out_free_path;
925
926
927 while (1) {
928 slot = path->slots[0];
929 leaf = path->nodes[0];
930 btrfs_item_key_to_cpu(leaf, &found_key, slot);
931
932 if (found_key.type == BTRFS_ROOT_REF_KEY) {
933 ret = add_qgroup_item(trans, quota_root,
934 found_key.offset);
935 if (ret)
936 goto out_free_path;
937
Wang Shilong7708f022013-04-07 10:24:57 +0000938 qgroup = add_qgroup_rb(fs_info, found_key.offset);
939 if (IS_ERR(qgroup)) {
Wang Shilong7708f022013-04-07 10:24:57 +0000940 ret = PTR_ERR(qgroup);
941 goto out_free_path;
942 }
Wang Shilong7708f022013-04-07 10:24:57 +0000943 }
944 ret = btrfs_next_item(tree_root, path);
945 if (ret < 0)
946 goto out_free_path;
947 if (ret)
948 break;
949 }
950
951out_add_root:
952 btrfs_release_path(path);
953 ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
954 if (ret)
955 goto out_free_path;
956
Wang Shilong7708f022013-04-07 10:24:57 +0000957 qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
958 if (IS_ERR(qgroup)) {
Wang Shilong7708f022013-04-07 10:24:57 +0000959 ret = PTR_ERR(qgroup);
960 goto out_free_path;
961 }
Wang Shilong58400fc2013-04-07 10:50:17 +0000962 spin_lock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200963 fs_info->quota_root = quota_root;
Josef Bacikafcdd122016-09-02 15:40:02 -0400964 set_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200965 spin_unlock(&fs_info->qgroup_lock);
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000966out_free_path:
Arne Jansenbed92ea2012-06-28 18:03:02 +0200967 btrfs_free_path(path);
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000968out_free_root:
969 if (ret) {
970 free_extent_buffer(quota_root->node);
971 free_extent_buffer(quota_root->commit_root);
972 kfree(quota_root);
973 }
974out:
Jan Schmidteb1716a2013-05-28 15:47:23 +0000975 if (ret) {
Wang Shilong1e8f9152013-05-06 11:03:27 +0000976 ulist_free(fs_info->qgroup_ulist);
Jan Schmidteb1716a2013-05-28 15:47:23 +0000977 fs_info->qgroup_ulist = NULL;
978 }
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000979 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200980 return ret;
981}
982
983int btrfs_quota_disable(struct btrfs_trans_handle *trans,
984 struct btrfs_fs_info *fs_info)
985{
986 struct btrfs_root *tree_root = fs_info->tree_root;
987 struct btrfs_root *quota_root;
988 int ret = 0;
989
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000990 mutex_lock(&fs_info->qgroup_ioctl_lock);
Wang Shilong58400fc2013-04-07 10:50:17 +0000991 if (!fs_info->quota_root)
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000992 goto out;
Josef Bacikafcdd122016-09-02 15:40:02 -0400993 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
994 set_bit(BTRFS_FS_QUOTA_DISABLING, &fs_info->flags);
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -0400995 btrfs_qgroup_wait_for_completion(fs_info, false);
Justin Maggard967ef512015-11-06 10:36:42 -0800996 spin_lock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200997 quota_root = fs_info->quota_root;
998 fs_info->quota_root = NULL;
Dongsheng Yang8ea0ec92015-02-27 16:24:26 +0800999 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001000 spin_unlock(&fs_info->qgroup_lock);
1001
Wang Shilonge685da12013-08-14 09:13:37 +08001002 btrfs_free_qgroup_config(fs_info);
1003
Arne Jansenbed92ea2012-06-28 18:03:02 +02001004 ret = btrfs_clean_quota_tree(trans, quota_root);
1005 if (ret)
1006 goto out;
1007
1008 ret = btrfs_del_root(trans, tree_root, &quota_root->root_key);
1009 if (ret)
1010 goto out;
1011
1012 list_del(&quota_root->dirty_list);
1013
1014 btrfs_tree_lock(quota_root->node);
Daniel Dressler01d58472014-11-21 17:15:07 +09001015 clean_tree_block(trans, tree_root->fs_info, quota_root->node);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001016 btrfs_tree_unlock(quota_root->node);
1017 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
1018
1019 free_extent_buffer(quota_root->node);
1020 free_extent_buffer(quota_root->commit_root);
1021 kfree(quota_root);
1022out:
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001023 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001024 return ret;
1025}
1026
Jan Schmidt2f232032013-04-25 16:04:51 +00001027static void qgroup_dirty(struct btrfs_fs_info *fs_info,
1028 struct btrfs_qgroup *qgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001029{
Jan Schmidt2f232032013-04-25 16:04:51 +00001030 if (list_empty(&qgroup->dirty))
1031 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001032}
1033
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001034/*
1035 * The easy accounting, if we are adding/removing the only ref for an extent
Nicholas D Steeves01327612016-05-19 21:18:45 -04001036 * then this qgroup and all of the parent qgroups get their reference and
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001037 * exclusive counts adjusted.
1038 *
1039 * Caller should hold fs_info->qgroup_lock.
1040 */
1041static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
1042 struct ulist *tmp, u64 ref_root,
1043 u64 num_bytes, int sign)
1044{
1045 struct btrfs_qgroup *qgroup;
1046 struct btrfs_qgroup_list *glist;
1047 struct ulist_node *unode;
1048 struct ulist_iterator uiter;
1049 int ret = 0;
1050
1051 qgroup = find_qgroup_rb(fs_info, ref_root);
1052 if (!qgroup)
1053 goto out;
1054
1055 qgroup->rfer += sign * num_bytes;
1056 qgroup->rfer_cmpr += sign * num_bytes;
1057
1058 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1059 qgroup->excl += sign * num_bytes;
1060 qgroup->excl_cmpr += sign * num_bytes;
1061 if (sign > 0)
1062 qgroup->reserved -= num_bytes;
1063
1064 qgroup_dirty(fs_info, qgroup);
1065
1066 /* Get all of the parent groups that contain this qgroup */
1067 list_for_each_entry(glist, &qgroup->groups, next_group) {
1068 ret = ulist_add(tmp, glist->group->qgroupid,
1069 ptr_to_u64(glist->group), GFP_ATOMIC);
1070 if (ret < 0)
1071 goto out;
1072 }
1073
1074 /* Iterate all of the parents and adjust their reference counts */
1075 ULIST_ITER_INIT(&uiter);
1076 while ((unode = ulist_next(tmp, &uiter))) {
1077 qgroup = u64_to_ptr(unode->aux);
1078 qgroup->rfer += sign * num_bytes;
1079 qgroup->rfer_cmpr += sign * num_bytes;
1080 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1081 qgroup->excl += sign * num_bytes;
1082 if (sign > 0)
1083 qgroup->reserved -= num_bytes;
1084 qgroup->excl_cmpr += sign * num_bytes;
1085 qgroup_dirty(fs_info, qgroup);
1086
1087 /* Add any parents of the parents */
1088 list_for_each_entry(glist, &qgroup->groups, next_group) {
1089 ret = ulist_add(tmp, glist->group->qgroupid,
1090 ptr_to_u64(glist->group), GFP_ATOMIC);
1091 if (ret < 0)
1092 goto out;
1093 }
1094 }
1095 ret = 0;
1096out:
1097 return ret;
1098}
1099
1100
1101/*
1102 * Quick path for updating qgroup with only excl refs.
1103 *
1104 * In that case, just update all parent will be enough.
1105 * Or we needs to do a full rescan.
1106 * Caller should also hold fs_info->qgroup_lock.
1107 *
1108 * Return 0 for quick update, return >0 for need to full rescan
1109 * and mark INCONSISTENT flag.
1110 * Return < 0 for other error.
1111 */
1112static int quick_update_accounting(struct btrfs_fs_info *fs_info,
1113 struct ulist *tmp, u64 src, u64 dst,
1114 int sign)
1115{
1116 struct btrfs_qgroup *qgroup;
1117 int ret = 1;
1118 int err = 0;
1119
1120 qgroup = find_qgroup_rb(fs_info, src);
1121 if (!qgroup)
1122 goto out;
1123 if (qgroup->excl == qgroup->rfer) {
1124 ret = 0;
1125 err = __qgroup_excl_accounting(fs_info, tmp, dst,
1126 qgroup->excl, sign);
1127 if (err < 0) {
1128 ret = err;
1129 goto out;
1130 }
1131 }
1132out:
1133 if (ret)
1134 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1135 return ret;
1136}
1137
Arne Jansenbed92ea2012-06-28 18:03:02 +02001138int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
1139 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1140{
1141 struct btrfs_root *quota_root;
Wang Shilongb7fef4f2013-04-07 10:50:18 +00001142 struct btrfs_qgroup *parent;
1143 struct btrfs_qgroup *member;
Wang Shilong534e6622013-04-17 14:49:51 +00001144 struct btrfs_qgroup_list *list;
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001145 struct ulist *tmp;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001146 int ret = 0;
1147
Qu Wenruo8465ece2015-02-27 16:24:22 +08001148 /* Check the level of src and dst first */
1149 if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
1150 return -EINVAL;
1151
Christian Engelmayerab3680d2015-05-02 17:19:55 +02001152 tmp = ulist_alloc(GFP_NOFS);
1153 if (!tmp)
1154 return -ENOMEM;
1155
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001156 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001157 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001158 if (!quota_root) {
1159 ret = -EINVAL;
1160 goto out;
1161 }
Wang Shilongb7fef4f2013-04-07 10:50:18 +00001162 member = find_qgroup_rb(fs_info, src);
1163 parent = find_qgroup_rb(fs_info, dst);
1164 if (!member || !parent) {
1165 ret = -EINVAL;
1166 goto out;
1167 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001168
Wang Shilong534e6622013-04-17 14:49:51 +00001169 /* check if such qgroup relation exist firstly */
1170 list_for_each_entry(list, &member->groups, next_group) {
1171 if (list->group == parent) {
1172 ret = -EEXIST;
1173 goto out;
1174 }
1175 }
1176
Arne Jansenbed92ea2012-06-28 18:03:02 +02001177 ret = add_qgroup_relation_item(trans, quota_root, src, dst);
1178 if (ret)
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001179 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001180
1181 ret = add_qgroup_relation_item(trans, quota_root, dst, src);
1182 if (ret) {
1183 del_qgroup_relation_item(trans, quota_root, src, dst);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001184 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001185 }
1186
1187 spin_lock(&fs_info->qgroup_lock);
1188 ret = add_relation_rb(quota_root->fs_info, src, dst);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001189 if (ret < 0) {
1190 spin_unlock(&fs_info->qgroup_lock);
1191 goto out;
1192 }
1193 ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001194 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001195out:
1196 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001197 ulist_free(tmp);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001198 return ret;
1199}
1200
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001201int __del_qgroup_relation(struct btrfs_trans_handle *trans,
Arne Jansenbed92ea2012-06-28 18:03:02 +02001202 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1203{
1204 struct btrfs_root *quota_root;
Wang Shilong534e6622013-04-17 14:49:51 +00001205 struct btrfs_qgroup *parent;
1206 struct btrfs_qgroup *member;
1207 struct btrfs_qgroup_list *list;
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001208 struct ulist *tmp;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001209 int ret = 0;
1210 int err;
1211
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001212 tmp = ulist_alloc(GFP_NOFS);
1213 if (!tmp)
1214 return -ENOMEM;
1215
Arne Jansenbed92ea2012-06-28 18:03:02 +02001216 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001217 if (!quota_root) {
1218 ret = -EINVAL;
1219 goto out;
1220 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001221
Wang Shilong534e6622013-04-17 14:49:51 +00001222 member = find_qgroup_rb(fs_info, src);
1223 parent = find_qgroup_rb(fs_info, dst);
1224 if (!member || !parent) {
1225 ret = -EINVAL;
1226 goto out;
1227 }
1228
1229 /* check if such qgroup relation exist firstly */
1230 list_for_each_entry(list, &member->groups, next_group) {
1231 if (list->group == parent)
1232 goto exist;
1233 }
1234 ret = -ENOENT;
1235 goto out;
1236exist:
Arne Jansenbed92ea2012-06-28 18:03:02 +02001237 ret = del_qgroup_relation_item(trans, quota_root, src, dst);
1238 err = del_qgroup_relation_item(trans, quota_root, dst, src);
1239 if (err && !ret)
1240 ret = err;
1241
1242 spin_lock(&fs_info->qgroup_lock);
1243 del_relation_rb(fs_info, src, dst);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001244 ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001245 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001246out:
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001247 ulist_free(tmp);
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001248 return ret;
1249}
1250
1251int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
1252 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1253{
1254 int ret = 0;
1255
1256 mutex_lock(&fs_info->qgroup_ioctl_lock);
1257 ret = __del_qgroup_relation(trans, fs_info, src, dst);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001258 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001259
Arne Jansenbed92ea2012-06-28 18:03:02 +02001260 return ret;
1261}
1262
1263int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
Dongsheng Yang4087cf22015-01-18 10:59:23 -05001264 struct btrfs_fs_info *fs_info, u64 qgroupid)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001265{
1266 struct btrfs_root *quota_root;
1267 struct btrfs_qgroup *qgroup;
1268 int ret = 0;
1269
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001270 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001271 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001272 if (!quota_root) {
1273 ret = -EINVAL;
1274 goto out;
1275 }
Wang Shilong534e6622013-04-17 14:49:51 +00001276 qgroup = find_qgroup_rb(fs_info, qgroupid);
1277 if (qgroup) {
1278 ret = -EEXIST;
1279 goto out;
1280 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001281
1282 ret = add_qgroup_item(trans, quota_root, qgroupid);
Wang Shilong534e6622013-04-17 14:49:51 +00001283 if (ret)
1284 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001285
1286 spin_lock(&fs_info->qgroup_lock);
1287 qgroup = add_qgroup_rb(fs_info, qgroupid);
1288 spin_unlock(&fs_info->qgroup_lock);
1289
1290 if (IS_ERR(qgroup))
1291 ret = PTR_ERR(qgroup);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001292out:
1293 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001294 return ret;
1295}
1296
1297int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
1298 struct btrfs_fs_info *fs_info, u64 qgroupid)
1299{
1300 struct btrfs_root *quota_root;
Arne Jansen2cf68702013-01-17 01:22:09 -07001301 struct btrfs_qgroup *qgroup;
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001302 struct btrfs_qgroup_list *list;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001303 int ret = 0;
1304
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001305 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001306 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001307 if (!quota_root) {
1308 ret = -EINVAL;
1309 goto out;
1310 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001311
Arne Jansen2cf68702013-01-17 01:22:09 -07001312 qgroup = find_qgroup_rb(fs_info, qgroupid);
Wang Shilong534e6622013-04-17 14:49:51 +00001313 if (!qgroup) {
1314 ret = -ENOENT;
1315 goto out;
1316 } else {
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001317 /* check if there are no children of this qgroup */
1318 if (!list_empty(&qgroup->members)) {
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001319 ret = -EBUSY;
1320 goto out;
Arne Jansen2cf68702013-01-17 01:22:09 -07001321 }
1322 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001323 ret = del_qgroup_item(trans, quota_root, qgroupid);
1324
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001325 while (!list_empty(&qgroup->groups)) {
1326 list = list_first_entry(&qgroup->groups,
1327 struct btrfs_qgroup_list, next_group);
1328 ret = __del_qgroup_relation(trans, fs_info,
1329 qgroupid,
1330 list->group->qgroupid);
1331 if (ret)
1332 goto out;
1333 }
1334
Arne Jansenbed92ea2012-06-28 18:03:02 +02001335 spin_lock(&fs_info->qgroup_lock);
1336 del_qgroup_rb(quota_root->fs_info, qgroupid);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001337 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001338out:
1339 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001340 return ret;
1341}
1342
1343int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
1344 struct btrfs_fs_info *fs_info, u64 qgroupid,
1345 struct btrfs_qgroup_limit *limit)
1346{
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001347 struct btrfs_root *quota_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001348 struct btrfs_qgroup *qgroup;
1349 int ret = 0;
Yang Dongshengfe759902015-06-03 14:57:32 +08001350 /* Sometimes we would want to clear the limit on this qgroup.
1351 * To meet this requirement, we treat the -1 as a special value
1352 * which tell kernel to clear the limit on this qgroup.
1353 */
1354 const u64 CLEAR_VALUE = -1;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001355
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001356 mutex_lock(&fs_info->qgroup_ioctl_lock);
1357 quota_root = fs_info->quota_root;
1358 if (!quota_root) {
1359 ret = -EINVAL;
1360 goto out;
1361 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001362
Wang Shilongddb47af2013-04-07 10:50:20 +00001363 qgroup = find_qgroup_rb(fs_info, qgroupid);
1364 if (!qgroup) {
1365 ret = -ENOENT;
1366 goto out;
1367 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001368
Wang Shilong58400fc2013-04-07 10:50:17 +00001369 spin_lock(&fs_info->qgroup_lock);
Yang Dongshengfe759902015-06-03 14:57:32 +08001370 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
1371 if (limit->max_rfer == CLEAR_VALUE) {
1372 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1373 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1374 qgroup->max_rfer = 0;
1375 } else {
1376 qgroup->max_rfer = limit->max_rfer;
1377 }
1378 }
1379 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
1380 if (limit->max_excl == CLEAR_VALUE) {
1381 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1382 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1383 qgroup->max_excl = 0;
1384 } else {
1385 qgroup->max_excl = limit->max_excl;
1386 }
1387 }
1388 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
1389 if (limit->rsv_rfer == CLEAR_VALUE) {
1390 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1391 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1392 qgroup->rsv_rfer = 0;
1393 } else {
1394 qgroup->rsv_rfer = limit->rsv_rfer;
1395 }
1396 }
1397 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
1398 if (limit->rsv_excl == CLEAR_VALUE) {
1399 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1400 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1401 qgroup->rsv_excl = 0;
1402 } else {
1403 qgroup->rsv_excl = limit->rsv_excl;
1404 }
1405 }
Dongsheng Yang03477d92015-02-06 11:06:25 -05001406 qgroup->lim_flags |= limit->flags;
1407
Arne Jansenbed92ea2012-06-28 18:03:02 +02001408 spin_unlock(&fs_info->qgroup_lock);
Dongsheng Yang1510e712014-11-20 21:01:41 -05001409
1410 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
1411 if (ret) {
1412 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1413 btrfs_info(fs_info, "unable to update quota limit for %llu",
1414 qgroupid);
1415 }
1416
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001417out:
1418 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001419 return ret;
1420}
Mark Fasheh11526512014-07-17 12:39:01 -07001421
Qu Wenruo3b7d00f2015-04-16 16:40:39 +08001422int btrfs_qgroup_prepare_account_extents(struct btrfs_trans_handle *trans,
1423 struct btrfs_fs_info *fs_info)
1424{
1425 struct btrfs_qgroup_extent_record *record;
1426 struct btrfs_delayed_ref_root *delayed_refs;
1427 struct rb_node *node;
Qu Wenruo9086db82015-04-20 09:53:50 +08001428 u64 qgroup_to_skip;
Qu Wenruo3b7d00f2015-04-16 16:40:39 +08001429 int ret = 0;
1430
1431 delayed_refs = &trans->transaction->delayed_refs;
Qu Wenruo9086db82015-04-20 09:53:50 +08001432 qgroup_to_skip = delayed_refs->qgroup_to_skip;
Qu Wenruo3b7d00f2015-04-16 16:40:39 +08001433
1434 /*
1435 * No need to do lock, since this function will only be called in
Nicholas D Steeves01327612016-05-19 21:18:45 -04001436 * btrfs_commit_transaction().
Qu Wenruo3b7d00f2015-04-16 16:40:39 +08001437 */
1438 node = rb_first(&delayed_refs->dirty_extent_root);
1439 while (node) {
1440 record = rb_entry(node, struct btrfs_qgroup_extent_record,
1441 node);
1442 ret = btrfs_find_all_roots(NULL, fs_info, record->bytenr, 0,
1443 &record->old_roots);
1444 if (ret < 0)
1445 break;
Qu Wenruo9086db82015-04-20 09:53:50 +08001446 if (qgroup_to_skip)
1447 ulist_del(record->old_roots, qgroup_to_skip, 0);
Qu Wenruo3b7d00f2015-04-16 16:40:39 +08001448 node = rb_next(node);
1449 }
1450 return ret;
1451}
1452
Qu Wenruocb93b522016-08-15 10:36:50 +08001453int btrfs_qgroup_insert_dirty_extent_nolock(struct btrfs_fs_info *fs_info,
1454 struct btrfs_delayed_ref_root *delayed_refs,
1455 struct btrfs_qgroup_extent_record *record)
Qu Wenruo3368d002015-04-16 14:34:17 +08001456{
1457 struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
1458 struct rb_node *parent_node = NULL;
1459 struct btrfs_qgroup_extent_record *entry;
1460 u64 bytenr = record->bytenr;
1461
Mark Fasheh82bd1012015-11-05 14:38:00 -08001462 assert_spin_locked(&delayed_refs->lock);
Jeff Mahoneybc074522016-06-09 17:27:55 -04001463 trace_btrfs_qgroup_insert_dirty_extent(fs_info, record);
Mark Fasheh82bd1012015-11-05 14:38:00 -08001464
Qu Wenruo3368d002015-04-16 14:34:17 +08001465 while (*p) {
1466 parent_node = *p;
1467 entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
1468 node);
1469 if (bytenr < entry->bytenr)
1470 p = &(*p)->rb_left;
1471 else if (bytenr > entry->bytenr)
1472 p = &(*p)->rb_right;
1473 else
Qu Wenruocb93b522016-08-15 10:36:50 +08001474 return 1;
Qu Wenruo3368d002015-04-16 14:34:17 +08001475 }
1476
1477 rb_link_node(&record->node, parent_node, p);
1478 rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
Qu Wenruocb93b522016-08-15 10:36:50 +08001479 return 0;
1480}
1481
1482int btrfs_qgroup_insert_dirty_extent(struct btrfs_trans_handle *trans,
1483 struct btrfs_fs_info *fs_info, u64 bytenr, u64 num_bytes,
1484 gfp_t gfp_flag)
1485{
1486 struct btrfs_qgroup_extent_record *record;
1487 struct btrfs_delayed_ref_root *delayed_refs;
1488 int ret;
1489
Josef Bacikafcdd122016-09-02 15:40:02 -04001490 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)
1491 || bytenr == 0 || num_bytes == 0)
Qu Wenruocb93b522016-08-15 10:36:50 +08001492 return 0;
1493 if (WARN_ON(trans == NULL))
1494 return -EINVAL;
1495 record = kmalloc(sizeof(*record), gfp_flag);
1496 if (!record)
1497 return -ENOMEM;
1498
1499 delayed_refs = &trans->transaction->delayed_refs;
1500 record->bytenr = bytenr;
1501 record->num_bytes = num_bytes;
1502 record->old_roots = NULL;
1503
1504 spin_lock(&delayed_refs->lock);
1505 ret = btrfs_qgroup_insert_dirty_extent_nolock(fs_info, delayed_refs,
1506 record);
1507 spin_unlock(&delayed_refs->lock);
1508 if (ret > 0)
1509 kfree(record);
1510 return 0;
Qu Wenruo3368d002015-04-16 14:34:17 +08001511}
1512
Qu Wenruod810ef22015-04-12 16:52:34 +08001513#define UPDATE_NEW 0
1514#define UPDATE_OLD 1
1515/*
1516 * Walk all of the roots that points to the bytenr and adjust their refcnts.
1517 */
1518static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
1519 struct ulist *roots, struct ulist *tmp,
1520 struct ulist *qgroups, u64 seq, int update_old)
1521{
1522 struct ulist_node *unode;
1523 struct ulist_iterator uiter;
1524 struct ulist_node *tmp_unode;
1525 struct ulist_iterator tmp_uiter;
1526 struct btrfs_qgroup *qg;
1527 int ret = 0;
1528
1529 if (!roots)
1530 return 0;
1531 ULIST_ITER_INIT(&uiter);
1532 while ((unode = ulist_next(roots, &uiter))) {
1533 qg = find_qgroup_rb(fs_info, unode->val);
1534 if (!qg)
1535 continue;
1536
1537 ulist_reinit(tmp);
1538 ret = ulist_add(qgroups, qg->qgroupid, ptr_to_u64(qg),
1539 GFP_ATOMIC);
1540 if (ret < 0)
1541 return ret;
1542 ret = ulist_add(tmp, qg->qgroupid, ptr_to_u64(qg), GFP_ATOMIC);
1543 if (ret < 0)
1544 return ret;
1545 ULIST_ITER_INIT(&tmp_uiter);
1546 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1547 struct btrfs_qgroup_list *glist;
1548
1549 qg = u64_to_ptr(tmp_unode->aux);
1550 if (update_old)
1551 btrfs_qgroup_update_old_refcnt(qg, seq, 1);
1552 else
1553 btrfs_qgroup_update_new_refcnt(qg, seq, 1);
1554 list_for_each_entry(glist, &qg->groups, next_group) {
1555 ret = ulist_add(qgroups, glist->group->qgroupid,
1556 ptr_to_u64(glist->group),
1557 GFP_ATOMIC);
1558 if (ret < 0)
1559 return ret;
1560 ret = ulist_add(tmp, glist->group->qgroupid,
1561 ptr_to_u64(glist->group),
1562 GFP_ATOMIC);
1563 if (ret < 0)
1564 return ret;
1565 }
1566 }
1567 }
1568 return 0;
1569}
1570
Josef Bacikfcebe452014-05-13 17:30:47 -07001571/*
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001572 * Update qgroup rfer/excl counters.
1573 * Rfer update is easy, codes can explain themselves.
Qu Wenruoe69bcee2015-04-17 10:23:16 +08001574 *
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001575 * Excl update is tricky, the update is split into 2 part.
1576 * Part 1: Possible exclusive <-> sharing detect:
1577 * | A | !A |
1578 * -------------------------------------
1579 * B | * | - |
1580 * -------------------------------------
1581 * !B | + | ** |
1582 * -------------------------------------
1583 *
1584 * Conditions:
1585 * A: cur_old_roots < nr_old_roots (not exclusive before)
1586 * !A: cur_old_roots == nr_old_roots (possible exclusive before)
1587 * B: cur_new_roots < nr_new_roots (not exclusive now)
Nicholas D Steeves01327612016-05-19 21:18:45 -04001588 * !B: cur_new_roots == nr_new_roots (possible exclusive now)
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001589 *
1590 * Results:
1591 * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
1592 * *: Definitely not changed. **: Possible unchanged.
1593 *
1594 * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
1595 *
1596 * To make the logic clear, we first use condition A and B to split
1597 * combination into 4 results.
1598 *
1599 * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
1600 * only on variant maybe 0.
1601 *
1602 * Lastly, check result **, since there are 2 variants maybe 0, split them
1603 * again(2x2).
1604 * But this time we don't need to consider other things, the codes and logic
1605 * is easy to understand now.
1606 */
1607static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
1608 struct ulist *qgroups,
1609 u64 nr_old_roots,
1610 u64 nr_new_roots,
1611 u64 num_bytes, u64 seq)
1612{
1613 struct ulist_node *unode;
1614 struct ulist_iterator uiter;
1615 struct btrfs_qgroup *qg;
1616 u64 cur_new_count, cur_old_count;
1617
1618 ULIST_ITER_INIT(&uiter);
1619 while ((unode = ulist_next(qgroups, &uiter))) {
1620 bool dirty = false;
1621
1622 qg = u64_to_ptr(unode->aux);
1623 cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
1624 cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
1625
Jeff Mahoneybc074522016-06-09 17:27:55 -04001626 trace_qgroup_update_counters(fs_info, qg->qgroupid,
1627 cur_old_count, cur_new_count);
Mark Fasheh0f5dcf82016-03-29 17:19:55 -07001628
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001629 /* Rfer update part */
1630 if (cur_old_count == 0 && cur_new_count > 0) {
1631 qg->rfer += num_bytes;
1632 qg->rfer_cmpr += num_bytes;
1633 dirty = true;
1634 }
1635 if (cur_old_count > 0 && cur_new_count == 0) {
1636 qg->rfer -= num_bytes;
1637 qg->rfer_cmpr -= num_bytes;
1638 dirty = true;
1639 }
1640
1641 /* Excl update part */
1642 /* Exclusive/none -> shared case */
1643 if (cur_old_count == nr_old_roots &&
1644 cur_new_count < nr_new_roots) {
1645 /* Exclusive -> shared */
1646 if (cur_old_count != 0) {
1647 qg->excl -= num_bytes;
1648 qg->excl_cmpr -= num_bytes;
1649 dirty = true;
1650 }
1651 }
1652
1653 /* Shared -> exclusive/none case */
1654 if (cur_old_count < nr_old_roots &&
1655 cur_new_count == nr_new_roots) {
1656 /* Shared->exclusive */
1657 if (cur_new_count != 0) {
1658 qg->excl += num_bytes;
1659 qg->excl_cmpr += num_bytes;
1660 dirty = true;
1661 }
1662 }
1663
1664 /* Exclusive/none -> exclusive/none case */
1665 if (cur_old_count == nr_old_roots &&
1666 cur_new_count == nr_new_roots) {
1667 if (cur_old_count == 0) {
1668 /* None -> exclusive/none */
1669
1670 if (cur_new_count != 0) {
1671 /* None -> exclusive */
1672 qg->excl += num_bytes;
1673 qg->excl_cmpr += num_bytes;
1674 dirty = true;
1675 }
1676 /* None -> none, nothing changed */
1677 } else {
1678 /* Exclusive -> exclusive/none */
1679
1680 if (cur_new_count == 0) {
1681 /* Exclusive -> none */
1682 qg->excl -= num_bytes;
1683 qg->excl_cmpr -= num_bytes;
1684 dirty = true;
1685 }
1686 /* Exclusive -> exclusive, nothing changed */
1687 }
1688 }
Qu Wenruoc05f9422015-08-03 14:44:29 +08001689
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001690 if (dirty)
1691 qgroup_dirty(fs_info, qg);
1692 }
1693 return 0;
1694}
1695
Qu Wenruo442244c2015-04-16 17:18:36 +08001696int
Qu Wenruo550d7a22015-04-16 15:37:33 +08001697btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans,
1698 struct btrfs_fs_info *fs_info,
1699 u64 bytenr, u64 num_bytes,
1700 struct ulist *old_roots, struct ulist *new_roots)
1701{
1702 struct ulist *qgroups = NULL;
1703 struct ulist *tmp = NULL;
1704 u64 seq;
1705 u64 nr_new_roots = 0;
1706 u64 nr_old_roots = 0;
1707 int ret = 0;
1708
1709 if (new_roots)
1710 nr_new_roots = new_roots->nnodes;
1711 if (old_roots)
1712 nr_old_roots = old_roots->nnodes;
1713
Josef Bacikafcdd122016-09-02 15:40:02 -04001714 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Qu Wenruo550d7a22015-04-16 15:37:33 +08001715 goto out_free;
1716 BUG_ON(!fs_info->quota_root);
1717
Jeff Mahoneybc074522016-06-09 17:27:55 -04001718 trace_btrfs_qgroup_account_extent(fs_info, bytenr, num_bytes,
1719 nr_old_roots, nr_new_roots);
Mark Fasheh0f5dcf82016-03-29 17:19:55 -07001720
Qu Wenruo550d7a22015-04-16 15:37:33 +08001721 qgroups = ulist_alloc(GFP_NOFS);
1722 if (!qgroups) {
1723 ret = -ENOMEM;
1724 goto out_free;
1725 }
1726 tmp = ulist_alloc(GFP_NOFS);
1727 if (!tmp) {
1728 ret = -ENOMEM;
1729 goto out_free;
1730 }
1731
1732 mutex_lock(&fs_info->qgroup_rescan_lock);
1733 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
1734 if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
1735 mutex_unlock(&fs_info->qgroup_rescan_lock);
1736 ret = 0;
1737 goto out_free;
1738 }
1739 }
1740 mutex_unlock(&fs_info->qgroup_rescan_lock);
1741
1742 spin_lock(&fs_info->qgroup_lock);
1743 seq = fs_info->qgroup_seq;
1744
1745 /* Update old refcnts using old_roots */
1746 ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
1747 UPDATE_OLD);
1748 if (ret < 0)
1749 goto out;
1750
1751 /* Update new refcnts using new_roots */
1752 ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
1753 UPDATE_NEW);
1754 if (ret < 0)
1755 goto out;
1756
1757 qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
1758 num_bytes, seq);
1759
1760 /*
1761 * Bump qgroup_seq to avoid seq overlap
1762 */
1763 fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
1764out:
1765 spin_unlock(&fs_info->qgroup_lock);
1766out_free:
1767 ulist_free(tmp);
1768 ulist_free(qgroups);
1769 ulist_free(old_roots);
1770 ulist_free(new_roots);
1771 return ret;
1772}
1773
1774int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans,
1775 struct btrfs_fs_info *fs_info)
1776{
1777 struct btrfs_qgroup_extent_record *record;
1778 struct btrfs_delayed_ref_root *delayed_refs;
1779 struct ulist *new_roots = NULL;
1780 struct rb_node *node;
Qu Wenruo9086db82015-04-20 09:53:50 +08001781 u64 qgroup_to_skip;
Qu Wenruo550d7a22015-04-16 15:37:33 +08001782 int ret = 0;
1783
1784 delayed_refs = &trans->transaction->delayed_refs;
Qu Wenruo9086db82015-04-20 09:53:50 +08001785 qgroup_to_skip = delayed_refs->qgroup_to_skip;
Qu Wenruo550d7a22015-04-16 15:37:33 +08001786 while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
1787 record = rb_entry(node, struct btrfs_qgroup_extent_record,
1788 node);
1789
Jeff Mahoneybc074522016-06-09 17:27:55 -04001790 trace_btrfs_qgroup_account_extents(fs_info, record);
Mark Fasheh0f5dcf82016-03-29 17:19:55 -07001791
Qu Wenruo550d7a22015-04-16 15:37:33 +08001792 if (!ret) {
1793 /*
1794 * Use (u64)-1 as time_seq to do special search, which
1795 * doesn't lock tree or delayed_refs and search current
1796 * root. It's safe inside commit_transaction().
1797 */
1798 ret = btrfs_find_all_roots(trans, fs_info,
1799 record->bytenr, (u64)-1, &new_roots);
1800 if (ret < 0)
1801 goto cleanup;
Qu Wenruo9086db82015-04-20 09:53:50 +08001802 if (qgroup_to_skip)
1803 ulist_del(new_roots, qgroup_to_skip, 0);
Qu Wenruo550d7a22015-04-16 15:37:33 +08001804 ret = btrfs_qgroup_account_extent(trans, fs_info,
1805 record->bytenr, record->num_bytes,
1806 record->old_roots, new_roots);
1807 record->old_roots = NULL;
1808 new_roots = NULL;
1809 }
1810cleanup:
1811 ulist_free(record->old_roots);
1812 ulist_free(new_roots);
1813 new_roots = NULL;
1814 rb_erase(node, &delayed_refs->dirty_extent_root);
1815 kfree(record);
1816
1817 }
1818 return ret;
1819}
1820
Josef Bacikfcebe452014-05-13 17:30:47 -07001821/*
Arne Jansenbed92ea2012-06-28 18:03:02 +02001822 * called from commit_transaction. Writes all changed qgroups to disk.
1823 */
1824int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
1825 struct btrfs_fs_info *fs_info)
1826{
1827 struct btrfs_root *quota_root = fs_info->quota_root;
1828 int ret = 0;
Jan Schmidt3d7b5a22013-04-25 16:04:52 +00001829 int start_rescan_worker = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001830
1831 if (!quota_root)
1832 goto out;
1833
Josef Bacikafcdd122016-09-02 15:40:02 -04001834 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
1835 test_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags))
Jan Schmidt3d7b5a22013-04-25 16:04:52 +00001836 start_rescan_worker = 1;
1837
Josef Bacikafcdd122016-09-02 15:40:02 -04001838 if (test_and_clear_bit(BTRFS_FS_QUOTA_ENABLING, &fs_info->flags))
1839 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
1840 if (test_and_clear_bit(BTRFS_FS_QUOTA_DISABLING, &fs_info->flags))
1841 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001842
1843 spin_lock(&fs_info->qgroup_lock);
1844 while (!list_empty(&fs_info->dirty_qgroups)) {
1845 struct btrfs_qgroup *qgroup;
1846 qgroup = list_first_entry(&fs_info->dirty_qgroups,
1847 struct btrfs_qgroup, dirty);
1848 list_del_init(&qgroup->dirty);
1849 spin_unlock(&fs_info->qgroup_lock);
1850 ret = update_qgroup_info_item(trans, quota_root, qgroup);
1851 if (ret)
1852 fs_info->qgroup_flags |=
1853 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Dongsheng Yangd3001ed2014-11-20 21:04:56 -05001854 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
1855 if (ret)
1856 fs_info->qgroup_flags |=
1857 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001858 spin_lock(&fs_info->qgroup_lock);
1859 }
Josef Bacikafcdd122016-09-02 15:40:02 -04001860 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Arne Jansenbed92ea2012-06-28 18:03:02 +02001861 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
1862 else
1863 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
1864 spin_unlock(&fs_info->qgroup_lock);
1865
1866 ret = update_qgroup_status_item(trans, fs_info, quota_root);
1867 if (ret)
1868 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1869
Jan Schmidt3d7b5a22013-04-25 16:04:52 +00001870 if (!ret && start_rescan_worker) {
Jan Schmidtb382a322013-05-28 15:47:24 +00001871 ret = qgroup_rescan_init(fs_info, 0, 1);
1872 if (!ret) {
1873 qgroup_rescan_zero_tracking(fs_info);
Qu Wenruofc97fab2014-02-28 10:46:16 +08001874 btrfs_queue_work(fs_info->qgroup_rescan_workers,
1875 &fs_info->qgroup_rescan_work);
Jan Schmidtb382a322013-05-28 15:47:24 +00001876 }
Jan Schmidt3d7b5a22013-04-25 16:04:52 +00001877 ret = 0;
1878 }
1879
Arne Jansenbed92ea2012-06-28 18:03:02 +02001880out:
1881
1882 return ret;
1883}
1884
1885/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04001886 * Copy the accounting information between qgroups. This is necessary
Mark Fasheh918c2ee2016-03-30 17:57:48 -07001887 * when a snapshot or a subvolume is created. Throwing an error will
1888 * cause a transaction abort so we take extra care here to only error
1889 * when a readonly fs is a reasonable outcome.
Arne Jansenbed92ea2012-06-28 18:03:02 +02001890 */
1891int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
1892 struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
1893 struct btrfs_qgroup_inherit *inherit)
1894{
1895 int ret = 0;
1896 int i;
1897 u64 *i_qgroups;
1898 struct btrfs_root *quota_root = fs_info->quota_root;
1899 struct btrfs_qgroup *srcgroup;
1900 struct btrfs_qgroup *dstgroup;
1901 u32 level_size = 0;
Wang Shilong3f5e2d32013-04-07 10:50:19 +00001902 u64 nums;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001903
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001904 mutex_lock(&fs_info->qgroup_ioctl_lock);
Josef Bacikafcdd122016-09-02 15:40:02 -04001905 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001906 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001907
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001908 if (!quota_root) {
1909 ret = -EINVAL;
1910 goto out;
1911 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001912
Wang Shilong3f5e2d32013-04-07 10:50:19 +00001913 if (inherit) {
1914 i_qgroups = (u64 *)(inherit + 1);
1915 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
1916 2 * inherit->num_excl_copies;
1917 for (i = 0; i < nums; ++i) {
1918 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
Dongsheng Yang09870d22014-11-11 07:18:22 -05001919
Mark Fasheh918c2ee2016-03-30 17:57:48 -07001920 /*
1921 * Zero out invalid groups so we can ignore
1922 * them later.
1923 */
1924 if (!srcgroup ||
1925 ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
1926 *i_qgroups = 0ULL;
1927
Wang Shilong3f5e2d32013-04-07 10:50:19 +00001928 ++i_qgroups;
1929 }
1930 }
1931
Arne Jansenbed92ea2012-06-28 18:03:02 +02001932 /*
1933 * create a tracking group for the subvol itself
1934 */
1935 ret = add_qgroup_item(trans, quota_root, objectid);
1936 if (ret)
1937 goto out;
1938
Arne Jansenbed92ea2012-06-28 18:03:02 +02001939 if (srcid) {
1940 struct btrfs_root *srcroot;
1941 struct btrfs_key srckey;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001942
1943 srckey.objectid = srcid;
1944 srckey.type = BTRFS_ROOT_ITEM_KEY;
1945 srckey.offset = (u64)-1;
1946 srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
1947 if (IS_ERR(srcroot)) {
1948 ret = PTR_ERR(srcroot);
1949 goto out;
1950 }
1951
1952 rcu_read_lock();
David Sterba707e8a02014-06-04 19:22:26 +02001953 level_size = srcroot->nodesize;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001954 rcu_read_unlock();
1955 }
1956
1957 /*
1958 * add qgroup to all inherited groups
1959 */
1960 if (inherit) {
1961 i_qgroups = (u64 *)(inherit + 1);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07001962 for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) {
1963 if (*i_qgroups == 0)
1964 continue;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001965 ret = add_qgroup_relation_item(trans, quota_root,
1966 objectid, *i_qgroups);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07001967 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001968 goto out;
1969 ret = add_qgroup_relation_item(trans, quota_root,
1970 *i_qgroups, objectid);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07001971 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001972 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001973 }
Mark Fasheh918c2ee2016-03-30 17:57:48 -07001974 ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001975 }
1976
1977
1978 spin_lock(&fs_info->qgroup_lock);
1979
1980 dstgroup = add_qgroup_rb(fs_info, objectid);
Dan Carpenter57a5a882012-07-30 02:15:43 -06001981 if (IS_ERR(dstgroup)) {
1982 ret = PTR_ERR(dstgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001983 goto unlock;
Dan Carpenter57a5a882012-07-30 02:15:43 -06001984 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001985
Dongsheng Yange8c85412014-11-20 20:58:34 -05001986 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
Dongsheng Yange8c85412014-11-20 20:58:34 -05001987 dstgroup->lim_flags = inherit->lim.flags;
1988 dstgroup->max_rfer = inherit->lim.max_rfer;
1989 dstgroup->max_excl = inherit->lim.max_excl;
1990 dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
1991 dstgroup->rsv_excl = inherit->lim.rsv_excl;
Dongsheng Yang1510e712014-11-20 21:01:41 -05001992
1993 ret = update_qgroup_limit_item(trans, quota_root, dstgroup);
1994 if (ret) {
1995 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001996 btrfs_info(fs_info,
1997 "unable to update quota limit for %llu",
1998 dstgroup->qgroupid);
Dongsheng Yang1510e712014-11-20 21:01:41 -05001999 goto unlock;
2000 }
Dongsheng Yange8c85412014-11-20 20:58:34 -05002001 }
2002
Arne Jansenbed92ea2012-06-28 18:03:02 +02002003 if (srcid) {
2004 srcgroup = find_qgroup_rb(fs_info, srcid);
Chris Masonf3a87f12012-09-14 20:06:30 -04002005 if (!srcgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002006 goto unlock;
Josef Bacikfcebe452014-05-13 17:30:47 -07002007
2008 /*
2009 * We call inherit after we clone the root in order to make sure
2010 * our counts don't go crazy, so at this point the only
2011 * difference between the two roots should be the root node.
2012 */
2013 dstgroup->rfer = srcgroup->rfer;
2014 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
2015 dstgroup->excl = level_size;
2016 dstgroup->excl_cmpr = level_size;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002017 srcgroup->excl = level_size;
2018 srcgroup->excl_cmpr = level_size;
Dongsheng Yang3eeb4d52014-11-20 20:14:38 -05002019
2020 /* inherit the limit info */
2021 dstgroup->lim_flags = srcgroup->lim_flags;
2022 dstgroup->max_rfer = srcgroup->max_rfer;
2023 dstgroup->max_excl = srcgroup->max_excl;
2024 dstgroup->rsv_rfer = srcgroup->rsv_rfer;
2025 dstgroup->rsv_excl = srcgroup->rsv_excl;
2026
Arne Jansenbed92ea2012-06-28 18:03:02 +02002027 qgroup_dirty(fs_info, dstgroup);
2028 qgroup_dirty(fs_info, srcgroup);
2029 }
2030
Chris Masonf3a87f12012-09-14 20:06:30 -04002031 if (!inherit)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002032 goto unlock;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002033
2034 i_qgroups = (u64 *)(inherit + 1);
2035 for (i = 0; i < inherit->num_qgroups; ++i) {
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002036 if (*i_qgroups) {
2037 ret = add_relation_rb(quota_root->fs_info, objectid,
2038 *i_qgroups);
2039 if (ret)
2040 goto unlock;
2041 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002042 ++i_qgroups;
2043 }
2044
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002045 for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002046 struct btrfs_qgroup *src;
2047 struct btrfs_qgroup *dst;
2048
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002049 if (!i_qgroups[0] || !i_qgroups[1])
2050 continue;
2051
Arne Jansenbed92ea2012-06-28 18:03:02 +02002052 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2053 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2054
2055 if (!src || !dst) {
2056 ret = -EINVAL;
2057 goto unlock;
2058 }
2059
2060 dst->rfer = src->rfer - level_size;
2061 dst->rfer_cmpr = src->rfer_cmpr - level_size;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002062 }
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002063 for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002064 struct btrfs_qgroup *src;
2065 struct btrfs_qgroup *dst;
2066
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002067 if (!i_qgroups[0] || !i_qgroups[1])
2068 continue;
2069
Arne Jansenbed92ea2012-06-28 18:03:02 +02002070 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2071 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2072
2073 if (!src || !dst) {
2074 ret = -EINVAL;
2075 goto unlock;
2076 }
2077
2078 dst->excl = src->excl + level_size;
2079 dst->excl_cmpr = src->excl_cmpr + level_size;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002080 }
2081
2082unlock:
2083 spin_unlock(&fs_info->qgroup_lock);
2084out:
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002085 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002086 return ret;
2087}
2088
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002089static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002090{
2091 struct btrfs_root *quota_root;
2092 struct btrfs_qgroup *qgroup;
2093 struct btrfs_fs_info *fs_info = root->fs_info;
2094 u64 ref_root = root->root_key.objectid;
2095 int ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002096 struct ulist_node *unode;
2097 struct ulist_iterator uiter;
2098
2099 if (!is_fstree(ref_root))
2100 return 0;
2101
2102 if (num_bytes == 0)
2103 return 0;
2104
2105 spin_lock(&fs_info->qgroup_lock);
2106 quota_root = fs_info->quota_root;
2107 if (!quota_root)
2108 goto out;
2109
2110 qgroup = find_qgroup_rb(fs_info, ref_root);
2111 if (!qgroup)
2112 goto out;
2113
2114 /*
2115 * in a first step, we check all affected qgroups if any limits would
2116 * be exceeded
2117 */
Wang Shilong1e8f9152013-05-06 11:03:27 +00002118 ulist_reinit(fs_info->qgroup_ulist);
2119 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
Wang Shilong3c971852013-04-17 14:00:36 +00002120 (uintptr_t)qgroup, GFP_ATOMIC);
2121 if (ret < 0)
2122 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002123 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00002124 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002125 struct btrfs_qgroup *qg;
2126 struct btrfs_qgroup_list *glist;
2127
Josef Bacikfcebe452014-05-13 17:30:47 -07002128 qg = u64_to_ptr(unode->aux);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002129
2130 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
Dongsheng Yange2d1f922015-02-06 10:26:52 -05002131 qg->reserved + (s64)qg->rfer + num_bytes >
Wang Shilong720f1e22013-03-06 11:51:47 +00002132 qg->max_rfer) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002133 ret = -EDQUOT;
Wang Shilong720f1e22013-03-06 11:51:47 +00002134 goto out;
2135 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002136
2137 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
Dongsheng Yange2d1f922015-02-06 10:26:52 -05002138 qg->reserved + (s64)qg->excl + num_bytes >
Wang Shilong720f1e22013-03-06 11:51:47 +00002139 qg->max_excl) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002140 ret = -EDQUOT;
Wang Shilong720f1e22013-03-06 11:51:47 +00002141 goto out;
2142 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002143
2144 list_for_each_entry(glist, &qg->groups, next_group) {
Wang Shilong1e8f9152013-05-06 11:03:27 +00002145 ret = ulist_add(fs_info->qgroup_ulist,
2146 glist->group->qgroupid,
Wang Shilong3c971852013-04-17 14:00:36 +00002147 (uintptr_t)glist->group, GFP_ATOMIC);
2148 if (ret < 0)
2149 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002150 }
2151 }
Wang Shilong3c971852013-04-17 14:00:36 +00002152 ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002153 /*
2154 * no limits exceeded, now record the reservation into all qgroups
2155 */
2156 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00002157 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002158 struct btrfs_qgroup *qg;
2159
Josef Bacikfcebe452014-05-13 17:30:47 -07002160 qg = u64_to_ptr(unode->aux);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002161
Dongsheng Yange2d1f922015-02-06 10:26:52 -05002162 qg->reserved += num_bytes;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002163 }
2164
2165out:
2166 spin_unlock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002167 return ret;
2168}
2169
Qu Wenruo297d7502015-09-08 17:08:37 +08002170void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
2171 u64 ref_root, u64 num_bytes)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002172{
2173 struct btrfs_root *quota_root;
2174 struct btrfs_qgroup *qgroup;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002175 struct ulist_node *unode;
2176 struct ulist_iterator uiter;
Wang Shilong3c971852013-04-17 14:00:36 +00002177 int ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002178
2179 if (!is_fstree(ref_root))
2180 return;
2181
2182 if (num_bytes == 0)
2183 return;
2184
2185 spin_lock(&fs_info->qgroup_lock);
2186
2187 quota_root = fs_info->quota_root;
2188 if (!quota_root)
2189 goto out;
2190
2191 qgroup = find_qgroup_rb(fs_info, ref_root);
2192 if (!qgroup)
2193 goto out;
2194
Wang Shilong1e8f9152013-05-06 11:03:27 +00002195 ulist_reinit(fs_info->qgroup_ulist);
2196 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
Wang Shilong3c971852013-04-17 14:00:36 +00002197 (uintptr_t)qgroup, GFP_ATOMIC);
2198 if (ret < 0)
2199 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002200 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00002201 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002202 struct btrfs_qgroup *qg;
2203 struct btrfs_qgroup_list *glist;
2204
Josef Bacikfcebe452014-05-13 17:30:47 -07002205 qg = u64_to_ptr(unode->aux);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002206
Dongsheng Yange2d1f922015-02-06 10:26:52 -05002207 qg->reserved -= num_bytes;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002208
2209 list_for_each_entry(glist, &qg->groups, next_group) {
Wang Shilong1e8f9152013-05-06 11:03:27 +00002210 ret = ulist_add(fs_info->qgroup_ulist,
2211 glist->group->qgroupid,
Wang Shilong3c971852013-04-17 14:00:36 +00002212 (uintptr_t)glist->group, GFP_ATOMIC);
2213 if (ret < 0)
2214 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002215 }
2216 }
2217
2218out:
2219 spin_unlock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002220}
2221
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002222static inline void qgroup_free(struct btrfs_root *root, u64 num_bytes)
2223{
2224 return btrfs_qgroup_free_refroot(root->fs_info, root->objectid,
2225 num_bytes);
2226}
Arne Jansenbed92ea2012-06-28 18:03:02 +02002227void assert_qgroups_uptodate(struct btrfs_trans_handle *trans)
2228{
2229 if (list_empty(&trans->qgroup_ref_list) && !trans->delayed_ref_elem.seq)
2230 return;
Jeff Mahoney64b63582016-06-20 17:23:41 -04002231 btrfs_err(trans->fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002232 "qgroups not uptodate in trans handle %p: list is%s empty, seq is %#x.%x",
Arne Jansenbed92ea2012-06-28 18:03:02 +02002233 trans, list_empty(&trans->qgroup_ref_list) ? "" : " not",
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +00002234 (u32)(trans->delayed_ref_elem.seq >> 32),
2235 (u32)trans->delayed_ref_elem.seq);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002236 BUG();
2237}
Jan Schmidt2f232032013-04-25 16:04:51 +00002238
2239/*
2240 * returns < 0 on error, 0 when more leafs are to be scanned.
Qu Wenruo33931682015-02-27 16:24:24 +08002241 * returns 1 when done.
Jan Schmidt2f232032013-04-25 16:04:51 +00002242 */
2243static int
Jan Schmidtb382a322013-05-28 15:47:24 +00002244qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
Qu Wenruo0a0e8b82015-10-26 09:19:43 +08002245 struct btrfs_trans_handle *trans)
Jan Schmidt2f232032013-04-25 16:04:51 +00002246{
2247 struct btrfs_key found;
Qu Wenruo0a0e8b82015-10-26 09:19:43 +08002248 struct extent_buffer *scratch_leaf = NULL;
Jan Schmidt2f232032013-04-25 16:04:51 +00002249 struct ulist *roots = NULL;
David Sterba3284da72015-02-25 15:47:32 +01002250 struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
Josef Bacikfcebe452014-05-13 17:30:47 -07002251 u64 num_bytes;
Jan Schmidt2f232032013-04-25 16:04:51 +00002252 int slot;
2253 int ret;
2254
Jan Schmidt2f232032013-04-25 16:04:51 +00002255 mutex_lock(&fs_info->qgroup_rescan_lock);
2256 ret = btrfs_search_slot_for_read(fs_info->extent_root,
2257 &fs_info->qgroup_rescan_progress,
2258 path, 1, 0);
2259
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002260 btrfs_debug(fs_info,
2261 "current progress key (%llu %u %llu), search_slot ret %d",
2262 fs_info->qgroup_rescan_progress.objectid,
2263 fs_info->qgroup_rescan_progress.type,
2264 fs_info->qgroup_rescan_progress.offset, ret);
Jan Schmidt2f232032013-04-25 16:04:51 +00002265
2266 if (ret) {
2267 /*
2268 * The rescan is about to end, we will not be scanning any
2269 * further blocks. We cannot unset the RESCAN flag here, because
2270 * we want to commit the transaction if everything went well.
2271 * To make the live accounting work in this phase, we set our
2272 * scan progress pointer such that every real extent objectid
2273 * will be smaller.
2274 */
2275 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
2276 btrfs_release_path(path);
2277 mutex_unlock(&fs_info->qgroup_rescan_lock);
2278 return ret;
2279 }
2280
2281 btrfs_item_key_to_cpu(path->nodes[0], &found,
2282 btrfs_header_nritems(path->nodes[0]) - 1);
2283 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
2284
2285 btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
Qu Wenruo0a0e8b82015-10-26 09:19:43 +08002286 scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
2287 if (!scratch_leaf) {
2288 ret = -ENOMEM;
2289 mutex_unlock(&fs_info->qgroup_rescan_lock);
2290 goto out;
2291 }
2292 extent_buffer_get(scratch_leaf);
2293 btrfs_tree_read_lock(scratch_leaf);
2294 btrfs_set_lock_blocking_rw(scratch_leaf, BTRFS_READ_LOCK);
Jan Schmidt2f232032013-04-25 16:04:51 +00002295 slot = path->slots[0];
2296 btrfs_release_path(path);
2297 mutex_unlock(&fs_info->qgroup_rescan_lock);
2298
2299 for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
2300 btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
Josef Bacik3a6d75e2014-01-23 16:45:10 -05002301 if (found.type != BTRFS_EXTENT_ITEM_KEY &&
2302 found.type != BTRFS_METADATA_ITEM_KEY)
Jan Schmidt2f232032013-04-25 16:04:51 +00002303 continue;
Josef Bacik3a6d75e2014-01-23 16:45:10 -05002304 if (found.type == BTRFS_METADATA_ITEM_KEY)
David Sterba707e8a02014-06-04 19:22:26 +02002305 num_bytes = fs_info->extent_root->nodesize;
Josef Bacik3a6d75e2014-01-23 16:45:10 -05002306 else
2307 num_bytes = found.offset;
2308
Josef Bacikfcebe452014-05-13 17:30:47 -07002309 ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
2310 &roots);
Jan Schmidt2f232032013-04-25 16:04:51 +00002311 if (ret < 0)
2312 goto out;
Qu Wenruo9d220c92015-04-13 11:02:16 +08002313 /* For rescan, just pass old_roots as NULL */
2314 ret = btrfs_qgroup_account_extent(trans, fs_info,
2315 found.objectid, num_bytes, NULL, roots);
2316 if (ret < 0)
Jan Schmidt2f232032013-04-25 16:04:51 +00002317 goto out;
Jan Schmidt2f232032013-04-25 16:04:51 +00002318 }
Jan Schmidt2f232032013-04-25 16:04:51 +00002319out:
Qu Wenruo0a0e8b82015-10-26 09:19:43 +08002320 if (scratch_leaf) {
2321 btrfs_tree_read_unlock_blocking(scratch_leaf);
2322 free_extent_buffer(scratch_leaf);
2323 }
Jan Schmidt2f232032013-04-25 16:04:51 +00002324 btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
2325
2326 return ret;
2327}
2328
Qu Wenruod458b052014-02-28 10:46:19 +08002329static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
Jan Schmidt2f232032013-04-25 16:04:51 +00002330{
Jan Schmidtb382a322013-05-28 15:47:24 +00002331 struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
2332 qgroup_rescan_work);
Jan Schmidt2f232032013-04-25 16:04:51 +00002333 struct btrfs_path *path;
2334 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt2f232032013-04-25 16:04:51 +00002335 int err = -ENOMEM;
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002336 int ret = 0;
Jan Schmidt2f232032013-04-25 16:04:51 +00002337
2338 path = btrfs_alloc_path();
2339 if (!path)
2340 goto out;
Jan Schmidt2f232032013-04-25 16:04:51 +00002341
2342 err = 0;
Justin Maggard7343dd62015-11-04 15:56:16 -08002343 while (!err && !btrfs_fs_closing(fs_info)) {
Jan Schmidt2f232032013-04-25 16:04:51 +00002344 trans = btrfs_start_transaction(fs_info->fs_root, 0);
2345 if (IS_ERR(trans)) {
2346 err = PTR_ERR(trans);
2347 break;
2348 }
Josef Bacikafcdd122016-09-02 15:40:02 -04002349 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
Jan Schmidt2f232032013-04-25 16:04:51 +00002350 err = -EINTR;
2351 } else {
Qu Wenruo0a0e8b82015-10-26 09:19:43 +08002352 err = qgroup_rescan_leaf(fs_info, path, trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00002353 }
2354 if (err > 0)
2355 btrfs_commit_transaction(trans, fs_info->fs_root);
2356 else
2357 btrfs_end_transaction(trans, fs_info->fs_root);
2358 }
2359
2360out:
Jan Schmidt2f232032013-04-25 16:04:51 +00002361 btrfs_free_path(path);
Jan Schmidt2f232032013-04-25 16:04:51 +00002362
2363 mutex_lock(&fs_info->qgroup_rescan_lock);
Justin Maggard7343dd62015-11-04 15:56:16 -08002364 if (!btrfs_fs_closing(fs_info))
2365 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
Jan Schmidt2f232032013-04-25 16:04:51 +00002366
Qu Wenruo33931682015-02-27 16:24:24 +08002367 if (err > 0 &&
Jan Schmidt2f232032013-04-25 16:04:51 +00002368 fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
2369 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2370 } else if (err < 0) {
2371 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2372 }
2373 mutex_unlock(&fs_info->qgroup_rescan_lock);
2374
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002375 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002376 * only update status, since the previous part has already updated the
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002377 * qgroup info.
2378 */
2379 trans = btrfs_start_transaction(fs_info->quota_root, 1);
2380 if (IS_ERR(trans)) {
2381 err = PTR_ERR(trans);
2382 btrfs_err(fs_info,
2383 "fail to start transaction for status update: %d\n",
2384 err);
2385 goto done;
2386 }
2387 ret = update_qgroup_status_item(trans, fs_info, fs_info->quota_root);
2388 if (ret < 0) {
2389 err = ret;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002390 btrfs_err(fs_info, "fail to update qgroup status: %d", err);
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002391 }
2392 btrfs_end_transaction(trans, fs_info->quota_root);
2393
Justin Maggard7343dd62015-11-04 15:56:16 -08002394 if (btrfs_fs_closing(fs_info)) {
2395 btrfs_info(fs_info, "qgroup scan paused");
2396 } else if (err >= 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05002397 btrfs_info(fs_info, "qgroup scan completed%s",
Qu Wenruo33931682015-02-27 16:24:24 +08002398 err > 0 ? " (inconsistency flag cleared)" : "");
Jan Schmidt2f232032013-04-25 16:04:51 +00002399 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05002400 btrfs_err(fs_info, "qgroup scan failed with %d", err);
Jan Schmidt2f232032013-04-25 16:04:51 +00002401 }
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002402
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002403done:
Jeff Mahoneyd2c609b2016-08-15 12:10:33 -04002404 mutex_lock(&fs_info->qgroup_rescan_lock);
2405 fs_info->qgroup_rescan_running = false;
2406 mutex_unlock(&fs_info->qgroup_rescan_lock);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002407 complete_all(&fs_info->qgroup_rescan_completion);
Jan Schmidt2f232032013-04-25 16:04:51 +00002408}
2409
Jan Schmidtb382a322013-05-28 15:47:24 +00002410/*
2411 * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
2412 * memory required for the rescan context.
2413 */
2414static int
2415qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
2416 int init_flags)
Jan Schmidt2f232032013-04-25 16:04:51 +00002417{
2418 int ret = 0;
Jan Schmidt2f232032013-04-25 16:04:51 +00002419
Jan Schmidtb382a322013-05-28 15:47:24 +00002420 if (!init_flags &&
2421 (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
2422 !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
2423 ret = -EINVAL;
2424 goto err;
2425 }
Jan Schmidt2f232032013-04-25 16:04:51 +00002426
2427 mutex_lock(&fs_info->qgroup_rescan_lock);
2428 spin_lock(&fs_info->qgroup_lock);
Jan Schmidtb382a322013-05-28 15:47:24 +00002429
2430 if (init_flags) {
2431 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
2432 ret = -EINPROGRESS;
2433 else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
2434 ret = -EINVAL;
2435
2436 if (ret) {
2437 spin_unlock(&fs_info->qgroup_lock);
2438 mutex_unlock(&fs_info->qgroup_rescan_lock);
2439 goto err;
2440 }
Jan Schmidtb382a322013-05-28 15:47:24 +00002441 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2442 }
2443
2444 memset(&fs_info->qgroup_rescan_progress, 0,
2445 sizeof(fs_info->qgroup_rescan_progress));
2446 fs_info->qgroup_rescan_progress.objectid = progress_objectid;
Filipe Manana190631f2015-11-05 10:06:23 +00002447 init_completion(&fs_info->qgroup_rescan_completion);
Filipe Manana1a5ec7d2016-11-24 02:09:04 +00002448 fs_info->qgroup_rescan_running = true;
Jan Schmidtb382a322013-05-28 15:47:24 +00002449
2450 spin_unlock(&fs_info->qgroup_lock);
2451 mutex_unlock(&fs_info->qgroup_rescan_lock);
2452
Jan Schmidtb382a322013-05-28 15:47:24 +00002453 memset(&fs_info->qgroup_rescan_work, 0,
2454 sizeof(fs_info->qgroup_rescan_work));
Qu Wenruofc97fab2014-02-28 10:46:16 +08002455 btrfs_init_work(&fs_info->qgroup_rescan_work,
Liu Bo9e0af232014-08-15 23:36:53 +08002456 btrfs_qgroup_rescan_helper,
Qu Wenruofc97fab2014-02-28 10:46:16 +08002457 btrfs_qgroup_rescan_worker, NULL, NULL);
Jan Schmidtb382a322013-05-28 15:47:24 +00002458
Jan Schmidt2f232032013-04-25 16:04:51 +00002459 if (ret) {
Jan Schmidtb382a322013-05-28 15:47:24 +00002460err:
Frank Holtonefe120a2013-12-20 11:37:06 -05002461 btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret);
Jan Schmidt2f232032013-04-25 16:04:51 +00002462 return ret;
2463 }
2464
Jan Schmidtb382a322013-05-28 15:47:24 +00002465 return 0;
2466}
Jan Schmidt2f232032013-04-25 16:04:51 +00002467
Jan Schmidtb382a322013-05-28 15:47:24 +00002468static void
2469qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
2470{
2471 struct rb_node *n;
2472 struct btrfs_qgroup *qgroup;
2473
2474 spin_lock(&fs_info->qgroup_lock);
Jan Schmidt2f232032013-04-25 16:04:51 +00002475 /* clear all current qgroup tracking information */
2476 for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
2477 qgroup = rb_entry(n, struct btrfs_qgroup, node);
2478 qgroup->rfer = 0;
2479 qgroup->rfer_cmpr = 0;
2480 qgroup->excl = 0;
2481 qgroup->excl_cmpr = 0;
2482 }
2483 spin_unlock(&fs_info->qgroup_lock);
Jan Schmidtb382a322013-05-28 15:47:24 +00002484}
Jan Schmidt2f232032013-04-25 16:04:51 +00002485
Jan Schmidtb382a322013-05-28 15:47:24 +00002486int
2487btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
2488{
2489 int ret = 0;
2490 struct btrfs_trans_handle *trans;
2491
2492 ret = qgroup_rescan_init(fs_info, 0, 1);
2493 if (ret)
2494 return ret;
2495
2496 /*
2497 * We have set the rescan_progress to 0, which means no more
2498 * delayed refs will be accounted by btrfs_qgroup_account_ref.
2499 * However, btrfs_qgroup_account_ref may be right after its call
2500 * to btrfs_find_all_roots, in which case it would still do the
2501 * accounting.
2502 * To solve this, we're committing the transaction, which will
2503 * ensure we run all delayed refs and only after that, we are
2504 * going to clear all tracking information for a clean start.
2505 */
2506
2507 trans = btrfs_join_transaction(fs_info->fs_root);
2508 if (IS_ERR(trans)) {
2509 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2510 return PTR_ERR(trans);
2511 }
2512 ret = btrfs_commit_transaction(trans, fs_info->fs_root);
2513 if (ret) {
2514 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2515 return ret;
2516 }
2517
2518 qgroup_rescan_zero_tracking(fs_info);
2519
Qu Wenruofc97fab2014-02-28 10:46:16 +08002520 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2521 &fs_info->qgroup_rescan_work);
Jan Schmidt2f232032013-04-25 16:04:51 +00002522
2523 return 0;
2524}
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002525
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04002526int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
2527 bool interruptible)
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002528{
2529 int running;
2530 int ret = 0;
2531
2532 mutex_lock(&fs_info->qgroup_rescan_lock);
2533 spin_lock(&fs_info->qgroup_lock);
Jeff Mahoneyd2c609b2016-08-15 12:10:33 -04002534 running = fs_info->qgroup_rescan_running;
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002535 spin_unlock(&fs_info->qgroup_lock);
2536 mutex_unlock(&fs_info->qgroup_rescan_lock);
2537
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04002538 if (!running)
2539 return 0;
2540
2541 if (interruptible)
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002542 ret = wait_for_completion_interruptible(
2543 &fs_info->qgroup_rescan_completion);
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04002544 else
2545 wait_for_completion(&fs_info->qgroup_rescan_completion);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002546
2547 return ret;
2548}
Jan Schmidtb382a322013-05-28 15:47:24 +00002549
2550/*
2551 * this is only called from open_ctree where we're still single threaded, thus
2552 * locking is omitted here.
2553 */
2554void
2555btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
2556{
2557 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
Qu Wenruofc97fab2014-02-28 10:46:16 +08002558 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2559 &fs_info->qgroup_rescan_work);
Jan Schmidtb382a322013-05-28 15:47:24 +00002560}
Qu Wenruo52472552015-10-12 16:05:40 +08002561
2562/*
2563 * Reserve qgroup space for range [start, start + len).
2564 *
2565 * This function will either reserve space from related qgroups or doing
2566 * nothing if the range is already reserved.
2567 *
2568 * Return 0 for successful reserve
2569 * Return <0 for error (including -EQUOT)
2570 *
2571 * NOTE: this function may sleep for memory allocation.
2572 */
2573int btrfs_qgroup_reserve_data(struct inode *inode, u64 start, u64 len)
2574{
2575 struct btrfs_root *root = BTRFS_I(inode)->root;
2576 struct extent_changeset changeset;
2577 struct ulist_node *unode;
2578 struct ulist_iterator uiter;
2579 int ret;
2580
Josef Bacikafcdd122016-09-02 15:40:02 -04002581 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
2582 !is_fstree(root->objectid) || len == 0)
Qu Wenruo52472552015-10-12 16:05:40 +08002583 return 0;
2584
2585 changeset.bytes_changed = 0;
2586 changeset.range_changed = ulist_alloc(GFP_NOFS);
Qu Wenruo52472552015-10-12 16:05:40 +08002587 ret = set_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
David Sterba2c53b912016-04-26 23:54:39 +02002588 start + len -1, EXTENT_QGROUP_RESERVED, &changeset);
Qu Wenruo81fb6f72015-09-28 16:57:53 +08002589 trace_btrfs_qgroup_reserve_data(inode, start, len,
2590 changeset.bytes_changed,
2591 QGROUP_RESERVE);
Qu Wenruo52472552015-10-12 16:05:40 +08002592 if (ret < 0)
2593 goto cleanup;
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002594 ret = qgroup_reserve(root, changeset.bytes_changed);
Qu Wenruo52472552015-10-12 16:05:40 +08002595 if (ret < 0)
2596 goto cleanup;
2597
2598 ulist_free(changeset.range_changed);
2599 return ret;
2600
2601cleanup:
2602 /* cleanup already reserved ranges */
2603 ULIST_ITER_INIT(&uiter);
2604 while ((unode = ulist_next(changeset.range_changed, &uiter)))
2605 clear_extent_bit(&BTRFS_I(inode)->io_tree, unode->val,
2606 unode->aux, EXTENT_QGROUP_RESERVED, 0, 0, NULL,
2607 GFP_NOFS);
2608 ulist_free(changeset.range_changed);
2609 return ret;
2610}
Qu Wenruof695fdc2015-10-12 16:28:06 +08002611
2612static int __btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len,
2613 int free)
2614{
2615 struct extent_changeset changeset;
Qu Wenruo81fb6f72015-09-28 16:57:53 +08002616 int trace_op = QGROUP_RELEASE;
Qu Wenruof695fdc2015-10-12 16:28:06 +08002617 int ret;
2618
2619 changeset.bytes_changed = 0;
2620 changeset.range_changed = ulist_alloc(GFP_NOFS);
2621 if (!changeset.range_changed)
2622 return -ENOMEM;
2623
2624 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
David Sterbaf734c442016-04-26 23:54:39 +02002625 start + len -1, EXTENT_QGROUP_RESERVED, &changeset);
Qu Wenruof695fdc2015-10-12 16:28:06 +08002626 if (ret < 0)
2627 goto out;
2628
Qu Wenruo81fb6f72015-09-28 16:57:53 +08002629 if (free) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002630 qgroup_free(BTRFS_I(inode)->root, changeset.bytes_changed);
Qu Wenruo81fb6f72015-09-28 16:57:53 +08002631 trace_op = QGROUP_FREE;
2632 }
2633 trace_btrfs_qgroup_release_data(inode, start, len,
2634 changeset.bytes_changed, trace_op);
Qu Wenruof695fdc2015-10-12 16:28:06 +08002635out:
2636 ulist_free(changeset.range_changed);
2637 return ret;
2638}
2639
2640/*
2641 * Free a reserved space range from io_tree and related qgroups
2642 *
2643 * Should be called when a range of pages get invalidated before reaching disk.
2644 * Or for error cleanup case.
2645 *
2646 * For data written to disk, use btrfs_qgroup_release_data().
2647 *
2648 * NOTE: This function may sleep for memory allocation.
2649 */
2650int btrfs_qgroup_free_data(struct inode *inode, u64 start, u64 len)
2651{
2652 return __btrfs_qgroup_release_data(inode, start, len, 1);
2653}
2654
2655/*
2656 * Release a reserved space range from io_tree only.
2657 *
2658 * Should be called when a range of pages get written to disk and corresponding
2659 * FILE_EXTENT is inserted into corresponding root.
2660 *
2661 * Since new qgroup accounting framework will only update qgroup numbers at
2662 * commit_transaction() time, its reserved space shouldn't be freed from
2663 * related qgroups.
2664 *
2665 * But we should release the range from io_tree, to allow further write to be
2666 * COWed.
2667 *
2668 * NOTE: This function may sleep for memory allocation.
2669 */
2670int btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len)
2671{
2672 return __btrfs_qgroup_release_data(inode, start, len, 0);
2673}
Qu Wenruo55eeaf02015-09-08 17:08:38 +08002674
2675int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes)
2676{
2677 int ret;
2678
Josef Bacikafcdd122016-09-02 15:40:02 -04002679 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
2680 !is_fstree(root->objectid) || num_bytes == 0)
Qu Wenruo55eeaf02015-09-08 17:08:38 +08002681 return 0;
2682
2683 BUG_ON(num_bytes != round_down(num_bytes, root->nodesize));
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002684 ret = qgroup_reserve(root, num_bytes);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08002685 if (ret < 0)
2686 return ret;
2687 atomic_add(num_bytes, &root->qgroup_meta_rsv);
2688 return ret;
2689}
2690
2691void btrfs_qgroup_free_meta_all(struct btrfs_root *root)
2692{
2693 int reserved;
2694
Josef Bacikafcdd122016-09-02 15:40:02 -04002695 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
2696 !is_fstree(root->objectid))
Qu Wenruo55eeaf02015-09-08 17:08:38 +08002697 return;
2698
2699 reserved = atomic_xchg(&root->qgroup_meta_rsv, 0);
2700 if (reserved == 0)
2701 return;
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002702 qgroup_free(root, reserved);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08002703}
2704
2705void btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes)
2706{
Josef Bacikafcdd122016-09-02 15:40:02 -04002707 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
2708 !is_fstree(root->objectid))
Qu Wenruo55eeaf02015-09-08 17:08:38 +08002709 return;
2710
2711 BUG_ON(num_bytes != round_down(num_bytes, root->nodesize));
2712 WARN_ON(atomic_read(&root->qgroup_meta_rsv) < num_bytes);
2713 atomic_sub(num_bytes, &root->qgroup_meta_rsv);
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002714 qgroup_free(root, num_bytes);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08002715}
Qu Wenruo56fa9d02015-10-13 09:53:10 +08002716
2717/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002718 * Check qgroup reserved space leaking, normally at destroy inode
Qu Wenruo56fa9d02015-10-13 09:53:10 +08002719 * time
2720 */
2721void btrfs_qgroup_check_reserved_leak(struct inode *inode)
2722{
2723 struct extent_changeset changeset;
2724 struct ulist_node *unode;
2725 struct ulist_iterator iter;
2726 int ret;
2727
2728 changeset.bytes_changed = 0;
2729 changeset.range_changed = ulist_alloc(GFP_NOFS);
2730 if (WARN_ON(!changeset.range_changed))
2731 return;
2732
2733 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
David Sterbaf734c442016-04-26 23:54:39 +02002734 EXTENT_QGROUP_RESERVED, &changeset);
Qu Wenruo56fa9d02015-10-13 09:53:10 +08002735
2736 WARN_ON(ret < 0);
2737 if (WARN_ON(changeset.bytes_changed)) {
2738 ULIST_ITER_INIT(&iter);
2739 while ((unode = ulist_next(changeset.range_changed, &iter))) {
2740 btrfs_warn(BTRFS_I(inode)->root->fs_info,
2741 "leaking qgroup reserved space, ino: %lu, start: %llu, end: %llu",
2742 inode->i_ino, unode->val, unode->aux);
2743 }
2744 qgroup_free(BTRFS_I(inode)->root, changeset.bytes_changed);
2745 }
2746 ulist_free(changeset.range_changed);
2747}