blob: 8ec103deb3619f635ecbd6233282f0465c0b6273 [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
Qu Wenruof59c0342017-12-12 15:34:24 +080050/*
51 * Helpers to access qgroup reservation
52 *
53 * Callers should ensure the lock context and type are valid
54 */
55
56static u64 qgroup_rsv_total(const struct btrfs_qgroup *qgroup)
57{
58 u64 ret = 0;
59 int i;
60
61 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
62 ret += qgroup->rsv.values[i];
63
64 return ret;
65}
66
67#ifdef CONFIG_BTRFS_DEBUG
68static const char *qgroup_rsv_type_str(enum btrfs_qgroup_rsv_type type)
69{
70 if (type == BTRFS_QGROUP_RSV_DATA)
71 return "data";
72 if (type == BTRFS_QGROUP_RSV_META)
73 return "meta";
74 return NULL;
75}
76#endif
77
Qu Wenruo64ee4e72017-12-12 15:34:27 +080078static void qgroup_rsv_add(struct btrfs_fs_info *fs_info,
79 struct btrfs_qgroup *qgroup, u64 num_bytes,
Qu Wenruof59c0342017-12-12 15:34:24 +080080 enum btrfs_qgroup_rsv_type type)
81{
Qu Wenruo64ee4e72017-12-12 15:34:27 +080082 trace_qgroup_update_reserve(fs_info, qgroup, num_bytes, type);
Qu Wenruof59c0342017-12-12 15:34:24 +080083 qgroup->rsv.values[type] += num_bytes;
84}
85
Qu Wenruo64ee4e72017-12-12 15:34:27 +080086static void qgroup_rsv_release(struct btrfs_fs_info *fs_info,
87 struct btrfs_qgroup *qgroup, u64 num_bytes,
Qu Wenruof59c0342017-12-12 15:34:24 +080088 enum btrfs_qgroup_rsv_type type)
89{
Qu Wenruo64ee4e72017-12-12 15:34:27 +080090 trace_qgroup_update_reserve(fs_info, qgroup, -(s64)num_bytes, type);
Qu Wenruof59c0342017-12-12 15:34:24 +080091 if (qgroup->rsv.values[type] >= num_bytes) {
92 qgroup->rsv.values[type] -= num_bytes;
93 return;
94 }
95#ifdef CONFIG_BTRFS_DEBUG
96 WARN_RATELIMIT(1,
97 "qgroup %llu %s reserved space underflow, have %llu to free %llu",
98 qgroup->qgroupid, qgroup_rsv_type_str(type),
99 qgroup->rsv.values[type], num_bytes);
100#endif
101 qgroup->rsv.values[type] = 0;
102}
103
Qu Wenruo64ee4e72017-12-12 15:34:27 +0800104static void qgroup_rsv_add_by_qgroup(struct btrfs_fs_info *fs_info,
105 struct btrfs_qgroup *dest,
106 struct btrfs_qgroup *src)
Qu Wenruof59c0342017-12-12 15:34:24 +0800107{
108 int i;
109
110 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
Qu Wenruo64ee4e72017-12-12 15:34:27 +0800111 qgroup_rsv_add(fs_info, dest, src->rsv.values[i], i);
Qu Wenruof59c0342017-12-12 15:34:24 +0800112}
113
Qu Wenruo64ee4e72017-12-12 15:34:27 +0800114static void qgroup_rsv_release_by_qgroup(struct btrfs_fs_info *fs_info,
115 struct btrfs_qgroup *dest,
Qu Wenruof59c0342017-12-12 15:34:24 +0800116 struct btrfs_qgroup *src)
117{
118 int i;
119
120 for (i = 0; i < BTRFS_QGROUP_RSV_LAST; i++)
Qu Wenruo64ee4e72017-12-12 15:34:27 +0800121 qgroup_rsv_release(fs_info, dest, src->rsv.values[i], i);
Qu Wenruof59c0342017-12-12 15:34:24 +0800122}
123
Qu Wenruo9c542132015-03-12 16:10:13 +0800124static void btrfs_qgroup_update_old_refcnt(struct btrfs_qgroup *qg, u64 seq,
125 int mod)
126{
127 if (qg->old_refcnt < seq)
128 qg->old_refcnt = seq;
129 qg->old_refcnt += mod;
130}
131
132static void btrfs_qgroup_update_new_refcnt(struct btrfs_qgroup *qg, u64 seq,
133 int mod)
134{
135 if (qg->new_refcnt < seq)
136 qg->new_refcnt = seq;
137 qg->new_refcnt += mod;
138}
139
140static inline u64 btrfs_qgroup_get_old_refcnt(struct btrfs_qgroup *qg, u64 seq)
141{
142 if (qg->old_refcnt < seq)
143 return 0;
144 return qg->old_refcnt - seq;
145}
146
147static inline u64 btrfs_qgroup_get_new_refcnt(struct btrfs_qgroup *qg, u64 seq)
148{
149 if (qg->new_refcnt < seq)
150 return 0;
151 return qg->new_refcnt - seq;
152}
153
Arne Jansenbed92ea2012-06-28 18:03:02 +0200154/*
155 * glue structure to represent the relations between qgroups.
156 */
157struct btrfs_qgroup_list {
158 struct list_head next_group;
159 struct list_head next_member;
160 struct btrfs_qgroup *group;
161 struct btrfs_qgroup *member;
162};
163
David Sterbaef2fff62016-10-26 16:23:50 +0200164static inline u64 qgroup_to_aux(struct btrfs_qgroup *qg)
165{
166 return (u64)(uintptr_t)qg;
167}
168
169static inline struct btrfs_qgroup* unode_aux_to_qgroup(struct ulist_node *n)
170{
171 return (struct btrfs_qgroup *)(uintptr_t)n->aux;
172}
Josef Bacikfcebe452014-05-13 17:30:47 -0700173
Jan Schmidtb382a322013-05-28 15:47:24 +0000174static int
175qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
176 int init_flags);
177static void qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +0000178
Wang Shilong58400fc2013-04-07 10:50:17 +0000179/* must be called with qgroup_ioctl_lock held */
Arne Jansenbed92ea2012-06-28 18:03:02 +0200180static struct btrfs_qgroup *find_qgroup_rb(struct btrfs_fs_info *fs_info,
181 u64 qgroupid)
182{
183 struct rb_node *n = fs_info->qgroup_tree.rb_node;
184 struct btrfs_qgroup *qgroup;
185
186 while (n) {
187 qgroup = rb_entry(n, struct btrfs_qgroup, node);
188 if (qgroup->qgroupid < qgroupid)
189 n = n->rb_left;
190 else if (qgroup->qgroupid > qgroupid)
191 n = n->rb_right;
192 else
193 return qgroup;
194 }
195 return NULL;
196}
197
198/* must be called with qgroup_lock held */
199static struct btrfs_qgroup *add_qgroup_rb(struct btrfs_fs_info *fs_info,
200 u64 qgroupid)
201{
202 struct rb_node **p = &fs_info->qgroup_tree.rb_node;
203 struct rb_node *parent = NULL;
204 struct btrfs_qgroup *qgroup;
205
206 while (*p) {
207 parent = *p;
208 qgroup = rb_entry(parent, struct btrfs_qgroup, node);
209
210 if (qgroup->qgroupid < qgroupid)
211 p = &(*p)->rb_left;
212 else if (qgroup->qgroupid > qgroupid)
213 p = &(*p)->rb_right;
214 else
215 return qgroup;
216 }
217
218 qgroup = kzalloc(sizeof(*qgroup), GFP_ATOMIC);
219 if (!qgroup)
220 return ERR_PTR(-ENOMEM);
221
222 qgroup->qgroupid = qgroupid;
223 INIT_LIST_HEAD(&qgroup->groups);
224 INIT_LIST_HEAD(&qgroup->members);
225 INIT_LIST_HEAD(&qgroup->dirty);
226
227 rb_link_node(&qgroup->node, parent, p);
228 rb_insert_color(&qgroup->node, &fs_info->qgroup_tree);
229
230 return qgroup;
231}
232
Wang Shilong4082bd32013-08-14 09:13:36 +0800233static void __del_qgroup_rb(struct btrfs_qgroup *qgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200234{
Arne Jansenbed92ea2012-06-28 18:03:02 +0200235 struct btrfs_qgroup_list *list;
236
Arne Jansenbed92ea2012-06-28 18:03:02 +0200237 list_del(&qgroup->dirty);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200238 while (!list_empty(&qgroup->groups)) {
239 list = list_first_entry(&qgroup->groups,
240 struct btrfs_qgroup_list, next_group);
241 list_del(&list->next_group);
242 list_del(&list->next_member);
243 kfree(list);
244 }
245
246 while (!list_empty(&qgroup->members)) {
247 list = list_first_entry(&qgroup->members,
248 struct btrfs_qgroup_list, next_member);
249 list_del(&list->next_group);
250 list_del(&list->next_member);
251 kfree(list);
252 }
253 kfree(qgroup);
Wang Shilong4082bd32013-08-14 09:13:36 +0800254}
Arne Jansenbed92ea2012-06-28 18:03:02 +0200255
Wang Shilong4082bd32013-08-14 09:13:36 +0800256/* must be called with qgroup_lock held */
257static int del_qgroup_rb(struct btrfs_fs_info *fs_info, u64 qgroupid)
258{
259 struct btrfs_qgroup *qgroup = find_qgroup_rb(fs_info, qgroupid);
260
261 if (!qgroup)
262 return -ENOENT;
263
264 rb_erase(&qgroup->node, &fs_info->qgroup_tree);
265 __del_qgroup_rb(qgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200266 return 0;
267}
268
269/* must be called with qgroup_lock held */
270static int add_relation_rb(struct btrfs_fs_info *fs_info,
271 u64 memberid, u64 parentid)
272{
273 struct btrfs_qgroup *member;
274 struct btrfs_qgroup *parent;
275 struct btrfs_qgroup_list *list;
276
277 member = find_qgroup_rb(fs_info, memberid);
278 parent = find_qgroup_rb(fs_info, parentid);
279 if (!member || !parent)
280 return -ENOENT;
281
282 list = kzalloc(sizeof(*list), GFP_ATOMIC);
283 if (!list)
284 return -ENOMEM;
285
286 list->group = parent;
287 list->member = member;
288 list_add_tail(&list->next_group, &member->groups);
289 list_add_tail(&list->next_member, &parent->members);
290
291 return 0;
292}
293
294/* must be called with qgroup_lock held */
295static int del_relation_rb(struct btrfs_fs_info *fs_info,
296 u64 memberid, u64 parentid)
297{
298 struct btrfs_qgroup *member;
299 struct btrfs_qgroup *parent;
300 struct btrfs_qgroup_list *list;
301
302 member = find_qgroup_rb(fs_info, memberid);
303 parent = find_qgroup_rb(fs_info, parentid);
304 if (!member || !parent)
305 return -ENOENT;
306
307 list_for_each_entry(list, &member->groups, next_group) {
308 if (list->group == parent) {
309 list_del(&list->next_group);
310 list_del(&list->next_member);
311 kfree(list);
312 return 0;
313 }
314 }
315 return -ENOENT;
316}
317
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400318#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
319int btrfs_verify_qgroup_counts(struct btrfs_fs_info *fs_info, u64 qgroupid,
320 u64 rfer, u64 excl)
321{
322 struct btrfs_qgroup *qgroup;
323
324 qgroup = find_qgroup_rb(fs_info, qgroupid);
325 if (!qgroup)
326 return -EINVAL;
327 if (qgroup->rfer != rfer || qgroup->excl != excl)
328 return -EINVAL;
329 return 0;
330}
331#endif
332
Arne Jansenbed92ea2012-06-28 18:03:02 +0200333/*
334 * The full config is read in one go, only called from open_ctree()
335 * It doesn't use any locking, as at this point we're still single-threaded
336 */
337int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info)
338{
339 struct btrfs_key key;
340 struct btrfs_key found_key;
341 struct btrfs_root *quota_root = fs_info->quota_root;
342 struct btrfs_path *path = NULL;
343 struct extent_buffer *l;
344 int slot;
345 int ret = 0;
346 u64 flags = 0;
Jan Schmidtb382a322013-05-28 15:47:24 +0000347 u64 rescan_progress = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200348
Josef Bacikafcdd122016-09-02 15:40:02 -0400349 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Arne Jansenbed92ea2012-06-28 18:03:02 +0200350 return 0;
351
David Sterba323b88f2017-02-13 12:10:20 +0100352 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
Wang Shilong1e8f9152013-05-06 11:03:27 +0000353 if (!fs_info->qgroup_ulist) {
354 ret = -ENOMEM;
355 goto out;
356 }
357
Arne Jansenbed92ea2012-06-28 18:03:02 +0200358 path = btrfs_alloc_path();
359 if (!path) {
360 ret = -ENOMEM;
361 goto out;
362 }
363
364 /* default this to quota off, in case no status key is found */
365 fs_info->qgroup_flags = 0;
366
367 /*
368 * pass 1: read status, all qgroup infos and limits
369 */
370 key.objectid = 0;
371 key.type = 0;
372 key.offset = 0;
373 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 1);
374 if (ret)
375 goto out;
376
377 while (1) {
378 struct btrfs_qgroup *qgroup;
379
380 slot = path->slots[0];
381 l = path->nodes[0];
382 btrfs_item_key_to_cpu(l, &found_key, slot);
383
384 if (found_key.type == BTRFS_QGROUP_STATUS_KEY) {
385 struct btrfs_qgroup_status_item *ptr;
386
387 ptr = btrfs_item_ptr(l, slot,
388 struct btrfs_qgroup_status_item);
389
390 if (btrfs_qgroup_status_version(l, ptr) !=
391 BTRFS_QGROUP_STATUS_VERSION) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500392 btrfs_err(fs_info,
393 "old qgroup version, quota disabled");
Arne Jansenbed92ea2012-06-28 18:03:02 +0200394 goto out;
395 }
396 if (btrfs_qgroup_status_generation(l, ptr) !=
397 fs_info->generation) {
398 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Frank Holtonefe120a2013-12-20 11:37:06 -0500399 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400400 "qgroup generation mismatch, marked as inconsistent");
Arne Jansenbed92ea2012-06-28 18:03:02 +0200401 }
402 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l,
403 ptr);
Jan Schmidtb382a322013-05-28 15:47:24 +0000404 rescan_progress = btrfs_qgroup_status_rescan(l, ptr);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200405 goto next1;
406 }
407
408 if (found_key.type != BTRFS_QGROUP_INFO_KEY &&
409 found_key.type != BTRFS_QGROUP_LIMIT_KEY)
410 goto next1;
411
412 qgroup = find_qgroup_rb(fs_info, found_key.offset);
413 if ((qgroup && found_key.type == BTRFS_QGROUP_INFO_KEY) ||
414 (!qgroup && found_key.type == BTRFS_QGROUP_LIMIT_KEY)) {
Geert Uytterhoevend41e36a2015-07-06 15:38:11 +0200415 btrfs_err(fs_info, "inconsistent qgroup config");
Arne Jansenbed92ea2012-06-28 18:03:02 +0200416 flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
417 }
418 if (!qgroup) {
419 qgroup = add_qgroup_rb(fs_info, found_key.offset);
420 if (IS_ERR(qgroup)) {
421 ret = PTR_ERR(qgroup);
422 goto out;
423 }
424 }
425 switch (found_key.type) {
426 case BTRFS_QGROUP_INFO_KEY: {
427 struct btrfs_qgroup_info_item *ptr;
428
429 ptr = btrfs_item_ptr(l, slot,
430 struct btrfs_qgroup_info_item);
431 qgroup->rfer = btrfs_qgroup_info_rfer(l, ptr);
432 qgroup->rfer_cmpr = btrfs_qgroup_info_rfer_cmpr(l, ptr);
433 qgroup->excl = btrfs_qgroup_info_excl(l, ptr);
434 qgroup->excl_cmpr = btrfs_qgroup_info_excl_cmpr(l, ptr);
435 /* generation currently unused */
436 break;
437 }
438 case BTRFS_QGROUP_LIMIT_KEY: {
439 struct btrfs_qgroup_limit_item *ptr;
440
441 ptr = btrfs_item_ptr(l, slot,
442 struct btrfs_qgroup_limit_item);
443 qgroup->lim_flags = btrfs_qgroup_limit_flags(l, ptr);
444 qgroup->max_rfer = btrfs_qgroup_limit_max_rfer(l, ptr);
445 qgroup->max_excl = btrfs_qgroup_limit_max_excl(l, ptr);
446 qgroup->rsv_rfer = btrfs_qgroup_limit_rsv_rfer(l, ptr);
447 qgroup->rsv_excl = btrfs_qgroup_limit_rsv_excl(l, ptr);
448 break;
449 }
450 }
451next1:
452 ret = btrfs_next_item(quota_root, path);
453 if (ret < 0)
454 goto out;
455 if (ret)
456 break;
457 }
458 btrfs_release_path(path);
459
460 /*
461 * pass 2: read all qgroup relations
462 */
463 key.objectid = 0;
464 key.type = BTRFS_QGROUP_RELATION_KEY;
465 key.offset = 0;
466 ret = btrfs_search_slot_for_read(quota_root, &key, path, 1, 0);
467 if (ret)
468 goto out;
469 while (1) {
470 slot = path->slots[0];
471 l = path->nodes[0];
472 btrfs_item_key_to_cpu(l, &found_key, slot);
473
474 if (found_key.type != BTRFS_QGROUP_RELATION_KEY)
475 goto next2;
476
477 if (found_key.objectid > found_key.offset) {
478 /* parent <- member, not needed to build config */
479 /* FIXME should we omit the key completely? */
480 goto next2;
481 }
482
483 ret = add_relation_rb(fs_info, found_key.objectid,
484 found_key.offset);
Arne Jansenff248582013-01-17 01:22:08 -0700485 if (ret == -ENOENT) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500486 btrfs_warn(fs_info,
487 "orphan qgroup relation 0x%llx->0x%llx",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200488 found_key.objectid, found_key.offset);
Arne Jansenff248582013-01-17 01:22:08 -0700489 ret = 0; /* ignore the error */
490 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200491 if (ret)
492 goto out;
493next2:
494 ret = btrfs_next_item(quota_root, path);
495 if (ret < 0)
496 goto out;
497 if (ret)
498 break;
499 }
500out:
501 fs_info->qgroup_flags |= flags;
Josef Bacikafcdd122016-09-02 15:40:02 -0400502 if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
503 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
504 else if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN &&
505 ret >= 0)
Jan Schmidtb382a322013-05-28 15:47:24 +0000506 ret = qgroup_rescan_init(fs_info, rescan_progress, 0);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200507 btrfs_free_path(path);
508
Jan Schmidteb1716a2013-05-28 15:47:23 +0000509 if (ret < 0) {
Wang Shilong1e8f9152013-05-06 11:03:27 +0000510 ulist_free(fs_info->qgroup_ulist);
Jan Schmidteb1716a2013-05-28 15:47:23 +0000511 fs_info->qgroup_ulist = NULL;
Jan Schmidtb382a322013-05-28 15:47:24 +0000512 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
Jan Schmidteb1716a2013-05-28 15:47:23 +0000513 }
Wang Shilong1e8f9152013-05-06 11:03:27 +0000514
Arne Jansenbed92ea2012-06-28 18:03:02 +0200515 return ret < 0 ? ret : 0;
516}
517
518/*
Wang Shilonge685da12013-08-14 09:13:37 +0800519 * This is called from close_ctree() or open_ctree() or btrfs_quota_disable(),
520 * first two are in single-threaded paths.And for the third one, we have set
521 * quota_root to be null with qgroup_lock held before, so it is safe to clean
522 * up the in-memory structures without qgroup_lock held.
Arne Jansenbed92ea2012-06-28 18:03:02 +0200523 */
524void btrfs_free_qgroup_config(struct btrfs_fs_info *fs_info)
525{
526 struct rb_node *n;
527 struct btrfs_qgroup *qgroup;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200528
529 while ((n = rb_first(&fs_info->qgroup_tree))) {
530 qgroup = rb_entry(n, struct btrfs_qgroup, node);
531 rb_erase(n, &fs_info->qgroup_tree);
Wang Shilong4082bd32013-08-14 09:13:36 +0800532 __del_qgroup_rb(qgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200533 }
Wang Shilong1e7bac12013-07-13 21:02:54 +0800534 /*
535 * we call btrfs_free_qgroup_config() when umounting
Nicholas D Steeves01327612016-05-19 21:18:45 -0400536 * filesystem and disabling quota, so we set qgroup_ulist
Wang Shilong1e7bac12013-07-13 21:02:54 +0800537 * to be null here to avoid double free.
538 */
Wang Shilong1e8f9152013-05-06 11:03:27 +0000539 ulist_free(fs_info->qgroup_ulist);
Wang Shilong1e7bac12013-07-13 21:02:54 +0800540 fs_info->qgroup_ulist = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200541}
542
543static int add_qgroup_relation_item(struct btrfs_trans_handle *trans,
544 struct btrfs_root *quota_root,
545 u64 src, u64 dst)
546{
547 int ret;
548 struct btrfs_path *path;
549 struct btrfs_key key;
550
551 path = btrfs_alloc_path();
552 if (!path)
553 return -ENOMEM;
554
555 key.objectid = src;
556 key.type = BTRFS_QGROUP_RELATION_KEY;
557 key.offset = dst;
558
559 ret = btrfs_insert_empty_item(trans, quota_root, path, &key, 0);
560
561 btrfs_mark_buffer_dirty(path->nodes[0]);
562
563 btrfs_free_path(path);
564 return ret;
565}
566
567static int del_qgroup_relation_item(struct btrfs_trans_handle *trans,
568 struct btrfs_root *quota_root,
569 u64 src, u64 dst)
570{
571 int ret;
572 struct btrfs_path *path;
573 struct btrfs_key key;
574
575 path = btrfs_alloc_path();
576 if (!path)
577 return -ENOMEM;
578
579 key.objectid = src;
580 key.type = BTRFS_QGROUP_RELATION_KEY;
581 key.offset = dst;
582
583 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
584 if (ret < 0)
585 goto out;
586
587 if (ret > 0) {
588 ret = -ENOENT;
589 goto out;
590 }
591
592 ret = btrfs_del_item(trans, quota_root, path);
593out:
594 btrfs_free_path(path);
595 return ret;
596}
597
598static int add_qgroup_item(struct btrfs_trans_handle *trans,
599 struct btrfs_root *quota_root, u64 qgroupid)
600{
601 int ret;
602 struct btrfs_path *path;
603 struct btrfs_qgroup_info_item *qgroup_info;
604 struct btrfs_qgroup_limit_item *qgroup_limit;
605 struct extent_buffer *leaf;
606 struct btrfs_key key;
607
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -0400608 if (btrfs_is_testing(quota_root->fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400609 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +0200610
Arne Jansenbed92ea2012-06-28 18:03:02 +0200611 path = btrfs_alloc_path();
612 if (!path)
613 return -ENOMEM;
614
615 key.objectid = 0;
616 key.type = BTRFS_QGROUP_INFO_KEY;
617 key.offset = qgroupid;
618
Mark Fasheh0b4699d2014-08-18 14:01:17 -0700619 /*
620 * Avoid a transaction abort by catching -EEXIST here. In that
621 * case, we proceed by re-initializing the existing structure
622 * on disk.
623 */
624
Arne Jansenbed92ea2012-06-28 18:03:02 +0200625 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
626 sizeof(*qgroup_info));
Mark Fasheh0b4699d2014-08-18 14:01:17 -0700627 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200628 goto out;
629
630 leaf = path->nodes[0];
631 qgroup_info = btrfs_item_ptr(leaf, path->slots[0],
632 struct btrfs_qgroup_info_item);
633 btrfs_set_qgroup_info_generation(leaf, qgroup_info, trans->transid);
634 btrfs_set_qgroup_info_rfer(leaf, qgroup_info, 0);
635 btrfs_set_qgroup_info_rfer_cmpr(leaf, qgroup_info, 0);
636 btrfs_set_qgroup_info_excl(leaf, qgroup_info, 0);
637 btrfs_set_qgroup_info_excl_cmpr(leaf, qgroup_info, 0);
638
639 btrfs_mark_buffer_dirty(leaf);
640
641 btrfs_release_path(path);
642
643 key.type = BTRFS_QGROUP_LIMIT_KEY;
644 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
645 sizeof(*qgroup_limit));
Mark Fasheh0b4699d2014-08-18 14:01:17 -0700646 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200647 goto out;
648
649 leaf = path->nodes[0];
650 qgroup_limit = btrfs_item_ptr(leaf, path->slots[0],
651 struct btrfs_qgroup_limit_item);
652 btrfs_set_qgroup_limit_flags(leaf, qgroup_limit, 0);
653 btrfs_set_qgroup_limit_max_rfer(leaf, qgroup_limit, 0);
654 btrfs_set_qgroup_limit_max_excl(leaf, qgroup_limit, 0);
655 btrfs_set_qgroup_limit_rsv_rfer(leaf, qgroup_limit, 0);
656 btrfs_set_qgroup_limit_rsv_excl(leaf, qgroup_limit, 0);
657
658 btrfs_mark_buffer_dirty(leaf);
659
660 ret = 0;
661out:
662 btrfs_free_path(path);
663 return ret;
664}
665
666static int del_qgroup_item(struct btrfs_trans_handle *trans,
667 struct btrfs_root *quota_root, u64 qgroupid)
668{
669 int ret;
670 struct btrfs_path *path;
671 struct btrfs_key key;
672
673 path = btrfs_alloc_path();
674 if (!path)
675 return -ENOMEM;
676
677 key.objectid = 0;
678 key.type = BTRFS_QGROUP_INFO_KEY;
679 key.offset = qgroupid;
680 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
681 if (ret < 0)
682 goto out;
683
684 if (ret > 0) {
685 ret = -ENOENT;
686 goto out;
687 }
688
689 ret = btrfs_del_item(trans, quota_root, path);
690 if (ret)
691 goto out;
692
693 btrfs_release_path(path);
694
695 key.type = BTRFS_QGROUP_LIMIT_KEY;
696 ret = btrfs_search_slot(trans, quota_root, &key, path, -1, 1);
697 if (ret < 0)
698 goto out;
699
700 if (ret > 0) {
701 ret = -ENOENT;
702 goto out;
703 }
704
705 ret = btrfs_del_item(trans, quota_root, path);
706
707out:
708 btrfs_free_path(path);
709 return ret;
710}
711
712static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,
Dongsheng Yang1510e712014-11-20 21:01:41 -0500713 struct btrfs_root *root,
714 struct btrfs_qgroup *qgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200715{
716 struct btrfs_path *path;
717 struct btrfs_key key;
718 struct extent_buffer *l;
719 struct btrfs_qgroup_limit_item *qgroup_limit;
720 int ret;
721 int slot;
722
723 key.objectid = 0;
724 key.type = BTRFS_QGROUP_LIMIT_KEY;
Dongsheng Yang1510e712014-11-20 21:01:41 -0500725 key.offset = qgroup->qgroupid;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200726
727 path = btrfs_alloc_path();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000728 if (!path)
729 return -ENOMEM;
730
Arne Jansenbed92ea2012-06-28 18:03:02 +0200731 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
732 if (ret > 0)
733 ret = -ENOENT;
734
735 if (ret)
736 goto out;
737
738 l = path->nodes[0];
739 slot = path->slots[0];
Valentina Giustia3df41e2013-11-04 22:34:29 +0100740 qgroup_limit = btrfs_item_ptr(l, slot, struct btrfs_qgroup_limit_item);
Dongsheng Yang1510e712014-11-20 21:01:41 -0500741 btrfs_set_qgroup_limit_flags(l, qgroup_limit, qgroup->lim_flags);
742 btrfs_set_qgroup_limit_max_rfer(l, qgroup_limit, qgroup->max_rfer);
743 btrfs_set_qgroup_limit_max_excl(l, qgroup_limit, qgroup->max_excl);
744 btrfs_set_qgroup_limit_rsv_rfer(l, qgroup_limit, qgroup->rsv_rfer);
745 btrfs_set_qgroup_limit_rsv_excl(l, qgroup_limit, qgroup->rsv_excl);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200746
747 btrfs_mark_buffer_dirty(l);
748
749out:
750 btrfs_free_path(path);
751 return ret;
752}
753
754static int update_qgroup_info_item(struct btrfs_trans_handle *trans,
755 struct btrfs_root *root,
756 struct btrfs_qgroup *qgroup)
757{
758 struct btrfs_path *path;
759 struct btrfs_key key;
760 struct extent_buffer *l;
761 struct btrfs_qgroup_info_item *qgroup_info;
762 int ret;
763 int slot;
764
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -0400765 if (btrfs_is_testing(root->fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400766 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +0200767
Arne Jansenbed92ea2012-06-28 18:03:02 +0200768 key.objectid = 0;
769 key.type = BTRFS_QGROUP_INFO_KEY;
770 key.offset = qgroup->qgroupid;
771
772 path = btrfs_alloc_path();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000773 if (!path)
774 return -ENOMEM;
775
Arne Jansenbed92ea2012-06-28 18:03:02 +0200776 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
777 if (ret > 0)
778 ret = -ENOENT;
779
780 if (ret)
781 goto out;
782
783 l = path->nodes[0];
784 slot = path->slots[0];
Valentina Giustia3df41e2013-11-04 22:34:29 +0100785 qgroup_info = btrfs_item_ptr(l, slot, struct btrfs_qgroup_info_item);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200786 btrfs_set_qgroup_info_generation(l, qgroup_info, trans->transid);
787 btrfs_set_qgroup_info_rfer(l, qgroup_info, qgroup->rfer);
788 btrfs_set_qgroup_info_rfer_cmpr(l, qgroup_info, qgroup->rfer_cmpr);
789 btrfs_set_qgroup_info_excl(l, qgroup_info, qgroup->excl);
790 btrfs_set_qgroup_info_excl_cmpr(l, qgroup_info, qgroup->excl_cmpr);
791
792 btrfs_mark_buffer_dirty(l);
793
794out:
795 btrfs_free_path(path);
796 return ret;
797}
798
799static int update_qgroup_status_item(struct btrfs_trans_handle *trans,
800 struct btrfs_fs_info *fs_info,
801 struct btrfs_root *root)
802{
803 struct btrfs_path *path;
804 struct btrfs_key key;
805 struct extent_buffer *l;
806 struct btrfs_qgroup_status_item *ptr;
807 int ret;
808 int slot;
809
810 key.objectid = 0;
811 key.type = BTRFS_QGROUP_STATUS_KEY;
812 key.offset = 0;
813
814 path = btrfs_alloc_path();
Wang Shilong84cbe2f2013-02-27 11:20:56 +0000815 if (!path)
816 return -ENOMEM;
817
Arne Jansenbed92ea2012-06-28 18:03:02 +0200818 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
819 if (ret > 0)
820 ret = -ENOENT;
821
822 if (ret)
823 goto out;
824
825 l = path->nodes[0];
826 slot = path->slots[0];
827 ptr = btrfs_item_ptr(l, slot, struct btrfs_qgroup_status_item);
828 btrfs_set_qgroup_status_flags(l, ptr, fs_info->qgroup_flags);
829 btrfs_set_qgroup_status_generation(l, ptr, trans->transid);
Jan Schmidt2f232032013-04-25 16:04:51 +0000830 btrfs_set_qgroup_status_rescan(l, ptr,
831 fs_info->qgroup_rescan_progress.objectid);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200832
833 btrfs_mark_buffer_dirty(l);
834
835out:
836 btrfs_free_path(path);
837 return ret;
838}
839
840/*
841 * called with qgroup_lock held
842 */
843static int btrfs_clean_quota_tree(struct btrfs_trans_handle *trans,
844 struct btrfs_root *root)
845{
846 struct btrfs_path *path;
847 struct btrfs_key key;
Wang Shilong06b3a862013-02-27 11:16:57 +0000848 struct extent_buffer *leaf = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200849 int ret;
Wang Shilong06b3a862013-02-27 11:16:57 +0000850 int nr = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200851
Arne Jansenbed92ea2012-06-28 18:03:02 +0200852 path = btrfs_alloc_path();
853 if (!path)
854 return -ENOMEM;
855
Wang Shilong06b3a862013-02-27 11:16:57 +0000856 path->leave_spinning = 1;
857
858 key.objectid = 0;
859 key.offset = 0;
860 key.type = 0;
861
Arne Jansenbed92ea2012-06-28 18:03:02 +0200862 while (1) {
Arne Jansenbed92ea2012-06-28 18:03:02 +0200863 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Wang Shilong06b3a862013-02-27 11:16:57 +0000864 if (ret < 0)
865 goto out;
866 leaf = path->nodes[0];
867 nr = btrfs_header_nritems(leaf);
868 if (!nr)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200869 break;
Wang Shilong06b3a862013-02-27 11:16:57 +0000870 /*
871 * delete the leaf one by one
872 * since the whole tree is going
873 * to be deleted.
874 */
875 path->slots[0] = 0;
876 ret = btrfs_del_items(trans, root, path, 0, nr);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200877 if (ret)
878 goto out;
Wang Shilong06b3a862013-02-27 11:16:57 +0000879
Arne Jansenbed92ea2012-06-28 18:03:02 +0200880 btrfs_release_path(path);
881 }
882 ret = 0;
883out:
Arne Jansenbed92ea2012-06-28 18:03:02 +0200884 btrfs_free_path(path);
885 return ret;
886}
887
888int btrfs_quota_enable(struct btrfs_trans_handle *trans,
889 struct btrfs_fs_info *fs_info)
890{
891 struct btrfs_root *quota_root;
Wang Shilong7708f022013-04-07 10:24:57 +0000892 struct btrfs_root *tree_root = fs_info->tree_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200893 struct btrfs_path *path = NULL;
894 struct btrfs_qgroup_status_item *ptr;
895 struct extent_buffer *leaf;
896 struct btrfs_key key;
Wang Shilong7708f022013-04-07 10:24:57 +0000897 struct btrfs_key found_key;
898 struct btrfs_qgroup *qgroup = NULL;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200899 int ret = 0;
Wang Shilong7708f022013-04-07 10:24:57 +0000900 int slot;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200901
Wang Shilongf2f6ed32013-04-07 10:50:16 +0000902 mutex_lock(&fs_info->qgroup_ioctl_lock);
Nikolay Borisov5d235152018-01-31 10:52:04 +0200903 if (fs_info->quota_root)
Arne Jansenbed92ea2012-06-28 18:03:02 +0200904 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200905
David Sterba52bf8e72017-02-13 11:03:44 +0100906 fs_info->qgroup_ulist = ulist_alloc(GFP_KERNEL);
Wang Shilong1e8f9152013-05-06 11:03:27 +0000907 if (!fs_info->qgroup_ulist) {
908 ret = -ENOMEM;
909 goto out;
910 }
911
Arne Jansenbed92ea2012-06-28 18:03:02 +0200912 /*
913 * initially create the quota tree
914 */
915 quota_root = btrfs_create_tree(trans, fs_info,
916 BTRFS_QUOTA_TREE_OBJECTID);
917 if (IS_ERR(quota_root)) {
918 ret = PTR_ERR(quota_root);
919 goto out;
920 }
921
922 path = btrfs_alloc_path();
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000923 if (!path) {
924 ret = -ENOMEM;
925 goto out_free_root;
926 }
Arne Jansenbed92ea2012-06-28 18:03:02 +0200927
928 key.objectid = 0;
929 key.type = BTRFS_QGROUP_STATUS_KEY;
930 key.offset = 0;
931
932 ret = btrfs_insert_empty_item(trans, quota_root, path, &key,
933 sizeof(*ptr));
934 if (ret)
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +0000935 goto out_free_path;
Arne Jansenbed92ea2012-06-28 18:03:02 +0200936
937 leaf = path->nodes[0];
938 ptr = btrfs_item_ptr(leaf, path->slots[0],
939 struct btrfs_qgroup_status_item);
940 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
941 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
942 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON |
943 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
944 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags);
Jan Schmidt2f232032013-04-25 16:04:51 +0000945 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200946
947 btrfs_mark_buffer_dirty(leaf);
948
Wang Shilong7708f022013-04-07 10:24:57 +0000949 key.objectid = 0;
950 key.type = BTRFS_ROOT_REF_KEY;
951 key.offset = 0;
952
953 btrfs_release_path(path);
954 ret = btrfs_search_slot_for_read(tree_root, &key, path, 1, 0);
955 if (ret > 0)
956 goto out_add_root;
957 if (ret < 0)
958 goto out_free_path;
959
960
961 while (1) {
962 slot = path->slots[0];
963 leaf = path->nodes[0];
964 btrfs_item_key_to_cpu(leaf, &found_key, slot);
965
966 if (found_key.type == BTRFS_ROOT_REF_KEY) {
967 ret = add_qgroup_item(trans, quota_root,
968 found_key.offset);
969 if (ret)
970 goto out_free_path;
971
Wang Shilong7708f022013-04-07 10:24:57 +0000972 qgroup = add_qgroup_rb(fs_info, found_key.offset);
973 if (IS_ERR(qgroup)) {
Wang Shilong7708f022013-04-07 10:24:57 +0000974 ret = PTR_ERR(qgroup);
975 goto out_free_path;
976 }
Wang Shilong7708f022013-04-07 10:24:57 +0000977 }
978 ret = btrfs_next_item(tree_root, path);
979 if (ret < 0)
980 goto out_free_path;
981 if (ret)
982 break;
983 }
984
985out_add_root:
986 btrfs_release_path(path);
987 ret = add_qgroup_item(trans, quota_root, BTRFS_FS_TREE_OBJECTID);
988 if (ret)
989 goto out_free_path;
990
Wang Shilong7708f022013-04-07 10:24:57 +0000991 qgroup = add_qgroup_rb(fs_info, BTRFS_FS_TREE_OBJECTID);
992 if (IS_ERR(qgroup)) {
Wang Shilong7708f022013-04-07 10:24:57 +0000993 ret = PTR_ERR(qgroup);
994 goto out_free_path;
995 }
Wang Shilong58400fc2013-04-07 10:50:17 +0000996 spin_lock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200997 fs_info->quota_root = quota_root;
Nikolay Borisov5d235152018-01-31 10:52:04 +0200998 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
Arne Jansenbed92ea2012-06-28 18:03:02 +0200999 spin_unlock(&fs_info->qgroup_lock);
Nikolay Borisov5d235152018-01-31 10:52:04 +02001000 ret = qgroup_rescan_init(fs_info, 0, 1);
1001 if (!ret) {
1002 qgroup_rescan_zero_tracking(fs_info);
1003 btrfs_queue_work(fs_info->qgroup_rescan_workers,
1004 &fs_info->qgroup_rescan_work);
1005 }
1006
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +00001007out_free_path:
Arne Jansenbed92ea2012-06-28 18:03:02 +02001008 btrfs_free_path(path);
Tsutomu Itoh5b7ff5b2012-10-16 05:44:21 +00001009out_free_root:
1010 if (ret) {
1011 free_extent_buffer(quota_root->node);
1012 free_extent_buffer(quota_root->commit_root);
1013 kfree(quota_root);
1014 }
1015out:
Jan Schmidteb1716a2013-05-28 15:47:23 +00001016 if (ret) {
Wang Shilong1e8f9152013-05-06 11:03:27 +00001017 ulist_free(fs_info->qgroup_ulist);
Jan Schmidteb1716a2013-05-28 15:47:23 +00001018 fs_info->qgroup_ulist = NULL;
1019 }
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001020 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001021 return ret;
1022}
1023
1024int btrfs_quota_disable(struct btrfs_trans_handle *trans,
1025 struct btrfs_fs_info *fs_info)
1026{
Arne Jansenbed92ea2012-06-28 18:03:02 +02001027 struct btrfs_root *quota_root;
1028 int ret = 0;
1029
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001030 mutex_lock(&fs_info->qgroup_ioctl_lock);
Wang Shilong58400fc2013-04-07 10:50:17 +00001031 if (!fs_info->quota_root)
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001032 goto out;
Josef Bacikafcdd122016-09-02 15:40:02 -04001033 clear_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04001034 btrfs_qgroup_wait_for_completion(fs_info, false);
Justin Maggard967ef512015-11-06 10:36:42 -08001035 spin_lock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001036 quota_root = fs_info->quota_root;
1037 fs_info->quota_root = NULL;
Dongsheng Yang8ea0ec92015-02-27 16:24:26 +08001038 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001039 spin_unlock(&fs_info->qgroup_lock);
1040
Wang Shilonge685da12013-08-14 09:13:37 +08001041 btrfs_free_qgroup_config(fs_info);
1042
Arne Jansenbed92ea2012-06-28 18:03:02 +02001043 ret = btrfs_clean_quota_tree(trans, quota_root);
1044 if (ret)
1045 goto out;
1046
Jeff Mahoney1cd54472017-08-17 10:25:11 -04001047 ret = btrfs_del_root(trans, fs_info, &quota_root->root_key);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001048 if (ret)
1049 goto out;
1050
1051 list_del(&quota_root->dirty_list);
1052
1053 btrfs_tree_lock(quota_root->node);
David Sterba7c302b42017-02-10 18:47:57 +01001054 clean_tree_block(fs_info, quota_root->node);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001055 btrfs_tree_unlock(quota_root->node);
1056 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
1057
1058 free_extent_buffer(quota_root->node);
1059 free_extent_buffer(quota_root->commit_root);
1060 kfree(quota_root);
1061out:
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001062 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001063 return ret;
1064}
1065
Jan Schmidt2f232032013-04-25 16:04:51 +00001066static void qgroup_dirty(struct btrfs_fs_info *fs_info,
1067 struct btrfs_qgroup *qgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001068{
Jan Schmidt2f232032013-04-25 16:04:51 +00001069 if (list_empty(&qgroup->dirty))
1070 list_add(&qgroup->dirty, &fs_info->dirty_qgroups);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001071}
1072
Qu Wenruo18dc22c2016-10-20 10:28:41 +08001073static void report_reserved_underflow(struct btrfs_fs_info *fs_info,
1074 struct btrfs_qgroup *qgroup,
1075 u64 num_bytes)
1076{
David Sterba338bd522017-04-18 17:00:12 +02001077#ifdef CONFIG_BTRFS_DEBUG
1078 WARN_ON(qgroup->reserved < num_bytes);
1079 btrfs_debug(fs_info,
Qu Wenruo18dc22c2016-10-20 10:28:41 +08001080 "qgroup %llu reserved space underflow, have: %llu, to free: %llu",
1081 qgroup->qgroupid, qgroup->reserved, num_bytes);
David Sterba338bd522017-04-18 17:00:12 +02001082#endif
Qu Wenruo18dc22c2016-10-20 10:28:41 +08001083 qgroup->reserved = 0;
1084}
Qu Wenruo429d6272017-12-12 15:34:26 +08001085
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001086/*
Qu Wenruo429d6272017-12-12 15:34:26 +08001087 * The easy accounting, we're updating qgroup relationship whose child qgroup
1088 * only has exclusive extents.
1089 *
1090 * In this case, all exclsuive extents will also be exlusive for parent, so
1091 * excl/rfer just get added/removed.
1092 *
1093 * So is qgroup reservation space, which should also be added/removed to
1094 * parent.
1095 * Or when child tries to release reservation space, parent will underflow its
1096 * reservation (for relationship adding case).
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001097 *
1098 * Caller should hold fs_info->qgroup_lock.
1099 */
1100static int __qgroup_excl_accounting(struct btrfs_fs_info *fs_info,
1101 struct ulist *tmp, u64 ref_root,
Qu Wenruo429d6272017-12-12 15:34:26 +08001102 struct btrfs_qgroup *src, int sign)
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001103{
1104 struct btrfs_qgroup *qgroup;
1105 struct btrfs_qgroup_list *glist;
1106 struct ulist_node *unode;
1107 struct ulist_iterator uiter;
Qu Wenruo429d6272017-12-12 15:34:26 +08001108 u64 num_bytes = src->excl;
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001109 int ret = 0;
1110
1111 qgroup = find_qgroup_rb(fs_info, ref_root);
1112 if (!qgroup)
1113 goto out;
1114
1115 qgroup->rfer += sign * num_bytes;
1116 qgroup->rfer_cmpr += sign * num_bytes;
1117
1118 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1119 qgroup->excl += sign * num_bytes;
1120 qgroup->excl_cmpr += sign * num_bytes;
Qu Wenruo429d6272017-12-12 15:34:26 +08001121
1122 if (sign > 0)
Qu Wenruo64ee4e72017-12-12 15:34:27 +08001123 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
Qu Wenruo429d6272017-12-12 15:34:26 +08001124 else
Qu Wenruo64ee4e72017-12-12 15:34:27 +08001125 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001126
1127 qgroup_dirty(fs_info, qgroup);
1128
1129 /* Get all of the parent groups that contain this qgroup */
1130 list_for_each_entry(glist, &qgroup->groups, next_group) {
1131 ret = ulist_add(tmp, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02001132 qgroup_to_aux(glist->group), GFP_ATOMIC);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001133 if (ret < 0)
1134 goto out;
1135 }
1136
1137 /* Iterate all of the parents and adjust their reference counts */
1138 ULIST_ITER_INIT(&uiter);
1139 while ((unode = ulist_next(tmp, &uiter))) {
David Sterbaef2fff62016-10-26 16:23:50 +02001140 qgroup = unode_aux_to_qgroup(unode);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001141 qgroup->rfer += sign * num_bytes;
1142 qgroup->rfer_cmpr += sign * num_bytes;
1143 WARN_ON(sign < 0 && qgroup->excl < num_bytes);
1144 qgroup->excl += sign * num_bytes;
Qu Wenruo429d6272017-12-12 15:34:26 +08001145 if (sign > 0)
Qu Wenruo64ee4e72017-12-12 15:34:27 +08001146 qgroup_rsv_add_by_qgroup(fs_info, qgroup, src);
Qu Wenruo429d6272017-12-12 15:34:26 +08001147 else
Qu Wenruo64ee4e72017-12-12 15:34:27 +08001148 qgroup_rsv_release_by_qgroup(fs_info, qgroup, src);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001149 qgroup->excl_cmpr += sign * num_bytes;
1150 qgroup_dirty(fs_info, qgroup);
1151
1152 /* Add any parents of the parents */
1153 list_for_each_entry(glist, &qgroup->groups, next_group) {
1154 ret = ulist_add(tmp, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02001155 qgroup_to_aux(glist->group), GFP_ATOMIC);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001156 if (ret < 0)
1157 goto out;
1158 }
1159 }
1160 ret = 0;
1161out:
1162 return ret;
1163}
1164
1165
1166/*
1167 * Quick path for updating qgroup with only excl refs.
1168 *
1169 * In that case, just update all parent will be enough.
1170 * Or we needs to do a full rescan.
1171 * Caller should also hold fs_info->qgroup_lock.
1172 *
1173 * Return 0 for quick update, return >0 for need to full rescan
1174 * and mark INCONSISTENT flag.
1175 * Return < 0 for other error.
1176 */
1177static int quick_update_accounting(struct btrfs_fs_info *fs_info,
1178 struct ulist *tmp, u64 src, u64 dst,
1179 int sign)
1180{
1181 struct btrfs_qgroup *qgroup;
1182 int ret = 1;
1183 int err = 0;
1184
1185 qgroup = find_qgroup_rb(fs_info, src);
1186 if (!qgroup)
1187 goto out;
1188 if (qgroup->excl == qgroup->rfer) {
1189 ret = 0;
1190 err = __qgroup_excl_accounting(fs_info, tmp, dst,
Qu Wenruo429d6272017-12-12 15:34:26 +08001191 qgroup, sign);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001192 if (err < 0) {
1193 ret = err;
1194 goto out;
1195 }
1196 }
1197out:
1198 if (ret)
1199 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1200 return ret;
1201}
1202
Arne Jansenbed92ea2012-06-28 18:03:02 +02001203int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
1204 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1205{
1206 struct btrfs_root *quota_root;
Wang Shilongb7fef4f2013-04-07 10:50:18 +00001207 struct btrfs_qgroup *parent;
1208 struct btrfs_qgroup *member;
Wang Shilong534e6622013-04-17 14:49:51 +00001209 struct btrfs_qgroup_list *list;
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001210 struct ulist *tmp;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001211 int ret = 0;
1212
Qu Wenruo8465ece2015-02-27 16:24:22 +08001213 /* Check the level of src and dst first */
1214 if (btrfs_qgroup_level(src) >= btrfs_qgroup_level(dst))
1215 return -EINVAL;
1216
David Sterba6602caf2017-02-13 12:41:02 +01001217 tmp = ulist_alloc(GFP_KERNEL);
Christian Engelmayerab3680d2015-05-02 17:19:55 +02001218 if (!tmp)
1219 return -ENOMEM;
1220
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001221 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001222 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001223 if (!quota_root) {
1224 ret = -EINVAL;
1225 goto out;
1226 }
Wang Shilongb7fef4f2013-04-07 10:50:18 +00001227 member = find_qgroup_rb(fs_info, src);
1228 parent = find_qgroup_rb(fs_info, dst);
1229 if (!member || !parent) {
1230 ret = -EINVAL;
1231 goto out;
1232 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001233
Wang Shilong534e6622013-04-17 14:49:51 +00001234 /* check if such qgroup relation exist firstly */
1235 list_for_each_entry(list, &member->groups, next_group) {
1236 if (list->group == parent) {
1237 ret = -EEXIST;
1238 goto out;
1239 }
1240 }
1241
Arne Jansenbed92ea2012-06-28 18:03:02 +02001242 ret = add_qgroup_relation_item(trans, quota_root, src, dst);
1243 if (ret)
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001244 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001245
1246 ret = add_qgroup_relation_item(trans, quota_root, dst, src);
1247 if (ret) {
1248 del_qgroup_relation_item(trans, quota_root, src, dst);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001249 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001250 }
1251
1252 spin_lock(&fs_info->qgroup_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001253 ret = add_relation_rb(fs_info, src, dst);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001254 if (ret < 0) {
1255 spin_unlock(&fs_info->qgroup_lock);
1256 goto out;
1257 }
1258 ret = quick_update_accounting(fs_info, tmp, src, dst, 1);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001259 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001260out:
1261 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001262 ulist_free(tmp);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001263 return ret;
1264}
1265
David Sterba025db912017-02-13 13:00:51 +01001266static int __del_qgroup_relation(struct btrfs_trans_handle *trans,
Arne Jansenbed92ea2012-06-28 18:03:02 +02001267 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1268{
1269 struct btrfs_root *quota_root;
Wang Shilong534e6622013-04-17 14:49:51 +00001270 struct btrfs_qgroup *parent;
1271 struct btrfs_qgroup *member;
1272 struct btrfs_qgroup_list *list;
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001273 struct ulist *tmp;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001274 int ret = 0;
1275 int err;
1276
David Sterba6602caf2017-02-13 12:41:02 +01001277 tmp = ulist_alloc(GFP_KERNEL);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001278 if (!tmp)
1279 return -ENOMEM;
1280
Arne Jansenbed92ea2012-06-28 18:03:02 +02001281 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001282 if (!quota_root) {
1283 ret = -EINVAL;
1284 goto out;
1285 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001286
Wang Shilong534e6622013-04-17 14:49:51 +00001287 member = find_qgroup_rb(fs_info, src);
1288 parent = find_qgroup_rb(fs_info, dst);
1289 if (!member || !parent) {
1290 ret = -EINVAL;
1291 goto out;
1292 }
1293
1294 /* check if such qgroup relation exist firstly */
1295 list_for_each_entry(list, &member->groups, next_group) {
1296 if (list->group == parent)
1297 goto exist;
1298 }
1299 ret = -ENOENT;
1300 goto out;
1301exist:
Arne Jansenbed92ea2012-06-28 18:03:02 +02001302 ret = del_qgroup_relation_item(trans, quota_root, src, dst);
1303 err = del_qgroup_relation_item(trans, quota_root, dst, src);
1304 if (err && !ret)
1305 ret = err;
1306
1307 spin_lock(&fs_info->qgroup_lock);
1308 del_relation_rb(fs_info, src, dst);
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001309 ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001310 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001311out:
Qu Wenruo9c8b35b2015-02-27 16:24:27 +08001312 ulist_free(tmp);
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001313 return ret;
1314}
1315
1316int btrfs_del_qgroup_relation(struct btrfs_trans_handle *trans,
1317 struct btrfs_fs_info *fs_info, u64 src, u64 dst)
1318{
1319 int ret = 0;
1320
1321 mutex_lock(&fs_info->qgroup_ioctl_lock);
1322 ret = __del_qgroup_relation(trans, fs_info, src, dst);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001323 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001324
Arne Jansenbed92ea2012-06-28 18:03:02 +02001325 return ret;
1326}
1327
1328int btrfs_create_qgroup(struct btrfs_trans_handle *trans,
Dongsheng Yang4087cf22015-01-18 10:59:23 -05001329 struct btrfs_fs_info *fs_info, u64 qgroupid)
Arne Jansenbed92ea2012-06-28 18:03:02 +02001330{
1331 struct btrfs_root *quota_root;
1332 struct btrfs_qgroup *qgroup;
1333 int ret = 0;
1334
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001335 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001336 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001337 if (!quota_root) {
1338 ret = -EINVAL;
1339 goto out;
1340 }
Wang Shilong534e6622013-04-17 14:49:51 +00001341 qgroup = find_qgroup_rb(fs_info, qgroupid);
1342 if (qgroup) {
1343 ret = -EEXIST;
1344 goto out;
1345 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001346
1347 ret = add_qgroup_item(trans, quota_root, qgroupid);
Wang Shilong534e6622013-04-17 14:49:51 +00001348 if (ret)
1349 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001350
1351 spin_lock(&fs_info->qgroup_lock);
1352 qgroup = add_qgroup_rb(fs_info, qgroupid);
1353 spin_unlock(&fs_info->qgroup_lock);
1354
1355 if (IS_ERR(qgroup))
1356 ret = PTR_ERR(qgroup);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001357out:
1358 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001359 return ret;
1360}
1361
1362int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
1363 struct btrfs_fs_info *fs_info, u64 qgroupid)
1364{
1365 struct btrfs_root *quota_root;
Arne Jansen2cf68702013-01-17 01:22:09 -07001366 struct btrfs_qgroup *qgroup;
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001367 struct btrfs_qgroup_list *list;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001368 int ret = 0;
1369
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001370 mutex_lock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001371 quota_root = fs_info->quota_root;
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001372 if (!quota_root) {
1373 ret = -EINVAL;
1374 goto out;
1375 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001376
Arne Jansen2cf68702013-01-17 01:22:09 -07001377 qgroup = find_qgroup_rb(fs_info, qgroupid);
Wang Shilong534e6622013-04-17 14:49:51 +00001378 if (!qgroup) {
1379 ret = -ENOENT;
1380 goto out;
1381 } else {
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001382 /* check if there are no children of this qgroup */
1383 if (!list_empty(&qgroup->members)) {
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001384 ret = -EBUSY;
1385 goto out;
Arne Jansen2cf68702013-01-17 01:22:09 -07001386 }
1387 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001388 ret = del_qgroup_item(trans, quota_root, qgroupid);
Sargun Dhillon36b96fd2017-09-17 09:02:29 +00001389 if (ret && ret != -ENOENT)
1390 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001391
Dongsheng Yangf5a6b1c2014-11-24 10:27:09 -05001392 while (!list_empty(&qgroup->groups)) {
1393 list = list_first_entry(&qgroup->groups,
1394 struct btrfs_qgroup_list, next_group);
1395 ret = __del_qgroup_relation(trans, fs_info,
1396 qgroupid,
1397 list->group->qgroupid);
1398 if (ret)
1399 goto out;
1400 }
1401
Arne Jansenbed92ea2012-06-28 18:03:02 +02001402 spin_lock(&fs_info->qgroup_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001403 del_qgroup_rb(fs_info, qgroupid);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001404 spin_unlock(&fs_info->qgroup_lock);
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001405out:
1406 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001407 return ret;
1408}
1409
1410int btrfs_limit_qgroup(struct btrfs_trans_handle *trans,
1411 struct btrfs_fs_info *fs_info, u64 qgroupid,
1412 struct btrfs_qgroup_limit *limit)
1413{
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001414 struct btrfs_root *quota_root;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001415 struct btrfs_qgroup *qgroup;
1416 int ret = 0;
Yang Dongshengfe759902015-06-03 14:57:32 +08001417 /* Sometimes we would want to clear the limit on this qgroup.
1418 * To meet this requirement, we treat the -1 as a special value
1419 * which tell kernel to clear the limit on this qgroup.
1420 */
1421 const u64 CLEAR_VALUE = -1;
Arne Jansenbed92ea2012-06-28 18:03:02 +02001422
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001423 mutex_lock(&fs_info->qgroup_ioctl_lock);
1424 quota_root = fs_info->quota_root;
1425 if (!quota_root) {
1426 ret = -EINVAL;
1427 goto out;
1428 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001429
Wang Shilongddb47af2013-04-07 10:50:20 +00001430 qgroup = find_qgroup_rb(fs_info, qgroupid);
1431 if (!qgroup) {
1432 ret = -ENOENT;
1433 goto out;
1434 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02001435
Wang Shilong58400fc2013-04-07 10:50:17 +00001436 spin_lock(&fs_info->qgroup_lock);
Yang Dongshengfe759902015-06-03 14:57:32 +08001437 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_RFER) {
1438 if (limit->max_rfer == CLEAR_VALUE) {
1439 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1440 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_RFER;
1441 qgroup->max_rfer = 0;
1442 } else {
1443 qgroup->max_rfer = limit->max_rfer;
1444 }
1445 }
1446 if (limit->flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) {
1447 if (limit->max_excl == CLEAR_VALUE) {
1448 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1449 limit->flags &= ~BTRFS_QGROUP_LIMIT_MAX_EXCL;
1450 qgroup->max_excl = 0;
1451 } else {
1452 qgroup->max_excl = limit->max_excl;
1453 }
1454 }
1455 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_RFER) {
1456 if (limit->rsv_rfer == CLEAR_VALUE) {
1457 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1458 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_RFER;
1459 qgroup->rsv_rfer = 0;
1460 } else {
1461 qgroup->rsv_rfer = limit->rsv_rfer;
1462 }
1463 }
1464 if (limit->flags & BTRFS_QGROUP_LIMIT_RSV_EXCL) {
1465 if (limit->rsv_excl == CLEAR_VALUE) {
1466 qgroup->lim_flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1467 limit->flags &= ~BTRFS_QGROUP_LIMIT_RSV_EXCL;
1468 qgroup->rsv_excl = 0;
1469 } else {
1470 qgroup->rsv_excl = limit->rsv_excl;
1471 }
1472 }
Dongsheng Yang03477d92015-02-06 11:06:25 -05001473 qgroup->lim_flags |= limit->flags;
1474
Arne Jansenbed92ea2012-06-28 18:03:02 +02001475 spin_unlock(&fs_info->qgroup_lock);
Dongsheng Yang1510e712014-11-20 21:01:41 -05001476
1477 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
1478 if (ret) {
1479 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1480 btrfs_info(fs_info, "unable to update quota limit for %llu",
1481 qgroupid);
1482 }
1483
Wang Shilongf2f6ed32013-04-07 10:50:16 +00001484out:
1485 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02001486 return ret;
1487}
Mark Fasheh11526512014-07-17 12:39:01 -07001488
Qu Wenruo50b3e042016-10-18 09:31:27 +08001489int btrfs_qgroup_trace_extent_nolock(struct btrfs_fs_info *fs_info,
Qu Wenruocb93b522016-08-15 10:36:50 +08001490 struct btrfs_delayed_ref_root *delayed_refs,
1491 struct btrfs_qgroup_extent_record *record)
Qu Wenruo3368d002015-04-16 14:34:17 +08001492{
1493 struct rb_node **p = &delayed_refs->dirty_extent_root.rb_node;
1494 struct rb_node *parent_node = NULL;
1495 struct btrfs_qgroup_extent_record *entry;
1496 u64 bytenr = record->bytenr;
1497
Mark Fasheh82bd1012015-11-05 14:38:00 -08001498 assert_spin_locked(&delayed_refs->lock);
Qu Wenruo50b3e042016-10-18 09:31:27 +08001499 trace_btrfs_qgroup_trace_extent(fs_info, record);
Mark Fasheh82bd1012015-11-05 14:38:00 -08001500
Qu Wenruo3368d002015-04-16 14:34:17 +08001501 while (*p) {
1502 parent_node = *p;
1503 entry = rb_entry(parent_node, struct btrfs_qgroup_extent_record,
1504 node);
1505 if (bytenr < entry->bytenr)
1506 p = &(*p)->rb_left;
1507 else if (bytenr > entry->bytenr)
1508 p = &(*p)->rb_right;
1509 else
Qu Wenruocb93b522016-08-15 10:36:50 +08001510 return 1;
Qu Wenruo3368d002015-04-16 14:34:17 +08001511 }
1512
1513 rb_link_node(&record->node, parent_node, p);
1514 rb_insert_color(&record->node, &delayed_refs->dirty_extent_root);
Qu Wenruocb93b522016-08-15 10:36:50 +08001515 return 0;
1516}
1517
Qu Wenruofb235dc2017-02-15 10:43:03 +08001518int btrfs_qgroup_trace_extent_post(struct btrfs_fs_info *fs_info,
1519 struct btrfs_qgroup_extent_record *qrecord)
1520{
1521 struct ulist *old_root;
1522 u64 bytenr = qrecord->bytenr;
1523 int ret;
1524
Zygo Blaxellc995ab32017-09-22 13:58:45 -04001525 ret = btrfs_find_all_roots(NULL, fs_info, bytenr, 0, &old_root, false);
Nikolay Borisov952bd3db2018-01-29 15:53:01 +02001526 if (ret < 0) {
1527 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1528 btrfs_warn(fs_info,
1529"error accounting new delayed refs extent (err code: %d), quota inconsistent",
1530 ret);
1531 return 0;
1532 }
Qu Wenruofb235dc2017-02-15 10:43:03 +08001533
1534 /*
1535 * Here we don't need to get the lock of
1536 * trans->transaction->delayed_refs, since inserted qrecord won't
1537 * be deleted, only qrecord->node may be modified (new qrecord insert)
1538 *
1539 * So modifying qrecord->old_roots is safe here
1540 */
1541 qrecord->old_roots = old_root;
1542 return 0;
1543}
1544
Qu Wenruo50b3e042016-10-18 09:31:27 +08001545int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans,
Qu Wenruocb93b522016-08-15 10:36:50 +08001546 struct btrfs_fs_info *fs_info, u64 bytenr, u64 num_bytes,
1547 gfp_t gfp_flag)
1548{
1549 struct btrfs_qgroup_extent_record *record;
1550 struct btrfs_delayed_ref_root *delayed_refs;
1551 int ret;
1552
Josef Bacikafcdd122016-09-02 15:40:02 -04001553 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)
1554 || bytenr == 0 || num_bytes == 0)
Qu Wenruocb93b522016-08-15 10:36:50 +08001555 return 0;
1556 if (WARN_ON(trans == NULL))
1557 return -EINVAL;
1558 record = kmalloc(sizeof(*record), gfp_flag);
1559 if (!record)
1560 return -ENOMEM;
1561
1562 delayed_refs = &trans->transaction->delayed_refs;
1563 record->bytenr = bytenr;
1564 record->num_bytes = num_bytes;
1565 record->old_roots = NULL;
1566
1567 spin_lock(&delayed_refs->lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001568 ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record);
Qu Wenruocb93b522016-08-15 10:36:50 +08001569 spin_unlock(&delayed_refs->lock);
Qu Wenruofb235dc2017-02-15 10:43:03 +08001570 if (ret > 0) {
Qu Wenruocb93b522016-08-15 10:36:50 +08001571 kfree(record);
Qu Wenruofb235dc2017-02-15 10:43:03 +08001572 return 0;
1573 }
1574 return btrfs_qgroup_trace_extent_post(fs_info, record);
Qu Wenruo3368d002015-04-16 14:34:17 +08001575}
1576
Qu Wenruo33d1f052016-10-18 09:31:28 +08001577int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001578 struct btrfs_fs_info *fs_info,
Qu Wenruo33d1f052016-10-18 09:31:28 +08001579 struct extent_buffer *eb)
1580{
1581 int nr = btrfs_header_nritems(eb);
1582 int i, extent_type, ret;
1583 struct btrfs_key key;
1584 struct btrfs_file_extent_item *fi;
1585 u64 bytenr, num_bytes;
1586
1587 /* We can be called directly from walk_up_proc() */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001588 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Qu Wenruo33d1f052016-10-18 09:31:28 +08001589 return 0;
1590
1591 for (i = 0; i < nr; i++) {
1592 btrfs_item_key_to_cpu(eb, &key, i);
1593
1594 if (key.type != BTRFS_EXTENT_DATA_KEY)
1595 continue;
1596
1597 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
1598 /* filter out non qgroup-accountable extents */
1599 extent_type = btrfs_file_extent_type(eb, fi);
1600
1601 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
1602 continue;
1603
1604 bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1605 if (!bytenr)
1606 continue;
1607
1608 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1609
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001610 ret = btrfs_qgroup_trace_extent(trans, fs_info, bytenr,
1611 num_bytes, GFP_NOFS);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001612 if (ret)
1613 return ret;
1614 }
Jeff Mahoneycddf3b22017-06-20 08:15:26 -04001615 cond_resched();
Qu Wenruo33d1f052016-10-18 09:31:28 +08001616 return 0;
1617}
1618
1619/*
1620 * Walk up the tree from the bottom, freeing leaves and any interior
1621 * nodes which have had all slots visited. If a node (leaf or
1622 * interior) is freed, the node above it will have it's slot
1623 * incremented. The root node will never be freed.
1624 *
1625 * At the end of this function, we should have a path which has all
1626 * slots incremented to the next position for a search. If we need to
1627 * read a new node it will be NULL and the node above it will have the
1628 * correct slot selected for a later read.
1629 *
1630 * If we increment the root nodes slot counter past the number of
1631 * elements, 1 is returned to signal completion of the search.
1632 */
David Sterba15b34512017-02-10 20:30:23 +01001633static int adjust_slots_upwards(struct btrfs_path *path, int root_level)
Qu Wenruo33d1f052016-10-18 09:31:28 +08001634{
1635 int level = 0;
1636 int nr, slot;
1637 struct extent_buffer *eb;
1638
1639 if (root_level == 0)
1640 return 1;
1641
1642 while (level <= root_level) {
1643 eb = path->nodes[level];
1644 nr = btrfs_header_nritems(eb);
1645 path->slots[level]++;
1646 slot = path->slots[level];
1647 if (slot >= nr || level == 0) {
1648 /*
1649 * Don't free the root - we will detect this
1650 * condition after our loop and return a
1651 * positive value for caller to stop walking the tree.
1652 */
1653 if (level != root_level) {
1654 btrfs_tree_unlock_rw(eb, path->locks[level]);
1655 path->locks[level] = 0;
1656
1657 free_extent_buffer(eb);
1658 path->nodes[level] = NULL;
1659 path->slots[level] = 0;
1660 }
1661 } else {
1662 /*
1663 * We have a valid slot to walk back down
1664 * from. Stop here so caller can process these
1665 * new nodes.
1666 */
1667 break;
1668 }
1669
1670 level++;
1671 }
1672
1673 eb = path->nodes[root_level];
1674 if (path->slots[root_level] >= btrfs_header_nritems(eb))
1675 return 1;
1676
1677 return 0;
1678}
1679
1680int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
1681 struct btrfs_root *root,
1682 struct extent_buffer *root_eb,
1683 u64 root_gen, int root_level)
1684{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001685 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruo33d1f052016-10-18 09:31:28 +08001686 int ret = 0;
1687 int level;
1688 struct extent_buffer *eb = root_eb;
1689 struct btrfs_path *path = NULL;
1690
Nikolay Borisovb6e6bca2017-07-12 09:42:19 +03001691 BUG_ON(root_level < 0 || root_level >= BTRFS_MAX_LEVEL);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001692 BUG_ON(root_eb == NULL);
1693
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001694 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Qu Wenruo33d1f052016-10-18 09:31:28 +08001695 return 0;
1696
1697 if (!extent_buffer_uptodate(root_eb)) {
1698 ret = btrfs_read_buffer(root_eb, root_gen);
1699 if (ret)
1700 goto out;
1701 }
1702
1703 if (root_level == 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001704 ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, root_eb);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001705 goto out;
1706 }
1707
1708 path = btrfs_alloc_path();
1709 if (!path)
1710 return -ENOMEM;
1711
1712 /*
1713 * Walk down the tree. Missing extent blocks are filled in as
1714 * we go. Metadata is accounted every time we read a new
1715 * extent block.
1716 *
1717 * When we reach a leaf, we account for file extent items in it,
1718 * walk back up the tree (adjusting slot pointers as we go)
1719 * and restart the search process.
1720 */
1721 extent_buffer_get(root_eb); /* For path */
1722 path->nodes[root_level] = root_eb;
1723 path->slots[root_level] = 0;
1724 path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
1725walk_down:
1726 level = root_level;
1727 while (level >= 0) {
1728 if (path->nodes[level] == NULL) {
1729 int parent_slot;
1730 u64 child_gen;
1731 u64 child_bytenr;
1732
1733 /*
1734 * We need to get child blockptr/gen from parent before
1735 * we can read it.
1736 */
1737 eb = path->nodes[level + 1];
1738 parent_slot = path->slots[level + 1];
1739 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
1740 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
1741
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001742 eb = read_tree_block(fs_info, child_bytenr, child_gen);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001743 if (IS_ERR(eb)) {
1744 ret = PTR_ERR(eb);
1745 goto out;
1746 } else if (!extent_buffer_uptodate(eb)) {
1747 free_extent_buffer(eb);
1748 ret = -EIO;
1749 goto out;
1750 }
1751
1752 path->nodes[level] = eb;
1753 path->slots[level] = 0;
1754
1755 btrfs_tree_read_lock(eb);
1756 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1757 path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
1758
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001759 ret = btrfs_qgroup_trace_extent(trans, fs_info,
1760 child_bytenr,
1761 fs_info->nodesize,
1762 GFP_NOFS);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001763 if (ret)
1764 goto out;
1765 }
1766
1767 if (level == 0) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001768 ret = btrfs_qgroup_trace_leaf_items(trans,fs_info,
1769 path->nodes[level]);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001770 if (ret)
1771 goto out;
1772
1773 /* Nonzero return here means we completed our search */
David Sterba15b34512017-02-10 20:30:23 +01001774 ret = adjust_slots_upwards(path, root_level);
Qu Wenruo33d1f052016-10-18 09:31:28 +08001775 if (ret)
1776 break;
1777
1778 /* Restart search with new slots */
1779 goto walk_down;
1780 }
1781
1782 level--;
1783 }
1784
1785 ret = 0;
1786out:
1787 btrfs_free_path(path);
1788
1789 return ret;
1790}
1791
Qu Wenruod810ef22015-04-12 16:52:34 +08001792#define UPDATE_NEW 0
1793#define UPDATE_OLD 1
1794/*
1795 * Walk all of the roots that points to the bytenr and adjust their refcnts.
1796 */
1797static int qgroup_update_refcnt(struct btrfs_fs_info *fs_info,
1798 struct ulist *roots, struct ulist *tmp,
1799 struct ulist *qgroups, u64 seq, int update_old)
1800{
1801 struct ulist_node *unode;
1802 struct ulist_iterator uiter;
1803 struct ulist_node *tmp_unode;
1804 struct ulist_iterator tmp_uiter;
1805 struct btrfs_qgroup *qg;
1806 int ret = 0;
1807
1808 if (!roots)
1809 return 0;
1810 ULIST_ITER_INIT(&uiter);
1811 while ((unode = ulist_next(roots, &uiter))) {
1812 qg = find_qgroup_rb(fs_info, unode->val);
1813 if (!qg)
1814 continue;
1815
1816 ulist_reinit(tmp);
David Sterbaef2fff62016-10-26 16:23:50 +02001817 ret = ulist_add(qgroups, qg->qgroupid, qgroup_to_aux(qg),
Qu Wenruod810ef22015-04-12 16:52:34 +08001818 GFP_ATOMIC);
1819 if (ret < 0)
1820 return ret;
David Sterbaef2fff62016-10-26 16:23:50 +02001821 ret = ulist_add(tmp, qg->qgroupid, qgroup_to_aux(qg), GFP_ATOMIC);
Qu Wenruod810ef22015-04-12 16:52:34 +08001822 if (ret < 0)
1823 return ret;
1824 ULIST_ITER_INIT(&tmp_uiter);
1825 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
1826 struct btrfs_qgroup_list *glist;
1827
David Sterbaef2fff62016-10-26 16:23:50 +02001828 qg = unode_aux_to_qgroup(tmp_unode);
Qu Wenruod810ef22015-04-12 16:52:34 +08001829 if (update_old)
1830 btrfs_qgroup_update_old_refcnt(qg, seq, 1);
1831 else
1832 btrfs_qgroup_update_new_refcnt(qg, seq, 1);
1833 list_for_each_entry(glist, &qg->groups, next_group) {
1834 ret = ulist_add(qgroups, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02001835 qgroup_to_aux(glist->group),
Qu Wenruod810ef22015-04-12 16:52:34 +08001836 GFP_ATOMIC);
1837 if (ret < 0)
1838 return ret;
1839 ret = ulist_add(tmp, glist->group->qgroupid,
David Sterbaef2fff62016-10-26 16:23:50 +02001840 qgroup_to_aux(glist->group),
Qu Wenruod810ef22015-04-12 16:52:34 +08001841 GFP_ATOMIC);
1842 if (ret < 0)
1843 return ret;
1844 }
1845 }
1846 }
1847 return 0;
1848}
1849
Josef Bacikfcebe452014-05-13 17:30:47 -07001850/*
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001851 * Update qgroup rfer/excl counters.
1852 * Rfer update is easy, codes can explain themselves.
Qu Wenruoe69bcee2015-04-17 10:23:16 +08001853 *
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001854 * Excl update is tricky, the update is split into 2 part.
1855 * Part 1: Possible exclusive <-> sharing detect:
1856 * | A | !A |
1857 * -------------------------------------
1858 * B | * | - |
1859 * -------------------------------------
1860 * !B | + | ** |
1861 * -------------------------------------
1862 *
1863 * Conditions:
1864 * A: cur_old_roots < nr_old_roots (not exclusive before)
1865 * !A: cur_old_roots == nr_old_roots (possible exclusive before)
1866 * B: cur_new_roots < nr_new_roots (not exclusive now)
Nicholas D Steeves01327612016-05-19 21:18:45 -04001867 * !B: cur_new_roots == nr_new_roots (possible exclusive now)
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001868 *
1869 * Results:
1870 * +: Possible sharing -> exclusive -: Possible exclusive -> sharing
1871 * *: Definitely not changed. **: Possible unchanged.
1872 *
1873 * For !A and !B condition, the exception is cur_old/new_roots == 0 case.
1874 *
1875 * To make the logic clear, we first use condition A and B to split
1876 * combination into 4 results.
1877 *
1878 * Then, for result "+" and "-", check old/new_roots == 0 case, as in them
1879 * only on variant maybe 0.
1880 *
1881 * Lastly, check result **, since there are 2 variants maybe 0, split them
1882 * again(2x2).
1883 * But this time we don't need to consider other things, the codes and logic
1884 * is easy to understand now.
1885 */
1886static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
1887 struct ulist *qgroups,
1888 u64 nr_old_roots,
1889 u64 nr_new_roots,
1890 u64 num_bytes, u64 seq)
1891{
1892 struct ulist_node *unode;
1893 struct ulist_iterator uiter;
1894 struct btrfs_qgroup *qg;
1895 u64 cur_new_count, cur_old_count;
1896
1897 ULIST_ITER_INIT(&uiter);
1898 while ((unode = ulist_next(qgroups, &uiter))) {
1899 bool dirty = false;
1900
David Sterbaef2fff62016-10-26 16:23:50 +02001901 qg = unode_aux_to_qgroup(unode);
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001902 cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
1903 cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
1904
Jeff Mahoneybc074522016-06-09 17:27:55 -04001905 trace_qgroup_update_counters(fs_info, qg->qgroupid,
1906 cur_old_count, cur_new_count);
Mark Fasheh0f5dcf82016-03-29 17:19:55 -07001907
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001908 /* Rfer update part */
1909 if (cur_old_count == 0 && cur_new_count > 0) {
1910 qg->rfer += num_bytes;
1911 qg->rfer_cmpr += num_bytes;
1912 dirty = true;
1913 }
1914 if (cur_old_count > 0 && cur_new_count == 0) {
1915 qg->rfer -= num_bytes;
1916 qg->rfer_cmpr -= num_bytes;
1917 dirty = true;
1918 }
1919
1920 /* Excl update part */
1921 /* Exclusive/none -> shared case */
1922 if (cur_old_count == nr_old_roots &&
1923 cur_new_count < nr_new_roots) {
1924 /* Exclusive -> shared */
1925 if (cur_old_count != 0) {
1926 qg->excl -= num_bytes;
1927 qg->excl_cmpr -= num_bytes;
1928 dirty = true;
1929 }
1930 }
1931
1932 /* Shared -> exclusive/none case */
1933 if (cur_old_count < nr_old_roots &&
1934 cur_new_count == nr_new_roots) {
1935 /* Shared->exclusive */
1936 if (cur_new_count != 0) {
1937 qg->excl += num_bytes;
1938 qg->excl_cmpr += num_bytes;
1939 dirty = true;
1940 }
1941 }
1942
1943 /* Exclusive/none -> exclusive/none case */
1944 if (cur_old_count == nr_old_roots &&
1945 cur_new_count == nr_new_roots) {
1946 if (cur_old_count == 0) {
1947 /* None -> exclusive/none */
1948
1949 if (cur_new_count != 0) {
1950 /* None -> exclusive */
1951 qg->excl += num_bytes;
1952 qg->excl_cmpr += num_bytes;
1953 dirty = true;
1954 }
1955 /* None -> none, nothing changed */
1956 } else {
1957 /* Exclusive -> exclusive/none */
1958
1959 if (cur_new_count == 0) {
1960 /* Exclusive -> none */
1961 qg->excl -= num_bytes;
1962 qg->excl_cmpr -= num_bytes;
1963 dirty = true;
1964 }
1965 /* Exclusive -> exclusive, nothing changed */
1966 }
1967 }
Qu Wenruoc05f9422015-08-03 14:44:29 +08001968
Qu Wenruo823ae5b2015-04-12 16:59:57 +08001969 if (dirty)
1970 qgroup_dirty(fs_info, qg);
1971 }
1972 return 0;
1973}
1974
Qu Wenruo5edfd9f2017-02-27 15:10:34 +08001975/*
1976 * Check if the @roots potentially is a list of fs tree roots
1977 *
1978 * Return 0 for definitely not a fs/subvol tree roots ulist
1979 * Return 1 for possible fs/subvol tree roots in the list (considering an empty
1980 * one as well)
1981 */
1982static int maybe_fs_roots(struct ulist *roots)
1983{
1984 struct ulist_node *unode;
1985 struct ulist_iterator uiter;
1986
1987 /* Empty one, still possible for fs roots */
1988 if (!roots || roots->nnodes == 0)
1989 return 1;
1990
1991 ULIST_ITER_INIT(&uiter);
1992 unode = ulist_next(roots, &uiter);
1993 if (!unode)
1994 return 1;
1995
1996 /*
1997 * If it contains fs tree roots, then it must belong to fs/subvol
1998 * trees.
1999 * If it contains a non-fs tree, it won't be shared with fs/subvol trees.
2000 */
2001 return is_fstree(unode->val);
2002}
2003
Qu Wenruo442244c2015-04-16 17:18:36 +08002004int
Qu Wenruo550d7a22015-04-16 15:37:33 +08002005btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans,
2006 struct btrfs_fs_info *fs_info,
2007 u64 bytenr, u64 num_bytes,
2008 struct ulist *old_roots, struct ulist *new_roots)
2009{
2010 struct ulist *qgroups = NULL;
2011 struct ulist *tmp = NULL;
2012 u64 seq;
2013 u64 nr_new_roots = 0;
2014 u64 nr_old_roots = 0;
2015 int ret = 0;
2016
David Sterba81353d52017-02-13 14:05:24 +01002017 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
2018 return 0;
2019
Qu Wenruo5edfd9f2017-02-27 15:10:34 +08002020 if (new_roots) {
2021 if (!maybe_fs_roots(new_roots))
2022 goto out_free;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002023 nr_new_roots = new_roots->nnodes;
Qu Wenruo5edfd9f2017-02-27 15:10:34 +08002024 }
2025 if (old_roots) {
2026 if (!maybe_fs_roots(old_roots))
2027 goto out_free;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002028 nr_old_roots = old_roots->nnodes;
Qu Wenruo5edfd9f2017-02-27 15:10:34 +08002029 }
2030
2031 /* Quick exit, either not fs tree roots, or won't affect any qgroup */
2032 if (nr_old_roots == 0 && nr_new_roots == 0)
2033 goto out_free;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002034
Qu Wenruo550d7a22015-04-16 15:37:33 +08002035 BUG_ON(!fs_info->quota_root);
2036
Jeff Mahoneybc074522016-06-09 17:27:55 -04002037 trace_btrfs_qgroup_account_extent(fs_info, bytenr, num_bytes,
2038 nr_old_roots, nr_new_roots);
Mark Fasheh0f5dcf82016-03-29 17:19:55 -07002039
Qu Wenruo550d7a22015-04-16 15:37:33 +08002040 qgroups = ulist_alloc(GFP_NOFS);
2041 if (!qgroups) {
2042 ret = -ENOMEM;
2043 goto out_free;
2044 }
2045 tmp = ulist_alloc(GFP_NOFS);
2046 if (!tmp) {
2047 ret = -ENOMEM;
2048 goto out_free;
2049 }
2050
2051 mutex_lock(&fs_info->qgroup_rescan_lock);
2052 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
2053 if (fs_info->qgroup_rescan_progress.objectid <= bytenr) {
2054 mutex_unlock(&fs_info->qgroup_rescan_lock);
2055 ret = 0;
2056 goto out_free;
2057 }
2058 }
2059 mutex_unlock(&fs_info->qgroup_rescan_lock);
2060
2061 spin_lock(&fs_info->qgroup_lock);
2062 seq = fs_info->qgroup_seq;
2063
2064 /* Update old refcnts using old_roots */
2065 ret = qgroup_update_refcnt(fs_info, old_roots, tmp, qgroups, seq,
2066 UPDATE_OLD);
2067 if (ret < 0)
2068 goto out;
2069
2070 /* Update new refcnts using new_roots */
2071 ret = qgroup_update_refcnt(fs_info, new_roots, tmp, qgroups, seq,
2072 UPDATE_NEW);
2073 if (ret < 0)
2074 goto out;
2075
2076 qgroup_update_counters(fs_info, qgroups, nr_old_roots, nr_new_roots,
2077 num_bytes, seq);
2078
2079 /*
2080 * Bump qgroup_seq to avoid seq overlap
2081 */
2082 fs_info->qgroup_seq += max(nr_old_roots, nr_new_roots) + 1;
2083out:
2084 spin_unlock(&fs_info->qgroup_lock);
2085out_free:
2086 ulist_free(tmp);
2087 ulist_free(qgroups);
2088 ulist_free(old_roots);
2089 ulist_free(new_roots);
2090 return ret;
2091}
2092
Nikolay Borisov460fb202018-03-15 16:00:25 +02002093int btrfs_qgroup_account_extents(struct btrfs_trans_handle *trans)
Qu Wenruo550d7a22015-04-16 15:37:33 +08002094{
Nikolay Borisov460fb202018-03-15 16:00:25 +02002095 struct btrfs_fs_info *fs_info = trans->fs_info;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002096 struct btrfs_qgroup_extent_record *record;
2097 struct btrfs_delayed_ref_root *delayed_refs;
2098 struct ulist *new_roots = NULL;
2099 struct rb_node *node;
Qu Wenruo9086db82015-04-20 09:53:50 +08002100 u64 qgroup_to_skip;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002101 int ret = 0;
2102
2103 delayed_refs = &trans->transaction->delayed_refs;
Qu Wenruo9086db82015-04-20 09:53:50 +08002104 qgroup_to_skip = delayed_refs->qgroup_to_skip;
Qu Wenruo550d7a22015-04-16 15:37:33 +08002105 while ((node = rb_first(&delayed_refs->dirty_extent_root))) {
2106 record = rb_entry(node, struct btrfs_qgroup_extent_record,
2107 node);
2108
Jeff Mahoneybc074522016-06-09 17:27:55 -04002109 trace_btrfs_qgroup_account_extents(fs_info, record);
Mark Fasheh0f5dcf82016-03-29 17:19:55 -07002110
Qu Wenruo550d7a22015-04-16 15:37:33 +08002111 if (!ret) {
2112 /*
Qu Wenruod1b8b942017-02-27 15:10:35 +08002113 * Old roots should be searched when inserting qgroup
2114 * extent record
2115 */
2116 if (WARN_ON(!record->old_roots)) {
2117 /* Search commit root to find old_roots */
2118 ret = btrfs_find_all_roots(NULL, fs_info,
2119 record->bytenr, 0,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002120 &record->old_roots, false);
Qu Wenruod1b8b942017-02-27 15:10:35 +08002121 if (ret < 0)
2122 goto cleanup;
2123 }
2124
2125 /*
Edmund Nadolskide47c9d2017-03-16 10:04:34 -06002126 * Use SEQ_LAST as time_seq to do special search, which
Qu Wenruo550d7a22015-04-16 15:37:33 +08002127 * doesn't lock tree or delayed_refs and search current
2128 * root. It's safe inside commit_transaction().
2129 */
2130 ret = btrfs_find_all_roots(trans, fs_info,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002131 record->bytenr, SEQ_LAST, &new_roots, false);
Qu Wenruo550d7a22015-04-16 15:37:33 +08002132 if (ret < 0)
2133 goto cleanup;
Qu Wenruod1b8b942017-02-27 15:10:35 +08002134 if (qgroup_to_skip) {
Qu Wenruo9086db82015-04-20 09:53:50 +08002135 ulist_del(new_roots, qgroup_to_skip, 0);
Qu Wenruod1b8b942017-02-27 15:10:35 +08002136 ulist_del(record->old_roots, qgroup_to_skip,
2137 0);
2138 }
Qu Wenruo550d7a22015-04-16 15:37:33 +08002139 ret = btrfs_qgroup_account_extent(trans, fs_info,
2140 record->bytenr, record->num_bytes,
2141 record->old_roots, new_roots);
2142 record->old_roots = NULL;
2143 new_roots = NULL;
2144 }
2145cleanup:
2146 ulist_free(record->old_roots);
2147 ulist_free(new_roots);
2148 new_roots = NULL;
2149 rb_erase(node, &delayed_refs->dirty_extent_root);
2150 kfree(record);
2151
2152 }
2153 return ret;
2154}
2155
Josef Bacikfcebe452014-05-13 17:30:47 -07002156/*
Arne Jansenbed92ea2012-06-28 18:03:02 +02002157 * called from commit_transaction. Writes all changed qgroups to disk.
2158 */
2159int btrfs_run_qgroups(struct btrfs_trans_handle *trans,
2160 struct btrfs_fs_info *fs_info)
2161{
2162 struct btrfs_root *quota_root = fs_info->quota_root;
2163 int ret = 0;
2164
2165 if (!quota_root)
Nikolay Borisov5d235152018-01-31 10:52:04 +02002166 return ret;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002167
2168 spin_lock(&fs_info->qgroup_lock);
2169 while (!list_empty(&fs_info->dirty_qgroups)) {
2170 struct btrfs_qgroup *qgroup;
2171 qgroup = list_first_entry(&fs_info->dirty_qgroups,
2172 struct btrfs_qgroup, dirty);
2173 list_del_init(&qgroup->dirty);
2174 spin_unlock(&fs_info->qgroup_lock);
2175 ret = update_qgroup_info_item(trans, quota_root, qgroup);
2176 if (ret)
2177 fs_info->qgroup_flags |=
2178 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Dongsheng Yangd3001ed2014-11-20 21:04:56 -05002179 ret = update_qgroup_limit_item(trans, quota_root, qgroup);
2180 if (ret)
2181 fs_info->qgroup_flags |=
2182 BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002183 spin_lock(&fs_info->qgroup_lock);
2184 }
Josef Bacikafcdd122016-09-02 15:40:02 -04002185 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Arne Jansenbed92ea2012-06-28 18:03:02 +02002186 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_ON;
2187 else
2188 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
2189 spin_unlock(&fs_info->qgroup_lock);
2190
2191 ret = update_qgroup_status_item(trans, fs_info, quota_root);
2192 if (ret)
2193 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2194
Arne Jansenbed92ea2012-06-28 18:03:02 +02002195 return ret;
2196}
2197
2198/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002199 * Copy the accounting information between qgroups. This is necessary
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002200 * when a snapshot or a subvolume is created. Throwing an error will
2201 * cause a transaction abort so we take extra care here to only error
2202 * when a readonly fs is a reasonable outcome.
Arne Jansenbed92ea2012-06-28 18:03:02 +02002203 */
2204int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
2205 struct btrfs_fs_info *fs_info, u64 srcid, u64 objectid,
2206 struct btrfs_qgroup_inherit *inherit)
2207{
2208 int ret = 0;
2209 int i;
2210 u64 *i_qgroups;
2211 struct btrfs_root *quota_root = fs_info->quota_root;
2212 struct btrfs_qgroup *srcgroup;
2213 struct btrfs_qgroup *dstgroup;
2214 u32 level_size = 0;
Wang Shilong3f5e2d32013-04-07 10:50:19 +00002215 u64 nums;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002216
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002217 mutex_lock(&fs_info->qgroup_ioctl_lock);
Josef Bacikafcdd122016-09-02 15:40:02 -04002218 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002219 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002220
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002221 if (!quota_root) {
2222 ret = -EINVAL;
2223 goto out;
2224 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002225
Wang Shilong3f5e2d32013-04-07 10:50:19 +00002226 if (inherit) {
2227 i_qgroups = (u64 *)(inherit + 1);
2228 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
2229 2 * inherit->num_excl_copies;
2230 for (i = 0; i < nums; ++i) {
2231 srcgroup = find_qgroup_rb(fs_info, *i_qgroups);
Dongsheng Yang09870d22014-11-11 07:18:22 -05002232
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002233 /*
2234 * Zero out invalid groups so we can ignore
2235 * them later.
2236 */
2237 if (!srcgroup ||
2238 ((srcgroup->qgroupid >> 48) <= (objectid >> 48)))
2239 *i_qgroups = 0ULL;
2240
Wang Shilong3f5e2d32013-04-07 10:50:19 +00002241 ++i_qgroups;
2242 }
2243 }
2244
Arne Jansenbed92ea2012-06-28 18:03:02 +02002245 /*
2246 * create a tracking group for the subvol itself
2247 */
2248 ret = add_qgroup_item(trans, quota_root, objectid);
2249 if (ret)
2250 goto out;
2251
Arne Jansenbed92ea2012-06-28 18:03:02 +02002252 if (srcid) {
2253 struct btrfs_root *srcroot;
2254 struct btrfs_key srckey;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002255
2256 srckey.objectid = srcid;
2257 srckey.type = BTRFS_ROOT_ITEM_KEY;
2258 srckey.offset = (u64)-1;
2259 srcroot = btrfs_read_fs_root_no_name(fs_info, &srckey);
2260 if (IS_ERR(srcroot)) {
2261 ret = PTR_ERR(srcroot);
2262 goto out;
2263 }
2264
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002265 level_size = fs_info->nodesize;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002266 }
2267
2268 /*
2269 * add qgroup to all inherited groups
2270 */
2271 if (inherit) {
2272 i_qgroups = (u64 *)(inherit + 1);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002273 for (i = 0; i < inherit->num_qgroups; ++i, ++i_qgroups) {
2274 if (*i_qgroups == 0)
2275 continue;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002276 ret = add_qgroup_relation_item(trans, quota_root,
2277 objectid, *i_qgroups);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002278 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002279 goto out;
2280 ret = add_qgroup_relation_item(trans, quota_root,
2281 *i_qgroups, objectid);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002282 if (ret && ret != -EEXIST)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002283 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002284 }
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002285 ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002286 }
2287
2288
2289 spin_lock(&fs_info->qgroup_lock);
2290
2291 dstgroup = add_qgroup_rb(fs_info, objectid);
Dan Carpenter57a5a882012-07-30 02:15:43 -06002292 if (IS_ERR(dstgroup)) {
2293 ret = PTR_ERR(dstgroup);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002294 goto unlock;
Dan Carpenter57a5a882012-07-30 02:15:43 -06002295 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002296
Dongsheng Yange8c85412014-11-20 20:58:34 -05002297 if (inherit && inherit->flags & BTRFS_QGROUP_INHERIT_SET_LIMITS) {
Dongsheng Yange8c85412014-11-20 20:58:34 -05002298 dstgroup->lim_flags = inherit->lim.flags;
2299 dstgroup->max_rfer = inherit->lim.max_rfer;
2300 dstgroup->max_excl = inherit->lim.max_excl;
2301 dstgroup->rsv_rfer = inherit->lim.rsv_rfer;
2302 dstgroup->rsv_excl = inherit->lim.rsv_excl;
Dongsheng Yang1510e712014-11-20 21:01:41 -05002303
2304 ret = update_qgroup_limit_item(trans, quota_root, dstgroup);
2305 if (ret) {
2306 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002307 btrfs_info(fs_info,
2308 "unable to update quota limit for %llu",
2309 dstgroup->qgroupid);
Dongsheng Yang1510e712014-11-20 21:01:41 -05002310 goto unlock;
2311 }
Dongsheng Yange8c85412014-11-20 20:58:34 -05002312 }
2313
Arne Jansenbed92ea2012-06-28 18:03:02 +02002314 if (srcid) {
2315 srcgroup = find_qgroup_rb(fs_info, srcid);
Chris Masonf3a87f12012-09-14 20:06:30 -04002316 if (!srcgroup)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002317 goto unlock;
Josef Bacikfcebe452014-05-13 17:30:47 -07002318
2319 /*
2320 * We call inherit after we clone the root in order to make sure
2321 * our counts don't go crazy, so at this point the only
2322 * difference between the two roots should be the root node.
2323 */
2324 dstgroup->rfer = srcgroup->rfer;
2325 dstgroup->rfer_cmpr = srcgroup->rfer_cmpr;
2326 dstgroup->excl = level_size;
2327 dstgroup->excl_cmpr = level_size;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002328 srcgroup->excl = level_size;
2329 srcgroup->excl_cmpr = level_size;
Dongsheng Yang3eeb4d52014-11-20 20:14:38 -05002330
2331 /* inherit the limit info */
2332 dstgroup->lim_flags = srcgroup->lim_flags;
2333 dstgroup->max_rfer = srcgroup->max_rfer;
2334 dstgroup->max_excl = srcgroup->max_excl;
2335 dstgroup->rsv_rfer = srcgroup->rsv_rfer;
2336 dstgroup->rsv_excl = srcgroup->rsv_excl;
2337
Arne Jansenbed92ea2012-06-28 18:03:02 +02002338 qgroup_dirty(fs_info, dstgroup);
2339 qgroup_dirty(fs_info, srcgroup);
2340 }
2341
Chris Masonf3a87f12012-09-14 20:06:30 -04002342 if (!inherit)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002343 goto unlock;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002344
2345 i_qgroups = (u64 *)(inherit + 1);
2346 for (i = 0; i < inherit->num_qgroups; ++i) {
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002347 if (*i_qgroups) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002348 ret = add_relation_rb(fs_info, objectid, *i_qgroups);
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002349 if (ret)
2350 goto unlock;
2351 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002352 ++i_qgroups;
2353 }
2354
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002355 for (i = 0; i < inherit->num_ref_copies; ++i, i_qgroups += 2) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002356 struct btrfs_qgroup *src;
2357 struct btrfs_qgroup *dst;
2358
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002359 if (!i_qgroups[0] || !i_qgroups[1])
2360 continue;
2361
Arne Jansenbed92ea2012-06-28 18:03:02 +02002362 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2363 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2364
2365 if (!src || !dst) {
2366 ret = -EINVAL;
2367 goto unlock;
2368 }
2369
2370 dst->rfer = src->rfer - level_size;
2371 dst->rfer_cmpr = src->rfer_cmpr - level_size;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002372 }
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002373 for (i = 0; i < inherit->num_excl_copies; ++i, i_qgroups += 2) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002374 struct btrfs_qgroup *src;
2375 struct btrfs_qgroup *dst;
2376
Mark Fasheh918c2ee2016-03-30 17:57:48 -07002377 if (!i_qgroups[0] || !i_qgroups[1])
2378 continue;
2379
Arne Jansenbed92ea2012-06-28 18:03:02 +02002380 src = find_qgroup_rb(fs_info, i_qgroups[0]);
2381 dst = find_qgroup_rb(fs_info, i_qgroups[1]);
2382
2383 if (!src || !dst) {
2384 ret = -EINVAL;
2385 goto unlock;
2386 }
2387
2388 dst->excl = src->excl + level_size;
2389 dst->excl_cmpr = src->excl_cmpr + level_size;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002390 }
2391
2392unlock:
2393 spin_unlock(&fs_info->qgroup_lock);
2394out:
Wang Shilongf2f6ed32013-04-07 10:50:16 +00002395 mutex_unlock(&fs_info->qgroup_ioctl_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002396 return ret;
2397}
2398
Jeff Mahoney003d7c52017-01-25 09:50:33 -05002399static bool qgroup_check_limits(const struct btrfs_qgroup *qg, u64 num_bytes)
2400{
2401 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
Qu Wenruodba21322017-12-12 15:34:25 +08002402 qgroup_rsv_total(qg) + (s64)qg->rfer + num_bytes > qg->max_rfer)
Jeff Mahoney003d7c52017-01-25 09:50:33 -05002403 return false;
2404
2405 if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL) &&
Qu Wenruodba21322017-12-12 15:34:25 +08002406 qgroup_rsv_total(qg) + (s64)qg->excl + num_bytes > qg->max_excl)
Jeff Mahoney003d7c52017-01-25 09:50:33 -05002407 return false;
2408
2409 return true;
2410}
2411
Qu Wenruodba21322017-12-12 15:34:25 +08002412static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce,
2413 enum btrfs_qgroup_rsv_type type)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002414{
2415 struct btrfs_root *quota_root;
2416 struct btrfs_qgroup *qgroup;
2417 struct btrfs_fs_info *fs_info = root->fs_info;
2418 u64 ref_root = root->root_key.objectid;
2419 int ret = 0;
Goldwyn Rodrigues48a89bc2017-03-27 12:29:57 -05002420 int retried = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002421 struct ulist_node *unode;
2422 struct ulist_iterator uiter;
2423
2424 if (!is_fstree(ref_root))
2425 return 0;
2426
2427 if (num_bytes == 0)
2428 return 0;
Sargun Dhillonf29efe22017-05-11 21:17:33 +00002429
2430 if (test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags) &&
2431 capable(CAP_SYS_RESOURCE))
2432 enforce = false;
2433
Goldwyn Rodrigues48a89bc2017-03-27 12:29:57 -05002434retry:
Arne Jansenbed92ea2012-06-28 18:03:02 +02002435 spin_lock(&fs_info->qgroup_lock);
2436 quota_root = fs_info->quota_root;
2437 if (!quota_root)
2438 goto out;
2439
2440 qgroup = find_qgroup_rb(fs_info, ref_root);
2441 if (!qgroup)
2442 goto out;
2443
2444 /*
2445 * in a first step, we check all affected qgroups if any limits would
2446 * be exceeded
2447 */
Wang Shilong1e8f9152013-05-06 11:03:27 +00002448 ulist_reinit(fs_info->qgroup_ulist);
2449 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
Wang Shilong3c971852013-04-17 14:00:36 +00002450 (uintptr_t)qgroup, GFP_ATOMIC);
2451 if (ret < 0)
2452 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002453 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00002454 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002455 struct btrfs_qgroup *qg;
2456 struct btrfs_qgroup_list *glist;
2457
David Sterbaef2fff62016-10-26 16:23:50 +02002458 qg = unode_aux_to_qgroup(unode);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002459
Jeff Mahoney003d7c52017-01-25 09:50:33 -05002460 if (enforce && !qgroup_check_limits(qg, num_bytes)) {
Goldwyn Rodrigues48a89bc2017-03-27 12:29:57 -05002461 /*
2462 * Commit the tree and retry, since we may have
2463 * deletions which would free up space.
2464 */
Qu Wenruodba21322017-12-12 15:34:25 +08002465 if (!retried && qgroup_rsv_total(qg) > 0) {
Goldwyn Rodrigues48a89bc2017-03-27 12:29:57 -05002466 struct btrfs_trans_handle *trans;
2467
2468 spin_unlock(&fs_info->qgroup_lock);
2469 ret = btrfs_start_delalloc_inodes(root, 0);
2470 if (ret)
2471 return ret;
Chris Mason6374e57a2017-06-23 09:48:21 -07002472 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
Goldwyn Rodrigues48a89bc2017-03-27 12:29:57 -05002473 trans = btrfs_join_transaction(root);
2474 if (IS_ERR(trans))
2475 return PTR_ERR(trans);
2476 ret = btrfs_commit_transaction(trans);
2477 if (ret)
2478 return ret;
2479 retried++;
2480 goto retry;
2481 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002482 ret = -EDQUOT;
Wang Shilong720f1e22013-03-06 11:51:47 +00002483 goto out;
2484 }
Arne Jansenbed92ea2012-06-28 18:03:02 +02002485
2486 list_for_each_entry(glist, &qg->groups, next_group) {
Wang Shilong1e8f9152013-05-06 11:03:27 +00002487 ret = ulist_add(fs_info->qgroup_ulist,
2488 glist->group->qgroupid,
Wang Shilong3c971852013-04-17 14:00:36 +00002489 (uintptr_t)glist->group, GFP_ATOMIC);
2490 if (ret < 0)
2491 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002492 }
2493 }
Wang Shilong3c971852013-04-17 14:00:36 +00002494 ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002495 /*
2496 * no limits exceeded, now record the reservation into all qgroups
2497 */
2498 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00002499 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002500 struct btrfs_qgroup *qg;
2501
David Sterbaef2fff62016-10-26 16:23:50 +02002502 qg = unode_aux_to_qgroup(unode);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002503
Qu Wenruo64ee4e72017-12-12 15:34:27 +08002504 trace_qgroup_update_reserve(fs_info, qg, num_bytes, type);
2505 qgroup_rsv_add(fs_info, qg, num_bytes, type);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002506 }
2507
2508out:
2509 spin_unlock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002510 return ret;
2511}
2512
Qu Wenruo297d7502015-09-08 17:08:37 +08002513void btrfs_qgroup_free_refroot(struct btrfs_fs_info *fs_info,
Qu Wenruod4e5c922017-12-12 15:34:23 +08002514 u64 ref_root, u64 num_bytes,
2515 enum btrfs_qgroup_rsv_type type)
Arne Jansenbed92ea2012-06-28 18:03:02 +02002516{
2517 struct btrfs_root *quota_root;
2518 struct btrfs_qgroup *qgroup;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002519 struct ulist_node *unode;
2520 struct ulist_iterator uiter;
Wang Shilong3c971852013-04-17 14:00:36 +00002521 int ret = 0;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002522
2523 if (!is_fstree(ref_root))
2524 return;
2525
2526 if (num_bytes == 0)
2527 return;
2528
2529 spin_lock(&fs_info->qgroup_lock);
2530
2531 quota_root = fs_info->quota_root;
2532 if (!quota_root)
2533 goto out;
2534
2535 qgroup = find_qgroup_rb(fs_info, ref_root);
2536 if (!qgroup)
2537 goto out;
2538
Wang Shilong1e8f9152013-05-06 11:03:27 +00002539 ulist_reinit(fs_info->qgroup_ulist);
2540 ret = ulist_add(fs_info->qgroup_ulist, qgroup->qgroupid,
Wang Shilong3c971852013-04-17 14:00:36 +00002541 (uintptr_t)qgroup, GFP_ATOMIC);
2542 if (ret < 0)
2543 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002544 ULIST_ITER_INIT(&uiter);
Wang Shilong1e8f9152013-05-06 11:03:27 +00002545 while ((unode = ulist_next(fs_info->qgroup_ulist, &uiter))) {
Arne Jansenbed92ea2012-06-28 18:03:02 +02002546 struct btrfs_qgroup *qg;
2547 struct btrfs_qgroup_list *glist;
2548
David Sterbaef2fff62016-10-26 16:23:50 +02002549 qg = unode_aux_to_qgroup(unode);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002550
Qu Wenruo64ee4e72017-12-12 15:34:27 +08002551 trace_qgroup_update_reserve(fs_info, qg, -(s64)num_bytes, type);
2552 qgroup_rsv_release(fs_info, qg, num_bytes, type);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002553
2554 list_for_each_entry(glist, &qg->groups, next_group) {
Wang Shilong1e8f9152013-05-06 11:03:27 +00002555 ret = ulist_add(fs_info->qgroup_ulist,
2556 glist->group->qgroupid,
Wang Shilong3c971852013-04-17 14:00:36 +00002557 (uintptr_t)glist->group, GFP_ATOMIC);
2558 if (ret < 0)
2559 goto out;
Arne Jansenbed92ea2012-06-28 18:03:02 +02002560 }
2561 }
2562
2563out:
2564 spin_unlock(&fs_info->qgroup_lock);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002565}
2566
Jan Schmidt2f232032013-04-25 16:04:51 +00002567/*
2568 * returns < 0 on error, 0 when more leafs are to be scanned.
Qu Wenruo33931682015-02-27 16:24:24 +08002569 * returns 1 when done.
Jan Schmidt2f232032013-04-25 16:04:51 +00002570 */
2571static int
Jan Schmidtb382a322013-05-28 15:47:24 +00002572qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
Qu Wenruo0a0e8b82015-10-26 09:19:43 +08002573 struct btrfs_trans_handle *trans)
Jan Schmidt2f232032013-04-25 16:04:51 +00002574{
2575 struct btrfs_key found;
Qu Wenruo0a0e8b82015-10-26 09:19:43 +08002576 struct extent_buffer *scratch_leaf = NULL;
Jan Schmidt2f232032013-04-25 16:04:51 +00002577 struct ulist *roots = NULL;
David Sterba3284da72015-02-25 15:47:32 +01002578 struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
Josef Bacikfcebe452014-05-13 17:30:47 -07002579 u64 num_bytes;
Jan Schmidt2f232032013-04-25 16:04:51 +00002580 int slot;
2581 int ret;
2582
Jan Schmidt2f232032013-04-25 16:04:51 +00002583 mutex_lock(&fs_info->qgroup_rescan_lock);
2584 ret = btrfs_search_slot_for_read(fs_info->extent_root,
2585 &fs_info->qgroup_rescan_progress,
2586 path, 1, 0);
2587
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002588 btrfs_debug(fs_info,
2589 "current progress key (%llu %u %llu), search_slot ret %d",
2590 fs_info->qgroup_rescan_progress.objectid,
2591 fs_info->qgroup_rescan_progress.type,
2592 fs_info->qgroup_rescan_progress.offset, ret);
Jan Schmidt2f232032013-04-25 16:04:51 +00002593
2594 if (ret) {
2595 /*
2596 * The rescan is about to end, we will not be scanning any
2597 * further blocks. We cannot unset the RESCAN flag here, because
2598 * we want to commit the transaction if everything went well.
2599 * To make the live accounting work in this phase, we set our
2600 * scan progress pointer such that every real extent objectid
2601 * will be smaller.
2602 */
2603 fs_info->qgroup_rescan_progress.objectid = (u64)-1;
2604 btrfs_release_path(path);
2605 mutex_unlock(&fs_info->qgroup_rescan_lock);
2606 return ret;
2607 }
2608
2609 btrfs_item_key_to_cpu(path->nodes[0], &found,
2610 btrfs_header_nritems(path->nodes[0]) - 1);
2611 fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
2612
2613 btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
Qu Wenruo0a0e8b82015-10-26 09:19:43 +08002614 scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
2615 if (!scratch_leaf) {
2616 ret = -ENOMEM;
2617 mutex_unlock(&fs_info->qgroup_rescan_lock);
2618 goto out;
2619 }
2620 extent_buffer_get(scratch_leaf);
2621 btrfs_tree_read_lock(scratch_leaf);
2622 btrfs_set_lock_blocking_rw(scratch_leaf, BTRFS_READ_LOCK);
Jan Schmidt2f232032013-04-25 16:04:51 +00002623 slot = path->slots[0];
2624 btrfs_release_path(path);
2625 mutex_unlock(&fs_info->qgroup_rescan_lock);
2626
2627 for (; slot < btrfs_header_nritems(scratch_leaf); ++slot) {
2628 btrfs_item_key_to_cpu(scratch_leaf, &found, slot);
Josef Bacik3a6d75e2014-01-23 16:45:10 -05002629 if (found.type != BTRFS_EXTENT_ITEM_KEY &&
2630 found.type != BTRFS_METADATA_ITEM_KEY)
Jan Schmidt2f232032013-04-25 16:04:51 +00002631 continue;
Josef Bacik3a6d75e2014-01-23 16:45:10 -05002632 if (found.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoneyda170662016-06-15 09:22:56 -04002633 num_bytes = fs_info->nodesize;
Josef Bacik3a6d75e2014-01-23 16:45:10 -05002634 else
2635 num_bytes = found.offset;
2636
Josef Bacikfcebe452014-05-13 17:30:47 -07002637 ret = btrfs_find_all_roots(NULL, fs_info, found.objectid, 0,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002638 &roots, false);
Jan Schmidt2f232032013-04-25 16:04:51 +00002639 if (ret < 0)
2640 goto out;
Qu Wenruo9d220c92015-04-13 11:02:16 +08002641 /* For rescan, just pass old_roots as NULL */
2642 ret = btrfs_qgroup_account_extent(trans, fs_info,
2643 found.objectid, num_bytes, NULL, roots);
2644 if (ret < 0)
Jan Schmidt2f232032013-04-25 16:04:51 +00002645 goto out;
Jan Schmidt2f232032013-04-25 16:04:51 +00002646 }
Jan Schmidt2f232032013-04-25 16:04:51 +00002647out:
Qu Wenruo0a0e8b82015-10-26 09:19:43 +08002648 if (scratch_leaf) {
2649 btrfs_tree_read_unlock_blocking(scratch_leaf);
2650 free_extent_buffer(scratch_leaf);
2651 }
Jan Schmidt2f232032013-04-25 16:04:51 +00002652 btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
2653
2654 return ret;
2655}
2656
Qu Wenruod458b052014-02-28 10:46:19 +08002657static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
Jan Schmidt2f232032013-04-25 16:04:51 +00002658{
Jan Schmidtb382a322013-05-28 15:47:24 +00002659 struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
2660 qgroup_rescan_work);
Jan Schmidt2f232032013-04-25 16:04:51 +00002661 struct btrfs_path *path;
2662 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt2f232032013-04-25 16:04:51 +00002663 int err = -ENOMEM;
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002664 int ret = 0;
Jan Schmidt2f232032013-04-25 16:04:51 +00002665
2666 path = btrfs_alloc_path();
2667 if (!path)
2668 goto out;
Jan Schmidt2f232032013-04-25 16:04:51 +00002669
2670 err = 0;
Justin Maggard7343dd62015-11-04 15:56:16 -08002671 while (!err && !btrfs_fs_closing(fs_info)) {
Jan Schmidt2f232032013-04-25 16:04:51 +00002672 trans = btrfs_start_transaction(fs_info->fs_root, 0);
2673 if (IS_ERR(trans)) {
2674 err = PTR_ERR(trans);
2675 break;
2676 }
Josef Bacikafcdd122016-09-02 15:40:02 -04002677 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
Jan Schmidt2f232032013-04-25 16:04:51 +00002678 err = -EINTR;
2679 } else {
Qu Wenruo0a0e8b82015-10-26 09:19:43 +08002680 err = qgroup_rescan_leaf(fs_info, path, trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00002681 }
2682 if (err > 0)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002683 btrfs_commit_transaction(trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00002684 else
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002685 btrfs_end_transaction(trans);
Jan Schmidt2f232032013-04-25 16:04:51 +00002686 }
2687
2688out:
Jan Schmidt2f232032013-04-25 16:04:51 +00002689 btrfs_free_path(path);
Jan Schmidt2f232032013-04-25 16:04:51 +00002690
2691 mutex_lock(&fs_info->qgroup_rescan_lock);
Justin Maggard7343dd62015-11-04 15:56:16 -08002692 if (!btrfs_fs_closing(fs_info))
2693 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
Jan Schmidt2f232032013-04-25 16:04:51 +00002694
Qu Wenruo33931682015-02-27 16:24:24 +08002695 if (err > 0 &&
Jan Schmidt2f232032013-04-25 16:04:51 +00002696 fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT) {
2697 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2698 } else if (err < 0) {
2699 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
2700 }
2701 mutex_unlock(&fs_info->qgroup_rescan_lock);
2702
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002703 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002704 * only update status, since the previous part has already updated the
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002705 * qgroup info.
2706 */
2707 trans = btrfs_start_transaction(fs_info->quota_root, 1);
2708 if (IS_ERR(trans)) {
2709 err = PTR_ERR(trans);
2710 btrfs_err(fs_info,
David Sterba913e1532017-07-13 15:32:18 +02002711 "fail to start transaction for status update: %d",
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002712 err);
2713 goto done;
2714 }
2715 ret = update_qgroup_status_item(trans, fs_info, fs_info->quota_root);
2716 if (ret < 0) {
2717 err = ret;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002718 btrfs_err(fs_info, "fail to update qgroup status: %d", err);
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002719 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002720 btrfs_end_transaction(trans);
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002721
Justin Maggard7343dd62015-11-04 15:56:16 -08002722 if (btrfs_fs_closing(fs_info)) {
2723 btrfs_info(fs_info, "qgroup scan paused");
2724 } else if (err >= 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05002725 btrfs_info(fs_info, "qgroup scan completed%s",
Qu Wenruo33931682015-02-27 16:24:24 +08002726 err > 0 ? " (inconsistency flag cleared)" : "");
Jan Schmidt2f232032013-04-25 16:04:51 +00002727 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05002728 btrfs_err(fs_info, "qgroup scan failed with %d", err);
Jan Schmidt2f232032013-04-25 16:04:51 +00002729 }
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002730
Qu Wenruo53b7cde2015-02-27 16:24:25 +08002731done:
Jeff Mahoneyd2c609b2016-08-15 12:10:33 -04002732 mutex_lock(&fs_info->qgroup_rescan_lock);
2733 fs_info->qgroup_rescan_running = false;
2734 mutex_unlock(&fs_info->qgroup_rescan_lock);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002735 complete_all(&fs_info->qgroup_rescan_completion);
Jan Schmidt2f232032013-04-25 16:04:51 +00002736}
2737
Jan Schmidtb382a322013-05-28 15:47:24 +00002738/*
2739 * Checks that (a) no rescan is running and (b) quota is enabled. Allocates all
2740 * memory required for the rescan context.
2741 */
2742static int
2743qgroup_rescan_init(struct btrfs_fs_info *fs_info, u64 progress_objectid,
2744 int init_flags)
Jan Schmidt2f232032013-04-25 16:04:51 +00002745{
2746 int ret = 0;
Jan Schmidt2f232032013-04-25 16:04:51 +00002747
Jan Schmidtb382a322013-05-28 15:47:24 +00002748 if (!init_flags &&
2749 (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) ||
2750 !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))) {
2751 ret = -EINVAL;
2752 goto err;
2753 }
Jan Schmidt2f232032013-04-25 16:04:51 +00002754
2755 mutex_lock(&fs_info->qgroup_rescan_lock);
2756 spin_lock(&fs_info->qgroup_lock);
Jan Schmidtb382a322013-05-28 15:47:24 +00002757
2758 if (init_flags) {
2759 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
2760 ret = -EINPROGRESS;
2761 else if (!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON))
2762 ret = -EINVAL;
2763
2764 if (ret) {
2765 spin_unlock(&fs_info->qgroup_lock);
2766 mutex_unlock(&fs_info->qgroup_rescan_lock);
2767 goto err;
2768 }
Jan Schmidtb382a322013-05-28 15:47:24 +00002769 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2770 }
2771
2772 memset(&fs_info->qgroup_rescan_progress, 0,
2773 sizeof(fs_info->qgroup_rescan_progress));
2774 fs_info->qgroup_rescan_progress.objectid = progress_objectid;
Filipe Manana190631f2015-11-05 10:06:23 +00002775 init_completion(&fs_info->qgroup_rescan_completion);
Filipe Manana8d9edda2016-11-24 02:09:04 +00002776 fs_info->qgroup_rescan_running = true;
Jan Schmidtb382a322013-05-28 15:47:24 +00002777
2778 spin_unlock(&fs_info->qgroup_lock);
2779 mutex_unlock(&fs_info->qgroup_rescan_lock);
2780
Jan Schmidtb382a322013-05-28 15:47:24 +00002781 memset(&fs_info->qgroup_rescan_work, 0,
2782 sizeof(fs_info->qgroup_rescan_work));
Qu Wenruofc97fab2014-02-28 10:46:16 +08002783 btrfs_init_work(&fs_info->qgroup_rescan_work,
Liu Bo9e0af232014-08-15 23:36:53 +08002784 btrfs_qgroup_rescan_helper,
Qu Wenruofc97fab2014-02-28 10:46:16 +08002785 btrfs_qgroup_rescan_worker, NULL, NULL);
Jan Schmidtb382a322013-05-28 15:47:24 +00002786
Jan Schmidt2f232032013-04-25 16:04:51 +00002787 if (ret) {
Jan Schmidtb382a322013-05-28 15:47:24 +00002788err:
Frank Holtonefe120a2013-12-20 11:37:06 -05002789 btrfs_info(fs_info, "qgroup_rescan_init failed with %d", ret);
Jan Schmidt2f232032013-04-25 16:04:51 +00002790 return ret;
2791 }
2792
Jan Schmidtb382a322013-05-28 15:47:24 +00002793 return 0;
2794}
Jan Schmidt2f232032013-04-25 16:04:51 +00002795
Jan Schmidtb382a322013-05-28 15:47:24 +00002796static void
2797qgroup_rescan_zero_tracking(struct btrfs_fs_info *fs_info)
2798{
2799 struct rb_node *n;
2800 struct btrfs_qgroup *qgroup;
2801
2802 spin_lock(&fs_info->qgroup_lock);
Jan Schmidt2f232032013-04-25 16:04:51 +00002803 /* clear all current qgroup tracking information */
2804 for (n = rb_first(&fs_info->qgroup_tree); n; n = rb_next(n)) {
2805 qgroup = rb_entry(n, struct btrfs_qgroup, node);
2806 qgroup->rfer = 0;
2807 qgroup->rfer_cmpr = 0;
2808 qgroup->excl = 0;
2809 qgroup->excl_cmpr = 0;
2810 }
2811 spin_unlock(&fs_info->qgroup_lock);
Jan Schmidtb382a322013-05-28 15:47:24 +00002812}
Jan Schmidt2f232032013-04-25 16:04:51 +00002813
Jan Schmidtb382a322013-05-28 15:47:24 +00002814int
2815btrfs_qgroup_rescan(struct btrfs_fs_info *fs_info)
2816{
2817 int ret = 0;
2818 struct btrfs_trans_handle *trans;
2819
2820 ret = qgroup_rescan_init(fs_info, 0, 1);
2821 if (ret)
2822 return ret;
2823
2824 /*
2825 * We have set the rescan_progress to 0, which means no more
2826 * delayed refs will be accounted by btrfs_qgroup_account_ref.
2827 * However, btrfs_qgroup_account_ref may be right after its call
2828 * to btrfs_find_all_roots, in which case it would still do the
2829 * accounting.
2830 * To solve this, we're committing the transaction, which will
2831 * ensure we run all delayed refs and only after that, we are
2832 * going to clear all tracking information for a clean start.
2833 */
2834
2835 trans = btrfs_join_transaction(fs_info->fs_root);
2836 if (IS_ERR(trans)) {
2837 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2838 return PTR_ERR(trans);
2839 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002840 ret = btrfs_commit_transaction(trans);
Jan Schmidtb382a322013-05-28 15:47:24 +00002841 if (ret) {
2842 fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_RESCAN;
2843 return ret;
2844 }
2845
2846 qgroup_rescan_zero_tracking(fs_info);
2847
Qu Wenruofc97fab2014-02-28 10:46:16 +08002848 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2849 &fs_info->qgroup_rescan_work);
Jan Schmidt2f232032013-04-25 16:04:51 +00002850
2851 return 0;
2852}
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002853
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04002854int btrfs_qgroup_wait_for_completion(struct btrfs_fs_info *fs_info,
2855 bool interruptible)
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002856{
2857 int running;
2858 int ret = 0;
2859
2860 mutex_lock(&fs_info->qgroup_rescan_lock);
2861 spin_lock(&fs_info->qgroup_lock);
Jeff Mahoneyd2c609b2016-08-15 12:10:33 -04002862 running = fs_info->qgroup_rescan_running;
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002863 spin_unlock(&fs_info->qgroup_lock);
2864 mutex_unlock(&fs_info->qgroup_rescan_lock);
2865
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04002866 if (!running)
2867 return 0;
2868
2869 if (interruptible)
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002870 ret = wait_for_completion_interruptible(
2871 &fs_info->qgroup_rescan_completion);
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04002872 else
2873 wait_for_completion(&fs_info->qgroup_rescan_completion);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00002874
2875 return ret;
2876}
Jan Schmidtb382a322013-05-28 15:47:24 +00002877
2878/*
2879 * this is only called from open_ctree where we're still single threaded, thus
2880 * locking is omitted here.
2881 */
2882void
2883btrfs_qgroup_rescan_resume(struct btrfs_fs_info *fs_info)
2884{
2885 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN)
Qu Wenruofc97fab2014-02-28 10:46:16 +08002886 btrfs_queue_work(fs_info->qgroup_rescan_workers,
2887 &fs_info->qgroup_rescan_work);
Jan Schmidtb382a322013-05-28 15:47:24 +00002888}
Qu Wenruo52472552015-10-12 16:05:40 +08002889
2890/*
2891 * Reserve qgroup space for range [start, start + len).
2892 *
2893 * This function will either reserve space from related qgroups or doing
2894 * nothing if the range is already reserved.
2895 *
2896 * Return 0 for successful reserve
2897 * Return <0 for error (including -EQUOT)
2898 *
2899 * NOTE: this function may sleep for memory allocation.
Qu Wenruo364ecf32017-02-27 15:10:38 +08002900 * if btrfs_qgroup_reserve_data() is called multiple times with
2901 * same @reserved, caller must ensure when error happens it's OK
2902 * to free *ALL* reserved space.
Qu Wenruo52472552015-10-12 16:05:40 +08002903 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08002904int btrfs_qgroup_reserve_data(struct inode *inode,
2905 struct extent_changeset **reserved_ret, u64 start,
2906 u64 len)
Qu Wenruo52472552015-10-12 16:05:40 +08002907{
2908 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruo52472552015-10-12 16:05:40 +08002909 struct ulist_node *unode;
2910 struct ulist_iterator uiter;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002911 struct extent_changeset *reserved;
2912 u64 orig_reserved;
2913 u64 to_reserve;
Qu Wenruo52472552015-10-12 16:05:40 +08002914 int ret;
2915
Josef Bacikafcdd122016-09-02 15:40:02 -04002916 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) ||
2917 !is_fstree(root->objectid) || len == 0)
Qu Wenruo52472552015-10-12 16:05:40 +08002918 return 0;
2919
Qu Wenruo364ecf32017-02-27 15:10:38 +08002920 /* @reserved parameter is mandatory for qgroup */
2921 if (WARN_ON(!reserved_ret))
2922 return -EINVAL;
2923 if (!*reserved_ret) {
2924 *reserved_ret = extent_changeset_alloc();
2925 if (!*reserved_ret)
2926 return -ENOMEM;
2927 }
2928 reserved = *reserved_ret;
2929 /* Record already reserved space */
2930 orig_reserved = reserved->bytes_changed;
Qu Wenruo52472552015-10-12 16:05:40 +08002931 ret = set_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
Qu Wenruo364ecf32017-02-27 15:10:38 +08002932 start + len -1, EXTENT_QGROUP_RESERVED, reserved);
2933
2934 /* Newly reserved space */
2935 to_reserve = reserved->bytes_changed - orig_reserved;
Qu Wenruo81fb6f72015-09-28 16:57:53 +08002936 trace_btrfs_qgroup_reserve_data(inode, start, len,
Qu Wenruo364ecf32017-02-27 15:10:38 +08002937 to_reserve, QGROUP_RESERVE);
Qu Wenruo52472552015-10-12 16:05:40 +08002938 if (ret < 0)
2939 goto cleanup;
Qu Wenruodba21322017-12-12 15:34:25 +08002940 ret = qgroup_reserve(root, to_reserve, true, BTRFS_QGROUP_RSV_DATA);
Qu Wenruo52472552015-10-12 16:05:40 +08002941 if (ret < 0)
2942 goto cleanup;
2943
Qu Wenruo52472552015-10-12 16:05:40 +08002944 return ret;
2945
2946cleanup:
Qu Wenruo364ecf32017-02-27 15:10:38 +08002947 /* cleanup *ALL* already reserved ranges */
Qu Wenruo52472552015-10-12 16:05:40 +08002948 ULIST_ITER_INIT(&uiter);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002949 while ((unode = ulist_next(&reserved->range_changed, &uiter)))
Qu Wenruo52472552015-10-12 16:05:40 +08002950 clear_extent_bit(&BTRFS_I(inode)->io_tree, unode->val,
David Sterbaae0f1622017-10-31 16:37:52 +01002951 unode->aux, EXTENT_QGROUP_RESERVED, 0, 0, NULL);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002952 extent_changeset_release(reserved);
Qu Wenruo52472552015-10-12 16:05:40 +08002953 return ret;
2954}
Qu Wenruof695fdc2015-10-12 16:28:06 +08002955
Qu Wenruobc42bda2017-02-27 15:10:39 +08002956/* Free ranges specified by @reserved, normally in error path */
2957static int qgroup_free_reserved_data(struct inode *inode,
2958 struct extent_changeset *reserved, u64 start, u64 len)
2959{
2960 struct btrfs_root *root = BTRFS_I(inode)->root;
2961 struct ulist_node *unode;
2962 struct ulist_iterator uiter;
2963 struct extent_changeset changeset;
2964 int freed = 0;
2965 int ret;
2966
2967 extent_changeset_init(&changeset);
2968 len = round_up(start + len, root->fs_info->sectorsize);
2969 start = round_down(start, root->fs_info->sectorsize);
2970
2971 ULIST_ITER_INIT(&uiter);
2972 while ((unode = ulist_next(&reserved->range_changed, &uiter))) {
2973 u64 range_start = unode->val;
2974 /* unode->aux is the inclusive end */
2975 u64 range_len = unode->aux - range_start + 1;
2976 u64 free_start;
2977 u64 free_len;
2978
2979 extent_changeset_release(&changeset);
2980
2981 /* Only free range in range [start, start + len) */
2982 if (range_start >= start + len ||
2983 range_start + range_len <= start)
2984 continue;
2985 free_start = max(range_start, start);
2986 free_len = min(start + len, range_start + range_len) -
2987 free_start;
2988 /*
2989 * TODO: To also modify reserved->ranges_reserved to reflect
2990 * the modification.
2991 *
2992 * However as long as we free qgroup reserved according to
2993 * EXTENT_QGROUP_RESERVED, we won't double free.
2994 * So not need to rush.
2995 */
2996 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_failure_tree,
2997 free_start, free_start + free_len - 1,
2998 EXTENT_QGROUP_RESERVED, &changeset);
2999 if (ret < 0)
3000 goto out;
3001 freed += changeset.bytes_changed;
3002 }
Qu Wenruod4e5c922017-12-12 15:34:23 +08003003 btrfs_qgroup_free_refroot(root->fs_info, root->objectid, freed,
3004 BTRFS_QGROUP_RSV_DATA);
Qu Wenruobc42bda2017-02-27 15:10:39 +08003005 ret = freed;
3006out:
3007 extent_changeset_release(&changeset);
3008 return ret;
3009}
3010
3011static int __btrfs_qgroup_release_data(struct inode *inode,
3012 struct extent_changeset *reserved, u64 start, u64 len,
3013 int free)
Qu Wenruof695fdc2015-10-12 16:28:06 +08003014{
3015 struct extent_changeset changeset;
Qu Wenruo81fb6f72015-09-28 16:57:53 +08003016 int trace_op = QGROUP_RELEASE;
Qu Wenruof695fdc2015-10-12 16:28:06 +08003017 int ret;
3018
Qu Wenruobc42bda2017-02-27 15:10:39 +08003019 /* In release case, we shouldn't have @reserved */
3020 WARN_ON(!free && reserved);
3021 if (free && reserved)
3022 return qgroup_free_reserved_data(inode, reserved, start, len);
Qu Wenruo364ecf32017-02-27 15:10:38 +08003023 extent_changeset_init(&changeset);
Qu Wenruof695fdc2015-10-12 16:28:06 +08003024 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, start,
David Sterbaf734c442016-04-26 23:54:39 +02003025 start + len -1, EXTENT_QGROUP_RESERVED, &changeset);
Qu Wenruof695fdc2015-10-12 16:28:06 +08003026 if (ret < 0)
3027 goto out;
3028
Qu Wenruod51ea5d2017-03-13 15:52:09 +08003029 if (free)
3030 trace_op = QGROUP_FREE;
3031 trace_btrfs_qgroup_release_data(inode, start, len,
3032 changeset.bytes_changed, trace_op);
3033 if (free)
David Sterba0b08e1f2017-02-13 14:24:35 +01003034 btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
3035 BTRFS_I(inode)->root->objectid,
Qu Wenruod4e5c922017-12-12 15:34:23 +08003036 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
Qu Wenruo7bc329c2017-02-27 15:10:36 +08003037 ret = changeset.bytes_changed;
Qu Wenruof695fdc2015-10-12 16:28:06 +08003038out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08003039 extent_changeset_release(&changeset);
Qu Wenruof695fdc2015-10-12 16:28:06 +08003040 return ret;
3041}
3042
3043/*
3044 * Free a reserved space range from io_tree and related qgroups
3045 *
3046 * Should be called when a range of pages get invalidated before reaching disk.
3047 * Or for error cleanup case.
Qu Wenruobc42bda2017-02-27 15:10:39 +08003048 * if @reserved is given, only reserved range in [@start, @start + @len) will
3049 * be freed.
Qu Wenruof695fdc2015-10-12 16:28:06 +08003050 *
3051 * For data written to disk, use btrfs_qgroup_release_data().
3052 *
3053 * NOTE: This function may sleep for memory allocation.
3054 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08003055int btrfs_qgroup_free_data(struct inode *inode,
3056 struct extent_changeset *reserved, u64 start, u64 len)
Qu Wenruof695fdc2015-10-12 16:28:06 +08003057{
Qu Wenruobc42bda2017-02-27 15:10:39 +08003058 return __btrfs_qgroup_release_data(inode, reserved, start, len, 1);
Qu Wenruof695fdc2015-10-12 16:28:06 +08003059}
3060
3061/*
3062 * Release a reserved space range from io_tree only.
3063 *
3064 * Should be called when a range of pages get written to disk and corresponding
3065 * FILE_EXTENT is inserted into corresponding root.
3066 *
3067 * Since new qgroup accounting framework will only update qgroup numbers at
3068 * commit_transaction() time, its reserved space shouldn't be freed from
3069 * related qgroups.
3070 *
3071 * But we should release the range from io_tree, to allow further write to be
3072 * COWed.
3073 *
3074 * NOTE: This function may sleep for memory allocation.
3075 */
3076int btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len)
3077{
Qu Wenruobc42bda2017-02-27 15:10:39 +08003078 return __btrfs_qgroup_release_data(inode, NULL, start, len, 0);
Qu Wenruof695fdc2015-10-12 16:28:06 +08003079}
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003080
Jeff Mahoney003d7c52017-01-25 09:50:33 -05003081int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes,
3082 bool enforce)
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003083{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003084 struct btrfs_fs_info *fs_info = root->fs_info;
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003085 int ret;
3086
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003087 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
Josef Bacikafcdd122016-09-02 15:40:02 -04003088 !is_fstree(root->objectid) || num_bytes == 0)
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003089 return 0;
3090
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003091 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
Qu Wenruo3159fe72017-03-13 15:52:08 +08003092 trace_qgroup_meta_reserve(root, (s64)num_bytes);
Qu Wenruodba21322017-12-12 15:34:25 +08003093 ret = qgroup_reserve(root, num_bytes, enforce, BTRFS_QGROUP_RSV_META);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003094 if (ret < 0)
3095 return ret;
Goldwyn Rodriguesce0dcee2017-03-14 05:25:09 -05003096 atomic64_add(num_bytes, &root->qgroup_meta_rsv);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003097 return ret;
3098}
3099
3100void btrfs_qgroup_free_meta_all(struct btrfs_root *root)
3101{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003102 struct btrfs_fs_info *fs_info = root->fs_info;
Goldwyn Rodriguesce0dcee2017-03-14 05:25:09 -05003103 u64 reserved;
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003104
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003105 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
Josef Bacikafcdd122016-09-02 15:40:02 -04003106 !is_fstree(root->objectid))
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003107 return;
3108
Goldwyn Rodriguesce0dcee2017-03-14 05:25:09 -05003109 reserved = atomic64_xchg(&root->qgroup_meta_rsv, 0);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003110 if (reserved == 0)
3111 return;
Qu Wenruo3159fe72017-03-13 15:52:08 +08003112 trace_qgroup_meta_reserve(root, -(s64)reserved);
Qu Wenruod4e5c922017-12-12 15:34:23 +08003113 btrfs_qgroup_free_refroot(fs_info, root->objectid, reserved,
3114 BTRFS_QGROUP_RSV_META);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003115}
3116
3117void btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes)
3118{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003119 struct btrfs_fs_info *fs_info = root->fs_info;
3120
3121 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
Josef Bacikafcdd122016-09-02 15:40:02 -04003122 !is_fstree(root->objectid))
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003123 return;
3124
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003125 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
Goldwyn Rodriguesce0dcee2017-03-14 05:25:09 -05003126 WARN_ON(atomic64_read(&root->qgroup_meta_rsv) < num_bytes);
3127 atomic64_sub(num_bytes, &root->qgroup_meta_rsv);
Qu Wenruo3159fe72017-03-13 15:52:08 +08003128 trace_qgroup_meta_reserve(root, -(s64)num_bytes);
Qu Wenruod4e5c922017-12-12 15:34:23 +08003129 btrfs_qgroup_free_refroot(fs_info, root->objectid, num_bytes,
3130 BTRFS_QGROUP_RSV_META);
Qu Wenruo55eeaf02015-09-08 17:08:38 +08003131}
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003132
3133/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04003134 * Check qgroup reserved space leaking, normally at destroy inode
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003135 * time
3136 */
3137void btrfs_qgroup_check_reserved_leak(struct inode *inode)
3138{
3139 struct extent_changeset changeset;
3140 struct ulist_node *unode;
3141 struct ulist_iterator iter;
3142 int ret;
3143
Qu Wenruo364ecf32017-02-27 15:10:38 +08003144 extent_changeset_init(&changeset);
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003145 ret = clear_record_extent_bits(&BTRFS_I(inode)->io_tree, 0, (u64)-1,
David Sterbaf734c442016-04-26 23:54:39 +02003146 EXTENT_QGROUP_RESERVED, &changeset);
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003147
3148 WARN_ON(ret < 0);
3149 if (WARN_ON(changeset.bytes_changed)) {
3150 ULIST_ITER_INIT(&iter);
David Sterba53d32352017-02-13 13:42:29 +01003151 while ((unode = ulist_next(&changeset.range_changed, &iter))) {
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003152 btrfs_warn(BTRFS_I(inode)->root->fs_info,
3153 "leaking qgroup reserved space, ino: %lu, start: %llu, end: %llu",
3154 inode->i_ino, unode->val, unode->aux);
3155 }
David Sterba0b08e1f2017-02-13 14:24:35 +01003156 btrfs_qgroup_free_refroot(BTRFS_I(inode)->root->fs_info,
3157 BTRFS_I(inode)->root->objectid,
Qu Wenruod4e5c922017-12-12 15:34:23 +08003158 changeset.bytes_changed, BTRFS_QGROUP_RSV_DATA);
David Sterba0b08e1f2017-02-13 14:24:35 +01003159
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003160 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08003161 extent_changeset_release(&changeset);
Qu Wenruo56fa9d02015-10-13 09:53:10 +08003162}